Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
liangfung committed Nov 14, 2024
1 parent cfe04af commit 52fdde8
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 46 deletions.
19 changes: 6 additions & 13 deletions 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 { ChatFeature } from "../lsp/ChatFeature";
import { isBrowser } from "../env";
import { getFileContextFromActiveEditor, getFileContextFromSelection, showFileContext } from "./fileContext";
import { getFileContextFromSelection, showFileContext } from "./fileContext";

export class WebviewHelper {
webview?: Webview;
Expand Down Expand Up @@ -340,7 +340,7 @@ export class WebviewHelper {
return;
}

const fileContext = await getFileContextFromActiveEditor(editor, this.gitProvider);
const fileContext = await getFileContextFromSelection(editor, this.gitProvider);
this.syncActiveSelectionToChatPanel(fileContext);
}

Expand All @@ -364,26 +364,19 @@ export class WebviewHelper {

public addTextEditorEventListeners() {
window.onDidChangeActiveTextEditor((e) => {
// Handle two cases here:
// 1. The active text editor is an empty untitled editor
// 2. No active text editor
if (e && e.document.uri.scheme !== "untitled") {
if (e && e.document.uri.scheme !== "file") {
this.syncActiveSelection(undefined)
return;
}
// this.logger.info('onDidChangeActiveTextEditor', !!e)

this.syncActiveSelection(e);
});

window.onDidChangeTextEditorSelection((e) => {
if (e.textEditor !== window.activeTextEditor) {
return;
}

// Check if the active editor is a file editor, for example, not the output window
// This listener only handles text files.
if (e.textEditor.document.uri.scheme !== "file") {
return;
}
// this.logger.info('onDidChangeTextEditorSelection')
this.syncActiveSelection(e.textEditor);
});
}
Expand Down
17 changes: 1 addition & 16 deletions clients/vscode/src/chat/fileContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,16 @@ export async function getFileContextFromSelection(
return getFileContext(editor, gitProvider, true);
}

export async function getFileContextFromActiveEditor(
editor: TextEditor,
gitProvider: GitProvider,
): Promise<FileContext | null> {
return getFileContext(editor, gitProvider, true, true);
}

export async function getFileContext(
editor: TextEditor,
gitProvider: GitProvider,
useSelection = false,
alwaysReturnContext = false,
): Promise<FileContext | null> {
const uri = editor.document.uri;
const text = editor.document.getText(useSelection ? editor.selection : undefined);
const isEmptyText = !text || text.trim().length < 1;
if (isEmptyText && !alwaysReturnContext) {
if (!text || text.trim().length < 1) {
return null;
}

const content = useSelection ? alignIndent(text) : text;
const range = useSelection
? {
Expand All @@ -45,11 +35,6 @@ export async function getFileContext(
end: editor.document.lineCount,
};

if (alwaysReturnContext && isEmptyText) {
range.start = 0;
range.end = 0;
}

const workspaceFolder = workspace.getWorkspaceFolder(uri);
const repo = gitProvider.getRepository(uri);
const gitRemoteUrl = repo ? gitProvider.getDefaultRemoteUrl(repo) : undefined;
Expand Down
8 changes: 3 additions & 5 deletions ee/tabby-ui/app/chat/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export default function ChatPage() {
const updateActiveSelection = (ctx: Context | null) => {
if (chatRef.current) {
chatRef.current.updateActiveSelection(ctx)
} else {
} else if (ctx) {
setPendingActiveSelection(ctx)
}
}
Expand Down Expand Up @@ -146,9 +146,7 @@ export default function ChatPage() {
document.documentElement.className =
themeClass + ` client client-${client}`
},
updateActiveSelection: context => {
return updateActiveSelection(context)
}
updateActiveSelection
})

useEffect(() => {
Expand Down Expand Up @@ -255,7 +253,7 @@ export default function ChatPage() {
const onChatLoaded = () => {
pendingRelevantContexts.forEach(addRelevantContext)
pendingMessages.forEach(sendMessage)
updateActiveSelection(pendingActiveSelection)
chatRef.current?.updateActiveSelection(pendingActiveSelection)

clearPendingState()
setChatLoaded(true)
Expand Down
2 changes: 1 addition & 1 deletion ee/tabby-ui/components/chat/chat-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ function ChatPanelRenderer(
</div>
<div className="border-t bg-background px-4 py-2 shadow-lg sm:space-y-4 sm:rounded-t-xl sm:border md:py-4">
<div className="flex flex-wrap gap-2">
<AnimatePresence>
<AnimatePresence presenceAffectsLayout>
{activeSelection ? (
<motion.div
initial={{ opacity: 0, scale: 0.9, y: -5 }}
Expand Down
18 changes: 8 additions & 10 deletions ee/tabby-ui/components/chat/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
UserMessage,
UserMessageWithOptionalId
} from '@/lib/types/chat'
import { cn, isUsableFileContext, nanoid } from '@/lib/utils'
import { cn, isValidFileContext, nanoid } from '@/lib/utils'

import { ListSkeleton } from '../skeleton'
import { ChatPanel, ChatPanelRef } from './chat-panel'
Expand Down Expand Up @@ -344,7 +344,7 @@ function ChatRenderer(
}

const codeQuery: InputMaybe<CodeQueryInput> =
contextForCodeQuery && isUsableFileContext(contextForCodeQuery)
contextForCodeQuery && isValidFileContext(contextForCodeQuery)
? {
content: contextForCodeQuery.content ?? '',
filepath: contextForCodeQuery.filepath,
Expand All @@ -356,7 +356,7 @@ function ChatRenderer(
: null

const hasUsableActiveContext =
enableActiveSelection && isUsableFileContext(userMessage.activeContext)
enableActiveSelection && isValidFileContext(userMessage.activeContext)
const fileContext: FileContext[] = uniqWith(
compact([
hasUsableActiveContext && userMessage.activeContext,
Expand Down Expand Up @@ -412,7 +412,7 @@ function ChatRenderer(
selectContext: userMessage.selectContext,
// For forward compatibility
activeContext:
enableActiveSelection && isUsableFileContext(finalActiveContext)
enableActiveSelection && isValidFileContext(finalActiveContext)
? finalActiveContext
: undefined
}
Expand Down Expand Up @@ -475,11 +475,12 @@ function ChatRenderer(
(ctx: Context | null) => {
setActiveSelection(ctx)
},
100
300
)

const updateActiveSelection = (ctx: Context | null) =>
const updateActiveSelection = (ctx: Context | null) => {
debouncedUpdateActiveSelection.run(ctx)
}

React.useImperativeHandle(
ref,
Expand All @@ -497,11 +498,8 @@ function ChatRenderer(
)

React.useEffect(() => {
if (isOnLoadExecuted.current) return

isOnLoadExecuted.current = true
onLoaded?.()
setInitialzed(true)
onLoaded?.()
}, [])

const chatMaxWidthClass = maxWidth ? `max-w-${maxWidth}` : 'max-w-2xl'
Expand Down
3 changes: 3 additions & 0 deletions ee/tabby-ui/lib/hooks/use-debounce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { debounce, DebouncedFunc, type DebounceSettings } from 'lodash-es'
import { useLatest } from './use-latest'
import { useUnmount } from './use-unmount'

// import { useUnmount } from './use-unmount'

type noop = (...args: any[]) => any

interface UseDebounceOptions<T extends noop> extends DebounceSettings {
Expand Down Expand Up @@ -32,6 +34,7 @@ function useDebounceCallback<T extends noop>(

useUnmount(() => {
options?.onUnmount?.(debounced)
console.log('cannnnnnnel')
debounced.cancel()
})

Expand Down
3 changes: 2 additions & 1 deletion ee/tabby-ui/lib/utils/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ export function isFileContextContentEmpty(context: FileContext) {
return context.range.start === 0
}

export function isUsableFileContext(context: FileContext | null | undefined) {
export function isValidFileContext(context: FileContext | null | undefined) {
if (!context) return false

// FIXME: evaluate the necessity of permitting an empty git_url
return !!context.git_url && !isFileContextContentEmpty(context)
}

0 comments on commit 52fdde8

Please sign in to comment.