|
| 1 | +// kilocode_change - new file |
| 2 | +import OpenAI from "openai" |
| 3 | + |
| 4 | +import type { ApiHandlerCreateMessageMetadata } from "../../index" |
| 5 | +import type { ApiHandlerOptions } from "../../../shared/api" |
| 6 | +import { ZenMuxHandler } from "../zenmux" |
| 7 | + |
| 8 | +vi.mock("../fetchers/modelCache", () => ({ |
| 9 | + getModels: vi.fn().mockResolvedValue({}), |
| 10 | +})) |
| 11 | + |
| 12 | +function createMockStream() { |
| 13 | + return { |
| 14 | + async *[Symbol.asyncIterator]() { |
| 15 | + yield { |
| 16 | + choices: [{ delta: { content: "ok" }, finish_reason: "stop" }], |
| 17 | + usage: { prompt_tokens: 1, completion_tokens: 1, cost: 0 }, |
| 18 | + } |
| 19 | + }, |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +async function consume(generator: AsyncGenerator<unknown>) { |
| 24 | + for await (const _chunk of generator) { |
| 25 | + // Consume all chunks |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +describe("ZenMuxHandler native tools and message pipeline", () => { |
| 30 | + const baseOptions: ApiHandlerOptions = { |
| 31 | + zenmuxApiKey: "test-key", |
| 32 | + zenmuxModelId: "z-ai/glm-5", |
| 33 | + zenmuxBaseUrl: "https://test.zenmux.ai/api/v1", |
| 34 | + } |
| 35 | + |
| 36 | + it("merges native tool defaults when model cache entry lacks native metadata", () => { |
| 37 | + const handler = new ZenMuxHandler(baseOptions) |
| 38 | + ;(handler as unknown as { models: Record<string, unknown> }).models = { |
| 39 | + "z-ai/glm-5": { |
| 40 | + maxTokens: 8192, |
| 41 | + contextWindow: 128000, |
| 42 | + supportsImages: false, |
| 43 | + supportsPromptCache: false, |
| 44 | + inputPrice: 0, |
| 45 | + outputPrice: 0, |
| 46 | + description: "GLM 5", |
| 47 | + }, |
| 48 | + } |
| 49 | + |
| 50 | + const model = handler.getModel() |
| 51 | + expect(model.info.supportsNativeTools).toBe(true) |
| 52 | + expect(model.info.defaultToolProtocol).toBe("native") |
| 53 | + }) |
| 54 | + |
| 55 | + it("passes tools and tool choice to stream creation when task protocol is native", async () => { |
| 56 | + const handler = new ZenMuxHandler(baseOptions) |
| 57 | + |
| 58 | + vi.spyOn(handler, "fetchModel").mockResolvedValue({ |
| 59 | + id: "z-ai/glm-5", |
| 60 | + info: { |
| 61 | + maxTokens: 8192, |
| 62 | + contextWindow: 128000, |
| 63 | + supportsNativeTools: true, |
| 64 | + supportsImages: false, |
| 65 | + supportsPromptCache: false, |
| 66 | + inputPrice: 0, |
| 67 | + outputPrice: 0, |
| 68 | + description: "GLM 5", |
| 69 | + }, |
| 70 | + } as any) |
| 71 | + |
| 72 | + const streamSpy = vi.spyOn(handler, "createZenMuxStream").mockResolvedValue(createMockStream() as any) |
| 73 | + |
| 74 | + const tools: OpenAI.Chat.ChatCompletionTool[] = [ |
| 75 | + { |
| 76 | + type: "function", |
| 77 | + function: { |
| 78 | + name: "attempt_completion", |
| 79 | + description: "Complete the task", |
| 80 | + parameters: { type: "object", properties: {} }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + ] |
| 84 | + const metadata: ApiHandlerCreateMessageMetadata = { |
| 85 | + taskId: "task-native", |
| 86 | + toolProtocol: "native", |
| 87 | + tools, |
| 88 | + tool_choice: "auto", |
| 89 | + parallelToolCalls: true, |
| 90 | + } |
| 91 | + |
| 92 | + await consume(handler.createMessage("system", [{ role: "user", content: "hi" }], metadata)) |
| 93 | + |
| 94 | + expect(streamSpy).toHaveBeenCalledTimes(1) |
| 95 | + expect(streamSpy.mock.calls[0][6]).toEqual(tools) |
| 96 | + expect(streamSpy.mock.calls[0][7]).toBe("auto") |
| 97 | + expect(streamSpy.mock.calls[0][8]).toBe(true) |
| 98 | + }) |
| 99 | + |
| 100 | + it("omits tools when task protocol is xml even if tools are provided", async () => { |
| 101 | + const handler = new ZenMuxHandler(baseOptions) |
| 102 | + |
| 103 | + vi.spyOn(handler, "fetchModel").mockResolvedValue({ |
| 104 | + id: "z-ai/glm-5", |
| 105 | + info: { |
| 106 | + maxTokens: 8192, |
| 107 | + contextWindow: 128000, |
| 108 | + supportsNativeTools: true, |
| 109 | + supportsImages: false, |
| 110 | + supportsPromptCache: false, |
| 111 | + inputPrice: 0, |
| 112 | + outputPrice: 0, |
| 113 | + description: "GLM 5", |
| 114 | + }, |
| 115 | + } as any) |
| 116 | + |
| 117 | + const streamSpy = vi.spyOn(handler, "createZenMuxStream").mockResolvedValue(createMockStream() as any) |
| 118 | + |
| 119 | + const tools: OpenAI.Chat.ChatCompletionTool[] = [ |
| 120 | + { |
| 121 | + type: "function", |
| 122 | + function: { |
| 123 | + name: "ask_followup_question", |
| 124 | + description: "Ask a follow-up question", |
| 125 | + parameters: { type: "object", properties: {} }, |
| 126 | + }, |
| 127 | + }, |
| 128 | + ] |
| 129 | + |
| 130 | + await consume( |
| 131 | + handler.createMessage("system", [{ role: "user", content: "hi" }], { |
| 132 | + taskId: "task-xml", |
| 133 | + toolProtocol: "xml", |
| 134 | + tools, |
| 135 | + tool_choice: "auto", |
| 136 | + parallelToolCalls: true, |
| 137 | + }), |
| 138 | + ) |
| 139 | + |
| 140 | + expect(streamSpy).toHaveBeenCalledTimes(1) |
| 141 | + expect(streamSpy.mock.calls[0][6]).toBeUndefined() |
| 142 | + expect(streamSpy.mock.calls[0][7]).toBeUndefined() |
| 143 | + expect(streamSpy.mock.calls[0][8]).toBe(false) |
| 144 | + }) |
| 145 | + |
| 146 | + it("passes transformed DeepSeek R1 messages into stream creation", async () => { |
| 147 | + const handler = new ZenMuxHandler({ |
| 148 | + ...baseOptions, |
| 149 | + zenmuxModelId: "deepseek/deepseek-r1", |
| 150 | + }) |
| 151 | + |
| 152 | + vi.spyOn(handler, "fetchModel").mockResolvedValue({ |
| 153 | + id: "deepseek/deepseek-r1", |
| 154 | + info: { |
| 155 | + maxTokens: 8192, |
| 156 | + contextWindow: 128000, |
| 157 | + supportsNativeTools: true, |
| 158 | + supportsImages: false, |
| 159 | + supportsPromptCache: false, |
| 160 | + inputPrice: 0, |
| 161 | + outputPrice: 0, |
| 162 | + description: "DeepSeek R1", |
| 163 | + }, |
| 164 | + } as any) |
| 165 | + |
| 166 | + const streamSpy = vi.spyOn(handler, "createZenMuxStream").mockResolvedValue(createMockStream() as any) |
| 167 | + |
| 168 | + await consume(handler.createMessage("system prompt", [{ role: "user", content: "hi" }], { taskId: "task-r1" })) |
| 169 | + |
| 170 | + expect(streamSpy).toHaveBeenCalledTimes(1) |
| 171 | + const sentMessages = streamSpy.mock.calls[0][1] as OpenAI.Chat.ChatCompletionMessageParam[] |
| 172 | + expect(sentMessages.some((message: any) => message.role === "system")).toBe(false) |
| 173 | + expect((sentMessages[0] as any).role).toBe("user") |
| 174 | + }) |
| 175 | +}) |
0 commit comments