108 lines
3 KiB
TypeScript
108 lines
3 KiB
TypeScript
import { ChatHistory, ChatMessage, MessagePart } from "../index.js";
|
|
import { normalizeToMessageParts } from "../util/messageContent.js";
|
|
|
|
const SYSTEM_MESSAGE = `When generating new code:
|
|
|
|
1. Always produce a single code block.
|
|
2. Never separate the code into multiple code blocks.
|
|
3. Only include the code that is being added.
|
|
4. Replace existing code with a "lazy" comment like this: "// ... existing code ..."
|
|
5. The "lazy" comment must always be a valid comment in the current context (e.g. "<!-- ... existing code ... -->" for HTML, "// ... existing code ..." for JavaScript, "{/* ... existing code */}" for TSX, etc.)
|
|
6. You must always provide 1-2 lines of context above and below a "lazy" comment
|
|
7. If the user submits a code block that contains a filename in the language specifier, always include the filename in any code block you generate based on that file. The filename should be on the same line as the language specifier in your code block.
|
|
|
|
Example 1:
|
|
Input:
|
|
\`\`\`test.js
|
|
import addition from "addition"
|
|
|
|
class Calculator {
|
|
constructor() {
|
|
this.result = 0;
|
|
}
|
|
|
|
add(number) {
|
|
this.result += number;
|
|
return this;
|
|
}
|
|
}
|
|
\`\`\`
|
|
User request: Add a subtract method
|
|
|
|
Output:
|
|
\`\`\`javascript test.js
|
|
// ... existing code ...
|
|
import subtraction from "subtraction"
|
|
|
|
class Calculator {
|
|
// ... existing code ...
|
|
|
|
subtract(number) {
|
|
this.result -= number;
|
|
return this;
|
|
}
|
|
}
|
|
\`\`\`
|
|
|
|
Example 2:
|
|
Input:
|
|
\`\`\`javascript test.js (6-9)
|
|
function helloWorld() {}
|
|
\`\`\`
|
|
|
|
Output:
|
|
\`\`\`javascript test.js
|
|
function helloWorld() {
|
|
// New code here
|
|
}
|
|
\`\`\`
|
|
|
|
Always follow these guidelines when generating code responses.
|
|
|
|
Other rules:
|
|
- Avoid calling tools unless they are absolutely necessary. For example, if you are asked a simple programming question you do not need web search. As another example, if the user asks you to explain something about code, do not create a new file.`;
|
|
|
|
export function constructMessages(
|
|
history: ChatHistory,
|
|
model: string,
|
|
): ChatMessage[] {
|
|
const msgs: ChatMessage[] = [];
|
|
|
|
// Only using this system message with Sonnet right now
|
|
if (
|
|
// hasCodeBlockWithFilename(history[0].message.content) &&
|
|
model.includes("sonnet")
|
|
) {
|
|
msgs.push({
|
|
role: "system" as const,
|
|
content: SYSTEM_MESSAGE,
|
|
});
|
|
}
|
|
|
|
for (let i = 0; i < history.length; i++) {
|
|
const historyItem = history[i];
|
|
|
|
if (historyItem.message.role === "user") {
|
|
// Gather context items for user messages
|
|
let content = normalizeToMessageParts(historyItem.message);
|
|
|
|
const ctxItems = historyItem.contextItems.map((ctxItem) => {
|
|
return { type: "text", text: `${ctxItem.content}\n` } as MessagePart;
|
|
});
|
|
|
|
content = [...ctxItems, ...content];
|
|
msgs.push({
|
|
...historyItem.message,
|
|
content,
|
|
});
|
|
} else {
|
|
msgs.push(historyItem.message);
|
|
}
|
|
}
|
|
|
|
// Remove the "id" from all of the messages
|
|
return msgs.map((msg) => {
|
|
const { id, ...rest } = msg as any;
|
|
return rest;
|
|
});
|
|
}
|