@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
49 lines • 2.71 kB
JavaScript
import { modelOnlyText } from "../model-only-text.js";
export function displayPath(path) {
return /[\x00-\x1f\x7f]/.test(path) ? JSON.stringify(path) : path;
}
/** now supplies the elapsed duration, allowing deterministic text in renderer tests. */
export function formatGrepContent(result, { now = () => result.scan.elapsedMs } = {}) {
const blocks = [];
if (result.matches.length) {
for (const file of result.fileMatches) {
const rows = result.matches.filter((match) => match.path === file.path);
blocks.push([displayPath(file.path), ...rows.map((row) => `${row.line}${row.isContext ? "-" : ":"} ${row.text}`)].join("\n"));
}
}
else if (result.fileCount) {
blocks.push(result.fileMatches
.map((file) => (file.count === null ? displayPath(file.path) : `${displayPath(file.path)}: ${file.count}`))
.join("\n"));
}
else {
blocks.push(result.status === "pageEnd"
? `No more results (skip=${result.skip})`
: result.status === "partial"
? "No matches found in searched portion"
: "No matches found");
}
const notes = [];
const scan = result.scan;
if (scan.prefixSearched)
notes.push(`Partial coverage: searched only the first 4 MiB of ${scan.prefixSearched} large file(s); later matches are omitted.`);
if (scan.skippedOversized)
notes.push(`Skipped ${scan.skippedOversized} unreadable/unsearchable large file(s).`);
if (scan.skippedBinary)
notes.push(`Skipped ${scan.skippedBinary} binary file(s).`);
if (scan.missingPaths.length)
notes.push(`Skipped missing path(s): ${scan.missingPaths.map(displayPath).join(", ")}`);
if (scan.timedOut)
notes.push(scan.warnings.find((w) => w.code === "TIMED_OUT")?.message ??
"Timed out after 30000 ms; showing the completed ordered prefix.");
if (scan.regexEngine === "pcre2")
notes.push("Regex unsupported by the native engine; matched with ripgrep --pcre2.");
if (scan.patternKind === "literal")
notes.push(`Pattern searched literally: ${scan.effectivePattern}`);
if (scan.patternKind === "sanitized")
notes.push(`Pattern sanitized: ${scan.effectivePattern}`);
const footer = `[grep: matches=${result.matchCount ?? "n/a"} files=${result.fileCount} searched=${scan.filesSearched} elapsedMs=${Math.max(0, Math.round(now()))} engine=${result.engine} nextSkip=${result.nextSkip ?? "none"}]`;
const body = `${blocks.join("\n\n")}\n${notes.length ? `\n${notes.join("\n")}` : ""}`;
return [{ type: "text", text: body }, modelOnlyText(footer)];
}
//# sourceMappingURL=format.js.map