src/vs/platform/agentHost/node/shared/sessionPluginBundler.ts
184 LOC · 166 covered · 18 uncovered · 29 ranges · 54 concepts · 9 introducers · 34 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.
/*---------------------------------------------------------------------------------------------
sessionPluginBundler.ts ×6
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { VSBuffer } from '../../../../base/common/buffer.js';
import { CancellationToken } from '../../../../base/common/cancellation.js';
import { hash } from '../../../../base/common/hash.js';
import { Disposable } from '../../../../base/common/lifecycle.js';
import { basename, dirname } from '../../../../base/common/resources.js';
import { URI } from '../../../../base/common/uri.js';
import { IFileService } from '../../../files/common/files.js';
import { IAgentPluginManager } from '../../common/agentPluginManager.js';
import { customizationId, type ClientPluginCustomization } from '../../common/state/sessionState.js';
import { CustomizationType, type URI as ProtocolURI } from '../../common/state/protocol/state.js';
import { DiscoveredType, type IDiscoveredDirectory } from '../copilot/sessionCustomizationDiscovery.js';
const DISPLAY_NAME = 'VS Code Synced Data';
const HOST_DISCOVERY_DIR = 'host-discovery';
const MANIFEST_CONTENT = JSON.stringify({
name: DISPLAY_NAME,
description: 'Customization data discovered from this workspace and your home directory',
}, null, '\t');
/**
* Maps a {@link DiscoveredType} to the plugin sub-directory under which that
* component type lives in the Open Plugin format.
*/
function pluginDirForType(type: DiscoveredType): string | undefined {
sessionPluginBundler.ts ×10
switch (type) {
case DiscoveredType.Agent: return 'agents';
case DiscoveredType.Skill: return 'skills';
case DiscoveredType.Instruction: return 'rules';
case DiscoveredType.Hook: return 'hooks';
case DiscoveredType.AgentInstruction: return undefined;
}
}
interface IBundleResult {
readonly ref: ClientPluginCustomization;
}
/**
* Bundles host-discovered customization files into an Open Plugin layout
* on real disk under `<agentPluginManager.basePath>/host-discovery/<hash>/`.
*
* Writing to a real directory (rather than an in-memory provider) is
* required because the Copilot SDK subprocess receives skill directories
* and hook commands as on-disk paths via `fsPath`, and because the
* workbench fetches files through the agent-host filesystem bridge —
* neither of which can read a host-side in-memory FS.
*
* The directory is namespaced by a hash of the working directory so
* concurrent sessions on different folders don't collide. Repeated
* `bundle()` calls with identical content reuse the prior bundle (nonce
* match) and skip the rewrite.
*/
export class SessionPluginBundler extends Disposable {
private readonly _rootUri: URI;
private _lastNonce: string | undefined;
constructor(
@IFileService private readonly _fileService: IFileService,
@IAgentPluginManager pluginManager: IAgentPluginManager,
) {
super();
const authority = `host-${hash(workingDirectory.toString())}`;
this._rootUri = URI.joinPath(pluginManager.basePath, HOST_DISCOVERY_DIR, authority);
}
get rootUri(): URI {
}
get lastNonce(): string | undefined {
return this._lastNonce;
}
/**
* Bundles the given files into the on-disk plugin directory.
*
* Overwrites any previous bundle for this working directory. Returns a
* {@link ClientPluginCustomization} pointing at the on-disk plugin root
* with a content-based nonce, or `undefined` when there are no files or
* cancellation was requested.
*/
async bundle(directories: readonly IDiscoveredDirectory[], token: CancellationToken = CancellationToken.None): Promise<IBundleResult | undefined> {
}
const hashParts: string[] = [];
const files: { readonly destUri: URI; readonly content: VSBuffer }[] = [];
for (const discoveredDirectory of directories) {
const dir = pluginDirForType(discoveredDirectory.type);
if (!dir) {
continue; // do not bundle agent instructions
}
const fileName = basename(fileUri);
let destUri: URI;
let hashKey: string;
if (discoveredDirectory.type === DiscoveredType.Skill) {
// containing directory name so multiple skills don't collide.
const skillDirName = basename(dirname(fileUri));
destUri = URI.joinPath(this._rootUri, dir, skillDirName, fileName);
hashKey = `${dir}/${skillDirName}/${fileName}`;
destUri = URI.joinPath(this._rootUri, dir, fileName);
hashKey = `${dir}/${fileName}`;
}
const content = await this._fileService.readFile(fileUri);
if (token.isCancellationRequested) {
return undefined;
}
hashParts.push(`${hashKey}:${content.value.toString()}`);
}
if (token.isCancellationRequested) {
return undefined;
}
hashParts.sort();
const nonce = String(hash(hashParts.join('\n')));
const rootUriString = this._rootUri.toString() as ProtocolURI;
const result = {
ref: {
type: CustomizationType.Plugin,
id: customizationId(rootUriString),
uri: rootUriString,
name: DISPLAY_NAME,
enabled: true,
nonce,
},
} satisfies IBundleResult;
if (this._lastNonce === nonce) {
}
try {
await this._fileService.del(this._rootUri, { recursive: true });
} catch {
// Directory may not exist on first bundle.
}
if (token.isCancellationRequested) {
return undefined;
}
const manifestUri = URI.joinPath(this._rootUri, '.plugin', 'plugin.json');
await this._fileService.createFolder(dirname(manifestUri));
if (token.isCancellationRequested) {
return undefined;
}
await this._fileService.writeFile(manifestUri, VSBuffer.fromString(MANIFEST_CONTENT));
sessionPluginBundler.ts ×10
if (token.isCancellationRequested) {
return undefined;
}
for (const file of files) {
if (token.isCancellationRequested) {
return undefined;
}
if (token.isCancellationRequested) {
return undefined;
}
this._lastNonce = nonce;
return result;