Elffuss BraidHub

kikocis/continue public

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