Skip to content

Commit eb736c1

Browse files
authored
Limit chat history to messages with the current participant (microsoft#209898)
* Limit chat history to messages with the current participant * Fix build error * Fix build for real
1 parent 923c0a8 commit eb736c1

File tree

5 files changed

+40
-12
lines changed

5 files changed

+40
-12
lines changed

extensions/vscode-api-tests/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@
7575
"description": "Hello"
7676
}
7777
]
78+
},
79+
{
80+
"id": "api-test.participant2",
81+
"name": "participant2",
82+
"description": "test",
83+
"commands": [ ]
7884
}
7985
],
8086
"configuration": {

extensions/vscode-api-tests/src/singlefolder-tests/chat.test.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import * as assert from 'assert';
77
import 'mocha';
88
import { ChatContext, ChatRequest, ChatResult, ChatVariableLevel, Disposable, Event, EventEmitter, chat, commands } from 'vscode';
9-
import { DeferredPromise, assertNoRpc, closeAllEditors, disposeAll } from '../utils';
9+
import { DeferredPromise, asPromise, assertNoRpc, closeAllEditors, disposeAll } from '../utils';
1010

1111
suite('chat', () => {
1212

@@ -28,11 +28,12 @@ suite('chat', () => {
2828
return deferred;
2929
}
3030

31-
function setupParticipant(): Event<{ request: ChatRequest; context: ChatContext }> {
31+
function setupParticipant(second?: boolean): Event<{ request: ChatRequest; context: ChatContext }> {
3232
const emitter = new EventEmitter<{ request: ChatRequest; context: ChatContext }>();
3333
disposables.push(emitter);
3434

35-
const participant = chat.createChatParticipant('api-test.participant', (request, context, _progress, _token) => {
35+
const id = second ? 'api-test.participant2' : 'api-test.participant';
36+
const participant = chat.createChatParticipant(id, (request, context, _progress, _token) => {
3637
emitter.fire({ request, context });
3738
});
3839
participant.isDefault = true;
@@ -101,4 +102,25 @@ suite('chat', () => {
101102
const result = await deferred.p;
102103
assert.deepStrictEqual(result.metadata, { key: 'value' });
103104
});
105+
106+
test('isolated participant history', async () => {
107+
const onRequest = setupParticipant();
108+
const onRequest2 = setupParticipant(true);
109+
110+
commands.executeCommand('workbench.action.chat.open', { query: '@participant hi' });
111+
await asPromise(onRequest);
112+
113+
// Request is still being handled at this point, wait for it to end
114+
setTimeout(() => {
115+
commands.executeCommand('workbench.action.chat.open', { query: '@participant2 hi' });
116+
}, 0);
117+
const request2 = await asPromise(onRequest2);
118+
assert.strictEqual(request2.context.history.length, 0);
119+
120+
setTimeout(() => {
121+
commands.executeCommand('workbench.action.chat.open', { query: '@participant2 hi' });
122+
}, 0);
123+
const request3 = await asPromise(onRequest2);
124+
assert.strictEqual(request3.context.history.length, 2); // request + response = 2
125+
});
104126
});

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -894,13 +894,19 @@ export class ChatWelcomeMessageModel implements IChatWelcomeMessageModel {
894894
}
895895
}
896896

897-
export function getHistoryEntriesFromModel(model: IChatModel): IChatAgentHistoryEntry[] {
897+
export function getHistoryEntriesFromModel(model: IChatModel, forAgentId: string | undefined): IChatAgentHistoryEntry[] {
898898
const history: IChatAgentHistoryEntry[] = [];
899899
for (const request of model.getRequests()) {
900900
if (!request.response) {
901901
continue;
902902
}
903903

904+
if (forAgentId && forAgentId !== request.response.agent?.id) {
905+
// An agent only gets to see requests that were sent to this agent.
906+
// The default agent (the undefined case) gets to see all of them.
907+
continue;
908+
}
909+
904910
const promptTextResult = getPromptText(request.message);
905911
const historyRequest: IChatAgentRequest = {
906912
sessionId: model.sessionId,

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -331,12 +331,6 @@ export class ChatService extends Disposable implements IChatService {
331331
return model;
332332
}
333333

334-
// TODO, when default agent shows up?
335-
// private reinitializeModel(model: ChatModel): void {
336-
// this.trace('reinitializeModel', `Start reinit`);
337-
// this.initializeSession(model, CancellationToken.None);
338-
// }
339-
340334
private async initializeSession(model: ChatModel, token: CancellationToken): Promise<void> {
341335
try {
342336
this.trace('initializeSession', `Initialize session ${model.sessionId}`);
@@ -521,7 +515,7 @@ export class ChatService extends Disposable implements IChatService {
521515
if (agentPart || (defaultAgent && !commandPart)) {
522516
const agent = (agentPart?.agent ?? defaultAgent)!;
523517
await this.extensionService.activateByEvent(`onChatParticipant:${agent.id}`);
524-
const history = getHistoryEntriesFromModel(model);
518+
const history = getHistoryEntriesFromModel(model, agentPart?.agent.id);
525519

526520
const initVariableData: IChatRequestVariableData = { variables: [] };
527521
request = model.addRequest(parsedRequest, initVariableData, attempt, agent, agentSlashCommandPart?.command);

src/vs/workbench/contrib/terminalContrib/chat/browser/terminalChatController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ export class TerminalChatController extends Disposable implements ITerminalContr
301301
location: ChatAgentLocation.Terminal
302302
};
303303
try {
304-
const task = this._chatAgentService.invokeAgent(this._terminalAgentId!, requestProps, progressCallback, getHistoryEntriesFromModel(model), cancellationToken);
304+
const task = this._chatAgentService.invokeAgent(this._terminalAgentId!, requestProps, progressCallback, getHistoryEntriesFromModel(model, this._terminalAgentId!), cancellationToken);
305305
this._chatWidget?.value.inlineChatWidget.updateChatMessage(undefined);
306306
this._chatWidget?.value.inlineChatWidget.updateFollowUps(undefined);
307307
this._chatWidget?.value.inlineChatWidget.updateProgress(true);

0 commit comments

Comments
 (0)