qwenproxy-cli 1.0.0 → 1.0.2

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 (109) hide show
  1. package/LICENSE +14 -14
  2. package/README.md +906 -906
  3. package/bin/qwenproxy.js +5 -1
  4. package/package.json +77 -78
  5. package/src/api/error-classifier.ts +159 -159
  6. package/src/api/error-helpers.ts +118 -118
  7. package/src/api/models.ts +261 -261
  8. package/src/api/server.ts +860 -859
  9. package/src/cache/memory-cache.ts +385 -385
  10. package/src/clean-cache.ts +204 -204
  11. package/src/core/account-concurrency.ts +671 -671
  12. package/src/core/account-manager.ts +301 -297
  13. package/src/core/account-priority.ts +163 -163
  14. package/src/core/accounts.ts +186 -186
  15. package/src/core/config.ts +383 -383
  16. package/src/core/crypto-utils.ts +79 -79
  17. package/src/core/database.ts +276 -276
  18. package/src/core/errors.ts +118 -118
  19. package/src/core/logger.ts +269 -269
  20. package/src/core/memory-usage.ts +84 -84
  21. package/src/core/metrics.ts +291 -291
  22. package/src/core/model-alias.ts +77 -77
  23. package/src/core/model-registry.ts +544 -544
  24. package/src/core/mutex.ts +119 -119
  25. package/src/core/paths.ts +199 -199
  26. package/src/core/prompt-limits.ts +214 -214
  27. package/src/core/reasoning-effort.ts +102 -102
  28. package/src/core/stream-registry.ts +96 -96
  29. package/src/core/waf-isolation.ts +117 -117
  30. package/src/core/watchdog.ts +195 -195
  31. package/src/delete-chats.ts +23 -23
  32. package/src/index.ts +65 -64
  33. package/src/login.ts +147 -147
  34. package/src/reset-cooldowns.ts +11 -11
  35. package/src/routes/anthropic/index.ts +355 -355
  36. package/src/routes/anthropic/translate.ts +522 -522
  37. package/src/routes/anthropic/types.ts +154 -154
  38. package/src/routes/anthropic/validation.ts +144 -144
  39. package/src/routes/chat/account.ts +1817 -1817
  40. package/src/routes/chat/context.ts +241 -241
  41. package/src/routes/chat/errors.ts +85 -85
  42. package/src/routes/chat/helpers.ts +268 -268
  43. package/src/routes/chat/index.ts +618 -618
  44. package/src/routes/chat/media.ts +285 -285
  45. package/src/routes/chat/retry-policy.ts +754 -754
  46. package/src/routes/chat/stop.ts +98 -98
  47. package/src/routes/chat/streaming.ts +2710 -2710
  48. package/src/routes/chat/validation.ts +526 -526
  49. package/src/routes/chat.ts +2 -2
  50. package/src/routes/completions.ts +290 -290
  51. package/src/routes/images.ts +139 -139
  52. package/src/routes/responses/adapter.ts +503 -503
  53. package/src/routes/responses/index.ts +405 -405
  54. package/src/routes/responses/state.ts +230 -230
  55. package/src/routes/responses/streaming.ts +528 -528
  56. package/src/routes/responses/types.ts +285 -285
  57. package/src/routes/responses/validation.ts +202 -202
  58. package/src/routes/upload.ts +731 -731
  59. package/src/routes/videos.ts +214 -214
  60. package/src/services/auth-playwright.ts +173 -173
  61. package/src/services/captcha-coordinator.ts +161 -161
  62. package/src/services/captcha-solver.ts +553 -553
  63. package/src/services/chat-cleanup.ts +80 -80
  64. package/src/services/context-meter.ts +317 -317
  65. package/src/services/fingerprint.ts +242 -242
  66. package/src/services/human-behavior.ts +173 -173
  67. package/src/services/media-generation.ts +1748 -1748
  68. package/src/services/playwright.ts +2878 -2800
  69. package/src/services/qwen-chat-pool.ts +345 -345
  70. package/src/services/qwen-errors.ts +133 -133
  71. package/src/services/qwen-headers.ts +79 -79
  72. package/src/services/qwen-thread-state.ts +393 -393
  73. package/src/services/qwen-url.ts +19 -19
  74. package/src/services/qwen.ts +3126 -3126
  75. package/src/services/session-keeper.ts +88 -88
  76. package/src/services/token-estimation-metrics.ts +118 -118
  77. package/src/sync/claude-code.ts +75 -75
  78. package/src/sync/codex.ts +123 -123
  79. package/src/sync/index.ts +362 -362
  80. package/src/sync/omp.ts +105 -105
  81. package/src/sync/opencode.ts +214 -214
  82. package/src/sync/types.ts +53 -53
  83. package/src/sync/utils.ts +27 -27
  84. package/src/sync-clients.ts +189 -189
  85. package/src/tools/instructions.ts +137 -137
  86. package/src/tools/manifest.ts +81 -81
  87. package/src/tools/parser.ts +2989 -2989
  88. package/src/tools/toolcall-tags.ts +142 -142
  89. package/src/tui/app.ts +259 -264
  90. package/src/tui/index.ts +61 -61
  91. package/src/tui/markdown.ts +258 -258
  92. package/src/tui/proxy-client.ts +331 -326
  93. package/src/tui/screen.ts +294 -278
  94. package/src/tui/server-manager.ts +270 -270
  95. package/src/tui/theme.ts +432 -432
  96. package/src/tui/types.ts +33 -33
  97. package/src/tui/views/accounts-view.ts +656 -656
  98. package/src/tui/views/chat-view.ts +1018 -823
  99. package/src/tui/views/logs-view.ts +479 -413
  100. package/src/tui/views/status-view.ts +204 -204
  101. package/src/tui/views/storage-view.ts +304 -291
  102. package/src/tui/views/sync-view.ts +409 -409
  103. package/src/types/ali-oss.d.ts +32 -32
  104. package/src/update-cli.ts +121 -0
  105. package/src/utils/context-truncation.ts +84 -84
  106. package/src/utils/json.ts +380 -380
  107. package/src/utils/session-id.ts +37 -37
  108. package/src/utils/tool-call-guard.ts +84 -84
  109. package/src/utils/types.ts +109 -109
@@ -1,522 +1,522 @@
1
- import crypto from "crypto";
2
- import type {
3
- AnthropicRequest,
4
- AnthropicResponse,
5
- AnthropicResponseContentBlock,
6
- OpenAIRequest,
7
- OpenAIMessage,
8
- OpenAITool,
9
- OpenAIResponse,
10
- } from "./types.ts";
11
- import { stripThinkingSuffix } from "../../core/model-alias.ts";
12
-
13
- /**
14
- * Resolves Anthropic model requests dynamically to internal Qwen models.
15
- * Zero hardcoded versions — matches by model family/tier so ANY future Claude version
16
- * (Claude 3.8, 4, 4.5, 5, etc.) works out of the box:
17
- * - opus / sonnet / generic claude -> qwen3.8-max
18
- * - haiku -> qwen3.7-plus
19
- * - direct qwen* models pass through as-is
20
- * Preserves -fast and -thinking reasoning suffixes.
21
- */
22
- export function mapAnthropicModel(model: string): string {
23
- if (!model) return "qwen3.8-max";
24
-
25
- const { baseModel, reasoningMode } = stripThinkingSuffix(model.trim());
26
- const normalized = baseModel.toLowerCase();
27
-
28
- let targetBase = baseModel;
29
-
30
- // Direct Qwen models pass through untouched (e.g. qwen3.8-max, qwen3.7-plus)
31
- if (!normalized.startsWith("qwen")) {
32
- if (normalized.includes("haiku")) {
33
- targetBase = "qwen3.7-plus";
34
- } else if (
35
- normalized.includes("opus") ||
36
- normalized.includes("sonnet") ||
37
- normalized.startsWith("claude")
38
- ) {
39
- targetBase = "qwen3.8-max";
40
- }
41
- }
42
-
43
- if (reasoningMode === "fast") return `${targetBase}-fast`;
44
- if (reasoningMode === "thinking") return `${targetBase}-thinking`;
45
- return targetBase;
46
- }
47
-
48
- export function generateMessageId(): string {
49
- return `msg_${crypto.randomBytes(12).toString("hex")}`;
50
- }
51
-
52
- export function generateToolId(): string {
53
- return `toolu_${crypto.randomBytes(12).toString("hex")}`;
54
- }
55
-
56
- /**
57
- * Translate Anthropic request to OpenAI chat completions format.
58
- */
59
- export function translateAnthropicToOpenAI(
60
- body: AnthropicRequest,
61
- ): OpenAIRequest {
62
- const messages: OpenAIMessage[] = [];
63
-
64
- // 1. System prompt → message role=system
65
- if (body.system) {
66
- if (typeof body.system === "string") {
67
- messages.push({ role: "system", content: body.system });
68
- } else if (Array.isArray(body.system)) {
69
- const text = body.system
70
- .filter((b) => b && (b.type === "text" || !b.type))
71
- .map((b) => b.text || "")
72
- .join("\n");
73
- if (text) {
74
- messages.push({ role: "system", content: text });
75
- }
76
- }
77
- }
78
-
79
- // 2. Messages
80
- for (const msg of body.messages) {
81
- if (!msg || typeof msg !== "object") continue;
82
-
83
- if (msg.role === "user") {
84
- if (typeof msg.content === "string") {
85
- messages.push({ role: "user", content: msg.content });
86
- } else if (Array.isArray(msg.content)) {
87
- const toolResults = msg.content.filter(
88
- (b) => b && typeof b === "object" && b.type === "tool_result",
89
- );
90
- const textBlocks = msg.content.filter(
91
- (b) => b && typeof b === "object" && b.type === "text",
92
- );
93
- const imageBlocks = msg.content.filter(
94
- (b) => b && typeof b === "object" && b.type === "image",
95
- );
96
-
97
- // Tool results become individual role="tool" messages
98
- for (const tr of toolResults) {
99
- let contentStr = "";
100
- if (typeof tr.content === "string") {
101
- contentStr = tr.content;
102
- } else if (Array.isArray(tr.content)) {
103
- contentStr = tr.content
104
- .filter((b) => b && typeof b === "object" && b.type === "text")
105
- .map((b) => b.text || "")
106
- .join("\n");
107
- } else if (tr.content && typeof tr.content === "object") {
108
- contentStr = JSON.stringify(tr.content);
109
- }
110
-
111
- if (tr.is_error) {
112
- contentStr = `[Tool Error] ${contentStr}`;
113
- }
114
-
115
- messages.push({
116
- role: "tool",
117
- tool_call_id: tr.tool_use_id || "",
118
- content: contentStr,
119
- });
120
- }
121
-
122
- // Remaining user content (text and images)
123
- if (imageBlocks.length > 0) {
124
- const parts: Array<{ type: string; text?: string; image_url?: { url: string } }> = [];
125
- for (const tb of textBlocks) {
126
- if (tb.text) parts.push({ type: "text", text: tb.text });
127
- }
128
- for (const ib of imageBlocks) {
129
- if (ib.source) {
130
- if (ib.source.type === "base64" && ib.source.data && ib.source.media_type) {
131
- parts.push({
132
- type: "image_url",
133
- image_url: {
134
- url: `data:${ib.source.media_type};base64,${ib.source.data}`,
135
- },
136
- });
137
- } else if (ib.source.type === "url" && ib.source.url) {
138
- parts.push({
139
- type: "image_url",
140
- image_url: { url: ib.source.url },
141
- });
142
- }
143
- }
144
- }
145
- if (parts.length > 0) {
146
- messages.push({ role: "user", content: parts });
147
- }
148
- } else if (textBlocks.length > 0) {
149
- const joinedText = textBlocks.map((b) => b.text || "").join("\n");
150
- messages.push({ role: "user", content: joinedText });
151
- }
152
- }
153
- } else if (msg.role === "assistant") {
154
- if (typeof msg.content === "string") {
155
- messages.push({ role: "assistant", content: msg.content });
156
- } else if (Array.isArray(msg.content)) {
157
- const textBlocks = msg.content.filter(
158
- (b) => b && typeof b === "object" && b.type === "text",
159
- );
160
- const toolUses = msg.content.filter(
161
- (b) => b && typeof b === "object" && b.type === "tool_use",
162
- );
163
-
164
- const textContent = textBlocks.map((b) => b.text || "").join("\n") || null;
165
- const assistantMsg: OpenAIMessage = {
166
- role: "assistant",
167
- content: textContent,
168
- };
169
-
170
- if (toolUses.length > 0) {
171
- assistantMsg.tool_calls = toolUses.map((tu) => ({
172
- id: tu.id || generateToolId(),
173
- type: "function" as const,
174
- function: {
175
- name: tu.name || "",
176
- arguments:
177
- typeof tu.input === "string"
178
- ? tu.input
179
- : JSON.stringify(tu.input || {}),
180
- },
181
- }));
182
- }
183
-
184
- messages.push(assistantMsg);
185
- }
186
- }
187
- }
188
-
189
- // 3. Tools
190
- let tools: OpenAITool[] | undefined;
191
- if (Array.isArray(body.tools) && body.tools.length > 0) {
192
- tools = body.tools.map((tool) => ({
193
- type: "function" as const,
194
- function: {
195
- name: tool.name,
196
- description: tool.description,
197
- parameters: tool.input_schema || {},
198
- },
199
- }));
200
- }
201
-
202
- // 4. Tool choice
203
- let toolChoice: string | object | undefined;
204
- if (body.tool_choice && typeof body.tool_choice === "object") {
205
- switch (body.tool_choice.type) {
206
- case "auto":
207
- toolChoice = "auto";
208
- break;
209
- case "any":
210
- toolChoice = "required";
211
- break;
212
- case "tool":
213
- toolChoice = {
214
- type: "function",
215
- function: { name: body.tool_choice.name || "" },
216
- };
217
- break;
218
- case "none":
219
- toolChoice = "none";
220
- break;
221
- }
222
- }
223
-
224
- // 5. Reasoning / Thinking mapping
225
- let reasoningEffort: string | undefined;
226
- if (body.thinking?.type === "disabled") {
227
- reasoningEffort = "none";
228
- } else if (body.thinking?.type === "enabled") {
229
- reasoningEffort = "high";
230
- }
231
-
232
- const model = mapAnthropicModel(body.model);
233
-
234
- return {
235
- model,
236
- messages,
237
- max_tokens: body.max_tokens,
238
- max_completion_tokens: body.max_tokens,
239
- tools,
240
- tool_choice: toolChoice,
241
- reasoning_effort: reasoningEffort,
242
- stream: body.stream ?? false,
243
- temperature: body.temperature,
244
- top_p: body.top_p,
245
- };
246
- }
247
-
248
- /**
249
- * Translate OpenAI chat completion response to Anthropic format.
250
- */
251
- export function translateOpenAIToAnthropic(
252
- openaiResponse: OpenAIResponse,
253
- requestModel: string,
254
- ): AnthropicResponse {
255
- const choice = openaiResponse.choices[0];
256
- const content: AnthropicResponseContentBlock[] = [];
257
-
258
- // 1. Thinking block
259
- if (choice.message.reasoning_content) {
260
- content.push({
261
- type: "thinking",
262
- thinking: choice.message.reasoning_content,
263
- });
264
- }
265
-
266
- // 2. Text block
267
- if (choice.message.content) {
268
- let text =
269
- typeof choice.message.content === "string"
270
- ? choice.message.content
271
- : JSON.stringify(choice.message.content);
272
- // Strip raw tool call tags if tool calls exist so they don't leak into assistant text
273
- if (choice.message.tool_calls && choice.message.tool_calls.length > 0) {
274
- text = text
275
- .replace(/<tool_call>[\s\S]*?<\/tool_call>/gi, "")
276
- .replace(/<tool_call>[\s\S]*$/gi, "")
277
- .replace(/<qpx_call>[\s\S]*?<\/qpx_call>/gi, "")
278
- .replace(/<qpx_call>[\s\S]*$/gi, "")
279
- .trim();
280
- }
281
- if (text) {
282
- content.push({
283
- type: "text",
284
- text,
285
- });
286
- }
287
- }
288
-
289
- // 3. Tool use blocks
290
- if (choice.message.tool_calls && choice.message.tool_calls.length > 0) {
291
- for (const tc of choice.message.tool_calls) {
292
- let input: Record<string, unknown> = {};
293
- try {
294
- input =
295
- typeof tc.function.arguments === "string"
296
- ? JSON.parse(tc.function.arguments)
297
- : tc.function.arguments || {};
298
- } catch {
299
- input = { raw: tc.function.arguments };
300
- }
301
-
302
- content.push({
303
- type: "tool_use",
304
- id: tc.id || generateToolId(),
305
- name: tc.function.name,
306
- input,
307
- });
308
- }
309
- }
310
-
311
- const stopReasonMap: Record<string, AnthropicResponse["stop_reason"]> = {
312
- stop: "end_turn",
313
- tool_calls: "tool_use",
314
- length: "max_tokens",
315
- content_filter: "end_turn",
316
- };
317
-
318
- const hasToolUse = content.some((b) => b.type === "tool_use");
319
- const stopReason = hasToolUse
320
- ? "tool_use"
321
- : stopReasonMap[choice.finish_reason || "stop"] || "end_turn";
322
-
323
- return {
324
- id: generateMessageId(),
325
- type: "message",
326
- role: "assistant",
327
- content,
328
- model: requestModel,
329
- stop_reason: stopReason,
330
- stop_sequence: null,
331
- usage: {
332
- input_tokens: openaiResponse.usage?.prompt_tokens ?? 0,
333
- output_tokens: openaiResponse.usage?.completion_tokens ?? 0,
334
- },
335
- };
336
- }
337
-
338
- export interface AnthropicStreamState {
339
- contentBlockIndex: number;
340
- currentBlockType: "thinking" | "text" | "tool_use" | null;
341
- currentToolId: string | null;
342
- currentToolIndex: number | null;
343
- requestModel: string;
344
- inputTokens: number;
345
- outputTokens: number;
346
- hasEmittedToolUse: boolean;
347
- }
348
-
349
- /**
350
- * Translate an OpenAI streaming SSE chunk into an array of Anthropic SSE data strings.
351
- */
352
- export function translateStreamChunk(
353
- chunk: any,
354
- state: AnthropicStreamState,
355
- ): string[] {
356
- const events: string[] = [];
357
-
358
- const usage = chunk.usage;
359
- if (usage?.prompt_tokens !== undefined) {
360
- state.inputTokens = usage.prompt_tokens;
361
- }
362
- if (usage?.completion_tokens !== undefined) {
363
- state.outputTokens = usage.completion_tokens;
364
- }
365
-
366
- const choice = chunk.choices?.[0];
367
- const delta = choice?.delta ?? {};
368
-
369
- // 1. Thinking delta
370
- if (delta.reasoning_content) {
371
- if (state.currentBlockType !== "thinking") {
372
- if (state.currentBlockType !== null) {
373
- events.push(
374
- JSON.stringify({
375
- type: "content_block_stop",
376
- index: state.contentBlockIndex,
377
- }),
378
- );
379
- state.contentBlockIndex++;
380
- }
381
- events.push(
382
- JSON.stringify({
383
- type: "content_block_start",
384
- index: state.contentBlockIndex,
385
- content_block: { type: "thinking", thinking: "" },
386
- }),
387
- );
388
- state.currentBlockType = "thinking";
389
- }
390
-
391
- events.push(
392
- JSON.stringify({
393
- type: "content_block_delta",
394
- index: state.contentBlockIndex,
395
- delta: { type: "thinking_delta", thinking: delta.reasoning_content },
396
- }),
397
- );
398
- }
399
-
400
- // 2. Text delta
401
- if (delta.content) {
402
- if (state.currentBlockType !== "text") {
403
- if (state.currentBlockType !== null) {
404
- events.push(
405
- JSON.stringify({
406
- type: "content_block_stop",
407
- index: state.contentBlockIndex,
408
- }),
409
- );
410
- state.contentBlockIndex++;
411
- }
412
- events.push(
413
- JSON.stringify({
414
- type: "content_block_start",
415
- index: state.contentBlockIndex,
416
- content_block: { type: "text", text: "" },
417
- }),
418
- );
419
- state.currentBlockType = "text";
420
- }
421
-
422
- events.push(
423
- JSON.stringify({
424
- type: "content_block_delta",
425
- index: state.contentBlockIndex,
426
- delta: { type: "text_delta", text: delta.content },
427
- }),
428
- );
429
- }
430
-
431
- // 3. Tool calls delta
432
- if (Array.isArray(delta.tool_calls) && delta.tool_calls.length > 0) {
433
- for (const tc of delta.tool_calls) {
434
- const tcIndex = tc.index ?? 0;
435
-
436
- // Start new tool_use block when name appears or index changes
437
- if (
438
- tc.function?.name ||
439
- tc.id ||
440
- state.currentBlockType !== "tool_use" ||
441
- state.currentToolIndex !== tcIndex
442
- ) {
443
- if (state.currentBlockType !== null) {
444
- events.push(
445
- JSON.stringify({
446
- type: "content_block_stop",
447
- index: state.contentBlockIndex,
448
- }),
449
- );
450
- state.contentBlockIndex++;
451
- }
452
-
453
- const toolId = tc.id || generateToolId();
454
- state.currentToolId = toolId;
455
- state.currentToolIndex = tcIndex;
456
- state.hasEmittedToolUse = true;
457
-
458
- events.push(
459
- JSON.stringify({
460
- type: "content_block_start",
461
- index: state.contentBlockIndex,
462
- content_block: {
463
- type: "tool_use",
464
- id: toolId,
465
- name: tc.function?.name || "",
466
- input: {},
467
- },
468
- }),
469
- );
470
- state.currentBlockType = "tool_use";
471
- }
472
-
473
- if (tc.function?.arguments) {
474
- events.push(
475
- JSON.stringify({
476
- type: "content_block_delta",
477
- index: state.contentBlockIndex,
478
- delta: {
479
- type: "input_json_delta",
480
- partial_json: tc.function.arguments,
481
- },
482
- }),
483
- );
484
- }
485
- }
486
- }
487
-
488
- // 4. Finish reason (message end)
489
- if (choice?.finish_reason) {
490
- if (state.currentBlockType !== null) {
491
- events.push(
492
- JSON.stringify({
493
- type: "content_block_stop",
494
- index: state.contentBlockIndex,
495
- }),
496
- );
497
- state.contentBlockIndex++;
498
- state.currentBlockType = null;
499
- }
500
-
501
- const stopReason = state.hasEmittedToolUse
502
- ? "tool_use"
503
- : choice.finish_reason === "length"
504
- ? "max_tokens"
505
- : "end_turn";
506
-
507
- events.push(
508
- JSON.stringify({
509
- type: "message_delta",
510
- delta: {
511
- stop_reason: stopReason,
512
- stop_sequence: null,
513
- },
514
- usage: {
515
- output_tokens: state.outputTokens || 0,
516
- },
517
- }),
518
- );
519
- }
520
-
521
- return events;
522
- }
1
+ import crypto from "crypto";
2
+ import type {
3
+ AnthropicRequest,
4
+ AnthropicResponse,
5
+ AnthropicResponseContentBlock,
6
+ OpenAIRequest,
7
+ OpenAIMessage,
8
+ OpenAITool,
9
+ OpenAIResponse,
10
+ } from "./types.ts";
11
+ import { stripThinkingSuffix } from "../../core/model-alias.ts";
12
+
13
+ /**
14
+ * Resolves Anthropic model requests dynamically to internal Qwen models.
15
+ * Zero hardcoded versions — matches by model family/tier so ANY future Claude version
16
+ * (Claude 3.8, 4, 4.5, 5, etc.) works out of the box:
17
+ * - opus / sonnet / generic claude -> qwen3.8-max
18
+ * - haiku -> qwen3.7-plus
19
+ * - direct qwen* models pass through as-is
20
+ * Preserves -fast and -thinking reasoning suffixes.
21
+ */
22
+ export function mapAnthropicModel(model: string): string {
23
+ if (!model) return "qwen3.8-max";
24
+
25
+ const { baseModel, reasoningMode } = stripThinkingSuffix(model.trim());
26
+ const normalized = baseModel.toLowerCase();
27
+
28
+ let targetBase = baseModel;
29
+
30
+ // Direct Qwen models pass through untouched (e.g. qwen3.8-max, qwen3.7-plus)
31
+ if (!normalized.startsWith("qwen")) {
32
+ if (normalized.includes("haiku")) {
33
+ targetBase = "qwen3.7-plus";
34
+ } else if (
35
+ normalized.includes("opus") ||
36
+ normalized.includes("sonnet") ||
37
+ normalized.startsWith("claude")
38
+ ) {
39
+ targetBase = "qwen3.8-max";
40
+ }
41
+ }
42
+
43
+ if (reasoningMode === "fast") return `${targetBase}-fast`;
44
+ if (reasoningMode === "thinking") return `${targetBase}-thinking`;
45
+ return targetBase;
46
+ }
47
+
48
+ export function generateMessageId(): string {
49
+ return `msg_${crypto.randomBytes(12).toString("hex")}`;
50
+ }
51
+
52
+ export function generateToolId(): string {
53
+ return `toolu_${crypto.randomBytes(12).toString("hex")}`;
54
+ }
55
+
56
+ /**
57
+ * Translate Anthropic request to OpenAI chat completions format.
58
+ */
59
+ export function translateAnthropicToOpenAI(
60
+ body: AnthropicRequest,
61
+ ): OpenAIRequest {
62
+ const messages: OpenAIMessage[] = [];
63
+
64
+ // 1. System prompt → message role=system
65
+ if (body.system) {
66
+ if (typeof body.system === "string") {
67
+ messages.push({ role: "system", content: body.system });
68
+ } else if (Array.isArray(body.system)) {
69
+ const text = body.system
70
+ .filter((b) => b && (b.type === "text" || !b.type))
71
+ .map((b) => b.text || "")
72
+ .join("\n");
73
+ if (text) {
74
+ messages.push({ role: "system", content: text });
75
+ }
76
+ }
77
+ }
78
+
79
+ // 2. Messages
80
+ for (const msg of body.messages) {
81
+ if (!msg || typeof msg !== "object") continue;
82
+
83
+ if (msg.role === "user") {
84
+ if (typeof msg.content === "string") {
85
+ messages.push({ role: "user", content: msg.content });
86
+ } else if (Array.isArray(msg.content)) {
87
+ const toolResults = msg.content.filter(
88
+ (b) => b && typeof b === "object" && b.type === "tool_result",
89
+ );
90
+ const textBlocks = msg.content.filter(
91
+ (b) => b && typeof b === "object" && b.type === "text",
92
+ );
93
+ const imageBlocks = msg.content.filter(
94
+ (b) => b && typeof b === "object" && b.type === "image",
95
+ );
96
+
97
+ // Tool results become individual role="tool" messages
98
+ for (const tr of toolResults) {
99
+ let contentStr = "";
100
+ if (typeof tr.content === "string") {
101
+ contentStr = tr.content;
102
+ } else if (Array.isArray(tr.content)) {
103
+ contentStr = tr.content
104
+ .filter((b) => b && typeof b === "object" && b.type === "text")
105
+ .map((b) => b.text || "")
106
+ .join("\n");
107
+ } else if (tr.content && typeof tr.content === "object") {
108
+ contentStr = JSON.stringify(tr.content);
109
+ }
110
+
111
+ if (tr.is_error) {
112
+ contentStr = `[Tool Error] ${contentStr}`;
113
+ }
114
+
115
+ messages.push({
116
+ role: "tool",
117
+ tool_call_id: tr.tool_use_id || "",
118
+ content: contentStr,
119
+ });
120
+ }
121
+
122
+ // Remaining user content (text and images)
123
+ if (imageBlocks.length > 0) {
124
+ const parts: Array<{ type: string; text?: string; image_url?: { url: string } }> = [];
125
+ for (const tb of textBlocks) {
126
+ if (tb.text) parts.push({ type: "text", text: tb.text });
127
+ }
128
+ for (const ib of imageBlocks) {
129
+ if (ib.source) {
130
+ if (ib.source.type === "base64" && ib.source.data && ib.source.media_type) {
131
+ parts.push({
132
+ type: "image_url",
133
+ image_url: {
134
+ url: `data:${ib.source.media_type};base64,${ib.source.data}`,
135
+ },
136
+ });
137
+ } else if (ib.source.type === "url" && ib.source.url) {
138
+ parts.push({
139
+ type: "image_url",
140
+ image_url: { url: ib.source.url },
141
+ });
142
+ }
143
+ }
144
+ }
145
+ if (parts.length > 0) {
146
+ messages.push({ role: "user", content: parts });
147
+ }
148
+ } else if (textBlocks.length > 0) {
149
+ const joinedText = textBlocks.map((b) => b.text || "").join("\n");
150
+ messages.push({ role: "user", content: joinedText });
151
+ }
152
+ }
153
+ } else if (msg.role === "assistant") {
154
+ if (typeof msg.content === "string") {
155
+ messages.push({ role: "assistant", content: msg.content });
156
+ } else if (Array.isArray(msg.content)) {
157
+ const textBlocks = msg.content.filter(
158
+ (b) => b && typeof b === "object" && b.type === "text",
159
+ );
160
+ const toolUses = msg.content.filter(
161
+ (b) => b && typeof b === "object" && b.type === "tool_use",
162
+ );
163
+
164
+ const textContent = textBlocks.map((b) => b.text || "").join("\n") || null;
165
+ const assistantMsg: OpenAIMessage = {
166
+ role: "assistant",
167
+ content: textContent,
168
+ };
169
+
170
+ if (toolUses.length > 0) {
171
+ assistantMsg.tool_calls = toolUses.map((tu) => ({
172
+ id: tu.id || generateToolId(),
173
+ type: "function" as const,
174
+ function: {
175
+ name: tu.name || "",
176
+ arguments:
177
+ typeof tu.input === "string"
178
+ ? tu.input
179
+ : JSON.stringify(tu.input || {}),
180
+ },
181
+ }));
182
+ }
183
+
184
+ messages.push(assistantMsg);
185
+ }
186
+ }
187
+ }
188
+
189
+ // 3. Tools
190
+ let tools: OpenAITool[] | undefined;
191
+ if (Array.isArray(body.tools) && body.tools.length > 0) {
192
+ tools = body.tools.map((tool) => ({
193
+ type: "function" as const,
194
+ function: {
195
+ name: tool.name,
196
+ description: tool.description,
197
+ parameters: tool.input_schema || {},
198
+ },
199
+ }));
200
+ }
201
+
202
+ // 4. Tool choice
203
+ let toolChoice: string | object | undefined;
204
+ if (body.tool_choice && typeof body.tool_choice === "object") {
205
+ switch (body.tool_choice.type) {
206
+ case "auto":
207
+ toolChoice = "auto";
208
+ break;
209
+ case "any":
210
+ toolChoice = "required";
211
+ break;
212
+ case "tool":
213
+ toolChoice = {
214
+ type: "function",
215
+ function: { name: body.tool_choice.name || "" },
216
+ };
217
+ break;
218
+ case "none":
219
+ toolChoice = "none";
220
+ break;
221
+ }
222
+ }
223
+
224
+ // 5. Reasoning / Thinking mapping
225
+ let reasoningEffort: string | undefined;
226
+ if (body.thinking?.type === "disabled") {
227
+ reasoningEffort = "none";
228
+ } else if (body.thinking?.type === "enabled") {
229
+ reasoningEffort = "high";
230
+ }
231
+
232
+ const model = mapAnthropicModel(body.model);
233
+
234
+ return {
235
+ model,
236
+ messages,
237
+ max_tokens: body.max_tokens,
238
+ max_completion_tokens: body.max_tokens,
239
+ tools,
240
+ tool_choice: toolChoice,
241
+ reasoning_effort: reasoningEffort,
242
+ stream: body.stream ?? false,
243
+ temperature: body.temperature,
244
+ top_p: body.top_p,
245
+ };
246
+ }
247
+
248
+ /**
249
+ * Translate OpenAI chat completion response to Anthropic format.
250
+ */
251
+ export function translateOpenAIToAnthropic(
252
+ openaiResponse: OpenAIResponse,
253
+ requestModel: string,
254
+ ): AnthropicResponse {
255
+ const choice = openaiResponse.choices[0];
256
+ const content: AnthropicResponseContentBlock[] = [];
257
+
258
+ // 1. Thinking block
259
+ if (choice.message.reasoning_content) {
260
+ content.push({
261
+ type: "thinking",
262
+ thinking: choice.message.reasoning_content,
263
+ });
264
+ }
265
+
266
+ // 2. Text block
267
+ if (choice.message.content) {
268
+ let text =
269
+ typeof choice.message.content === "string"
270
+ ? choice.message.content
271
+ : JSON.stringify(choice.message.content);
272
+ // Strip raw tool call tags if tool calls exist so they don't leak into assistant text
273
+ if (choice.message.tool_calls && choice.message.tool_calls.length > 0) {
274
+ text = text
275
+ .replace(/<tool_call>[\s\S]*?<\/tool_call>/gi, "")
276
+ .replace(/<tool_call>[\s\S]*$/gi, "")
277
+ .replace(/<qpx_call>[\s\S]*?<\/qpx_call>/gi, "")
278
+ .replace(/<qpx_call>[\s\S]*$/gi, "")
279
+ .trim();
280
+ }
281
+ if (text) {
282
+ content.push({
283
+ type: "text",
284
+ text,
285
+ });
286
+ }
287
+ }
288
+
289
+ // 3. Tool use blocks
290
+ if (choice.message.tool_calls && choice.message.tool_calls.length > 0) {
291
+ for (const tc of choice.message.tool_calls) {
292
+ let input: Record<string, unknown> = {};
293
+ try {
294
+ input =
295
+ typeof tc.function.arguments === "string"
296
+ ? JSON.parse(tc.function.arguments)
297
+ : tc.function.arguments || {};
298
+ } catch {
299
+ input = { raw: tc.function.arguments };
300
+ }
301
+
302
+ content.push({
303
+ type: "tool_use",
304
+ id: tc.id || generateToolId(),
305
+ name: tc.function.name,
306
+ input,
307
+ });
308
+ }
309
+ }
310
+
311
+ const stopReasonMap: Record<string, AnthropicResponse["stop_reason"]> = {
312
+ stop: "end_turn",
313
+ tool_calls: "tool_use",
314
+ length: "max_tokens",
315
+ content_filter: "end_turn",
316
+ };
317
+
318
+ const hasToolUse = content.some((b) => b.type === "tool_use");
319
+ const stopReason = hasToolUse
320
+ ? "tool_use"
321
+ : stopReasonMap[choice.finish_reason || "stop"] || "end_turn";
322
+
323
+ return {
324
+ id: generateMessageId(),
325
+ type: "message",
326
+ role: "assistant",
327
+ content,
328
+ model: requestModel,
329
+ stop_reason: stopReason,
330
+ stop_sequence: null,
331
+ usage: {
332
+ input_tokens: openaiResponse.usage?.prompt_tokens ?? 0,
333
+ output_tokens: openaiResponse.usage?.completion_tokens ?? 0,
334
+ },
335
+ };
336
+ }
337
+
338
+ export interface AnthropicStreamState {
339
+ contentBlockIndex: number;
340
+ currentBlockType: "thinking" | "text" | "tool_use" | null;
341
+ currentToolId: string | null;
342
+ currentToolIndex: number | null;
343
+ requestModel: string;
344
+ inputTokens: number;
345
+ outputTokens: number;
346
+ hasEmittedToolUse: boolean;
347
+ }
348
+
349
+ /**
350
+ * Translate an OpenAI streaming SSE chunk into an array of Anthropic SSE data strings.
351
+ */
352
+ export function translateStreamChunk(
353
+ chunk: any,
354
+ state: AnthropicStreamState,
355
+ ): string[] {
356
+ const events: string[] = [];
357
+
358
+ const usage = chunk.usage;
359
+ if (usage?.prompt_tokens !== undefined) {
360
+ state.inputTokens = usage.prompt_tokens;
361
+ }
362
+ if (usage?.completion_tokens !== undefined) {
363
+ state.outputTokens = usage.completion_tokens;
364
+ }
365
+
366
+ const choice = chunk.choices?.[0];
367
+ const delta = choice?.delta ?? {};
368
+
369
+ // 1. Thinking delta
370
+ if (delta.reasoning_content) {
371
+ if (state.currentBlockType !== "thinking") {
372
+ if (state.currentBlockType !== null) {
373
+ events.push(
374
+ JSON.stringify({
375
+ type: "content_block_stop",
376
+ index: state.contentBlockIndex,
377
+ }),
378
+ );
379
+ state.contentBlockIndex++;
380
+ }
381
+ events.push(
382
+ JSON.stringify({
383
+ type: "content_block_start",
384
+ index: state.contentBlockIndex,
385
+ content_block: { type: "thinking", thinking: "" },
386
+ }),
387
+ );
388
+ state.currentBlockType = "thinking";
389
+ }
390
+
391
+ events.push(
392
+ JSON.stringify({
393
+ type: "content_block_delta",
394
+ index: state.contentBlockIndex,
395
+ delta: { type: "thinking_delta", thinking: delta.reasoning_content },
396
+ }),
397
+ );
398
+ }
399
+
400
+ // 2. Text delta
401
+ if (delta.content) {
402
+ if (state.currentBlockType !== "text") {
403
+ if (state.currentBlockType !== null) {
404
+ events.push(
405
+ JSON.stringify({
406
+ type: "content_block_stop",
407
+ index: state.contentBlockIndex,
408
+ }),
409
+ );
410
+ state.contentBlockIndex++;
411
+ }
412
+ events.push(
413
+ JSON.stringify({
414
+ type: "content_block_start",
415
+ index: state.contentBlockIndex,
416
+ content_block: { type: "text", text: "" },
417
+ }),
418
+ );
419
+ state.currentBlockType = "text";
420
+ }
421
+
422
+ events.push(
423
+ JSON.stringify({
424
+ type: "content_block_delta",
425
+ index: state.contentBlockIndex,
426
+ delta: { type: "text_delta", text: delta.content },
427
+ }),
428
+ );
429
+ }
430
+
431
+ // 3. Tool calls delta
432
+ if (Array.isArray(delta.tool_calls) && delta.tool_calls.length > 0) {
433
+ for (const tc of delta.tool_calls) {
434
+ const tcIndex = tc.index ?? 0;
435
+
436
+ // Start new tool_use block when name appears or index changes
437
+ if (
438
+ tc.function?.name ||
439
+ tc.id ||
440
+ state.currentBlockType !== "tool_use" ||
441
+ state.currentToolIndex !== tcIndex
442
+ ) {
443
+ if (state.currentBlockType !== null) {
444
+ events.push(
445
+ JSON.stringify({
446
+ type: "content_block_stop",
447
+ index: state.contentBlockIndex,
448
+ }),
449
+ );
450
+ state.contentBlockIndex++;
451
+ }
452
+
453
+ const toolId = tc.id || generateToolId();
454
+ state.currentToolId = toolId;
455
+ state.currentToolIndex = tcIndex;
456
+ state.hasEmittedToolUse = true;
457
+
458
+ events.push(
459
+ JSON.stringify({
460
+ type: "content_block_start",
461
+ index: state.contentBlockIndex,
462
+ content_block: {
463
+ type: "tool_use",
464
+ id: toolId,
465
+ name: tc.function?.name || "",
466
+ input: {},
467
+ },
468
+ }),
469
+ );
470
+ state.currentBlockType = "tool_use";
471
+ }
472
+
473
+ if (tc.function?.arguments) {
474
+ events.push(
475
+ JSON.stringify({
476
+ type: "content_block_delta",
477
+ index: state.contentBlockIndex,
478
+ delta: {
479
+ type: "input_json_delta",
480
+ partial_json: tc.function.arguments,
481
+ },
482
+ }),
483
+ );
484
+ }
485
+ }
486
+ }
487
+
488
+ // 4. Finish reason (message end)
489
+ if (choice?.finish_reason) {
490
+ if (state.currentBlockType !== null) {
491
+ events.push(
492
+ JSON.stringify({
493
+ type: "content_block_stop",
494
+ index: state.contentBlockIndex,
495
+ }),
496
+ );
497
+ state.contentBlockIndex++;
498
+ state.currentBlockType = null;
499
+ }
500
+
501
+ const stopReason = state.hasEmittedToolUse
502
+ ? "tool_use"
503
+ : choice.finish_reason === "length"
504
+ ? "max_tokens"
505
+ : "end_turn";
506
+
507
+ events.push(
508
+ JSON.stringify({
509
+ type: "message_delta",
510
+ delta: {
511
+ stop_reason: stopReason,
512
+ stop_sequence: null,
513
+ },
514
+ usage: {
515
+ output_tokens: state.outputTokens || 0,
516
+ },
517
+ }),
518
+ );
519
+ }
520
+
521
+ return events;
522
+ }