Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/platform/agentHost/common/agentClientUri.ts
13394 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { URI } from '../../../base/common/uri.js';
7
8
/**
9
* The URI scheme for accessing client-side files from the agent host.
10
*
11
* This is the inverse of {@link AGENT_HOST_SCHEME}: the agent host uses
12
* this scheme to address files that live on the connected client.
13
*
14
* ```
15
* vscode-agent-client://[clientId]/[originalScheme]/[originalAuthority]/[originalPath]
16
* ```
17
*
18
* For example, `file:///Users/user/plugins/my-plugin` on client `client-1` becomes:
19
* ```
20
* vscode-agent-client://client-1/file/-/Users/user/plugins/my-plugin
21
* ```
22
*/
23
export const AGENT_CLIENT_SCHEME = 'vscode-agent-client';
24
25
/**
26
* Wraps a client-side URI into a {@link AGENT_CLIENT_SCHEME} URI that
27
* can be resolved through the agent host's client filesystem provider.
28
*
29
* @param originalUri The URI on the client (e.g. `file:///path`)
30
* @param clientId The client identifier (from the protocol `clientId`)
31
*/
32
export function toAgentClientUri(originalUri: URI, clientId: string): URI {
33
const originalAuthority = originalUri.authority || '-';
34
return URI.from({
35
scheme: AGENT_CLIENT_SCHEME,
36
authority: clientId,
37
path: `/${originalUri.scheme}/${originalAuthority}${originalUri.path}`,
38
});
39
}
40
41
/**
42
* Extracts the original client-side URI from a {@link AGENT_CLIENT_SCHEME} URI.
43
*
44
* The inverse of {@link toAgentClientUri}.
45
*/
46
export function fromAgentClientUri(agentClientUri: URI): URI {
47
const path = agentClientUri.path;
48
49
const schemeEnd = path.indexOf('/', 1);
50
if (schemeEnd === -1) {
51
return URI.from({ scheme: 'file', path });
52
}
53
54
const originalScheme = path.substring(1, schemeEnd);
55
56
const authorityEnd = path.indexOf('/', schemeEnd + 1);
57
if (authorityEnd === -1) {
58
const originalAuthority = path.substring(schemeEnd + 1);
59
return URI.from({ scheme: originalScheme, authority: originalAuthority === '-' ? '' : originalAuthority, path: '/' });
60
}
61
62
let originalAuthority = path.substring(schemeEnd + 1, authorityEnd);
63
if (originalAuthority === '-') {
64
originalAuthority = '';
65
}
66
67
const originalPath = path.substring(authorityEnd);
68
69
return URI.from({
70
scheme: originalScheme,
71
authority: originalAuthority || undefined,
72
path: originalPath,
73
});
74
}
75
76