@molecule/api-ai-openai 1.0.1 → 1.1.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/dist/provider.d.ts +18 -1
- package/dist/provider.d.ts.map +1 -1
- package/dist/provider.js +78 -13
- package/package.json +2 -2
package/dist/provider.d.ts
CHANGED
|
@@ -31,9 +31,26 @@ export declare class OpenaiAIProvider implements AIProvider {
|
|
|
31
31
|
/**
|
|
32
32
|
* Convert internal `ChatMessage` objects to the OpenAI message format.
|
|
33
33
|
* The system prompt becomes a leading `{role:'system'}` message.
|
|
34
|
+
*
|
|
35
|
+
* `tool_use` and `tool_result` blocks are serialized STRUCTURALLY — a
|
|
36
|
+
* `tool_use` becomes an assistant message carrying `tool_calls`, a
|
|
37
|
+
* `tool_result` becomes its own `{role:'tool', tool_call_id, content}`
|
|
38
|
+
* message — exactly as the Chat Completions API requires and as the sibling
|
|
39
|
+
* OpenAI-compatible bonds (minimax/deepseek) already do. They used to be
|
|
40
|
+
* flattened to placeholder text (`[tool_result for <id>]`), which silently
|
|
41
|
+
* dropped every tool result: a multi-turn agentic loop fed the model empty
|
|
42
|
+
* placeholders, so at temperature 0 it re-emitted the same opening tool call
|
|
43
|
+
* forever and never finalized (observed 0/8 on gpt-5.6-luna in the
|
|
44
|
+
* starting-point selftest — the failure was this serialization, NOT the
|
|
45
|
+
* model). Single-shot text/vision requests are unaffected: they never carry
|
|
46
|
+
* these block types.
|
|
34
47
|
*/
|
|
35
48
|
private formatMessages;
|
|
36
|
-
/**
|
|
49
|
+
/**
|
|
50
|
+
* Map a generic content-part `ContentBlock` (text/image/document/audio/video)
|
|
51
|
+
* to OpenAI's content-part shape. `tool_use`/`tool_result` are handled
|
|
52
|
+
* structurally in {@link formatMessages} and never reach here.
|
|
53
|
+
*/
|
|
37
54
|
private formatBlock;
|
|
38
55
|
/** Convert internal `AITool` definitions to OpenAI's `tools` array. */
|
|
39
56
|
private formatTools;
|
package/dist/provider.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAKH,OAAO,cAAc,CAAA;AAErB,OAAO,KAAK,EACV,UAAU,EAGV,SAAS,EAET,UAAU,EAEX,MAAM,kBAAkB,CAAA;AAGzB,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAc9C;;GAEG;AACH,qBAAa,gBAAiB,YAAW,UAAU;IACjD,QAAQ,CAAC,IAAI,YAAW;IACxB,OAAO,CAAC,MAAM,CAAQ;IACtB,OAAO,CAAC,YAAY,CAAQ;IAC5B,OAAO,CAAC,SAAS,CAAQ;IACzB,OAAO,CAAC,OAAO,CAAQ;IACvB,OAAO,CAAC,WAAW,CAAC,CAAqB;gBAE7B,MAAM,GAAE,YAAiB;
|
|
1
|
+
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAKH,OAAO,cAAc,CAAA;AAErB,OAAO,KAAK,EACV,UAAU,EAGV,SAAS,EAET,UAAU,EAEX,MAAM,kBAAkB,CAAA;AAGzB,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAc9C;;GAEG;AACH,qBAAa,gBAAiB,YAAW,UAAU;IACjD,QAAQ,CAAC,IAAI,YAAW;IACxB,OAAO,CAAC,MAAM,CAAQ;IACtB,OAAO,CAAC,YAAY,CAAQ;IAC5B,OAAO,CAAC,SAAS,CAAQ;IACzB,OAAO,CAAC,OAAO,CAAQ;IACvB,OAAO,CAAC,WAAW,CAAC,CAAqB;gBAE7B,MAAM,GAAE,YAAiB;IAqBrC;;;;;OAKG;IACI,IAAI,CAAC,MAAM,EAAE,UAAU,GAAG,aAAa,CAAC,SAAS,CAAC;IA2KzD;;;;;;;;;;;;;;;;OAgBG;IACH,OAAO,CAAC,cAAc;IA+DtB;;;;OAIG;IACH,OAAO,CAAC,WAAW;IA6BnB,uEAAuE;IACvE,OAAO,CAAC,WAAW;IAWnB;;;;;OAKG;IACH,OAAO,CAAE,yBAAyB;IA8ClC;;;;;OAKG;YACY,sBAAsB;IA4ErC;;;;;;OAMG;IACH,OAAO,CAAE,eAAe;CA0EzB;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,MAAM,CAAC,EAAE,YAAY,GAAG,UAAU,CAEhE"}
|
package/dist/provider.js
CHANGED
|
@@ -32,8 +32,11 @@ export class OpenaiAIProvider {
|
|
|
32
32
|
// Fail fast with an actionable local error rather than a cryptic 401 on the
|
|
33
33
|
// first request. The default `provider` export constructs lazily on first
|
|
34
34
|
// use (see index.ts), so this surfaces the moment the provider is actually
|
|
35
|
-
// used, not at bond/module-load time.
|
|
36
|
-
|
|
35
|
+
// used, not at bond/module-load time. A key is required only for the
|
|
36
|
+
// official endpoint — a custom baseUrl may target a keyless self-hosted /
|
|
37
|
+
// local server (Ollama, LM Studio), where the auth header is omitted below.
|
|
38
|
+
const usingCustomEndpoint = Boolean(config.baseUrl ?? process.env.OPENAI_BASE_URL);
|
|
39
|
+
if (!this.apiKey && !usingCustomEndpoint) {
|
|
37
40
|
throw new Error('OPENAI_API_KEY is not set. Add it to your environment to use the OpenAI AI provider.');
|
|
38
41
|
}
|
|
39
42
|
}
|
|
@@ -48,6 +51,10 @@ export class OpenaiAIProvider {
|
|
|
48
51
|
const maxTokens = params.maxTokens ?? this.maxTokens;
|
|
49
52
|
const messages = this.formatMessages(params.messages, params.system);
|
|
50
53
|
const body = {
|
|
54
|
+
// BYO / provider-native tunables (reasoning_effort, enable_thinking, …).
|
|
55
|
+
// Spread FIRST so the structural fields below (model, messages,
|
|
56
|
+
// max_completion_tokens, tools, stream) always win and cannot be broken.
|
|
57
|
+
...(params.extraBody ?? {}),
|
|
51
58
|
// Abuse attribution (see ChatParams.endUserId). OpenAI DEPRECATED `user`
|
|
52
59
|
// and replaced it with `safety_identifier` for exactly this purpose —
|
|
53
60
|
// their guidance is a hashed, stable per-user string, which is what
|
|
@@ -98,7 +105,8 @@ export class OpenaiAIProvider {
|
|
|
98
105
|
}
|
|
99
106
|
const headers = {
|
|
100
107
|
'Content-Type': 'application/json',
|
|
101
|
-
|
|
108
|
+
// Omitted for a keyless custom endpoint (a self-hosted / local server).
|
|
109
|
+
...(this.apiKey ? { Authorization: `Bearer ${this.apiKey}` } : {}),
|
|
102
110
|
};
|
|
103
111
|
const DEFAULT_TIMEOUT_MS = 5 * 60 * 1000;
|
|
104
112
|
const signal = params.signal ?? AbortSignal.timeout(DEFAULT_TIMEOUT_MS);
|
|
@@ -200,6 +208,19 @@ export class OpenaiAIProvider {
|
|
|
200
208
|
/**
|
|
201
209
|
* Convert internal `ChatMessage` objects to the OpenAI message format.
|
|
202
210
|
* The system prompt becomes a leading `{role:'system'}` message.
|
|
211
|
+
*
|
|
212
|
+
* `tool_use` and `tool_result` blocks are serialized STRUCTURALLY — a
|
|
213
|
+
* `tool_use` becomes an assistant message carrying `tool_calls`, a
|
|
214
|
+
* `tool_result` becomes its own `{role:'tool', tool_call_id, content}`
|
|
215
|
+
* message — exactly as the Chat Completions API requires and as the sibling
|
|
216
|
+
* OpenAI-compatible bonds (minimax/deepseek) already do. They used to be
|
|
217
|
+
* flattened to placeholder text (`[tool_result for <id>]`), which silently
|
|
218
|
+
* dropped every tool result: a multi-turn agentic loop fed the model empty
|
|
219
|
+
* placeholders, so at temperature 0 it re-emitted the same opening tool call
|
|
220
|
+
* forever and never finalized (observed 0/8 on gpt-5.6-luna in the
|
|
221
|
+
* starting-point selftest — the failure was this serialization, NOT the
|
|
222
|
+
* model). Single-shot text/vision requests are unaffected: they never carry
|
|
223
|
+
* these block types.
|
|
203
224
|
*/
|
|
204
225
|
formatMessages(messages, systemPrompt) {
|
|
205
226
|
const out = [];
|
|
@@ -210,14 +231,62 @@ export class OpenaiAIProvider {
|
|
|
210
231
|
out.push({ role: 'system', content: typeof m.content === 'string' ? m.content : '' });
|
|
211
232
|
continue;
|
|
212
233
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
234
|
+
if (typeof m.content === 'string') {
|
|
235
|
+
out.push({ role: m.role, content: m.content });
|
|
236
|
+
continue;
|
|
237
|
+
}
|
|
238
|
+
const parts = [];
|
|
239
|
+
const toolCalls = [];
|
|
240
|
+
const toolResults = [];
|
|
241
|
+
for (const block of m.content) {
|
|
242
|
+
switch (block.type) {
|
|
243
|
+
case 'tool_use':
|
|
244
|
+
toolCalls.push({
|
|
245
|
+
id: block.id,
|
|
246
|
+
type: 'function',
|
|
247
|
+
function: { name: block.name, arguments: JSON.stringify(block.input ?? {}) },
|
|
248
|
+
});
|
|
249
|
+
break;
|
|
250
|
+
case 'tool_result':
|
|
251
|
+
toolResults.push({
|
|
252
|
+
role: 'tool',
|
|
253
|
+
tool_call_id: block.tool_use_id,
|
|
254
|
+
content: typeof block.content === 'string' ? block.content : JSON.stringify(block.content),
|
|
255
|
+
});
|
|
256
|
+
break;
|
|
257
|
+
default:
|
|
258
|
+
parts.push(this.formatBlock(block));
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
// Tool results are standalone `role:'tool'` messages. An assistant turn
|
|
262
|
+
// that made tool calls carries them on the message; a plain content
|
|
263
|
+
// message (text/image/…) is emitted as-is. These are mutually exclusive
|
|
264
|
+
// in practice (assistant emits tool_use, the next user turn the results).
|
|
265
|
+
if (toolResults.length > 0) {
|
|
266
|
+
for (const tr of toolResults)
|
|
267
|
+
out.push(tr);
|
|
268
|
+
}
|
|
269
|
+
else if (toolCalls.length > 0) {
|
|
270
|
+
out.push({
|
|
271
|
+
role: 'assistant',
|
|
272
|
+
content: parts.length > 0 ? parts : null,
|
|
273
|
+
tool_calls: toolCalls,
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
else {
|
|
277
|
+
out.push({
|
|
278
|
+
role: m.role,
|
|
279
|
+
content: parts.length === 1 && parts[0].type === 'text' ? parts[0].text : parts,
|
|
280
|
+
});
|
|
281
|
+
}
|
|
217
282
|
}
|
|
218
283
|
return out;
|
|
219
284
|
}
|
|
220
|
-
/**
|
|
285
|
+
/**
|
|
286
|
+
* Map a generic content-part `ContentBlock` (text/image/document/audio/video)
|
|
287
|
+
* to OpenAI's content-part shape. `tool_use`/`tool_result` are handled
|
|
288
|
+
* structurally in {@link formatMessages} and never reach here.
|
|
289
|
+
*/
|
|
221
290
|
formatBlock(block) {
|
|
222
291
|
switch (block.type) {
|
|
223
292
|
case 'text':
|
|
@@ -242,12 +311,8 @@ export class OpenaiAIProvider {
|
|
|
242
311
|
type: 'text',
|
|
243
312
|
text: `[Video attachment (${block.mediaType}) — not supported by this provider]`,
|
|
244
313
|
};
|
|
245
|
-
case 'tool_use':
|
|
246
|
-
return { type: 'text', text: `[tool_use ${block.name}]` };
|
|
247
|
-
case 'tool_result':
|
|
248
|
-
return { type: 'text', text: `[tool_result for ${block.tool_use_id}]` };
|
|
249
314
|
default:
|
|
250
|
-
return
|
|
315
|
+
return { type: 'text', text: '' };
|
|
251
316
|
}
|
|
252
317
|
}
|
|
253
318
|
/** Convert internal `AITool` definitions to OpenAI's `tools` array. */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@molecule/api-ai-openai",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"directory": "packages/api/bonds/ai/openai"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
|
-
"@molecule/api-ai": "1.0
|
|
35
|
+
"@molecule/api-ai": "1.1.0",
|
|
36
36
|
"@molecule/api-bond": "1.0.1",
|
|
37
37
|
"@types/node": "26.1.2",
|
|
38
38
|
"typescript": "6.0.3",
|