Skip to content

Commit 8f9c8fe

Browse files
committed
Set up persisting of terminal chat model
1 parent d1ba5b0 commit 8f9c8fe

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

src/vs/workbench/contrib/terminalContrib/chat/browser/terminalChatWidget.ts

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { ChatModel, IChatResponseModel } from '../../../chat/common/chatModel.js
2626
import { IChatService, IChatProgress } from '../../../chat/common/chatService.js';
2727
import { CancellationTokenSource } from '../../../../../base/common/cancellation.js';
2828
import { MenuId } from '../../../../../platform/actions/common/actions.js';
29+
import type { IChatViewState } from '../../../chat/browser/chatWidget.js';
2930

3031
const enum Constants {
3132
HorizontalMargin = 10,
@@ -65,7 +66,8 @@ export class TerminalChatWidget extends Disposable {
6566

6667
private _messages = this._store.add(new Emitter<Message>());
6768

68-
private _storageKey = 'terminal-inline-chat-history';
69+
private _historyStorageKey = 'terminal-inline-chat-history';
70+
private _viewStateStorageKey = 'terminal-inline-chat-view-state';
6971
private _promptHistory: string[] = [];
7072

7173
private _lastResponseContent: string | undefined;
@@ -137,6 +139,7 @@ export class TerminalChatWidget extends Disposable {
137139
}
138140
},
139141
);
142+
this._register(this._inlineChatWidget.chatWidget.onDidChangeViewModel(() => this._saveViewState()));
140143
this._register(Event.any(
141144
this._inlineChatWidget.onDidChangeHeight,
142145
this._instance.onDimensionsChanged,
@@ -161,7 +164,7 @@ export class TerminalChatWidget extends Disposable {
161164
this._responseContainsCodeBlockContextKey = TerminalChatContextKeys.responseContainsCodeBlock.bindTo(this._contextKeyService);
162165
this._responseContainsMulitpleCodeBlocksContextKey = TerminalChatContextKeys.responseContainsMultipleCodeBlocks.bindTo(this._contextKeyService);
163166

164-
this._promptHistory = JSON.parse(this._storageService.get(this._storageKey, StorageScope.PROFILE, '[]'));
167+
this._promptHistory = JSON.parse(this._storageService.get(this._historyStorageKey, StorageScope.PROFILE, '[]'));
165168
this._historyUpdate = (prompt: string) => {
166169
const idx = this._promptHistory.indexOf(prompt);
167170
if (idx >= 0) {
@@ -170,7 +173,7 @@ export class TerminalChatWidget extends Disposable {
170173
this._promptHistory.unshift(prompt);
171174
this._historyOffset = -1;
172175
this._historyCandidate = '';
173-
this._storageService.store(this._storageKey, JSON.stringify(this._promptHistory), StorageScope.PROFILE, StorageTarget.USER);
176+
this._storageService.store(this._historyStorageKey, JSON.stringify(this._promptHistory), StorageScope.PROFILE, StorageTarget.USER);
174177
};
175178
}
176179

@@ -217,8 +220,8 @@ export class TerminalChatWidget extends Disposable {
217220
this._inlineChatWidget.updateInfo(localize('welcome.1', "AI-generated commands may be incorrect"));
218221
}
219222

220-
async reveal(): Promise<void> {
221-
await this._createSession();
223+
async reveal(viewState?: IChatViewState): Promise<void> {
224+
await this._createSession(viewState);
222225
this._doLayout(this._inlineChatWidget.contentHeight);
223226
this._container.classList.remove('hide');
224227
this._visibleContextKey.set(true);
@@ -310,13 +313,13 @@ export class TerminalChatWidget extends Disposable {
310313
return this._focusTracker;
311314
}
312315

313-
private async _createSession(): Promise<void> {
316+
private async _createSession(viewState?: IChatViewState): Promise<void> {
314317
this._sessionCtor = createCancelablePromise<void>(async token => {
315318
if (!this._model.value) {
316319
this._model.value = this._chatService.startSession(ChatAgentLocation.Terminal, token);
317320
const model = this._model.value;
318321
if (model) {
319-
this._inlineChatWidget.setChatModel(model);
322+
this._inlineChatWidget.setChatModel(model, this._loadViewState());
320323
}
321324
if (!this._model.value) {
322325
throw new Error('Failed to start chat session');
@@ -326,6 +329,23 @@ export class TerminalChatWidget extends Disposable {
326329
this._register(toDisposable(() => this._sessionCtor?.cancel()));
327330
}
328331

332+
private _loadViewState() {
333+
const rawViewState = this._storageService.get(this._viewStateStorageKey, StorageScope.PROFILE, undefined);
334+
let viewState: IChatViewState | undefined;
335+
if (rawViewState) {
336+
try {
337+
viewState = JSON.parse(rawViewState);
338+
} catch {
339+
viewState = undefined;
340+
}
341+
}
342+
return viewState;
343+
}
344+
345+
private _saveViewState() {
346+
this._storageService.store(this._viewStateStorageKey, JSON.stringify(this._inlineChatWidget.chatWidget.getViewState()), StorageScope.PROFILE, StorageTarget.USER);
347+
}
348+
329349
private _forcedPlaceholder: string | undefined = undefined;
330350

331351
private _updatePlaceholder(): void {

0 commit comments

Comments
 (0)