325 lines
11 KiB
TypeScript
325 lines
11 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 { getPreferences } from "./preferences";
|
||
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";
|
||
|
||
function getIsoWeek(date: Date): number {
|
||
const d = new Date(
|
||
Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate()),
|
||
);
|
||
const dayNum = d.getUTCDay() || 7;
|
||
d.setUTCDate(d.getUTCDate() + 4 - dayNum);
|
||
const yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
|
||
return Math.ceil(((d.getTime() - yearStart.getTime()) / 86400000 + 1) / 7);
|
||
}
|
||
|
||
function buildProjectStatusPrompt(userFullName: string) {
|
||
const signatureName = userFullName.trim() || "Raphael";
|
||
|
||
return function (pseudonymizedText: string, instructions: string): string {
|
||
const today = new Date();
|
||
const todayIso = today.toISOString().slice(0, 10);
|
||
const week = getIsoWeek(today);
|
||
|
||
const extra = instructions.trim()
|
||
? `\n\nZusätzliche Anweisungen des Nutzers:\n${instructions.trim()}`
|
||
: "";
|
||
|
||
return [
|
||
`Heute ist ${todayIso} (KW ${week}).`,
|
||
"Personenbezogene Daten in den Rohnotizen sind durch Platzhalter wie <PERSON_1>, <ORG_2>, <DATE_3>, <EMAIL_4> ersetzt.",
|
||
"",
|
||
STRICT_PLACEHOLDER_RULE,
|
||
"",
|
||
"# ROLLE",
|
||
"Du bist erfahrener SAP-Projektleiter und unterstützt mich bei der",
|
||
"Vorbereitung von Steering-Committee-Updates und Management-Kommunikation.",
|
||
"",
|
||
"# KONTEXT",
|
||
"Ich liefere dir laufend Rohnotizen aus dem Projekt: Stichpunkte aus",
|
||
"Statusmeetings, Auszüge aus E-Mails, Risiken, Zahlen, Entscheidungen.",
|
||
"Daraus erstelle ich Steering-Updates für den Lenkungsausschuss.",
|
||
"",
|
||
"# AUFGABE",
|
||
"Wenn ich Rohnotizen schicke, erstelle ein Steering-Update in genau",
|
||
"folgender Struktur:",
|
||
"",
|
||
"## Steering-Update · KW [Nummer]",
|
||
"",
|
||
"## Status: [🔴 rot | 🟡 gelb | 🟢 grün] // immer das Ampel Emoji ausgeben!",
|
||
"Begründung in einem Satz.",
|
||
"",
|
||
"## Fortschritt:",
|
||
"2–3 Bullets: Was ist seit dem letzten Update passiert.",
|
||
"",
|
||
"## Top-Risiken:",
|
||
"Maximal 3 Bullets. Jeder Bullet im Format:",
|
||
"- [Risiko] — Auswirkung — empfohlene Maßnahme.",
|
||
"",
|
||
"## Entscheidung:",
|
||
"Welche Entscheidung muss der Lenkungsausschuss diese Woche treffen?",
|
||
"Formuliere sie als klare Frage mit 2 Optionen.",
|
||
"",
|
||
"## Nächster Schritt:",
|
||
"2–3 konkrete nächste Aktionen mit Verantwortlichkeit (sofern erkennbar).",
|
||
"",
|
||
"## GF-Summary:",
|
||
"Eine einzige Aussage in 1–2 Sätzen — was die Geschäftsführung wissen muss,",
|
||
"wenn sie nur diesen Block liest.",
|
||
"",
|
||
"# REGELN",
|
||
"- Antworte ausschließlich in deutscher Geschäftssprache.",
|
||
"- Sachlich, knapp, nicht dramatisierend. Keine Floskeln, keine Emojis (außer dem Ampel-Emoji im Status-Block).",
|
||
"- Verwende Ampellogik konsequent: rot = gefährdet, gelb = unter Beobachtung, grün = im Plan.",
|
||
'- Wenn Informationen fehlen, frage nicht nach — sondern markiere die Stelle mit „[unklar: …]“. Lass keine Stelle leer.',
|
||
'- Erfinde keine Zahlen, Termine oder Risiken. Wenn etwas nicht in den Rohnotizen steht, schreibe „[nicht im Input]“.',
|
||
'- Schreibe keine Einleitung („Gerne — hier ist dein Update …“) und keinen Abschluss. Nur die Struktur oben, nichts darum herum.',
|
||
`- Emails beginnen mit „Liebe“ oder „Lieber“ und enden mit „Alles Liebe,\\n${signatureName}“.`,
|
||
"",
|
||
"# OPTIONAL — auf Anforderung",
|
||
"Wenn der Nutzer in den zusätzlichen Anweisungen eine der folgenden Phrasen schreibt, hänge den entsprechenden Zusatz NACH der oben definierten Struktur an (mit einer Leerzeile getrennt):",
|
||
'- „Mach mir auch eine GF-Mail dazu.“ → liefere zusätzlich eine 4–6-Zeilen-Mail an die Geschäftsführung im selben Ton.',
|
||
'- „Wo sind blinde Flecken?“ → liste 3–5 kritische Rückfragen, die ein skeptischer Geschäftsführer stellen würde.',
|
||
'- „Kürzer.“ → kürze das gesamte Update auf die Hälfte der Länge ohne Substanzverlust (statt anhängen: kürzere Version ersetzt die normale).',
|
||
"",
|
||
"Antworte ausschließlich in Markdown, ohne Code-Fences (keine ``` Zeilen am Anfang oder Ende).",
|
||
extra,
|
||
"",
|
||
"--- Rohnotizen (pseudonymisiert) ---",
|
||
pseudonymizedText,
|
||
"--- Ende ---",
|
||
].join("\n");
|
||
};
|
||
}
|
||
|
||
type FormValues = ContextFormValues & {
|
||
text: string;
|
||
entityTypes: string[];
|
||
model: string;
|
||
creativity: Creativity;
|
||
instructions: string;
|
||
};
|
||
|
||
export default function Command() {
|
||
const preferences = getPreferences();
|
||
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();
|
||
|
||
const buildPrompt = useMemo(
|
||
() => buildProjectStatusPrompt(preferences.userFullName),
|
||
[preferences.userFullName],
|
||
);
|
||
|
||
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();
|
||
}, []);
|
||
|
||
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: "Rohnotizen eingeben",
|
||
});
|
||
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) => {
|
||
push(
|
||
<PseudonymizationConfirm
|
||
session={updatedSession}
|
||
pseudonymizedText={pseudoResult.pseudonymized_text}
|
||
model={values.model}
|
||
creativity={values.creativity}
|
||
instructions={values.instructions}
|
||
buildPrompt={buildPrompt}
|
||
labels={{
|
||
pageHeading: "Steering-Update",
|
||
runningLabel: "KI erstellt Steering-Update …",
|
||
failureTitle: "Steering-Update fehlgeschlagen",
|
||
copyTitle: "Steering-Update kopieren",
|
||
pasteTitle: "Steering-Update 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="Steering-Update erstellen"
|
||
icon={Icon.LineChart}
|
||
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="Rohnotizen"
|
||
value={text}
|
||
onChange={setText}
|
||
placeholder="Statusmeeting-Stichpunkte, Email-Auszüge, Risiken, Zahlen, Entscheidungen …"
|
||
/>
|
||
<ContextPicker sessions={sessions} activeSessionId={activeSessionId}
|
||
availableEntityTypes={entityTypes} 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="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. Triggerphrasen: „Mach mir auch eine GF-Mail dazu.“ · „Wo sind blinde Flecken?“ · „Kürzer.“"
|
||
/>
|
||
</Form>
|
||
);
|
||
}
|