src/vs/platform/agentHost/common/agentHostUri.ts

163 LOC · 159 covered · 4 uncovered · 25 ranges · 2435 concepts · 14 introducers · 1255 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 > /*--------------------------------------------------------------------------------------------- agentHostUri.ts ×5
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 { decodeBase64, encodeBase64, VSBuffer } from '../../../base/common/buffer.js';
7 > import { Schemas } from '../../../base/common/network.js';
8 > import { URI } from '../../../base/common/uri.js';
9 > import type { ResourceLabelFormatter } from '../../label/common/label.js';
10 >
11 > /**
12 > * The URI scheme for accessing files on a remote agent host.
13 > *
14 > * The original file path is kept verbatim as the URI path so resource
15 > * labels, language detection, and path comparisons see a real path. The
16 > * original scheme, authority, and query are carried in a single
17 > * url-safe-base64 `_ah` query parameter so any remote resource can be
18 > * represented without assuming `file://`:
19 > *
20 > * ```
21 > * vscode-agent-host://[connectionAuthority][originalPath]?_ah=[meta]#[originalFragment]
22 > * ```
23 > *
24 > * where `meta` is {@link IAgentHostUriMeta} as url-safe-base64-encoded
25 > * JSON. Encoding the metadata as a single opaque parameter (rather than
26 > * raw JSON) keeps the query a well-formed parameter list, so unrelated
27 > * query parameters such as `vscodeLinkType` can coexist on the wrapped
28 > * URI without corrupting the metadata. For example,
29 > * `file:///home/user/foo.ts` on remote `my-server` becomes:
30 > * ```
31 > * vscode-agent-host://my-server/home/user/foo.ts?_ah=eyJzY2hlbWUiOiJmaWxlIn0
32 > * ```
33 > */
34 > export const AGENT_HOST_SCHEME = 'vscode-agent-host';
35 >
36 > /**
37 > * Query parameter that carries the {@link IAgentHostUriMeta} payload.
38 > */
39 > const AGENT_HOST_META_PARAM = '_ah';
40 >
41 > /**
42 > * Metadata carried in the query of a {@link AGENT_HOST_SCHEME} URI so the
43 > * original URI can be reconstructed while keeping the path label-friendly.
44 > */
45 > interface IAgentHostUriMeta {
46 > /** Original URI scheme (e.g. `file`, `git-blob`). */
47 > readonly scheme: string;
48 > /** Original URI authority, omitted when empty. */
49 > readonly authority?: string;
50 > /** Original URI query, omitted when empty. */
51 > readonly query?: string;
52 > }
53 >
54 > /**
55 > * Wraps a remote URI into a {@link AGENT_HOST_SCHEME} URI that can be
56 > * resolved through the agent host filesystem provider.
57 > *
58 > * @param originalUri The URI on the remote (e.g. `file:///path` or
59 > * `agenthost-content:///sessionId/...`)
60 > * @param connectionAuthority The sanitized connection identifier used as
61 > * the URI authority (from {@link agentHostAuthority}).
62 > */
63 > export function toAgentHostUri(originalUri: URI, connectionAuthority: string): URI {
64 > if (connectionAuthority === 'local' && originalUri.scheme === Schemas.file) { agentHostUri.ts ×2
65 > return originalUri; agentHostUri.ts ×1
66 > }
68 > const meta: IAgentHostUriMeta = {
69 > scheme: originalUri.scheme,
70 > ...(originalUri.authority ? { authority: originalUri.authority } : {}), agentHostUri.ts ×2
71 > ...(originalUri.query ? { query: originalUri.query } : {}),
72 > };
73 > const params = new URLSearchParams();
74 > params.set(AGENT_HOST_META_PARAM, encodeBase64(VSBuffer.fromString(JSON.stringify(meta)), false, true));
75 > return URI.from({
76 > scheme: AGENT_HOST_SCHEME,
77 > authority: connectionAuthority,
78 > path: originalUri.path || '/',
79 > query: params.toString(),
80 > fragment: originalUri.fragment,
81 > });
82 > }
84 > /**
85 > * Extracts the original URI from a {@link AGENT_HOST_SCHEME} URI.
86 > *
87 > * The inverse of {@link toAgentHostUri}.
88 > */
89 > export function fromAgentHostUri(agentHostUri: URI): URI {
90 > if (agentHostUri.scheme !== AGENT_HOST_SCHEME) { agentHostUri.ts ×4
91 return agentHostUri;
92 }
94 > let meta: Partial<IAgentHostUriMeta> | undefined;
95 > const encoded = agentHostUri.query ? new URLSearchParams(agentHostUri.query).get(AGENT_HOST_META_PARAM) : null;
96 > if (encoded) {
97 > try { agentHostUri.ts ×3
98 > meta = JSON.parse(decodeBase64(encoded).toString()) as Partial<IAgentHostUriMeta>;
99 > } catch {
100 meta = undefined;
101 }
104 > if (!meta || typeof meta.scheme !== 'string') {
105 > // Missing/invalid metadata — fall back to treating the path as a agentHostUri.ts ×1
106 > // file path so callers get a usable URI instead of an exception.
107 > return URI.from({ scheme: Schemas.file, path: agentHostUri.path, fragment: agentHostUri.fragment });
108 > }
110 > return URI.from({
111 > scheme: meta.scheme,
112 > authority: meta.authority || undefined,
113 > path: agentHostUri.path, agentHostUri.ts ×4
114 > query: meta.query || '',
115 > fragment: agentHostUri.fragment,
116 > });
117 > }
119 > /**
120 > * Strips the redundant `ws://` scheme from an address. The transport layer
121 > * already defaults to `ws://`, so only `wss://` needs to be preserved.
122 > */
123 > export function normalizeRemoteAgentHostAddress(address: string): string {
124 > if (address.startsWith('ws://')) { agentHostUri.ts ×2
125 > return address.slice('ws://'.length); agentHostUri.ts ×1
126 > }
127 > return address; agentHostUri.ts ×2
128 > }
130 > /**
131 > * Encode a remote address into an identifier that is safe for use in
132 > * both URI schemes and URI authorities, and is collision-free.
133 > *
134 > * Three tiers:
135 > * 1. Purely alphanumeric addresses are returned as-is.
136 > * 2. "Normal" addresses containing only `[a-zA-Z0-9.:-]` get colons
137 > * replaced with `__` (double underscore) for human readability.
138 > * Addresses containing `_` skip this tier to keep the encoding
139 > * collision-free (`__` can only appear from colon replacement).
140 > * 3. Everything else is url-safe base64-encoded with a `b64-` prefix.
141 > */
142 > export function agentHostAuthority(address: string): string {
143 > const normalized = normalizeRemoteAgentHostAddress(address); agentHostUri.ts ×1
144 > if (/^[a-zA-Z0-9]+$/.test(normalized)) {
145 > return normalized; agentHostUri.ts ×1
146 > }
147 > if (/^[a-zA-Z0-9.:\-]+$/.test(normalized)) { agentHostUri.ts ×1
148 > return normalized.replaceAll(':', '__'); agentHostUri.ts ×1
149 > }
150 > return `b64-${encodeBase64(VSBuffer.fromString(normalized), false, true)}`; agentHostUri.ts ×1
151 > }
153 > /**
154 > * Label formatter for {@link AGENT_HOST_SCHEME} URIs. The URI path is
155 > * already the original resource path, so the label is the path verbatim.
156 > */
157 > export const AGENT_HOST_LABEL_FORMATTER: ResourceLabelFormatter = {
158 > scheme: AGENT_HOST_SCHEME,
159 > formatting: {
160 > label: '${path}',
161 > separator: '/',
162 > },
163 > };