import * as path from "node:path"; import type { SearchHit } from "../retrieval/search_index"; import { tokenizeCode } from "../retrieval/search_bm25"; export const CODE_SEARCH_MODES = ["auto", "full", "preview", "summary"] as const; export type CodeSearchMode = (typeof CODE_SEARCH_MODES)[number]; /** Maximum automatic response size in characters (before the tool footer). */ export const AUTO_OUTPUT_BUDGET = 8_000; const PREVIEW_LINE_LIMIT = 12; const PREVIEW_CHAR_LIMIT = 1_400; const AUTO_FULL_CHAR_LIMIT = 6_000; const AUTO_PREVIEW_HIT_LIMIT = 5; /** A previewed chunk this small is shown whole: a 12-line window often misses the relevant lines of a mid-size body. */ const PREVIEW_WHOLE_CHUNK_CHAR_LIMIT = 2_000; export interface VisibleCodeRange { chunkId: string; startLine: number; endLine: number; } export interface FormattedCodeSearchResults { text: string; mode: Exclude | "mixed"; truncated: boolean; visibleRanges: VisibleCodeRange[]; } function normalizePath(filePath: string): string { return filePath.replace(/\\/g, "/"); } function languageFor(filePath: string): string { return path.extname(filePath).slice(1) || "text"; } function compactSignature(signature: string): string { const normalized = signature.replace(/\s+/g, " ").trim(); return normalized.length > 220 ? `${normalized.slice(0, 217)}...` : normalized; } function hitHeader(hit: SearchHit): string { const chunk = hit.chunk; const signature = compactSignature(chunk.signature || ""); const signal = hit.signal ? ` [${hit.signal}]` : ""; const matches = hit.matches.length > 0 ? `; matches: ${hit.matches.slice(0, 5).join(", ")}` : ""; const headline = signature && signature !== chunk.symbolName ? ` — ${signature}` : ""; return `${normalizePath(chunk.filePath)}:${chunk.startLine}-${chunk.endLine} [${chunk.kind}] ${chunk.symbolName}${headline}${signal}${matches}`; } function fullHit(hit: SearchHit): string { const chunk = hit.chunk; return `${normalizePath(chunk.filePath)}:${chunk.startLine}-${chunk.endLine} [${chunk.kind}] ${chunk.symbolName} (${chunk.breadcrumb}):\n\`\`\`${languageFor(chunk.filePath)}\n${chunk.content}\n\`\`\``; } function summaryHit(hit: SearchHit): string { return hitHeader(hit); } function bestMatchingLine(lines: string[], queryTerms: string[]): number { if (lines.length === 0 || queryTerms.length === 0) return 0; let bestIndex = 0; let bestScore = 0; for (let index = 0; index < lines.length; index++) { const lowerLine = lines[index].toLowerCase(); const score = queryTerms.reduce( (total, term) => total + (lowerLine.includes(term) ? 1 : 0), 0, ); if (score > bestScore) { bestScore = score; bestIndex = index; } } return bestIndex; } /** * Return a bounded excerpt around the query-bearing line rather than always * returning the beginning of a large AST chunk. This preserves evidence for * the search decision while avoiding a forced full-file read in the common * multi-result case. */ function queryPreview( hit: SearchHit, query: string, ): { text: string; visibleRange?: VisibleCodeRange } { if (hit.chunk.content.length <= PREVIEW_WHOLE_CHUNK_CHAR_LIMIT) { return { text: `${hitHeader(hit)}\n\`\`\`${languageFor(hit.chunk.filePath)}\n${hit.chunk.content}\n\`\`\``, visibleRange: { chunkId: hit.chunk.id, startLine: hit.chunk.startLine, endLine: hit.chunk.endLine }, }; } const lines = hit.chunk.content.split("\n"); const terms = Array.from(new Set(tokenizeCode(query))); const bestLine = bestMatchingLine(lines, terms); const halfBefore = 2; let start = Math.max(0, bestLine - halfBefore); if (start + PREVIEW_LINE_LIMIT > lines.length) { start = Math.max(0, lines.length - PREVIEW_LINE_LIMIT); } const end = Math.min(lines.length, start + PREVIEW_LINE_LIMIT); const excerpt = lines.slice(start, end).join("\n"); const lineStart = hit.chunk.startLine + start; const lineEnd = hit.chunk.startLine + end - 1; const before = start > 0 ? `... lines ${hit.chunk.startLine}-${lineStart - 1} omitted ...\n` : ""; const after = end < lines.length ? `\n... lines ${lineEnd + 1}-${hit.chunk.endLine} omitted ...` : ""; const boundedExcerpt = `${before}${excerpt}${after}`; const clipped = boundedExcerpt.length > PREVIEW_CHAR_LIMIT; const previewBody = clipped ? `${boundedExcerpt.slice(0, PREVIEW_CHAR_LIMIT)}\n... preview clipped ...` : boundedExcerpt; return { text: `${hitHeader(hit)}\n\`\`\`${languageFor(hit.chunk.filePath)}\n${previewBody}\n\`\`\``, // A char-clipped body may omit most of its final line, so it cannot // authorize a complete source line. Omit the range instead of guessing. visibleRange: clipped ? undefined : { chunkId: hit.chunk.id, startLine: lineStart, endLine: lineEnd }, }; } function clipToBudget(text: string, budget: number): string { if (text.length <= budget) return text; if (budget <= 40) return text.slice(0, budget); return `${text.slice(0, budget - 27)}\n... output clipped ...`; } interface BudgetEntry { text: string; visibleRange?: VisibleCodeRange; } function applyBudget( entries: BudgetEntry[], budget: number, ): { text: string; truncated: boolean; visibleRanges: VisibleCodeRange[] } { const selected: string[] = []; const visibleRanges: VisibleCodeRange[] = []; let used = 0; let omitted = 0; let truncated = false; for (let index = 0; index < entries.length; index++) { const entry = entries[index]; if (!entry) continue; const separator = selected.length > 0 ? "\n\n" : ""; const candidateLength = separator.length + entry.text.length; if (used + candidateLength <= budget) { selected.push(entry.text); used += candidateLength; if (entry.visibleRange) visibleRanges.push(entry.visibleRange); continue; } if (selected.length === 0) { const clipped = clipToBudget(entry.text, budget); selected.push(clipped); used = clipped.length; truncated = clipped.length < entry.text.length; // A clipped entry cannot prove that its full declared source range // was returned, so its evidence range is intentionally omitted. omitted = entries.length - index - 1; } else { omitted = entries.length - index; truncated = true; } break; } if (omitted > 0) { const notice = `... ${omitted} additional match${omitted === 1 ? "" : "es"} omitted; use mode: "full" or a narrower query to inspect them ...`; const separator = selected.length > 0 ? "\n\n" : ""; const remaining = Math.max(0, budget - used - separator.length); if (remaining > 0) { const clippedNotice = clipToBudget(notice, remaining); selected.push(clippedNotice); truncated = truncated || clippedNotice.length < notice.length; } } return { text: selected.join("\n\n"), truncated, visibleRanges }; } function resolveAutoMode(hits: SearchHit[]): Exclude | "mixed" { if (hits.length === 1 && hits[0].chunk.content.length <= AUTO_FULL_CHAR_LIMIT) { return "full"; } if (hits.length <= 3) return "preview"; return "mixed"; } /** * Format search hits for the model. Explicit full mode preserves the legacy * response. Automatic mode is context-preserving: it previews a small number * of top hits, summarizes the rest, and enforces a total response budget. */ function decorateResult( result: FormattedCodeSearchResults, requestedMode: CodeSearchMode, hitCount: number, ): FormattedCodeSearchResults { if (result.mode === "full" && requestedMode === "full") { return result; } const notice = result.mode === "mixed" ? `${hitCount} matches returned; first ${Math.min(AUTO_PREVIEW_HIT_LIMIT, hitCount)} shown as previews, remaining ${hitCount - Math.min(AUTO_PREVIEW_HIT_LIMIT, hitCount)} summarized.` : ""; const prefix = notice ? `${notice}\n\n` : ""; const available = Math.max(0, AUTO_OUTPUT_BUDGET - prefix.length); const boundedText = clipToBudget(result.text, available); const finalClipped = boundedText.length < result.text.length; return { ...result, text: `${prefix}${boundedText}`, truncated: result.truncated || finalClipped, visibleRanges: finalClipped ? [] : result.visibleRanges, }; } /** * Format search hits for the model. Explicit full mode preserves the legacy * response. Automatic mode is context-preserving: it previews a small number * of top hits, summarizes the rest, and enforces a total response budget. */ export function formatCodeSearchResults( hits: SearchHit[], query: string, requestedMode: CodeSearchMode = "auto", ): FormattedCodeSearchResults { const autoMode = requestedMode === "auto" ? resolveAutoMode(hits) : requestedMode; if (autoMode === "full") { return decorateResult( { text: hits.map(fullHit).join("\n\n"), mode: "full", truncated: false, visibleRanges: hits.map((hit) => ({ chunkId: hit.chunk.id, startLine: hit.chunk.startLine, endLine: hit.chunk.endLine, })), }, requestedMode, hits.length, ); } if (autoMode === "summary") { const bounded = applyBudget( hits.map((hit) => ({ text: summaryHit(hit) })), AUTO_OUTPUT_BUDGET, ); return decorateResult({ ...bounded, mode: "summary" }, requestedMode, hits.length); } if (autoMode === "preview") { const bounded = applyBudget( hits.map((hit) => queryPreview(hit, query)), AUTO_OUTPUT_BUDGET, ); return decorateResult({ ...bounded, mode: "preview" }, requestedMode, hits.length); } // Automatic mode for many results: preserve useful body context for the // highest-ranked few hits and keep the rest as cheap navigational metadata. const topPreview = hits .slice(0, AUTO_PREVIEW_HIT_LIMIT) .map((hit) => queryPreview(hit, query)); const remainingSummary = hits .slice(AUTO_PREVIEW_HIT_LIMIT) .map((hit) => ({ text: summaryHit(hit) })); const bounded = applyBudget([...topPreview, ...remainingSummary], AUTO_OUTPUT_BUDGET); return decorateResult({ ...bounded, mode: "mixed" }, requestedMode, hits.length); }