@molecule/api-ai-openai 1.3.0 → 1.4.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.
@@ -69,7 +69,7 @@ export declare function formatResponsesTools(tools: AITool[], serverTools: Serve
69
69
  * @param usage - The `usage` object of a completed response.
70
70
  * @returns The normalized usage.
71
71
  */
72
- export declare function responsesUsage(usage: ResponsesUsage | undefined | null): TokenUsage;
72
+ export declare function responsesUsage(usage: ResponsesUsage | undefined | null, webSearchCalls?: number): TokenUsage;
73
73
  /**
74
74
  * Parse a non-streaming `/v1/responses` result.
75
75
  *
@@ -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,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,CA2BzB;AAED;;;;;;;GAOG;AACH,wBAAgB,oBAAoB,CAAC,QAAQ,EAAE,WAAW,EAAE,GAAG,KAAK,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CA+C5F;AA6BD;;;;;;;;;;;;GAYG;AACH,wBAAgB,oBAAoB,CAClC,KAAK,EAAE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;;;;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,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,QAAQ,CAAC,SAAS,CAAC,CAwB9F;AAED;;;;;GAKG;AACH,wBAAuB,oBAAoB,CAAC,QAAQ,EAAE,QAAQ,GAAG,aAAa,CAAC,SAAS,CAAC,CAwJxF"}
package/dist/responses.js CHANGED
@@ -176,7 +176,7 @@ export function formatResponsesTools(tools, serverTools) {
176
176
  * @param usage - The `usage` object of a completed response.
177
177
  * @returns The normalized usage.
178
178
  */
179
- export function responsesUsage(usage) {
179
+ export function responsesUsage(usage, webSearchCalls = 0) {
180
180
  const cached = usage?.input_tokens_details?.cached_tokens ?? 0;
181
181
  const written = usage?.input_tokens_details?.cache_write_tokens ?? 0;
182
182
  return {
@@ -184,6 +184,7 @@ export function responsesUsage(usage) {
184
184
  outputTokens: usage?.output_tokens ?? 0,
185
185
  cacheReadInputTokens: cached,
186
186
  ...(written > 0 ? { cacheCreationInputTokens: written } : {}),
187
+ ...(webSearchCalls > 0 ? { webSearchRequests: webSearchCalls } : {}),
187
188
  };
188
189
  }
189
190
  /**
@@ -209,7 +210,10 @@ function parseArgs(args) {
209
210
  */
210
211
  export function* parseResponsesNonStreaming(data) {
211
212
  const output = data.output ?? [];
213
+ let webSearchCalls = 0;
212
214
  for (const item of output) {
215
+ if (item.type === 'web_search_call')
216
+ webSearchCalls++;
213
217
  if (item.type === 'message') {
214
218
  for (const part of item.content ?? []) {
215
219
  if (part.type === 'output_text' && typeof part.text === 'string' && part.text.length > 0) {
@@ -226,7 +230,10 @@ export function* parseResponsesNonStreaming(data) {
226
230
  };
227
231
  }
228
232
  }
229
- yield { type: 'done', usage: responsesUsage(data.usage) };
233
+ yield {
234
+ type: 'done',
235
+ usage: responsesUsage(data.usage, webSearchCalls),
236
+ };
230
237
  }
231
238
  /**
232
239
  * Parse a streaming SSE response from `/v1/responses`.
@@ -249,6 +256,11 @@ export async function* parseResponsesStream(response) {
249
256
  let usage = null;
250
257
  /** Function calls in progress, keyed by output item id. */
251
258
  const calls = new Map();
259
+ /**
260
+ * `web_search_call` items started — OpenAI bills each one. Counted when the
261
+ * call starts so a stream cut mid-search still meters it.
262
+ */
263
+ let webSearchCalls = 0;
252
264
  /**
253
265
  * Turn one SSE data payload into zero or more ChatEvents.
254
266
  *
@@ -272,7 +284,15 @@ export async function* parseResponsesStream(response) {
272
284
  }
273
285
  return;
274
286
  case 'response.output_item.added':
275
- if (item?.type === 'function_call') {
287
+ if (item?.type === 'web_search_call') {
288
+ webSearchCalls++;
289
+ // Metering snapshot: the search is billed even if the turn is cut.
290
+ yield {
291
+ type: 'usage',
292
+ usage: { inputTokens: 0, outputTokens: 0, webSearchRequests: webSearchCalls },
293
+ };
294
+ }
295
+ else if (item?.type === 'function_call') {
276
296
  const callId = String(item.call_id ?? '');
277
297
  const name = String(item.name ?? '');
278
298
  calls.set(String(item.id ?? ''), { callId, name });
@@ -305,7 +325,7 @@ export async function* parseResponsesStream(response) {
305
325
  case 'response.completed':
306
326
  case 'response.incomplete': {
307
327
  const res = event.response;
308
- usage = responsesUsage(res?.usage);
328
+ usage = responsesUsage(res?.usage, webSearchCalls);
309
329
  if (event.type === 'response.incomplete') {
310
330
  logger.warn('OpenAI response incomplete', { details: res?.incomplete_details });
311
331
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@molecule/api-ai-openai",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "",
5
5
  "homepage": "https://www.molecule.dev/packages/api-ai-openai",
6
6
  "type": "module",
@@ -33,7 +33,7 @@
33
33
  "directory": "packages/api/bonds/ai/openai"
34
34
  },
35
35
  "devDependencies": {
36
- "@molecule/api-ai": "1.2.1",
36
+ "@molecule/api-ai": "1.3.0",
37
37
  "@molecule/api-bond": "1.0.2",
38
38
  "@types/node": "26.1.2",
39
39
  "typescript": "6.0.3",