@opencode/ai 2.1.4 → 2.2.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.
Files changed (269) hide show
  1. package/README.md +283 -114
  2. package/dist/ai-client.d.ts +2 -2
  3. package/dist/ai-client.js +2 -1
  4. package/dist/cache-policy.d.ts +4 -0
  5. package/dist/cache-policy.js +20 -6
  6. package/dist/decision-client.d.ts +21 -0
  7. package/dist/decision-client.js +55 -0
  8. package/dist/decision.d.ts +472 -0
  9. package/dist/decision.js +158 -0
  10. package/dist/effect.d.ts +38 -0
  11. package/dist/effect.js +27 -0
  12. package/dist/effort-updates.d.ts +2 -2
  13. package/dist/experimental/cloudflare-run-extraction.d.ts +13 -0
  14. package/dist/experimental/cloudflare-run-extraction.js +83 -0
  15. package/dist/experimental/extraction.d.ts +183 -0
  16. package/dist/experimental/extraction.js +102 -0
  17. package/dist/experimental/realtime.d.ts +7 -2
  18. package/dist/experimental/realtime.js +5 -5
  19. package/dist/experimental.d.ts +2 -3
  20. package/dist/experimental.js +1 -2
  21. package/dist/image-client.d.ts +5 -14
  22. package/dist/image-client.js +0 -1
  23. package/dist/image.d.ts +6 -176
  24. package/dist/image.js +4 -11
  25. package/dist/index.d.ts +1426 -34
  26. package/dist/index.js +127 -24
  27. package/dist/llm.d.ts +16 -1
  28. package/dist/llm.js +34 -0
  29. package/dist/media-client.d.ts +2 -1
  30. package/dist/media-model.d.ts +3 -0
  31. package/dist/media-model.js +4 -1
  32. package/dist/media.d.ts +0 -7
  33. package/dist/media.js +0 -6
  34. package/dist/protocols/alibaba-chat.d.ts +20 -0
  35. package/dist/protocols/alibaba-images.d.ts +22 -0
  36. package/dist/protocols/alibaba-images.js +77 -0
  37. package/dist/protocols/alibaba-messages.d.ts +52 -17
  38. package/dist/protocols/alibaba-responses.d.ts +6 -3
  39. package/dist/protocols/anthropic-messages.d.ts +160 -54
  40. package/dist/protocols/anthropic-messages.js +44 -13
  41. package/dist/protocols/bedrock-converse.d.ts +110 -15
  42. package/dist/protocols/bedrock-converse.js +112 -15
  43. package/dist/protocols/bedrock-event-stream.js +1 -0
  44. package/dist/protocols/bfl-images.d.ts +9 -0
  45. package/dist/protocols/bfl-images.js +17 -15
  46. package/dist/protocols/cartesia-speech.d.ts +0 -18
  47. package/dist/protocols/cloudflare-run-decisions.d.ts +9 -0
  48. package/dist/protocols/cloudflare-run-decisions.js +36 -0
  49. package/dist/protocols/cloudflare-workers-ai-images.d.ts +23 -0
  50. package/dist/protocols/cloudflare-workers-ai-images.js +80 -0
  51. package/dist/protocols/deepgram-speech.d.ts +0 -18
  52. package/dist/protocols/elevenlabs-speech.d.ts +0 -18
  53. package/dist/protocols/fal-images.d.ts +10 -1
  54. package/dist/protocols/fal-images.js +16 -25
  55. package/dist/protocols/fal-video.js +1 -1
  56. package/dist/protocols/gemini.d.ts +3 -0
  57. package/dist/protocols/gemini.js +11 -6
  58. package/dist/protocols/google-images.d.ts +2 -2
  59. package/dist/protocols/google-images.js +8 -37
  60. package/dist/protocols/google-interactions.d.ts +4 -0
  61. package/dist/protocols/google-interactions.js +12 -5
  62. package/dist/protocols/google-speech.d.ts +0 -18
  63. package/dist/protocols/google-speech.js +0 -1
  64. package/dist/protocols/google-transcription.d.ts +0 -18
  65. package/dist/protocols/google-transcription.js +0 -1
  66. package/dist/protocols/google-video.d.ts +1 -1
  67. package/dist/protocols/google-video.js +2 -7
  68. package/dist/protocols/index.d.ts +3 -0
  69. package/dist/protocols/index.js +3 -0
  70. package/dist/protocols/meta-images.d.ts +7 -1
  71. package/dist/protocols/meta-images.js +10 -8
  72. package/dist/protocols/meta-messages.d.ts +52 -17
  73. package/dist/protocols/meta-messages.js +14 -1
  74. package/dist/protocols/meta-responses.d.ts +6 -0
  75. package/dist/protocols/meta-responses.js +29 -1
  76. package/dist/protocols/open-responses.d.ts +30 -0
  77. package/dist/protocols/open-responses.js +8 -18
  78. package/dist/protocols/openai-chat.d.ts +713 -1
  79. package/dist/protocols/openai-chat.js +35 -22
  80. package/dist/protocols/openai-compatible-chat.d.ts +22 -1
  81. package/dist/protocols/openai-compatible-responses.d.ts +8 -5
  82. package/dist/protocols/openai-decisions.d.ts +22 -0
  83. package/dist/protocols/openai-decisions.js +128 -0
  84. package/dist/protocols/openai-images.d.ts +21 -24
  85. package/dist/protocols/openai-images.js +100 -34
  86. package/dist/protocols/openai-responses.d.ts +82 -24
  87. package/dist/protocols/openai-responses.js +32 -2
  88. package/dist/protocols/openai-speech.d.ts +0 -18
  89. package/dist/protocols/openai-speech.js +15 -5
  90. package/dist/protocols/openai-transcription.d.ts +0 -18
  91. package/dist/protocols/openai-transcription.js +3 -4
  92. package/dist/protocols/replicate-images.js +1 -1
  93. package/dist/protocols/runway-video.d.ts +1 -1
  94. package/dist/protocols/runway-video.js +1 -1
  95. package/dist/protocols/shared.d.ts +9 -1
  96. package/dist/protocols/shared.js +13 -2
  97. package/dist/protocols/stability-images.d.ts +5 -0
  98. package/dist/protocols/stability-images.js +4 -9
  99. package/dist/protocols/system-one.d.ts +101 -0
  100. package/dist/protocols/system-one.js +230 -0
  101. package/dist/protocols/utils/bedrock-cache.d.ts +2 -2
  102. package/dist/protocols/utils/bedrock-cache.js +7 -6
  103. package/dist/protocols/utils/cache.d.ts +14 -3
  104. package/dist/protocols/utils/cache.js +11 -4
  105. package/dist/protocols/utils/gemini-generate-content.d.ts +3 -3
  106. package/dist/protocols/utils/gemini-generate-content.js +10 -16
  107. package/dist/protocols/utils/gemini-json-schema.d.ts +5 -0
  108. package/dist/protocols/utils/gemini-json-schema.js +3 -3
  109. package/dist/protocols/utils/image-sizing.d.ts +31 -0
  110. package/dist/protocols/utils/image-sizing.js +73 -0
  111. package/dist/protocols/utils/media-input.d.ts +4 -5
  112. package/dist/protocols/utils/media-input.js +8 -5
  113. package/dist/protocols/utils/speech-stream.d.ts +0 -1
  114. package/dist/protocols/utils/speech-stream.js +0 -1
  115. package/dist/protocols/utils/tool-schema.js +15 -5
  116. package/dist/protocols/utils/tool-stream.d.ts +9 -0
  117. package/dist/protocols/venice-chat.d.ts +19 -0
  118. package/dist/protocols/venice-chat.js +1 -1
  119. package/dist/protocols/xai-images.d.ts +1 -2
  120. package/dist/protocols/xai-images.js +9 -2
  121. package/dist/protocols/xai-responses.d.ts +10 -0
  122. package/dist/protocols/xai-responses.js +28 -2
  123. package/dist/protocols/xai-video.d.ts +1 -1
  124. package/dist/protocols/xai-video.js +11 -8
  125. package/dist/protocols/zai-chat.d.ts +21 -0
  126. package/dist/protocols/zai-images.d.ts +3 -1
  127. package/dist/protocols/zai-images.js +12 -11
  128. package/dist/protocols/zai-messages.d.ts +52 -17
  129. package/dist/provider-error.js +19 -4
  130. package/dist/providers/alibaba/images.d.ts +2 -0
  131. package/dist/providers/alibaba/images.js +1 -0
  132. package/dist/providers/alibaba.d.ts +79 -18
  133. package/dist/providers/alibaba.js +16 -8
  134. package/dist/providers/amazon-bedrock-mantle.d.ts +197 -138
  135. package/dist/providers/amazon-bedrock.d.ts +34 -14
  136. package/dist/providers/amazon-bedrock.js +11 -11
  137. package/dist/providers/anthropic-compatible.d.ts +52 -17
  138. package/dist/providers/anthropic.d.ts +52 -17
  139. package/dist/providers/azure/images.d.ts +2 -0
  140. package/dist/providers/azure/images.js +1 -0
  141. package/dist/providers/azure.d.ts +50 -9
  142. package/dist/providers/azure.js +19 -1
  143. package/dist/providers/baseten/decisions.d.ts +2 -0
  144. package/dist/providers/baseten/decisions.js +1 -0
  145. package/dist/providers/baseten.d.ts +51 -1
  146. package/dist/providers/baseten.js +41 -8
  147. package/dist/providers/cerebras.d.ts +42 -0
  148. package/dist/providers/cloudflare-ai-gateway/decisions.d.ts +2 -0
  149. package/dist/providers/cloudflare-ai-gateway/decisions.js +1 -0
  150. package/dist/providers/cloudflare-ai-gateway.d.ts +177 -43
  151. package/dist/providers/cloudflare-ai-gateway.js +32 -4
  152. package/dist/providers/cloudflare-workers-ai/decisions.d.ts +2 -0
  153. package/dist/providers/cloudflare-workers-ai/decisions.js +1 -0
  154. package/dist/providers/cloudflare-workers-ai/images.d.ts +2 -0
  155. package/dist/providers/cloudflare-workers-ai/images.js +1 -0
  156. package/dist/providers/cloudflare-workers-ai.d.ts +55 -1
  157. package/dist/providers/cloudflare-workers-ai.js +45 -9
  158. package/dist/providers/cohere.d.ts +42 -0
  159. package/dist/providers/deepinfra.d.ts +42 -0
  160. package/dist/providers/deepseek.d.ts +42 -0
  161. package/dist/providers/digitalocean/decisions.d.ts +2 -0
  162. package/dist/providers/digitalocean/decisions.js +1 -0
  163. package/dist/providers/digitalocean/images.d.ts +2 -0
  164. package/dist/providers/digitalocean/images.js +1 -0
  165. package/dist/providers/digitalocean.d.ts +72 -1
  166. package/dist/providers/digitalocean.js +39 -8
  167. package/dist/providers/fireworks.d.ts +42 -0
  168. package/dist/providers/google/images.d.ts +2 -0
  169. package/dist/providers/google/images.js +1 -0
  170. package/dist/providers/google-vertex/images.d.ts +2 -0
  171. package/dist/providers/google-vertex/images.js +1 -0
  172. package/dist/providers/google-vertex-chat.d.ts +21 -0
  173. package/dist/providers/google-vertex-messages.d.ts +52 -17
  174. package/dist/providers/google-vertex-responses.d.ts +3 -0
  175. package/dist/providers/google-vertex.d.ts +3 -1
  176. package/dist/providers/google-vertex.js +30 -13
  177. package/dist/providers/google.d.ts +5 -4
  178. package/dist/providers/google.js +15 -8
  179. package/dist/providers/groq.d.ts +42 -4
  180. package/dist/providers/groq.js +0 -6
  181. package/dist/providers/meta.d.ts +197 -138
  182. package/dist/providers/minimax.d.ts +76 -17
  183. package/dist/providers/moonshot.d.ts +76 -17
  184. package/dist/providers/openai/images.d.ts +2 -0
  185. package/dist/providers/openai/images.js +1 -0
  186. package/dist/providers/openai-compatible/images.d.ts +2 -0
  187. package/dist/providers/openai-compatible/images.js +1 -0
  188. package/dist/providers/openai-compatible-responses.d.ts +3 -0
  189. package/dist/providers/openai-compatible.d.ts +24 -1
  190. package/dist/providers/openai-compatible.js +21 -9
  191. package/dist/providers/openai-options.d.ts +3 -1
  192. package/dist/providers/openai.d.ts +46 -9
  193. package/dist/providers/openai.js +20 -4
  194. package/dist/providers/opencode/decisions.d.ts +2 -0
  195. package/dist/providers/opencode/decisions.js +1 -0
  196. package/dist/providers/opencode.d.ts +9 -10
  197. package/dist/providers/opencode.js +10 -8
  198. package/dist/providers/openrouter/decisions.d.ts +2 -0
  199. package/dist/providers/openrouter/decisions.js +1 -0
  200. package/dist/providers/openrouter/images.d.ts +2 -0
  201. package/dist/providers/openrouter/images.js +1 -0
  202. package/dist/providers/openrouter.d.ts +92 -11
  203. package/dist/providers/openrouter.js +38 -16
  204. package/dist/providers/togetherai.d.ts +42 -0
  205. package/dist/providers/typesafe-ai/decisions.d.ts +2 -0
  206. package/dist/providers/typesafe-ai/decisions.js +1 -0
  207. package/dist/providers/typesafe-ai.d.ts +9 -10
  208. package/dist/providers/typesafe-ai.js +10 -8
  209. package/dist/providers/venice.d.ts +19 -0
  210. package/dist/providers/vercel-ai-gateway/decisions.d.ts +2 -0
  211. package/dist/providers/vercel-ai-gateway/decisions.js +1 -0
  212. package/dist/providers/vercel-ai-gateway/images.d.ts +2 -0
  213. package/dist/providers/vercel-ai-gateway/images.js +1 -0
  214. package/dist/providers/vercel-ai-gateway.d.ts +10 -16
  215. package/dist/providers/vercel-ai-gateway.js +31 -85
  216. package/dist/providers/xai/images.d.ts +2 -0
  217. package/dist/providers/xai/images.js +1 -0
  218. package/dist/providers/xai.d.ts +28 -5
  219. package/dist/providers/xai.js +15 -8
  220. package/dist/providers/zai/images.d.ts +2 -0
  221. package/dist/providers/zai/images.js +1 -0
  222. package/dist/providers/zai-coding-plan.d.ts +76 -17
  223. package/dist/providers/zai.d.ts +26 -4
  224. package/dist/providers/zai.js +15 -8
  225. package/dist/route/client.d.ts +2 -2
  226. package/dist/route/client.js +26 -4
  227. package/dist/route/decision.d.ts +58 -0
  228. package/dist/route/decision.js +75 -0
  229. package/dist/route/executor.d.ts +1 -1
  230. package/dist/route/framing.d.ts +2 -0
  231. package/dist/route/index.d.ts +3 -0
  232. package/dist/route/index.js +3 -0
  233. package/dist/route/media-protocol.d.ts +1 -0
  234. package/dist/route/media-protocol.js +12 -8
  235. package/dist/route/media.d.ts +5 -1
  236. package/dist/route/media.js +6 -2
  237. package/dist/route/transport/http.js +1 -0
  238. package/dist/route/transport/index.d.ts +1 -0
  239. package/dist/schema/events.d.ts +148 -6
  240. package/dist/schema/events.js +20 -0
  241. package/dist/schema/messages.d.ts +6 -2
  242. package/dist/schema/messages.js +3 -0
  243. package/dist/schema/options.d.ts +5 -3
  244. package/dist/schema/options.js +8 -3
  245. package/dist/speech-client.d.ts +5 -14
  246. package/dist/speech-client.js +0 -1
  247. package/dist/speech.d.ts +0 -135
  248. package/dist/speech.js +2 -4
  249. package/dist/testing.d.ts +25 -24
  250. package/dist/testing.js +3 -21
  251. package/dist/transcription-client.d.ts +5 -14
  252. package/dist/transcription.d.ts +0 -135
  253. package/dist/transcription.js +1 -1
  254. package/dist/utils/media-type.js +22 -1
  255. package/dist/video-client.d.ts +5 -14
  256. package/dist/video-client.js +0 -1
  257. package/dist/video.d.ts +1 -136
  258. package/dist/video.js +3 -5
  259. package/package.json +513 -11
  260. package/dist/experimental/evaluation-client.d.ts +0 -21
  261. package/dist/experimental/evaluation-client.js +0 -64
  262. package/dist/experimental/evaluation.d.ts +0 -242
  263. package/dist/experimental/evaluation.js +0 -108
  264. package/dist/experimental/system-one.d.ts +0 -16
  265. package/dist/experimental/system-one.js +0 -135
  266. package/dist/promise.d.ts +0 -1021
  267. package/dist/promise.js +0 -101
  268. package/dist/provider.d.ts +0 -23
  269. package/dist/provider.js +0 -2
@@ -9,7 +9,9 @@ import { Protocol } from "../route/protocol.js";
9
9
  import { AIError, AIErrorReason, InvalidProviderOutputError, LLMEvent, ProviderInternalError, UnknownProviderError, Usage, } from "../schema/index.js";
10
10
  import { classifyProviderFailure } from "../provider-error.js";
11
11
  import { isRecord, JsonObject, optionalArray, optionalNull, ProviderShared } from "./shared.js";
12
- import { cacheControl } from "./utils/cache.js";
12
+ import { placesMarkers, requestTTL } from "../cache-policy.js";
13
+ import * as Cache from "./utils/cache.js";
14
+ import { resolveParallelToolCalls } from "./open-responses.js";
13
15
  import { OpenResponsesOptions } from "./utils/open-responses-options.js";
14
16
  import { Lifecycle } from "./utils/lifecycle.js";
15
17
  import { ToolStream } from "./utils/tool-stream.js";
@@ -90,6 +92,10 @@ const OpenAIChatUserContent = Schema.Union([
90
92
  type: Schema.Literal("image_url"),
91
93
  image_url: Schema.Struct({ url: Schema.String }),
92
94
  }),
95
+ Schema.Struct({
96
+ type: Schema.Literal("video_url"),
97
+ video_url: Schema.Struct({ url: Schema.String }),
98
+ }),
93
99
  Schema.Struct({
94
100
  type: Schema.Literal("file"),
95
101
  file: Schema.Struct({ filename: Schema.String, file_data: Schema.String }),
@@ -135,7 +141,14 @@ export const bodyFields = {
135
141
  stream_options: Schema.optional(Schema.Struct({ include_usage: Schema.Boolean })),
136
142
  store: Schema.optional(Schema.Boolean),
137
143
  prompt_cache_key: Schema.optional(Schema.String),
144
+ prompt_cache_retention: Schema.optional(Schema.String),
145
+ prompt_cache_options: Schema.optional(Schema.Struct({ ttl: Schema.optional(Schema.String) })),
138
146
  reasoning_effort: Schema.optional(OpenResponsesOptions.ReasoningEffort),
147
+ verbosity: Schema.optional(OpenResponsesOptions.TextVerbositySchema),
148
+ service_tier: Schema.optional(OpenResponsesOptions.ServiceTierSchema),
149
+ parallel_tool_calls: Schema.optional(Schema.Boolean),
150
+ metadata: Schema.optional(Schema.Record(Schema.String, Schema.String)),
151
+ safety_identifier: Schema.optional(Schema.String),
139
152
  tool_stream: Schema.optional(Schema.Boolean),
140
153
  max_completion_tokens: Schema.optional(Schema.Number),
141
154
  max_tokens: Schema.optional(Schema.Number),
@@ -244,9 +257,11 @@ const lowerMedia = Effect.fnUntraced(function* (part) {
244
257
  file_data: (yield* ProviderShared.requireInlineMedia("OpenAI Chat", part.media)).dataUrl,
245
258
  },
246
259
  };
247
- if (part.media.kind !== "image")
260
+ if (part.media.kind !== "image" && part.media.kind !== "video")
248
261
  return yield* ProviderShared.invalidRequest(`OpenAI Chat does not support media type ${part.media.mediaType}`);
249
262
  const url = ProviderShared.mediaUrl(part.media) ?? (yield* ProviderShared.requireInlineMedia("OpenAI Chat", part.media)).dataUrl;
263
+ if (part.media.kind === "video")
264
+ return { type: "video_url", video_url: { url } };
250
265
  return { type: "image_url", image_url: { url } };
251
266
  });
252
267
  const openAICompatibleReasoningContent = (native) => isRecord(native) && typeof native.reasoning_content === "string" ? native.reasoning_content : undefined;
@@ -587,8 +602,10 @@ const detectZaiToolStream = (provider, baseURL, modelID) => {
587
602
  return false;
588
603
  return true;
589
604
  };
590
- const lowerOptions = (request, supportsStore) => {
605
+ const lowerOptions = (request, supportsStore, hasActiveTools) => {
591
606
  const options = OpenResponsesOptions.resolve(request);
607
+ // OpenAI rejects `parallel_tool_calls` unless tools are present.
608
+ const parallelToolCalls = hasActiveTools ? resolveParallelToolCalls(request, options) : undefined;
592
609
  // Default off: strict providers 400 on unknown body fields, so only send
593
610
  // the key where compatibility explicitly allows it. Header-based affinity
594
611
  // (x-session-affinity, x-grok-conv-id, ...) is unaffected.
@@ -596,7 +613,14 @@ const lowerOptions = (request, supportsStore) => {
596
613
  return {
597
614
  ...(supportsStore ? { store: options.store ?? false } : {}),
598
615
  ...(cacheKey ? { prompt_cache_key: cacheKey } : {}),
616
+ // Inline markers carry the lifetime when the policy places them.
617
+ ...(placesMarkers(request) ? {} : Cache.openAIRequestTTL(requestTTL(request))),
599
618
  ...(options.reasoningEffort ? { reasoning_effort: options.reasoningEffort } : {}),
619
+ ...(options.textVerbosity ? { verbosity: options.textVerbosity } : {}),
620
+ ...(options.serviceTier ? { service_tier: options.serviceTier } : {}),
621
+ ...(parallelToolCalls !== undefined ? { parallel_tool_calls: parallelToolCalls } : {}),
622
+ ...(options.metadata ? { metadata: options.metadata } : {}),
623
+ ...(options.safetyIdentifier ? { safety_identifier: options.safetyIdentifier } : {}),
600
624
  };
601
625
  };
602
626
  export const fromRequest = Effect.fn("OpenAIChat.fromRequest")(function* (request, options = {}) {
@@ -638,11 +662,11 @@ export const fromRequest = Effect.fn("OpenAIChat.fromRequest")(function* (reques
638
662
  presence_penalty: generation?.presencePenalty,
639
663
  seed: generation?.seed,
640
664
  stop: generation?.stop,
641
- ...lowerOptions(request, supportsStore),
665
+ ...lowerOptions(request, supportsStore, hasActiveTools),
642
666
  };
643
667
  });
644
668
  // Provider wrappers that pass their own marker writer keep it; otherwise a declared cache format adds one.
645
- const declaredCacheControl = (request) => request.model.compatibility?.cacheFormat === undefined ? undefined : cacheControl();
669
+ const declaredCacheControl = (request) => request.model.compatibility?.cacheFormat === undefined ? undefined : Cache.cacheControl();
646
670
  // =============================================================================
647
671
  // Stream Parsing
648
672
  // =============================================================================
@@ -676,12 +700,7 @@ const mapFinishReason = Effect.fnUntraced(function* (event, reason) {
676
700
  case "tool_calls":
677
701
  return "tool-calls";
678
702
  default:
679
- return yield* new AIError({
680
- reason: new UnknownProviderError({
681
- message: `Provider finish_reason: ${reason}`,
682
- body: ProviderShared.encodeJson(event),
683
- }),
684
- });
703
+ return "unknown";
685
704
  }
686
705
  });
687
706
  // OpenAI Chat reports `prompt_tokens` (inclusive total) with a
@@ -714,6 +733,7 @@ const mapUsage = (usage, providerMetadataKey) => {
714
733
  cacheWriteInputTokens: cacheWrite,
715
734
  reasoningTokens: reasoning,
716
735
  totalTokens: ProviderShared.totalTokens(input, output, usage.total_tokens ?? undefined),
736
+ ...ProviderShared.reportedBilling(usage),
717
737
  providerMetadata: { [providerMetadataKey]: usage },
718
738
  });
719
739
  };
@@ -839,16 +859,9 @@ const step = (state, event) => Effect.gen(function* () {
839
859
  let nextToolIndex = state.nextToolIndex;
840
860
  let lifecycle = state.lifecycle;
841
861
  const reasoning = reasoningDelta(delta, state.reasoningField);
842
- const hasLateContent = Boolean(delta?.content) ||
843
- Boolean(delta?.refusal) ||
844
- reasoning !== undefined ||
845
- (Array.isArray(delta?.reasoning_details) && delta.reasoning_details.length > 0) ||
846
- toolDeltas.some((tool) => Boolean(tool.id) || Boolean(tool.function?.name) || Boolean(tool.function?.arguments));
847
- if (state.finishReason !== undefined) {
848
- if (hasLateContent)
849
- return yield* ProviderShared.eventError(ADAPTER, "OpenAI Chat received content after the finish reason", ProviderShared.encodeJson(event));
850
- return [{ ...state, usage }, events];
851
- }
862
+ if (state.finishReason !== undefined &&
863
+ toolDeltas.some((tool) => Boolean(tool.id) || Boolean(tool.function?.name) || Boolean(tool.function?.arguments)))
864
+ return yield* ProviderShared.eventError(ADAPTER, "OpenAI Chat received content after the finish reason", ProviderShared.encodeJson(event));
852
865
  const reasoningField = state.reasoningField ?? reasoning?.field;
853
866
  const reasoningTextObserved = state.reasoningTextObserved || reasoning !== undefined;
854
867
  const detailDelta = Array.isArray(delta?.reasoning_details)
@@ -941,7 +954,7 @@ const step = (state, event) => Effect.gen(function* () {
941
954
  pendingTools,
942
955
  toolCallEvents: finished?.events ?? state.toolCallEvents,
943
956
  usage,
944
- finishReason,
957
+ finishReason: state.finishReason ?? finishReason,
945
958
  lifecycle,
946
959
  reasoningField,
947
960
  reasoningTextObserved,
@@ -15,6 +15,11 @@ export declare const route: Route<{
15
15
  readonly image_url: {
16
16
  readonly url: string;
17
17
  };
18
+ } | {
19
+ readonly type: "video_url";
20
+ readonly video_url: {
21
+ readonly url: string;
22
+ };
18
23
  } | {
19
24
  readonly type: "file";
20
25
  readonly file: {
@@ -36,6 +41,11 @@ export declare const route: Route<{
36
41
  readonly image_url: {
37
42
  readonly url: string;
38
43
  };
44
+ } | {
45
+ readonly type: "video_url";
46
+ readonly video_url: {
47
+ readonly url: string;
48
+ };
39
49
  } | {
40
50
  readonly type: "file";
41
51
  readonly file: {
@@ -117,7 +127,18 @@ export declare const route: Route<{
117
127
  } | undefined;
118
128
  readonly store?: boolean | undefined;
119
129
  readonly prompt_cache_key?: string | undefined;
120
- readonly reasoning_effort?: import("../index.js").ReasoningEffort | undefined;
130
+ readonly prompt_cache_retention?: string | undefined;
131
+ readonly prompt_cache_options?: {
132
+ readonly ttl?: string | undefined;
133
+ } | undefined;
134
+ readonly reasoning_effort?: import("../effect.js").ReasoningEffort | undefined;
135
+ readonly verbosity?: import("../schema/options.js").OpenString<"high" | "low" | "medium"> | undefined;
136
+ readonly service_tier?: import("../schema/options.js").OpenString<"auto" | "default" | "flex" | "priority"> | undefined;
137
+ readonly parallel_tool_calls?: boolean | undefined;
138
+ readonly metadata?: {
139
+ readonly [x: string]: string;
140
+ } | undefined;
141
+ readonly safety_identifier?: string | undefined;
121
142
  readonly tool_stream?: boolean | undefined;
122
143
  readonly max_completion_tokens?: number | undefined;
123
144
  readonly max_tokens?: number | undefined;
@@ -9,7 +9,7 @@ export declare const route: Route<{
9
9
  readonly input: readonly ({
10
10
  readonly type: "configuration_update";
11
11
  readonly reasoning: {
12
- readonly effort: import("../index.js").ReasoningEffort;
12
+ readonly effort: import("../effect.js").ReasoningEffort;
13
13
  };
14
14
  } | {
15
15
  readonly type: "message";
@@ -57,6 +57,9 @@ export declare const route: Route<{
57
57
  readonly content: readonly ({
58
58
  readonly type: "input_text";
59
59
  readonly text: string;
60
+ } | {
61
+ readonly type: "input_video";
62
+ readonly video_url: string;
60
63
  } | {
61
64
  readonly type: "input_image";
62
65
  readonly image_url: string;
@@ -163,15 +166,15 @@ export declare const route: Route<{
163
166
  } | undefined;
164
167
  readonly top_logprobs?: number | undefined;
165
168
  readonly truncation?: "auto" | "disabled" | undefined;
166
- readonly service_tier?: import("../index.js").OpenString<"auto" | "default" | "flex" | "priority"> | undefined;
169
+ readonly service_tier?: import("../schema/options.js").OpenString<"auto" | "default" | "flex" | "priority"> | undefined;
167
170
  readonly prompt_cache_key?: string | undefined;
168
- readonly include?: readonly import("../index.js").OpenString<"code_interpreter_call.outputs" | "computer_call_output.output.image_url" | "file_search_call.results" | "message.input_image.image_url" | "message.output_text.logprobs" | "reasoning.encrypted_content" | "web_search_call.action.sources" | "web_search_call.results">[] | undefined;
171
+ readonly include?: readonly import("../schema/options.js").OpenString<"code_interpreter_call.outputs" | "computer_call_output.output.image_url" | "file_search_call.results" | "message.input_image.image_url" | "message.output_text.logprobs" | "reasoning.encrypted_content" | "web_search_call.action.sources" | "web_search_call.results">[] | undefined;
169
172
  readonly reasoning?: {
170
- readonly effort?: import("../index.js").ReasoningEffort | undefined;
173
+ readonly effort?: import("../effect.js").ReasoningEffort | undefined;
171
174
  readonly summary?: "auto" | "concise" | "detailed" | undefined;
172
175
  } | undefined;
173
176
  readonly text?: {
174
- readonly verbosity?: import("../index.js").OpenString<"high" | "low" | "medium"> | undefined;
177
+ readonly verbosity?: import("../schema/options.js").OpenString<"high" | "low" | "medium"> | undefined;
175
178
  } | undefined;
176
179
  readonly max_output_tokens?: number | undefined;
177
180
  readonly max_tool_calls?: number | undefined;
@@ -0,0 +1,22 @@
1
+ import { Schema } from "effect";
2
+ import { DecisionRoute } from "../route/decision.js";
3
+ import { type ProviderOptions } from "../schema/index.js";
4
+ export declare const DEFAULT_BASE_URL = "https://api.openai.com/v1";
5
+ export declare const PATH = "/decisions";
6
+ declare const Options: Schema.Struct<{
7
+ readonly promptCacheKey: Schema.optional<Schema.String>;
8
+ readonly promptCacheOptions: Schema.optional<Schema.Struct<{
9
+ readonly mode: Schema.optional<Schema.String>;
10
+ readonly ttl: Schema.optional<Schema.String>;
11
+ }>>;
12
+ readonly safetyIdentifier: Schema.optional<Schema.String>;
13
+ }>;
14
+ /** Known fields map to their wire names; anything else is sent as given. */
15
+ export type OptionsInput = ProviderOptions & typeof Options.Encoded;
16
+ export declare const protocol: DecisionRoute.Protocol;
17
+ export declare const route: DecisionRoute.Route;
18
+ export declare const OpenAIDecisions: {
19
+ readonly protocol: DecisionRoute.Protocol;
20
+ readonly route: DecisionRoute.Route;
21
+ };
22
+ export {};
@@ -0,0 +1,128 @@
1
+ import { Effect, Schema } from "effect";
2
+ import { DecisionResponse, DecisionUsage } from "../decision.js";
3
+ import { DecisionRoute } from "../route/decision.js";
4
+ import { Endpoint } from "../route/endpoint.js";
5
+ import { MediaProtocol } from "../route/media-protocol.js";
6
+ import { AIError, UnsupportedOperationError, mergeJsonRecords } from "../schema/index.js";
7
+ import { encodeJson } from "../utils/json.js";
8
+ import { OpenResponses } from "./open-responses.js";
9
+ import { ProviderShared } from "./shared.js";
10
+ const ID = "openai-decisions";
11
+ const NAME = "OpenAI Decisions";
12
+ export const DEFAULT_BASE_URL = "https://api.openai.com/v1";
13
+ export const PATH = "/decisions";
14
+ const Options = Schema.Struct({
15
+ promptCacheKey: Schema.optional(Schema.String),
16
+ promptCacheOptions: Schema.optional(Schema.Struct({ mode: Schema.optional(Schema.String), ttl: Schema.optional(Schema.String) })),
17
+ safetyIdentifier: Schema.optional(Schema.String),
18
+ });
19
+ const Probability = Schema.Number.check(Schema.isBetween({ minimum: 0, maximum: 1 }));
20
+ const ChoiceValue = Schema.Union([Schema.String, Schema.Boolean]);
21
+ const Answer = Schema.Union([
22
+ Schema.Struct({ type: Schema.Literal("predicate"), name: Schema.String, probability: Probability }),
23
+ Schema.Struct({
24
+ type: Schema.Literal("choice"),
25
+ name: Schema.String,
26
+ choice: ChoiceValue,
27
+ probabilities: Schema.Array(Schema.Struct({ value: ChoiceValue, probability: Probability })),
28
+ confidence: Probability,
29
+ }),
30
+ Schema.Struct({
31
+ type: Schema.Literal("score"),
32
+ name: Schema.String,
33
+ score: Schema.Number,
34
+ probabilities: Schema.Array(Schema.Struct({ value: Schema.Int, label: Schema.String, probability: Probability })),
35
+ confidence: Probability,
36
+ }),
37
+ Schema.Struct({ type: Schema.Literal("refusal"), name: Schema.String, message: Schema.optional(Schema.String) }),
38
+ ]);
39
+ const Response = Schema.Struct({
40
+ model: Schema.String,
41
+ answers: Schema.Array(Answer),
42
+ usage: Schema.optional(OpenResponses.OpenResponsesUsage),
43
+ });
44
+ const decodeOptions = ProviderShared.validateWith(Schema.decodeUnknownEffect(Options));
45
+ const fromRequest = Effect.fn("OpenAIDecisions.fromRequest")(function* (request) {
46
+ const options = yield* decodeOptions(request.providerOptions ?? {});
47
+ const { promptCacheKey: _, promptCacheOptions: __, safetyIdentifier: ___, ...native } = request.providerOptions ?? {};
48
+ return {
49
+ ...mergeJsonRecords({
50
+ prompt_cache_key: options.promptCacheKey,
51
+ prompt_cache_options: options.promptCacheOptions,
52
+ safety_identifier: options.safetyIdentifier,
53
+ ...native,
54
+ }, request.http?.body),
55
+ model: request.model.id,
56
+ input: [{ role: "user", content: yield* Effect.forEach(request.input, (part) => inputPart(request, part)) }],
57
+ questions: request.questions,
58
+ };
59
+ });
60
+ // `input_json` takes an object or a list of objects; other JSON values go as text.
61
+ const inputPart = (request, part) => {
62
+ if (part.type === "text")
63
+ return Effect.succeed({ type: "input_text", text: part.text });
64
+ if (part.type === "json")
65
+ return Effect.succeed(isObject(part.value) || (Array.isArray(part.value) && part.value.every(isObject))
66
+ ? { type: "input_json", data: part.value }
67
+ : { type: "input_text", text: encodeJson(part.value) });
68
+ if (part.media.kind !== "image")
69
+ return Effect.fail(new AIError({
70
+ reason: new UnsupportedOperationError({
71
+ operation: "decision.media",
72
+ provider: request.model.provider,
73
+ route: request.model.route.id,
74
+ message: `${NAME} takes images only; got ${part.media.kind} media`,
75
+ }),
76
+ }));
77
+ return ProviderShared.mediaReference(part.media, undefined, `${NAME} image input`).pipe(Effect.map((reference) => ({ type: "input_image", image_url: reference.value })));
78
+ };
79
+ const isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
80
+ const decodeDocument = MediaProtocol.identity({ id: ID, name: NAME, provider: "openai" }).decodeJson(Response);
81
+ export const protocol = DecisionRoute.protocol({
82
+ id: ID,
83
+ name: NAME,
84
+ body: { from: fromRequest },
85
+ response: {
86
+ decode: Effect.fn("OpenAIDecisions.decodeResponse")(function* (response, context) {
87
+ const output = yield* decodeDocument(response);
88
+ const answers = output.value.answers.map((answer) => answer.type === "refusal"
89
+ ? {
90
+ name: answer.name,
91
+ type: "error",
92
+ reason: "refusal",
93
+ ...(answer.message === undefined ? {} : { message: answer.message }),
94
+ }
95
+ : answer);
96
+ const byName = new Map(answers.map((answer) => [answer.name, answer]));
97
+ const ordered = context.request.questions.flatMap((question) => byName.get(question.name) ?? []);
98
+ const usage = OpenResponses.mapUsage(output.value.usage, context.providerMetadataKey);
99
+ return new DecisionResponse({
100
+ answers: ordered.length === answers.length ? ordered : answers,
101
+ usage: usage === undefined
102
+ ? undefined
103
+ : new DecisionUsage({
104
+ inputTokens: usage.inputTokens,
105
+ outputTokens: usage.outputTokens,
106
+ totalTokens: usage.totalTokens,
107
+ cost: usage.cost,
108
+ }),
109
+ providerMetadata: {
110
+ [context.providerMetadataKey]: {
111
+ model: output.value.model,
112
+ ...(output.value.usage === undefined ? {} : { usage: output.value.usage }),
113
+ },
114
+ },
115
+ });
116
+ }),
117
+ },
118
+ });
119
+ export const route = DecisionRoute.make({
120
+ id: ID,
121
+ provider: "openai",
122
+ protocol,
123
+ endpoint: Endpoint.path(PATH, { baseURL: DEFAULT_BASE_URL }),
124
+ });
125
+ export const OpenAIDecisions = {
126
+ protocol,
127
+ route,
128
+ };
@@ -1,13 +1,19 @@
1
+ import { Schema } from "effect";
1
2
  import { ImageModel, type ImageRequestFor } from "../image.js";
2
3
  import { Media } from "../media.js";
3
4
  import { MediaProtocol } from "../route/media-protocol.js";
4
5
  import { MediaRoute } from "../route/media.js";
5
- import { type MediaUsage, type OpenString } from "../schema/index.js";
6
+ import { type OpenString } from "../schema/index.js";
6
7
  export declare const DEFAULT_BASE_URL = "https://api.openai.com/v1";
7
8
  export declare const PATH = "/images/generations";
8
9
  export declare const EDIT_PATH = "/images/edits";
9
- /** Provider-native options. Common fields (`n`, `size`, `format`, `images`, `mask`) live on the request. */
10
+ /** Provider-native options. Common fields (`n`, `aspect`, `resolution`, `images`) live on the request. */
10
11
  export type OpenAIImageOptions = {
12
+ /** Exact `WIDTHxHEIGHT` or `auto`; it replaces `aspect` and `resolution`, so setting both fails. */
13
+ readonly size?: OpenString<"auto" | "1024x1024" | "1536x1024" | "1024x1536">;
14
+ readonly outputFormat?: OpenString<"png" | "jpeg" | "webp">;
15
+ /** Inpainting mask for the first input image. */
16
+ readonly mask?: Media.Asset;
11
17
  readonly quality?: OpenString<"auto" | "low" | "medium" | "high" | "standard" | "hd">;
12
18
  readonly background?: OpenString<"auto" | "opaque" | "transparent">;
13
19
  readonly moderation?: OpenString<"auto" | "low">;
@@ -16,10 +22,16 @@ export type OpenAIImageOptions = {
16
22
  readonly partialImages?: number;
17
23
  } & Record<string, unknown>;
18
24
  export type Request = ImageRequestFor<OpenAIImageOptions>;
19
- /** `generate` reads the whole JSON response as one frame, with the requested format for responses that omit it. */
25
+ declare const Usage: Schema.Struct<{
26
+ readonly input_tokens: Schema.optional<Schema.Number>;
27
+ readonly output_tokens: Schema.optional<Schema.Number>;
28
+ readonly total_tokens: Schema.optional<Schema.Number>;
29
+ readonly input_tokens_details: Schema.optional<Schema.$Record<Schema.String, Schema.Unknown>>;
30
+ readonly output_tokens_details: Schema.optional<Schema.$Record<Schema.String, Schema.Unknown>>;
31
+ }>;
32
+ /** `generate` reads the whole JSON response as one frame. */
20
33
  type Frame = string | {
21
34
  readonly document: string;
22
- readonly requested: string | undefined;
23
35
  };
24
36
  interface State {
25
37
  readonly completed: number;
@@ -27,7 +39,7 @@ interface State {
27
39
  readonly size?: string;
28
40
  readonly quality?: string;
29
41
  readonly background?: string;
30
- readonly usage?: MediaUsage;
42
+ readonly usage?: Schema.Schema.Type<typeof Usage>;
31
43
  }
32
44
  export declare const protocol: MediaProtocol.Streamed<Request, {
33
45
  readonly type: "image-partial";
@@ -68,22 +80,16 @@ export declare const protocol: MediaProtocol.Streamed<Request, {
68
80
  readonly [x: string]: unknown;
69
81
  } | undefined;
70
82
  } | undefined;
71
- readonly notices?: readonly {
72
- readonly type: "filtered" | "moderated" | "other";
73
- readonly message: string;
74
- readonly providerMetadata?: {
75
- readonly [x: string]: {
76
- readonly [x: string]: unknown;
77
- };
78
- } | undefined;
79
- }[] | undefined;
80
83
  readonly providerMetadata?: {
81
84
  readonly [x: string]: {
82
85
  readonly [x: string]: unknown;
83
86
  };
84
87
  } | undefined;
85
88
  }, Frame, State>;
86
- export declare const model: (input: MediaRoute.ModelInput) => ImageModel<OpenAIImageOptions>;
89
+ /** `provider` attributes models, refs, and metadata to an OpenAI-compatible host; it defaults to `openai`. */
90
+ export declare const model: (input: MediaRoute.ModelInput & {
91
+ readonly provider?: string;
92
+ }) => ImageModel<OpenAIImageOptions>;
87
93
  export declare const OpenAIImages: {
88
94
  readonly protocol: MediaProtocol.Streamed<Request, {
89
95
  readonly type: "image-partial";
@@ -124,15 +130,6 @@ export declare const OpenAIImages: {
124
130
  readonly [x: string]: unknown;
125
131
  } | undefined;
126
132
  } | undefined;
127
- readonly notices?: readonly {
128
- readonly type: "filtered" | "moderated" | "other";
129
- readonly message: string;
130
- readonly providerMetadata?: {
131
- readonly [x: string]: {
132
- readonly [x: string]: unknown;
133
- };
134
- } | undefined;
135
- }[] | undefined;
136
133
  readonly providerMetadata?: {
137
134
  readonly [x: string]: {
138
135
  readonly [x: string]: unknown;