Elffuss BraidHub

kikocis/continue public

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