@ftrdotdev/pi-delegates 0.0.0-stage → 0.1.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/CHANGELOG.md +77 -0
- package/LICENSE +21 -0
- package/OPERATIONS.md +57 -0
- package/README.md +448 -2
- package/ROADMAP.md +158 -0
- package/TESTING.md +1061 -0
- package/agents.ts +8 -0
- package/examples/README.md +114 -0
- package/examples/gated.ts +28 -0
- package/index.ts +9 -0
- package/package.json +93 -4
- package/skills/pi-delegates/SKILL.md +147 -0
- package/skills/pi-delegates/references/control.md +159 -0
- package/skills/pi-delegates/references/definitions.md +163 -0
- package/skills/pi-delegates/references/gates.md +175 -0
- package/skills/pi-delegates/references/launch.md +156 -0
- package/src/domain/definitions.test.ts +41 -0
- package/src/domain/definitions.ts +161 -0
- package/src/domain/ids.ts +13 -0
- package/src/domain/names.ts +35 -0
- package/src/domain/orchestration/admission.ts +78 -0
- package/src/domain/orchestration/control.test.ts +1298 -0
- package/src/domain/orchestration/control.ts +436 -0
- package/src/domain/orchestration/fanout.test.ts +419 -0
- package/src/domain/orchestration/fanout.ts +334 -0
- package/src/domain/orchestration/gates.test.ts +686 -0
- package/src/domain/orchestration/gates.ts +360 -0
- package/src/domain/orchestration/launch.test.ts +173 -0
- package/src/domain/orchestration/launch.ts +581 -0
- package/src/domain/orchestration/requests.test.ts +700 -0
- package/src/domain/orchestration/requests.ts +769 -0
- package/src/domain/orchestration/review.test.ts +214 -0
- package/src/domain/orchestration/review.ts +168 -0
- package/src/domain/orchestration/run-wait.test.ts +262 -0
- package/src/domain/orchestration/run-wait.ts +256 -0
- package/src/domain/orchestration/workflow-section.test.ts +62 -0
- package/src/domain/orchestration/workflow-section.ts +99 -0
- package/src/domain/orchestration/workflow.test.ts +1425 -0
- package/src/domain/orchestration/workflow.ts +705 -0
- package/src/domain/run-records.test.ts +27 -0
- package/src/domain/run-records.ts +527 -0
- package/src/domain/shapes.test.ts +165 -0
- package/src/domain/skills.test.ts +466 -0
- package/src/domain/skills.ts +97 -0
- package/src/domain/tool-schema.test.ts +46 -0
- package/src/domain/tool-schema.ts +20 -0
- package/src/domain/toolkit.test.ts +201 -0
- package/src/domain/toolkit.ts +379 -0
- package/src/domain/view.test.ts +739 -0
- package/src/domain/view.ts +459 -0
- package/src/example.test.ts +15 -0
- package/src/extension/commands.test.ts +481 -0
- package/src/extension/commands.ts +227 -0
- package/src/extension/entry.test.ts +1118 -0
- package/src/extension/entry.ts +342 -0
- package/src/extension/gate-alerts.test.ts +235 -0
- package/src/extension/gate-alerts.ts +143 -0
- package/src/extension/guard.test.ts +48 -0
- package/src/extension/guard.ts +31 -0
- package/src/extension/host-signals.test.ts +20 -0
- package/src/extension/host-signals.ts +13 -0
- package/src/extension/inspector-command.test.ts +1191 -0
- package/src/extension/inspector-command.ts +638 -0
- package/src/extension/prompt.test.ts +110 -0
- package/src/extension/prompt.ts +50 -0
- package/src/extension/request-alerts.test.ts +183 -0
- package/src/extension/request-alerts.ts +181 -0
- package/src/extension/revocation.test.ts +81 -0
- package/src/extension/revocation.ts +84 -0
- package/src/extension/shortcut.test.ts +64 -0
- package/src/extension/shortcut.ts +105 -0
- package/src/extension/tools.test.ts +993 -0
- package/src/extension/tools.ts +505 -0
- package/src/extension/version.test.ts +123 -0
- package/src/extension/version.ts +97 -0
- package/src/harness-policy.test.ts +74 -0
- package/src/package-manifest.test.ts +157 -0
- package/src/presentation/actions.test.ts +221 -0
- package/src/presentation/actions.ts +85 -0
- package/src/presentation/activity.test.ts +387 -0
- package/src/presentation/activity.ts +66 -0
- package/src/presentation/close.test.ts +361 -0
- package/src/presentation/inspector.test.ts +679 -0
- package/src/presentation/inspector.ts +440 -0
- package/src/presentation/overlay.test.ts +555 -0
- package/src/presentation/overlay.ts +229 -0
- package/src/presentation/repaint.test.ts +329 -0
- package/src/presentation/shortcut.ts +2 -0
- package/src/presentation/snapshot.test.ts +530 -0
- package/src/presentation/snapshot.ts +306 -0
- package/src/presentation/strip.test.ts +196 -0
- package/src/presentation/strip.ts +74 -0
- package/src/presentation/wording.test.ts +238 -0
- package/src/presentation/wording.ts +222 -0
- package/src/runtime/bridge.test.ts +131 -0
- package/src/runtime/bridge.ts +134 -0
- package/src/runtime/config.test.ts +44 -0
- package/src/runtime/config.ts +87 -0
- package/src/runtime/context.test.ts +19 -0
- package/src/runtime/context.ts +9 -0
- package/src/runtime/execution-env.test.ts +35 -0
- package/src/runtime/execution-env.ts +15 -0
- package/src/runtime/extensions.test.ts +86 -0
- package/src/runtime/extensions.ts +140 -0
- package/src/runtime/log.test.ts +128 -0
- package/src/runtime/log.ts +68 -0
- package/src/runtime/mcp.test.ts +1453 -0
- package/src/runtime/mcp.ts +938 -0
- package/src/runtime/models.test.ts +145 -0
- package/src/runtime/models.ts +259 -0
- package/src/runtime/observe.test.ts +50 -0
- package/src/runtime/observe.ts +150 -0
- package/src/runtime/session.test.ts +359 -0
- package/src/runtime/session.ts +425 -0
- package/src/runtime/timers.test.ts +31 -0
- package/src/runtime/timers.ts +10 -0
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
import {
|
|
2
|
+
configure,
|
|
3
|
+
defineDoc,
|
|
4
|
+
type AgentChange,
|
|
5
|
+
type AgentState,
|
|
6
|
+
type ConversationId,
|
|
7
|
+
type ConversationRecord,
|
|
8
|
+
type Extension,
|
|
9
|
+
type JsonObject,
|
|
10
|
+
type TaskId,
|
|
11
|
+
type ToolRegistration,
|
|
12
|
+
type Tx,
|
|
13
|
+
} from "@earendil-works/pi-durable";
|
|
14
|
+
import { Effect, Schema } from "effect";
|
|
15
|
+
import { CODEMODE_TOOL_NAME } from "@earendil-works/pi-durable/codemode";
|
|
16
|
+
import type { Skill } from "@earendil-works/pi-coding-agent";
|
|
17
|
+
import {
|
|
18
|
+
CODEMODE_EXTENSION,
|
|
19
|
+
GATE_GUARD_EXTENSION,
|
|
20
|
+
RUNS_EXTENSION,
|
|
21
|
+
RUN_TOOL_NAMES,
|
|
22
|
+
SKILLS_EXTENSION,
|
|
23
|
+
TOOL_SEARCH_EXTENSION,
|
|
24
|
+
} from "./names.ts";
|
|
25
|
+
import type { AgentDefinition } from "./definitions.ts";
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* One gate rule. A gate parks the matching tool call before it runs, until a
|
|
29
|
+
* `delegate_control` approve or deny decides it.
|
|
30
|
+
*
|
|
31
|
+
* Rules belong to a registered definition. Presets carry none, and an inline
|
|
32
|
+
* definition cannot set one.
|
|
33
|
+
*/
|
|
34
|
+
export const ApprovalRule = Schema.Struct({
|
|
35
|
+
/**
|
|
36
|
+
* The gate name. It is unique within a run, so one decision covers this name
|
|
37
|
+
* in every child of the run. Two definitions with the same gate name share
|
|
38
|
+
* one decision.
|
|
39
|
+
*/
|
|
40
|
+
name: Schema.String,
|
|
41
|
+
/**
|
|
42
|
+
* Tool names this rule watches, for example `["bash"]`. Each name must
|
|
43
|
+
* resolve at launch. A name outside this list never parks.
|
|
44
|
+
*/
|
|
45
|
+
tools: Schema.Array(Schema.String),
|
|
46
|
+
/**
|
|
47
|
+
* Case-sensitive substring of the raw `bash` command. It applies to `bash`
|
|
48
|
+
* only: while it is set, another listed tool never matches. Omit it to park
|
|
49
|
+
* every call of a listed tool.
|
|
50
|
+
*/
|
|
51
|
+
match: Schema.optional(Schema.String),
|
|
52
|
+
});
|
|
53
|
+
export type ApprovalRule = typeof ApprovalRule.Type;
|
|
54
|
+
|
|
55
|
+
/** A `Schema.Struct`, so the type, the document encoding and the arbitrary have one source. */
|
|
56
|
+
export const ToolkitRecord = Schema.Struct({
|
|
57
|
+
definition: Schema.String,
|
|
58
|
+
/** The launch run that owns gates: this run or a fanout parent. */
|
|
59
|
+
runId: Schema.String,
|
|
60
|
+
/** The run task that owns this run's children: the task a scripted child is owned by. */
|
|
61
|
+
runTaskId: Schema.Finite,
|
|
62
|
+
/** The child run whose conversation owns this toolkit. */
|
|
63
|
+
childRunId: Schema.String,
|
|
64
|
+
approval: Schema.Struct({ gates: Schema.Array(ApprovalRule) }),
|
|
65
|
+
/** The registered definition's declared skills, which the prompt section renders. */
|
|
66
|
+
skills: Schema.Array(Schema.String),
|
|
67
|
+
});
|
|
68
|
+
export type ToolkitRecord = typeof ToolkitRecord.Type;
|
|
69
|
+
|
|
70
|
+
const VERSION_WITH_SKILLS = 2;
|
|
71
|
+
const VERSION_WITH_RUN_TASK = 3;
|
|
72
|
+
|
|
73
|
+
/** Two migrations: version 1 predates skills, versions below 3 predate the run task id. */
|
|
74
|
+
export const migrateToolkit = (
|
|
75
|
+
value: JsonObject,
|
|
76
|
+
fromVersion: number,
|
|
77
|
+
): ToolkitRecord & JsonObject =>
|
|
78
|
+
({
|
|
79
|
+
...value,
|
|
80
|
+
...(fromVersion < VERSION_WITH_SKILLS ? { skills: [] } : {}),
|
|
81
|
+
...(fromVersion < VERSION_WITH_RUN_TASK ? { runTaskId: 0 } : {}),
|
|
82
|
+
}) as unknown as ToolkitRecord & JsonObject;
|
|
83
|
+
|
|
84
|
+
export const ToolkitDoc = defineDoc<ToolkitRecord & JsonObject>({
|
|
85
|
+
kind: "delegates.toolkit",
|
|
86
|
+
version: VERSION_WITH_RUN_TASK,
|
|
87
|
+
scope: "conversation",
|
|
88
|
+
history: "latest",
|
|
89
|
+
fork: "current",
|
|
90
|
+
initial: () => ({
|
|
91
|
+
definition: "",
|
|
92
|
+
runId: "",
|
|
93
|
+
runTaskId: 0,
|
|
94
|
+
childRunId: "",
|
|
95
|
+
approval: { gates: [] },
|
|
96
|
+
skills: [],
|
|
97
|
+
}),
|
|
98
|
+
migrate: migrateToolkit,
|
|
99
|
+
checkpointWhen: () => true,
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
export class ToolkitUnresolved extends Schema.TaggedError<
|
|
103
|
+
ToolkitUnresolved,
|
|
104
|
+
{ readonly brand: unique symbol }
|
|
105
|
+
>()("ToolkitUnresolved", { message: Schema.String }) {}
|
|
106
|
+
|
|
107
|
+
export interface ToolkitResolver {
|
|
108
|
+
readonly availableExtensions: readonly string[];
|
|
109
|
+
readonly availableTools: readonly string[];
|
|
110
|
+
readonly availableSkills: readonly string[];
|
|
111
|
+
readonly extension: (name: string) => Extension | undefined;
|
|
112
|
+
readonly tool: (name: string) => ToolRegistration | undefined;
|
|
113
|
+
readonly skill: (name: string) => Skill | undefined;
|
|
114
|
+
/** The connected MCP servers, derived from the snapshot's `mcp:*` extensions. */
|
|
115
|
+
readonly mcpServers: readonly McpServerTools[];
|
|
116
|
+
/** Every MCP tool whose effective exposure is `codemode`: selecting one selects `codemode`. */
|
|
117
|
+
readonly codemodeTools: readonly string[];
|
|
118
|
+
/** Configured but not connected; `undefined` means this runtime has no MCP layer. */
|
|
119
|
+
readonly unconnected: readonly string[] | undefined;
|
|
120
|
+
/** Every available tool name a `mcp__<server>__<prefix>*` pattern selects. */
|
|
121
|
+
readonly matchTools: (pattern: string) => readonly string[];
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** One connected server as the binder sees it: its extension, namespace and tool names. */
|
|
125
|
+
export interface McpServerTools {
|
|
126
|
+
readonly extension: string;
|
|
127
|
+
readonly namespace: string;
|
|
128
|
+
readonly tools: readonly string[];
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export interface BoundToolkit {
|
|
132
|
+
/** Extensions and tools as objects, written by `configure`. */
|
|
133
|
+
readonly change: AgentChange;
|
|
134
|
+
/** What `pi.agent` records: names, not objects. */
|
|
135
|
+
readonly names: {
|
|
136
|
+
readonly extensions: readonly string[];
|
|
137
|
+
readonly tools: readonly string[];
|
|
138
|
+
readonly skills: readonly string[];
|
|
139
|
+
};
|
|
140
|
+
readonly approval: { readonly gates: readonly ApprovalRule[] };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const GATE_GUARD = GATE_GUARD_EXTENSION;
|
|
144
|
+
|
|
145
|
+
/** One sentence of a refusal message; an empty unresolved list is omitted. */
|
|
146
|
+
const sentence = (label: string, names: readonly string[]): string =>
|
|
147
|
+
names.length === 0 ? "" : `${label}: ${names.join(", ")}.`;
|
|
148
|
+
|
|
149
|
+
/** One sentence of a refusal message; an empty available list still says `(none)`. */
|
|
150
|
+
const available = (label: string, names: readonly string[]): string =>
|
|
151
|
+
`${label}: ${names.join(", ") || "(none)"}.`;
|
|
152
|
+
|
|
153
|
+
/** A pattern must name its server: `mcp__<server>__*` or `mcp__<server>__<prefix>*`. */
|
|
154
|
+
const MCP_PATTERN = /^mcp__[A-Za-z0-9_]+__[A-Za-z0-9_]*\*$/;
|
|
155
|
+
|
|
156
|
+
const namespacesOf = (resolver: ToolkitResolver): string =>
|
|
157
|
+
resolver.mcpServers.map((server) => server.namespace).join(", ") || "(none)";
|
|
158
|
+
|
|
159
|
+
/** A pattern outside the grammar is refused before anything is resolved. */
|
|
160
|
+
const grammarFailure = (pattern: string, resolver: ToolkitResolver): ToolkitUnresolved =>
|
|
161
|
+
ToolkitUnresolved.make({
|
|
162
|
+
message:
|
|
163
|
+
`Only "mcp__<server>__*" patterns are allowed: ${pattern}. ` +
|
|
164
|
+
`Available namespaces: ${namespacesOf(resolver)}.`,
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
/** A pattern that matches nothing names what this runtime has, and what is configured but down. */
|
|
168
|
+
const patternMessage = (pattern: string, resolver: ToolkitResolver): string => {
|
|
169
|
+
const server = resolver.mcpServers.find((entry) => pattern.startsWith(`${entry.namespace}__`));
|
|
170
|
+
const parts = [
|
|
171
|
+
server === undefined
|
|
172
|
+
? `No MCP server matches ${pattern}.`
|
|
173
|
+
: `No tool of ${server.namespace} matches ${pattern}.`,
|
|
174
|
+
`Available namespaces: ${namespacesOf(resolver)}.`,
|
|
175
|
+
...(server === undefined
|
|
176
|
+
? []
|
|
177
|
+
: [`The ${server.namespace} server offers: ${server.tools.join(", ") || "(none)"}.`]),
|
|
178
|
+
];
|
|
179
|
+
const unconnected = resolver.unconnected ?? [];
|
|
180
|
+
if (unconnected.length > 0) {
|
|
181
|
+
parts.push(`Configured but not connected: ${unconnected.join(", ")}.`);
|
|
182
|
+
}
|
|
183
|
+
return parts.join(" ");
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
/** Expands the declared names: a plain name stays exact, a pattern must be an MCP pattern. */
|
|
187
|
+
const expandToolNames = (
|
|
188
|
+
declared: readonly string[],
|
|
189
|
+
resolver: ToolkitResolver,
|
|
190
|
+
):
|
|
191
|
+
| { readonly kind: "ok"; readonly names: readonly string[] }
|
|
192
|
+
| { readonly kind: "failed"; readonly error: ToolkitUnresolved } => {
|
|
193
|
+
const expanded: string[] = [];
|
|
194
|
+
for (const name of declared) {
|
|
195
|
+
if (!name.includes("*")) {
|
|
196
|
+
expanded.push(name);
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
if (!MCP_PATTERN.test(name)) {
|
|
200
|
+
return { kind: "failed", error: grammarFailure(name, resolver) };
|
|
201
|
+
}
|
|
202
|
+
const matched = resolver.matchTools(name);
|
|
203
|
+
if (matched.length === 0) {
|
|
204
|
+
return {
|
|
205
|
+
kind: "failed",
|
|
206
|
+
error: ToolkitUnresolved.make({ message: patternMessage(name, resolver) }),
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
expanded.push(...matched);
|
|
210
|
+
}
|
|
211
|
+
return { kind: "ok", names: expanded };
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Resolves names to registry objects; the names go into `pi.agent` and
|
|
216
|
+
* `ToolkitDoc`. Extensions are the definition's selection plus `skills` when it
|
|
217
|
+
* declares skills and `gate-guard` when its rules are non-empty.
|
|
218
|
+
*/
|
|
219
|
+
export const bindToolkit = (
|
|
220
|
+
definition: AgentDefinition,
|
|
221
|
+
resolver: ToolkitResolver,
|
|
222
|
+
): Effect.Effect<BoundToolkit, ToolkitUnresolved> => {
|
|
223
|
+
const skills = definition.skills ?? [];
|
|
224
|
+
const codemode = definition.codemode === "runs";
|
|
225
|
+
const reserved = definition.extensions.filter(
|
|
226
|
+
(name) => name === CODEMODE_EXTENSION || name === RUNS_EXTENSION,
|
|
227
|
+
);
|
|
228
|
+
if (reserved.length > 0) {
|
|
229
|
+
return Effect.fail(
|
|
230
|
+
ToolkitUnresolved.make({
|
|
231
|
+
message: `Extension "${reserved[0]}" is reserved; set codemode: "runs" on the definition instead.`,
|
|
232
|
+
}),
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
const expansion = expandToolNames(definition.tools, resolver);
|
|
236
|
+
if (expansion.kind === "failed") return Effect.fail(expansion.error);
|
|
237
|
+
const expanded = expansion.names;
|
|
238
|
+
// A codemode-exposed tool needs the codemode extension; a deferred one needs `tool_search`.
|
|
239
|
+
const needsCodemode = expanded.some((name) => resolver.codemodeTools.includes(name));
|
|
240
|
+
const needsToolSearch = expanded.some((name) => resolver.tool(name)?.deferred ?? false);
|
|
241
|
+
const mcpExtensions = resolver.mcpServers
|
|
242
|
+
.filter((server) => server.tools.some((tool) => expanded.includes(tool)))
|
|
243
|
+
.map((server) => server.extension);
|
|
244
|
+
const extensionNames = [
|
|
245
|
+
...new Set([
|
|
246
|
+
...definition.extensions,
|
|
247
|
+
...(skills.length > 0 && !definition.extensions.includes(SKILLS_EXTENSION)
|
|
248
|
+
? [SKILLS_EXTENSION]
|
|
249
|
+
: []),
|
|
250
|
+
...(codemode ? [CODEMODE_EXTENSION, RUNS_EXTENSION] : []),
|
|
251
|
+
...(needsCodemode && !codemode ? [CODEMODE_EXTENSION] : []),
|
|
252
|
+
...(definition.approval.gates.length > 0 ? [GATE_GUARD] : []),
|
|
253
|
+
...mcpExtensions,
|
|
254
|
+
...(needsToolSearch ? [TOOL_SEARCH_EXTENSION] : []),
|
|
255
|
+
]),
|
|
256
|
+
];
|
|
257
|
+
const missingExtensions: string[] = [];
|
|
258
|
+
const extensions: Extension[] = [];
|
|
259
|
+
for (const name of extensionNames) {
|
|
260
|
+
const extension = resolver.extension(name);
|
|
261
|
+
if (extension === undefined) missingExtensions.push(name);
|
|
262
|
+
else extensions.push(extension);
|
|
263
|
+
}
|
|
264
|
+
const missingTools: string[] = [];
|
|
265
|
+
const tools: ToolRegistration[] = [];
|
|
266
|
+
const enabled = [...expanded, ...(codemode ? [CODEMODE_TOOL_NAME, ...RUN_TOOL_NAMES] : [])];
|
|
267
|
+
for (const name of enabled) {
|
|
268
|
+
const tool = resolver.tool(name);
|
|
269
|
+
if (tool === undefined) missingTools.push(name);
|
|
270
|
+
else tools.push(tool);
|
|
271
|
+
}
|
|
272
|
+
const missingSkills = skills.filter((name) => resolver.skill(name) === undefined);
|
|
273
|
+
if (missingExtensions.length > 0 || missingTools.length > 0 || missingSkills.length > 0) {
|
|
274
|
+
return Effect.fail(
|
|
275
|
+
ToolkitUnresolved.make({
|
|
276
|
+
message: [
|
|
277
|
+
sentence("Unresolved extensions", missingExtensions),
|
|
278
|
+
sentence("Unresolved tools", missingTools),
|
|
279
|
+
sentence("Unresolved skills", missingSkills),
|
|
280
|
+
available("Available extensions", resolver.availableExtensions),
|
|
281
|
+
available("Available tools", resolver.availableTools),
|
|
282
|
+
available("Available skills", resolver.availableSkills),
|
|
283
|
+
]
|
|
284
|
+
.filter((part) => part.length > 0)
|
|
285
|
+
.join(" "),
|
|
286
|
+
}),
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
return Effect.succeed({
|
|
291
|
+
change: {
|
|
292
|
+
extensions,
|
|
293
|
+
tools,
|
|
294
|
+
...(definition.model === undefined ? {} : { model: definition.model }),
|
|
295
|
+
...(definition.thinkingLevel === undefined
|
|
296
|
+
? {}
|
|
297
|
+
: { thinkingLevel: definition.thinkingLevel }),
|
|
298
|
+
instructions: instructionsFor(definition),
|
|
299
|
+
...(definition.cwd === undefined ? {} : { cwd: definition.cwd }),
|
|
300
|
+
},
|
|
301
|
+
names: { extensions: extensionNames, tools: enabled, skills },
|
|
302
|
+
approval: definition.approval,
|
|
303
|
+
});
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
export const instructionsFor = (definition: AgentDefinition): string =>
|
|
307
|
+
[definition.prompt.shared, definition.prompt.role]
|
|
308
|
+
.filter((part): part is string => part !== undefined && part.length > 0)
|
|
309
|
+
.join("\n\n");
|
|
310
|
+
|
|
311
|
+
/** Both records in the commit that creates the conversation. */
|
|
312
|
+
export const createBoundChild = async (
|
|
313
|
+
tx: Tx,
|
|
314
|
+
runId: string,
|
|
315
|
+
childRunId: string,
|
|
316
|
+
taskId: TaskId,
|
|
317
|
+
bound: BoundToolkit,
|
|
318
|
+
definition: AgentDefinition,
|
|
319
|
+
maxTurns: number,
|
|
320
|
+
): Promise<ConversationRecord> => {
|
|
321
|
+
const record = await tx.createConversation({ ownership: { kind: "task", taskId } });
|
|
322
|
+
await configure(tx, record.id, { ...bound.change, maxTurns });
|
|
323
|
+
const doc = await tx.doc(ToolkitDoc, record.id);
|
|
324
|
+
doc.definition = definition.name;
|
|
325
|
+
doc.runId = runId;
|
|
326
|
+
doc.runTaskId = taskId;
|
|
327
|
+
doc.childRunId = childRunId;
|
|
328
|
+
doc.approval = {
|
|
329
|
+
gates: bound.approval.gates.map((rule) => ({
|
|
330
|
+
name: rule.name,
|
|
331
|
+
tools: [...rule.tools],
|
|
332
|
+
...(rule.match === undefined ? {} : { match: rule.match }),
|
|
333
|
+
})),
|
|
334
|
+
};
|
|
335
|
+
doc.skills = [...bound.names.skills];
|
|
336
|
+
return record;
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
/** The names a stored agent records that the current registry and catalogue cannot resolve. */
|
|
340
|
+
export const resolveStoredToolkit = (
|
|
341
|
+
state: AgentState | undefined,
|
|
342
|
+
resolver: ToolkitResolver,
|
|
343
|
+
skills: readonly string[] = [],
|
|
344
|
+
): {
|
|
345
|
+
readonly missing: readonly string[];
|
|
346
|
+
readonly available: {
|
|
347
|
+
readonly extensions: readonly string[];
|
|
348
|
+
readonly tools: readonly string[];
|
|
349
|
+
};
|
|
350
|
+
} => {
|
|
351
|
+
const missing: string[] = [];
|
|
352
|
+
// A read-only runtime connects to nothing, so it cannot verify an MCP name.
|
|
353
|
+
const verifyMcp = resolver.unconnected !== undefined;
|
|
354
|
+
if (Array.isArray(state?.extensions)) {
|
|
355
|
+
for (const name of state.extensions) {
|
|
356
|
+
if (!verifyMcp && name.startsWith("mcp:")) continue;
|
|
357
|
+
if (resolver.extension(name) === undefined) missing.push(name);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
if (Array.isArray(state?.tools)) {
|
|
361
|
+
for (const name of state.tools) {
|
|
362
|
+
if (!verifyMcp && name.startsWith("mcp__")) continue;
|
|
363
|
+
if (resolver.tool(name) === undefined) missing.push(name);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
for (const name of skills) {
|
|
367
|
+
if (resolver.skill(name) === undefined) missing.push(name);
|
|
368
|
+
}
|
|
369
|
+
return {
|
|
370
|
+
missing,
|
|
371
|
+
available: {
|
|
372
|
+
extensions: resolver.availableExtensions,
|
|
373
|
+
tools: resolver.availableTools,
|
|
374
|
+
},
|
|
375
|
+
};
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
/** A conversation id as a plain number, for documents and views. */
|
|
379
|
+
export const conversationNumber = (id: ConversationId): number => id;
|