import { Action, ActionPanel, Clipboard, Form, Icon, showToast, Toast, useNavigation, } from "@raycast/api"; import { useEffect, useState } from "react"; import { type Creativity, CREATIVITY_OPTIONS, DEFAULT_MODEL, getLastUsedModel, MODEL_OPTIONS, setLastUsedModel, STRICT_PLACEHOLDER_RULE, } from "./ai"; import { PseudonymizationConfirm } from "./ai-views"; import { getSelectedTextSafely } from "./selection"; import { ContextPicker, type ContextFormValues } from "./context-picker"; import { prepareTextInContext } from "./text-context"; import { TextHitReview } from "./text-hit-review"; import { getActiveSessionId, listSessions, } from "./sessions"; import type { EntityType, VelumSession } from "./types"; import { getEntityTypes, } from "./velum"; type OutputFormat = "json" | "markdown-table"; const FORMAT_OPTIONS: Array<{ value: OutputFormat; title: string }> = [ { value: "json", title: "JSON" }, { value: "markdown-table", title: "Markdown-Tabelle" }, ]; function buildStructuredDataPrompt(args: { pseudonymizedText: string; instructions: string; schema: string; format: OutputFormat; }): string { const extra = args.instructions.trim() ? `\n\nZusätzliche Anweisungen des Nutzers:\n${args.instructions.trim()}` : ""; const formatInstructions = args.format === "json" ? [ "Format: gib genau einen Code-Block zurück, der valides JSON enthält.", "Verwende für nicht gefundene Felder den Wert null. Bei Listen: leeres Array `[]`.", "Antworte mit dem Code-Block und nichts anderem.", ].join("\n") : [ "Format: gib genau eine Markdown-Tabelle zurück. Eine Zeile pro Datensatz.", 'Verwende für nicht gefundene Felder „—".', "Antworte mit der Tabelle und nichts anderem.", ].join("\n"); return [ "Du erhältst pseudonymisierten Text und ein Schema. Personenbezogene Daten wurden durch Platzhalter wie , , ersetzt.", "", STRICT_PLACEHOLDER_RULE, "", "Aufgabe: Extrahiere die im Schema beschriebenen Daten aus dem Text.", "", "Regeln:", "- Verwende ausschließlich Informationen, die im Text vorkommen. Erfinde nichts.", "- Personen, Organisationen etc. erscheinen als Platzhalter (z. B. ) — übernimm sie zeichengetreu.", "- Keine einleitenden Floskeln, keine Erklärungen — nur das angeforderte Ausgabeformat.", "", formatInstructions, extra, "", "--- Schema ---", args.schema.trim(), "--- Ende Schema ---", "", "--- Text (pseudonymisiert) ---", args.pseudonymizedText, "--- Ende Text ---", ].join("\n"); } type FormValues = ContextFormValues & { text: string; schema: string; format: OutputFormat; entityTypes: string[]; model: string; creativity: Creativity; instructions: string; }; export default function Command() { const [text, setText] = useState(""); const [sessions, setSessions] = useState([]); const [activeSessionId, setActiveSessionId] = useState(); const [entityTypes, setEntityTypes] = useState([]); const [defaultEntityTypes, setDefaultEntityTypes] = useState([]); const [selectedEntityTypes, setSelectedEntityTypes] = useState([]); const [model, setModel] = useState(DEFAULT_MODEL); const [isLoading, setIsLoading] = useState(true); const { push } = useNavigation(); useEffect(() => { async function load() { const [loadedSessions, activeId, lastModel] = await Promise.all([ listSessions(), getActiveSessionId(), getLastUsedModel(), ]); setSessions(loadedSessions); setActiveSessionId(activeId); setModel(lastModel); const selection = await getSelectedTextSafely(); if (selection && selection.trim()) { setText(selection); } try { const loadedEntityTypes = await getEntityTypes(); setEntityTypes(loadedEntityTypes.all); setDefaultEntityTypes(loadedEntityTypes.defaults); setSelectedEntityTypes(loadedEntityTypes.defaults); } catch { setEntityTypes([]); } finally { setIsLoading(false); } } load(); }, []); 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 Extrahieren eingeben", }); return; } if (!values.schema.trim()) { await showToast({ style: Toast.Style.Failure, title: "Schema beschreiben", }); return; } void setLastUsedModel(values.model); const toast = await showToast({ style: Toast.Style.Animated, title: "Pseudonymisiere…", }); try { const prepared = await prepareTextInContext(values, input, values.entityTypes); toast.style = Toast.Style.Success; toast.title = "Treffer zur Prüfung bereit"; toast.message = `${prepared.raw.spans.length} Treffer prüfen`; push( { const schema = values.schema; const format = values.format; const buildPrompt = (pseudonymizedText: string, instructions: string) => buildStructuredDataPrompt({ pseudonymizedText, instructions, schema, format, }); push( , ); }} />); } catch (error) { toast.style = Toast.Style.Failure; toast.title = "Pseudonymisierung fehlgeschlagen"; toast.message = error instanceof Error ? error.message : String(error); } } return (
} > {FORMAT_OPTIONS.map((option) => ( ))} {entityTypes.length > 0 ? ( {entityTypes.map((entityType) => ( ))} ) : null} {MODEL_OPTIONS.map((option) => ( ))} {CREATIVITY_OPTIONS.map((value) => ( ))} ); }