Skip to content

Commit

Permalink
update: sync git url
Browse files Browse the repository at this point in the history
  • Loading branch information
liangfung committed Dec 12, 2024
1 parent 165b41a commit 2f67a42
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 23 deletions.
2 changes: 2 additions & 0 deletions clients/tabby-chat-panel/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export interface ServerApi {
addRelevantContext: (context: Context) => void
updateTheme: (style: string, themeClass: string) => void
updateActiveSelection: (context: Context | null) => void
updateGitUrl: (gitUrl: string | undefined) => void
}
export interface SymbolInfo {
sourceFile: string
Expand Down Expand Up @@ -143,6 +144,7 @@ export function createServer(api: ServerApi): ClientApi {
addRelevantContext: api.addRelevantContext,
updateTheme: api.updateTheme,
updateActiveSelection: api.updateActiveSelection,
updateGitUrl: api.updateGitUrl,
},
})
}
29 changes: 28 additions & 1 deletion clients/vscode/src/chat/WebviewHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { GitProvider } from "../git/GitProvider";
import { createClient } from "./chatPanel";
import { Client as LspClient } from "../lsp/Client";
import { isBrowser } from "../env";
import { getFileContextFromSelection, showFileContext, openTextDocument } from "./fileContext";
import { getFileContextFromSelection, showFileContext, openTextDocument, buildFilePathParams } from "./fileContext";

export class WebviewHelper {
webview?: Webview;
Expand Down Expand Up @@ -269,6 +269,21 @@ export class WebviewHelper {
}
}

public async syncIndexedGitUrlToChatPanel(url: string | undefined) {
try {
this.logger.log('sync indexed git url', url)
await this.client?.updateGitUrl(url);
} catch {
this.logger.log(
{
every: 100,
level: "warn",
},
"Git URL sync failed. Please update your Tabby server to the latest version.",
);
}
}

public addRelevantContext(context: Context) {
if (!this.client) {
this.pendingRelevantContexts.push(context);
Expand Down Expand Up @@ -296,6 +311,7 @@ export class WebviewHelper {
this.pendingRelevantContexts.forEach((ctx) => this.addRelevantContext(ctx));
this.pendingMessages.forEach((message) => this.sendMessageToChatPanel(message));
this.syncActiveSelection(window.activeTextEditor);
this.syncIndexedGitURL(window.activeTextEditor);

const agentConfig = this.lspClient.agentConfig.current;
if (agentConfig?.server.token) {
Expand Down Expand Up @@ -349,6 +365,16 @@ export class WebviewHelper {
this.syncActiveSelectionToChatPanel(fileContext);
}

public async syncIndexedGitURL(editor: TextEditor | undefined) {
if (!editor) {
this.syncIndexedGitUrlToChatPanel(undefined);
return;
}

const filePathParams = await buildFilePathParams(editor.document.uri, this.gitProvider);
this.syncIndexedGitUrlToChatPanel(filePathParams?.gitRemoteUrl)
}

public addAgentEventListeners() {
this.lspClient.status.on("didChange", async (status: StatusInfo) => {
const agentConfig = this.lspClient.agentConfig.current;
Expand All @@ -364,6 +390,7 @@ export class WebviewHelper {
public addTextEditorEventListeners() {
window.onDidChangeActiveTextEditor((e) => {
this.syncActiveSelection(e);
this.syncIndexedGitURL(e);
});

window.onDidChangeTextEditorSelection((e) => {
Expand Down
29 changes: 13 additions & 16 deletions ee/tabby-ui/app/chat/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ import { MemoizedReactMarkdown } from '@/components/markdown'

import './page.css'

import { useQuery } from 'urql'

import { resolveGitUrlQuery } from '@/lib/tabby/query'
import { saveFetcherOptions } from '@/lib/tabby/token-management'

const convertToHSLColor = (style: string) => {
Expand Down Expand Up @@ -60,6 +57,7 @@ export default function ChatPage() {
>([])
const [pendingActiveSelection, setPendingActiveSelection] =
useState<Context | null>(null)
const [pendingGitUrl, setPendingGitUrl] = useState<string | undefined>()
const [errorMessage, setErrorMessage] = useState<ErrorMessage | null>(null)
const [isRefreshLoading, setIsRefreshLoading] = useState(false)

Expand Down Expand Up @@ -107,6 +105,14 @@ export default function ChatPage() {
}
}

const updateGitUrl = (gitUrl: string | undefined) => {
if (chatRef.current) {
chatRef.current.updateGitUrl(gitUrl)
} else if (gitUrl) {
setPendingGitUrl(gitUrl)
}
}

const server = useServer({
init: (request: InitRequest) => {
if (chatRef.current) return
Expand Down Expand Up @@ -154,18 +160,8 @@ export default function ChatPage() {
document.documentElement.className =
themeClass + ` client client-${client}`
},
updateActiveSelection
})

// FIXME get url from workspace
const workspaceGitURL = 'https://github.com/tabbyML/tabby'

const [{ data, fetching }] = useQuery({
query: resolveGitUrlQuery,
variables: {
gitUrl: workspaceGitURL
},
pause: !workspaceGitURL
updateActiveSelection,
updateGitUrl
})

useEffect(() => {
Expand Down Expand Up @@ -276,13 +272,15 @@ export default function ChatPage() {
setPendingRelevantContexts([])
setPendingMessages([])
setPendingActiveSelection(null)
setPendingGitUrl(undefined)
}

const onChatLoaded = () => {
pendingRelevantContexts.forEach(addRelevantContext)
pendingMessages.forEach(sendMessage)

chatRef.current?.updateActiveSelection(pendingActiveSelection)
chatRef.current?.updateGitUrl(pendingGitUrl)

clearPendingState()
setChatLoaded(true)
Expand Down Expand Up @@ -409,7 +407,6 @@ export default function ChatPage() {
isInEditor &&
(supportsOnLookupSymbol ? server?.onLookupSymbol : undefined)
}
indexedRepository={data?.resolveGitUrl}
/>
</ErrorBoundary>
)
Expand Down
5 changes: 4 additions & 1 deletion ee/tabby-ui/components/chat/chat-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import {
IconCheck,
IconEye,
IconEyeOff,
IconFileSearch2,
IconFileText,
IconFolderGit,
IconRefresh,
IconRemove,
Expand Down Expand Up @@ -237,7 +239,7 @@ function ChatPanelRenderer(
</Badge>
</motion.div>
</TooltipTrigger>
<TooltipContent className="text-md space-y-2">
<TooltipContent className="text-md space-y-2" align="start">
<span className="font-semibold text-muted-foreground">
Indexed repository of current workspace:
</span>
Expand Down Expand Up @@ -265,6 +267,7 @@ function ChatPanelRenderer(
}
)}
>
<IconFileText />
<ContextLabel
context={activeSelection}
className="flex-1 truncate"
Expand Down
35 changes: 30 additions & 5 deletions ee/tabby-ui/components/chat/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
NavigateOpts,
SymbolInfo
} from 'tabby-chat-panel'
import { useQuery } from 'urql'

import { ERROR_CODE_NOT_FOUND } from '@/lib/constants'
import {
Expand All @@ -21,6 +22,7 @@ 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 { resolveGitUrlQuery } from '@/lib/tabby/query'
import { ExtendedCombinedError } from '@/lib/types'
import {
AssistantMessage,
Expand Down Expand Up @@ -75,6 +77,7 @@ export interface ChatRef {
addRelevantContext: (context: Context) => void
focus: () => void
updateActiveSelection: (context: Context | null) => void
updateGitUrl: (gitUrl: string | undefined) => void
}

interface ChatProps extends React.ComponentProps<'div'> {
Expand All @@ -101,7 +104,6 @@ interface ChatProps extends React.ComponentProps<'div'> {
) => Promise<SymbolInfo | undefined>
chatInputRef: RefObject<HTMLTextAreaElement>
supportsOnApplyInEditorV2: boolean
indexedRepository: ResolveGitUrlQuery['resolveGitUrl']
}

function ChatRenderer(
Expand All @@ -123,8 +125,7 @@ function ChatRenderer(
onApplyInEditor,
onLookupSymbol,
chatInputRef,
supportsOnApplyInEditorV2,
indexedRepository
supportsOnApplyInEditorV2
}: ChatProps,
ref: React.ForwardedRef<ChatRef>
) {
Expand All @@ -137,11 +138,24 @@ function ChatRenderer(
const [activeSelection, setActiveSelection] = React.useState<Context | null>(
null
)
const [gitUrl, setGitUrl] = React.useState<string | undefined>()
const enableActiveSelection = useChatStore(
state => state.enableActiveSelection
)
const enableIndexedRepository = useChatStore(
state => state.enableIndexedRepository
)

const chatPanelRef = React.useRef<ChatPanelRef>(null)

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

const {
sendUserMessage,
isLoading,
Expand Down Expand Up @@ -506,9 +520,19 @@ function ChatRenderer(
300
)

const debouncedUpdateGitUrl = useDebounceCallback(
(url: string | undefined) => {
setGitUrl(url)
},
300
)

const updateActiveSelection = (ctx: Context | null) => {
debouncedUpdateActiveSelection.run(ctx)
}
const updateGitUrl = (gitUrl: string | undefined) => {
debouncedUpdateGitUrl.run(gitUrl)
}

React.useImperativeHandle(
ref,
Expand All @@ -519,7 +543,8 @@ function ChatRenderer(
isLoading,
addRelevantContext,
focus: () => chatPanelRef.current?.focus(),
updateActiveSelection
updateActiveSelection,
updateGitUrl
}
},
[]
Expand Down Expand Up @@ -555,7 +580,7 @@ function ChatRenderer(
chatInputRef,
activeSelection,
supportsOnApplyInEditorV2,
indexedRepository
indexedRepository: gitUrl ? data?.resolveGitUrl : undefined
}}
>
<div className="flex justify-center overflow-x-hidden">
Expand Down

0 comments on commit 2f67a42

Please sign in to comment.