@molecule/api-ai-moonshot 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 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,2 @@
1
+ export {};
2
+ //# sourceMappingURL=browser-guard.d.ts.map
@@ -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-moonshot` 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-moonshot 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 {};
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Moonshot (Kimi) AI provider for molecule.dev.
3
+ *
4
+ * @remarks
5
+ * Config: `MOONSHOT_API_KEY` (SERVER-side only) plus an optional default model id/base URL.
6
+ *
7
+ * **Missing `MOONSHOT_API_KEY` fails fast**: the provider throws naming the exact env var on
8
+ * first use (the exported `provider` is a lazy proxy, so this fires on the first `chat()` call,
9
+ * not at 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
+ * **Reasoning effort** (`KIMI_REASONING_EFFORT`, optional): when a call doesn't pass
16
+ * `params.thinking`, the provider reads this env var — `disabled` (default) fully disables
17
+ * thinking (fastest, least accurate); `minimal` | `low` | `medium` | `high` set the API's
18
+ * `reasoning_effort`. Note: on kimi-k2.6 even `minimal` produces ~1000 chars of reasoning
19
+ * per turn (~5x slower than `disabled`, but more reliable for complex multi-step tasks).
20
+ *
21
+ * @module
22
+ */
23
+ export * from './browser-guard.js';
24
+ export * from './provider.js';
25
+ export * from './secrets.js';
26
+ export * from './types.js';
27
+ import type { AIProvider } from '@molecule/api-ai';
28
+ /**
29
+ * The provider implementation.
30
+ */
31
+ export declare const provider: AIProvider;
32
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;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,45 @@
1
+ /**
2
+ * Moonshot (Kimi) AI provider for molecule.dev.
3
+ *
4
+ * @remarks
5
+ * Config: `MOONSHOT_API_KEY` (SERVER-side only) plus an optional default model id/base URL.
6
+ *
7
+ * **Missing `MOONSHOT_API_KEY` fails fast**: the provider throws naming the exact env var on
8
+ * first use (the exported `provider` is a lazy proxy, so this fires on the first `chat()` call,
9
+ * not at 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
+ * **Reasoning effort** (`KIMI_REASONING_EFFORT`, optional): when a call doesn't pass
16
+ * `params.thinking`, the provider reads this env var — `disabled` (default) fully disables
17
+ * thinking (fastest, least accurate); `minimal` | `low` | `medium` | `high` set the API's
18
+ * `reasoning_effort`. Note: on kimi-k2.6 even `minimal` produces ~1000 chars of reasoning
19
+ * per turn (~5x slower than `disabled`, but more reliable for complex multi-step tasks).
20
+ *
21
+ * @module
22
+ */
23
+ export * from './browser-guard.js';
24
+ export * from './provider.js';
25
+ export * from './secrets.js';
26
+ export * from './types.js';
27
+ import { createProvider } from './provider.js';
28
+ /** Lazily-initialized provider singleton. Defers creation until first use so that env vars / secrets are resolved. */
29
+ let _provider = null;
30
+ /**
31
+ * The provider implementation.
32
+ */
33
+ export const provider = new Proxy({}, {
34
+ get(_, prop, receiver) {
35
+ if (!_provider)
36
+ _provider = createProvider();
37
+ return Reflect.get(_provider, prop, receiver);
38
+ },
39
+ // 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)
40
+ set(_, prop, value) {
41
+ if (!_provider)
42
+ _provider = createProvider();
43
+ return Reflect.set(_provider, prop, value);
44
+ },
45
+ });
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Moonshot (Kimi) implementation of AIProvider.
3
+ *
4
+ * Uses the Moonshot 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 { MoonshotConfig } from './types.js';
11
+ /**
12
+ * Creates a Moonshot (Kimi) AI provider instance.
13
+ *
14
+ * @param config - Moonshot-specific configuration (API key, model, max tokens, base URL).
15
+ * @returns An `AIProvider` backed by the Moonshot Chat Completions API.
16
+ */
17
+ export declare function createProvider(config?: MoonshotConfig): 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,cAAc,EAAE,MAAM,YAAY,CAAA;AAioBhD;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,MAAM,CAAC,EAAE,cAAc,GAAG,UAAU,CAElE"}
@@ -0,0 +1,608 @@
1
+ /**
2
+ * Moonshot (Kimi) implementation of AIProvider.
3
+ *
4
+ * Uses the Moonshot 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
+ * Map thinking budget tokens to reasoning_effort level (only 'low' or 'high' supported).
17
+ *
18
+ * @param budgetTokens - Requested thinking budget in tokens.
19
+ * @returns Either `'high'` or `'low'` for the upstream effort hint.
20
+ */
21
+ function budgetToEffort(budgetTokens) {
22
+ return budgetTokens >= 8_000 ? 'high' : 'low';
23
+ }
24
+ /**
25
+ * Moonshot (Kimi) AI provider that implements the `AIProvider` interface
26
+ * using Moonshot's OpenAI-compatible Chat Completions API with support
27
+ * for streaming, tool use, and reasoning.
28
+ */
29
+ class MoonshotAIProvider {
30
+ name = 'moonshot';
31
+ apiKey;
32
+ defaultModel;
33
+ maxTokens;
34
+ baseUrl;
35
+ completionsPath;
36
+ modelMap;
37
+ onRateLimit;
38
+ constructor(config = {}) {
39
+ this.apiKey = config.apiKey ?? process.env.MOONSHOT_API_KEY ?? '';
40
+ this.defaultModel = config.defaultModel ?? 'kimi-k3';
41
+ this.maxTokens = config.maxTokens ?? 4096;
42
+ this.baseUrl = config.baseUrl ?? process.env.MOONSHOT_BASE_URL ?? 'https://api.moonshot.ai';
43
+ this.completionsPath =
44
+ config.completionsPath ?? process.env.MOONSHOT_COMPLETIONS_PATH ?? '/v1/chat/completions';
45
+ this.modelMap = config.modelMap ?? {};
46
+ this.onRateLimit = config.onRateLimit;
47
+ // Fail fast with an actionable local error rather than a cryptic 401 on the
48
+ // first request. The default `provider` export constructs lazily on first
49
+ // use (see index.ts), so this surfaces the moment the provider is actually
50
+ // used, not at bond/module-load time.
51
+ if (!this.apiKey) {
52
+ throw new Error('MOONSHOT_API_KEY is not set. Add it to your environment to use the Moonshot (Kimi) AI provider.');
53
+ }
54
+ }
55
+ /**
56
+ * Sends a chat request and yields streamed events (text chunks, tool use, done/error).
57
+ *
58
+ * @param params - Chat parameters including messages, model, system prompt, and tools.
59
+ * @yields {ChatEvent} Chat events including text chunks, tool use requests, done signals, and errors.
60
+ */
61
+ async *chat(params) {
62
+ // Canonical catalog id (used everywhere else); translate to the upstream's
63
+ // id only for the outbound request body via `modelMap`.
64
+ const canonicalModel = params.model ?? this.defaultModel;
65
+ const model = this.modelMap[canonicalModel] ?? canonicalModel;
66
+ const maxTokens = params.maxTokens ?? this.maxTokens;
67
+ const messages = this.formatMessages(params.messages, params.system);
68
+ const body = {
69
+ // Abuse attribution (see ChatParams.endUserId): OpenAI-compatible APIs
70
+ // scope enforcement by `user`, so an abusive tenant can be actioned
71
+ // individually instead of the org key that serves everyone. The value is
72
+ // the caller's opaque hash — never derived or logged here.
73
+ ...(params.endUserId ? { user: params.endUserId } : {}),
74
+ model,
75
+ max_completion_tokens: maxTokens,
76
+ messages,
77
+ stream: params.stream !== false,
78
+ ...(params.stream !== false ? { stream_options: { include_usage: true } } : {}),
79
+ };
80
+ // Tools: merge custom tools (function format) and server tools
81
+ const functions = params.tools?.length ? this.formatTools(params.tools) : [];
82
+ const serverTools = (params.serverTools ?? []).map((st) => ({ ...st }));
83
+ const allTools = [...functions, ...serverTools];
84
+ if (allTools.length > 0) {
85
+ body.tools = allTools;
86
+ }
87
+ // Model-family thinking rules (platform.kimi.ai docs, verified 2026-07-28):
88
+ // - kimi-k3: thinking is ALWAYS ON and the K2.x `thinking` param is
89
+ // unsupported (sending {type:"disabled"} is explicitly rejected). Depth
90
+ // rides the top-level reasoning_effort: low | high | max (default max).
91
+ // - kimi-k2.7-code: thinking always on, no reasoning_effort — send neither.
92
+ // - other kimi-k2.x: thinking on/off via `thinking: {type:"disabled"}`;
93
+ // reasoning_effort (minimal..high) is accepted though undocumented.
94
+ if (model.startsWith('kimi-k3')) {
95
+ const effort = params.thinking?.effort;
96
+ if (effort === 'low' || effort === 'high' || effort === 'max') {
97
+ body.reasoning_effort = effort;
98
+ }
99
+ // No effort resolved → omit and let the upstream default (max) apply.
100
+ }
101
+ else if (model.startsWith('kimi-k2.7-code')) {
102
+ // Forced thinking, no depth knob — nothing to send.
103
+ }
104
+ else if (params.thinking) {
105
+ // Prefer a caller-resolved native value from the model catalog; fall back
106
+ // to the budget threshold. NOTE: Moonshot's official docs expose only
107
+ // thinking on/off for K2.x — current k2.x catalog entries are
108
+ // fixed-effort and never set params.thinking, so this branch only fires
109
+ // for explicit callers.
110
+ body.reasoning_effort = params.thinking.effort ?? budgetToEffort(params.thinking.budgetTokens);
111
+ }
112
+ else {
113
+ // Default: read from env so callers can tune kimi's reasoning level
114
+ // without touching code. Supported values:
115
+ // - 'disabled' (default) — fully disables thinking via
116
+ // `thinking: {type: "disabled"}`. Fastest, least accurate.
117
+ // - 'minimal' | 'low' | 'medium' | 'high' — sets `reasoning_effort`.
118
+ // Note: on kimi-k2.6, `minimal` still produces ~1000 chars of
119
+ // reasoning_content per turn (~5x slower than disabled but more
120
+ // reliable for complex multi-step tasks).
121
+ const effort = (process.env.KIMI_REASONING_EFFORT || 'disabled').toLowerCase();
122
+ if (effort === 'minimal' || effort === 'low' || effort === 'medium' || effort === 'high') {
123
+ body.reasoning_effort = effort;
124
+ }
125
+ else {
126
+ body.thinking = { type: 'disabled' };
127
+ }
128
+ }
129
+ if (params.temperature !== undefined) {
130
+ body.temperature = params.temperature;
131
+ }
132
+ const headers = {
133
+ 'Content-Type': 'application/json',
134
+ Authorization: `Bearer ${this.apiKey}`,
135
+ };
136
+ // Kimi with reasoning enabled (especially on large multimodal/vision
137
+ // prompts) regularly takes longer than 5 minutes to finish a streaming
138
+ // response. 5 min was too aggressive — bumped to 15 min to accommodate
139
+ // long-context + reasoning workloads. Caller can override via
140
+ // params.signal if a tighter budget is needed.
141
+ const DEFAULT_TIMEOUT_MS = 15 * 60 * 1000;
142
+ const signal = params.signal ?? AbortSignal.timeout(DEFAULT_TIMEOUT_MS);
143
+ // Retry with exponential backoff for rate limits (429) and overloaded (503)
144
+ const MAX_RETRIES = 3;
145
+ let response = null;
146
+ for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
147
+ response = await fetch(`${this.baseUrl}${this.completionsPath}`, {
148
+ method: 'POST',
149
+ headers,
150
+ body: JSON.stringify(body),
151
+ signal,
152
+ });
153
+ if (response.status === 429 || response.status === 503) {
154
+ const willRetry = attempt < MAX_RETRIES;
155
+ // Retry-After is equally-valid as delta-seconds or an HTTP-date;
156
+ // parseInt on the date form yields NaN, degrading the backoff to a
157
+ // ~0ms retry against an already rate-limiting API. Guard it.
158
+ const retryAfter = response.headers.get('retry-after');
159
+ const parsedRetryAfterSeconds = retryAfter ? parseInt(retryAfter, 10) : NaN;
160
+ const hasRetryAfter = Number.isFinite(parsedRetryAfterSeconds) && parsedRetryAfterSeconds >= 0;
161
+ // Additive jitter (<500ms): concurrent callers rejected together must
162
+ // not sleep identical delays and re-arrive as the same burst.
163
+ const delayMs = willRetry
164
+ ? (hasRetryAfter
165
+ ? Math.min(parsedRetryAfterSeconds * 1000, 60_000)
166
+ : Math.min(1000 * 2 ** attempt, 30_000)) + Math.floor(Math.random() * 500)
167
+ : 0;
168
+ try {
169
+ this.onRateLimit?.({
170
+ provider: this.name,
171
+ model,
172
+ status: response.status,
173
+ attempt: attempt + 1,
174
+ willRetry,
175
+ retryInMs: delayMs,
176
+ ...(hasRetryAfter ? { retryAfterSeconds: parsedRetryAfterSeconds } : {}),
177
+ });
178
+ }
179
+ catch (error) {
180
+ // Host-app telemetry; its failure must never break the request.
181
+ logger.warn('onRateLimit callback threw', { error });
182
+ }
183
+ if (willRetry) {
184
+ logger.warn('Moonshot API rate limited, retrying', {
185
+ status: response.status,
186
+ attempt: attempt + 1,
187
+ delayMs,
188
+ });
189
+ await new Promise((resolve) => {
190
+ const timer = setTimeout(resolve, delayMs);
191
+ signal.addEventListener('abort', () => {
192
+ clearTimeout(timer);
193
+ resolve();
194
+ }, { once: true });
195
+ });
196
+ if (signal.aborted)
197
+ break;
198
+ continue;
199
+ }
200
+ }
201
+ break;
202
+ }
203
+ if (!response.ok) {
204
+ const errorBody = await response.text();
205
+ let detail = `HTTP ${response.status}`;
206
+ try {
207
+ const parsed = JSON.parse(errorBody);
208
+ if (parsed.error?.message)
209
+ detail = parsed.error.message;
210
+ }
211
+ catch (_error) {
212
+ // Error body is not JSON — use raw text as detail if short enough
213
+ if (errorBody.length > 0 && errorBody.length < 200)
214
+ detail = errorBody;
215
+ }
216
+ logger.error('Moonshot API error', { status: response.status, detail, errorBody });
217
+ const clientMessage = response.status === 429
218
+ ? 'AI rate limit exceeded. Please try again shortly.'
219
+ : response.status === 401
220
+ ? 'AI service configuration error.'
221
+ : response.status === 400 &&
222
+ /prompt is too long|too many tokens|token.*limit|context.*length/i.test(detail)
223
+ ? "Conversation too long for the model's context window. Use /compact to free space, or start a new conversation."
224
+ : response.status === 400
225
+ ? 'AI request was invalid — check the model and request parameters.'
226
+ : response.status === 503
227
+ ? 'AI service is temporarily overloaded. Please try again in a moment.'
228
+ : 'AI service error. Please try again.';
229
+ yield { type: 'error', message: clientMessage, errorKey: 'ai.error.apiError' };
230
+ return;
231
+ }
232
+ if (params.stream === false) {
233
+ const data = (await response.json());
234
+ yield* this.parseNonStreamingResponse(data);
235
+ return;
236
+ }
237
+ yield* this.parseStreamingResponse(response);
238
+ }
239
+ /**
240
+ * Converts internal `ChatMessage` objects to the OpenAI-compatible message format.
241
+ * System prompt is prepended as a system role message.
242
+ *
243
+ * @param messages - The chat messages to format.
244
+ * @param system - Optional system prompt to prepend.
245
+ * @returns The formatted messages array for the Moonshot API.
246
+ */
247
+ formatMessages(messages, system) {
248
+ const formatted = [];
249
+ if (system) {
250
+ formatted.push({ role: 'system', content: system });
251
+ }
252
+ for (const m of messages) {
253
+ if (m.role === 'system')
254
+ continue; // handled above
255
+ if (typeof m.content === 'string') {
256
+ formatted.push({
257
+ role: m.role,
258
+ content: m.content,
259
+ ...(m.role === 'assistant' && m.reasoning ? { reasoning_content: m.reasoning } : {}),
260
+ });
261
+ continue;
262
+ }
263
+ // Content blocks — convert to OpenAI format
264
+ const blocks = m.content;
265
+ const parts = [];
266
+ const toolCalls = [];
267
+ const toolResults = [];
268
+ for (const block of blocks) {
269
+ switch (block.type) {
270
+ case 'text':
271
+ parts.push({ type: 'text', text: block.text });
272
+ break;
273
+ case 'image':
274
+ parts.push({
275
+ type: 'image_url',
276
+ image_url: { url: `data:${block.mediaType};base64,${block.data}` },
277
+ });
278
+ break;
279
+ case 'tool_use':
280
+ toolCalls.push({
281
+ id: block.id,
282
+ type: 'function',
283
+ function: { name: block.name, arguments: JSON.stringify(block.input) },
284
+ });
285
+ break;
286
+ case 'tool_result':
287
+ toolResults.push({
288
+ role: 'tool',
289
+ tool_call_id: block.tool_use_id,
290
+ content: typeof block.content === 'string' ? block.content : JSON.stringify(block.content),
291
+ });
292
+ break;
293
+ default:
294
+ // document, audio, video — unsupported, skip
295
+ break;
296
+ }
297
+ }
298
+ if (toolResults.length > 0) {
299
+ // Tool results are separate messages in OpenAI format
300
+ for (const tr of toolResults) {
301
+ formatted.push(tr);
302
+ }
303
+ }
304
+ else if (toolCalls.length > 0) {
305
+ // Assistant message with tool calls — replay reasoning_content so
306
+ // preserved-thinking models (kimi-k3, kimi-k2.7-code) keep reasoning
307
+ // continuity through the tool loop. The typed ChatMessage.reasoning
308
+ // field is preferred; the legacy untyped `_reasoning` stays as a
309
+ // fallback for older callers.
310
+ const rawMsg = m;
311
+ formatted.push({
312
+ role: 'assistant',
313
+ content: parts.length > 0 ? parts : null,
314
+ reasoning_content: m.reasoning ?? (rawMsg._reasoning || ''),
315
+ tool_calls: toolCalls,
316
+ });
317
+ }
318
+ else if (m.role === 'assistant' && m.reasoning) {
319
+ // Plain assistant turn that carried reasoning — replay it too.
320
+ formatted.push({
321
+ role: 'assistant',
322
+ content: parts.length === 1 && parts[0].type === 'text' ? parts[0].text : parts,
323
+ reasoning_content: m.reasoning,
324
+ });
325
+ }
326
+ else {
327
+ formatted.push({
328
+ role: m.role,
329
+ content: parts.length === 1 && parts[0].type === 'text' ? parts[0].text : parts,
330
+ });
331
+ }
332
+ }
333
+ return formatted;
334
+ }
335
+ /**
336
+ * Converts internal `AITool` definitions to the OpenAI function calling format.
337
+ *
338
+ * @param tools - The AI tool definitions to format.
339
+ * @returns The formatted tools array for the Moonshot API.
340
+ */
341
+ formatTools(tools) {
342
+ return tools.map((tool) => ({
343
+ type: 'function',
344
+ function: {
345
+ name: tool.name,
346
+ description: tool.description,
347
+ parameters: tool.parameters,
348
+ },
349
+ }));
350
+ }
351
+ /**
352
+ * Parses a non-streaming OpenAI-compatible response into `ChatEvent` objects.
353
+ *
354
+ * @param data - The raw API response data to parse.
355
+ * @yields {ChatEvent} Chat events parsed from the response.
356
+ */
357
+ *parseNonStreamingResponse(data) {
358
+ const choices = data.choices;
359
+ if (choices?.length) {
360
+ const message = choices[0].message;
361
+ if (message) {
362
+ // Reasoning/thinking content
363
+ if (message.reasoning_content && typeof message.reasoning_content === 'string') {
364
+ yield { type: 'thinking', content: message.reasoning_content };
365
+ }
366
+ if (message.content && typeof message.content === 'string') {
367
+ yield { type: 'text', content: message.content };
368
+ }
369
+ const toolCalls = message.tool_calls;
370
+ if (toolCalls) {
371
+ for (const tc of toolCalls) {
372
+ const fn = tc.function;
373
+ let input = {};
374
+ try {
375
+ input = JSON.parse(fn.arguments);
376
+ }
377
+ catch (_error) {
378
+ // API returned malformed tool argument JSON — fall back to empty object
379
+ }
380
+ yield {
381
+ type: 'tool_use',
382
+ id: tc.id,
383
+ name: fn.name,
384
+ input,
385
+ };
386
+ }
387
+ }
388
+ }
389
+ }
390
+ const usage = data.usage;
391
+ // prompt_tokens INCLUDES cached tokens on OpenAI-compatible APIs — report
392
+ // the cached portion separately (cacheReadPricePerMTok) and only the fresh
393
+ // remainder as inputTokens, matching the other bonds' semantics.
394
+ const cachedTokens = usage?.prompt_tokens_details?.cached_tokens ?? 0;
395
+ yield {
396
+ type: 'done',
397
+ usage: {
398
+ inputTokens: Math.max(0, (usage?.prompt_tokens ?? 0) - cachedTokens),
399
+ outputTokens: usage?.completion_tokens ?? 0,
400
+ ...(cachedTokens ? { cacheReadInputTokens: cachedTokens } : {}),
401
+ },
402
+ };
403
+ }
404
+ /**
405
+ * Parses a streaming SSE response from the Moonshot API (OpenAI-compatible format),
406
+ * yielding `ChatEvent` objects as they arrive.
407
+ *
408
+ * @param response - The fetch Response with an SSE body stream.
409
+ * @yields {ChatEvent} Chat events as they arrive from the SSE stream.
410
+ */
411
+ async *parseStreamingResponse(response) {
412
+ const reader = response.body?.getReader();
413
+ if (!reader) {
414
+ yield {
415
+ type: 'error',
416
+ message: t('ai.error.noResponseBody'),
417
+ errorKey: 'ai.error.noResponseBody',
418
+ };
419
+ return;
420
+ }
421
+ const decoder = new TextDecoder();
422
+ let buffer = '';
423
+ const state = { inputTokens: 0, outputTokens: 0, cacheReadTokens: 0 };
424
+ // Track pending tool calls (accumulated across deltas)
425
+ const pendingTools = new Map();
426
+ try {
427
+ while (true) {
428
+ const { done, value } = await reader.read();
429
+ if (done)
430
+ break;
431
+ buffer += decoder.decode(value, { stream: true });
432
+ const lines = buffer.split('\n');
433
+ buffer = lines.pop() ?? '';
434
+ let yieldedContent = false;
435
+ for (const event of this.processSSELines(lines, pendingTools, state)) {
436
+ yieldedContent = true;
437
+ yield event;
438
+ if (event.type === 'error')
439
+ return;
440
+ }
441
+ // Signal liveness when the API sends data but no content events were produced
442
+ // (e.g., during extended reasoning with empty chunks or keepalive comments).
443
+ // This resets the consumer's stream timeout without forwarding to the client.
444
+ if (!yieldedContent) {
445
+ yield { type: 'keep_alive' };
446
+ }
447
+ }
448
+ // Flush any remaining data in the buffer after EOF
449
+ if (buffer.trim()) {
450
+ for (const event of this.processSSELines(buffer.split('\n'), pendingTools, state)) {
451
+ yield event;
452
+ if (event.type === 'error')
453
+ return;
454
+ }
455
+ }
456
+ // Emit any tool calls still pending after stream ends (safety net for missing finish_reason)
457
+ if (pendingTools.size > 0) {
458
+ for (const [, pending] of pendingTools) {
459
+ let input = {};
460
+ try {
461
+ input = JSON.parse(pending.args);
462
+ }
463
+ catch (_error) {
464
+ // Accumulated streaming tool args are malformed JSON — fall back to empty object
465
+ }
466
+ yield { type: 'tool_use', id: pending.id, name: pending.name, input };
467
+ }
468
+ pendingTools.clear();
469
+ }
470
+ }
471
+ finally {
472
+ reader.releaseLock();
473
+ }
474
+ // See the non-streaming path: cached tokens are reported separately and
475
+ // subtracted from inputTokens (prompt_tokens includes them).
476
+ yield {
477
+ type: 'done',
478
+ usage: {
479
+ inputTokens: Math.max(0, state.inputTokens - state.cacheReadTokens),
480
+ outputTokens: state.outputTokens,
481
+ ...(state.cacheReadTokens ? { cacheReadInputTokens: state.cacheReadTokens } : {}),
482
+ },
483
+ };
484
+ }
485
+ /**
486
+ * Processes an array of SSE lines from the Moonshot streaming response, yielding
487
+ * ChatEvent objects for text, thinking, tool use, and tool completion.
488
+ *
489
+ * @param lines - Raw SSE lines to process.
490
+ * @param pendingTools - Mutable map tracking tool call deltas accumulated across chunks.
491
+ * @param state - Mutable token usage counters.
492
+ * @yields {ChatEvent} Chat events extracted from the SSE lines.
493
+ */
494
+ *processSSELines(lines, pendingTools, state) {
495
+ for (const line of lines) {
496
+ if (!line.startsWith('data: '))
497
+ continue;
498
+ const json = line.slice(6).trim();
499
+ if (json === '[DONE]')
500
+ continue;
501
+ try {
502
+ const event = JSON.parse(json);
503
+ // A mid-stream error arrives as a `data: {"error": {...}}` chunk (no
504
+ // `choices`) — most importantly rate-limit/overload during high load.
505
+ // Without this it matches no branch below and is silently dropped, so the
506
+ // stream falls through to a misleading `done` and the truncated turn reads
507
+ // as a successful completion. Surface it as a real error event (also
508
+ // satisfies the no-silent-swallow rule).
509
+ const streamError = event.error;
510
+ if (streamError) {
511
+ const detail = `${streamError.code ?? streamError.type ?? ''} ${streamError.message ?? ''}`;
512
+ logger.error('Moonshot streaming error event', {
513
+ type: streamError.type,
514
+ code: streamError.code,
515
+ message: streamError.message,
516
+ });
517
+ const clientMessage = /overload|capacity|503|529/i.test(detail)
518
+ ? 'AI service is temporarily overloaded. Please try again in a moment.'
519
+ : /rate.?limit|429|quota/i.test(detail)
520
+ ? 'AI rate limit exceeded. Please try again shortly.'
521
+ : 'AI service error. Please try again.';
522
+ yield { type: 'error', message: clientMessage, errorKey: 'ai.error.apiError' };
523
+ continue;
524
+ }
525
+ // Usage info (may appear in the final chunk)
526
+ const usage = event.usage;
527
+ if (usage) {
528
+ if (usage.prompt_tokens)
529
+ state.inputTokens = usage.prompt_tokens;
530
+ if (usage.completion_tokens)
531
+ state.outputTokens = usage.completion_tokens;
532
+ if (usage.prompt_tokens_details?.cached_tokens)
533
+ state.cacheReadTokens = usage.prompt_tokens_details.cached_tokens;
534
+ }
535
+ const choices = event.choices;
536
+ if (!choices?.length)
537
+ continue;
538
+ const choice = choices[0];
539
+ const delta = choice.delta;
540
+ if (!delta)
541
+ continue;
542
+ // Reasoning/thinking content
543
+ if (delta.reasoning_content && typeof delta.reasoning_content === 'string') {
544
+ yield { type: 'thinking', content: delta.reasoning_content };
545
+ }
546
+ // Text content
547
+ if (delta.content && typeof delta.content === 'string') {
548
+ yield { type: 'text', content: delta.content };
549
+ }
550
+ // Tool calls (accumulated across deltas)
551
+ const toolCalls = delta.tool_calls;
552
+ if (toolCalls) {
553
+ for (const tc of toolCalls) {
554
+ const idx = tc.index;
555
+ const fn = tc.function;
556
+ if (!pendingTools.has(idx)) {
557
+ pendingTools.set(idx, {
558
+ id: tc.id ?? '',
559
+ name: fn?.name ? fn.name : '',
560
+ args: '',
561
+ });
562
+ }
563
+ const pending = pendingTools.get(idx);
564
+ if (tc.id)
565
+ pending.id = tc.id;
566
+ if (fn?.name)
567
+ pending.name = fn.name;
568
+ if (fn?.arguments)
569
+ pending.args += fn.arguments;
570
+ }
571
+ }
572
+ // finish_reason signals completion
573
+ const finishReason = choice.finish_reason;
574
+ if (finishReason === 'tool_calls' || finishReason === 'stop') {
575
+ // Emit any accumulated tool calls
576
+ for (const [, pending] of pendingTools) {
577
+ let input = {};
578
+ try {
579
+ input = JSON.parse(pending.args);
580
+ }
581
+ catch (_error) {
582
+ // Tool args accumulated from SSE deltas are malformed JSON — fall back to empty object
583
+ }
584
+ yield {
585
+ type: 'tool_use',
586
+ id: pending.id,
587
+ name: pending.name,
588
+ input,
589
+ };
590
+ }
591
+ pendingTools.clear();
592
+ }
593
+ }
594
+ catch (_error) {
595
+ logger.debug('Skipping malformed SSE JSON line', { json });
596
+ }
597
+ }
598
+ }
599
+ }
600
+ /**
601
+ * Creates a Moonshot (Kimi) AI provider instance.
602
+ *
603
+ * @param config - Moonshot-specific configuration (API key, model, max tokens, base URL).
604
+ * @returns An `AIProvider` backed by the Moonshot Chat Completions API.
605
+ */
606
+ export function createProvider(config) {
607
+ return new MoonshotAIProvider(config);
608
+ }
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Moonshot 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 Moonshot AI bond. */
15
+ export declare const aiMoonshotSecretDefinitions: 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,2DAA2D;AAC3D,eAAO,MAAM,2BAA2B,EAAE,gBAAgB,EAQzD,CAAA"}
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Moonshot 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 Moonshot AI bond. */
15
+ export const aiMoonshotSecretDefinitions = [
16
+ {
17
+ key: 'MOONSHOT_API_KEY',
18
+ description: 'Moonshot API key — Create an API key in the Moonshot AI console.',
19
+ helpUrl: 'https://platform.moonshot.ai/console/api-keys',
20
+ required: true,
21
+ example: 'sk-...',
22
+ },
23
+ ];
24
+ registerSecrets(aiMoonshotSecretDefinitions);
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Moonshot (Kimi) AI provider configuration.
3
+ *
4
+ * @module
5
+ */
6
+ import type { AiRateLimitCallback } from '@molecule/api-ai';
7
+ /**
8
+ * Configuration for Moonshot.
9
+ */
10
+ export interface MoonshotConfig {
11
+ /** API key. Defaults to MOONSHOT_API_KEY env var. */
12
+ apiKey?: string;
13
+ /** Default model. Defaults to 'kimi-k3'. */
14
+ defaultModel?: string;
15
+ /** Maximum tokens for completions. */
16
+ maxTokens?: number;
17
+ /** Base URL override (for proxies). Defaults to 'https://api.moonshot.cn'. */
18
+ baseUrl?: string;
19
+ /**
20
+ * Chat-completions path appended to {@link baseUrl}. Defaults to
21
+ * `/v1/chat/completions` (Moonshot-direct puts the version in the path). Set
22
+ * this when the same open-weight model is served by a US OpenAI-compatible
23
+ * host whose version prefix lives in the base URL instead — e.g. DeepInfra:
24
+ * `baseUrl='https://api.deepinfra.com/v1/openai'` + `completionsPath='/chat/completions'`.
25
+ */
26
+ completionsPath?: string;
27
+ /**
28
+ * Optional catalog-id → upstream-model-id map, applied to the outbound request
29
+ * ONLY. Lets a US OpenAI-compatible host (DeepInfra) receive its namespaced id
30
+ * (`moonshotai/Kimi-K3`) while the rest of the platform — pricing, cost
31
+ * ceilings, display — keeps using the canonical catalog id (`kimi-k3`). An id
32
+ * not in the map passes through unchanged.
33
+ */
34
+ modelMap?: Record<string, string>;
35
+ /** Called on each rate-limited/overloaded upstream response, before any retry sleep. */
36
+ onRateLimit?: AiRateLimitCallback;
37
+ }
38
+ /**
39
+ * Process Env interface.
40
+ */
41
+ export interface ProcessEnv {
42
+ MOONSHOT_API_KEY: string;
43
+ /** Base URL override (for credential brokers / gateways / US OpenAI-compatible hosts). */
44
+ MOONSHOT_BASE_URL?: string;
45
+ /** Chat-completions path override (see {@link MoonshotConfig.completionsPath}). */
46
+ MOONSHOT_COMPLETIONS_PATH?: string;
47
+ }
48
+ //# 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,cAAc;IAC7B,qDAAqD;IACrD,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,4CAA4C;IAC5C,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,sCAAsC;IACtC,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,8EAA8E;IAC9E,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB;;;;;;OAMG;IACH,eAAe,CAAC,EAAE,MAAM,CAAA;IACxB;;;;;;OAMG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IACjC,wFAAwF;IACxF,WAAW,CAAC,EAAE,mBAAmB,CAAA;CAClC;AAED;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,gBAAgB,EAAE,MAAM,CAAA;IACxB,0FAA0F;IAC1F,iBAAiB,CAAC,EAAE,MAAM,CAAA;IAC1B,mFAAmF;IACnF,yBAAyB,CAAC,EAAE,MAAM,CAAA;CACnC"}
package/dist/types.js ADDED
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Moonshot (Kimi) AI provider configuration.
3
+ *
4
+ * @module
5
+ */
6
+ export {};
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@molecule/api-ai-moonshot",
3
+ "version": "1.0.0",
4
+ "description": "Moonshot (Kimi) 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
+ "moonshot",
26
+ "kimi"
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
+ }