Elffuss BraidHub

kikocis/continue public

2d2c10bf01a5 · 4.2 KiBRawPermalink
1import {
2 ChatHistoryItem,
3 ChatMessage,
4 RuleWithSource,
5 TextMessagePart,
6 ToolResultChatMessage,
7 UserChatMessage,
8} from "../";
9import { findLast } from "../util/findLast";
10import { normalizeToMessageParts } from "../util/messageContent";
11import { isUserOrToolMsg } from "./messages";
12import { getSystemMessageWithRules } from "./rules/getSystemMessageWithRules";
13
14export const DEFAULT_CHAT_SYSTEM_MESSAGE_URL =
15 "https://github.com/continuedev/continue/blob/main/core/llm/constructMessages.ts";
16
17export const DEFAULT_AGENT_SYSTEM_MESSAGE_URL =
18 "https://github.com/continuedev/continue/blob/main/core/llm/constructMessages.ts";
19
20const EDIT_MESSAGE = `\
21 Always include the language and file name in the info string when you write code blocks.
22 If you are editing "src/main.py" for example, your code block should start with '\`\`\`python src/main.py'
23
24 When addressing code modification requests, present a concise code snippet that
25 emphasizes only the necessary changes and uses abbreviated placeholders for
26 unmodified sections. For example:
27
28 \`\`\`language /path/to/file
29 // ... existing code ...
30
31 {{ modified code here }}
32
33 // ... existing code ...
34
35 {{ another modification }}
36
37 // ... rest of code ...
38 \`\`\`
39
40 In existing files, you should always restate the function or class that the snippet belongs to:
41
42 \`\`\`language /path/to/file
43 // ... existing code ...
44
45 function exampleFunction() {
46 // ... existing code ...
47
48 {{ modified code here }}
49
50 // ... rest of function ...
51 }
52
53 // ... rest of code ...
54 \`\`\`
55
56 Since users have access to their complete file, they prefer reading only the
57 relevant modifications. It's perfectly acceptable to omit unmodified portions
58 at the beginning, middle, or end of files using these "lazy" comments. Only
59 provide the complete file when explicitly requested. Include a concise explanation
60 of changes unless the user specifically asks for code only.
61`
62
63export const DEFAULT_CHAT_SYSTEM_MESSAGE = `\
64<important_rules>
65 You are in chat mode.
66
67 If the user asks to make changes to files offer that they can use the Apply Button on the code block, or switch to Agent Mode to make the suggested updates automatically.
68 If needed consisely explain to the user they can switch to agent mode using the Mode Selector dropdown and provide no other details.
69
70${EDIT_MESSAGE}
71</important_rules>`;
72
73export const DEFAULT_AGENT_SYSTEM_MESSAGE = `\
74<important_rules>
75 You are in agent mode.
76
77${EDIT_MESSAGE}
78</important_rules>`;
79
80export function constructMessages(
81 messageMode: string,
82 history: ChatHistoryItem[],
83 baseChatOrAgentSystemMessage: string | undefined,
84 rules: RuleWithSource[],
85): ChatMessage[] {
86 const filteredHistory = history.filter(
87 (item) => item.message.role !== "system",
88 );
89 const msgs: ChatMessage[] = [];
90
91 for (let i = 0; i < filteredHistory.length; i++) {
92 const historyItem = filteredHistory[i];
93
94 if (messageMode === "chat") {
95 const toolMessage: ToolResultChatMessage = historyItem.message as ToolResultChatMessage;
96 if (historyItem.toolCallState?.toolCallId || toolMessage.toolCallId) {
97 // remove all tool calls from the history
98 continue;
99 }
100 }
101
102 if (historyItem.message.role === "user") {
103 // Gather context items for user messages
104 let content = normalizeToMessageParts(historyItem.message);
105
106 const ctxItems = historyItem.contextItems
107 .map((ctxItem) => {
108 return {
109 type: "text",
110 text: `${ctxItem.content}\n`,
111 } as TextMessagePart;
112 })
113 .filter((part) => !!part.text.trim());
114
115 content = [...ctxItems, ...content];
116 msgs.push({
117 ...historyItem.message,
118 content,
119 });
120 } else {
121 msgs.push(historyItem.message);
122 }
123 }
124
125 const lastUserMsg = findLast(msgs, isUserOrToolMsg) as
126 | UserChatMessage
127 | ToolResultChatMessage
128 | undefined;
129
130 const systemMessage = getSystemMessageWithRules({
131 baseSystemMessage: baseChatOrAgentSystemMessage,
132 rules,
133 userMessage: lastUserMsg,
134 });
135
136 if (systemMessage.trim()) {
137 msgs.unshift({
138 role: "system",
139 content: systemMessage,
140 });
141 }
142
143 // Remove the "id" from all of the messages
144 return msgs.map((msg) => {
145 const { id, ...rest } = msg as any;
146 return rest;
147 });
148}