@molecule/api-ai-zhipu 1.0.2 → 1.1.1

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 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-26T19:42:20.290Z
6
+ Generated: 2026-08-28T08:14:50.657Z
7
7
  -->
8
8
 
9
9
  # @molecule/api-ai-zhipu
@@ -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
 
@@ -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';
@@ -1 +1 @@
1
- {"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EACV,UAAU,EAOX,MAAM,kBAAkB,CAAA;AASzB,OAAO,cAAc,CAAA;AAErB,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAmlB7C;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,MAAM,CAAC,EAAE,WAAW,GAAG,UAAU,CAE/D"}
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';
@@ -41,6 +47,7 @@ class ZhipuAIProvider {
41
47
  baseUrl;
42
48
  completionsPath;
43
49
  modelMap;
50
+ supportsServerTools;
44
51
  onRateLimit;
45
52
  constructor(config = {}) {
46
53
  this.apiKey = config.apiKey ?? process.env.ZHIPU_API_KEY ?? '';
@@ -50,6 +57,7 @@ class ZhipuAIProvider {
50
57
  this.completionsPath =
51
58
  config.completionsPath ?? process.env.ZHIPU_COMPLETIONS_PATH ?? '/v4/chat/completions';
52
59
  this.modelMap = config.modelMap ?? {};
60
+ this.supportsServerTools = config.supportsServerTools ?? true;
53
61
  this.onRateLimit = config.onRateLimit;
54
62
  // Fail fast with an actionable local error rather than a cryptic 401 on the
55
63
  // first request. The default `provider` export constructs lazily on first
@@ -82,12 +90,31 @@ class ZhipuAIProvider {
82
90
  stream: params.stream !== false,
83
91
  ...(params.stream !== false ? { stream_options: { include_usage: true } } : {}),
84
92
  };
85
- // 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.
86
102
  const functions = params.tools?.length ? this.formatTools(params.tools) : [];
87
- const serverTools = (params.serverTools ?? []).map((st) => ({ ...st }));
103
+ const serverTools = this.supportsServerTools
104
+ ? (params.serverTools ?? []).map((st) => this.formatServerTool(st))
105
+ : [];
88
106
  const allTools = [...functions, ...serverTools];
89
107
  if (allTools.length > 0) {
90
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
+ }
91
118
  }
92
119
  if (params.thinking) {
93
120
  // Prefer the caller-resolved native value (minimal | high | max on
@@ -277,6 +304,22 @@ class ZhipuAIProvider {
277
304
  }
278
305
  return formatted;
279
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
+ }
280
323
  /**
281
324
  * Converts internal `AITool` definitions to the OpenAI function calling format.
282
325
  *
package/dist/types.d.ts CHANGED
@@ -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.
@@ -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;CAClC;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"}
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,7 +1,8 @@
1
1
  {
2
2
  "name": "@molecule/api-ai-zhipu",
3
- "version": "1.0.2",
3
+ "version": "1.1.1",
4
4
  "description": "Zhipu GLM AI provider for molecule.dev",
5
+ "homepage": "https://www.molecule.dev/packages/api-ai-zhipu",
5
6
  "type": "module",
6
7
  "main": "dist/index.js",
7
8
  "types": "dist/index.d.ts",
@@ -33,11 +34,11 @@
33
34
  "directory": "packages/api/bonds/ai/zhipu"
34
35
  },
35
36
  "devDependencies": {
36
- "@molecule/api-ai": "1.1.0",
37
- "@molecule/api-bond": "1.0.1",
37
+ "@molecule/api-ai": "1.2.1",
38
+ "@molecule/api-bond": "1.0.2",
38
39
  "@types/node": "26.1.2",
39
40
  "typescript": "6.0.3",
40
- "vitest": "4.1.10"
41
+ "vitest": "4.1.11"
41
42
  },
42
43
  "peerDependencies": {
43
44
  "@molecule/api-ai": "^1.0.1",