Skip to content

Commit 2a347db

Browse files
authored
Prepare for inline chat participants (microsoft#209628)
* debt - remove bridge agent when "true" agent appears * create a SessionExchange from chat model request/response pair * fix markdown message passing * use (a little) more of ChatModel world and less of inline chat models * remove unused test * fix tests
1 parent 47fc35d commit 2a347db

File tree

6 files changed

+192
-116
lines changed

6 files changed

+192
-116
lines changed

src/vs/workbench/contrib/chat/common/chatAgents.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export namespace ChatAgentLocation {
3939
case 'panel': return ChatAgentLocation.Panel;
4040
case 'terminal': return ChatAgentLocation.Terminal;
4141
case 'notebook': return ChatAgentLocation.Notebook;
42+
case 'editor': return ChatAgentLocation.Editor;
4243
}
4344
return ChatAgentLocation.Panel;
4445
}

src/vs/workbench/contrib/chat/common/chatModel.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,10 @@ export class ChatModel extends Disposable implements IChatModel {
527527
return !!lastRequest && !!lastRequest.response && !lastRequest.response.isComplete;
528528
}
529529

530+
get hasRequests(): boolean {
531+
return this._requests.length > 0;
532+
}
533+
530534
private _creationDate: number;
531535
get creationDate(): number {
532536
return this._creationDate;

src/vs/workbench/contrib/inlineChat/browser/inlineChatController.ts

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import { IModelDeltaDecoration, ITextModel, IValidEditOperation } from 'vs/edito
4545
import { InlineChatContentWidget } from 'vs/workbench/contrib/inlineChat/browser/inlineChatContentWidget';
4646
import { MessageController } from 'vs/editor/contrib/message/browser/messageController';
4747
import { tail } from 'vs/base/common/arrays';
48-
import { IChatRequestModel } from 'vs/workbench/contrib/chat/common/chatModel';
48+
import { IChatRequestModel, IResponse } from 'vs/workbench/contrib/chat/common/chatModel';
4949
import { InlineChatError } from 'vs/workbench/contrib/inlineChat/browser/inlineChatSessionServiceImpl';
5050
import { ILanguageFeaturesService } from 'vs/editor/common/services/languageFeatures';
5151
import { ChatInputPart } from 'vs/workbench/contrib/chat/browser/chatInputPart';
@@ -197,10 +197,9 @@ export class InlineChatController implements IEditorContribution {
197197

198198
dispose(): void {
199199
if (this._currentRun) {
200-
this._messages.fire((this._session?.lastExchange
200+
this._messages.fire(this._session?.chatModel.hasRequests
201201
? Message.PAUSE_SESSION
202-
: Message.CANCEL_SESSION)
203-
);
202+
: Message.CANCEL_SESSION);
204203
}
205204
this._store.dispose();
206205
this._isDisposed = true;
@@ -381,10 +380,10 @@ export class InlineChatController implements IEditorContribution {
381380

382381
this._zone.value.widget.updateInfo(message);
383382

384-
this._showWidget(!this._session.lastExchange);
383+
this._showWidget(!this._session.chatModel.hasRequests);
385384

386385
this._sessionStore.add(this._editor.onDidChangeModel((e) => {
387-
const msg = this._session?.lastExchange
386+
const msg = this._session?.chatModel.hasRequests
388387
? Message.PAUSE_SESSION // pause when switching models/tabs and when having a previous exchange
389388
: Message.CANCEL_SESSION;
390389
this._log('model changed, pause or cancel session', msg, e);
@@ -520,7 +519,7 @@ export class InlineChatController implements IEditorContribution {
520519

521520
//#endregion ------- DEBT
522521

523-
if (!this._session.lastExchange) {
522+
if (!this._session.chatModel.hasRequests) {
524523
return State.WAIT_FOR_INPUT;
525524
} else if (options.isUnstashed) {
526525
delete options.isUnstashed;
@@ -646,7 +645,7 @@ export class InlineChatController implements IEditorContribution {
646645

647646
private async [State.SHOW_REQUEST](options: InlineChatRunOptions): Promise<State.APPLY_RESPONSE | State.CANCEL | State.PAUSE | State.ACCEPT> {
648647
assertType(this._session);
649-
assertType(this._session.lastInput);
648+
assertType(this._session.chatModel.requestInProgress);
650649

651650
const request: IChatRequestModel | undefined = tail(this._session.chatModel.getRequests());
652651

@@ -789,12 +788,11 @@ export class InlineChatController implements IEditorContribution {
789788
const { response } = this._session.lastExchange!;
790789

791790
let responseTypes: InlineChatResponseTypes | undefined;
792-
for (const { response } of this._session.exchanges) {
793-
794-
const thisType = response instanceof ReplyResponse
795-
? response.responseType
796-
: undefined;
797-
791+
for (const request of this._session.chatModel.getRequests()) {
792+
if (!request.response) {
793+
continue;
794+
}
795+
const thisType = asInlineChatResponseType(request.response.response);
798796
if (responseTypes === undefined) {
799797
responseTypes = thisType;
800798
} else if (responseTypes !== thisType) {
@@ -1144,7 +1142,7 @@ export class InlineChatController implements IEditorContribution {
11441142
}
11451143

11461144
feedbackLast(kind: InlineChatResponseFeedbackKind) {
1147-
if (this._session?.lastExchange && this._session.lastExchange.response instanceof ReplyResponse) {
1145+
if (this._session?.lastExchange?.response instanceof ReplyResponse) {
11481146
this._session.provider.handleInlineChatResponseFeedback?.(this._session.session, this._session.lastExchange.response.raw, kind);
11491147
switch (kind) {
11501148
case InlineChatResponseFeedbackKind.Helpful:
@@ -1167,7 +1165,7 @@ export class InlineChatController implements IEditorContribution {
11671165
}
11681166

11691167
acceptSession(): void {
1170-
if (this._session?.lastExchange && this._session.lastExchange.response instanceof ReplyResponse) {
1168+
if (this._session?.lastExchange?.response instanceof ReplyResponse) {
11711169
this._session.provider.handleInlineChatResponseFeedback?.(this._session.session, this._session.lastExchange.response.raw, InlineChatResponseFeedbackKind.Accepted);
11721170
}
11731171
this._messages.fire(Message.ACCEPT_SESSION);
@@ -1189,7 +1187,7 @@ export class InlineChatController implements IEditorContribution {
11891187
const diff = await this._editorWorkerService.computeDiff(this._session.textModel0.uri, this._session.textModelN.uri, { ignoreTrimWhitespace: false, maxComputationTimeMs: 5000, computeMoves: false }, 'advanced');
11901188
result = this._session.asChangedText(diff?.changes ?? []);
11911189

1192-
if (this._session.lastExchange && this._session.lastExchange.response instanceof ReplyResponse) {
1190+
if (this._session.lastExchange?.response instanceof ReplyResponse) {
11931191
this._session.provider.handleInlineChatResponseFeedback?.(this._session.session, this._session.lastExchange.response.raw, InlineChatResponseFeedbackKind.Undone);
11941192
}
11951193
}
@@ -1253,3 +1251,25 @@ async function sendRequest(accessor: ServicesAccessor, query: string) {
12531251
widget.focusInput();
12541252
widget.acceptInput(query);
12551253
}
1254+
1255+
function asInlineChatResponseType(response: IResponse): InlineChatResponseTypes {
1256+
let result: InlineChatResponseTypes | undefined;
1257+
for (const item of response.value) {
1258+
let thisType: InlineChatResponseTypes;
1259+
switch (item.kind) {
1260+
case 'textEdit':
1261+
thisType = InlineChatResponseTypes.OnlyEdits;
1262+
break;
1263+
case 'markdownContent':
1264+
default:
1265+
thisType = InlineChatResponseTypes.OnlyMessages;
1266+
break;
1267+
}
1268+
if (result === undefined) {
1269+
result = thisType;
1270+
} else if (result !== thisType) {
1271+
return InlineChatResponseTypes.Mixed;
1272+
}
1273+
}
1274+
return result ?? InlineChatResponseTypes.Empty;
1275+
}

src/vs/workbench/contrib/inlineChat/browser/inlineChatSession.ts

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { Emitter, Event } from 'vs/base/common/event';
88
import { ResourceEdit, ResourceFileEdit, ResourceTextEdit } from 'vs/editor/browser/services/bulkEditService';
99
import { IWorkspaceTextEdit, TextEdit, WorkspaceEdit } from 'vs/editor/common/languages';
1010
import { IIdentifiedSingleEditOperation, IModelDecorationOptions, IModelDeltaDecoration, ITextModel, IValidEditOperation, TrackedRangeStickiness } from 'vs/editor/common/model';
11-
import { EditMode, IInlineChatSessionProvider, IInlineChatSession, IInlineChatBulkEditResponse, IInlineChatEditResponse, InlineChatResponseType, InlineChatResponseTypes, CTX_INLINE_CHAT_HAS_STASHED_SESSION } from 'vs/workbench/contrib/inlineChat/common/inlineChat';
11+
import { EditMode, IInlineChatSessionProvider, IInlineChatSession, IInlineChatBulkEditResponse, IInlineChatEditResponse, InlineChatResponseType, CTX_INLINE_CHAT_HAS_STASHED_SESSION } from 'vs/workbench/contrib/inlineChat/common/inlineChat';
1212
import { IRange, Range } from 'vs/editor/common/core/range';
1313
import { ModelDecorationOptions } from 'vs/editor/common/model/textModel';
1414
import { toErrorMessage } from 'vs/base/common/errorMessage';
@@ -212,11 +212,7 @@ export class Session {
212212
this._isUnstashed = false;
213213
const newLen = this._exchange.push(exchange);
214214
this._teldata.rounds += `${newLen}|`;
215-
this._teldata.responseTypes += `${exchange.response instanceof ReplyResponse ? exchange.response.responseType : InlineChatResponseTypes.Empty}|`;
216-
}
217-
218-
get exchanges(): Iterable<SessionExchange> {
219-
return this._exchange;
215+
// this._teldata.responseTypes += `${exchange.response instanceof ReplyResponse ? exchange.response.responseType : InlineChatResponseTypes.Empty}|`;
220216
}
221217

222218
get lastExchange(): SessionExchange | undefined {
@@ -321,7 +317,6 @@ export class ReplyResponse {
321317
readonly untitledTextModel: IUntitledTextEditorModel | undefined;
322318
readonly workspaceEdit: WorkspaceEdit | undefined;
323319

324-
readonly responseType: InlineChatResponseTypes;
325320

326321
constructor(
327322
readonly raw: IInlineChatBulkEditResponse | IInlineChatEditResponse,
@@ -405,19 +400,6 @@ export class ReplyResponse {
405400
}
406401
this.workspaceEdit = { edits: workspaceEdits };
407402
}
408-
409-
410-
const hasEdits = editsMap.size > 0;
411-
const hasMessage = mdContent.value.length > 0;
412-
if (hasEdits && hasMessage) {
413-
this.responseType = InlineChatResponseTypes.Mixed;
414-
} else if (hasEdits) {
415-
this.responseType = InlineChatResponseTypes.OnlyEdits;
416-
} else if (hasMessage) {
417-
this.responseType = InlineChatResponseTypes.OnlyMessages;
418-
} else {
419-
this.responseType = InlineChatResponseTypes.Empty;
420-
}
421403
}
422404
}
423405

0 commit comments

Comments
 (0)