@molecule/api-ai-local 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.
@@ -36,9 +36,23 @@ export declare class LocalAIProvider implements AIProvider {
36
36
  /**
37
37
  * Convert internal `ChatMessage` objects to the OpenAI message format.
38
38
  * The system prompt becomes a leading `{role:'system'}` message.
39
+ *
40
+ * `tool_use`/`tool_result` blocks are serialized STRUCTURALLY (assistant
41
+ * `tool_calls` + standalone `{role:'tool'}` messages), as the OpenAI-
42
+ * compatible chat API requires. They used to flatten to placeholder text
43
+ * (`[tool_result for <id>]`), which silently dropped every tool result and
44
+ * made multi-turn agentic tool loops impossible — a local/BYO model looped on
45
+ * its first tool call and never finalized. See the same fix + regression test
46
+ * in `@molecule/api-ai-openai` (this bond is that provider with a configurable
47
+ * baseUrl and optional key). Single-shot text/vision requests never carry
48
+ * these block types and are unaffected.
39
49
  */
40
50
  private formatMessages;
41
- /** Map a generic `ContentBlock` to OpenAI's content-part shape. */
51
+ /**
52
+ * Map a generic content-part `ContentBlock` (text/image/document/audio/video)
53
+ * to OpenAI's content-part shape. `tool_use`/`tool_result` are handled
54
+ * structurally in {@link formatMessages} and never reach here.
55
+ */
42
56
  private formatBlock;
43
57
  /** Convert internal `AITool` definitions to OpenAI's `tools` array. */
44
58
  private formatTools;
@@ -1 +1 @@
1
- {"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAKH,OAAO,cAAc,CAAA;AAErB,OAAO,KAAK,EACV,UAAU,EAGV,SAAS,EAET,UAAU,EAEX,MAAM,kBAAkB,CAAA;AAGzB,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAqB7C;;;;GAIG;AACH,qBAAa,eAAgB,YAAW,UAAU;IAChD,QAAQ,CAAC,IAAI,WAAU;IACvB,OAAO,CAAC,MAAM,CAAQ;IACtB,OAAO,CA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;;;;;;;;OAaG;IACH,OAAO,CAAC,cAAc;IA2DtB;;;;OAIG;IACH,OAAO,CAAC,WAAW;IA6BnB,uEAAuE;IACvE,OAAO,CAAC,WAAW;IAWnB;;;;;OAKG;IACH,OAAO,CAAE,yBAAyB;IA8ClC;;;;;OAKG;YACY,sBAAsB;IA2ErC;;;;;;OAMG;IACH,OAAO,CAAE,eAAe;IA+DxB;;;;;;;;;;OAUG;IACH,OAAO,CAAE,qBAAqB;CAqC/B;AAED;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAAC,MAAM,CAAC,EAAE,WAAW,GAAG,UAAU,CAE/D"}
package/dist/provider.js CHANGED
@@ -225,6 +225,16 @@ export class LocalAIProvider {
225
225
  /**
226
226
  * Convert internal `ChatMessage` objects to the OpenAI message format.
227
227
  * The system prompt becomes a leading `{role:'system'}` message.
228
+ *
229
+ * `tool_use`/`tool_result` blocks are serialized STRUCTURALLY (assistant
230
+ * `tool_calls` + standalone `{role:'tool'}` messages), as the OpenAI-
231
+ * compatible chat API requires. They used to flatten to placeholder text
232
+ * (`[tool_result for <id>]`), which silently dropped every tool result and
233
+ * made multi-turn agentic tool loops impossible — a local/BYO model looped on
234
+ * its first tool call and never finalized. See the same fix + regression test
235
+ * in `@molecule/api-ai-openai` (this bond is that provider with a configurable
236
+ * baseUrl and optional key). Single-shot text/vision requests never carry
237
+ * these block types and are unaffected.
228
238
  */
229
239
  formatMessages(messages, systemPrompt) {
230
240
  const out = [];
@@ -235,14 +245,58 @@ export class LocalAIProvider {
235
245
  out.push({ role: 'system', content: typeof m.content === 'string' ? m.content : '' });
236
246
  continue;
237
247
  }
238
- const content = typeof m.content === 'string'
239
- ? m.content
240
- : m.content.map((b) => this.formatBlock(b));
241
- out.push({ role: m.role, content });
248
+ if (typeof m.content === 'string') {
249
+ out.push({ role: m.role, content: m.content });
250
+ continue;
251
+ }
252
+ const parts = [];
253
+ const toolCalls = [];
254
+ const toolResults = [];
255
+ for (const block of m.content) {
256
+ switch (block.type) {
257
+ case 'tool_use':
258
+ toolCalls.push({
259
+ id: block.id,
260
+ type: 'function',
261
+ function: { name: block.name, arguments: JSON.stringify(block.input ?? {}) },
262
+ });
263
+ break;
264
+ case 'tool_result':
265
+ toolResults.push({
266
+ role: 'tool',
267
+ tool_call_id: block.tool_use_id,
268
+ content: typeof block.content === 'string' ? block.content : JSON.stringify(block.content),
269
+ });
270
+ break;
271
+ default:
272
+ parts.push(this.formatBlock(block));
273
+ }
274
+ }
275
+ if (toolResults.length > 0) {
276
+ for (const tr of toolResults)
277
+ out.push(tr);
278
+ }
279
+ else if (toolCalls.length > 0) {
280
+ out.push({
281
+ role: 'assistant',
282
+ content: parts.length > 0 ? parts : null,
283
+ tool_calls: toolCalls,
284
+ });
285
+ }
286
+ else {
287
+ out.push({
288
+ role: m.role,
289
+ content: parts.length === 1 && parts[0].type === 'text' ? parts[0].text : parts,
290
+ });
291
+ }
242
292
  }
243
293
  return out;
244
294
  }
245
- /** Map a generic `ContentBlock` to OpenAI's content-part shape. */
295
+ /**
296
+ * Map a generic content-part `ContentBlock` (text/image/document/audio/video)
297
+ * to OpenAI's content-part shape. `tool_use`/`tool_result` are handled
298
+ * structurally in {@link formatMessages} and never reach here.
299
+ */
246
300
  formatBlock(block) {
247
301
  switch (block.type) {
248
302
  case 'text':
@@ -267,12 +321,8 @@ export class LocalAIProvider {
267
321
  type: 'text',
268
322
  text: `[Video attachment (${block.mediaType}) — not supported by this provider]`,
269
323
  };
270
- case 'tool_use':
271
- return { type: 'text', text: `[tool_use ${block.name}]` };
272
- case 'tool_result':
273
- return { type: 'text', text: `[tool_result for ${block.tool_use_id}]` };
274
324
  default:
275
- return block;
325
+ return { type: 'text', text: '' };
276
326
  }
277
327
  }
278
328
  /** 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-local",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "AIProvider bond for any OpenAI-compatible local inference server (Ollama, LM Studio, llama.cpp, vLLM) — configurable base URL, keyless by default, streamed via raw fetch.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",