Skip to content
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
33 changes: 28 additions & 5 deletions src/api/providers/base-openai-compatible-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import OpenAI from "openai"

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

import { type ApiHandlerOptions, getModelMaxOutputTokens } from "../../shared/api"
import { type ApiHandlerOptions, getModelMaxOutputTokens, shouldUseReasoningEffort } from "../../shared/api"
import { TagMatcher } from "../../utils/tag-matcher"
import { ApiStream, ApiStreamUsageChunk } from "../transform/stream"
import { convertToOpenAiMessages } from "../transform/openai-format"
Expand All @@ -14,6 +14,7 @@ import { BaseProvider } from "./base-provider"
import { handleOpenAIError } from "./utils/openai-error-handler"
import { calculateApiCostOpenAI } from "../../shared/cost"
import { getApiRequestTimeout } from "./utils/timeout-config"
import { getGlmModelOptions } from "./utils/glm-model-detection"

type BaseOpenAiCompatibleProviderOptions<ModelName extends string> = ApiHandlerOptions & {
providerName: string
Expand All @@ -23,6 +24,11 @@ type BaseOpenAiCompatibleProviderOptions<ModelName extends string> = ApiHandlerO
defaultTemperature?: number
}

// Extended chat completion params to support thinking mode for GLM-4.7+
type ChatCompletionParamsWithThinking = OpenAI.Chat.Completions.ChatCompletionCreateParamsStreaming & {
thinking?: { type: "enabled" | "disabled" }
}

export abstract class BaseOpenAiCompatibleProvider<ModelName extends string>
extends BaseProvider
implements SingleCompletionHandler
Expand Down Expand Up @@ -75,6 +81,9 @@ export abstract class BaseOpenAiCompatibleProvider<ModelName extends string>
) {
const { id: model, info } = this.getModel()

// Check if this is a GLM model and get recommended options
const glmOptions = getGlmModelOptions(model)

// Centralized cap: clamp to 20% of the context window (unless provider-specific exceptions apply)
const max_tokens =
getModelMaxOutputTokens({
Expand All @@ -86,21 +95,35 @@ export abstract class BaseOpenAiCompatibleProvider<ModelName extends string>

const temperature = this.options.modelTemperature ?? info.defaultTemperature ?? this.defaultTemperature

const params: OpenAI.Chat.Completions.ChatCompletionCreateParamsStreaming = {
// For GLM models, disable parallel_tool_calls as they may not support it
const parallelToolCalls = glmOptions?.disableParallelToolCalls ? false : (metadata?.parallelToolCalls ?? true)

const params: ChatCompletionParamsWithThinking = {
model,
max_tokens,
temperature,
messages: [{ role: "system", content: systemPrompt }, ...convertToOpenAiMessages(messages)],
messages: [
{ role: "system", content: systemPrompt },
...convertToOpenAiMessages(messages, {
mergeToolResultText: glmOptions?.mergeToolResultText ?? false,
}),
],
stream: true,
stream_options: { include_usage: true },
tools: this.convertToolsForOpenAI(metadata?.tools),
tool_choice: metadata?.tool_choice,
parallel_tool_calls: metadata?.parallelToolCalls ?? true,
parallel_tool_calls: parallelToolCalls,
}

// Add thinking parameter if reasoning is enabled and model supports it
if (this.options.enableReasoningEffort && info.supportsReasoningBinary) {
;(params as any).thinking = { type: "enabled" }
params.thinking = { type: "enabled" }
}

// For GLM-4.7+ models, add thinking mode support similar to Z.ai
if (glmOptions?.supportsThinking) {
const useReasoning = shouldUseReasoningEffort({ model: info, settings: this.options })
params.thinking = useReasoning ? { type: "enabled" } : { type: "disabled" }
}
Comment on lines +123 to 127
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same issue as in lm-studio.ts: shouldUseReasoningEffort() will always return false for models that use default model info (e.g., dynamic providers) because openAiModelInfoSaneDefaults lacks supportsReasoningEffort and reasoningEffort properties. The thinking parameter will always be set to disabled for GLM-4.7+ models using generic OpenAI-compatible providers.

Fix it with Roo Code or mention @roomote and request a fix.


try {
Expand Down
35 changes: 30 additions & 5 deletions src/api/providers/lm-studio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import axios from "axios"

import { type ModelInfo, openAiModelInfoSaneDefaults, LMSTUDIO_DEFAULT_TEMPERATURE } from "@roo-code/types"

import type { ApiHandlerOptions } from "../../shared/api"
import { type ApiHandlerOptions, shouldUseReasoningEffort } from "../../shared/api"

import { NativeToolCallParser } from "../../core/assistant-message/NativeToolCallParser"
import { TagMatcher } from "../../utils/tag-matcher"
Expand All @@ -17,6 +17,13 @@ import type { SingleCompletionHandler, ApiHandlerCreateMessageMetadata } from ".
import { getModelsFromCache } from "./fetchers/modelCache"
import { getApiRequestTimeout } from "./utils/timeout-config"
import { handleOpenAIError } from "./utils/openai-error-handler"
import { getGlmModelOptions } from "./utils/glm-model-detection"

// Extended chat completion params to support thinking mode for GLM-4.7+
type ChatCompletionParamsWithThinking = OpenAI.Chat.ChatCompletionCreateParamsStreaming & {
thinking?: { type: "enabled" | "disabled" }
draft_model?: string
}

export class LmStudioHandler extends BaseProvider implements SingleCompletionHandler {
protected options: ApiHandlerOptions
Expand All @@ -42,9 +49,16 @@ export class LmStudioHandler extends BaseProvider implements SingleCompletionHan
messages: Anthropic.Messages.MessageParam[],
metadata?: ApiHandlerCreateMessageMetadata,
): ApiStream {
const { id: modelId, info: modelInfo } = this.getModel()

// Check if this is a GLM model and get recommended options
const glmOptions = getGlmModelOptions(modelId)

const openAiMessages: OpenAI.Chat.ChatCompletionMessageParam[] = [
{ role: "system", content: systemPrompt },
...convertToOpenAiMessages(messages),
...convertToOpenAiMessages(messages, {
mergeToolResultText: glmOptions?.mergeToolResultText ?? false,
}),
]

// -------------------------
Expand Down Expand Up @@ -83,20 +97,31 @@ export class LmStudioHandler extends BaseProvider implements SingleCompletionHan
let assistantText = ""

try {
const params: OpenAI.Chat.ChatCompletionCreateParamsStreaming & { draft_model?: string } = {
model: this.getModel().id,
// For GLM models, disable parallel_tool_calls as they may not support it
const parallelToolCalls = glmOptions?.disableParallelToolCalls
? false
: (metadata?.parallelToolCalls ?? true)

const params: ChatCompletionParamsWithThinking = {
model: modelId,
messages: openAiMessages,
temperature: this.options.modelTemperature ?? LMSTUDIO_DEFAULT_TEMPERATURE,
stream: true,
tools: this.convertToolsForOpenAI(metadata?.tools),
tool_choice: metadata?.tool_choice,
parallel_tool_calls: metadata?.parallelToolCalls ?? true,
parallel_tool_calls: parallelToolCalls,
}

if (this.options.lmStudioSpeculativeDecodingEnabled && this.options.lmStudioDraftModelId) {
params.draft_model = this.options.lmStudioDraftModelId
}

// For GLM-4.7+ models, add thinking mode support similar to Z.ai
if (glmOptions?.supportsThinking) {
const useReasoning = shouldUseReasoningEffort({ model: modelInfo, settings: this.options })
params.thinking = useReasoning ? { type: "enabled" } : { type: "disabled" }
}
Comment on lines +119 to +123
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldUseReasoningEffort() will always return false for LM Studio models because modelInfo comes from either the model cache or openAiModelInfoSaneDefaults, neither of which have supportsReasoningEffort or reasoningEffort properties. The function's fallback logic checks !!modelDefaultEffort, which will be undefined for these models. This means thinking mode will never be enabled - the code will always send thinking: { type: "disabled" }. Consider either: (1) not relying on shouldUseReasoningEffort() for detected GLM-4.7+ models and instead default to enabled unless user explicitly disables via enableReasoningEffort: false, or (2) have getGlmModelOptions() return synthetic reasoning capability info that can be used instead of/in addition to the model info.

Fix it with Roo Code or mention @roomote and request a fix.


let results
try {
results = await this.client.chat.completions.create(params)
Expand Down
Loading
Loading