| 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 | 8cac59a | 2025-04-24 12:21:19 +0100 | [diff] [blame] | 3 | |
| 4 | // Mock state updates for long-polling simulation |
| 5 | let currentState = { ...initialState }; |
| Pokey Rule | e923bb3 | 2025-04-24 18:48:01 +0100 | [diff] [blame] | 6 | const EMPTY_CONVERSATION = |
| 7 | new URL(window.location.href).searchParams.get("emptyConversation") === "1"; |
| Pokey Rule | 8cac59a | 2025-04-24 12:21:19 +0100 | [diff] [blame] | 8 | const ADD_NEW_MESSAGES = |
| 9 | new URL(window.location.href).searchParams.get("addNewMessages") === "1"; |
| 10 | |
| Pokey Rule | e923bb3 | 2025-04-24 18:48:01 +0100 | [diff] [blame] | 11 | const messages = EMPTY_CONVERSATION ? [] : [...initialMessages]; |
| 12 | |
| Pokey Rule | 8cac59a | 2025-04-24 12:21:19 +0100 | [diff] [blame] | 13 | export const handlers = [ |
| 14 | // Unified state endpoint that handles both regular and polling requests |
| 15 | http.get("*/state", async ({ request }) => { |
| 16 | const url = new URL(request.url); |
| 17 | const isPoll = url.searchParams.get("poll") === "true"; |
| 18 | |
| 19 | if (!isPoll) { |
| 20 | // Regular state request |
| 21 | return HttpResponse.json(currentState); |
| 22 | } |
| 23 | |
| 24 | // This is a long-polling request |
| 25 | await delay(ADD_NEW_MESSAGES ? 2000 : 60000); // Simulate waiting for changes |
| 26 | |
| 27 | if (ADD_NEW_MESSAGES) { |
| 28 | // Simulate adding new messages |
| 29 | messages.push({ |
| 30 | type: "agent", |
| 31 | end_of_turn: false, |
| 32 | content: "Here's a message", |
| 33 | timestamp: "2025-04-24T10:32:29.072661+01:00", |
| 34 | conversation_id: "37s-g6xg", |
| 35 | usage: { |
| 36 | input_tokens: 5, |
| 37 | cache_creation_input_tokens: 250, |
| 38 | cache_read_input_tokens: 4017, |
| 39 | output_tokens: 92, |
| 40 | cost_usd: 0.0035376, |
| 41 | }, |
| 42 | start_time: "2025-04-24T10:32:26.99749+01:00", |
| 43 | end_time: "2025-04-24T10:32:29.072654+01:00", |
| 44 | elapsed: 2075193375, |
| 45 | turnDuration: 28393844125, |
| 46 | idx: messages.length, |
| 47 | }); |
| 48 | |
| 49 | // Update the state with new messages |
| 50 | currentState = { |
| 51 | ...currentState, |
| 52 | message_count: messages.length, |
| 53 | }; |
| 54 | } |
| 55 | |
| 56 | return HttpResponse.json(currentState); |
| 57 | }), |
| 58 | |
| 59 | // Messages endpoint |
| 60 | http.get("*/messages", ({ request }) => { |
| 61 | const url = new URL(request.url); |
| 62 | const startIndex = parseInt(url.searchParams.get("start") || "0"); |
| 63 | |
| 64 | return HttpResponse.json(messages.slice(startIndex)); |
| 65 | }), |
| 66 | ]; |