fix(ts-sdk): prevent hallucinated memories on empty messages payload … (#5613)
This commit is contained in:
@@ -253,6 +253,11 @@ export default class MemoryClient {
|
|||||||
messages: Array<Message>,
|
messages: Array<Message>,
|
||||||
options: AddMemoryOptions & Record<string, any> = {},
|
options: AddMemoryOptions & Record<string, any> = {},
|
||||||
): Promise<Array<Memory>> {
|
): Promise<Array<Memory>> {
|
||||||
|
// Tightly scoped validation guard to resolve #5465
|
||||||
|
if (!messages || (Array.isArray(messages) && messages.length === 0)) {
|
||||||
|
throw new Error("Cannot process an empty messages payload.");
|
||||||
|
}
|
||||||
|
|
||||||
if (this.telemetryId === "") await this.ping();
|
if (this.telemetryId === "") await this.ping();
|
||||||
|
|
||||||
const payload = this._preparePayload(messages, options);
|
const payload = this._preparePayload(messages, options);
|
||||||
|
|||||||
@@ -59,16 +59,15 @@ describe("MemoryClient - add()", () => {
|
|||||||
expect(getFetchBody(call!).user_id).toBe("user_1");
|
expect(getFetchBody(call!).user_id).toBe("user_1");
|
||||||
});
|
});
|
||||||
|
|
||||||
test("sends empty messages array without crashing", async () => {
|
test("throws an error when given an empty messages array", async () => {
|
||||||
const extra = new Map<string, { status: number; body: unknown }>();
|
setupMockFetch();
|
||||||
extra.set("/v3/memories/add/", { status: 200, body: [] });
|
|
||||||
const mock = setupMockFetch(extra);
|
|
||||||
|
|
||||||
const client = new MemoryClient({ apiKey: TEST_API_KEY });
|
const client = new MemoryClient({ apiKey: TEST_API_KEY });
|
||||||
await client.add([], { userId: "u1" });
|
|
||||||
|
|
||||||
const call = findFetchCall(mock, "/v3/memories/add/", "POST");
|
//Asserts that the validation guard catches the empty input early
|
||||||
expect(getFetchBody(call!).messages).toEqual([]);
|
await expect(client.add([], { userId: "u1" })).rejects.toThrow(
|
||||||
|
"Cannot process an empty messages payload.",
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user