fix(memory): default non-finite lancedb text limits

This commit is contained in:
Peter Steinberger
2026-05-29 00:23:43 -04:00
parent 21b33bd04d
commit 27cd18748f
2 changed files with 10 additions and 2 deletions

View File

@@ -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", () => {

View File

@@ -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;
}