cladogram 0.5.5-beta.6 → 0.5.5-beta.8

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.
@@ -1,42 +1,225 @@
1
1
  /**
2
- * 统一模型协议路由器(OpenAI 兼容 + Anthropic 原生)。
2
+ * 统一模型协议路由器与多供应商注册中心(OpenAI 兼容 + Anthropic 原生 + 本地免密)。
3
3
  */
4
4
 
5
5
  import { streamOpenAI } from './openai.js';
6
6
  import { streamAnthropic } from './anthropic.js';
7
7
 
8
+ export const PROVIDER_PRESETS = [
9
+ {
10
+ id: 'openai',
11
+ label: 'OpenAI',
12
+ protocol: 'openai',
13
+ defaultBaseUrl: 'https://api.openai.com/v1',
14
+ defaultModel: 'gpt-4.1',
15
+ models: ['gpt-4.1', 'gpt-4.1-mini', 'gpt-4o', 'o4-mini'],
16
+ requiresKey: true,
17
+ },
18
+ {
19
+ id: 'anthropic',
20
+ label: 'Anthropic',
21
+ protocol: 'anthropic',
22
+ defaultBaseUrl: 'https://api.anthropic.com/v1',
23
+ defaultModel: 'claude-sonnet-4-5',
24
+ models: ['claude-sonnet-4-5', 'claude-opus-4-5', 'claude-3-7-sonnet-latest'],
25
+ requiresKey: true,
26
+ },
27
+ {
28
+ id: 'deepseek',
29
+ label: 'DeepSeek',
30
+ protocol: 'openai',
31
+ defaultBaseUrl: 'https://api.deepseek.com/v1',
32
+ defaultModel: 'deepseek-chat',
33
+ models: ['deepseek-chat', 'deepseek-reasoner'],
34
+ requiresKey: true,
35
+ },
36
+ {
37
+ id: 'openrouter',
38
+ label: 'OpenRouter',
39
+ protocol: 'openai',
40
+ defaultBaseUrl: 'https://openrouter.ai/api/v1',
41
+ defaultModel: 'anthropic/claude-sonnet-4',
42
+ models: ['anthropic/claude-sonnet-4', 'openai/gpt-4.1', 'google/gemini-2.5-pro', 'deepseek/deepseek-r1'],
43
+ requiresKey: true,
44
+ },
45
+ {
46
+ id: 'siliconflow',
47
+ label: '硅基流动 SiliconFlow',
48
+ protocol: 'openai',
49
+ defaultBaseUrl: 'https://api.siliconflow.cn/v1',
50
+ defaultModel: 'deepseek-ai/DeepSeek-V3',
51
+ models: ['deepseek-ai/DeepSeek-V3', 'deepseek-ai/DeepSeek-R1', 'Qwen/Qwen2.5-Coder-32B-Instruct'],
52
+ requiresKey: true,
53
+ },
54
+ {
55
+ id: 'ollama',
56
+ label: 'Ollama 本地',
57
+ protocol: 'openai',
58
+ defaultBaseUrl: 'http://127.0.0.1:11434/v1',
59
+ defaultModel: 'qwen2.5-coder:14b',
60
+ models: ['qwen2.5-coder:14b', 'llama3.3', 'deepseek-r1:14b'],
61
+ requiresKey: false,
62
+ },
63
+ {
64
+ id: 'custom',
65
+ label: '自定义兼容接口',
66
+ protocol: 'openai',
67
+ defaultBaseUrl: 'https://api.openai.com/v1',
68
+ defaultModel: 'gpt-4.1-mini',
69
+ models: [],
70
+ requiresKey: true,
71
+ },
72
+ ];
73
+
74
+ export function getProviderPreset(id) {
75
+ return PROVIDER_PRESETS.find(p => p.id === id) || null;
76
+ }
77
+
78
+ export function maskApiKey(key = '') {
79
+ const clean = String(key || '').trim();
80
+ if (!clean) return '';
81
+ if (clean.length <= 8) return '••••' + clean.slice(-2);
82
+ return clean.slice(0, 3) + '••••' + clean.slice(-4);
83
+ }
84
+
8
85
  export function resolveProvider(config = {}) {
9
- const explicit = String(config.provider || process.env.CLADO_LLM_PROVIDER || 'auto').toLowerCase();
86
+ const activeId = String(config.providerId || '').toLowerCase();
87
+ const profile = (activeId && config.providers?.[activeId]) || {};
88
+ const explicit = String(profile.protocol || config.provider || process.env.CLADO_LLM_PROVIDER || 'auto').toLowerCase();
10
89
  if (explicit === 'anthropic' || explicit === 'openai') return explicit;
11
- const baseUrl = String(config.baseUrl || process.env.CLADO_LLM_BASE_URL || '');
90
+ const preset = getProviderPreset(activeId);
91
+ if (preset?.protocol === 'anthropic') return 'anthropic';
92
+ const baseUrl = String(profile.baseUrl || config.baseUrl || config.apiBaseUrl || process.env.CLADO_LLM_BASE_URL || '');
12
93
  if (/anthropic\.com/i.test(baseUrl)) return 'anthropic';
13
94
  if (!baseUrl && process.env.ANTHROPIC_API_KEY && !process.env.OPENAI_API_KEY && !process.env.CLADO_LLM_API_KEY) {
14
95
  return 'anthropic';
15
96
  }
16
- const model = String(config.model || process.env.CLADO_LLM_MODEL || '');
97
+ const model = String(profile.model || config.model || process.env.CLADO_LLM_MODEL || '');
17
98
  if (!baseUrl && /^claude-/i.test(model)) return 'anthropic';
18
99
  return 'openai';
19
100
  }
20
101
 
21
102
  export function resolveProviderCredentials(config = {}) {
103
+ const activeId = String(config.providerId || '').toLowerCase();
104
+ const preset = getProviderPreset(activeId);
105
+ const profile = (activeId && config.providers?.[activeId]) || {};
22
106
  const provider = resolveProvider(config);
23
- const apiKey = config.apiKey
24
- || process.env.CLADO_LLM_API_KEY
25
- || (provider === 'anthropic' ? process.env.ANTHROPIC_API_KEY : process.env.OPENAI_API_KEY)
26
- || '';
27
- const baseUrl = config.baseUrl
107
+
108
+ const rawKey = profile.apiKey
109
+ ?? config.apiKey
110
+ ?? process.env.CLADO_LLM_API_KEY
111
+ ?? (provider === 'anthropic' ? process.env.ANTHROPIC_API_KEY : process.env.OPENAI_API_KEY)
112
+ ?? '';
113
+ const hasApiKey = Boolean(String(rawKey || '').trim());
114
+
115
+ const baseUrl = String(
116
+ profile.baseUrl
117
+ || config.baseUrl
118
+ || config.apiBaseUrl
119
+ || preset?.defaultBaseUrl
28
120
  || process.env.CLADO_LLM_BASE_URL
29
- || (provider === 'anthropic' ? 'https://api.anthropic.com/v1' : 'https://api.openai.com/v1');
30
- const model = config.model
121
+ || (provider === 'anthropic' ? 'https://api.anthropic.com/v1' : 'https://api.openai.com/v1')
122
+ ).trim();
123
+
124
+ const isKeylessLocal = preset?.requiresKey === false || /^https?:\/\/(127\.0\.0\.1|localhost):11434(\/|$)/i.test(baseUrl);
125
+ const apiKey = hasApiKey ? String(rawKey).trim() : (isKeylessLocal ? 'ollama' : '');
126
+ const configured = hasApiKey || Boolean(activeId === 'ollama' && profile.enabled !== false && config.providers?.ollama);
127
+
128
+ const model = String(
129
+ profile.model
130
+ || config.model
131
+ || preset?.defaultModel
31
132
  || process.env.CLADO_LLM_MODEL
32
- || (provider === 'anthropic' ? 'claude-sonnet-4-5' : 'gpt-4.1-mini');
33
- return { provider, apiKey, baseUrl, model };
133
+ || (provider === 'anthropic' ? 'claude-sonnet-4-5' : 'gpt-4.1-mini')
134
+ ).trim();
135
+
136
+ const resolvedId = preset?.id || (provider === 'anthropic' ? 'anthropic' : 'openai');
137
+ const providerLabel = profile.label || preset?.label || (provider === 'anthropic' ? 'Anthropic' : 'OpenAI');
138
+
139
+ return {
140
+ providerId: resolvedId,
141
+ providerLabel,
142
+ provider,
143
+ apiKey,
144
+ hasApiKey,
145
+ configured,
146
+ baseUrl,
147
+ model,
148
+ };
149
+ }
150
+
151
+ export function listConfiguredModels(config = {}) {
152
+ const out = [];
153
+ const providersMap = config.providers && typeof config.providers === 'object' ? config.providers : {};
154
+ const seen = new Set();
155
+
156
+ for (const preset of PROVIDER_PRESETS) {
157
+ const prof = providersMap[preset.id];
158
+ if (!prof) continue;
159
+ const hasKey = Boolean(String(prof.apiKey || '').trim()) || preset.requiresKey === false;
160
+ if (!hasKey) continue;
161
+ const models = Array.isArray(prof.models) && prof.models.length
162
+ ? prof.models
163
+ : [prof.model || preset.defaultModel, ...(preset.models || [])].filter(Boolean);
164
+ const uniqueModels = [...new Set(models)].slice(0, 12);
165
+ for (const m of uniqueModels) {
166
+ const k = `${preset.id}::${m}`;
167
+ if (seen.has(k)) continue;
168
+ seen.add(k);
169
+ out.push({
170
+ providerId: preset.id,
171
+ providerLabel: prof.label || preset.label,
172
+ model: m,
173
+ });
174
+ }
175
+ }
176
+
177
+ // 兼容旧版单组顶层配置
178
+ if (!out.length) {
179
+ const creds = resolveProviderCredentials(config);
180
+ if (creds.configured) {
181
+ out.push({
182
+ providerId: creds.providerId,
183
+ providerLabel: creds.providerLabel,
184
+ model: creds.model,
185
+ });
186
+ }
187
+ }
188
+ return out;
189
+ }
190
+
191
+ export async function fetchProviderModels({ protocol = 'openai', baseUrl = '', apiKey = '', fetchImpl = fetch } = {}) {
192
+ const cleanBase = String(baseUrl || '').replace(/\/+$/, '');
193
+ if (!cleanBase) throw new Error('请先填写 API Base URL。');
194
+ const headers = { accept: 'application/json' };
195
+ if (protocol === 'anthropic') {
196
+ if (apiKey) headers['x-api-key'] = apiKey;
197
+ headers['anthropic-version'] = '2023-06-01';
198
+ } else if (apiKey) {
199
+ headers.authorization = `Bearer ${apiKey}`;
200
+ }
201
+ const res = await fetchImpl(`${cleanBase}/models`, {
202
+ method: 'GET',
203
+ headers,
204
+ signal: AbortSignal.timeout(6500),
205
+ });
206
+ if (!res.ok) {
207
+ const txt = await res.text().catch(() => '');
208
+ throw new Error(`拉取模型列表失败(HTTP ${res.status})${txt ? ':' + txt.slice(0, 140) : ''}`);
209
+ }
210
+ const body = await res.json();
211
+ const rawList = Array.isArray(body?.data) ? body.data : (Array.isArray(body?.models) ? body.models : []);
212
+ const ids = rawList
213
+ .map(item => (typeof item === 'string' ? item : (item?.id || item?.name || '')))
214
+ .map(s => String(s).trim())
215
+ .filter(Boolean);
216
+ return [...new Set(ids)].slice(0, 60);
34
217
  }
35
218
 
36
219
  export async function streamModelTurn(options) {
37
220
  const creds = resolveProviderCredentials(options.config);
38
221
  if (!creds.apiKey) {
39
- throw new Error('未配置内置 Agent 的 API Key。请在「灵感」设置中填写 apiKey / provider / baseUrl / model,或改用外部 ACP 执行器。');
222
+ throw new Error('尚未配置供应商与模型 API Key。请点击输入框左侧模型按钮或「Agent 设置 → 供应商与模型」完成配置。');
40
223
  }
41
224
  const fn = creds.provider === 'anthropic' ? streamAnthropic : streamOpenAI;
42
225
  return fn({