@linnlabs/linnkit 0.8.0
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/CHANGELOG.md +84 -0
- package/LICENSE +21 -0
- package/README.md +178 -0
- package/README.zh-CN.md +182 -0
- package/dist/agent-invocation-BHcNfrBV.d.cts +30 -0
- package/dist/agent-invocation-BznDaXDs.d.ts +30 -0
- package/dist/agentEvents-DEB7Fy_J.d.cts +81 -0
- package/dist/agentEvents-DEB7Fy_J.d.ts +81 -0
- package/dist/agentSpec-EkmviZjy.d.cts +2621 -0
- package/dist/agentSpec-EkmviZjy.d.ts +2621 -0
- package/dist/ai-engine.types-BpeU_XQG.d.cts +158 -0
- package/dist/ai-engine.types-vZRnQcJa.d.ts +158 -0
- package/dist/audit-BaRUGaqv.d.cts +307 -0
- package/dist/audit-BaRUGaqv.d.ts +307 -0
- package/dist/audit-CtcfART1.d.ts +33 -0
- package/dist/audit-LeOrm2hX.d.cts +33 -0
- package/dist/checkpointMarker-DAI3wUQu.d.cts +8 -0
- package/dist/checkpointMarker-DAI3wUQu.d.ts +8 -0
- package/dist/cli.cjs +8028 -0
- package/dist/cli.cjs.map +1 -0
- package/dist/cli.d.cts +4 -0
- package/dist/cli.d.ts +4 -0
- package/dist/cli.js +8025 -0
- package/dist/cli.js.map +1 -0
- package/dist/context-manager.cjs +8704 -0
- package/dist/context-manager.cjs.map +1 -0
- package/dist/context-manager.d.cts +2190 -0
- package/dist/context-manager.d.ts +2190 -0
- package/dist/context-manager.js +8650 -0
- package/dist/context-manager.js.map +1 -0
- package/dist/context-trace-DRi5M4lX.d.ts +239 -0
- package/dist/context-trace-HE2qY5Q-.d.cts +239 -0
- package/dist/contracts.cjs +1333 -0
- package/dist/contracts.cjs.map +1 -0
- package/dist/contracts.d.cts +8 -0
- package/dist/contracts.d.ts +8 -0
- package/dist/contracts.js +1214 -0
- package/dist/contracts.js.map +1 -0
- package/dist/defaultGraphExecutor-BBswR8wn.d.ts +624 -0
- package/dist/defaultGraphExecutor-BIjJj7WF.d.cts +624 -0
- package/dist/execution-CAIypb41.d.cts +129 -0
- package/dist/execution-CAIypb41.d.ts +129 -0
- package/dist/index-CHqwkvGp.d.ts +149 -0
- package/dist/index-CJeWHopy.d.ts +584 -0
- package/dist/index-Cm-JbzTH.d.cts +1450 -0
- package/dist/index-Cvr23YCl.d.cts +23 -0
- package/dist/index-DDzuSb0n.d.ts +23 -0
- package/dist/index-DO4dQgf2.d.cts +584 -0
- package/dist/index-DRBWi1fy.d.ts +1450 -0
- package/dist/index-Dl5PLgAv.d.cts +149 -0
- package/dist/index.cjs +9577 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +89 -0
- package/dist/index.d.ts +89 -0
- package/dist/index.js +9563 -0
- package/dist/index.js.map +1 -0
- package/dist/messages-XthmnHZ3.d.cts +8007 -0
- package/dist/messages-XthmnHZ3.d.ts +8007 -0
- package/dist/ports-DaatKJXp.d.cts +90 -0
- package/dist/ports-DnLuKfpE.d.ts +90 -0
- package/dist/ports.cjs +4 -0
- package/dist/ports.cjs.map +1 -0
- package/dist/ports.d.cts +7 -0
- package/dist/ports.d.ts +7 -0
- package/dist/ports.js +3 -0
- package/dist/ports.js.map +1 -0
- package/dist/quickstart.cjs +7697 -0
- package/dist/quickstart.cjs.map +1 -0
- package/dist/quickstart.d.cts +24 -0
- package/dist/quickstart.d.ts +24 -0
- package/dist/quickstart.js +7691 -0
- package/dist/quickstart.js.map +1 -0
- package/dist/runAgent-CPj_9e58.d.ts +88 -0
- package/dist/runAgent-HYKlXbVr.d.cts +88 -0
- package/dist/runHandle-CyXvzgzk.d.ts +239 -0
- package/dist/runHandle-D3gPsD7B.d.cts +239 -0
- package/dist/runtime-kernel/events.cjs +1485 -0
- package/dist/runtime-kernel/events.cjs.map +1 -0
- package/dist/runtime-kernel/events.d.cts +8 -0
- package/dist/runtime-kernel/events.d.ts +8 -0
- package/dist/runtime-kernel/events.js +1475 -0
- package/dist/runtime-kernel/events.js.map +1 -0
- package/dist/runtime-kernel.cjs +8656 -0
- package/dist/runtime-kernel.cjs.map +1 -0
- package/dist/runtime-kernel.d.cts +19 -0
- package/dist/runtime-kernel.d.ts +19 -0
- package/dist/runtime-kernel.js +8568 -0
- package/dist/runtime-kernel.js.map +1 -0
- package/dist/sse-vPyrOPa0.d.cts +1687 -0
- package/dist/sse-vPyrOPa0.d.ts +1687 -0
- package/dist/testkit.cjs +10613 -0
- package/dist/testkit.cjs.map +1 -0
- package/dist/testkit.d.cts +284 -0
- package/dist/testkit.d.ts +284 -0
- package/dist/testkit.js +10593 -0
- package/dist/testkit.js.map +1 -0
- package/dist/todo-B1PmDlp3.d.cts +2253 -0
- package/dist/todo-B1PmDlp3.d.ts +2253 -0
- package/dist/tokenizer-DFL4I7-I.d.ts +28 -0
- package/dist/tokenizer-DH_JXv-H.d.cts +28 -0
- package/dist/toolContracts-Blll0241.d.ts +463 -0
- package/dist/toolContracts-CLkQmhTG.d.cts +463 -0
- package/docs/README.md +76 -0
- package/docs/integration/01-installation.md +94 -0
- package/docs/integration/02-quickstart.md +104 -0
- package/docs/integration/README.md +223 -0
- package/docs/integration/agent-registration-guide.md +330 -0
- package/docs/integration/audit.md +64 -0
- package/docs/integration/child-runs.md +87 -0
- package/docs/integration/constraints-and-pitfalls.md +87 -0
- package/docs/integration/context-engineering.md +650 -0
- package/docs/integration/context-fences.md +289 -0
- package/docs/integration/glossary.md +69 -0
- package/docs/integration/llm-provider.md +76 -0
- package/docs/integration/persistence.md +44 -0
- package/docs/integration/realtime.md +76 -0
- package/docs/integration/run-supervisor.md +69 -0
- package/docs/integration/telemetry.md +48 -0
- package/docs/integration/testing.md +95 -0
- package/docs/integration/tool-development-guide.md +362 -0
- package/docs/integration/tool-history.md +202 -0
- package/docs/integration/tools.md +188 -0
- package/package.json +115 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { a as LlmRequestMessage } from './ai-engine.types-vZRnQcJa.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* TokenizerPort · host 可替换的 token 估算协议。
|
|
5
|
+
*
|
|
6
|
+
* 中文备注:
|
|
7
|
+
* - tokenizer 是“计算 token 的方法”的总称;
|
|
8
|
+
* - linnkit 默认实现和 host 自定义实现都实现这个接口;
|
|
9
|
+
* - 该 port 只服务上下文预算决策,不用于计费;
|
|
10
|
+
* - 计费 token 应以 provider 返回的 usage 为准,由 host 自己消费。
|
|
11
|
+
*/
|
|
12
|
+
interface TokenizerPort {
|
|
13
|
+
/**
|
|
14
|
+
* 估算纯文本 token 数。
|
|
15
|
+
*
|
|
16
|
+
* modelId 可用于 host 在多模型场景下选择不同 tokenizer。
|
|
17
|
+
*/
|
|
18
|
+
estimateText(text: string, modelId?: string): number;
|
|
19
|
+
/**
|
|
20
|
+
* 估算一条 LLM message 的 token 数。
|
|
21
|
+
*
|
|
22
|
+
* 自定义实现应包含 message overhead、tool_call overhead 和 tool_call_id 开销;
|
|
23
|
+
* 否则上下文预算会系统性低估。
|
|
24
|
+
*/
|
|
25
|
+
estimateMessage(message: LlmRequestMessage, modelId?: string): number;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export type { TokenizerPort as T };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { a as LlmRequestMessage } from './ai-engine.types-BpeU_XQG.cjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* TokenizerPort · host 可替换的 token 估算协议。
|
|
5
|
+
*
|
|
6
|
+
* 中文备注:
|
|
7
|
+
* - tokenizer 是“计算 token 的方法”的总称;
|
|
8
|
+
* - linnkit 默认实现和 host 自定义实现都实现这个接口;
|
|
9
|
+
* - 该 port 只服务上下文预算决策,不用于计费;
|
|
10
|
+
* - 计费 token 应以 provider 返回的 usage 为准,由 host 自己消费。
|
|
11
|
+
*/
|
|
12
|
+
interface TokenizerPort {
|
|
13
|
+
/**
|
|
14
|
+
* 估算纯文本 token 数。
|
|
15
|
+
*
|
|
16
|
+
* modelId 可用于 host 在多模型场景下选择不同 tokenizer。
|
|
17
|
+
*/
|
|
18
|
+
estimateText(text: string, modelId?: string): number;
|
|
19
|
+
/**
|
|
20
|
+
* 估算一条 LLM message 的 token 数。
|
|
21
|
+
*
|
|
22
|
+
* 自定义实现应包含 message overhead、tool_call overhead 和 tool_call_id 开销;
|
|
23
|
+
* 否则上下文预算会系统性低估。
|
|
24
|
+
*/
|
|
25
|
+
estimateMessage(message: LlmRequestMessage, modelId?: string): number;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export type { TokenizerPort as T };
|
|
@@ -0,0 +1,463 @@
|
|
|
1
|
+
import { e as RuntimeEvent, g as SubRunTraceEvent, a as AgentTodoSnapshot } from './todo-B1PmDlp3.js';
|
|
2
|
+
|
|
3
|
+
interface ToolContextConversationView {
|
|
4
|
+
/**
|
|
5
|
+
* 当前工具执行可见的 working history
|
|
6
|
+
*
|
|
7
|
+
* 中文备注:
|
|
8
|
+
* - 该视图允许工具读取“本轮 run 中已产生、但可能尚未持久化”的最新事实事件;
|
|
9
|
+
* - ToolNode / graph runtime 后续只应更新这一层背后的 source,而不是覆写顶层 getter。
|
|
10
|
+
*/
|
|
11
|
+
getWorkingHistoryEvents(): ReadonlyArray<RuntimeEvent>;
|
|
12
|
+
/**
|
|
13
|
+
* run 启动时的 persisted history 视图
|
|
14
|
+
*
|
|
15
|
+
* 中文备注:
|
|
16
|
+
* - 该视图对应 EventStore 回放得到的稳定历史;
|
|
17
|
+
* - 与 working history 显式区分,避免调用方误把“最新运行态”当成“已持久化事实”。
|
|
18
|
+
*/
|
|
19
|
+
getPersistedHistoryEvents(): ReadonlyArray<RuntimeEvent>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* 子 run trace 的“业务无关”发布载荷。
|
|
24
|
+
*
|
|
25
|
+
* 说明:
|
|
26
|
+
* - 该结构刻意只包含 subrun_trace 的 payload 字段(不包含 conversation_id/turn_id 等基础字段)
|
|
27
|
+
* - parent_tool_call_id/subrun_id 是发布器构造时绑定的,不需要每次 publish 重复提供
|
|
28
|
+
*/
|
|
29
|
+
interface SubRunTraceEnvelope {
|
|
30
|
+
/** 分片类型 */
|
|
31
|
+
kind: SubRunTraceEvent['kind'];
|
|
32
|
+
/** 增量文本(kind=*_delta 时使用) */
|
|
33
|
+
delta?: string;
|
|
34
|
+
/** 完整文本(kind=*_complete/final_answer 时使用) */
|
|
35
|
+
content?: string;
|
|
36
|
+
/** 关联工具名(kind=tool_call_decision/tool_process/tool_output 时使用) */
|
|
37
|
+
tool_name?: string;
|
|
38
|
+
/** 关联工具调用 ID(子 run 内部的 tool_call_id,用于展示步骤) */
|
|
39
|
+
tool_call_id?: string;
|
|
40
|
+
/** 工具调用阶段(kind=tool_call_decision/tool_process 时使用) */
|
|
41
|
+
phase?: SubRunTraceEvent['phase'];
|
|
42
|
+
/** 工具调用状态(kind=tool_call_decision/tool_process/tool_output 时使用) */
|
|
43
|
+
status?: SubRunTraceEvent['status'];
|
|
44
|
+
/** 工具参数(结构不做强约束,避免与业务强耦合) */
|
|
45
|
+
args?: unknown;
|
|
46
|
+
/** 工具输出(结构不做强约束,避免与业务强耦合) */
|
|
47
|
+
output?: unknown;
|
|
48
|
+
/** 执行耗时(毫秒) */
|
|
49
|
+
duration_ms?: number;
|
|
50
|
+
/** 可选调试/渲染元信息 */
|
|
51
|
+
meta?: Record<string, unknown>;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* 子 run trace 发布器接口(面向业务工具层)。
|
|
55
|
+
*
|
|
56
|
+
* 约束:
|
|
57
|
+
* - 发布器必须确保生成的 RuntimeEvent 为 `type='subrun_trace'` 且 `ephemeral=true`
|
|
58
|
+
* - 发布器应保证每条事件的 id 唯一(用于前端 processedEvents 幂等去重)
|
|
59
|
+
*/
|
|
60
|
+
interface SubRunTracePublisher {
|
|
61
|
+
publish(envelope: SubRunTraceEnvelope): void;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* @file src/agent/runtime-kernel/run-context/types.ts
|
|
66
|
+
* @description 统一运行上下文定义
|
|
67
|
+
*
|
|
68
|
+
* 核心目标:
|
|
69
|
+
* - 统一管理跨 Run(Graph执行)的链路追踪信息
|
|
70
|
+
* - 支持父子任务(SubAgent)与审阅等长程业务的上下文贯穿
|
|
71
|
+
* - 替代散落在各处的 review_run_id 等专用字段
|
|
72
|
+
*/
|
|
73
|
+
interface RunContext {
|
|
74
|
+
/**
|
|
75
|
+
* 当前 Run 的唯一标识(对应一次 Graph 执行 / 一次 conversation turn)
|
|
76
|
+
* - 通常对应 `turnId` 或 `messageId`
|
|
77
|
+
*/
|
|
78
|
+
runId: string;
|
|
79
|
+
/**
|
|
80
|
+
* 链路追踪 ID(贯穿业务全流程)
|
|
81
|
+
* - Review场景:对应 review_run_id(一次审阅包含多次 AI 调用)
|
|
82
|
+
* - 简单对话场景:可与 conversationId 或 runId 相同
|
|
83
|
+
*/
|
|
84
|
+
traceId: string;
|
|
85
|
+
/**
|
|
86
|
+
* 父级 Run ID(用于子 Agent / 嵌套图)
|
|
87
|
+
* - 如果是顶层任务,则为 undefined
|
|
88
|
+
*/
|
|
89
|
+
parentId?: string;
|
|
90
|
+
/**
|
|
91
|
+
* 根 Run ID(业务发起的原点)
|
|
92
|
+
*/
|
|
93
|
+
rootRunId?: string;
|
|
94
|
+
/**
|
|
95
|
+
* 业务标签/元数据(业务特定的上下文挂载点)
|
|
96
|
+
* - 例如:{ agentId: 'logicCheck', chunkIndex: 0 }
|
|
97
|
+
* - 替代原先散落在 ToolContext 顶层的 loose fields
|
|
98
|
+
*/
|
|
99
|
+
tags: Record<string, string | number | boolean | undefined>;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* 创建默认的 RunContext
|
|
103
|
+
*/
|
|
104
|
+
declare function createDefaultRunContext(overrides?: Partial<RunContext>): RunContext;
|
|
105
|
+
|
|
106
|
+
interface ToolContextCompatibilityFields {
|
|
107
|
+
/**
|
|
108
|
+
* 兼容字段:原始用户请求
|
|
109
|
+
*
|
|
110
|
+
* 中文备注:
|
|
111
|
+
* - 当前由 host context injection 写入;
|
|
112
|
+
* - 仍有少量 deep_search 调用方在读取;
|
|
113
|
+
* - 后续应继续收窄,不再扩散新的读取点。
|
|
114
|
+
*/
|
|
115
|
+
user_query?: string;
|
|
116
|
+
/**
|
|
117
|
+
* 兼容字段:当前 run 选定模型 ID
|
|
118
|
+
*
|
|
119
|
+
* 中文备注:
|
|
120
|
+
* - 当前主要给 child-run 调用方继承父模型选择;
|
|
121
|
+
* - 后续应继续收窄到显式 child-run execution policy。
|
|
122
|
+
*/
|
|
123
|
+
modelId?: string;
|
|
124
|
+
/**
|
|
125
|
+
* 兼容字段:当前 runContext
|
|
126
|
+
*
|
|
127
|
+
* 中文备注:
|
|
128
|
+
* - 当前主要由 host 注入;尚未在工具侧广泛直接读取;
|
|
129
|
+
* - 保留显式字段是为了停止继续依赖 string-index loose access。
|
|
130
|
+
*/
|
|
131
|
+
run_context?: RunContext;
|
|
132
|
+
}
|
|
133
|
+
type CompatibilityCarrier = ToolContextCompatibilityFields | Record<string, unknown> | undefined;
|
|
134
|
+
declare function readToolContextUserQuery(context: CompatibilityCarrier): string | undefined;
|
|
135
|
+
declare function readToolContextModelId(context: CompatibilityCarrier): string | undefined;
|
|
136
|
+
declare function readToolContextRunContext(context: CompatibilityCarrier): RunContext | undefined;
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* child-run 父上下文的 runtime-owned 最小合同
|
|
140
|
+
*
|
|
141
|
+
* 中文备注:
|
|
142
|
+
* - 这里只声明 child-run 协议真正需要的 runtime capability / compatibility fields;
|
|
143
|
+
* - 不显式依赖完整 ToolContext,避免 child-run 主链编译期吃进整套 host/product 服务类型;
|
|
144
|
+
* - 仍保留 index signature,用于在创建 child ToolContext 时透传宿主/产品层 patch。
|
|
145
|
+
*/
|
|
146
|
+
interface ChildRunParentContext extends ToolExecutionContext, ToolContextCompatibilityFields {
|
|
147
|
+
deepSearchDepth?: number;
|
|
148
|
+
[key: string]: unknown;
|
|
149
|
+
}
|
|
150
|
+
interface ChildRunHistoryPolicy {
|
|
151
|
+
inheritTurns: number;
|
|
152
|
+
eventFilter?: (event: RuntimeEvent) => boolean;
|
|
153
|
+
}
|
|
154
|
+
interface ChildRunTracePolicy {
|
|
155
|
+
parentToolCallId?: string;
|
|
156
|
+
subrunId?: string;
|
|
157
|
+
source?: string;
|
|
158
|
+
metadata?: Record<string, unknown>;
|
|
159
|
+
persistForReplay?: boolean;
|
|
160
|
+
}
|
|
161
|
+
interface ChildRunExecutionPolicy {
|
|
162
|
+
/**
|
|
163
|
+
* 显式 child-run runId。默认由 host adapter 取 subrunId。
|
|
164
|
+
*/
|
|
165
|
+
runId?: string;
|
|
166
|
+
/**
|
|
167
|
+
* 父 runId。默认从 parentToolContext.runId 继承。
|
|
168
|
+
*/
|
|
169
|
+
parentRunId?: string;
|
|
170
|
+
maxSteps?: number;
|
|
171
|
+
modelId?: string;
|
|
172
|
+
abortSignal?: AbortSignal;
|
|
173
|
+
}
|
|
174
|
+
interface ChildRunRequest<TParentToolContext = ChildRunParentContext> {
|
|
175
|
+
userMessage: string;
|
|
176
|
+
parentToolContext: TParentToolContext;
|
|
177
|
+
historyPolicy?: ChildRunHistoryPolicy;
|
|
178
|
+
tracePolicy?: ChildRunTracePolicy;
|
|
179
|
+
executionPolicy?: ChildRunExecutionPolicy;
|
|
180
|
+
}
|
|
181
|
+
interface ChildRunResult {
|
|
182
|
+
runId?: string;
|
|
183
|
+
parentRunId?: string;
|
|
184
|
+
subrunId: string;
|
|
185
|
+
success: boolean;
|
|
186
|
+
finalAnswer: string;
|
|
187
|
+
events: RuntimeEvent[];
|
|
188
|
+
stepCount: number;
|
|
189
|
+
error?: string;
|
|
190
|
+
judgeToolOutput?: string;
|
|
191
|
+
}
|
|
192
|
+
interface ChildRunInvokerPort<TRequest extends ChildRunRequest = ChildRunRequest, TResult extends ChildRunResult = ChildRunResult> {
|
|
193
|
+
invoke(params: TRequest): Promise<TResult>;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* runtime-owned 工具执行上下文最小合同
|
|
198
|
+
*
|
|
199
|
+
* 中文备注:
|
|
200
|
+
* - 这里只保留 graph-engine / tool runtime / child-run 真正需要解释执行的字段;
|
|
201
|
+
* - 不包含 KnowledgeBase / Workspace / DB 等宿主服务类型;
|
|
202
|
+
* - 不包含 deepSearch / research / workspaceProject 等产品语义字段。
|
|
203
|
+
*/
|
|
204
|
+
interface ToolExecutionContext {
|
|
205
|
+
/**
|
|
206
|
+
* 当前对话 run 的唯一标识(由 FlowOrchestrator 注入)
|
|
207
|
+
* 说明:Workspace 工具 vNext 协议已不再依赖 index 快照,但 runId 仍可用于其他链路的追踪/调试。
|
|
208
|
+
*/
|
|
209
|
+
runId?: string;
|
|
210
|
+
/**
|
|
211
|
+
* 父级 run 标识。
|
|
212
|
+
*
|
|
213
|
+
* 中文备注:
|
|
214
|
+
* - 顶层 run 通常为空;
|
|
215
|
+
* - 同步 child-run 会把自己的 `runId` 设为子 run / subrun ID,同时把父 run 写到这里;
|
|
216
|
+
* - Telemetry / Audit / Cost 聚合依赖这个字段建立父子关系,但 graph-engine 不解释产品语义。
|
|
217
|
+
*/
|
|
218
|
+
parentRunId?: string;
|
|
219
|
+
/**
|
|
220
|
+
* 🔥 引用编号偏移量(用于多次工具调用时的连续编号)
|
|
221
|
+
*
|
|
222
|
+
* 工作原理:
|
|
223
|
+
* - 每次调用知识库工具时,由 ToolNode 传入当前轮次已使用的引用数量
|
|
224
|
+
* - 工具在格式化 observation 和构建 citations 时使用此偏移量
|
|
225
|
+
* - 确保 AI 看到的编号和引用元数据的编号都是连续且一致的
|
|
226
|
+
*
|
|
227
|
+
* @default 0 - 表示从 1 开始编号
|
|
228
|
+
*/
|
|
229
|
+
citationOffset?: number;
|
|
230
|
+
/**
|
|
231
|
+
* 🔥 取消信号(贯穿一次对话请求)
|
|
232
|
+
*
|
|
233
|
+
* 约定:
|
|
234
|
+
* - 工具实现必须在合适的边界检查 `abortSignal.aborted` 并尽快退出;
|
|
235
|
+
* - 该字段属于“执行期上下文”,不应被写入持久化事件。
|
|
236
|
+
*/
|
|
237
|
+
abortSignal?: AbortSignal;
|
|
238
|
+
/**
|
|
239
|
+
* 🔥 当前这次工具调用的 tool_call_id(由 ToolNode 在执行前注入)
|
|
240
|
+
*
|
|
241
|
+
* 说明:
|
|
242
|
+
* - tool_call_id 属于“事件层/执行层”的稳定关联键;
|
|
243
|
+
* - 工具内部如果需要发布 subrun_trace(子 run 过程),必须使用该字段作为 parent_tool_call_id。
|
|
244
|
+
* - 该字段仅在工具执行期间有效,不应被工具写入持久化历史。
|
|
245
|
+
*/
|
|
246
|
+
parentToolCallId?: string;
|
|
247
|
+
/**
|
|
248
|
+
* 🔥 当前对话 ID(由 GraphExecutor/ToolNode 注入,用于工具内部做链路追踪)
|
|
249
|
+
*/
|
|
250
|
+
conversationId?: string;
|
|
251
|
+
/**
|
|
252
|
+
* 🔥 当前轮次 ID(由 GraphExecutor/ToolNode 注入,用于工具内部做链路追踪)
|
|
253
|
+
*/
|
|
254
|
+
turnId?: string;
|
|
255
|
+
/**
|
|
256
|
+
* 🔥 Agent ToDo(working memory)快照(对话级)
|
|
257
|
+
*
|
|
258
|
+
* 设计说明:
|
|
259
|
+
* - ToDo 的权威状态应落到 RuntimeEvent(todo_updated)以便截断/回放一致;
|
|
260
|
+
* - 但工具执行期间需要“读到本轮最新状态”(尚未落库),因此把最新快照同时缓存到 ToolContext。
|
|
261
|
+
* - AgentRunnerService 会在每次 run 开始时用历史事件回放的最后一条 todo_updated 为其初始化。
|
|
262
|
+
*/
|
|
263
|
+
agentTodo?: AgentTodoSnapshot;
|
|
264
|
+
/**
|
|
265
|
+
* 🔥 显式会话视图 capability
|
|
266
|
+
*
|
|
267
|
+
* 中文备注:
|
|
268
|
+
* - `conversationView` 是后续统一承载 working/persisted history 的稳定协议面;
|
|
269
|
+
* - 迁移期仍保留 `getConversationHistoryEvents()` 兼容旧工具。
|
|
270
|
+
*/
|
|
271
|
+
conversationView?: ToolContextConversationView;
|
|
272
|
+
/**
|
|
273
|
+
* 🔥 获取父会话的历史事件(兼容 getter)
|
|
274
|
+
*
|
|
275
|
+
* 约束:
|
|
276
|
+
* - 返回值必须被视为只读:工具不得修改数组内容;
|
|
277
|
+
* - 迁移期该 getter 语义固定为 `conversationView.getWorkingHistoryEvents()`;
|
|
278
|
+
* - 新代码优先直接使用 `conversationView`,避免继续把 working/persisted history 混为一个概念。
|
|
279
|
+
*/
|
|
280
|
+
getConversationHistoryEvents?: () => ReadonlyArray<RuntimeEvent>;
|
|
281
|
+
/**
|
|
282
|
+
* 🔥 发布 todo_updated RuntimeEvent(低耦合注入)
|
|
283
|
+
*/
|
|
284
|
+
publishAgentTodoUpdated?: (snapshot: AgentTodoSnapshot) => void;
|
|
285
|
+
/**
|
|
286
|
+
* 🔥 SubRun Trace Channel:为工具提供 publisher 工厂(低耦合)
|
|
287
|
+
*/
|
|
288
|
+
createSubRunTracePublisher?: (opts: {
|
|
289
|
+
parentToolCallId: string;
|
|
290
|
+
subrunId: string;
|
|
291
|
+
subrunParentId?: string;
|
|
292
|
+
source?: string;
|
|
293
|
+
metadata?: Record<string, unknown>;
|
|
294
|
+
persistForReplay?: boolean;
|
|
295
|
+
}) => SubRunTracePublisher;
|
|
296
|
+
/**
|
|
297
|
+
* 显式 child-run invoker 注入(迁移期优先入口)
|
|
298
|
+
*/
|
|
299
|
+
registeredChildRunInvoker?: ChildRunInvokerPort;
|
|
300
|
+
/**
|
|
301
|
+
* 与 ConversationArtifactContext 等「可扩展宿主字段」交织时的结构兼容:
|
|
302
|
+
* - 运行期 toolContext 常为普通对象,可能携带 sharedMemory / research 等附加键;
|
|
303
|
+
* - 无此索引签名时,`ToolExecutionContext` 无法赋给 `ToolExecutionContext & ConversationArtifactContext`(strict 下报错)。
|
|
304
|
+
*/
|
|
305
|
+
[key: string]: unknown;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* @file src/agent/runtime-kernel/tools/ui-types.ts
|
|
310
|
+
*
|
|
311
|
+
* @brief 定义工具在前端UI展示时所需的元数据类型
|
|
312
|
+
*
|
|
313
|
+
* @description
|
|
314
|
+
* 这个文件中的类型专门用于 "后端驱动UI (Backend-Driven UI)" 架构。
|
|
315
|
+
* 当后端Agent执行一个工具时,它会附带这些UI元数据。
|
|
316
|
+
* 前端则根据这些元数据,动态地、智能地渲染出对应的UI组件,
|
|
317
|
+
* 从而实现工具展示逻辑与前端渲染逻辑的解耦。
|
|
318
|
+
*/
|
|
319
|
+
interface ToolLayoutOptions {
|
|
320
|
+
contentPadding?: boolean;
|
|
321
|
+
showBorder?: boolean;
|
|
322
|
+
showBackground?: boolean;
|
|
323
|
+
}
|
|
324
|
+
interface ToolDisplayOptions {
|
|
325
|
+
viewType: string;
|
|
326
|
+
titleTemplate?: string;
|
|
327
|
+
icon?: string;
|
|
328
|
+
layout?: ToolLayoutOptions;
|
|
329
|
+
}
|
|
330
|
+
interface ToolControlInfo {
|
|
331
|
+
requireUser?: boolean;
|
|
332
|
+
questionnaireId?: string;
|
|
333
|
+
resumeStrategy?: 'continue';
|
|
334
|
+
terminateRun?: boolean;
|
|
335
|
+
reason?: string;
|
|
336
|
+
}
|
|
337
|
+
interface StructuredToolResult<T = Record<string, unknown>> {
|
|
338
|
+
data: T;
|
|
339
|
+
observation?: string;
|
|
340
|
+
control?: ToolControlInfo;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
type ToolIdempotencyScope = 'conversation' | 'turn';
|
|
344
|
+
type ToolIdempotencyPolicy = {
|
|
345
|
+
scope: ToolIdempotencyScope;
|
|
346
|
+
};
|
|
347
|
+
declare function computeToolIdempotencyKey(params: {
|
|
348
|
+
policy: ToolIdempotencyPolicy;
|
|
349
|
+
toolName: string;
|
|
350
|
+
args: Record<string, unknown>;
|
|
351
|
+
context: ToolExecutionContext;
|
|
352
|
+
}): string;
|
|
353
|
+
declare function findCachedToolOutputByIdempotencyKey(params: {
|
|
354
|
+
history: ReadonlyArray<RuntimeEvent>;
|
|
355
|
+
toolName: string;
|
|
356
|
+
idempotencyKey: string;
|
|
357
|
+
}): {
|
|
358
|
+
output: string;
|
|
359
|
+
} | undefined;
|
|
360
|
+
|
|
361
|
+
type ToolArgs = Record<string, unknown>;
|
|
362
|
+
type JsonObjectSchema = Record<string, unknown>;
|
|
363
|
+
interface ToolParameterProperty {
|
|
364
|
+
type: string;
|
|
365
|
+
description: string;
|
|
366
|
+
default?: unknown;
|
|
367
|
+
enum?: string[];
|
|
368
|
+
properties?: Record<string, ToolParameterProperty>;
|
|
369
|
+
items?: ToolParameterProperty;
|
|
370
|
+
required?: string[];
|
|
371
|
+
}
|
|
372
|
+
interface ToolParameterSchema {
|
|
373
|
+
type: 'object';
|
|
374
|
+
properties: Record<string, ToolParameterProperty>;
|
|
375
|
+
required?: string[];
|
|
376
|
+
additionalProperties?: boolean;
|
|
377
|
+
}
|
|
378
|
+
interface ToolResult {
|
|
379
|
+
success: boolean;
|
|
380
|
+
data: string;
|
|
381
|
+
metadata?: Record<string, unknown>;
|
|
382
|
+
}
|
|
383
|
+
type UnifiedToolResult = {
|
|
384
|
+
kind: 'ok';
|
|
385
|
+
output: unknown;
|
|
386
|
+
} | {
|
|
387
|
+
kind: 'need_user';
|
|
388
|
+
spec: unknown;
|
|
389
|
+
} | {
|
|
390
|
+
kind: 'async';
|
|
391
|
+
run_id: string;
|
|
392
|
+
};
|
|
393
|
+
declare abstract class BaseTool<TArgs extends ToolArgs = ToolArgs, TResult extends string = string> {
|
|
394
|
+
abstract readonly name: string;
|
|
395
|
+
abstract readonly description: string;
|
|
396
|
+
abstract readonly parameters: ToolParameterSchema;
|
|
397
|
+
readonly idempotency?: ToolIdempotencyPolicy;
|
|
398
|
+
readonly displayOptions?: ToolDisplayOptions;
|
|
399
|
+
getExecutionSummary?(output: string): string;
|
|
400
|
+
abstract run(args: TArgs, context: ToolExecutionContext): Promise<TResult>;
|
|
401
|
+
protected validateArguments(args: TArgs): {
|
|
402
|
+
success: boolean;
|
|
403
|
+
error?: string;
|
|
404
|
+
};
|
|
405
|
+
getMetadata(): {
|
|
406
|
+
name: string;
|
|
407
|
+
description: string;
|
|
408
|
+
parameters: ToolParameterSchema;
|
|
409
|
+
};
|
|
410
|
+
}
|
|
411
|
+
interface ToolRegistryEntry {
|
|
412
|
+
name: string;
|
|
413
|
+
toolClass: new () => BaseTool<ToolArgs, string>;
|
|
414
|
+
metadata: ReturnType<BaseTool['getMetadata']>;
|
|
415
|
+
}
|
|
416
|
+
declare const CommonParameterTypes: {
|
|
417
|
+
readonly docId: {
|
|
418
|
+
readonly type: "string";
|
|
419
|
+
readonly description: "The unique ID of the document";
|
|
420
|
+
};
|
|
421
|
+
readonly query: {
|
|
422
|
+
readonly type: "string";
|
|
423
|
+
readonly description: "The search query text";
|
|
424
|
+
};
|
|
425
|
+
readonly topK: {
|
|
426
|
+
readonly type: "integer";
|
|
427
|
+
readonly description: "The maximum number of results to return";
|
|
428
|
+
readonly default: 5;
|
|
429
|
+
};
|
|
430
|
+
readonly blockId: {
|
|
431
|
+
readonly type: "string";
|
|
432
|
+
readonly description: "The unique ID of a content block";
|
|
433
|
+
};
|
|
434
|
+
readonly pageNumber: {
|
|
435
|
+
readonly type: "integer";
|
|
436
|
+
readonly description: "Page number (1-indexed)";
|
|
437
|
+
readonly default: 1;
|
|
438
|
+
};
|
|
439
|
+
};
|
|
440
|
+
interface AgentTool<TArgs extends ToolArgs = ToolArgs, TResult = unknown> {
|
|
441
|
+
name: string;
|
|
442
|
+
description: string;
|
|
443
|
+
parameters: JsonObjectSchema;
|
|
444
|
+
execute(args: TArgs): Promise<TResult>;
|
|
445
|
+
}
|
|
446
|
+
interface OpenAIToolSchema {
|
|
447
|
+
type: 'function';
|
|
448
|
+
function: {
|
|
449
|
+
name: string;
|
|
450
|
+
description: string;
|
|
451
|
+
parameters: JsonObjectSchema;
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
interface ToolCallResult<TResult = unknown> {
|
|
455
|
+
toolName: string;
|
|
456
|
+
args: ToolArgs;
|
|
457
|
+
result: TResult;
|
|
458
|
+
success: boolean;
|
|
459
|
+
error?: string;
|
|
460
|
+
durationMs: number;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
export { type AgentTool as A, BaseTool as B, CommonParameterTypes as C, type JsonObjectSchema as J, type OpenAIToolSchema as O, type RunContext as R, type StructuredToolResult as S, type ToolExecutionContext as T, type UnifiedToolResult as U, type ToolArgs as a, type ToolParameterSchema as b, type ToolParameterProperty as c, type ToolContextConversationView as d, type ToolCallResult as e, type ToolContextCompatibilityFields as f, type ToolControlInfo as g, type ToolDisplayOptions as h, type ToolLayoutOptions as i, type ToolRegistryEntry as j, type ToolResult as k, computeToolIdempotencyKey as l, findCachedToolOutputByIdempotencyKey as m, readToolContextRunContext as n, readToolContextUserQuery as o, createDefaultRunContext as p, type ChildRunParentContext as q, readToolContextModelId as r, type SubRunTracePublisher as s, type ChildRunHistoryPolicy as t, type ChildRunInvokerPort as u, type ChildRunRequest as v, type ChildRunResult as w, type SubRunTraceEnvelope as x, type ToolIdempotencyPolicy as y };
|