@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,87 @@
|
|
|
1
|
+
import { env } from "cloudflare:test";
|
|
2
|
+
import { describe, it, expect } from "vitest";
|
|
3
|
+
import { MessageType } from "../types";
|
|
4
|
+
import type { UIMessage as ChatMessage } from "ai";
|
|
5
|
+
import { connectChatWS, isUseChatResponseMessage } from "./test-utils";
|
|
6
|
+
import { getAgentByName } from "agents";
|
|
7
|
+
|
|
8
|
+
describe("Plain text response handling", () => {
|
|
9
|
+
it("produces a single text part for plain text responses", async () => {
|
|
10
|
+
const room = crypto.randomUUID();
|
|
11
|
+
const { ws } = await connectChatWS(`/agents/test-chat-agent/${room}`);
|
|
12
|
+
|
|
13
|
+
const messages: unknown[] = [];
|
|
14
|
+
let resolvePromise: (value: boolean) => void;
|
|
15
|
+
const donePromise = new Promise<boolean>((res) => {
|
|
16
|
+
resolvePromise = res;
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
const timeout = setTimeout(() => resolvePromise(false), 2000);
|
|
20
|
+
|
|
21
|
+
ws.addEventListener("message", (e: MessageEvent) => {
|
|
22
|
+
const data = JSON.parse(e.data as string);
|
|
23
|
+
messages.push(data);
|
|
24
|
+
|
|
25
|
+
if (data.type === MessageType.CF_AGENT_USE_CHAT_RESPONSE && data.done) {
|
|
26
|
+
clearTimeout(timeout);
|
|
27
|
+
resolvePromise(true);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
// Send a chat request - the test worker returns plain text "Hello from chat agent!"
|
|
32
|
+
const userMessage: ChatMessage = {
|
|
33
|
+
id: "msg-plain-1",
|
|
34
|
+
role: "user",
|
|
35
|
+
parts: [{ type: "text", text: "Test" }]
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
ws.send(
|
|
39
|
+
JSON.stringify({
|
|
40
|
+
type: MessageType.CF_AGENT_USE_CHAT_REQUEST,
|
|
41
|
+
id: "req-plain-1",
|
|
42
|
+
init: {
|
|
43
|
+
method: "POST",
|
|
44
|
+
body: JSON.stringify({ messages: [userMessage] })
|
|
45
|
+
}
|
|
46
|
+
})
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
const done = await donePromise;
|
|
50
|
+
expect(done).toBe(true);
|
|
51
|
+
|
|
52
|
+
// Verify the persisted message has a single text part (not multiple)
|
|
53
|
+
const agentStub = await getAgentByName(env.TestChatAgent, room);
|
|
54
|
+
const persisted = (await agentStub.getPersistedMessages()) as ChatMessage[];
|
|
55
|
+
|
|
56
|
+
const assistantMessages = persisted.filter((m) => m.role === "assistant");
|
|
57
|
+
expect(assistantMessages.length).toBeGreaterThanOrEqual(1);
|
|
58
|
+
|
|
59
|
+
// The last assistant message should have exactly 1 text part
|
|
60
|
+
const lastAssistant = assistantMessages[assistantMessages.length - 1];
|
|
61
|
+
const textParts = lastAssistant.parts.filter((p) => p.type === "text");
|
|
62
|
+
expect(textParts.length).toBe(1);
|
|
63
|
+
|
|
64
|
+
// The text should be the full response content
|
|
65
|
+
const textPart = textParts[0] as { text: string };
|
|
66
|
+
expect(textPart.text).toBe("Hello from chat agent!");
|
|
67
|
+
|
|
68
|
+
// Verify the stream protocol events: text-start, text-delta(s), text-end
|
|
69
|
+
const streamResponses = messages.filter(isUseChatResponseMessage);
|
|
70
|
+
const nonEmptyResponses = streamResponses.filter(
|
|
71
|
+
(m) =>
|
|
72
|
+
"body" in m && typeof m.body === "string" && m.body.trim().length > 0
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
// Should have text-start, text-delta(s), text-end events
|
|
76
|
+
const bodies = nonEmptyResponses.map((m) => JSON.parse(m.body as string));
|
|
77
|
+
const types = bodies.map((b) => b.type);
|
|
78
|
+
|
|
79
|
+
expect(types[0]).toBe("text-start");
|
|
80
|
+
expect(types[types.length - 1]).toBe("text-end");
|
|
81
|
+
expect(
|
|
82
|
+
types.filter((t: string) => t === "text-delta").length
|
|
83
|
+
).toBeGreaterThanOrEqual(1);
|
|
84
|
+
|
|
85
|
+
ws.close(1000);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
@@ -49,7 +49,7 @@ describe("Resumable Streaming", () => {
|
|
|
49
49
|
expect(metadata?.status).toBe("streaming");
|
|
50
50
|
expect(metadata?.request_id).toBe("req-123");
|
|
51
51
|
|
|
52
|
-
ws.close();
|
|
52
|
+
ws.close(1000);
|
|
53
53
|
});
|
|
54
54
|
|
|
55
55
|
it("stores stream chunks in batches", async () => {
|
|
@@ -86,7 +86,7 @@ describe("Resumable Streaming", () => {
|
|
|
86
86
|
expect(chunks[2].chunk_index).toBe(2);
|
|
87
87
|
expect(chunks[0].body).toBe('{"type":"text","text":"Hello"}');
|
|
88
88
|
|
|
89
|
-
ws.close();
|
|
89
|
+
ws.close(1000);
|
|
90
90
|
});
|
|
91
91
|
|
|
92
92
|
it("marks stream as completed and clears active state", async () => {
|
|
@@ -113,7 +113,7 @@ describe("Resumable Streaming", () => {
|
|
|
113
113
|
const metadata = await agentStub.getStreamMetadata(streamId);
|
|
114
114
|
expect(metadata?.status).toBe("completed");
|
|
115
115
|
|
|
116
|
-
ws.close();
|
|
116
|
+
ws.close(1000);
|
|
117
117
|
});
|
|
118
118
|
|
|
119
119
|
it("marks stream as error on failure", async () => {
|
|
@@ -135,7 +135,7 @@ describe("Resumable Streaming", () => {
|
|
|
135
135
|
const metadata = await agentStub.getStreamMetadata(streamId);
|
|
136
136
|
expect(metadata?.status).toBe("error");
|
|
137
137
|
|
|
138
|
-
ws.close();
|
|
138
|
+
ws.close(1000);
|
|
139
139
|
});
|
|
140
140
|
});
|
|
141
141
|
|
|
@@ -172,7 +172,7 @@ describe("Resumable Streaming", () => {
|
|
|
172
172
|
expect(resumeMsg).toBeDefined();
|
|
173
173
|
expect(resumeMsg?.id).toBe("req-resume");
|
|
174
174
|
|
|
175
|
-
ws2.close();
|
|
175
|
+
ws2.close(1000);
|
|
176
176
|
});
|
|
177
177
|
|
|
178
178
|
it("sends stream chunks after client ACK", async () => {
|
|
@@ -223,7 +223,7 @@ describe("Resumable Streaming", () => {
|
|
|
223
223
|
expect(chunkMsgs[0].body).toBe('{"type":"text","text":"chunk1"}');
|
|
224
224
|
expect(chunkMsgs[1].body).toBe('{"type":"text","text":"chunk2"}');
|
|
225
225
|
|
|
226
|
-
ws2.close();
|
|
226
|
+
ws2.close(1000);
|
|
227
227
|
});
|
|
228
228
|
|
|
229
229
|
it("does not deliver live chunks before ACK to resuming connections", async () => {
|
|
@@ -299,7 +299,7 @@ describe("Resumable Streaming", () => {
|
|
|
299
299
|
);
|
|
300
300
|
|
|
301
301
|
ws1.close();
|
|
302
|
-
ws2.close();
|
|
302
|
+
ws2.close(1000);
|
|
303
303
|
});
|
|
304
304
|
|
|
305
305
|
it("ignores ACK with wrong request ID", async () => {
|
|
@@ -344,7 +344,7 @@ describe("Resumable Streaming", () => {
|
|
|
344
344
|
const chunkMsgs = messages2.filter(isUseChatResponseMessage);
|
|
345
345
|
expect(chunkMsgs.length).toBe(0);
|
|
346
346
|
|
|
347
|
-
ws2.close();
|
|
347
|
+
ws2.close(1000);
|
|
348
348
|
});
|
|
349
349
|
});
|
|
350
350
|
|
|
@@ -379,7 +379,7 @@ describe("Resumable Streaming", () => {
|
|
|
379
379
|
// Active stream should NOT be set
|
|
380
380
|
expect(await agentStub.getActiveStreamId()).toBeNull();
|
|
381
381
|
|
|
382
|
-
ws.close();
|
|
382
|
+
ws.close(1000);
|
|
383
383
|
});
|
|
384
384
|
|
|
385
385
|
it("restores fresh streams (under 5 minutes old)", async () => {
|
|
@@ -411,7 +411,7 @@ describe("Resumable Streaming", () => {
|
|
|
411
411
|
expect(await agentStub.getActiveStreamId()).toBe(freshStreamId);
|
|
412
412
|
expect(await agentStub.getActiveRequestId()).toBe("req-fresh");
|
|
413
413
|
|
|
414
|
-
ws.close();
|
|
414
|
+
ws.close(1000);
|
|
415
415
|
});
|
|
416
416
|
});
|
|
417
417
|
|
|
@@ -451,7 +451,7 @@ describe("Resumable Streaming", () => {
|
|
|
451
451
|
// Active state should be cleared
|
|
452
452
|
expect(await agentStub.getActiveStreamId()).toBeNull();
|
|
453
453
|
|
|
454
|
-
ws.close();
|
|
454
|
+
ws.close(1000);
|
|
455
455
|
});
|
|
456
456
|
});
|
|
457
457
|
|
|
@@ -485,7 +485,7 @@ describe("Resumable Streaming", () => {
|
|
|
485
485
|
// Second stream is active
|
|
486
486
|
expect(await agentStub.getActiveStreamId()).toBe(stream2);
|
|
487
487
|
|
|
488
|
-
ws.close();
|
|
488
|
+
ws.close(1000);
|
|
489
489
|
});
|
|
490
490
|
|
|
491
491
|
it("flushes on complete", async () => {
|
|
@@ -509,7 +509,7 @@ describe("Resumable Streaming", () => {
|
|
|
509
509
|
expect(chunks.length).toBe(1);
|
|
510
510
|
expect(chunks[0].body).toBe('{"type":"text","text":"final"}');
|
|
511
511
|
|
|
512
|
-
ws.close();
|
|
512
|
+
ws.close(1000);
|
|
513
513
|
});
|
|
514
514
|
});
|
|
515
515
|
|
|
@@ -546,7 +546,213 @@ describe("Resumable Streaming", () => {
|
|
|
546
546
|
const resumeMsg = messages2.find(isStreamResumingMessage);
|
|
547
547
|
expect(resumeMsg).toBeUndefined();
|
|
548
548
|
|
|
549
|
-
ws2.close();
|
|
549
|
+
ws2.close(1000);
|
|
550
|
+
});
|
|
551
|
+
});
|
|
552
|
+
|
|
553
|
+
describe("Client-initiated resume (issue #896)", () => {
|
|
554
|
+
it("CF_AGENT_STREAM_RESUME_REQUEST triggers resume notification", async () => {
|
|
555
|
+
const room = crypto.randomUUID();
|
|
556
|
+
|
|
557
|
+
// First connection: start a stream
|
|
558
|
+
const { ws: ws1 } = await connectChatWS(
|
|
559
|
+
`/agents/test-chat-agent/${room}`
|
|
560
|
+
);
|
|
561
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
562
|
+
|
|
563
|
+
const agentStub = await getAgentByName(env.TestChatAgent, room);
|
|
564
|
+
const streamId = await agentStub.testStartStream("req-client-resume");
|
|
565
|
+
await agentStub.testStoreStreamChunk(
|
|
566
|
+
streamId,
|
|
567
|
+
'{"type":"text-start","id":"t1"}'
|
|
568
|
+
);
|
|
569
|
+
await agentStub.testStoreStreamChunk(
|
|
570
|
+
streamId,
|
|
571
|
+
'{"type":"text-delta","id":"t1","delta":"hello"}'
|
|
572
|
+
);
|
|
573
|
+
await agentStub.testFlushChunkBuffer();
|
|
574
|
+
|
|
575
|
+
ws1.close();
|
|
576
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
577
|
+
|
|
578
|
+
// Second connection: send CF_AGENT_STREAM_RESUME_REQUEST
|
|
579
|
+
const { ws: ws2 } = await connectChatWS(
|
|
580
|
+
`/agents/test-chat-agent/${room}`
|
|
581
|
+
);
|
|
582
|
+
const messages2 = collectMessages(ws2);
|
|
583
|
+
|
|
584
|
+
// Wait briefly for any onConnect push (which we'll also get)
|
|
585
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
586
|
+
|
|
587
|
+
// Send the client-initiated resume request
|
|
588
|
+
ws2.send(
|
|
589
|
+
JSON.stringify({
|
|
590
|
+
type: MessageType.CF_AGENT_STREAM_RESUME_REQUEST
|
|
591
|
+
})
|
|
592
|
+
);
|
|
593
|
+
|
|
594
|
+
await new Promise((r) => setTimeout(r, 100));
|
|
595
|
+
|
|
596
|
+
// Should have received CF_AGENT_STREAM_RESUMING (from request, not just onConnect)
|
|
597
|
+
const resumeMsgs = messages2.filter(isStreamResumingMessage);
|
|
598
|
+
// May get 2 (one from onConnect, one from request) or 1 if timing collapses them
|
|
599
|
+
expect(resumeMsgs.length).toBeGreaterThanOrEqual(1);
|
|
600
|
+
|
|
601
|
+
ws2.close(1000);
|
|
602
|
+
});
|
|
603
|
+
|
|
604
|
+
it("CF_AGENT_STREAM_RESUME_REQUEST with no active stream is a no-op", async () => {
|
|
605
|
+
const room = crypto.randomUUID();
|
|
606
|
+
const { ws } = await connectChatWS(`/agents/test-chat-agent/${room}`);
|
|
607
|
+
const messages = collectMessages(ws);
|
|
608
|
+
|
|
609
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
610
|
+
|
|
611
|
+
// Send resume request when there's no active stream
|
|
612
|
+
ws.send(
|
|
613
|
+
JSON.stringify({
|
|
614
|
+
type: MessageType.CF_AGENT_STREAM_RESUME_REQUEST
|
|
615
|
+
})
|
|
616
|
+
);
|
|
617
|
+
|
|
618
|
+
await new Promise((r) => setTimeout(r, 100));
|
|
619
|
+
|
|
620
|
+
// Should NOT get CF_AGENT_STREAM_RESUMING
|
|
621
|
+
const resumeMsg = messages.find(isStreamResumingMessage);
|
|
622
|
+
expect(resumeMsg).toBeUndefined();
|
|
623
|
+
|
|
624
|
+
ws.close(1000);
|
|
625
|
+
});
|
|
626
|
+
|
|
627
|
+
it("replayed chunks have replay=true flag", async () => {
|
|
628
|
+
const room = crypto.randomUUID();
|
|
629
|
+
|
|
630
|
+
const { ws: ws1 } = await connectChatWS(
|
|
631
|
+
`/agents/test-chat-agent/${room}`
|
|
632
|
+
);
|
|
633
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
634
|
+
|
|
635
|
+
const agentStub = await getAgentByName(env.TestChatAgent, room);
|
|
636
|
+
// Start a stream and add chunks but do NOT complete it
|
|
637
|
+
// (stream must be active for resume to work)
|
|
638
|
+
const streamId = await agentStub.testStartStream("req-replay-flag");
|
|
639
|
+
await agentStub.testStoreStreamChunk(
|
|
640
|
+
streamId,
|
|
641
|
+
'{"type":"text-start","id":"t1"}'
|
|
642
|
+
);
|
|
643
|
+
await agentStub.testStoreStreamChunk(
|
|
644
|
+
streamId,
|
|
645
|
+
'{"type":"text-delta","id":"t1","delta":"test"}'
|
|
646
|
+
);
|
|
647
|
+
await agentStub.testFlushChunkBuffer();
|
|
648
|
+
|
|
649
|
+
ws1.close();
|
|
650
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
651
|
+
|
|
652
|
+
// Reconnect — active stream triggers resume
|
|
653
|
+
const { ws: ws2 } = await connectChatWS(
|
|
654
|
+
`/agents/test-chat-agent/${room}`
|
|
655
|
+
);
|
|
656
|
+
const messages2 = collectMessages(ws2);
|
|
657
|
+
|
|
658
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
659
|
+
|
|
660
|
+
// Send resume request
|
|
661
|
+
ws2.send(
|
|
662
|
+
JSON.stringify({
|
|
663
|
+
type: MessageType.CF_AGENT_STREAM_RESUME_REQUEST
|
|
664
|
+
})
|
|
665
|
+
);
|
|
666
|
+
|
|
667
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
668
|
+
|
|
669
|
+
// ACK the resuming notification
|
|
670
|
+
const resumeMsg = messages2.find(isStreamResumingMessage);
|
|
671
|
+
expect(resumeMsg).toBeDefined();
|
|
672
|
+
|
|
673
|
+
ws2.send(
|
|
674
|
+
JSON.stringify({
|
|
675
|
+
type: MessageType.CF_AGENT_STREAM_RESUME_ACK,
|
|
676
|
+
id: (resumeMsg as { id: string }).id
|
|
677
|
+
})
|
|
678
|
+
);
|
|
679
|
+
|
|
680
|
+
await new Promise((r) => setTimeout(r, 200));
|
|
681
|
+
|
|
682
|
+
// All CF_AGENT_USE_CHAT_RESPONSE messages should have replay=true
|
|
683
|
+
const responseMessages = messages2.filter(isUseChatResponseMessage);
|
|
684
|
+
expect(responseMessages.length).toBeGreaterThan(0);
|
|
685
|
+
|
|
686
|
+
for (const msg of responseMessages) {
|
|
687
|
+
expect((msg as { replay?: boolean }).replay).toBe(true);
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
ws2.close(1000);
|
|
691
|
+
});
|
|
692
|
+
});
|
|
693
|
+
|
|
694
|
+
describe("clearAll clears chunk buffer", () => {
|
|
695
|
+
it("buffered chunks are not flushed to SQLite after clearAll", async () => {
|
|
696
|
+
const room = crypto.randomUUID();
|
|
697
|
+
const { ws } = await connectChatWS(`/agents/test-chat-agent/${room}`);
|
|
698
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
699
|
+
|
|
700
|
+
const agentStub = await getAgentByName(env.TestChatAgent, room);
|
|
701
|
+
|
|
702
|
+
// Start a stream and buffer some chunks (do NOT flush)
|
|
703
|
+
const streamId = await agentStub.testStartStream("req-buffer-clear");
|
|
704
|
+
await agentStub.testStoreStreamChunk(streamId, "chunk-1");
|
|
705
|
+
await agentStub.testStoreStreamChunk(streamId, "chunk-2");
|
|
706
|
+
|
|
707
|
+
// Chunks should be in buffer but not yet in SQLite (buffer size < 10)
|
|
708
|
+
let chunks = await agentStub.getStreamChunks(streamId);
|
|
709
|
+
expect(chunks.length).toBe(0); // Still in memory buffer
|
|
710
|
+
|
|
711
|
+
// Clear all — should discard the buffer
|
|
712
|
+
ws.send(JSON.stringify({ type: "cf_agent_chat_clear" }));
|
|
713
|
+
await new Promise((r) => setTimeout(r, 100));
|
|
714
|
+
|
|
715
|
+
// Flush should be a no-op since buffer was cleared
|
|
716
|
+
await agentStub.testFlushChunkBuffer();
|
|
717
|
+
chunks = await agentStub.getStreamChunks(streamId);
|
|
718
|
+
expect(chunks.length).toBe(0);
|
|
719
|
+
|
|
720
|
+
// Wait before close to let the agent settle
|
|
721
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
722
|
+
ws.close(1000);
|
|
723
|
+
});
|
|
724
|
+
});
|
|
725
|
+
|
|
726
|
+
describe("errored stream cleanup", () => {
|
|
727
|
+
it("errored streams are cleaned up alongside completed streams", async () => {
|
|
728
|
+
const room = crypto.randomUUID();
|
|
729
|
+
const { ws } = await connectChatWS(`/agents/test-chat-agent/${room}`);
|
|
730
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
731
|
+
|
|
732
|
+
const agentStub = await getAgentByName(env.TestChatAgent, room);
|
|
733
|
+
|
|
734
|
+
// Insert an old errored stream (25 hours old, past the 24h cleanup threshold)
|
|
735
|
+
await agentStub.testInsertOldErroredStream(
|
|
736
|
+
"old-errored",
|
|
737
|
+
"req-errored",
|
|
738
|
+
25 * 60 * 60 * 1000
|
|
739
|
+
);
|
|
740
|
+
|
|
741
|
+
// Verify the errored stream exists
|
|
742
|
+
const metadata = await agentStub.getStreamMetadata("old-errored");
|
|
743
|
+
expect(metadata?.status).toBe("error");
|
|
744
|
+
|
|
745
|
+
// Trigger cleanup by completing a dummy stream
|
|
746
|
+
// (cleanup runs periodically inside completeStream)
|
|
747
|
+
await agentStub.testTriggerStreamCleanup();
|
|
748
|
+
|
|
749
|
+
// The old errored stream should be cleaned up
|
|
750
|
+
const afterMetadata = await agentStub.getStreamMetadata("old-errored");
|
|
751
|
+
expect(afterMetadata).toBeNull();
|
|
752
|
+
|
|
753
|
+
// Wait before close to let the agent settle
|
|
754
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
755
|
+
ws.close(1000);
|
|
550
756
|
});
|
|
551
757
|
});
|
|
552
758
|
});
|