@springbrand/agent-runtime 0.1.3-alpha.4 → 0.1.3-alpha.6

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.
Files changed (45) hide show
  1. package/package.json +11 -3
  2. package/src/adapter/cloudflare/index.ts +55 -0
  3. package/src/adapter/cloudflare/resources/runtime-resources.ts +89 -0
  4. package/src/adapter/cloudflare/sandbox/adapter.ts +1509 -0
  5. package/src/adapter/cloudflare/sandbox/id.ts +23 -0
  6. package/src/adapter/cloudflare/sandbox/policy.ts +15 -0
  7. package/src/adapter/cloudflare/subagent/definition.ts +574 -0
  8. package/src/adapter/cloudflare/subagent/runner.ts +175 -0
  9. package/src/adapter/cloudflare/subagent/tools.ts +254 -0
  10. package/src/adapter/cloudflare/universal-agent/hooks.ts +35 -0
  11. package/src/adapter/cloudflare/universal-agent/preparation.ts +273 -0
  12. package/src/adapter/cloudflare/universal-agent/tools.ts +74 -0
  13. package/src/adapter/cloudflare/workspace/publisher.ts +31 -0
  14. package/src/adapter/cloudflare/workspace/scoped-workspace.ts +376 -0
  15. package/src/agent-tool-runtime.ts +152 -0
  16. package/src/index.ts +49 -7
  17. package/src/kernel/bindings.ts +6 -6
  18. package/src/kernel/recoverable-chat-agent.ts +12 -0
  19. package/src/kernel/runtime-load.ts +89 -0
  20. package/src/layers/orchestration/temporary-agent/core.ts +12 -1
  21. package/src/layers/orchestration/temporary-agent/runner.ts +1 -2
  22. package/src/lib/mcp.ts +7 -3
  23. package/src/pi/assembly/context.ts +3 -3
  24. package/src/pi/assembly/extensions.ts +11 -22
  25. package/src/pi/assembly/snapshot.ts +1 -1
  26. package/src/pi/message/contract.ts +7 -0
  27. package/src/pi/message/conversion.ts +9 -1
  28. package/src/pi/runtime-adapter/assembly.ts +4 -10
  29. package/src/pi/runtime-adapter/index.ts +6 -2
  30. package/src/pi/tool/base.ts +17 -2
  31. package/src/pi/tool/compiler.ts +0 -1
  32. package/src/pi/tool/core.ts +13 -3
  33. package/src/pi/tool/mcp.ts +3 -4
  34. package/src/pi/tool/schedule.ts +11 -1
  35. package/src/pi/tool/skill.ts +55 -41
  36. package/src/pi/tool/subagent.ts +14 -2
  37. package/src/pi/tool/web-fetch.ts +0 -1
  38. package/src/pi/tool/web-search/web-search.ts +0 -1
  39. package/src/pi/tool/workspace-sandbox.ts +15 -7
  40. package/src/runtime-agent-context.ts +112 -0
  41. package/src/runtime-agent.ts +442 -328
  42. package/src/runtime-assembler.ts +255 -103
  43. package/src/runtime-definition.ts +173 -0
  44. package/src/runtime.ts +185 -25
  45. package/src/tool-registry.ts +143 -0
@@ -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
 
@@ -87,7 +91,6 @@ export function subagentPiToolCandidates(
87
91
  };
88
92
  return {
89
93
  owner: `subagent:${type.name}`,
90
- authorized: true,
91
94
  requiredExecutionLevel: "safe",
92
95
  tool,
93
96
  };
@@ -138,7 +141,6 @@ export function subagentPiToolCandidates(
138
141
  };
139
142
  candidates.push({
140
143
  owner: "subagent:background",
141
- authorized: true,
142
144
  requiredExecutionLevel: "low",
143
145
  summary: "Dispatch a background sub-agent",
144
146
  source: "action",
@@ -146,3 +148,13 @@ export function subagentPiToolCandidates(
146
148
  });
147
149
  return candidates;
148
150
  }
151
+
152
+ /** 从 {@link RuntimeSubagentPort} 生成已启用 SubAgent Tool 集。 */
153
+ export function createSubagentTools(
154
+ subagents: RuntimeSubagentPort | undefined,
155
+ enabledSubagents: readonly string[],
156
+ ): ToolRegistry {
157
+ return toolRegistryFromPiCandidates(
158
+ subagentPiToolCandidates(subagents, enabledSubagents),
159
+ );
160
+ }
@@ -272,7 +272,6 @@ export function webFetchPiToolCandidate(): PiToolCandidate {
272
272
 
273
273
  return {
274
274
  owner: "runtime-base",
275
- authorized: true,
276
275
  requiredExecutionLevel: "safe",
277
276
  source: "action",
278
277
  tool,
@@ -119,7 +119,6 @@ export function webSearchPiToolCandidate(
119
119
  };
120
120
  return {
121
121
  owner: "runtime-base",
122
- authorized: true,
123
122
  requiredExecutionLevel: "safe",
124
123
  source: "action",
125
124
  tool,
@@ -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[] {
@@ -283,13 +288,11 @@ export function workspacePiToolCandidates(
283
288
  return [
284
289
  ...[read, write, edit, list, find, grep, remove].map((tool) => ({
285
290
  owner: "workspace",
286
- authorized: true,
287
291
  requiredExecutionLevel: "safe" as const,
288
292
  tool,
289
293
  })),
290
294
  {
291
295
  owner: "workspace",
292
- authorized: true,
293
296
  requiredExecutionLevel: "high" as const,
294
297
  summary: "Run a Bash script over Workspace files",
295
298
  source: "action" as const,
@@ -298,6 +301,11 @@ export function workspacePiToolCandidates(
298
301
  ];
299
302
  }
300
303
 
304
+ /** 从 {@link WorkspacePort} 生成标准 workspace 文件 Tool 集(read / write / edit …)。 */
305
+ export function createWorkspaceTools(workspace: WorkspacePort): ToolRegistry {
306
+ return toolRegistryFromPiCandidates(workspacePiToolCandidates(workspace));
307
+ }
308
+
301
309
  // #endregion
302
310
 
303
311
  // #region Sandbox tools
@@ -464,35 +472,35 @@ export function sandboxPiToolCandidates(
464
472
  return [
465
473
  {
466
474
  owner: "sandbox",
467
- authorized: true,
468
475
  requiredExecutionLevel: "high",
469
476
  tool: exec,
470
477
  },
471
478
  {
472
479
  owner: "sandbox",
473
- authorized: true,
474
480
  requiredExecutionLevel: "high",
475
481
  tool: startProcess,
476
482
  },
477
483
  {
478
484
  owner: "sandbox",
479
- authorized: true,
480
485
  requiredExecutionLevel: "safe",
481
486
  tool: processLogs,
482
487
  },
483
488
  {
484
489
  owner: "sandbox",
485
- authorized: true,
486
490
  requiredExecutionLevel: "high",
487
491
  tool: stopProcess,
488
492
  },
489
493
  {
490
494
  owner: "sandbox",
491
- authorized: true,
492
495
  requiredExecutionLevel: "high",
493
496
  tool: publishFiles,
494
497
  },
495
498
  ];
496
499
  }
497
500
 
501
+ /** 从 {@link RuntimeSandboxPort} 生成 Sandbox Tool 集。 */
502
+ export function createSandboxTools(sandbox: RuntimeSandboxPort): ToolRegistry {
503
+ return toolRegistryFromPiCandidates(sandboxPiToolCandidates(sandbox));
504
+ }
505
+
498
506
  // #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>;