@cloudflare/ai-chat 0.0.7 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +105 -0
- package/README.md +225 -459
- package/dist/ai-chat-v5-migration.d.ts +9 -9
- package/dist/ai-chat-v5-migration.js +25 -4
- package/dist/ai-chat-v5-migration.js.map +1 -1
- package/dist/experimental/forever.d.ts +29 -0
- package/dist/experimental/forever.js +55 -0
- package/dist/experimental/forever.js.map +1 -0
- package/dist/index.d.ts +126 -74
- package/dist/index.js +753 -632
- package/dist/index.js.map +1 -1
- package/dist/message-builder-Bwc_Umye.js +231 -0
- package/dist/message-builder-Bwc_Umye.js.map +1 -0
- package/dist/react.d.ts +27 -9
- package/dist/react.js +248 -259
- package/dist/react.js.map +1 -1
- package/dist/resumable-stream-DqXxQVjf.d.ts +103 -0
- package/dist/types.d.ts +16 -4
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -1
- package/e2e/chat.spec.ts +1703 -0
- package/e2e/client-tools.spec.ts +504 -0
- package/e2e/llm.spec.ts +637 -0
- package/e2e/playwright.config.ts +24 -0
- package/e2e/worker.ts +176 -0
- package/e2e/wrangler.jsonc +23 -0
- package/package.json +12 -5
- package/scripts/build.ts +2 -1
- package/src/ai-chat-v5-migration.ts +32 -5
- package/src/experimental/forever.ts +66 -0
- package/src/index.ts +850 -1132
- package/src/message-builder.ts +384 -0
- package/src/react-tests/use-agent-chat.test.tsx +738 -2
- package/src/react.tsx +396 -462
- package/src/resumable-stream.ts +441 -0
- package/src/tests/chat-context.test.ts +1 -1
- package/src/tests/chat-persistence.test.ts +5 -5
- package/src/tests/clear-history.test.ts +132 -0
- package/src/tests/client-tool-duplicate-message.test.ts +457 -14
- package/src/tests/client-tools-broadcast.test.ts +2 -2
- package/src/tests/client-tools-continuation.test.ts +314 -0
- package/src/tests/client-tools-reconnect.test.ts +143 -0
- package/src/tests/custom-body-continuation.test.ts +309 -0
- package/src/tests/custom-body.test.ts +186 -0
- package/src/tests/destroy.test.ts +57 -0
- package/src/tests/get-messages-endpoint.test.ts +79 -0
- package/src/tests/max-persisted-messages.test.ts +201 -0
- package/src/tests/merge-server-state.test.ts +106 -0
- package/src/tests/message-builder.test.ts +808 -0
- package/src/tests/migration.test.ts +282 -0
- package/src/tests/non-sse-response.test.ts +2 -2
- package/src/tests/onfinish-cleanup.test.ts +362 -0
- package/src/tests/plaintext-response.test.ts +87 -0
- package/src/tests/resumable-streaming.test.ts +220 -14
- package/src/tests/row-size-guard.test.ts +374 -0
- package/src/tests/sanitize-messages.test.ts +215 -0
- package/src/tests/validate-messages.test.ts +269 -0
- package/src/tests/worker.ts +85 -23
- package/src/types.ts +17 -1
- package/src/ws-chat-transport.ts +203 -0
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
import { env } from "cloudflare:test";
|
|
2
|
+
import { describe, it, expect } from "vitest";
|
|
3
|
+
import type { UIMessage as ChatMessage } from "ai";
|
|
4
|
+
import { connectChatWS } from "./test-utils";
|
|
5
|
+
import { getAgentByName } from "agents";
|
|
6
|
+
import { MessageType } from "../types";
|
|
7
|
+
|
|
8
|
+
describe("Row Size Guard and Incremental Persistence", () => {
|
|
9
|
+
describe("Incremental persistence", () => {
|
|
10
|
+
it("persists new messages and skips unchanged ones on second call", async () => {
|
|
11
|
+
const room = crypto.randomUUID();
|
|
12
|
+
const { ws } = await connectChatWS(`/agents/test-chat-agent/${room}`);
|
|
13
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
14
|
+
|
|
15
|
+
const agentStub = await getAgentByName(env.TestChatAgent, room);
|
|
16
|
+
|
|
17
|
+
// Persist two messages
|
|
18
|
+
const messages: ChatMessage[] = [
|
|
19
|
+
{
|
|
20
|
+
id: "inc-1",
|
|
21
|
+
role: "user",
|
|
22
|
+
parts: [{ type: "text", text: "Hello" }]
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
id: "inc-2",
|
|
26
|
+
role: "assistant",
|
|
27
|
+
parts: [{ type: "text", text: "Hi there!" }]
|
|
28
|
+
}
|
|
29
|
+
];
|
|
30
|
+
|
|
31
|
+
await agentStub.persistMessages(messages);
|
|
32
|
+
|
|
33
|
+
let persisted = (await agentStub.getPersistedMessages()) as ChatMessage[];
|
|
34
|
+
expect(persisted.length).toBe(2);
|
|
35
|
+
|
|
36
|
+
// Persist the same messages again -- should be a no-op in SQL
|
|
37
|
+
// (we can't directly observe SQL write count, but we can verify
|
|
38
|
+
// the messages are still correct)
|
|
39
|
+
await agentStub.persistMessages(messages);
|
|
40
|
+
|
|
41
|
+
persisted = (await agentStub.getPersistedMessages()) as ChatMessage[];
|
|
42
|
+
expect(persisted.length).toBe(2);
|
|
43
|
+
expect(persisted[0].id).toBe("inc-1");
|
|
44
|
+
expect(persisted[1].id).toBe("inc-2");
|
|
45
|
+
|
|
46
|
+
ws.close(1000);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("persists modified messages when content changes", async () => {
|
|
50
|
+
const room = crypto.randomUUID();
|
|
51
|
+
const { ws } = await connectChatWS(`/agents/test-chat-agent/${room}`);
|
|
52
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
53
|
+
|
|
54
|
+
const agentStub = await getAgentByName(env.TestChatAgent, room);
|
|
55
|
+
|
|
56
|
+
// Persist initial message
|
|
57
|
+
await agentStub.persistMessages([
|
|
58
|
+
{
|
|
59
|
+
id: "mod-1",
|
|
60
|
+
role: "assistant",
|
|
61
|
+
parts: [{ type: "text", text: "Original" }]
|
|
62
|
+
}
|
|
63
|
+
]);
|
|
64
|
+
|
|
65
|
+
// Modify the message
|
|
66
|
+
await agentStub.persistMessages([
|
|
67
|
+
{
|
|
68
|
+
id: "mod-1",
|
|
69
|
+
role: "assistant",
|
|
70
|
+
parts: [{ type: "text", text: "Updated content" }]
|
|
71
|
+
}
|
|
72
|
+
]);
|
|
73
|
+
|
|
74
|
+
const persisted =
|
|
75
|
+
(await agentStub.getPersistedMessages()) as ChatMessage[];
|
|
76
|
+
expect(persisted.length).toBe(1);
|
|
77
|
+
const textPart = persisted[0].parts[0] as { text: string };
|
|
78
|
+
expect(textPart.text).toBe("Updated content");
|
|
79
|
+
|
|
80
|
+
ws.close(1000);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("cache is cleared on chat clear", async () => {
|
|
84
|
+
const room = crypto.randomUUID();
|
|
85
|
+
const { ws } = await connectChatWS(`/agents/test-chat-agent/${room}`);
|
|
86
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
87
|
+
|
|
88
|
+
const agentStub = await getAgentByName(env.TestChatAgent, room);
|
|
89
|
+
|
|
90
|
+
// Persist a message
|
|
91
|
+
await agentStub.persistMessages([
|
|
92
|
+
{
|
|
93
|
+
id: "clear-cache-1",
|
|
94
|
+
role: "user",
|
|
95
|
+
parts: [{ type: "text", text: "Hello" }]
|
|
96
|
+
}
|
|
97
|
+
]);
|
|
98
|
+
|
|
99
|
+
// Clear via WebSocket
|
|
100
|
+
ws.send(JSON.stringify({ type: MessageType.CF_AGENT_CHAT_CLEAR }));
|
|
101
|
+
await new Promise((r) => setTimeout(r, 100));
|
|
102
|
+
|
|
103
|
+
// Verify cleared
|
|
104
|
+
const persisted =
|
|
105
|
+
(await agentStub.getPersistedMessages()) as ChatMessage[];
|
|
106
|
+
expect(persisted.length).toBe(0);
|
|
107
|
+
|
|
108
|
+
// Persist a new message with the same ID -- should succeed
|
|
109
|
+
// (cache was cleared, so it won't skip)
|
|
110
|
+
await agentStub.persistMessages([
|
|
111
|
+
{
|
|
112
|
+
id: "clear-cache-1",
|
|
113
|
+
role: "user",
|
|
114
|
+
parts: [{ type: "text", text: "New message same ID" }]
|
|
115
|
+
}
|
|
116
|
+
]);
|
|
117
|
+
|
|
118
|
+
const afterPersist =
|
|
119
|
+
(await agentStub.getPersistedMessages()) as ChatMessage[];
|
|
120
|
+
expect(afterPersist.length).toBe(1);
|
|
121
|
+
expect((afterPersist[0].parts[0] as { text: string }).text).toBe(
|
|
122
|
+
"New message same ID"
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
ws.close(1000);
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
describe("Row size enforcement", () => {
|
|
130
|
+
it("messages under 1.8MB pass through unchanged", async () => {
|
|
131
|
+
const room = crypto.randomUUID();
|
|
132
|
+
const { ws } = await connectChatWS(`/agents/test-chat-agent/${room}`);
|
|
133
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
134
|
+
|
|
135
|
+
const agentStub = await getAgentByName(env.TestChatAgent, room);
|
|
136
|
+
|
|
137
|
+
// Create a message with a moderately large tool output (50KB)
|
|
138
|
+
const toolOutput = "A".repeat(50_000);
|
|
139
|
+
const message: ChatMessage = {
|
|
140
|
+
id: "size-ok",
|
|
141
|
+
role: "assistant",
|
|
142
|
+
parts: [
|
|
143
|
+
{
|
|
144
|
+
type: "tool-bigTool",
|
|
145
|
+
toolCallId: "call_ok",
|
|
146
|
+
state: "output-available",
|
|
147
|
+
input: {},
|
|
148
|
+
output: toolOutput
|
|
149
|
+
}
|
|
150
|
+
] as ChatMessage["parts"]
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
await agentStub.persistMessages([message]);
|
|
154
|
+
|
|
155
|
+
const persisted =
|
|
156
|
+
(await agentStub.getPersistedMessages()) as ChatMessage[];
|
|
157
|
+
expect(persisted.length).toBe(1);
|
|
158
|
+
|
|
159
|
+
// Output should be preserved at full fidelity (under 1.8MB)
|
|
160
|
+
const part = persisted[0].parts[0] as { output: unknown };
|
|
161
|
+
expect(part.output).toBe(toolOutput);
|
|
162
|
+
|
|
163
|
+
ws.close(1000);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it("messages over 1.8MB have tool outputs compacted", async () => {
|
|
167
|
+
const room = crypto.randomUUID();
|
|
168
|
+
const { ws } = await connectChatWS(`/agents/test-chat-agent/${room}`);
|
|
169
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
170
|
+
|
|
171
|
+
const agentStub = await getAgentByName(env.TestChatAgent, room);
|
|
172
|
+
|
|
173
|
+
// Create a message with a huge tool output that pushes over 1.8MB
|
|
174
|
+
const hugeOutput = "X".repeat(1_900_000);
|
|
175
|
+
const message: ChatMessage = {
|
|
176
|
+
id: "size-big",
|
|
177
|
+
role: "assistant",
|
|
178
|
+
parts: [
|
|
179
|
+
{
|
|
180
|
+
type: "tool-hugeTool",
|
|
181
|
+
toolCallId: "call_huge",
|
|
182
|
+
state: "output-available",
|
|
183
|
+
input: { query: "big data" },
|
|
184
|
+
output: hugeOutput
|
|
185
|
+
}
|
|
186
|
+
] as ChatMessage["parts"]
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
// Should NOT throw -- the guard compacts the output
|
|
190
|
+
await agentStub.persistMessages([message]);
|
|
191
|
+
|
|
192
|
+
const persisted =
|
|
193
|
+
(await agentStub.getPersistedMessages()) as ChatMessage[];
|
|
194
|
+
expect(persisted.length).toBe(1);
|
|
195
|
+
|
|
196
|
+
// Output should be compacted (not the original huge string)
|
|
197
|
+
const part = persisted[0].parts[0] as { output: unknown };
|
|
198
|
+
const outputStr = part.output as string;
|
|
199
|
+
expect(outputStr).toContain("too large to persist");
|
|
200
|
+
expect(outputStr).toContain("bytes");
|
|
201
|
+
expect(outputStr.length).toBeLessThan(hugeOutput.length);
|
|
202
|
+
|
|
203
|
+
ws.close(1000);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it("compacted messages have metadata with compactedToolOutputs", async () => {
|
|
207
|
+
const room = crypto.randomUUID();
|
|
208
|
+
const { ws } = await connectChatWS(`/agents/test-chat-agent/${room}`);
|
|
209
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
210
|
+
|
|
211
|
+
const agentStub = await getAgentByName(env.TestChatAgent, room);
|
|
212
|
+
|
|
213
|
+
const message: ChatMessage = {
|
|
214
|
+
id: "meta-compact",
|
|
215
|
+
role: "assistant",
|
|
216
|
+
parts: [
|
|
217
|
+
{
|
|
218
|
+
type: "tool-bigTool",
|
|
219
|
+
toolCallId: "call_meta",
|
|
220
|
+
state: "output-available",
|
|
221
|
+
input: {},
|
|
222
|
+
output: "Y".repeat(1_900_000)
|
|
223
|
+
}
|
|
224
|
+
] as ChatMessage["parts"]
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
await agentStub.persistMessages([message]);
|
|
228
|
+
|
|
229
|
+
const persisted =
|
|
230
|
+
(await agentStub.getPersistedMessages()) as ChatMessage[];
|
|
231
|
+
const metadata = persisted[0].metadata as Record<string, unknown>;
|
|
232
|
+
expect(metadata).toBeDefined();
|
|
233
|
+
expect(metadata.compactedToolOutputs).toEqual(["call_meta"]);
|
|
234
|
+
|
|
235
|
+
ws.close(1000);
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it("non-assistant messages pass through even if large", async () => {
|
|
239
|
+
const room = crypto.randomUUID();
|
|
240
|
+
const { ws } = await connectChatWS(`/agents/test-chat-agent/${room}`);
|
|
241
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
242
|
+
|
|
243
|
+
const agentStub = await getAgentByName(env.TestChatAgent, room);
|
|
244
|
+
|
|
245
|
+
// A large user message (no tool outputs to compact)
|
|
246
|
+
// This tests the text truncation fallback for non-assistant messages
|
|
247
|
+
const largeText = "Z".repeat(1_900_000);
|
|
248
|
+
const message: ChatMessage = {
|
|
249
|
+
id: "user-big",
|
|
250
|
+
role: "user",
|
|
251
|
+
parts: [{ type: "text", text: largeText }]
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
// Should not crash
|
|
255
|
+
await agentStub.persistMessages([message]);
|
|
256
|
+
|
|
257
|
+
const persisted =
|
|
258
|
+
(await agentStub.getPersistedMessages()) as ChatMessage[];
|
|
259
|
+
expect(persisted.length).toBe(1);
|
|
260
|
+
|
|
261
|
+
// Text should be truncated
|
|
262
|
+
const textPart = persisted[0].parts[0] as { text: string };
|
|
263
|
+
expect(textPart.text).toContain("truncated for storage");
|
|
264
|
+
expect(textPart.text.length).toBeLessThan(largeText.length);
|
|
265
|
+
|
|
266
|
+
ws.close(1000);
|
|
267
|
+
});
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
describe("Unicode byte-length measurement", () => {
|
|
271
|
+
it("compacts messages with multi-byte Unicode that exceed byte limit", async () => {
|
|
272
|
+
const room = crypto.randomUUID();
|
|
273
|
+
const { ws } = await connectChatWS(`/agents/test-chat-agent/${room}`);
|
|
274
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
275
|
+
|
|
276
|
+
const agentStub = await getAgentByName(env.TestChatAgent, room);
|
|
277
|
+
|
|
278
|
+
// CJK character \u4e00 is 1 JS char but 3 bytes in UTF-8.
|
|
279
|
+
// 700,000 CJK chars = 700,000 JS chars (under 1.8M char limit)
|
|
280
|
+
// but 2,100,000 UTF-8 bytes (over 1.8MB byte limit).
|
|
281
|
+
// This tests that the byte-length guard catches it.
|
|
282
|
+
const cjkOutput = "\u4e00".repeat(700_000);
|
|
283
|
+
|
|
284
|
+
const message: ChatMessage = {
|
|
285
|
+
id: "unicode-test",
|
|
286
|
+
role: "assistant",
|
|
287
|
+
parts: [
|
|
288
|
+
{
|
|
289
|
+
type: "tool-cjkTool",
|
|
290
|
+
toolCallId: "call_unicode",
|
|
291
|
+
state: "output-available",
|
|
292
|
+
input: {},
|
|
293
|
+
output: cjkOutput
|
|
294
|
+
}
|
|
295
|
+
] as ChatMessage["parts"]
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
await agentStub.persistMessages([message]);
|
|
299
|
+
|
|
300
|
+
const persisted =
|
|
301
|
+
(await agentStub.getPersistedMessages()) as ChatMessage[];
|
|
302
|
+
expect(persisted.length).toBe(1);
|
|
303
|
+
|
|
304
|
+
// The tool output should be compacted (byte size exceeds limit)
|
|
305
|
+
const part = persisted[0].parts[0] as { output: unknown };
|
|
306
|
+
expect(typeof part.output).toBe("string");
|
|
307
|
+
expect((part.output as string).length).toBeLessThan(cjkOutput.length);
|
|
308
|
+
expect(part.output as string).toContain("too large to persist");
|
|
309
|
+
|
|
310
|
+
ws.close(1000);
|
|
311
|
+
});
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
describe("Stream chunk size guard", () => {
|
|
315
|
+
it("normal chunks are stored", async () => {
|
|
316
|
+
const room = crypto.randomUUID();
|
|
317
|
+
const { ws } = await connectChatWS(`/agents/test-chat-agent/${room}`);
|
|
318
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
319
|
+
|
|
320
|
+
const agentStub = await getAgentByName(env.TestChatAgent, room);
|
|
321
|
+
|
|
322
|
+
const streamId = await agentStub.testStartStream("req-chunk-ok");
|
|
323
|
+
await agentStub.testStoreStreamChunk(
|
|
324
|
+
streamId,
|
|
325
|
+
'{"type":"text-delta","delta":"hello"}'
|
|
326
|
+
);
|
|
327
|
+
await agentStub.testFlushChunkBuffer();
|
|
328
|
+
|
|
329
|
+
const chunks = await agentStub.getStreamChunks(streamId);
|
|
330
|
+
expect(chunks.length).toBe(1);
|
|
331
|
+
|
|
332
|
+
ws.close(1000);
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
it("oversized chunks are skipped without crash", async () => {
|
|
336
|
+
const room = crypto.randomUUID();
|
|
337
|
+
const { ws } = await connectChatWS(`/agents/test-chat-agent/${room}`);
|
|
338
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
339
|
+
|
|
340
|
+
const agentStub = await getAgentByName(env.TestChatAgent, room);
|
|
341
|
+
|
|
342
|
+
const streamId = await agentStub.testStartStream("req-chunk-big");
|
|
343
|
+
|
|
344
|
+
// Store a normal chunk first
|
|
345
|
+
await agentStub.testStoreStreamChunk(
|
|
346
|
+
streamId,
|
|
347
|
+
'{"type":"text-start","id":"t1"}'
|
|
348
|
+
);
|
|
349
|
+
|
|
350
|
+
// Store an oversized chunk (>1.8MB) -- should be skipped
|
|
351
|
+
const hugeChunk =
|
|
352
|
+
'{"type":"tool-output-available","output":"' +
|
|
353
|
+
"X".repeat(1_900_000) +
|
|
354
|
+
'"}';
|
|
355
|
+
await agentStub.testStoreStreamChunk(streamId, hugeChunk);
|
|
356
|
+
|
|
357
|
+
// Store another normal chunk after
|
|
358
|
+
await agentStub.testStoreStreamChunk(
|
|
359
|
+
streamId,
|
|
360
|
+
'{"type":"text-end","id":"t1"}'
|
|
361
|
+
);
|
|
362
|
+
|
|
363
|
+
await agentStub.testFlushChunkBuffer();
|
|
364
|
+
|
|
365
|
+
const chunks = await agentStub.getStreamChunks(streamId);
|
|
366
|
+
// Should have 2 chunks (the oversized one was skipped)
|
|
367
|
+
expect(chunks.length).toBe(2);
|
|
368
|
+
expect(chunks[0].body).toContain("text-start");
|
|
369
|
+
expect(chunks[1].body).toContain("text-end");
|
|
370
|
+
|
|
371
|
+
ws.close(1000);
|
|
372
|
+
});
|
|
373
|
+
});
|
|
374
|
+
});
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { env } from "cloudflare:test";
|
|
2
|
+
import { describe, it, expect } from "vitest";
|
|
3
|
+
import type { UIMessage as ChatMessage } from "ai";
|
|
4
|
+
import { connectChatWS } from "./test-utils";
|
|
5
|
+
import { getAgentByName } from "agents";
|
|
6
|
+
|
|
7
|
+
describe("Message Sanitization", () => {
|
|
8
|
+
it("strips OpenAI itemId from persisted messages", async () => {
|
|
9
|
+
const room = crypto.randomUUID();
|
|
10
|
+
const { ws } = await connectChatWS(`/agents/test-chat-agent/${room}`);
|
|
11
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
12
|
+
|
|
13
|
+
const agentStub = await getAgentByName(env.TestChatAgent, room);
|
|
14
|
+
|
|
15
|
+
// Persist a message with OpenAI providerMetadata containing itemId
|
|
16
|
+
const messageWithItemId: ChatMessage = {
|
|
17
|
+
id: "msg-sanitize-1",
|
|
18
|
+
role: "assistant",
|
|
19
|
+
parts: [
|
|
20
|
+
{
|
|
21
|
+
type: "text",
|
|
22
|
+
text: "Hello!",
|
|
23
|
+
providerMetadata: {
|
|
24
|
+
openai: {
|
|
25
|
+
itemId: "item_abc123",
|
|
26
|
+
someOtherField: "keep-me"
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
]
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
await agentStub.persistMessages([messageWithItemId]);
|
|
34
|
+
|
|
35
|
+
const persisted = (await agentStub.getPersistedMessages()) as ChatMessage[];
|
|
36
|
+
expect(persisted.length).toBe(1);
|
|
37
|
+
|
|
38
|
+
const textPart = persisted[0].parts[0] as {
|
|
39
|
+
type: string;
|
|
40
|
+
text: string;
|
|
41
|
+
providerMetadata?: Record<string, unknown>;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
// itemId should be stripped
|
|
45
|
+
expect(
|
|
46
|
+
(textPart.providerMetadata?.openai as Record<string, unknown>)?.itemId
|
|
47
|
+
).toBeUndefined();
|
|
48
|
+
|
|
49
|
+
// Other OpenAI fields should be preserved
|
|
50
|
+
expect(
|
|
51
|
+
(textPart.providerMetadata?.openai as Record<string, unknown>)
|
|
52
|
+
?.someOtherField
|
|
53
|
+
).toBe("keep-me");
|
|
54
|
+
|
|
55
|
+
ws.close(1000);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("strips reasoningEncryptedContent from persisted messages", async () => {
|
|
59
|
+
const room = crypto.randomUUID();
|
|
60
|
+
const { ws } = await connectChatWS(`/agents/test-chat-agent/${room}`);
|
|
61
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
62
|
+
|
|
63
|
+
const agentStub = await getAgentByName(env.TestChatAgent, room);
|
|
64
|
+
|
|
65
|
+
const messageWithEncrypted: ChatMessage = {
|
|
66
|
+
id: "msg-sanitize-2",
|
|
67
|
+
role: "assistant",
|
|
68
|
+
parts: [
|
|
69
|
+
{
|
|
70
|
+
type: "text",
|
|
71
|
+
text: "Thought about it",
|
|
72
|
+
providerMetadata: {
|
|
73
|
+
openai: {
|
|
74
|
+
itemId: "item_xyz",
|
|
75
|
+
reasoningEncryptedContent: "encrypted-blob"
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
]
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
await agentStub.persistMessages([messageWithEncrypted]);
|
|
83
|
+
|
|
84
|
+
const persisted = (await agentStub.getPersistedMessages()) as ChatMessage[];
|
|
85
|
+
const textPart = persisted[0].parts[0] as {
|
|
86
|
+
type: string;
|
|
87
|
+
providerMetadata?: Record<string, unknown>;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
// Both itemId and reasoningEncryptedContent should be stripped
|
|
91
|
+
// Since no other openai fields remain, the openai key itself should be gone
|
|
92
|
+
expect(textPart.providerMetadata?.openai).toBeUndefined();
|
|
93
|
+
|
|
94
|
+
ws.close(1000);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("removes empty reasoning parts from persisted messages", async () => {
|
|
98
|
+
const room = crypto.randomUUID();
|
|
99
|
+
const { ws } = await connectChatWS(`/agents/test-chat-agent/${room}`);
|
|
100
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
101
|
+
|
|
102
|
+
const agentStub = await getAgentByName(env.TestChatAgent, room);
|
|
103
|
+
|
|
104
|
+
const messageWithEmptyReasoning: ChatMessage = {
|
|
105
|
+
id: "msg-sanitize-3",
|
|
106
|
+
role: "assistant",
|
|
107
|
+
parts: [
|
|
108
|
+
{ type: "reasoning", text: "", state: "done" },
|
|
109
|
+
{ type: "reasoning", text: " ", state: "done" },
|
|
110
|
+
{ type: "text", text: "Hello!" },
|
|
111
|
+
{ type: "reasoning", text: "I thought about this", state: "done" }
|
|
112
|
+
] as ChatMessage["parts"]
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
await agentStub.persistMessages([messageWithEmptyReasoning]);
|
|
116
|
+
|
|
117
|
+
const persisted = (await agentStub.getPersistedMessages()) as ChatMessage[];
|
|
118
|
+
expect(persisted.length).toBe(1);
|
|
119
|
+
|
|
120
|
+
// Empty reasoning parts should be filtered out, but non-empty ones kept
|
|
121
|
+
const reasoningParts = persisted[0].parts.filter(
|
|
122
|
+
(p) => p.type === "reasoning"
|
|
123
|
+
);
|
|
124
|
+
expect(reasoningParts.length).toBe(1);
|
|
125
|
+
expect((reasoningParts[0] as { text: string }).text).toBe(
|
|
126
|
+
"I thought about this"
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
// Text part should be preserved
|
|
130
|
+
const textParts = persisted[0].parts.filter((p) => p.type === "text");
|
|
131
|
+
expect(textParts.length).toBe(1);
|
|
132
|
+
|
|
133
|
+
ws.close(1000);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("strips callProviderMetadata from tool parts", async () => {
|
|
137
|
+
const room = crypto.randomUUID();
|
|
138
|
+
const { ws } = await connectChatWS(`/agents/test-chat-agent/${room}`);
|
|
139
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
140
|
+
|
|
141
|
+
const agentStub = await getAgentByName(env.TestChatAgent, room);
|
|
142
|
+
|
|
143
|
+
const messageWithToolMeta: ChatMessage = {
|
|
144
|
+
id: "msg-sanitize-4",
|
|
145
|
+
role: "assistant",
|
|
146
|
+
parts: [
|
|
147
|
+
{
|
|
148
|
+
type: "tool-getWeather",
|
|
149
|
+
toolCallId: "call_meta1",
|
|
150
|
+
state: "output-available",
|
|
151
|
+
input: { city: "London" },
|
|
152
|
+
output: "Sunny",
|
|
153
|
+
callProviderMetadata: {
|
|
154
|
+
openai: {
|
|
155
|
+
itemId: "item_tool_123"
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
] as ChatMessage["parts"]
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
await agentStub.persistMessages([messageWithToolMeta]);
|
|
163
|
+
|
|
164
|
+
const persisted = (await agentStub.getPersistedMessages()) as ChatMessage[];
|
|
165
|
+
const toolPart = persisted[0].parts[0] as Record<string, unknown>;
|
|
166
|
+
|
|
167
|
+
// callProviderMetadata with only itemId should be completely removed
|
|
168
|
+
expect(toolPart.callProviderMetadata).toBeUndefined();
|
|
169
|
+
|
|
170
|
+
// Tool data should be preserved
|
|
171
|
+
expect(toolPart.state).toBe("output-available");
|
|
172
|
+
expect(toolPart.output).toBe("Sunny");
|
|
173
|
+
|
|
174
|
+
ws.close(1000);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it("preserves messages without OpenAI metadata unchanged", async () => {
|
|
178
|
+
const room = crypto.randomUUID();
|
|
179
|
+
const { ws } = await connectChatWS(`/agents/test-chat-agent/${room}`);
|
|
180
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
181
|
+
|
|
182
|
+
const agentStub = await getAgentByName(env.TestChatAgent, room);
|
|
183
|
+
|
|
184
|
+
const plainMessage: ChatMessage = {
|
|
185
|
+
id: "msg-sanitize-5",
|
|
186
|
+
role: "assistant",
|
|
187
|
+
parts: [
|
|
188
|
+
{ type: "text", text: "Just a plain message" },
|
|
189
|
+
{
|
|
190
|
+
type: "text",
|
|
191
|
+
text: "With non-OpenAI metadata",
|
|
192
|
+
providerMetadata: {
|
|
193
|
+
anthropic: { cacheControl: "ephemeral" }
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
] as ChatMessage["parts"]
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
await agentStub.persistMessages([plainMessage]);
|
|
200
|
+
|
|
201
|
+
const persisted = (await agentStub.getPersistedMessages()) as ChatMessage[];
|
|
202
|
+
expect(persisted.length).toBe(1);
|
|
203
|
+
expect(persisted[0].parts.length).toBe(2);
|
|
204
|
+
|
|
205
|
+
// Non-OpenAI metadata should be preserved
|
|
206
|
+
const metaPart = persisted[0].parts[1] as {
|
|
207
|
+
providerMetadata?: Record<string, unknown>;
|
|
208
|
+
};
|
|
209
|
+
expect(metaPart.providerMetadata?.anthropic).toEqual({
|
|
210
|
+
cacheControl: "ephemeral"
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
ws.close(1000);
|
|
214
|
+
});
|
|
215
|
+
});
|