Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134// Pi holds `!` output until the agent run ends, and it starts no turn when a `!` command ends while// the agent is idle (earendil-works/pi#10090, #8530). This extension fills that gap. Remove it when// Pi delivers `!` output at the next turn boundary and can start a turn after it.
import { stripVTControlCharacters } from "node:util";import { type BashOperations, type ContextEvent, convertToLlm, createLocalBashOperations, DEFAULT_MAX_BYTES, type ExtensionAPI, truncateTail,} from "@earendil-works/pi-coding-agent";import { Type } from "typebox";import { Check } from "typebox/value";
const OUTPUT = "user-bash-output";const DONE = "user-bash-done";// Pi keeps a rolling buffer of the same size before it truncates the output.const RAW_OUTPUT_LIMIT = 2 * DEFAULT_MAX_BYTES;
const OutputDetails = Type.Object({ command: Type.String() });
type Messages = ContextEvent["messages"];
/** Drops each `!` result that Pi records after the steered copy of the same command. */export function dropSteeredDuplicates(messages: Messages): Messages | undefined { const owed = new Map<string, number>(); const kept = messages.filter((message) => { if (message.role === "custom" && message.customType === OUTPUT) { if (Check(OutputDetails, message.details)) { const { command } = message.details; owed.set(command, (owed.get(command) ?? 0) + 1); } return true; } if (message.role !== "bashExecution" || message.excludeFromContext) { return true; } const count = owed.get(message.command) ?? 0; if (count === 0) { return true; } owed.set(message.command, count - 1); return false; }); return kept.length === messages.length ? undefined : kept;}
/** * Runs a `!` command locally. If the agent is busy when the command ends, the output goes to the * agent as a steering message. If the agent is idle, Pi records the output and a turn starts. */export function steeringOperations( command: string, pi: Pick<ExtensionAPI, "sendMessage">, isIdle: () => boolean, local: BashOperations = createLocalBashOperations(),): BashOperations { return { exec: async (resolvedCommand, cwd, options) => { const decoder = new TextDecoder(); let raw = ""; const result = await local.exec(resolvedCommand, cwd, { ...options, onData: (data) => { options.onData(data); raw += decoder.decode(data, { stream: true }); if (raw.length > 2 * RAW_OUTPUT_LIMIT) { raw = raw.slice(-RAW_OUTPUT_LIMIT); } }, }); const cancelled = options.signal?.aborted ?? false;
if (!isIdle()) { const output = stripVTControlCharacters(raw + decoder.decode()).replaceAll( "\r", "", ); const truncation = truncateTail(output); const [message] = convertToLlm([ { role: "bashExecution", command, output: truncation.content, exitCode: cancelled ? undefined : (result.exitCode ?? undefined), cancelled, truncated: truncation.truncated, timestamp: Date.now(), }, ]); if (message?.role === "user") { pi.sendMessage( { customType: OUTPUT, content: message.content, display: false, details: { command }, }, { deliverAs: "steer" }, ); } } else if (!cancelled) { // Pi records the output after this function returns. The turn must start after that. setTimeout(() => { pi.sendMessage( { customType: DONE, content: "Shell command finished.", display: false }, { triggerTurn: true }, ); }); } return result; }, };}
export default function userBashExtension(pi: ExtensionAPI): void { const local = createLocalBashOperations();
pi.on("user_bash", (event, ctx) => { if (event.excludeFromContext) { return undefined; } return { operations: steeringOperations(event.command, pi, () => ctx.isIdle(), local) }; });
pi.on("context", (event) => { const messages = dropSteeredDuplicates(event.messages); return messages ? { messages } : undefined; });}