Skip to content

Commit

Permalink
Save chat input in model for restoring later
Browse files Browse the repository at this point in the history
  • Loading branch information
bhavyaus committed Jul 21, 2023
1 parent 8716df3 commit 8c8c162
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export function getMoveToEditorAction(viewId: string, providerId: string) {
return;
}

view.widget.saveState();
const editorService = accessor.get(IEditorService);
view.clear();
await editorService.openEditor({ resource: ChatEditorInput.getNewEditorUri(), options: <IChatEditorOptions>{ target: { sessionId: viewModel.sessionId }, pinned: true } });
Expand Down
1 change: 1 addition & 0 deletions src/vs/workbench/contrib/chat/browser/chatEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export class ChatEditor extends EditorPane {
private updateModel(model: IChatModel): void {
this._memento = new Memento('interactive-session-editor-' + model.sessionId, this.storageService);
this._viewState = this._memento.getMemento(StorageScope.WORKSPACE, StorageTarget.MACHINE) as IViewState;
this._viewState.inputValue = model.chatInput;
this.widget.setModel(model, { ...this._viewState });
}

Expand Down
16 changes: 15 additions & 1 deletion src/vs/workbench/contrib/chat/browser/chatViewPane.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,22 @@ export class ChatViewPane extends ViewPane implements IChatViewPane {
}));
this._widget.render(parent);

const sessionId = this.chatService.transferredSessionId ?? this.viewState.sessionId;

let sessionId: string | undefined;
let isTransferred = false;
if (this.chatService.transferredSessionId) {
isTransferred = true;
sessionId = this.chatService.transferredSessionId;
} else {
sessionId = this.viewState.sessionId;
}

const initialModel = sessionId ? this.chatService.getOrRestoreSession(sessionId) : undefined;

if (!this.viewState.inputValue && isTransferred && initialModel) {
this.viewState.inputValue = initialModel.chatInput;
}

this.updateModel(initialModel);
} catch (e) {
this.logService.error(e);
Expand Down
1 change: 1 addition & 0 deletions src/vs/workbench/contrib/chat/browser/chatWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,7 @@ export class ChatWidget extends Disposable implements IChatWidget {
}

saveState(): void {
this.viewModel?.setChatInput(this.inputPart.inputEditor.getValue());
this.inputPart.saveState();
}

Expand Down
19 changes: 18 additions & 1 deletion src/vs/workbench/contrib/chat/common/chatModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,11 @@ export interface IChatModel {
readonly welcomeMessage: IChatWelcomeMessageModel | undefined;
readonly requestInProgress: boolean;
readonly inputPlaceholder?: string;
readonly chatInput: string | undefined;
getRequests(): IChatRequestModel[];
toExport(): IExportableChatData;
toJSON(): ISerializableChatData;
setChatInput(chatInput: string): void;
}

export interface ISerializableChatsData {
Expand All @@ -222,6 +224,7 @@ export interface IExportableChatData {
requesterAvatarIconUri: UriComponents | undefined;
responderAvatarIconUri: UriComponents | undefined;
providerState: any;
chatInput: string | undefined;
}

export interface ISerializableChatData extends IExportableChatData {
Expand Down Expand Up @@ -303,6 +306,11 @@ export class ChatModel extends Disposable implements IChatModel {
return this._session?.inputPlaceholder;
}

private _chatInput: string | undefined;
get chatInput(): any {
return this._chatInput;
}

get requestInProgress(): boolean {
const lastRequest = this._requests[this._requests.length - 1];
return !!lastRequest && !!lastRequest.response && !lastRequest.response.isComplete;
Expand Down Expand Up @@ -358,7 +366,7 @@ export class ChatModel extends Disposable implements IChatModel {
this._requests = initialData ? this._deserialize(initialData) : [];
this._providerState = initialData ? initialData.providerState : undefined;
this._creationDate = (isSerializableSessionData(initialData) && initialData.creationDate) || Date.now();

this._chatInput = initialData?.chatInput;
this._initialRequesterAvatarIconUri = initialData?.requesterAvatarIconUri && URI.revive(initialData.requesterAvatarIconUri);
this._initialResponderAvatarIconUri = initialData?.responderAvatarIconUri && URI.revive(initialData.responderAvatarIconUri);
}
Expand All @@ -375,6 +383,10 @@ export class ChatModel extends Disposable implements IChatModel {
this._welcomeMessage = new ChatWelcomeMessageModel(content, obj.responderUsername, obj.responderAvatarIconUri && URI.revive(obj.responderAvatarIconUri));
}

if (obj.chatInput) {
this._chatInput = obj.chatInput;
}

return requests.map((raw: ISerializableChatRequestData) => {
const request = new ChatRequestModel(this, raw.message, raw.providerRequestId);
if (raw.response || raw.responseErrorDetails) {
Expand Down Expand Up @@ -508,6 +520,10 @@ export class ChatModel extends Disposable implements IChatModel {
request.response.setFollowups(followups);
}

setChatInput(chatEditorInput: string): void {
this._chatInput = chatEditorInput;
}

setResponseModel(request: ChatRequestModel, response: ChatResponseModel): void {
request.response = response;
this._onDidChange.fire({ kind: 'addResponse', response });
Expand All @@ -526,6 +542,7 @@ export class ChatModel extends Disposable implements IChatModel {
return c.value;
}
}),
chatInput: this._chatInput,
requests: this._requests.map((r): ISerializableChatRequestData => {
return {
providerRequestId: r.providerRequestId,
Expand Down
5 changes: 5 additions & 0 deletions src/vs/workbench/contrib/chat/common/chatViewModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export interface IChatViewModel {
readonly requestInProgress: boolean;
readonly inputPlaceholder?: string;
getItems(): (IChatRequestViewModel | IChatResponseViewModel | IChatWelcomeMessageViewModel)[];
setChatInput(input: string): void;
}

export interface IChatRequestViewModel {
Expand Down Expand Up @@ -173,6 +174,10 @@ export class ChatViewModel extends Disposable implements IChatViewModel {
return [...(this._model.welcomeMessage ? [this._model.welcomeMessage] : []), ...this._items];
}

setChatInput(input: string): void {
this._model.setChatInput(input);
}

override dispose() {
super.dispose();
this._items
Expand Down

0 comments on commit 8c8c162

Please sign in to comment.