Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
liangfung committed Dec 27, 2024
1 parent 227e43a commit 70852db
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 41 deletions.
69 changes: 35 additions & 34 deletions clients/vscode/src/chat/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ export function isSupportedSchemeForActiveSelection(scheme: string) {
}

export function localUriToChatPanelFilepath(uri: Uri, gitProvider: GitProvider): Filepath {
let uriFilePath = uri.toString(true);
if (uri.scheme === Schemes.vscodeNotebookCell) {
const notebook = parseNotebookCellUri(uri);
if (notebook) {
// add fragment `#cell={number}` to filepath
uriFilePath = uri.with({ scheme: notebook.notebook.scheme, fragment: `cell=${notebook.handle}` }).toString(true);
}
}

const workspaceFolder = workspace.getWorkspaceFolder(uri);

let repo = gitProvider.getRepository(uri);
Expand All @@ -35,12 +44,6 @@ export function localUriToChatPanelFilepath(uri: Uri, gitProvider: GitProvider):
}
const gitRemoteUrl = repo ? gitProvider.getDefaultRemoteUrl(repo) : undefined;
if (repo && gitRemoteUrl) {
let uriFilePath: string = uri.toString(true);

if (uri.scheme === Schemes.vscodeNotebookCell) {
uriFilePath = localUriToUriFilePath(uri);
}

const relativeFilePath = path.relative(repo.rootUri.toString(true), uriFilePath);
if (!relativeFilePath.startsWith("..")) {
return {
Expand All @@ -53,26 +56,24 @@ export function localUriToChatPanelFilepath(uri: Uri, gitProvider: GitProvider):

return {
kind: "uri",
uri: localUriToUriFilePath(uri),
uri: uriFilePath,
};
}

function localUriToUriFilePath(uri: Uri): string {
let uriFilePath = uri.toString(true);

if (uri.scheme === Schemes.vscodeNotebookCell) {
const notebook = parseNotebookCellUri(uri);
if (notebook) {
// add fragment `#cell={number}` to filepath
uriFilePath = uri.with({ scheme: notebook.notebook.scheme, fragment: `cell=${notebook.handle}` }).toString(true);
}
}
return uriFilePath;
function isJupyterNotebookFilepath(filepath: Filepath): boolean {
const _filepath = filepath.kind === "uri" ? filepath.uri : filepath.filepath;
const extname = path.extname(_filepath);
return extname.startsWith(".ipynb");
}

export function chatPanelFilepathToLocalUri(filepath: Filepath, gitProvider: GitProvider): Uri | null {
const isNotebook = isJupyterNotebookFilepath(filepath);

if (filepath.kind === "uri") {
try {
if (isNotebook) {
return generateLocalNotebookCellUri(Uri.parse(filepath.uri), 0);
}
return Uri.parse(filepath.uri, true);
} catch (e) {
// FIXME(@icycodes): this is a hack for uri is relative filepaths in workspaces
Expand All @@ -84,10 +85,8 @@ export function chatPanelFilepathToLocalUri(filepath: Filepath, gitProvider: Git
} else if (filepath.kind === "git") {
const localGitRoot = gitProvider.findLocalRootUriByRemoteUrl(filepath.gitUrl);
if (localGitRoot) {
const extname = path.extname(filepath.filepath);

// handling for Jupyter Notebook (.ipynb) files
if (extname.startsWith(".ipynb")) {
if (isNotebook) {
return chatPanelFilepathToVscodeNotebookCellUri(localGitRoot, filepath);
}

Expand All @@ -104,24 +103,26 @@ function chatPanelFilepathToVscodeNotebookCellUri(root: Uri, filepath: FilepathI
return null;
}

const uri = Uri.joinPath(root, filepath.filepath);
const fileUri = Uri.parse(filepath.filepath);
const fragment = fileUri.fragment;

const uri = Uri.joinPath(root, fileUri.path);

let handle: number | undefined;
const fragment = uri.fragment;
if (fragment.startsWith("cell=")) {
const handleStr = fragment.slice("cell=".length);
const _handle = parseInt(handleStr, 10);
if (isNaN(_handle)) {
return uri;
}

const searchParams = new URLSearchParams(fragment);

if (searchParams.has("cell")) {
const cellString = searchParams.get("cell")?.toString() || "";
handle = parseInt(cellString, 10);
}

if (typeof handle === "undefined") {
if (typeof handle === "undefined" || isNaN(handle)) {
logger.warn(`Invalid handle in filepath.`, filepath.filepath);
return uri;
return null;
}

const cellUri = generateNotebookCellUri(uri, handle);
return cellUri;
return generateLocalNotebookCellUri(uri, handle);
}

export function vscodePositionToChatPanelPosition(position: VSCodePosition): ChatPanelPosition {
Expand Down Expand Up @@ -199,7 +200,7 @@ export function parseNotebookCellUri(cell: Uri): { notebook: Uri; handle: number
};
}

export function generateNotebookCellUri(notebook: Uri, handle: number): Uri {
export function generateLocalNotebookCellUri(notebook: Uri, handle: number): Uri {
const s = handle.toString(nb_radix);
const p = s.length < nb_lengths.length ? nb_lengths[s.length - 1] : "z";
const fragment = `${p}${s}s${Buffer.from(notebook.scheme).toString("base64")}`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,13 @@ export function AssistantMessageSection({
if (!ctx.filepath) return
const url = new URL(`${window.location.origin}/files`)
const searchParams = new URLSearchParams()
searchParams.append('redirect_filepath', ctx.filepath)

const filePathUrl = new URL(ctx.filepath, window.location.origin)

searchParams.append(
'redirect_filepath',
filePathUrl.pathname.replace(/^\//, '')
)
searchParams.append('redirect_git_url', ctx.git_url)
url.search = searchParams.toString()

Expand Down
14 changes: 8 additions & 6 deletions ee/tabby-ui/lib/utils/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,20 @@ export function checkSourcesAvailability(
}

/**
* url e.g: path/to/file.ipynb#handle=1
* @param uri
* url e.g #cell=1
* @param fragment
* @returns
*/
function parseNotebookCellUri(fragment: string) {
function parseNotebookCellUriFragment(fragment: string) {
if (!fragment) return undefined
try {
if (!fragment.startsWith('cell=')) {
const searchParams = new URLSearchParams(fragment)
const cellString = searchParams.get('cell')?.toString()
if (!cellString) {
return undefined
}

const handle = parseInt(fragment.slice('cell='.length), 10)
const handle = parseInt(cellString, 10)

if (isNaN(handle)) {
return undefined
Expand All @@ -142,7 +144,7 @@ export function resolveFileNameForDisplay(uri: string) {
const extname = filename.includes('.') ? `.${filename.split('.').pop()}` : ''
const isNotebook = extname.startsWith('.ipynb')
const hash = url.hash ? url.hash.substring(1) : ''
const cell = parseNotebookCellUri(hash)
const cell = parseNotebookCellUriFragment(hash)
if (isNotebook && cell) {
return `${filename} · Cell ${(cell.handle || 0) + 1}`
}
Expand Down

0 comments on commit 70852db

Please sign in to comment.