Skip to content

Commit d79cd9d

Browse files
authored
Fix chat history storage in empty windows (#246003)
Fix microsoft/vscode-copilot-release#7216
1 parent bf3f7f0 commit d79cd9d

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

src/vs/workbench/contrib/chat/common/chatSessionStore.ts

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { FileOperationResult, IFileService, toFileOperationResult } from '../../
1717
import { ILogService } from '../../../../platform/log/common/log.js';
1818
import { IStorageService, StorageScope, StorageTarget } from '../../../../platform/storage/common/storage.js';
1919
import { ITelemetryService } from '../../../../platform/telemetry/common/telemetry.js';
20+
import { IUserDataProfilesService } from '../../../../platform/userDataProfile/common/userDataProfile.js';
2021
import { IWorkspaceContextService } from '../../../../platform/workspace/common/workspace.js';
2122
import { ILifecycleService } from '../../../services/lifecycle/common/lifecycle.js';
2223
import { ChatModel, ISerializableChatData, ISerializableChatDataIn, ISerializableChatsData, normalizeSerializableChatData } from './chatModel.js';
@@ -29,6 +30,7 @@ const ChatIndexStorageKey = 'chat.ChatSessionStore.index';
2930

3031
export class ChatSessionStore extends Disposable {
3132
private readonly storageRoot: URI;
33+
private readonly previousEmptyWindowStorageRoot: URI | undefined;
3234
// private readonly transferredSessionStorageRoot: URI;
3335

3436
private readonly storeQueue = new Sequencer();
@@ -44,15 +46,20 @@ export class ChatSessionStore extends Disposable {
4446
@ITelemetryService private readonly telemetryService: ITelemetryService,
4547
@IStorageService private readonly storageService: IStorageService,
4648
@ILifecycleService private readonly lifecycleService: ILifecycleService,
49+
@IUserDataProfilesService private readonly userDataProfilesService: IUserDataProfilesService,
4750
) {
4851
super();
4952

5053
const workspace = this.workspaceContextService.getWorkspace();
5154
const isEmptyWindow = !workspace.configuration && workspace.folders.length === 0;
52-
const workspaceId = isEmptyWindow ?
53-
'no-workspace' :
54-
this.workspaceContextService.getWorkspace().id;
55-
this.storageRoot = joinPath(this.environmentService.workspaceStorageHome, workspaceId, 'chatSessions');
55+
const workspaceId = this.workspaceContextService.getWorkspace().id;
56+
this.storageRoot = isEmptyWindow ?
57+
joinPath(this.userDataProfilesService.defaultProfile.globalStorageHome, 'emptyWindowChatSessions') :
58+
joinPath(this.environmentService.workspaceStorageHome, workspaceId, 'chatSessions');
59+
60+
this.previousEmptyWindowStorageRoot = isEmptyWindow ?
61+
joinPath(this.environmentService.workspaceStorageHome, 'no-workspace', 'chatSessions') :
62+
undefined;
5663

5764
// TODO tmpdir
5865
// this.transferredSessionStorageRoot = joinPath(this.environmentService.workspaceStorageHome, 'transferredChatSessions');
@@ -309,13 +316,20 @@ export class ChatSessionStore extends Disposable {
309316

310317
public async readSession(sessionId: string): Promise<ISerializableChatData | undefined> {
311318
return await this.storeQueue.queue(async () => {
312-
let rawData: string;
319+
let rawData: string | undefined;
313320
const storageLocation = this.getStorageLocation(sessionId);
314321
try {
315322
rawData = (await this.fileService.readFile(storageLocation)).value.toString();
316323
} catch (e) {
317324
this.reportError('sessionReadFile', `Error reading chat session file ${sessionId}`, e);
318-
return undefined;
325+
326+
if (toFileOperationResult(e) === FileOperationResult.FILE_NOT_FOUND && this.previousEmptyWindowStorageRoot) {
327+
rawData = await this.readSessionFromPreviousLocation(sessionId);
328+
}
329+
330+
if (!rawData) {
331+
return undefined;
332+
}
319333
}
320334

321335
try {
@@ -343,6 +357,23 @@ export class ChatSessionStore extends Disposable {
343357
});
344358
}
345359

360+
private async readSessionFromPreviousLocation(sessionId: string): Promise<string | undefined> {
361+
let rawData: string | undefined;
362+
363+
if (this.previousEmptyWindowStorageRoot) {
364+
const storageLocation2 = joinPath(this.previousEmptyWindowStorageRoot, `${sessionId}.json`);
365+
try {
366+
rawData = (await this.fileService.readFile(storageLocation2)).value.toString();
367+
this.logService.info(`ChatSessionStore: Read chat session ${sessionId} from previous location`);
368+
} catch (e) {
369+
this.reportError('sessionReadFile', `Error reading chat session file ${sessionId} from previous location`, e);
370+
return undefined;
371+
}
372+
}
373+
374+
return rawData;
375+
}
376+
346377
private getStorageLocation(chatSessionId: string): URI {
347378
return joinPath(this.storageRoot, `${chatSessionId}.json`);
348379
}

0 commit comments

Comments
 (0)