Skip to content

Commit 80ef8fe

Browse files
authored
fix: freeze chat history before doing intent detection (microsoft#227028)
* fix: freeze chat history before doing intent detection * fix: restore title provider dependence on chat request already being added
1 parent 4ab6e5d commit 80ef8fe

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@ export class ChatService extends Disposable implements IChatService {
545545
const agentPart = 'kind' in parsedRequest ? undefined : parsedRequest.parts.find((r): r is ChatRequestAgentPart => r instanceof ChatRequestAgentPart);
546546
const agentSlashCommandPart = 'kind' in parsedRequest ? undefined : parsedRequest.parts.find((r): r is ChatRequestAgentSubcommandPart => r instanceof ChatRequestAgentSubcommandPart);
547547
const commandPart = 'kind' in parsedRequest ? undefined : parsedRequest.parts.find((r): r is ChatRequestSlashCommandPart => r instanceof ChatRequestSlashCommandPart);
548+
const requests = [...model.getRequests()];
548549

549550
let gotProgress = false;
550551
const requestType = commandPart ? 'slashCommand' : 'string';
@@ -638,7 +639,7 @@ export class ChatService extends Disposable implements IChatService {
638639

639640
if (this.configurationService.getValue('chat.experimental.detectParticipant.enabled') !== false && this.chatAgentService.hasChatParticipantDetectionProviders() && !agentPart && !commandPart && enableCommandDetection) {
640641
// We have no agent or command to scope history with, pass the full history to the participant detection provider
641-
const defaultAgentHistory = this.getHistoryEntriesFromModel(model, location, defaultAgent.id);
642+
const defaultAgentHistory = this.getHistoryEntriesFromModel(requests, model.sessionId, location, defaultAgent.id);
642643

643644
// Prepare the request object that we will send to the participant detection provider
644645
const chatAgentRequest = await prepareChatAgentRequest(defaultAgent, agentSlashCommandPart?.command, enableCommandDetection, undefined, false);
@@ -657,7 +658,7 @@ export class ChatService extends Disposable implements IChatService {
657658
await this.extensionService.activateByEvent(`onChatParticipant:${agent.id}`);
658659

659660
// Recompute history in case the agent or command changed
660-
const history = this.getHistoryEntriesFromModel(model, location, agent.id);
661+
const history = this.getHistoryEntriesFromModel(requests, model.sessionId, location, agent.id);
661662
const requestProps = await prepareChatAgentRequest(agent, command, enableCommandDetection, request /* Reuse the request object if we already created it for participant detection */, !!detectedAgent);
662663
const pendingRequest = this._pendingRequests.get(sessionId);
663664
if (pendingRequest && !pendingRequest.requestId) {
@@ -667,7 +668,7 @@ export class ChatService extends Disposable implements IChatService {
667668
const agentResult = await this.chatAgentService.invokeAgent(agent.id, requestProps, progressCallback, history, token);
668669
rawResult = agentResult;
669670
agentOrCommandFollowups = this.chatAgentService.getFollowups(agent.id, requestProps, agentResult, history, followupsCancelToken);
670-
chatTitlePromise = model.getRequests().length === 1 && !model.customTitle ? this.chatAgentService.getChatTitle(defaultAgent.id, this.getHistoryEntriesFromModel(model, location, agent.id), CancellationToken.None) : undefined;
671+
chatTitlePromise = model.getRequests().length === 1 && !model.customTitle ? this.chatAgentService.getChatTitle(defaultAgent.id, this.getHistoryEntriesFromModel(model.getRequests(), model.sessionId, location, agent.id), CancellationToken.None) : undefined;
671672
} else if (commandPart && this.chatSlashCommandService.hasCommand(commandPart.slashCommand.command)) {
672673
request = model.addRequest(parsedRequest, { variables: [] }, attempt);
673674
completeResponseCreated();
@@ -774,9 +775,9 @@ export class ChatService extends Disposable implements IChatService {
774775
};
775776
}
776777

777-
private getHistoryEntriesFromModel(model: IChatModel, location: ChatAgentLocation, forAgentId: string): IChatAgentHistoryEntry[] {
778+
private getHistoryEntriesFromModel(requests: IChatRequestModel[], sessionId: string, location: ChatAgentLocation, forAgentId: string): IChatAgentHistoryEntry[] {
778779
const history: IChatAgentHistoryEntry[] = [];
779-
for (const request of model.getRequests()) {
780+
for (const request of requests) {
780781
if (!request.response) {
781782
continue;
782783
}
@@ -790,7 +791,7 @@ export class ChatService extends Disposable implements IChatService {
790791

791792
const promptTextResult = getPromptText(request.message);
792793
const historyRequest: IChatAgentRequest = {
793-
sessionId: model.sessionId,
794+
sessionId: sessionId,
794795
requestId: request.id,
795796
agentId: request.response.agent?.id ?? '',
796797
message: promptTextResult.message,

0 commit comments

Comments
 (0)