@h-ai/ai 0.1.0-alpha5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +1886 -0
- package/dist/ai-reasoning-types-ZRy23rSK.d.ts +2790 -0
- package/dist/ai-types-7iSHMnGM.d.ts +1095 -0
- package/dist/api/index.d.ts +628 -0
- package/dist/api/index.js +243 -0
- package/dist/api/index.js.map +1 -0
- package/dist/browser.d.ts +9 -0
- package/dist/browser.js +4 -0
- package/dist/browser.js.map +1 -0
- package/dist/chunk-ABP7RKIP.js +311 -0
- package/dist/chunk-ABP7RKIP.js.map +1 -0
- package/dist/chunk-ZOTQ75VE.js +462 -0
- package/dist/chunk-ZOTQ75VE.js.map +1 -0
- package/dist/client/index.d.ts +266 -0
- package/dist/client/index.js +3 -0
- package/dist/client/index.js.map +1 -0
- package/dist/index.d.ts +40 -0
- package/dist/index.js +5074 -0
- package/dist/index.js.map +1 -0
- package/package.json +63 -0
|
@@ -0,0 +1,2790 @@
|
|
|
1
|
+
import { AgentExecutor } from '@a2a-js/sdk/server';
|
|
2
|
+
import { HaiResult, HaiError } from '@h-ai/core';
|
|
3
|
+
import { CleanOptionsInput, ChunkOptionsInput } from '@h-ai/datapipe';
|
|
4
|
+
import { ZodType, z } from 'zod';
|
|
5
|
+
import OpenAI from 'openai';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @h-ai/ai — Store 存储抽象类型
|
|
9
|
+
*
|
|
10
|
+
* 定义统一的键值式 CRUD + 查询接口,所有需要状态持久化的子系统通过此接口存取数据。
|
|
11
|
+
* @module ai-store-types
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* 存储作用域——用于索引列加速查询
|
|
15
|
+
*
|
|
16
|
+
* 在 save 时传入 scope,值将写入独立索引列(object_id / session_id),
|
|
17
|
+
* 后续 query / removeBy 可通过 StoreFilter.objectId / sessionId 使用索引过滤。
|
|
18
|
+
*/
|
|
19
|
+
interface StoreScope {
|
|
20
|
+
/** 交互主体 ID(写入 object_id 索引列) */
|
|
21
|
+
objectId?: string;
|
|
22
|
+
/** 会话 ID(写入 session_id 索引列) */
|
|
23
|
+
sessionId?: string;
|
|
24
|
+
/** 状态(写入 status 索引列) */
|
|
25
|
+
status?: string;
|
|
26
|
+
/** 引用 ID(写入 ref_id 索引列,关联外部实体) */
|
|
27
|
+
refId?: string;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* where 条件中单个字段的值——纯值表示等值匹配,对象表示操作符
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* // 等值:type === 'fact'
|
|
35
|
+
* { type: 'fact' }
|
|
36
|
+
*
|
|
37
|
+
* // IN:type ∈ ['fact', 'preference']
|
|
38
|
+
* { type: { $in: ['fact', 'preference'] } }
|
|
39
|
+
*
|
|
40
|
+
* // 范围:importance >= 0.5
|
|
41
|
+
* { importance: { $gte: 0.5 } }
|
|
42
|
+
*
|
|
43
|
+
* // 组合:objectId === 'user-1' AND type ∈ ['fact'] AND importance >= 0.3
|
|
44
|
+
* { objectId: 'user-1', type: { $in: ['fact'] }, importance: { $gte: 0.3 } }
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
interface WhereOperator<V> {
|
|
48
|
+
/** 值在给定列表中(IN 语义) */
|
|
49
|
+
$in?: V[];
|
|
50
|
+
/** 大于等于 */
|
|
51
|
+
$gte?: V;
|
|
52
|
+
/** 大于 */
|
|
53
|
+
$gt?: V;
|
|
54
|
+
/** 小于等于 */
|
|
55
|
+
$lte?: V;
|
|
56
|
+
/** 小于 */
|
|
57
|
+
$lt?: V;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* 单字段条件:纯值 = 等值匹配,WhereOperator 对象 = 操作符匹配
|
|
61
|
+
*/
|
|
62
|
+
type WhereValue<V> = V | WhereOperator<V>;
|
|
63
|
+
/**
|
|
64
|
+
* where 子句类型——每个字段可以是等值匹配或操作符对象
|
|
65
|
+
*/
|
|
66
|
+
type WhereClause<T> = {
|
|
67
|
+
[K in keyof T]?: WhereValue<T[K]>;
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* 存储查询过滤条件
|
|
71
|
+
*
|
|
72
|
+
* @typeParam T - 记录类型
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```ts
|
|
76
|
+
* // 等值匹配(兼容旧写法)
|
|
77
|
+
* store.query({ where: { type: 'fact', objectId: 'user-1' } })
|
|
78
|
+
*
|
|
79
|
+
* // 操作符匹配
|
|
80
|
+
* store.query({ where: { type: { $in: ['fact', 'preference'] }, importance: { $gte: 0.5 } } })
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
interface StoreFilter<T> {
|
|
84
|
+
/** 字段匹配条件(等值或操作符) */
|
|
85
|
+
where?: WhereClause<T>;
|
|
86
|
+
/** 按 object_id 索引列过滤(需要 AIRelStoreOptions.hasObjectId 启用) */
|
|
87
|
+
objectId?: string;
|
|
88
|
+
/** 按 session_id 索引列过滤(需要 AIRelStoreOptions.hasSessionId 启用) */
|
|
89
|
+
sessionId?: string;
|
|
90
|
+
/** 按 status 索引列过滤(需要 AIRelStoreOptions.hasStatus 启用,支持单值或多值 IN 匹配) */
|
|
91
|
+
status?: string | string[];
|
|
92
|
+
/** 按 ref_id 索引列过滤(需要 AIRelStoreOptions.hasRefId 启用) */
|
|
93
|
+
refId?: string;
|
|
94
|
+
/** 排序 */
|
|
95
|
+
orderBy?: {
|
|
96
|
+
field: keyof T;
|
|
97
|
+
direction: 'asc' | 'desc';
|
|
98
|
+
};
|
|
99
|
+
/** 数量限制 */
|
|
100
|
+
limit?: number;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* 分页查询结果
|
|
104
|
+
*/
|
|
105
|
+
interface StorePage<T> {
|
|
106
|
+
/** 当前页数据 */
|
|
107
|
+
items: T[];
|
|
108
|
+
/** 总记录数 */
|
|
109
|
+
total: number;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* AIRelStore 配置选项
|
|
113
|
+
*
|
|
114
|
+
* 控制存储实例需要创建哪些索引列(object_id / session_id / status / ref_id)。
|
|
115
|
+
*/
|
|
116
|
+
interface AIRelStoreOptions {
|
|
117
|
+
/** 是否创建 object_id 索引列 */
|
|
118
|
+
hasObjectId?: boolean;
|
|
119
|
+
/** 是否创建 session_id 索引列 */
|
|
120
|
+
hasSessionId?: boolean;
|
|
121
|
+
/** 是否创建 status 索引列 */
|
|
122
|
+
hasStatus?: boolean;
|
|
123
|
+
/** 是否创建 ref_id 索引列 */
|
|
124
|
+
hasRefId?: boolean;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* AI 关系存储适配器
|
|
128
|
+
*
|
|
129
|
+
* 提供统一的 KV 式 CRUD + 查询能力,由 AIStoreProvider 创建具体实现。
|
|
130
|
+
*
|
|
131
|
+
* @typeParam T - 记录类型
|
|
132
|
+
*/
|
|
133
|
+
interface AIRelStore<T> {
|
|
134
|
+
/** 保存一条记录(upsert 语义) */
|
|
135
|
+
save: (id: string, data: T, scope?: StoreScope) => Promise<void>;
|
|
136
|
+
/** 批量保存 */
|
|
137
|
+
saveMany: (items: Array<{
|
|
138
|
+
id: string;
|
|
139
|
+
data: T;
|
|
140
|
+
scope?: StoreScope;
|
|
141
|
+
}>) => Promise<void>;
|
|
142
|
+
/** 按 ID 获取 */
|
|
143
|
+
get: (id: string) => Promise<T | undefined>;
|
|
144
|
+
/** 按条件查询 */
|
|
145
|
+
query: (filter: StoreFilter<T>) => Promise<T[]>;
|
|
146
|
+
/** 分页查询 */
|
|
147
|
+
queryPage: (filter: StoreFilter<T>, page: {
|
|
148
|
+
offset: number;
|
|
149
|
+
limit: number;
|
|
150
|
+
}) => Promise<StorePage<T>>;
|
|
151
|
+
/** 删除一条记录 */
|
|
152
|
+
remove: (id: string) => Promise<boolean>;
|
|
153
|
+
/** 按条件删除 */
|
|
154
|
+
removeBy: (filter: StoreFilter<T>) => Promise<number>;
|
|
155
|
+
/** 计数 */
|
|
156
|
+
count: (filter?: StoreFilter<T>) => Promise<number>;
|
|
157
|
+
/** 清空(可选按条件) */
|
|
158
|
+
clear: (filter?: StoreFilter<T>) => Promise<void>;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* AI 向量存储适配器
|
|
162
|
+
*
|
|
163
|
+
* 专用于需要向量检索的场景(如 Memory),由 AIStoreProvider 创建具体实现。
|
|
164
|
+
*/
|
|
165
|
+
interface AIVectorStore {
|
|
166
|
+
/** 存储向量 */
|
|
167
|
+
upsert: (id: string, vector: number[], metadata?: Record<string, unknown>) => Promise<void>;
|
|
168
|
+
/** 向量相似度检索 */
|
|
169
|
+
search: (vector: number[], options?: {
|
|
170
|
+
topK?: number;
|
|
171
|
+
minScore?: number;
|
|
172
|
+
filter?: Record<string, unknown>;
|
|
173
|
+
}) => Promise<Array<{
|
|
174
|
+
id: string;
|
|
175
|
+
score: number;
|
|
176
|
+
content?: string;
|
|
177
|
+
metadata?: Record<string, unknown>;
|
|
178
|
+
}>>;
|
|
179
|
+
/** 删除向量 */
|
|
180
|
+
remove: (id: string) => Promise<void>;
|
|
181
|
+
/** 清空 */
|
|
182
|
+
clear: (filter?: Record<string, unknown>) => Promise<void>;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* 交互主体引用
|
|
186
|
+
*
|
|
187
|
+
* 代表"和谁"交互 — 可以是人、AI Agent、或系统自身。
|
|
188
|
+
*/
|
|
189
|
+
interface ObjectRef {
|
|
190
|
+
/** 主体唯一 ID */
|
|
191
|
+
objectId: string;
|
|
192
|
+
/** 主体类型 */
|
|
193
|
+
objectType?: 'human' | 'agent' | 'system';
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* 完整的交互作用域 = Object + Session
|
|
197
|
+
*/
|
|
198
|
+
interface InteractionScope {
|
|
199
|
+
/** 交互主体 ID */
|
|
200
|
+
objectId: string;
|
|
201
|
+
/** 会话 ID */
|
|
202
|
+
sessionId: string;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* 会话信息
|
|
206
|
+
*/
|
|
207
|
+
interface SessionInfo {
|
|
208
|
+
/** 会话 ID */
|
|
209
|
+
sessionId: string;
|
|
210
|
+
/** 所属主体 ID */
|
|
211
|
+
objectId: string;
|
|
212
|
+
/** 会话标题 */
|
|
213
|
+
title?: string;
|
|
214
|
+
/** 创建时间(Unix 毫秒) */
|
|
215
|
+
createdAt: number;
|
|
216
|
+
/** 更新时间(Unix 毫秒) */
|
|
217
|
+
updatedAt: number;
|
|
218
|
+
/** 附加元数据 */
|
|
219
|
+
metadata?: Record<string, unknown>;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Knowledge 专用存储接口
|
|
223
|
+
*
|
|
224
|
+
* 封装知识库的实体索引、文档元数据和向量操作。
|
|
225
|
+
* 默认实现使用 reldb 归一化表 + vecdb 向量检索;
|
|
226
|
+
* SaaS 实现可对接远端知识库 API。
|
|
227
|
+
*/
|
|
228
|
+
interface KnowledgeStore {
|
|
229
|
+
/** 初始化存储(建表 / 建集合 / 建索引,幂等) */
|
|
230
|
+
initialize: (collection: string, dimension: number) => Promise<void>;
|
|
231
|
+
/** 插入或更新实体 */
|
|
232
|
+
upsertEntity: (entity: {
|
|
233
|
+
id: string;
|
|
234
|
+
name: string;
|
|
235
|
+
type: string;
|
|
236
|
+
aliases?: string[];
|
|
237
|
+
description?: string;
|
|
238
|
+
}) => Promise<void>;
|
|
239
|
+
/** 按名称模糊搜索实体(匹配 name 和 aliases) */
|
|
240
|
+
findEntitiesByName: (keyword: string) => Promise<Array<{
|
|
241
|
+
id: string;
|
|
242
|
+
name: string;
|
|
243
|
+
type: string;
|
|
244
|
+
aliases: string[];
|
|
245
|
+
}>>;
|
|
246
|
+
/** 列出实体(支持类型过滤和关键词搜索) */
|
|
247
|
+
listEntities: (options?: {
|
|
248
|
+
type?: string;
|
|
249
|
+
keyword?: string;
|
|
250
|
+
limit?: number;
|
|
251
|
+
}) => Promise<Array<{
|
|
252
|
+
id: string;
|
|
253
|
+
name: string;
|
|
254
|
+
type: string;
|
|
255
|
+
aliases: string[];
|
|
256
|
+
description: string | null;
|
|
257
|
+
createdAt: string | null;
|
|
258
|
+
updatedAt: string | null;
|
|
259
|
+
}>>;
|
|
260
|
+
/** 插入文档-实体关联 */
|
|
261
|
+
insertEntityDocument: (relation: {
|
|
262
|
+
entityId: string;
|
|
263
|
+
documentId: string;
|
|
264
|
+
chunkId?: string;
|
|
265
|
+
collection: string;
|
|
266
|
+
relevance?: number;
|
|
267
|
+
context?: string;
|
|
268
|
+
}) => Promise<void>;
|
|
269
|
+
/** 按实体 ID 列表查询关联文档 */
|
|
270
|
+
findDocumentsByEntityIds: (entityIds: string[], collection?: string) => Promise<Array<{
|
|
271
|
+
entityId: string;
|
|
272
|
+
documentId: string;
|
|
273
|
+
chunkId: string;
|
|
274
|
+
collection: string;
|
|
275
|
+
relevance: number;
|
|
276
|
+
context: string | null;
|
|
277
|
+
}>>;
|
|
278
|
+
/** 按实体名称查询实体及其关联文档 */
|
|
279
|
+
findByEntityName: (entityName: string, options?: {
|
|
280
|
+
collection?: string;
|
|
281
|
+
type?: string;
|
|
282
|
+
}) => Promise<Array<{
|
|
283
|
+
entity: {
|
|
284
|
+
id: string;
|
|
285
|
+
name: string;
|
|
286
|
+
type: string;
|
|
287
|
+
aliases: string[];
|
|
288
|
+
description: string | null;
|
|
289
|
+
};
|
|
290
|
+
documents: Array<{
|
|
291
|
+
documentId: string;
|
|
292
|
+
chunkId: string;
|
|
293
|
+
collection: string;
|
|
294
|
+
relevance: number;
|
|
295
|
+
context: string | null;
|
|
296
|
+
}>;
|
|
297
|
+
}>>;
|
|
298
|
+
/** 删除文档相关的实体关联 */
|
|
299
|
+
removeDocumentEntityRelations: (documentId: string, collection: string) => Promise<void>;
|
|
300
|
+
/** 保存文档元数据 */
|
|
301
|
+
upsertDocument: (doc: {
|
|
302
|
+
documentId: string;
|
|
303
|
+
collection: string;
|
|
304
|
+
title?: string;
|
|
305
|
+
url?: string;
|
|
306
|
+
chunkCount: number;
|
|
307
|
+
createdAt: number;
|
|
308
|
+
}) => Promise<void>;
|
|
309
|
+
/** 按 documentId + collection 获取单个文档元数据 */
|
|
310
|
+
getDocument: (documentId: string, collection: string) => Promise<{
|
|
311
|
+
documentId: string;
|
|
312
|
+
collection: string;
|
|
313
|
+
title: string | null;
|
|
314
|
+
url: string | null;
|
|
315
|
+
chunkCount: number;
|
|
316
|
+
createdAt: number;
|
|
317
|
+
} | undefined>;
|
|
318
|
+
/** 列出文档元数据 */
|
|
319
|
+
listDocuments: (collection: string, options?: {
|
|
320
|
+
offset?: number;
|
|
321
|
+
limit?: number;
|
|
322
|
+
}) => Promise<Array<{
|
|
323
|
+
documentId: string;
|
|
324
|
+
collection: string;
|
|
325
|
+
title: string | null;
|
|
326
|
+
url: string | null;
|
|
327
|
+
chunkCount: number;
|
|
328
|
+
createdAt: number;
|
|
329
|
+
}>>;
|
|
330
|
+
/** 查询每个文档的实体关联数 */
|
|
331
|
+
listDocumentEntityCounts: (documentIds: string[], collection: string) => Promise<Map<string, number>>;
|
|
332
|
+
/** 删除文档元数据 */
|
|
333
|
+
removeDocument: (documentId: string, collection: string) => Promise<void>;
|
|
334
|
+
/** 批量写入向量 */
|
|
335
|
+
upsertVectors: (collection: string, vectors: Array<{
|
|
336
|
+
id: string;
|
|
337
|
+
vector: number[];
|
|
338
|
+
content?: string;
|
|
339
|
+
metadata?: Record<string, unknown>;
|
|
340
|
+
}>) => Promise<void>;
|
|
341
|
+
/** 向量相似度检索 */
|
|
342
|
+
searchVectors: (collection: string, vector: number[], options?: {
|
|
343
|
+
topK?: number;
|
|
344
|
+
minScore?: number;
|
|
345
|
+
filter?: Record<string, unknown>;
|
|
346
|
+
}) => Promise<Array<{
|
|
347
|
+
id: string;
|
|
348
|
+
score: number;
|
|
349
|
+
content?: string;
|
|
350
|
+
metadata?: Record<string, unknown>;
|
|
351
|
+
}>>;
|
|
352
|
+
/** 批量删除向量 */
|
|
353
|
+
removeVectors: (collection: string, ids: string[]) => Promise<void>;
|
|
354
|
+
/** 确保向量集合存在 */
|
|
355
|
+
ensureCollection: (collection: string, dimension: number) => Promise<void>;
|
|
356
|
+
/** 注册 collection(setup 时持久化,幂等) */
|
|
357
|
+
registerCollection: (collection: string, dimension: number) => Promise<void>;
|
|
358
|
+
/** 检查 collection 是否已在注册表中存在(跨节点/重启后仍有效) */
|
|
359
|
+
collectionExists: (collection: string) => Promise<boolean>;
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* AI 存储 Provider 接口
|
|
363
|
+
*
|
|
364
|
+
* 负责创建 AIRelStore / AIVectorStore 实例,并管理存储层生命周期。
|
|
365
|
+
* 默认实现基于 reldb + vecdb;也可对接 SaaS API 或其他后端。
|
|
366
|
+
*
|
|
367
|
+
* @example
|
|
368
|
+
* ```ts
|
|
369
|
+
* // 使用默认 reldb+vecdb provider
|
|
370
|
+
* ai.init({ store: { type: 'db' } })
|
|
371
|
+
*
|
|
372
|
+
* // 使用自定义 provider
|
|
373
|
+
* ai.init({ store: { type: 'custom', provider: myProvider } })
|
|
374
|
+
* ```
|
|
375
|
+
*/
|
|
376
|
+
interface AIStoreProvider {
|
|
377
|
+
/** Provider 名称(如 'db'、'api') */
|
|
378
|
+
readonly name: string;
|
|
379
|
+
/** 创建关系数据存储实例 */
|
|
380
|
+
createRelStore: <T>(name: string, options?: AIRelStoreOptions) => AIRelStore<T>;
|
|
381
|
+
/** 创建向量数据存储实例 */
|
|
382
|
+
createVectorStore: (name: string) => AIVectorStore;
|
|
383
|
+
/**
|
|
384
|
+
* 创建 knowledge 专用存储(可选)
|
|
385
|
+
*
|
|
386
|
+
* 如果 Provider 不提供此方法,knowledge 子系统将不可用。
|
|
387
|
+
* 默认 db Provider 提供基于 reldb 归一化表 + vecdb 的高效实现。
|
|
388
|
+
*/
|
|
389
|
+
createKnowledgeStore?: () => KnowledgeStore;
|
|
390
|
+
/**
|
|
391
|
+
* 初始化所有已创建的存储(建表、建连接等)
|
|
392
|
+
*
|
|
393
|
+
* 由 ai.init() 在创建完所有 store 实例后统一调用。
|
|
394
|
+
*/
|
|
395
|
+
initialize: () => Promise<void>;
|
|
396
|
+
/** 关闭所有存储(释放连接等),由 ai.close() 调用 */
|
|
397
|
+
close?: () => Promise<void>;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* @h-ai/ai — A2A(Agent-to-Agent)子功能类型
|
|
402
|
+
*
|
|
403
|
+
* 定义 A2A 协议集成所需的类型、SDK 类型再导出、操作接口。
|
|
404
|
+
* @module ai-a2a-types
|
|
405
|
+
*/
|
|
406
|
+
|
|
407
|
+
/** A2A 调用方身份信息 */
|
|
408
|
+
interface A2ACallerIdentity {
|
|
409
|
+
/** 调用方 Agent ID(如 AgentCard.name 或自定义标识) */
|
|
410
|
+
agentId: string;
|
|
411
|
+
/** 调用方显示名 */
|
|
412
|
+
name?: string;
|
|
413
|
+
/** 调用方 URL(AgentCard 地址) */
|
|
414
|
+
url?: string;
|
|
415
|
+
}
|
|
416
|
+
/** A2A 消息记录(存储每条 A2A 交互) */
|
|
417
|
+
interface A2AMessageRecord {
|
|
418
|
+
/** 记录 ID(自动生成) */
|
|
419
|
+
id: string;
|
|
420
|
+
/** 关联的 A2A Task ID */
|
|
421
|
+
taskId: string;
|
|
422
|
+
/** 消息角色:user(入站请求)/ agent(出站响应) */
|
|
423
|
+
role: 'user' | 'agent';
|
|
424
|
+
/** 消息内容(JSON 序列化的 Part[] 数组) */
|
|
425
|
+
parts: unknown[];
|
|
426
|
+
/** 调用方身份信息(仅 role=user 时) */
|
|
427
|
+
caller?: A2ACallerIdentity;
|
|
428
|
+
/** 创建时间戳(毫秒) */
|
|
429
|
+
createdAt: number;
|
|
430
|
+
}
|
|
431
|
+
/** A2A 客户端调用记录(作为客户端调用远端 Agent 时的日志) */
|
|
432
|
+
interface A2AClientCallRecord {
|
|
433
|
+
/** 记录 ID */
|
|
434
|
+
id: string;
|
|
435
|
+
/** 远端 Agent URL */
|
|
436
|
+
remoteUrl: string;
|
|
437
|
+
/** 远端 Agent 名称 */
|
|
438
|
+
remoteName?: string;
|
|
439
|
+
/** 请求消息 Part[] */
|
|
440
|
+
requestParts: unknown[];
|
|
441
|
+
/** 响应消息 Part[](可选,流式时可能为空) */
|
|
442
|
+
responseParts?: unknown[];
|
|
443
|
+
/** A2A Task ID(远端返回的) */
|
|
444
|
+
taskId?: string;
|
|
445
|
+
/** 任务最终状态 */
|
|
446
|
+
taskState?: string;
|
|
447
|
+
/** 调用耗时(毫秒) */
|
|
448
|
+
duration?: number;
|
|
449
|
+
/** 创建时间戳 */
|
|
450
|
+
createdAt: number;
|
|
451
|
+
}
|
|
452
|
+
/** A2A 上下文信息(对话/会话级别) */
|
|
453
|
+
interface A2AContextInfo {
|
|
454
|
+
/** 上下文 ID(对应 SDK 的 contextId) */
|
|
455
|
+
id: string;
|
|
456
|
+
/** 关联的 Agent 标识 */
|
|
457
|
+
agentId?: string;
|
|
458
|
+
/** 上下文标题(可选) */
|
|
459
|
+
title?: string;
|
|
460
|
+
/** 创建时间戳 */
|
|
461
|
+
createdAt: number;
|
|
462
|
+
/** 最后更新时间戳 */
|
|
463
|
+
updatedAt: number;
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* A2A 认证器接口
|
|
467
|
+
*
|
|
468
|
+
* 纯接口设计,由应用层实现,Kit 模块做胶水集成。
|
|
469
|
+
* 用于验证入站 A2A 请求的身份。
|
|
470
|
+
*/
|
|
471
|
+
interface A2AAuthenticator {
|
|
472
|
+
/**
|
|
473
|
+
* 验证入站请求
|
|
474
|
+
*
|
|
475
|
+
* @param headers - HTTP 请求头
|
|
476
|
+
* @returns 成功返回调用方身份信息,失败返回错误
|
|
477
|
+
*/
|
|
478
|
+
authenticate: (headers: Record<string, string | undefined>) => Promise<HaiResult<A2ACallerIdentity>>;
|
|
479
|
+
}
|
|
480
|
+
/** A2A 任务查询过滤器 */
|
|
481
|
+
interface A2ATaskFilter {
|
|
482
|
+
/** 按任务状态过滤 */
|
|
483
|
+
status?: string | string[];
|
|
484
|
+
/** 按上下文 ID 过滤 */
|
|
485
|
+
contextId?: string;
|
|
486
|
+
/** 按调用方 Agent ID 过滤 */
|
|
487
|
+
callerId?: string;
|
|
488
|
+
/** 时间范围起始(毫秒) */
|
|
489
|
+
since?: number;
|
|
490
|
+
/** 最大返回数 */
|
|
491
|
+
limit?: number;
|
|
492
|
+
/** 偏移量 */
|
|
493
|
+
offset?: number;
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
496
|
+
* A2A 操作接口
|
|
497
|
+
*
|
|
498
|
+
* 提供 Agent-to-Agent 协议能力:Agent Card 管理、请求处理、客户端调用。
|
|
499
|
+
*/
|
|
500
|
+
interface A2AOperations {
|
|
501
|
+
/**
|
|
502
|
+
* 注册 Agent 执行器
|
|
503
|
+
*
|
|
504
|
+
* 需要先调用 `ai.init()` 并配置 `a2a` 后才能注册。
|
|
505
|
+
* 注册后 `handleRequest()`、`listMessages()` 等方法即可使用。
|
|
506
|
+
*
|
|
507
|
+
* @param executor - Agent 执行器(由应用层实现)
|
|
508
|
+
* @returns 注册成功返回 `ok(undefined)`;未配置/未初始化返回 `err(HaiAIError.*)`
|
|
509
|
+
*/
|
|
510
|
+
registerExecutor: (executor: AgentExecutor) => HaiResult<void>;
|
|
511
|
+
/**
|
|
512
|
+
* 获取当前 Agent Card
|
|
513
|
+
*
|
|
514
|
+
* 配置了 a2a 后即可使用,无需注册 executor。
|
|
515
|
+
*
|
|
516
|
+
* @returns Agent Card 配置
|
|
517
|
+
*/
|
|
518
|
+
getAgentCard: () => HaiResult<A2AAgentCardConfig>;
|
|
519
|
+
/**
|
|
520
|
+
* 处理入站 JSON-RPC 请求
|
|
521
|
+
*
|
|
522
|
+
* 将 HTTP 请求体路由到 SDK 的 JsonRpcTransportHandler。
|
|
523
|
+
*
|
|
524
|
+
* @param requestBody - JSON-RPC 请求体
|
|
525
|
+
* @param context - 可选的调用上下文(含认证信息)
|
|
526
|
+
* @returns JSON-RPC 响应(单条或流式)
|
|
527
|
+
*/
|
|
528
|
+
handleRequest: (requestBody: unknown, context?: Record<string, unknown>) => Promise<A2AHandleResult>;
|
|
529
|
+
/**
|
|
530
|
+
* 查询 A2A 任务列表
|
|
531
|
+
*
|
|
532
|
+
* @param filter - 查询过滤器
|
|
533
|
+
* @returns 分页的消息记录列表
|
|
534
|
+
*/
|
|
535
|
+
listMessages: (filter: A2ATaskFilter) => Promise<HaiResult<StorePage<A2AMessageRecord>>>;
|
|
536
|
+
/**
|
|
537
|
+
* 作为客户端调用远端 Agent
|
|
538
|
+
*
|
|
539
|
+
* @param remoteUrl - 远端 Agent 的 A2A 端点 URL
|
|
540
|
+
* @param message - 发送的消息文本
|
|
541
|
+
* @param options - 调用选项
|
|
542
|
+
* @returns 远端响应
|
|
543
|
+
*/
|
|
544
|
+
callRemoteAgent: (remoteUrl: string, message: string, options?: A2ACallOptions) => Promise<HaiResult<A2ACallResult>>;
|
|
545
|
+
}
|
|
546
|
+
/** A2A API Key 安全配置 */
|
|
547
|
+
interface A2AApiKeySecurity {
|
|
548
|
+
/** API Key 的传递位置 */
|
|
549
|
+
in: 'header' | 'query';
|
|
550
|
+
/** 参数名 */
|
|
551
|
+
name: string;
|
|
552
|
+
}
|
|
553
|
+
/** A2A 安全认证配置 */
|
|
554
|
+
interface A2ASecurityConfig {
|
|
555
|
+
/** API Key 认证配置 */
|
|
556
|
+
apiKey?: A2AApiKeySecurity;
|
|
557
|
+
}
|
|
558
|
+
/** Agent Card 配置(应用层提供) */
|
|
559
|
+
interface A2AAgentCardConfig {
|
|
560
|
+
/** Agent 名称 */
|
|
561
|
+
name: string;
|
|
562
|
+
/** Agent 描述 */
|
|
563
|
+
description?: string;
|
|
564
|
+
/** Agent URL(对外可访问的 base URL) */
|
|
565
|
+
url: string;
|
|
566
|
+
/** Agent 版本 */
|
|
567
|
+
version?: string;
|
|
568
|
+
/** Agent 能力声明 */
|
|
569
|
+
skills?: Array<{
|
|
570
|
+
id: string;
|
|
571
|
+
name: string;
|
|
572
|
+
description?: string;
|
|
573
|
+
tags?: string[];
|
|
574
|
+
}>;
|
|
575
|
+
/** 安全认证配置(体现在 Agent Card 的 securitySchemes / security 字段) */
|
|
576
|
+
security?: A2ASecurityConfig;
|
|
577
|
+
}
|
|
578
|
+
/** 处理结果(单条或流式) */
|
|
579
|
+
interface A2AHandleResult {
|
|
580
|
+
/** 是否为流式响应 */
|
|
581
|
+
streaming: boolean;
|
|
582
|
+
/** 单条 JSON-RPC 响应体(非流式时) */
|
|
583
|
+
body?: unknown;
|
|
584
|
+
/** 流式 JSON-RPC 响应迭代器(流式时) */
|
|
585
|
+
stream?: AsyncGenerator<unknown, void, undefined>;
|
|
586
|
+
}
|
|
587
|
+
/** 远端调用选项 */
|
|
588
|
+
interface A2ACallOptions {
|
|
589
|
+
/** 请求超时(毫秒) */
|
|
590
|
+
timeout?: number;
|
|
591
|
+
/** 额外请求头 */
|
|
592
|
+
headers?: Record<string, string>;
|
|
593
|
+
}
|
|
594
|
+
/** 远端调用结果 */
|
|
595
|
+
interface A2ACallResult {
|
|
596
|
+
/** 远端任务 ID */
|
|
597
|
+
taskId?: string;
|
|
598
|
+
/** 任务最终状态 */
|
|
599
|
+
taskState?: string;
|
|
600
|
+
/** 响应消息文本 */
|
|
601
|
+
responseText?: string;
|
|
602
|
+
/** 响应消息 Part 数组 */
|
|
603
|
+
responseParts?: unknown[];
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
/**
|
|
607
|
+
* @h-ai/ai — LLM 子功能类型
|
|
608
|
+
*
|
|
609
|
+
* 定义 LLM 消息、请求、响应、流、工具等公共类型。
|
|
610
|
+
* @module ai-llm-types
|
|
611
|
+
*/
|
|
612
|
+
|
|
613
|
+
/** 消息角色枚举:`'system'` | `'user'` | `'assistant'` | `'tool'` */
|
|
614
|
+
type MessageRole = 'system' | 'user' | 'assistant' | 'tool';
|
|
615
|
+
/** 文本内容块(多模态消息中的纯文本部分) */
|
|
616
|
+
type TextContent = OpenAI.Chat.Completions.ChatCompletionContentPartText;
|
|
617
|
+
/** 图片内容块(多模态消息中的图片部分) */
|
|
618
|
+
type ImageContent = OpenAI.Chat.Completions.ChatCompletionContentPartImage;
|
|
619
|
+
/** 消息内容(纯文本字符串,或由内容块组成的多模态数组) */
|
|
620
|
+
type MessageContent = string | OpenAI.Chat.Completions.ChatCompletionContentPart[];
|
|
621
|
+
/** 系统消息,用于设定对话的行为规则 */
|
|
622
|
+
type SystemMessage = OpenAI.Chat.Completions.ChatCompletionSystemMessageParam;
|
|
623
|
+
/** 用户消息 */
|
|
624
|
+
type UserMessage = OpenAI.Chat.Completions.ChatCompletionUserMessageParam;
|
|
625
|
+
/** 工具调用描述,由助手消息中的 `tool_calls` 字段携带 */
|
|
626
|
+
type ToolCall = OpenAI.Chat.Completions.ChatCompletionMessageToolCall;
|
|
627
|
+
/** 助手消息(模型生成的回复,或传入对话上下文的助手轮次) */
|
|
628
|
+
type AssistantMessage = OpenAI.Chat.Completions.ChatCompletionAssistantMessageParam;
|
|
629
|
+
/** 工具消息(工具执行结果,用于回传给模型) */
|
|
630
|
+
type ToolMessage = OpenAI.Chat.Completions.ChatCompletionToolMessageParam;
|
|
631
|
+
/** 聊天消息联合类型,涵盖对话中所有角色的消息 */
|
|
632
|
+
type ChatMessage = OpenAI.Chat.Completions.ChatCompletionMessageParam;
|
|
633
|
+
/** OpenAI function calling 工具定义格式 */
|
|
634
|
+
type ToolDefinition = OpenAI.Chat.Completions.ChatCompletionTool;
|
|
635
|
+
/**
|
|
636
|
+
* 聊天完成请求参数
|
|
637
|
+
*
|
|
638
|
+
* 继承 OpenAI SDK 全部标准请求字段,并扩展框架专属字段(`objectId`、`sessionId`)。
|
|
639
|
+
* `model` 改为可选(未指定时使用配置中的默认模型)。
|
|
640
|
+
* `stream` 字段由框架内部控制,不对外暴露。
|
|
641
|
+
*/
|
|
642
|
+
type ChatCompletionRequest = Omit<OpenAI.Chat.ChatCompletionCreateParamsNonStreaming, 'model' | 'stream'> & {
|
|
643
|
+
/** 模型名称(可选,未指定时使用配置中的默认模型) */
|
|
644
|
+
model?: string;
|
|
645
|
+
/** 交互主体 ID(传入后 LLM 会自动关联到该主体) */
|
|
646
|
+
objectId?: string;
|
|
647
|
+
/** 会话 ID(传入后 LLM 会自动关联到该会话) */
|
|
648
|
+
sessionId?: string;
|
|
649
|
+
/** 是否持久化对话记录(默认 true;传入 false 时跳过记录,适用于内部调用如实体提取) */
|
|
650
|
+
enablePersist?: boolean;
|
|
651
|
+
};
|
|
652
|
+
/** Token 使用统计 */
|
|
653
|
+
type TokenUsage = OpenAI.CompletionUsage;
|
|
654
|
+
/** 聊天完成响应中的单个选择 */
|
|
655
|
+
type ChatCompletionChoice = OpenAI.Chat.ChatCompletion.Choice;
|
|
656
|
+
/** 聊天完成响应(非流式) */
|
|
657
|
+
type ChatCompletionResponse = OpenAI.Chat.ChatCompletion;
|
|
658
|
+
/** 流式增量内容(每个 chunk 中的变化部分) */
|
|
659
|
+
type ChatCompletionDelta = OpenAI.Chat.ChatCompletionChunk.Choice.Delta;
|
|
660
|
+
/** 流式响应块(SSE 传输的单个数据帧) */
|
|
661
|
+
type ChatCompletionChunk = OpenAI.Chat.ChatCompletionChunk;
|
|
662
|
+
/** 流处理结果(完整消费流后的累积数据) */
|
|
663
|
+
interface StreamResult {
|
|
664
|
+
/** 累积的完整文本内容 */
|
|
665
|
+
content: string;
|
|
666
|
+
/** 累积的完整工具调用列表 */
|
|
667
|
+
toolCalls: ToolCall[];
|
|
668
|
+
/** 完成原因(流未结束时为 `null`) */
|
|
669
|
+
finishReason: string | null;
|
|
670
|
+
}
|
|
671
|
+
/**
|
|
672
|
+
* 流处理器接口
|
|
673
|
+
*
|
|
674
|
+
* 逐 chunk 喂入,内部累积文本和工具调用,支持 reset 复用。
|
|
675
|
+
*/
|
|
676
|
+
interface StreamProcessor {
|
|
677
|
+
/** 处理单个 chunk,返回增量 delta;空 choices 时返回 `null` */
|
|
678
|
+
process: (chunk: ChatCompletionChunk) => ChatCompletionDelta | null;
|
|
679
|
+
/** 获取当前累积结果(不重置状态) */
|
|
680
|
+
getResult: () => StreamResult;
|
|
681
|
+
/** 将累积结果转换为 AssistantMessage(有 tool_calls 时 content 为 `null`) */
|
|
682
|
+
toAssistantMessage: () => AssistantMessage;
|
|
683
|
+
/** 重置内部状态,可重新处理新一轮流 */
|
|
684
|
+
reset: () => void;
|
|
685
|
+
}
|
|
686
|
+
/** SSE(Server-Sent Events)事件结构 */
|
|
687
|
+
interface SSEEvent {
|
|
688
|
+
/** 事件类型(`event:` 字段) */
|
|
689
|
+
event?: string;
|
|
690
|
+
/** 事件 ID(`id:` 字段) */
|
|
691
|
+
id?: string;
|
|
692
|
+
/** 重连间隔(毫秒,`retry:` 字段) */
|
|
693
|
+
retry?: number;
|
|
694
|
+
/** 数据载荷(`data:` 字段,多行数据以 `\n` 合并) */
|
|
695
|
+
data?: string;
|
|
696
|
+
}
|
|
697
|
+
/**
|
|
698
|
+
* SSE 解码器接口
|
|
699
|
+
*
|
|
700
|
+
* 内部维护缓冲区,支持跨 chunk 的不完整数据拼接。
|
|
701
|
+
*/
|
|
702
|
+
interface SSEDecoder {
|
|
703
|
+
/** 追加文本并解码出完整事件;未完成的部分留在缓冲区 */
|
|
704
|
+
decode: (text: string) => Iterable<SSEEvent>;
|
|
705
|
+
/** 清空缓冲区 */
|
|
706
|
+
reset: () => void;
|
|
707
|
+
}
|
|
708
|
+
/** 流处理操作接口(通过 `ai.stream` 访问,纯函数,无需初始化) */
|
|
709
|
+
interface StreamOperations {
|
|
710
|
+
/** 创建新的流处理器实例 */
|
|
711
|
+
createProcessor: () => StreamProcessor;
|
|
712
|
+
/** 完整消费流并返回累积结果 */
|
|
713
|
+
collect: (stream: AsyncIterable<ChatCompletionChunk>) => Promise<StreamResult>;
|
|
714
|
+
/** 创建新的 SSE 解码器实例 */
|
|
715
|
+
createSSEDecoder: () => SSEDecoder;
|
|
716
|
+
/** 将 SSE 事件编码为符合规范的文本(以 `\n\n` 结尾) */
|
|
717
|
+
encodeSSE: (event: SSEEvent) => string;
|
|
718
|
+
}
|
|
719
|
+
/** 工具错误类型枚举 */
|
|
720
|
+
type ToolErrorType = 'TOOL_NOT_FOUND' | 'VALIDATION_FAILED' | 'EXECUTION_FAILED' | 'TIMEOUT';
|
|
721
|
+
/**
|
|
722
|
+
* 工具定义选项(传给 `ai.tools.define()`)
|
|
723
|
+
*
|
|
724
|
+
* @typeParam TInput - 参数类型(由 Zod schema 推断)
|
|
725
|
+
* @typeParam TOutput - 返回值类型
|
|
726
|
+
*/
|
|
727
|
+
interface DefineToolOptions<TInput, TOutput> {
|
|
728
|
+
/** 工具名称(需唯一,用于 function calling name 字段) */
|
|
729
|
+
name: string;
|
|
730
|
+
/** 工具功能描述(供模型理解何时调用) */
|
|
731
|
+
description: string;
|
|
732
|
+
/** Zod schema,用于参数校验和 JSON Schema 转换 */
|
|
733
|
+
parameters: ZodType<TInput>;
|
|
734
|
+
/** 执行函数,接收校验后的参数,支持同步/异步 */
|
|
735
|
+
handler: (input: TInput) => Promise<TOutput> | TOutput;
|
|
736
|
+
}
|
|
737
|
+
/**
|
|
738
|
+
* 工具实例(由 `ai.tools.define()` 创建)
|
|
739
|
+
*
|
|
740
|
+
* @typeParam TInput - 参数类型
|
|
741
|
+
* @typeParam TOutput - 返回值类型
|
|
742
|
+
*/
|
|
743
|
+
interface Tool<TInput = unknown, TOutput = unknown> {
|
|
744
|
+
/** 工具名称 */
|
|
745
|
+
name: string;
|
|
746
|
+
/** 工具功能描述 */
|
|
747
|
+
description: string;
|
|
748
|
+
/** Zod 参数 schema */
|
|
749
|
+
parameters: ZodType<TInput>;
|
|
750
|
+
/** 执行工具(自动校验参数),失败返回 ToolError */
|
|
751
|
+
execute: (input: TInput) => Promise<HaiResult<TOutput>>;
|
|
752
|
+
/** 转换为 OpenAI function calling 定义格式($schema 字段已移除) */
|
|
753
|
+
toDefinition: () => ToolDefinition;
|
|
754
|
+
}
|
|
755
|
+
/**
|
|
756
|
+
* 工具注册表接口(由 `ai.tools.createRegistry()` 创建)
|
|
757
|
+
*
|
|
758
|
+
* 管理一组工具的注册、查询与批量执行,支持链式调用。
|
|
759
|
+
*/
|
|
760
|
+
interface ToolRegistryOperations {
|
|
761
|
+
/** 注册工具(同名覆盖),返回 registry 自身以支持链式调用 */
|
|
762
|
+
register: <TInput, TOutput>(tool: Tool<TInput, TOutput>) => ToolRegistryOperations;
|
|
763
|
+
/** 批量注册工具,返回 registry 自身 */
|
|
764
|
+
registerMany: (tools: Tool<unknown, unknown>[]) => ToolRegistryOperations;
|
|
765
|
+
/** 注销指定名称的工具,成功返回 `true`,不存在返回 `false` */
|
|
766
|
+
unregister: (name: string) => boolean;
|
|
767
|
+
/** 按名称获取工具实例,不存在返回 `undefined` */
|
|
768
|
+
get: (name: string) => Tool | undefined;
|
|
769
|
+
/** 判断指定名称的工具是否已注册 */
|
|
770
|
+
has: (name: string) => boolean;
|
|
771
|
+
/** 获取所有已注册的工具名称列表 */
|
|
772
|
+
getNames: () => string[];
|
|
773
|
+
/** 获取所有工具的 OpenAI function calling 定义(用于传入 ChatCompletionRequest.tools) */
|
|
774
|
+
getDefinitions: () => ToolDefinition[];
|
|
775
|
+
/** 执行单个工具调用,自动解析 JSON 参数并校验;失败返回 ToolError */
|
|
776
|
+
execute: (toolCall: ToolCall) => Promise<HaiResult<ToolMessage>>;
|
|
777
|
+
/** 批量执行工具调用(默认并行),任一失败立即返回错误 */
|
|
778
|
+
executeAll: (toolCalls: ToolCall[], options?: {
|
|
779
|
+
parallel?: boolean;
|
|
780
|
+
}) => Promise<HaiResult<ToolMessage[]>>;
|
|
781
|
+
/** 清空所有已注册的工具 */
|
|
782
|
+
clear: () => void;
|
|
783
|
+
/** 当前已注册的工具数量 */
|
|
784
|
+
readonly size: number;
|
|
785
|
+
}
|
|
786
|
+
/** 工具操作接口(通过 `ai.tools` 访问,纯函数,无需初始化) */
|
|
787
|
+
interface ToolsOperations {
|
|
788
|
+
/** 定义工具(Zod schema 类型推断 + 自动参数校验) */
|
|
789
|
+
define: <TInput, TOutput>(options: DefineToolOptions<TInput, TOutput>) => Tool<TInput, TOutput>;
|
|
790
|
+
/** 创建新的工具注册表实例 */
|
|
791
|
+
createRegistry: () => ToolRegistryOperations;
|
|
792
|
+
}
|
|
793
|
+
/** 对话记录查询选项 */
|
|
794
|
+
interface ChatHistoryOptions {
|
|
795
|
+
/** 返回数量限制 */
|
|
796
|
+
limit?: number;
|
|
797
|
+
/** 排序方向(默认 `'desc'` 最新在前) */
|
|
798
|
+
order?: 'asc' | 'desc';
|
|
799
|
+
}
|
|
800
|
+
/**
|
|
801
|
+
* ask/askStream 便捷方法选项
|
|
802
|
+
*/
|
|
803
|
+
interface AskOptions {
|
|
804
|
+
/** 系统提示词 */
|
|
805
|
+
systemPrompt?: string;
|
|
806
|
+
/** 使用的模型 */
|
|
807
|
+
model?: string;
|
|
808
|
+
/** 交互主体 ID */
|
|
809
|
+
objectId?: string;
|
|
810
|
+
/** 会话 ID */
|
|
811
|
+
sessionId?: string;
|
|
812
|
+
/** 温度(0~2) */
|
|
813
|
+
temperature?: number;
|
|
814
|
+
/** 是否持久化对话记录(默认 true;传入 false 时跳过记录) */
|
|
815
|
+
enablePersist?: boolean;
|
|
816
|
+
}
|
|
817
|
+
/**
|
|
818
|
+
* 对话记录
|
|
819
|
+
*
|
|
820
|
+
* 每次 `llm.chat()` 调用在传入 `objectId` 时自动保存的请求+响应快照。
|
|
821
|
+
*/
|
|
822
|
+
interface ChatRecord {
|
|
823
|
+
/** 记录唯一 ID */
|
|
824
|
+
id: string;
|
|
825
|
+
/** 交互主体 ID */
|
|
826
|
+
objectId: string;
|
|
827
|
+
/** 会话 ID */
|
|
828
|
+
sessionId: string;
|
|
829
|
+
/** 请求摘要 */
|
|
830
|
+
request: {
|
|
831
|
+
model: string;
|
|
832
|
+
messages: ChatMessage[];
|
|
833
|
+
};
|
|
834
|
+
/** 响应摘要 */
|
|
835
|
+
response: {
|
|
836
|
+
content: string;
|
|
837
|
+
toolCalls?: ToolCall[];
|
|
838
|
+
finishReason: string;
|
|
839
|
+
usage: TokenUsage;
|
|
840
|
+
};
|
|
841
|
+
/** 创建时间(Unix 毫秒) */
|
|
842
|
+
createdAt: number;
|
|
843
|
+
/** 耗时(毫秒) */
|
|
844
|
+
duration: number;
|
|
845
|
+
}
|
|
846
|
+
/**
|
|
847
|
+
* LLM Provider 接口
|
|
848
|
+
*
|
|
849
|
+
* 底层 API 适配层,当前内置 OpenAI 兼容实现。
|
|
850
|
+
*/
|
|
851
|
+
interface LLMProvider {
|
|
852
|
+
/** 发送聊天请求并获取完整响应 */
|
|
853
|
+
chat: (request: ChatCompletionRequest) => Promise<HaiResult<ChatCompletionResponse>>;
|
|
854
|
+
/** 发送聊天请求并获取流式响应(逐 chunk 产出) */
|
|
855
|
+
chatStream: (request: ChatCompletionRequest) => AsyncIterable<ChatCompletionChunk>;
|
|
856
|
+
/** 获取可用模型列表 */
|
|
857
|
+
listModels: () => Promise<HaiResult<string[]>>;
|
|
858
|
+
}
|
|
859
|
+
/**
|
|
860
|
+
* LLM 操作接口(通过 `ai.llm` 访问)
|
|
861
|
+
*
|
|
862
|
+
* 需要先调用 `ai.init()` 初始化,否则返回 `NOT_INITIALIZED` 错误。
|
|
863
|
+
*/
|
|
864
|
+
interface LLMOperations {
|
|
865
|
+
/** 发送聊天请求,返回 `HaiResult<ChatCompletionResponse>` */
|
|
866
|
+
chat: (request: ChatCompletionRequest) => Promise<HaiResult<ChatCompletionResponse>>;
|
|
867
|
+
/** 发送流式聊天请求,逐 chunk 产出 `ChatCompletionChunk` */
|
|
868
|
+
chatStream: (request: ChatCompletionRequest) => AsyncIterable<ChatCompletionChunk>;
|
|
869
|
+
/** 获取可用模型名称列表 */
|
|
870
|
+
listModels: () => Promise<HaiResult<string[]>>;
|
|
871
|
+
/** 查询对话历史记录(需传入 objectId;可选 sessionId 以按会话过滤) */
|
|
872
|
+
getHistory: (scope: InteractionScope, options?: ChatHistoryOptions) => Promise<HaiResult<ChatRecord[]>>;
|
|
873
|
+
/** 列出指定 objectId 下的所有会话 */
|
|
874
|
+
listSessions: (objectId: string) => Promise<HaiResult<SessionInfo[]>>;
|
|
875
|
+
/**
|
|
876
|
+
* 便捷方法:发送纯文本问题,返回回复文本
|
|
877
|
+
*
|
|
878
|
+
* 内部构建 ChatCompletionRequest 并调用 `chat()`,只返回第一个 choice 的文本。
|
|
879
|
+
*
|
|
880
|
+
* @param question - 用户问题文本
|
|
881
|
+
* @param options - 可选的模型、systemPrompt、objectId、sessionId 等
|
|
882
|
+
* @returns 回复文本
|
|
883
|
+
*/
|
|
884
|
+
ask: (question: string, options?: AskOptions) => Promise<HaiResult<string>>;
|
|
885
|
+
/**
|
|
886
|
+
* 便捷方法:流式发送纯文本问题,返回文本片段异步迭代器
|
|
887
|
+
*
|
|
888
|
+
* @param question - 用户问题文本
|
|
889
|
+
* @param options - 可选配置
|
|
890
|
+
* @returns 文本片段的异步迭代器
|
|
891
|
+
*/
|
|
892
|
+
askStream: (question: string, options?: AskOptions) => AsyncIterable<string>;
|
|
893
|
+
}
|
|
894
|
+
/** LLM 子功能工厂依赖(内部使用) */
|
|
895
|
+
interface AILLMFunctionsDeps {
|
|
896
|
+
/** 校验后的 AI 配置 */
|
|
897
|
+
config: AIConfig;
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
/**
|
|
901
|
+
* @h-ai/ai — Retrieval 子功能类型
|
|
902
|
+
*
|
|
903
|
+
* 定义检索操作的类型接口,支持向量检索和混合检索。
|
|
904
|
+
* @module ai-retrieval-types
|
|
905
|
+
*/
|
|
906
|
+
|
|
907
|
+
/**
|
|
908
|
+
* 信源引用——结构化描述检索结果的来源信息
|
|
909
|
+
*
|
|
910
|
+
* 用于在 RAG 回答中追踪每段内容的出处。
|
|
911
|
+
*
|
|
912
|
+
* @example
|
|
913
|
+
* ```ts
|
|
914
|
+
* const citation: Citation = {
|
|
915
|
+
* documentId: 'doc-001',
|
|
916
|
+
* title: '项目文档',
|
|
917
|
+
* url: 'https://docs.example.com/project',
|
|
918
|
+
* position: 'section:2',
|
|
919
|
+
* chunkId: 'chunk-003',
|
|
920
|
+
* }
|
|
921
|
+
* ```
|
|
922
|
+
*/
|
|
923
|
+
interface Citation {
|
|
924
|
+
/** 原始文档 ID(入库时的文档级唯一标识) */
|
|
925
|
+
documentId?: string;
|
|
926
|
+
/** 原始文档标题 */
|
|
927
|
+
title?: string;
|
|
928
|
+
/** 原始文档 URL / 路径 */
|
|
929
|
+
url?: string;
|
|
930
|
+
/** 位置信息(页码、段落、section 等) */
|
|
931
|
+
position?: string;
|
|
932
|
+
/** 分块 ID(对应 vecdb 中的向量记录 ID) */
|
|
933
|
+
chunkId?: string;
|
|
934
|
+
/** 信源集合名 */
|
|
935
|
+
collection?: string;
|
|
936
|
+
}
|
|
937
|
+
/**
|
|
938
|
+
* 检索源——描述一个可查询的知识来源
|
|
939
|
+
*/
|
|
940
|
+
interface RetrievalSource {
|
|
941
|
+
/** 来源唯一标识 */
|
|
942
|
+
id: string;
|
|
943
|
+
/** collection / table 名称 */
|
|
944
|
+
collection: string;
|
|
945
|
+
/** 信源显示名(用于 UI 展示) */
|
|
946
|
+
name?: string;
|
|
947
|
+
/** 信源 URL / 路径 */
|
|
948
|
+
url?: string;
|
|
949
|
+
/** 最大返回条数(默认 5) */
|
|
950
|
+
topK?: number;
|
|
951
|
+
/** 最低相似度(低于此值的结果被过滤) */
|
|
952
|
+
minScore?: number;
|
|
953
|
+
/** 元数据过滤条件 */
|
|
954
|
+
filter?: Record<string, unknown>;
|
|
955
|
+
}
|
|
956
|
+
/**
|
|
957
|
+
* 检索请求参数
|
|
958
|
+
*/
|
|
959
|
+
interface RetrievalRequest {
|
|
960
|
+
/** 查询文本 */
|
|
961
|
+
query: string;
|
|
962
|
+
/** 使用的检索源(不指定则使用全部已注册源) */
|
|
963
|
+
sources?: string[];
|
|
964
|
+
/** 全局 topK 覆盖 */
|
|
965
|
+
topK?: number;
|
|
966
|
+
/** 全局 minScore 覆盖 */
|
|
967
|
+
minScore?: number;
|
|
968
|
+
/** 是否启用 Rerank 重排序(需要已初始化 ai.rerank) */
|
|
969
|
+
enableRerank?: boolean;
|
|
970
|
+
/** Rerank 使用的模型名称覆盖 */
|
|
971
|
+
rerankModel?: string;
|
|
972
|
+
}
|
|
973
|
+
/**
|
|
974
|
+
* 单条检索结果
|
|
975
|
+
*/
|
|
976
|
+
interface RetrievalResultItem {
|
|
977
|
+
/** 文档 ID */
|
|
978
|
+
id: string;
|
|
979
|
+
/** 内容文本 */
|
|
980
|
+
content: string;
|
|
981
|
+
/** 相似度分数 [0, 1] */
|
|
982
|
+
score: number;
|
|
983
|
+
/** 来源 id */
|
|
984
|
+
sourceId: string;
|
|
985
|
+
/** 元数据 */
|
|
986
|
+
metadata?: Record<string, unknown>;
|
|
987
|
+
/** 结构化信源引用 */
|
|
988
|
+
citation?: Citation;
|
|
989
|
+
}
|
|
990
|
+
/**
|
|
991
|
+
* 检索结果
|
|
992
|
+
*/
|
|
993
|
+
interface RetrievalResult {
|
|
994
|
+
/** 检索结果列表(按分数降序) */
|
|
995
|
+
items: RetrievalResultItem[];
|
|
996
|
+
/** 查询文本 */
|
|
997
|
+
query: string;
|
|
998
|
+
/** 查询耗时(毫秒) */
|
|
999
|
+
duration: number;
|
|
1000
|
+
}
|
|
1001
|
+
/**
|
|
1002
|
+
* 检索操作接口
|
|
1003
|
+
*/
|
|
1004
|
+
interface RetrievalOperations {
|
|
1005
|
+
/**
|
|
1006
|
+
* 注册一个检索源(持久化到 DB)
|
|
1007
|
+
*
|
|
1008
|
+
* @param source - 检索源配置
|
|
1009
|
+
* @returns 成功返回 ok,重复 id 返回错误
|
|
1010
|
+
*/
|
|
1011
|
+
addSource: (source: RetrievalSource) => Promise<HaiResult<void>>;
|
|
1012
|
+
/**
|
|
1013
|
+
* 移除一个检索源(从 DB 删除)
|
|
1014
|
+
*
|
|
1015
|
+
* @param sourceId - 检索源 ID
|
|
1016
|
+
* @returns 成功返回 ok,未找到返回错误
|
|
1017
|
+
*/
|
|
1018
|
+
removeSource: (sourceId: string) => Promise<HaiResult<void>>;
|
|
1019
|
+
/**
|
|
1020
|
+
* 列出所有已注册的检索源(从 DB 读取,分布式一致)
|
|
1021
|
+
*/
|
|
1022
|
+
listSources: () => Promise<RetrievalSource[]>;
|
|
1023
|
+
/**
|
|
1024
|
+
* 执行检索
|
|
1025
|
+
*
|
|
1026
|
+
* @param request - 检索请求
|
|
1027
|
+
* @returns 检索结果
|
|
1028
|
+
*/
|
|
1029
|
+
retrieve: (request: RetrievalRequest) => Promise<HaiResult<RetrievalResult>>;
|
|
1030
|
+
}
|
|
1031
|
+
|
|
1032
|
+
/**
|
|
1033
|
+
* @h-ai/ai — Knowledge 子功能类型
|
|
1034
|
+
*
|
|
1035
|
+
* 定义知识库操作的类型接口:文档导入、实体索引、信源追踪检索。
|
|
1036
|
+
* @module ai-knowledge-types
|
|
1037
|
+
*/
|
|
1038
|
+
|
|
1039
|
+
/**
|
|
1040
|
+
* 内置实体类型枚举(预设值,可通过配置 `entityTypes` 扩展)
|
|
1041
|
+
*/
|
|
1042
|
+
declare const EntityTypeSchema: z.ZodEnum<{
|
|
1043
|
+
event: "event";
|
|
1044
|
+
person: "person";
|
|
1045
|
+
project: "project";
|
|
1046
|
+
concept: "concept";
|
|
1047
|
+
organization: "organization";
|
|
1048
|
+
location: "location";
|
|
1049
|
+
other: "other";
|
|
1050
|
+
}>;
|
|
1051
|
+
/** 实体类型(字符串,支持内置类型及用户自定义类型) */
|
|
1052
|
+
type EntityType = string;
|
|
1053
|
+
/**
|
|
1054
|
+
* 知识实体
|
|
1055
|
+
*
|
|
1056
|
+
* 表示从文档中提取的命名实体(人名、项目名、概念等),
|
|
1057
|
+
* 存储在 KnowledgeStore 中用于倒排索引查询。
|
|
1058
|
+
*
|
|
1059
|
+
* @example
|
|
1060
|
+
* ```ts
|
|
1061
|
+
* const entity: KnowledgeEntity = {
|
|
1062
|
+
* id: 'ent-001',
|
|
1063
|
+
* name: '张三',
|
|
1064
|
+
* type: 'person',
|
|
1065
|
+
* aliases: ['小张', 'Zhang San'],
|
|
1066
|
+
* }
|
|
1067
|
+
* ```
|
|
1068
|
+
*/
|
|
1069
|
+
interface KnowledgeEntity {
|
|
1070
|
+
/** 实体唯一标识 */
|
|
1071
|
+
id: string;
|
|
1072
|
+
/** 实体名称 */
|
|
1073
|
+
name: string;
|
|
1074
|
+
/** 实体类型 */
|
|
1075
|
+
type: EntityType;
|
|
1076
|
+
/** 别名列表(可选) */
|
|
1077
|
+
aliases?: string[];
|
|
1078
|
+
/** 描述(可选) */
|
|
1079
|
+
description?: string;
|
|
1080
|
+
/** 创建时间 */
|
|
1081
|
+
createdAt?: string;
|
|
1082
|
+
/** 更新时间 */
|
|
1083
|
+
updatedAt?: string;
|
|
1084
|
+
}
|
|
1085
|
+
/**
|
|
1086
|
+
* 文档-实体关联记录
|
|
1087
|
+
*
|
|
1088
|
+
* 倒排索引中的一条记录,记录实体与文档/分块的关联关系。
|
|
1089
|
+
*/
|
|
1090
|
+
interface EntityDocumentRelation {
|
|
1091
|
+
/** 实体 ID */
|
|
1092
|
+
entityId: string;
|
|
1093
|
+
/** 文档 ID(对应向量存储中的 documentId) */
|
|
1094
|
+
documentId: string;
|
|
1095
|
+
/** 分块 ID(对应向量存储中的向量记录 ID) */
|
|
1096
|
+
chunkId?: string;
|
|
1097
|
+
/** 集合名 */
|
|
1098
|
+
collection: string;
|
|
1099
|
+
/** 关联强度 [0, 1](默认 1.0) */
|
|
1100
|
+
relevance?: number;
|
|
1101
|
+
/** 实体在该文档中的上下文片段(可选) */
|
|
1102
|
+
context?: string;
|
|
1103
|
+
/** 创建时间 */
|
|
1104
|
+
createdAt?: string;
|
|
1105
|
+
}
|
|
1106
|
+
/**
|
|
1107
|
+
* 知识库初始化选项
|
|
1108
|
+
*/
|
|
1109
|
+
interface KnowledgeSetupOptions {
|
|
1110
|
+
/** 集合名(可选,默认使用配置中的 collection) */
|
|
1111
|
+
collection?: string;
|
|
1112
|
+
/** 向量维度(可选,默认使用配置中的 dimension) */
|
|
1113
|
+
dimension?: number;
|
|
1114
|
+
}
|
|
1115
|
+
/**
|
|
1116
|
+
* 文档导入输入
|
|
1117
|
+
*
|
|
1118
|
+
* @example
|
|
1119
|
+
* ```ts
|
|
1120
|
+
* const input: KnowledgeIngestInput = {
|
|
1121
|
+
* documentId: 'doc-001',
|
|
1122
|
+
* content: '# 项目文档\n\n张三负责了核心模块...',
|
|
1123
|
+
* title: '项目文档',
|
|
1124
|
+
* url: 'https://docs.example.com/project',
|
|
1125
|
+
* metadata: { author: '李四' },
|
|
1126
|
+
* }
|
|
1127
|
+
* ```
|
|
1128
|
+
*/
|
|
1129
|
+
interface KnowledgeIngestInput {
|
|
1130
|
+
/** 文档唯一标识(必填,用于关联信源和实体) */
|
|
1131
|
+
documentId: string;
|
|
1132
|
+
/** 文档原始内容文本 */
|
|
1133
|
+
content: string;
|
|
1134
|
+
/** 文档标题(可选,存入 metadata 用于信源展示) */
|
|
1135
|
+
title?: string;
|
|
1136
|
+
/** 文档 URL / 路径(可选,存入 metadata 用于信源展示) */
|
|
1137
|
+
url?: string;
|
|
1138
|
+
/** 集合名(可选,默认使用配置中的 collection) */
|
|
1139
|
+
collection?: string;
|
|
1140
|
+
/** 附加元数据(可选,合并到每个 chunk 的 metadata 中) */
|
|
1141
|
+
metadata?: Record<string, unknown>;
|
|
1142
|
+
/** 是否启用实体提取(可选,覆盖全局配置) */
|
|
1143
|
+
enableEntityExtraction?: boolean;
|
|
1144
|
+
/**
|
|
1145
|
+
* 文本清洗选项(可选,覆盖全局配置默认值)
|
|
1146
|
+
*
|
|
1147
|
+
* 字段含义与 @h-ai/datapipe CleanOptionsInput 完全一致,
|
|
1148
|
+
* 支持 removeHtml、removeUrls、normalizeWhitespace、customReplacements 等。
|
|
1149
|
+
*/
|
|
1150
|
+
cleanOptions?: CleanOptionsInput;
|
|
1151
|
+
/**
|
|
1152
|
+
* 分块选项(可选,覆盖全局配置默认值)
|
|
1153
|
+
*
|
|
1154
|
+
* 字段含义与 @h-ai/datapipe ChunkOptionsInput 完全一致,
|
|
1155
|
+
* 支持 mode、maxSize、overlap、separator、markdownMinLevel 等完整选项。
|
|
1156
|
+
*/
|
|
1157
|
+
chunkOptions?: ChunkOptionsInput;
|
|
1158
|
+
}
|
|
1159
|
+
/**
|
|
1160
|
+
* 文档导入结果
|
|
1161
|
+
*/
|
|
1162
|
+
interface KnowledgeIngestResult {
|
|
1163
|
+
/** 文档 ID */
|
|
1164
|
+
documentId: string;
|
|
1165
|
+
/** 生成的分块数量 */
|
|
1166
|
+
chunkCount: number;
|
|
1167
|
+
/** 提取的实体列表(未启用实体提取时为空数组) */
|
|
1168
|
+
entities: KnowledgeEntity[];
|
|
1169
|
+
/** 处理耗时(毫秒) */
|
|
1170
|
+
duration: number;
|
|
1171
|
+
}
|
|
1172
|
+
/**
|
|
1173
|
+
* 知识检索选项
|
|
1174
|
+
*/
|
|
1175
|
+
interface KnowledgeRetrieveOptions {
|
|
1176
|
+
/** 集合名(可选,默认使用配置中的 collection) */
|
|
1177
|
+
collection?: string;
|
|
1178
|
+
/** 返回的最大结果数(默认 10) */
|
|
1179
|
+
topK?: number;
|
|
1180
|
+
/** 最低相似度(默认无限制) */
|
|
1181
|
+
minScore?: number;
|
|
1182
|
+
/** 是否启用实体增强检索(默认 true) */
|
|
1183
|
+
enableEntityBoost?: boolean;
|
|
1184
|
+
/** 元数据过滤条件 */
|
|
1185
|
+
filter?: Record<string, unknown>;
|
|
1186
|
+
}
|
|
1187
|
+
/**
|
|
1188
|
+
* 知识检索结果项
|
|
1189
|
+
*/
|
|
1190
|
+
interface KnowledgeRetrieveItem {
|
|
1191
|
+
/** 分块 ID */
|
|
1192
|
+
id: string;
|
|
1193
|
+
/** 分块内容 */
|
|
1194
|
+
content: string;
|
|
1195
|
+
/** 综合得分(向量相似度 + 实体加权) */
|
|
1196
|
+
score: number;
|
|
1197
|
+
/** 结构化信源引用 */
|
|
1198
|
+
citation: Citation;
|
|
1199
|
+
/** 元数据 */
|
|
1200
|
+
metadata?: Record<string, unknown>;
|
|
1201
|
+
/** 命中的实体名称列表(实体增强检索时填充) */
|
|
1202
|
+
matchedEntities?: string[];
|
|
1203
|
+
}
|
|
1204
|
+
/**
|
|
1205
|
+
* 知识检索结果
|
|
1206
|
+
*/
|
|
1207
|
+
interface KnowledgeRetrieveResult {
|
|
1208
|
+
/** 检索结果列表(按综合分数降序) */
|
|
1209
|
+
items: KnowledgeRetrieveItem[];
|
|
1210
|
+
/** 去重后的信源引用列表 */
|
|
1211
|
+
citations: Citation[];
|
|
1212
|
+
/** 查询文本 */
|
|
1213
|
+
query: string;
|
|
1214
|
+
/** 查询耗时(毫秒) */
|
|
1215
|
+
duration: number;
|
|
1216
|
+
}
|
|
1217
|
+
/**
|
|
1218
|
+
* 知识问答选项
|
|
1219
|
+
*/
|
|
1220
|
+
interface KnowledgeAskOptions extends KnowledgeRetrieveOptions {
|
|
1221
|
+
/** LLM 模型名称覆盖 */
|
|
1222
|
+
model?: string;
|
|
1223
|
+
/** 系统提示词覆盖 */
|
|
1224
|
+
systemPrompt?: string;
|
|
1225
|
+
/** 温度覆盖 */
|
|
1226
|
+
temperature?: number;
|
|
1227
|
+
/** 消息历史(多轮对话) */
|
|
1228
|
+
messages?: ChatMessage[];
|
|
1229
|
+
}
|
|
1230
|
+
/**
|
|
1231
|
+
* 知识问答结果
|
|
1232
|
+
*/
|
|
1233
|
+
interface KnowledgeAskResult {
|
|
1234
|
+
/** LLM 生成的回答 */
|
|
1235
|
+
answer: string;
|
|
1236
|
+
/** 使用的上下文 */
|
|
1237
|
+
context: KnowledgeRetrieveItem[];
|
|
1238
|
+
/** 去重后的信源引用列表 */
|
|
1239
|
+
citations: Citation[];
|
|
1240
|
+
/** 查询文本 */
|
|
1241
|
+
query: string;
|
|
1242
|
+
/** 使用的模型 */
|
|
1243
|
+
model: string;
|
|
1244
|
+
/** Token 使用统计 */
|
|
1245
|
+
usage?: {
|
|
1246
|
+
prompt_tokens: number;
|
|
1247
|
+
completion_tokens: number;
|
|
1248
|
+
total_tokens: number;
|
|
1249
|
+
};
|
|
1250
|
+
}
|
|
1251
|
+
/**
|
|
1252
|
+
* 实体查询选项
|
|
1253
|
+
*/
|
|
1254
|
+
interface EntityQueryOptions {
|
|
1255
|
+
/** 集合名(可选,默认使用配置中的 collection) */
|
|
1256
|
+
collection?: string;
|
|
1257
|
+
/** 实体类型过滤 */
|
|
1258
|
+
type?: EntityType;
|
|
1259
|
+
}
|
|
1260
|
+
/**
|
|
1261
|
+
* 实体关联文档结果
|
|
1262
|
+
*/
|
|
1263
|
+
interface EntityDocumentResult {
|
|
1264
|
+
/** 实体信息 */
|
|
1265
|
+
entity: KnowledgeEntity;
|
|
1266
|
+
/** 关联文档列表 */
|
|
1267
|
+
documents: Array<{
|
|
1268
|
+
documentId: string;
|
|
1269
|
+
chunkId?: string;
|
|
1270
|
+
collection: string;
|
|
1271
|
+
relevance: number;
|
|
1272
|
+
context?: string;
|
|
1273
|
+
}>;
|
|
1274
|
+
}
|
|
1275
|
+
/**
|
|
1276
|
+
* 实体列表查询选项
|
|
1277
|
+
*/
|
|
1278
|
+
interface EntityListOptions {
|
|
1279
|
+
/** 实体类型过滤 */
|
|
1280
|
+
type?: EntityType;
|
|
1281
|
+
/** 关键词搜索(模糊匹配实体名称和别名) */
|
|
1282
|
+
keyword?: string;
|
|
1283
|
+
/** 最大返回数 */
|
|
1284
|
+
limit?: number;
|
|
1285
|
+
}
|
|
1286
|
+
/**
|
|
1287
|
+
* 文件导入输入(从文件路径读取内容后自动 ingest)
|
|
1288
|
+
*
|
|
1289
|
+
* 仅 Node.js 端可用。
|
|
1290
|
+
*
|
|
1291
|
+
* @example
|
|
1292
|
+
* ```ts
|
|
1293
|
+
* await knowledge.ingestFile({
|
|
1294
|
+
* filePath: '/data/docs/README.md',
|
|
1295
|
+
* documentId: 'readme',
|
|
1296
|
+
* })
|
|
1297
|
+
* ```
|
|
1298
|
+
*/
|
|
1299
|
+
interface KnowledgeIngestFileInput {
|
|
1300
|
+
/** 文件路径(绝对路径或相对路径) */
|
|
1301
|
+
filePath: string;
|
|
1302
|
+
/** 文档唯一标识(可选,默认从文件名派生) */
|
|
1303
|
+
documentId?: string;
|
|
1304
|
+
/** 文档标题(可选,默认为文件名) */
|
|
1305
|
+
title?: string;
|
|
1306
|
+
/** 文件编码(默认 'utf-8') */
|
|
1307
|
+
encoding?: BufferEncoding;
|
|
1308
|
+
/** 集合名(可选,默认使用配置中的 collection) */
|
|
1309
|
+
collection?: string;
|
|
1310
|
+
/** 附加元数据 */
|
|
1311
|
+
metadata?: Record<string, unknown>;
|
|
1312
|
+
/** 是否启用实体提取 */
|
|
1313
|
+
enableEntityExtraction?: boolean;
|
|
1314
|
+
/** 清洗选项 */
|
|
1315
|
+
cleanOptions?: CleanOptionsInput;
|
|
1316
|
+
/** 分块选项 */
|
|
1317
|
+
chunkOptions?: ChunkOptionsInput;
|
|
1318
|
+
}
|
|
1319
|
+
/**
|
|
1320
|
+
* 文档信息(列表展示用)
|
|
1321
|
+
*/
|
|
1322
|
+
interface KnowledgeDocumentInfo {
|
|
1323
|
+
/** 文档 ID */
|
|
1324
|
+
documentId: string;
|
|
1325
|
+
/** 文档标题 */
|
|
1326
|
+
title?: string;
|
|
1327
|
+
/** 文档来源 URL / 路径 */
|
|
1328
|
+
url?: string;
|
|
1329
|
+
/** 分块数量 */
|
|
1330
|
+
chunkCount: number;
|
|
1331
|
+
/** 关联实体数 */
|
|
1332
|
+
entityCount: number;
|
|
1333
|
+
/** 创建时间(Unix 毫秒) */
|
|
1334
|
+
createdAt: number;
|
|
1335
|
+
}
|
|
1336
|
+
/**
|
|
1337
|
+
* 文档列表查询选项
|
|
1338
|
+
*/
|
|
1339
|
+
interface KnowledgeDocumentListOptions {
|
|
1340
|
+
/** 集合名覆盖(不指定则使用配置中的 default) */
|
|
1341
|
+
collection?: string;
|
|
1342
|
+
/** 偏移量 */
|
|
1343
|
+
offset?: number;
|
|
1344
|
+
/** 每页数量(默认 20) */
|
|
1345
|
+
limit?: number;
|
|
1346
|
+
}
|
|
1347
|
+
/**
|
|
1348
|
+
* 文档删除选项
|
|
1349
|
+
*/
|
|
1350
|
+
interface KnowledgeDocumentRemoveOptions {
|
|
1351
|
+
/** 集合名覆盖 */
|
|
1352
|
+
collection?: string;
|
|
1353
|
+
}
|
|
1354
|
+
/**
|
|
1355
|
+
* 批量导入进度回调参数
|
|
1356
|
+
*/
|
|
1357
|
+
interface KnowledgeIngestBatchProgress {
|
|
1358
|
+
/** 已完成数量 */
|
|
1359
|
+
completed: number;
|
|
1360
|
+
/** 总数量 */
|
|
1361
|
+
total: number;
|
|
1362
|
+
/** 当前文档 ID */
|
|
1363
|
+
currentDocumentId: string;
|
|
1364
|
+
/** 当前文档导入结果(失败时为 undefined) */
|
|
1365
|
+
result?: KnowledgeIngestResult;
|
|
1366
|
+
/** 当前文档导入错误(成功时为 undefined) */
|
|
1367
|
+
error?: HaiError;
|
|
1368
|
+
}
|
|
1369
|
+
/**
|
|
1370
|
+
* 批量导入结果
|
|
1371
|
+
*/
|
|
1372
|
+
interface KnowledgeIngestBatchResult {
|
|
1373
|
+
/** 成功导入的文档数 */
|
|
1374
|
+
successCount: number;
|
|
1375
|
+
/** 失败的文档数 */
|
|
1376
|
+
failureCount: number;
|
|
1377
|
+
/** 各文档导入结果 */
|
|
1378
|
+
results: Array<{
|
|
1379
|
+
documentId: string;
|
|
1380
|
+
result?: KnowledgeIngestResult;
|
|
1381
|
+
error?: HaiError;
|
|
1382
|
+
}>;
|
|
1383
|
+
/** 总耗时(毫秒) */
|
|
1384
|
+
duration: number;
|
|
1385
|
+
}
|
|
1386
|
+
/**
|
|
1387
|
+
* Knowledge 操作接口(通过 `ai.knowledge` 访问)
|
|
1388
|
+
*
|
|
1389
|
+
* 知识库管理与检索的统一入口,编排 datapipe + KnowledgeStore + embedding + LLM。
|
|
1390
|
+
*
|
|
1391
|
+
* ## 生命周期
|
|
1392
|
+
*
|
|
1393
|
+
* 每个 collection 在使用前必须先调用 `setup()`,支持多次调用以初始化不同分区:
|
|
1394
|
+
*
|
|
1395
|
+
* ```ts
|
|
1396
|
+
* await ai.knowledge.setup() // 使用配置默认 collection
|
|
1397
|
+
* await ai.knowledge.setup({ collection: 'kb-prod' }) // 初始化额外分区
|
|
1398
|
+
* ```
|
|
1399
|
+
*
|
|
1400
|
+
* ## 单 collection 示例
|
|
1401
|
+
*
|
|
1402
|
+
* ```ts
|
|
1403
|
+
* // 初始化
|
|
1404
|
+
* await ai.knowledge.setup()
|
|
1405
|
+
*
|
|
1406
|
+
* // 导入文档
|
|
1407
|
+
* await ai.knowledge.ingest({
|
|
1408
|
+
* documentId: 'doc-001',
|
|
1409
|
+
* content: markdownContent,
|
|
1410
|
+
* title: '项目文档',
|
|
1411
|
+
* })
|
|
1412
|
+
*
|
|
1413
|
+
* // 查询(带信源追踪)
|
|
1414
|
+
* const result = await ai.knowledge.retrieve('张三负责了哪些模块?')
|
|
1415
|
+
*
|
|
1416
|
+
* // 问答(RAG + 信源引用)
|
|
1417
|
+
* const answer = await ai.knowledge.ask('张三负责了哪些模块?')
|
|
1418
|
+
* ```
|
|
1419
|
+
*
|
|
1420
|
+
* ## 多 collection(多分区)示例
|
|
1421
|
+
*
|
|
1422
|
+
* ```ts
|
|
1423
|
+
* // 分别初始化两个知识库分区
|
|
1424
|
+
* await ai.knowledge.setup({ collection: 'kb-product' })
|
|
1425
|
+
* await ai.knowledge.setup({ collection: 'kb-support' })
|
|
1426
|
+
*
|
|
1427
|
+
* // 写入不同分区
|
|
1428
|
+
* await ai.knowledge.ingest({ documentId: 'p-001', content: '...', collection: 'kb-product' })
|
|
1429
|
+
* await ai.knowledge.ingest({ documentId: 's-001', content: '...', collection: 'kb-support' })
|
|
1430
|
+
*
|
|
1431
|
+
* // 从指定分区检索(对未 setup 的 collection 发起任何操作都会返回 KNOWLEDGE_NOT_SETUP 错误)
|
|
1432
|
+
* const result = await ai.knowledge.retrieve('query', { collection: 'kb-support' })
|
|
1433
|
+
* ```
|
|
1434
|
+
*/
|
|
1435
|
+
interface KnowledgeOperations {
|
|
1436
|
+
/**
|
|
1437
|
+
* 初始化知识库(幂等,支持多次调用以初始化不同 collection)
|
|
1438
|
+
*
|
|
1439
|
+
* 创建向量集合和实体表。每个 collection 需独立调用本方法,
|
|
1440
|
+
* 未 setup 的 collection 执行以下操作时均会返回 `KNOWLEDGE_NOT_SETUP` 错误:
|
|
1441
|
+
* `ingest` / `ingestFile` / `ingestBatch` / `retrieve` / `ask` /
|
|
1442
|
+
* `findByEntity` / `listDocuments` / `removeDocument`。
|
|
1443
|
+
*
|
|
1444
|
+
* @param options - 初始化选项(collection 名称、向量维度)
|
|
1445
|
+
* @returns 成功返回 ok(undefined)
|
|
1446
|
+
*/
|
|
1447
|
+
setup: (options?: KnowledgeSetupOptions) => Promise<HaiResult<void>>;
|
|
1448
|
+
/**
|
|
1449
|
+
* 导入文档
|
|
1450
|
+
*
|
|
1451
|
+
* 执行流程:clean → chunk → embed → 向量存储 → 实体提取 → 实体存储
|
|
1452
|
+
*
|
|
1453
|
+
* @param input - 文档导入输入
|
|
1454
|
+
* @returns 导入结果(分块数、实体列表等)
|
|
1455
|
+
*/
|
|
1456
|
+
ingest: (input: KnowledgeIngestInput) => Promise<HaiResult<KnowledgeIngestResult>>;
|
|
1457
|
+
/**
|
|
1458
|
+
* 知识检索(带实体增强 + 信源追踪)
|
|
1459
|
+
*
|
|
1460
|
+
* @param query - 查询文本
|
|
1461
|
+
* @param options - 检索选项
|
|
1462
|
+
* @returns 检索结果(带 citations)
|
|
1463
|
+
*/
|
|
1464
|
+
retrieve: (query: string, options?: KnowledgeRetrieveOptions) => Promise<HaiResult<KnowledgeRetrieveResult>>;
|
|
1465
|
+
/**
|
|
1466
|
+
* 知识问答(RAG + 信源引用)
|
|
1467
|
+
*
|
|
1468
|
+
* @param query - 用户问题
|
|
1469
|
+
* @param options - 问答选项
|
|
1470
|
+
* @returns 问答结果(回答 + 信源)
|
|
1471
|
+
*/
|
|
1472
|
+
ask: (query: string, options?: KnowledgeAskOptions) => Promise<HaiResult<KnowledgeAskResult>>;
|
|
1473
|
+
/**
|
|
1474
|
+
* 按实体查询关联文档
|
|
1475
|
+
*
|
|
1476
|
+
* @param entityName - 实体名称
|
|
1477
|
+
* @param options - 查询选项
|
|
1478
|
+
* @returns 实体关联文档列表
|
|
1479
|
+
*/
|
|
1480
|
+
findByEntity: (entityName: string, options?: EntityQueryOptions) => Promise<HaiResult<EntityDocumentResult[]>>;
|
|
1481
|
+
/**
|
|
1482
|
+
* 列出所有实体
|
|
1483
|
+
*
|
|
1484
|
+
* @param options - 列表选项
|
|
1485
|
+
* @returns 实体列表
|
|
1486
|
+
*/
|
|
1487
|
+
listEntities: (options?: EntityListOptions) => Promise<HaiResult<KnowledgeEntity[]>>;
|
|
1488
|
+
/**
|
|
1489
|
+
* 列出已导入的文档列表
|
|
1490
|
+
*
|
|
1491
|
+
* @param options - 列表选项
|
|
1492
|
+
* @returns 文档信息列表
|
|
1493
|
+
*/
|
|
1494
|
+
listDocuments: (options?: KnowledgeDocumentListOptions) => Promise<HaiResult<KnowledgeDocumentInfo[]>>;
|
|
1495
|
+
/**
|
|
1496
|
+
* 删除已导入的文档(同时删除向量和实体关联)
|
|
1497
|
+
*
|
|
1498
|
+
* @param documentId - 文档 ID
|
|
1499
|
+
* @param options - 删除选项
|
|
1500
|
+
* @returns 成功返回 ok(undefined)
|
|
1501
|
+
*/
|
|
1502
|
+
removeDocument: (documentId: string, options?: KnowledgeDocumentRemoveOptions) => Promise<HaiResult<void>>;
|
|
1503
|
+
/**
|
|
1504
|
+
* 从文件路径导入文档(仅 Node.js 端可用)
|
|
1505
|
+
*
|
|
1506
|
+
* 读取文件内容后自动调用 ingest() 完成导入。
|
|
1507
|
+
*
|
|
1508
|
+
* @param input - 文件导入输入
|
|
1509
|
+
* @returns 导入结果
|
|
1510
|
+
*/
|
|
1511
|
+
ingestFile: (input: KnowledgeIngestFileInput) => Promise<HaiResult<KnowledgeIngestResult>>;
|
|
1512
|
+
/**
|
|
1513
|
+
* 批量导入文档
|
|
1514
|
+
*
|
|
1515
|
+
* 依次执行 ingest(),通过 onProgress 回调报告进度,单个文档失败不中断整体流程。
|
|
1516
|
+
*
|
|
1517
|
+
* @param inputs - 文档导入输入列表
|
|
1518
|
+
* @param onProgress - 每完成一个文档后的回调
|
|
1519
|
+
* @returns 批量导入汇总结果
|
|
1520
|
+
*/
|
|
1521
|
+
ingestBatch: (inputs: KnowledgeIngestInput[], onProgress?: (progress: KnowledgeIngestBatchProgress) => void) => Promise<HaiResult<KnowledgeIngestBatchResult>>;
|
|
1522
|
+
}
|
|
1523
|
+
|
|
1524
|
+
/**
|
|
1525
|
+
* @h-ai/ai — Memory 子功能类型
|
|
1526
|
+
*
|
|
1527
|
+
* 定义记忆管理操作的类型接口:提取、存储、检索、注入。
|
|
1528
|
+
* 支持从对话中自动提取关键事实、偏好、事件等记忆,并在后续对话中检索注入。
|
|
1529
|
+
* @module ai-memory-types
|
|
1530
|
+
*/
|
|
1531
|
+
|
|
1532
|
+
/**
|
|
1533
|
+
* 记忆类型枚举 Schema
|
|
1534
|
+
*
|
|
1535
|
+
* 定义系统支持的五种记忆类型,用于对提取和存储的记忆进行分类:
|
|
1536
|
+
* - `fact` — 客观事实(如「用户是后端工程师」「项目使用 TypeScript」)
|
|
1537
|
+
* - `preference` — 用户偏好(如「喜欢简洁的代码风格」「偏好中文回复」)
|
|
1538
|
+
* - `event` — 事件/时间线信息(如「上周部署了 v2.0」「昨天修复了登录 Bug」)
|
|
1539
|
+
* - `entity` — 命名实体(如人名、产品名、组织名等,便于后续实体关联)
|
|
1540
|
+
* - `instruction` — 用户给出的持久指令(如「以后都用函数式写法」「回复不超过 200 字」)
|
|
1541
|
+
*
|
|
1542
|
+
* @example
|
|
1543
|
+
* ```ts
|
|
1544
|
+
* import { MemoryTypeSchema } from '@h-ai/ai'
|
|
1545
|
+
*
|
|
1546
|
+
* // 校验字符串是否为合法记忆类型
|
|
1547
|
+
* const result = MemoryTypeSchema.safeParse('preference') // { success: true }
|
|
1548
|
+
*
|
|
1549
|
+
* // 用于配置或过滤
|
|
1550
|
+
* await ai.memory.recall('编程语言', { types: ['preference', 'fact'] })
|
|
1551
|
+
* ```
|
|
1552
|
+
*/
|
|
1553
|
+
declare const MemoryTypeSchema: z.ZodEnum<{
|
|
1554
|
+
fact: "fact";
|
|
1555
|
+
preference: "preference";
|
|
1556
|
+
event: "event";
|
|
1557
|
+
entity: "entity";
|
|
1558
|
+
instruction: "instruction";
|
|
1559
|
+
}>;
|
|
1560
|
+
/** 记忆类型 */
|
|
1561
|
+
type MemoryType = z.infer<typeof MemoryTypeSchema>;
|
|
1562
|
+
/**
|
|
1563
|
+
* 记忆条目输入(手动添加时使用)
|
|
1564
|
+
*
|
|
1565
|
+
* @example
|
|
1566
|
+
* ```ts
|
|
1567
|
+
* const input: MemoryEntryInput = {
|
|
1568
|
+
* content: '用户偏好使用中文回复',
|
|
1569
|
+
* type: 'preference',
|
|
1570
|
+
* importance: 0.8,
|
|
1571
|
+
* objectId: 'user-001',
|
|
1572
|
+
* }
|
|
1573
|
+
* ```
|
|
1574
|
+
*/
|
|
1575
|
+
interface MemoryEntryInput {
|
|
1576
|
+
/** 记忆内容 */
|
|
1577
|
+
content: string;
|
|
1578
|
+
/** 记忆类型 */
|
|
1579
|
+
type: MemoryType;
|
|
1580
|
+
/** 重要性评分 [0, 1](可选,默认 0.5) */
|
|
1581
|
+
importance?: number;
|
|
1582
|
+
/** 所属主体 ID(不指定时为全局记忆) */
|
|
1583
|
+
objectId?: string;
|
|
1584
|
+
/** 附加元数据 */
|
|
1585
|
+
metadata?: Record<string, unknown>;
|
|
1586
|
+
}
|
|
1587
|
+
/**
|
|
1588
|
+
* 完整的记忆条目
|
|
1589
|
+
*/
|
|
1590
|
+
interface MemoryEntry {
|
|
1591
|
+
/** 记忆唯一标识 */
|
|
1592
|
+
id: string;
|
|
1593
|
+
/** 记忆内容 */
|
|
1594
|
+
content: string;
|
|
1595
|
+
/** 记忆类型 */
|
|
1596
|
+
type: MemoryType;
|
|
1597
|
+
/** 重要性评分 [0, 1] */
|
|
1598
|
+
importance: number;
|
|
1599
|
+
/** 所属主体 ID */
|
|
1600
|
+
objectId?: string;
|
|
1601
|
+
/** 附加元数据 */
|
|
1602
|
+
metadata?: Record<string, unknown>;
|
|
1603
|
+
/** 向量(embedding 已计算时填充) */
|
|
1604
|
+
vector?: number[];
|
|
1605
|
+
/** 创建时间(Unix 毫秒) */
|
|
1606
|
+
createdAt: number;
|
|
1607
|
+
/** 最近访问时间(Unix 毫秒) */
|
|
1608
|
+
lastAccessedAt: number;
|
|
1609
|
+
/** 被检索次数 */
|
|
1610
|
+
accessCount: number;
|
|
1611
|
+
}
|
|
1612
|
+
/**
|
|
1613
|
+
* 记忆提取选项
|
|
1614
|
+
*/
|
|
1615
|
+
interface MemoryExtractOptions {
|
|
1616
|
+
/** 只提取指定类型 */
|
|
1617
|
+
types?: MemoryType[];
|
|
1618
|
+
/** 指定提取用的模型 */
|
|
1619
|
+
model?: string;
|
|
1620
|
+
/** 自定义提取 systemPrompt(覆盖模块配置与内置默认提示词) */
|
|
1621
|
+
systemPrompt?: string;
|
|
1622
|
+
/** 过滤低重要性条目(默认 0) */
|
|
1623
|
+
minImportance?: number;
|
|
1624
|
+
/** 所属主体 ID(关联到提取结果) */
|
|
1625
|
+
objectId?: string;
|
|
1626
|
+
}
|
|
1627
|
+
/**
|
|
1628
|
+
* 记忆检索选项
|
|
1629
|
+
*/
|
|
1630
|
+
interface MemoryRecallOptions {
|
|
1631
|
+
/** 返回数量(默认使用配置的 defaultTopK) */
|
|
1632
|
+
topK?: number;
|
|
1633
|
+
/** 过滤类型 */
|
|
1634
|
+
types?: MemoryType[];
|
|
1635
|
+
/** 最低重要性 */
|
|
1636
|
+
minImportance?: number;
|
|
1637
|
+
/** 时间衰减权重 [0, 1](0 = 不考虑时间,1 = 仅按时间排序) */
|
|
1638
|
+
recencyWeight?: number;
|
|
1639
|
+
/** 限定主体 ID */
|
|
1640
|
+
objectId?: string;
|
|
1641
|
+
}
|
|
1642
|
+
/**
|
|
1643
|
+
* 记忆注入选项(`injectMemories` 使用)
|
|
1644
|
+
*
|
|
1645
|
+
* 控制记忆注入行为:检索数量、Token 预算、注入位置等。
|
|
1646
|
+
*/
|
|
1647
|
+
interface MemoryInjectionOptions {
|
|
1648
|
+
/** 注入的记忆数量(默认 5) */
|
|
1649
|
+
topK?: number;
|
|
1650
|
+
/** 记忆占用的最大 token 预算(默认不限) */
|
|
1651
|
+
maxTokens?: number;
|
|
1652
|
+
/** 注入位置:system = 追加到 system 消息末尾,before-last = 插入在最后一条用户消息之前 */
|
|
1653
|
+
position?: 'system' | 'before-last';
|
|
1654
|
+
/** 限定主体 ID */
|
|
1655
|
+
objectId?: string;
|
|
1656
|
+
}
|
|
1657
|
+
/**
|
|
1658
|
+
* 记忆列表选项
|
|
1659
|
+
*/
|
|
1660
|
+
interface MemoryListOptions {
|
|
1661
|
+
/** 过滤类型 */
|
|
1662
|
+
types?: MemoryType[];
|
|
1663
|
+
/** 限定主体 ID */
|
|
1664
|
+
objectId?: string;
|
|
1665
|
+
/** 最大返回数 */
|
|
1666
|
+
limit?: number;
|
|
1667
|
+
}
|
|
1668
|
+
/**
|
|
1669
|
+
* 记忆分页选项
|
|
1670
|
+
*/
|
|
1671
|
+
interface MemoryListPageOptions {
|
|
1672
|
+
/** 过滤类型 */
|
|
1673
|
+
types?: MemoryType[];
|
|
1674
|
+
/** 限定主体 ID */
|
|
1675
|
+
objectId?: string;
|
|
1676
|
+
/** 偏移量 */
|
|
1677
|
+
offset?: number;
|
|
1678
|
+
/** 每页数量(默认 20) */
|
|
1679
|
+
limit?: number;
|
|
1680
|
+
}
|
|
1681
|
+
/**
|
|
1682
|
+
* 记忆清空选项
|
|
1683
|
+
*/
|
|
1684
|
+
interface MemoryClearOptions {
|
|
1685
|
+
/** 仅清空指定类型 */
|
|
1686
|
+
types?: MemoryType[];
|
|
1687
|
+
/** 仅清空指定主体 */
|
|
1688
|
+
objectId?: string;
|
|
1689
|
+
}
|
|
1690
|
+
/**
|
|
1691
|
+
* 记忆条目更新输入
|
|
1692
|
+
*
|
|
1693
|
+
* 所有字段均为可选,仅传入需要更新的字段。
|
|
1694
|
+
*/
|
|
1695
|
+
interface MemoryUpdateInput {
|
|
1696
|
+
/** 更新记忆内容(同时重新计算向量) */
|
|
1697
|
+
content?: string;
|
|
1698
|
+
/** 更新记忆类型 */
|
|
1699
|
+
type?: MemoryType;
|
|
1700
|
+
/** 更新重要性 */
|
|
1701
|
+
importance?: number;
|
|
1702
|
+
/** 更新元数据 */
|
|
1703
|
+
metadata?: Record<string, unknown>;
|
|
1704
|
+
}
|
|
1705
|
+
/**
|
|
1706
|
+
* Memory 操作接口(通过 `ai.memory` 访问)
|
|
1707
|
+
*
|
|
1708
|
+
* 管理对话中产生的关键事实、用户偏好、长期知识的提取、存储与检索。
|
|
1709
|
+
* 需要先调用 `ai.init()` 初始化后使用。
|
|
1710
|
+
*
|
|
1711
|
+
* @example
|
|
1712
|
+
* ```ts
|
|
1713
|
+
* // 从对话中自动提取记忆
|
|
1714
|
+
* const extracted = await ai.memory.extract(messages, {
|
|
1715
|
+
* objectId: 'user-001',
|
|
1716
|
+
* systemPrompt: 'Only extract durable user preferences and explicit long-term instructions.',
|
|
1717
|
+
* })
|
|
1718
|
+
*
|
|
1719
|
+
* // 手动添加记忆
|
|
1720
|
+
* await ai.memory.add({ content: '用户偏好中文', type: 'preference', objectId: 'user-001' })
|
|
1721
|
+
*
|
|
1722
|
+
* // 检索相关记忆
|
|
1723
|
+
* const memories = await ai.memory.recall('用户的语言偏好', { objectId: 'user-001' })
|
|
1724
|
+
*
|
|
1725
|
+
* // 将记忆注入消息列表
|
|
1726
|
+
* const enriched = await ai.memory.injectMemories(newMessages, { objectId: 'user-001' })
|
|
1727
|
+
* const response = await ai.llm.chat({ messages: enriched.value })
|
|
1728
|
+
* ```
|
|
1729
|
+
*/
|
|
1730
|
+
interface MemoryOperations {
|
|
1731
|
+
/**
|
|
1732
|
+
* 从对话消息中自动提取记忆条目
|
|
1733
|
+
*
|
|
1734
|
+
* 使用 LLM 分析对话内容,提取值得记住的事实、偏好、事件等。
|
|
1735
|
+
* 提取的记忆会自动持久化到 Store(含向量计算)。
|
|
1736
|
+
*
|
|
1737
|
+
* @param messages - 对话消息列表
|
|
1738
|
+
* @param options - 提取选项
|
|
1739
|
+
* @returns 提取到的记忆条目列表
|
|
1740
|
+
*/
|
|
1741
|
+
extract: (messages: ChatMessage[], options?: MemoryExtractOptions) => Promise<HaiResult<MemoryEntry[]>>;
|
|
1742
|
+
/**
|
|
1743
|
+
* 根据查询检索最相关的记忆
|
|
1744
|
+
*
|
|
1745
|
+
* 综合向量相似度、重要性、时间衰减进行排序。
|
|
1746
|
+
*
|
|
1747
|
+
* @param query - 查询文本
|
|
1748
|
+
* @param options - 检索选项
|
|
1749
|
+
* @returns 相关记忆列表
|
|
1750
|
+
*/
|
|
1751
|
+
recall: (query: string, options?: MemoryRecallOptions) => Promise<HaiResult<MemoryEntry[]>>;
|
|
1752
|
+
/**
|
|
1753
|
+
* 将相关记忆注入到消息列表中
|
|
1754
|
+
*
|
|
1755
|
+
* 工作流程:
|
|
1756
|
+
* 1. 从消息列表中提取最后一条用户消息作为检索查询
|
|
1757
|
+
* 2. 调用 `recall` 检索最相关的记忆条目
|
|
1758
|
+
* 3. 将记忆格式化为文本块,按指定位置注入消息列表
|
|
1759
|
+
*
|
|
1760
|
+
* @param messages - 原始消息列表
|
|
1761
|
+
* @param options - 注入选项(数量、位置、Token 预算等)
|
|
1762
|
+
* @returns 注入记忆后的新消息列表(不修改原数组)
|
|
1763
|
+
*/
|
|
1764
|
+
injectMemories: (messages: ChatMessage[], options?: MemoryInjectionOptions) => Promise<HaiResult<ChatMessage[]>>;
|
|
1765
|
+
/**
|
|
1766
|
+
* 手动添加一条记忆
|
|
1767
|
+
*
|
|
1768
|
+
* 记忆会自动持久化到 Store(含向量计算)。
|
|
1769
|
+
*
|
|
1770
|
+
* @param entry - 记忆条目输入
|
|
1771
|
+
* @returns 存储后的完整记忆条目
|
|
1772
|
+
*/
|
|
1773
|
+
add: (entry: MemoryEntryInput) => Promise<HaiResult<MemoryEntry>>;
|
|
1774
|
+
/**
|
|
1775
|
+
* 更新一条已有记忆
|
|
1776
|
+
*
|
|
1777
|
+
* 仅更新传入的字段,其余字段保持不变。
|
|
1778
|
+
* 若 content 被更新,会重新计算向量。
|
|
1779
|
+
* 更新结果自动持久化到 Store。
|
|
1780
|
+
*
|
|
1781
|
+
* @param memoryId - 记忆 ID
|
|
1782
|
+
* @param updates - 需要更新的字段
|
|
1783
|
+
* @returns 更新后的完整记忆条目
|
|
1784
|
+
*/
|
|
1785
|
+
update: (memoryId: string, updates: MemoryUpdateInput) => Promise<HaiResult<MemoryEntry>>;
|
|
1786
|
+
/**
|
|
1787
|
+
* 按 ID 获取单条记忆
|
|
1788
|
+
*
|
|
1789
|
+
* @param memoryId - 记忆 ID
|
|
1790
|
+
* @returns 记忆条目,不存在时返回 MEMORY_NOT_FOUND
|
|
1791
|
+
*/
|
|
1792
|
+
get: (memoryId: string) => Promise<HaiResult<MemoryEntry>>;
|
|
1793
|
+
/**
|
|
1794
|
+
* 删除单条记忆
|
|
1795
|
+
*
|
|
1796
|
+
* 同时从 Store 中移除持久化数据。
|
|
1797
|
+
*
|
|
1798
|
+
* @param memoryId - 记忆 ID
|
|
1799
|
+
* @returns 成功返回 ok(undefined)
|
|
1800
|
+
*/
|
|
1801
|
+
remove: (memoryId: string) => Promise<HaiResult<void>>;
|
|
1802
|
+
/**
|
|
1803
|
+
* 获取记忆列表
|
|
1804
|
+
*
|
|
1805
|
+
* @param options - 列表选项
|
|
1806
|
+
* @returns 记忆条目列表
|
|
1807
|
+
*/
|
|
1808
|
+
list: (options?: MemoryListOptions) => Promise<HaiResult<MemoryEntry[]>>;
|
|
1809
|
+
/**
|
|
1810
|
+
* 分页获取记忆列表
|
|
1811
|
+
*
|
|
1812
|
+
* @param options - 分页选项
|
|
1813
|
+
* @returns 分页结果
|
|
1814
|
+
*/
|
|
1815
|
+
listPage: (options?: MemoryListPageOptions) => Promise<HaiResult<StorePage<MemoryEntry>>>;
|
|
1816
|
+
/**
|
|
1817
|
+
* 清空记忆
|
|
1818
|
+
*
|
|
1819
|
+
* 同时从 Store 中移除持久化数据。
|
|
1820
|
+
*
|
|
1821
|
+
* @param options - 清空选项(可按类型/主体过滤)
|
|
1822
|
+
*/
|
|
1823
|
+
clear: (options?: MemoryClearOptions) => Promise<HaiResult<void>>;
|
|
1824
|
+
}
|
|
1825
|
+
|
|
1826
|
+
/**
|
|
1827
|
+
* @h-ai/ai — 错误码 + 配置 Schema
|
|
1828
|
+
*
|
|
1829
|
+
* 定义 AI 模块的错误码常量、Zod Schema 和配置类型。
|
|
1830
|
+
* @module ai-config
|
|
1831
|
+
*/
|
|
1832
|
+
|
|
1833
|
+
/**
|
|
1834
|
+
* 模型场景枚举
|
|
1835
|
+
*
|
|
1836
|
+
* 预定义的模型使用场景,用于自动选择合适的模型。
|
|
1837
|
+
*
|
|
1838
|
+
* - `default` — 默认场景(兜底)
|
|
1839
|
+
* - `chat` — 对话场景
|
|
1840
|
+
* - `reasoning` — 推理场景(ReAct、CoT,需要强逻辑能力)
|
|
1841
|
+
* - `plan` — Plan-Execute 规划阶段(需要强推理)
|
|
1842
|
+
* - `execute` — Plan-Execute 执行阶段(需要工具调用能力)
|
|
1843
|
+
* - `extraction` — 信息提取场景(记忆提取、实体抽取)
|
|
1844
|
+
* - `summary` — 摘要/压缩场景(上下文摘要)
|
|
1845
|
+
* - `embedding` — 向量嵌入场景
|
|
1846
|
+
* - `rerank` — 文档重排序场景
|
|
1847
|
+
* - `ocr` — 图片 OCR 识别场景(视觉模型)
|
|
1848
|
+
* - `fast` — 快速响应场景(低延迟优先)
|
|
1849
|
+
*/
|
|
1850
|
+
declare const ModelScenarioSchema: z.ZodEnum<{
|
|
1851
|
+
default: "default";
|
|
1852
|
+
chat: "chat";
|
|
1853
|
+
reasoning: "reasoning";
|
|
1854
|
+
plan: "plan";
|
|
1855
|
+
execute: "execute";
|
|
1856
|
+
extraction: "extraction";
|
|
1857
|
+
summary: "summary";
|
|
1858
|
+
embedding: "embedding";
|
|
1859
|
+
rerank: "rerank";
|
|
1860
|
+
ocr: "ocr";
|
|
1861
|
+
fast: "fast";
|
|
1862
|
+
}>;
|
|
1863
|
+
/** 模型场景类型 */
|
|
1864
|
+
type ModelScenario = z.infer<typeof ModelScenarioSchema>;
|
|
1865
|
+
/**
|
|
1866
|
+
* 模型条目 Schema
|
|
1867
|
+
*
|
|
1868
|
+
* 定义单个模型的配置信息,包含唯一 ID、模型名称和可选参数覆盖。
|
|
1869
|
+
*
|
|
1870
|
+
* @example
|
|
1871
|
+
* ```ts
|
|
1872
|
+
* const model = {
|
|
1873
|
+
* id: 'gpt-4o',
|
|
1874
|
+
* model: 'gpt-4o',
|
|
1875
|
+
* maxTokens: 8192,
|
|
1876
|
+
* temperature: 0.3,
|
|
1877
|
+
* }
|
|
1878
|
+
* ```
|
|
1879
|
+
*/
|
|
1880
|
+
declare const ModelEntrySchema: z.ZodObject<{
|
|
1881
|
+
id: z.ZodString;
|
|
1882
|
+
model: z.ZodString;
|
|
1883
|
+
apiKey: z.ZodOptional<z.ZodString>;
|
|
1884
|
+
baseUrl: z.ZodOptional<z.ZodURL>;
|
|
1885
|
+
maxTokens: z.ZodOptional<z.ZodNumber>;
|
|
1886
|
+
temperature: z.ZodOptional<z.ZodNumber>;
|
|
1887
|
+
timeout: z.ZodOptional<z.ZodNumber>;
|
|
1888
|
+
}, z.core.$strip>;
|
|
1889
|
+
/** 模型条目类型 */
|
|
1890
|
+
type ModelEntry = z.infer<typeof ModelEntrySchema>;
|
|
1891
|
+
/**
|
|
1892
|
+
* LLM 配置 Schema
|
|
1893
|
+
*
|
|
1894
|
+
* 配置大模型调用参数:模型名称、API Key、Base URL、温度等。
|
|
1895
|
+
* 支持多模型注册和场景映射。
|
|
1896
|
+
*
|
|
1897
|
+
* 顶层 `apiKey`、`baseUrl`、`maxTokens`、`temperature`、`timeout` 作为全局默认值,
|
|
1898
|
+
* 当 `models` 条目中未指定对应字段时,自动回退到这些全局默认值。
|
|
1899
|
+
* 通过 `resolveModelEntry()` 统一解析。
|
|
1900
|
+
*
|
|
1901
|
+
* @example
|
|
1902
|
+
* ```ts
|
|
1903
|
+
* const llmConfig = {
|
|
1904
|
+
* // 全局默认值(各模型未指定时回退到此处)
|
|
1905
|
+
* apiKey: 'sk-xxx',
|
|
1906
|
+
* baseUrl: 'https://api.openai.com/v1',
|
|
1907
|
+
* model: 'gpt-4o-mini',
|
|
1908
|
+
* maxTokens: 4096,
|
|
1909
|
+
* temperature: 0.7,
|
|
1910
|
+
* timeout: 60000,
|
|
1911
|
+
* // 多模型注册(各字段可选,未指定时回退到全局默认值)
|
|
1912
|
+
* models: [
|
|
1913
|
+
* { id: 'fast', model: 'gpt-4o-mini', temperature: 0.3 },
|
|
1914
|
+
* { id: 'strong', model: 'gpt-4o', maxTokens: 8192 },
|
|
1915
|
+
* { id: 'rerank', model: 'rerank-english-v3.0', baseUrl: 'https://api.cohere.com' },
|
|
1916
|
+
* ],
|
|
1917
|
+
* // 场景映射(场景名 → 模型 ID 或模型名称)
|
|
1918
|
+
* scenarios: { chat: 'fast', reasoning: 'strong', rerank: 'rerank' },
|
|
1919
|
+
* }
|
|
1920
|
+
* ```
|
|
1921
|
+
*/
|
|
1922
|
+
declare const LLMConfigSchema: z.ZodObject<{
|
|
1923
|
+
apiKey: z.ZodOptional<z.ZodString>;
|
|
1924
|
+
baseUrl: z.ZodOptional<z.ZodURL>;
|
|
1925
|
+
model: z.ZodDefault<z.ZodOptional<z.ZodString>>;
|
|
1926
|
+
maxTokens: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
|
|
1927
|
+
temperature: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
|
|
1928
|
+
timeout: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
|
|
1929
|
+
models: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
1930
|
+
id: z.ZodString;
|
|
1931
|
+
model: z.ZodString;
|
|
1932
|
+
apiKey: z.ZodOptional<z.ZodString>;
|
|
1933
|
+
baseUrl: z.ZodOptional<z.ZodURL>;
|
|
1934
|
+
maxTokens: z.ZodOptional<z.ZodNumber>;
|
|
1935
|
+
temperature: z.ZodOptional<z.ZodNumber>;
|
|
1936
|
+
timeout: z.ZodOptional<z.ZodNumber>;
|
|
1937
|
+
}, z.core.$strip>>>;
|
|
1938
|
+
scenarios: z.ZodOptional<z.ZodObject<{
|
|
1939
|
+
default: z.ZodOptional<z.ZodString>;
|
|
1940
|
+
chat: z.ZodOptional<z.ZodString>;
|
|
1941
|
+
reasoning: z.ZodOptional<z.ZodString>;
|
|
1942
|
+
plan: z.ZodOptional<z.ZodString>;
|
|
1943
|
+
execute: z.ZodOptional<z.ZodString>;
|
|
1944
|
+
extraction: z.ZodOptional<z.ZodString>;
|
|
1945
|
+
summary: z.ZodOptional<z.ZodString>;
|
|
1946
|
+
embedding: z.ZodOptional<z.ZodString>;
|
|
1947
|
+
rerank: z.ZodOptional<z.ZodString>;
|
|
1948
|
+
ocr: z.ZodOptional<z.ZodString>;
|
|
1949
|
+
fast: z.ZodOptional<z.ZodString>;
|
|
1950
|
+
}, z.core.$strip>>;
|
|
1951
|
+
}, z.core.$strip>;
|
|
1952
|
+
/** LLM 配置类型 */
|
|
1953
|
+
type LLMConfig = z.infer<typeof LLMConfigSchema>;
|
|
1954
|
+
/**
|
|
1955
|
+
* 已解析的模型配置
|
|
1956
|
+
*
|
|
1957
|
+
* 由 `resolveModelEntry()` 返回,包含模型名称和完整的参数配置(已合并全局默认值和环境变量)。
|
|
1958
|
+
*/
|
|
1959
|
+
interface ResolvedModelConfig {
|
|
1960
|
+
/** 模型名称(传给 API 的实际模型名) */
|
|
1961
|
+
model: string;
|
|
1962
|
+
/** API Key(模型条目 > 全局配置 > 环境变量) */
|
|
1963
|
+
apiKey: string | undefined;
|
|
1964
|
+
/** API 基础 URL(模型条目 > 全局配置 > 环境变量 > 默认 OpenAI) */
|
|
1965
|
+
baseUrl: string;
|
|
1966
|
+
/** 最大 Token 数(模型条目 > 全局配置) */
|
|
1967
|
+
maxTokens: number;
|
|
1968
|
+
/** 采样温度(模型条目 > 全局配置) */
|
|
1969
|
+
temperature: number;
|
|
1970
|
+
/** 请求超时时间(毫秒)(模型条目 > 全局配置) */
|
|
1971
|
+
timeout: number;
|
|
1972
|
+
}
|
|
1973
|
+
/**
|
|
1974
|
+
* 必需模型解析选项
|
|
1975
|
+
*/
|
|
1976
|
+
interface ResolveRequiredModelEntryOptions {
|
|
1977
|
+
/** 缺少 API Key 时的自定义错误消息 */
|
|
1978
|
+
missingApiKeyMessage?: string;
|
|
1979
|
+
}
|
|
1980
|
+
/**
|
|
1981
|
+
* 根据场景解析完整模型配置,并要求必须存在 API Key
|
|
1982
|
+
*
|
|
1983
|
+
* 用于需要访问远程模型 API 的场景,避免各子模块重复编写
|
|
1984
|
+
* `if (!resolved.apiKey)` 之类的配置校验逻辑。
|
|
1985
|
+
*
|
|
1986
|
+
* @param llmConfig - LLM 配置
|
|
1987
|
+
* @param scenario - 使用场景
|
|
1988
|
+
* @param explicit - 调用方显式指定的模型名称(最高优先级)
|
|
1989
|
+
* @param options - 必需校验选项
|
|
1990
|
+
* @returns 成功返回已解析模型配置,失败返回 `CONFIGURATION_ERROR`
|
|
1991
|
+
*/
|
|
1992
|
+
declare function resolveModelEntry(llmConfig: LLMConfig, scenario: ModelScenario, explicit?: string, options?: ResolveRequiredModelEntryOptions): HaiResult<ResolvedModelConfig>;
|
|
1993
|
+
/** MCP 服务器能力 Schema */
|
|
1994
|
+
declare const MCPServerCapabilitiesSchema: z.ZodObject<{
|
|
1995
|
+
tools: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
|
|
1996
|
+
resources: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
|
|
1997
|
+
prompts: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
|
|
1998
|
+
}, z.core.$strip>;
|
|
1999
|
+
/** MCP 服务器能力类型 */
|
|
2000
|
+
type MCPServerCapabilities = z.infer<typeof MCPServerCapabilitiesSchema>;
|
|
2001
|
+
/** MCP 服务器配置 Schema */
|
|
2002
|
+
declare const MCPServerConfigSchema: z.ZodObject<{
|
|
2003
|
+
name: z.ZodString;
|
|
2004
|
+
version: z.ZodDefault<z.ZodOptional<z.ZodString>>;
|
|
2005
|
+
capabilities: z.ZodOptional<z.ZodObject<{
|
|
2006
|
+
tools: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
|
|
2007
|
+
resources: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
|
|
2008
|
+
prompts: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
|
|
2009
|
+
}, z.core.$strip>>;
|
|
2010
|
+
}, z.core.$strip>;
|
|
2011
|
+
/** MCP 服务器配置类型 */
|
|
2012
|
+
type MCPServerConfig = z.infer<typeof MCPServerConfigSchema>;
|
|
2013
|
+
/**
|
|
2014
|
+
* MCP 配置 Schema
|
|
2015
|
+
*
|
|
2016
|
+
* 配置 MCP(Model Context Protocol)服务器参数。
|
|
2017
|
+
*
|
|
2018
|
+
* @example
|
|
2019
|
+
* ```ts
|
|
2020
|
+
* const mcpConfig = {
|
|
2021
|
+
* server: {
|
|
2022
|
+
* name: 'my-app',
|
|
2023
|
+
* version: '1.0.0',
|
|
2024
|
+
* capabilities: { tools: true, resources: true, prompts: true },
|
|
2025
|
+
* },
|
|
2026
|
+
* }
|
|
2027
|
+
* ```
|
|
2028
|
+
*/
|
|
2029
|
+
declare const MCPConfigSchema: z.ZodObject<{
|
|
2030
|
+
server: z.ZodOptional<z.ZodObject<{
|
|
2031
|
+
name: z.ZodString;
|
|
2032
|
+
version: z.ZodDefault<z.ZodOptional<z.ZodString>>;
|
|
2033
|
+
capabilities: z.ZodOptional<z.ZodObject<{
|
|
2034
|
+
tools: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
|
|
2035
|
+
resources: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
|
|
2036
|
+
prompts: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
|
|
2037
|
+
}, z.core.$strip>>;
|
|
2038
|
+
}, z.core.$strip>>;
|
|
2039
|
+
}, z.core.$strip>;
|
|
2040
|
+
/** MCP 配置类型 */
|
|
2041
|
+
type MCPConfig = z.infer<typeof MCPConfigSchema>;
|
|
2042
|
+
/**
|
|
2043
|
+
* Embedding 配置 Schema
|
|
2044
|
+
*
|
|
2045
|
+
* 配置文本向量化参数。
|
|
2046
|
+
* 模型通过 LLMConfigSchema.scenarios.embedding 解析,
|
|
2047
|
+
* apiKey / baseUrl 统一使用 LLM 配置。
|
|
2048
|
+
*
|
|
2049
|
+
* @example
|
|
2050
|
+
* ```ts
|
|
2051
|
+
* const embeddingConfig = {
|
|
2052
|
+
* dimensions: 1536,
|
|
2053
|
+
* batchSize: 100,
|
|
2054
|
+
* }
|
|
2055
|
+
* ```
|
|
2056
|
+
*/
|
|
2057
|
+
declare const EmbeddingConfigSchema: z.ZodObject<{
|
|
2058
|
+
dimensions: z.ZodOptional<z.ZodNumber>;
|
|
2059
|
+
batchSize: z.ZodDefault<z.ZodNumber>;
|
|
2060
|
+
}, z.core.$strip>;
|
|
2061
|
+
/** Embedding 配置类型 */
|
|
2062
|
+
type EmbeddingConfig = z.infer<typeof EmbeddingConfigSchema>;
|
|
2063
|
+
|
|
2064
|
+
/**
|
|
2065
|
+
* Knowledge 配置 Schema
|
|
2066
|
+
*
|
|
2067
|
+
* 配置知识库管理参数:向量集合、分块策略、实体提取等。
|
|
2068
|
+
* 模型通过 LLMConfigSchema.scenarios 解析,
|
|
2069
|
+
* apiKey / baseUrl 统一使用 LLM 配置。
|
|
2070
|
+
*
|
|
2071
|
+
* @example
|
|
2072
|
+
* ```ts
|
|
2073
|
+
* const knowledgeConfig = {
|
|
2074
|
+
* collection: 'my-knowledge',
|
|
2075
|
+
* dimension: 1536,
|
|
2076
|
+
* enableEntityExtraction: true
|
|
2077
|
+
* }
|
|
2078
|
+
* ```
|
|
2079
|
+
*/
|
|
2080
|
+
declare const KnowledgeConfigSchema: z.ZodObject<{
|
|
2081
|
+
collection: z.ZodDefault<z.ZodString>;
|
|
2082
|
+
dimension: z.ZodDefault<z.ZodNumber>;
|
|
2083
|
+
enableEntityExtraction: z.ZodDefault<z.ZodBoolean>;
|
|
2084
|
+
cleanOptions: z.ZodDefault<z.ZodObject<{
|
|
2085
|
+
removeHtml: z.ZodDefault<z.ZodBoolean>;
|
|
2086
|
+
removeUrls: z.ZodDefault<z.ZodBoolean>;
|
|
2087
|
+
removeEmails: z.ZodDefault<z.ZodBoolean>;
|
|
2088
|
+
normalizeWhitespace: z.ZodDefault<z.ZodBoolean>;
|
|
2089
|
+
trim: z.ZodDefault<z.ZodBoolean>;
|
|
2090
|
+
customReplacements: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
2091
|
+
pattern: z.ZodString;
|
|
2092
|
+
replacement: z.ZodString;
|
|
2093
|
+
}, z.core.$strip>>>;
|
|
2094
|
+
}, z.core.$strip>>;
|
|
2095
|
+
chunkOptions: z.ZodDefault<z.ZodObject<{
|
|
2096
|
+
mode: z.ZodEnum<{
|
|
2097
|
+
custom: "custom";
|
|
2098
|
+
sentence: "sentence";
|
|
2099
|
+
paragraph: "paragraph";
|
|
2100
|
+
markdown: "markdown";
|
|
2101
|
+
page: "page";
|
|
2102
|
+
word: "word";
|
|
2103
|
+
character: "character";
|
|
2104
|
+
}>;
|
|
2105
|
+
maxSize: z.ZodDefault<z.ZodNumber>;
|
|
2106
|
+
overlap: z.ZodDefault<z.ZodNumber>;
|
|
2107
|
+
separator: z.ZodOptional<z.ZodString>;
|
|
2108
|
+
markdownMinLevel: z.ZodDefault<z.ZodNumber>;
|
|
2109
|
+
markdownKeepTitle: z.ZodDefault<z.ZodBoolean>;
|
|
2110
|
+
}, z.core.$strip>>;
|
|
2111
|
+
entityBoostWeight: z.ZodDefault<z.ZodNumber>;
|
|
2112
|
+
entityTypes: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
2113
|
+
systemPrompt: z.ZodOptional<z.ZodString>;
|
|
2114
|
+
}, z.core.$strip>;
|
|
2115
|
+
/** Knowledge 配置类型 */
|
|
2116
|
+
type KnowledgeConfig = z.infer<typeof KnowledgeConfigSchema>;
|
|
2117
|
+
|
|
2118
|
+
/**
|
|
2119
|
+
* Memory 配置 Schema
|
|
2120
|
+
*
|
|
2121
|
+
* 配置对话记忆的提取、存储与检索参数。
|
|
2122
|
+
* 模型通过 LLMConfigSchema.scenarios.extraction 解析,
|
|
2123
|
+
* apiKey / baseUrl 统一使用 LLM 配置。
|
|
2124
|
+
*
|
|
2125
|
+
* @example
|
|
2126
|
+
* ```ts
|
|
2127
|
+
* const memoryConfig = {
|
|
2128
|
+
* maxEntries: 1000,
|
|
2129
|
+
* embeddingEnabled: true,
|
|
2130
|
+
* recencyDecay: 0.95,
|
|
2131
|
+
* defaultTopK: 10,
|
|
2132
|
+
* }
|
|
2133
|
+
* ```
|
|
2134
|
+
*/
|
|
2135
|
+
declare const MemoryConfigSchema: z.ZodObject<{
|
|
2136
|
+
maxEntries: z.ZodDefault<z.ZodNumber>;
|
|
2137
|
+
systemPrompt: z.ZodOptional<z.ZodString>;
|
|
2138
|
+
recencyDecay: z.ZodDefault<z.ZodNumber>;
|
|
2139
|
+
embeddingEnabled: z.ZodDefault<z.ZodBoolean>;
|
|
2140
|
+
defaultTopK: z.ZodDefault<z.ZodNumber>;
|
|
2141
|
+
}, z.core.$strip>;
|
|
2142
|
+
/** Memory 配置类型 */
|
|
2143
|
+
type MemoryConfig = z.infer<typeof MemoryConfigSchema>;
|
|
2144
|
+
|
|
2145
|
+
/**
|
|
2146
|
+
* Token 配置 Schema
|
|
2147
|
+
*
|
|
2148
|
+
* 配置 Token 估算参数。
|
|
2149
|
+
*
|
|
2150
|
+
* @example
|
|
2151
|
+
* ```ts
|
|
2152
|
+
* const tokenConfig = { tokenRatio: 0.25 }
|
|
2153
|
+
* ```
|
|
2154
|
+
*/
|
|
2155
|
+
declare const TokenConfigSchema: z.ZodObject<{
|
|
2156
|
+
tokenRatio: z.ZodDefault<z.ZodNumber>;
|
|
2157
|
+
}, z.core.$strip>;
|
|
2158
|
+
/** Token 配置类型 */
|
|
2159
|
+
type TokenConfig = z.infer<typeof TokenConfigSchema>;
|
|
2160
|
+
/**
|
|
2161
|
+
* Summary 配置 Schema
|
|
2162
|
+
*
|
|
2163
|
+
* 配置摘要生成参数。
|
|
2164
|
+
* 模型通过 LLMConfigSchema.scenarios.summary 解析,
|
|
2165
|
+
* apiKey / baseUrl 统一使用 LLM 配置。
|
|
2166
|
+
*
|
|
2167
|
+
* @example
|
|
2168
|
+
* ```ts
|
|
2169
|
+
* const summaryConfig = { systemPrompt: 'You are a summarizer.' }
|
|
2170
|
+
* ```
|
|
2171
|
+
*/
|
|
2172
|
+
declare const SummaryConfigSchema: z.ZodObject<{
|
|
2173
|
+
systemPrompt: z.ZodOptional<z.ZodString>;
|
|
2174
|
+
}, z.core.$strip>;
|
|
2175
|
+
/** Summary 配置类型 */
|
|
2176
|
+
type SummaryConfig = z.infer<typeof SummaryConfigSchema>;
|
|
2177
|
+
/**
|
|
2178
|
+
* Compress 配置 Schema
|
|
2179
|
+
*
|
|
2180
|
+
* 配置上下文压缩参数:压缩策略、Token 预算、保留消息数。
|
|
2181
|
+
*
|
|
2182
|
+
* @example
|
|
2183
|
+
* ```ts
|
|
2184
|
+
* const compressConfig = {
|
|
2185
|
+
* defaultStrategy: 'hybrid',
|
|
2186
|
+
* defaultMaxTokens: 4000,
|
|
2187
|
+
* preserveLastN: 4,
|
|
2188
|
+
* }
|
|
2189
|
+
* ```
|
|
2190
|
+
*/
|
|
2191
|
+
declare const CompressConfigSchema: z.ZodObject<{
|
|
2192
|
+
defaultStrategy: z.ZodDefault<z.ZodEnum<{
|
|
2193
|
+
summary: "summary";
|
|
2194
|
+
"sliding-window": "sliding-window";
|
|
2195
|
+
hybrid: "hybrid";
|
|
2196
|
+
}>>;
|
|
2197
|
+
defaultMaxTokens: z.ZodDefault<z.ZodNumber>;
|
|
2198
|
+
preserveLastN: z.ZodDefault<z.ZodNumber>;
|
|
2199
|
+
}, z.core.$strip>;
|
|
2200
|
+
/** Compress 配置类型 */
|
|
2201
|
+
type CompressConfig = z.infer<typeof CompressConfigSchema>;
|
|
2202
|
+
/**
|
|
2203
|
+
* File 配置 Schema
|
|
2204
|
+
*
|
|
2205
|
+
* 配置文件解析参数:OCR 提示词。
|
|
2206
|
+
* OCR 使用的视觉模型通过 `llm.scenarios.ocr` 指定。
|
|
2207
|
+
*/
|
|
2208
|
+
declare const FileConfigSchema: z.ZodObject<{
|
|
2209
|
+
systemPrompt: z.ZodOptional<z.ZodString>;
|
|
2210
|
+
}, z.core.$strip>;
|
|
2211
|
+
/** File 配置类型 */
|
|
2212
|
+
type FileConfig = z.infer<typeof FileConfigSchema>;
|
|
2213
|
+
/**
|
|
2214
|
+
* 检索源配置 Schema
|
|
2215
|
+
*
|
|
2216
|
+
* 与 `RetrievalSource` 接口字段对齐,支持在 `ai.init()` 中预注册检索源。
|
|
2217
|
+
*/
|
|
2218
|
+
declare const RetrievalSourceSchema: z.ZodObject<{
|
|
2219
|
+
id: z.ZodString;
|
|
2220
|
+
collection: z.ZodString;
|
|
2221
|
+
name: z.ZodOptional<z.ZodString>;
|
|
2222
|
+
url: z.ZodOptional<z.ZodString>;
|
|
2223
|
+
topK: z.ZodOptional<z.ZodNumber>;
|
|
2224
|
+
minScore: z.ZodOptional<z.ZodNumber>;
|
|
2225
|
+
filter: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
2226
|
+
}, z.core.$strip>;
|
|
2227
|
+
/** 检索源配置类型 */
|
|
2228
|
+
type RetrievalSourceConfig = z.infer<typeof RetrievalSourceSchema>;
|
|
2229
|
+
/**
|
|
2230
|
+
* Retrieval 配置 Schema
|
|
2231
|
+
*
|
|
2232
|
+
* 在 `ai.init()` 时预注册检索源,等价于初始化后逐条调用 `ai.retrieval.addSource()`。
|
|
2233
|
+
*
|
|
2234
|
+
* @example
|
|
2235
|
+
* ```ts
|
|
2236
|
+
* ai.init({
|
|
2237
|
+
* llm: { apiKey: 'sk-xxx', model: 'gpt-4o-mini' },
|
|
2238
|
+
* retrieval: {
|
|
2239
|
+
* sources: [
|
|
2240
|
+
* { id: 'docs', collection: 'documentation', name: '产品文档', topK: 5, minScore: 0.7 },
|
|
2241
|
+
* { id: 'faq', collection: 'faq', name: '常见问题' },
|
|
2242
|
+
* ],
|
|
2243
|
+
* },
|
|
2244
|
+
* })
|
|
2245
|
+
* ```
|
|
2246
|
+
*/
|
|
2247
|
+
declare const RetrievalConfigSchema: z.ZodObject<{
|
|
2248
|
+
sources: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
2249
|
+
id: z.ZodString;
|
|
2250
|
+
collection: z.ZodString;
|
|
2251
|
+
name: z.ZodOptional<z.ZodString>;
|
|
2252
|
+
url: z.ZodOptional<z.ZodString>;
|
|
2253
|
+
topK: z.ZodOptional<z.ZodNumber>;
|
|
2254
|
+
minScore: z.ZodOptional<z.ZodNumber>;
|
|
2255
|
+
filter: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
2256
|
+
}, z.core.$strip>>>;
|
|
2257
|
+
}, z.core.$strip>;
|
|
2258
|
+
/** Retrieval 配置类型 */
|
|
2259
|
+
type RetrievalConfig = z.infer<typeof RetrievalConfigSchema>;
|
|
2260
|
+
/** A2A Agent Skill 配置 Schema */
|
|
2261
|
+
declare const A2ASkillConfigSchema: z.ZodObject<{
|
|
2262
|
+
id: z.ZodString;
|
|
2263
|
+
name: z.ZodString;
|
|
2264
|
+
description: z.ZodOptional<z.ZodString>;
|
|
2265
|
+
tags: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
2266
|
+
}, z.core.$strip>;
|
|
2267
|
+
/**
|
|
2268
|
+
* A2A 配置 Schema
|
|
2269
|
+
*
|
|
2270
|
+
* 配置 Agent-to-Agent 协议参数:Agent Card、认证等。
|
|
2271
|
+
*
|
|
2272
|
+
* @example
|
|
2273
|
+
* ```ts
|
|
2274
|
+
* ai.init({
|
|
2275
|
+
* llm: { apiKey: 'sk-xxx', model: 'gpt-4o-mini' },
|
|
2276
|
+
* a2a: {
|
|
2277
|
+
* agentCard: {
|
|
2278
|
+
* name: 'my-agent',
|
|
2279
|
+
* description: 'An example agent',
|
|
2280
|
+
* url: 'https://example.com',
|
|
2281
|
+
* skills: [{ id: 'chat', name: 'General Chat' }],
|
|
2282
|
+
* },
|
|
2283
|
+
* },
|
|
2284
|
+
* })
|
|
2285
|
+
* ```
|
|
2286
|
+
*/
|
|
2287
|
+
declare const A2AConfigSchema: z.ZodObject<{
|
|
2288
|
+
agentCard: z.ZodObject<{
|
|
2289
|
+
name: z.ZodString;
|
|
2290
|
+
description: z.ZodOptional<z.ZodString>;
|
|
2291
|
+
url: z.ZodString;
|
|
2292
|
+
version: z.ZodOptional<z.ZodString>;
|
|
2293
|
+
skills: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
2294
|
+
id: z.ZodString;
|
|
2295
|
+
name: z.ZodString;
|
|
2296
|
+
description: z.ZodOptional<z.ZodString>;
|
|
2297
|
+
tags: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
2298
|
+
}, z.core.$strip>>>;
|
|
2299
|
+
}, z.core.$strip>;
|
|
2300
|
+
security: z.ZodOptional<z.ZodObject<{
|
|
2301
|
+
apiKey: z.ZodOptional<z.ZodObject<{
|
|
2302
|
+
in: z.ZodDefault<z.ZodEnum<{
|
|
2303
|
+
header: "header";
|
|
2304
|
+
query: "query";
|
|
2305
|
+
}>>;
|
|
2306
|
+
name: z.ZodDefault<z.ZodString>;
|
|
2307
|
+
}, z.core.$strip>>;
|
|
2308
|
+
}, z.core.$strip>>;
|
|
2309
|
+
}, z.core.$strip>;
|
|
2310
|
+
/** A2A 配置类型 */
|
|
2311
|
+
type A2AConfig = z.infer<typeof A2AConfigSchema>;
|
|
2312
|
+
/**
|
|
2313
|
+
* AI 配置 Schema
|
|
2314
|
+
*
|
|
2315
|
+
* 统一 AI 模块配置:LLM、MCP、Embedding、Knowledge、Retrieval、Memory、Token、Summary、Compress、File。
|
|
2316
|
+
* 模型通过 LLM.scenarios 映射场景,子系统不再独立配置 apiKey / baseUrl / model。
|
|
2317
|
+
*
|
|
2318
|
+
* @example
|
|
2319
|
+
* ```ts
|
|
2320
|
+
* ai.init({
|
|
2321
|
+
* llm: {
|
|
2322
|
+
* apiKey: 'sk-xxx',
|
|
2323
|
+
* model: 'gpt-4o-mini',
|
|
2324
|
+
* maxTokens: 4096,
|
|
2325
|
+
* models: [
|
|
2326
|
+
* { id: 'rerank', model: 'rerank-english-v3.0', baseUrl: 'https://api.cohere.com' },
|
|
2327
|
+
* ],
|
|
2328
|
+
* scenarios: {
|
|
2329
|
+
* extraction: 'gpt-4o',
|
|
2330
|
+
* summary: 'gpt-4o-mini',
|
|
2331
|
+
* embedding: 'text-embedding-3-small',
|
|
2332
|
+
* rerank: 'rerank',
|
|
2333
|
+
* ocr: 'gpt-4o',
|
|
2334
|
+
* },
|
|
2335
|
+
* },
|
|
2336
|
+
* embedding: { dimensions: 1536 },
|
|
2337
|
+
* knowledge: {
|
|
2338
|
+
* collection: 'docs',
|
|
2339
|
+
* enableEntityExtraction: true,
|
|
2340
|
+
* cleanOptions: { removeHtml: true },
|
|
2341
|
+
* chunkOptions: { mode: 'markdown', maxSize: 1500, overlap: 200 },
|
|
2342
|
+
* },
|
|
2343
|
+
* retrieval: {
|
|
2344
|
+
* sources: [
|
|
2345
|
+
* { id: 'docs', collection: 'documentation', name: '产品文档', topK: 5, minScore: 0.7 },
|
|
2346
|
+
* ],
|
|
2347
|
+
* },
|
|
2348
|
+
* memory: { maxEntries: 500, embeddingEnabled: true },
|
|
2349
|
+
* token: { tokenRatio: 0.25 },
|
|
2350
|
+
* summary: { systemPrompt: 'You are a summarizer.' },
|
|
2351
|
+
* compress: { defaultStrategy: 'hybrid', preserveLastN: 4 },
|
|
2352
|
+
* })
|
|
2353
|
+
* ```
|
|
2354
|
+
*/
|
|
2355
|
+
declare const AIConfigSchema: z.ZodObject<{
|
|
2356
|
+
llm: z.ZodDefault<z.ZodObject<{
|
|
2357
|
+
apiKey: z.ZodOptional<z.ZodString>;
|
|
2358
|
+
baseUrl: z.ZodOptional<z.ZodURL>;
|
|
2359
|
+
model: z.ZodDefault<z.ZodOptional<z.ZodString>>;
|
|
2360
|
+
maxTokens: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
|
|
2361
|
+
temperature: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
|
|
2362
|
+
timeout: z.ZodDefault<z.ZodOptional<z.ZodNumber>>;
|
|
2363
|
+
models: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
2364
|
+
id: z.ZodString;
|
|
2365
|
+
model: z.ZodString;
|
|
2366
|
+
apiKey: z.ZodOptional<z.ZodString>;
|
|
2367
|
+
baseUrl: z.ZodOptional<z.ZodURL>;
|
|
2368
|
+
maxTokens: z.ZodOptional<z.ZodNumber>;
|
|
2369
|
+
temperature: z.ZodOptional<z.ZodNumber>;
|
|
2370
|
+
timeout: z.ZodOptional<z.ZodNumber>;
|
|
2371
|
+
}, z.core.$strip>>>;
|
|
2372
|
+
scenarios: z.ZodOptional<z.ZodObject<{
|
|
2373
|
+
default: z.ZodOptional<z.ZodString>;
|
|
2374
|
+
chat: z.ZodOptional<z.ZodString>;
|
|
2375
|
+
reasoning: z.ZodOptional<z.ZodString>;
|
|
2376
|
+
plan: z.ZodOptional<z.ZodString>;
|
|
2377
|
+
execute: z.ZodOptional<z.ZodString>;
|
|
2378
|
+
extraction: z.ZodOptional<z.ZodString>;
|
|
2379
|
+
summary: z.ZodOptional<z.ZodString>;
|
|
2380
|
+
embedding: z.ZodOptional<z.ZodString>;
|
|
2381
|
+
rerank: z.ZodOptional<z.ZodString>;
|
|
2382
|
+
ocr: z.ZodOptional<z.ZodString>;
|
|
2383
|
+
fast: z.ZodOptional<z.ZodString>;
|
|
2384
|
+
}, z.core.$strip>>;
|
|
2385
|
+
}, z.core.$strip>>;
|
|
2386
|
+
mcp: z.ZodOptional<z.ZodObject<{
|
|
2387
|
+
server: z.ZodOptional<z.ZodObject<{
|
|
2388
|
+
name: z.ZodString;
|
|
2389
|
+
version: z.ZodDefault<z.ZodOptional<z.ZodString>>;
|
|
2390
|
+
capabilities: z.ZodOptional<z.ZodObject<{
|
|
2391
|
+
tools: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
|
|
2392
|
+
resources: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
|
|
2393
|
+
prompts: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
|
|
2394
|
+
}, z.core.$strip>>;
|
|
2395
|
+
}, z.core.$strip>>;
|
|
2396
|
+
}, z.core.$strip>>;
|
|
2397
|
+
embedding: z.ZodOptional<z.ZodObject<{
|
|
2398
|
+
dimensions: z.ZodOptional<z.ZodNumber>;
|
|
2399
|
+
batchSize: z.ZodDefault<z.ZodNumber>;
|
|
2400
|
+
}, z.core.$strip>>;
|
|
2401
|
+
knowledge: z.ZodOptional<z.ZodObject<{
|
|
2402
|
+
collection: z.ZodDefault<z.ZodString>;
|
|
2403
|
+
dimension: z.ZodDefault<z.ZodNumber>;
|
|
2404
|
+
enableEntityExtraction: z.ZodDefault<z.ZodBoolean>;
|
|
2405
|
+
cleanOptions: z.ZodDefault<z.ZodObject<{
|
|
2406
|
+
removeHtml: z.ZodDefault<z.ZodBoolean>;
|
|
2407
|
+
removeUrls: z.ZodDefault<z.ZodBoolean>;
|
|
2408
|
+
removeEmails: z.ZodDefault<z.ZodBoolean>;
|
|
2409
|
+
normalizeWhitespace: z.ZodDefault<z.ZodBoolean>;
|
|
2410
|
+
trim: z.ZodDefault<z.ZodBoolean>;
|
|
2411
|
+
customReplacements: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
2412
|
+
pattern: z.ZodString;
|
|
2413
|
+
replacement: z.ZodString;
|
|
2414
|
+
}, z.core.$strip>>>;
|
|
2415
|
+
}, z.core.$strip>>;
|
|
2416
|
+
chunkOptions: z.ZodDefault<z.ZodObject<{
|
|
2417
|
+
mode: z.ZodEnum<{
|
|
2418
|
+
custom: "custom";
|
|
2419
|
+
sentence: "sentence";
|
|
2420
|
+
paragraph: "paragraph";
|
|
2421
|
+
markdown: "markdown";
|
|
2422
|
+
page: "page";
|
|
2423
|
+
word: "word";
|
|
2424
|
+
character: "character";
|
|
2425
|
+
}>;
|
|
2426
|
+
maxSize: z.ZodDefault<z.ZodNumber>;
|
|
2427
|
+
overlap: z.ZodDefault<z.ZodNumber>;
|
|
2428
|
+
separator: z.ZodOptional<z.ZodString>;
|
|
2429
|
+
markdownMinLevel: z.ZodDefault<z.ZodNumber>;
|
|
2430
|
+
markdownKeepTitle: z.ZodDefault<z.ZodBoolean>;
|
|
2431
|
+
}, z.core.$strip>>;
|
|
2432
|
+
entityBoostWeight: z.ZodDefault<z.ZodNumber>;
|
|
2433
|
+
entityTypes: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
2434
|
+
systemPrompt: z.ZodOptional<z.ZodString>;
|
|
2435
|
+
}, z.core.$strip>>;
|
|
2436
|
+
memory: z.ZodOptional<z.ZodObject<{
|
|
2437
|
+
maxEntries: z.ZodDefault<z.ZodNumber>;
|
|
2438
|
+
systemPrompt: z.ZodOptional<z.ZodString>;
|
|
2439
|
+
recencyDecay: z.ZodDefault<z.ZodNumber>;
|
|
2440
|
+
embeddingEnabled: z.ZodDefault<z.ZodBoolean>;
|
|
2441
|
+
defaultTopK: z.ZodDefault<z.ZodNumber>;
|
|
2442
|
+
}, z.core.$strip>>;
|
|
2443
|
+
token: z.ZodOptional<z.ZodObject<{
|
|
2444
|
+
tokenRatio: z.ZodDefault<z.ZodNumber>;
|
|
2445
|
+
}, z.core.$strip>>;
|
|
2446
|
+
summary: z.ZodOptional<z.ZodObject<{
|
|
2447
|
+
systemPrompt: z.ZodOptional<z.ZodString>;
|
|
2448
|
+
}, z.core.$strip>>;
|
|
2449
|
+
compress: z.ZodOptional<z.ZodObject<{
|
|
2450
|
+
defaultStrategy: z.ZodDefault<z.ZodEnum<{
|
|
2451
|
+
summary: "summary";
|
|
2452
|
+
"sliding-window": "sliding-window";
|
|
2453
|
+
hybrid: "hybrid";
|
|
2454
|
+
}>>;
|
|
2455
|
+
defaultMaxTokens: z.ZodDefault<z.ZodNumber>;
|
|
2456
|
+
preserveLastN: z.ZodDefault<z.ZodNumber>;
|
|
2457
|
+
}, z.core.$strip>>;
|
|
2458
|
+
retrieval: z.ZodOptional<z.ZodObject<{
|
|
2459
|
+
sources: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
2460
|
+
id: z.ZodString;
|
|
2461
|
+
collection: z.ZodString;
|
|
2462
|
+
name: z.ZodOptional<z.ZodString>;
|
|
2463
|
+
url: z.ZodOptional<z.ZodString>;
|
|
2464
|
+
topK: z.ZodOptional<z.ZodNumber>;
|
|
2465
|
+
minScore: z.ZodOptional<z.ZodNumber>;
|
|
2466
|
+
filter: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
2467
|
+
}, z.core.$strip>>>;
|
|
2468
|
+
}, z.core.$strip>>;
|
|
2469
|
+
file: z.ZodOptional<z.ZodObject<{
|
|
2470
|
+
systemPrompt: z.ZodOptional<z.ZodString>;
|
|
2471
|
+
}, z.core.$strip>>;
|
|
2472
|
+
a2a: z.ZodOptional<z.ZodObject<{
|
|
2473
|
+
agentCard: z.ZodObject<{
|
|
2474
|
+
name: z.ZodString;
|
|
2475
|
+
description: z.ZodOptional<z.ZodString>;
|
|
2476
|
+
url: z.ZodString;
|
|
2477
|
+
version: z.ZodOptional<z.ZodString>;
|
|
2478
|
+
skills: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
2479
|
+
id: z.ZodString;
|
|
2480
|
+
name: z.ZodString;
|
|
2481
|
+
description: z.ZodOptional<z.ZodString>;
|
|
2482
|
+
tags: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
2483
|
+
}, z.core.$strip>>>;
|
|
2484
|
+
}, z.core.$strip>;
|
|
2485
|
+
security: z.ZodOptional<z.ZodObject<{
|
|
2486
|
+
apiKey: z.ZodOptional<z.ZodObject<{
|
|
2487
|
+
in: z.ZodDefault<z.ZodEnum<{
|
|
2488
|
+
header: "header";
|
|
2489
|
+
query: "query";
|
|
2490
|
+
}>>;
|
|
2491
|
+
name: z.ZodDefault<z.ZodString>;
|
|
2492
|
+
}, z.core.$strip>>;
|
|
2493
|
+
}, z.core.$strip>>;
|
|
2494
|
+
}, z.core.$strip>>;
|
|
2495
|
+
}, z.core.$strip>;
|
|
2496
|
+
/** AI 配置类型(校验后的完整类型) */
|
|
2497
|
+
type AIConfig = z.infer<typeof AIConfigSchema>;
|
|
2498
|
+
/** AI 配置输入类型(允许部分字段) */
|
|
2499
|
+
type AIConfigInput = z.input<typeof AIConfigSchema>;
|
|
2500
|
+
|
|
2501
|
+
/**
|
|
2502
|
+
* @h-ai/ai — RAG(Retrieval-Augmented Generation)子功能类型
|
|
2503
|
+
*
|
|
2504
|
+
* 定义 RAG 操作的类型接口:检索 + 生成。
|
|
2505
|
+
* @module ai-rag-types
|
|
2506
|
+
*/
|
|
2507
|
+
|
|
2508
|
+
/**
|
|
2509
|
+
* RAG 选项
|
|
2510
|
+
*/
|
|
2511
|
+
interface RagOptions {
|
|
2512
|
+
/** 交互主体 ID */
|
|
2513
|
+
objectId?: string;
|
|
2514
|
+
/** 会话 ID */
|
|
2515
|
+
sessionId?: string;
|
|
2516
|
+
/** 使用的检索源(不指定则使用全部已注册源) */
|
|
2517
|
+
sources?: string[];
|
|
2518
|
+
/** 返回的上下文条数(默认 5) */
|
|
2519
|
+
topK?: number;
|
|
2520
|
+
/** 最低相似度 */
|
|
2521
|
+
minScore?: number;
|
|
2522
|
+
/** 是否启用 Rerank 重排序 */
|
|
2523
|
+
enableRerank?: boolean;
|
|
2524
|
+
/** Rerank 使用的模型名称覆盖 */
|
|
2525
|
+
rerankModel?: string;
|
|
2526
|
+
/** LLM 模型名称覆盖 */
|
|
2527
|
+
model?: string;
|
|
2528
|
+
/** 系统提示词(可选,会自动注入检索上下文) */
|
|
2529
|
+
systemPrompt?: string;
|
|
2530
|
+
/** 温度覆盖 */
|
|
2531
|
+
temperature?: number;
|
|
2532
|
+
/** 自定义上下文格式化函数 */
|
|
2533
|
+
formatContext?: (items: RagContextItem[]) => string;
|
|
2534
|
+
/** 是否保留消息历史(用于多轮对话) */
|
|
2535
|
+
messages?: ChatMessage[];
|
|
2536
|
+
/** 是否启用内部 LLM 调用的持久化(默认 true;Context 层调用时传 false 避免重复记录) */
|
|
2537
|
+
enablePersist?: boolean;
|
|
2538
|
+
}
|
|
2539
|
+
/**
|
|
2540
|
+
* RAG 上下文项
|
|
2541
|
+
*/
|
|
2542
|
+
interface RagContextItem {
|
|
2543
|
+
/** 文档内容 */
|
|
2544
|
+
content: string;
|
|
2545
|
+
/** 相似度分数 */
|
|
2546
|
+
score: number;
|
|
2547
|
+
/** 来源 */
|
|
2548
|
+
sourceId: string;
|
|
2549
|
+
/** 元数据 */
|
|
2550
|
+
metadata?: Record<string, unknown>;
|
|
2551
|
+
/** 结构化信源引用 */
|
|
2552
|
+
citation?: Citation;
|
|
2553
|
+
}
|
|
2554
|
+
/**
|
|
2555
|
+
* RAG 结果
|
|
2556
|
+
*/
|
|
2557
|
+
interface RagResult {
|
|
2558
|
+
/** LLM 生成的回答 */
|
|
2559
|
+
answer: string;
|
|
2560
|
+
/** 使用的上下文 */
|
|
2561
|
+
context: RagContextItem[];
|
|
2562
|
+
/** 去重后的信源引用列表(方便 UI 展示引用栏) */
|
|
2563
|
+
citations: Citation[];
|
|
2564
|
+
/** 查询文本 */
|
|
2565
|
+
query: string;
|
|
2566
|
+
/** 使用的模型 */
|
|
2567
|
+
model: string;
|
|
2568
|
+
/** Token 使用统计 */
|
|
2569
|
+
usage?: {
|
|
2570
|
+
prompt_tokens: number;
|
|
2571
|
+
completion_tokens: number;
|
|
2572
|
+
total_tokens: number;
|
|
2573
|
+
};
|
|
2574
|
+
}
|
|
2575
|
+
/**
|
|
2576
|
+
* RAG 流式事件
|
|
2577
|
+
*
|
|
2578
|
+
* queryStream 产出的事件序列:
|
|
2579
|
+
* 1. `context` — 检索结果就绪,携带上下文列表
|
|
2580
|
+
* 2. `delta` — LLM 增量文本
|
|
2581
|
+
* 3. `done` — 生成完成,携带完整结果汇总
|
|
2582
|
+
*/
|
|
2583
|
+
type RagStreamEvent = {
|
|
2584
|
+
type: 'context';
|
|
2585
|
+
items: RagContextItem[];
|
|
2586
|
+
citations: Citation[];
|
|
2587
|
+
} | {
|
|
2588
|
+
type: 'delta';
|
|
2589
|
+
text: string;
|
|
2590
|
+
} | {
|
|
2591
|
+
type: 'done';
|
|
2592
|
+
answer: string;
|
|
2593
|
+
model: string;
|
|
2594
|
+
usage?: {
|
|
2595
|
+
prompt_tokens: number;
|
|
2596
|
+
completion_tokens: number;
|
|
2597
|
+
total_tokens: number;
|
|
2598
|
+
};
|
|
2599
|
+
};
|
|
2600
|
+
/**
|
|
2601
|
+
* RAG 操作接口
|
|
2602
|
+
*/
|
|
2603
|
+
interface RagOperations {
|
|
2604
|
+
/**
|
|
2605
|
+
* 执行 RAG(检索增强生成)
|
|
2606
|
+
*
|
|
2607
|
+
* 1. 将查询文本向量化
|
|
2608
|
+
* 2. 从已注册的检索源中检索相关文档
|
|
2609
|
+
* 3. 将检索上下文注入 LLM 提示词
|
|
2610
|
+
* 4. 调用 LLM 生成回答
|
|
2611
|
+
*
|
|
2612
|
+
* @param query - 用户查询文本
|
|
2613
|
+
* @param options - RAG 选项
|
|
2614
|
+
* @returns RAG 结果
|
|
2615
|
+
*/
|
|
2616
|
+
query: (query: string, options?: RagOptions) => Promise<HaiResult<RagResult>>;
|
|
2617
|
+
/**
|
|
2618
|
+
* 流式 RAG 查询
|
|
2619
|
+
*
|
|
2620
|
+
* 产出事件序列:context → delta* → done
|
|
2621
|
+
*
|
|
2622
|
+
* @param query - 用户查询文本
|
|
2623
|
+
* @param options - RAG 选项
|
|
2624
|
+
* @returns 异步可迭代的 RagStreamEvent
|
|
2625
|
+
*/
|
|
2626
|
+
queryStream: (query: string, options?: RagOptions) => AsyncIterable<RagStreamEvent>;
|
|
2627
|
+
}
|
|
2628
|
+
|
|
2629
|
+
/**
|
|
2630
|
+
* @h-ai/ai — Reasoning 子功能类型
|
|
2631
|
+
*
|
|
2632
|
+
* 定义推理策略和推理操作的类型接口。
|
|
2633
|
+
* 支持 ReAct、Chain-of-Thought(CoT)、Plan-and-Execute 三种策略。
|
|
2634
|
+
* @module ai-reasoning-types
|
|
2635
|
+
*/
|
|
2636
|
+
|
|
2637
|
+
/**
|
|
2638
|
+
* 推理策略类型
|
|
2639
|
+
*
|
|
2640
|
+
* - `react` — ReAct(Reasoning + Acting):交替 思考→行动→观察 循环
|
|
2641
|
+
* - `cot` — Chain-of-Thought:将问题分解为思维链逐步推理
|
|
2642
|
+
* - `plan-execute` — Plan-and-Execute:先生成计划再逐步执行
|
|
2643
|
+
*/
|
|
2644
|
+
type ReasoningStrategy = 'react' | 'cot' | 'plan-execute';
|
|
2645
|
+
/**
|
|
2646
|
+
* 推理执行选项
|
|
2647
|
+
*/
|
|
2648
|
+
interface ReasoningOptions {
|
|
2649
|
+
/** 交互主体 ID */
|
|
2650
|
+
objectId?: string;
|
|
2651
|
+
/** 会话 ID */
|
|
2652
|
+
sessionId?: string;
|
|
2653
|
+
/** 推理策略(默认 `'react'`) */
|
|
2654
|
+
strategy?: ReasoningStrategy;
|
|
2655
|
+
/** 最大推理轮次(默认 10) */
|
|
2656
|
+
maxRounds?: number;
|
|
2657
|
+
/** 使用的模型 ID 或名称(可选,默认使用 reasoning 场景的模型) */
|
|
2658
|
+
model?: string;
|
|
2659
|
+
/** Plan-Execute 规划阶段模型(可选,默认回退到 `model` → 场景 `plan` → 场景 `reasoning`) */
|
|
2660
|
+
planModel?: string;
|
|
2661
|
+
/** Plan-Execute 执行阶段模型(可选,默认回退到 `model` → 场景 `execute` → 场景 `reasoning`) */
|
|
2662
|
+
executeModel?: string;
|
|
2663
|
+
/** 系统提示词(可选,覆盖默认的策略提示词) */
|
|
2664
|
+
systemPrompt?: string;
|
|
2665
|
+
/** 是否启用内部 LLM 调用的持久化(默认 true;Context 层调用时传 false 避免重复记录) */
|
|
2666
|
+
enablePersist?: boolean;
|
|
2667
|
+
/**
|
|
2668
|
+
* 前置对话上下文(可选)
|
|
2669
|
+
*
|
|
2670
|
+
* 插入到系统提示词之后、当前问题之前,用于注入:
|
|
2671
|
+
* - 历史对话轮次(连续对话场景)
|
|
2672
|
+
* - `ai.memory.injectMemories()` 处理后的记忆增强消息
|
|
2673
|
+
* - RAG 检索到的内容片段(构造为 system 消息)
|
|
2674
|
+
*
|
|
2675
|
+
* @example
|
|
2676
|
+
* ```ts
|
|
2677
|
+
* // 将记忆注入推理上下文
|
|
2678
|
+
* const withMemory = await ai.memory.injectMemories(
|
|
2679
|
+
* [{ role: 'user', content: query }],
|
|
2680
|
+
* { objectId: 'user-001' },
|
|
2681
|
+
* )
|
|
2682
|
+
* await ai.reasoning.run(query, { messages: withMemory.data })
|
|
2683
|
+
* ```
|
|
2684
|
+
*/
|
|
2685
|
+
messages?: ChatMessage[];
|
|
2686
|
+
/** 可用工具注册表(可选,ReAct 和 Plan-Execute 策略可用) */
|
|
2687
|
+
tools?: ToolRegistryOperations;
|
|
2688
|
+
/** 温度覆盖 */
|
|
2689
|
+
temperature?: number;
|
|
2690
|
+
}
|
|
2691
|
+
/**
|
|
2692
|
+
* 推理步骤类型
|
|
2693
|
+
*/
|
|
2694
|
+
type ReasoningStepType = 'thought' | 'action' | 'observation' | 'plan' | 'answer';
|
|
2695
|
+
/**
|
|
2696
|
+
* 单个推理步骤
|
|
2697
|
+
*/
|
|
2698
|
+
interface ReasoningStep {
|
|
2699
|
+
/** 步骤类型 */
|
|
2700
|
+
type: ReasoningStepType;
|
|
2701
|
+
/** 步骤内容 */
|
|
2702
|
+
content: string;
|
|
2703
|
+
/** 工具调用信息(仅 action 类型) */
|
|
2704
|
+
toolCall?: {
|
|
2705
|
+
name: string;
|
|
2706
|
+
arguments: Record<string, unknown>;
|
|
2707
|
+
result?: string;
|
|
2708
|
+
};
|
|
2709
|
+
/** 步骤索引(从 0 开始) */
|
|
2710
|
+
index: number;
|
|
2711
|
+
}
|
|
2712
|
+
/**
|
|
2713
|
+
* 推理执行结果
|
|
2714
|
+
*/
|
|
2715
|
+
interface ReasoningResult {
|
|
2716
|
+
/** 最终答案 */
|
|
2717
|
+
answer: string;
|
|
2718
|
+
/** 推理步骤列表 */
|
|
2719
|
+
steps: ReasoningStep[];
|
|
2720
|
+
/** 使用的策略 */
|
|
2721
|
+
strategy: ReasoningStrategy;
|
|
2722
|
+
/** 总推理轮次 */
|
|
2723
|
+
rounds: number;
|
|
2724
|
+
/** 完整的消息历史 */
|
|
2725
|
+
messages: ChatMessage[];
|
|
2726
|
+
}
|
|
2727
|
+
/**
|
|
2728
|
+
* 推理流式事件
|
|
2729
|
+
*
|
|
2730
|
+
* runStream 产出的事件序列:
|
|
2731
|
+
* - `step` — 推理步骤完成(思考/行动/观察/计划)
|
|
2732
|
+
* - `delta` — LLM 增量文本(最终答案的流式输出)
|
|
2733
|
+
* - `done` — 推理完成,携带完整结果
|
|
2734
|
+
*/
|
|
2735
|
+
type ReasoningStreamEvent = {
|
|
2736
|
+
type: 'step';
|
|
2737
|
+
step: ReasoningStep;
|
|
2738
|
+
} | {
|
|
2739
|
+
type: 'delta';
|
|
2740
|
+
text: string;
|
|
2741
|
+
} | {
|
|
2742
|
+
type: 'done';
|
|
2743
|
+
result: ReasoningResult;
|
|
2744
|
+
};
|
|
2745
|
+
/**
|
|
2746
|
+
* 推理操作接口(通过 `ai.reasoning` 访问)
|
|
2747
|
+
*
|
|
2748
|
+
* 需要先调用 `ai.init()` 初始化后使用。
|
|
2749
|
+
*
|
|
2750
|
+
* @example
|
|
2751
|
+
* ```ts
|
|
2752
|
+
* // ReAct 推理(带工具)
|
|
2753
|
+
* const result = await ai.reasoning.run(
|
|
2754
|
+
* '分析这份数据...',
|
|
2755
|
+
* {
|
|
2756
|
+
* strategy: 'react',
|
|
2757
|
+
* tools: registry,
|
|
2758
|
+
* maxRounds: 5,
|
|
2759
|
+
* },
|
|
2760
|
+
* )
|
|
2761
|
+
*
|
|
2762
|
+
* // CoT 推理
|
|
2763
|
+
* const result = await ai.reasoning.run(
|
|
2764
|
+
* '解释量子纠缠',
|
|
2765
|
+
* { strategy: 'cot' },
|
|
2766
|
+
* )
|
|
2767
|
+
* ```
|
|
2768
|
+
*/
|
|
2769
|
+
interface ReasoningOperations {
|
|
2770
|
+
/**
|
|
2771
|
+
* 执行推理
|
|
2772
|
+
*
|
|
2773
|
+
* @param query - 用户问题或任务描述
|
|
2774
|
+
* @param options - 推理选项
|
|
2775
|
+
* @returns 推理结果
|
|
2776
|
+
*/
|
|
2777
|
+
run: (query: string, options?: ReasoningOptions) => Promise<HaiResult<ReasoningResult>>;
|
|
2778
|
+
/**
|
|
2779
|
+
* 流式推理(逐步产出推理步骤 + 最终答案增量文本)
|
|
2780
|
+
*
|
|
2781
|
+
* 产出事件序列:step* → delta* → done
|
|
2782
|
+
*
|
|
2783
|
+
* @param query - 用户问题或任务描述
|
|
2784
|
+
* @param options - 推理选项
|
|
2785
|
+
* @returns 异步可迭代的 ReasoningStreamEvent
|
|
2786
|
+
*/
|
|
2787
|
+
runStream: (query: string, options?: ReasoningOptions) => AsyncIterable<ReasoningStreamEvent>;
|
|
2788
|
+
}
|
|
2789
|
+
|
|
2790
|
+
export { type A2AOperations as $, type A2AConfig as A, type RetrievalConfig as B, type CompressConfig as C, RetrievalConfigSchema as D, type EmbeddingConfig as E, type FileConfig as F, type RetrievalSourceConfig as G, RetrievalSourceSchema as H, SummaryConfigSchema as I, TokenConfigSchema as J, type KnowledgeConfig as K, type LLMConfig as L, type MCPConfig as M, resolveModelEntry as N, type A2AAgentCardConfig as O, type A2AApiKeySecurity as P, type A2AAuthenticator as Q, type ResolveRequiredModelEntryOptions as R, type SummaryConfig as S, type TokenConfig as T, type A2ACallOptions as U, type A2ACallResult as V, type A2ACallerIdentity as W, type A2AClientCallRecord as X, type A2AContextInfo as Y, type A2AHandleResult as Z, type A2AMessageRecord as _, A2AConfigSchema as a, type ReasoningOptions as a$, type A2ASecurityConfig as a0, type A2ATaskFilter as a1, type AILLMFunctionsDeps as a2, type AIRelStore as a3, type AIRelStoreOptions as a4, type AIStoreProvider as a5, type AIVectorStore as a6, type AskOptions as a7, type AssistantMessage as a8, type ChatCompletionChoice as a9, type KnowledgeOperations as aA, type KnowledgeRetrieveItem as aB, type KnowledgeRetrieveOptions as aC, type KnowledgeRetrieveResult as aD, type KnowledgeSetupOptions as aE, type KnowledgeStore as aF, type LLMOperations as aG, type LLMProvider as aH, type MemoryClearOptions as aI, type MemoryEntry as aJ, type MemoryEntryInput as aK, type MemoryExtractOptions as aL, type MemoryInjectionOptions as aM, type MemoryListOptions as aN, type MemoryListPageOptions as aO, type MemoryOperations as aP, type MemoryRecallOptions as aQ, type MemoryUpdateInput as aR, type MessageContent as aS, type MessageRole as aT, type ObjectRef as aU, type RagContextItem as aV, type RagOperations as aW, type RagOptions as aX, type RagResult as aY, type RagStreamEvent as aZ, type ReasoningOperations as a_, type ChatCompletionChunk as aa, type ChatCompletionDelta as ab, type ChatCompletionRequest as ac, type ChatCompletionResponse as ad, type ChatHistoryOptions as ae, type ChatMessage as af, type ChatRecord as ag, type Citation as ah, type DefineToolOptions as ai, type EntityDocumentRelation as aj, type EntityDocumentResult as ak, type EntityListOptions as al, type EntityQueryOptions as am, type ImageContent as an, type InteractionScope as ao, type KnowledgeAskOptions as ap, type KnowledgeAskResult as aq, type KnowledgeDocumentInfo as ar, type KnowledgeDocumentListOptions as as, type KnowledgeDocumentRemoveOptions as at, type KnowledgeEntity as au, type KnowledgeIngestBatchProgress as av, type KnowledgeIngestBatchResult as aw, type KnowledgeIngestFileInput as ax, type KnowledgeIngestInput as ay, type KnowledgeIngestResult as az, A2ASkillConfigSchema as b, type ReasoningResult as b0, type ReasoningStep as b1, type ReasoningStepType as b2, type ReasoningStrategy as b3, type ReasoningStreamEvent as b4, type RetrievalOperations as b5, type RetrievalRequest as b6, type RetrievalResult as b7, type RetrievalResultItem as b8, type RetrievalSource as b9, type SSEDecoder as ba, type SSEEvent as bb, type SessionInfo as bc, type StoreFilter as bd, type StorePage as be, type StoreScope as bf, type StreamOperations as bg, type StreamProcessor as bh, type StreamResult as bi, type SystemMessage as bj, type TextContent as bk, type TokenUsage as bl, type Tool as bm, type ToolCall as bn, type ToolDefinition as bo, type ToolErrorType as bp, type ToolMessage as bq, type ToolRegistryOperations as br, type ToolsOperations as bs, type UserMessage as bt, type WhereClause as bu, type WhereOperator as bv, type WhereValue as bw, type AIConfig as c, type AIConfigInput as d, AIConfigSchema as e, CompressConfigSchema as f, EmbeddingConfigSchema as g, type EntityType as h, EntityTypeSchema as i, FileConfigSchema as j, KnowledgeConfigSchema as k, LLMConfigSchema as l, MCPConfigSchema as m, type MCPServerCapabilities as n, MCPServerCapabilitiesSchema as o, type MCPServerConfig as p, MCPServerConfigSchema as q, type MemoryConfig as r, MemoryConfigSchema as s, type MemoryType as t, MemoryTypeSchema as u, type ModelEntry as v, ModelEntrySchema as w, type ModelScenario as x, ModelScenarioSchema as y, type ResolvedModelConfig as z };
|