@opencode-ai/ai 0.0.0-beta-17941 → 0.0.0-beta-18027

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.
Files changed (34) hide show
  1. package/dist/protocols/anthropic-messages.js +3 -2
  2. package/dist/protocols/gemini.d.ts +40 -40
  3. package/dist/protocols/gemini.js +68 -50
  4. package/dist/protocols/open-responses.d.ts +6 -7
  5. package/dist/protocols/open-responses.js +40 -50
  6. package/dist/protocols/openai-chat.d.ts +42 -2
  7. package/dist/protocols/openai-chat.js +34 -5
  8. package/dist/protocols/openai-compatible-responses.d.ts +1 -1
  9. package/dist/protocols/openai-responses.d.ts +6 -6
  10. package/dist/protocols/openai-responses.js +1 -2
  11. package/dist/protocols/utils/open-responses-options.d.ts +4 -3
  12. package/dist/protocols/utils/open-responses-options.js +2 -1
  13. package/dist/protocols/utils/openai-options.d.ts +3 -3
  14. package/dist/protocols/utils/openai-options.js +1 -1
  15. package/dist/protocols/xai-responses.d.ts +1 -1
  16. package/dist/provider-error.d.ts +1 -0
  17. package/dist/provider-error.js +4 -2
  18. package/dist/providers/amazon-bedrock-mantle.d.ts +11 -11
  19. package/dist/providers/azure.d.ts +8 -8
  20. package/dist/providers/cloudflare.d.ts +2 -2
  21. package/dist/providers/google-vertex-chat.d.ts +2 -2
  22. package/dist/providers/google-vertex-responses.d.ts +1 -1
  23. package/dist/providers/google-vertex.d.ts +7 -7
  24. package/dist/providers/google-vertex.js +13 -1
  25. package/dist/providers/google.d.ts +7 -7
  26. package/dist/providers/openai-compatible-responses.d.ts +1 -1
  27. package/dist/providers/openai-compatible.d.ts +9 -9
  28. package/dist/providers/openai-options.d.ts +6 -2
  29. package/dist/providers/openai.d.ts +12 -12
  30. package/dist/providers/openrouter.d.ts +22 -0
  31. package/dist/providers/xai.d.ts +11 -11
  32. package/dist/schema/errors.d.ts +1 -0
  33. package/dist/schema/errors.js +3 -0
  34. package/package.json +5 -5
@@ -1,7 +1,7 @@
1
1
  import { Effect, Schema } from "effect";
2
2
  import { HttpTransport } from "../route/transport/index.js";
3
3
  import { Protocol } from "../route/protocol.js";
4
- import { AIError, LLMEvent, Usage, } from "../schema/index.js";
4
+ import { AIError, LLMEvent, ProviderInternalReason, Usage, } from "../schema/index.js";
5
5
  import { JsonObject, optionalArray, optionalNull, ProviderShared } from "./shared.js";
6
6
  import { classifyProviderFailure } from "../provider-error.js";
7
7
  import { OpenResponsesOptions } from "./utils/open-responses-options.js";
@@ -39,6 +39,11 @@ const OpenResponsesOutputText = Schema.Struct({
39
39
  text: Schema.String,
40
40
  });
41
41
  export const MessagePhase = Schema.NullOr(Schema.Literals(["commentary", "final_answer"]));
42
+ const messagePhase = (value) => {
43
+ if (value === null || value === "commentary" || value === "final_answer")
44
+ return value;
45
+ return undefined;
46
+ };
42
47
  const OpenResponsesReasoningSummaryText = Schema.Struct({
43
48
  type: Schema.tag("summary_text"),
44
49
  text: Schema.String,
@@ -218,19 +223,6 @@ export const Event = Schema.StructWithRest(Schema.Struct({
218
223
  status_code: Schema.optional(Schema.Unknown),
219
224
  headers: Schema.optional(Schema.Unknown),
220
225
  }), [Schema.Record(Schema.String, Schema.Unknown)]);
221
- const RefusalEvent = Schema.Union([
222
- Schema.Struct({
223
- type: Schema.tag("response.refusal.delta"),
224
- item_id: Schema.String,
225
- delta: Schema.String,
226
- }),
227
- Schema.Struct({
228
- type: Schema.tag("response.refusal.done"),
229
- item_id: Schema.String,
230
- refusal: Schema.String,
231
- }),
232
- ]);
233
- const isRefusalEvent = Schema.is(RefusalEvent);
234
226
  const BASE = { id: ADAPTER, name: NAME };
235
227
  // =============================================================================
236
228
  // Request Lowering
@@ -284,9 +276,6 @@ const lowerReasoning = (part, providerMetadataKey) => {
284
276
  encrypted_content: encryptedContent,
285
277
  };
286
278
  };
287
- const hostedToolItemID = (part, providerMetadataKey) => {
288
- return itemID(part.providerMetadata, providerMetadataKey);
289
- };
290
279
  const lowerMedia = Effect.fn("OpenResponses.lowerMedia")(function* (part, request, extension, target) {
291
280
  const media = ProviderShared.normalizeMedia(part);
292
281
  const extended = extension.lowerMedia?.({ part, media, request });
@@ -301,7 +290,7 @@ const lowerMedia = Effect.fn("OpenResponses.lowerMedia")(function* (part, reques
301
290
  return {
302
291
  type: "input_file",
303
292
  filename: part.filename ?? (media.mime === "application/pdf" ? "document.pdf" : "file"),
304
- ...(url ? { file_url: url } : { file_data: media.base64 }),
293
+ ...(url ? { file_url: url } : { file_data: media.dataUrl }),
305
294
  };
306
295
  }
307
296
  return { type: "input_image", image_url: url ?? media.dataUrl };
@@ -424,9 +413,9 @@ const lowerMessages = Effect.fn("OpenResponses.lowerMessages")(function* (reques
424
413
  }
425
414
  if (part.type === "tool-result" && part.providerExecuted === true) {
426
415
  flushText();
427
- const itemID = hostedToolItemID(part, providerMetadataKey);
428
- if (store !== false && itemID && !hostedToolReferences.has(itemID))
429
- input.push({ type: "item_reference", id: itemID });
416
+ const id = itemID(part.providerMetadata, providerMetadataKey);
417
+ if (store !== false && id && !hostedToolReferences.has(id))
418
+ input.push({ type: "item_reference", id });
430
419
  if (store === false && part.result.type === "content") {
431
420
  const content = part.result.value;
432
421
  input.push({
@@ -434,8 +423,8 @@ const lowerMessages = Effect.fn("OpenResponses.lowerMessages")(function* (reques
434
423
  content: yield* Effect.forEach(content, (item) => lowerHostedToolResultContentItem(item, request, extension)),
435
424
  });
436
425
  }
437
- if (itemID)
438
- hostedToolReferences.add(itemID);
426
+ if (id)
427
+ hostedToolReferences.add(id);
439
428
  continue;
440
429
  }
441
430
  return yield* ProviderShared.unsupportedContent(extension.name, "assistant", [
@@ -619,18 +608,17 @@ const reasoningMetadata = (state, item) => providerMetadata(state, { itemId: ite
619
608
  // best-effort, not guaranteed.
620
609
  const onOutputItemAdded = (state, event) => {
621
610
  const item = event.item;
622
- if (item?.type === "message" && item.id)
611
+ if (item?.type === "message" && item.id) {
612
+ const phase = messagePhase(item.phase);
623
613
  return [
624
614
  {
625
615
  ...state,
626
616
  messageItems: new Set([...state.messageItems, item.id]),
627
- messagePhases: (() => {
628
- const phase = state.messagePhase(item.phase);
629
- return phase === undefined ? state.messagePhases : { ...state.messagePhases, [item.id]: phase };
630
- })(),
617
+ messagePhases: phase === undefined ? state.messagePhases : { ...state.messagePhases, [item.id]: phase },
631
618
  },
632
619
  NO_EVENTS,
633
620
  ];
621
+ }
634
622
  if (item && isReasoningItem(item)) {
635
623
  const events = [];
636
624
  return [
@@ -752,7 +740,7 @@ const onOutputItemDone = Effect.fn("OpenResponses.onOutputItemDone")(function* (
752
740
  if (!item)
753
741
  return [state, NO_EVENTS];
754
742
  if (item.type === "message" && item.id) {
755
- const itemPhase = state.messagePhase(item.phase);
743
+ const itemPhase = messagePhase(item.phase);
756
744
  const phase = itemPhase === undefined ? state.messagePhases[item.id] : itemPhase;
757
745
  const events = [];
758
746
  const messageItems = new Set(state.messageItems);
@@ -843,31 +831,43 @@ const onResponseFinish = Effect.fn("OpenResponses.onResponseFinish")(function* (
843
831
  });
844
832
  return [{ ...state, lifecycle, hasFunctionCall, tools: pending.tools }, events];
845
833
  });
846
- // Build a single human-readable message from whatever the provider supplied.
834
+ // Build the prettiest summary available from whatever the provider supplied.
847
835
  // When both code and message are present, prefix the code so consumers see
848
836
  // the failure mode (e.g. `rate_limit_exceeded: Slow down`) instead of just
849
837
  // the bare message — production rate limits and context-length failures used
850
- // to be indistinguishable from generic stream drops.
851
- const providerErrorMessage = (event, fallback) => {
852
- const nested = event.error ?? event.response?.error ?? undefined;
838
+ // to be indistinguishable from generic stream drops. Returns undefined when
839
+ // the payload carries no usable summary.
840
+ const providerErrorMessage = (event, nested) => {
853
841
  const message = event.message || nested?.message || undefined;
854
842
  const code = event.code || nested?.code || undefined;
855
843
  if (message && code)
856
844
  return `${code}: ${message}`;
857
- return message || code || fallback;
845
+ return message || code;
858
846
  };
859
847
  export const providerFailure = (id, event, fallback) => {
860
- const code = event.code || event.error?.code || event.response?.error?.code || undefined;
861
- const message = providerErrorMessage(event, fallback);
848
+ const nested = event.error ?? event.response?.error ?? undefined;
849
+ const code = event.code || nested?.code || undefined;
850
+ // Keep the full raw payload on the error even when the message is a summary.
851
+ const body = JSON.stringify(nested ?? event) ?? "";
852
+ const summary = providerErrorMessage(event, nested);
853
+ const message = summary ?? (body === "{}" ? fallback : body);
862
854
  const status = typeof event.status === "number"
863
855
  ? event.status
864
856
  : typeof event.status_code === "number"
865
857
  ? event.status_code
866
858
  : undefined;
859
+ const reason = event.type === "error" &&
860
+ event.error === undefined &&
861
+ event.response === undefined &&
862
+ summary === undefined &&
863
+ status === undefined
864
+ ? new ProviderInternalReason({ message })
865
+ : classifyProviderFailure({ message, code, status, rawBody: body });
867
866
  return new AIError({
868
867
  module: id,
869
868
  method: "stream",
870
- reason: classifyProviderFailure({ message, code, status }),
869
+ body,
870
+ reason,
871
871
  });
872
872
  };
873
873
  const providerError = (state, event, fallback) => providerFailure(state.id, event, fallback);
@@ -880,22 +880,18 @@ export const step = (state, event) => {
880
880
  : onOutputTextDone(state, event, event.item_id));
881
881
  }
882
882
  if (event.type === "response.refusal.delta" || event.type === "response.refusal.done") {
883
- if (!isRefusalEvent(event))
883
+ const value = event.type === "response.refusal.delta" ? event.delta : event.refusal;
884
+ if (!event.item_id || typeof value !== "string")
884
885
  return ProviderShared.eventError(state.id, `${event.type} is malformed`);
885
886
  return Effect.succeed(event.type === "response.refusal.delta"
886
887
  ? onOutputTextDelta(state, event, event.item_id)
887
- : onOutputTextDone(state, { ...event, text: event.refusal }, event.item_id));
888
+ : onOutputTextDone(state, { ...event, text: value }, event.item_id));
888
889
  }
889
890
  if (event.type === "response.reasoning.delta" || event.type === "response.reasoning_summary_text.delta") {
890
891
  if (!event.item_id)
891
892
  return ProviderShared.eventError(state.id, `${event.type} is missing item_id`);
892
893
  return Effect.succeed(onReasoningDelta(state, event, event.item_id));
893
894
  }
894
- if (event.type === "response.reasoning.done" || event.type === "response.reasoning_summary_text.done") {
895
- if (!event.item_id)
896
- return ProviderShared.eventError(state.id, `${event.type} is missing item_id`);
897
- return Effect.succeed(onReasoningDone(state, event));
898
- }
899
895
  if (event.type === "response.reasoning_summary_part.added")
900
896
  return event.item_id
901
897
  ? Effect.succeed(onReasoningSummaryPartAdded(state, event))
@@ -939,16 +935,10 @@ export const initial = (request, extension = BASE) => ({
939
935
  tools: ToolStream.empty(),
940
936
  lifecycle: Lifecycle.initial(),
941
937
  messageItems: new Set(),
942
- messagePhase,
943
938
  messagePhases: {},
944
939
  reasoningItems: {},
945
940
  store: OpenResponsesOptions.resolve(request).store,
946
941
  });
947
- const messagePhase = (value) => {
948
- if (value === null || value === "commentary" || value === "final_answer")
949
- return value;
950
- return undefined;
951
- };
952
942
  export const protocol = Protocol.make({
953
943
  id: ADAPTER,
954
944
  body: {
@@ -201,7 +201,7 @@ declare const OpenAIChatBody: Schema.Struct<{
201
201
  }>;
202
202
  export type OpenAIChatBody = Schema.Schema.Type<typeof OpenAIChatBody>;
203
203
  export declare const OpenAIChatEvent: Schema.Struct<{
204
- readonly choices: Schema.optional<Schema.NullOr<Schema.$Array<Schema.Struct<{
204
+ readonly choices: Schema.optional<Schema.NullOr<Schema.$Array<Schema.StructWithRest<Schema.Struct<{
205
205
  readonly delta: Schema.optional<Schema.NullOr<Schema.StructWithRest<Schema.Struct<{
206
206
  readonly content: Schema.optional<Schema.NullOr<Schema.String>>;
207
207
  readonly refusal: Schema.optional<Schema.NullOr<Schema.String>>;
@@ -220,11 +220,29 @@ export declare const OpenAIChatEvent: Schema.Struct<{
220
220
  }>, readonly [Schema.$Record<Schema.String, Schema.Unknown>]>>>;
221
221
  readonly finish_reason: Schema.optional<Schema.NullOr<Schema.String>>;
222
222
  readonly native_finish_reason: Schema.optional<Schema.NullOr<Schema.String>>;
223
- }>>>>;
223
+ readonly usage: Schema.optional<Schema.NullOr<Schema.StructWithRest<Schema.Struct<{
224
+ readonly prompt_tokens: Schema.optional<Schema.NullOr<Schema.Number>>;
225
+ readonly completion_tokens: Schema.optional<Schema.NullOr<Schema.Number>>;
226
+ readonly total_tokens: Schema.optional<Schema.NullOr<Schema.Number>>;
227
+ readonly cached_tokens: Schema.optional<Schema.NullOr<Schema.Number>>;
228
+ readonly prompt_cache_hit_tokens: Schema.optional<Schema.NullOr<Schema.Number>>;
229
+ readonly prompt_tokens_details: Schema.optional<Schema.NullOr<Schema.StructWithRest<Schema.Struct<{
230
+ readonly cached_tokens: Schema.optional<Schema.NullOr<Schema.Number>>;
231
+ readonly cache_write_tokens: Schema.optional<Schema.NullOr<Schema.Number>>;
232
+ }>, readonly [Schema.$Record<Schema.String, Schema.Unknown>]>>>;
233
+ readonly completion_tokens_details: Schema.optional<Schema.NullOr<Schema.StructWithRest<Schema.Struct<{
234
+ readonly reasoning_tokens: Schema.optional<Schema.NullOr<Schema.Number>>;
235
+ readonly accepted_prediction_tokens: Schema.optional<Schema.NullOr<Schema.Number>>;
236
+ readonly rejected_prediction_tokens: Schema.optional<Schema.NullOr<Schema.Number>>;
237
+ }>, readonly [Schema.$Record<Schema.String, Schema.Unknown>]>>>;
238
+ }>, readonly [Schema.$Record<Schema.String, Schema.Unknown>]>>>;
239
+ }>, readonly [Schema.$Record<Schema.String, Schema.Unknown>]>>>>;
224
240
  readonly usage: Schema.optional<Schema.NullOr<Schema.StructWithRest<Schema.Struct<{
225
241
  readonly prompt_tokens: Schema.optional<Schema.NullOr<Schema.Number>>;
226
242
  readonly completion_tokens: Schema.optional<Schema.NullOr<Schema.Number>>;
227
243
  readonly total_tokens: Schema.optional<Schema.NullOr<Schema.Number>>;
244
+ readonly cached_tokens: Schema.optional<Schema.NullOr<Schema.Number>>;
245
+ readonly prompt_cache_hit_tokens: Schema.optional<Schema.NullOr<Schema.Number>>;
228
246
  readonly prompt_tokens_details: Schema.optional<Schema.NullOr<Schema.StructWithRest<Schema.Struct<{
229
247
  readonly cached_tokens: Schema.optional<Schema.NullOr<Schema.Number>>;
230
248
  readonly cache_write_tokens: Schema.optional<Schema.NullOr<Schema.Number>>;
@@ -566,6 +584,8 @@ export declare const protocol: Protocol<{
566
584
  readonly prompt_tokens?: number | null | undefined;
567
585
  readonly completion_tokens?: number | null | undefined;
568
586
  readonly total_tokens?: number | null | undefined;
587
+ readonly cached_tokens?: number | null | undefined;
588
+ readonly prompt_cache_hit_tokens?: number | null | undefined;
569
589
  readonly prompt_tokens_details?: {
570
590
  readonly [x: string]: unknown;
571
591
  readonly cached_tokens?: number | null | undefined;
@@ -579,6 +599,7 @@ export declare const protocol: Protocol<{
579
599
  } | null | undefined;
580
600
  } | null | undefined;
581
601
  readonly choices?: readonly {
602
+ readonly [x: string]: unknown;
582
603
  readonly delta?: {
583
604
  readonly [x: string]: unknown;
584
605
  readonly reasoning?: string | null | undefined;
@@ -596,6 +617,25 @@ export declare const protocol: Protocol<{
596
617
  }[] | null | undefined;
597
618
  readonly reasoning_details?: unknown;
598
619
  } | null | undefined;
620
+ readonly usage?: {
621
+ readonly [x: string]: unknown;
622
+ readonly prompt_tokens?: number | null | undefined;
623
+ readonly completion_tokens?: number | null | undefined;
624
+ readonly total_tokens?: number | null | undefined;
625
+ readonly cached_tokens?: number | null | undefined;
626
+ readonly prompt_cache_hit_tokens?: number | null | undefined;
627
+ readonly prompt_tokens_details?: {
628
+ readonly [x: string]: unknown;
629
+ readonly cached_tokens?: number | null | undefined;
630
+ readonly cache_write_tokens?: number | null | undefined;
631
+ } | null | undefined;
632
+ readonly completion_tokens_details?: {
633
+ readonly [x: string]: unknown;
634
+ readonly reasoning_tokens?: number | null | undefined;
635
+ readonly accepted_prediction_tokens?: number | null | undefined;
636
+ readonly rejected_prediction_tokens?: number | null | undefined;
637
+ } | null | undefined;
638
+ } | null | undefined;
599
639
  readonly finish_reason?: string | null | undefined;
600
640
  readonly native_finish_reason?: string | null | undefined;
601
641
  }[] | null | undefined;
@@ -123,6 +123,9 @@ const OpenAIChatUsage = Schema.StructWithRest(Schema.Struct({
123
123
  prompt_tokens: optionalNull(Schema.Number),
124
124
  completion_tokens: optionalNull(Schema.Number),
125
125
  total_tokens: optionalNull(Schema.Number),
126
+ // Zai reports cache hits as top-level `cached_tokens`; DeepSeek uses `prompt_cache_hit_tokens`.
127
+ cached_tokens: optionalNull(Schema.Number),
128
+ prompt_cache_hit_tokens: optionalNull(Schema.Number),
126
129
  prompt_tokens_details: optionalNull(Schema.StructWithRest(Schema.Struct({
127
130
  cached_tokens: optionalNull(Schema.Number),
128
131
  cache_write_tokens: optionalNull(Schema.Number),
@@ -151,11 +154,13 @@ const OpenAIChatDelta = Schema.StructWithRest(Schema.Struct({
151
154
  reasoning_details: optionalNull(Schema.Unknown),
152
155
  tool_calls: optionalNull(Schema.Array(OpenAIChatToolCallDelta)),
153
156
  }), [Schema.Record(Schema.String, Schema.Unknown)]);
154
- const OpenAIChatChoice = Schema.Struct({
157
+ const OpenAIChatChoice = Schema.StructWithRest(Schema.Struct({
155
158
  delta: optionalNull(OpenAIChatDelta),
156
159
  finish_reason: optionalNull(Schema.String),
157
160
  native_finish_reason: optionalNull(Schema.String),
158
- });
161
+ // Moonshot streams usage on `choice.usage` instead of top-level `usage`.
162
+ usage: optionalNull(OpenAIChatUsage),
163
+ }), [Schema.Record(Schema.String, Schema.Unknown)]);
159
164
  const OpenAIChatError = Schema.Struct({
160
165
  code: optionalNull(Schema.Union([Schema.String, Schema.Number])),
161
166
  message: Schema.String,
@@ -396,6 +401,18 @@ const lowerMessages = Effect.fn("OpenAIChat.lowerMessages")(function* (request,
396
401
  flushImages();
397
402
  return messages;
398
403
  });
404
+ // Anthropic via LiteLLM and Amazon Bedrock require `tools` to be present
405
+ // whenever the conversation history contains tool calls/results. Send an
406
+ // explicit empty array when we have history but no active tools.
407
+ const hasToolHistory = (messages) => {
408
+ for (const message of messages) {
409
+ if (message.role === "tool")
410
+ return true;
411
+ if (message.role === "assistant" && message.content.some((part) => part.type === "tool-call"))
412
+ return true;
413
+ }
414
+ return false;
415
+ };
399
416
  const lowerOptions = (request) => {
400
417
  const options = OpenAIOptions.resolve(request);
401
418
  return {
@@ -413,11 +430,14 @@ export const fromRequest = Effect.fn("OpenAIChat.fromRequest")(function* (reques
413
430
  const generation = request.generation;
414
431
  const toolSchemaCompatibility = request.model.compatibility?.toolSchema;
415
432
  const maxTokensField = request.model.compatibility?.maxTokensField ?? "max_tokens";
433
+ const hasHistory = hasToolHistory(request.messages);
416
434
  return {
417
435
  model: request.model.id,
418
436
  messages: yield* lowerMessages(request, options),
419
437
  tools: request.tools.length === 0
420
- ? undefined
438
+ ? hasHistory
439
+ ? []
440
+ : undefined
421
441
  : request.tools.map((tool) => lowerTool(tool, ToolSchemaProjection.modelCompatibility(tool.inputSchema, toolSchemaCompatibility), options)),
422
442
  tool_choice: request.toolChoice ? yield* lowerToolChoice(request.toolChoice) : undefined,
423
443
  stream: true,
@@ -458,12 +478,18 @@ const mapFinishReason = (reason) => {
458
478
  // total) with a `reasoning_tokens` subset. We pass the inclusive totals
459
479
  // through and derive the non-cached breakdown so the `AI.Usage` contract is
460
480
  // satisfied on both sides.
481
+ // Providers differ on cache-hit location: OpenAI uses
482
+ // `prompt_tokens_details.cached_tokens`, DeepSeek uses
483
+ // `prompt_cache_hit_tokens`, and Zai uses top-level `cached_tokens`.
461
484
  const mapUsage = (usage) => {
462
485
  if (!usage)
463
486
  return undefined;
464
487
  const input = usage.prompt_tokens ?? undefined;
465
488
  const output = usage.completion_tokens ?? undefined;
466
- const cached = usage.prompt_tokens_details?.cached_tokens ?? undefined;
489
+ const cached = (usage.prompt_tokens_details?.cached_tokens ??
490
+ usage.prompt_cache_hit_tokens ??
491
+ usage.cached_tokens ??
492
+ undefined);
467
493
  const cacheWrite = usage.prompt_tokens_details?.cache_write_tokens ?? undefined;
468
494
  const reasoning = usage.completion_tokens_details?.reasoning_tokens ?? undefined;
469
495
  const nonCached = ProviderShared.subtractTokens(input, ProviderShared.sumTokens(cached, cacheWrite));
@@ -554,8 +580,11 @@ const step = (state, event) => Effect.gen(function* () {
554
580
  }),
555
581
  });
556
582
  const events = [];
557
- const usage = mapUsage(event.usage) ?? state.usage;
558
583
  const choice = event.choices?.[0];
584
+ // Moonshot (and a few other OpenAI-compatible providers) attach usage to
585
+ // `choice.usage` instead of the top-level `usage` field.
586
+ const choiceUsage = choice?.usage;
587
+ const usage = mapUsage(event.usage) ?? (choiceUsage ? mapUsage(choiceUsage) : undefined) ?? state.usage;
559
588
  const rawFinishReason = choice?.finish_reason;
560
589
  const finishReason = rawFinishReason !== undefined && rawFinishReason !== null
561
590
  ? { normalized: mapFinishReason(rawFinishReason), raw: choice?.native_finish_reason ?? rawFinishReason }
@@ -117,7 +117,7 @@ export declare const route: Route<{
117
117
  readonly presence_penalty?: number | undefined;
118
118
  readonly safety_identifier?: string | undefined;
119
119
  readonly top_logprobs?: number | undefined;
120
- readonly service_tier?: "default" | "auto" | "flex" | "priority" | undefined;
120
+ readonly service_tier?: import("./utils/open-responses-options.js").ServiceTier | undefined;
121
121
  readonly max_output_tokens?: number | undefined;
122
122
  readonly max_tool_calls?: number | undefined;
123
123
  readonly parallel_tool_calls?: boolean | undefined;
@@ -112,7 +112,7 @@ declare const OpenAIResponsesBody: Schema.Struct<{
112
112
  }>>;
113
113
  readonly top_logprobs: Schema.optional<Schema.Int>;
114
114
  readonly truncation: Schema.optional<Schema.Literals<readonly ["auto", "disabled"]>>;
115
- readonly service_tier: Schema.optional<Schema.Literals<readonly ["auto", "default", "flex", "priority"]>>;
115
+ readonly service_tier: Schema.optional<Schema.declare<import("./utils/open-responses-options.js").ServiceTier, import("./utils/open-responses-options.js").ServiceTier>>;
116
116
  readonly prompt_cache_key: Schema.optional<Schema.String>;
117
117
  readonly include: Schema.optional<Schema.$Array<Schema.declare<import("./utils/open-responses-options.js").ResponseIncludable, import("./utils/open-responses-options.js").ResponseIncludable>>>;
118
118
  readonly reasoning: Schema.optional<Schema.Struct<{
@@ -255,7 +255,7 @@ export declare const protocol: Protocol<{
255
255
  readonly presence_penalty?: number | undefined;
256
256
  readonly safety_identifier?: string | undefined;
257
257
  readonly top_logprobs?: number | undefined;
258
- readonly service_tier?: "default" | "auto" | "flex" | "priority" | undefined;
258
+ readonly service_tier?: import("./utils/open-responses-options.js").ServiceTier | undefined;
259
259
  readonly max_output_tokens?: number | undefined;
260
260
  readonly max_tool_calls?: number | undefined;
261
261
  readonly parallel_tool_calls?: boolean | undefined;
@@ -438,7 +438,7 @@ export declare const httpTransport: HttpTransport.HttpJsonTransport<{
438
438
  readonly presence_penalty?: number | undefined;
439
439
  readonly safety_identifier?: string | undefined;
440
440
  readonly top_logprobs?: number | undefined;
441
- readonly service_tier?: "default" | "auto" | "flex" | "priority" | undefined;
441
+ readonly service_tier?: import("./utils/open-responses-options.js").ServiceTier | undefined;
442
442
  readonly max_output_tokens?: number | undefined;
443
443
  readonly max_tool_calls?: number | undefined;
444
444
  readonly parallel_tool_calls?: boolean | undefined;
@@ -567,7 +567,7 @@ export declare const channelTransport: (options: import("./open-responses-channe
567
567
  readonly presence_penalty?: number | undefined;
568
568
  readonly safety_identifier?: string | undefined;
569
569
  readonly top_logprobs?: number | undefined;
570
- readonly service_tier?: "default" | "auto" | "flex" | "priority" | undefined;
570
+ readonly service_tier?: import("./utils/open-responses-options.js").ServiceTier | undefined;
571
571
  readonly max_output_tokens?: number | undefined;
572
572
  readonly max_tool_calls?: number | undefined;
573
573
  readonly parallel_tool_calls?: boolean | undefined;
@@ -696,7 +696,7 @@ export declare const transport: import("../route/transport/index.js").Transport<
696
696
  readonly presence_penalty?: number | undefined;
697
697
  readonly safety_identifier?: string | undefined;
698
698
  readonly top_logprobs?: number | undefined;
699
- readonly service_tier?: "default" | "auto" | "flex" | "priority" | undefined;
699
+ readonly service_tier?: import("./utils/open-responses-options.js").ServiceTier | undefined;
700
700
  readonly max_output_tokens?: number | undefined;
701
701
  readonly max_tool_calls?: number | undefined;
702
702
  readonly parallel_tool_calls?: boolean | undefined;
@@ -825,7 +825,7 @@ export declare const route: Route<{
825
825
  readonly presence_penalty?: number | undefined;
826
826
  readonly safety_identifier?: string | undefined;
827
827
  readonly top_logprobs?: number | undefined;
828
- readonly service_tier?: "default" | "auto" | "flex" | "priority" | undefined;
828
+ readonly service_tier?: import("./utils/open-responses-options.js").ServiceTier | undefined;
829
829
  readonly max_output_tokens?: number | undefined;
830
830
  readonly max_tool_calls?: number | undefined;
831
831
  readonly parallel_tool_calls?: boolean | undefined;
@@ -109,13 +109,12 @@ const HOSTED_TOOLS = {
109
109
  name: "code_interpreter",
110
110
  input: (item) => ({ code: item.code, container_id: item.container_id }),
111
111
  },
112
- computer_use_call: { name: "computer_use", input: (item) => item.action ?? {} },
112
+ computer_call: { name: "computer_use", input: (item) => item.action ?? {} },
113
113
  image_generation_call: { name: "image_generation", input: () => ({}), result: hostedToolResult },
114
114
  mcp_call: {
115
115
  name: "mcp",
116
116
  input: (item) => ({ server_label: item.server_label, name: item.name, arguments: item.arguments }),
117
117
  },
118
- local_shell_call: { name: "local_shell", input: (item) => item.action ?? {} },
119
118
  };
120
119
  const step = (state, event) => {
121
120
  if (event.type === "response.reasoning_text.delta" || event.type === "response.reasoning_summary.delta")
@@ -9,12 +9,13 @@ export declare const TextVerbosity: Schema.declare<TextVerbosity, TextVerbosity>
9
9
  export declare const ResponseIncludables: readonly ["file_search_call.results", "web_search_call.results", "web_search_call.action.sources", "message.input_image.image_url", "computer_call_output.output.image_url", "code_interpreter_call.outputs", "reasoning.encrypted_content", "message.output_text.logprobs"];
10
10
  export type ResponseIncludable = (typeof ResponseIncludables)[number] | (string & {});
11
11
  export declare const ServiceTiers: readonly ["auto", "default", "flex", "priority"];
12
- export type ServiceTier = (typeof ServiceTiers)[number];
12
+ export type ServiceTier = (typeof ServiceTiers)[number] | (string & {});
13
+ export declare const ServiceTier: Schema.declare<ServiceTier, ServiceTier>;
13
14
  export declare const Truncations: readonly ["auto", "disabled"];
14
15
  export type Truncation = (typeof Truncations)[number];
15
16
  export declare const TextVerbositySchema: Schema.declare<TextVerbosity, TextVerbosity>;
16
17
  export declare const ResponseIncludableSchema: Schema.declare<ResponseIncludable, ResponseIncludable>;
17
- export declare const ServiceTierSchema: Schema.Literals<readonly ["auto", "default", "flex", "priority"]>;
18
+ export declare const ServiceTierSchema: Schema.declare<ServiceTier, ServiceTier>;
18
19
  export declare const TruncationSchema: Schema.Literals<readonly ["auto", "disabled"]>;
19
20
  export declare const AllowedTools: Schema.Struct<{
20
21
  readonly toolNames: Schema.$Array<Schema.String>;
@@ -37,7 +38,7 @@ export declare const Options: Schema.Struct<{
37
38
  readonly reasoningSummary: Schema.optional<Schema.Literals<readonly ["auto", "concise", "detailed"]>>;
38
39
  readonly include: Schema.optional<Schema.$Array<Schema.declare<ResponseIncludable, ResponseIncludable>>>;
39
40
  readonly textVerbosity: Schema.optional<Schema.declare<TextVerbosity, TextVerbosity>>;
40
- readonly serviceTier: Schema.optional<Schema.Literals<readonly ["auto", "default", "flex", "priority"]>>;
41
+ readonly serviceTier: Schema.optional<Schema.declare<ServiceTier, ServiceTier>>;
41
42
  readonly truncation: Schema.optional<Schema.Literals<readonly ["auto", "disabled"]>>;
42
43
  readonly allowedTools: Schema.optional<Schema.Struct<{
43
44
  readonly toolNames: Schema.$Array<Schema.String>;
@@ -14,10 +14,11 @@ export const ResponseIncludables = [
14
14
  "message.output_text.logprobs",
15
15
  ];
16
16
  export const ServiceTiers = ["auto", "default", "flex", "priority"];
17
+ export const ServiceTier = Schema.declare((value) => typeof value === "string", { title: "ServiceTier" });
17
18
  export const Truncations = ["auto", "disabled"];
18
19
  export const TextVerbositySchema = TextVerbosity;
19
20
  export const ResponseIncludableSchema = Schema.declare((value) => typeof value === "string", { title: "ResponseIncludable" });
20
- export const ServiceTierSchema = Schema.Literals(ServiceTiers);
21
+ export const ServiceTierSchema = ServiceTier;
21
22
  export const TruncationSchema = Schema.Literals(Truncations);
22
23
  export const AllowedTools = Schema.Struct({
23
24
  toolNames: Schema.Array(Schema.String),
@@ -5,12 +5,12 @@ export declare const OpenAITextVerbosities: readonly ["low", "medium", "high"];
5
5
  export type OpenAITextVerbosity = OpenResponsesOptions.TextVerbosity;
6
6
  export declare const OpenAIResponseIncludables: readonly ["file_search_call.results", "web_search_call.results", "web_search_call.action.sources", "message.input_image.image_url", "computer_call_output.output.image_url", "code_interpreter_call.outputs", "reasoning.encrypted_content", "message.output_text.logprobs"];
7
7
  export type OpenAIResponseIncludable = OpenResponsesOptions.ResponseIncludable;
8
- export declare const OpenAIServiceTiers: readonly ["auto", "default", "flex", "priority"];
9
- export type OpenAIServiceTier = OpenResponsesOptions.ServiceTier;
8
+ export declare const OpenAIServiceTiers: readonly ["auto", "default", "flex", "priority", "scale"];
9
+ export type OpenAIServiceTier = (typeof OpenAIServiceTiers)[number] | (string & {});
10
10
  export declare const OpenAIReasoningEffort: import("effect/Schema").declare<OpenResponsesOptions.ReasoningEffort, OpenResponsesOptions.ReasoningEffort>;
11
11
  export declare const OpenAITextVerbosity: import("effect/Schema").declare<OpenResponsesOptions.TextVerbosity, OpenResponsesOptions.TextVerbosity>;
12
12
  export declare const OpenAIResponseIncludable: import("effect/Schema").declare<OpenResponsesOptions.ResponseIncludable, OpenResponsesOptions.ResponseIncludable>;
13
- export declare const OpenAIServiceTier: import("effect/Schema").Literals<readonly ["auto", "default", "flex", "priority"]>;
13
+ export declare const OpenAIServiceTier: import("effect/Schema").declare<OpenResponsesOptions.ServiceTier, OpenResponsesOptions.ServiceTier>;
14
14
  export declare const isReasoningEffort: (effort: unknown) => effort is OpenAIReasoningEffort;
15
15
  export declare const resolve: (request: import("../../index.js").LLMRequest) => OpenResponsesOptions.Resolved;
16
16
  export * as OpenAIOptions from "./openai-options.js";
@@ -4,7 +4,7 @@ export const OpenAITextVerbosities = OpenResponsesOptions.TextVerbosities;
4
4
  // Mirrors OpenAI's `ResponseIncludable` union from the official SDK. Keep this
5
5
  // in lockstep with `openai-node/src/resources/responses/responses.ts`.
6
6
  export const OpenAIResponseIncludables = OpenResponsesOptions.ResponseIncludables;
7
- export const OpenAIServiceTiers = OpenResponsesOptions.ServiceTiers;
7
+ export const OpenAIServiceTiers = [...OpenResponsesOptions.ServiceTiers, "scale"];
8
8
  export const OpenAIReasoningEffort = OpenResponsesOptions.ReasoningEffort;
9
9
  export const OpenAITextVerbosity = OpenResponsesOptions.TextVerbosity;
10
10
  export const OpenAIResponseIncludable = OpenResponsesOptions.ResponseIncludableSchema;
@@ -112,7 +112,7 @@ export declare const protocol: Protocol<{
112
112
  readonly presence_penalty?: number | undefined;
113
113
  readonly safety_identifier?: string | undefined;
114
114
  readonly top_logprobs?: number | undefined;
115
- readonly service_tier?: "default" | "auto" | "flex" | "priority" | undefined;
115
+ readonly service_tier?: import("./utils/open-responses-options.js").ServiceTier | undefined;
116
116
  readonly max_output_tokens?: number | undefined;
117
117
  readonly max_tool_calls?: number | undefined;
118
118
  readonly parallel_tool_calls?: boolean | undefined;
@@ -6,6 +6,7 @@ export interface ProviderFailure {
6
6
  readonly message: string;
7
7
  readonly status?: number | undefined;
8
8
  readonly code?: string | undefined;
9
+ readonly rawBody?: string | undefined;
9
10
  readonly retryAfterMs?: number | undefined;
10
11
  readonly rateLimit?: HttpRateLimitDetails | undefined;
11
12
  readonly http?: HttpContext | undefined;
@@ -56,11 +56,13 @@ const NETWORK_ERROR_TEXT = /network[-_\s]error/i;
56
56
  // Keep HTTP failures and provider-reported stream failures on one typed path so
57
57
  // session retry policy never needs provider-specific string matching.
58
58
  export function classifyProviderFailure(input) {
59
- const body = input.http?.body ?? "";
59
+ const body = input.http?.body ?? input.rawBody ?? "";
60
60
  const codes = [input.code, ...providerCodes(body), ...providerCodes(input.message)]
61
61
  .filter((code) => code !== undefined)
62
62
  .map((code) => code.toLowerCase());
63
- const text = body || input.message;
63
+ // Scan the raw payload too so signals missing from the summary message
64
+ // (e.g. overflow phrases nested in a JSON error body) still classify.
65
+ const text = [input.message, body].filter((value) => value.length > 0).join("\n");
64
66
  const common = { message: input.message, providerMetadata: input.providerMetadata, http: input.http };
65
67
  const clientScoped = input.status === undefined || (input.status >= 400 && input.status < 500);
66
68
  if (clientScoped &&
@@ -239,31 +239,31 @@ export declare const routes: (RouteDef<{
239
239
  readonly presence_penalty?: number | undefined;
240
240
  readonly safety_identifier?: string | undefined;
241
241
  readonly top_logprobs?: number | undefined;
242
- readonly service_tier?: "default" | "auto" | "flex" | "priority" | undefined;
242
+ readonly service_tier?: import("../protocols/utils/open-responses-options.js").ServiceTier | undefined;
243
243
  readonly max_output_tokens?: number | undefined;
244
244
  readonly max_tool_calls?: number | undefined;
245
245
  readonly parallel_tool_calls?: boolean | undefined;
246
246
  }, import("../protocols/open-responses-channel.js").Prepared>)[];
247
247
  export declare const configure: (input?: Config) => {
248
248
  id: string & import("effect/Brand").Brand<"AI.ProviderID">;
249
- model: (modelID: string | ModelID) => import("../schema/options.js").LanguageModel<import("./open-responses-options.js").OpenResponsesOptionsInput>;
250
- chat: (modelID: string | ModelID) => import("../schema/options.js").LanguageModel<import("./open-responses-options.js").OpenResponsesOptionsInput>;
251
- responses: (modelID: string | ModelID) => import("../schema/options.js").LanguageModel<import("./open-responses-options.js").OpenResponsesOptionsInput>;
249
+ model: (modelID: string | ModelID) => import("../schema/options.js").LanguageModel<import("./openai-options.js").OpenAIOptionsInput>;
250
+ chat: (modelID: string | ModelID) => import("../schema/options.js").LanguageModel<import("./openai-options.js").OpenAIOptionsInput>;
251
+ responses: (modelID: string | ModelID) => import("../schema/options.js").LanguageModel<import("./openai-options.js").OpenAIOptionsInput>;
252
252
  configure: (input?: Config) => /*elided*/ any;
253
253
  };
254
254
  export declare const provider: {
255
255
  id: string & import("effect/Brand").Brand<"AI.ProviderID">;
256
- model: (modelID: string | ModelID) => import("../schema/options.js").LanguageModel<import("./open-responses-options.js").OpenResponsesOptionsInput>;
257
- chat: (modelID: string | ModelID) => import("../schema/options.js").LanguageModel<import("./open-responses-options.js").OpenResponsesOptionsInput>;
258
- responses: (modelID: string | ModelID) => import("../schema/options.js").LanguageModel<import("./open-responses-options.js").OpenResponsesOptionsInput>;
256
+ model: (modelID: string | ModelID) => import("../schema/options.js").LanguageModel<import("./openai-options.js").OpenAIOptionsInput>;
257
+ chat: (modelID: string | ModelID) => import("../schema/options.js").LanguageModel<import("./openai-options.js").OpenAIOptionsInput>;
258
+ responses: (modelID: string | ModelID) => import("../schema/options.js").LanguageModel<import("./openai-options.js").OpenAIOptionsInput>;
259
259
  configure: (input?: Config) => {
260
260
  id: string & import("effect/Brand").Brand<"AI.ProviderID">;
261
- model: (modelID: string | ModelID) => import("../schema/options.js").LanguageModel<import("./open-responses-options.js").OpenResponsesOptionsInput>;
262
- chat: (modelID: string | ModelID) => import("../schema/options.js").LanguageModel<import("./open-responses-options.js").OpenResponsesOptionsInput>;
263
- responses: (modelID: string | ModelID) => import("../schema/options.js").LanguageModel<import("./open-responses-options.js").OpenResponsesOptionsInput>;
261
+ model: (modelID: string | ModelID) => import("../schema/options.js").LanguageModel<import("./openai-options.js").OpenAIOptionsInput>;
262
+ chat: (modelID: string | ModelID) => import("../schema/options.js").LanguageModel<import("./openai-options.js").OpenAIOptionsInput>;
263
+ responses: (modelID: string | ModelID) => import("../schema/options.js").LanguageModel<import("./openai-options.js").OpenAIOptionsInput>;
264
264
  configure: /*elided*/ any;
265
265
  };
266
266
  };
267
267
  export declare const chatModel: ProviderPackage.Definition<Settings, OpenAIProviderOptionsInput>["model"];
268
268
  export declare const responsesModel: ProviderPackage.Definition<Settings, OpenAIProviderOptionsInput>["model"];
269
- export declare const model: (modelID: string, settings: Settings) => import("../schema/options.js").LanguageModel<import("./open-responses-options.js").OpenResponsesOptionsInput>;
269
+ export declare const model: (modelID: string, settings: Settings) => import("../schema/options.js").LanguageModel<import("./openai-options.js").OpenAIOptionsInput>;