@opencode/ai 0.0.0-dev-1297 → 0.0.0-dev-1299

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 (43) hide show
  1. package/README.md +27 -3
  2. package/dist/generation.d.ts +10 -10
  3. package/dist/generation.js +8 -14
  4. package/dist/image-client.d.ts +5 -1
  5. package/dist/image-client.js +1 -1
  6. package/dist/image.d.ts +10 -89
  7. package/dist/image.js +2 -9
  8. package/dist/index.d.ts +13 -18
  9. package/dist/index.js +17 -15
  10. package/dist/media-client.d.ts +5 -1
  11. package/dist/media.d.ts +9 -1
  12. package/dist/media.js +14 -7
  13. package/dist/protocols/assemblyai-transcription.js +7 -6
  14. package/dist/protocols/cartesia-speech.d.ts +0 -8
  15. package/dist/protocols/deepgram-speech.d.ts +0 -8
  16. package/dist/protocols/elevenlabs-speech.d.ts +0 -8
  17. package/dist/protocols/google-interactions-video.js +3 -3
  18. package/dist/protocols/google-speech.d.ts +0 -8
  19. package/dist/protocols/google-transcription.d.ts +0 -8
  20. package/dist/protocols/minimax-video.js +11 -11
  21. package/dist/protocols/openai-images.d.ts +0 -8
  22. package/dist/protocols/openai-speech.d.ts +0 -8
  23. package/dist/protocols/openai-transcription.d.ts +0 -8
  24. package/dist/protocols/openrouter-video.js +7 -7
  25. package/dist/protocols/seedance-video.js +5 -5
  26. package/dist/protocols/xai-video.js +6 -3
  27. package/dist/public.d.ts +2 -1
  28. package/dist/route/executor-service.d.ts +14 -1
  29. package/dist/route/executor-service.js +28 -1
  30. package/dist/route/executor.d.ts +5 -2
  31. package/dist/route/executor.js +20 -5
  32. package/dist/route/media-protocol.d.ts +12 -7
  33. package/dist/route/media-protocol.js +11 -4
  34. package/dist/route/media.js +12 -3
  35. package/dist/speech-client.d.ts +5 -5
  36. package/dist/speech.d.ts +7 -88
  37. package/dist/speech.js +1 -3
  38. package/dist/transcription-client.d.ts +5 -5
  39. package/dist/transcription.d.ts +7 -88
  40. package/dist/transcription.js +1 -3
  41. package/dist/video-client.js +1 -1
  42. package/dist/video.d.ts +3 -1
  43. package/package.json +3 -3
package/README.md CHANGED
@@ -249,6 +249,29 @@ const logged = RequestExecutor.middleware((request, next) =>
249
249
  const everything = AIClient.layerWith(logged) // or AI.make({ layer: logged })
250
250
  ```
251
251
 
252
+ To hook only some calls, such as adding a per-user header, scope middleware to them. It runs on every provider request
253
+ they make, whatever the modality: submits, polls, results and cancels. Executor-wide middleware wraps it.
254
+
255
+ ```ts
256
+ const signed: HttpMiddleware = (request, next) => next(HttpClientRequest.setHeader(request, "x-user", "user-1"))
257
+
258
+ // Effect: an Effect or a Stream
259
+ const video =
260
+ yield *
261
+ Effect.gen(function* () {
262
+ const job = yield* ai.video.start({ model, prompt: "A red balloon floats up" })
263
+ return yield* ai.video.result(model, job.id)
264
+ }).pipe(RequestExecutor.withMiddleware(signed))
265
+
266
+ // Promise: `http` on any call's options; a handle it returns keeps it
267
+ await AI.ai.video.start({ model, prompt: "A red balloon floats up" }, { http: signed })
268
+ ```
269
+
270
+ Downloading a `url` asset never uses the caller's scoped middleware, so hooks that add credentials can't reach another
271
+ host such as a CDN or a presigned storage URL. A result URL on the provider's own origin carries the middleware of the
272
+ call that returned it (`asset.middleware`), so it downloads the same way later. Set `middleware` yourself with
273
+ `Media.url(url, { middleware })`.
274
+
252
275
  ## Venice AI
253
276
 
254
277
  `Venice` provides native Chat Completions with streaming tools and reasoning. `model` and `chat`
@@ -1077,7 +1100,8 @@ const program = Effect.gen(function* () {
1077
1100
  job.id // the provider's job ID: store it with the provider and model ID
1078
1101
 
1079
1102
  const status = yield* ai.video.status(model, job.id)
1080
- status.status // "queued" | "running" | "completed" | "failed" | "cancelled" | "expired"
1103
+ status.status // "running" | "completed" | "error"
1104
+ status.reason // "failed" | "cancelled" | "expired", only when status is "error"
1081
1105
  status.progress // 0..1, only when the provider reports it
1082
1106
 
1083
1107
  const response = yield* ai.video.result(model, job.id)
@@ -1328,8 +1352,8 @@ const program = Effect.gen(function* () {
1328
1352
  })
1329
1353
  ```
1330
1354
 
1331
- Inline routes emit only `finish` from `stream` (no faked deltas); queued routes emit `generation-queued` /
1332
- `generation-progress` before it.
1355
+ Inline routes emit only `finish` from `stream` (no faked deltas); queued routes emit `generation-progress`
1356
+ before it.
1333
1357
 
1334
1358
  Provider notes:
1335
1359
 
@@ -1,14 +1,18 @@
1
1
  import { Duration, Effect, Schema, Stream } from "effect";
2
2
  import { AIError } from "./schema/errors.js";
3
- export declare const Status: Schema.Literals<readonly ["queued", "running", "completed", "failed", "cancelled", "expired"]>;
3
+ export declare const Status: Schema.Literals<readonly ["running", "completed", "error"]>;
4
4
  export type Status = Schema.Schema.Type<typeof Status>;
5
+ /** Why a generation ended in `error`. */
6
+ export declare const Reason: Schema.Literals<readonly ["failed", "cancelled", "expired"]>;
7
+ export type Reason = Schema.Schema.Type<typeof Reason>;
5
8
  /** Provider-neutral view of one generation observation. */
6
9
  export interface Snapshot {
7
10
  readonly id: string;
8
11
  readonly status: Status;
12
+ /** Set when `status` is `error`. */
13
+ readonly reason?: Reason;
9
14
  /** Normalized 0..1 when the provider reports progress. */
10
15
  readonly progress?: number;
11
- readonly position?: number;
12
16
  }
13
17
  /**
14
18
  * Route-owned generation operations for one generation. The media route decodes its serializable token once (from the
@@ -26,31 +30,27 @@ export interface Poll {
26
30
  export interface AwaitOptions {
27
31
  readonly poll?: Poll;
28
32
  }
29
- export declare const QueuedEvent: Schema.Struct<{
30
- readonly type: Schema.tag<"generation-queued">;
31
- readonly id: Schema.String;
32
- readonly position: Schema.optional<Schema.Number>;
33
- }>;
34
33
  export declare const ProgressEvent: Schema.Struct<{
35
34
  readonly type: Schema.tag<"generation-progress">;
36
35
  readonly id: Schema.String;
37
36
  readonly progress: Schema.optional<Schema.Number>;
38
37
  }>;
39
- export type Observation = Schema.Schema.Type<typeof QueuedEvent> | Schema.Schema.Type<typeof ProgressEvent>;
38
+ export type Observation = Schema.Schema.Type<typeof ProgressEvent>;
40
39
  export type Event = Observation | {
41
40
  readonly type: "generation-finished";
42
41
  readonly id: string;
43
42
  readonly status: Status;
43
+ readonly reason?: Reason;
44
44
  };
45
- export declare const isTerminal: (status: Status) => boolean;
45
+ export declare const isTerminal: (status: Status) => status is "completed" | "error";
46
46
  export declare class Generation<Response> {
47
47
  readonly route: Route<Response>;
48
48
  /** Route-owned serializable JSON; pass it to the modality's `resume` from another process. */
49
49
  readonly token: unknown;
50
50
  readonly id: string;
51
51
  readonly status: Status;
52
+ readonly reason?: Reason;
52
53
  readonly progress?: number;
53
- readonly position?: number;
54
54
  constructor(route: Route<Response>,
55
55
  /** Route-owned serializable JSON; pass it to the modality's `resume` from another process. */
56
56
  token: unknown, snapshot: Snapshot);
@@ -1,27 +1,23 @@
1
1
  import { Clock, Duration, Effect, Schedule, Schema, Stream } from "effect";
2
2
  import { AIError, TimeoutError } from "./schema/errors.js";
3
- export const Status = Schema.Literals(["queued", "running", "completed", "failed", "cancelled", "expired"]);
3
+ export const Status = Schema.Literals(["running", "completed", "error"]);
4
+ /** Why a generation ended in `error`. */
5
+ export const Reason = Schema.Literals(["failed", "cancelled", "expired"]);
4
6
  const DEFAULT_POLL_INTERVAL = Duration.seconds(5);
5
7
  const DEFAULT_POLL_TIMEOUT = Duration.minutes(10);
6
- export const QueuedEvent = Schema.Struct({
7
- type: Schema.tag("generation-queued"),
8
- id: Schema.String,
9
- position: Schema.optional(Schema.Number),
10
- }).annotate({ identifier: "Generation.Event.Queued" });
11
8
  export const ProgressEvent = Schema.Struct({
12
9
  type: Schema.tag("generation-progress"),
13
10
  id: Schema.String,
14
11
  progress: Schema.optional(Schema.Number),
15
12
  }).annotate({ identifier: "Generation.Event.Progress" });
16
- const TERMINAL = new Set(["completed", "failed", "cancelled", "expired"]);
17
- export const isTerminal = (status) => TERMINAL.has(status);
13
+ export const isTerminal = (status) => status !== "running";
18
14
  export class Generation {
19
15
  route;
20
16
  token;
21
17
  id;
22
18
  status;
19
+ reason;
23
20
  progress;
24
- position;
25
21
  constructor(route,
26
22
  /** Route-owned serializable JSON; pass it to the modality's `resume` from another process. */
27
23
  token, snapshot) {
@@ -29,15 +25,15 @@ export class Generation {
29
25
  this.token = token;
30
26
  this.id = snapshot.id;
31
27
  this.status = snapshot.status;
28
+ this.reason = snapshot.reason;
32
29
  this.progress = snapshot.progress;
33
- this.position = snapshot.position;
34
30
  }
35
31
  get snapshot() {
36
32
  return {
37
33
  id: this.id,
38
34
  status: this.status,
35
+ reason: this.reason,
39
36
  progress: this.progress,
40
- position: this.position,
41
37
  };
42
38
  }
43
39
  get terminal() {
@@ -80,9 +76,7 @@ export class Generation {
80
76
  }
81
77
  event() {
82
78
  if (this.terminal)
83
- return { type: "generation-finished", id: this.id, status: this.status };
84
- if (this.status === "queued")
85
- return { type: "generation-queued", id: this.id, position: this.position };
79
+ return { type: "generation-finished", id: this.id, status: this.status, reason: this.reason };
86
80
  return { type: "generation-progress", id: this.id, progress: this.progress };
87
81
  }
88
82
  poll(poll) {
@@ -63,6 +63,10 @@ export declare const ImageClient: {
63
63
  readonly type: "image";
64
64
  readonly index: number;
65
65
  readonly image: import("./media.js").Asset;
66
+ } | {
67
+ readonly type: "generation-progress";
68
+ readonly id: string;
69
+ readonly progress?: number | undefined;
66
70
  } | {
67
71
  readonly type: "finish";
68
72
  readonly usage?: {
@@ -91,7 +95,7 @@ export declare const ImageClient: {
91
95
  readonly [x: string]: unknown;
92
96
  };
93
97
  } | undefined;
94
- } | import("./generation.js").Observation, AIError, ImageClientService>;
98
+ }, AIError, ImageClientService>;
95
99
  readonly start: (request: ImageRequest) => Effect.Effect<ImageJob, AIError, ImageClientService>;
96
100
  readonly status: (model: ImageModel, id: string) => Effect.Effect<ImageStatus, AIError, ImageClientService>;
97
101
  readonly result: (model: ImageModel, id: string) => Effect.Effect<ImageResponse, AIError, ImageClientService>;
@@ -28,7 +28,7 @@ export const layer = Layer.effect(Service, Effect.gen(function* () {
28
28
  start: (request) => media.start(request).pipe(
29
29
  // The token, not the snapshot id, is what `status` and `result` decode; `ImageModel.fromRoute` makes it a string.
30
30
  Effect.flatMap((generation) => decodeID(generation.token)), Effect.map((id) => ({ id }))),
31
- status: (model, id) => attach(model, id, "status").pipe(Effect.flatMap((route) => route.status), Effect.map((snapshot) => ({ status: snapshot.status, progress: snapshot.progress }))),
31
+ status: (model, id) => attach(model, id, "status").pipe(Effect.flatMap((route) => route.status), Effect.map((snapshot) => ({ status: snapshot.status, reason: snapshot.reason, progress: snapshot.progress }))),
32
32
  result: (model, id) => attach(model, id, "result").pipe(Effect.flatMap((route) => route.result)),
33
33
  cancel: (model, id) => attach(model, id, "cancel").pipe(Effect.flatMap((route) => route.cancel ??
34
34
  Effect.fail(new AIError({
package/dist/image.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Effect, Schema, Stream } from "effect";
2
- import { type AwaitOptions, type Status } from "./generation.js";
2
+ import { type AwaitOptions, type Reason, type Status } from "./generation.js";
3
3
  import { Media } from "./media.js";
4
4
  import { MediaModel } from "./media-model.js";
5
5
  import { MediaRoute } from "./route/media.js";
@@ -49,6 +49,8 @@ export interface ImageJob {
49
49
  }
50
50
  export interface ImageStatus {
51
51
  readonly status: Status;
52
+ /** Set when `status` is `error`. */
53
+ readonly reason?: Reason;
52
54
  /** 0..1, only when the provider reports progress. */
53
55
  readonly progress?: number;
54
56
  }
@@ -202,10 +204,6 @@ export declare const ImageFinishEvent: Schema.Struct<{
202
204
  readonly providerMetadata: Schema.optional<Schema.$Record<Schema.String, Schema.$Record<Schema.String, Schema.Unknown>>>;
203
205
  }>;
204
206
  declare const imageEventTagged: Schema.toTaggedUnion<"type", readonly [Schema.Struct<{
205
- readonly type: Schema.tag<"generation-queued">;
206
- readonly id: Schema.String;
207
- readonly position: Schema.optional<Schema.Number>;
208
- }>, Schema.Struct<{
209
207
  readonly type: Schema.tag<"generation-progress">;
210
208
  readonly id: Schema.String;
211
209
  readonly progress: Schema.optional<Schema.Number>;
@@ -301,10 +299,6 @@ declare const imageEventTagged: Schema.toTaggedUnion<"type", readonly [Schema.St
301
299
  readonly providerMetadata: Schema.optional<Schema.$Record<Schema.String, Schema.$Record<Schema.String, Schema.Unknown>>>;
302
300
  }>]>;
303
301
  export declare const ImageEvent: Schema.Union<readonly [Schema.Struct<{
304
- readonly type: Schema.tag<"generation-queued">;
305
- readonly id: Schema.String;
306
- readonly position: Schema.optional<Schema.Number>;
307
- }>, Schema.Struct<{
308
302
  readonly type: Schema.tag<"generation-progress">;
309
303
  readonly id: Schema.String;
310
304
  readonly progress: Schema.optional<Schema.Number>;
@@ -400,7 +394,7 @@ export declare const ImageEvent: Schema.Union<readonly [Schema.Struct<{
400
394
  readonly providerMetadata: Schema.optional<Schema.$Record<Schema.String, Schema.$Record<Schema.String, Schema.Unknown>>>;
401
395
  }>]> & {
402
396
  readonly tag: "type";
403
- readonly discriminants: readonly ["generation-queued", "generation-progress", "image-partial", "image", "finish"];
397
+ readonly discriminants: readonly ["generation-progress", "image-partial", "image", "finish"];
404
398
  readonly cases: {
405
399
  finish: Schema.Struct<{
406
400
  readonly type: Schema.tag<"finish">;
@@ -430,11 +424,6 @@ export declare const ImageEvent: Schema.Union<readonly [Schema.Struct<{
430
424
  readonly id: Schema.String;
431
425
  readonly progress: Schema.optional<Schema.Number>;
432
426
  }>;
433
- "generation-queued": Schema.Struct<{
434
- readonly type: Schema.tag<"generation-queued">;
435
- readonly id: Schema.String;
436
- readonly position: Schema.optional<Schema.Number>;
437
- }>;
438
427
  image: Schema.Struct<{
439
428
  readonly type: Schema.tag<"image">;
440
429
  readonly index: Schema.Number;
@@ -514,10 +503,6 @@ export declare const ImageEvent: Schema.Union<readonly [Schema.Struct<{
514
503
  readonly type: "image";
515
504
  readonly index: number;
516
505
  readonly image: Media.Asset;
517
- } | {
518
- readonly type: "generation-queued";
519
- readonly id: string;
520
- readonly position?: number | undefined;
521
506
  } | {
522
507
  readonly type: "generation-progress";
523
508
  readonly id: string;
@@ -562,12 +547,6 @@ export declare const ImageEvent: Schema.Union<readonly [Schema.Struct<{
562
547
  readonly image: Media.Asset;
563
548
  }, {
564
549
  readonly type: Keys;
565
- }> | Extract<{
566
- readonly type: "generation-queued";
567
- readonly id: string;
568
- readonly position?: number | undefined;
569
- }, {
570
- readonly type: Keys;
571
550
  }> | Extract<{
572
551
  readonly type: "generation-progress";
573
552
  readonly id: string;
@@ -640,11 +619,6 @@ export declare const ImageEvent: Schema.Union<readonly [Schema.Struct<{
640
619
  readonly id: string;
641
620
  readonly progress?: number | undefined;
642
621
  };
643
- "generation-queued": (u: unknown) => u is {
644
- readonly type: "generation-queued";
645
- readonly id: string;
646
- readonly position?: number | undefined;
647
- };
648
622
  image: (u: unknown) => u is {
649
623
  readonly type: "image";
650
624
  readonly index: number;
@@ -657,7 +631,7 @@ export declare const ImageEvent: Schema.Union<readonly [Schema.Struct<{
657
631
  };
658
632
  };
659
633
  readonly match: {
660
- <Handlers extends { [K in "finish" | "generation-progress" | "generation-queued" | "image" | "image-partial"]: (value: {
634
+ <Handlers extends { [K in "finish" | "generation-progress" | "image" | "image-partial"]: (value: {
661
635
  readonly finish: Schema.Struct<{
662
636
  readonly type: Schema.tag<"finish">;
663
637
  readonly usage: Schema.optional<Schema.Union<readonly [Schema.Struct<{
@@ -686,11 +660,6 @@ export declare const ImageEvent: Schema.Union<readonly [Schema.Struct<{
686
660
  readonly id: Schema.String;
687
661
  readonly progress: Schema.optional<Schema.Number>;
688
662
  }>;
689
- readonly "generation-queued": Schema.Struct<{
690
- readonly type: Schema.tag<"generation-queued">;
691
- readonly id: Schema.String;
692
- readonly position: Schema.optional<Schema.Number>;
693
- }>;
694
663
  readonly image: Schema.Struct<{
695
664
  readonly type: Schema.tag<"image">;
696
665
  readonly index: Schema.Number;
@@ -769,10 +738,6 @@ export declare const ImageEvent: Schema.Union<readonly [Schema.Struct<{
769
738
  readonly type: "image";
770
739
  readonly index: number;
771
740
  readonly image: Media.Asset;
772
- } | {
773
- readonly type: "generation-queued";
774
- readonly id: string;
775
- readonly position?: number | undefined;
776
741
  } | {
777
742
  readonly type: "generation-progress";
778
743
  readonly id: string;
@@ -805,8 +770,8 @@ export declare const ImageEvent: Schema.Union<readonly [Schema.Struct<{
805
770
  readonly [x: string]: unknown;
806
771
  };
807
772
  } | undefined;
808
- }, cases: Handlers): import("effect/Unify").Unify<ReturnType<Handlers["finish" | "generation-progress" | "generation-queued" | "image" | "image-partial"]>>;
809
- <Handlers extends { [K in "finish" | "generation-progress" | "generation-queued" | "image" | "image-partial"]: (value: {
773
+ }, cases: Handlers): import("effect/Unify").Unify<ReturnType<Handlers["finish" | "generation-progress" | "image" | "image-partial"]>>;
774
+ <Handlers extends { [K in "finish" | "generation-progress" | "image" | "image-partial"]: (value: {
810
775
  readonly finish: Schema.Struct<{
811
776
  readonly type: Schema.tag<"finish">;
812
777
  readonly usage: Schema.optional<Schema.Union<readonly [Schema.Struct<{
@@ -835,11 +800,6 @@ export declare const ImageEvent: Schema.Union<readonly [Schema.Struct<{
835
800
  readonly id: Schema.String;
836
801
  readonly progress: Schema.optional<Schema.Number>;
837
802
  }>;
838
- readonly "generation-queued": Schema.Struct<{
839
- readonly type: Schema.tag<"generation-queued">;
840
- readonly id: Schema.String;
841
- readonly position: Schema.optional<Schema.Number>;
842
- }>;
843
803
  readonly image: Schema.Struct<{
844
804
  readonly type: Schema.tag<"image">;
845
805
  readonly index: Schema.Number;
@@ -918,10 +878,6 @@ export declare const ImageEvent: Schema.Union<readonly [Schema.Struct<{
918
878
  readonly type: "image";
919
879
  readonly index: number;
920
880
  readonly image: Media.Asset;
921
- } | {
922
- readonly type: "generation-queued";
923
- readonly id: string;
924
- readonly position?: number | undefined;
925
881
  } | {
926
882
  readonly type: "generation-progress";
927
883
  readonly id: string;
@@ -954,7 +910,7 @@ export declare const ImageEvent: Schema.Union<readonly [Schema.Struct<{
954
910
  readonly [x: string]: unknown;
955
911
  };
956
912
  } | undefined;
957
- }) => import("effect/Unify").Unify<ReturnType<Handlers["finish" | "generation-progress" | "generation-queued" | "image" | "image-partial"]>>;
913
+ }) => import("effect/Unify").Unify<ReturnType<Handlers["finish" | "generation-progress" | "image" | "image-partial"]>>;
958
914
  };
959
915
  readonly matchOrElse: {
960
916
  <Handlers, OrElse>(value: {
@@ -965,10 +921,6 @@ export declare const ImageEvent: Schema.Union<readonly [Schema.Struct<{
965
921
  readonly type: "image";
966
922
  readonly index: number;
967
923
  readonly image: Media.Asset;
968
- } | {
969
- readonly type: "generation-queued";
970
- readonly id: string;
971
- readonly position?: number | undefined;
972
924
  } | {
973
925
  readonly type: "generation-progress";
974
926
  readonly id: string;
@@ -1036,11 +988,6 @@ export declare const ImageEvent: Schema.Union<readonly [Schema.Struct<{
1036
988
  readonly id: string;
1037
989
  readonly progress?: number | undefined;
1038
990
  }) => any) | undefined;
1039
- readonly "generation-queued"?: ((value: {
1040
- readonly type: "generation-queued";
1041
- readonly id: string;
1042
- readonly position?: number | undefined;
1043
- }) => any) | undefined;
1044
991
  readonly image?: ((value: {
1045
992
  readonly type: "image";
1046
993
  readonly index: number;
@@ -1051,7 +998,7 @@ export declare const ImageEvent: Schema.Union<readonly [Schema.Struct<{
1051
998
  readonly index: number;
1052
999
  readonly image: Media.Asset;
1053
1000
  }) => any) | undefined;
1054
- } & (Exclude<keyof Handlers, "finish" | "generation-progress" | "generation-queued" | "image" | "image-partial"> extends never ? unknown : never), orElse: OrElse & ((value: Exclude<{
1001
+ } & (Exclude<keyof Handlers, "finish" | "generation-progress" | "image" | "image-partial"> extends never ? unknown : never), orElse: OrElse & ((value: Exclude<{
1055
1002
  readonly type: "image-partial";
1056
1003
  readonly index: number;
1057
1004
  readonly image: Media.Asset;
@@ -1063,12 +1010,6 @@ export declare const ImageEvent: Schema.Union<readonly [Schema.Struct<{
1063
1010
  readonly image: Media.Asset;
1064
1011
  }, {
1065
1012
  readonly type: keyof Handlers;
1066
- }> | Exclude<{
1067
- readonly type: "generation-queued";
1068
- readonly id: string;
1069
- readonly position?: number | undefined;
1070
- }, {
1071
- readonly type: keyof Handlers;
1072
1013
  }> | Exclude<{
1073
1014
  readonly type: "generation-progress";
1074
1015
  readonly id: string;
@@ -1141,11 +1082,6 @@ export declare const ImageEvent: Schema.Union<readonly [Schema.Struct<{
1141
1082
  readonly id: string;
1142
1083
  readonly progress?: number | undefined;
1143
1084
  }) => any) | undefined;
1144
- readonly "generation-queued"?: ((value: {
1145
- readonly type: "generation-queued";
1146
- readonly id: string;
1147
- readonly position?: number | undefined;
1148
- }) => any) | undefined;
1149
1085
  readonly image?: ((value: {
1150
1086
  readonly type: "image";
1151
1087
  readonly index: number;
@@ -1156,7 +1092,7 @@ export declare const ImageEvent: Schema.Union<readonly [Schema.Struct<{
1156
1092
  readonly index: number;
1157
1093
  readonly image: Media.Asset;
1158
1094
  }) => any) | undefined;
1159
- } & (Exclude<keyof Handlers, "finish" | "generation-progress" | "generation-queued" | "image" | "image-partial"> extends never ? unknown : never), orElse: OrElse & ((value: Exclude<{
1095
+ } & (Exclude<keyof Handlers, "finish" | "generation-progress" | "image" | "image-partial"> extends never ? unknown : never), orElse: OrElse & ((value: Exclude<{
1160
1096
  readonly type: "image-partial";
1161
1097
  readonly index: number;
1162
1098
  readonly image: Media.Asset;
@@ -1168,12 +1104,6 @@ export declare const ImageEvent: Schema.Union<readonly [Schema.Struct<{
1168
1104
  readonly image: Media.Asset;
1169
1105
  }, {
1170
1106
  readonly type: keyof Handlers;
1171
- }> | Exclude<{
1172
- readonly type: "generation-queued";
1173
- readonly id: string;
1174
- readonly position?: number | undefined;
1175
- }, {
1176
- readonly type: keyof Handlers;
1177
1107
  }> | Exclude<{
1178
1108
  readonly type: "generation-progress";
1179
1109
  readonly id: string;
@@ -1218,10 +1148,6 @@ export declare const ImageEvent: Schema.Union<readonly [Schema.Struct<{
1218
1148
  readonly type: "image";
1219
1149
  readonly index: number;
1220
1150
  readonly image: Media.Asset;
1221
- } | {
1222
- readonly type: "generation-queued";
1223
- readonly id: string;
1224
- readonly position?: number | undefined;
1225
1151
  } | {
1226
1152
  readonly type: "generation-progress";
1227
1153
  readonly id: string;
@@ -1258,11 +1184,6 @@ export declare const ImageEvent: Schema.Union<readonly [Schema.Struct<{
1258
1184
  };
1259
1185
  } & {
1260
1186
  is: {
1261
- generationQueued: (u: unknown) => u is {
1262
- readonly type: "generation-queued";
1263
- readonly id: string;
1264
- readonly position?: number | undefined;
1265
- };
1266
1187
  generationProgress: (u: unknown) => u is {
1267
1188
  readonly type: "generation-progress";
1268
1189
  readonly id: string;
package/dist/image.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Effect, Schema, Stream } from "effect";
2
- import { ProgressEvent, QueuedEvent } from "./generation.js";
2
+ import { ProgressEvent } from "./generation.js";
3
3
  import { Media } from "./media.js";
4
4
  import { MediaModel, composeRoute, tryRequest } from "./media-model.js";
5
5
  import { MediaRoute } from "./route/media.js";
@@ -51,16 +51,9 @@ export const ImageFinishEvent = Schema.Struct({
51
51
  usage: Schema.optional(MediaUsage),
52
52
  providerMetadata: Schema.optional(ProviderMetadata),
53
53
  }).annotate({ identifier: "Image.Event.Finish" });
54
- const imageEventTagged = Schema.Union([
55
- QueuedEvent,
56
- ProgressEvent,
57
- ImagePartialEvent,
58
- ImageOutputEvent,
59
- ImageFinishEvent,
60
- ]).pipe(Schema.toTaggedUnion("type"));
54
+ const imageEventTagged = Schema.Union([ProgressEvent, ImagePartialEvent, ImageOutputEvent, ImageFinishEvent]).pipe(Schema.toTaggedUnion("type"));
61
55
  export const ImageEvent = Object.assign(imageEventTagged, {
62
56
  is: {
63
- generationQueued: imageEventTagged.guards["generation-queued"],
64
57
  generationProgress: imageEventTagged.guards["generation-progress"],
65
58
  imagePartial: imageEventTagged.guards["image-partial"],
66
59
  image: imageEventTagged.guards.image,
package/dist/index.d.ts CHANGED
@@ -7,7 +7,7 @@ import { type ImageModel, type ImageRequest, type ImageRequestInput } from "./im
7
7
  import { LLM } from "./llm.js";
8
8
  import { Media } from "./media.js";
9
9
  import { type CheckpointRequest, type CompactionRequest, type EndpointCompactOptions, type StreamOptions, type TriggerCompactOptions } from "./route/client.js";
10
- import { RequestExecutor } from "./route/executor.js";
10
+ import { RequestExecutor, type HttpMiddleware } from "./route/executor.js";
11
11
  import { LanguageModel, type CompactionCheckpointResponse, type CompactionResponse, type LLMEvent, type LLMResponse } from "./schema/index.js";
12
12
  import type { LoopObjectEvent, ObjectEvent, ObjectResponse, OutputSchema, OutputType, LLMLoopInput, LLMRequestInput } from "./llm.js";
13
13
  import { type SpeechModel, type SpeechRequest, type SpeechRequestInput } from "./speech.js";
@@ -25,7 +25,14 @@ export interface Options {
25
25
  }
26
26
  export interface RunOptions {
27
27
  readonly signal?: AbortSignal;
28
+ /**
29
+ * Runs on every provider request the call makes, like `RequestExecutor.withMiddleware`: submits, polls, results and
30
+ * cancels included. A handle the call returns keeps it for its own calls.
31
+ */
32
+ readonly http?: HttpMiddleware;
28
33
  }
34
+ /** Options for reading or writing an asset. Downloads use the asset's own `middleware`, never a caller's `http`. */
35
+ export type AssetRunOptions = Omit<RunOptions, "http">;
29
36
  export type Services = AIClient.Services;
30
37
  /**
31
38
  * Promise view of a `Generation`. Its fields are a snapshot taken when the handle was created; `refresh()` resolves to a
@@ -47,14 +54,14 @@ export type GenerationHandle<Response> = Snapshot & {
47
54
  export declare const make: (options?: Options) => {
48
55
  run: <A, E>(effect: Effect.Effect<A, E, Services>, options?: RunOptions) => Promise<A>;
49
56
  /** Decoded asset bytes, downloading `url` sources through the executor. */
50
- bytes: (asset: Media.Asset, options?: RunOptions) => Promise<Uint8Array<ArrayBufferLike>>;
51
- base64: (asset: Media.Asset, options?: RunOptions) => Promise<string>;
57
+ bytes: (asset: Media.Asset, options?: AssetRunOptions) => Promise<Uint8Array<ArrayBufferLike>>;
58
+ base64: (asset: Media.Asset, options?: AssetRunOptions) => Promise<string>;
52
59
  /** Pull a `url` asset into owned bytes before the provider URL expires. */
53
- materialize: (asset: Media.Asset, options?: RunOptions) => Promise<Media.Asset>;
60
+ materialize: (asset: Media.Asset, options?: AssetRunOptions) => Promise<Media.Asset>;
54
61
  /** Read a file into an asset like `Media.file`: sniffed media type, then the extension's. */
55
- file: (path: string, options?: Media.AssetOptions & RunOptions) => Promise<Media.Asset>;
62
+ file: (path: string, options?: Media.AssetOptions & AssetRunOptions) => Promise<Media.Asset>;
56
63
  /** Write an asset's bytes like `Media.write`: streamed through a temporary file that replaces `path` on success. */
57
- write: (asset: Media.Asset, path: string, options?: RunOptions) => Promise<void>;
64
+ write: (asset: Media.Asset, path: string, options?: AssetRunOptions) => Promise<void>;
58
65
  request: typeof LLM.request;
59
66
  generate: {
60
67
  <const Model extends LanguageModel, S extends OutputSchema>(input: LLMRequestInput<Model> & {
@@ -93,10 +100,6 @@ export declare const make: (options?: Options) => {
93
100
  readonly type: "image";
94
101
  readonly index: number;
95
102
  readonly image: Media.Asset;
96
- } | {
97
- readonly type: "generation-queued";
98
- readonly id: string;
99
- readonly position?: number | undefined;
100
103
  } | {
101
104
  readonly type: "generation-progress";
102
105
  readonly id: string;
@@ -171,10 +174,6 @@ export declare const make: (options?: Options) => {
171
174
  readonly startSeconds: number;
172
175
  readonly endSeconds: number;
173
176
  }[];
174
- } | {
175
- readonly type: "generation-queued";
176
- readonly id: string;
177
- readonly position?: number | undefined;
178
177
  } | {
179
178
  readonly type: "generation-progress";
180
179
  readonly id: string;
@@ -224,10 +223,6 @@ export declare const make: (options?: Options) => {
224
223
  readonly endSeconds: number;
225
224
  readonly speaker?: string | undefined;
226
225
  };
227
- } | {
228
- readonly type: "generation-queued";
229
- readonly id: string;
230
- readonly position?: number | undefined;
231
226
  } | {
232
227
  readonly type: "generation-progress";
233
228
  readonly id: string;
package/dist/index.js CHANGED
@@ -28,35 +28,36 @@ const abortEffect = (signal) => signal === undefined
28
28
  export const make = (options = {}) => {
29
29
  const runtime = ManagedRuntime.make(AIClient.layerWith(options.layer ?? RequestExecutor.fetchLayer));
30
30
  /** Run any package Effect (for example `ai.compact(...)` from `@opencode/ai/effect`) inside this runtime. */
31
- const run = (effect, options) => runtime.runPromise(Effect.raceFirst(effect, abortEffect(options?.signal)));
32
- const iterate = (stream, options) => Stream.toAsyncIterable(Stream.unwrap(runtime.contextEffect.pipe(Effect.map((context) => stream.pipe(Stream.interruptWhen(abortEffect(options?.signal)), Stream.provideContext(context))))));
31
+ const run = (effect, options) => runtime.runPromise(Effect.raceFirst(scoped(effect, options?.http), abortEffect(options?.signal)));
32
+ const iterate = (stream, options) => Stream.toAsyncIterable(Stream.unwrap(runtime.contextEffect.pipe(Effect.map((context) => scoped(stream, options?.http).pipe(Stream.interruptWhen(abortEffect(options?.signal)), Stream.provideContext(context))))));
33
+ // `run` and `iterate` apply `http` around the whole call, so the LLM call gets none of its own.
33
34
  function generate(input, options) {
34
- return run(LLM.generate(input, options), options);
35
+ return run(LLM.generate(input, { ...options, http: undefined }), options);
35
36
  }
36
37
  function stream(input, options) {
37
- return iterate(LLM.stream(input, options), options);
38
+ return iterate(LLM.stream(input, { ...options, http: undefined }), options);
38
39
  }
39
40
  function loop(input, options) {
40
- return iterate(LLM.loop(input, options), options);
41
+ return iterate(LLM.loop(input, { ...options, http: undefined }), options);
41
42
  }
42
43
  /** Read `events` from `stream` or `loop` to the end and fold them into the final result. */
43
44
  const collect = (events, options) => run(LLM.collect(Stream.fromAsyncIterable(events, (error) => error)), options);
44
45
  // The overloads pair each request type with its mechanism; `LLM.compact` checks the route's capability at runtime.
45
46
  function compact(request, options) {
46
47
  return options?.mechanism === "trigger"
47
- ? run(LLM.compact(request, options), options)
48
- : run(LLM.compact(request, options), options);
48
+ ? run(LLM.compact(request, { ...options, http: undefined }), options)
49
+ : run(LLM.compact(request, { ...options, http: undefined }), options);
49
50
  }
50
- const handle = (generation) => ({
51
+ const handle = (http) => (generation) => ({
51
52
  ...generation.snapshot,
52
53
  snapshot: generation.snapshot,
53
54
  terminal: generation.terminal,
54
55
  token: generation.token,
55
- await: (options) => run(generation.await(options), options),
56
- events: (options) => iterate(generation.events(options), options),
57
- result: (options) => run(generation.result(), options),
58
- refresh: (options) => run(generation.refresh(), options).then(handle),
59
- cancel: (options) => run(generation.cancel(), options),
56
+ await: (options) => run(generation.await(options), { http, ...options }),
57
+ events: (options) => iterate(generation.events(options), { http, ...options }),
58
+ result: (options) => run(generation.result(), { http, ...options }),
59
+ refresh: (options) => run(generation.refresh(), { http, ...options }).then(handle(options?.http ?? http)),
60
+ cancel: (options) => run(generation.cancel(), { http, ...options }),
60
61
  });
61
62
  return {
62
63
  run,
@@ -148,13 +149,14 @@ export const make = (options = {}) => {
148
149
  request: Transcription.request,
149
150
  generate: (input, options) => run(Transcription.generate(input, { poll: options?.poll }), options),
150
151
  stream: (input, options) => iterate(Transcription.stream(input, { poll: options?.poll }), options),
151
- start: (input, options) => run(Transcription.start(input), options).then(handle),
152
- resume: (model, token, options) => run(Transcription.resume(model, token), options).then(handle),
152
+ start: (input, options) => run(Transcription.start(input), options).then(handle(options?.http)),
153
+ resume: (model, token, options) => run(Transcription.resume(model, token), options).then(handle(options?.http)),
153
154
  },
154
155
  },
155
156
  dispose: () => runtime.dispose(),
156
157
  };
157
158
  };
159
+ const scoped = (self, http) => (http === undefined ? self : RequestExecutor.withMiddleware(http)(self));
158
160
  const fileError = (message, cause) => new AIError({ reason: new InvalidRequestError({ message, cause }) });
159
161
  /** `tool` from `@opencode/ai/effect`, with an `execute` that returns a value or a Promise and throws to fail. */
160
162
  export const tool = (config) => {
@@ -33,7 +33,11 @@ export declare const make: <Self, Req extends RoutedRequest<Req, Event, Response
33
33
  responseEvents: (response: Response) => readonly Event[];
34
34
  layer: Layer.Layer<Self, never, RequestExecutor.RequestExecutorService>;
35
35
  generate: (request: Req, options?: AwaitOptions) => Effect.Effect<Response, AIError, Self>;
36
- stream: (request: Req, options?: AwaitOptions) => Stream.Stream<Event | Observation, AIError, Exclude<Self, import("effect/Scope").Scope>>;
36
+ stream: (request: Req, options?: AwaitOptions) => Stream.Stream<Event | {
37
+ readonly type: "generation-progress";
38
+ readonly id: string;
39
+ readonly progress?: number | undefined;
40
+ }, AIError, Exclude<Self, import("effect/Scope").Scope>>;
37
41
  start: (request: Req) => Effect.Effect<Generation<Response>, AIError, Self>;
38
42
  resume: (model: Req["model"], token: unknown) => Effect.Effect<Generation<Response>, AIError, Self>;
39
43
  };