feat: improve Velum hit review and session creation

This commit is contained in:
muena
2026-12-02 08:53:37 +01:00
parent 96d5f3f57d
commit 019f6b6d7c
10 changed files with 467 additions and 95 deletions

View File

@@ -121,6 +121,37 @@ export function groupReviewHits<T extends GroupableHit>(hits: T[]): ReviewGroup<
return [...groups.values()].sort((a, b) => a.type.localeCompare(b.type) || a.original.localeCompare(b.original));
}
/** Render source text literally, even if it contains Markdown or HTML. */
export function literalReviewContext(text: string): string {
let longestRun = 2;
for (const [run] of text.matchAll(/`+/g)) longestRun = Math.max(longestRun, run.length);
const fence = "`".repeat(longestRun + 1);
return `${fence}\n${text}\n${fence}`;
}
/** Text API positions count Unicode code points. Mark the exact occurrence in each snippet. */
export function textHitContexts(text: string, spans: Array<{ start: number; end: number }>): string[] {
const characters = Array.from(text);
const contexts: string[] = [];
let cursor = 0;
let line = 1;
for (const { span, index } of spans.map((span, index) => ({ span, index })).sort((a, b) => a.span.start - b.span.start)) {
const { start, end } = span;
if (!Number.isInteger(start) || !Number.isInteger(end) || start < 0 || start >= end || end > characters.length) {
contexts[index] = "Ungültige Trefferposition der Velum-API.";
continue;
}
for (; cursor < start; cursor++) {
if (characters[cursor] === "\n" || (characters[cursor] === "\r" && characters[cursor + 1] !== "\n")) line++;
}
const before = characters.slice(Math.max(0, start - 90), start).join("");
const match = characters.slice(start, end).join("");
const after = characters.slice(end, end + 90).join("");
contexts[index] = `**Zeile ${line}**\n\n${literalReviewContext(`${before}⟦${match}⟧${after}`)}`;
}
return contexts;
}
export function withoutWhitelistedHits<T extends GroupableHit>(hits: T[], whitelist: ProjectWhitelistEntry[]): T[] {
const excluded = new Set(whitelist.map(whitelistKey));
return hits.filter((hit) => !excluded.has(whitelistKey(hit)));