feat: add standalone document pseudonymization to Raycast
This commit is contained in:
@@ -11,6 +11,9 @@ import {
|
||||
} from "@raycast/api";
|
||||
import { getSelectedTextSafely } from "./selection";
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { dirname } from "node:path";
|
||||
import { DocumentResult } from "./document-result";
|
||||
import { saveDocumentCopy } from "./documents";
|
||||
import {
|
||||
createSession,
|
||||
getActiveSessionId,
|
||||
@@ -19,23 +22,36 @@ import {
|
||||
setActiveSession,
|
||||
updateSessionMapping,
|
||||
} from "./sessions";
|
||||
import type { EntityType, PseudonymizeResponse, VelumSession } from "./types";
|
||||
import type {
|
||||
EntityType,
|
||||
ImagePolicy,
|
||||
PseudonymizeResponse,
|
||||
VelumSession,
|
||||
} from "./types";
|
||||
import {
|
||||
markdownCodeBlock,
|
||||
mappingSummary,
|
||||
NEW_SESSION_ID,
|
||||
sessionSubtitle,
|
||||
} from "./ui";
|
||||
import { getEntityTypes, pseudonymize } from "./velum";
|
||||
import { getEntityTypes, pseudonymize, pseudonymizeDocument } from "./velum";
|
||||
|
||||
type FormValues = {
|
||||
text: string;
|
||||
text?: string;
|
||||
inputMode: "text" | "document";
|
||||
files?: string[];
|
||||
outputDirectory?: string[];
|
||||
imagePolicy?: ImagePolicy;
|
||||
sessionId: string;
|
||||
entityTypes: string[];
|
||||
};
|
||||
|
||||
export default function Command() {
|
||||
const [text, setText] = useState("");
|
||||
const [inputMode, setInputMode] = useState("text");
|
||||
const [imagePolicy, setImagePolicy] = useState<ImagePolicy>("placeholder");
|
||||
const [files, setFiles] = useState<string[]>([]);
|
||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||
const [sessions, setSessions] = useState<VelumSession[]>([]);
|
||||
const [activeSessionId, setActiveSessionId] = useState<string>();
|
||||
const [entityTypes, setEntityTypes] = useState<EntityType[]>([]);
|
||||
@@ -79,6 +95,7 @@ export default function Command() {
|
||||
});
|
||||
return;
|
||||
}
|
||||
setInputMode("text");
|
||||
setText(selected);
|
||||
}
|
||||
|
||||
@@ -91,19 +108,26 @@ export default function Command() {
|
||||
});
|
||||
return;
|
||||
}
|
||||
setInputMode("text");
|
||||
setText(clipboardText);
|
||||
}
|
||||
|
||||
async function handleSubmit(values: FormValues) {
|
||||
const input = values.text.trim();
|
||||
if (!input) {
|
||||
if (isSubmitting || isLoading) return;
|
||||
const isDocument = values.inputMode === "document";
|
||||
const input = (values.text ?? "").trim();
|
||||
const filePath = values.files?.[0];
|
||||
if (isDocument ? !filePath : !input) {
|
||||
await showToast({
|
||||
style: Toast.Style.Failure,
|
||||
title: "Text zum Pseudonymisieren eingeben",
|
||||
title: isDocument
|
||||
? "Dokument zum Pseudonymisieren auswählen"
|
||||
: "Text zum Pseudonymisieren eingeben",
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
setIsSubmitting(true);
|
||||
const toast = await showToast({
|
||||
style: Toast.Style.Animated,
|
||||
title: "Pseudonymisiere…",
|
||||
@@ -115,6 +139,36 @@ export default function Command() {
|
||||
? await createSession()
|
||||
: ((await getSession(values.sessionId)) ?? (await createSession()));
|
||||
await setActiveSession(session.id);
|
||||
if (isDocument && filePath) {
|
||||
const result = await pseudonymizeDocument(
|
||||
filePath,
|
||||
session.mapping,
|
||||
values.entityTypes,
|
||||
values.imagePolicy ?? "placeholder",
|
||||
);
|
||||
const updatedSession = await updateSessionMapping(
|
||||
session.id,
|
||||
result.mapping,
|
||||
values.entityTypes,
|
||||
);
|
||||
const path = await saveDocumentCopy(
|
||||
result,
|
||||
values.outputDirectory?.[0] ?? dirname(filePath),
|
||||
);
|
||||
toast.style = Toast.Style.Success;
|
||||
toast.title = result.warnings.length
|
||||
? "Dokument mit Warnhinweis bereit"
|
||||
: "Dokument pseudonymisiert";
|
||||
toast.message = `${result.images_replaced} Bilder ersetzt · ${result.images_removed} entfernt`;
|
||||
push(
|
||||
<DocumentResult
|
||||
result={result}
|
||||
session={updatedSession}
|
||||
path={path}
|
||||
/>,
|
||||
);
|
||||
return;
|
||||
}
|
||||
const result = await pseudonymize(
|
||||
input,
|
||||
session.mapping,
|
||||
@@ -135,12 +189,14 @@ export default function Command() {
|
||||
toast.style = Toast.Style.Failure;
|
||||
toast.title = "Pseudonymisierung fehlgeschlagen";
|
||||
toast.message = error instanceof Error ? error.message : String(error);
|
||||
} finally {
|
||||
setIsSubmitting(false);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Form
|
||||
isLoading={isLoading}
|
||||
isLoading={isLoading || isSubmitting}
|
||||
actions={
|
||||
<ActionPanel>
|
||||
<Action.SubmitForm
|
||||
@@ -161,13 +217,76 @@ export default function Command() {
|
||||
</ActionPanel>
|
||||
}
|
||||
>
|
||||
<Form.TextArea
|
||||
id="text"
|
||||
title="Text"
|
||||
value={text}
|
||||
onChange={setText}
|
||||
placeholder="Zu pseudonymisierender Text"
|
||||
/>
|
||||
<Form.Dropdown
|
||||
id="inputMode"
|
||||
title="Eingabe"
|
||||
value={inputMode}
|
||||
onChange={setInputMode}
|
||||
>
|
||||
<Form.Dropdown.Item value="text" title="Text" icon={Icon.Text} />
|
||||
<Form.Dropdown.Item
|
||||
value="document"
|
||||
title="Office-Dokument oder PDF"
|
||||
icon={Icon.Document}
|
||||
/>
|
||||
</Form.Dropdown>
|
||||
{inputMode === "text" ? (
|
||||
<Form.TextArea
|
||||
id="text"
|
||||
title="Text"
|
||||
value={text}
|
||||
onChange={setText}
|
||||
placeholder="Zu pseudonymisierender Text"
|
||||
/>
|
||||
) : (
|
||||
<>
|
||||
<Form.FilePicker
|
||||
id="files"
|
||||
title="Dokument"
|
||||
allowMultipleSelection={false}
|
||||
canChooseDirectories={false}
|
||||
value={files}
|
||||
onChange={setFiles}
|
||||
/>
|
||||
<Form.Description text="DOCX, XLSX, PPTX oder PDF · maximal 20 MiB. PDF wird als Word-Dokument ausgegeben." />
|
||||
<Form.Dropdown
|
||||
id="imagePolicy"
|
||||
title="Bilder im Dokument"
|
||||
value={imagePolicy}
|
||||
onChange={(value) => setImagePolicy(value as ImagePolicy)}
|
||||
>
|
||||
<Form.Dropdown.Item
|
||||
value="placeholder"
|
||||
title="Datenschutz-Platzhalter (Standard)"
|
||||
/>
|
||||
<Form.Dropdown.Item
|
||||
value="remove"
|
||||
title="Bilder vollständig entfernen"
|
||||
/>
|
||||
<Form.Dropdown.Item
|
||||
value="keep"
|
||||
title="Bilder behalten — Datenschutzrisiko"
|
||||
icon={Icon.Warning}
|
||||
/>
|
||||
</Form.Dropdown>
|
||||
<Form.Description
|
||||
title={imagePolicy === "keep" ? "Achtung" : "Bildbehandlung"}
|
||||
text={
|
||||
imagePolicy === "keep"
|
||||
? "Bilder können sensible Informationen enthalten. Sie werden unverändert übernommen und nicht auf personenbezogene Daten geprüft."
|
||||
: "Bildinhalte werden nicht analysiert. Platzhalter behalten Position und Größe soweit möglich bei. Nicht sicher bereinigbare Inhalte werden abgewiesen."
|
||||
}
|
||||
/>
|
||||
<Form.FilePicker
|
||||
id="outputDirectory"
|
||||
title="Ausgabeordner"
|
||||
canChooseFiles={false}
|
||||
canChooseDirectories
|
||||
allowMultipleSelection={false}
|
||||
/>
|
||||
<Form.Description text="Ohne Ordnerauswahl wird die Kopie neben dem Original gespeichert. Bestehende Dateien werden nicht überschrieben." />
|
||||
</>
|
||||
)}
|
||||
<Form.Dropdown
|
||||
id="sessionId"
|
||||
title="Sitzung"
|
||||
|
||||
Reference in New Issue
Block a user