Skip to content

Commit

Permalink
Store input value in IChatTransferData
Browse files Browse the repository at this point in the history
  • Loading branch information
bhavyaus committed Jul 22, 2023
1 parent 8716df3 commit 0ca38fe
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
4 changes: 3 additions & 1 deletion src/vs/workbench/api/browser/mainThreadChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ export class MainThreadChat extends Disposable implements MainThreadChatShape {
}

$transferChatSession(sessionId: number, toWorkspace: UriComponents): void {
this._chatService.transferChatSession(sessionId, URI.revive(toWorkspace));
const widget = this._chatWidgetService.lastFocusedWidget;
const inputValue = widget?.inputEditor.getValue() ?? '';
this._chatService.transferChatSession({ sessionProviderId: sessionId, inputValue: inputValue }, URI.revive(toWorkspace));
}

async $registerChatProvider(handle: number, id: string): Promise<void> {
Expand Down
9 changes: 8 additions & 1 deletion src/vs/workbench/contrib/chat/browser/chatViewPane.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,14 @@ export class ChatViewPane extends ViewPane implements IChatViewPane {
}));
this._widget.render(parent);

const sessionId = this.chatService.transferredSessionId ?? this.viewState.sessionId;
let sessionId: string | undefined;
if (this.chatService.transferredSessionId) {
sessionId = this.chatService.transferredSessionId;
this.viewState.inputValue = this.chatService.transferredInputValue;
} else {
sessionId = this.viewState.sessionId;
}

const initialModel = sessionId ? this.chatService.getOrRestoreSession(sessionId) : undefined;
this.updateModel(initialModel);
} catch (e) {
Expand Down
3 changes: 2 additions & 1 deletion src/vs/workbench/contrib/chat/common/chatService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ export const IChatService = createDecorator<IChatService>('IChatService');
export interface IChatService {
_serviceBrand: undefined;
transferredSessionId: string | undefined;
transferredInputValue: string | undefined;

onDidSubmitSlashCommand: Event<{ slashCommand: string; sessionId: string }>;
registerProvider(provider: IChatProvider): IDisposable;
Expand Down Expand Up @@ -221,5 +222,5 @@ export interface IChatService {
onDidPerformUserAction: Event<IChatUserActionEvent>;
notifyUserAction(event: IChatUserActionEvent): void;

transferChatSession(sessionProviderId: number, toWorkspace: URI): void;
transferChatSession(transferredSessionData: { sessionProviderId: number; inputValue: string }, toWorkspace: URI): void;
}
32 changes: 21 additions & 11 deletions src/vs/workbench/contrib/chat/common/chatServiceImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ interface IChatTransfer {
toWorkspace: UriComponents;
timestampInMilliseconds: number;
chat: ISerializableChatData;
inputValue: string;
}
const SESSION_TRANSFER_EXPIRATION_IN_MILLISECONDS = 1000 * 60;

Expand Down Expand Up @@ -127,11 +128,15 @@ export class ChatService extends Disposable implements IChatService {
private readonly _persistedSessions: ISerializableChatsData;
private readonly _hasProvider: IContextKey<boolean>;

private _transferred: ISerializableChatData | undefined;
private _transferredChat: ISerializableChatData | undefined;
public get transferredSessionId(): string | undefined {
return this._transferred?.sessionId;
return this._transferredChat?.sessionId;
}

private _transferredInputValue: string | undefined;
public get transferredInputValue(): string | undefined {
return this._transferredInputValue;
}
private readonly _onDidPerformUserAction = this._register(new Emitter<IChatUserActionEvent>());
public readonly onDidPerformUserAction: Event<IChatUserActionEvent> = this._onDidPerformUserAction.event;

Expand Down Expand Up @@ -162,11 +167,13 @@ export class ChatService extends Disposable implements IChatService {
this._persistedSessions = {};
}

this._transferred = this.getTransferredSession();
if (this._transferred) {
this.trace('constructor', `Transferred session ${this._transferred.sessionId}`);
this._persistedSessions[this._transferred.sessionId] = this._transferred;
const transferredData = this.getTransferredSessionData();
this._transferredChat = transferredData?.chat;
if (this._transferredChat) {
this.trace('constructor', `Transferred session ${this._transferredChat.sessionId}`);
this._persistedSessions[this._transferredChat.sessionId] = this._transferredChat;
}
this._transferredInputValue = transferredData?.inputValue;

this._register(storageService.onWillSaveState(() => this.saveState()));
}
Expand Down Expand Up @@ -252,7 +259,7 @@ export class ChatService extends Disposable implements IChatService {
}
}

private getTransferredSession(): ISerializableChatData | undefined {
private getTransferredSessionData(): IChatTransfer | undefined {
const data: IChatTransfer[] = this.storageService.getObject(globalChatKey, StorageScope.PROFILE, []);
const workspaceUri = this.workspaceContextService.getWorkspace().folders[0]?.uri;
if (!workspaceUri) {
Expand All @@ -266,7 +273,7 @@ export class ChatService extends Disposable implements IChatService {
// Keep data that isn't for the current workspace and that hasn't expired yet
const filtered = data.filter(item => URI.revive(item.toWorkspace).toString() !== thisWorkspace && (currentTime - item.timestampInMilliseconds < SESSION_TRANSFER_EXPIRATION_IN_MILLISECONDS));
this.storageService.store(globalChatKey, JSON.stringify(filtered), StorageScope.PROFILE, StorageTarget.MACHINE);
return transferred?.chat;
return transferred;
}

getHistory(): IChatDetail[] {
Expand Down Expand Up @@ -367,7 +374,8 @@ export class ChatService extends Disposable implements IChatService {
}

if (sessionId === this.transferredSessionId) {
this._transferred = undefined;
this._transferredChat = undefined;
this._transferredInputValue = undefined;
}

return this._startSession(sessionData.providerId, sessionData, CancellationToken.None);
Expand Down Expand Up @@ -669,7 +677,8 @@ export class ChatService extends Disposable implements IChatService {
});
}

transferChatSession(sessionProviderId: number, toWorkspace: URI): void {
transferChatSession(transferredSessionData: { sessionProviderId: number; inputValue: string }, toWorkspace: URI): void {
const sessionProviderId = transferredSessionData.sessionProviderId;
const model = Iterable.find(this._sessionModels.values(), model => model.session?.id === sessionProviderId);
if (!model) {
throw new Error(`Failed to transfer session. Unknown session provider ID: ${sessionProviderId}`);
Expand All @@ -679,7 +688,8 @@ export class ChatService extends Disposable implements IChatService {
existingRaw.push({
chat: model.toJSON(),
timestampInMilliseconds: Date.now(),
toWorkspace: toWorkspace
toWorkspace: toWorkspace,
inputValue: transferredSessionData.inputValue,
});

this.storageService.store(globalChatKey, JSON.stringify(existingRaw), StorageScope.PROFILE, StorageTarget.MACHINE);
Expand Down

0 comments on commit 0ca38fe

Please sign in to comment.