@molecule/api-ai-openai 1.5.0 → 1.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -112
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@ AUTO-GENERATED — DO NOT EDIT THIS FILE.
|
|
|
3
3
|
Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
|
|
4
4
|
Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
|
|
5
5
|
To change this document, edit the module-level JSDoc in src/index.ts.
|
|
6
|
-
Generated: 2026-
|
|
6
|
+
Generated: 2026-11-13T19:22:14.614Z
|
|
7
7
|
-->
|
|
8
8
|
|
|
9
9
|
# @molecule/api-ai-openai
|
|
@@ -132,12 +132,6 @@ class OpenaiAIProvider implements AIProvider {
|
|
|
132
132
|
this.api = config.api ?? defaultApiFor(this.baseUrl)
|
|
133
133
|
this.quotaSemantics = config.quotaSemantics ?? defaultQuotaSemanticsFor(this.baseUrl)
|
|
134
134
|
|
|
135
|
-
// Fail fast with an actionable local error rather than a cryptic 401 on the
|
|
136
|
-
// first request. The default `provider` export constructs lazily on first
|
|
137
|
-
// use (see index.ts), so this surfaces the moment the provider is actually
|
|
138
|
-
// used, not at bond/module-load time. A key is required only for the
|
|
139
|
-
// official endpoint — a custom baseUrl may target a keyless self-hosted /
|
|
140
|
-
// local server (Ollama, LM Studio), where the auth header is omitted below.
|
|
141
135
|
const usingCustomEndpoint = Boolean(config.baseUrl ?? process.env.OPENAI_BASE_URL)
|
|
142
136
|
if (!this.apiKey && !usingCustomEndpoint) {
|
|
143
137
|
throw new Error(
|
|
@@ -146,15 +140,6 @@ class OpenaiAIProvider implements AIProvider {
|
|
|
146
140
|
}
|
|
147
141
|
}
|
|
148
142
|
|
|
149
|
-
/**
|
|
150
|
-
* Build the request body for `POST /v1/chat/completions`.
|
|
151
|
-
*
|
|
152
|
-
* @param params - The caller's chat parameters.
|
|
153
|
-
* @param model - The resolved model id.
|
|
154
|
-
* @param maxTokens - The resolved output-token limit.
|
|
155
|
-
* @param useStream - Whether to request an SSE stream.
|
|
156
|
-
* @returns The JSON request body.
|
|
157
|
-
*/
|
|
158
143
|
private buildChatCompletionsBody(
|
|
159
144
|
params: ChatParams,
|
|
160
145
|
model: string,
|
|
@@ -164,40 +149,14 @@ class OpenaiAIProvider implements AIProvider {
|
|
|
164
149
|
const messages = this.formatMessages(params.messages, params.system)
|
|
165
150
|
|
|
166
151
|
const body: Record<string, unknown> = {
|
|
167
|
-
// BYO / provider-native tunables (reasoning_effort, enable_thinking, …).
|
|
168
|
-
// Spread FIRST so the structural fields below (model, messages,
|
|
169
|
-
// max_completion_tokens, tools, stream) always win and cannot be broken.
|
|
170
152
|
...(params.extraBody ?? {}),
|
|
171
|
-
// Abuse attribution (see ChatParams.endUserId). OpenAI DEPRECATED `user`
|
|
172
|
-
// and replaced it with `safety_identifier` for exactly this purpose —
|
|
173
|
-
// their guidance is a hashed, stable per-user string, which is what
|
|
174
|
-
// endUserId already is. Sibling OpenAI-compatible bonds keep `user`: it is
|
|
175
|
-
// the field they actually implement, verified accepted by deepseek, xai,
|
|
176
|
-
// moonshot, alibaba and zhipu against their live APIs.
|
|
177
|
-
//
|
|
178
|
-
// This bond is also pointed at arbitrary BYO OpenAI-compatible endpoints
|
|
179
|
-
// (molecule-dev's custom providers), so the unknown-field question matters
|
|
180
|
-
// here: those same five providers all returned 200 when sent an unknown
|
|
181
|
-
// `safety_identifier`, i.e. tolerating unknown body params is the norm.
|
|
182
|
-
// A BYO endpoint therefore ignores this rather than rejecting it — and BYO
|
|
183
|
-
// runs on the user's OWN key, where attribution protects nothing of ours.
|
|
184
153
|
...(params.endUserId ? { safety_identifier: params.endUserId } : {}),
|
|
185
154
|
model,
|
|
186
155
|
messages,
|
|
187
|
-
// `max_completion_tokens`, NOT `max_tokens`. Every GPT-5-family model
|
|
188
|
-
// hard-rejects the latter — "Unsupported parameter: 'max_tokens' is not
|
|
189
|
-
// supported with this model" (400) — and the catalog's OpenAI entries are
|
|
190
|
-
// all gpt-5.6-*, so this bond could not call any of its own models. The
|
|
191
|
-
// newer name is accepted by the older generations too (verified 200 on
|
|
192
|
-
// gpt-4o-mini and gpt-3.5-turbo), so there is no need to branch per model.
|
|
193
156
|
max_completion_tokens: maxTokens,
|
|
194
157
|
}
|
|
195
158
|
if (params.temperature !== undefined) body.temperature = params.temperature
|
|
196
159
|
|
|
197
|
-
// Reasoning depth: the caller resolves the model's native reasoning_effort
|
|
198
|
-
// value (none | low | medium | high | xhigh on GPT-5.4/5.5-family) from the
|
|
199
|
-
// model catalog and passes it as `thinking.effort`. Without it, omit the
|
|
200
|
-
// param entirely (the model's own default applies — medium on gpt-5.5).
|
|
201
160
|
if (params.thinking?.effort) {
|
|
202
161
|
body.reasoning_effort = params.thinking.effort
|
|
203
162
|
}
|
|
@@ -218,77 +177,11 @@ class OpenaiAIProvider implements AIProvider {
|
|
|
218
177
|
}
|
|
219
178
|
return body
|
|
220
179
|
}
|
|
221
|
-
|
|
222
|
-
/**
|
|
223
|
-
* Send a chat request and yield streamed `ChatEvent`s.
|
|
224
|
-
*
|
|
225
|
-
* @param params - Chat parameters.
|
|
226
|
-
* @yields {ChatEvent}
|
|
227
|
-
*/
|
|
228
|
-
async *chat(params: ChatParams): AsyncIterable<ChatEvent> {
|
|
229
|
-
const model = params.model ?? this.defaultModel
|
|
230
|
-
const maxTokens = params.maxTokens ?? this.maxTokens
|
|
231
|
-
const useStream = params.stream !== false
|
|
232
|
-
const isResponses = this.api === 'responses'
|
|
233
|
-
|
|
234
|
-
const body = isResponses
|
|
235
|
-
? buildResponsesBody(params, model, maxTokens, useStream)
|
|
236
|
-
: this.buildChatCompletionsBody(params, model, maxTokens, useStream)
|
|
237
|
-
const path = isResponses ? '/v1/responses' : '/v1/chat/completions'
|
|
238
|
-
|
|
239
|
-
// Per-call correlation (see ChatParams.requestId): a header rather than the
|
|
240
|
-
// body's `user`, which is the stable per-account abuse-attribution value.
|
|
241
|
-
const requestId = params.requestId ?? randomUUID()
|
|
242
|
-
const call: OpenaiCallContext = {
|
|
243
|
-
meta: { requestId, endpoint: `${this.baseUrl}${path}` },
|
|
244
|
-
endUserId: params.endUserId,
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
const headers: Record<string, string> = {
|
|
248
|
-
'Content-Type': 'application/json',
|
|
249
|
-
// Omitted for a keyless custom endpoint (a self-hosted / local server).
|
|
250
|
-
...(this.apiKey ? { Authorization: `Bearer ${this.apiKey}` } : {}),
|
|
251
|
-
'X-Mol-Request-Id': requestId,
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
const DEFAULT_TIMEOUT_MS = 5 * 60 * 1000
|
|
255
|
-
const signal = params.signal ?? AbortSignal.timeout(DEFAULT_TIMEOUT_MS)
|
|
256
|
-
|
|
257
|
-
const MAX_RETRIES = 3
|
|
258
|
-
let response: Response | null = null
|
|
259
|
-
let rejectedBody: string | undefined
|
|
260
|
-
for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
|
|
261
|
-
rejectedBody = undefined
|
|
262
|
-
response = await fetch(`${this.baseUrl}${path}`, {
|
|
263
|
-
method: 'POST',
|
|
264
|
-
headers,
|
|
265
|
-
body: JSON.stringify(body),
|
|
266
|
-
signal,
|
|
267
|
-
})
|
|
268
|
-
|
|
269
|
-
if (response.status === 429 || response.status === 503) {
|
|
270
|
-
// Read the body once: an unread body pins its connection, and only the
|
|
271
|
-
// body says whether a 429 is a rate limit or an exhausted quota.
|
|
272
|
-
try {
|
|
273
|
-
rejectedBody = await response.text()
|
|
274
|
-
} catch (error) {
|
|
275
|
-
// Caller cancellation still ends the request; any other read failure
|
|
276
|
-
// only loses the reason, so the body counts as read and the retry
|
|
277
|
-
// policy goes on (the same guard the moonshot bond applies).
|
|
278
|
-
signal.throwIfAborted()
|
|
279
|
-
logger.debug('OpenAI rejected response body could not be read', { error })
|
|
280
|
-
rejectedBody = ''
|
|
281
|
-
}
|
|
282
|
-
const upstreamError = parseUpstreamError(rejectedBody)
|
|
283
|
-
// Billing, spend, and quota failures do not recover by waiting: a
|
|
284
|
-
// bounded retry budget fails at once instead of burning its retries.
|
|
285
|
-
const quotaExhausted =
|
|
286
|
-
this.quotaSemantics === 'openai' && isQuotaExhausted(response.status, upstreamError)
|
|
287
|
-
const willRetry = !quotaExhausted && attempt < MAX_RETRIES
|
|
288
|
-
// Retry-After is equally-valid as delta-seconds or an HTTP-date;
|
|
289
|
-
// … truncated — the full declaration is in node_modules/@molecule/api-ai-openai/dist/provider.d.ts
|
|
180
|
+
// … truncated — 7 more members: chat, formatMessages, formatBlock, formatTools, parseNonStreamingResponse, parseStreamingResponse, processSSELines; the full declaration is in node_modules/@molecule/api-ai-openai/dist/provider.d.ts
|
|
290
181
|
```
|
|
291
182
|
|
|
183
|
+
_(Member comments are left out to fit; the full declaration is in `node_modules/@molecule/api-ai-openai/dist/provider.d.ts`.)_
|
|
184
|
+
|
|
292
185
|
### Functions
|
|
293
186
|
|
|
294
187
|
#### `createProvider(config)`
|
|
@@ -344,7 +237,7 @@ export function setupAiOpenai(): void {
|
|
|
344
237
|
|
|
345
238
|
Peer dependencies:
|
|
346
239
|
|
|
347
|
-
- `@molecule/api-ai` ^1.0
|
|
240
|
+
- `@molecule/api-ai` ^1.6.0
|
|
348
241
|
- `@molecule/api-bond` ^1.0.1
|
|
349
242
|
- `@molecule/api-secrets` ^1.0.1
|
|
350
243
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@molecule/api-ai-openai",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.2",
|
|
4
4
|
"description": "",
|
|
5
5
|
"homepage": "https://www.molecule.dev/packages/api-ai-openai",
|
|
6
6
|
"type": "module",
|
|
@@ -33,14 +33,14 @@
|
|
|
33
33
|
"directory": "packages/api/bonds/ai/openai"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@molecule/api-ai": "1.
|
|
36
|
+
"@molecule/api-ai": "1.6.0",
|
|
37
37
|
"@molecule/api-bond": "1.0.4",
|
|
38
38
|
"@types/node": "26.6.5",
|
|
39
39
|
"typescript": "6.0.3",
|
|
40
40
|
"vitest": "4.1.12"
|
|
41
41
|
},
|
|
42
42
|
"peerDependencies": {
|
|
43
|
-
"@molecule/api-ai": "^1.0
|
|
43
|
+
"@molecule/api-ai": "^1.6.0",
|
|
44
44
|
"@molecule/api-bond": "^1.0.1",
|
|
45
45
|
"@molecule/api-secrets": "^1.0.1"
|
|
46
46
|
},
|