import { Clipboard, showHUD, showToast, Toast } from "@raycast/api"; import { getPreferences } from "./preferences"; import { getActiveSession, resolveDefaultSession, updateSessionMapping, } from "./sessions"; import type { VelumSession } from "./types"; import { localDepseudonymize, pseudonymize } from "./velum"; export async function pseudonymizeQuickText( text: string, source: string, ): Promise { const trimmed = text.trim(); if (!trimmed) { await showToast({ style: Toast.Style.Failure, title: `Kein Text in ${source} gefunden`, }); return; } const toast = await showToast({ style: Toast.Style.Animated, title: "Pseudonymisiere…", }); try { const session = await resolveDefaultSession(); const result = await pseudonymize( trimmed, session.mapping, session.entityTypes, ); const updatedSession = await updateSessionMapping( session.id, result.mapping, result.selected_entity_types, ); await writeQuickOutput( result.pseudonymized_text, updatedSession, "Pseudonymisierten Text", ); toast.style = Toast.Style.Success; toast.title = "Pseudonymisiert"; toast.message = `${result.entity_count} Zuordnungen in ${updatedSession.name}`; } catch (error) { toast.style = Toast.Style.Failure; toast.title = "Pseudonymisierung fehlgeschlagen"; toast.message = error instanceof Error ? error.message : String(error); } } export async function depseudonymizeQuickText( text: string, source: string, ): Promise { const trimmed = text.trim(); if (!trimmed) { await showToast({ style: Toast.Style.Failure, title: `Kein Text in ${source} gefunden`, }); return; } const toast = await showToast({ style: Toast.Style.Animated, title: "Stelle wieder her…", }); try { const session = await getActiveSession(); if (Object.keys(session.mapping).length === 0) { toast.style = Toast.Style.Failure; toast.title = "Aktive Sitzung hat keine Zuordnungen"; toast.message = session.name; return; } const result = localDepseudonymize(trimmed, session.mapping); if (result.replacements_made === 0) { toast.style = Toast.Style.Failure; toast.title = "Keine Platzhalter ersetzt"; toast.message = `Sitzung: ${session.name}`; return; } await writeQuickOutput( result.original_text, session, "Wiederhergestellten Text", ); toast.style = Toast.Style.Success; toast.title = "Wiederhergestellt"; toast.message = `${result.replacements_made} Ersetzungen in ${session.name}`; } catch (error) { toast.style = Toast.Style.Failure; toast.title = "Wiederherstellung fehlgeschlagen"; toast.message = error instanceof Error ? error.message : String(error); } } async function writeQuickOutput( text: string, session: VelumSession, textKind: string, ): Promise { if (getPreferences().quickOutput === "paste") { await Clipboard.paste(text); await showHUD(`${textKind} eingefügt (${session.name})`); return; } await Clipboard.copy(text); await showHUD(`${textKind} kopiert (${session.name})`); }