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,144 @@
1
+ export interface ValidationResult {
2
+ valid: boolean;
3
+ error?: string;
4
+ }
5
+
6
+ /**
7
+ * Validate Anthropic request format
8
+ */
9
+ export function validateAnthropicRequest(body: unknown): ValidationResult {
10
+ if (!body || typeof body !== "object" || Array.isArray(body)) {
11
+ return { valid: false, error: "Request body must be a JSON object" };
12
+ }
13
+
14
+ const req = body as Record<string, unknown>;
15
+
16
+ // Model is required
17
+ if (!req.model || typeof req.model !== "string") {
18
+ return { valid: false, error: "Missing or invalid 'model' field" };
19
+ }
20
+
21
+ // max_tokens is required
22
+ if (req.max_tokens === undefined || req.max_tokens === null) {
23
+ return { valid: false, error: "Missing required field: max_tokens" };
24
+ }
25
+
26
+ if (typeof req.max_tokens !== "number" || req.max_tokens < 1) {
27
+ return { valid: false, error: "max_tokens must be a positive number" };
28
+ }
29
+
30
+ // messages is required
31
+ if (!req.messages || !Array.isArray(req.messages)) {
32
+ return { valid: false, error: "Missing or invalid 'messages' field" };
33
+ }
34
+
35
+ if (req.messages.length === 0) {
36
+ return { valid: false, error: "messages array cannot be empty" };
37
+ }
38
+
39
+ // Validate each message
40
+ for (let i = 0; i < req.messages.length; i++) {
41
+ const msg = req.messages[i];
42
+ if (!msg || typeof msg !== "object") {
43
+ return { valid: false, error: `messages[${i}] must be an object` };
44
+ }
45
+
46
+ if (
47
+ msg.role !== "user" &&
48
+ msg.role !== "assistant" &&
49
+ msg.role !== "system"
50
+ ) {
51
+ return {
52
+ valid: false,
53
+ error: `messages[${i}].role must be 'user', 'assistant' or 'system'`,
54
+ };
55
+ }
56
+
57
+ if (msg.content === undefined || msg.content === null) {
58
+ return { valid: false, error: `messages[${i}].content is required` };
59
+ }
60
+
61
+ // Content can be string or array of content blocks
62
+ if (typeof msg.content !== "string" && !Array.isArray(msg.content)) {
63
+ return {
64
+ valid: false,
65
+ error: `messages[${i}].content must be a string or array of content blocks`,
66
+ };
67
+ }
68
+ }
69
+
70
+ // Validate tools if present
71
+ if (req.tools !== undefined) {
72
+ if (!Array.isArray(req.tools)) {
73
+ return { valid: false, error: "'tools' must be an array" };
74
+ }
75
+
76
+ for (let i = 0; i < req.tools.length; i++) {
77
+ const tool = req.tools[i];
78
+ if (!tool || typeof tool !== "object") {
79
+ return { valid: false, error: `tools[${i}] must be an object` };
80
+ }
81
+
82
+ if (!tool.name || typeof tool.name !== "string") {
83
+ return {
84
+ valid: false,
85
+ error: `tools[${i}].name is required and must be a string`,
86
+ };
87
+ }
88
+
89
+ if (tool.input_schema && typeof tool.input_schema !== "object") {
90
+ return {
91
+ valid: false,
92
+ error: `tools[${i}].input_schema must be an object`,
93
+ };
94
+ }
95
+ }
96
+ }
97
+
98
+ // Validate tool_choice if present
99
+ if (req.tool_choice !== undefined && req.tool_choice !== null) {
100
+ const toolChoice = req.tool_choice as Record<string, unknown>;
101
+ if (typeof toolChoice !== "object" || !toolChoice.type) {
102
+ return {
103
+ valid: false,
104
+ error: "'tool_choice' must be an object with a 'type' field",
105
+ };
106
+ }
107
+
108
+ const validTypes = ["auto", "any", "tool", "none"];
109
+ if (!validTypes.includes(toolChoice.type as string)) {
110
+ return {
111
+ valid: false,
112
+ error: `tool_choice.type must be one of: ${validTypes.join(", ")}`,
113
+ };
114
+ }
115
+
116
+ if (toolChoice.type === "tool" && !toolChoice.name) {
117
+ return {
118
+ valid: false,
119
+ error: "tool_choice.name is required when type is 'tool'",
120
+ };
121
+ }
122
+ }
123
+
124
+ // Validate stream if present
125
+ if (req.stream !== undefined && typeof req.stream !== "boolean") {
126
+ return { valid: false, error: "'stream' must be a boolean" };
127
+ }
128
+
129
+ // Validate temperature if present
130
+ if (req.temperature !== undefined) {
131
+ if (
132
+ typeof req.temperature !== "number" ||
133
+ req.temperature < 0 ||
134
+ req.temperature > 1
135
+ ) {
136
+ return {
137
+ valid: false,
138
+ error: "'temperature' must be a number between 0 and 1",
139
+ };
140
+ }
141
+ }
142
+
143
+ return { valid: true };
144
+ }