@h-ai/ai 0.1.0-alpha.67 → 0.1.0-alpha.69
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +104 -35
- package/README.ja.md +104 -35
- package/README.md +128 -36
- package/dist/ai-audio-types-4R5yUAVl.d.ts +614 -0
- package/dist/ai-audio-ws-protocol-nfhO0tOo.d.ts +7605 -0
- package/dist/browser.d.ts +6 -6
- package/dist/browser.js +3 -3
- package/dist/chunk-GMQFIWND.js +254 -0
- package/dist/chunk-GMQFIWND.js.map +1 -0
- package/dist/{chunk-Q7NORWP4.js → chunk-HEVXR252.js} +236 -15
- package/dist/chunk-HEVXR252.js.map +1 -0
- package/dist/{chunk-JC7QBT3C.js → chunk-HYMP57AC.js} +6 -2
- package/dist/chunk-HYMP57AC.js.map +1 -0
- package/dist/client/index.d.ts +38 -274
- package/dist/client/index.js +2 -2
- package/dist/index.d.ts +18 -40
- package/dist/index.js +2179 -1321
- package/dist/index.js.map +1 -1
- package/package.json +20 -8
- package/dist/ai-audio-ws-protocol-S4N8-LBa.d.ts +0 -1739
- package/dist/ai-reasoning-types-DZd_6Umd.d.ts +0 -3936
- package/dist/chunk-7SMQSE4S.js +0 -578
- package/dist/chunk-7SMQSE4S.js.map +0 -1
- package/dist/chunk-JC7QBT3C.js.map +0 -1
- package/dist/chunk-Q7NORWP4.js.map +0 -1
|
@@ -1,1739 +0,0 @@
|
|
|
1
|
-
import * as _h_ai_core from '@h-ai/core';
|
|
2
|
-
import { HaiResult } from '@h-ai/core';
|
|
3
|
-
import { a_ as ChatMessage, az as InteractionScope, Y as MemoryType, bL as RagOptions, bP as ReasoningOptions, cm as ToolRegistryOperations, bx as MemoryEntry, c2 as SessionInfo, bq as LLMOperations, bD as MemoryOperations, bK as RagOperations, bO as ReasoningOperations, d as AIConfigInput, aP as AIStoreProvider, c as AIConfig, co as ToolsOperations, c6 as StreamOperations, bV as RetrievalOperations, bk as KnowledgeOperations, aJ as A2AOperations, s as AudioOperations, k as AudioContent, a3 as PlayableAudio, l as AudioFormat } from './ai-reasoning-types-DZd_6Umd.js';
|
|
4
|
-
import * as zod from 'zod';
|
|
5
|
-
import { z } from 'zod';
|
|
6
|
-
import { Buffer } from 'node:buffer';
|
|
7
|
-
import { PromptMessage } from '@modelcontextprotocol/sdk/types.js';
|
|
8
|
-
import * as zod_v4_core from 'zod/v4/core';
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* @h-ai/ai — Compress 子功能类型
|
|
12
|
-
*
|
|
13
|
-
* 定义上下文压缩操作的类型接口:滑动窗口、摘要、混合三种策略。
|
|
14
|
-
* 在对话超出模型上下文窗口时,自动压缩历史消息以保持对话连续性。
|
|
15
|
-
* @module ai-compress-types
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
/** 压缩策略枚举 */
|
|
19
|
-
declare const CompressionStrategySchema: z.ZodEnum<{
|
|
20
|
-
summary: "summary";
|
|
21
|
-
"sliding-window": "sliding-window";
|
|
22
|
-
hybrid: "hybrid";
|
|
23
|
-
}>;
|
|
24
|
-
/** 压缩策略类型 */
|
|
25
|
-
type CompressionStrategy = z.infer<typeof CompressionStrategySchema>;
|
|
26
|
-
/**
|
|
27
|
-
* 上下文压缩选项
|
|
28
|
-
*/
|
|
29
|
-
interface CompressOptions {
|
|
30
|
-
/** 压缩策略(默认使用配置的 defaultStrategy) */
|
|
31
|
-
strategy?: CompressionStrategy;
|
|
32
|
-
/** 目标 token 数(默认使用配置的 defaultMaxTokens,0 表示取模型 maxTokens 的 80%) */
|
|
33
|
-
maxTokens?: number;
|
|
34
|
-
/** 保留 system 消息(默认 true) */
|
|
35
|
-
preserveSystem?: boolean;
|
|
36
|
-
/** 保留最近 N 条消息不压缩(默认使用配置的 preserveLastN) */
|
|
37
|
-
preserveLastN?: number;
|
|
38
|
-
/** 摘要用的模型 */
|
|
39
|
-
summaryModel?: string;
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* 上下文压缩结果
|
|
43
|
-
*/
|
|
44
|
-
interface CompressResult {
|
|
45
|
-
/** 压缩后的消息列表 */
|
|
46
|
-
messages: ChatMessage[];
|
|
47
|
-
/** 原始消息的估算 token 数 */
|
|
48
|
-
originalTokens: number;
|
|
49
|
-
/** 压缩后的估算 token 数 */
|
|
50
|
-
compressedTokens: number;
|
|
51
|
-
/** 被移除/合并的消息数 */
|
|
52
|
-
removedCount: number;
|
|
53
|
-
/** 生成的摘要文本(仅 summary/hybrid 策略有值) */
|
|
54
|
-
summary?: string;
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Compress 操作接口
|
|
58
|
-
*
|
|
59
|
-
* 管理对话消息的压缩,支持三种策略:
|
|
60
|
-
* - `sliding-window`:移除最旧的消息,保留最近 N 条
|
|
61
|
-
* - `summary`:使用 LLM 生成摘要替换旧消息
|
|
62
|
-
* - `hybrid`:先滑动窗口,不够则回退到摘要
|
|
63
|
-
*
|
|
64
|
-
* @example
|
|
65
|
-
* ```ts
|
|
66
|
-
* const result = await compressOps.tryCompress(messages, {
|
|
67
|
-
* strategy: 'hybrid',
|
|
68
|
-
* maxTokens: 4000,
|
|
69
|
-
* })
|
|
70
|
-
* if (result.success) {
|
|
71
|
-
* const compressed = result.data.messages
|
|
72
|
-
* }
|
|
73
|
-
* ```
|
|
74
|
-
*/
|
|
75
|
-
interface CompressOperations {
|
|
76
|
-
/**
|
|
77
|
-
* 尝试压缩消息列表,使其不超过指定 Token 预算
|
|
78
|
-
*
|
|
79
|
-
* 如果当前 token 数未超限则直接返回(不压缩),因此命名为 tryCompress。
|
|
80
|
-
* 如果受保护的 system、最近消息或最新 user 输入自身已超过预算,返回
|
|
81
|
-
* `HaiAIError.CONTEXT_BUDGET_EXCEEDED`,不会删除受保护内容或返回超预算的成功结果。
|
|
82
|
-
*
|
|
83
|
-
* @param messages - 消息列表
|
|
84
|
-
* @param options - 压缩选项
|
|
85
|
-
* @returns 满足预算的压缩结果,或无法满足预算时的显式错误
|
|
86
|
-
*/
|
|
87
|
-
tryCompress: (messages: ChatMessage[], options?: CompressOptions) => Promise<HaiResult<CompressResult>>;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* @h-ai/ai — Summary 子功能类型
|
|
92
|
-
*
|
|
93
|
-
* 定义摘要生成操作的类型接口:使用 LLM 对对话消息生成摘要,支持增量摘要。
|
|
94
|
-
* @module ai-summary-types
|
|
95
|
-
*/
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* 摘要生成选项
|
|
99
|
-
*/
|
|
100
|
-
interface SummaryOptions {
|
|
101
|
-
/** 摘要用的模型 */
|
|
102
|
-
model?: string;
|
|
103
|
-
/** 温度覆盖 */
|
|
104
|
-
temperature?: number;
|
|
105
|
-
/** 自定义摘要 systemPrompt(覆盖模块配置与内置默认提示词) */
|
|
106
|
-
systemPrompt?: string;
|
|
107
|
-
/** 前序摘要文本(用于增量摘要) */
|
|
108
|
-
previousSummary?: string;
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* 摘要生成结果
|
|
112
|
-
*/
|
|
113
|
-
interface SummaryResult {
|
|
114
|
-
/** 摘要文本 */
|
|
115
|
-
summary: string;
|
|
116
|
-
/** 摘要的估算 Token 数 */
|
|
117
|
-
tokenCount: number;
|
|
118
|
-
/** 覆盖的原始消息数 */
|
|
119
|
-
coveredMessages: number;
|
|
120
|
-
}
|
|
121
|
-
/**
|
|
122
|
-
* Summary 操作接口
|
|
123
|
-
*
|
|
124
|
-
* 使用 LLM 对消息列表生成摘要,支持全量摘要与增量摘要。
|
|
125
|
-
*
|
|
126
|
-
* @example
|
|
127
|
-
* ```ts
|
|
128
|
-
* // 生成摘要(含元数据)
|
|
129
|
-
* const result = await summaryOps.summarize(messages)
|
|
130
|
-
* // result.data: { summary, tokenCount, coveredMessages }
|
|
131
|
-
*
|
|
132
|
-
* // 仅获取摘要文本
|
|
133
|
-
* const text = await summaryOps.generate(messages)
|
|
134
|
-
*
|
|
135
|
-
* // 增量摘要
|
|
136
|
-
* const updated = await summaryOps.summarize(newMessages, {
|
|
137
|
-
* systemPrompt: 'Focus on product decisions and pending action items.',
|
|
138
|
-
* previousSummary: oldSummary,
|
|
139
|
-
* })
|
|
140
|
-
* ```
|
|
141
|
-
*/
|
|
142
|
-
interface SummaryOperations {
|
|
143
|
-
/**
|
|
144
|
-
* 生成摘要文本
|
|
145
|
-
*
|
|
146
|
-
* @param messages - 消息列表
|
|
147
|
-
* @param options - 摘要选项
|
|
148
|
-
* @returns 摘要文本
|
|
149
|
-
*/
|
|
150
|
-
generate: (messages: ChatMessage[], options?: SummaryOptions) => Promise<HaiResult<string>>;
|
|
151
|
-
/**
|
|
152
|
-
* 生成摘要(含元数据)
|
|
153
|
-
*
|
|
154
|
-
* @param messages - 消息列表
|
|
155
|
-
* @param options - 摘要选项
|
|
156
|
-
* @returns 摘要结果(含 Token 数、覆盖消息数)
|
|
157
|
-
*/
|
|
158
|
-
summarize: (messages: ChatMessage[], options?: SummaryOptions) => Promise<HaiResult<SummaryResult>>;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* @h-ai/ai — Context 子功能类型
|
|
163
|
-
*
|
|
164
|
-
* 定义上下文管理操作的类型接口。
|
|
165
|
-
* Context 是全部子模块的聚合层,提供有状态的 ContextManager:
|
|
166
|
-
* 多轮对话自动压缩 + 可选 LLM / Memory / RAG / Reasoning / Tools 编排。
|
|
167
|
-
* @module ai-context-types
|
|
168
|
-
*/
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* Context 子功能可选依赖
|
|
172
|
-
*
|
|
173
|
-
* 传入后 ContextManager 可提供 chat/chatStream 等高层编排能力。
|
|
174
|
-
* 各依赖按需传入,未传入的能力不可用。
|
|
175
|
-
*/
|
|
176
|
-
interface ContextDeps {
|
|
177
|
-
/** LLM 操作(chat/chatStream 必需) */
|
|
178
|
-
llm?: LLMOperations;
|
|
179
|
-
/** Memory 操作(记忆注入/提取需要) */
|
|
180
|
-
memory?: MemoryOperations;
|
|
181
|
-
/** RAG 操作(检索增强生成需要) */
|
|
182
|
-
rag?: RagOperations;
|
|
183
|
-
/** Reasoning 操作(推理引擎需要) */
|
|
184
|
-
reasoning?: ReasoningOperations;
|
|
185
|
-
/** Summary 操作(会话固化 consolidate 需要) */
|
|
186
|
-
summary?: SummaryOperations;
|
|
187
|
-
}
|
|
188
|
-
/**
|
|
189
|
-
* 会话固化选项(`ContextManager.consolidate` 使用)
|
|
190
|
-
*
|
|
191
|
-
* 把「短期会话记忆 + 摘要」固化为「长期记忆」,形成
|
|
192
|
-
* `Session Memory → Summary → Long-term Memory` 的生命周期闭环。
|
|
193
|
-
*/
|
|
194
|
-
interface ConsolidateOptions {
|
|
195
|
-
/**
|
|
196
|
-
* 长期记忆作用域。
|
|
197
|
-
*
|
|
198
|
-
* 默认使用管理器 `memory.scope`。通常应传入**不含 sessionId** 的作用域
|
|
199
|
-
* (如 `{ userId, personaId }`),使固化后的记忆跨会话持久,而非绑定单次会话。
|
|
200
|
-
*/
|
|
201
|
-
scope?: Record<string, unknown>;
|
|
202
|
-
/** 固化提取的记忆类型限制 */
|
|
203
|
-
types?: MemoryType[];
|
|
204
|
-
/** 摘要 / 提取使用的模型 */
|
|
205
|
-
model?: string;
|
|
206
|
-
/** 固化提取的自定义 systemPrompt */
|
|
207
|
-
extractionSystemPrompt?: string;
|
|
208
|
-
}
|
|
209
|
-
/**
|
|
210
|
-
* 会话固化结果
|
|
211
|
-
*/
|
|
212
|
-
interface ConsolidateResult {
|
|
213
|
-
/** 本次会话的整合摘要 */
|
|
214
|
-
summary: string;
|
|
215
|
-
/** 固化到长期记忆的条目 */
|
|
216
|
-
memories: MemoryEntry[];
|
|
217
|
-
}
|
|
218
|
-
/**
|
|
219
|
-
* 对话轮次状态
|
|
220
|
-
*
|
|
221
|
-
* 描述一次 assistant 生成从「模型产出」到「真实进入对话」的生命周期:
|
|
222
|
-
* - `generating` — 模型正在/已生成,但尚未确定实际对外表达的内容
|
|
223
|
-
* - `speaking` — 已进入下游表达(如 TTS 合成 / 播放)
|
|
224
|
-
* - `completed` — 已提交,完整生成文本即为真实文本
|
|
225
|
-
* - `interrupted` — 被打断,仅实际表达出去的部分(committed)进入对话
|
|
226
|
-
*/
|
|
227
|
-
type ConversationTurnStatus = 'generating' | 'speaking' | 'completed' | 'interrupted';
|
|
228
|
-
/**
|
|
229
|
-
* 对话轮次
|
|
230
|
-
*
|
|
231
|
-
* 区分「模型生成的文本」与「真实进入对话的文本」,解决多智能体 / 语音访谈等场景中
|
|
232
|
-
* 「AI 说到一半被打断,下一轮所有参与者应看到真实发生了什么」的问题。
|
|
233
|
-
*/
|
|
234
|
-
interface ConversationTurn {
|
|
235
|
-
/** 轮次唯一标识 */
|
|
236
|
-
id: string;
|
|
237
|
-
/** 发言者 */
|
|
238
|
-
speaker: 'user' | 'assistant';
|
|
239
|
-
/** 模型生成的完整文本 */
|
|
240
|
-
generated: string;
|
|
241
|
-
/** 实际提交进入上下文的文本(`completed` 时通常等于 generated;`interrupted` 时为真实表达部分) */
|
|
242
|
-
committed: string;
|
|
243
|
-
/** 轮次状态 */
|
|
244
|
-
status: ConversationTurnStatus;
|
|
245
|
-
/** 创建时间(Unix 毫秒) */
|
|
246
|
-
createdAt: number;
|
|
247
|
-
/** 提交时间(Unix 毫秒,未提交时为 undefined) */
|
|
248
|
-
committedAt?: number;
|
|
249
|
-
}
|
|
250
|
-
/**
|
|
251
|
-
* 提交 / 打断轮次的输入
|
|
252
|
-
*/
|
|
253
|
-
interface CommitTurnInput {
|
|
254
|
-
/**
|
|
255
|
-
* 实际进入对话的文本。
|
|
256
|
-
*
|
|
257
|
-
* - `commitTurn` 不传时默认使用模型生成的完整文本(generated)。
|
|
258
|
-
* - `interruptTurn` 不传时默认视为「未表达任何内容」(空串,不写入上下文)。
|
|
259
|
-
*/
|
|
260
|
-
text?: string;
|
|
261
|
-
}
|
|
262
|
-
/**
|
|
263
|
-
* 有状态上下文管理器配置
|
|
264
|
-
*
|
|
265
|
-
* 通过嵌套子对象直接引用各子模块的配置类型,避免字段重复声明。
|
|
266
|
-
*/
|
|
267
|
-
interface ContextManagerOptions {
|
|
268
|
-
/** 交互作用域(objectId + sessionId) */
|
|
269
|
-
scope?: InteractionScope;
|
|
270
|
-
/** 系统提示词(创建时作为首条 system 消息追加) */
|
|
271
|
-
systemPrompt?: string;
|
|
272
|
-
/** LLM 模型名覆盖 */
|
|
273
|
-
model?: string;
|
|
274
|
-
/** 温度覆盖 */
|
|
275
|
-
temperature?: number;
|
|
276
|
-
/**
|
|
277
|
-
* 对话提交模式(默认 `auto`)
|
|
278
|
-
*
|
|
279
|
-
* - `auto`:`chat` / `chatStream` 生成结束后,自动把**模型生成的完整文本**写入上下文并触发记忆提取。
|
|
280
|
-
* - `manual`:生成结束后**不写入**上下文,仅登记一个待提交轮次并返回 `turnId`;由调用方在确定
|
|
281
|
-
* 「实际发生了什么」后,通过 `commitTurn` / `interruptTurn` 写入**真实文本**。
|
|
282
|
-
*
|
|
283
|
-
* 用于「模型生成 → TTS 合成 → 实际播放」链路:AI 说到一半被打断时,只有真正播放出去的
|
|
284
|
-
* 部分才应进入下一轮所有参与者可见的对话状态,而不是模型本想说完的全文。
|
|
285
|
-
*/
|
|
286
|
-
turnCommit?: 'auto' | 'manual';
|
|
287
|
-
/**
|
|
288
|
-
* 并发生成策略(默认 `reject`)
|
|
289
|
-
*
|
|
290
|
-
* ContextManager 默认实行**单活动生成**:同一管理器同一时刻只允许一个未完成的
|
|
291
|
-
* `chat` / `chatStream` 轮次,避免「上一轮 AI 尚未退出,下一轮 user 消息先写入」
|
|
292
|
-
* 导致的消息乱序(user1 → user2 → assistant1)。
|
|
293
|
-
*
|
|
294
|
-
* - `reject`:已有活动生成时,新的 `chat` / `chatStream` 立即返回 `CONTEXT_BUSY` 错误。
|
|
295
|
-
* - `queue`:新请求排队,等待前一轮次进入终态(`completed` / `interrupted`)后再开始。
|
|
296
|
-
*
|
|
297
|
-
* `manual` 提交模式下,屏障持续到 `commitTurn` / `interruptTurn` 提交真实文本为止,
|
|
298
|
-
* 因此「打断当前轮次 → 立即发起下一轮」也能保证顺序正确。
|
|
299
|
-
*/
|
|
300
|
-
concurrency?: 'reject' | 'queue';
|
|
301
|
-
/**
|
|
302
|
-
* 压缩配置(覆盖全局 compress 配置)
|
|
303
|
-
*
|
|
304
|
-
* 直接引用 CompressOptions,加上 auto 开关。
|
|
305
|
-
*/
|
|
306
|
-
compress?: CompressOptions & {
|
|
307
|
-
/** 是否自动触发压缩(默认 true) */
|
|
308
|
-
auto?: boolean;
|
|
309
|
-
};
|
|
310
|
-
/**
|
|
311
|
-
* 记忆配置
|
|
312
|
-
*
|
|
313
|
-
* 控制记忆注入与提取。`scope` / `types` / `minImportance` 会完整透传给 Memory 的
|
|
314
|
-
* `injectMemories` 与 `extract`,用于表达「用户 + 主题 + 角色」等多维隔离
|
|
315
|
-
* (如 `{ userId, topicId, personaId }`)。
|
|
316
|
-
*/
|
|
317
|
-
memory?: {
|
|
318
|
-
/** 是否启用记忆注入(默认 false) */
|
|
319
|
-
enable?: boolean;
|
|
320
|
-
/** 是否启用自动记忆提取(默认 false) */
|
|
321
|
-
enableExtract?: boolean;
|
|
322
|
-
/** 业务作用域(透传给 injectMemories / extract,key-value 匹配隔离) */
|
|
323
|
-
scope?: Record<string, unknown>;
|
|
324
|
-
/** 注入 / 提取时限定的记忆类型 */
|
|
325
|
-
types?: MemoryType[];
|
|
326
|
-
/** 注入时的最低重要性阈值 */
|
|
327
|
-
minImportance?: number;
|
|
328
|
-
/** 注入的记忆数量 */
|
|
329
|
-
topK?: number;
|
|
330
|
-
/** 注入记忆占用的最大 token 预算 */
|
|
331
|
-
maxTokens?: number;
|
|
332
|
-
/** 注入位置:system 追加或最后一条用户消息前插入 */
|
|
333
|
-
position?: 'system' | 'before-last';
|
|
334
|
-
/** 记忆提取使用的模型(覆盖默认提取模型) */
|
|
335
|
-
extractionModel?: string;
|
|
336
|
-
/** 记忆提取的自定义 systemPrompt */
|
|
337
|
-
extractionSystemPrompt?: string;
|
|
338
|
-
};
|
|
339
|
-
/**
|
|
340
|
-
* RAG 配置
|
|
341
|
-
*
|
|
342
|
-
* 引用 RagOptions 中检索相关的字段。
|
|
343
|
-
*/
|
|
344
|
-
rag?: Pick<RagOptions, 'sources' | 'topK' | 'minScore' | 'enableRerank' | 'rerankModel'> & {
|
|
345
|
-
/** 是否启用 RAG 检索增强(默认 false) */
|
|
346
|
-
enable?: boolean;
|
|
347
|
-
};
|
|
348
|
-
/**
|
|
349
|
-
* 推理配置
|
|
350
|
-
*
|
|
351
|
-
* 引用 ReasoningOptions 中策略相关的字段。
|
|
352
|
-
*/
|
|
353
|
-
reasoning?: Pick<ReasoningOptions, 'strategy' | 'maxRounds'> & {
|
|
354
|
-
/** 是否启用推理引擎替代普通 LLM(默认 false) */
|
|
355
|
-
enable?: boolean;
|
|
356
|
-
};
|
|
357
|
-
/** 工具注册表(传入后 chat/chatStream 支持 function calling) */
|
|
358
|
-
tools?: ToolRegistryOperations;
|
|
359
|
-
/**
|
|
360
|
-
* 工具调用最大轮次(默认 10)
|
|
361
|
-
*
|
|
362
|
-
* 当 LLM 返回 tool_calls 时,自动执行工具并将结果回传 LLM,
|
|
363
|
-
* 重复此过程直到 LLM 给出文本回复或达到最大轮次。
|
|
364
|
-
*/
|
|
365
|
-
maxToolRounds?: number;
|
|
366
|
-
}
|
|
367
|
-
/**
|
|
368
|
-
* 重置管理器选项(`ContextManager.reset` 使用)
|
|
369
|
-
*/
|
|
370
|
-
interface ContextResetOptions {
|
|
371
|
-
/**
|
|
372
|
-
* 是否保留系统提示词(默认 `true`)
|
|
373
|
-
*
|
|
374
|
-
* 系统提示词(Persona / System Prompt)只在创建时追加一次;重置若不保留,
|
|
375
|
-
* 会连同对话历史一起清空。默认重新写入系统提示词,避免 Persona 语义丢失。
|
|
376
|
-
*/
|
|
377
|
-
preserveSystemPrompt?: boolean;
|
|
378
|
-
/**
|
|
379
|
-
* 是否终止活动轮次(默认 `true`)
|
|
380
|
-
*
|
|
381
|
-
* 为 `true` 时:中断内部生成信号,使所有非终态轮次进入 `interrupted` 终态,
|
|
382
|
-
* 释放并发屏障,并阻止这些旧轮次在重置后被再次 `commitTurn` / `interruptTurn`。
|
|
383
|
-
*/
|
|
384
|
-
cancelActiveTurn?: boolean;
|
|
385
|
-
/**
|
|
386
|
-
* 是否等待后台记忆提取任务完成后再清空(默认 `false`)
|
|
387
|
-
*
|
|
388
|
-
* 为 `true` 时先 `flush()` 等待正在运行的记忆提取写入完成;否则不等待
|
|
389
|
-
* (已在途的提取任务仍会自行写入记忆后端,但其结果不再影响本管理器状态)。
|
|
390
|
-
*/
|
|
391
|
-
waitForMemoryTasks?: boolean;
|
|
392
|
-
}
|
|
393
|
-
/**
|
|
394
|
-
* 单次 chat/chatStream 请求的覆盖选项
|
|
395
|
-
*/
|
|
396
|
-
interface ContextChatOptions {
|
|
397
|
-
/** LLM 模型名覆盖 */
|
|
398
|
-
model?: string;
|
|
399
|
-
/** 温度覆盖 */
|
|
400
|
-
temperature?: number;
|
|
401
|
-
/** 是否启用本次 LLM 调用的持久化(默认 false,Context 自行管理状态) */
|
|
402
|
-
enablePersist?: boolean;
|
|
403
|
-
/**
|
|
404
|
-
* 请求取消信号
|
|
405
|
-
*
|
|
406
|
-
* 透传给底层 LLM 调用;打断、用户切换等场景可 `abortController.abort()`
|
|
407
|
-
* 立即停止上游生成与计费。
|
|
408
|
-
*/
|
|
409
|
-
signal?: AbortSignal;
|
|
410
|
-
}
|
|
411
|
-
/**
|
|
412
|
-
* chat() 返回的结果
|
|
413
|
-
*/
|
|
414
|
-
interface ContextChatResult {
|
|
415
|
-
/** LLM 回复内容 */
|
|
416
|
-
reply: string;
|
|
417
|
-
/** 使用的模型 */
|
|
418
|
-
model: string;
|
|
419
|
-
/**
|
|
420
|
-
* 本次生成对应的对话轮次 ID
|
|
421
|
-
*
|
|
422
|
-
* `turnCommit: 'manual'` 时,用于后续 `commitTurn` / `interruptTurn` 提交真实文本;
|
|
423
|
-
* `auto` 模式下该轮已自动提交(`completed`)。
|
|
424
|
-
*/
|
|
425
|
-
turnId: string;
|
|
426
|
-
/** Token 使用统计 */
|
|
427
|
-
usage?: {
|
|
428
|
-
prompt_tokens: number;
|
|
429
|
-
completion_tokens: number;
|
|
430
|
-
total_tokens: number;
|
|
431
|
-
};
|
|
432
|
-
}
|
|
433
|
-
/**
|
|
434
|
-
* chatStream() 产出的事件
|
|
435
|
-
*
|
|
436
|
-
* 事件序列:`turn_started` → `delta`* → `done`;中途取消(AbortSignal)时为
|
|
437
|
-
* `turn_started` → `delta`* → `cancelled`。`cancelled` 保留 turnId 与已生成文本,
|
|
438
|
-
* 调用方可用真实内容调用 `commitTurn` / `interruptTurn` 提交。
|
|
439
|
-
*/
|
|
440
|
-
type ContextStreamEvent = {
|
|
441
|
-
type: 'turn_started';
|
|
442
|
-
turnId: string;
|
|
443
|
-
} | {
|
|
444
|
-
type: 'delta';
|
|
445
|
-
text: string;
|
|
446
|
-
} | {
|
|
447
|
-
type: 'tool_call';
|
|
448
|
-
name: string;
|
|
449
|
-
arguments: string;
|
|
450
|
-
} | {
|
|
451
|
-
type: 'tool_result';
|
|
452
|
-
name: string;
|
|
453
|
-
content: string;
|
|
454
|
-
success: boolean;
|
|
455
|
-
} | {
|
|
456
|
-
type: 'done';
|
|
457
|
-
reply: string;
|
|
458
|
-
model: string;
|
|
459
|
-
turnId: string;
|
|
460
|
-
usage?: {
|
|
461
|
-
prompt_tokens: number;
|
|
462
|
-
completion_tokens: number;
|
|
463
|
-
total_tokens: number;
|
|
464
|
-
};
|
|
465
|
-
} | {
|
|
466
|
-
type: 'cancelled';
|
|
467
|
-
turnId: string;
|
|
468
|
-
generated: string;
|
|
469
|
-
};
|
|
470
|
-
/**
|
|
471
|
-
* 有状态上下文管理器接口
|
|
472
|
-
*
|
|
473
|
-
* 适用于多轮对话场景。追加消息并在超限时自动压缩;
|
|
474
|
-
* 若传入 deps.llm 则可直接通过 chat/chatStream 进行对话编排。
|
|
475
|
-
*
|
|
476
|
-
* @example
|
|
477
|
-
* ```ts
|
|
478
|
-
* // 创建管理器并直接对话
|
|
479
|
-
* const managerResult = ai.context.createManager({
|
|
480
|
-
* scope: { objectId: 'user-001', sessionId: 'sess-001' },
|
|
481
|
-
* systemPrompt: '你是一个友好的助手。',
|
|
482
|
-
* compress: { maxTokens: 8000, strategy: 'hybrid' },
|
|
483
|
-
* memory: { enable: true, enableExtract: true },
|
|
484
|
-
* })
|
|
485
|
-
* const manager = managerResult.data
|
|
486
|
-
*
|
|
487
|
-
* const result = await manager.chat('你好')
|
|
488
|
-
* if (result.success) {
|
|
489
|
-
* const reply = result.data.reply
|
|
490
|
-
* // 将 reply 渲染到对话界面
|
|
491
|
-
* }
|
|
492
|
-
*
|
|
493
|
-
* for await (const event of manager.chatStream('讲个故事')) {
|
|
494
|
-
* if (event.type === 'delta') process.stdout.write(event.text)
|
|
495
|
-
* }
|
|
496
|
-
*
|
|
497
|
-
* await manager.save()
|
|
498
|
-
* ```
|
|
499
|
-
*/
|
|
500
|
-
interface ContextManager {
|
|
501
|
-
/** 当前作用域(如已配置) */
|
|
502
|
-
readonly scope?: InteractionScope;
|
|
503
|
-
/**
|
|
504
|
-
* 追加消息
|
|
505
|
-
*
|
|
506
|
-
* 自动在超限时触发压缩(如果启用了 compress.auto)。
|
|
507
|
-
*
|
|
508
|
-
* @param message - 要追加的消息
|
|
509
|
-
* @returns 成功返回 ok(undefined)
|
|
510
|
-
*/
|
|
511
|
-
addMessage: (message: ChatMessage) => Promise<HaiResult<void>>;
|
|
512
|
-
/**
|
|
513
|
-
* 获取当前消息列表(压缩后)
|
|
514
|
-
*
|
|
515
|
-
* @returns 当前消息列表
|
|
516
|
-
*/
|
|
517
|
-
getMessages: () => HaiResult<ChatMessage[]>;
|
|
518
|
-
/**
|
|
519
|
-
* 获取当前 token 使用量
|
|
520
|
-
*
|
|
521
|
-
* @returns 当前 token 数和预算
|
|
522
|
-
*/
|
|
523
|
-
getTokenUsage: () => HaiResult<{
|
|
524
|
-
current: number;
|
|
525
|
-
budget: number;
|
|
526
|
-
}>;
|
|
527
|
-
/**
|
|
528
|
-
* 获取历史摘要列表
|
|
529
|
-
*
|
|
530
|
-
* @returns 每次压缩产生的摘要
|
|
531
|
-
*/
|
|
532
|
-
getSummaries: () => HaiResult<SummaryResult[]>;
|
|
533
|
-
/**
|
|
534
|
-
* 持久化当前状态(需要 scope + 存储可用)
|
|
535
|
-
*
|
|
536
|
-
* 内部会先 `flush()` 等待所有后台记忆提取完成,确保持久化时记忆已写入。
|
|
537
|
-
*/
|
|
538
|
-
save: () => Promise<HaiResult<void>>;
|
|
539
|
-
/**
|
|
540
|
-
* 等待所有后台记忆提取任务完成
|
|
541
|
-
*
|
|
542
|
-
* chat/chatStream 的自动记忆提取是「即发即忘」的后台任务;在开始下一轮召回、
|
|
543
|
-
* 生成总结或关闭前调用 `flush()`,可避免「上一轮记忆尚未写完」的时序问题(issue #14)。
|
|
544
|
-
*
|
|
545
|
-
* @returns 全部任务完成返回 ok(undefined)
|
|
546
|
-
*/
|
|
547
|
-
flush: () => Promise<HaiResult<void>>;
|
|
548
|
-
/**
|
|
549
|
-
* 当前挂起的后台记忆提取任务数量
|
|
550
|
-
*
|
|
551
|
-
* 供应用侧观测;为 0 表示无待写入的记忆任务。
|
|
552
|
-
*/
|
|
553
|
-
readonly pendingMemoryTasks: number;
|
|
554
|
-
/**
|
|
555
|
-
* 重置管理器
|
|
556
|
-
*
|
|
557
|
-
* 默认行为:终止活动轮次(进入终态并释放并发屏障)、清空消息 / 摘要 / 轮次 /
|
|
558
|
-
* 待提交轮次,并重新写入系统提示词。可通过选项调整。
|
|
559
|
-
*
|
|
560
|
-
* @param options - 重置选项(保留系统提示词 / 终止活动轮次 / 等待记忆任务)
|
|
561
|
-
* @returns 成功返回 ok(undefined)
|
|
562
|
-
*/
|
|
563
|
-
reset: (options?: ContextResetOptions) => Promise<HaiResult<void>>;
|
|
564
|
-
/**
|
|
565
|
-
* 获取对话轮次列表(Conversation Commit Layer)
|
|
566
|
-
*
|
|
567
|
-
* 记录每次 chat/chatStream 生成的轮次及其 `generated` / `committed` / `status`,
|
|
568
|
-
* 供应用侧观测「模型生成」与「真实进入对话」的差异。
|
|
569
|
-
*
|
|
570
|
-
* @returns 轮次列表(按发生顺序)
|
|
571
|
-
*/
|
|
572
|
-
getTurns: () => HaiResult<ConversationTurn[]>;
|
|
573
|
-
/**
|
|
574
|
-
* 标记某轮次进入「表达中」(如 TTS 开始播放)
|
|
575
|
-
*
|
|
576
|
-
* 仅更新状态用于观测,不改变上下文内容。
|
|
577
|
-
*
|
|
578
|
-
* @param turnId - 轮次 ID
|
|
579
|
-
* @returns 成功返回 ok(undefined);轮次不存在返回 CONTEXT_TURN_NOT_FOUND
|
|
580
|
-
*/
|
|
581
|
-
markTurnSpeaking: (turnId: string) => HaiResult<void>;
|
|
582
|
-
/**
|
|
583
|
-
* 提交轮次(`turnCommit: 'manual'` 场景)
|
|
584
|
-
*
|
|
585
|
-
* 把真实文本写入上下文并触发记忆提取;`text` 缺省时使用模型生成的完整文本。
|
|
586
|
-
* 提交后该轮 `status` 变为 `completed`。
|
|
587
|
-
*
|
|
588
|
-
* @param turnId - 轮次 ID
|
|
589
|
-
* @param input - 提交内容(可覆盖为真实表达文本)
|
|
590
|
-
* @returns 成功返回 ok(undefined);轮次不存在或已提交返回对应错误
|
|
591
|
-
*/
|
|
592
|
-
commitTurn: (turnId: string, input?: CommitTurnInput) => Promise<HaiResult<void>>;
|
|
593
|
-
/**
|
|
594
|
-
* 打断轮次(`turnCommit: 'manual'` 场景)
|
|
595
|
-
*
|
|
596
|
-
* 只把「实际表达出去的部分」写入上下文;`text` 缺省时视为未表达任何内容(不写入)。
|
|
597
|
-
* 打断后该轮 `status` 变为 `interrupted`。
|
|
598
|
-
*
|
|
599
|
-
* @param turnId - 轮次 ID
|
|
600
|
-
* @param input - 实际表达出去的文本
|
|
601
|
-
* @returns 成功返回 ok(undefined);轮次不存在或已提交返回对应错误
|
|
602
|
-
*/
|
|
603
|
-
interruptTurn: (turnId: string, input?: CommitTurnInput) => Promise<HaiResult<void>>;
|
|
604
|
-
/**
|
|
605
|
-
* 将当前会话固化为长期记忆(Memory 生命周期)
|
|
606
|
-
*
|
|
607
|
-
* 流程:整合会话摘要(历史摘要 + 当前消息)→ 从摘要中提取长期记忆 → 以持久作用域写入。
|
|
608
|
-
* 需要 deps.summary + deps.memory 可用。用于会话结束时把「短期会话记忆」沉淀为
|
|
609
|
-
* 「跨会话长期记忆」,形成 Session → Summary → Long-term Memory 闭环。
|
|
610
|
-
*
|
|
611
|
-
* @param options - 固化选项(长期作用域、类型、模型等)
|
|
612
|
-
* @returns 会话摘要与固化的记忆条目
|
|
613
|
-
*/
|
|
614
|
-
consolidate: (options?: ConsolidateOptions) => Promise<HaiResult<ConsolidateResult>>;
|
|
615
|
-
/**
|
|
616
|
-
* 发送消息并获取回复(需 deps.llm 可用)
|
|
617
|
-
*
|
|
618
|
-
* 流程:追加用户消息 → 自动压缩 → 注入记忆(可选) → RAG(可选) → LLM/Reasoning → 追加助手消息 → 提取记忆(可选)
|
|
619
|
-
*
|
|
620
|
-
* @param message - 用户消息文本
|
|
621
|
-
* @param options - 单次请求覆盖选项
|
|
622
|
-
* @returns 对话结果
|
|
623
|
-
*/
|
|
624
|
-
chat: (message: string, options?: ContextChatOptions) => Promise<HaiResult<ContextChatResult>>;
|
|
625
|
-
/**
|
|
626
|
-
* 流式发送消息并获取回复(需 deps.llm 可用)
|
|
627
|
-
*
|
|
628
|
-
* 产出事件序列:turn_started → delta* → done(中途取消时 → cancelled)
|
|
629
|
-
*
|
|
630
|
-
* @param message - 用户消息文本
|
|
631
|
-
* @param options - 单次请求覆盖选项
|
|
632
|
-
* @returns 异步可迭代的 ContextStreamEvent
|
|
633
|
-
*/
|
|
634
|
-
chatStream: (message: string, options?: ContextChatOptions) => AsyncIterable<ContextStreamEvent>;
|
|
635
|
-
}
|
|
636
|
-
/**
|
|
637
|
-
* Context 操作接口(通过 `ai.context` 访问)
|
|
638
|
-
*
|
|
639
|
-
* 提供有状态的 ContextManager,管理多轮对话的消息追加、自动压缩与对话编排。
|
|
640
|
-
* 原子操作(token / summary / compress)已独立暴露在 `ai.token`、`ai.summary`、`ai.compress`。
|
|
641
|
-
* 需要先调用 `ai.init()` 初始化后使用。
|
|
642
|
-
*
|
|
643
|
-
* @example
|
|
644
|
-
* ```ts
|
|
645
|
-
* // 创建管理器并对话
|
|
646
|
-
* const managerResult = ai.context.createManager({
|
|
647
|
-
* scope: { objectId: 'user-001', sessionId: 'sess-001' },
|
|
648
|
-
* compress: { maxTokens: 8000 },
|
|
649
|
-
* memory: { enable: true },
|
|
650
|
-
* })
|
|
651
|
-
* const manager = managerResult.data
|
|
652
|
-
* const result = await manager.chat('你好')
|
|
653
|
-
*
|
|
654
|
-
* // 从持久化恢复管理器
|
|
655
|
-
* const restored = await ai.context.restoreManager(
|
|
656
|
-
* { objectId: 'user-001', sessionId: 'sess-001' },
|
|
657
|
-
* { memory: { enable: true } },
|
|
658
|
-
* )
|
|
659
|
-
* ```
|
|
660
|
-
*/
|
|
661
|
-
interface ContextOperations {
|
|
662
|
-
/**
|
|
663
|
-
* 创建有状态上下文管理器
|
|
664
|
-
*
|
|
665
|
-
* @param options - 管理器配置
|
|
666
|
-
* @returns 管理器实例
|
|
667
|
-
*/
|
|
668
|
-
createManager: (options?: ContextManagerOptions) => HaiResult<ContextManager>;
|
|
669
|
-
/**
|
|
670
|
-
* 从持久化恢复上下文管理器
|
|
671
|
-
*
|
|
672
|
-
* @param scope - 交互作用域
|
|
673
|
-
* @param options - 管理器配置覆盖
|
|
674
|
-
* @returns 恢复的管理器实例
|
|
675
|
-
*/
|
|
676
|
-
restoreManager: (scope: InteractionScope, options?: Omit<ContextManagerOptions, 'scope'>) => Promise<HaiResult<ContextManager>>;
|
|
677
|
-
/**
|
|
678
|
-
* 列出指定主体的所有会话
|
|
679
|
-
*
|
|
680
|
-
* @param objectId - 主体 ID
|
|
681
|
-
* @returns 会话信息列表
|
|
682
|
-
*/
|
|
683
|
-
listSessions: (objectId: string) => Promise<HaiResult<SessionInfo[]>>;
|
|
684
|
-
/**
|
|
685
|
-
* 重命名会话
|
|
686
|
-
*
|
|
687
|
-
* @param scope - 交互作用域(objectId + sessionId,用于多租户隔离)
|
|
688
|
-
* @param title - 新标题
|
|
689
|
-
* @returns 成功返回 ok(undefined)
|
|
690
|
-
*/
|
|
691
|
-
renameSession: (scope: InteractionScope, title: string) => Promise<HaiResult<void>>;
|
|
692
|
-
/**
|
|
693
|
-
* 删除会话(删除会话元数据和对应的上下文数据)
|
|
694
|
-
*
|
|
695
|
-
* @param scope - 交互作用域(objectId + sessionId,用于多租户隔离)
|
|
696
|
-
* @returns 成功返回 ok(undefined)
|
|
697
|
-
*/
|
|
698
|
-
removeSession: (scope: InteractionScope) => Promise<HaiResult<void>>;
|
|
699
|
-
}
|
|
700
|
-
|
|
701
|
-
/**
|
|
702
|
-
* @h-ai/ai — Embedding 子功能类型
|
|
703
|
-
*
|
|
704
|
-
* 定义向量嵌入操作的类型接口。
|
|
705
|
-
* @module ai-embedding-types
|
|
706
|
-
*/
|
|
707
|
-
|
|
708
|
-
/**
|
|
709
|
-
* Embedding 请求参数
|
|
710
|
-
*/
|
|
711
|
-
interface EmbeddingRequest {
|
|
712
|
-
/** 输入文本(单条或批量) */
|
|
713
|
-
input: string | string[];
|
|
714
|
-
/** 模型名称(可选,未指定时使用配置中的默认模型) */
|
|
715
|
-
model?: string;
|
|
716
|
-
/** 向量维度(可选,部分模型支持) */
|
|
717
|
-
dimensions?: number;
|
|
718
|
-
}
|
|
719
|
-
/**
|
|
720
|
-
* 单条 Embedding 结果
|
|
721
|
-
*/
|
|
722
|
-
interface EmbeddingItem {
|
|
723
|
-
/** 在输入列表中的索引 */
|
|
724
|
-
index: number;
|
|
725
|
-
/** 向量数据 */
|
|
726
|
-
embedding: number[];
|
|
727
|
-
}
|
|
728
|
-
/**
|
|
729
|
-
* Embedding 响应
|
|
730
|
-
*/
|
|
731
|
-
interface EmbeddingResponse {
|
|
732
|
-
/** 模型名称 */
|
|
733
|
-
model: string;
|
|
734
|
-
/** Embedding 结果列表 */
|
|
735
|
-
data: EmbeddingItem[];
|
|
736
|
-
/** Token 使用统计 */
|
|
737
|
-
usage: {
|
|
738
|
-
prompt_tokens: number;
|
|
739
|
-
total_tokens: number;
|
|
740
|
-
};
|
|
741
|
-
}
|
|
742
|
-
/**
|
|
743
|
-
* Embedding Provider 接口
|
|
744
|
-
*
|
|
745
|
-
* 底层嵌入 API 适配层。
|
|
746
|
-
*/
|
|
747
|
-
interface EmbeddingProvider {
|
|
748
|
-
/** 生成向量嵌入 */
|
|
749
|
-
embed: (request: EmbeddingRequest) => Promise<HaiResult<EmbeddingResponse>>;
|
|
750
|
-
}
|
|
751
|
-
/**
|
|
752
|
-
* Embedding 操作接口(通过 `ai.embedding` 访问)
|
|
753
|
-
*
|
|
754
|
-
* 需要先调用 `ai.init()` 初始化后使用。
|
|
755
|
-
*
|
|
756
|
-
* @example
|
|
757
|
-
* ```ts
|
|
758
|
-
* // 单条文本嵌入
|
|
759
|
-
* const result = await ai.embedding.embed({ input: 'Hello World' })
|
|
760
|
-
*
|
|
761
|
-
* // 批量嵌入
|
|
762
|
-
* const result = await ai.embedding.embed({
|
|
763
|
-
* input: ['文本1', '文本2', '文本3'],
|
|
764
|
-
* })
|
|
765
|
-
* ```
|
|
766
|
-
*/
|
|
767
|
-
interface EmbeddingOperations {
|
|
768
|
-
/**
|
|
769
|
-
* 生成向量嵌入
|
|
770
|
-
*
|
|
771
|
-
* @param request - Embedding 请求
|
|
772
|
-
* @returns Embedding 响应
|
|
773
|
-
*/
|
|
774
|
-
embed: (request: EmbeddingRequest) => Promise<HaiResult<EmbeddingResponse>>;
|
|
775
|
-
/**
|
|
776
|
-
* 便捷方法:嵌入单条文本,直接返回向量
|
|
777
|
-
*
|
|
778
|
-
* @param text - 输入文本
|
|
779
|
-
* @returns 向量数组
|
|
780
|
-
*/
|
|
781
|
-
embedText: (text: string) => Promise<HaiResult<number[]>>;
|
|
782
|
-
/**
|
|
783
|
-
* 便捷方法:批量嵌入文本,返回向量列表
|
|
784
|
-
*
|
|
785
|
-
* @param texts - 输入文本列表
|
|
786
|
-
* @returns 向量数组列表(与输入顺序一致)
|
|
787
|
-
*/
|
|
788
|
-
embedBatch: (texts: string[]) => Promise<HaiResult<number[][]>>;
|
|
789
|
-
}
|
|
790
|
-
|
|
791
|
-
/**
|
|
792
|
-
* @h-ai/ai — File 子功能类型
|
|
793
|
-
*
|
|
794
|
-
* 定义文件解析操作的类型接口。
|
|
795
|
-
* @module ai-file-types
|
|
796
|
-
*/
|
|
797
|
-
|
|
798
|
-
/**
|
|
799
|
-
* 文件解析方法
|
|
800
|
-
*
|
|
801
|
-
* - `text` — 直接解码为 UTF-8 文本(txt / md / csv / 等文本格式)
|
|
802
|
-
* - `html` — HTML 标签剥除后提取文本
|
|
803
|
-
* - `pdf` — 使用 pdfjs-dist 解析 PDF(需安装可选依赖)
|
|
804
|
-
* - `docx` — 使用 mammoth 解析 Word 文档(需安装可选依赖)
|
|
805
|
-
* - `ocr` — 通过视觉 LLM 进行 OCR 识别(图片及无原生解析器的格式)
|
|
806
|
-
*/
|
|
807
|
-
type FileParseMethod = 'text' | 'html' | 'pdf' | 'docx' | 'ocr';
|
|
808
|
-
/**
|
|
809
|
-
* 文件解析输出格式
|
|
810
|
-
* - `text` — 纯文本(默认),适合程序处理
|
|
811
|
-
* - `markdown` — Markdown 格式,保留文档结构(标题、列表、粗斜体等),
|
|
812
|
-
* 适用于 HTML、PDF、DOCX;图片 OCR 会提示模型输出 Markdown
|
|
813
|
-
*/
|
|
814
|
-
type OutputFormat = 'text' | 'markdown';
|
|
815
|
-
/**
|
|
816
|
-
* 文件解析选项
|
|
817
|
-
*/
|
|
818
|
-
interface FileParseOptions {
|
|
819
|
-
/** 强制使用指定 MIME 类型(覆盖自动检测) */
|
|
820
|
-
mimeType?: string;
|
|
821
|
-
/** 强制使用 OCR(即使有原生解析器支持) */
|
|
822
|
-
useOcr?: boolean;
|
|
823
|
-
/**
|
|
824
|
-
* OCR 使用的视觉模型(请求级覆盖)
|
|
825
|
-
*
|
|
826
|
-
* 最高优先级,覆盖 `llm.scenarios.ocr` 场景映射。
|
|
827
|
-
* 未指定时通过 `llm.scenarios.ocr` 解析。
|
|
828
|
-
*/
|
|
829
|
-
model?: string;
|
|
830
|
-
/** OCR 系统提示词(覆盖全局 `file.systemPrompt` 配置) */
|
|
831
|
-
systemPrompt?: string;
|
|
832
|
-
/** PDF 最大解析页数(默认解析全部页) */
|
|
833
|
-
maxPages?: number;
|
|
834
|
-
/**
|
|
835
|
-
* 输出格式(默认 `'text'`)
|
|
836
|
-
*/
|
|
837
|
-
outputFormat?: OutputFormat;
|
|
838
|
-
}
|
|
839
|
-
/**
|
|
840
|
-
* 文件解析请求
|
|
841
|
-
*/
|
|
842
|
-
interface FileParseRequest {
|
|
843
|
-
/**
|
|
844
|
-
* 文件内容
|
|
845
|
-
*
|
|
846
|
-
* - `Buffer` — 二进制文件(图片、PDF、DOCX 等)
|
|
847
|
-
* - `string` — 文本内容(直接传入文本格式文件)
|
|
848
|
-
*/
|
|
849
|
-
content: Buffer | string;
|
|
850
|
-
/** 文件名(用于 MIME 类型自动检测,可选) */
|
|
851
|
-
filename?: string;
|
|
852
|
-
/** 解析选项 */
|
|
853
|
-
options?: FileParseOptions;
|
|
854
|
-
}
|
|
855
|
-
/**
|
|
856
|
-
* 文件解析结果
|
|
857
|
-
*/
|
|
858
|
-
interface FileParseResult {
|
|
859
|
-
/** 提取的文本内容(当 `outputFormat` 为 `'markdown'` 时为 Markdown 格式) */
|
|
860
|
-
text: string;
|
|
861
|
-
/** 解析方法 */
|
|
862
|
-
method: FileParseMethod;
|
|
863
|
-
/** 页数(PDF 解析时) */
|
|
864
|
-
pageCount?: number;
|
|
865
|
-
/** 元数据 */
|
|
866
|
-
metadata?: {
|
|
867
|
-
filename?: string;
|
|
868
|
-
mimeType?: string;
|
|
869
|
-
charCount?: number;
|
|
870
|
-
};
|
|
871
|
-
}
|
|
872
|
-
/**
|
|
873
|
-
* File 操作接口(通过 `ai.file` 访问)
|
|
874
|
-
*
|
|
875
|
-
* 需要先调用 `ai.init()` 初始化后使用(OCR 功能需要 LLM 配置)。
|
|
876
|
-
*
|
|
877
|
-
* 支持的格式:
|
|
878
|
-
* - 文本格式(txt、md、csv 等):直接解码
|
|
879
|
-
* - HTML:剥除标签后提取文本
|
|
880
|
-
* - PDF:使用 pdfjs-dist(需安装),否则回退到 OCR
|
|
881
|
-
* - DOCX:使用 mammoth(需安装),否则回退到 OCR
|
|
882
|
-
* - 图片(JPEG、PNG、GIF、WebP):通过视觉 LLM OCR 识别
|
|
883
|
-
*
|
|
884
|
-
* @example
|
|
885
|
-
* ```ts
|
|
886
|
-
* import { readFile } from 'node:fs/promises'
|
|
887
|
-
*
|
|
888
|
-
* // 解析 PDF(OCR 模型通过 llm.scenarios.ocr 配置)
|
|
889
|
-
* ai.init({ llm: { apiKey: 'sk-...', scenarios: { ocr: 'gpt-4o' } } })
|
|
890
|
-
*
|
|
891
|
-
* const pdf = await readFile('document.pdf')
|
|
892
|
-
* const result = await ai.file.parse({ content: pdf, filename: 'document.pdf' })
|
|
893
|
-
*
|
|
894
|
-
* // 解析图片(OCR)
|
|
895
|
-
* const image = await readFile('screenshot.png')
|
|
896
|
-
* const result = await ai.file.parse({ content: image, filename: 'screenshot.png' })
|
|
897
|
-
*
|
|
898
|
-
* // 便捷方法:直接获取文本
|
|
899
|
-
* const text = await ai.file.parseText(content, 'document.pdf')
|
|
900
|
-
* ```
|
|
901
|
-
*/
|
|
902
|
-
interface FileOperations {
|
|
903
|
-
/**
|
|
904
|
-
* 解析文件内容,提取文本
|
|
905
|
-
*
|
|
906
|
-
* 自动检测文件格式,选择最优解析方法。
|
|
907
|
-
*
|
|
908
|
-
* @param request - 文件解析请求
|
|
909
|
-
* @returns 解析结果,包含文本内容、解析方法和元数据
|
|
910
|
-
*/
|
|
911
|
-
parse: (request: FileParseRequest) => Promise<HaiResult<FileParseResult>>;
|
|
912
|
-
/**
|
|
913
|
-
* 便捷方法:直接返回提取的文本字符串
|
|
914
|
-
*
|
|
915
|
-
* @param content - 文件内容(Buffer 或文本字符串)
|
|
916
|
-
* @param filename - 文件名(用于格式检测,可选)
|
|
917
|
-
* @returns 提取的文本内容
|
|
918
|
-
*/
|
|
919
|
-
parseText: (content: Buffer | string, filename?: string) => Promise<HaiResult<string>>;
|
|
920
|
-
}
|
|
921
|
-
|
|
922
|
-
/**
|
|
923
|
-
* @h-ai/ai — 文生图公共类型
|
|
924
|
-
*
|
|
925
|
-
* 公共契约只暴露业务需要的提示词、尺寸和标准化图片字节。
|
|
926
|
-
* 厂商请求字段、临时 URL 与响应中间态由 Provider 内部处理。
|
|
927
|
-
* @module image/ai-image-types
|
|
928
|
-
*/
|
|
929
|
-
|
|
930
|
-
/** 图片像素尺寸 */
|
|
931
|
-
interface ImageSize {
|
|
932
|
-
/** 宽度(像素) */
|
|
933
|
-
width: number;
|
|
934
|
-
/** 高度(像素) */
|
|
935
|
-
height: number;
|
|
936
|
-
}
|
|
937
|
-
/** 可选参考图;Provider 会转换为对应厂商的传输格式 */
|
|
938
|
-
interface ReferenceImage {
|
|
939
|
-
/** 图片二进制内容 */
|
|
940
|
-
data: Uint8Array;
|
|
941
|
-
/** 图片 MIME 类型,例如 `image/png`、`image/jpeg` */
|
|
942
|
-
mimeType: string;
|
|
943
|
-
}
|
|
944
|
-
/** 文生图请求 */
|
|
945
|
-
interface GenerateImageRequest {
|
|
946
|
-
/** 图片内容与风格提示词 */
|
|
947
|
-
prompt: string;
|
|
948
|
-
/** 模型 ID;不传时使用配置中的默认文生图模型 */
|
|
949
|
-
model?: string;
|
|
950
|
-
/** 输出像素尺寸;不传时由模型决定 */
|
|
951
|
-
size?: ImageSize;
|
|
952
|
-
/** 用于图生图、编辑或风格参考的一张或多张图片 */
|
|
953
|
-
referenceImages?: ReferenceImage[];
|
|
954
|
-
/** 请求取消信号 */
|
|
955
|
-
signal?: AbortSignal;
|
|
956
|
-
}
|
|
957
|
-
/** 标准化后的单张图片 */
|
|
958
|
-
interface GeneratedImage {
|
|
959
|
-
/** 图片二进制内容 */
|
|
960
|
-
data: Uint8Array;
|
|
961
|
-
/** 图片 MIME 类型 */
|
|
962
|
-
mimeType: string;
|
|
963
|
-
/** 实际宽度(厂商返回时提供) */
|
|
964
|
-
width?: number;
|
|
965
|
-
/** 实际高度(厂商返回时提供) */
|
|
966
|
-
height?: number;
|
|
967
|
-
}
|
|
968
|
-
/** 文生图结果 */
|
|
969
|
-
interface GenerateImageResult {
|
|
970
|
-
/** 厂商实际返回的图片;通常为一张 */
|
|
971
|
-
images: GeneratedImage[];
|
|
972
|
-
}
|
|
973
|
-
/** 文生图操作接口(通过 `ai.image` 访问) */
|
|
974
|
-
interface ImageOperations {
|
|
975
|
-
/** 根据文本提示生成图片 */
|
|
976
|
-
generate: (request: GenerateImageRequest) => Promise<HaiResult<GenerateImageResult>>;
|
|
977
|
-
}
|
|
978
|
-
|
|
979
|
-
/**
|
|
980
|
-
* @h-ai/ai — MCP 子功能类型
|
|
981
|
-
*
|
|
982
|
-
* 定义 MCP 工具、资源、提示词的注册与调用接口。
|
|
983
|
-
* @module ai-mcp-types
|
|
984
|
-
*/
|
|
985
|
-
|
|
986
|
-
/** MCP 工具定义(注册工具时的元数据) */
|
|
987
|
-
interface MCPToolDefinition {
|
|
988
|
-
/** 工具名称(需唯一) */
|
|
989
|
-
name: string;
|
|
990
|
-
/** 工具功能描述 */
|
|
991
|
-
description: string;
|
|
992
|
-
/** 输入参数的 JSON Schema */
|
|
993
|
-
inputSchema: Record<string, unknown>;
|
|
994
|
-
}
|
|
995
|
-
/** MCP 工具处理器,接收输入参数和执行上下文 */
|
|
996
|
-
type MCPToolHandler<TInput = unknown, TOutput = unknown> = (input: TInput, context: MCPContext) => Promise<TOutput> | TOutput;
|
|
997
|
-
/** MCP 执行上下文,携带请求元数据 */
|
|
998
|
-
interface MCPContext {
|
|
999
|
-
/** 请求唯一标识(未传入时自动生成 UUID) */
|
|
1000
|
-
requestId?: string;
|
|
1001
|
-
/** 客户端信息(可选) */
|
|
1002
|
-
clientInfo?: {
|
|
1003
|
-
name: string;
|
|
1004
|
-
version: string;
|
|
1005
|
-
};
|
|
1006
|
-
/** 自定义元数据(可选) */
|
|
1007
|
-
metadata?: Record<string, unknown>;
|
|
1008
|
-
}
|
|
1009
|
-
/** MCP 资源描述(注册资源时的元数据) */
|
|
1010
|
-
interface MCPResource {
|
|
1011
|
-
/** 资源 URI(唯一标识) */
|
|
1012
|
-
uri: string;
|
|
1013
|
-
/** 资源名称 */
|
|
1014
|
-
name: string;
|
|
1015
|
-
/** 资源描述(可选) */
|
|
1016
|
-
description?: string;
|
|
1017
|
-
/** MIME 类型(可选,如 `'application/json'`) */
|
|
1018
|
-
mimeType?: string;
|
|
1019
|
-
}
|
|
1020
|
-
/** MCP 资源内容(readResource 的返回值) */
|
|
1021
|
-
interface MCPResourceContent {
|
|
1022
|
-
/** 资源 URI */
|
|
1023
|
-
uri: string;
|
|
1024
|
-
/** MIME 类型(可选) */
|
|
1025
|
-
mimeType?: string;
|
|
1026
|
-
/** 文本内容(与 blob 二选一) */
|
|
1027
|
-
text?: string;
|
|
1028
|
-
/** Base64 编码的二进制内容(与 text 二选一) */
|
|
1029
|
-
blob?: string;
|
|
1030
|
-
}
|
|
1031
|
-
/** MCP 提示词模板描述 */
|
|
1032
|
-
interface MCPPrompt {
|
|
1033
|
-
/** 提示词名称(唯一标识) */
|
|
1034
|
-
name: string;
|
|
1035
|
-
/** 提示词描述(可选) */
|
|
1036
|
-
description?: string;
|
|
1037
|
-
/** 参数定义列表(可选) */
|
|
1038
|
-
arguments?: MCPPromptArgument[];
|
|
1039
|
-
}
|
|
1040
|
-
/** MCP 提示词参数定义 */
|
|
1041
|
-
interface MCPPromptArgument {
|
|
1042
|
-
/** 参数名称 */
|
|
1043
|
-
name: string;
|
|
1044
|
-
/** 参数描述(可选) */
|
|
1045
|
-
description?: string;
|
|
1046
|
-
/** 是否必填(默认 `false`) */
|
|
1047
|
-
required?: boolean;
|
|
1048
|
-
}
|
|
1049
|
-
/** MCP 提示词消息,直接复用 SDK 的完整判别联合(含图片、音频与嵌入资源) */
|
|
1050
|
-
type MCPPromptMessage = PromptMessage;
|
|
1051
|
-
/** MCP 提示词内容;按 type 缩窄后访问对应字段 */
|
|
1052
|
-
type MCPPromptContent = PromptMessage['content'];
|
|
1053
|
-
/** MCP 服务器创建选项 */
|
|
1054
|
-
interface McpServerOptions {
|
|
1055
|
-
/** 服务器名称 */
|
|
1056
|
-
name: string;
|
|
1057
|
-
/** 服务器版本(默认 `'1.0.0'`) */
|
|
1058
|
-
version?: string;
|
|
1059
|
-
}
|
|
1060
|
-
/**
|
|
1061
|
-
* MCP 操作接口(通过 `ai.mcp` 访问)
|
|
1062
|
-
*
|
|
1063
|
-
* 需要先调用 `ai.init()` 初始化,否则所有方法返回 `NOT_INITIALIZED` 错误。
|
|
1064
|
-
*/
|
|
1065
|
-
interface MCPOperations {
|
|
1066
|
-
/** 注册工具并编译 JSON Schema;非法 Schema 返回 MCP_TOOL_ERROR,同名注册覆盖 */
|
|
1067
|
-
registerTool: <TInput, TOutput>(definition: MCPToolDefinition, handler: MCPToolHandler<TInput, TOutput>) => HaiResult<void>;
|
|
1068
|
-
/** 注册资源加载器;读取时校验 text/blob 输出 */
|
|
1069
|
-
registerResource: (resource: MCPResource, handler: () => Promise<MCPResourceContent>) => HaiResult<void>;
|
|
1070
|
-
/** 注册提示词;调用时校验必填参数和 SDK 消息格式 */
|
|
1071
|
-
registerPrompt: (prompt: MCPPrompt, handler: (args: Record<string, string>) => Promise<MCPPromptMessage[]>) => HaiResult<void>;
|
|
1072
|
-
/** 校验输入后执行工具;自动补齐缺失的 requestId */
|
|
1073
|
-
callTool: (name: string, args: unknown, context?: MCPContext) => Promise<HaiResult<unknown>>;
|
|
1074
|
-
/** 读取资源,输出必须恰好含一个有效 text 或 blob */
|
|
1075
|
-
readResource: (uri: string) => Promise<HaiResult<MCPResourceContent>>;
|
|
1076
|
-
/** 渲染提示词;必填参数必须是自身属性,且提供的已声明参数必须是字符串 */
|
|
1077
|
-
getPrompt: (name: string, args: Record<string, string>) => Promise<HaiResult<MCPPromptMessage[]>>;
|
|
1078
|
-
}
|
|
1079
|
-
|
|
1080
|
-
/**
|
|
1081
|
-
* @h-ai/ai — Persona 子功能类型
|
|
1082
|
-
*
|
|
1083
|
-
* 定义「AI 角色人格」的持久化与组合接口。Persona 解决的是「AI 是谁」的问题:
|
|
1084
|
-
* 多智能体场景(如多位专家同台访谈)中,每个 AI 有稳定的系统提示词、性格特征与
|
|
1085
|
-
* 长期人格。它与 Memory 正交——Memory 用 `objectId` / `scope` 回答「谁的记忆」,
|
|
1086
|
-
* Persona 回答「这个 AI 的身份与人设」,二者通过 `scope: { personaId }` 关联。
|
|
1087
|
-
* @module ai-persona-types
|
|
1088
|
-
*/
|
|
1089
|
-
|
|
1090
|
-
/**
|
|
1091
|
-
* Persona 档案输入(创建 / 覆盖保存时使用)
|
|
1092
|
-
*
|
|
1093
|
-
* @example
|
|
1094
|
-
* ```ts
|
|
1095
|
-
* const input: PersonaProfileInput = {
|
|
1096
|
-
* id: 'xiaop',
|
|
1097
|
-
* name: '小P',
|
|
1098
|
-
* systemPrompt: '你是一位社会学家,善于从群体行为视角分析问题。',
|
|
1099
|
-
* traits: ['谨慎', '喜欢引用真实案例'],
|
|
1100
|
-
* }
|
|
1101
|
-
* ```
|
|
1102
|
-
*/
|
|
1103
|
-
interface PersonaProfileInput {
|
|
1104
|
-
/** 角色唯一标识(业务侧稳定 ID,如 `xiaop`) */
|
|
1105
|
-
id: string;
|
|
1106
|
-
/**
|
|
1107
|
-
* 所属主体 ID(多租户隔离)
|
|
1108
|
-
*
|
|
1109
|
-
* 不同主体可创建同名角色而互不覆盖;不传时归为平台内置角色 `system`。
|
|
1110
|
-
*/
|
|
1111
|
-
objectId?: string;
|
|
1112
|
-
/** 角色显示名(如「小P」) */
|
|
1113
|
-
name?: string;
|
|
1114
|
-
/** 角色系统提示词(定义身份、职责、语气) */
|
|
1115
|
-
systemPrompt: string;
|
|
1116
|
-
/** 性格 / 风格特征(组合进系统提示词,如「谨慎」「喜欢引用案例」) */
|
|
1117
|
-
traits?: string[];
|
|
1118
|
-
/** 附加元数据 */
|
|
1119
|
-
metadata?: Record<string, unknown>;
|
|
1120
|
-
}
|
|
1121
|
-
/**
|
|
1122
|
-
* 完整的 Persona 档案
|
|
1123
|
-
*/
|
|
1124
|
-
interface PersonaProfile {
|
|
1125
|
-
/** 角色唯一标识 */
|
|
1126
|
-
id: string;
|
|
1127
|
-
/** 所属主体 ID(多租户隔离;平台内置角色为 `system`) */
|
|
1128
|
-
objectId: string;
|
|
1129
|
-
/** 角色显示名 */
|
|
1130
|
-
name?: string;
|
|
1131
|
-
/** 角色系统提示词 */
|
|
1132
|
-
systemPrompt: string;
|
|
1133
|
-
/** 性格 / 风格特征 */
|
|
1134
|
-
traits: string[];
|
|
1135
|
-
/** 附加元数据 */
|
|
1136
|
-
metadata?: Record<string, unknown>;
|
|
1137
|
-
/** 创建时间(Unix 毫秒) */
|
|
1138
|
-
createdAt: number;
|
|
1139
|
-
/** 更新时间(Unix 毫秒) */
|
|
1140
|
-
updatedAt: number;
|
|
1141
|
-
}
|
|
1142
|
-
/**
|
|
1143
|
-
* Persona 档案更新输入
|
|
1144
|
-
*
|
|
1145
|
-
* 所有字段可选,仅更新传入的字段。
|
|
1146
|
-
*/
|
|
1147
|
-
interface PersonaProfileUpdate {
|
|
1148
|
-
/** 更新显示名 */
|
|
1149
|
-
name?: string;
|
|
1150
|
-
/** 更新系统提示词 */
|
|
1151
|
-
systemPrompt?: string;
|
|
1152
|
-
/** 更新特征列表(整体替换) */
|
|
1153
|
-
traits?: string[];
|
|
1154
|
-
/** 更新元数据 */
|
|
1155
|
-
metadata?: Record<string, unknown>;
|
|
1156
|
-
}
|
|
1157
|
-
/**
|
|
1158
|
-
* Persona 操作接口(通过 `ai.persona` 访问)
|
|
1159
|
-
*
|
|
1160
|
-
* 管理 AI 角色人格的持久化与系统提示词组合。需要先调用 `ai.init()` 初始化后使用。
|
|
1161
|
-
*
|
|
1162
|
-
* @example
|
|
1163
|
-
* ```ts
|
|
1164
|
-
* // 定义一个 AI 角色
|
|
1165
|
-
* await ai.persona.save({
|
|
1166
|
-
* id: 'xiaoq',
|
|
1167
|
-
* name: '小Q',
|
|
1168
|
-
* systemPrompt: '你是一位经济学家。',
|
|
1169
|
-
* traits: ['数据驱动', '偏好长期视角'],
|
|
1170
|
-
* })
|
|
1171
|
-
*
|
|
1172
|
-
* // 组合出可直接喂给 ContextManager 的系统提示词
|
|
1173
|
-
* const composed = await ai.persona.compose('xiaoq')
|
|
1174
|
-
* const manager = ai.context.createManager({
|
|
1175
|
-
* systemPrompt: composed.data,
|
|
1176
|
-
* // 该角色的长期记忆用 scope 关联
|
|
1177
|
-
* memory: { enable: true, enableExtract: true, scope: { personaId: 'xiaoq' } },
|
|
1178
|
-
* })
|
|
1179
|
-
* ```
|
|
1180
|
-
*/
|
|
1181
|
-
interface PersonaOperations {
|
|
1182
|
-
/**
|
|
1183
|
-
* 创建或覆盖保存一个角色档案(upsert 语义)
|
|
1184
|
-
*
|
|
1185
|
-
* @param profile - 角色档案输入
|
|
1186
|
-
* @returns 保存后的完整档案
|
|
1187
|
-
*/
|
|
1188
|
-
save: (profile: PersonaProfileInput) => Promise<HaiResult<PersonaProfile>>;
|
|
1189
|
-
/**
|
|
1190
|
-
* 按 ID 获取角色档案
|
|
1191
|
-
*
|
|
1192
|
-
* @param id - 角色 ID
|
|
1193
|
-
* @returns 角色档案,不存在时返回 PERSONA_NOT_FOUND
|
|
1194
|
-
*/
|
|
1195
|
-
get: (id: string, options?: PersonaScopeOptions) => Promise<HaiResult<PersonaProfile>>;
|
|
1196
|
-
/**
|
|
1197
|
-
* 更新角色档案(仅更新传入字段)
|
|
1198
|
-
*
|
|
1199
|
-
* @param id - 角色 ID
|
|
1200
|
-
* @param updates - 需要更新的字段
|
|
1201
|
-
* @param options - 主体作用域(默认 `system`)
|
|
1202
|
-
* @returns 更新后的完整档案
|
|
1203
|
-
*/
|
|
1204
|
-
update: (id: string, updates: PersonaProfileUpdate, options?: PersonaScopeOptions) => Promise<HaiResult<PersonaProfile>>;
|
|
1205
|
-
/**
|
|
1206
|
-
* 删除角色档案
|
|
1207
|
-
*
|
|
1208
|
-
* @param id - 角色 ID
|
|
1209
|
-
* @param options - 主体作用域(默认 `system`)
|
|
1210
|
-
* @returns 成功返回 ok(undefined)
|
|
1211
|
-
*/
|
|
1212
|
-
remove: (id: string, options?: PersonaScopeOptions) => Promise<HaiResult<void>>;
|
|
1213
|
-
/**
|
|
1214
|
-
* 列出指定主体的角色档案
|
|
1215
|
-
*
|
|
1216
|
-
* @param options - 主体作用域(默认 `system`)
|
|
1217
|
-
* @returns 角色档案列表
|
|
1218
|
-
*/
|
|
1219
|
-
list: (options?: PersonaScopeOptions) => Promise<HaiResult<PersonaProfile[]>>;
|
|
1220
|
-
/**
|
|
1221
|
-
* 组合角色的完整系统提示词
|
|
1222
|
-
*
|
|
1223
|
-
* 将 `systemPrompt` 与 `traits` 拼装为一段可直接作为 system 消息的文本,
|
|
1224
|
-
* 供 `ai.context.createManager({ systemPrompt })` 使用。
|
|
1225
|
-
*
|
|
1226
|
-
* @param id - 角色 ID
|
|
1227
|
-
* @param options - 主体作用域(默认 `system`)
|
|
1228
|
-
* @returns 组合后的系统提示词,角色不存在时返回 PERSONA_NOT_FOUND
|
|
1229
|
-
*/
|
|
1230
|
-
compose: (id: string, options?: PersonaScopeOptions) => Promise<HaiResult<string>>;
|
|
1231
|
-
}
|
|
1232
|
-
/** Persona 操作的主体作用域选项 */
|
|
1233
|
-
interface PersonaScopeOptions {
|
|
1234
|
-
/** 所属主体 ID(不传时归为平台内置角色 `system`) */
|
|
1235
|
-
objectId?: string;
|
|
1236
|
-
}
|
|
1237
|
-
|
|
1238
|
-
/**
|
|
1239
|
-
* @h-ai/ai — Rerank 子功能类型
|
|
1240
|
-
*
|
|
1241
|
-
* 定义文档重排序操作的类型接口。
|
|
1242
|
-
* @module ai-rerank-types
|
|
1243
|
-
*/
|
|
1244
|
-
|
|
1245
|
-
/**
|
|
1246
|
-
* 单条待排序文档
|
|
1247
|
-
*/
|
|
1248
|
-
interface RerankDocument {
|
|
1249
|
-
/** 文档唯一标识(可选,便于结果追踪) */
|
|
1250
|
-
id?: string;
|
|
1251
|
-
/** 文档文本内容 */
|
|
1252
|
-
text: string;
|
|
1253
|
-
}
|
|
1254
|
-
/**
|
|
1255
|
-
* Rerank 请求参数
|
|
1256
|
-
*/
|
|
1257
|
-
interface RerankRequest {
|
|
1258
|
-
/** 查询文本 */
|
|
1259
|
-
query: string;
|
|
1260
|
-
/** 待排序文档列表(字符串数组或带 id 的文档对象数组) */
|
|
1261
|
-
documents: string[] | RerankDocument[];
|
|
1262
|
-
/** 模型名称(可选,未指定时使用配置中的默认模型) */
|
|
1263
|
-
model?: string;
|
|
1264
|
-
/** 返回结果数量(可选,默认返回全部文档) */
|
|
1265
|
-
topN?: number;
|
|
1266
|
-
/** 是否在结果中返回文档原文(默认 false) */
|
|
1267
|
-
returnDocuments?: boolean;
|
|
1268
|
-
}
|
|
1269
|
-
/**
|
|
1270
|
-
* 单条 Rerank 结果
|
|
1271
|
-
*/
|
|
1272
|
-
interface RerankItem {
|
|
1273
|
-
/** 文档在原始输入中的索引 */
|
|
1274
|
-
index: number;
|
|
1275
|
-
/** 文档 ID(当输入为带 id 的文档对象时) */
|
|
1276
|
-
id?: string;
|
|
1277
|
-
/** 相关性分数(越高越相关) */
|
|
1278
|
-
relevanceScore: number;
|
|
1279
|
-
/** 文档原文(当 returnDocuments 为 true 时) */
|
|
1280
|
-
document?: string;
|
|
1281
|
-
}
|
|
1282
|
-
/**
|
|
1283
|
-
* Rerank 响应
|
|
1284
|
-
*/
|
|
1285
|
-
interface RerankResponse {
|
|
1286
|
-
/** 使用的模型名称 */
|
|
1287
|
-
model: string;
|
|
1288
|
-
/** 重排序结果列表(按相关性分数降序排列) */
|
|
1289
|
-
results: RerankItem[];
|
|
1290
|
-
}
|
|
1291
|
-
/**
|
|
1292
|
-
* Rerank 操作接口(通过 `ai.rerank` 访问)
|
|
1293
|
-
*
|
|
1294
|
-
* 需要先调用 `ai.init()` 初始化后使用。
|
|
1295
|
-
* 通过专用 Rerank API(兼容 Cohere 格式)对文档进行相关性重排序。
|
|
1296
|
-
*
|
|
1297
|
-
* @example
|
|
1298
|
-
* ```ts
|
|
1299
|
-
* // 对检索结果重排序
|
|
1300
|
-
* const result = await ai.rerank.rerank({
|
|
1301
|
-
* query: '机器学习入门',
|
|
1302
|
-
* documents: [
|
|
1303
|
-
* '深度学习是机器学习的一个分支',
|
|
1304
|
-
* '今天天气真好',
|
|
1305
|
-
* '神经网络是深度学习的基础',
|
|
1306
|
-
* ],
|
|
1307
|
-
* topN: 2,
|
|
1308
|
-
* })
|
|
1309
|
-
*
|
|
1310
|
-
* // 便捷方法:直接传入文本数组
|
|
1311
|
-
* const items = await ai.rerank.rerankTexts('query', ['doc1', 'doc2'])
|
|
1312
|
-
* ```
|
|
1313
|
-
*/
|
|
1314
|
-
interface RerankOperations {
|
|
1315
|
-
/**
|
|
1316
|
-
* 对文档列表进行相关性重排序
|
|
1317
|
-
*
|
|
1318
|
-
* @param request - Rerank 请求
|
|
1319
|
-
* @returns 重排序响应,结果按相关性分数降序排列
|
|
1320
|
-
*/
|
|
1321
|
-
rerank: (request: RerankRequest) => Promise<HaiResult<RerankResponse>>;
|
|
1322
|
-
/**
|
|
1323
|
-
* 便捷方法:对文本数组重排序,直接返回结果列表
|
|
1324
|
-
*
|
|
1325
|
-
* @param query - 查询文本
|
|
1326
|
-
* @param texts - 待排序文本列表
|
|
1327
|
-
* @param topN - 返回条数(可选)
|
|
1328
|
-
* @returns RerankItem 列表,按相关性分数降序排列
|
|
1329
|
-
*/
|
|
1330
|
-
rerankTexts: (query: string, texts: string[], topN?: number) => Promise<HaiResult<RerankItem[]>>;
|
|
1331
|
-
}
|
|
1332
|
-
|
|
1333
|
-
/**
|
|
1334
|
-
* @h-ai/ai — Token 子功能类型
|
|
1335
|
-
*
|
|
1336
|
-
* 定义 Token 估算操作的类型接口:文本与消息级别的 Token 数估算。
|
|
1337
|
-
* 使用 CJK 感知的字符级估算算法,无需外部分词库依赖。
|
|
1338
|
-
* @module ai-token-types
|
|
1339
|
-
*/
|
|
1340
|
-
|
|
1341
|
-
/**
|
|
1342
|
-
* Token 操作接口
|
|
1343
|
-
*
|
|
1344
|
-
* 提供 Token 估算能力,使用配置的 `tokenRatio` 做估算。
|
|
1345
|
-
*
|
|
1346
|
-
* @example
|
|
1347
|
-
* ```ts
|
|
1348
|
-
* const tokens = tokenOps.estimateText('Hello 世界')
|
|
1349
|
-
* const total = tokenOps.estimateMessages(messages)
|
|
1350
|
-
* ```
|
|
1351
|
-
*/
|
|
1352
|
-
interface TokenOperations {
|
|
1353
|
-
/**
|
|
1354
|
-
* 估算单条文本的 Token 数
|
|
1355
|
-
*
|
|
1356
|
-
* 使用字符级估算:中文约每字 1.5 token,英文约每 4 字符 1 token。
|
|
1357
|
-
*
|
|
1358
|
-
* @param text - 文本内容
|
|
1359
|
-
* @returns 估算 Token 数
|
|
1360
|
-
*/
|
|
1361
|
-
estimateText: (text: string) => number;
|
|
1362
|
-
/**
|
|
1363
|
-
* 估算消息列表的总 Token 数
|
|
1364
|
-
*
|
|
1365
|
-
* 包含消息结构开销(每条消息约 4 token 用于角色标记和分隔)。
|
|
1366
|
-
*
|
|
1367
|
-
* @param messages - 消息列表
|
|
1368
|
-
* @returns 估算 Token 总数
|
|
1369
|
-
*/
|
|
1370
|
-
estimateMessages: (messages: ChatMessage[]) => number;
|
|
1371
|
-
}
|
|
1372
|
-
|
|
1373
|
-
/**
|
|
1374
|
-
* AI 模块标准错误定义对象。
|
|
1375
|
-
*
|
|
1376
|
-
* 通过 `HaiAIError.ERROR_CODE_NAME` 访问具体错误定义,如:
|
|
1377
|
-
* ```ts
|
|
1378
|
-
* const def = HaiAIError.NOT_INITIALIZED
|
|
1379
|
-
* // => { code: 'hai:ai:010', httpStatus: 500, system: 'hai', module: 'ai' }
|
|
1380
|
-
* ```
|
|
1381
|
-
*/
|
|
1382
|
-
declare const HaiAIError: {
|
|
1383
|
-
readonly INTERNAL_ERROR: _h_ai_core.HaiErrorDef;
|
|
1384
|
-
readonly NOT_INITIALIZED: _h_ai_core.HaiErrorDef;
|
|
1385
|
-
readonly CONFIGURATION_ERROR: _h_ai_core.HaiErrorDef;
|
|
1386
|
-
readonly INIT_IN_PROGRESS: _h_ai_core.HaiErrorDef;
|
|
1387
|
-
readonly RERANK_API_ERROR: _h_ai_core.HaiErrorDef;
|
|
1388
|
-
readonly RERANK_INVALID_REQUEST: _h_ai_core.HaiErrorDef;
|
|
1389
|
-
readonly API_ERROR: _h_ai_core.HaiErrorDef;
|
|
1390
|
-
readonly INVALID_REQUEST: _h_ai_core.HaiErrorDef;
|
|
1391
|
-
readonly RATE_LIMITED: _h_ai_core.HaiErrorDef;
|
|
1392
|
-
readonly TIMEOUT: _h_ai_core.HaiErrorDef;
|
|
1393
|
-
readonly MODEL_NOT_FOUND: _h_ai_core.HaiErrorDef;
|
|
1394
|
-
readonly CONTEXT_LENGTH_EXCEEDED: _h_ai_core.HaiErrorDef;
|
|
1395
|
-
readonly LLM_RECORD_FAILED: _h_ai_core.HaiErrorDef;
|
|
1396
|
-
readonly LLM_HISTORY_FAILED: _h_ai_core.HaiErrorDef;
|
|
1397
|
-
readonly MCP_CONNECTION_ERROR: _h_ai_core.HaiErrorDef;
|
|
1398
|
-
readonly MCP_PROTOCOL_ERROR: _h_ai_core.HaiErrorDef;
|
|
1399
|
-
readonly MCP_TOOL_ERROR: _h_ai_core.HaiErrorDef;
|
|
1400
|
-
readonly MCP_RESOURCE_ERROR: _h_ai_core.HaiErrorDef;
|
|
1401
|
-
readonly MCP_SERVER_ERROR: _h_ai_core.HaiErrorDef;
|
|
1402
|
-
readonly EMBEDDING_API_ERROR: _h_ai_core.HaiErrorDef;
|
|
1403
|
-
readonly EMBEDDING_MODEL_NOT_FOUND: _h_ai_core.HaiErrorDef;
|
|
1404
|
-
readonly EMBEDDING_INPUT_TOO_LONG: _h_ai_core.HaiErrorDef;
|
|
1405
|
-
readonly TOOL_NOT_FOUND: _h_ai_core.HaiErrorDef;
|
|
1406
|
-
readonly TOOL_VALIDATION_FAILED: _h_ai_core.HaiErrorDef;
|
|
1407
|
-
readonly TOOL_EXECUTION_FAILED: _h_ai_core.HaiErrorDef;
|
|
1408
|
-
readonly TOOL_TIMEOUT: _h_ai_core.HaiErrorDef;
|
|
1409
|
-
readonly TOOL_ALREADY_REGISTERED: _h_ai_core.HaiErrorDef;
|
|
1410
|
-
readonly TOOL_FORBIDDEN: _h_ai_core.HaiErrorDef;
|
|
1411
|
-
readonly REASONING_FAILED: _h_ai_core.HaiErrorDef;
|
|
1412
|
-
readonly REASONING_MAX_ROUNDS: _h_ai_core.HaiErrorDef;
|
|
1413
|
-
readonly REASONING_STRATEGY_NOT_FOUND: _h_ai_core.HaiErrorDef;
|
|
1414
|
-
readonly RETRIEVAL_FAILED: _h_ai_core.HaiErrorDef;
|
|
1415
|
-
readonly RETRIEVAL_SOURCE_NOT_FOUND: _h_ai_core.HaiErrorDef;
|
|
1416
|
-
readonly RAG_FAILED: _h_ai_core.HaiErrorDef;
|
|
1417
|
-
readonly RAG_CONTEXT_BUILD_FAILED: _h_ai_core.HaiErrorDef;
|
|
1418
|
-
readonly KNOWLEDGE_SETUP_FAILED: _h_ai_core.HaiErrorDef;
|
|
1419
|
-
readonly KNOWLEDGE_INGEST_FAILED: _h_ai_core.HaiErrorDef;
|
|
1420
|
-
readonly KNOWLEDGE_RETRIEVE_FAILED: _h_ai_core.HaiErrorDef;
|
|
1421
|
-
readonly KNOWLEDGE_ENTITY_EXTRACT_FAILED: _h_ai_core.HaiErrorDef;
|
|
1422
|
-
readonly KNOWLEDGE_NOT_SETUP: _h_ai_core.HaiErrorDef;
|
|
1423
|
-
readonly KNOWLEDGE_COLLECTION_NOT_FOUND: _h_ai_core.HaiErrorDef;
|
|
1424
|
-
readonly MEMORY_EXTRACT_FAILED: _h_ai_core.HaiErrorDef;
|
|
1425
|
-
readonly MEMORY_STORE_FAILED: _h_ai_core.HaiErrorDef;
|
|
1426
|
-
readonly MEMORY_RECALL_FAILED: _h_ai_core.HaiErrorDef;
|
|
1427
|
-
readonly MEMORY_NOT_FOUND: _h_ai_core.HaiErrorDef;
|
|
1428
|
-
readonly MEMORY_ENRICH_FAILED: _h_ai_core.HaiErrorDef;
|
|
1429
|
-
readonly MEMORY_PROMOTE_FAILED: _h_ai_core.HaiErrorDef;
|
|
1430
|
-
readonly PERSONA_NOT_FOUND: _h_ai_core.HaiErrorDef;
|
|
1431
|
-
readonly PERSONA_SAVE_FAILED: _h_ai_core.HaiErrorDef;
|
|
1432
|
-
readonly FILE_PARSE_FAILED: _h_ai_core.HaiErrorDef;
|
|
1433
|
-
readonly FILE_UNSUPPORTED_FORMAT: _h_ai_core.HaiErrorDef;
|
|
1434
|
-
readonly FILE_OCR_FAILED: _h_ai_core.HaiErrorDef;
|
|
1435
|
-
readonly FILE_INVALID_CONTENT: _h_ai_core.HaiErrorDef;
|
|
1436
|
-
readonly AUDIO_INVALID_REQUEST: _h_ai_core.HaiErrorDef;
|
|
1437
|
-
readonly AUDIO_MODEL_NOT_FOUND: _h_ai_core.HaiErrorDef;
|
|
1438
|
-
readonly AUDIO_PROVIDER_NOT_FOUND: _h_ai_core.HaiErrorDef;
|
|
1439
|
-
readonly AUDIO_UNSUPPORTED_INPUT: _h_ai_core.HaiErrorDef;
|
|
1440
|
-
readonly AUDIO_UPSTREAM_ERROR: _h_ai_core.HaiErrorDef;
|
|
1441
|
-
readonly AUDIO_PROTOCOL_ERROR: _h_ai_core.HaiErrorDef;
|
|
1442
|
-
readonly AUDIO_CONNECTION_FAILED: _h_ai_core.HaiErrorDef;
|
|
1443
|
-
readonly AUDIO_TIMEOUT: _h_ai_core.HaiErrorDef;
|
|
1444
|
-
readonly AUDIO_INPUT_TOO_LARGE: _h_ai_core.HaiErrorDef;
|
|
1445
|
-
readonly AUDIO_CANCELLED: _h_ai_core.HaiErrorDef;
|
|
1446
|
-
readonly IMAGE_INVALID_REQUEST: _h_ai_core.HaiErrorDef;
|
|
1447
|
-
readonly IMAGE_MODEL_NOT_FOUND: _h_ai_core.HaiErrorDef;
|
|
1448
|
-
readonly IMAGE_PROVIDER_NOT_FOUND: _h_ai_core.HaiErrorDef;
|
|
1449
|
-
readonly IMAGE_UPSTREAM_ERROR: _h_ai_core.HaiErrorDef;
|
|
1450
|
-
readonly IMAGE_PROTOCOL_ERROR: _h_ai_core.HaiErrorDef;
|
|
1451
|
-
readonly IMAGE_CANCELLED: _h_ai_core.HaiErrorDef;
|
|
1452
|
-
readonly CONTEXT_COMPRESS_FAILED: _h_ai_core.HaiErrorDef;
|
|
1453
|
-
readonly CONTEXT_SUMMARIZE_FAILED: _h_ai_core.HaiErrorDef;
|
|
1454
|
-
readonly CONTEXT_TOKEN_ESTIMATE_FAILED: _h_ai_core.HaiErrorDef;
|
|
1455
|
-
readonly CONTEXT_BUDGET_EXCEEDED: _h_ai_core.HaiErrorDef;
|
|
1456
|
-
readonly CONTEXT_TURN_NOT_FOUND: _h_ai_core.HaiErrorDef;
|
|
1457
|
-
readonly CONTEXT_TURN_INVALID_STATE: _h_ai_core.HaiErrorDef;
|
|
1458
|
-
readonly CONTEXT_BUSY: _h_ai_core.HaiErrorDef;
|
|
1459
|
-
readonly STORE_FAILED: _h_ai_core.HaiErrorDef;
|
|
1460
|
-
readonly STORE_NOT_AVAILABLE: _h_ai_core.HaiErrorDef;
|
|
1461
|
-
readonly SESSION_NOT_FOUND: _h_ai_core.HaiErrorDef;
|
|
1462
|
-
readonly SESSION_FAILED: _h_ai_core.HaiErrorDef;
|
|
1463
|
-
readonly A2A_NOT_CONFIGURED: _h_ai_core.HaiErrorDef;
|
|
1464
|
-
readonly A2A_HANDLE_FAILED: _h_ai_core.HaiErrorDef;
|
|
1465
|
-
readonly A2A_REMOTE_CALL_FAILED: _h_ai_core.HaiErrorDef;
|
|
1466
|
-
readonly A2A_AUTH_FAILED: _h_ai_core.HaiErrorDef;
|
|
1467
|
-
readonly A2A_LIST_MESSAGES_FAILED: _h_ai_core.HaiErrorDef;
|
|
1468
|
-
};
|
|
1469
|
-
/**
|
|
1470
|
-
* AI 初始化运行时选项
|
|
1471
|
-
*
|
|
1472
|
-
* 用于传入运行时对象(如自定义 StoreProvider),
|
|
1473
|
-
* 这些对象无法通过 Zod 配置 Schema 传递。
|
|
1474
|
-
*/
|
|
1475
|
-
interface AIInitOptions {
|
|
1476
|
-
/**
|
|
1477
|
-
* 自定义存储 Provider
|
|
1478
|
-
*
|
|
1479
|
-
* 提供后 AI 模块将使用此 Provider,而不采用自动选择的存储实现。
|
|
1480
|
-
* 未提供时:reldb + vecdb 已初始化则使用持久化 DB Provider,否则使用进程内临时 Provider。
|
|
1481
|
-
*/
|
|
1482
|
-
storeProvider?: AIStoreProvider;
|
|
1483
|
-
}
|
|
1484
|
-
/**
|
|
1485
|
-
* AI 服务接口(通过 `ai` 对象访问)
|
|
1486
|
-
*
|
|
1487
|
-
* 所有 AI 功能的统一入口,需先调用 `init()` 初始化后才能使用 `llm`、`mcp` 操作。
|
|
1488
|
-
* `tools` 和 `stream` 为纯函数,无需初始化即可使用。
|
|
1489
|
-
*
|
|
1490
|
-
* @example
|
|
1491
|
-
* ```ts
|
|
1492
|
-
* import { ai } from '@h-ai/ai'
|
|
1493
|
-
*
|
|
1494
|
-
* await ai.init({ llm: { model: 'gpt-4o-mini' } })
|
|
1495
|
-
* const result = await ai.llm.chat({ messages: [{ role: 'user', content: '你好' }] })
|
|
1496
|
-
* await ai.close()
|
|
1497
|
-
* ```
|
|
1498
|
-
*/
|
|
1499
|
-
interface AIFunctions {
|
|
1500
|
-
/**
|
|
1501
|
-
* 初始化 AI 服务
|
|
1502
|
-
*
|
|
1503
|
-
* 使用 Zod Schema 校验配置,失败返回 `CONFIGURATION_ERROR`。
|
|
1504
|
-
* 重复调用会先关闭旧实例再重新初始化。
|
|
1505
|
-
*
|
|
1506
|
-
* @param config - AI 配置(可选,默认使用空对象并应用 Schema 默认值)
|
|
1507
|
-
* @param options - 运行时选项(可选,用于传入自定义 StoreProvider 等运行时对象)
|
|
1508
|
-
* @returns 成功返回 `ok(undefined)`;配置校验失败返回 `err(HaiAIError.CONFIGURATION_ERROR)`
|
|
1509
|
-
*/
|
|
1510
|
-
init: (config?: AIConfigInput, options?: AIInitOptions) => Promise<HaiResult<void>>;
|
|
1511
|
-
/**
|
|
1512
|
-
* 关闭 AI 服务,释放内部状态
|
|
1513
|
-
*
|
|
1514
|
-
* 关闭后 `llm`、`mcp` 操作将返回 `NOT_INITIALIZED` 错误。
|
|
1515
|
-
* 重复关闭不会报错。
|
|
1516
|
-
*/
|
|
1517
|
-
close: () => Promise<void>;
|
|
1518
|
-
/** 当前脱敏后的配置快照(未初始化时为 `null`) */
|
|
1519
|
-
readonly config: AIConfig | null;
|
|
1520
|
-
/** 是否已初始化(`init()` 成功后为 `true`,`close()` 后为 `false`) */
|
|
1521
|
-
readonly isInitialized: boolean;
|
|
1522
|
-
/** LLM 操作(聊天、流式、模型列表),需要先调用 `init()` */
|
|
1523
|
-
readonly llm: LLMOperations;
|
|
1524
|
-
/** MCP 操作(工具/资源/提示词注册与调用),需要先调用 `init()` */
|
|
1525
|
-
readonly mcp: MCPOperations;
|
|
1526
|
-
/** 工具操作(定义工具与注册表),纯函数,无需初始化 */
|
|
1527
|
-
readonly tools: ToolsOperations;
|
|
1528
|
-
/** 流处理操作(流处理器、SSE 编解码),纯函数,无需初始化 */
|
|
1529
|
-
readonly stream: StreamOperations;
|
|
1530
|
-
/** Embedding 操作(向量嵌入),需要先调用 `init()` */
|
|
1531
|
-
readonly embedding: EmbeddingOperations;
|
|
1532
|
-
/** 推理操作(ReAct / CoT / Plan-Execute),需要先调用 `init()` */
|
|
1533
|
-
readonly reasoning: ReasoningOperations;
|
|
1534
|
-
/** 检索操作(向量检索),需要先调用 `init()` */
|
|
1535
|
-
readonly retrieval: RetrievalOperations;
|
|
1536
|
-
/** RAG 操作(检索增强生成),需要先调用 `init()` */
|
|
1537
|
-
readonly rag: RagOperations;
|
|
1538
|
-
/** Knowledge 操作(知识库管理与检索),需要先调用 `init()` 和 `knowledge.setup()` */
|
|
1539
|
-
readonly knowledge: KnowledgeOperations;
|
|
1540
|
-
/** Memory 操作(记忆提取、存储、检索、注入),需要先调用 `init()` */
|
|
1541
|
-
readonly memory: MemoryOperations;
|
|
1542
|
-
/** Persona 操作(AI 角色人格档案与系统提示词组合),需要先调用 `init()` */
|
|
1543
|
-
readonly persona: PersonaOperations;
|
|
1544
|
-
/** Token 操作(Token 估算),需要先调用 `init()` */
|
|
1545
|
-
readonly token: TokenOperations;
|
|
1546
|
-
/** Summary 操作(消息摘要生成),需要先调用 `init()` */
|
|
1547
|
-
readonly summary: SummaryOperations;
|
|
1548
|
-
/** Compress 操作(上下文压缩),需要先调用 `init()` */
|
|
1549
|
-
readonly compress: CompressOperations;
|
|
1550
|
-
/** Context 操作(有状态上下文管理器,编排 LLM + Memory + RAG + Reasoning),需要先调用 `init()` */
|
|
1551
|
-
readonly context: ContextOperations;
|
|
1552
|
-
/** Rerank 操作(文档重排序),需要先调用 `init()` */
|
|
1553
|
-
readonly rerank: RerankOperations;
|
|
1554
|
-
/** File 操作(文件内容解析),需要先调用 `init()` */
|
|
1555
|
-
readonly file: FileOperations;
|
|
1556
|
-
/** A2A 操作(Agent-to-Agent 协议),需要先调用 `init()` 并配置 `a2a` */
|
|
1557
|
-
readonly a2a: A2AOperations;
|
|
1558
|
-
/** Audio 操作(语音识别 / 语音合成),需要先调用 `init()` 并配置 `audio` */
|
|
1559
|
-
readonly audio: AudioOperations;
|
|
1560
|
-
/** Image 操作(文生图),需要先调用 `init()` 并配置 `image` */
|
|
1561
|
-
readonly image: ImageOperations;
|
|
1562
|
-
}
|
|
1563
|
-
|
|
1564
|
-
/** @h-ai/ai — 音频播放负载序列化 */
|
|
1565
|
-
|
|
1566
|
-
/**
|
|
1567
|
-
* 将标准音频内容转换为浏览器可直接播放的 Base64 负载。
|
|
1568
|
-
*
|
|
1569
|
-
* `synthesize` 只负责统一供应商输出的字节与格式;裸 PCM 没有容器头,
|
|
1570
|
-
* 因此这里负责补齐 WAV 头。MP3/WAV 则只进行 Base64 编码,不重新封装。
|
|
1571
|
-
*/
|
|
1572
|
-
declare function serializePlayableAudio(audio: AudioContent): HaiResult<PlayableAudio>;
|
|
1573
|
-
|
|
1574
|
-
/** 统一语音入口的默认路径(相对 API 前缀) */
|
|
1575
|
-
declare const AUDIO_WS_PATH = "/ai/audio";
|
|
1576
|
-
/**
|
|
1577
|
-
* 会话起始消息(客户端首个 JSON 帧)
|
|
1578
|
-
*
|
|
1579
|
-
* 表达本次语音操作:识别或合成,及可选的模型 / 语言 / 音色 / 格式等参数。
|
|
1580
|
-
*/
|
|
1581
|
-
interface AudioWsStartMessage {
|
|
1582
|
-
type: 'start';
|
|
1583
|
-
/** 操作类型 */
|
|
1584
|
-
operation: 'transcribe' | 'synthesize';
|
|
1585
|
-
/**
|
|
1586
|
-
* 是否流式返回增量结果
|
|
1587
|
-
*
|
|
1588
|
-
* 识别操作:`true` 时服务端桥接为持续音频输入并流式返回临时结果;
|
|
1589
|
-
* `false`(默认)时服务端缓冲完整音频后返回单条最终结果。
|
|
1590
|
-
*/
|
|
1591
|
-
stream?: boolean;
|
|
1592
|
-
/** 模型 ID(不传时使用服务端默认模型) */
|
|
1593
|
-
model?: string;
|
|
1594
|
-
/** 识别语言提示 */
|
|
1595
|
-
language?: string;
|
|
1596
|
-
/** 领域提示词 / 热词(识别) */
|
|
1597
|
-
contextHints?: string[];
|
|
1598
|
-
/** 合成音色 */
|
|
1599
|
-
voice?: string;
|
|
1600
|
-
/** 合成自然语言风格指令 */
|
|
1601
|
-
instruction?: string;
|
|
1602
|
-
/** 音频格式(识别时为输入格式,合成时为输出格式) */
|
|
1603
|
-
format?: AudioFormat;
|
|
1604
|
-
/** 采样率 */
|
|
1605
|
-
sampleRate?: number;
|
|
1606
|
-
/** 声道数 */
|
|
1607
|
-
channels?: 1 | 2;
|
|
1608
|
-
}
|
|
1609
|
-
/** 文本输入帧(合成操作时携带待合成文本) */
|
|
1610
|
-
interface AudioWsTextMessage {
|
|
1611
|
-
type: 'text';
|
|
1612
|
-
/** 调用方分配的稳定文本段 ID */
|
|
1613
|
-
segmentId: string;
|
|
1614
|
-
/** 待合成文本片段 */
|
|
1615
|
-
text: string;
|
|
1616
|
-
}
|
|
1617
|
-
/** 输入结束帧(音频 / 文本输入全部发送完毕) */
|
|
1618
|
-
interface AudioWsDoneMessage {
|
|
1619
|
-
type: 'done';
|
|
1620
|
-
}
|
|
1621
|
-
/** 客户端 JSON 控制消息(音频输入以二进制帧发送,不走 JSON) */
|
|
1622
|
-
type AudioWsClientMessage = AudioWsStartMessage | AudioWsTextMessage | AudioWsDoneMessage;
|
|
1623
|
-
/** 合法音频格式 */
|
|
1624
|
-
declare const AudioFormatSchema: zod.ZodEnum<{
|
|
1625
|
-
pcm16: "pcm16";
|
|
1626
|
-
wav: "wav";
|
|
1627
|
-
mp3: "mp3";
|
|
1628
|
-
opus: "opus";
|
|
1629
|
-
}>;
|
|
1630
|
-
/** 单个字符串控制字段最大长度(model / language / voice / instruction 等) */
|
|
1631
|
-
/** 会话起始帧 Schema */
|
|
1632
|
-
declare const AudioWsStartMessageSchema: zod.ZodObject<{
|
|
1633
|
-
type: zod.ZodLiteral<"start">;
|
|
1634
|
-
operation: zod.ZodEnum<{
|
|
1635
|
-
transcribe: "transcribe";
|
|
1636
|
-
synthesize: "synthesize";
|
|
1637
|
-
}>;
|
|
1638
|
-
stream: zod.ZodOptional<zod.ZodBoolean>;
|
|
1639
|
-
model: zod.ZodOptional<zod.ZodString>;
|
|
1640
|
-
language: zod.ZodOptional<zod.ZodString>;
|
|
1641
|
-
contextHints: zod.ZodOptional<zod.ZodArray<zod.ZodString>>;
|
|
1642
|
-
voice: zod.ZodOptional<zod.ZodString>;
|
|
1643
|
-
instruction: zod.ZodOptional<zod.ZodString>;
|
|
1644
|
-
format: zod.ZodOptional<zod.ZodEnum<{
|
|
1645
|
-
pcm16: "pcm16";
|
|
1646
|
-
wav: "wav";
|
|
1647
|
-
mp3: "mp3";
|
|
1648
|
-
opus: "opus";
|
|
1649
|
-
}>>;
|
|
1650
|
-
sampleRate: zod.ZodOptional<zod.ZodNumber>;
|
|
1651
|
-
channels: zod.ZodOptional<zod.ZodUnion<readonly [zod.ZodLiteral<1>, zod.ZodLiteral<2>]>>;
|
|
1652
|
-
}, zod_v4_core.$strip>;
|
|
1653
|
-
/** 文本输入帧 Schema(segmentId 非空且长度受限) */
|
|
1654
|
-
declare const AudioWsTextMessageSchema: zod.ZodObject<{
|
|
1655
|
-
type: zod.ZodLiteral<"text">;
|
|
1656
|
-
segmentId: zod.ZodString;
|
|
1657
|
-
text: zod.ZodString;
|
|
1658
|
-
}, zod_v4_core.$strip>;
|
|
1659
|
-
/** 输入结束帧 Schema */
|
|
1660
|
-
declare const AudioWsDoneMessageSchema: zod.ZodObject<{
|
|
1661
|
-
type: zod.ZodLiteral<"done">;
|
|
1662
|
-
}, zod_v4_core.$strip>;
|
|
1663
|
-
/** 客户端 JSON 控制消息 Schema(按 type 判别) */
|
|
1664
|
-
declare const AudioWsClientMessageSchema: zod.ZodDiscriminatedUnion<[zod.ZodObject<{
|
|
1665
|
-
type: zod.ZodLiteral<"start">;
|
|
1666
|
-
operation: zod.ZodEnum<{
|
|
1667
|
-
transcribe: "transcribe";
|
|
1668
|
-
synthesize: "synthesize";
|
|
1669
|
-
}>;
|
|
1670
|
-
stream: zod.ZodOptional<zod.ZodBoolean>;
|
|
1671
|
-
model: zod.ZodOptional<zod.ZodString>;
|
|
1672
|
-
language: zod.ZodOptional<zod.ZodString>;
|
|
1673
|
-
contextHints: zod.ZodOptional<zod.ZodArray<zod.ZodString>>;
|
|
1674
|
-
voice: zod.ZodOptional<zod.ZodString>;
|
|
1675
|
-
instruction: zod.ZodOptional<zod.ZodString>;
|
|
1676
|
-
format: zod.ZodOptional<zod.ZodEnum<{
|
|
1677
|
-
pcm16: "pcm16";
|
|
1678
|
-
wav: "wav";
|
|
1679
|
-
mp3: "mp3";
|
|
1680
|
-
opus: "opus";
|
|
1681
|
-
}>>;
|
|
1682
|
-
sampleRate: zod.ZodOptional<zod.ZodNumber>;
|
|
1683
|
-
channels: zod.ZodOptional<zod.ZodUnion<readonly [zod.ZodLiteral<1>, zod.ZodLiteral<2>]>>;
|
|
1684
|
-
}, zod_v4_core.$strip>, zod.ZodObject<{
|
|
1685
|
-
type: zod.ZodLiteral<"text">;
|
|
1686
|
-
segmentId: zod.ZodString;
|
|
1687
|
-
text: zod.ZodString;
|
|
1688
|
-
}, zod_v4_core.$strip>, zod.ZodObject<{
|
|
1689
|
-
type: zod.ZodLiteral<"done">;
|
|
1690
|
-
}, zod_v4_core.$strip>], "type">;
|
|
1691
|
-
/** 语音起止事件(识别操作时服务端 VAD 检测到语音开始 / 结束) */
|
|
1692
|
-
interface AudioWsSpeechMessage {
|
|
1693
|
-
type: 'speech_started' | 'speech_stopped';
|
|
1694
|
-
}
|
|
1695
|
-
/** 识别结果帧(识别操作时返回当前语句的完整文本) */
|
|
1696
|
-
interface AudioWsTranscriptMessage {
|
|
1697
|
-
type: 'transcript';
|
|
1698
|
-
/** 当前语句的完整识别文本 */
|
|
1699
|
-
text: string;
|
|
1700
|
-
/** 是否为该语句的最终结果 */
|
|
1701
|
-
final: boolean;
|
|
1702
|
-
}
|
|
1703
|
-
/**
|
|
1704
|
-
* 合成文本段开始;后续二进制帧均属于该段,直到收到对应的 `segment_done`。
|
|
1705
|
-
*
|
|
1706
|
-
* 携带服务端解析 Provider 后的真实输出音频参数,供浏览器正确标注音频格式。
|
|
1707
|
-
*/
|
|
1708
|
-
interface AudioWsSegmentStartedMessage {
|
|
1709
|
-
type: 'segment_started';
|
|
1710
|
-
segmentId: string;
|
|
1711
|
-
text: string;
|
|
1712
|
-
/** 真实输出音频格式(来自服务端解析后的 Provider 输出,非客户端请求参数) */
|
|
1713
|
-
format: AudioFormat;
|
|
1714
|
-
/** 采样率(Hz);pcm16 等裸音频必填 */
|
|
1715
|
-
sampleRate?: number;
|
|
1716
|
-
/** 声道数(默认单声道) */
|
|
1717
|
-
channels?: 1 | 2;
|
|
1718
|
-
}
|
|
1719
|
-
/** 合成文本段的音频已全部发送。 */
|
|
1720
|
-
interface AudioWsSegmentDoneMessage {
|
|
1721
|
-
type: 'segment_done';
|
|
1722
|
-
segmentId: string;
|
|
1723
|
-
}
|
|
1724
|
-
/** 错误帧(领域语义错误码,不暴露厂商协议细节) */
|
|
1725
|
-
interface AudioWsErrorMessage {
|
|
1726
|
-
type: 'error';
|
|
1727
|
-
/** 领域错误码(如 `hai:ai:054`) */
|
|
1728
|
-
code: string;
|
|
1729
|
-
/** 错误消息 */
|
|
1730
|
-
message: string;
|
|
1731
|
-
}
|
|
1732
|
-
/** 结束帧(服务端已发送全部结果,随后关闭连接) */
|
|
1733
|
-
interface AudioWsEndMessage {
|
|
1734
|
-
type: 'end';
|
|
1735
|
-
}
|
|
1736
|
-
/** 服务端 JSON 消息(合成音频以二进制帧返回,不走 JSON) */
|
|
1737
|
-
type AudioWsServerMessage = AudioWsSpeechMessage | AudioWsTranscriptMessage | AudioWsSegmentStartedMessage | AudioWsSegmentDoneMessage | AudioWsErrorMessage | AudioWsEndMessage;
|
|
1738
|
-
|
|
1739
|
-
export { type FileParseOptions as $, type AIFunctions as A, type CompressResult as B, type CompressionStrategy as C, type ConsolidateOptions as D, type ConsolidateResult as E, type ContextChatOptions as F, type GenerateImageRequest as G, HaiAIError as H, type ImageOperations as I, type ContextChatResult as J, type ContextDeps as K, type ContextManager as L, type McpServerOptions as M, type ContextManagerOptions as N, type ContextOperations as O, type ContextResetOptions as P, type ContextStreamEvent as Q, type ReferenceImage as R, type ConversationTurn as S, type ConversationTurnStatus as T, type EmbeddingItem as U, type EmbeddingOperations as V, type EmbeddingProvider as W, type EmbeddingRequest as X, type EmbeddingResponse as Y, type FileOperations as Z, type FileParseMethod as _, type AIInitOptions as a, type FileParseRequest as a0, type FileParseResult as a1, type MCPContext as a2, type MCPOperations as a3, type MCPPrompt as a4, type MCPPromptArgument as a5, type MCPPromptContent as a6, type MCPPromptMessage as a7, type MCPResource as a8, type MCPResourceContent as a9, type MCPToolDefinition as aa, type MCPToolHandler as ab, type OutputFormat as ac, type PersonaOperations as ad, type PersonaProfile as ae, type PersonaProfileInput as af, type PersonaProfileUpdate as ag, type PersonaScopeOptions as ah, type RerankDocument as ai, type RerankItem as aj, type RerankOperations as ak, type RerankRequest as al, type RerankResponse as am, type SummaryOperations as an, type SummaryOptions as ao, type SummaryResult as ap, type TokenOperations as aq, AUDIO_WS_PATH as b, AudioFormatSchema as c, type AudioWsClientMessage as d, AudioWsClientMessageSchema as e, type AudioWsDoneMessage as f, AudioWsDoneMessageSchema as g, type AudioWsEndMessage as h, type AudioWsErrorMessage as i, type AudioWsSegmentDoneMessage as j, type AudioWsSegmentStartedMessage as k, type AudioWsServerMessage as l, type AudioWsSpeechMessage as m, type AudioWsStartMessage as n, AudioWsStartMessageSchema as o, type AudioWsTextMessage as p, AudioWsTextMessageSchema as q, type AudioWsTranscriptMessage as r, CompressionStrategySchema as s, type GenerateImageResult as t, type GeneratedImage as u, type ImageSize as v, serializePlayableAudio as w, type CommitTurnInput as x, type CompressOperations as y, type CompressOptions as z };
|