Skip to content
Closed
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 @@ -100,6 +100,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 @@ -42,6 +42,7 @@ import {
DeepInfraHandler,
} 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 @@ -65,6 +66,14 @@ export interface ApiHandlerCreateMessageMetadata {
* @default true
*/
store?: boolean
/**
* tool call
*/
tools?: ToolName[]
/**
* tool call args
*/
toolArgs?: ToolArgs
}

export interface ApiHandler {
Expand Down
111 changes: 111 additions & 0 deletions src/api/providers/__tests__/lmstudio.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,115 @@ describe("LmStudioHandler", () => {
expect(modelInfo.info.contextWindow).toBe(128_000)
})
})
describe("LmStudioHandler Tool Calling", () => {
let handler: LmStudioHandler
let mockOptions: ApiHandlerOptions

beforeEach(() => {
mockOptions = {
apiModelId: "local-model",
lmStudioModelId: "local-model",
lmStudioBaseUrl: "http://localhost:1234",
}
handler = new LmStudioHandler(mockOptions)
mockCreate.mockClear()
})

describe("createMessage with tool calls", () => {
const systemPrompt = "You are a helpful assistant."
const messages: Anthropic.Messages.MessageParam[] = [
{
role: "user",
content: "Hello!",
},
]

it("should include tool call parameters when tools are provided", async () => {
mockCreate.mockImplementation(async function* () {
yield {
choices: [
{
delta: { content: "Test response" },
index: 0,
},
],
usage: null,
}
})

const stream = handler.createMessage(systemPrompt, messages, {
tools: ["test_tool" as any],
taskId: "test-task-id",
})

// Consume the stream
for await (const _ of stream) {
//
}

expect(mockCreate).toHaveBeenCalledWith(
expect.objectContaining({
tools: expect.any(Array),
tool_choice: "auto",
}),
)
})

it("should yield tool_call chunks when model returns tool calls", async () => {
const toolCallChunk = {
choices: [
{
delta: {
tool_calls: [
{
index: 0,
id: "tool-call-1",
type: "function",
function: {
name: "test_tool",
arguments: '{"param1":"value1"}',
},
},
],
},
index: 0,
},
],
}
const finalChunk = {
choices: [
{
delta: {},
finish_reason: "tool_calls",
},
],
usage: {
prompt_tokens: 10,
completion_tokens: 5,
total_tokens: 15,
},
}

mockCreate.mockImplementation(async function* () {
yield toolCallChunk
yield finalChunk
})

const stream = handler.createMessage(systemPrompt, messages, {
tools: ["test_tool" as any],
taskId: "test-task-id",
})

const chunks: any[] = []
for await (const chunk of stream) {
chunks.push(chunk)
}

const toolCallChunks = chunks.filter((c) => c.type === "tool_call")
expect(toolCallChunks.length).toBe(1)
expect(toolCallChunks[0].toolCalls).toEqual(toolCallChunk.choices[0].delta.tool_calls)
expect(toolCallChunks[0].toolCallType).toBe("openai")
})
})
})
})
201 changes: 201 additions & 0 deletions src/api/providers/__tests__/openai-tool-call.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
// npx vitest run api/providers/__tests__/openai-tool-call.spec.ts

import { OpenAiHandler } from "../openai"
import { ApiHandlerOptions } from "../../../shared/api"
import OpenAI from "openai"
import { getToolRegistry } from "../../../core/prompts/tools/schemas/tool-registry"
import { ToolName } from "@roo-code/types"

const mockCreate = vitest.fn()
const mockGenerateFunctionCallSchemas = vitest.fn()

vitest.mock("openai", () => {
const mockConstructor = vitest.fn()
return {
__esModule: true,
default: mockConstructor.mockImplementation(() => ({
chat: {
completions: {
create: mockCreate,
},
},
})),
}
})

vitest.mock("../../../core/prompts/tools/schemas/tool-registry", () => ({
getToolRegistry: () => ({
generateFunctionCallSchemas: mockGenerateFunctionCallSchemas,
}),
}))

describe("OpenAiHandler Tool Call", () => {
let handler: OpenAiHandler
let mockOptions: ApiHandlerOptions

beforeEach(() => {
mockOptions = {
openAiApiKey: "test-api-key",
openAiModelId: "gpt-4",
openAiBaseUrl: "https://api.openai.com/v1",
}
handler = new OpenAiHandler(mockOptions)
mockCreate.mockClear()
mockGenerateFunctionCallSchemas.mockClear()
})

it("should include tools and tool_choice in the request when metadata.tools are provided", async () => {
const systemPrompt = "You are a helpful assistant."
const messages = [
{
role: "user" as const,
content: "Hello!",
},
]
const metadata = {
taskId: "test-task-id",
tools: ["read_file" as ToolName],
toolArgs: { cwd: ".", supportsComputerUse: true },
}

mockGenerateFunctionCallSchemas.mockReturnValue([
{
type: "function" as const,
function: {
name: "read_file",
description: "A function to interact with files.",
parameters: {},
},
},
])

mockCreate.mockImplementation(async function* () {
yield {
choices: [
{
delta: { content: "Test response" },
index: 0,
},
],
usage: null,
}
})

const stream = handler.createMessage(systemPrompt, messages, metadata)

for await (const _ of stream) {
// Consume stream
}

expect(mockCreate).toHaveBeenCalledWith(
expect.objectContaining({
tools: [
{
type: "function",
function: {
name: "read_file",
description: "A function to interact with files.",
parameters: {},
},
},
],
tool_choice: "auto",
}),
expect.any(Object),
)
})

it("should yield a tool_call event when the API returns tool_calls", async () => {
const systemPrompt = "You are a helpful assistant."
const messages = [
{
role: "user" as const,
content: "Hello!",
},
]
const metadata = {
taskId: "test-task-id",
tools: ["write_to_file" as ToolName],
toolArgs: { cwd: ".", supportsComputerUse: true },
}

mockCreate.mockImplementation(async function* () {
yield {
choices: [
{
delta: {
tool_calls: [
{
index: 0,
id: "call_123",
type: "function",
function: {
name: "write_to_file",
arguments: '{"query":"test"}',
},
},
],
},
index: 0,
},
],
}
})

const stream = handler.createMessage(systemPrompt, messages, metadata)
const chunks: any[] = []
for await (const chunk of stream) {
chunks.push(chunk)
}

const toolCallChunk = chunks.find((chunk) => chunk.type === "tool_call")

expect(toolCallChunk).toBeDefined()
expect(toolCallChunk.toolCalls).toEqual([
{
index: 0,
id: "call_123",
type: "function",
function: {
name: "write_to_file",
arguments: '{"query":"test"}',
},
},
])
})

it("should not include tools and tool_choice in the request when metadata.tools are not provided", async () => {
const systemPrompt = "You are a helpful assistant."
const messages = [
{
role: "user" as const,
content: "Hello!",
},
]

mockCreate.mockImplementation(async function* () {
yield {
choices: [
{
delta: { content: "Test response" },
index: 0,
},
],
usage: null,
}
})

const stream = handler.createMessage(systemPrompt, messages)
for await (const _ of stream) {
// Consume stream
}

expect(mockCreate).toHaveBeenCalledWith(
expect.not.objectContaining({
tools: expect.any(Array),
tool_choice: expect.any(String),
}),
expect.any(Object),
)
})
})
Loading
Loading