Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/chat/common/chatTransferService.ts
3296 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
import { IWorkspaceContextService } from '../../../../platform/workspace/common/workspace.js';
6
import { IStorageService, StorageScope, StorageTarget } from '../../../../platform/storage/common/storage.js';
7
import { IFileService } from '../../../../platform/files/common/files.js';
8
import { IWorkspaceTrustManagementService } from '../../../../platform/workspace/common/workspaceTrust.js';
9
import { areWorkspaceFoldersEmpty } from '../../../services/workspaces/common/workspaceUtils.js';
10
import { createDecorator } from '../../../../platform/instantiation/common/instantiation.js';
11
import { URI } from '../../../../base/common/uri.js';
12
13
export const IChatTransferService = createDecorator<IChatTransferService>('chatTransferService');
14
const transferredWorkspacesKey = 'chat.transferedWorkspaces';
15
16
export interface IChatTransferService {
17
readonly _serviceBrand: undefined;
18
19
checkAndSetTransferredWorkspaceTrust(): Promise<void>;
20
addWorkspaceToTransferred(workspace: URI): void;
21
}
22
23
export class ChatTransferService implements IChatTransferService {
24
_serviceBrand: undefined;
25
26
constructor(
27
@IWorkspaceContextService private readonly workspaceService: IWorkspaceContextService,
28
@IStorageService private readonly storageService: IStorageService,
29
@IFileService private readonly fileService: IFileService,
30
@IWorkspaceTrustManagementService private readonly workspaceTrustManagementService: IWorkspaceTrustManagementService
31
) { }
32
33
deleteWorkspaceFromTransferredList(workspace: URI): void {
34
const transferredWorkspaces = this.storageService.getObject<string[]>(transferredWorkspacesKey, StorageScope.PROFILE, []);
35
const updatedWorkspaces = transferredWorkspaces.filter(uri => uri !== workspace.toString());
36
this.storageService.store(transferredWorkspacesKey, updatedWorkspaces, StorageScope.PROFILE, StorageTarget.MACHINE);
37
}
38
39
addWorkspaceToTransferred(workspace: URI): void {
40
const transferredWorkspaces = this.storageService.getObject<string[]>(transferredWorkspacesKey, StorageScope.PROFILE, []);
41
transferredWorkspaces.push(workspace.toString());
42
this.storageService.store(transferredWorkspacesKey, transferredWorkspaces, StorageScope.PROFILE, StorageTarget.MACHINE);
43
}
44
45
async checkAndSetTransferredWorkspaceTrust(): Promise<void> {
46
const workspace = this.workspaceService.getWorkspace();
47
const currentWorkspaceUri = workspace.folders[0]?.uri;
48
if (!currentWorkspaceUri) {
49
return;
50
}
51
if (this.isChatTransferredWorkspace(currentWorkspaceUri, this.storageService) && await areWorkspaceFoldersEmpty(workspace, this.fileService)) {
52
await this.workspaceTrustManagementService.setWorkspaceTrust(true);
53
this.deleteWorkspaceFromTransferredList(currentWorkspaceUri);
54
}
55
}
56
57
isChatTransferredWorkspace(workspace: URI, storageService: IStorageService): boolean {
58
if (!workspace) {
59
return false;
60
}
61
const chatWorkspaceTransfer: URI[] = storageService.getObject(transferredWorkspacesKey, StorageScope.PROFILE, []);
62
return chatWorkspaceTransfer.some(item => item.toString() === workspace.toString());
63
}
64
}
65
66