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