feat(ai): open Ask AI side panel with a preprompt in two modes (#22582)
Add the ability to open the Ask AI side panel pre-filled with a prompt from any frontend component, with a mode to control whether the message is sent automatically or left for the user to review. - agentChatPrepromptState: holds the pending preprompt and its mode (PREFILL = fill only, SEND = fill and auto-submit) - useOpenAskAiPageWithPreprompt: seeds the new-thread draft, opens a fresh Ask AI thread and stores the preprompt intent - AgentChatPrepromptEffect: applies the intent once the chat editor and send listener are mounted, either restoring the editor content or dispatching the send event and clearing the editor <!-- This is an auto-generated description by cubic. --> <a href="https://cubic.dev/pr/twentyhq/twenty/pull/22582?utm_source=github" target="_blank" rel="noopener noreferrer" data-no-image-dialog="true"><picture><source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img alt="Review in cubic" src="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"></picture></a> <!-- End of auto-generated description by cubic. -->
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
import { useEffect } from 'react';
|
||||
import { isDefined } from 'twenty-shared/utils';
|
||||
|
||||
import { AGENT_CHAT_RESTORE_EDITOR_CONTENT_EVENT_NAME } from '@/ai/constants/AgentChatRestoreEditorContentEventName';
|
||||
import { agentChatPrepromptState } from '@/ai/states/agentChatPrepromptState';
|
||||
import { dispatchAgentChatSendMessageEvent } from '@/ai/utils/dispatchAgentChatSendMessageEvent';
|
||||
import { dispatchBrowserEvent } from '@/browser-event/utils/dispatchBrowserEvent';
|
||||
import { useAtomState } from '@/ui/utilities/state/jotai/hooks/useAtomState';
|
||||
|
||||
export const AgentChatPrepromptEffect = () => {
|
||||
const [agentChatPreprompt, setAgentChatPreprompt] = useAtomState(
|
||||
agentChatPrepromptState,
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (!isDefined(agentChatPreprompt)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const { text, mode } = agentChatPreprompt;
|
||||
|
||||
const timeoutId = setTimeout(() => {
|
||||
if (mode === 'SEND') {
|
||||
dispatchAgentChatSendMessageEvent();
|
||||
dispatchBrowserEvent(AGENT_CHAT_RESTORE_EDITOR_CONTENT_EVENT_NAME, {
|
||||
content: '',
|
||||
});
|
||||
} else {
|
||||
dispatchBrowserEvent(AGENT_CHAT_RESTORE_EDITOR_CONTENT_EVENT_NAME, {
|
||||
content: text,
|
||||
});
|
||||
}
|
||||
|
||||
setAgentChatPreprompt(null);
|
||||
}, 0);
|
||||
|
||||
return () => clearTimeout(timeoutId);
|
||||
}, [agentChatPreprompt, setAgentChatPreprompt]);
|
||||
|
||||
return null;
|
||||
};
|
||||
@@ -1,4 +1,5 @@
|
||||
import { AgentChatMessagesFetchEffect } from '@/ai/components/AgentChatMessagesFetchEffect';
|
||||
import { AgentChatPrepromptEffect } from '@/ai/components/AgentChatPrepromptEffect';
|
||||
import { AgentChatSessionStartTimeEffect } from '@/ai/components/AgentChatSessionStartTimeEffect';
|
||||
import { AgentChatStreamKeepAliveEffect } from '@/ai/components/AgentChatStreamKeepAliveEffect';
|
||||
import { AgentChatStreamSubscriptionEffect } from '@/ai/components/AgentChatStreamSubscriptionEffect';
|
||||
@@ -37,6 +38,7 @@ export const AgentChatRuntimeEffects = () => {
|
||||
<>
|
||||
<AgentChatMessagesFetchEffect />
|
||||
<AgentChatStreamSubscriptionEffect />
|
||||
<AgentChatPrepromptEffect />
|
||||
<AgentChatStreamKeepAliveEffect />
|
||||
<AgentChatSessionStartTimeEffect />
|
||||
{isAgentChatOpen && (
|
||||
|
||||
Reference in New Issue
Block a user