Skip to content

Native Tool Call #6744

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/types/src/provider-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const baseProviderSettingsSchema = z.object({
includeMaxTokens: z.boolean().optional(),
diffEnabled: z.boolean().optional(),
todoListEnabled: z.boolean().optional(),
toolCallEnabled: z.boolean().optional(),
fuzzyMatchThreshold: z.number().optional(),
modelTemperature: z.number().nullish(),
rateLimitSeconds: z.number().optional(),
Expand Down
11 changes: 10 additions & 1 deletion src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Anthropic } from "@anthropic-ai/sdk"

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

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

Expand Down Expand Up @@ -38,6 +38,7 @@ import {
RooHandler,
} from "./providers"
import { NativeOllamaHandler } from "./providers/native-ollama"
import { ToolArgs } from "../core/prompts/tools/types"

export interface SingleCompletionHandler {
completePrompt(prompt: string): Promise<string>
Expand All @@ -53,6 +54,14 @@ export interface ApiHandlerCreateMessageMetadata {
* Used to enforce "skip once" after a condense operation.
*/
suppressPreviousResponseId?: boolean
/**
* tool call
*/
tools?: ToolName[]
/**
* tool call args
*/
toolArgs?: ToolArgs
}

export interface ApiHandler {
Expand Down
30 changes: 30 additions & 0 deletions src/api/providers/base-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,34 @@ export abstract class BaseProvider implements ApiHandler {

return countTokens(content, { useWorker: true })
}

/**
* Convert tool schemas to text format for token counting
*/
protected convertToolSchemasToText(toolSchemas: Anthropic.ToolUnion[]): string {
if (toolSchemas.length === 0) {
return ""
}

const toolsDescription = toolSchemas
.map((tool) => {
// Handle different tool types by accessing properties safely
const toolName = tool.name
let toolText = `Tool: ${toolName}\n`

// Try to access description and input_schema properties
if ("description" in tool) {
toolText += `Description: ${tool.description}\n`
}

if ("input_schema" in tool && tool.input_schema && typeof tool.input_schema === "object") {
toolText += `Parameters:\n${JSON.stringify(tool.input_schema, null, 2)}\n`
}

return toolText
})
.join("\n---\n")

return `Available Tools:\n${toolsDescription}`
}
}
22 changes: 21 additions & 1 deletion src/api/providers/lm-studio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { BaseProvider } from "./base-provider"
import type { SingleCompletionHandler, ApiHandlerCreateMessageMetadata } from "../index"
import { getModels, getModelsFromCache } from "./fetchers/modelCache"
import { getApiRequestTimeout } from "./utils/timeout-config"
import { getToolRegistry } from "../../core/prompts/tools/schemas/tool-registry"

export class LmStudioHandler extends BaseProvider implements SingleCompletionHandler {
protected options: ApiHandlerOptions
Expand All @@ -40,6 +41,8 @@ export class LmStudioHandler extends BaseProvider implements SingleCompletionHan
{ role: "system", content: systemPrompt },
...convertToOpenAiMessages(messages),
]
const toolCallEnabled = metadata?.tools && metadata.tools.length > 0
const toolRegistry = getToolRegistry()

// -------------------------
// Track token usage
Expand Down Expand Up @@ -68,7 +71,17 @@ export class LmStudioHandler extends BaseProvider implements SingleCompletionHan

let inputTokens = 0
try {
inputTokens = await this.countTokens([{ type: "text", text: systemPrompt }, ...toContentBlocks(messages)])
const inputMessages: Anthropic.Messages.ContentBlockParam[] = [{ type: "text", text: systemPrompt }]
if (toolCallEnabled) {
const toolSchemas: Anthropic.ToolUnion[] = toolRegistry.generateAnthropicToolSchemas(
metadata.tools!,
metadata.toolArgs,
)
const toolsText = this.convertToolSchemasToText(toolSchemas)
inputMessages.push({ type: "text", text: toolsText })
}
inputMessages.push(...toContentBlocks(messages))
inputTokens = await this.countTokens(inputMessages)
} catch (err) {
console.error("[LmStudio] Failed to count input tokens:", err)
inputTokens = 0
Expand All @@ -83,6 +96,10 @@ export class LmStudioHandler extends BaseProvider implements SingleCompletionHan
temperature: this.options.modelTemperature ?? LMSTUDIO_DEFAULT_TEMPERATURE,
stream: true,
}
if (toolCallEnabled) {
params.tools = toolRegistry.generateFunctionCallSchemas(metadata.tools!, metadata.toolArgs)
params.tool_choice = "auto"
}

if (this.options.lmStudioSpeculativeDecodingEnabled && this.options.lmStudioDraftModelId) {
params.draft_model = this.options.lmStudioDraftModelId
Expand All @@ -108,6 +125,9 @@ export class LmStudioHandler extends BaseProvider implements SingleCompletionHan
yield processedChunk
}
}
if (delta?.tool_calls) {
yield { type: "tool_call", toolCalls: delta.tool_calls, toolCallType: "openai" }
}
}

for (const processedChunk of matcher.final()) {
Expand Down
11 changes: 11 additions & 0 deletions src/api/providers/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { DEFAULT_HEADERS } from "./constants"
import { BaseProvider } from "./base-provider"
import type { SingleCompletionHandler, ApiHandlerCreateMessageMetadata } from "../index"
import { getApiRequestTimeout } from "./utils/timeout-config"
import { getToolRegistry } from "../../core/prompts/tools/schemas/tool-registry"

// TODO: Rename this to OpenAICompatibleHandler. Also, I think the
// `OpenAINativeHandler` can subclass from this, since it's obviously
Expand Down Expand Up @@ -92,6 +93,9 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl
const deepseekReasoner = modelId.includes("deepseek-reasoner") || enabledR1Format
const ark = modelUrl.includes(".volces.com")

const toolCallEnabled = metadata?.tools && metadata.tools.length > 0
const toolRegistry = getToolRegistry()

if (modelId.includes("o1") || modelId.includes("o3") || modelId.includes("o4")) {
yield* this.handleO3FamilyMessage(modelId, systemPrompt, messages)
return
Expand Down Expand Up @@ -163,6 +167,10 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl
...(isGrokXAI ? {} : { stream_options: { include_usage: true } }),
...(reasoning && reasoning),
}
if (toolCallEnabled) {
requestOptions.tools = toolRegistry.generateFunctionCallSchemas(metadata.tools!, metadata.toolArgs)
requestOptions.tool_choice = "auto"
}

// Add max_tokens if needed
this.addMaxTokensIfNeeded(requestOptions, modelInfo)
Expand Down Expand Up @@ -198,6 +206,9 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl
text: (delta.reasoning_content as string | undefined) || "",
}
}
if (delta?.tool_calls) {
yield { type: "tool_call", toolCalls: delta.tool_calls, toolCallType: "openai" }
}
if (chunk.usage) {
lastUsage = chunk.usage
}
Expand Down
14 changes: 13 additions & 1 deletion src/api/providers/openrouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import { getModelEndpoints } from "./fetchers/modelEndpointCache"

import { DEFAULT_HEADERS } from "./constants"
import { BaseProvider } from "./base-provider"
import type { SingleCompletionHandler } from "../index"
import type { SingleCompletionHandler, ApiHandlerCreateMessageMetadata } from "../index"
import { getToolRegistry } from "../../core/prompts/tools/schemas/tool-registry"

// Add custom interface for OpenRouter params.
type OpenRouterChatCompletionParams = OpenAI.Chat.ChatCompletionCreateParams & {
Expand Down Expand Up @@ -72,10 +73,13 @@ export class OpenRouterHandler extends BaseProvider implements SingleCompletionH
override async *createMessage(
systemPrompt: string,
messages: Anthropic.Messages.MessageParam[],
metadata?: ApiHandlerCreateMessageMetadata,
): AsyncGenerator<ApiStreamChunk> {
const model = await this.fetchModel()

let { id: modelId, maxTokens, temperature, topP, reasoning } = model
const toolCallEnabled = metadata?.tools && metadata.tools.length > 0
const toolRegistry = getToolRegistry()

// OpenRouter sends reasoning tokens by default for Gemini 2.5 Pro
// Preview even if you don't request them. This is not the default for
Expand Down Expand Up @@ -133,6 +137,10 @@ export class OpenRouterHandler extends BaseProvider implements SingleCompletionH
...(transforms && { transforms }),
...(reasoning && { reasoning }),
}
if (toolCallEnabled) {
completionParams.tools = toolRegistry.generateFunctionCallSchemas(metadata.tools!, metadata.toolArgs!)
completionParams.tool_choice = "auto"
}

const stream = await this.client.chat.completions.create(completionParams)

Expand All @@ -156,6 +164,10 @@ export class OpenRouterHandler extends BaseProvider implements SingleCompletionH
yield { type: "text", text: delta.content }
}

if (delta?.tool_calls) {
yield { type: "tool_call", toolCalls: delta.tool_calls, toolCallType: "openai" }
}

if (chunk.usage) {
lastUsage = chunk.usage
}
Expand Down
15 changes: 14 additions & 1 deletion src/api/transform/stream.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { ToolCallProviderType } from "../../shared/tools"

export type ApiStream = AsyncGenerator<ApiStreamChunk>

export type ApiStreamChunk = ApiStreamTextChunk | ApiStreamUsageChunk | ApiStreamReasoningChunk | ApiStreamError
export type ApiStreamChunk =
| ApiStreamTextChunk
| ApiStreamUsageChunk
| ApiStreamReasoningChunk
| ApiStreamError
| ApiStreamToolCallChunk

export interface ApiStreamError {
type: "error"
Expand All @@ -27,3 +34,9 @@ export interface ApiStreamUsageChunk {
reasoningTokens?: number
totalCost?: number
}

export interface ApiStreamToolCallChunk {
type: "tool_call"
toolCalls: any
toolCallType: ToolCallProviderType
}
8 changes: 7 additions & 1 deletion src/core/assistant-message/AssistantMessageParser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { type ToolName, toolNames } from "@roo-code/types"
import { TextContent, ToolUse, ToolParamName, toolParamNames } from "../../shared/tools"
import { AssistantMessageContent } from "./parseAssistantMessage"
import { ToolCallParam } from "../task/tool-call-helper"

/**
* Parser for assistant messages. Maintains state between chunks
Expand Down Expand Up @@ -51,7 +52,7 @@ export class AssistantMessageParser {
* Process a new chunk of text and update the parser state.
* @param chunk The new chunk of text to process.
*/
public processChunk(chunk: string): AssistantMessageContent[] {
public processChunk(chunk: string, toolCallParam?: ToolCallParam): AssistantMessageContent[] {
if (this.accumulator.length + chunk.length > this.MAX_ACCUMULATOR_SIZE) {
throw new Error("Assistant message exceeds maximum allowed size")
}
Expand Down Expand Up @@ -174,6 +175,11 @@ export class AssistantMessageParser {
name: extractedToolName as ToolName,
params: {},
partial: true,
toolUseId: toolCallParam && toolCallParam.toolUserId ? toolCallParam.toolUserId : undefined,
toolUseParam:
toolCallParam && toolCallParam?.anthropicContent
? toolCallParam?.anthropicContent
: undefined,
}

this.currentToolUseStartIndex = this.accumulator.length
Expand Down
9 changes: 8 additions & 1 deletion src/core/assistant-message/parseAssistantMessage.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { type ToolName, toolNames } from "@roo-code/types"

import { TextContent, ToolUse, ToolParamName, toolParamNames } from "../../shared/tools"
import { ToolCallParam } from "../task/tool-call-helper"

export type AssistantMessageContent = TextContent | ToolUse

export function parseAssistantMessage(assistantMessage: string): AssistantMessageContent[] {
export function parseAssistantMessage(
assistantMessage: string,
toolCallParam?: ToolCallParam,
): AssistantMessageContent[] {
let contentBlocks: AssistantMessageContent[] = []
let currentTextContent: TextContent | undefined = undefined
let currentTextContentStartIndex = 0
Expand Down Expand Up @@ -103,6 +107,9 @@ export function parseAssistantMessage(assistantMessage: string): AssistantMessag
name: toolUseOpeningTag.slice(1, -1) as ToolName,
params: {},
partial: true,
toolUseId: toolCallParam && toolCallParam.toolUserId ? toolCallParam.toolUserId : undefined,
toolUseParam:
toolCallParam && toolCallParam?.anthropicContent ? toolCallParam?.anthropicContent : undefined,
}

currentToolUseStartIndex = accumulator.length
Expand Down
47 changes: 42 additions & 5 deletions src/core/assistant-message/presentAssistantMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { Task } from "../task/Task"
import { codebaseSearchTool } from "../tools/codebaseSearchTool"
import { experiments, EXPERIMENT_IDS } from "../../shared/experiments"
import { applyDiffToolLegacy } from "../tools/applyDiffTool"
import Anthropic from "@anthropic-ai/sdk"

/**
* Processes and presents assistant message content to the user interface.
Expand Down Expand Up @@ -61,6 +62,7 @@ export async function presentAssistantMessage(cline: Task) {
return
}

const toolCallEnabled = cline.apiConfiguration?.toolCallEnabled
cline.presentAssistantMessageLocked = true
cline.presentAssistantMessageHasPendingUpdates = false

Expand Down Expand Up @@ -245,12 +247,47 @@ export async function presentAssistantMessage(cline: Task) {
}

const pushToolResult = (content: ToolResponse) => {
cline.userMessageContent.push({ type: "text", text: `${toolDescription()} Result:` })

const newUserMessages: (Anthropic.TextBlockParam | Anthropic.ImageBlockParam)[] = [
{ type: "text", text: `${toolDescription()} Result:` },
]
if (typeof content === "string") {
cline.userMessageContent.push({ type: "text", text: content || "(tool did not return anything)" })
newUserMessages.push({ type: "text", text: content || "(tool did not return anything)" })
} else {
newUserMessages.push(...content)
}

if (toolCallEnabled) {
const lastToolUseMessage = cline.assistantMessageContent.find((msg) => msg.type === "tool_use")
if (lastToolUseMessage && lastToolUseMessage.toolUseId) {
const toolUseId = lastToolUseMessage.toolUseId
let toolResultMessage = cline.userMessageContent.find(
(msg) => msg.type === "tool_result" && msg.tool_use_id === toolUseId,
)
if (toolResultMessage !== undefined && toolResultMessage.type === "tool_result") {
const content = toolResultMessage.content
const updateMessages: Array<Anthropic.TextBlockParam | Anthropic.ImageBlockParam> = []
if (typeof content === "string") {
updateMessages.push({ type: "text", text: content })
} else if (Array.isArray(content)) {
updateMessages.push(...content)
} else {
throw new Error(
"Unexpected tool result content type: " + JSON.stringify(toolResultMessage),
)
}
updateMessages.push(...newUserMessages)
toolResultMessage.content = updateMessages
} else {
const toolMessage: Anthropic.ToolResultBlockParam = {
tool_use_id: toolUseId,
type: "tool_result",
content: newUserMessages,
}
cline.userMessageContent.push(toolMessage)
}
}
} else {
cline.userMessageContent.push(...content)
cline.userMessageContent.push(...newUserMessages)
}

// Once a tool result has been collected, ignore all other tool
Expand Down Expand Up @@ -429,7 +466,7 @@ export async function presentAssistantMessage(cline: Task) {
)
}

if (isMultiFileApplyDiffEnabled) {
if (isMultiFileApplyDiffEnabled || toolCallEnabled) {
await checkpointSaveAndMark(cline)
await applyDiffTool(cline, block, askApproval, handleError, pushToolResult, removeClosingTag)
} else {
Expand Down
Loading