feat(models): execute Responses and validated streams with capability routing
This commit is contained in:
@@ -5,6 +5,7 @@ import { createHash, randomUUID } from 'node:crypto';
|
||||
import { DatabaseSync } from 'node:sqlite';
|
||||
import { parse as parseToml } from 'smol-toml';
|
||||
import { modelEndpoint } from './model-hub.mjs';
|
||||
import { MODEL_PROTOCOLS } from '../../shared/contracts/model-capabilities.mjs';
|
||||
|
||||
const digest = value => createHash('sha256').update(JSON.stringify(value)).digest('hex');
|
||||
export class ConfigImporter {
|
||||
@@ -13,7 +14,7 @@ export class ConfigImporter {
|
||||
const root = os.homedir();
|
||||
const sourcePath = path.join(root, '.cc-switch', 'cc-switch.db');
|
||||
return { sources: [{ source: 'ccswitch', path: sourcePath, available: fs.existsSync(sourcePath), supported: true,
|
||||
detail: '仅导入可验证的Chat Completions配置;Responses、Anthropic和客户端登录态分别标记不兼容。' },
|
||||
detail: '导入已核对格式的Chat Completions/Responses API配置;Anthropic和客户端登录态当前不支持。' },
|
||||
{ source: 'workbuddy', path: '', available: false, supported: false, detail: '当前尚未取得固定版本的WorkBuddy模型配置契约。可通过连接JSON导入已知API配置;不读取其登录态。' }] };
|
||||
}
|
||||
read(source, filename) {
|
||||
@@ -25,8 +26,10 @@ export class ConfigImporter {
|
||||
const value = JSON.parse(fs.readFileSync(filename, 'utf8').replace(/^\uFEFF/, ''));
|
||||
if (value.schema !== 1 || !Array.isArray(value.connections) || value.connections.length > 100) throw new Error('连接JSON需要schema:1与connections数组(最多100项)。');
|
||||
for (const [index, item] of value.connections.entries()) {
|
||||
if (item.protocol && item.protocol !== 'chat-completions') { notices.push('第' + (index + 1) + '项协议暂不支持,已跳过。'); continue; }
|
||||
rows.push({ sourceId: String(item.id ?? index), name: item.name, model: item.model, baseUrl: item.baseUrl, apiKey: item.apiKey, requiresKey: item.requiresKey !== false, brand: item.brand });
|
||||
if (item.protocol && !MODEL_PROTOCOLS.includes(item.protocol)) { notices.push('第' + (index + 1) + '项协议暂不支持,已跳过。'); continue; }
|
||||
rows.push({ sourceId: String(item.id ?? index), name: item.name, model: item.model, protocol: item.protocol ?? 'chat-completions',
|
||||
streaming: item.streaming === true, capabilityDeclarations: item.capabilityDeclarations,
|
||||
baseUrl: item.baseUrl, apiKey: item.apiKey, requiresKey: item.requiresKey !== false, brand: item.brand });
|
||||
}
|
||||
} else if (source === 'ccswitch') {
|
||||
const db = new DatabaseSync(filename, { readOnly: true });
|
||||
@@ -42,9 +45,9 @@ export class ConfigImporter {
|
||||
let parsed;
|
||||
try { parsed = typeof config.config === 'string' ? parseToml(config.config) : null; } catch { notices.push(provider.name + '的Codex TOML无法解析。'); continue; }
|
||||
const selected = parsed?.model_providers?.[parsed.model_provider];
|
||||
if (!selected?.base_url || selected.wire_api !== 'chat') { notices.push(provider.name + '使用Responses或未知协议,需对应的独立适配器。'); continue; }
|
||||
if (!selected?.base_url || !['chat', 'responses'].includes(selected.wire_api)) { notices.push(provider.name + '使用未知协议,需对应的独立适配器。'); continue; }
|
||||
rows.push({ sourceId: 'codex:' + provider.id, name: provider.name, baseUrl: selected.base_url, model: parsed.model,
|
||||
apiKey: config.auth?.OPENAI_API_KEY, requiresKey: true, brand: 'CCSwitch' });
|
||||
protocol: selected.wire_api === 'responses' ? 'responses' : 'chat-completions', apiKey: config.auth?.OPENAI_API_KEY, requiresKey: true, brand: 'CCSwitch' });
|
||||
} else notices.push(provider.name + '(' + provider.app_type + ')不属于已验证的Chat Completions配置,已跳过。');
|
||||
}
|
||||
} finally { db.close(); }
|
||||
|
||||
Reference in New Issue
Block a user