diff --git a/extensions/memory-lancedb/index.test.ts b/extensions/memory-lancedb/index.test.ts index 98ca81ae71a..b2ed4920bb9 100644 --- a/extensions/memory-lancedb/index.test.ts +++ b/extensions/memory-lancedb/index.test.ts @@ -2408,6 +2408,7 @@ describe("memory plugin e2e", () => { const customTooLong = `I always prefer this style. ${"x".repeat(1600)}`; expect(shouldCapture(customAllowed, { maxChars: 1500 })).toBe(true); expect(shouldCapture(customTooLong, { maxChars: 1500 })).toBe(false); + expect(shouldCapture(defaultTooLong, { maxChars: Number.NaN })).toBe(false); }); test("normalizeRecallQuery trims whitespace and bounds embedding input", () => { @@ -2415,6 +2416,7 @@ describe("memory plugin e2e", () => { "remember the blue mug", ); expect(normalizeRecallQuery(`look up ${"x".repeat(200)}`, 120)).toHaveLength(120); + expect(normalizeRecallQuery(`look up ${"x".repeat(2000)}`, Number.NaN)).toHaveLength(1000); }); test("normalizeEmbeddingVector accepts float arrays and base64 float32 responses", () => { diff --git a/extensions/memory-lancedb/index.ts b/extensions/memory-lancedb/index.ts index a084767b3f5..dd12ccf1d0f 100644 --- a/extensions/memory-lancedb/index.ts +++ b/extensions/memory-lancedb/index.ts @@ -139,10 +139,16 @@ export function normalizeRecallQuery( maxChars: number = DEFAULT_RECALL_MAX_CHARS, ): string { const normalized = text.replace(/\s+/g, " ").trim(); - const limit = Math.max(0, Math.floor(maxChars)); + const limit = normalizeMaxChars(maxChars, DEFAULT_RECALL_MAX_CHARS); return normalized.length > limit ? truncateUtf16Safe(normalized, limit).trimEnd() : normalized; } +function normalizeMaxChars(value: number | undefined, fallback: number): number { + return typeof value === "number" && Number.isFinite(value) + ? Math.max(0, Math.floor(value)) + : fallback; +} + function messageFingerprint(message: unknown): string { const msgObj = asRecord(message); if (!msgObj) { @@ -572,7 +578,7 @@ export function shouldCapture( text: string, options?: { customTriggers?: string[]; maxChars?: number }, ): boolean { - const maxChars = options?.maxChars ?? DEFAULT_CAPTURE_MAX_CHARS; + const maxChars = normalizeMaxChars(options?.maxChars, DEFAULT_CAPTURE_MAX_CHARS); if (text.length > maxChars) { return false; }