feat(providers): add Z.AI Coding Plan with streaming tool calls (#12205)

Co-authored-by: Michael Neale <michael.neale@gmail.com>
This commit is contained in:
James Dumay
2026-09-19 03:58:19 +00:00
committed by GitHub
parent d57c9a4d73
commit ba8ba0cadb
8 changed files with 427 additions and 2 deletions
@@ -59,6 +59,7 @@ pub(crate) mod declarative_providers {
venice,
vercel_ai_gateway,
zai,
zai_coding_plan,
zhipu,
);
}
@@ -0,0 +1,31 @@
{
"name": "zai_coding_plan",
"engine": "openai",
"display_name": "Z.AI Coding Plan",
"description": "GLM models through the Z.AI Coding Plan with streaming tool calls",
"api_key_env": "ZAI_CODING_PLAN_API_KEY",
"base_url": "https://api.z.ai/api/coding/paas/v4",
"model_doc_link": "https://docs.z.ai/devpack/overview",
"preserves_thinking": true,
"dynamic_models": true,
"models": [
{
"name": "glm-5.3",
"context_limit": 1000000,
"reasoning": true
},
{
"name": "glm-5.3-flash",
"context_limit": 1000000,
"reasoning": true
}
],
"supports_streaming": true,
"setup": {
"category": "model",
"setup_method": "single_api_key",
"group": "additional",
"docs_url": "https://docs.z.ai/devpack/overview",
"aliases": ["z.ai coding plan", "glm coding plan"]
}
}
+32
View File
@@ -417,6 +417,10 @@ impl OpenAiProvider {
const PROVIDERS_NEEDING_STANDARD_CHAT_PARAMS: &[&str] = &["nearai", "pleumrouter"];
/// Providers whose endpoints require the non-standard `tool_stream`
/// field to stream tool-call arguments incrementally.
const PROVIDERS_NEEDING_TOOL_STREAM: &[&str] = &["zai_coding_plan"];
/// Providers whose reasoning models accept an OpenAI-style
/// `reasoning_effort` field on chat-completions requests but aren't
/// matched by [`is_openai_responses_model`] (which only recognises
@@ -452,6 +456,12 @@ impl OpenAiProvider {
model_config: &ModelConfig,
) -> serde_json::Value {
if let Some(obj) = payload.as_object_mut() {
if Self::PROVIDERS_NEEDING_TOOL_STREAM.contains(&self.name.as_str())
&& obj.get("stream") == Some(&json!(true))
{
obj.entry("tool_stream").or_insert(json!(true));
}
if Self::PROVIDERS_NEEDING_MAX_TOKENS_REMAP.contains(&self.name.as_str()) {
if let Some(value) = obj.remove("max_completion_tokens") {
obj.entry("max_tokens").or_insert(value);
@@ -1001,6 +1011,28 @@ mod tests {
}
}
#[test]
fn coding_plan_tool_stream_is_scoped_and_preserves_explicit_overrides() {
let model = ModelConfig::new("glm-future");
for (provider, payload, expected) in [
(
"zai_coding_plan",
json!({"stream": true}),
Some(json!(true)),
),
("zai_coding_plan", json!({"stream": false}), None),
("openai", json!({"stream": true}), None),
(
"zai_coding_plan",
json!({"stream": true, "tool_stream": false}),
Some(json!(false)),
),
] {
let request = make_provider(provider).sanitize_request_for_compat(payload, &model);
assert_eq!(request.get("tool_stream"), expected.as_ref());
}
}
#[test]
fn sanitize_remaps_max_completion_tokens_for_compat_provider() {
let provider = make_provider("mistral");
@@ -0,0 +1,222 @@
use futures::StreamExt;
use goose_providers::{
base::Provider,
conversation::message::{Message, MessageContentBlock},
declarative::{deserialize_provider_config, KeyResolver},
model::ModelConfig,
openai::from_declarative_config,
zai_coding_plan,
};
use rmcp::model::{CallToolResult, ContentBlock, Tool};
use serde_json::{json, Value};
use std::{convert::Infallible, sync::Arc, time::Duration};
use wiremock::{
matchers::{body_partial_json, header, method, path},
Mock, MockServer, ResponseTemplate,
};
struct TestKey;
impl KeyResolver for TestKey {
type Error = Infallible;
fn resolve_key(&self, key: &str) -> Result<String, Self::Error> {
assert_eq!(key, "ZAI_CODING_PLAN_API_KEY");
Ok("test-key".into())
}
}
fn provider(server: &MockServer) -> impl Provider {
let mut config = deserialize_provider_config(zai_coding_plan::JSON).unwrap();
assert_eq!(config.base_url, "https://api.z.ai/api/coding/paas/v4");
config.base_url = format!("{}/api/coding/paas/v4", server.uri());
from_declarative_config(config, None, TestKey)
.unwrap()
.build()
}
fn tool() -> Tool {
Tool::new(
"write_file",
"Write a file",
Arc::new(
json!({"type":"object","properties":{"text":{"type":"string"}},"required":["text"]})
.as_object()
.unwrap()
.clone(),
),
)
}
fn sse(deltas: Vec<Value>, finish: &str) -> String {
let mut body = String::new();
for delta in deltas {
body.push_str(&format!(
"data: {}\n\n",
json!({"id":"response-1","model":"glm-5.3","choices":[{"index":0,"delta":delta,"finish_reason":null}]})
));
}
body.push_str(&format!(
"data: {}\n\ndata: [DONE]\n\n",
json!({"id":"response-1","model":"glm-5.3","choices":[{"index":0,"delta":{},"finish_reason":finish}],
"usage":{"prompt_tokens":20,"completion_tokens":10,"total_tokens":30}})
));
body
}
#[tokio::test]
async fn streams_fragmented_tools_and_replays_reasoning_with_tool_results() {
let server = MockServer::start().await;
let provider = provider(&server);
Mock::given(method("GET"))
.and(path("/api/coding/paas/v4/models"))
.and(header("authorization", "Bearer test-key"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"data": [{"id":"glm-5.3"}, {"id":"glm-5.3-flash"}, {"id":"glm-future"}]
})))
.expect(1)
.mount(&server)
.await;
assert_eq!(
provider.fetch_supported_models().await.unwrap(),
["glm-5.3", "glm-5.3-flash", "glm-future"]
);
server.verify().await;
server.reset().await;
for model in ["glm-5.3", "glm-5.3-flash", "glm-future"] {
let response = sse(
vec![
json!({"role":"assistant","reasoning_content":"Inspect first. "}),
json!({"reasoning_content":"Then write."}),
json!({"tool_calls":[{"index":0,"id":"call-1","type":"function","function":{"name":"write_file","arguments":""}}]}),
json!({"tool_calls":[{"index":0,"function":{"arguments":"{\"text\":\"Hello"}}]}),
json!({"tool_calls":[{"index":0,"function":{"arguments":" \\u4e16\\u754c\\n\"}"}}]}),
],
"tool_calls",
);
Mock::given(method("POST"))
.and(path("/api/coding/paas/v4/chat/completions"))
.and(header("authorization", "Bearer test-key"))
.and(body_partial_json(
json!({"model":model,"stream":true,"tool_stream":true}),
))
.respond_with(ResponseTemplate::new(200).set_body_raw(response, "text/event-stream"))
.expect(1)
.mount(&server)
.await;
let mut messages = vec![Message::user().with_text("Write the greeting")];
let (reply, usage) = provider
.complete(
&ModelConfig::new(model),
"You are a coding assistant",
&messages,
&[tool()],
)
.await
.unwrap();
assert_eq!(usage.usage.total_tokens, Some(30));
let calls: Vec<_> = reply
.content
.iter()
.filter_map(|block| match block {
MessageContentBlock::ToolRequest(request) => Some(request),
_ => None,
})
.collect();
assert_eq!(calls.len(), 1);
assert_eq!(calls[0].id, "call-1");
let call = calls[0].tool_call.as_ref().unwrap();
assert_eq!(call.name, "write_file");
assert_eq!(
call.arguments,
Some(json!({"text":"Hello 世界\n"}).as_object().unwrap().clone())
);
messages.push(reply);
messages.push(Message::user().with_tool_response(
"call-1",
Ok(CallToolResult::success(vec![ContentBlock::text("Written")])),
));
server.verify().await;
server.reset().await;
Mock::given(method("POST"))
.respond_with(ResponseTemplate::new(200).set_body_raw(
sse(vec![json!({"content":"Done"})], "stop"),
"text/event-stream",
))
.expect(1)
.mount(&server)
.await;
let (reply, _) = provider
.complete(
&ModelConfig::new(model),
"You are a coding assistant",
&messages,
&[tool()],
)
.await
.unwrap();
assert_eq!(reply.as_concat_text(), "Done");
let requests = server.received_requests().await.unwrap();
let body: Value = requests[0].body_json().unwrap();
let history = body["messages"].as_array().unwrap();
let assistant = history.iter().find(|m| m["role"] == "assistant").unwrap();
assert_eq!(assistant["reasoning_content"], "Inspect first. Then write.");
assert_eq!(assistant["tool_calls"][0]["id"], "call-1");
let result = history.iter().find(|m| m["role"] == "tool").unwrap();
assert_eq!(result["tool_call_id"], "call-1");
assert_eq!(body["tool_stream"], true);
server.verify().await;
server.reset().await;
}
}
#[tokio::test]
async fn cancelled_request_does_not_contaminate_next_turn() {
let server = MockServer::start().await;
let provider = provider(&server);
Mock::given(method("POST"))
.respond_with(ResponseTemplate::new(200).set_delay(Duration::from_secs(10)))
.mount(&server)
.await;
let model = ModelConfig::new("glm-5.3");
assert!(tokio::time::timeout(
Duration::from_millis(100),
provider.stream(
&model,
"system",
&[Message::user().with_text("Old task")],
&[tool()]
),
)
.await
.is_err());
server.reset().await;
Mock::given(method("POST"))
.and(body_partial_json(json!({"stream":true,"tool_stream":true})))
.respond_with(ResponseTemplate::new(200).set_body_raw(
sse(vec![json!({"content":"New task"})], "stop"),
"text/event-stream",
))
.expect(1)
.mount(&server)
.await;
let mut stream = provider
.stream(
&model,
"system",
&[Message::user().with_text("New task")],
&[],
)
.await
.unwrap();
let mut text = String::new();
while let Some(chunk) = stream.next().await {
if let (Some(message), _) = chunk.unwrap() {
text.push_str(&message.as_concat_text());
}
}
assert_eq!(text, "New task");
}
@@ -91,6 +91,29 @@ goose supports [Agent Client Protocol (ACP)](https://agentclientprotocol.com/) a
See the [ACP Providers guide](/docs/guides/acp-providers) for detailed setup instructions.
:::
### Z.AI Coding Plan
In goose Desktop, open **Settings → Models**, choose **Z.AI Coding Plan**, and
enter your Coding Plan API key. The model selector discovers available models
from your Coding Plan endpoint; use **Refresh** to update the list.
Z.AI may list models that your subscription cannot use; access is checked when
you send a request.
In the CLI, select **Z.AI Coding Plan** in `goose configure` and supply your Coding Plan API key,
or set `GOOSE_PROVIDER=zai_coding_plan`, `GOOSE_MODEL=glm-5.3`, and
`ZAI_CODING_PLAN_API_KEY`. GLM-5.3 and GLM-5.3-Flash are the fallback choices
when the endpoint does not support model discovery.
This provider uses the dedicated OpenAI-compatible endpoint
`https://api.z.ai/api/coding/paas/v4`. It enables both `stream` and `tool_stream`
for streaming requests, including newly discovered models, so tool arguments arrive incrementally, and preserves
`reasoning_content` across tool-result turns. The existing **Z.AI** provider
continues to use the Anthropic-compatible endpoint.
See Z.AI's [Coding Plan model availability](https://docs.z.ai/devpack/overview),
[tool streaming](https://docs.z.ai/guides/capabilities/stream-tool), and
[thinking preservation](https://docs.z.ai/guides/capabilities/thinking-mode) documentation.
## Configure Provider and Model
To configure your chosen provider, see available options, or select a model, visit the `Models` tab in goose Desktop or run `goose configure` in the CLI.
+26 -1
View File
@@ -15,6 +15,7 @@ activities:
- Test load tool for knowledge injection and discovery
- Test delegate tool for task delegation (sync and async)
- Test multi-turn thinking preservation for providers that reject replayed reasoning_content
- Test Z.AI Coding Plan streamed tool calls and interruption recovery
- Validate ACP thinking-effort discovery, forwarding, and provider switching
- Validate GDK observability hook lifecycle events and payload opt-in
- Test error boundaries including nested delegation prevention
@@ -25,7 +26,7 @@ parameters:
input_type: string
requirement: optional
default: "all"
description: "Which test phases to run: all, basic, extensions, delegation, reasoning, acp-effort, advanced"
description: "Which test phases to run: all, basic, extensions, delegation, reasoning, zai-coding-plan, acp-effort, advanced"
- key: test_depth
input_type: string
@@ -408,6 +409,30 @@ prompt: |
Log results to: {{ workspace_dir }}/phase3c_reasoning.md
{% endif %}
{% if test_phases == "all" or "zai-coding-plan" in test_phases %}
## Z.AI Coding Plan
**Prerequisites**: The active provider must be `zai_coding_plan`, with a Coding Plan API key
configured and the developer extension enabled. Otherwise record this phase as SKIPPED.
In Desktop, confirm the provider appears in Settings → Models, accepts the API key,
and refreshes available models from the Coding Plan endpoint. Record this UI check
as SKIPPED in headless mode. Newly discovered models must also use `tool_stream`.
1. Use the file-writing tool to create a substantial Python module in the test workspace
in a single call, including Unicode text and escaped newlines. Read it back and run it.
2. Modify that module in a second tool call, then run it again. Verify the tool-result
continuation succeeds without malformed JSON, missing tool IDs, or reasoning errors.
3. Ask the human tester to interrupt a long response with Ctrl+C and give a replacement
instruction. Verify the next turn follows the replacement and tool use still works.
In headless mode, mark this interactive step SKIPPED rather than claiming success.
4. When transport diagnostics are available, verify `stream: true` and `tool_stream: true`
on requests, multiple argument deltas for the large tool call, and unmodified
`reasoning_content` on subsequent tool-result turns. Never log API keys.
Log results to: {{ workspace_dir }}/zai_coding_plan.md
{% endif %}
{% if test_phases == "all" or "acp-effort" in test_phases %}
## 🧠 PHASE 3D: ACP Thinking-Effort Testing
@@ -30,6 +30,49 @@ describe('ACP providers', () => {
vi.clearAllMocks();
});
it('exposes Coding Plan in Desktop setup and refreshes newly discovered models', async () => {
const entry = providerEntry({
providerId: 'zai_coding_plan',
providerName: 'Z.AI Coding Plan',
providerType: 'Declarative',
acp: false,
configKeys: [{ name: 'ZAI_CODING_PLAN_API_KEY', required: true, secret: true }],
});
const discovered = {
...entry,
models: [{ id: 'glm-future', name: 'glm-future', recommended: true }],
};
const client = {
goose: {
providersList_unstable: vi
.fn()
.mockResolvedValueOnce({ entries: [entry] })
.mockResolvedValueOnce({ entries: [entry] })
.mockResolvedValueOnce({ entries: [discovered] }),
providersReadinessCheck_unstable: vi.fn().mockResolvedValue({ ready: true }),
providersInventoryRefresh_unstable: vi
.fn()
.mockResolvedValue({ started: ['zai_coding_plan'], skipped: [] }),
},
};
vi.mocked(getAcpClient).mockResolvedValue(
client as unknown as Awaited<ReturnType<typeof getAcpClient>>
);
const setup = await acpListSetupProviderDetails();
expect(setup[0].metadata.display_name).toBe('Z.AI Coding Plan');
expect(setup[0].supports_refresh).toBe(true);
expect(setup[0].metadata.config_keys[0].name).toBe('ZAI_CODING_PLAN_API_KEY');
const refreshed = await acpRefreshProviderDetails('zai_coding_plan');
expect(client.goose.providersInventoryRefresh_unstable).toHaveBeenCalledWith({
providerIds: ['zai_coding_plan'],
});
expect(refreshed.provider.metadata.known_models.map((model) => model.name)).toEqual([
'glm-future',
]);
});
it('sets thinking effort after provider and model, then returns the final config response', async () => {
const client = {
connection: {
@@ -1,7 +1,11 @@
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { describe, expect, it, vi } from 'vitest';
import { acpEnableProvider, acpRefreshProviderDetails } from '../../../../acp/providers';
import {
acpEnableProvider,
acpRefreshProviderDetails,
acpSaveProviderConfig,
} from '../../../../acp/providers';
import { IntlTestWrapper } from '../../../../i18n/test-utils';
import type { ProviderDetails } from '../../../../types/providers';
import ProviderConfigurationModal from './ProviderConfigurationModal';
@@ -22,6 +26,7 @@ vi.mock('../../../../acp/providers', () => ({
acpEnableProvider: vi.fn(),
acpRefreshProviderDetails: vi.fn(),
acpSaveProviderConfig: vi.fn(),
acpReadProviderConfig: vi.fn().mockResolvedValue([]),
}));
const oauthProvider: ProviderDetails = {
@@ -51,6 +56,49 @@ const oauthProvider: ProviderDetails = {
};
describe('ProviderConfigurationModal', () => {
it('configures Z.AI Coding Plan through the standard Desktop API key form', async () => {
const user = userEvent.setup();
const onConfigured = vi.fn();
const provider: ProviderDetails = {
...oauthProvider,
name: 'zai_coding_plan',
provider_type: 'Declarative',
is_configured: false,
supports_refresh: true,
metadata: {
...oauthProvider.metadata,
name: 'zai_coding_plan',
display_name: 'Z.AI Coding Plan',
default_model: 'glm-5.3',
config_keys: [
{ name: 'ZAI_CODING_PLAN_API_KEY', required: true, secret: true, oauth_flow: false },
],
},
};
vi.mocked(acpSaveProviderConfig).mockResolvedValue(undefined);
render(
<ProviderConfigurationModal
provider={provider}
onClose={vi.fn()}
onConfigured={onConfigured}
/>,
{
wrapper: IntlTestWrapper,
}
);
expect(screen.getByRole('heading', { name: /Configure Z.AI Coding Plan/ })).toBeInTheDocument();
await user.type(await screen.findByPlaceholderText('Your API key'), 'test-coding-plan-key');
await user.click(screen.getByRole('button', { name: 'Submit' }));
await waitFor(() =>
expect(acpSaveProviderConfig).toHaveBeenCalledWith('zai_coding_plan', [
{ key: 'ZAI_CODING_PLAN_API_KEY', value: 'test-coding-plan-key' },
])
);
expect(onConfigured).toHaveBeenCalledWith(provider);
});
it('offers to remove an existing OAuth configuration without an ACP readiness check', () => {
render(<ProviderConfigurationModal provider={oauthProvider} onClose={vi.fn()} />, {
wrapper: IntlTestWrapper,