// SPDX-License-Identifier: MIT // SPDX-FileCopyrightText: 2026 avtc /** * How a prompt tells the model to dispatch a subagent. The wording depends on the host that runs * the workflow, because each host has its own dispatch tool: * * - `pi` (default): the `subagent` tool, one call carrying one agent or a `tasks` list. * - `claude` (`PI_FY_HOST=claude`): the `Agent` tool, one call per agent, with the agent type * namespaced by the plugin. * * Every dispatch example and inline dispatch call in the prompt sources is built here, so the * host's syntax lives in one place. */ /** The host whose dispatch syntax the prompts use. */ type DispatchHost = "pi" | "claude"; /** One agent to dispatch and the task text shown in the example. */ export interface DispatchCall { agent: string; task: string; } /** The quote character an inline call is written with. */ type CallQuote = '"' | "'" | "`"; /** The layout of a multi-line dispatch block under pi. */ type BlockLayout = "single" | "single-template" | "list"; /** The dispatch examples the skill and agent markdown refer to by id. */ export type DispatchExampleId = "researcher" | "reviewer-researcher"; /** The namespace the Claude host registers the agent types under. */ const CLAUDE_AGENT_NAMESPACE = "featyard"; const CLAUDE_PARALLEL_NOTE = "// Send every Agent call in one message so the agents run in parallel."; const CLAUDE_WAIT_NOTE = "// Agents may run in the background: wait for every report before you continue."; const CLAUDE_NO_NESTING_NOTE = "// Nested dispatch is unavailable in this session: do the trace yourself."; function getDispatchHost(): DispatchHost { return process.env.PI_FY_HOST === "claude" ? "claude" : "pi"; } /** Whether a subagent may dispatch subagents of its own. A host without nesting sets the variable to `0`. */ export function nestedSubagentsEnabled(): boolean { return process.env.PI_FY_CAP_NESTED_SUBAGENTS !== "0"; } function piCall(call: DispatchCall, quote: CallQuote): string { return `subagent({ agent: ${quote}${call.agent}${quote}, task: ${quote}${call.task}${quote} })`; } /** * The Agent calls for one agent. A review agent follows the session's fork mode: a fork inherits * the conversation and takes its role from the marker that starts its prompt. */ function claudeCalls(call: DispatchCall, quote: CallQuote): string[] { const type = `${CLAUDE_AGENT_NAMESPACE}:${call.agent}`; const label = `description: ${quote}<3-5 word label>${quote}`; const fresh = `Agent({ subagent_type: ${quote}${type}${quote}, ${label}, prompt: ${quote}${call.task}${quote} })`; const fork = `Agent({ subagent_type: ${quote}fork${quote}, ${label}, prompt: ${quote}[${type}] ${call.task}${quote} })`; const mode = call.agent.includes("review") ? process.env.PI_SUBAGENT_FORK_MODE : undefined; if (mode === "fork") return [fork]; if (mode === "new+fork") return [fresh, fork]; return [fresh]; } /** A dispatch call written inline, to sit inside a sentence's code span. */ export function dispatchCall(call: DispatchCall, quote: CallQuote): string { if (getDispatchHost() === "pi") return piCall(call, quote); return claudeCalls(call, quote).join("` and `"); } /** A multi-line dispatch block: the lines that sit inside a code fence. */ export function dispatchBlock(calls: DispatchCall[], layout: BlockLayout): string { if (getDispatchHost() === "claude") { const agentCalls = calls.flatMap((call) => claudeCalls(call, '"')); const notes = agentCalls.length > 1 ? [CLAUDE_PARALLEL_NOTE, CLAUDE_WAIT_NOTE] : [CLAUDE_WAIT_NOTE]; return [...notes, ...agentCalls].join("\n"); } const first = calls[0]; if (layout === "single" && first) { return ["subagent({", ` tasks: [{ agent: "${first.agent}", task: "${first.task}" }]`, "})"].join("\n"); } if (layout === "single-template" && first) { return ["subagent({", ` tasks: [{ agent: "${first.agent}", task: \`${first.task}\` }],`, "})"].join("\n"); } return [ "subagent({", " tasks: [", ...calls.map((call) => ` { agent: "${call.agent}", task: "${call.task}" },`), " ],", "})", ].join("\n"); } const DISPATCH_EXAMPLES: Record = { researcher: { agent: "fy-researcher", task: "" }, "reviewer-researcher": { agent: "fy-researcher", task: ". Write to: -agent-N.md", }, }; /** The dispatch example a skill or agent body names by id. */ export function dispatchExample(id: DispatchExampleId): string { // A reviewer runs as a subagent: its own dispatch needs nesting. if (id === "reviewer-researcher" && getDispatchHost() === "claude" && !nestedSubagentsEnabled()) { return CLAUDE_NO_NESTING_NOTE; } return dispatchBlock([DISPATCH_EXAMPLES[id]], "single"); } /** How tool paths reach the worktree the feature works in. */ export function worktreeToolPathsNote(): string { if (getDispatchHost() === "claude") { return "The plugin redirects file and shell tools into the worktree automatically: a path under the main repo resolves inside the worktree, and shell commands start there."; } return "You are working in a git worktree. The extension rewrites all tool paths automatically — use relative paths as normal."; } /** How a dispatched subagent reaches the worktree. */ export function worktreeDispatchNote(worktreePath: string): string { if (getDispatchHost() === "claude") { return "Subagents are redirected into the worktree the same way. The Agent tool takes no working directory."; } return `All subagent dispatches MUST include \`cwd: "${worktreePath}"\` — this applies to all modes: single (\`cwd\`), parallel (\`tasks[].cwd\`), and chain (\`chain[].cwd\`). Subagents without \`cwd\` start in the main repo, not the worktree.`; } /** The warning shown when the worktree could not be created. */ export function worktreeCreationFailedNote(): string { if (getDispatchHost() === "claude") { return "⚠️ Worktree creation failed. You are working in the main repo directory."; } return "⚠️ Worktree creation failed. You are working in the main repo directory. Do NOT set cwd on subagent dispatches — let them inherit the main repo path."; }