-
Notifications
You must be signed in to change notification settings - Fork 2.5k
feat: add thinking mode toggle for Z AI provider #8466
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,8 +8,12 @@ import { | |
| 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 type { ApiHandlerCreateMessageMetadata } from "../index" | ||
| import { convertToOpenAiMessages } from "../transform/openai-format" | ||
|
|
||
| import { BaseOpenAiCompatibleProvider } from "./base-openai-compatible-provider" | ||
|
|
||
|
|
@@ -29,4 +33,43 @@ export class ZAiHandler extends BaseOpenAiCompatibleProvider<InternationalZAiMod | |
| defaultTemperature: ZAI_DEFAULT_TEMPERATURE, | ||
| }) | ||
| } | ||
|
|
||
| protected override createStream( | ||
| systemPrompt: string, | ||
| messages: Anthropic.Messages.MessageParam[], | ||
| metadata?: ApiHandlerCreateMessageMetadata, | ||
| requestOptions?: OpenAI.RequestOptions, | ||
| ) { | ||
| const { | ||
| id: model, | ||
| info: { maxTokens: max_tokens }, | ||
| } = this.getModel() | ||
|
|
||
| const temperature = this.options.modelTemperature ?? ZAI_DEFAULT_TEMPERATURE | ||
|
|
||
| // Build base parameters | ||
| 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 for models that support it (GLM-4.6, etc.) | ||
| // Only add if explicitly enabled via the zaiEnableThinking setting | ||
| if (this.options.zaiEnableThinking === true) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P2] Scope 'enable_thinking' to supported models. As written, any model will receive the flag when the setting is on, which can trigger 400s for models that don't implement thinking mode. Consider gating by model id (e.g., /^glm-4.6/) or a capability map so unsupported models are unaffected. |
||
| // Z AI uses a custom parameter for thinking mode | ||
| // This follows the pattern used by other providers with thinking support | ||
| ;(params as any).enable_thinking = true | ||
| } | ||
|
|
||
| try { | ||
| return this.client.chat.completions.create(params, requestOptions) | ||
| } catch (error) { | ||
| const errorMessage = error instanceof Error ? error.message : String(error) | ||
| throw new Error(`Z AI completion error: ${errorMessage}`) | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| import { useCallback } from "react" | ||
| import { VSCodeTextField, VSCodeDropdown, VSCodeOption } from "@vscode/webview-ui-toolkit/react" | ||
| import { VSCodeTextField, VSCodeDropdown, VSCodeOption, VSCodeCheckbox } from "@vscode/webview-ui-toolkit/react" | ||
|
|
||
| import { zaiApiLineConfigs, zaiApiLineSchema, type ProviderSettings } from "@roo-code/types" | ||
|
|
||
|
|
@@ -28,6 +28,13 @@ export const ZAi = ({ apiConfiguration, setApiConfigurationField }: ZAiProps) => | |
| [setApiConfigurationField], | ||
| ) | ||
|
|
||
| const handleCheckboxChange = useCallback( | ||
| (field: keyof ProviderSettings) => () => { | ||
| setApiConfigurationField(field, !apiConfiguration?.[field]) | ||
| }, | ||
| [setApiConfigurationField, apiConfiguration], | ||
| ) | ||
|
|
||
| return ( | ||
| <> | ||
| <div> | ||
|
|
@@ -73,6 +80,16 @@ export const ZAi = ({ apiConfiguration, setApiConfigurationField }: ZAiProps) => | |
| </VSCodeButtonLink> | ||
| )} | ||
| </div> | ||
| <div> | ||
| <VSCodeCheckbox | ||
| checked={apiConfiguration?.zaiEnableThinking || false} | ||
| onChange={handleCheckboxChange("zaiEnableThinking")}> | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P3] Event-driven checkbox: Flipping based on current state can race with queued updates in React. Prefer using the event's checked value to set an explicit boolean, e.g. onChange={(e) => setApiConfigurationField('zaiEnableThinking', (e.target as HTMLInputElement).checked)}. |
||
| <label className="font-medium">{t("settings:providers.zaiEnableThinking")}</label> | ||
| </VSCodeCheckbox> | ||
| <div className="text-xs text-vscode-descriptionForeground mt-1 ml-6"> | ||
| {t("settings:providers.zaiEnableThinkingDescription")} | ||
| </div> | ||
| </div> | ||
| </> | ||
| ) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -302,6 +302,8 @@ | |
| "getZaiApiKey": "Get Z AI API Key", | ||
| "zaiEntrypoint": "Z AI Entrypoint", | ||
| "zaiEntrypointDescription": "Please select the appropriate API entrypoint based on your location. If you are in China, choose open.bigmodel.cn. Otherwise, choose api.z.ai.", | ||
| "zaiEnableThinking": "Enable Thinking Mode", | ||
| "zaiEnableThinkingDescription": "Enable thinking/reasoning mode for models that support it (e.g., GLM-4.6, DeepSeek v3.2). This allows models to show their reasoning process before providing answers.", | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P2] Provider-specific copy: This toggle is under the Z AI provider. Mentioning DeepSeek here can confuse users. Suggest: "Enable thinking/reasoning mode for supported Z AI models (e.g., GLM-4.6). This allows models to show their reasoning process before providing answers." |
||
| "geminiApiKey": "Gemini API Key", | ||
| "getGroqApiKey": "Get Groq API Key", | ||
| "groqApiKey": "Groq API Key", | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P3] Type-only import: This symbol is used purely for types. Importing it as a value pulls in '@anthropic-ai/sdk' at runtime unnecessarily.