-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
Expand file tree
/
Copy pathglm.ts
More file actions
617 lines (557 loc) · 21.7 KB
/
Copy pathglm.ts
File metadata and controls
617 lines (557 loc) · 21.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
import { randomUUID } from "node:crypto";
import type { KeyHealth } from "../services/apiKeyRotator.ts";
import { DefaultExecutor } from "./default.ts";
import {
applyConfiguredUserAgent,
mergeAbortSignals,
mergeUpstreamExtraHeaders,
type CountTokensInput,
type ExecuteInput,
type ProviderCredentials,
} from "./base.ts";
import {
buildGlmBaseHeaders,
buildGlmChatUrl,
buildGlmCodingHeaders,
buildGlmCountTokensUrl,
GLM_COUNT_TOKENS_TIMEOUT_MS,
type GlmTransport,
getGlmTransport,
} from "../config/glmProvider.ts";
import { applyProviderRequestDefaults } from "../services/providerRequestDefaults.ts";
import { stripUnsupportedParams } from "../translator/paramSupport.ts";
import { getRotatingApiKey } from "../services/apiKeyRotator.ts";
import { CLAUDE_CLI_STAINLESS_PACKAGE_VERSION } from "../config/anthropicHeaders.ts";
import {
getRuntimeVersion,
normalizeStainlessArch,
normalizeStainlessPlatform,
} from "../config/providerHeaderProfiles.ts";
import { translateNonStreamingResponse } from "../handlers/responseTranslator.ts";
import { translateRequest } from "../translator/index.ts";
import { FORMATS } from "../translator/formats.ts";
import { createSSETransformStreamWithLogger } from "../utils/stream.ts";
import { ensureStreamReadiness } from "../utils/streamReadiness.ts";
import { STREAM_READINESS_TIMEOUT_MS } from "../config/constants.ts";
import { resolveSuppressThinkClose, THINKING_MARKER_HEADER } from "../utils/thinkCloseMarker.ts";
type JsonRecord = Record<string, unknown>;
type GlmExecuteResult = Awaited<ReturnType<DefaultExecutor["execute"]>> & {
targetFormat?: string;
};
function asRecord(value: unknown): JsonRecord | null {
return value && typeof value === "object" && !Array.isArray(value) ? (value as JsonRecord) : null;
}
function getEffectiveKey(credentials: ProviderCredentials): string {
const extraKeys = (credentials.providerSpecificData?.extraApiKeys as string[] | undefined) ?? [];
if (credentials.apiKey && credentials.connectionId && extraKeys.length > 0) {
return getRotatingApiKey(credentials.connectionId, credentials.apiKey, extraKeys);
}
return credentials.apiKey || credentials.accessToken || "";
}
export type GlmEffortLevel = "low" | "high" | "max";
type GlmEffortTier = {
baseModel: string;
effort: GlmEffortLevel;
/** Transport where the upstream honors the effort selector for this family. */
transport: GlmTransport;
};
/**
* GLM-5.2 effort tiers (glm-5.2-high/-max) route exclusively through the
* Anthropic transport, where Zhipu maps Claude Code effort selectors (high/max)
* to reasoning intensity. The base model ID sent upstream is always "glm-5.2".
*
* GLM-5.3 replaced tier endpoints with a documented `reasoning_effort` request
* parameter (low|high|max, default max) on the coding chat/completions endpoint,
* so its tiers stay on the OpenAI transport and inject `reasoning_effort` +
* `thinking.type=enabled` (5.3 no longer accepts thinking disabled).
*
* https://docs.z.ai/devpack/latest-model
* https://docs.z.ai/guides/llm/glm-5.3
*/
function parseGlmEffortTier(model: string): GlmEffortTier | null {
switch (model) {
case "glm-5.2-high":
return { baseModel: "glm-5.2", effort: "high", transport: "anthropic" };
case "glm-5.2-max":
return { baseModel: "glm-5.2", effort: "max", transport: "anthropic" };
case "glm-5.3-high":
return { baseModel: "glm-5.3", effort: "high", transport: "openai" };
case "glm-5.3-low":
return { baseModel: "glm-5.3", effort: "low", transport: "openai" };
case "glm-5.3-max":
return { baseModel: "glm-5.3", effort: "max", transport: "openai" };
case "glm-5.3-flash-high":
return { baseModel: "glm-5.3-flash", effort: "high", transport: "openai" };
case "glm-5.3-flash-low":
return { baseModel: "glm-5.3-flash", effort: "low", transport: "openai" };
case "glm-5.3-flash-max":
return { baseModel: "glm-5.3-flash", effort: "max", transport: "openai" };
default:
return null;
}
}
/**
* Detects GLM models that support deep thinking (5.2+).
* These models share a single max_tokens budget for reasoning + response
* (Z.AI does not document a separate thinking budget). When the client
* doesn't explicitly request max_tokens, we default to the model's full
* output capacity so reasoning isn't truncated by a low generic default.
*
* To add future models (e.g. glm-5.3, glm-5.4), just extend the regex.
* https://docs.z.ai/guides/overview/concept-param
*/
const GLM_THINKING_MODEL_PATTERN = /^glm-5\.(?:[2-9]|\d{2,})/i;
const GLM_53_OR_HIGHER_PATTERN = /^glm-5\.(?:[3-9]|\d{2,})/i;
function isGlmThinkingModel(model: string): boolean {
return GLM_THINKING_MODEL_PATTERN.test(model);
}
/**
* Z.AI's official max output for GLM-5.2+ is 131072 tokens (128K).
* This budget covers BOTH reasoning and the final response.
* https://z.ai/blog/glm-5.2
*/
const GLM_THINKING_DEFAULT_MAX_TOKENS = 131072;
function applyGlmRequestDefaults(body: unknown, defaults?: JsonRecord | null): unknown {
const record = asRecord(body);
if (!record || !defaults) return body;
const next = { ...(applyProviderRequestDefaults(record, defaults) as JsonRecord) };
const thinkingType = typeof defaults.thinkingType === "string" ? defaults.thinkingType : null;
if (thinkingType && next.thinking === undefined) {
next.thinking = { type: thinkingType };
} else if (thinkingType && asRecord(next.thinking)?.type === "enabled") {
next.thinking = { ...asRecord(next.thinking), type: thinkingType };
}
return next;
}
function hasTools(body: unknown): boolean {
const record = asRecord(body);
return Array.isArray(record?.tools) && record.tools.length > 0;
}
function isRetryableGlmFallbackStatus(status: number): boolean {
return status === 404 || status === 408 || status === 409 || status === 429 || status >= 500;
}
function isRetryableGlmFallbackError(error: unknown): boolean {
if (!error) return false;
const err = error instanceof Error ? error : new Error(String(error));
if (err.name === "AbortError") return false;
return true;
}
function cloneHeaders(headers: Headers): Headers {
const next = new Headers();
headers.forEach((value, key) => next.set(key, value));
return next;
}
function isJsonResponse(response: Response): boolean {
return (response.headers.get("content-type") || "").toLowerCase().includes("application/json");
}
async function translateJsonResponse(response: Response): Promise<Response> {
const parsed = await response.json().catch(() => null);
const translated = translateNonStreamingResponse(parsed, FORMATS.CLAUDE, FORMATS.OPENAI);
const headers = cloneHeaders(response.headers);
headers.set("content-type", "application/json");
headers.delete("content-length");
return new Response(JSON.stringify(translated), {
status: response.status,
statusText: response.statusText,
headers,
});
}
async function translateAnthropicJsonResponse(response: Response): Promise<Response> {
const parsed = await response.json().catch(() => null);
const translated = response.ok
? translateNonStreamingResponse(parsed, FORMATS.CLAUDE, FORMATS.OPENAI)
: translateAnthropicJsonError(parsed);
const headers = cloneHeaders(response.headers);
headers.set("content-type", "application/json");
headers.delete("content-length");
return new Response(JSON.stringify(translated), {
status: response.status,
statusText: response.statusText,
headers,
});
}
function translateAnthropicJsonError(parsed: unknown): JsonRecord {
const root = asRecord(parsed) || {};
const error = asRecord(root.error) || root;
const message =
typeof error.message === "string" && error.message.trim()
? error.message
: typeof root.message === "string" && root.message.trim()
? root.message
: "GLM Anthropic transport error";
const type =
typeof error.type === "string" && error.type.trim()
? error.type
: typeof root.type === "string" && root.type.trim()
? root.type
: "upstream_error";
return {
error: {
message,
type,
},
};
}
export function translateSseResponse(
response: Response,
provider: string,
model: string,
suppressThinkClose: boolean = false
): Response {
if (!response.body) return response;
// GLM is a high-throughput provider — use a larger stream buffer (64KB) to
// keep provider → client pacing ahead of the model's token emission rate.
const transform = createSSETransformStreamWithLogger(
FORMATS.CLAUDE,
FORMATS.OPENAI,
provider,
null,
null,
model,
null,
null,
null,
null,
null,
false,
suppressThinkClose,
undefined,
undefined,
65536
);
const headers = cloneHeaders(response.headers);
headers.set("content-type", "text/event-stream");
headers.delete("content-length");
return new Response(response.body.pipeThrough(transform), {
status: response.status,
statusText: response.statusText,
headers,
});
}
export class GlmExecutor extends DefaultExecutor {
constructor(provider = "glm") {
super(provider);
}
buildUrl(
_model: string,
_stream: boolean,
_urlIndex = 0,
credentials: ProviderCredentials | null = null
) {
const primaryTransport = getGlmTransport(credentials?.providerSpecificData);
const transport =
_urlIndex === 1 ? (primaryTransport === "openai" ? "anthropic" : "openai") : primaryTransport;
return buildGlmChatUrl(credentials?.providerSpecificData, transport, this.config.baseUrl);
}
buildCountTokensUrl(_model: string, credentials: ProviderCredentials | null = null) {
return buildGlmCountTokensUrl(credentials?.providerSpecificData, this.config.baseUrl);
}
getCountTokensTimeoutMs() {
return GLM_COUNT_TOKENS_TIMEOUT_MS;
}
buildHeaders(
credentials: ProviderCredentials,
stream = true,
_clientHeaders?: Record<string, string> | null,
_model?: string,
_health?: unknown,
_body?: unknown
): Record<string, string> {
const transport: GlmTransport = getGlmTransport(credentials.providerSpecificData);
if (transport === "openai") {
return buildGlmCodingHeaders(getEffectiveKey(credentials), stream);
}
return {
...buildGlmBaseHeaders(getEffectiveKey(credentials), stream),
"X-Stainless-Arch": normalizeStainlessArch(),
"X-Stainless-OS": normalizeStainlessPlatform(),
"X-Stainless-Runtime-Version": getRuntimeVersion(),
"X-Stainless-Package-Version": CLAUDE_CLI_STAINLESS_PACKAGE_VERSION,
"X-Claude-Code-Session-Id": randomUUID(),
"x-client-request-id": randomUUID(),
};
}
transformRequest(
model: string,
body: unknown,
stream: boolean,
credentials: ProviderCredentials
) {
const cleanedBody = super.transformRequest(model, body, stream, credentials);
return applyGlmRequestDefaults(cleanedBody, this.config.requestDefaults as JsonRecord | null);
}
transformForTransport(
model: string,
body: unknown,
stream: boolean,
credentials: ProviderCredentials,
transport: GlmTransport
) {
const effortTier = parseGlmEffortTier(model);
const effectiveModel = effortTier ? effortTier.baseModel : model;
const transformed = this.transformRequest(effectiveModel, body, stream, credentials);
const record = asRecord(transformed);
// #7364: unlike DefaultExecutor.execute() (default.ts), GlmExecutor.execute()
// never calls the base execute() loop — it drives its own fetch via
// executeTransport()/transformForTransport() — so stripUnsupportedParams()
// (normally applied at default.ts's execute() call site) never ran for GLM
// requests. Without this call, a STRIP_RULES clamp entry for provider "glm"
// (e.g. the glm-4.6v max_tokens ceiling) would be silently dead code.
if (record) stripUnsupportedParams(this.provider, effectiveModel, record);
// Ensure upstream receives the base model ID, not the effort-suffixed alias
if (record && effortTier) {
record.model = effectiveModel;
}
// GLM-5.2+ models share a single max_tokens budget for reasoning + response.
// When the client doesn't explicitly set max_tokens, default to the model's
// full output capacity (131072) so deep reasoning isn't truncated by the
// generic translator defaults (64000 for Anthropic, 16384 for OpenAI).
// This acts as the "transparent proxy override" described in Z.AI's own
// Terminal-Bench evaluation methodology.
// https://huggingface.co/blog/zai-org/glm-52-blog
if (record && isGlmThinkingModel(effectiveModel)) {
const clientBody = asRecord(body);
const clientMaxTokens = clientBody?.max_tokens ?? clientBody?.max_completion_tokens;
if (!clientMaxTokens) {
record.max_tokens = GLM_THINKING_DEFAULT_MAX_TOKENS;
}
}
if (transport === "openai") {
// GLM-5.3+ rejects thinking.type "disabled". Ensure thinking is enabled
// when targeting GLM-5.3 or higher.
if (record && GLM_53_OR_HIGHER_PATTERN.test(effectiveModel)) {
const existingThinking = asRecord(record.thinking);
if (existingThinking?.type === "disabled") {
record.thinking = { ...existingThinking, type: "enabled" };
}
}
// GLM-5.3 effort tiers: inject the documented `reasoning_effort` param and
// force thinking on — 5.3 rejects thinking.type "disabled", and an effort
// tier without thinking would silently drop the selector upstream.
if (record && effortTier && effortTier.transport === "openai") {
const existingThinking = asRecord(record.thinking);
record.thinking = { ...existingThinking, type: "enabled" };
record.reasoning_effort = effortTier.effort;
}
if (record && stream && hasTools(record) && record.tool_stream === undefined) {
return { ...record, tool_stream: true };
}
return transformed;
}
const translated = translateRequest(
FORMATS.OPENAI,
FORMATS.CLAUDE,
effectiveModel,
{ ...(record ?? {}), _disableToolPrefix: true },
stream,
credentials,
this.provider,
null,
{ preserveCacheControl: false }
);
// Inject effort and thinking for the Anthropic transport.
// Zhipu's Anthropic endpoint requires thinking.type=enabled to emit
// thinking_delta blocks in the SSE response. Without it, reasoning is
// not surfaced and clients see no thinking content.
// The effort-2025-11-24 beta header (in GLM_ANTHROPIC_BETA) carries
// the high/max intensity selector.
if (effortTier) {
const translatedRecord = asRecord(translated);
if (translatedRecord) {
translatedRecord.effort = effortTier.effort;
// Zhipu's Anthropic endpoint only supports thinking.type
// "enabled"/"disabled" — not "adaptive". Clients like Claude Code
// default to "adaptive" for reasoning models, so force "enabled"
// here while preserving any other fields (e.g. budget_tokens).
const existingThinking = asRecord(translatedRecord.thinking);
if (!existingThinking || existingThinking.type !== "enabled") {
translatedRecord.thinking = {
...existingThinking,
type: "enabled",
};
}
}
}
return translated;
}
private async executeTransport(
input: ExecuteInput,
transport: GlmTransport
): Promise<GlmExecuteResult> {
const credentials = input.credentials;
const url = buildGlmChatUrl(credentials?.providerSpecificData, transport, this.config.baseUrl);
// #10798 moved the transport out of buildHeaders' signature; the Anthropic
// transport must therefore be visible to buildHeaders through
// providerSpecificData (primaryTransport / anthropic-shaped baseUrl).
const headers =
transport === "anthropic"
? this.buildHeaders(
{
...credentials,
providerSpecificData: {
...credentials?.providerSpecificData,
primaryTransport: "anthropic",
},
},
input.stream,
input.clientHeaders,
input.model
)
: this.buildHeaders(credentials, input.stream, input.clientHeaders, input.model);
applyConfiguredUserAgent(headers, credentials.providerSpecificData);
mergeUpstreamExtraHeaders(headers, input.upstreamExtraHeaders);
const transformedBody = this.transformForTransport(
input.model,
input.body,
input.stream,
credentials,
transport
);
const fetchStartTimeoutMs = this.getTimeoutMs();
const timeoutController = fetchStartTimeoutMs > 0 ? new AbortController() : null;
let timeoutId: ReturnType<typeof setTimeout> | null = null;
if (timeoutController) {
timeoutId = setTimeout(() => {
const timeoutError = new Error(`Fetch timeout after ${fetchStartTimeoutMs}ms on ${url}`);
timeoutError.name = "TimeoutError";
timeoutController.abort(timeoutError);
}, fetchStartTimeoutMs);
}
const timeoutSignal = timeoutController?.signal ?? null;
const combinedSignal =
input.signal && timeoutSignal
? mergeAbortSignals(input.signal, timeoutSignal)
: input.signal || timeoutSignal;
let response: Response;
try {
this.assertOutboundUrlAllowed(url); // GHSA-4f49: glm has its own fetch path
response = await fetch(url, {
method: "POST",
headers,
body: JSON.stringify(transformedBody),
signal: combinedSignal || undefined,
});
} finally {
if (timeoutId) clearTimeout(timeoutId);
}
if (input.stream && response.ok) {
const readiness = await ensureStreamReadiness(response, {
timeoutMs: STREAM_READINESS_TIMEOUT_MS,
provider: this.provider,
model: input.model,
log: input.log,
});
response = readiness.response;
}
const result = { response, url, headers, transformedBody };
if (transport === "anthropic") {
return this.finalizeAnthropicTransportResult(input, result);
}
return {
...result,
url,
headers,
transformedBody,
targetFormat: FORMATS.OPENAI,
};
}
/**
* GLM's Anthropic transport does its own Claude→OpenAI translation
* (bypassing chatCore's stream), so the `</think>` close-marker
* suppression flag and the response translation both have to be resolved
* here from the original client headers (#5245 / #5312). Extracted from
* `executeTransport` to keep that method's cyclomatic complexity under the
* project cap.
*/
private async finalizeAnthropicTransportResult(
input: ExecuteInput,
result: {
response: Response;
url: string;
headers: Record<string, string>;
transformedBody: unknown;
}
): Promise<GlmExecuteResult> {
const { response: rawResponse, url, headers, transformedBody } = result;
const clientHeaders = input.clientHeaders ?? {};
const suppressThinkClose = resolveSuppressThinkClose({
userAgent: clientHeaders["user-agent"] ?? clientHeaders["User-Agent"] ?? null,
thinkingMarkerHeader:
clientHeaders[THINKING_MARKER_HEADER] ??
clientHeaders["x-omniroute-thinking-marker"] ??
null,
clientResponseFormat: input.clientResponseFormat ?? null,
});
const translatedResponse =
input.stream && rawResponse.ok
? translateSseResponse(rawResponse, this.provider, input.model, suppressThinkClose)
: isJsonResponse(rawResponse)
? await translateAnthropicJsonResponse(rawResponse)
: rawResponse;
return {
response: translatedResponse,
url,
headers,
transformedBody,
targetFormat: FORMATS.OPENAI,
};
}
async execute(input: ExecuteInput): Promise<GlmExecuteResult> {
const effortTier = parseGlmEffortTier(input.model);
// Effort tiers route directly through their family's transport (no fallback):
// GLM-5.2 → Anthropic (Zhipu only graduates effort there, via the
// effort-2025-11-24 beta header in GLM_ANTHROPIC_BETA); GLM-5.3 → OpenAI
// coding endpoint (`reasoning_effort` param). See parseGlmEffortTier.
if (effortTier) {
return this.executeTransport(input, effortTier.transport);
}
const primaryTransport = getGlmTransport(
input.credentials.providerSpecificData,
this.config.baseUrl
);
const fallbackTransport: GlmTransport = primaryTransport === "openai" ? "anthropic" : "openai";
let primaryResult: GlmExecuteResult | null = null;
try {
primaryResult = await this.executeTransport(input, primaryTransport);
if (!isRetryableGlmFallbackStatus(primaryResult.response.status)) {
return primaryResult;
}
input.log?.debug?.(
"GLM_FALLBACK",
`${primaryTransport} returned ${primaryResult.response.status}; trying ${fallbackTransport}`
);
} catch (error) {
if (!isRetryableGlmFallbackError(error)) throw error;
input.log?.debug?.(
"GLM_FALLBACK",
`${primaryTransport} error (${error instanceof Error ? error.message : String(error)}); trying ${fallbackTransport}`
);
}
try {
const fallbackResult = await this.executeTransport(input, fallbackTransport);
if (fallbackResult.response.ok || !primaryResult) {
return fallbackResult;
}
} catch (error) {
if (!primaryResult) throw error;
input.log?.debug?.(
"GLM_FALLBACK",
`${fallbackTransport} fallback failed (${error instanceof Error ? error.message : String(error)}); returning primary response`
);
}
return primaryResult;
}
async countTokens(input: CountTokensInput) {
return super.countTokens({
...input,
credentials: {
...input.credentials,
providerSpecificData: {
...(input.credentials.providerSpecificData || {}),
primaryTransport: "anthropic",
},
},
});
}
}
export default GlmExecutor;