@weisiren000/oiiai 0.1.3 → 0.1.4
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/dist/index.d.mts +120 -5
- package/dist/index.d.ts +120 -5
- package/dist/index.js +795 -28
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +795 -28
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -2
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,25 +196,83 @@ 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 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): 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): 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): Promise<string>;
|
|
167
276
|
}
|
|
168
277
|
|
|
169
278
|
/**
|
|
@@ -174,7 +283,7 @@ declare abstract class BaseProvider implements AIProvider {
|
|
|
174
283
|
/**
|
|
175
284
|
* 支持的 Provider 类型
|
|
176
285
|
*/
|
|
177
|
-
type ProviderType = 'openrouter' | 'gemini' | 'groq' | 'huggingface' | 'modelscope' | 'deepseek';
|
|
286
|
+
type ProviderType = 'openrouter' | 'gemini' | 'groq' | 'huggingface' | 'modelscope' | 'deepseek' | 'poe' | 'nova';
|
|
178
287
|
/**
|
|
179
288
|
* 统一的 Provider 配置
|
|
180
289
|
*/
|
|
@@ -212,6 +321,8 @@ declare const ai: {
|
|
|
212
321
|
huggingface: (apiKey: string, baseUrl?: string) => AIProvider;
|
|
213
322
|
modelscope: (apiKey: string, baseUrl?: string) => AIProvider;
|
|
214
323
|
deepseek: (apiKey: string, baseUrl?: string) => AIProvider;
|
|
324
|
+
poe: (apiKey: string, baseUrl?: string) => AIProvider;
|
|
325
|
+
nova: (apiKey: string, baseUrl?: string) => AIProvider;
|
|
215
326
|
};
|
|
216
327
|
|
|
217
328
|
/**
|
|
@@ -310,6 +421,10 @@ declare class HuggingFaceProvider extends BaseProvider {
|
|
|
310
421
|
constructor(config: HuggingFaceConfig | string);
|
|
311
422
|
/**
|
|
312
423
|
* 发送聊天请求(非流式)
|
|
424
|
+
*
|
|
425
|
+
* reasoning 参数说明:
|
|
426
|
+
* - HuggingFace 是模型聚合平台,thinking 支持取决于具体模型
|
|
427
|
+
* - 如果模型支持,会返回 reasoning_content
|
|
313
428
|
*/
|
|
314
429
|
chat(options: ChatOptions): Promise<ChatResult>;
|
|
315
430
|
/**
|
package/dist/index.d.ts
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,25 +196,83 @@ 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 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): 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): 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): Promise<string>;
|
|
167
276
|
}
|
|
168
277
|
|
|
169
278
|
/**
|
|
@@ -174,7 +283,7 @@ declare abstract class BaseProvider implements AIProvider {
|
|
|
174
283
|
/**
|
|
175
284
|
* 支持的 Provider 类型
|
|
176
285
|
*/
|
|
177
|
-
type ProviderType = 'openrouter' | 'gemini' | 'groq' | 'huggingface' | 'modelscope' | 'deepseek';
|
|
286
|
+
type ProviderType = 'openrouter' | 'gemini' | 'groq' | 'huggingface' | 'modelscope' | 'deepseek' | 'poe' | 'nova';
|
|
178
287
|
/**
|
|
179
288
|
* 统一的 Provider 配置
|
|
180
289
|
*/
|
|
@@ -212,6 +321,8 @@ declare const ai: {
|
|
|
212
321
|
huggingface: (apiKey: string, baseUrl?: string) => AIProvider;
|
|
213
322
|
modelscope: (apiKey: string, baseUrl?: string) => AIProvider;
|
|
214
323
|
deepseek: (apiKey: string, baseUrl?: string) => AIProvider;
|
|
324
|
+
poe: (apiKey: string, baseUrl?: string) => AIProvider;
|
|
325
|
+
nova: (apiKey: string, baseUrl?: string) => AIProvider;
|
|
215
326
|
};
|
|
216
327
|
|
|
217
328
|
/**
|
|
@@ -310,6 +421,10 @@ declare class HuggingFaceProvider extends BaseProvider {
|
|
|
310
421
|
constructor(config: HuggingFaceConfig | string);
|
|
311
422
|
/**
|
|
312
423
|
* 发送聊天请求(非流式)
|
|
424
|
+
*
|
|
425
|
+
* reasoning 参数说明:
|
|
426
|
+
* - HuggingFace 是模型聚合平台,thinking 支持取决于具体模型
|
|
427
|
+
* - 如果模型支持,会返回 reasoning_content
|
|
313
428
|
*/
|
|
314
429
|
chat(options: ChatOptions): Promise<ChatResult>;
|
|
315
430
|
/**
|