@longzai-intelligence-model-gateway/core 0.0.1 → 0.0.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/README.md +16 -0
- package/dist/index.d.ts +947 -249
- package/dist/index.js +1 -1
- package/package.json +25 -19
package/dist/index.d.ts
CHANGED
|
@@ -1,73 +1,185 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { DomainError } from "@longzai-intelligence/
|
|
3
|
-
import { ApplicationMapper, DomainEvent, Entity,
|
|
4
|
-
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
import { DomainError } from "@longzai-intelligence-error/core";
|
|
3
|
+
import { ApplicationMapper, DomainEvent, Entity, Result } from "@longzai-intelligence-shared-kernel/core";
|
|
5
4
|
//#region src/types/error.d.ts
|
|
6
5
|
/**
|
|
7
6
|
* 统一错误类型定义
|
|
8
7
|
* 包含错误分类、错误码、重试建议和降级建议
|
|
9
8
|
*/
|
|
10
|
-
/**
|
|
9
|
+
/**
|
|
10
|
+
* 错误分类
|
|
11
|
+
*/
|
|
11
12
|
type ErrorCategory = 'client' | 'server' | 'provider';
|
|
12
|
-
/**
|
|
13
|
+
/**
|
|
14
|
+
* 错误码
|
|
15
|
+
*/
|
|
13
16
|
declare enum ErrorCode {
|
|
17
|
+
/**
|
|
18
|
+
* 无效请求
|
|
19
|
+
*/
|
|
14
20
|
INVALID_REQUEST = "invalid_request",
|
|
21
|
+
/**
|
|
22
|
+
* 认证失败
|
|
23
|
+
*/
|
|
15
24
|
AUTHENTICATION_FAILED = "authentication_failed",
|
|
25
|
+
/**
|
|
26
|
+
* 权限不足
|
|
27
|
+
*/
|
|
16
28
|
PERMISSION_DENIED = "permission_denied",
|
|
29
|
+
/**
|
|
30
|
+
* 模型未找到
|
|
31
|
+
*/
|
|
17
32
|
MODEL_NOT_FOUND = "model_not_found",
|
|
33
|
+
/**
|
|
34
|
+
* 预算超限
|
|
35
|
+
*/
|
|
18
36
|
BUDGET_EXCEEDED = "budget_exceeded",
|
|
37
|
+
/**
|
|
38
|
+
* 速率限制超限
|
|
39
|
+
*/
|
|
19
40
|
RATE_LIMIT_EXCEEDED = "rate_limit_exceeded",
|
|
41
|
+
/**
|
|
42
|
+
* 无效 API 密钥
|
|
43
|
+
*/
|
|
20
44
|
INVALID_API_KEY = "invalid_api_key",
|
|
45
|
+
/**
|
|
46
|
+
* 内部错误
|
|
47
|
+
*/
|
|
21
48
|
INTERNAL_ERROR = "internal_error",
|
|
49
|
+
/**
|
|
50
|
+
* 路由错误
|
|
51
|
+
*/
|
|
22
52
|
ROUTING_ERROR = "routing_error",
|
|
53
|
+
/**
|
|
54
|
+
* 数据库错误
|
|
55
|
+
*/
|
|
23
56
|
DATABASE_ERROR = "database_error",
|
|
57
|
+
/**
|
|
58
|
+
* 缓存错误
|
|
59
|
+
*/
|
|
24
60
|
CACHE_ERROR = "cache_error",
|
|
61
|
+
/**
|
|
62
|
+
* 供应商不可用
|
|
63
|
+
*/
|
|
25
64
|
PROVIDER_UNAVAILABLE = "provider_unavailable",
|
|
65
|
+
/**
|
|
66
|
+
* 供应商速率限制
|
|
67
|
+
*/
|
|
26
68
|
PROVIDER_RATE_LIMITED = "provider_rate_limited",
|
|
69
|
+
/**
|
|
70
|
+
* 供应商超时
|
|
71
|
+
*/
|
|
27
72
|
PROVIDER_TIMEOUT = "provider_timeout",
|
|
73
|
+
/**
|
|
74
|
+
* 供应商错误
|
|
75
|
+
*/
|
|
28
76
|
PROVIDER_ERROR = "provider_error",
|
|
77
|
+
/**
|
|
78
|
+
* 模型过载
|
|
79
|
+
*/
|
|
29
80
|
MODEL_OVERLOADED = "model_overloaded",
|
|
81
|
+
/**
|
|
82
|
+
* 上下文长度超限
|
|
83
|
+
*/
|
|
30
84
|
CONTEXT_LENGTH_EXCEEDED = "context_length_exceeded"
|
|
31
85
|
}
|
|
32
|
-
/**
|
|
86
|
+
/**
|
|
87
|
+
* 重试建议
|
|
88
|
+
*/
|
|
33
89
|
type RetrySuggestion = {
|
|
34
|
-
/**
|
|
35
|
-
|
|
36
|
-
|
|
90
|
+
/**
|
|
91
|
+
* 是否可重试
|
|
92
|
+
*/
|
|
93
|
+
retryable: boolean;
|
|
94
|
+
/**
|
|
95
|
+
* 建议重试次数
|
|
96
|
+
*/
|
|
97
|
+
maxRetries: number;
|
|
98
|
+
/**
|
|
99
|
+
* 建议重试间隔(毫秒)
|
|
100
|
+
*/
|
|
101
|
+
retryAfter: number;
|
|
102
|
+
/**
|
|
103
|
+
* 可重试的错误码
|
|
104
|
+
*/
|
|
37
105
|
retryableCodes: ErrorCode[];
|
|
38
106
|
};
|
|
39
|
-
/**
|
|
107
|
+
/**
|
|
108
|
+
* 降级建议
|
|
109
|
+
*/
|
|
40
110
|
type FallbackSuggestion = {
|
|
41
|
-
/**
|
|
42
|
-
|
|
43
|
-
|
|
111
|
+
/**
|
|
112
|
+
* 是否可降级
|
|
113
|
+
*/
|
|
114
|
+
fallbackable: boolean;
|
|
115
|
+
/**
|
|
116
|
+
* 备用模型
|
|
117
|
+
*/
|
|
118
|
+
fallbackModel?: string;
|
|
119
|
+
/**
|
|
120
|
+
* 备用供应商
|
|
121
|
+
*/
|
|
122
|
+
fallbackProvider?: string;
|
|
123
|
+
/**
|
|
124
|
+
* 是否可返回缓存
|
|
125
|
+
*/
|
|
44
126
|
cacheAvailable: boolean;
|
|
45
127
|
};
|
|
46
|
-
/**
|
|
128
|
+
/**
|
|
129
|
+
* 网关错误详情
|
|
130
|
+
*/
|
|
47
131
|
type GatewayErrorDetail = {
|
|
48
|
-
/**
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
132
|
+
/**
|
|
133
|
+
* 错误码
|
|
134
|
+
*/
|
|
135
|
+
code: ErrorCode;
|
|
136
|
+
/**
|
|
137
|
+
* 错误分类
|
|
138
|
+
*/
|
|
139
|
+
category: ErrorCategory;
|
|
140
|
+
/**
|
|
141
|
+
* 错误消息
|
|
142
|
+
*/
|
|
143
|
+
message: string;
|
|
144
|
+
/**
|
|
145
|
+
* 详细错误信息
|
|
146
|
+
*/
|
|
147
|
+
detail?: string;
|
|
148
|
+
/**
|
|
149
|
+
* 重试建议
|
|
150
|
+
*/
|
|
151
|
+
retry: RetrySuggestion;
|
|
152
|
+
/**
|
|
153
|
+
* 降级建议
|
|
154
|
+
*/
|
|
155
|
+
fallback: FallbackSuggestion;
|
|
156
|
+
/**
|
|
157
|
+
* 供应商原始错误
|
|
158
|
+
*/
|
|
54
159
|
providerError?: {
|
|
55
160
|
provider: string;
|
|
56
161
|
code?: string;
|
|
57
162
|
message?: string;
|
|
58
163
|
};
|
|
59
164
|
};
|
|
60
|
-
/**
|
|
165
|
+
/**
|
|
166
|
+
* 网关错误(兼容 OpenAI 错误格式)
|
|
167
|
+
*/
|
|
61
168
|
type GatewayError = {
|
|
62
169
|
error: {
|
|
63
170
|
message: string;
|
|
64
171
|
type: string;
|
|
65
172
|
code: ErrorCode;
|
|
66
|
-
param?: string;
|
|
173
|
+
param?: string;
|
|
174
|
+
/**
|
|
175
|
+
* 网关扩展字段
|
|
176
|
+
*/
|
|
67
177
|
gateway?: GatewayErrorDetail;
|
|
68
178
|
};
|
|
69
179
|
};
|
|
70
|
-
/**
|
|
180
|
+
/**
|
|
181
|
+
* HTTP 状态码映射
|
|
182
|
+
*/
|
|
71
183
|
declare const ERROR_STATUS_MAP: Record<ErrorCategory, number>;
|
|
72
184
|
//#endregion
|
|
73
185
|
//#region src/types/request.d.ts
|
|
@@ -75,11 +187,17 @@ declare const ERROR_STATUS_MAP: Record<ErrorCategory, number>;
|
|
|
75
187
|
* 统一请求类型定义
|
|
76
188
|
* 完全兼容 OpenAI API 格式,并扩展路由策略、优先级、预算限制等参数
|
|
77
189
|
*/
|
|
78
|
-
/**
|
|
190
|
+
/**
|
|
191
|
+
* 消息角色
|
|
192
|
+
*/
|
|
79
193
|
type MessageRole = 'system' | 'user' | 'assistant' | 'tool';
|
|
80
|
-
/**
|
|
194
|
+
/**
|
|
195
|
+
* 消息内容类型
|
|
196
|
+
*/
|
|
81
197
|
type MessageContentType = 'text' | 'image_url';
|
|
82
|
-
/**
|
|
198
|
+
/**
|
|
199
|
+
* 消息内容项
|
|
200
|
+
*/
|
|
83
201
|
type MessageContentPart = {
|
|
84
202
|
type: MessageContentType;
|
|
85
203
|
text?: string;
|
|
@@ -88,74 +206,200 @@ type MessageContentPart = {
|
|
|
88
206
|
detail?: 'auto' | 'low' | 'high';
|
|
89
207
|
};
|
|
90
208
|
};
|
|
91
|
-
/**
|
|
209
|
+
/**
|
|
210
|
+
* 工具调用函数
|
|
211
|
+
*/
|
|
92
212
|
type ToolCallFunction = {
|
|
93
|
-
/**
|
|
213
|
+
/**
|
|
214
|
+
* 调用名称
|
|
215
|
+
*/
|
|
216
|
+
name: string;
|
|
217
|
+
/**
|
|
218
|
+
* 调用参数(JSON字符串)
|
|
219
|
+
*/
|
|
94
220
|
arguments: string;
|
|
95
221
|
};
|
|
96
|
-
/**
|
|
222
|
+
/**
|
|
223
|
+
* 工具调用
|
|
224
|
+
*/
|
|
97
225
|
type ToolCall = {
|
|
98
|
-
/**
|
|
99
|
-
|
|
226
|
+
/**
|
|
227
|
+
* 工具调用ID
|
|
228
|
+
*/
|
|
229
|
+
id: string;
|
|
230
|
+
/**
|
|
231
|
+
* 调用类型,固定为 function
|
|
232
|
+
*/
|
|
233
|
+
type: 'function';
|
|
234
|
+
/**
|
|
235
|
+
* 调用的函数
|
|
236
|
+
*/
|
|
100
237
|
function: ToolCallFunction;
|
|
101
238
|
};
|
|
102
|
-
/**
|
|
239
|
+
/**
|
|
240
|
+
* 聊天消息
|
|
241
|
+
*/
|
|
103
242
|
type ChatMessage = {
|
|
104
|
-
/**
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
243
|
+
/**
|
|
244
|
+
* 消息角色
|
|
245
|
+
*/
|
|
246
|
+
role: MessageRole;
|
|
247
|
+
/**
|
|
248
|
+
* 消息文本内容(与content二选一)
|
|
249
|
+
*/
|
|
250
|
+
content?: string | MessageContentPart[];
|
|
251
|
+
/**
|
|
252
|
+
* 工具调用列表(assistant角色)
|
|
253
|
+
*/
|
|
254
|
+
tool_calls?: ToolCall[];
|
|
255
|
+
/**
|
|
256
|
+
* 工具调用ID(tool角色)
|
|
257
|
+
*/
|
|
258
|
+
tool_call_id?: string;
|
|
259
|
+
/**
|
|
260
|
+
* 消息名称
|
|
261
|
+
*/
|
|
108
262
|
name?: string;
|
|
109
263
|
};
|
|
110
|
-
/**
|
|
264
|
+
/**
|
|
265
|
+
* 函数定义
|
|
266
|
+
*/
|
|
111
267
|
type FunctionDefinition = {
|
|
112
|
-
/**
|
|
113
|
-
|
|
268
|
+
/**
|
|
269
|
+
* 函数名称
|
|
270
|
+
*/
|
|
271
|
+
name: string;
|
|
272
|
+
/**
|
|
273
|
+
* 函数功能描述(供模型理解该函数的用途及调用时机)
|
|
274
|
+
*/
|
|
275
|
+
description?: string;
|
|
276
|
+
/**
|
|
277
|
+
* 函数参数 JSON Schema
|
|
278
|
+
*/
|
|
114
279
|
parameters?: Record<string, unknown>;
|
|
115
280
|
};
|
|
116
|
-
/**
|
|
281
|
+
/**
|
|
282
|
+
* 工具定义
|
|
283
|
+
*/
|
|
117
284
|
type ToolDefinition = {
|
|
118
|
-
/**
|
|
285
|
+
/**
|
|
286
|
+
* 工具类型
|
|
287
|
+
*/
|
|
288
|
+
type: 'function';
|
|
289
|
+
/**
|
|
290
|
+
* 函数定义
|
|
291
|
+
*/
|
|
119
292
|
function: FunctionDefinition;
|
|
120
293
|
};
|
|
121
|
-
/**
|
|
294
|
+
/**
|
|
295
|
+
* 响应格式
|
|
296
|
+
*/
|
|
122
297
|
type ResponseFormat = {
|
|
123
298
|
type: 'text' | 'json_object' | 'json_schema';
|
|
124
299
|
json_schema?: Record<string, unknown>;
|
|
125
300
|
};
|
|
126
|
-
/**
|
|
301
|
+
/**
|
|
302
|
+
* 路由策略扩展参数
|
|
303
|
+
*/
|
|
127
304
|
type RoutingExtension = {
|
|
128
|
-
/**
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
305
|
+
/**
|
|
306
|
+
* 路由策略
|
|
307
|
+
*/
|
|
308
|
+
routingStrategy?: 'model-name' | 'cost-first' | 'performance-first' | 'quality-first';
|
|
309
|
+
/**
|
|
310
|
+
* 请求优先级
|
|
311
|
+
*/
|
|
312
|
+
priority?: 'low' | 'normal' | 'high';
|
|
313
|
+
/**
|
|
314
|
+
* 预算限制(美元)
|
|
315
|
+
*/
|
|
316
|
+
budgetLimit?: number;
|
|
317
|
+
/**
|
|
318
|
+
* 请求超时时间(毫秒)
|
|
319
|
+
*/
|
|
320
|
+
timeout?: number;
|
|
321
|
+
/**
|
|
322
|
+
* 是否禁用缓存
|
|
323
|
+
*/
|
|
324
|
+
noCache?: boolean;
|
|
325
|
+
/**
|
|
326
|
+
* 调用者标签(用于成本归因)
|
|
327
|
+
*/
|
|
133
328
|
tags?: Record<string, string>;
|
|
134
329
|
};
|
|
135
|
-
/**
|
|
330
|
+
/**
|
|
331
|
+
* 聊天补全请求(兼容 OpenAI ChatCompletion)
|
|
332
|
+
*/
|
|
136
333
|
type ChatCompletionRequest = RoutingExtension & {
|
|
137
|
-
/**
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
334
|
+
/**
|
|
335
|
+
* 模型名称或别名
|
|
336
|
+
*/
|
|
337
|
+
model: string;
|
|
338
|
+
/**
|
|
339
|
+
* 消息列表
|
|
340
|
+
*/
|
|
341
|
+
messages: ChatMessage[];
|
|
342
|
+
/**
|
|
343
|
+
* 生成温度 (0-2)
|
|
344
|
+
*/
|
|
345
|
+
temperature?: number;
|
|
346
|
+
/**
|
|
347
|
+
* 核采样概率
|
|
348
|
+
*/
|
|
349
|
+
top_p?: number;
|
|
350
|
+
/**
|
|
351
|
+
* 生成数量
|
|
352
|
+
*/
|
|
353
|
+
n?: number;
|
|
354
|
+
/**
|
|
355
|
+
* 是否流式输出
|
|
356
|
+
*/
|
|
357
|
+
stream?: boolean;
|
|
358
|
+
/**
|
|
359
|
+
* 停止词
|
|
360
|
+
*/
|
|
361
|
+
stop?: string | string[];
|
|
362
|
+
/**
|
|
363
|
+
* 最大输出 Token 数
|
|
364
|
+
*/
|
|
365
|
+
max_tokens?: number;
|
|
366
|
+
/**
|
|
367
|
+
* 存在惩罚 (-2 到 2)
|
|
368
|
+
*/
|
|
369
|
+
presence_penalty?: number;
|
|
370
|
+
/**
|
|
371
|
+
* 频率惩罚 (-2 到 2)
|
|
372
|
+
*/
|
|
373
|
+
frequency_penalty?: number;
|
|
374
|
+
/**
|
|
375
|
+
* 随机种子
|
|
376
|
+
*/
|
|
377
|
+
seed?: number;
|
|
378
|
+
/**
|
|
379
|
+
* 工具定义列表
|
|
380
|
+
*/
|
|
381
|
+
tools?: ToolDefinition[];
|
|
382
|
+
/**
|
|
383
|
+
* 工具选择策略
|
|
384
|
+
*/
|
|
149
385
|
tool_choice?: 'none' | 'auto' | 'required' | {
|
|
150
386
|
type: 'function';
|
|
151
387
|
function: {
|
|
152
388
|
name: string;
|
|
153
389
|
};
|
|
154
|
-
};
|
|
155
|
-
|
|
390
|
+
};
|
|
391
|
+
/**
|
|
392
|
+
* 响应格式
|
|
393
|
+
*/
|
|
394
|
+
response_format?: ResponseFormat;
|
|
395
|
+
/**
|
|
396
|
+
* 用户标识
|
|
397
|
+
*/
|
|
156
398
|
user?: string;
|
|
157
399
|
};
|
|
158
|
-
/**
|
|
400
|
+
/**
|
|
401
|
+
* 文本补全请求(兼容 OpenAI Completion)
|
|
402
|
+
*/
|
|
159
403
|
type CompletionRequest = RoutingExtension & {
|
|
160
404
|
model: string;
|
|
161
405
|
prompt: string | string[];
|
|
@@ -170,7 +414,9 @@ type CompletionRequest = RoutingExtension & {
|
|
|
170
414
|
seed?: number;
|
|
171
415
|
user?: string;
|
|
172
416
|
};
|
|
173
|
-
/**
|
|
417
|
+
/**
|
|
418
|
+
* 嵌入请求(兼容 OpenAI Embedding)
|
|
419
|
+
*/
|
|
174
420
|
type EmbeddingRequest = RoutingExtension & {
|
|
175
421
|
model: string;
|
|
176
422
|
input: string | string[];
|
|
@@ -180,19 +426,34 @@ type EmbeddingRequest = RoutingExtension & {
|
|
|
180
426
|
};
|
|
181
427
|
//#endregion
|
|
182
428
|
//#region src/types/response.d.ts
|
|
183
|
-
/**
|
|
429
|
+
/**
|
|
430
|
+
* 响应消息
|
|
431
|
+
*/
|
|
184
432
|
type ResponseMessage = {
|
|
185
433
|
role: 'assistant';
|
|
186
434
|
content: string | null;
|
|
187
435
|
tool_calls?: ToolCall[];
|
|
188
436
|
};
|
|
189
|
-
/**
|
|
437
|
+
/**
|
|
438
|
+
* 补全选择项
|
|
439
|
+
*/
|
|
190
440
|
type ChatChoice = {
|
|
191
|
-
/**
|
|
192
|
-
|
|
441
|
+
/**
|
|
442
|
+
* 选择项索引
|
|
443
|
+
*/
|
|
444
|
+
index: number;
|
|
445
|
+
/**
|
|
446
|
+
* 消息内容
|
|
447
|
+
*/
|
|
448
|
+
message: ResponseMessage;
|
|
449
|
+
/**
|
|
450
|
+
* 结束原因
|
|
451
|
+
*/
|
|
193
452
|
finish_reason: 'stop' | 'length' | 'tool_calls' | 'content_filter' | null;
|
|
194
453
|
};
|
|
195
|
-
/**
|
|
454
|
+
/**
|
|
455
|
+
* 流式补全增量
|
|
456
|
+
*/
|
|
196
457
|
type ChatChunkDelta = {
|
|
197
458
|
role?: 'assistant';
|
|
198
459
|
content?: string | null;
|
|
@@ -206,25 +467,49 @@ type ChatChunkDelta = {
|
|
|
206
467
|
};
|
|
207
468
|
}>;
|
|
208
469
|
};
|
|
209
|
-
/**
|
|
470
|
+
/**
|
|
471
|
+
* 流式选择项
|
|
472
|
+
*/
|
|
210
473
|
type ChatStreamChoice = {
|
|
211
474
|
index: number;
|
|
212
475
|
delta: ChatChunkDelta;
|
|
213
476
|
finish_reason: 'stop' | 'length' | 'tool_calls' | 'content_filter' | null;
|
|
214
477
|
};
|
|
215
|
-
/**
|
|
478
|
+
/**
|
|
479
|
+
* Token 使用量
|
|
480
|
+
*/
|
|
216
481
|
type TokenUsage = {
|
|
217
482
|
prompt_tokens: number;
|
|
218
483
|
completion_tokens: number;
|
|
219
484
|
total_tokens: number;
|
|
220
485
|
};
|
|
221
|
-
/**
|
|
486
|
+
/**
|
|
487
|
+
* 网关扩展响应字段
|
|
488
|
+
*/
|
|
222
489
|
type GatewayExtension = {
|
|
223
|
-
/**
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
490
|
+
/**
|
|
491
|
+
* 实际使用的模型ID
|
|
492
|
+
*/
|
|
493
|
+
gateway_model?: string;
|
|
494
|
+
/**
|
|
495
|
+
* 实际使用的供应商
|
|
496
|
+
*/
|
|
497
|
+
gateway_provider?: string;
|
|
498
|
+
/**
|
|
499
|
+
* 请求延迟(毫秒)
|
|
500
|
+
*/
|
|
501
|
+
gateway_latency_ms?: number;
|
|
502
|
+
/**
|
|
503
|
+
* 请求成本(美元)
|
|
504
|
+
*/
|
|
505
|
+
gateway_cost?: number;
|
|
506
|
+
/**
|
|
507
|
+
* 是否命中缓存
|
|
508
|
+
*/
|
|
509
|
+
gateway_cache_hit?: boolean;
|
|
510
|
+
/**
|
|
511
|
+
* 路由决策信息
|
|
512
|
+
*/
|
|
228
513
|
gateway_routing?: {
|
|
229
514
|
strategy: string;
|
|
230
515
|
original_model?: string;
|
|
@@ -232,7 +517,9 @@ type GatewayExtension = {
|
|
|
232
517
|
routed_provider: string;
|
|
233
518
|
};
|
|
234
519
|
};
|
|
235
|
-
/**
|
|
520
|
+
/**
|
|
521
|
+
* 聊天补全响应(兼容 OpenAI ChatCompletion)
|
|
522
|
+
*/
|
|
236
523
|
type ChatCompletionResponse = GatewayExtension & {
|
|
237
524
|
id: string;
|
|
238
525
|
object: 'chat.completion';
|
|
@@ -242,7 +529,9 @@ type ChatCompletionResponse = GatewayExtension & {
|
|
|
242
529
|
usage: TokenUsage;
|
|
243
530
|
system_fingerprint?: string;
|
|
244
531
|
};
|
|
245
|
-
/**
|
|
532
|
+
/**
|
|
533
|
+
* 流式聊天补全块
|
|
534
|
+
*/
|
|
246
535
|
type ChatCompletionChunk = GatewayExtension & {
|
|
247
536
|
id: string;
|
|
248
537
|
object: 'chat.completion.chunk';
|
|
@@ -250,13 +539,17 @@ type ChatCompletionChunk = GatewayExtension & {
|
|
|
250
539
|
model: string;
|
|
251
540
|
choices: ChatStreamChoice[];
|
|
252
541
|
};
|
|
253
|
-
/**
|
|
542
|
+
/**
|
|
543
|
+
* 文本补全选择项
|
|
544
|
+
*/
|
|
254
545
|
type CompletionChoice = {
|
|
255
546
|
index: number;
|
|
256
547
|
text: string;
|
|
257
548
|
finish_reason: 'stop' | 'length' | 'tool_calls' | 'content_filter' | null;
|
|
258
549
|
};
|
|
259
|
-
/**
|
|
550
|
+
/**
|
|
551
|
+
* 文本补全响应(兼容 OpenAI Completion)
|
|
552
|
+
*/
|
|
260
553
|
type CompletionResponse = GatewayExtension & {
|
|
261
554
|
id: string;
|
|
262
555
|
object: 'text_completion';
|
|
@@ -265,13 +558,17 @@ type CompletionResponse = GatewayExtension & {
|
|
|
265
558
|
choices: CompletionChoice[];
|
|
266
559
|
usage: TokenUsage;
|
|
267
560
|
};
|
|
268
|
-
/**
|
|
561
|
+
/**
|
|
562
|
+
* 嵌入数据项
|
|
563
|
+
*/
|
|
269
564
|
type EmbeddingData = {
|
|
270
565
|
object: 'embedding';
|
|
271
566
|
index: number;
|
|
272
567
|
embedding: number[] | string;
|
|
273
568
|
};
|
|
274
|
-
/**
|
|
569
|
+
/**
|
|
570
|
+
* 嵌入响应(兼容 OpenAI Embedding)
|
|
571
|
+
*/
|
|
275
572
|
type EmbeddingResponse = GatewayExtension & {
|
|
276
573
|
id: string;
|
|
277
574
|
object: 'list';
|
|
@@ -280,14 +577,18 @@ type EmbeddingResponse = GatewayExtension & {
|
|
|
280
577
|
data: EmbeddingData[];
|
|
281
578
|
usage: TokenUsage;
|
|
282
579
|
};
|
|
283
|
-
/**
|
|
580
|
+
/**
|
|
581
|
+
* 模型信息(兼容 OpenAI Model)
|
|
582
|
+
*/
|
|
284
583
|
type ModelInfo = {
|
|
285
584
|
id: string;
|
|
286
585
|
object: 'model';
|
|
287
586
|
created: number;
|
|
288
587
|
owned_by: string;
|
|
289
588
|
};
|
|
290
|
-
/**
|
|
589
|
+
/**
|
|
590
|
+
* 模型列表响应
|
|
591
|
+
*/
|
|
291
592
|
type ModelListResponse = {
|
|
292
593
|
object: 'list';
|
|
293
594
|
data: ModelInfo[];
|
|
@@ -298,61 +599,159 @@ type ModelListResponse = {
|
|
|
298
599
|
* 执行策略类型定义
|
|
299
600
|
* 包含重试策略、超时策略、降级策略
|
|
300
601
|
*/
|
|
301
|
-
/**
|
|
602
|
+
/**
|
|
603
|
+
* 重试策略
|
|
604
|
+
*/
|
|
302
605
|
type RetryStrategy = {
|
|
303
|
-
/**
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
606
|
+
/**
|
|
607
|
+
* 最大重试次数(默认3次)
|
|
608
|
+
*/
|
|
609
|
+
maxRetries: number;
|
|
610
|
+
/**
|
|
611
|
+
* 初始重试间隔(毫秒,默认1000)
|
|
612
|
+
*/
|
|
613
|
+
initialInterval: number;
|
|
614
|
+
/**
|
|
615
|
+
* 最大重试间隔(毫秒,默认30000)
|
|
616
|
+
*/
|
|
617
|
+
maxInterval: number;
|
|
618
|
+
/**
|
|
619
|
+
* 退避乘数(默认2)
|
|
620
|
+
*/
|
|
621
|
+
multiplier: number;
|
|
622
|
+
/**
|
|
623
|
+
* 可重试的HTTP状态码
|
|
624
|
+
*/
|
|
625
|
+
retryableStatusCodes: number[];
|
|
626
|
+
/**
|
|
627
|
+
* 可重试的错误码
|
|
628
|
+
*/
|
|
308
629
|
retryableErrorCodes: string[];
|
|
309
630
|
};
|
|
310
|
-
/**
|
|
631
|
+
/**
|
|
632
|
+
* 超时策略
|
|
633
|
+
*/
|
|
311
634
|
type TimeoutStrategy = {
|
|
312
|
-
/**
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
635
|
+
/**
|
|
636
|
+
* 连接超时(毫秒,默认10000)
|
|
637
|
+
*/
|
|
638
|
+
connectTimeout: number;
|
|
639
|
+
/**
|
|
640
|
+
* 读取超时(毫秒,默认60000)
|
|
641
|
+
*/
|
|
642
|
+
readTimeout: number;
|
|
643
|
+
/**
|
|
644
|
+
* 写入超时(毫秒,默认10000)
|
|
645
|
+
*/
|
|
646
|
+
writeTimeout: number;
|
|
647
|
+
/**
|
|
648
|
+
* 总请求超时(毫秒,默认120000)
|
|
649
|
+
*/
|
|
650
|
+
requestTimeout: number;
|
|
651
|
+
};
|
|
652
|
+
/**
|
|
653
|
+
* 降级动作类型
|
|
654
|
+
*/
|
|
655
|
+
type FallbackAction = 'fallback_model' | 'cache_result' | 'default_response' | 'reject';
|
|
656
|
+
/**
|
|
657
|
+
* 降级策略
|
|
658
|
+
*/
|
|
659
|
+
type FallbackStrategy = {
|
|
660
|
+
/**
|
|
661
|
+
* 降级动作列表(按优先级排序)
|
|
662
|
+
*/
|
|
663
|
+
actions: FallbackAction[];
|
|
664
|
+
/**
|
|
665
|
+
* 备用模型列表
|
|
666
|
+
*/
|
|
667
|
+
fallbackModels: string[];
|
|
668
|
+
/**
|
|
669
|
+
* 是否允许返回缓存
|
|
670
|
+
*/
|
|
671
|
+
allowCache: boolean;
|
|
672
|
+
/**
|
|
673
|
+
* 默认响应内容
|
|
674
|
+
*/
|
|
675
|
+
defaultResponse?: string;
|
|
676
|
+
/**
|
|
677
|
+
* 最大降级次数
|
|
678
|
+
*/
|
|
325
679
|
maxFallbacks: number;
|
|
326
680
|
};
|
|
327
|
-
/**
|
|
681
|
+
/**
|
|
682
|
+
* 执行策略(组合重试、超时、降级)
|
|
683
|
+
*/
|
|
328
684
|
type ExecutionStrategy = {
|
|
329
|
-
/**
|
|
330
|
-
|
|
685
|
+
/**
|
|
686
|
+
* 重试策略
|
|
687
|
+
*/
|
|
688
|
+
retry: RetryStrategy;
|
|
689
|
+
/**
|
|
690
|
+
* 超时策略
|
|
691
|
+
*/
|
|
692
|
+
timeout: TimeoutStrategy;
|
|
693
|
+
/**
|
|
694
|
+
* 降级策略
|
|
695
|
+
*/
|
|
331
696
|
fallback: FallbackStrategy;
|
|
332
697
|
};
|
|
333
|
-
/**
|
|
698
|
+
/**
|
|
699
|
+
* 默认重试策略
|
|
700
|
+
*/
|
|
334
701
|
declare const DEFAULT_RETRY_STRATEGY: RetryStrategy;
|
|
335
|
-
/**
|
|
702
|
+
/**
|
|
703
|
+
* 默认超时策略
|
|
704
|
+
*/
|
|
336
705
|
declare const DEFAULT_TIMEOUT_STRATEGY: TimeoutStrategy;
|
|
337
|
-
/**
|
|
706
|
+
/**
|
|
707
|
+
* 默认降级策略
|
|
708
|
+
*/
|
|
338
709
|
declare const DEFAULT_FALLBACK_STRATEGY: FallbackStrategy;
|
|
339
|
-
/**
|
|
710
|
+
/**
|
|
711
|
+
* 默认执行策略
|
|
712
|
+
*/
|
|
340
713
|
declare const DEFAULT_EXECUTION_STRATEGY: ExecutionStrategy;
|
|
341
714
|
//#endregion
|
|
342
715
|
//#region src/utils/cost.d.ts
|
|
343
716
|
/**
|
|
344
717
|
* 成本计算工具函数
|
|
345
718
|
*/
|
|
346
|
-
/**
|
|
719
|
+
/**
|
|
720
|
+
* 模型定价信息
|
|
721
|
+
*/
|
|
347
722
|
type ModelPricing = {
|
|
348
|
-
/**
|
|
723
|
+
/**
|
|
724
|
+
* 输入 Token 价格(美元/千Token)
|
|
725
|
+
*/
|
|
726
|
+
inputPrice: number;
|
|
727
|
+
/**
|
|
728
|
+
* 输出 Token 价格(美元/千Token)
|
|
729
|
+
*/
|
|
349
730
|
outputPrice: number;
|
|
350
731
|
};
|
|
351
|
-
/**
|
|
732
|
+
/**
|
|
733
|
+
* 计算单次请求成本
|
|
734
|
+
*
|
|
735
|
+
* @param pricing - 模型定价信息(输入/输出单价,美元/千Token)
|
|
736
|
+
* @param inputTokens - 输入 Token 数量
|
|
737
|
+
* @param outputTokens - 输出 Token 数量
|
|
738
|
+
* @returns 请求总成本(美元),精确到小数点后8位
|
|
739
|
+
*/
|
|
352
740
|
declare function calculateCost(pricing: ModelPricing, inputTokens: number, outputTokens: number): number;
|
|
353
|
-
/**
|
|
741
|
+
/**
|
|
742
|
+
* 格式化成本显示
|
|
743
|
+
*
|
|
744
|
+
* @param cost - 成本金额(美元)
|
|
745
|
+
* @returns 格式化后的成本字符串,根据金额大小选择不同精度
|
|
746
|
+
*/
|
|
354
747
|
declare function formatCost(cost: number): string;
|
|
355
|
-
/**
|
|
748
|
+
/**
|
|
749
|
+
* 计算预算使用百分比
|
|
750
|
+
*
|
|
751
|
+
* @param used - 已使用金额(美元)
|
|
752
|
+
* @param limit - 预算上限(美元)
|
|
753
|
+
* @returns 预算使用百分比(0-100),精确到小数点后2位;上限非正数时返回 0
|
|
754
|
+
*/
|
|
356
755
|
declare function budgetUsagePercent(used: number, limit: number): number;
|
|
357
756
|
//#endregion
|
|
358
757
|
//#region src/utils/crypto.d.ts
|
|
@@ -360,26 +759,47 @@ declare function budgetUsagePercent(used: number, limit: number): number;
|
|
|
360
759
|
* 加密解密工具函数
|
|
361
760
|
* 使用 AES-256-GCM 算法加密 API 密钥等敏感数据
|
|
362
761
|
*/
|
|
363
|
-
/**
|
|
762
|
+
/**
|
|
763
|
+
* 加密文本
|
|
764
|
+
*
|
|
765
|
+
* @param plaintext - 待加密的明文字符串
|
|
766
|
+
* @param secret - 用于派生加密密钥的原始密钥
|
|
767
|
+
* @returns Base64 编码的密文(包含 IV 和认证标签)
|
|
768
|
+
*/
|
|
364
769
|
declare function encrypt(plaintext: string, secret: string): Promise<string>;
|
|
365
|
-
/**
|
|
770
|
+
/**
|
|
771
|
+
* 解密文本
|
|
772
|
+
*
|
|
773
|
+
* @param ciphertext - encrypt 生成的 Base64 密文(含 IV 和认证标签)
|
|
774
|
+
* @param secret - 用于派生解密密钥的原始密钥(须与加密时一致)
|
|
775
|
+
* @returns 解密后的明文字符串
|
|
776
|
+
*/
|
|
366
777
|
declare function decrypt(ciphertext: string, secret: string): Promise<string>;
|
|
367
778
|
//#endregion
|
|
368
779
|
//#region src/utils/token.d.ts
|
|
369
780
|
/**
|
|
370
781
|
* Token 计数工具函数
|
|
371
782
|
* 提供基于字符数的粗略 Token 估算
|
|
783
|
+
*
|
|
784
|
+
* @param text - 待估算的文本内容
|
|
785
|
+
* @returns 估算的 Token 数量
|
|
372
786
|
*/
|
|
373
|
-
/** 粗略估算文本的 Token 数(中文约1.5字/Token,英文约4字符/Token) */
|
|
374
787
|
declare function estimateTokens(text: string): number;
|
|
375
|
-
/**
|
|
788
|
+
/**
|
|
789
|
+
* 估算消息列表的 Token 数
|
|
790
|
+
*
|
|
791
|
+
* @param messages - 聊天消息列表(content 可为字符串或多模态内容数组)
|
|
792
|
+
* @returns 消息列表的估算 Token 总数(每条消息额外计入约 4 个 Token 的角色标记开销)
|
|
793
|
+
*/
|
|
376
794
|
declare function estimateMessagesTokens(messages: Array<{
|
|
377
795
|
role: string;
|
|
378
796
|
content?: string | unknown[];
|
|
379
797
|
}>): number;
|
|
380
798
|
//#endregion
|
|
381
799
|
//#region src/cache/cache.dto.d.ts
|
|
382
|
-
/**
|
|
800
|
+
/**
|
|
801
|
+
* 缓存条目响应 DTO
|
|
802
|
+
*/
|
|
383
803
|
type CacheEntryResponseDTO = {
|
|
384
804
|
id: string;
|
|
385
805
|
requestHash: string;
|
|
@@ -391,7 +811,9 @@ type CacheEntryResponseDTO = {
|
|
|
391
811
|
expiresAt: string;
|
|
392
812
|
createdAt: string;
|
|
393
813
|
};
|
|
394
|
-
/**
|
|
814
|
+
/**
|
|
815
|
+
* 创建缓存条目请求 DTO
|
|
816
|
+
*/
|
|
395
817
|
type CreateCacheEntryRequestDTO = {
|
|
396
818
|
requestHash: string;
|
|
397
819
|
requestVector?: string | null;
|
|
@@ -402,19 +824,40 @@ type CreateCacheEntryRequestDTO = {
|
|
|
402
824
|
};
|
|
403
825
|
//#endregion
|
|
404
826
|
//#region src/cache/cache.port.d.ts
|
|
405
|
-
/**
|
|
827
|
+
/**
|
|
828
|
+
* 缓存端口接口
|
|
829
|
+
*/
|
|
406
830
|
type CachePort = {
|
|
407
|
-
/**
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
831
|
+
/**
|
|
832
|
+
* 根据请求哈希获取缓存条目
|
|
833
|
+
*/
|
|
834
|
+
getByHash(requestHash: string): Promise<Result<CacheEntryResponseDTO | null, DomainError>>;
|
|
835
|
+
/**
|
|
836
|
+
* 创建缓存条目
|
|
837
|
+
*/
|
|
838
|
+
create(input: CreateCacheEntryRequestDTO): Promise<Result<CacheEntryResponseDTO, DomainError>>;
|
|
839
|
+
/**
|
|
840
|
+
* 记录缓存命中
|
|
841
|
+
*/
|
|
842
|
+
recordHit(id: string): Promise<Result<CacheEntryResponseDTO, DomainError>>;
|
|
843
|
+
/**
|
|
844
|
+
* 删除缓存条目
|
|
845
|
+
*/
|
|
846
|
+
delete(id: string): Promise<Result<boolean, DomainError>>;
|
|
847
|
+
/**
|
|
848
|
+
* 清理过期缓存
|
|
849
|
+
*/
|
|
411
850
|
deleteExpired(): Promise<Result<number, DomainError>>;
|
|
412
851
|
};
|
|
413
|
-
/**
|
|
852
|
+
/**
|
|
853
|
+
* 缓存端口令牌
|
|
854
|
+
*/
|
|
414
855
|
declare const CACHE_PORT_TOKEN: unique symbol;
|
|
415
856
|
//#endregion
|
|
416
857
|
//#region src/cache/cache.schema.d.ts
|
|
417
|
-
/**
|
|
858
|
+
/**
|
|
859
|
+
* 创建缓存条目的校验 Schema
|
|
860
|
+
*/
|
|
418
861
|
declare const createCacheEntrySchema: z.ZodObject<{
|
|
419
862
|
requestHash: z.ZodString;
|
|
420
863
|
requestVector: z.ZodOptional<z.ZodString>;
|
|
@@ -423,11 +866,15 @@ declare const createCacheEntrySchema: z.ZodObject<{
|
|
|
423
866
|
similarityThreshold: z.ZodOptional<z.ZodNumber>;
|
|
424
867
|
ttlSeconds: z.ZodOptional<z.ZodNumber>;
|
|
425
868
|
}, z.core.$strip>;
|
|
426
|
-
/**
|
|
869
|
+
/**
|
|
870
|
+
* 创建缓存条目的输入类型
|
|
871
|
+
*/
|
|
427
872
|
type CreateCacheEntryInput = z.infer<typeof createCacheEntrySchema>;
|
|
428
873
|
//#endregion
|
|
429
874
|
//#region src/cache/cache.types.d.ts
|
|
430
|
-
/**
|
|
875
|
+
/**
|
|
876
|
+
* 缓存策略配置
|
|
877
|
+
*/
|
|
431
878
|
type CachePolicy = {
|
|
432
879
|
enabled: boolean;
|
|
433
880
|
ttlSeconds: number;
|
|
@@ -435,22 +882,47 @@ type CachePolicy = {
|
|
|
435
882
|
};
|
|
436
883
|
//#endregion
|
|
437
884
|
//#region src/cache/cache.error.d.ts
|
|
438
|
-
/**
|
|
885
|
+
/**
|
|
886
|
+
* 缓存领域错误
|
|
887
|
+
*/
|
|
439
888
|
declare class CacheError extends DomainError {
|
|
889
|
+
/**
|
|
890
|
+
* 构造缓存错误
|
|
891
|
+
*
|
|
892
|
+
* @param message - 错误消息
|
|
893
|
+
*/
|
|
440
894
|
constructor(message: string);
|
|
441
895
|
}
|
|
442
896
|
//#endregion
|
|
443
897
|
//#region src/cache/cache.event.d.ts
|
|
444
|
-
/**
|
|
898
|
+
/**
|
|
899
|
+
* 缓存命中事件
|
|
900
|
+
*/
|
|
445
901
|
declare class CacheHitEvent extends DomainEvent {
|
|
446
902
|
readonly cacheEntryId: string;
|
|
447
903
|
readonly modelId: string;
|
|
904
|
+
/**
|
|
905
|
+
* 事件类型标识
|
|
906
|
+
*/
|
|
448
907
|
static readonly TYPE = "cache:hit";
|
|
908
|
+
/**
|
|
909
|
+
* 实例事件类型
|
|
910
|
+
*/
|
|
449
911
|
readonly type = "cache:hit";
|
|
912
|
+
/**
|
|
913
|
+
* 构造缓存命中事件
|
|
914
|
+
*
|
|
915
|
+
* @param eventId - 事件唯一标识
|
|
916
|
+
* @param cacheEntryId - 缓存条目唯一标识
|
|
917
|
+
* @param modelId - 关联的模型标识
|
|
918
|
+
*/
|
|
450
919
|
constructor(eventId: string, cacheEntryId: string, modelId: string);
|
|
451
920
|
}
|
|
452
921
|
//#endregion
|
|
453
922
|
//#region src/cache/cache-entry.entity.d.ts
|
|
923
|
+
/**
|
|
924
|
+
* 缓存条目属性
|
|
925
|
+
*/
|
|
454
926
|
type CacheEntryProps = {
|
|
455
927
|
requestHash: string;
|
|
456
928
|
requestVector: string | null;
|
|
@@ -465,22 +937,88 @@ type CacheEntryProps = {
|
|
|
465
937
|
* 缓存条目实体
|
|
466
938
|
*/
|
|
467
939
|
declare class CacheEntryEntity extends Entity<CacheEntryProps> {
|
|
940
|
+
/**
|
|
941
|
+
* 获取请求哈希值
|
|
942
|
+
*
|
|
943
|
+
* @returns 请求内容的哈希值
|
|
944
|
+
*/
|
|
468
945
|
get requestHash(): string;
|
|
946
|
+
/**
|
|
947
|
+
* 获取请求向量
|
|
948
|
+
*
|
|
949
|
+
* @returns 请求的语义向量,无向量时为 null
|
|
950
|
+
*/
|
|
469
951
|
get requestVector(): string | null;
|
|
952
|
+
/**
|
|
953
|
+
* 获取响应数据
|
|
954
|
+
*
|
|
955
|
+
* @returns 缓存的响应数据
|
|
956
|
+
*/
|
|
470
957
|
get responseData(): string;
|
|
958
|
+
/**
|
|
959
|
+
* 获取模型标识
|
|
960
|
+
*
|
|
961
|
+
* @returns 关联的模型 ID
|
|
962
|
+
*/
|
|
471
963
|
get modelId(): string;
|
|
964
|
+
/**
|
|
965
|
+
* 获取相似度阈值
|
|
966
|
+
*
|
|
967
|
+
* @returns 语义匹配的相似度阈值
|
|
968
|
+
*/
|
|
472
969
|
get similarityThreshold(): number;
|
|
970
|
+
/**
|
|
971
|
+
* 获取命中次数
|
|
972
|
+
*
|
|
973
|
+
* @returns 缓存被命中的次数
|
|
974
|
+
*/
|
|
473
975
|
get hitCount(): number;
|
|
976
|
+
/**
|
|
977
|
+
* 获取过期时间
|
|
978
|
+
*
|
|
979
|
+
* @returns 缓存条目的过期时间(ISO 格式)
|
|
980
|
+
*/
|
|
474
981
|
get expiresAt(): string;
|
|
982
|
+
/**
|
|
983
|
+
* 获取创建时间
|
|
984
|
+
*
|
|
985
|
+
* @returns 缓存条目的创建时间(ISO 格式)
|
|
986
|
+
*/
|
|
475
987
|
get createdAt(): string;
|
|
988
|
+
/**
|
|
989
|
+
* 私有构造函数
|
|
990
|
+
*
|
|
991
|
+
* @param id - 缓存条目唯一标识
|
|
992
|
+
* @param props - 缓存条目属性
|
|
993
|
+
*/
|
|
476
994
|
private constructor();
|
|
995
|
+
/**
|
|
996
|
+
* 创建新的缓存条目实体
|
|
997
|
+
*
|
|
998
|
+
* @param id - 缓存条目唯一标识
|
|
999
|
+
* @param props - 缓存条目创建属性(不含自动生成的 hitCount 和 createdAt)
|
|
1000
|
+
* @returns 新创建的缓存条目实体
|
|
1001
|
+
*/
|
|
477
1002
|
static create(id: string, props: Omit<CacheEntryProps, 'hitCount' | 'createdAt'> & {
|
|
478
1003
|
ttlSeconds?: number;
|
|
479
1004
|
}): CacheEntryEntity;
|
|
1005
|
+
/**
|
|
1006
|
+
* 从持久化数据重建缓存条目实体
|
|
1007
|
+
*
|
|
1008
|
+
* @param id - 缓存条目唯一标识
|
|
1009
|
+
* @param props - 缓存条目完整属性
|
|
1010
|
+
* @returns 重建的缓存条目实体
|
|
1011
|
+
*/
|
|
480
1012
|
static reconstitute(id: string, props: CacheEntryProps): CacheEntryEntity;
|
|
481
|
-
/**
|
|
1013
|
+
/**
|
|
1014
|
+
* 记录缓存命中
|
|
1015
|
+
*/
|
|
482
1016
|
recordHit(): void;
|
|
483
|
-
/**
|
|
1017
|
+
/**
|
|
1018
|
+
* 检查是否过期
|
|
1019
|
+
*
|
|
1020
|
+
* @returns 是否已过期
|
|
1021
|
+
*/
|
|
484
1022
|
isExpired(): boolean;
|
|
485
1023
|
}
|
|
486
1024
|
//#endregion
|
|
@@ -495,150 +1033,310 @@ declare const CacheEntryMapper: ApplicationMapper<CacheEntryEntity, CacheEntryRe
|
|
|
495
1033
|
* 缓存条目仓储接口
|
|
496
1034
|
*/
|
|
497
1035
|
type CacheRepository = {
|
|
498
|
-
/**
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
1036
|
+
/**
|
|
1037
|
+
* 根据请求哈希获取缓存条目
|
|
1038
|
+
*/
|
|
1039
|
+
getByHash(requestHash: string): Promise<Result<CacheEntryEntity | null, DomainError>>;
|
|
1040
|
+
/**
|
|
1041
|
+
* 创建缓存条目
|
|
1042
|
+
*/
|
|
1043
|
+
create(entity: CacheEntryEntity): Promise<Result<CacheEntryEntity, DomainError>>;
|
|
1044
|
+
/**
|
|
1045
|
+
* 记录缓存命中
|
|
1046
|
+
*/
|
|
1047
|
+
recordHit(id: string): Promise<Result<CacheEntryEntity, DomainError>>;
|
|
1048
|
+
/**
|
|
1049
|
+
* 删除缓存条目
|
|
1050
|
+
*/
|
|
1051
|
+
delete(id: string): Promise<Result<boolean, DomainError>>;
|
|
1052
|
+
/**
|
|
1053
|
+
* 清理过期缓存
|
|
1054
|
+
*/
|
|
502
1055
|
deleteExpired(): Promise<Result<number, DomainError>>;
|
|
503
1056
|
};
|
|
504
1057
|
//#endregion
|
|
505
|
-
//#region src/
|
|
1058
|
+
//#region src/ports/model-repository.port.d.ts
|
|
506
1059
|
/**
|
|
507
|
-
*
|
|
1060
|
+
* 模型仓储端口
|
|
1061
|
+
*
|
|
1062
|
+
* @typeParam TModel - 模型实体类型,由实现方绑定
|
|
508
1063
|
*/
|
|
509
|
-
type
|
|
510
|
-
id: string
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
1064
|
+
type ModelRepositoryPort<TModel = unknown> = {
|
|
1065
|
+
getById(id: string): Promise<Result<TModel | null, DomainError>>;
|
|
1066
|
+
getByModelId(modelId: string): Promise<Result<TModel | null, DomainError>>;
|
|
1067
|
+
query(params: ModelQueryParams): Promise<Result<ModelQueryResult<TModel>, DomainError>>;
|
|
1068
|
+
save(entity: TModel): Promise<Result<TModel, DomainError>>;
|
|
1069
|
+
delete(id: string): Promise<Result<boolean, DomainError>>;
|
|
1070
|
+
};
|
|
1071
|
+
/**
|
|
1072
|
+
* 模型查询参数
|
|
1073
|
+
*/
|
|
1074
|
+
type ModelQueryParams = {
|
|
1075
|
+
/**
|
|
1076
|
+
* 模型状态过滤
|
|
1077
|
+
*/
|
|
1078
|
+
status?: string;
|
|
1079
|
+
/**
|
|
1080
|
+
* 供应商 ID 过滤
|
|
1081
|
+
*/
|
|
1082
|
+
providerId?: string;
|
|
1083
|
+
/**
|
|
1084
|
+
* 当前页码
|
|
1085
|
+
*/
|
|
1086
|
+
page?: number;
|
|
1087
|
+
/**
|
|
1088
|
+
* 每页条数
|
|
1089
|
+
*/
|
|
1090
|
+
pageSize?: number;
|
|
1091
|
+
/**
|
|
1092
|
+
* 排序字段
|
|
1093
|
+
*/
|
|
1094
|
+
sortBy?: string;
|
|
1095
|
+
/**
|
|
1096
|
+
* 排序方向
|
|
1097
|
+
*/
|
|
1098
|
+
sortOrder?: 'asc' | 'desc';
|
|
519
1099
|
};
|
|
520
|
-
//#endregion
|
|
521
|
-
//#region src/cache/cache.orm-mapper.d.ts
|
|
522
1100
|
/**
|
|
523
|
-
*
|
|
1101
|
+
* 模型查询结果
|
|
1102
|
+
*
|
|
1103
|
+
* @typeParam TModel - 模型实体类型
|
|
524
1104
|
*/
|
|
525
|
-
|
|
1105
|
+
type ModelQueryResult<TModel> = {
|
|
1106
|
+
/**
|
|
1107
|
+
* 当前页模型列表
|
|
1108
|
+
*/
|
|
1109
|
+
items: TModel[];
|
|
1110
|
+
/**
|
|
1111
|
+
* 总记录数
|
|
1112
|
+
*/
|
|
1113
|
+
total: number;
|
|
1114
|
+
/**
|
|
1115
|
+
* 当前页码
|
|
1116
|
+
*/
|
|
1117
|
+
page: number;
|
|
1118
|
+
/**
|
|
1119
|
+
* 每页条数
|
|
1120
|
+
*/
|
|
1121
|
+
pageSize: number;
|
|
1122
|
+
/**
|
|
1123
|
+
* 总页数
|
|
1124
|
+
*/
|
|
1125
|
+
totalPages: number;
|
|
1126
|
+
};
|
|
1127
|
+
/**
|
|
1128
|
+
* 模型仓储端口 DI 令牌
|
|
1129
|
+
*/
|
|
1130
|
+
declare const MODEL_REPOSITORY_PORT_TOKEN: unique symbol;
|
|
526
1131
|
//#endregion
|
|
527
|
-
//#region src/
|
|
1132
|
+
//#region src/ports/routing-rule-repository.port.d.ts
|
|
528
1133
|
/**
|
|
529
|
-
*
|
|
1134
|
+
* 路由规则仓储端口
|
|
1135
|
+
*
|
|
1136
|
+
* @typeParam TRoutingRule - 路由规则实体类型,由实现方绑定
|
|
530
1137
|
*/
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
getByHash(requestHash: string): Promise<Result<CacheEntryEntity | null, DomainError>>;
|
|
537
|
-
/** 创建缓存条目 */
|
|
538
|
-
create(entity: CacheEntryEntity): Promise<Result<CacheEntryEntity, DomainError>>;
|
|
539
|
-
/** 记录缓存命中 */
|
|
540
|
-
recordHit(id: string): Promise<Result<CacheEntryEntity, DomainError>>;
|
|
541
|
-
/** 删除缓存条目 */
|
|
1138
|
+
type RoutingRuleRepositoryPort<TRoutingRule = unknown> = {
|
|
1139
|
+
getById(id: string): Promise<Result<TRoutingRule | null, DomainError>>;
|
|
1140
|
+
getActiveRules(): Promise<Result<TRoutingRule[], DomainError>>;
|
|
1141
|
+
create(entity: TRoutingRule): Promise<Result<TRoutingRule, DomainError>>;
|
|
1142
|
+
update(entity: TRoutingRule): Promise<Result<TRoutingRule, DomainError>>;
|
|
542
1143
|
delete(id: string): Promise<Result<boolean, DomainError>>;
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
1144
|
+
};
|
|
1145
|
+
/**
|
|
1146
|
+
* 路由规则仓储端口 DI 令牌
|
|
1147
|
+
*/
|
|
1148
|
+
declare const ROUTING_RULE_REPOSITORY_PORT_TOKEN: unique symbol;
|
|
546
1149
|
//#endregion
|
|
547
|
-
//#region src/
|
|
1150
|
+
//#region src/ports/llm-complete.port.d.ts
|
|
548
1151
|
/**
|
|
549
|
-
*
|
|
1152
|
+
* LLM 调用消息
|
|
550
1153
|
*
|
|
551
|
-
*
|
|
552
|
-
* 若条目有效则记录命中并返回实体
|
|
1154
|
+
* 兼容 OpenAI Chat Completion 消息格式。
|
|
553
1155
|
*/
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
1156
|
+
type CompleteMessage = {
|
|
1157
|
+
/**
|
|
1158
|
+
* 消息角色
|
|
1159
|
+
*/
|
|
1160
|
+
role: 'system' | 'user' | 'assistant' | 'tool';
|
|
1161
|
+
/**
|
|
1162
|
+
* 消息内容
|
|
1163
|
+
*/
|
|
1164
|
+
content: string;
|
|
1165
|
+
/**
|
|
1166
|
+
* 工具调用 ID(role 为 tool 时使用)
|
|
1167
|
+
*/
|
|
1168
|
+
toolCallId?: string;
|
|
1169
|
+
};
|
|
561
1170
|
/**
|
|
562
|
-
*
|
|
1171
|
+
* 结构化输出 Schema 描述
|
|
563
1172
|
*
|
|
564
|
-
*
|
|
1173
|
+
* 调用方提供 JSON Schema 描述期望的输出结构,
|
|
1174
|
+
* model-gateway 透传至底层 provider 约束输出。
|
|
565
1175
|
*/
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
1176
|
+
type CompleteSchema = {
|
|
1177
|
+
/**
|
|
1178
|
+
* JSON Schema 名称
|
|
1179
|
+
*/
|
|
1180
|
+
name: string;
|
|
1181
|
+
/**
|
|
1182
|
+
* JSON Schema 描述
|
|
1183
|
+
*/
|
|
1184
|
+
description?: string;
|
|
1185
|
+
/**
|
|
1186
|
+
* JSON Schema 定义
|
|
1187
|
+
*/
|
|
1188
|
+
schema: Record<string, unknown>;
|
|
1189
|
+
/**
|
|
1190
|
+
* 是否严格模式
|
|
1191
|
+
*/
|
|
1192
|
+
strict?: boolean;
|
|
1193
|
+
};
|
|
573
1194
|
/**
|
|
574
|
-
*
|
|
1195
|
+
* Complete 调用选项
|
|
575
1196
|
*
|
|
576
|
-
*
|
|
1197
|
+
* 路由、采样、约束相关可选参数。
|
|
577
1198
|
*/
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
1199
|
+
type CompleteOptions = {
|
|
1200
|
+
/**
|
|
1201
|
+
* 指定供应商,未提供时由 routing rules 决策
|
|
1202
|
+
*/
|
|
1203
|
+
provider?: string;
|
|
1204
|
+
/**
|
|
1205
|
+
* 采样温度
|
|
1206
|
+
*/
|
|
1207
|
+
temperature?: number;
|
|
1208
|
+
/**
|
|
1209
|
+
* 最大生成 token 数
|
|
1210
|
+
*/
|
|
1211
|
+
maxTokens?: number;
|
|
1212
|
+
/**
|
|
1213
|
+
* 结构化输出 Schema
|
|
1214
|
+
*/
|
|
1215
|
+
schema?: CompleteSchema;
|
|
1216
|
+
/**
|
|
1217
|
+
* 调用方用户标识,用于成本归集与可观测性
|
|
1218
|
+
*/
|
|
1219
|
+
userId?: string;
|
|
1220
|
+
/**
|
|
1221
|
+
* 调用方元数据,透传至可观测性层
|
|
1222
|
+
*/
|
|
1223
|
+
metadata?: Record<string, unknown>;
|
|
1224
|
+
};
|
|
585
1225
|
/**
|
|
586
|
-
*
|
|
1226
|
+
* Complete 调用请求
|
|
587
1227
|
*
|
|
588
|
-
*
|
|
1228
|
+
* 与 spec 契约 complete(req: { provider?, model, messages, schema?, temperature?, maxTokens?, userId?,
|
|
1229
|
+
* metadata? }) 一致。
|
|
589
1230
|
*/
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
1231
|
+
type CompleteRequest = {
|
|
1232
|
+
/**
|
|
1233
|
+
* 指定供应商,未提供时由 routing rules 决策
|
|
1234
|
+
*/
|
|
1235
|
+
provider?: string;
|
|
1236
|
+
/**
|
|
1237
|
+
* 模型标识
|
|
1238
|
+
*/
|
|
1239
|
+
model: string;
|
|
1240
|
+
/**
|
|
1241
|
+
* 消息列表
|
|
1242
|
+
*/
|
|
1243
|
+
messages: CompleteMessage[];
|
|
1244
|
+
/**
|
|
1245
|
+
* 结构化输出 Schema
|
|
1246
|
+
*/
|
|
1247
|
+
schema?: CompleteSchema;
|
|
1248
|
+
/**
|
|
1249
|
+
* 采样温度
|
|
1250
|
+
*/
|
|
1251
|
+
temperature?: number;
|
|
1252
|
+
/**
|
|
1253
|
+
* 最大生成 token 数
|
|
1254
|
+
*/
|
|
1255
|
+
maxTokens?: number;
|
|
1256
|
+
/**
|
|
1257
|
+
* 调用方用户标识
|
|
1258
|
+
*/
|
|
1259
|
+
userId?: string;
|
|
1260
|
+
/**
|
|
1261
|
+
* 调用方元数据
|
|
1262
|
+
*/
|
|
1263
|
+
metadata?: Record<string, unknown>;
|
|
1264
|
+
};
|
|
1265
|
+
/**
|
|
1266
|
+
* 调用结束原因
|
|
1267
|
+
*/
|
|
1268
|
+
type FinishReason = 'stop' | 'length' | 'tool_calls' | 'content_filter' | 'error';
|
|
597
1269
|
/**
|
|
598
|
-
*
|
|
1270
|
+
* Complete 调用用量统计
|
|
599
1271
|
*
|
|
600
|
-
*
|
|
1272
|
+
* 用于成本归集与可观测性。
|
|
601
1273
|
*/
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
1274
|
+
type CompleteUsage = {
|
|
1275
|
+
/**
|
|
1276
|
+
* 输入 token 数
|
|
1277
|
+
*/
|
|
1278
|
+
inputTokens: number;
|
|
1279
|
+
/**
|
|
1280
|
+
* 输出 token 数
|
|
1281
|
+
*/
|
|
1282
|
+
outputTokens: number;
|
|
1283
|
+
/**
|
|
1284
|
+
* 本次调用成本(美元)
|
|
1285
|
+
*/
|
|
1286
|
+
cost: number;
|
|
1287
|
+
};
|
|
609
1288
|
/**
|
|
610
|
-
*
|
|
1289
|
+
* Complete 调用响应
|
|
611
1290
|
*
|
|
612
|
-
*
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
1291
|
+
* 与 spec 契约 Promise<{ content, usage: { inputTokens, outputTokens, cost }, finishReason,
|
|
1292
|
+
* providerUsed, modelUsed }> 一致。
|
|
1293
|
+
*/
|
|
1294
|
+
type CompleteResponse = {
|
|
1295
|
+
/**
|
|
1296
|
+
* 模型生成内容
|
|
1297
|
+
*/
|
|
1298
|
+
content: string;
|
|
1299
|
+
/**
|
|
1300
|
+
* 用量统计
|
|
1301
|
+
*/
|
|
1302
|
+
usage: CompleteUsage;
|
|
1303
|
+
/**
|
|
1304
|
+
* 调用结束原因
|
|
1305
|
+
*/
|
|
1306
|
+
finishReason: FinishReason;
|
|
1307
|
+
/**
|
|
1308
|
+
* 实际使用的供应商标识
|
|
1309
|
+
*/
|
|
1310
|
+
providerUsed: string;
|
|
1311
|
+
/**
|
|
1312
|
+
* 实际使用的模型标识
|
|
1313
|
+
*/
|
|
1314
|
+
modelUsed: string;
|
|
1315
|
+
};
|
|
632
1316
|
/**
|
|
633
|
-
*
|
|
1317
|
+
* LLM Complete 端口
|
|
634
1318
|
*
|
|
635
|
-
*
|
|
1319
|
+
* model-gateway 的统一 LLM 调用入口契约。
|
|
1320
|
+
* 调用方通过此端口提交 complete 请求,由实现层完成路由决策、成本归集与可观测性采集。
|
|
636
1321
|
*
|
|
637
|
-
* @
|
|
638
|
-
*
|
|
639
|
-
*
|
|
1322
|
+
* @example
|
|
1323
|
+
* ```ts
|
|
1324
|
+
* const result = await llmCompletePort.complete('Summarize this article', {
|
|
1325
|
+
* model: 'gpt-4',
|
|
1326
|
+
* temperature: 0.3,
|
|
1327
|
+
* maxTokens: 500,
|
|
1328
|
+
* userId: 'user-123',
|
|
1329
|
+
* });
|
|
1330
|
+
* ```;
|
|
1331
|
+
*/
|
|
1332
|
+
type LlmCompletePort = {
|
|
1333
|
+
complete(prompt: string, options?: CompleteOptions): Promise<Result<string, DomainError>>;
|
|
1334
|
+
completeRequest(request: CompleteRequest): Promise<Result<CompleteResponse, DomainError>>;
|
|
1335
|
+
};
|
|
1336
|
+
/**
|
|
1337
|
+
* LLM Complete 端口 DI 令牌
|
|
640
1338
|
*/
|
|
641
|
-
declare
|
|
1339
|
+
declare const LLM_COMPLETE_PORT_TOKEN: unique symbol;
|
|
642
1340
|
//#endregion
|
|
643
1341
|
//#region src/ports/event-publisher.port.d.ts
|
|
644
1342
|
/**
|
|
@@ -682,4 +1380,4 @@ type EventPublisherPort = {
|
|
|
682
1380
|
*/
|
|
683
1381
|
declare const EVENT_PUBLISHER_PORT_TOKEN: unique symbol;
|
|
684
1382
|
//#endregion
|
|
685
|
-
export { CACHE_PORT_TOKEN, CacheEntryEntity, CacheEntryMapper,
|
|
1383
|
+
export { CACHE_PORT_TOKEN, CacheEntryEntity, CacheEntryMapper, type CacheEntryResponseDTO, CacheError, CacheHitEvent, type CachePolicy, type CachePort, type CacheRepository, type ChatChoice, type ChatChunkDelta, type ChatCompletionChunk, type ChatCompletionRequest, type ChatCompletionResponse, type ChatMessage, type ChatStreamChoice, type CompleteOptions, type CompleteRequest, type CompleteResponse, type CompleteUsage, type CompletionChoice, type CompletionRequest, type CompletionResponse, type CreateCacheEntryInput, type CreateCacheEntryRequestDTO, DEFAULT_EXECUTION_STRATEGY, DEFAULT_FALLBACK_STRATEGY, DEFAULT_RETRY_STRATEGY, DEFAULT_TIMEOUT_STRATEGY, ERROR_STATUS_MAP, EVENT_PUBLISHER_PORT_TOKEN, type EmbeddingData, type EmbeddingRequest, type EmbeddingResponse, type ErrorCategory, ErrorCode, type EventPublisherPort, type EventSubscriber, type ExecutionStrategy, type FallbackAction, type FallbackStrategy, type FinishReason, type FunctionDefinition, type GatewayError, type GatewayErrorDetail, type GatewayExtension, LLM_COMPLETE_PORT_TOKEN, type LlmCompletePort, MODEL_REPOSITORY_PORT_TOKEN, type MessageContentPart, type MessageContentType, type MessageRole, type ModelInfo, type ModelListResponse, type ModelPricing, type ModelRepositoryPort, ROUTING_RULE_REPOSITORY_PORT_TOKEN, type ResponseFormat, type ResponseMessage, type RetryStrategy, type RoutingExtension, type RoutingRuleRepositoryPort, type TimeoutStrategy, type TokenUsage, type ToolCall, type ToolCallFunction, type ToolDefinition, budgetUsagePercent, calculateCost, createCacheEntrySchema, decrypt, encrypt, estimateMessagesTokens, estimateTokens, formatCost };
|