feat: add GLM Coding Plan toggle for Z AI provider

- Add zaiUseGlmCodingPlan field to provider settings schema
- Update ZAiHandler to route requests through coding plan endpoints when enabled
- Add UI toggle in Z AI settings with appropriate labels and descriptions
- Add comprehensive tests for the new functionality
- Add English translation keys for the new toggle

This allows users with a Z AI GLM Coding Plan to use their plan instead of API credits
This commit is contained in:
Roo Code 2025-09-15 20:03:00 +00:00
parent 94b4511053
commit 5f6f2be830
5 changed files with 49 additions and 2 deletions

View file

@ -311,6 +311,7 @@ const sambaNovaSchema = apiModelIdProviderModelSchema.extend({
const zaiSchema = apiModelIdProviderModelSchema.extend({
zaiApiKey: z.string().optional(),
zaiApiLine: z.union([z.literal("china"), z.literal("international")]).optional(),
zaiUseGlmCodingPlan: z.boolean().optional(),
})
const fireworksSchema = apiModelIdProviderModelSchema.extend({

View file

@ -44,6 +44,13 @@ describe("ZAiHandler", () => {
expect(OpenAI).toHaveBeenCalledWith(expect.objectContaining({ baseURL: "https://api.z.ai/api/paas/v4" }))
})
it("should use the international GLM Coding Plan URL when toggle is enabled", () => {
new ZAiHandler({ zaiApiKey: "test-zai-api-key", zaiApiLine: "international", zaiUseGlmCodingPlan: true })
expect(OpenAI).toHaveBeenCalledWith(
expect.objectContaining({ baseURL: "https://api.z.ai/api/coding/paas/v4" }),
)
})
it("should use the provided API key for international", () => {
const zaiApiKey = "test-zai-api-key"
new ZAiHandler({ zaiApiKey, zaiApiLine: "international" })
@ -81,6 +88,13 @@ describe("ZAiHandler", () => {
)
})
it("should use the China GLM Coding Plan URL when toggle is enabled", () => {
new ZAiHandler({ zaiApiKey: "test-zai-api-key", zaiApiLine: "china", zaiUseGlmCodingPlan: true })
expect(OpenAI).toHaveBeenCalledWith(
expect.objectContaining({ baseURL: "https://open.bigmodel.cn/api/coding/paas/v4" }),
)
})
it("should use the provided API key for China", () => {
const zaiApiKey = "test-zai-api-key"
new ZAiHandler({ zaiApiKey, zaiApiLine: "china" })
@ -120,6 +134,16 @@ describe("ZAiHandler", () => {
new ZAiHandler({ zaiApiLine: "international" })
expect(OpenAI).toHaveBeenCalledWith(expect.objectContaining({ apiKey: "not-provided" }))
})
it("should use standard URL when GLM Coding Plan toggle is false", () => {
new ZAiHandler({ zaiApiKey: "test-zai-api-key", zaiApiLine: "international", zaiUseGlmCodingPlan: false })
expect(OpenAI).toHaveBeenCalledWith(expect.objectContaining({ baseURL: "https://api.z.ai/api/paas/v4" }))
})
it("should use standard URL when GLM Coding Plan toggle is undefined", () => {
new ZAiHandler({ zaiApiKey: "test-zai-api-key", zaiApiLine: "international" })
expect(OpenAI).toHaveBeenCalledWith(expect.objectContaining({ baseURL: "https://api.z.ai/api/paas/v4" }))
})
})
describe("API Methods", () => {

View file

@ -18,10 +18,20 @@ export class ZAiHandler extends BaseOpenAiCompatibleProvider<InternationalZAiMod
const models = isChina ? mainlandZAiModels : internationalZAiModels
const defaultModelId = isChina ? mainlandZAiDefaultModelId : internationalZAiDefaultModelId
// Determine the base URL based on region and GLM Coding Plan toggle
let baseURL: string
if (options.zaiUseGlmCodingPlan) {
// Use coding plan endpoints
baseURL = isChina ? "https://open.bigmodel.cn/api/coding/paas/v4" : "https://api.z.ai/api/coding/paas/v4"
} else {
// Use standard endpoints
baseURL = isChina ? "https://open.bigmodel.cn/api/paas/v4" : "https://api.z.ai/api/paas/v4"
}
super({
...options,
providerName: "Z AI",
baseURL: isChina ? "https://open.bigmodel.cn/api/paas/v4" : "https://api.z.ai/api/paas/v4",
baseURL,
apiKey: options.zaiApiKey ?? "not-provided",
defaultProviderModelId: defaultModelId,
providerModels: models,

View file

@ -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 type { ProviderSettings } from "@roo-code/types"
@ -71,6 +71,16 @@ export const ZAi = ({ apiConfiguration, setApiConfigurationField }: ZAiProps) =>
</VSCodeButtonLink>
)}
</div>
<div>
<VSCodeCheckbox
checked={apiConfiguration?.zaiUseGlmCodingPlan || false}
onChange={(e: any) => setApiConfigurationField("zaiUseGlmCodingPlan", e.target.checked)}>
{t("settings:providers.zaiUseGlmCodingPlan")}
</VSCodeCheckbox>
<div className="text-xs text-vscode-descriptionForeground mt-1">
{t("settings:providers.zaiUseGlmCodingPlanDescription")}
</div>
</div>
</>
)
}

View file

@ -294,6 +294,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.",
"zaiUseGlmCodingPlan": "Use GLM Coding Plan",
"zaiUseGlmCodingPlanDescription": "Route requests through GLM Coding Plan endpoints to use your plan instead of API credits. When plan limits are reached, disable this toggle to continue with API credits.",
"geminiApiKey": "Gemini API Key",
"getGroqApiKey": "Get Groq API Key",
"groqApiKey": "Groq API Key",