Resolve merge conflict with origin/main in base-openai-compatible-provider.ts

This commit is contained in:
Roo Code 2025-11-21 22:03:33 +00:00
commit 76e2494b8b
16 changed files with 486 additions and 161 deletions

View file

@ -1,6 +1,6 @@
{
"name": "@roo-code/types",
"version": "1.86.0",
"version": "1.87.0",
"description": "TypeScript type definitions for Roo Code.",
"publishConfig": {
"access": "public",

View file

@ -380,7 +380,7 @@ describe("BaseOpenAiCompatibleProvider", () => {
const firstChunk = await stream.next()
expect(firstChunk.done).toBe(false)
expect(firstChunk.value).toEqual({ type: "usage", inputTokens: 100, outputTokens: 50 })
expect(firstChunk.value).toMatchObject({ type: "usage", inputTokens: 100, outputTokens: 50 })
})
})
})

View file

@ -123,11 +123,9 @@ describe("FeatherlessHandler", () => {
chunks.push(chunk)
}
expect(chunks).toEqual([
{ type: "reasoning", text: "Thinking..." },
{ type: "text", text: "Hello" },
{ type: "usage", inputTokens: 10, outputTokens: 5 },
])
expect(chunks[0]).toEqual({ type: "reasoning", text: "Thinking..." })
expect(chunks[1]).toEqual({ type: "text", text: "Hello" })
expect(chunks[2]).toMatchObject({ type: "usage", inputTokens: 10, outputTokens: 5 })
})
it("should fall back to base provider for non-DeepSeek models", async () => {
@ -145,10 +143,8 @@ describe("FeatherlessHandler", () => {
chunks.push(chunk)
}
expect(chunks).toEqual([
{ type: "text", text: "Test response" },
{ type: "usage", inputTokens: 10, outputTokens: 5 },
])
expect(chunks[0]).toEqual({ type: "text", text: "Test response" })
expect(chunks[1]).toMatchObject({ type: "usage", inputTokens: 10, outputTokens: 5 })
})
it("should return default model when no model is specified", () => {
@ -226,7 +222,7 @@ describe("FeatherlessHandler", () => {
const firstChunk = await stream.next()
expect(firstChunk.done).toBe(false)
expect(firstChunk.value).toEqual({ type: "usage", inputTokens: 10, outputTokens: 20 })
expect(firstChunk.value).toMatchObject({ type: "usage", inputTokens: 10, outputTokens: 20 })
})
it("createMessage should pass correct parameters to Featherless client for DeepSeek R1", async () => {

View file

@ -384,7 +384,7 @@ describe("FireworksHandler", () => {
const firstChunk = await stream.next()
expect(firstChunk.done).toBe(false)
expect(firstChunk.value).toEqual({ type: "usage", inputTokens: 10, outputTokens: 20 })
expect(firstChunk.value).toMatchObject({ type: "usage", inputTokens: 10, outputTokens: 20 })
})
it("createMessage should pass correct parameters to Fireworks client", async () => {
@ -494,10 +494,8 @@ describe("FireworksHandler", () => {
chunks.push(chunk)
}
expect(chunks).toEqual([
{ type: "text", text: "Hello" },
{ type: "text", text: " world" },
{ type: "usage", inputTokens: 5, outputTokens: 10 },
])
expect(chunks[0]).toEqual({ type: "text", text: "Hello" })
expect(chunks[1]).toEqual({ type: "text", text: " world" })
expect(chunks[2]).toMatchObject({ type: "usage", inputTokens: 5, outputTokens: 10 })
})
})

View file

@ -112,9 +112,10 @@ describe("GroqHandler", () => {
type: "usage",
inputTokens: 10,
outputTokens: 20,
cacheWriteTokens: 0,
cacheReadTokens: 0,
})
// cacheWriteTokens and cacheReadTokens will be undefined when 0
expect(firstChunk.value.cacheWriteTokens).toBeUndefined()
expect(firstChunk.value.cacheReadTokens).toBeUndefined()
// Check that totalCost is a number (we don't need to test the exact value as that's tested in cost.spec.ts)
expect(typeof firstChunk.value.totalCost).toBe("number")
})
@ -151,9 +152,10 @@ describe("GroqHandler", () => {
type: "usage",
inputTokens: 100,
outputTokens: 50,
cacheWriteTokens: 0,
cacheReadTokens: 30,
})
// cacheWriteTokens will be undefined when 0
expect(firstChunk.value.cacheWriteTokens).toBeUndefined()
expect(typeof firstChunk.value.totalCost).toBe("number")
})

View file

@ -178,7 +178,7 @@ describe("IOIntelligenceHandler", () => {
expect(results).toHaveLength(3)
expect(results[0]).toEqual({ type: "text", text: "Hello" })
expect(results[1]).toEqual({ type: "text", text: " world" })
expect(results[2]).toEqual({
expect(results[2]).toMatchObject({
type: "usage",
inputTokens: 10,
outputTokens: 5,
@ -243,7 +243,7 @@ describe("IOIntelligenceHandler", () => {
const firstChunk = await stream.next()
expect(firstChunk.done).toBe(false)
expect(firstChunk.value).toEqual({ type: "usage", inputTokens: 10, outputTokens: 20 })
expect(firstChunk.value).toMatchObject({ type: "usage", inputTokens: 10, outputTokens: 20 })
})
it("should return model info from cache when available", () => {

View file

@ -279,6 +279,56 @@ describe("OpenAiHandler", () => {
})
})
it("should yield tool calls even when finish_reason is not set (fallback behavior)", async () => {
mockCreate.mockImplementation(async (options) => {
return {
[Symbol.asyncIterator]: async function* () {
yield {
choices: [
{
delta: {
tool_calls: [
{
index: 0,
id: "call_fallback",
function: { name: "fallback_tool", arguments: '{"test":"fallback"}' },
},
],
},
finish_reason: null,
},
],
}
// Stream ends without finish_reason being set to "tool_calls"
yield {
choices: [
{
delta: {},
finish_reason: "stop", // Different finish reason
},
],
}
},
}
})
const stream = handler.createMessage(systemPrompt, messages)
const chunks: any[] = []
for await (const chunk of stream) {
chunks.push(chunk)
}
// Tool calls should still be yielded via the fallback mechanism
const toolCallChunks = chunks.filter((chunk) => chunk.type === "tool_call")
expect(toolCallChunks).toHaveLength(1)
expect(toolCallChunks[0]).toEqual({
type: "tool_call",
id: "call_fallback",
name: "fallback_tool",
arguments: '{"test":"fallback"}',
})
})
it("should include reasoning_effort when reasoning effort is enabled", async () => {
const reasoningOptions: ApiHandlerOptions = {
...mockOptions,
@ -779,6 +829,58 @@ describe("OpenAiHandler", () => {
})
})
it("should yield tool calls for O3 model even when finish_reason is not set (fallback behavior)", async () => {
const o3Handler = new OpenAiHandler(o3Options)
mockCreate.mockImplementation(async (options) => {
return {
[Symbol.asyncIterator]: async function* () {
yield {
choices: [
{
delta: {
tool_calls: [
{
index: 0,
id: "call_o3_fallback",
function: { name: "o3_fallback_tool", arguments: '{"o3":"test"}' },
},
],
},
finish_reason: null,
},
],
}
// Stream ends with different finish reason
yield {
choices: [
{
delta: {},
finish_reason: "length", // Different finish reason
},
],
}
},
}
})
const stream = o3Handler.createMessage("system", [])
const chunks: any[] = []
for await (const chunk of stream) {
chunks.push(chunk)
}
// Tool calls should still be yielded via the fallback mechanism
const toolCallChunks = chunks.filter((chunk) => chunk.type === "tool_call")
expect(toolCallChunks).toHaveLength(1)
expect(toolCallChunks[0]).toEqual({
type: "tool_call",
id: "call_o3_fallback",
name: "o3_fallback_tool",
arguments: '{"o3":"test"}',
})
})
it("should handle O3 model with streaming and exclude max_tokens when includeMaxTokens is false", async () => {
const o3Handler = new OpenAiHandler({
...o3Options,

View file

@ -630,4 +630,280 @@ describe("RooHandler", () => {
)
})
})
describe("tool calls handling", () => {
beforeEach(() => {
handler = new RooHandler(mockOptions)
})
it("should yield tool calls when finish_reason is tool_calls", async () => {
mockCreate.mockResolvedValueOnce({
[Symbol.asyncIterator]: async function* () {
yield {
choices: [
{
delta: {
tool_calls: [
{
index: 0,
id: "call_123",
function: { name: "read_file", arguments: '{"path":"' },
},
],
},
index: 0,
},
],
}
yield {
choices: [
{
delta: {
tool_calls: [
{
index: 0,
function: { arguments: 'test.ts"}' },
},
],
},
index: 0,
},
],
}
yield {
choices: [
{
delta: {},
finish_reason: "tool_calls",
index: 0,
},
],
usage: { prompt_tokens: 10, completion_tokens: 5, total_tokens: 15 },
}
},
})
const stream = handler.createMessage(systemPrompt, messages)
const chunks: any[] = []
for await (const chunk of stream) {
chunks.push(chunk)
}
const toolCallChunks = chunks.filter((chunk) => chunk.type === "tool_call")
expect(toolCallChunks).toHaveLength(1)
expect(toolCallChunks[0].id).toBe("call_123")
expect(toolCallChunks[0].name).toBe("read_file")
expect(toolCallChunks[0].arguments).toBe('{"path":"test.ts"}')
})
it("should yield tool calls even when finish_reason is not set (fallback behavior)", async () => {
mockCreate.mockResolvedValueOnce({
[Symbol.asyncIterator]: async function* () {
yield {
choices: [
{
delta: {
tool_calls: [
{
index: 0,
id: "call_456",
function: {
name: "write_to_file",
arguments: '{"path":"test.ts","content":"hello"}',
},
},
],
},
index: 0,
},
],
}
// Stream ends without finish_reason being set to "tool_calls"
yield {
choices: [
{
delta: {},
finish_reason: "stop", // Different finish reason
index: 0,
},
],
usage: { prompt_tokens: 10, completion_tokens: 5, total_tokens: 15 },
}
},
})
const stream = handler.createMessage(systemPrompt, messages)
const chunks: any[] = []
for await (const chunk of stream) {
chunks.push(chunk)
}
// Tool calls should still be yielded via the fallback mechanism
const toolCallChunks = chunks.filter((chunk) => chunk.type === "tool_call")
expect(toolCallChunks).toHaveLength(1)
expect(toolCallChunks[0].id).toBe("call_456")
expect(toolCallChunks[0].name).toBe("write_to_file")
expect(toolCallChunks[0].arguments).toBe('{"path":"test.ts","content":"hello"}')
})
it("should handle multiple tool calls", async () => {
mockCreate.mockResolvedValueOnce({
[Symbol.asyncIterator]: async function* () {
yield {
choices: [
{
delta: {
tool_calls: [
{
index: 0,
id: "call_1",
function: { name: "read_file", arguments: '{"path":"file1.ts"}' },
},
],
},
index: 0,
},
],
}
yield {
choices: [
{
delta: {
tool_calls: [
{
index: 1,
id: "call_2",
function: { name: "read_file", arguments: '{"path":"file2.ts"}' },
},
],
},
index: 0,
},
],
}
yield {
choices: [
{
delta: {},
finish_reason: "tool_calls",
index: 0,
},
],
usage: { prompt_tokens: 10, completion_tokens: 5, total_tokens: 15 },
}
},
})
const stream = handler.createMessage(systemPrompt, messages)
const chunks: any[] = []
for await (const chunk of stream) {
chunks.push(chunk)
}
const toolCallChunks = chunks.filter((chunk) => chunk.type === "tool_call")
expect(toolCallChunks).toHaveLength(2)
expect(toolCallChunks[0].id).toBe("call_1")
expect(toolCallChunks[0].name).toBe("read_file")
expect(toolCallChunks[1].id).toBe("call_2")
expect(toolCallChunks[1].name).toBe("read_file")
})
it("should accumulate tool call arguments across multiple chunks", async () => {
mockCreate.mockResolvedValueOnce({
[Symbol.asyncIterator]: async function* () {
yield {
choices: [
{
delta: {
tool_calls: [
{
index: 0,
id: "call_789",
function: { name: "execute_command", arguments: '{"command":"' },
},
],
},
index: 0,
},
],
}
yield {
choices: [
{
delta: {
tool_calls: [
{
index: 0,
function: { arguments: "npm install" },
},
],
},
index: 0,
},
],
}
yield {
choices: [
{
delta: {
tool_calls: [
{
index: 0,
function: { arguments: '"}' },
},
],
},
index: 0,
},
],
}
yield {
choices: [
{
delta: {},
finish_reason: "tool_calls",
index: 0,
},
],
usage: { prompt_tokens: 10, completion_tokens: 5, total_tokens: 15 },
}
},
})
const stream = handler.createMessage(systemPrompt, messages)
const chunks: any[] = []
for await (const chunk of stream) {
chunks.push(chunk)
}
const toolCallChunks = chunks.filter((chunk) => chunk.type === "tool_call")
expect(toolCallChunks).toHaveLength(1)
expect(toolCallChunks[0].id).toBe("call_789")
expect(toolCallChunks[0].name).toBe("execute_command")
expect(toolCallChunks[0].arguments).toBe('{"command":"npm install"}')
})
it("should not yield empty tool calls when no tool calls present", async () => {
mockCreate.mockResolvedValueOnce({
[Symbol.asyncIterator]: async function* () {
yield {
choices: [{ delta: { content: "Regular text response" }, index: 0 }],
}
yield {
choices: [{ delta: {}, finish_reason: "stop", index: 0 }],
usage: { prompt_tokens: 10, completion_tokens: 5, total_tokens: 15 },
}
},
})
const stream = handler.createMessage(systemPrompt, messages)
const chunks: any[] = []
for await (const chunk of stream) {
chunks.push(chunk)
}
const toolCallChunks = chunks.filter((chunk) => chunk.type === "tool_call")
expect(toolCallChunks).toHaveLength(0)
})
})
})

View file

@ -113,7 +113,7 @@ describe("SambaNovaHandler", () => {
const firstChunk = await stream.next()
expect(firstChunk.done).toBe(false)
expect(firstChunk.value).toEqual({ type: "usage", inputTokens: 10, outputTokens: 20 })
expect(firstChunk.value).toMatchObject({ type: "usage", inputTokens: 10, outputTokens: 20 })
})
it("createMessage should pass correct parameters to SambaNova client", async () => {

View file

@ -252,7 +252,7 @@ describe("ZAiHandler", () => {
const firstChunk = await stream.next()
expect(firstChunk.done).toBe(false)
expect(firstChunk.value).toEqual({ type: "usage", inputTokens: 10, outputTokens: 20 })
expect(firstChunk.value).toMatchObject({ type: "usage", inputTokens: 10, outputTokens: 20 })
})
it("createMessage should pass correct parameters to Z AI client", async () => {

View file

@ -95,6 +95,11 @@ export abstract class BaseOpenAiCompatibleProvider<ModelName extends string>
...(metadata?.tool_choice && { tool_choice: metadata.tool_choice }),
}
// Add thinking parameter if reasoning is enabled and model supports it
if (this.options.enableReasoningEffort && info.supportsReasoningBinary) {
;(params as any).thinking = { type: "enabled" }
}
try {
return this.client.chat.completions.create(params, requestOptions)
} catch (error) {
@ -188,6 +193,20 @@ export abstract class BaseOpenAiCompatibleProvider<ModelName extends string>
}
}
// Fallback: If stream ends with accumulated tool calls that weren't yielded
// (e.g., finish_reason was 'stop' or 'length' instead of 'tool_calls')
if (toolCallAccumulator.size > 0) {
for (const toolCall of toolCallAccumulator.values()) {
yield {
type: "tool_call",
id: toolCall.id,
name: toolCall.name,
arguments: toolCall.arguments,
}
}
toolCallAccumulator.clear()
}
if (lastUsage) {
yield this.processUsageMetrics(lastUsage, this.getModel().info)
}
@ -219,13 +238,20 @@ export abstract class BaseOpenAiCompatibleProvider<ModelName extends string>
}
async completePrompt(prompt: string): Promise<string> {
const { id: modelId } = this.getModel()
const { id: modelId, info: modelInfo } = this.getModel()
const params: OpenAI.Chat.Completions.ChatCompletionCreateParams = {
model: modelId,
messages: [{ role: "user", content: prompt }],
}
// Add thinking parameter if reasoning is enabled and model supports it
if (this.options.enableReasoningEffort && modelInfo.supportsReasoningBinary) {
;(params as any).thinking = { type: "enabled" }
}
try {
const response = await this.client.chat.completions.create({
model: modelId,
messages: [{ role: "user", content: prompt }],
})
const response = await this.client.chat.completions.create(params)
// Check for provider-specific error responses (e.g., MiniMax base_resp)
const responseAny = response as any

View file

@ -1,22 +1,9 @@
import { type GroqModelId, groqDefaultModelId, groqModels } from "@roo-code/types"
import { Anthropic } from "@anthropic-ai/sdk"
import OpenAI from "openai"
import type { ApiHandlerOptions } from "../../shared/api"
import type { ApiHandlerCreateMessageMetadata } from "../index"
import { ApiStream } from "../transform/stream"
import { convertToOpenAiMessages } from "../transform/openai-format"
import { calculateApiCostOpenAI } from "../../shared/cost"
import { BaseOpenAiCompatibleProvider } from "./base-openai-compatible-provider"
// Enhanced usage interface to support Groq's cached token fields
interface GroqUsage extends OpenAI.CompletionUsage {
prompt_tokens_details?: {
cached_tokens?: number
}
}
export class GroqHandler extends BaseOpenAiCompatibleProvider<GroqModelId> {
constructor(options: ApiHandlerOptions) {
super({
@ -29,50 +16,4 @@ export class GroqHandler extends BaseOpenAiCompatibleProvider<GroqModelId> {
defaultTemperature: 0.5,
})
}
override async *createMessage(
systemPrompt: string,
messages: Anthropic.Messages.MessageParam[],
metadata?: ApiHandlerCreateMessageMetadata,
): ApiStream {
const stream = await this.createStream(systemPrompt, messages, metadata)
for await (const chunk of stream) {
const delta = chunk.choices[0]?.delta
if (delta?.content) {
yield {
type: "text",
text: delta.content,
}
}
if (chunk.usage) {
yield* this.yieldUsage(chunk.usage as GroqUsage)
}
}
}
private async *yieldUsage(usage: GroqUsage | undefined): ApiStream {
const { info } = this.getModel()
const inputTokens = usage?.prompt_tokens || 0
const outputTokens = usage?.completion_tokens || 0
const cacheReadTokens = usage?.prompt_tokens_details?.cached_tokens || 0
// Groq does not track cache writes
const cacheWriteTokens = 0
// Calculate cost using OpenAI-compatible cost calculation
const { totalCost } = calculateApiCostOpenAI(info, inputTokens, outputTokens, cacheWriteTokens, cacheReadTokens)
yield {
type: "usage",
inputTokens,
outputTokens,
cacheWriteTokens,
cacheReadTokens,
totalCost,
}
}
}

View file

@ -246,6 +246,20 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl
}
}
// Fallback: If stream ends with accumulated tool calls that weren't yielded
// (e.g., finish_reason was 'stop' or 'length' instead of 'tool_calls')
if (toolCallAccumulator.size > 0) {
for (const toolCall of toolCallAccumulator.values()) {
yield {
type: "tool_call",
id: toolCall.id,
name: toolCall.name,
arguments: toolCall.arguments,
}
}
toolCallAccumulator.clear()
}
for (const chunk of matcher.final()) {
yield chunk
}
@ -506,6 +520,20 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl
}
}
}
// Fallback: If stream ends with accumulated tool calls that weren't yielded
// (e.g., finish_reason was 'stop' or 'length' instead of 'tool_calls')
if (toolCallAccumulator.size > 0) {
for (const toolCall of toolCallAccumulator.values()) {
yield {
type: "tool_call",
id: toolCall.id,
name: toolCall.name,
arguments: toolCall.arguments,
}
}
toolCallAccumulator.clear()
}
}
private _getUrlHost(baseUrl?: string): string {

View file

@ -265,6 +265,20 @@ export class OpenRouterHandler extends BaseProvider implements SingleCompletionH
}
}
// Fallback: If stream ends with accumulated tool calls that weren't yielded
// (e.g., finish_reason was 'stop' or 'length' instead of 'tool_calls')
if (toolCallAccumulator.size > 0) {
for (const toolCall of toolCallAccumulator.values()) {
yield {
type: "tool_call",
id: toolCall.id,
name: toolCall.name,
arguments: toolCall.arguments,
}
}
toolCallAccumulator.clear()
}
if (lastUsage) {
yield {
type: "usage",

View file

@ -199,6 +199,20 @@ export class RooHandler extends BaseOpenAiCompatibleProvider<string> {
}
}
// Fallback: If stream ends with accumulated tool calls that weren't yielded
// (e.g., finish_reason was 'stop' or 'length' instead of 'tool_calls')
if (toolCallAccumulator.size > 0) {
for (const [index, toolCall] of toolCallAccumulator.entries()) {
yield {
type: "tool_call",
id: toolCall.id,
name: toolCall.name,
arguments: toolCall.arguments,
}
}
toolCallAccumulator.clear()
}
if (lastUsage) {
// Check if the current model is marked as free
const model = this.getModel()

View file

@ -3,21 +3,12 @@ import {
mainlandZAiModels,
internationalZAiDefaultModelId,
mainlandZAiDefaultModelId,
type InternationalZAiModelId,
type MainlandZAiModelId,
type ModelInfo,
ZAI_DEFAULT_TEMPERATURE,
zaiApiLineConfigs,
} from "@roo-code/types"
import { Anthropic } from "@anthropic-ai/sdk"
import OpenAI from "openai"
import type { ApiHandlerOptions } from "../../shared/api"
import { getModelMaxOutputTokens } from "../../shared/api"
import { convertToOpenAiMessages } from "../transform/openai-format"
import type { ApiHandlerCreateMessageMetadata } from "../index"
import { handleOpenAIError } from "./utils/openai-error-handler"
import { BaseOpenAiCompatibleProvider } from "./base-openai-compatible-provider"
@ -37,67 +28,4 @@ export class ZAiHandler extends BaseOpenAiCompatibleProvider<string> {
defaultTemperature: ZAI_DEFAULT_TEMPERATURE,
})
}
protected override createStream(
systemPrompt: string,
messages: Anthropic.Messages.MessageParam[],
metadata?: ApiHandlerCreateMessageMetadata,
requestOptions?: OpenAI.RequestOptions,
) {
const { id: model, info } = this.getModel()
// Centralized cap: clamp to 20% of the context window (unless provider-specific exceptions apply)
const max_tokens =
getModelMaxOutputTokens({
modelId: model,
model: info,
settings: this.options,
format: "openai",
}) ?? undefined
const temperature = this.options.modelTemperature ?? this.defaultTemperature
const params: OpenAI.Chat.Completions.ChatCompletionCreateParamsStreaming = {
model,
max_tokens,
temperature,
messages: [{ role: "system", content: systemPrompt }, ...convertToOpenAiMessages(messages)],
stream: true,
stream_options: { include_usage: true },
}
// Add thinking parameter if reasoning is enabled and model supports it
const { id: modelId, info: modelInfo } = this.getModel()
if (this.options.enableReasoningEffort && modelInfo.supportsReasoningBinary) {
;(params as any).thinking = { type: "enabled" }
}
try {
return this.client.chat.completions.create(params, requestOptions)
} catch (error) {
throw handleOpenAIError(error, this.providerName)
}
}
override async completePrompt(prompt: string): Promise<string> {
const { id: modelId } = this.getModel()
const params: OpenAI.Chat.Completions.ChatCompletionCreateParams = {
model: modelId,
messages: [{ role: "user", content: prompt }],
}
// Add thinking parameter if reasoning is enabled and model supports it
const { info: modelInfo } = this.getModel()
if (this.options.enableReasoningEffort && modelInfo.supportsReasoningBinary) {
;(params as any).thinking = { type: "enabled" }
}
try {
const response = await this.client.chat.completions.create(params)
return response.choices[0]?.message.content || ""
} catch (error) {
throw handleOpenAIError(error, this.providerName)
}
}
}