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 { SessionOptions } from '@github/copilot/sdk' ;
7+ import { IAuthenticationService } from '../../../../platform/authentication/common/authentication' ;
8+ import { ILogService } from '../../../../platform/log/common/logService' ;
9+ import { IWorkspaceService } from '../../../../platform/workspace/common/workspaceService' ;
10+ import { Disposable , IDisposable , toDisposable } from '../../../../util/vs/base/common/lifecycle' ;
11+ import { getCopilotLogger } from './logger' ;
12+
13+ export class CopilotCLISessionOptionsService {
14+ constructor (
15+ @IWorkspaceService private readonly workspaceService : IWorkspaceService ,
16+ @IAuthenticationService private readonly _authenticationService : IAuthenticationService ,
17+ @ILogService private readonly logService : ILogService ,
18+ ) { }
19+
20+ public async createOptions ( options : SessionOptions , permissionHandler : CopilotCLIPermissionsHandler ) {
21+ const copilotToken = await this . _authenticationService . getCopilotToken ( ) ;
22+ const workingDirectory = await this . getWorkspaceFolderPath ( ) ;
23+ const allOptions : SessionOptions = {
24+ copilotToken : copilotToken . token ,
25+ env : {
26+ ...process . env ,
27+ COPILOTCLI_DISABLE_NONESSENTIAL_TRAFFIC : '1'
28+ } ,
29+ logger : getCopilotLogger ( this . logService ) ,
30+ requestPermission : async ( permissionRequest ) => {
31+ return await permissionHandler . getPermissions ( permissionRequest ) ;
32+ } ,
33+ ...options
34+ } ;
35+
36+ if ( workingDirectory ) {
37+ allOptions . workingDirectory = workingDirectory ;
38+ }
39+ return allOptions ;
40+ }
41+ private async getWorkspaceFolderPath ( ) {
42+ if ( this . workspaceService . getWorkspaceFolders ( ) . length === 0 ) {
43+ return undefined ;
44+ }
45+ if ( this . workspaceService . getWorkspaceFolders ( ) . length === 1 ) {
46+ return this . workspaceService . getWorkspaceFolders ( ) [ 0 ] . fsPath ;
47+ }
48+ const folder = await this . workspaceService . showWorkspaceFolderPicker ( ) ;
49+ return folder ?. uri ?. fsPath ;
50+ }
51+ }
52+
53+ export interface ICopilotCLIPermissions {
54+ onDidRequestPermissions ( handler : SessionOptions [ 'requestPermission' ] ) : IDisposable ;
55+ }
56+
57+ export class CopilotCLIPermissionsHandler extends Disposable implements ICopilotCLIPermissions {
58+ private _handler : SessionOptions [ 'requestPermission' ] | undefined ;
59+
60+ public onDidRequestPermissions ( handler : SessionOptions [ 'requestPermission' ] ) : IDisposable {
61+ this . _handler = handler ;
62+ return this . _register ( toDisposable ( ( ) => {
63+ this . _handler = undefined ;
64+ } ) ) ;
65+ }
66+
67+ public async getPermissions ( permission : Parameters < NonNullable < SessionOptions [ 'requestPermission' ] > > [ 0 ] ) : Promise < ReturnType < NonNullable < SessionOptions [ 'requestPermission' ] > > > {
68+ if ( ! this . _handler ) {
69+ return {
70+ kind : "denied-interactively-by-user"
71+ } ;
72+ }
73+ return await this . _handler ( permission ) ;
74+ }
75+ }
0 commit comments