@lune_99/pi-subagent 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 +24 -0
- package/LICENSE +21 -0
- package/README.md +169 -2
- package/docs/capture.json +30 -0
- package/docs/previews.md +22 -0
- package/docs/reference.md +240 -0
- package/docs/subagent-auto-tiers.png +0 -0
- package/docs/subagent-inspector.png +0 -0
- package/docs/subagent-overview.png +0 -0
- package/package.json +82 -5
- package/skills/create-lune-subagent/SKILL.md +186 -0
- package/src/auto-model.ts +592 -0
- package/src/catalog.ts +111 -0
- package/src/definition-file.ts +112 -0
- package/src/definitions.ts +324 -0
- package/src/dock/dock-alert.ts +78 -0
- package/src/dock/dock-contribution.ts +318 -0
- package/src/extension-tools.ts +46 -0
- package/src/index.ts +168 -0
- package/src/inspector/details.ts +113 -0
- package/src/inspector/inspector.ts +653 -0
- package/src/inspector/layout.ts +119 -0
- package/src/inspector/transcript.ts +735 -0
- package/src/inspector/viewport.ts +62 -0
- package/src/inspector/wrap.ts +136 -0
- package/src/models.ts +54 -0
- package/src/notify/notification-box.ts +381 -0
- package/src/notify/notifier.ts +569 -0
- package/src/paths.ts +34 -0
- package/src/render/lune-theme.ts +68 -0
- package/src/render/text.ts +121 -0
- package/src/run/guide.ts +26 -0
- package/src/run/report-tool.ts +90 -0
- package/src/run/run-file.ts +193 -0
- package/src/run/run-manager.ts +838 -0
- package/src/run/run-session.ts +307 -0
- package/src/run/run.ts +681 -0
- package/src/run/session-start.ts +283 -0
- package/src/run/types.ts +167 -0
- package/src/run/worktree.ts +138 -0
- package/src/schema.ts +9 -0
- package/src/settings/agents-menu.ts +363 -0
- package/src/settings/panel.ts +179 -0
- package/src/settings/schema.ts +71 -0
- package/src/tools/format.ts +375 -0
- package/src/tools/subagent-tools.ts +467 -0
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
import type { ThinkingLevel } from "@earendil-works/pi-agent-core";
|
|
2
|
+
import type { Api, Model } from "@earendil-works/pi-ai";
|
|
3
|
+
import {
|
|
4
|
+
createAgentSessionFromServices,
|
|
5
|
+
createAgentSessionServices,
|
|
6
|
+
type AgentSession,
|
|
7
|
+
type AgentSessionEvent,
|
|
8
|
+
type AgentSessionServices,
|
|
9
|
+
type ExtensionError,
|
|
10
|
+
type ExtensionUIContext,
|
|
11
|
+
type LoadExtensionsResult,
|
|
12
|
+
type ModelRuntime,
|
|
13
|
+
type RegisteredTool,
|
|
14
|
+
type SessionManager,
|
|
15
|
+
type SettingsManager,
|
|
16
|
+
type Theme,
|
|
17
|
+
type ToolDefinition,
|
|
18
|
+
} from "@earendil-works/pi-coding-agent";
|
|
19
|
+
|
|
20
|
+
import { runExtensionFactories } from "../catalog.ts";
|
|
21
|
+
import { isSyntheticPath, type ExtensionTool } from "../extension-tools.ts";
|
|
22
|
+
|
|
23
|
+
export interface RunServicesOptions {
|
|
24
|
+
cwd: string;
|
|
25
|
+
agentDir: string;
|
|
26
|
+
projectTrusted: boolean;
|
|
27
|
+
/** The definition body, if any, then the subagent guide. */
|
|
28
|
+
appendSystemPrompt: readonly string[];
|
|
29
|
+
/** Tests only: a runtime with an offline provider. Runs otherwise create their own. */
|
|
30
|
+
modelRuntime?: ModelRuntime;
|
|
31
|
+
/** The run's settings; created for `cwd` when left out. */
|
|
32
|
+
settingsManager?: SettingsManager;
|
|
33
|
+
/** The extension tools the run selected; it loads their extensions. */
|
|
34
|
+
extensionTools: readonly ExtensionTool[];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Services for one run's working directory: its settings, resources and its own model runtime.
|
|
39
|
+
*
|
|
40
|
+
* `noExtensions` keeps user and project extensions out, including installed Lune packages. The
|
|
41
|
+
* run loads the three built-in factories and, for each selected extension tool, the file of the
|
|
42
|
+
* extension that registers it, which then registers only the selected tools.
|
|
43
|
+
*/
|
|
44
|
+
export async function createRunServices(
|
|
45
|
+
options: RunServicesOptions,
|
|
46
|
+
): Promise<AgentSessionServices> {
|
|
47
|
+
const selected = new Set(options.extensionTools.map((tool) => tool.name));
|
|
48
|
+
|
|
49
|
+
return createAgentSessionServices({
|
|
50
|
+
cwd: options.cwd,
|
|
51
|
+
agentDir: options.agentDir,
|
|
52
|
+
settingsManager: options.settingsManager,
|
|
53
|
+
modelRuntime: options.modelRuntime,
|
|
54
|
+
resourceLoaderOptions: {
|
|
55
|
+
noExtensions: true,
|
|
56
|
+
extensionFactories: runExtensionFactories(),
|
|
57
|
+
additionalExtensionPaths: [...new Set(options.extensionTools.map((tool) => tool.path))],
|
|
58
|
+
extensionsOverride: (base) => keepSelectedTools(base, selected),
|
|
59
|
+
// The override takes literal text. `appendSystemPrompt` would read an entry that happens
|
|
60
|
+
// to name an existing file as that file, and it would drop a discovered APPEND_SYSTEM.md.
|
|
61
|
+
appendSystemPromptOverride: (base) => [
|
|
62
|
+
...base,
|
|
63
|
+
...options.appendSystemPrompt.filter((text) => text.trim() !== ""),
|
|
64
|
+
],
|
|
65
|
+
},
|
|
66
|
+
resourceLoaderReloadOptions: {
|
|
67
|
+
resolveProjectTrust: async () => options.projectTrusted,
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* With `noExtensions`, the only extensions with a file are those loaded for selected tools. Their
|
|
74
|
+
* tool maps keep only the selected tools, also for tools they register later, so an extension
|
|
75
|
+
* loaded for one tool adds no others and does not replace the run's native tools.
|
|
76
|
+
*/
|
|
77
|
+
function keepSelectedTools(
|
|
78
|
+
result: LoadExtensionsResult,
|
|
79
|
+
selected: ReadonlySet<string>,
|
|
80
|
+
): LoadExtensionsResult {
|
|
81
|
+
for (const extension of result.extensions) {
|
|
82
|
+
if (!isSyntheticPath(extension.path)) {
|
|
83
|
+
extension.tools = new SelectedTools(selected, extension.tools);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return result;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** A tool map that ignores tools outside the selection; the extension API registers through `set`. */
|
|
91
|
+
class SelectedTools extends Map<string, RegisteredTool> {
|
|
92
|
+
private readonly selected: ReadonlySet<string>;
|
|
93
|
+
|
|
94
|
+
constructor(selected: ReadonlySet<string>, tools: Iterable<[string, RegisteredTool]>) {
|
|
95
|
+
super();
|
|
96
|
+
this.selected = selected;
|
|
97
|
+
|
|
98
|
+
for (const [name, tool] of tools) {
|
|
99
|
+
this.set(name, tool);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
override set(name: string, tool: RegisteredTool): this {
|
|
104
|
+
return this.selected.has(name) ? super.set(name, tool) : this;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Resolve a model in the run's own runtime and check its auth the way `session.prompt()` does. */
|
|
109
|
+
export async function resolveRunModel(
|
|
110
|
+
services: AgentSessionServices,
|
|
111
|
+
provider: string,
|
|
112
|
+
id: string,
|
|
113
|
+
): Promise<Model<Api>> {
|
|
114
|
+
const model = services.modelRuntime.getModel(provider, id);
|
|
115
|
+
|
|
116
|
+
if (!model) {
|
|
117
|
+
throw new Error(
|
|
118
|
+
`The model ${provider}/${id} is not available to a run. Runs load extensions only ` +
|
|
119
|
+
"for selected tools, so models that an extension provides are missing.",
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const authenticated =
|
|
124
|
+
services.modelRuntime.hasConfiguredAuth(model.provider) ||
|
|
125
|
+
(await services.modelRuntime.checkAuth(model.provider)) !== undefined;
|
|
126
|
+
|
|
127
|
+
if (!authenticated) {
|
|
128
|
+
throw new Error(`The model ${provider}/${id} has no configured authentication.`);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return model;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export interface Diagnostic {
|
|
135
|
+
level: "info" | "warning" | "error";
|
|
136
|
+
message: string;
|
|
137
|
+
at: number;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export interface LiveSessionOptions {
|
|
141
|
+
services: AgentSessionServices;
|
|
142
|
+
sessionManager: SessionManager;
|
|
143
|
+
model: Model<Api>;
|
|
144
|
+
/** Undefined lets Pi pick the level from the run's settings. */
|
|
145
|
+
thinkingLevel: ThinkingLevel | undefined;
|
|
146
|
+
tools: string[];
|
|
147
|
+
customTools: ToolDefinition[];
|
|
148
|
+
reason: "startup" | "resume";
|
|
149
|
+
theme: Theme;
|
|
150
|
+
onEvent: (event: AgentSessionEvent) => void;
|
|
151
|
+
onDiagnostic: (diagnostic: Diagnostic) => void;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export interface LiveSession {
|
|
155
|
+
readonly session: AgentSession;
|
|
156
|
+
/** Idempotent; also the cleanup after a failed initialization. */
|
|
157
|
+
close(): Promise<void>;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Create the run's SDK session and start its extensions.
|
|
162
|
+
*
|
|
163
|
+
* Events are subscribed before `bindExtensions()`, which emits `session_start` and starts the MCP
|
|
164
|
+
* connections, so nothing the extensions report during startup is missed.
|
|
165
|
+
*/
|
|
166
|
+
export async function openLiveSession(options: LiveSessionOptions): Promise<LiveSession> {
|
|
167
|
+
const { session } = await createAgentSessionFromServices({
|
|
168
|
+
services: options.services,
|
|
169
|
+
sessionManager: options.sessionManager,
|
|
170
|
+
sessionStartEvent: { type: "session_start", reason: options.reason },
|
|
171
|
+
model: options.model,
|
|
172
|
+
thinkingLevel: options.thinkingLevel,
|
|
173
|
+
tools: options.tools,
|
|
174
|
+
customTools: options.customTools,
|
|
175
|
+
});
|
|
176
|
+
const unsubscribe = session.subscribe(options.onEvent);
|
|
177
|
+
let closing: Promise<void> | undefined;
|
|
178
|
+
|
|
179
|
+
const live: LiveSession = {
|
|
180
|
+
session,
|
|
181
|
+
close() {
|
|
182
|
+
closing ??= closeSession(session, unsubscribe, options.onDiagnostic);
|
|
183
|
+
|
|
184
|
+
return closing;
|
|
185
|
+
},
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
try {
|
|
189
|
+
await session.bindExtensions({
|
|
190
|
+
uiContext: createHeadlessUi(options.theme, options.onDiagnostic),
|
|
191
|
+
onError: (error) => options.onDiagnostic(extensionDiagnostic(error)),
|
|
192
|
+
});
|
|
193
|
+
} catch (error) {
|
|
194
|
+
await live.close();
|
|
195
|
+
throw error;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
return live;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Abort, then let the extensions shut down, then release the session. `dispose()` alone does not
|
|
203
|
+
* run the MCP shutdown handlers that close server connections.
|
|
204
|
+
*/
|
|
205
|
+
async function closeSession(
|
|
206
|
+
session: AgentSession,
|
|
207
|
+
unsubscribe: () => void,
|
|
208
|
+
onDiagnostic: (diagnostic: Diagnostic) => void,
|
|
209
|
+
): Promise<void> {
|
|
210
|
+
try {
|
|
211
|
+
await session.abort();
|
|
212
|
+
} catch (error) {
|
|
213
|
+
onDiagnostic(errorDiagnostic("Aborting the run failed", error));
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
try {
|
|
217
|
+
if (session.extensionRunner.hasHandlers("session_shutdown")) {
|
|
218
|
+
await session.extensionRunner.emit({ type: "session_shutdown", reason: "quit" });
|
|
219
|
+
}
|
|
220
|
+
} catch (error) {
|
|
221
|
+
onDiagnostic(errorDiagnostic("Shutting down the run's extensions failed", error));
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
unsubscribe();
|
|
225
|
+
session.dispose();
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* The run has no UI. Notifications become run diagnostics, and a dialog that an extension opens is
|
|
230
|
+
* answered as dismissed, with a diagnostic, instead of waiting for a user who cannot see it.
|
|
231
|
+
*/
|
|
232
|
+
function createHeadlessUi(
|
|
233
|
+
theme: Theme,
|
|
234
|
+
onDiagnostic: (diagnostic: Diagnostic) => void,
|
|
235
|
+
): ExtensionUIContext {
|
|
236
|
+
const dismissed = (kind: string, title: string): void => {
|
|
237
|
+
onDiagnostic({
|
|
238
|
+
level: "warning",
|
|
239
|
+
message: `An extension asked for input (${kind}: ${title}), but runs have no UI.`,
|
|
240
|
+
at: Date.now(),
|
|
241
|
+
});
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
return {
|
|
245
|
+
async select(title) {
|
|
246
|
+
dismissed("select", title);
|
|
247
|
+
return undefined;
|
|
248
|
+
},
|
|
249
|
+
async confirm(title) {
|
|
250
|
+
dismissed("confirm", title);
|
|
251
|
+
return false;
|
|
252
|
+
},
|
|
253
|
+
async input(title) {
|
|
254
|
+
dismissed("input", title);
|
|
255
|
+
return undefined;
|
|
256
|
+
},
|
|
257
|
+
notify(message, type = "info") {
|
|
258
|
+
onDiagnostic({ level: type, message, at: Date.now() });
|
|
259
|
+
},
|
|
260
|
+
onTerminalInput: () => () => {},
|
|
261
|
+
setStatus() {},
|
|
262
|
+
setWorkingMessage() {},
|
|
263
|
+
setWorkingVisible() {},
|
|
264
|
+
setWorkingIndicator() {},
|
|
265
|
+
setHiddenThinkingLabel() {},
|
|
266
|
+
setWidget() {},
|
|
267
|
+
setFooter() {},
|
|
268
|
+
setHeader() {},
|
|
269
|
+
setTitle() {},
|
|
270
|
+
async custom() {
|
|
271
|
+
dismissed("custom", "panel");
|
|
272
|
+
return undefined as never;
|
|
273
|
+
},
|
|
274
|
+
pasteToEditor() {},
|
|
275
|
+
setEditorText() {},
|
|
276
|
+
getEditorText: () => "",
|
|
277
|
+
async editor(title) {
|
|
278
|
+
dismissed("editor", title);
|
|
279
|
+
return undefined;
|
|
280
|
+
},
|
|
281
|
+
addAutocompleteProvider() {},
|
|
282
|
+
setEditorComponent() {},
|
|
283
|
+
getEditorComponent: () => undefined,
|
|
284
|
+
theme,
|
|
285
|
+
getAllThemes: () => [],
|
|
286
|
+
getTheme: () => undefined,
|
|
287
|
+
setTheme: () => ({ success: false, error: "Runs have no UI." }),
|
|
288
|
+
getToolsExpanded: () => false,
|
|
289
|
+
setToolsExpanded() {},
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
function extensionDiagnostic(error: ExtensionError): Diagnostic {
|
|
294
|
+
return {
|
|
295
|
+
level: "error",
|
|
296
|
+
message: `${error.extensionPath} failed in ${error.event}: ${error.error}`,
|
|
297
|
+
at: Date.now(),
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
export function errorDiagnostic(prefix: string, error: unknown): Diagnostic {
|
|
302
|
+
return {
|
|
303
|
+
level: "error",
|
|
304
|
+
message: `${prefix}: ${error instanceof Error ? error.message : String(error)}`,
|
|
305
|
+
at: Date.now(),
|
|
306
|
+
};
|
|
307
|
+
}
|