continue-custom/core/tools/systemMessageTools/buildToolsSystemMessage.ts
2025-07-20 10:52:22 +02:00

157 lines
4.6 KiB
TypeScript

import { Tool } from "../..";
import { closeTag } from "./systemToolUtils";
export const TOOL_INSTRUCTIONS_TAG = "<tool_use_instructions>";
export const TOOL_DEFINITION_TAG = "TOOL_DEFINITION";
export const TOOL_DESCRIPTION_TAG = "DESCRIPTION";
const EXAMPLE_DYNAMIC_TOOL = `
\`\`\`tool_definition
TOOL_NAME: example_tool
TOOL_ARG: arg_1 (string, required)
Description of the first argument
END_ARG
TOOL_ARG: arg_2 (number, optional)
END_ARG
\`\`\``.trim();
const EXAMPLE_DYNAMIC_TOOL_CALL = `
\`\`\`tool
TOOL_NAME: example_tool
BEGIN_ARG: arg_1
The value
of arg 1
END_ARG
BEGIN_ARG: arg_2
3
END_ARG
\`\`\``.trim();
export function createSystemMessageExampleCall(
name: string,
instructions: string,
args: Array<[string, string]> = [],
) {
let callExample = `\`\`\`tool
TOOL_NAME: ${name}`;
// Add each argument dynamically
for (const [argName, argValue] of args) {
callExample += `
BEGIN_ARG: ${argName}
${argValue}
END_ARG`;
}
callExample += `
\`\`\``;
return `${instructions.trim()}
${callExample}`;
}
function toolToSystemToolDefinition(tool: Tool): string {
let toolDefinition = `\`\`\`tool_definition\nTOOL_NAME: ${tool.function.name}\n`;
if (tool.function.description) {
toolDefinition += `TOOL_DESCRIPTION:\n${tool.function.description}\n`;
}
if (tool.function.parameters && "properties" in tool.function.parameters) {
for (const [key, value] of Object.entries(
tool.function.parameters.properties as object,
)) {
const isRequired = tool.function.parameters.required?.includes(key);
const requiredText = isRequired ? "required" : "optional";
let argType = "string";
if ("type" in value) {
argType = value.type;
}
let argDescription = "";
if ("description" in value) {
argDescription = value.argDescription;
}
toolDefinition += `TOOL_ARG: ${key} (${argType}, ${requiredText})\n`;
if (argDescription) {
toolDefinition += argDescription + "\n";
}
toolDefinition += `END_ARG\n`;
}
}
toolDefinition += `\`\`\``;
return toolDefinition.trim();
}
export const generateToolsSystemMessage = (tools: Tool[]) => {
if (tools.length === 0) {
return undefined;
}
const withPredefinedMessage = tools.filter(
(tool) => !!tool.systemMessageDescription,
);
const withDynamicMessage = tools.filter(
(tool) => !tool.systemMessageDescription,
);
let prompt = `${TOOL_INSTRUCTIONS_TAG}\n`;
prompt += `You have access to several "tools" that you can use at any time to retrieve information and/or perform tasks for the User.`;
prompt += `\nTo use a tool, respond with a tool code block (\`\`\`tool) using the syntax shown in the examples below:`;
if (withPredefinedMessage.length > 0) {
prompt += `\n\nThe following tools are available to you:`;
for (const tool of withPredefinedMessage) {
prompt += "\n\n";
prompt += tool.systemMessageDescription;
}
}
if (withDynamicMessage.length > 0) {
prompt += `\n\nAlso, these additional tool definitions show other tools you can call with the same syntax:`;
for (const tool of tools) {
try {
const definition = toolToSystemToolDefinition(tool);
prompt += "\n\n";
prompt += definition;
} catch (e) {
console.error(
"Failed to convert tool to system message tool:\n" +
JSON.stringify(tool),
);
}
}
prompt += `\n\nFor example, this tool definition:\n\n`;
prompt += EXAMPLE_DYNAMIC_TOOL;
prompt += "\n\nCan be called like this:\n";
prompt += EXAMPLE_DYNAMIC_TOOL_CALL;
}
prompt += `\n\nIf it seems like the User's request could be solved with one of the tools, choose the BEST one for the job based on the user's request and the tool descriptions`;
prompt += `\nThen send the \`\`\`tool codeblock (YOU call the tool, not the user). Always start the codeblock on a new line.`;
prompt += `\nDo not perform actions with/for hypothetical files. Ask the user or use tools to deduce which files are relevant.`;
prompt += `\nYou can only call ONE tool at at time. The tool codeblock should be the last thing you say; stop your response after the tool codeblock.`;
prompt += `\n${closeTag(TOOL_INSTRUCTIONS_TAG)}`;
return prompt;
};
export function addSystemMessageToolsToSystemMessage(
baseSystemMessage: string,
systemMessageTools: Tool[],
): string {
let systemMessage = baseSystemMessage;
if (systemMessageTools.length > 0) {
const toolsSystemMessage = generateToolsSystemMessage(systemMessageTools);
systemMessage += `\n\n${toolsSystemMessage}`;
}
return systemMessage;
}