feat: remember project exclusions and merge selected people

This commit is contained in:
muena
2026-09-25 08:08:20 +02:00
parent 392db8601a
commit e7e21e9281
7 changed files with 366 additions and 38 deletions

View File

@@ -1,3 +1,7 @@
import type { PlaceholderMapping } from "../types";
import { whitelistKey } from "./project-whitelist";
import type { ProjectWhitelistEntry } from "./project-whitelist";
const SEPARATOR = "\n\n\n\n";
export const MAX_BATCH_CODEPOINTS = 24000;
@@ -64,3 +68,57 @@ 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));
}
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)));
}
export function withoutWhitelistedRules<T extends { type: string; value: string }>(
rules: T[], whitelist: ProjectWhitelistEntry[],
): T[] {
const excluded = new Set(whitelist.map(whitelistKey));
return rules.filter((rule) => !excluded.has(whitelistKey({ type: rule.type, original: rule.value })));
}
export function canonicalPerson(placeholder: string, merges: Record<string, string>): string {
const seen = new Set<string>();
let current = placeholder;
while (merges[current] && !seen.has(current)) {
seen.add(current);
current = merges[current];
}
return current;
}
export function mergePeople(merges: Record<string, string>, source: string, target: string): Record<string, string> {
const from = canonicalPerson(source, merges);
const into = canonicalPerson(target, merges);
if (from === into) return merges;
const next = { ...merges };
for (const person of new Set([...Object.keys(next), ...Object.values(next), from])) {
if (canonicalPerson(person, merges) === from && person !== into) next[person] = into;
}
delete next[into];
return next;
}
export function mappingForSelection(
preview: PlaceholderMapping, previous: PlaceholderMapping, accepted: GroupableHit[],
merges: Record<string, string>,
): PlaceholderMapping {
const keys = new Set([...Object.keys(previous), ...accepted.map((hit) => hit.placeholder)]);
const mapping: PlaceholderMapping = {};
for (const key of keys) {
const entry = preview[key] ?? previous[key];
if (entry) mapping[key] = { ...entry };
}
for (const [source, target] of Object.entries(merges)) {
const root = canonicalPerson(target, merges);
if (source === root || mapping[source]?.type !== "PERSON" || mapping[root]?.type !== "PERSON") {
throw new Error("Ungültige Personen-Zusammenführung.");
}
mapping[source] = { ...mapping[source], merged_into: root };
}
return mapping;
}

View File

@@ -0,0 +1,5 @@
export type ProjectWhitelistEntry = { type: string; original: string };
export function whitelistKey(entry: ProjectWhitelistEntry): string {
return JSON.stringify([entry.type.toUpperCase(), entry.original.trim().toLocaleLowerCase("de")]);
}

View File

@@ -3,6 +3,11 @@ import { constants as fsConstants, promises as fs } from "node:fs";
import { basename, dirname, extname, join, relative, resolve, sep } from "node:path";
import { environment } from "@raycast/api";
import type { PlaceholderMapping } from "../types";
import { whitelistKey } from "./project-whitelist";
import type { ProjectWhitelistEntry } from "./project-whitelist";
export { whitelistKey } from "./project-whitelist";
export type { ProjectWhitelistEntry } from "./project-whitelist";
export type ProjectRule = { type: "BEGRIFF" | "PERSON" | "ORG" | "ORT" | "FIRMA" | "KUNDE"; value: string };
export type ProjectFile = { relPath: string; absPath: string; kind: "text" | "document"; size: number };
@@ -19,6 +24,8 @@ export type ProjectRun = {
matchModes?: Record<string, "token" | "anywhere">;
files: RunFile[];
inPlace?: boolean;
/** The project source already contains outputs of an earlier in-place run. */
sourceInPlace?: boolean;
};
const DOCUMENTS = new Set([".docx", ".xlsx", ".pptx", ".pdf"]);
@@ -49,6 +56,38 @@ export function rulesPath(projectPath: string, support = environment.supportPath
return join(projectDataRoot(projectPath, support), "rules.json");
}
export function whitelistPath(projectPath: string, support = environment.supportPath): string {
return join(projectDataRoot(projectPath, support), "whitelist.json");
}
export async function loadProjectWhitelist(projectPath: string, support?: string): Promise<ProjectWhitelistEntry[]> {
try {
const value = JSON.parse(await fs.readFile(whitelistPath(projectPath, support), "utf8")) as unknown;
if (!Array.isArray(value)) throw new Error("Ungültige Projekt-Whitelist.");
return value.filter((entry): entry is ProjectWhitelistEntry => Boolean(entry) && typeof entry === "object"
&& typeof entry.type === "string" && typeof entry.original === "string"
&& entry.type.length > 0 && entry.original.trim().length > 0);
} catch (error) {
if ((error as NodeJS.ErrnoException).code === "ENOENT") return [];
throw error;
}
}
export async function saveProjectWhitelist(projectPath: string, entries: ProjectWhitelistEntry[], support?: string): Promise<void> {
const unique = [...new Map(entries.map((entry) => [whitelistKey(entry),
{ type: entry.type.toUpperCase(), original: entry.original.trim() }])).values()];
const path = whitelistPath(projectPath, support);
await fs.mkdir(dirname(path), { recursive: true, mode: 0o700 });
const temp = `${path}.${randomUUID()}.tmp`;
try {
await fs.writeFile(temp, JSON.stringify(unique), { encoding: "utf8", mode: 0o600 });
await fs.rename(temp, path);
} catch (error) {
await fs.rm(temp, { force: true });
throw error;
}
}
export async function loadProjectRules(projectPath: string, support?: string): Promise<ProjectRule[]> {
try {
const value = JSON.parse(await fs.readFile(rulesPath(projectPath, support), "utf8")) as unknown;
@@ -241,6 +280,17 @@ export async function listRuns(projectPath: string, support = environment.suppor
return runs.sort((a, b) => b.run.createdAt.localeCompare(a.run.createdAt));
}
/** An unchanged output from an in-place run can be carried into the next snapshot. */
export async function matchesPreviousOutput(directory: string, relPath: string, currentHash: string): Promise<boolean> {
try {
const previous = await fs.readFile(resolveWithin(join(directory, "files"), relPath));
return digest(previous) === currentHash;
} catch (error) {
if ((error as NodeJS.ErrnoException).code === "ENOENT") return false;
throw error;
}
}
export async function listAllRuns(support = environment.supportPath): Promise<Array<{
run: ProjectRun; directory: string;
}>> {