Skip to content

Commit 246232b

Browse files
authored
Monitor Copilot CLI session directory for creation of sessions outside chat editor (#1793)
1 parent 69af445 commit 246232b

File tree

5 files changed

+24
-9
lines changed

5 files changed

+24
-9
lines changed

src/extension/agents/copilotcli/node/copilotcliSessionService.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import type { ModelProvider, Session, SessionManager, internal } from '@github/copilot/sdk';
6+
import type { internal, ModelProvider, Session, SessionManager } from '@github/copilot/sdk';
77
import { ChatCompletionMessageParam } from 'openai/resources/chat/completions';
88
import type { CancellationToken, ChatRequest } from 'vscode';
9+
import { INativeEnvService } from '../../../../platform/env/common/envService';
10+
import { IFileSystemService } from '../../../../platform/filesystem/common/fileSystemService';
11+
import { RelativePattern } from '../../../../platform/filesystem/common/fileTypes';
912
import { ILogService } from '../../../../platform/log/common/logService';
1013
import { createServiceIdentifier } from '../../../../util/common/services';
1114
import { coalesce } from '../../../../util/vs/base/common/arrays';
1215
import { raceCancellationError } from '../../../../util/vs/base/common/async';
1316
import { Emitter, Event } from '../../../../util/vs/base/common/event';
1417
import { Lazy } from '../../../../util/vs/base/common/lazy';
1518
import { Disposable, DisposableMap, DisposableStore, IDisposable, toDisposable } from '../../../../util/vs/base/common/lifecycle';
19+
import { joinPath } from '../../../../util/vs/base/common/resources';
1620
import { IInstantiationService } from '../../../../util/vs/platform/instantiation/common/instantiation';
1721
import { ChatSessionStatus } from '../../../../vscodeTypes';
1822
import { ICopilotCLISDK } from './copilotCli';
@@ -63,10 +67,12 @@ export class CopilotCLISessionService extends Disposable implements ICopilotCLIS
6367
constructor(
6468
@ILogService private readonly logService: ILogService,
6569
@ICopilotCLISDK private readonly copilotCLISDK: ICopilotCLISDK,
66-
@IInstantiationService private readonly instantiationService: IInstantiationService
70+
@IInstantiationService private readonly instantiationService: IInstantiationService,
71+
@INativeEnvService private readonly nativeEnv: INativeEnvService,
72+
@IFileSystemService private readonly fileSystem: IFileSystemService,
6773
) {
6874
super();
69-
75+
this.monitorSessionFiles();
7076
this._sessionManager = new Lazy<Promise<internal.CLISessionManager>>(async () => {
7177
const { internal } = await this.copilotCLISDK.getPackage();
7278
return new internal.CLISessionManager({
@@ -75,6 +81,15 @@ export class CopilotCLISessionService extends Disposable implements ICopilotCLIS
7581
});
7682
}
7783

84+
private monitorSessionFiles() {
85+
try {
86+
const sessionDir = joinPath(this.nativeEnv.userHome, '.copilot', 'session-state');
87+
const watcher = this._register(this.fileSystem.createFileSystemWatcher(new RelativePattern(sessionDir, '*.jsonl')));
88+
this._register(watcher.onDidCreate(() => this._onDidChangeSessions.fire()));
89+
} catch (error) {
90+
this.logService.error(`Failed to monitor Copilot CLI session files: ${error}`);
91+
}
92+
}
7893
async getSessionManager() {
7994
return this._sessionManager.value;
8095
}

src/platform/filesystem/common/fileSystemService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import type { FileStat, FileSystem, FileSystemWatcher, Uri } from 'vscode';
6+
import type { FileStat, FileSystem, FileSystemWatcher, RelativePattern, Uri } from 'vscode';
77
import { LRUCache } from '../../../util/common/cache';
88
import { createServiceIdentifier } from '../../../util/common/services';
99
import { FileType } from './fileTypes';
@@ -29,7 +29,7 @@ export interface IFileSystemService extends FileSystem {
2929
copy(source: Uri, destination: Uri, options?: { overwrite?: boolean }): Promise<void>;
3030
isWritableFileSystem(scheme: string): boolean | undefined;
3131

32-
createFileSystemWatcher(glob: string): FileSystemWatcher;
32+
createFileSystemWatcher(glob: string | RelativePattern): FileSystemWatcher;
3333
}
3434

3535
/**

src/platform/filesystem/node/fileSystemServiceImpl.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import * as fs from 'fs';
7-
import type { FileStat, FileSystemWatcher, Uri } from 'vscode';
7+
import type { FileStat, FileSystemWatcher, RelativePattern, Uri } from 'vscode';
88
import { Event } from '../../../util/vs/base/common/event';
99
import { dirname, isEqual } from '../../../util/vs/base/common/resources';
1010
import { URI } from '../../../util/vs/base/common/uri';
@@ -82,7 +82,7 @@ export class NodeFileSystemService implements IFileSystemService {
8282
return true;
8383
}
8484

85-
createFileSystemWatcher(_glob: string): FileSystemWatcher {
85+
createFileSystemWatcher(_glob: string | RelativePattern): FileSystemWatcher {
8686
return new class implements FileSystemWatcher {
8787
ignoreCreateEvents = false;
8888
ignoreChangeEvents = false;

src/platform/filesystem/vscode/fileSystemServiceImpl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export class VSCodeFileSystemService implements IFileSystemService {
4949
return !!vscode.workspace.fs.isWritableFileSystem(scheme);
5050
}
5151

52-
createFileSystemWatcher(glob: string): vscode.FileSystemWatcher {
52+
createFileSystemWatcher(glob: string | vscode.RelativePattern): vscode.FileSystemWatcher {
5353
return vscode.workspace.createFileSystemWatcher(glob);
5454
}
5555
}

src/platform/test/node/simulationWorkspaceServices.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ export class SimulationFileSystemAdaptor implements IFileSystemService {
235235
return this._delegate.isWritableFileSystem(scheme);
236236
}
237237

238-
createFileSystemWatcher(glob: string): vscode.FileSystemWatcher {
238+
createFileSystemWatcher(glob: string | vscode.RelativePattern): vscode.FileSystemWatcher {
239239
return this._delegate.createFileSystemWatcher(glob);
240240
}
241241
}

0 commit comments

Comments
 (0)