Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
liangfung committed Dec 18, 2024
1 parent c1af81e commit b24c6b0
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 46 deletions.
4 changes: 2 additions & 2 deletions ee/tabby-schema/src/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ impl Query {
}

async fn repository_list(ctx: &Context) -> Result<Vec<Repository>> {
let user = check_user(ctx).await?;
let user = check_user_allow_auth_token(ctx).await?;

ctx.locator
.repository()
Expand All @@ -550,7 +550,7 @@ impl Query {
}

async fn context_info(ctx: &Context) -> Result<ContextInfo> {
let user = check_user_allow_auth_token(ctx).await?;
let user = check_user(ctx).await?;
ctx.locator.context().read(Some(&user.policy)).await
}

Expand Down
78 changes: 36 additions & 42 deletions ee/tabby-ui/components/chat/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,20 @@ import type {
import { useQuery } from 'urql'

import { ERROR_CODE_NOT_FOUND } from '@/lib/constants'
import { graphql } from '@/lib/gql/generates'
import {
CodeQueryInput,
ContextInfo,
ContextSourceKind,
CreateMessageInput,
InputMaybe,
MessageAttachmentCodeInput,
RepositorySourceListQuery,
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 {
AssistantMessage,
Expand All @@ -36,13 +34,27 @@ import {
UserMessage,
UserMessageWithOptionalId
} from '@/lib/types/chat'
import { cn, nanoid } from '@/lib/utils'
import { cn, findClosestRepositoryMatch, nanoid } from '@/lib/utils'

import { ChatPanel, ChatPanelRef } from './chat-panel'
import { ChatScrollAnchor } from './chat-scroll-anchor'
import { EmptyScreen } from './empty-screen'
import { QuestionAnswerList } from './question-answer'

const repositoryListQuery = graphql(/* GraphQL */ `
query RepositorySourceList {
repositoryList {
id
name
kind
gitUrl
sourceId
sourceName
sourceKind
}
}
`)

type ChatContextValue = {
initialized: boolean
threadId: string | undefined
Expand Down Expand Up @@ -71,8 +83,8 @@ type ChatContextValue = {
supportsOnApplyInEditorV2: boolean
selectedRepoId: string | undefined
setSelectedRepoId: React.Dispatch<React.SetStateAction<string | undefined>>
repos: ContextInfo['sources']
fetchingSources: boolean
repos: RepositorySourceListQuery['repositoryList'] | undefined
fetchingRepos: boolean
}

export const ChatContext = React.createContext<ChatContextValue>(
Expand Down Expand Up @@ -160,20 +172,11 @@ function ChatRenderer(
)

const chatPanelRef = React.useRef<ChatPanelRef>(null)
const [{ data: contextInfoData, fetching: fetchingSources }] = useQuery({
query: contextInfoQuery

const [{ data: repositoryListData, fetching: fetchingRepos }] = useQuery({
query: repositoryListQuery
})
const repos = React.useMemo(() => {
return (
contextInfoData?.contextInfo?.sources.filter(source => {
return [
ContextSourceKind.Git,
ContextSourceKind.Github,
ContextSourceKind.Gitlab
].includes(source.sourceKind)
}) ?? []
)
}, [contextInfoData])
const repos = repositoryListData?.repositoryList

const {
sendUserMessage,
Expand Down Expand Up @@ -530,30 +533,31 @@ function ChatRenderer(
}
}

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) {
if (gitRepoInfo?.length && repos?.length) {
const defaultGitUrl = gitRepoInfo[0].gitUrl
const repo = await resolveGitUrl(defaultGitUrl)
if (repo?.data?.resolveGitUrl) {
setSelectedRepoId(repo.data.resolveGitUrl.sourceId)
const targetGirUrl = findClosestRepositoryMatch(
defaultGitUrl,
repos.map(x => x.gitUrl)
)
if (targetGirUrl) {
const repo = repos.find(x => x.gitUrl === targetGirUrl)
if (repo) {
setSelectedRepoId(repo.sourceId)
}
}
}

setInitialized(true)
}

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

React.useEffect(() => {
if (initialized) {
Expand Down Expand Up @@ -600,7 +604,7 @@ function ChatRenderer(
selectedRepoId,
setSelectedRepoId,
repos,
fetchingSources,
fetchingRepos,
initialized
}}
>
Expand Down Expand Up @@ -669,13 +673,3 @@ function formatThreadRunErrorMessage(error: ExtendedCombinedError | undefined) {

return error.message || 'Failed to fetch'
}

function isValidContextForCodeQuery(context: FileContext | undefined) {
if (!context) return false

const isUntitledFile =
context.filepath.startsWith('untitled:') &&
!filename2prism(context.filepath)[0]

return !isUntitledFile
}
4 changes: 2 additions & 2 deletions ee/tabby-ui/components/chat/repo-select.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useRef, useState } from 'react'

import { ContextInfo } from '@/lib/gql/generates/graphql'
import { RepositorySourceListQuery } from '@/lib/gql/generates/graphql'
import { cn } from '@/lib/utils'
import { Badge } from '@/components/ui/badge'
import { Button } from '@/components/ui/button'
Expand Down Expand Up @@ -29,7 +29,7 @@ import LoadingWrapper from '@/components/loading-wrapper'
import { SourceIcon } from '../source-icon'

interface RepoSelectProps {
repos: ContextInfo['sources'] | undefined
repos: RepositorySourceListQuery['repositoryList'] | undefined
value: string | undefined
onChange: (v: string | undefined) => void
isInitializing?: boolean
Expand Down
1 change: 1 addition & 0 deletions ee/tabby-ui/lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { AttachmentCodeItem, AttachmentDocItem } from '@/lib/types'
import { Maybe } from '../gql/generates/graphql'

export * from './chat'
export * from './repository'

export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
Expand Down

0 comments on commit b24c6b0

Please sign in to comment.