Skip to content

Commit

Permalink
Fix #213525. Move nb chat variable to service (#213835)
Browse files Browse the repository at this point in the history
  • Loading branch information
rebornix authored May 29, 2024
1 parent b6298d8 commit c98f749
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,38 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { Disposable } from 'vs/base/common/lifecycle';
import { IWorkbenchContribution, registerWorkbenchContribution2, WorkbenchPhase } from 'vs/workbench/common/contributions';
import { IChatVariablesService } from 'vs/workbench/contrib/chat/common/chatVariables';
import 'vs/workbench/contrib/notebook/browser/controller/chat/cellChatActions';
import { NotebookChatController } from 'vs/workbench/contrib/notebook/browser/controller/chat/notebookChatController';
import { INotebookEditorService } from 'vs/workbench/contrib/notebook/browser/services/notebookEditorService';

class NotebookChatVariables extends Disposable implements IWorkbenchContribution {

static readonly ID = 'workbench.contrib.notebookChatVariables';

constructor(
@IChatVariablesService private readonly _chatVariableService: IChatVariablesService,
@INotebookEditorService private readonly _notebookEditorService: INotebookEditorService
) {
super();

this._register(this._chatVariableService.registerVariable(
{ id: '_notebookChatInput', name: '_notebookChatInput', description: '', hidden: true },
async (_message, _arg, model) => {
const editors = this._notebookEditorService.listNotebookEditors();
for (const editor of editors) {
const chatController = editor.getContribution(NotebookChatController.id) as NotebookChatController | undefined;
if (chatController?.hasSession(model)) {
return chatController.getSessionInputUri();
}
}

return undefined;
}
));
}
}

registerWorkbenchContribution2(NotebookChatVariables.ID, NotebookChatVariables, WorkbenchPhase.BlockRestore);
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ import { IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/c
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IStorageService, StorageScope, StorageTarget } from 'vs/platform/storage/common/storage';
import { ChatAgentLocation } from 'vs/workbench/contrib/chat/common/chatAgents';
import { ChatModel } from 'vs/workbench/contrib/chat/common/chatModel';
import { ChatModel, IChatModel } from 'vs/workbench/contrib/chat/common/chatModel';
import { IChatService } from 'vs/workbench/contrib/chat/common/chatService';
import { IChatVariablesService } from 'vs/workbench/contrib/chat/common/chatVariables';
import { countWords } from 'vs/workbench/contrib/chat/common/chatWordCounter';
import { ProgressingEditsOptions } from 'vs/workbench/contrib/inlineChat/browser/inlineChatStrategies';
import { InlineChatWidget } from 'vs/workbench/contrib/inlineChat/browser/inlineChatWidget';
Expand Down Expand Up @@ -279,8 +278,7 @@ export class NotebookChatController extends Disposable implements INotebookEdito
@ILanguageService private readonly _languageService: ILanguageService,
@INotebookExecutionStateService private _executionStateService: INotebookExecutionStateService,
@IStorageService private readonly _storageService: IStorageService,
@IChatService private readonly _chatService: IChatService,
@IChatVariablesService private readonly _chatVariableService: IChatVariablesService,
@IChatService private readonly _chatService: IChatService
) {
super();
this._ctxHasActiveRequest = CTX_NOTEBOOK_CHAT_HAS_ACTIVE_REQUEST.bindTo(this._contextKeyService);
Expand All @@ -301,13 +299,6 @@ export class NotebookChatController extends Disposable implements INotebookEdito
this._historyCandidate = '';
this._storageService.store(NotebookChatController._storageKey, JSON.stringify(NotebookChatController._promptHistory), StorageScope.PROFILE, StorageTarget.USER);
};

this._register(this._chatVariableService.registerVariable(
{ id: '_notebookChatInput', name: '_notebookChatInput', description: '', hidden: true },
async (_message, _arg, model) => {
return this._widget?.parentEditor.getModel()?.uri;
}
));
}

private _registerFocusTracker() {
Expand Down Expand Up @@ -541,6 +532,14 @@ export class NotebookChatController extends Disposable implements INotebookEdito
this._widget.updateNotebookEditorFocusNSelections();
}

hasSession(chatModel: IChatModel) {
return this._model.value === chatModel;
}

getSessionInputUri() {
return this._widget?.parentEditor.getModel()?.uri;
}

async acceptInput() {
assertType(this._widget);
await this._sessionCtor;
Expand Down

0 comments on commit c98f749

Please sign in to comment.