@cloudflare/ai-chat 0.1.4 → 0.1.6
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/dist/experimental/forever.d.ts +4 -13
- package/dist/experimental/forever.js +1 -46
- package/dist/experimental/forever.js.map +1 -1
- package/dist/index.d.ts +59 -4
- package/dist/index.js +170 -28
- package/dist/index.js.map +1 -1
- package/dist/{message-builder-Bwc_Umye.js → message-builder-uSvexDl2.js} +17 -4
- package/dist/message-builder-uSvexDl2.js.map +1 -0
- package/dist/react.js +125 -31
- package/dist/react.js.map +1 -1
- package/dist/{resumable-stream-DqXxQVjf.d.ts → resumable-stream-DTLAk9XC.d.ts} +25 -2
- package/dist/types.d.ts +2 -1
- package/dist/types.js.map +1 -1
- package/package.json +7 -3
- package/CHANGELOG.md +0 -244
- package/dist/message-builder-Bwc_Umye.js.map +0 -1
- package/e2e/chat.spec.ts +0 -1703
- package/e2e/client-tools.spec.ts +0 -504
- package/e2e/llm.spec.ts +0 -637
- package/e2e/playwright.config.ts +0 -27
- package/e2e/worker.ts +0 -174
- package/e2e/wrangler.jsonc +0 -23
- package/scripts/build.ts +0 -32
- package/src/ai-chat-v5-migration.ts +0 -403
- package/src/experimental/forever.ts +0 -66
- package/src/index.ts +0 -2269
- package/src/message-builder.ts +0 -384
- package/src/react-tests/setup.ts +0 -3
- package/src/react-tests/use-agent-chat.test.tsx +0 -1435
- package/src/react-tests/vitest.config.ts +0 -20
- package/src/react.tsx +0 -1447
- package/src/resumable-stream.ts +0 -441
- package/src/tests/cancel-request.test.ts +0 -280
- package/src/tests/chat-context.test.ts +0 -85
- package/src/tests/chat-persistence.test.ts +0 -426
- package/src/tests/clear-history.test.ts +0 -132
- package/src/tests/client-tool-duplicate-message.test.ts +0 -1957
- package/src/tests/client-tools-broadcast.test.ts +0 -138
- package/src/tests/client-tools-continuation.test.ts +0 -314
- package/src/tests/client-tools-reconnect.test.ts +0 -143
- package/src/tests/cloudflare-test.d.ts +0 -5
- package/src/tests/custom-body-continuation.test.ts +0 -309
- package/src/tests/custom-body.test.ts +0 -186
- package/src/tests/destroy.test.ts +0 -57
- package/src/tests/get-messages-endpoint.test.ts +0 -153
- package/src/tests/max-persisted-messages.test.ts +0 -201
- package/src/tests/merge-server-state.test.ts +0 -343
- package/src/tests/message-builder.test.ts +0 -808
- package/src/tests/migration.test.ts +0 -282
- package/src/tests/non-sse-response.test.ts +0 -186
- package/src/tests/onfinish-cleanup.test.ts +0 -362
- package/src/tests/plaintext-response.test.ts +0 -87
- package/src/tests/request-id.test.ts +0 -312
- package/src/tests/resumable-streaming.test.ts +0 -758
- package/src/tests/row-size-guard.test.ts +0 -374
- package/src/tests/sanitize-messages.test.ts +0 -325
- package/src/tests/test-utils.ts +0 -39
- package/src/tests/tsconfig.json +0 -10
- package/src/tests/validate-messages.test.ts +0 -269
- package/src/tests/vitest.config.ts +0 -30
- package/src/tests/worker.ts +0 -456
- package/src/tests/wrangler.jsonc +0 -50
- package/src/types.ts +0 -152
- package/src/ws-chat-transport.ts +0 -203
- package/tsconfig.json +0 -4
- package/vitest.config.ts +0 -7
|
@@ -1,201 +0,0 @@
|
|
|
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("maxPersistedMessages", () => {
|
|
8
|
-
it("does not limit messages when maxPersistedMessages is undefined", 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
|
-
// Default: no limit
|
|
16
|
-
const messages: ChatMessage[] = Array.from({ length: 20 }, (_, i) => ({
|
|
17
|
-
id: `msg-nolimit-${i}`,
|
|
18
|
-
role: "user" as const,
|
|
19
|
-
parts: [{ type: "text" as const, text: `Message ${i}` }]
|
|
20
|
-
}));
|
|
21
|
-
|
|
22
|
-
await agentStub.persistMessages(messages);
|
|
23
|
-
|
|
24
|
-
const count = await agentStub.getMessageCount();
|
|
25
|
-
expect(count).toBe(20);
|
|
26
|
-
|
|
27
|
-
ws.close(1000);
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
it("deletes oldest messages when count exceeds limit", async () => {
|
|
31
|
-
const room = crypto.randomUUID();
|
|
32
|
-
const { ws } = await connectChatWS(`/agents/test-chat-agent/${room}`);
|
|
33
|
-
await new Promise((r) => setTimeout(r, 50));
|
|
34
|
-
|
|
35
|
-
const agentStub = await getAgentByName(env.TestChatAgent, room);
|
|
36
|
-
|
|
37
|
-
// Set limit to 5
|
|
38
|
-
await agentStub.setMaxPersistedMessages(5);
|
|
39
|
-
|
|
40
|
-
// Persist 10 messages
|
|
41
|
-
const messages: ChatMessage[] = Array.from({ length: 10 }, (_, i) => ({
|
|
42
|
-
id: `msg-limit-${i}`,
|
|
43
|
-
role: "user" as const,
|
|
44
|
-
parts: [{ type: "text" as const, text: `Message ${i}` }]
|
|
45
|
-
}));
|
|
46
|
-
|
|
47
|
-
await agentStub.persistMessages(messages);
|
|
48
|
-
|
|
49
|
-
// Should have exactly 5 messages
|
|
50
|
-
const count = await agentStub.getMessageCount();
|
|
51
|
-
expect(count).toBe(5);
|
|
52
|
-
|
|
53
|
-
// The kept messages should be the most recent 5
|
|
54
|
-
const persisted = (await agentStub.getPersistedMessages()) as ChatMessage[];
|
|
55
|
-
expect(persisted.length).toBe(5);
|
|
56
|
-
expect(persisted[0].id).toBe("msg-limit-5");
|
|
57
|
-
expect(persisted[4].id).toBe("msg-limit-9");
|
|
58
|
-
|
|
59
|
-
ws.close(1000);
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
it("keeps all messages when count is under the limit", async () => {
|
|
63
|
-
const room = crypto.randomUUID();
|
|
64
|
-
const { ws } = await connectChatWS(`/agents/test-chat-agent/${room}`);
|
|
65
|
-
await new Promise((r) => setTimeout(r, 50));
|
|
66
|
-
|
|
67
|
-
const agentStub = await getAgentByName(env.TestChatAgent, room);
|
|
68
|
-
|
|
69
|
-
await agentStub.setMaxPersistedMessages(100);
|
|
70
|
-
|
|
71
|
-
const messages: ChatMessage[] = Array.from({ length: 5 }, (_, i) => ({
|
|
72
|
-
id: `msg-under-${i}`,
|
|
73
|
-
role: "user" as const,
|
|
74
|
-
parts: [{ type: "text" as const, text: `Message ${i}` }]
|
|
75
|
-
}));
|
|
76
|
-
|
|
77
|
-
await agentStub.persistMessages(messages);
|
|
78
|
-
|
|
79
|
-
const count = await agentStub.getMessageCount();
|
|
80
|
-
expect(count).toBe(5);
|
|
81
|
-
|
|
82
|
-
ws.close(1000);
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
it("enforces limit across multiple persist calls", async () => {
|
|
86
|
-
const room = crypto.randomUUID();
|
|
87
|
-
const { ws } = await connectChatWS(`/agents/test-chat-agent/${room}`);
|
|
88
|
-
await new Promise((r) => setTimeout(r, 50));
|
|
89
|
-
|
|
90
|
-
const agentStub = await getAgentByName(env.TestChatAgent, room);
|
|
91
|
-
|
|
92
|
-
await agentStub.setMaxPersistedMessages(4);
|
|
93
|
-
|
|
94
|
-
// First batch: 3 messages
|
|
95
|
-
await agentStub.persistMessages([
|
|
96
|
-
{
|
|
97
|
-
id: "batch1-1",
|
|
98
|
-
role: "user",
|
|
99
|
-
parts: [{ type: "text", text: "First" }]
|
|
100
|
-
},
|
|
101
|
-
{
|
|
102
|
-
id: "batch1-2",
|
|
103
|
-
role: "assistant",
|
|
104
|
-
parts: [{ type: "text", text: "Response 1" }]
|
|
105
|
-
},
|
|
106
|
-
{
|
|
107
|
-
id: "batch1-3",
|
|
108
|
-
role: "user",
|
|
109
|
-
parts: [{ type: "text", text: "Second" }]
|
|
110
|
-
}
|
|
111
|
-
]);
|
|
112
|
-
|
|
113
|
-
expect(await agentStub.getMessageCount()).toBe(3);
|
|
114
|
-
|
|
115
|
-
// Second batch: adds 3 more (total would be 6, limit is 4)
|
|
116
|
-
await agentStub.persistMessages([
|
|
117
|
-
{
|
|
118
|
-
id: "batch1-1",
|
|
119
|
-
role: "user",
|
|
120
|
-
parts: [{ type: "text", text: "First" }]
|
|
121
|
-
},
|
|
122
|
-
{
|
|
123
|
-
id: "batch1-2",
|
|
124
|
-
role: "assistant",
|
|
125
|
-
parts: [{ type: "text", text: "Response 1" }]
|
|
126
|
-
},
|
|
127
|
-
{
|
|
128
|
-
id: "batch1-3",
|
|
129
|
-
role: "user",
|
|
130
|
-
parts: [{ type: "text", text: "Second" }]
|
|
131
|
-
},
|
|
132
|
-
{
|
|
133
|
-
id: "batch2-1",
|
|
134
|
-
role: "assistant",
|
|
135
|
-
parts: [{ type: "text", text: "Response 2" }]
|
|
136
|
-
},
|
|
137
|
-
{
|
|
138
|
-
id: "batch2-2",
|
|
139
|
-
role: "user",
|
|
140
|
-
parts: [{ type: "text", text: "Third" }]
|
|
141
|
-
},
|
|
142
|
-
{
|
|
143
|
-
id: "batch2-3",
|
|
144
|
-
role: "assistant",
|
|
145
|
-
parts: [{ type: "text", text: "Response 3" }]
|
|
146
|
-
}
|
|
147
|
-
]);
|
|
148
|
-
|
|
149
|
-
// Should be capped at 4
|
|
150
|
-
expect(await agentStub.getMessageCount()).toBe(4);
|
|
151
|
-
|
|
152
|
-
// The kept messages should be the most recent 4
|
|
153
|
-
const persisted = (await agentStub.getPersistedMessages()) as ChatMessage[];
|
|
154
|
-
expect(persisted.map((m) => m.id)).toEqual([
|
|
155
|
-
"batch1-3",
|
|
156
|
-
"batch2-1",
|
|
157
|
-
"batch2-2",
|
|
158
|
-
"batch2-3"
|
|
159
|
-
]);
|
|
160
|
-
|
|
161
|
-
ws.close(1000);
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
it("can be disabled by setting to null", async () => {
|
|
165
|
-
const room = crypto.randomUUID();
|
|
166
|
-
const { ws } = await connectChatWS(`/agents/test-chat-agent/${room}`);
|
|
167
|
-
await new Promise((r) => setTimeout(r, 50));
|
|
168
|
-
|
|
169
|
-
const agentStub = await getAgentByName(env.TestChatAgent, room);
|
|
170
|
-
|
|
171
|
-
// Enable limit
|
|
172
|
-
await agentStub.setMaxPersistedMessages(3);
|
|
173
|
-
|
|
174
|
-
await agentStub.persistMessages(
|
|
175
|
-
Array.from({ length: 5 }, (_, i) => ({
|
|
176
|
-
id: `disable-${i}`,
|
|
177
|
-
role: "user" as const,
|
|
178
|
-
parts: [{ type: "text" as const, text: `Msg ${i}` }]
|
|
179
|
-
}))
|
|
180
|
-
);
|
|
181
|
-
expect(await agentStub.getMessageCount()).toBe(3);
|
|
182
|
-
|
|
183
|
-
// Disable limit
|
|
184
|
-
await agentStub.setMaxPersistedMessages(null);
|
|
185
|
-
|
|
186
|
-
// Add more messages -- should keep all now
|
|
187
|
-
await agentStub.persistMessages(
|
|
188
|
-
Array.from({ length: 8 }, (_, i) => ({
|
|
189
|
-
id: `disable-after-${i}`,
|
|
190
|
-
role: "user" as const,
|
|
191
|
-
parts: [{ type: "text" as const, text: `Msg after ${i}` }]
|
|
192
|
-
}))
|
|
193
|
-
);
|
|
194
|
-
|
|
195
|
-
// All 8 new + 3 remaining from before (but IDs overlap detection may differ)
|
|
196
|
-
// The key assertion: count is more than 3 (limit was removed)
|
|
197
|
-
expect(await agentStub.getMessageCount()).toBeGreaterThan(3);
|
|
198
|
-
|
|
199
|
-
ws.close(1000);
|
|
200
|
-
});
|
|
201
|
-
});
|
|
@@ -1,343 +0,0 @@
|
|
|
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
|
-
// Type helper for tool call parts
|
|
8
|
-
type TestToolCallPart = Extract<
|
|
9
|
-
ChatMessage["parts"][number],
|
|
10
|
-
{ type: `tool-${string}` }
|
|
11
|
-
>;
|
|
12
|
-
|
|
13
|
-
describe("Merge Incoming With Server State", () => {
|
|
14
|
-
it("preserves server-side tool outputs when client sends messages without them", async () => {
|
|
15
|
-
const room = crypto.randomUUID();
|
|
16
|
-
const { ws } = await connectChatWS(`/agents/test-chat-agent/${room}`);
|
|
17
|
-
await new Promise((r) => setTimeout(r, 50));
|
|
18
|
-
|
|
19
|
-
const agentStub = await getAgentByName(env.TestChatAgent, room);
|
|
20
|
-
|
|
21
|
-
// Step 1: Persist a message with tool output on the server
|
|
22
|
-
const toolResultPart: TestToolCallPart = {
|
|
23
|
-
type: "tool-getWeather",
|
|
24
|
-
toolCallId: "call_merge_1",
|
|
25
|
-
state: "output-available",
|
|
26
|
-
input: { city: "London" },
|
|
27
|
-
output: "Rainy, 12°C"
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
const serverMessage: ChatMessage = {
|
|
31
|
-
id: "assistant-merge-1",
|
|
32
|
-
role: "assistant",
|
|
33
|
-
parts: [toolResultPart] as ChatMessage["parts"]
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
await agentStub.persistMessages([serverMessage]);
|
|
37
|
-
|
|
38
|
-
// Step 2: Client sends the same message but without the tool output
|
|
39
|
-
// (client only knows about input-available state)
|
|
40
|
-
const clientMessage: ChatMessage = {
|
|
41
|
-
id: "assistant-merge-1",
|
|
42
|
-
role: "assistant",
|
|
43
|
-
parts: [
|
|
44
|
-
{
|
|
45
|
-
type: "tool-getWeather",
|
|
46
|
-
toolCallId: "call_merge_1",
|
|
47
|
-
state: "input-available",
|
|
48
|
-
input: { city: "London" }
|
|
49
|
-
} as unknown as ChatMessage["parts"][number]
|
|
50
|
-
]
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
// Send via CF_AGENT_CHAT_MESSAGES (which triggers persistMessages with merge)
|
|
54
|
-
const newUserMsg: ChatMessage = {
|
|
55
|
-
id: "user-merge-1",
|
|
56
|
-
role: "user",
|
|
57
|
-
parts: [{ type: "text", text: "Follow up question" }]
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
await agentStub.persistMessages([clientMessage, newUserMsg]);
|
|
61
|
-
|
|
62
|
-
// Step 3: Verify the tool output is preserved
|
|
63
|
-
const persisted = (await agentStub.getPersistedMessages()) as ChatMessage[];
|
|
64
|
-
|
|
65
|
-
const assistantMsg = persisted.find((m) => m.id === "assistant-merge-1");
|
|
66
|
-
expect(assistantMsg).toBeDefined();
|
|
67
|
-
|
|
68
|
-
const toolPart = assistantMsg!.parts[0] as {
|
|
69
|
-
state: string;
|
|
70
|
-
output?: unknown;
|
|
71
|
-
};
|
|
72
|
-
expect(toolPart.state).toBe("output-available");
|
|
73
|
-
expect(toolPart.output).toBe("Rainy, 12°C");
|
|
74
|
-
|
|
75
|
-
ws.close(1000);
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
it("passes through messages unchanged when server has no tool outputs", async () => {
|
|
79
|
-
const room = crypto.randomUUID();
|
|
80
|
-
const { ws } = await connectChatWS(`/agents/test-chat-agent/${room}`);
|
|
81
|
-
await new Promise((r) => setTimeout(r, 50));
|
|
82
|
-
|
|
83
|
-
const agentStub = await getAgentByName(env.TestChatAgent, room);
|
|
84
|
-
|
|
85
|
-
const userMessage: ChatMessage = {
|
|
86
|
-
id: "user-no-merge",
|
|
87
|
-
role: "user",
|
|
88
|
-
parts: [{ type: "text", text: "Hello" }]
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
const assistantMessage: ChatMessage = {
|
|
92
|
-
id: "assistant-no-merge",
|
|
93
|
-
role: "assistant",
|
|
94
|
-
parts: [{ type: "text", text: "Hi there!" }]
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
await agentStub.persistMessages([userMessage, assistantMessage]);
|
|
98
|
-
|
|
99
|
-
const persisted = (await agentStub.getPersistedMessages()) as ChatMessage[];
|
|
100
|
-
expect(persisted.length).toBe(2);
|
|
101
|
-
expect(persisted[0].id).toBe("user-no-merge");
|
|
102
|
-
expect(persisted[1].id).toBe("assistant-no-merge");
|
|
103
|
-
|
|
104
|
-
ws.close(1000);
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
it("reuses server assistant IDs for plain text messages to avoid duplicates", async () => {
|
|
108
|
-
const room = crypto.randomUUID();
|
|
109
|
-
const { ws } = await connectChatWS(`/agents/test-chat-agent/${room}`);
|
|
110
|
-
await new Promise((r) => setTimeout(r, 50));
|
|
111
|
-
|
|
112
|
-
const agentStub = await getAgentByName(env.TestChatAgent, room);
|
|
113
|
-
|
|
114
|
-
await agentStub.persistMessages([
|
|
115
|
-
{
|
|
116
|
-
id: "user-1",
|
|
117
|
-
role: "user",
|
|
118
|
-
parts: [{ type: "text", text: "Hello" }]
|
|
119
|
-
},
|
|
120
|
-
{
|
|
121
|
-
id: "assistant_server_1",
|
|
122
|
-
role: "assistant",
|
|
123
|
-
parts: [{ type: "text", text: "Hi there!" }]
|
|
124
|
-
}
|
|
125
|
-
]);
|
|
126
|
-
|
|
127
|
-
await agentStub.persistMessages([
|
|
128
|
-
{
|
|
129
|
-
id: "user-1",
|
|
130
|
-
role: "user",
|
|
131
|
-
parts: [{ type: "text", text: "Hello" }]
|
|
132
|
-
},
|
|
133
|
-
{
|
|
134
|
-
id: "assistant_client_1",
|
|
135
|
-
role: "assistant",
|
|
136
|
-
parts: [{ type: "text", text: "Hi there!" }]
|
|
137
|
-
},
|
|
138
|
-
{
|
|
139
|
-
id: "user-2",
|
|
140
|
-
role: "user",
|
|
141
|
-
parts: [{ type: "text", text: "How are you?" }]
|
|
142
|
-
}
|
|
143
|
-
]);
|
|
144
|
-
|
|
145
|
-
const persisted = (await agentStub.getPersistedMessages()) as ChatMessage[];
|
|
146
|
-
expect(persisted.length).toBe(3);
|
|
147
|
-
|
|
148
|
-
const assistantMessages = persisted.filter((m) => m.role === "assistant");
|
|
149
|
-
expect(assistantMessages.length).toBe(1);
|
|
150
|
-
expect(assistantMessages[0].id).toBe("assistant_server_1");
|
|
151
|
-
expect((assistantMessages[0].parts[0] as { text: string }).text).toBe(
|
|
152
|
-
"Hi there!"
|
|
153
|
-
);
|
|
154
|
-
|
|
155
|
-
ws.close(1000);
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
it("matches repeated assistant text messages in order", async () => {
|
|
159
|
-
const room = crypto.randomUUID();
|
|
160
|
-
const { ws } = await connectChatWS(`/agents/test-chat-agent/${room}`);
|
|
161
|
-
await new Promise((r) => setTimeout(r, 50));
|
|
162
|
-
|
|
163
|
-
const agentStub = await getAgentByName(env.TestChatAgent, room);
|
|
164
|
-
|
|
165
|
-
await agentStub.persistMessages([
|
|
166
|
-
{
|
|
167
|
-
id: "user-server-1",
|
|
168
|
-
role: "user",
|
|
169
|
-
parts: [{ type: "text", text: "Say hi" }]
|
|
170
|
-
},
|
|
171
|
-
{
|
|
172
|
-
id: "assistant_server_1",
|
|
173
|
-
role: "assistant",
|
|
174
|
-
parts: [{ type: "text", text: "Hi there!" }]
|
|
175
|
-
},
|
|
176
|
-
{
|
|
177
|
-
id: "user-server-2",
|
|
178
|
-
role: "user",
|
|
179
|
-
parts: [{ type: "text", text: "Say hi again" }]
|
|
180
|
-
},
|
|
181
|
-
{
|
|
182
|
-
id: "assistant_server_2",
|
|
183
|
-
role: "assistant",
|
|
184
|
-
parts: [{ type: "text", text: "Hi there!" }]
|
|
185
|
-
}
|
|
186
|
-
]);
|
|
187
|
-
|
|
188
|
-
await agentStub.persistMessages([
|
|
189
|
-
{
|
|
190
|
-
id: "user-server-1",
|
|
191
|
-
role: "user",
|
|
192
|
-
parts: [{ type: "text", text: "Say hi" }]
|
|
193
|
-
},
|
|
194
|
-
{
|
|
195
|
-
id: "assistant_client_1",
|
|
196
|
-
role: "assistant",
|
|
197
|
-
parts: [{ type: "text", text: "Hi there!" }]
|
|
198
|
-
},
|
|
199
|
-
{
|
|
200
|
-
id: "user-server-2",
|
|
201
|
-
role: "user",
|
|
202
|
-
parts: [{ type: "text", text: "Say hi again" }]
|
|
203
|
-
},
|
|
204
|
-
{
|
|
205
|
-
id: "assistant_client_2",
|
|
206
|
-
role: "assistant",
|
|
207
|
-
parts: [{ type: "text", text: "Hi there!" }]
|
|
208
|
-
}
|
|
209
|
-
]);
|
|
210
|
-
|
|
211
|
-
const persisted = (await agentStub.getPersistedMessages()) as ChatMessage[];
|
|
212
|
-
|
|
213
|
-
const assistantMessages = persisted.filter((m) => m.role === "assistant");
|
|
214
|
-
expect(assistantMessages.length).toBe(2);
|
|
215
|
-
expect(assistantMessages[0].id).toBe("assistant_server_1");
|
|
216
|
-
expect(assistantMessages[1].id).toBe("assistant_server_2");
|
|
217
|
-
|
|
218
|
-
ws.close(1000);
|
|
219
|
-
});
|
|
220
|
-
|
|
221
|
-
it("does not reconcile when assistant content differs at the same position", async () => {
|
|
222
|
-
const room = crypto.randomUUID();
|
|
223
|
-
const { ws } = await connectChatWS(`/agents/test-chat-agent/${room}`);
|
|
224
|
-
await new Promise((r) => setTimeout(r, 50));
|
|
225
|
-
|
|
226
|
-
const agentStub = await getAgentByName(env.TestChatAgent, room);
|
|
227
|
-
|
|
228
|
-
await agentStub.persistMessages([
|
|
229
|
-
{
|
|
230
|
-
id: "user-1",
|
|
231
|
-
role: "user",
|
|
232
|
-
parts: [{ type: "text", text: "Hello" }]
|
|
233
|
-
},
|
|
234
|
-
{
|
|
235
|
-
id: "assistant_server_1",
|
|
236
|
-
role: "assistant",
|
|
237
|
-
parts: [{ type: "text", text: "Hi there!" }]
|
|
238
|
-
}
|
|
239
|
-
]);
|
|
240
|
-
|
|
241
|
-
await agentStub.persistMessages([
|
|
242
|
-
{
|
|
243
|
-
id: "user-1",
|
|
244
|
-
role: "user",
|
|
245
|
-
parts: [{ type: "text", text: "Hello" }]
|
|
246
|
-
},
|
|
247
|
-
{
|
|
248
|
-
id: "assistant_client_1",
|
|
249
|
-
role: "assistant",
|
|
250
|
-
parts: [{ type: "text", text: "Completely different response" }]
|
|
251
|
-
}
|
|
252
|
-
]);
|
|
253
|
-
|
|
254
|
-
const persisted = (await agentStub.getPersistedMessages()) as ChatMessage[];
|
|
255
|
-
|
|
256
|
-
const assistantMessages = persisted.filter((m) => m.role === "assistant");
|
|
257
|
-
expect(assistantMessages.length).toBe(2);
|
|
258
|
-
expect(assistantMessages.map((m) => m.id)).toContain("assistant_server_1");
|
|
259
|
-
expect(assistantMessages.map((m) => m.id)).toContain("assistant_client_1");
|
|
260
|
-
|
|
261
|
-
ws.close(1000);
|
|
262
|
-
});
|
|
263
|
-
|
|
264
|
-
it("skips tool-bearing assistant messages during reconciliation without breaking cursor", async () => {
|
|
265
|
-
const room = crypto.randomUUID();
|
|
266
|
-
const { ws } = await connectChatWS(`/agents/test-chat-agent/${room}`);
|
|
267
|
-
await new Promise((r) => setTimeout(r, 50));
|
|
268
|
-
|
|
269
|
-
const agentStub = await getAgentByName(env.TestChatAgent, room);
|
|
270
|
-
|
|
271
|
-
const toolPart: TestToolCallPart = {
|
|
272
|
-
type: "tool-getWeather",
|
|
273
|
-
toolCallId: "call_mixed_1",
|
|
274
|
-
state: "output-available",
|
|
275
|
-
input: { city: "London" },
|
|
276
|
-
output: "Rainy, 12°C"
|
|
277
|
-
};
|
|
278
|
-
|
|
279
|
-
await agentStub.persistMessages([
|
|
280
|
-
{
|
|
281
|
-
id: "user-1",
|
|
282
|
-
role: "user",
|
|
283
|
-
parts: [{ type: "text", text: "What is the weather?" }]
|
|
284
|
-
},
|
|
285
|
-
{
|
|
286
|
-
id: "assistant_server_tool",
|
|
287
|
-
role: "assistant",
|
|
288
|
-
parts: [toolPart] as ChatMessage["parts"]
|
|
289
|
-
},
|
|
290
|
-
{
|
|
291
|
-
id: "user-2",
|
|
292
|
-
role: "user",
|
|
293
|
-
parts: [{ type: "text", text: "Thanks" }]
|
|
294
|
-
},
|
|
295
|
-
{
|
|
296
|
-
id: "assistant_server_text",
|
|
297
|
-
role: "assistant",
|
|
298
|
-
parts: [{ type: "text", text: "You're welcome!" }]
|
|
299
|
-
}
|
|
300
|
-
]);
|
|
301
|
-
|
|
302
|
-
await agentStub.persistMessages([
|
|
303
|
-
{
|
|
304
|
-
id: "user-1",
|
|
305
|
-
role: "user",
|
|
306
|
-
parts: [{ type: "text", text: "What is the weather?" }]
|
|
307
|
-
},
|
|
308
|
-
{
|
|
309
|
-
id: "assistant_server_tool",
|
|
310
|
-
role: "assistant",
|
|
311
|
-
parts: [toolPart] as ChatMessage["parts"]
|
|
312
|
-
},
|
|
313
|
-
{
|
|
314
|
-
id: "user-2",
|
|
315
|
-
role: "user",
|
|
316
|
-
parts: [{ type: "text", text: "Thanks" }]
|
|
317
|
-
},
|
|
318
|
-
{
|
|
319
|
-
id: "assistant_client_text",
|
|
320
|
-
role: "assistant",
|
|
321
|
-
parts: [{ type: "text", text: "You're welcome!" }]
|
|
322
|
-
}
|
|
323
|
-
]);
|
|
324
|
-
|
|
325
|
-
const persisted = (await agentStub.getPersistedMessages()) as ChatMessage[];
|
|
326
|
-
|
|
327
|
-
expect(persisted.length).toBe(4);
|
|
328
|
-
|
|
329
|
-
const toolMsg = persisted.find((m) => m.id === "assistant_server_tool");
|
|
330
|
-
expect(toolMsg).toBeDefined();
|
|
331
|
-
|
|
332
|
-
const textAssistants = persisted.filter(
|
|
333
|
-
(m) => m.role === "assistant" && m.id !== "assistant_server_tool"
|
|
334
|
-
);
|
|
335
|
-
expect(textAssistants.length).toBe(1);
|
|
336
|
-
expect(textAssistants[0].id).toBe("assistant_server_text");
|
|
337
|
-
expect((textAssistants[0].parts[0] as { text: string }).text).toBe(
|
|
338
|
-
"You're welcome!"
|
|
339
|
-
);
|
|
340
|
-
|
|
341
|
-
ws.close(1000);
|
|
342
|
-
});
|
|
343
|
-
});
|