Skip to content

Commit abb831f

Browse files
authored
add IChatModel#initialLocation (microsoft#209930)
This represents the location in which chat started, it also comes with `startSession(location)`. Finally, pass the location along when asking for sample questions
1 parent e2f29f7 commit abb831f

21 files changed

+58
-47
lines changed

src/vs/workbench/api/browser/mainThreadChatAgents2.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ export class MainThreadChatAgents2 extends Disposable implements MainThreadChatA
121121
provideWelcomeMessage: (token: CancellationToken) => {
122122
return this._proxy.$provideWelcomeMessage(handle, token);
123123
},
124-
provideSampleQuestions: (token: CancellationToken) => {
125-
return this._proxy.$provideSampleQuestions(handle, token);
124+
provideSampleQuestions: (location: ChatAgentLocation, token: CancellationToken) => {
125+
return this._proxy.$provideSampleQuestions(handle, location, token);
126126
}
127127
};
128128

src/vs/workbench/api/common/extHost.protocol.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ import * as tasks from 'vs/workbench/api/common/shared/tasks';
5050
import { SaveReason } from 'vs/workbench/common/editor';
5151
import { IRevealOptions, ITreeItem, IViewBadge } from 'vs/workbench/common/views';
5252
import { CallHierarchyItem } from 'vs/workbench/contrib/callHierarchy/common/callHierarchy';
53-
import { IChatAgentMetadata, IChatAgentRequest, IChatAgentResult } from 'vs/workbench/contrib/chat/common/chatAgents';
53+
import { ChatAgentLocation, IChatAgentMetadata, IChatAgentRequest, IChatAgentResult } from 'vs/workbench/contrib/chat/common/chatAgents';
5454
import { IChatProgressResponseContent } from 'vs/workbench/contrib/chat/common/chatModel';
5555
import { IChatFollowup, IChatProgress, IChatResponseErrorDetails, IChatUserActionEvent, InteractiveSessionVoteDirection } from 'vs/workbench/contrib/chat/common/chatService';
5656
import { IChatRequestVariableValue, IChatVariableData, IChatVariableResolverProgress } from 'vs/workbench/contrib/chat/common/chatVariables';
@@ -1246,7 +1246,7 @@ export interface ExtHostChatAgentsShape2 {
12461246
$acceptAction(handle: number, result: IChatAgentResult, action: IChatUserActionEvent): void;
12471247
$invokeCompletionProvider(handle: number, query: string, token: CancellationToken): Promise<IChatAgentCompletionItem[]>;
12481248
$provideWelcomeMessage(handle: number, token: CancellationToken): Promise<(string | IMarkdownString)[] | undefined>;
1249-
$provideSampleQuestions(handle: number, token: CancellationToken): Promise<IChatFollowup[] | undefined>;
1249+
$provideSampleQuestions(handle: number, location: ChatAgentLocation, token: CancellationToken): Promise<IChatFollowup[] | undefined>;
12501250
$releaseSession(sessionId: string): void;
12511251
}
12521252

src/vs/workbench/api/common/extHostChatAgents2.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { ExtHostChatAgentsShape2, IChatAgentCompletionItem, IChatAgentHistoryEnt
2222
import { CommandsConverter, ExtHostCommands } from 'vs/workbench/api/common/extHostCommands';
2323
import * as typeConvert from 'vs/workbench/api/common/extHostTypeConverters';
2424
import * as extHostTypes from 'vs/workbench/api/common/extHostTypes';
25-
import { IChatAgentRequest, IChatAgentResult } from 'vs/workbench/contrib/chat/common/chatAgents';
25+
import { ChatAgentLocation, IChatAgentRequest, IChatAgentResult } from 'vs/workbench/contrib/chat/common/chatAgents';
2626
import { IChatContentReference, IChatFollowup, IChatProgress, IChatUserActionEvent, InteractiveSessionVoteDirection } from 'vs/workbench/contrib/chat/common/chatService';
2727
import { checkProposedApiEnabled, isProposedApiEnabled } from 'vs/workbench/services/extensions/common/extensions';
2828
import { Dto } from 'vs/workbench/services/extensions/common/proxyIdentifier';
@@ -384,13 +384,13 @@ export class ExtHostChatAgents2 implements ExtHostChatAgentsShape2 {
384384
return await agent.provideWelcomeMessage(token);
385385
}
386386

387-
async $provideSampleQuestions(handle: number, token: CancellationToken): Promise<IChatFollowup[] | undefined> {
387+
async $provideSampleQuestions(handle: number, location: ChatAgentLocation, token: CancellationToken): Promise<IChatFollowup[] | undefined> {
388388
const agent = this._agents.get(handle);
389389
if (!agent) {
390390
return;
391391
}
392392

393-
return (await agent.provideSampleQuestions(token))
393+
return (await agent.provideSampleQuestions(typeConvert.ChatLocation.to(location), token))
394394
.map(f => typeConvert.ChatFollowup.from(f, undefined));
395395
}
396396
}
@@ -470,11 +470,11 @@ class ExtHostChatAgent {
470470
});
471471
}
472472

473-
async provideSampleQuestions(token: CancellationToken): Promise<vscode.ChatFollowup[]> {
473+
async provideSampleQuestions(location: vscode.ChatLocation, token: CancellationToken): Promise<vscode.ChatFollowup[]> {
474474
if (!this._welcomeMessageProvider || !this._welcomeMessageProvider.provideSampleQuestions) {
475475
return [];
476476
}
477-
const content = await this._welcomeMessageProvider.provideSampleQuestions(token);
477+
const content = await this._welcomeMessageProvider.provideSampleQuestions(location, token);
478478
if (!content) {
479479
return [];
480480
}

src/vs/workbench/contrib/chat/browser/chatEditorInput.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { registerIcon } from 'vs/platform/theme/common/iconRegistry';
1616
import { EditorInputCapabilities, IEditorSerializer, IUntypedEditorInput } from 'vs/workbench/common/editor';
1717
import { EditorInput } from 'vs/workbench/common/editor/editorInput';
1818
import type { IChatEditorOptions } from 'vs/workbench/contrib/chat/browser/chatEditor';
19+
import { ChatAgentLocation } from 'vs/workbench/contrib/chat/common/chatAgents';
1920
import { IChatModel } from 'vs/workbench/contrib/chat/common/chatModel';
2021
import { IChatService } from 'vs/workbench/contrib/chat/common/chatService';
2122

@@ -94,7 +95,7 @@ export class ChatEditorInput extends EditorInput {
9495
if (typeof this.sessionId === 'string') {
9596
this.model = this.chatService.getOrRestoreSession(this.sessionId);
9697
} else if (!this.options.target) {
97-
this.model = this.chatService.startSession(CancellationToken.None);
98+
this.model = this.chatService.startSession(ChatAgentLocation.Panel, CancellationToken.None);
9899
} else if ('data' in this.options.target) {
99100
this.model = this.chatService.loadSessionFromContent(this.options.target.data);
100101
}

src/vs/workbench/contrib/chat/browser/chatQuick.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ class QuickChat extends Disposable {
314314
}
315315

316316
private updateModel(): void {
317-
this.model ??= this.chatService.startSession(CancellationToken.None);
317+
this.model ??= this.chatService.startSession(ChatAgentLocation.Panel, CancellationToken.None);
318318
if (!this.model) {
319319
throw new Error('Could not start chat session');
320320
}

src/vs/workbench/contrib/chat/browser/chatViewPane.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export class ChatViewPane extends ViewPane implements IChatViewPane {
105105

106106
model = model ?? (this.chatService.transferredSessionData?.sessionId
107107
? this.chatService.getOrRestoreSession(this.chatService.transferredSessionData.sessionId)
108-
: this.chatService.startSession(CancellationToken.None));
108+
: this.chatService.startSession(ChatAgentLocation.Panel, CancellationToken.None));
109109
if (!model) {
110110
throw new Error('Could not start chat session');
111111
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export interface IChatAgentImplementation {
6363
invoke(request: IChatAgentRequest, progress: (part: IChatProgress) => void, history: IChatAgentHistoryEntry[], token: CancellationToken): Promise<IChatAgentResult>;
6464
provideFollowups?(request: IChatAgentRequest, result: IChatAgentResult, history: IChatAgentHistoryEntry[], token: CancellationToken): Promise<IChatFollowup[]>;
6565
provideWelcomeMessage?(token: CancellationToken): ProviderResult<(string | IMarkdownString)[] | undefined>;
66-
provideSampleQuestions?(token: CancellationToken): ProviderResult<IChatFollowup[] | undefined>;
66+
provideSampleQuestions?(location: ChatAgentLocation, token: CancellationToken): ProviderResult<IChatFollowup[] | undefined>;
6767
}
6868

6969
export type IChatAgent = IChatAgentData & IChatAgentImplementation;
@@ -341,9 +341,9 @@ export class MergedChatAgent implements IChatAgent {
341341
return undefined;
342342
}
343343

344-
provideSampleQuestions(token: CancellationToken): ProviderResult<IChatFollowup[] | undefined> {
344+
provideSampleQuestions(location: ChatAgentLocation, token: CancellationToken): ProviderResult<IChatFollowup[] | undefined> {
345345
if (this.impl.provideSampleQuestions) {
346-
return this.impl.provideSampleQuestions(token);
346+
return this.impl.provideSampleQuestions(location, token);
347347
}
348348

349349
return undefined;

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ export interface IChatModel {
394394
readonly onDidChange: Event<IChatChangeEvent>;
395395
readonly sessionId: string;
396396
readonly initState: ChatModelInitState;
397+
readonly initialLocation: ChatAgentLocation;
397398
readonly title: string;
398399
readonly welcomeMessage: IChatWelcomeMessageModel | undefined;
399400
readonly requestInProgress: boolean;
@@ -427,6 +428,7 @@ export interface ISerializableChatRequestData {
427428
}
428429

429430
export interface IExportableChatData {
431+
initialLocation: ChatAgentLocation | undefined;
430432
welcomeMessage: (string | IChatFollowup[])[] | undefined;
431433
requests: ISerializableChatRequestData[];
432434
requesterUsername: string;
@@ -573,8 +575,13 @@ export class ChatModel extends Disposable implements IChatModel {
573575
return ChatModel.getDefaultTitle(this._requests);
574576
}
575577

578+
get initialLocation() {
579+
return this._initialLocation;
580+
}
581+
576582
constructor(
577583
private readonly initialData: ISerializableChatData | IExportableChatData | undefined,
584+
private readonly _initialLocation: ChatAgentLocation,
578585
@ILogService private readonly logService: ILogService,
579586
@IChatAgentService private readonly chatAgentService: IChatAgentService,
580587
@IInstantiationService private readonly instantiationService: IInstantiationService,
@@ -790,6 +797,7 @@ export class ChatModel extends Disposable implements IChatModel {
790797
requesterAvatarIconUri: this.requesterAvatarIconUri,
791798
responderUsername: this.responderUsername,
792799
responderAvatarIconUri: this.responderAvatarIcon,
800+
initialLocation: this.initialLocation,
793801
welcomeMessage: this._welcomeMessage?.content.map(c => {
794802
if (Array.isArray(c)) {
795803
return c;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ export interface IChatService {
279279

280280
isEnabled(location: ChatAgentLocation): boolean;
281281
hasSessions(): boolean;
282-
startSession(token: CancellationToken): ChatModel | undefined;
282+
startSession(location: ChatAgentLocation, token: CancellationToken): ChatModel | undefined;
283283
getSession(sessionId: string): IChatModel | undefined;
284284
getOrRestoreSession(sessionId: string): IChatModel | undefined;
285285
loadSessionFromContent(data: IExportableChatData | ISerializableChatData): IChatModel | undefined;

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -319,13 +319,13 @@ export class ChatService extends Disposable implements IChatService {
319319
this.saveState();
320320
}
321321

322-
startSession(token: CancellationToken): ChatModel {
322+
startSession(location: ChatAgentLocation, token: CancellationToken): ChatModel {
323323
this.trace('startSession');
324-
return this._startSession(undefined, token);
324+
return this._startSession(undefined, location, token);
325325
}
326326

327-
private _startSession(someSessionHistory: IExportableChatData | ISerializableChatData | undefined, token: CancellationToken): ChatModel {
328-
const model = this.instantiationService.createInstance(ChatModel, someSessionHistory);
327+
private _startSession(someSessionHistory: IExportableChatData | ISerializableChatData | undefined, location: ChatAgentLocation, token: CancellationToken): ChatModel {
328+
const model = this.instantiationService.createInstance(ChatModel, someSessionHistory, location);
329329
this._sessionModels.set(model.sessionId, model);
330330
this.initializeSession(model, token);
331331
return model;
@@ -343,14 +343,14 @@ export class ChatService extends Disposable implements IChatService {
343343
model.startInitialize();
344344

345345
await this.extensionService.whenInstalledExtensionsRegistered();
346-
const defaultAgentData = this.chatAgentService.getContributedDefaultAgent(ChatAgentLocation.Panel);
346+
const defaultAgentData = this.chatAgentService.getContributedDefaultAgent(model.initialLocation);
347347
if (!defaultAgentData) {
348348
throw new ErrorNoTelemetry('No default agent contributed');
349349
}
350350

351351
await this.extensionService.activateByEvent(`onChatParticipant:${defaultAgentData.id}`);
352352

353-
const defaultAgent = this.chatAgentService.getDefaultAgent(ChatAgentLocation.Panel);
353+
const defaultAgent = this.chatAgentService.getDefaultAgent(model.initialLocation);
354354
if (!defaultAgent) {
355355
// Should have been registered during activation above!
356356
throw new ErrorNoTelemetry('No default agent registered');
@@ -359,7 +359,7 @@ export class ChatService extends Disposable implements IChatService {
359359
const welcomeModel = welcomeMessage && this.instantiationService.createInstance(
360360
ChatWelcomeMessageModel,
361361
welcomeMessage.map(item => typeof item === 'string' ? new MarkdownString(item) : item),
362-
await defaultAgent.provideSampleQuestions?.(token) ?? []
362+
await defaultAgent.provideSampleQuestions?.(model.initialLocation, token) ?? []
363363
);
364364

365365
model.initialize(welcomeModel);
@@ -391,11 +391,11 @@ export class ChatService extends Disposable implements IChatService {
391391
this._transferredSessionData = undefined;
392392
}
393393

394-
return this._startSession(sessionData, CancellationToken.None);
394+
return this._startSession(sessionData, sessionData.initialLocation ?? ChatAgentLocation.Panel, CancellationToken.None);
395395
}
396396

397397
loadSessionFromContent(data: IExportableChatData | ISerializableChatData): IChatModel | undefined {
398-
return this._startSession(data, CancellationToken.None);
398+
return this._startSession(data, data.initialLocation ?? ChatAgentLocation.Panel, CancellationToken.None);
399399
}
400400

401401
async resendRequest(request: IChatRequestModel, options?: IChatSendRequestOptions): Promise<void> {
@@ -425,10 +425,6 @@ export class ChatService extends Disposable implements IChatService {
425425

426426
async sendRequest(sessionId: string, request: string, options?: IChatSendRequestOptions): Promise<IChatSendRequestData | undefined> {
427427

428-
const location = options?.location ?? ChatAgentLocation.Panel;
429-
const attempt = options?.attempt ?? 0;
430-
const implicitVariablesEnabled = options?.implicitVariablesEnabled ?? false;
431-
432428
this.trace('sendRequest', `sessionId: ${sessionId}, message: ${request.substring(0, 20)}${request.length > 20 ? '[...]' : ''}}`);
433429
if (!request.trim()) {
434430
this.trace('sendRequest', 'Rejected empty message');
@@ -447,6 +443,9 @@ export class ChatService extends Disposable implements IChatService {
447443
return;
448444
}
449445

446+
const location = options?.location ?? model.initialLocation;
447+
const attempt = options?.attempt ?? 0;
448+
const implicitVariablesEnabled = options?.implicitVariablesEnabled ?? false;
450449
const defaultAgent = this.chatAgentService.getDefaultAgent(location)!;
451450

452451
const parsedRequest = this.instantiationService.createInstance(ChatRequestParser).parseChatRequest(sessionId, request, location, options?.parserContext);

0 commit comments

Comments
 (0)