Skip to content

Commit 9dd2fff

Browse files
committed
Fix Z.ai e2e state reset and Responses API tool fallback
1 parent 39fb2c6 commit 9dd2fff

3 files changed

Lines changed: 72 additions & 19 deletions

File tree

apps/vscode-e2e/src/suite/providers/zai.test.ts

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ import { setDefaultSuiteTimeout } from "../test-utils"
1919
type ZAiFixture = { match: string; result: string }
2020
type ZAiRequestCapture = { maxTokens?: number }
2121

22+
function getBaseZAiConfiguration() {
23+
return {
24+
apiProvider: "zai" as const,
25+
zaiApiKey: ZAI_API_KEY ?? "mock-key",
26+
zaiApiLine: "international_api" as const,
27+
modelMaxTokens: undefined,
28+
modelMaxThinkingTokens: undefined,
29+
}
30+
}
31+
2232
function installZAiFetchInterceptor(
2333
fixtures: ZAiFixture[],
2434
capture?: ZAiRequestCapture,
@@ -151,6 +161,10 @@ suite("Z.ai GLM provider", function () {
151161
let restoreFetch: (() => void) | undefined
152162
const requestCapture: ZAiRequestCapture = {}
153163

164+
setup(() => {
165+
requestCapture.maxTokens = undefined
166+
})
167+
154168
suiteSetup(async () => {
155169
restoreFetch = installZAiFetchInterceptor(
156170
[
@@ -161,12 +175,14 @@ suite("Z.ai GLM provider", function () {
161175
!!ZAI_API_KEY,
162176
)
163177

164-
await globalThis.api.setConfiguration({
165-
apiProvider: "zai" as const,
166-
zaiApiKey: ZAI_API_KEY ?? "mock-key",
167-
zaiApiLine: "international_api" as const,
168-
apiModelId: "glm-5.1",
169-
})
178+
await globalThis.api.upsertProfile(
179+
"default",
180+
{
181+
...getBaseZAiConfiguration(),
182+
apiModelId: "glm-5.1",
183+
},
184+
true,
185+
)
170186
})
171187

172188
suiteTeardown(async () => {
@@ -175,12 +191,16 @@ suite("Z.ai GLM provider", function () {
175191

176192
const aimockUrl = process.env.AIMOCK_URL
177193
const isRecord = process.env.AIMOCK_RECORD === "true"
178-
await globalThis.api.setConfiguration({
179-
apiProvider: "openrouter" as const,
180-
openRouterApiKey: aimockUrl && !isRecord ? "mock-key" : process.env.OPENROUTER_API_KEY!,
181-
openRouterModelId: "openai/gpt-4.1",
182-
...(aimockUrl && { openRouterBaseUrl: `${aimockUrl}/v1` }),
183-
})
194+
await globalThis.api.upsertProfile(
195+
"default",
196+
{
197+
apiProvider: "openrouter" as const,
198+
openRouterApiKey: aimockUrl && !isRecord ? "mock-key" : process.env.OPENROUTER_API_KEY!,
199+
openRouterModelId: "openai/gpt-4.1",
200+
...(aimockUrl && { openRouterBaseUrl: `${aimockUrl}/v1` }),
201+
},
202+
true,
203+
)
184204
})
185205

186206
test("Should complete a task end-to-end using glm-5.1 via Z.ai provider", async () => {
@@ -216,12 +236,14 @@ suite("Z.ai GLM provider", function () {
216236
})
217237

218238
test("Should complete a task end-to-end using glm-5-turbo via Z.ai provider", async () => {
219-
await globalThis.api.setConfiguration({
220-
apiProvider: "zai" as const,
221-
zaiApiKey: ZAI_API_KEY ?? "mock-key",
222-
zaiApiLine: "international_api" as const,
223-
apiModelId: "glm-5-turbo",
224-
})
239+
await globalThis.api.upsertProfile(
240+
"default",
241+
{
242+
...getBaseZAiConfiguration(),
243+
apiModelId: "glm-5-turbo",
244+
},
245+
true,
246+
)
225247

226248
const api = globalThis.api
227249
const messages: ClineMessage[] = []

src/api/transform/__tests__/responses-api-stream.spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,37 @@ describe("processResponsesApiStream", () => {
235235
},
236236
])
237237
})
238+
239+
it("should fall back to output_item.done when delta omits the tool name", async () => {
240+
const stream = mockStream([
241+
{
242+
type: "response.function_call_arguments.delta",
243+
call_id: "call_123",
244+
delta: '{"path":',
245+
index: 0,
246+
},
247+
{
248+
type: "response.output_item.done",
249+
item: {
250+
type: "function_call",
251+
call_id: "call_123",
252+
name: "read_file",
253+
arguments: '{"path":"/tmp/test.txt"}',
254+
},
255+
},
256+
])
257+
258+
const chunks = await collectChunks(processResponsesApiStream(stream, noopUsage))
259+
260+
expect(chunks).toEqual([
261+
{
262+
type: "tool_call",
263+
id: "call_123",
264+
name: "read_file",
265+
arguments: '{"path":"/tmp/test.txt"}',
266+
},
267+
])
268+
})
238269
})
239270

240271
describe("completion and usage", () => {

src/api/transform/responses-api-stream.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export async function* processResponsesApiStream(
8383
) {
8484
const callId = event.call_id || event.tool_call_id || event.id || event.item_id
8585
const name = event.name || event.function_name
86-
if (typeof callId === "string" && callId.length > 0) {
86+
if (typeof callId === "string" && callId.length > 0 && typeof name === "string" && name.length > 0) {
8787
streamedCallIds.add(callId)
8888
yield {
8989
type: "tool_call_partial",

0 commit comments

Comments
 (0)