Skip to content

Commit 03a0165

Browse files
authored
Fix #new (#1042)
Fix #265380
1 parent e6cd264 commit 03a0165

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

src/extension/prompt/common/conversation.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import { Location, Range } from '../../../vscodeTypes';
1515
import { InternalToolReference, IToolCallRound } from '../common/intents';
1616
import { ChatVariablesCollection } from './chatVariablesCollection';
1717
import { ToolCallRound } from './toolCallRound';
18+
import { ServicesAccessor } from '../../../util/vs/platform/instantiation/common/instantiation';
19+
import { IWorkspaceService } from '../../../platform/workspace/common/workspaceService';
1820
export { PromptReference } from '@vscode/prompt-tsx';
1921

2022
export enum TurnStatus {
@@ -331,6 +333,7 @@ export interface IResultMetadata {
331333
/** The user message exactly as it must be rendered in history. Should not be optional, but not every prompt will adopt this immediately */
332334
renderedUserMessage?: Raw.ChatCompletionContentPart[];
333335
renderedGlobalContext?: Raw.ChatCompletionContentPart[];
336+
globalContextCacheKey?: string;
334337
command?: string;
335338
filterCategory?: FilterReason;
336339

@@ -363,5 +366,11 @@ export class RenderedUserMessageMetadata {
363366
export class GlobalContextMessageMetadata {
364367
constructor(
365368
readonly renderedGlobalContext: Raw.ChatCompletionContentPart[],
369+
readonly cacheKey: string
366370
) { }
367371
}
372+
373+
export function getGlobalContextCacheKey(accessor: ServicesAccessor): string {
374+
const workspaceService = accessor.get(IWorkspaceService);
375+
return workspaceService.getWorkspaceFolders().map(folder => folder.toString()).join(',');
376+
}

src/extension/prompt/node/chatParticipantRequestHandler.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import { IIntentService } from '../../intents/node/intentService';
3535
import { UnknownIntent } from '../../intents/node/unknownIntent';
3636
import { ContributedToolName } from '../../tools/common/toolNames';
3737
import { ChatVariablesCollection } from '../common/chatVariablesCollection';
38-
import { Conversation, GlobalContextMessageMetadata, ICopilotChatResult, ICopilotChatResultIn, normalizeSummariesOnRounds, RenderedUserMessageMetadata, Turn, TurnStatus } from '../common/conversation';
38+
import { Conversation, getGlobalContextCacheKey, GlobalContextMessageMetadata, ICopilotChatResult, ICopilotChatResultIn, normalizeSummariesOnRounds, RenderedUserMessageMetadata, Turn, TurnStatus } from '../common/conversation';
3939
import { InternalToolReference } from '../common/intents';
4040
import { ChatTelemetryBuilder } from './chatParticipantTelemetry';
4141
import { DefaultIntentRequestHandler } from './defaultIntentRequestHandler';
@@ -325,9 +325,7 @@ export class ChatParticipantRequestHandler {
325325

326326

327327
export function addHistoryToConversation(accessor: ServicesAccessor, history: ReadonlyArray<ChatRequestTurn | ChatResponseTurn>): { turns: Turn[]; sessionId: string | undefined } {
328-
const commandService = accessor.get(ICommandService);
329-
const conversationStore = accessor.get(IConversationStore);
330-
const workspaceService = accessor.get(IWorkspaceService);
328+
const instaService = accessor.get(IInstantiationService);
331329

332330
const turns: Turn[] = [];
333331
let sessionId: string | undefined;
@@ -339,12 +337,12 @@ export function addHistoryToConversation(accessor: ServicesAccessor, history: Re
339337
if (entry instanceof ChatRequestTurn) {
340338
previousChatRequestTurn = entry;
341339
} else {
342-
const existingTurn = findExistingTurnFromVSCodeChatHistoryTurn(conversationStore, entry);
340+
const existingTurn = instaService.invokeFunction(findExistingTurnFromVSCodeChatHistoryTurn, entry);
343341
if (existingTurn) {
344342
turns.push(existingTurn);
345343
} else {
346344
if (previousChatRequestTurn) {
347-
const deserializedTurn = createTurnFromVSCodeChatHistoryTurns(previousChatRequestTurn, entry, commandService, workspaceService);
345+
const deserializedTurn = instaService.invokeFunction(createTurnFromVSCodeChatHistoryTurns, previousChatRequestTurn, entry);
348346
previousChatRequestTurn = undefined;
349347
turns.push(deserializedTurn);
350348
}
@@ -363,7 +361,8 @@ export function addHistoryToConversation(accessor: ServicesAccessor, history: Re
363361
/**
364362
* Try to find an existing `Turn` instance that we created previously based on the responseId of a vscode turn.
365363
*/
366-
function findExistingTurnFromVSCodeChatHistoryTurn(conversationStore: IConversationStore, turn: ChatRequestTurn | ChatResponseTurn): Turn | undefined {
364+
function findExistingTurnFromVSCodeChatHistoryTurn(accessor: ServicesAccessor, turn: ChatRequestTurn | ChatResponseTurn): Turn | undefined {
365+
const conversationStore = accessor.get(IConversationStore);
367366
const responseId = getResponseIdFromVSCodeChatHistoryTurn(turn);
368367
const conversation = responseId ? conversationStore.getConversation(responseId) : undefined;
369368
return conversation?.turns.find(turn => turn.id === responseId);
@@ -381,11 +380,14 @@ function getResponseIdFromVSCodeChatHistoryTurn(turn: ChatRequestTurn | ChatResp
381380
* Try as best as possible to create a `Turn` object from data that comes from vscode.
382381
*/
383382
function createTurnFromVSCodeChatHistoryTurns(
383+
accessor: ServicesAccessor,
384384
chatRequestTurn: ChatRequestTurn2,
385-
chatResponseTurn: ChatResponseTurn,
386-
commandService: ICommandService,
387-
workspaceService: IWorkspaceService
385+
chatResponseTurn: ChatResponseTurn
388386
): Turn {
387+
const commandService = accessor.get(ICommandService);
388+
const workspaceService = accessor.get(IWorkspaceService);
389+
const instaService = accessor.get(IInstantiationService);
390+
389391
const currentTurn = new Turn(
390392
undefined,
391393
{ message: chatRequestTurn.prompt, type: 'user' },
@@ -428,7 +430,8 @@ function createTurnFromVSCodeChatHistoryTurns(
428430
currentTurn.setResponse(status, { message: content, type: 'model', name: command?.commandId || UnknownIntent.ID }, undefined, chatResponseTurn.result);
429431
const turnMetadata = (chatResponseTurn.result as ICopilotChatResultIn).metadata;
430432
if (turnMetadata?.renderedGlobalContext) {
431-
currentTurn.setMetadata(new GlobalContextMessageMetadata(turnMetadata?.renderedGlobalContext));
433+
const cacheKey = turnMetadata.globalContextCacheKey ?? instaService.invokeFunction(getGlobalContextCacheKey);
434+
currentTurn.setMetadata(new GlobalContextMessageMetadata(turnMetadata?.renderedGlobalContext, cacheKey));
432435
}
433436
if (turnMetadata?.renderedUserMessage) {
434437
currentTurn.setMetadata(new RenderedUserMessageMetadata(turnMetadata.renderedUserMessage));

src/extension/prompts/node/agent/agentPrompt.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { ChatRequestEditedFileEventKind, Position, Range } from '../../../../vsc
2626
import { GenericBasePromptElementProps } from '../../../context/node/resolvers/genericPanelIntentInvocation';
2727
import { GitHubPullRequestProviders } from '../../../conversation/node/githubPullRequestProviders';
2828
import { ChatVariablesCollection } from '../../../prompt/common/chatVariablesCollection';
29-
import { GlobalContextMessageMetadata, RenderedUserMessageMetadata, Turn } from '../../../prompt/common/conversation';
29+
import { getGlobalContextCacheKey, GlobalContextMessageMetadata, RenderedUserMessageMetadata, Turn } from '../../../prompt/common/conversation';
3030
import { InternalToolReference } from '../../../prompt/common/intents';
3131
import { IPromptVariablesService } from '../../../prompt/node/promptVariablesService';
3232
import { ToolName } from '../../../tools/common/toolNames';
@@ -234,14 +234,17 @@ export class AgentPrompt extends PromptElement<AgentPromptProps> {
234234
if (firstTurn) {
235235
const metadata = firstTurn.getMetadata(GlobalContextMessageMetadata);
236236
if (metadata) {
237-
return metadata.renderedGlobalContext;
237+
const currentCacheKey = this.instantiationService.invokeFunction(getGlobalContextCacheKey);
238+
if (metadata.cacheKey === currentCacheKey) {
239+
return metadata.renderedGlobalContext;
240+
}
238241
}
239242
}
240243

241244
const rendered = await renderPromptElement(this.instantiationService, endpoint, GlobalAgentContext, { enableCacheBreakpoints: this.props.enableCacheBreakpoints, availableTools: this.props.promptContext.tools?.availableTools }, undefined, undefined);
242245
const msg = rendered.messages.at(0)?.content;
243246
if (msg) {
244-
firstTurn?.setMetadata(new GlobalContextMessageMetadata(msg));
247+
firstTurn?.setMetadata(new GlobalContextMessageMetadata(msg, this.instantiationService.invokeFunction(getGlobalContextCacheKey)));
245248
return msg;
246249
}
247250
}

0 commit comments

Comments
 (0)