@molecule/api-ai-zhipu 1.0.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/LICENSE +115 -0
- package/dist/browser-guard.d.ts +2 -0
- package/dist/browser-guard.d.ts.map +1 -0
- package/dist/browser-guard.js +18 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +39 -0
- package/dist/provider.d.ts +18 -0
- package/dist/provider.d.ts.map +1 -0
- package/dist/provider.js +556 -0
- package/dist/secrets.d.ts +16 -0
- package/dist/secrets.d.ts.map +1 -0
- package/dist/secrets.js +23 -0
- package/dist/types.d.ts +45 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +6 -0
- package/package.json +47 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work.
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
+
represent, as a whole, an original work of authorship.
|
|
43
|
+
|
|
44
|
+
"Contribution" shall mean any work of authorship, including the
|
|
45
|
+
original version of the Work and any modifications or additions
|
|
46
|
+
to that Work, that is intentionally submitted to the Licensor for
|
|
47
|
+
inclusion in the Work by the copyright owner or by an individual or
|
|
48
|
+
Legal Entity authorized to submit on behalf of the copyright owner.
|
|
49
|
+
|
|
50
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
51
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
52
|
+
subsequently incorporated within the Work.
|
|
53
|
+
|
|
54
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
55
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
56
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
57
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
58
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
59
|
+
Work and such Derivative Works in Source or Object form.
|
|
60
|
+
|
|
61
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
62
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
63
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
64
|
+
patent license to make, have made, use, offer to sell, sell, import,
|
|
65
|
+
and otherwise transfer the Work.
|
|
66
|
+
|
|
67
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
68
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
69
|
+
modifications, and in Source or Object form, provided that You
|
|
70
|
+
meet the following conditions:
|
|
71
|
+
|
|
72
|
+
(a) You must give any other recipients of the Work or
|
|
73
|
+
Derivative Works a copy of this License; and
|
|
74
|
+
|
|
75
|
+
(b) You must cause any modified files to carry prominent notices
|
|
76
|
+
stating that You changed the files; and
|
|
77
|
+
|
|
78
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
79
|
+
that You distribute, all copyright, patent, trademark, and
|
|
80
|
+
attribution notices from the Source form of the Work,
|
|
81
|
+
excluding those notices that do not pertain to any part of
|
|
82
|
+
the Derivative Works; and
|
|
83
|
+
|
|
84
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
85
|
+
distribution, then any Derivative Works that You distribute must
|
|
86
|
+
include a readable copy of the attribution notices contained
|
|
87
|
+
within such NOTICE file.
|
|
88
|
+
|
|
89
|
+
5. Submission of Contributions.
|
|
90
|
+
|
|
91
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
92
|
+
names, trademarks, service marks, or product names of the Licensor.
|
|
93
|
+
|
|
94
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
95
|
+
agreed to in writing, Licensor provides the Work on an "AS IS" BASIS,
|
|
96
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND.
|
|
97
|
+
|
|
98
|
+
8. Limitation of Liability. In no event and under no legal theory shall
|
|
99
|
+
any Contributor be liable to You for damages.
|
|
100
|
+
|
|
101
|
+
9. Accepting Warranty or Additional Liability.
|
|
102
|
+
|
|
103
|
+
Copyright 2026 Molecule Dev, Inc.
|
|
104
|
+
|
|
105
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
106
|
+
you may not use this file except in compliance with the License.
|
|
107
|
+
You may obtain a copy of the License at
|
|
108
|
+
|
|
109
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
110
|
+
|
|
111
|
+
Unless required by applicable law or agreed to in writing, software
|
|
112
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
113
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
114
|
+
See the License for the specific language governing permissions and
|
|
115
|
+
limitations under the License.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser-guard.d.ts","sourceRoot":"","sources":["../src/browser-guard.ts"],"names":[],"mappings":"AAuBA,OAAO,EAAE,CAAA"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser guard — `@molecule/api-ai-zhipu` is SERVER-ONLY.
|
|
3
|
+
*
|
|
4
|
+
* Generated by scripts/gen-browser-guards.mjs (workspace root) — edit THAT, not this.
|
|
5
|
+
* Evaluating a server package in a browser bundle is always an import-graph mistake
|
|
6
|
+
* (node APIs, secrets); without this guard it surfaces as a cryptic downstream crash
|
|
7
|
+
* ("Buffer is not defined") far from the culprit. Throwing here names the package and
|
|
8
|
+
* the fix at the exact moment the client bundle evaluates it. jsdom tests and SSR are
|
|
9
|
+
* unaffected: the throw requires browser globals AND the absence of a node runtime.
|
|
10
|
+
*/
|
|
11
|
+
const g = globalThis;
|
|
12
|
+
if (g.window !== undefined && g.document !== undefined && !g.process?.versions?.node) {
|
|
13
|
+
throw new Error('@molecule/api-ai-zhipu is SERVER-ONLY: it was bundled into browser/client code. Import it only ' +
|
|
14
|
+
'from server code (a server route/function or your API), or dynamic-import it inside ' +
|
|
15
|
+
'the server handler — never from components or shared client modules, and never ' +
|
|
16
|
+
'polyfill Buffer/process to silence this.');
|
|
17
|
+
}
|
|
18
|
+
export {};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zhipu GLM AI provider for molecule.dev.
|
|
3
|
+
*
|
|
4
|
+
* @remarks
|
|
5
|
+
* Config: `ZHIPU_API_KEY` (SERVER-side only) plus an optional default model id/base URL.
|
|
6
|
+
*
|
|
7
|
+
* **Missing `ZHIPU_API_KEY` fails fast**: the provider throws naming the exact env var on first
|
|
8
|
+
* use (the exported `provider` is a lazy proxy, so this fires on the first `chat()` call, not at
|
|
9
|
+
* bond/module-load time) — it never silently sends an empty key.
|
|
10
|
+
*
|
|
11
|
+
* **Error message disambiguation**: a plain 400 that ISN'T a context-length error (bad param,
|
|
12
|
+
* malformed tool schema) gets its own non-retryable message distinct from the generic
|
|
13
|
+
* "AI service error. Please try again." used for retryable failures.
|
|
14
|
+
*
|
|
15
|
+
* @module
|
|
16
|
+
*/
|
|
17
|
+
export * from './browser-guard.js';
|
|
18
|
+
export * from './provider.js';
|
|
19
|
+
export * from './secrets.js';
|
|
20
|
+
export * from './types.js';
|
|
21
|
+
import type { AIProvider } from '@molecule/api-ai';
|
|
22
|
+
/**
|
|
23
|
+
* The provider implementation.
|
|
24
|
+
*/
|
|
25
|
+
export declare const provider: AIProvider;
|
|
26
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,cAAc,oBAAoB,CAAA;AAClC,cAAc,eAAe,CAAA;AAC7B,cAAc,cAAc,CAAA;AAC5B,cAAc,YAAY,CAAA;AAE1B,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAMlD;;GAEG;AACH,eAAO,MAAM,QAAQ,EAAE,UAUrB,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zhipu GLM AI provider for molecule.dev.
|
|
3
|
+
*
|
|
4
|
+
* @remarks
|
|
5
|
+
* Config: `ZHIPU_API_KEY` (SERVER-side only) plus an optional default model id/base URL.
|
|
6
|
+
*
|
|
7
|
+
* **Missing `ZHIPU_API_KEY` fails fast**: the provider throws naming the exact env var on first
|
|
8
|
+
* use (the exported `provider` is a lazy proxy, so this fires on the first `chat()` call, not at
|
|
9
|
+
* bond/module-load time) — it never silently sends an empty key.
|
|
10
|
+
*
|
|
11
|
+
* **Error message disambiguation**: a plain 400 that ISN'T a context-length error (bad param,
|
|
12
|
+
* malformed tool schema) gets its own non-retryable message distinct from the generic
|
|
13
|
+
* "AI service error. Please try again." used for retryable failures.
|
|
14
|
+
*
|
|
15
|
+
* @module
|
|
16
|
+
*/
|
|
17
|
+
export * from './browser-guard.js';
|
|
18
|
+
export * from './provider.js';
|
|
19
|
+
export * from './secrets.js';
|
|
20
|
+
export * from './types.js';
|
|
21
|
+
import { createProvider } from './provider.js';
|
|
22
|
+
/** Lazily-initialized provider singleton. Defers creation until first use so that env vars / secrets are resolved. */
|
|
23
|
+
let _provider = null;
|
|
24
|
+
/**
|
|
25
|
+
* The provider implementation.
|
|
26
|
+
*/
|
|
27
|
+
export const provider = new Proxy({}, {
|
|
28
|
+
get(_, prop, receiver) {
|
|
29
|
+
if (!_provider)
|
|
30
|
+
_provider = createProvider();
|
|
31
|
+
return Reflect.get(_provider, prop, receiver);
|
|
32
|
+
},
|
|
33
|
+
// set trap: methods run with `this` bound to the proxy — without it, instance-state writes land on the dummy target and are lost (see api-push-notifications-web-push)
|
|
34
|
+
set(_, prop, value) {
|
|
35
|
+
if (!_provider)
|
|
36
|
+
_provider = createProvider();
|
|
37
|
+
return Reflect.set(_provider, prop, value);
|
|
38
|
+
},
|
|
39
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zhipu GLM implementation of AIProvider.
|
|
3
|
+
*
|
|
4
|
+
* Uses the Zhipu API (OpenAI-compatible) for streaming chat completions with tool use.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
import type { AIProvider } from '@molecule/api-ai';
|
|
9
|
+
import './secrets.js';
|
|
10
|
+
import type { ZhipuConfig } from './types.js';
|
|
11
|
+
/**
|
|
12
|
+
* Creates a Zhipu GLM AI provider instance.
|
|
13
|
+
*
|
|
14
|
+
* @param config - Zhipu-specific configuration (API key, model, max tokens, base URL).
|
|
15
|
+
* @returns An `AIProvider` backed by the Zhipu Chat Completions API.
|
|
16
|
+
*/
|
|
17
|
+
export declare function createProvider(config?: ZhipuConfig): AIProvider;
|
|
18
|
+
//# sourceMappingURL=provider.d.ts.map
|
|
@@ -0,0 +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;AAilB7C;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,MAAM,CAAC,EAAE,WAAW,GAAG,UAAU,CAE/D"}
|
package/dist/provider.js
ADDED
|
@@ -0,0 +1,556 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zhipu GLM implementation of AIProvider.
|
|
3
|
+
*
|
|
4
|
+
* Uses the Zhipu API (OpenAI-compatible) for streaming chat completions with tool use.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
import { getLogger } from '@molecule/api-bond';
|
|
9
|
+
import { t } from '@molecule/api-i18n';
|
|
10
|
+
const logger = getLogger();
|
|
11
|
+
// Side-effect import: registers this bond's secret definitions so the
|
|
12
|
+
// runtime registry is populated even when provider.js is imported directly
|
|
13
|
+
// (not through the package barrel).
|
|
14
|
+
import './secrets.js';
|
|
15
|
+
/**
|
|
16
|
+
* Fallback: map thinking budget tokens to a Zhipu reasoning_effort level when
|
|
17
|
+
* the caller didn't resolve a native value from the model catalog. Note
|
|
18
|
+
* `reasoning_effort` is only honored on glm-5.2+ (values minimal | none | low |
|
|
19
|
+
* medium | high | xhigh | max; low/medium coerce to high, xhigh to max) —
|
|
20
|
+
* callers should prefer passing `thinking.effort`; older GLM models only
|
|
21
|
+
* support thinking on/off and should not receive a thinking param at all.
|
|
22
|
+
*
|
|
23
|
+
* @param budgetTokens - Requested thinking budget in tokens.
|
|
24
|
+
* @returns Either `'high'` or `'low'` for the upstream effort hint.
|
|
25
|
+
*/
|
|
26
|
+
function budgetToEffort(budgetTokens) {
|
|
27
|
+
return budgetTokens >= 8_000 ? 'high' : 'low';
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Zhipu GLM AI provider that implements the `AIProvider` interface
|
|
31
|
+
* using Zhipu's OpenAI-compatible Chat Completions API with support
|
|
32
|
+
* for streaming, tool use, and reasoning.
|
|
33
|
+
*/
|
|
34
|
+
class ZhipuAIProvider {
|
|
35
|
+
name = 'zhipu';
|
|
36
|
+
apiKey;
|
|
37
|
+
defaultModel;
|
|
38
|
+
maxTokens;
|
|
39
|
+
baseUrl;
|
|
40
|
+
completionsPath;
|
|
41
|
+
modelMap;
|
|
42
|
+
onRateLimit;
|
|
43
|
+
constructor(config = {}) {
|
|
44
|
+
this.apiKey = config.apiKey ?? process.env.ZHIPU_API_KEY ?? '';
|
|
45
|
+
this.defaultModel = config.defaultModel ?? 'glm-5.2';
|
|
46
|
+
this.maxTokens = config.maxTokens ?? 4096;
|
|
47
|
+
this.baseUrl = config.baseUrl ?? process.env.ZHIPU_BASE_URL ?? 'https://api.z.ai/api/paas';
|
|
48
|
+
this.completionsPath =
|
|
49
|
+
config.completionsPath ?? process.env.ZHIPU_COMPLETIONS_PATH ?? '/v4/chat/completions';
|
|
50
|
+
this.modelMap = config.modelMap ?? {};
|
|
51
|
+
this.onRateLimit = config.onRateLimit;
|
|
52
|
+
// Fail fast with an actionable local error rather than a cryptic 401 on the
|
|
53
|
+
// first request. The default `provider` export constructs lazily on first
|
|
54
|
+
// use (see index.ts), so this surfaces the moment the provider is actually
|
|
55
|
+
// used, not at bond/module-load time.
|
|
56
|
+
if (!this.apiKey) {
|
|
57
|
+
throw new Error('ZHIPU_API_KEY is not set. Add it to your environment to use the Zhipu GLM AI provider.');
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Sends a chat request and yields streamed events (text chunks, tool use, done/error).
|
|
62
|
+
*
|
|
63
|
+
* @param params - Chat parameters including messages, model, system prompt, and tools.
|
|
64
|
+
* @yields {ChatEvent} Chat events including text chunks, tool use requests, done signals, and errors.
|
|
65
|
+
*/
|
|
66
|
+
async *chat(params) {
|
|
67
|
+
const canonicalModel = params.model ?? this.defaultModel;
|
|
68
|
+
const model = this.modelMap[canonicalModel] ?? canonicalModel;
|
|
69
|
+
const maxTokens = params.maxTokens ?? this.maxTokens;
|
|
70
|
+
const messages = this.formatMessages(params.messages, params.system);
|
|
71
|
+
const body = {
|
|
72
|
+
// Abuse attribution (see ChatParams.endUserId): OpenAI-compatible APIs
|
|
73
|
+
// scope enforcement by `user`, so an abusive tenant can be actioned
|
|
74
|
+
// individually instead of the org key that serves everyone. The value is
|
|
75
|
+
// the caller's opaque hash — never derived or logged here.
|
|
76
|
+
...(params.endUserId ? { user: params.endUserId } : {}),
|
|
77
|
+
model,
|
|
78
|
+
max_completion_tokens: maxTokens,
|
|
79
|
+
messages,
|
|
80
|
+
stream: params.stream !== false,
|
|
81
|
+
...(params.stream !== false ? { stream_options: { include_usage: true } } : {}),
|
|
82
|
+
};
|
|
83
|
+
// Tools: merge custom tools (function format) and server tools
|
|
84
|
+
const functions = params.tools?.length ? this.formatTools(params.tools) : [];
|
|
85
|
+
const serverTools = (params.serverTools ?? []).map((st) => ({ ...st }));
|
|
86
|
+
const allTools = [...functions, ...serverTools];
|
|
87
|
+
if (allTools.length > 0) {
|
|
88
|
+
body.tools = allTools;
|
|
89
|
+
}
|
|
90
|
+
if (params.thinking) {
|
|
91
|
+
// Prefer the caller-resolved native value (minimal | high | max on
|
|
92
|
+
// glm-5.2, from the model catalog); fall back to the budget threshold.
|
|
93
|
+
body.reasoning_effort = params.thinking.effort ?? budgetToEffort(params.thinking.budgetTokens);
|
|
94
|
+
}
|
|
95
|
+
else if (params.temperature !== undefined) {
|
|
96
|
+
body.temperature = params.temperature;
|
|
97
|
+
}
|
|
98
|
+
const headers = {
|
|
99
|
+
'Content-Type': 'application/json',
|
|
100
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
101
|
+
};
|
|
102
|
+
const DEFAULT_TIMEOUT_MS = 5 * 60 * 1000;
|
|
103
|
+
const signal = params.signal ?? AbortSignal.timeout(DEFAULT_TIMEOUT_MS);
|
|
104
|
+
// Retry with exponential backoff for rate limits (429) and overloaded (503)
|
|
105
|
+
const MAX_RETRIES = 3;
|
|
106
|
+
let response = null;
|
|
107
|
+
for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
|
|
108
|
+
response = await fetch(`${this.baseUrl}${this.completionsPath}`, {
|
|
109
|
+
method: 'POST',
|
|
110
|
+
headers,
|
|
111
|
+
body: JSON.stringify(body),
|
|
112
|
+
signal,
|
|
113
|
+
});
|
|
114
|
+
if (response.status === 429 || response.status === 503) {
|
|
115
|
+
const willRetry = attempt < MAX_RETRIES;
|
|
116
|
+
// Retry-After is equally-valid as delta-seconds or an HTTP-date;
|
|
117
|
+
// parseInt on the date form yields NaN, degrading the backoff to a
|
|
118
|
+
// ~0ms retry against an already rate-limiting API. Guard it.
|
|
119
|
+
const retryAfter = response.headers.get('retry-after');
|
|
120
|
+
const parsedRetryAfterSeconds = retryAfter ? parseInt(retryAfter, 10) : NaN;
|
|
121
|
+
const hasRetryAfter = Number.isFinite(parsedRetryAfterSeconds) && parsedRetryAfterSeconds >= 0;
|
|
122
|
+
// Additive jitter (<500ms): concurrent callers rejected together must
|
|
123
|
+
// not sleep identical delays and re-arrive as the same burst.
|
|
124
|
+
const delayMs = willRetry
|
|
125
|
+
? (hasRetryAfter
|
|
126
|
+
? Math.min(parsedRetryAfterSeconds * 1000, 60_000)
|
|
127
|
+
: Math.min(1000 * 2 ** attempt, 30_000)) + Math.floor(Math.random() * 500)
|
|
128
|
+
: 0;
|
|
129
|
+
try {
|
|
130
|
+
this.onRateLimit?.({
|
|
131
|
+
provider: this.name,
|
|
132
|
+
model,
|
|
133
|
+
status: response.status,
|
|
134
|
+
attempt: attempt + 1,
|
|
135
|
+
willRetry,
|
|
136
|
+
retryInMs: delayMs,
|
|
137
|
+
...(hasRetryAfter ? { retryAfterSeconds: parsedRetryAfterSeconds } : {}),
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
catch (error) {
|
|
141
|
+
// Host-app telemetry; its failure must never break the request.
|
|
142
|
+
logger.warn('onRateLimit callback threw', { error });
|
|
143
|
+
}
|
|
144
|
+
if (willRetry) {
|
|
145
|
+
logger.warn('Zhipu API rate limited, retrying', {
|
|
146
|
+
status: response.status,
|
|
147
|
+
attempt: attempt + 1,
|
|
148
|
+
delayMs,
|
|
149
|
+
});
|
|
150
|
+
await new Promise((resolve) => {
|
|
151
|
+
const timer = setTimeout(resolve, delayMs);
|
|
152
|
+
signal.addEventListener('abort', () => {
|
|
153
|
+
clearTimeout(timer);
|
|
154
|
+
resolve();
|
|
155
|
+
}, { once: true });
|
|
156
|
+
});
|
|
157
|
+
if (signal.aborted)
|
|
158
|
+
break;
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
if (!response.ok) {
|
|
165
|
+
const errorBody = await response.text();
|
|
166
|
+
let detail = `HTTP ${response.status}`;
|
|
167
|
+
try {
|
|
168
|
+
const parsed = JSON.parse(errorBody);
|
|
169
|
+
if (parsed.error?.message)
|
|
170
|
+
detail = parsed.error.message;
|
|
171
|
+
}
|
|
172
|
+
catch (_error) {
|
|
173
|
+
// JSON.parse failed — fall back to the raw errorBody string; ignoring parse error is safe here
|
|
174
|
+
if (errorBody.length > 0 && errorBody.length < 200)
|
|
175
|
+
detail = errorBody;
|
|
176
|
+
}
|
|
177
|
+
logger.error('Zhipu API error', { status: response.status, detail, errorBody });
|
|
178
|
+
const clientMessage = response.status === 429
|
|
179
|
+
? 'AI rate limit exceeded. Please try again shortly.'
|
|
180
|
+
: response.status === 401
|
|
181
|
+
? 'AI service configuration error.'
|
|
182
|
+
: response.status === 400 &&
|
|
183
|
+
/prompt is too long|too many tokens|token.*limit|context.*length/i.test(detail)
|
|
184
|
+
? "Conversation too long for the model's context window. Use /compact to free space, or start a new conversation."
|
|
185
|
+
: response.status === 400
|
|
186
|
+
? 'AI request was invalid — check the model and request parameters.'
|
|
187
|
+
: response.status === 503
|
|
188
|
+
? 'AI service is temporarily overloaded. Please try again in a moment.'
|
|
189
|
+
: 'AI service error. Please try again.';
|
|
190
|
+
yield { type: 'error', message: clientMessage, errorKey: 'ai.error.apiError' };
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
if (params.stream === false) {
|
|
194
|
+
const data = (await response.json());
|
|
195
|
+
yield* this.parseNonStreamingResponse(data);
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
yield* this.parseStreamingResponse(response);
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Converts internal `ChatMessage` objects to the OpenAI-compatible message format.
|
|
202
|
+
* System prompt is prepended as a system role message.
|
|
203
|
+
*
|
|
204
|
+
* @param messages - The chat messages to format.
|
|
205
|
+
* @param system - Optional system prompt to prepend.
|
|
206
|
+
* @returns The formatted messages array for the Zhipu API.
|
|
207
|
+
*/
|
|
208
|
+
formatMessages(messages, system) {
|
|
209
|
+
const formatted = [];
|
|
210
|
+
if (system) {
|
|
211
|
+
formatted.push({ role: 'system', content: system });
|
|
212
|
+
}
|
|
213
|
+
for (const m of messages) {
|
|
214
|
+
if (m.role === 'system')
|
|
215
|
+
continue; // handled above
|
|
216
|
+
if (typeof m.content === 'string') {
|
|
217
|
+
formatted.push({ role: m.role, content: m.content });
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
// Content blocks — convert to OpenAI format
|
|
221
|
+
const blocks = m.content;
|
|
222
|
+
const parts = [];
|
|
223
|
+
const toolCalls = [];
|
|
224
|
+
const toolResults = [];
|
|
225
|
+
for (const block of blocks) {
|
|
226
|
+
switch (block.type) {
|
|
227
|
+
case 'text':
|
|
228
|
+
parts.push({ type: 'text', text: block.text });
|
|
229
|
+
break;
|
|
230
|
+
case 'image':
|
|
231
|
+
parts.push({
|
|
232
|
+
type: 'image_url',
|
|
233
|
+
image_url: { url: `data:${block.mediaType};base64,${block.data}` },
|
|
234
|
+
});
|
|
235
|
+
break;
|
|
236
|
+
case 'tool_use':
|
|
237
|
+
toolCalls.push({
|
|
238
|
+
id: block.id,
|
|
239
|
+
type: 'function',
|
|
240
|
+
function: { name: block.name, arguments: JSON.stringify(block.input) },
|
|
241
|
+
});
|
|
242
|
+
break;
|
|
243
|
+
case 'tool_result':
|
|
244
|
+
toolResults.push({
|
|
245
|
+
role: 'tool',
|
|
246
|
+
tool_call_id: block.tool_use_id,
|
|
247
|
+
content: typeof block.content === 'string' ? block.content : JSON.stringify(block.content),
|
|
248
|
+
});
|
|
249
|
+
break;
|
|
250
|
+
default:
|
|
251
|
+
// document, audio, video — unsupported, skip
|
|
252
|
+
break;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
if (toolResults.length > 0) {
|
|
256
|
+
// Tool results are separate messages in OpenAI format
|
|
257
|
+
for (const tr of toolResults) {
|
|
258
|
+
formatted.push(tr);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
else if (toolCalls.length > 0) {
|
|
262
|
+
// Assistant message with tool calls
|
|
263
|
+
formatted.push({
|
|
264
|
+
role: 'assistant',
|
|
265
|
+
content: parts.length > 0 ? parts : null,
|
|
266
|
+
tool_calls: toolCalls,
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
else {
|
|
270
|
+
formatted.push({
|
|
271
|
+
role: m.role,
|
|
272
|
+
content: parts.length === 1 && parts[0].type === 'text' ? parts[0].text : parts,
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
return formatted;
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Converts internal `AITool` definitions to the OpenAI function calling format.
|
|
280
|
+
*
|
|
281
|
+
* @param tools - The AI tool definitions to format.
|
|
282
|
+
* @returns The formatted tools array for the Zhipu API.
|
|
283
|
+
*/
|
|
284
|
+
formatTools(tools) {
|
|
285
|
+
return tools.map((tool) => ({
|
|
286
|
+
type: 'function',
|
|
287
|
+
function: {
|
|
288
|
+
name: tool.name,
|
|
289
|
+
description: tool.description,
|
|
290
|
+
parameters: tool.parameters,
|
|
291
|
+
},
|
|
292
|
+
}));
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Parses a non-streaming OpenAI-compatible response into `ChatEvent` objects.
|
|
296
|
+
*
|
|
297
|
+
* @param data - The raw API response data to parse.
|
|
298
|
+
* @yields {ChatEvent} Chat events parsed from the response.
|
|
299
|
+
*/
|
|
300
|
+
*parseNonStreamingResponse(data) {
|
|
301
|
+
const choices = data.choices;
|
|
302
|
+
if (choices?.length) {
|
|
303
|
+
const message = choices[0].message;
|
|
304
|
+
if (message) {
|
|
305
|
+
// Reasoning/thinking content
|
|
306
|
+
if (message.reasoning_content && typeof message.reasoning_content === 'string') {
|
|
307
|
+
yield { type: 'thinking', content: message.reasoning_content };
|
|
308
|
+
}
|
|
309
|
+
if (message.content && typeof message.content === 'string') {
|
|
310
|
+
yield { type: 'text', content: message.content };
|
|
311
|
+
}
|
|
312
|
+
const toolCalls = message.tool_calls;
|
|
313
|
+
if (toolCalls) {
|
|
314
|
+
for (const tc of toolCalls) {
|
|
315
|
+
const fn = tc.function;
|
|
316
|
+
let input = {};
|
|
317
|
+
try {
|
|
318
|
+
input = JSON.parse(fn.arguments);
|
|
319
|
+
}
|
|
320
|
+
catch (_error) {
|
|
321
|
+
// JSON.parse failed — fall back to empty object; invalid arguments are non-fatal
|
|
322
|
+
}
|
|
323
|
+
yield {
|
|
324
|
+
type: 'tool_use',
|
|
325
|
+
id: tc.id,
|
|
326
|
+
name: fn.name,
|
|
327
|
+
input,
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
const usage = data.usage;
|
|
334
|
+
// prompt_tokens INCLUDES cached tokens on OpenAI-compatible APIs — report
|
|
335
|
+
// the cached portion separately (cacheReadPricePerMTok) and only the fresh
|
|
336
|
+
// remainder as inputTokens, matching the other bonds' semantics.
|
|
337
|
+
const cachedTokens = usage?.prompt_tokens_details?.cached_tokens ?? 0;
|
|
338
|
+
yield {
|
|
339
|
+
type: 'done',
|
|
340
|
+
usage: {
|
|
341
|
+
inputTokens: Math.max(0, (usage?.prompt_tokens ?? 0) - cachedTokens),
|
|
342
|
+
outputTokens: usage?.completion_tokens ?? 0,
|
|
343
|
+
...(cachedTokens ? { cacheReadInputTokens: cachedTokens } : {}),
|
|
344
|
+
},
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Parses a streaming SSE response from the Zhipu API (OpenAI-compatible format),
|
|
349
|
+
* yielding `ChatEvent` objects as they arrive.
|
|
350
|
+
*
|
|
351
|
+
* @param response - The fetch Response with an SSE body stream.
|
|
352
|
+
* @yields {ChatEvent} Chat events as they arrive from the SSE stream.
|
|
353
|
+
*/
|
|
354
|
+
async *parseStreamingResponse(response) {
|
|
355
|
+
const reader = response.body?.getReader();
|
|
356
|
+
if (!reader) {
|
|
357
|
+
yield {
|
|
358
|
+
type: 'error',
|
|
359
|
+
message: t('ai.error.noResponseBody'),
|
|
360
|
+
errorKey: 'ai.error.noResponseBody',
|
|
361
|
+
};
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
const decoder = new TextDecoder();
|
|
365
|
+
let buffer = '';
|
|
366
|
+
const state = { inputTokens: 0, outputTokens: 0, cacheReadTokens: 0 };
|
|
367
|
+
// Track pending tool calls (accumulated across deltas)
|
|
368
|
+
const pendingTools = new Map();
|
|
369
|
+
try {
|
|
370
|
+
while (true) {
|
|
371
|
+
const { done, value } = await reader.read();
|
|
372
|
+
if (done)
|
|
373
|
+
break;
|
|
374
|
+
buffer += decoder.decode(value, { stream: true });
|
|
375
|
+
const lines = buffer.split('\n');
|
|
376
|
+
buffer = lines.pop() ?? '';
|
|
377
|
+
let yieldedContent = false;
|
|
378
|
+
for (const event of this.processSSELines(lines, pendingTools, state)) {
|
|
379
|
+
yieldedContent = true;
|
|
380
|
+
yield event;
|
|
381
|
+
// A mid-stream error (rate-limit/overload during high load, sent as a
|
|
382
|
+
// `data: {"error": {...}}` chunk) means the turn was truncated. Stop
|
|
383
|
+
// here and do NOT fall through to the trailing `done`, which would
|
|
384
|
+
// report the truncated turn as a successful completion and skip the
|
|
385
|
+
// consumer's overload/retry recovery.
|
|
386
|
+
if (event.type === 'error')
|
|
387
|
+
return;
|
|
388
|
+
}
|
|
389
|
+
// Signal liveness when the API sends data but no content events were produced
|
|
390
|
+
// (e.g., during extended reasoning with empty chunks or keepalive comments).
|
|
391
|
+
// This resets the consumer's stream timeout without forwarding to the client.
|
|
392
|
+
if (!yieldedContent) {
|
|
393
|
+
yield { type: 'keep_alive' };
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
// Flush any remaining data in the buffer after EOF
|
|
397
|
+
if (buffer.trim()) {
|
|
398
|
+
for (const event of this.processSSELines(buffer.split('\n'), pendingTools, state)) {
|
|
399
|
+
yield event;
|
|
400
|
+
if (event.type === 'error')
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
// Emit any tool calls still pending after stream ends (safety net for missing finish_reason)
|
|
405
|
+
if (pendingTools.size > 0) {
|
|
406
|
+
for (const [, pending] of pendingTools) {
|
|
407
|
+
let input = {};
|
|
408
|
+
try {
|
|
409
|
+
input = JSON.parse(pending.args);
|
|
410
|
+
}
|
|
411
|
+
catch (_error) {
|
|
412
|
+
// JSON.parse failed — fall back to empty object; unparseable args are non-fatal
|
|
413
|
+
}
|
|
414
|
+
yield { type: 'tool_use', id: pending.id, name: pending.name, input };
|
|
415
|
+
}
|
|
416
|
+
pendingTools.clear();
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
finally {
|
|
420
|
+
reader.releaseLock();
|
|
421
|
+
}
|
|
422
|
+
// See the non-streaming path: cached tokens are reported separately and
|
|
423
|
+
// subtracted from inputTokens (prompt_tokens includes them).
|
|
424
|
+
yield {
|
|
425
|
+
type: 'done',
|
|
426
|
+
usage: {
|
|
427
|
+
inputTokens: Math.max(0, state.inputTokens - state.cacheReadTokens),
|
|
428
|
+
outputTokens: state.outputTokens,
|
|
429
|
+
...(state.cacheReadTokens ? { cacheReadInputTokens: state.cacheReadTokens } : {}),
|
|
430
|
+
},
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* Processes an array of SSE lines from the Zhipu streaming response, yielding
|
|
435
|
+
* ChatEvent objects for text, thinking, tool use, and tool completion.
|
|
436
|
+
*
|
|
437
|
+
* @param lines - Raw SSE lines to process.
|
|
438
|
+
* @param pendingTools - Mutable map tracking tool call deltas accumulated across chunks.
|
|
439
|
+
* @param state - Mutable token usage counters.
|
|
440
|
+
* @yields {ChatEvent} Chat events extracted from the SSE lines.
|
|
441
|
+
*/
|
|
442
|
+
*processSSELines(lines, pendingTools, state) {
|
|
443
|
+
for (const line of lines) {
|
|
444
|
+
if (!line.startsWith('data: '))
|
|
445
|
+
continue;
|
|
446
|
+
const json = line.slice(6).trim();
|
|
447
|
+
if (json === '[DONE]')
|
|
448
|
+
continue;
|
|
449
|
+
try {
|
|
450
|
+
const event = JSON.parse(json);
|
|
451
|
+
// A mid-stream error arrives as a `data: {"error": {...}}` chunk (no
|
|
452
|
+
// `choices`) — most importantly rate-limit/overload during high load.
|
|
453
|
+
// Without this it matches no branch below and is silently dropped, so the
|
|
454
|
+
// stream falls through to a misleading `done` and the truncated turn reads
|
|
455
|
+
// as a successful completion. Surface it as a real error event (also
|
|
456
|
+
// satisfies the no-silent-swallow rule).
|
|
457
|
+
const streamError = event.error;
|
|
458
|
+
if (streamError) {
|
|
459
|
+
const detail = `${streamError.code ?? streamError.type ?? ''} ${streamError.message ?? ''}`;
|
|
460
|
+
logger.error('Zhipu streaming error event', {
|
|
461
|
+
type: streamError.type,
|
|
462
|
+
code: streamError.code,
|
|
463
|
+
message: streamError.message,
|
|
464
|
+
});
|
|
465
|
+
const clientMessage = /overload|capacity|503|529/i.test(detail)
|
|
466
|
+
? 'AI service is temporarily overloaded. Please try again in a moment.'
|
|
467
|
+
: /rate.?limit|429|quota/i.test(detail)
|
|
468
|
+
? 'AI rate limit exceeded. Please try again shortly.'
|
|
469
|
+
: 'AI service error. Please try again.';
|
|
470
|
+
yield { type: 'error', message: clientMessage, errorKey: 'ai.error.apiError' };
|
|
471
|
+
continue;
|
|
472
|
+
}
|
|
473
|
+
// Usage info (may appear in the final chunk)
|
|
474
|
+
const usage = event.usage;
|
|
475
|
+
if (usage) {
|
|
476
|
+
if (usage.prompt_tokens)
|
|
477
|
+
state.inputTokens = usage.prompt_tokens;
|
|
478
|
+
if (usage.completion_tokens)
|
|
479
|
+
state.outputTokens = usage.completion_tokens;
|
|
480
|
+
if (usage.prompt_tokens_details?.cached_tokens)
|
|
481
|
+
state.cacheReadTokens = usage.prompt_tokens_details.cached_tokens;
|
|
482
|
+
}
|
|
483
|
+
const choices = event.choices;
|
|
484
|
+
if (!choices?.length)
|
|
485
|
+
continue;
|
|
486
|
+
const choice = choices[0];
|
|
487
|
+
const delta = choice.delta;
|
|
488
|
+
if (!delta)
|
|
489
|
+
continue;
|
|
490
|
+
// Reasoning/thinking content
|
|
491
|
+
if (delta.reasoning_content && typeof delta.reasoning_content === 'string') {
|
|
492
|
+
yield { type: 'thinking', content: delta.reasoning_content };
|
|
493
|
+
}
|
|
494
|
+
// Text content
|
|
495
|
+
if (delta.content && typeof delta.content === 'string') {
|
|
496
|
+
yield { type: 'text', content: delta.content };
|
|
497
|
+
}
|
|
498
|
+
// Tool calls (accumulated across deltas)
|
|
499
|
+
const toolCalls = delta.tool_calls;
|
|
500
|
+
if (toolCalls) {
|
|
501
|
+
for (const tc of toolCalls) {
|
|
502
|
+
const idx = tc.index;
|
|
503
|
+
const fn = tc.function;
|
|
504
|
+
if (!pendingTools.has(idx)) {
|
|
505
|
+
pendingTools.set(idx, {
|
|
506
|
+
id: tc.id ?? '',
|
|
507
|
+
name: fn?.name ? fn.name : '',
|
|
508
|
+
args: '',
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
const pending = pendingTools.get(idx);
|
|
512
|
+
if (tc.id)
|
|
513
|
+
pending.id = tc.id;
|
|
514
|
+
if (fn?.name)
|
|
515
|
+
pending.name = fn.name;
|
|
516
|
+
if (fn?.arguments)
|
|
517
|
+
pending.args += fn.arguments;
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
// finish_reason signals completion
|
|
521
|
+
const finishReason = choice.finish_reason;
|
|
522
|
+
if (finishReason === 'tool_calls' || finishReason === 'stop') {
|
|
523
|
+
// Emit any accumulated tool calls
|
|
524
|
+
for (const [, pending] of pendingTools) {
|
|
525
|
+
let input = {};
|
|
526
|
+
try {
|
|
527
|
+
input = JSON.parse(pending.args);
|
|
528
|
+
}
|
|
529
|
+
catch (_error) {
|
|
530
|
+
// JSON.parse failed — fall back to empty object; unparseable args are non-fatal
|
|
531
|
+
}
|
|
532
|
+
yield {
|
|
533
|
+
type: 'tool_use',
|
|
534
|
+
id: pending.id,
|
|
535
|
+
name: pending.name,
|
|
536
|
+
input,
|
|
537
|
+
};
|
|
538
|
+
}
|
|
539
|
+
pendingTools.clear();
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
catch (error) {
|
|
543
|
+
logger.debug('Skipping malformed SSE JSON line', { json, error });
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
/**
|
|
549
|
+
* Creates a Zhipu GLM AI provider instance.
|
|
550
|
+
*
|
|
551
|
+
* @param config - Zhipu-specific configuration (API key, model, max tokens, base URL).
|
|
552
|
+
* @returns An `AIProvider` backed by the Zhipu Chat Completions API.
|
|
553
|
+
*/
|
|
554
|
+
export function createProvider(config) {
|
|
555
|
+
return new ZhipuAIProvider(config);
|
|
556
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zhipu AI secret definitions — self-registered at import time so the
|
|
3
|
+
* runtime secrets registry (`@molecule/api-secrets`) can drive boot-time
|
|
4
|
+
* configuration reports and actionable "not configured" errors.
|
|
5
|
+
*
|
|
6
|
+
* Content is derived MECHANICALLY from this package's mlcl registry secrets
|
|
7
|
+
* entry (label/instructions/setupUrl/example) via the fleet formula, so
|
|
8
|
+
* packages sharing a key register byte-identical definitions and
|
|
9
|
+
* registration order never matters.
|
|
10
|
+
*
|
|
11
|
+
* @module
|
|
12
|
+
*/
|
|
13
|
+
import type { SecretDefinition } from '@molecule/api-secrets';
|
|
14
|
+
/** Secret definitions required by the Zhipu AI bond. */
|
|
15
|
+
export declare const aiZhipuSecretDefinitions: SecretDefinition[];
|
|
16
|
+
//# sourceMappingURL=secrets.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"secrets.d.ts","sourceRoot":"","sources":["../src/secrets.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AAG7D,wDAAwD;AACxD,eAAO,MAAM,wBAAwB,EAAE,gBAAgB,EAOtD,CAAA"}
|
package/dist/secrets.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zhipu AI secret definitions — self-registered at import time so the
|
|
3
|
+
* runtime secrets registry (`@molecule/api-secrets`) can drive boot-time
|
|
4
|
+
* configuration reports and actionable "not configured" errors.
|
|
5
|
+
*
|
|
6
|
+
* Content is derived MECHANICALLY from this package's mlcl registry secrets
|
|
7
|
+
* entry (label/instructions/setupUrl/example) via the fleet formula, so
|
|
8
|
+
* packages sharing a key register byte-identical definitions and
|
|
9
|
+
* registration order never matters.
|
|
10
|
+
*
|
|
11
|
+
* @module
|
|
12
|
+
*/
|
|
13
|
+
import { registerSecrets } from '@molecule/api-secrets';
|
|
14
|
+
/** Secret definitions required by the Zhipu AI bond. */
|
|
15
|
+
export const aiZhipuSecretDefinitions = [
|
|
16
|
+
{
|
|
17
|
+
key: 'ZHIPU_API_KEY',
|
|
18
|
+
description: 'Zhipu AI API key — Create an API key in the Zhipu AI (BigModel) user center.',
|
|
19
|
+
helpUrl: 'https://open.bigmodel.cn/usercenter/apikeys',
|
|
20
|
+
required: true,
|
|
21
|
+
},
|
|
22
|
+
];
|
|
23
|
+
registerSecrets(aiZhipuSecretDefinitions);
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zhipu GLM AI provider configuration.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type { AiRateLimitCallback } from '@molecule/api-ai';
|
|
7
|
+
/**
|
|
8
|
+
* Configuration for Zhipu.
|
|
9
|
+
*/
|
|
10
|
+
export interface ZhipuConfig {
|
|
11
|
+
/** Called on each rate-limited/overloaded upstream response, before any retry sleep. */
|
|
12
|
+
onRateLimit?: AiRateLimitCallback;
|
|
13
|
+
/** API key. Defaults to ZHIPU_API_KEY env var. */
|
|
14
|
+
apiKey?: string;
|
|
15
|
+
/** Default model. Defaults to 'glm-5.2'. */
|
|
16
|
+
defaultModel?: string;
|
|
17
|
+
/** Maximum tokens for completions. */
|
|
18
|
+
maxTokens?: number;
|
|
19
|
+
/** Base URL override (for proxies). Defaults to 'https://open.bigmodel.cn/api/paas'. */
|
|
20
|
+
baseUrl?: string;
|
|
21
|
+
/**
|
|
22
|
+
* Chat-completions path appended to {@link baseUrl}. Defaults to
|
|
23
|
+
* `/v4/chat/completions` (Zhipu-native). Set to `/chat/completions` when the
|
|
24
|
+
* GLM open weights are served from a US OpenAI-compatible host (DeepInfra:
|
|
25
|
+
* baseUrl='https://api.deepinfra.com/v1/openai').
|
|
26
|
+
*/
|
|
27
|
+
completionsPath?: string;
|
|
28
|
+
/**
|
|
29
|
+
* Catalog-id → upstream-model-id map, applied to the outbound request only, so
|
|
30
|
+
* a US host (DeepInfra) receives its namespaced id (`zai-org/GLM-5.2`) while
|
|
31
|
+
* pricing/cost/display keep the canonical catalog id (`glm-5.2`).
|
|
32
|
+
*/
|
|
33
|
+
modelMap?: Record<string, string>;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Process Env interface.
|
|
37
|
+
*/
|
|
38
|
+
export interface ProcessEnv {
|
|
39
|
+
ZHIPU_API_KEY: string;
|
|
40
|
+
/** Base URL override (for credential brokers / gateways / US OpenAI-compatible hosts). */
|
|
41
|
+
ZHIPU_BASE_URL?: string;
|
|
42
|
+
/** Chat-completions path override (see {@link ZhipuConfig.completionsPath}). */
|
|
43
|
+
ZHIPU_COMPLETIONS_PATH?: string;
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +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"}
|
package/dist/types.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@molecule/api-ai-zhipu",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Zhipu GLM AI provider for molecule.dev",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc",
|
|
10
|
+
"test": "vitest run",
|
|
11
|
+
"test:watch": "vitest"
|
|
12
|
+
},
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"keywords": [
|
|
23
|
+
"molecule",
|
|
24
|
+
"ai",
|
|
25
|
+
"zhipu",
|
|
26
|
+
"glm"
|
|
27
|
+
],
|
|
28
|
+
"license": "Apache-2.0",
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@molecule/api-ai": "1.0.0",
|
|
31
|
+
"@molecule/api-bond": "1.0.0",
|
|
32
|
+
"@types/node": "26.1.2",
|
|
33
|
+
"typescript": "6.0.3",
|
|
34
|
+
"vitest": "4.1.10"
|
|
35
|
+
},
|
|
36
|
+
"peerDependencies": {
|
|
37
|
+
"@molecule/api-ai": "^1.0.0",
|
|
38
|
+
"@molecule/api-bond": "^1.0.0",
|
|
39
|
+
"@molecule/api-i18n": "^1.0.0",
|
|
40
|
+
"@molecule/api-secrets": "^1.0.0"
|
|
41
|
+
},
|
|
42
|
+
"peerDependenciesMeta": {
|
|
43
|
+
"@molecule/api-ai": {
|
|
44
|
+
"optional": true
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|