forked from r.muellner/velum-raycast
Initial commit: Velum Raycast extension
Pseudonymize-and-AI workflow for handling PII-sensitive text via the Velum API and Raycast AI. Commands cover end-to-end email summary and reply, briefing/action-items/structured-data extraction, manual pseudonymize/depseudonymize on selection or clipboard, and session management. Includes Raycast 2.0 Beta workarounds for selection capture and rich-text clipboard. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
248
src/pseudonymize-text.tsx
Normal file
248
src/pseudonymize-text.tsx
Normal file
@@ -0,0 +1,248 @@
|
||||
import {
|
||||
Action,
|
||||
ActionPanel,
|
||||
Clipboard,
|
||||
Detail,
|
||||
Form,
|
||||
Icon,
|
||||
showToast,
|
||||
Toast,
|
||||
useNavigation,
|
||||
} from "@raycast/api";
|
||||
import { getSelectedTextSafely } from "./selection";
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import {
|
||||
createSession,
|
||||
getActiveSessionId,
|
||||
getSession,
|
||||
listSessions,
|
||||
setActiveSession,
|
||||
updateSessionMapping,
|
||||
} from "./sessions";
|
||||
import type { EntityType, PseudonymizeResponse, VelumSession } from "./types";
|
||||
import {
|
||||
markdownCodeBlock,
|
||||
mappingSummary,
|
||||
NEW_SESSION_ID,
|
||||
sessionSubtitle,
|
||||
} from "./ui";
|
||||
import { getEntityTypes, pseudonymize } from "./velum";
|
||||
|
||||
type FormValues = {
|
||||
text: string;
|
||||
sessionId: string;
|
||||
entityTypes: string[];
|
||||
};
|
||||
|
||||
export default function Command() {
|
||||
const [text, setText] = useState("");
|
||||
const [sessions, setSessions] = useState<VelumSession[]>([]);
|
||||
const [activeSessionId, setActiveSessionId] = useState<string>();
|
||||
const [entityTypes, setEntityTypes] = useState<EntityType[]>([]);
|
||||
const [selectedEntityTypes, setSelectedEntityTypes] = useState<string[]>([]);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const { push } = useNavigation();
|
||||
|
||||
useEffect(() => {
|
||||
async function load() {
|
||||
const [loadedSessions, activeId] = await Promise.all([
|
||||
listSessions(),
|
||||
getActiveSessionId(),
|
||||
]);
|
||||
setSessions(loadedSessions);
|
||||
setActiveSessionId(activeId);
|
||||
|
||||
try {
|
||||
const loadedEntityTypes = await getEntityTypes();
|
||||
setEntityTypes(loadedEntityTypes);
|
||||
setSelectedEntityTypes(loadedEntityTypes);
|
||||
} catch {
|
||||
setEntityTypes([]);
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
}
|
||||
load();
|
||||
}, []);
|
||||
|
||||
const defaultSessionId = useMemo(
|
||||
() => activeSessionId || sessions[0]?.id || NEW_SESSION_ID,
|
||||
[activeSessionId, sessions],
|
||||
);
|
||||
|
||||
async function loadSelectedText() {
|
||||
const selected = await getSelectedTextSafely();
|
||||
if (!selected) {
|
||||
await showToast({
|
||||
style: Toast.Style.Failure,
|
||||
title: "Kein markierter Text gefunden",
|
||||
});
|
||||
return;
|
||||
}
|
||||
setText(selected);
|
||||
}
|
||||
|
||||
async function loadClipboardText() {
|
||||
const clipboardText = await Clipboard.readText();
|
||||
if (!clipboardText) {
|
||||
await showToast({
|
||||
style: Toast.Style.Failure,
|
||||
title: "Zwischenablage enthält keinen Text",
|
||||
});
|
||||
return;
|
||||
}
|
||||
setText(clipboardText);
|
||||
}
|
||||
|
||||
async function handleSubmit(values: FormValues) {
|
||||
const input = values.text.trim();
|
||||
if (!input) {
|
||||
await showToast({
|
||||
style: Toast.Style.Failure,
|
||||
title: "Text zum Pseudonymisieren eingeben",
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const toast = await showToast({
|
||||
style: Toast.Style.Animated,
|
||||
title: "Pseudonymisiere…",
|
||||
});
|
||||
|
||||
try {
|
||||
const session =
|
||||
values.sessionId === NEW_SESSION_ID
|
||||
? await createSession()
|
||||
: ((await getSession(values.sessionId)) ?? (await createSession()));
|
||||
await setActiveSession(session.id);
|
||||
const result = await pseudonymize(
|
||||
input,
|
||||
session.mapping,
|
||||
values.entityTypes,
|
||||
);
|
||||
const updatedSession = await updateSessionMapping(
|
||||
session.id,
|
||||
result.mapping,
|
||||
result.selected_entity_types,
|
||||
);
|
||||
|
||||
toast.style = Toast.Style.Success;
|
||||
toast.title = "Pseudonymisiert";
|
||||
toast.message = `${result.entity_count} Zuordnungen in ${updatedSession.name}`;
|
||||
|
||||
push(<PseudonymizeResult result={result} session={updatedSession} />);
|
||||
} catch (error) {
|
||||
toast.style = Toast.Style.Failure;
|
||||
toast.title = "Pseudonymisierung fehlgeschlagen";
|
||||
toast.message = error instanceof Error ? error.message : String(error);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Form
|
||||
isLoading={isLoading}
|
||||
actions={
|
||||
<ActionPanel>
|
||||
<Action.SubmitForm
|
||||
title="Pseudonymisieren"
|
||||
icon={Icon.EyeDisabled}
|
||||
onSubmit={handleSubmit}
|
||||
/>
|
||||
<Action
|
||||
title="Markierten Text übernehmen"
|
||||
icon={Icon.TextSelection}
|
||||
onAction={loadSelectedText}
|
||||
/>
|
||||
<Action
|
||||
title="Zwischenablage übernehmen"
|
||||
icon={Icon.Clipboard}
|
||||
onAction={loadClipboardText}
|
||||
/>
|
||||
</ActionPanel>
|
||||
}
|
||||
>
|
||||
<Form.TextArea
|
||||
id="text"
|
||||
title="Text"
|
||||
value={text}
|
||||
onChange={setText}
|
||||
placeholder="Zu pseudonymisierender Text"
|
||||
/>
|
||||
<Form.Dropdown
|
||||
id="sessionId"
|
||||
title="Sitzung"
|
||||
defaultValue={defaultSessionId}
|
||||
>
|
||||
<Form.Dropdown.Item
|
||||
value={NEW_SESSION_ID}
|
||||
title="Neue Sitzung"
|
||||
icon={Icon.Plus}
|
||||
/>
|
||||
{sessions.map((session) => (
|
||||
<Form.Dropdown.Item
|
||||
key={session.id}
|
||||
value={session.id}
|
||||
title={session.name}
|
||||
icon={
|
||||
session.id === activeSessionId ? Icon.CheckCircle : Icon.Circle
|
||||
}
|
||||
/>
|
||||
))}
|
||||
</Form.Dropdown>
|
||||
{entityTypes.length > 0 ? (
|
||||
<Form.TagPicker
|
||||
id="entityTypes"
|
||||
title="Entitätstypen"
|
||||
value={selectedEntityTypes}
|
||||
onChange={setSelectedEntityTypes}
|
||||
>
|
||||
{entityTypes.map((entityType) => (
|
||||
<Form.TagPicker.Item
|
||||
key={entityType}
|
||||
value={entityType}
|
||||
title={entityType}
|
||||
/>
|
||||
))}
|
||||
</Form.TagPicker>
|
||||
) : null}
|
||||
</Form>
|
||||
);
|
||||
}
|
||||
|
||||
function PseudonymizeResult({
|
||||
result,
|
||||
session,
|
||||
}: {
|
||||
result: PseudonymizeResponse;
|
||||
session: VelumSession;
|
||||
}) {
|
||||
const markdown = [
|
||||
`# ${session.name}`,
|
||||
sessionSubtitle(session),
|
||||
"",
|
||||
"## Pseudonymisierter Text",
|
||||
markdownCodeBlock(result.pseudonymized_text),
|
||||
"",
|
||||
"## Zuordnung",
|
||||
mappingSummary(result.mapping),
|
||||
].join("\n");
|
||||
|
||||
return (
|
||||
<Detail
|
||||
markdown={markdown}
|
||||
actions={
|
||||
<ActionPanel>
|
||||
<Action.CopyToClipboard
|
||||
title="Pseudonymisierten Text kopieren"
|
||||
content={result.pseudonymized_text}
|
||||
/>
|
||||
<Action
|
||||
title="Pseudonymisierten Text einfügen"
|
||||
icon={Icon.TextCursor}
|
||||
onAction={() => Clipboard.paste(result.pseudonymized_text)}
|
||||
/>
|
||||
</ActionPanel>
|
||||
}
|
||||
/>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user