Elffuss BraidHub

kikocis/continue public

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