@timqi/pier 0.1.3 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -7
- package/dist/agent/config.js +9 -3
- package/dist/agent/events.js +13 -5
- package/dist/agent/listing.js +10 -2
- package/dist/agent/packages.js +4 -20
- package/dist/agent/pi.js +18 -86
- package/dist/channels/attach.js +2 -2
- package/dist/channels/commands.js +10 -8
- package/dist/channels/config.js +32 -24
- package/dist/channels/control.js +60 -14
- package/dist/channels/conversations.js +60 -13
- package/dist/channels/handoff.js +90 -0
- package/dist/channels/lark-api.js +7 -0
- package/dist/channels/lark-outbound.js +19 -0
- package/dist/channels/lark-panel.js +41 -26
- package/dist/channels/lark-render.js +2 -1
- package/dist/channels/lark.js +35 -26
- package/dist/channels/lines.js +1 -1
- package/dist/channels/panel.js +324 -115
- package/dist/channels/receipts.js +25 -12
- package/dist/channels/routes.js +21 -7
- package/dist/channels/runtime.js +21 -7
- package/dist/channels/slack-api.js +19 -10
- package/dist/channels/slack-cli.js +503 -0
- package/dist/channels/slack-directory.js +2 -2
- package/dist/channels/slack-outbound.js +11 -1
- package/dist/channels/slack-panel.js +81 -40
- package/dist/channels/slack-render.js +3 -1
- package/dist/channels/slack-thread.js +41 -0
- package/dist/channels/slack-transcript.js +107 -0
- package/dist/channels/slack.js +38 -31
- package/dist/channels/types.js +1 -3
- package/dist/cli.js +166 -7
- package/dist/core/identity.js +61 -17
- package/dist/core/reply.js +19 -15
- package/dist/core/router.js +23 -3
- package/dist/db.js +33 -0
- package/dist/main.js +61 -41
- package/dist/paths.js +3 -0
- package/dist/secrets.js +2 -1
- package/dist/settings.js +3 -12
- package/dist/socket.js +99 -0
- package/dist/tasks/agent.js +5 -6
- package/dist/tasks/callbacks.js +2 -2
- package/dist/tasks/cli.js +225 -0
- package/dist/tasks/definitions.js +3 -3
- package/dist/tasks/execution.js +1 -3
- package/dist/tasks/groups.js +2 -5
- package/dist/tasks/messages.js +18 -158
- package/dist/tasks/operations.js +335 -0
- package/dist/tasks/routes.js +1 -11
- package/dist/tasks/runs.js +5 -20
- package/dist/tasks/service.js +18 -43
- package/dist/tasks/store.js +15 -15
- package/dist/tools.js +18 -3
- package/dist/vault.js +107 -0
- package/dist/web/auth.js +2 -1
- package/dist/web/public/assets/{activity-BSMeRcN2.js → activity-CrybM-E8.js} +2 -2
- package/dist/web/public/assets/activity-CrybM-E8.js.br +0 -0
- package/dist/web/public/assets/activity-CrybM-E8.js.gz +0 -0
- package/dist/web/public/assets/{boards-BCWQMZry.js → boards-Cw7_6J6L.js} +1 -1
- package/dist/web/public/assets/boards-Cw7_6J6L.js.br +0 -0
- package/dist/web/public/assets/boards-Cw7_6J6L.js.gz +0 -0
- package/dist/web/public/assets/{explorer-Cr4XTi4j.js → explorer-DV066dUD.js} +1 -1
- package/dist/web/public/assets/explorer-DV066dUD.js.br +0 -0
- package/dist/web/public/assets/explorer-DV066dUD.js.gz +0 -0
- package/dist/web/public/assets/index-BrNHu2qj.js +85 -0
- package/dist/web/public/assets/index-BrNHu2qj.js.br +0 -0
- package/dist/web/public/assets/index-BrNHu2qj.js.gz +0 -0
- package/dist/web/public/assets/index-DLszkDUV.css +2 -0
- package/dist/web/public/assets/index-DLszkDUV.css.br +0 -0
- package/dist/web/public/assets/index-DLszkDUV.css.gz +0 -0
- package/dist/web/public/assets/{runs-DdERzeac.js → runs-DDagTNaM.js} +1 -1
- package/dist/web/public/assets/runs-DDagTNaM.js.br +0 -0
- package/dist/web/public/assets/runs-DDagTNaM.js.gz +0 -0
- package/dist/web/public/assets/settings-BEdSeXpm.js +5 -0
- package/dist/web/public/assets/settings-BEdSeXpm.js.br +0 -0
- package/dist/web/public/assets/settings-BEdSeXpm.js.gz +0 -0
- package/dist/web/public/assets/{task-runs-C-dGUDsH.js → task-runs-BnMack9t.js} +1 -1
- package/dist/web/public/assets/task-runs-BnMack9t.js.br +0 -0
- package/dist/web/public/assets/task-runs-BnMack9t.js.gz +0 -0
- package/dist/web/public/assets/{tasks-CY30H1u1.js → tasks-BkW7YShZ.js} +1 -1
- package/dist/web/public/assets/tasks-BkW7YShZ.js.br +0 -0
- package/dist/web/public/assets/tasks-BkW7YShZ.js.gz +0 -0
- package/dist/web/public/index.html +2 -2
- package/dist/web/public/index.html.br +0 -0
- package/dist/web/public/index.html.gz +0 -0
- package/dist/web/push.js +4 -10
- package/dist/web/vault.js +68 -0
- package/dist/{extensions/web → websearch}/artifacts.js +2 -2
- package/dist/websearch/cli.js +73 -0
- package/dist/websearch/run.js +273 -0
- package/docs/deploy.md +34 -11
- package/package.json +2 -3
- package/skills/pier-help/SKILL.md +32 -16
- package/skills/pier-slack/SKILL.md +52 -71
- package/skills/pier-tasks/SKILL.md +60 -148
- package/skills/pier-vault/SKILL.md +36 -0
- package/skills/pier-web/SKILL.md +50 -0
- package/dist/channels/slack-tool.js +0 -416
- package/dist/channels/telegram-api.js +0 -86
- package/dist/channels/telegram-panel.js +0 -97
- package/dist/channels/telegram-render.js +0 -69
- package/dist/channels/telegram.js +0 -421
- package/dist/extensions/index.js +0 -10
- package/dist/extensions/web/index.js +0 -9
- package/dist/extensions/web/tools.js +0 -265
- package/dist/tasks/tool.js +0 -416
- package/dist/web/public/assets/activity-BSMeRcN2.js.br +0 -0
- package/dist/web/public/assets/activity-BSMeRcN2.js.gz +0 -0
- package/dist/web/public/assets/boards-BCWQMZry.js.br +0 -0
- package/dist/web/public/assets/boards-BCWQMZry.js.gz +0 -0
- package/dist/web/public/assets/explorer-Cr4XTi4j.js.br +0 -0
- package/dist/web/public/assets/explorer-Cr4XTi4j.js.gz +0 -0
- package/dist/web/public/assets/index-C7tA0Ufu.js +0 -85
- package/dist/web/public/assets/index-C7tA0Ufu.js.br +0 -0
- package/dist/web/public/assets/index-C7tA0Ufu.js.gz +0 -0
- package/dist/web/public/assets/index-DVIt5Gio.css +0 -2
- package/dist/web/public/assets/index-DVIt5Gio.css.br +0 -0
- package/dist/web/public/assets/index-DVIt5Gio.css.gz +0 -0
- package/dist/web/public/assets/runs-DdERzeac.js.br +0 -0
- package/dist/web/public/assets/runs-DdERzeac.js.gz +0 -0
- package/dist/web/public/assets/settings-CQDAHoMM.js +0 -5
- package/dist/web/public/assets/settings-CQDAHoMM.js.br +0 -0
- package/dist/web/public/assets/settings-CQDAHoMM.js.gz +0 -0
- package/dist/web/public/assets/task-runs-C-dGUDsH.js.br +0 -0
- package/dist/web/public/assets/task-runs-C-dGUDsH.js.gz +0 -0
- package/dist/web/public/assets/tasks-CY30H1u1.js.br +0 -0
- package/dist/web/public/assets/tasks-CY30H1u1.js.gz +0 -0
- /package/dist/{extensions/web → websearch}/anthropic.js +0 -0
- /package/dist/{extensions/web → websearch}/content.js +0 -0
- /package/dist/{extensions/web → websearch}/http.js +0 -0
- /package/dist/{extensions/web → websearch}/json.js +0 -0
- /package/dist/{extensions/web → websearch}/language.js +0 -0
- /package/dist/{extensions/web → websearch}/openai.js +0 -0
- /package/dist/{extensions/web → websearch}/provider.js +0 -0
package/dist/tasks/tool.js
DELETED
|
@@ -1,416 +0,0 @@
|
|
|
1
|
-
import { isAbsolute, resolve } from "node:path";
|
|
2
|
-
import { Type } from "typebox";
|
|
3
|
-
import { logger } from "../log.js";
|
|
4
|
-
import { record, requiredString } from "./definitions.js";
|
|
5
|
-
import { isTerminal } from "./types.js";
|
|
6
|
-
const log = logger("tasks");
|
|
7
|
-
// JSON-Schema enum emits ~1/3 the tokens of typebox's anyOf-of-consts.
|
|
8
|
-
const strEnum = (...values) => Type.Unsafe({ type: "string", enum: [...values] });
|
|
9
|
-
/** Absent instead of `null`: a model reads both the same way, and on a group
|
|
10
|
-
* summary the nulls are a third of the payload. The input names every field
|
|
11
|
-
* so a summary that forgot one cannot pass as "empty". */
|
|
12
|
-
const defined = (value) => Object.fromEntries(Object.entries(value).filter(([, v]) => v !== null && v !== undefined));
|
|
13
|
-
const summarize = (run, pendingDecisionId) => defined({
|
|
14
|
-
runId: run.id,
|
|
15
|
-
taskId: run.taskId,
|
|
16
|
-
taskName: run.context.definition.name,
|
|
17
|
-
state: run.state,
|
|
18
|
-
triggerSource: run.triggerSource,
|
|
19
|
-
groupId: run.groupId,
|
|
20
|
-
sessionMode: run.sessionMode,
|
|
21
|
-
targetSessionId: run.targetSessionId,
|
|
22
|
-
callbackSessionId: run.callbackSessionId,
|
|
23
|
-
callbackState: run.callbackState,
|
|
24
|
-
callbackMode: run.callbackMode ?? null,
|
|
25
|
-
pendingDecisionId,
|
|
26
|
-
depth: run.depth,
|
|
27
|
-
queuedAt: run.queuedAt,
|
|
28
|
-
startedAt: run.startedAt,
|
|
29
|
-
finishedAt: run.finishedAt,
|
|
30
|
-
result: run.result,
|
|
31
|
-
error: run.error,
|
|
32
|
-
skipReason: run.skipReason,
|
|
33
|
-
next: null,
|
|
34
|
-
});
|
|
35
|
-
/** The receipt says the callback is the whole answer, or a model that just
|
|
36
|
-
* launched work reaches for a status call. */
|
|
37
|
-
const receipt = (summary, callbackSessionId, mode, callerSessionId) => ({
|
|
38
|
-
...summary,
|
|
39
|
-
next: callbackSessionId === null
|
|
40
|
-
? "callback none: the result is not delivered to anyone"
|
|
41
|
-
: callbackSessionId !== callerSessionId
|
|
42
|
-
? `the result is delivered to session ${callbackSessionId}; this session will not receive a callback`
|
|
43
|
-
: mode === "steer"
|
|
44
|
-
? "the result interrupts your running turn as a steer message; nothing to query"
|
|
45
|
-
: "the result arrives as a callback message once your turn ends; nothing to query",
|
|
46
|
-
});
|
|
47
|
-
const SUBAGENT_REDIRECT = "subagents cannot redirect callbacks (callback_session_id)";
|
|
48
|
-
/** Who a new run's result goes to — the caller, nobody, or a named session that
|
|
49
|
-
* must exist. Shared by `run` and `resume`: a resumed run is a new run. */
|
|
50
|
-
const callbackTarget = async (input, definitions, callerSessionId) => {
|
|
51
|
-
if (input.callback_session_id === undefined)
|
|
52
|
-
return input.callback === "none" ? null : callerSessionId;
|
|
53
|
-
if (input.callback === "none")
|
|
54
|
-
throw new Error("callback none and callback_session_id conflict: pick one delivery target");
|
|
55
|
-
const target = requiredString(input.callback_session_id, "callback_session_id");
|
|
56
|
-
if (!(await definitions.sessionExists(target)))
|
|
57
|
-
throw new Error(`unknown session: ${target}`);
|
|
58
|
-
return target;
|
|
59
|
-
};
|
|
60
|
-
/** A group echoes many results at once, so each is capped; a single-run
|
|
61
|
-
* `recover` stays whole — it is the escape hatch every truncation note points at. */
|
|
62
|
-
const trimResult = (summary) => {
|
|
63
|
-
if (summary.result?.type !== "agent" || summary.result.text.length <= 2000)
|
|
64
|
-
return summary;
|
|
65
|
-
return {
|
|
66
|
-
...summary,
|
|
67
|
-
result: {
|
|
68
|
-
...summary.result,
|
|
69
|
-
text: `${summary.result.text.slice(0, 2000)}\n[truncated — recover run_id ${summary.runId} with a reason for the full text]`,
|
|
70
|
-
},
|
|
71
|
-
};
|
|
72
|
-
};
|
|
73
|
-
/** Delivered, given up on and reported, or never owed (`callback:"none"`).
|
|
74
|
-
* Anything else is still on its way, and reading it here would be reading it twice. */
|
|
75
|
-
const settled = (callback) => callback.callbackState === null || callback.callbackState === "delivered" || callback.callbackState === "abandoned";
|
|
76
|
-
/** The same words for queued, running, pending and retrying: a refusal that
|
|
77
|
-
* named the state would be the status query this operation replaced. */
|
|
78
|
-
const notRecoverable = (what, callback) => {
|
|
79
|
-
throw new Error(callback.callbackSessionId === null
|
|
80
|
-
? `${what} is not recoverable yet: it was launched with callback none, so nothing will be delivered; recover reads finished results only and cannot wait for work`
|
|
81
|
-
: `${what} is not recoverable yet: wait for automatic delivery to session ${callback.callbackSessionId}; recover cannot wait for work`);
|
|
82
|
-
};
|
|
83
|
-
const summarizeGroup = (group, members, messages) => defined({
|
|
84
|
-
groupId: group.id,
|
|
85
|
-
join: group.join,
|
|
86
|
-
state: group.finishedAt ? "finished" : "running",
|
|
87
|
-
callbackState: group.callbackState,
|
|
88
|
-
callbackMode: group.callbackMode ?? null,
|
|
89
|
-
winnerRunId: group.winnerRunId,
|
|
90
|
-
members: members.map((run) => trimResult(summarize(run, messages.openDecisionId(run.id)))),
|
|
91
|
-
next: null,
|
|
92
|
-
});
|
|
93
|
-
const LaunchSchema = Type.Object({
|
|
94
|
-
model: Type.Optional(Type.Object({ provider: Type.String(), id: Type.String() })),
|
|
95
|
-
thinking: Type.Optional(Type.String()),
|
|
96
|
-
});
|
|
97
|
-
// Guidance only: runtime truth stays in parseDraft, so schema drift cannot
|
|
98
|
-
// loosen boundary validation.
|
|
99
|
-
const DraftSchema = Type.Object({
|
|
100
|
-
name: Type.Optional(Type.String({ description: "Defaults to the prompt's first line." })),
|
|
101
|
-
description: Type.Optional(Type.String()),
|
|
102
|
-
trigger: Type.Optional(Type.Union([
|
|
103
|
-
Type.Object({ type: Type.Literal("manual") }),
|
|
104
|
-
Type.Object({ type: Type.Literal("cron"), expression: Type.String(), timezone: Type.String() }),
|
|
105
|
-
Type.Object({
|
|
106
|
-
type: Type.Literal("watch"),
|
|
107
|
-
script: Type.String(),
|
|
108
|
-
cwd: Type.String(),
|
|
109
|
-
intervalSeconds: Type.Number(),
|
|
110
|
-
mode: strEnum("once", "repeat"),
|
|
111
|
-
}),
|
|
112
|
-
])),
|
|
113
|
-
action: Type.Union([
|
|
114
|
-
Type.Object({
|
|
115
|
-
type: Type.Literal("agent"),
|
|
116
|
-
session: Type.Union([
|
|
117
|
-
Type.Object({ mode: Type.Literal("fresh"), cwd: Type.Optional(Type.String({ description: "Absolute, or relative to your session's directory; omitted = your directory." })) }),
|
|
118
|
-
Type.Object({ mode: Type.Literal("reuse"), sessionId: Type.String() }),
|
|
119
|
-
]),
|
|
120
|
-
prompt: Type.String(),
|
|
121
|
-
launch: Type.Optional(LaunchSchema),
|
|
122
|
-
}),
|
|
123
|
-
Type.Object({ type: Type.Literal("bash"), script: Type.String(), cwd: Type.String() }),
|
|
124
|
-
Type.Object({ type: Type.Literal("task"), taskId: Type.String() }),
|
|
125
|
-
]),
|
|
126
|
-
callback: Type.Optional(Type.Union([
|
|
127
|
-
Type.Object({ type: Type.Literal("none") }),
|
|
128
|
-
Type.Object({ type: Type.Literal("origin") }),
|
|
129
|
-
Type.Object({ type: Type.Literal("session"), sessionId: Type.String() }),
|
|
130
|
-
])),
|
|
131
|
-
timeoutSeconds: Type.Optional(Type.Number()),
|
|
132
|
-
});
|
|
133
|
-
/** The model-facing `task` tool contract, injected into the agent seam as data. */
|
|
134
|
-
export function taskToolSpec(execute) {
|
|
135
|
-
return {
|
|
136
|
-
name: "task",
|
|
137
|
-
label: "Pier Task",
|
|
138
|
-
description: "Manage durable Pier tasks and subagents. Agent tasks run in a fresh session or a reused one. create files a definition the operator sees in the Console — only for schedules or roles you will run again; a one-off is run with a prompt. Run executes a stored task by task_id, a one-shot subagent from a prompt (shorthand: prompt + optional cwd/launch/name/timeoutSeconds — cwd defaults to your own directory, relative paths resolve against it, name comes from the prompt) or from a full inline task draft, or a core-joined fan-out via tasks[] with join all|first. Every operation returns immediately: results, group joins, and decision replies arrive as callback messages once your turn ends — there is no status query; pass callback 'steer' to have a result interrupt your running turn instead, or 'none' for no callback at all. recover (run_id or group_id, plus a reason) re-reads a finished result after its callback has settled — for truncated text or lost context, never to check progress. Use steer/follow_up/resume for child control and contact/reply for supervisor decisions. models lists the deployment's model menu (operator pins with intent notes, else the live catalog).",
|
|
139
|
-
parameters: Type.Object({
|
|
140
|
-
operation: strEnum("list", "create", "update", "run", "recover", "cancel", "steer", "follow_up", "resume", "contact", "reply", "models"),
|
|
141
|
-
task_id: Type.Optional(Type.String()),
|
|
142
|
-
run_id: Type.Optional(Type.String()),
|
|
143
|
-
group_id: Type.Optional(Type.String()),
|
|
144
|
-
message_id: Type.Optional(Type.String()),
|
|
145
|
-
message: Type.Optional(Type.String()),
|
|
146
|
-
reason: Type.Optional(Type.String({ description: "contact: progress | decision. recover: why the delivered callback is not enough (required)." })),
|
|
147
|
-
session_mode: Type.Optional(strEnum("fresh")),
|
|
148
|
-
prompt: Type.Optional(Type.String()),
|
|
149
|
-
cwd: Type.Optional(Type.String()),
|
|
150
|
-
launch: Type.Optional(LaunchSchema),
|
|
151
|
-
name: Type.Optional(Type.String()),
|
|
152
|
-
timeoutSeconds: Type.Optional(Type.Number({ description: "1–86400; defaults to 3600." })),
|
|
153
|
-
task: Type.Optional(DraftSchema),
|
|
154
|
-
// Spelled out, the draft schema costs more tokens per session than the
|
|
155
|
-
// rest of this contract; `parseDraft` validates either shape.
|
|
156
|
-
tasks: Type.Optional(Type.Unsafe({
|
|
157
|
-
type: "array",
|
|
158
|
-
description: "2+ entries, each a prompt string, {prompt, cwd?, launch?, name?, timeoutSeconds?}, a task draft shaped exactly like `task`, or {task_id}.",
|
|
159
|
-
items: { type: "object" },
|
|
160
|
-
})),
|
|
161
|
-
join: Type.Optional(strEnum("all", "first")),
|
|
162
|
-
input: Type.Optional(Type.Unknown()),
|
|
163
|
-
callback: Type.Optional(strEnum("origin", "none", "steer")),
|
|
164
|
-
callback_session_id: Type.Optional(Type.String()),
|
|
165
|
-
}),
|
|
166
|
-
execute,
|
|
167
|
-
};
|
|
168
|
-
}
|
|
169
|
-
export async function handleTaskTool(host, definitions, store, messages, raw, callerSessionId) {
|
|
170
|
-
const input = record(raw);
|
|
171
|
-
if (!input)
|
|
172
|
-
throw new Error("task tool parameters required");
|
|
173
|
-
const active = store.findActiveRunForTarget(callerSessionId);
|
|
174
|
-
if (input.operation === "list")
|
|
175
|
-
return definitions.list().filter((task) => task.kind !== "subagent");
|
|
176
|
-
if (input.operation === "models")
|
|
177
|
-
return host.models();
|
|
178
|
-
if (input.operation === "create") {
|
|
179
|
-
if (active)
|
|
180
|
-
throw new Error("subagents cannot create task definitions");
|
|
181
|
-
return definitions.create(await expandDraft(definitions, input.task, callerSessionId), `session:${callerSessionId}`);
|
|
182
|
-
}
|
|
183
|
-
if (input.operation === "update") {
|
|
184
|
-
if (active)
|
|
185
|
-
throw new Error("subagents cannot update task definitions");
|
|
186
|
-
return definitions.update(requiredString(input.task_id, "task_id"), await expandDraft(definitions, input.task, callerSessionId));
|
|
187
|
-
}
|
|
188
|
-
if (input.operation === "run") {
|
|
189
|
-
const callbackMode = input.callback === "steer" ? "steer" : "followUp";
|
|
190
|
-
// A run's own callback is its parent's link back; a child that could point
|
|
191
|
-
// it elsewhere would strand the supervisor waiting for a result.
|
|
192
|
-
if (active && input.callback_session_id !== undefined)
|
|
193
|
-
throw new Error(SUBAGENT_REDIRECT);
|
|
194
|
-
if (Array.isArray(input.tasks)) {
|
|
195
|
-
// Core-joined fan-out: members run detached, one aggregated callback.
|
|
196
|
-
if (input.task !== undefined || input.task_id !== undefined)
|
|
197
|
-
throw new Error("use either task/task_id or tasks[]");
|
|
198
|
-
if (input.session_mode !== undefined)
|
|
199
|
-
throw new Error("session_mode applies to a single run only");
|
|
200
|
-
if (input.callback_session_id !== undefined)
|
|
201
|
-
throw new Error("callback_session_id applies to a single run only");
|
|
202
|
-
if (input.tasks.length < 2)
|
|
203
|
-
throw new Error("tasks[] needs at least 2 entries; use task for a single run");
|
|
204
|
-
const resolved = [];
|
|
205
|
-
for (const rawEntry of input.tasks) {
|
|
206
|
-
const entry = typeof rawEntry === "string" ? { prompt: rawEntry } : record(rawEntry);
|
|
207
|
-
if (!entry)
|
|
208
|
-
throw new Error("invalid tasks[] entry");
|
|
209
|
-
resolved.push(entry.task_id === undefined
|
|
210
|
-
? await resolveDraft(definitions, entry, active, callerSessionId)
|
|
211
|
-
: resolveStored(definitions, entry.task_id, active));
|
|
212
|
-
}
|
|
213
|
-
const groupCallbackSessionId = input.callback === "none" ? null : callerSessionId;
|
|
214
|
-
const { group, runs } = host.runGroup(resolved, input.join === "first" ? "first" : "all", callerSessionId, active?.id ?? null, groupCallbackSessionId, callbackMode);
|
|
215
|
-
return receipt(summarizeGroup(group, runs, messages), groupCallbackSessionId, callbackMode, callerSessionId);
|
|
216
|
-
}
|
|
217
|
-
const draft = input.task_id === undefined ? inlineDraft(input) : undefined;
|
|
218
|
-
const task = draft
|
|
219
|
-
? await resolveDraft(definitions, draft, active, callerSessionId)
|
|
220
|
-
: resolveStored(definitions, input.task_id, active);
|
|
221
|
-
// Same as the HTTP route: a named mode the schema no longer offers is
|
|
222
|
-
// answered, not quietly swapped for the definition's own policy.
|
|
223
|
-
if (input.session_mode !== undefined && input.session_mode !== "fresh") {
|
|
224
|
-
throw new Error(`unsupported session_mode: ${String(input.session_mode)}`);
|
|
225
|
-
}
|
|
226
|
-
const sessionMode = input.session_mode;
|
|
227
|
-
const callbackSessionId = await callbackTarget(input, definitions, callerSessionId);
|
|
228
|
-
const run = host.run(task.id, input.input, "agent", active?.id ?? null, {
|
|
229
|
-
invokedBySessionId: callerSessionId,
|
|
230
|
-
sourceSessionId: callerSessionId,
|
|
231
|
-
callbackSessionId,
|
|
232
|
-
callbackMode,
|
|
233
|
-
background: true,
|
|
234
|
-
sessionMode,
|
|
235
|
-
});
|
|
236
|
-
return receipt(summarize(run, null), callbackSessionId, callbackMode, callerSessionId);
|
|
237
|
-
}
|
|
238
|
-
if (input.operation === "recover") {
|
|
239
|
-
// History only, never status: readable once the callback has said its last
|
|
240
|
-
// word. The required reason is the friction, and the operator sees it.
|
|
241
|
-
const reason = requiredString(input.reason, "reason");
|
|
242
|
-
// The open question is the notification; the reply's continuation reports.
|
|
243
|
-
const decisionOpen = (run) => {
|
|
244
|
-
throw new Error(`run ${run.id} finished awaiting your decision ${messages.openDecisionId(run.id) ?? ""}; reply to it — the continuation's callback brings the result`);
|
|
245
|
-
};
|
|
246
|
-
// A race winner need not wait for losing members to finish cancelling.
|
|
247
|
-
const groupReady = (group) => {
|
|
248
|
-
if (!group.finishedAt || !settled(group))
|
|
249
|
-
notRecoverable(`group ${group.id}`, group);
|
|
250
|
-
};
|
|
251
|
-
if (typeof input.group_id === "string") {
|
|
252
|
-
const { group, members } = host.getGroup(input.group_id);
|
|
253
|
-
groupReady(group);
|
|
254
|
-
if (!members.every((run) => isTerminal(run.state))) {
|
|
255
|
-
throw new Error(`recover cannot inspect active members; the race has settled — recover its winning result with run_id ${group.winnerRunId ?? "from the callback"} and a reason`);
|
|
256
|
-
}
|
|
257
|
-
const asking = members.find((run) => messages.openDecisionId(run.id));
|
|
258
|
-
if (asking)
|
|
259
|
-
decisionOpen(asking);
|
|
260
|
-
log.info(`recover group ${group.id} by ${callerSessionId}: ${reason}`);
|
|
261
|
-
return summarizeGroup(group, members, messages);
|
|
262
|
-
}
|
|
263
|
-
const run = host.getRun(requiredString(input.run_id, "run_id"));
|
|
264
|
-
if (run.groupId) {
|
|
265
|
-
const { group } = host.getGroup(run.groupId);
|
|
266
|
-
groupReady(group);
|
|
267
|
-
if (!isTerminal(run.state))
|
|
268
|
-
throw new Error("the group has reported its outcome; recover reads finished results only and cannot wait for this member");
|
|
269
|
-
}
|
|
270
|
-
else if (!isTerminal(run.state) || !settled(run)) {
|
|
271
|
-
notRecoverable(`run ${run.id}`, run);
|
|
272
|
-
}
|
|
273
|
-
if (messages.openDecisionId(run.id))
|
|
274
|
-
decisionOpen(run);
|
|
275
|
-
log.info(`recover run ${run.id} by ${callerSessionId}: ${reason}`);
|
|
276
|
-
return summarize(run, null);
|
|
277
|
-
}
|
|
278
|
-
if (input.operation === "cancel") {
|
|
279
|
-
if (typeof input.group_id === "string") {
|
|
280
|
-
const { group, members } = host.getGroup(input.group_id);
|
|
281
|
-
for (const member of members)
|
|
282
|
-
assertOwns(store, callerSessionId, active, member);
|
|
283
|
-
const cancelled = host.cancelGroup(group.id);
|
|
284
|
-
return summarizeGroup(cancelled, cancelled.memberRunIds.map((id) => host.getRun(id)), messages);
|
|
285
|
-
}
|
|
286
|
-
const run = host.getRun(requiredString(input.run_id, "run_id"));
|
|
287
|
-
assertOwns(store, callerSessionId, active, run);
|
|
288
|
-
const cancelled = host.cancel(run.id);
|
|
289
|
-
return summarize(cancelled, messages.openDecisionId(cancelled.id));
|
|
290
|
-
}
|
|
291
|
-
if (input.operation === "steer" || input.operation === "follow_up") {
|
|
292
|
-
const run = host.getRun(requiredString(input.run_id, "run_id"));
|
|
293
|
-
assertOwns(store, callerSessionId, active, run);
|
|
294
|
-
return host.control(run.id, callerSessionId, input.operation, requiredString(input.message, "message"));
|
|
295
|
-
}
|
|
296
|
-
if (input.operation === "resume") {
|
|
297
|
-
const prior = host.getRun(requiredString(input.run_id, "run_id"));
|
|
298
|
-
assertOwns(store, callerSessionId, active, prior);
|
|
299
|
-
// The resumed run is a new run, so it carries its own callback options,
|
|
300
|
-
// under the same rule as `run`: a subagent may not redirect them.
|
|
301
|
-
if (active && input.callback_session_id !== undefined)
|
|
302
|
-
throw new Error(SUBAGENT_REDIRECT);
|
|
303
|
-
const callbackMode = input.callback === "steer" ? "steer" : "followUp";
|
|
304
|
-
const callbackSessionId = await callbackTarget(input, definitions, callerSessionId);
|
|
305
|
-
const run = host.resume(prior.id, requiredString(input.message, "message"), {
|
|
306
|
-
invokedBySessionId: callerSessionId,
|
|
307
|
-
callbackSessionId,
|
|
308
|
-
callbackMode,
|
|
309
|
-
background: true,
|
|
310
|
-
});
|
|
311
|
-
return receipt(summarize(run, null), callbackSessionId, callbackMode, callerSessionId);
|
|
312
|
-
}
|
|
313
|
-
if (input.operation === "contact") {
|
|
314
|
-
if (!active)
|
|
315
|
-
throw new Error("contact is only available inside an active Agent run");
|
|
316
|
-
// The schema no longer narrows `reason` (recover shares the field), so the
|
|
317
|
-
// two names contact accepts are checked here.
|
|
318
|
-
const reason = input.reason === undefined ? "progress" : input.reason;
|
|
319
|
-
if (reason !== "progress" && reason !== "decision")
|
|
320
|
-
throw new Error(`contact reason must be progress or decision, got ${String(reason)}`);
|
|
321
|
-
return messages.contact(active, callerSessionId, reason, requiredString(input.message, "message"));
|
|
322
|
-
}
|
|
323
|
-
if (input.operation === "reply") {
|
|
324
|
-
return messages.reply(requiredString(input.message_id, "message_id"), callerSessionId, requiredString(input.message, "message"));
|
|
325
|
-
}
|
|
326
|
-
throw new Error("unknown task operation");
|
|
327
|
-
}
|
|
328
|
-
/** A single run's draft: the top-level shorthand (`prompt` …) or `task`, never both. */
|
|
329
|
-
function inlineDraft(input) {
|
|
330
|
-
const { prompt, cwd, launch, name, timeoutSeconds } = input;
|
|
331
|
-
if (prompt === undefined)
|
|
332
|
-
return record(input.task) ?? undefined;
|
|
333
|
-
if (input.task !== undefined)
|
|
334
|
-
throw new Error("use either prompt or task");
|
|
335
|
-
return { prompt, cwd, launch, name, timeoutSeconds };
|
|
336
|
-
}
|
|
337
|
-
/** A label for the Console, not an identifier. */
|
|
338
|
-
function nameFromPrompt(prompt) {
|
|
339
|
-
const line = prompt.split("\n")
|
|
340
|
-
.map((l) => l.replace(/^[\s#>*-]+/, "").replace(/[*_`]/g, "").replace(/\s+/g, " ").trim())
|
|
341
|
-
.find(Boolean) ?? "subagent";
|
|
342
|
-
return line.length > 60 ? `${line.slice(0, 59).trimEnd()}…` : line;
|
|
343
|
-
}
|
|
344
|
-
/** A `prompt` shorthand becomes a fresh Agent action in the caller's own
|
|
345
|
-
* directory; everything the caller did spell out passes through to parseDraft. */
|
|
346
|
-
async function expandDraft(definitions, raw, callerSessionId) {
|
|
347
|
-
let draft = record(raw);
|
|
348
|
-
if (!draft)
|
|
349
|
-
return raw;
|
|
350
|
-
if (typeof draft.prompt === "string") {
|
|
351
|
-
if (draft.action !== undefined)
|
|
352
|
-
throw new Error("use either prompt (shorthand) or action");
|
|
353
|
-
const { prompt, cwd, launch, name, ...rest } = draft;
|
|
354
|
-
draft = { ...rest, name, action: { type: "agent", session: { mode: "fresh", cwd }, prompt, launch } };
|
|
355
|
-
}
|
|
356
|
-
const action = record(draft.action);
|
|
357
|
-
const session = record(action?.session);
|
|
358
|
-
if (action?.type === "agent" && session?.mode === "fresh" && (session.cwd === undefined || (typeof session.cwd === "string" && !isAbsolute(session.cwd)))) {
|
|
359
|
-
const base = await definitions.sessionCwd(callerSessionId);
|
|
360
|
-
if (!base)
|
|
361
|
-
throw new Error(`cwd ${session.cwd === undefined ? "omitted" : `"${session.cwd}" is relative`} and the calling session has no working directory; give an absolute path`);
|
|
362
|
-
draft = { ...draft, action: { ...action, session: { ...session, cwd: resolve(base, session.cwd ?? ".") } } };
|
|
363
|
-
}
|
|
364
|
-
if (draft.name === undefined && typeof action?.prompt === "string")
|
|
365
|
-
draft = { ...draft, name: nameFromPrompt(action.prompt) };
|
|
366
|
-
return draft;
|
|
367
|
-
}
|
|
368
|
-
/** Persisted like any task (kind "subagent", filtered from default lists) so
|
|
369
|
-
* runs stay auditable and resumable. */
|
|
370
|
-
async function resolveDraft(definitions, raw, active, callerSessionId) {
|
|
371
|
-
const draft = record(await expandDraft(definitions, raw, callerSessionId));
|
|
372
|
-
if (!draft)
|
|
373
|
-
throw new Error("task definition required");
|
|
374
|
-
if (draft.trigger !== undefined && record(draft.trigger)?.type !== "manual") {
|
|
375
|
-
throw new Error("inline subagent tasks must use a manual trigger");
|
|
376
|
-
}
|
|
377
|
-
// Delivery of a one-off run is the top-level fields' business; a nested
|
|
378
|
-
// callback only means anything on a stored definition's schedule.
|
|
379
|
-
if (draft.callback !== undefined || draft.callback_session_id !== undefined) {
|
|
380
|
-
throw new Error("an inline task draft cannot set callback; use the top-level callback / callback_session_id");
|
|
381
|
-
}
|
|
382
|
-
if (active) {
|
|
383
|
-
const action = record(draft.action);
|
|
384
|
-
if (action?.type !== "agent")
|
|
385
|
-
throw new Error("subagents may only inline Agent tasks");
|
|
386
|
-
if (record(action.session)?.mode === "reuse")
|
|
387
|
-
throw new Error("subagent inline tasks cannot reuse an existing session");
|
|
388
|
-
}
|
|
389
|
-
return definitions.create({ ...draft, trigger: { type: "manual" } }, `session:${callerSessionId}`, "subagent");
|
|
390
|
-
}
|
|
391
|
-
function resolveStored(definitions, taskId, active) {
|
|
392
|
-
const task = definitions.get(requiredString(taskId, "task_id"));
|
|
393
|
-
if (active && task.action.type !== "agent")
|
|
394
|
-
throw new Error("subagents may only invoke Agent tasks");
|
|
395
|
-
return task;
|
|
396
|
-
}
|
|
397
|
-
function assertOwns(store, callerSessionId, active, target) {
|
|
398
|
-
if (active) {
|
|
399
|
-
let cursor = target;
|
|
400
|
-
while (cursor?.parentRunId) {
|
|
401
|
-
if (cursor.parentRunId === active.id)
|
|
402
|
-
return;
|
|
403
|
-
cursor = store.getRun(cursor.parentRunId);
|
|
404
|
-
}
|
|
405
|
-
throw new Error("subagent may only control descendant runs");
|
|
406
|
-
}
|
|
407
|
-
let root = target;
|
|
408
|
-
while (root.parentRunId) {
|
|
409
|
-
const parent = store.getRun(root.parentRunId);
|
|
410
|
-
if (!parent)
|
|
411
|
-
throw new Error(`unknown parent run: ${root.parentRunId}`);
|
|
412
|
-
root = parent;
|
|
413
|
-
}
|
|
414
|
-
if (root.invokedBySessionId !== callerSessionId)
|
|
415
|
-
throw new Error("session does not own this run");
|
|
416
|
-
}
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|