@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
package/src/tests/test-utils.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { createExecutionContext, env } from "cloudflare:test";
|
|
2
|
-
import { expect } from "vitest";
|
|
3
|
-
import { MessageType, type OutgoingMessage } from "../types";
|
|
4
|
-
import worker from "./worker";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Connects to the chat agent and returns the WebSocket and execution context
|
|
8
|
-
*/
|
|
9
|
-
export async function connectChatWS(
|
|
10
|
-
path: string
|
|
11
|
-
): Promise<{ ws: WebSocket; ctx: ExecutionContext }> {
|
|
12
|
-
const ctx = createExecutionContext();
|
|
13
|
-
const req = new Request(`http://example.com${path}`, {
|
|
14
|
-
headers: { Upgrade: "websocket" }
|
|
15
|
-
});
|
|
16
|
-
const res = await worker.fetch(req, env, ctx);
|
|
17
|
-
expect(res.status).toBe(101);
|
|
18
|
-
const ws = res.webSocket as WebSocket;
|
|
19
|
-
expect(ws).toBeDefined();
|
|
20
|
-
ws.accept();
|
|
21
|
-
return { ws, ctx };
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Type guard for CF_AGENT_USE_CHAT_RESPONSE messages
|
|
26
|
-
*/
|
|
27
|
-
export function isUseChatResponseMessage(
|
|
28
|
-
m: unknown
|
|
29
|
-
): m is Extract<
|
|
30
|
-
OutgoingMessage,
|
|
31
|
-
{ type: MessageType.CF_AGENT_USE_CHAT_RESPONSE }
|
|
32
|
-
> {
|
|
33
|
-
return (
|
|
34
|
-
typeof m === "object" &&
|
|
35
|
-
m !== null &&
|
|
36
|
-
"type" in m &&
|
|
37
|
-
m.type === MessageType.CF_AGENT_USE_CHAT_RESPONSE
|
|
38
|
-
);
|
|
39
|
-
}
|
package/src/tests/tsconfig.json
DELETED
|
@@ -1,269 +0,0 @@
|
|
|
1
|
-
import { createExecutionContext, env } from "cloudflare:test";
|
|
2
|
-
import { describe, it, expect } from "vitest";
|
|
3
|
-
import worker from "./worker";
|
|
4
|
-
import type { UIMessage as ChatMessage } from "ai";
|
|
5
|
-
import { getAgentByName } from "agents";
|
|
6
|
-
|
|
7
|
-
describe("Message Structural Validation", () => {
|
|
8
|
-
async function setupAgent(room: string) {
|
|
9
|
-
const ctx = createExecutionContext();
|
|
10
|
-
const req = new Request(
|
|
11
|
-
`http://example.com/agents/test-chat-agent/${room}`,
|
|
12
|
-
{ headers: { Upgrade: "websocket" } }
|
|
13
|
-
);
|
|
14
|
-
const res = await worker.fetch(req, env, ctx);
|
|
15
|
-
expect(res.status).toBe(101);
|
|
16
|
-
const ws = res.webSocket as WebSocket;
|
|
17
|
-
ws.accept();
|
|
18
|
-
await ctx.waitUntil(Promise.resolve());
|
|
19
|
-
|
|
20
|
-
const agentStub = await getAgentByName(env.TestChatAgent, room);
|
|
21
|
-
return { ws, agentStub };
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Fetch messages via the /get-messages HTTP endpoint, which goes through
|
|
26
|
-
* _loadMessagesFromDb() and applies structural validation.
|
|
27
|
-
*/
|
|
28
|
-
async function getValidatedMessages(room: string): Promise<ChatMessage[]> {
|
|
29
|
-
const getMessagesReq = new Request(
|
|
30
|
-
`http://example.com/agents/test-chat-agent/${room}/get-messages`
|
|
31
|
-
);
|
|
32
|
-
const getMessagesRes = await worker.fetch(
|
|
33
|
-
getMessagesReq,
|
|
34
|
-
env,
|
|
35
|
-
createExecutionContext()
|
|
36
|
-
);
|
|
37
|
-
expect(getMessagesRes.status).toBe(200);
|
|
38
|
-
return (await getMessagesRes.json()) as ChatMessage[];
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
it("loads valid messages without filtering", async () => {
|
|
42
|
-
const room = crypto.randomUUID();
|
|
43
|
-
const { ws, agentStub } = await setupAgent(room);
|
|
44
|
-
|
|
45
|
-
const messages: ChatMessage[] = [
|
|
46
|
-
{ id: "msg1", role: "user", parts: [{ type: "text", text: "Hello" }] },
|
|
47
|
-
{
|
|
48
|
-
id: "msg2",
|
|
49
|
-
role: "assistant",
|
|
50
|
-
parts: [{ type: "text", text: "Hi!" }]
|
|
51
|
-
}
|
|
52
|
-
];
|
|
53
|
-
|
|
54
|
-
await agentStub.persistMessages(messages);
|
|
55
|
-
|
|
56
|
-
const persisted = await getValidatedMessages(room);
|
|
57
|
-
expect(persisted.length).toBe(2);
|
|
58
|
-
expect(persisted[0].id).toBe("msg1");
|
|
59
|
-
expect(persisted[1].id).toBe("msg2");
|
|
60
|
-
|
|
61
|
-
ws.close(1000);
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
it("filters out messages with missing id", async () => {
|
|
65
|
-
const room = crypto.randomUUID();
|
|
66
|
-
const { ws, agentStub } = await setupAgent(room);
|
|
67
|
-
|
|
68
|
-
await agentStub.persistMessages([
|
|
69
|
-
{ id: "valid1", role: "user", parts: [{ type: "text", text: "Hello" }] }
|
|
70
|
-
]);
|
|
71
|
-
|
|
72
|
-
// Manually insert a malformed message (no id field)
|
|
73
|
-
agentStub.insertRawMessage(
|
|
74
|
-
"bad-no-id",
|
|
75
|
-
JSON.stringify({
|
|
76
|
-
role: "user",
|
|
77
|
-
parts: [{ type: "text", text: "bad" }]
|
|
78
|
-
})
|
|
79
|
-
);
|
|
80
|
-
|
|
81
|
-
const persisted = await getValidatedMessages(room);
|
|
82
|
-
expect(persisted.length).toBe(1);
|
|
83
|
-
expect(persisted[0].id).toBe("valid1");
|
|
84
|
-
|
|
85
|
-
ws.close(1000);
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
it("filters out messages with invalid role", async () => {
|
|
89
|
-
const room = crypto.randomUUID();
|
|
90
|
-
const { ws, agentStub } = await setupAgent(room);
|
|
91
|
-
|
|
92
|
-
await agentStub.persistMessages([
|
|
93
|
-
{ id: "valid1", role: "user", parts: [{ type: "text", text: "Hello" }] }
|
|
94
|
-
]);
|
|
95
|
-
|
|
96
|
-
agentStub.insertRawMessage(
|
|
97
|
-
"bad-role",
|
|
98
|
-
JSON.stringify({
|
|
99
|
-
id: "bad-role",
|
|
100
|
-
role: "invalid-role",
|
|
101
|
-
parts: [{ type: "text", text: "bad" }]
|
|
102
|
-
})
|
|
103
|
-
);
|
|
104
|
-
|
|
105
|
-
const persisted = await getValidatedMessages(room);
|
|
106
|
-
expect(persisted.length).toBe(1);
|
|
107
|
-
expect(persisted[0].id).toBe("valid1");
|
|
108
|
-
|
|
109
|
-
ws.close(1000);
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
it("filters out messages with non-array parts", async () => {
|
|
113
|
-
const room = crypto.randomUUID();
|
|
114
|
-
const { ws, agentStub } = await setupAgent(room);
|
|
115
|
-
|
|
116
|
-
await agentStub.persistMessages([
|
|
117
|
-
{ id: "valid1", role: "user", parts: [{ type: "text", text: "Hello" }] }
|
|
118
|
-
]);
|
|
119
|
-
|
|
120
|
-
agentStub.insertRawMessage(
|
|
121
|
-
"bad-parts",
|
|
122
|
-
JSON.stringify({
|
|
123
|
-
id: "bad-parts",
|
|
124
|
-
role: "user",
|
|
125
|
-
parts: "not an array"
|
|
126
|
-
})
|
|
127
|
-
);
|
|
128
|
-
|
|
129
|
-
const persisted = await getValidatedMessages(room);
|
|
130
|
-
expect(persisted.length).toBe(1);
|
|
131
|
-
expect(persisted[0].id).toBe("valid1");
|
|
132
|
-
|
|
133
|
-
ws.close(1000);
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
it("filters out messages with empty id", async () => {
|
|
137
|
-
const room = crypto.randomUUID();
|
|
138
|
-
const { ws, agentStub } = await setupAgent(room);
|
|
139
|
-
|
|
140
|
-
await agentStub.persistMessages([
|
|
141
|
-
{ id: "valid1", role: "user", parts: [{ type: "text", text: "Hello" }] }
|
|
142
|
-
]);
|
|
143
|
-
|
|
144
|
-
agentStub.insertRawMessage(
|
|
145
|
-
"bad-empty-id",
|
|
146
|
-
JSON.stringify({
|
|
147
|
-
id: "",
|
|
148
|
-
role: "user",
|
|
149
|
-
parts: [{ type: "text", text: "bad" }]
|
|
150
|
-
})
|
|
151
|
-
);
|
|
152
|
-
|
|
153
|
-
const persisted = await getValidatedMessages(room);
|
|
154
|
-
expect(persisted.length).toBe(1);
|
|
155
|
-
expect(persisted[0].id).toBe("valid1");
|
|
156
|
-
|
|
157
|
-
ws.close(1000);
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
it("keeps messages with empty parts array (lenient)", async () => {
|
|
161
|
-
const room = crypto.randomUUID();
|
|
162
|
-
const { ws, agentStub } = await setupAgent(room);
|
|
163
|
-
|
|
164
|
-
await agentStub.persistMessages([
|
|
165
|
-
{ id: "empty-parts", role: "assistant", parts: [] }
|
|
166
|
-
]);
|
|
167
|
-
|
|
168
|
-
const persisted = await getValidatedMessages(room);
|
|
169
|
-
expect(persisted.length).toBe(1);
|
|
170
|
-
expect(persisted[0].id).toBe("empty-parts");
|
|
171
|
-
expect(persisted[0].parts).toEqual([]);
|
|
172
|
-
|
|
173
|
-
ws.close(1000);
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
it("filters out non-object values stored as messages", async () => {
|
|
177
|
-
const room = crypto.randomUUID();
|
|
178
|
-
const { ws, agentStub } = await setupAgent(room);
|
|
179
|
-
|
|
180
|
-
await agentStub.persistMessages([
|
|
181
|
-
{ id: "valid1", role: "user", parts: [{ type: "text", text: "Hello" }] }
|
|
182
|
-
]);
|
|
183
|
-
|
|
184
|
-
// Raw string value (not a JSON object)
|
|
185
|
-
agentStub.insertRawMessage("bad-string", JSON.stringify("just a string"));
|
|
186
|
-
|
|
187
|
-
// null value
|
|
188
|
-
agentStub.insertRawMessage("bad-null", "null");
|
|
189
|
-
|
|
190
|
-
const persisted = await getValidatedMessages(room);
|
|
191
|
-
expect(persisted.length).toBe(1);
|
|
192
|
-
expect(persisted[0].id).toBe("valid1");
|
|
193
|
-
|
|
194
|
-
ws.close(1000);
|
|
195
|
-
});
|
|
196
|
-
|
|
197
|
-
it("preserves messages with metadata", async () => {
|
|
198
|
-
const room = crypto.randomUUID();
|
|
199
|
-
const { ws, agentStub } = await setupAgent(room);
|
|
200
|
-
|
|
201
|
-
const messageWithMeta: ChatMessage = {
|
|
202
|
-
id: "meta1",
|
|
203
|
-
role: "assistant",
|
|
204
|
-
parts: [{ type: "text", text: "Response" }],
|
|
205
|
-
metadata: { model: "gpt-4o", totalTokens: 150 }
|
|
206
|
-
};
|
|
207
|
-
|
|
208
|
-
await agentStub.persistMessages([messageWithMeta]);
|
|
209
|
-
|
|
210
|
-
const persisted = await getValidatedMessages(room);
|
|
211
|
-
expect(persisted.length).toBe(1);
|
|
212
|
-
expect(persisted[0].id).toBe("meta1");
|
|
213
|
-
expect(persisted[0].metadata).toEqual({
|
|
214
|
-
model: "gpt-4o",
|
|
215
|
-
totalTokens: 150
|
|
216
|
-
});
|
|
217
|
-
|
|
218
|
-
ws.close(1000);
|
|
219
|
-
});
|
|
220
|
-
|
|
221
|
-
it("filters out completely unparseable JSON", async () => {
|
|
222
|
-
const room = crypto.randomUUID();
|
|
223
|
-
const { ws, agentStub } = await setupAgent(room);
|
|
224
|
-
|
|
225
|
-
await agentStub.persistMessages([
|
|
226
|
-
{ id: "valid1", role: "user", parts: [{ type: "text", text: "Hello" }] }
|
|
227
|
-
]);
|
|
228
|
-
|
|
229
|
-
// Insert raw broken JSON that will fail JSON.parse
|
|
230
|
-
agentStub.insertRawMessage("bad-json", "{broken json!!!");
|
|
231
|
-
|
|
232
|
-
const persisted = await getValidatedMessages(room);
|
|
233
|
-
expect(persisted.length).toBe(1);
|
|
234
|
-
expect(persisted[0].id).toBe("valid1");
|
|
235
|
-
|
|
236
|
-
ws.close(1000);
|
|
237
|
-
});
|
|
238
|
-
|
|
239
|
-
it("accepts all valid roles: user, assistant, system", async () => {
|
|
240
|
-
const room = crypto.randomUUID();
|
|
241
|
-
const { ws, agentStub } = await setupAgent(room);
|
|
242
|
-
|
|
243
|
-
const messages: ChatMessage[] = [
|
|
244
|
-
{
|
|
245
|
-
id: "sys1",
|
|
246
|
-
role: "system",
|
|
247
|
-
parts: [{ type: "text", text: "You are helpful" }]
|
|
248
|
-
},
|
|
249
|
-
{ id: "usr1", role: "user", parts: [{ type: "text", text: "Hello" }] },
|
|
250
|
-
{
|
|
251
|
-
id: "ast1",
|
|
252
|
-
role: "assistant",
|
|
253
|
-
parts: [{ type: "text", text: "Hi!" }]
|
|
254
|
-
}
|
|
255
|
-
];
|
|
256
|
-
|
|
257
|
-
await agentStub.persistMessages(messages);
|
|
258
|
-
|
|
259
|
-
const persisted = await getValidatedMessages(room);
|
|
260
|
-
expect(persisted.length).toBe(3);
|
|
261
|
-
expect(persisted.map((m) => m.role)).toEqual([
|
|
262
|
-
"system",
|
|
263
|
-
"user",
|
|
264
|
-
"assistant"
|
|
265
|
-
]);
|
|
266
|
-
|
|
267
|
-
ws.close(1000);
|
|
268
|
-
});
|
|
269
|
-
});
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config";
|
|
2
|
-
|
|
3
|
-
export default defineWorkersConfig({
|
|
4
|
-
test: {
|
|
5
|
-
name: "workers",
|
|
6
|
-
include: ["**/*.test.ts"],
|
|
7
|
-
deps: {
|
|
8
|
-
optimizer: {
|
|
9
|
-
ssr: {
|
|
10
|
-
include: [
|
|
11
|
-
// vitest can't seem to properly import
|
|
12
|
-
// `require('./path/to/anything.json')` files,
|
|
13
|
-
// which ajv uses (by way of @modelcontextprotocol/sdk)
|
|
14
|
-
// the workaround is to add the package to the include list
|
|
15
|
-
"ajv"
|
|
16
|
-
]
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
},
|
|
20
|
-
poolOptions: {
|
|
21
|
-
workers: {
|
|
22
|
-
isolatedStorage: false,
|
|
23
|
-
singleWorker: true,
|
|
24
|
-
wrangler: {
|
|
25
|
-
configPath: "./wrangler.jsonc"
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
});
|