Skip to content

Commit

Permalink
AI Chat: fix action menu (#26555)
Browse files Browse the repository at this point in the history
  • Loading branch information
petemill committed Nov 20, 2024
1 parent 2879f23 commit 5fe6921
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
11 changes: 3 additions & 8 deletions components/ai_chat/resources/page/state/conversation_context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
// You can obtain one at https://mozilla.org/MPL/2.0/.

import * as React from 'react'

import * as mojom from 'gen/brave/components/ai_chat/core/common/mojom/ai_chat.mojom.m.js'
import usePromise from '$web-common/usePromise'
import * as API from '../api/'
import { useAIChat } from './ai_chat_context'
import { isLeoModel } from '../model_utils'
import { loadTimeData } from '$web-common/loadTimeData'
import { useAIChat } from './ai_chat_context'

const MAX_INPUT_CHAR = 2000
const CHAR_LIMIT_THRESHOLD = MAX_INPUT_CHAR * 0.8
Expand Down Expand Up @@ -126,10 +124,8 @@ export const getFirstValidAction = (actionList: mojom.ActionGroup[]) =>

export function useActionMenu(
filter: string,
getActions: () => Promise<mojom.ActionGroup[]>
actionList: mojom.ActionGroup[]
) {
const { result: actionList = [] } = usePromise(getActions, [])

return React.useMemo(() => {
const reg = new RegExp(/^\/\w+/)

Expand Down Expand Up @@ -309,8 +305,7 @@ export function ConversationContextProvider(
})
}, [context.conversationUuid, context.faviconCacheKey])

const actionList = useActionMenu(context.inputText, () =>
Promise.resolve(aiChatContext.allActions)
const actionList = useActionMenu(context.inputText, aiChatContext.allActions
)

const shouldShowLongConversationInfo = React.useMemo(() => {
Expand Down
10 changes: 8 additions & 2 deletions components/ai_rewriter/resources/page/Context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,21 @@ export default function Context(props: React.PropsWithChildren) {
const [forwardHistory, setForwardHistory] = React.useState<string[]>([])
const [backHistory, setBackHistory] = React.useState<string[]>([])
const [isGenerating, setIsGenerating] = React.useState(false)
const [allActions, setAllActions] = React.useState<ActionGroup[]>([])

const actionList = useActionMenu(instructionsText, () => getRewriterPageHandler().getActionMenuList().then(({ actionList }) => actionList))
const actionList = useActionMenu(instructionsText, allActions)
const charCountContext = useCharCountInfo(instructionsText)

React.useEffect(() => {
getRewriterPageHandler()
const rewriterAPI = getRewriterPageHandler()
rewriterAPI
.getInitialText()
.then(({ initialText }) => setInitialText(initialText))

rewriterAPI
.getActionMenuList()
.then(({ actionList }) => setAllActions(actionList))

const callbackRouter = getCallbackRouter()
callbackRouter.onUpdatedGeneratedText.addListener(setGeneratedText)
return () => {
Expand Down

0 comments on commit 5fe6921

Please sign in to comment.