src/vs/platform/agentHost/node/copilot/copilotSlashCommandProvider.ts

138 LOC · 96 covered · 42 uncovered · 28 ranges · 869 concepts · 6 introducers · 409 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 > /*--------------------------------------------------------------------------------------------- copilotAgentSession.ts ×141
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 type { CopilotClient } from '@github/copilot-sdk';
7 > import { ILogService } from '../../../log/common/log.js';
8 > import { raceTimeout } from '../../../../base/common/async.js';
9 >
10 > type RuntimeSlashCommandCatalog = {
11 > readonly commands: readonly RuntimeSlashCommandInfo[];
12 > readonly byName: ReadonlyMap<string, RuntimeSlashCommandInfo>;
13 > readonly byAlias: ReadonlyMap<string, RuntimeSlashCommandInfo>;
14 > };
15 >
16 > type RuntimeSlashCommandCache = {
17 > value?: RuntimeSlashCommandCatalog;
18 > inFlight?: Promise<RuntimeSlashCommandCatalog>;
19 > };
20 >
21 > type RuntimeSlashCommandInfo = Awaited<ReturnType<CopilotClient['rpc']['commands']['list']>>['commands'][number];
22 >
23 > export class CopilotSlashCommandProvider {
24 > private _runtimeSlashCommandCache: RuntimeSlashCommandCache | undefined;
25 > constructor(
26 > private readonly listCommands: () => Promise<RuntimeSlashCommandInfo[]>, copilotSlashCommandProvider.ts ×1
27 > @ILogService private readonly _logService: ILogService,
28 > ) { }
30 > async getSlashCommands(options?: { readonly maxWaitMs?: number }): Promise<readonly RuntimeSlashCommandInfo[]> {
31 try {
32 const maxWaitMs = options?.maxWaitMs;
33 const catalog = await this._getRuntimeSlashCommandCatalog(maxWaitMs === undefined ? undefined : Math.max(0, maxWaitMs));
34 return catalog.commands;
35 } catch (err) {
36 this._logService.warn(`[Copilot] rpc.commands.list failed`, err);
37 return [];
38 }
39 }
41 > public async resolveSlashCommand(command: string, maxWaitMs: number | undefined = undefined): Promise<RuntimeSlashCommandInfo | undefined> {
42 > const key = this._normalizeSlashCommandKey(command); copilotSlashCommandProvider.ts ×12
43 > if (!key) {
44 return undefined;
45 }
46 > const catalog = await this._getRuntimeSlashCommandCatalog(maxWaitMs); copilotSlashCommandProvider.ts ×12
47 > return catalog.byName.get(key) ?? catalog.byAlias.get(key);
48 > }
50 > public clearCache(): void {
51 if (this._runtimeSlashCommandCache) {
52 // Keep in-flight promises isolated from fresh lookups after invalidation.
53 this._runtimeSlashCommandCache = undefined;
54 }
55 }
57 > private async _getRuntimeSlashCommandCatalog(maxWaitMs: number | undefined = undefined): Promise<RuntimeSlashCommandCatalog> {
58 > const cache = this._runtimeSlashCommandCache ??= {}; copilotSlashCommandProvider.ts ×12
59 > if (cache.value) {
60 > return cache.value; copilotAgentSession.ts ×2
61 > }
63 > const inFlight = this._refreshRuntimeSlashCommandCatalog(cache);
64 > if (maxWaitMs === undefined) {
65 > return inFlight;
66 > }
67 const settled = await raceTimeout(inFlight, maxWaitMs);
68 if (settled) {
69 return settled;
70 }
71 if (cache.value) {
72 return cache.value;
73 }
74 return {
75 commands: [],
76 byName: new Map(),
77 byAlias: new Map(),
78 };
81 > private async _refreshRuntimeSlashCommandCatalog(cache: RuntimeSlashCommandCache): Promise<RuntimeSlashCommandCatalog> {
82 > if (cache.inFlight) { copilotSlashCommandProvider.ts ×12
83 return cache.inFlight;
84 }
85 > const inFlight = this.listCommands() copilotSlashCommandProvider.ts ×12
86 > .then(result => this._toRuntimeSlashCommandCatalog(result));
87 > cache.inFlight = inFlight;
88 > inFlight.then(catalog => {
89 > if (this._runtimeSlashCommandCache === cache) {
90 > cache.value = catalog;
91 > cache.inFlight = undefined;
92 > }
93 > }, () => {
94 if (this._runtimeSlashCommandCache === cache) {
95 cache.inFlight = undefined;
96 if (!cache.value) {
97 this._runtimeSlashCommandCache = undefined;
98 }
99 }
101 > return inFlight;
102 > }
104 > private _toRuntimeSlashCommandCatalog(commands: readonly RuntimeSlashCommandInfo[]): RuntimeSlashCommandCatalog {
105 > const byName = new Map<string, RuntimeSlashCommandInfo>(); copilotSlashCommandProvider.ts ×12
106 > const byAlias = new Map<string, RuntimeSlashCommandInfo>();
107 > const deduped: RuntimeSlashCommandInfo[] = [];
108 > for (const command of commands) {
109 > const nameKey = this._normalizeSlashCommandKey(command.name); copilotSlashCommandProvider.ts ×3
110 > if (!nameKey) {
111 continue;
112 }
113 > let canonical = byName.get(nameKey); copilotSlashCommandProvider.ts ×3
114 > if (!canonical) {
115 > canonical = command;
116 > byName.set(nameKey, canonical);
117 > deduped.push(canonical);
118 > }
119 > for (const alias of command.aliases ?? []) {
120 > const aliasKey = this._normalizeSlashCommandKey(alias); copilotSlashCommandProvider.ts ×2
121 > if (!aliasKey || byAlias.has(aliasKey)) {
122 continue;
123 }
124 > byAlias.set(aliasKey, canonical); copilotSlashCommandProvider.ts ×2
125 > }
127 > return { commands: deduped, byName, byAlias }; copilotSlashCommandProvider.ts ×12
128 > }
130 > private _normalizeSlashCommandKey(command: string): string | undefined {
131 > const trimmed = command.trim(); copilotSlashCommandProvider.ts ×12
132 > if (!trimmed) {
133 return undefined;
134 }
135 > const slashStripped = trimmed.charCodeAt(0) === 0x2f /* / */ ? trimmed.slice(1) : trimmed; copilotSlashCommandProvider.ts ×12
136 > return slashStripped.toLowerCase();
137 > }