-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
Expand file tree
/
Copy pathconol-web.ts
More file actions
893 lines (825 loc) · 29.5 KB
/
Copy pathconol-web.ts
File metadata and controls
893 lines (825 loc) · 29.5 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
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
/**
* ConolExecutor — conol.ai browser-session chat (Unofficial/Experimental).
*
* Protocol verified against the web client on 2026-07-30:
* - POST /api/assets for raw image uploads
* - POST /api/sessions to create a session
* - POST /api/sessions/{id}/model to pin preset, then model, then effort
* - POST /api/sessions/{id}/messages to submit a turn
* - GET /api/sessions/{id}/messages?logDeltas=1 for cumulative NDJSON updates
* - Cookie authentication via __Secure-better-auth.session_token
*
* Session creation ignores agentModel/agentEffort and answers with
* `modelDowngraded: true` on the account default, so the session is always
* created empty and configured via /model before the first turn is submitted.
*/
import { createHash } from "node:crypto";
import { BaseExecutor, mergeAbortSignals, type ExecuteInput } from "./base.ts";
import { makeExecutorErrorResult as makeErrorResult } from "../utils/error.ts";
import { CursorImageError, extractImageUrls, resolveCursorImages } from "../utils/cursorImages.ts";
import { normalizeConolCookie, resolveConolCredentials } from "../services/conolAuth.ts";
import { resolveConolModelSelection, type ConolEffort } from "../services/conolModels.ts";
import {
applyConolSessionModel,
buildConolSessionModelPlan,
} from "../services/conolSessionModel.ts";
export { normalizeConolCookie, resolveConolCredentials };
const CONOL_ORIGIN = "https://conol.ai";
const CONOL_SESSION_URL = `${CONOL_ORIGIN}/api/sessions`;
const CONOL_REQUEST_TIMEOUT_MS = 300_000;
const CONOL_MAX_STREAM_BYTES = 16 * 1024 * 1024;
const CONOL_SESSION_TTL_MS = 6 * 60 * 60 * 1000;
const CONOL_MAX_SESSION_BINDINGS = 500;
const USER_AGENT =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/150.0.0.0 Safari/537.36";
interface ChatMessage {
role: string;
content: unknown;
}
interface ConolRequestBody {
messages?: ChatMessage[];
model?: string;
timezone?: string;
metadata?: unknown;
conversation_id?: unknown;
conversationId?: unknown;
session_id?: unknown;
sessionId?: unknown;
prompt_cache_key?: unknown;
promptCacheKey?: unknown;
}
interface ConolMessagePart {
type: "text" | "image";
content: string;
mediaType?: string;
}
interface ConolUserTurn {
text: string;
imageUrls: string[];
}
interface ConolSessionBinding {
upstreamSessionId: string;
lastUsedAt: number;
/** Model preset already primed on this session — sent once, not per turn. */
presetApplied: boolean;
/** Model/effort currently pinned upstream, so we only re-pin on an actual switch. */
appliedModel: string;
appliedEffort: ConolEffort | null;
/** Conol wants `hasImageHistory` sticky once the session has seen an image. */
hasImageHistory: boolean;
}
export interface ParsedConolStream {
text: string;
usedTokens: number | null;
contextWindow: number | null;
modelId: string;
done: boolean;
}
const conolSessionBindings = new Map<string, ConolSessionBinding>();
const conolSessionLocks = new Map<string, Promise<void>>();
function readString(value: unknown): string {
return typeof value === "string" ? value.trim() : "";
}
function extractText(value: unknown): string {
if (typeof value === "string") return value;
if (value == null) return "";
if (Array.isArray(value)) {
return value
.map((item) => extractText(item))
.filter(Boolean)
.join("\n");
}
if (typeof value !== "object") return "";
const record = value as Record<string, unknown>;
const type = readString(record.type).toLowerCase();
if (type === "image_url" || type === "input_image" || type === "image") return "";
return (
readString(record.text) ||
(typeof record.content === "string" ? record.content : extractText(record.content)) ||
extractText(record.output) ||
extractText(record.result)
);
}
function extractUserText(value: unknown): string {
if (typeof value === "string") return value;
if (value == null) return "";
if (Array.isArray(value)) {
return value
.map((item) => extractUserText(item))
.filter(Boolean)
.join("\n");
}
if (typeof value !== "object") return "";
const record = value as Record<string, unknown>;
const type = readString(record.type).toLowerCase();
if (type === "text" || type === "input_text" || type === "output_text") {
return readString(record.text) || readString(record.content);
}
if (type) {
// Conol owns the agent loop. Do not flatten tool calls/results, images, or
// other agentic protocol blocks into the user's text prompt.
return "";
}
return readString(record.text) || extractUserText(record.content);
}
function stripGeneratedImageMarkers(value: string): string {
return value
.replace(/^\s*\[Image\s+\d+\]:\s*\(unavailable\)\s*$/gim, "")
.replace(/^\s*\[Image:\s*source:\s*[^\]\r\n]+\]\s*$/gim, "")
.replace(/\n{3,}/g, "\n\n")
.trim();
}
export function buildConolUserTurn(messages: ChatMessage[]): ConolUserTurn {
const latestUserMessage = [...messages]
.reverse()
.find((message) => readString(message.role).toLowerCase() === "user");
if (!latestUserMessage) return { text: "", imageUrls: [] };
return {
text: stripGeneratedImageMarkers(extractUserText(latestUserMessage.content)),
imageUrls: extractImageUrls(latestUserMessage.content),
};
}
export function buildConolPromptText(messages: ChatMessage[]): string {
return buildConolUserTurn(messages).text;
}
function readHeader(headers: Record<string, string> | null | undefined, name: string): string {
if (!headers) return "";
const direct = readString(headers[name]);
if (direct) return direct;
const normalizedName = name.toLowerCase();
for (const [key, value] of Object.entries(headers)) {
if (key.toLowerCase() === normalizedName) return readString(value);
}
return "";
}
function readMetadataSessionId(metadata: unknown): string {
if (!metadata || typeof metadata !== "object" || Array.isArray(metadata)) return "";
const record = metadata as Record<string, unknown>;
const direct = readString(record.session_id) || readString(record.sessionId);
if (direct) return direct;
const userId = record.user_id;
if (userId && typeof userId === "object" && !Array.isArray(userId)) {
return readString((userId as Record<string, unknown>).session_id);
}
if (typeof userId !== "string" || userId.length > 4096) return "";
try {
const parsed = JSON.parse(userId) as unknown;
return parsed && typeof parsed === "object" && !Array.isArray(parsed)
? readString((parsed as Record<string, unknown>).session_id)
: "";
} catch {
return "";
}
}
function hashKey(value: string): string {
return createHash("sha256").update(value).digest("hex");
}
export function resolveConolClientSessionKey(
body: ConolRequestBody,
clientHeaders?: Record<string, string> | null
): string | null {
const candidates = [
readHeader(clientHeaders, "x-claude-code-session-id"),
readHeader(clientHeaders, "x-codex-session-id"),
readHeader(clientHeaders, "x-session-id"),
readHeader(clientHeaders, "x_session_id"),
readHeader(clientHeaders, "session-id"),
readHeader(clientHeaders, "session_id"),
readHeader(clientHeaders, "x-omniroute-session-id"),
readHeader(clientHeaders, "x-omniroute-session"),
readMetadataSessionId(body.metadata),
readString(body.conversation_id),
readString(body.conversationId),
readString(body.session_id),
readString(body.sessionId),
readString(body.prompt_cache_key),
readString(body.promptCacheKey),
];
const candidate = candidates.find((value) => value.length > 0 && value.length <= 4096);
return candidate ? hashKey(candidate) : null;
}
function sweepConolSessionBindings(now = Date.now()): void {
for (const [key, binding] of conolSessionBindings) {
if (now - binding.lastUsedAt > CONOL_SESSION_TTL_MS) {
conolSessionBindings.delete(key);
}
}
while (conolSessionBindings.size > CONOL_MAX_SESSION_BINDINGS) {
let oldestKey = "";
let oldestTime = Number.POSITIVE_INFINITY;
for (const [key, binding] of conolSessionBindings) {
if (binding.lastUsedAt < oldestTime) {
oldestKey = key;
oldestTime = binding.lastUsedAt;
}
}
if (!oldestKey) break;
conolSessionBindings.delete(oldestKey);
}
}
function getConolSessionBinding(key: string): ConolSessionBinding | null {
sweepConolSessionBindings();
const binding = conolSessionBindings.get(key);
if (!binding) return null;
binding.lastUsedAt = Date.now();
return binding;
}
function setConolSessionBinding(
key: string,
binding: Omit<ConolSessionBinding, "lastUsedAt">
): void {
conolSessionBindings.set(key, { ...binding, lastUsedAt: Date.now() });
sweepConolSessionBindings();
}
/**
* Model/effort are deliberately excluded: switching models must re-pin the
* existing Conol session (POST /model) rather than stranding it and losing the
* conversation history.
*/
function buildConolSessionBindingKey(
input: ExecuteInput,
cookie: string,
clientSessionKey: string
): string {
const accountKey = input.credentials.connectionId
? `connection:${hashKey(input.credentials.connectionId)}`
: `cookie:${hashKey(cookie)}`;
return hashKey(`${accountKey}:${clientSessionKey}`);
}
async function withConolSessionLock<T>(
key: string | null,
operation: () => Promise<T>
): Promise<T> {
if (!key) return operation();
const previous = conolSessionLocks.get(key) ?? Promise.resolve();
let releaseCurrent!: () => void;
const currentGate = new Promise<void>((resolve) => {
releaseCurrent = resolve;
});
const current = previous.catch(() => undefined).then(() => currentGate);
conolSessionLocks.set(key, current);
await previous.catch(() => undefined);
try {
return await operation();
} finally {
releaseCurrent();
if (conolSessionLocks.get(key) === current) {
conolSessionLocks.delete(key);
}
}
}
export function clearConolSessionBindingsForTests(): void {
conolSessionBindings.clear();
conolSessionLocks.clear();
}
/** True when this turn continues a session we created on an earlier request. */
function reusedSessionCandidate(
cachedBinding: ConolSessionBinding | null,
sessionId: string
): boolean {
return !!cachedBinding && cachedBinding.upstreamSessionId === sessionId;
}
function messageText(value: unknown): string {
if (!value || typeof value !== "object" || Array.isArray(value)) return "";
const message = value as Record<string, unknown>;
if (readString(message.role).toLowerCase() !== "assistant") return "";
return extractText(message.content).trim();
}
function stageAssistantText(stages: unknown, field: "logs" | "preview"): string {
if (!Array.isArray(stages)) return "";
let result = "";
for (const stage of stages) {
if (!stage || typeof stage !== "object" || Array.isArray(stage)) continue;
const entries = (stage as Record<string, unknown>)[field];
if (!Array.isArray(entries)) continue;
for (const entry of entries) {
const text = messageText(entry);
if (text) result = text;
}
}
return result;
}
function parseEventLine(originalLine: string): unknown | null {
let line = originalLine.trim();
if (!line || line.startsWith(":") || line.startsWith("event:")) return null;
if (line.startsWith("data:")) line = line.slice(5).trim();
if (line.startsWith("message\t")) line = line.slice("message\t".length);
if (!line) return null;
if (line === "[DONE]") return { type: "done" };
try {
return JSON.parse(line);
} catch {
// Ignore non-JSON keepalive and timestamp lines.
return null;
}
}
function isDoneEvent(value: unknown): boolean {
return (
!!value &&
typeof value === "object" &&
!Array.isArray(value) &&
readString((value as Record<string, unknown>).type) === "done"
);
}
function parseEventLines(raw: string): unknown[] {
const events: unknown[] = [];
for (const line of raw.replace(/\r\n/g, "\n").split("\n")) {
const event = parseEventLine(line);
if (event) events.push(event);
}
return events;
}
/**
* Conol emits a terminal `done` event but keeps the HTTP stream open. Reading
* `response.text()` therefore waits until the request timeout even though the
* assistant answer is already complete. Consume complete lines and cancel the
* reader as soon as `done` arrives.
*/
export async function collectConolMessageStream(response: Response): Promise<string> {
if (!response.body) return response.text();
const reader = response.body.getReader();
const decoder = new TextDecoder();
const lines: string[] = [];
let pending = "";
let totalBytes = 0;
let doneEventReceived = false;
try {
while (!doneEventReceived) {
const chunk = await reader.read();
if (chunk.done) {
pending += decoder.decode();
break;
}
totalBytes += chunk.value.byteLength;
if (totalBytes > CONOL_MAX_STREAM_BYTES) {
throw new Error("Conol message stream exceeded the safety limit");
}
pending += decoder.decode(chunk.value, { stream: true });
const completeLines = pending.split(/\r?\n/);
pending = completeLines.pop() ?? "";
for (const line of completeLines) {
lines.push(line);
if (isDoneEvent(parseEventLine(line))) {
doneEventReceived = true;
break;
}
}
}
if (!doneEventReceived && pending) lines.push(pending);
} finally {
if (doneEventReceived) {
try {
await reader.cancel();
} catch {
// The upstream may close at the same instant as its done event.
}
} else {
reader.releaseLock();
}
}
return lines.join("\n");
}
export function parseConolMessageStream(raw: string): ParsedConolStream {
let finalizedText = "";
let previewText = "";
let streamedText = "";
let usedTokens: number | null = null;
let contextWindow: number | null = null;
let modelId = "";
let done = false;
for (const value of parseEventLines(raw)) {
if (!value || typeof value !== "object" || Array.isArray(value)) continue;
const event = value as Record<string, unknown>;
const type = readString(event.type);
if (type === "done") {
done = true;
continue;
}
const finalCandidate = stageAssistantText(event.stages, "logs");
const previewCandidate = stageAssistantText(event.stages, "preview");
if (finalCandidate) finalizedText = finalCandidate;
if (previewCandidate) previewText = previewCandidate;
if (type === "assistant") {
const direct = extractText(event.content ?? event.message ?? event.text).trim();
if (direct) finalizedText = direct;
} else if (type === "stream_event") {
const delta = extractText(event.delta ?? event.content ?? event.text);
if (delta) streamedText += delta;
}
const context =
event.contextUsage &&
typeof event.contextUsage === "object" &&
!Array.isArray(event.contextUsage)
? (event.contextUsage as Record<string, unknown>)
: null;
if (context) {
const used = Number(context.usedTokens);
const window = Number(context.contextWindow);
if (Number.isFinite(used)) usedTokens = used;
if (Number.isFinite(window)) contextWindow = window;
modelId = readString(context.modelId) || modelId;
}
}
return {
text: finalizedText || previewText || streamedText,
usedTokens,
contextWindow,
modelId,
done,
};
}
function conolHeaders(
cookie: string,
extra?: Record<string, string>,
sessionId?: string
): Record<string, string> {
return {
accept: "application/json",
"accept-language": "en-US,en;q=0.9",
cookie,
origin: CONOL_ORIGIN,
referer: sessionId
? `${CONOL_ORIGIN}/home?chat_session=${encodeURIComponent(sessionId)}`
: `${CONOL_ORIGIN}/home`,
"user-agent": USER_AGENT,
...extra,
};
}
function safeTimezone(value: unknown): string {
const explicit = readString(value);
if (/^[A-Za-z_+-]+(?:\/[A-Za-z0-9_+-]+)*$/.test(explicit)) return explicit;
try {
return Intl.DateTimeFormat().resolvedOptions().timeZone || "UTC";
} catch {
return "UTC";
}
}
async function uploadConolImages(
cookie: string,
imageUrls: string[],
signal?: AbortSignal | null,
sessionId?: string
): Promise<ConolMessagePart[]> {
// Conol's asset endpoint stores the original bytes (no Cursor wire prep).
const images = await resolveCursorImages(imageUrls, { prepareForWire: false });
const parts: ConolMessagePart[] = [];
for (const image of images) {
const response = await fetch(`${CONOL_ORIGIN}/api/assets`, {
method: "POST",
headers: conolHeaders(
cookie,
{
accept: "application/json",
"content-type": image.mimeType,
},
sessionId
),
body: new Uint8Array(image.data),
signal: signal ?? undefined,
});
if (!response.ok) {
throw new Error(`Conol image upload failed (HTTP ${response.status})`);
}
const payload = (await response.json()) as Record<string, unknown>;
const id = readString(payload.id);
if (!/^[A-Za-z0-9_-]+$/.test(id)) {
throw new Error("Conol image upload returned an invalid asset ID");
}
parts.push({
type: "image",
content: `/api/assets/${id}`,
mediaType: readString(payload.mediaType) || image.mimeType,
});
}
return parts;
}
function estimateTokens(text: string): number {
return Math.max(0, Math.ceil(text.length / 4));
}
function completionResponse(
text: string,
model: string,
sessionId: string,
prompt: string
): Response {
const promptTokens = estimateTokens(prompt);
const completionTokens = estimateTokens(text);
return new Response(
JSON.stringify({
id: `chatcmpl-conol-${sessionId}`,
object: "chat.completion",
created: Math.floor(Date.now() / 1000),
model,
choices: [
{
index: 0,
message: { role: "assistant", content: text },
finish_reason: "stop",
},
],
usage: {
prompt_tokens: promptTokens,
completion_tokens: completionTokens,
total_tokens: promptTokens + completionTokens,
},
}),
{ status: 200, headers: { "content-type": "application/json" } }
);
}
function streamResponse(text: string, model: string, sessionId: string): Response {
const encoder = new TextEncoder();
const id = `chatcmpl-conol-${sessionId}`;
const created = Math.floor(Date.now() / 1000);
const readable = new ReadableStream({
start(controller) {
const chunks = [
{
id,
object: "chat.completion.chunk",
created,
model,
choices: [{ index: 0, delta: { role: "assistant", content: text }, finish_reason: null }],
},
{
id,
object: "chat.completion.chunk",
created,
model,
choices: [{ index: 0, delta: {}, finish_reason: "stop" }],
},
];
for (const chunk of chunks) {
controller.enqueue(encoder.encode(`data: ${JSON.stringify(chunk)}\n\n`));
}
controller.enqueue(encoder.encode("data: [DONE]\n\n"));
controller.close();
},
});
return new Response(readable, {
status: 200,
headers: {
"content-type": "text/event-stream",
"cache-control": "no-cache",
connection: "keep-alive",
},
});
}
export class ConolWebExecutor extends BaseExecutor {
constructor() {
super("conol-web", { id: "conol-web", baseUrl: CONOL_SESSION_URL });
}
async execute(input: ExecuteInput) {
const requestBody = (input.body || {}) as ConolRequestBody;
const messages = Array.isArray(requestBody.messages) ? requestBody.messages : [];
const userTurn = buildConolUserTurn(messages);
const prompt = userTurn.text;
const imageUrls = userTurn.imageUrls;
if (!prompt && imageUrls.length === 0) {
return makeErrorResult(
400,
"No user message found",
{ model: input.model },
CONOL_SESSION_URL
);
}
const { cookie } = resolveConolCredentials(input.credentials);
if (!cookie) {
return makeErrorResult(
401,
"Missing Conol session cookie — sign in with the browser or paste the Cookie header",
{ model: input.model },
CONOL_SESSION_URL
);
}
const { model, effort, effortExplicit } = resolveConolModelSelection(
input.model || requestBody.model
);
const clientSessionKey = resolveConolClientSessionKey(requestBody, input.clientHeaders);
const sessionBindingKey = clientSessionKey
? buildConolSessionBindingKey(input, cookie, clientSessionKey)
: null;
const timeoutSignal = AbortSignal.timeout(CONOL_REQUEST_TIMEOUT_MS);
const upstreamSignal = input.signal
? mergeAbortSignals(input.signal, timeoutSignal)
: timeoutSignal;
try {
return await withConolSessionLock(sessionBindingKey, async () => {
if (upstreamSignal.aborted) {
throw upstreamSignal.reason ?? new DOMException("Aborted", "AbortError");
}
const cachedBinding = sessionBindingKey ? getConolSessionBinding(sessionBindingKey) : null;
let sessionId = cachedBinding?.upstreamSessionId || "";
let reusedSession = false;
let presetApplied = cachedBinding?.presetApplied ?? false;
let appliedModel = cachedBinding?.appliedModel ?? "";
let appliedEffort: ConolEffort | null = cachedBinding?.appliedEffort ?? null;
const imageParts = await uploadConolImages(
cookie,
imageUrls,
upstreamSignal,
sessionId || undefined
);
const parts: ConolMessagePart[] = [...imageParts];
if (prompt) parts.push({ type: "text", content: prompt });
const timezone = safeTimezone(requestBody.timezone);
// Sticky: once a session has carried an image, Conol keeps treating it as
// multimodal, which drives preset text/multimodal model resolution.
const hasImageHistory = (cachedBinding?.hasImageHistory ?? false) || imageParts.length > 0;
// Conol ignores agentModel/agentEffort on session creation, so create the
// session empty and configure it before any turn is submitted. Otherwise the
// very first turn silently runs on the downgraded account default.
if (!sessionId) {
const createResponse = await fetch(CONOL_SESSION_URL, {
method: "POST",
headers: conolHeaders(cookie, { "content-type": "application/json" }),
body: JSON.stringify({ source: { type: "home" }, messages: [], timezone }),
signal: upstreamSignal,
});
if (createResponse.status === 401 || createResponse.status === 403) {
return makeErrorResult(
createResponse.status,
"Conol session expired or is invalid — sign in again",
{ model },
CONOL_SESSION_URL
);
}
if (!createResponse.ok) {
return makeErrorResult(
createResponse.status,
`Conol session creation failed (HTTP ${createResponse.status})`,
{ model },
CONOL_SESSION_URL
);
}
const created = (await createResponse.json()) as Record<string, unknown>;
sessionId = readString(created.sessionId);
if (!/^[A-Za-z0-9_-]+$/.test(sessionId)) {
return makeErrorResult(
502,
"Conol returned an invalid session identifier",
{ model },
CONOL_SESSION_URL
);
}
presetApplied = false;
appliedModel = "";
appliedEffort = null;
}
const plan = buildConolSessionModelPlan({ model, effort, hasImageHistory });
const desiredEffort = plan.effort?.agentEffort ?? null;
// Re-pin only on a real change: a new session, a model switch, or an
// effort switch. Steady-state follow-ups cost no extra round trips.
const needsModelUpdate =
!presetApplied || appliedModel !== model || appliedEffort !== desiredEffort;
if (needsModelUpdate) {
const configured = await applyConolSessionModel({
sessionId,
plan,
skipPreset: presetApplied,
buildHeaders: (id) => conolHeaders(cookie, undefined, id),
signal: upstreamSignal,
onWarning: (message) => input.log?.warn?.("conol-web", message),
});
presetApplied = presetApplied || configured.presetApplied;
if (configured.modelApplied) {
appliedModel = model;
appliedEffort = configured.effortApplied;
}
}
if (reusedSessionCandidate(cachedBinding, sessionId)) {
const followUpUrl = `${CONOL_SESSION_URL}/${sessionId}/messages`;
const followUpResponse = await fetch(followUpUrl, {
method: "POST",
headers: conolHeaders(cookie, { "content-type": "application/json" }, sessionId),
body: JSON.stringify({ messages: parts, timezone }),
signal: upstreamSignal,
});
if (followUpResponse.status === 401 || followUpResponse.status === 403) {
return makeErrorResult(
followUpResponse.status,
"Conol session expired or is invalid — sign in again",
{ model },
followUpUrl
);
}
if (followUpResponse.status === 404 || followUpResponse.status === 410) {
if (sessionBindingKey) conolSessionBindings.delete(sessionBindingKey);
return makeErrorResult(
followUpResponse.status,
"Conol session no longer exists — retry to start a new session",
{ model, sessionId },
followUpUrl
);
}
if (!followUpResponse.ok) {
return makeErrorResult(
followUpResponse.status,
`Conol follow-up submission failed (HTTP ${followUpResponse.status})`,
{ model, sessionId },
followUpUrl
);
}
reusedSession = true;
await followUpResponse.body?.cancel().catch(() => undefined);
} else {
const firstTurnUrl = `${CONOL_SESSION_URL}/${sessionId}/messages`;
const firstTurnResponse = await fetch(firstTurnUrl, {
method: "POST",
headers: conolHeaders(cookie, { "content-type": "application/json" }, sessionId),
body: JSON.stringify({ messages: parts, timezone }),
signal: upstreamSignal,
});
if (firstTurnResponse.status === 401 || firstTurnResponse.status === 403) {
return makeErrorResult(
firstTurnResponse.status,
"Conol session expired or is invalid — sign in again",
{ model },
firstTurnUrl
);
}
if (!firstTurnResponse.ok) {
return makeErrorResult(
firstTurnResponse.status,
`Conol message submission failed (HTTP ${firstTurnResponse.status})`,
{ model, sessionId },
firstTurnUrl
);
}
await firstTurnResponse.body?.cancel().catch(() => undefined);
}
if (sessionBindingKey) {
setConolSessionBinding(sessionBindingKey, {
upstreamSessionId: sessionId,
presetApplied,
appliedModel,
appliedEffort,
hasImageHistory,
});
}
const messagesUrl = `${CONOL_SESSION_URL}/${sessionId}/messages?logDeltas=1`;
const messageResponse = await fetch(messagesUrl, {
method: "GET",
headers: conolHeaders(
cookie,
{ accept: "text/event-stream, application/x-ndjson" },
sessionId
),
signal: upstreamSignal,
});
if (!messageResponse.ok) {
if (
sessionBindingKey &&
(messageResponse.status === 404 || messageResponse.status === 410)
) {
conolSessionBindings.delete(sessionBindingKey);
}
return makeErrorResult(
messageResponse.status,
`Conol message stream failed (HTTP ${messageResponse.status})`,
{ model, sessionId },
messagesUrl
);
}
const parsed = parseConolMessageStream(await collectConolMessageStream(messageResponse));
if (!parsed.text) {
return makeErrorResult(
502,
"Conol returned no assistant response",
{ model, sessionId },
messagesUrl
);
}
const response = input.stream
? streamResponse(parsed.text, model, sessionId)
: completionResponse(parsed.text, model, sessionId, prompt);
return {
response,
url: messagesUrl,
headers: { cookie: "***" },
transformedBody: {
model,
...(appliedEffort ? { effort: appliedEffort } : {}),
effortRequested: effort,
effortExplicit,
sessionId,
reusedSession,
clientSessionBound: sessionBindingKey !== null,
imageCount: imageParts.length,
},
};
});
} catch (error) {
const isTimeout = error instanceof Error && error.name === "TimeoutError";
const status = error instanceof CursorImageError ? error.status : isTimeout ? 504 : 502;
const message =
error instanceof CursorImageError
? error.message
: isTimeout
? "Conol request timed out"
: error instanceof Error && error.name === "AbortError"
? "Conol request was cancelled"
: "Conol request failed";
return makeErrorResult(status, message, { model }, CONOL_SESSION_URL);
}
}
}