From 97e21c3403a6f81914719969e75ba89077fb5f65 Mon Sep 17 00:00:00 2001 From: Weiko Date: Wed, 8 Jul 2026 22:52:38 +0200 Subject: [PATCH] feat(ai): optionally preselect fast or smart model when opening Ask AI (#22679) Allow front components to preselect the AI model when opening the Ask AI side panel with a preprompt. - Add an optional `model: 'FAST' | 'SMART'` to the preprompt in the openSidePanelPage AskAI params of the front-component SDK. - openAskAiPageWithPreprompt resolves FAST to the workspace fast model and SMART to the workspace default (null selection = pinned default model), writing to agentChatUserSelectedModelState. --- .../ai/hooks/useOpenAskAiPageWithPreprompt.ts | 22 +++++++++++++++++++ .../useFrontComponentExecutionContext.ts | 1 + .../frontComponentHostCommunicationApi.ts | 1 + 3 files changed, 24 insertions(+) diff --git a/packages/twenty-front/src/modules/ai/hooks/useOpenAskAiPageWithPreprompt.ts b/packages/twenty-front/src/modules/ai/hooks/useOpenAskAiPageWithPreprompt.ts index 1d0ed5790e..6ad2c30ab3 100644 --- a/packages/twenty-front/src/modules/ai/hooks/useOpenAskAiPageWithPreprompt.ts +++ b/packages/twenty-front/src/modules/ai/hooks/useOpenAskAiPageWithPreprompt.ts @@ -1,3 +1,5 @@ +import { isDefined } from 'twenty-shared/utils'; + import { useSwitchToNewAiChat } from '@/ai/hooks/useSwitchToNewAiChat'; import { AGENT_CHAT_NEW_THREAD_DRAFT_KEY, @@ -7,23 +9,43 @@ import { type AgentChatPrepromptMode, agentChatPrepromptState, } from '@/ai/states/agentChatPrepromptState'; +import { agentChatUserSelectedModelState } from '@/ai/states/agentChatUserSelectedModelState'; +import { currentWorkspaceState } from '@/auth/states/currentWorkspaceState'; +import { useAtomStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomStateValue'; import { useSetAtomState } from '@/ui/utilities/state/jotai/hooks/useSetAtomState'; +export type AgentChatModelPreselection = 'FAST' | 'SMART'; + export const useOpenAskAiPageWithPreprompt = () => { const { switchToNewChat } = useSwitchToNewAiChat(); + const currentWorkspace = useAtomStateValue(currentWorkspaceState); const setAgentChatDraftsByThreadId = useSetAtomState( agentChatDraftsByThreadIdState, ); const setAgentChatPreprompt = useSetAtomState(agentChatPrepromptState); + const setAgentChatUserSelectedModel = useSetAtomState( + agentChatUserSelectedModelState, + ); const openAskAiPageWithPreprompt = ({ text, mode = 'PREFILL', + model, }: { text: string; mode?: AgentChatPrepromptMode; + model?: AgentChatModelPreselection; }) => { switchToNewChat(); + + if (isDefined(model)) { + const fastModelId = currentWorkspace?.fastModel; + + setAgentChatUserSelectedModel( + model === 'FAST' && isDefined(fastModelId) ? fastModelId : null, + ); + } + setAgentChatDraftsByThreadId((prev) => ({ ...prev, [AGENT_CHAT_NEW_THREAD_DRAFT_KEY]: text, diff --git a/packages/twenty-front/src/modules/front-components/hooks/useFrontComponentExecutionContext.ts b/packages/twenty-front/src/modules/front-components/hooks/useFrontComponentExecutionContext.ts index 8b2c4fdb76..9e2cfa626c 100644 --- a/packages/twenty-front/src/modules/front-components/hooks/useFrontComponentExecutionContext.ts +++ b/packages/twenty-front/src/modules/front-components/hooks/useFrontComponentExecutionContext.ts @@ -203,6 +203,7 @@ export const useFrontComponentExecutionContext = ({ openAskAiPageWithPreprompt({ text: params.preprompt.text, mode: params.preprompt.mode, + model: params.preprompt.model, }); if (params.shouldResetSearchState === true) { diff --git a/packages/twenty-sdk/src/sdk/front-component/globals/frontComponentHostCommunicationApi.ts b/packages/twenty-sdk/src/sdk/front-component/globals/frontComponentHostCommunicationApi.ts index 15189a50a2..b765079939 100644 --- a/packages/twenty-sdk/src/sdk/front-component/globals/frontComponentHostCommunicationApi.ts +++ b/packages/twenty-sdk/src/sdk/front-component/globals/frontComponentHostCommunicationApi.ts @@ -53,6 +53,7 @@ export type OpenSidePanelPageParams = preprompt?: { text: string; mode?: 'PREFILL' | 'SEND'; + model?: 'FAST' | 'SMART'; }; } | {