UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

150 lines • 6.71 kB
import { isKeyRelease, matchesKey } from "@earendil-works/pi-tui"; import { convertToLlm, filterContextExcludedMessages } from "../../../messages.js"; import { buildSessionContext } from "../../../session-manager.js"; import { BtwPanel } from "./panel.js"; import { buildSideQueryContext, getSideQueryPromptContextWindow, runSideQuery } from "./side-query.js"; const WIDGET_KEY = "btw"; export default function btwExtension(pi) { let active; function dismiss(ctx, options) { const current = active; if (!current) return; active = undefined; if (options.abort) current.controller.abort(); current.unsubscribeEscape?.(); if (current.panel) ctx.ui.setWidget(WIDGET_KEY, undefined); } pi.on("session_before_switch", (_event, ctx) => { dismiss(ctx, { abort: true }); }); pi.on("session_before_fork", (_event, ctx) => { dismiss(ctx, { abort: true }); }); pi.on("session_shutdown", (_event, ctx) => { dismiss(ctx, { abort: true }); }); pi.on("input", (_event, ctx) => { if (active?.settled) dismiss(ctx, { abort: false }); }); pi.registerCommand("btw", { description: "Ask a side question in parallel without touching the main session", argumentHint: "<question>", requiresArguments: false, handler: async (args, ctx) => { const question = args.trim(); if (!question) { // Bare /btw is the explicit off switch: it closes the panel (or cancels an // in-flight side query) without interrupting the main turn the way Escape does. if (active) { dismiss(ctx, { abort: true }); return; } ctx.ui.notify("Usage: /btw <question>", "warning"); return; } const model = ctx.model; if (!model) { ctx.ui.notify("No active model available for /btw.", "error"); return; } const snapshot = buildSessionContext(ctx.sessionManager.getEntries(), ctx.sessionManager.getLeafId()); const history = convertToLlm(filterContextExcludedMessages(snapshot.messages)); const systemPrompt = ctx.getSystemPrompt(); const systemPromptStablePrefix = ctx.getSystemPromptStablePrefix?.(); const thinkingLevel = pi.getThinkingLevel(); const sessionId = ctx.sessionManager.getSessionId(); dismiss(ctx, { abort: true }); const controller = new AbortController(); const entry = { controller, panel: undefined, unsubscribeEscape: undefined, settled: false }; active = entry; if (ctx.mode === "tui" && ctx.hasUI) { ctx.ui.setWidget(WIDGET_KEY, (tui, theme) => { const panel = new BtwPanel(question, tui, theme); entry.panel = panel; return panel.component; }); entry.unsubscribeEscape = ctx.ui.onTerminalInput((data) => { // matchesKey accepts the raw byte plus kitty CSI-u / modifyOtherKeys encodings. // Ignore key releases: kitty CSI-u emits a release event after every press, // and this listener runs ahead of the TUI's release filter, so a release // whose press was consumed elsewhere would otherwise cancel the query. if (active !== entry || isKeyRelease(data) || !matchesKey(data, "escape")) return undefined; dismiss(ctx, { abort: true }); return undefined; }); } const auth = await ctx.modelRegistry.getApiKeyAndHeaders(model, { sessionId: ctx.sessionManager.getSessionId(), }); if (!auth.ok) { if (active !== entry) return; dismiss(ctx, { abort: false }); ctx.ui.notify(`/btw: ${auth.error}`, "error"); return; } try { const context = buildSideQueryContext({ systemPrompt, ...(systemPromptStablePrefix === undefined ? {} : { systemPromptStablePrefix }), history, question, promptContextWindow: getSideQueryPromptContextWindow(model), }); const { replyText } = await runSideQuery({ // The credential's own API host (a Copilot Business or Enterprise account) must // survive the explicit key below, as it does for the session's chat requests. model: auth.baseUrl ? { ...model, baseUrl: auth.baseUrl } : model, auth: { apiKey: auth.apiKey, headers: auth.headers, extraBody: auth.extraBody, }, sessionId, thinkingLevel: thinkingLevel === "off" ? undefined : thinkingLevel, streamFn: (streamModel, streamContext, options) => ctx.modelRegistry.modelRuntime.streamSimple(streamModel, streamContext, { ...options, cwd: ctx.cwd, }), }, context, { signal: controller.signal, onTextDelta: (delta) => { if (active === entry) entry.panel?.appendText(delta); }, }); if (active !== entry) return; entry.settled = true; if (entry.panel) { entry.panel.markDone(); } else { ctx.ui.notify(replyText, "info"); } } catch (error) { if (active !== entry) return; entry.settled = true; const message = error instanceof Error ? error.message : String(error); if (controller.signal.aborted) { entry.panel?.markAborted(); return; } if (entry.panel) { entry.panel.markError(message); } else { ctx.ui.notify(`/btw failed: ${message}`, "error"); } } }, }); } //# sourceMappingURL=index.js.map