cartethyia 2.7.6 → 2.7.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (126) hide show
  1. package/.env.example +4 -2
  2. package/README.md +25 -1
  3. package/dist/dashboard/assets/{BannedPage-B1N603cM.js → BannedPage-B05aF8Dg.js} +1 -1
  4. package/dist/dashboard/assets/BannedPage-B05aF8Dg.js.gz +0 -0
  5. package/dist/dashboard/assets/{CliToolDetailPage-9q4_4NoK.js → CliToolDetailPage-CLUhmjUy.js} +2 -2
  6. package/dist/dashboard/assets/CliToolDetailPage-CLUhmjUy.js.gz +0 -0
  7. package/dist/dashboard/assets/{CliToolsPage-DwshGtK-.js → CliToolsPage-CQmrqqrk.js} +1 -1
  8. package/dist/dashboard/assets/CliToolsPage-CQmrqqrk.js.gz +0 -0
  9. package/dist/dashboard/assets/{CombosPage-D762MIkp.js → CombosPage-CHG_1Wr3.js} +1 -1
  10. package/dist/dashboard/assets/CombosPage-CHG_1Wr3.js.gz +0 -0
  11. package/dist/dashboard/assets/{ConsoleLogPage-KRYjHZsX.js → ConsoleLogPage-DPgbklad.js} +1 -1
  12. package/dist/dashboard/assets/ConsoleLogPage-DPgbklad.js.gz +0 -0
  13. package/dist/dashboard/assets/CustomizationPage-pnfAqr_-.js +1 -0
  14. package/dist/dashboard/assets/CustomizationPage-pnfAqr_-.js.gz +0 -0
  15. package/dist/dashboard/assets/HealthEventsModal-DGJuU2A6.js +1 -0
  16. package/dist/dashboard/assets/HealthEventsModal-DGJuU2A6.js.gz +0 -0
  17. package/dist/dashboard/assets/{InjectSystemPromptPanel-d5v3K_Yc.js → InjectSystemPromptPanel-C-e2AMz6.js} +2 -2
  18. package/dist/dashboard/assets/InjectSystemPromptPanel-C-e2AMz6.js.gz +0 -0
  19. package/dist/dashboard/assets/{LoginPage-Ccv4tjNS.js → LoginPage-J8wPbmfT.js} +1 -1
  20. package/dist/dashboard/assets/LoginPage-J8wPbmfT.js.gz +0 -0
  21. package/dist/dashboard/assets/{OverviewPage-DN9fYNWA.js → OverviewPage-BSQnF2Lp.js} +1 -1
  22. package/dist/dashboard/assets/OverviewPage-BSQnF2Lp.js.gz +0 -0
  23. package/dist/dashboard/assets/ProviderDetailPage-DK_e0FJz.js +7 -0
  24. package/dist/dashboard/assets/ProviderDetailPage-DK_e0FJz.js.gz +0 -0
  25. package/dist/dashboard/assets/ProvidersPage-D_a3r6Gc.js +1 -0
  26. package/dist/dashboard/assets/ProvidersPage-D_a3r6Gc.js.gz +0 -0
  27. package/dist/dashboard/assets/{ProxyPage-DxjvUzSD.js → ProxyPage-BBTRBAQ3.js} +1 -1
  28. package/dist/dashboard/assets/ProxyPage-BBTRBAQ3.js.gz +0 -0
  29. package/dist/dashboard/assets/QuotaPage-B2WqMSG1.js +1 -0
  30. package/dist/dashboard/assets/QuotaPage-B2WqMSG1.js.gz +0 -0
  31. package/dist/dashboard/assets/{SettingsPage-BW4AKj2H.js → SettingsPage-D7xp5Ij_.js} +1 -1
  32. package/dist/dashboard/assets/SettingsPage-D7xp5Ij_.js.gz +0 -0
  33. package/dist/dashboard/assets/{SetupPage-44BZrxFC.js → SetupPage-LND6_Ss7.js} +1 -1
  34. package/dist/dashboard/assets/SetupPage-LND6_Ss7.js.gz +0 -0
  35. package/dist/dashboard/assets/{StudioPage-DRPavCdU.js → StudioPage-BzNnm6Bi.js} +2 -2
  36. package/dist/dashboard/assets/StudioPage-BzNnm6Bi.js.gz +0 -0
  37. package/dist/dashboard/assets/{UsagePage-B7M7LvMM.js → UsagePage-C1TbL24N.js} +2 -2
  38. package/dist/dashboard/assets/UsagePage-C1TbL24N.js.gz +0 -0
  39. package/dist/dashboard/assets/{entry-D6WvojeD.js → entry-vqGb8yVz.js} +3 -3
  40. package/dist/dashboard/assets/entry-vqGb8yVz.js.gz +0 -0
  41. package/dist/dashboard/assets/formatters-DP-5E01-.js +1 -0
  42. package/dist/dashboard/assets/formatters-DP-5E01-.js.gz +0 -0
  43. package/dist/dashboard/assets/{index-Ko4dLRnV.js → index-D36vCZht.js} +2 -2
  44. package/dist/dashboard/assets/index-D36vCZht.js.gz +0 -0
  45. package/dist/dashboard/assets/{select-BHqpd-bg.js → select-C9Ssle9b.js} +1 -1
  46. package/dist/dashboard/assets/select-C9Ssle9b.js.gz +0 -0
  47. package/dist/dashboard/index.html +2 -2
  48. package/dist/dashboard/index.html.gz +0 -0
  49. package/dist/dashboard/precompressed-assets.json +22 -22
  50. package/migrations/full-postgres/0043_add_provider_custom_user_agent.sql +14 -0
  51. package/migrations/lite-sqlite3/0043_add_provider_custom_user_agent.sql +10 -0
  52. package/package.json +3 -1
  53. package/src/config/security.ts +1 -1
  54. package/src/config/spec.ts +13 -6
  55. package/src/console/cli-tools/cartethyia-websearch-plugin.ts +186 -0
  56. package/src/console/cli-tools/injectors/opencode.ts +42 -14
  57. package/src/console/providers/catalog/contracts.ts +4 -0
  58. package/src/console/providers/catalog/routes.ts +1 -0
  59. package/src/console/providers/detail/routes.ts +8 -0
  60. package/src/console/providers/detail/store.ts +4 -0
  61. package/src/console/quota/account-quota-tenant.ts +74 -0
  62. package/src/observability/telemetry-buffer.ts +2 -1
  63. package/src/persistence/boot.ts +13 -4
  64. package/src/persistence/schema.ts +7 -1
  65. package/src/protocol/canonical-model.ts +7 -0
  66. package/src/protocol/request/gemini.ts +20 -1
  67. package/src/protocol/tool-identity.ts +15 -1
  68. package/src/providers/default-registry.ts +8 -2
  69. package/src/providers/discovery/discovery-types.ts +18 -0
  70. package/src/providers/discovery/probe-phases.ts +45 -0
  71. package/src/providers/discovery/probing-service.ts +41 -6
  72. package/src/providers/integrations/antigravity/antigravity-protocol.ts +20 -2
  73. package/src/providers/integrations/antigravity/antigravity.ts +24 -0
  74. package/src/providers/integrations/codex/codex.ts +28 -1
  75. package/src/providers/integrations/qoder/chat-proto.ts +328 -0
  76. package/src/providers/integrations/qoder/claim-qoder-credits.ts +258 -0
  77. package/src/providers/integrations/qoder/qoder-adapter.ts +275 -0
  78. package/src/providers/integrations/qoder/qoder-auth.ts +201 -0
  79. package/src/providers/integrations/qoder/qoder-body.ts +78 -0
  80. package/src/providers/integrations/qoder/qoder-catalog.ts +5 -1
  81. package/src/providers/integrations/qoder/qoder-chat.ts +514 -0
  82. package/src/providers/integrations/qoder/qoder-cosy.ts +162 -0
  83. package/src/providers/integrations/qoder/qoder-grpc.ts +667 -0
  84. package/src/providers/integrations/qoder/qoder-ide.ts +584 -0
  85. package/src/providers/integrations/qoder/qoder-oauth.ts +4 -4
  86. package/src/providers/integrations/qoder/qoder-quota.ts +1 -1
  87. package/src/providers/integrations/qoder/qoder-realm.ts +40 -4
  88. package/src/providers/integrations/qoder/qoder-types.ts +51 -0
  89. package/src/providers/integrations/qoder/qoder-umid.ts +123 -8
  90. package/src/providers/integrations/qoder/qoder.ts +147 -1889
  91. package/src/providers/provider-contracts.ts +7 -0
  92. package/src/providers/provider-usage.ts +9 -6
  93. package/src/security/abuse.ts +5 -5
  94. package/src/transport/dispatch/upstream.ts +4 -2
  95. package/src/transport/routing/route-catalog.ts +8 -0
  96. package/src/transport/routing/route-model.ts +4 -0
  97. package/src/transport/routing/router.ts +8 -117
  98. package/dist/dashboard/assets/BannedPage-B1N603cM.js.gz +0 -0
  99. package/dist/dashboard/assets/CliToolDetailPage-9q4_4NoK.js.gz +0 -0
  100. package/dist/dashboard/assets/CliToolsPage-DwshGtK-.js.gz +0 -0
  101. package/dist/dashboard/assets/CombosPage-D762MIkp.js.gz +0 -0
  102. package/dist/dashboard/assets/ConsoleLogPage-KRYjHZsX.js.gz +0 -0
  103. package/dist/dashboard/assets/CustomizationPage-DXsxk5uM.js +0 -1
  104. package/dist/dashboard/assets/CustomizationPage-DXsxk5uM.js.gz +0 -0
  105. package/dist/dashboard/assets/HealthEventsModal-BEjAjD4F.js +0 -1
  106. package/dist/dashboard/assets/HealthEventsModal-BEjAjD4F.js.gz +0 -0
  107. package/dist/dashboard/assets/InjectSystemPromptPanel-d5v3K_Yc.js.gz +0 -0
  108. package/dist/dashboard/assets/LoginPage-Ccv4tjNS.js.gz +0 -0
  109. package/dist/dashboard/assets/OverviewPage-DN9fYNWA.js.gz +0 -0
  110. package/dist/dashboard/assets/ProviderDetailPage-BY0ieUgz.js +0 -5
  111. package/dist/dashboard/assets/ProviderDetailPage-BY0ieUgz.js.gz +0 -0
  112. package/dist/dashboard/assets/ProvidersPage-DNsbfea2.js +0 -1
  113. package/dist/dashboard/assets/ProvidersPage-DNsbfea2.js.gz +0 -0
  114. package/dist/dashboard/assets/ProxyPage-DxjvUzSD.js.gz +0 -0
  115. package/dist/dashboard/assets/QuotaPage-OaHScjpX.js +0 -1
  116. package/dist/dashboard/assets/QuotaPage-OaHScjpX.js.gz +0 -0
  117. package/dist/dashboard/assets/SettingsPage-BW4AKj2H.js.gz +0 -0
  118. package/dist/dashboard/assets/SetupPage-44BZrxFC.js.gz +0 -0
  119. package/dist/dashboard/assets/StudioPage-DRPavCdU.js.gz +0 -0
  120. package/dist/dashboard/assets/UsagePage-B7M7LvMM.js.gz +0 -0
  121. package/dist/dashboard/assets/entry-D6WvojeD.js.gz +0 -0
  122. package/dist/dashboard/assets/formatters-u7JPB5BX.js +0 -1
  123. package/dist/dashboard/assets/formatters-u7JPB5BX.js.gz +0 -0
  124. package/dist/dashboard/assets/index-Ko4dLRnV.js.gz +0 -0
  125. package/dist/dashboard/assets/select-BHqpd-bg.js.gz +0 -0
  126. package/src/providers/integrations/qoder/qoder-text-tools.ts +0 -277
@@ -0,0 +1,584 @@
1
+ /**
2
+ * Qoder IDE surface (`/model/v1/chat/completions`) — request builder, SSE/
3
+ * envelope parsing, canonical-event mapping, and the empty-stream recovery
4
+ * sniff.
5
+ *
6
+ * Surface contract: the IDE surface is the only Qoder surface that echoes
7
+ * strategy metadata (`raw_usage` → provider_model/target) and it streams
8
+ * immediately, but it caps at ~256 KiB and is used in production **only for
9
+ * probing/testing** — gRPC is the default billed carrier and COSY the
10
+ * free/lite carrier (see `qoder-adapter.ts`).
11
+ */
12
+ import { GatewayError } from "../../../protocol/gateway-error";
13
+ import type { CanonicalEvent, CanonicalRequest, CanonicalStopReason } from "../../../protocol/canonical-model";
14
+ import { usageFromProvider, readReasoningText } from "../../provider-usage";
15
+ import { classifyUpstreamError, extractUpstreamMessage, isUpstreamQueued, queueRetryAfterMs, upstreamErrorIdentifier } from "../../../transport/failure-policy";
16
+ import { getQoderVersion } from "../../operations/client-versions";
17
+ import { buildQoderRequest } from "./qoder-chat";
18
+ import { decodeQoderSseEvents } from "./qoder-sse";
19
+ import { QODER_PROVIDER_ID, type QoderRealm } from "./qoder-realm";
20
+ import type { QoderAuth, QoderModelConfig } from "./qoder-types";
21
+
22
+ /**
23
+ * The human-readable text out of a Qoder error envelope.
24
+ *
25
+ * The envelope is Qoder's own wrapper around the stream, and it does not use
26
+ * one field for the message: the text has been seen as a nested `error`
27
+ * object, a bare string `body`, and a top-level `message`/`msg`. The canonical
28
+ * extractor covers the structured shapes; a string body and the top-level
29
+ * fields cover the rest. Whatever survives is bounded the same way an upstream
30
+ * message is, so a verbose envelope cannot blow up the public error line.
31
+ */
32
+ function qoderEnvelopeMessage(rec: Record<string, unknown>): string {
33
+ const structured = extractUpstreamMessage(rec["body"]);
34
+ if (structured.length > 0) return structured;
35
+ if (typeof rec["body"] === "string" && rec["body"].trim().length > 0 && rec["body"] !== "[DONE]")
36
+ return rec["body"].replace(/[\r\n]+/g, " ").trim().slice(0, 500);
37
+ return extractUpstreamMessage(rec);
38
+ }
39
+
40
+ export function qoderEnvelopeToFrames(data: string): Array<{ event: string | null; data: string }> {
41
+ if (data === "[DONE]") return [{ event: null, data: "[DONE]" }];
42
+ let envelope: unknown;
43
+ // The IDE surface sometimes embeds literal newlines inside JSON string
44
+ // values (e.g. the raw_usage event carries keys like
45
+ // `"data_policy_preferre\n\nnd_count"`). Those bytes are not valid JSON, so
46
+ // the first parse fails; when it does, salvage the event by escaping the
47
+ // raw newlines inside string values and parse again. The repaired text is
48
+ // also what the direct-chunk branch hands to the chunk parser below, so its
49
+ // own `JSON.parse` succeeds too.
50
+ let repaired = data;
51
+ try {
52
+ envelope = JSON.parse(data);
53
+ } catch {
54
+ const attempted = escapeJsonRawNewlines(data);
55
+ if (attempted !== data) {
56
+ try {
57
+ envelope = JSON.parse(attempted);
58
+ repaired = attempted;
59
+ } catch {
60
+ envelope = undefined;
61
+ }
62
+ }
63
+ }
64
+ if (typeof envelope !== "object" || envelope === null || Array.isArray(envelope)) return [];
65
+ const rec = envelope as Record<string, unknown>;
66
+ if (rec["statusCodeValue"] !== undefined && rec["statusCodeValue"] !== 200) {
67
+ const status = typeof rec["statusCodeValue"] === "number" ? (rec["statusCodeValue"] as number) : 502;
68
+ // Qoder reports failures inside the stream envelope instead of as HTTP
69
+ // responses, so classify its structured provider code and status through
70
+ // the same gateway error policy as the other dispatch paths.
71
+ const detail = qoderEnvelopeMessage(rec);
72
+ const identifier = upstreamErrorIdentifier(rec["body"]) ?? upstreamErrorIdentifier(rec);
73
+ // Qoder signals a queued request the same way inside the stream as it does
74
+ // on HTTP: `{"code":"10605", "isQueued":true, "retryAfterSeconds":N}` on a
75
+ // non-200 envelope. The status table would read the 403 as a refusal, and
76
+ // the caller would end its turn on a request that was only waiting.
77
+ const queuedWaitMs = isUpstreamQueued(rec["body"]) ?? false;
78
+ const waitMs = queuedWaitMs
79
+ ? (queueRetryAfterMs(rec["body"]) ?? undefined)
80
+ : undefined;
81
+ const classification = queuedWaitMs
82
+ ? { code: "upstream_queued" as const, status: 503, origin: "upstream" as const }
83
+ : classifyUpstreamError(status, identifier);
84
+ throw new GatewayError(
85
+ classification.code,
86
+ classification.status,
87
+ detail.length > 0 ? `${detail} (request {requestId}). The upstream came back empty-handed — retry 🌙` : `Qoder stream envelope returned HTTP ${status} (request {requestId}). The upstream came back empty-handed — retry 🌙`,
88
+ {
89
+ providerId: QODER_PROVIDER_ID,
90
+ providerStatus: status,
91
+ ...(identifier ? { providerCode: identifier } : {}),
92
+ ...(classification.credentialEvidence ? { credentialEvidence: true } : {}),
93
+ ...(classification.rateLimitScope
94
+ ? { rateLimitScope: classification.rateLimitScope }
95
+ : {}),
96
+ ...(waitMs === undefined ? {} : { retryAfterMs: waitMs, cooldownMs: waitMs }),
97
+ ...(queuedWaitMs ? { reason: "upstream-queued" } : {}),
98
+ },
99
+ classification.origin,
100
+ );
101
+ }
102
+ const body = rec["body"];
103
+ if (body === "[DONE]") return [{ event: null, data: "[DONE]" }];
104
+ if (typeof body === "string") return [{ event: null, data: body.replace(/\r?\n/g, "") }];
105
+ // Direct OpenAI chunk (IDE surface): no envelope body, but a `choices` array
106
+ // is present. Pass the (repaired) raw data through so the chunk parser sees
107
+ // it.
108
+ if (Array.isArray(rec["choices"])) return [{ event: null, data: repaired }];
109
+ return [];
110
+ }
111
+
112
+ /**
113
+ * Escapes raw newline bytes that appear inside JSON string values.
114
+ *
115
+ * The IDE surface emits `data:` events whose JSON payload contains literal
116
+ * newlines inside strings (the terminal `raw_usage` event carries keys like
117
+ * `"data_policy_preferre\n\nnd_count"`). `JSON.parse` rejects those bytes, so
118
+ * before parsing we rewrite each raw `\n` found inside a string to the two
119
+ * characters `\n`. Bytes outside strings (whitespace, structural separators)
120
+ * are left untouched.
121
+ */
122
+ function escapeJsonRawNewlines(data: string): string {
123
+ let out = "";
124
+ let inString = false;
125
+ let escaped = false;
126
+ for (let i = 0; i < data.length; i += 1) {
127
+ const ch = data[i]!;
128
+ if (inString) {
129
+ if (escaped) {
130
+ out += ch;
131
+ escaped = false;
132
+ continue;
133
+ }
134
+ if (ch === "\\") {
135
+ out += ch;
136
+ escaped = true;
137
+ continue;
138
+ }
139
+ if (ch === '"') {
140
+ inString = false;
141
+ out += ch;
142
+ continue;
143
+ }
144
+ if (ch === "\n" || ch === "\r") {
145
+ out += "\\n";
146
+ continue;
147
+ }
148
+ out += ch;
149
+ continue;
150
+ }
151
+ if (ch === '"') {
152
+ inString = true;
153
+ out += ch;
154
+ continue;
155
+ }
156
+ out += ch;
157
+ }
158
+ return out;
159
+ }
160
+
161
+ function mapQoderFinishReason(raw: unknown): CanonicalStopReason | undefined {
162
+ if (raw === "stop") return "stop";
163
+ if (raw === "length") return "length";
164
+ if (raw === "tool_calls") return "tool_use";
165
+ if (raw === "content_filter") return "content_filter";
166
+ return undefined;
167
+ }
168
+
169
+ /**
170
+ * Keys the IDE surface's `raw_usage` (and top-level frame) carries that
171
+ * describe the model that *actually* served the turn. The production
172
+ * transports (gRPC / COSY) only echo an opaque roster key, so this metadata is
173
+ * the one place the upstream names its real supply model/target.
174
+ */
175
+ const QODER_PROVIDER_META_KEYS = [
176
+ "provider_model",
177
+ "from_model",
178
+ "supply_model",
179
+ "supply_account",
180
+ "supply_region",
181
+ "model_level",
182
+ "router_strategy_name",
183
+ "router_strategy_type",
184
+ "scheduler_times",
185
+ "target",
186
+ "target_id",
187
+ ] as const;
188
+
189
+ /** Collects provider metadata from a raw_usage object and its `data` child. */
190
+ function collectQoderProviderMeta(
191
+ target: Record<string, unknown>,
192
+ rawUsageObj: Record<string, unknown>,
193
+ ): void {
194
+ const data = rawUsageObj["data"];
195
+ const candidates: unknown[] = [rawUsageObj, data];
196
+ for (const src of candidates) {
197
+ if (!src || typeof src !== "object" || Array.isArray(src)) continue;
198
+ const s = src as Record<string, unknown>;
199
+ for (const key of QODER_PROVIDER_META_KEYS) {
200
+ const value = s[key];
201
+ if (value !== undefined && value !== null && target[key] === undefined) {
202
+ target[key] = value;
203
+ }
204
+ }
205
+ }
206
+ }
207
+
208
+ export async function* qoderBodyToCanonicalEvents(
209
+ body: ReadableStream<Uint8Array>,
210
+ signal: AbortSignal,
211
+ model: string,
212
+ ): AsyncIterable<CanonicalEvent> {
213
+ let seq = 1;
214
+ let started = false;
215
+ let finishReason: unknown = undefined;
216
+ let rawUsage: Record<string, unknown> | undefined = undefined;
217
+ const providerMetadata: Record<string, unknown> = {};
218
+ let hasTerminal = false;
219
+ // Once a finish reason or `[DONE]` is seen the turn is over: delta content,
220
+ // reasoning, and tool-call fragments in later frames must be dropped —
221
+ // strict OpenAI clients reject content after the finish reason. Usage,
222
+ // errors, and the marker itself still flow.
223
+ let finishSeen = false;
224
+ /** First-sighting id per wire tool-call index, for continuation chunks. */
225
+ const indexIds = new Map<number, string>();
226
+ /** Last seen call id, for index-less continuation fragments. */
227
+ let lastSeenToolId: string | undefined = undefined;
228
+
229
+ for await (const sse of decodeQoderSseEvents(body, signal)) {
230
+ const sseData = sse.data;
231
+ let frames: Array<{ event: string | null; data: string }>;
232
+ try {
233
+ frames = qoderEnvelopeToFrames(sseData);
234
+ } catch (e) {
235
+ throw e;
236
+ }
237
+ for (const frame of frames) {
238
+ const data = frame.data.trim();
239
+ if (!data) continue;
240
+ if (data === "[DONE]") {
241
+ hasTerminal = true;
242
+ finishSeen = true;
243
+ if (!started) {
244
+ yield { type: "response_start", sequence_number: seq++, model } as CanonicalEvent;
245
+ started = true;
246
+ }
247
+ continue;
248
+ }
249
+ let json: Record<string, unknown>;
250
+ try {
251
+ json = JSON.parse(data) as Record<string, unknown>;
252
+ } catch {
253
+ continue;
254
+ }
255
+ // The upstream names the real model in each chunk. Use the first one we
256
+ // see for the opening event so the client sees the same model the
257
+ // provider actually served, not the (virtual) id that was requested.
258
+ if (!started && typeof json["model"] === "string") {
259
+ const upstreamModel = (json["model"] as string).trim();
260
+ yield {
261
+ type: "response_start",
262
+ sequence_number: seq++,
263
+ model: upstreamModel.length > 0 ? upstreamModel : model,
264
+ } as CanonicalEvent;
265
+ started = true;
266
+ }
267
+ // IDE surface error event: `{"code":"invalid_model_error","message":"..."}`
268
+ // carries no choices. Surface it as an upstream failure instead of an
269
+ // empty successful stream.
270
+ if (json["error"] !== undefined && json["choices"] === undefined) {
271
+ const error = json["error"] as Record<string, unknown> | string;
272
+ const message =
273
+ typeof error === "string"
274
+ ? error
275
+ : typeof error?.["message"] === "string"
276
+ ? (error["message"] as string)
277
+ : typeof json["message"] === "string"
278
+ ? (json["message"] as string)
279
+ : "Qoder model server error";
280
+ const code = typeof json["code"] === "string" ? (json["code"] as string) : undefined;
281
+ throw new GatewayError(
282
+ code === "invalid_model_error" ? "model_not_found" : "platform_unavailable",
283
+ code === "invalid_model_error" ? 404 : 502,
284
+ `${message} (request {requestId}). The upstream came back empty-handed — retry 🌙`,
285
+ { providerId: QODER_PROVIDER_ID, ...(code === undefined ? {} : { providerCode: code }) },
286
+ "upstream",
287
+ );
288
+ }
289
+ if (json["usage"] && typeof json["usage"] === "object") rawUsage = json["usage"] as Record<string, unknown>;
290
+ // IDE surface terminal event: `choices:[]` + `raw_usage`. It carries the
291
+ // final usage and confirms the turn ended, so record it and synthesize a
292
+ // stop reason — the terminal event has no `choices[].finish_reason`.
293
+ if (json["raw_usage"] && typeof json["raw_usage"] === "object") {
294
+ const rawUsageObj = json["raw_usage"] as Record<string, unknown>;
295
+ collectQoderProviderMeta(providerMetadata, rawUsageObj);
296
+ rawUsage = (rawUsageObj["data"] as Record<string, unknown> | undefined) ?? rawUsageObj;
297
+ if (json["choices"] === undefined || (json["choices"] as unknown[]).length === 0) {
298
+ finishReason = finishReason ?? "stop";
299
+ }
300
+ }
301
+ const choices = (json["choices"] as Array<Record<string, unknown>>) ?? [];
302
+ // After a finish was seen (per-choice or top-level finish_reason, or
303
+ // `[DONE]`) the turn is over: delta content, reasoning, and tool-call
304
+ // fragments in later frames are dropped — strict OpenAI clients reject
305
+ // content after the finish reason. Usage and errors still flow.
306
+ if (!finishSeen) {
307
+ for (const choice of choices) {
308
+ const delta = choice["delta"] as Record<string, unknown> | undefined;
309
+ if (delta) {
310
+ if (typeof delta["content"] === "string" && (delta["content"] as string).length > 0) {
311
+ const piece = delta["content"] as string;
312
+ yield {
313
+ type: "content_delta",
314
+ sequence_number: seq++,
315
+ content: { kind: "text", text: piece },
316
+ } as CanonicalEvent;
317
+ continue;
318
+ }
319
+ const reasoningText = readReasoningText(delta);
320
+ if (typeof reasoningText === "string" && reasoningText.length > 0) {
321
+ yield {
322
+ type: "content_delta",
323
+ sequence_number: seq++,
324
+ content: { kind: "reasoning", payload: null, summary: reasoningText },
325
+ } as CanonicalEvent;
326
+ continue;
327
+ }
328
+ const toolCalls = delta["tool_calls"] as Array<Record<string, unknown>> | undefined;
329
+ if (toolCalls) {
330
+ for (const tc of toolCalls) {
331
+ const fn = tc["function"] as Record<string, unknown> | undefined;
332
+ const index = typeof tc["index"] === "number" ? (tc["index"] as number) : undefined;
333
+ // The donor (StreamAdapter `vTe` / `$Dl`) forwards each tool-call
334
+ // delta exactly as received — `id`, `index`, `name`, and the raw
335
+ // `arguments` string fragment — with no buffering, splitting, or
336
+ // JSON repair. It does pin the first seen id per wire `index`
337
+ // and reuses it for continuation fragments, so the client's
338
+ // trackers keep one identity per call. Do the same here: each
339
+ // delta becomes its own `tool_call_delta` under the pinned id.
340
+ const declaredId =
341
+ typeof tc["id"] === "string" && (tc["id"] as string).length > 0
342
+ ? (tc["id"] as string)
343
+ : undefined;
344
+ const fragName = typeof fn?.["name"] === "string" ? (fn["name"] as string).trim() : undefined;
345
+ const fragArgs =
346
+ typeof fn?.["arguments"] === "string" ? (fn["arguments"] as string) : "";
347
+ if (declaredId !== undefined && index !== undefined && !indexIds.has(index)) {
348
+ indexIds.set(index, declaredId);
349
+ }
350
+ if (declaredId !== undefined) lastSeenToolId = declaredId;
351
+ const callId =
352
+ (index !== undefined ? indexIds.get(index) : undefined) ??
353
+ declaredId ??
354
+ lastSeenToolId ??
355
+ `call_anon_${Date.now().toString(36)}_${seq}`;
356
+ yield {
357
+ type: "tool_call_delta",
358
+ sequence_number: seq++,
359
+ call_id: callId,
360
+ index: index ?? 0,
361
+ name: fragName,
362
+ arguments_delta: fragArgs,
363
+ } as CanonicalEvent;
364
+ }
365
+ }
366
+ }
367
+ const fr = choice["finish_reason"];
368
+ // The IDE surface sends an empty string (`""`) for the per-delta finish
369
+ // reason, which is not a real stop signal — only a value that maps to a
370
+ // canonical stop reason (or the terminal `[DONE]` frame) ends the turn.
371
+ if (fr !== null && fr !== undefined && fr !== "") {
372
+ finishReason = fr;
373
+ finishSeen = true;
374
+ }
375
+ // Some providers send usage in choices[0].usage or top-level
376
+ if (choice["usage"] && typeof choice["usage"] === "object") rawUsage = choice["usage"] as Record<string, unknown>;
377
+ }
378
+ }
379
+ // Some Qoder chunks carry finish_reason at top level (empty string means
380
+ // no stop signal, same as the per-delta field).
381
+ if (json["finish_reason"] !== undefined && json["finish_reason"] !== "") {
382
+ finishReason = json["finish_reason"];
383
+ finishSeen = true;
384
+ }
385
+ }
386
+ }
387
+ // A `[DONE]` frame from the IDE surface may be the only terminal signal (the
388
+ // //-delta `finish_reason` is empty and `raw_usage` is not always sent), so
389
+ // a clean end-of-stream with no finish reason is a normal stop, not a
390
+ // truncation. If no terminal arrived at all, still synthesize one (prevents
391
+ // truncation error).
392
+ if (!started) {
393
+ yield { type: "response_start", sequence_number: seq++, model } as CanonicalEvent;
394
+ }
395
+ if (hasTerminal && finishReason === undefined) finishReason = "stop";
396
+ const stopReason = mapQoderFinishReason(finishReason);
397
+ const _providerStop = typeof finishReason === "string" ? finishReason : undefined;
398
+ void hasTerminal;
399
+ // No buffered tool-call flush: each `delta.tool_calls` fragment is emitted
400
+ // as its own `tool_call_delta` as it arrives (donor mirror), so the client
401
+ // stitches by `index`/`id` itself. Usage and terminal still flow here.
402
+ yield {
403
+ type: "terminal",
404
+ sequence_number: seq++,
405
+ state: "complete",
406
+ ...(stopReason === undefined ? {} : { stop_reason: stopReason }),
407
+ ...(_providerStop === undefined ? {} : { provider_stop_reason: _providerStop }),
408
+ usage: usageFromProvider(rawUsage),
409
+ ...(Object.keys(providerMetadata).length > 0 ? { provider_metadata: providerMetadata } : {}),
410
+ } as CanonicalEvent;
411
+ }
412
+
413
+ /**
414
+ * Builds the modern model-server (`/model/v1/chat/completions`) request body.
415
+ *
416
+ * It reuses the message translation from `buildQoderRequest` (tool turns,
417
+ * reasoning echo, image parts) and wraps the result in the OpenAI-shaped body
418
+ * the IDE surface expects: top-level `model`, `messages`, `stream_options`,
419
+ * and a `metadata.context`/`metadata.business` block. `model` is the roster
420
+ * key after virtual-name resolution, so the upstream never sees a virtual id
421
+ * (which it answers by falling back to `auto`).
422
+ */
423
+ export function buildQoderIdeRequest(
424
+ modelId: string,
425
+ request: CanonicalRequest,
426
+ modelConfig: QoderModelConfig,
427
+ auth: QoderAuth,
428
+ realm: QoderRealm = "intl",
429
+ version = getQoderVersion(),
430
+ ): Record<string, unknown> {
431
+ const legacy = buildQoderRequest(modelId, request, modelConfig, auth, realm);
432
+ const messages = legacy.messages as Array<Record<string, unknown>>;
433
+ // The IDE surface (`model/v1/chat/completions`) must ship the same tool
434
+ // declaration as the legacy builder. The unpacked desktop client (worker
435
+ // `PFn`) injects `tools` directly when present — without it the upstream
436
+ // sees no toolset and answers with a DSML text block (or claims no tool
437
+ // exists) instead of a structured `tool_calls`. The remaining knobs
438
+ // (`parallel_tool_calls`, `tool_choice`, …) ride the `parameters`
439
+ // passthrough (worker `vyl`), matching the reference exactly.
440
+ const tools = legacy.tools as Array<Record<string, unknown>> | undefined;
441
+ return {
442
+ model: modelId,
443
+ messages,
444
+ stream: true,
445
+ stream_options: { include_usage: true },
446
+ ...(tools !== undefined && tools.length > 0 ? { tools } : {}),
447
+ ...(legacy.parameters === undefined ? {} : { parameters: legacy.parameters }),
448
+ // The same reasoning-effort field the legacy surface uses; without it the
449
+ // IDE surface streams no thinking for models that need the push (Sonus,
450
+ // Cantus). Undefined when the caller asked for none and the roster has no
451
+ // default, matching the legacy builder.
452
+ ...(legacy.reasoningEffort === undefined ? {} : { reasoningEffort: legacy.reasoningEffort }),
453
+ // The full wire config (model key, window, output ceiling, tier) the legacy
454
+ // surface sends. The IDE surface reads the same block, so shipping it keeps
455
+ // the request the same shape for every Qoder model — including lite's 1M
456
+ // window and 64k output cap.
457
+ ...(legacy.model_config === undefined ? {} : { model_config: legacy.model_config }),
458
+ metadata: {
459
+ context: {
460
+ request_id: legacy.request_id,
461
+ request_set_id: legacy.request_set_id,
462
+ session_id: legacy.session_id,
463
+ source_session_id: "",
464
+ task_id: "common",
465
+ client_type: "5",
466
+ },
467
+ business: {
468
+ business_product: "cli",
469
+ business_type: "agent",
470
+ business_version: version,
471
+ scene: "assistant",
472
+ },
473
+ },
474
+ max_tokens: (legacy.max_tokens as number) ?? 65_536,
475
+ };
476
+ }
477
+
478
+ /**
479
+ * Qoder upstream streams are flaky: the IDE surface sometimes answers a full
480
+ * normal stop with reasoning only and no output text/tool calls (observed ~1
481
+ * in 4 runs with Sonus). That is not a valid completion — the lab would show a
482
+ * thought with "No text came back". Replay such a stream before committing to
483
+ * it. `sniffQoderStream` below inspects just enough of the body to make that
484
+ * call without buffering the whole (possibly long) stream.
485
+ */
486
+
487
+ /**
488
+ * Reads the upstream body just far enough to decide whether it carries real
489
+ * output, then returns a replay stream that re-emits the already-read bytes
490
+ * followed by the rest of the original body. If the body ends before any
491
+ * meaningful output is seen, `meaningful` is false and the caller replays the
492
+ * whole request (empty / thought-only recovery) without ever buffering a large
493
+ * response: the original `drainQoderResponse` read the entire stream before
494
+ * forwarding, which made every stream appear to complete in one burst at the
495
+ * end.
496
+ */
497
+ async function sniffQoderStream(
498
+ response: Response,
499
+ signal: AbortSignal,
500
+ ): Promise<{ meaningful: boolean; response: Response }> {
501
+ const body = response.body;
502
+ if (body === null) return { meaningful: true, response };
503
+ const reader = body.getReader();
504
+ const decoder = new TextDecoder();
505
+ const pending: Uint8Array[] = [];
506
+ let inspected = "";
507
+ let meaningful = false;
508
+ for (let i = 0; i < 32 && !meaningful; i += 1) {
509
+ const { done, value } = await reader.read();
510
+ if (signal.aborted) break;
511
+ if (done) break;
512
+ pending.push(value);
513
+ inspected += decoder.decode(value, { stream: true });
514
+ // Peek at completed SSE frames only: a content delta or a tool-call
515
+ // fragment means real output is coming. Reasoning-only streams are NOT
516
+ // meaningful (they are exactly what empty-stream recovery replays), and a
517
+ // finish reason alone is not output either.
518
+ if (/data:/.test(inspected)) {
519
+ for (const frame of inspected.split(/\r?\n\r?\n/)) {
520
+ if (!frame.includes("data:")) continue;
521
+ if (frame.includes('"content":"') || frame.includes('"tool_calls"')) {
522
+ meaningful = true;
523
+ break;
524
+ }
525
+ }
526
+ }
527
+ if (inspected.length > 65536) {
528
+ meaningful = true;
529
+ break;
530
+ }
531
+ }
532
+ // Replay: previously-read bytes first, then continue the original body.
533
+ let offset = 0;
534
+ const replay = new ReadableStream<Uint8Array>({
535
+ start(controller) {
536
+ for (; offset < pending.length; offset += 1) controller.enqueue(pending[offset]);
537
+ },
538
+ async pull(controller) {
539
+ if (offset < pending.length) {
540
+ controller.enqueue(pending[offset]);
541
+ offset += 1;
542
+ return;
543
+ }
544
+ const { done, value } = await reader.read();
545
+ if (signal.aborted) {
546
+ controller.close();
547
+ return;
548
+ }
549
+ if (done) {
550
+ controller.close();
551
+ return;
552
+ }
553
+ controller.enqueue(value);
554
+ },
555
+ cancel(reason) {
556
+ return reader.cancel(reason);
557
+ },
558
+ });
559
+ return { meaningful, response: new Response(replay, response) };
560
+ }
561
+
562
+ /**
563
+ * Runs the IDE request up to `1 + maxReplays` times, returning the first
564
+ * response whose stream carried real output (text or tool calls). A 200 with
565
+ * no output (empty or thought-only) is replayed rather than surfaced. The
566
+ * stream is forwarded progressively (not buffered to completion).
567
+ */
568
+ async function fetchQoderIdeWithEmptyRecovery(
569
+ requestOnce: () => Promise<Response>,
570
+ signal: AbortSignal,
571
+ maxReplays = 2,
572
+ ): Promise<Response> {
573
+ let last: Response | undefined;
574
+ for (let attempt = 0; attempt <= maxReplays; attempt += 1) {
575
+ const res = await requestOnce();
576
+ if (!res.ok || !res.body) return res;
577
+ const sniff = await sniffQoderStream(res, signal);
578
+ last = sniff.response;
579
+ if (sniff.meaningful) return last;
580
+ }
581
+ return last!;
582
+ }
583
+
584
+ export { fetchQoderIdeWithEmptyRecovery };
@@ -18,7 +18,7 @@
18
18
  import { createHash, randomBytes, randomUUID } from "node:crypto";
19
19
  import { execSync } from "node:child_process";
20
20
  import { readFileSync } from "node:fs";
21
- import { QODER_REALMS, type QoderRealm } from "./qoder-realm";
21
+ import { QODER_REALMS, QODER_UA_CLI, type QoderRealm } from "./qoder-realm";
22
22
  import {
23
23
  base64UrlEncode,
24
24
  nonEmptyTrimmedString,
@@ -384,7 +384,7 @@ export class QoderOAuthClient extends OAuthDeviceFlow {
384
384
  try {
385
385
  response = await this.fetchFn(url, {
386
386
  method: "GET",
387
- headers: { accept: "application/json", "user-agent": "Go-http-client/2.0" },
387
+ headers: { accept: "application/json", "user-agent": QODER_UA_CLI },
388
388
  signal: AbortSignal.timeout(POLL_TIMEOUT_MS),
389
389
  });
390
390
  } catch (error) {
@@ -467,7 +467,7 @@ export class QoderOAuthClient extends OAuthDeviceFlow {
467
467
  headers: {
468
468
  authorization: `Bearer ${accessToken}`,
469
469
  accept: "application/json",
470
- "user-agent": "Go-http-client/2.0",
470
+ "user-agent": QODER_UA_CLI,
471
471
  },
472
472
  signal: AbortSignal.timeout(POLL_TIMEOUT_MS),
473
473
  });
@@ -502,7 +502,7 @@ export class QoderOAuthClient extends OAuthDeviceFlow {
502
502
  headers: {
503
503
  "content-type": "application/json",
504
504
  accept: "application/json",
505
- "user-agent": "Go-http-client/2.0",
505
+ "user-agent": QODER_UA_CLI,
506
506
  },
507
507
  body: JSON.stringify({
508
508
  refresh_token: refreshToken,
@@ -38,7 +38,7 @@ import {
38
38
  import type { QuotaCollectionContext } from "../../quota/quota-support";
39
39
  import { log } from "../../../observability/logger";
40
40
  import { QODER_REALMS, type QoderRealm } from "./qoder-realm";
41
- import { isQoderPat, resolveQoderAuth } from "./qoder";
41
+ import { isQoderPat, resolveQoderAuth } from "./qoder-auth";
42
42
 
43
43
  /** Paths on the realm's openapi host. */
44
44
  const QUOTA_PATH = "/api/v2/quota/usage";