Elffuss BraidHub

kikocis/continue public

main · 3.8 KiBRawPermalink
1import { Tool, ToolCallState } from "../../..";
2import { SystemMessageToolsFramework } from "../types";
3import { handleToolCallBuffer } from "./parseSystemToolCall";
4
5export class SystemMessageToolCodeblocksFramework
6 implements SystemMessageToolsFramework
7{
8 // Poor models are really bad at following instructions, alternate starts allowed:
9 acceptedToolCallStarts: [string, string][] = [
10 ["```tool\n", "```tool\n"],
11 ["tool_name:", "```tool\nTOOL_NAME:"],
12 ];
13
14 toolCallStateToSystemToolCall(state: ToolCallState): string {
15 let parts = ["```tool"];
16 parts.push(`TOOL_NAME: ${state.toolCall.function.name}`);
17 try {
18 for (const arg in state.parsedArgs) {
19 parts.push(`BEGIN_ARG: ${arg}`);
20 parts.push(JSON.stringify(state.parsedArgs[arg]));
21 parts.push(`END_ARG`);
22 }
23 } catch (e) {
24 console.log("Failed to stringify json args", state.parsedArgs);
25 }
26 // TODO - include tool call id for parallel. Confuses dumb models
27 parts.push("```");
28 return parts.join("\n");
29 }
30
31 handleToolCallBuffer = handleToolCallBuffer;
32
33 toolToSystemToolDefinition(tool: Tool): string {
34 let toolDefinition = `\`\`\`tool_definition\nTOOL_NAME: ${tool.function.name}\n`;
35
36 if (tool.function.description) {
37 toolDefinition += `TOOL_DESCRIPTION:\n${tool.function.description}\n`;
38 }
39
40 if (tool.function.parameters && "properties" in tool.function.parameters) {
41 for (const [key, value] of Object.entries(
42 tool.function.parameters.properties as object,
43 )) {
44 const isRequired = tool.function.parameters.required?.includes(key);
45 const requiredText = isRequired ? "required" : "optional";
46
47 let argType = "string";
48 if ("type" in value) {
49 argType = value.type;
50 }
51 let argDescription = "";
52 if ("description" in value) {
53 argDescription = value.description;
54 }
55
56 toolDefinition += `TOOL_ARG: ${key} (${argType}, ${requiredText})\n`;
57 if (argDescription) {
58 toolDefinition += argDescription + "\n";
59 }
60 toolDefinition += `END_ARG\n`;
61 }
62 }
63
64 toolDefinition += `\`\`\``;
65 return toolDefinition.trim();
66 }
67
68 systemMessagePrefix = `You have access to several "tools" that you can use at any time to retrieve information and/or perform tasks for the User.
69To use a tool, respond with a tool code block (\`\`\`tool) using the syntax shown in the examples below:`;
70
71 systemMessageSuffix = `If 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
72Then send the \`\`\`tool codeblock (YOU call the tool, not the user). Always start the codeblock on a new line.
73Do not perform actions with/for hypothetical files. Ask the user or use tools to deduce which files are relevant.
74You 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.`;
75
76 exampleDynamicToolDefinition = `
77\`\`\`tool_definition
78TOOL_NAME: example_tool
79TOOL_ARG: arg_1 (string, required)
80Description of the first argument
81END_ARG
82TOOL_ARG: arg_2 (number, optional)
83END_ARG
84\`\`\``.trim();
85
86 exampleDynamicToolCall = `
87\`\`\`tool
88TOOL_NAME: example_tool
89BEGIN_ARG: arg_1
90The value
91of arg 1
92END_ARG
93BEGIN_ARG: arg_2
943
95END_ARG
96\`\`\``.trim();
97
98 createSystemMessageExampleCall(
99 toolName: string,
100 prefix: string,
101 exampleArgs: Array<[string, string | number]> = [],
102 ) {
103 let callExample = `\`\`\`tool
104TOOL_NAME: ${toolName}`;
105
106 // Add each argument dynamically
107 for (const [argName, argValue] of exampleArgs) {
108 callExample += `
109BEGIN_ARG: ${argName}
110${argValue}
111END_ARG`;
112 }
113
114 callExample += `
115\`\`\``;
116
117 return `${prefix.trim()}
118${callExample}`;
119 }
120}