feat(os-notifier): notify when Pi awaits input
f811fc16bc562dac78b5582160665794f27bff07
Oliver Krylow <o.krylow@isp-insoft.de> ·
SSH signature · not checked
- Parent 1: ea36be0ae5ec
bugabingas pi extensions
f811fc16bc562dac78b5582160665794f27bff07
Oliver Krylow <o.krylow@isp-insoft.de> ·
SSH signature · not checked
+180 / −17 text lines
+8 / −4 text lines
Native-first OS notifications for direct interactive Pi completion.Native-first OS notifications when direct interactive Pi work completes or blocks awaiting input.- terminal unfocused at `agent_settled`- terminal unfocused when the blocking prompt starts or at `agent_settled`Focused completions are discarded rather than deferred.Notifications include outcome, duration, project, Git branch, session name, model, bounded user prompt, and bounded failure detail.+85 / −0 text lines
agentWaitingNotification, expect(state.isEligible()).toBe(true); expect(state.isEligible()).toBe(false); it("includes bounded context when Pi needs input", () => { expect( agentWaitingNotification( { kind: "confirm", title: "Allow destructive operation?" }, { cwd: "/home/oli/Workspace/dotfiles", branch: "trunk", sessionName: "notifier work", }, ), ).toEqual({ title: "Pi needs input", message: "Confirmation • Allow destructive operation? • dotfiles • trunk • notifier work", severity: "warning", }); }); it.skipIf(process.platform !== "linux")( "notifies once per uninterrupted unfocused interactive prompt wait", async () => { const extensionPath = path.resolve(import.meta.dirname, "../index.ts"); t = await createTestSession({ extensions: [extensionPath] }); const bin = join(t.cwd, "bin"); const log = join(t.cwd, "notify.log"); const executable = join(bin, "notify-send"); mkdirSync(bin); writeFileSync( executable, `#!${process.execPath}\nrequire("node:fs").appendFileSync(process.env.TEST_NOTIFY_LOG, JSON.stringify(process.argv.slice(2)) + "\\n");\n`, ); chmodSync(executable, 0o755); t.env.PATH = `${bin}${delimiter}${t.env.PATH ?? ""}`; t.env.TEST_NOTIFY_LOG = log; t.session.extensionRunner.setUIContext( t.session.extensionRunner.getUIContext(), "tui", ); focusState.focused = false; await t.session.extensionRunner.emit({ type: "input", text: "test notification", source: "interactive", } as never); await t.session.extensionRunner.emitBeforeAgentStart( "test notification", undefined, "BASE", {} as never, ); await t.session.extensionRunner.emit({ type: "agent_start" } as never); const prompt = { type: "ui_prompt_start", reason: "ui_prompt", kind: "confirm", title: "Continue?", } as const; await t.session.extensionRunner.emit(prompt as never); await waitFor( () => readFileSync(log, "utf8").trim().split("\n").length === 1, ); await t.session.extensionRunner.emit(prompt as never); await new Promise((resolve) => setTimeout(resolve, 50)); expect(readFileSync(log, "utf8").trim().split("\n")).toHaveLength(1); await t.session.extensionRunner.emit({ type: "ui_prompt_end", reason: "ui_prompt", kind: "confirm", title: "Continue?", } as never); await t.session.extensionRunner.emit(prompt as never); await waitFor( () => readFileSync(log, "utf8").trim().split("\n").length === 2, ); const args = JSON.parse(readFileSync(log, "utf8").trim().split("\n")[0]); expect(args).toContain("Pi needs input"); expect(args.join(" ")).toContain("Confirmation • Continue?"); }, );+87 / −13 text lines
export function agentWaitingNotification( event: { kind: "select" | "confirm" | "input" | "editor" | "custom"; title?: string; }, meta: { cwd?: string; branch?: string; sessionName?: string },): AgentDoneNotification { const kind = { select: "Selection", confirm: "Confirmation", input: "Input", editor: "Editor", custom: "Request", }[event.kind]; return { title: "Pi needs input", message: [ kind, compact(event.title, ERROR_LIMIT), meta.cwd ? compact(basename(meta.cwd)) : "", compact(meta.branch), compact(meta.sessionName), ] .filter(Boolean) .join(" • "), severity: "warning", };} let awaitingInput = false; const deliver = (notification: AgentDoneNotification): void => { void deliverNative( nativeCommand( process.platform, notification.title, notification.message, notification.severity, ), notification.title, notification.message, notification.severity, oscBackend(process.env), ); }; type PromptEvent = { kind: "select" | "confirm" | "input" | "editor" | "custom"; title?: string; }; const onPromptStart = pi.on as unknown as ( event: "ui_prompt_start", handler: ( event: PromptEvent, ctx: ExtensionContext, ) => Promise<void> | void, ) => void; const onPromptEnd = pi.on as unknown as ( event: "ui_prompt_end", handler: ( event: PromptEvent, ctx: ExtensionContext, ) => Promise<void> | void, ) => void; onPromptStart("ui_prompt_start", async (event, ctx) => { if (awaitingInput) return; awaitingInput = true; if (!eligibility.isEligible() || focus.isFocused()) return; deliver( agentWaitingNotification(event, { cwd: ctx.cwd, branch: currentGitBranch(ctx.cwd), sessionName: pi.getSessionName(), }), ); }); onPromptEnd("ui_prompt_end", async () => { awaitingInput = false; }); void deliverNative( nativeCommand( process.platform, notification.title, notification.message, notification.severity, ), notification.title, notification.message, notification.severity, oscBackend(process.env), ); deliver(notification); awaitingInput = false; awaitingInput = false;