repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | /// <reference types="jest" /> |
| #2 | import { Memory } from "../src"; |
| #3 | import { MemoryItem, SearchResult } from "../src/types"; |
| #4 | import dotenv from "dotenv"; |
| #5 | |
| #6 | dotenv.config(); |
| #7 | |
| #8 | jest.setTimeout(30000); // Increase timeout to 30 seconds |
| #9 | |
| #10 | describe("Memory Class", () => { |
| #11 | let memory: Memory; |
| #12 | const userId = |
| #13 | Math.random().toString(36).substring(2, 15) + |
| #14 | Math.random().toString(36).substring(2, 15); |
| #15 | |
| #16 | beforeEach(async () => { |
| #17 | // Initialize with default configuration |
| #18 | memory = new Memory({ |
| #19 | version: "v1.1", |
| #20 | embedder: { |
| #21 | provider: "openai", |
| #22 | config: { |
| #23 | apiKey: process.env.OPENAI_API_KEY || "", |
| #24 | model: "text-embedding-3-small", |
| #25 | }, |
| #26 | }, |
| #27 | vectorStore: { |
| #28 | provider: "memory", |
| #29 | config: { |
| #30 | collectionName: "test-memories", |
| #31 | dimension: 1536, |
| #32 | }, |
| #33 | }, |
| #34 | llm: { |
| #35 | provider: "openai", |
| #36 | config: { |
| #37 | apiKey: process.env.OPENAI_API_KEY || "", |
| #38 | model: "gpt-4-turbo-preview", |
| #39 | }, |
| #40 | }, |
| #41 | historyDbPath: ":memory:", // Use in-memory SQLite for tests |
| #42 | }); |
| #43 | // Reset all memories before each test |
| #44 | await memory.reset(); |
| #45 | }); |
| #46 | |
| #47 | afterEach(async () => { |
| #48 | // Clean up after each test |
| #49 | await memory.reset(); |
| #50 | }); |
| #51 | |
| #52 | describe("Basic Memory Operations", () => { |
| #53 | it("should add a single memory", async () => { |
| #54 | const result = (await memory.add( |
| #55 | "Hi, my name is John and I am a software engineer.", |
| #56 | userId, |
| #57 | )) as SearchResult; |
| #58 | |
| #59 | expect(result).toBeDefined(); |
| #60 | expect(result.results).toBeDefined(); |
| #61 | expect(Array.isArray(result.results)).toBe(true); |
| #62 | expect(result.results.length).toBeGreaterThan(0); |
| #63 | expect(result.results[0]?.id).toBeDefined(); |
| #64 | }); |
| #65 | |
| #66 | it("should add multiple messages", async () => { |
| #67 | const messages = [ |
| #68 | { role: "user", content: "What is your favorite city?" }, |
| #69 | { role: "assistant", content: "I love Paris, it is my favorite city." }, |
| #70 | ]; |
| #71 | |
| #72 | const result = (await memory.add(messages, userId)) as SearchResult; |
| #73 | |
| #74 | expect(result).toBeDefined(); |
| #75 | expect(result.results).toBeDefined(); |
| #76 | expect(Array.isArray(result.results)).toBe(true); |
| #77 | expect(result.results.length).toBeGreaterThan(0); |
| #78 | }); |
| #79 | |
| #80 | it("should get a single memory", async () => { |
| #81 | // First add a memory |
| #82 | const addResult = (await memory.add( |
| #83 | "I am a big advocate of using AI to make the world a better place", |
| #84 | userId, |
| #85 | )) as SearchResult; |
| #86 | |
| #87 | if (!addResult.results?.[0]?.id) { |
| #88 | throw new Error("Failed to create test memory"); |
| #89 | } |
| #90 | |
| #91 | const memoryId = addResult.results[0].id; |
| #92 | const result = (await memory.get(memoryId)) as MemoryItem; |
| #93 | |
| #94 | expect(result).toBeDefined(); |
| #95 | expect(result.id).toBe(memoryId); |
| #96 | expect(result.memory).toBeDefined(); |
| #97 | expect(typeof result.memory).toBe("string"); |
| #98 | }); |
| #99 | |
| #100 | it("should update a memory", async () => { |
| #101 | // First add a memory |
| #102 | const addResult = (await memory.add( |
| #103 | "I love speaking foreign languages especially Spanish", |
| #104 | userId, |
| #105 | )) as SearchResult; |
| #106 | |
| #107 | if (!addResult.results?.[0]?.id) { |
| #108 | throw new Error("Failed to create test memory"); |
| #109 | } |
| #110 | |
| #111 | const memoryId = addResult.results[0].id; |
| #112 | const updatedContent = "Updated content"; |
| #113 | const result = await memory.update(memoryId, updatedContent); |
| #114 | |
| #115 | expect(result).toBeDefined(); |
| #116 | expect(result.message).toBe("Memory updated successfully!"); |
| #117 | |
| #118 | // Verify the update by getting the memory |
| #119 | const updatedMemory = (await memory.get(memoryId)) as MemoryItem; |
| #120 | expect(updatedMemory.memory).toBe(updatedContent); |
| #121 | }); |
| #122 | |
| #123 | it("should get all memories for a user", async () => { |
| #124 | // Add a few memories |
| #125 | await memory.add("I love visiting new places in the winters", userId); |
| #126 | await memory.add("I like to rule the world", userId); |
| #127 | |
| #128 | const result = (await memory.getAll(userId)) as SearchResult; |
| #129 | |
| #130 | expect(result).toBeDefined(); |
| #131 | expect(Array.isArray(result.results)).toBe(true); |
| #132 | expect(result.results.length).toBeGreaterThanOrEqual(2); |
| #133 | }); |
| #134 | |
| #135 | it("should search memories", async () => { |
| #136 | // Add some test memories |
| #137 | await memory.add("I love programming in Python", userId); |
| #138 | await memory.add("JavaScript is my favorite language", userId); |
| #139 | |
| #140 | const result = (await memory.search( |
| #141 | "What programming languages do I know?", |
| #142 | userId, |
| #143 | )) as SearchResult; |
| #144 | |
| #145 | expect(result).toBeDefined(); |
| #146 | expect(Array.isArray(result.results)).toBe(true); |
| #147 | expect(result.results.length).toBeGreaterThan(0); |
| #148 | }); |
| #149 | |
| #150 | it("should get memory history", async () => { |
| #151 | // Add and update a memory to create history |
| #152 | const addResult = (await memory.add( |
| #153 | "I like swimming in warm water", |
| #154 | userId, |
| #155 | )) as SearchResult; |
| #156 | |
| #157 | if (!addResult.results?.[0]?.id) { |
| #158 | throw new Error("Failed to create test memory"); |
| #159 | } |
| #160 | |
| #161 | const memoryId = addResult.results[0].id; |
| #162 | await memory.update(memoryId, "Updated content"); |
| #163 | |
| #164 | const history = await memory.history(memoryId); |
| #165 | |
| #166 | expect(history).toBeDefined(); |
| #167 | expect(Array.isArray(history)).toBe(true); |
| #168 | expect(history.length).toBeGreaterThan(0); |
| #169 | }); |
| #170 | |
| #171 | it("should delete a memory", async () => { |
| #172 | // First add a memory |
| #173 | const addResult = (await memory.add( |
| #174 | "I love to drink vodka in summers", |
| #175 | userId, |
| #176 | )) as SearchResult; |
| #177 | |
| #178 | if (!addResult.results?.[0]?.id) { |
| #179 | throw new Error("Failed to create test memory"); |
| #180 | } |
| #181 | |
| #182 | const memoryId = addResult.results[0].id; |
| #183 | |
| #184 | // Delete the memory |
| #185 | await memory.delete(memoryId); |
| #186 | |
| #187 | // Try to get the deleted memory - should throw or return null |
| #188 | const result = await memory.get(memoryId); |
| #189 | expect(result).toBeNull(); |
| #190 | }); |
| #191 | }); |
| #192 | |
| #193 | describe("Memory with Custom Configuration", () => { |
| #194 | let customMemory: Memory; |
| #195 | |
| #196 | beforeEach(() => { |
| #197 | customMemory = new Memory({ |
| #198 | version: "v1.1", |
| #199 | embedder: { |
| #200 | provider: "openai", |
| #201 | config: { |
| #202 | apiKey: process.env.OPENAI_API_KEY || "", |
| #203 | model: "text-embedding-3-small", |
| #204 | }, |
| #205 | }, |
| #206 | vectorStore: { |
| #207 | provider: "memory", |
| #208 | config: { |
| #209 | collectionName: "test-memories", |
| #210 | dimension: 1536, |
| #211 | }, |
| #212 | }, |
| #213 | llm: { |
| #214 | provider: "openai", |
| #215 | config: { |
| #216 | apiKey: process.env.OPENAI_API_KEY || "", |
| #217 | model: "gpt-4-turbo-preview", |
| #218 | }, |
| #219 | }, |
| #220 | historyDbPath: ":memory:", // Use in-memory SQLite for tests |
| #221 | }); |
| #222 | }); |
| #223 | |
| #224 | afterEach(async () => { |
| #225 | await customMemory.reset(); |
| #226 | }); |
| #227 | |
| #228 | it("should work with custom configuration", async () => { |
| #229 | const result = (await customMemory.add( |
| #230 | "I love programming in Python", |
| #231 | userId, |
| #232 | )) as SearchResult; |
| #233 | |
| #234 | expect(result).toBeDefined(); |
| #235 | expect(result.results).toBeDefined(); |
| #236 | expect(Array.isArray(result.results)).toBe(true); |
| #237 | expect(result.results.length).toBeGreaterThan(0); |
| #238 | }); |
| #239 | |
| #240 | it("should perform semantic search with custom embeddings", async () => { |
| #241 | // Add test memories |
| #242 | await customMemory.add("The weather in London is rainy today", userId); |
| #243 | await customMemory.add("The temperature in Paris is 25 degrees", userId); |
| #244 | |
| #245 | const result = (await customMemory.search( |
| #246 | "What is the weather like?", |
| #247 | userId, |
| #248 | )) as SearchResult; |
| #249 | |
| #250 | expect(result).toBeDefined(); |
| #251 | expect(Array.isArray(result.results)).toBe(true); |
| #252 | // Results should be ordered by relevance |
| #253 | expect(result.results.length).toBeGreaterThan(0); |
| #254 | }); |
| #255 | }); |
| #256 | }); |
| #257 |