@codehz/ai 0.4.6 → 0.7.1
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/README.md +221 -75
- package/dist/index.d.mts +670 -523
- package/dist/index.mjs +3677 -2207
- package/dist/index.mjs.map +1 -1
- package/package.json +19 -8
- package/.github/workflows/publish.yml +0 -56
- package/.oxfmtrc.json +0 -12
- package/.oxlintrc.json +0 -34
- package/AGENTS.md +0 -37
- package/src/adapters/chat-completions.ts +0 -624
- package/src/adapters/index.ts +0 -44
- package/src/adapters/messages.ts +0 -635
- package/src/adapters/mock.ts +0 -934
- package/src/adapters/ollama.ts +0 -526
- package/src/adapters/responses.ts +0 -818
- package/src/core/aggregator.ts +0 -428
- package/src/core/client.ts +0 -36
- package/src/core/collect-stream.ts +0 -19
- package/src/core/errors.ts +0 -105
- package/src/core/event-factory.ts +0 -151
- package/src/core/index.ts +0 -18
- package/src/core/merge-auxiliary.ts +0 -22
- package/src/core/normalize.ts +0 -65
- package/src/core/validation.ts +0 -404
- package/src/helpers/adapter-auxiliary.ts +0 -155
- package/src/helpers/adapter-base.ts +0 -218
- package/src/helpers/adapter-security.ts +0 -126
- package/src/helpers/auxiliary-collector.ts +0 -166
- package/src/helpers/incremental-stream-parser.ts +0 -142
- package/src/helpers/index.ts +0 -87
- package/src/helpers/mapping.ts +0 -192
- package/src/helpers/provider-request-options.ts +0 -25
- package/src/helpers/provider-stream.ts +0 -147
- package/src/helpers/reasoning-level.ts +0 -86
- package/src/helpers/request-mapper.ts +0 -94
- package/src/helpers/synthetic-stream.ts +0 -188
- package/src/helpers/usage-mapping.ts +0 -110
- package/src/index.ts +0 -17
- package/src/types/adapter.ts +0 -42
- package/src/types/content.ts +0 -15
- package/src/types/events.ts +0 -138
- package/src/types/index.ts +0 -49
- package/src/types/items.ts +0 -57
- package/src/types/request.ts +0 -52
- package/src/types/response.ts +0 -68
- package/tsdown.config.ts +0 -10
package/dist/index.d.mts
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
|
+
//#region src/types/kind.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Adapter kind 标识
|
|
4
|
+
*
|
|
5
|
+
* KnownAdapterKind 覆盖内置 adapter;
|
|
6
|
+
* AdapterKind 额外接受任意 string,便于自定义 backend 扩展(无需改库联合类型)。
|
|
7
|
+
*/
|
|
8
|
+
declare const KNOWN_ADAPTER_KINDS: readonly ["chat-completions", "messages", "responses", "ollama", "gemini", "mock"];
|
|
9
|
+
type KnownAdapterKind = (typeof KNOWN_ADAPTER_KINDS)[number];
|
|
10
|
+
/** 内置 kind 自动补全 + 自定义 string 扩展 */
|
|
11
|
+
type AdapterKind = KnownAdapterKind | (string & {});
|
|
12
|
+
//#endregion
|
|
1
13
|
//#region src/types/content.d.ts
|
|
2
14
|
/**
|
|
3
15
|
* ContentBlock — 统一内容块类型
|
|
@@ -25,11 +37,28 @@ type ContentBlock = InstructionBlock | {
|
|
|
25
37
|
};
|
|
26
38
|
//#endregion
|
|
27
39
|
//#region src/types/items.d.ts
|
|
40
|
+
type UrlCitation = {
|
|
41
|
+
type: "url";
|
|
42
|
+
url: string;
|
|
43
|
+
title?: string;
|
|
44
|
+
startIndex?: number;
|
|
45
|
+
endIndex?: number;
|
|
46
|
+
};
|
|
47
|
+
type ContainerFileCitation = {
|
|
48
|
+
type: "container_file";
|
|
49
|
+
containerId: string;
|
|
50
|
+
fileId: string;
|
|
51
|
+
filename?: string;
|
|
52
|
+
startIndex?: number;
|
|
53
|
+
endIndex?: number;
|
|
54
|
+
};
|
|
55
|
+
type Citation = UrlCitation | ContainerFileCitation;
|
|
28
56
|
type MessageItem = {
|
|
29
57
|
type: "message";
|
|
30
58
|
id?: string;
|
|
31
59
|
role: "user" | "assistant";
|
|
32
60
|
content: ContentBlock[];
|
|
61
|
+
citations?: Citation[];
|
|
33
62
|
};
|
|
34
63
|
type ReasoningItem = {
|
|
35
64
|
type: "reasoning";
|
|
@@ -57,10 +86,44 @@ type OpaqueItem = {
|
|
|
57
86
|
purpose: "replay" | "provider_state" | "unknown";
|
|
58
87
|
payload: unknown;
|
|
59
88
|
};
|
|
89
|
+
/** Provider 托管工具调用(调用方不执行) */
|
|
90
|
+
type ServerToolCallItem = {
|
|
91
|
+
type: "server_tool_call";
|
|
92
|
+
id: string;
|
|
93
|
+
tool: "web_search" | "code_execution" | "mcp" | string;
|
|
94
|
+
name?: string;
|
|
95
|
+
argumentsText?: string;
|
|
96
|
+
status?: "in_progress" | "completed" | "failed";
|
|
97
|
+
serverLabel?: string;
|
|
98
|
+
providerPayload?: unknown;
|
|
99
|
+
};
|
|
100
|
+
/** Provider 托管工具结果 */
|
|
101
|
+
type ServerToolResultItem = {
|
|
102
|
+
type: "server_tool_result";
|
|
103
|
+
id?: string;
|
|
104
|
+
callId: string;
|
|
105
|
+
tool: string;
|
|
106
|
+
outcome: "success" | "error";
|
|
107
|
+
content: ContentBlock[];
|
|
108
|
+
providerPayload?: unknown;
|
|
109
|
+
};
|
|
110
|
+
/** MCP 等远端工具发现列表 */
|
|
111
|
+
type ServerToolDiscoveryItem = {
|
|
112
|
+
type: "server_tool_discovery";
|
|
113
|
+
id: string;
|
|
114
|
+
tool: "mcp";
|
|
115
|
+
serverLabel: string;
|
|
116
|
+
tools: Array<{
|
|
117
|
+
name: string;
|
|
118
|
+
description?: string;
|
|
119
|
+
inputSchema?: unknown;
|
|
120
|
+
}>;
|
|
121
|
+
providerPayload?: unknown;
|
|
122
|
+
};
|
|
60
123
|
/** 可出现在请求 input 中的 item 类型 */
|
|
61
|
-
type InputItem = MessageItem | ReasoningItem | ToolCallItem | ToolResultItem | OpaqueItem;
|
|
62
|
-
/** 可出现在响应 output 中的 item
|
|
63
|
-
type OutputItem = MessageItem | ReasoningItem | ToolCallItem | OpaqueItem;
|
|
124
|
+
type InputItem = MessageItem | ReasoningItem | ToolCallItem | ToolResultItem | OpaqueItem | ServerToolCallItem | ServerToolResultItem | ServerToolDiscoveryItem;
|
|
125
|
+
/** 可出现在响应 output 中的 item 类型(不含客户端 ToolResultItem) */
|
|
126
|
+
type OutputItem = MessageItem | ReasoningItem | ToolCallItem | OpaqueItem | ServerToolCallItem | ServerToolResultItem | ServerToolDiscoveryItem;
|
|
64
127
|
/** replay 材料的类型等价于 InputItem */
|
|
65
128
|
type ReplayItem = InputItem;
|
|
66
129
|
//#endregion
|
|
@@ -74,6 +137,39 @@ type ToolChoice = "auto" | "none" | {
|
|
|
74
137
|
type: "tool";
|
|
75
138
|
name: string;
|
|
76
139
|
};
|
|
140
|
+
type WebSearchUserLocation = {
|
|
141
|
+
type: "approximate";
|
|
142
|
+
country?: string;
|
|
143
|
+
city?: string;
|
|
144
|
+
region?: string;
|
|
145
|
+
timezone?: string;
|
|
146
|
+
};
|
|
147
|
+
type WebSearchServerTool = {
|
|
148
|
+
type: "web_search";
|
|
149
|
+
allowedDomains?: string[];
|
|
150
|
+
blockedDomains?: string[];
|
|
151
|
+
userLocation?: WebSearchUserLocation;
|
|
152
|
+
searchContextSize?: "low" | "medium" | "high";
|
|
153
|
+
};
|
|
154
|
+
type CodeExecutionServerTool = {
|
|
155
|
+
type: "code_execution";
|
|
156
|
+
container?: {
|
|
157
|
+
type: "auto";
|
|
158
|
+
memoryLimit?: "1g" | "4g" | "16g" | "64g";
|
|
159
|
+
fileIds?: string[];
|
|
160
|
+
};
|
|
161
|
+
};
|
|
162
|
+
type McpServerTool = {
|
|
163
|
+
type: "mcp";
|
|
164
|
+
serverLabel: string;
|
|
165
|
+
serverUrl: string;
|
|
166
|
+
serverDescription?: string; /** 每请求由调用方提供;不得写入日志或 opaque 回放。 */
|
|
167
|
+
authorization?: string;
|
|
168
|
+
allowedTools?: string[]; /** 首版仅支持 never */
|
|
169
|
+
requireApproval: "never";
|
|
170
|
+
};
|
|
171
|
+
/** Provider 托管执行的工具声明(不进客户端 tool loop) */
|
|
172
|
+
type ServerToolDefinition = WebSearchServerTool | CodeExecutionServerTool | McpServerTool;
|
|
77
173
|
type IncludeSettings = {
|
|
78
174
|
usage?: "off" | "best_effort";
|
|
79
175
|
billing?: "off" | "best_effort";
|
|
@@ -81,10 +177,15 @@ type IncludeSettings = {
|
|
|
81
177
|
};
|
|
82
178
|
/** Portable reasoning / thinking effort. Mapped per-adapter to provider wire fields. */
|
|
83
179
|
type ReasoningLevel = "none" | "minimal" | "low" | "medium" | "high" | "xhigh" | "max";
|
|
180
|
+
/** 可移植 reasoning level 枚举(单源;validation / provider 共用)。 */
|
|
181
|
+
declare const REASONING_LEVELS: readonly ["none", "minimal", "low", "medium", "high", "xhigh", "max"];
|
|
182
|
+
/** 用于校验任意字符串 membership;值域与 REASONING_LEVELS 一致。 */
|
|
183
|
+
declare const REASONING_LEVEL_SET: ReadonlySet<string>;
|
|
84
184
|
type AIRequest = {
|
|
85
185
|
instructions?: string | InstructionBlock[];
|
|
86
186
|
input: InputItem[];
|
|
87
|
-
tools?: ToolDefinition[];
|
|
187
|
+
tools?: ToolDefinition[]; /** Provider 托管工具(web_search / code_execution / mcp 等);与 tools 可共存 */
|
|
188
|
+
serverTools?: ServerToolDefinition[];
|
|
88
189
|
toolChoice?: ToolChoice;
|
|
89
190
|
include?: IncludeSettings;
|
|
90
191
|
metadata?: Record<string, string>;
|
|
@@ -93,12 +194,58 @@ type AIRequest = {
|
|
|
93
194
|
/**
|
|
94
195
|
* Portable reasoning effort. Adapters map this to provider-native fields
|
|
95
196
|
* (e.g. Responses `reasoning.effort`, Chat Completions `reasoning_effort`,
|
|
96
|
-
* Messages `thinking`, Ollama `think`).
|
|
197
|
+
* Messages `thinking`, Ollama `think`, Gemini `thinkingConfig`).
|
|
198
|
+
* Unsupported levels throw.
|
|
97
199
|
*/
|
|
98
200
|
reasoningLevel?: ReasoningLevel; /** AbortSignal 用于打断请求。abort 时 fetch 调用会被取消,流迭代器抛出 AbortError。 */
|
|
99
201
|
signal?: AbortSignal;
|
|
100
202
|
};
|
|
101
203
|
//#endregion
|
|
204
|
+
//#region src/types/warning-codes.d.ts
|
|
205
|
+
/**
|
|
206
|
+
* 标准 warning 代码 — types 层单源
|
|
207
|
+
*
|
|
208
|
+
* runtime.errors 与 events 均从此导出,避免手工双表漂移。
|
|
209
|
+
*/
|
|
210
|
+
declare const WarningCode: {
|
|
211
|
+
/** replay fidelity 低于预期 */readonly REPLAY_FIDELITY_LOW: "REPLAY_FIDELITY_LOW"; /** usage 字段缺失 */
|
|
212
|
+
readonly USAGE_MISSING: "USAGE_MISSING"; /** billing 字段缺失 */
|
|
213
|
+
readonly BILLING_MISSING: "BILLING_MISSING"; /** billing 只能给估算值 */
|
|
214
|
+
readonly BILLING_ESTIMATED: "BILLING_ESTIMATED"; /** follow-up lookup 失败 */
|
|
215
|
+
readonly LOOKUP_FAILED: "LOOKUP_FAILED"; /** lookup 超时 */
|
|
216
|
+
readonly LOOKUP_TIMEOUT: "LOOKUP_TIMEOUT"; /** 流提前中断 */
|
|
217
|
+
readonly STREAM_INCOMPLETE: "STREAM_INCOMPLETE"; /** 流帧/行解析失败 */
|
|
218
|
+
readonly STREAM_ERROR: "STREAM_ERROR"; /** 能力降级 */
|
|
219
|
+
readonly CAPABILITY_DOWNGRADE: "CAPABILITY_DOWNGRADE"; /** 模拟流式 */
|
|
220
|
+
readonly SYNTHETIC_STREAM: "SYNTHETIC_STREAM"; /** 工具调用以批量方式到达(非 token 级流式) */
|
|
221
|
+
readonly TOOL_CALL_BATCHED: "TOOL_CALL_BATCHED"; /** AIMappingError 降级为 warning */
|
|
222
|
+
readonly MAPPING_ERROR: "MAPPING_ERROR"; /** 入站 tool_call.argumentsText 非合法 JSON object(object-wire adapter soft-complete) */
|
|
223
|
+
readonly TOOL_CALL_ARGUMENTS_INVALID: "TOOL_CALL_ARGUMENTS_INVALID"; /** 请求 metadata 不被该 adapter 支持 */
|
|
224
|
+
readonly UNSUPPORTED_METADATA: "UNSUPPORTED_METADATA"; /** 重复 finish / done 信号被忽略 */
|
|
225
|
+
readonly DUPLICATE_FINISH: "DUPLICATE_FINISH"; /** provider 发出未知事件类型 */
|
|
226
|
+
readonly UNKNOWN_PROVIDER_EVENT: "UNKNOWN_PROVIDER_EVENT"; /** 内容被安全/策略过滤 */
|
|
227
|
+
readonly CONTENT_FILTER: "CONTENT_FILTER"; /** 多 choice 仅支持 index 0,其余忽略 */
|
|
228
|
+
readonly MULTIPLE_CHOICES_IGNORED: "MULTIPLE_CHOICES_IGNORED"; /** MCP 审批流不被支持 */
|
|
229
|
+
readonly MCP_APPROVAL_REQUIRED: "MCP_APPROVAL_REQUIRED"; /** provider 侧 response.failed 等失败 */
|
|
230
|
+
readonly PROVIDER_FAILURE: "PROVIDER_FAILURE"; /** 出站 opaque 超限被省略(避免下一轮 accept 自产毒) */
|
|
231
|
+
readonly OPAQUE_REPLAY_OMITTED: "OPAQUE_REPLAY_OMITTED";
|
|
232
|
+
};
|
|
233
|
+
type WarningCodeName = (typeof WarningCode)[keyof typeof WarningCode];
|
|
234
|
+
/** 与 WarningCodeName 同义;保留以兼容既有 KnownWarningCode 命名 */
|
|
235
|
+
type KnownWarningCode = WarningCodeName;
|
|
236
|
+
/** 已知码 + 开放字符串扩展 */
|
|
237
|
+
type WarningCodeValue = KnownWarningCode | (string & {});
|
|
238
|
+
/** 结构化 warning(AIResponse / response.completed / factory 共用) */
|
|
239
|
+
type StreamWarning = {
|
|
240
|
+
message: string;
|
|
241
|
+
code?: WarningCodeValue;
|
|
242
|
+
};
|
|
243
|
+
/**
|
|
244
|
+
* 去重键:以 message 为主(与旧 string[] 行为一致)。
|
|
245
|
+
* 同一 message 若先无 code 后有 code,保留先到的那条。
|
|
246
|
+
*/
|
|
247
|
+
declare function streamWarningKey(warning: StreamWarning): string;
|
|
248
|
+
//#endregion
|
|
102
249
|
//#region src/types/response.d.ts
|
|
103
250
|
type StopReason = "end_turn" | "tool_call" | "max_output_tokens" | "content_filter" | "error" | "unknown";
|
|
104
251
|
type Usage = {
|
|
@@ -126,22 +273,24 @@ type AuxiliaryInfo = {
|
|
|
126
273
|
type BackendTrace = {
|
|
127
274
|
requestId?: string;
|
|
128
275
|
rawResponseId?: string;
|
|
129
|
-
adapter:
|
|
276
|
+
adapter: AdapterKind;
|
|
130
277
|
isSyntheticStream: boolean;
|
|
131
278
|
metadataSources?: string[];
|
|
132
|
-
warnings?:
|
|
279
|
+
warnings?: StreamWarning[];
|
|
133
280
|
};
|
|
134
281
|
type AIResponse = {
|
|
135
282
|
id?: string;
|
|
136
283
|
output: OutputItem[];
|
|
137
284
|
replay: ReplayItem[];
|
|
138
285
|
text: string;
|
|
139
|
-
toolCalls: ToolCallItem[];
|
|
286
|
+
toolCalls: ToolCallItem[]; /** Provider 托管工具调用列表(web_search / code_execution / mcp 等) */
|
|
287
|
+
serverToolCalls: ServerToolCallItem[]; /** Provider 托管工具结果列表 */
|
|
288
|
+
serverToolResults: ServerToolResultItem[];
|
|
140
289
|
stopReason?: StopReason;
|
|
141
290
|
usage?: Usage;
|
|
142
291
|
billing?: BillingInfo;
|
|
143
|
-
auxiliary?: AuxiliaryInfo;
|
|
144
|
-
warnings?:
|
|
292
|
+
auxiliary?: AuxiliaryInfo; /** 结构化 warning;权威源含 message + 可选 code */
|
|
293
|
+
warnings?: StreamWarning[];
|
|
145
294
|
backend: BackendTrace;
|
|
146
295
|
};
|
|
147
296
|
//#endregion
|
|
@@ -152,7 +301,7 @@ type StreamEventBase = {
|
|
|
152
301
|
sequence: number;
|
|
153
302
|
timestamp: string;
|
|
154
303
|
backend: {
|
|
155
|
-
kind:
|
|
304
|
+
kind: AdapterKind;
|
|
156
305
|
isSynthetic: boolean;
|
|
157
306
|
};
|
|
158
307
|
};
|
|
@@ -163,7 +312,7 @@ type ResponseStartedEvent = StreamEventBase & {
|
|
|
163
312
|
type ResponseWarningEvent = StreamEventBase & {
|
|
164
313
|
type: "response.warning";
|
|
165
314
|
message: string;
|
|
166
|
-
code?:
|
|
315
|
+
code?: WarningCodeValue;
|
|
167
316
|
};
|
|
168
317
|
type ResponseAuxiliaryEvent = StreamEventBase & {
|
|
169
318
|
type: "response.auxiliary";
|
|
@@ -178,7 +327,7 @@ type ResponseCompletedEvent = StreamEventBase & {
|
|
|
178
327
|
usage?: Usage;
|
|
179
328
|
billing?: BillingInfo;
|
|
180
329
|
auxiliary?: AuxiliaryInfo;
|
|
181
|
-
warnings?:
|
|
330
|
+
warnings?: StreamWarning[];
|
|
182
331
|
opaqueOutput?: OpaqueItem[];
|
|
183
332
|
trace?: Partial<BackendTrace>;
|
|
184
333
|
};
|
|
@@ -197,6 +346,7 @@ type MessageDeltaEvent = StreamEventBase & {
|
|
|
197
346
|
type MessageCompletedEvent = StreamEventBase & {
|
|
198
347
|
type: "message.completed";
|
|
199
348
|
itemId: string;
|
|
349
|
+
citations?: Citation[];
|
|
200
350
|
};
|
|
201
351
|
type ReasoningStartedEvent = StreamEventBase & {
|
|
202
352
|
type: "reasoning.started";
|
|
@@ -232,7 +382,37 @@ type ToolCallCompletedEvent = StreamEventBase & {
|
|
|
232
382
|
type: "tool_call.completed";
|
|
233
383
|
itemId: string;
|
|
234
384
|
};
|
|
235
|
-
type
|
|
385
|
+
type ServerToolStartedEvent = StreamEventBase & {
|
|
386
|
+
type: "server_tool.started";
|
|
387
|
+
item: {
|
|
388
|
+
id: string;
|
|
389
|
+
tool: string;
|
|
390
|
+
name?: string;
|
|
391
|
+
serverLabel?: string;
|
|
392
|
+
};
|
|
393
|
+
};
|
|
394
|
+
type ServerToolDeltaEvent = StreamEventBase & {
|
|
395
|
+
type: "server_tool.delta";
|
|
396
|
+
itemId: string;
|
|
397
|
+
delta: {
|
|
398
|
+
argumentsText?: string;
|
|
399
|
+
};
|
|
400
|
+
};
|
|
401
|
+
type ServerToolCompletedEvent = StreamEventBase & {
|
|
402
|
+
type: "server_tool.completed";
|
|
403
|
+
itemId: string;
|
|
404
|
+
status?: "completed" | "failed";
|
|
405
|
+
providerPayload?: unknown;
|
|
406
|
+
};
|
|
407
|
+
type ServerToolResultCompletedEvent = StreamEventBase & {
|
|
408
|
+
type: "server_tool_result.completed";
|
|
409
|
+
item: ServerToolResultItem;
|
|
410
|
+
};
|
|
411
|
+
type ServerToolDiscoveryCompletedEvent = StreamEventBase & {
|
|
412
|
+
type: "server_tool_discovery.completed";
|
|
413
|
+
item: ServerToolDiscoveryItem;
|
|
414
|
+
};
|
|
415
|
+
type AIStreamEvent = ResponseStartedEvent | ResponseWarningEvent | ResponseAuxiliaryEvent | MessageStartedEvent | MessageDeltaEvent | MessageCompletedEvent | ReasoningStartedEvent | ReasoningDeltaEvent | ReasoningCompletedEvent | ToolCallStartedEvent | ToolCallDeltaEvent | ToolCallCompletedEvent | ServerToolStartedEvent | ServerToolDeltaEvent | ServerToolCompletedEvent | ServerToolResultCompletedEvent | ServerToolDiscoveryCompletedEvent | ResponseCompletedEvent;
|
|
236
416
|
//#endregion
|
|
237
417
|
//#region src/types/adapter.d.ts
|
|
238
418
|
/** HTTP fetch 函数签名,用于注入自定义请求实现(测试/代理) */
|
|
@@ -242,7 +422,7 @@ type NormalizedRequest = AIRequest & {
|
|
|
242
422
|
requestId: string;
|
|
243
423
|
};
|
|
244
424
|
interface BackendAdapter {
|
|
245
|
-
readonly kind:
|
|
425
|
+
readonly kind: AdapterKind;
|
|
246
426
|
readonly isSyntheticStream: boolean;
|
|
247
427
|
stream(request: NormalizedRequest): AsyncIterable<AIStreamEvent>;
|
|
248
428
|
}
|
|
@@ -256,50 +436,39 @@ interface AIClient {
|
|
|
256
436
|
stream(request: AIRequest): AsyncIterable<AIStreamEvent>;
|
|
257
437
|
}
|
|
258
438
|
//#endregion
|
|
259
|
-
//#region src/
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
//#region src/core/normalize.d.ts
|
|
263
|
-
type NormalizeOptions = {
|
|
439
|
+
//#region src/types/compress.d.ts
|
|
440
|
+
/** 显式上下文压缩请求(独立于 stream) */
|
|
441
|
+
type CompressRequest = {
|
|
264
442
|
model: string;
|
|
265
|
-
|
|
443
|
+
input: InputItem[];
|
|
444
|
+
instructions?: string | InstructionBlock[];
|
|
445
|
+
include?: IncludeSettings; /** AbortSignal 用于打断压缩请求。 */
|
|
446
|
+
signal?: AbortSignal;
|
|
266
447
|
};
|
|
267
|
-
/**
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
*/
|
|
274
|
-
declare function normalizeRequest(request: AIRequest, options: NormalizeOptions): NormalizedRequest;
|
|
275
|
-
//#endregion
|
|
276
|
-
//#region src/core/validation.d.ts
|
|
277
|
-
type ValidationIssue = {
|
|
278
|
-
field: string;
|
|
279
|
-
code: string;
|
|
280
|
-
message: string;
|
|
448
|
+
/** 压缩结果:调用方用 replay 替换旧 transcript(非 append 全文) */
|
|
449
|
+
type CompressResult = {
|
|
450
|
+
replay: ReplayItem[];
|
|
451
|
+
usage?: Usage;
|
|
452
|
+
auxiliary?: AuxiliaryInfo;
|
|
453
|
+
rawResponseId?: string;
|
|
281
454
|
};
|
|
282
|
-
/**
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
* 校验请求并抛出首个问题。
|
|
289
|
-
* 适用于客户端入口的快速失败检查。
|
|
290
|
-
*/
|
|
291
|
-
declare function assertValidRequest(request: AIRequest): void;
|
|
455
|
+
/** Adapter 可选能力:原生上下文压缩 */
|
|
456
|
+
interface ContextCompressCapable {
|
|
457
|
+
compress(request: CompressRequest): Promise<CompressResult>;
|
|
458
|
+
}
|
|
459
|
+
/** 探测 adapter 是否实现 compress(不依赖 kind 硬编码)。 */
|
|
460
|
+
declare function supportsContextCompress(adapter: BackendAdapter): adapter is BackendAdapter & ContextCompressCapable;
|
|
292
461
|
//#endregion
|
|
293
|
-
//#region src/
|
|
462
|
+
//#region src/runtime/client.d.ts
|
|
463
|
+
declare function createAIClient(options: CreateAIClientOptions): AIClient;
|
|
464
|
+
//#endregion
|
|
465
|
+
//#region src/runtime/errors.d.ts
|
|
294
466
|
/**
|
|
295
|
-
*
|
|
296
|
-
*
|
|
297
|
-
* 把失败、降级、断流三类情况明确区分:
|
|
298
|
-
* - 致命错误 → 同步抛错或迭代器抛错
|
|
299
|
-
* - 非致命差异 → warning 通道
|
|
300
|
-
* - 流中断 → 不伪造 response.completed
|
|
467
|
+
* 已知错误码。保留补全;未知码用 `string & {}` 扩展,避免 `| string` 吞掉字面量提示。
|
|
468
|
+
* ValidationIssue.code 可更细,不强制全部列入此处;以作为 AIError.code 传入的码为主。
|
|
301
469
|
*/
|
|
302
|
-
type
|
|
470
|
+
type KnownErrorCode = "INPUT_EMPTY" | "TEMPERATURE_OUT_OF_RANGE" | "MAX_OUTPUT_TOKENS_INVALID" | "TOOL_CHOICE_NO_TOOLS" | "TOOL_CHOICE_UNKNOWN_TOOL" | "TOOL_CALL_ARGUMENTS_INVALID" | "PROVIDER_ERROR" | "AUTH_ERROR" | "STREAM_ERROR" | "STREAM_PROTOCOL_ERROR" | "MAPPING_ERROR" | "STREAM_INCOMPLETE" | "LOOKUP_FAILED" | "LOOKUP_TIMEOUT" | "INVALID_OPAQUE_REPLAY" | "UNSUPPORTED_CONTENT_BLOCK" | "UNSUPPORTED_SERVER_TOOL" | "UNSUPPORTED_REASONING_LEVEL" | "UNSUPPORTED_COMPRESS" | "MOCK_CONCURRENT_STREAM" | "MOCK_COMPRESS_NOT_CONFIGURED" | "MOCK_EXPECTATION_FAILED" | "MOCK_STREAM_CONFIG_INVALID" | "MOCK_OPAQUE_OUTPUT" | "MOCK_MESSAGE_ID_MISSING" | "MOCK_REASONING_ID_MISSING";
|
|
471
|
+
type ErrorCode = KnownErrorCode | (string & {});
|
|
303
472
|
declare class AIError extends Error {
|
|
304
473
|
readonly code: ErrorCode;
|
|
305
474
|
readonly name: string;
|
|
@@ -318,40 +487,109 @@ declare class AIRequestError extends AIError {
|
|
|
318
487
|
message: string;
|
|
319
488
|
}[] | undefined);
|
|
320
489
|
}
|
|
321
|
-
/**
|
|
490
|
+
/**
|
|
491
|
+
* Provider 调用失败 — HTTP 非 2xx、网络错误。
|
|
492
|
+
* AdapterBase **rethrow**(致命),不会转为 warning。
|
|
493
|
+
*/
|
|
322
494
|
declare class AIProviderError extends AIError {
|
|
323
495
|
readonly statusCode?: number | undefined;
|
|
324
496
|
readonly responseBody?: string | undefined;
|
|
325
497
|
constructor(message: string, code: ErrorCode, statusCode?: number | undefined, responseBody?: string | undefined);
|
|
326
498
|
}
|
|
327
|
-
/**
|
|
499
|
+
/**
|
|
500
|
+
* 流协议/传输损坏 — SSE 解析失败、chunk 格式异常、body 不可读等。
|
|
501
|
+
* 致命:同步或在异步迭代中抛出,不伪造 response.completed。
|
|
502
|
+
*/
|
|
328
503
|
declare class AIStreamError extends AIError {
|
|
329
504
|
constructor(message: string, code: ErrorCode);
|
|
330
505
|
}
|
|
331
|
-
/**
|
|
506
|
+
/**
|
|
507
|
+
* Canonical 映射失败 — 无法将 provider 响应映射到 canonical 类型。
|
|
508
|
+
*
|
|
509
|
+
* AdapterBase 捕获后降级为:
|
|
510
|
+
* - `response.warning`(code = MAPPING_ERROR)
|
|
511
|
+
* - 空 output 的 `response.completed`
|
|
512
|
+
*
|
|
513
|
+
* 生产 adapter 原则上不应抛出;此路径是协议级降级通道(测试 / 防御性边界)。
|
|
514
|
+
*/
|
|
332
515
|
declare class AIMappingError extends AIError {
|
|
333
516
|
constructor(message: string, code: ErrorCode);
|
|
334
517
|
}
|
|
335
518
|
/**
|
|
336
|
-
*
|
|
337
|
-
*
|
|
519
|
+
* 可恢复的回合失败 — buildRequest / runStream 中可安全 soft-complete 的语义错误。
|
|
520
|
+
*
|
|
521
|
+
* AdapterBase 在 `response.started` 之后捕获后降级为:
|
|
522
|
+
* - `response.warning`(code = 错误 code,通常对齐 WarningCode)
|
|
523
|
+
* - 空 replay 的 `response.completed`(`stopReason` 默认 `"error"`)
|
|
524
|
+
*
|
|
525
|
+
* 与 AIMappingError 的区别:recoverable 携带显式 stopReason,表示调用方应清理
|
|
526
|
+
* 中毒历史后重试;mapping 是协议级降级通道,通常不带 stopReason。
|
|
338
527
|
*/
|
|
339
|
-
declare
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
528
|
+
declare class AIRecoverableError extends AIError {
|
|
529
|
+
readonly stopReason: StopReason;
|
|
530
|
+
constructor(message: string, code: ErrorCode, stopReason?: StopReason);
|
|
531
|
+
}
|
|
532
|
+
//#endregion
|
|
533
|
+
//#region src/stream/collect-stream.d.ts
|
|
534
|
+
declare function collectStream(stream: AsyncIterable<AIStreamEvent>): Promise<AIResponse>;
|
|
535
|
+
//#endregion
|
|
536
|
+
//#region src/canonical/stop-reason.d.ts
|
|
537
|
+
declare function mapStopReason(providerReason: string): StopReason;
|
|
538
|
+
declare function mapReasoningVisibility(hasThinking: boolean, hasRedacted: boolean): ReasoningItem["visibility"];
|
|
539
|
+
//#endregion
|
|
540
|
+
//#region src/canonical/content.d.ts
|
|
541
|
+
declare function textBlock(text: string): ContentBlock & {
|
|
542
|
+
type: "text";
|
|
543
|
+
};
|
|
544
|
+
declare function jsonBlock(json: unknown): ContentBlock & {
|
|
545
|
+
type: "json";
|
|
546
|
+
};
|
|
547
|
+
declare function imageBlock(imageUrl: string): ContentBlock & {
|
|
548
|
+
type: "image";
|
|
549
|
+
};
|
|
550
|
+
declare function opaqueBlock(payload: unknown): ContentBlock & {
|
|
551
|
+
type: "opaque";
|
|
350
552
|
};
|
|
553
|
+
/**
|
|
554
|
+
* 将单个 ContentBlock 转为纯文本。
|
|
555
|
+
* text 块直接返回文本,json 块序列化,其余返回空串。
|
|
556
|
+
*/
|
|
557
|
+
declare function blockToText(b: ContentBlock): string;
|
|
558
|
+
/**
|
|
559
|
+
* 将 ContentBlock 数组拼接为纯文本,块间以换行符分隔。
|
|
560
|
+
*/
|
|
561
|
+
declare function contentBlocksToText(blocks: ContentBlock[]): string;
|
|
562
|
+
/**
|
|
563
|
+
* 合并相邻 text content blocks(直接拼接、不插入分隔符)。
|
|
564
|
+
* 非 text block 保留边界。供 aggregator 与 StreamingItemSession 共用。
|
|
565
|
+
*/
|
|
566
|
+
declare function coalesceContentBlocks(blocks: readonly ContentBlock[]): ContentBlock[];
|
|
567
|
+
//#endregion
|
|
568
|
+
//#region src/canonical/items.d.ts
|
|
569
|
+
declare function messageItem(content: ContentBlock[], overrides?: Partial<Omit<MessageItem, "type" | "content">>): MessageItem;
|
|
570
|
+
declare function reasoningItem(content: ContentBlock[], visibility?: ReasoningItem["visibility"], id?: string): ReasoningItem;
|
|
571
|
+
declare function toolCallItem(id: string, name: string, argumentsText: string): ToolCallItem;
|
|
572
|
+
declare function toolResultItem(callId: string, toolName: string, outcome: ToolResultItem["outcome"], content: ContentBlock[]): ToolResultItem;
|
|
573
|
+
declare function opaqueItem(source: OpaqueItem["source"], purpose: OpaqueItem["purpose"], payload: unknown, id?: string): OpaqueItem;
|
|
574
|
+
declare function serverToolCallItem(id: string, tool: ServerToolCallItem["tool"], overrides?: Partial<Omit<ServerToolCallItem, "type" | "id" | "tool">>): ServerToolCallItem;
|
|
575
|
+
declare function serverToolResultItem(callId: string, tool: string, outcome: ServerToolResultItem["outcome"], content: ContentBlock[], overrides?: Partial<Omit<ServerToolResultItem, "type" | "callId" | "tool" | "outcome" | "content">>): ServerToolResultItem;
|
|
576
|
+
declare function serverToolDiscoveryItem(id: string, serverLabel: string, tools: ServerToolDiscoveryItem["tools"], overrides?: Partial<Omit<ServerToolDiscoveryItem, "type" | "id" | "tool" | "serverLabel" | "tools">>): ServerToolDiscoveryItem;
|
|
577
|
+
//#endregion
|
|
578
|
+
//#region src/canonical/replay.d.ts
|
|
579
|
+
/**
|
|
580
|
+
* 从 output items 构建标准 replay items。
|
|
581
|
+
* 简单场景下 replay 与 output 一致。
|
|
582
|
+
* 复杂场景(需要 opaque continuation)由 adapter 自行扩展。
|
|
583
|
+
*/
|
|
584
|
+
declare function replayFromOutput(output: readonly OutputItem[]): ReplayItem[];
|
|
585
|
+
/**
|
|
586
|
+
* 从 OutputItem 数组中提取所有 message 类型 item 的文本内容。
|
|
587
|
+
*/
|
|
588
|
+
declare function extractText(output: OutputItem[]): string;
|
|
351
589
|
//#endregion
|
|
352
|
-
//#region src/
|
|
590
|
+
//#region src/stream/event-factory.d.ts
|
|
353
591
|
type EventFactoryBackend = {
|
|
354
|
-
kind:
|
|
592
|
+
kind: AdapterKind;
|
|
355
593
|
isSynthetic: boolean;
|
|
356
594
|
};
|
|
357
595
|
type EventFactoryState = {
|
|
@@ -360,7 +598,7 @@ type EventFactoryState = {
|
|
|
360
598
|
};
|
|
361
599
|
declare function createEventFactory(state: EventFactoryState): {
|
|
362
600
|
responseStarted(model: string): ResponseStartedEvent;
|
|
363
|
-
responseWarning(message: string, code?:
|
|
601
|
+
responseWarning(message: string, code?: WarningCodeValue): ResponseWarningEvent;
|
|
364
602
|
responseAuxiliary(data: {
|
|
365
603
|
usage?: Usage;
|
|
366
604
|
billing?: BillingInfo;
|
|
@@ -372,13 +610,15 @@ declare function createEventFactory(state: EventFactoryState): {
|
|
|
372
610
|
usage?: Usage;
|
|
373
611
|
billing?: BillingInfo;
|
|
374
612
|
auxiliary?: AuxiliaryInfo;
|
|
375
|
-
warnings?:
|
|
613
|
+
warnings?: StreamWarning[];
|
|
376
614
|
opaqueOutput?: OpaqueItem[];
|
|
377
615
|
trace?: Partial<BackendTrace>;
|
|
378
616
|
}): ResponseCompletedEvent;
|
|
379
617
|
messageStarted(id: string): MessageStartedEvent;
|
|
380
618
|
messageDelta(itemId: string, delta: ContentBlock): MessageDeltaEvent;
|
|
381
|
-
messageCompleted(itemId: string
|
|
619
|
+
messageCompleted(itemId: string, options?: {
|
|
620
|
+
citations?: Citation[];
|
|
621
|
+
}): MessageCompletedEvent;
|
|
382
622
|
reasoningStarted(id: string, visibility: ReasoningItem["visibility"]): ReasoningStartedEvent;
|
|
383
623
|
reasoningDelta(itemId: string, delta: ContentBlock): ReasoningDeltaEvent;
|
|
384
624
|
reasoningCompleted(itemId: string): ReasoningCompletedEvent;
|
|
@@ -386,23 +626,26 @@ declare function createEventFactory(state: EventFactoryState): {
|
|
|
386
626
|
toolCallDelta(itemId: string, delta: {
|
|
387
627
|
argumentsText?: string;
|
|
388
628
|
}): ToolCallDeltaEvent;
|
|
389
|
-
toolCallCompleted(itemId: string): ToolCallCompletedEvent;
|
|
629
|
+
toolCallCompleted(itemId: string): ToolCallCompletedEvent;
|
|
630
|
+
serverToolStarted(id: string, tool: string, options?: {
|
|
631
|
+
name?: string;
|
|
632
|
+
serverLabel?: string;
|
|
633
|
+
}): ServerToolStartedEvent;
|
|
634
|
+
serverToolDelta(itemId: string, delta: {
|
|
635
|
+
argumentsText?: string;
|
|
636
|
+
}): ServerToolDeltaEvent;
|
|
637
|
+
serverToolCompleted(itemId: string, options?: {
|
|
638
|
+
status?: "completed" | "failed";
|
|
639
|
+
providerPayload?: unknown;
|
|
640
|
+
}): ServerToolCompletedEvent;
|
|
641
|
+
serverToolResultCompleted(item: ServerToolResultItem): ServerToolResultCompletedEvent;
|
|
642
|
+
serverToolDiscoveryCompleted(item: ServerToolDiscoveryItem): ServerToolDiscoveryCompletedEvent; /** 返回当前已发出的 sequence 计数(用于断言) */
|
|
390
643
|
readonly sequence: number; /** 返回当前已记录的 warning 副本。 */
|
|
391
|
-
readonly warnings:
|
|
644
|
+
readonly warnings: StreamWarning[];
|
|
392
645
|
};
|
|
393
646
|
type EventFactory = ReturnType<typeof createEventFactory>;
|
|
394
647
|
//#endregion
|
|
395
|
-
//#region src/
|
|
396
|
-
/**
|
|
397
|
-
* 将事件数组聚合为 AIResponse。
|
|
398
|
-
* 适用于测试和离线处理场景。
|
|
399
|
-
*/
|
|
400
|
-
declare function aggregateEvents(events: readonly AIStreamEvent[]): AIResponse;
|
|
401
|
-
//#endregion
|
|
402
|
-
//#region src/core/collect-stream.d.ts
|
|
403
|
-
declare function collectStream(stream: AsyncIterable<AIStreamEvent>): Promise<AIResponse>;
|
|
404
|
-
//#endregion
|
|
405
|
-
//#region src/helpers/auxiliary-collector.d.ts
|
|
648
|
+
//#region src/provider/auxiliary-collector.d.ts
|
|
406
649
|
type UsageSource = NonNullable<AuxiliaryInfo["usageSource"]>;
|
|
407
650
|
type BillingSource = NonNullable<AuxiliaryInfo["billingSource"]>;
|
|
408
651
|
type LookupResult = {
|
|
@@ -410,70 +653,26 @@ type LookupResult = {
|
|
|
410
653
|
billing?: Partial<BillingInfo>;
|
|
411
654
|
providerMetadata?: Record<string, unknown>;
|
|
412
655
|
};
|
|
413
|
-
declare class AuxiliaryCollector {
|
|
414
|
-
private usage;
|
|
415
|
-
private usageSource;
|
|
416
|
-
private billing;
|
|
417
|
-
private billingSource;
|
|
418
|
-
private providerMetadata;
|
|
419
|
-
private providerUsage;
|
|
420
|
-
private providerBilling;
|
|
421
|
-
private warnings;
|
|
422
|
-
private lookupAttempted;
|
|
423
|
-
/**
|
|
424
|
-
* 记录 usage 信息。
|
|
425
|
-
* 后调用的覆盖先调用的(优先级由调用方控制)。
|
|
426
|
-
*/
|
|
427
|
-
recordUsage(usage: Partial<Usage>, source: UsageSource, raw?: unknown): this;
|
|
428
|
-
/**
|
|
429
|
-
* 记录 billing 信息。
|
|
430
|
-
* 后调用的覆盖先调用的。
|
|
431
|
-
*/
|
|
432
|
-
recordBilling(billing: Partial<BillingInfo>, source: BillingSource, raw?: unknown): this;
|
|
433
|
-
/**
|
|
434
|
-
* 记录 provider 元数据(非 canonical 的 key-value 信息)。
|
|
435
|
-
*/
|
|
436
|
-
recordMetadata(metadata: Record<string, unknown>): this;
|
|
437
|
-
/**
|
|
438
|
-
* 记录一条 warning。
|
|
439
|
-
*/
|
|
440
|
-
recordWarning(message: string): this;
|
|
441
|
-
/**
|
|
442
|
-
* 执行一次有界 follow-up lookup。
|
|
443
|
-
* 最多调用一次;后续调用被忽略。
|
|
444
|
-
* lookup 失败(抛错)仅记录 warning,不传播异常。
|
|
445
|
-
*/
|
|
446
|
-
tryLookup(lookupFn: () => Promise<LookupResult>, timeoutMs?: number): Promise<void>;
|
|
447
|
-
/**
|
|
448
|
-
* 构建最终的 usage / billing / auxiliary。
|
|
449
|
-
* 所有字段均为可选的 — 拿不到就不给。
|
|
450
|
-
*/
|
|
451
|
-
build(): {
|
|
452
|
-
usage?: Usage;
|
|
453
|
-
billing?: BillingInfo;
|
|
454
|
-
auxiliary?: AuxiliaryInfo;
|
|
455
|
-
warnings?: string[];
|
|
456
|
-
};
|
|
457
|
-
/**
|
|
458
|
-
* 已使用的来源列表(用于 debugging)。
|
|
459
|
-
*/
|
|
460
|
-
get sources(): {
|
|
461
|
-
usage?: UsageSource;
|
|
462
|
-
billing?: BillingSource;
|
|
463
|
-
};
|
|
464
|
-
}
|
|
465
656
|
//#endregion
|
|
466
|
-
//#region src/
|
|
657
|
+
//#region src/provider/auxiliary.d.ts
|
|
467
658
|
type MaybePromise<T> = T | Promise<T>;
|
|
659
|
+
/**
|
|
660
|
+
* 实验性:由调用方接线;库内 HTTP adapter **未**默认启用。
|
|
661
|
+
* 用于从 usage/auxiliary 派生 billing 的后处理钩子。
|
|
662
|
+
*/
|
|
468
663
|
type BillingPostprocessHook = (context: {
|
|
469
664
|
request: NormalizedRequest;
|
|
470
665
|
usage?: Usage;
|
|
471
666
|
billing?: BillingInfo;
|
|
472
667
|
auxiliary?: AuxiliaryInfo;
|
|
473
668
|
}) => MaybePromise<Partial<BillingInfo> | undefined>;
|
|
669
|
+
/**
|
|
670
|
+
* finalize 选项。`lookup` / `postprocessBilling` 为 experimental unused 扩展点:
|
|
671
|
+
* 生产 adapter 当前不传入;仅测试或宿主自定义 wiring 使用。
|
|
672
|
+
*/
|
|
474
673
|
type AuxiliaryFinalizeOptions = {
|
|
475
|
-
lookup?: () => Promise<LookupResult>;
|
|
476
|
-
lookupTimeoutMs?: number;
|
|
674
|
+
/** experimental:异步补查 usage/billing */lookup?: () => Promise<LookupResult>;
|
|
675
|
+
lookupTimeoutMs?: number; /** experimental:在尚无 billing 时派生估算账单 */
|
|
477
676
|
postprocessBilling?: BillingPostprocessHook;
|
|
478
677
|
postprocessBillingSource?: BillingSource;
|
|
479
678
|
};
|
|
@@ -482,7 +681,7 @@ type AuxiliaryFinalizeResult = {
|
|
|
482
681
|
usage?: Usage;
|
|
483
682
|
billing?: BillingInfo;
|
|
484
683
|
auxiliary?: AuxiliaryInfo;
|
|
485
|
-
warnings?:
|
|
684
|
+
warnings?: StreamWarning[];
|
|
486
685
|
metadataSources?: string[];
|
|
487
686
|
};
|
|
488
687
|
declare class AdapterAuxiliaryState {
|
|
@@ -496,32 +695,36 @@ declare class AdapterAuxiliaryState {
|
|
|
496
695
|
finalize(factory: EventFactory, options?: AuxiliaryFinalizeOptions): Promise<AuxiliaryFinalizeResult>;
|
|
497
696
|
private shouldAttemptLookup;
|
|
498
697
|
}
|
|
499
|
-
declare function emitMalformedStreamWarning(factory: EventFactory, options: {
|
|
500
|
-
count: number;
|
|
501
|
-
providerLabel: string;
|
|
502
|
-
transportLabel: string;
|
|
503
|
-
}): AIStreamEvent | undefined;
|
|
504
698
|
//#endregion
|
|
505
|
-
//#region src/
|
|
699
|
+
//#region src/provider/base.d.ts
|
|
506
700
|
type ProviderResponse = unknown;
|
|
507
701
|
/**
|
|
508
|
-
* adapter
|
|
509
|
-
*
|
|
702
|
+
* adapter 完成一轮流处理后交给 emitStreamCompleted 的元数据。
|
|
703
|
+
* 不含 output/text/toolCalls — 那些由事件聚合得到。
|
|
510
704
|
*/
|
|
511
705
|
type StreamResult = {
|
|
512
|
-
output: OutputItem[];
|
|
513
706
|
replay: ReplayItem[];
|
|
514
707
|
stopReason?: StopReason;
|
|
515
708
|
usage?: Usage;
|
|
516
709
|
billing?: BillingInfo;
|
|
517
710
|
providerMetadata?: Record<string, unknown>;
|
|
518
711
|
auxiliary?: Partial<AuxiliaryInfo>;
|
|
519
|
-
warnings?:
|
|
712
|
+
warnings?: StreamWarning[];
|
|
520
713
|
metadataSources?: string[];
|
|
521
714
|
rawResponseId?: string;
|
|
522
715
|
};
|
|
716
|
+
/** response.completed 所需的完成元数据(无 output 账本) */
|
|
717
|
+
type StreamCompletedPayload = {
|
|
718
|
+
replay: ReplayItem[];
|
|
719
|
+
stopReason?: StopReason;
|
|
720
|
+
usage?: Usage;
|
|
721
|
+
billing?: BillingInfo;
|
|
722
|
+
auxiliary?: AuxiliaryInfo;
|
|
723
|
+
warnings?: StreamWarning[] | undefined;
|
|
724
|
+
trace: Partial<BackendTrace>;
|
|
725
|
+
};
|
|
523
726
|
declare abstract class AdapterBase implements BackendAdapter {
|
|
524
|
-
abstract readonly kind:
|
|
727
|
+
abstract readonly kind: AdapterKind;
|
|
525
728
|
abstract readonly isSyntheticStream: boolean;
|
|
526
729
|
/**
|
|
527
730
|
* stream 模板方法:
|
|
@@ -537,31 +740,139 @@ declare abstract class AdapterBase implements BackendAdapter {
|
|
|
537
740
|
* 子类负责:
|
|
538
741
|
* - 调用 provider
|
|
539
742
|
* - 解析每个 chunk
|
|
540
|
-
* - 通过 factory 发射 item 事件
|
|
541
|
-
* -
|
|
542
|
-
* - 发射
|
|
743
|
+
* - 通过 StreamingItemSession / factory 发射 item 事件
|
|
744
|
+
* - 组装 StreamResult(replay + 元数据)
|
|
745
|
+
* - 发射 response.completed(通常经 emitStreamCompleted)
|
|
543
746
|
*/
|
|
544
747
|
protected abstract runStream(providerRequest: ProviderResponse, factory: EventFactory, request: NormalizedRequest): AsyncIterable<AIStreamEvent>;
|
|
545
748
|
/**
|
|
546
|
-
* 从 StreamResult
|
|
547
|
-
* 子类可在返回前自定义覆盖。
|
|
749
|
+
* 从 StreamResult 构建 response.completed 载荷(无 output/text/toolCalls)。
|
|
548
750
|
*/
|
|
549
|
-
protected
|
|
751
|
+
protected buildCompletedPayload(request: NormalizedRequest, result: StreamResult, factory: EventFactory): StreamCompletedPayload;
|
|
550
752
|
/**
|
|
551
753
|
* 统一 finalize auxiliary → response.completed。
|
|
552
|
-
* adapter 在调用前组装
|
|
754
|
+
* adapter 在调用前组装 replay / stopReason 等业务字段(不含 output 账本)。
|
|
553
755
|
*/
|
|
554
756
|
protected emitStreamCompleted(factory: EventFactory, request: NormalizedRequest, auxiliary: AdapterAuxiliaryState, result: StreamResult): AsyncIterable<AIStreamEvent>;
|
|
555
757
|
protected createAuxiliaryState(request: NormalizedRequest): AdapterAuxiliaryState;
|
|
556
758
|
}
|
|
557
759
|
//#endregion
|
|
558
|
-
//#region src/
|
|
559
|
-
type
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
760
|
+
//#region src/provider/transport/parser.d.ts
|
|
761
|
+
type StreamSplitResult = {
|
|
762
|
+
items: string[];
|
|
763
|
+
rest: string;
|
|
764
|
+
};
|
|
765
|
+
type StreamParseResult<T> = {
|
|
766
|
+
status: "parsed";
|
|
767
|
+
value: T;
|
|
768
|
+
} | {
|
|
769
|
+
status: "ignored";
|
|
770
|
+
} | {
|
|
771
|
+
status: "malformed";
|
|
772
|
+
};
|
|
773
|
+
declare class IncrementalStreamParser<T> {
|
|
774
|
+
private readonly split;
|
|
775
|
+
private readonly parse;
|
|
776
|
+
/** Pending decoded fragments; compacted to at most one rest string after each consume. */
|
|
777
|
+
private chunks;
|
|
778
|
+
private readonly decoder;
|
|
779
|
+
constructor(split: (buffer: string, allowEOF: boolean) => StreamSplitResult, parse: (item: string) => StreamParseResult<T>);
|
|
780
|
+
feed(value: Uint8Array): {
|
|
781
|
+
items: T[];
|
|
782
|
+
malformed: number;
|
|
783
|
+
};
|
|
784
|
+
flush(): {
|
|
785
|
+
items: T[];
|
|
786
|
+
malformed: number;
|
|
787
|
+
};
|
|
788
|
+
getRemaining(): string;
|
|
789
|
+
private materializeBuffer;
|
|
790
|
+
private consume;
|
|
791
|
+
}
|
|
792
|
+
//#endregion
|
|
793
|
+
//#region src/provider/transport/open-stream.d.ts
|
|
794
|
+
type ProviderStreamBatch<T> = {
|
|
795
|
+
items: T[];
|
|
796
|
+
warnings: AIStreamEvent[];
|
|
797
|
+
};
|
|
798
|
+
//#endregion
|
|
799
|
+
//#region src/provider/transport/run-json-stream.d.ts
|
|
800
|
+
type ProviderJsonStreamOpenOptions = {
|
|
801
|
+
url: string;
|
|
802
|
+
headers: Record<string, string>;
|
|
803
|
+
body: unknown;
|
|
804
|
+
};
|
|
805
|
+
type ProviderJsonStreamBatchOptions<T> = {
|
|
806
|
+
parser: IncrementalStreamParser<T>;
|
|
807
|
+
providerLabel: string;
|
|
808
|
+
transportLabel: string;
|
|
809
|
+
incompleteMessage: string;
|
|
810
|
+
};
|
|
811
|
+
type ProviderJsonStreamCompleteOptions = {
|
|
812
|
+
/**
|
|
813
|
+
* 重复 complete 时:`warn` 发 DUPLICATE_FINISH(默认,对齐 chat/gemini/ollama);
|
|
814
|
+
* `silent` 静默忽略(messages/responses 历史路径几乎不会触发)。
|
|
815
|
+
*/
|
|
816
|
+
onDuplicate?: "warn" | "silent";
|
|
817
|
+
};
|
|
818
|
+
type ProviderJsonStreamSession = {
|
|
819
|
+
readonly auxiliary: AdapterAuxiliaryState;
|
|
820
|
+
readonly gate: {
|
|
821
|
+
readonly completed: boolean;
|
|
822
|
+
tryComplete(): boolean;
|
|
823
|
+
}; /** open 成功后填充的 response headers */
|
|
824
|
+
readonly headers: Headers | undefined;
|
|
825
|
+
open(options: ProviderJsonStreamOpenOptions): Promise<{
|
|
826
|
+
headers: Headers;
|
|
827
|
+
}>;
|
|
828
|
+
batches<T>(options: ProviderJsonStreamBatchOptions<T>): AsyncGenerator<ProviderStreamBatch<T>, void, undefined>;
|
|
829
|
+
complete(result: StreamResult, options?: ProviderJsonStreamCompleteOptions): AsyncIterable<AIStreamEvent>;
|
|
830
|
+
};
|
|
831
|
+
//#endregion
|
|
832
|
+
//#region src/provider/http-adapter.d.ts
|
|
833
|
+
/** 真实 HTTP adapter 的公共构造选项;apiKey 由各 adapter 收紧或保持可选。 */
|
|
834
|
+
type HttpAdapterOptions = {
|
|
835
|
+
apiKey?: string;
|
|
836
|
+
baseUrl?: string; /** 可注入自定义 fetch 实现(测试 / 代理) */
|
|
837
|
+
fetch?: FetchFn; /** 额外请求头;后写覆盖内置鉴权 / Content-Type 等 */
|
|
563
838
|
headers?: Record<string, string>; /** 额外 body 顶层字段;浅层合并,同名键可覆盖 */
|
|
564
839
|
extraBody?: Record<string, unknown>;
|
|
840
|
+
/**
|
|
841
|
+
* 单条 opaque replay payload 体积上限(JSON.stringify 码元长度)。
|
|
842
|
+
* 默认 1 MiB;夹到 [1, 8 MiB] 硬顶。emit 与 accept 共用。
|
|
843
|
+
*/
|
|
844
|
+
maxOpaquePayloadBytes?: number;
|
|
845
|
+
};
|
|
846
|
+
type HttpAdapterDefaults = {
|
|
847
|
+
baseUrl: string;
|
|
848
|
+
};
|
|
849
|
+
/**
|
|
850
|
+
* HTTP adapter 薄基类:统一字段、默认值与 JSON 流 session。
|
|
851
|
+
*/
|
|
852
|
+
declare abstract class HttpAdapterBase extends AdapterBase {
|
|
853
|
+
protected apiKey: string | undefined;
|
|
854
|
+
protected baseUrl: string;
|
|
855
|
+
protected fetchFn: FetchFn;
|
|
856
|
+
protected headers: Record<string, string> | undefined;
|
|
857
|
+
protected extraBody: Record<string, unknown> | undefined;
|
|
858
|
+
/** 已 clamp 的 opaque 体积上限(emit / accept 共用)。 */
|
|
859
|
+
protected maxOpaquePayloadBytes: number;
|
|
860
|
+
constructor(options: HttpAdapterOptions, defaults: HttpAdapterDefaults);
|
|
861
|
+
/** 合并内置 headers 与构造期自定义 headers。 */
|
|
862
|
+
protected mergeHeaders(base: Record<string, string>): Record<string, string>;
|
|
863
|
+
/** 将构造期 extraBody 浅层合并进已构建 body。 */
|
|
864
|
+
protected withExtraBody<T extends object>(body: T): T;
|
|
865
|
+
/**
|
|
866
|
+
* 开启 JSON provider 流 session(auxiliary + gate + open/batches/complete)。
|
|
867
|
+
* 不自动 complete;调用方在业务 finish 点 `yield* session.complete(...)`。
|
|
868
|
+
*/
|
|
869
|
+
protected beginJsonStream(factory: EventFactory, request: NormalizedRequest): ProviderJsonStreamSession;
|
|
870
|
+
}
|
|
871
|
+
//#endregion
|
|
872
|
+
//#region src/adapters/responses/types.d.ts
|
|
873
|
+
/** apiKey 必填;默认 baseUrl https://api.openai.com/v1 */
|
|
874
|
+
type ResponsesAdapterOptions = HttpAdapterOptions & {
|
|
875
|
+
apiKey: string;
|
|
565
876
|
};
|
|
566
877
|
type ResponsesAPIRequest = {
|
|
567
878
|
model: string;
|
|
@@ -636,36 +947,83 @@ type ResponsesItemReference = {
|
|
|
636
947
|
type: "item_reference";
|
|
637
948
|
id: string;
|
|
638
949
|
};
|
|
639
|
-
|
|
640
|
-
type
|
|
950
|
+
/** compact 输出中的加密 compaction 项(可原样回传为 input) */
|
|
951
|
+
type ResponsesCompactionInput = {
|
|
952
|
+
type: "compaction";
|
|
953
|
+
id?: string;
|
|
954
|
+
encrypted_content?: string;
|
|
955
|
+
[key: string]: unknown;
|
|
956
|
+
};
|
|
957
|
+
/**
|
|
958
|
+
* 保真透传的 wire item(compact window 中可能含 message/function_call/compaction 等)。
|
|
959
|
+
* 用于 compacted_window 原样展开,不在此做严格 shape 收窄。
|
|
960
|
+
*/
|
|
961
|
+
type ResponsesWirePassthroughItem = {
|
|
962
|
+
type: string;
|
|
963
|
+
[key: string]: unknown;
|
|
964
|
+
};
|
|
965
|
+
type ResponsesInputItem = ResponsesEasyMessage | ResponsesFunctionCall | ResponsesFunctionCallOutput | ResponsesReasoningInput | ResponsesItemReference | ResponsesCompactionInput | ResponsesWirePassthroughItem;
|
|
966
|
+
type ResponsesFunctionTool = {
|
|
641
967
|
type: "function";
|
|
642
968
|
name: string;
|
|
643
969
|
description?: string;
|
|
644
970
|
parameters: Record<string, unknown>;
|
|
645
971
|
strict?: boolean | null;
|
|
646
972
|
};
|
|
647
|
-
|
|
973
|
+
type ResponsesWebSearchTool = {
|
|
974
|
+
type: "web_search";
|
|
975
|
+
filters?: {
|
|
976
|
+
allowed_domains?: string[];
|
|
977
|
+
blocked_domains?: string[];
|
|
978
|
+
};
|
|
979
|
+
user_location?: {
|
|
980
|
+
type: "approximate";
|
|
981
|
+
country?: string;
|
|
982
|
+
city?: string;
|
|
983
|
+
region?: string;
|
|
984
|
+
timezone?: string;
|
|
985
|
+
};
|
|
986
|
+
search_context_size?: "low" | "medium" | "high";
|
|
987
|
+
};
|
|
988
|
+
type ResponsesCodeInterpreterTool = {
|
|
989
|
+
type: "code_interpreter";
|
|
990
|
+
container: string | {
|
|
991
|
+
type: "auto";
|
|
992
|
+
memory_limit?: "1g" | "4g" | "16g" | "64g";
|
|
993
|
+
file_ids?: string[];
|
|
994
|
+
};
|
|
995
|
+
};
|
|
996
|
+
type ResponsesMcpTool = {
|
|
997
|
+
type: "mcp";
|
|
998
|
+
server_label: string;
|
|
999
|
+
server_url: string;
|
|
1000
|
+
server_description?: string;
|
|
1001
|
+
authorization?: string;
|
|
1002
|
+
allowed_tools?: string[];
|
|
1003
|
+
require_approval: "never";
|
|
1004
|
+
};
|
|
1005
|
+
/** Responses API tools 联合:客户端 function + 内置 server tools */
|
|
1006
|
+
type ResponsesTool = ResponsesFunctionTool | ResponsesWebSearchTool | ResponsesCodeInterpreterTool | ResponsesMcpTool;
|
|
1007
|
+
//#endregion
|
|
1008
|
+
//#region src/adapters/responses/adapter.d.ts
|
|
1009
|
+
declare class ResponsesAdapter extends HttpAdapterBase implements ContextCompressCapable {
|
|
648
1010
|
readonly kind: "responses";
|
|
649
1011
|
readonly isSyntheticStream = false;
|
|
650
|
-
private apiKey;
|
|
651
|
-
private baseUrl;
|
|
652
|
-
private fetchFn;
|
|
653
|
-
private headers;
|
|
654
|
-
private extraBody;
|
|
655
1012
|
constructor(options: ResponsesAdapterOptions);
|
|
656
1013
|
protected buildRequest(request: NormalizedRequest): ResponsesAPIRequest;
|
|
1014
|
+
/**
|
|
1015
|
+
* 原生上下文压缩:POST /responses/compact。
|
|
1016
|
+
* 结果以单个 opaque(kind=compacted_window) 回传;调用方用 replay 替换旧 transcript。
|
|
1017
|
+
*/
|
|
1018
|
+
compress(request: CompressRequest): Promise<CompressResult>;
|
|
657
1019
|
protected runStream(providerRequest: ResponsesAPIRequest, factory: EventFactory, request: NormalizedRequest): AsyncIterable<AIStreamEvent>;
|
|
658
|
-
private inferStopReason;
|
|
659
1020
|
}
|
|
660
1021
|
//#endregion
|
|
661
|
-
//#region src/adapters/messages.d.ts
|
|
662
|
-
|
|
663
|
-
|
|
1022
|
+
//#region src/adapters/messages/types.d.ts
|
|
1023
|
+
/** apiKey 必填;默认 baseUrl https://api.anthropic.com/v1 */
|
|
1024
|
+
type MessagesAdapterOptions = HttpAdapterOptions & {
|
|
1025
|
+
apiKey: string; /** Anthropic API 版本头,默认 2023-06-01 */
|
|
664
1026
|
apiVersion?: string;
|
|
665
|
-
baseUrl?: string; /** 可注入自定义 fetch 实现(用于测试/代理) */
|
|
666
|
-
fetch?: FetchFn; /** 额外请求头;后写覆盖内置 x-api-key / Content-Type / anthropic-version */
|
|
667
|
-
headers?: Record<string, string>; /** 额外 body 顶层字段;浅层合并,同名键可覆盖 */
|
|
668
|
-
extraBody?: Record<string, unknown>;
|
|
669
1027
|
};
|
|
670
1028
|
type MessagesAPIRequest = {
|
|
671
1029
|
model: string;
|
|
@@ -718,27 +1076,21 @@ type MessagesAPITool = {
|
|
|
718
1076
|
description?: string;
|
|
719
1077
|
input_schema: Record<string, unknown>;
|
|
720
1078
|
};
|
|
721
|
-
|
|
1079
|
+
//#endregion
|
|
1080
|
+
//#region src/adapters/messages/adapter.d.ts
|
|
1081
|
+
declare class MessagesAdapter extends HttpAdapterBase {
|
|
722
1082
|
readonly kind: "messages";
|
|
723
1083
|
readonly isSyntheticStream = false;
|
|
724
|
-
private apiKey;
|
|
725
1084
|
private apiVersion;
|
|
726
|
-
private baseUrl;
|
|
727
|
-
private fetchFn;
|
|
728
|
-
private headers;
|
|
729
|
-
private extraBody;
|
|
730
1085
|
constructor(options: MessagesAdapterOptions);
|
|
731
1086
|
protected buildRequest(request: NormalizedRequest): MessagesAPIRequest;
|
|
732
1087
|
protected runStream(providerRequest: MessagesAPIRequest, factory: EventFactory, request: NormalizedRequest): AsyncIterable<AIStreamEvent>;
|
|
733
1088
|
}
|
|
734
1089
|
//#endregion
|
|
735
|
-
//#region src/adapters/chat-completions.d.ts
|
|
736
|
-
|
|
1090
|
+
//#region src/adapters/chat-completions/types.d.ts
|
|
1091
|
+
/** apiKey 必填;默认 baseUrl https://api.openai.com/v1 */
|
|
1092
|
+
type ChatCompletionsAdapterOptions = HttpAdapterOptions & {
|
|
737
1093
|
apiKey: string;
|
|
738
|
-
baseUrl?: string;
|
|
739
|
-
fetch?: FetchFn; /** 额外请求头;后写覆盖内置 Authorization / Content-Type */
|
|
740
|
-
headers?: Record<string, string>; /** 额外 body 顶层字段;浅层合并,同名键可覆盖 */
|
|
741
|
-
extraBody?: Record<string, unknown>;
|
|
742
1094
|
};
|
|
743
1095
|
type ChatRequest = {
|
|
744
1096
|
model: string;
|
|
@@ -781,27 +1133,21 @@ type ChatTool = {
|
|
|
781
1133
|
parameters: Record<string, unknown>;
|
|
782
1134
|
};
|
|
783
1135
|
};
|
|
784
|
-
|
|
1136
|
+
//#endregion
|
|
1137
|
+
//#region src/adapters/chat-completions/adapter.d.ts
|
|
1138
|
+
declare class ChatCompletionsAdapter extends HttpAdapterBase {
|
|
785
1139
|
readonly kind: "chat-completions";
|
|
786
1140
|
readonly isSyntheticStream = false;
|
|
787
|
-
private apiKey;
|
|
788
|
-
private baseUrl;
|
|
789
|
-
private fetchFn;
|
|
790
|
-
private headers;
|
|
791
|
-
private extraBody;
|
|
792
1141
|
constructor(options: ChatCompletionsAdapterOptions);
|
|
793
1142
|
protected buildRequest(request: NormalizedRequest): ChatRequest;
|
|
794
1143
|
protected runStream(providerRequest: ChatRequest, factory: EventFactory, request: NormalizedRequest): AsyncIterable<AIStreamEvent>;
|
|
795
1144
|
}
|
|
796
1145
|
//#endregion
|
|
797
|
-
//#region src/adapters/ollama.d.ts
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
headers?: Record<string, string>; /** 额外 body 顶层字段;浅层合并,同名键可覆盖 */
|
|
803
|
-
extraBody?: Record<string, unknown>;
|
|
804
|
-
};
|
|
1146
|
+
//#region src/adapters/ollama/types.d.ts
|
|
1147
|
+
/**
|
|
1148
|
+
* apiKey 可选(代理鉴权);默认 baseUrl http://localhost:11434
|
|
1149
|
+
*/
|
|
1150
|
+
type OllamaAdapterOptions = HttpAdapterOptions;
|
|
805
1151
|
type OllamaChatRequest = {
|
|
806
1152
|
model: string;
|
|
807
1153
|
messages: OllamaMessage[];
|
|
@@ -834,20 +1180,86 @@ type OllamaTool = {
|
|
|
834
1180
|
parameters: Record<string, unknown>;
|
|
835
1181
|
};
|
|
836
1182
|
};
|
|
837
|
-
|
|
1183
|
+
//#endregion
|
|
1184
|
+
//#region src/adapters/ollama/adapter.d.ts
|
|
1185
|
+
declare class OllamaAdapter extends HttpAdapterBase {
|
|
838
1186
|
readonly kind: "ollama";
|
|
839
1187
|
readonly isSyntheticStream = false;
|
|
840
|
-
private baseUrl;
|
|
841
|
-
private apiKey;
|
|
842
|
-
private fetchFn;
|
|
843
|
-
private headers;
|
|
844
|
-
private extraBody;
|
|
845
1188
|
constructor(options?: OllamaAdapterOptions);
|
|
846
1189
|
protected buildRequest(request: NormalizedRequest): OllamaChatRequest;
|
|
847
1190
|
protected runStream(providerRequest: OllamaChatRequest, factory: EventFactory, request: NormalizedRequest): AsyncIterable<AIStreamEvent>;
|
|
848
1191
|
}
|
|
849
1192
|
//#endregion
|
|
850
|
-
//#region src/adapters/
|
|
1193
|
+
//#region src/adapters/gemini/types.d.ts
|
|
1194
|
+
/** apiKey 必填;默认 baseUrl https://generativelanguage.googleapis.com/v1beta */
|
|
1195
|
+
type GeminiAdapterOptions = HttpAdapterOptions & {
|
|
1196
|
+
apiKey: string;
|
|
1197
|
+
};
|
|
1198
|
+
type GeminiPart = {
|
|
1199
|
+
text?: string;
|
|
1200
|
+
thought?: boolean;
|
|
1201
|
+
thoughtSignature?: string;
|
|
1202
|
+
functionCall?: {
|
|
1203
|
+
name: string;
|
|
1204
|
+
args?: Record<string, unknown>;
|
|
1205
|
+
id?: string;
|
|
1206
|
+
};
|
|
1207
|
+
functionResponse?: {
|
|
1208
|
+
name: string;
|
|
1209
|
+
response?: Record<string, unknown>;
|
|
1210
|
+
id?: string;
|
|
1211
|
+
};
|
|
1212
|
+
[key: string]: unknown;
|
|
1213
|
+
};
|
|
1214
|
+
type GeminiContent = {
|
|
1215
|
+
role: "user" | "model";
|
|
1216
|
+
parts: GeminiPart[];
|
|
1217
|
+
};
|
|
1218
|
+
type GeminiFunctionDeclaration = {
|
|
1219
|
+
name: string;
|
|
1220
|
+
description?: string;
|
|
1221
|
+
parameters: Record<string, unknown>;
|
|
1222
|
+
};
|
|
1223
|
+
type GeminiTool = {
|
|
1224
|
+
functionDeclarations: GeminiFunctionDeclaration[];
|
|
1225
|
+
};
|
|
1226
|
+
type GeminiFunctionCallingConfig = {
|
|
1227
|
+
mode: "AUTO" | "ANY" | "NONE";
|
|
1228
|
+
allowedFunctionNames?: string[];
|
|
1229
|
+
};
|
|
1230
|
+
type GeminiGenerateContentRequest = {
|
|
1231
|
+
contents: GeminiContent[];
|
|
1232
|
+
systemInstruction?: {
|
|
1233
|
+
parts: Array<{
|
|
1234
|
+
text: string;
|
|
1235
|
+
}>;
|
|
1236
|
+
};
|
|
1237
|
+
tools?: GeminiTool[];
|
|
1238
|
+
toolConfig?: {
|
|
1239
|
+
functionCallingConfig: GeminiFunctionCallingConfig;
|
|
1240
|
+
};
|
|
1241
|
+
generationConfig?: {
|
|
1242
|
+
temperature?: number;
|
|
1243
|
+
maxOutputTokens?: number;
|
|
1244
|
+
thinkingConfig?: {
|
|
1245
|
+
includeThoughts: false;
|
|
1246
|
+
} | {
|
|
1247
|
+
includeThoughts: true;
|
|
1248
|
+
thinkingLevel: "MINIMAL" | "LOW" | "MEDIUM" | "HIGH";
|
|
1249
|
+
};
|
|
1250
|
+
};
|
|
1251
|
+
};
|
|
1252
|
+
//#endregion
|
|
1253
|
+
//#region src/adapters/gemini/adapter.d.ts
|
|
1254
|
+
declare class GeminiAdapter extends HttpAdapterBase {
|
|
1255
|
+
readonly kind: "gemini";
|
|
1256
|
+
readonly isSyntheticStream = false;
|
|
1257
|
+
constructor(options: GeminiAdapterOptions);
|
|
1258
|
+
protected buildRequest(request: NormalizedRequest): GeminiGenerateContentRequest;
|
|
1259
|
+
protected runStream(providerRequest: GeminiGenerateContentRequest, factory: EventFactory, request: NormalizedRequest): AsyncIterable<AIStreamEvent>;
|
|
1260
|
+
}
|
|
1261
|
+
//#endregion
|
|
1262
|
+
//#region src/adapters/mock/types.d.ts
|
|
851
1263
|
type MockInputExpectation = {
|
|
852
1264
|
type: InputItem["type"];
|
|
853
1265
|
id?: string;
|
|
@@ -855,7 +1267,7 @@ type MockInputExpectation = {
|
|
|
855
1267
|
name?: string;
|
|
856
1268
|
toolName?: string;
|
|
857
1269
|
callId?: string;
|
|
858
|
-
outcome?: ToolResultItem["outcome"];
|
|
1270
|
+
outcome?: ToolResultItem["outcome"] | ServerToolResultItem["outcome"];
|
|
859
1271
|
visibility?: Extract<InputItem, {
|
|
860
1272
|
type: "reasoning";
|
|
861
1273
|
}>["visibility"];
|
|
@@ -874,6 +1286,7 @@ type MockRequestExpectation = {
|
|
|
874
1286
|
requireReplayFromPreviousTurn?: boolean;
|
|
875
1287
|
requireToolResultsForPendingCalls?: boolean;
|
|
876
1288
|
tools?: "ignore" | "present" | "absent";
|
|
1289
|
+
serverTools?: "ignore" | "present" | "absent";
|
|
877
1290
|
toolChoice?: "ignore" | "present" | "absent";
|
|
878
1291
|
items?: MockInputExpectation[];
|
|
879
1292
|
};
|
|
@@ -920,8 +1333,29 @@ type MockMessageStep = {
|
|
|
920
1333
|
type: "message";
|
|
921
1334
|
id?: string;
|
|
922
1335
|
content: string | ContentBlock[];
|
|
1336
|
+
citations?: Citation[];
|
|
923
1337
|
stream?: MockTextStreamOptions | false;
|
|
924
1338
|
};
|
|
1339
|
+
type MockServerToolCallStep = {
|
|
1340
|
+
type: "server_tool_call";
|
|
1341
|
+
id: string;
|
|
1342
|
+
tool: ServerToolCallItem["tool"];
|
|
1343
|
+
name?: string;
|
|
1344
|
+
argumentsText?: string;
|
|
1345
|
+
serverLabel?: string;
|
|
1346
|
+
status?: ServerToolCallItem["status"];
|
|
1347
|
+
providerPayload?: unknown;
|
|
1348
|
+
streamArguments?: boolean;
|
|
1349
|
+
stream?: MockTextStreamOptions | false;
|
|
1350
|
+
};
|
|
1351
|
+
type MockServerToolResultStep = {
|
|
1352
|
+
type: "server_tool_result";
|
|
1353
|
+
item: ServerToolResultItem;
|
|
1354
|
+
};
|
|
1355
|
+
type MockServerToolDiscoveryStep = {
|
|
1356
|
+
type: "server_tool_discovery";
|
|
1357
|
+
item: ServerToolDiscoveryItem;
|
|
1358
|
+
};
|
|
925
1359
|
type MockReasoningStep = {
|
|
926
1360
|
type: "reasoning";
|
|
927
1361
|
id?: string;
|
|
@@ -955,7 +1389,7 @@ type MockCompleteStep = {
|
|
|
955
1389
|
auxiliary?: Partial<AuxiliaryInfo>;
|
|
956
1390
|
providerMetadata?: Record<string, unknown>;
|
|
957
1391
|
rawResponseId?: string;
|
|
958
|
-
warnings?:
|
|
1392
|
+
warnings?: StreamWarning[];
|
|
959
1393
|
};
|
|
960
1394
|
type MockErrorStep = {
|
|
961
1395
|
type: "error";
|
|
@@ -971,13 +1405,16 @@ type MockThrowStep = {
|
|
|
971
1405
|
type: "throw";
|
|
972
1406
|
error: string | Error;
|
|
973
1407
|
};
|
|
974
|
-
type MockStep = MockWarningStep | MockAuxiliaryStep | MockMessageStep | MockReasoningStep | MockToolCallStep | MockOutputStep | MockCompleteStep | MockErrorStep | MockInterruptStep | MockThrowStep;
|
|
1408
|
+
type MockStep = MockWarningStep | MockAuxiliaryStep | MockMessageStep | MockReasoningStep | MockToolCallStep | MockServerToolCallStep | MockServerToolResultStep | MockServerToolDiscoveryStep | MockOutputStep | MockCompleteStep | MockErrorStep | MockInterruptStep | MockThrowStep;
|
|
975
1409
|
type MockHandler = (request: NormalizedRequest, context: MockHandlerContext) => AsyncIterable<MockStep>;
|
|
976
1410
|
type MockHandlerSource = Iterable<MockStep> | AsyncIterable<MockStep>;
|
|
977
1411
|
type MockStaticHandler = (request: NormalizedRequest, context: MockHandlerContext) => MockHandlerSource | Promise<MockHandlerSource>;
|
|
1412
|
+
/** Mock compress 夹具;未配置时 compress() 抛 MOCK_COMPRESS_NOT_CONFIGURED */
|
|
1413
|
+
type MockCompressHandler = (request: CompressRequest) => CompressResult | Promise<CompressResult>;
|
|
978
1414
|
type MockAdapterOptions = {
|
|
979
1415
|
handler: MockHandler;
|
|
980
|
-
providerMetadata?: Record<string, unknown>;
|
|
1416
|
+
providerMetadata?: Record<string, unknown>; /** 可选:实现 ContextCompressCapable 供 compress 契约测试 */
|
|
1417
|
+
compressHandler?: MockCompressHandler;
|
|
981
1418
|
};
|
|
982
1419
|
type MockProviderRequest = {
|
|
983
1420
|
request: NormalizedRequest;
|
|
@@ -985,326 +1422,36 @@ type MockProviderRequest = {
|
|
|
985
1422
|
turnIndex: number;
|
|
986
1423
|
remainingPendingToolCalls: ToolCallItem[];
|
|
987
1424
|
};
|
|
988
|
-
|
|
989
|
-
|
|
1425
|
+
//#endregion
|
|
1426
|
+
//#region src/adapters/mock/adapter.d.ts
|
|
1427
|
+
declare class MockAdapter extends AdapterBase implements ContextCompressCapable {
|
|
990
1428
|
readonly kind: "mock";
|
|
991
1429
|
readonly isSyntheticStream = true;
|
|
992
1430
|
private readonly handler;
|
|
993
1431
|
private readonly providerMetadata?;
|
|
1432
|
+
private readonly compressHandler?;
|
|
994
1433
|
private cursor;
|
|
995
1434
|
private previousReplay;
|
|
996
1435
|
private pendingToolCalls;
|
|
997
1436
|
private history;
|
|
998
1437
|
private activeStream;
|
|
999
1438
|
constructor(options: MockAdapterOptions);
|
|
1439
|
+
/**
|
|
1440
|
+
* 可选压缩夹具:需构造时提供 compressHandler,否则抛 MOCK_COMPRESS_NOT_CONFIGURED。
|
|
1441
|
+
* 始终存在 compress 方法以便 supportsContextCompress(mock) === true。
|
|
1442
|
+
*/
|
|
1443
|
+
compress(request: CompressRequest): Promise<CompressResult>;
|
|
1000
1444
|
protected buildRequest(request: NormalizedRequest): Promise<MockProviderRequest>;
|
|
1001
1445
|
protected runStream(providerRequest: unknown, factory: EventFactory, request: NormalizedRequest): AsyncIterable<AIStreamEvent>;
|
|
1002
1446
|
private finalizeTurn;
|
|
1003
1447
|
private buildHandlerContext;
|
|
1004
1448
|
}
|
|
1005
|
-
declare function withMockStreaming(handler: MockStaticHandler, options: MockTextStreamOptions): MockHandler;
|
|
1006
|
-
//#endregion
|
|
1007
|
-
//#region src/helpers/mapping.d.ts
|
|
1008
|
-
declare function mapStopReason(providerReason: string): StopReason;
|
|
1009
|
-
declare function mapReasoningVisibility(hasThinking: boolean, hasRedacted: boolean): ReasoningItem["visibility"];
|
|
1010
|
-
declare function textBlock(text: string): ContentBlock & {
|
|
1011
|
-
type: "text";
|
|
1012
|
-
};
|
|
1013
|
-
declare function jsonBlock(json: unknown): ContentBlock & {
|
|
1014
|
-
type: "json";
|
|
1015
|
-
};
|
|
1016
|
-
declare function imageBlock(imageUrl: string): ContentBlock & {
|
|
1017
|
-
type: "image";
|
|
1018
|
-
};
|
|
1019
|
-
declare function opaqueBlock(payload: unknown): ContentBlock & {
|
|
1020
|
-
type: "opaque";
|
|
1021
|
-
};
|
|
1022
|
-
declare function messageItem(content: ContentBlock[], overrides?: Partial<Omit<MessageItem, "type" | "content">>): MessageItem;
|
|
1023
|
-
declare function reasoningItem(content: ContentBlock[], visibility?: ReasoningItem["visibility"], id?: string): ReasoningItem;
|
|
1024
|
-
declare function toolCallItem(id: string, name: string, argumentsText: string): ToolCallItem;
|
|
1025
|
-
declare function toolResultItem(callId: string, toolName: string, outcome: ToolResultItem["outcome"], content: ContentBlock[]): ToolResultItem;
|
|
1026
|
-
declare function opaqueItem(source: OpaqueItem["source"], purpose: OpaqueItem["purpose"], payload: unknown, id?: string): OpaqueItem;
|
|
1027
|
-
/**
|
|
1028
|
-
* 从 output items 构建标准 replay items。
|
|
1029
|
-
* 简单场景下 replay 与 output 一致。
|
|
1030
|
-
* 复杂场景(需要 opaque continuation)由 adapter 自行扩展。
|
|
1031
|
-
*/
|
|
1032
|
-
declare function replayFromOutput(output: readonly OutputItem[]): ReplayItem[];
|
|
1033
|
-
/**
|
|
1034
|
-
* 将单个 ContentBlock 转为纯文本。
|
|
1035
|
-
* text 块直接返回文本,json 块序列化,其余返回空串。
|
|
1036
|
-
*/
|
|
1037
|
-
declare function blockToText(b: ContentBlock): string;
|
|
1038
|
-
/**
|
|
1039
|
-
* 将 ContentBlock 数组拼接为纯文本,块间以换行符分隔。
|
|
1040
|
-
*/
|
|
1041
|
-
declare function contentBlocksToText(blocks: ContentBlock[]): string;
|
|
1042
|
-
/**
|
|
1043
|
-
* 从 OutputItem 数组中提取所有 message 类型 item 的文本内容。
|
|
1044
|
-
*/
|
|
1045
|
-
declare function extractText(output: OutputItem[]): string;
|
|
1046
|
-
//#endregion
|
|
1047
|
-
//#region src/helpers/synthetic-stream.d.ts
|
|
1048
|
-
type SyntheticStreamOptions = {
|
|
1049
|
-
model: string;
|
|
1050
|
-
responseId: string;
|
|
1051
|
-
backend: {
|
|
1052
|
-
kind: "chat-completions" | "messages" | "responses" | "mock";
|
|
1053
|
-
};
|
|
1054
|
-
output: OutputItem[];
|
|
1055
|
-
replay?: ReplayItem[];
|
|
1056
|
-
stopReason?: StopReason;
|
|
1057
|
-
usage?: Usage;
|
|
1058
|
-
billing?: BillingInfo;
|
|
1059
|
-
providerMetadata?: Record<string, unknown>;
|
|
1060
|
-
rawResponseId?: string;
|
|
1061
|
-
warnings?: string[];
|
|
1062
|
-
};
|
|
1063
|
-
/**
|
|
1064
|
-
* 将已解析的 output items 包装为完整规范事件流。
|
|
1065
|
-
*
|
|
1066
|
-
* 用法示例(在 adapter 的 runStream 中):
|
|
1067
|
-
* ```ts
|
|
1068
|
-
* const result = parseNonStreamingResponse(data);
|
|
1069
|
-
* yield* syntheticStream({
|
|
1070
|
-
* model: request.model,
|
|
1071
|
-
* responseId: request.requestId,
|
|
1072
|
-
* backend: { kind: "chat-completions" },
|
|
1073
|
-
* output: result.output,
|
|
1074
|
-
* stopReason: result.stopReason,
|
|
1075
|
-
* usage: result.usage,
|
|
1076
|
-
* });
|
|
1077
|
-
* ```
|
|
1078
|
-
*/
|
|
1079
|
-
declare function syntheticStream(options: SyntheticStreamOptions): AsyncIterable<AIStreamEvent>;
|
|
1080
|
-
//#endregion
|
|
1081
|
-
//#region src/helpers/usage-mapping.d.ts
|
|
1082
|
-
/** OpenAI Chat Completions `usage` */
|
|
1083
|
-
declare function usageFromChatCompletions(raw: {
|
|
1084
|
-
prompt_tokens?: number;
|
|
1085
|
-
completion_tokens?: number;
|
|
1086
|
-
total_tokens?: number;
|
|
1087
|
-
prompt_tokens_details?: {
|
|
1088
|
-
cached_tokens?: number;
|
|
1089
|
-
[key: string]: unknown;
|
|
1090
|
-
};
|
|
1091
|
-
completion_tokens_details?: {
|
|
1092
|
-
reasoning_tokens?: number;
|
|
1093
|
-
[key: string]: unknown;
|
|
1094
|
-
};
|
|
1095
|
-
}): Partial<Usage>;
|
|
1096
|
-
/** OpenAI Responses API `usage` */
|
|
1097
|
-
declare function usageFromOpenAIResponses(raw: {
|
|
1098
|
-
input_tokens?: number;
|
|
1099
|
-
output_tokens?: number;
|
|
1100
|
-
total_tokens?: number;
|
|
1101
|
-
input_tokens_details?: {
|
|
1102
|
-
cached_tokens?: number;
|
|
1103
|
-
[key: string]: unknown;
|
|
1104
|
-
};
|
|
1105
|
-
output_tokens_details?: {
|
|
1106
|
-
reasoning_tokens?: number;
|
|
1107
|
-
[key: string]: unknown;
|
|
1108
|
-
};
|
|
1109
|
-
[key: string]: unknown;
|
|
1110
|
-
}): Partial<Usage>;
|
|
1111
|
-
/** Anthropic Messages `usage`(message_start / message_delta) */
|
|
1112
|
-
declare function usageFromAnthropicMessages(raw: {
|
|
1113
|
-
input_tokens?: number;
|
|
1114
|
-
output_tokens?: number;
|
|
1115
|
-
cache_creation_input_tokens?: number;
|
|
1116
|
-
cache_read_input_tokens?: number;
|
|
1117
|
-
[key: string]: unknown;
|
|
1118
|
-
}): Partial<Usage>;
|
|
1119
|
-
/** Ollama 流式 chunk */
|
|
1120
|
-
declare function usageFromOllama(raw: {
|
|
1121
|
-
prompt_eval_count?: number;
|
|
1122
|
-
eval_count?: number;
|
|
1123
|
-
}): Partial<Usage>;
|
|
1124
|
-
//#endregion
|
|
1125
|
-
//#region src/helpers/adapter-security.d.ts
|
|
1126
|
-
declare const MAX_OPAQUE_PAYLOAD_BYTES = 65536;
|
|
1127
|
-
declare const MAX_OPAQUE_JSON_DEPTH = 8;
|
|
1128
|
-
declare const PROVIDER_ERROR_MESSAGE_MAX_LEN = 500;
|
|
1129
|
-
declare const PROVIDER_ERROR_RAW_BODY_THRESHOLD = 200;
|
|
1130
|
-
type OpaqueEnvelopeResult = {
|
|
1131
|
-
ok: true;
|
|
1132
|
-
} | {
|
|
1133
|
-
ok: false;
|
|
1134
|
-
reason: string;
|
|
1135
|
-
};
|
|
1136
|
-
/** 测量 JSON 值嵌套深度(对象/数组);循环引用按已访问节点深度计。 */
|
|
1137
|
-
declare function measureJsonDepth(value: unknown, seen?: WeakSet<object>): number;
|
|
1138
|
-
/**
|
|
1139
|
-
* Opaque replay 通用 envelope:必须是 object、体积 ≤ 64KB、深度 ≤ 8。
|
|
1140
|
-
* 不校验 adapter 专用字段形状。
|
|
1141
|
-
*/
|
|
1142
|
-
declare function validateOpaqueReplayEnvelope(payload: unknown): OpaqueEnvelopeResult;
|
|
1143
|
-
/** envelope 失败时抛 AIRequestError。 */
|
|
1144
|
-
declare function assertOpaqueReplayEnvelope(payload: unknown): void;
|
|
1145
|
-
/**
|
|
1146
|
-
* 从 provider HTTP 错误 body 提取可对外暴露的短消息,避免泄漏 HTML / 内部路径等。
|
|
1147
|
-
*/
|
|
1148
|
-
declare function extractProviderErrorMessage(body: string, status: number): string;
|
|
1149
|
-
/** 统一构造脱敏后的 AIProviderError。 */
|
|
1150
|
-
declare function providerHttpError(status: number, body: string): AIProviderError;
|
|
1151
|
-
//#endregion
|
|
1152
|
-
//#region src/helpers/incremental-stream-parser.d.ts
|
|
1153
|
-
type StreamSplitResult = {
|
|
1154
|
-
items: string[];
|
|
1155
|
-
rest: string;
|
|
1156
|
-
};
|
|
1157
|
-
type StreamParseResult<T> = {
|
|
1158
|
-
status: "parsed";
|
|
1159
|
-
value: T;
|
|
1160
|
-
} | {
|
|
1161
|
-
status: "ignored";
|
|
1162
|
-
} | {
|
|
1163
|
-
status: "malformed";
|
|
1164
|
-
};
|
|
1165
|
-
declare class IncrementalStreamParser<T> {
|
|
1166
|
-
private readonly split;
|
|
1167
|
-
private readonly parse;
|
|
1168
|
-
private buffer;
|
|
1169
|
-
private readonly decoder;
|
|
1170
|
-
constructor(split: (buffer: string, allowEOF: boolean) => StreamSplitResult, parse: (item: string) => StreamParseResult<T>);
|
|
1171
|
-
feed(value: Uint8Array): {
|
|
1172
|
-
items: T[];
|
|
1173
|
-
malformed: number;
|
|
1174
|
-
};
|
|
1175
|
-
flush(): {
|
|
1176
|
-
items: T[];
|
|
1177
|
-
malformed: number;
|
|
1178
|
-
};
|
|
1179
|
-
getRemaining(): string;
|
|
1180
|
-
private consume;
|
|
1181
|
-
}
|
|
1182
|
-
declare function splitLines(buffer: string, allowEOF: boolean): StreamSplitResult;
|
|
1183
|
-
declare function splitSSEFrames(buffer: string, allowEOF: boolean): StreamSplitResult;
|
|
1184
|
-
type SseJsonEvent = {
|
|
1185
|
-
type: string;
|
|
1186
|
-
data: unknown;
|
|
1187
|
-
};
|
|
1188
|
-
/** 解析标准 SSE frame(event: + data:),用于 Messages / Responses。 */
|
|
1189
|
-
declare function parseSseJsonFrame(frame: string): StreamParseResult<SseJsonEvent>;
|
|
1190
|
-
declare function createSseJsonParser<T extends SseJsonEvent = SseJsonEvent>(): IncrementalStreamParser<T>;
|
|
1191
|
-
/** OpenAI Chat Completions 简化 SSE:仅 `data: ...` 行,忽略 `[DONE]`。 */
|
|
1192
|
-
declare function parseChatCompletionsDataLine(item: string): StreamParseResult<unknown>;
|
|
1193
|
-
declare function createChatCompletionsSseParser<T>(): IncrementalStreamParser<T>;
|
|
1194
|
-
/** NDJSON 行解析(Ollama 等):空行忽略,JSON 失败为 malformed。 */
|
|
1195
|
-
declare function createNdjsonLineParser<T>(isValid: (value: unknown) => value is T): IncrementalStreamParser<T>;
|
|
1196
1449
|
//#endregion
|
|
1197
|
-
//#region src/
|
|
1198
|
-
|
|
1199
|
-
fetchFn: FetchFn;
|
|
1200
|
-
url: string;
|
|
1201
|
-
headers: Record<string, string>;
|
|
1202
|
-
body: unknown;
|
|
1203
|
-
signal?: AbortSignal;
|
|
1204
|
-
};
|
|
1205
|
-
type OpenedProviderStream = {
|
|
1206
|
-
reader: ReadableStreamDefaultReader<Uint8Array>;
|
|
1207
|
-
headers: Headers;
|
|
1208
|
-
};
|
|
1209
|
-
/** POST JSON 并返回可读 body reader + response headers;统一网络/HTTP/空 body 错误。 */
|
|
1210
|
-
declare function openProviderJsonStream(options: OpenProviderJsonStreamOptions): Promise<OpenedProviderStream>;
|
|
1211
|
-
type ProviderStreamBatchOptions<T> = {
|
|
1212
|
-
reader: ReadableStreamDefaultReader<Uint8Array>;
|
|
1213
|
-
parser: IncrementalStreamParser<T>;
|
|
1214
|
-
factory: EventFactory;
|
|
1215
|
-
providerLabel: string;
|
|
1216
|
-
transportLabel: string;
|
|
1217
|
-
incompleteMessage: string;
|
|
1218
|
-
};
|
|
1219
|
-
type ProviderStreamBatch<T> = {
|
|
1220
|
-
items: T[];
|
|
1221
|
-
warnings: AIStreamEvent[];
|
|
1222
|
-
};
|
|
1223
|
-
/**
|
|
1224
|
-
* 读取并解析 provider 流。
|
|
1225
|
-
* 每个 batch 携带本轮解析出的 items 与(可选)malformed / incomplete warning。
|
|
1226
|
-
* 调用方应 `for await` 消费完毕;reader 在迭代结束时 cancel/release。
|
|
1227
|
-
*/
|
|
1228
|
-
declare function iterateProviderStreamBatches<T>(options: ProviderStreamBatchOptions<T>): AsyncGenerator<ProviderStreamBatch<T>, void, undefined>;
|
|
1229
|
-
/** 一次性 complete 守卫:首次成功,后续返回 false。 */
|
|
1230
|
-
declare function createCompletionGate(): {
|
|
1231
|
-
readonly completed: boolean;
|
|
1232
|
-
tryComplete(): boolean;
|
|
1233
|
-
};
|
|
1234
|
-
//#endregion
|
|
1235
|
-
//#region src/helpers/provider-request-options.d.ts
|
|
1236
|
-
/**
|
|
1237
|
-
* Provider 请求 headers / body 扩展合并
|
|
1238
|
-
*
|
|
1239
|
-
* 供真实后端 adapter 构造选项 `headers` / `extraBody` 使用:
|
|
1240
|
-
* - headers:内置鉴权头为基,自定义后写覆盖
|
|
1241
|
-
* - extraBody:已构建 body 为基,额外字段浅层 spread,同名顶层键可覆盖
|
|
1242
|
-
*/
|
|
1243
|
-
/** 合并内置 headers 与自定义 headers;自定义后写覆盖同名键。 */
|
|
1244
|
-
declare function mergeProviderHeaders(base: Record<string, string>, custom?: Record<string, string>): Record<string, string>;
|
|
1245
|
-
/**
|
|
1246
|
-
* 将构造期 extraBody 浅层合并到已构建的 provider body。
|
|
1247
|
-
* 无 extraBody 时原样返回;有则允许覆盖同名顶层键。
|
|
1248
|
-
*/
|
|
1249
|
-
declare function applyExtraBody<T extends object>(body: T, extraBody?: Record<string, unknown>): T;
|
|
1250
|
-
//#endregion
|
|
1251
|
-
//#region src/helpers/reasoning-level.d.ts
|
|
1252
|
-
declare const REASONING_LEVELS: readonly ["none", "minimal", "low", "medium", "high", "xhigh", "max"];
|
|
1253
|
-
declare const REASONING_LEVEL_SET: ReadonlySet<string>;
|
|
1254
|
-
type OpenAIReasoningEffort = ReasoningLevel;
|
|
1255
|
-
type MessagesThinkingConfig = {
|
|
1256
|
-
type: "disabled";
|
|
1257
|
-
} | {
|
|
1258
|
-
type: "enabled";
|
|
1259
|
-
budget_tokens: number;
|
|
1260
|
-
};
|
|
1261
|
-
type OllamaThinkValue = false | "low" | "medium" | "high";
|
|
1262
|
-
/** 若 level 不在 supported 集合内则抛 AIRequestError。 */
|
|
1263
|
-
declare function assertSupportedReasoningLevel(level: ReasoningLevel, supported: ReadonlySet<ReasoningLevel>, adapterKind: string): void;
|
|
1264
|
-
/** Responses API:`reasoning: { effort }` */
|
|
1265
|
-
declare function mapResponsesReasoning(level: ReasoningLevel): {
|
|
1266
|
-
effort: OpenAIReasoningEffort;
|
|
1267
|
-
};
|
|
1268
|
-
/** Chat Completions:顶层 `reasoning_effort` */
|
|
1269
|
-
declare function mapChatCompletionsReasoningEffort(level: ReasoningLevel): OpenAIReasoningEffort;
|
|
1270
|
-
/**
|
|
1271
|
-
* Messages thinking budget。
|
|
1272
|
-
* 基于 maxTokens 按比例推导,clamp 到 [1024, max(1024, maxTokens - 1)],
|
|
1273
|
-
* 满足 Anthropic budget_tokens < max_tokens。
|
|
1274
|
-
*/
|
|
1275
|
-
declare function mapMessagesThinkingBudget(level: Exclude<ReasoningLevel, "none">, maxTokens: number): number;
|
|
1276
|
-
/** Messages API:`thinking` 字段 */
|
|
1277
|
-
declare function mapMessagesThinking(level: ReasoningLevel, maxTokens: number): MessagesThinkingConfig;
|
|
1278
|
-
/** Ollama:`think` 字段;minimal/xhigh/max 不支持 */
|
|
1279
|
-
declare function mapOllamaThink(level: ReasoningLevel): OllamaThinkValue;
|
|
1450
|
+
//#region src/adapters/mock/expectations.d.ts
|
|
1451
|
+
declare function assertMockRequest(request: NormalizedRequest, expectation: MockRequestExpectation, context: MockHandlerContext): void;
|
|
1280
1452
|
//#endregion
|
|
1281
|
-
//#region src/
|
|
1282
|
-
declare
|
|
1283
|
-
readonly kind: string;
|
|
1284
|
-
constructor(kind: string);
|
|
1285
|
-
mapInstructions(instructions: string | InstructionBlock[]): string;
|
|
1286
|
-
ensureTextBlocks(blocks: ContentBlock[], field: string): ContentBlock[];
|
|
1287
|
-
ensureReasoningBlocks(blocks: ContentBlock[], field: string): Array<Extract<ContentBlock, {
|
|
1288
|
-
type: "text";
|
|
1289
|
-
}>>;
|
|
1290
|
-
/** ensureTextBlocks + contentBlocksToText 的常见组合。 */
|
|
1291
|
-
textFromBlocks(blocks: ContentBlock[], field: string): string;
|
|
1292
|
-
parseToolArguments(item: ToolCallItem): Record<string, unknown>;
|
|
1293
|
-
rollbackTrailingAssistantMessages<T extends {
|
|
1294
|
-
role: string;
|
|
1295
|
-
}>(messages: T[]): void;
|
|
1296
|
-
mapToolsIfPresent<T>(tools: ToolDefinition[] | undefined, map: (tool: ToolDefinition) => T): T[] | undefined;
|
|
1297
|
-
/**
|
|
1298
|
-
* 将 canonical toolChoice 映射为 provider 形状。
|
|
1299
|
-
* 返回 undefined 表示调用方无需写入 body 字段。
|
|
1300
|
-
*/
|
|
1301
|
-
mapToolChoice<T>(toolChoice: ToolChoice | undefined, mappers: {
|
|
1302
|
-
auto: T;
|
|
1303
|
-
none: T;
|
|
1304
|
-
tool: (name: string) => T;
|
|
1305
|
-
}): T | undefined;
|
|
1306
|
-
private ensureBlocks;
|
|
1307
|
-
}
|
|
1453
|
+
//#region src/adapters/mock/streaming.d.ts
|
|
1454
|
+
declare function withMockStreaming(handler: MockStaticHandler, options: MockTextStreamOptions): MockHandler;
|
|
1308
1455
|
//#endregion
|
|
1309
|
-
export { type AIClient, AIError, AIMappingError, AIProviderError, type AIRequest, AIRequestError, type AIResponse, AIStreamError, type AIStreamEvent,
|
|
1456
|
+
export { type AIClient, AIError, AIMappingError, AIProviderError, AIRecoverableError, type AIRequest, AIRequestError, type AIResponse, AIStreamError, type AIStreamEvent, type AdapterKind, type AuxiliaryInfo, type BackendAdapter, type BackendTrace, type BillingInfo, ChatCompletionsAdapter, type ChatCompletionsAdapterOptions, type Citation, type CodeExecutionServerTool, type CompressRequest, type CompressResult, type ContainerFileCitation, type ContentBlock, type ContextCompressCapable, type CreateAIClientOptions, type ErrorCode, type FetchFn, GeminiAdapter, type GeminiAdapterOptions, type IncludeSettings, type InputItem, type InstructionBlock, type JsonContentBlock, KNOWN_ADAPTER_KINDS, type KnownAdapterKind, type KnownErrorCode, type KnownWarningCode, type McpServerTool, type MessageCompletedEvent, type MessageDeltaEvent, type MessageItem, type MessageStartedEvent, MessagesAdapter, type MessagesAdapterOptions, MockAdapter, type MockAdapterOptions, type MockAuxiliaryStep, type MockCompleteStep, type MockCompressHandler, type MockErrorStep, type MockHandler, type MockHandlerContext, type MockHistoryRecord, type MockInputExpectation, type MockInterruptStep, type MockMessageStep, type MockOutputStep, type MockReasoningStep, type MockRequestExpectation, type MockServerToolCallStep, type MockServerToolDiscoveryStep, type MockServerToolResultStep, type MockStaticHandler, type MockStep, type MockTextStreamOptions, type MockThrowStep, type MockToolCallStep, type MockWarningStep, type NormalizedRequest, OllamaAdapter, type OllamaAdapterOptions, type OpaqueItem, type OutputItem, REASONING_LEVELS, REASONING_LEVEL_SET, type ReasoningCompletedEvent, type ReasoningDeltaEvent, type ReasoningItem, type ReasoningLevel, type ReasoningStartedEvent, type ReplayItem, type ResponseAuxiliaryEvent, type ResponseCompletedEvent, type ResponseStartedEvent, type ResponseWarningEvent, ResponsesAdapter, type ResponsesAdapterOptions, type ServerToolCallItem, type ServerToolCompletedEvent, type ServerToolDefinition, type ServerToolDeltaEvent, type ServerToolDiscoveryCompletedEvent, type ServerToolDiscoveryItem, type ServerToolResultCompletedEvent, type ServerToolResultItem, type ServerToolStartedEvent, type StopReason, type StreamEventBase, type StreamWarning, type TextContentBlock, type ToolCallCompletedEvent, type ToolCallDeltaEvent, type ToolCallItem, type ToolCallStartedEvent, type ToolChoice, type ToolDefinition, type ToolResultItem, type UrlCitation, type Usage, WarningCode, type WarningCodeName, type WarningCodeValue, type WebSearchServerTool, type WebSearchUserLocation, assertMockRequest, blockToText, coalesceContentBlocks, collectStream, contentBlocksToText, createAIClient, extractText, imageBlock, jsonBlock, mapReasoningVisibility, mapStopReason, messageItem, opaqueBlock, opaqueItem, reasoningItem, replayFromOutput, serverToolCallItem, serverToolDiscoveryItem, serverToolResultItem, streamWarningKey, supportsContextCompress, textBlock, toolCallItem, toolResultItem, withMockStreaming };
|
|
1310
1457
|
//# sourceMappingURL=index.d.mts.map
|