mirror of
https://github.com/RooVetGit/Roo-Code.git
synced 2026-09-06 08:18:39 +00:00
* feat: Add supportsReasoning property for Z.ai GLM binary thinking mode - Add supportsReasoning to ModelInfo schema for binary reasoning models - Update GLM-4.5 and GLM-4.6 models to use supportsReasoning: true - Implement thinking parameter support in ZAiHandler for Deep Thinking API - Update ThinkingBudget component to show simple toggle for supportsReasoning models - Add comprehensive tests for binary reasoning functionality Closes #8465 * refactor: rename supportsReasoning to supportsReasoningBinary for clarity - Rename supportsReasoning -> supportsReasoningBinary in model schema - Update Z.AI GLM model configurations to use supportsReasoningBinary - Update Z.AI provider logic in createStream and completePrompt methods - Update ThinkingBudget UI component and tests - Update all test comments and expectations This change improves naming clarity by distinguishing between: - supportsReasoningBinary: Simple on/off reasoning toggle - supportsReasoningBudget: Advanced reasoning with token budget controls - supportsReasoningEffort: Advanced reasoning with effort levels
103 lines
3.4 KiB
TypeScript
103 lines
3.4 KiB
TypeScript
import {
|
|
internationalZAiModels,
|
|
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"
|
|
|
|
export class ZAiHandler extends BaseOpenAiCompatibleProvider<string> {
|
|
constructor(options: ApiHandlerOptions) {
|
|
const isChina = zaiApiLineConfigs[options.zaiApiLine ?? "international_coding"].isChina
|
|
const models = (isChina ? mainlandZAiModels : internationalZAiModels) as unknown as Record<string, ModelInfo>
|
|
const defaultModelId = (isChina ? mainlandZAiDefaultModelId : internationalZAiDefaultModelId) as string
|
|
|
|
super({
|
|
...options,
|
|
providerName: "Z AI",
|
|
baseURL: zaiApiLineConfigs[options.zaiApiLine ?? "international_coding"].baseUrl,
|
|
apiKey: options.zaiApiKey ?? "not-provided",
|
|
defaultProviderModelId: defaultModelId,
|
|
providerModels: models,
|
|
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)
|
|
}
|
|
}
|
|
}
|