|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import os from "node:os"; |
| 3 | +import path from "node:path"; |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 5 | +import { getMemorySearchManager, type MemoryIndexManager } from "./index.js"; |
| 6 | + |
| 7 | +const embedBatch = vi.fn(async (texts: string[]) => texts.map(() => [0, 1, 0])); |
| 8 | +const embedQuery = vi.fn(async () => [0, 1, 0]); |
| 9 | + |
| 10 | +vi.mock("./embeddings.js", () => ({ |
| 11 | + createEmbeddingProvider: async () => ({ |
| 12 | + requestedProvider: "openai", |
| 13 | + provider: { |
| 14 | + id: "mock", |
| 15 | + model: "mock-embed", |
| 16 | + maxInputTokens: 8192, |
| 17 | + embedQuery, |
| 18 | + embedBatch, |
| 19 | + }, |
| 20 | + }), |
| 21 | +})); |
| 22 | + |
| 23 | +describe("memory embedding token limits", () => { |
| 24 | + let workspaceDir: string; |
| 25 | + let indexPath: string; |
| 26 | + let manager: MemoryIndexManager | null = null; |
| 27 | + |
| 28 | + beforeEach(async () => { |
| 29 | + embedBatch.mockReset(); |
| 30 | + embedQuery.mockReset(); |
| 31 | + embedBatch.mockImplementation(async (texts: string[]) => texts.map(() => [0, 1, 0])); |
| 32 | + embedQuery.mockImplementation(async () => [0, 1, 0]); |
| 33 | + workspaceDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-mem-token-")); |
| 34 | + indexPath = path.join(workspaceDir, "index.sqlite"); |
| 35 | + await fs.mkdir(path.join(workspaceDir, "memory")); |
| 36 | + }); |
| 37 | + |
| 38 | + afterEach(async () => { |
| 39 | + if (manager) { |
| 40 | + await manager.close(); |
| 41 | + manager = null; |
| 42 | + } |
| 43 | + await fs.rm(workspaceDir, { recursive: true, force: true }); |
| 44 | + }); |
| 45 | + |
| 46 | + it("splits oversized chunks so each embedding input stays <= 8192 UTF-8 bytes", async () => { |
| 47 | + const content = "x".repeat(9500); |
| 48 | + await fs.writeFile(path.join(workspaceDir, "memory", "2026-01-09.md"), content); |
| 49 | + |
| 50 | + const cfg = { |
| 51 | + agents: { |
| 52 | + defaults: { |
| 53 | + workspace: workspaceDir, |
| 54 | + memorySearch: { |
| 55 | + provider: "openai", |
| 56 | + model: "mock-embed", |
| 57 | + store: { path: indexPath }, |
| 58 | + chunking: { tokens: 10_000, overlap: 0 }, |
| 59 | + sync: { watch: false, onSessionStart: false, onSearch: false }, |
| 60 | + query: { minScore: 0 }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + list: [{ id: "main", default: true }], |
| 64 | + }, |
| 65 | + }; |
| 66 | + |
| 67 | + const result = await getMemorySearchManager({ cfg, agentId: "main" }); |
| 68 | + expect(result.manager).not.toBeNull(); |
| 69 | + if (!result.manager) { |
| 70 | + throw new Error("manager missing"); |
| 71 | + } |
| 72 | + manager = result.manager; |
| 73 | + await manager.sync({ force: true }); |
| 74 | + |
| 75 | + const inputs = embedBatch.mock.calls.flatMap((call) => call[0] ?? []); |
| 76 | + expect(inputs.length).toBeGreaterThan(1); |
| 77 | + expect( |
| 78 | + Math.max(...inputs.map((input) => Buffer.byteLength(input, "utf8"))), |
| 79 | + ).toBeLessThanOrEqual(8192); |
| 80 | + }); |
| 81 | + |
| 82 | + it("uses UTF-8 byte estimates when batching multibyte chunks", async () => { |
| 83 | + const line = "😀".repeat(1800); |
| 84 | + const content = `${line}\n${line}\n${line}`; |
| 85 | + await fs.writeFile(path.join(workspaceDir, "memory", "2026-01-10.md"), content); |
| 86 | + |
| 87 | + const cfg = { |
| 88 | + agents: { |
| 89 | + defaults: { |
| 90 | + workspace: workspaceDir, |
| 91 | + memorySearch: { |
| 92 | + provider: "openai", |
| 93 | + model: "mock-embed", |
| 94 | + store: { path: indexPath }, |
| 95 | + chunking: { tokens: 1000, overlap: 0 }, |
| 96 | + sync: { watch: false, onSessionStart: false, onSearch: false }, |
| 97 | + query: { minScore: 0 }, |
| 98 | + }, |
| 99 | + }, |
| 100 | + list: [{ id: "main", default: true }], |
| 101 | + }, |
| 102 | + }; |
| 103 | + |
| 104 | + const result = await getMemorySearchManager({ cfg, agentId: "main" }); |
| 105 | + expect(result.manager).not.toBeNull(); |
| 106 | + if (!result.manager) { |
| 107 | + throw new Error("manager missing"); |
| 108 | + } |
| 109 | + manager = result.manager; |
| 110 | + await manager.sync({ force: true }); |
| 111 | + |
| 112 | + const batchSizes = embedBatch.mock.calls.map( |
| 113 | + (call) => (call[0] as string[] | undefined)?.length ?? 0, |
| 114 | + ); |
| 115 | + expect(batchSizes.length).toBe(3); |
| 116 | + expect(batchSizes.every((size) => size === 1)).toBe(true); |
| 117 | + const inputs = embedBatch.mock.calls.flatMap((call) => call[0] ?? []); |
| 118 | + expect(inputs.every((input) => Buffer.byteLength(input, "utf8") <= 8192)).toBe(true); |
| 119 | + }); |
| 120 | +}); |
0 commit comments