@springbrand/agent-runtime 0.3.0-alpha.8 → 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.
- package/package.json +2 -2
- package/src/adapter/cloudflare/subagent/definition.ts +4 -4
- package/src/adapter/cloudflare/subagent/runner.ts +1 -1
- package/src/adapter/cloudflare/subagent/tools.ts +2 -2
- package/src/adapter/cloudflare/universal-agent/preparation.ts +3 -3
- package/src/adapter/cloudflare/universal-agent/tools.ts +4 -6
- package/src/adapter/cloudflare/workspace/version-control.ts +1 -1
- package/src/config/agent-context.ts +1 -1
- package/src/config/assembler.ts +17 -31
- package/src/config/assembly-snapshot.ts +1 -1
- package/src/config/definition.ts +1 -1
- package/src/config/model-guidance.ts +1 -1
- package/src/config/ports.ts +3 -3
- package/src/config/public-contracts.ts +2 -2
- package/src/context/extension/assembly.ts +2 -2
- package/src/context/memory/budget/gate.ts +1 -1
- package/src/conversation/state.ts +1 -1
- package/src/index.ts +7 -7
- package/src/lib/prompt.ts +3 -5
- package/src/models/index.ts +1 -0
- package/src/session/cloudflare/migration.ts +29 -8
- package/src/skills/springbrand-worker-website/index.ts +1 -1
- package/src/submission/delivery.ts +1 -1
- package/src/submission/finalization.ts +1 -1
- package/src/telemetry/coordinator.ts +1 -1
- package/src/telemetry/subagent-lifecycle.ts +1 -1
- package/src/telemetry/turn-events.ts +1 -1
- package/src/tools/README.md +44 -0
- package/src/tools/{ai-adapter.ts → adapters/ai-to-pi.ts} +6 -9
- package/src/tools/{declared.ts → adapters/declared.ts} +2 -2
- package/src/tools/{nested-tools.ts → adapters/pi-to-ai.ts} +2 -2
- package/src/tools/assistant/ask-user.ts +317 -0
- package/src/tools/assistant/candidate.ts +15 -0
- package/src/tools/assistant/followups.ts +34 -0
- package/src/tools/assistant/index.ts +19 -0
- package/src/tools/assistant/memory.ts +73 -0
- package/src/tools/assistant/plan.ts +98 -0
- package/src/tools/{time.ts → assistant/time.ts} +1 -1
- package/src/tools/{core-host.ts → codemode/adapter.ts} +50 -49
- package/src/tools/{core.ts → codemode/index.ts} +36 -92
- package/src/tools/codemode/workspace-connector.ts +198 -0
- package/src/tools/extension/index.ts +42 -0
- package/src/tools/index.ts +28 -26
- package/src/tools/{gateway.ts → mcp/gateway.ts} +19 -19
- package/src/tools/{mcp.ts → mcp/index.ts} +5 -5
- package/src/tools/{approval → runtime/approval}/repository.ts +3 -9
- package/src/tools/{compiler.ts → runtime/compiler.ts} +24 -157
- package/src/tools/runtime/contracts.ts +117 -0
- package/src/tools/{decision-gate.ts → runtime/decision-gate.ts} +6 -9
- package/src/tools/{human-decisions.ts → runtime/human-decisions.ts} +4 -4
- package/src/tools/{interaction → runtime/interaction}/repository.ts +1 -7
- package/src/tools/runtime/result.ts +22 -0
- package/src/tools/sandbox/index.ts +228 -0
- package/src/tools/{schedule.ts → schedule/index.ts} +50 -49
- package/src/tools/skill/contracts.ts +20 -0
- package/src/tools/skill/index.ts +99 -0
- package/src/tools/skill/resources.ts +322 -0
- package/src/tools/skill/scripts.ts +168 -0
- package/src/tools/{subagent-authority.ts → subagent/authority.ts} +31 -31
- package/src/tools/{subagent.ts → subagent/index.ts} +34 -37
- package/src/tools/{subagent-run-repository.ts → subagent/repository.ts} +1 -1
- package/src/tools/{subagent-runtime.ts → subagent/runtime.ts} +1 -1
- package/src/tools/{temporary-agent → subagent/temporary}/approvals.ts +4 -7
- package/src/tools/{temporary-agent → subagent/temporary}/core.ts +1 -1
- package/src/tools/{temporary-agent → subagent/temporary}/runner.ts +1 -1
- package/src/tools/{temporary-agent → subagent/temporary}/workspace.ts +1 -4
- package/src/tools/{web-fetch.ts → web/fetch.ts} +4 -13
- package/src/tools/web/index.ts +2 -0
- package/src/tools/web/search/api.ts +1320 -0
- package/src/tools/web/search/index.ts +2 -0
- package/src/tools/{web-search/web-search.ts → web/search/search.ts} +5 -7
- package/src/tools/{workspace-sandbox.ts → workspace/index.ts} +12 -272
- package/src/tools/{workspace-revision.ts → workspace/revision.ts} +3 -3
- package/src/tools/base.ts +0 -542
- package/src/tools/skill.ts +0 -565
- package/src/tools/web-search/api.ts +0 -1318
- package/src/tools/web-search/index.ts +0 -2
- package/src/tools/workspace-state-connector.ts +0 -146
- package/src/tools/{approval → runtime/approval}/index.ts +0 -0
- package/src/tools/{interaction → runtime/interaction}/index.ts +0 -0
- package/src/tools/{unattended-error.ts → runtime/unattended-error.ts} +0 -0
- package/src/tools/{temporary-agent → subagent/temporary}/coordinator.ts +0 -0
- package/src/tools/{subagent-types → subagent/types}/contract.ts +0 -0
- package/src/tools/{subagent-types → subagent/types}/extract/index.ts +0 -0
- package/src/tools/{subagent-types → subagent/types}/fanout/index.ts +0 -0
- package/src/tools/{subagent-types → subagent/types}/registry.ts +1 -1
- /package/src/tools/{workspace-versioning.ts → workspace/versioning.ts} +0 -0
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import type { AgentTool } from "@earendil-works/pi-agent-core";
|
|
2
|
+
import { Type } from "@earendil-works/pi-ai";
|
|
3
|
+
import type { RuntimeSandboxPort, WorkspacePort } from "../../config/ports";
|
|
4
|
+
import type { PiToolCandidate } from "../runtime/contracts";
|
|
5
|
+
import { result, running } from "../runtime/result";
|
|
6
|
+
|
|
7
|
+
/** 从 {@link WorkspacePort} 生成标准 workspace 文件 Tool 集(read / write / edit …)。 */
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
const sandboxCommandParameters = {
|
|
12
|
+
command: Type.String({
|
|
13
|
+
minLength: 1,
|
|
14
|
+
maxLength: 32_768,
|
|
15
|
+
description: "Shell command to run in the isolated Linux Sandbox.",
|
|
16
|
+
}),
|
|
17
|
+
cwd: Type.Optional(Type.String({
|
|
18
|
+
maxLength: 4_096,
|
|
19
|
+
description: "Sandbox working directory. Omit to use the Sandbox default.",
|
|
20
|
+
})),
|
|
21
|
+
stdin: Type.Optional(Type.String({
|
|
22
|
+
maxLength: 65_536,
|
|
23
|
+
description: "Optional text to pass to the command on standard input.",
|
|
24
|
+
})),
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const sandboxExecParameters = Type.Object({
|
|
28
|
+
...sandboxCommandParameters,
|
|
29
|
+
timeoutMs: Type.Optional(
|
|
30
|
+
Type.Integer({
|
|
31
|
+
minimum: 1,
|
|
32
|
+
maximum: 60_000,
|
|
33
|
+
description: "Maximum command runtime in milliseconds, from 1 to 60000.",
|
|
34
|
+
}),
|
|
35
|
+
),
|
|
36
|
+
}, { additionalProperties: false });
|
|
37
|
+
|
|
38
|
+
const sandboxStartParameters = Type.Object(
|
|
39
|
+
sandboxCommandParameters,
|
|
40
|
+
{ additionalProperties: false },
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
const sandboxProcessParameters = Type.Object({
|
|
44
|
+
id: Type.String({
|
|
45
|
+
minLength: 1,
|
|
46
|
+
maxLength: 128,
|
|
47
|
+
description: "Process ID returned by sandbox_start_process.",
|
|
48
|
+
}),
|
|
49
|
+
}, { additionalProperties: false });
|
|
50
|
+
|
|
51
|
+
const sandboxPublishParameters = Type.Object({
|
|
52
|
+
paths: Type.Array(Type.String({
|
|
53
|
+
minLength: 1,
|
|
54
|
+
maxLength: 4_096,
|
|
55
|
+
description: "Sandbox file path to publish.",
|
|
56
|
+
}), {
|
|
57
|
+
minItems: 1,
|
|
58
|
+
maxItems: 100,
|
|
59
|
+
description: "Files to copy from the temporary Sandbox into the persistent Workspace.",
|
|
60
|
+
}),
|
|
61
|
+
}, { additionalProperties: false });
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 为现有 RuntimeSandboxPort 创建 Pi 命令、进程和文件发布工具。
|
|
65
|
+
*
|
|
66
|
+
* Worker 的 Sandbox Plugin 在容器能力和持久 Workspace 都可用时调用它。调用方
|
|
67
|
+
* 应传入已绑定当前会话的 Port;前台命令适合一次性执行,后台进程适合需要持续
|
|
68
|
+
* 运行的服务,二者不能互换结果语义。
|
|
69
|
+
*
|
|
70
|
+
* 这里的 “Sandbox” 指 Cloudflare Sandbox SDK 提供的隔离 Linux 容器;本层只
|
|
71
|
+
* 面向项目自己的 Port。命令限制、cwd 校验、工作区装载、超时、取消、清理和
|
|
72
|
+
* 冲突安全发布仍在 Port 内。容器文件不会自动写回 Workspace,只有 publish
|
|
73
|
+
* 工具显式选中的文件才进入持久视图;执行、启动、停止和发布继续走高风险审批。
|
|
74
|
+
*
|
|
75
|
+
* `Pi`、`Port` 与“候选工具”等项目核心术语见 `src/index.ts`。
|
|
76
|
+
*/
|
|
77
|
+
export function sandboxPiToolCandidates(
|
|
78
|
+
sandbox: RuntimeSandboxPort,
|
|
79
|
+
): PiToolCandidate[] {
|
|
80
|
+
const exec: AgentTool<typeof sandboxExecParameters> = {
|
|
81
|
+
name: "sandbox_exec",
|
|
82
|
+
label: "Run command",
|
|
83
|
+
description:
|
|
84
|
+
"Run a foreground command in this Chat's isolated Linux Sandbox. Files stay temporary until sandbox_publish_files is called.",
|
|
85
|
+
parameters: sandboxExecParameters,
|
|
86
|
+
// 作用:在 Sandbox 中运行一次前台命令并等待完整退出结果。
|
|
87
|
+
// 调用:Pi 执行构建、测试或其他会结束的命令时调用。
|
|
88
|
+
// 原因:输入和 AbortSignal 原样交给 Port,返回值只投影公开字段,
|
|
89
|
+
// 避免容器内部句柄进入对话。
|
|
90
|
+
async execute(_toolCallId, input, signal, onUpdate) {
|
|
91
|
+
signal?.throwIfAborted();
|
|
92
|
+
running(onUpdate, "Running the Sandbox command.");
|
|
93
|
+
const output = await sandbox.exec(input, signal);
|
|
94
|
+
return result({
|
|
95
|
+
success: output.success,
|
|
96
|
+
stdout: output.stdout,
|
|
97
|
+
stderr: output.stderr,
|
|
98
|
+
exitCode: output.exitCode,
|
|
99
|
+
stdoutTruncated: output.stdoutTruncated,
|
|
100
|
+
stderrTruncated: output.stderrTruncated,
|
|
101
|
+
workspacePersistence: "explicit_publish_required",
|
|
102
|
+
});
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
const startProcess: AgentTool<typeof sandboxStartParameters> = {
|
|
107
|
+
name: "sandbox_start_process",
|
|
108
|
+
label: "Start process",
|
|
109
|
+
description:
|
|
110
|
+
"Start a long-running background process in this Chat's isolated Linux Sandbox.",
|
|
111
|
+
parameters: sandboxStartParameters,
|
|
112
|
+
// 作用:在 Sandbox 中启动一个长期运行的后台进程并返回进程标识。
|
|
113
|
+
// 调用:Pi 启动开发服务器、监听器或其他不能等待退出的程序时调用。
|
|
114
|
+
// 原因:Cloudflare 官方接口把长期进程与 exec 分开;
|
|
115
|
+
// 这里只公开后续查日志和停止所需字段。
|
|
116
|
+
async execute(_toolCallId, input, signal, onUpdate) {
|
|
117
|
+
signal?.throwIfAborted();
|
|
118
|
+
running(onUpdate, "Starting the Sandbox process.");
|
|
119
|
+
const process = await sandbox.startProcess(input);
|
|
120
|
+
return result({
|
|
121
|
+
id: process.id,
|
|
122
|
+
command: process.command,
|
|
123
|
+
status: process.status,
|
|
124
|
+
...(process.exitCode === undefined
|
|
125
|
+
? {}
|
|
126
|
+
: { exitCode: process.exitCode }),
|
|
127
|
+
workspacePersistence: "explicit_publish_required",
|
|
128
|
+
});
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
const processLogs: AgentTool<typeof sandboxProcessParameters> = {
|
|
133
|
+
name: "sandbox_process_logs",
|
|
134
|
+
label: "Read process logs",
|
|
135
|
+
description:
|
|
136
|
+
"Read bounded stdout and stderr logs for one background Sandbox process.",
|
|
137
|
+
parameters: sandboxProcessParameters,
|
|
138
|
+
// 作用:读取一个 Sandbox 后台进程当前的有界日志。
|
|
139
|
+
// 调用:Pi 拿到 startProcess 返回的 id 后检查启动或运行状态时调用。
|
|
140
|
+
// 原因:日志截断由 Port 统一处理并显式回传标记,
|
|
141
|
+
// 避免适配层再次缓存不断增长的输出。
|
|
142
|
+
async execute(_toolCallId, { id }, signal, onUpdate) {
|
|
143
|
+
signal?.throwIfAborted();
|
|
144
|
+
running(onUpdate, "Reading the Sandbox process logs.");
|
|
145
|
+
const logs = await sandbox.getProcessLogs(id);
|
|
146
|
+
return result({
|
|
147
|
+
id: logs.id,
|
|
148
|
+
stdout: logs.stdout,
|
|
149
|
+
stderr: logs.stderr,
|
|
150
|
+
stdoutTruncated: logs.stdoutTruncated,
|
|
151
|
+
stderrTruncated: logs.stderrTruncated,
|
|
152
|
+
});
|
|
153
|
+
},
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
const stopProcess: AgentTool<typeof sandboxProcessParameters> = {
|
|
157
|
+
name: "sandbox_stop_process",
|
|
158
|
+
label: "Stop process",
|
|
159
|
+
description: "Stop one background Sandbox process.",
|
|
160
|
+
parameters: sandboxProcessParameters,
|
|
161
|
+
// 作用:停止一个由当前 Sandbox 管理的后台进程。
|
|
162
|
+
// 调用:Pi 不再需要该进程或需要清理运行环境时调用。
|
|
163
|
+
// 原因:只把进程 id 交给 Port,
|
|
164
|
+
// 进程归属检查和实际清理由 Sandbox 边界统一负责。
|
|
165
|
+
async execute(_toolCallId, { id }, signal, onUpdate) {
|
|
166
|
+
signal?.throwIfAborted();
|
|
167
|
+
running(onUpdate, "Stopping the Sandbox process.");
|
|
168
|
+
const output = await sandbox.stopProcess(id);
|
|
169
|
+
return result({ stopped: output.stopped });
|
|
170
|
+
},
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
const publishFiles: AgentTool<typeof sandboxPublishParameters> = {
|
|
174
|
+
name: "sandbox_publish_files",
|
|
175
|
+
label: "Publish files",
|
|
176
|
+
description:
|
|
177
|
+
"Publish named files from the temporary Sandbox into the scoped Workspace. Shared files remain read-only.",
|
|
178
|
+
parameters: sandboxPublishParameters,
|
|
179
|
+
// 作用:把明确列出的 Sandbox 文件发布到持久 Workspace。
|
|
180
|
+
// 调用:Pi 确认命令产物需要保留到会话文件区时调用。
|
|
181
|
+
// 原因:显式发布避免临时容器状态被误当成已持久化;
|
|
182
|
+
// 冲突、共享写保护和失败码继续由 Port 决定。
|
|
183
|
+
async execute(_toolCallId, { paths }, signal, onUpdate) {
|
|
184
|
+
signal?.throwIfAborted();
|
|
185
|
+
running(onUpdate, "Publishing Sandbox files to the Workspace.");
|
|
186
|
+
const output = await sandbox.publishFiles(paths);
|
|
187
|
+
return result({
|
|
188
|
+
files: output.files,
|
|
189
|
+
skipped: output.skipped,
|
|
190
|
+
failed: output.failed.map(({ path, errorCode }) => ({
|
|
191
|
+
path,
|
|
192
|
+
errorCode,
|
|
193
|
+
})),
|
|
194
|
+
bytes: output.bytes,
|
|
195
|
+
workspacePersistence: "published",
|
|
196
|
+
});
|
|
197
|
+
},
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
return [
|
|
201
|
+
{
|
|
202
|
+
owner: "sandbox",
|
|
203
|
+
requiredExecutionLevel: "high",
|
|
204
|
+
tool: exec,
|
|
205
|
+
},
|
|
206
|
+
{
|
|
207
|
+
owner: "sandbox",
|
|
208
|
+
requiredExecutionLevel: "high",
|
|
209
|
+
tool: startProcess,
|
|
210
|
+
},
|
|
211
|
+
{
|
|
212
|
+
owner: "sandbox",
|
|
213
|
+
requiredExecutionLevel: "safe",
|
|
214
|
+
retry: "idempotent",
|
|
215
|
+
tool: processLogs,
|
|
216
|
+
},
|
|
217
|
+
{
|
|
218
|
+
owner: "sandbox",
|
|
219
|
+
requiredExecutionLevel: "high",
|
|
220
|
+
tool: stopProcess,
|
|
221
|
+
},
|
|
222
|
+
{
|
|
223
|
+
owner: "sandbox",
|
|
224
|
+
requiredExecutionLevel: "high",
|
|
225
|
+
tool: publishFiles,
|
|
226
|
+
},
|
|
227
|
+
];
|
|
228
|
+
}
|
|
@@ -1,17 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
} from "
|
|
7
|
-
import {
|
|
8
|
-
import type {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
} from "../
|
|
12
|
-
import type { ScheduleSpec } from "../submission/contracts";
|
|
13
|
-
import { serializeOutput } from "../lib/artifacts";
|
|
14
|
-
import type { PiToolCandidate } from "./compiler";
|
|
1
|
+
import type { AgentTool, AgentToolResult } from "@earendil-works/pi-agent-core";
|
|
2
|
+
import { Type, fauxToolCall, validateToolArguments, type TSchema } from "@earendil-works/pi-ai";
|
|
3
|
+
import {
|
|
4
|
+
toScheduleRuleSpec,
|
|
5
|
+
validateScheduleRuleSpec,
|
|
6
|
+
} from "../../adapter/cloudflare/scheduling/schedule-rule";
|
|
7
|
+
import { isValidTimeZone } from "../../adapter/cloudflare/scheduling/zoned-cron";
|
|
8
|
+
import type { RuntimeSchedulePort, RuntimeSchedulePreparation } from "../../config/ports";
|
|
9
|
+
import { serializeOutput } from "../../lib/artifacts";
|
|
10
|
+
import type { ScheduleSpec } from "../../submission/contracts";
|
|
11
|
+
import type { PiToolCandidate } from "../runtime/contracts";
|
|
15
12
|
|
|
16
13
|
const scheduleTriggerParameters = Type.Union([
|
|
17
14
|
Type.Object({
|
|
@@ -62,7 +59,8 @@ const scheduleTriggerParameters = Type.Union([
|
|
|
62
59
|
});
|
|
63
60
|
|
|
64
61
|
const scheduleParameters = Type.Object({
|
|
65
|
-
prompt: Type.String({
|
|
62
|
+
prompt: Type.String({
|
|
63
|
+
minLength: 1, maxLength: 1000, pattern: "\\S",
|
|
66
64
|
description:
|
|
67
65
|
"The standalone instruction to run when the schedule fires. The future turn has no context from this chat, so make it self-contained.",
|
|
68
66
|
}),
|
|
@@ -260,40 +258,43 @@ export function schedulePiToolCandidates(
|
|
|
260
258
|
if (name !== "schedule" && name !== "update_schedule") return [toolCandidate];
|
|
261
259
|
const confirmation = schedule.confirmation;
|
|
262
260
|
if (!confirmation) return [];
|
|
263
|
-
return [{
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
if (
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
261
|
+
return [{
|
|
262
|
+
...toolCandidate, alwaysRequiresApproval: true, confirmation: {
|
|
263
|
+
async prepare(args, { intentId }) {
|
|
264
|
+
const validated = validateToolArguments(toolCandidate.tool, fauxToolCall(name, args as Record<string, unknown>));
|
|
265
|
+
const input = validated as { id?: string; prompt?: string; trigger?: ScheduleSpec["trigger"]; label?: string; timezone?: string };
|
|
266
|
+
if (input.timezone !== undefined && !isValidTimeZone(input.timezone)) throw new Error("A valid IANA timezone is required");
|
|
267
|
+
if (input.trigger?.kind === "local" && input.timezone) {
|
|
268
|
+
const error = validateScheduleRuleSpec(toScheduleRuleSpec(input.trigger, input.timezone));
|
|
269
|
+
if (error) throw new Error(error.message);
|
|
270
|
+
}
|
|
271
|
+
const prepared = await confirmation.prepare({
|
|
272
|
+
intentId, ...(name === "update_schedule" ? { id: input.id } : {}),
|
|
273
|
+
...(input.prompt !== undefined ? { prompt: input.prompt } : {}),
|
|
274
|
+
...(input.trigger !== undefined ? { trigger: input.trigger } : {}),
|
|
275
|
+
...(input.label !== undefined ? { label: input.label } : {}),
|
|
276
|
+
...(input.timezone !== undefined ? { tz: input.timezone } : {}),
|
|
277
|
+
});
|
|
278
|
+
if (prepared.id !== input.id) throw new Error("Host preparation changed the target Task ID");
|
|
279
|
+
const fullInput = { prompt: prepared.prompt, label: prepared.label, trigger: prepared.trigger, timezone: prepared.timezone };
|
|
280
|
+
void validateToolArguments({ ...toolCandidate.tool, parameters: scheduleParameters }, fauxToolCall(name, fullInput));
|
|
281
|
+
if (!prepared.label?.trim() || !isValidTimeZone(prepared.timezone) || !prepared.candidateId?.trim() || !prepared.candidateVersion?.trim() || !prepared.authorization?.version?.trim() || !prepared.authorization.scope?.trim() || !Array.isArray(prepared.authorization.missing)) {
|
|
282
|
+
throw new Error("Host preparation is incomplete");
|
|
283
|
+
}
|
|
284
|
+
if (prepared.trigger.kind === "local") {
|
|
285
|
+
const error = validateScheduleRuleSpec(toScheduleRuleSpec(prepared.trigger, prepared.timezone));
|
|
286
|
+
if (error) throw new Error(error.message);
|
|
287
|
+
}
|
|
288
|
+
return prepared;
|
|
289
|
+
},
|
|
290
|
+
async execute(prepared, context) {
|
|
291
|
+
const preview = prepared as RuntimeSchedulePreparation;
|
|
292
|
+
if (preview.authorization.missing.length) throw new Error(`Missing authorization: ${preview.authorization.missing.join("; ")}`);
|
|
293
|
+
const { id } = await confirmation.commit(preview, context);
|
|
294
|
+
return result({ [name === "schedule" ? "scheduled" : "updated"]: true, id, task: { label: preview.label, prompt: preview.prompt, trigger: preview.trigger, timezone: preview.timezone } });
|
|
295
|
+
},
|
|
296
|
+
}
|
|
297
|
+
}];
|
|
297
298
|
});
|
|
298
299
|
}
|
|
299
300
|
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type SkillSource } from "agents/skills";
|
|
2
|
+
import type { RuntimeSkillScriptPolicy, WorkspacePort } from "../../config/ports";
|
|
3
|
+
import type { RuntimeSkillBodyTransform } from "../../config/definition";
|
|
4
|
+
import type { PiToolCandidate } from "../runtime/contracts";
|
|
5
|
+
|
|
6
|
+
/** A configured Skill source and its script capabilities. */
|
|
7
|
+
export interface PiSkillBinding {
|
|
8
|
+
readonly name: string;
|
|
9
|
+
readonly description: string;
|
|
10
|
+
readonly source: SkillSource;
|
|
11
|
+
readonly script?: RuntimeSkillScriptPolicy;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Host capabilities available while creating Skill tools. */
|
|
15
|
+
export interface SkillPiToolOptions {
|
|
16
|
+
readonly loader?: WorkerLoader;
|
|
17
|
+
readonly workspace?: WorkspacePort;
|
|
18
|
+
readonly tools?: readonly PiToolCandidate[];
|
|
19
|
+
readonly transformSkillBody?: RuntimeSkillBodyTransform;
|
|
20
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { SkillRegistry } from "agents/skills";
|
|
2
|
+
import { aiToolToPi } from "../adapters/ai-to-pi";
|
|
3
|
+
import type { PiToolCandidate } from "../runtime/contracts";
|
|
4
|
+
import type { PiSkillBinding, SkillPiToolOptions } from "./contracts";
|
|
5
|
+
import { catalogSkillSource, materializeSkillResourceTool, wrapSkillResourceRead } from "./resources";
|
|
6
|
+
import { scriptRunner, wrapSkillScript } from "./scripts";
|
|
7
|
+
|
|
8
|
+
export type { PiSkillBinding, SkillPiToolOptions } from "./contracts";
|
|
9
|
+
const SKILL_TOOL_LABELS: Readonly<Record<string, string>> = {
|
|
10
|
+
activate_skill: "Activate Skill",
|
|
11
|
+
read_skill_resource: "Read Skill resource",
|
|
12
|
+
run_skill_script: "Run Skill script",
|
|
13
|
+
materialize_skill_resource: "Materialize Skill resource",
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const SKILL_TOOL_PARAMETER_DESCRIPTIONS: Readonly<Record<string, Readonly<Record<string, string>>>> = {
|
|
17
|
+
activate_skill: {
|
|
18
|
+
name: "Exact name of the available Skill to activate.",
|
|
19
|
+
},
|
|
20
|
+
read_skill_resource: {
|
|
21
|
+
name: "Name of the activated Skill. Omit only when path starts with the Skill name.",
|
|
22
|
+
path: "Bundled resource path listed by activate_skill.",
|
|
23
|
+
},
|
|
24
|
+
run_skill_script: {
|
|
25
|
+
name: "Name of the activated Skill that supplies the script.",
|
|
26
|
+
path: "Bundled script path listed by activate_skill.",
|
|
27
|
+
input: "JSON input expected by the Skill script. Defaults to an empty object.",
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/** Create Pi candidates from the official Agents SDK SkillRegistry tools. */
|
|
32
|
+
export async function skillPiToolCandidates(
|
|
33
|
+
bindings: readonly PiSkillBinding[],
|
|
34
|
+
options: SkillPiToolOptions = {},
|
|
35
|
+
): Promise<PiToolCandidate[]> {
|
|
36
|
+
if (bindings.length === 0) return [];
|
|
37
|
+
|
|
38
|
+
const availableToolNames = (options.tools ?? []).map(({ tool }) => tool.name);
|
|
39
|
+
|
|
40
|
+
const registry = new SkillRegistry(
|
|
41
|
+
bindings.map((binding) => catalogSkillSource(
|
|
42
|
+
binding,
|
|
43
|
+
options.transformSkillBody,
|
|
44
|
+
availableToolNames,
|
|
45
|
+
)),
|
|
46
|
+
scriptRunner(bindings, options),
|
|
47
|
+
);
|
|
48
|
+
await registry.load();
|
|
49
|
+
|
|
50
|
+
const tools = registry.tools();
|
|
51
|
+
if (options.workspace) {
|
|
52
|
+
tools.materialize_skill_resource = materializeSkillResourceTool(
|
|
53
|
+
bindings,
|
|
54
|
+
options.workspace,
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return Object.entries(tools).map(([name, tool]) => {
|
|
59
|
+
const adapted = aiToolToPi(name, tool, {
|
|
60
|
+
label: SKILL_TOOL_LABELS[name] ?? name,
|
|
61
|
+
...(name === "read_skill_resource"
|
|
62
|
+
? {
|
|
63
|
+
description:
|
|
64
|
+
"Read a bundled resource listed by activate_skill. Returns structured resource metadata and content. Do not pass SKILL.md; activate_skill returns the Skill instructions.",
|
|
65
|
+
}
|
|
66
|
+
: {}),
|
|
67
|
+
});
|
|
68
|
+
const properties = (adapted.parameters as { properties?: unknown }).properties;
|
|
69
|
+
if (properties && typeof properties === "object" && !Array.isArray(properties)) {
|
|
70
|
+
for (const [parameter, description] of Object.entries(
|
|
71
|
+
SKILL_TOOL_PARAMETER_DESCRIPTIONS[name] ?? {},
|
|
72
|
+
)) {
|
|
73
|
+
const schema = (properties as Record<string, unknown>)[parameter];
|
|
74
|
+
if (schema && typeof schema === "object" && !Array.isArray(schema)) {
|
|
75
|
+
(schema as Record<string, unknown>).description ??= description;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
if (name === "read_skill_resource") wrapSkillResourceRead(adapted, bindings);
|
|
80
|
+
if (name === "run_skill_script") wrapSkillScript(adapted, options.workspace);
|
|
81
|
+
return {
|
|
82
|
+
owner: "runtime-skill",
|
|
83
|
+
requiredExecutionLevel: "safe",
|
|
84
|
+
// activate_skill 返回的是 Skill 指令本身,是模型接下来所有动作的依据。把它外置
|
|
85
|
+
// 成文件,模型手里就只剩一个路径,必须再取一次才能知道该做什么 —— 而典型
|
|
86
|
+
// SKILL.md 正好落在会触发外置的区间。指令必须当场到手。
|
|
87
|
+
...(name === "activate_skill"
|
|
88
|
+
? { outputBudget: { kind: "structure" as const } }
|
|
89
|
+
: {}),
|
|
90
|
+
...(name === "activate_skill"
|
|
91
|
+
? { exposureMode: "direct" as const }
|
|
92
|
+
: { exposureMode: "codemode" as const }),
|
|
93
|
+
...(name === "activate_skill" || name === "read_skill_resource"
|
|
94
|
+
? { retry: "idempotent" as const }
|
|
95
|
+
: {}),
|
|
96
|
+
tool: adapted,
|
|
97
|
+
};
|
|
98
|
+
});
|
|
99
|
+
}
|