Path: blob/main/src/vs/platform/agentHost/common/agentClientUri.ts
13394 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { URI } from '../../../base/common/uri.js';67/**8* The URI scheme for accessing client-side files from the agent host.9*10* This is the inverse of {@link AGENT_HOST_SCHEME}: the agent host uses11* this scheme to address files that live on the connected client.12*13* ```14* vscode-agent-client://[clientId]/[originalScheme]/[originalAuthority]/[originalPath]15* ```16*17* For example, `file:///Users/user/plugins/my-plugin` on client `client-1` becomes:18* ```19* vscode-agent-client://client-1/file/-/Users/user/plugins/my-plugin20* ```21*/22export const AGENT_CLIENT_SCHEME = 'vscode-agent-client';2324/**25* Wraps a client-side URI into a {@link AGENT_CLIENT_SCHEME} URI that26* can be resolved through the agent host's client filesystem provider.27*28* @param originalUri The URI on the client (e.g. `file:///path`)29* @param clientId The client identifier (from the protocol `clientId`)30*/31export function toAgentClientUri(originalUri: URI, clientId: string): URI {32const originalAuthority = originalUri.authority || '-';33return URI.from({34scheme: AGENT_CLIENT_SCHEME,35authority: clientId,36path: `/${originalUri.scheme}/${originalAuthority}${originalUri.path}`,37});38}3940/**41* Extracts the original client-side URI from a {@link AGENT_CLIENT_SCHEME} URI.42*43* The inverse of {@link toAgentClientUri}.44*/45export function fromAgentClientUri(agentClientUri: URI): URI {46const path = agentClientUri.path;4748const schemeEnd = path.indexOf('/', 1);49if (schemeEnd === -1) {50return URI.from({ scheme: 'file', path });51}5253const originalScheme = path.substring(1, schemeEnd);5455const authorityEnd = path.indexOf('/', schemeEnd + 1);56if (authorityEnd === -1) {57const originalAuthority = path.substring(schemeEnd + 1);58return URI.from({ scheme: originalScheme, authority: originalAuthority === '-' ? '' : originalAuthority, path: '/' });59}6061let originalAuthority = path.substring(schemeEnd + 1, authorityEnd);62if (originalAuthority === '-') {63originalAuthority = '';64}6566const originalPath = path.substring(authorityEnd);6768return URI.from({69scheme: originalScheme,70authority: originalAuthority || undefined,71path: originalPath,72});73}747576