@restranger/repi-subagents 0.0.0-stage → 0.0.1-sha.2ba2721
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 +270 -2
- package/package.json +40 -5
- package/src/agent-file.ts +297 -0
- package/src/agent-schema.ts +90 -0
- package/src/child-guard.ts +151 -0
- package/src/command.ts +256 -0
- package/src/default-agents.ts +400 -0
- package/src/discovery.ts +354 -0
- package/src/extensions.ts +119 -0
- package/src/fleet-ui.ts +472 -0
- package/src/format.ts +974 -0
- package/src/frontmatter.ts +302 -0
- package/src/index.ts +112 -0
- package/src/manager.ts +1164 -0
- package/src/model-resolver.ts +58 -0
- package/src/overrides.ts +693 -0
- package/src/plugin.ts +568 -0
- package/src/prompt.ts +127 -0
- package/src/registry.ts +71 -0
- package/src/session.ts +304 -0
- package/src/status-widget.ts +65 -0
- package/src/tool-policy.ts +77 -0
- package/src/tools.ts +546 -0
- package/src/types.ts +161 -0
package/src/registry.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { type DiscoverAgentsOptions, discoverAgents } from "./discovery.js";
|
|
2
|
+
import type { AgentDefinition, AgentDiagnostic, AgentScanResult } from "./types.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Per-session view of the declarative agent files.
|
|
6
|
+
*
|
|
7
|
+
* The registry is deliberately small: discovery owns filesystem I/O, the registry owns lookup and
|
|
8
|
+
* reload semantics, and the tool/manager never touch disk directly.
|
|
9
|
+
*/
|
|
10
|
+
export class AgentRegistry {
|
|
11
|
+
private scan: AgentScanResult;
|
|
12
|
+
private readonly options: DiscoverAgentsOptions;
|
|
13
|
+
|
|
14
|
+
constructor(options: DiscoverAgentsOptions) {
|
|
15
|
+
this.options = options;
|
|
16
|
+
this.scan = discoverAgents(options);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
reload(overrides?: Partial<DiscoverAgentsOptions>): AgentScanResult {
|
|
20
|
+
this.scan = discoverAgents({ ...this.options, ...overrides });
|
|
21
|
+
return this.scan;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
list(): readonly AgentDefinition[] {
|
|
25
|
+
return this.scan.agents;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
get(name: string): AgentDefinition | undefined {
|
|
29
|
+
const key = name.trim().toLowerCase();
|
|
30
|
+
return this.scan.agents.find((agent) => agent.name.toLowerCase() === key);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Replace an agent's fallback models in memory (used by `re:subagents@1`).
|
|
35
|
+
*
|
|
36
|
+
* Returns false for an unknown name so a caller can report it instead of silently writing an
|
|
37
|
+
* override that discovery will ignore on the next scan.
|
|
38
|
+
*/
|
|
39
|
+
setFallbackModels(name: string, fallbackModels: readonly string[]): boolean {
|
|
40
|
+
const key = name.trim().toLowerCase();
|
|
41
|
+
const index = this.scan.agents.findIndex((agent) => agent.name.toLowerCase() === key);
|
|
42
|
+
if (index === -1) {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
const agents = this.scan.agents.map((agent, current) =>
|
|
46
|
+
current === index ? { ...agent, fallbackModels: [...fallbackModels] } : agent,
|
|
47
|
+
);
|
|
48
|
+
this.scan = { ...this.scan, agents };
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
diagnostics(): readonly AgentDiagnostic[] {
|
|
53
|
+
return this.scan.diagnostics;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
directories(): readonly string[] {
|
|
57
|
+
return this.scan.directories;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
configFiles(): readonly string[] {
|
|
61
|
+
return this.scan.configFiles ?? [];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
maxConcurrent(): number | undefined {
|
|
65
|
+
return this.scan.maxConcurrent;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
snapshot(): AgentScanResult {
|
|
69
|
+
return this.scan;
|
|
70
|
+
}
|
|
71
|
+
}
|
package/src/session.ts
ADDED
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import type { PermissionService } from "@restranger/repi-lib";
|
|
5
|
+
import { type SubagentConfirmationRequest, createSubagentGuardExtension } from "./child-guard.js";
|
|
6
|
+
import type { ChildBuiltinExtension } from "./extensions.js";
|
|
7
|
+
import type { AgentDefinition } from "./types.js";
|
|
8
|
+
|
|
9
|
+
export interface ChildSession {
|
|
10
|
+
readonly sessionId?: string;
|
|
11
|
+
prompt(text: string): Promise<void>;
|
|
12
|
+
abort(): Promise<void> | void;
|
|
13
|
+
dispose(): void;
|
|
14
|
+
subscribe(handler: (event: unknown) => void): () => void;
|
|
15
|
+
steer?(text: string): Promise<unknown>;
|
|
16
|
+
getLastAssistantText?(): string | undefined;
|
|
17
|
+
getAllTools?(): readonly { readonly name: string }[];
|
|
18
|
+
readonly messages?: readonly unknown[];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface CreateChildSessionOptions {
|
|
22
|
+
readonly runId: string;
|
|
23
|
+
readonly agent: AgentDefinition;
|
|
24
|
+
readonly cwd: string;
|
|
25
|
+
readonly parentSessionId: string;
|
|
26
|
+
readonly permission?: PermissionService;
|
|
27
|
+
/** Parent-side approval for permission decisions that need a human. */
|
|
28
|
+
readonly requestConfirmation?: (request: SubagentConfirmationRequest) => Promise<boolean>;
|
|
29
|
+
/** Resolved allowlist; `undefined` means every tool the child session exposes. */
|
|
30
|
+
readonly tools?: readonly string[];
|
|
31
|
+
readonly excludeTools: readonly string[];
|
|
32
|
+
/** Extension module files that provide the child's requested tools. */
|
|
33
|
+
readonly extensionPaths?: readonly string[];
|
|
34
|
+
/** Host built-ins the child re-creates through SDK factories. */
|
|
35
|
+
readonly builtinExtensions?: readonly ChildBuiltinExtension[];
|
|
36
|
+
/**
|
|
37
|
+
* Parent ModelRuntime. Sharing it keeps extension-registered providers, OAuth credentials
|
|
38
|
+
* and request hooks usable in the child; a fresh runtime only knows built-in providers.
|
|
39
|
+
*/
|
|
40
|
+
readonly modelRuntime?: unknown;
|
|
41
|
+
readonly model?: unknown;
|
|
42
|
+
readonly thinking?: string;
|
|
43
|
+
readonly signal?: AbortSignal;
|
|
44
|
+
readonly onDiagnostic?: (message: string) => void;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface ChildSessionFactory {
|
|
48
|
+
create(options: CreateChildSessionOptions): Promise<ChildSession>;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
interface HostAgentSession {
|
|
52
|
+
readonly sessionId?: string;
|
|
53
|
+
readonly messages?: readonly unknown[];
|
|
54
|
+
prompt(text: string): Promise<void>;
|
|
55
|
+
abort(): Promise<void>;
|
|
56
|
+
dispose(): void;
|
|
57
|
+
subscribe(handler: (event: unknown) => void): () => void;
|
|
58
|
+
steer?(text: string): Promise<unknown>;
|
|
59
|
+
getLastAssistantText?(): string | undefined;
|
|
60
|
+
getAllTools?(): readonly { readonly name: string }[];
|
|
61
|
+
bindExtensions?(bindings: Record<string, unknown>): Promise<void>;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
interface HostSdk {
|
|
65
|
+
DefaultResourceLoader: new (
|
|
66
|
+
options: Record<string, unknown>,
|
|
67
|
+
) => {
|
|
68
|
+
reload(): Promise<void>;
|
|
69
|
+
getExtensions?(): {
|
|
70
|
+
errors?: readonly unknown[];
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
SessionManager: {
|
|
74
|
+
inMemory(cwd?: string): unknown;
|
|
75
|
+
};
|
|
76
|
+
createAgentSession(options: Record<string, unknown>): Promise<{
|
|
77
|
+
session: HostAgentSession;
|
|
78
|
+
modelFallbackMessage?: string;
|
|
79
|
+
}>;
|
|
80
|
+
createCodemodeExtension?: (options?: Record<string, unknown>) => unknown;
|
|
81
|
+
createToolSearchExtension?: () => unknown;
|
|
82
|
+
createMcpExtension?: (options?: Record<string, unknown>) => unknown;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const HOST_PACKAGE = "@earendil-works/pi-coding-agent";
|
|
86
|
+
let hostSdk: Promise<HostSdk> | undefined;
|
|
87
|
+
|
|
88
|
+
async function loadHostSdk(): Promise<HostSdk> {
|
|
89
|
+
hostSdk ??= import(HOST_PACKAGE) as Promise<HostSdk>;
|
|
90
|
+
return hostSdk;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Test hook to inject a mock HostSdk without needing the actual peer dependency. */
|
|
94
|
+
export function _setHostSdkForTest(sdk: HostSdk | undefined): void {
|
|
95
|
+
hostSdk = sdk ? Promise.resolve(sdk) : undefined;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Create a child Pi session in-process.
|
|
100
|
+
*
|
|
101
|
+
* The child is deliberately assembled from a *scoped* resource loader. Ambient
|
|
102
|
+
* package/user/project extensions stay disabled (`noExtensions: true`), but the loader receives
|
|
103
|
+
* exactly the extension modules that registered the tools the agent asked for
|
|
104
|
+
* (`additionalExtensionPaths`) plus recreated host built-ins. This forwards the useful plugin
|
|
105
|
+
* surface without starting a second copy of every unrelated ambient extension in the parent
|
|
106
|
+
* process, and without loading `@restranger/repi-subagents` recursively.
|
|
107
|
+
*
|
|
108
|
+
* The temporary `agentDir` keeps global *settings* discovery out of the child, while the loader
|
|
109
|
+
* `cwd` stays the real project directory so the system prompt's `<cwd>` matches tool execution.
|
|
110
|
+
* `noContextFiles`/`noSkills`/... still keep project skills, prompts, themes and AGENTS.md out of
|
|
111
|
+
* the prompt; the child never inherits the parent's conversation.
|
|
112
|
+
*
|
|
113
|
+
* This is resource scoping, not a security sandbox: the child runs in the parent process, so it
|
|
114
|
+
* shares the process environment (API keys included) and provider credentials. A child that needs
|
|
115
|
+
* a narrower surface must be restricted through `tools`/`excludeTools` and the permission engine,
|
|
116
|
+
* which is what `subagent-guard` does.
|
|
117
|
+
*/
|
|
118
|
+
export function createChildSessionFactory(): ChildSessionFactory {
|
|
119
|
+
return {
|
|
120
|
+
async create(options) {
|
|
121
|
+
if (options.signal?.aborted) {
|
|
122
|
+
throw abortError();
|
|
123
|
+
}
|
|
124
|
+
const sdk = await loadHostSdk();
|
|
125
|
+
const tempAgentDir = mkdtempSync(join(tmpdir(), "repi-subagents-"));
|
|
126
|
+
try {
|
|
127
|
+
const systemPrompt = options.agent.systemPrompt.trim();
|
|
128
|
+
const extensionFactories: unknown[] = [
|
|
129
|
+
createSubagentGuardExtension({
|
|
130
|
+
allowedTools: options.tools,
|
|
131
|
+
permission: options.permission,
|
|
132
|
+
...(options.requestConfirmation ? { requestConfirmation: options.requestConfirmation } : {}),
|
|
133
|
+
parentSessionId: options.parentSessionId,
|
|
134
|
+
cwd: options.cwd,
|
|
135
|
+
...(options.signal ? { signal: options.signal } : {}),
|
|
136
|
+
}),
|
|
137
|
+
];
|
|
138
|
+
for (const builtin of options.builtinExtensions ?? []) {
|
|
139
|
+
const factory = createBuiltinExtension(sdk, builtin);
|
|
140
|
+
if (factory) {
|
|
141
|
+
extensionFactories.push(factory);
|
|
142
|
+
} else {
|
|
143
|
+
options.onDiagnostic?.(
|
|
144
|
+
`Host does not export the ${builtin} built-in extension; child tool may be unavailable.`,
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
const loaderOptions: Record<string, unknown> = {
|
|
149
|
+
cwd: options.cwd,
|
|
150
|
+
agentDir: tempAgentDir,
|
|
151
|
+
noExtensions: true,
|
|
152
|
+
noSkills: true,
|
|
153
|
+
noPromptTemplates: true,
|
|
154
|
+
noThemes: true,
|
|
155
|
+
noContextFiles: true,
|
|
156
|
+
...(options.extensionPaths && options.extensionPaths.length > 0
|
|
157
|
+
? { additionalExtensionPaths: [...options.extensionPaths] }
|
|
158
|
+
: {}),
|
|
159
|
+
extensionFactories,
|
|
160
|
+
};
|
|
161
|
+
if (options.agent.promptMode === "replace") {
|
|
162
|
+
loaderOptions.systemPromptOverride = () => systemPrompt;
|
|
163
|
+
loaderOptions.appendSystemPromptOverride = () => [];
|
|
164
|
+
} else if (systemPrompt.length > 0) {
|
|
165
|
+
loaderOptions.appendSystemPromptOverride = (base: string[]) => [...base, systemPrompt];
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const loader = new sdk.DefaultResourceLoader(loaderOptions);
|
|
169
|
+
await loader.reload();
|
|
170
|
+
|
|
171
|
+
const extensionErrors = loader.getExtensions?.()?.errors;
|
|
172
|
+
if (Array.isArray(extensionErrors)) {
|
|
173
|
+
for (const err of extensionErrors) {
|
|
174
|
+
const errObj = err as { message?: unknown; error?: unknown } | null | undefined;
|
|
175
|
+
const message =
|
|
176
|
+
errObj?.message ??
|
|
177
|
+
(errObj?.error && typeof errObj.error === "object" && "message" in errObj.error
|
|
178
|
+
? (errObj.error as { message?: unknown }).message
|
|
179
|
+
: undefined) ??
|
|
180
|
+
err;
|
|
181
|
+
options.onDiagnostic?.(`Child extension failed to load: ${message ?? err}`);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const sessionManager = sdk.SessionManager.inMemory(options.cwd);
|
|
186
|
+
const createOptions: Record<string, unknown> = {
|
|
187
|
+
cwd: options.cwd,
|
|
188
|
+
sessionManager,
|
|
189
|
+
resourceLoader: loader,
|
|
190
|
+
excludeTools: [...options.excludeTools],
|
|
191
|
+
};
|
|
192
|
+
if (options.modelRuntime !== undefined) {
|
|
193
|
+
createOptions.modelRuntime = options.modelRuntime;
|
|
194
|
+
}
|
|
195
|
+
if (options.model !== undefined) {
|
|
196
|
+
createOptions.model = options.model;
|
|
197
|
+
}
|
|
198
|
+
if (options.thinking !== undefined) {
|
|
199
|
+
createOptions.thinkingLevel = options.thinking;
|
|
200
|
+
}
|
|
201
|
+
if (options.tools !== undefined) {
|
|
202
|
+
createOptions.tools = [...options.tools];
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const created = await sdk.createAgentSession(createOptions);
|
|
206
|
+
if (created.modelFallbackMessage) {
|
|
207
|
+
options.onDiagnostic?.(created.modelFallbackMessage);
|
|
208
|
+
}
|
|
209
|
+
const session = created.session;
|
|
210
|
+
try {
|
|
211
|
+
await session.bindExtensions?.({});
|
|
212
|
+
} catch (err) {
|
|
213
|
+
session.dispose();
|
|
214
|
+
throw err;
|
|
215
|
+
}
|
|
216
|
+
return new HostChildSession(session, tempAgentDir, options.signal);
|
|
217
|
+
} catch (err) {
|
|
218
|
+
rmSync(tempAgentDir, { recursive: true, force: true });
|
|
219
|
+
throw err;
|
|
220
|
+
}
|
|
221
|
+
},
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
class HostChildSession implements ChildSession {
|
|
226
|
+
private disposed = false;
|
|
227
|
+
private readonly removeAbort: (() => void) | undefined;
|
|
228
|
+
|
|
229
|
+
constructor(
|
|
230
|
+
private readonly session: HostAgentSession,
|
|
231
|
+
private readonly tempAgentDir: string,
|
|
232
|
+
signal?: AbortSignal,
|
|
233
|
+
) {
|
|
234
|
+
if (signal) {
|
|
235
|
+
const onAbort = (): void => {
|
|
236
|
+
void Promise.resolve(this.session.abort()).catch(() => undefined);
|
|
237
|
+
};
|
|
238
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
239
|
+
this.removeAbort = () => signal.removeEventListener("abort", onAbort);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
get sessionId(): string | undefined {
|
|
244
|
+
return this.session.sessionId;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
get messages(): readonly unknown[] | undefined {
|
|
248
|
+
return this.session.messages;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
prompt(text: string): Promise<void> {
|
|
252
|
+
return this.session.prompt(text);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
abort(): Promise<void> {
|
|
256
|
+
return this.session.abort();
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
steer(text: string): Promise<unknown> {
|
|
260
|
+
return this.session.steer ? this.session.steer(text) : Promise.resolve(undefined);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
getLastAssistantText(): string | undefined {
|
|
264
|
+
return this.session.getLastAssistantText?.();
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
getAllTools(): readonly { readonly name: string }[] {
|
|
268
|
+
return this.session.getAllTools?.() ?? [];
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
subscribe(handler: (event: unknown) => void): () => void {
|
|
272
|
+
return this.session.subscribe(handler);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
dispose(): void {
|
|
276
|
+
if (this.disposed) {
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
this.disposed = true;
|
|
280
|
+
this.removeAbort?.();
|
|
281
|
+
try {
|
|
282
|
+
this.session.dispose();
|
|
283
|
+
} finally {
|
|
284
|
+
rmSync(this.tempAgentDir, { recursive: true, force: true });
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function createBuiltinExtension(sdk: HostSdk, name: ChildBuiltinExtension): unknown {
|
|
290
|
+
switch (name) {
|
|
291
|
+
case "codemode":
|
|
292
|
+
return sdk.createCodemodeExtension?.({ mode: "on" });
|
|
293
|
+
case "tool_search":
|
|
294
|
+
return sdk.createToolSearchExtension?.();
|
|
295
|
+
case "mcp":
|
|
296
|
+
return sdk.createMcpExtension?.();
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
function abortError(): Error {
|
|
301
|
+
const error = new Error("Subagent run was aborted.");
|
|
302
|
+
error.name = "AbortError";
|
|
303
|
+
return error;
|
|
304
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { PiUiCustomComponent, PiUiCustomTheme, PiUiCustomTui } from "@restranger/repi-lib";
|
|
2
|
+
import { formatStatusWidget, widgetLineBudget } from "./format.js";
|
|
3
|
+
import type { SubagentRunSnapshot } from "./types.js";
|
|
4
|
+
|
|
5
|
+
export interface FleetStatusWidgetOptions {
|
|
6
|
+
readonly runs: () => readonly SubagentRunSnapshot[];
|
|
7
|
+
/** Suppress widget painting while modal inspector is open. */
|
|
8
|
+
readonly isInspectorOpen?: () => boolean;
|
|
9
|
+
readonly now?: () => number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Live fleet tree above the editor: one header with the totals plus a `● agent · status · time ·
|
|
14
|
+
* turn · tokens` row per active child, its two most recent finished steps and the tool it is
|
|
15
|
+
* running right now. Queued children get a bare row; finished children disappear (their result is
|
|
16
|
+
* in the tool call, `/subagents fleet` owns history), so the widget is gone as soon as the last child settles.
|
|
17
|
+
*
|
|
18
|
+
* The height is capped from the terminal's row count instead of a constant, so a short window never
|
|
19
|
+
* pushes Pi's editor off the screen, and the plugin's spinner timer only asks for frames.
|
|
20
|
+
*/
|
|
21
|
+
export class FleetStatusWidget implements PiUiCustomComponent {
|
|
22
|
+
private tui?: PiUiCustomTui;
|
|
23
|
+
private theme?: PiUiCustomTheme;
|
|
24
|
+
|
|
25
|
+
constructor(private readonly options: FleetStatusWidgetOptions) {}
|
|
26
|
+
|
|
27
|
+
attach(tui: PiUiCustomTui, theme: PiUiCustomTheme): void {
|
|
28
|
+
this.tui = tui;
|
|
29
|
+
this.theme = theme;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Pi's `Container.invalidate()` walks its children and calls this; without it the whole UI throws
|
|
34
|
+
* on the first invalidate (session switch, shutdown, `switchTuiMode`).
|
|
35
|
+
*/
|
|
36
|
+
invalidate(): void {
|
|
37
|
+
// Every line is computed in `render()`, so there is nothing cached to drop.
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
dispose(): void {
|
|
41
|
+
this.tui = undefined;
|
|
42
|
+
this.theme = undefined;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** A tick asks for one frame; a host that already dropped the component is not an error. */
|
|
46
|
+
refresh(): void {
|
|
47
|
+
try {
|
|
48
|
+
this.tui?.requestRender();
|
|
49
|
+
} catch {
|
|
50
|
+
// Detached or stale TUI: the plugin unmounts on the next sync.
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
render(width: number): string[] {
|
|
55
|
+
if (this.options.isInspectorOpen?.()) {
|
|
56
|
+
return [];
|
|
57
|
+
}
|
|
58
|
+
return formatStatusWidget(this.options.runs(), {
|
|
59
|
+
width,
|
|
60
|
+
maxLines: widgetLineBudget(this.tui?.terminal?.rows),
|
|
61
|
+
...(this.theme ? { theme: this.theme } : {}),
|
|
62
|
+
...(this.options.now ? { now: this.options.now() } : {}),
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { AgentDefinition } from "./types.js";
|
|
2
|
+
|
|
3
|
+
/** Tools a child agent may use when its declaration omits `tools:`. */
|
|
4
|
+
export const DEFAULT_CHILD_TOOLS = ["read", "bash", "edit", "write", "grep", "find", "ls"] as const;
|
|
5
|
+
|
|
6
|
+
/** Tools owned by this extension; a child must never inherit them (recursion guard). */
|
|
7
|
+
export const SUBAGENT_TOOL_NAMES = [
|
|
8
|
+
"subagent",
|
|
9
|
+
"subagent_status",
|
|
10
|
+
"subagent_wait",
|
|
11
|
+
"subagent_abort",
|
|
12
|
+
"subagent_list",
|
|
13
|
+
] as const;
|
|
14
|
+
|
|
15
|
+
const RECURSION_GUARD = new Set<string>(SUBAGENT_TOOL_NAMES);
|
|
16
|
+
|
|
17
|
+
export interface ResolvedToolAllowlist {
|
|
18
|
+
/** `undefined` means "every tool the child session can see". */
|
|
19
|
+
readonly tools?: readonly string[];
|
|
20
|
+
readonly source: "default" | "all" | "list";
|
|
21
|
+
/** Requested tools the parent host does not currently expose. */
|
|
22
|
+
readonly unavailable: readonly string[];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Turn an agent declaration into the child session's tool allowlist.
|
|
27
|
+
*
|
|
28
|
+
* The declaration is the authority: an omitted `tools:` means the documented built-ins, `tools:
|
|
29
|
+
* none`/`[]` means none, and `tools: "*"` means every parent tool. The extension's own tool names
|
|
30
|
+
* are always removed, even when a declaration asks for them, because a child that can spawn
|
|
31
|
+
* children would make the parent's wait/abort ownership ambiguous in P0.
|
|
32
|
+
*/
|
|
33
|
+
export function resolveToolAllowlist(
|
|
34
|
+
agent: AgentDefinition,
|
|
35
|
+
parentToolNames: readonly string[],
|
|
36
|
+
): ResolvedToolAllowlist {
|
|
37
|
+
const disallowed = new Set(agent.disallowedTools);
|
|
38
|
+
const parent = parentToolNames.length > 0 ? new Set(parentToolNames) : undefined;
|
|
39
|
+
|
|
40
|
+
let source: ResolvedToolAllowlist["source"];
|
|
41
|
+
let requested: readonly string[] | undefined;
|
|
42
|
+
if (agent.tools === "*") {
|
|
43
|
+
source = "all";
|
|
44
|
+
requested = parent ? [...parent] : undefined;
|
|
45
|
+
} else if (agent.tools === undefined) {
|
|
46
|
+
source = "default";
|
|
47
|
+
requested = [...DEFAULT_CHILD_TOOLS];
|
|
48
|
+
} else {
|
|
49
|
+
source = "list";
|
|
50
|
+
requested = agent.tools;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const unavailable = parent && requested ? requested.filter((name) => !parent.has(name)) : [];
|
|
54
|
+
const filtered = requested
|
|
55
|
+
?.filter((name) => !RECURSION_GUARD.has(name) && !disallowed.has(name))
|
|
56
|
+
.filter((name, index, list) => list.indexOf(name) === index);
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
...(filtered ? { tools: filtered } : {}),
|
|
60
|
+
source,
|
|
61
|
+
unavailable,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Human-readable form for listings and diagnostics. */
|
|
66
|
+
export function describeToolSpec(agent: AgentDefinition): string {
|
|
67
|
+
if (agent.tools === "*") {
|
|
68
|
+
return "all tools";
|
|
69
|
+
}
|
|
70
|
+
if (agent.tools === undefined) {
|
|
71
|
+
return "default tools";
|
|
72
|
+
}
|
|
73
|
+
if (agent.tools.length === 0) {
|
|
74
|
+
return "no tools";
|
|
75
|
+
}
|
|
76
|
+
return agent.tools.join(", ");
|
|
77
|
+
}
|