// installed by dingger // managed by dingger; reinstalling or updating the integration overwrites this file. // add custom hooks/plugins beside this file instead of editing it. import net from "node:net"; // The dingger hook receiver listens here; override with DINGGER_AGENT_SOCKET. const socketPath = process.env.DINGGER_AGENT_SOCKET || `${process.env.HOME}/.local/state/dingger/agent.sock`; // tmux sets TMUX_PANE in the environment of every process it starts in a // pane, so it names exactly the pane this pi runs in. (Asking tmux for "the // current pane" is wrong: that is the session's active pane, not ours.) const paneId = process.env.DINGGER_PANE_ID || process.env.TMUX_PANE; const source = "dingger:pi"; function enabled() { return !!paneId; } function sendRequestAttempt(request: unknown, timeoutMs: number): Promise { if (!enabled()) { return Promise.resolve(true); } return new Promise((resolve) => { let done = false; let timeout: ReturnType | undefined; const finish = (delivered: boolean) => { if (done) return; done = true; if (timeout) { clearTimeout(timeout); } socket.destroy(); resolve(delivered); }; const socket = net.createConnection(socketPath); socket.on("error", () => finish(false)); socket.on("connect", () => socket.write(`${JSON.stringify(request)}\n`)); socket.on("data", () => finish(true)); socket.on("end", () => finish(false)); timeout = setTimeout(() => finish(false), timeoutMs); timeout.unref?.(); }); } async function sendRequest(request: unknown): Promise { if (await sendRequestAttempt(request, 500)) { return; } await sendRequestAttempt(request, 1500); } type AgentState = "working" | "blocked" | "idle"; // The session file, so `dingger restore` can resume this session with // `pi --session `. It changes with /new, /resume, and forks. let sessionFile: string | undefined; function updateSessionFile(ctx: any): void { try { const file = ctx?.sessionManager?.getSessionFile?.(); sessionFile = typeof file === "string" && file.length > 0 ? file : undefined; } catch { sessionFile = undefined; } } function report(state: AgentState, message?: string): Promise { return sendRequest({ id: `${source}:${Date.now()}:${Math.random().toString(36).slice(2)}`, method: "pane.report_agent", params: { pane_id: paneId!, source, agent: "pi", state, message, seq: Date.now() * 1000, ...(sessionFile ? { agent_session_path: sessionFile } : {}), }, }); } let sendInFlight = false; let queuedState: { state: AgentState; message?: string } | undefined; function queueState(state: AgentState, message?: string): void { queuedState = { state, message }; if (!sendInFlight) { void drainStateQueue(); } } async function drainStateQueue(): Promise { if (sendInFlight) { return; } sendInFlight = true; try { while (queuedState) { const next = queuedState; queuedState = undefined; await report(next.state, next.message); } } finally { sendInFlight = false; if (queuedState) { void drainStateQueue(); } } } export default function (pi) { if (!enabled()) { return; } let agentActive = false; let blockedCount = 0; let blockedMessage: string | undefined; let lastState: AgentState | undefined; let lastMessage: string | undefined; function desiredState() { if (blockedCount > 0) { return { state: "blocked" as const, message: blockedMessage }; } if (agentActive) { return { state: "working" as const, message: undefined }; } return { state: "idle" as const, message: undefined }; } function publishState(force = false) { const next = desiredState(); if (!force && next.state === lastState && next.message === lastMessage) { return; } lastState = next.state; lastMessage = next.message; queueState(next.state, next.message); } pi.events.on("blocked", (data) => { if (!data?.active) { blockedCount = Math.max(0, blockedCount - 1); if (blockedCount === 0) { blockedMessage = undefined; } publishState(); return; } blockedCount += 1; blockedMessage = data.label; publishState(); }); pi.on("session_start", async (event, ctx) => { // TUI only: RPC/JSON/print modes are headless. if (ctx?.mode !== "tui") { return; } // A reload can replace this extension mid-run without emitting another agent_start. agentActive = ctx?.isIdle?.() === false; updateSessionFile(ctx); publishState(true); }); pi.on("agent_start", (_event, ctx) => { agentActive = true; updateSessionFile(ctx); publishState(); }); pi.on("agent_settled", (_event, ctx) => { if (ctx?.isIdle?.() !== true) { return; } agentActive = false; publishState(); }); }