@opencode/ai 0.0.0-dev-466 → 0.0.0-dev-469

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 (87) hide show
  1. package/README.md +27 -16
  2. package/dist/cache-policy.d.ts +1 -1
  3. package/dist/cache-policy.js +2 -2
  4. package/dist/decision-client.d.ts +3 -3
  5. package/dist/decision.d.ts +13 -120
  6. package/dist/decision.js +17 -13
  7. package/dist/effect.d.ts +8 -6
  8. package/dist/effect.js +4 -3
  9. package/dist/effort-updates.d.ts +1 -1
  10. package/dist/effort-updates.js +2 -2
  11. package/dist/experimental/cloudflare-run-extraction.d.ts +7 -3
  12. package/dist/experimental/cloudflare-run-extraction.js +4 -1
  13. package/dist/experimental/extraction.d.ts +12 -38
  14. package/dist/experimental/extraction.js +13 -11
  15. package/dist/experimental.d.ts +8 -8
  16. package/dist/experimental.js +4 -4
  17. package/dist/image-client.d.ts +11 -5
  18. package/dist/image-client.js +1 -1
  19. package/dist/image.d.ts +21 -62
  20. package/dist/image.js +16 -13
  21. package/dist/index.d.ts +20 -14
  22. package/dist/index.js +6 -3
  23. package/dist/llm.d.ts +60 -25
  24. package/dist/llm.js +126 -38
  25. package/dist/protocols/alibaba-images.d.ts +2 -2
  26. package/dist/protocols/alibaba-messages.js +3 -3
  27. package/dist/protocols/anthropic-messages.d.ts +1 -1
  28. package/dist/protocols/anthropic-messages.js +4 -3
  29. package/dist/protocols/assemblyai-transcription.d.ts +2 -2
  30. package/dist/protocols/cartesia-speech.d.ts +2 -2
  31. package/dist/protocols/cloudflare-workers-ai-images.d.ts +2 -2
  32. package/dist/protocols/deepgram-speech.d.ts +2 -2
  33. package/dist/protocols/deepgram-transcription.d.ts +2 -2
  34. package/dist/protocols/elevenlabs-speech.d.ts +2 -2
  35. package/dist/protocols/elevenlabs-transcription.d.ts +2 -2
  36. package/dist/protocols/gemini.js +2 -8
  37. package/dist/protocols/google-images.d.ts +2 -2
  38. package/dist/protocols/google-speech.d.ts +2 -2
  39. package/dist/protocols/google-transcription.d.ts +2 -2
  40. package/dist/protocols/google-video.d.ts +2 -2
  41. package/dist/protocols/meta-images.d.ts +2 -2
  42. package/dist/protocols/meta-responses.js +4 -3
  43. package/dist/protocols/minimax-images.d.ts +2 -2
  44. package/dist/protocols/open-responses.d.ts +1 -21
  45. package/dist/protocols/openai-chat.d.ts +1 -21
  46. package/dist/protocols/openai-chat.js +7 -9
  47. package/dist/protocols/openai-decisions.js +1 -1
  48. package/dist/protocols/openai-images.d.ts +2 -2
  49. package/dist/protocols/openai-responses.js +0 -1
  50. package/dist/protocols/openai-speech.d.ts +2 -2
  51. package/dist/protocols/openai-transcription.d.ts +2 -2
  52. package/dist/protocols/runway-video.d.ts +2 -2
  53. package/dist/protocols/shared.d.ts +1 -1
  54. package/dist/protocols/shared.js +2 -2
  55. package/dist/protocols/system-one.d.ts +4 -4
  56. package/dist/protocols/utils/responses-checkpoint.js +1 -1
  57. package/dist/protocols/utils/responses-compaction.d.ts +1 -1
  58. package/dist/protocols/utils/responses-compaction.js +4 -3
  59. package/dist/protocols/utils/tool-stream.d.ts +3 -63
  60. package/dist/protocols/xai-images.d.ts +2 -2
  61. package/dist/protocols/xai-video.d.ts +2 -2
  62. package/dist/protocols/zai-images.d.ts +2 -2
  63. package/dist/protocols/zai-messages.js +3 -3
  64. package/dist/provider-error.js +4 -4
  65. package/dist/providers/vercel-ai-gateway.js +4 -3
  66. package/dist/route/client.d.ts +43 -19
  67. package/dist/route/client.js +30 -18
  68. package/dist/route/decision.d.ts +6 -6
  69. package/dist/schema/errors.d.ts +1 -2
  70. package/dist/schema/errors.js +1 -2
  71. package/dist/schema/events.d.ts +61 -769
  72. package/dist/schema/events.js +3 -30
  73. package/dist/schema/messages.d.ts +20 -92
  74. package/dist/schema/messages.js +1 -52
  75. package/dist/speech-client.d.ts +5 -5
  76. package/dist/speech.d.ts +17 -19
  77. package/dist/speech.js +16 -12
  78. package/dist/testing.d.ts +8 -168
  79. package/dist/tool-runtime.js +12 -30
  80. package/dist/transcription-client.d.ts +5 -5
  81. package/dist/transcription.d.ts +17 -19
  82. package/dist/transcription.js +16 -12
  83. package/dist/video-client.d.ts +5 -5
  84. package/dist/video-client.js +1 -1
  85. package/dist/video.d.ts +18 -20
  86. package/dist/video.js +16 -12
  87. package/package.json +3 -3
@@ -1,16 +1,16 @@
1
1
  export { VideoClient } from "./video-client.js";
2
- export { VideoAspectRatio, VideoEvent, VideoFrames, VideoModel, VideoModelSchema, VideoRequest, VideoResponse, } from "./video.js";
3
- export type { VideoModelOptions, VideoOptions, VideoRequestFor, VideoRequestInput, VideoResolution, VideoRoute, } from "./video.js";
2
+ export { VideoAspectRatio, VideoEvent, VideoFrames, VideoModel, VideoModelSchema, VideoResponse } from "./video.js";
3
+ export type { VideoModelOptions, VideoOptions, VideoRequest, VideoRequestInput, VideoResolution, VideoRoute, } from "./video.js";
4
4
  export { Video } from "./video.js";
5
5
  export { SpeechClient } from "./speech-client.js";
6
- export { SpeechEvent, SpeechModel, SpeechModelSchema, SpeechRequest, SpeechResponse, SpeechTimestamp, SpeechVoice, } from "./speech.js";
7
- export type { SpeechFormat, SpeechModelOptions, SpeechOptions, SpeechRequestFor, SpeechRequestInput, SpeechRoute, } from "./speech.js";
6
+ export { SpeechEvent, SpeechModel, SpeechModelSchema, SpeechResponse, SpeechTimestamp, SpeechVoice } from "./speech.js";
7
+ export type { SpeechFormat, SpeechModelOptions, SpeechOptions, SpeechRequest, SpeechRequestInput, SpeechRoute, } from "./speech.js";
8
8
  export { Speech } from "./speech.js";
9
9
  export { TranscriptionClient } from "./transcription-client.js";
10
- export { TranscriptionEvent, TranscriptionModel, TranscriptionModelSchema, TranscriptionRequest, TranscriptionResponse, TranscriptionSegment, TranscriptionTimestamps, TranscriptionWord, } from "./transcription.js";
11
- export type { TranscriptionModelOptions, TranscriptionOptions, TranscriptionRequestFor, TranscriptionRequestInput, TranscriptionRoute, } from "./transcription.js";
10
+ export { TranscriptionEvent, TranscriptionModel, TranscriptionModelSchema, TranscriptionResponse, TranscriptionSegment, TranscriptionTimestamps, TranscriptionWord, } from "./transcription.js";
11
+ export type { TranscriptionModelOptions, TranscriptionOptions, TranscriptionRequest, TranscriptionRequestInput, TranscriptionRoute, } from "./transcription.js";
12
12
  export { Transcription } from "./transcription.js";
13
- export { Extraction, ExtractionModel, ExtractionRequest, ExtractionResponse } from "./experimental/extraction.js";
14
- export type { ExtractionAnswerFor, ExtractionAnswersFor, ExtractionField, ExtractionFields, ExtractionModelOptions, ExtractionOptions, ExtractionRequestFor, ExtractionRequestInput, ExtractionResponseFor, ExtractionRoute, } from "./experimental/extraction.js";
13
+ export { Extraction, ExtractionModel, ExtractionResponse } from "./experimental/extraction.js";
14
+ export type { ExtractionAnswerFor, ExtractionAnswersFor, ExtractionField, ExtractionFields, ExtractionModelOptions, ExtractionOptions, ExtractionRequest, ExtractionRequestInput, ExtractionResponseFor, ExtractionRoute, } from "./experimental/extraction.js";
15
15
  export { Realtime } from "./experimental/realtime.js";
16
16
  export type { RealtimeConnectInput, RealtimeSession, RealtimeTransport, RealtimeTransportInput, RealtimeTransportType, } from "./experimental/realtime.js";
@@ -1,11 +1,11 @@
1
1
  export { VideoClient } from "./video-client.js";
2
- export { VideoAspectRatio, VideoEvent, VideoFrames, VideoModel, VideoModelSchema, VideoRequest, VideoResponse, } from "./video.js";
2
+ export { VideoAspectRatio, VideoEvent, VideoFrames, VideoModel, VideoModelSchema, VideoResponse } from "./video.js";
3
3
  export { Video } from "./video.js";
4
4
  export { SpeechClient } from "./speech-client.js";
5
- export { SpeechEvent, SpeechModel, SpeechModelSchema, SpeechRequest, SpeechResponse, SpeechTimestamp, SpeechVoice, } from "./speech.js";
5
+ export { SpeechEvent, SpeechModel, SpeechModelSchema, SpeechResponse, SpeechTimestamp, SpeechVoice } from "./speech.js";
6
6
  export { Speech } from "./speech.js";
7
7
  export { TranscriptionClient } from "./transcription-client.js";
8
- export { TranscriptionEvent, TranscriptionModel, TranscriptionModelSchema, TranscriptionRequest, TranscriptionResponse, TranscriptionSegment, TranscriptionTimestamps, TranscriptionWord, } from "./transcription.js";
8
+ export { TranscriptionEvent, TranscriptionModel, TranscriptionModelSchema, TranscriptionResponse, TranscriptionSegment, TranscriptionTimestamps, TranscriptionWord, } from "./transcription.js";
9
9
  export { Transcription } from "./transcription.js";
10
- export { Extraction, ExtractionModel, ExtractionRequest, ExtractionResponse } from "./experimental/extraction.js";
10
+ export { Extraction, ExtractionModel, ExtractionResponse } from "./experimental/extraction.js";
11
11
  export { Realtime } from "./experimental/realtime.js";
@@ -1,7 +1,7 @@
1
1
  import { Context } from "effect";
2
2
  import { MediaClient } from "./media-client.js";
3
- import { type ImageEvent, type ImageRequestFor, type ImageResponse } from "./image.js";
4
- export type Interface = MediaClient.Interface<ImageRequestFor, ImageEvent, ImageResponse>;
3
+ import { type ImageEvent, type ImageRequest, type ImageResponse } from "./image.js";
4
+ export type Interface = MediaClient.Interface<ImageRequest, ImageEvent, ImageResponse>;
5
5
  declare const ImageClientService_base: Context.ServiceClass<ImageClientService, "@opencode/ImageClient", Interface>;
6
6
  export declare class ImageClientService extends ImageClientService_base {
7
7
  }
@@ -54,8 +54,12 @@ export declare const ImageClient: {
54
54
  } | undefined;
55
55
  })[];
56
56
  readonly layer: import("effect/Layer").Layer<ImageClientService, never, import("./route.js").RequestExecutorService>;
57
- readonly generate: (request: ImageRequestFor, options?: import("./generation.js").AwaitOptions) => import("effect/Effect").Effect<ImageResponse, import("./effect.js").AIError, ImageClientService>;
58
- readonly stream: (request: ImageRequestFor, options?: import("./generation.js").AwaitOptions) => import("effect/Stream").Stream<{
57
+ readonly generate: (request: ImageRequest<import("./image.js").ImageModel<{
58
+ readonly [x: string]: unknown;
59
+ }>>, options?: import("./generation.js").AwaitOptions) => import("effect/Effect").Effect<ImageResponse, import("./effect.js").AIError, ImageClientService>;
60
+ readonly stream: (request: ImageRequest<import("./image.js").ImageModel<{
61
+ readonly [x: string]: unknown;
62
+ }>>, options?: import("./generation.js").AwaitOptions) => import("effect/Stream").Stream<{
59
63
  readonly type: "image-partial";
60
64
  readonly index: number;
61
65
  readonly image: import("./media.js").Asset;
@@ -92,7 +96,9 @@ export declare const ImageClient: {
92
96
  };
93
97
  } | undefined;
94
98
  } | import("./generation.js").Observation, import("./effect.js").AIError, ImageClientService>;
95
- readonly start: (request: ImageRequestFor) => import("effect/Effect").Effect<import("./generation.js").Generation<ImageResponse>, import("./effect.js").AIError, ImageClientService>;
99
+ readonly start: (request: ImageRequest<import("./image.js").ImageModel<{
100
+ readonly [x: string]: unknown;
101
+ }>>) => import("effect/Effect").Effect<import("./generation.js").Generation<ImageResponse>, import("./effect.js").AIError, ImageClientService>;
96
102
  readonly resume: (model: import("./image.js").ImageModel<{
97
103
  readonly [x: string]: unknown;
98
104
  }>, token: unknown) => import("effect/Effect").Effect<import("./generation.js").Generation<ImageResponse>, import("./effect.js").AIError, ImageClientService>;
@@ -1,6 +1,6 @@
1
1
  import { Context } from "effect";
2
2
  import { MediaClient } from "./media-client.js";
3
- import { ImageOutputEvent, ImageFinishEvent, } from "./image.js";
3
+ import { ImageOutputEvent, ImageFinishEvent } from "./image.js";
4
4
  export class ImageClientService extends Context.Service()("@opencode/ImageClient") {
5
5
  }
6
6
  export const Service = ImageClientService;
package/dist/image.d.ts CHANGED
@@ -6,13 +6,13 @@ import { MediaRoute } from "./route/media.js";
6
6
  import { AIError, HttpOptions, type OpenString } from "./schema/index.js";
7
7
  import { Service } from "./image-client.js";
8
8
  export type ImageOptions = MediaModel.Options;
9
- export type ImageRoute = MediaRoute.AnyRoute<ImageRequestFor, ImageEvent, ImageResponse>;
9
+ export type ImageRoute = MediaRoute.AnyRoute<ImageRequest, ImageEvent, ImageResponse>;
10
10
  export declare class ImageModel<Options extends ImageOptions = ImageOptions> extends MediaModel<ImageRoute, Options> {
11
11
  protected readonly _ImageModel: void;
12
12
  /** The number of type arguments selects the kind: `<Options>`, `<Options, Frame, State>`, or `<Options, Token>`. */
13
- static fromRoute<Options extends ImageOptions>(route: MediaModel.InlineRouteInput<ImageRequestFor<Options>, ImageResponse>, input: MediaRoute.ModelInput): ImageModel<Options>;
14
- static fromRoute<Options extends ImageOptions, Frame, State>(route: MediaModel.StreamRouteInput<ImageRequestFor<Options>, ImageEvent, Frame, State>, input: MediaRoute.ModelInput): ImageModel<Options>;
15
- static fromRoute<Options extends ImageOptions, Token>(route: MediaModel.QueuedRouteInput<ImageRequestFor<Options>, ImageResponse, Token>, input: MediaRoute.ModelInput): ImageModel<Options>;
13
+ static fromRoute<Options extends ImageOptions>(route: MediaModel.InlineRouteInput<ImageRequest<ImageModel<Options>>, ImageResponse>, input: MediaRoute.ModelInput): ImageModel<Options>;
14
+ static fromRoute<Options extends ImageOptions, Frame, State>(route: MediaModel.StreamRouteInput<ImageRequest<ImageModel<Options>>, ImageEvent, Frame, State>, input: MediaRoute.ModelInput): ImageModel<Options>;
15
+ static fromRoute<Options extends ImageOptions, Token>(route: MediaModel.QueuedRouteInput<ImageRequest<ImageModel<Options>>, ImageResponse, Token>, input: MediaRoute.ModelInput): ImageModel<Options>;
16
16
  }
17
17
  export declare const ImageModelSchema: Schema.declare<ImageModel<{
18
18
  readonly [x: string]: unknown;
@@ -23,62 +23,21 @@ export type ImageAspectRatio = Media.AspectRatio;
23
23
  export declare const ImageAspectRatio: Schema.declare<`${number}:${number}`, `${number}:${number}`>;
24
24
  /** Size tier: `1K` is about as many pixels as 1024×1024 at the requested aspect, `2K` as 2048×2048. */
25
25
  export type ImageResolution = OpenString<"1K" | "2K" | "4K">;
26
- declare const ImageRequest_base: Schema.Class<ImageRequest, Schema.Struct<{
27
- readonly model: Schema.declare<ImageModel<{
28
- readonly [x: string]: unknown;
29
- }>, ImageModel<{
30
- readonly [x: string]: unknown;
31
- }>>;
32
- readonly prompt: Schema.String;
26
+ /** A normalized image request, the plain object `ai.image.request(...)` returns. */
27
+ export interface ImageRequest<Model extends ImageModel = ImageModel> {
28
+ readonly model: Model;
29
+ readonly prompt: string;
33
30
  /** Edit sources or style/subject references, in order. */
34
- readonly images: Schema.optional<Schema.$Array<Schema.decodeTo<Schema.declare<Media.Asset, Media.Asset>, Schema.Struct<{
35
- readonly source: Schema.Union<readonly [Schema.Struct<{
36
- readonly type: Schema.Literal<"bytes">;
37
- readonly data: Schema.Uint8Array;
38
- readonly mediaType: Schema.String;
39
- }>, Schema.Struct<{
40
- readonly type: Schema.Literal<"base64">;
41
- readonly data: Schema.String;
42
- readonly mediaType: Schema.String;
43
- }>, Schema.Struct<{
44
- readonly type: Schema.Literal<"url">;
45
- readonly url: Schema.String;
46
- readonly mediaType: Schema.optional<Schema.String>;
47
- readonly expiresAt: Schema.optional<Schema.Number>;
48
- }>, Schema.Struct<{
49
- readonly type: Schema.Literal<"ref">;
50
- readonly provider: Schema.brand<Schema.String, "AI.ProviderID">;
51
- readonly id: Schema.String;
52
- readonly mediaType: Schema.optional<Schema.String>;
53
- }>]>;
54
- readonly info: Schema.optional<Schema.Struct<{
55
- readonly width: Schema.optional<Schema.Number>;
56
- readonly height: Schema.optional<Schema.Number>;
57
- readonly durationSeconds: Schema.optional<Schema.Number>;
58
- readonly sampleRate: Schema.optional<Schema.Number>;
59
- readonly channels: Schema.optional<Schema.Number>;
60
- readonly encoding: Schema.optional<Schema.String>;
61
- readonly format: Schema.optional<Schema.String>;
62
- }>>;
63
- readonly providerMetadata: Schema.optional<Schema.$Record<Schema.String, Schema.$Record<Schema.String, Schema.Unknown>>>;
64
- }>, never, never>>>;
65
- readonly n: Schema.optional<Schema.Int>;
66
- readonly aspect: Schema.optional<Schema.declare<`${number}:${number}`, `${number}:${number}`>>;
67
- readonly resolution: Schema.optional<Schema.String>;
68
- readonly providerOptions: Schema.optional<Schema.$Record<Schema.String, Schema.Unknown>>;
69
- readonly http: Schema.optional<typeof HttpOptions>;
70
- }>, {}>;
71
- export declare class ImageRequest extends ImageRequest_base {
72
- protected readonly _ImageRequest: void;
31
+ readonly images?: ReadonlyArray<Media.Asset>;
32
+ readonly n?: number;
33
+ readonly aspect?: ImageAspectRatio;
34
+ readonly resolution?: ImageResolution;
35
+ readonly providerOptions?: ImageModelOptions<Model>;
36
+ readonly http?: HttpOptions;
73
37
  }
74
- export type ImageRequestFor<Options extends ImageOptions = ImageOptions> = Omit<ImageRequest, "model" | "providerOptions"> & {
75
- readonly model: ImageModel<Options>;
76
- readonly providerOptions?: Options;
77
- };
78
38
  export type ImageModelOptions<Model> = Model extends ImageModel<infer Options> ? Options : never;
79
- export type ImageRequestInput<Model extends ImageModel = ImageModel> = Omit<ConstructorParameters<typeof ImageRequest>[0], "model" | "providerOptions" | "http" | "resolution"> & {
80
- readonly model: Model;
81
- readonly resolution?: ImageResolution;
39
+ /** Input accepted by `ai.image.request`, `generate`, `stream` and `start`. An `ImageRequest` is also a valid input. */
40
+ export type ImageRequestInput<Model extends ImageModel = ImageModel> = Omit<ImageRequest<Model>, "providerOptions" | "http"> & {
82
41
  readonly providerOptions?: NoInfer<ImageModelOptions<Model>>;
83
42
  readonly http?: HttpOptions.Input;
84
43
  };
@@ -1341,12 +1300,12 @@ export declare const ImageEvent: Schema.Union<readonly [Schema.Struct<{
1341
1300
  };
1342
1301
  };
1343
1302
  export type ImageEvent = Schema.Schema.Type<typeof imageEventTagged>;
1344
- export declare function request<const Model extends ImageModel>(input: ImageRequestInput<Model>): ImageRequestFor<ImageModelOptions<Model>>;
1345
- export declare function request(input: ImageRequest): ImageRequest;
1346
- export declare function generate<const Model extends ImageModel>(input: ImageRequest | ImageRequestInput<Model>, options?: AwaitOptions): Effect.Effect<ImageResponse, AIError, Service>;
1347
- export declare function stream<const Model extends ImageModel>(input: ImageRequest | ImageRequestInput<Model>, options?: AwaitOptions): Stream.Stream<ImageEvent, AIError, Service>;
1303
+ /** Normalize image request input into a plain `ImageRequest`. */
1304
+ export declare const request: <const Model extends ImageModel>(input: ImageRequestInput<Model>) => ImageRequest<Model>;
1305
+ export declare function generate<const Model extends ImageModel>(input: ImageRequestInput<Model>, options?: AwaitOptions): Effect.Effect<ImageResponse, AIError, Service>;
1306
+ export declare function stream<const Model extends ImageModel>(input: ImageRequestInput<Model>, options?: AwaitOptions): Stream.Stream<ImageEvent, AIError, Service>;
1348
1307
  /** Inline and streaming routes fail with `UnsupportedOperation`. */
1349
- export declare function start<const Model extends ImageModel>(input: ImageRequest | ImageRequestInput<Model>): Effect.Effect<Generation<ImageResponse>, AIError, Service>;
1308
+ export declare function start<const Model extends ImageModel>(input: ImageRequestInput<Model>): Effect.Effect<Generation<ImageResponse>, AIError, Service>;
1350
1309
  export declare const resume: (model: ImageModel, token: unknown) => Effect.Effect<Generation<ImageResponse>, AIError, Service>;
1351
1310
  export declare const Image: {
1352
1311
  readonly request: typeof request;
package/dist/image.js CHANGED
@@ -20,18 +20,16 @@ export const ImageModelSchema = Schema.declare((value) => value instanceof Image
20
20
  expected: "Image.Model",
21
21
  });
22
22
  export const ImageAspectRatio = Media.AspectRatio;
23
- export class ImageRequest extends Schema.Class("Image.Request")({
23
+ const ImageRequestSchema = Schema.Struct({
24
24
  model: ImageModelSchema,
25
25
  prompt: Schema.String,
26
- /** Edit sources or style/subject references, in order. */
27
26
  images: Schema.optional(Schema.Array(Media.AssetSchema)),
28
27
  n: Schema.optional(Schema.Int),
29
28
  aspect: Schema.optional(ImageAspectRatio),
30
29
  resolution: Schema.optional(Schema.String),
31
30
  providerOptions: Schema.optional(ProviderOptions),
32
31
  http: Schema.optional(HttpOptions),
33
- }) {
34
- }
32
+ });
35
33
  // ---------------------------------------------------------------------------
36
34
  // Response and events
37
35
  // ---------------------------------------------------------------------------
@@ -87,15 +85,20 @@ const collectResponse = (events) => {
87
85
  providerMetadata: finish.providerMetadata,
88
86
  }));
89
87
  };
90
- export function request(input) {
91
- if (input instanceof ImageRequest)
92
- return input;
93
- return new ImageRequest({
94
- ...input,
95
- http: HttpOptions.make(input.http),
96
- });
97
- }
98
- const requestEffect = (input) => tryRequest(() => request(input));
88
+ // ---------------------------------------------------------------------------
89
+ // Request-shaped call API
90
+ // ---------------------------------------------------------------------------
91
+ /** Normalize image request input into a plain `ImageRequest`. */
92
+ export const request = (input) => ({
93
+ ...input,
94
+ http: HttpOptions.make(input.http),
95
+ });
96
+ const validate = Schema.decodeUnknownSync(ImageRequestSchema);
97
+ const requestEffect = (input) => tryRequest(() => {
98
+ const normalized = request(input);
99
+ validate(normalized);
100
+ return normalized;
101
+ });
99
102
  export function generate(input, options) {
100
103
  return requestEffect(input).pipe(Effect.flatMap((request) => ImageClient.generate(request, options)));
101
104
  }
package/dist/index.d.ts CHANGED
@@ -7,8 +7,8 @@ import { LLM } from "./llm.js";
7
7
  import { Media } from "./media.js";
8
8
  import { type CheckpointRequest, type CompactionRequest, type EndpointCompactOptions, type StreamOptions, type TriggerCompactOptions } from "./route/client.js";
9
9
  import { RequestExecutor } from "./route/executor.js";
10
- import { LanguageModel, type CompactionCheckpointResponse, type CompactionResponse, type LLMEvent, type LLMRequest, type LLMResponse } from "./schema/index.js";
11
- import type { LoopInput, LoopObjectEvent, ObjectEvent, ObjectInput, ObjectResponse, OutputSchema, OutputType, RequestInput } from "./llm.js";
10
+ import { LanguageModel, type CompactionCheckpointResponse, type CompactionResponse, type LLMEvent, type LLMResponse } from "./schema/index.js";
11
+ import type { LoopObjectEvent, ObjectEvent, ObjectResponse, OutputSchema, OutputType, LLMLoopInput, LLMRequestInput } from "./llm.js";
12
12
  import { type SpeechModel, type SpeechRequest, type SpeechRequestInput } from "./speech.js";
13
13
  import { type TranscriptionModel, type TranscriptionRequest, type TranscriptionRequestInput } from "./transcription.js";
14
14
  import type { NoExecute, ToolExecuteContext, ToolInput, ToolInputType, ToolOptions, ToolSchema } from "./tool.js";
@@ -56,20 +56,25 @@ export declare const make: (options?: Options) => {
56
56
  write: (asset: Media.Asset, path: string, options?: RunOptions) => Promise<void>;
57
57
  request: typeof LLM.request;
58
58
  generate: {
59
- <const Model extends LanguageModel, S extends OutputSchema>(input: ObjectInput<S, Model>, options?: StreamOptions & RunOptions): Promise<ObjectResponse<OutputType<S>>>;
60
- <const Model extends LanguageModel>(input: RequestInput<Model> | LLMRequest, options?: StreamOptions & RunOptions): Promise<LLMResponse>;
59
+ <const Model extends LanguageModel, S extends OutputSchema>(input: LLMRequestInput<Model> & {
60
+ readonly schema: S;
61
+ }, options?: StreamOptions & RunOptions): Promise<ObjectResponse<OutputType<S>>>;
62
+ <const Model extends LanguageModel>(input: LLMRequestInput<Model>, options?: StreamOptions & RunOptions): Promise<LLMResponse>;
61
63
  };
62
64
  stream: {
63
- <const Model extends LanguageModel, S extends OutputSchema>(input: ObjectInput<S, Model>, options?: StreamOptions & RunOptions): AsyncIterable<ObjectEvent<OutputType<S>>>;
64
- <const Model extends LanguageModel>(input: RequestInput<Model> | LLMRequest, options?: StreamOptions & RunOptions): AsyncIterable<LLMEvent>;
65
+ <const Model extends LanguageModel, S extends OutputSchema>(input: LLMRequestInput<Model> & {
66
+ readonly schema: S;
67
+ }, options?: StreamOptions & RunOptions): AsyncIterable<ObjectEvent<OutputType<S>>>;
68
+ <const Model extends LanguageModel>(input: LLMRequestInput<Model>, options?: StreamOptions & RunOptions): AsyncIterable<LLMEvent>;
65
69
  };
66
70
  /** Stream model calls, running their tool calls, until the model stops calling tools or `until` is true. */
67
71
  loop: {
68
- <const Model extends LanguageModel, S extends OutputSchema>(input: LoopInput<Model> & {
72
+ <const Model extends LanguageModel, S extends OutputSchema>(input: LLMLoopInput<Model> & {
69
73
  readonly schema: S;
70
74
  }, options?: StreamOptions & RunOptions): AsyncIterable<LoopObjectEvent<OutputType<S>>>;
71
- <const Model extends LanguageModel>(input: LoopInput<Model>, options?: StreamOptions & RunOptions): AsyncIterable<LLMEvent>;
75
+ <const Model extends LanguageModel>(input: LLMLoopInput<Model>, options?: StreamOptions & RunOptions): AsyncIterable<LLMEvent>;
72
76
  };
77
+ collect: <Event extends LLMEvent>(events: AsyncIterable<Event>, options?: RunOptions) => Promise<LLM.CollectResult<Event>>;
73
78
  /** Compact a Responses request: a replacement window, or a checkpoint with `mechanism: "trigger"`. */
74
79
  compact: {
75
80
  (request: CheckpointRequest, options: TriggerCompactOptions & RunOptions): Promise<CompactionCheckpointResponse>;
@@ -314,13 +319,14 @@ export declare const tool: <Input extends ToolInput = typeof Schema.Unknown, Suc
314
319
  export declare const ai: Omit<Client, "dispose">;
315
320
  export * as AI from "./index.js";
316
321
  export { isContextOverflow, isContextOverflowFailure, isRetryable } from "./provider-error.js";
317
- export { ProtocolID, RouteID, ModelID, ProviderID, ResponseID, ContentBlockID, ToolCallID, JsonSchema, ProviderOptions, ProviderMetadata, HttpTimeout, RequestEncoding, HttpOptions, GenerationOptions, LanguageModelDefaults, ReasoningEfforts, ReasoningEffort, LanguageModelCompatibility, LanguageModel, LanguageModelSchema, CacheTTL, CacheHint, CachePolicyObject, CachePolicy, MessageRole, SystemPart, TextPart, MediaPart, ToolResultValue, ToolOutput, ToolCallPart, ToolResultPart, ReasoningPart, CompactionPart, EffortPart, ContentPart, Message, Freeform, ToolDefinition, ToolNamespace, ToolEntry, ToolChoice, LLMRequest, FinishReason, Usage, MediaUsage, CompactionResponse, CompactionCheckpointResponse, StepStart, TextStart, TextDelta, TextEnd, ReasoningStart, ReasoningDelta, ReasoningEnd, ToolInputStart, ToolInputDelta, ToolInputEnd, ToolInputError, ToolCall, ToolResult, ToolError, FinishReasonDetails, StepFinish, Finish, MediaEvent, ProviderErrorEvent, LLMEvent, LLMResponse, HttpContext, HttpRateLimitDetails, InvalidRequestError, UnsupportedOperationError, ProviderConfigurationError, AuthenticationError, RateLimitError, QuotaExceededError, ContentPolicyError, ProviderInternalError, TransportType, TransportOperation, TransportError, InvalidProviderOutputError, UnknownProviderError, TimeoutError, AIErrorReason, AIError, } from "./schema/index.js";
318
- export type { GenerationOptionsFields, GenerationOptionsInput, LanguageModelInput, LanguageModelProviderOptions, ToolDefinitionInput, ToolNamespaceInput, ToolNamespaceEntryInput, ToolEntryInput, UsageInput, } from "./schema/index.js";
319
- export { ImageAspectRatio, ImageEvent, ImageModel, ImageModelSchema, ImageRequest, ImageResponse } from "./image.js";
320
- export type { ImageModelOptions, ImageOptions, ImageRequestFor, ImageRequestInput, ImageResolution, ImageRoute, } from "./image.js";
321
- export { ChoiceAnswer, ChoiceQuestion, ChoiceValue, DecisionAnswer, DecisionJsonPart, DecisionMediaPart, DecisionModel, DecisionModelCompatibility, DecisionModelSchema, DecisionPart, DecisionQuestion, DecisionRequest, DecisionResponse, DecisionTextPart, DecisionUsage, ErrorAnswer, ErrorReason, PredicateAnswer, PredicateQuestion, ScoreAnswer, ScoreQuestion, } from "./decision.js";
322
- export type { AnswerFor, AnswersFor, DecisionInput, DecisionModelOptions, DecisionOptions, DecisionQuestions, DecisionRequestFor, DecisionRequestInput, DecisionResponseFor, DecisionModelRoute, } from "./decision.js";
322
+ export { ProtocolID, RouteID, ModelID, ProviderID, ResponseID, ContentBlockID, ToolCallID, JsonSchema, ProviderOptions, ProviderMetadata, HttpTimeout, RequestEncoding, HttpOptions, GenerationOptions, LanguageModelDefaults, ReasoningEfforts, ReasoningEffort, LanguageModelCompatibility, LanguageModel, LanguageModelSchema, CacheTTL, CacheHint, CachePolicyObject, CachePolicy, MessageRole, SystemPart, TextPart, MediaPart, ToolResultValue, ToolOutput, ToolCallPart, ToolResultPart, ReasoningPart, CompactionPart, EffortPart, ContentPart, Message, Freeform, ToolDefinition, ToolNamespace, ToolEntry, ToolChoice, FinishReason, Usage, MediaUsage, CompactionResponse, CompactionCheckpointResponse, StepStart, TextStart, TextDelta, TextEnd, ReasoningStart, ReasoningDelta, ReasoningEnd, ToolInputStart, ToolInputDelta, ToolInputEnd, ToolInputError, ToolCall, ToolResult, FinishReasonDetails, StepFinish, Finish, MediaEvent, RawEvent, LLMEvent, LLMResponse, HttpContext, HttpRateLimitDetails, InvalidRequestError, UnsupportedOperationError, ProviderConfigurationError, AuthenticationError, RateLimitError, QuotaExceededError, ContentPolicyError, ProviderInternalError, TransportType, TransportOperation, TransportError, InvalidProviderOutputError, UnknownProviderError, TimeoutError, AIErrorReason, AIError, } from "./schema/index.js";
323
+ export type { GenerationOptionsFields, GenerationOptionsInput, LanguageModelInput, LanguageModelProviderOptions, LLMRequest, ToolDefinitionInput, ToolNamespaceInput, ToolNamespaceEntryInput, ToolEntryInput, UsageInput, } from "./schema/index.js";
324
+ export { ImageAspectRatio, ImageEvent, ImageModel, ImageModelSchema, ImageResponse } from "./image.js";
325
+ export type { ImageModelOptions, ImageOptions, ImageRequest, ImageRequestInput, ImageResolution, ImageRoute, } from "./image.js";
326
+ export { ChoiceAnswer, ChoiceQuestion, ChoiceValue, DecisionAnswer, DecisionJsonPart, DecisionMediaPart, DecisionModel, DecisionModelCompatibility, DecisionModelSchema, DecisionPart, DecisionQuestion, DecisionResponse, DecisionTextPart, DecisionUsage, ErrorAnswer, ErrorReason, PredicateAnswer, PredicateQuestion, ScoreAnswer, ScoreQuestion, } from "./decision.js";
327
+ export type { AnswerFor, AnswersFor, DecisionInput, DecisionModelOptions, DecisionOptions, DecisionQuestions, DecisionRequest, DecisionRequestInput, DecisionResponseFor, DecisionModelRoute, } from "./decision.js";
323
328
  export { Media } from "./media.js";
324
329
  export type { AwaitOptions as GenerationAwaitOptions, Event as GenerationEvent, Poll, Route as GenerationRoute, Snapshot as GenerationSnapshot, Status as GenerationStatus, } from "./generation.js";
325
330
  export type { ExecutableTool, Tool, ToolExecute, ToolExecuteContext, ToolInput, ToolInputType, ToolModelOutputInput, ToolOptions, ToolSchema, ToolToModelOutput, } from "./tool.js";
326
331
  export type { LLM } from "./llm.js";
332
+ export type { LLMLoopInput, LLMRequestInput } from "./llm.js";
package/dist/index.js CHANGED
@@ -38,6 +38,8 @@ export const make = (options = {}) => {
38
38
  function loop(input, options) {
39
39
  return iterate(LLM.loop(input, options), options);
40
40
  }
41
+ /** Read `events` from `stream` or `loop` to the end and fold them into the final result. */
42
+ const collect = (events, options) => run(LLM.collect(Stream.fromAsyncIterable(events, (error) => error)), options);
41
43
  // The overloads pair each request type with its mechanism; `LLM.compact` checks the route's capability at runtime.
42
44
  function compact(request, options) {
43
45
  return options?.mechanism === "trigger"
@@ -86,6 +88,7 @@ export const make = (options = {}) => {
86
88
  stream,
87
89
  /** Stream model calls, running their tool calls, until the model stops calling tools or `until` is true. */
88
90
  loop,
91
+ collect,
89
92
  /** Compact a Responses request: a replacement window, or a checkpoint with `mechanism: "trigger"`. */
90
93
  compact,
91
94
  canCompact: LLM.canCompact,
@@ -139,7 +142,7 @@ export const tool = (config) => {
139
142
  export const ai = make();
140
143
  export * as AI from "./index.js";
141
144
  export { isContextOverflow, isContextOverflowFailure, isRetryable } from "./provider-error.js";
142
- export { ProtocolID, RouteID, ModelID, ProviderID, ResponseID, ContentBlockID, ToolCallID, JsonSchema, ProviderOptions, ProviderMetadata, HttpTimeout, RequestEncoding, HttpOptions, GenerationOptions, LanguageModelDefaults, ReasoningEfforts, ReasoningEffort, LanguageModelCompatibility, LanguageModel, LanguageModelSchema, CacheTTL, CacheHint, CachePolicyObject, CachePolicy, MessageRole, SystemPart, TextPart, MediaPart, ToolResultValue, ToolOutput, ToolCallPart, ToolResultPart, ReasoningPart, CompactionPart, EffortPart, ContentPart, Message, Freeform, ToolDefinition, ToolNamespace, ToolEntry, ToolChoice, LLMRequest, FinishReason, Usage, MediaUsage, CompactionResponse, CompactionCheckpointResponse, StepStart, TextStart, TextDelta, TextEnd, ReasoningStart, ReasoningDelta, ReasoningEnd, ToolInputStart, ToolInputDelta, ToolInputEnd, ToolInputError, ToolCall, ToolResult, ToolError, FinishReasonDetails, StepFinish, Finish, MediaEvent, ProviderErrorEvent, LLMEvent, LLMResponse, HttpContext, HttpRateLimitDetails, InvalidRequestError, UnsupportedOperationError, ProviderConfigurationError, AuthenticationError, RateLimitError, QuotaExceededError, ContentPolicyError, ProviderInternalError, TransportType, TransportOperation, TransportError, InvalidProviderOutputError, UnknownProviderError, TimeoutError, AIErrorReason, AIError, } from "./schema/index.js";
143
- export { ImageAspectRatio, ImageEvent, ImageModel, ImageModelSchema, ImageRequest, ImageResponse } from "./image.js";
144
- export { ChoiceAnswer, ChoiceQuestion, ChoiceValue, DecisionAnswer, DecisionJsonPart, DecisionMediaPart, DecisionModel, DecisionModelCompatibility, DecisionModelSchema, DecisionPart, DecisionQuestion, DecisionRequest, DecisionResponse, DecisionTextPart, DecisionUsage, ErrorAnswer, ErrorReason, PredicateAnswer, PredicateQuestion, ScoreAnswer, ScoreQuestion, } from "./decision.js";
145
+ export { ProtocolID, RouteID, ModelID, ProviderID, ResponseID, ContentBlockID, ToolCallID, JsonSchema, ProviderOptions, ProviderMetadata, HttpTimeout, RequestEncoding, HttpOptions, GenerationOptions, LanguageModelDefaults, ReasoningEfforts, ReasoningEffort, LanguageModelCompatibility, LanguageModel, LanguageModelSchema, CacheTTL, CacheHint, CachePolicyObject, CachePolicy, MessageRole, SystemPart, TextPart, MediaPart, ToolResultValue, ToolOutput, ToolCallPart, ToolResultPart, ReasoningPart, CompactionPart, EffortPart, ContentPart, Message, Freeform, ToolDefinition, ToolNamespace, ToolEntry, ToolChoice, FinishReason, Usage, MediaUsage, CompactionResponse, CompactionCheckpointResponse, StepStart, TextStart, TextDelta, TextEnd, ReasoningStart, ReasoningDelta, ReasoningEnd, ToolInputStart, ToolInputDelta, ToolInputEnd, ToolInputError, ToolCall, ToolResult, FinishReasonDetails, StepFinish, Finish, MediaEvent, RawEvent, LLMEvent, LLMResponse, HttpContext, HttpRateLimitDetails, InvalidRequestError, UnsupportedOperationError, ProviderConfigurationError, AuthenticationError, RateLimitError, QuotaExceededError, ContentPolicyError, ProviderInternalError, TransportType, TransportOperation, TransportError, InvalidProviderOutputError, UnknownProviderError, TimeoutError, AIErrorReason, AIError, } from "./schema/index.js";
146
+ export { ImageAspectRatio, ImageEvent, ImageModel, ImageModelSchema, ImageResponse } from "./image.js";
147
+ export { ChoiceAnswer, ChoiceQuestion, ChoiceValue, DecisionAnswer, DecisionJsonPart, DecisionMediaPart, DecisionModel, DecisionModelCompatibility, DecisionModelSchema, DecisionPart, DecisionQuestion, DecisionResponse, DecisionTextPart, DecisionUsage, ErrorAnswer, ErrorReason, PredicateAnswer, PredicateQuestion, ScoreAnswer, ScoreQuestion, } from "./decision.js";
145
148
  export { Media } from "./media.js";
package/dist/llm.d.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import { Effect, JsonSchema, Schema, Stream } from "effect";
2
- import { Service, type StreamOptions } from "./route/client.js";
3
- import { GenerationOptions, HttpOptions, AIError, LLMEvent, LLMRequest, LLMResponse, Message, LanguageModel, SystemPart, type Finish, ToolChoice, type ContentPart, type LanguageModelProviderOptions, type ToolEntryInput } from "./schema/index.js";
2
+ import { Service, canCompact, type CheckpointRequest, type CompactionRequest, type EndpointCompactOptions, type StreamOptions, type TriggerCompactOptions } from "./route/client.js";
3
+ import { GenerationOptions, HttpOptions, AIError, LLMEvent, LLMResponse, Message, LanguageModel, SystemPart, type CachePolicy, type CompactionCheckpointResponse, type CompactionResponse, type Finish, type FinishReasonDetails, ToolChoice, Usage, type ContentPart, type LanguageModelProviderOptions, type LLMRequest, type ToolEntryInput } from "./schema/index.js";
4
4
  import { type ExecutableTool, type Tool, type ToolSchema } from "./tool.js";
5
- /** Input accepted by `LLM.request`, normalized into the canonical `LLMRequest` class. */
6
- export type RequestInput<SelectedLanguageModel extends LanguageModel = LanguageModel> = RequestFields<SelectedLanguageModel> & PromptOrMessages;
5
+ /** Input accepted by `ai.request`, `ai.generate`, `ai.stream` and `ai.compact`. An `LLMRequest` is also a valid input. */
6
+ export type LLMRequestInput<Model extends LanguageModel = LanguageModel> = TextRequestFields<Model> & PromptOrMessages;
7
7
  /** Exactly one of `prompt`, a single user message, or `messages`, the full conversation. */
8
8
  export type PromptOrMessages = {
9
9
  readonly prompt: string | ContentPart | ReadonlyArray<ContentPart>;
@@ -12,22 +12,26 @@ export type PromptOrMessages = {
12
12
  readonly messages: ReadonlyArray<Message | Message.Input>;
13
13
  readonly prompt?: never;
14
14
  };
15
- type RequestFields<SelectedLanguageModel extends LanguageModel> = Omit<ConstructorParameters<typeof LLMRequest>[0], "model" | "system" | "messages" | "tools" | "toolChoice" | "generation" | "http" | "providerOptions"> & {
16
- readonly model: SelectedLanguageModel;
15
+ interface TextRequestFields<Model extends LanguageModel> {
16
+ readonly id?: string;
17
+ readonly model: Model;
17
18
  readonly system?: string | SystemPart | ReadonlyArray<SystemPart>;
18
19
  /** Sent in array order. Pass `tool(...)` values or wire entries such as hosted provider tools. */
19
20
  readonly tools?: ReadonlyArray<Tool | ToolEntryInput>;
20
21
  readonly toolChoice?: ToolChoice.Input;
21
22
  readonly generation?: GenerationOptions.Input;
22
- readonly providerOptions?: NoInfer<LanguageModelProviderOptions<SelectedLanguageModel>>;
23
+ readonly providerOptions?: NoInfer<LanguageModelProviderOptions<Model>>;
23
24
  readonly http?: HttpOptions.Input;
24
- };
25
+ readonly cache?: CachePolicy;
26
+ /** Stable cache affinity for protocols that support provider-managed prompt caching. */
27
+ readonly promptCacheKey?: string;
28
+ readonly metadata?: Record<string, unknown>;
29
+ /** Emit a `raw` event with each provider frame before the events parsed from it. */
30
+ readonly includeRaw?: boolean;
31
+ }
25
32
  /** An Effect Schema decodes and types `object`; a raw JSON Schema leaves it `unknown`. */
26
33
  export type OutputSchema = ToolSchema<any> | JsonSchema.JsonSchema;
27
34
  export type OutputType<S extends OutputSchema> = S extends Schema.Top ? Schema.Schema.Type<S> : unknown;
28
- export type ObjectInput<S extends OutputSchema, SelectedLanguageModel extends LanguageModel = LanguageModel> = RequestInput<SelectedLanguageModel> & {
29
- readonly schema: S;
30
- };
31
35
  export type ObjectResponse<T> = LLMResponse & {
32
36
  readonly object: T;
33
37
  };
@@ -35,17 +39,24 @@ export type ObjectResponse<T> = LLMResponse & {
35
39
  export type ObjectEvent<T> = Exclude<LLMEvent, Finish> | (Finish & {
36
40
  readonly object: T;
37
41
  });
38
- /** Loop events for a request with `schema`: the final step's `finish` event carries the decoded `object`. */
42
+ /** Loop events for a request with `schema`: the loop's `finish` event carries the decoded `object`. */
39
43
  export type LoopObjectEvent<T> = Exclude<LLMEvent, Finish> | (Finish & {
40
44
  readonly object?: T;
41
45
  });
42
46
  /** Run one model call. With `schema`, the model must answer through a forced tool call decoded into `object`. */
43
- export declare function generate<const Model extends LanguageModel, S extends OutputSchema>(input: ObjectInput<S, Model>, options?: StreamOptions): Effect.Effect<ObjectResponse<OutputType<S>>, AIError, Service>;
44
- export declare function generate<const Model extends LanguageModel>(input: RequestInput<Model> | LLMRequest, options?: StreamOptions): Effect.Effect<LLMResponse, AIError, Service>;
45
- /** Stream one model call. With `schema`, the `finish` event carries the decoded `object`. */
46
- export declare function stream<const Model extends LanguageModel, S extends OutputSchema>(input: ObjectInput<S, Model>, options?: StreamOptions): Stream.Stream<ObjectEvent<OutputType<S>>, AIError, Service>;
47
- export declare function stream<const Model extends LanguageModel>(input: RequestInput<Model> | LLMRequest, options?: StreamOptions): Stream.Stream<LLMEvent, AIError, Service>;
48
- export type LoopInput<SelectedLanguageModel extends LanguageModel = LanguageModel> = Omit<RequestFields<SelectedLanguageModel>, "tools"> & PromptOrMessages & {
47
+ export declare function generate<const Model extends LanguageModel, S extends OutputSchema>(input: LLMRequestInput<Model> & {
48
+ readonly schema: S;
49
+ }, options?: StreamOptions): Effect.Effect<ObjectResponse<OutputType<S>>, AIError, Service>;
50
+ export declare function generate<const Model extends LanguageModel>(input: LLMRequestInput<Model>, options?: StreamOptions): Effect.Effect<LLMResponse, AIError, Service>;
51
+ /**
52
+ * Stream one model call. The `finish` event carries the assistant `message` and `usage`; with `schema`, also the
53
+ * decoded `object`.
54
+ */
55
+ export declare function stream<const Model extends LanguageModel, S extends OutputSchema>(input: LLMRequestInput<Model> & {
56
+ readonly schema: S;
57
+ }, options?: StreamOptions): Stream.Stream<ObjectEvent<OutputType<S>>, AIError, Service>;
58
+ export declare function stream<const Model extends LanguageModel>(input: LLMRequestInput<Model>, options?: StreamOptions): Stream.Stream<LLMEvent, AIError, Service>;
59
+ export type LLMLoopInput<Model extends LanguageModel = LanguageModel> = Omit<TextRequestFields<Model>, "tools"> & PromptOrMessages & {
49
60
  readonly tools: ReadonlyArray<ExecutableTool>;
50
61
  /** Called after each step that has tool calls; returning `true` ends the loop without running them. */
51
62
  readonly until?: (state: {
@@ -53,14 +64,38 @@ export type LoopInput<SelectedLanguageModel extends LanguageModel = LanguageMode
53
64
  }) => boolean;
54
65
  };
55
66
  /**
56
- * Stream model calls, running the model's tool calls between them, until a step has no client-executed tool calls,
57
- * reports a provider error, or `until` returns `true`. Each step's `step-start` and `step-finish` carry its step index.
58
- * With `schema`, the model ends by calling the hidden answer tool, decoded into `object` on that step's `finish`.
67
+ * Stream model calls, running the model's tool calls between them, until a step has no client-executed tool calls or
68
+ * `until` returns `true`. Each step's `step-start` and `step-finish` carry its step index and usage; one `finish` ends
69
+ * the loop with the final step's reason and `message` and the usage summed across steps. With `schema`, the model ends
70
+ * by calling the hidden answer tool, decoded into `object` on the `finish`.
59
71
  */
60
- export declare function loop<const Model extends LanguageModel, S extends OutputSchema>(input: LoopInput<Model> & {
72
+ export declare function loop<const Model extends LanguageModel, S extends OutputSchema>(input: LLMLoopInput<Model> & {
61
73
  readonly schema: S;
62
74
  }, options?: StreamOptions): Stream.Stream<LoopObjectEvent<OutputType<S>>, AIError, Service>;
63
- export declare function loop<const Model extends LanguageModel>(input: LoopInput<Model>, options?: StreamOptions): Stream.Stream<LLMEvent, AIError, Service>;
64
- export declare const request: <const SelectedLanguageModel extends LanguageModel>(input: RequestInput<SelectedLanguageModel>) => LLMRequest<SelectedLanguageModel>;
65
- export { canCompact, compact } from "./route/client.js";
75
+ export declare function loop<const Model extends LanguageModel>(input: LLMLoopInput<Model>, options?: StreamOptions): Stream.Stream<LLMEvent, AIError, Service>;
76
+ /** The result of `collect`: every step of a stream from `stream` or `loop`, folded into one value. */
77
+ export interface Collected {
78
+ /** Text of the final step. */
79
+ readonly text: string;
80
+ /** One response per model call, in order. */
81
+ readonly steps: ReadonlyArray<LLMResponse>;
82
+ /** Each step's assistant message followed by its tool results, ready to append to the request's messages. */
83
+ readonly messages: ReadonlyArray<Message>;
84
+ /** Usage from the terminal `finish`, summed across steps for a loop. */
85
+ readonly usage?: Usage;
86
+ readonly finishReason: FinishReasonDetails;
87
+ }
88
+ type FinishOf<Event> = Extract<Event, {
89
+ readonly type: "finish";
90
+ }>;
91
+ /** `Collected`, plus `object` when the stream was started with `schema`. */
92
+ export type CollectResult<Event> = "object" extends keyof FinishOf<Event> ? Collected & Pick<FinishOf<Event>, "object"> : Collected;
93
+ /** Run a stream from `stream` or `loop` to the end and fold its events into the final result. */
94
+ export declare function collect<Event extends LLMEvent, E, R>(events: Stream.Stream<Event, E, R>): Effect.Effect<CollectResult<Event>, E | AIError, R>;
95
+ /** Normalize text request input into a plain `LLMRequest`, with `prompt` turned into the one user message. */
96
+ export declare const request: <const Model extends LanguageModel>(input: LLMRequestInput<Model>) => LLMRequest<Model>;
97
+ /** Compact a text request: a replacement window, or a checkpoint with `mechanism: "trigger"`. */
98
+ export declare function compact(input: CheckpointRequest, options: TriggerCompactOptions): Effect.Effect<CompactionCheckpointResponse, AIError, Service>;
99
+ export declare function compact(input: CompactionRequest, options?: EndpointCompactOptions): Effect.Effect<CompactionResponse, AIError, Service>;
100
+ export { canCompact };
66
101
  export * as LLM from "./llm.js";