Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115import assert from "node:assert/strict";import test from "node:test";import type { BashOperations, ContextEvent } from "@earendil-works/pi-coding-agent";import { dropSteeredDuplicates, steeringOperations } from "../index.ts";
interface Sent { message: { customType: string; content: unknown; details?: unknown }; options?: { triggerTurn?: boolean; deliverAs?: string };}
function bashExecution(command: string, excludeFromContext = false) { return { role: "bashExecution" as const, command, output: "out", exitCode: 0, cancelled: false, truncated: false, timestamp: 0, excludeFromContext, };}
async function run(idle: boolean) { const sent: Sent[] = []; const forwarded: string[] = []; const local: BashOperations = { exec: async (_command, _cwd, options) => { options.onData(Buffer.from("\x1b[31mhello\x1b[0m\r\n")); return { exitCode: 2 }; }, }; const pi = { sendMessage(message: Sent["message"], options?: Sent["options"]) { sent.push({ message, options }); }, }; const operations = steeringOperations("make", pi, () => idle, local); const result = await operations.exec("make", "/", { onData: (data) => forwarded.push(data.toString()), }); return { result, sent, forwarded };}
test("steers the output of a command that ends while the agent is busy", async () => { const { result, sent, forwarded } = await run(false);
assert.deepEqual(result, { exitCode: 2 }); assert.deepEqual(forwarded, ["\x1b[31mhello\x1b[0m\r\n"]); assert.deepEqual(sent, [ { message: { customType: "user-bash-output", content: [ { type: "text", text: "Ran `make`\n```\nhello\n\n```\n\nCommand exited with code 2", }, ], display: false, details: { command: "make" }, }, options: { deliverAs: "steer" }, }, ]);});
test("starts a turn after a command ends while the agent is idle", async () => { const { sent } = await run(true); assert.deepEqual(sent, []);
await new Promise((resolve) => setTimeout(resolve)); assert.deepEqual(sent, [ { message: { customType: "user-bash-done", content: "Shell command finished.", display: false, }, options: { triggerTurn: true }, }, ]);});
test("drops the deferred copy of each steered command", () => { const steered = { role: "custom" as const, customType: "user-bash-output", content: "steered", display: false, details: { command: "make" }, timestamp: 0, }; const prompt = { role: "user" as const, content: "next", timestamp: 0 }; const messages: ContextEvent["messages"] = [ bashExecution("make"), steered, prompt, bashExecution("make", true), bashExecution("ls"), bashExecution("make"), bashExecution("make"), ];
assert.deepEqual(dropSteeredDuplicates(messages), [ bashExecution("make"), steered, prompt, bashExecution("make", true), bashExecution("ls"), bashExecution("make"), ]); assert.equal(dropSteeredDuplicates([bashExecution("make")]), undefined);});