fix(ts-oss): reject empty/blank messages in Memory.add() to prevent hallucinated memories (#5545)
This commit is contained in:
@@ -619,6 +619,25 @@ export class Memory {
|
|||||||
"messages is required and cannot be undefined or null. Provide a string or array of messages.",
|
"messages is required and cannot be undefined or null. Provide a string or array of messages.",
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
if (Array.isArray(messages)) {
|
||||||
|
if (messages.length === 0) {
|
||||||
|
throw new Error(
|
||||||
|
"messages array cannot be empty. Provide at least one message with non-empty content.",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const allBlank = messages.every(
|
||||||
|
(m) => typeof m.content === "string" && m.content.trim() === "",
|
||||||
|
);
|
||||||
|
if (allBlank) {
|
||||||
|
throw new Error(
|
||||||
|
"messages array cannot contain only blank content. Provide at least one message with non-empty content.",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else if (messages.trim() === "") {
|
||||||
|
throw new Error(
|
||||||
|
"messages string cannot be empty. Provide non-empty content.",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const temporalUsageNotice = detectTemporalUsageFromMetadata(
|
const temporalUsageNotice = detectTemporalUsageFromMetadata(
|
||||||
config?.metadata,
|
config?.metadata,
|
||||||
|
|||||||
@@ -84,6 +84,24 @@ describe("Memory Input Validation", () => {
|
|||||||
memory.add(null, { userId: testUserId }),
|
memory.add(null, { userId: testUserId }),
|
||||||
).rejects.toThrow("messages is required");
|
).rejects.toThrow("messages is required");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should throw error when messages is an empty array", async () => {
|
||||||
|
await expect(memory.add([], { userId: testUserId })).rejects.toThrow(
|
||||||
|
"messages array cannot be empty",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should throw error when messages array contains only blank content", async () => {
|
||||||
|
await expect(
|
||||||
|
memory.add([{ role: "user", content: " " }], { userId: testUserId }),
|
||||||
|
).rejects.toThrow("messages array cannot contain only blank content");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should throw error when messages is an empty string", async () => {
|
||||||
|
await expect(memory.add(" ", { userId: testUserId })).rejects.toThrow(
|
||||||
|
"messages string cannot be empty",
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("search() threshold validation", () => {
|
describe("search() threshold validation", () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user