Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/zai-glm5-default-lines.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"kilo-code": patch
---

Add support for GLM 5 and set Z.ai default to `glm-5` and align Z.ai API line model selection in VS Code and webview settings
41 changes: 39 additions & 2 deletions packages/types/src/providers/zai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import { ZaiApiLine } from "../provider-settings.js"
// https://docs.z.ai/guides/llm/glm-4.5
// https://docs.z.ai/guides/llm/glm-4.6
// https://docs.z.ai/guides/llm/glm-4.7
// https://docs.z.ai/guides/llm/glm-5 // kilocode_change
// https://docs.z.ai/guides/overview/pricing
// https://bigmodel.cn/pricing

export type InternationalZAiModelId = keyof typeof internationalZAiModels
export const internationalZAiDefaultModelId: InternationalZAiModelId = "glm-4.7"
export const internationalZAiDefaultModelId: InternationalZAiModelId = "glm-5" // kilocode_change
export const internationalZAiModels = {
"glm-4.5": {
maxTokens: 16_384,
Expand Down Expand Up @@ -157,6 +158,24 @@ export const internationalZAiModels = {
preferredIndex: 1,
},
// kilocode_change start
"glm-5": {
maxTokens: 131_072,
contextWindow: 200_000,
supportsImages: false,
supportsPromptCache: true,
supportsNativeTools: true,
defaultToolProtocol: "native",
supportsReasoningEffort: ["disable", "medium"],
reasoningEffort: "medium",
preserveReasoning: true,
inputPrice: 1,
outputPrice: 3.2,
cacheWritesPrice: 0,
cacheReadsPrice: 0.2,
description:
"GLM-5 is Z.AI's flagship text model with 200K context, 128K max output, thinking mode, function calling, and context caching.",
preferredIndex: 0,
},
"glm-4.7-flash": {
maxTokens: 16_384,
contextWindow: 200_000,
Expand Down Expand Up @@ -189,7 +208,7 @@ export const internationalZAiModels = {
} as const satisfies Record<string, ModelInfo>

export type MainlandZAiModelId = keyof typeof mainlandZAiModels
export const mainlandZAiDefaultModelId: MainlandZAiModelId = "glm-4.7"
export const mainlandZAiDefaultModelId: MainlandZAiModelId = "glm-5" // kilocode_change
export const mainlandZAiModels = {
"glm-4.5": {
maxTokens: 16_384,
Expand Down Expand Up @@ -306,6 +325,24 @@ export const mainlandZAiModels = {
preferredIndex: 1,
},
// kilocode_change start
"glm-5": {
maxTokens: 131_072,
contextWindow: 200_000,
supportsImages: false,
supportsPromptCache: true,
supportsNativeTools: true,
defaultToolProtocol: "native",
supportsReasoningEffort: ["disable", "medium"],
reasoningEffort: "medium",
preserveReasoning: true,
inputPrice: 0.57,
outputPrice: 2.57,
cacheWritesPrice: 0,
cacheReadsPrice: 0.14,
description:
"GLM-5 is Z.AI's flagship text model with 200K context, 128K max output, thinking mode, function calling, and context caching.",
preferredIndex: 0,
},
"glm-4.7-flash": {
maxTokens: 16_384,
contextWindow: 204_800,
Expand Down
134 changes: 133 additions & 1 deletion src/api/providers/__tests__/zai.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,25 @@ describe("ZAiHandler", () => {
expect(model.info.preserveReasoning).toBe(true)
})

// kilocode_change start
it("should return GLM-5 international model with documented limits", () => {
const testModelId: InternationalZAiModelId = "glm-5"
const handlerWithModel = new ZAiHandler({
apiModelId: testModelId,
zaiApiKey: "test-zai-api-key",
zaiApiLine: "international_coding",
})
const model = handlerWithModel.getModel()
expect(model.id).toBe(testModelId)
expect(model.info).toEqual(internationalZAiModels[testModelId])
expect(model.info.contextWindow).toBe(200_000)
expect(model.info.maxTokens).toBe(131_072)
expect(model.info.supportsReasoningEffort).toEqual(["disable", "medium"])
expect(model.info.reasoningEffort).toBe("medium")
expect(model.info.preserveReasoning).toBe(true)
})
// kilocode_change end

it("should return GLM-4.5v international model with vision support", () => {
const testModelId: InternationalZAiModelId = "glm-4.5v"
const handlerWithModel = new ZAiHandler({
Expand Down Expand Up @@ -203,6 +222,25 @@ describe("ZAiHandler", () => {
expect(model.info.reasoningEffort).toBe("medium")
expect(model.info.preserveReasoning).toBe(true)
})

// kilocode_change start
it("should return GLM-5 China model with documented limits", () => {
const testModelId: MainlandZAiModelId = "glm-5"
const handlerWithModel = new ZAiHandler({
apiModelId: testModelId,
zaiApiKey: "test-zai-api-key",
zaiApiLine: "china_coding",
})
const model = handlerWithModel.getModel()
expect(model.id).toBe(testModelId)
expect(model.info).toEqual(mainlandZAiModels[testModelId])
expect(model.info.contextWindow).toBe(200_000)
expect(model.info.maxTokens).toBe(131_072)
expect(model.info.supportsReasoningEffort).toEqual(["disable", "medium"])
expect(model.info.reasoningEffort).toBe("medium")
expect(model.info.preserveReasoning).toBe(true)
})
// kilocode_change end
})

describe("International API", () => {
Expand Down Expand Up @@ -242,6 +280,23 @@ describe("ZAiHandler", () => {
expect(model.id).toBe(testModelId)
expect(model.info).toEqual(internationalZAiModels[testModelId])
})

// kilocode_change start
it("should return GLM-5 international API model with documented limits", () => {
const testModelId: InternationalZAiModelId = "glm-5"
const handlerWithModel = new ZAiHandler({
apiModelId: testModelId,
zaiApiKey: "test-zai-api-key",
zaiApiLine: "international_api",
})
const model = handlerWithModel.getModel()
expect(model.id).toBe(testModelId)
expect(model.info).toEqual(internationalZAiModels[testModelId])
expect(model.info.contextWindow).toBe(200_000)
expect(model.info.maxTokens).toBe(131_072)
expect(model.info.supportsReasoningEffort).toEqual(["disable", "medium"])
})
// kilocode_change end
})

describe("China API", () => {
Expand Down Expand Up @@ -281,6 +336,23 @@ describe("ZAiHandler", () => {
expect(model.id).toBe(testModelId)
expect(model.info).toEqual(mainlandZAiModels[testModelId])
})

// kilocode_change start
it("should return GLM-5 China API model with documented limits", () => {
const testModelId: MainlandZAiModelId = "glm-5"
const handlerWithModel = new ZAiHandler({
apiModelId: testModelId,
zaiApiKey: "test-zai-api-key",
zaiApiLine: "china_api",
})
const model = handlerWithModel.getModel()
expect(model.id).toBe(testModelId)
expect(model.info).toEqual(mainlandZAiModels[testModelId])
expect(model.info.contextWindow).toBe(200_000)
expect(model.info.maxTokens).toBe(131_072)
expect(model.info.supportsReasoningEffort).toEqual(["disable", "medium"])
})
// kilocode_change end
})

describe("Default behavior", () => {
Expand Down Expand Up @@ -414,7 +486,8 @@ describe("ZAiHandler", () => {
})
})

describe("GLM-4.7 Thinking Mode", () => {
// kilocode_change start
describe("Z.ai Thinking Mode", () => {
it("should enable thinking by default for GLM-4.7 (default reasoningEffort is medium)", async () => {
const handlerWithModel = new ZAiHandler({
apiModelId: "glm-4.7",
Expand Down Expand Up @@ -507,6 +580,64 @@ describe("ZAiHandler", () => {
)
})

it("should enable thinking by default for GLM-5 (default reasoningEffort is medium)", async () => {
const handlerWithModel = new ZAiHandler({
apiModelId: "glm-5",
zaiApiKey: "test-zai-api-key",
zaiApiLine: "international_coding",
})

mockCreate.mockImplementationOnce(() => {
return {
[Symbol.asyncIterator]: () => ({
async next() {
return { done: true }
},
}),
}
})

const messageGenerator = handlerWithModel.createMessage("system prompt", [])
await messageGenerator.next()

expect(mockCreate).toHaveBeenCalledWith(
expect.objectContaining({
model: "glm-5",
thinking: { type: "enabled" },
}),
)
})

it("should disable thinking for GLM-5 when reasoningEffort is set to disable", async () => {
const handlerWithModel = new ZAiHandler({
apiModelId: "glm-5",
zaiApiKey: "test-zai-api-key",
zaiApiLine: "international_coding",
enableReasoningEffort: true,
reasoningEffort: "disable",
})

mockCreate.mockImplementationOnce(() => {
return {
[Symbol.asyncIterator]: () => ({
async next() {
return { done: true }
},
}),
}
})

const messageGenerator = handlerWithModel.createMessage("system prompt", [])
await messageGenerator.next()

expect(mockCreate).toHaveBeenCalledWith(
expect.objectContaining({
model: "glm-5",
thinking: { type: "disabled" },
}),
)
})

it("should NOT add thinking parameter for non-thinking models like GLM-4.6", async () => {
const handlerWithModel = new ZAiHandler({
apiModelId: "glm-4.6",
Expand All @@ -532,4 +663,5 @@ describe("ZAiHandler", () => {
expect(callArgs.thinking).toBeUndefined()
})
})
// kilocode_change end
})
22 changes: 13 additions & 9 deletions src/api/providers/zai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,25 @@ export class ZAiHandler extends BaseOpenAiCompatibleProvider<string> {
})
}

// kilocode_change start
/**
* Override createStream to handle GLM-4.7's thinking mode.
* GLM-4.7 has thinking enabled by default in the API, so we need to
* explicitly send { type: "disabled" } when the user turns off reasoning.
* Override createStream to handle Z.ai models with thinking mode.
* Thinking-capable models have reasoning enabled by default in the API,
* so we explicitly send { type: "disabled" } when users turn reasoning off.
*/
protected override createStream(
systemPrompt: string,
messages: Anthropic.Messages.MessageParam[],
metadata?: ApiHandlerCreateMessageMetadata,
requestOptions?: OpenAI.RequestOptions,
) {
const { id: modelId, info } = this.getModel()
const { info } = this.getModel()

// Check if this is a GLM-4.7 model with thinking support
const isThinkingModel = modelId === "glm-4.7" && Array.isArray(info.supportsReasoningEffort)
// Thinking models advertise explicit reasoning effort support.
const isThinkingModel = Array.isArray(info.supportsReasoningEffort)

if (isThinkingModel) {
// For GLM-4.7, thinking is ON by default in the API.
// For thinking-enabled models, thinking is ON by default in the API.
// We need to explicitly disable it when reasoning is off.
const useReasoning = shouldUseReasoningEffort({ model: info, settings: this.options })

Expand All @@ -67,9 +68,11 @@ export class ZAiHandler extends BaseOpenAiCompatibleProvider<string> {
// For non-thinking models, use the default behavior
return super.createStream(systemPrompt, messages, metadata, requestOptions)
}
// kilocode_change end

// kilocode_change start
/**
* Creates a stream with explicit thinking control for GLM-4.7
* Creates a stream with explicit thinking control for Z.ai thinking models.
*/
private createStreamWithThinking(
systemPrompt: string,
Expand Down Expand Up @@ -99,7 +102,7 @@ export class ZAiHandler extends BaseOpenAiCompatibleProvider<string> {
messages: [{ role: "system", content: systemPrompt }, ...convertedMessages],
stream: true,
stream_options: { include_usage: true },
// For GLM-4.7: thinking is ON by default, so we explicitly disable when needed
// Thinking is ON by default, so we explicitly disable when needed.
thinking: useReasoning ? { type: "enabled" } : { type: "disabled" },
...(metadata?.tools && { tools: this.convertToolsForOpenAI(metadata.tools) }),
...(metadata?.tool_choice && { tool_choice: metadata.tool_choice }),
Expand All @@ -110,4 +113,5 @@ export class ZAiHandler extends BaseOpenAiCompatibleProvider<string> {

return this.client.chat.completions.create(params)
}
// kilocode_change end
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,11 @@ describe("getOptionsForProvider", () => {
const result = getOptionsForProvider("zai", { zaiApiLine: "china_coding" })
expect(result).toEqual({ isChina: true })
})

// kilocode_change start
it("returns isChina: true for zai provider with china_api apiConfiguration", () => {
const result = getOptionsForProvider("zai", { zaiApiLine: "china_api" })
expect(result).toEqual({ isChina: true })
})
// kilocode_change end
})
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,12 @@ export const getOptionsForProvider = (provider: ProviderName, apiConfiguration?:
switch (provider) {
case "zai":
// Determine which Z.AI model set to use based on the API line configuration
return { isChina: apiConfiguration?.zaiApiLine === "china_coding" }
// kilocode_change start
return {
isChina:
apiConfiguration?.zaiApiLine === "china_coding" || apiConfiguration?.zaiApiLine === "china_api",
}
// kilocode_change end
default:
return {}
}
Expand Down
3 changes: 2 additions & 1 deletion webview-ui/src/components/settings/ApiOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,8 @@ const ApiOptions = ({
zai: {
field: "apiModelId",
default:
apiConfiguration.zaiApiLine === "china_coding"
// kilocode_change - china_api uses mainland model catalog too.
apiConfiguration.zaiApiLine === "china_coding" || apiConfiguration.zaiApiLine === "china_api"
? mainlandZAiDefaultModelId
: internationalZAiDefaultModelId,
},
Expand Down
Loading
Loading