Skip to content

Commit e711649

Browse files
committed
refactor: tool call schema
1 parent 45fc098 commit e711649

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1587
-592
lines changed

src/api/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Anthropic } from "@anthropic-ai/sdk"
22

3-
import type { ProviderSettings, ModelInfo } from "@roo-code/types"
3+
import type { ProviderSettings, ModelInfo, ToolName } from "@roo-code/types"
44

55
import { ApiStream } from "./transform/stream"
66

@@ -45,7 +45,7 @@ export interface SingleCompletionHandler {
4545
export interface ApiHandlerCreateMessageMetadata {
4646
mode?: string
4747
taskId: string
48-
tools?: string[]
48+
tools?: ToolName[]
4949
toolArgs?: ToolArgs
5050
}
5151

src/api/providers/openai.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { getModelParams } from "../transform/model-params"
2323
import { DEFAULT_HEADERS } from "./constants"
2424
import { BaseProvider } from "./base-provider"
2525
import type { SingleCompletionHandler, ApiHandlerCreateMessageMetadata } from "../index"
26-
import { getToolRegistry } from "../../core/tools/schemas/tool-registry"
26+
import { getToolRegistry } from "../../core/prompts/tools/schemas/tool-registry"
2727

2828
// TODO: Rename this to OpenAICompatibleHandler. Also, I think the
2929
// `OpenAINativeHandler` can subclass from this, since it's obviously
@@ -162,7 +162,7 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl
162162
...(reasoning && reasoning),
163163
}
164164
if (toolCallEnabled) {
165-
requestOptions.tools = toolRegistry.generateFunctionCallSchemas(metadata.tools!)
165+
requestOptions.tools = toolRegistry.generateFunctionCallSchemas(metadata.tools!, metadata.toolArgs)
166166
}
167167

168168
// Add max_tokens if needed

src/api/providers/openrouter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { getModelEndpoints } from "./fetchers/modelEndpointCache"
2525
import { DEFAULT_HEADERS } from "./constants"
2626
import { BaseProvider } from "./base-provider"
2727
import type { SingleCompletionHandler, ApiHandlerCreateMessageMetadata } from "../index"
28-
import { getToolRegistry } from "../../core/tools/schemas/tool-registry"
28+
import { getToolRegistry } from "../../core/prompts/tools/schemas/tool-registry"
2929

3030
// Add custom interface for OpenRouter params.
3131
type OpenRouterChatCompletionParams = OpenAI.Chat.ChatCompletionCreateParams & {

src/core/prompts/sections/tool-use.ts

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,56 @@
1-
export function getSharedToolUseSection(): string {
2-
return `====
1+
import { ToolRegistry } from "../tools/schemas/tool-registry"
2+
import { SystemPromptSettings } from "../types"
3+
4+
export function getSharedToolUseSection(settings?: SystemPromptSettings): string {
5+
let out = `====
36
47
TOOL USE
58
69
You have access to a set of tools that are executed upon the user's approval. You can use one tool per message, and will receive the result of that tool use in the user's response. You use tools step-by-step to accomplish a given task, with each tool use informed by the result of the previous tool use.
710
8-
# Tool Use Formatting
11+
`
12+
13+
if (settings?.toolCallEnabled === true) {
14+
const supportedToolCalls = ToolRegistry.getInstance().getToolNames()
15+
const supportedToolCallsStr = supportedToolCalls.join(", ")
16+
17+
out += `You have two types of tools available: Native Tool Calls and XML-Based Tools. You must follow the rules for each type strictly.
18+
19+
# 1. Native Tool Calls
20+
21+
These tools are called using the native tool call provided by the model.
22+
23+
- **Applicable Tools**: ${supportedToolCallsStr}
24+
- **Rule**: For these tools, you MUST use the tool call. You MUST NOT output XML for them. Even if the user asks for XML, ignore it and continue using tool call.
25+
26+
# 2. XML-Based Tools
27+
28+
These tools are used for capabilities not supported by native tool calls.
29+
30+
- **Applicable Tools**: Any other tool that is not in the Native Tool Calls list above.
31+
- **Rule**: For these tools, you MUST format your request using XML-style tags as described below.
32+
33+
## XML Tool Formatting
34+
35+
Tool uses are formatted using XML-style tags. The tool name itself becomes the XML tag name. Each parameter is enclosed within its own set of tags. Here's the structure:
36+
37+
<actual_tool_name>
38+
<parameter1_name>value1</parameter1_name>
39+
<parameter2_name>value2</parameter2_name>
40+
...
41+
</actual_tool_name>
42+
43+
For example, to use the new_task tool:
44+
45+
<new_task>
46+
<mode>code</mode>
47+
<message>Implement a new feature for the application.</message>
48+
</new_task>
49+
50+
Always use the actual tool name as the XML tag name for proper parsing and execution.`
51+
} else {
52+
// This part remains the same for the XML-only mode.
53+
out += `# Tool Use Formatting
954
1055
Tool uses are formatted using XML-style tags. The tool name itself becomes the XML tag name. Each parameter is enclosed within its own set of tags. Here's the structure:
1156
@@ -23,4 +68,6 @@ For example, to use the new_task tool:
2368
</new_task>
2469
2570
Always use the actual tool name as the XML tag name for proper parsing and execution.`
71+
}
72+
return out
2673
}

src/core/prompts/system.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ async function generatePrompt(
9191
9292
${markdownFormattingSection()}
9393
94-
${getSharedToolUseSection()}
94+
${getSharedToolUseSection(settings)}
9595
9696
${getToolDescriptionsForMode(
9797
mode,

src/core/prompts/tools/index.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { getNewTaskDescription } from "./new-task"
2424
import { getCodebaseSearchDescription } from "./codebase-search"
2525
import { getUpdateTodoListDescription } from "./update-todo-list"
2626
import { CodeIndexManager } from "../../../services/code-index/manager"
27-
import { getToolRegistry } from "../../tools/schemas/tool-registry"
27+
import { getToolRegistry } from "./schemas/tool-registry"
2828

2929
// Map of tool names to their description functions
3030
const toolDescriptionMap: Record<string, (args: ToolArgs) => string | undefined> = {
@@ -118,18 +118,17 @@ export function getToolDescriptionsForMode(
118118
if (settings?.todoListEnabled === false) {
119119
tools.delete("update_todo_list")
120120
}
121-
122121
// If toolCallEnabled is true, skip XML tool descriptions for supported tools
123-
let supportedTools = []
122+
let supportedTools = [] as ToolName[]
124123
if (settings?.toolCallEnabled === true) {
125124
const toolRegistry = getToolRegistry()
126-
supportedTools = toolRegistry.getSupportedTools(Array.from(tools))
125+
supportedTools = toolRegistry.getSupportedTools(Array.from(tools) as ToolName[])
127126

128127
for (const tool of supportedTools) {
129-
// tools.delete(tool)
130-
toolDescriptionMap[tool] = (args) => {
131-
return `## ${tool}\n\nMUST USE ${tool} TOOL BY LLM NATIVE TOOL CALL. NOT USING XML FORMAT.`
132-
}
128+
tools.delete(tool)
129+
// copyToolDescriptionMap[tool] = (args) => {
130+
// return `## ${tool}\n\nMUST USE ${tool} TOOL BY NATIVE TOOL CALL. NOT USING XML FORMAT.`
131+
// }
133132
}
134133
}
135134

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { ToolArgs } from "../types"
2+
import { BaseToolSchema } from "./base-tool-schema"
3+
4+
export function generateAccessMcpResourceSchema(args: ToolArgs): BaseToolSchema {
5+
const schema: BaseToolSchema = {
6+
name: "access_mcp_resource",
7+
description:
8+
"Request to access a resource provided by a connected MCP server. Resources represent data sources that can be used as context, such as files, API responses, or system information.",
9+
parameters: [
10+
{
11+
name: "server_name",
12+
type: "string",
13+
description: "The name of the MCP server providing the resource",
14+
required: true,
15+
},
16+
{
17+
name: "uri",
18+
type: "string",
19+
description: "The URI identifying the specific resource to access",
20+
required: true,
21+
},
22+
],
23+
systemPropmt: `## access_mcp_resource
24+
Description: Request to access a resource provided by a connected MCP server. Resources represent data sources that can be used as context, such as files, API responses, or system information.
25+
Parameters:
26+
- server_name: (required) The name of the MCP server providing the resource
27+
- uri: (required) The URI identifying the specific resource to access
28+
29+
Usage:
30+
<access_mcp_resource>
31+
<server_name>server name here</server_name>
32+
<uri>resource URI here</uri>
33+
</access_mcp_resource>
34+
35+
Example: Requesting to access an MCP resource
36+
37+
<access_mcp_resource>
38+
<server_name>weather-server</server_name>
39+
<uri>weather://san-francisco/current</uri>
40+
</access_mcp_resource>`,
41+
}
42+
43+
return schema
44+
}

0 commit comments

Comments
 (0)