| Pokey Rule | 8cac59a | 2025-04-24 12:21:19 +0100 | [diff] [blame] | 1 | import { http, HttpResponse, delay } from "msw"; |
| Pokey Rule | e7c9a44 | 2025-04-25 20:02:22 +0100 | [diff] [blame] | 2 | import { initialState, initialMessages } from "../../../fixtures/dummy"; |
| Pokey Rule | c575fd7 | 2025-05-06 15:46:38 +0100 | [diff] [blame] | 3 | import { AgentMessage, State } from "../../../types"; |
| Pokey Rule | 8cac59a | 2025-04-24 12:21:19 +0100 | [diff] [blame] | 4 | |
| Pokey Rule | c575fd7 | 2025-05-06 15:46:38 +0100 | [diff] [blame] | 5 | // Mock state updates for SSE simulation |
| Pokey Rule | 8cac59a | 2025-04-24 12:21:19 +0100 | [diff] [blame] | 6 | let currentState = { ...initialState }; |
| Pokey Rule | e923bb3 | 2025-04-24 18:48:01 +0100 | [diff] [blame] | 7 | const EMPTY_CONVERSATION = |
| 8 | new URL(window.location.href).searchParams.get("emptyConversation") === "1"; |
| Pokey Rule | 8cac59a | 2025-04-24 12:21:19 +0100 | [diff] [blame] | 9 | const ADD_NEW_MESSAGES = |
| 10 | new URL(window.location.href).searchParams.get("addNewMessages") === "1"; |
| 11 | |
| Pokey Rule | e923bb3 | 2025-04-24 18:48:01 +0100 | [diff] [blame] | 12 | const messages = EMPTY_CONVERSATION ? [] : [...initialMessages]; |
| 13 | |
| Pokey Rule | c575fd7 | 2025-05-06 15:46:38 +0100 | [diff] [blame] | 14 | // Text encoder for SSE messages |
| 15 | const encoder = new TextEncoder(); |
| 16 | |
| 17 | // Helper function to create SSE formatted messages |
| 18 | function formatSSE(event, data) { |
| 19 | return `event: ${event}\ndata: ${JSON.stringify(data)}\n\n`; |
| 20 | } |
| 21 | |
| Pokey Rule | 8cac59a | 2025-04-24 12:21:19 +0100 | [diff] [blame] | 22 | export const handlers = [ |
| Pokey Rule | c575fd7 | 2025-05-06 15:46:38 +0100 | [diff] [blame] | 23 | // SSE stream endpoint |
| 24 | http.get("*/stream", async ({ request }) => { |
| Pokey Rule | 8cac59a | 2025-04-24 12:21:19 +0100 | [diff] [blame] | 25 | const url = new URL(request.url); |
| Pokey Rule | c575fd7 | 2025-05-06 15:46:38 +0100 | [diff] [blame] | 26 | const fromIndex = parseInt(url.searchParams.get("from") || "0"); |
| Pokey Rule | 8cac59a | 2025-04-24 12:21:19 +0100 | [diff] [blame] | 27 | |
| Pokey Rule | c575fd7 | 2025-05-06 15:46:38 +0100 | [diff] [blame] | 28 | // Create a readable stream for SSE |
| 29 | const stream = new ReadableStream({ |
| 30 | async start(controller) { |
| 31 | // Send initial state update |
| 32 | controller.enqueue(encoder.encode(formatSSE("state", currentState))); |
| Pokey Rule | 8cac59a | 2025-04-24 12:21:19 +0100 | [diff] [blame] | 33 | |
| Pokey Rule | c575fd7 | 2025-05-06 15:46:38 +0100 | [diff] [blame] | 34 | // Send any existing messages that are newer than the fromIndex |
| 35 | const newMessages = messages.filter((msg) => msg.idx >= fromIndex); |
| 36 | for (const message of newMessages) { |
| 37 | controller.enqueue(encoder.encode(formatSSE("message", message))); |
| 38 | } |
| Pokey Rule | 8cac59a | 2025-04-24 12:21:19 +0100 | [diff] [blame] | 39 | |
| Pokey Rule | c575fd7 | 2025-05-06 15:46:38 +0100 | [diff] [blame] | 40 | // Simulate heartbeats and new messages |
| 41 | let heartbeatInterval; |
| 42 | let messageInterval; |
| Pokey Rule | 8cac59a | 2025-04-24 12:21:19 +0100 | [diff] [blame] | 43 | |
| Pokey Rule | c575fd7 | 2025-05-06 15:46:38 +0100 | [diff] [blame] | 44 | // Send heartbeats every 30 seconds |
| 45 | heartbeatInterval = setInterval(() => { |
| 46 | controller.enqueue( |
| 47 | encoder.encode( |
| 48 | formatSSE("heartbeat", { timestamp: new Date().toISOString() }), |
| 49 | ), |
| 50 | ); |
| 51 | }, 30000); |
| Pokey Rule | 8cac59a | 2025-04-24 12:21:19 +0100 | [diff] [blame] | 52 | |
| Pokey Rule | c575fd7 | 2025-05-06 15:46:38 +0100 | [diff] [blame] | 53 | // Add new messages if enabled |
| 54 | if (ADD_NEW_MESSAGES) { |
| 55 | messageInterval = setInterval(() => { |
| 56 | const newMessage = { |
| 57 | type: "agent" as const, |
| 58 | end_of_turn: false, |
| 59 | content: "Here's a new message via SSE", |
| 60 | timestamp: new Date().toISOString(), |
| 61 | conversation_id: "37s-g6xg", |
| 62 | usage: { |
| 63 | input_tokens: 5, |
| 64 | cache_creation_input_tokens: 250, |
| 65 | cache_read_input_tokens: 4017, |
| 66 | output_tokens: 92, |
| 67 | cost_usd: 0.0035376, |
| 68 | }, |
| 69 | start_time: new Date(Date.now() - 2000).toISOString(), |
| 70 | end_time: new Date().toISOString(), |
| 71 | elapsed: 2075193375, |
| 72 | turnDuration: 28393844125, |
| 73 | idx: messages.length, |
| 74 | }; |
| 75 | |
| 76 | // Add to our messages array |
| 77 | messages.push(newMessage); |
| 78 | |
| 79 | // Update the state |
| 80 | currentState = { |
| 81 | ...currentState, |
| 82 | message_count: messages.length, |
| 83 | }; |
| 84 | |
| 85 | // Send the message and updated state through SSE |
| 86 | controller.enqueue( |
| 87 | encoder.encode(formatSSE("message", newMessage)), |
| 88 | ); |
| 89 | controller.enqueue( |
| 90 | encoder.encode(formatSSE("state", currentState)), |
| 91 | ); |
| 92 | }, 2000); // Add a new message every 2 seconds |
| 93 | } |
| 94 | |
| 95 | // Clean up on connection close |
| 96 | request.signal.addEventListener("abort", () => { |
| 97 | clearInterval(heartbeatInterval); |
| 98 | if (messageInterval) clearInterval(messageInterval); |
| 99 | }); |
| 100 | }, |
| 101 | }); |
| 102 | |
| 103 | return new HttpResponse(stream, { |
| 104 | headers: { |
| 105 | "Content-Type": "text/event-stream", |
| 106 | "Cache-Control": "no-cache", |
| 107 | Connection: "keep-alive", |
| 108 | }, |
| 109 | }); |
| 110 | }), |
| 111 | |
| 112 | // State endpoint (non-streaming version for initial state) |
| 113 | http.get("*/state", () => { |
| Pokey Rule | 8cac59a | 2025-04-24 12:21:19 +0100 | [diff] [blame] | 114 | return HttpResponse.json(currentState); |
| 115 | }), |
| 116 | |
| 117 | // Messages endpoint |
| 118 | http.get("*/messages", ({ request }) => { |
| 119 | const url = new URL(request.url); |
| 120 | const startIndex = parseInt(url.searchParams.get("start") || "0"); |
| 121 | |
| 122 | return HttpResponse.json(messages.slice(startIndex)); |
| 123 | }), |
| Pokey Rule | c575fd7 | 2025-05-06 15:46:38 +0100 | [diff] [blame] | 124 | |
| 125 | // Chat endpoint for sending messages |
| 126 | http.post("*/chat", async ({ request }) => { |
| 127 | const body = await request.json(); |
| 128 | |
| 129 | // Add a user message |
| 130 | messages.push({ |
| 131 | type: "user" as const, |
| 132 | end_of_turn: true, |
| 133 | content: |
| 134 | typeof body === "object" && body !== null |
| 135 | ? String(body.message || "") |
| 136 | : "", |
| 137 | timestamp: new Date().toISOString(), |
| 138 | conversation_id: "37s-g6xg", |
| 139 | idx: messages.length, |
| 140 | }); |
| 141 | |
| 142 | // Update state |
| 143 | currentState = { |
| 144 | ...currentState, |
| 145 | message_count: messages.length, |
| 146 | }; |
| 147 | |
| 148 | return new HttpResponse(null, { status: 204 }); |
| 149 | }), |
| 150 | |
| 151 | // Cancel endpoint |
| 152 | http.post("*/cancel", () => { |
| 153 | return new HttpResponse(null, { status: 204 }); |
| 154 | }), |
| Pokey Rule | 8cac59a | 2025-04-24 12:21:19 +0100 | [diff] [blame] | 155 | ]; |