@molecule/api-ai-openai 1.4.0 → 1.4.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.
@@ -1 +1 @@
1
- {"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAKH,OAAO,cAAc,CAAA;AAErB,OAAO,KAAK,EACV,UAAU,EAGV,SAAS,EAET,UAAU,EAEX,MAAM,kBAAkB,CAAA;AAQzB,OAAO,KAAK,EAAa,YAAY,EAAE,MAAM,YAAY,CAAA;AAkCzD;;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;IACzC,OAAO,CAAC,GAAG,CAAW;gBAEV,MAAM,GAAE,YAAiB;IAsBrC;;;;;;;;OAQG;IACH,OAAO,CAAC,wBAAwB;IAgEhC;;;;;OAKG;IACI,IAAI,CAAC,MAAM,EAAE,UAAU,GAAG,aAAa,CAAC,SAAS,CAAC;IAyHzD;;;;;;;;;;;;;;;;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;CAqEzB;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,MAAM,CAAC,EAAE,YAAY,GAAG,UAAU,CAEhE"}
1
+ {"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAKH,OAAO,cAAc,CAAA;AAErB,OAAO,KAAK,EACV,UAAU,EAGV,SAAS,EAET,UAAU,EAEX,MAAM,kBAAkB,CAAA;AAQzB,OAAO,KAAK,EAAa,YAAY,EAAE,MAAM,YAAY,CAAA;AAkCzD;;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;IACzC,OAAO,CAAC,GAAG,CAAW;gBAEV,MAAM,GAAE,YAAiB;IAsBrC;;;;;;;;OAQG;IACH,OAAO,CAAC,wBAAwB;IAgEhC;;;;;OAKG;IACI,IAAI,CAAC,MAAM,EAAE,UAAU,GAAG,aAAa,CAAC,SAAS,CAAC;IA6HzD;;;;;;;;;;;;;;;;OAgBG;IACH,OAAO,CAAC,cAAc;IAmEtB;;;;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;CAqEzB;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,MAAM,CAAC,EAAE,YAAY,GAAG,UAAU,CAEhE"}
package/dist/provider.js CHANGED
@@ -240,10 +240,14 @@ export class OpenaiAIProvider {
240
240
  }
241
241
  if (!useStream) {
242
242
  const data = (await response.json());
243
- yield* isResponses ? parseResponsesNonStreaming(data) : this.parseNonStreamingResponse(data);
243
+ yield* isResponses
244
+ ? parseResponsesNonStreaming(data, model)
245
+ : this.parseNonStreamingResponse(data);
244
246
  return;
245
247
  }
246
- yield* isResponses ? parseResponsesStream(response) : this.parseStreamingResponse(response);
248
+ yield* isResponses
249
+ ? parseResponsesStream(response, model)
250
+ : this.parseStreamingResponse(response);
247
251
  }
248
252
  /**
249
253
  * Convert internal `ChatMessage` objects to the OpenAI message format.
@@ -305,6 +309,11 @@ export class OpenaiAIProvider {
305
309
  if (toolResults.length > 0) {
306
310
  for (const tr of toolResults)
307
311
  out.push(tr);
312
+ // Non-tool parts of the SAME message (an image riding a tool result — the
313
+ // screenshot tool) must not be dropped: Chat Completions accepts a user
314
+ // message after the tool messages, so emit them instead of losing them.
315
+ if (parts.length > 0)
316
+ out.push({ role: m.role, content: parts });
308
317
  }
309
318
  else if (toolCalls.length > 0) {
310
319
  out.push({
@@ -11,10 +11,16 @@
11
11
  * server-side tools (`web_search`). Verified live 2026-09-24.
12
12
  *
13
13
  * Requests are stateless (`store: false`): the full conversation is sent each
14
- * time, as with chat/completions. Prior tool calls are replayed as
15
- * `function_call` items WITHOUT an item `id` — an id makes the API look for the
16
- * stored reasoning item that preceded the call, which a stateless request does
17
- * not have.
14
+ * time, as with chat/completions. A prior tool call is replayed as a
15
+ * `function_call` item rebuilt from its `tool_use` block, preceded by the
16
+ * reasoning and `web_search_call` items the model produced before it. Those
17
+ * travel in the tool call's `signature` (the provider-opaque replay token
18
+ * callers persist with each `tool_use`), and reasoning is requested in its
19
+ * encrypted form so it survives without server-side storage. Without them the
20
+ * model cannot see that it already searched: after a tool result it searched
21
+ * again and re-issued the same call, turn after turn (reproduced live
22
+ * 2026-09-26 on gpt-6-luna and gpt-6-astra; replaying them ended the loop).
23
+ * Reasoning is only replayed to the model that produced it.
18
24
  *
19
25
  * @module
20
26
  */
@@ -41,12 +47,14 @@ export declare function buildResponsesBody(params: ChatParams, model: string, ma
41
47
  /**
42
48
  * Convert `ChatMessage`s to Responses API input items. Text and images become
43
49
  * message items; `tool_use` blocks become `function_call` items and
44
- * `tool_result` blocks `function_call_output` items, in block order.
50
+ * `tool_result` blocks `function_call_output` items, in block order. A tool
51
+ * call's replay items (see the module docs) go immediately before it.
45
52
  *
46
53
  * @param messages - The conversation.
54
+ * @param model - The model this request targets.
47
55
  * @returns The `input` array.
48
56
  */
49
- export declare function formatResponsesInput(messages: ChatMessage[]): Array<Record<string, unknown>>;
57
+ export declare function formatResponsesInput(messages: ChatMessage[], model: string): Array<Record<string, unknown>>;
50
58
  /**
51
59
  * Convert function tools and provider-native server tools to the Responses
52
60
  * API `tools` array.
@@ -76,13 +84,14 @@ export declare function responsesUsage(usage: ResponsesUsage | undefined | null,
76
84
  * @param data - The parsed JSON response body.
77
85
  * @yields {ChatEvent} Text, tool-use, and done events.
78
86
  */
79
- export declare function parseResponsesNonStreaming(data: Record<string, unknown>): Iterable<ChatEvent>;
87
+ export declare function parseResponsesNonStreaming(data: Record<string, unknown>, model: string): Iterable<ChatEvent>;
80
88
  /**
81
89
  * Parse a streaming SSE response from `/v1/responses`.
82
90
  *
83
91
  * @param response - The fetch Response whose body is an SSE stream.
92
+ * @param model - The model the request targeted.
84
93
  * @yields {ChatEvent} Text, tool-call progress, tool-use, keep-alive, and done events.
85
94
  */
86
- export declare function parseResponsesStream(response: Response): AsyncIterable<ChatEvent>;
95
+ export declare function parseResponsesStream(response: Response, model: string): AsyncIterable<ChatEvent>;
87
96
  export {};
88
97
  //# sourceMappingURL=responses.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"responses.d.ts","sourceRoot":"","sources":["../src/responses.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,KAAK,EACV,MAAM,EACN,SAAS,EACT,WAAW,EACX,UAAU,EAEV,UAAU,EACV,UAAU,EACX,MAAM,kBAAkB,CAAA;AAOzB,8DAA8D;AAC9D,UAAU,cAAc;IACtB,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,aAAa,CAAC,EAAE,MAAM,CAAA;IACtB,oBAAoB,CAAC,EAAE;QAAE,aAAa,CAAC,EAAE,MAAM,CAAC;QAAC,kBAAkB,CAAC,EAAE,MAAM,CAA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EAAE;QAAE,aAAa,CAAC,EAAE,MAAM,CAAC;QAAC,kBAAkB,CAAC,EAAE,MAAM,CAAA;KAAE,CAAA;CAC/E;AAED;;;;;;;;GAQG;AACH,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,UAAU,EAClB,KAAK,EAAE,MAAM,EACb,SAAS,EAAE,MAAM,EACjB,SAAS,EAAE,OAAO,GACjB,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CA6BzB;AAED;;;;;;;;;GASG;AACH,wBAAgB,oBAAoB,CAClC,QAAQ,EAAE,WAAW,EAAE,EACvB,KAAK,EAAE,MAAM,GACZ,KAAK,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAgDhC;AA6BD;;;;;;;;;;;;GAYG;AACH,wBAAgB,oBAAoB,CAClC,KAAK,EAAE,MAAM,EAAE,EACf,WAAW,EAAE,UAAU,EAAE,GACxB,KAAK,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAWhC;AAED;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAC5B,KAAK,EAAE,cAAc,GAAG,SAAS,GAAG,IAAI,EACxC,cAAc,SAAI,GACjB,UAAU,CAUZ;AAiBD;;;;;GAKG;AACH,wBAAiB,0BAA0B,CACzC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC7B,KAAK,EAAE,MAAM,GACZ,QAAQ,CAAC,SAAS,CAAC,CA2BrB;AAED;;;;;;GAMG;AACH,wBAAuB,oBAAoB,CACzC,QAAQ,EAAE,QAAQ,EAClB,KAAK,EAAE,MAAM,GACZ,aAAa,CAAC,SAAS,CAAC,CA4J1B"}
package/dist/responses.js CHANGED
@@ -11,16 +11,86 @@
11
11
  * server-side tools (`web_search`). Verified live 2026-09-24.
12
12
  *
13
13
  * Requests are stateless (`store: false`): the full conversation is sent each
14
- * time, as with chat/completions. Prior tool calls are replayed as
15
- * `function_call` items WITHOUT an item `id` — an id makes the API look for the
16
- * stored reasoning item that preceded the call, which a stateless request does
17
- * not have.
14
+ * time, as with chat/completions. A prior tool call is replayed as a
15
+ * `function_call` item rebuilt from its `tool_use` block, preceded by the
16
+ * reasoning and `web_search_call` items the model produced before it. Those
17
+ * travel in the tool call's `signature` (the provider-opaque replay token
18
+ * callers persist with each `tool_use`), and reasoning is requested in its
19
+ * encrypted form so it survives without server-side storage. Without them the
20
+ * model cannot see that it already searched: after a tool result it searched
21
+ * again and re-issued the same call, turn after turn (reproduced live
22
+ * 2026-09-26 on gpt-6-luna and gpt-6-astra; replaying them ended the loop).
23
+ * Reasoning is only replayed to the model that produced it.
18
24
  *
19
25
  * @module
20
26
  */
21
27
  import { getLogger } from '@molecule/api-bond';
22
28
  import { streamErrorEvent } from './utilities.js';
23
29
  const logger = getLogger();
30
+ /** Prefix marking a `tool_use.signature` that carries Responses API items to replay. */
31
+ const REPLAY_PREFIX = 'openai-responses-items:v1:';
32
+ /** Output item types replayed ahead of the function call they preceded. */
33
+ const REPLAYED_ITEM_TYPES = new Set(['reasoning', 'web_search_call']);
34
+ /**
35
+ * Collects the reasoning and web search items that precede each function call
36
+ * in a response, so they can ride on that call's `signature`.
37
+ */
38
+ class ReplayItems {
39
+ model;
40
+ items = [];
41
+ /**
42
+ * Start an empty collector for one response.
43
+ *
44
+ * @param model - The model producing the response (reasoning is model-bound).
45
+ */
46
+ constructor(model) {
47
+ this.model = model;
48
+ }
49
+ /**
50
+ * Record an output item if it is one that must be replayed.
51
+ *
52
+ * @param item - A completed output item.
53
+ */
54
+ add(item) {
55
+ if (REPLAYED_ITEM_TYPES.has(String(item.type)))
56
+ this.items.push(item);
57
+ }
58
+ /**
59
+ * The `signature` partial for the next function call, emptying the buffer.
60
+ *
61
+ * @returns `{ signature }` when items preceded the call, else `{}`.
62
+ */
63
+ take() {
64
+ if (this.items.length === 0)
65
+ return {};
66
+ const signature = REPLAY_PREFIX + JSON.stringify({ model: this.model, items: this.items });
67
+ this.items = [];
68
+ return { signature };
69
+ }
70
+ }
71
+ /**
72
+ * The items to replay ahead of a prior tool call, decoded from its signature.
73
+ * A signature from another provider (no prefix) yields nothing; reasoning from
74
+ * a different model is dropped, since encrypted reasoning is bound to the model
75
+ * that produced it, while its web search items are kept.
76
+ *
77
+ * @param signature - The stored `tool_use.signature`.
78
+ * @param model - The model this request targets.
79
+ * @returns The items to place before the `function_call`.
80
+ */
81
+ function replayedItems(signature, model) {
82
+ if (!signature?.startsWith(REPLAY_PREFIX))
83
+ return [];
84
+ try {
85
+ const payload = JSON.parse(signature.slice(REPLAY_PREFIX.length));
86
+ const items = Array.isArray(payload.items) ? payload.items : [];
87
+ return payload.model === model ? items : items.filter((i) => i.type !== 'reasoning');
88
+ }
89
+ catch (error) {
90
+ logger.warn('Dropping an unreadable OpenAI replay signature', { error });
91
+ return [];
92
+ }
93
+ }
24
94
  /**
25
95
  * Build the request body for `POST /v1/responses`.
26
96
  *
@@ -38,9 +108,11 @@ export function buildResponsesBody(params, model, maxTokens, useStream) {
38
108
  ...(params.extraBody ?? {}),
39
109
  ...(params.endUserId ? { safety_identifier: params.endUserId } : {}),
40
110
  model,
41
- input: formatResponsesInput(params.messages),
111
+ input: formatResponsesInput(params.messages, model),
42
112
  max_output_tokens: maxTokens,
43
113
  store: false,
114
+ // Encrypted reasoning, so it can be replayed without server-side storage.
115
+ include: ['reasoning.encrypted_content'],
44
116
  };
45
117
  if (params.system)
46
118
  body.instructions = params.system;
@@ -65,12 +137,14 @@ export function buildResponsesBody(params, model, maxTokens, useStream) {
65
137
  /**
66
138
  * Convert `ChatMessage`s to Responses API input items. Text and images become
67
139
  * message items; `tool_use` blocks become `function_call` items and
68
- * `tool_result` blocks `function_call_output` items, in block order.
140
+ * `tool_result` blocks `function_call_output` items, in block order. A tool
141
+ * call's replay items (see the module docs) go immediately before it.
69
142
  *
70
143
  * @param messages - The conversation.
144
+ * @param model - The model this request targets.
71
145
  * @returns The `input` array.
72
146
  */
73
- export function formatResponsesInput(messages) {
147
+ export function formatResponsesInput(messages, model) {
74
148
  const out = [];
75
149
  for (const m of messages) {
76
150
  if (m.role === 'system') {
@@ -94,6 +168,7 @@ export function formatResponsesInput(messages) {
94
168
  switch (block.type) {
95
169
  case 'tool_use':
96
170
  flush();
171
+ out.push(...replayedItems(block.signature, model));
97
172
  out.push({
98
173
  type: 'function_call',
99
174
  call_id: block.id,
@@ -208,12 +283,14 @@ function parseArgs(args) {
208
283
  * @param data - The parsed JSON response body.
209
284
  * @yields {ChatEvent} Text, tool-use, and done events.
210
285
  */
211
- export function* parseResponsesNonStreaming(data) {
286
+ export function* parseResponsesNonStreaming(data, model) {
212
287
  const output = data.output ?? [];
288
+ const replay = new ReplayItems(model);
213
289
  let webSearchCalls = 0;
214
290
  for (const item of output) {
215
291
  if (item.type === 'web_search_call')
216
292
  webSearchCalls++;
293
+ replay.add(item);
217
294
  if (item.type === 'message') {
218
295
  for (const part of item.content ?? []) {
219
296
  if (part.type === 'output_text' && typeof part.text === 'string' && part.text.length > 0) {
@@ -227,6 +304,7 @@ export function* parseResponsesNonStreaming(data) {
227
304
  id: String(item.call_id ?? ''),
228
305
  name: String(item.name ?? ''),
229
306
  input: parseArgs(item.arguments),
307
+ ...replay.take(),
230
308
  };
231
309
  }
232
310
  }
@@ -239,9 +317,10 @@ export function* parseResponsesNonStreaming(data) {
239
317
  * Parse a streaming SSE response from `/v1/responses`.
240
318
  *
241
319
  * @param response - The fetch Response whose body is an SSE stream.
320
+ * @param model - The model the request targeted.
242
321
  * @yields {ChatEvent} Text, tool-call progress, tool-use, keep-alive, and done events.
243
322
  */
244
- export async function* parseResponsesStream(response) {
323
+ export async function* parseResponsesStream(response, model) {
245
324
  const reader = response.body?.getReader();
246
325
  if (!reader) {
247
326
  yield {
@@ -256,6 +335,7 @@ export async function* parseResponsesStream(response) {
256
335
  let usage = null;
257
336
  /** Function calls in progress, keyed by output item id. */
258
337
  const calls = new Map();
338
+ const replay = new ReplayItems(model);
259
339
  /**
260
340
  * `web_search_call` items started — OpenAI bills each one. Counted when the
261
341
  * call starts so a stream cut mid-search still meters it.
@@ -319,8 +399,12 @@ export async function* parseResponsesStream(response) {
319
399
  id: String(item.call_id ?? ''),
320
400
  name: String(item.name ?? ''),
321
401
  input: parseArgs(item.arguments),
402
+ ...replay.take(),
322
403
  };
323
404
  }
405
+ else if (item) {
406
+ replay.add(item);
407
+ }
324
408
  return;
325
409
  case 'response.completed':
326
410
  case 'response.incomplete': {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@molecule/api-ai-openai",
3
- "version": "1.4.0",
3
+ "version": "1.4.2",
4
4
  "description": "",
5
5
  "homepage": "https://www.molecule.dev/packages/api-ai-openai",
6
6
  "type": "module",