pi-suite 0.0.0-stage → 0.3.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/LICENSE +21 -0
- package/README.md +100 -2
- package/README.zh-CN.md +101 -0
- package/package.json +63 -4
- package/skills/pi-suite-jobs/SKILL.md +76 -0
- package/skills/pi-suite-workflow/SKILL.md +114 -0
- package/src/agents/catalog.ts +80 -0
- package/src/agents/child.ts +79 -0
- package/src/agents/commands.ts +17 -0
- package/src/agents/defaults.ts +78 -0
- package/src/agents/history.ts +112 -0
- package/src/agents/index.ts +211 -0
- package/src/agents/manager.ts +438 -0
- package/src/agents/mentions/clone.ts +51 -0
- package/src/agents/mentions/grammar.ts +64 -0
- package/src/agents/mentions/index.ts +76 -0
- package/src/agents/mentions/provider.ts +79 -0
- package/src/agents/model-editor.ts +95 -0
- package/src/agents/models.ts +43 -0
- package/src/agents/output/schema.ts +101 -0
- package/src/agents/output/tool.ts +98 -0
- package/src/agents/parent.ts +13 -0
- package/src/agents/questions.ts +63 -0
- package/src/agents/records.ts +90 -0
- package/src/agents/registry.ts +20 -0
- package/src/agents/render.ts +85 -0
- package/src/agents/resources/context.ts +48 -0
- package/src/agents/resources/loader.ts +86 -0
- package/src/agents/resources/memory.ts +67 -0
- package/src/agents/resources/packages.ts +96 -0
- package/src/agents/resources/paths.ts +8 -0
- package/src/agents/resources/prompts.ts +11 -0
- package/src/agents/resources/scope.ts +147 -0
- package/src/agents/resources/selectors.ts +186 -0
- package/src/agents/retry.ts +20 -0
- package/src/agents/rpc/handlers.ts +62 -0
- package/src/agents/rpc/index.ts +73 -0
- package/src/agents/rpc/options.ts +44 -0
- package/src/agents/run-groups.ts +16 -0
- package/src/agents/runner.ts +131 -0
- package/src/agents/runtime.ts +40 -0
- package/src/agents/schedules/index.ts +136 -0
- package/src/agents/schedules/menu.ts +38 -0
- package/src/agents/schedules/parse.ts +42 -0
- package/src/agents/schedules/store.ts +78 -0
- package/src/agents/schema.ts +5 -0
- package/src/agents/session.ts +29 -0
- package/src/agents/tools.ts +177 -0
- package/src/agents/types.ts +87 -0
- package/src/agents/ui/index.ts +42 -0
- package/src/agents/ui/runs.ts +122 -0
- package/src/agents/ui/types.ts +91 -0
- package/src/agents/viewer-info.ts +36 -0
- package/src/agents/viewer.ts +160 -0
- package/src/agents/workflow/collisions.ts +124 -0
- package/src/agents/workflow/entry.ts +93 -0
- package/src/agents/workflow/host.ts +119 -0
- package/src/agents/workflow/index.ts +100 -0
- package/src/agents/workflow/journal.ts +147 -0
- package/src/agents/workflow/literal.ts +57 -0
- package/src/agents/workflow/manager.ts +169 -0
- package/src/agents/workflow/meta.ts +323 -0
- package/src/agents/workflow/progress/format.ts +184 -0
- package/src/agents/workflow/progress/index.ts +3 -0
- package/src/agents/workflow/progress/model.ts +224 -0
- package/src/agents/workflow/progress/types.ts +144 -0
- package/src/agents/workflow/render.ts +46 -0
- package/src/agents/workflow/runtime/helpers.ts +236 -0
- package/src/agents/workflow/runtime/index.ts +3 -0
- package/src/agents/workflow/runtime/run.ts +646 -0
- package/src/agents/workflow/runtime/types.ts +273 -0
- package/src/agents/workflow/saved.ts +74 -0
- package/src/agents/workflow/store.ts +23 -0
- package/src/agents/workflow/task.ts +277 -0
- package/src/agents/workflow/tool-description.ts +32 -0
- package/src/agents/workflow/tools.ts +66 -0
- package/src/agents/workflow/ui/menu.ts +144 -0
- package/src/agents/workflow/ui/progress.ts +91 -0
- package/src/agents/workflow/worker/bootstrap.ts +316 -0
- package/src/agents/workflow/worker/flow.ts +218 -0
- package/src/agents/workflow/worker/index.ts +7 -0
- package/src/agents/workflow/worker/scope.ts +215 -0
- package/src/ask/.gitkeep +0 -0
- package/src/ask/form.ts +67 -0
- package/src/ask/index.ts +73 -0
- package/src/ask/keys.ts +32 -0
- package/src/ask/layout.ts +78 -0
- package/src/ask/queue.ts +40 -0
- package/src/ask/render.ts +62 -0
- package/src/ask/result.ts +56 -0
- package/src/ask/schema.ts +67 -0
- package/src/ask/tool-render.ts +49 -0
- package/src/ask/view.ts +406 -0
- package/src/goal/.gitkeep +0 -0
- package/src/goal/activity.ts +50 -0
- package/src/goal/commands.ts +75 -0
- package/src/goal/context.ts +36 -0
- package/src/goal/index.ts +38 -0
- package/src/goal/manager.ts +386 -0
- package/src/goal/presentation.ts +15 -0
- package/src/goal/render.ts +36 -0
- package/src/goal/snapshot.ts +78 -0
- package/src/goal/state.ts +39 -0
- package/src/goal/store.ts +59 -0
- package/src/goal/tools.ts +59 -0
- package/src/goal/wait.ts +39 -0
- package/src/index.ts +20 -0
- package/src/jobs/backends/linux-tree.ts +87 -0
- package/src/jobs/backends/pipe.ts +74 -0
- package/src/jobs/backends/process.ts +79 -0
- package/src/jobs/backends/pty-job.ts +45 -0
- package/src/jobs/backends/pty.ts +502 -0
- package/src/jobs/backends/windows-tree.ts +289 -0
- package/src/jobs/commands.ts +7 -0
- package/src/jobs/control.ts +13 -0
- package/src/jobs/index.ts +331 -0
- package/src/jobs/launch/config.ts +26 -0
- package/src/jobs/launch/name.ts +16 -0
- package/src/jobs/launch/presets.ts +61 -0
- package/src/jobs/log-viewer.ts +147 -0
- package/src/jobs/manager.ts +507 -0
- package/src/jobs/monitors/compile.ts +101 -0
- package/src/jobs/monitors/hands-free.ts +108 -0
- package/src/jobs/monitors/runtime.ts +165 -0
- package/src/jobs/output.ts +54 -0
- package/src/jobs/query.ts +62 -0
- package/src/jobs/render.ts +103 -0
- package/src/jobs/runtime.ts +89 -0
- package/src/jobs/state.ts +25 -0
- package/src/jobs/store.ts +168 -0
- package/src/jobs/terminal/keys.ts +292 -0
- package/src/jobs/terminal/protocol.ts +37 -0
- package/src/jobs/terminal/read.ts +131 -0
- package/src/jobs/terminal/render.ts +70 -0
- package/src/jobs/terminal/screen.ts +92 -0
- package/src/jobs/terminal/session.ts +100 -0
- package/src/jobs/terminal/shell.ts +22 -0
- package/src/jobs/terminal/snapshot.ts +8 -0
- package/src/jobs/terminal/visible-text.ts +53 -0
- package/src/jobs/tools.ts +203 -0
- package/src/jobs/types.ts +60 -0
- package/src/jobs/viewer/component.ts +209 -0
- package/src/jobs/viewer/metadata.ts +9 -0
- package/src/jobs/viewer.ts +149 -0
- package/src/jobs/workspace.ts +31 -0
- package/src/runtime/.gitkeep +0 -0
- package/src/runtime/binding.ts +26 -0
- package/src/runtime/cli-prompt.ts +24 -0
- package/src/runtime/clipboard.ts +16 -0
- package/src/runtime/config-edit.ts +31 -0
- package/src/runtime/config-fields.ts +65 -0
- package/src/runtime/config.ts +111 -0
- package/src/runtime/errors.ts +11 -0
- package/src/runtime/json-store.ts +156 -0
- package/src/runtime/notification-render.ts +35 -0
- package/src/runtime/notifications.ts +98 -0
- package/src/runtime/owner.ts +54 -0
- package/src/runtime/read.ts +200 -0
- package/src/runtime/renderers.ts +50 -0
- package/src/runtime/responses/index.ts +108 -0
- package/src/runtime/responses/payload.ts +78 -0
- package/src/runtime/settings/index.ts +24 -0
- package/src/runtime/settings/view.ts +84 -0
- package/src/runtime/suite.ts +192 -0
- package/src/runtime/text.ts +6 -0
- package/src/runtime/tools.ts +131 -0
- package/src/runtime/wait-render.ts +33 -0
- package/src/runtime/wait.ts +134 -0
- package/src/tasks/commands.ts +41 -0
- package/src/tasks/index.ts +305 -0
- package/src/tasks/model.ts +152 -0
- package/src/tasks/presentation.ts +40 -0
- package/src/tasks/query.ts +38 -0
- package/src/tasks/render.ts +54 -0
- package/src/tasks/schema.ts +25 -0
- package/src/tasks/store.ts +164 -0
- package/src/tasks/tools.ts +69 -0
- package/src/tasks/types.ts +61 -0
- package/src/tasks/viewer.ts +68 -0
- package/src/ui/.gitkeep +0 -0
- package/src/ui/actions.ts +50 -0
- package/src/ui/browser-chrome.ts +175 -0
- package/src/ui/browser.ts +292 -0
- package/src/ui/details.ts +111 -0
- package/src/ui/dock/line.ts +14 -0
- package/src/ui/dock/rows.ts +78 -0
- package/src/ui/dock.ts +224 -0
- package/src/ui/format.ts +25 -0
- package/src/ui/frame.ts +8 -0
- package/src/ui/inline-input.ts +98 -0
- package/src/ui/reading.ts +105 -0
- package/src/ui/rendering/builtin-command.ts +34 -0
- package/src/ui/rendering/command.ts +60 -0
- package/src/ui/rendering/read-page.ts +15 -0
- package/src/ui/rendering/shell/bash.ts +281 -0
- package/src/ui/rendering/shell/index.ts +24 -0
- package/src/ui/rendering/shell/powershell.ts +155 -0
- package/src/ui/rendering/shell/spans.ts +28 -0
- package/src/ui/scroll.ts +111 -0
- package/src/ui/select.ts +67 -0
- package/src/ui/status.ts +67 -0
- package/src/ui/structured-details.ts +41 -0
- package/src/ui/terminal.ts +137 -0
- package/src/ui/ticker.ts +18 -0
- package/src/ui/tool-render.ts +152 -0
- package/src/ui/transcript.ts +140 -0
- package/src/ui/viewer/agent-information.ts +34 -0
- package/src/ui/viewer/editor.ts +18 -0
- package/src/ui/viewer/navigation.ts +25 -0
- package/themes/oh-my-pi.json +95 -0
- package/third-party/UPSTREAM.md +88 -0
- package/third-party/licenses/.gitkeep +0 -0
- package/third-party/licenses/oh-my-pi.txt +23 -0
- package/third-party/licenses/pi-ask-user.txt +21 -0
- package/third-party/licenses/pi-background-jobs.txt +21 -0
- package/third-party/licenses/pi-goal.txt +21 -0
- package/third-party/licenses/pi-interactive-shell.txt +25 -0
- package/third-party/licenses/pi-subagents.txt +21 -0
- package/third-party/licenses/pi-tasks.txt +21 -0
- package/third-party/sources.json +111 -0
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
// Imported/adapted from @tintinweb/pi-subagents 0.23.0 src/workflow/runtime.ts (MIT).
|
|
2
|
+
import { cpus } from "node:os";
|
|
3
|
+
import type { CompiledSchema } from "../../output/schema.ts";
|
|
4
|
+
import type { WorkflowJournalEntry } from "../journal.ts";
|
|
5
|
+
import type { WorkflowMeta } from "../meta.ts";
|
|
6
|
+
import type { WorkflowEntry } from "../progress/index.ts";
|
|
7
|
+
|
|
8
|
+
export const MAX_SCRIPT_LENGTH = 524_288;
|
|
9
|
+
|
|
10
|
+
/** Agents one run may schedule, in total. */
|
|
11
|
+
export const WORKFLOW_AGENT_CAP = 1000;
|
|
12
|
+
|
|
13
|
+
/** Items one `parallel()` or `pipeline()` call may take. */
|
|
14
|
+
export const WORKFLOW_ITEM_CAP = 4096;
|
|
15
|
+
|
|
16
|
+
/** Nested `workflow()` invocations allowed per run. */
|
|
17
|
+
export const WORKFLOW_NESTED_CAP = 256;
|
|
18
|
+
|
|
19
|
+
export class WorkflowRuntimeError extends Error {}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Concurrent agents allowed, leaving two cores for the host and the TUI.
|
|
23
|
+
*
|
|
24
|
+
* `Math.max(1, …)` is not decoration: the raw `min(16, cpus - 2)` is 0 on a one-
|
|
25
|
+
* or two-core machine, and a semaphore with zero permits never hands out a slot,
|
|
26
|
+
* so the run would hang before its first agent rather than fail.
|
|
27
|
+
*/
|
|
28
|
+
export function workflowConcurrency(cpuCount: number = cpus().length): number {
|
|
29
|
+
return Math.max(1, Math.min(16, cpuCount - 2));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** One agent the script asked for. `agentId` is the handle for {@link WorkflowHost.abortAgent}. */
|
|
33
|
+
export interface WorkflowSpawnRequest {
|
|
34
|
+
agentId: string;
|
|
35
|
+
/** Position in the run, and the progress entry's stable identity. */
|
|
36
|
+
index: number;
|
|
37
|
+
prompt: string;
|
|
38
|
+
label: string;
|
|
39
|
+
agentType: string;
|
|
40
|
+
model?: string;
|
|
41
|
+
/**
|
|
42
|
+
* Reasoning effort for this child, as one of pi's thinking levels.
|
|
43
|
+
*
|
|
44
|
+
* Typed as a plain string because this interface is the host boundary and
|
|
45
|
+
* deliberately knows nothing about pi — `host.ts` is where it becomes a
|
|
46
|
+
* `ThinkingLevel`. The worker has already rejected anything off the list.
|
|
47
|
+
*/
|
|
48
|
+
effort?: string;
|
|
49
|
+
/**
|
|
50
|
+
* Called by the host once the child's EFFECTIVE configuration is known —
|
|
51
|
+
* which is when its session exists, not when the spawn resolves.
|
|
52
|
+
*
|
|
53
|
+
* Without it a row could only ever show what the script asked for: a fuzzy
|
|
54
|
+
* `model: "haiku"` stays `haiku` instead of the id it resolved to, an
|
|
55
|
+
* `agent()` that named no model shows nothing at all, and a level pi clamped
|
|
56
|
+
* is presented as the level that was requested (#168, #182).
|
|
57
|
+
*
|
|
58
|
+
* Plain strings, like `effort` above: this interface is the host boundary and
|
|
59
|
+
* deliberately knows nothing about pi's `AgentInvocation`. Optional, so a host
|
|
60
|
+
* that cannot report any of this simply does not, and the row keeps the
|
|
61
|
+
* requested values it started with.
|
|
62
|
+
*/
|
|
63
|
+
onResolved?(info: {
|
|
64
|
+
/**
|
|
65
|
+
* The host's own id for the child — the manager's `AgentRecord` id here.
|
|
66
|
+
*
|
|
67
|
+
* Reported as soon as the host has one, which is earlier than the rest of
|
|
68
|
+
* this: the model is knowable only once a session exists, but the id is
|
|
69
|
+
* what lets a reader open that child's conversation, and a child that
|
|
70
|
+
* never got a model is exactly the one worth opening.
|
|
71
|
+
*/
|
|
72
|
+
recordId?: string;
|
|
73
|
+
modelName?: string;
|
|
74
|
+
modelId?: string;
|
|
75
|
+
thinking?: string;
|
|
76
|
+
requestedThinking?: string;
|
|
77
|
+
requestedModel?: string;
|
|
78
|
+
}): void;
|
|
79
|
+
/**
|
|
80
|
+
* Compiled from the script's `agent({ schema })`.
|
|
81
|
+
*
|
|
82
|
+
* The host must give the child a `StructuredOutput` tool built from it and
|
|
83
|
+
* return the validated payload as JSON text. Compiled rather than raw so the
|
|
84
|
+
* runtime can re-check the answer without re-parsing the schema per call.
|
|
85
|
+
*/
|
|
86
|
+
schema?: CompiledSchema;
|
|
87
|
+
phaseIndex?: number;
|
|
88
|
+
phaseTitle?: string;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export interface WorkflowSpawnResult {
|
|
92
|
+
ok: boolean;
|
|
93
|
+
/** The agent's answer. Present when `ok`. */
|
|
94
|
+
text?: string;
|
|
95
|
+
/** Why it failed. Present when not `ok`. */
|
|
96
|
+
error?: string;
|
|
97
|
+
/** The user dismissed it rather than it failing; renders as skipped. */
|
|
98
|
+
skipped?: boolean;
|
|
99
|
+
tokens?: number;
|
|
100
|
+
/**
|
|
101
|
+
* Output tokens only, for the script's `budget.spent()`.
|
|
102
|
+
*
|
|
103
|
+
* Separate from {@link tokens}, which is the lifetime total. Claude Code's
|
|
104
|
+
* budget counts output, and a fan-out's re-sent input would swamp it.
|
|
105
|
+
*/
|
|
106
|
+
outputTokens?: number;
|
|
107
|
+
/** Whether the child needed an extra prompt to produce its structured answer. */
|
|
108
|
+
structuredRetried?: boolean;
|
|
109
|
+
toolCalls?: number;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Outcome of a `gate` command. `output` is what the user is shown when it fails. */
|
|
113
|
+
export interface WorkflowGateResult {
|
|
114
|
+
ok: boolean;
|
|
115
|
+
/** Combined stdout/stderr, or whatever the host wants surfaced as the failure. */
|
|
116
|
+
output: string;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** The one seam between a workflow and the rest of the extension. */
|
|
120
|
+
/** How a script names another workflow: a saved name, or a path to a file. */
|
|
121
|
+
export interface WorkflowScriptRef {
|
|
122
|
+
name?: string;
|
|
123
|
+
scriptPath?: string;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export type WorkflowScriptSource =
|
|
127
|
+
| { ok: true; script: string; path?: string }
|
|
128
|
+
| { ok: false; message: string };
|
|
129
|
+
|
|
130
|
+
export interface WorkflowHost {
|
|
131
|
+
spawnAgent(request: WorkflowSpawnRequest): Promise<WorkflowSpawnResult>;
|
|
132
|
+
/** Called for every in-flight agent when the run aborts. */
|
|
133
|
+
abortAgent(agentId: string): void;
|
|
134
|
+
/**
|
|
135
|
+
* Continue a child that already ran in this run, keeping its context.
|
|
136
|
+
*
|
|
137
|
+
* `agentId` is one previously handed out in a {@link WorkflowSpawnRequest};
|
|
138
|
+
* the child keeps the agent type, model and tool contract it started with, so
|
|
139
|
+
* only the follow-up prompt crosses.
|
|
140
|
+
*
|
|
141
|
+
* Optional: a host without it rejects `resume` rather than quietly starting a
|
|
142
|
+
* fresh child that has none of the context the script is counting on.
|
|
143
|
+
*/
|
|
144
|
+
resumeAgent?(
|
|
145
|
+
agentId: string,
|
|
146
|
+
prompt: string,
|
|
147
|
+
/**
|
|
148
|
+
* Same reporter {@link WorkflowSpawnRequest.onResolved} carries, for the
|
|
149
|
+
* same reason: a resumed row is rebuilt from scratch, so without it the
|
|
150
|
+
* continuation of a child would show the model the script *asked* for while
|
|
151
|
+
* the row above it shows the one that ran.
|
|
152
|
+
*/
|
|
153
|
+
onResolved?: WorkflowSpawnRequest["onResolved"],
|
|
154
|
+
): Promise<WorkflowSpawnResult>;
|
|
155
|
+
/**
|
|
156
|
+
* Run a `gate` command and report whether it passed.
|
|
157
|
+
*
|
|
158
|
+
* The host runs it in the workflow's cwd. Optional for the same reason
|
|
159
|
+
* as {@link resumeAgent}: a gate that silently does not run
|
|
160
|
+
* would mark unverified work as verified, so the runtime fails the call
|
|
161
|
+
* instead of skipping it.
|
|
162
|
+
*/
|
|
163
|
+
runGate?(command: string, options: { agentId: string; onStarted?(jobId: string): void }): Promise<WorkflowGateResult>;
|
|
164
|
+
/**
|
|
165
|
+
* Resolve a nested `workflow()` reference to source.
|
|
166
|
+
*
|
|
167
|
+
* The runtime knows nothing about the filesystem or about pi, so it asks. It
|
|
168
|
+
* still decides whether what comes back *is* a workflow — see
|
|
169
|
+
* {@link validateScript} — because those rules belong with the runtime that
|
|
170
|
+
* enforces them everywhere else.
|
|
171
|
+
*
|
|
172
|
+
* Optional for the same reason as {@link resumeAgent}: a host without it
|
|
173
|
+
* rejects `workflow()` outright rather than silently running nothing.
|
|
174
|
+
*/
|
|
175
|
+
loadWorkflow?(ref: WorkflowScriptRef): Promise<WorkflowScriptSource> | WorkflowScriptSource;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* What a run can be told to do while it is going, from the workflows dialog.
|
|
180
|
+
*
|
|
181
|
+
* Every method is best-effort and idempotent: the dialog renders off a progress
|
|
182
|
+
* log that lags the runtime slightly, so it will sometimes ask for something
|
|
183
|
+
* that has just stopped being possible. `false` means "there was nothing to do
|
|
184
|
+
* that to" — a caller can say so, but it is never an error.
|
|
185
|
+
*/
|
|
186
|
+
export interface WorkflowControl {
|
|
187
|
+
/**
|
|
188
|
+
* Stop *starting* agents. Ones already running are left to finish, because
|
|
189
|
+
* killing model work mid-turn throws away everything it has spent and there
|
|
190
|
+
* is no way to hand it back its context.
|
|
191
|
+
*/
|
|
192
|
+
pause(): void;
|
|
193
|
+
resume(): void;
|
|
194
|
+
isPaused(): boolean;
|
|
195
|
+
/**
|
|
196
|
+
* Give up on the agent at `index`: its `agent()` call returns `null`, exactly
|
|
197
|
+
* as a terminal failure does, and the row renders skipped.
|
|
198
|
+
*
|
|
199
|
+
* Immediate for a running agent and for one held at a pause. An agent parked
|
|
200
|
+
* behind the concurrency limit takes its skip when it reaches the front —
|
|
201
|
+
* the alternative is a cancellable semaphore for a case that resolves itself
|
|
202
|
+
* as soon as any sibling finishes.
|
|
203
|
+
*/
|
|
204
|
+
skip(index: number): boolean;
|
|
205
|
+
/**
|
|
206
|
+
* Start the agent at `index` over: the child is stopped and the same call is
|
|
207
|
+
* re-run, so the script's `agent()` promise is still the one waiting and it
|
|
208
|
+
* gets the new answer.
|
|
209
|
+
*
|
|
210
|
+
* Only while it is running — that is the whole window. Once the call has
|
|
211
|
+
* settled its value is already the script's, and re-running would produce a
|
|
212
|
+
* result with nowhere to go.
|
|
213
|
+
*/
|
|
214
|
+
retry(index: number): boolean;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export interface RunWorkflowOptions {
|
|
218
|
+
/** Full script source, starting with `export const meta = { … }`. */
|
|
219
|
+
script: string;
|
|
220
|
+
args?: unknown;
|
|
221
|
+
host: WorkflowHost;
|
|
222
|
+
signal?: AbortSignal;
|
|
223
|
+
/** Validated once by the core, before the Worker and its first progress batch. */
|
|
224
|
+
onMeta?(meta: WorkflowMeta): void;
|
|
225
|
+
/** Fired per batch, not per entry — see the worker's progress batching. */
|
|
226
|
+
onProgress?(entries: readonly WorkflowEntry[]): void;
|
|
227
|
+
concurrency?: number;
|
|
228
|
+
agentCap?: number;
|
|
229
|
+
itemCap?: number;
|
|
230
|
+
/**
|
|
231
|
+
* Hands the caller the run's control surface, once per run.
|
|
232
|
+
*
|
|
233
|
+
* A callback rather than a return value because `runWorkflow` resolves when
|
|
234
|
+
* the run is *over*, which is the one moment there is nothing left to
|
|
235
|
+
* control. Fired before the first agent starts.
|
|
236
|
+
*/
|
|
237
|
+
onControl?(control: WorkflowControl): void;
|
|
238
|
+
/**
|
|
239
|
+
* How many nested `workflow()` invocations one run may make in total.
|
|
240
|
+
*
|
|
241
|
+
* Each costs a compile and a scope rather than a thread, so the ceiling is
|
|
242
|
+
* generous — but unbounded is worse than capped, on the same reasoning as
|
|
243
|
+
* {@link agentCap}.
|
|
244
|
+
*/
|
|
245
|
+
nestedCap?: number;
|
|
246
|
+
/**
|
|
247
|
+
* Replay and record, for `resumeFromRunId`.
|
|
248
|
+
*
|
|
249
|
+
* The runtime does no file IO — `entries` come in already read and `append`
|
|
250
|
+
* goes back out — so its tests stay free of a filesystem, the same reason
|
|
251
|
+
* spawning is behind {@link WorkflowHost}.
|
|
252
|
+
*/
|
|
253
|
+
journal?: {
|
|
254
|
+
/** A previous run's settled calls, in position order. Empty replays nothing. */
|
|
255
|
+
entries?: readonly WorkflowJournalEntry[];
|
|
256
|
+
/** Called as each call of *this* run settles, so it can be resumed in turn. */
|
|
257
|
+
append?(entry: WorkflowJournalEntry): void;
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
export interface WorkflowRunResult {
|
|
262
|
+
status: "completed" | "failed" | "killed";
|
|
263
|
+
meta: WorkflowMeta;
|
|
264
|
+
/** The script's return value, JSON-checked at the boundary. */
|
|
265
|
+
value?: unknown;
|
|
266
|
+
error?: string;
|
|
267
|
+
/** The append-only log, in emission order. */
|
|
268
|
+
progress: WorkflowEntry[];
|
|
269
|
+
/** Agents scheduled, including those that failed. */
|
|
270
|
+
agentCount: number;
|
|
271
|
+
/** How many of those came back from the journal instead of being spawned. */
|
|
272
|
+
replayedCount: number;
|
|
273
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// Adapted from @tintinweb/pi-subagents 0.23.0 src/workflow/saved.ts (MIT).
|
|
2
|
+
// Resource roots, trust and package declarations use the suite's common resource loader.
|
|
3
|
+
import { readFileSync, readdirSync, statSync } from 'node:fs';
|
|
4
|
+
import { basename, join, resolve } from 'node:path';
|
|
5
|
+
import { getAgentDir } from '@earendil-works/pi-coding-agent';
|
|
6
|
+
import { packageResources, type PackageGate } from '../resources/packages.ts';
|
|
7
|
+
import { hasMetaDeclaration } from './meta.ts';
|
|
8
|
+
import { MAX_SCRIPT_LENGTH, type WorkflowScriptSource } from './runtime/types.ts';
|
|
9
|
+
|
|
10
|
+
export type WorkflowResources = { trusted: boolean; packageGate?: PackageGate; agentDir?: string };
|
|
11
|
+
export function savedWorkflowRoots(cwd: string, options: WorkflowResources): string[] {
|
|
12
|
+
return [...options.trusted ? [join(cwd, '.pi', 'workflows'), join(cwd, '.agents', 'workflows')] : [],
|
|
13
|
+
join(options.agentDir ?? getAgentDir(), 'workflows')];
|
|
14
|
+
}
|
|
15
|
+
const usableName = (name: string) => name.length <= 128 && /^[a-zA-Z0-9][a-zA-Z0-9._-]*$/.test(name);
|
|
16
|
+
|
|
17
|
+
function readScript(path: string): string {
|
|
18
|
+
const stat = statSync(path);
|
|
19
|
+
if (!stat.isFile() || stat.size > MAX_SCRIPT_LENGTH) throw new Error(`Cannot use ${path} as a workflow script. Choose a regular file of at most ${MAX_SCRIPT_LENGTH} bytes.`);
|
|
20
|
+
return readFileSync(path, 'utf8');
|
|
21
|
+
}
|
|
22
|
+
function packageScripts(cwd: string, options: WorkflowResources) {
|
|
23
|
+
return packageResources(cwd, 'workflows', options.trusted, options.packageGate, options.agentDir);
|
|
24
|
+
}
|
|
25
|
+
/** Listings filter ordinary JS files, while named lookup reports an invalid shadowing file. */
|
|
26
|
+
export function listSavedWorkflows(cwd: string, options: WorkflowResources): string[] {
|
|
27
|
+
const names = new Set<string>();
|
|
28
|
+
const paths: string[] = packageScripts(cwd, options).map(file => file.path);
|
|
29
|
+
for (const root of savedWorkflowRoots(cwd, options)) {
|
|
30
|
+
let entries: string[];
|
|
31
|
+
try { entries = readdirSync(root); }
|
|
32
|
+
catch (error) { if ((error as NodeJS.ErrnoException).code === 'ENOENT') continue; throw error; }
|
|
33
|
+
paths.push(...entries.filter(name => name.endsWith('.js')).map(name => join(root, name)));
|
|
34
|
+
}
|
|
35
|
+
for (const path of paths) {
|
|
36
|
+
const name = basename(path, '.js');
|
|
37
|
+
const stat = statSync(path);
|
|
38
|
+
if (usableName(name) && stat.isFile() && stat.size <= MAX_SCRIPT_LENGTH && hasMetaDeclaration(readScript(path))) names.add(name);
|
|
39
|
+
}
|
|
40
|
+
return [...names].sort();
|
|
41
|
+
}
|
|
42
|
+
export function readSavedWorkflow(name: string, cwd: string, options: WorkflowResources): WorkflowScriptSource {
|
|
43
|
+
const trimmed = name.trim();
|
|
44
|
+
if (!usableName(trimmed)) return {ok: false, message: `Invalid workflow name "${name}"; use letters, digits, dots, hyphens and underscores. Use scriptPath for a path.`};
|
|
45
|
+
try {
|
|
46
|
+
for (const root of savedWorkflowRoots(cwd, options)) {
|
|
47
|
+
const path = join(root, `${trimmed}.js`);
|
|
48
|
+
let script: string;
|
|
49
|
+
try { script = readScript(path); }
|
|
50
|
+
catch (error) { if ((error as NodeJS.ErrnoException).code === 'ENOENT') continue; throw error; }
|
|
51
|
+
return hasMetaDeclaration(script) ? {ok: true, script, path} : {ok: false, message: `${path} has no export const meta declaration. Nothing was run.`};
|
|
52
|
+
}
|
|
53
|
+
const file = packageScripts(cwd, options).find(file => basename(file.path, '.js') === trimmed);
|
|
54
|
+
if (file) {
|
|
55
|
+
const script = readScript(file.path);
|
|
56
|
+
return hasMetaDeclaration(script) ? {ok: true, script, path: file.path} : {ok: false, message: `${file.path} has no export const meta declaration. Nothing was run.`};
|
|
57
|
+
}
|
|
58
|
+
return {ok: false, message: `No saved workflow "${trimmed}". Roots: ${savedWorkflowRoots(cwd, options).join(', ')}. Available: ${listSavedWorkflows(cwd, options).join(', ') || 'none'}.`};
|
|
59
|
+
} catch (error) { return {ok: false, message: error instanceof Error ? error.message : String(error)}; }
|
|
60
|
+
}
|
|
61
|
+
/** The same mutually exclusive reference contract serves root and nested workflows. */
|
|
62
|
+
export function resolveWorkflowSource(ref: {name?: string; scriptPath?: string}, cwd: string, options: WorkflowResources): WorkflowScriptSource {
|
|
63
|
+
if ([ref.name, ref.scriptPath].filter(value => value !== undefined).length !== 1) return {ok: false, message: 'Provide exactly one of name or scriptPath.'};
|
|
64
|
+
if (ref.name !== undefined) return readSavedWorkflow(ref.name, cwd, options);
|
|
65
|
+
if (!ref.scriptPath?.trim()) return {ok: false, message: 'scriptPath must be non-empty.'};
|
|
66
|
+
const path = resolve(cwd, ref.scriptPath);
|
|
67
|
+
try { return {ok: true, script: readScript(path), path}; }
|
|
68
|
+
catch (error) { return {ok: false, message: `Could not read workflow ${path}: ${String(error)}`}; }
|
|
69
|
+
}
|
|
70
|
+
export function resolveWorkflowScript(params: {script?: string; scriptPath?: string; name?: string}, cwd: string, options: WorkflowResources): WorkflowScriptSource {
|
|
71
|
+
if ([params.script, params.scriptPath, params.name].filter(value => value !== undefined).length !== 1) return {ok: false, message: 'Provide exactly one of script, scriptPath or name.'};
|
|
72
|
+
if (params.script !== undefined) return params.script.trim() ? {ok: true, script: params.script} : {ok: false, message: 'script must be non-empty.'};
|
|
73
|
+
return resolveWorkflowSource(params, cwd, options);
|
|
74
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { dirname, join } from 'node:path';
|
|
3
|
+
import { getAgentDir } from '@earendil-works/pi-coding-agent';
|
|
4
|
+
import { JsonStore } from '../../runtime/json-store.ts';
|
|
5
|
+
import { decodeWorkflowEntry, type WorkflowEntryData } from './entry.ts';
|
|
6
|
+
|
|
7
|
+
/** Each run owns a script copy and journal; snapshot updates use the shared atomic file protocol. */
|
|
8
|
+
export function createWorkflowFiles(ownerSessionId: string, id: string, script: string, agentDir = getAgentDir()) {
|
|
9
|
+
const directory = join(agentDir, 'pi-suite', 'agents', 'workflows', ownerSessionId, id);
|
|
10
|
+
mkdirSync(dirname(directory), {recursive: true, mode: 0o700});
|
|
11
|
+
mkdirSync(directory, {mode: 0o700});
|
|
12
|
+
const scriptPath = join(directory, 'script.js'), journalPath = join(directory, 'journal.jsonl');
|
|
13
|
+
writeFileSync(scriptPath, script, {flag: 'wx', mode: 0o600});
|
|
14
|
+
writeFileSync(journalPath, '', {flag: 'wx', mode: 0o600});
|
|
15
|
+
return {scriptPath, journalPath, recordPath: join(directory, 'run.json'), resultPath: join(directory, 'result.txt')};
|
|
16
|
+
}
|
|
17
|
+
export class WorkflowStore extends JsonStore<WorkflowEntryData> {
|
|
18
|
+
constructor(initial: WorkflowEntryData) { super(initial.recordPath, initial, decodeWorkflowEntry, 'WORKFLOW'); }
|
|
19
|
+
save(data: WorkflowEntryData): Promise<void> { return this.mutate(current => { Object.assign(current, data); }); }
|
|
20
|
+
}
|
|
21
|
+
export function readWorkflowSnapshot(path: string): WorkflowEntryData {
|
|
22
|
+
return decodeWorkflowEntry(JSON.parse(readFileSync(path, 'utf8')));
|
|
23
|
+
}
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
// Imported/adapted from @tintinweb/pi-subagents 0.23.0 src/workflow/task.ts (MIT).
|
|
2
|
+
/**
|
|
3
|
+
* task.ts — the background record one workflow run lives in.
|
|
4
|
+
*
|
|
5
|
+
* An `agent_workflow` tool call returns a task id immediately and the run continues
|
|
6
|
+
* without it, so the run's state cannot live in the tool call's closure: the
|
|
7
|
+
* inline card, the completion notification and the `/subagents` Workflows page
|
|
8
|
+
* all read it after `execute` has returned. Field names keep the upstream shape.
|
|
9
|
+
*
|
|
10
|
+
* The progress log is append-only and collapses by index (see `progress/model.ts`),
|
|
11
|
+
* so every derived counter here is recomputed from the log rather than
|
|
12
|
+
* incremented as entries arrive — a re-emitted agent entry replaces its
|
|
13
|
+
* predecessor, and adding its tokens on top would double-count them.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { randomUUID } from "node:crypto";
|
|
17
|
+
|
|
18
|
+
import type { WorkflowJournalEntry } from "./journal.ts";
|
|
19
|
+
import type { WorkflowMeta } from "./meta.ts";
|
|
20
|
+
import { collapse, elapsedMs, stats, type WorkflowEntry, type WorkflowRunStatus } from "./progress/index.ts";
|
|
21
|
+
import type { WorkflowControl, WorkflowRunResult } from "./runtime/index.ts";
|
|
22
|
+
|
|
23
|
+
/** `wf_` + hex, matching Claude Code's `^wf_[a-z0-9-]{6,}$` run ids. */
|
|
24
|
+
export function workflowRunId(): string {
|
|
25
|
+
return `wf_${randomUUID().replace(/-/g, "").slice(0, 12)}`;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface WorkflowTask {
|
|
29
|
+
/** Fixed discriminator kept from the upstream task shape. */
|
|
30
|
+
type: "local_workflow";
|
|
31
|
+
id: string;
|
|
32
|
+
status: WorkflowRunStatus;
|
|
33
|
+
script: string;
|
|
34
|
+
/** Where the script can be edited and re-run from. */
|
|
35
|
+
scriptPath?: string;
|
|
36
|
+
args?: unknown;
|
|
37
|
+
meta?: WorkflowMeta;
|
|
38
|
+
workflowName?: string;
|
|
39
|
+
/** The `tool_use_id` of the call that started this, when one did. */
|
|
40
|
+
toolCallId?: string;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Pause, skip and retry, once the run is up.
|
|
44
|
+
*
|
|
45
|
+
* Absent before the runtime hands it over and after the run settles — the
|
|
46
|
+
* dialog treats "no control" as "those keys do nothing", which is the same
|
|
47
|
+
* thing it does for a run that has finished.
|
|
48
|
+
*/
|
|
49
|
+
control?: WorkflowControl;
|
|
50
|
+
/** When the current pause started, so `totalPausedMs` can be closed out. */
|
|
51
|
+
pausedAt?: number;
|
|
52
|
+
|
|
53
|
+
/** Where this run records its own settled calls, for a later resume. */
|
|
54
|
+
journalPath?: string;
|
|
55
|
+
/** A previous run's journal, when this call asked to resume one. */
|
|
56
|
+
replay?: readonly WorkflowJournalEntry[];
|
|
57
|
+
/** The run id this one resumed, for the result line that says so. */
|
|
58
|
+
resumedFrom?: string;
|
|
59
|
+
/** How many agents came back from {@link replay} instead of being spawned. */
|
|
60
|
+
replayedCount: number;
|
|
61
|
+
|
|
62
|
+
/** The append-only event log, in emission order. */
|
|
63
|
+
workflowProgress: WorkflowEntry[];
|
|
64
|
+
/** Bumped once per applied batch, so a renderer can tell nothing changed. */
|
|
65
|
+
progressVersion: number;
|
|
66
|
+
agentCount: number;
|
|
67
|
+
/**
|
|
68
|
+
* Agents that have settled successfully, recomputed with the other counters.
|
|
69
|
+
*
|
|
70
|
+
* Cached rather than derived on read because the fleet list asks five times a
|
|
71
|
+
* second: deriving it there would walk the whole append-only log on every
|
|
72
|
+
* tick, which for a thousand-agent run is real work in the render loop.
|
|
73
|
+
*/
|
|
74
|
+
doneCount: number;
|
|
75
|
+
totalTokens: number;
|
|
76
|
+
totalToolCalls: number;
|
|
77
|
+
logs: string[];
|
|
78
|
+
|
|
79
|
+
abortController: AbortController;
|
|
80
|
+
startTime: number;
|
|
81
|
+
endTime?: number;
|
|
82
|
+
/** Excluded from the elapsed clock the header shows. */
|
|
83
|
+
totalPausedMs: number;
|
|
84
|
+
|
|
85
|
+
/** The script's return value, once the run produced one. */
|
|
86
|
+
value?: unknown;
|
|
87
|
+
error?: string;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function createWorkflowTask(init: {
|
|
91
|
+
id: string;
|
|
92
|
+
script: string;
|
|
93
|
+
scriptPath?: string;
|
|
94
|
+
args?: unknown;
|
|
95
|
+
meta?: WorkflowMeta;
|
|
96
|
+
toolCallId?: string;
|
|
97
|
+
startTime?: number;
|
|
98
|
+
journalPath?: string;
|
|
99
|
+
replay?: readonly WorkflowJournalEntry[];
|
|
100
|
+
resumedFrom?: string;
|
|
101
|
+
}): WorkflowTask {
|
|
102
|
+
return {
|
|
103
|
+
type: "local_workflow",
|
|
104
|
+
id: init.id,
|
|
105
|
+
status: "running",
|
|
106
|
+
script: init.script,
|
|
107
|
+
scriptPath: init.scriptPath,
|
|
108
|
+
args: init.args,
|
|
109
|
+
meta: init.meta,
|
|
110
|
+
workflowName: init.meta?.name,
|
|
111
|
+
toolCallId: init.toolCallId,
|
|
112
|
+
journalPath: init.journalPath,
|
|
113
|
+
replay: init.replay,
|
|
114
|
+
resumedFrom: init.resumedFrom,
|
|
115
|
+
replayedCount: 0,
|
|
116
|
+
workflowProgress: [],
|
|
117
|
+
progressVersion: 0,
|
|
118
|
+
agentCount: 0,
|
|
119
|
+
doneCount: 0,
|
|
120
|
+
totalTokens: 0,
|
|
121
|
+
totalToolCalls: 0,
|
|
122
|
+
logs: [],
|
|
123
|
+
abortController: new AbortController(),
|
|
124
|
+
startTime: init.startTime ?? Date.now(),
|
|
125
|
+
totalPausedMs: 0,
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Apply one batch of progress entries.
|
|
131
|
+
*
|
|
132
|
+
* Batched rather than per-entry because that is how the worker emits them, and
|
|
133
|
+
* because every counter below scans the event log — doing it once per fan-out
|
|
134
|
+
* frame instead of once per agent is the difference that keeps a 200-agent run
|
|
135
|
+
* cheap to render.
|
|
136
|
+
*/
|
|
137
|
+
export function updateWorkflowProgressBatch(
|
|
138
|
+
task: WorkflowTask,
|
|
139
|
+
entries: readonly WorkflowEntry[],
|
|
140
|
+
): void {
|
|
141
|
+
if (entries.length === 0) return;
|
|
142
|
+
task.workflowProgress.push(...entries);
|
|
143
|
+
task.progressVersion++;
|
|
144
|
+
|
|
145
|
+
const { agents, logs } = collapse(task.workflowProgress);
|
|
146
|
+
task.logs = logs;
|
|
147
|
+
// `agentCount` is what the runtime has scheduled, which can lead what the log
|
|
148
|
+
// has seen — never let a recompute walk it backwards.
|
|
149
|
+
task.agentCount = Math.max(task.agentCount, agents.length);
|
|
150
|
+
|
|
151
|
+
let totalTokens = 0;
|
|
152
|
+
let totalToolCalls = 0;
|
|
153
|
+
let done = 0;
|
|
154
|
+
for (const agent of agents) {
|
|
155
|
+
totalTokens += agent.tokens ?? 0;
|
|
156
|
+
totalToolCalls += agent.toolCalls ?? 0;
|
|
157
|
+
// Counted off the collapsed agents, so a re-emitted row counts once.
|
|
158
|
+
if (agent.state === "done") done++;
|
|
159
|
+
}
|
|
160
|
+
task.totalTokens = totalTokens;
|
|
161
|
+
task.totalToolCalls = totalToolCalls;
|
|
162
|
+
task.doneCount = done;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Hold the run, and stop its clock.
|
|
167
|
+
*
|
|
168
|
+
* The elapsed figure every surface shows subtracts `totalPausedMs`, so a run
|
|
169
|
+
* left paused overnight does not come back reading as a twelve-hour run.
|
|
170
|
+
*/
|
|
171
|
+
export function pauseWorkflowTask(task: WorkflowTask, now = Date.now()): boolean {
|
|
172
|
+
if (task.status !== "running" || task.control === undefined) return false;
|
|
173
|
+
task.control.pause();
|
|
174
|
+
task.status = "paused";
|
|
175
|
+
task.pausedAt = now;
|
|
176
|
+
return true;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** Let it go again, banking however long it was held. */
|
|
180
|
+
export function resumeWorkflowTask(task: WorkflowTask, now = Date.now()): boolean {
|
|
181
|
+
if (task.status !== "paused" || task.control === undefined) return false;
|
|
182
|
+
task.control.resume();
|
|
183
|
+
task.status = "running";
|
|
184
|
+
task.totalPausedMs += Math.max(0, now - task.pausedAt!);
|
|
185
|
+
task.pausedAt = undefined;
|
|
186
|
+
return true;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** Settle a task from the run's own result. */
|
|
190
|
+
export function completeWorkflowTask(task: WorkflowTask, result: WorkflowRunResult): void {
|
|
191
|
+
// Banked before the status moves off "paused": a run that finished while held
|
|
192
|
+
// still spent that time held, and the elapsed figure has to say so.
|
|
193
|
+
if (task.pausedAt !== undefined) {
|
|
194
|
+
task.totalPausedMs += Math.max(0, Date.now() - task.pausedAt);
|
|
195
|
+
task.pausedAt = undefined;
|
|
196
|
+
}
|
|
197
|
+
// Nothing left to control, and holding the handle would let the dialog offer
|
|
198
|
+
// pause on a run that has already stopped.
|
|
199
|
+
task.control = undefined;
|
|
200
|
+
task.status = result.status;
|
|
201
|
+
task.meta ??= result.meta;
|
|
202
|
+
task.workflowName ??= result.meta.name;
|
|
203
|
+
task.agentCount = Math.max(task.agentCount, result.agentCount);
|
|
204
|
+
task.replayedCount = result.replayedCount;
|
|
205
|
+
task.value = result.value;
|
|
206
|
+
task.error = result.error;
|
|
207
|
+
task.endTime = Date.now();
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Settle a task that never produced a result — a script rejected before the
|
|
212
|
+
* worker started (bad `meta`, oversized source, non-JSON `args`), or a restored
|
|
213
|
+
* running snapshot with no live execution owner.
|
|
214
|
+
*/
|
|
215
|
+
export function failWorkflowTask(task: WorkflowTask, error: string): void {
|
|
216
|
+
task.control = undefined;
|
|
217
|
+
task.pausedAt = undefined;
|
|
218
|
+
task.status = "failed";
|
|
219
|
+
task.error = error;
|
|
220
|
+
task.endTime = Date.now();
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** The run's outcome as text, for the notification and the LLM-facing result. */
|
|
224
|
+
export function workflowResultText(task: WorkflowTask): string {
|
|
225
|
+
if (task.error !== undefined) return task.error;
|
|
226
|
+
if (task.value === undefined) return "No output.";
|
|
227
|
+
if (typeof task.value === "string") return task.value;
|
|
228
|
+
return JSON.stringify(task.value, null, 2);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Resolve a `resumeFromRunId` against the runs this session has seen.
|
|
233
|
+
*
|
|
234
|
+
* Same-session only, and deliberately so: the journal lives beside the
|
|
235
|
+
* session's task files, and a run id from another session would silently find
|
|
236
|
+
* nothing to replay — reporting that as "resumed" would be a lie the caller
|
|
237
|
+
* could not see through. An unknown id is an error rather than a cold start,
|
|
238
|
+
* because a caller that asked to resume is expecting not to pay.
|
|
239
|
+
* Post-identity checks in `WorkflowManager.start` refine this to current branch.
|
|
240
|
+
*/
|
|
241
|
+
export function resolveResumeTarget(
|
|
242
|
+
runId: string | undefined,
|
|
243
|
+
tasks: ReadonlyMap<string, WorkflowTask>,
|
|
244
|
+
):
|
|
245
|
+
| undefined
|
|
246
|
+
| { ok: true; runId: string; journalPath: string; scriptPath: string }
|
|
247
|
+
| { ok: false; message: string } {
|
|
248
|
+
const id = runId?.trim();
|
|
249
|
+
if (id === undefined || id === "") return undefined;
|
|
250
|
+
|
|
251
|
+
const prior = tasks.get(id);
|
|
252
|
+
if (prior === undefined) {
|
|
253
|
+
const known = [...tasks.keys()];
|
|
254
|
+
return {
|
|
255
|
+
ok: false,
|
|
256
|
+
message:
|
|
257
|
+
`No workflow run "${id}". Use a finished run from this branch or omit resumeFromRunId.` +
|
|
258
|
+
(known.length > 0 ? ` Known IDs: ${known.join(", ")}.` : ""),
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
if (prior.status === "running" || prior.status === "paused") {
|
|
262
|
+
return {
|
|
263
|
+
ok: false,
|
|
264
|
+
message: `Workflow "${id}" is ${prior.status}. Wait for it to finish or stop it in /subagents Workflows before replaying.`,
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
if (prior.journalPath === undefined) {
|
|
268
|
+
return { ok: false, message: `Workflow "${id}" has no journal. Choose a run with a journal or omit resumeFromRunId.` };
|
|
269
|
+
}
|
|
270
|
+
return {
|
|
271
|
+
ok: true,
|
|
272
|
+
runId: id,
|
|
273
|
+
journalPath: prior.journalPath,
|
|
274
|
+
// Use the saved copy, independent of how the caller supplied the script.
|
|
275
|
+
scriptPath: prior.scriptPath ?? "",
|
|
276
|
+
};
|
|
277
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// Imported/adapted from @tintinweb/pi-subagents 0.23.0 src/workflow/tool-description.ts (MIT).
|
|
2
|
+
/**
|
|
3
|
+
* tool-description.ts — the model-facing description of the `agent_workflow` tool.
|
|
4
|
+
*
|
|
5
|
+
* The wording follows Claude Code's `Workflow` tool description; deviate only where
|
|
6
|
+
* its sentence would be false about pi. The port rules, omissions and deviations are
|
|
7
|
+
* recorded beside the full guide in skills/pi-suite-workflow/SKILL.md.
|
|
8
|
+
*
|
|
9
|
+
* Why short: the tool sits in every provider request while agents.workflows is on,
|
|
10
|
+
* but it may only be called after an explicit user opt-in. The description therefore
|
|
11
|
+
* keeps what the model needs to decide whether and how to call it (precondition,
|
|
12
|
+
* parameters, background result); the script format, hooks, limits and patterns live
|
|
13
|
+
* in the skill, which the model reads only when it actually writes a script. Keep each
|
|
14
|
+
* passage in exactly one of the two places.
|
|
15
|
+
*
|
|
16
|
+
* `{{typeList}}` is replaced with the enabled agent type names when the tool binds to
|
|
17
|
+
* the current session. Names only: the built-in type descriptions alone exceed this
|
|
18
|
+
* whole description, and agent_start likewise lists names without descriptions.
|
|
19
|
+
*/
|
|
20
|
+
export const workflowToolDescription = `Run a JavaScript workflow to coordinate subagents.
|
|
21
|
+
|
|
22
|
+
Call only if the user requests a workflow or orchestration, invokes a skill or command that requires this tool, or requests a saved workflow. Otherwise use agent_start, or explain the plan and rough cost and ask for approval.
|
|
23
|
+
|
|
24
|
+
Read the pi-suite-workflow skill before you write or edit a workflow script.
|
|
25
|
+
|
|
26
|
+
- Choose one source: script (inline JavaScript with \`export const meta = {...}\`), scriptPath (relative to the working directory or absolute), or name (saved workflow).
|
|
27
|
+
- args: optional JSON value. Pass arrays and objects directly.
|
|
28
|
+
- resumeFromRunId: optional finished wf_ run on the current session branch. Reuse its unchanged, successful agent() prefix.
|
|
29
|
+
|
|
30
|
+
Starts in the background. Returns a wf_ ID and resultPath; details include the saved scriptPath. On completion, use read on resultPath for the full result.
|
|
31
|
+
|
|
32
|
+
Enabled agentType names: {{typeList}}`;
|