Skip to content

Commit

Permalink
fix: use persisted chat session (microsoft#195959)
Browse files Browse the repository at this point in the history
Also don't record init failure on session clear
  • Loading branch information
joyceerhl committed Oct 19, 2023
1 parent 29c169c commit c42f896
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
30 changes: 18 additions & 12 deletions src/vs/workbench/contrib/chat/browser/chatViewPane.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ export class ChatViewPane extends ViewPane implements IChatViewPane {
this.viewState = this.memento.getMemento(StorageScope.WORKSPACE, StorageTarget.MACHINE) as IViewPaneState;
this._register(this.chatService.onDidRegisterProvider(({ providerId }) => {
if (providerId === this.chatViewOptions.providerId && !this._widget?.viewModel) {
this.updateModel();
const sessionId = this.getSessionId();
const model = sessionId ? this.chatService.getOrRestoreSession(sessionId) : undefined;
this.updateModel(model);
}
}));
}
Expand All @@ -93,6 +95,17 @@ export class ChatViewPane extends ViewPane implements IChatViewPane {
return !this._widget?.viewModel && (noPersistedSessions || this.didProviderRegistrationFail);
}

private getSessionId() {
let sessionId: string | undefined;
if (this.chatService.transferredSessionData) {
sessionId = this.chatService.transferredSessionData.sessionId;
this.viewState.inputValue = this.chatService.transferredSessionData.inputValue;
} else {
sessionId = this.viewState.sessionId;
}
return sessionId;
}

protected override renderBody(parent: HTMLElement): void {
try {
super.renderBody(parent);
Expand All @@ -115,26 +128,19 @@ export class ChatViewPane extends ViewPane implements IChatViewPane {
this._register(this._widget.onDidClear(() => this.clear()));
this._widget.render(parent);

let sessionId: string | undefined;
if (this.chatService.transferredSessionData) {
sessionId = this.chatService.transferredSessionData.sessionId;
this.viewState.inputValue = this.chatService.transferredSessionData.inputValue;
} else {
sessionId = this.viewState.sessionId;
}

const sessionId = this.getSessionId();
// Render the welcome view if this session gets disposed at any point,
// including if the provider registration fails
const disposeListener = sessionId ? this._register(this.chatService.onDidDisposeSession((e) => {
if (e.sessionId === sessionId) {
if (e.reason === 'initializationFailed' && e.sessionId === sessionId) {
this.didProviderRegistrationFail = true;
disposeListener?.dispose();
this._onDidChangeViewWelcomeState.fire();
}
})) : undefined;
const model = sessionId ? this.chatService.getOrRestoreSession(sessionId) : undefined;

const initialModel = sessionId ? this.chatService.getOrRestoreSession(sessionId) : undefined;
this.updateModel(initialModel);
this.updateModel(model);
} catch (e) {
this.logService.error(e);
throw e;
Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/contrib/chat/common/chatService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ export interface IChatService {

onDidPerformUserAction: Event<IChatUserActionEvent>;
notifyUserAction(event: IChatUserActionEvent): void;
onDidDisposeSession: Event<{ sessionId: string }>;
onDidDisposeSession: Event<{ sessionId: string; reason: 'initializationFailed' | 'cleared' }>;

transferChatSession(transferredSessionData: IChatTransferredSessionData, toWorkspace: URI): void;
}
6 changes: 3 additions & 3 deletions src/vs/workbench/contrib/chat/common/chatServiceImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export class ChatService extends Disposable implements IChatService {
private readonly _onDidSubmitSlashCommand = this._register(new Emitter<{ slashCommand: string; sessionId: string }>());
public readonly onDidSubmitSlashCommand = this._onDidSubmitSlashCommand.event;

private readonly _onDidDisposeSession = this._register(new Emitter<{ sessionId: string }>());
private readonly _onDidDisposeSession = this._register(new Emitter<{ sessionId: string; reason: 'initializationFailed' | 'cleared' }>());
public readonly onDidDisposeSession = this._onDidDisposeSession.event;

private readonly _onDidRegisterProvider = this._register(new Emitter<{ providerId: string }>());
Expand Down Expand Up @@ -379,7 +379,7 @@ export class ChatService extends Disposable implements IChatService {
model.setInitializationError(err);
model.dispose();
this._sessionModels.delete(model.sessionId);
this._onDidDisposeSession.fire({ sessionId: model.sessionId });
this._onDidDisposeSession.fire({ sessionId: model.sessionId, reason: 'initializationFailed' });
}
}

Expand Down Expand Up @@ -733,7 +733,7 @@ export class ChatService extends Disposable implements IChatService {
model.dispose();
this._sessionModels.delete(sessionId);
this._pendingRequests.get(sessionId)?.cancel();
this._onDidDisposeSession.fire({ sessionId });
this._onDidDisposeSession.fire({ sessionId, reason: 'cleared' });
}

registerProvider(provider: IChatProvider): IDisposable {
Expand Down

0 comments on commit c42f896

Please sign in to comment.