Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596import { buildSessionContext, convertToLlm, type ExtensionContext,} from "@earendil-works/pi-coding-agent";
export type ParentContext = { brief: string } | { snapshot: true };
export function parentContext(ctx: ExtensionContext, option?: ParentContext): string | undefined { if (!option) { return undefined; } if ("brief" in option) { return option.brief; } const branch = ctx.sessionManager.getBranch(); const messages = convertToLlm(buildSessionContext(branch).messages); const completeResults = new Set( messages .filter((message) => message.role === "toolResult") .map((message) => message.toolCallId), ); const transcript: string[] = []; const includedCalls = new Set<string>(); for (const message of messages) { if (message.role === "system") { continue; } if (message.role === "toolResult" && !includedCalls.has(message.toolCallId)) { continue; } // Stop before the launch turn. Its sibling calls can still be unfinished. if ( message.role === "assistant" && message.content.some( (part) => part.type === "toolCall" && !completeResults.has(part.id), ) ) { break; } if (!Array.isArray(message.content)) { transcript.push(`${message.role}: ${message.content}`); continue; } const content: string[] = []; for (const part of message.content) { if (part.type === "text") { content.push(part.text); } else if (part.type === "toolCall") { includedCalls.add(part.id); content.push(`Tool ${part.name}: ${JSON.stringify(part.arguments)}`); } else if (part.type === "image") { throw new Error( "Parent snapshots support text only. Supply a context brief for conversations with images.", ); } // Thinking, signatures, and opaque provider state never enter a child snapshot. } if (content.length) { transcript.push(`${message.role}: ${content.join("\n")}`); } } return transcript.join("\n\n");}
/** Resolves an exact provider/model selector or the current model ID. */export function resolveModel(ctx: ExtensionContext, selector: string | undefined) { const slash = selector?.indexOf("/") ?? -1; return slash >= 0 && selector ? ctx.modelRegistry.find(selector.slice(0, slash), selector.slice(slash + 1)) : ctx.model?.id === selector ? ctx.model : undefined;}
export function checkContext( ctx: ExtensionContext, selector: string | undefined, text: string, prompt: string,): void { const model = resolveModel(ctx, selector); if (!model) { throw new Error( "Seeded context requires an exact registered provider/model ID or the current model ID. Pi does not expose its CLI model resolver through the extension registry.", ); } // UTF-8 bytes are a conservative text-token upper bound. Leave space for tools and a report. const available = model.contextWindow - Math.max(32_000, model.contextWindow * 0.2); if (Buffer.byteLength(text) + Buffer.byteLength(prompt) > available) { throw new Error( `Parent context exceeds the child context budget (${Math.max(0, available)} bytes). Supply a smaller context brief. No content was truncated.`, ); }}