forked from r.muellner/velum-raycast
336 lines
10 KiB
TypeScript
336 lines
10 KiB
TypeScript
import {
|
||
Action,
|
||
ActionPanel,
|
||
Clipboard,
|
||
Form,
|
||
Icon,
|
||
showToast,
|
||
Toast,
|
||
useNavigation,
|
||
} from "@raycast/api";
|
||
import { useEffect, useMemo, 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 {
|
||
createSession,
|
||
getActiveSessionId,
|
||
getSession,
|
||
listSessions,
|
||
setActiveSession,
|
||
updateSessionMapping,
|
||
} from "./sessions";
|
||
import type { EntityType, VelumSession } from "./types";
|
||
import { NEW_SESSION_ID } from "./ui";
|
||
import {
|
||
getEntityTypes,
|
||
normalizePseudonymizeResponse,
|
||
pseudonymize,
|
||
} from "./velum";
|
||
|
||
function buildMeetingNotesPrompt(
|
||
pseudonymizedText: string,
|
||
instructions: string,
|
||
): string {
|
||
const extra = instructions.trim()
|
||
? `\n\nZusätzliche Anweisungen des Nutzers:\n${instructions.trim()}`
|
||
: "";
|
||
|
||
return [
|
||
"Du erhältst pseudonymisierte Meeting-Notizen als FLIESSTEXT / PROSA.",
|
||
"Personenbezogene Daten wurden bereits durch Platzhalter wie <PERSON_1>, <ORG_2>, <DATE_3>, <EMAIL_4> ersetzt.",
|
||
"",
|
||
STRICT_PLACEHOLDER_RULE,
|
||
"",
|
||
"Aufgabe: Verwandle den Fließtext in eine SAUBER STRUKTURIERTE, deutschsprachige Meeting-Zusammenfassung in Markdown.",
|
||
"Ziehe Struktur, Verantwortlichkeiten, Entscheidungen und Aufgaben aus der Prosa heraus — verdichte, ohne Inhalte zu erfinden.",
|
||
"",
|
||
"Verwende GENAU diese Abschnitte in dieser Reihenfolge:",
|
||
"",
|
||
"## Kontext",
|
||
"Ein kurzer Absatz (2–4 Sätze): Worum ging es im Meeting, Anlass, Rahmen. Datum/Ort nur, wenn aus dem Text ableitbar (sonst weglassen).",
|
||
"",
|
||
"## Teilnehmer",
|
||
"- Stichpunktliste der beteiligten Platzhalter, jeweils mit Rolle/Funktion (falls aus dem Text ableitbar).",
|
||
"- Format pro Zeile: `- <PERSON_X> — Rolle/Funktion`",
|
||
"",
|
||
"## Themen & Diskussion",
|
||
"Pro behandeltem Thema ein eigener Unter-Absatz:",
|
||
"### <Themenbezeichnung in 3–6 Wörtern>",
|
||
"Ein kurzer Absatz (2–5 Sätze), der Kernaussagen, Positionen und Argumente verdichtet. Wer hat was gesagt — nur wenn relevant. Keine wörtlichen Zitate, außer sie sind im Quelltext markiert.",
|
||
"",
|
||
"## Entscheidungen",
|
||
"- Eine Stichpunktliste mit jeweils einer klar formulierten Entscheidung pro Zeile.",
|
||
"- Nur tatsächlich getroffene Entscheidungen; keine Vorschläge oder Überlegungen.",
|
||
"",
|
||
"## Action Items",
|
||
"Markdown-Tabelle mit GENAU dieser Kopfzeile:",
|
||
"| Aufgabe | Verantwortlich | Deadline | Status |",
|
||
"| --- | --- | --- | --- |",
|
||
"- Status MUSS gesetzt sein, genau einer von: „offen“, „abgeschlossen“. „abgeschlossen“ nur wenn der Text die Erledigung explizit beschreibt.",
|
||
"- Verantwortlich als Platzhalter (z. B. <PERSON_1>); wenn nicht ableitbar: „—“.",
|
||
"- Deadline nur, wenn ein konkretes Datum/Zeitraum genannt ist; sonst leer lassen. Niemals Deadlines erfinden.",
|
||
"- Wenn keine Action Items vorkommen, schreibe stattdessen die einzelne Zeile: „Keine Action Items.“",
|
||
"",
|
||
"## Offene Punkte",
|
||
"- Stichpunktliste der Themen, die NICHT abschließend geklärt wurden bzw. Klärungsbedarf haben.",
|
||
"",
|
||
"## Nächste Schritte",
|
||
"Ein kurzer Absatz (1–3 Sätze): Wie geht es nach diesem Meeting weiter? Nur wenn aus dem Text ableitbar.",
|
||
"",
|
||
"Regeln:",
|
||
'Wenn ein Abschnitt im Text nicht vorkommt, schreibe „—" als einzigen Inhalt darunter.',
|
||
"Nichts erfinden, nichts ausschmücken — strikt am Quelltext bleiben.",
|
||
"Antworte ausschließlich in Markdown, ohne einleitende Floskeln und ohne umschließende Code-Fences (keine ``` Zeilen am Anfang oder Ende).",
|
||
extra,
|
||
"",
|
||
"--- Meeting-Notizen (pseudonymisiert, Prosa) ---",
|
||
pseudonymizedText,
|
||
"--- Ende ---",
|
||
].join("\n");
|
||
}
|
||
|
||
type FormValues = {
|
||
text: string;
|
||
sessionId: string;
|
||
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 [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);
|
||
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: "Meeting-Notizen eingeben",
|
||
});
|
||
return;
|
||
}
|
||
|
||
void setLastUsedModel(values.model);
|
||
|
||
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 rawResult = await pseudonymize(
|
||
input,
|
||
session.mapping,
|
||
values.entityTypes,
|
||
);
|
||
const pseudoResult = normalizePseudonymizeResponse(rawResult);
|
||
const updatedSession = await updateSessionMapping(
|
||
session.id,
|
||
pseudoResult.mapping,
|
||
pseudoResult.selected_entity_types,
|
||
);
|
||
|
||
toast.style = Toast.Style.Success;
|
||
toast.title = "Pseudonymisiert";
|
||
toast.message = `${pseudoResult.entity_count} Zuordnungen`;
|
||
|
||
push(
|
||
<PseudonymizationConfirm
|
||
session={updatedSession}
|
||
pseudonymizedText={pseudoResult.pseudonymized_text}
|
||
model={values.model}
|
||
creativity={values.creativity}
|
||
instructions={values.instructions}
|
||
buildPrompt={buildMeetingNotesPrompt}
|
||
labels={{
|
||
pageHeading: "Meeting-Zusammenfassung",
|
||
runningLabel: "KI strukturiert Meeting-Notizen …",
|
||
failureTitle: "Zusammenfassung fehlgeschlagen",
|
||
copyTitle: "Zusammenfassung kopieren",
|
||
pasteTitle: "Zusammenfassung 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="Meeting-Notizen strukturieren"
|
||
icon={Icon.Document}
|
||
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="Meeting-Notizen (Prosa)"
|
||
value={text}
|
||
onChange={setText}
|
||
placeholder="Meeting-Notizen als Fließtext einfügen — z. B. handgeschriebene Protokollnotizen, ein diktiertes Transkript oder ein zusammenhängender Bericht."
|
||
/>
|
||
<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.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="low">
|
||
{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: Fokus auf bestimmte Themen, Tonalität, Zielpublikum (z. B. „für Lenkungsausschuss verdichten“), …"
|
||
/>
|
||
</Form>
|
||
);
|
||
}
|