@molecule/api-ai-zhipu 1.0.1 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -2
- package/dist/provider.d.ts +6 -0
- package/dist/provider.d.ts.map +1 -1
- package/dist/provider.js +50 -5
- package/dist/types.d.ts +10 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@ AUTO-GENERATED — DO NOT EDIT THIS FILE.
|
|
|
3
3
|
Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
|
|
4
4
|
Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
|
|
5
5
|
To change this document, edit the module-level JSDoc in src/index.ts.
|
|
6
|
-
Generated: 2026-08-
|
|
6
|
+
Generated: 2026-08-28T08:14:50.657Z
|
|
7
7
|
-->
|
|
8
8
|
|
|
9
9
|
# @molecule/api-ai-zhipu
|
|
@@ -52,7 +52,7 @@ interface ZhipuConfig {
|
|
|
52
52
|
onRateLimit?: AiRateLimitCallback
|
|
53
53
|
/** API key. Defaults to ZHIPU_API_KEY env var. */
|
|
54
54
|
apiKey?: string
|
|
55
|
-
/** Default model. Defaults to 'glm-5.
|
|
55
|
+
/** Default model. Defaults to 'glm-5.3'. */
|
|
56
56
|
defaultModel?: string
|
|
57
57
|
/** Maximum tokens for completions. */
|
|
58
58
|
maxTokens?: number
|
|
@@ -71,6 +71,15 @@ interface ZhipuConfig {
|
|
|
71
71
|
* pricing/cost/display keep the canonical catalog id (`glm-5.2`).
|
|
72
72
|
*/
|
|
73
73
|
modelMap?: Record<string, string>
|
|
74
|
+
/**
|
|
75
|
+
* Whether the upstream host serves Zhipu's provider-side server tools
|
|
76
|
+
* (`web_search`). Defaults to `true` (the native z.ai/bigmodel endpoint).
|
|
77
|
+
* Set to `false` for OpenAI-compatible re-hosts of the open weights
|
|
78
|
+
* (DeepInfra), which run the model only — a server-tool entry in `tools`
|
|
79
|
+
* is rejected there (HTTP 422) and would fail the whole request, so the
|
|
80
|
+
* provider drops `params.serverTools` instead of forwarding them.
|
|
81
|
+
*/
|
|
82
|
+
supportsServerTools?: boolean
|
|
74
83
|
}
|
|
75
84
|
```
|
|
76
85
|
|
package/dist/provider.d.ts
CHANGED
|
@@ -3,6 +3,12 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Uses the Zhipu API (OpenAI-compatible) for streaming chat completions with tool use.
|
|
5
5
|
*
|
|
6
|
+
* Server tools (`params.serverTools`, e.g. web_search) are translated to
|
|
7
|
+
* Zhipu's native nested schema (`{ type: 'web_search', web_search: {} }`);
|
|
8
|
+
* on hosts that only serve the open weights (`supportsServerTools: false`,
|
|
9
|
+
* e.g. DeepInfra) they are dropped rather than forwarded, since any server
|
|
10
|
+
* tool entry fails the whole request there.
|
|
11
|
+
*
|
|
6
12
|
* @module
|
|
7
13
|
*/
|
|
8
14
|
import type { AIProvider } from '@molecule/api-ai';
|
package/dist/provider.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,EACV,UAAU,EAQX,MAAM,kBAAkB,CAAA;AASzB,OAAO,cAAc,CAAA;AAErB,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAwnB7C;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,MAAM,CAAC,EAAE,WAAW,GAAG,UAAU,CAE/D"}
|
package/dist/provider.js
CHANGED
|
@@ -3,6 +3,12 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Uses the Zhipu API (OpenAI-compatible) for streaming chat completions with tool use.
|
|
5
5
|
*
|
|
6
|
+
* Server tools (`params.serverTools`, e.g. web_search) are translated to
|
|
7
|
+
* Zhipu's native nested schema (`{ type: 'web_search', web_search: {} }`);
|
|
8
|
+
* on hosts that only serve the open weights (`supportsServerTools: false`,
|
|
9
|
+
* e.g. DeepInfra) they are dropped rather than forwarded, since any server
|
|
10
|
+
* tool entry fails the whole request there.
|
|
11
|
+
*
|
|
6
12
|
* @module
|
|
7
13
|
*/
|
|
8
14
|
import { getLogger } from '@molecule/api-bond';
|
|
@@ -15,8 +21,10 @@ import './secrets.js';
|
|
|
15
21
|
/**
|
|
16
22
|
* Fallback: map thinking budget tokens to a Zhipu reasoning_effort level when
|
|
17
23
|
* the caller didn't resolve a native value from the model catalog. Note
|
|
18
|
-
* `reasoning_effort` is only honored on glm-5.2+ (
|
|
19
|
-
* medium | high | xhigh | max
|
|
24
|
+
* `reasoning_effort` is only honored on glm-5.2+ (glm-5.2 takes minimal | none |
|
|
25
|
+
* low | medium | high | xhigh | max, where low/medium coerce to high and xhigh
|
|
26
|
+
* to max; glm-5.3 narrows this to low | high | max and can no longer disable
|
|
27
|
+
* reasoning at all — both accept the `'high'`/`'low'` this returns) —
|
|
20
28
|
* callers should prefer passing `thinking.effort`; older GLM models only
|
|
21
29
|
* support thinking on/off and should not receive a thinking param at all.
|
|
22
30
|
*
|
|
@@ -39,15 +47,17 @@ class ZhipuAIProvider {
|
|
|
39
47
|
baseUrl;
|
|
40
48
|
completionsPath;
|
|
41
49
|
modelMap;
|
|
50
|
+
supportsServerTools;
|
|
42
51
|
onRateLimit;
|
|
43
52
|
constructor(config = {}) {
|
|
44
53
|
this.apiKey = config.apiKey ?? process.env.ZHIPU_API_KEY ?? '';
|
|
45
|
-
this.defaultModel = config.defaultModel ?? 'glm-5.
|
|
54
|
+
this.defaultModel = config.defaultModel ?? 'glm-5.3';
|
|
46
55
|
this.maxTokens = config.maxTokens ?? 4096;
|
|
47
56
|
this.baseUrl = config.baseUrl ?? process.env.ZHIPU_BASE_URL ?? 'https://api.z.ai/api/paas';
|
|
48
57
|
this.completionsPath =
|
|
49
58
|
config.completionsPath ?? process.env.ZHIPU_COMPLETIONS_PATH ?? '/v4/chat/completions';
|
|
50
59
|
this.modelMap = config.modelMap ?? {};
|
|
60
|
+
this.supportsServerTools = config.supportsServerTools ?? true;
|
|
51
61
|
this.onRateLimit = config.onRateLimit;
|
|
52
62
|
// Fail fast with an actionable local error rather than a cryptic 401 on the
|
|
53
63
|
// first request. The default `provider` export constructs lazily on first
|
|
@@ -80,12 +90,31 @@ class ZhipuAIProvider {
|
|
|
80
90
|
stream: params.stream !== false,
|
|
81
91
|
...(params.stream !== false ? { stream_options: { include_usage: true } } : {}),
|
|
82
92
|
};
|
|
83
|
-
// Tools: merge custom tools (function format) and server tools
|
|
93
|
+
// Tools: merge custom tools (function format) and server tools.
|
|
94
|
+
// Server tools are translated to Zhipu's NATIVE schema — the caller-facing
|
|
95
|
+
// `{ type, name }` marker is not valid on the Zhipu API (a bare
|
|
96
|
+
// `{ type: 'web_search', name: 'web_search' }` entry is a 400,
|
|
97
|
+
// "tools[1].web_search can not be null": Zhipu requires the tool's config
|
|
98
|
+
// nested under a key matching the type, e.g. `web_search: {}`). On hosts
|
|
99
|
+
// without server tools (supportsServerTools: false — OpenAI-compat
|
|
100
|
+
// re-hosts of the open weights) they are dropped entirely, because ANY
|
|
101
|
+
// web_search entry fails the whole request there.
|
|
84
102
|
const functions = params.tools?.length ? this.formatTools(params.tools) : [];
|
|
85
|
-
const serverTools =
|
|
103
|
+
const serverTools = this.supportsServerTools
|
|
104
|
+
? (params.serverTools ?? []).map((st) => this.formatServerTool(st))
|
|
105
|
+
: [];
|
|
86
106
|
const allTools = [...functions, ...serverTools];
|
|
87
107
|
if (allTools.length > 0) {
|
|
88
108
|
body.tools = allTools;
|
|
109
|
+
// Honor forced tool choice (discovery mode relies on it — an unenforced
|
|
110
|
+
// 'required' lets the model narrate the call as text instead of emitting
|
|
111
|
+
// tool_use). OpenAI-compatible forms; 'auto' is the API default → omit.
|
|
112
|
+
if (params.toolChoice === 'required') {
|
|
113
|
+
body.tool_choice = 'required';
|
|
114
|
+
}
|
|
115
|
+
else if (typeof params.toolChoice === 'object' && params.toolChoice.type === 'tool') {
|
|
116
|
+
body.tool_choice = { type: 'function', function: { name: params.toolChoice.name } };
|
|
117
|
+
}
|
|
89
118
|
}
|
|
90
119
|
if (params.thinking) {
|
|
91
120
|
// Prefer the caller-resolved native value (minimal | high | max on
|
|
@@ -275,6 +304,22 @@ class ZhipuAIProvider {
|
|
|
275
304
|
}
|
|
276
305
|
return formatted;
|
|
277
306
|
}
|
|
307
|
+
/**
|
|
308
|
+
* Converts a caller-facing `ServerTool` marker into Zhipu's native tool
|
|
309
|
+
* schema. Zhipu requires the tool's configuration object nested under a key
|
|
310
|
+
* matching its type (`{ type: 'web_search', web_search: { ... } }`) — the
|
|
311
|
+
* abstract `{ type, name }` marker alone is rejected with HTTP 400
|
|
312
|
+
* `"tools[N].web_search can not be null"`. Any provider-specific extra
|
|
313
|
+
* fields on the marker (search_engine, search_recency_filter, …) become the
|
|
314
|
+
* nested config.
|
|
315
|
+
*
|
|
316
|
+
* @param st - The abstract server tool marker from ChatParams.
|
|
317
|
+
* @returns The Zhipu-native tool object.
|
|
318
|
+
*/
|
|
319
|
+
formatServerTool(st) {
|
|
320
|
+
const { type, name: _name, ...rest } = st;
|
|
321
|
+
return { type, [type]: rest };
|
|
322
|
+
}
|
|
278
323
|
/**
|
|
279
324
|
* Converts internal `AITool` definitions to the OpenAI function calling format.
|
|
280
325
|
*
|
package/dist/types.d.ts
CHANGED
|
@@ -12,7 +12,7 @@ export interface ZhipuConfig {
|
|
|
12
12
|
onRateLimit?: AiRateLimitCallback;
|
|
13
13
|
/** API key. Defaults to ZHIPU_API_KEY env var. */
|
|
14
14
|
apiKey?: string;
|
|
15
|
-
/** Default model. Defaults to 'glm-5.
|
|
15
|
+
/** Default model. Defaults to 'glm-5.3'. */
|
|
16
16
|
defaultModel?: string;
|
|
17
17
|
/** Maximum tokens for completions. */
|
|
18
18
|
maxTokens?: number;
|
|
@@ -31,6 +31,15 @@ export interface ZhipuConfig {
|
|
|
31
31
|
* pricing/cost/display keep the canonical catalog id (`glm-5.2`).
|
|
32
32
|
*/
|
|
33
33
|
modelMap?: Record<string, string>;
|
|
34
|
+
/**
|
|
35
|
+
* Whether the upstream host serves Zhipu's provider-side server tools
|
|
36
|
+
* (`web_search`). Defaults to `true` (the native z.ai/bigmodel endpoint).
|
|
37
|
+
* Set to `false` for OpenAI-compatible re-hosts of the open weights
|
|
38
|
+
* (DeepInfra), which run the model only — a server-tool entry in `tools`
|
|
39
|
+
* is rejected there (HTTP 422) and would fail the whole request, so the
|
|
40
|
+
* provider drops `params.serverTools` instead of forwarding them.
|
|
41
|
+
*/
|
|
42
|
+
supportsServerTools?: boolean;
|
|
34
43
|
}
|
|
35
44
|
/**
|
|
36
45
|
* Process Env interface.
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAA;AAE3D;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,wFAAwF;IACxF,WAAW,CAAC,EAAE,mBAAmB,CAAA;IACjC,kDAAkD;IAClD,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,4CAA4C;IAC5C,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,sCAAsC;IACtC,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,wFAAwF;IACxF,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB;;;;;OAKG;IACH,eAAe,CAAC,EAAE,MAAM,CAAA;IACxB;;;;OAIG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAA;AAE3D;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,wFAAwF;IACxF,WAAW,CAAC,EAAE,mBAAmB,CAAA;IACjC,kDAAkD;IAClD,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,4CAA4C;IAC5C,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,sCAAsC;IACtC,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,wFAAwF;IACxF,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB;;;;;OAKG;IACH,eAAe,CAAC,EAAE,MAAM,CAAA;IACxB;;;;OAIG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IACjC;;;;;;;OAOG;IACH,mBAAmB,CAAC,EAAE,OAAO,CAAA;CAC9B;AAED;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,aAAa,EAAE,MAAM,CAAA;IACrB,0FAA0F;IAC1F,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,gFAAgF;IAChF,sBAAsB,CAAC,EAAE,MAAM,CAAA;CAChC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@molecule/api-ai-zhipu",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Zhipu GLM AI provider for molecule.dev",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"directory": "packages/api/bonds/ai/zhipu"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@molecule/api-ai": "1.0
|
|
36
|
+
"@molecule/api-ai": "1.2.0",
|
|
37
37
|
"@molecule/api-bond": "1.0.1",
|
|
38
38
|
"@types/node": "26.1.2",
|
|
39
39
|
"typescript": "6.0.3",
|