From 38aaa44b95022f40fb830aeb440eb35028c46752 Mon Sep 17 00:00:00 2001 From: HugoCasa Date: Mon, 2 Jun 2025 12:02:47 +0200 Subject: [PATCH] fix: reactivity --- .../src/lib/components/FlowBuilder.svelte | 3 +- .../lib/components/copilot/chat/AIChat.svelte | 92 +++--- .../copilot/chat/AIChatDisplay.svelte | 11 +- .../components/copilot/chat/ChatMode.svelte | 9 +- .../copilot/chat/ContextManager.svelte | 300 ------------------ .../copilot/chat/ContextManager.svelte.ts | 282 ++++++++++++++++ .../copilot/chat/flow/FlowAIChat.svelte | 28 +- .../flows/content/FlowModuleComponent.svelte | 9 +- frontend/src/lib/stores.ts | 1 + 9 files changed, 364 insertions(+), 371 deletions(-) delete mode 100644 frontend/src/lib/components/copilot/chat/ContextManager.svelte create mode 100644 frontend/src/lib/components/copilot/chat/ContextManager.svelte.ts diff --git a/frontend/src/lib/components/FlowBuilder.svelte b/frontend/src/lib/components/FlowBuilder.svelte index 3fc5b2dcb4..78a24b7aaf 100644 --- a/frontend/src/lib/components/FlowBuilder.svelte +++ b/frontend/src/lib/components/FlowBuilder.svelte @@ -8,8 +8,7 @@ type OpenFlow, type InputTransform, type TriggersCount, - CaptureService, - type HubScriptKind + CaptureService } from '$lib/gen' import { initHistory, redo, undo } from '$lib/history' import { diff --git a/frontend/src/lib/components/copilot/chat/AIChat.svelte b/frontend/src/lib/components/copilot/chat/AIChat.svelte index b2968ba845..c5f315e473 100644 --- a/frontend/src/lib/components/copilot/chat/AIChat.svelte +++ b/frontend/src/lib/components/copilot/chat/AIChat.svelte @@ -18,7 +18,7 @@ import { onDestroy, setContext, untrack, type Snippet } from 'svelte' import { type OpenFlow, type ScriptLang } from '$lib/gen' import { sendUserToast } from '$lib/toast' - import ContextManager from './ContextManager.svelte' + import ContextManager, { type ScriptOptions } from './ContextManager.svelte' import HistoryManager from './HistoryManager.svelte' import { flowTools, @@ -30,17 +30,9 @@ ChatCompletionMessageParam, ChatCompletionSystemMessageParam } from 'openai/resources/index.mjs' + import { chatMode, copilotSessionModel, dbSchemas, workspaceStore } from '$lib/stores' interface Props { - scriptOptions?: { - lang: ScriptLang | 'bunnative' - code: string - error: string | undefined - args: Record - path: string | undefined - lastSavedCode?: string | undefined - lastDeployedCode?: string | undefined - diffMode: boolean - } + scriptOptions?: ScriptOptions flowHelpers?: FlowAIChatHelpers & { getFlow: () => OpenFlow } @@ -60,16 +52,16 @@ script: scriptOptions !== undefined, flow: flowHelpers !== undefined }) - let mode: 'script' | 'flow' = $state(flowHelpers ? 'flow' : 'script') async function updateMode(currentMode: 'script' | 'flow') { if (!allowedModes[currentMode]) { - mode = currentMode === 'script' ? 'flow' : 'script' + chatMode.set(currentMode === 'script' ? 'flow' : 'script') } } $effect(() => { - updateMode(untrack(() => mode)) + updateMode(untrack(() => $chatMode)) }) + let displayMessages: DisplayMessage[] = $state([]) let abortController: AbortController | undefined = undefined let messages: ChatCompletionMessageParam[] = $state([]) @@ -92,7 +84,7 @@ } = {} ) { if (options.mode) { - mode = options.mode + $chatMode = options.mode } if (options.instructions) { instructions = options.instructions @@ -102,7 +94,7 @@ } try { const oldSelectedContext = contextManager?.getSelectedContext() ?? [] - if (mode === 'script') { + if ($chatMode === 'script') { contextManager?.updateContextOnRequest(options) } loading.set(true) @@ -114,20 +106,20 @@ { role: 'user', content: instructions, - contextElements: mode === 'script' ? oldSelectedContext : undefined + contextElements: $chatMode === 'script' ? oldSelectedContext : undefined } ] const oldInstructions = instructions instructions = '' const systemMessage = - mode === 'script' ? prepareScriptSystemMessage() : prepareFlowSystemMessage() + $chatMode === 'script' ? prepareScriptSystemMessage() : prepareFlowSystemMessage() - if (mode === 'flow' && !flowHelpers) { + if ($chatMode === 'flow' && !flowHelpers) { throw new Error('No flow helpers passed') } - if (mode === 'script' && !scriptOptions && !options.lang) { + if ($chatMode === 'script' && !scriptOptions && !options.lang) { throw new Error('No script options passed') } @@ -135,7 +127,7 @@ const isPreprocessor = scriptOptions?.path === 'preprocessor' || options.isPreprocessor const userMessage = - mode === 'flow' + $chatMode === 'flow' ? prepareFlowUserMessage(oldInstructions, flowHelpers!.getFlow()) : await prepareScriptUserMessage(oldInstructions, lang, oldSelectedContext, { isPreprocessor @@ -168,7 +160,7 @@ role: 'assistant', content: $currentReply, contextElements: - mode === 'script' + $chatMode === 'script' ? oldSelectedContext.filter((c) => c.type === 'code') : undefined } @@ -193,7 +185,7 @@ } } - if (mode === 'flow') { + if ($chatMode === 'flow') { if (!flowHelpers) { throw new Error('No flow helpers found') } @@ -227,7 +219,9 @@ role: 'assistant', content: $currentReply, contextElements: - mode === 'script' ? oldSelectedContext.filter((c) => c.type === 'code') : undefined + $chatMode === 'script' + ? oldSelectedContext.filter((c) => c.type === 'code') + : undefined } ] currentReply.set('') @@ -272,7 +266,7 @@ throw new Error('No script options passed') } instructions = prompt - contextManager?.setAskAiContext(options) + contextManager.setAskAiContext(options) sendRequest({ removeDiff: options.withDiff, addBackCode: options.withCode === false @@ -295,42 +289,48 @@ }) let aiChatDisplay: AIChatDisplay | undefined = $state(undefined) - let contextManager: ContextManager | undefined = $state(undefined) - + // let contextManager: ContextManager | undefined = $state(undefined) -{#if mode === 'script' && scriptOptions} - -{/if} + const contextManager = new ContextManager() + + $effect(() => { + if (scriptOptions) { + contextManager.updateAvailableContext( + scriptOptions, + $dbSchemas, + $workspaceStore ?? '', + !$copilotSessionModel?.model.endsWith('/thinking'), + untrack(() => contextManager.getSelectedContext()) + ) + } + }) + + $effect(() => { + displayMessages = ContextManager.updateDisplayMessages( + untrack(() => displayMessages), + $dbSchemas + ) + }) + contextManager?.getSelectedContext() ?? [], + () => contextManager.getSelectedContext(), (sc) => { - contextManager?.setSelectedContext(sc) + scriptOptions && contextManager.setSelectedContext(sc) } } - availableContext={contextManager?.getAvailableContext() ?? []} + availableContext={contextManager.getAvailableContext()} messages={$currentReply ? [ ...displayMessages, { role: 'assistant', content: $currentReply, - contextElements: contextManager?.getSelectedContext()?.filter((c) => c.type === 'code') + contextElements: contextManager.getSelectedContext().filter((c) => c.type === 'code') } ] : displayMessages} @@ -345,7 +345,7 @@ loadPastChat={(id) => { const chat = historyManager.loadPastChat(id) if (chat) { - displayMessages = chat.displayMessages + displayMessages = ContextManager.updateDisplayMessages(chat.displayMessages, $dbSchemas) messages = chat.actualMessages aiChatDisplay?.enableAutomaticScroll() } diff --git a/frontend/src/lib/components/copilot/chat/AIChatDisplay.svelte b/frontend/src/lib/components/copilot/chat/AIChatDisplay.svelte index 19f2155851..62122e6efa 100644 --- a/frontend/src/lib/components/copilot/chat/AIChatDisplay.svelte +++ b/frontend/src/lib/components/copilot/chat/AIChatDisplay.svelte @@ -14,9 +14,9 @@ import ChatQuickActions from './ChatQuickActions.svelte' import ProviderModelSelector from './ProviderModelSelector.svelte' import ChatMode from './ChatMode.svelte' + import { chatMode } from '$lib/stores' let { - mode = $bindable(), allowedModes, messages, instructions = $bindable(), @@ -34,7 +34,6 @@ headerLeft, headerRight }: { - mode: 'script' | 'flow' allowedModes: { script: boolean flow: boolean @@ -249,7 +248,7 @@ {/if}
0}> - {#if mode === 'script'} + {#if $chatMode === 'script'}
@@ -314,14 +313,14 @@ {/if}
- {#if mode === 'script' && hasDiff} + {#if $chatMode === 'script' && hasDiff} {/if}
- +
diff --git a/frontend/src/lib/components/copilot/chat/ChatMode.svelte b/frontend/src/lib/components/copilot/chat/ChatMode.svelte index d03eac13aa..d366fd7011 100644 --- a/frontend/src/lib/components/copilot/chat/ChatMode.svelte +++ b/frontend/src/lib/components/copilot/chat/ChatMode.svelte @@ -2,12 +2,11 @@ import { ChevronDown } from 'lucide-svelte' import Popover from '$lib/components/meltComponents/Popover.svelte' import { twMerge } from 'tailwind-merge' + import { chatMode } from '$lib/stores' let { - mode = $bindable(), allowedModes }: { - mode: 'script' | 'flow' allowedModes: { script: boolean flow: boolean @@ -22,7 +21,7 @@ class="text-tertiary text-xs flex flex-row items-center font-normal gap-0.5 border px-1 rounded-lg" > - {mode} mode + {$chatMode} mode {#if allowedModes.script && allowedModes.flow}
@@ -37,10 +36,10 @@