From 854c448bad2b7a9022c2e5530452e970e1218197 Mon Sep 17 00:00:00 2001 From: Zeno Kapitein Date: Fri, 22 Aug 2025 17:32:10 +0200 Subject: [PATCH] Custom assistants followup (#3584) --- .changeset/weak-kiwis-type.md | 6 ++++++ packages/gitbook/src/components/AI/useAI.tsx | 17 +++++++++++++++++ .../components/AIActions/AIActionsDropdown.tsx | 12 +++++++++--- .../Integrations/LoadIntegrations.tsx | 8 +++++++- 4 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 .changeset/weak-kiwis-type.md diff --git a/.changeset/weak-kiwis-type.md b/.changeset/weak-kiwis-type.md new file mode 100644 index 000000000..e68447985 --- /dev/null +++ b/.changeset/weak-kiwis-type.md @@ -0,0 +1,6 @@ +--- +"@gitbook/browser-types": patch +"gitbook": patch +--- + +Custom assistants followup diff --git a/packages/gitbook/src/components/AI/useAI.tsx b/packages/gitbook/src/components/AI/useAI.tsx index 834319f00..6cb6744ba 100644 --- a/packages/gitbook/src/components/AI/useAI.tsx +++ b/packages/gitbook/src/components/AI/useAI.tsx @@ -34,6 +34,12 @@ export type Assistant = Omit & { */ mode?: 'overlay' | 'sidebar' | 'search'; + /** + * Whether the assistant is displayed in the page action menu. + * @default false + */ + pageAction: boolean; + /** * Icon of the assistant displayed in the UI. */ @@ -90,6 +96,7 @@ export function useAI(): AIContext { chatController.postMessage({ message: query }); } }, + pageAction: true, ui: true, mode: 'sidebar', }); @@ -114,6 +121,7 @@ export function useAI(): AIContext { ); } }, + pageAction: false, ui: false, mode: 'search', }); @@ -125,6 +133,15 @@ export function useAI(): AIContext { ...integrationAssistants.map((assistant) => ({ ...assistant, icon: , + open: (query?: string) => { + setSearchState((prev) => ({ + ask: null, // Reset ask as we assume the assistant will handle it + query: prev?.query ?? null, + global: prev?.global ?? false, + open: false, + })); + assistant.open(query); + }, })) ); } diff --git a/packages/gitbook/src/components/AIActions/AIActionsDropdown.tsx b/packages/gitbook/src/components/AIActions/AIActionsDropdown.tsx index a65ca97ce..5ac3c1e9c 100644 --- a/packages/gitbook/src/components/AIActions/AIActionsDropdown.tsx +++ b/packages/gitbook/src/components/AIActions/AIActionsDropdown.tsx @@ -25,7 +25,9 @@ interface AIActionsDropdownProps { */ export function AIActionsDropdown(props: AIActionsDropdownProps) { const ref = useRef(null); - const assistants = useAI().assistants.filter((assistant) => assistant.ui === true); + const assistants = useAI().assistants.filter( + (assistant) => assistant.ui === true && assistant.pageAction + ); const language = useLanguage(); return assistants.length > 0 || props.actions.markdown || props.actions.externalAI ? ( @@ -63,7 +65,9 @@ export function AIActionsDropdown(props: AIActionsDropdownProps) { */ function AIActionsDropdownMenuContent(props: AIActionsDropdownProps) { const { markdownPageUrl, actions } = props; - const assistants = useAI().assistants.filter((assistant) => assistant.ui === true); + const assistants = useAI().assistants.filter( + (assistant) => assistant.ui === true && assistant.pageAction + ); return ( <> @@ -103,7 +107,9 @@ function AIActionsDropdownMenuContent(props: AIActionsDropdownProps) { */ function DefaultAction(props: AIActionsDropdownProps) { const { markdownPageUrl, actions } = props; - const assistants = useAI().assistants.filter((assistant) => assistant.ui === true); + const assistants = useAI().assistants.filter( + (assistant) => assistant.ui === true && assistant.pageAction + ); if (assistants.length) { return ; diff --git a/packages/gitbook/src/components/Integrations/LoadIntegrations.tsx b/packages/gitbook/src/components/Integrations/LoadIntegrations.tsx index b4f198863..d1bbceed2 100644 --- a/packages/gitbook/src/components/Integrations/LoadIntegrations.tsx +++ b/packages/gitbook/src/components/Integrations/LoadIntegrations.tsx @@ -50,7 +50,13 @@ if (typeof window !== 'undefined') { integrationAssistants.setState( (state) => [ ...state, - { ...assistant, id, ui: assistant.ui ?? true, mode: 'overlay' }, + { + ...assistant, + id, + ui: assistant.ui ?? true, + mode: 'overlay', + pageAction: false, + }, ], true );