Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/prompts/node/agent/agentConversationHistory.tsx
13405 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { AssistantMessage, BasePromptElementProps, Chunk, PrioritizedList, PromptElement, PromptSizing, UserMessage } from '@vscode/prompt-tsx';
7
import { IResultMetadata, Turn } from '../../../prompt/common/conversation';
8
import { IBuildPromptContext } from '../../../prompt/common/intents';
9
import { Tag } from '../base/tag';
10
import { ChatVariables } from '../panel/chatVariables';
11
import { ChatToolCalls } from '../panel/toolCalling';
12
import { EditedFileEvents, renderedMessageToTsxChildren } from './agentPrompt';
13
14
export interface AgentUserMessageInHistoryProps extends BasePromptElementProps {
15
readonly turn: Turn;
16
}
17
18
export class AgentUserMessageInHistory extends PromptElement<AgentUserMessageInHistoryProps> {
19
constructor(
20
props: AgentUserMessageInHistoryProps,
21
) {
22
super(props);
23
}
24
25
override async render(state: void, sizing: PromptSizing) {
26
const turn = this.props.turn;
27
return <UserMessage>
28
{turn.promptVariables && <ChatVariables flexGrow={1} priority={898} chatVariables={turn.promptVariables} isAgent={true} omitReferences />}
29
{turn.editedFileEvents?.length &&
30
<Tag name='context'>
31
<EditedFileEvents flexGrow={2} editedFileEvents={turn.editedFileEvents} />
32
</Tag>}
33
<Tag name='userRequest'>{turn.request.message}</Tag>
34
</UserMessage>;
35
}
36
}
37
38
export interface AgentConversationHistoryProps extends BasePromptElementProps {
39
readonly priority: number;
40
readonly promptContext: IBuildPromptContext;
41
}
42
43
/**
44
* Agent conversation history for when summarization/cache breakpoints are disabled.
45
*/
46
export class AgentConversationHistory extends PromptElement<AgentConversationHistoryProps> {
47
override async render(state: void, sizing: PromptSizing) {
48
const history: PromptElement[] = [];
49
const contextHistory = this.props.promptContext.history;
50
for (const [i, turn] of contextHistory.entries()) {
51
const metadata = turn.responseChatResult?.metadata as IResultMetadata | undefined;
52
53
if (metadata?.renderedUserMessage) {
54
history.push(<UserMessage><Chunk>{renderedMessageToTsxChildren(metadata.renderedUserMessage, false)}</Chunk></UserMessage>);
55
} else {
56
history.push(<AgentUserMessageInHistory turn={turn} />);
57
}
58
59
if (Array.isArray(metadata?.toolCallRounds) && metadata.toolCallRounds?.length > 0) {
60
// If a tool call limit is exceeded, the tool call from this turn will
61
// have been aborted and any result should be found in the next turn.
62
const toolCallResultInNextTurn = metadata.maxToolCallsExceeded;
63
let toolCallResults = metadata.toolCallResults;
64
if (toolCallResultInNextTurn) {
65
const nextMetadata = contextHistory.at(i + 1)?.responseChatResult?.metadata as IResultMetadata | undefined;
66
const mergeFrom = i === contextHistory.length - 1 ? this.props.promptContext.toolCallResults : nextMetadata?.toolCallResults;
67
toolCallResults = { ...toolCallResults, ...mergeFrom };
68
}
69
70
history.push(<ChatToolCalls
71
promptContext={this.props.promptContext}
72
toolCallRounds={metadata.toolCallRounds}
73
toolCallResults={toolCallResults}
74
isHistorical={!(toolCallResultInNextTurn && i === contextHistory.length - 1)}
75
/>);
76
} else if (turn.responseMessage) {
77
history.push(<AssistantMessage>{turn.responseMessage?.message}</AssistantMessage>);
78
}
79
}
80
81
return (<PrioritizedList priority={this.props.priority} descending={false}>{history}</PrioritizedList>);
82
}
83
}
84