@molecule/api-ai-openai 1.0.1 → 1.0.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/dist/provider.d.ts +18 -1
- package/dist/provider.d.ts.map +1 -1
- package/dist/provider.js +67 -10
- package/package.json +1 -1
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;IAkBrC;;;;;OAKG;IACI,IAAI,CAAC,MAAM,EAAE,UAAU,GAAG,aAAa,CAAC,SAAS,CAAC;IAsKzD
|
|
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;IAkBrC;;;;;OAKG;IACI,IAAI,CAAC,MAAM,EAAE,UAAU,GAAG,aAAa,CAAC,SAAS,CAAC;IAsKzD;;;;;;;;;;;;;;;;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
|
@@ -200,6 +200,19 @@ export class OpenaiAIProvider {
|
|
|
200
200
|
/**
|
|
201
201
|
* Convert internal `ChatMessage` objects to the OpenAI message format.
|
|
202
202
|
* The system prompt becomes a leading `{role:'system'}` message.
|
|
203
|
+
*
|
|
204
|
+
* `tool_use` and `tool_result` blocks are serialized STRUCTURALLY — a
|
|
205
|
+
* `tool_use` becomes an assistant message carrying `tool_calls`, a
|
|
206
|
+
* `tool_result` becomes its own `{role:'tool', tool_call_id, content}`
|
|
207
|
+
* message — exactly as the Chat Completions API requires and as the sibling
|
|
208
|
+
* OpenAI-compatible bonds (minimax/deepseek) already do. They used to be
|
|
209
|
+
* flattened to placeholder text (`[tool_result for <id>]`), which silently
|
|
210
|
+
* dropped every tool result: a multi-turn agentic loop fed the model empty
|
|
211
|
+
* placeholders, so at temperature 0 it re-emitted the same opening tool call
|
|
212
|
+
* forever and never finalized (observed 0/8 on gpt-5.6-luna in the
|
|
213
|
+
* starting-point selftest — the failure was this serialization, NOT the
|
|
214
|
+
* model). Single-shot text/vision requests are unaffected: they never carry
|
|
215
|
+
* these block types.
|
|
203
216
|
*/
|
|
204
217
|
formatMessages(messages, systemPrompt) {
|
|
205
218
|
const out = [];
|
|
@@ -210,14 +223,62 @@ export class OpenaiAIProvider {
|
|
|
210
223
|
out.push({ role: 'system', content: typeof m.content === 'string' ? m.content : '' });
|
|
211
224
|
continue;
|
|
212
225
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
226
|
+
if (typeof m.content === 'string') {
|
|
227
|
+
out.push({ role: m.role, content: m.content });
|
|
228
|
+
continue;
|
|
229
|
+
}
|
|
230
|
+
const parts = [];
|
|
231
|
+
const toolCalls = [];
|
|
232
|
+
const toolResults = [];
|
|
233
|
+
for (const block of m.content) {
|
|
234
|
+
switch (block.type) {
|
|
235
|
+
case 'tool_use':
|
|
236
|
+
toolCalls.push({
|
|
237
|
+
id: block.id,
|
|
238
|
+
type: 'function',
|
|
239
|
+
function: { name: block.name, arguments: JSON.stringify(block.input ?? {}) },
|
|
240
|
+
});
|
|
241
|
+
break;
|
|
242
|
+
case 'tool_result':
|
|
243
|
+
toolResults.push({
|
|
244
|
+
role: 'tool',
|
|
245
|
+
tool_call_id: block.tool_use_id,
|
|
246
|
+
content: typeof block.content === 'string' ? block.content : JSON.stringify(block.content),
|
|
247
|
+
});
|
|
248
|
+
break;
|
|
249
|
+
default:
|
|
250
|
+
parts.push(this.formatBlock(block));
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
// Tool results are standalone `role:'tool'` messages. An assistant turn
|
|
254
|
+
// that made tool calls carries them on the message; a plain content
|
|
255
|
+
// message (text/image/…) is emitted as-is. These are mutually exclusive
|
|
256
|
+
// in practice (assistant emits tool_use, the next user turn the results).
|
|
257
|
+
if (toolResults.length > 0) {
|
|
258
|
+
for (const tr of toolResults)
|
|
259
|
+
out.push(tr);
|
|
260
|
+
}
|
|
261
|
+
else if (toolCalls.length > 0) {
|
|
262
|
+
out.push({
|
|
263
|
+
role: 'assistant',
|
|
264
|
+
content: parts.length > 0 ? parts : null,
|
|
265
|
+
tool_calls: toolCalls,
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
else {
|
|
269
|
+
out.push({
|
|
270
|
+
role: m.role,
|
|
271
|
+
content: parts.length === 1 && parts[0].type === 'text' ? parts[0].text : parts,
|
|
272
|
+
});
|
|
273
|
+
}
|
|
217
274
|
}
|
|
218
275
|
return out;
|
|
219
276
|
}
|
|
220
|
-
/**
|
|
277
|
+
/**
|
|
278
|
+
* Map a generic content-part `ContentBlock` (text/image/document/audio/video)
|
|
279
|
+
* to OpenAI's content-part shape. `tool_use`/`tool_result` are handled
|
|
280
|
+
* structurally in {@link formatMessages} and never reach here.
|
|
281
|
+
*/
|
|
221
282
|
formatBlock(block) {
|
|
222
283
|
switch (block.type) {
|
|
223
284
|
case 'text':
|
|
@@ -242,12 +303,8 @@ export class OpenaiAIProvider {
|
|
|
242
303
|
type: 'text',
|
|
243
304
|
text: `[Video attachment (${block.mediaType}) — not supported by this provider]`,
|
|
244
305
|
};
|
|
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
306
|
default:
|
|
250
|
-
return
|
|
307
|
+
return { type: 'text', text: '' };
|
|
251
308
|
}
|
|
252
309
|
}
|
|
253
310
|
/** Convert internal `AITool` definitions to OpenAI's `tools` array. */
|