tabby-ai-assistant 1.0.12 → 1.0.15
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.
- package/.editorconfig +18 -0
- package/README.md +113 -55
- package/dist/index.js +1 -1
- package/package.json +6 -4
- package/src/components/chat/ai-sidebar.component.scss +220 -9
- package/src/components/chat/ai-sidebar.component.ts +364 -29
- package/src/components/chat/chat-input.component.ts +36 -4
- package/src/components/chat/chat-interface.component.ts +225 -5
- package/src/components/chat/chat-message.component.ts +6 -1
- package/src/components/settings/context-settings.component.ts +91 -91
- package/src/components/terminal/ai-toolbar-button.component.ts +4 -2
- package/src/components/terminal/command-suggestion.component.ts +148 -6
- package/src/index.ts +0 -6
- package/src/providers/tabby/ai-toolbar-button.provider.ts +7 -3
- package/src/services/chat/ai-sidebar.service.ts +414 -410
- package/src/services/chat/chat-session.service.ts +36 -12
- package/src/services/context/compaction.ts +110 -134
- package/src/services/context/manager.ts +27 -7
- package/src/services/context/memory.ts +17 -33
- package/src/services/context/summary.service.ts +136 -0
- package/src/services/core/ai-assistant.service.ts +1060 -37
- package/src/services/core/ai-provider-manager.service.ts +154 -25
- package/src/services/core/checkpoint.service.ts +218 -18
- package/src/services/core/config-provider.service.ts +4 -12
- package/src/services/core/toast.service.ts +106 -106
- package/src/services/providers/anthropic-provider.service.ts +126 -202
- package/src/services/providers/base-provider.service.ts +315 -21
- package/src/services/providers/glm-provider.service.ts +151 -233
- package/src/services/providers/minimax-provider.service.ts +55 -238
- package/src/services/providers/ollama-provider.service.ts +117 -188
- package/src/services/providers/openai-compatible.service.ts +165 -177
- package/src/services/providers/openai-provider.service.ts +170 -177
- package/src/services/providers/vllm-provider.service.ts +116 -188
- package/src/services/terminal/terminal-context.service.ts +265 -5
- package/src/services/terminal/terminal-manager.service.ts +748 -748
- package/src/services/terminal/terminal-tools.service.ts +612 -441
- package/src/types/ai.types.ts +156 -3
- package/src/types/provider.types.ts +206 -75
- package/src/utils/cost.utils.ts +249 -0
- package/src/utils/validation.utils.ts +306 -2
- package/dist/index.js.LICENSE.txt +0 -18
- package/src/index.ts.backup +0 -165
- package/src/services/chat/chat-history.service.ts.backup +0 -239
- package/src/services/terminal/command-analyzer.service.ts +0 -43
- package/src/services/terminal/context-menu.service.ts +0 -45
- package/src/services/terminal/hotkey.service.ts +0 -53
- package/webpack.config.js.backup +0 -57
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* API成本计算工具类
|
|
3
|
+
* 提供各AI提供商的API调用成本估算功能
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* AI提供商类型
|
|
8
|
+
*/
|
|
9
|
+
export type AIProvider = 'openai' | 'anthropic' | 'minimax' | 'glm' | 'openai-compatible';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 模型定价信息
|
|
13
|
+
*/
|
|
14
|
+
export interface ModelPricing {
|
|
15
|
+
provider: AIProvider;
|
|
16
|
+
model: string;
|
|
17
|
+
inputPricePerMillion: number; // 每百万输入token的价格(美元)
|
|
18
|
+
outputPricePerMillion: number; // 每百万输出token的价格(美元)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Token使用信息
|
|
23
|
+
*/
|
|
24
|
+
export interface TokenUsage {
|
|
25
|
+
inputTokens: number;
|
|
26
|
+
outputTokens: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* 成本计算结果
|
|
31
|
+
*/
|
|
32
|
+
export interface CostResult {
|
|
33
|
+
inputCost: number; // 输入成本(美元)
|
|
34
|
+
outputCost: number; // 输出成本(美元)
|
|
35
|
+
totalCost: number; // 总成本(美元)
|
|
36
|
+
inputPricePerMillion: number;
|
|
37
|
+
outputPricePerMillion: number;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* 默认模型定价表(2024年最新价格)
|
|
42
|
+
*/
|
|
43
|
+
const DEFAULT_PRICING: ModelPricing[] = [
|
|
44
|
+
// OpenAI
|
|
45
|
+
{ provider: 'openai', model: 'gpt-4', inputPricePerMillion: 30, outputPricePerMillion: 60 },
|
|
46
|
+
{ provider: 'openai', model: 'gpt-4-turbo', inputPricePerMillion: 10, outputPricePerMillion: 30 },
|
|
47
|
+
{ provider: 'openai', model: 'gpt-4o', inputPricePerMillion: 5, outputPricePerMillion: 15 },
|
|
48
|
+
{ provider: 'openai', model: 'gpt-3.5-turbo', inputPricePerMillion: 0.5, outputPricePerMillion: 1.5 },
|
|
49
|
+
|
|
50
|
+
// Anthropic
|
|
51
|
+
{ provider: 'anthropic', model: 'claude-3-5-sonnet-20241022', inputPricePerMillion: 3, outputPricePerMillion: 15 },
|
|
52
|
+
{ provider: 'anthropic', model: 'claude-3-opus-20240229', inputPricePerMillion: 15, outputPricePerMillion: 75 },
|
|
53
|
+
{ provider: 'anthropic', model: 'claude-3-haiku-20240307', inputPricePerMillion: 0.25, outputPricePerMillion: 1.25 },
|
|
54
|
+
|
|
55
|
+
// Minimax (智谱AI)
|
|
56
|
+
{ provider: 'minimax', model: 'abab6.5s-chat', inputPricePerMillion: 0.3, outputPricePerMillion: 0.3 },
|
|
57
|
+
{ provider: 'minimax', model: 'abab6.5-chat', inputPricePerMillion: 0.5, outputPricePerMillion: 0.5 },
|
|
58
|
+
{ provider: 'minimax', model: 'abab5.5-chat', inputPricePerMillion: 1, outputPricePerMillion: 1 },
|
|
59
|
+
|
|
60
|
+
// GLM (智谱AI)
|
|
61
|
+
{ provider: 'glm', model: 'glm-4', inputPricePerMillion: 0.5, outputPricePerMillion: 1.5 },
|
|
62
|
+
{ provider: 'glm', model: 'glm-4v', inputPricePerMillion: 0.5, outputPricePerMillion: 1.5 },
|
|
63
|
+
{ provider: 'glm', model: 'glm-3-turbo', inputPricePerMillion: 0.1, outputPricePerMillion: 0.1 },
|
|
64
|
+
];
|
|
65
|
+
|
|
66
|
+
// 自定义定价表(可扩展)
|
|
67
|
+
let customPricing: ModelPricing[] = [];
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* 设置自定义模型定价
|
|
71
|
+
*/
|
|
72
|
+
export function setCustomPricing(pricing: ModelPricing[]): void {
|
|
73
|
+
customPricing = [...pricing];
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* 获取模型定价信息
|
|
78
|
+
*/
|
|
79
|
+
export function getModelPricing(provider: AIProvider, model: string): ModelPricing | undefined {
|
|
80
|
+
// 首先查找自定义定价
|
|
81
|
+
const custom = customPricing.find(p => p.provider === provider && p.model === model);
|
|
82
|
+
if (custom) {
|
|
83
|
+
return custom;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// 然后查找默认定价
|
|
87
|
+
const defaultPricing = DEFAULT_PRICING.find(p => p.provider === provider && p.model === model);
|
|
88
|
+
|
|
89
|
+
if (defaultPricing) {
|
|
90
|
+
return defaultPricing;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// 返回该提供商的通用定价
|
|
94
|
+
return getDefaultPricingForProvider(provider);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* 获取提供商的默认定价
|
|
99
|
+
*/
|
|
100
|
+
function getDefaultPricingForProvider(provider: AIProvider): ModelPricing | undefined {
|
|
101
|
+
const providerDefaults: Record<AIProvider, Partial<ModelPricing>> = {
|
|
102
|
+
'openai': { inputPricePerMillion: 5, outputPricePerMillion: 15 },
|
|
103
|
+
'anthropic': { inputPricePerMillion: 3, outputPricePerMillion: 15 },
|
|
104
|
+
'minimax': { inputPricePerMillion: 0.5, outputPricePerMillion: 0.5 },
|
|
105
|
+
'glm': { inputPricePerMillion: 0.5, outputPricePerMillion: 1 },
|
|
106
|
+
'openai-compatible': { inputPricePerMillion: 1, outputPricePerMillion: 2 }
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
const defaults = providerDefaults[provider];
|
|
110
|
+
if (defaults) {
|
|
111
|
+
return {
|
|
112
|
+
provider,
|
|
113
|
+
model: 'default',
|
|
114
|
+
inputPricePerMillion: defaults.inputPricePerMillion ?? 1,
|
|
115
|
+
outputPricePerMillion: defaults.outputPricePerMillion ?? 2
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return undefined;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* 计算API调用成本
|
|
124
|
+
* @param provider AI提供商
|
|
125
|
+
* @param model 模型名称
|
|
126
|
+
* @param usage Token使用情况
|
|
127
|
+
* @returns 成本计算结果
|
|
128
|
+
*/
|
|
129
|
+
export function calculateCost(
|
|
130
|
+
provider: AIProvider,
|
|
131
|
+
model: string,
|
|
132
|
+
usage: TokenUsage
|
|
133
|
+
): CostResult {
|
|
134
|
+
const pricing = getModelPricing(provider, model);
|
|
135
|
+
|
|
136
|
+
if (!pricing) {
|
|
137
|
+
// 未知提供商,返回零成本
|
|
138
|
+
return {
|
|
139
|
+
inputCost: 0,
|
|
140
|
+
outputCost: 0,
|
|
141
|
+
totalCost: 0,
|
|
142
|
+
inputPricePerMillion: 0,
|
|
143
|
+
outputPricePerMillion: 0
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const inputCost = (usage.inputTokens / 1000000) * pricing.inputPricePerMillion;
|
|
148
|
+
const outputCost = (usage.outputTokens / 1000000) * pricing.outputPricePerMillion;
|
|
149
|
+
|
|
150
|
+
return {
|
|
151
|
+
inputCost: Math.round(inputCost * 1000000) / 1000000, // 保留6位小数
|
|
152
|
+
outputCost: Math.round(outputCost * 1000000) / 1000000,
|
|
153
|
+
totalCost: Math.round((inputCost + outputCost) * 1000000) / 1000000,
|
|
154
|
+
inputPricePerMillion: pricing.inputPricePerMillion,
|
|
155
|
+
outputPricePerMillion: pricing.outputPricePerMillion
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* 计算摘要生成成本
|
|
161
|
+
*/
|
|
162
|
+
export function calculateSummaryCost(
|
|
163
|
+
provider: AIProvider,
|
|
164
|
+
model: string,
|
|
165
|
+
originalMessageCount: number,
|
|
166
|
+
tokensUsed: number
|
|
167
|
+
): CostResult {
|
|
168
|
+
// 摘要生成主要是输入成本
|
|
169
|
+
const pricing = getModelPricing(provider, model);
|
|
170
|
+
|
|
171
|
+
if (!pricing) {
|
|
172
|
+
return {
|
|
173
|
+
inputCost: 0,
|
|
174
|
+
outputCost: 0,
|
|
175
|
+
totalCost: 0,
|
|
176
|
+
inputPricePerMillion: 0,
|
|
177
|
+
outputPricePerMillion: 0
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// 估算摘要的输入和输出token(假设输出占输入的5%)
|
|
182
|
+
const estimatedInputTokens = tokensUsed;
|
|
183
|
+
const estimatedOutputTokens = Math.floor(tokensUsed * 0.05);
|
|
184
|
+
|
|
185
|
+
return calculateCost(provider, model, {
|
|
186
|
+
inputTokens: estimatedInputTokens,
|
|
187
|
+
outputTokens: estimatedOutputTokens
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* 格式化成本为可读字符串
|
|
193
|
+
*/
|
|
194
|
+
export function formatCost(cost: number): string {
|
|
195
|
+
if (cost < 0.001) {
|
|
196
|
+
return `$${(cost * 1000000).toFixed(2)}`;
|
|
197
|
+
} else if (cost < 1) {
|
|
198
|
+
return `$${cost.toFixed(4)}`;
|
|
199
|
+
} else {
|
|
200
|
+
return `$${cost.toFixed(2)}`;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* 格式化成本详细信息
|
|
206
|
+
*/
|
|
207
|
+
export function formatCostDetail(result: CostResult): string {
|
|
208
|
+
const parts: string[] = [];
|
|
209
|
+
|
|
210
|
+
if (result.inputCost > 0) {
|
|
211
|
+
parts.push(`输入: ${formatCost(result.inputCost)}`);
|
|
212
|
+
}
|
|
213
|
+
if (result.outputCost > 0) {
|
|
214
|
+
parts.push(`输出: ${formatCost(result.outputCost)}`);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
return parts.join(', ') + ` (总计: ${formatCost(result.totalCost)})`;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* 估算消息的Token数量
|
|
222
|
+
*/
|
|
223
|
+
export function estimateTokenCount(text: string): number {
|
|
224
|
+
// 粗略估算:1个Token约等于4个字符(英文)
|
|
225
|
+
// 中文:1个Token约等于1.5个字符
|
|
226
|
+
const chineseCharCount = (text.match(/[\u4e00-\u9fa5]/g) || []).length;
|
|
227
|
+
const englishCharCount = text.length - chineseCharCount;
|
|
228
|
+
|
|
229
|
+
return Math.ceil(chineseCharCount / 1.5 + englishCharCount / 4);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* 计算批量请求的总成本
|
|
234
|
+
*/
|
|
235
|
+
export function calculateBatchCost(
|
|
236
|
+
provider: AIProvider,
|
|
237
|
+
model: string,
|
|
238
|
+
requests: TokenUsage[]
|
|
239
|
+
): CostResult {
|
|
240
|
+
const totalUsage = requests.reduce(
|
|
241
|
+
(acc, usage) => ({
|
|
242
|
+
inputTokens: acc.inputTokens + usage.inputTokens,
|
|
243
|
+
outputTokens: acc.outputTokens + usage.outputTokens
|
|
244
|
+
}),
|
|
245
|
+
{ inputTokens: 0, outputTokens: 0 }
|
|
246
|
+
);
|
|
247
|
+
|
|
248
|
+
return calculateCost(provider, model, totalUsage);
|
|
249
|
+
}
|
|
@@ -121,8 +121,7 @@ export function validateModel(model: string, _provider: string): { valid: boolea
|
|
|
121
121
|
return { valid: false, error: '模型名称包含非法字符' };
|
|
122
122
|
}
|
|
123
123
|
|
|
124
|
-
//
|
|
125
|
-
// _provider 参数保留用于未来扩展
|
|
124
|
+
// 注意:更完整的提供商特定验证请使用 validateProviderModel()
|
|
126
125
|
|
|
127
126
|
return { valid: true };
|
|
128
127
|
}
|
|
@@ -312,3 +311,308 @@ export function validateFilePath(path: string): { valid: boolean; error?: string
|
|
|
312
311
|
|
|
313
312
|
return { valid: true };
|
|
314
313
|
}
|
|
314
|
+
|
|
315
|
+
// ==================== AI提供商特定验证 ====================
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* AI提供商类型
|
|
319
|
+
*/
|
|
320
|
+
export type AIProviderType = 'openai' | 'anthropic' | 'minimax' | 'glm' | 'openai-compatible' | 'ollama' | 'vllm';
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* OpenAI模型列表
|
|
324
|
+
*/
|
|
325
|
+
export const OPENAI_MODELS = [
|
|
326
|
+
'gpt-4',
|
|
327
|
+
'gpt-4-turbo',
|
|
328
|
+
'gpt-4o',
|
|
329
|
+
'gpt-4o-mini',
|
|
330
|
+
'gpt-3.5-turbo',
|
|
331
|
+
'gpt-3.5-turbo-16k'
|
|
332
|
+
];
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Anthropic模型列表
|
|
336
|
+
*/
|
|
337
|
+
export const ANTHROPIC_MODELS = [
|
|
338
|
+
'claude-3-5-sonnet-20241022',
|
|
339
|
+
'claude-3-5-sonnet-20240620',
|
|
340
|
+
'claude-3-opus-20240229',
|
|
341
|
+
'claude-3-haiku-20240307'
|
|
342
|
+
];
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* GLM/智谱模型列表
|
|
346
|
+
*/
|
|
347
|
+
export const GLM_MODELS = [
|
|
348
|
+
'glm-4',
|
|
349
|
+
'glm-4-plus',
|
|
350
|
+
'glm-4v',
|
|
351
|
+
'glm-3-turbo'
|
|
352
|
+
];
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* Minimax模型列表
|
|
356
|
+
*/
|
|
357
|
+
export const MINIMAX_MODELS = [
|
|
358
|
+
'MiniMax-M2',
|
|
359
|
+
'MiniMax-M2-16k',
|
|
360
|
+
'abab6.5s-chat',
|
|
361
|
+
'abab6.5-chat',
|
|
362
|
+
'abab5.5-chat'
|
|
363
|
+
];
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* 验证AI提供商配置
|
|
367
|
+
*/
|
|
368
|
+
export function validateProviderConfig(
|
|
369
|
+
provider: AIProviderType,
|
|
370
|
+
config: {
|
|
371
|
+
apiKey?: string;
|
|
372
|
+
baseURL?: string;
|
|
373
|
+
model?: string;
|
|
374
|
+
}
|
|
375
|
+
): { valid: boolean; errors?: string[]; warnings?: string[] } {
|
|
376
|
+
const errors: string[] = [];
|
|
377
|
+
const warnings: string[] = [];
|
|
378
|
+
|
|
379
|
+
// 验证API密钥
|
|
380
|
+
if (!config.apiKey || config.apiKey.trim().length === 0) {
|
|
381
|
+
errors.push('API密钥不能为空');
|
|
382
|
+
} else {
|
|
383
|
+
const keyValidation = validateProviderApiKey(provider, config.apiKey);
|
|
384
|
+
if (!keyValidation.valid) {
|
|
385
|
+
errors.push(keyValidation.error || 'API密钥格式无效');
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
// 验证模型
|
|
390
|
+
if (config.model) {
|
|
391
|
+
const modelValidation = validateProviderModel(provider, config.model);
|
|
392
|
+
if (!modelValidation.valid) {
|
|
393
|
+
errors.push(modelValidation.error || '模型名称无效');
|
|
394
|
+
} else if (modelValidation.warning) {
|
|
395
|
+
warnings.push(modelValidation.warning);
|
|
396
|
+
}
|
|
397
|
+
} else {
|
|
398
|
+
warnings.push(`未指定模型,${provider}将使用默认模型`);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
// 验证基础URL(对于需要自定义URL的提供商)
|
|
402
|
+
if (needsCustomBaseURL(provider)) {
|
|
403
|
+
if (!config.baseURL || config.baseURL.trim().length === 0) {
|
|
404
|
+
warnings.push(`未指定基础URL,将使用${provider}的默认端点`);
|
|
405
|
+
} else {
|
|
406
|
+
const urlValidation = validateUrl(config.baseURL);
|
|
407
|
+
if (!urlValidation.valid) {
|
|
408
|
+
errors.push(`基础URL无效: ${urlValidation.error}`);
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
return {
|
|
414
|
+
valid: errors.length === 0,
|
|
415
|
+
errors: errors.length > 0 ? errors : undefined,
|
|
416
|
+
warnings: warnings.length > 0 ? warnings : undefined
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* 验证提供商API密钥格式
|
|
422
|
+
*/
|
|
423
|
+
export function validateProviderApiKey(
|
|
424
|
+
provider: AIProviderType,
|
|
425
|
+
apiKey: string
|
|
426
|
+
): { valid: boolean; error?: string } {
|
|
427
|
+
const trimmedKey = apiKey.trim();
|
|
428
|
+
|
|
429
|
+
switch (provider) {
|
|
430
|
+
case 'openai':
|
|
431
|
+
if (!trimmedKey.startsWith('sk-')) {
|
|
432
|
+
return { valid: false, error: 'OpenAI API密钥应以 sk- 开头' };
|
|
433
|
+
}
|
|
434
|
+
if (trimmedKey.length < 50) {
|
|
435
|
+
return { valid: false, error: 'OpenAI API密钥长度不足' };
|
|
436
|
+
}
|
|
437
|
+
break;
|
|
438
|
+
|
|
439
|
+
case 'anthropic':
|
|
440
|
+
if (!trimmedKey.startsWith('sk-ant-')) {
|
|
441
|
+
return { valid: false, error: 'Anthropic API密钥应以 sk-ant- 开头' };
|
|
442
|
+
}
|
|
443
|
+
if (trimmedKey.length < 50) {
|
|
444
|
+
return { valid: false, error: 'Anthropic API密钥长度不足' };
|
|
445
|
+
}
|
|
446
|
+
break;
|
|
447
|
+
|
|
448
|
+
case 'minimax':
|
|
449
|
+
if (trimmedKey.length < 20) {
|
|
450
|
+
return { valid: false, error: 'Minimax API密钥长度不足' };
|
|
451
|
+
}
|
|
452
|
+
// Minimax密钥通常以sk-开头
|
|
453
|
+
if (!trimmedKey.startsWith('sk-')) {
|
|
454
|
+
return { valid: false, error: 'Minimax API密钥应以 sk- 开头' };
|
|
455
|
+
}
|
|
456
|
+
break;
|
|
457
|
+
|
|
458
|
+
case 'glm':
|
|
459
|
+
if (trimmedKey.length < 20) {
|
|
460
|
+
return { valid: false, error: 'GLM API密钥长度不足' };
|
|
461
|
+
}
|
|
462
|
+
break;
|
|
463
|
+
|
|
464
|
+
case 'openai-compatible':
|
|
465
|
+
// 兼容模式不验证具体格式
|
|
466
|
+
if (trimmedKey.length < 10) {
|
|
467
|
+
return { valid: false, error: 'API密钥长度不足' };
|
|
468
|
+
}
|
|
469
|
+
break;
|
|
470
|
+
|
|
471
|
+
case 'ollama':
|
|
472
|
+
// Ollama本地服务通常不需要API密钥
|
|
473
|
+
break;
|
|
474
|
+
|
|
475
|
+
case 'vllm':
|
|
476
|
+
// vLLM可能有basic auth或无认证
|
|
477
|
+
break;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
return { valid: true };
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/**
|
|
484
|
+
* 验证提供商模型名称
|
|
485
|
+
*/
|
|
486
|
+
export function validateProviderModel(
|
|
487
|
+
provider: AIProviderType,
|
|
488
|
+
model: string
|
|
489
|
+
): { valid: boolean; error?: string; warning?: string } {
|
|
490
|
+
const trimmedModel = model.trim();
|
|
491
|
+
|
|
492
|
+
if (!trimmedModel) {
|
|
493
|
+
return { valid: false, error: '模型名称不能为空' };
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
// 检查模型名称格式
|
|
497
|
+
if (!/^[a-zA-Z0-9._-/]+$/.test(trimmedModel)) {
|
|
498
|
+
return { valid: false, error: '模型名称包含非法字符' };
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
// 检查是否在已知模型列表中
|
|
502
|
+
const isKnownModel = isKnownModelForProvider(provider, trimmedModel);
|
|
503
|
+
if (!isKnownModel) {
|
|
504
|
+
return {
|
|
505
|
+
valid: true,
|
|
506
|
+
warning: `模型 "${trimmedModel}" 不在${provider}的官方模型列表中`
|
|
507
|
+
};
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
return { valid: true };
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
/**
|
|
514
|
+
* 检查模型是否在提供商的已知模型列表中
|
|
515
|
+
*/
|
|
516
|
+
function isKnownModelForProvider(provider: AIProviderType, model: string): boolean {
|
|
517
|
+
const normalizedModel = model.toLowerCase();
|
|
518
|
+
|
|
519
|
+
switch (provider) {
|
|
520
|
+
case 'openai':
|
|
521
|
+
return OPENAI_MODELS.some(m => m.toLowerCase() === normalizedModel);
|
|
522
|
+
|
|
523
|
+
case 'anthropic':
|
|
524
|
+
return ANTHROPIC_MODELS.some(m => m.toLowerCase() === normalizedModel);
|
|
525
|
+
|
|
526
|
+
case 'glm':
|
|
527
|
+
return GLM_MODELS.some(m => m.toLowerCase() === normalizedModel);
|
|
528
|
+
|
|
529
|
+
case 'minimax':
|
|
530
|
+
return MINIMAX_MODELS.some(m => m.toLowerCase() === normalizedModel);
|
|
531
|
+
|
|
532
|
+
case 'openai-compatible':
|
|
533
|
+
case 'ollama':
|
|
534
|
+
case 'vllm':
|
|
535
|
+
// 这些提供商支持自定义模型名称
|
|
536
|
+
return true;
|
|
537
|
+
|
|
538
|
+
default:
|
|
539
|
+
return true;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
/**
|
|
544
|
+
* 检查提供商是否需要自定义基础URL
|
|
545
|
+
*/
|
|
546
|
+
export function needsCustomBaseURL(provider: AIProviderType): boolean {
|
|
547
|
+
return ['openai-compatible', 'ollama', 'vllm'].includes(provider);
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
/**
|
|
551
|
+
* 获取提供商的默认基础URL
|
|
552
|
+
*/
|
|
553
|
+
export function getProviderDefaultBaseURL(provider: AIProviderType): string {
|
|
554
|
+
switch (provider) {
|
|
555
|
+
case 'openai':
|
|
556
|
+
return 'https://api.openai.com/v1';
|
|
557
|
+
|
|
558
|
+
case 'anthropic':
|
|
559
|
+
return 'https://api.anthropic.com';
|
|
560
|
+
|
|
561
|
+
case 'minimax':
|
|
562
|
+
return 'https://api.minimax.chat';
|
|
563
|
+
|
|
564
|
+
case 'glm':
|
|
565
|
+
return 'https://open.bigmodel.cn/api/paas/v4';
|
|
566
|
+
|
|
567
|
+
case 'openai-compatible':
|
|
568
|
+
return '';
|
|
569
|
+
|
|
570
|
+
case 'ollama':
|
|
571
|
+
return 'http://localhost:11434';
|
|
572
|
+
|
|
573
|
+
case 'vllm':
|
|
574
|
+
return 'http://localhost:8000';
|
|
575
|
+
|
|
576
|
+
default:
|
|
577
|
+
return '';
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
/**
|
|
582
|
+
* 验证本地服务连接(用于Ollama、vLLM等本地提供商)
|
|
583
|
+
*/
|
|
584
|
+
export async function validateLocalServiceConnection(
|
|
585
|
+
baseURL: string,
|
|
586
|
+
timeout: number = 5000
|
|
587
|
+
): Promise<{ valid: boolean; error?: string; latency?: number }> {
|
|
588
|
+
const controller = new AbortController();
|
|
589
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
590
|
+
|
|
591
|
+
try {
|
|
592
|
+
const start = Date.now();
|
|
593
|
+
const response = await fetch(`${baseURL}/models`, {
|
|
594
|
+
method: 'GET',
|
|
595
|
+
signal: controller.signal
|
|
596
|
+
});
|
|
597
|
+
const latency = Date.now() - start;
|
|
598
|
+
|
|
599
|
+
clearTimeout(timeoutId);
|
|
600
|
+
|
|
601
|
+
if (response.ok) {
|
|
602
|
+
return { valid: true, latency };
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
return { valid: false, error: `服务返回状态码 ${response.status}` };
|
|
606
|
+
} catch (error) {
|
|
607
|
+
clearTimeout(timeoutId);
|
|
608
|
+
|
|
609
|
+
if (error instanceof Error) {
|
|
610
|
+
if (error.name === 'AbortError') {
|
|
611
|
+
return { valid: false, error: '连接超时' };
|
|
612
|
+
}
|
|
613
|
+
return { valid: false, error: error.message };
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
return { valid: false, error: '无法连接到服务' };
|
|
617
|
+
}
|
|
618
|
+
}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
/*! Axios v1.13.2 Copyright (c) 2025 Matt Zabriskie and contributors */
|
|
2
|
-
|
|
3
|
-
/** @preserve
|
|
4
|
-
* Counter block mode compatible with Dr Brian Gladman fileenc.c
|
|
5
|
-
* derived from CryptoJS.mode.CTR
|
|
6
|
-
* Jan Hruby jhruby.web@gmail.com
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
/** @preserve
|
|
10
|
-
(c) 2012 by Cédric Mesnil. All rights reserved.
|
|
11
|
-
|
|
12
|
-
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
13
|
-
|
|
14
|
-
- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
15
|
-
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
16
|
-
|
|
17
|
-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
18
|
-
*/
|