@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
65 lines • 3.03 kB
JavaScript
import { resolve } from "node:path";
import { GrepEngineError } from "../engine.js";
export function parseRgEvent(line) {
const event = JSON.parse(line);
if (!event || !["begin", "match", "context", "end", "summary"].includes(event.type) || !event.data)
throw new Error("Invalid ripgrep JSON event");
return event;
}
function decode(value) {
if (typeof value?.text === "string")
return value.text;
if (typeof value?.bytes === "string")
return Buffer.from(value.bytes, "base64").toString("utf8");
throw new GrepEngineError("ENGINE_UNAVAILABLE", "Invalid ripgrep JSON text/bytes field");
}
export function collectSegmentRows(segment, request, oversized) {
const first = segment[0];
const files = new Map();
const byPath = new Map(segment.map((candidate) => [candidate.absolute, candidate]));
const segmentBinary = new Set();
const onEvent = (event) => {
if (event.type === "summary")
return;
const candidate = oversized ? first : byPath.get(resolve(first.root.cwd, decode(event.data.path)));
if (!candidate)
throw new Error("ripgrep searched a file outside the ordered segment");
if (event.type === "begin") {
files.set(candidate.display, { candidate, rows: new Map() });
return;
}
if (event.type === "end") {
if (event.data.binary_offset != null) {
files.delete(candidate.display);
segmentBinary.add(candidate.display);
}
return;
}
const file = files.get(candidate.display);
if (!file || typeof event.data.line_number !== "number")
throw new Error("ripgrep match/context without a begin or line number");
const text = decode(event.data.lines).replace(/\n$/, "").split("\n");
for (const [index, physical] of text.entries()) {
const line = event.data.line_number + index;
if (line < (request.lineStart ?? 1) || line > (request.lineEnd ?? Infinity))
continue;
const isContext = event.type === "context";
const existing = file.rows.get(line);
if (existing && (!existing.isContext || isContext))
continue;
const column = !isContext && index === 0 && event.data.submatches?.[0] ? event.data.submatches[0].start + 1 : undefined;
let content = physical.replace(/\r$/, "");
let truncated = false;
if (request.maxColumns !== undefined) {
const scalars = Array.from(content);
if (scalars.length > request.maxColumns) {
content = `${scalars.slice(0, request.maxColumns).join("")}...`;
truncated = true;
}
}
file.rows.set(line, { path: candidate.display, line, column, text: content, isContext, truncated });
}
};
return { files, segmentBinary, onEvent };
}
//# sourceMappingURL=json-rows.js.map