Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
liangfung committed Dec 26, 2024
1 parent 96514ba commit 579c300
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 110 deletions.
36 changes: 16 additions & 20 deletions clients/tabby-chat-panel/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,11 @@ export interface LineRange {
*/
end: number
}
/**
* Represents a range of lines in a notebook file.
*/
export interface NotebookCellRange {
/**
* 0-based cell index
*/
cellIndex: number
/**
* 1-based line number
*/
start?: number
/**
* 1-based line number
*/
end?: number
}
/**
* Represents a location in a file.
* It could be a 1-based line number, a line range, a position or a position range.
*/
export type Location = number | LineRange | Position | PositionRange | NotebookCellRange
export type Location = number | LineRange | Position | PositionRange

/**
* Represents a client-side file context.
Expand All @@ -83,8 +66,21 @@ export interface EditorFileContext {
* The range of the selected content in the file.
* If the range is not provided, the whole file is considered.
*/
range?: LineRange | PositionRange | NotebookCellRange

range?: LineRange | PositionRange
/**
* The information of a jupyter notebook file
*/
notebookCell?: {
/**
* numeric identifier of the cell
*/
handle: number
/**
* The scheme of the notebook cell document
* eg: 'file', 'untitled'
*/
scheme: string
}
/**
* The content of the file context.
*/
Expand Down
9 changes: 7 additions & 2 deletions clients/vscode/src/chat/fileContext.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { TextEditor } from "vscode";
import type { EditorContext } from "tabby-chat-panel";
import type { GitProvider } from "../git/GitProvider";
import { localRangeToChatPanelRange, localUriToChatPanelFilepath } from "./utils";
import { localUriToChatPanelFilepath } from "./utils";

export async function getFileContextFromSelection(
editor: TextEditor,
Expand All @@ -20,9 +20,14 @@ export async function getFileContext(
return null;
}
const content = useSelection ? alignIndent(text) : text;
const range = useSelection
? {
start: editor.selection.start.line + 1,
end: editor.selection.end.line + 1,
}
: undefined;

const filepath = localUriToChatPanelFilepath(editor.document.uri, gitProvider);
const range = localRangeToChatPanelRange(editor, useSelection);

return {
kind: "file",
Expand Down
41 changes: 4 additions & 37 deletions clients/vscode/src/chat/utils.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import path from "path";
import { Position as VSCodePosition, Range as VSCodeRange, Uri, workspace, TextEditor, NotebookRange } from "vscode";
import { Position as VSCodePosition, Range as VSCodeRange, Uri, workspace } from "vscode";
import type {
Filepath,
Position as ChatPanelPosition,
LineRange,
PositionRange,
Location,
FilepathInGitRepository,
EditorFileContext,
NotebookCellRange,
} from "tabby-chat-panel";
import type { GitProvider } from "../git/GitProvider";
import { getLogger } from "../logger";
Expand All @@ -24,11 +22,9 @@ export function localUriToChatPanelFilepath(uri: Uri, gitProvider: GitProvider):
}
const gitRemoteUrl = repo ? gitProvider.getDefaultRemoteUrl(repo) : undefined;
if (repo && gitRemoteUrl) {
// todo check if is file or untitled ?
const uriFilePath =
uri.scheme === "vscode-notebook-cell" ? uri.with({ scheme: "file" }).toString(true) : uri.toString(true);
const relativeFilePath = path.relative(repo.rootUri.toString(true), uriFilePath);
logger.info(repo.rootUri.toString(true), uriFilePath, relativeFilePath);
if (!relativeFilePath.startsWith("..")) {
return {
kind: "git",
Expand All @@ -37,33 +33,12 @@ export function localUriToChatPanelFilepath(uri: Uri, gitProvider: GitProvider):
};
}
}

return {
kind: "uri",
uri: uri.toString(true),
};
}

export function localRangeToChatPanelRange(editor: TextEditor, useSelection?: boolean) {
const uri = editor.document.uri;
let range: EditorFileContext["range"];
if (uri.scheme == "vscode-notebook-cell") {
const notebook = parseVscodeNotebookCellURI(uri);
range = {
cellIndex: notebook?.handle || 0,
} as NotebookCellRange;
}
if (useSelection) {
range = {
...range,
start: editor.selection.start.line + 1,
end: editor.selection.end.line + 1,
};
}

return range;
}

export function vscodeNoteCellUriToChagePanelRange(uri: Uri) {
if (uri.scheme !== "vscode-notebook-cell") return undefined;
const notebook = parseVscodeNotebookCellURI(uri);
Expand All @@ -85,7 +60,8 @@ export function chatPanelFilepathToLocalUri(filepath: Filepath, gitProvider: Git
const localGitRoot = gitProvider.findLocalRootUriByRemoteUrl(filepath.gitUrl);
if (localGitRoot) {
const extname = path.extname(filepath.filepath);
// In VSCode, handle Jupyter Notebook files specially

// handling for Jupyter Notebook (.ipynb) files
if (extname.startsWith(".ipynb")) {
return chatPanelFilepathToVscodeNotebookCellUri(localGitRoot, filepath);
}
Expand All @@ -106,10 +82,7 @@ function chatPanelFilepathToVscodeNotebookCellUri(root: Uri, filepath: FilepathI
const parsedUrl = new URL(filepath.filepath, "file://");
const hash = parsedUrl.hash;
const cleanPath = parsedUrl.pathname;
// FIXME clean up newuri after testing
const newuri = Uri.joinPath(root, cleanPath).with({ scheme: "vscode-notebook-cell", fragment: hash.slice(1) });
logger.info(newuri.toString(true));
return newuri;
return Uri.joinPath(root, cleanPath).with({ scheme: "vscode-notebook-cell", fragment: hash.slice(1) });
}

export function vscodePositionToChatPanelPosition(position: VSCodePosition): ChatPanelPosition {
Expand All @@ -130,12 +103,6 @@ export function vscodeRangeToChatPanelPositionRange(range: VSCodeRange): Positio
};
}

// FIXME(jueliang) is it neccessary?
export function chatPanelNotebookCellRangeToVSCodeRange(lineRange: LineRange): VSCodeRange {
// Do not minus 1 from end line number, as we want to include the last line.
return new VSCodeRange(Math.max(0, lineRange.start - 1), 0, lineRange.end, 0);
}

export function chatPanelPositionRangeToVSCodeRange(positionRange: PositionRange): VSCodeRange {
return new VSCodeRange(
chatPanelPositionToVSCodePosition(positionRange.start),
Expand Down
12 changes: 1 addition & 11 deletions ee/tabby-ui/components/chat/code-references.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { forwardRef, useEffect, useState } from 'react'
import { isNil } from 'lodash-es'

import { VSCODE_NOTEBOOK_CELL_SCHEME } from '@/lib/constants'
import { RelevantCodeContext } from '@/lib/types'
import { cn, resolveFileNameForDisplay } from '@/lib/utils'
import {
Expand Down Expand Up @@ -153,10 +152,6 @@ function ContextItem({
context.range.start < context.range.end
const pathSegments = context.filepath.split('/')
const path = pathSegments.slice(0, pathSegments.length - 1).join('/')
const isVscodeNotebookCell = path.startsWith(VSCODE_NOTEBOOK_CELL_SCHEME)
// const showPath = !!path && !isVscodeNotebookCell
// todo
const showPath = true
const scores = context?.extra?.scores
const onTooltipOpenChange = (v: boolean) => {
if (!enableTooltip || !scores) return
Expand Down Expand Up @@ -197,12 +192,7 @@ function ContextItem({
)}
</>
) : null}
{/* FIXME(jueliang) */}
{showPath && (
<span className="ml-2 text-xs text-muted-foreground">
{path}
</span>
)}
<span className="ml-2 text-xs text-muted-foreground">{path}</span>
</div>
{showClientCodeIcon && (
<IconFileSearch2 className="shrink-0 text-muted-foreground" />
Expand Down
14 changes: 14 additions & 0 deletions ee/tabby-ui/lib/types/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ export interface FileContext {
* If the range is not provided, the whole file is considered.
*/
range?: { start: number; end: number }
// /**
// * The info of jupyter notebook cell
// */
// notebookCell?: {
// /**
// * numeric identifier of the cell
// */
// handle: number
// /**
// * The scheme of the document in notebook cell
// * eg 'file', 'untitled'
// */
// scheme: string
// }
content: string
git_url: string
}
Expand Down
33 changes: 17 additions & 16 deletions ee/tabby-ui/lib/utils/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
} from '@/lib/gql/generates/graphql'
import { MentionAttributes } from '@/lib/types'

import { VSCODE_NOTEBOOK_CELL_SCHEME } from '../constants'
import { MARKDOWN_SOURCE_REGEX } from '../constants/regex'

export const isCodeSourceContext = (kind: ContextSourceKind) => {
Expand Down Expand Up @@ -107,13 +106,12 @@ export function checkSourcesAvailability(
return { hasCodebaseSource, hasDocumentSource }
}

function parseVscodeNotebookCellURI(uri: string) {
if (!uri.startsWith(VSCODE_NOTEBOOK_CELL_SCHEME)) return undefined
function parseNotebookCellFragment(fragment: string) {
if (!fragment) return undefined

const _lengths = ['W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f']
const _padRegexp = new RegExp(`^[${_lengths.join('')}]+`)
const _radix = 7
const fragment = uri.split('#').pop() || ''
const idx = fragment.indexOf('s')
if (idx < 0) {
return undefined
Expand All @@ -136,17 +134,20 @@ function parseVscodeNotebookCellURI(uri: string) {
}

export function resolveFileNameForDisplay(uri: string) {
const regexPattern = `(?:${VSCODE_NOTEBOOK_CELL_SCHEME}:)?(.*?)(\\?|#|$)`
const regex = new RegExp(regexPattern)

const pathSegments = uri.split('/')
const fileName = pathSegments[pathSegments.length - 1]
const match = fileName.match(regex)
const displayName = match ? match[1] : null
const notebook = parseVscodeNotebookCellURI(uri)

if (displayName && notebook) {
return `${displayName} · Cell ${(notebook.handle || 0) + 1}`
let url: URL
try {
url = new URL(uri)
} catch (e) {
url = new URL(uri, 'file://')
}
const filename = url.pathname.split('/').pop() || ''
const extname = filename.includes('.') ? `.${filename.split('.').pop()}` : ''
const isNotebook = extname.startsWith('.ipynb')
const hash = url.hash ? url.hash.substring(1) : ''
const notebook = parseNotebookCellFragment(hash)

if (isNotebook && notebook) {
return `${filename} · Cell ${(notebook.handle || 0) + 1}`
}
return displayName
return filename
}
5 changes: 1 addition & 4 deletions ee/tabby-ui/lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import type {
Filepath,
LineRange,
Location,
NotebookCellRange,
Position,
PositionRange
} from 'tabby-chat-panel'
Expand Down Expand Up @@ -199,9 +198,7 @@ export function getPromptForChatCommand(command: ChatCommand) {
export function convertEditorContext(
editorContext: EditorContext
): FileContext {
const convertRange = (
range: LineRange | PositionRange | NotebookCellRange | undefined
) => {
const convertRange = (range: LineRange | PositionRange | undefined) => {
// If the range is not provided, the whole file is considered.
if (!range) {
return undefined
Expand Down
20 changes: 0 additions & 20 deletions test-open.ipynb

This file was deleted.

0 comments on commit 579c300

Please sign in to comment.