qwenproxy-cli 1.0.0

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 -0
  2. package/README.md +907 -0
  3. package/bin/qwenproxy.js +141 -0
  4. package/package.json +78 -0
  5. package/src/api/error-classifier.ts +159 -0
  6. package/src/api/error-helpers.ts +118 -0
  7. package/src/api/models.ts +261 -0
  8. package/src/api/server.ts +859 -0
  9. package/src/cache/memory-cache.ts +385 -0
  10. package/src/clean-cache.ts +204 -0
  11. package/src/core/account-concurrency.ts +671 -0
  12. package/src/core/account-manager.ts +297 -0
  13. package/src/core/account-priority.ts +163 -0
  14. package/src/core/accounts.ts +186 -0
  15. package/src/core/config.ts +383 -0
  16. package/src/core/crypto-utils.ts +79 -0
  17. package/src/core/database.ts +276 -0
  18. package/src/core/errors.ts +118 -0
  19. package/src/core/logger.ts +269 -0
  20. package/src/core/memory-usage.ts +84 -0
  21. package/src/core/metrics.ts +291 -0
  22. package/src/core/model-alias.ts +77 -0
  23. package/src/core/model-registry.ts +544 -0
  24. package/src/core/mutex.ts +119 -0
  25. package/src/core/paths.ts +199 -0
  26. package/src/core/prompt-limits.ts +214 -0
  27. package/src/core/reasoning-effort.ts +102 -0
  28. package/src/core/stream-registry.ts +96 -0
  29. package/src/core/waf-isolation.ts +117 -0
  30. package/src/core/watchdog.ts +195 -0
  31. package/src/delete-chats.ts +23 -0
  32. package/src/index.ts +64 -0
  33. package/src/login.ts +147 -0
  34. package/src/reset-cooldowns.ts +11 -0
  35. package/src/routes/anthropic/index.ts +355 -0
  36. package/src/routes/anthropic/translate.ts +522 -0
  37. package/src/routes/anthropic/types.ts +154 -0
  38. package/src/routes/anthropic/validation.ts +144 -0
  39. package/src/routes/chat/account.ts +1817 -0
  40. package/src/routes/chat/context.ts +241 -0
  41. package/src/routes/chat/errors.ts +85 -0
  42. package/src/routes/chat/helpers.ts +268 -0
  43. package/src/routes/chat/index.ts +618 -0
  44. package/src/routes/chat/media.ts +285 -0
  45. package/src/routes/chat/retry-policy.ts +754 -0
  46. package/src/routes/chat/stop.ts +98 -0
  47. package/src/routes/chat/streaming.ts +2710 -0
  48. package/src/routes/chat/validation.ts +526 -0
  49. package/src/routes/chat.ts +2 -0
  50. package/src/routes/completions.ts +290 -0
  51. package/src/routes/images.ts +139 -0
  52. package/src/routes/responses/adapter.ts +503 -0
  53. package/src/routes/responses/index.ts +405 -0
  54. package/src/routes/responses/state.ts +230 -0
  55. package/src/routes/responses/streaming.ts +528 -0
  56. package/src/routes/responses/types.ts +285 -0
  57. package/src/routes/responses/validation.ts +202 -0
  58. package/src/routes/upload.ts +731 -0
  59. package/src/routes/videos.ts +214 -0
  60. package/src/services/auth-playwright.ts +173 -0
  61. package/src/services/captcha-coordinator.ts +161 -0
  62. package/src/services/captcha-solver.ts +553 -0
  63. package/src/services/chat-cleanup.ts +80 -0
  64. package/src/services/context-meter.ts +317 -0
  65. package/src/services/fingerprint.ts +242 -0
  66. package/src/services/human-behavior.ts +173 -0
  67. package/src/services/media-generation.ts +1748 -0
  68. package/src/services/playwright.ts +2800 -0
  69. package/src/services/qwen-chat-pool.ts +345 -0
  70. package/src/services/qwen-errors.ts +133 -0
  71. package/src/services/qwen-headers.ts +79 -0
  72. package/src/services/qwen-thread-state.ts +393 -0
  73. package/src/services/qwen-url.ts +19 -0
  74. package/src/services/qwen.ts +3126 -0
  75. package/src/services/session-keeper.ts +88 -0
  76. package/src/services/token-estimation-metrics.ts +118 -0
  77. package/src/sync/claude-code.ts +75 -0
  78. package/src/sync/codex.ts +123 -0
  79. package/src/sync/index.ts +362 -0
  80. package/src/sync/omp.ts +105 -0
  81. package/src/sync/opencode.ts +214 -0
  82. package/src/sync/types.ts +53 -0
  83. package/src/sync/utils.ts +27 -0
  84. package/src/sync-clients.ts +189 -0
  85. package/src/tools/instructions.ts +137 -0
  86. package/src/tools/manifest.ts +81 -0
  87. package/src/tools/parser.ts +2989 -0
  88. package/src/tools/toolcall-tags.ts +142 -0
  89. package/src/tools/types.ts +53 -0
  90. package/src/tui/app.ts +264 -0
  91. package/src/tui/index.ts +61 -0
  92. package/src/tui/markdown.ts +258 -0
  93. package/src/tui/proxy-client.ts +326 -0
  94. package/src/tui/screen.ts +278 -0
  95. package/src/tui/server-manager.ts +270 -0
  96. package/src/tui/theme.ts +432 -0
  97. package/src/tui/types.ts +33 -0
  98. package/src/tui/views/accounts-view.ts +656 -0
  99. package/src/tui/views/chat-view.ts +823 -0
  100. package/src/tui/views/logs-view.ts +413 -0
  101. package/src/tui/views/status-view.ts +204 -0
  102. package/src/tui/views/storage-view.ts +291 -0
  103. package/src/tui/views/sync-view.ts +409 -0
  104. package/src/types/ali-oss.d.ts +32 -0
  105. package/src/utils/context-truncation.ts +84 -0
  106. package/src/utils/json.ts +380 -0
  107. package/src/utils/session-id.ts +37 -0
  108. package/src/utils/tool-call-guard.ts +85 -0
  109. package/src/utils/types.ts +109 -0
@@ -0,0 +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
+ }
@@ -0,0 +1,154 @@
1
+ // Anthropic API Types
2
+
3
+ export interface AnthropicRequest {
4
+ model: string;
5
+ max_tokens: number;
6
+ system?: string | AnthropicContentBlock[];
7
+ messages: AnthropicMessage[];
8
+ tools?: AnthropicTool[];
9
+ tool_choice?: AnthropicToolChoice;
10
+ thinking?: AnthropicThinking;
11
+ stream?: boolean;
12
+ temperature?: number;
13
+ top_p?: number;
14
+ top_k?: number;
15
+ metadata?: Record<string, unknown>;
16
+ }
17
+
18
+ export interface AnthropicThinking {
19
+ type: "enabled" | "disabled";
20
+ budget_tokens?: number;
21
+ }
22
+
23
+ export interface AnthropicMessage {
24
+ role: "user" | "assistant";
25
+ content: string | AnthropicContentBlock[];
26
+ }
27
+
28
+ export interface AnthropicContentBlock {
29
+ type: "text" | "tool_use" | "tool_result" | "image" | "document" | "thinking";
30
+ text?: string;
31
+ id?: string;
32
+ name?: string;
33
+ input?: Record<string, unknown>;
34
+ tool_use_id?: string;
35
+ content?: string | AnthropicContentBlock[];
36
+ is_error?: boolean;
37
+ source?: AnthropicImageSource;
38
+ thinking?: string;
39
+ }
40
+
41
+ export interface AnthropicImageSource {
42
+ type: "base64" | "url";
43
+ media_type: string;
44
+ data?: string;
45
+ url?: string;
46
+ }
47
+
48
+ export interface AnthropicTool {
49
+ name: string;
50
+ description?: string;
51
+ input_schema: Record<string, unknown>;
52
+ }
53
+
54
+ export interface AnthropicToolChoice {
55
+ type: "auto" | "any" | "tool" | "none";
56
+ name?: string;
57
+ }
58
+
59
+ export interface AnthropicResponse {
60
+ id: string;
61
+ type: "message";
62
+ role: "assistant";
63
+ content: AnthropicResponseContentBlock[];
64
+ model: string;
65
+ stop_reason: "end_turn" | "tool_use" | "max_tokens" | "stop_sequence" | null;
66
+ stop_sequence: string | null;
67
+ usage: AnthropicUsage;
68
+ }
69
+
70
+ export interface AnthropicResponseContentBlock {
71
+ type: "text" | "tool_use" | "thinking";
72
+ text?: string;
73
+ id?: string;
74
+ name?: string;
75
+ input?: Record<string, unknown>;
76
+ thinking?: string;
77
+ }
78
+
79
+ export interface AnthropicUsage {
80
+ input_tokens: number;
81
+ output_tokens: number;
82
+ cache_creation_input_tokens?: number;
83
+ cache_read_input_tokens?: number;
84
+ }
85
+
86
+ export interface AnthropicError {
87
+ type: "error";
88
+ error: {
89
+ type: string;
90
+ message: string;
91
+ };
92
+ request_id?: string;
93
+ }
94
+
95
+ // Internal OpenAI-compatible types for conversion
96
+ export interface OpenAIRequest {
97
+ model: string;
98
+ messages: OpenAIMessage[];
99
+ max_tokens?: number;
100
+ max_completion_tokens?: number;
101
+ tools?: OpenAITool[];
102
+ tool_choice?: string | object;
103
+ reasoning_effort?: string;
104
+ stream?: boolean;
105
+ temperature?: number;
106
+ top_p?: number;
107
+ }
108
+
109
+ export interface OpenAIMessage {
110
+ role: "system" | "user" | "assistant" | "tool";
111
+ content: string | null | Array<{ type: string; text?: string; image_url?: { url: string } }>;
112
+ tool_calls?: OpenAIToolCall[];
113
+ tool_call_id?: string;
114
+ reasoning_content?: string;
115
+ }
116
+
117
+ export interface OpenAITool {
118
+ type: "function";
119
+ function: {
120
+ name: string;
121
+ description?: string;
122
+ parameters?: Record<string, unknown>;
123
+ };
124
+ }
125
+
126
+ export interface OpenAIToolCall {
127
+ id: string;
128
+ type: "function";
129
+ function: {
130
+ name: string;
131
+ arguments: string;
132
+ };
133
+ }
134
+
135
+ export interface OpenAIResponse {
136
+ id: string;
137
+ object: "chat.completion";
138
+ created: number;
139
+ model: string;
140
+ choices: OpenAIChoice[];
141
+ usage: OpenAIUsage;
142
+ }
143
+
144
+ export interface OpenAIChoice {
145
+ index: number;
146
+ message: OpenAIMessage;
147
+ finish_reason: "stop" | "tool_calls" | "length" | "content_filter" | null;
148
+ }
149
+
150
+ export interface OpenAIUsage {
151
+ prompt_tokens: number;
152
+ completion_tokens: number;
153
+ total_tokens: number;
154
+ }