Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/chat/browser/createSlashCommandsUsageTracker.ts
13401 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 { Disposable } from '../../../../base/common/lifecycle.js';
7
import { IContextKeyService } from '../../../../platform/contextkey/common/contextkey.js';
8
import { IStorageService, StorageScope, StorageTarget } from '../../../../platform/storage/common/storage.js';
9
import { IChatService } from '../common/chatService/chatService.js';
10
import { ChatContextKeys } from '../common/actions/chatContextKeys.js';
11
import { ChatRequestSlashCommandPart } from '../common/requestParser/chatParserTypes.js';
12
13
export class CreateSlashCommandsUsageTracker extends Disposable {
14
private static readonly _USED_CREATE_SLASH_COMMANDS_KEY = 'chat.tips.usedCreateSlashCommands';
15
16
constructor(
17
private readonly _chatService: IChatService,
18
private readonly _storageService: IStorageService,
19
private readonly _getActiveContextKeyService: () => IContextKeyService | undefined,
20
) {
21
super();
22
23
this._register(this._chatService.onDidSubmitRequest(e => {
24
const message = e.message ?? this._chatService.getSession(e.chatSessionResource)?.lastRequest?.message;
25
if (!message) {
26
return;
27
}
28
29
for (const part of message.parts) {
30
if (part.kind === ChatRequestSlashCommandPart.Kind) {
31
const slash = part as ChatRequestSlashCommandPart;
32
if (CreateSlashCommandsUsageTracker._isCreateSlashCommand(slash.slashCommand.command)) {
33
this._markUsed();
34
return;
35
}
36
}
37
}
38
39
// Fallback when parsing doesn't produce a slash command part.
40
const trimmed = message.text.trimStart();
41
const match = /^\/(create-(?:instructions|prompt|agent|skill))(?:\s|$)/.exec(trimmed);
42
if (match && CreateSlashCommandsUsageTracker._isCreateSlashCommand(match[1])) {
43
this._markUsed();
44
}
45
}));
46
}
47
48
syncContextKey(contextKeyService: IContextKeyService): void {
49
const used = this._storageService.getBoolean(CreateSlashCommandsUsageTracker._USED_CREATE_SLASH_COMMANDS_KEY, StorageScope.APPLICATION, false);
50
ChatContextKeys.hasUsedCreateSlashCommands.bindTo(contextKeyService).set(used);
51
}
52
53
private _markUsed(): void {
54
if (this._storageService.getBoolean(CreateSlashCommandsUsageTracker._USED_CREATE_SLASH_COMMANDS_KEY, StorageScope.APPLICATION, false)) {
55
return;
56
}
57
58
this._storageService.store(CreateSlashCommandsUsageTracker._USED_CREATE_SLASH_COMMANDS_KEY, true, StorageScope.APPLICATION, StorageTarget.MACHINE);
59
60
const contextKeyService = this._getActiveContextKeyService();
61
if (contextKeyService) {
62
ChatContextKeys.hasUsedCreateSlashCommands.bindTo(contextKeyService).set(true);
63
}
64
}
65
66
private static _isCreateSlashCommand(command: string): boolean {
67
switch (command) {
68
case 'create-instructions':
69
case 'create-prompt':
70
case 'create-agent':
71
case 'create-skill':
72
return true;
73
default:
74
return false;
75
}
76
}
77
}
78
79