Elffuss BraidHub

kikocis/continue public

4940c71be2bf · 4.8 KiBRawPermalink
1import { Tool } from "../..";
2import { closeTag } from "./systemToolUtils";
3
4export const TOOL_INSTRUCTIONS_TAG = "<tool_use_instructions>";
5export const TOOL_DEFINITION_TAG = "TOOL_DEFINITION";
6export const TOOL_DESCRIPTION_TAG = "DESCRIPTION";
7
8const EXAMPLE_DYNAMIC_TOOL = `
9\`\`\`tool_definition
10TOOL_NAME: example_tool
11TOOL_ARG: arg_1 (string, required)
12Description of the first argument
13END_ARG
14TOOL_ARG: arg_2 (number, optional)
15END_ARG
16\`\`\``.trim();
17
18const EXAMPLE_DYNAMIC_TOOL_CALL = `
19\`\`\`tool
20TOOL_NAME: example_tool
21BEGIN_ARG: arg_1
22The value
23of arg 1
24END_ARG
25BEGIN_ARG: arg_2
263
27END_ARG
28\`\`\``.trim();
29
30export function createSystemMessageExampleCall(
31 name: string,
32 instructions: string,
33 args: Array<[string, string]> = [],
34) {
35 let callExample = `\`\`\`tool
36TOOL_NAME: ${name}`;
37
38 // Add each argument dynamically
39 for (const [argName, argValue] of args) {
40 callExample += `
41BEGIN_ARG: ${argName}
42${argValue}
43END_ARG`;
44 }
45
46 callExample += `
47\`\`\``;
48
49 return `${instructions.trim()}
50${callExample}`;
51}
52
53function toolToSystemToolDefinition(tool: Tool): string {
54 let toolDefinition = `\`\`\`tool_definition\nTOOL_NAME: ${tool.function.name}\n`;
55
56 if (tool.function.description) {
57 toolDefinition += `TOOL_DESCRIPTION:\n${tool.function.description}\n`;
58 }
59
60 if (tool.function.parameters && "properties" in tool.function.parameters) {
61 for (const [key, value] of Object.entries(
62 tool.function.parameters.properties as object,
63 )) {
64 const isRequired = tool.function.parameters.required?.includes(key);
65 const requiredText = isRequired ? "required" : "optional";
66
67 let argType = "string";
68 if ("type" in value) {
69 argType = value.type;
70 }
71 let argDescription = "";
72 if ("description" in value) {
73 argDescription = value.description;
74 }
75
76 toolDefinition += `TOOL_ARG: ${key} (${argType}, ${requiredText})\n`;
77 if (argDescription) {
78 toolDefinition += argDescription + "\n";
79 }
80 toolDefinition += `END_ARG\n`;
81 }
82 }
83
84 toolDefinition += `\`\`\``;
85 return toolDefinition.trim();
86}
87
88export const generateToolsSystemMessage = (tools: Tool[]): string => {
89 if (tools.length === 0) {
90 return "";
91 }
92 const withPredefinedMessage = tools.filter(
93 (tool) => !!tool.systemMessageDescription,
94 );
95
96 const withDynamicMessage = tools.filter(
97 (tool) => !tool.systemMessageDescription,
98 );
99
100 const instructions: string[] = [];
101 instructions.push(TOOL_INSTRUCTIONS_TAG);
102 instructions.push(
103 `You have access to several "tools" that you can use at any time to retrieve information and/or perform tasks for the User.`,
104 );
105 instructions.push(
106 `To use a tool, respond with a tool code block (\`\`\`tool) using the syntax shown in the examples below:`,
107 );
108
109 if (withPredefinedMessage.length > 0) {
110 instructions.push(`\nThe following tools are available to you:`);
111 for (const tool of withPredefinedMessage) {
112 instructions.push(`\n${tool.systemMessageDescription}`);
113 }
114 }
115
116 if (withDynamicMessage.length > 0) {
117 instructions.push(
118 `\nAlso, these additional tool definitions show other tools you can call with the same syntax:`,
119 );
120
121 for (const tool of tools) {
122 try {
123 const definition = toolToSystemToolDefinition(tool);
124 instructions.push(`\n${definition}`);
125 } catch (e) {
126 console.error(
127 "Failed to convert tool to system message tool:\n" +
128 JSON.stringify(tool),
129 );
130 }
131 }
132
133 instructions.push(`\nFor example, this tool definition:\n`);
134 instructions.push(EXAMPLE_DYNAMIC_TOOL);
135 instructions.push("\nCan be called like this:\n");
136 instructions.push(EXAMPLE_DYNAMIC_TOOL_CALL);
137 }
138
139 instructions.push(
140 `\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`,
141 );
142 instructions.push(
143 `Then send the \`\`\`tool codeblock (YOU call the tool, not the user). Always start the codeblock on a new line.`,
144 );
145 instructions.push(
146 `Do not perform actions with/for hypothetical files. Ask the user or use tools to deduce which files are relevant.`,
147 );
148 instructions.push(
149 `You 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.`,
150 );
151
152 instructions.push(`${closeTag(TOOL_INSTRUCTIONS_TAG)}`);
153 return instructions.join("\n");
154};
155
156export function addSystemMessageToolsToSystemMessage(
157 baseSystemMessage: string,
158 systemMessageTools: Tool[],
159): string {
160 let systemMessage = baseSystemMessage;
161 if (systemMessageTools.length > 0) {
162 const toolsSystemMessage = generateToolsSystemMessage(systemMessageTools);
163 systemMessage += `\n\n${toolsSystemMessage}`;
164 }
165
166 return systemMessage;
167}