@springbrand/agent-runtime 0.3.0-alpha.7 → 0.3.0-alpha.9

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 (93) hide show
  1. package/package.json +2 -2
  2. package/src/adapter/cloudflare/agent.ts +4 -7
  3. package/src/adapter/cloudflare/define-agent.ts +2 -0
  4. package/src/adapter/cloudflare/schema.ts +3 -0
  5. package/src/adapter/cloudflare/subagent/definition.ts +4 -4
  6. package/src/adapter/cloudflare/subagent/runner.ts +1 -1
  7. package/src/adapter/cloudflare/subagent/tools.ts +2 -2
  8. package/src/adapter/cloudflare/universal-agent/preparation.ts +3 -3
  9. package/src/adapter/cloudflare/universal-agent/tools.ts +4 -6
  10. package/src/adapter/cloudflare/workspace/version-control.ts +1 -1
  11. package/src/config/agent-context.ts +1 -1
  12. package/src/config/assembler.ts +17 -31
  13. package/src/config/assembly-snapshot.ts +1 -1
  14. package/src/config/definition.ts +1 -1
  15. package/src/config/model-guidance.ts +1 -1
  16. package/src/config/ports.ts +5 -15
  17. package/src/config/public-contracts.ts +2 -2
  18. package/src/context/extension/assembly.ts +2 -2
  19. package/src/context/memory/budget/gate.ts +1 -1
  20. package/src/conversation/state.ts +1 -1
  21. package/src/index.ts +7 -7
  22. package/src/lib/execution-level.ts +13 -0
  23. package/src/lib/prompt.ts +3 -5
  24. package/src/models/index.ts +1 -0
  25. package/src/session/cloudflare/migration.ts +29 -8
  26. package/src/skills/springbrand-worker-website/index.ts +1 -1
  27. package/src/submission/admission.ts +4 -0
  28. package/src/submission/delivery.ts +1 -1
  29. package/src/submission/finalization.ts +1 -1
  30. package/src/submission/repository.ts +8 -1
  31. package/src/telemetry/coordinator.ts +1 -1
  32. package/src/telemetry/subagent-lifecycle.ts +1 -1
  33. package/src/telemetry/turn-events.ts +1 -1
  34. package/src/tools/README.md +44 -0
  35. package/src/tools/{ai-adapter.ts → adapters/ai-to-pi.ts} +6 -9
  36. package/src/tools/{declared.ts → adapters/declared.ts} +2 -2
  37. package/src/tools/{nested-tools.ts → adapters/pi-to-ai.ts} +2 -2
  38. package/src/tools/assistant/ask-user.ts +317 -0
  39. package/src/tools/assistant/candidate.ts +15 -0
  40. package/src/tools/assistant/followups.ts +34 -0
  41. package/src/tools/assistant/index.ts +19 -0
  42. package/src/tools/assistant/memory.ts +73 -0
  43. package/src/tools/assistant/plan.ts +98 -0
  44. package/src/tools/{time.ts → assistant/time.ts} +1 -1
  45. package/src/tools/{core-host.ts → codemode/adapter.ts} +50 -49
  46. package/src/tools/{core.ts → codemode/index.ts} +36 -92
  47. package/src/tools/codemode/workspace-connector.ts +198 -0
  48. package/src/tools/extension/index.ts +42 -0
  49. package/src/tools/index.ts +28 -26
  50. package/src/tools/{gateway.ts → mcp/gateway.ts} +19 -19
  51. package/src/tools/{mcp.ts → mcp/index.ts} +5 -5
  52. package/src/tools/{approval → runtime/approval}/repository.ts +3 -9
  53. package/src/tools/{compiler.ts → runtime/compiler.ts} +24 -155
  54. package/src/tools/runtime/contracts.ts +117 -0
  55. package/src/tools/{decision-gate.ts → runtime/decision-gate.ts} +36 -61
  56. package/src/tools/{human-decisions.ts → runtime/human-decisions.ts} +4 -4
  57. package/src/tools/{interaction → runtime/interaction}/repository.ts +1 -7
  58. package/src/tools/runtime/result.ts +22 -0
  59. package/src/tools/sandbox/index.ts +228 -0
  60. package/src/tools/{schedule.ts → schedule/index.ts} +50 -49
  61. package/src/tools/skill/contracts.ts +20 -0
  62. package/src/tools/skill/index.ts +99 -0
  63. package/src/tools/skill/resources.ts +322 -0
  64. package/src/tools/skill/scripts.ts +168 -0
  65. package/src/tools/{subagent-authority.ts → subagent/authority.ts} +31 -31
  66. package/src/tools/{subagent.ts → subagent/index.ts} +34 -37
  67. package/src/tools/{subagent-run-repository.ts → subagent/repository.ts} +1 -1
  68. package/src/tools/{subagent-runtime.ts → subagent/runtime.ts} +1 -1
  69. package/src/tools/{temporary-agent → subagent/temporary}/approvals.ts +4 -7
  70. package/src/tools/{temporary-agent → subagent/temporary}/core.ts +1 -1
  71. package/src/tools/{temporary-agent → subagent/temporary}/runner.ts +1 -1
  72. package/src/tools/{temporary-agent → subagent/temporary}/workspace.ts +1 -4
  73. package/src/tools/{web-fetch.ts → web/fetch.ts} +4 -13
  74. package/src/tools/web/index.ts +2 -0
  75. package/src/tools/web/search/api.ts +1320 -0
  76. package/src/tools/web/search/index.ts +2 -0
  77. package/src/tools/{web-search/web-search.ts → web/search/search.ts} +5 -7
  78. package/src/tools/{workspace-sandbox.ts → workspace/index.ts} +12 -272
  79. package/src/tools/{workspace-revision.ts → workspace/revision.ts} +3 -3
  80. package/src/tools/base.ts +0 -542
  81. package/src/tools/skill.ts +0 -565
  82. package/src/tools/web-search/api.ts +0 -1318
  83. package/src/tools/web-search/index.ts +0 -2
  84. package/src/tools/workspace-state-connector.ts +0 -146
  85. package/src/tools/{approval → runtime/approval}/index.ts +0 -0
  86. package/src/tools/{interaction → runtime/interaction}/index.ts +0 -0
  87. package/src/tools/{unattended-error.ts → runtime/unattended-error.ts} +0 -0
  88. package/src/tools/{temporary-agent → subagent/temporary}/coordinator.ts +0 -0
  89. package/src/tools/{subagent-types → subagent/types}/contract.ts +0 -0
  90. package/src/tools/{subagent-types → subagent/types}/extract/index.ts +0 -0
  91. package/src/tools/{subagent-types → subagent/types}/fanout/index.ts +0 -0
  92. package/src/tools/{subagent-types → subagent/types}/registry.ts +1 -1
  93. /package/src/tools/{workspace-versioning.ts → workspace/versioning.ts} +0 -0
@@ -0,0 +1,42 @@
1
+ import type { AgentTool } from "@earendil-works/pi-agent-core";
2
+ import { Type } from "@earendil-works/pi-ai";
3
+ import type { PiLoadedExtension } from "../../context/extension/assembly";
4
+ import type { PiToolCandidate } from "../runtime/contracts";
5
+ import { result } from "../runtime/result";
6
+
7
+
8
+ const noParameters = Type.Object({}, { additionalProperties: false });
9
+
10
+ /**
11
+ * 为当前已加载 Extension 列表创建一个 Pi 工具候选项。
12
+ *
13
+ * Runtime 组装层在 Extension 装载完成后调用,模型需要发现当前可用扩展时使用该工具。
14
+ *
15
+ * 候选项捕获准备阶段的只读列表,这与 pinned Runtime 组装保持一致,不能在执行时重新扫描出另一份 Extension 集合。
16
+ */
17
+ export function listExtensionsPiToolCandidate(
18
+ extensions: readonly PiLoadedExtension[],
19
+ ): PiToolCandidate {
20
+ const tool: AgentTool<typeof noParameters> = {
21
+ name: "list_extensions",
22
+ label: "List extensions",
23
+ description: "List all currently loaded extensions and their tools.",
24
+ parameters: noParameters,
25
+ // 返回这个 pinned Runtime 已加载的 Extension 快照。
26
+ // Pi 工具循环在模型选择 `list_extensions` 时调用,调用前允许 Turn 取消。
27
+ // 同时返回 count 和原列表,使模型既能快速判空又不丢失 Extension 细节。
28
+ async execute(_toolCallId, _input, signal) {
29
+ signal?.throwIfAborted();
30
+ return result({
31
+ count: extensions.length,
32
+ extensions,
33
+ });
34
+ },
35
+ };
36
+ return {
37
+ owner: "core:extensions",
38
+ requiredExecutionLevel: "low",
39
+ retry: "idempotent",
40
+ tool,
41
+ };
42
+ }
@@ -1,30 +1,32 @@
1
- export * from "./ai-adapter";
2
- export * from "./approval";
3
- export * from "./base";
4
- export * from "./compiler";
5
- export * from "./core";
6
- export * from "./core-host";
7
- export * from "./declared";
8
- export * from "./decision-gate";
9
- export * from "./human-decisions";
10
- export * from "./gateway";
1
+ export * from "./adapters/ai-to-pi";
2
+ export * from "./runtime/approval";
3
+ export * from "./assistant";
4
+ export * from "./runtime/contracts";
5
+ export * from "./runtime/compiler";
6
+ export * from "./codemode";
7
+ export * from "./codemode/adapter";
8
+ export * from "./extension";
9
+ export * from "./adapters/declared";
10
+ export * from "./runtime/decision-gate";
11
+ export * from "./runtime/human-decisions";
12
+ export * from "./mcp/gateway";
11
13
  export * from "./mcp";
12
- export * from "./interaction";
14
+ export * from "./runtime/interaction";
13
15
  export * from "./schedule";
14
16
  export * from "./skill";
15
17
  export * from "./subagent";
16
- export * from "./subagent-authority";
17
- export * from "./subagent-run-repository";
18
- export * from "./subagent-runtime";
19
- export * from "./subagent-types/contract";
20
- export * from "./subagent-types/registry";
21
- export * from "./temporary-agent/approvals";
22
- export * from "./temporary-agent/core";
23
- export * from "./temporary-agent/coordinator";
24
- export * from "./temporary-agent/runner";
25
- export * from "./temporary-agent/workspace";
26
- export * from "./time";
27
- export * from "./workspace-sandbox";
28
- export * from "./workspace-revision";
29
- export * from "./workspace-versioning";
30
- export * from "./web-search";
18
+ export * from "./subagent/authority";
19
+ export * from "./subagent/repository";
20
+ export * from "./subagent/runtime";
21
+ export * from "./subagent/types/contract";
22
+ export * from "./subagent/types/registry";
23
+ export * from "./subagent/temporary/approvals";
24
+ export * from "./subagent/temporary/core";
25
+ export * from "./subagent/temporary/coordinator";
26
+ export * from "./subagent/temporary/runner";
27
+ export * from "./subagent/temporary/workspace";
28
+ export * from "./workspace";
29
+ export * from "./sandbox";
30
+ export * from "./workspace/revision";
31
+ export * from "./workspace/versioning";
32
+ export * from "./web/search";
@@ -1,6 +1,6 @@
1
- import type { RuntimeGatewaySession } from "../config/ports";
2
- import type { PiToolCandidate } from "./compiler";
3
- import { createPiMcpToolCandidate } from "./mcp";
1
+ import type { RuntimeGatewaySession } from "../../config/ports";
2
+ import type { PiToolCandidate } from "../runtime/contracts";
3
+ import { createPiMcpToolCandidate } from "./index";
4
4
 
5
5
  const GATEWAY_TOOLS = [
6
6
  "search_capabilities",
@@ -31,22 +31,22 @@ export function createPiGatewayToolCandidates(
31
31
  requiredExecutionLevel: execute ? "high" : "safe",
32
32
  ...(execute
33
33
  ? {
34
- requiredExecutionLevelForInput: async (input: unknown) => {
35
- const reference = input && typeof input === "object" &&
36
- !Array.isArray(input)
37
- ? (input as Record<string, unknown>).name
38
- : undefined;
39
- if (typeof reference !== "string" || !reference) {
40
- throw new Error(
41
- "Gateway Capability Reference is not verified",
42
- );
43
- }
44
- return (await session.resolveCapabilityRisk(reference)) ===
45
- "none"
46
- ? "safe" as const
47
- : "high" as const;
48
- },
49
- }
34
+ requiredExecutionLevelForInput: async (input: unknown) => {
35
+ const reference = input && typeof input === "object" &&
36
+ !Array.isArray(input)
37
+ ? (input as Record<string, unknown>).name
38
+ : undefined;
39
+ if (typeof reference !== "string" || !reference) {
40
+ throw new Error(
41
+ "Gateway Capability Reference is not verified",
42
+ );
43
+ }
44
+ return (await session.resolveCapabilityRisk(reference)) ===
45
+ "none"
46
+ ? "safe" as const
47
+ : "high" as const;
48
+ },
49
+ }
50
50
  : {}),
51
51
  },
52
52
  );
@@ -1,6 +1,6 @@
1
- import type { RuntimeMcpServer } from "../config/profile";
2
- import type { PiToolCandidate } from "./compiler";
3
- import { createPiDeclaredToolCandidate } from "./declared";
1
+ import type { RuntimeMcpServer } from "../../config/profile";
2
+ import { createPiDeclaredToolCandidate } from "../adapters/declared";
3
+ import type { PiToolCandidate } from "../runtime/contracts";
4
4
 
5
5
  interface McpCallResult {
6
6
  content: unknown[];
@@ -34,7 +34,7 @@ interface PiMcpCandidatePolicy {
34
34
  * 单个 Server 的客户端,Server 是对外提供工具的服务。连接持久化、OAuth 和
35
35
  * 恢复仍由 Agents SDK 的 MCP Client 管理,本接口只读取和调用。
36
36
  *
37
- * `Host` 与 `Pi` 等项目核心术语见 `../../index.ts`。
37
+ * `Host` 与 `Pi` 等项目核心术语见 `src/index.ts`。
38
38
  */
39
39
  export interface PiMcpHost {
40
40
  /**
@@ -284,7 +284,7 @@ export function createPiMcpToolCandidate(
284
284
  * 远端 Server 提供的 annotations 只是提示,不是本地审批边界,所以所有动态
285
285
  * MCP 工具继续标为高风险,不能据此绕过审批。
286
286
  *
287
- * `Host`、`Pi`、`Runtime Snapshot` 与“候选工具”见 `../../index.ts`。
287
+ * `Host`、`Pi`、`Runtime Snapshot` 与“候选工具”见 `src/index.ts`。
288
288
  */
289
289
  export async function createPiMcpToolCandidates(
290
290
  host: PiMcpHost,
@@ -1,8 +1,7 @@
1
1
  import type { SqlTaggedTemplate } from "agents/chat";
2
- import type { ExecutionLevel } from "../../lib/execution-level";
2
+ import type { ExecutionLevel } from "../../../lib/execution-level";
3
3
 
4
4
  // Approval 的统一定义见 ./index.ts。
5
- // #region 状态与行类型
6
5
  export type ApprovalStatus = "pending" | "approved" | "rejected";
7
6
 
8
7
  export interface StoredApproval {
@@ -76,8 +75,6 @@ type ApprovalRow = {
76
75
  reason: string | null;
77
76
  };
78
77
 
79
- // #endregion
80
- // #region 行映射
81
78
  // 把 SQLite 的 Approval 行转成 Runtime 对象。
82
79
  // find 查到记录后调用,上层因而不需要理解 snake_case 列名。
83
80
  // 状态由表内字符串转成联合类型;新增列或状态时必须同步核对类型、SELECT 和映射。
@@ -101,13 +98,11 @@ function mapRow(row: ApprovalRow): StoredApproval {
101
98
  };
102
99
  }
103
100
 
104
- // #endregion
105
- // #region Repository
106
101
  export class ApprovalRepository {
107
102
  // 保存当前 Agent SQLite 的查询入口。
108
103
  // RuntimeDatabase 构造时调用,HarnessDecisionGate 和 Transcript 随后共享这个 Repository。
109
104
  // Approval 必须与所属 Submission 在同一 Agent 存储中查询,不能跨 Durable Object 拼接状态。
110
- constructor(private readonly sql: SqlTaggedTemplate) {}
105
+ constructor(private readonly sql: SqlTaggedTemplate) { }
111
106
 
112
107
  // 按 executionId 读取一条完整 Approval,不存在时返回 null。
113
108
  // HarnessDecisionGate 在请求去重、决策、继续和拒绝时调用,调用方必须处理 null。
@@ -198,7 +193,7 @@ export class ApprovalRepository {
198
193
  execution_level: string;
199
194
  required_level: string;
200
195
  input_json: string;
201
- confirmation_json: string | null;
196
+ confirmation_json: string | null;
202
197
  request_id: string;
203
198
  }>`
204
199
  SELECT approval.execution_id, approval.tool_call_id, approval.source,
@@ -278,4 +273,3 @@ export class ApprovalRepository {
278
273
  }));
279
274
  }
280
275
  }
281
- // #endregion
@@ -1,143 +1,16 @@
1
- import type {
2
- AgentHarnessTool,
3
- AgentTool,
4
- AgentToolResult,
5
- } from "@earendil-works/pi-agent-core";
6
- import type { ApprovalReceipt } from "../submission/contracts";
7
- import {
8
- boundDurableToolOutput,
9
- type BudgetPolicy,
10
- spillDurableToolOutput,
11
- } from "../context/memory/budget/gate";
12
- import {
13
- type ArtifactRef,
14
- serializeOutput,
15
- type SpillWorkspace,
16
- } from "../lib/artifacts";
17
- import {
18
- type ExecutionLevel,
19
- } from "../lib/execution-level";
1
+ import type { AgentHarnessTool, AgentToolResult } from "@earendil-works/pi-agent-core";
2
+ import { boundDurableToolOutput, spillDurableToolOutput } from "../../context/memory/budget/gate";
3
+ import { serializeOutput, type ArtifactRef, type SpillWorkspace } from "../../lib/artifacts";
4
+ import type { CompileHarnessToolsOptions, PiToolCandidate } from "./contracts";
20
5
  import { UnattendedExecutionError } from "./unattended-error";
21
6
 
22
- // 本文件沿用 `../../index.ts` 入口定义的 Tool Candidate 和 Tool Settlement 术语。
23
-
24
- // #region Public contracts
25
-
26
- /**
27
- * 声明一个 Tool 的结果由客户端投递,而不是由 `execute()` 产出。
28
- *
29
- * 带这个字段的 Tool 会在执行链里 park 住,等宿主经 `respondToolInteraction` 把响应送进来,
30
- * 再用 `settle` 把响应映射成这次 `toolCallId` 的权威 ToolResult。它的 `execute` 不会被调用。
31
- *
32
- * 这是 AI SDK 里「没有 execute 的 user-interaction tool」的等价物;差别是我们 park 住整个 Turn,
33
- * 而不是让 tool part 悬空到下一次请求 —— 我们的 transcript 是服务端权威的,悬空的 tool_use
34
- * 会让下一次模型调用非法。
35
- */
36
- export interface PiToolInteractionSpec {
37
- /** 结合 Tool 输入校验客户端响应体;不通过则拒绝投递,park 保持不变。 */
38
- readonly validateResponse: (input: unknown, response: unknown) => boolean;
39
- /** 把通过校验的响应映射成 ToolResult;缺省是原样回传。 */
40
- readonly settle?: (
41
- input: unknown,
42
- response: unknown,
43
- ) => Omit<AgentToolResult<unknown>, "terminate">;
44
- }
45
-
46
- /** Trusted Tool metadata used to evaluate a scheduled task grant. */
47
- export interface PiToolUnattendedExecutionSpec {
48
- readonly capability: string;
49
- readonly operation: string;
50
- readonly objectForInput?: (
51
- input: unknown,
52
- ) => unknown | Promise<unknown>;
53
- }
54
-
55
- /** 描述一个尚未进入最终 Tool Surface 和结算包装的 Pi 工具。 */
56
- export type ToolExposureMode = "direct" | "codemode" | "both";
57
-
58
- /** Host-prepared confirmation travels through the existing durable approval. */
59
- export interface PiToolConfirmationSpec {
60
- prepare(input: unknown, context: { intentId: string }): Promise<unknown>;
61
- execute(prepared: unknown, context: { intentId: string; signal: AbortSignal }): Promise<AgentToolResult<unknown>>;
62
- }
63
-
64
- export interface PiToolCandidate {
65
- /** Presentation only, resolved at the model boundary; never changes approved execution inputs. */
66
- readonly modelGuidance?: () => Promise<{ description: string; systemPrompt?: string }>;
67
- readonly confirmation?: PiToolConfirmationSpec;
68
- readonly owner: string;
69
- readonly tool: AgentTool<any, any>;
70
- /** @internal Send the complete schema but hide it until provider Tool Search finds it. */
71
- readonly deferLoading?: true;
72
- /** Omitted means visible both directly and through Code Mode. */
73
- readonly exposureMode?: ToolExposureMode;
74
- /** Conservative maximum used in the stable Runtime descriptor. */
75
- readonly requiredExecutionLevel: ExecutionLevel;
76
- /** Trusted parameter-level policy, evaluated before approval or dispatch. */
77
- readonly requiredExecutionLevelForInput?: (
78
- input: unknown,
79
- ) => ExecutionLevel | Promise<ExecutionLevel>;
80
- /**
81
- * 无条件需要人来决定:不看执行档位矩阵,每次调用都 park 出审批。
82
- *
83
- * 档位差是「够不够格自己跑」,不是「该不该问人」。花用户钱、改变账户
84
- * 状态这类工具,一旦 Agent 档位被 `allow_level` 永久拉高就会静默执行,
85
- * 那是失效不是降级。`deny` 仍然优先,被 deny 的工具根本不会注入。
86
- */
87
- readonly alwaysRequiresApproval?: boolean;
88
- /** Declares the concrete grant identity derived from actual Tool input. */
89
- readonly unattendedExecution?: PiToolUnattendedExecutionSpec;
90
- /** Recovery may replay this Tool Call only when the candidate guarantees convergence. Omitted means non-idempotent. */
91
- readonly retry?: "idempotent" | "non-idempotent";
92
- readonly summary?: string;
93
- readonly source?: ApprovalReceipt["source"];
94
- readonly interaction?: PiToolInteractionSpec;
95
- /** Large payloads spill by default; protocol-shaped results opt into structure. */
96
- readonly outputBudget?: BudgetPolicy;
97
- }
98
-
99
- export interface CompileHarnessToolsOptions {
100
- readonly workspace?: SpillWorkspace;
101
- readonly resolveConfirmation?: (candidate: PiToolCandidate, toolCallId: string, args: unknown) => { prepared: unknown; intentId: string };
102
- /** Recheck live authorization at the effect boundary, including safe replay. */
103
- readonly authorize?: (
104
- candidate: PiToolCandidate,
105
- args: unknown,
106
- invocation: { readonly toolCallId: string; readonly signal: AbortSignal },
107
- ) => void | Promise<void>;
108
- /** Return an already decided interaction result without invoking the handler. */
109
- readonly resolveReplay?: (
110
- candidate: PiToolCandidate,
111
- toolCallId: string,
112
- args: unknown,
113
- ) => AgentToolResult<unknown> | undefined | Promise<AgentToolResult<unknown> | undefined>;
114
- /** Optional business projection; Pi remains the authoritative Tool result. */
115
- readonly onSettlement?: (
116
- call: SettledPiToolCall,
117
- ) => void | Promise<void>;
118
- }
119
-
120
- /** 描述一次已进入持久化结算边界的 Pi 工具调用。 */
121
- export interface SettledPiToolCall {
122
- readonly toolCallId: string;
123
- readonly toolName: string;
124
- readonly args: unknown;
125
- readonly result: AgentToolResult<unknown>;
126
- readonly isError: boolean;
127
- /** Diagnostic-only original error; callers must not persist or expose it. */
128
- readonly cause?: unknown;
129
- readonly summary?: string;
130
- readonly source?: ApprovalReceipt["source"];
131
- }
132
-
133
7
  // DO SQLite rows also contain arguments and transcript/recovery envelopes.
134
8
  // Keep each serialized result below half the 2 MiB row limit.
135
9
  const DURABLE_TOOL_RESULT_MAX_BYTES = 1024 * 1024;
10
+
136
11
  const MEDIA_CAPTION_MAX_CHARS = 4 * 1024;
137
12
 
138
- // #endregion
139
13
 
140
- // #region Governance helpers
141
14
 
142
15
  // 把任意失败值收敛为可安全显示的简短消息。
143
16
  // 受治理工具在工具执行或结算抛错时调用它。
@@ -176,11 +49,11 @@ function artifactResult(
176
49
  let captionRemaining = MEDIA_CAPTION_MAX_CHARS;
177
50
  const mediaContent = result.content.some((part) => part.type !== "text")
178
51
  ? result.content.flatMap((part): AgentToolResult<unknown>["content"] => {
179
- if (part.type !== "text") return [part];
180
- const text = part.text.slice(0, captionRemaining);
181
- captionRemaining -= text.length;
182
- return text ? [{ ...part, text }] : [];
183
- })
52
+ if (part.type !== "text") return [part];
53
+ const text = part.text.slice(0, captionRemaining);
54
+ captionRemaining -= text.length;
55
+ return text ? [{ ...part, text }] : [];
56
+ })
184
57
  : [];
185
58
  return {
186
59
  ...result,
@@ -243,9 +116,7 @@ async function untilAborted<T>(
243
116
  }
244
117
  }
245
118
 
246
- // #endregion
247
119
 
248
- // #region Governance and compilation
249
120
 
250
121
  /**
251
122
  * Convert the configured candidate surface once at the AgentHarness boundary.
@@ -261,19 +132,19 @@ export function compileHarnessTools(
261
132
  const tool = source.constrainedSampling === false
262
133
  ? source
263
134
  : {
264
- ...source,
265
- constrainedSampling: source.constrainedSampling ?? {
266
- type: "json_schema" as const,
267
- strict: "prefer" as const,
268
- },
269
- };
135
+ ...source,
136
+ constrainedSampling: source.constrainedSampling ?? {
137
+ type: "json_schema" as const,
138
+ strict: "prefer" as const,
139
+ },
140
+ };
270
141
  return {
271
142
  ...tool,
272
143
  replay: candidate.retry === "idempotent" &&
273
- candidate.requiredExecutionLevel === "safe" &&
274
- !candidate.alwaysRequiresApproval &&
275
- !candidate.requiredExecutionLevelForInput &&
276
- !candidate.interaction
144
+ candidate.requiredExecutionLevel === "safe" &&
145
+ !candidate.alwaysRequiresApproval &&
146
+ !candidate.requiredExecutionLevelForInput &&
147
+ !candidate.interaction
277
148
  ? "safe" as const
278
149
  : "never" as const,
279
150
  ...(candidate.deferLoading ? { deferLoading: true as const } : {}),
@@ -303,10 +174,10 @@ export function compileHarnessTools(
303
174
  async () => durableResult(
304
175
  await (candidate.confirmation
305
176
  ? (() => {
306
- const confirmed = options.resolveConfirmation?.(candidate, toolCallId, args);
307
- if (!confirmed) throw new Error("User confirmation is required");
308
- return candidate.confirmation.execute(confirmed.prepared, { intentId: confirmed.intentId, signal });
309
- })()
177
+ const confirmed = options.resolveConfirmation?.(candidate, toolCallId, args);
178
+ if (!confirmed) throw new Error("User confirmation is required");
179
+ return candidate.confirmation.execute(confirmed.prepared, { intentId: confirmed.intentId, signal });
180
+ })()
310
181
  : source.execute(toolCallId, args, signal, (update) => onUpdate(update))),
311
182
  candidate,
312
183
  options.workspace,
@@ -349,5 +220,3 @@ export function compileHarnessTools(
349
220
  } satisfies AgentHarnessTool<object | undefined>;
350
221
  });
351
222
  }
352
-
353
- // #endregion
@@ -0,0 +1,117 @@
1
+ import type { AgentTool, AgentToolResult } from "@earendil-works/pi-agent-core";
2
+ import type { ApprovalReceipt } from "../../submission/contracts";
3
+ import { type BudgetPolicy } from "../../context/memory/budget/gate";
4
+ import { type SpillWorkspace } from "../../lib/artifacts";
5
+ import { type ExecutionLevel } from "../../lib/execution-level";
6
+
7
+ // 本文件沿用 `src/index.ts` 入口定义的 Tool Candidate 和 Tool Settlement 术语。
8
+
9
+
10
+ /**
11
+ * 声明一个 Tool 的结果由客户端投递,而不是由 `execute()` 产出。
12
+ *
13
+ * 带这个字段的 Tool 会在执行链里 park 住,等宿主经 `respondToolInteraction` 把响应送进来,
14
+ * 再用 `settle` 把响应映射成这次 `toolCallId` 的权威 ToolResult。它的 `execute` 不会被调用。
15
+ *
16
+ * 这是 AI SDK 里「没有 execute 的 user-interaction tool」的等价物;差别是我们 park 住整个 Turn,
17
+ * 而不是让 tool part 悬空到下一次请求 —— 我们的 transcript 是服务端权威的,悬空的 tool_use
18
+ * 会让下一次模型调用非法。
19
+ */
20
+ export interface PiToolInteractionSpec {
21
+ /** 结合 Tool 输入校验客户端响应体;不通过则拒绝投递,park 保持不变。 */
22
+ readonly validateResponse: (input: unknown, response: unknown) => boolean;
23
+ /** 把通过校验的响应映射成 ToolResult;缺省是原样回传。 */
24
+ readonly settle?: (
25
+ input: unknown,
26
+ response: unknown,
27
+ ) => Omit<AgentToolResult<unknown>, "terminate">;
28
+ }
29
+
30
+ /** Trusted Tool metadata used to evaluate a scheduled task grant. */
31
+ export interface PiToolUnattendedExecutionSpec {
32
+ readonly capability: string;
33
+ readonly operation: string;
34
+ readonly objectForInput?: (
35
+ input: unknown,
36
+ ) => unknown | Promise<unknown>;
37
+ }
38
+
39
+ /** 描述一个尚未进入最终 Tool Surface 和结算包装的 Pi 工具。 */
40
+ export type ToolExposureMode = "direct" | "codemode" | "both";
41
+
42
+ /** Host-prepared confirmation travels through the existing durable approval. */
43
+ export interface PiToolConfirmationSpec {
44
+ prepare(input: unknown, context: { intentId: string }): Promise<unknown>;
45
+ execute(prepared: unknown, context: { intentId: string; signal: AbortSignal }): Promise<AgentToolResult<unknown>>;
46
+ }
47
+
48
+ export interface PiToolCandidate {
49
+ /** Presentation only, resolved at the model boundary; never changes approved execution inputs. */
50
+ readonly modelGuidance?: () => Promise<{ description: string; systemPrompt?: string }>;
51
+ readonly confirmation?: PiToolConfirmationSpec;
52
+ readonly owner: string;
53
+ readonly tool: AgentTool<any, any>;
54
+ /** @internal Send the complete schema but hide it until provider Tool Search finds it. */
55
+ readonly deferLoading?: true;
56
+ /** Omitted means visible both directly and through Code Mode. */
57
+ readonly exposureMode?: ToolExposureMode;
58
+ /** Conservative maximum used in the stable Runtime descriptor. */
59
+ readonly requiredExecutionLevel: ExecutionLevel;
60
+ /** Trusted parameter-level policy, evaluated before approval or dispatch. */
61
+ readonly requiredExecutionLevelForInput?: (
62
+ input: unknown,
63
+ ) => ExecutionLevel | Promise<ExecutionLevel>;
64
+ /**
65
+ * 无条件需要人来决定:不看执行档位矩阵,每次调用都 park 出审批。
66
+ *
67
+ * 档位差是「够不够格自己跑」,不是「该不该问人」。花用户钱、改变账户
68
+ * 状态这类工具,一旦 Agent 档位被 `allow_level` 永久拉高就会静默执行,
69
+ * 那是失效不是降级。`deny` 仍然优先,被 deny 的工具根本不会注入。
70
+ */
71
+ readonly alwaysRequiresApproval?: boolean;
72
+ /** Declares the concrete grant identity derived from actual Tool input. */
73
+ readonly unattendedExecution?: PiToolUnattendedExecutionSpec;
74
+ /** Explicitly forbids this Tool from unattended Scheduled Runs. */
75
+ readonly unattendedMode?: "forbidden";
76
+ /** Recovery may replay this Tool Call only when the candidate guarantees convergence. Omitted means non-idempotent. */
77
+ readonly retry?: "idempotent" | "non-idempotent";
78
+ readonly summary?: string;
79
+ readonly source?: ApprovalReceipt["source"];
80
+ readonly interaction?: PiToolInteractionSpec;
81
+ /** Large payloads spill by default; protocol-shaped results opt into structure. */
82
+ readonly outputBudget?: BudgetPolicy;
83
+ }
84
+
85
+ export interface CompileHarnessToolsOptions {
86
+ readonly workspace?: SpillWorkspace;
87
+ readonly resolveConfirmation?: (candidate: PiToolCandidate, toolCallId: string, args: unknown) => { prepared: unknown; intentId: string };
88
+ /** Recheck live authorization at the effect boundary, including safe replay. */
89
+ readonly authorize?: (
90
+ candidate: PiToolCandidate,
91
+ args: unknown,
92
+ invocation: { readonly toolCallId: string; readonly signal: AbortSignal },
93
+ ) => void | Promise<void>;
94
+ /** Return an already decided interaction result without invoking the handler. */
95
+ readonly resolveReplay?: (
96
+ candidate: PiToolCandidate,
97
+ toolCallId: string,
98
+ args: unknown,
99
+ ) => AgentToolResult<unknown> | undefined | Promise<AgentToolResult<unknown> | undefined>;
100
+ /** Optional business projection; Pi remains the authoritative Tool result. */
101
+ readonly onSettlement?: (
102
+ call: SettledPiToolCall,
103
+ ) => void | Promise<void>;
104
+ }
105
+
106
+ /** 描述一次已进入持久化结算边界的 Pi 工具调用。 */
107
+ export interface SettledPiToolCall {
108
+ readonly toolCallId: string;
109
+ readonly toolName: string;
110
+ readonly args: unknown;
111
+ readonly result: AgentToolResult<unknown>;
112
+ readonly isError: boolean;
113
+ /** Diagnostic-only original error; callers must not persist or expose it. */
114
+ readonly cause?: unknown;
115
+ readonly summary?: string;
116
+ readonly source?: ApprovalReceipt["source"];
117
+ }