All files / src/models summarizer.ts

94.11% Statements 80/85
87.5% Branches 35/40
87.5% Functions 7/8
93.82% Lines 76/81

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 2008x 8x   8x 8x 8x   8x               8x 8x     14x 14x 14x 14x 36x         36x               36x                   36x             36x 36x 36x 36x 36x 1x   36x 36x 36x     14x 14x 13x   1x       1x               8x               12x     12x 12x 12x       12x                           9x 9x 1x   8x         4x 2x   2x       2x 2x 2x                     8x       43x 43x 10x   10x 10x   10x 25x 25x 5x 5x   20x 20x           10x 4x             10x 10x 11x 11x 18x 18x 15x   3x   5x 3x   3x 1x 1x 1x         10x          
import { chatCompletion } from "../api/client";
import { debugLog } from "../shared/logging";
import { NimChatMessage } from "../types";
import { FALLBACK_MODEL_ID } from "./catalog";
import { estimateNimMessagesTokens, truncateMessagesForContext } from "../messages/converter";
import { ConfigManager } from "../shared/config";
 
const SUMMARIZATION_PROMPT = `Summarize the following conversation concisely, preserving:
- Key decisions and their rationale
- File paths, function names, and code references mentioned
- Important context about the user's intent and requirements
- Any unresolved questions or open tasks
 
Do not include greetings, pleasantries, or filler. Focus on technical substance.
Return only the summary, no meta-commentary.`;
const MAX_SUMMARIZATION_INPUT_CHARS = 48000;
const SUMMARIZATION_TRUNCATION_NOTICE = "\n[Earlier content clipped before summarization.]";
 
function messagesToText(messages: NimChatMessage[]): string {
  const lines: string[] = [];
  let remainingChars = MAX_SUMMARIZATION_INPUT_CHARS;
  let wasClipped = false;
  for (const [index, msg] of messages.entries()) {
    Iif (remainingChars <= 0) {
      wasClipped = index < messages.length;
      break;
    }
    const role =
      msg.role === "user"
        ? "User"
        : msg.role === "assistant"
          ? "Assistant"
          : msg.role === "tool"
            ? "Tool Result"
            : "System";
    const content =
      typeof msg.content === "string"
        ? msg.content
        : msg.content
            .map((part) => {
              Iif (part.type === "image_url") {
                return "[image omitted from summary]";
              }
              return JSON.stringify(part);
            })
            .join(" ");
    const metadata = [
      msg.reasoning_content ? `[reasoning]: ${msg.reasoning_content}` : "",
      msg.tool_call_id ? `[tool_call_id]: ${msg.tool_call_id}` : "",
      msg.tool_calls?.length ? `[tool_calls]: ${JSON.stringify(msg.tool_calls)}` : "",
    ]
      .filter(Boolean)
      .join(" ");
    const summaryContent = [content, metadata].filter(Boolean).join(" ");
    if (summaryContent.trim()) {
      const prefix = `[${role}]: `;
      const availableContentChars = Math.max(0, remainingChars - prefix.length);
      if (summaryContent.length > availableContentChars) {
        wasClipped = true;
      }
      const clippedContent = summaryContent.slice(0, availableContentChars);
      lines.push(`${prefix}${clippedContent}`);
      remainingChars -= prefix.length + clippedContent.length;
    }
  }
  const result = lines.join("\n\n");
  if (!wasClipped) {
    return result;
  }
  const bodyLimit = Math.max(
    0,
    MAX_SUMMARIZATION_INPUT_CHARS - SUMMARIZATION_TRUNCATION_NOTICE.length,
  );
  return `${result.slice(0, bodyLimit)}${SUMMARIZATION_TRUNCATION_NOTICE}`;
}
 
/**
 * Summarize old conversation messages via a lightweight API call.
 * Uses dedicated summarization model from config or custom argument.
 * Falls back to simple truncation if the API call fails.
 */
export async function summarizeOldMessages(
  oldMessages: NimChatMessage[],
  apiKey: string,
  userAgent: string,
  signal?: AbortSignal,
  summarizationModel?: string,
): Promise<NimChatMessage> {
  const targetModel =
    summarizationModel?.trim() ||
    ConfigManager.getContextConfig().summarizationModel ||
    FALLBACK_MODEL_ID;
  const conversationText = messagesToText(oldMessages);
  try {
    debugLog(
      "summarizer",
      `Summarizing ${oldMessages.length} messages (${conversationText.length} chars) via ${targetModel}.`,
    );
    const summary = await chatCompletion(
      apiKey,
      {
        model: targetModel,
        messages: [
          { role: "system", content: SUMMARIZATION_PROMPT },
          { role: "user", content: conversationText },
        ],
        max_tokens: 4096,
        temperature: 0,
      },
      signal,
      userAgent,
    );
    const trimmedSummary = summary.trim();
    if (!trimmedSummary) {
      throw new Error("Summarization returned an empty response");
    }
    return {
      role: "system",
      content: `[Previous conversation summary]: ${trimmedSummary}`,
    };
  } catch (error) {
    if (error instanceof Error && error.name === "AbortError") {
      throw error;
    }
    debugLog(
      "summarizer",
      `API summarization failed on ${targetModel}, using simple truncation: ${error instanceof Error ? error.message : String(error)}`,
    );
    const truncated = truncateMessagesForContext(oldMessages, 8192);
    const truncatedText = messagesToText(truncated);
    return {
      role: "system",
      content: `[Previous conversation — truncated due to context limits]:\n${truncatedText}`,
    };
  }
}
 
/**
 * Split messages into "old" (to summarize) and "recent" (to keep verbatim).
 * Preserves system messages in the recent portion.
 */
export function splitMessagesForSummarization(
  messages: NimChatMessage[],
  maxRecentTokens: number,
): { oldMessages: NimChatMessage[]; recentMessages: NimChatMessage[] } {
  const systemMessages = messages.filter((message) => message.role === "system");
  const nonSystemMessages = messages.filter((message) => message.role !== "system");
  const recentBudget = Math.max(1, maxRecentTokens);
 
  let recentTokenCount = 0;
  let splitIndex = nonSystemMessages.length;
 
  for (let i = nonSystemMessages.length - 1; i >= 0; i -= 1) {
    const messageTokens = estimateNimMessagesTokens([nonSystemMessages[i]]);
    if (recentTokenCount + messageTokens > recentBudget) {
      splitIndex = i + 1;
      break;
    }
    recentTokenCount += messageTokens;
    splitIndex = i;
  }
 
  // Keep at least one historical non-system message available for a summary
  // when there is enough history to split, even if the requested recent budget
  // is larger than this small test/conversation.
  if (splitIndex === 0 && nonSystemMessages.length > 1) {
    splitIndex = 1;
  }
 
  // A tool result is only valid when the corresponding assistant tool call is
  // present in the same request. If the token boundary lands between the two,
  // keep the owner in the recent suffix even when that exceeds the nominal
  // recent budget slightly.
  let pairAdjusted = true;
  while (pairAdjusted) {
    pairAdjusted = false;
    for (let i = splitIndex; i < nonSystemMessages.length; i += 1) {
      const message = nonSystemMessages[i];
      if (message.role !== "tool" || !message.tool_call_id) {
        continue;
      }
      const ownerIndex = nonSystemMessages.findIndex(
        (candidate) =>
          candidate.role === "assistant" &&
          candidate.tool_calls?.some((call) => call.id === message.tool_call_id),
      );
      if (ownerIndex >= 0 && ownerIndex < splitIndex) {
        splitIndex = ownerIndex;
        pairAdjusted = true;
        break;
      }
    }
  }
 
  return {
    oldMessages: nonSystemMessages.slice(0, splitIndex),
    recentMessages: [...systemMessages, ...nonSystemMessages.slice(splitIndex)],
  };
}