forked from r.muellner/velum-raycast
Forms and the project-file command now preselect the backend's default_entity_types from /api/entity-types instead of every type. Older backends without that field fall back to all types except IP, DATUM and REFERENZ. Saved project selections are still loaded unchanged; the project-file command keeps excluding ORT by default as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
328 lines
9.7 KiB
TypeScript
328 lines
9.7 KiB
TypeScript
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 <PERSON_1>, <ORG_2>, <EMAIL_3> 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. <PERSON_1>) — ü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<VelumSession[]>([]);
|
|
const [activeSessionId, setActiveSessionId] = useState<string>();
|
|
const [entityTypes, setEntityTypes] = useState<EntityType[]>([]);
|
|
const [defaultEntityTypes, setDefaultEntityTypes] = useState<EntityType[]>([]);
|
|
const [selectedEntityTypes, setSelectedEntityTypes] = useState<string[]>([]);
|
|
const [model, setModel] = useState<string>(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(<TextHitReview prepared={prepared} forAi onConfirmed={async (pseudoResult, updatedSession) => {
|
|
const schema = values.schema;
|
|
const format = values.format;
|
|
const buildPrompt = (pseudonymizedText: string, instructions: string) =>
|
|
buildStructuredDataPrompt({
|
|
pseudonymizedText,
|
|
instructions,
|
|
schema,
|
|
format,
|
|
});
|
|
|
|
push(
|
|
<PseudonymizationConfirm
|
|
session={updatedSession}
|
|
pseudonymizedText={pseudoResult.pseudonymized_text}
|
|
model={values.model}
|
|
creativity={values.creativity}
|
|
instructions={values.instructions}
|
|
buildPrompt={buildPrompt}
|
|
labels={{
|
|
pageHeading:
|
|
format === "json" ? "Strukturierte Daten (JSON)" : "Tabelle",
|
|
runningLabel:
|
|
format === "json"
|
|
? "KI extrahiert Daten (JSON) …"
|
|
: "KI extrahiert Tabelle …",
|
|
failureTitle: "Extraktion fehlgeschlagen",
|
|
copyTitle:
|
|
format === "json" ? "JSON kopieren" : "Markdown-Tabelle kopieren",
|
|
pasteTitle:
|
|
format === "json" ? "JSON einfügen" : "Tabelle einfügen",
|
|
}}
|
|
/>,
|
|
);
|
|
}} />);
|
|
} 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="Daten extrahieren"
|
|
icon={Icon.AppWindowGrid3x3}
|
|
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="Freitext mit den zu extrahierenden Informationen"
|
|
/>
|
|
<Form.TextArea
|
|
id="schema"
|
|
title="Schema"
|
|
placeholder={
|
|
'z. B. „Liste aller Teilnehmer mit Rolle und E-Mail" oder „Felder: name, rolle, email, telefon"'
|
|
}
|
|
/>
|
|
<Form.Dropdown id="format" title="Ausgabeformat" defaultValue="json">
|
|
{FORMAT_OPTIONS.map((option) => (
|
|
<Form.Dropdown.Item
|
|
key={option.value}
|
|
value={option.value}
|
|
title={option.title}
|
|
/>
|
|
))}
|
|
</Form.Dropdown>
|
|
<ContextPicker sessions={sessions} activeSessionId={activeSessionId}
|
|
availableEntityTypes={entityTypes} defaultEntityTypes={defaultEntityTypes}
|
|
onEntityTypesChange={setSelectedEntityTypes} />
|
|
{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.Dropdown
|
|
id="model"
|
|
title="KI-Modell"
|
|
value={model}
|
|
onChange={setModel}
|
|
>
|
|
{MODEL_OPTIONS.map((option) => (
|
|
<Form.Dropdown.Item
|
|
key={option.value}
|
|
value={option.value}
|
|
title={option.title}
|
|
/>
|
|
))}
|
|
</Form.Dropdown>
|
|
<Form.Dropdown id="creativity" title="Kreativität" defaultValue="none">
|
|
{CREATIVITY_OPTIONS.map((value) => (
|
|
<Form.Dropdown.Item key={value} value={value} title={value} />
|
|
))}
|
|
</Form.Dropdown>
|
|
<Form.TextArea
|
|
id="instructions"
|
|
title="Zusätzliche Anweisungen"
|
|
placeholder="Optional: Sortierung, Filterung, Sonderfälle, …"
|
|
/>
|
|
</Form>
|
|
);
|
|
}
|