@springbrand/agent-runtime 0.1.3-alpha.4 → 0.1.3-alpha.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/package.json +3 -1
- package/src/adapter/cloudflare/index.ts +60 -0
- package/src/adapter/cloudflare/resources/runtime-resources.ts +86 -0
- package/src/adapter/cloudflare/sandbox/adapter.ts +1509 -0
- package/src/adapter/cloudflare/sandbox/id.ts +23 -0
- package/src/adapter/cloudflare/sandbox/policy.ts +15 -0
- package/src/adapter/cloudflare/subagent/definition.ts +574 -0
- package/src/adapter/cloudflare/subagent/runner.ts +175 -0
- package/src/adapter/cloudflare/subagent/tools.ts +256 -0
- package/src/adapter/cloudflare/universal-agent/definition.ts +71 -0
- package/src/adapter/cloudflare/universal-agent/hooks.ts +35 -0
- package/src/adapter/cloudflare/universal-agent/preparation.ts +273 -0
- package/src/adapter/cloudflare/universal-agent/tools.ts +74 -0
- package/src/adapter/cloudflare/workspace/publisher.ts +31 -0
- package/src/adapter/cloudflare/workspace/scoped-workspace.ts +376 -0
- package/src/agent-tool-runtime.ts +152 -0
- package/src/index.ts +49 -7
- package/src/layers/orchestration/temporary-agent/core.ts +12 -1
- package/src/layers/orchestration/temporary-agent/runner.ts +1 -2
- package/src/pi/message/contract.ts +7 -0
- package/src/pi/message/conversion.ts +9 -1
- package/src/pi/runtime-adapter/assembly.ts +4 -2
- package/src/pi/runtime-adapter/index.ts +6 -2
- package/src/pi/tool/base.ts +17 -0
- package/src/pi/tool/core.ts +13 -0
- package/src/pi/tool/schedule.ts +11 -0
- package/src/pi/tool/subagent.ts +14 -0
- package/src/pi/tool/workspace-sandbox.ts +15 -0
- package/src/runtime-agent-context.ts +112 -0
- package/src/runtime-agent.ts +429 -315
- package/src/runtime-assembler.ts +249 -99
- package/src/runtime-definition.ts +173 -0
- package/src/runtime.ts +139 -12
- package/src/tool-registry.ts +143 -0
|
@@ -26,6 +26,7 @@ function attachmentText(
|
|
|
26
26
|
|
|
27
27
|
function userContent(
|
|
28
28
|
parts: ReadonlyArray<UIMessage["parts"][number]>,
|
|
29
|
+
privateContext?: string,
|
|
29
30
|
): UserMessage["content"] {
|
|
30
31
|
const content: Exclude<UserMessage["content"], string> = [];
|
|
31
32
|
for (const part of parts) {
|
|
@@ -46,16 +47,23 @@ function userContent(
|
|
|
46
47
|
}
|
|
47
48
|
}
|
|
48
49
|
if (content.length === 0) throw new Error("User message content is required");
|
|
50
|
+
if (privateContext) {
|
|
51
|
+
content.unshift({
|
|
52
|
+
type: "text",
|
|
53
|
+
text: `[Agent private context]\n${privateContext}\n[/Agent private context]`,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
49
56
|
return content;
|
|
50
57
|
}
|
|
51
58
|
|
|
52
59
|
/** Convert an official user UIMessage into canonical Pi model input. */
|
|
53
60
|
export function uiUserMessageToPi(
|
|
54
61
|
message: UIMessage & { readonly role: "user" },
|
|
62
|
+
privateContext?: string,
|
|
55
63
|
): UserMessage {
|
|
56
64
|
return {
|
|
57
65
|
role: "user",
|
|
58
|
-
content: userContent(message.parts),
|
|
66
|
+
content: userContent(message.parts, privateContext),
|
|
59
67
|
timestamp: timestampOf(message),
|
|
60
68
|
};
|
|
61
69
|
}
|
|
@@ -248,10 +248,12 @@ function describeTools(candidates: readonly PiToolCandidate[]) {
|
|
|
248
248
|
name: candidate.tool.name,
|
|
249
249
|
owner: candidate.owner,
|
|
250
250
|
label: candidate.tool.label,
|
|
251
|
-
description:
|
|
252
|
-
candidate.tool.description ?? candidate.tool.label,
|
|
251
|
+
description: candidate.tool.description ?? candidate.tool.label,
|
|
253
252
|
parameters: candidate.tool.parameters,
|
|
254
253
|
requiredExecutionLevel: candidate.requiredExecutionLevel,
|
|
254
|
+
...(candidate.alwaysRequiresApproval
|
|
255
|
+
? { alwaysRequiresApproval: true }
|
|
256
|
+
: {}),
|
|
255
257
|
...(candidate.source ? { source: candidate.source } : {}),
|
|
256
258
|
retry: piToolRetryPolicy(candidate),
|
|
257
259
|
}))
|
|
@@ -107,8 +107,11 @@ export class PiRuntimeAdapter {
|
|
|
107
107
|
* 实现理由:转换只生成 Pi UserMessage 的 content 和 timestamp,原始浏览器视图由 transcript sidecar 另行保存。
|
|
108
108
|
* 不要把 UI id、metadata 或原始 part 结构并入 canonical message。
|
|
109
109
|
*/
|
|
110
|
-
normalizeUserInput(
|
|
111
|
-
|
|
110
|
+
normalizeUserInput(
|
|
111
|
+
input: UIMessage & { role: "user" },
|
|
112
|
+
privateContext?: string,
|
|
113
|
+
) {
|
|
114
|
+
return uiUserMessageToPi(input, privateContext);
|
|
112
115
|
}
|
|
113
116
|
|
|
114
117
|
resolveApiKey(
|
|
@@ -209,6 +212,7 @@ export class PiRuntimeAdapter {
|
|
|
209
212
|
}
|
|
210
213
|
|
|
211
214
|
export type {
|
|
215
|
+
RequestedCapability,
|
|
212
216
|
UIChatRequestBody,
|
|
213
217
|
} from "../message";
|
|
214
218
|
export type { PiToolApproval } from "../turn";
|
package/src/pi/tool/base.ts
CHANGED
|
@@ -8,6 +8,10 @@ import type { RuntimeMemoryPort } from "../../kernel/bindings";
|
|
|
8
8
|
import type { RuntimeMemoryProfile } from "../../kernel/profile";
|
|
9
9
|
import { serializeOutput } from "../../lib/artifacts";
|
|
10
10
|
import type { PiToolCandidate } from "./compiler";
|
|
11
|
+
import {
|
|
12
|
+
toolRegistryFromPiCandidates,
|
|
13
|
+
type ToolRegistry,
|
|
14
|
+
} from "../../tool-registry";
|
|
11
15
|
import { webSearchPiToolCandidate } from "./web-search";
|
|
12
16
|
import type { WebSearch } from "./web-search/api";
|
|
13
17
|
|
|
@@ -271,3 +275,16 @@ export function memoryPiToolCandidate(
|
|
|
271
275
|
tool,
|
|
272
276
|
};
|
|
273
277
|
}
|
|
278
|
+
|
|
279
|
+
/** 从 Memory Port 生成 `set_context` Tool。 */
|
|
280
|
+
export function createMemoryTools(
|
|
281
|
+
memory: RuntimeMemoryPort,
|
|
282
|
+
profile: Pick<
|
|
283
|
+
RuntimeMemoryProfile,
|
|
284
|
+
"memoryTokens" | "preferencesTokens"
|
|
285
|
+
>,
|
|
286
|
+
): ToolRegistry {
|
|
287
|
+
return toolRegistryFromPiCandidates([
|
|
288
|
+
memoryPiToolCandidate(memory, profile),
|
|
289
|
+
]);
|
|
290
|
+
}
|
package/src/pi/tool/core.ts
CHANGED
|
@@ -12,6 +12,10 @@ import { serializeOutput } from "../../lib/artifacts";
|
|
|
12
12
|
import type { PiLoadedExtension } from "../assembly/extensions";
|
|
13
13
|
import { aiToolToPi } from "./ai-adapter";
|
|
14
14
|
import type { PiToolCandidate } from "./compiler";
|
|
15
|
+
import {
|
|
16
|
+
toolRegistryFromPiCandidates,
|
|
17
|
+
type ToolRegistry,
|
|
18
|
+
} from "../../tool-registry";
|
|
15
19
|
|
|
16
20
|
// 本文件沿用 `../../index.ts` 入口定义的 Extension、Port 和 Tool Candidate 术语。
|
|
17
21
|
|
|
@@ -178,4 +182,13 @@ export function codeExecutionPiToolCandidate(
|
|
|
178
182
|
};
|
|
179
183
|
}
|
|
180
184
|
|
|
185
|
+
/** 从 Codemode Runtime Port 生成 `execute` Tool。 */
|
|
186
|
+
export function createCodeExecutionTool(
|
|
187
|
+
runtime: RuntimeCodeExecutionPort,
|
|
188
|
+
): ToolRegistry {
|
|
189
|
+
return toolRegistryFromPiCandidates([
|
|
190
|
+
codeExecutionPiToolCandidate(runtime),
|
|
191
|
+
]);
|
|
192
|
+
}
|
|
193
|
+
|
|
181
194
|
// #endregion
|
package/src/pi/tool/schedule.ts
CHANGED
|
@@ -10,6 +10,10 @@ import type {
|
|
|
10
10
|
import type { ScheduleSpec } from "../../kernel/receipts";
|
|
11
11
|
import { serializeOutput } from "../../lib/artifacts";
|
|
12
12
|
import type { PiToolCandidate } from "./compiler";
|
|
13
|
+
import {
|
|
14
|
+
toolRegistryFromPiCandidates,
|
|
15
|
+
type ToolRegistry,
|
|
16
|
+
} from "../../tool-registry";
|
|
13
17
|
|
|
14
18
|
const scheduleTriggerParameters = Type.Union([
|
|
15
19
|
Type.Object({
|
|
@@ -240,3 +244,10 @@ export function schedulePiToolCandidates(
|
|
|
240
244
|
),
|
|
241
245
|
];
|
|
242
246
|
}
|
|
247
|
+
|
|
248
|
+
/** 从 {@link RuntimeSchedulePort} 生成定时任务 Tool 集。 */
|
|
249
|
+
export function createScheduleTools(
|
|
250
|
+
schedule: RuntimeSchedulePort,
|
|
251
|
+
): ToolRegistry {
|
|
252
|
+
return toolRegistryFromPiCandidates(schedulePiToolCandidates(schedule));
|
|
253
|
+
}
|
package/src/pi/tool/subagent.ts
CHANGED
|
@@ -4,6 +4,10 @@ import type { RuntimeSubagentPort } from "../../kernel/bindings";
|
|
|
4
4
|
import { AGENT_TYPES } from "../../layers/orchestration/subagents/agent-types/registry";
|
|
5
5
|
import { serializeOutput } from "../../lib/artifacts";
|
|
6
6
|
import type { PiToolCandidate } from "./compiler";
|
|
7
|
+
import {
|
|
8
|
+
toolRegistryFromPiCandidates,
|
|
9
|
+
type ToolRegistry,
|
|
10
|
+
} from "../../tool-registry";
|
|
7
11
|
|
|
8
12
|
const BACKGROUND_MAX_BUDGET_MS = 10 * 60 * 1_000;
|
|
9
13
|
|
|
@@ -146,3 +150,13 @@ export function subagentPiToolCandidates(
|
|
|
146
150
|
});
|
|
147
151
|
return candidates;
|
|
148
152
|
}
|
|
153
|
+
|
|
154
|
+
/** 从 {@link RuntimeSubagentPort} 生成已启用 SubAgent Tool 集。 */
|
|
155
|
+
export function createSubagentTools(
|
|
156
|
+
subagents: RuntimeSubagentPort | undefined,
|
|
157
|
+
enabledSubagents: readonly string[],
|
|
158
|
+
): ToolRegistry {
|
|
159
|
+
return toolRegistryFromPiCandidates(
|
|
160
|
+
subagentPiToolCandidates(subagents, enabledSubagents),
|
|
161
|
+
);
|
|
162
|
+
}
|
|
@@ -21,6 +21,10 @@ import type {
|
|
|
21
21
|
import { serializeOutput } from "../../lib/artifacts";
|
|
22
22
|
import { aiToolToPi } from "./ai-adapter";
|
|
23
23
|
import type { PiToolCandidate } from "./compiler";
|
|
24
|
+
import {
|
|
25
|
+
toolRegistryFromPiCandidates,
|
|
26
|
+
type ToolRegistry,
|
|
27
|
+
} from "../../tool-registry";
|
|
24
28
|
|
|
25
29
|
// #region Shared Pi result helpers
|
|
26
30
|
|
|
@@ -145,6 +149,7 @@ function serializeByPath<T>(
|
|
|
145
149
|
*
|
|
146
150
|
* `Pi`、`Port` 与“候选工具”等项目核心术语见 `../../index.ts`。
|
|
147
151
|
*/
|
|
152
|
+
/** Pi 编译路径:从 {@link WorkspacePort} 生成标准 workspace 文件 Tool 候选项。 */
|
|
148
153
|
export function workspacePiToolCandidates(
|
|
149
154
|
workspace: WorkspacePort,
|
|
150
155
|
): PiToolCandidate[] {
|
|
@@ -298,6 +303,11 @@ export function workspacePiToolCandidates(
|
|
|
298
303
|
];
|
|
299
304
|
}
|
|
300
305
|
|
|
306
|
+
/** 从 {@link WorkspacePort} 生成标准 workspace 文件 Tool 集(read / write / edit …)。 */
|
|
307
|
+
export function createWorkspaceTools(workspace: WorkspacePort): ToolRegistry {
|
|
308
|
+
return toolRegistryFromPiCandidates(workspacePiToolCandidates(workspace));
|
|
309
|
+
}
|
|
310
|
+
|
|
301
311
|
// #endregion
|
|
302
312
|
|
|
303
313
|
// #region Sandbox tools
|
|
@@ -495,4 +505,9 @@ export function sandboxPiToolCandidates(
|
|
|
495
505
|
];
|
|
496
506
|
}
|
|
497
507
|
|
|
508
|
+
/** 从 {@link RuntimeSandboxPort} 生成 Sandbox Tool 集。 */
|
|
509
|
+
export function createSandboxTools(sandbox: RuntimeSandboxPort): ToolRegistry {
|
|
510
|
+
return toolRegistryFromPiCandidates(sandboxPiToolCandidates(sandbox));
|
|
511
|
+
}
|
|
512
|
+
|
|
498
513
|
// #endregion
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AgentToolRunStatus,
|
|
3
|
+
ChatCapableAgentClass,
|
|
4
|
+
RunAgentToolOptions,
|
|
5
|
+
} from "agents";
|
|
6
|
+
import type { ExecutionLevel } from "./lib/execution-level";
|
|
7
|
+
import type { RuntimeConfigUpdateResult } from "./kernel/runtime-config";
|
|
8
|
+
import type {
|
|
9
|
+
TemporaryAgentRequest,
|
|
10
|
+
TemporaryAgentRunContext,
|
|
11
|
+
} from "./layers/orchestration/temporary-agent/core";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 描述 Agent 寻址路径中的一级父子节点。
|
|
15
|
+
*
|
|
16
|
+
* @remarks
|
|
17
|
+
* Cloudflare Agents SDK 用 `parentPath` 暴露根节点到直接父节点的路径。
|
|
18
|
+
*
|
|
19
|
+
* 调用方通常只读取,不应自行构造后写回 SDK。
|
|
20
|
+
*/
|
|
21
|
+
export interface RuntimeAgentPathStep {
|
|
22
|
+
className: string;
|
|
23
|
+
name: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 表示一次 Agent Tool 执行返回给 Runtime 的稳定字段。
|
|
28
|
+
*
|
|
29
|
+
* @remarks
|
|
30
|
+
* Subagent 端口在调用 `RuntimeAgentConfigContext.runAgentTool` 后读取它。
|
|
31
|
+
*
|
|
32
|
+
* 这里只保留 Runtime 需要的结果,避免把 SDK 内部运行对象暴露给应用。
|
|
33
|
+
*/
|
|
34
|
+
export interface RuntimeAgentToolResult {
|
|
35
|
+
status: string;
|
|
36
|
+
runId: string;
|
|
37
|
+
output?: unknown;
|
|
38
|
+
summary?: string;
|
|
39
|
+
error?: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export type RuntimeAgentRole = "primary" | "temporary";
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* 这是应用创建 Config 时唯一可以接触的 Agent 实例能力。
|
|
46
|
+
*
|
|
47
|
+
* 身份通过实时 getter 暴露。
|
|
48
|
+
*/
|
|
49
|
+
export interface RuntimeAgentConfigContext<
|
|
50
|
+
Env extends Cloudflare.Env = Cloudflare.Env,
|
|
51
|
+
> {
|
|
52
|
+
/** 读取当前 Durable Object 的状态句柄。 */
|
|
53
|
+
readonly ctx: DurableObjectState;
|
|
54
|
+
/** 读取当前 Worker 的环境绑定。 */
|
|
55
|
+
readonly env: Env;
|
|
56
|
+
/** 读取当前 Agent 实例名称。 */
|
|
57
|
+
readonly name: string;
|
|
58
|
+
/** 读取从根节点到直接父节点的 Agent 路径。 */
|
|
59
|
+
readonly parentPath: readonly RuntimeAgentPathStep[];
|
|
60
|
+
/** Runtime 根据 Agent facet 父子关系自动判定,不是外部配置项。 */
|
|
61
|
+
readonly role: RuntimeAgentRole;
|
|
62
|
+
/**
|
|
63
|
+
* 通过当前 Agent 调用一个受 SDK 管理的子 Agent Tool。
|
|
64
|
+
*
|
|
65
|
+
* @remarks
|
|
66
|
+
* subagent 端口在父 Agent 需要保留子运行状态、事件和取消边界时调用。
|
|
67
|
+
*/
|
|
68
|
+
runAgentTool(
|
|
69
|
+
agentClass: ChatCapableAgentClass,
|
|
70
|
+
options: RunAgentToolOptions<unknown>,
|
|
71
|
+
): Promise<RuntimeAgentToolResult>;
|
|
72
|
+
/**
|
|
73
|
+
* 清理符合年龄和状态条件的 SDK 子运行记录。
|
|
74
|
+
*/
|
|
75
|
+
clearAgentToolRuns(options: {
|
|
76
|
+
olderThan: number;
|
|
77
|
+
status: AgentToolRunStatus[];
|
|
78
|
+
}): Promise<void>;
|
|
79
|
+
/** 读取当前已装配 User Agent 的执行档位。 */
|
|
80
|
+
executionLevel(): ExecutionLevel;
|
|
81
|
+
/**
|
|
82
|
+
* 在当前 Session Runtime 中执行一个仅对本次调用存活的临时 Agent。
|
|
83
|
+
*/
|
|
84
|
+
runTemporaryAgent(
|
|
85
|
+
request: TemporaryAgentRequest,
|
|
86
|
+
context: TemporaryAgentRunContext,
|
|
87
|
+
): Promise<string>;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export interface RuntimeAgentPlanningContext<
|
|
91
|
+
Env extends Cloudflare.Env = Cloudflare.Env,
|
|
92
|
+
Command = never,
|
|
93
|
+
Change = never,
|
|
94
|
+
> extends RuntimeAgentConfigContext<Env> {
|
|
95
|
+
/** 仅可变 Definition 提供;Host Tool 用它复用生成类的更新与重载编排。 */
|
|
96
|
+
readonly updateConfig?: (
|
|
97
|
+
command: Command,
|
|
98
|
+
) => Promise<RuntimeConfigUpdateResult<Change>>;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** 装配、Hook 与 Port 准备共用的 Agent 上下文。 */
|
|
102
|
+
export type RuntimeAssemblyContext<
|
|
103
|
+
Env extends Cloudflare.Env = Cloudflare.Env,
|
|
104
|
+
Command = never,
|
|
105
|
+
Change = never,
|
|
106
|
+
> = RuntimeAgentPlanningContext<Env, Command, Change>;
|
|
107
|
+
|
|
108
|
+
export type RuntimeAgentContext<
|
|
109
|
+
Env extends Cloudflare.Env = Cloudflare.Env,
|
|
110
|
+
Command = never,
|
|
111
|
+
Change = never,
|
|
112
|
+
> = RuntimeAgentPlanningContext<Env, Command, Change>;
|