diff --git a/packages/types/src/provider-settings.ts b/packages/types/src/provider-settings.ts index d713a47d6b4..19e7e3644e9 100644 --- a/packages/types/src/provider-settings.ts +++ b/packages/types/src/provider-settings.ts @@ -391,7 +391,7 @@ const sambaNovaSchema = apiModelIdProviderModelSchema.extend({ sambaNovaApiKey: z.string().optional(), }) -export const zaiApiLineSchema = z.enum(["international_coding", "china_coding"]) +export const zaiApiLineSchema = z.enum(["international_coding", "china_coding", "international_api", "china_api"]) export type ZaiApiLine = z.infer diff --git a/packages/types/src/providers/zai.ts b/packages/types/src/providers/zai.ts index e21fcc698bf..3eae002f17d 100644 --- a/packages/types/src/providers/zai.ts +++ b/packages/types/src/providers/zai.ts @@ -212,13 +212,23 @@ export const ZAI_DEFAULT_TEMPERATURE = 0.6 export const zaiApiLineConfigs = { international_coding: { - name: "International", + name: "International Coding", baseUrl: "https://api.z.ai/api/coding/paas/v4", isChina: false, }, china_coding: { - name: "China", + name: "China Coding", baseUrl: "https://open.bigmodel.cn/api/coding/paas/v4", isChina: true, }, + international_api: { + name: "International API", + baseUrl: "https://api.z.ai/api/paas/v4", + isChina: false, + }, + china_api: { + name: "China API", + baseUrl: "https://open.bigmodel.cn/api/paas/v4", + isChina: true, + }, } satisfies Record diff --git a/src/api/providers/__tests__/zai.spec.ts b/src/api/providers/__tests__/zai.spec.ts index 083fdc13ef8..516685c3ab0 100644 --- a/src/api/providers/__tests__/zai.spec.ts +++ b/src/api/providers/__tests__/zai.spec.ts @@ -166,6 +166,84 @@ describe("ZAiHandler", () => { }) }) + describe("International API", () => { + beforeEach(() => { + handler = new ZAiHandler({ zaiApiKey: "test-zai-api-key", zaiApiLine: "international_api" }) + }) + + it("should use the correct international API base URL", () => { + new ZAiHandler({ zaiApiKey: "test-zai-api-key", zaiApiLine: "international_api" }) + expect(OpenAI).toHaveBeenCalledWith( + expect.objectContaining({ + baseURL: "https://api.z.ai/api/paas/v4", + }), + ) + }) + + it("should use the provided API key for international API", () => { + const zaiApiKey = "test-zai-api-key" + new ZAiHandler({ zaiApiKey, zaiApiLine: "international_api" }) + expect(OpenAI).toHaveBeenCalledWith(expect.objectContaining({ apiKey: zaiApiKey })) + }) + + it("should return international default model when no model is specified", () => { + const model = handler.getModel() + expect(model.id).toBe(internationalZAiDefaultModelId) + expect(model.info).toEqual(internationalZAiModels[internationalZAiDefaultModelId]) + }) + + it("should return specified international model when valid model is provided", () => { + const testModelId: InternationalZAiModelId = "glm-4.5-air" + 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]) + }) + }) + + describe("China API", () => { + beforeEach(() => { + handler = new ZAiHandler({ zaiApiKey: "test-zai-api-key", zaiApiLine: "china_api" }) + }) + + it("should use the correct China API base URL", () => { + new ZAiHandler({ zaiApiKey: "test-zai-api-key", zaiApiLine: "china_api" }) + expect(OpenAI).toHaveBeenCalledWith( + expect.objectContaining({ + baseURL: "https://open.bigmodel.cn/api/paas/v4", + }), + ) + }) + + it("should use the provided API key for China API", () => { + const zaiApiKey = "test-zai-api-key" + new ZAiHandler({ zaiApiKey, zaiApiLine: "china_api" }) + expect(OpenAI).toHaveBeenCalledWith(expect.objectContaining({ apiKey: zaiApiKey })) + }) + + it("should return China default model when no model is specified", () => { + const model = handler.getModel() + expect(model.id).toBe(mainlandZAiDefaultModelId) + expect(model.info).toEqual(mainlandZAiModels[mainlandZAiDefaultModelId]) + }) + + it("should return specified China model when valid model is provided", () => { + const testModelId: MainlandZAiModelId = "glm-4.5-air" + 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]) + }) + }) + describe("Default behavior", () => { it("should default to international when no zaiApiLine is specified", () => { const handlerDefault = new ZAiHandler({ zaiApiKey: "test-zai-api-key" })