Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/chat/common/chat.ts
5263 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 { ResourceSet } from '../../../../base/common/map.js';
7
import { chatEditingSessionIsReady } from './editing/chatEditingService.js';
8
import { IChatModel } from './model/chatModel.js';
9
import { isLegacyChatTerminalToolInvocationData, type IChatSessionStats, type IChatTerminalToolInvocationData, type ILegacyChatTerminalToolInvocationData } from './chatService/chatService.js';
10
import { ChatModeKind } from './constants.js';
11
12
export function checkModeOption(mode: ChatModeKind, option: boolean | ((mode: ChatModeKind) => boolean) | undefined): boolean | undefined {
13
if (option === undefined) {
14
return undefined;
15
}
16
if (typeof option === 'function') {
17
return option(mode);
18
}
19
return option;
20
}
21
22
/**
23
* @deprecated This is the old API shape, we should support this for a while before removing it so
24
* we don't break existing chats
25
*/
26
export function migrateLegacyTerminalToolSpecificData(data: IChatTerminalToolInvocationData | ILegacyChatTerminalToolInvocationData): IChatTerminalToolInvocationData {
27
if (isLegacyChatTerminalToolInvocationData(data)) {
28
data = {
29
kind: 'terminal',
30
commandLine: {
31
original: data.command,
32
toolEdited: undefined,
33
userEdited: undefined
34
},
35
language: data.language
36
} satisfies IChatTerminalToolInvocationData;
37
}
38
return data;
39
}
40
41
export async function awaitStatsForSession(model: IChatModel): Promise<IChatSessionStats | undefined> {
42
if (!model.editingSession) {
43
return undefined;
44
}
45
46
await chatEditingSessionIsReady(model.editingSession);
47
await Promise.all(model.editingSession.entries.get().map(entry => entry.getDiffInfo?.()));
48
49
const diffs = model.editingSession.entries.get();
50
const reduceResult = diffs.reduce((acc, diff) => {
51
acc.fileUris.add(diff.originalURI);
52
acc.added += diff.linesAdded?.get() ?? 0;
53
acc.removed += diff.linesRemoved?.get() ?? 0;
54
return acc;
55
}, { fileUris: new ResourceSet(), added: 0, removed: 0 });
56
57
if (reduceResult.fileUris.size > 0 && (reduceResult.added > 0 || reduceResult.removed > 0)) {
58
return {
59
fileCount: reduceResult.fileUris.size,
60
added: reduceResult.added,
61
removed: reduceResult.removed
62
};
63
}
64
return undefined;
65
}
66
67