mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 17:54:32 +00:00
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:
committed by
Peter Steinberger
parent
c529e6005a
commit
edbc68e9f1
@@ -172,6 +172,39 @@ function createOpenRouterHeadersWrapper(baseStreamFn: StreamFn | undefined): Str
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a streamFn wrapper that injects tool_stream=true for Z.AI providers.
|
||||
*
|
||||
* Z.AI's API supports the `tool_stream` parameter to enable real-time streaming
|
||||
* of tool call arguments and reasoning content. When enabled, the API returns
|
||||
* progressive tool_call deltas, allowing users to see tool execution in real-time.
|
||||
*
|
||||
* @see https://docs.z.ai/api-reference#streaming
|
||||
*/
|
||||
function createZaiToolStreamWrapper(
|
||||
baseStreamFn: StreamFn | undefined,
|
||||
enabled: boolean,
|
||||
): StreamFn {
|
||||
const underlying = baseStreamFn ?? streamSimple;
|
||||
return (model, context, options) => {
|
||||
if (!enabled) {
|
||||
return underlying(model, context, options);
|
||||
}
|
||||
|
||||
const originalOnPayload = options?.onPayload;
|
||||
return underlying(model, context, {
|
||||
...options,
|
||||
onPayload: (payload) => {
|
||||
if (payload && typeof payload === "object") {
|
||||
// Inject tool_stream: true for Z.AI API
|
||||
(payload as Record<string, unknown>).tool_stream = true;
|
||||
}
|
||||
originalOnPayload?.(payload);
|
||||
},
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply extra params (like temperature) to an agent's streamFn.
|
||||
* Also adds OpenRouter app attribution headers when using the OpenRouter provider.
|
||||
@@ -209,6 +242,16 @@ export function applyExtraParamsToAgent(
|
||||
agent.streamFn = createOpenRouterHeadersWrapper(agent.streamFn);
|
||||
}
|
||||
|
||||
// Enable Z.AI tool_stream for real-time tool call streaming.
|
||||
// Enabled by default for Z.AI provider, can be disabled via params.tool_stream: false
|
||||
if (provider === "zai" || provider === "z-ai") {
|
||||
const toolStreamEnabled = merged?.tool_stream !== false;
|
||||
if (toolStreamEnabled) {
|
||||
log.debug(`enabling Z.AI tool_stream for ${provider}/${modelId}`);
|
||||
agent.streamFn = createZaiToolStreamWrapper(agent.streamFn, true);
|
||||
}
|
||||
}
|
||||
|
||||
// Work around upstream pi-ai hardcoding `store: false` for Responses API.
|
||||
// Force `store=true` for direct OpenAI/OpenAI Codex providers so multi-turn
|
||||
// server-side conversation state is preserved.
|
||||
|
||||
Reference in New Issue
Block a user