Skip to content

Commit

Permalink
update: init
Browse files Browse the repository at this point in the history
  • Loading branch information
liangfung committed Dec 17, 2024
1 parent 2d7f928 commit c87eaf6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 62 deletions.
6 changes: 2 additions & 4 deletions ee/tabby-ui/components/chat/chat-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,10 @@ function ChatPanelRenderer(
removeRelevantContext,
activeSelection,
onCopyContent,
indexedRepository,
selectedRepoId,
setSelectedRepoId,
repos,
fetchingRepos
initialized
} = React.useContext(ChatContext)
const enableActiveSelection = useChatStore(
state => state.enableActiveSelection
Expand Down Expand Up @@ -240,8 +239,7 @@ function ChatPanelRenderer(
value={selectedRepoId}
onChange={onSelectRepo}
repos={repos}
workspaceRepoId={indexedRepository?.sourceId}
isInitializing={fetchingRepos}
isInitializing={!initialized}
/>
{activeSelection ? (
<motion.div
Expand Down
91 changes: 44 additions & 47 deletions ee/tabby-ui/components/chat/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ import {
CreateMessageInput,
InputMaybe,
MessageAttachmentCodeInput,
ResolveGitUrlQuery,
ThreadRunOptionsInput
} from '@/lib/gql/generates/graphql'
import { useDebounceCallback } from '@/lib/hooks/use-debounce'
import { useLatest } from '@/lib/hooks/use-latest'
import { useThreadRun } from '@/lib/hooks/use-thread-run'
import { filename2prism } from '@/lib/language-utils'
import { useChatStore } from '@/lib/stores/chat-store'
import { client } from '@/lib/tabby/gql'
import { contextInfoQuery, resolveGitUrlQuery } from '@/lib/tabby/query'
import { ExtendedCombinedError } from '@/lib/types'
import {
Expand All @@ -45,6 +45,7 @@ import { EmptyScreen } from './empty-screen'
import { QuestionAnswerList } from './question-answer'

type ChatContextValue = {
initialized: boolean
threadId: string | undefined
isLoading: boolean
qaPairs: QuestionAnswerPair[]
Expand All @@ -69,11 +70,10 @@ type ChatContextValue = {
removeRelevantContext: (index: number) => void
chatInputRef: RefObject<HTMLTextAreaElement>
supportsOnApplyInEditorV2: boolean
indexedRepository: ResolveGitUrlQuery['resolveGitUrl']
selectedRepoId: string | undefined
setSelectedRepoId: React.Dispatch<React.SetStateAction<string | undefined>>
repos: ContextInfo['sources']
fetchingRepos: boolean
fetchingSources: boolean
}

export const ChatContext = React.createContext<ChatContextValue>(
Expand Down Expand Up @@ -151,8 +151,6 @@ function ChatRenderer(
const [activeSelection, setActiveSelection] = React.useState<Context | null>(
null
)
// gitUrl from workspace
const [gitUrl, setGitUrl] = React.useState<string | undefined>()
// sourceId
const [selectedRepoId, setSelectedRepoId] = React.useState<
string | undefined
Expand All @@ -178,14 +176,6 @@ function ChatRenderer(
)
}, [contextInfoData])

const [{ data: resolvedGitUrl }] = useQuery({
query: resolveGitUrlQuery,
variables: {
gitUrl: gitUrl as string
},
pause: !gitUrl
})

const {
sendUserMessage,
isLoading,
Expand All @@ -198,22 +188,6 @@ function ChatRenderer(
threadId
})

React.useEffect(() => {
const setDefaultRepoId = () => {
if (!resolvedGitUrl || !repos?.length) return
if (selectedRepoId) return

const defaultRepo = repos.find(
o => o.sourceId === resolvedGitUrl.resolveGitUrl?.sourceId
)
if (defaultRepo) {
setSelectedRepoId(defaultRepo.sourceId)
}
}

setDefaultRepoId()
}, [resolvedGitUrl, repos])

const onDeleteMessage = async (userMessageId: string) => {
if (!threadId) return

Expand Down Expand Up @@ -549,6 +523,45 @@ function ChatRenderer(
debouncedUpdateActiveSelection.run(ctx)
}

const fetchWorkspaceGitRepo = () => {
if (provideWorkspaceGitRepoInfo) {
return provideWorkspaceGitRepoInfo()
} else {
return []
}
}

const resolveGitUrl = async (gitUrl: string | undefined) => {
if (!gitUrl) return undefined
return client.query(resolveGitUrlQuery, { gitUrl }).toPromise()
}

React.useEffect(() => {
const init = async () => {
const gitRepoInfo = await fetchWorkspaceGitRepo()
// get default repo
if (gitRepoInfo?.length) {
const defaultGitUrl = gitRepoInfo[0].gitUrl
const repo = await resolveGitUrl(defaultGitUrl)
if (repo?.data?.resolveGitUrl) {
setSelectedRepoId(repo.data.resolveGitUrl.sourceId)
}
}

setInitialzed(true)
}

if (!fetchingSources && !initialized) {
init()
}
}, [fetchingSources])

React.useEffect(() => {
if (initialized) {
onLoaded?.()
}
}, [initialized])

React.useImperativeHandle(
ref,
() => {
Expand All @@ -564,22 +577,6 @@ function ChatRenderer(
[]
)

React.useEffect(() => {
const fetchWorkspaceGitRepo = () => {
if (provideWorkspaceGitRepoInfo) {
provideWorkspaceGitRepoInfo().then(repos => {
if (repos.length) {
setGitUrl(repos[0].gitUrl)
}
})
}
}

fetchWorkspaceGitRepo()
setInitialzed(true)
onLoaded?.()
}, [])

const chatMaxWidthClass = maxWidth ? `max-w-${maxWidth}` : 'max-w-2xl'
if (!initialized) {
return (
Expand All @@ -606,11 +603,11 @@ function ChatRenderer(
chatInputRef,
activeSelection,
supportsOnApplyInEditorV2,
indexedRepository: gitUrl ? resolvedGitUrl?.resolveGitUrl : undefined,
selectedRepoId,
setSelectedRepoId,
repos,
fetchingRepos: fetchingSources
fetchingSources,
initialized
}}
>
<div className="flex justify-center overflow-x-hidden">
Expand Down
15 changes: 4 additions & 11 deletions ee/tabby-ui/components/chat/repo-select.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useRef, useState } from 'react'
import { useWindowSize } from '@uidotdev/usehooks'

import { ContextInfo } from '@/lib/gql/generates/graphql'
import { cn } from '@/lib/utils'
Expand Down Expand Up @@ -30,27 +29,21 @@ import LoadingWrapper from '@/components/loading-wrapper'
import { SourceIcon } from '../source-icon'

interface RepoSelectProps {
repos: ContextInfo['sources']
repos: ContextInfo['sources'] | undefined
value: string | undefined
onChange: (v: string | undefined) => void
isInitializing?: boolean
// sourceId
workspaceRepoId?: string
}

export function RepoSelect({
repos,
value,
onChange,
isInitializing,
workspaceRepoId
isInitializing
}: RepoSelectProps) {
const [open, setOpen] = useState(false)
const commandListRef = useRef<HTMLDivElement>(null)

const { width } = useWindowSize()
const isExtraSmallScreen = typeof width === 'number' && width < 360

const onSelectRepo = (v: string) => {
onChange(v)
}
Expand All @@ -73,7 +66,7 @@ export function RepoSelect({
const selectedRepoName = selectedRepo?.sourceName

// if there's no repo, hide the repo select
if (!isInitializing && !repos.length) return null
if (!isInitializing && !repos?.length) return null

return (
<LoadingWrapper
Expand Down Expand Up @@ -156,7 +149,7 @@ export function RepoSelect({
<CommandList className="max-h-[30vh]" ref={commandListRef}>
<CommandEmpty>No context found</CommandEmpty>
<CommandGroup>
{repos.map(repo => {
{repos?.map(repo => {
const isSelected = repo.sourceId === value

return (
Expand Down

0 comments on commit c87eaf6

Please sign in to comment.