Skip to content

Commit

Permalink
fix: update naming for EditorContext.
Browse files Browse the repository at this point in the history
  • Loading branch information
icycodes committed Dec 23, 2024
1 parent 4e5fa14 commit b487fd6
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 41 deletions.
12 changes: 6 additions & 6 deletions clients/tabby-chat-panel/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ export type Location = number | LineRange | Position | PositionRange

/**
* Represents a client-side file context.
* This type should only be used for send context from client to server.
* This type should only be used for sending context from client to server.
*/
export interface ClientFileContext {
export interface EditorFileContext {
kind: 'file'

/**
Expand All @@ -77,9 +77,9 @@ export interface ClientFileContext {

/**
* Represents a client-side context.
* This type should only be used for send context from client to server.
* This type should only be used for sending context from client to server.
*/
export type ClientSideContext = ClientFileContext
export type EditorContext = EditorFileContext

export interface FetcherOptions {
authorization: string
Expand Down Expand Up @@ -220,9 +220,9 @@ export interface ServerApi {

showError: (error: ErrorMessage) => void
cleanError: () => void
addRelevantContext: (context: ClientSideContext) => void
addRelevantContext: (context: EditorContext) => void
updateTheme: (style: string, themeClass: string) => void
updateActiveSelection: (context: ClientFileContext | null) => void
updateActiveSelection: (context: EditorContext | null) => void
}

export interface ClientApiMethods {
Expand Down
4 changes: 2 additions & 2 deletions clients/vscode/src/chat/ChatPanelViewProvider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ExtensionContext, window, WebviewPanel } from "vscode";
import type { ServerApi, ChatCommand, ClientFileContext } from "tabby-chat-panel";
import type { ServerApi, ChatCommand, EditorContext } from "tabby-chat-panel";
import { WebviewHelper } from "./WebviewHelper";
import { Client } from "../lsp/Client";
import { GitProvider } from "../git/GitProvider";
Expand Down Expand Up @@ -61,7 +61,7 @@ export class ChatPanelViewProvider {
this.webviewHelper.executeCommand(command);
}

public addRelevantContext(context: ClientFileContext) {
public addRelevantContext(context: EditorContext) {
this.webviewHelper.addRelevantContext(context);
}
}
4 changes: 2 additions & 2 deletions clients/vscode/src/chat/ChatSideViewProvider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ExtensionContext, WebviewViewProvider, WebviewView, window } from "vscode";
import type { ServerApi, ChatCommand, ClientFileContext } from "tabby-chat-panel";
import type { ServerApi, ChatCommand, EditorContext } from "tabby-chat-panel";
import { WebviewHelper } from "./WebviewHelper";
import { Client } from "../lsp/Client";
import type { LogOutputChannel } from "../logger";
Expand Down Expand Up @@ -64,7 +64,7 @@ export class ChatSideViewProvider implements WebviewViewProvider {
this.webviewHelper.executeCommand(command);
}

public addRelevantContext(context: ClientFileContext) {
public addRelevantContext(context: EditorContext) {
this.webviewHelper.addRelevantContext(context);
}
}
6 changes: 3 additions & 3 deletions clients/vscode/src/chat/WebviewHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
import type {
ServerApi,
ChatCommand,
ClientFileContext,
EditorContext,
OnLoadedParams,
LookupSymbolHint,
SymbolInfo,
Expand Down Expand Up @@ -268,7 +268,7 @@ export class WebviewHelper {
return supportedSchemes.includes(scheme);
}

public async syncActiveSelectionToChatPanel(context: ClientFileContext | null) {
public async syncActiveSelectionToChatPanel(context: EditorContext | null) {
try {
await this.client?.updateActiveSelection(context);
} catch {
Expand All @@ -282,7 +282,7 @@ export class WebviewHelper {
}
}

public addRelevantContext(context: ClientFileContext) {
public addRelevantContext(context: EditorContext) {
if (this.client) {
this.logger.info(`Adding relevant context: ${context}`);
this.client.addRelevantContext(context);
Expand Down
6 changes: 3 additions & 3 deletions clients/vscode/src/chat/fileContext.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import type { TextEditor } from "vscode";
import type { ClientFileContext } from "tabby-chat-panel";
import type { EditorContext } from "tabby-chat-panel";
import type { GitProvider } from "../git/GitProvider";
import { localUriToChatPanelFilepath } from "./utils";

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

export async function getFileContext(
editor: TextEditor,
gitProvider: GitProvider,
useSelection = false,
): Promise<ClientFileContext | null> {
): Promise<EditorContext | null> {
const text = editor.document.getText(useSelection ? editor.selection : undefined);
if (!text || text.trim().length < 1) {
return null;
Expand Down
14 changes: 7 additions & 7 deletions ee/tabby-ui/app/chat/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import remarkMath from 'remark-math'
import {
TABBY_CHAT_PANEL_API_VERSION,
type ChatCommand,
type ClientFileContext,
type EditorContext,
type ErrorMessage,
type FetcherOptions,
type FileLocation,
Expand Down Expand Up @@ -53,10 +53,10 @@ export default function ChatPage() {
const [activeChatId, setActiveChatId] = useState('')
const [pendingCommand, setPendingCommand] = useState<ChatCommand>()
const [pendingRelevantContexts, setPendingRelevantContexts] = useState<
ClientFileContext[]
EditorContext[]
>([])
const [pendingActiveSelection, setPendingActiveSelection] =
useState<ClientFileContext | null>(null)
useState<EditorContext | null>(null)
const [errorMessage, setErrorMessage] = useState<ErrorMessage | null>(null)
const [isRefreshLoading, setIsRefreshLoading] = useState(false)

Expand Down Expand Up @@ -88,7 +88,7 @@ export default function ChatPage() {
}
}

const addRelevantContext = (ctx: ClientFileContext) => {
const addRelevantContext = (ctx: EditorContext) => {
if (chatRef.current) {
chatRef.current.addRelevantContext(ctx)
} else {
Expand All @@ -98,7 +98,7 @@ export default function ChatPage() {
}
}

const updateActiveSelection = (ctx: ClientFileContext | null) => {
const updateActiveSelection = (ctx: EditorContext | null) => {
if (chatRef.current) {
chatRef.current.updateActiveSelection(ctx)
} else if (ctx) {
Expand Down Expand Up @@ -130,7 +130,7 @@ export default function ChatPage() {
cleanError: () => {
setErrorMessage(null)
},
addRelevantContext: (context: ClientFileContext) => {
addRelevantContext: (context: EditorContext) => {
return addRelevantContext(context)
},
updateTheme: (style, themeClass) => {
Expand All @@ -153,7 +153,7 @@ export default function ChatPage() {
document.documentElement.className =
themeClass + ` client client-${client}`
},
updateActiveSelection: (context: ClientFileContext | null) => {
updateActiveSelection: (context: EditorContext | null) => {
return updateActiveSelection(context)
}
})
Expand Down
20 changes: 8 additions & 12 deletions ee/tabby-ui/components/chat/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { RefObject } from 'react'
import { compact, findIndex, isEqual, some, uniqWith } from 'lodash-es'
import type {
ChatCommand,
ClientFileContext,
EditorContext,
FileLocation,
GitRepository,
LookupSymbolHint,
Expand Down Expand Up @@ -37,7 +37,7 @@ import {
} from '@/lib/types/chat'
import {
cn,
convertClientFileContext,
convertEditorContext,
findClosestGitRepository,
getFileLocationFromContext,
getPromptForChatCommand,
Expand Down Expand Up @@ -103,9 +103,9 @@ export interface ChatRef {
executeCommand: (command: ChatCommand) => Promise<void>
stop: () => void
isLoading: boolean
addRelevantContext: (context: ClientFileContext) => void
addRelevantContext: (context: EditorContext) => void
focus: () => void
updateActiveSelection: (context: ClientFileContext | null) => void
updateActiveSelection: (context: EditorContext | null) => void
}

interface ChatProps extends React.ComponentProps<'div'> {
Expand Down Expand Up @@ -511,8 +511,8 @@ function ChatRenderer(
setRelevantContext(oldValue => appendContextAndDedupe(oldValue, context))
})

const addRelevantContext = (clientFileContext: ClientFileContext) => {
const context = convertClientFileContext(clientFileContext)
const addRelevantContext = (editorContext: EditorContext) => {
const context = convertEditorContext(editorContext)
handleAddRelevantContext.current?.(context)
}

Expand All @@ -534,12 +534,8 @@ function ChatRenderer(
300
)

const updateActiveSelection = (
clientFileContext: ClientFileContext | null
) => {
const context = clientFileContext
? convertClientFileContext(clientFileContext)
: null
const updateActiveSelection = (editorContext: EditorContext | null) => {
const context = editorContext ? convertEditorContext(editorContext) : null
debouncedUpdateActiveSelection.run(context)
}

Expand Down
12 changes: 6 additions & 6 deletions ee/tabby-ui/lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { compact, isNil } from 'lodash-es'
import { customAlphabet } from 'nanoid'
import type {
ChatCommand,
ClientFileContext,
EditorContext,
FileLocation,
Filepath,
LineRange,
Expand Down Expand Up @@ -195,8 +195,8 @@ export function getPromptForChatCommand(command: ChatCommand) {
}
}

export function convertClientFileContext(
clientFileContext: ClientFileContext
export function convertEditorContext(
editorContext: EditorContext
): FileContext {
const convertRange = (range: LineRange | PositionRange | undefined) => {
// FIXME: If the range is not provided, the whole file is considered.
Expand Down Expand Up @@ -231,9 +231,9 @@ export function convertClientFileContext(

return {
kind: 'file',
content: clientFileContext.content,
range: convertRange(clientFileContext.range),
...convertFilepath(clientFileContext.filepath)
content: editorContext.content,
range: convertRange(editorContext.range),
...convertFilepath(editorContext.filepath)
}
}

Expand Down

0 comments on commit b487fd6

Please sign in to comment.