test_memories.mjs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { activate } from "./index.js";
  2. const plugin = {};
  3. activate(plugin);
  4. async function run() {
  5. const userId = "golem-openclaw-test";
  6. const timestamp = new Date().toISOString();
  7. const text = `openclaw-mem0-python plugin test at ${timestamp}`;
  8. const baseUrl = process.env.MEM0_BASE_URL || "http://192.168.0.200:8420";
  9. console.log("Writing memory...");
  10. const writeResult = await plugin.write({ text, userId });
  11. console.log("Write result:", writeResult);
  12. console.log("Searching memory...");
  13. const searchResult = await plugin.search({ query: "openclaw-mem0-python", userId });
  14. console.log("Search result:", searchResult);
  15. console.log("Reading recent memories...");
  16. const readResult = await plugin.read({ userId, limit: 3 });
  17. console.log("Read result:", readResult);
  18. console.log("Listing knowledge sources...");
  19. const sources = await plugin.listKnowledgeSources();
  20. console.log("Sources:", sources);
  21. const firstSource = sources?.sources?.[0]?.source_file;
  22. if (firstSource) {
  23. console.log("Describing knowledge book:", firstSource);
  24. const describe = await plugin.describeKnowledgeBook({ sourceFile: firstSource });
  25. console.log("Describe:", describe.summary);
  26. console.log("Searching knowledge book...");
  27. const bookSearch = await plugin.searchKnowledgeBook({
  28. query: describe?.hintQuery || "introduction",
  29. sourceFile: firstSource,
  30. limit: 3,
  31. });
  32. console.log("Book search:", bookSearch);
  33. } else {
  34. console.log("No knowledge sources found; skipping book tests.");
  35. }
  36. console.log("Cleaning up test memories...");
  37. const allRes = await fetch(`${baseUrl}/memories/all`, {
  38. method: "POST",
  39. headers: { "Content-Type": "application/json" },
  40. body: JSON.stringify({ user_id: userId }),
  41. }).then((r) => r.json());
  42. const ids = (allRes?.results || []).map((r) => r.id).filter(Boolean);
  43. for (const id of ids) {
  44. await fetch(`${baseUrl}/memory/${id}`, {
  45. method: "DELETE",
  46. headers: { "Content-Type": "application/json" },
  47. body: JSON.stringify({ collection: "conversational" }),
  48. });
  49. }
  50. console.log(`Cleanup done (${ids.length} deleted).`);
  51. }
  52. run().catch((err) => {
  53. console.error("Test failed:", err);
  54. process.exit(1);
  55. });