feat: support Z.AI tool_stream for real-time tool call streaming

Add support for Z.AI's native tool_stream parameter to enable real-time
visibility into model reasoning and tool call execution.

- Automatically inject tool_stream=true for zai/z-ai providers
- Allow disabling via params.tool_stream: false in model config
- Follows existing pattern of OpenRouter and OpenAI wrappers

This enables Z.AI API features described in:
https://docs.z.ai/api-reference#streaming

AI-assisted: Claude (OpenClaw agent) helped write this implementation.
Testing: lightly tested (code review + pattern matching existing wrappers)

Closes #18135
This commit is contained in:
tian Xiao
2026-02-17 00:30:33 +09:00
committed by Peter Steinberger
parent c529e6005a
commit edbc68e9f1
2 changed files with 137 additions and 0 deletions

View File

@@ -0,0 +1,94 @@
import { describe, expect, it, vi } from "vitest";
import type { StreamFn } from "@mariozechner/pi-agent-core";
import type { Context, Model } from "@mariozechner/pi-ai";
import { applyExtraParamsToAgent } from "./extra-params.js";
// Mock streamSimple for testing
vi.mock("@mariozechner/pi-ai", () => ({
streamSimple: vi.fn(() => ({
push: vi.fn(),
result: vi.fn(),
})),
}));
describe("extra-params: Z.AI tool_stream support", () => {
it("should inject tool_stream=true for zai provider by default", () => {
const capturedPayloads: unknown[] = [];
const mockStreamFn: StreamFn = vi.fn((model, context, options) => {
// Capture the payload that would be sent
options?.onPayload?.({ model: model.id, messages: [] });
return {
push: vi.fn(),
result: vi.fn().mockResolvedValue({
role: "assistant",
content: [{ type: "text", text: "ok" }],
stopReason: "stop",
}),
} as any;
});
const agent = { streamFn: mockStreamFn };
const cfg = {
agents: {
defaults: {},
},
};
applyExtraParamsToAgent(agent, cfg as any, "zai", "glm-5");
// The streamFn should be wrapped
expect(agent.streamFn).toBeDefined();
expect(agent.streamFn).not.toBe(mockStreamFn);
});
it("should not inject tool_stream for non-zai providers", () => {
const mockStreamFn: StreamFn = vi.fn(() => ({
push: vi.fn(),
result: vi.fn().mockResolvedValue({
role: "assistant",
content: [{ type: "text", text: "ok" }],
stopReason: "stop",
}),
} as any));
const agent = { streamFn: mockStreamFn };
const cfg = {};
applyExtraParamsToAgent(agent, cfg as any, "anthropic", "claude-opus-4-6");
// Should remain unchanged (except for OpenAI wrapper)
expect(agent.streamFn).toBeDefined();
});
it("should allow disabling tool_stream via params", () => {
const mockStreamFn: StreamFn = vi.fn(() => ({
push: vi.fn(),
result: vi.fn().mockResolvedValue({
role: "assistant",
content: [{ type: "text", text: "ok" }],
stopReason: "stop",
}),
} as any));
const agent = { streamFn: mockStreamFn };
const cfg = {
agents: {
defaults: {
models: {
"zai/glm-5": {
params: {
tool_stream: false,
},
},
},
},
},
};
applyExtraParamsToAgent(agent, cfg as any, "zai", "glm-5");
// The tool_stream wrapper should be applied but with enabled=false
// In this case, it should just return the underlying streamFn
expect(agent.streamFn).toBeDefined();
});
});