Elffuss BraidHub

kikocis/continue public

3ff29c567dd0 · 2.4 KiBRawPermalink
1import { Tool } from "../..";
2import { closeTag } from "./systemToolUtils";
3import { SystemMessageToolsFramework } from "./types";
4
5export const TOOL_INSTRUCTIONS_TAG = "<tool_use_instructions>";
6
7export const generateToolsSystemMessage = (
8 tools: Tool[],
9 framework: SystemMessageToolsFramework,
10): string => {
11 if (tools.length === 0) {
12 return "";
13 }
14 const withPredefinedMessage = tools.filter(
15 (tool) => !!tool.systemMessageDescription,
16 );
17
18 const withDynamicMessage = tools.filter(
19 (tool) => !tool.systemMessageDescription,
20 );
21
22 const instructions: string[] = [];
23 instructions.push(TOOL_INSTRUCTIONS_TAG);
24 instructions.push(framework.systemMessagePrefix);
25
26 if (withPredefinedMessage.length > 0) {
27 instructions.push(`\nThe following tools are available to you:`);
28 for (const tool of withPredefinedMessage) {
29 const definition = framework.createSystemMessageExampleCall(
30 tool.function.name,
31 tool.systemMessageDescription!.prefix,
32 tool.systemMessageDescription!.exampleArgs,
33 );
34 instructions.push(`\n${definition}`);
35 }
36 }
37
38 if (withDynamicMessage.length > 0) {
39 instructions.push(
40 `\nAlso, these additional tool definitions show other tools you can call with the same syntax:`,
41 );
42
43 for (const tool of tools) {
44 try {
45 const definition = framework.toolToSystemToolDefinition(tool);
46 instructions.push(`\n${definition}`);
47 } catch (e) {
48 console.error(
49 "Failed to convert tool to system message tool:\n" +
50 JSON.stringify(tool),
51 );
52 }
53 }
54
55 instructions.push(`\nFor example, this tool definition:\n`);
56 instructions.push(framework.exampleDynamicToolDefinition);
57 instructions.push("\nCan be called like this:\n");
58 instructions.push(framework.exampleDynamicToolCall);
59 }
60
61 instructions.push("\n" + framework.systemMessageSuffix);
62
63 instructions.push(`${closeTag(TOOL_INSTRUCTIONS_TAG)}`);
64 return instructions.join("\n");
65};
66
67export function addSystemMessageToolsToSystemMessage(
68 framework: SystemMessageToolsFramework,
69 baseSystemMessage: string,
70 systemMessageTools: Tool[],
71): string {
72 let systemMessage = baseSystemMessage;
73 if (systemMessageTools.length > 0) {
74 const toolsSystemMessage = generateToolsSystemMessage(
75 systemMessageTools,
76 framework,
77 );
78 systemMessage += `\n\n${toolsSystemMessage}`;
79 }
80
81 return systemMessage;
82}