@radheim/ai 0.0.0-stage → 2.4.3

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 (560) hide show
  1. package/README.md +1890 -3
  2. package/dist/ai.d.ts +36 -0
  3. package/dist/ai.js +46 -0
  4. package/dist/cache-policy.d.ts +6 -0
  5. package/dist/cache-policy.js +205 -0
  6. package/dist/decision-client.d.ts +10 -0
  7. package/dist/decision-client.js +42 -0
  8. package/dist/decision.d.ts +378 -0
  9. package/dist/decision.js +171 -0
  10. package/dist/effect.d.ts +59 -0
  11. package/dist/effect.js +33 -0
  12. package/dist/effort-updates.d.ts +12 -0
  13. package/dist/effort-updates.js +23 -0
  14. package/dist/embedding-client.d.ts +10 -0
  15. package/dist/embedding-client.js +17 -0
  16. package/dist/embedding.d.ts +113 -0
  17. package/dist/embedding.js +76 -0
  18. package/dist/experimental/cloudflare-run-extraction.d.ts +17 -0
  19. package/dist/experimental/cloudflare-run-extraction.js +85 -0
  20. package/dist/experimental/extraction.d.ts +153 -0
  21. package/dist/experimental/extraction.js +103 -0
  22. package/dist/experimental/realtime.d.ts +80 -0
  23. package/dist/experimental/realtime.js +96 -0
  24. package/dist/experimental.d.ts +10 -0
  25. package/dist/experimental.js +6 -0
  26. package/dist/fetch-middleware.d.ts +7 -0
  27. package/dist/fetch-middleware.js +41 -0
  28. package/dist/generation.d.ts +77 -0
  29. package/dist/generation.js +109 -0
  30. package/dist/image-client.d.ts +14 -0
  31. package/dist/image-client.js +39 -0
  32. package/dist/image.d.ts +323 -0
  33. package/dist/image.js +103 -0
  34. package/dist/index.d.ts +300 -0
  35. package/dist/index.js +179 -0
  36. package/dist/llm.d.ts +109 -0
  37. package/dist/llm.js +177 -0
  38. package/dist/loop.d.ts +38 -0
  39. package/dist/loop.js +139 -0
  40. package/dist/media-client.d.ts +30 -0
  41. package/dist/media-client.js +44 -0
  42. package/dist/media-model.d.ts +49 -0
  43. package/dist/media-model.js +55 -0
  44. package/dist/media.d.ts +237 -0
  45. package/dist/media.js +285 -0
  46. package/dist/promise-media.d.ts +20 -0
  47. package/dist/promise-media.js +16 -0
  48. package/dist/protocols/alibaba-chat.d.ts +315 -0
  49. package/dist/protocols/alibaba-chat.js +78 -0
  50. package/dist/protocols/alibaba-images.d.ts +22 -0
  51. package/dist/protocols/alibaba-images.js +77 -0
  52. package/dist/protocols/alibaba-messages.d.ts +413 -0
  53. package/dist/protocols/alibaba-messages.js +44 -0
  54. package/dist/protocols/alibaba-responses.d.ts +298 -0
  55. package/dist/protocols/alibaba-responses.js +67 -0
  56. package/dist/protocols/anthropic-messages.d.ts +1213 -0
  57. package/dist/protocols/anthropic-messages.js +1412 -0
  58. package/dist/protocols/assemblyai-transcription.d.ts +41 -0
  59. package/dist/protocols/assemblyai-transcription.js +137 -0
  60. package/dist/protocols/bedrock-converse.d.ts +548 -0
  61. package/dist/protocols/bedrock-converse.js +801 -0
  62. package/dist/protocols/bedrock-event-stream.d.ts +14 -0
  63. package/dist/protocols/bedrock-event-stream.js +100 -0
  64. package/dist/protocols/cartesia-speech.d.ts +117 -0
  65. package/dist/protocols/cartesia-speech.js +120 -0
  66. package/dist/protocols/cloudflare-run-decisions.d.ts +9 -0
  67. package/dist/protocols/cloudflare-run-decisions.js +36 -0
  68. package/dist/protocols/cloudflare-workers-ai-images.d.ts +23 -0
  69. package/dist/protocols/cloudflare-workers-ai-images.js +80 -0
  70. package/dist/protocols/cohere-chat.d.ts +184 -0
  71. package/dist/protocols/cohere-chat.js +317 -0
  72. package/dist/protocols/deepgram-speech.d.ts +109 -0
  73. package/dist/protocols/deepgram-speech.js +93 -0
  74. package/dist/protocols/deepgram-transcription.d.ts +26 -0
  75. package/dist/protocols/deepgram-transcription.js +119 -0
  76. package/dist/protocols/elevenlabs-speech.d.ts +112 -0
  77. package/dist/protocols/elevenlabs-speech.js +111 -0
  78. package/dist/protocols/elevenlabs-transcription.d.ts +28 -0
  79. package/dist/protocols/elevenlabs-transcription.js +137 -0
  80. package/dist/protocols/gemini.d.ts +388 -0
  81. package/dist/protocols/gemini.js +741 -0
  82. package/dist/protocols/google-decisions.d.ts +13 -0
  83. package/dist/protocols/google-decisions.js +133 -0
  84. package/dist/protocols/google-embed-content.d.ts +34 -0
  85. package/dist/protocols/google-embed-content.js +110 -0
  86. package/dist/protocols/google-images.d.ts +18 -0
  87. package/dist/protocols/google-images.js +169 -0
  88. package/dist/protocols/google-interactions-video.d.ts +38 -0
  89. package/dist/protocols/google-interactions-video.js +185 -0
  90. package/dist/protocols/google-interactions.d.ts +554 -0
  91. package/dist/protocols/google-interactions.js +756 -0
  92. package/dist/protocols/google-speech.d.ts +120 -0
  93. package/dist/protocols/google-speech.js +94 -0
  94. package/dist/protocols/google-transcription.d.ts +148 -0
  95. package/dist/protocols/google-transcription.js +129 -0
  96. package/dist/protocols/google-vertex-predict-embeddings.d.ts +18 -0
  97. package/dist/protocols/google-vertex-predict-embeddings.js +74 -0
  98. package/dist/protocols/index.d.ts +21 -0
  99. package/dist/protocols/index.js +21 -0
  100. package/dist/protocols/meta-images.d.ts +26 -0
  101. package/dist/protocols/meta-images.js +101 -0
  102. package/dist/protocols/meta-messages.d.ts +423 -0
  103. package/dist/protocols/meta-messages.js +63 -0
  104. package/dist/protocols/meta-responses.d.ts +533 -0
  105. package/dist/protocols/meta-responses.js +189 -0
  106. package/dist/protocols/minimax-images.d.ts +25 -0
  107. package/dist/protocols/minimax-images.js +150 -0
  108. package/dist/protocols/minimax-video.d.ts +45 -0
  109. package/dist/protocols/minimax-video.js +296 -0
  110. package/dist/protocols/mistral-chat.d.ts +565 -0
  111. package/dist/protocols/mistral-chat.js +606 -0
  112. package/dist/protocols/open-responses-channel.d.ts +25 -0
  113. package/dist/protocols/open-responses-channel.js +137 -0
  114. package/dist/protocols/open-responses-continuation.d.ts +18 -0
  115. package/dist/protocols/open-responses-continuation.js +172 -0
  116. package/dist/protocols/open-responses.d.ts +2509 -0
  117. package/dist/protocols/open-responses.js +1335 -0
  118. package/dist/protocols/openai-chat.d.ts +2571 -0
  119. package/dist/protocols/openai-chat.js +1140 -0
  120. package/dist/protocols/openai-compatible-chat.d.ts +184 -0
  121. package/dist/protocols/openai-compatible-chat.js +19 -0
  122. package/dist/protocols/openai-compatible-responses.d.ts +231 -0
  123. package/dist/protocols/openai-compatible-responses.js +18 -0
  124. package/dist/protocols/openai-decisions.d.ts +22 -0
  125. package/dist/protocols/openai-decisions.js +141 -0
  126. package/dist/protocols/openai-embeddings.d.ts +16 -0
  127. package/dist/protocols/openai-embeddings.js +76 -0
  128. package/dist/protocols/openai-freeform.d.ts +78 -0
  129. package/dist/protocols/openai-freeform.js +222 -0
  130. package/dist/protocols/openai-images.d.ts +148 -0
  131. package/dist/protocols/openai-images.js +303 -0
  132. package/dist/protocols/openai-responses.d.ts +1600 -0
  133. package/dist/protocols/openai-responses.js +360 -0
  134. package/dist/protocols/openai-speech.d.ts +110 -0
  135. package/dist/protocols/openai-speech.js +112 -0
  136. package/dist/protocols/openai-transcription.d.ts +185 -0
  137. package/dist/protocols/openai-transcription.js +206 -0
  138. package/dist/protocols/openrouter-video.d.ts +36 -0
  139. package/dist/protocols/openrouter-video.js +139 -0
  140. package/dist/protocols/seedance-video.d.ts +45 -0
  141. package/dist/protocols/seedance-video.js +217 -0
  142. package/dist/protocols/shared.d.ts +249 -0
  143. package/dist/protocols/shared.js +446 -0
  144. package/dist/protocols/system-one.d.ts +145 -0
  145. package/dist/protocols/system-one.js +272 -0
  146. package/dist/protocols/utils/bedrock-auth.d.ts +60 -0
  147. package/dist/protocols/utils/bedrock-auth.js +86 -0
  148. package/dist/protocols/utils/bedrock-cache.d.ts +19 -0
  149. package/dist/protocols/utils/bedrock-cache.js +49 -0
  150. package/dist/protocols/utils/bedrock-media.d.ts +53 -0
  151. package/dist/protocols/utils/bedrock-media.js +98 -0
  152. package/dist/protocols/utils/cache.d.ts +25 -0
  153. package/dist/protocols/utils/cache.js +33 -0
  154. package/dist/protocols/utils/claude-model.d.ts +8 -0
  155. package/dist/protocols/utils/claude-model.js +15 -0
  156. package/dist/protocols/utils/gateway-protocol.d.ts +17 -0
  157. package/dist/protocols/utils/gateway-protocol.js +59 -0
  158. package/dist/protocols/utils/gemini-generate-content.d.ts +75 -0
  159. package/dist/protocols/utils/gemini-generate-content.js +96 -0
  160. package/dist/protocols/utils/gemini-json-schema.d.ts +8 -0
  161. package/dist/protocols/utils/gemini-json-schema.js +76 -0
  162. package/dist/protocols/utils/image-sizing.d.ts +31 -0
  163. package/dist/protocols/utils/image-sizing.js +73 -0
  164. package/dist/protocols/utils/lifecycle.d.ts +24 -0
  165. package/dist/protocols/utils/lifecycle.js +62 -0
  166. package/dist/protocols/utils/media-input.d.ts +32 -0
  167. package/dist/protocols/utils/media-input.js +62 -0
  168. package/dist/protocols/utils/mistral-tool-id.d.ts +5 -0
  169. package/dist/protocols/utils/mistral-tool-id.js +31 -0
  170. package/dist/protocols/utils/open-responses-options.d.ts +54 -0
  171. package/dist/protocols/utils/open-responses-options.js +59 -0
  172. package/dist/protocols/utils/partial-json-options.d.ts +61 -0
  173. package/dist/protocols/utils/partial-json-options.js +51 -0
  174. package/dist/protocols/utils/partial-json.d.ts +8 -0
  175. package/dist/protocols/utils/partial-json.js +280 -0
  176. package/dist/protocols/utils/responses-checkpoint.d.ts +4 -0
  177. package/dist/protocols/utils/responses-checkpoint.js +103 -0
  178. package/dist/protocols/utils/responses-compaction.d.ts +6 -0
  179. package/dist/protocols/utils/responses-compaction.js +162 -0
  180. package/dist/protocols/utils/responses-hosted-tools.d.ts +122 -0
  181. package/dist/protocols/utils/responses-hosted-tools.js +63 -0
  182. package/dist/protocols/utils/speaker-turns.d.ts +3 -0
  183. package/dist/protocols/utils/speaker-turns.js +9 -0
  184. package/dist/protocols/utils/speech-stream.d.ts +49 -0
  185. package/dist/protocols/utils/speech-stream.js +64 -0
  186. package/dist/protocols/utils/tool-schema.d.ts +10 -0
  187. package/dist/protocols/utils/tool-schema.js +109 -0
  188. package/dist/protocols/utils/tool-stream.d.ts +478 -0
  189. package/dist/protocols/utils/tool-stream.js +153 -0
  190. package/dist/protocols/venice-chat.d.ts +340 -0
  191. package/dist/protocols/venice-chat.js +283 -0
  192. package/dist/protocols/xai-images.d.ts +25 -0
  193. package/dist/protocols/xai-images.js +87 -0
  194. package/dist/protocols/xai-responses.d.ts +294 -0
  195. package/dist/protocols/xai-responses.js +86 -0
  196. package/dist/protocols/xai-video.d.ts +35 -0
  197. package/dist/protocols/xai-video.js +159 -0
  198. package/dist/protocols/zai-chat.d.ts +296 -0
  199. package/dist/protocols/zai-chat.js +56 -0
  200. package/dist/protocols/zai-images.d.ts +20 -0
  201. package/dist/protocols/zai-images.js +79 -0
  202. package/dist/protocols/zai-messages.d.ts +409 -0
  203. package/dist/protocols/zai-messages.js +30 -0
  204. package/dist/protocols.d.ts +1 -0
  205. package/dist/protocols.js +1 -0
  206. package/dist/provider-error.d.ts +22 -0
  207. package/dist/provider-error.js +228 -0
  208. package/dist/provider-package.d.ts +17 -0
  209. package/dist/provider-package.js +1 -0
  210. package/dist/providers/alibaba/chat.d.ts +1 -0
  211. package/dist/providers/alibaba/chat.js +1 -0
  212. package/dist/providers/alibaba/embeddings.d.ts +2 -0
  213. package/dist/providers/alibaba/embeddings.js +1 -0
  214. package/dist/providers/alibaba/images.d.ts +2 -0
  215. package/dist/providers/alibaba/images.js +1 -0
  216. package/dist/providers/alibaba/messages.d.ts +3 -0
  217. package/dist/providers/alibaba/messages.js +1 -0
  218. package/dist/providers/alibaba/responses.d.ts +3 -0
  219. package/dist/providers/alibaba/responses.js +1 -0
  220. package/dist/providers/alibaba.d.ts +905 -0
  221. package/dist/providers/alibaba.js +119 -0
  222. package/dist/providers/amazon-bedrock/mantle/chat.d.ts +2 -0
  223. package/dist/providers/amazon-bedrock/mantle/chat.js +1 -0
  224. package/dist/providers/amazon-bedrock/mantle/messages.d.ts +2 -0
  225. package/dist/providers/amazon-bedrock/mantle/messages.js +1 -0
  226. package/dist/providers/amazon-bedrock/mantle/responses.d.ts +2 -0
  227. package/dist/providers/amazon-bedrock/mantle/responses.js +1 -0
  228. package/dist/providers/amazon-bedrock/mantle.d.ts +2 -0
  229. package/dist/providers/amazon-bedrock/mantle.js +1 -0
  230. package/dist/providers/amazon-bedrock-mantle.d.ts +789 -0
  231. package/dist/providers/amazon-bedrock-mantle.js +107 -0
  232. package/dist/providers/amazon-bedrock.d.ts +190 -0
  233. package/dist/providers/amazon-bedrock.js +44 -0
  234. package/dist/providers/anthropic-compatible.d.ts +364 -0
  235. package/dist/providers/anthropic-compatible.js +47 -0
  236. package/dist/providers/anthropic.d.ts +362 -0
  237. package/dist/providers/anthropic.js +36 -0
  238. package/dist/providers/assemblyai.d.ts +19 -0
  239. package/dist/providers/assemblyai.js +19 -0
  240. package/dist/providers/azure/chat.d.ts +2 -0
  241. package/dist/providers/azure/chat.js +1 -0
  242. package/dist/providers/azure/embeddings.d.ts +2 -0
  243. package/dist/providers/azure/embeddings.js +1 -0
  244. package/dist/providers/azure/images.d.ts +2 -0
  245. package/dist/providers/azure/images.js +1 -0
  246. package/dist/providers/azure/messages.d.ts +2 -0
  247. package/dist/providers/azure/messages.js +1 -0
  248. package/dist/providers/azure/responses.d.ts +2 -0
  249. package/dist/providers/azure/responses.js +1 -0
  250. package/dist/providers/azure.d.ts +1074 -0
  251. package/dist/providers/azure.js +150 -0
  252. package/dist/providers/baseten/decisions.d.ts +2 -0
  253. package/dist/providers/baseten/decisions.js +1 -0
  254. package/dist/providers/baseten.d.ts +384 -0
  255. package/dist/providers/baseten.js +56 -0
  256. package/dist/providers/byteplus/videos.d.ts +2 -0
  257. package/dist/providers/byteplus/videos.js +1 -0
  258. package/dist/providers/byteplus.d.ts +24 -0
  259. package/dist/providers/byteplus.js +21 -0
  260. package/dist/providers/cartesia.d.ts +18 -0
  261. package/dist/providers/cartesia.js +17 -0
  262. package/dist/providers/cerebras.d.ts +375 -0
  263. package/dist/providers/cerebras.js +33 -0
  264. package/dist/providers/cloudflare-ai-gateway/decisions.d.ts +2 -0
  265. package/dist/providers/cloudflare-ai-gateway/decisions.js +1 -0
  266. package/dist/providers/cloudflare-ai-gateway.d.ts +1581 -0
  267. package/dist/providers/cloudflare-ai-gateway.js +120 -0
  268. package/dist/providers/cloudflare-shared.d.ts +9 -0
  269. package/dist/providers/cloudflare-shared.js +25 -0
  270. package/dist/providers/cloudflare-workers-ai/decisions.d.ts +2 -0
  271. package/dist/providers/cloudflare-workers-ai/decisions.js +1 -0
  272. package/dist/providers/cloudflare-workers-ai/images.d.ts +2 -0
  273. package/dist/providers/cloudflare-workers-ai/images.js +1 -0
  274. package/dist/providers/cloudflare-workers-ai.d.ts +391 -0
  275. package/dist/providers/cloudflare-workers-ai.js +69 -0
  276. package/dist/providers/cohere/chat.d.ts +4 -0
  277. package/dist/providers/cohere/chat.js +3 -0
  278. package/dist/providers/cohere/embeddings.d.ts +2 -0
  279. package/dist/providers/cohere/embeddings.js +1 -0
  280. package/dist/providers/cohere.d.ts +502 -0
  281. package/dist/providers/cohere.js +55 -0
  282. package/dist/providers/deepgram.d.ts +22 -0
  283. package/dist/providers/deepgram.js +20 -0
  284. package/dist/providers/deepinfra/embeddings.d.ts +2 -0
  285. package/dist/providers/deepinfra/embeddings.js +1 -0
  286. package/dist/providers/deepinfra.d.ts +378 -0
  287. package/dist/providers/deepinfra.js +52 -0
  288. package/dist/providers/deepseek.d.ts +376 -0
  289. package/dist/providers/deepseek.js +30 -0
  290. package/dist/providers/digitalocean/decisions.d.ts +2 -0
  291. package/dist/providers/digitalocean/decisions.js +1 -0
  292. package/dist/providers/digitalocean/embeddings.d.ts +2 -0
  293. package/dist/providers/digitalocean/embeddings.js +1 -0
  294. package/dist/providers/digitalocean/images.d.ts +2 -0
  295. package/dist/providers/digitalocean/images.js +1 -0
  296. package/dist/providers/digitalocean/responses.d.ts +4 -0
  297. package/dist/providers/digitalocean/responses.js +1 -0
  298. package/dist/providers/digitalocean.d.ts +874 -0
  299. package/dist/providers/digitalocean.js +106 -0
  300. package/dist/providers/elevenlabs.d.ts +22 -0
  301. package/dist/providers/elevenlabs.js +22 -0
  302. package/dist/providers/fireworks/embeddings.d.ts +2 -0
  303. package/dist/providers/fireworks/embeddings.js +1 -0
  304. package/dist/providers/fireworks.d.ts +379 -0
  305. package/dist/providers/fireworks.js +40 -0
  306. package/dist/providers/google/decisions.d.ts +2 -0
  307. package/dist/providers/google/decisions.js +1 -0
  308. package/dist/providers/google/embeddings.d.ts +2 -0
  309. package/dist/providers/google/embeddings.js +1 -0
  310. package/dist/providers/google/images.d.ts +2 -0
  311. package/dist/providers/google/images.js +1 -0
  312. package/dist/providers/google/interactions.d.ts +7 -0
  313. package/dist/providers/google/interactions.js +3 -0
  314. package/dist/providers/google/videos.d.ts +2 -0
  315. package/dist/providers/google/videos.js +1 -0
  316. package/dist/providers/google-vertex/chat.d.ts +2 -0
  317. package/dist/providers/google-vertex/chat.js +1 -0
  318. package/dist/providers/google-vertex/embeddings.d.ts +2 -0
  319. package/dist/providers/google-vertex/embeddings.js +1 -0
  320. package/dist/providers/google-vertex/gemini.d.ts +2 -0
  321. package/dist/providers/google-vertex/gemini.js +1 -0
  322. package/dist/providers/google-vertex/images.d.ts +2 -0
  323. package/dist/providers/google-vertex/images.js +1 -0
  324. package/dist/providers/google-vertex/interactions.d.ts +2 -0
  325. package/dist/providers/google-vertex/interactions.js +1 -0
  326. package/dist/providers/google-vertex/messages.d.ts +2 -0
  327. package/dist/providers/google-vertex/messages.js +1 -0
  328. package/dist/providers/google-vertex/mistral.d.ts +2 -0
  329. package/dist/providers/google-vertex/mistral.js +1 -0
  330. package/dist/providers/google-vertex/responses.d.ts +2 -0
  331. package/dist/providers/google-vertex/responses.js +1 -0
  332. package/dist/providers/google-vertex-chat.d.ts +204 -0
  333. package/dist/providers/google-vertex-chat.js +29 -0
  334. package/dist/providers/google-vertex-interactions.d.ts +111 -0
  335. package/dist/providers/google-vertex-interactions.js +62 -0
  336. package/dist/providers/google-vertex-messages.d.ts +362 -0
  337. package/dist/providers/google-vertex-messages.js +51 -0
  338. package/dist/providers/google-vertex-mistral.d.ts +120 -0
  339. package/dist/providers/google-vertex-mistral.js +43 -0
  340. package/dist/providers/google-vertex-responses.d.ts +253 -0
  341. package/dist/providers/google-vertex-responses.js +30 -0
  342. package/dist/providers/google-vertex-shared.d.ts +58 -0
  343. package/dist/providers/google-vertex-shared.js +128 -0
  344. package/dist/providers/google-vertex.d.ts +132 -0
  345. package/dist/providers/google-vertex.js +161 -0
  346. package/dist/providers/google.d.ts +246 -0
  347. package/dist/providers/google.js +80 -0
  348. package/dist/providers/groq.d.ts +470 -0
  349. package/dist/providers/groq.js +63 -0
  350. package/dist/providers/index.d.ts +45 -0
  351. package/dist/providers/index.js +45 -0
  352. package/dist/providers/meta/chat.d.ts +2 -0
  353. package/dist/providers/meta/chat.js +1 -0
  354. package/dist/providers/meta/images.d.ts +2 -0
  355. package/dist/providers/meta/images.js +1 -0
  356. package/dist/providers/meta/messages.d.ts +2 -0
  357. package/dist/providers/meta/messages.js +1 -0
  358. package/dist/providers/meta/responses.d.ts +2 -0
  359. package/dist/providers/meta/responses.js +1 -0
  360. package/dist/providers/meta.d.ts +804 -0
  361. package/dist/providers/meta.js +96 -0
  362. package/dist/providers/minimax/chat.d.ts +4 -0
  363. package/dist/providers/minimax/chat.js +3 -0
  364. package/dist/providers/minimax/images.d.ts +3 -0
  365. package/dist/providers/minimax/images.js +1 -0
  366. package/dist/providers/minimax/messages.d.ts +1 -0
  367. package/dist/providers/minimax/messages.js +1 -0
  368. package/dist/providers/minimax/responses.d.ts +4 -0
  369. package/dist/providers/minimax/responses.js +3 -0
  370. package/dist/providers/minimax/videos.d.ts +3 -0
  371. package/dist/providers/minimax/videos.js +1 -0
  372. package/dist/providers/minimax.d.ts +798 -0
  373. package/dist/providers/minimax.js +99 -0
  374. package/dist/providers/mistral.d.ts +208 -0
  375. package/dist/providers/mistral.js +17 -0
  376. package/dist/providers/moonshot/chat.d.ts +1 -0
  377. package/dist/providers/moonshot/chat.js +1 -0
  378. package/dist/providers/moonshot/messages.d.ts +4 -0
  379. package/dist/providers/moonshot/messages.js +3 -0
  380. package/dist/providers/moonshot/responses.d.ts +4 -0
  381. package/dist/providers/moonshot/responses.js +3 -0
  382. package/dist/providers/moonshot.d.ts +785 -0
  383. package/dist/providers/moonshot.js +78 -0
  384. package/dist/providers/open-responses-options.d.ts +5 -0
  385. package/dist/providers/open-responses-options.js +1 -0
  386. package/dist/providers/openai/chat.d.ts +2 -0
  387. package/dist/providers/openai/chat.js +1 -0
  388. package/dist/providers/openai/embeddings.d.ts +2 -0
  389. package/dist/providers/openai/embeddings.js +1 -0
  390. package/dist/providers/openai/images.d.ts +2 -0
  391. package/dist/providers/openai/images.js +1 -0
  392. package/dist/providers/openai/responses.d.ts +2 -0
  393. package/dist/providers/openai/responses.js +1 -0
  394. package/dist/providers/openai-compatible/decisions.d.ts +2 -0
  395. package/dist/providers/openai-compatible/decisions.js +1 -0
  396. package/dist/providers/openai-compatible/embeddings.d.ts +2 -0
  397. package/dist/providers/openai-compatible/embeddings.js +1 -0
  398. package/dist/providers/openai-compatible/images.d.ts +2 -0
  399. package/dist/providers/openai-compatible/images.js +1 -0
  400. package/dist/providers/openai-compatible/responses.d.ts +1 -0
  401. package/dist/providers/openai-compatible/responses.js +1 -0
  402. package/dist/providers/openai-compatible-responses.d.ts +250 -0
  403. package/dist/providers/openai-compatible-responses.js +26 -0
  404. package/dist/providers/openai-compatible.d.ts +208 -0
  405. package/dist/providers/openai-compatible.js +71 -0
  406. package/dist/providers/openai-options.d.ts +21 -0
  407. package/dist/providers/openai-options.js +37 -0
  408. package/dist/providers/openai.d.ts +522 -0
  409. package/dist/providers/openai.js +118 -0
  410. package/dist/providers/opencode/decisions.d.ts +2 -0
  411. package/dist/providers/opencode/decisions.js +1 -0
  412. package/dist/providers/opencode.d.ts +23 -0
  413. package/dist/providers/opencode.js +25 -0
  414. package/dist/providers/openrouter/decisions.d.ts +2 -0
  415. package/dist/providers/openrouter/decisions.js +1 -0
  416. package/dist/providers/openrouter/embeddings.d.ts +2 -0
  417. package/dist/providers/openrouter/embeddings.js +1 -0
  418. package/dist/providers/openrouter/images.d.ts +2 -0
  419. package/dist/providers/openrouter/images.js +1 -0
  420. package/dist/providers/openrouter/responses.d.ts +3 -0
  421. package/dist/providers/openrouter/responses.js +1 -0
  422. package/dist/providers/openrouter/videos.d.ts +2 -0
  423. package/dist/providers/openrouter/videos.js +1 -0
  424. package/dist/providers/openrouter.d.ts +1204 -0
  425. package/dist/providers/openrouter.js +210 -0
  426. package/dist/providers/settings.d.ts +40 -0
  427. package/dist/providers/settings.js +18 -0
  428. package/dist/providers/togetherai.d.ts +375 -0
  429. package/dist/providers/togetherai.js +30 -0
  430. package/dist/providers/typesafe-ai/decisions.d.ts +2 -0
  431. package/dist/providers/typesafe-ai/decisions.js +1 -0
  432. package/dist/providers/typesafe-ai.d.ts +23 -0
  433. package/dist/providers/typesafe-ai.js +25 -0
  434. package/dist/providers/venice/responses.d.ts +4 -0
  435. package/dist/providers/venice/responses.js +1 -0
  436. package/dist/providers/venice.d.ts +632 -0
  437. package/dist/providers/venice.js +57 -0
  438. package/dist/providers/vercel-ai-gateway/chat.d.ts +2 -0
  439. package/dist/providers/vercel-ai-gateway/chat.js +1 -0
  440. package/dist/providers/vercel-ai-gateway/decisions.d.ts +2 -0
  441. package/dist/providers/vercel-ai-gateway/decisions.js +1 -0
  442. package/dist/providers/vercel-ai-gateway/embeddings.d.ts +2 -0
  443. package/dist/providers/vercel-ai-gateway/embeddings.js +1 -0
  444. package/dist/providers/vercel-ai-gateway/images.d.ts +2 -0
  445. package/dist/providers/vercel-ai-gateway/images.js +1 -0
  446. package/dist/providers/vercel-ai-gateway/messages.d.ts +2 -0
  447. package/dist/providers/vercel-ai-gateway/messages.js +1 -0
  448. package/dist/providers/vercel-ai-gateway/responses.d.ts +2 -0
  449. package/dist/providers/vercel-ai-gateway/responses.js +1 -0
  450. package/dist/providers/vercel-ai-gateway.d.ts +111 -0
  451. package/dist/providers/vercel-ai-gateway.js +181 -0
  452. package/dist/providers/volcengine/videos.d.ts +2 -0
  453. package/dist/providers/volcengine/videos.js +1 -0
  454. package/dist/providers/volcengine.d.ts +24 -0
  455. package/dist/providers/volcengine.js +21 -0
  456. package/dist/providers/xai/images.d.ts +2 -0
  457. package/dist/providers/xai/images.js +1 -0
  458. package/dist/providers/xai/videos.d.ts +2 -0
  459. package/dist/providers/xai/videos.js +1 -0
  460. package/dist/providers/xai.d.ts +235 -0
  461. package/dist/providers/xai.js +89 -0
  462. package/dist/providers/zai/chat.d.ts +1 -0
  463. package/dist/providers/zai/chat.js +1 -0
  464. package/dist/providers/zai/images.d.ts +2 -0
  465. package/dist/providers/zai/images.js +1 -0
  466. package/dist/providers/zai-coding-plan/chat.d.ts +1 -0
  467. package/dist/providers/zai-coding-plan/chat.js +1 -0
  468. package/dist/providers/zai-coding-plan/messages.d.ts +4 -0
  469. package/dist/providers/zai-coding-plan/messages.js +3 -0
  470. package/dist/providers/zai-coding-plan/responses.d.ts +4 -0
  471. package/dist/providers/zai-coding-plan/responses.js +3 -0
  472. package/dist/providers/zai-coding-plan.d.ts +846 -0
  473. package/dist/providers/zai-coding-plan.js +56 -0
  474. package/dist/providers/zai.d.ts +285 -0
  475. package/dist/providers/zai.js +44 -0
  476. package/dist/providers.d.ts +1 -0
  477. package/dist/providers.js +1 -0
  478. package/dist/public.d.ts +18 -0
  479. package/dist/public.js +7 -0
  480. package/dist/route/auth-options.d.ts +35 -0
  481. package/dist/route/auth-options.js +17 -0
  482. package/dist/route/auth.d.ts +52 -0
  483. package/dist/route/auth.js +92 -0
  484. package/dist/route/client.d.ts +229 -0
  485. package/dist/route/client.js +293 -0
  486. package/dist/route/decision.d.ts +55 -0
  487. package/dist/route/decision.js +73 -0
  488. package/dist/route/embedding.d.ts +58 -0
  489. package/dist/route/embedding.js +85 -0
  490. package/dist/route/endpoint.d.ts +29 -0
  491. package/dist/route/endpoint.js +21 -0
  492. package/dist/route/executor-service.d.ts +54 -0
  493. package/dist/route/executor-service.js +115 -0
  494. package/dist/route/executor.d.ts +37 -0
  495. package/dist/route/executor.js +225 -0
  496. package/dist/route/framing.d.ts +44 -0
  497. package/dist/route/framing.js +63 -0
  498. package/dist/route/index.d.ts +21 -0
  499. package/dist/route/index.js +18 -0
  500. package/dist/route/media-protocol.d.ts +187 -0
  501. package/dist/route/media-protocol.js +117 -0
  502. package/dist/route/media.d.ts +91 -0
  503. package/dist/route/media.js +251 -0
  504. package/dist/route/protocol.d.ts +85 -0
  505. package/dist/route/protocol.js +19 -0
  506. package/dist/route/transport/http.d.ts +34 -0
  507. package/dist/route/transport/http.js +67 -0
  508. package/dist/route/transport/index.d.ts +42 -0
  509. package/dist/route/transport/index.js +4 -0
  510. package/dist/route/transport/websocket-channel.d.ts +56 -0
  511. package/dist/route/transport/websocket-channel.js +1 -0
  512. package/dist/route/transport/websocket.d.ts +32 -0
  513. package/dist/route/transport/websocket.js +315 -0
  514. package/dist/route.d.ts +1 -0
  515. package/dist/route.js +1 -0
  516. package/dist/schema/errors.d.ts +194 -0
  517. package/dist/schema/errors.js +217 -0
  518. package/dist/schema/events.d.ts +11001 -0
  519. package/dist/schema/events.js +542 -0
  520. package/dist/schema/ids.d.ts +17 -0
  521. package/dist/schema/ids.js +10 -0
  522. package/dist/schema/index.d.ts +5 -0
  523. package/dist/schema/index.js +5 -0
  524. package/dist/schema/messages.d.ts +2706 -0
  525. package/dist/schema/messages.js +344 -0
  526. package/dist/schema/options.d.ts +270 -0
  527. package/dist/schema/options.js +198 -0
  528. package/dist/speech.d.ts +266 -0
  529. package/dist/speech.js +114 -0
  530. package/dist/test-ai.d.ts +15 -0
  531. package/dist/test-ai.js +49 -0
  532. package/dist/testing.d.ts +1585 -0
  533. package/dist/testing.js +108 -0
  534. package/dist/tool-history.d.ts +2 -0
  535. package/dist/tool-history.js +91 -0
  536. package/dist/tool-runtime.d.ts +22 -0
  537. package/dist/tool-runtime.js +55 -0
  538. package/dist/tool-updates.d.ts +34 -0
  539. package/dist/tool-updates.js +93 -0
  540. package/dist/tool.d.ts +150 -0
  541. package/dist/tool.js +159 -0
  542. package/dist/transcription.d.ts +254 -0
  543. package/dist/transcription.js +119 -0
  544. package/dist/utils/bytes.d.ts +1 -0
  545. package/dist/utils/bytes.js +10 -0
  546. package/dist/utils/json.d.ts +4 -0
  547. package/dist/utils/json.js +4 -0
  548. package/dist/utils/media-type.d.ts +8 -0
  549. package/dist/utils/media-type.js +93 -0
  550. package/dist/utils/provider-codes.d.ts +3 -0
  551. package/dist/utils/provider-codes.js +41 -0
  552. package/dist/utils/record.d.ts +2 -0
  553. package/dist/utils/record.js +2 -0
  554. package/dist/utils/sanitize.d.ts +5 -0
  555. package/dist/utils/sanitize.js +31 -0
  556. package/dist/video-client.d.ts +12 -0
  557. package/dist/video-client.js +24 -0
  558. package/dist/video.d.ts +178 -0
  559. package/dist/video.js +79 -0
  560. package/package.json +648 -4
@@ -0,0 +1,237 @@
1
+ export * as Media from "./media.js";
2
+ import { Effect, FileSystem, Schema, Stream } from "effect";
3
+ import { ProviderID } from "./schema/ids.js";
4
+ import { AIError } from "./schema/errors.js";
5
+ import { ProviderMetadata } from "./schema/options.js";
6
+ import { type HttpMiddleware, Service } from "./route/executor-service.js";
7
+ export { detectMediaType } from "./utils/media-type.js";
8
+ export declare const Source: Schema.Union<readonly [Schema.Struct<{
9
+ readonly type: Schema.Literal<"bytes">;
10
+ readonly data: Schema.Uint8Array;
11
+ readonly mediaType: Schema.String;
12
+ }>, Schema.Struct<{
13
+ readonly type: Schema.Literal<"base64">;
14
+ readonly data: Schema.String;
15
+ readonly mediaType: Schema.String;
16
+ }>, Schema.Struct<{
17
+ readonly type: Schema.Literal<"url">;
18
+ readonly url: Schema.String;
19
+ readonly mediaType: Schema.optional<Schema.String>;
20
+ /** Epoch milliseconds after which the provider no longer serves the URL. */
21
+ readonly expiresAt: Schema.optional<Schema.Number>;
22
+ }>, Schema.Struct<{
23
+ readonly type: Schema.Literal<"ref">;
24
+ readonly provider: Schema.brand<Schema.String, "AI.ProviderID">;
25
+ readonly id: Schema.String;
26
+ readonly mediaType: Schema.optional<Schema.String>;
27
+ }>]>;
28
+ export type Source = Schema.Schema.Type<typeof Source>;
29
+ export type AspectRatio = `${number}:${number}`;
30
+ export declare const AspectRatio: Schema.declare<`${number}:${number}`, `${number}:${number}`>;
31
+ export declare const Kind: Schema.Literals<readonly ["image", "video", "audio", "document", "other"]>;
32
+ export type Kind = Schema.Schema.Type<typeof Kind>;
33
+ export declare const kindOf: (mediaType: string) => Kind;
34
+ /** Container-independent facts about the payload; raw PCM audio relies on these because it has no header. */
35
+ export declare const Info: Schema.Struct<{
36
+ readonly width: Schema.optional<Schema.Number>;
37
+ readonly height: Schema.optional<Schema.Number>;
38
+ readonly durationSeconds: Schema.optional<Schema.Number>;
39
+ readonly sampleRate: Schema.optional<Schema.Number>;
40
+ readonly channels: Schema.optional<Schema.Number>;
41
+ readonly encoding: Schema.optional<Schema.String>;
42
+ readonly format: Schema.optional<Schema.String>;
43
+ }>;
44
+ export type Info = Schema.Schema.Type<typeof Info>;
45
+ /** Synchronous view of an inline payload; `undefined` for `url` and `ref` sources, which carry no local bytes. */
46
+ export interface Inline {
47
+ readonly mime: string;
48
+ readonly base64: string;
49
+ readonly dataUrl: string;
50
+ }
51
+ export declare class Asset {
52
+ #private;
53
+ readonly source: Source;
54
+ /** Derived from the source: declared type, sniffed magic bytes, then `application/octet-stream`. */
55
+ readonly mediaType: string;
56
+ readonly kind: Kind;
57
+ readonly info?: Info;
58
+ /** Epoch milliseconds after which a `url` source stops resolving. */
59
+ readonly expiresAt?: number;
60
+ readonly providerMetadata?: ProviderMetadata;
61
+ /** Transient download credentials for `url` sources; see `Asset.Input.headers`. */
62
+ readonly headers?: Record<string, string>;
63
+ /** Middleware for downloading a `url` source; see `Asset.Input.middleware`. */
64
+ readonly middleware?: HttpMiddleware;
65
+ constructor(input: Asset.Input);
66
+ /** Inline payload without effects, for protocols that embed base64 or data URLs directly. */
67
+ inline(): Inline | undefined;
68
+ /** Decoded payload; downloads `url` sources through the request executor and caches the result. */
69
+ bytes(): Effect.Effect<Uint8Array, AIError, Service>;
70
+ /** The payload as a byte stream; `url` sources stream from the network without being buffered or cached. */
71
+ stream(): Stream.Stream<Uint8Array, AIError, Service>;
72
+ base64(): Effect.Effect<string, AIError, Service>;
73
+ dataUrl(): Effect.Effect<string, AIError, Service>;
74
+ /**
75
+ * The `AssetEncoded` JSON form with `bytes` sources as base64, matching `Schema.toCodecJson(AssetSchema)`, so a
76
+ * plain `JSON.stringify` of messages or events stays lossless and decodes back through the JSON codec.
77
+ */
78
+ toJSON(): {
79
+ source: {
80
+ readonly type: "base64";
81
+ readonly data: string;
82
+ readonly mediaType: string;
83
+ } | {
84
+ readonly type: "url";
85
+ readonly url: string;
86
+ readonly mediaType?: string | undefined;
87
+ readonly expiresAt?: number | undefined;
88
+ } | {
89
+ readonly type: "ref";
90
+ readonly provider: string & import("effect/Brand").Brand<"AI.ProviderID">;
91
+ readonly id: string;
92
+ readonly mediaType?: string | undefined;
93
+ } | {
94
+ type: "bytes";
95
+ mediaType: string;
96
+ data: string;
97
+ };
98
+ info: {
99
+ readonly width?: number | undefined;
100
+ readonly height?: number | undefined;
101
+ readonly durationSeconds?: number | undefined;
102
+ readonly sampleRate?: number | undefined;
103
+ readonly channels?: number | undefined;
104
+ readonly encoding?: string | undefined;
105
+ readonly format?: string | undefined;
106
+ } | undefined;
107
+ providerMetadata: {
108
+ readonly [x: string]: {
109
+ readonly [x: string]: unknown;
110
+ };
111
+ } | undefined;
112
+ };
113
+ /** Pull `url` sources into owned bytes before the URL expires. Inline sources return themselves. */
114
+ materialize(): Effect.Effect<Asset, AIError, Service>;
115
+ }
116
+ export declare namespace Asset {
117
+ interface Input {
118
+ readonly source: Source;
119
+ readonly info?: Info;
120
+ readonly providerMetadata?: ProviderMetadata;
121
+ /**
122
+ * Headers required to download a `url` source, such as a provider API key.
123
+ * They are runtime-only: never part of `source`, `toJSON()`, or `AssetSchema`, so a persisted asset cannot leak
124
+ * credentials and cannot be downloaded again after a round-trip. Call `materialize()` before persisting.
125
+ */
126
+ readonly headers?: Record<string, string>;
127
+ /**
128
+ * Middleware for downloading a `url` source, runtime-only like `headers`. A download never uses the caller's
129
+ * scoped middleware (`RequestExecutor.withMiddleware`), so hooks that add provider credentials can't reach another
130
+ * host. Routes set this to that middleware only for result URLs on the provider's origin.
131
+ */
132
+ readonly middleware?: HttpMiddleware;
133
+ }
134
+ }
135
+ /** JSON form of an asset: the serializable `Source` plus caller-supplied metadata. `bytes` sources encode as base64. */
136
+ export declare const AssetEncoded: Schema.Struct<{
137
+ readonly source: Schema.Union<readonly [Schema.Struct<{
138
+ readonly type: Schema.Literal<"bytes">;
139
+ readonly data: Schema.Uint8Array;
140
+ readonly mediaType: Schema.String;
141
+ }>, Schema.Struct<{
142
+ readonly type: Schema.Literal<"base64">;
143
+ readonly data: Schema.String;
144
+ readonly mediaType: Schema.String;
145
+ }>, Schema.Struct<{
146
+ readonly type: Schema.Literal<"url">;
147
+ readonly url: Schema.String;
148
+ readonly mediaType: Schema.optional<Schema.String>;
149
+ /** Epoch milliseconds after which the provider no longer serves the URL. */
150
+ readonly expiresAt: Schema.optional<Schema.Number>;
151
+ }>, Schema.Struct<{
152
+ readonly type: Schema.Literal<"ref">;
153
+ readonly provider: Schema.brand<Schema.String, "AI.ProviderID">;
154
+ readonly id: Schema.String;
155
+ readonly mediaType: Schema.optional<Schema.String>;
156
+ }>]>;
157
+ readonly info: Schema.optional<Schema.Struct<{
158
+ readonly width: Schema.optional<Schema.Number>;
159
+ readonly height: Schema.optional<Schema.Number>;
160
+ readonly durationSeconds: Schema.optional<Schema.Number>;
161
+ readonly sampleRate: Schema.optional<Schema.Number>;
162
+ readonly channels: Schema.optional<Schema.Number>;
163
+ readonly encoding: Schema.optional<Schema.String>;
164
+ readonly format: Schema.optional<Schema.String>;
165
+ }>>;
166
+ readonly providerMetadata: Schema.optional<Schema.$Record<Schema.String, Schema.$Record<Schema.String, Schema.Unknown>>>;
167
+ }>;
168
+ /** `Asset` in the type domain and `AssetEncoded` on the wire, so messages and events holding assets serialize. */
169
+ export declare const AssetSchema: Schema.decodeTo<Schema.declare<Asset, Asset>, Schema.Struct<{
170
+ readonly source: Schema.Union<readonly [Schema.Struct<{
171
+ readonly type: Schema.Literal<"bytes">;
172
+ readonly data: Schema.Uint8Array;
173
+ readonly mediaType: Schema.String;
174
+ }>, Schema.Struct<{
175
+ readonly type: Schema.Literal<"base64">;
176
+ readonly data: Schema.String;
177
+ readonly mediaType: Schema.String;
178
+ }>, Schema.Struct<{
179
+ readonly type: Schema.Literal<"url">;
180
+ readonly url: Schema.String;
181
+ readonly mediaType: Schema.optional<Schema.String>;
182
+ /** Epoch milliseconds after which the provider no longer serves the URL. */
183
+ readonly expiresAt: Schema.optional<Schema.Number>;
184
+ }>, Schema.Struct<{
185
+ readonly type: Schema.Literal<"ref">;
186
+ readonly provider: Schema.brand<Schema.String, "AI.ProviderID">;
187
+ readonly id: Schema.String;
188
+ readonly mediaType: Schema.optional<Schema.String>;
189
+ }>]>;
190
+ readonly info: Schema.optional<Schema.Struct<{
191
+ readonly width: Schema.optional<Schema.Number>;
192
+ readonly height: Schema.optional<Schema.Number>;
193
+ readonly durationSeconds: Schema.optional<Schema.Number>;
194
+ readonly sampleRate: Schema.optional<Schema.Number>;
195
+ readonly channels: Schema.optional<Schema.Number>;
196
+ readonly encoding: Schema.optional<Schema.String>;
197
+ readonly format: Schema.optional<Schema.String>;
198
+ }>>;
199
+ readonly providerMetadata: Schema.optional<Schema.$Record<Schema.String, Schema.$Record<Schema.String, Schema.Unknown>>>;
200
+ }>, never, never>;
201
+ /**
202
+ * An image, or a URL or provider file reference whose type it doesn't state, which the provider checks itself. Build one with
203
+ * `Media.image`; an asset known to be a video, audio or document is rejected.
204
+ */
205
+ export declare const ImageAssetSchema: Schema.brand<Schema.declare<Asset, Asset>, "Media.ImageAsset">;
206
+ export type ImageAsset = typeof ImageAssetSchema.Type;
207
+ export type AssetOptions = Omit<Asset.Input, "source">;
208
+ export declare const from: (source: Source, options?: AssetOptions) => Asset;
209
+ export declare const bytes: (data: Uint8Array, mediaType?: string, options?: AssetOptions) => Asset;
210
+ export declare const base64: (data: string, mediaType: string, options?: AssetOptions) => Asset;
211
+ export declare const url: (value: string, options?: AssetOptions & Omit<Extract<Source, {
212
+ readonly type: "url";
213
+ }>, "type" | "url">) => Asset;
214
+ export declare const ref: (provider: string | ProviderID, id: string, mediaType?: string, options?: AssetOptions) => Asset;
215
+ /** Parse a `data:<mime>;base64,<data>` URL, or `undefined` when the value is not a base64 data URL. */
216
+ export declare const parseDataUrl: (value: string, options?: AssetOptions) => Asset | undefined;
217
+ /** Parse a `data:<mime>;base64,<data>` URL. Malformed input throws a typed `AIError` because constructors are sync. */
218
+ export declare const fromDataUrl: (dataUrl: string, options?: AssetOptions) => Asset;
219
+ /** Plain file data: bytes, a URL, or a string holding a data URL, an http(s) URL or base64. */
220
+ export type Data = Uint8Array | ArrayBuffer | URL | string;
221
+ /** What file inputs accept: an `Asset` or plain `Data`. */
222
+ export type Input = Asset | Data;
223
+ /** Normalize input into an `ImageAsset`; throws when it's known not to be an image. */
224
+ export declare const image: (input: Input, mediaType?: string) => ImageAsset;
225
+ /**
226
+ * Normalize file input into an `Asset`. Data URLs carry their media type and bytes or base64 are sniffed from magic
227
+ * bytes; `mediaType` is required when sniffing fails. Assets pass through unchanged.
228
+ */
229
+ export declare const make: (input: Input, mediaType?: string) => Asset;
230
+ /** Read a file through `FileSystem` and sniff its media type from magic bytes, then the extension. */
231
+ export declare const file: (path: string, options?: AssetOptions) => Effect.Effect<Asset, AIError, FileSystem.FileSystem>;
232
+ /**
233
+ * Stream an asset's bytes into a file through `FileSystem` without buffering `url` downloads. The bytes go to a temporary
234
+ * file next to `path` that replaces it only once the download finished, so a failed download leaves an existing file as
235
+ * it was.
236
+ */
237
+ export declare const write: (asset: Asset, path: string) => Effect.Effect<void, AIError, FileSystem.FileSystem | Service>;
package/dist/media.js ADDED
@@ -0,0 +1,285 @@
1
+ export * as Media from "./media.js";
2
+ import { Effect, FileSystem, Result, Schema, SchemaGetter, Stream } from "effect";
3
+ import { Base64 } from "effect/encoding";
4
+ import { HttpClientRequest } from "effect/http";
5
+ import { ProviderID } from "./schema/ids.js";
6
+ import { AIError, InvalidRequestError } from "./schema/errors.js";
7
+ import { ProviderMetadata } from "./schema/options.js";
8
+ import { CurrentMiddleware, responseBytes, responseStream, Service, } from "./route/executor-service.js";
9
+ import { detectMediaType, fileMediaType } from "./utils/media-type.js";
10
+ export { detectMediaType } from "./utils/media-type.js";
11
+ const OCTET_STREAM = "application/octet-stream";
12
+ // ---------------------------------------------------------------------------
13
+ // Source — the serializable wire/persistence form of a media asset
14
+ // ---------------------------------------------------------------------------
15
+ const BytesSource = Schema.Struct({
16
+ type: Schema.Literal("bytes"),
17
+ data: Schema.Uint8Array,
18
+ mediaType: Schema.String,
19
+ });
20
+ const Base64Source = Schema.Struct({
21
+ type: Schema.Literal("base64"),
22
+ data: Schema.String,
23
+ mediaType: Schema.String,
24
+ });
25
+ const UrlSource = Schema.Struct({
26
+ type: Schema.Literal("url"),
27
+ url: Schema.String,
28
+ mediaType: Schema.optional(Schema.String),
29
+ /** Epoch milliseconds after which the provider no longer serves the URL. */
30
+ expiresAt: Schema.optional(Schema.Number),
31
+ });
32
+ /** A provider-side handle: OpenAI `file_id`, Gemini file URI, `gs://`, or a prior generation id. */
33
+ const RefSource = Schema.Struct({
34
+ type: Schema.Literal("ref"),
35
+ provider: ProviderID,
36
+ id: Schema.String,
37
+ mediaType: Schema.optional(Schema.String),
38
+ });
39
+ export const Source = Schema.Union([BytesSource, Base64Source, UrlSource, RefSource])
40
+ .pipe(Schema.toTaggedUnion("type"))
41
+ .annotate({ identifier: "Media.Source" });
42
+ export const AspectRatio = Schema.declare((value) => typeof value === "string" && /^\d+(?:\.\d+)?:\d+(?:\.\d+)?$/.test(value), { title: "Media.AspectRatio" });
43
+ export const Kind = Schema.Literals(["image", "video", "audio", "document", "other"]);
44
+ export const kindOf = (mediaType) => {
45
+ const lower = mediaType.toLowerCase();
46
+ if (lower.startsWith("image/"))
47
+ return "image";
48
+ if (lower.startsWith("video/"))
49
+ return "video";
50
+ if (lower.startsWith("audio/"))
51
+ return "audio";
52
+ if (lower === "application/pdf" || lower.startsWith("text/"))
53
+ return "document";
54
+ return "other";
55
+ };
56
+ /** Container-independent facts about the payload; raw PCM audio relies on these because it has no header. */
57
+ export const Info = Schema.Struct({
58
+ width: Schema.optional(Schema.Number),
59
+ height: Schema.optional(Schema.Number),
60
+ durationSeconds: Schema.optional(Schema.Number),
61
+ sampleRate: Schema.optional(Schema.Number),
62
+ channels: Schema.optional(Schema.Number),
63
+ encoding: Schema.optional(Schema.String),
64
+ format: Schema.optional(Schema.String),
65
+ }).annotate({ identifier: "Media.Info" });
66
+ // ---------------------------------------------------------------------------
67
+ // Asset
68
+ // ---------------------------------------------------------------------------
69
+ const invalid = (message, cause) => new AIError({ reason: new InvalidRequestError({ message, cause }) });
70
+ export class Asset {
71
+ source;
72
+ /** Derived from the source: declared type, sniffed magic bytes, then `application/octet-stream`. */
73
+ mediaType;
74
+ kind;
75
+ info;
76
+ /** Epoch milliseconds after which a `url` source stops resolving. */
77
+ expiresAt;
78
+ providerMetadata;
79
+ /** Transient download credentials for `url` sources; see `Asset.Input.headers`. */
80
+ headers;
81
+ /** Middleware for downloading a `url` source; see `Asset.Input.middleware`. */
82
+ middleware;
83
+ // Derived payload forms are cached on the instance because every protocol lowering re-reads the same payload. The
84
+ // cache is check-then-set (concurrent first reads of a `url` source may both download) and is never observable
85
+ // through `source`, so round-tripping through `Media.from(asset.source)` stays lossless.
86
+ #bytes;
87
+ #base64;
88
+ constructor(input) {
89
+ this.source = input.source;
90
+ this.mediaType =
91
+ input.source.mediaType ??
92
+ (input.source.type === "bytes" ? detectMediaType(input.source.data) : undefined) ??
93
+ OCTET_STREAM;
94
+ this.kind = kindOf(this.mediaType);
95
+ this.info = input.info;
96
+ this.expiresAt = input.source.type === "url" ? input.source.expiresAt : undefined;
97
+ this.providerMetadata = input.providerMetadata;
98
+ this.headers = input.source.type === "url" ? input.headers : undefined;
99
+ this.middleware = input.source.type === "url" ? input.middleware : undefined;
100
+ }
101
+ /** Inline payload without effects, for protocols that embed base64 or data URLs directly. */
102
+ inline() {
103
+ const source = this.source;
104
+ if (source.type !== "bytes" && source.type !== "base64")
105
+ return undefined;
106
+ const base64 = source.type === "base64" ? source.data : (this.#base64 ??= Base64.encode(source.data));
107
+ const mime = this.mediaType.toLowerCase();
108
+ return { mime, base64, dataUrl: `data:${mime};base64,${base64}` };
109
+ }
110
+ /** Decoded payload; downloads `url` sources through the request executor and caches the result. */
111
+ bytes() {
112
+ return Effect.suspend(() => {
113
+ const source = this.source;
114
+ if (source.type === "bytes")
115
+ return Effect.succeed(source.data);
116
+ if (this.#bytes !== undefined)
117
+ return Effect.succeed(this.#bytes);
118
+ if (source.type === "ref")
119
+ return Effect.fail(invalid(`Cannot materialize provider ref ${source.provider}:${source.id}`));
120
+ const decoded = source.type === "base64"
121
+ ? Effect.fromResult(Base64.decode(source.data)).pipe(Effect.mapError((cause) => invalid(`Media asset contains invalid base64 data`, cause)))
122
+ : download(source, this.headers, this.middleware);
123
+ return decoded.pipe(Effect.tap((data) => Effect.sync(() => (this.#bytes = data))));
124
+ });
125
+ }
126
+ /** The payload as a byte stream; `url` sources stream from the network without being buffered or cached. */
127
+ stream() {
128
+ const source = this.source;
129
+ if (source.type !== "url" || this.#bytes !== undefined)
130
+ return Stream.fromEffect(this.bytes());
131
+ return Stream.unwrap(downloadStream(source, this.headers, this.middleware));
132
+ }
133
+ base64() {
134
+ return Effect.suspend(() => {
135
+ const source = this.source;
136
+ if (source.type === "base64")
137
+ return Effect.succeed(source.data);
138
+ if (this.#base64 !== undefined)
139
+ return Effect.succeed(this.#base64);
140
+ return this.bytes().pipe(Effect.map((data) => (this.#base64 = Base64.encode(data))));
141
+ });
142
+ }
143
+ dataUrl() {
144
+ return this.base64().pipe(Effect.map((data) => `data:${this.mediaType};base64,${data}`));
145
+ }
146
+ /**
147
+ * The `AssetEncoded` JSON form with `bytes` sources as base64, matching `Schema.toCodecJson(AssetSchema)`, so a
148
+ * plain `JSON.stringify` of messages or events stays lossless and decodes back through the JSON codec.
149
+ */
150
+ toJSON() {
151
+ const source = this.source;
152
+ return {
153
+ source: source.type === "bytes" ? { ...source, data: Base64.encode(source.data) } : source,
154
+ info: this.info,
155
+ providerMetadata: this.providerMetadata,
156
+ };
157
+ }
158
+ /** Pull `url` sources into owned bytes before the URL expires. Inline sources return themselves. */
159
+ materialize() {
160
+ if (this.source.type === "bytes" || this.source.type === "base64")
161
+ return Effect.succeed(this);
162
+ return this.bytes().pipe(Effect.map((data) => bytes(data, this.source.mediaType, { info: this.info, providerMetadata: this.providerMetadata })));
163
+ }
164
+ }
165
+ /** JSON form of an asset: the serializable `Source` plus caller-supplied metadata. `bytes` sources encode as base64. */
166
+ export const AssetEncoded = Schema.Struct({
167
+ source: Source,
168
+ info: Schema.optional(Info),
169
+ providerMetadata: Schema.optional(ProviderMetadata),
170
+ }).annotate({ identifier: "Media.AssetEncoded" });
171
+ const encodeAsset = (asset) => ({
172
+ source: asset.source,
173
+ info: asset.info,
174
+ providerMetadata: asset.providerMetadata,
175
+ });
176
+ const AssetInstance = Schema.declare((value) => value instanceof Asset, {
177
+ expected: "Media.Asset",
178
+ });
179
+ /** `Asset` in the type domain and `AssetEncoded` on the wire, so messages and events holding assets serialize. */
180
+ export const AssetSchema = AssetEncoded.pipe(Schema.decodeTo(AssetInstance, {
181
+ decode: SchemaGetter.transform((encoded) => new Asset(encoded)),
182
+ encode: SchemaGetter.transform(encodeAsset),
183
+ }));
184
+ /**
185
+ * An image, or a URL or provider file reference whose type it doesn't state, which the provider checks itself. Build one with
186
+ * `Media.image`; an asset known to be a video, audio or document is rejected.
187
+ */
188
+ export const ImageAssetSchema = AssetInstance.check(Schema.makeFilter((asset) => asset.kind === "image" ||
189
+ ((asset.source.type === "url" || asset.source.type === "ref") && asset.source.mediaType === undefined) ||
190
+ `expected an image, got ${asset.mediaType}`)).pipe(Schema.brand("Media.ImageAsset"));
191
+ const download = Effect.fn("Media.download")(function* (source, headers, middleware) {
192
+ const executor = yield* Service;
193
+ const response = yield* executor
194
+ .execute(HttpClientRequest.get(source.url).pipe(HttpClientRequest.setHeaders(headers ?? {})))
195
+ .pipe(Effect.provideService(CurrentMiddleware, middleware));
196
+ return yield* responseBytes(response);
197
+ });
198
+ const downloadStream = Effect.fn("Media.downloadStream")(function* (source, headers, middleware) {
199
+ const executor = yield* Service;
200
+ const response = yield* executor
201
+ .execute(HttpClientRequest.get(source.url).pipe(HttpClientRequest.setHeaders(headers ?? {})))
202
+ .pipe(Effect.provideService(CurrentMiddleware, middleware));
203
+ return responseStream(response);
204
+ });
205
+ export const from = (source, options) => new Asset({ ...options, source });
206
+ export const bytes = (data, mediaType, options) => from({ type: "bytes", data, mediaType: mediaType ?? detectMediaType(data) ?? OCTET_STREAM }, options);
207
+ export const base64 = (data, mediaType, options) => from({ type: "base64", data, mediaType }, options);
208
+ export const url = (value, options) => {
209
+ const { mediaType, expiresAt, ...rest } = options ?? {};
210
+ return from({ type: "url", url: value, mediaType, expiresAt }, rest);
211
+ };
212
+ export const ref = (provider, id, mediaType, options) => from({ type: "ref", provider: ProviderID.make(provider), id, mediaType }, options);
213
+ const DATA_URL = /^data:([^;,]+)(?:;[^,]*)*;base64,(.*)$/is;
214
+ /** Parse a `data:<mime>;base64,<data>` URL, or `undefined` when the value is not a base64 data URL. */
215
+ export const parseDataUrl = (value, options) => {
216
+ const match = DATA_URL.exec(value);
217
+ return match === null ? undefined : base64(match[2], match[1], options);
218
+ };
219
+ /** Parse a `data:<mime>;base64,<data>` URL. Malformed input throws a typed `AIError` because constructors are sync. */
220
+ export const fromDataUrl = (dataUrl, options) => {
221
+ const asset = parseDataUrl(dataUrl, options);
222
+ if (asset === undefined)
223
+ throw invalid("Media data URLs must contain a MIME type and base64 data");
224
+ return asset;
225
+ };
226
+ /** Normalize input into an `ImageAsset`; throws when it's known not to be an image. */
227
+ export const image = (input, mediaType) => {
228
+ const asset = make(input, mediaType);
229
+ if (!Schema.is(ImageAssetSchema)(asset))
230
+ throw invalid(`expected an image, got ${asset.mediaType}`);
231
+ return asset;
232
+ };
233
+ /**
234
+ * Normalize file input into an `Asset`. Data URLs carry their media type and bytes or base64 are sniffed from magic
235
+ * bytes; `mediaType` is required when sniffing fails. Assets pass through unchanged.
236
+ */
237
+ export const make = (input, mediaType) => {
238
+ if (input instanceof Asset)
239
+ return input;
240
+ if (input instanceof ArrayBuffer)
241
+ return make(new Uint8Array(input), mediaType);
242
+ if (input instanceof Uint8Array)
243
+ return bytes(input, mediaType ?? requireMediaType(detectMediaType(input)));
244
+ const value = input instanceof URL ? input.href : input;
245
+ if (/^data:/i.test(value)) {
246
+ const match = DATA_URL.exec(value);
247
+ if (match === null)
248
+ throw invalid("Media data URLs must contain a MIME type and base64 data");
249
+ return base64(match[2], mediaType ?? match[1]);
250
+ }
251
+ if (input instanceof URL || /^https?:\/\//i.test(value))
252
+ return url(value, { mediaType });
253
+ if (!BASE64.test(value))
254
+ throw invalid("Media strings must be base64, a data URL or an http(s) URL; read files with Media.file");
255
+ return base64(value, mediaType ?? requireMediaType(sniffBase64(value)));
256
+ };
257
+ const BASE64 = /^[A-Za-z0-9+/\-_\s]*={0,2}\s*$/;
258
+ // 64 characters decode to 48 bytes, enough for every signature `detectMediaType` reads.
259
+ const sniffBase64 = (value) => {
260
+ const head = Result.getOrUndefined(Base64.decode(value.slice(0, 64)));
261
+ return head === undefined ? undefined : detectMediaType(head);
262
+ };
263
+ const requireMediaType = (detected) => {
264
+ if (detected === undefined)
265
+ throw invalid("Media data needs a mediaType because its type cannot be detected");
266
+ return detected;
267
+ };
268
+ /** Read a file through `FileSystem` and sniff its media type from magic bytes, then the extension. */
269
+ export const file = (path, options) => Effect.gen(function* () {
270
+ const fs = yield* FileSystem.FileSystem;
271
+ const data = yield* fs
272
+ .readFile(path)
273
+ .pipe(Effect.mapError((cause) => invalid(`Failed to read media file ${path}`, cause)));
274
+ return bytes(data, fileMediaType(data, path), options);
275
+ });
276
+ /**
277
+ * Stream an asset's bytes into a file through `FileSystem` without buffering `url` downloads. The bytes go to a temporary
278
+ * file next to `path` that replaces it only once the download finished, so a failed download leaves an existing file as
279
+ * it was.
280
+ */
281
+ export const write = (asset, path) => Effect.gen(function* () {
282
+ const fs = yield* FileSystem.FileSystem;
283
+ const temporary = `${path}.${crypto.randomUUID()}.tmp`;
284
+ yield* asset.stream().pipe(Stream.run(fs.sink(temporary)), Effect.andThen(fs.rename(temporary, path)), Effect.onError(() => fs.remove(temporary).pipe(Effect.ignore)), Effect.mapError((cause) => cause instanceof AIError ? cause : invalid(`Failed to write media file ${path}`, cause)));
285
+ });
@@ -0,0 +1,20 @@
1
+ export * as Media from "./promise-media.js";
2
+ import { type FetchMiddleware } from "./fetch-middleware.js";
3
+ import { Media } from "./media.js";
4
+ export { AspectRatio, Asset, AssetEncoded, AssetSchema, detectMediaType, ImageAssetSchema, image, Info, Kind, kindOf, make, Source, } from "./media.js";
5
+ export type { Data, ImageAsset, Inline, Input } from "./media.js";
6
+ export type AssetOptions = Omit<Media.AssetOptions, "middleware"> & {
7
+ /** Middleware for downloading a `url` source, with fetch types; see `Media.Asset.Input.middleware`. */
8
+ readonly middleware?: FetchMiddleware;
9
+ };
10
+ export declare const from: (source: Media.Source, options?: AssetOptions) => Media.Asset;
11
+ export declare const bytes: (data: Uint8Array, mediaType?: string, options?: AssetOptions) => Media.Asset;
12
+ export declare const base64: (data: string, mediaType: string, options?: AssetOptions) => Media.Asset;
13
+ export declare const url: (value: string, options?: AssetOptions & Omit<Extract<Media.Source, {
14
+ readonly type: "url";
15
+ }>, "type" | "url">) => Media.Asset;
16
+ export declare const ref: (provider: Parameters<typeof Media.ref>[0], id: string, mediaType?: string, options?: AssetOptions) => Media.Asset;
17
+ /** Parse a `data:<mime>;base64,<data>` URL, or `undefined` when the value is not a base64 data URL. */
18
+ export declare const parseDataUrl: (value: string, options?: AssetOptions) => Media.Asset | undefined;
19
+ /** Parse a `data:<mime>;base64,<data>` URL. Malformed input throws a typed `AIError`. */
20
+ export declare const fromDataUrl: (dataUrl: string, options?: AssetOptions) => Media.Asset;
@@ -0,0 +1,16 @@
1
+ export * as Media from "./promise-media.js";
2
+ import { fromFetch } from "./fetch-middleware.js";
3
+ import { Media } from "./media.js";
4
+ // `Media` for the Promise API: the Effect constructors with download `middleware` typed like `RunOptions.middleware`.
5
+ // `Media.file` and `Media.write` stay Effect-only; Promise callers use `ai.file`, `ai.write`, `ai.bytes` and friends.
6
+ export { AspectRatio, Asset, AssetEncoded, AssetSchema, detectMediaType, ImageAssetSchema, image, Info, Kind, kindOf, make, Source, } from "./media.js";
7
+ export const from = (source, options) => Media.from(source, effectOptions(options));
8
+ export const bytes = (data, mediaType, options) => Media.bytes(data, mediaType, effectOptions(options));
9
+ export const base64 = (data, mediaType, options) => Media.base64(data, mediaType, effectOptions(options));
10
+ export const url = (value, options) => Media.url(value, effectOptions(options));
11
+ export const ref = (provider, id, mediaType, options) => Media.ref(provider, id, mediaType, effectOptions(options));
12
+ /** Parse a `data:<mime>;base64,<data>` URL, or `undefined` when the value is not a base64 data URL. */
13
+ export const parseDataUrl = (value, options) => Media.parseDataUrl(value, effectOptions(options));
14
+ /** Parse a `data:<mime>;base64,<data>` URL. Malformed input throws a typed `AIError`. */
15
+ export const fromDataUrl = (dataUrl, options) => Media.fromDataUrl(dataUrl, effectOptions(options));
16
+ const effectOptions = (options) => options === undefined ? undefined : { ...options, middleware: options.middleware && fromFetch(options.middleware) };