-
Notifications
You must be signed in to change notification settings - Fork 245
chore(compass-assistant): split drawer and provider usage for connections context COMPASS-9603 #7199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore(compass-assistant): split drawer and provider usage for connections context COMPASS-9603 #7199
Changes from 18 commits
a9f31a7
2688960
2cb2cca
989329b
27fe44a
9fc210d
95ddec6
9ab5ebb
20e985e
354afd6
ac6fb52
f5436f2
41cb841
dbdee46
6212cb1
dff3d43
30847bd
f65bcf6
708626d
a2cfff9
60ca22f
5c6bcb7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,25 @@ | ||
| import { DrawerSection } from '@mongodb-js/compass-components'; | ||
| import React, { type PropsWithChildren, useCallback, useRef } from 'react'; | ||
| import { type UIMessage, useChat } from './@ai-sdk/react/use-chat'; | ||
| import React, { type PropsWithChildren, useRef } from 'react'; | ||
| import { type UIMessage } from './@ai-sdk/react/use-chat'; | ||
| import type { Chat } from './@ai-sdk/react/chat-react'; | ||
| import { AssistantChat } from './assistant-chat'; | ||
| import { usePreference } from 'compass-preferences-model/provider'; | ||
| import { createContext, useContext } from 'react'; | ||
|
|
||
| export const ASSISTANT_DRAWER_ID = 'compass-assistant-drawer'; | ||
|
|
||
| import { createContext, useContext } from 'react'; | ||
| interface AssistantContextType { | ||
| chat: Chat<UIMessage>; | ||
| isEnabled: boolean; | ||
| } | ||
|
|
||
| type AssistantActions = unknown; | ||
| export const AssistantContext = createContext<AssistantContextType | null>( | ||
| null | ||
| ); | ||
|
|
||
| export const AssistantActionsContext = createContext<AssistantActions>({}); | ||
| type AssistantActionsContextType = unknown; | ||
| export const AssistantActionsContext = | ||
| createContext<AssistantActionsContextType>({}); | ||
|
|
||
| export function useAssistantActions(): AssistantActions { | ||
| export function useAssistantActions(): AssistantActionsContextType { | ||
| return useContext(AssistantActionsContext); | ||
| } | ||
|
|
||
|
|
@@ -24,35 +30,27 @@ export const AssistantProvider: React.FunctionComponent< | |
| > = ({ chat, children }) => { | ||
| const enableAIAssistant = usePreference('enableAIAssistant'); | ||
|
|
||
| const { messages, sendMessage } = useChat({ | ||
| const assistantContext = useRef<AssistantContextType>({ | ||
| chat, | ||
| isEnabled: enableAIAssistant, | ||
| }); | ||
| assistantContext.current = { | ||
|
||
| chat, | ||
| isEnabled: enableAIAssistant, | ||
| }; | ||
|
|
||
| const contextActions = useRef({}); | ||
|
|
||
| const handleMessageSend = useCallback( | ||
| (messageBody: string) => { | ||
| void sendMessage({ text: messageBody }); | ||
| }, | ||
| [sendMessage] | ||
| ); | ||
| const assistantActionsContext = useRef<AssistantActionsContextType>({}); | ||
|
|
||
| if (!enableAIAssistant) { | ||
| return <>{children}</>; | ||
| } | ||
|
|
||
| return ( | ||
| <AssistantActionsContext.Provider value={contextActions.current}> | ||
| <DrawerSection | ||
| id={ASSISTANT_DRAWER_ID} | ||
| title="MongoDB Assistant" | ||
| label="MongoDB Assistant" | ||
| glyph="Sparkle" | ||
| > | ||
| <AssistantChat messages={messages} onSendMessage={handleMessageSend} /> | ||
| </DrawerSection> | ||
| {children} | ||
| </AssistantActionsContext.Provider> | ||
| <AssistantContext.Provider value={assistantContext.current}> | ||
| <AssistantActionsContext.Provider value={assistantActionsContext.current}> | ||
| {children} | ||
| </AssistantActionsContext.Provider> | ||
| </AssistantContext.Provider> | ||
| ); | ||
| }; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import React, { useContext } from 'react'; | ||
| import { DrawerSection } from '@mongodb-js/compass-components'; | ||
| import { AssistantChat } from './assistant-chat'; | ||
| import { ASSISTANT_DRAWER_ID, AssistantContext } from './assistant-provider'; | ||
|
|
||
| /** | ||
| * CompassAssistantDrawer component that wraps AssistantChat in a DrawerSection. | ||
| * This component can be placed at any level in the component tree as long as | ||
| * it's within an AssistantProvider. | ||
| */ | ||
| export const CompassAssistantDrawer: React.FunctionComponent = () => { | ||
| const context = useContext(AssistantContext); | ||
|
|
||
| if (!context) { | ||
| throw new Error( | ||
| 'CompassAssistantDrawer must be used within an CompassAssistantProvider' | ||
| ); | ||
| } | ||
|
|
||
| if (!context.isEnabled) { | ||
| return null; | ||
| } | ||
|
|
||
| const { chat } = context; | ||
|
|
||
| return ( | ||
| <DrawerSection | ||
| id={ASSISTANT_DRAWER_ID} | ||
| title="MongoDB Assistant" | ||
| label="MongoDB Assistant" | ||
| glyph="Sparkle" | ||
| > | ||
| <AssistantChat chat={chat} /> | ||
| </DrawerSection> | ||
| ); | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,7 +58,10 @@ import { WebWorkspaceTab as WelcomeWorkspaceTab } from '@mongodb-js/compass-welc | |
| import { useCompassWebPreferences } from './preferences'; | ||
| import { DataModelingWorkspaceTab as DataModelingWorkspace } from '@mongodb-js/compass-data-modeling'; | ||
| import { DataModelStorageServiceProviderInMemory } from '@mongodb-js/compass-data-modeling/web'; | ||
| import { CompassAssistantProvider } from '@mongodb-js/compass-assistant'; | ||
| import { | ||
| CompassAssistantDrawer, | ||
| CompassAssistantProvider, | ||
| } from '@mongodb-js/compass-assistant'; | ||
|
|
||
| export type TrackFunction = ( | ||
| event: string, | ||
|
|
@@ -219,6 +222,7 @@ function CompassWorkspace({ | |
| <CreateNamespacePlugin></CreateNamespacePlugin> | ||
| <DropNamespacePlugin></DropNamespacePlugin> | ||
| <RenameCollectionPlugin></RenameCollectionPlugin> | ||
| <CompassAssistantDrawer /> | ||
gagik marked this conversation as resolved.
Show resolved
Hide resolved
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can add
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not really required if we don't have a reason for it be a plugin, so all good here from my perspective |
||
| </> | ||
| ); | ||
| }} | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the
chat.sendMessageis basically the same as the one returned by the hook. The hook basically just subscribes to the chat for re-rendering based on chat so we can just use it as our context object