|
| 1 | +import { completeSimple } from '@mariozechner/pi-ai' |
| 2 | +import { resolvePiModel, toAgentHistory } from './pi-adapter' |
| 3 | +import { |
| 4 | + COMPACTION_SUMMARIZATION_PROMPT, |
| 5 | + COMPACTION_SUMMARY_PREFIX, |
| 6 | +} from './compaction' |
| 7 | +import type { LLMMessage, SummarizeCompleteFn } from './types' |
| 8 | + |
| 9 | +export type { SummarizeCompleteFn } |
| 10 | + |
| 11 | +const DEFAULT_SUMMARY_MAX_TOKENS = 2048 |
| 12 | + |
| 13 | +/** |
| 14 | + * Hard deadline for a single summarization request. |
| 15 | + * |
| 16 | + * pi-ai's anthropic provider applies a client-side timeout (and abort) ONLY |
| 17 | + * when the caller passes `timeoutMs`/`signal`, and it never retries. Background |
| 18 | + * compaction fires this call CONCURRENTLY with the agent's own streaming turn on |
| 19 | + * the same (OAuth) token; if that concurrent stream stalls, an unbounded call |
| 20 | + * hangs forever — wedging the pending slot and blocking all future attempts. |
| 21 | + * Bounding it turns a stall into a failure the caller can retry next turn-end. |
| 22 | + */ |
| 23 | +const DEFAULT_SUMMARY_TIMEOUT_MS = 120_000 |
| 24 | + |
| 25 | +/** |
| 26 | + * Summarize a conversation into a compaction handoff summary. |
| 27 | + * |
| 28 | + * Uses the conversation's own model by default: a cheaper, small-window model |
| 29 | + * would overflow on a near-full context — the whole reason we are compacting. |
| 30 | + * The full history is sent followed by Codex's summarization prompt; the summary |
| 31 | + * is prefixed with Codex's preamble so the resuming model knows it's a handoff. |
| 32 | + */ |
| 33 | +interface SummarizeCoreInput { |
| 34 | + model: string | object |
| 35 | + provider?: string |
| 36 | + apiKey?: string |
| 37 | + maxTokens?: number |
| 38 | + /** Hard deadline for the model call; defaults to {@link DEFAULT_SUMMARY_TIMEOUT_MS}. */ |
| 39 | + timeoutMs?: number |
| 40 | + complete?: SummarizeCompleteFn |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Core summarization over already-converted history messages (pi-agent's |
| 45 | + * `AgentMessage[]` shape). Appends Codex's summarization prompt, calls the |
| 46 | + * model, and prefixes the result. Both the LLMMessage path and the mid-turn |
| 47 | + * AgentMessage path funnel through here. |
| 48 | + */ |
| 49 | +async function summarizeConverted( |
| 50 | + historyMessages: ReadonlyArray<unknown>, |
| 51 | + input: SummarizeCoreInput |
| 52 | +): Promise<string> { |
| 53 | + const complete = |
| 54 | + input.complete ?? (completeSimple as unknown as SummarizeCompleteFn) |
| 55 | + const model = resolvePiModel({ |
| 56 | + model: input.model as never, |
| 57 | + ...(input.provider && { provider: input.provider as never }), |
| 58 | + }) |
| 59 | + const context = { |
| 60 | + messages: [ |
| 61 | + ...historyMessages, |
| 62 | + { |
| 63 | + role: `user`, |
| 64 | + content: COMPACTION_SUMMARIZATION_PROMPT, |
| 65 | + timestamp: Date.now(), |
| 66 | + }, |
| 67 | + ], |
| 68 | + } |
| 69 | + |
| 70 | + // Bound the call: pass `timeoutMs`/`signal` (which the anthropic provider |
| 71 | + // honours) AND race against a hard timer, so a stalled stream that ignores the |
| 72 | + // abort still rejects rather than hanging the background slot forever. |
| 73 | + const timeoutMs = input.timeoutMs ?? DEFAULT_SUMMARY_TIMEOUT_MS |
| 74 | + const controller = new AbortController() |
| 75 | + let timer: ReturnType<typeof setTimeout> | undefined |
| 76 | + const timeout = new Promise<never>((_, reject) => { |
| 77 | + timer = setTimeout(() => { |
| 78 | + controller.abort() |
| 79 | + reject(new Error(`[compaction] summarize timed out after ${timeoutMs}ms`)) |
| 80 | + }, timeoutMs) |
| 81 | + }) |
| 82 | + |
| 83 | + const call = complete(model, context, { |
| 84 | + maxTokens: input.maxTokens ?? DEFAULT_SUMMARY_MAX_TOKENS, |
| 85 | + ...(input.apiKey && { apiKey: input.apiKey }), |
| 86 | + signal: controller.signal, |
| 87 | + timeoutMs, |
| 88 | + }) |
| 89 | + // If the timeout wins the race, `call` rejects later (aborted) — swallow it so |
| 90 | + // the loser doesn't surface as an unhandled rejection. |
| 91 | + call.catch(() => {}) |
| 92 | + |
| 93 | + let res: Awaited<ReturnType<SummarizeCompleteFn>> |
| 94 | + try { |
| 95 | + res = await Promise.race([call, timeout]) |
| 96 | + } finally { |
| 97 | + if (timer) clearTimeout(timer) |
| 98 | + } |
| 99 | + |
| 100 | + const textBlock = res.content.find((block) => block.type === `text`) |
| 101 | + const text = textBlock && `text` in textBlock ? (textBlock.text ?? ``) : `` |
| 102 | + if (text.trim().length === 0) { |
| 103 | + throw new Error( |
| 104 | + `[compaction] empty summary (stopReason=${res.stopReason ?? `none`} error=${res.errorMessage ?? `none`})` |
| 105 | + ) |
| 106 | + } |
| 107 | + |
| 108 | + return `${COMPACTION_SUMMARY_PREFIX}\n${text}` |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * Summarize a conversation (LLMMessage form) into a compaction handoff summary. |
| 113 | + * |
| 114 | + * Uses the conversation's own model by default: a cheaper, small-window model |
| 115 | + * would overflow on a near-full context — the whole reason we are compacting. |
| 116 | + */ |
| 117 | +export async function summarizeMessages( |
| 118 | + input: SummarizeCoreInput & { messages: ReadonlyArray<LLMMessage> } |
| 119 | +): Promise<string> { |
| 120 | + return summarizeConverted(toAgentHistory([...input.messages]), input) |
| 121 | +} |
| 122 | + |
| 123 | +/** |
| 124 | + * Summarize already-converted `AgentMessage[]` (what `transformContext` hands |
| 125 | + * us mid-turn) — same as `summarizeMessages` but skips the LLMMessage→Agent |
| 126 | + * conversion since the messages are already in that shape. |
| 127 | + */ |
| 128 | +export async function summarizeAgentMessages( |
| 129 | + input: SummarizeCoreInput & { messages: ReadonlyArray<unknown> } |
| 130 | +): Promise<string> { |
| 131 | + return summarizeConverted(input.messages, input) |
| 132 | +} |
0 commit comments