diff --git a/frontend/src/lib/api.ts b/frontend/src/lib/api.ts index 81d35d7..1a52027 100644 --- a/frontend/src/lib/api.ts +++ b/frontend/src/lib/api.ts @@ -1433,3 +1433,31 @@ export async function setMixerRole( role, }); } + +// ========================================================================= +// AI-assistert script-generering (oppgave 24.7) +// ========================================================================= + +export interface AiSuggestScriptRequest { + description: string; + trigger_event?: string; + trigger_conditions?: Record; + eventually?: boolean; + collection_id?: string; +} + +export interface AiSuggestScriptResponse { + status: string; + script?: string; + compile_result?: CompileScriptResponse; + work_item_id?: string; + message?: string; +} + +/** AI-assistert generering av orkestreringsscript fra fritekst-beskrivelse. */ +export function aiSuggestScript( + accessToken: string, + req: AiSuggestScriptRequest +): Promise { + return post(accessToken, '/intentions/ai_suggest_script', req); +} diff --git a/frontend/src/lib/components/traits/OrchestrationTrait.svelte b/frontend/src/lib/components/traits/OrchestrationTrait.svelte index ae8f5f6..fab115e 100644 --- a/frontend/src/lib/components/traits/OrchestrationTrait.svelte +++ b/frontend/src/lib/components/traits/OrchestrationTrait.svelte @@ -6,6 +6,7 @@ compileScript, testOrchestration, fetchOrchestrationLog, + aiSuggestScript, type CompileScriptResponse, type OrchestrationLogEntry } from '$lib/api'; @@ -77,6 +78,12 @@ let triggerConditions = $state(''); let executor = $state('script'); + // AI-assist + let showAiAssist = $state(false); + let aiDescription = $state(''); + let aiGenerating = $state(false); + let aiError: string | null = $state(null); + // History let logEntries: OrchestrationLogEntry[] = $state([]); let showHistory = $state(false); @@ -214,6 +221,42 @@ if (showHistory) loadHistory(); } + async function generateWithAi() { + if (!accessToken || !aiDescription.trim()) return; + aiGenerating = true; + aiError = null; + try { + const result = await aiSuggestScript(accessToken, { + description: aiDescription, + trigger_event: triggerEvent !== 'manual' ? triggerEvent : undefined, + collection_id: collection?.id, + }); + + if (result.script) { + scriptContent = result.script; + aiDescription = ''; + showAiAssist = false; + } + + if (result.status === 'generated_with_errors' && result.compile_result) { + const errors = result.compile_result.diagnostics + ?.filter((d) => d.severity === 'Error') + .map((d) => d.message) + .join('; '); + if (errors) { + aiError = `Script generert med feil: ${errors}`; + } + } else if (result.status === 'deferred') { + aiError = null; + showAiAssist = false; + } + } catch (err) { + aiError = err instanceof Error ? err.message : 'Ukjent feil'; + } finally { + aiGenerating = false; + } + } + // ========================================================================= // Trigger event options // ========================================================================= @@ -383,6 +426,35 @@ {/if} + + {#if showAiAssist} +
+
+ AI-assistent + +
+ + {#if aiError} +
{aiError}
+ {/if} +
+ +
+
+ {/if} +
+