@kiturone/kapibala 0.0.3 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +5604 -2512
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1089 -195
- package/dist/index.d.ts +1089 -195
- package/dist/index.js +3783 -717
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,200 @@
|
|
|
1
|
+
/** 技能知识来源;层级决定覆盖顺序,不授予文件或执行权限。 */
|
|
2
|
+
type SkillScope = 'builtin' | 'global' | 'project';
|
|
3
|
+
/** 结构化技能诊断;路径及文案需由宿主安全展示,不包含正文或凭据。 */
|
|
4
|
+
interface SkillDiagnostic {
|
|
5
|
+
code: string;
|
|
6
|
+
level: 'warn' | 'error';
|
|
7
|
+
path: string;
|
|
8
|
+
message: string;
|
|
9
|
+
winnerPath?: string;
|
|
10
|
+
loserPath?: string;
|
|
11
|
+
}
|
|
12
|
+
/** 已校验的文件内容;available=false 的执行扩展仅供诊断,不得激活。 */
|
|
13
|
+
interface SkillDocument {
|
|
14
|
+
name: string;
|
|
15
|
+
description: string;
|
|
16
|
+
body: string;
|
|
17
|
+
when?: string;
|
|
18
|
+
license?: string;
|
|
19
|
+
compatibility?: string;
|
|
20
|
+
argumentHint?: string;
|
|
21
|
+
metadata?: Readonly<Record<string, string>>;
|
|
22
|
+
frontmatter: Readonly<Record<string, unknown>>;
|
|
23
|
+
allowedTools?: readonly string[];
|
|
24
|
+
modelInvocable: boolean;
|
|
25
|
+
userInvocable: boolean;
|
|
26
|
+
available: boolean;
|
|
27
|
+
unavailableReason?: string;
|
|
28
|
+
diagnostics: readonly SkillDiagnostic[];
|
|
29
|
+
}
|
|
30
|
+
/** 宿主显式提供的扫描根;项目缺省不可信,boundary 必须是授权物理边界。 */
|
|
31
|
+
interface SkillRoot {
|
|
32
|
+
path: string;
|
|
33
|
+
scope: SkillScope;
|
|
34
|
+
kind?: 'native' | 'agents';
|
|
35
|
+
trusted?: boolean;
|
|
36
|
+
boundary?: string;
|
|
37
|
+
}
|
|
38
|
+
/** 可查询元数据;文件指纹来自发现快照,加载前必须重新核验。 */
|
|
39
|
+
interface SkillMetadata extends Omit<SkillDocument, 'body' | 'frontmatter' | 'diagnostics'> {
|
|
40
|
+
scope: SkillScope;
|
|
41
|
+
sourceRoot: string;
|
|
42
|
+
filePath: string;
|
|
43
|
+
baseDir: string;
|
|
44
|
+
contentDigest: string;
|
|
45
|
+
/** 原词法发现身份;加载重核根链接,不以旧物理文件继续授权。 */
|
|
46
|
+
discoveryRoot: string;
|
|
47
|
+
discoveryPath: string;
|
|
48
|
+
priority: number;
|
|
49
|
+
disabled: boolean;
|
|
50
|
+
}
|
|
51
|
+
/** 完整快照实体;正文仅在校验通过后按需进入模型历史。 */
|
|
52
|
+
interface SkillDefinition extends SkillMetadata {
|
|
53
|
+
body: string;
|
|
54
|
+
}
|
|
55
|
+
/** 会话技能选项;无根即不扫描 home,disabled 名称不能由项目配置重新启用。 */
|
|
56
|
+
interface SkillOptions {
|
|
57
|
+
roots: readonly SkillRoot[];
|
|
58
|
+
enabled?: boolean;
|
|
59
|
+
disabled?: readonly string[];
|
|
60
|
+
}
|
|
61
|
+
/** 技能正文持久来源;只追踪事实,历史来源不恢复授权。 */
|
|
62
|
+
interface SkillContentSource {
|
|
63
|
+
version: 1;
|
|
64
|
+
name: string;
|
|
65
|
+
scope: SkillScope;
|
|
66
|
+
filePath: string;
|
|
67
|
+
baseDir: string;
|
|
68
|
+
contentDigest: string;
|
|
69
|
+
renderedDigest: string;
|
|
70
|
+
caller: 'user' | 'model';
|
|
71
|
+
runId: string;
|
|
72
|
+
invocationId: string;
|
|
73
|
+
}
|
|
74
|
+
/** 本轮活动记录;pending 只收紧,committed 后才可受控读取资源。 */
|
|
75
|
+
interface SkillActivation {
|
|
76
|
+
definition: SkillDefinition;
|
|
77
|
+
source: SkillContentSource;
|
|
78
|
+
state: 'pending' | 'committed';
|
|
79
|
+
allowedTools?: ReadonlySet<string>;
|
|
80
|
+
}
|
|
81
|
+
/** 统一默认限额;宿主不得另算一套目录、文件或响应预算。 */
|
|
82
|
+
declare const SKILL_LIMITS: Readonly<{
|
|
83
|
+
fileBytes: number;
|
|
84
|
+
frontmatterBytes: number;
|
|
85
|
+
depth: 6;
|
|
86
|
+
directories: 2000;
|
|
87
|
+
entries: 20000;
|
|
88
|
+
descriptionChars: 250;
|
|
89
|
+
catalogTokens: 2000;
|
|
90
|
+
queryTokens: 1000;
|
|
91
|
+
resourceBytes: number;
|
|
92
|
+
resourceFiles: 20;
|
|
93
|
+
}>;
|
|
94
|
+
|
|
95
|
+
/** 内置适配器的实际请求目标;用于拒绝模型、协议或端点错配的绑定。 */
|
|
96
|
+
interface ModelProviderBinding {
|
|
97
|
+
protocol: ModelProtocol;
|
|
98
|
+
modelName: string;
|
|
99
|
+
baseURL: string;
|
|
100
|
+
workspaceId?: string;
|
|
101
|
+
}
|
|
102
|
+
/** 协议适配器:把 canonical 请求翻译为 wire 协议并流式返回事件。 */
|
|
103
|
+
interface ModelProvider {
|
|
104
|
+
/** 用于诊断与脱敏说明的稳定名称。 */
|
|
105
|
+
readonly name: string;
|
|
106
|
+
readonly binding?: ModelProviderBinding;
|
|
107
|
+
/** 内置 Provider 凭据是否可用;自定义无需鉴权的 Provider 可省略。 */
|
|
108
|
+
readonly credentialsReady?: boolean;
|
|
109
|
+
/** 发起一次模型调用;signal 取消时立即停止,实现负责协议翻译、私有状态校验与流清理。 */
|
|
110
|
+
create(req: ModelRequest): AsyncIterable<ModelEvent>;
|
|
111
|
+
/** 返回统一 canonical tool 消息;wire 角色转换仅发生在请求翻译阶段。 */
|
|
112
|
+
assembleToolResults(results: ToolResultBlock[]): CanonicalMessage[];
|
|
113
|
+
}
|
|
114
|
+
/** 按消息级事务装配工具结果;任何协议都不得把 wire user/Item 写为历史角色。 */
|
|
115
|
+
declare function assembleCanonicalToolResults(results: ToolResultBlock[]): CanonicalMessage[];
|
|
116
|
+
|
|
117
|
+
/** 逻辑模型角色:default 为回退绑定,planning / execution / fast 是主任务可选场景,summary 仅用于独立的上下文压缩。 */
|
|
118
|
+
type ModelRole = 'default' | 'planning' | 'execution' | 'summary' | 'fast';
|
|
119
|
+
/** 主任务可显式选择的场景;summary 只用于独立的上下文压缩。 */
|
|
120
|
+
type PrimaryModelRole = Exclude<ModelRole, 'summary'>;
|
|
121
|
+
/** wire 协议族;决定适配器实现、私有状态格式与错误解析方式。 */
|
|
122
|
+
type ModelProtocol = 'openai-compatible' | 'anthropic' | 'openai-responses';
|
|
123
|
+
/** 兼容 Chat 端点的显式参数/完成能力;缺省保持既有参数并要求完整 DONE。 */
|
|
124
|
+
interface ChatCapabilities {
|
|
125
|
+
maxTokensField?: 'max_tokens' | 'max_completion_tokens';
|
|
126
|
+
supportsStreamingUsage?: boolean;
|
|
127
|
+
supportsTemperature?: boolean;
|
|
128
|
+
requiresDone?: boolean;
|
|
129
|
+
replayReasoningContent?: boolean;
|
|
130
|
+
}
|
|
131
|
+
/** 配置侧窗口值:正整数 Token 数,或十进制 K / M 简写(如 '200K');解析规则见 resolveContextWindow。 */
|
|
132
|
+
type ContextWindowValue = number | `${number}${'K' | 'k' | 'M' | 'm'}`;
|
|
133
|
+
/** 解析后的窗口;estimated 为 true 时数值来自默认估算,不能当作模型的精确保证。 */
|
|
134
|
+
interface ResolvedContextWindow {
|
|
135
|
+
tokens: number;
|
|
136
|
+
/** true 表示模型未声明窗口,当前值来自 Kapibala 的默认估算。 */
|
|
137
|
+
estimated: boolean;
|
|
138
|
+
}
|
|
139
|
+
/** 模型未声明 contextWindow 时的默认窗口估算值。 */
|
|
140
|
+
declare const DEFAULT_CONTEXT_WINDOW_TOKENS = 1000000;
|
|
141
|
+
/**
|
|
142
|
+
* 将模型上下文窗口统一解析为整数 Token 数。
|
|
143
|
+
*
|
|
144
|
+
* 配置允许直接填写整数,或使用十进制 K/M 简写;未填写时返回默认 1M,并标记为估算值。
|
|
145
|
+
*/
|
|
146
|
+
declare function resolveContextWindow(value?: ContextWindowValue): ResolvedContextWindow;
|
|
147
|
+
/** 模型配置档案;provider 是 wire 协议族而非厂商名,绑定必须与适配器声明一致(见 validateModelBinding)。 */
|
|
148
|
+
interface ModelProfile {
|
|
149
|
+
id: string;
|
|
150
|
+
name: string;
|
|
151
|
+
provider: ModelProtocol;
|
|
152
|
+
baseURL: string;
|
|
153
|
+
/** 承载密钥的环境变量名;core 不直接读取,由宿主解析后注入 Provider。 */
|
|
154
|
+
apiKeyEnv: string;
|
|
155
|
+
apiKey?: string;
|
|
156
|
+
modelName: string;
|
|
157
|
+
/** 未声明时按 DEFAULT_CONTEXT_WINDOW_TOKENS 估算;解析规则见 resolveContextWindow。 */
|
|
158
|
+
contextWindow?: ContextWindowValue;
|
|
159
|
+
supportsThinking?: boolean;
|
|
160
|
+
/** 单次输出上限;参与上下文预算推导,缺省按 4096 预留。 */
|
|
161
|
+
maxOutputTokens?: number;
|
|
162
|
+
chatCapabilities?: ChatCapabilities;
|
|
163
|
+
/** Anthropic 请求携带的 workspace;参与绑定一致性校验。 */
|
|
164
|
+
anthropicWorkspaceId?: string;
|
|
165
|
+
}
|
|
166
|
+
/** 角色到 Profile / Provider 配对的解析接口;实现必须保证配对一致且 profile 以副本返回。 */
|
|
167
|
+
interface ModelRouter {
|
|
168
|
+
resolve(role?: ModelRole): ModelProvider;
|
|
169
|
+
getProfile(role?: ModelRole): ModelProfile;
|
|
170
|
+
}
|
|
171
|
+
/** 校验内置适配器的实际请求目标;旧自定义 Provider 由宿主显式提供绑定。 */
|
|
172
|
+
declare function validateModelBinding(profile: ModelProfile, provider: ModelProvider): void;
|
|
173
|
+
/** 保存原子 Profile/Provider 配对;普通与摘要查询可回退 default,显式任务严格解析。 */
|
|
174
|
+
declare class SimpleModelRouter implements ModelRouter {
|
|
175
|
+
private readonly providers;
|
|
176
|
+
private readonly profiles;
|
|
177
|
+
private defaultRole;
|
|
178
|
+
constructor(defaultProfile: ModelProfile, defaultProvider: ModelProvider);
|
|
179
|
+
/** 校验配对后同时替换两者并保存配置副本,校验失败不污染既有绑定。 */
|
|
180
|
+
setRole(role: ModelRole, profile: ModelProfile, provider: ModelProvider): void;
|
|
181
|
+
/** 显式场景要求存在绑定;只有普通查询和摘要允许既有默认回退。 */
|
|
182
|
+
getBinding(role?: ModelRole, strict?: boolean): {
|
|
183
|
+
profile: ModelProfile;
|
|
184
|
+
provider: ModelProvider;
|
|
185
|
+
};
|
|
186
|
+
resolve(role?: ModelRole): ModelProvider;
|
|
187
|
+
/** 返回指定角色或默认模型的独立副本,调用方不能改写在途绑定。 */
|
|
188
|
+
getProfile(role?: ModelRole): ModelProfile;
|
|
189
|
+
}
|
|
190
|
+
|
|
1
191
|
/**
|
|
2
192
|
* Error Taxonomy for Kapibala Agent Harness
|
|
3
193
|
*/
|
|
194
|
+
|
|
195
|
+
/** 模型失败的安全分类;由明确状态码与受控错误码推导,不基于自然语言猜测根因。 */
|
|
4
196
|
type ModelErrorCategory = 'authentication' | 'quota' | 'rate_limit' | 'context' | 'service' | 'network' | 'timeout' | 'invalid_request' | 'invalid_response' | 'unknown';
|
|
197
|
+
/** ModelError 的受控输入;仅这些字段会进入错误对象,原始响应体与密钥不得携带。 */
|
|
5
198
|
interface ModelErrorOptions {
|
|
6
199
|
status?: number;
|
|
7
200
|
retryable?: boolean;
|
|
@@ -22,6 +215,8 @@ interface ModelErrorInfo {
|
|
|
22
215
|
transportCode?: string;
|
|
23
216
|
stage?: ModelErrorOptions['stage'];
|
|
24
217
|
operation?: 'primary' | 'summary';
|
|
218
|
+
/** type-only 引用协议层角色类型;运行时无循环依赖。 */
|
|
219
|
+
modelRole?: PrimaryModelRole;
|
|
25
220
|
modelId?: string;
|
|
26
221
|
provider?: string;
|
|
27
222
|
retryPolicy: KapibalaError['retryPolicy'];
|
|
@@ -30,7 +225,8 @@ interface ModelErrorInfo {
|
|
|
30
225
|
/** 清理服务端报错中的终端控制符、常见凭据及带凭据的 URL,限制长度;不展示完整响应或请求头。 */
|
|
31
226
|
declare function sanitizeModelErrorMessage(value: string): string;
|
|
32
227
|
/** 生成统一脱敏说明;上下文补充调用场景,不携带原始异常、请求正文或密钥。 */
|
|
33
|
-
declare function describeModelError(error: ModelError, context?: Pick<ModelErrorInfo, 'modelId' | 'provider' | 'operation'>): ModelErrorInfo;
|
|
228
|
+
declare function describeModelError(error: ModelError, context?: Pick<ModelErrorInfo, 'modelId' | 'provider' | 'operation' | 'modelRole'>): ModelErrorInfo;
|
|
229
|
+
/** 错误体系基类:code 供机器判定,retryPolicy 是处理建议而非重试授权,safeMessage 是可对外展示的固定文案。 */
|
|
34
230
|
declare class KapibalaError extends Error {
|
|
35
231
|
readonly code: string;
|
|
36
232
|
readonly retryPolicy: 'never' | 'immediate' | 'backoff' | 'after_user_action';
|
|
@@ -41,8 +237,10 @@ declare class KapibalaError extends Error {
|
|
|
41
237
|
safeMessage?: string;
|
|
42
238
|
});
|
|
43
239
|
}
|
|
240
|
+
/** 模型调用失败;message 已脱敏,对外诊断统一经 describeModelError 产出,retryPolicy 仅为处理建议。 */
|
|
44
241
|
declare class ModelError extends KapibalaError {
|
|
45
242
|
readonly status?: number;
|
|
243
|
+
/** 属于可退避重试的瞬态类别(限流/服务/超时/网络);不等于自动重试授权。 */
|
|
46
244
|
readonly retryable: boolean;
|
|
47
245
|
/** 可选的受控底层连接错误码,不包含原始传输异常文本。 */
|
|
48
246
|
readonly transportCode?: string;
|
|
@@ -60,24 +258,30 @@ declare class ContextOverflowError extends ModelError {
|
|
|
60
258
|
message?: string;
|
|
61
259
|
});
|
|
62
260
|
}
|
|
261
|
+
/** 模型 API 语义之外的传输失败;retryable 表示属于可立即重试的网络类错误。 */
|
|
63
262
|
declare class TransportError extends KapibalaError {
|
|
64
263
|
readonly retryable: boolean;
|
|
65
264
|
constructor(message: string, retryable?: boolean);
|
|
66
265
|
}
|
|
266
|
+
/** 工具执行失败的通用错误;retryPolicy 固定 never,后续处理由调用方决定。 */
|
|
67
267
|
declare class ToolError extends KapibalaError {
|
|
68
268
|
constructor(message: string);
|
|
69
269
|
}
|
|
270
|
+
/** 请求的工具在注册表中不存在;message 回显工具名与可用列表,对外展示用 safeMessage。 */
|
|
70
271
|
declare class ToolNotFound extends KapibalaError {
|
|
71
272
|
readonly toolName: string;
|
|
72
273
|
readonly availableTools: string[];
|
|
73
274
|
constructor(toolName: string, availableTools?: string[]);
|
|
74
275
|
}
|
|
276
|
+
/** 用户主动中止;不属于失败,恢复需要用户重新发起操作。 */
|
|
75
277
|
declare class AbortError extends KapibalaError {
|
|
76
278
|
constructor(message?: string);
|
|
77
279
|
}
|
|
280
|
+
/** 无受控分类的致命错误(code 固定 INTERNAL_ERROR、不可重试);终止策略由宿主决定。 */
|
|
78
281
|
declare class FatalError extends KapibalaError {
|
|
79
282
|
constructor(message: string);
|
|
80
283
|
}
|
|
284
|
+
/** 会话正在执行任务时拒绝新的排他操作(切换模型、挂载插件等),operation 描述被拒绝的动作。 */
|
|
81
285
|
declare class SessionBusyError extends KapibalaError {
|
|
82
286
|
constructor(operation: string);
|
|
83
287
|
}
|
|
@@ -96,10 +300,49 @@ declare class ContextStorageError extends KapibalaError {
|
|
|
96
300
|
* Canonical Message & Event Types for Kapibala
|
|
97
301
|
*/
|
|
98
302
|
|
|
99
|
-
|
|
303
|
+
/** 原生协议内容的来源;仅同一次运行、同模型和端点允许回传私有状态。 */
|
|
304
|
+
interface ProtocolOrigin {
|
|
305
|
+
version: 1;
|
|
306
|
+
protocol: 'anthropic' | 'openai-responses';
|
|
307
|
+
modelName: string;
|
|
308
|
+
modelId?: string;
|
|
309
|
+
runId: string;
|
|
310
|
+
endpointScope: string;
|
|
311
|
+
}
|
|
312
|
+
/** Responses Item 与 canonical 块的对应关系;调用 Item 身份独立于业务 call_id。 */
|
|
313
|
+
interface ProtocolItemMetadata {
|
|
314
|
+
origin: ProtocolOrigin;
|
|
315
|
+
itemIndex: number;
|
|
316
|
+
itemId?: string;
|
|
317
|
+
contentIndex?: number;
|
|
318
|
+
phase?: 'commentary' | 'final_answer';
|
|
319
|
+
contentType?: 'output_text' | 'refusal';
|
|
320
|
+
arguments?: string;
|
|
321
|
+
}
|
|
322
|
+
/** 只保存受控 reasoning Item;密文不进入展示或摘要源。 */
|
|
323
|
+
interface ProviderStateBlock {
|
|
324
|
+
type: 'provider_state';
|
|
325
|
+
origin: ProtocolOrigin;
|
|
326
|
+
item: {
|
|
327
|
+
type: 'reasoning';
|
|
328
|
+
id: string;
|
|
329
|
+
encrypted_content?: string;
|
|
330
|
+
summary: {
|
|
331
|
+
type: 'summary_text';
|
|
332
|
+
text: string;
|
|
333
|
+
}[];
|
|
334
|
+
status?: 'in_progress' | 'completed' | 'incomplete';
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
/** canonical 历史的内容块联合;provider_state 等私有块只在同 run、同模型与端点续答回传。 */
|
|
338
|
+
type ContentBlock = TextBlock | ToolUseBlock | ToolResultBlock | ThinkingBlock | RedactedThinkingBlock | ProviderStateBlock;
|
|
100
339
|
interface TextBlock {
|
|
101
340
|
type: 'text';
|
|
102
341
|
text: string;
|
|
342
|
+
/** 宿主注入技能的可选来源;只追踪正文,缺失或恢复不授予权限。 */
|
|
343
|
+
skillSource?: SkillContentSource;
|
|
344
|
+
/** 源 wire Item 元数据;正文被改写后由调用方清除,不作为业务身份。 */
|
|
345
|
+
protocolMeta?: ProtocolItemMetadata;
|
|
103
346
|
}
|
|
104
347
|
interface ToolUseBlock {
|
|
105
348
|
/** 工具执行语义来源;新运行记录,旧历史可缺省。未知插件不按内置只读工具剪裁。 */
|
|
@@ -110,9 +353,13 @@ interface ToolUseBlock {
|
|
|
110
353
|
id: string;
|
|
111
354
|
name: string;
|
|
112
355
|
input: Record<string, unknown>;
|
|
356
|
+
/** 源 wire Item 元数据;内容被改写后由调用方清除,不作为业务身份。 */
|
|
357
|
+
protocolMeta?: ProtocolItemMetadata;
|
|
113
358
|
}
|
|
114
359
|
interface ToolResultBlock {
|
|
115
360
|
type: 'tool_result';
|
|
361
|
+
/** 完整技能正文的宿主来源;不发送到Provider wire,不从历史恢复授权。 */
|
|
362
|
+
skillSource?: SkillContentSource;
|
|
116
363
|
toolUseId: string;
|
|
117
364
|
content: string;
|
|
118
365
|
isError?: boolean;
|
|
@@ -126,12 +373,17 @@ interface ToolResultBlock {
|
|
|
126
373
|
interface ThinkingBlock {
|
|
127
374
|
type: 'thinking';
|
|
128
375
|
thinking: string;
|
|
376
|
+
/** 存在签名或来源时属于私有输出,改写内容会被整批拒绝。 */
|
|
129
377
|
signature?: string;
|
|
378
|
+
origin?: ProtocolOrigin;
|
|
130
379
|
}
|
|
380
|
+
/** Provider 提供的密文占位;仅按原样回传用于续答,不进入展示或摘要源。 */
|
|
131
381
|
interface RedactedThinkingBlock {
|
|
132
382
|
type: 'redacted_thinking';
|
|
133
383
|
data: string;
|
|
384
|
+
origin?: ProtocolOrigin;
|
|
134
385
|
}
|
|
386
|
+
/** 单次模型请求的 token 用量;由 Provider 上报,缺失的字段不能按零推断。 */
|
|
135
387
|
interface Usage {
|
|
136
388
|
promptTokens: number;
|
|
137
389
|
completionTokens: number;
|
|
@@ -139,7 +391,12 @@ interface Usage {
|
|
|
139
391
|
/** Provider 明确提供的缓存命中输入量;缺失表示未知,不能按零推断。 */
|
|
140
392
|
cachedPromptTokens?: number;
|
|
141
393
|
}
|
|
394
|
+
/** canonical 消息角色;'tool' 仅用于工具结果消息,wire 角色转换只发生在请求投影阶段。 */
|
|
142
395
|
type MessageRole = 'user' | 'assistant' | 'system' | 'tool';
|
|
396
|
+
/**
|
|
397
|
+
* 协议无关的历史真源;wire 格式只在请求投影时出现。
|
|
398
|
+
* 不变量:assistant 消息中的 tool_use 进入历史后,对应 tool_result 消息必须紧跟其后。
|
|
399
|
+
*/
|
|
143
400
|
interface CanonicalMessage {
|
|
144
401
|
/** 派生背景锚点独立于真实用户输入,避免合并后丢失受保护原消息的来源身份。 */
|
|
145
402
|
contextSummary?: {
|
|
@@ -155,10 +412,15 @@ interface CanonicalMessage {
|
|
|
155
412
|
content: ContentBlock[];
|
|
156
413
|
timestamp?: number;
|
|
157
414
|
}
|
|
415
|
+
/** 判别并收窄为文本块。 */
|
|
158
416
|
declare function isTextBlock(block: ContentBlock): block is TextBlock;
|
|
417
|
+
/** 判别并收窄为工具调用块。 */
|
|
159
418
|
declare function isToolUseBlock(block: ContentBlock): block is ToolUseBlock;
|
|
419
|
+
/** 判别并收窄为工具结果块。 */
|
|
160
420
|
declare function isToolResultBlock(block: ContentBlock): block is ToolResultBlock;
|
|
421
|
+
/** 判别并收窄为思考块。 */
|
|
161
422
|
declare function isThinkingBlock(block: ContentBlock): block is ThinkingBlock;
|
|
423
|
+
/** 单个 turn 的指标快照;错误与熔断路径也以统一构造产出(工具字段为零),保证结构一致。 */
|
|
162
424
|
interface TurnMetrics {
|
|
163
425
|
turn: number;
|
|
164
426
|
startTime: number;
|
|
@@ -172,6 +434,7 @@ interface TurnMetrics {
|
|
|
172
434
|
totalTokens: number;
|
|
173
435
|
toolCallsCount: number;
|
|
174
436
|
}
|
|
437
|
+
/** 一次完整 run 的累计指标;token 数为各轮请求之和,上下文占用单独见 contextUsage。 */
|
|
175
438
|
interface RunMetrics {
|
|
176
439
|
startTime: number;
|
|
177
440
|
endTime: number;
|
|
@@ -190,6 +453,7 @@ interface RunMetrics {
|
|
|
190
453
|
/** 最近一次内部模型请求占用的上下文;不使用 run 累计 promptTokens。 */
|
|
191
454
|
contextUsage?: ContextUsage;
|
|
192
455
|
}
|
|
456
|
+
/** 供宿主展示的上下文占用快照;估算值不能用于计费或精确预算。 */
|
|
193
457
|
interface ContextUsage {
|
|
194
458
|
usedTokens?: number;
|
|
195
459
|
/** true 表示未收到当前请求 usage,输入占用使用预算估算,不能用于计费。 */
|
|
@@ -198,7 +462,9 @@ interface ContextUsage {
|
|
|
198
462
|
percent?: number;
|
|
199
463
|
estimatedLimit: boolean;
|
|
200
464
|
}
|
|
465
|
+
/** 单 turn 内关键阶段的进度标记;顺序与 AgentLoop 的实际派发时序一致,供展示与诊断。 */
|
|
201
466
|
type StepLogStage = 'model_request_start' | 'first_token' | 'model_stream_finish' | 'tool_execution_start' | 'tool_execution_finish' | 'turn_finish';
|
|
467
|
+
/** 面向宿主的步骤日志;message 可直接展示,结构化数值放在 metadata 中。 */
|
|
202
468
|
interface StepLogEntry {
|
|
203
469
|
timestamp: number;
|
|
204
470
|
turn: number;
|
|
@@ -215,6 +481,11 @@ interface StepLogEntry {
|
|
|
215
481
|
* 不应直接把本类型当作长期兼容的网络协议。
|
|
216
482
|
*/
|
|
217
483
|
type SessionEvent = {
|
|
484
|
+
type: 'skill_loaded';
|
|
485
|
+
source: SkillContentSource;
|
|
486
|
+
messageId: string;
|
|
487
|
+
persistence: 'memory' | 'disk';
|
|
488
|
+
} | {
|
|
218
489
|
type: 'session_resumed';
|
|
219
490
|
conversationId: string;
|
|
220
491
|
runtimeSessionId: string;
|
|
@@ -273,9 +544,16 @@ type SessionEvent = {
|
|
|
273
544
|
} | {
|
|
274
545
|
type: 'text_delta';
|
|
275
546
|
text: string;
|
|
547
|
+
} | {
|
|
548
|
+
type: 'thinking_block_start';
|
|
549
|
+
blockId: string;
|
|
276
550
|
} | {
|
|
277
551
|
type: 'thinking_delta';
|
|
278
552
|
thinking: string;
|
|
553
|
+
blockId?: string;
|
|
554
|
+
} | {
|
|
555
|
+
type: 'thinking_block_stop';
|
|
556
|
+
blockId: string;
|
|
279
557
|
} | {
|
|
280
558
|
type: 'tool_start';
|
|
281
559
|
id: string;
|
|
@@ -325,9 +603,16 @@ type SessionEvent = {
|
|
|
325
603
|
type ModelEvent = {
|
|
326
604
|
type: 'text_delta';
|
|
327
605
|
text: string;
|
|
606
|
+
} | {
|
|
607
|
+
type: 'thinking_block_start';
|
|
608
|
+
blockId: string;
|
|
328
609
|
} | {
|
|
329
610
|
type: 'thinking_delta';
|
|
330
611
|
thinking: string;
|
|
612
|
+
blockId?: string;
|
|
613
|
+
} | {
|
|
614
|
+
type: 'thinking_block_stop';
|
|
615
|
+
blockId: string;
|
|
331
616
|
} | {
|
|
332
617
|
type: 'tool_call_start';
|
|
333
618
|
id: string;
|
|
@@ -349,19 +634,30 @@ type ModelEvent = {
|
|
|
349
634
|
ttftMs?: number;
|
|
350
635
|
durationMs?: number;
|
|
351
636
|
finishReason?: string;
|
|
637
|
+
/** 完整且有序的最终内容;有此字段时增量只供展示,不重复装配或执行工具。 */
|
|
638
|
+
finalContent?: ContentBlock[];
|
|
639
|
+
refusal?: boolean;
|
|
352
640
|
};
|
|
641
|
+
/** 投影给模型的工具描述;由注册表条目转换而来,仅暴露名称、描述与参数 Schema。 */
|
|
353
642
|
interface ToolDefinition {
|
|
354
643
|
name: string;
|
|
355
644
|
description: string;
|
|
356
645
|
parameters: Record<string, unknown>;
|
|
357
646
|
}
|
|
647
|
+
/** 协议无关的模型请求;messages 是 canonical 真源的克隆,wire 翻译只发生在 Provider 内。 */
|
|
358
648
|
interface ModelRequest {
|
|
359
649
|
systemPrompt?: string;
|
|
360
650
|
messages: CanonicalMessage[];
|
|
361
651
|
tools?: ToolDefinition[];
|
|
362
652
|
signal?: AbortSignal;
|
|
653
|
+
/** 以下两项可选覆盖采样温度与单次输出上限;未设置时由适配器按协议与能力决定。 */
|
|
363
654
|
temperature?: number;
|
|
364
655
|
maxTokens?: number;
|
|
656
|
+
/** 宿主提供的本次运行来源;缺省时原生适配器不回传来源未知的私有状态。 */
|
|
657
|
+
context?: {
|
|
658
|
+
runId: string;
|
|
659
|
+
modelId?: string;
|
|
660
|
+
};
|
|
365
661
|
}
|
|
366
662
|
/** runtime 通用请求准备契约;实现可以预算或投影,Loop 不依赖具体上下文管理器。 */
|
|
367
663
|
type RequestPreparation = (request: ModelRequest, history: readonly CanonicalMessage[], reason?: 'threshold' | 'overflow') => AsyncGenerator<SessionEvent, ModelRequest>;
|
|
@@ -371,16 +667,23 @@ declare function createMessageId(): string;
|
|
|
371
667
|
|
|
372
668
|
/** 按来源及原始位置生成旧消息身份;复制输入,不改变调用者持有的正文或已有身份。 */
|
|
373
669
|
declare function identifyHistory(messages: readonly CanonicalMessage[], source: string): CanonicalMessage[];
|
|
670
|
+
/**
|
|
671
|
+
* 一次缺失工具结果的补齐:result 为 OUTCOME_UNKNOWN 占位,只用于闭合历史事务。
|
|
672
|
+
* 持久化为 history_repair 记录后,恢复时叠加到原工具事务;不重放工具。
|
|
673
|
+
*/
|
|
374
674
|
interface HistoryRepair {
|
|
375
675
|
assistantId: string;
|
|
376
676
|
toolCallId: string;
|
|
377
677
|
result: ToolResultBlock;
|
|
378
678
|
}
|
|
679
|
+
/** normalizeHistory 的产物:合法请求视图、来源追踪与修复/丢弃审计。 */
|
|
379
680
|
interface NormalizedHistory {
|
|
380
681
|
messages: CanonicalMessage[];
|
|
381
682
|
/** 与 messages 同序的原始消息 ID 集合;修复结果关联原始 assistant。 */
|
|
382
683
|
sources: string[][];
|
|
684
|
+
/** 已注入 messages 的缺失结果占位;结构与 history_repair 持久记录一致。 */
|
|
383
685
|
repairs: HistoryRepair[];
|
|
686
|
+
/** 被忽略的孤儿、迟到或重复工具结果提示;只诊断,不回写调用方输入。 */
|
|
384
687
|
diagnostics: string[];
|
|
385
688
|
}
|
|
386
689
|
/**
|
|
@@ -388,11 +691,15 @@ interface NormalizedHistory {
|
|
|
388
691
|
* 合并仅适用于纯文本的同角色消息,失败正文不重放,所有来源均可追踪。
|
|
389
692
|
*/
|
|
390
693
|
declare function normalizeHistory(input: readonly CanonicalMessage[]): NormalizedHistory;
|
|
694
|
+
/** 以用户输入为界划分的一次交互及其终态;保护范围与摘要资格都以它为单位。 */
|
|
391
695
|
interface InteractionBoundary {
|
|
392
696
|
interactionId: string;
|
|
697
|
+
/** completed 需显式终态或推断成功;failed 只来自显式终态,缺省 interrupted。 */
|
|
393
698
|
status: 'completed' | 'failed' | 'interrupted';
|
|
699
|
+
/** 该交互按历史顺序覆盖的原始消息 ID。 */
|
|
394
700
|
messageIds: string[];
|
|
395
701
|
}
|
|
702
|
+
/** 运行收尾时记录的交互终态;有 start 无 finish 的运行恢复后保持 interrupted。 */
|
|
396
703
|
interface InteractionTerminal {
|
|
397
704
|
interactionId: string;
|
|
398
705
|
status: InteractionBoundary['status'];
|
|
@@ -406,11 +713,18 @@ declare function indexInteractions(input: readonly CanonicalMessage[], terminals
|
|
|
406
713
|
protectedMessageIds: string[];
|
|
407
714
|
};
|
|
408
715
|
|
|
716
|
+
/**
|
|
717
|
+
* 旧版单文件 JSONL MessageStore:每行一条消息,无版本化状态能力。
|
|
718
|
+
* 写入为同步追加,不做 fsync 或单写者保护;恢复时容错跳过损坏行并闭合悬挂事务。
|
|
719
|
+
*/
|
|
409
720
|
declare class JSONLMessageStore implements MessageStore {
|
|
410
721
|
readonly filePath: string;
|
|
411
722
|
constructor(filePath: string);
|
|
723
|
+
/** 校验协议内容后追加一行 JSON;不补身份、不查重、不 fsync。 */
|
|
412
724
|
append(message: CanonicalMessage): Promise<void>;
|
|
725
|
+
/** 读取全部消息;半截损坏行忽略,悬挂 tool_use 就地补 OUTCOME_UNKNOWN 结果。 */
|
|
413
726
|
load(): Promise<CanonicalMessage[]>;
|
|
727
|
+
/** 删除整个文件;文件不存在时静默成功。 */
|
|
414
728
|
clear(): Promise<void>;
|
|
415
729
|
/**
|
|
416
730
|
* 历史自愈(设计文档 §4.4):逐条扫描 assistant 的 tool_use,
|
|
@@ -423,22 +737,32 @@ declare class JSONLMessageStore implements MessageStore {
|
|
|
423
737
|
private healDanglingToolCalls;
|
|
424
738
|
}
|
|
425
739
|
|
|
740
|
+
/**
|
|
741
|
+
* 消息级持久化最小契约:user、assistant、tool 三类消息逐条经 append 落盘,
|
|
742
|
+
* load 恢复原始正文(而非模型请求投影),clear 实现 SDK 清空语义。
|
|
743
|
+
* 需要持久压缩、交互终态等能力的宿主另实现 StatefulMessageStore。
|
|
744
|
+
*/
|
|
426
745
|
interface MessageStore {
|
|
427
746
|
append(message: CanonicalMessage): Promise<void>;
|
|
428
747
|
load(): Promise<CanonicalMessage[]>;
|
|
429
748
|
clear(): Promise<void>;
|
|
430
749
|
}
|
|
431
750
|
|
|
751
|
+
/** 会话正文 schema 版本;读取时不匹配即拒绝恢复,不做跨版本猜测。 */
|
|
432
752
|
declare const SESSION_SCHEMA_VERSION = 1;
|
|
753
|
+
/** 会话记录类型:header 只出现一次,message 承载正文,其余为状态记录。 */
|
|
433
754
|
type SessionRecordType = 'header' | 'message' | 'run_started' | 'run_finished' | 'message_state' | 'history_repair' | 'usage' | 'checkpoint' | 'context_pruned' | 'compaction_state' | 'title' | 'reset' | 'legacy_import';
|
|
755
|
+
/** 会话文件首条记录:身份与创建环境快照;conversationId 与文件名强绑定。 */
|
|
434
756
|
interface SessionHeader {
|
|
435
757
|
conversationId: string;
|
|
436
758
|
projectRoot: string;
|
|
437
759
|
initialCwd: string;
|
|
438
760
|
createdAt?: number;
|
|
439
761
|
gitBranch?: string;
|
|
762
|
+
/** 旧历史导入的幂等身份;存在时同一来源只导入一次。 */
|
|
440
763
|
importIdentity?: string;
|
|
441
764
|
}
|
|
765
|
+
/** 单条 JSONL 记录封装;parentId 链接前驱,恢复时校验追加次序。 */
|
|
442
766
|
interface SessionRecord {
|
|
443
767
|
schemaVersion: number;
|
|
444
768
|
type: SessionRecordType;
|
|
@@ -447,6 +771,7 @@ interface SessionRecord {
|
|
|
447
771
|
parentId: string | null;
|
|
448
772
|
payload: Record<string, unknown>;
|
|
449
773
|
}
|
|
774
|
+
/** loadState 返回的版本化状态视图;记录自最近 reset 起有效。 */
|
|
450
775
|
interface SessionState {
|
|
451
776
|
header: SessionHeader;
|
|
452
777
|
/** 最近 reset 之后的记录;包含正文及状态,不包含头部。 */
|
|
@@ -527,33 +852,48 @@ declare class SessionLock {
|
|
|
527
852
|
private static isDead;
|
|
528
853
|
}
|
|
529
854
|
|
|
855
|
+
/** 会话桶的项目身份:会话按工作树根隔离存储在项目桶目录下。 */
|
|
530
856
|
interface ProjectIdentity {
|
|
857
|
+
/** 真实工作树根(非 Git 目录时为真实 cwd),作为会话归属与来源基准。 */
|
|
531
858
|
projectRoot: string;
|
|
859
|
+
/** 解析符号链接后的真实 cwd。 */
|
|
532
860
|
cwd: string;
|
|
861
|
+
/** 规范化根目录的 SHA-256,决定 ~/.kapibala/sessions 下的桶目录。 */
|
|
533
862
|
projectKey: string;
|
|
863
|
+
/** detached HEAD 或非 Git 目录时缺省。 */
|
|
534
864
|
gitBranch?: string;
|
|
865
|
+
/** true 表示 cwd 位于 Git 工作树内;同工作树子目录共用同一桶。 */
|
|
535
866
|
gitWorktree?: boolean;
|
|
536
867
|
}
|
|
537
868
|
/** 解析真实工作树根;同工作树子目录共用桶,非 Git 目录和不同 worktree 独立。不会改变 cwd。 */
|
|
538
869
|
declare function resolveSessionProject(cwd: string): ProjectIdentity;
|
|
870
|
+
/**
|
|
871
|
+
* 会话 sidecar 缓存元数据;可随时从正文重建,校验失败即丢弃,不是恢复依据。
|
|
872
|
+
* 正文 JSONL 始终是唯一真源。
|
|
873
|
+
*/
|
|
539
874
|
interface SessionMetadata {
|
|
540
875
|
schemaVersion: 1;
|
|
541
876
|
conversationId: string;
|
|
542
877
|
fileName: string;
|
|
543
878
|
projectRoot: string;
|
|
544
879
|
createdAt: number;
|
|
880
|
+
/** 最后一次正文、用户 run 或 reset 的时间;usage 与标题不影响排序。 */
|
|
545
881
|
lastActivityAt: number;
|
|
546
882
|
messageCount: number;
|
|
547
883
|
title: string;
|
|
884
|
+
/** 用户显式重命名的标题;存在时 reset 不再回退为默认标题。 */
|
|
548
885
|
customTitle?: string;
|
|
549
886
|
gitBranch?: string;
|
|
550
887
|
lastActivityGitBranch?: string;
|
|
551
888
|
lastModelId?: string;
|
|
889
|
+
/** 旧历史导入的幂等身份(来源路径 + 内容哈希);重复导入据此跳过。 */
|
|
552
890
|
importIdentity?: string;
|
|
553
891
|
size: number;
|
|
554
892
|
mtimeMs: number;
|
|
893
|
+
/** 最后计入的记录 ID;与正文 stat 一同用于增量校验。 */
|
|
555
894
|
revision: string;
|
|
556
895
|
}
|
|
896
|
+
/** 打开的会话句柄:持有单写者锁与 Store,宿主用毕必须 release。 */
|
|
557
897
|
interface ManagedSession {
|
|
558
898
|
conversationId: string;
|
|
559
899
|
store: SessionStore;
|
|
@@ -566,8 +906,10 @@ interface ManagedSession {
|
|
|
566
906
|
createdBranch?: string;
|
|
567
907
|
currentBranch?: string;
|
|
568
908
|
};
|
|
909
|
+
/** 释放单写者锁并移除句柄;宿主须先结束 AgentSession 的工具/摘要清理。 */
|
|
569
910
|
release(): Promise<void>;
|
|
570
911
|
}
|
|
912
|
+
/** SessionManager 配置;cwd 决定项目桶,homeDirectory 便于测试隔离。 */
|
|
571
913
|
interface SessionManagerOptions {
|
|
572
914
|
cwd?: string;
|
|
573
915
|
homeDirectory?: string;
|
|
@@ -623,57 +965,19 @@ declare class SessionManager {
|
|
|
623
965
|
private report;
|
|
624
966
|
}
|
|
625
967
|
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
create(req: ModelRequest): AsyncIterable<ModelEvent>;
|
|
629
|
-
/** 把一批工具结果装配成符合本协议的 canonical 消息序列 */
|
|
630
|
-
assembleToolResults(results: ToolResultBlock[]): CanonicalMessage[];
|
|
631
|
-
}
|
|
632
|
-
|
|
633
|
-
type ModelRole = 'default' | 'planning' | 'execution' | 'summary' | 'fast';
|
|
634
|
-
type ContextWindowValue = number | `${number}${'K' | 'k' | 'M' | 'm'}`;
|
|
635
|
-
interface ResolvedContextWindow {
|
|
636
|
-
tokens: number;
|
|
637
|
-
/** true 表示模型未声明窗口,当前值来自 Kapibala 的默认估算。 */
|
|
638
|
-
estimated: boolean;
|
|
639
|
-
}
|
|
640
|
-
declare const DEFAULT_CONTEXT_WINDOW_TOKENS = 1000000;
|
|
641
|
-
/**
|
|
642
|
-
* 将模型上下文窗口统一解析为整数 Token 数。
|
|
643
|
-
*
|
|
644
|
-
* 配置允许直接填写整数,或使用十进制 K/M 简写;未填写时返回默认 1M,并标记为估算值。
|
|
645
|
-
*/
|
|
646
|
-
declare function resolveContextWindow(value?: ContextWindowValue): ResolvedContextWindow;
|
|
647
|
-
interface ModelProfile {
|
|
648
|
-
id: string;
|
|
649
|
-
name: string;
|
|
650
|
-
provider: 'openai-compatible';
|
|
651
|
-
baseURL: string;
|
|
652
|
-
apiKeyEnv: string;
|
|
653
|
-
apiKey?: string;
|
|
654
|
-
modelName: string;
|
|
655
|
-
contextWindow?: ContextWindowValue;
|
|
656
|
-
supportsThinking?: boolean;
|
|
657
|
-
}
|
|
658
|
-
interface ModelRouter {
|
|
659
|
-
resolve(role?: ModelRole): ModelProvider;
|
|
660
|
-
getProfile(role?: ModelRole): ModelProfile;
|
|
661
|
-
}
|
|
662
|
-
declare class SimpleModelRouter implements ModelRouter {
|
|
663
|
-
private readonly providers;
|
|
664
|
-
private readonly profiles;
|
|
665
|
-
private defaultRole;
|
|
666
|
-
constructor(defaultProfile: ModelProfile, defaultProvider: ModelProvider);
|
|
667
|
-
setRole(role: ModelRole, profile: ModelProfile, provider: ModelProvider): void;
|
|
668
|
-
resolve(role?: ModelRole): ModelProvider;
|
|
669
|
-
getProfile(role?: ModelRole): ModelProfile;
|
|
670
|
-
}
|
|
968
|
+
/** 对对象 key 确定性排序,保证摘要、工具 schema 与 fingerprint 不受插入顺序影响。 */
|
|
969
|
+
declare function stableSerialize(value: unknown): string;
|
|
671
970
|
|
|
971
|
+
/** 由生效窗口推导的请求预算;实际请求与只读展示必须共用同一份推导。 */
|
|
672
972
|
interface ContextBudget {
|
|
673
973
|
contextWindow: number;
|
|
974
|
+
/** 窗口来自保守估算(未知按 32K 推导)时为 true。 */
|
|
674
975
|
estimatedWindow: boolean;
|
|
976
|
+
/** 输出预留(含私有推理消耗),同时作为实际请求的 maxTokens。 */
|
|
675
977
|
outputReserve: number;
|
|
978
|
+
/** 至少 128 的窗口安全余量,防止输入预算顶满窗口。 */
|
|
676
979
|
safetyReserve: number;
|
|
980
|
+
/** 输入预算 = 窗口 − 输出预留 − 安全余量;超限请求在 Provider 调用前失败。 */
|
|
677
981
|
inputBudget: number;
|
|
678
982
|
/** 触发阈值 0.85B:超过才启动一级剪裁/二级摘要(迁移文档:剪裁后低于触发阈值则不调用摘要模型)。 */
|
|
679
983
|
trigger: number;
|
|
@@ -684,12 +988,14 @@ interface ContextBudget {
|
|
|
684
988
|
*/
|
|
685
989
|
target: number;
|
|
686
990
|
}
|
|
991
|
+
/** 单次请求的 token 占用分解;total 与各部分独立取整,不保证严格相加。 */
|
|
687
992
|
interface TokenEstimate {
|
|
688
993
|
total: number;
|
|
689
994
|
system: number;
|
|
690
995
|
tools: number;
|
|
691
996
|
history: number;
|
|
692
997
|
overhead: number;
|
|
998
|
+
/** calibrated 表示该请求指纹已有实测校准;估算结果仍非计费依据。 */
|
|
693
999
|
source: 'estimated' | 'calibrated';
|
|
694
1000
|
}
|
|
695
1001
|
/** 可替换估算器;不得执行 Provider、Hook 或改写传入请求。 */
|
|
@@ -697,10 +1003,16 @@ interface TokenEstimator {
|
|
|
697
1003
|
estimate(request: ModelRequest, modelFingerprint?: string): TokenEstimate;
|
|
698
1004
|
observe?(request: ModelRequest, actualPromptTokens: number, modelFingerprint?: string): void;
|
|
699
1005
|
}
|
|
700
|
-
/**
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
1006
|
+
/**
|
|
1007
|
+
* 计算一次有效输出预留及输入预算;输出上限须为正安全整数,缺省沿用 4096。
|
|
1008
|
+
* 未知窗口以 32K 控制,输出最多占窗口 12.5%,另留至少 128 的 5% 安全余量。
|
|
1009
|
+
* 输出预留包含思考/推理;不能把 Profile 的较低覆盖重新放大为默认值。
|
|
1010
|
+
* @param window 模型声明的上下文窗口;缺省采用保守估算。
|
|
1011
|
+
* @param maxOutputTokens 当前目的的输出上限,包含可见文字及私有推理消耗。
|
|
1012
|
+
* @returns 实际请求与上下文展示共用的预算。
|
|
1013
|
+
* @throws {ModelError} 输出上限不是正安全整数或窗口过小;错误纳入统一脱敏诊断通道。
|
|
1014
|
+
*/
|
|
1015
|
+
declare function createContextBudget(window?: ContextWindowValue, maxOutputTokens?: number): ContextBudget;
|
|
704
1016
|
/** 请求 fingerprint 不包含 AbortSignal 等宿主对象,包含实际指令、schema、正文和输出配置。 */
|
|
705
1017
|
declare function requestFingerprint(request: ModelRequest, modelFingerprint?: string): string;
|
|
706
1018
|
/**
|
|
@@ -716,36 +1028,58 @@ declare class UnicodeTokenEstimator implements TokenEstimator {
|
|
|
716
1028
|
}
|
|
717
1029
|
|
|
718
1030
|
type CompactionReason = 'threshold' | 'manual' | 'overflow';
|
|
1031
|
+
/**
|
|
1032
|
+
* 最近一次请求准备或只读检查发布的上下文快照,供宿主展示与诊断。
|
|
1033
|
+
* 反映原始历史与实际请求两侧状态;不代表工具执行结果,也不承诺下次压缩行为。
|
|
1034
|
+
*/
|
|
719
1035
|
interface ContextSnapshot {
|
|
720
1036
|
conversationId: string;
|
|
721
1037
|
modelId: string;
|
|
1038
|
+
/** 最终请求指纹;observe 只对匹配指纹的请求回填实测 usage。 */
|
|
722
1039
|
fingerprint: string;
|
|
723
1040
|
budget: ContextBudget;
|
|
724
1041
|
estimate: TokenEstimate;
|
|
1042
|
+
/** 最近成功交互及其后全部交互的原始消息 ID;剪裁与摘要不得越过。 */
|
|
725
1043
|
protectedMessageIds: string[];
|
|
1044
|
+
/** 原始历史条数;与 projectedMessageCount 的差值来自摘要覆盖与压缩投影。 */
|
|
726
1045
|
rawMessageCount: number;
|
|
727
1046
|
projectedMessageCount: number;
|
|
1047
|
+
/** 模型或请求配置变化后为 true,直到下一次准备成功。 */
|
|
728
1048
|
stale: boolean;
|
|
1049
|
+
/** 匹配指纹请求的实测 usage;缺失表示尚未观测,不按零推断。 */
|
|
729
1050
|
actualPromptTokens?: number;
|
|
730
1051
|
actualCachedPromptTokens?: number;
|
|
731
1052
|
/** false 表示只读静态估算,尚未执行下一请求的动态 Hook。 */
|
|
732
1053
|
dynamicKnown: boolean;
|
|
1054
|
+
/** 当前检查点摘要消息的估算占用;无检查点为 0。 */
|
|
733
1055
|
summaryTokens: number;
|
|
734
1056
|
checkpointId?: string;
|
|
1057
|
+
/** 累计被替换为占位描述的成功工具结果数。 */
|
|
735
1058
|
prunedResults: number;
|
|
1059
|
+
/** 已提交(含恢复自持久状态)的摘要检查点次数。 */
|
|
736
1060
|
summaryCount: number;
|
|
1061
|
+
/** 当前配置纪元内连续摘要失败次数;达到 3 次暂停自动摘要。 */
|
|
737
1062
|
consecutiveFailures: number;
|
|
1063
|
+
/** 熔断生效标志;阈值触发的压缩被暂停,手动压缩仍可重试。 */
|
|
738
1064
|
automaticSummaryPaused: boolean;
|
|
1065
|
+
/** disk 有版本化 Store 支撑;memory 只在内存,unsupported 禁用剪裁与摘要。 */
|
|
739
1066
|
persistence: 'disk' | 'memory' | 'unsupported';
|
|
740
1067
|
}
|
|
1068
|
+
/** ContextManager 的宿主注入;除 conversationId 外均可缺省,缺省时按保守方式降级。 */
|
|
741
1069
|
interface ContextManagerOptions {
|
|
1070
|
+
/** 提供状态能力时启用持久剪裁与摘要;旧 Store 降级为仅内存并给出诊断。 */
|
|
742
1071
|
store?: MessageStore;
|
|
743
1072
|
estimator?: TokenEstimator;
|
|
744
1073
|
conversationId: string;
|
|
1074
|
+
/** 存储降级、无效状态记录等非致命事件的脱敏诊断通道。 */
|
|
745
1075
|
onDiagnostic?: (message: string) => void;
|
|
1076
|
+
/** 摘要角色绑定来源;缺省时跳过二级摘要,仅保留一级剪裁。 */
|
|
746
1077
|
router?: ModelRouter;
|
|
1078
|
+
/** 转发给文件观察提取;项目根不作为历史工具相对路径的推断依据。 */
|
|
747
1079
|
projectRoot?: string;
|
|
1080
|
+
/** 当前已注册的内置工具名集合;剪裁白名单只认可其中的内置只读工具。 */
|
|
748
1081
|
builtInToolNames?: () => ReadonlySet<string>;
|
|
1082
|
+
/** 每个摘要 attempt 的使用量回调;在候选校验前触发,失败摘要仍计消耗。 */
|
|
749
1083
|
onSummaryUsage?: (attemptId: string, usage: Usage | undefined, modelId: string) => Promise<void>;
|
|
750
1084
|
}
|
|
751
1085
|
/** 原始历史与请求视图之间的协调器;不执行工具、Hook 或任何终端操作。 */
|
|
@@ -767,15 +1101,21 @@ declare class ContextManager {
|
|
|
767
1101
|
/** 添加本次交互终态,使下一次准备的保护范围与恢复后的边界一致。 */
|
|
768
1102
|
finishInteraction(terminal: InteractionTerminal): void;
|
|
769
1103
|
/**
|
|
770
|
-
* 最终 Hook
|
|
1104
|
+
* 最终 Hook 之后统一预算;Profile 与请求的更低覆盖同时约束输出预留和实际 maxTokens。
|
|
1105
|
+
* 非法输出配置或输入超限在 Provider 调用前失败;不使用固定 4096 覆盖原生模型上限。
|
|
1106
|
+
* 签名或加密续答可禁止压缩;此时仍估算并拒绝超预算请求,不剪裁历史或调用摘要网络。
|
|
1107
|
+
* @param allowCompaction 是否允许当前请求剪裁/摘要;冻结私有续答前缀时传 false。
|
|
771
1108
|
* @returns 语义事件及准备完成的请求,不改变持久历史。
|
|
772
1109
|
*/
|
|
773
|
-
prepare(history: readonly CanonicalMessage[], request: ModelRequest, profile: ModelProfile, runId?: string, reason?: CompactionReason): AsyncGenerator<SessionEvent, ModelRequest>;
|
|
1110
|
+
prepare(history: readonly CanonicalMessage[], request: ModelRequest, profile: ModelProfile, runId?: string, reason?: CompactionReason, allowCompaction?: boolean): AsyncGenerator<SessionEvent, ModelRequest>;
|
|
774
1111
|
/** 观测真实使用量只作用于对应最终请求,不把累计 usage 当成当前窗口占用。 */
|
|
775
1112
|
observe(request: ModelRequest, usage: Usage, profile: ModelProfile): void;
|
|
776
1113
|
/** 查询已发布快照,不运行 Hook/Provider;配置变化后明确标记 stale。 */
|
|
777
1114
|
getSnapshot(): ContextSnapshot | undefined;
|
|
778
|
-
/**
|
|
1115
|
+
/**
|
|
1116
|
+
* 从当前静态投影估算,不执行 Hook/Provider 或更新状态;恢复后即可查看检查点。
|
|
1117
|
+
* 输出预留与准备请求使用同一 Profile/请求覆盖规则;非法覆盖也在只读检查时失败。
|
|
1118
|
+
*/
|
|
779
1119
|
inspect(history: readonly CanonicalMessage[], request: ModelRequest, profile: ModelProfile): ContextSnapshot;
|
|
780
1120
|
/** 模型或指令发生变化后使已有预算快照过期,直到下一最终请求准备成功。 */
|
|
781
1121
|
invalidate(): void;
|
|
@@ -785,7 +1125,7 @@ declare class ContextManager {
|
|
|
785
1125
|
private publishSnapshot;
|
|
786
1126
|
/** 最终请求与只读估算共用来源和状态计数。 */
|
|
787
1127
|
private buildSnapshot;
|
|
788
|
-
/**
|
|
1128
|
+
/** 摘要与技能重载事实由已验证原历史生成,不升级为系统指令或恢复旧授权;旧检查点同样适用。 */
|
|
789
1129
|
private summaryMessage;
|
|
790
1130
|
/**
|
|
791
1131
|
* 旧轮次仅在工具事务闭合时可摘要;成功可由旧记录推断,失败或中断必须有运行终态来源。
|
|
@@ -805,6 +1145,7 @@ declare class ContextManager {
|
|
|
805
1145
|
private report;
|
|
806
1146
|
}
|
|
807
1147
|
|
|
1148
|
+
/** 摘要模型的 JSON 输出契约;只有通过 validateSummary 的实例可进入检查点。 */
|
|
808
1149
|
interface ContextSummary {
|
|
809
1150
|
schemaVersion: 1;
|
|
810
1151
|
goal: string;
|
|
@@ -814,8 +1155,10 @@ interface ContextSummary {
|
|
|
814
1155
|
pendingWork: string[];
|
|
815
1156
|
/** 原消息身份,不接受模型虚构来源或把旧摘要当成授权。 */
|
|
816
1157
|
references: string[];
|
|
1158
|
+
/** 结果未知的副作用记录;保留不确定性,不冒充成功或失败。 */
|
|
817
1159
|
unknownEffects: string[];
|
|
818
1160
|
}
|
|
1161
|
+
/** 一次文件操作的历史观察;只描述曾经发生,不描述文件当前状态。 */
|
|
819
1162
|
interface FileObservation {
|
|
820
1163
|
/** 有执行目录证据时为绝对路径;旧记录缺少依据时为原相对路径,基目录未知。 */
|
|
821
1164
|
path: string;
|
|
@@ -823,11 +1166,14 @@ interface FileObservation {
|
|
|
823
1166
|
toolCallId: string;
|
|
824
1167
|
operation: string;
|
|
825
1168
|
}
|
|
1169
|
+
/** 检查点携带的文件事实:按结果分类的观察列表,每类上限 500 条。 */
|
|
826
1170
|
interface FileDetails {
|
|
827
1171
|
readFiles: FileObservation[];
|
|
828
1172
|
modifiedFiles: FileObservation[];
|
|
829
1173
|
failedFileOperations: FileObservation[];
|
|
1174
|
+
/** 工具结果缺失或 OUTCOME_UNKNOWN 时的操作记录。 */
|
|
830
1175
|
unknownFileOperations: FileObservation[];
|
|
1176
|
+
/** 超出 500 上限被丢弃的观察总数;渲染时与列表截断数合并展示。 */
|
|
831
1177
|
omitted: number;
|
|
832
1178
|
}
|
|
833
1179
|
/** 对超长结果保留总计 2000 code point 首尾正文,省略说明不冒充原始结果。 */
|
|
@@ -846,21 +1192,27 @@ declare function extractFileDetails(history: readonly CanonicalMessage[], _proje
|
|
|
846
1192
|
declare function renderFileDetails(details: FileDetails): string;
|
|
847
1193
|
/** 校验结构与来源;空、超长、未知字段及虚构消息引用不能进入检查点。 */
|
|
848
1194
|
declare function validateSummary(value: unknown, sourceIds: ReadonlySet<string>): ContextSummary;
|
|
1195
|
+
/** 摘要服务配置;router 提供摘要角色绑定,全部 attempt 共享总时限。 */
|
|
849
1196
|
interface SummaryServiceOptions {
|
|
850
1197
|
router: ModelRouter;
|
|
1198
|
+
/** 保留旧 API 形参;项目根不作为历史工具相对路径的推断依据。 */
|
|
851
1199
|
projectRoot: string;
|
|
852
1200
|
estimator?: TokenEstimator;
|
|
853
1201
|
/** 真实使用量在候选校验前记账,失败摘要仍保留消耗;不得包含主任务 TTFT。 */
|
|
854
1202
|
onUsage?: (attemptId: string, usage: Usage | undefined, modelId: string) => Promise<void>;
|
|
1203
|
+
/** 单次 generate 的总时限,生效值不超过 120 秒;超时按摘要失败处理。 */
|
|
855
1204
|
timeoutMs?: number;
|
|
856
1205
|
}
|
|
1206
|
+
/** 一次成功摘要的产出与计量;usage 为本次 generate 全部 attempt 的累计值。 */
|
|
857
1207
|
interface SummaryCandidate {
|
|
858
1208
|
summary: ContextSummary;
|
|
859
1209
|
details: FileDetails;
|
|
1210
|
+
/** 实际发起的摘要模型调用次数;上限 4。 */
|
|
860
1211
|
calls: number;
|
|
861
1212
|
modelId: string;
|
|
862
1213
|
durationMs: number;
|
|
863
1214
|
usage: Usage;
|
|
1215
|
+
/** 任一 attempt 缺失 usage 时为 false,不将缺失消耗伪装为零。 */
|
|
864
1216
|
usageKnown: boolean;
|
|
865
1217
|
}
|
|
866
1218
|
/** 摘要单次流式输出文本最大防御倍数;防止模型无限输出耗尽内存。 */
|
|
@@ -873,12 +1225,16 @@ declare class SummaryService {
|
|
|
873
1225
|
/**
|
|
874
1226
|
* 按完整用户交互分批,以旧摘要和新增原始前缀取材;中间结果只是局部候选,不激活。
|
|
875
1227
|
* 单个不可拆分输入超预算、无终态、截断或非法来源均失败,取消先关闭 Provider 迭代。
|
|
1228
|
+
* 摘要输出独立限制为 4096,并受 Profile 更低覆盖及窗口约束;非法 Profile 预算先失败。
|
|
876
1229
|
*/
|
|
877
1230
|
generate(history: readonly CanonicalMessage[], previous?: Pick<SummaryCandidate, 'summary' | 'details'>, signal?: AbortSignal): Promise<SummaryCandidate>;
|
|
878
1231
|
}
|
|
879
1232
|
|
|
1233
|
+
/** 事件严重度;debug / trace 仅在 StructuredLogger 显式开启 debug 时写入。 */
|
|
880
1234
|
type LogLevel = 'trace' | 'debug' | 'info' | 'warn' | 'error';
|
|
1235
|
+
/** 事件通道:operation 为运行日志,audit 为审批与操作审计;两者持久化保证与保留策略不同。 */
|
|
881
1236
|
type LogChannel = 'operation' | 'audit';
|
|
1237
|
+
/** 允许落盘的字段值类型;复杂结构必须先降维为标量或受控摘要。 */
|
|
882
1238
|
type LogField = string | number | boolean | null;
|
|
883
1239
|
/** 单条可持久化的脱敏运行或审计事件。 */
|
|
884
1240
|
interface LogEvent {
|
|
@@ -898,6 +1254,7 @@ interface LogEvent {
|
|
|
898
1254
|
interface LogSink {
|
|
899
1255
|
write(event: LogEvent): Promise<void>;
|
|
900
1256
|
}
|
|
1257
|
+
/** 调用方提供的宽松输入;fields 仅白名单字段经脱敏校验后进入 LogEvent。 */
|
|
901
1258
|
interface LogInput {
|
|
902
1259
|
level: LogLevel;
|
|
903
1260
|
event: string;
|
|
@@ -954,11 +1311,17 @@ declare class StructuredLogger implements EventLogger {
|
|
|
954
1311
|
private format;
|
|
955
1312
|
}
|
|
956
1313
|
|
|
1314
|
+
/** FileLogSink 的构造项;保留与落盘策略按通道强制,调用方传值不能放宽。 */
|
|
957
1315
|
interface FileLogSinkOptions {
|
|
1316
|
+
/** 日志目录;不存在时按 0o700 创建。 */
|
|
958
1317
|
directory: string;
|
|
1318
|
+
/** 通道前缀,同时决定文件名与保留、同步策略。 */
|
|
959
1319
|
prefix: 'operation' | 'audit';
|
|
1320
|
+
/** 单文件字节上限,超出后换下一个序号文件;缺省 10MB。 */
|
|
960
1321
|
maxBytes?: number;
|
|
1322
|
+
/** 按天保留的天数;仅 operation 通道生效,audit 恒为长期保留。 */
|
|
961
1323
|
retentionDays?: number;
|
|
1324
|
+
/** 每条写入后强制 fsync;audit 通道强制开启。 */
|
|
962
1325
|
sync?: boolean;
|
|
963
1326
|
}
|
|
964
1327
|
/** 将每条 JSONL 事件完整追加到本地文件,并按日期及文件大小轮转。 */
|
|
@@ -972,7 +1335,9 @@ declare class FileLogSink implements LogSink {
|
|
|
972
1335
|
write(event: LogEvent): Promise<void>;
|
|
973
1336
|
/** 返回该通道已占用的字节数,供宿主提示长期保留的审计用量。 */
|
|
974
1337
|
getUsageBytes(): number;
|
|
1338
|
+
/** 优先写入当天未满的最后一个序号文件,超限或无同名文件时开下一个序号。 */
|
|
975
1339
|
private chooseFile;
|
|
1340
|
+
/** 按文件名中的日期删除早于保留期的自有文件;删除依据文件名而非文件 mtime。 */
|
|
976
1341
|
private cleanupExpired;
|
|
977
1342
|
private isOwnedFile;
|
|
978
1343
|
}
|
|
@@ -984,102 +1349,194 @@ declare function createDefaultLogSinks(baseDirectory?: string): {
|
|
|
984
1349
|
/** 启动时保守关闭上次进程未完成的审计事务,绝不将其当作未执行或自动重试。 */
|
|
985
1350
|
declare function recoverIncompleteAudit(directory: string): Promise<number>;
|
|
986
1351
|
|
|
1352
|
+
/** 单个通过全部校验的指令文件;path 为去重后的候选路径,content 是严格 UTF-8 解码的全文。 */
|
|
987
1353
|
interface InstructionSource {
|
|
988
1354
|
path: string;
|
|
989
1355
|
content: string;
|
|
990
1356
|
}
|
|
1357
|
+
/** 一次成功加载的完整结果;sources 按「用户层 → 项目根 → 逐级子目录」顺序排列。 */
|
|
991
1358
|
interface InstructionSnapshot {
|
|
992
1359
|
sources: readonly InstructionSource[];
|
|
1360
|
+
/** 全部来源实际字节数之和(以读取结果为准),供调用方审计体积预算。 */
|
|
993
1361
|
totalBytes: number;
|
|
994
1362
|
}
|
|
995
1363
|
interface InstructionLoadOptions {
|
|
1364
|
+
/** 目录链上界;cwd 必须位于其内,越界直接失败而不是静默截断链。 */
|
|
996
1365
|
projectRoot: string;
|
|
1366
|
+
/** 指令收集的终点目录;相对 projectRoot 的每一层都会尝试读取 AGENTS.md。 */
|
|
997
1367
|
cwd: string;
|
|
1368
|
+
/** 用户层指令文件;缺省为 ~/.kapibala/AGENTS.md,与项目层同路径时去重。 */
|
|
998
1369
|
userFile?: string;
|
|
999
1370
|
}
|
|
1000
|
-
/**
|
|
1371
|
+
/**
|
|
1372
|
+
* 按用户层和项目根至 cwd 的目录链读取指令,全部成功后才返回快照。
|
|
1373
|
+
* 所有路径统一用 native 语义解析(与 resolveSessionProject 的 projectRoot 对齐):JS realpath
|
|
1374
|
+
* 不展开 Windows 8.3 短名(CI 临时目录形如 RUNNER~1),混用会让包含性检查把同一目录误判越界、
|
|
1375
|
+
* 让去重错过同一文件;返回的 source 路径因此是 canonical 形式,调用方按 canonical 比较。
|
|
1376
|
+
*/
|
|
1001
1377
|
declare function loadInstructions(options: InstructionLoadOptions): InstructionSnapshot;
|
|
1002
1378
|
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1379
|
+
/** 安全名称语法;不接受厂商命名空间、路径或连续连字符。 */
|
|
1380
|
+
declare function isSkillName(value: string): boolean;
|
|
1381
|
+
/** 解析常用 Frontmatter 子集;格式错误抛出,未知执行扩展返回不可执行诊断,正文不截断。 */
|
|
1382
|
+
declare function parseSkillDocument(content: string, filePath: string): SkillDocument;
|
|
1383
|
+
|
|
1384
|
+
/** 技能错误仅返回受控文案;retryPolicy 不触发工具内部重试。 */
|
|
1385
|
+
declare function skillFailure(code: string, message: string): KapibalaError;
|
|
1386
|
+
/** 以路径段判断物理边界,允许边界自身但不接受越界或其他盘符。 */
|
|
1387
|
+
declare function isWithinSkillRoot(root: string, target: string): boolean;
|
|
1388
|
+
/** Windows 物理身份忽略盘符/路径大小写,其他平台维持文件系统原义。 */
|
|
1389
|
+
declare function skillPathKey(value: string): string;
|
|
1390
|
+
/** 按 code point 排序,不使用受语言区域影响的比较器。 */
|
|
1391
|
+
declare function compareSkillPaths(left: string, right: string): number;
|
|
1392
|
+
/** 有界 UTF-8 读取;先检查文件类型/大小,增长中的文件也不能突破上限。 */
|
|
1393
|
+
declare function readBoundedSkillText(filePath: string, maximum: number, signal?: AbortSignal): Promise<string>;
|
|
1394
|
+
/** 原文件字节摘要;有效 UTF-8 的重新编码保留 BOM 与换行,不使用规范化正文替代。 */
|
|
1395
|
+
declare function skillDigest(content: string): string;
|
|
1396
|
+
/** 单根发现结果;诊断可包含无效候选,扫描结束后只保留元数据,不常驻正文。 */
|
|
1397
|
+
interface SkillScanResult {
|
|
1398
|
+
skills: SkillMetadata[];
|
|
1399
|
+
diagnostics: SkillDiagnostic[];
|
|
1400
|
+
}
|
|
1401
|
+
/** 扫描单根的技能边界;有界异步遍历,项目未声明信任时实体只供宿主诊断。 */
|
|
1402
|
+
declare function scanSkillRoot(root: SkillRoot, signal?: AbortSignal): Promise<SkillScanResult>;
|
|
1403
|
+
|
|
1404
|
+
/** 使用现有 Unicode 估算器计完整文本,非 ASCII 不按四字符折算。 */
|
|
1405
|
+
declare function estimateSkillTokens(text: string): number;
|
|
1406
|
+
/** 技能元数据的 XML 转义;不把未转义的描述或路径当作目录结构。 */
|
|
1407
|
+
declare function escapeSkillXml(text: string): string;
|
|
1408
|
+
/** 完整目录投影及遗漏统计;text 为空表示最小目录也无法容纳。 */
|
|
1409
|
+
interface SkillCatalog {
|
|
1410
|
+
text: string;
|
|
1411
|
+
included: number;
|
|
1412
|
+
omitted: number;
|
|
1413
|
+
estimatedTokens: number;
|
|
1414
|
+
}
|
|
1415
|
+
/** 生成确定性 L2.5;预算含标题、XML、转义及遗漏提示,只删完整条目。 */
|
|
1416
|
+
declare function formatSkillCatalog(skills: readonly SkillMetadata[], contextWindow?: number, remainingTokens?: number): SkillCatalog;
|
|
1417
|
+
/** 模型目录分页输入;游标只定位快照,不提供信任或正文访问授权。 */
|
|
1418
|
+
interface SkillQuery {
|
|
1419
|
+
query?: string;
|
|
1420
|
+
cursor?: string;
|
|
1421
|
+
limit?: number;
|
|
1422
|
+
}
|
|
1423
|
+
/** 有界模型元数据页;不包含正文,游标按实际返回条目推进。 */
|
|
1424
|
+
interface SkillQueryPage {
|
|
1425
|
+
skills: {
|
|
1426
|
+
name: string;
|
|
1427
|
+
description: string;
|
|
1428
|
+
scope: string;
|
|
1429
|
+
argumentHint?: string;
|
|
1430
|
+
}[];
|
|
1431
|
+
total: number;
|
|
1432
|
+
nextCursor?: string;
|
|
1011
1433
|
}
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1434
|
+
|
|
1435
|
+
/** 确定性技能注册表;刷新原子替换快照,运行中加载必须与发现指纹一致。 */
|
|
1436
|
+
declare class SkillRegistry {
|
|
1437
|
+
private readonly options;
|
|
1438
|
+
private definitions;
|
|
1439
|
+
private warnings;
|
|
1440
|
+
private revisionValue;
|
|
1441
|
+
/** 固定宿主配置副本,外部对象变更不能静默改变本轮发现或信任。 */
|
|
1442
|
+
constructor(options: SkillOptions);
|
|
1443
|
+
/** 获取独立诊断副本,调用方不能修改生效候选或权限。 */
|
|
1444
|
+
get diagnostics(): SkillDiagnostic[];
|
|
1445
|
+
/** 快照游标身份随正文、来源、可用性变化而失效。 */
|
|
1446
|
+
get revision(): string;
|
|
1447
|
+
/** 是否由宿主启用;关闭只影响本次能力,不删除历史或文件。 */
|
|
1448
|
+
get enabled(): boolean;
|
|
1449
|
+
/** 每轮开始重新发现;取消不发布半个快照,非法单文件只产生诊断。 */
|
|
1450
|
+
refresh(signal?: AbortSignal): Promise<void>;
|
|
1451
|
+
/** 按名称查询胜出元数据副本;未知名称返回 undefined,正文仅由 load 校验后懒读取。 */
|
|
1452
|
+
get(name: string): SkillMetadata | undefined;
|
|
1453
|
+
/** 宿主可检查不可用条目;模型/用户视图分别施加调用控制,均不读取正文。 */
|
|
1454
|
+
list(caller?: 'host' | 'model' | 'user', query?: string): SkillMetadata[];
|
|
1455
|
+
/** 按当前快照校验调用控制与物理身份,不接受模型提供的信任或 caller 升级。 */
|
|
1456
|
+
load(name: string, caller: 'model' | 'user', signal?: AbortSignal): Promise<SkillDefinition>;
|
|
1457
|
+
/** 查询可信模型集合;过期游标可修正,预算不足不返回不前进的分页。 */
|
|
1458
|
+
queryModel(input?: SkillQuery, remainingTokens?: number): SkillQueryPage;
|
|
1459
|
+
/** 复核配置根、词法发现路径和原文件字节,返回本次有界读取文本;漂移要求新运行,不能热替换。 */
|
|
1460
|
+
verify(skill: SkillMetadata, signal?: AbortSignal): Promise<string>;
|
|
1017
1461
|
}
|
|
1018
|
-
/**
|
|
1019
|
-
* 按 uname 输出分类 bash 家族:Git Bash/MSYS2/Cygwin 归 native bash,
|
|
1020
|
-
* Windows 上报告 Linux 的是 WSL launcher(独立 kind,路径语义不同)。
|
|
1021
|
-
*/
|
|
1022
|
-
declare function classifyBashOutput(unameOutput: string, platform: NodeJS.Platform): ShellKind | undefined;
|
|
1023
|
-
/**
|
|
1024
|
-
* 发现并固定实际解释器:只用 PATH 与用户显式给出的路径,不写死任何安装目录。
|
|
1025
|
-
* auto 在 Windows 上按 native Bash → WSL → pwsh → powershell 依次兜底,
|
|
1026
|
-
* bash 家族用运行时 uname 探针区分 Git Bash 与 WSL launcher。
|
|
1027
|
-
*/
|
|
1028
|
-
declare function detectShell(options: ShellDetectOptions): ShellEnvironment;
|
|
1029
1462
|
|
|
1030
1463
|
/** 本次会话的自动执行边界;FullAccess 只能由宿主为本次会话显式选择。 */
|
|
1031
1464
|
type SessionMode = 'Approval' | 'Plan' | 'Auto' | 'FullAccess';
|
|
1465
|
+
/** 规则动作与裁决结果共用同一词表;ask 表示交人工审批,不是放行。 */
|
|
1032
1466
|
type PermissionAction = 'allow' | 'ask' | 'deny';
|
|
1467
|
+
/** 一次 Shell 执行的精确目标;字段组合构成审批与缓存命中的身份,任何维度都不允许通配。 */
|
|
1033
1468
|
interface ShellScope {
|
|
1034
1469
|
command: string;
|
|
1035
1470
|
interpreter: string;
|
|
1036
1471
|
executablePath?: string;
|
|
1037
1472
|
cwd: string;
|
|
1473
|
+
/** 解释器身份摘要(路径、大小、mtime 推导);解释器被替换后规则与缓存即失配。 */
|
|
1038
1474
|
executableDigest?: string;
|
|
1475
|
+
/** 命令直接引用的工作目录内脚本的内容摘要;仅单脚本命令可计算,组合命令缺省。 */
|
|
1039
1476
|
scriptDigest?: string;
|
|
1477
|
+
/** 执行前复核用指纹;审批后重新推导不一致时按目标漂移拒绝执行。 */
|
|
1040
1478
|
executionFingerprint?: string;
|
|
1041
1479
|
}
|
|
1480
|
+
/** 宿主提供的显式规则;tool / capability / shell 三种范围必须恰好声明一个。 */
|
|
1042
1481
|
interface PermissionRule {
|
|
1043
1482
|
action: PermissionAction;
|
|
1044
1483
|
tool?: string;
|
|
1045
1484
|
capability?: Capability;
|
|
1046
1485
|
shell?: ShellScope;
|
|
1047
1486
|
}
|
|
1487
|
+
/** 权限裁决的全部输入;硬限制与缓存命中由宿主查询后以标志位传入,策略本身不持有状态。 */
|
|
1048
1488
|
interface PermissionRequest {
|
|
1049
1489
|
mode: SessionMode;
|
|
1050
1490
|
toolName: string;
|
|
1051
1491
|
capabilities: readonly Capability[];
|
|
1492
|
+
/** 危险性来自工具元数据;未命中 allow 规则时,非 FullAccess 模式一律降级为 ask。 */
|
|
1052
1493
|
dangerous?: boolean;
|
|
1494
|
+
/** 宿主硬限制;命中即拒绝,优先级高于一切规则与缓存。 */
|
|
1053
1495
|
hardDenied?: boolean;
|
|
1054
1496
|
shell?: ShellScope;
|
|
1497
|
+
/** 规则表须先经 validatePermissionRules 校验;同动作规则按声明顺序取首个命中。 */
|
|
1055
1498
|
rules?: readonly PermissionRule[];
|
|
1499
|
+
/** 会话缓存命中标志(来自 SessionApprovalCache);仅在 deny / ask 规则都未命中时生效。 */
|
|
1056
1500
|
cachedApproval?: boolean;
|
|
1501
|
+
/** 会话缓存拒绝标志;在 deny 规则之后、ask 规则之前生效。 */
|
|
1057
1502
|
cachedDenial?: boolean;
|
|
1058
1503
|
}
|
|
1504
|
+
/** 裁决结果及其来源;source 供审计区分硬限制、Plan、规则、缓存与模式默认。 */
|
|
1059
1505
|
interface PermissionDecision {
|
|
1060
1506
|
decision: PermissionAction;
|
|
1061
1507
|
source: 'hard_limit' | 'plan' | 'explicit_rule' | 'session_cache' | 'mode_default';
|
|
1508
|
+
/** 命中规则在 rules 中的下标;仅 source 为 explicit_rule 时存在。 */
|
|
1062
1509
|
ruleIndex?: number;
|
|
1063
1510
|
}
|
|
1064
1511
|
/** 拒绝含糊的 Shell 前缀/通配符和结构不完整的规则。 */
|
|
1065
1512
|
declare function validatePermissionRules(rules: readonly PermissionRule[]): void;
|
|
1066
1513
|
/** 纯策略裁决;宿主硬限制和 Plan 始终先于任何规则或会话缓存。 */
|
|
1067
1514
|
declare class PermissionPolicy {
|
|
1515
|
+
/**
|
|
1516
|
+
* 裁决顺序固定:硬限制 → Plan 收窄 → deny 规则 → 会话缓存拒绝 → ask 规则 →
|
|
1517
|
+
* 会话缓存允许 → allow 规则(run_command 必须带 shell 范围,工具级 allow 不能放行命令)
|
|
1518
|
+
* → 模式默认。同一输入总是得到同一裁决,不读取任何外部状态。
|
|
1519
|
+
*/
|
|
1068
1520
|
decide(request: PermissionRequest): PermissionDecision;
|
|
1069
1521
|
}
|
|
1070
1522
|
/** 只向模型展示当前模式与整项拒绝规则允许请求的工具。 */
|
|
1071
1523
|
declare function visibleTools(tools: readonly Tool[], mode: SessionMode, rules?: readonly PermissionRule[]): Tool[];
|
|
1072
1524
|
|
|
1525
|
+
/** 执行能力的最小分类;权限规则、模式裁决与工具元数据声明共用这一词汇表。 */
|
|
1073
1526
|
type Capability = 'fs:read' | 'fs:write' | 'exec' | 'net:outbound' | 'env:read' | 'agent:spawn';
|
|
1074
1527
|
interface ToolMetadata {
|
|
1528
|
+
/** 注册来源标记(如 'builtin');注册表据此执行冲突策略并支持按源注销。 */
|
|
1075
1529
|
source?: string;
|
|
1530
|
+
/** 标记危险工具;未命中 allow 规则时,非 FullAccess 模式一律降级为 ask 而非按模式默认放行。 */
|
|
1076
1531
|
dangerous?: boolean;
|
|
1077
1532
|
/** 所需能力的声明;v0.0.1 不据此授权,v0.0.2 的执行前权限决策才会消费。 */
|
|
1078
1533
|
permissions?: Capability[];
|
|
1079
1534
|
/** 工具自身等待进程资源回收,执行器不可再使用通用 Promise.race 超时。 */
|
|
1080
1535
|
managesTimeout?: boolean;
|
|
1081
1536
|
}
|
|
1537
|
+
/** 工具执行期由宿主注入的运行环境;信号取消后工具应尽快清理在途资源再返回。 */
|
|
1082
1538
|
interface ToolContext {
|
|
1539
|
+
/** 本次执行的沙箱根目录;文件类工具以此为界解析相对路径并拒绝越界。 */
|
|
1083
1540
|
rootDir: string;
|
|
1084
1541
|
signal?: AbortSignal;
|
|
1085
1542
|
logger?: (message: string) => void;
|
|
@@ -1090,74 +1547,53 @@ interface ToolContext {
|
|
|
1090
1547
|
outputBytes: number;
|
|
1091
1548
|
}) => void;
|
|
1092
1549
|
}
|
|
1550
|
+
/** 工具契约;parameters 是 JSON Schema,模型只能看到经 toToolDefinition 投影后的三个字段。 */
|
|
1093
1551
|
interface Tool<TInput = Record<string, unknown>, TOutput = unknown> {
|
|
1094
1552
|
readonly name: string;
|
|
1095
1553
|
readonly description: string;
|
|
1096
1554
|
readonly parameters: Record<string, unknown>;
|
|
1097
1555
|
readonly metadata?: ToolMetadata;
|
|
1556
|
+
/** 声明本次输入的精确执行目标,供审批展示与执行前指纹复核;无固定执行目标的工具不实现。 */
|
|
1098
1557
|
approvalScope?(input: TInput, rootDir: string): ShellScope;
|
|
1099
1558
|
execute(input: TInput, ctx: ToolContext): Promise<TOutput>;
|
|
1100
1559
|
}
|
|
1560
|
+
/** 恒等构造器:只为工具实现提供类型推断与统一书写形式,不产生任何运行时包装。 */
|
|
1101
1561
|
declare function defineTool<TInput = Record<string, unknown>, TOutput = unknown>(tool: Tool<TInput, TOutput>): Tool<TInput, TOutput>;
|
|
1562
|
+
/** 投影为模型可见的工具描述;刻意丢弃 metadata 与 approvalScope,执行侧信息不进入协议。 */
|
|
1102
1563
|
declare function toToolDefinition(tool: Tool): ToolDefinition;
|
|
1103
1564
|
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
timeout_ms?: number;
|
|
1109
|
-
}
|
|
1110
|
-
interface CommandResult {
|
|
1111
|
-
exitCode: number | null;
|
|
1112
|
-
output: string;
|
|
1113
|
-
outputTruncated: boolean;
|
|
1114
|
-
outputFile?: string;
|
|
1115
|
-
outputFileTruncated: boolean;
|
|
1116
|
-
outputBytes: number;
|
|
1117
|
-
shell: ShellEnvironment['kind'];
|
|
1118
|
-
}
|
|
1119
|
-
/** 仅清理受管 UUID 文件,避免删除用户在同目录保存的结果。 */
|
|
1120
|
-
declare function cleanupCommandResults(rootDir: string, now?: number): void;
|
|
1121
|
-
/** 使用固定解释器执行命令;超时和中止必须先处理进程树再返回。 */
|
|
1122
|
-
declare function createRunCommandTool(environment: ShellEnvironment): Tool<RunCommandInput, CommandResult>;
|
|
1565
|
+
/** 归一化技能白名单;参数模式/未知名称不得退化成宽泛命令授权。 */
|
|
1566
|
+
declare function normalizeSkillTools(skill: SkillDefinition, tools: readonly Tool[]): ReadonlySet<string> | undefined;
|
|
1567
|
+
/** 多活动技能白名单取交集;未声明不参与,空集合不等于无限制。 */
|
|
1568
|
+
declare function intersectSkillTools(activations: Iterable<Pick<SkillActivation, 'allowedTools'>>): ReadonlySet<string> | undefined;
|
|
1123
1569
|
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
}
|
|
1132
|
-
declare class PathSandbox {
|
|
1133
|
-
readonly rootDir: string;
|
|
1134
|
-
readonly allowSymlinks: boolean;
|
|
1135
|
-
private cachedRealRootDir?;
|
|
1136
|
-
constructor(config: SandboxConfig);
|
|
1137
|
-
/**
|
|
1138
|
-
* rootDir 的物理路径。rootDir 自身可能是一个符号链接(如 macOS 的 /var → /private/var),
|
|
1139
|
-
* 必须先归一化,否则物理路径与 rootDir 比较时会产生误判。
|
|
1140
|
-
*/
|
|
1141
|
-
private get realRootDir();
|
|
1142
|
-
/**
|
|
1143
|
-
* 检查并解析安全路径
|
|
1144
|
-
* 1. 解析相对路径为相对于 rootDir 的绝对路径
|
|
1145
|
-
* 2. 词法检查:`..` 归一化后必须仍在 rootDir 内
|
|
1146
|
-
* 3. 物理检查:解析符号链接后的真实路径必须仍在 rootDir 内
|
|
1147
|
-
*/
|
|
1148
|
-
resolveSafePath(targetPath: string): string;
|
|
1149
|
-
/**
|
|
1150
|
-
* 把词法路径映射为物理路径。
|
|
1151
|
-
*
|
|
1152
|
-
* 关键点在于**向上回溯到最近一个真实存在的祖先**再取 realpath,然后把尚未存在的尾段拼回去:
|
|
1153
|
-
* - 目标已存在 → 直接 realpath,符号链接立刻被展开;
|
|
1154
|
-
* - 目标不存在(典型场景:write_file 新建文件) → 若只看目标本身,所有校验都会被跳过,
|
|
1155
|
-
* 于是 `write_file('linkDir/new.txt')` 可以借道指向外部的 linkDir 越界写入。
|
|
1156
|
-
* 回溯到 linkDir 取 realpath 即可识破这种穿透。
|
|
1157
|
-
*/
|
|
1158
|
-
private toPhysicalPath;
|
|
1570
|
+
/** 受控文本范围;offset 用于 UTF-8 大行续读,不能与行范围混用。 */
|
|
1571
|
+
interface SkillResourceInput {
|
|
1572
|
+
name: string;
|
|
1573
|
+
path: string;
|
|
1574
|
+
startLine?: number;
|
|
1575
|
+
endLine?: number;
|
|
1576
|
+
offset?: number;
|
|
1159
1577
|
}
|
|
1578
|
+
/** 只读资源页;nextOffset 是完整 UTF-8 字符边界,不代表文件执行授权。 */
|
|
1579
|
+
interface SkillResourcePage {
|
|
1580
|
+
text: string;
|
|
1581
|
+
truncated: boolean;
|
|
1582
|
+
nextOffset?: number;
|
|
1583
|
+
}
|
|
1584
|
+
/** 读取技能根内文本;复核物理边界,有界返回并关闭文件,不扩大普通文件沙箱。 */
|
|
1585
|
+
declare function readSkillResource(skill: SkillDefinition, input: SkillResourceInput, signal?: AbortSignal): Promise<SkillResourcePage>;
|
|
1586
|
+
/** 限量列出包内资源名而不读取正文;外部链接、默认忽略目录及深层资源不展开。 */
|
|
1587
|
+
declare function listSkillResources(skill: SkillDefinition, signal?: AbortSignal): Promise<{
|
|
1588
|
+
files: string[];
|
|
1589
|
+
truncated: boolean;
|
|
1590
|
+
}>;
|
|
1160
1591
|
|
|
1592
|
+
/**
|
|
1593
|
+
* 人工审批的五选一结果。allow_once / deny_once 只作用于本次执行;
|
|
1594
|
+
* allow_session / deny_session 由执行器按精确调用范围写入会话缓存(allow_session 还要求请求允许会话级缓存);
|
|
1595
|
+
* cancel 表示放弃审批,一律拒绝且绝不进入缓存。
|
|
1596
|
+
*/
|
|
1161
1597
|
type ApprovalChoice = 'allow_once' | 'allow_session' | 'deny_once' | 'deny_session' | 'cancel';
|
|
1162
1598
|
/** 宿主展示实际执行目标并取得人工选择;无通道视为非交互拒绝。 */
|
|
1163
1599
|
interface ApprovalRequest {
|
|
@@ -1166,8 +1602,10 @@ interface ApprovalRequest {
|
|
|
1166
1602
|
input: Record<string, unknown>;
|
|
1167
1603
|
rootDir: string;
|
|
1168
1604
|
shell?: ShellScope;
|
|
1605
|
+
/** 宿主是否允许本次请求写入会话级允许缓存;deny_session 的缓存不受此限制。 */
|
|
1169
1606
|
sessionAllowed: boolean;
|
|
1170
1607
|
}
|
|
1608
|
+
/** 宿主实现的审批入口;未提供通道时执行器按非交互直接拒绝,通道抛错视同 cancel。 */
|
|
1171
1609
|
interface ApprovalChannel {
|
|
1172
1610
|
requestApproval(request: ApprovalRequest, signal?: AbortSignal): Promise<ApprovalChoice>;
|
|
1173
1611
|
}
|
|
@@ -1181,7 +1619,12 @@ declare class SessionApprovalCache {
|
|
|
1181
1619
|
set(request: ApprovalRequest, choice: 'allow' | 'deny'): void;
|
|
1182
1620
|
}
|
|
1183
1621
|
|
|
1622
|
+
/** 同名工具冲突策略:error 直接抛错;prefer-builtin 保留既有内置注册;prefer-last 后到者覆盖。 */
|
|
1184
1623
|
type ConflictPolicy = 'error' | 'prefer-builtin' | 'prefer-last';
|
|
1624
|
+
/**
|
|
1625
|
+
* 按名称索引的工具注册表。冲突按构造时给定的策略处理,胜出条目的来源标签保持不变;
|
|
1626
|
+
* 来源标签支持按源批量注册与注销,注销只移除以该标签注册的工具,不回收冲突中保留的内置实现。
|
|
1627
|
+
*/
|
|
1185
1628
|
declare class ToolRegistry {
|
|
1186
1629
|
private readonly tools;
|
|
1187
1630
|
private readonly toolSources;
|
|
@@ -1195,57 +1638,30 @@ declare class ToolRegistry {
|
|
|
1195
1638
|
registerSource(source: string, tools: Tool[]): void;
|
|
1196
1639
|
unregisterSource(source: string): void;
|
|
1197
1640
|
get(name: string): Tool | undefined;
|
|
1641
|
+
/** 注销指定名称及来源记录;调用方须核对实例身份,不能移除同名替代工具。 */
|
|
1642
|
+
unregister(name: string): void;
|
|
1643
|
+
/** 未命中时抛 ToolNotFound,并携带当前全部可用名称辅助模型自纠。 */
|
|
1198
1644
|
resolve(name: string): Tool;
|
|
1199
1645
|
list(): Tool[];
|
|
1200
1646
|
definitions(): ToolDefinition[];
|
|
1201
1647
|
}
|
|
1202
1648
|
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
path: string;
|
|
1210
|
-
content: string;
|
|
1211
|
-
}, string>;
|
|
1212
|
-
declare const editFileTool: Tool<{
|
|
1213
|
-
path: string;
|
|
1214
|
-
targetContent: string;
|
|
1215
|
-
replacementContent: string;
|
|
1216
|
-
}, string>;
|
|
1217
|
-
declare const globTool: Tool<{
|
|
1218
|
-
pattern: string;
|
|
1219
|
-
subDirectory?: string;
|
|
1220
|
-
}, string>;
|
|
1221
|
-
declare const grepTool: Tool<{
|
|
1222
|
-
pattern: string;
|
|
1223
|
-
path?: string;
|
|
1224
|
-
}, string>;
|
|
1225
|
-
declare const builtinTools: (Tool<{
|
|
1226
|
-
path: string;
|
|
1227
|
-
startLine?: number;
|
|
1228
|
-
endLine?: number;
|
|
1229
|
-
}, string> | Tool<{
|
|
1230
|
-
path: string;
|
|
1231
|
-
content: string;
|
|
1232
|
-
}, string> | Tool<{
|
|
1233
|
-
path: string;
|
|
1234
|
-
targetContent: string;
|
|
1235
|
-
replacementContent: string;
|
|
1236
|
-
}, string> | Tool<{
|
|
1237
|
-
pattern: string;
|
|
1238
|
-
subDirectory?: string;
|
|
1239
|
-
}, string> | Tool<{
|
|
1240
|
-
pattern: string;
|
|
1241
|
-
path?: string;
|
|
1242
|
-
}, string>)[];
|
|
1243
|
-
|
|
1649
|
+
/**
|
|
1650
|
+
* 运行时钩子契约:宿主与插件在固定切点观察或调整流程。
|
|
1651
|
+
* 通知型切点由 HookRegistry.emit 串行触发;需要返回值的 model:before / tool:before
|
|
1652
|
+
* 由执行方(AgentLoop / ToolExecutor)自行遍历并消费返回值。
|
|
1653
|
+
*/
|
|
1654
|
+
/** 钩子切点;session:start / session:end 由会话初始化与销毁触发,其余在单次交互流程内触发。 */
|
|
1244
1655
|
type HookPoint = 'session:start' | 'session:end' | 'model:before' | 'model:after' | 'tool:before' | 'tool:after' | 'error';
|
|
1656
|
+
/** 传给每个钩子的运行上下文;signal 与当前执行的取消联动,未提供表示该切点不可取消。 */
|
|
1245
1657
|
interface HookContext {
|
|
1246
1658
|
readonly signal?: AbortSignal;
|
|
1247
1659
|
readonly logger?: (msg: string) => void;
|
|
1248
1660
|
}
|
|
1661
|
+
/**
|
|
1662
|
+
* tool:before 的裁决结果:continue 放行;skip 不执行工具,以 result 作为工具结果落盘;
|
|
1663
|
+
* modify 用替换后的入参继续(仍会经过后续权限裁决,不构成绕过审批的通道)。
|
|
1664
|
+
*/
|
|
1249
1665
|
type ToolDecision = {
|
|
1250
1666
|
action: 'continue';
|
|
1251
1667
|
} | {
|
|
@@ -1256,10 +1672,12 @@ type ToolDecision = {
|
|
|
1256
1672
|
action: 'modify';
|
|
1257
1673
|
input: Record<string, unknown>;
|
|
1258
1674
|
};
|
|
1675
|
+
/** 各切点的处理器签名;未注册的切点直接跳过,处理器抛出的异常向上传播。 */
|
|
1259
1676
|
interface HookHandlers {
|
|
1260
1677
|
'session:start'?: (ctx: HookContext) => void | Promise<void>;
|
|
1261
1678
|
'session:end'?: (ctx: HookContext) => void | Promise<void>;
|
|
1262
1679
|
'model:before'?: (ctx: HookContext, req: ModelRequest) => ModelRequest | Promise<ModelRequest>;
|
|
1680
|
+
/** 收到的是即将进入历史的完整 assistant 消息;执行方会校验钩子未改动身份与受保护内容。 */
|
|
1263
1681
|
'model:after'?: (ctx: HookContext, res: {
|
|
1264
1682
|
message: CanonicalMessage;
|
|
1265
1683
|
}) => void | Promise<void>;
|
|
@@ -1279,30 +1697,38 @@ interface HookHandlers {
|
|
|
1279
1697
|
error?: (ctx: HookContext, err: Error) => void | Promise<void>;
|
|
1280
1698
|
}
|
|
1281
1699
|
|
|
1700
|
+
/** 按切点收集钩子的有序注册表;同一处理器可注册多次,触发时按注册顺序执行。 */
|
|
1282
1701
|
declare class HookRegistry {
|
|
1283
1702
|
private readonly hooks;
|
|
1703
|
+
/** 注册到指定切点;不查重,触发顺序即注册顺序。 */
|
|
1284
1704
|
on<K extends HookPoint>(point: K, handler: NonNullable<HookHandlers[K]>): void;
|
|
1705
|
+
/** 返回该切点的处理器列表(无注册时为空数组);数组为内部引用,调用方不应修改。 */
|
|
1285
1706
|
get<K extends HookPoint>(point: K): Array<NonNullable<HookHandlers[K]>>;
|
|
1707
|
+
/**
|
|
1708
|
+
* 串行 await 通知型切点的全部处理器;任一抛错立即中断并向调用方传播。
|
|
1709
|
+
* 需要返回值的 model:before / tool:before 不经 emit,由执行方自行遍历。
|
|
1710
|
+
*/
|
|
1286
1711
|
emit<K extends 'session:start' | 'session:end' | 'model:after' | 'tool:after' | 'error'>(point: K, ctx: HookContext, ...args: any[]): Promise<void>;
|
|
1287
1712
|
}
|
|
1288
1713
|
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
readonly name: string;
|
|
1297
|
-
readonly version?: string;
|
|
1298
|
-
setup(ctx: PluginContext): void | Promise<void>;
|
|
1299
|
-
teardown?(): void | Promise<void>;
|
|
1714
|
+
/** ToolExecutor 的构造项;审计写入需要 eventLogger 与 sessionId 同时提供,否则不产生审计。 */
|
|
1715
|
+
/** 宿主运行级硬限制;优先于模式、审批及缓存,文案和字段不得携带凭据。 */
|
|
1716
|
+
interface ToolHardDenial {
|
|
1717
|
+
code: string;
|
|
1718
|
+
message: string;
|
|
1719
|
+
retryPolicy: ToolResultBlock['retryPolicy'];
|
|
1720
|
+
fields?: Record<string, unknown>;
|
|
1300
1721
|
}
|
|
1301
|
-
|
|
1302
1722
|
interface ExecutorOptions {
|
|
1723
|
+
/** 在最终参数上补充硬拒绝,不能把拒绝转为授权。 */
|
|
1724
|
+
hardDenial?: (tool: Tool, input: Record<string, unknown>) => ToolHardDenial | undefined;
|
|
1725
|
+
/** 结果钩子和执行审计完成后调用;用于在同批次下一调用前安装宿主收紧状态。 */
|
|
1726
|
+
onToolResult?: (tool: Tool, input: Record<string, unknown>, result: ToolResultBlock) => void | Promise<void>;
|
|
1303
1727
|
tools: ToolRegistry;
|
|
1304
1728
|
hooks: HookRegistry;
|
|
1729
|
+
/** 工具执行的绝对根目录;路径解析与审批均以此为基准。 */
|
|
1305
1730
|
rootDir: string;
|
|
1731
|
+
/** 会话级取消信号;与 cancel() 的内部信号合并生效。 */
|
|
1306
1732
|
signal?: AbortSignal;
|
|
1307
1733
|
logger?: (msg: string) => void;
|
|
1308
1734
|
/** 单次工具执行超时(毫秒)。异步挂起的工具超时后返回 isError 结果,避免永久卡死会话循环 */
|
|
@@ -1310,12 +1736,22 @@ interface ExecutorOptions {
|
|
|
1310
1736
|
eventLogger?: EventLogger;
|
|
1311
1737
|
sessionId?: string;
|
|
1312
1738
|
runId?: string;
|
|
1739
|
+
/** 固定模式或每次裁决时读取的取值函数;宿主可中途切换。 */
|
|
1313
1740
|
mode?: SessionMode | (() => SessionMode);
|
|
1314
1741
|
permissionRules?: readonly PermissionRule[];
|
|
1742
|
+
/** 审批入口;缺省视为非交互环境,需要询问的调用一律拒绝。 */
|
|
1315
1743
|
approvalChannel?: ApprovalChannel;
|
|
1744
|
+
/** 会话内 allow_session / deny_session 审批缓存;缺省使用实例私有缓存。 */
|
|
1316
1745
|
approvalCache?: SessionApprovalCache;
|
|
1317
1746
|
}
|
|
1747
|
+
/**
|
|
1748
|
+
* 工具执行器:按「审计 → 钩子裁决 → 权限裁决 → 执行 → 结果闭合」处理单次调用。
|
|
1749
|
+
* 所有拒绝与异常都闭合为 isError 的工具结果,不向调用方抛出;
|
|
1750
|
+
* 同一 run 内参数完全相同的 never 失败会被拒绝重复执行。
|
|
1751
|
+
*/
|
|
1318
1752
|
declare class ToolExecutor {
|
|
1753
|
+
private readonly hardDenial?;
|
|
1754
|
+
private readonly onToolResult?;
|
|
1319
1755
|
private readonly tools;
|
|
1320
1756
|
private readonly hooks;
|
|
1321
1757
|
private readonly rootDir;
|
|
@@ -1335,12 +1771,16 @@ declare class ToolExecutor {
|
|
|
1335
1771
|
constructor(options: ExecutorOptions);
|
|
1336
1772
|
/** 中止本轮正在等待或执行的工具;调用方仍须等待 runAll 完成资源回收及结果审计。 */
|
|
1337
1773
|
cancel(): void;
|
|
1774
|
+
/**
|
|
1775
|
+
* 串行执行全部调用并返回与入参顺序一致的结果;某个结果要求用户介入后,其余调用直接拒绝。
|
|
1776
|
+
* onResult 在每个最终结果(含拒绝/失败)产生后提供独立快照,供在途预算估算,不代表历史已提交;回调不得抛错。
|
|
1777
|
+
*/
|
|
1338
1778
|
runAll(calls: ToolUseBlock[], onProgress?: (event: {
|
|
1339
1779
|
id: string;
|
|
1340
1780
|
name: string;
|
|
1341
1781
|
elapsedMs: number;
|
|
1342
1782
|
outputBytes: number;
|
|
1343
|
-
}) => void): Promise<ToolResultBlock[]>;
|
|
1783
|
+
}) => void, onResult?: (result: ToolResultBlock) => void): Promise<ToolResultBlock[]>;
|
|
1344
1784
|
/**
|
|
1345
1785
|
* 在最终参数上裁决并审计一次调用,所有拒绝和异常均闭合为工具结果。
|
|
1346
1786
|
* 为调用记录本次绝对工具根目录,供跨目录恢复后的文件事实解析;不使用历史目录授权。
|
|
@@ -1362,6 +1802,238 @@ declare class ToolExecutor {
|
|
|
1362
1802
|
private withTimeout;
|
|
1363
1803
|
}
|
|
1364
1804
|
|
|
1805
|
+
/** 加载预检结果;只有最终成功工具结果或真实用户消息提交后才形成活动资格。 */
|
|
1806
|
+
interface PreparedSkill {
|
|
1807
|
+
/** 本轮完整正文仍可见时只引用,引用结果不附完整正文来源。 */
|
|
1808
|
+
alreadyPresent?: boolean;
|
|
1809
|
+
definition: SkillDefinition;
|
|
1810
|
+
source: SkillContentSource;
|
|
1811
|
+
text: string;
|
|
1812
|
+
allowedTools?: ReadonlySet<string>;
|
|
1813
|
+
}
|
|
1814
|
+
/** 会话内技能运行状态;控制工具按实例身份识别,pending 只收紧而不授予资源。 */
|
|
1815
|
+
declare class SkillRuntime {
|
|
1816
|
+
readonly registry: SkillRegistry;
|
|
1817
|
+
private readonly tools;
|
|
1818
|
+
private readonly onResourceRead?;
|
|
1819
|
+
readonly loaderTool: Tool;
|
|
1820
|
+
readonly queryTool: Tool;
|
|
1821
|
+
readonly resourceTool: Tool;
|
|
1822
|
+
private readonly controls;
|
|
1823
|
+
private readonly prepared;
|
|
1824
|
+
private readonly active;
|
|
1825
|
+
private runId;
|
|
1826
|
+
private history;
|
|
1827
|
+
private projection;
|
|
1828
|
+
private reservedTokens;
|
|
1829
|
+
private remaining;
|
|
1830
|
+
/** 注册表和工具集合属于一个Session;不跨会话共享授权或隐式目录。 */
|
|
1831
|
+
constructor(registry: SkillRegistry, tools: ToolRegistry, onResourceRead?: ((source: SkillContentSource, input: SkillResourceInput, page: SkillResourcePage) => Promise<void>) | undefined);
|
|
1832
|
+
/**
|
|
1833
|
+
* 开始新运行;历史正文只作事实,必须由本轮加载重新激活。
|
|
1834
|
+
* remaining 必须扣除在途 assistant 和普通工具结果;尚未提交的成功技能正文由本类独立预留,不重复扣除。
|
|
1835
|
+
*/
|
|
1836
|
+
begin(runId: string, history: () => readonly CanonicalMessage[], remaining: () => number, projection?: () => readonly CanonicalMessage[]): void;
|
|
1837
|
+
/** 按实际集合安装实例工具;同名插件不覆盖为受信任工具,也不能制造目录引导。 */
|
|
1838
|
+
syncTools(): void;
|
|
1839
|
+
/** 清空本轮状态;调用方必须先等待工具和历史事务清理,不能提前释放会话锁。 */
|
|
1840
|
+
clear(): void;
|
|
1841
|
+
/** 查询本轮活动状态副本,避免宿主修改限制。 */
|
|
1842
|
+
get activations(): SkillActivation[];
|
|
1843
|
+
/** 预检正文、限制和输入空间;调用方由宿主固定,不读取模型伪造caller。 */
|
|
1844
|
+
prepare(name: string, caller: 'model' | 'user', signal?: AbortSignal): Promise<PreparedSkill>;
|
|
1845
|
+
/** 真实用户入口安装pending;正文需进入用户消息,提交前不提供资源。 */
|
|
1846
|
+
acceptUser(prepared: PreparedSkill): void;
|
|
1847
|
+
/** 最终工具结果通过执行器钩子/审计后安装收紧;同名或仿造结果不激活。 */
|
|
1848
|
+
acceptResult(tool: Tool, result: ToolResultBlock): void;
|
|
1849
|
+
/** 业务调用的动态硬拒绝;控制工具仅按实例豁免,仍须宿主权限裁决。 */
|
|
1850
|
+
denial(tool: Tool): ToolHardDenial | undefined;
|
|
1851
|
+
/** 完整消息提交后记录审计再发布事件;失败不回滚历史,但不提交资源资格。 */
|
|
1852
|
+
commit(audit: (source: SkillContentSource, messageId: string) => Promise<void>, persistence: 'memory' | 'disk'): Promise<SessionEvent[]>;
|
|
1853
|
+
/** 检查最终模型投影的完整活动指导;钩子或压缩不能移走后继续执行。 */
|
|
1854
|
+
assertProjection(messages: readonly CanonicalMessage[]): void;
|
|
1855
|
+
/** 只读本轮已提交技能资源;文件/来源漂移要求重新开始,不重放脚本。 */
|
|
1856
|
+
readResource(input: SkillResourceInput, signal?: AbortSignal): Promise<SkillResourcePage>;
|
|
1857
|
+
}
|
|
1858
|
+
|
|
1859
|
+
/** 技能正文在实际投影中的状态;来源只关联事实,present 不代表当前运行获得授权。 */
|
|
1860
|
+
interface SkillContextState {
|
|
1861
|
+
source: SkillContentSource;
|
|
1862
|
+
messageId: string;
|
|
1863
|
+
state: 'present' | 'requires_reload';
|
|
1864
|
+
}
|
|
1865
|
+
/** 查找真实投影中的完整正文来源;字段缺失、未知版本或文本摘要不匹配不用于去重。 */
|
|
1866
|
+
declare function findSkillContent(history: readonly CanonicalMessage[], source: SkillContentSource): {
|
|
1867
|
+
messageId: string;
|
|
1868
|
+
source: SkillContentSource;
|
|
1869
|
+
} | undefined;
|
|
1870
|
+
/** 比较原始来源与真实投影,以最后一次相同文件/版本正文为准;不恢复旧授权或执行动作。 */
|
|
1871
|
+
declare function inspectSkillContent(raw: readonly CanonicalMessage[], projected: readonly CanonicalMessage[]): SkillContextState[];
|
|
1872
|
+
/** 将离开投影的旧正文表示为有界重载事实;最多50条,遗漏数明确,不把历史包装成当前指令。 */
|
|
1873
|
+
declare function renderSkillReloads(covered: readonly CanonicalMessage[]): string;
|
|
1874
|
+
|
|
1875
|
+
/** 受支持的解释器家族;wsl 是 Windows 上的 WSL bash launcher,路径与输出语义与 native bash 不同。 */
|
|
1876
|
+
type ShellKind = 'bash' | 'wsl' | 'pwsh' | 'powershell';
|
|
1877
|
+
/** auto / 具名解释器,或用户显式提供的解释器可执行文件全路径。 */
|
|
1878
|
+
type ShellPreference = 'auto' | ShellKind | (string & {});
|
|
1879
|
+
/** 探测成功后固定的解释器身份;executableDigest 参与命令审批指纹,解释器被替换即失配。 */
|
|
1880
|
+
interface ShellEnvironment {
|
|
1881
|
+
kind: ShellKind;
|
|
1882
|
+
executable: string;
|
|
1883
|
+
executableDigest: string;
|
|
1884
|
+
cwd: string;
|
|
1885
|
+
}
|
|
1886
|
+
interface ShellDetectOptions {
|
|
1887
|
+
/** 'auto'、具名解释器,或解释器可执行文件全路径;含路径分隔符或 .exe 后缀按显式路径处理。 */
|
|
1888
|
+
preference?: ShellPreference;
|
|
1889
|
+
/** 探测与后续执行的基准目录;探测前先取 realpath,符号链接会被展开。 */
|
|
1890
|
+
cwd: string;
|
|
1891
|
+
/** 以下两项供测试注入平台与 PATH;缺省取当前进程的值。 */
|
|
1892
|
+
platform?: NodeJS.Platform;
|
|
1893
|
+
pathEnv?: string;
|
|
1894
|
+
}
|
|
1895
|
+
/**
|
|
1896
|
+
* 按 uname 输出分类 bash 家族:Git Bash/MSYS2/Cygwin 归 native bash,
|
|
1897
|
+
* Windows 上报告 Linux 的是 WSL launcher(独立 kind,路径语义不同)。
|
|
1898
|
+
*/
|
|
1899
|
+
declare function classifyBashOutput(unameOutput: string, platform: NodeJS.Platform): ShellKind | undefined;
|
|
1900
|
+
/**
|
|
1901
|
+
* 发现并固定实际解释器:只用 PATH 与用户显式给出的路径,不写死任何安装目录。
|
|
1902
|
+
* auto 在 Windows 上按 native Bash → WSL → pwsh → powershell 依次兜底,
|
|
1903
|
+
* bash 家族用运行时 uname 探针区分 Git Bash 与 WSL launcher。
|
|
1904
|
+
*/
|
|
1905
|
+
declare function detectShell(options: ShellDetectOptions): ShellEnvironment;
|
|
1906
|
+
|
|
1907
|
+
/** run_command 的输入;cwd 相对工作区根解析,timeout_ms 合法区间 1..600000,越界在执行前失败。 */
|
|
1908
|
+
interface RunCommandInput {
|
|
1909
|
+
[key: string]: unknown;
|
|
1910
|
+
command: string;
|
|
1911
|
+
cwd?: string;
|
|
1912
|
+
timeout_ms?: number;
|
|
1913
|
+
}
|
|
1914
|
+
/** 命令执行结果;output 只是内联前缀,完整输出超过内联上限的部分落盘为受管结果文件。 */
|
|
1915
|
+
interface CommandResult {
|
|
1916
|
+
/** 进程正常退出时的退出码;被信号终止时为 null。 */
|
|
1917
|
+
exitCode: number | null;
|
|
1918
|
+
/** stdout 与 stderr 交错合并的文本,最多前 64 KiB(按 UTF-8 字符边界截断)。 */
|
|
1919
|
+
output: string;
|
|
1920
|
+
/** output 并非全部输出的标志;超出内联上限的完整内容写入 outputFile。 */
|
|
1921
|
+
outputTruncated: boolean;
|
|
1922
|
+
/** 受管结果文件路径(工作区内 .kapibala/tool-results/),仅输出超过内联上限时存在。 */
|
|
1923
|
+
outputFile?: string;
|
|
1924
|
+
/** 结果文件触及 16 MiB 上限后被截断的标志。 */
|
|
1925
|
+
outputFileTruncated: boolean;
|
|
1926
|
+
/** 两个流产生的原始字节总数(不受截断影响),供进度与审计使用。 */
|
|
1927
|
+
outputBytes: number;
|
|
1928
|
+
shell: ShellEnvironment['kind'];
|
|
1929
|
+
}
|
|
1930
|
+
/** 仅清理受管 UUID 文件,避免删除用户在同目录保存的结果。 */
|
|
1931
|
+
declare function cleanupCommandResults(rootDir: string, now?: number): void;
|
|
1932
|
+
/** 使用固定解释器执行命令;超时和中止必须先处理进程树再返回。 */
|
|
1933
|
+
declare function createRunCommandTool(environment: ShellEnvironment): Tool<RunCommandInput, CommandResult>;
|
|
1934
|
+
|
|
1935
|
+
/** 路径沙箱配置;rootDir 是所有文件访问的词法与物理边界。 */
|
|
1936
|
+
interface SandboxConfig {
|
|
1937
|
+
rootDir: string;
|
|
1938
|
+
/**
|
|
1939
|
+
* v0.0.1 恒为严格模式:任何解析后落在 rootDir 之外的物理路径一律拒绝,
|
|
1940
|
+
* 不存在 fail-open 开关。该字段为 v0.0.2 权限引擎预留,当前不改变校验行为。
|
|
1941
|
+
*/
|
|
1942
|
+
allowSymlinks?: boolean;
|
|
1943
|
+
}
|
|
1944
|
+
/**
|
|
1945
|
+
* 把文件访问限制在 rootDir 内:先做词法归一化检查,再展开符号链接做物理检查,两层都通过才放行。
|
|
1946
|
+
* 实例会缓存 rootDir 的物理路径,应按 rootDir 复用实例,而不是每次调用新建。
|
|
1947
|
+
*/
|
|
1948
|
+
declare class PathSandbox {
|
|
1949
|
+
readonly rootDir: string;
|
|
1950
|
+
readonly allowSymlinks: boolean;
|
|
1951
|
+
private cachedRealRootDir?;
|
|
1952
|
+
constructor(config: SandboxConfig);
|
|
1953
|
+
/**
|
|
1954
|
+
* rootDir 的物理路径。rootDir 自身可能是一个符号链接(如 macOS 的 /var → /private/var),
|
|
1955
|
+
* 必须先归一化,否则物理路径与 rootDir 比较时会产生误判。
|
|
1956
|
+
*/
|
|
1957
|
+
private get realRootDir();
|
|
1958
|
+
/**
|
|
1959
|
+
* 检查并解析安全路径
|
|
1960
|
+
* 1. 解析相对路径为相对于 rootDir 的绝对路径
|
|
1961
|
+
* 2. 词法检查:`..` 归一化后必须仍在 rootDir 内
|
|
1962
|
+
* 3. 物理检查:解析符号链接后的真实路径必须仍在 rootDir 内
|
|
1963
|
+
*/
|
|
1964
|
+
resolveSafePath(targetPath: string): string;
|
|
1965
|
+
/**
|
|
1966
|
+
* 把词法路径映射为物理路径。
|
|
1967
|
+
*
|
|
1968
|
+
* 关键点在于**向上回溯到最近一个真实存在的祖先**再取 realpath,然后把尚未存在的尾段拼回去:
|
|
1969
|
+
* - 目标已存在 → 直接 realpath,符号链接立刻被展开;
|
|
1970
|
+
* - 目标不存在(典型场景:write_file 新建文件) → 若只看目标本身,所有校验都会被跳过,
|
|
1971
|
+
* 于是 `write_file('linkDir/new.txt')` 可以借道指向外部的 linkDir 越界写入。
|
|
1972
|
+
* 回溯到 linkDir 取 realpath 即可识破这种穿透。
|
|
1973
|
+
*/
|
|
1974
|
+
private toPhysicalPath;
|
|
1975
|
+
}
|
|
1976
|
+
|
|
1977
|
+
declare const readFileTool: Tool<{
|
|
1978
|
+
path: string;
|
|
1979
|
+
startLine?: number;
|
|
1980
|
+
endLine?: number;
|
|
1981
|
+
}, string>;
|
|
1982
|
+
declare const writeFileTool: Tool<{
|
|
1983
|
+
path: string;
|
|
1984
|
+
content: string;
|
|
1985
|
+
}, string>;
|
|
1986
|
+
declare const editFileTool: Tool<{
|
|
1987
|
+
path: string;
|
|
1988
|
+
targetContent: string;
|
|
1989
|
+
replacementContent: string;
|
|
1990
|
+
}, string>;
|
|
1991
|
+
declare const globTool: Tool<{
|
|
1992
|
+
pattern: string;
|
|
1993
|
+
subDirectory?: string;
|
|
1994
|
+
}, string>;
|
|
1995
|
+
declare const grepTool: Tool<{
|
|
1996
|
+
pattern: string;
|
|
1997
|
+
path?: string;
|
|
1998
|
+
}, string>;
|
|
1999
|
+
/** 五个内置文件工具的聚合;全部经 PathSandbox 校验,路径越界或符号链接穿透一律拒绝。 */
|
|
2000
|
+
declare const builtinTools: (Tool<{
|
|
2001
|
+
path: string;
|
|
2002
|
+
startLine?: number;
|
|
2003
|
+
endLine?: number;
|
|
2004
|
+
}, string> | Tool<{
|
|
2005
|
+
path: string;
|
|
2006
|
+
content: string;
|
|
2007
|
+
}, string> | Tool<{
|
|
2008
|
+
path: string;
|
|
2009
|
+
targetContent: string;
|
|
2010
|
+
replacementContent: string;
|
|
2011
|
+
}, string> | Tool<{
|
|
2012
|
+
pattern: string;
|
|
2013
|
+
subDirectory?: string;
|
|
2014
|
+
}, string> | Tool<{
|
|
2015
|
+
pattern: string;
|
|
2016
|
+
path?: string;
|
|
2017
|
+
}, string>)[];
|
|
2018
|
+
|
|
2019
|
+
/** setup 收到的能力入口;tools / hooks 是会话共享的注册表,注册的条目影响后续所有执行。 */
|
|
2020
|
+
interface PluginContext {
|
|
2021
|
+
readonly tools: ToolRegistry;
|
|
2022
|
+
readonly hooks: HookRegistry;
|
|
2023
|
+
/** 本次会话的绝对工具根目录;工具的路径解析与审批都以此为基准。 */
|
|
2024
|
+
readonly rootDir: string;
|
|
2025
|
+
readonly logger?: (msg: string) => void;
|
|
2026
|
+
}
|
|
2027
|
+
/** 插件生命周期:setup 在会话空闲时挂载执行一次,teardown 在会话销毁时逐个调用以回收资源。 */
|
|
2028
|
+
interface AgentPlugin {
|
|
2029
|
+
/** 用于挂载失败与 teardown 的诊断信息。 */
|
|
2030
|
+
readonly name: string;
|
|
2031
|
+
readonly version?: string;
|
|
2032
|
+
setup(ctx: PluginContext): void | Promise<void>;
|
|
2033
|
+
teardown?(): void | Promise<void>;
|
|
2034
|
+
}
|
|
2035
|
+
|
|
2036
|
+
/** Chat 端点配置;能力覆盖只改变请求字段和完成边界,不改变协议身份。 */
|
|
1365
2037
|
interface OpenAIProviderOptions {
|
|
1366
2038
|
baseURL: string;
|
|
1367
2039
|
apiKey: string;
|
|
@@ -1369,35 +2041,158 @@ interface OpenAIProviderOptions {
|
|
|
1369
2041
|
supportsThinking?: boolean;
|
|
1370
2042
|
/** 回传 assistant 思考历史;默认仅为 DeepSeek 官方端点启用,自建兼容网关可显式开启。 */
|
|
1371
2043
|
replayReasoningContent?: boolean;
|
|
2044
|
+
/** 显式端点能力;缺省保留 max_tokens、usage 和 temperature 请求基线,要求 DONE。 */
|
|
2045
|
+
chatCapabilities?: ChatCapabilities;
|
|
1372
2046
|
/** 建立 HTTP 连接(收到响应头)的超时毫秒数;仅约束建连阶段,不限制流式读取总时长 */
|
|
1373
2047
|
connectTimeoutMs?: number;
|
|
1374
2048
|
}
|
|
2049
|
+
/** 将 canonical 历史投影为 Chat 请求,在完整终态和全部工具验证后交付可执行结果。 */
|
|
1375
2050
|
declare class OpenAICompatibleProvider implements ModelProvider {
|
|
1376
2051
|
readonly name = "openai-compatible";
|
|
1377
2052
|
readonly baseURL: string;
|
|
1378
2053
|
readonly apiKey: string;
|
|
2054
|
+
/** 未提供凭据时由 Session 在记录用户消息前拒绝请求。 */
|
|
2055
|
+
get credentialsReady(): boolean;
|
|
1379
2056
|
readonly modelName: string;
|
|
1380
2057
|
readonly supportsThinking: boolean;
|
|
1381
2058
|
readonly replayReasoningContent: boolean;
|
|
1382
2059
|
readonly connectTimeoutMs: number;
|
|
1383
|
-
|
|
2060
|
+
readonly chatCapabilities: Readonly<ChatCapabilities>;
|
|
2061
|
+
/** 暴露实际请求目标供原子绑定校验,不包含凭据。 */
|
|
2062
|
+
get binding(): {
|
|
2063
|
+
protocol: "openai-compatible";
|
|
2064
|
+
baseURL: string;
|
|
2065
|
+
modelName: string;
|
|
2066
|
+
};
|
|
2067
|
+
/**
|
|
2068
|
+
* 初始化 Chat 协议适配;显式端点能力优先,思考回传独立于模型 supportsThinking 标签。
|
|
2069
|
+
* @param options 请求目标、凭据和能力配置;缺省只有 DeepSeek 官方端点回传可见思考。
|
|
2070
|
+
*/
|
|
1384
2071
|
constructor(options: OpenAIProviderOptions);
|
|
1385
|
-
/**
|
|
2072
|
+
/**
|
|
2073
|
+
* 发送 Chat 流式请求;默认同时要求合法 finish_reason 和 DONE,只有显式例外允许完整 EOF。
|
|
2074
|
+
* 全部工具的 ID、名称和对象参数在任何完成事件之前校验;截断、过滤或中断不交付工具。
|
|
2075
|
+
* @param req canonical 历史、工具、预算与取消信号;能力覆盖来自端点配置。
|
|
2076
|
+
* @returns 文本、可见思考和工具增量,完整响应最后产生 message_stop。
|
|
2077
|
+
* @throws ModelError 响应缺少合法终态、工具身份或完整对象参数时失败,不自动重放请求。
|
|
2078
|
+
*/
|
|
1386
2079
|
create(req: ModelRequest): AsyncIterable<ModelEvent>;
|
|
2080
|
+
/**
|
|
2081
|
+
* 将工具结果装配为 canonical tool 消息,wire 角色仅在下一请求投影时转换。
|
|
2082
|
+
* @param results 已完成的工具结果,保持调用 ID 与内容,不重放工具。
|
|
2083
|
+
* @returns 逐条克隆的 canonical tool 消息,允许消息级落盘。
|
|
2084
|
+
*/
|
|
1387
2085
|
assembleToolResults(results: ToolResultBlock[]): CanonicalMessage[];
|
|
1388
|
-
/**
|
|
2086
|
+
/**
|
|
2087
|
+
* 将历史投影为 Chat 消息;合并连续 user,剔除空 assistant,保持工具调用与结果身份。
|
|
2088
|
+
* 仅显式允许的端点回传可见思考;签名、脱敏思考和原生私有 Item 不适用于 Chat。
|
|
2089
|
+
* @param messages canonical 历史;只投影文本、可选可见思考和工具语义。
|
|
2090
|
+
* @param systemPrompt 可选全局提示词,去除首尾空白后作为首条 system。
|
|
2091
|
+
* @returns 与输入历史独立的 Chat wire 消息数组。
|
|
2092
|
+
*/
|
|
1389
2093
|
private translateMessagesToWire;
|
|
1390
2094
|
}
|
|
1391
2095
|
|
|
2096
|
+
/** Messages 原生连接配置;baseURL 包含版本前缀,workspaceId 仅选择请求工作区。 */
|
|
2097
|
+
interface AnthropicProviderOptions {
|
|
2098
|
+
baseURL: string;
|
|
2099
|
+
apiKey: string;
|
|
2100
|
+
modelName: string;
|
|
2101
|
+
connectTimeoutMs?: number;
|
|
2102
|
+
workspaceId?: string;
|
|
2103
|
+
}
|
|
2104
|
+
/** 独立 Messages 适配器;复用 canonical 工具事务及来源校验,不拥有工具执行权限。 */
|
|
2105
|
+
declare class AnthropicProvider implements ModelProvider {
|
|
2106
|
+
readonly name = "anthropic";
|
|
2107
|
+
readonly baseURL: string;
|
|
2108
|
+
readonly modelName: string;
|
|
2109
|
+
readonly workspaceId?: string;
|
|
2110
|
+
readonly connectTimeoutMs: number;
|
|
2111
|
+
private readonly apiKey;
|
|
2112
|
+
/** 表明该适配器已装载非空凭据,不暴露凭据内容。 */
|
|
2113
|
+
get credentialsReady(): boolean;
|
|
2114
|
+
/** 暴露不含凭据的请求目标;用于宿主校验 Profile 与 Provider 的原子绑定。 */
|
|
2115
|
+
get binding(): {
|
|
2116
|
+
protocol: "anthropic";
|
|
2117
|
+
baseURL: string;
|
|
2118
|
+
modelName: string;
|
|
2119
|
+
workspaceId: string | undefined;
|
|
2120
|
+
};
|
|
2121
|
+
/** 初始化连接;无效 URL/凭据/超时在请求前失败,错误不回显配置值。 */
|
|
2122
|
+
constructor(options: AnthropicProviderOptions);
|
|
2123
|
+
/** 装配统一 tool 历史;Messages 的 user/tool_result 转换只作用于请求视图。 */
|
|
2124
|
+
assembleToolResults(results: ToolResultBlock[]): CanonicalMessage[];
|
|
2125
|
+
/**
|
|
2126
|
+
* 发送 Messages 流并按索引组装完整内容;增量仅供展示,终态提供有序落盘来源。
|
|
2127
|
+
* maxTokens 包含思考消耗,缺省为 4096;缺终态、坏参数与服务端流错误均抛错。
|
|
2128
|
+
* pause_turn 表示服务端工具暂停;当前不支持其续答,明确失败而非报告完成或调度客户端工具。
|
|
2129
|
+
* 同 run/模型/端点的来源校验允许私有续答;来源不匹配的可见思考降为普通文字。
|
|
2130
|
+
* 取消或消费者提前结束会等待 reader 清理并解除 HTTP 取消监听,不自动重发请求。
|
|
2131
|
+
*/
|
|
2132
|
+
create(req: ModelRequest): AsyncIterable<ModelEvent>;
|
|
2133
|
+
/**
|
|
2134
|
+
* 投影 system、普通内容与工具结果;连续结果合并为下一 user 内容并置于文字之前。
|
|
2135
|
+
* 私有块由共享来源规则筛选,原始消息和工具 ID 不被改写;不发送能力标签推测的思考参数。
|
|
2136
|
+
*/
|
|
2137
|
+
private createPayload;
|
|
2138
|
+
}
|
|
2139
|
+
|
|
2140
|
+
/** 原生 Responses 的端点、模型及建连配置;输出预算由每次请求指定。 */
|
|
2141
|
+
interface OpenAIResponsesProviderOptions {
|
|
2142
|
+
baseURL: string;
|
|
2143
|
+
apiKey: string;
|
|
2144
|
+
modelName: string;
|
|
2145
|
+
supportsThinking?: boolean;
|
|
2146
|
+
connectTimeoutMs?: number;
|
|
2147
|
+
}
|
|
2148
|
+
/** 原生 Responses 适配器拥有手动无状态历史,完成边界与 Agent 的 canonical 工具事务一致。 */
|
|
2149
|
+
declare class OpenAIResponsesProvider implements ModelProvider {
|
|
2150
|
+
readonly name = "openai-responses";
|
|
2151
|
+
readonly binding: {
|
|
2152
|
+
protocol: 'openai-responses';
|
|
2153
|
+
modelName: string;
|
|
2154
|
+
baseURL: string;
|
|
2155
|
+
};
|
|
2156
|
+
private readonly options;
|
|
2157
|
+
/** 表明该适配器已装载非空凭据,不暴露凭据内容。 */
|
|
2158
|
+
get credentialsReady(): boolean;
|
|
2159
|
+
/** 绑定明确模型与端点;不从模型名推断协议,也不在失败后自动切换协议。非法配置在请求前失败。 */
|
|
2160
|
+
constructor(options: OpenAIResponsesProviderOptions);
|
|
2161
|
+
/** 消费完整 Responses 流,增量仅用于展示;终态与全部 Item 合法后才交付工具完成和最终内容。 */
|
|
2162
|
+
create(request: ModelRequest): AsyncIterable<ModelEvent>;
|
|
2163
|
+
/** 保持消息级 canonical tool 落盘,Responses function_call_output 仅存在于请求投影。 */
|
|
2164
|
+
assembleToolResults(results: ToolResultBlock[]): CanonicalMessage[];
|
|
2165
|
+
/** 服务端错误仅保留明确标识,不回显请求、输出正文、签名或加密 reasoning。 */
|
|
2166
|
+
private throwWireError;
|
|
2167
|
+
/** 只接受真实且合法的计数;缺失 usage 保持未知,推理 token 已包含在 output_tokens 中。 */
|
|
2168
|
+
private toUsage;
|
|
2169
|
+
}
|
|
2170
|
+
|
|
2171
|
+
/** 分层提示选项;目录受实际工具定义和请求剩余空间约束,不能用同名工具冒充加载能力。 */
|
|
1392
2172
|
interface PromptAssemblerOptions {
|
|
2173
|
+
/** 可信技能快照;仅匹配加载工具实例实际可见时注入L2.5。 */
|
|
2174
|
+
skills?: SkillRegistry;
|
|
2175
|
+
skillLoader?: Tool;
|
|
2176
|
+
contextWindow?: number;
|
|
2177
|
+
skillBudgetTokens?: number;
|
|
2178
|
+
/** 提供时才生成 L2 工具清单段;缺省表示本次运行不向模型展示任何工具。 */
|
|
1393
2179
|
tools?: ToolRegistry;
|
|
2180
|
+
/** L3 环境段展示的工作区根目录;文件工具的沙箱边界与其保持一致。 */
|
|
1394
2181
|
rootDir: string;
|
|
1395
2182
|
agentName?: string;
|
|
2183
|
+
/** 宿主自定义提示词,插在环境段与 L4 指令之间;纯空白视为未提供。 */
|
|
1396
2184
|
customInstructions?: string;
|
|
2185
|
+
/** L4 项目指令段的内容,来自 loadInstructions 快照;为空时整段省略。 */
|
|
1397
2186
|
instructionSources?: readonly InstructionSource[];
|
|
2187
|
+
/** 覆盖 tools.list() 作为 L2 清单来源;通常传入按权限过滤后的可见工具。 */
|
|
1398
2188
|
visibleTools?: readonly Tool[];
|
|
1399
2189
|
}
|
|
2190
|
+
/**
|
|
2191
|
+
* 把行为规则(L1)、工具清单(L2)、环境(L3)与项目指令(L4)拼装为系统提示词。
|
|
2192
|
+
* 各段之间以 `\n\n---\n\n` 分隔;不提供的内容整段跳过,不做占位填充。
|
|
2193
|
+
*/
|
|
1400
2194
|
declare class PromptAssembler {
|
|
2195
|
+
private readonly options;
|
|
1401
2196
|
private readonly tools?;
|
|
1402
2197
|
private readonly rootDir;
|
|
1403
2198
|
private readonly agentName;
|
|
@@ -1405,39 +2200,60 @@ declare class PromptAssembler {
|
|
|
1405
2200
|
private readonly instructionSources;
|
|
1406
2201
|
private readonly visibleTools?;
|
|
1407
2202
|
constructor(options: PromptAssemblerOptions);
|
|
2203
|
+
/** 按固定层级拼装系统提示词;同一选项集总是产出相同文本,便于审计与缓存。 */
|
|
1408
2204
|
assemble(): string;
|
|
2205
|
+
/** 返回与 assemble 相同的目录统计;加载器被隐藏、替换或禁用时省略执行目录。 */
|
|
2206
|
+
getSkillCatalog(): SkillCatalog;
|
|
1409
2207
|
private getL1Behavior;
|
|
1410
2208
|
private getL2Tools;
|
|
1411
2209
|
private getL3Environment;
|
|
1412
2210
|
}
|
|
1413
2211
|
|
|
2212
|
+
/** 循环宿主配置;请求投影与在途预算只操作临时视图,历史仍按完整工具事务追加。 */
|
|
1414
2213
|
interface AgentLoopOptions {
|
|
1415
2214
|
/** 本次交互身份,内部步骤仍独立编号。 */
|
|
1416
2215
|
interactionId?: string;
|
|
2216
|
+
/** 仅用于诊断、用量归因与脱敏说明,不参与请求构造。 */
|
|
1417
2217
|
modelId?: string;
|
|
2218
|
+
modelRole?: PrimaryModelRole;
|
|
2219
|
+
/** 请求视图投影:只允许变换克隆出的请求消息,不得改动真实历史。 */
|
|
1418
2220
|
projectHistory?: (history: readonly CanonicalMessage[]) => CanonicalMessage[];
|
|
2221
|
+
/** 请求准备契约;可在请求前产出事件,并在上下文溢出时参与一次预算重试。 */
|
|
1419
2222
|
prepareRequest?: RequestPreparation;
|
|
2223
|
+
/** 每次收到 usage 后回调;attemptId 标识 run:step:attempt,便于按尝试归因。 */
|
|
1420
2224
|
onUsage?: (request: ModelRequest, usage: Usage, attemptId: string) => Promise<void>;
|
|
2225
|
+
/** 在途批次预算视图;含当前 assistant 和已完成结果,不能作为落盘或授权依据,闭合后清空;回调不得抛错。 */
|
|
2226
|
+
onToolBatchUpdate?: (messages: readonly CanonicalMessage[]) => void;
|
|
1421
2227
|
provider: ModelProvider;
|
|
1422
2228
|
executor: ToolExecutor;
|
|
1423
2229
|
tools: ToolRegistry;
|
|
1424
2230
|
hooks: HookRegistry;
|
|
1425
2231
|
systemPrompt?: string;
|
|
2232
|
+
/** 单次 run 的最大 turn 数,耗尽时以 error 事件收尾;缺省 20。 */
|
|
1426
2233
|
maxSteps?: number;
|
|
2234
|
+
/** 连续工具失败达到该阈值即熔断终止本轮 run;缺省 3。 */
|
|
1427
2235
|
maxConsecutiveErrors?: number;
|
|
1428
2236
|
signal?: AbortSignal;
|
|
1429
2237
|
eventLogger?: EventLogger;
|
|
1430
2238
|
sessionId?: string;
|
|
1431
2239
|
runId?: string;
|
|
2240
|
+
/** 每轮开始时读取;工具可见性与权限模式可随宿主状态变化。 */
|
|
1432
2241
|
getMode?: () => SessionMode;
|
|
1433
2242
|
permissionRules?: readonly PermissionRule[];
|
|
1434
2243
|
}
|
|
2244
|
+
/**
|
|
2245
|
+
* 主执行循环:按 turn 推进「模型请求 → 整批校验 → 工具调度 → 历史闭合」。
|
|
2246
|
+
* run 会就地追加传入的 history,assistant 与全部 tool_result 在同一同步临界段落入,
|
|
2247
|
+
* 消费方在任何对外事件后停止迭代都不会看到半闭合历史(设计文档 §4.3 / §4.4)。
|
|
2248
|
+
*/
|
|
1435
2249
|
declare class AgentLoop {
|
|
1436
2250
|
private readonly interactionId?;
|
|
1437
2251
|
private readonly modelId?;
|
|
2252
|
+
private readonly modelRole?;
|
|
1438
2253
|
private readonly projectHistory?;
|
|
1439
2254
|
private readonly prepareRequest?;
|
|
1440
2255
|
private readonly onUsage?;
|
|
2256
|
+
private readonly onToolBatchUpdate?;
|
|
1441
2257
|
private readonly provider;
|
|
1442
2258
|
private readonly executor;
|
|
1443
2259
|
private readonly tools;
|
|
@@ -1452,49 +2268,79 @@ declare class AgentLoop {
|
|
|
1452
2268
|
private readonly getMode;
|
|
1453
2269
|
private readonly permissionRules;
|
|
1454
2270
|
constructor(options: AgentLoopOptions);
|
|
2271
|
+
/**
|
|
2272
|
+
* 执行一次交互并流式产出事件。
|
|
2273
|
+
*
|
|
2274
|
+
* 语义要点:
|
|
2275
|
+
* - message_stop 携带完整 canonical 消息,是唯一落盘来源;text / thinking 增量仅供展示。
|
|
2276
|
+
* - 模型失败以 error + turn_finish 事件收尾并结束 run(不抛出);用户中止抛 AbortError。
|
|
2277
|
+
* - turn_start / turn_finish 严格配对,错误、熔断与步数耗尽路径均保证收口。
|
|
2278
|
+
*/
|
|
1455
2279
|
run(history: CanonicalMessage[]): AsyncIterable<SessionEvent>;
|
|
1456
2280
|
}
|
|
1457
2281
|
|
|
2282
|
+
/** AgentSession 的构造配置;除主任务绑定外均可缺省,缺省行为见各字段。 */
|
|
1458
2283
|
interface SessionConfig {
|
|
2284
|
+
/** 宿主显式技能配置;缺省不扫描home,项目根需独立信任。 */
|
|
2285
|
+
skills?: SkillOptions;
|
|
1459
2286
|
/** 持久会话身份;有版本化 Store 时须与头部一致,日志实例身份保持独立。 */
|
|
1460
2287
|
conversationId?: string;
|
|
1461
2288
|
tokenEstimator?: TokenEstimator;
|
|
2289
|
+
/** 无 Git 工作树时随 run_started 记录的分支回退值。 */
|
|
1462
2290
|
gitBranch?: string;
|
|
2291
|
+
/** 主任务默认模型绑定;恢复历史及未显式选择角色时使用。 */
|
|
1463
2292
|
defaultProfile: ModelProfile;
|
|
2293
|
+
/** 与 defaultProfile 匹配的协议适配器。 */
|
|
1464
2294
|
defaultProvider: ModelProvider;
|
|
2295
|
+
/** 消息级落盘通道;支持状态能力的 Store 额外启用持久压缩与交互终态。 */
|
|
1465
2296
|
store?: MessageStore;
|
|
2297
|
+
/** 工具执行与会话基目录;projectRoot/cwd 缺省沿用此值。 */
|
|
1466
2298
|
rootDir?: string;
|
|
1467
2299
|
systemPrompt?: string;
|
|
1468
2300
|
maxSteps?: number;
|
|
1469
2301
|
logger?: (msg: string) => void;
|
|
1470
2302
|
eventLogger?: EventLogger;
|
|
1471
2303
|
loggingDirectory?: string;
|
|
2304
|
+
/** 初始权限模式,缺省 Approval;仅空闲时可切换。 */
|
|
1472
2305
|
mode?: SessionMode;
|
|
1473
2306
|
permissionRules?: readonly PermissionRule[];
|
|
1474
2307
|
approvalChannel?: ApprovalChannel;
|
|
1475
2308
|
projectRoot?: string;
|
|
1476
2309
|
cwd?: string;
|
|
1477
2310
|
userInstructionsPath?: string;
|
|
2311
|
+
/** 未提供时使用默认日志 sink 的审计目录;init 时恢复不完整审计。 */
|
|
1478
2312
|
auditRecoveryDirectory?: string;
|
|
1479
2313
|
/** 接收普通运行日志失败的脱敏诊断;诊断失败也不得中断问答或替代审计。 */
|
|
1480
2314
|
onDiagnostic?: (message: string) => void;
|
|
1481
2315
|
}
|
|
2316
|
+
/** getStats 返回的统计快照;全部字段为独立副本,宿主修改不影响会话内部状态。 */
|
|
1482
2317
|
interface SessionStats {
|
|
1483
2318
|
conversationId?: string;
|
|
1484
2319
|
/** 历史存在无 usage 的模型步骤时为 false,不将旧消耗伪装为精确零。 */
|
|
1485
2320
|
usageKnown?: boolean;
|
|
2321
|
+
/** 摘要 attempt 的累计消耗;独立于主任务指标。 */
|
|
1486
2322
|
summaryUsage?: Usage;
|
|
1487
2323
|
summaryUsageKnown?: boolean;
|
|
1488
2324
|
contextSnapshot?: ContextSnapshot;
|
|
2325
|
+
/** 累计用户任务次数,含恢复自持久状态的 run_started 记录。 */
|
|
1489
2326
|
totalTurns: number;
|
|
2327
|
+
/** 主任务累计消耗;摘要消耗在 summaryUsage 单独统计。 */
|
|
1490
2328
|
totalTokens: Usage;
|
|
1491
2329
|
activeModel: string;
|
|
1492
2330
|
loadedToolsCount: number;
|
|
1493
2331
|
lastMetrics?: TurnMetrics;
|
|
1494
2332
|
lastRunMetrics?: RunMetrics;
|
|
2333
|
+
/** 最近一次内部模型请求的窗口占用;无实测时仅提供窗口上限。 */
|
|
1495
2334
|
contextUsage: ContextUsage;
|
|
1496
2335
|
}
|
|
2336
|
+
/**
|
|
2337
|
+
* 宿主无关的 Agent 会话门面:统一模型路由、工具执行、权限与上下文压缩。
|
|
2338
|
+
* 每次用户输入产生一个 run;历史与状态经 MessageStore 持久化,恢复后语义连续。
|
|
2339
|
+
* 运行或清理进行中,改变状态的公开方法一律被拒绝;Core 不承担任何界面职责。
|
|
2340
|
+
*/
|
|
1497
2341
|
declare class AgentSession {
|
|
2342
|
+
private readonly skillRegistry?;
|
|
2343
|
+
private readonly skillRuntime?;
|
|
1498
2344
|
private persistentId;
|
|
1499
2345
|
private readonly configuredConversationId?;
|
|
1500
2346
|
private readonly estimator?;
|
|
@@ -1537,12 +2383,32 @@ declare class AgentSession {
|
|
|
1537
2383
|
private cleanupPromise?;
|
|
1538
2384
|
private sessionEndFinished;
|
|
1539
2385
|
private activeRun;
|
|
2386
|
+
private primaryRole;
|
|
2387
|
+
private lastPrimaryProfile?;
|
|
2388
|
+
private lastPrimaryProvider?;
|
|
1540
2389
|
private readonly plugins;
|
|
1541
2390
|
constructor(config: SessionConfig);
|
|
2391
|
+
/** 幂等初始化:恢复审计残留、指令快照与持久历史,校验会话身份并回放 usage。 */
|
|
1542
2392
|
init(): Promise<void>;
|
|
2393
|
+
/** 挂载插件并执行 setup;销毁后或运行中拒绝,setup 失败的插件不进入已装列表。 */
|
|
1543
2394
|
use(plugin: AgentPlugin): Promise<void>;
|
|
2395
|
+
/**
|
|
2396
|
+
* 空闲时原子替换角色的模型绑定;本方法不改变当前角色或工具权限。
|
|
2397
|
+
* @param profile 模型协议、请求目标和输出预算。
|
|
2398
|
+
* @param role 被配置的主任务或摘要角色。
|
|
2399
|
+
* @param provider 与目标匹配的适配器;省略时只允许复用相同目标。
|
|
2400
|
+
* @throws {ModelError} 协议、模型、端点或 workspace 与适配器错配,旧绑定保留。
|
|
2401
|
+
* @throws {SessionBusyError} 正在执行或清理当前任务。
|
|
2402
|
+
*/
|
|
1544
2403
|
switchModel(profile: ModelProfile, role?: ModelRole, provider?: ModelProvider): void;
|
|
2404
|
+
/** 查询指定绑定或当前明确选择的主任务模型,返回独立配置副本。 */
|
|
1545
2405
|
getActiveProfile(role?: ModelRole): ModelProfile;
|
|
2406
|
+
/** 空闲时显式选择场景;缺少映射或凭据时拒绝,不回退默认模型、不修改权限。 */
|
|
2407
|
+
selectModelRole(role: PrimaryModelRole): void;
|
|
2408
|
+
/** 返回本进程明确选择的场景,历史恢复不改变此状态。 */
|
|
2409
|
+
getModelRole(): PrimaryModelRole;
|
|
2410
|
+
/** 严格解析主任务快照,在输入落盘与网络调用之前检查角色、凭据与预算。 */
|
|
2411
|
+
private resolvePrimaryBinding;
|
|
1546
2412
|
/** 只在空闲时切换本次会话模式;宿主负责 FullAccess 的显式选择交互。 */
|
|
1547
2413
|
switchMode(mode: SessionMode): void;
|
|
1548
2414
|
getMode(): SessionMode;
|
|
@@ -1554,6 +2420,17 @@ declare class AgentSession {
|
|
|
1554
2420
|
getHistory(): CanonicalMessage[];
|
|
1555
2421
|
/** 返回当前投影视图,区别于原始历史;不执行模型或 Hook。 */
|
|
1556
2422
|
getContextHistory(): CanonicalMessage[];
|
|
2423
|
+
/** 查询技能元数据、诊断与本轮活动状态;只读,不加载正文或改变权限。 */
|
|
2424
|
+
getSkills(query?: string): {
|
|
2425
|
+
skills: SkillMetadata[];
|
|
2426
|
+
diagnostics: SkillDiagnostic[];
|
|
2427
|
+
activations: SkillActivation[];
|
|
2428
|
+
content: SkillContextState[];
|
|
2429
|
+
catalog: {
|
|
2430
|
+
included: number;
|
|
2431
|
+
omitted: number;
|
|
2432
|
+
};
|
|
2433
|
+
};
|
|
1557
2434
|
/** 查询最近最终请求的预算快照,尚未准备请求时返回 undefined。 */
|
|
1558
2435
|
getContextSnapshot(): ContextSnapshot | undefined;
|
|
1559
2436
|
/** 执行中止后的工具/摘要与存储收口期间仍返回 true。 */
|
|
@@ -1564,6 +2441,7 @@ declare class AgentSession {
|
|
|
1564
2441
|
getLastMetrics(): TurnMetrics | undefined;
|
|
1565
2442
|
/** 返回最近用户任务指标的独立副本;恢复的 usage 不伪造实时任务指标。 */
|
|
1566
2443
|
getLastRunMetrics(): RunMetrics | undefined;
|
|
2444
|
+
/** 空闲时清空存储与全部内存状态;conversationId 不变,恢复语义等同全新会话。 */
|
|
1567
2445
|
reset(): Promise<void>;
|
|
1568
2446
|
/**
|
|
1569
2447
|
* 执行一次用户输入,并以宿主无关的语义事件流返回全过程。
|
|
@@ -1572,17 +2450,31 @@ declare class AgentSession {
|
|
|
1572
2450
|
* {@link SessionEvent},分别完成展示、交互和中止控制。
|
|
1573
2451
|
*
|
|
1574
2452
|
* @param userInput 用户本轮输入。
|
|
1575
|
-
* @param options
|
|
2453
|
+
* @param options 可选中止信号与显式主任务角色;角色在整个工具任务内固定。
|
|
1576
2454
|
* @returns 文本、思考、工具、指标和错误等结构化事件的异步流。
|
|
1577
2455
|
* @throws {SessionBusyError} 当前会话已有正在执行的 run 时抛出。
|
|
2456
|
+
* @throws {ModelError} 显式角色缺少绑定或凭据时,在输入落盘和网络调用前抛出。
|
|
1578
2457
|
*/
|
|
1579
2458
|
run(userInput: string, options?: {
|
|
1580
2459
|
signal?: AbortSignal;
|
|
2460
|
+
role?: PrimaryModelRole;
|
|
2461
|
+
}): AsyncIterable<SessionEvent>;
|
|
2462
|
+
/** 显式用户技能入口;任务原文单条提交,与普通run共享互斥、取消及清理,不恢复历史授权。 */
|
|
2463
|
+
runSkill(name: string, task: string, options?: {
|
|
2464
|
+
signal?: AbortSignal;
|
|
2465
|
+
role?: PrimaryModelRole;
|
|
1581
2466
|
}): AsyncIterable<SessionEvent>;
|
|
1582
2467
|
/** 单次任务执行主体;调用方 return 的中止与 finally 清理由外层统一协调。 */
|
|
1583
2468
|
private runOperation;
|
|
2469
|
+
/** 幂等销毁:卸载插件后发出 session:end;运行中拒绝,teardown 失败聚合抛出且不完成销毁。 */
|
|
1584
2470
|
destroy(): Promise<void>;
|
|
1585
2471
|
private assertIdle;
|
|
2472
|
+
/** 实际请求、静态统计和手动压缩共用目录空间推导;先计正文、工具和非目录提示,再预算整段目录。 */
|
|
2473
|
+
private createPromptAssembler;
|
|
2474
|
+
/** 完整消息提交后完成技能审计;审计失败保留真实历史,停止本轮且不发布成功事件。 */
|
|
2475
|
+
private commitSkills;
|
|
2476
|
+
/** 显式加载仍按同一读取策略、精确缓存与宿主审批裁决;拒绝/取消/审计失败时不追加用户任务。 */
|
|
2477
|
+
private authorizeSkill;
|
|
1586
2478
|
/**
|
|
1587
2479
|
* 计算最近一次内部模型请求的上下文占用;无 usage 时显式标注预算估算。
|
|
1588
2480
|
* run 累计 promptTokens 会重复计算每一步都重发的历史,不能用于窗口占用率。
|
|
@@ -1600,15 +2492,17 @@ declare class AgentSession {
|
|
|
1600
2492
|
compact(options?: {
|
|
1601
2493
|
signal?: AbortSignal;
|
|
1602
2494
|
}): AsyncGenerator<SessionEvent>;
|
|
1603
|
-
/**
|
|
2495
|
+
/** 手动压缩按主任务协议投影旧历史,不复用旧 run 私有状态;原始历史与落盘内容保持不变。 */
|
|
1604
2496
|
private compactOperation;
|
|
1605
|
-
/**
|
|
2497
|
+
/** 压缩与预算超限事件写入操作日志并携带 runId;其他事件不落日志。 */
|
|
1606
2498
|
private logContextEvent;
|
|
1607
2499
|
/** 以模型 attempt 身份幂等累加真实消耗;非法或缺失 usage 保持未知。 */
|
|
1608
2500
|
private accountUsage;
|
|
1609
2501
|
private persistToolMessages;
|
|
2502
|
+
/** 中止时为缓冲 assistant 的每个 tool_use 生成 OUTCOME_UNKNOWN 结果并入历史,保持事务闭合。 */
|
|
1610
2503
|
private closeInterruptedToolCalls;
|
|
2504
|
+
/** 收集历史中紧跟该 assistant 的连续 tool 消息;仅当覆盖全部 tool_use 时视为已闭合。 */
|
|
1611
2505
|
private findClosedToolMessages;
|
|
1612
2506
|
}
|
|
1613
2507
|
|
|
1614
|
-
export { AbortError, AgentLoop, type AgentLoopOptions, type AgentPlugin, AgentSession, type ApprovalChannel, type ApprovalChoice, type ApprovalRequest, type CanonicalMessage, type Capability, type CommandResult, type ConflictPolicy, type ContentBlock, type ContextBudget, ContextBudgetError, ContextManager, type ContextManagerOptions, ContextOverflowError, type ContextSnapshot, ContextStorageError, type ContextSummary, type ContextUsage, type ContextWindowValue, DEFAULT_CONTEXT_WINDOW_TOKENS, type EventLogger, type ExecutorOptions, FatalError, type FileDetails, FileLogSink, type FileLogSinkOptions, type FileObservation, type HistoryRepair, type HookContext, type HookHandlers, type HookPoint, HookRegistry, type InstructionLoadOptions, type InstructionSnapshot, type InstructionSource, type InteractionBoundary, type InteractionTerminal, JSONLMessageStore, KapibalaError, type LogChannel, type LogEvent, type LogField, type LogInput, type LogLevel, type LogSink, type ManagedSession, type MessageRole, type MessageStore, ModelError, type ModelErrorCategory, type ModelErrorInfo, type ModelErrorOptions, type ModelEvent, type ModelProfile, type ModelProvider, type ModelRequest, type ModelRole, type ModelRouter, type NormalizedHistory, OpenAICompatibleProvider, type OpenAIProviderOptions, PathSandbox, type PermissionAction, type PermissionDecision, PermissionPolicy, type PermissionRequest, type PermissionRule, type PluginContext, type ProjectIdentity, PromptAssembler, type PromptAssemblerOptions, type RedactedThinkingBlock, type RequestPreparation, type ResolvedContextWindow, type RunCommandInput, type RunMetrics, SESSION_SCHEMA_VERSION, SUMMARY_OUTPUT_BUFFER_MULTIPLIER, type SandboxConfig, SessionApprovalCache, SessionBusyError, type SessionConfig, type SessionEvent, type SessionHeader, SessionLock, SessionManager, type SessionManagerOptions, type SessionMetadata, type SessionMode, type SessionRecord, type SessionRecordType, type SessionState, type SessionStats, SessionStore, type ShellDetectOptions, type ShellEnvironment, type ShellKind, type ShellPreference, type ShellScope, SimpleModelRouter, type StatefulMessageStore, type StepLogEntry, type StepLogStage, StructuredLogger, type SummaryCandidate, SummaryService, type SummaryServiceOptions, type TextBlock, type ThinkingBlock, type TokenEstimate, type TokenEstimator, type Tool, type ToolContext, type ToolDecision, type ToolDefinition, ToolError, ToolExecutor, type ToolMetadata, ToolNotFound, ToolRegistry, type ToolResultBlock, type ToolUseBlock, TransportError, type TurnMetrics, UnicodeTokenEstimator, type Usage, builtinTools, classifyBashOutput, cleanupCommandResults, commandAuditSummary, createContextBudget, createDefaultLogSinks, createLogId, createMessageId, createRunCommandTool, defineTool, describeModelError, detectShell, editFileTool, executionFingerprint, extractFileDetails, globTool, grepTool, identifyHistory, indexInteractions, isTextBlock, isThinkingBlock, isToolResultBlock, isToolUseBlock, loadInstructions, normalizeHistory, projectSafeFields, readFileTool, recoverIncompleteAudit, renderFileDetails, requestFingerprint, resolveContextWindow, resolveSessionProject, sanitizeModelErrorMessage, sanitizeSessionTitle, stableJsonStringify, stableSerialize, supportsSessionState, toToolDefinition, truncateToolOutput, validatePermissionRules, validateSummary, visibleTools, writeFileTool };
|
|
2508
|
+
export { AbortError, AgentLoop, type AgentLoopOptions, type AgentPlugin, AgentSession, AnthropicProvider, type AnthropicProviderOptions, type ApprovalChannel, type ApprovalChoice, type ApprovalRequest, type CanonicalMessage, type Capability, type ChatCapabilities, type CommandResult, type ConflictPolicy, type ContentBlock, type ContextBudget, ContextBudgetError, ContextManager, type ContextManagerOptions, ContextOverflowError, type ContextSnapshot, ContextStorageError, type ContextSummary, type ContextUsage, type ContextWindowValue, DEFAULT_CONTEXT_WINDOW_TOKENS, type EventLogger, type ExecutorOptions, FatalError, type FileDetails, FileLogSink, type FileLogSinkOptions, type FileObservation, type HistoryRepair, type HookContext, type HookHandlers, type HookPoint, HookRegistry, type InstructionLoadOptions, type InstructionSnapshot, type InstructionSource, type InteractionBoundary, type InteractionTerminal, JSONLMessageStore, KapibalaError, type LogChannel, type LogEvent, type LogField, type LogInput, type LogLevel, type LogSink, type ManagedSession, type MessageRole, type MessageStore, ModelError, type ModelErrorCategory, type ModelErrorInfo, type ModelErrorOptions, type ModelEvent, type ModelProfile, type ModelProtocol, type ModelProvider, type ModelProviderBinding, type ModelRequest, type ModelRole, type ModelRouter, type NormalizedHistory, OpenAICompatibleProvider, type OpenAIProviderOptions, OpenAIResponsesProvider, type OpenAIResponsesProviderOptions, PathSandbox, type PermissionAction, type PermissionDecision, PermissionPolicy, type PermissionRequest, type PermissionRule, type PluginContext, type PreparedSkill, type PrimaryModelRole, type ProjectIdentity, PromptAssembler, type PromptAssemblerOptions, type ProtocolItemMetadata, type ProtocolOrigin, type ProviderStateBlock, type RedactedThinkingBlock, type RequestPreparation, type ResolvedContextWindow, type RunCommandInput, type RunMetrics, SESSION_SCHEMA_VERSION, SKILL_LIMITS, SUMMARY_OUTPUT_BUFFER_MULTIPLIER, type SandboxConfig, SessionApprovalCache, SessionBusyError, type SessionConfig, type SessionEvent, type SessionHeader, SessionLock, SessionManager, type SessionManagerOptions, type SessionMetadata, type SessionMode, type SessionRecord, type SessionRecordType, type SessionState, type SessionStats, SessionStore, type ShellDetectOptions, type ShellEnvironment, type ShellKind, type ShellPreference, type ShellScope, SimpleModelRouter, type SkillActivation, type SkillCatalog, type SkillContentSource, type SkillContextState, type SkillDefinition, type SkillDiagnostic, type SkillDocument, type SkillMetadata, type SkillOptions, type SkillQuery, type SkillQueryPage, SkillRegistry, type SkillResourceInput, type SkillResourcePage, type SkillRoot, SkillRuntime, type SkillScanResult, type SkillScope, type StatefulMessageStore, type StepLogEntry, type StepLogStage, StructuredLogger, type SummaryCandidate, SummaryService, type SummaryServiceOptions, type TextBlock, type ThinkingBlock, type TokenEstimate, type TokenEstimator, type Tool, type ToolContext, type ToolDecision, type ToolDefinition, ToolError, ToolExecutor, type ToolHardDenial, type ToolMetadata, ToolNotFound, ToolRegistry, type ToolResultBlock, type ToolUseBlock, TransportError, type TurnMetrics, UnicodeTokenEstimator, type Usage, assembleCanonicalToolResults, builtinTools, classifyBashOutput, cleanupCommandResults, commandAuditSummary, compareSkillPaths, createContextBudget, createDefaultLogSinks, createLogId, createMessageId, createRunCommandTool, defineTool, describeModelError, detectShell, editFileTool, escapeSkillXml, estimateSkillTokens, executionFingerprint, extractFileDetails, findSkillContent, formatSkillCatalog, globTool, grepTool, identifyHistory, indexInteractions, inspectSkillContent, intersectSkillTools, isSkillName, isTextBlock, isThinkingBlock, isToolResultBlock, isToolUseBlock, isWithinSkillRoot, listSkillResources, loadInstructions, normalizeHistory, normalizeSkillTools, parseSkillDocument, projectSafeFields, readBoundedSkillText, readFileTool, readSkillResource, recoverIncompleteAudit, renderFileDetails, renderSkillReloads, requestFingerprint, resolveContextWindow, resolveSessionProject, sanitizeModelErrorMessage, sanitizeSessionTitle, scanSkillRoot, skillDigest, skillFailure, skillPathKey, stableJsonStringify, stableSerialize, supportsSessionState, toToolDefinition, truncateToolOutput, validateModelBinding, validatePermissionRules, validateSummary, visibleTools, writeFileTool };
|