@weisiren000/oiiai 0.1.3 → 0.2.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.
- package/LICENSE +21 -0
- package/README.md +359 -27
- package/dist/index.d.mts +1762 -17
- package/dist/index.d.ts +1762 -17
- package/dist/index.js +3154 -797
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +3135 -789
- package/dist/index.mjs.map +1 -1
- package/package.json +10 -3
package/dist/index.d.mts
CHANGED
|
@@ -114,6 +114,57 @@ interface ModelInfo {
|
|
|
114
114
|
supportedParameters: string[];
|
|
115
115
|
}
|
|
116
116
|
|
|
117
|
+
/**
|
|
118
|
+
* 模型特性自动检测与智能降级策略
|
|
119
|
+
*
|
|
120
|
+
* 解决问题:
|
|
121
|
+
* 1. 思考模型(thinking models)在低 token 限制下 content 为空
|
|
122
|
+
* 2. 模型聚合平台无法预知哪些是思考模型
|
|
123
|
+
* 3. 避免硬编码模型列表
|
|
124
|
+
*
|
|
125
|
+
* 策略:
|
|
126
|
+
* 1. 基于模型名称模式匹配(启发式检测)
|
|
127
|
+
* 2. 基于响应特征动态检测(运行时检测)
|
|
128
|
+
* 3. 智能降级:自动调整参数或提取有效内容
|
|
129
|
+
*/
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* 模型行为类型
|
|
133
|
+
*/
|
|
134
|
+
type ModelBehavior = 'thinking-first' | 'direct-answer' | 'hybrid' | 'unknown';
|
|
135
|
+
/**
|
|
136
|
+
* 模型特性信息
|
|
137
|
+
*/
|
|
138
|
+
interface ModelCharacteristics {
|
|
139
|
+
/** 模型行为类型 */
|
|
140
|
+
behavior: ModelBehavior;
|
|
141
|
+
/** 是否支持 reasoning 参数 */
|
|
142
|
+
supportsReasoningConfig: boolean;
|
|
143
|
+
/** 推荐的最小 maxTokens(针对思考模型) */
|
|
144
|
+
recommendedMinTokens: number;
|
|
145
|
+
/** 检测置信度 0-1 */
|
|
146
|
+
confidence: number;
|
|
147
|
+
/** 检测来源 */
|
|
148
|
+
detectedBy: 'pattern' | 'runtime' | 'cache';
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* 降级策略配置
|
|
152
|
+
*/
|
|
153
|
+
interface FallbackConfig {
|
|
154
|
+
/** 是否启用自动降级 */
|
|
155
|
+
enabled: boolean;
|
|
156
|
+
/** 当 content 为空时,是否返回 reasoning */
|
|
157
|
+
returnReasoningAsContent: boolean;
|
|
158
|
+
/** 是否从 reasoning 中提取结论 */
|
|
159
|
+
extractConclusionFromReasoning: boolean;
|
|
160
|
+
/** 是否自动增加 maxTokens 重试 */
|
|
161
|
+
autoRetryWithMoreTokens: boolean;
|
|
162
|
+
/** 重试时的 token 增量 */
|
|
163
|
+
retryTokenIncrement: number;
|
|
164
|
+
/** 最大重试次数 */
|
|
165
|
+
maxRetries: number;
|
|
166
|
+
}
|
|
167
|
+
|
|
117
168
|
/**
|
|
118
169
|
* AI Provider 接口和基类
|
|
119
170
|
*/
|
|
@@ -145,42 +196,207 @@ interface AIProvider {
|
|
|
145
196
|
* 获取可用模型列表(可选实现)
|
|
146
197
|
*/
|
|
147
198
|
listModels?(): Promise<ModelInfo[]>;
|
|
199
|
+
/**
|
|
200
|
+
* 获取模型特性信息(可选实现)
|
|
201
|
+
*/
|
|
202
|
+
getModelCharacteristics?(modelId: string): ModelCharacteristics;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* 扩展的 ask 选项,支持降级配置
|
|
206
|
+
*/
|
|
207
|
+
interface AskOptions$1 extends Omit<ChatOptions, 'model' | 'messages'> {
|
|
208
|
+
/** 降级策略配置 */
|
|
209
|
+
fallback?: Partial<FallbackConfig>;
|
|
210
|
+
/** 是否自动调整参数以适应模型特性 */
|
|
211
|
+
autoAdjust?: boolean;
|
|
148
212
|
}
|
|
149
213
|
/**
|
|
150
214
|
* AI Provider 基础抽象类
|
|
151
215
|
* 提供一些通用实现,子类只需实现核心方法
|
|
216
|
+
*
|
|
217
|
+
* 特性:
|
|
218
|
+
* - 自动检测模型类型(思考模型 vs 直接回答模型)
|
|
219
|
+
* - 智能降级策略(content 为空时自动处理)
|
|
220
|
+
* - 参数自动调整(为思考模型增加 maxTokens)
|
|
152
221
|
*/
|
|
153
222
|
declare abstract class BaseProvider implements AIProvider {
|
|
154
223
|
abstract readonly name: string;
|
|
224
|
+
/** 降级策略配置 */
|
|
225
|
+
protected fallbackConfig: FallbackConfig;
|
|
226
|
+
/** 是否启用自动参数调整 */
|
|
227
|
+
protected autoAdjustEnabled: boolean;
|
|
155
228
|
abstract chat(options: ChatOptions): Promise<ChatResult>;
|
|
156
229
|
abstract chatStream(options: ChatOptions): AsyncGenerator<StreamChunk, void, unknown>;
|
|
230
|
+
/**
|
|
231
|
+
* 配置降级策略
|
|
232
|
+
*/
|
|
233
|
+
configureFallback(config: Partial<FallbackConfig>): this;
|
|
234
|
+
/**
|
|
235
|
+
* 启用/禁用自动参数调整
|
|
236
|
+
*/
|
|
237
|
+
setAutoAdjust(enabled: boolean): this;
|
|
238
|
+
/**
|
|
239
|
+
* 获取模型特性信息
|
|
240
|
+
*/
|
|
241
|
+
getModelCharacteristics(modelId: string): ModelCharacteristics;
|
|
242
|
+
/**
|
|
243
|
+
* 智能聊天:自动检测模型特性并应用降级策略
|
|
244
|
+
*/
|
|
245
|
+
chatSmart(options: ChatOptions): Promise<ChatResult>;
|
|
157
246
|
/**
|
|
158
247
|
* 简单对话:单轮问答(默认实现)
|
|
159
|
-
*
|
|
248
|
+
*
|
|
249
|
+
* 智能处理思考模型:
|
|
250
|
+
* 1. 自动检测模型类型
|
|
251
|
+
* 2. 为思考模型自动调整 maxTokens
|
|
252
|
+
* 3. 如果 content 为空,智能降级(提取结论或返回 reasoning)
|
|
160
253
|
*/
|
|
161
|
-
ask(model: string, question: string, options?:
|
|
254
|
+
ask(model: string, question: string, options?: AskOptions$1): Promise<string>;
|
|
162
255
|
/**
|
|
163
256
|
* 带系统提示的对话(默认实现)
|
|
164
|
-
*
|
|
257
|
+
*
|
|
258
|
+
* 智能处理思考模型:
|
|
259
|
+
* 1. 自动检测模型类型
|
|
260
|
+
* 2. 为思考模型自动调整 maxTokens
|
|
261
|
+
* 3. 如果 content 为空,智能降级(提取结论或返回 reasoning)
|
|
165
262
|
*/
|
|
166
|
-
askWithSystem(model: string, systemPrompt: string, userMessage: string, options?:
|
|
263
|
+
askWithSystem(model: string, systemPrompt: string, userMessage: string, options?: AskOptions$1): Promise<string>;
|
|
264
|
+
/**
|
|
265
|
+
* 场景化问答:根据场景自动配置参数
|
|
266
|
+
*
|
|
267
|
+
* @param model 模型 ID
|
|
268
|
+
* @param question 问题
|
|
269
|
+
* @param scenario 场景类型
|
|
270
|
+
* - 'simple': 简单问答(默认)
|
|
271
|
+
* - 'math': 数学计算
|
|
272
|
+
* - 'reasoning': 逻辑推理
|
|
273
|
+
* - 'fast': 快速回答(关闭思考)
|
|
274
|
+
*/
|
|
275
|
+
askWithScenario(model: string, question: string, scenario?: 'simple' | 'math' | 'reasoning' | 'fast', options?: AskOptions$1): Promise<string>;
|
|
167
276
|
}
|
|
168
277
|
|
|
169
278
|
/**
|
|
170
|
-
*
|
|
171
|
-
*
|
|
279
|
+
* 配置管理类型定义
|
|
280
|
+
* 定义统一的 Provider 配置格式和注册表配置
|
|
281
|
+
*/
|
|
282
|
+
/**
|
|
283
|
+
* Provider 类型
|
|
284
|
+
*/
|
|
285
|
+
type ProviderType$2 = 'openrouter' | 'gemini' | 'groq' | 'huggingface' | 'modelscope' | 'deepseek' | 'poe' | 'nova';
|
|
286
|
+
/**
|
|
287
|
+
* 统一的 Provider 配置格式
|
|
288
|
+
* 用于配置各种 AI Provider 的连接和行为
|
|
289
|
+
*/
|
|
290
|
+
interface UnifiedProviderConfig {
|
|
291
|
+
/** Provider 类型 */
|
|
292
|
+
provider: ProviderType$2;
|
|
293
|
+
/** 自定义适配器路径(可选) */
|
|
294
|
+
adapter?: string;
|
|
295
|
+
/** 凭证信息 */
|
|
296
|
+
credentials: {
|
|
297
|
+
/** API 密钥 */
|
|
298
|
+
apiKey: string;
|
|
299
|
+
/** 基础 URL(可选) */
|
|
300
|
+
baseUrl?: string;
|
|
301
|
+
};
|
|
302
|
+
/** 选项配置(可选) */
|
|
303
|
+
options?: {
|
|
304
|
+
/** 超时时间(毫秒) */
|
|
305
|
+
timeout?: number;
|
|
306
|
+
/** 重试次数 */
|
|
307
|
+
retries?: number;
|
|
308
|
+
/** 额外请求头 */
|
|
309
|
+
headers?: Record<string, string>;
|
|
310
|
+
};
|
|
311
|
+
/** 功能开关(可选) */
|
|
312
|
+
features?: {
|
|
313
|
+
/** 是否支持流式输出 */
|
|
314
|
+
streaming?: boolean;
|
|
315
|
+
/** 是否支持推理 */
|
|
316
|
+
reasoning?: boolean;
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* 注册表配置(用于配置文件)
|
|
321
|
+
* 支持从 JSON 配置文件加载多个 Provider
|
|
322
|
+
*/
|
|
323
|
+
interface RegistryConfig {
|
|
324
|
+
/** Provider 配置映射 */
|
|
325
|
+
providers: {
|
|
326
|
+
[key: string]: {
|
|
327
|
+
/** 自定义适配器路径(可选) */
|
|
328
|
+
adapter?: string;
|
|
329
|
+
/** Provider 配置 */
|
|
330
|
+
config: {
|
|
331
|
+
/** API 密钥 */
|
|
332
|
+
apiKey: string;
|
|
333
|
+
/** 基础 URL(可选) */
|
|
334
|
+
baseUrl?: string;
|
|
335
|
+
};
|
|
336
|
+
};
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* Provider 能力描述
|
|
341
|
+
* 描述 Provider 支持的功能特性
|
|
342
|
+
*/
|
|
343
|
+
interface ProviderCapabilities {
|
|
344
|
+
/** 是否支持流式输出 */
|
|
345
|
+
streaming: boolean;
|
|
346
|
+
/** 是否支持推理/思考 */
|
|
347
|
+
reasoning: boolean;
|
|
348
|
+
/** 是否支持函数调用 */
|
|
349
|
+
functionCalling: boolean;
|
|
350
|
+
/** 是否支持视觉/图像 */
|
|
351
|
+
vision: boolean;
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* 旧格式的 Provider 配置
|
|
355
|
+
* 用于向后兼容
|
|
356
|
+
*/
|
|
357
|
+
interface LegacyProviderConfig {
|
|
358
|
+
/** Provider 类型 */
|
|
359
|
+
provider: ProviderType$2;
|
|
360
|
+
/** API 密钥 */
|
|
361
|
+
apiKey: string;
|
|
362
|
+
/** 基础 URL(可选) */
|
|
363
|
+
baseUrl?: string;
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* 配置默认值
|
|
367
|
+
*/
|
|
368
|
+
declare const CONFIG_DEFAULTS: {
|
|
369
|
+
/** 默认超时时间(毫秒) */
|
|
370
|
+
readonly timeout: 30000;
|
|
371
|
+
/** 默认重试次数 */
|
|
372
|
+
readonly retries: 3;
|
|
373
|
+
/** 默认功能开关 */
|
|
374
|
+
readonly features: {
|
|
375
|
+
readonly streaming: true;
|
|
376
|
+
readonly reasoning: false;
|
|
377
|
+
};
|
|
378
|
+
};
|
|
379
|
+
/**
|
|
380
|
+
* 有效的 Provider 类型列表
|
|
172
381
|
*/
|
|
382
|
+
declare const VALID_PROVIDERS: ProviderType$2[];
|
|
173
383
|
|
|
174
384
|
/**
|
|
175
|
-
*
|
|
385
|
+
* 统一的 Provider 工厂
|
|
386
|
+
* 让所有 Provider 使用方式一致
|
|
387
|
+
*
|
|
388
|
+
* 重构后使用 ProviderRegistry 和 ConfigManager 实现:
|
|
389
|
+
* - 通过注册表获取适配器
|
|
390
|
+
* - 通过配置管理器处理配置
|
|
391
|
+
* - 保持与现有 API 的完全向后兼容性
|
|
176
392
|
*/
|
|
177
|
-
|
|
393
|
+
|
|
178
394
|
/**
|
|
179
|
-
* 统一的 Provider
|
|
395
|
+
* 统一的 Provider 配置(旧格式,保持向后兼容)
|
|
180
396
|
*/
|
|
181
397
|
interface ProviderConfig {
|
|
182
398
|
/** Provider 类型 */
|
|
183
|
-
provider: ProviderType;
|
|
399
|
+
provider: ProviderType$2;
|
|
184
400
|
/** API Key */
|
|
185
401
|
apiKey: string;
|
|
186
402
|
/** 自定义 API 地址(可选) */
|
|
@@ -189,6 +405,11 @@ interface ProviderConfig {
|
|
|
189
405
|
/**
|
|
190
406
|
* 创建 AI Provider 实例
|
|
191
407
|
*
|
|
408
|
+
* 重构后的实现:
|
|
409
|
+
* - 使用 ConfigManager 处理配置(支持旧格式自动转换)
|
|
410
|
+
* - 使用 ProviderRegistry 获取适配器
|
|
411
|
+
* - 返回基于适配器的 Provider 包装类
|
|
412
|
+
*
|
|
192
413
|
* @example
|
|
193
414
|
* ```ts
|
|
194
415
|
* // 所有 Provider 使用相同方式创建
|
|
@@ -204,6 +425,7 @@ interface ProviderConfig {
|
|
|
204
425
|
declare function createProvider(config: ProviderConfig): AIProvider;
|
|
205
426
|
/**
|
|
206
427
|
* 快捷创建函数
|
|
428
|
+
* 提供更简洁的 API 来创建各种 Provider
|
|
207
429
|
*/
|
|
208
430
|
declare const ai: {
|
|
209
431
|
openrouter: (apiKey: string, baseUrl?: string) => AIProvider;
|
|
@@ -212,10 +434,13 @@ declare const ai: {
|
|
|
212
434
|
huggingface: (apiKey: string, baseUrl?: string) => AIProvider;
|
|
213
435
|
modelscope: (apiKey: string, baseUrl?: string) => AIProvider;
|
|
214
436
|
deepseek: (apiKey: string, baseUrl?: string) => AIProvider;
|
|
437
|
+
poe: (apiKey: string, baseUrl?: string) => AIProvider;
|
|
438
|
+
nova: (apiKey: string, baseUrl?: string) => AIProvider;
|
|
215
439
|
};
|
|
216
440
|
|
|
217
441
|
/**
|
|
218
442
|
* OpenRouter Provider 实现
|
|
443
|
+
* 使用 OpenRouterAdapter 和 HttpProviderClient 进行重构
|
|
219
444
|
*/
|
|
220
445
|
|
|
221
446
|
/**
|
|
@@ -237,11 +462,15 @@ interface OpenRouterModelInfo extends ModelInfo {
|
|
|
237
462
|
}
|
|
238
463
|
/**
|
|
239
464
|
* OpenRouter Provider
|
|
465
|
+
* 使用适配器模式重构,复用统一的工具层
|
|
240
466
|
*/
|
|
241
467
|
declare class OpenRouterProvider extends BaseProvider {
|
|
242
468
|
readonly name = "openrouter";
|
|
469
|
+
private adapter;
|
|
243
470
|
private client;
|
|
244
|
-
|
|
471
|
+
private baseUrl;
|
|
472
|
+
private apiKey;
|
|
473
|
+
constructor(apiKey: string, baseUrl?: string);
|
|
245
474
|
/**
|
|
246
475
|
* 发送聊天请求(非流式)
|
|
247
476
|
*/
|
|
@@ -252,6 +481,7 @@ declare class OpenRouterProvider extends BaseProvider {
|
|
|
252
481
|
chatStream(options: ChatOptions): AsyncGenerator<StreamChunk, void, unknown>;
|
|
253
482
|
/**
|
|
254
483
|
* 获取可用模型列表
|
|
484
|
+
* 注意:此方法直接调用 OpenRouter API,不使用适配器
|
|
255
485
|
*/
|
|
256
486
|
listModels(): Promise<OpenRouterModelInfo[]>;
|
|
257
487
|
}
|
|
@@ -259,6 +489,7 @@ declare class OpenRouterProvider extends BaseProvider {
|
|
|
259
489
|
/**
|
|
260
490
|
* ModelScope Provider 实现
|
|
261
491
|
* 使用 OpenAI 兼容 API
|
|
492
|
+
* 使用 ModelScopeAdapter 和 HttpProviderClient 进行重构
|
|
262
493
|
*/
|
|
263
494
|
|
|
264
495
|
/**
|
|
@@ -271,10 +502,12 @@ interface ModelScopeConfig {
|
|
|
271
502
|
/**
|
|
272
503
|
* ModelScope Provider
|
|
273
504
|
* 兼容 OpenAI API 格式,支持 DeepSeek 等模型
|
|
505
|
+
* 使用适配器模式重构,复用统一的工具层
|
|
274
506
|
*/
|
|
275
507
|
declare class ModelScopeProvider extends BaseProvider {
|
|
276
508
|
readonly name = "modelscope";
|
|
277
|
-
private
|
|
509
|
+
private adapter;
|
|
510
|
+
private client;
|
|
278
511
|
private baseUrl;
|
|
279
512
|
constructor(config: ModelScopeConfig | string);
|
|
280
513
|
/**
|
|
@@ -290,6 +523,7 @@ declare class ModelScopeProvider extends BaseProvider {
|
|
|
290
523
|
/**
|
|
291
524
|
* HuggingFace Provider 实现
|
|
292
525
|
* 使用 HuggingFace Router API (OpenAI 兼容)
|
|
526
|
+
* 使用 HuggingFaceAdapter 和 HttpProviderClient 进行重构
|
|
293
527
|
*/
|
|
294
528
|
|
|
295
529
|
/**
|
|
@@ -302,14 +536,20 @@ interface HuggingFaceConfig {
|
|
|
302
536
|
/**
|
|
303
537
|
* HuggingFace Provider
|
|
304
538
|
* 使用 HuggingFace Router,兼容 OpenAI API 格式
|
|
539
|
+
* 使用适配器模式重构,复用统一的工具层
|
|
305
540
|
*/
|
|
306
541
|
declare class HuggingFaceProvider extends BaseProvider {
|
|
307
542
|
readonly name = "huggingface";
|
|
308
|
-
private
|
|
543
|
+
private adapter;
|
|
544
|
+
private client;
|
|
309
545
|
private baseUrl;
|
|
310
546
|
constructor(config: HuggingFaceConfig | string);
|
|
311
547
|
/**
|
|
312
548
|
* 发送聊天请求(非流式)
|
|
549
|
+
*
|
|
550
|
+
* reasoning 参数说明:
|
|
551
|
+
* - HuggingFace 是模型聚合平台,thinking 支持取决于具体模型
|
|
552
|
+
* - 如果模型支持,会返回 reasoning_content
|
|
313
553
|
*/
|
|
314
554
|
chat(options: ChatOptions): Promise<ChatResult>;
|
|
315
555
|
/**
|
|
@@ -321,6 +561,7 @@ declare class HuggingFaceProvider extends BaseProvider {
|
|
|
321
561
|
/**
|
|
322
562
|
* Groq Provider 实现
|
|
323
563
|
* 使用 Groq API (OpenAI 兼容)
|
|
564
|
+
* 使用 GroqAdapter 和 HttpProviderClient 进行重构
|
|
324
565
|
*/
|
|
325
566
|
|
|
326
567
|
/**
|
|
@@ -332,11 +573,13 @@ interface GroqConfig {
|
|
|
332
573
|
}
|
|
333
574
|
/**
|
|
334
575
|
* Groq Provider
|
|
335
|
-
* 兼容 OpenAI API 格式,支持
|
|
576
|
+
* 兼容 OpenAI API 格式,支持 reasoning_format 参数
|
|
577
|
+
* 使用适配器模式重构,复用统一的工具层
|
|
336
578
|
*/
|
|
337
579
|
declare class GroqProvider extends BaseProvider {
|
|
338
580
|
readonly name = "groq";
|
|
339
|
-
private
|
|
581
|
+
private adapter;
|
|
582
|
+
private client;
|
|
340
583
|
private baseUrl;
|
|
341
584
|
constructor(config: GroqConfig | string);
|
|
342
585
|
/**
|
|
@@ -352,6 +595,7 @@ declare class GroqProvider extends BaseProvider {
|
|
|
352
595
|
/**
|
|
353
596
|
* Gemini Provider 实现
|
|
354
597
|
* 使用 Google Gemini API (OpenAI 兼容格式)
|
|
598
|
+
* 使用 GeminiAdapter 和 HttpProviderClient 进行重构
|
|
355
599
|
* https://ai.google.dev/gemini-api/docs/openai
|
|
356
600
|
*/
|
|
357
601
|
|
|
@@ -365,10 +609,12 @@ interface GeminiConfig {
|
|
|
365
609
|
/**
|
|
366
610
|
* Gemini Provider
|
|
367
611
|
* 使用 Google Gemini API,兼容 OpenAI 格式
|
|
612
|
+
* 使用适配器模式重构,复用统一的工具层
|
|
368
613
|
*/
|
|
369
614
|
declare class GeminiProvider extends BaseProvider {
|
|
370
615
|
readonly name = "gemini";
|
|
371
|
-
private
|
|
616
|
+
private adapter;
|
|
617
|
+
private client;
|
|
372
618
|
private baseUrl;
|
|
373
619
|
constructor(config: GeminiConfig | string);
|
|
374
620
|
/**
|
|
@@ -381,4 +627,1503 @@ declare class GeminiProvider extends BaseProvider {
|
|
|
381
627
|
chatStream(options: ChatOptions): AsyncGenerator<StreamChunk, void, unknown>;
|
|
382
628
|
}
|
|
383
629
|
|
|
384
|
-
|
|
630
|
+
/**
|
|
631
|
+
* 流处理器 - 处理 SSE 流式响应
|
|
632
|
+
* 从各 Provider 中提取并统一的流处理逻辑
|
|
633
|
+
*/
|
|
634
|
+
|
|
635
|
+
/**
|
|
636
|
+
* Delta 提取器函数类型
|
|
637
|
+
* 从流式响应的 delta 中提取 StreamChunk
|
|
638
|
+
*/
|
|
639
|
+
type DeltaExtractor = (delta: Record<string, unknown>) => StreamChunk | null;
|
|
640
|
+
/**
|
|
641
|
+
* 流处理器类
|
|
642
|
+
* 提供统一的 SSE 流式响应处理功能
|
|
643
|
+
*/
|
|
644
|
+
declare class StreamProcessor {
|
|
645
|
+
/**
|
|
646
|
+
* 从响应内容中提取文本
|
|
647
|
+
* 支持字符串和数组格式的 content
|
|
648
|
+
*
|
|
649
|
+
* @param content - 响应内容,可以是字符串、数组或其他类型
|
|
650
|
+
* @returns 提取的文本内容
|
|
651
|
+
*
|
|
652
|
+
* @example
|
|
653
|
+
* ```ts
|
|
654
|
+
* // 字符串格式
|
|
655
|
+
* StreamProcessor.extractTextContent('Hello') // => 'Hello'
|
|
656
|
+
*
|
|
657
|
+
* // 数组格式
|
|
658
|
+
* StreamProcessor.extractTextContent([
|
|
659
|
+
* { type: 'text', text: 'Hello' },
|
|
660
|
+
* { type: 'text', text: ' World' }
|
|
661
|
+
* ]) // => 'Hello World'
|
|
662
|
+
* ```
|
|
663
|
+
*/
|
|
664
|
+
static extractTextContent(content: unknown): string;
|
|
665
|
+
/**
|
|
666
|
+
* 解析 SSE 数据行
|
|
667
|
+
*
|
|
668
|
+
* @param line - SSE 数据行(如 "data: {...}")
|
|
669
|
+
* @returns 解析后的 JSON 对象,或 null(如果是 [DONE] 或无效数据)
|
|
670
|
+
*
|
|
671
|
+
* @example
|
|
672
|
+
* ```ts
|
|
673
|
+
* StreamProcessor.parseSSELine('data: {"content": "Hello"}')
|
|
674
|
+
* // => { content: 'Hello' }
|
|
675
|
+
*
|
|
676
|
+
* StreamProcessor.parseSSELine('data: [DONE]')
|
|
677
|
+
* // => null
|
|
678
|
+
* ```
|
|
679
|
+
*/
|
|
680
|
+
static parseSSELine(line: string): Record<string, unknown> | null;
|
|
681
|
+
/**
|
|
682
|
+
* 创建流式响应处理器
|
|
683
|
+
* 处理 SSE 格式的流式响应,提取并生成 StreamChunk
|
|
684
|
+
*
|
|
685
|
+
* @param response - fetch Response 对象
|
|
686
|
+
* @param deltaExtractor - 从 delta 中提取 StreamChunk 的函数
|
|
687
|
+
* @returns AsyncGenerator<StreamChunk>
|
|
688
|
+
*
|
|
689
|
+
* @example
|
|
690
|
+
* ```ts
|
|
691
|
+
* const response = await fetch(url, { ... });
|
|
692
|
+
* const extractor = (delta) => {
|
|
693
|
+
* if (delta.content) {
|
|
694
|
+
* return { type: 'content', text: delta.content };
|
|
695
|
+
* }
|
|
696
|
+
* return null;
|
|
697
|
+
* };
|
|
698
|
+
*
|
|
699
|
+
* for await (const chunk of StreamProcessor.processStream(response, extractor)) {
|
|
700
|
+
* console.log(chunk.type, chunk.text);
|
|
701
|
+
* }
|
|
702
|
+
* ```
|
|
703
|
+
*/
|
|
704
|
+
static processStream(response: Response, deltaExtractor: DeltaExtractor): AsyncGenerator<StreamChunk, void, unknown>;
|
|
705
|
+
/**
|
|
706
|
+
* 创建默认的 delta 提取器
|
|
707
|
+
* 支持 reasoning_content、reasoning、thoughts 和 content 字段
|
|
708
|
+
*
|
|
709
|
+
* @returns DeltaExtractor 函数
|
|
710
|
+
*/
|
|
711
|
+
static createDefaultExtractor(): DeltaExtractor;
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
/**
|
|
715
|
+
* 请求构建器 - 构建标准化的 API 请求
|
|
716
|
+
* 统一各 Provider 的请求参数构建逻辑
|
|
717
|
+
*/
|
|
718
|
+
|
|
719
|
+
/**
|
|
720
|
+
* 请求构建器类
|
|
721
|
+
* 提供统一的 API 请求参数构建功能
|
|
722
|
+
*/
|
|
723
|
+
declare class RequestBuilder {
|
|
724
|
+
/**
|
|
725
|
+
* 构建聊天请求的基础参数
|
|
726
|
+
* 生成标准化的 OpenAI 兼容格式请求体
|
|
727
|
+
*
|
|
728
|
+
* @param options - 聊天选项
|
|
729
|
+
* @param stream - 是否为流式请求
|
|
730
|
+
* @returns 请求体对象
|
|
731
|
+
*
|
|
732
|
+
* @example
|
|
733
|
+
* ```ts
|
|
734
|
+
* const body = RequestBuilder.buildChatBody({
|
|
735
|
+
* model: 'gpt-4',
|
|
736
|
+
* messages: [{ role: 'user', content: 'Hello' }],
|
|
737
|
+
* temperature: 0.7
|
|
738
|
+
* });
|
|
739
|
+
* ```
|
|
740
|
+
*/
|
|
741
|
+
static buildChatBody(options: ChatOptions, stream?: boolean): Record<string, unknown>;
|
|
742
|
+
/**
|
|
743
|
+
* 构建 OpenRouter 格式的 reasoning 参数
|
|
744
|
+
*
|
|
745
|
+
* @param config - 推理配置
|
|
746
|
+
* @returns OpenRouter 格式的 reasoning 参数,或 undefined
|
|
747
|
+
*
|
|
748
|
+
* @example
|
|
749
|
+
* ```ts
|
|
750
|
+
* const reasoning = RequestBuilder.buildOpenRouterReasoning({ effort: 'high' });
|
|
751
|
+
* // => { effort: 'high', max_tokens: 16384 }
|
|
752
|
+
* ```
|
|
753
|
+
*/
|
|
754
|
+
static buildOpenRouterReasoning(config?: ReasoningConfig): Record<string, unknown> | undefined;
|
|
755
|
+
/**
|
|
756
|
+
* 构建 Gemini 格式的 reasoning 参数
|
|
757
|
+
* Gemini 2.5+ 模型使用 reasoning_effort 控制思考
|
|
758
|
+
*
|
|
759
|
+
* @param config - 推理配置
|
|
760
|
+
* @returns Gemini 格式的参数对象
|
|
761
|
+
*
|
|
762
|
+
* @example
|
|
763
|
+
* ```ts
|
|
764
|
+
* const params = RequestBuilder.buildGeminiReasoning({ effort: 'high' });
|
|
765
|
+
* // => { reasoning_effort: 'high' }
|
|
766
|
+
* ```
|
|
767
|
+
*/
|
|
768
|
+
static buildGeminiReasoning(config?: ReasoningConfig): Record<string, unknown>;
|
|
769
|
+
/**
|
|
770
|
+
* 构建 Groq 格式的 reasoning 参数
|
|
771
|
+
* Groq 使用 reasoning_format 参数控制推理输出
|
|
772
|
+
*
|
|
773
|
+
* @param config - 推理配置
|
|
774
|
+
* @returns Groq 格式的参数对象
|
|
775
|
+
*
|
|
776
|
+
* @example
|
|
777
|
+
* ```ts
|
|
778
|
+
* const params = RequestBuilder.buildGroqReasoning({ effort: 'high' });
|
|
779
|
+
* // => { reasoning_format: 'parsed' }
|
|
780
|
+
* ```
|
|
781
|
+
*/
|
|
782
|
+
static buildGroqReasoning(config?: ReasoningConfig): Record<string, unknown>;
|
|
783
|
+
/**
|
|
784
|
+
* 构建 DeepSeek 格式的 reasoning 参数
|
|
785
|
+
* DeepSeek 使用 thinking 参数启用思考模式
|
|
786
|
+
*
|
|
787
|
+
* @param config - 推理配置
|
|
788
|
+
* @returns DeepSeek 格式的参数对象
|
|
789
|
+
*/
|
|
790
|
+
static buildDeepSeekReasoning(config?: ReasoningConfig): Record<string, unknown>;
|
|
791
|
+
/**
|
|
792
|
+
* 构建 Nova 格式的 reasoning 参数
|
|
793
|
+
* Nova 使用 reasoningConfig 控制 extended thinking
|
|
794
|
+
*
|
|
795
|
+
* @param config - 推理配置
|
|
796
|
+
* @returns Nova 格式的参数对象
|
|
797
|
+
*/
|
|
798
|
+
static buildNovaReasoning(config?: ReasoningConfig): Record<string, unknown>;
|
|
799
|
+
/**
|
|
800
|
+
* 构建 HTTP 请求头
|
|
801
|
+
*
|
|
802
|
+
* @param apiKey - API 密钥
|
|
803
|
+
* @param additionalHeaders - 额外的请求头
|
|
804
|
+
* @returns 请求头对象
|
|
805
|
+
*
|
|
806
|
+
* @example
|
|
807
|
+
* ```ts
|
|
808
|
+
* const headers = RequestBuilder.buildHeaders('sk-xxx', {
|
|
809
|
+
* 'X-Custom-Header': 'value'
|
|
810
|
+
* });
|
|
811
|
+
* ```
|
|
812
|
+
*/
|
|
813
|
+
static buildHeaders(apiKey: string, additionalHeaders?: Record<string, string>): Record<string, string>;
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
/**
|
|
817
|
+
* 配置验证器 - 验证 Provider 配置的有效性
|
|
818
|
+
*/
|
|
819
|
+
/**
|
|
820
|
+
* 验证错误
|
|
821
|
+
*/
|
|
822
|
+
interface ValidationError$1 {
|
|
823
|
+
/** 错误字段路径 */
|
|
824
|
+
field: string;
|
|
825
|
+
/** 错误消息 */
|
|
826
|
+
message: string;
|
|
827
|
+
/** 错误代码 */
|
|
828
|
+
code: string;
|
|
829
|
+
}
|
|
830
|
+
/**
|
|
831
|
+
* 验证结果
|
|
832
|
+
*/
|
|
833
|
+
interface ValidationResult {
|
|
834
|
+
/** 是否验证通过 */
|
|
835
|
+
valid: boolean;
|
|
836
|
+
/** 错误列表 */
|
|
837
|
+
errors: ValidationError$1[];
|
|
838
|
+
}
|
|
839
|
+
/**
|
|
840
|
+
* Provider 类型
|
|
841
|
+
*/
|
|
842
|
+
type ProviderType$1 = 'openrouter' | 'gemini' | 'groq' | 'huggingface' | 'modelscope' | 'deepseek' | 'poe' | 'nova';
|
|
843
|
+
/**
|
|
844
|
+
* 配置验证器类
|
|
845
|
+
* 提供 Provider 配置验证功能
|
|
846
|
+
*/
|
|
847
|
+
declare class ConfigValidator {
|
|
848
|
+
/**
|
|
849
|
+
* 验证 Provider 配置
|
|
850
|
+
*
|
|
851
|
+
* @param config - 要验证的配置对象
|
|
852
|
+
* @returns 验证结果
|
|
853
|
+
*
|
|
854
|
+
* @example
|
|
855
|
+
* ```ts
|
|
856
|
+
* const result = ConfigValidator.validate({
|
|
857
|
+
* provider: 'openrouter',
|
|
858
|
+
* credentials: { apiKey: 'sk-xxx' }
|
|
859
|
+
* });
|
|
860
|
+
*
|
|
861
|
+
* if (!result.valid) {
|
|
862
|
+
* console.error(result.errors);
|
|
863
|
+
* }
|
|
864
|
+
* ```
|
|
865
|
+
*/
|
|
866
|
+
static validate(config: unknown): ValidationResult;
|
|
867
|
+
/**
|
|
868
|
+
* 验证 API Key 格式
|
|
869
|
+
* 不同 Provider 可能有不同的 API Key 格式要求
|
|
870
|
+
*
|
|
871
|
+
* @param apiKey - API 密钥
|
|
872
|
+
* @param provider - Provider 类型
|
|
873
|
+
* @returns 验证结果
|
|
874
|
+
*/
|
|
875
|
+
static validateApiKey(apiKey: string, provider: ProviderType$1): ValidationResult;
|
|
876
|
+
/**
|
|
877
|
+
* 验证 URL 格式
|
|
878
|
+
*
|
|
879
|
+
* @param url - 要验证的 URL
|
|
880
|
+
* @returns 验证结果
|
|
881
|
+
*/
|
|
882
|
+
static validateUrl(url: unknown): ValidationResult;
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
/**
|
|
886
|
+
* 配置管理器
|
|
887
|
+
* 提供配置验证、默认值应用、环境变量替换等功能
|
|
888
|
+
*/
|
|
889
|
+
|
|
890
|
+
/**
|
|
891
|
+
* 配置管理器类
|
|
892
|
+
* 统一管理 Provider 配置的验证、转换和处理
|
|
893
|
+
*/
|
|
894
|
+
declare class ConfigManager {
|
|
895
|
+
/**
|
|
896
|
+
* 验证配置
|
|
897
|
+
* 检查配置是否符合 UnifiedProviderConfig 格式要求
|
|
898
|
+
*
|
|
899
|
+
* @param config - 要验证的配置对象
|
|
900
|
+
* @returns 验证结果
|
|
901
|
+
*
|
|
902
|
+
* @example
|
|
903
|
+
* ```ts
|
|
904
|
+
* const result = ConfigManager.validate({
|
|
905
|
+
* provider: 'openrouter',
|
|
906
|
+
* credentials: { apiKey: 'sk-xxx' }
|
|
907
|
+
* });
|
|
908
|
+
*
|
|
909
|
+
* if (!result.valid) {
|
|
910
|
+
* console.error(result.errors);
|
|
911
|
+
* }
|
|
912
|
+
* ```
|
|
913
|
+
*/
|
|
914
|
+
static validate(config: unknown): ValidationResult;
|
|
915
|
+
/**
|
|
916
|
+
* 应用默认值
|
|
917
|
+
* 为缺失的可选字段填充默认值
|
|
918
|
+
*
|
|
919
|
+
* @param config - 部分配置对象
|
|
920
|
+
* @returns 填充默认值后的完整配置
|
|
921
|
+
*
|
|
922
|
+
* @example
|
|
923
|
+
* ```ts
|
|
924
|
+
* const fullConfig = ConfigManager.applyDefaults({
|
|
925
|
+
* provider: 'openrouter',
|
|
926
|
+
* credentials: { apiKey: 'sk-xxx' }
|
|
927
|
+
* });
|
|
928
|
+
* // fullConfig.options.timeout === 30000
|
|
929
|
+
* // fullConfig.options.retries === 3
|
|
930
|
+
* ```
|
|
931
|
+
*/
|
|
932
|
+
static applyDefaults(config: Partial<UnifiedProviderConfig>): UnifiedProviderConfig;
|
|
933
|
+
/**
|
|
934
|
+
* 合并环境变量
|
|
935
|
+
* 将 ${ENV_VAR} 格式的占位符替换为实际环境变量值
|
|
936
|
+
*
|
|
937
|
+
* @param config - 包含环境变量占位符的配置
|
|
938
|
+
* @returns 替换后的配置
|
|
939
|
+
*
|
|
940
|
+
* @example
|
|
941
|
+
* ```ts
|
|
942
|
+
* // 假设 process.env.OPENROUTER_API_KEY = 'sk-xxx'
|
|
943
|
+
* const config = ConfigManager.mergeWithEnv({
|
|
944
|
+
* provider: 'openrouter',
|
|
945
|
+
* credentials: { apiKey: '${OPENROUTER_API_KEY}' }
|
|
946
|
+
* });
|
|
947
|
+
* // config.credentials.apiKey === 'sk-xxx'
|
|
948
|
+
* ```
|
|
949
|
+
*/
|
|
950
|
+
static mergeWithEnv(config: UnifiedProviderConfig): UnifiedProviderConfig;
|
|
951
|
+
/**
|
|
952
|
+
* 替换字符串中的环境变量占位符
|
|
953
|
+
* 支持 ${ENV_VAR} 格式
|
|
954
|
+
*
|
|
955
|
+
* @param str - 包含占位符的字符串
|
|
956
|
+
* @returns 替换后的字符串
|
|
957
|
+
*/
|
|
958
|
+
private static replaceEnvPlaceholders;
|
|
959
|
+
/**
|
|
960
|
+
* 从旧格式配置转换为新格式
|
|
961
|
+
* 保持向后兼容性
|
|
962
|
+
*
|
|
963
|
+
* @param config - 旧格式的 Provider 配置
|
|
964
|
+
* @returns 新格式的统一配置
|
|
965
|
+
*
|
|
966
|
+
* @example
|
|
967
|
+
* ```ts
|
|
968
|
+
* const newConfig = ConfigManager.fromLegacyConfig({
|
|
969
|
+
* provider: 'openrouter',
|
|
970
|
+
* apiKey: 'sk-xxx',
|
|
971
|
+
* baseUrl: 'https://api.example.com'
|
|
972
|
+
* });
|
|
973
|
+
* // newConfig.credentials.apiKey === 'sk-xxx'
|
|
974
|
+
* // newConfig.credentials.baseUrl === 'https://api.example.com'
|
|
975
|
+
* ```
|
|
976
|
+
*/
|
|
977
|
+
static fromLegacyConfig(config: LegacyProviderConfig): UnifiedProviderConfig;
|
|
978
|
+
/**
|
|
979
|
+
* 检查配置是否为旧格式
|
|
980
|
+
*
|
|
981
|
+
* @param config - 要检查的配置对象
|
|
982
|
+
* @returns 是否为旧格式
|
|
983
|
+
*/
|
|
984
|
+
static isLegacyConfig(config: unknown): config is LegacyProviderConfig;
|
|
985
|
+
/**
|
|
986
|
+
* 智能转换配置
|
|
987
|
+
* 自动检测配置格式并转换为统一格式
|
|
988
|
+
*
|
|
989
|
+
* @param config - 任意格式的配置
|
|
990
|
+
* @returns 统一格式的配置
|
|
991
|
+
*/
|
|
992
|
+
static normalize(config: LegacyProviderConfig | Partial<UnifiedProviderConfig>): UnifiedProviderConfig;
|
|
993
|
+
/**
|
|
994
|
+
* 获取指定 Provider 的默认基础 URL
|
|
995
|
+
*
|
|
996
|
+
* @param provider - Provider 类型
|
|
997
|
+
* @returns 默认基础 URL
|
|
998
|
+
*/
|
|
999
|
+
static getDefaultBaseUrl(provider: ProviderType$2): string;
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
/**
|
|
1003
|
+
* Provider 客户端层类型定义
|
|
1004
|
+
* 定义执行 HTTP 请求的客户端接口
|
|
1005
|
+
*/
|
|
1006
|
+
/**
|
|
1007
|
+
* Provider 客户端配置
|
|
1008
|
+
*/
|
|
1009
|
+
interface ProviderClientConfig {
|
|
1010
|
+
/** API 密钥 */
|
|
1011
|
+
apiKey: string;
|
|
1012
|
+
/** 基础 URL */
|
|
1013
|
+
baseUrl: string;
|
|
1014
|
+
/** 请求超时时间(毫秒) */
|
|
1015
|
+
timeout?: number;
|
|
1016
|
+
/** 额外的请求头 */
|
|
1017
|
+
headers?: Record<string, string>;
|
|
1018
|
+
}
|
|
1019
|
+
/**
|
|
1020
|
+
* Provider 客户端接口
|
|
1021
|
+
* 定义执行 HTTP 请求的统一接口
|
|
1022
|
+
*/
|
|
1023
|
+
interface ProviderClient {
|
|
1024
|
+
/**
|
|
1025
|
+
* 发送聊天请求(非流式)
|
|
1026
|
+
*
|
|
1027
|
+
* @param endpoint - API 端点路径
|
|
1028
|
+
* @param body - 请求体
|
|
1029
|
+
* @returns 响应数据
|
|
1030
|
+
* @throws ProviderError 当请求失败时
|
|
1031
|
+
*/
|
|
1032
|
+
chat(endpoint: string, body: Record<string, unknown>): Promise<Record<string, unknown>>;
|
|
1033
|
+
/**
|
|
1034
|
+
* 发送流式聊天请求
|
|
1035
|
+
*
|
|
1036
|
+
* @param endpoint - API 端点路径
|
|
1037
|
+
* @param body - 请求体
|
|
1038
|
+
* @returns fetch Response 对象,用于流式处理
|
|
1039
|
+
* @throws ProviderError 当请求失败时
|
|
1040
|
+
*/
|
|
1041
|
+
chatStream(endpoint: string, body: Record<string, unknown>): Promise<Response>;
|
|
1042
|
+
}
|
|
1043
|
+
/**
|
|
1044
|
+
* Provider 错误基类
|
|
1045
|
+
*/
|
|
1046
|
+
declare class ProviderError extends Error {
|
|
1047
|
+
readonly code: string;
|
|
1048
|
+
readonly provider: string;
|
|
1049
|
+
readonly cause?: Error | undefined;
|
|
1050
|
+
constructor(message: string, code: string, provider: string, cause?: Error | undefined);
|
|
1051
|
+
}
|
|
1052
|
+
/**
|
|
1053
|
+
* API 错误
|
|
1054
|
+
* 用于 HTTP 请求失败的情况
|
|
1055
|
+
*/
|
|
1056
|
+
declare class APIError extends ProviderError {
|
|
1057
|
+
readonly statusCode: number;
|
|
1058
|
+
readonly responseBody?: string | undefined;
|
|
1059
|
+
constructor(message: string, provider: string, statusCode: number, responseBody?: string | undefined);
|
|
1060
|
+
}
|
|
1061
|
+
/**
|
|
1062
|
+
* 网络错误
|
|
1063
|
+
* 用于网络连接失败的情况
|
|
1064
|
+
*/
|
|
1065
|
+
declare class NetworkError extends ProviderError {
|
|
1066
|
+
constructor(message: string, provider: string, cause?: Error);
|
|
1067
|
+
}
|
|
1068
|
+
/**
|
|
1069
|
+
* 超时错误
|
|
1070
|
+
* 用于请求超时的情况
|
|
1071
|
+
*/
|
|
1072
|
+
declare class TimeoutError extends ProviderError {
|
|
1073
|
+
readonly timeoutMs: number;
|
|
1074
|
+
constructor(message: string, provider: string, timeoutMs: number);
|
|
1075
|
+
}
|
|
1076
|
+
|
|
1077
|
+
/**
|
|
1078
|
+
* HTTP Provider 客户端实现
|
|
1079
|
+
* 提供基于 fetch 的 HTTP 请求功能
|
|
1080
|
+
*/
|
|
1081
|
+
|
|
1082
|
+
/**
|
|
1083
|
+
* HTTP Provider 客户端
|
|
1084
|
+
* 实现 ProviderClient 接口,提供统一的 HTTP 请求功能
|
|
1085
|
+
*/
|
|
1086
|
+
declare class HttpProviderClient implements ProviderClient {
|
|
1087
|
+
private readonly config;
|
|
1088
|
+
/**
|
|
1089
|
+
* 创建 HTTP Provider 客户端实例
|
|
1090
|
+
*
|
|
1091
|
+
* @param config - 客户端配置
|
|
1092
|
+
*
|
|
1093
|
+
* @example
|
|
1094
|
+
* ```ts
|
|
1095
|
+
* const client = new HttpProviderClient({
|
|
1096
|
+
* apiKey: 'sk-xxx',
|
|
1097
|
+
* baseUrl: 'https://api.openai.com/v1',
|
|
1098
|
+
* timeout: 60000
|
|
1099
|
+
* });
|
|
1100
|
+
* ```
|
|
1101
|
+
*/
|
|
1102
|
+
constructor(config: ProviderClientConfig);
|
|
1103
|
+
/**
|
|
1104
|
+
* 获取 Provider 名称(从 baseUrl 推断)
|
|
1105
|
+
*/
|
|
1106
|
+
private getProviderName;
|
|
1107
|
+
/**
|
|
1108
|
+
* 构建完整的请求 URL
|
|
1109
|
+
*/
|
|
1110
|
+
private buildUrl;
|
|
1111
|
+
/**
|
|
1112
|
+
* 构建请求头
|
|
1113
|
+
*/
|
|
1114
|
+
private buildHeaders;
|
|
1115
|
+
/**
|
|
1116
|
+
* 创建带超时的 AbortController
|
|
1117
|
+
*/
|
|
1118
|
+
private createAbortController;
|
|
1119
|
+
/**
|
|
1120
|
+
* 处理 HTTP 错误响应
|
|
1121
|
+
*/
|
|
1122
|
+
private handleErrorResponse;
|
|
1123
|
+
/**
|
|
1124
|
+
* 发送聊天请求(非流式)
|
|
1125
|
+
*
|
|
1126
|
+
* @param endpoint - API 端点路径
|
|
1127
|
+
* @param body - 请求体
|
|
1128
|
+
* @returns 响应数据
|
|
1129
|
+
*/
|
|
1130
|
+
chat(endpoint: string, body: Record<string, unknown>): Promise<Record<string, unknown>>;
|
|
1131
|
+
/**
|
|
1132
|
+
* 发送流式聊天请求
|
|
1133
|
+
*
|
|
1134
|
+
* @param endpoint - API 端点路径
|
|
1135
|
+
* @param body - 请求体
|
|
1136
|
+
* @returns fetch Response 对象
|
|
1137
|
+
*/
|
|
1138
|
+
chatStream(endpoint: string, body: Record<string, unknown>): Promise<Response>;
|
|
1139
|
+
}
|
|
1140
|
+
|
|
1141
|
+
/**
|
|
1142
|
+
* Provider 适配器层类型定义
|
|
1143
|
+
* 定义所有 Provider 适配器必须实现的接口
|
|
1144
|
+
*/
|
|
1145
|
+
|
|
1146
|
+
/**
|
|
1147
|
+
* Provider 类型枚举
|
|
1148
|
+
* 支持的所有 Provider 类型
|
|
1149
|
+
*/
|
|
1150
|
+
type ProviderType = 'openrouter' | 'gemini' | 'groq' | 'huggingface' | 'modelscope' | 'deepseek' | 'poe' | 'nova';
|
|
1151
|
+
/**
|
|
1152
|
+
* 适配器配置
|
|
1153
|
+
* 用于创建适配器实例时的配置
|
|
1154
|
+
*/
|
|
1155
|
+
interface AdapterConfig {
|
|
1156
|
+
/** API 密钥 */
|
|
1157
|
+
apiKey: string;
|
|
1158
|
+
/** 基础 URL(可选,使用默认值) */
|
|
1159
|
+
baseUrl?: string;
|
|
1160
|
+
/** 额外的请求头 */
|
|
1161
|
+
headers?: Record<string, string>;
|
|
1162
|
+
/** 请求超时时间(毫秒) */
|
|
1163
|
+
timeout?: number;
|
|
1164
|
+
}
|
|
1165
|
+
/**
|
|
1166
|
+
* Provider 适配器接口
|
|
1167
|
+
* 每个 Provider 实现一个适配器,负责将 Provider 特定的 API 适配为统一接口
|
|
1168
|
+
*
|
|
1169
|
+
* 适配器的职责:
|
|
1170
|
+
* 1. 构建 Provider 特定的请求格式
|
|
1171
|
+
* 2. 解析 Provider 特定的响应格式
|
|
1172
|
+
* 3. 处理 Provider 特定的 reasoning 参数
|
|
1173
|
+
* 4. 提供 API 端点 URL
|
|
1174
|
+
*/
|
|
1175
|
+
interface ProviderAdapter {
|
|
1176
|
+
/**
|
|
1177
|
+
* 适配器名称(与 ProviderType 对应)
|
|
1178
|
+
*/
|
|
1179
|
+
readonly name: ProviderType;
|
|
1180
|
+
/**
|
|
1181
|
+
* Provider 的默认基础 URL
|
|
1182
|
+
*/
|
|
1183
|
+
readonly defaultBaseUrl: string;
|
|
1184
|
+
/**
|
|
1185
|
+
* 创建 Provider 客户端
|
|
1186
|
+
*
|
|
1187
|
+
* @param config - 客户端配置
|
|
1188
|
+
* @returns ProviderClient 实例
|
|
1189
|
+
*/
|
|
1190
|
+
createClient(config: ProviderClientConfig): ProviderClient;
|
|
1191
|
+
/**
|
|
1192
|
+
* 构建聊天请求体
|
|
1193
|
+
* 将统一的 ChatOptions 转换为 Provider 特定的请求格式
|
|
1194
|
+
*
|
|
1195
|
+
* @param options - 聊天选项
|
|
1196
|
+
* @param stream - 是否为流式请求
|
|
1197
|
+
* @returns Provider 特定格式的请求体
|
|
1198
|
+
*/
|
|
1199
|
+
buildChatRequest(options: ChatOptions, stream?: boolean): Record<string, unknown>;
|
|
1200
|
+
/**
|
|
1201
|
+
* 构建 Provider 特定的 reasoning 参数
|
|
1202
|
+
*
|
|
1203
|
+
* @param config - 推理配置
|
|
1204
|
+
* @returns Provider 特定格式的 reasoning 参数
|
|
1205
|
+
*/
|
|
1206
|
+
buildReasoningParams(config?: ReasoningConfig): Record<string, unknown>;
|
|
1207
|
+
/**
|
|
1208
|
+
* 解析聊天响应
|
|
1209
|
+
* 将 Provider 特定的响应格式转换为统一的 ChatResult
|
|
1210
|
+
*
|
|
1211
|
+
* @param response - Provider 返回的原始响应
|
|
1212
|
+
* @param model - 请求使用的模型
|
|
1213
|
+
* @returns 统一格式的 ChatResult
|
|
1214
|
+
*/
|
|
1215
|
+
parseChatResponse(response: Record<string, unknown>, model: string): ChatResult;
|
|
1216
|
+
/**
|
|
1217
|
+
* 从流式响应的 delta 中提取 StreamChunk
|
|
1218
|
+
*
|
|
1219
|
+
* @param delta - 流式响应中的 delta 对象
|
|
1220
|
+
* @returns StreamChunk 或 null(如果无法提取)
|
|
1221
|
+
*/
|
|
1222
|
+
extractStreamChunk(delta: Record<string, unknown>): StreamChunk | null;
|
|
1223
|
+
/**
|
|
1224
|
+
* 获取 API 端点 URL
|
|
1225
|
+
*
|
|
1226
|
+
* @param baseUrl - 基础 URL
|
|
1227
|
+
* @returns 完整的 API 端点 URL
|
|
1228
|
+
*/
|
|
1229
|
+
getEndpointUrl(baseUrl: string): string;
|
|
1230
|
+
}
|
|
1231
|
+
/**
|
|
1232
|
+
* 基础适配器抽象类
|
|
1233
|
+
* 提供通用的默认实现,子类可以覆盖特定方法
|
|
1234
|
+
*/
|
|
1235
|
+
declare abstract class BaseAdapter implements ProviderAdapter {
|
|
1236
|
+
abstract readonly name: ProviderType;
|
|
1237
|
+
abstract readonly defaultBaseUrl: string;
|
|
1238
|
+
/**
|
|
1239
|
+
* 创建 Provider 客户端
|
|
1240
|
+
* 默认实现:需要在运行时导入 HttpProviderClient 以避免循环依赖
|
|
1241
|
+
*/
|
|
1242
|
+
createClient(config: ProviderClientConfig): ProviderClient;
|
|
1243
|
+
/**
|
|
1244
|
+
* 构建聊天请求体
|
|
1245
|
+
* 默认实现:构建 OpenAI 兼容格式的请求体
|
|
1246
|
+
*/
|
|
1247
|
+
buildChatRequest(options: ChatOptions, stream?: boolean): Record<string, unknown>;
|
|
1248
|
+
/**
|
|
1249
|
+
* 构建 reasoning 参数
|
|
1250
|
+
* 默认实现:返回空对象,子类应覆盖此方法
|
|
1251
|
+
*/
|
|
1252
|
+
buildReasoningParams(_config?: ReasoningConfig): Record<string, unknown>;
|
|
1253
|
+
/**
|
|
1254
|
+
* 解析聊天响应
|
|
1255
|
+
* 默认实现:解析 OpenAI 兼容格式的响应
|
|
1256
|
+
*/
|
|
1257
|
+
parseChatResponse(response: Record<string, unknown>, model: string): ChatResult;
|
|
1258
|
+
/**
|
|
1259
|
+
* 从 delta 中提取 StreamChunk
|
|
1260
|
+
* 默认实现:支持 reasoning_content、reasoning、thoughts 和 content 字段
|
|
1261
|
+
*/
|
|
1262
|
+
extractStreamChunk(delta: Record<string, unknown>): StreamChunk | null;
|
|
1263
|
+
/**
|
|
1264
|
+
* 获取 API 端点 URL
|
|
1265
|
+
* 默认实现:返回 /chat/completions 端点
|
|
1266
|
+
*/
|
|
1267
|
+
getEndpointUrl(baseUrl: string): string;
|
|
1268
|
+
}
|
|
1269
|
+
|
|
1270
|
+
/**
|
|
1271
|
+
* Provider 注册表
|
|
1272
|
+
* 管理所有已注册的 Provider 适配器
|
|
1273
|
+
*
|
|
1274
|
+
* 职责:
|
|
1275
|
+
* 1. 注册和管理 Provider 适配器
|
|
1276
|
+
* 2. 提供适配器的获取和查询功能
|
|
1277
|
+
* 3. 支持从配置文件加载 Provider
|
|
1278
|
+
* 4. 自动初始化内置 Provider
|
|
1279
|
+
*/
|
|
1280
|
+
|
|
1281
|
+
/**
|
|
1282
|
+
* 注册表错误
|
|
1283
|
+
* 当 Provider 未注册或注册失败时抛出
|
|
1284
|
+
*/
|
|
1285
|
+
declare class RegistryError extends Error {
|
|
1286
|
+
readonly provider?: (ProviderType | string) | undefined;
|
|
1287
|
+
readonly code: string;
|
|
1288
|
+
constructor(message: string, provider?: (ProviderType | string) | undefined, code?: string);
|
|
1289
|
+
}
|
|
1290
|
+
/**
|
|
1291
|
+
* Provider 注册表
|
|
1292
|
+
* 管理所有已注册的 Provider 适配器
|
|
1293
|
+
*
|
|
1294
|
+
* @example
|
|
1295
|
+
* ```typescript
|
|
1296
|
+
* // 获取适配器
|
|
1297
|
+
* const adapter = ProviderRegistry.getAdapter('openrouter');
|
|
1298
|
+
*
|
|
1299
|
+
* // 检查是否支持
|
|
1300
|
+
* if (ProviderRegistry.hasAdapter('gemini')) {
|
|
1301
|
+
* // ...
|
|
1302
|
+
* }
|
|
1303
|
+
*
|
|
1304
|
+
* // 列出所有支持的 Provider
|
|
1305
|
+
* const supported = ProviderRegistry.listSupported();
|
|
1306
|
+
*
|
|
1307
|
+
* // 注册自定义适配器
|
|
1308
|
+
* ProviderRegistry.register(myCustomAdapter);
|
|
1309
|
+
* ```
|
|
1310
|
+
*/
|
|
1311
|
+
declare class ProviderRegistry {
|
|
1312
|
+
/** 适配器映射表 */
|
|
1313
|
+
private static adapters;
|
|
1314
|
+
/** 是否已初始化内置适配器 */
|
|
1315
|
+
private static initialized;
|
|
1316
|
+
/**
|
|
1317
|
+
* 注册 Provider 适配器
|
|
1318
|
+
*
|
|
1319
|
+
* @param adapter - 要注册的适配器实例
|
|
1320
|
+
* @throws RegistryError 如果适配器无效
|
|
1321
|
+
*
|
|
1322
|
+
* @example
|
|
1323
|
+
* ```typescript
|
|
1324
|
+
* const myAdapter = new MyCustomAdapter();
|
|
1325
|
+
* ProviderRegistry.register(myAdapter);
|
|
1326
|
+
* ```
|
|
1327
|
+
*/
|
|
1328
|
+
static register(adapter: ProviderAdapter): void;
|
|
1329
|
+
/**
|
|
1330
|
+
* 获取 Provider 适配器
|
|
1331
|
+
*
|
|
1332
|
+
* @param type - Provider 类型
|
|
1333
|
+
* @returns 对应的适配器实例
|
|
1334
|
+
* @throws RegistryError 如果 Provider 未注册
|
|
1335
|
+
*
|
|
1336
|
+
* @example
|
|
1337
|
+
* ```typescript
|
|
1338
|
+
* const adapter = ProviderRegistry.getAdapter('openrouter');
|
|
1339
|
+
* const client = adapter.createClient(config);
|
|
1340
|
+
* ```
|
|
1341
|
+
*/
|
|
1342
|
+
static getAdapter(type: ProviderType | string): ProviderAdapter;
|
|
1343
|
+
/**
|
|
1344
|
+
* 检查 Provider 是否已注册
|
|
1345
|
+
*
|
|
1346
|
+
* @param type - Provider 类型
|
|
1347
|
+
* @returns 是否已注册
|
|
1348
|
+
*
|
|
1349
|
+
* @example
|
|
1350
|
+
* ```typescript
|
|
1351
|
+
* if (ProviderRegistry.hasAdapter('gemini')) {
|
|
1352
|
+
* console.log('Gemini 已注册');
|
|
1353
|
+
* }
|
|
1354
|
+
* ```
|
|
1355
|
+
*/
|
|
1356
|
+
static hasAdapter(type: ProviderType | string): boolean;
|
|
1357
|
+
/**
|
|
1358
|
+
* 获取所有已注册的 Provider 类型
|
|
1359
|
+
*
|
|
1360
|
+
* @returns Provider 类型数组
|
|
1361
|
+
*
|
|
1362
|
+
* @example
|
|
1363
|
+
* ```typescript
|
|
1364
|
+
* const providers = ProviderRegistry.listSupported();
|
|
1365
|
+
* console.log('支持的 Provider:', providers);
|
|
1366
|
+
* ```
|
|
1367
|
+
*/
|
|
1368
|
+
static listSupported(): (ProviderType | string)[];
|
|
1369
|
+
/**
|
|
1370
|
+
* 从配置文件加载并注册 Provider
|
|
1371
|
+
*
|
|
1372
|
+
* @param config - 注册表配置
|
|
1373
|
+
* @throws RegistryError 如果配置无效或加载失败
|
|
1374
|
+
*
|
|
1375
|
+
* @example
|
|
1376
|
+
* ```typescript
|
|
1377
|
+
* const config: RegistryConfig = {
|
|
1378
|
+
* providers: {
|
|
1379
|
+
* 'custom-provider': {
|
|
1380
|
+
* adapter: './my-adapter',
|
|
1381
|
+
* config: {
|
|
1382
|
+
* apiKey: 'xxx',
|
|
1383
|
+
* baseUrl: 'https://api.example.com'
|
|
1384
|
+
* }
|
|
1385
|
+
* }
|
|
1386
|
+
* }
|
|
1387
|
+
* };
|
|
1388
|
+
* ProviderRegistry.loadFromConfig(config);
|
|
1389
|
+
* ```
|
|
1390
|
+
*/
|
|
1391
|
+
static loadFromConfig(config: RegistryConfig): void;
|
|
1392
|
+
/**
|
|
1393
|
+
* 初始化内置 Provider
|
|
1394
|
+
* 在首次使用时自动调用
|
|
1395
|
+
*
|
|
1396
|
+
* @example
|
|
1397
|
+
* ```typescript
|
|
1398
|
+
* // 通常不需要手动调用,会在首次使用时自动初始化
|
|
1399
|
+
* ProviderRegistry.initializeBuiltIn();
|
|
1400
|
+
* ```
|
|
1401
|
+
*/
|
|
1402
|
+
static initializeBuiltIn(): void;
|
|
1403
|
+
/**
|
|
1404
|
+
* 重置注册表(主要用于测试)
|
|
1405
|
+
* 清除所有已注册的适配器并重置初始化状态
|
|
1406
|
+
*/
|
|
1407
|
+
static reset(): void;
|
|
1408
|
+
/**
|
|
1409
|
+
* 获取适配器数量(主要用于测试)
|
|
1410
|
+
*
|
|
1411
|
+
* @returns 已注册的适配器数量
|
|
1412
|
+
*/
|
|
1413
|
+
static get size(): number;
|
|
1414
|
+
}
|
|
1415
|
+
|
|
1416
|
+
/**
|
|
1417
|
+
* OpenRouter 适配器实现
|
|
1418
|
+
* 将 OpenRouter API 适配为统一接口
|
|
1419
|
+
*
|
|
1420
|
+
* OpenRouter 特点:
|
|
1421
|
+
* - 支持多种模型(Claude、GPT、Gemini 等)
|
|
1422
|
+
* - 使用 reasoning 参数控制思考模式
|
|
1423
|
+
* - 支持 effort 和 max_tokens 参数
|
|
1424
|
+
*/
|
|
1425
|
+
|
|
1426
|
+
/**
|
|
1427
|
+
* OpenRouter 适配器
|
|
1428
|
+
* 实现 ProviderAdapter 接口,处理 OpenRouter 特定的 API 格式
|
|
1429
|
+
*/
|
|
1430
|
+
declare class OpenRouterAdapter extends BaseAdapter {
|
|
1431
|
+
readonly name: ProviderType;
|
|
1432
|
+
readonly defaultBaseUrl = "https://openrouter.ai/api/v1";
|
|
1433
|
+
/**
|
|
1434
|
+
* 构建聊天请求体
|
|
1435
|
+
* OpenRouter 使用 OpenAI 兼容格式,但有特殊的 reasoning 参数
|
|
1436
|
+
*/
|
|
1437
|
+
buildChatRequest(options: ChatOptions, stream?: boolean): Record<string, unknown>;
|
|
1438
|
+
/**
|
|
1439
|
+
* 构建 OpenRouter 格式的 reasoning 参数
|
|
1440
|
+
*
|
|
1441
|
+
* OpenRouter reasoning 参数格式:
|
|
1442
|
+
* {
|
|
1443
|
+
* effort: 'low' | 'medium' | 'high',
|
|
1444
|
+
* max_tokens: number,
|
|
1445
|
+
* exclude: boolean
|
|
1446
|
+
* }
|
|
1447
|
+
*/
|
|
1448
|
+
buildReasoningParams(config?: ReasoningConfig): Record<string, unknown>;
|
|
1449
|
+
/**
|
|
1450
|
+
* 获取 API 端点 URL
|
|
1451
|
+
* OpenRouter 使用标准的 /chat/completions 端点
|
|
1452
|
+
*/
|
|
1453
|
+
getEndpointUrl(baseUrl: string): string;
|
|
1454
|
+
}
|
|
1455
|
+
|
|
1456
|
+
/**
|
|
1457
|
+
* Gemini 适配器实现
|
|
1458
|
+
* 将 Google Gemini API 适配为统一接口
|
|
1459
|
+
*
|
|
1460
|
+
* Gemini 特点:
|
|
1461
|
+
* - 使用 OpenAI 兼容格式
|
|
1462
|
+
* - Gemini 2.5+ 模型支持 reasoning_effort 参数
|
|
1463
|
+
* - 思考内容通过 reasoning_content 或 thoughts 字段返回
|
|
1464
|
+
*/
|
|
1465
|
+
|
|
1466
|
+
/**
|
|
1467
|
+
* Gemini 适配器
|
|
1468
|
+
* 实现 ProviderAdapter 接口,处理 Gemini 特定的 API 格式
|
|
1469
|
+
*/
|
|
1470
|
+
declare class GeminiAdapter extends BaseAdapter {
|
|
1471
|
+
readonly name: ProviderType;
|
|
1472
|
+
readonly defaultBaseUrl = "https://generativelanguage.googleapis.com/v1beta/openai";
|
|
1473
|
+
/**
|
|
1474
|
+
* 构建聊天请求体
|
|
1475
|
+
* Gemini 使用 OpenAI 兼容格式,reasoning_effort 直接放在请求体中
|
|
1476
|
+
*/
|
|
1477
|
+
buildChatRequest(options: ChatOptions, stream?: boolean): Record<string, unknown>;
|
|
1478
|
+
/**
|
|
1479
|
+
* 构建 Gemini 格式的 reasoning 参数
|
|
1480
|
+
*
|
|
1481
|
+
* Gemini 2.5+ 模型使用 reasoning_effort 参数:
|
|
1482
|
+
* - 'low': 快速思考
|
|
1483
|
+
* - 'medium': 平衡模式
|
|
1484
|
+
* - 'high': 深度思考
|
|
1485
|
+
*/
|
|
1486
|
+
buildReasoningParams(config?: ReasoningConfig): Record<string, unknown>;
|
|
1487
|
+
/**
|
|
1488
|
+
* 从 delta 中提取 StreamChunk
|
|
1489
|
+
* Gemini 可能使用 reasoning_content 或 thoughts 字段
|
|
1490
|
+
*/
|
|
1491
|
+
extractStreamChunk(delta: Record<string, unknown>): StreamChunk | null;
|
|
1492
|
+
/**
|
|
1493
|
+
* 获取 API 端点 URL
|
|
1494
|
+
*/
|
|
1495
|
+
getEndpointUrl(baseUrl: string): string;
|
|
1496
|
+
}
|
|
1497
|
+
|
|
1498
|
+
/**
|
|
1499
|
+
* Groq 适配器实现
|
|
1500
|
+
* 将 Groq API 适配为统一接口
|
|
1501
|
+
*
|
|
1502
|
+
* Groq 特点:
|
|
1503
|
+
* - 使用 OpenAI 兼容格式
|
|
1504
|
+
* - 使用 reasoning_format 参数控制推理输出
|
|
1505
|
+
* - 不能同时使用 include_reasoning 和 reasoning_format
|
|
1506
|
+
* - 使用 max_completion_tokens 而不是 max_tokens
|
|
1507
|
+
*/
|
|
1508
|
+
|
|
1509
|
+
/**
|
|
1510
|
+
* Groq 适配器
|
|
1511
|
+
* 实现 ProviderAdapter 接口,处理 Groq 特定的 API 格式
|
|
1512
|
+
*/
|
|
1513
|
+
declare class GroqAdapter extends BaseAdapter {
|
|
1514
|
+
readonly name: ProviderType;
|
|
1515
|
+
readonly defaultBaseUrl = "https://api.groq.com/openai/v1";
|
|
1516
|
+
/**
|
|
1517
|
+
* 构建聊天请求体
|
|
1518
|
+
* Groq 使用 OpenAI 兼容格式,但有一些特殊参数
|
|
1519
|
+
*/
|
|
1520
|
+
buildChatRequest(options: ChatOptions, stream?: boolean): Record<string, unknown>;
|
|
1521
|
+
/**
|
|
1522
|
+
* 构建 Groq 格式的 reasoning 参数
|
|
1523
|
+
*
|
|
1524
|
+
* Groq 使用 reasoning_format 参数:
|
|
1525
|
+
* - 'raw': 原始格式
|
|
1526
|
+
* - 'parsed': 解析格式(推荐)
|
|
1527
|
+
*
|
|
1528
|
+
* 注意:不能同时使用 include_reasoning 和 reasoning_format
|
|
1529
|
+
*/
|
|
1530
|
+
buildReasoningParams(config?: ReasoningConfig): Record<string, unknown>;
|
|
1531
|
+
/**
|
|
1532
|
+
* 从 delta 中提取 StreamChunk
|
|
1533
|
+
* Groq 使用 reasoning_content 或 reasoning 字段
|
|
1534
|
+
*/
|
|
1535
|
+
extractStreamChunk(delta: Record<string, unknown>): StreamChunk | null;
|
|
1536
|
+
/**
|
|
1537
|
+
* 获取 API 端点 URL
|
|
1538
|
+
*/
|
|
1539
|
+
getEndpointUrl(baseUrl: string): string;
|
|
1540
|
+
}
|
|
1541
|
+
|
|
1542
|
+
/**
|
|
1543
|
+
* HuggingFace 适配器实现
|
|
1544
|
+
* 将 HuggingFace Router API 适配为统一接口
|
|
1545
|
+
*
|
|
1546
|
+
* HuggingFace 特点:
|
|
1547
|
+
* - 使用 OpenAI 兼容格式
|
|
1548
|
+
* - 是模型聚合平台,thinking 支持取决于具体模型
|
|
1549
|
+
* - 使用 reasoning_effort 参数(如果模型支持)
|
|
1550
|
+
*/
|
|
1551
|
+
|
|
1552
|
+
/**
|
|
1553
|
+
* HuggingFace 适配器
|
|
1554
|
+
* 实现 ProviderAdapter 接口,处理 HuggingFace 特定的 API 格式
|
|
1555
|
+
*/
|
|
1556
|
+
declare class HuggingFaceAdapter extends BaseAdapter {
|
|
1557
|
+
readonly name: ProviderType;
|
|
1558
|
+
readonly defaultBaseUrl = "https://router.huggingface.co/v1";
|
|
1559
|
+
/**
|
|
1560
|
+
* 构建聊天请求体
|
|
1561
|
+
* HuggingFace 使用标准 OpenAI 兼容格式
|
|
1562
|
+
*/
|
|
1563
|
+
buildChatRequest(options: ChatOptions, stream?: boolean): Record<string, unknown>;
|
|
1564
|
+
/**
|
|
1565
|
+
* 构建 HuggingFace 格式的 reasoning 参数
|
|
1566
|
+
* HuggingFace 使用 reasoning_effort 参数(取决于具体模型是否支持)
|
|
1567
|
+
*/
|
|
1568
|
+
buildReasoningParams(config?: ReasoningConfig): Record<string, unknown>;
|
|
1569
|
+
/**
|
|
1570
|
+
* 从 delta 中提取 StreamChunk
|
|
1571
|
+
*/
|
|
1572
|
+
extractStreamChunk(delta: Record<string, unknown>): StreamChunk | null;
|
|
1573
|
+
/**
|
|
1574
|
+
* 获取 API 端点 URL
|
|
1575
|
+
*/
|
|
1576
|
+
getEndpointUrl(baseUrl: string): string;
|
|
1577
|
+
}
|
|
1578
|
+
|
|
1579
|
+
/**
|
|
1580
|
+
* ModelScope 适配器实现
|
|
1581
|
+
* 将 ModelScope API 适配为统一接口
|
|
1582
|
+
*
|
|
1583
|
+
* ModelScope 特点:
|
|
1584
|
+
* - 使用 OpenAI 兼容格式
|
|
1585
|
+
* - 使用 enable_thinking 参数控制思考模式
|
|
1586
|
+
* - 支持 DeepSeek 等模型
|
|
1587
|
+
*/
|
|
1588
|
+
|
|
1589
|
+
/**
|
|
1590
|
+
* ModelScope 适配器
|
|
1591
|
+
* 实现 ProviderAdapter 接口,处理 ModelScope 特定的 API 格式
|
|
1592
|
+
*/
|
|
1593
|
+
declare class ModelScopeAdapter extends BaseAdapter {
|
|
1594
|
+
readonly name: ProviderType;
|
|
1595
|
+
readonly defaultBaseUrl = "https://api-inference.modelscope.cn/v1";
|
|
1596
|
+
/**
|
|
1597
|
+
* 构建聊天请求体
|
|
1598
|
+
* ModelScope 使用 OpenAI 兼容格式
|
|
1599
|
+
*/
|
|
1600
|
+
buildChatRequest(options: ChatOptions, stream?: boolean): Record<string, unknown>;
|
|
1601
|
+
/**
|
|
1602
|
+
* 构建 ModelScope 格式的 reasoning 参数
|
|
1603
|
+
* ModelScope 使用 enable_thinking 参数控制思考模式
|
|
1604
|
+
*/
|
|
1605
|
+
buildReasoningParams(config?: ReasoningConfig): Record<string, unknown>;
|
|
1606
|
+
/**
|
|
1607
|
+
* 从 delta 中提取 StreamChunk
|
|
1608
|
+
*/
|
|
1609
|
+
extractStreamChunk(delta: Record<string, unknown>): StreamChunk | null;
|
|
1610
|
+
/**
|
|
1611
|
+
* 获取 API 端点 URL
|
|
1612
|
+
*/
|
|
1613
|
+
getEndpointUrl(baseUrl: string): string;
|
|
1614
|
+
}
|
|
1615
|
+
|
|
1616
|
+
/**
|
|
1617
|
+
* DeepSeek 适配器实现
|
|
1618
|
+
* 将 DeepSeek API 适配为统一接口
|
|
1619
|
+
*
|
|
1620
|
+
* DeepSeek 特点:
|
|
1621
|
+
* - 使用 OpenAI 兼容格式
|
|
1622
|
+
* - 使用 thinking 参数启用思考模式
|
|
1623
|
+
* - deepseek-chat: 通用对话模型(可通过 thinking 参数启用思考)
|
|
1624
|
+
* - deepseek-reasoner: 推理模型(自动启用思考)
|
|
1625
|
+
*/
|
|
1626
|
+
|
|
1627
|
+
/**
|
|
1628
|
+
* DeepSeek 适配器
|
|
1629
|
+
* 实现 ProviderAdapter 接口,处理 DeepSeek 特定的 API 格式
|
|
1630
|
+
*/
|
|
1631
|
+
declare class DeepSeekAdapter extends BaseAdapter {
|
|
1632
|
+
readonly name: ProviderType;
|
|
1633
|
+
readonly defaultBaseUrl = "https://api.deepseek.com";
|
|
1634
|
+
/**
|
|
1635
|
+
* 构建聊天请求体
|
|
1636
|
+
* DeepSeek 使用 OpenAI 兼容格式
|
|
1637
|
+
*/
|
|
1638
|
+
buildChatRequest(options: ChatOptions, stream?: boolean): Record<string, unknown>;
|
|
1639
|
+
/**
|
|
1640
|
+
* 构建 DeepSeek 格式的 reasoning 参数
|
|
1641
|
+
* DeepSeek 使用 thinking 参数启用思考模式
|
|
1642
|
+
*/
|
|
1643
|
+
buildReasoningParams(config?: ReasoningConfig): Record<string, unknown>;
|
|
1644
|
+
/**
|
|
1645
|
+
* 从 delta 中提取 StreamChunk
|
|
1646
|
+
* DeepSeek R1 使用 reasoning_content 返回思考过程
|
|
1647
|
+
*/
|
|
1648
|
+
extractStreamChunk(delta: Record<string, unknown>): StreamChunk | null;
|
|
1649
|
+
/**
|
|
1650
|
+
* 获取 API 端点 URL
|
|
1651
|
+
*/
|
|
1652
|
+
getEndpointUrl(baseUrl: string): string;
|
|
1653
|
+
}
|
|
1654
|
+
|
|
1655
|
+
/**
|
|
1656
|
+
* Poe 适配器实现
|
|
1657
|
+
* 将 Poe API 适配为统一接口
|
|
1658
|
+
*
|
|
1659
|
+
* Poe 特点:
|
|
1660
|
+
* - 使用 OpenAI 兼容格式
|
|
1661
|
+
* - 单一 API Key 访问数百个模型
|
|
1662
|
+
* - 通过 extra_body 传递 reasoning_effort, thinking_budget 等参数
|
|
1663
|
+
* - 支持 <think>...</think> 标签和 *Thinking...*\n> ... 格式的思考内容
|
|
1664
|
+
*/
|
|
1665
|
+
|
|
1666
|
+
/**
|
|
1667
|
+
* Poe 适配器
|
|
1668
|
+
* 实现 ProviderAdapter 接口,处理 Poe 特定的 API 格式
|
|
1669
|
+
*/
|
|
1670
|
+
declare class PoeAdapter extends BaseAdapter {
|
|
1671
|
+
readonly name: ProviderType;
|
|
1672
|
+
readonly defaultBaseUrl = "https://api.poe.com/v1";
|
|
1673
|
+
/**
|
|
1674
|
+
* 构建聊天请求体
|
|
1675
|
+
* Poe 使用 OpenAI 兼容格式,通过 extra_body 传递自定义参数
|
|
1676
|
+
*/
|
|
1677
|
+
buildChatRequest(options: ChatOptions, stream?: boolean): Record<string, unknown>;
|
|
1678
|
+
/**
|
|
1679
|
+
* 构建 Poe 格式的 reasoning 参数
|
|
1680
|
+
* Poe 通过 extra_body 传递 reasoning_effort 和 thinking_budget
|
|
1681
|
+
*/
|
|
1682
|
+
buildReasoningParams(config?: ReasoningConfig): Record<string, unknown>;
|
|
1683
|
+
/**
|
|
1684
|
+
* 解析聊天响应
|
|
1685
|
+
* Poe 可能返回 reasoning_content,或者需要从 <think> 标签提取
|
|
1686
|
+
*/
|
|
1687
|
+
parseChatResponse(response: Record<string, unknown>, model: string): ChatResult;
|
|
1688
|
+
/**
|
|
1689
|
+
* 从 delta 中提取 StreamChunk
|
|
1690
|
+
* Poe 的流式响应处理比较复杂,需要处理多种思考格式
|
|
1691
|
+
*/
|
|
1692
|
+
extractStreamChunk(delta: Record<string, unknown>): StreamChunk | null;
|
|
1693
|
+
/**
|
|
1694
|
+
* 获取 API 端点 URL
|
|
1695
|
+
*/
|
|
1696
|
+
getEndpointUrl(baseUrl: string): string;
|
|
1697
|
+
}
|
|
1698
|
+
|
|
1699
|
+
/**
|
|
1700
|
+
* AWS Nova 适配器实现
|
|
1701
|
+
* 将 Amazon Nova API 适配为统一接口
|
|
1702
|
+
*
|
|
1703
|
+
* Nova 特点:
|
|
1704
|
+
* - 使用 OpenAI 兼容格式
|
|
1705
|
+
* - Nova 2 Lite 支持 extended thinking (reasoningConfig)
|
|
1706
|
+
* - effort 映射为 maxReasoningEffort: low/medium/high
|
|
1707
|
+
* - temperature 范围是 0-1(不是 0-2)
|
|
1708
|
+
*/
|
|
1709
|
+
|
|
1710
|
+
/**
|
|
1711
|
+
* Nova 适配器
|
|
1712
|
+
* 实现 ProviderAdapter 接口,处理 Nova 特定的 API 格式
|
|
1713
|
+
*/
|
|
1714
|
+
declare class NovaAdapter extends BaseAdapter {
|
|
1715
|
+
readonly name: ProviderType;
|
|
1716
|
+
readonly defaultBaseUrl = "https://api.nova.amazon.com/v1";
|
|
1717
|
+
/**
|
|
1718
|
+
* 构建聊天请求体
|
|
1719
|
+
* Nova 使用 OpenAI 兼容格式
|
|
1720
|
+
*/
|
|
1721
|
+
buildChatRequest(options: ChatOptions, stream?: boolean): Record<string, unknown>;
|
|
1722
|
+
/**
|
|
1723
|
+
* 构建 Nova 格式的 reasoning 参数
|
|
1724
|
+
* Nova 使用 reasoningConfig 控制 extended thinking
|
|
1725
|
+
*/
|
|
1726
|
+
buildReasoningParams(config?: ReasoningConfig): Record<string, unknown>;
|
|
1727
|
+
/**
|
|
1728
|
+
* 从 delta 中提取 StreamChunk
|
|
1729
|
+
* Nova 返回 reasoning_content 作为思考过程
|
|
1730
|
+
*/
|
|
1731
|
+
extractStreamChunk(delta: Record<string, unknown>): StreamChunk | null;
|
|
1732
|
+
/**
|
|
1733
|
+
* 获取 API 端点 URL
|
|
1734
|
+
*/
|
|
1735
|
+
getEndpointUrl(baseUrl: string): string;
|
|
1736
|
+
}
|
|
1737
|
+
|
|
1738
|
+
/**
|
|
1739
|
+
* 适配器层导出文件
|
|
1740
|
+
* 导出所有 Provider 适配器和相关类型
|
|
1741
|
+
*/
|
|
1742
|
+
|
|
1743
|
+
/**
|
|
1744
|
+
* 创建所有内置适配器实例
|
|
1745
|
+
* 用于 ProviderRegistry 初始化
|
|
1746
|
+
*/
|
|
1747
|
+
declare function createBuiltInAdapters(): Map<ProviderType, ProviderAdapter>;
|
|
1748
|
+
|
|
1749
|
+
/**
|
|
1750
|
+
* Fluent API 类型定义
|
|
1751
|
+
* 提供链式调用和预设实例的类型支持
|
|
1752
|
+
*/
|
|
1753
|
+
|
|
1754
|
+
/**
|
|
1755
|
+
* 问答选项
|
|
1756
|
+
* 用于配置单次 AI 调用的参数
|
|
1757
|
+
*/
|
|
1758
|
+
interface AskOptions {
|
|
1759
|
+
/** 系统提示词 */
|
|
1760
|
+
system?: string;
|
|
1761
|
+
/** 温度参数 (0-2) */
|
|
1762
|
+
temperature?: number;
|
|
1763
|
+
/** 最大输出 token 数 */
|
|
1764
|
+
maxTokens?: number;
|
|
1765
|
+
/** 思考/推理配置 */
|
|
1766
|
+
reasoning?: ReasoningConfig;
|
|
1767
|
+
}
|
|
1768
|
+
/**
|
|
1769
|
+
* 流式回调函数
|
|
1770
|
+
* 用于处理流式响应的各个阶段
|
|
1771
|
+
*/
|
|
1772
|
+
interface StreamCallbacks {
|
|
1773
|
+
/** 收到思考内容时调用 */
|
|
1774
|
+
onReasoning?: (text: string) => void;
|
|
1775
|
+
/** 收到正式内容时调用 */
|
|
1776
|
+
onContent?: (text: string) => void;
|
|
1777
|
+
/** 完成时调用,包含完整的思考和内容 */
|
|
1778
|
+
onDone?: (result: {
|
|
1779
|
+
reasoning: string;
|
|
1780
|
+
content: string;
|
|
1781
|
+
}) => void;
|
|
1782
|
+
}
|
|
1783
|
+
/**
|
|
1784
|
+
* 对话会话配置
|
|
1785
|
+
* 用于创建多轮对话会话时的配置
|
|
1786
|
+
*/
|
|
1787
|
+
interface ChatSessionOptions {
|
|
1788
|
+
/** 系统提示词 */
|
|
1789
|
+
system?: string;
|
|
1790
|
+
/** 温度参数 (0-2) */
|
|
1791
|
+
temperature?: number;
|
|
1792
|
+
/** 最大输出 token 数 */
|
|
1793
|
+
maxTokens?: number;
|
|
1794
|
+
/** 思考/推理配置 */
|
|
1795
|
+
reasoning?: ReasoningConfig;
|
|
1796
|
+
}
|
|
1797
|
+
/**
|
|
1798
|
+
* 多轮对话会话接口
|
|
1799
|
+
* 自动维护对话历史,支持流式和非流式响应
|
|
1800
|
+
*/
|
|
1801
|
+
interface ChatSession {
|
|
1802
|
+
/**
|
|
1803
|
+
* 发送消息并获取响应(非流式)
|
|
1804
|
+
* @param message - 用户消息
|
|
1805
|
+
* @returns 助手响应内容
|
|
1806
|
+
*/
|
|
1807
|
+
send(message: string): Promise<string>;
|
|
1808
|
+
/**
|
|
1809
|
+
* 发送消息并获取流式响应
|
|
1810
|
+
* @param message - 用户消息
|
|
1811
|
+
* @returns 流式数据块生成器
|
|
1812
|
+
*/
|
|
1813
|
+
sendStream(message: string): AsyncGenerator<StreamChunk>;
|
|
1814
|
+
/**
|
|
1815
|
+
* 获取对话历史
|
|
1816
|
+
* @returns 按发送顺序排列的消息列表
|
|
1817
|
+
*/
|
|
1818
|
+
getHistory(): ChatMessage[];
|
|
1819
|
+
/**
|
|
1820
|
+
* 清空对话历史
|
|
1821
|
+
*/
|
|
1822
|
+
clearHistory(): void;
|
|
1823
|
+
}
|
|
1824
|
+
/**
|
|
1825
|
+
* 构建器内部配置状态
|
|
1826
|
+
* 存储链式调用过程中设置的所有参数
|
|
1827
|
+
*/
|
|
1828
|
+
interface BuilderConfig {
|
|
1829
|
+
/** Provider 类型 */
|
|
1830
|
+
provider?: ProviderType;
|
|
1831
|
+
/** 模型 ID */
|
|
1832
|
+
model?: string;
|
|
1833
|
+
/** API Key */
|
|
1834
|
+
apiKey?: string;
|
|
1835
|
+
/** 基础 URL */
|
|
1836
|
+
baseUrl?: string;
|
|
1837
|
+
/** 系统提示词 */
|
|
1838
|
+
system?: string;
|
|
1839
|
+
/** 温度参数 (0-2) */
|
|
1840
|
+
temperature?: number;
|
|
1841
|
+
/** 最大输出 token 数 */
|
|
1842
|
+
maxTokens?: number;
|
|
1843
|
+
/** 思考/推理配置 */
|
|
1844
|
+
reasoning?: ReasoningConfig;
|
|
1845
|
+
/** 是否流式请求 */
|
|
1846
|
+
isStream?: boolean;
|
|
1847
|
+
}
|
|
1848
|
+
/**
|
|
1849
|
+
* 链式调用构建器接口
|
|
1850
|
+
* 支持流畅的方法链配置请求参数
|
|
1851
|
+
*
|
|
1852
|
+
* @example
|
|
1853
|
+
* ```typescript
|
|
1854
|
+
* const answer = await oiiai
|
|
1855
|
+
* .use('deepseek')
|
|
1856
|
+
* .key('your-api-key')
|
|
1857
|
+
* .model('deepseek-chat')
|
|
1858
|
+
* .system('你是助手')
|
|
1859
|
+
* .ask('你好');
|
|
1860
|
+
* ```
|
|
1861
|
+
*/
|
|
1862
|
+
interface OiiaiBuilder {
|
|
1863
|
+
/**
|
|
1864
|
+
* 选择服务提供商
|
|
1865
|
+
* @param provider - Provider 类型
|
|
1866
|
+
* @returns this 支持链式调用
|
|
1867
|
+
*/
|
|
1868
|
+
use(provider: ProviderType): this;
|
|
1869
|
+
/**
|
|
1870
|
+
* 指定模型
|
|
1871
|
+
* @param modelId - 模型 ID
|
|
1872
|
+
* @returns this 支持链式调用
|
|
1873
|
+
*/
|
|
1874
|
+
model(modelId: string): this;
|
|
1875
|
+
/**
|
|
1876
|
+
* 设置系统提示词
|
|
1877
|
+
* @param prompt - 系统提示词
|
|
1878
|
+
* @returns this 支持链式调用
|
|
1879
|
+
*/
|
|
1880
|
+
system(prompt: string): this;
|
|
1881
|
+
/**
|
|
1882
|
+
* 设置温度参数
|
|
1883
|
+
* @param value - 温度值 (0-2)
|
|
1884
|
+
* @returns this 支持链式调用
|
|
1885
|
+
*/
|
|
1886
|
+
temperature(value: number): this;
|
|
1887
|
+
/**
|
|
1888
|
+
* 设置最大输出 token 数
|
|
1889
|
+
* @param value - token 数量
|
|
1890
|
+
* @returns this 支持链式调用
|
|
1891
|
+
*/
|
|
1892
|
+
maxTokens(value: number): this;
|
|
1893
|
+
/**
|
|
1894
|
+
* 配置思考/推理模式
|
|
1895
|
+
* @param config - 推理配置
|
|
1896
|
+
* @returns this 支持链式调用
|
|
1897
|
+
*/
|
|
1898
|
+
reasoning(config: ReasoningConfig): this;
|
|
1899
|
+
/**
|
|
1900
|
+
* 设置 API Key
|
|
1901
|
+
* @param apiKey - API Key
|
|
1902
|
+
* @returns this 支持链式调用
|
|
1903
|
+
*/
|
|
1904
|
+
key(apiKey: string): this;
|
|
1905
|
+
/**
|
|
1906
|
+
* 设置基础 URL
|
|
1907
|
+
* @param url - 基础 URL
|
|
1908
|
+
* @returns this 支持链式调用
|
|
1909
|
+
*/
|
|
1910
|
+
baseUrl(url: string): this;
|
|
1911
|
+
/**
|
|
1912
|
+
* 标记为流式请求
|
|
1913
|
+
* 调用后 ask() 将返回 AsyncGenerator
|
|
1914
|
+
* @returns StreamBuilder 类型
|
|
1915
|
+
*/
|
|
1916
|
+
stream(): StreamBuilder;
|
|
1917
|
+
/**
|
|
1918
|
+
* 执行请求(非流式)
|
|
1919
|
+
* @param question - 问题
|
|
1920
|
+
* @returns 响应内容
|
|
1921
|
+
*/
|
|
1922
|
+
ask(question: string): Promise<string>;
|
|
1923
|
+
}
|
|
1924
|
+
/**
|
|
1925
|
+
* 流式构建器接口
|
|
1926
|
+
* 调用 stream() 后的构建器类型,ask() 返回 AsyncGenerator
|
|
1927
|
+
*/
|
|
1928
|
+
interface StreamBuilder extends Omit<OiiaiBuilder, 'ask' | 'stream'> {
|
|
1929
|
+
/**
|
|
1930
|
+
* 执行流式请求
|
|
1931
|
+
* @param question - 问题
|
|
1932
|
+
* @returns 流式数据块生成器
|
|
1933
|
+
*/
|
|
1934
|
+
ask(question: string): AsyncGenerator<StreamChunk>;
|
|
1935
|
+
}
|
|
1936
|
+
/**
|
|
1937
|
+
* 预设实例配置选项
|
|
1938
|
+
* 用于配置预设实例的 API Key 和基础 URL
|
|
1939
|
+
*/
|
|
1940
|
+
interface PresetConfigOptions {
|
|
1941
|
+
/** API Key */
|
|
1942
|
+
apiKey: string;
|
|
1943
|
+
/** 基础 URL(可选) */
|
|
1944
|
+
baseUrl?: string;
|
|
1945
|
+
}
|
|
1946
|
+
/**
|
|
1947
|
+
* 预设实例接口
|
|
1948
|
+
* 提供快捷的 AI 调用方法,预配置了 Provider 类型
|
|
1949
|
+
*
|
|
1950
|
+
* @example
|
|
1951
|
+
* ```typescript
|
|
1952
|
+
* // 配置并使用
|
|
1953
|
+
* deepseek.configure({ apiKey: 'your-key' });
|
|
1954
|
+
* const answer = await deepseek.ask('deepseek-chat', '你好');
|
|
1955
|
+
*
|
|
1956
|
+
* // 流式响应
|
|
1957
|
+
* for await (const chunk of deepseek.stream('deepseek-chat', '写首诗')) {
|
|
1958
|
+
* console.log(chunk.text);
|
|
1959
|
+
* }
|
|
1960
|
+
* ```
|
|
1961
|
+
*/
|
|
1962
|
+
interface PresetProvider {
|
|
1963
|
+
/** Provider 名称 */
|
|
1964
|
+
readonly name: ProviderType;
|
|
1965
|
+
/**
|
|
1966
|
+
* 配置 API Key 和其他选项
|
|
1967
|
+
* @param options - 配置选项
|
|
1968
|
+
* @returns this 支持链式调用
|
|
1969
|
+
*/
|
|
1970
|
+
configure(options: PresetConfigOptions): this;
|
|
1971
|
+
/**
|
|
1972
|
+
* 从环境变量读取配置
|
|
1973
|
+
* 环境变量名格式: {PROVIDER}_API_KEY (如 DEEPSEEK_API_KEY)
|
|
1974
|
+
* @returns this 支持链式调用
|
|
1975
|
+
*/
|
|
1976
|
+
fromEnv(): this;
|
|
1977
|
+
/**
|
|
1978
|
+
* 简单问答(非流式)
|
|
1979
|
+
* @param model - 模型 ID
|
|
1980
|
+
* @param question - 问题
|
|
1981
|
+
* @param options - 可选配置
|
|
1982
|
+
* @returns 响应内容
|
|
1983
|
+
*/
|
|
1984
|
+
ask(model: string, question: string, options?: AskOptions): Promise<string>;
|
|
1985
|
+
/**
|
|
1986
|
+
* 流式问答
|
|
1987
|
+
* @param model - 模型 ID
|
|
1988
|
+
* @param question - 问题
|
|
1989
|
+
* @param options - 可选配置
|
|
1990
|
+
* @returns 流式数据块生成器
|
|
1991
|
+
*/
|
|
1992
|
+
stream(model: string, question: string, options?: AskOptions): AsyncGenerator<StreamChunk>;
|
|
1993
|
+
/**
|
|
1994
|
+
* 带回调的流式问答
|
|
1995
|
+
* @param model - 模型 ID
|
|
1996
|
+
* @param question - 问题
|
|
1997
|
+
* @param callbacks - 回调函数
|
|
1998
|
+
* @returns Promise,完成时 resolve
|
|
1999
|
+
*/
|
|
2000
|
+
streamWithCallbacks(model: string, question: string, callbacks: StreamCallbacks): Promise<void>;
|
|
2001
|
+
/**
|
|
2002
|
+
* 获取构建器(预配置 provider 和 model)
|
|
2003
|
+
* @param model - 模型 ID
|
|
2004
|
+
* @returns 预配置的构建器
|
|
2005
|
+
*/
|
|
2006
|
+
builder(model: string): OiiaiBuilder;
|
|
2007
|
+
/**
|
|
2008
|
+
* 创建多轮对话会话
|
|
2009
|
+
* @param model - 模型 ID
|
|
2010
|
+
* @param options - 会话配置
|
|
2011
|
+
* @returns 对话会话实例
|
|
2012
|
+
*/
|
|
2013
|
+
chat(model: string, options?: ChatSessionOptions): ChatSession;
|
|
2014
|
+
}
|
|
2015
|
+
|
|
2016
|
+
/**
|
|
2017
|
+
* Fluent API 链式构建器实现
|
|
2018
|
+
* 支持流畅的方法链配置请求参数
|
|
2019
|
+
*/
|
|
2020
|
+
|
|
2021
|
+
/**
|
|
2022
|
+
* 创建新的构建器实例
|
|
2023
|
+
* @param initialConfig - 可选的初始配置
|
|
2024
|
+
* @returns 构建器实例
|
|
2025
|
+
*/
|
|
2026
|
+
declare function createBuilder(initialConfig?: Partial<BuilderConfig>): OiiaiBuilder;
|
|
2027
|
+
/**
|
|
2028
|
+
* 全局构建器入口
|
|
2029
|
+
* 每次访问返回新实例,避免状态污染
|
|
2030
|
+
*/
|
|
2031
|
+
declare const oiiai: OiiaiBuilder;
|
|
2032
|
+
|
|
2033
|
+
/**
|
|
2034
|
+
* Fluent API 错误类定义
|
|
2035
|
+
* 提供配置错误和验证错误的统一处理
|
|
2036
|
+
*/
|
|
2037
|
+
/**
|
|
2038
|
+
* 配置错误
|
|
2039
|
+
* 在调用 ask() 时验证配置是否完整
|
|
2040
|
+
*
|
|
2041
|
+
* @example
|
|
2042
|
+
* ```typescript
|
|
2043
|
+
* // 未配置 API Key
|
|
2044
|
+
* throw new ConfigurationError('请先配置 API Key:调用 configure({ apiKey: \'xxx\' }) 或 fromEnv()');
|
|
2045
|
+
*
|
|
2046
|
+
* // 未调用 use()
|
|
2047
|
+
* throw new ConfigurationError('请先调用 use(provider) 选择服务提供商');
|
|
2048
|
+
* ```
|
|
2049
|
+
*/
|
|
2050
|
+
declare class ConfigurationError extends Error {
|
|
2051
|
+
constructor(message: string);
|
|
2052
|
+
}
|
|
2053
|
+
/**
|
|
2054
|
+
* 验证错误
|
|
2055
|
+
* 在设置参数时验证参数是否有效
|
|
2056
|
+
*
|
|
2057
|
+
* @example
|
|
2058
|
+
* ```typescript
|
|
2059
|
+
* // temperature 超出范围
|
|
2060
|
+
* throw new ValidationError('temperature 必须在 0-2 之间');
|
|
2061
|
+
*
|
|
2062
|
+
* // 无效的 Provider
|
|
2063
|
+
* throw new ValidationError('不支持的 Provider: xxx,支持的 Provider: deepseek, openrouter, ...');
|
|
2064
|
+
* ```
|
|
2065
|
+
*/
|
|
2066
|
+
declare class ValidationError extends Error {
|
|
2067
|
+
constructor(message: string);
|
|
2068
|
+
}
|
|
2069
|
+
|
|
2070
|
+
/**
|
|
2071
|
+
* 预设实例导出
|
|
2072
|
+
* 提供所有 Provider 的预配置实例
|
|
2073
|
+
*
|
|
2074
|
+
* @example
|
|
2075
|
+
* ```typescript
|
|
2076
|
+
* import { deepseek, openrouter, gemini } from 'oiiai';
|
|
2077
|
+
*
|
|
2078
|
+
* // 配置并使用
|
|
2079
|
+
* deepseek.configure({ apiKey: 'your-key' });
|
|
2080
|
+
* const answer = await deepseek.ask('deepseek-chat', '你好');
|
|
2081
|
+
*
|
|
2082
|
+
* // 或从环境变量读取
|
|
2083
|
+
* openrouter.fromEnv();
|
|
2084
|
+
* const stream = openrouter.stream('gpt-4o', '写首诗');
|
|
2085
|
+
* ```
|
|
2086
|
+
*/
|
|
2087
|
+
|
|
2088
|
+
/**
|
|
2089
|
+
* DeepSeek 预设实例
|
|
2090
|
+
* 环境变量: DEEPSEEK_API_KEY
|
|
2091
|
+
*/
|
|
2092
|
+
declare const deepseek: PresetProvider;
|
|
2093
|
+
/**
|
|
2094
|
+
* OpenRouter 预设实例
|
|
2095
|
+
* 环境变量: OPENROUTER_API_KEY
|
|
2096
|
+
*/
|
|
2097
|
+
declare const openrouter: PresetProvider;
|
|
2098
|
+
/**
|
|
2099
|
+
* Gemini 预设实例
|
|
2100
|
+
* 环境变量: GEMINI_API_KEY
|
|
2101
|
+
*/
|
|
2102
|
+
declare const gemini: PresetProvider;
|
|
2103
|
+
/**
|
|
2104
|
+
* Groq 预设实例
|
|
2105
|
+
* 环境变量: GROQ_API_KEY
|
|
2106
|
+
*/
|
|
2107
|
+
declare const groq: PresetProvider;
|
|
2108
|
+
/**
|
|
2109
|
+
* HuggingFace 预设实例
|
|
2110
|
+
* 环境变量: HUGGINGFACE_API_KEY
|
|
2111
|
+
*/
|
|
2112
|
+
declare const huggingface: PresetProvider;
|
|
2113
|
+
/**
|
|
2114
|
+
* ModelScope 预设实例
|
|
2115
|
+
* 环境变量: MODELSCOPE_API_KEY
|
|
2116
|
+
*/
|
|
2117
|
+
declare const modelscope: PresetProvider;
|
|
2118
|
+
/**
|
|
2119
|
+
* Poe 预设实例
|
|
2120
|
+
* 环境变量: POE_API_KEY
|
|
2121
|
+
*/
|
|
2122
|
+
declare const poe: PresetProvider;
|
|
2123
|
+
/**
|
|
2124
|
+
* Nova 预设实例
|
|
2125
|
+
* 环境变量: NOVA_API_KEY
|
|
2126
|
+
*/
|
|
2127
|
+
declare const nova: PresetProvider;
|
|
2128
|
+
|
|
2129
|
+
export { type AIProvider, APIError, type AdapterConfig, type AskOptions, BaseAdapter, BaseProvider, type BuilderConfig, CONFIG_DEFAULTS, type ChatMessage, type ChatOptions, type ChatResult, type ChatSession, type ChatSessionOptions, ConfigManager, ConfigValidator, ConfigurationError, DeepSeekAdapter, type DeltaExtractor, EFFORT_TOKEN_MAP, ValidationError as FluentValidationError, GeminiAdapter, GeminiProvider, GroqAdapter, GroqProvider, HttpProviderClient, HuggingFaceAdapter, HuggingFaceProvider, type LegacyProviderConfig, type ModelInfo, type ModelPricing, ModelScopeAdapter, ModelScopeProvider, NetworkError, NovaAdapter, type OiiaiBuilder, OpenRouterAdapter, type OpenRouterModelInfo, OpenRouterProvider, PoeAdapter, type PresetConfigOptions, type PresetProvider, type ProviderAdapter, type ProviderCapabilities, type ProviderClient, type ProviderClientConfig, type ProviderConfig, ProviderError, ProviderRegistry, type ProviderType$2 as ProviderType, type ReasoningConfig, type ReasoningEffort, type RegistryConfig, RegistryError, RequestBuilder, type StreamBuilder, type StreamCallbacks, type StreamChunk, StreamProcessor, TimeoutError, type TokenUsage, type UnifiedProviderConfig, VALID_PROVIDERS, type ValidationError$1 as ValidationError, type ValidationResult, ai, createBuilder, createBuiltInAdapters, createProvider, deepseek, gemini, groq, huggingface, modelscope, nova, oiiai, openrouter, poe };
|