|
1 | 1 | import { expect } from "bun:test" |
2 | 2 | import { ConfigProvider, Effect } from "effect" |
3 | 3 | import { Headers } from "effect/unstable/http" |
4 | | -import { Auth, LLM, LLMClient, Message, ReasoningPart, ToolDefinition } from "../../src/index.js" |
| 4 | +import { Auth, LLM, LLMClient, Message, ReasoningPart, ToolCallPart, ToolDefinition } from "../../src/index.js" |
5 | 5 | import { ZAI, ZAICodingPlan } from "../../src/providers.js" |
6 | 6 | import { compileRequest } from "../../src/route/client.js" |
7 | 7 | import { Endpoint } from "../../src/route/endpoint.js" |
@@ -331,3 +331,77 @@ it.effect("ZAI Coding Messages serializes enabled thinking without a token budge |
331 | 331 | ), |
332 | 332 | ), |
333 | 333 | ) |
| 334 | + |
| 335 | +it.effect("scrubs GLM-hostile message shapes for legacy zai routes", () => |
| 336 | + Effect.gen(function* () { |
| 337 | + for (const model of [ZAI.chat("glm-4.5-air"), ZAICodingPlan.chat("glm-4.5-air")]) { |
| 338 | + const prepared = yield* compileRequest( |
| 339 | + LLM.request({ |
| 340 | + model, |
| 341 | + messages: [ |
| 342 | + Message.user("Plan the work"), |
| 343 | + Message.assistant([ |
| 344 | + { type: "reasoning", text: "hidden chain of thought" }, |
| 345 | + ToolCallPart.make({ id: "call 1 weird", name: "lookup", input: { city: "Paris" } }), |
| 346 | + ]), |
| 347 | + Message.tool({ id: "call 1 weird", name: "lookup", result: "Sunny" }), |
| 348 | + Message.assistant([{ type: "text", text: "Working on it." }]), |
| 349 | + ], |
| 350 | + }), |
| 351 | + ) |
| 352 | + const messages = prepared.body.messages |
| 353 | + const first = messages[1] |
| 354 | + expect(first?.role === "assistant" ? first.reasoning_content : undefined).toBeUndefined() |
| 355 | + expect(first?.role === "assistant" ? first.reasoning_details : undefined).toBeUndefined() |
| 356 | + expect(first?.role === "assistant" ? first.tool_calls?.[0]?.id : undefined).toBe("call1weird") |
| 357 | + const tool = messages[2] |
| 358 | + expect(tool?.role === "tool" ? tool.tool_call_id : undefined).toBe("call1weird") |
| 359 | + const last = messages.at(-1) |
| 360 | + expect(last?.role === "assistant" && typeof last.content === "string" ? last.content : undefined).toBe( |
| 361 | + "Working on it.", |
| 362 | + ) |
| 363 | + } |
| 364 | + }).pipe(Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env: { ZAI_API_KEY: "fixture" } })))), |
| 365 | +) |
| 366 | + |
| 367 | +it.effect("merges consecutive assistant turns for legacy zai routes", () => |
| 368 | + Effect.gen(function* () { |
| 369 | + const prepared = yield* compileRequest( |
| 370 | + LLM.request({ |
| 371 | + model: ZAI.chat("glm-4.5-air"), |
| 372 | + messages: [ |
| 373 | + Message.user("Plan the work"), |
| 374 | + Message.assistant([{ type: "text", text: "Working" }]), |
| 375 | + Message.assistant([{ type: "text", text: "on it." }]), |
| 376 | + Message.assistant([{ type: "reasoning", text: "forgotten" }]), |
| 377 | + ], |
| 378 | + }), |
| 379 | + ) |
| 380 | + const messages = prepared.body.messages |
| 381 | + expect(messages).toHaveLength(2) |
| 382 | + const merged = messages[1] |
| 383 | + expect(merged?.role === "assistant" ? Array.isArray(merged.content) : false).toBe(true) |
| 384 | + expect(merged?.role === "assistant" ? merged.reasoning_content : undefined).toBeUndefined() |
| 385 | + }).pipe(Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env: { ZAI_API_KEY: "fixture" } })))), |
| 386 | +) |
| 387 | + |
| 388 | +it.effect("preserves replayed reasoning for modern GLM models", () => |
| 389 | + Effect.gen(function* () { |
| 390 | + const prepared = yield* compileRequest( |
| 391 | + LLM.request({ |
| 392 | + model: ZAI.chat("glm-5.3"), |
| 393 | + messages: [ |
| 394 | + Message.user("Plan the work"), |
| 395 | + Message.assistant([ |
| 396 | + { type: "reasoning", text: "chain of thought" }, |
| 397 | + ToolCallPart.make({ id: "call_1", name: "lookup", input: { city: "Paris" } }), |
| 398 | + ]), |
| 399 | + Message.tool({ id: "call_1", name: "lookup", result: "Sunny" }), |
| 400 | + ], |
| 401 | + }), |
| 402 | + ) |
| 403 | + const assistant = prepared.body.messages[1] |
| 404 | + expect(assistant?.role === "assistant" ? assistant.reasoning_content : undefined).toBe("chain of thought") |
| 405 | + expect(assistant?.role === "assistant" ? assistant.tool_calls?.[0]?.id : undefined).toBe("call_1") |
| 406 | + }).pipe(Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env: { ZAI_API_KEY: "fixture" } })))), |
| 407 | +) |
0 commit comments