@molecule/api-ai-zhipu 1.1.1 → 1.2.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.
@@ -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;AAkpB7C;;;;;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
@@ -286,6 +303,11 @@ class ZhipuAIProvider {
286
303
  for (const tr of toolResults) {
287
304
  formatted.push(tr);
288
305
  }
306
+ // Non-tool parts of the SAME message (an image riding a tool result — the
307
+ // screenshot tool) must not be dropped: Chat Completions accepts a user
308
+ // message after the tool messages, so emit them instead of losing them.
309
+ if (parts.length > 0)
310
+ formatted.push({ role: m.role, content: parts });
289
311
  }
290
312
  else if (toolCalls.length > 0) {
291
313
  // Assistant message with tool calls
@@ -386,6 +408,7 @@ class ZhipuAIProvider {
386
408
  inputTokens: Math.max(0, (usage?.prompt_tokens ?? 0) - cachedTokens),
387
409
  outputTokens: usage?.completion_tokens ?? 0,
388
410
  ...(cachedTokens ? { cacheReadInputTokens: cachedTokens } : {}),
411
+ ...(carriesSearchResults(data) ? { webSearchRequests: 1 } : {}),
389
412
  },
390
413
  };
391
414
  }
@@ -408,7 +431,12 @@ class ZhipuAIProvider {
408
431
  }
409
432
  const decoder = new TextDecoder();
410
433
  let buffer = '';
411
- const state = { inputTokens: 0, outputTokens: 0, cacheReadTokens: 0 };
434
+ const state = {
435
+ inputTokens: 0,
436
+ outputTokens: 0,
437
+ cacheReadTokens: 0,
438
+ searched: false,
439
+ };
412
440
  // Track pending tool calls (accumulated across deltas)
413
441
  const pendingTools = new Map();
414
442
  try {
@@ -472,6 +500,7 @@ class ZhipuAIProvider {
472
500
  inputTokens: Math.max(0, state.inputTokens - state.cacheReadTokens),
473
501
  outputTokens: state.outputTokens,
474
502
  ...(state.cacheReadTokens ? { cacheReadInputTokens: state.cacheReadTokens } : {}),
503
+ ...(state.searched ? { webSearchRequests: 1 } : {}),
475
504
  },
476
505
  };
477
506
  }
@@ -515,6 +544,8 @@ class ZhipuAIProvider {
515
544
  yield { type: 'error', message: clientMessage, errorKey: 'ai.error.apiError' };
516
545
  continue;
517
546
  }
547
+ if (carriesSearchResults(event))
548
+ state.searched = true;
518
549
  // Usage info (may appear in the final chunk)
519
550
  const usage = event.usage;
520
551
  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.1",
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",