-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
Expand file tree
/
Copy pathcopilot-web.ts
More file actions
760 lines (698 loc) · 27.1 KB
/
Copy pathcopilot-web.ts
File metadata and controls
760 lines (698 loc) · 27.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
/**
* CopilotWebExecutor — Microsoft Copilot Web Session Provider
*
* Routes requests through copilot.microsoft.com's WebSocket API using
* session credentials, translating between OpenAI chat completions format
* and Copilot's proprietary WebSocket event protocol.
*
* Auth: access_token from copilot.microsoft.com (extracted from browser
* DevTools or HAR file). Anonymous access supported with limited models.
*
* Protocol:
* 1. POST /c/api/start → conversationId
* 2. WS connect wss://copilot.microsoft.com/c/api/chat?api-version=2
* 3. Send: { event: "send", conversationId, content, mode }
* 4. Receive: stream of JSON events (appendText, done, error, etc.)
*/
import { BaseExecutor, type ExecuteInput } from "./base.ts";
import { FETCH_TIMEOUT_MS } from "../config/constants.ts";
import { createHash, randomBytes } from "node:crypto";
import { sanitizeErrorMessage } from "../utils/error.ts";
// ─── Constants ──────────────────────────────────────────────────────────────
const COPILOT_BASE = "https://copilot.microsoft.com";
const COPILOT_START_URL = `${COPILOT_BASE}/c/api/start`;
const COPILOT_WS_URL = "wss://copilot.microsoft.com/c/api/chat?api-version=2";
const COPILOT_USER_AGENT =
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/149.0.0.0 Safari/537.36";
// Model mapping: OmniRoute model ID → Copilot mode
const MODEL_MODE_MAP: Record<string, string> = {
copilot: "chat",
"copilot-chat": "chat",
"gpt-4o": "chat",
"gpt-4": "chat",
"copilot-think": "reasoning",
"copilot-think-deeper": "reasoning",
o1: "reasoning",
o3: "reasoning",
"copilot-smart": "smart",
"copilot-gpt5": "smart",
"gpt-5": "smart",
"copilot-study": "chat",
};
const DEFAULT_MODE = "chat";
// ─── Types ──────────────────────────────────────────────────────────────────
interface CopilotStartResponse {
currentConversationId?: string;
conversationId?: string;
remainingTurns?: number;
isBlocked?: boolean;
banExpiresAt?: string;
}
interface CopilotWsEvent {
event: string;
text?: string;
conversationId?: string;
url?: string;
thumbnailUrl?: string;
suggestions?: string[];
error?: string;
[key: string]: unknown;
}
type NodeWebSocketConstructor = new (
url: string | URL,
options?: { headers?: Record<string, string> }
) => WebSocket;
// ─── Helpers ────────────────────────────────────────────────────────────────
export function getCopilotMode(model?: string): string {
if (!model) return DEFAULT_MODE;
const lower = model.toLowerCase();
return MODEL_MODE_MAP[lower] || DEFAULT_MODE;
}
// Hashcash difficulty cap. Upstream supplies `difficulty`, so we clamp it to
// prevent a malicious/buggy server from forcing huge prefix allocations or
// effectively infinite work. 8 hex zeros = 2^32 expected iterations, already
// far beyond the ~10M iteration budget below.
const MAX_HASHCASH_DIFFICULTY = 8;
export function solveHashcash(parameter: string, difficulty: number): number | null {
if (!Number.isInteger(difficulty) || difficulty < 1 || difficulty > MAX_HASHCASH_DIFFICULTY) {
return null;
}
const prefix = "0".repeat(difficulty);
for (let i = 0; i < 10_000_000; i++) {
const hash = createHash("sha256").update(`${parameter}:${i}`).digest("hex");
if (hash.startsWith(prefix)) return i;
}
return null;
}
export function extractAccessToken(credential: string): string | null {
const trimmed = credential?.trim();
if (!trimmed) return null;
// Parse structured input before applying the direct-token heuristic. Real
// DevTools cookie/HAR exports routinely exceed 100 characters.
const accessTokenMatch = trimmed.match(
/(?:^|[\s;,{"'])access_token\s*[=:]\s*["']?([^\s;,}"']+)/i
);
if (accessTokenMatch) return accessTokenMatch[1];
const bearerMatch = trimmed.match(/(?:^|[\s:{"'])bearer\s+([^\s,}"';]+)/i);
if (bearerMatch) return bearerMatch[1];
// A named cookie is not an OAuth access token. Reject it instead of sending
// the full cookie value as `Authorization: Bearer ...`.
if (/^(?:[^=;\s]+=[^;]*)(?:;|$)/.test(trimmed) || /^(?:\{|\[)/.test(trimmed)) {
return null;
}
return trimmed;
}
export function buildCopilotWebSocketUrl(
accessToken?: string,
clientSessionId = crypto.randomUUID()
): string {
const url = new URL(COPILOT_WS_URL);
url.searchParams.set("clientSessionId", clientSessionId);
if (accessToken) {
// Copilot's browser client authenticates the WebSocket with this query
// parameter. Node's browser-compatible global WebSocket cannot set custom
// headers, so the previous header-only fallback silently lost auth on Node 22+.
url.searchParams.set("accessToken", accessToken);
}
return url.toString();
}
/* @testonly */ export function buildCopilotWebSocketHeaders(
accessToken: string
): Record<string, string> {
return { Authorization: `Bearer ${accessToken}` };
}
/**
* Map a token (or absence of one) to an in-memory session-pool key.
*
* Earlier iterations hashed the token with SHA-256, then with HMAC-SHA-256.
* Both forms left CodeQL's data-flow analysis tracing an OAuth bearer into
* a "fast" hash and re-raising `js/insufficient-password-hash`, even though
* the value is high-entropy and the output never leaves the process.
* bcrypt/scrypt/argon2 are the wrong tool here (they slow down brute-force
* of low-entropy human passwords we do not have).
*
* We instead key the in-memory `sessionPool` by the token itself. The token
* already lives in this process — embedded in `CopilotSession.cookies` for
* every entry — so this exposes nothing the runtime did not already hold.
* The map is capped at MAX_POOL_SIZE with LRU eviction, so memory remains
* bounded regardless of how many distinct tokens appear.
*
* See docs/security/PUBLIC_CREDS.md for the broader credential-handling
* pattern.
*/
export function sessionPoolKey(token?: string): string {
return token && token.length > 0 ? token : "anonymous";
}
// ─── Session Management ─────────────────────────────────────────────────────
interface CopilotSession {
conversationId: string;
cookies: string;
remainingTurns: number;
isBlocked: boolean;
createdAt: number;
}
// Shared session pool across all executor instances (singleton)
const sessionPool = new Map<string, CopilotSession>();
let sessionRotationCount = 0;
const MIN_REMAINING_TURNS = 5;
const MAX_ROTATIONS = 1000;
const MAX_POOL_SIZE = 100;
// ─── Executor ───────────────────────────────────────────────────────────────
export class CopilotWebExecutor extends BaseExecutor {
constructor() {
super("copilot-web", { id: "copilot-web", baseUrl: COPILOT_START_URL });
}
/**
* Get or create a session. Rotates when remainingTurns is low or blocked.
*/
private async getSession(accessToken?: string, signal?: AbortSignal): Promise<CopilotSession> {
const poolKey = sessionPoolKey(accessToken);
const existing = sessionPool.get(poolKey);
if (
existing &&
!existing.isBlocked &&
existing.remainingTurns > MIN_REMAINING_TURNS &&
Date.now() - existing.createdAt < 3_600_000 // 1 hour max session age
) {
return existing;
}
// Create new session (rotate)
if (sessionRotationCount >= MAX_ROTATIONS) {
// Reset counter after max rotations (prevent memory leak)
sessionRotationCount = 0;
}
const session = await this.createSession(accessToken, signal);
// Evict oldest entry if pool is at capacity (Map preserves insertion order)
if (sessionPool.size >= MAX_POOL_SIZE) {
sessionPool.delete(sessionPool.keys().next().value!);
}
sessionPool.set(poolKey, session);
sessionRotationCount++;
return session;
}
/**
* Create a fresh session with new cookies and conversationId.
*/
private async createSession(accessToken?: string, signal?: AbortSignal): Promise<CopilotSession> {
const headers: Record<string, string> = {
"Content-Type": "application/json",
"User-Agent": COPILOT_USER_AGENT,
Origin: COPILOT_BASE,
Referer: `${COPILOT_BASE}/`,
};
if (accessToken) {
headers["Authorization"] = `Bearer ${accessToken}`;
}
const res = await fetch(COPILOT_START_URL, {
method: "POST",
headers,
body: JSON.stringify({
timeZone: "America/New_York",
startNewConversation: true,
teenSupportEnabled: false,
}),
signal,
});
if (!res.ok) {
const text = await res.text().catch(() => "");
throw new Error(`Copilot /c/api/start failed (${res.status}): ${text.slice(0, 200)}`);
}
const data = (await res.json()) as CopilotStartResponse;
const convId = data.currentConversationId || data.conversationId;
if (!convId) {
throw new Error("Copilot /c/api/start returned no conversationId");
}
// Extract cookies from response
const setCookies = res.headers.getSetCookie();
const cookies = setCookies.map((c) => c.split(";")[0]).join("; ");
return {
conversationId: convId,
cookies,
remainingTurns: data.remainingTurns ?? 1000,
isBlocked: data.isBlocked ?? false,
createdAt: Date.now(),
};
}
/**
* Send a message via WebSocket and collect the streamed response.
*/
private async wsChat(
conversationId: string,
prompt: string,
mode: string,
accessToken?: string,
signal?: AbortSignal
): Promise<ReadableStream<Uint8Array>> {
const wsUrl = buildCopilotWebSocketUrl(accessToken);
return new ReadableStream(
{
start: async (controller) => {
const encoder = new TextEncoder();
let ws: WebSocket | null = null;
let settled = false;
const cleanup = () => {
if (ws) {
try {
ws.close();
} catch {
/* ignore */
}
ws = null;
}
};
const finish = () => {
if (settled) return;
settled = true;
cleanup();
controller.enqueue(encoder.encode("data: [DONE]\n\n"));
controller.close();
};
const abort = (reason?: string) => {
if (settled) return;
settled = true;
cleanup();
if (reason) {
controller.enqueue(
encoder.encode(`data: ${JSON.stringify({ error: { message: reason } })}\n\n`)
);
}
controller.close();
};
// Handle upstream abort signal
signal?.addEventListener("abort", () => abort("Request aborted"), { once: true });
try {
// Authentication is present in wsUrl for both transports. The Node
// fallback also preserves the Authorization header where supported.
const BrowserWebSocket = globalThis.WebSocket;
if (BrowserWebSocket) {
ws = new BrowserWebSocket(wsUrl);
} else {
// @ts-ignore — ws module has no type declarations in this project
const NodeWebSocket = (await import("ws"))
.default as unknown as NodeWebSocketConstructor;
ws = new NodeWebSocket(
wsUrl,
accessToken ? { headers: buildCopilotWebSocketHeaders(accessToken) } : undefined
);
}
const timeout = setTimeout(() => abort("Copilot WebSocket timeout"), FETCH_TIMEOUT_MS);
let chatSent = false;
const sendChat = () => {
if (chatSent) return;
chatSent = true;
ws!.send(
JSON.stringify({
event: "send",
conversationId,
content: [{ type: "text", text: prompt }],
mode,
})
);
};
ws.onopen = () => {
sendChat();
};
ws.onmessage = (ev: MessageEvent) => {
try {
const event: CopilotWsEvent =
typeof ev.data === "string" ? JSON.parse(ev.data) : JSON.parse(String(ev.data));
switch (event.event) {
case "challenge": {
if (event.method === "hashcash" && event.parameter) {
const parts = String(event.parameter).split(":");
const param = parts[0];
const difficulty = parseInt(parts[1] || "1", 10);
const solution = solveHashcash(param, difficulty);
ws!.send(
JSON.stringify({
event: "challengeResponse",
token: solution !== null ? String(solution) : "",
method: "hashcash",
})
);
// Re-send chat after solving challenge
chatSent = false;
sendChat();
} else if (event.method === "cloudflare") {
abort(
"Copilot requires Cloudflare Turnstile verification. Use an authenticated session (access_token) instead."
);
} else {
abort(
`Copilot challenge "${event.method}" not supported. Use an authenticated session.`
);
}
break;
}
case "appendText": {
if (event.text) {
const chunk = {
id: `chatcmpl-copilot-${Date.now()}`,
object: "chat.completion.chunk",
created: Math.floor(Date.now() / 1000),
model: "copilot",
choices: [
{
index: 0,
delta: { content: event.text },
finish_reason: null,
},
],
};
controller.enqueue(encoder.encode(`data: ${JSON.stringify(chunk)}\n\n`));
}
break;
}
case "chainOfThought": {
if (event.text) {
const chunk = {
id: `chatcmpl-copilot-${Date.now()}`,
object: "chat.completion.chunk",
created: Math.floor(Date.now() / 1000),
model: "copilot",
choices: [
{
index: 0,
delta: { reasoning_content: event.text },
finish_reason: null,
},
],
};
controller.enqueue(encoder.encode(`data: ${JSON.stringify(chunk)}\n\n`));
}
break;
}
case "replaceText": {
if (event.text) {
const chunk = {
id: `chatcmpl-copilot-${Date.now()}`,
object: "chat.completion.chunk",
created: Math.floor(Date.now() / 1000),
model: "copilot",
choices: [
{
index: 0,
delta: { content: event.text },
finish_reason: null,
},
],
};
controller.enqueue(encoder.encode(`data: ${JSON.stringify(chunk)}\n\n`));
}
break;
}
case "imageGenerated": {
if (event.url) {
const chunk = {
id: `chatcmpl-copilot-${Date.now()}`,
object: "chat.completion.chunk",
created: Math.floor(Date.now() / 1000),
model: "copilot",
choices: [
{
index: 0,
delta: {
content: [
{
type: "image_url",
image_url: { url: event.url, detail: "auto" },
},
],
},
finish_reason: null,
},
],
};
controller.enqueue(encoder.encode(`data: ${JSON.stringify(chunk)}\n\n`));
}
break;
}
case "citation": {
if (event.url) {
const annotation = {
type: "url_citation",
url_citation: {
url: event.url,
title: event.title || event.url,
},
};
const chunk = {
id: `chatcmpl-copilot-${Date.now()}`,
object: "chat.completion.chunk",
created: Math.floor(Date.now() / 1000),
model: "copilot",
choices: [
{
index: 0,
delta: { annotations: [annotation] },
finish_reason: null,
},
],
};
controller.enqueue(encoder.encode(`data: ${JSON.stringify(chunk)}\n\n`));
}
break;
}
case "suggestedFollowups": {
if (event.suggestions && Array.isArray(event.suggestions)) {
const chunk = {
id: `chatcmpl-copilot-${Date.now()}`,
object: "chat.completion.chunk",
created: Math.floor(Date.now() / 1000),
model: "copilot",
choices: [
{
index: 0,
delta: {
content: `\n\n**Suggested follow-ups:**\n${event.suggestions.map((s: string) => `- ${s}`).join("\n")}`,
},
finish_reason: null,
},
],
};
controller.enqueue(encoder.encode(`data: ${JSON.stringify(chunk)}\n\n`));
}
break;
}
case "done": {
clearTimeout(timeout);
const finalChunk = {
id: `chatcmpl-copilot-${Date.now()}`,
object: "chat.completion.chunk",
created: Math.floor(Date.now() / 1000),
model: "copilot",
choices: [
{
index: 0,
delta: {},
finish_reason: "stop",
},
],
};
controller.enqueue(encoder.encode(`data: ${JSON.stringify(finalChunk)}\n\n`));
finish();
break;
}
case "error": {
clearTimeout(timeout);
abort(event.error || "Copilot stream error");
break;
}
// Ignore other events: connected, received, citation, etc.
default:
break;
}
} catch {
// Skip unparseable messages
}
};
ws.onerror = (err: Event) => {
clearTimeout(timeout);
const msg = sanitizeErrorMessage(
(err as ErrorEvent).message || "Copilot WebSocket error"
);
abort(msg);
};
ws.onclose = () => {
clearTimeout(timeout);
finish();
};
} catch (err) {
abort(
sanitizeErrorMessage(
err instanceof Error ? err.message : "Failed to connect to Copilot"
)
);
}
},
},
{ highWaterMark: 16384 }
);
}
/**
* Main execute method — translates OpenAI format to Copilot WebSocket protocol.
*/
async execute(input: ExecuteInput): Promise<{
response: Response;
url: string;
headers: Record<string, string>;
transformedBody: unknown;
}> {
const { credentials, signal, model: inputModel, stream: inputStream } = input;
const body = input.body as Record<string, unknown> | undefined;
const model = inputModel || (body?.model as string) || "copilot";
const mode = getCopilotMode(model);
const stream = inputStream !== false; // Default to streaming
// Extract access token from credentials
const rawCred =
credentials?.apiKey || (credentials?.providerSpecificData?.cookie as string) || "";
const accessToken = extractAccessToken(rawCred);
// Extract prompt from messages
const messages = (body?.messages as Array<Record<string, unknown>>) || [];
const userMsg = messages.filter((m) => m.role === "user").pop();
const systemMsgs = messages.filter((m) => m.role === "system");
const prompt = (userMsg?.content as string) || "";
if (!prompt || (typeof prompt === "string" && !prompt.trim())) {
return {
response: new Response(JSON.stringify({ error: { message: "No user message provided" } }), {
status: 400,
headers: { "Content-Type": "application/json" },
}),
url: COPILOT_START_URL,
headers: {},
transformedBody: null,
};
}
// Build full prompt with system instructions
let fullPrompt = "";
if (systemMsgs.length > 0) {
const sysText = systemMsgs
.map((m) => (typeof m.content === "string" ? m.content : ""))
.filter(Boolean)
.join("\n");
if (sysText) fullPrompt += `[System Instructions]\n${sysText}\n\n`;
}
fullPrompt += typeof prompt === "string" ? prompt : JSON.stringify(prompt);
// Get or create session (auto-rotates when turns exhausted)
let conversationId: string;
let sessionCookies = "";
try {
const session = await this.getSession(accessToken || undefined, signal);
conversationId = session.conversationId;
sessionCookies = session.cookies;
} catch (err) {
const msg = sanitizeErrorMessage(
err instanceof Error ? err.message : "Failed to start Copilot conversation"
);
return {
response: new Response(JSON.stringify({ error: { message: msg } }), {
status: 502,
headers: { "Content-Type": "application/json" },
}),
url: COPILOT_START_URL,
headers: {},
transformedBody: { conversationId: null, mode, prompt: fullPrompt.slice(0, 100) },
};
}
// Non-streaming: collect all chunks and return as single response
if (!stream) {
try {
const wsStream = await this.wsChat(
conversationId,
fullPrompt,
mode,
accessToken || undefined,
signal
);
const reader = wsStream.getReader();
const decoder = new TextDecoder();
let fullText = "";
let reasoningText = "";
while (true) {
const { done, value } = await reader.read();
if (done) break;
const lines = decoder.decode(value, { stream: true }).split("\n");
for (const line of lines) {
if (!line.startsWith("data: ")) continue;
const data = line.slice(6).trim();
if (data === "[DONE]") continue;
try {
const parsed = JSON.parse(data);
const delta = parsed.choices?.[0]?.delta;
if (delta?.content) fullText += delta.content;
if (delta?.reasoning_content) reasoningText += delta.reasoning_content;
} catch {
/* skip */
}
}
}
const result = {
id: `chatcmpl-copilot-${Date.now()}`,
object: "chat.completion",
created: Math.floor(Date.now() / 1000),
model,
choices: [
{
index: 0,
message: { role: "assistant", content: fullText || "(empty response)" },
finish_reason: "stop",
},
],
usage: { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 },
};
return {
response: new Response(JSON.stringify(result), {
headers: { "Content-Type": "application/json" },
}),
url: COPILOT_WS_URL,
headers: {},
transformedBody: { conversationId, mode, prompt: fullPrompt.slice(0, 100) },
};
} catch (err) {
const msg = err instanceof Error ? err.message : "Copilot non-streaming error";
return {
response: new Response(JSON.stringify({ error: { message: msg } }), {
status: 502,
headers: { "Content-Type": "application/json" },
}),
url: COPILOT_WS_URL,
headers: {},
transformedBody: { conversationId, mode },
};
}
}
// Streaming: pipe WebSocket events as SSE
try {
const wsStream = await this.wsChat(
conversationId,
fullPrompt,
mode,
accessToken || undefined,
signal
);
return {
response: new Response(wsStream, {
headers: {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
Connection: "keep-alive",
},
}),
url: COPILOT_WS_URL,
headers: {},
transformedBody: { conversationId, mode, prompt: fullPrompt.slice(0, 100) },
};
} catch (err) {
const msg = err instanceof Error ? err.message : "Copilot streaming error";
return {
response: new Response(JSON.stringify({ error: { message: msg } }), {
status: 502,
headers: { "Content-Type": "application/json" },
}),
url: COPILOT_WS_URL,
headers: {},
transformedBody: { conversationId, mode },
};
}
}
}