@opencode/ai 2.0.14 → 2.0.16

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 (208) hide show
  1. package/README.md +399 -56
  2. package/dist/experimental/evaluation-client.d.ts +1 -1
  3. package/dist/experimental/evaluation-client.js +39 -3
  4. package/dist/experimental/evaluation.d.ts +4 -4
  5. package/dist/experimental/evaluation.js +2 -2
  6. package/dist/experimental/system-one.d.ts +3 -3
  7. package/dist/experimental/system-one.js +40 -51
  8. package/dist/generation.d.ts +83 -0
  9. package/dist/generation.js +113 -0
  10. package/dist/image-client.d.ts +16 -7
  11. package/dist/image-client.js +29 -13
  12. package/dist/image.d.ts +1410 -81
  13. package/dist/image.js +97 -67
  14. package/dist/index.d.ts +17 -2
  15. package/dist/index.js +12 -1
  16. package/dist/llm.d.ts +9 -1
  17. package/dist/media-model.d.ts +44 -0
  18. package/dist/media-model.js +49 -0
  19. package/dist/media.d.ts +213 -0
  20. package/dist/media.js +227 -0
  21. package/dist/promise.d.ts +974 -0
  22. package/dist/promise.js +81 -0
  23. package/dist/protocols/alibaba-chat.d.ts +12 -0
  24. package/dist/protocols/alibaba-responses.d.ts +2 -2
  25. package/dist/protocols/anthropic-messages.js +8 -19
  26. package/dist/protocols/assemblyai-transcription.d.ts +40 -0
  27. package/dist/protocols/assemblyai-transcription.js +138 -0
  28. package/dist/protocols/bedrock-converse.d.ts +4 -4
  29. package/dist/protocols/bedrock-converse.js +6 -17
  30. package/dist/protocols/bfl-images.d.ts +32 -0
  31. package/dist/protocols/bfl-images.js +153 -0
  32. package/dist/protocols/cartesia-speech.d.ts +127 -0
  33. package/dist/protocols/cartesia-speech.js +126 -0
  34. package/dist/protocols/deepgram-speech.d.ts +119 -0
  35. package/dist/protocols/deepgram-speech.js +92 -0
  36. package/dist/protocols/deepgram-transcription.d.ts +25 -0
  37. package/dist/protocols/deepgram-transcription.js +129 -0
  38. package/dist/protocols/elevenlabs-speech.d.ts +122 -0
  39. package/dist/protocols/elevenlabs-speech.js +115 -0
  40. package/dist/protocols/fal-images.d.ts +24 -0
  41. package/dist/protocols/fal-images.js +114 -0
  42. package/dist/protocols/fal-video.d.ts +29 -0
  43. package/dist/protocols/fal-video.js +88 -0
  44. package/dist/protocols/gemini.d.ts +30 -9
  45. package/dist/protocols/gemini.js +45 -35
  46. package/dist/protocols/google-images.d.ts +9 -21
  47. package/dist/protocols/google-images.js +158 -133
  48. package/dist/protocols/google-speech.d.ts +130 -0
  49. package/dist/protocols/google-speech.js +84 -0
  50. package/dist/protocols/google-transcription.d.ts +173 -0
  51. package/dist/protocols/google-transcription.js +138 -0
  52. package/dist/protocols/google-video.d.ts +26 -0
  53. package/dist/protocols/google-video.js +158 -0
  54. package/dist/protocols/meta-images.d.ts +7 -12
  55. package/dist/protocols/meta-images.js +85 -66
  56. package/dist/protocols/meta-responses.d.ts +4 -4
  57. package/dist/protocols/meta-responses.js +1 -1
  58. package/dist/protocols/mistral-chat.js +7 -6
  59. package/dist/protocols/open-responses.d.ts +17 -9
  60. package/dist/protocols/open-responses.js +24 -14
  61. package/dist/protocols/openai-chat.d.ts +118 -1
  62. package/dist/protocols/openai-chat.js +125 -44
  63. package/dist/protocols/openai-compatible-chat.d.ts +12 -0
  64. package/dist/protocols/openai-compatible-responses.d.ts +2 -2
  65. package/dist/protocols/openai-images.d.ts +128 -18
  66. package/dist/protocols/openai-images.js +177 -154
  67. package/dist/protocols/openai-responses.d.ts +15 -15
  68. package/dist/protocols/openai-responses.js +5 -6
  69. package/dist/protocols/openai-speech.d.ts +116 -0
  70. package/dist/protocols/openai-speech.js +98 -0
  71. package/dist/protocols/openai-transcription.d.ts +207 -0
  72. package/dist/protocols/openai-transcription.js +190 -0
  73. package/dist/protocols/replicate-images.d.ts +28 -0
  74. package/dist/protocols/replicate-images.js +133 -0
  75. package/dist/protocols/runway-video.d.ts +38 -0
  76. package/dist/protocols/runway-video.js +146 -0
  77. package/dist/protocols/shared.d.ts +27 -17
  78. package/dist/protocols/shared.js +52 -35
  79. package/dist/protocols/stability-images.d.ts +38 -0
  80. package/dist/protocols/stability-images.js +148 -0
  81. package/dist/protocols/utils/bedrock-media.d.ts +2 -3
  82. package/dist/protocols/utils/bedrock-media.js +4 -4
  83. package/dist/protocols/utils/fal-queue.d.ts +28 -0
  84. package/dist/protocols/utils/fal-queue.js +69 -0
  85. package/dist/protocols/utils/gemini-generate-content.d.ts +65 -0
  86. package/dist/protocols/utils/gemini-generate-content.js +65 -0
  87. package/dist/protocols/utils/gemini-json-schema.d.ts +3 -0
  88. package/dist/protocols/utils/gemini-json-schema.js +76 -0
  89. package/dist/protocols/utils/media-input.d.ts +18 -0
  90. package/dist/protocols/utils/media-input.js +35 -0
  91. package/dist/protocols/utils/responses-compaction.js +6 -5
  92. package/dist/protocols/utils/speech-stream.d.ts +49 -0
  93. package/dist/protocols/utils/speech-stream.js +67 -0
  94. package/dist/protocols/utils/tool-schema.d.ts +2 -2
  95. package/dist/protocols/utils/tool-schema.js +40 -17
  96. package/dist/protocols/utils/tool-stream.d.ts +27 -3
  97. package/dist/protocols/xai-images.d.ts +9 -15
  98. package/dist/protocols/xai-images.js +75 -84
  99. package/dist/protocols/xai-responses.d.ts +2 -2
  100. package/dist/protocols/xai-video.d.ts +34 -0
  101. package/dist/protocols/xai-video.js +147 -0
  102. package/dist/protocols/zai-chat.d.ts +13 -1
  103. package/dist/protocols/zai-images.d.ts +9 -13
  104. package/dist/protocols/zai-images.js +59 -57
  105. package/dist/provider-error.js +3 -0
  106. package/dist/providers/alibaba.d.ts +14 -2
  107. package/dist/providers/amazon-bedrock-mantle.d.ts +14 -2
  108. package/dist/providers/amazon-bedrock.d.ts +2 -2
  109. package/dist/providers/assemblyai.d.ts +25 -0
  110. package/dist/providers/assemblyai.js +29 -0
  111. package/dist/providers/azure.d.ts +18 -6
  112. package/dist/providers/baseten.d.ts +24 -0
  113. package/dist/providers/black-forest-labs.d.ts +25 -0
  114. package/dist/providers/black-forest-labs.js +28 -0
  115. package/dist/providers/cartesia.d.ts +24 -0
  116. package/dist/providers/cartesia.js +22 -0
  117. package/dist/providers/cerebras.d.ts +24 -0
  118. package/dist/providers/cerebras.js +6 -1
  119. package/dist/providers/cloudflare-ai-gateway.d.ts +30 -6
  120. package/dist/providers/cloudflare-workers-ai.d.ts +24 -0
  121. package/dist/providers/deepgram.d.ts +29 -0
  122. package/dist/providers/deepgram.js +31 -0
  123. package/dist/providers/deepinfra.d.ts +24 -0
  124. package/dist/providers/deepinfra.js +6 -1
  125. package/dist/providers/deepseek.d.ts +24 -0
  126. package/dist/providers/elevenlabs.d.ts +24 -0
  127. package/dist/providers/elevenlabs.js +28 -0
  128. package/dist/providers/fal.d.ts +29 -0
  129. package/dist/providers/fal.js +33 -0
  130. package/dist/providers/fireworks.d.ts +24 -0
  131. package/dist/providers/google-vertex-chat.d.ts +12 -0
  132. package/dist/providers/google-vertex-responses.d.ts +2 -2
  133. package/dist/providers/google-vertex.d.ts +10 -3
  134. package/dist/providers/google.d.ts +25 -3
  135. package/dist/providers/google.js +11 -2
  136. package/dist/providers/groq.d.ts +24 -0
  137. package/dist/providers/index.d.ts +10 -0
  138. package/dist/providers/index.js +10 -0
  139. package/dist/providers/meta.d.ts +14 -2
  140. package/dist/providers/minimax.d.ts +14 -2
  141. package/dist/providers/moonshot.d.ts +14 -2
  142. package/dist/providers/moonshot.js +3 -3
  143. package/dist/providers/openai-compatible-responses.d.ts +2 -2
  144. package/dist/providers/openai-compatible.d.ts +12 -0
  145. package/dist/providers/openai.d.ts +25 -3
  146. package/dist/providers/openai.js +10 -1
  147. package/dist/providers/openrouter.d.ts +67 -0
  148. package/dist/providers/openrouter.js +13 -1
  149. package/dist/providers/replicate.d.ts +25 -0
  150. package/dist/providers/replicate.js +22 -0
  151. package/dist/providers/runway.d.ts +24 -0
  152. package/dist/providers/runway.js +22 -0
  153. package/dist/providers/stability.d.ts +28 -0
  154. package/dist/providers/stability.js +23 -0
  155. package/dist/providers/togetherai.d.ts +24 -0
  156. package/dist/providers/vercel-ai-gateway.d.ts +41 -0
  157. package/dist/providers/vercel-ai-gateway.js +85 -0
  158. package/dist/providers/xai.d.ts +17 -0
  159. package/dist/providers/xai.js +5 -2
  160. package/dist/providers/zai-coding-plan.d.ts +15 -3
  161. package/dist/providers/zai.d.ts +13 -1
  162. package/dist/route/auth.d.ts +4 -1
  163. package/dist/route/auth.js +6 -0
  164. package/dist/route/client.d.ts +9 -1
  165. package/dist/route/endpoint.d.ts +10 -10
  166. package/dist/route/executor-service.d.ts +12 -0
  167. package/dist/route/executor-service.js +3 -0
  168. package/dist/route/executor.d.ts +4 -9
  169. package/dist/route/executor.js +3 -3
  170. package/dist/route/framing.d.ts +5 -1
  171. package/dist/route/framing.js +9 -0
  172. package/dist/route/index.d.ts +2 -0
  173. package/dist/route/index.js +2 -0
  174. package/dist/route/media-protocol.d.ts +158 -0
  175. package/dist/route/media-protocol.js +96 -0
  176. package/dist/route/media.d.ts +97 -0
  177. package/dist/route/media.js +236 -0
  178. package/dist/schema/errors.d.ts +13 -3
  179. package/dist/schema/errors.js +7 -0
  180. package/dist/schema/events.d.ts +557 -40
  181. package/dist/schema/events.js +35 -2
  182. package/dist/schema/messages.d.ts +95 -8
  183. package/dist/schema/messages.js +8 -6
  184. package/dist/schema/options.d.ts +6 -3
  185. package/dist/schema/options.js +6 -2
  186. package/dist/speech-client.d.ts +21 -0
  187. package/dist/speech-client.js +25 -0
  188. package/dist/speech.d.ts +1150 -0
  189. package/dist/speech.js +119 -0
  190. package/dist/testing.d.ts +72 -8
  191. package/dist/transcription-client.d.ts +28 -0
  192. package/dist/transcription-client.js +44 -0
  193. package/dist/transcription.d.ts +1504 -0
  194. package/dist/transcription.js +133 -0
  195. package/dist/utils/bytes.d.ts +1 -0
  196. package/dist/utils/bytes.js +10 -0
  197. package/dist/utils/media-type.d.ts +7 -0
  198. package/dist/utils/media-type.js +70 -0
  199. package/dist/utils/sanitize.js +3 -1
  200. package/dist/video-client.d.ts +28 -0
  201. package/dist/video-client.js +40 -0
  202. package/dist/video.d.ts +1359 -0
  203. package/dist/video.js +119 -0
  204. package/package.json +7 -3
  205. package/dist/protocols/utils/gemini-tool-schema.d.ts +0 -2
  206. package/dist/protocols/utils/gemini-tool-schema.js +0 -103
  207. package/dist/protocols/utils/image-input.d.ts +0 -21
  208. package/dist/protocols/utils/image-input.js +0 -20
package/dist/image.js CHANGED
@@ -1,103 +1,133 @@
1
- import { Effect, Schema } from "effect";
2
- import { HttpOptions, InvalidRequestError, AIError, ModelID, ProviderID, ProviderMetadata, Usage, } from "./schema/index.js";
1
+ import { Effect, Schema, Stream } from "effect";
2
+ import { Generation, ProgressEvent, QueuedEvent } from "./generation.js";
3
+ import { Media } from "./media.js";
4
+ import { MediaModel, composeAnyRoute, tryRequest } from "./media-model.js";
5
+ import { MediaRoute } from "./route/media.js";
6
+ import { AIError, HttpOptions, MediaUsage, ProviderMetadata } from "./schema/index.js";
3
7
  import { ImageClient, Service } from "./image-client.js";
4
- export class ImageModel {
5
- id;
6
- provider;
7
- route;
8
- http;
9
- constructor(input) {
10
- this.id = input.id;
11
- this.provider = input.provider;
12
- this.route = input.route;
13
- this.http = input.http;
14
- }
8
+ export class ImageModel extends MediaModel {
15
9
  static make(input) {
10
+ return new ImageModel(input);
11
+ }
12
+ static fromRoute(route, input) {
16
13
  return new ImageModel({
17
- id: ModelID.make(input.id),
18
- provider: ProviderID.make(input.provider),
19
- route: input.route,
14
+ id: input.id,
15
+ provider: route.provider,
20
16
  http: input.http,
17
+ route: composeAnyRoute(route, input, collectResponse),
21
18
  });
22
19
  }
23
20
  }
24
21
  export const ImageModelSchema = Schema.declare((value) => value instanceof ImageModel, {
25
22
  expected: "Image.Model",
26
23
  });
27
- const ImageBytesInput = Schema.Struct({
28
- type: Schema.Literal("bytes"),
29
- data: Schema.Uint8Array,
30
- mediaType: Schema.String,
31
- });
32
- const ImageUrlInput = Schema.Struct({
33
- type: Schema.Literal("url"),
34
- url: Schema.String,
35
- });
36
- const ImageFileIDInput = Schema.Struct({
37
- type: Schema.Literal("file-id"),
38
- id: Schema.String,
39
- });
40
- const ImageFileURIInput = Schema.Struct({
41
- type: Schema.Literal("file-uri"),
42
- uri: Schema.String,
43
- mediaType: Schema.String,
44
- });
45
- export const ImageInputSchema = Schema.Union([
46
- ImageBytesInput,
47
- ImageUrlInput,
48
- ImageFileIDInput,
49
- ImageFileURIInput,
50
- ]).pipe(Schema.toTaggedUnion("type"));
51
- export const ImageInput = {
52
- bytes: (data, mediaType) => ({ type: "bytes", data, mediaType }),
53
- url: (url) => ({ type: "url", url }),
54
- file: (id) => ({ type: "file-id", id }),
55
- fileUri: (uri, mediaType) => ({ type: "file-uri", uri, mediaType }),
56
- };
24
+ export const ImageSize = Schema.declare((value) => typeof value === "string" && /^\d+x\d+$/.test(value), { title: "ImageSize" });
25
+ export const ImageAspectRatio = Media.AspectRatio;
57
26
  export class ImageRequest extends Schema.Class("Image.Request")({
58
27
  model: ImageModelSchema,
59
28
  prompt: Schema.String,
60
- images: Schema.optional(Schema.Array(ImageInputSchema)),
61
- options: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
29
+ /** Edit sources or style/subject references, in order. */
30
+ images: Schema.optional(Schema.Array(Media.AssetSchema)),
31
+ /** Inpainting mask; routes that cannot honor it fail with `UnsupportedOperation`. */
32
+ mask: Schema.optional(Media.AssetSchema),
33
+ n: Schema.optional(Schema.Int),
34
+ size: Schema.optional(ImageSize),
35
+ aspectRatio: Schema.optional(ImageAspectRatio),
36
+ seed: Schema.optional(Schema.Number),
37
+ format: Schema.optional(Schema.String),
38
+ providerOptions: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
62
39
  http: Schema.optional(HttpOptions),
63
40
  }) {
64
41
  }
65
- export class GeneratedImage extends Schema.Class("Image.Generated")({
66
- mediaType: Schema.String,
67
- data: Schema.Union([Schema.String, Schema.Uint8Array]),
68
- providerMetadata: Schema.optional(ProviderMetadata),
69
- }) {
70
- }
42
+ // ---------------------------------------------------------------------------
43
+ // Response and events
44
+ // ---------------------------------------------------------------------------
71
45
  export class ImageResponse extends Schema.Class("Image.Response")({
72
- images: Schema.Array(GeneratedImage),
73
- usage: Schema.optional(Usage),
46
+ images: Schema.Array(Media.AssetSchema),
47
+ usage: Schema.optional(MediaUsage),
48
+ notices: Schema.optional(Schema.Array(Media.Notice)),
74
49
  providerMetadata: Schema.optional(ProviderMetadata),
75
50
  }) {
76
51
  get image() {
77
52
  return this.images[0];
78
53
  }
79
54
  }
55
+ /** The `index`-th lower-fidelity preview of an image still generating; the `image` event supersedes it. */
56
+ export const ImagePartialEvent = Schema.Struct({
57
+ type: Schema.tag("image-partial"),
58
+ index: Schema.Number,
59
+ image: Media.AssetSchema,
60
+ }).annotate({ identifier: "Image.Event.Partial" });
61
+ export const ImageOutputEvent = Schema.Struct({
62
+ type: Schema.tag("image"),
63
+ index: Schema.Number,
64
+ image: Media.AssetSchema,
65
+ }).annotate({ identifier: "Image.Event.Image" });
66
+ export const ImageFinishEvent = Schema.Struct({
67
+ type: Schema.tag("finish"),
68
+ usage: Schema.optional(MediaUsage),
69
+ notices: Schema.optional(Schema.Array(Media.Notice)),
70
+ providerMetadata: Schema.optional(ProviderMetadata),
71
+ }).annotate({ identifier: "Image.Event.Finish" });
72
+ const imageEventTagged = Schema.Union([
73
+ QueuedEvent,
74
+ ProgressEvent,
75
+ ImagePartialEvent,
76
+ ImageOutputEvent,
77
+ ImageFinishEvent,
78
+ ]).pipe(Schema.toTaggedUnion("type"));
79
+ export const ImageEvent = Object.assign(imageEventTagged, {
80
+ is: {
81
+ generationQueued: imageEventTagged.guards["generation-queued"],
82
+ generationProgress: imageEventTagged.guards["generation-progress"],
83
+ imagePartial: imageEventTagged.guards["image-partial"],
84
+ image: imageEventTagged.guards.image,
85
+ finish: imageEventTagged.guards.finish,
86
+ },
87
+ });
88
+ export const responseEvents = (response) => [
89
+ ...response.images.map((image, index) => ImageOutputEvent.make({ index, image })),
90
+ ImageFinishEvent.make({
91
+ usage: response.usage,
92
+ notices: response.notices,
93
+ providerMetadata: response.providerMetadata,
94
+ }),
95
+ ];
96
+ const collectResponse = (events) => {
97
+ const finish = events.find(ImageEvent.is.finish);
98
+ // Every image protocol's `finish` emits the terminal event or fails, so a completed stream always has one.
99
+ if (finish === undefined)
100
+ return Effect.die(new Error("The image stream completed without a finish event"));
101
+ return Effect.succeed(new ImageResponse({
102
+ images: events.filter(ImageEvent.is.image).map((event) => event.image),
103
+ usage: finish.usage,
104
+ notices: finish.notices,
105
+ providerMetadata: finish.providerMetadata,
106
+ }));
107
+ };
80
108
  export function request(input) {
81
109
  if (input instanceof ImageRequest)
82
110
  return input;
83
111
  return new ImageRequest({
84
112
  ...input,
85
- model: input.model,
86
113
  http: input.http === undefined ? undefined : HttpOptions.make(input.http),
87
114
  });
88
115
  }
89
- export function generate(input) {
90
- return Effect.try({
91
- try: () => (input instanceof ImageRequest ? input : request(input)),
92
- catch: (error) => new AIError({
93
- reason: new InvalidRequestError({
94
- message: error instanceof Error ? error.message : String(error),
95
- cause: error,
96
- }),
97
- }),
98
- }).pipe(Effect.flatMap((request) => ImageClient.generate(request)));
116
+ const requestEffect = (input) => tryRequest(() => request(input));
117
+ export function generate(input, options) {
118
+ return requestEffect(input).pipe(Effect.flatMap((request) => ImageClient.generate(request, options)));
119
+ }
120
+ export function stream(input, options) {
121
+ return Stream.unwrap(requestEffect(input).pipe(Effect.map((request) => ImageClient.stream(request, options))));
122
+ }
123
+ export function start(input) {
124
+ return requestEffect(input).pipe(Effect.flatMap((request) => ImageClient.start(request)));
99
125
  }
126
+ export const resume = (model, token) => ImageClient.resume(model, token);
100
127
  export const Image = {
101
128
  request,
102
129
  generate,
130
+ stream,
131
+ start,
132
+ resume,
103
133
  };
package/dist/index.d.ts CHANGED
@@ -6,9 +6,24 @@ export { ProviderPackage } from "./provider-package.js";
6
6
  export { isContextOverflow, isContextOverflowFailure } from "./provider-error.js";
7
7
  export type { RouteLanguageModelInput, RouteRoutedLanguageModelInput, Interface as LLMClientShape, Service as LLMClientService, } from "./route/client.js";
8
8
  export * from "./schema/index.js";
9
- export { GeneratedImage, ImageInput, ImageInputSchema, ImageModel, ImageRequest, ImageResponse } from "./image.js";
10
- export type { ImageModelOptions, ImageOptions, ImageRequestFor, ImageRequestInput, ImageRoute } from "./image.js";
9
+ export { ImageAspectRatio, ImageEvent, ImageModel, ImageModelSchema, ImageRequest, ImageResponse, ImageSize, } from "./image.js";
10
+ export type { ImageFormat, ImageModelOptions, ImageOptions, ImageRequestFor, ImageRequestInput, ImageRoute, } from "./image.js";
11
11
  export { Image } from "./image.js";
12
+ export { VideoClient } from "./video-client.js";
13
+ export { VideoAspectRatio, VideoEvent, VideoFrames, VideoModel, VideoModelSchema, VideoRequest, VideoResponse, } from "./video.js";
14
+ export type { VideoModelOptions, VideoOptions, VideoRequestFor, VideoRequestInput, VideoResolution, VideoRoute, } from "./video.js";
15
+ export { Video } from "./video.js";
16
+ export { SpeechClient } from "./speech-client.js";
17
+ export { SpeechEvent, SpeechModel, SpeechModelSchema, SpeechRequest, SpeechResponse, SpeechTimestamp, SpeechVoice, } from "./speech.js";
18
+ export type { SpeechFormat, SpeechModelOptions, SpeechOptions, SpeechRequestFor, SpeechRequestInput, SpeechRoute, } from "./speech.js";
19
+ export { Speech } from "./speech.js";
20
+ export { TranscriptionClient } from "./transcription-client.js";
21
+ export { TranscriptionEvent, TranscriptionModel, TranscriptionModelSchema, TranscriptionRequest, TranscriptionResponse, TranscriptionSegment, TranscriptionTimestamps, TranscriptionWord, } from "./transcription.js";
22
+ export type { TranscriptionModelOptions, TranscriptionOptions, TranscriptionRequestFor, TranscriptionRequestInput, TranscriptionRoute, } from "./transcription.js";
23
+ export { Transcription } from "./transcription.js";
24
+ export { Media } from "./media.js";
25
+ export { Generation } from "./generation.js";
26
+ export type { AwaitOptions as GenerationAwaitOptions, Event as GenerationEvent, Poll, Route as GenerationRoute, Snapshot as GenerationSnapshot, Status as GenerationStatus, } from "./generation.js";
12
27
  export { Tool, ToolFailure, toDefinitions } from "./tool.js";
13
28
  export { ToolRuntime } from "./tool-runtime.js";
14
29
  export type { DispatchResult as ToolDispatchResult, ToolSettlement } from "./tool-runtime.js";
package/dist/index.js CHANGED
@@ -5,8 +5,19 @@ export { Provider } from "./provider.js";
5
5
  export { ProviderPackage } from "./provider-package.js";
6
6
  export { isContextOverflow, isContextOverflowFailure } from "./provider-error.js";
7
7
  export * from "./schema/index.js";
8
- export { GeneratedImage, ImageInput, ImageInputSchema, ImageModel, ImageRequest, ImageResponse } from "./image.js";
8
+ export { ImageAspectRatio, ImageEvent, ImageModel, ImageModelSchema, ImageRequest, ImageResponse, ImageSize, } from "./image.js";
9
9
  export { Image } from "./image.js";
10
+ export { VideoClient } from "./video-client.js";
11
+ export { VideoAspectRatio, VideoEvent, VideoFrames, VideoModel, VideoModelSchema, VideoRequest, VideoResponse, } from "./video.js";
12
+ export { Video } from "./video.js";
13
+ export { SpeechClient } from "./speech-client.js";
14
+ export { SpeechEvent, SpeechModel, SpeechModelSchema, SpeechRequest, SpeechResponse, SpeechTimestamp, SpeechVoice, } from "./speech.js";
15
+ export { Speech } from "./speech.js";
16
+ export { TranscriptionClient } from "./transcription-client.js";
17
+ export { TranscriptionEvent, TranscriptionModel, TranscriptionModelSchema, TranscriptionRequest, TranscriptionResponse, TranscriptionSegment, TranscriptionTimestamps, TranscriptionWord, } from "./transcription.js";
18
+ export { Transcription } from "./transcription.js";
19
+ export { Media } from "./media.js";
20
+ export { Generation } from "./generation.js";
10
21
  export { Tool, ToolFailure, toDefinitions } from "./tool.js";
11
22
  export { ToolRuntime } from "./tool-runtime.js";
12
23
  export * as LLM from "./llm.js";
package/dist/llm.d.ts CHANGED
@@ -191,6 +191,14 @@ export declare class GenerateObjectResponse<T> {
191
191
  };
192
192
  } | undefined;
193
193
  readonly namespace?: string | undefined;
194
+ } | {
195
+ readonly type: "media";
196
+ readonly media: import("./media.js").Asset;
197
+ readonly providerMetadata?: {
198
+ readonly [x: string]: {
199
+ readonly [x: string]: unknown;
200
+ };
201
+ } | undefined;
194
202
  } | {
195
203
  readonly type: "step-finish";
196
204
  readonly reason: {
@@ -219,12 +227,12 @@ export declare class GenerateObjectResponse<T> {
219
227
  } | {
220
228
  readonly type: "provider-error";
221
229
  readonly message: string;
230
+ readonly classification?: "context-overflow" | "payload-too-large" | undefined;
222
231
  readonly providerMetadata?: {
223
232
  readonly [x: string]: {
224
233
  readonly [x: string]: unknown;
225
234
  };
226
235
  } | undefined;
227
- readonly classification?: "context-overflow" | "payload-too-large" | undefined;
228
236
  })[];
229
237
  get usage(): import("./schema/events.js").Usage | undefined;
230
238
  }
@@ -0,0 +1,44 @@
1
+ import { Effect } from "effect";
2
+ import { Endpoint } from "./route/endpoint.js";
3
+ import { MediaRoute } from "./route/media.js";
4
+ import type { MediaProtocol } from "./route/media-protocol.js";
5
+ import { AIError, HttpOptions, ModelID, ProviderID } from "./schema/index.js";
6
+ /**
7
+ * What every media model carries: ids, the configured route, and deployment `http` overlays. Modality classes
8
+ * (`ImageModel`, `VideoModel`, `SpeechModel`) extend it with their route type and a nominal marker so one cannot stand
9
+ * in for the other in requests.
10
+ */
11
+ export declare class MediaModel<Route, Options> {
12
+ protected readonly _Options: (options: Options) => Options;
13
+ readonly id: ModelID;
14
+ readonly provider: ProviderID;
15
+ readonly route: Route;
16
+ readonly http?: HttpOptions;
17
+ constructor(input: MediaModel.Input<Route>);
18
+ }
19
+ export declare namespace MediaModel {
20
+ interface Input<Route> {
21
+ readonly id: string | ModelID;
22
+ readonly provider: string | ProviderID;
23
+ readonly route: Route;
24
+ readonly http?: HttpOptions;
25
+ }
26
+ /** A protocol plus its canonical start path; `ModelInput.baseURL` overrides `baseURL` per deployment. */
27
+ interface RouteInput<Request extends MediaRoute.MediaRequest, Protocol> {
28
+ readonly id: string;
29
+ readonly provider: string | ProviderID;
30
+ readonly protocol: Protocol;
31
+ readonly path: Endpoint.EndpointPart<MediaProtocol.Body, Request>;
32
+ readonly baseURL?: string;
33
+ /** Headers the protocol requires on every call, such as a pinned API version; deployment headers win. */
34
+ readonly headers?: Record<string, string>;
35
+ }
36
+ type AnyRouteInput<Request extends MediaRoute.MediaRequest, Event, Response, Frame, State, Token> = RouteInput<Request, MediaProtocol.Inline<Request, Response>> | RouteInput<MediaProtocol.Addressed<Request>, MediaProtocol.Streamed<Request, Event, Frame, State>> | RouteInput<Request, MediaProtocol.Queued<Request, Response, Token>>;
37
+ }
38
+ /** Compose a protocol route input with one deployment through `MediaRoute.inline`, `queued`, or `stream`. */
39
+ export declare const composeRoute: <Request extends MediaRoute.MediaRequest, Protocol, Route>(compose: (input: MediaRoute.Composition<Request> & {
40
+ readonly protocol: Protocol;
41
+ }) => Route, route: MediaModel.RouteInput<Request, Protocol>, input: MediaRoute.ModelInput) => Route;
42
+ export declare const composeAnyRoute: <Request extends MediaRoute.MediaRequest, Event, Response, Frame, State, Token>(route: MediaModel.AnyRouteInput<Request, Event, Response, Frame, State, Token>, input: MediaRoute.ModelInput, collect: (events: ReadonlyArray<Event>) => Effect.Effect<Response, AIError>) => MediaRoute.AnyRoute<Request, Event, Response>;
43
+ /** Lift a synchronous Schema-class constructor into a typed `InvalidRequest` failure. */
44
+ export declare const tryRequest: <A>(make: () => A) => Effect.Effect<A, AIError>;
@@ -0,0 +1,49 @@
1
+ import { Effect } from "effect";
2
+ import { Endpoint } from "./route/endpoint.js";
3
+ import { MediaRoute } from "./route/media.js";
4
+ import { AIError, HttpOptions, InvalidRequestError, ModelID, ProviderID } from "./schema/index.js";
5
+ /**
6
+ * What every media model carries: ids, the configured route, and deployment `http` overlays. Modality classes
7
+ * (`ImageModel`, `VideoModel`, `SpeechModel`) extend it with their route type and a nominal marker so one cannot stand
8
+ * in for the other in requests.
9
+ */
10
+ export class MediaModel {
11
+ id;
12
+ provider;
13
+ route;
14
+ http;
15
+ constructor(input) {
16
+ this.id = ModelID.make(input.id);
17
+ this.provider = ProviderID.make(input.provider);
18
+ this.route = input.route;
19
+ this.http = input.http;
20
+ }
21
+ }
22
+ /** Compose a protocol route input with one deployment through `MediaRoute.inline`, `queued`, or `stream`. */
23
+ export const composeRoute = (compose, route, input) => compose({
24
+ id: route.id,
25
+ provider: route.provider,
26
+ protocol: route.protocol,
27
+ endpoint: Endpoint.path(route.path, { baseURL: input.baseURL ?? route.baseURL }),
28
+ auth: input.auth,
29
+ headers: route.headers === undefined && input.headers === undefined ? undefined : { ...route.headers, ...input.headers },
30
+ });
31
+ export const composeAnyRoute = (route, input, collect) => {
32
+ if (isStreamInput(route))
33
+ return composeRoute((composition) => MediaRoute.stream({ ...composition, collect }), route, input);
34
+ if (isQueuedInput(route))
35
+ return composeRoute(MediaRoute.queued, route, input);
36
+ return composeRoute(MediaRoute.inline, route, input);
37
+ };
38
+ const isStreamInput = (route) => route.protocol.kind === "stream";
39
+ const isQueuedInput = (route) => route.protocol.kind === "queued";
40
+ /** Lift a synchronous Schema-class constructor into a typed `InvalidRequest` failure. */
41
+ export const tryRequest = (make) => Effect.try({
42
+ try: make,
43
+ catch: (error) => new AIError({
44
+ reason: new InvalidRequestError({
45
+ message: error instanceof Error ? error.message : String(error),
46
+ cause: error,
47
+ }),
48
+ }),
49
+ });
@@ -0,0 +1,213 @@
1
+ export * as Media from "./media.js";
2
+ import { Effect, FileSystem, Schema } 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 { 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
+ /** A provider-side partial result such as stripped audio or a moderated sample; never a silent drop. */
46
+ export declare const Notice: Schema.Struct<{
47
+ readonly type: Schema.Literals<readonly ["moderated", "filtered", "other"]>;
48
+ readonly message: Schema.String;
49
+ readonly providerMetadata: Schema.optional<Schema.$Record<Schema.String, Schema.$Record<Schema.String, Schema.Unknown>>>;
50
+ }>;
51
+ export type Notice = Schema.Schema.Type<typeof Notice>;
52
+ /** Synchronous view of an inline payload; `undefined` for `url` and `ref` sources, which carry no local bytes. */
53
+ export interface Inline {
54
+ readonly mime: string;
55
+ readonly base64: string;
56
+ readonly dataUrl: string;
57
+ }
58
+ export declare class Asset {
59
+ #private;
60
+ readonly source: Source;
61
+ /** Derived from the source: declared type, sniffed magic bytes, then `application/octet-stream`. */
62
+ readonly mediaType: string;
63
+ readonly kind: Kind;
64
+ readonly info?: Info;
65
+ /** Epoch milliseconds after which a `url` source stops resolving. */
66
+ readonly expiresAt?: number;
67
+ readonly providerMetadata?: ProviderMetadata;
68
+ /** Transient download credentials for `url` sources; see `Asset.Input.headers`. */
69
+ readonly headers?: Record<string, string>;
70
+ constructor(input: Asset.Input);
71
+ /** Inline payload without effects, for protocols that embed base64 or data URLs directly. */
72
+ inline(): Inline | undefined;
73
+ /** Decoded payload; downloads `url` sources through the request executor and caches the result. */
74
+ bytes(): Effect.Effect<Uint8Array, AIError, Service>;
75
+ base64(): Effect.Effect<string, AIError, Service>;
76
+ dataUrl(): Effect.Effect<string, AIError, Service>;
77
+ /**
78
+ * The `AssetEncoded` JSON form with `bytes` sources as base64, matching `Schema.toCodecJson(AssetSchema)`, so a
79
+ * plain `JSON.stringify` of messages or events stays lossless and decodes back through the JSON codec.
80
+ */
81
+ toJSON(): {
82
+ source: {
83
+ readonly type: "base64";
84
+ readonly data: string;
85
+ readonly mediaType: string;
86
+ } | {
87
+ readonly type: "url";
88
+ readonly url: string;
89
+ readonly mediaType?: string | undefined;
90
+ readonly expiresAt?: number | undefined;
91
+ } | {
92
+ readonly id: string;
93
+ readonly type: "ref";
94
+ readonly provider: string & import("effect/Brand").Brand<"AI.ProviderID">;
95
+ readonly mediaType?: string | undefined;
96
+ } | {
97
+ data: string;
98
+ type: "bytes";
99
+ mediaType: string;
100
+ };
101
+ info: {
102
+ readonly format?: string | undefined;
103
+ readonly width?: number | undefined;
104
+ readonly height?: number | undefined;
105
+ readonly durationSeconds?: number | undefined;
106
+ readonly sampleRate?: number | undefined;
107
+ readonly channels?: number | undefined;
108
+ readonly encoding?: string | undefined;
109
+ } | undefined;
110
+ providerMetadata: {
111
+ readonly [x: string]: {
112
+ readonly [x: string]: unknown;
113
+ };
114
+ } | undefined;
115
+ };
116
+ /** Pull `url` sources into owned bytes before the URL expires. Inline sources return themselves. */
117
+ materialize(): Effect.Effect<Asset, AIError, Service>;
118
+ }
119
+ export declare namespace Asset {
120
+ interface Input {
121
+ readonly source: Source;
122
+ readonly info?: Info;
123
+ readonly providerMetadata?: ProviderMetadata;
124
+ /**
125
+ * Headers required to download a `url` source, such as the provider API key Veo demands for its file URIs.
126
+ * They are runtime-only: never part of `source`, `toJSON()`, or `AssetSchema`, so a persisted asset cannot leak
127
+ * credentials and cannot be downloaded again after a round-trip. Call `materialize()` before persisting.
128
+ */
129
+ readonly headers?: Record<string, string>;
130
+ }
131
+ }
132
+ /** JSON form of an asset: the serializable `Source` plus caller-supplied metadata. `bytes` sources encode as base64. */
133
+ export declare const AssetEncoded: Schema.Struct<{
134
+ readonly source: Schema.Union<readonly [Schema.Struct<{
135
+ readonly type: Schema.Literal<"bytes">;
136
+ readonly data: Schema.Uint8Array;
137
+ readonly mediaType: Schema.String;
138
+ }>, Schema.Struct<{
139
+ readonly type: Schema.Literal<"base64">;
140
+ readonly data: Schema.String;
141
+ readonly mediaType: Schema.String;
142
+ }>, Schema.Struct<{
143
+ readonly type: Schema.Literal<"url">;
144
+ readonly url: Schema.String;
145
+ readonly mediaType: Schema.optional<Schema.String>;
146
+ /** Epoch milliseconds after which the provider no longer serves the URL. */
147
+ readonly expiresAt: Schema.optional<Schema.Number>;
148
+ }>, Schema.Struct<{
149
+ readonly type: Schema.Literal<"ref">;
150
+ readonly provider: Schema.brand<Schema.String, "AI.ProviderID">;
151
+ readonly id: Schema.String;
152
+ readonly mediaType: Schema.optional<Schema.String>;
153
+ }>]>;
154
+ readonly info: Schema.optional<Schema.Struct<{
155
+ readonly width: Schema.optional<Schema.Number>;
156
+ readonly height: Schema.optional<Schema.Number>;
157
+ readonly durationSeconds: Schema.optional<Schema.Number>;
158
+ readonly sampleRate: Schema.optional<Schema.Number>;
159
+ readonly channels: Schema.optional<Schema.Number>;
160
+ readonly encoding: Schema.optional<Schema.String>;
161
+ readonly format: Schema.optional<Schema.String>;
162
+ }>>;
163
+ readonly providerMetadata: Schema.optional<Schema.$Record<Schema.String, Schema.$Record<Schema.String, Schema.Unknown>>>;
164
+ }>;
165
+ /** `Asset` in the type domain and `AssetEncoded` on the wire, so messages and events holding assets serialize. */
166
+ export declare const AssetSchema: Schema.decodeTo<Schema.declare<Asset, Asset>, Schema.Struct<{
167
+ readonly source: Schema.Union<readonly [Schema.Struct<{
168
+ readonly type: Schema.Literal<"bytes">;
169
+ readonly data: Schema.Uint8Array;
170
+ readonly mediaType: Schema.String;
171
+ }>, Schema.Struct<{
172
+ readonly type: Schema.Literal<"base64">;
173
+ readonly data: Schema.String;
174
+ readonly mediaType: Schema.String;
175
+ }>, Schema.Struct<{
176
+ readonly type: Schema.Literal<"url">;
177
+ readonly url: Schema.String;
178
+ readonly mediaType: Schema.optional<Schema.String>;
179
+ /** Epoch milliseconds after which the provider no longer serves the URL. */
180
+ readonly expiresAt: Schema.optional<Schema.Number>;
181
+ }>, Schema.Struct<{
182
+ readonly type: Schema.Literal<"ref">;
183
+ readonly provider: Schema.brand<Schema.String, "AI.ProviderID">;
184
+ readonly id: Schema.String;
185
+ readonly mediaType: Schema.optional<Schema.String>;
186
+ }>]>;
187
+ readonly info: Schema.optional<Schema.Struct<{
188
+ readonly width: Schema.optional<Schema.Number>;
189
+ readonly height: Schema.optional<Schema.Number>;
190
+ readonly durationSeconds: Schema.optional<Schema.Number>;
191
+ readonly sampleRate: Schema.optional<Schema.Number>;
192
+ readonly channels: Schema.optional<Schema.Number>;
193
+ readonly encoding: Schema.optional<Schema.String>;
194
+ readonly format: Schema.optional<Schema.String>;
195
+ }>>;
196
+ readonly providerMetadata: Schema.optional<Schema.$Record<Schema.String, Schema.$Record<Schema.String, Schema.Unknown>>>;
197
+ }>, never, never>;
198
+ export type AssetOptions = Omit<Asset.Input, "source">;
199
+ export declare const from: (source: Source, options?: AssetOptions) => Asset;
200
+ export declare const bytes: (data: Uint8Array, mediaType?: string, options?: AssetOptions) => Asset;
201
+ export declare const base64: (data: string, mediaType: string, options?: AssetOptions) => Asset;
202
+ export declare const url: (value: string, options?: AssetOptions & Omit<Extract<Source, {
203
+ readonly type: "url";
204
+ }>, "type" | "url">) => Asset;
205
+ export declare const ref: (provider: string | ProviderID, id: string, mediaType?: string, options?: AssetOptions) => Asset;
206
+ /** Parse a `data:<mime>;base64,<data>` URL, or `undefined` when the value is not a base64 data URL. */
207
+ export declare const parseDataUrl: (value: string, options?: AssetOptions) => Asset | undefined;
208
+ /** Parse a `data:<mime>;base64,<data>` URL. Malformed input throws a typed `AIError` because constructors are sync. */
209
+ export declare const fromDataUrl: (dataUrl: string, options?: AssetOptions) => Asset;
210
+ /** Read a file through `FileSystem` and sniff its media type from magic bytes, then the extension. */
211
+ export declare const file: (path: string, options?: AssetOptions) => Effect.Effect<Asset, AIError, FileSystem.FileSystem>;
212
+ /** Materialize an asset and write its bytes through `FileSystem`. */
213
+ export declare const write: (asset: Asset, path: string) => Effect.Effect<void, AIError, FileSystem.FileSystem | Service>;