@molecule/api-ai-zhipu 1.1.1 → 1.2.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.
@@ -9,6 +9,13 @@
9
9
  * e.g. DeepInfra) they are dropped rather than forwarded, since any server
10
10
  * tool entry fails the whole request there.
11
11
  *
12
+ * Zhipu's `web_search` is retrieval before generation, not a tool the model
13
+ * calls: it searches only with `web_search: { enable: true }`, on a
14
+ * NON-streaming request, with NO function tools present (verified live
15
+ * 2026-09-25 — otherwise the request succeeds without searching). When it does
16
+ * search, the response carries a `web_search` results array, and each such
17
+ * response is reported as one billed search (`TokenUsage.webSearchRequests`).
18
+ *
12
19
  * @module
13
20
  */
14
21
  import type { AIProvider } from '@molecule/api-ai';
@@ -1 +1 @@
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"}
1
+ {"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,KAAK,EACV,UAAU,EAQX,MAAM,kBAAkB,CAAA;AASzB,OAAO,cAAc,CAAA;AAErB,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AA8oB7C;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,MAAM,CAAC,EAAE,WAAW,GAAG,UAAU,CAE/D"}
package/dist/provider.js CHANGED
@@ -9,6 +9,13 @@
9
9
  * e.g. DeepInfra) they are dropped rather than forwarded, since any server
10
10
  * tool entry fails the whole request there.
11
11
  *
12
+ * Zhipu's `web_search` is retrieval before generation, not a tool the model
13
+ * calls: it searches only with `web_search: { enable: true }`, on a
14
+ * NON-streaming request, with NO function tools present (verified live
15
+ * 2026-09-25 — otherwise the request succeeds without searching). When it does
16
+ * search, the response carries a `web_search` results array, and each such
17
+ * response is reported as one billed search (`TokenUsage.webSearchRequests`).
18
+ *
12
19
  * @module
13
20
  */
14
21
  import { getLogger } from '@molecule/api-bond';
@@ -18,6 +25,16 @@ const logger = getLogger();
18
25
  // runtime registry is populated even when provider.js is imported directly
19
26
  // (not through the package barrel).
20
27
  import './secrets.js';
28
+ /**
29
+ * Whether a response or chunk carries web search results — Zhipu bills each
30
+ * search-grounded response as one use.
31
+ *
32
+ * @param payload - A parsed response body or stream chunk.
33
+ * @returns True when a non-empty `web_search` array is present.
34
+ */
35
+ function carriesSearchResults(payload) {
36
+ return Array.isArray(payload.web_search) && payload.web_search.length > 0;
37
+ }
21
38
  /**
22
39
  * Fallback: map thinking budget tokens to a Zhipu reasoning_effort level when
23
40
  * the caller didn't resolve a native value from the model catalog. Note
@@ -386,6 +403,7 @@ class ZhipuAIProvider {
386
403
  inputTokens: Math.max(0, (usage?.prompt_tokens ?? 0) - cachedTokens),
387
404
  outputTokens: usage?.completion_tokens ?? 0,
388
405
  ...(cachedTokens ? { cacheReadInputTokens: cachedTokens } : {}),
406
+ ...(carriesSearchResults(data) ? { webSearchRequests: 1 } : {}),
389
407
  },
390
408
  };
391
409
  }
@@ -408,7 +426,12 @@ class ZhipuAIProvider {
408
426
  }
409
427
  const decoder = new TextDecoder();
410
428
  let buffer = '';
411
- const state = { inputTokens: 0, outputTokens: 0, cacheReadTokens: 0 };
429
+ const state = {
430
+ inputTokens: 0,
431
+ outputTokens: 0,
432
+ cacheReadTokens: 0,
433
+ searched: false,
434
+ };
412
435
  // Track pending tool calls (accumulated across deltas)
413
436
  const pendingTools = new Map();
414
437
  try {
@@ -472,6 +495,7 @@ class ZhipuAIProvider {
472
495
  inputTokens: Math.max(0, state.inputTokens - state.cacheReadTokens),
473
496
  outputTokens: state.outputTokens,
474
497
  ...(state.cacheReadTokens ? { cacheReadInputTokens: state.cacheReadTokens } : {}),
498
+ ...(state.searched ? { webSearchRequests: 1 } : {}),
475
499
  },
476
500
  };
477
501
  }
@@ -515,6 +539,8 @@ class ZhipuAIProvider {
515
539
  yield { type: 'error', message: clientMessage, errorKey: 'ai.error.apiError' };
516
540
  continue;
517
541
  }
542
+ if (carriesSearchResults(event))
543
+ state.searched = true;
518
544
  // Usage info (may appear in the final chunk)
519
545
  const usage = event.usage;
520
546
  if (usage) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@molecule/api-ai-zhipu",
3
- "version": "1.1.1",
3
+ "version": "1.2.0",
4
4
  "description": "Zhipu GLM AI provider for molecule.dev",
5
5
  "homepage": "https://www.molecule.dev/packages/api-ai-zhipu",
6
6
  "type": "module",
@@ -34,7 +34,7 @@
34
34
  "directory": "packages/api/bonds/ai/zhipu"
35
35
  },
36
36
  "devDependencies": {
37
- "@molecule/api-ai": "1.2.1",
37
+ "@molecule/api-ai": "1.3.0",
38
38
  "@molecule/api-bond": "1.0.2",
39
39
  "@types/node": "26.1.2",
40
40
  "typescript": "6.0.3",