mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-30 06:14:23 +00:00
docs(zai): document tool_stream defaults
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import type { StreamFn } from "@mariozechner/pi-agent-core";
|
||||
import type { Context, Model, SimpleStreamOptions } from "@mariozechner/pi-ai";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { applyExtraParamsToAgent } from "./extra-params.js";
|
||||
|
||||
@@ -10,104 +11,83 @@ vi.mock("@mariozechner/pi-ai", () => ({
|
||||
})),
|
||||
}));
|
||||
|
||||
type ToolStreamCase = {
|
||||
applyProvider: string;
|
||||
applyModelId: string;
|
||||
model: Model<"openai-completions">;
|
||||
cfg?: Parameters<typeof applyExtraParamsToAgent>[1];
|
||||
options?: SimpleStreamOptions;
|
||||
};
|
||||
|
||||
function runToolStreamCase(params: ToolStreamCase) {
|
||||
const payload: Record<string, unknown> = { model: params.model.id, messages: [] };
|
||||
const baseStreamFn: StreamFn = (_model, _context, options) => {
|
||||
options?.onPayload?.(payload);
|
||||
return {} as ReturnType<StreamFn>;
|
||||
};
|
||||
const agent = { streamFn: baseStreamFn };
|
||||
|
||||
applyExtraParamsToAgent(agent, params.cfg, params.applyProvider, params.applyModelId);
|
||||
|
||||
const context: Context = { messages: [] };
|
||||
void agent.streamFn?.(params.model, context, params.options ?? {});
|
||||
|
||||
return payload;
|
||||
}
|
||||
|
||||
describe("extra-params: Z.AI tool_stream support", () => {
|
||||
it("should inject tool_stream=true for zai provider by default", () => {
|
||||
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 unknown as ReturnType<StreamFn>;
|
||||
it("injects tool_stream=true for zai provider by default", () => {
|
||||
const payload = runToolStreamCase({
|
||||
applyProvider: "zai",
|
||||
applyModelId: "glm-5",
|
||||
model: {
|
||||
api: "openai-completions",
|
||||
provider: "zai",
|
||||
id: "glm-5",
|
||||
} as Model<"openai-completions">,
|
||||
});
|
||||
|
||||
const agent = { streamFn: mockStreamFn };
|
||||
const cfg = {
|
||||
agents: {
|
||||
defaults: {},
|
||||
},
|
||||
};
|
||||
|
||||
applyExtraParamsToAgent(
|
||||
agent,
|
||||
cfg as unknown as Parameters<typeof applyExtraParamsToAgent>[1],
|
||||
"zai",
|
||||
"glm-5",
|
||||
);
|
||||
|
||||
// The streamFn should be wrapped
|
||||
expect(agent.streamFn).toBeDefined();
|
||||
expect(agent.streamFn).not.toBe(mockStreamFn);
|
||||
expect(payload.tool_stream).toBe(true);
|
||||
});
|
||||
|
||||
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 unknown as ReturnType<StreamFn>,
|
||||
);
|
||||
it("does not inject tool_stream for non-zai providers", () => {
|
||||
const payload = runToolStreamCase({
|
||||
applyProvider: "openai",
|
||||
applyModelId: "gpt-5",
|
||||
model: {
|
||||
api: "openai-completions",
|
||||
provider: "openai",
|
||||
id: "gpt-5",
|
||||
} as Model<"openai-completions">,
|
||||
});
|
||||
|
||||
const agent = { streamFn: mockStreamFn };
|
||||
const cfg = {};
|
||||
|
||||
applyExtraParamsToAgent(
|
||||
agent,
|
||||
cfg as unknown as Parameters<typeof applyExtraParamsToAgent>[1],
|
||||
"anthropic",
|
||||
"claude-opus-4-6",
|
||||
);
|
||||
|
||||
// Should remain unchanged (except for OpenAI wrapper)
|
||||
expect(agent.streamFn).toBeDefined();
|
||||
expect(payload).not.toHaveProperty("tool_stream");
|
||||
});
|
||||
|
||||
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 unknown as ReturnType<StreamFn>,
|
||||
);
|
||||
|
||||
const agent = { streamFn: mockStreamFn };
|
||||
const cfg = {
|
||||
agents: {
|
||||
defaults: {
|
||||
models: {
|
||||
"zai/glm-5": {
|
||||
params: {
|
||||
tool_stream: false,
|
||||
it("allows disabling tool_stream via params", () => {
|
||||
const payload = runToolStreamCase({
|
||||
applyProvider: "zai",
|
||||
applyModelId: "glm-5",
|
||||
model: {
|
||||
api: "openai-completions",
|
||||
provider: "zai",
|
||||
id: "glm-5",
|
||||
} as Model<"openai-completions">,
|
||||
cfg: {
|
||||
agents: {
|
||||
defaults: {
|
||||
models: {
|
||||
"zai/glm-5": {
|
||||
params: {
|
||||
tool_stream: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
applyExtraParamsToAgent(
|
||||
agent,
|
||||
cfg as unknown as Parameters<typeof applyExtraParamsToAgent>[1],
|
||||
"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();
|
||||
expect(payload).not.toHaveProperty("tool_stream");
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user