Path: blob/main/src/vs/workbench/contrib/chat/common/chatTransferService.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/4import { IWorkspaceContextService } from '../../../../platform/workspace/common/workspace.js';5import { IStorageService, StorageScope, StorageTarget } from '../../../../platform/storage/common/storage.js';6import { IFileService } from '../../../../platform/files/common/files.js';7import { IWorkspaceTrustManagementService } from '../../../../platform/workspace/common/workspaceTrust.js';8import { areWorkspaceFoldersEmpty } from '../../../services/workspaces/common/workspaceUtils.js';9import { createDecorator } from '../../../../platform/instantiation/common/instantiation.js';10import { URI } from '../../../../base/common/uri.js';1112export const IChatTransferService = createDecorator<IChatTransferService>('chatTransferService');13const transferredWorkspacesKey = 'chat.transferedWorkspaces';1415export interface IChatTransferService {16readonly _serviceBrand: undefined;1718checkAndSetTransferredWorkspaceTrust(): Promise<void>;19addWorkspaceToTransferred(workspace: URI): void;20}2122export class ChatTransferService implements IChatTransferService {23_serviceBrand: undefined;2425constructor(26@IWorkspaceContextService private readonly workspaceService: IWorkspaceContextService,27@IStorageService private readonly storageService: IStorageService,28@IFileService private readonly fileService: IFileService,29@IWorkspaceTrustManagementService private readonly workspaceTrustManagementService: IWorkspaceTrustManagementService30) { }3132deleteWorkspaceFromTransferredList(workspace: URI): void {33const transferredWorkspaces = this.storageService.getObject<string[]>(transferredWorkspacesKey, StorageScope.PROFILE, []);34const updatedWorkspaces = transferredWorkspaces.filter(uri => uri !== workspace.toString());35this.storageService.store(transferredWorkspacesKey, updatedWorkspaces, StorageScope.PROFILE, StorageTarget.MACHINE);36}3738addWorkspaceToTransferred(workspace: URI): void {39const transferredWorkspaces = this.storageService.getObject<string[]>(transferredWorkspacesKey, StorageScope.PROFILE, []);40transferredWorkspaces.push(workspace.toString());41this.storageService.store(transferredWorkspacesKey, transferredWorkspaces, StorageScope.PROFILE, StorageTarget.MACHINE);42}4344async checkAndSetTransferredWorkspaceTrust(): Promise<void> {45const workspace = this.workspaceService.getWorkspace();46const currentWorkspaceUri = workspace.folders[0]?.uri;47if (!currentWorkspaceUri) {48return;49}50if (this.isChatTransferredWorkspace(currentWorkspaceUri, this.storageService) && await areWorkspaceFoldersEmpty(workspace, this.fileService)) {51await this.workspaceTrustManagementService.setWorkspaceTrust(true);52this.deleteWorkspaceFromTransferredList(currentWorkspaceUri);53}54}5556isChatTransferredWorkspace(workspace: URI, storageService: IStorageService): boolean {57if (!workspace) {58return false;59}60const chatWorkspaceTransfer: URI[] = storageService.getObject(transferredWorkspacesKey, StorageScope.PROFILE, []);61return chatWorkspaceTransfer.some(item => item.toString() === workspace.toString());62}63}646566