From 2f67a42a59c78ad8b00f7793ea93774c17b1dc55 Mon Sep 17 00:00:00 2001 From: liangfung Date: Thu, 12 Dec 2024 18:21:09 +0700 Subject: [PATCH] update: sync git url --- clients/tabby-chat-panel/src/index.ts | 2 ++ clients/vscode/src/chat/WebviewHelper.ts | 29 +++++++++++++++++- ee/tabby-ui/app/chat/page.tsx | 29 ++++++++---------- ee/tabby-ui/components/chat/chat-panel.tsx | 5 +++- ee/tabby-ui/components/chat/chat.tsx | 35 ++++++++++++++++++---- 5 files changed, 77 insertions(+), 23 deletions(-) diff --git a/clients/tabby-chat-panel/src/index.ts b/clients/tabby-chat-panel/src/index.ts index 0cfcfc43a0dc..f27fe0c71d5b 100644 --- a/clients/tabby-chat-panel/src/index.ts +++ b/clients/tabby-chat-panel/src/index.ts @@ -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 @@ -143,6 +144,7 @@ export function createServer(api: ServerApi): ClientApi { addRelevantContext: api.addRelevantContext, updateTheme: api.updateTheme, updateActiveSelection: api.updateActiveSelection, + updateGitUrl: api.updateGitUrl, }, }) } diff --git a/clients/vscode/src/chat/WebviewHelper.ts b/clients/vscode/src/chat/WebviewHelper.ts index 3bfa516f83d3..d30f0394947a 100644 --- a/clients/vscode/src/chat/WebviewHelper.ts +++ b/clients/vscode/src/chat/WebviewHelper.ts @@ -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; @@ -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); @@ -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) { @@ -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; @@ -364,6 +390,7 @@ export class WebviewHelper { public addTextEditorEventListeners() { window.onDidChangeActiveTextEditor((e) => { this.syncActiveSelection(e); + this.syncIndexedGitURL(e); }); window.onDidChangeTextEditorSelection((e) => { diff --git a/ee/tabby-ui/app/chat/page.tsx b/ee/tabby-ui/app/chat/page.tsx index 2c3c6a9e2940..a8eb1ce83377 100644 --- a/ee/tabby-ui/app/chat/page.tsx +++ b/ee/tabby-ui/app/chat/page.tsx @@ -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) => { @@ -60,6 +57,7 @@ export default function ChatPage() { >([]) const [pendingActiveSelection, setPendingActiveSelection] = useState(null) + const [pendingGitUrl, setPendingGitUrl] = useState() const [errorMessage, setErrorMessage] = useState(null) const [isRefreshLoading, setIsRefreshLoading] = useState(false) @@ -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 @@ -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(() => { @@ -276,6 +272,7 @@ export default function ChatPage() { setPendingRelevantContexts([]) setPendingMessages([]) setPendingActiveSelection(null) + setPendingGitUrl(undefined) } const onChatLoaded = () => { @@ -283,6 +280,7 @@ export default function ChatPage() { pendingMessages.forEach(sendMessage) chatRef.current?.updateActiveSelection(pendingActiveSelection) + chatRef.current?.updateGitUrl(pendingGitUrl) clearPendingState() setChatLoaded(true) @@ -409,7 +407,6 @@ export default function ChatPage() { isInEditor && (supportsOnLookupSymbol ? server?.onLookupSymbol : undefined) } - indexedRepository={data?.resolveGitUrl} /> ) diff --git a/ee/tabby-ui/components/chat/chat-panel.tsx b/ee/tabby-ui/components/chat/chat-panel.tsx index 4c48004b9148..0d56a41595a1 100644 --- a/ee/tabby-ui/components/chat/chat-panel.tsx +++ b/ee/tabby-ui/components/chat/chat-panel.tsx @@ -22,6 +22,8 @@ import { IconCheck, IconEye, IconEyeOff, + IconFileSearch2, + IconFileText, IconFolderGit, IconRefresh, IconRemove, @@ -237,7 +239,7 @@ function ChatPanelRenderer( - + Indexed repository of current workspace: @@ -265,6 +267,7 @@ function ChatPanelRenderer( } )} > + void focus: () => void updateActiveSelection: (context: Context | null) => void + updateGitUrl: (gitUrl: string | undefined) => void } interface ChatProps extends React.ComponentProps<'div'> { @@ -101,7 +104,6 @@ interface ChatProps extends React.ComponentProps<'div'> { ) => Promise chatInputRef: RefObject supportsOnApplyInEditorV2: boolean - indexedRepository: ResolveGitUrlQuery['resolveGitUrl'] } function ChatRenderer( @@ -123,8 +125,7 @@ function ChatRenderer( onApplyInEditor, onLookupSymbol, chatInputRef, - supportsOnApplyInEditorV2, - indexedRepository + supportsOnApplyInEditorV2 }: ChatProps, ref: React.ForwardedRef ) { @@ -137,11 +138,24 @@ function ChatRenderer( const [activeSelection, setActiveSelection] = React.useState( null ) + const [gitUrl, setGitUrl] = React.useState() const enableActiveSelection = useChatStore( state => state.enableActiveSelection ) + const enableIndexedRepository = useChatStore( + state => state.enableIndexedRepository + ) + const chatPanelRef = React.useRef(null) + const [{ data }] = useQuery({ + query: resolveGitUrlQuery, + variables: { + gitUrl: gitUrl as string + }, + pause: !gitUrl + }) + const { sendUserMessage, isLoading, @@ -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, @@ -519,7 +543,8 @@ function ChatRenderer( isLoading, addRelevantContext, focus: () => chatPanelRef.current?.focus(), - updateActiveSelection + updateActiveSelection, + updateGitUrl } }, [] @@ -555,7 +580,7 @@ function ChatRenderer( chatInputRef, activeSelection, supportsOnApplyInEditorV2, - indexedRepository + indexedRepository: gitUrl ? data?.resolveGitUrl : undefined }} >