-
Notifications
You must be signed in to change notification settings - Fork 40.1k
Expand file tree
/
Copy pathaiCustomizationTreeViewViews.ts
More file actions
826 lines (716 loc) · 29.8 KB
/
aiCustomizationTreeViewViews.ts
File metadata and controls
826 lines (716 loc) · 29.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import './media/aiCustomizationTreeView.css';
import * as dom from '../../../../base/browser/dom.js';
import { ActionBar } from '../../../../base/browser/ui/actionbar/actionbar.js';
import { CancellationToken } from '../../../../base/common/cancellation.js';
import { DisposableStore } from '../../../../base/common/lifecycle.js';
import { autorun } from '../../../../base/common/observable.js';
import { basename, dirname } from '../../../../base/common/resources.js';
import { ThemeIcon } from '../../../../base/common/themables.js';
import { URI } from '../../../../base/common/uri.js';
import { localize } from '../../../../nls.js';
import { createActionViewItem, getContextMenuActions } from '../../../../platform/actions/browser/menuEntryActionViewItem.js';
import { IMenuService } from '../../../../platform/actions/common/actions.js';
import { IConfigurationService } from '../../../../platform/configuration/common/configuration.js';
import { IContextKey, IContextKeyService, RawContextKey } from '../../../../platform/contextkey/common/contextkey.js';
import { IContextMenuService } from '../../../../platform/contextview/browser/contextView.js';
import { IHoverService } from '../../../../platform/hover/browser/hover.js';
import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';
import { IKeybindingService } from '../../../../platform/keybinding/common/keybinding.js';
import { WorkbenchAsyncDataTree } from '../../../../platform/list/browser/listService.js';
import { IOpenerService } from '../../../../platform/opener/common/opener.js';
import { IThemeService } from '../../../../platform/theme/common/themeService.js';
import { IViewPaneOptions, ViewPane } from '../../../../workbench/browser/parts/views/viewPane.js';
import { IViewDescriptorService } from '../../../../workbench/common/views.js';
import { IPromptsService, PromptsStorage, IAgentSkill, IPromptPath } from '../../../../workbench/contrib/chat/common/promptSyntax/service/promptsService.js';
import { ResourceSet } from '../../../../base/common/map.js';
import { PromptsType } from '../../../../workbench/contrib/chat/common/promptSyntax/promptTypes.js';
import { agentIcon, extensionIcon, instructionsIcon, mcpServerIcon, pluginIcon, promptIcon, skillIcon, userIcon, workspaceIcon, builtinIcon } from '../../../../workbench/contrib/chat/browser/aiCustomization/aiCustomizationIcons.js';
import { AICustomizationItemMenuId } from './aiCustomizationTreeView.js';
import { AICustomizationManagementSection } from '../../../../workbench/contrib/chat/browser/aiCustomization/aiCustomizationManagement.js';
import { AICustomizationPromptsStorage, BUILTIN_STORAGE } from '../../chat/common/builtinPromptsStorage.js';
import { AICustomizationManagementEditorInput } from '../../../../workbench/contrib/chat/browser/aiCustomization/aiCustomizationManagementEditorInput.js';
import { AICustomizationManagementEditor } from '../../../../workbench/contrib/chat/browser/aiCustomization/aiCustomizationManagementEditor.js';
import { IAsyncDataSource, ITreeNode, ITreeRenderer, ITreeContextMenuEvent } from '../../../../base/browser/ui/tree/tree.js';
import { FuzzyScore } from '../../../../base/common/filters.js';
import { IListVirtualDelegate } from '../../../../base/browser/ui/list/list.js';
import { IEditorService } from '../../../../workbench/services/editor/common/editorService.js';
import { ILogService } from '../../../../platform/log/common/log.js';
import { IWorkspaceContextService } from '../../../../platform/workspace/common/workspace.js';
import { IAICustomizationWorkspaceService } from '../../../../workbench/contrib/chat/common/aiCustomizationWorkspaceService.js';
//#region Context Keys
/**
* Context key indicating whether the AI Customization view has no items.
*/
export const AICustomizationIsEmptyContextKey = new RawContextKey<boolean>('aiCustomization.isEmpty', true);
/**
* Context key for the current item's prompt type in context menus.
*/
export const AICustomizationItemTypeContextKey = new RawContextKey<string>('aiCustomizationItemType', '');
/**
* Context key indicating whether the current item is disabled.
*/
export const AICustomizationItemDisabledContextKey = new RawContextKey<boolean>('aiCustomizationItemDisabled', false);
/**
* Context key for the current item's storage type in context menus.
*/
export const AICustomizationItemStorageContextKey = new RawContextKey<string>('aiCustomizationItemStorage', '');
//#endregion
//#region Tree Item Types
/**
* Root element marker for the tree.
*/
const ROOT_ELEMENT = Symbol('root');
type RootElement = typeof ROOT_ELEMENT;
/**
* Represents a type category in the tree (e.g., "Custom Agents", "Skills").
*/
interface IAICustomizationTypeItem {
readonly type: 'category';
readonly id: string;
readonly label: string;
readonly promptType: PromptsType;
readonly icon: ThemeIcon;
}
/**
* Represents a storage group header in the tree (e.g., "Workspace", "User", "Extensions").
*/
interface IAICustomizationGroupItem {
readonly type: 'group';
readonly id: string;
readonly label: string;
readonly storage: AICustomizationPromptsStorage;
readonly promptType: PromptsType;
readonly icon: ThemeIcon;
}
/**
* Represents an individual AI customization item (agent, skill, instruction, or prompt).
*/
interface IAICustomizationFileItem {
readonly type: 'file';
readonly id: string;
readonly uri: URI;
readonly name: string;
readonly description?: string;
readonly storage: AICustomizationPromptsStorage;
readonly promptType: PromptsType;
readonly disabled: boolean;
}
/**
* Represents a link item that navigates to the management editor.
*/
interface IAICustomizationLinkItem {
readonly type: 'link';
readonly id: string;
readonly label: string;
readonly icon: ThemeIcon;
readonly section: AICustomizationManagementSection;
}
type AICustomizationTreeItem = IAICustomizationTypeItem | IAICustomizationGroupItem | IAICustomizationFileItem | IAICustomizationLinkItem;
//#endregion
//#region Tree Infrastructure
class AICustomizationTreeDelegate implements IListVirtualDelegate<AICustomizationTreeItem> {
getHeight(_element: AICustomizationTreeItem): number {
return 22;
}
getTemplateId(element: AICustomizationTreeItem): string {
switch (element.type) {
case 'category':
case 'link':
return 'category';
case 'group':
return 'group';
case 'file':
return 'file';
}
}
}
interface ICategoryTemplateData {
readonly container: HTMLElement;
readonly icon: HTMLElement;
readonly label: HTMLElement;
}
interface IGroupTemplateData {
readonly container: HTMLElement;
readonly label: HTMLElement;
}
interface IFileTemplateData {
readonly container: HTMLElement;
readonly icon: HTMLElement;
readonly name: HTMLElement;
readonly actionBar: ActionBar;
readonly elementDisposables: DisposableStore;
readonly templateDisposables: DisposableStore;
}
class AICustomizationCategoryRenderer implements ITreeRenderer<IAICustomizationTypeItem | IAICustomizationLinkItem, FuzzyScore, ICategoryTemplateData> {
readonly templateId = 'category';
renderTemplate(container: HTMLElement): ICategoryTemplateData {
const element = dom.append(container, dom.$('.ai-customization-category'));
const icon = dom.append(element, dom.$('.icon'));
const label = dom.append(element, dom.$('.label'));
return { container: element, icon, label };
}
renderElement(node: ITreeNode<IAICustomizationTypeItem | IAICustomizationLinkItem, FuzzyScore>, _index: number, templateData: ICategoryTemplateData): void {
templateData.icon.className = 'icon';
templateData.icon.classList.add(...ThemeIcon.asClassNameArray(node.element.icon));
templateData.label.textContent = node.element.label;
}
disposeTemplate(_templateData: ICategoryTemplateData): void { }
}
class AICustomizationGroupRenderer implements ITreeRenderer<IAICustomizationGroupItem, FuzzyScore, IGroupTemplateData> {
readonly templateId = 'group';
renderTemplate(container: HTMLElement): IGroupTemplateData {
const element = dom.append(container, dom.$('.ai-customization-group-header'));
const label = dom.append(element, dom.$('.label'));
return { container: element, label };
}
renderElement(node: ITreeNode<IAICustomizationGroupItem, FuzzyScore>, _index: number, templateData: IGroupTemplateData): void {
templateData.label.textContent = node.element.label;
}
disposeTemplate(_templateData: IGroupTemplateData): void { }
}
class AICustomizationFileRenderer implements ITreeRenderer<IAICustomizationFileItem, FuzzyScore, IFileTemplateData> {
readonly templateId = 'file';
constructor(
private readonly menuService: IMenuService,
private readonly contextKeyService: IContextKeyService,
private readonly instantiationService: IInstantiationService,
) { }
renderTemplate(container: HTMLElement): IFileTemplateData {
const element = dom.append(container, dom.$('.ai-customization-tree-item'));
const icon = dom.append(element, dom.$('.icon'));
const name = dom.append(element, dom.$('.name'));
const actionsContainer = dom.append(element, dom.$('.actions'));
const templateDisposables = new DisposableStore();
const actionBar = templateDisposables.add(new ActionBar(actionsContainer, {
actionViewItemProvider: createActionViewItem.bind(undefined, this.instantiationService),
}));
return { container: element, icon, name, actionBar, elementDisposables: new DisposableStore(), templateDisposables };
}
renderElement(node: ITreeNode<IAICustomizationFileItem, FuzzyScore>, _index: number, templateData: IFileTemplateData): void {
const item = node.element;
templateData.elementDisposables.clear();
// Set icon based on prompt type
let icon: ThemeIcon;
switch (item.promptType) {
case PromptsType.agent:
icon = agentIcon;
break;
case PromptsType.skill:
icon = skillIcon;
break;
case PromptsType.instructions:
icon = instructionsIcon;
break;
case PromptsType.prompt:
default:
icon = promptIcon;
break;
}
templateData.icon.className = 'icon';
templateData.icon.classList.add(...ThemeIcon.asClassNameArray(icon));
templateData.name.textContent = item.name;
// Apply disabled styling
templateData.container.classList.toggle('disabled', item.disabled);
// Set tooltip with name and description
const tooltip = item.description ? `${item.name} - ${item.description}` : item.name;
templateData.container.title = tooltip;
// Build context for menu actions
const context = {
uri: item.uri.toString(),
name: item.name,
promptType: item.promptType,
storage: item.storage,
};
// Create scoped context key service with item type for when-clause filtering
const overlay = this.contextKeyService.createOverlay([
[AICustomizationItemTypeContextKey.key, item.promptType],
[AICustomizationItemDisabledContextKey.key, item.disabled],
[AICustomizationItemStorageContextKey.key, item.storage],
]);
// Create menu and extract inline actions
const menu = templateData.elementDisposables.add(
this.menuService.createMenu(AICustomizationItemMenuId, overlay)
);
const updateActions = () => {
const actions = menu.getActions({ arg: context, shouldForwardArgs: true });
const { primary } = getContextMenuActions(actions, 'inline');
templateData.actionBar.clear();
templateData.actionBar.push(primary, { icon: true, label: false });
};
updateActions();
templateData.elementDisposables.add(menu.onDidChange(updateActions));
templateData.actionBar.context = context;
}
disposeElement(_node: ITreeNode<IAICustomizationFileItem, FuzzyScore>, _index: number, templateData: IFileTemplateData): void {
templateData.elementDisposables.clear();
}
disposeTemplate(templateData: IFileTemplateData): void {
templateData.templateDisposables.dispose();
templateData.elementDisposables.dispose();
}
}
/**
* Cached data for a specific prompt type.
*/
interface ICachedTypeData {
skills?: IAgentSkill[];
files?: Map<string, readonly IPromptPath[]>;
}
/**
* Data source for the AI Customization tree with efficient caching.
* Caches data per-type to avoid redundant fetches when expanding groups.
*/
class UnifiedAICustomizationDataSource implements IAsyncDataSource<RootElement, AICustomizationTreeItem> {
private cache = new Map<PromptsType, ICachedTypeData>();
private totalItemCount = 0;
constructor(
private readonly promptsService: IPromptsService,
private readonly logService: ILogService,
private readonly onItemCountChanged: (count: number) => void,
) { }
/**
* Clears the cache. Should be called when the view refreshes.
*/
clearCache(): void {
this.cache.clear();
this.totalItemCount = 0;
}
hasChildren(element: RootElement | AICustomizationTreeItem): boolean {
if (element === ROOT_ELEMENT) {
return true;
}
if (element.type === 'link') {
return false;
}
return element.type === 'category' || element.type === 'group';
}
async getChildren(element: RootElement | AICustomizationTreeItem): Promise<AICustomizationTreeItem[]> {
try {
if (element === ROOT_ELEMENT) {
return this.getTypeCategories();
}
if (element.type === 'category') {
return this.getStorageGroups(element.promptType);
}
if (element.type === 'group') {
return this.getFilesForStorageAndType(element.storage, element.promptType);
}
return [];
} catch (error) {
this.logService.error('[AICustomization] Error fetching tree children:', error);
return [];
}
}
private getTypeCategories(): (IAICustomizationTypeItem | IAICustomizationLinkItem)[] {
return [
{
type: 'category',
id: 'category-agents',
label: localize('customAgents', "Custom Agents"),
promptType: PromptsType.agent,
icon: agentIcon,
},
{
type: 'category',
id: 'category-skills',
label: localize('skills', "Skills"),
promptType: PromptsType.skill,
icon: skillIcon,
},
{
type: 'category',
id: 'category-instructions',
label: localize('instructions', "Instructions"),
promptType: PromptsType.instructions,
icon: instructionsIcon,
},
{
type: 'category',
id: 'category-prompts',
label: localize('prompts', "Prompts"),
promptType: PromptsType.prompt,
icon: promptIcon,
},
{
type: 'link',
id: 'link-mcp-servers',
label: localize('mcpServers', "MCP Servers"),
icon: mcpServerIcon,
section: AICustomizationManagementSection.McpServers,
},
];
}
/**
* Fetches and caches data for a prompt type, returning storage groups with items.
*/
private async getStorageGroups(promptType: PromptsType): Promise<IAICustomizationGroupItem[]> {
const groups: IAICustomizationGroupItem[] = [];
// Check cache first
let cached = this.cache.get(promptType);
if (!cached) {
cached = {};
this.cache.set(promptType, cached);
}
// For skills, use findAgentSkills which has the proper names from frontmatter
if (promptType === PromptsType.skill) {
if (!cached.skills) {
const skills = await this.promptsService.findAgentSkills(CancellationToken.None);
cached.skills = skills || [];
this.totalItemCount += cached.skills.length;
this.onItemCountChanged(this.totalItemCount);
}
const workspaceSkills = cached.skills.filter(s => s.storage === PromptsStorage.local);
const userSkills = cached.skills.filter(s => s.storage === PromptsStorage.user);
const extensionSkills = cached.skills.filter(s => s.storage === PromptsStorage.extension);
const builtinSkills = cached.skills.filter(s => s.storage === BUILTIN_STORAGE);
if (workspaceSkills.length > 0) {
groups.push(this.createGroupItem(promptType, PromptsStorage.local, workspaceSkills.length));
}
if (userSkills.length > 0) {
groups.push(this.createGroupItem(promptType, PromptsStorage.user, userSkills.length));
}
if (extensionSkills.length > 0) {
groups.push(this.createGroupItem(promptType, PromptsStorage.extension, extensionSkills.length));
}
if (builtinSkills.length > 0) {
groups.push(this.createGroupItem(promptType, BUILTIN_STORAGE, builtinSkills.length));
}
return groups;
}
// For other types, fetch once and cache grouped by storage
if (!cached.files) {
const allItems: IPromptPath[] = [...await this.promptsService.listPromptFiles(promptType, CancellationToken.None)];
// For instructions, also include agent instructions (AGENTS.md, copilot-instructions.md, CLAUDE.md, etc.)
if (promptType === PromptsType.instructions) {
const existingUris = new ResourceSet(allItems.map(item => item.uri));
const agentInstructions = await this.promptsService.listAgentInstructions(CancellationToken.None);
for (const file of agentInstructions) {
if (!existingUris.has(file.uri)) {
allItems.push({ uri: file.uri, storage: PromptsStorage.local, type: PromptsType.instructions });
}
}
}
const workspaceItems = allItems.filter(item => item.storage === PromptsStorage.local);
const userItems = allItems.filter(item => item.storage === PromptsStorage.user);
const extensionItems = allItems.filter(item => item.storage === PromptsStorage.extension);
const builtinItems = allItems.filter(item => item.storage === BUILTIN_STORAGE);
cached.files = new Map<string, readonly IPromptPath[]>([
[PromptsStorage.local, workspaceItems],
[PromptsStorage.user, userItems],
[PromptsStorage.extension, extensionItems],
[BUILTIN_STORAGE, builtinItems],
]);
const itemCount = allItems.length;
this.totalItemCount += itemCount;
this.onItemCountChanged(this.totalItemCount);
}
const workspaceItems = cached.files!.get(PromptsStorage.local) || [];
const userItems = cached.files!.get(PromptsStorage.user) || [];
const extensionItems = cached.files!.get(PromptsStorage.extension) || [];
const builtinItems = cached.files!.get(BUILTIN_STORAGE) || [];
if (workspaceItems.length > 0) {
groups.push(this.createGroupItem(promptType, PromptsStorage.local, workspaceItems.length));
}
if (userItems.length > 0) {
groups.push(this.createGroupItem(promptType, PromptsStorage.user, userItems.length));
}
if (extensionItems.length > 0) {
groups.push(this.createGroupItem(promptType, PromptsStorage.extension, extensionItems.length));
}
if (builtinItems.length > 0) {
groups.push(this.createGroupItem(promptType, BUILTIN_STORAGE, builtinItems.length));
}
return groups;
}
/**
* Creates a group item with consistent structure.
*/
private createGroupItem(promptType: PromptsType, storage: AICustomizationPromptsStorage, count: number): IAICustomizationGroupItem {
const storageLabels: Record<string, string> = {
[PromptsStorage.local]: localize('workspaceWithCount', "Workspace ({0})", count),
[PromptsStorage.user]: localize('userWithCount', "User ({0})", count),
[PromptsStorage.extension]: localize('extensionsWithCount', "Extensions ({0})", count),
[PromptsStorage.plugin]: localize('pluginsWithCount', "Plugins ({0})", count),
[BUILTIN_STORAGE]: localize('builtinWithCount', "Built-in ({0})", count),
};
const storageIcons: Record<string, ThemeIcon> = {
[PromptsStorage.local]: workspaceIcon,
[PromptsStorage.user]: userIcon,
[PromptsStorage.extension]: extensionIcon,
[PromptsStorage.plugin]: pluginIcon,
[BUILTIN_STORAGE]: builtinIcon,
};
const storageSuffixes: Record<string, string> = {
[PromptsStorage.local]: 'workspace',
[PromptsStorage.user]: 'user',
[PromptsStorage.extension]: 'extensions',
[PromptsStorage.plugin]: 'plugins',
[BUILTIN_STORAGE]: 'builtin',
};
return {
type: 'group',
id: `group-${promptType}-${storageSuffixes[storage]}`,
label: storageLabels[storage],
storage,
promptType,
icon: storageIcons[storage],
};
}
/**
* Returns files for a specific storage/type combination from cache.
* getStorageGroups must be called first to populate the cache.
*/
private async getFilesForStorageAndType(storage: AICustomizationPromptsStorage, promptType: PromptsType): Promise<IAICustomizationFileItem[]> {
const cached = this.cache.get(promptType);
const disabledUris = this.promptsService.getDisabledPromptFiles(promptType);
// For skills, use the cached skills data and merge in disabled skills
if (promptType === PromptsType.skill) {
const skills = cached?.skills || [];
const filtered = skills.filter(skill => skill.storage === storage);
const seenUris = new Set<string>();
const result: IAICustomizationFileItem[] = filtered
.map(skill => {
seenUris.add(skill.uri.toString());
// Use skill name from frontmatter, or fallback to parent folder name
const skillName = skill.name || basename(dirname(skill.uri)) || basename(skill.uri);
return {
type: 'file' as const,
id: skill.uri.toString(),
uri: skill.uri,
name: skillName,
description: skill.description,
storage: skill.storage,
promptType,
disabled: disabledUris.has(skill.uri),
};
});
// Include disabled skills not already in the enabled list
if (disabledUris.size > 0) {
const allSkillFiles = await this.promptsService.listPromptFiles(PromptsType.skill, CancellationToken.None);
for (const file of allSkillFiles) {
if (file.storage === storage && !seenUris.has(file.uri.toString()) && disabledUris.has(file.uri)) {
result.push({
type: 'file' as const,
id: file.uri.toString(),
uri: file.uri,
name: file.name || basename(dirname(file.uri)) || basename(file.uri),
description: file.description,
storage: file.storage,
promptType,
disabled: true,
});
}
}
}
return result;
}
// Use cached files data (already fetched in getStorageGroups)
const items = [...(cached?.files?.get(storage) || [])];
return items.map(item => ({
type: 'file' as const,
id: item.uri.toString(),
uri: item.uri,
name: item.name || basename(item.uri),
description: item.description,
storage: item.storage,
promptType,
disabled: disabledUris.has(item.uri),
}));
}
}
//#endregion
//#region Unified View Pane
/**
* Unified view pane for all AI Customization items (agents, skills, instructions, prompts).
*/
export class AICustomizationViewPane extends ViewPane {
static readonly ID = 'aiCustomization.view';
private tree: WorkbenchAsyncDataTree<RootElement, AICustomizationTreeItem, FuzzyScore> | undefined;
private dataSource: UnifiedAICustomizationDataSource | undefined;
private treeContainer: HTMLElement | undefined;
private readonly treeDisposables = this._register(new DisposableStore());
// Context keys for controlling menu visibility and welcome content
private readonly isEmptyContextKey: IContextKey<boolean>;
private readonly itemTypeContextKey: IContextKey<string>;
private readonly itemDisabledContextKey: IContextKey<boolean>;
private readonly itemStorageContextKey: IContextKey<string>;
constructor(
options: IViewPaneOptions,
@IKeybindingService keybindingService: IKeybindingService,
@IContextMenuService contextMenuService: IContextMenuService,
@IConfigurationService configurationService: IConfigurationService,
@IContextKeyService contextKeyService: IContextKeyService,
@IViewDescriptorService viewDescriptorService: IViewDescriptorService,
@IInstantiationService instantiationService: IInstantiationService,
@IOpenerService openerService: IOpenerService,
@IThemeService themeService: IThemeService,
@IHoverService hoverService: IHoverService,
@IPromptsService private readonly promptsService: IPromptsService,
@IEditorService private readonly editorService: IEditorService,
@IMenuService private readonly menuService: IMenuService,
@ILogService private readonly logService: ILogService,
@IWorkspaceContextService private readonly workspaceContextService: IWorkspaceContextService,
@IAICustomizationWorkspaceService private readonly workspaceService: IAICustomizationWorkspaceService,
) {
super(options, keybindingService, contextMenuService, configurationService, contextKeyService, viewDescriptorService, instantiationService, openerService, themeService, hoverService);
// Initialize context keys
this.isEmptyContextKey = AICustomizationIsEmptyContextKey.bindTo(contextKeyService);
this.itemTypeContextKey = AICustomizationItemTypeContextKey.bindTo(contextKeyService);
this.itemDisabledContextKey = AICustomizationItemDisabledContextKey.bindTo(contextKeyService);
this.itemStorageContextKey = AICustomizationItemStorageContextKey.bindTo(contextKeyService);
// Subscribe to prompt service events to refresh tree
this._register(this.promptsService.onDidChangeCustomAgents(() => this.refresh()));
this._register(this.promptsService.onDidChangeSlashCommands(() => this.refresh()));
// Listen to workspace folder changes to refresh tree
this._register(this.workspaceContextService.onDidChangeWorkspaceFolders(() => this.refresh()));
this._register(autorun(reader => {
this.workspaceService.activeProjectRoot.read(reader);
this.refresh();
}));
}
protected override renderBody(container: HTMLElement): void {
super.renderBody(container);
container.classList.add('ai-customization-view');
this.treeContainer = dom.append(container, dom.$('.tree-container'));
this.createTree();
}
private createTree(): void {
if (!this.treeContainer) {
return;
}
// Create data source with callback for tracking item count
this.dataSource = new UnifiedAICustomizationDataSource(
this.promptsService,
this.logService,
(count) => this.isEmptyContextKey.set(count === 0),
);
this.tree = this.treeDisposables.add(this.instantiationService.createInstance(
WorkbenchAsyncDataTree<RootElement, AICustomizationTreeItem, FuzzyScore>,
'AICustomization',
this.treeContainer,
new AICustomizationTreeDelegate(),
[
new AICustomizationCategoryRenderer(),
new AICustomizationGroupRenderer(),
new AICustomizationFileRenderer(this.menuService, this.contextKeyService, this.instantiationService),
],
this.dataSource,
{
identityProvider: {
getId: (element: AICustomizationTreeItem) => element.id,
},
accessibilityProvider: {
getAriaLabel: (element: AICustomizationTreeItem) => {
if (element.type === 'category' || element.type === 'link') {
return element.label;
}
if (element.type === 'group') {
return element.label;
}
// For files, include description and disabled state
const nameAndDesc = element.description
? localize('fileAriaLabel', "{0}, {1}", element.name, element.description)
: element.name;
return element.disabled
? localize('fileAriaLabelDisabled', "{0}, disabled", nameAndDesc)
: nameAndDesc;
},
getWidgetAriaLabel: () => localize('aiCustomizationTree', "Chat Customization Items"),
},
keyboardNavigationLabelProvider: {
getKeyboardNavigationLabel: (element: AICustomizationTreeItem) => {
if (element.type === 'file') {
return element.name;
}
return element.label;
},
},
}
));
// Handle double-click to open file or navigate to section
this.treeDisposables.add(this.tree.onDidOpen(async e => {
if (e.element && e.element.type === 'file') {
this.editorService.openEditor({
resource: e.element.uri,
});
} else if (e.element && e.element.type === 'link') {
const input = AICustomizationManagementEditorInput.getOrCreate();
const editor = await this.editorService.openEditor(input, { pinned: true });
if (editor instanceof AICustomizationManagementEditor) {
editor.selectSectionById(e.element.section);
}
}
}));
// Handle context menu
this.treeDisposables.add(this.tree.onContextMenu(e => this.onContextMenu(e)));
// Initial load and auto-expand category nodes
void this.tree.setInput(ROOT_ELEMENT).then(() => this.autoExpandCategories());
}
private async autoExpandCategories(): Promise<void> {
if (!this.tree) {
return;
}
// Auto-expand all category nodes to show storage groups
const rootNode = this.tree.getNode(ROOT_ELEMENT);
for (const child of rootNode.children) {
if (child.element !== ROOT_ELEMENT) {
await this.tree.expand(child.element);
}
}
}
protected override layoutBody(height: number, width: number): void {
super.layoutBody(height, width);
this.tree?.layout(height, width);
}
public refresh(): void {
// Clear the cache before refreshing
this.dataSource?.clearCache();
this.isEmptyContextKey.set(true); // Reset until we know the count
void this.tree?.setInput(ROOT_ELEMENT).then(() => this.autoExpandCategories());
}
public collapseAll(): void {
this.tree?.collapseAll();
}
public expandAll(): void {
this.tree?.expandAll();
}
private onContextMenu(e: ITreeContextMenuEvent<AICustomizationTreeItem | null>): void {
// Only show context menu for file items
if (!e.element || e.element.type !== 'file') {
return;
}
const element = e.element;
// Set context keys for the item so menu items can use `when` clauses
this.itemTypeContextKey.set(element.promptType);
this.itemDisabledContextKey.set(element.disabled);
this.itemStorageContextKey.set(element.storage);
// Get menu actions from the menu service
const context = {
uri: element.uri.toString(),
name: element.name,
promptType: element.promptType,
disabled: element.disabled,
};
const menu = this.menuService.getMenuActions(AICustomizationItemMenuId, this.contextKeyService, { arg: context, shouldForwardArgs: true });
const { secondary } = getContextMenuActions(menu, 'inline');
// Show the context menu
if (secondary.length > 0) {
this.contextMenuService.showContextMenu({
getAnchor: () => e.anchor,
getActions: () => secondary,
getActionsContext: () => context,
onHide: () => {
// Clear the context keys when menu closes
this.itemTypeContextKey.reset();
this.itemDisabledContextKey.reset();
this.itemStorageContextKey.reset();
},
});
}
}
}
//#endregion