diff --git a/frontend/src/lib/components/ApiConnectForm.svelte b/frontend/src/lib/components/ApiConnectForm.svelte index dc5a5436a1..e2f5ebd4c0 100644 --- a/frontend/src/lib/components/ApiConnectForm.svelte +++ b/frontend/src/lib/components/ApiConnectForm.svelte @@ -7,7 +7,7 @@ import TestConnection from './TestConnection.svelte' import TestAiKey from './copilot/TestAIKey.svelte' import { aiResourceProvider } from './copilot/aiResourceProvider' - import { copilotInfo } from '$lib/aiStore' + import { getAiChatManager } from './copilot/chat/aiChatManagerContext' import Popover from './meltComponents/Popover.svelte' import Button from './common/button/Button.svelte' import { Loader2 } from 'lucide-svelte' @@ -60,6 +60,8 @@ onSynced = undefined }: Props = $props() + const aiChat = getAiChatManager() + let aiProvider = $derived(aiResourceProvider(resourceType, args)) let schema = $state(emptySchema()) @@ -225,7 +227,7 @@ {aiProvider} workspace={workspace ?? $operatingWorkspace} resourceValue={args} - model={$copilotInfo.aiModels.find((m) => m.provider === aiProvider)?.model} + model={aiChat.aiInfo?.aiModels.find((m) => m.provider === aiProvider)?.model} /> {/if} {#if resourceType == 'postgresql'} diff --git a/frontend/src/lib/components/DiffDrawer.svelte b/frontend/src/lib/components/DiffDrawer.svelte index b7b5aa8922..fe647785ce 100644 --- a/frontend/src/lib/components/DiffDrawer.svelte +++ b/frontend/src/lib/components/DiffDrawer.svelte @@ -15,7 +15,9 @@ import type { Script } from '$lib/gen' import Select from './select/Select.svelte' import type { DiffAiReview, DiffVersionOption } from './diff_drawer' - import { copilotInfo } from '$lib/aiStore' + import { getAiChatManager } from '$lib/components/copilot/chat/aiChatManagerContext' + import type { AIChatManager } from '$lib/components/copilot/chat/AIChatManager.svelte' + import { untrack } from 'svelte' import AiReview from './deploymentRequest/AiReview.svelte' import type { ReviewScope } from './deploymentRequest/aiReview' @@ -61,9 +63,15 @@ interface Props { restoreDeployed?: () => Promise isFlow?: boolean + /** The session's chat manager, for a session editor view that mounts this drawer outside + * the session's context. */ + aiChat?: AIChatManager } - let { restoreDeployed = undefined, isFlow = false }: Props = $props() + let { restoreDeployed = undefined, isFlow = false, aiChat: sessionChat }: Props = $props() + + const aiChat = untrack(() => sessionChat) ?? getAiChatManager() + const aiInfo = $derived(aiChat.aiInfo) let data: | { @@ -386,11 +394,15 @@
- {#if data && aiReviewScope && $copilotInfo.enabled} + {#if data && aiReviewScope && aiInfo?.enabled}
{#key aiReviewScope} - + {/key}
{/if} diff --git a/frontend/src/lib/components/ResourceForm.svelte b/frontend/src/lib/components/ResourceForm.svelte index 2a23d593d0..3d076a25ff 100644 --- a/frontend/src/lib/components/ResourceForm.svelte +++ b/frontend/src/lib/components/ResourceForm.svelte @@ -17,7 +17,7 @@ import TestConnection from './TestConnection.svelte' import TestAiKey from './copilot/TestAIKey.svelte' import { aiResourceProvider, isTokenVertexResource } from './copilot/aiResourceProvider' - import { copilotInfo } from '$lib/aiStore' + import { getAiChatManager } from './copilot/chat/aiChatManagerContext' import GfmMarkdown from './GfmMarkdown.svelte' import TestTriggerConnection from './triggers/TestTriggerConnection.svelte' import GitHubAppIntegration from './GitHubAppIntegration.svelte' @@ -94,6 +94,8 @@ onCredentialStored }: Props = $props() + const aiChat = getAiChatManager() + let ws = $derived(workspace ?? $operatingWorkspace) let aiProvider = $derived(aiResourceProvider(resource_type, args)) @@ -325,7 +327,7 @@ {aiProvider} workspace={ws} resourceValue={args} - model={$copilotInfo.aiModels.find((m) => m.provider === aiProvider)?.model} + model={aiChat.aiInfo?.aiModels.find((m) => m.provider === aiProvider)?.model} /> {/if} {#if resource_type === 'git_repository' && ws && (actingUser?.is_admin || actingUser?.is_super_admin)} diff --git a/frontend/src/lib/components/copilot/AIFormAssistant.svelte b/frontend/src/lib/components/copilot/AIFormAssistant.svelte index c6accd4c64..8c6f206817 100644 --- a/frontend/src/lib/components/copilot/AIFormAssistant.svelte +++ b/frontend/src/lib/components/copilot/AIFormAssistant.svelte @@ -4,7 +4,7 @@ import { slide } from 'svelte/transition' import OpenInSessionButton from '$lib/components/sessions/OpenInSessionButton.svelte' import { useOperatingWorkspace } from '$lib/components/operatingWorkspace.svelte' - import { copilotInfo } from '$lib/aiStore' + import { getAiChatManager } from '$lib/components/copilot/chat/aiChatManagerContext' import { logFeatureUsage } from '$lib/utils/featureUsage' interface Props { @@ -19,6 +19,9 @@ const { onEditInstructions, instructions, runnableType, path }: Props = $props() + const aiChat = getAiChatManager() + const aiInfo = $derived(aiChat.aiInfo) + const operatingWorkspace = useOperatingWorkspace() let expanded = $state(false) @@ -46,7 +49,7 @@ {/snippet} -{#if $copilotInfo.workspaceDisabled} +{#if !aiInfo || aiInfo.workspaceDisabled} {:else if instructions}
diff --git a/frontend/src/lib/components/copilot/AIFormSettings.svelte b/frontend/src/lib/components/copilot/AIFormSettings.svelte index 20458589b7..5d420168e3 100644 --- a/frontend/src/lib/components/copilot/AIFormSettings.svelte +++ b/frontend/src/lib/components/copilot/AIFormSettings.svelte @@ -1,7 +1,7 @@ -{#if !$copilotInfo.workspaceDisabled} +{#if aiInfo && !aiInfo.workspaceDisabled} - import { copilotInfo } from '$lib/aiStore' + import { getAiChatManager } from '$lib/components/copilot/chat/aiChatManagerContext' import { codeCompletionSessionEnabled, CODE_COMPLETION_SETTING_NAME } from '$lib/stores' import { storeLocalSetting } from '$lib/utils' import Toggle from '../Toggle.svelte' + const aiChat = getAiChatManager() + const aiInfo = $derived(aiChat.aiInfo) + function storeSetting() { storeLocalSetting(CODE_COMPLETION_SETTING_NAME, $codeCompletionSessionEnabled.toString()) } -{#if $copilotInfo.enabled && $copilotInfo.codeCompletionModel} +{#if aiInfo?.enabled && aiInfo.codeCompletionModel} -{:else} +{:else if aiInfo} ({ + goto: vi.fn(), + afterNavigate: vi.fn(), + beforeNavigate: vi.fn() +})) +vi.mock('monaco-editor', () => ({ Selection: class Selection {} })) +vi.mock('$lib/utils/featureUsage', () => ({ logFeatureUsage: vi.fn() })) +vi.mock('./lib', async (importOriginal) => ({ + ...(await importOriginal()), + getNonStreamingMetadataCompletion: vi.fn(async () => '0 0 * * 1') +})) + +const CONFIGS: Record = { + staging: { + providers: { anthropic: { resource_path: 'u/admin/a', models: ['claude-x'] } }, + default_model: { provider: 'anthropic', model: 'claude-x' } + }, + fork: { + providers: { mistral: { resource_path: 'u/admin/m', models: ['fork-z'] } }, + default_model: { provider: 'mistral', model: 'fork-z' } + } +} + +vi.mock('$lib/gen', async (importOriginal) => ({ + ...(await importOriginal()), + WorkspaceService: { + getCopilotInfo: async ({ workspace }: { workspace: string }) => CONFIGS[workspace] + } +})) + +let component: ReturnType | undefined +afterEach(() => component && unmount(component)) + +it("a generator in a session's editor sends with the session's workspace, not the tab's", async () => { + // The tab's copy holds staging; the session works in its fork. + setCopilotInfo(CONFIGS.staging) + const manager = new AIChatManager() + manager.isSessionChat = true + manager.workspaceResolver = () => 'fork' + await loadWorkspaceCopilotInfo('fork') + + component = mount(CronGen, { + target: document.body, + props: { schedule: '', cronVersion: 'v2' }, + context: new Map([['aiChatManager', manager]]) + }) + flushSync() + document.querySelector('button[title="AI Assistant"]')!.click() + await tick() + const input = document.querySelector( + 'input[placeholder="CRON schedule description"]' + )! + input.value = 'every monday' + input.dispatchEvent(new Event('input', { bubbles: true })) + input.dispatchEvent(new KeyboardEvent('keypress', { key: 'Enter', bubbles: true })) + await tick() + + const target = vi.mocked(getNonStreamingMetadataCompletion).mock.calls[0]?.[2] + expect(target?.workspace).toBe('fork') + expect(target?.info.defaultModel?.model).toBe('fork-z') +}) diff --git a/frontend/src/lib/components/copilot/CronGen.svelte b/frontend/src/lib/components/copilot/CronGen.svelte index 5e54136f07..c0035331f4 100644 --- a/frontend/src/lib/components/copilot/CronGen.svelte +++ b/frontend/src/lib/components/copilot/CronGen.svelte @@ -6,8 +6,11 @@ import { sendUserToast } from '$lib/toast' import type { ChatCompletionMessageParam } from 'openai/resources/index.mjs' - import { copilotInfo } from '$lib/aiStore' import { untrack } from 'svelte' + import { getAiChatManager } from './chat/aiChatManagerContext' + + const aiChat = getAiChatManager() + const aiInfo = $derived(aiChat.aiInfo) interface Props { schedule: string @@ -53,7 +56,11 @@ } ] - const response = await getNonStreamingMetadataCompletion(messages, abortController) + const response = await getNonStreamingMetadataCompletion( + messages, + abortController, + aiChat.loadedSessionAiTarget + ) if (response.startsWith('ERROR:')) { throw response.replace('ERROR:', '').trim() @@ -78,7 +85,7 @@ }) -{#if !$copilotInfo.workspaceDisabled} +{#if aiInfo && !aiInfo.workspaceDisabled} {#snippet trigger()}