@adversity/coding-tool-x 3.0.4 → 3.0.6

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.
@@ -9,21 +9,37 @@ const os = require('os');
9
9
  const https = require('https');
10
10
  const http = require('http');
11
11
  const { URL } = require('url');
12
+ const crypto = require('crypto');
12
13
 
13
14
  // Model priority by channel type
14
15
  const MODEL_PRIORITY = {
15
16
  claude: [
16
- 'claude-opus-4-5-20250929',
17
+ 'claude-opus-4-5-20251101',
17
18
  'claude-sonnet-4-5-20250929',
18
- 'claude-haiku-4-5-20250929',
19
+ 'claude-haiku-4-5-20251001',
19
20
  'claude-sonnet-4-20250514',
20
- 'claude-opus-4-20250514',
21
- 'claude-haiku-3-5-20241022',
22
- 'claude-3-5-haiku-20241022'
21
+ 'claude-opus-4-20250514'
23
22
  ],
24
- codex: ['gpt-4o-mini', 'gpt-4o', 'gpt-5-codex', 'o3'],
25
- gemini: ['gemini-2.5-flash', 'gemini-2.5-pro']
23
+ codex: [
24
+ 'gpt-5.2-codex',
25
+ 'gpt-5.1-codex-max',
26
+ 'gpt-5.1-codex-mini',
27
+ 'gpt-5.1-codex',
28
+ 'gpt-5-codex',
29
+ 'gpt-5.2',
30
+ 'gpt-5.1',
31
+ 'gpt-5'
32
+ ],
33
+ gemini: [
34
+ 'gemini-3-pro',
35
+ 'gemini-3-flash',
36
+ 'gemini-3-deep-think',
37
+ 'gemini-2.5-pro',
38
+ 'gemini-2.5-flash'
39
+ ]
26
40
  };
41
+ // openai_compatible 复用 codex 的模型列表
42
+ MODEL_PRIORITY.openai_compatible = MODEL_PRIORITY.codex;
27
43
 
28
44
  const PROVIDER_CAPABILITIES = {
29
45
  claude: {
@@ -132,6 +148,63 @@ const MODEL_ALIASES = {
132
148
  const CACHE_DURATION_MS = 5 * 60 * 1000; // 5 minutes
133
149
  const TEST_TIMEOUT_MS = 10000; // 10 seconds per model test
134
150
 
151
+ /**
152
+ * Generate realistic User-Agent strings that mimic official SDKs
153
+ * @param {string} channelType - 'claude' | 'codex' | 'gemini' | 'openai_compatible'
154
+ * @returns {string} - User-Agent string
155
+ */
156
+ function getRealisticUserAgent(channelType) {
157
+ const nodeVersion = process.version.slice(1); // e.g., "18.17.0"
158
+ const platform = process.platform; // e.g., "darwin", "linux", "win32"
159
+
160
+ switch (channelType) {
161
+ case 'claude':
162
+ // Mimics official Anthropic Python SDK
163
+ return `anthropic-sdk-python/0.39.0 python/3.11.4 ${platform}`;
164
+ case 'gemini':
165
+ // Mimics official Google SDK
166
+ return `google-generativeai/0.8.2 python/3.11.4 ${platform}`;
167
+ case 'codex':
168
+ case 'openai_compatible':
169
+ default:
170
+ // Mimics official OpenAI Python SDK
171
+ return `OpenAI/Python/1.56.0`;
172
+ }
173
+ }
174
+
175
+ /**
176
+ * Add a small random delay between requests to avoid rate limiting
177
+ * and appear more human-like (100-300ms)
178
+ * @returns {Promise<void>}
179
+ */
180
+ async function randomDelay() {
181
+ const delay = 100 + Math.random() * 200;
182
+ return new Promise(resolve => setTimeout(resolve, delay));
183
+ }
184
+
185
+ /**
186
+ * Build common headers for API requests that look like legitimate SDK clients
187
+ * @param {string} channelType - Channel type
188
+ * @param {Object} channel - Channel configuration
189
+ * @returns {Object} - Headers object
190
+ */
191
+ function buildRequestHeaders(channelType, channel) {
192
+ const headers = {
193
+ 'User-Agent': getRealisticUserAgent(channelType),
194
+ 'Accept': 'application/json',
195
+ 'Accept-Encoding': 'gzip, deflate',
196
+ 'Connection': 'keep-alive',
197
+ 'X-Request-Id': crypto.randomUUID()
198
+ };
199
+
200
+ // For OpenAI-compatible APIs, add additional headers
201
+ if (channelType === 'codex' || channelType === 'openai_compatible') {
202
+ headers['OpenAI-Beta'] = 'assistants=v2';
203
+ }
204
+
205
+ return headers;
206
+ }
207
+
135
208
  /**
136
209
  * Get cache file path
137
210
  */
@@ -210,9 +283,10 @@ async function testModelAvailability(channel, channelType, model) {
210
283
  const baseUrl = channel.baseUrl.trim().replace(/\/+$/, '');
211
284
  let testUrl;
212
285
  let requestBody;
286
+ // Start with common headers that look like legitimate SDK clients
213
287
  let headers = {
214
- 'Content-Type': 'application/json',
215
- 'User-Agent': 'Coding-Tool-ModelDetector/1.0'
288
+ ...buildRequestHeaders(channelType, channel),
289
+ 'Content-Type': 'application/json'
216
290
  };
217
291
 
218
292
  // Construct API endpoint and request based on channel type
@@ -225,8 +299,11 @@ async function testModelAvailability(channel, channelType, model) {
225
299
  max_tokens: 1,
226
300
  messages: [{ role: 'user', content: 'test' }]
227
301
  });
228
- } else if (channelType === 'codex') {
229
- testUrl = `${baseUrl}/v1/chat/completions`;
302
+ } else if (channelType === 'codex' || channelType === 'openai_compatible') {
303
+ // 处理 baseUrl 已包含 /v1 的情况
304
+ testUrl = baseUrl.endsWith('/v1')
305
+ ? `${baseUrl}/chat/completions`
306
+ : `${baseUrl}/v1/chat/completions`;
230
307
  headers['Authorization'] = `Bearer ${channel.apiKey}`;
231
308
  requestBody = JSON.stringify({
232
309
  model: model,
@@ -344,9 +421,16 @@ async function probeModelAvailability(channel, channelType) {
344
421
  console.log(`[ModelDetector] Testing models for channel ${channel.name} (${channelType})...`);
345
422
 
346
423
  const availableModels = [];
424
+ let isFirstModel = true;
347
425
 
348
426
  // Test models in priority order
349
427
  for (const model of modelsToTest) {
428
+ // Add delay between model tests to avoid rate limiting (skip first)
429
+ if (!isFirstModel) {
430
+ await randomDelay();
431
+ }
432
+ isFirstModel = false;
433
+
350
434
  const isAvailable = await testModelAvailability(channel, channelType, model);
351
435
 
352
436
  if (isAvailable) {
@@ -421,8 +505,12 @@ function getCachedModelInfo(channelId) {
421
505
  * @returns {Promise<Object>} { models: string[], supported: boolean, cached: boolean, error: string|null, fallbackUsed: boolean }
422
506
  */
423
507
  async function fetchModelsFromProvider(channel, channelType) {
424
- // If no type specified or type is 'claude', auto-detect
425
- if (!channelType || channelType === 'claude') {
508
+ // PRESERVE original channel type for fallback model selection
509
+ const originalChannelType = channelType;
510
+
511
+ // Only auto-detect if channelType is NOT specified at all
512
+ // DO NOT auto-detect when channelType is 'claude' - respect the caller's intent
513
+ if (!channelType) {
426
514
  channelType = detectChannelType(channel);
427
515
  console.log(`[ModelDetector] Auto-detected channel type: ${channelType} for ${channel.name}`);
428
516
  }
@@ -457,17 +545,18 @@ async function fetchModelsFromProvider(channel, channelType) {
457
545
  return new Promise((resolve) => {
458
546
  try {
459
547
  const baseUrl = channel.baseUrl.trim().replace(/\/+$/, '');
460
- const endpoint = capability.modelListEndpoint;
461
- const requestUrl = `${baseUrl}${endpoint}`;
548
+ const endpoint = capability.modelListEndpoint; // e.g. '/v1/models'
549
+ // 避免路径重复:如果 baseUrl 已包含 /v1,则只拼接 /models
550
+ const requestUrl = baseUrl.endsWith('/v1') && endpoint.startsWith('/v1/')
551
+ ? `${baseUrl}${endpoint.slice(3)}`
552
+ : `${baseUrl}${endpoint}`;
462
553
 
463
554
  const parsedUrl = new URL(requestUrl);
464
555
  const isHttps = parsedUrl.protocol === 'https:';
465
556
  const httpModule = isHttps ? https : http;
466
557
 
467
- const headers = {
468
- 'User-Agent': 'Coding-Tool-ModelDetector/1.0',
469
- 'Accept': 'application/json'
470
- };
558
+ // Use realistic SDK headers to avoid anti-crawler detection
559
+ const headers = buildRequestHeaders(channelType, channel);
471
560
 
472
561
  // Add authentication header
473
562
  if (capability.authHeader && channel.apiKey) {
@@ -540,11 +629,15 @@ async function fetchModelsFromProvider(channel, channelType) {
540
629
  let errorHint;
541
630
 
542
631
  if (isCloudflare) {
543
- errorMessage = 'Cloudflare 防护拦截,已使用默认模型';
544
- errorHint = '该 API 端点受 Cloudflare 保护,已自动使用默认模型 claude-sonnet-4-5';
545
- console.warn(`[ModelDetector] Cloudflare protection detected for ${channel.name}, using default model`);
632
+ // Use originalChannelType for fallback to ensure correct models
633
+ // This prevents Claude channels from getting Codex models when using third-party proxies
634
+ const fallbackModels = MODEL_PRIORITY[originalChannelType || channelType] || MODEL_PRIORITY.claude;
635
+ const fallbackLabel = fallbackModels[0] || 'unknown';
636
+ errorMessage = 'Cloudflare 防护拦截,已使用默认模型列表';
637
+ errorHint = `该 API 端点受 Cloudflare 保护,已自动使用默认模型列表`;
638
+ console.warn(`[ModelDetector] Cloudflare protection detected for ${channel.name}, using default models for ${originalChannelType || channelType}`);
546
639
  resolve({
547
- models: ['claude-sonnet-4-5'],
640
+ models: fallbackModels,
548
641
  supported: true,
549
642
  cached: false,
550
643
  fallbackUsed: true,
@@ -89,37 +89,6 @@ const BUILTIN_TEMPLATES = [
89
89
  ]
90
90
  },
91
91
  isBuiltin: true
92
- },
93
- {
94
- id: 'mcp-full',
95
- name: 'MCP 全功能',
96
- description: '允许所有 MCP 工具和常用命令,适合使用 MCP 服务器的项目',
97
- permissions: {
98
- allow: [
99
- 'Read(*)',
100
- 'Edit(*)',
101
- 'Bash(cat:*)',
102
- 'Bash(ls:*)',
103
- 'Bash(find:*)',
104
- 'Bash(grep:*)',
105
- 'Bash(tree:*)',
106
- 'Bash(git:*)',
107
- 'Bash(npm:*)',
108
- 'Bash(pnpm:*)',
109
- 'Bash(yarn:*)',
110
- 'WebSearch',
111
- 'mcp__Serena__*',
112
- 'mcp__fetch__fetch',
113
- 'mcp__memory__*',
114
- 'mcp__github__*',
115
- 'mcp__context7__*'
116
- ],
117
- deny: [
118
- 'Bash(rm -rf:*)',
119
- 'Bash(sudo:*)'
120
- ]
121
- },
122
- isBuiltin: true
123
92
  }
124
93
  ];
125
94