feat: choose project or session for text commands

This commit is contained in:
muena
2026-10-19 06:34:07 +02:00
parent 65134735f1
commit ebf78128c4
28 changed files with 386 additions and 641 deletions

View File

@@ -39,4 +39,11 @@ describe("Zuordnungen gruppieren und wiederherstellen", () => {
PERSON_02: { type: "PERSON", original: "B", merged_into: "PERSON_01" },
})).toThrow("Zyklische Zuordnung");
});
it("löst ältere Sitzungsverweise mit geklammerten Schlüsseln auf", () => {
expect(mappingForRestoration({
"<PERSON_01>": { type: "PERSON", original: "Franz Fuchs" },
"<PERSON_02>": { type: "PERSON", original: "Hr. Fuchs", merged_into: "PERSON_01" },
})["<PERSON_02>"].original).toBe("Franz Fuchs");
});
});

View File

@@ -5,7 +5,9 @@ import { join } from "node:path";
import { createHash } from "node:crypto";
import { migrate } from "../tools/migrate-ki-datenschutz.mjs";
mock.module("@raycast/api", () => ({ environment: { supportPath: tmpdir() } }));
mock.module("@raycast/api", () => ({ environment: { supportPath: tmpdir() },
Form: {}, Icon: {}, Toast: { Style: {} }, showToast: async () => undefined,
LocalStorage: {}, getPreferenceValues: () => ({}) }));
const { restorePath, restoreText } = await import("../src/lib/velum-project");
const roots: string[] = [];
afterEach(async () => { await Promise.all(roots.splice(0).map((root) => fs.rm(root, { recursive: true, force: true }))); });

View File

@@ -3,7 +3,9 @@ import { promises as fs } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
mock.module("@raycast/api", () => ({ environment: { supportPath: tmpdir() } }));
mock.module("@raycast/api", () => ({ environment: { supportPath: tmpdir() },
Form: {}, Icon: {}, Toast: { Style: {} }, showToast: async () => undefined,
LocalStorage: {}, getPreferenceValues: () => ({}) }));
const project = await import("../src/lib/velum-project");
const revision = await import("../src/lib/project-revision");
const roots: string[] = [];

View File

@@ -0,0 +1,94 @@
import { afterEach, expect, it, mock } from "bun:test";
import { promises as fs } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import type { PlaceholderMapping, PseudonymizeResponse } from "../src/types";
mock.module("@raycast/api", () => ({
environment: { supportPath: tmpdir() },
Form: {}, Icon: {}, Toast: { Style: {} }, showToast: async () => undefined,
LocalStorage: {}, getPreferenceValues: () => ({}),
}));
const { applyProjectWhitelist, pseudonymizeInContext } = await import("../src/text-context");
const project = await import("../src/lib/velum-project");
const { normalizePseudonymizeResponse } = await import("../src/velum");
const roots: string[] = [];
afterEach(async () => { await Promise.all(roots.splice(0).map((root) => fs.rm(root, { recursive: true, force: true }))); });
it("behält Verweise beim Umstellen auf KI-Platzhalter konsistent", () => {
const result = normalizePseudonymizeResponse({
pseudonymized_text: "PERSON_02", selected_entity_types: ["PERSON"], spans: [], entity_count: 1,
mapping: {
PERSON_01: { type: "PERSON", original: "Franz Fuchs" },
PERSON_02: { type: "PERSON", original: "Hr. Fuchs", merged_into: "PERSON_01" },
},
});
expect(result.mapping["<PERSON_02>"].merged_into).toBe("<PERSON_01>");
expect(result.pseudonymized_text).toBe("<PERSON_02>");
});
it("übernimmt eine Projekt-Ausnahme auch bei Textbefehlen", () => {
const result = applyProjectWhitelist("Max und Montag", {
pseudonymized_text: "PERSON_01 und ORT_01", selected_entity_types: ["PERSON", "ORT"],
spans: [
{ start: 0, end: 3, type: "PERSON", score: 1, placeholder: "PERSON_01", original: "Max" },
{ start: 8, end: 14, type: "ORT", score: 1, placeholder: "ORT_01", original: "Montag" },
], entity_count: 2,
mapping: {
PERSON_01: { type: "PERSON", original: "Max" },
ORT_01: { type: "ORT", original: "Montag" },
},
}, {}, [{ type: "ORT", original: "Montag" }]);
expect(result.pseudonymized_text).toBe("PERSON_01 und Montag");
expect(result.mapping.ORT_01).toBeUndefined();
expect(result.entity_count).toBe(1);
});
it("verlangt eine ausdrückliche Kontextwahl und legt ein Textprojekt nur einmal an", async () => {
const root = await fs.mkdtemp(join(tmpdir(), "velum-text-context-"));
roots.push(root);
const folder = join(root, "Projekt");
await fs.mkdir(folder);
const request = async (_text: string, mapping: PlaceholderMapping): Promise<PseudonymizeResponse> => ({
pseudonymized_text: "PERSON_01", mapping: {
...mapping, PERSON_01: { type: "PERSON", original: "Alex Beispiel" },
}, selected_entity_types: ["PERSON"], spans: [], entity_count: 1,
});
await expect(pseudonymizeInContext({ sessionId: "__choose_context__" }, "Alex Beispiel", ["PERSON"], false, request))
.rejects.toThrow("Bitte ein Projekt");
const created = await pseudonymizeInContext({ sessionId: "__new_project__", newProjectPath: [folder] },
"Alex Beispiel", ["PERSON"], false, request);
expect(created.session.name).toBe("Projekt Projekt");
expect((await project.listRuns(folder)).length).toBe(1);
await expect(pseudonymizeInContext({ sessionId: "__new_project__", newProjectPath: [folder] },
"Alex Beispiel", ["PERSON"], false, request)).rejects.toThrow("bereits ein Projekt");
});
it("verwendet die bestehende Projekttabelle und bewahrt ihre Verweise für spätere Läufe", async () => {
const root = await fs.mkdtemp(join(tmpdir(), "velum-text-reuse-"));
roots.push(root);
const folder = join(root, "Quelle");
await fs.mkdir(folder);
const saved = await project.createRun(folder);
saved.run.mapping = {
PERSON_01: { type: "PERSON", original: "Franz Fuchs" },
PERSON_02: { type: "PERSON", original: "Hr. Fuchs", merged_into: "PERSON_01" },
};
await project.saveRun(saved.directory, saved.run);
const request = async (_text: string, mapping: PlaceholderMapping): Promise<PseudonymizeResponse> => {
expect(mapping.PERSON_02.merged_into).toBe("PERSON_01");
return { pseudonymized_text: "PERSON_02 und PERSON_03", mapping: {
...mapping, PERSON_03: { type: "PERSON", original: "Mara Muster" },
}, selected_entity_types: ["PERSON"], spans: [], entity_count: 2 };
};
const { result, session } = await pseudonymizeInContext({ sessionId: `project:${folder}` },
"Hr. Fuchs und Mara Muster", ["PERSON"], true, request);
expect(result.pseudonymized_text).toBe("<PERSON_02> und <PERSON_03>");
expect(session.mapping["<PERSON_02>"].original).toBe("Franz Fuchs");
const runs = await project.listRuns(folder);
expect(runs).toHaveLength(2);
expect(runs[0].run.mapping.PERSON_02.original).toBe("Hr. Fuchs");
expect(runs[0].run.mapping.PERSON_02.merged_into).toBe("PERSON_01");
expect(runs[0].run.mapping.PERSON_03.original).toBe("Mara Muster");
expect(runs[1].run.mapping.PERSON_03).toBeUndefined();
});

View File

@@ -4,7 +4,9 @@ import { tmpdir } from "node:os";
import { join } from "node:path";
import type { RunFile } from "../src/lib/velum-project";
mock.module("@raycast/api", () => ({ environment: { supportPath: tmpdir() } }));
mock.module("@raycast/api", () => ({ environment: { supportPath: tmpdir() },
Form: {}, Icon: {}, Toast: { Style: {} }, showToast: async () => undefined,
LocalStorage: {}, getPreferenceValues: () => ({}) }));
const project = await import("../src/lib/velum-project");
const { mappingForRestoration } = await import("../src/lib/mapping-groups");
const { mergeMappingGroups } = await import("../src/lib/mapping-groups");