From aed733b042d27aa177b86d06aa798277c3a61346 Mon Sep 17 00:00:00 2001 From: liangfung Date: Thu, 2 Jan 2025 17:11:28 +0800 Subject: [PATCH] revert --- ee/tabby-ui/app/chat/page.tsx | 41 +++++------ .../app/files/components/chat-side-bar.tsx | 53 ++++++-------- .../files/components/source-code-browser.tsx | 11 +-- ee/tabby-ui/components/chat/chat.tsx | 73 +++++++------------ 4 files changed, 67 insertions(+), 111 deletions(-) diff --git a/ee/tabby-ui/app/chat/page.tsx b/ee/tabby-ui/app/chat/page.tsx index 2a33e1cc6437..d7864b99000a 100644 --- a/ee/tabby-ui/app/chat/page.tsx +++ b/ee/tabby-ui/app/chat/page.tsx @@ -46,8 +46,7 @@ const convertToHSLColor = (style: string) => { } export default function ChatPage() { - const [isChatComponentLoaded, setIsChatComponentLoaded] = useState(false) - const [isServerLoaded, setIsServerLoaded] = useState(false) + const [isInit, setIsInit] = useState(false) const [fetcherOptions, setFetcherOptions] = useState( null ) @@ -62,6 +61,7 @@ export default function ChatPage() { const [isRefreshLoading, setIsRefreshLoading] = useState(false) const chatRef = useRef(null) + const [chatLoaded, setChatLoaded] = useState(false) const { width } = useWindowSize() const prevWidthRef = useRef(width) const chatInputRef = useRef(null) @@ -76,8 +76,8 @@ export default function ChatPage() { useState(false) const [supportsOnLookupSymbol, setSupportsOnLookupSymbol] = useState(false) const [ - supportsReadWorkspaceGitRepoInfo, - setSupportsReadWorkspaceGitRepoInfo + supportsProvideWorkspaceGitRepoInfo, + setSupportsProvideWorkspaceGitRepoInfo ] = useState(false) const executeCommand = (command: ChatCommand) => { @@ -116,6 +116,7 @@ export default function ChatPage() { } setActiveChatId(nanoid()) + setIsInit(true) setFetcherOptions(request.fetcherOptions) useMacOSKeyboardEventHandler.current = request.useMacOSKeyboardEventHandler @@ -243,22 +244,18 @@ export default function ChatPage() { server?.hasCapability('lookupSymbol').then(setSupportsOnLookupSymbol) server ?.hasCapability('readWorkspaceGitRepositories') - .then(setSupportsReadWorkspaceGitRepoInfo) + .then(setSupportsProvideWorkspaceGitRepoInfo) } - checkCapabilities().then(() => { - setTimeout(() => { - setIsServerLoaded(true) - }) - }) + checkCapabilities() } }, [server]) useLayoutEffect(() => { - if (!isChatComponentLoaded) return + if (!chatLoaded) return if ( width && - isServerLoaded && + isInit && fetcherOptions && !errorMessage && !prevWidthRef.current @@ -266,7 +263,7 @@ export default function ChatPage() { chatRef.current?.focus() } prevWidthRef.current = width - }, [width, isChatComponentLoaded]) + }, [width, chatLoaded]) const clearPendingState = () => { setPendingRelevantContexts([]) @@ -274,7 +271,7 @@ export default function ChatPage() { setPendingActiveSelection(null) } - const onChatLoaded = async () => { + const onChatLoaded = () => { const currentChatRef = chatRef.current if (!currentChatRef) return @@ -287,11 +284,14 @@ export default function ChatPage() { } if (pendingCommand) { - await currentChatRef.executeCommand(pendingCommand) + // FIXME: this delay is a workaround for waiting for the active selection to be updated + setTimeout(() => { + currentChatRef.executeCommand(pendingCommand) + }, 500) } clearPendingState() - await setIsChatComponentLoaded(true) + setChatLoaded(true) } const openInEditor = async (fileLocation: FileLocation) => { @@ -302,10 +302,6 @@ export default function ChatPage() { return server?.openExternal(url) } - const getActiveEditorSelection = async () => { - return server?.getActiveEditorSelection() ?? null - } - const refresh = async () => { setIsRefreshLoading(true) await server?.refresh() @@ -380,7 +376,7 @@ export default function ChatPage() { ) } - if (!isServerLoaded || !fetcherOptions) { + if (!isInit || !fetcherOptions) { return ( <> @@ -424,11 +420,10 @@ export default function ChatPage() { openInEditor={openInEditor} openExternal={openExternal} readWorkspaceGitRepositories={ - supportsReadWorkspaceGitRepoInfo + supportsProvideWorkspaceGitRepoInfo ? server?.readWorkspaceGitRepositories : undefined } - getActiveEditorSelection={getActiveEditorSelection} /> ) diff --git a/ee/tabby-ui/app/files/components/chat-side-bar.tsx b/ee/tabby-ui/app/files/components/chat-side-bar.tsx index 668c04cae262..52d1a1156586 100644 --- a/ee/tabby-ui/app/files/components/chat-side-bar.tsx +++ b/ee/tabby-ui/app/files/components/chat-side-bar.tsx @@ -1,10 +1,6 @@ -import React, { useState } from 'react' +import React, { useRef, useState } from 'react' import { find } from 'lodash-es' -import type { - EditorContext, - FileLocation, - GitRepository -} from 'tabby-chat-panel' +import type { FileLocation, GitRepository } from 'tabby-chat-panel' import { useClient } from 'tabby-chat-panel/react' import { RepositoryListQuery } from '@/lib/gql/generates/graphql' @@ -36,6 +32,7 @@ export const ChatSideBar: React.FC = ({ React.useContext(SourceCodeBrowserContext) const activeChatId = useChatStore(state => state.activeChatId) const iframeRef = React.useRef(null) + const executedCommand = useRef(false) const repoMapRef = useLatest(repoMap) const openInCodeBrowser = async (fileLocation: FileLocation) => { const { filepath, location } = fileLocation @@ -75,25 +72,6 @@ export const ChatSideBar: React.FC = ({ return list }) - const getActiveEditorSelection = useLatest(() => { - if (!pendingEvent) return null - const { lineFrom, lineTo, code, path, gitUrl } = pendingEvent - const activeSelection: EditorContext = { - kind: 'file', - content: code, - range: { - start: lineFrom, - end: lineTo ?? lineFrom - }, - filepath: { - kind: 'git', - filepath: path, - gitUrl - } - } - return activeSelection - }) - const client = useClient(iframeRef, { refresh: async () => { window.location.reload() @@ -116,10 +94,7 @@ export const ChatSideBar: React.FC = ({ window.open(url, '_blank') }, readWorkspaceGitRepositories: async () => { - return readWorkspaceGitRepositories.current() - }, - getActiveEditorSelection: async () => { - return getActiveEditorSelection.current() + return readWorkspaceGitRepositories.current?.() } }) @@ -147,9 +122,25 @@ export const ChatSideBar: React.FC = ({ React.useEffect(() => { if (pendingEvent && client && initialized) { const execute = async () => { - // todo notify activeselection change + const { lineFrom, lineTo, code, path, gitUrl } = pendingEvent + client.updateActiveSelection({ + kind: 'file', + content: code, + range: { + start: lineFrom, + end: lineTo ?? lineFrom + }, + filepath: { + kind: 'git', + filepath: path, + gitUrl + } + }) const command = getCommand(pendingEvent) - client.executeCommand(command) + // FIXME: this delay is a workaround for waiting for the active selection to be updated + setTimeout(() => { + client.executeCommand(command) + }, 500) setPendingEvent(undefined) } diff --git a/ee/tabby-ui/app/files/components/source-code-browser.tsx b/ee/tabby-ui/app/files/components/source-code-browser.tsx index a7c1e96c0b49..ef501e77a9b3 100644 --- a/ee/tabby-ui/app/files/components/source-code-browser.tsx +++ b/ee/tabby-ui/app/files/components/source-code-browser.tsx @@ -361,7 +361,6 @@ const SourceCodeBrowserRenderer: React.FC = ({ setInitialized, chatSideBarVisible, setChatSideBarVisible, - pendingEvent, setPendingEvent, repoMap, setRepoMap, @@ -656,10 +655,8 @@ const SourceCodeBrowserRenderer: React.FC = ({ React.useEffect(() => { const onCallCompletion = (data: QuickActionEventPayload) => { + setChatSideBarVisible(true) setPendingEvent(data) - // setTimeout(() => { - // setChatSideBarVisible(true) - // }) } emitter.on('code_browser_quick_action', onCallCompletion) @@ -668,12 +665,6 @@ const SourceCodeBrowserRenderer: React.FC = ({ } }, []) - React.useEffect(() => { - if (pendingEvent && !chatSideBarVisible) { - setChatSideBarVisible(true) - } - }, [pendingEvent]) - return ( { chatId: string api?: string initialMessages?: QuestionAnswerPair[] - onLoaded?: () => Promise + onLoaded?: () => void onThreadUpdates?: (messages: QuestionAnswerPair[]) => void container?: HTMLDivElement docQuery?: boolean @@ -120,7 +119,6 @@ interface ChatProps extends React.ComponentProps<'div'> { chatInputRef: RefObject supportsOnApplyInEditorV2: boolean readWorkspaceGitRepositories?: () => Promise - getActiveEditorSelection?: () => Promise } function ChatRenderer( @@ -143,12 +141,10 @@ function ChatRenderer( openExternal, chatInputRef, supportsOnApplyInEditorV2, - readWorkspaceGitRepositories, - getActiveEditorSelection + readWorkspaceGitRepositories }: ChatProps, ref: React.ForwardedRef ) { - const [isDataSetup, setIsDataSetup] = React.useState(false) const [initialized, setInitialized] = React.useState(false) const [threadId, setThreadId] = React.useState() const isOnLoadExecuted = React.useRef(false) @@ -520,7 +516,7 @@ function ChatRenderer( (ctx: Context | null) => { setActiveSelection(ctx) }, - 200 + 300 ) const updateActiveSelection = (editorContext: EditorContext | null) => { @@ -536,18 +532,10 @@ function ChatRenderer( } } - const initActiveEditorSelection = async () => { - return getActiveEditorSelection?.() - } - React.useEffect(() => { const init = async () => { - const [workspaceGitRepositories, activeEditorSelecition] = - await Promise.all([ - fetchWorkspaceGitRepo(), - initActiveEditorSelection() - ]) - // get default repository + const workspaceGitRepositories = await fetchWorkspaceGitRepo() + // get default repo if (workspaceGitRepositories?.length && repos?.length) { const defaultGitUrl = workspaceGitRepositories[0].url const repo = findClosestGitRepository( @@ -559,26 +547,19 @@ function ChatRenderer( } } - // update active selection - if (activeEditorSelecition) { - const context = convertEditorContext(activeEditorSelecition) - setActiveSelection(context) - } + setInitialized(true) } - if (!fetchingRepos && !isDataSetup) { - init().finally(() => { - setIsDataSetup(true) - }) + if (!fetchingRepos && !initialized) { + init() } - }, [fetchingRepos, isDataSetup]) + }, [fetchingRepos]) React.useEffect(() => { - if (isDataSetup) { + if (initialized) { onLoaded?.() - setInitialized(true) } - }, [isDataSetup]) + }, [initialized]) React.useImperativeHandle( ref, @@ -628,23 +609,21 @@ function ChatRenderer( className={`w-full px-4 md:pl-10 md:pr-[3.75rem] ${chatMaxWidthClass}`} > {/* FIXME: pb-[200px] might not enough when adding a large number of relevantContext */} - {initialized && ( -
- {qaPairs?.length ? ( - - ) : ( - - )} - -
- )} +
+ {qaPairs?.length ? ( + + ) : ( + + )} + +