UNPKG

@code-yeongyu/senpi

Version:

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

267 lines • 12.1 kB
import { resolvePromptCacheTtlSeconds } from "@earendil-works/pi-ai"; import { CACHE_WARM_PING_EVENT } from "../cache-keepalive/index.js"; import { baseThresholdRatioForWindow } from "../compaction/policy.js"; import { readGoal } from "../goal/store.js"; import { goalStoreRef } from "../goal/store-ref.js"; import { buildHandoffBrief } from "./brief.js"; import { estimateHandoff, hintText } from "./estimate.js"; import { createReplacementRelay } from "./replacement-relay.js"; export const HANDOFF_HINT_ENTRY_TYPE = "handoff-hint"; export const HANDOFF_BRIEF_MESSAGE_TYPE = "handoff-brief"; export const HANDOFF_HINT_EVENT = "handoff_hint"; export const HANDOFF_HINT_WIDGET_KEY = "handoff-hint"; export const HANDOFF_COMMAND = "handoff"; export const START_FRESH_LABEL = "Start fresh with a handoff"; export const KEEP_GOING_LABEL = "Keep going"; export const DEFAULT_HANDOFF_HINT_SETTINGS = { enabled: true, minContextTokens: 200_000, minSavingUsd: 1 }; function settingsOf(pi) { const configured = pi.getSettings().handoffHint; return { enabled: configured?.enabled ?? DEFAULT_HANDOFF_HINT_SETTINGS.enabled, minContextTokens: configured?.minContextTokens ?? DEFAULT_HANDOFF_HINT_SETTINGS.minContextTokens, minSavingUsd: configured?.minSavingUsd ?? DEFAULT_HANDOFF_HINT_SETTINGS.minSavingUsd, }; } function promptTokensOfFirstReply(entries) { for (const entry of entries) { if (entry.type !== "message" || entry.message.role !== "assistant") continue; const usage = entry.message.usage; const tokens = usage.contextUsage === undefined ? usage.input + usage.cacheRead + usage.cacheWrite : (usage.contextUsage?.input ?? 0); if (tokens > 0) return tokens; } return undefined; } function lastReplyAtMs(entries) { for (let index = entries.length - 1; index >= 0; index -= 1) { const entry = entries[index]; if (entry?.type === "message" && entry.message.role === "assistant") return entry.message.timestamp; } return undefined; } function wasDismissed(entries) { return entries.some((entry) => entry.type === "custom" && entry.customType === HANDOFF_HINT_ENTRY_TYPE && entry.data?.phase === "dismissed"); } const readActiveGoal = (ctx) => readGoal(goalStoreRef(ctx.sessionManager, ctx.cwd)).catch(() => null); export function createHandoffHintExtension(dependencies = {}) { const now = dependencies.now ?? Date.now; const goalOf = dependencies.readGoal ?? readActiveGoal; const acceptances = createReplacementRelay(); return (pi) => { let ctx; let timer; let lastRequestAtMs; let humanStartedTurn = false; let dismissed = false; let shownThisCrossing = false; let visible; const worker = pi.sessionKind === "worker"; function hintEvent(action, current, estimate) { const model = current.model; return { action, sessionId: current.sessionManager.getSessionId(), provider: model?.provider ?? "", model: model?.id ?? "", contextTokens: estimate.contextTokens, nextReplyUsd: estimate.nextReplyUsd, freshUsd: estimate.freshUsd, savingUsd: estimate.savingUsd, subscription: model !== undefined && current.modelRegistry.isUsingOAuth(model), }; } function emit(action, current, estimate) { pi.events?.emit(HANDOFF_HINT_EVENT, hintEvent(action, current, estimate)); } function render(current, estimate) { const subscription = current.model !== undefined && current.modelRegistry.isUsingOAuth(current.model); visible = estimate; current.ui.setWidget(HANDOFF_HINT_WIDGET_KEY, [hintText(estimate, subscription)], { placement: "aboveEditor", actions: [ { label: START_FRESH_LABEL, command: `/${HANDOFF_COMMAND}` }, { label: KEEP_GOING_LABEL, command: `/${HANDOFF_COMMAND} keep` }, ], }); } function disarm() { if (timer !== undefined) clearTimeout(timer); timer = undefined; } function hide(current) { if (visible === undefined) return; visible = undefined; current?.ui.setWidget(HANDOFF_HINT_WIDGET_KEY, undefined); } async function eligibleEstimate(current) { const settings = settingsOf(pi); const model = current.model; if (!settings.enabled || dismissed || worker || !humanStartedTurn || model === undefined) return undefined; if (current.mode !== "tui" && current.mode !== "rpc") return undefined; if (!current.hasUI || !current.isIdle() || current.hasPendingMessages() || current.isCompacting?.()) return undefined; const usage = current.getContextUsage(); const tokens = usage?.tokens; if (usage === undefined || tokens === null || tokens === undefined) return undefined; if (tokens < settings.minContextTokens) { shownThisCrossing = false; return undefined; } if (shownThisCrossing) return undefined; if (usage.contextWindow > 0 && tokens >= usage.contextWindow * baseThresholdRatioForWindow(usage.contextWindow)) return undefined; const goal = await goalOf(current); if (goal?.status === "active") return undefined; const fresh = promptTokensOfFirstReply(current.sessionManager.getFullBranch()) ?? tokens; const estimate = estimateHandoff(model, tokens, fresh, now()); if (estimate === undefined || estimate.savingUsd < settings.minSavingUsd) return undefined; return estimate; } async function showIfDue() { timer = undefined; const current = ctx; if (current === undefined || visible !== undefined) return; const estimate = await eligibleEstimate(current); if (estimate === undefined || ctx !== current || !current.isIdle()) return; shownThisCrossing = true; render(current, estimate); emit("shown", current, estimate); } function arm() { disarm(); const current = ctx; const model = current?.model; if (current === undefined || model === undefined || lastRequestAtMs === undefined) return; if (dismissed || worker || !settingsOf(pi).enabled) return; const ttlSeconds = resolvePromptCacheTtlSeconds(model); if (ttlSeconds === undefined) return; const delayMs = Math.max(0, lastRequestAtMs + ttlSeconds * 1000 - now()); timer = setTimeout(() => void showIfDue(), delayMs); } async function startFresh(current) { const estimate = visible; const usage = current.getContextUsage(); const goal = await goalOf(current); const brief = buildHandoffBrief({ entries: current.sessionManager.getFullBranch(), goal, cwd: current.cwd, modelLabel: current.model ? `${current.model.provider}/${current.model.id}` : undefined, previousSessionFile: current.sessionManager.getSessionFile(), contextTokens: usage?.tokens ?? estimate?.contextTokens ?? 0, }); const sessionId = current.sessionManager.getSessionId(); const previousSessionFile = current.sessionManager.getSessionFile(); // The replacement's session_start emits it through the new generation's pi (senpi#4051). if (estimate !== undefined) acceptances.hold(previousSessionFile, hintEvent("accepted", current, estimate)); hide(current); const switching = current.newSession({ parentSession: previousSessionFile, withSession: async (next) => { await next.sendMessage({ customType: HANDOFF_BRIEF_MESSAGE_TYPE, content: brief, display: true }, { triggerTurn: false }); }, }); const result = await switching.finally(() => acceptances.release(previousSessionFile)); if (result.cancelled && estimate !== undefined && current.sessionManager.getSessionId() === sessionId) render(current, estimate); } pi.registerCommand(HANDOFF_COMMAND, { description: "Start a fresh session seeded with a short handoff brief (`/handoff keep` hides the hint)", argumentHint: "[keep]", handler: async (args, current) => { const argument = args.trim(); if (argument !== "" && argument !== "keep") { current.ui.notify(`Unknown /${HANDOFF_COMMAND} argument "${argument}". Use /${HANDOFF_COMMAND} to start fresh or /${HANDOFF_COMMAND} keep to keep going.`, "warning"); return; } if (argument === "keep") { const estimate = visible; dismissed = true; disarm(); hide(current); pi.appendEntry(HANDOFF_HINT_ENTRY_TYPE, { phase: "dismissed" }); if (estimate !== undefined) emit("dismissed", current, estimate); return; } await startFresh(current); }, }); pi.on("session_start", (event, next) => { ctx = next; const accepted = event.reason === "new" ? acceptances.take(event.previousSessionFile) : undefined; if (accepted !== undefined) pi.events?.emit(HANDOFF_HINT_EVENT, accepted); disarm(); visible = undefined; shownThisCrossing = false; const branch = next.sessionManager.getFullBranch(); dismissed = wasDismissed(branch); // A session opened with history (a resume, `--continue`, a reload) belongs to whoever opened it, and its // cache aged from the last reply. Arm from there, so reopening a big session the next day warns before // the first reply pays the cold resend. An unattended driver starts a turn at once, which hides the hint // and marks the next turn extension-owned. lastRequestAtMs = lastReplyAtMs(branch); humanStartedTurn = lastRequestAtMs !== undefined; arm(); }); pi.on("input", (event) => { humanStartedTurn = event.source !== "extension"; disarm(); hide(ctx); return { action: "continue" }; }); pi.on("agent_start", () => { disarm(); hide(ctx); }); pi.on("agent_end", (_event, next) => { ctx = next; lastRequestAtMs = now(); }); pi.on("agent_settled", (_event, next) => { ctx = next; arm(); }); pi.on("session_compact", () => { hide(ctx); arm(); }); pi.on("model_select", (_event, next) => { ctx = next; arm(); }); pi.events?.on(CACHE_WARM_PING_EVENT, () => { lastRequestAtMs = now(); if (timer !== undefined) arm(); }); pi.on("session_shutdown", () => { disarm(); ctx = undefined; }); }; } export default createHandoffHintExtension(); //# sourceMappingURL=index.js.map