




















@@ -40,6 +40,22 @@ const IDENTIFIER_PRESERVATION_INSTRUCTIONS =
4040"Preserve all opaque identifiers exactly as written (no shortening or reconstruction), " +
4141"including UUIDs, hashes, IDs, hostnames, IPs, ports, URLs, and file names.";
424243+const HANDOFF_INSTRUCTIONS = [
44+"Generate a concise recovery briefing for a new LLM taking over this session.",
45+"The previous model hit a quota limit and you are providing the context for a smooth handoff.",
46+"",
47+"LEADER HIERARCHY REINFORCEMENT:",
48+"- Explicitly state that the new model is the LEADER (Orchestrator).",
49+"- Identify any active autonomous units (like AutoClaw) as SUBORDINATES.",
50+"- Instruct the new model to NOT perform the subordinate's task, but to supervise and provide strategic commands.",
51+"",
52+"MUST CAPTURE:",
53+"- Current high-level goal and project path.",
54+"- Status of the latest tool executions (especially AutoClaw/Subagents).",
55+"- Critical files currently being modified.",
56+"- Pending items and next intended steps.",
57+].join("\n");
58+4359export type CompactionSummarizationInstructions = {
4460identifierPolicy?: AgentCompactionIdentifierPolicy;
4561identifierInstructions?: string;
@@ -518,6 +534,7 @@ export function pruneHistoryForContextShare(params: {
518534maxContextTokens: number;
519535maxHistoryShare?: number;
520536parts?: number;
537+mode?: "share" | "handoff";
521538}): {
522539messages: AgentMessage[];
523540droppedMessagesList: AgentMessage[];
@@ -527,7 +544,9 @@ export function pruneHistoryForContextShare(params: {
527544keptTokens: number;
528545budgetTokens: number;
529546} {
530-const maxHistoryShare = params.maxHistoryShare ?? 0.5;
547+const isHandoff = params.mode === "handoff";
548+const defaultShare = isHandoff ? 0.2 : 0.5; // Stricter budget for handoff snapshots
549+const maxHistoryShare = params.maxHistoryShare ?? defaultShare;
531550const budgetTokens = Math.max(1, Math.floor(params.maxContextTokens * maxHistoryShare));
532551let keptMessages = params.messages;
533552const allDroppedMessages: AgentMessage[] = [];
@@ -577,6 +596,36 @@ export function pruneHistoryForContextShare(params: {
577596};
578597}
579598599+/**
600+ * Generates a concise handoff summary for model transitions, enforcing a 4000 token limit.
601+ */
602+export async function summarizeForHandoff(params: {
603+messages: AgentMessage[];
604+model: NonNullable<ExtensionContext["model"]>;
605+apiKey: string;
606+headers?: Record<string, string>;
607+signal: AbortSignal;
608+maxChunkTokens: number;
609+contextWindow: number;
610+customInstructions?: string;
611+summarizationInstructions?: CompactionSummarizationInstructions;
612+}): Promise<string> {
613+const custom = params.customInstructions?.trim();
614+const handoffInstructions = custom
615+ ? `${HANDOFF_INSTRUCTIONS}\n\n${custom}`
616+ : HANDOFF_INSTRUCTIONS;
617+618+// Use a hard cap of 4000 tokens for the handoff summary as per plan
619+const handoffMaxTokens = 4000;
620+621+return summarizeWithFallback({
622+ ...params,
623+reserveTokens: SUMMARIZATION_OVERHEAD_TOKENS,
624+maxChunkTokens: Math.min(params.maxChunkTokens, handoffMaxTokens),
625+customInstructions: handoffInstructions,
626+});
627+}
628+580629export function resolveContextWindowTokens(model?: ExtensionContext["model"]): number {
581630const effective =
582631(model as { contextTokens?: number } | undefined)?.contextTokens ?? model?.contextWindow;
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。