|
| 1 | +import { createExecutionContext, env } from "cloudflare:test"; |
| 2 | +import { describe, it, expect } from "vitest"; |
| 3 | +import worker, { type Env } from "./worker"; |
| 4 | +import { MessageType } from "../ai-types"; |
| 5 | +import type { UIMessage as ChatMessage } from "ai"; |
| 6 | + |
| 7 | +declare module "cloudflare:test" { |
| 8 | + interface ProvidedEnv extends Env {} |
| 9 | +} |
| 10 | + |
| 11 | +async function connectChatWS(path: string) { |
| 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 | +describe("AIChatAgent Connection Context - Issue #711", () => { |
| 25 | + it("getCurrentAgent() should return connection in onChatMessage and nested async functions (tool execute)", async () => { |
| 26 | + const room = crypto.randomUUID(); |
| 27 | + const { ws } = await connectChatWS(`/agents/test-chat-agent/${room}`); |
| 28 | + |
| 29 | + // Get the agent stub to access captured context |
| 30 | + const agentStub = env.TestChatAgent.get(env.TestChatAgent.idFromName(room)); |
| 31 | + |
| 32 | + // Clear any previous captured context |
| 33 | + await agentStub.clearCapturedContext(); |
| 34 | + |
| 35 | + let resolvePromise: (value: boolean) => void; |
| 36 | + const donePromise = new Promise<boolean>((res) => { |
| 37 | + resolvePromise = res; |
| 38 | + }); |
| 39 | + |
| 40 | + const timeout = setTimeout(() => resolvePromise(false), 2000); |
| 41 | + |
| 42 | + ws.addEventListener("message", (e: MessageEvent) => { |
| 43 | + const data = JSON.parse(e.data as string); |
| 44 | + if (data.type === MessageType.CF_AGENT_USE_CHAT_RESPONSE && data.done) { |
| 45 | + clearTimeout(timeout); |
| 46 | + resolvePromise(true); |
| 47 | + } |
| 48 | + }); |
| 49 | + |
| 50 | + const userMessage: ChatMessage = { |
| 51 | + id: "msg1", |
| 52 | + role: "user", |
| 53 | + parts: [{ type: "text", text: "Hello" }] |
| 54 | + }; |
| 55 | + |
| 56 | + // Send a chat message which will trigger onChatMessage |
| 57 | + ws.send( |
| 58 | + JSON.stringify({ |
| 59 | + type: MessageType.CF_AGENT_USE_CHAT_REQUEST, |
| 60 | + id: "req1", |
| 61 | + init: { |
| 62 | + method: "POST", |
| 63 | + body: JSON.stringify({ messages: [userMessage] }) |
| 64 | + } |
| 65 | + }) |
| 66 | + ); |
| 67 | + |
| 68 | + const done = await donePromise; |
| 69 | + expect(done).toBe(true); |
| 70 | + |
| 71 | + // Wait a bit to ensure context is captured |
| 72 | + await new Promise((resolve) => setTimeout(resolve, 100)); |
| 73 | + |
| 74 | + // Check the captured context from onChatMessage |
| 75 | + const capturedContext = await agentStub.getCapturedContext(); |
| 76 | + |
| 77 | + expect(capturedContext).not.toBeNull(); |
| 78 | + // The agent should be available |
| 79 | + expect(capturedContext?.hasAgent).toBe(true); |
| 80 | + // The connection should be available - this is the bug being tested |
| 81 | + // Before the fix, this would be false |
| 82 | + expect(capturedContext?.hasConnection).toBe(true); |
| 83 | + // The connection ID should be defined |
| 84 | + expect(capturedContext?.connectionId).toBeDefined(); |
| 85 | + |
| 86 | + // Check the nested context |
| 87 | + // Tools called from onChatMessage couldn't access connection context |
| 88 | + const nestedContext = await agentStub.getNestedContext(); |
| 89 | + |
| 90 | + expect(nestedContext).not.toBeNull(); |
| 91 | + // The agent should be available in nested async functions |
| 92 | + expect(nestedContext?.hasAgent).toBe(true); |
| 93 | + // The connection should ALSO be available in nested async functions (tool execute) |
| 94 | + // Before the fix, this would be false |
| 95 | + expect(nestedContext?.hasConnection).toBe(true); |
| 96 | + // The connection ID should match between onChatMessage and nested function |
| 97 | + expect(nestedContext?.connectionId).toBe(capturedContext?.connectionId); |
| 98 | + |
| 99 | + ws.close(); |
| 100 | + }); |
| 101 | +}); |
0 commit comments