blob: c05a6c41cfd38c8d7364e3ed6d7a0898e9e44c4a [file] [log] [blame]
Pokey Rule8cac59a2025-04-24 12:21:19 +01001import { http, HttpResponse, delay } from "msw";
Pokey Rulee7c9a442025-04-25 20:02:22 +01002import { initialState, initialMessages } from "../../../fixtures/dummy";
Pokey Rule8cac59a2025-04-24 12:21:19 +01003
4// Mock state updates for long-polling simulation
5let currentState = { ...initialState };
Pokey Rulee923bb32025-04-24 18:48:01 +01006const EMPTY_CONVERSATION =
7 new URL(window.location.href).searchParams.get("emptyConversation") === "1";
Pokey Rule8cac59a2025-04-24 12:21:19 +01008const ADD_NEW_MESSAGES =
9 new URL(window.location.href).searchParams.get("addNewMessages") === "1";
10
Pokey Rulee923bb32025-04-24 18:48:01 +010011const messages = EMPTY_CONVERSATION ? [] : [...initialMessages];
12
Pokey Rule8cac59a2025-04-24 12:21:19 +010013export 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];