feat: select subagent runner from config

This commit is contained in:
pi
2026-04-10 23:57:53 +01:00
parent e864c3fe52
commit cf9312c8d7
4 changed files with 158 additions and 61 deletions

View File

@@ -2,8 +2,13 @@ import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { createRunArtifacts } from "./src/artifacts.ts";
import { loadSubagentsConfig } from "./src/config.ts";
import { monitorRun } from "./src/monitor.ts";
import { listAvailableModelReferences } from "./src/models.ts";
import { createProcessSingleRunner } from "./src/process-runner.ts";
import { createConfiguredRunSingleTask } from "./src/runner.ts";
import { createSubagentParamsSchema } from "./src/schema.ts";
import { createSubagentTool } from "./src/tool.ts";
import { createTmuxSingleRunner } from "./src/tmux-runner.ts";
import {
buildCurrentWindowArgs,
@@ -12,31 +17,27 @@ import {
buildWrapperShellCommand,
isInsideTmux,
} from "./src/tmux.ts";
import { createSubagentParamsSchema } from "./src/schema.ts";
import { createSubagentTool } from "./src/tool.ts";
const packageRoot = dirname(fileURLToPath(import.meta.url));
const wrapperPath = join(packageRoot, "src", "wrapper", "cli.mjs");
export default function tmuxSubagentExtension(pi: ExtensionAPI) {
if (process.env.PI_TMUX_SUBAGENT_GITHUB_COPILOT_INITIATOR === "agent") {
export default function subagentsExtension(pi: ExtensionAPI) {
if (process.env.PI_SUBAGENTS_GITHUB_COPILOT_INITIATOR === "agent") {
pi.registerProvider("github-copilot", {
headers: { "X-Initiator": "agent" },
});
}
// In wrapper/child sessions spawned by the tmux runner we must not register the
// subagent tool (that would cause nested subagent registrations). Skip all
// subagent-tool registration logic when PI_TMUX_SUBAGENT_CHILD is set. Provider
// overrides (above) are still allowed in child runs, so the guard is placed
// after provider registration.
if (process.env.PI_TMUX_SUBAGENT_CHILD === "1") {
// In wrapper/child sessions spawned by subagent runners we must not register
// the subagent tool again. Provider overrides above are still allowed in child
// runs, so the guard stays after provider registration.
if (process.env.PI_SUBAGENTS_CHILD === "1") {
return;
}
let lastRegisteredModelsKey: string | undefined;
const runSingleTask = createTmuxSingleRunner({
const tmuxRunner = createTmuxSingleRunner({
assertInsideTmux() {
if (!isInsideTmux()) throw new Error("tmux-backed subagents require pi to be running inside tmux.");
},
@@ -58,6 +59,20 @@ export default function tmuxSubagentExtension(pi: ExtensionAPI) {
},
});
const processRunner = createProcessSingleRunner({
createArtifacts: createRunArtifacts,
buildWrapperSpawn(metaPath: string) {
return { command: process.execPath, args: [wrapperPath, metaPath] };
},
monitorRun,
});
const runSingleTask = createConfiguredRunSingleTask({
loadConfig: (cwd) => loadSubagentsConfig(cwd),
processRunner,
tmuxRunner,
});
const registerSubagentTool = (availableModels: string[]) => {
// Do not register a tool when no models are available. Remember that the
// last-registered key is different from the empty sentinel so that a later