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,202 +1,202 @@
1
- import { z } from "zod";
2
- import type { ResponsesRequest } from "./types.ts";
3
-
4
- // ============ Zod schemas ============
5
-
6
- const ContentPartSchema = z.object({
7
- type: z.enum([
8
- "input_text",
9
- "output_text",
10
- "text",
11
- "input_image",
12
- "input_file",
13
- ]),
14
- text: z.string().optional(),
15
- image_url: z.string().optional(),
16
- detail: z.enum(["auto", "low", "high"]).optional(),
17
- file: z
18
- .object({
19
- file_id: z.string().optional(),
20
- file_data: z.string().optional(),
21
- filename: z.string().optional(),
22
- })
23
- .optional(),
24
- });
25
-
26
- const MessageInputSchema = z.object({
27
- type: z.literal("message").optional(),
28
- role: z.enum(["user", "assistant", "system", "developer"]),
29
- content: z.union([z.string(), z.array(ContentPartSchema)]),
30
- });
31
-
32
- const FunctionCallInputSchema = z.object({
33
- type: z.literal("function_call"),
34
- call_id: z.string().optional(),
35
- name: z.string().optional(),
36
- arguments: z.string().optional(),
37
- });
38
-
39
- const FunctionCallOutputInputSchema = z.object({
40
- type: z.literal("function_call_output"),
41
- call_id: z.string().optional(),
42
- output: z.string().optional(),
43
- content: z.union([z.string(), z.array(ContentPartSchema)]).optional(),
44
- role: z.literal("tool").optional(),
45
- });
46
-
47
- // Items with explicit type field
48
- const TypedInputSchema = z.union([
49
- FunctionCallInputSchema,
50
- FunctionCallOutputInputSchema,
51
- ]);
52
-
53
- // Fallback: accept any object with a type field (e.g. reasoning items)
54
- // This prevents rejection of input types we don't explicitly handle
55
- const UnknownTypedInputSchema = z
56
- .object({
57
- type: z.string(),
58
- })
59
- .passthrough();
60
-
61
- // Messages can have type: "message" or no type at all (inferred from role)
62
- const InputMessageSchema = z.union([
63
- TypedInputSchema,
64
- MessageInputSchema,
65
- UnknownTypedInputSchema,
66
- ]);
67
-
68
- // Function tool schema
69
- const FunctionToolSchema = z.object({
70
- type: z.literal("function"),
71
- name: z.string().min(1),
72
- description: z.string().optional(),
73
- parameters: z.record(z.string(), z.any()).optional(),
74
- strict: z.boolean().optional(),
75
- });
76
-
77
- // Built-in tools (web_search, file_search, shell, etc.) - passthrough
78
- const BuiltInToolSchema = z
79
- .object({
80
- type: z.string(),
81
- })
82
- .passthrough();
83
-
84
- const ToolSchema = z.union([FunctionToolSchema, BuiltInToolSchema]);
85
-
86
- const ToolChoiceSchema = z.union([
87
- z.literal("auto"),
88
- z.literal("required"),
89
- z.literal("none"),
90
- z
91
- .object({
92
- type: z.literal("function"),
93
- name: z.string().optional(),
94
- function: z.object({ name: z.string() }).optional(),
95
- })
96
- .refine((value) => value.name || value.function?.name, {
97
- message: "Function tool_choice requires 'name' or 'function.name'",
98
- }),
99
- ]);
100
-
101
- const ResponsesRequestSchema = z.object({
102
- model: z.string().min(1),
103
- input: z.union([z.string(), z.array(InputMessageSchema)]),
104
- instructions: z.string().nullable().optional(),
105
- stream: z.boolean().optional(),
106
- previous_response_id: z.string().nullable().optional(),
107
- tools: z.array(ToolSchema).nullable().optional(),
108
- tool_choice: ToolChoiceSchema.nullable().optional(),
109
- temperature: z.number().min(0).max(2).optional(),
110
- top_p: z.number().min(0).max(1).optional(),
111
- max_output_tokens: z.number().int().positive().optional(),
112
- store: z.boolean().optional(),
113
- user: z.string().optional(),
114
- metadata: z.record(z.string(), z.string()).optional(),
115
- parallel_tool_calls: z.boolean().optional(),
116
- reasoning: z
117
- .object({
118
- effort: z.string().optional(),
119
- summary: z.enum(["auto", "concise", "detailed"]).optional(),
120
- })
121
- .nullable()
122
- .optional(),
123
- reasoning_effort: z.string().optional(),
124
- text: z
125
- .object({
126
- format: z
127
- .object({
128
- type: z.enum(["text", "json_schema", "json_object"]),
129
- name: z.string().optional(),
130
- description: z.string().optional(),
131
- schema: z.record(z.string(), z.any()).optional(),
132
- strict: z.boolean().optional(),
133
- })
134
- .optional(),
135
- verbosity: z.enum(["low", "medium", "high"]).optional(),
136
- })
137
- .nullable()
138
- .optional(),
139
- truncation: z.enum(["auto", "disabled"]).optional(),
140
- service_tier: z.enum(["auto", "default", "flex", "priority"]).optional(),
141
- });
142
-
143
- // ============ Validation function ============
144
-
145
- export interface ValidationResult {
146
- valid: boolean;
147
- error?: string;
148
- // Use broader type to accept unknown input item types from clients like Codex
149
- data?: Omit<ResponsesRequest, "input"> & {
150
- input: string | unknown[];
151
- };
152
- }
153
-
154
- export function validateResponsesRequest(body: unknown): ValidationResult {
155
- const result = ResponsesRequestSchema.safeParse(body);
156
- if (!result.success) {
157
- const firstIssue = result.error.issues[0];
158
- const path = firstIssue.path.join(".");
159
- // Log the problematic input for debugging
160
- const inputVal = (body as any)?.input;
161
- const itemPath = firstIssue.path;
162
- let debugInfo = "";
163
- if (Array.isArray(inputVal) && itemPath.length >= 2) {
164
- const idx = typeof itemPath[1] === "number" ? itemPath[1] : undefined;
165
- if (idx !== undefined) {
166
- debugInfo = ` | item[${idx}]: ${JSON.stringify(inputVal[idx]).substring(0, 200)}`;
167
- }
168
- }
169
- console.warn(
170
- `[Responses] Validation failed: '${path}' — ${firstIssue.message}${debugInfo}`,
171
- );
172
- return {
173
- valid: false,
174
- error: `Invalid '${path}': ${firstIssue.message}`,
175
- };
176
- }
177
-
178
- // Extra validations
179
- const data = result.data;
180
-
181
- // Validate tool_choice requires tools
182
- if (
183
- data.tool_choice != null &&
184
- data.tool_choice !== "none" &&
185
- !data.tools?.length
186
- ) {
187
- return {
188
- valid: false,
189
- error: "'tool_choice' requires at least one tool in 'tools'",
190
- };
191
- }
192
-
193
- // Validate previous_response_id is not empty
194
- if (data.previous_response_id === "") {
195
- return {
196
- valid: false,
197
- error: "'previous_response_id' cannot be empty",
198
- };
199
- }
200
-
201
- return { valid: true, data };
202
- }
1
+ import { z } from "zod";
2
+ import type { ResponsesRequest } from "./types.ts";
3
+
4
+ // ============ Zod schemas ============
5
+
6
+ const ContentPartSchema = z.object({
7
+ type: z.enum([
8
+ "input_text",
9
+ "output_text",
10
+ "text",
11
+ "input_image",
12
+ "input_file",
13
+ ]),
14
+ text: z.string().optional(),
15
+ image_url: z.string().optional(),
16
+ detail: z.enum(["auto", "low", "high"]).optional(),
17
+ file: z
18
+ .object({
19
+ file_id: z.string().optional(),
20
+ file_data: z.string().optional(),
21
+ filename: z.string().optional(),
22
+ })
23
+ .optional(),
24
+ });
25
+
26
+ const MessageInputSchema = z.object({
27
+ type: z.literal("message").optional(),
28
+ role: z.enum(["user", "assistant", "system", "developer"]),
29
+ content: z.union([z.string(), z.array(ContentPartSchema)]),
30
+ });
31
+
32
+ const FunctionCallInputSchema = z.object({
33
+ type: z.literal("function_call"),
34
+ call_id: z.string().optional(),
35
+ name: z.string().optional(),
36
+ arguments: z.string().optional(),
37
+ });
38
+
39
+ const FunctionCallOutputInputSchema = z.object({
40
+ type: z.literal("function_call_output"),
41
+ call_id: z.string().optional(),
42
+ output: z.string().optional(),
43
+ content: z.union([z.string(), z.array(ContentPartSchema)]).optional(),
44
+ role: z.literal("tool").optional(),
45
+ });
46
+
47
+ // Items with explicit type field
48
+ const TypedInputSchema = z.union([
49
+ FunctionCallInputSchema,
50
+ FunctionCallOutputInputSchema,
51
+ ]);
52
+
53
+ // Fallback: accept any object with a type field (e.g. reasoning items)
54
+ // This prevents rejection of input types we don't explicitly handle
55
+ const UnknownTypedInputSchema = z
56
+ .object({
57
+ type: z.string(),
58
+ })
59
+ .passthrough();
60
+
61
+ // Messages can have type: "message" or no type at all (inferred from role)
62
+ const InputMessageSchema = z.union([
63
+ TypedInputSchema,
64
+ MessageInputSchema,
65
+ UnknownTypedInputSchema,
66
+ ]);
67
+
68
+ // Function tool schema
69
+ const FunctionToolSchema = z.object({
70
+ type: z.literal("function"),
71
+ name: z.string().min(1),
72
+ description: z.string().optional(),
73
+ parameters: z.record(z.string(), z.any()).optional(),
74
+ strict: z.boolean().optional(),
75
+ });
76
+
77
+ // Built-in tools (web_search, file_search, shell, etc.) - passthrough
78
+ const BuiltInToolSchema = z
79
+ .object({
80
+ type: z.string(),
81
+ })
82
+ .passthrough();
83
+
84
+ const ToolSchema = z.union([FunctionToolSchema, BuiltInToolSchema]);
85
+
86
+ const ToolChoiceSchema = z.union([
87
+ z.literal("auto"),
88
+ z.literal("required"),
89
+ z.literal("none"),
90
+ z
91
+ .object({
92
+ type: z.literal("function"),
93
+ name: z.string().optional(),
94
+ function: z.object({ name: z.string() }).optional(),
95
+ })
96
+ .refine((value) => value.name || value.function?.name, {
97
+ message: "Function tool_choice requires 'name' or 'function.name'",
98
+ }),
99
+ ]);
100
+
101
+ const ResponsesRequestSchema = z.object({
102
+ model: z.string().min(1),
103
+ input: z.union([z.string(), z.array(InputMessageSchema)]),
104
+ instructions: z.string().nullable().optional(),
105
+ stream: z.boolean().optional(),
106
+ previous_response_id: z.string().nullable().optional(),
107
+ tools: z.array(ToolSchema).nullable().optional(),
108
+ tool_choice: ToolChoiceSchema.nullable().optional(),
109
+ temperature: z.number().min(0).max(2).optional(),
110
+ top_p: z.number().min(0).max(1).optional(),
111
+ max_output_tokens: z.number().int().positive().optional(),
112
+ store: z.boolean().optional(),
113
+ user: z.string().optional(),
114
+ metadata: z.record(z.string(), z.string()).optional(),
115
+ parallel_tool_calls: z.boolean().optional(),
116
+ reasoning: z
117
+ .object({
118
+ effort: z.string().optional(),
119
+ summary: z.enum(["auto", "concise", "detailed"]).optional(),
120
+ })
121
+ .nullable()
122
+ .optional(),
123
+ reasoning_effort: z.string().optional(),
124
+ text: z
125
+ .object({
126
+ format: z
127
+ .object({
128
+ type: z.enum(["text", "json_schema", "json_object"]),
129
+ name: z.string().optional(),
130
+ description: z.string().optional(),
131
+ schema: z.record(z.string(), z.any()).optional(),
132
+ strict: z.boolean().optional(),
133
+ })
134
+ .optional(),
135
+ verbosity: z.enum(["low", "medium", "high"]).optional(),
136
+ })
137
+ .nullable()
138
+ .optional(),
139
+ truncation: z.enum(["auto", "disabled"]).optional(),
140
+ service_tier: z.enum(["auto", "default", "flex", "priority"]).optional(),
141
+ });
142
+
143
+ // ============ Validation function ============
144
+
145
+ export interface ValidationResult {
146
+ valid: boolean;
147
+ error?: string;
148
+ // Use broader type to accept unknown input item types from clients like Codex
149
+ data?: Omit<ResponsesRequest, "input"> & {
150
+ input: string | unknown[];
151
+ };
152
+ }
153
+
154
+ export function validateResponsesRequest(body: unknown): ValidationResult {
155
+ const result = ResponsesRequestSchema.safeParse(body);
156
+ if (!result.success) {
157
+ const firstIssue = result.error.issues[0];
158
+ const path = firstIssue.path.join(".");
159
+ // Log the problematic input for debugging
160
+ const inputVal = (body as any)?.input;
161
+ const itemPath = firstIssue.path;
162
+ let debugInfo = "";
163
+ if (Array.isArray(inputVal) && itemPath.length >= 2) {
164
+ const idx = typeof itemPath[1] === "number" ? itemPath[1] : undefined;
165
+ if (idx !== undefined) {
166
+ debugInfo = ` | item[${idx}]: ${JSON.stringify(inputVal[idx]).substring(0, 200)}`;
167
+ }
168
+ }
169
+ console.warn(
170
+ `[Responses] Validation failed: '${path}' — ${firstIssue.message}${debugInfo}`,
171
+ );
172
+ return {
173
+ valid: false,
174
+ error: `Invalid '${path}': ${firstIssue.message}`,
175
+ };
176
+ }
177
+
178
+ // Extra validations
179
+ const data = result.data;
180
+
181
+ // Validate tool_choice requires tools
182
+ if (
183
+ data.tool_choice != null &&
184
+ data.tool_choice !== "none" &&
185
+ !data.tools?.length
186
+ ) {
187
+ return {
188
+ valid: false,
189
+ error: "'tool_choice' requires at least one tool in 'tools'",
190
+ };
191
+ }
192
+
193
+ // Validate previous_response_id is not empty
194
+ if (data.previous_response_id === "") {
195
+ return {
196
+ valid: false,
197
+ error: "'previous_response_id' cannot be empty",
198
+ };
199
+ }
200
+
201
+ return { valid: true, data };
202
+ }