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