Skip to content

Commit

Permalink
revert
Browse files Browse the repository at this point in the history
  • Loading branch information
liangfung committed Jan 2, 2025
1 parent 2dd4c97 commit aed733b
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 111 deletions.
41 changes: 18 additions & 23 deletions ee/tabby-ui/app/chat/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<FetcherOptions | null>(
null
)
Expand All @@ -62,6 +61,7 @@ export default function ChatPage() {
const [isRefreshLoading, setIsRefreshLoading] = useState(false)

const chatRef = useRef<ChatRef>(null)
const [chatLoaded, setChatLoaded] = useState(false)
const { width } = useWindowSize()
const prevWidthRef = useRef(width)
const chatInputRef = useRef<HTMLTextAreaElement>(null)
Expand All @@ -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) => {
Expand Down Expand Up @@ -116,6 +116,7 @@ export default function ChatPage() {
}

setActiveChatId(nanoid())
setIsInit(true)
setFetcherOptions(request.fetcherOptions)
useMacOSKeyboardEventHandler.current =
request.useMacOSKeyboardEventHandler
Expand Down Expand Up @@ -243,38 +244,34 @@ 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
) {
chatRef.current?.focus()
}
prevWidthRef.current = width
}, [width, isChatComponentLoaded])
}, [width, chatLoaded])

const clearPendingState = () => {
setPendingRelevantContexts([])
setPendingCommand(undefined)
setPendingActiveSelection(null)
}

const onChatLoaded = async () => {
const onChatLoaded = () => {
const currentChatRef = chatRef.current
if (!currentChatRef) return

Expand All @@ -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) => {
Expand All @@ -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()
Expand Down Expand Up @@ -380,7 +376,7 @@ export default function ChatPage() {
)
}

if (!isServerLoaded || !fetcherOptions) {
if (!isInit || !fetcherOptions) {
return (
<StaticContent>
<>
Expand Down Expand Up @@ -424,11 +420,10 @@ export default function ChatPage() {
openInEditor={openInEditor}
openExternal={openExternal}
readWorkspaceGitRepositories={
supportsReadWorkspaceGitRepoInfo
supportsProvideWorkspaceGitRepoInfo
? server?.readWorkspaceGitRepositories
: undefined
}
getActiveEditorSelection={getActiveEditorSelection}
/>
</ErrorBoundary>
)
Expand Down
53 changes: 22 additions & 31 deletions ee/tabby-ui/app/files/components/chat-side-bar.tsx
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -36,6 +32,7 @@ export const ChatSideBar: React.FC<ChatSideBarProps> = ({
React.useContext(SourceCodeBrowserContext)
const activeChatId = useChatStore(state => state.activeChatId)
const iframeRef = React.useRef<HTMLIFrameElement>(null)
const executedCommand = useRef(false)
const repoMapRef = useLatest(repoMap)
const openInCodeBrowser = async (fileLocation: FileLocation) => {
const { filepath, location } = fileLocation
Expand Down Expand Up @@ -75,25 +72,6 @@ export const ChatSideBar: React.FC<ChatSideBarProps> = ({
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()
Expand All @@ -116,10 +94,7 @@ export const ChatSideBar: React.FC<ChatSideBarProps> = ({
window.open(url, '_blank')
},
readWorkspaceGitRepositories: async () => {
return readWorkspaceGitRepositories.current()
},
getActiveEditorSelection: async () => {
return getActiveEditorSelection.current()
return readWorkspaceGitRepositories.current?.()
}
})

Expand Down Expand Up @@ -147,9 +122,25 @@ export const ChatSideBar: React.FC<ChatSideBarProps> = ({
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)
}

Expand Down
11 changes: 1 addition & 10 deletions ee/tabby-ui/app/files/components/source-code-browser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,6 @@ const SourceCodeBrowserRenderer: React.FC<SourceCodeBrowserProps> = ({
setInitialized,
chatSideBarVisible,
setChatSideBarVisible,
pendingEvent,
setPendingEvent,
repoMap,
setRepoMap,
Expand Down Expand Up @@ -656,10 +655,8 @@ const SourceCodeBrowserRenderer: React.FC<SourceCodeBrowserProps> = ({

React.useEffect(() => {
const onCallCompletion = (data: QuickActionEventPayload) => {
setChatSideBarVisible(true)
setPendingEvent(data)
// setTimeout(() => {
// setChatSideBarVisible(true)
// })
}
emitter.on('code_browser_quick_action', onCallCompletion)

Expand All @@ -668,12 +665,6 @@ const SourceCodeBrowserRenderer: React.FC<SourceCodeBrowserProps> = ({
}
}, [])

React.useEffect(() => {
if (pendingEvent && !chatSideBarVisible) {
setChatSideBarVisible(true)
}
}, [pendingEvent])

return (
<ResizablePanelGroup
direction="horizontal"
Expand Down
73 changes: 26 additions & 47 deletions ee/tabby-ui/components/chat/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { compact, findIndex, isEqual, some, uniqWith } from 'lodash-es'
import type {
ChatCommand,
EditorContext,
EditorFileContext,
FileLocation,
GitRepository,
LookupSymbolHint,
Expand Down Expand Up @@ -99,7 +98,7 @@ interface ChatProps extends React.ComponentProps<'div'> {
chatId: string
api?: string
initialMessages?: QuestionAnswerPair[]
onLoaded?: () => Promise<void>
onLoaded?: () => void
onThreadUpdates?: (messages: QuestionAnswerPair[]) => void
container?: HTMLDivElement
docQuery?: boolean
Expand All @@ -120,7 +119,6 @@ interface ChatProps extends React.ComponentProps<'div'> {
chatInputRef: RefObject<HTMLTextAreaElement>
supportsOnApplyInEditorV2: boolean
readWorkspaceGitRepositories?: () => Promise<GitRepository[]>
getActiveEditorSelection?: () => Promise<EditorFileContext | null>
}

function ChatRenderer(
Expand All @@ -143,12 +141,10 @@ function ChatRenderer(
openExternal,
chatInputRef,
supportsOnApplyInEditorV2,
readWorkspaceGitRepositories,
getActiveEditorSelection
readWorkspaceGitRepositories
}: ChatProps,
ref: React.ForwardedRef<ChatRef>
) {
const [isDataSetup, setIsDataSetup] = React.useState(false)
const [initialized, setInitialized] = React.useState(false)
const [threadId, setThreadId] = React.useState<string | undefined>()
const isOnLoadExecuted = React.useRef(false)
Expand Down Expand Up @@ -520,7 +516,7 @@ function ChatRenderer(
(ctx: Context | null) => {
setActiveSelection(ctx)
},
200
300
)

const updateActiveSelection = (editorContext: EditorContext | null) => {
Expand All @@ -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(
Expand All @@ -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,
Expand Down Expand Up @@ -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 && (
<div className={cn('pb-[200px] pt-4 md:pt-10', className)}>
{qaPairs?.length ? (
<QuestionAnswerList
messages={qaPairs}
chatMaxWidthClass={chatMaxWidthClass}
/>
) : (
<EmptyScreen
setInput={setInput}
chatMaxWidthClass={chatMaxWidthClass}
welcomeMessage={welcomeMessage}
/>
)}
<ChatScrollAnchor trackVisibility={isLoading} />
</div>
)}
<div className={cn('pb-[200px] pt-4 md:pt-10', className)}>
{qaPairs?.length ? (
<QuestionAnswerList
messages={qaPairs}
chatMaxWidthClass={chatMaxWidthClass}
/>
) : (
<EmptyScreen
setInput={setInput}
chatMaxWidthClass={chatMaxWidthClass}
welcomeMessage={welcomeMessage}
/>
)}
<ChatScrollAnchor trackVisibility={isLoading} />
</div>
<ChatPanel
onSubmit={handleSubmit}
className={cn('fixed inset-x-0 bottom-0', promptFormClassname)}
Expand Down

0 comments on commit aed733b

Please sign in to comment.