@opencode/ai 2.0.15 → 2.0.17
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.
- package/README.md +441 -99
- package/dist/ai-client.d.ts +8 -0
- package/dist/ai-client.js +12 -0
- package/dist/experimental/evaluation-client.d.ts +3 -3
- package/dist/experimental/evaluation-client.js +1 -1
- package/dist/experimental/evaluation.js +1 -1
- package/dist/generation.d.ts +39 -29
- package/dist/generation.js +62 -36
- package/dist/image-client.d.ts +59 -17
- package/dist/image-client.js +16 -24
- package/dist/image.d.ts +394 -55
- package/dist/image.js +48 -57
- package/dist/index.d.ts +15 -2
- package/dist/index.js +10 -0
- package/dist/llm.d.ts +7 -5
- package/dist/llm.js +10 -4
- package/dist/media-client.d.ts +30 -0
- package/dist/media-client.js +51 -0
- package/dist/media-model.d.ts +43 -0
- package/dist/media-model.js +47 -0
- package/dist/media.d.ts +10 -9
- package/dist/media.js +11 -12
- package/dist/promise.d.ts +473 -15
- package/dist/promise.js +71 -13
- package/dist/protocols/alibaba-chat.d.ts +12 -0
- package/dist/protocols/alibaba-chat.js +4 -1
- package/dist/protocols/alibaba-messages.d.ts +1 -1
- package/dist/protocols/alibaba-messages.js +6 -4
- package/dist/protocols/alibaba-responses.d.ts +2 -2
- package/dist/protocols/anthropic-messages.d.ts +34 -34
- package/dist/protocols/anthropic-messages.js +14 -9
- package/dist/protocols/assemblyai-transcription.d.ts +41 -0
- package/dist/protocols/assemblyai-transcription.js +136 -0
- package/dist/protocols/bedrock-converse.d.ts +7 -0
- package/dist/protocols/bedrock-converse.js +39 -19
- package/dist/protocols/bfl-images.d.ts +38 -0
- package/dist/protocols/bfl-images.js +153 -0
- package/dist/protocols/cartesia-speech.d.ts +143 -0
- package/dist/protocols/cartesia-speech.js +120 -0
- package/dist/protocols/deepgram-speech.d.ts +135 -0
- package/dist/protocols/deepgram-speech.js +91 -0
- package/dist/protocols/deepgram-transcription.d.ts +26 -0
- package/dist/protocols/deepgram-transcription.js +125 -0
- package/dist/protocols/elevenlabs-speech.d.ts +138 -0
- package/dist/protocols/elevenlabs-speech.js +111 -0
- package/dist/protocols/fal-images.d.ts +25 -0
- package/dist/protocols/fal-images.js +104 -0
- package/dist/protocols/fal-video.d.ts +29 -0
- package/dist/protocols/fal-video.js +73 -0
- package/dist/protocols/gemini.d.ts +22 -22
- package/dist/protocols/gemini.js +19 -36
- package/dist/protocols/google-images.d.ts +3 -3
- package/dist/protocols/google-images.js +12 -34
- package/dist/protocols/google-speech.d.ts +146 -0
- package/dist/protocols/google-speech.js +88 -0
- package/dist/protocols/google-transcription.d.ts +174 -0
- package/dist/protocols/google-transcription.js +126 -0
- package/dist/protocols/google-video.d.ts +26 -0
- package/dist/protocols/google-video.js +142 -0
- package/dist/protocols/meta-images.d.ts +3 -4
- package/dist/protocols/meta-images.js +13 -29
- package/dist/protocols/meta-messages.d.ts +5 -5
- package/dist/protocols/meta-responses.d.ts +4 -4
- package/dist/protocols/meta-responses.js +6 -4
- package/dist/protocols/open-responses.d.ts +10 -9
- package/dist/protocols/open-responses.js +4 -9
- package/dist/protocols/openai-chat.d.ts +84 -0
- package/dist/protocols/openai-chat.js +28 -17
- package/dist/protocols/openai-compatible-chat.d.ts +12 -0
- package/dist/protocols/openai-compatible-responses.d.ts +2 -2
- package/dist/protocols/openai-images.d.ts +130 -7
- package/dist/protocols/openai-images.js +143 -91
- package/dist/protocols/openai-responses.d.ts +20 -20
- package/dist/protocols/openai-responses.js +31 -31
- package/dist/protocols/openai-speech.d.ts +136 -0
- package/dist/protocols/openai-speech.js +97 -0
- package/dist/protocols/openai-transcription.d.ts +211 -0
- package/dist/protocols/openai-transcription.js +202 -0
- package/dist/protocols/replicate-images.d.ts +28 -0
- package/dist/protocols/replicate-images.js +127 -0
- package/dist/protocols/runway-video.d.ts +38 -0
- package/dist/protocols/runway-video.js +140 -0
- package/dist/protocols/shared.d.ts +21 -17
- package/dist/protocols/shared.js +27 -36
- package/dist/protocols/stability-images.d.ts +39 -0
- package/dist/protocols/stability-images.js +136 -0
- package/dist/protocols/utils/fal-queue.d.ts +26 -0
- package/dist/protocols/utils/fal-queue.js +67 -0
- package/dist/protocols/utils/gemini-generate-content.d.ts +65 -0
- package/dist/protocols/utils/gemini-generate-content.js +65 -0
- package/dist/protocols/utils/gemini-json-schema.d.ts +3 -0
- package/dist/protocols/utils/gemini-json-schema.js +76 -0
- package/dist/protocols/utils/media-input.d.ts +23 -1
- package/dist/protocols/utils/media-input.js +40 -0
- package/dist/protocols/utils/responses-checkpoint.js +3 -7
- package/dist/protocols/utils/responses-compaction.d.ts +3 -1
- package/dist/protocols/utils/responses-compaction.js +16 -3
- package/dist/protocols/utils/speech-stream.d.ts +49 -0
- package/dist/protocols/utils/speech-stream.js +64 -0
- package/dist/protocols/utils/tool-schema.d.ts +2 -2
- package/dist/protocols/utils/tool-schema.js +62 -19
- package/dist/protocols/xai-images.d.ts +4 -4
- package/dist/protocols/xai-images.js +14 -40
- package/dist/protocols/xai-responses.d.ts +2 -2
- package/dist/protocols/xai-responses.js +1 -1
- package/dist/protocols/xai-video.d.ts +34 -0
- package/dist/protocols/xai-video.js +141 -0
- package/dist/protocols/zai-chat.d.ts +13 -1
- package/dist/protocols/zai-images.d.ts +2 -2
- package/dist/protocols/zai-images.js +11 -14
- package/dist/protocols/zai-messages.d.ts +1 -1
- package/dist/provider-error.js +10 -1
- package/dist/providers/alibaba.d.ts +15 -3
- package/dist/providers/amazon-bedrock-mantle.d.ts +14 -2
- package/dist/providers/amazon-bedrock.d.ts +2 -0
- package/dist/providers/amazon-bedrock.js +1 -0
- package/dist/providers/anthropic-compatible.d.ts +5 -5
- package/dist/providers/anthropic.d.ts +5 -5
- package/dist/providers/assemblyai.d.ts +25 -0
- package/dist/providers/assemblyai.js +24 -0
- package/dist/providers/azure.d.ts +20 -8
- package/dist/providers/azure.js +2 -2
- package/dist/providers/baseten.d.ts +24 -0
- package/dist/providers/black-forest-labs.d.ts +25 -0
- package/dist/providers/black-forest-labs.js +23 -0
- package/dist/providers/cartesia.d.ts +24 -0
- package/dist/providers/cartesia.js +17 -0
- package/dist/providers/cerebras.d.ts +24 -0
- package/dist/providers/cloudflare-ai-gateway.d.ts +42 -18
- package/dist/providers/cloudflare-workers-ai.d.ts +24 -0
- package/dist/providers/deepgram.d.ts +29 -0
- package/dist/providers/deepgram.js +25 -0
- package/dist/providers/deepinfra.d.ts +24 -0
- package/dist/providers/deepseek.d.ts +24 -0
- package/dist/providers/elevenlabs.d.ts +24 -0
- package/dist/providers/elevenlabs.js +23 -0
- package/dist/providers/fal.d.ts +29 -0
- package/dist/providers/fal.js +26 -0
- package/dist/providers/fireworks.d.ts +24 -0
- package/dist/providers/google-vertex-chat.d.ts +12 -0
- package/dist/providers/google-vertex-messages.d.ts +5 -5
- package/dist/providers/google-vertex-responses.d.ts +2 -2
- package/dist/providers/google-vertex.d.ts +6 -6
- package/dist/providers/google.d.ts +21 -6
- package/dist/providers/google.js +13 -9
- package/dist/providers/groq.d.ts +24 -0
- package/dist/providers/index.d.ts +9 -0
- package/dist/providers/index.js +9 -0
- package/dist/providers/meta.d.ts +22 -10
- package/dist/providers/meta.js +4 -8
- package/dist/providers/minimax.d.ts +19 -7
- package/dist/providers/moonshot.d.ts +19 -7
- package/dist/providers/moonshot.js +3 -3
- package/dist/providers/openai-compatible-responses.d.ts +2 -2
- package/dist/providers/openai-compatible.d.ts +12 -0
- package/dist/providers/openai-options.d.ts +3 -9
- package/dist/providers/openai-options.js +4 -7
- package/dist/providers/openai.d.ts +33 -12
- package/dist/providers/openai.js +17 -9
- package/dist/providers/opencode-zen.js +1 -1
- package/dist/providers/openrouter.d.ts +54 -7
- package/dist/providers/openrouter.js +10 -5
- package/dist/providers/replicate.d.ts +25 -0
- package/dist/providers/replicate.js +17 -0
- package/dist/providers/runway.d.ts +24 -0
- package/dist/providers/runway.js +17 -0
- package/dist/providers/stability.d.ts +28 -0
- package/dist/providers/stability.js +18 -0
- package/dist/providers/togetherai.d.ts +24 -0
- package/dist/providers/typesafe-ai.js +1 -1
- package/dist/providers/vercel-ai-gateway.js +1 -1
- package/dist/providers/xai.d.ts +17 -0
- package/dist/providers/xai.js +7 -9
- package/dist/providers/zai-coding-plan.d.ts +16 -4
- package/dist/providers/zai.d.ts +13 -1
- package/dist/providers/zai.js +4 -8
- package/dist/route/auth.d.ts +5 -2
- package/dist/route/auth.js +20 -13
- package/dist/route/client.d.ts +9 -7
- package/dist/route/client.js +6 -8
- package/dist/route/endpoint.d.ts +1 -0
- package/dist/route/endpoint.js +2 -2
- package/dist/route/executor-service.d.ts +4 -2
- package/dist/route/executor-service.js +2 -1
- package/dist/route/executor.d.ts +3 -1
- package/dist/route/executor.js +7 -0
- package/dist/route/framing.d.ts +15 -2
- package/dist/route/framing.js +53 -4
- package/dist/route/index.d.ts +1 -1
- package/dist/route/media-protocol.d.ts +145 -18
- package/dist/route/media-protocol.js +96 -28
- package/dist/route/media.d.ts +54 -9
- package/dist/route/media.js +194 -38
- package/dist/route/protocol.d.ts +3 -1
- package/dist/schema/events.d.ts +0 -6
- package/dist/schema/messages.d.ts +0 -3
- package/dist/schema/options.d.ts +10 -6
- package/dist/schema/options.js +10 -5
- package/dist/speech-client.d.ts +66 -0
- package/dist/speech-client.js +21 -0
- package/dist/speech.d.ts +1307 -0
- package/dist/speech.js +124 -0
- package/dist/testing.d.ts +2 -2
- package/dist/transcription-client.d.ts +82 -0
- package/dist/transcription-client.js +13 -0
- package/dist/transcription.d.ts +1492 -0
- package/dist/transcription.js +127 -0
- package/dist/utils/bytes.d.ts +1 -0
- package/dist/utils/bytes.js +10 -0
- package/dist/utils/json.d.ts +4 -0
- package/dist/utils/json.js +4 -0
- package/dist/utils/media-type.d.ts +3 -1
- package/dist/utils/media-type.js +25 -2
- package/dist/video-client.d.ts +59 -0
- package/dist/video-client.js +20 -0
- package/dist/video.d.ts +1351 -0
- package/dist/video.js +118 -0
- package/package.json +3 -3
- package/dist/protocols/utils/gemini-tool-schema.d.ts +0 -2
- package/dist/protocols/utils/gemini-tool-schema.js +0 -103
- package/dist/protocols/utils/meta-image.d.ts +0 -2
- package/dist/protocols/utils/meta-image.js +0 -13
- package/dist/protocols/utils/openai-image.d.ts +0 -5
- package/dist/protocols/utils/openai-image.js +0 -18
package/dist/promise.d.ts
CHANGED
|
@@ -1,24 +1,53 @@
|
|
|
1
1
|
import { Effect, Layer } from "effect";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
2
|
+
import { AIClient } from "./ai-client.js";
|
|
3
|
+
import type { AwaitOptions, Event, Snapshot } from "./generation.js";
|
|
4
|
+
import { type ImageModel, type ImageRequest, type ImageRequestInput } from "./image.js";
|
|
5
|
+
import { Media } from "./media.js";
|
|
5
6
|
import { RequestExecutor } from "./route/executor.js";
|
|
6
7
|
import { LanguageModel, LLMRequest } from "./schema/index.js";
|
|
7
8
|
import type { RequestInput } from "./llm.js";
|
|
9
|
+
import { type SpeechModel, type SpeechRequest, type SpeechRequestInput } from "./speech.js";
|
|
10
|
+
import { type TranscriptionModel, type TranscriptionRequest, type TranscriptionRequestInput } from "./transcription.js";
|
|
11
|
+
import { type VideoModel, type VideoRequest, type VideoRequestInput } from "./video.js";
|
|
8
12
|
/**
|
|
9
|
-
* Promise-first entrypoint for scripts and non-Effect callers. One `ManagedRuntime` hosts the LLM
|
|
10
|
-
* over a request executor; every method runs the corresponding Effect API and rethrows
|
|
13
|
+
* Promise-first entrypoint for scripts and non-Effect callers. One `ManagedRuntime` hosts the LLM, image, video, speech,
|
|
14
|
+
* and transcription clients over a request executor; every method runs the corresponding Effect API and rethrows
|
|
15
|
+
* `AIError` unchanged. `file` and `write` load `node:fs/promises` on first use, so importing this module does not.
|
|
11
16
|
*/
|
|
12
17
|
export interface Options {
|
|
13
|
-
/** Executor layer; defaults to `RequestExecutor.fetchLayer`. Inject a recorder or middleware here. */
|
|
18
|
+
/** Executor layer; defaults to `RequestExecutor.fetchLayer`. Inject a recorder or `RequestExecutor.middleware(fn)` here. */
|
|
14
19
|
readonly layer?: Layer.Layer<RequestExecutor.Service>;
|
|
15
20
|
}
|
|
16
21
|
export interface RunOptions {
|
|
17
22
|
readonly signal?: AbortSignal;
|
|
18
23
|
}
|
|
19
|
-
export type Services =
|
|
24
|
+
export type Services = AIClient.Services;
|
|
25
|
+
/**
|
|
26
|
+
* Promise view of a `Generation`. Its fields are a snapshot taken when the handle was created; `refresh()` resolves to a
|
|
27
|
+
* new handle rather than updating this one.
|
|
28
|
+
*/
|
|
29
|
+
export type GenerationHandle<Response> = Snapshot & {
|
|
30
|
+
/** Serializable JSON; pass it back to `resume` from another process. */
|
|
31
|
+
readonly token: unknown;
|
|
32
|
+
readonly await: (options?: AwaitOptions & RunOptions) => Promise<Response>;
|
|
33
|
+
/** Status observations until the first terminal one, polling like `await`; abort ends iteration without throwing. */
|
|
34
|
+
readonly events: (options?: AwaitOptions & RunOptions) => AsyncIterable<Event>;
|
|
35
|
+
/** The result without polling; fails when the generation has not completed. */
|
|
36
|
+
readonly result: (options?: RunOptions) => Promise<Response>;
|
|
37
|
+
readonly refresh: (options?: RunOptions) => Promise<GenerationHandle<Response>>;
|
|
38
|
+
readonly cancel: (options?: RunOptions) => Promise<void>;
|
|
39
|
+
};
|
|
20
40
|
export declare const make: (options?: Options) => {
|
|
21
41
|
run: <A, E>(effect: Effect.Effect<A, E, Services>, options?: RunOptions) => Promise<A>;
|
|
42
|
+
/** Decoded asset bytes, downloading `url` sources through the executor. */
|
|
43
|
+
bytes: (asset: Media.Asset, options?: RunOptions) => Promise<Uint8Array<ArrayBufferLike>>;
|
|
44
|
+
base64: (asset: Media.Asset, options?: RunOptions) => Promise<string>;
|
|
45
|
+
/** Pull a `url` asset into owned bytes before the provider URL expires. */
|
|
46
|
+
materialize: (asset: Media.Asset, options?: RunOptions) => Promise<Media.Asset>;
|
|
47
|
+
/** Read a file into an asset like `Media.file`: sniffed media type, then the extension's. */
|
|
48
|
+
file: (path: string, options?: Media.AssetOptions & RunOptions) => Promise<Media.Asset>;
|
|
49
|
+
/** Write an asset's bytes like `Media.write`, downloading `url` sources through the executor. */
|
|
50
|
+
write: (asset: Media.Asset, path: string, options?: RunOptions) => Promise<void>;
|
|
22
51
|
llm: {
|
|
23
52
|
request: <const SelectedLanguageModel extends LanguageModel>(input: RequestInput<SelectedLanguageModel>) => LLMRequest<SelectedLanguageModel>;
|
|
24
53
|
generate: <const Model extends LanguageModel>(input: RequestInput<Model> | LLMRequest, options?: RunOptions) => Promise<import("./index.js").LLMResponse>;
|
|
@@ -193,7 +222,7 @@ export declare const make: (options?: Options) => {
|
|
|
193
222
|
readonly namespace?: string | undefined;
|
|
194
223
|
} | {
|
|
195
224
|
readonly type: "media";
|
|
196
|
-
readonly media:
|
|
225
|
+
readonly media: Media.Asset;
|
|
197
226
|
readonly providerMetadata?: {
|
|
198
227
|
readonly [x: string]: {
|
|
199
228
|
readonly [x: string]: unknown;
|
|
@@ -237,13 +266,144 @@ export declare const make: (options?: Options) => {
|
|
|
237
266
|
};
|
|
238
267
|
image: {
|
|
239
268
|
request: typeof import("./image.js").request;
|
|
240
|
-
generate: <const Model extends ImageModel>(input: ImageRequestInput<Model> | ImageRequest, options?: RunOptions) => Promise<import("./image.js").ImageResponse>;
|
|
241
|
-
stream: <const Model extends ImageModel>(input: ImageRequestInput<Model> | ImageRequest, options?: RunOptions) => AsyncIterable<{
|
|
269
|
+
generate: <const Model extends ImageModel>(input: ImageRequestInput<Model> | ImageRequest, options?: AwaitOptions & RunOptions) => Promise<import("./image.js").ImageResponse>;
|
|
270
|
+
stream: <const Model extends ImageModel>(input: ImageRequestInput<Model> | ImageRequest, options?: AwaitOptions & RunOptions) => AsyncIterable<{
|
|
271
|
+
readonly id: string;
|
|
272
|
+
readonly type: "generation-queued";
|
|
273
|
+
readonly position?: number | undefined;
|
|
274
|
+
} | {
|
|
275
|
+
readonly id: string;
|
|
276
|
+
readonly type: "generation-progress";
|
|
277
|
+
readonly progress?: number | undefined;
|
|
278
|
+
} | {
|
|
279
|
+
readonly type: "image-partial";
|
|
280
|
+
readonly index: number;
|
|
281
|
+
readonly image: Media.Asset;
|
|
282
|
+
} | {
|
|
242
283
|
readonly type: "image";
|
|
243
284
|
readonly index: number;
|
|
244
|
-
readonly image:
|
|
285
|
+
readonly image: Media.Asset;
|
|
286
|
+
} | {
|
|
287
|
+
readonly type: "finish";
|
|
288
|
+
readonly providerMetadata?: {
|
|
289
|
+
readonly [x: string]: {
|
|
290
|
+
readonly [x: string]: unknown;
|
|
291
|
+
};
|
|
292
|
+
} | undefined;
|
|
293
|
+
readonly usage?: {
|
|
294
|
+
readonly type: "tokens";
|
|
295
|
+
readonly input?: number | undefined;
|
|
296
|
+
readonly output?: number | undefined;
|
|
297
|
+
readonly total?: number | undefined;
|
|
298
|
+
readonly details?: {
|
|
299
|
+
readonly [x: string]: unknown;
|
|
300
|
+
} | undefined;
|
|
301
|
+
} | {
|
|
302
|
+
readonly type: "seconds";
|
|
303
|
+
readonly seconds: number;
|
|
304
|
+
} | {
|
|
305
|
+
readonly type: "characters";
|
|
306
|
+
readonly characters: number;
|
|
307
|
+
} | {
|
|
308
|
+
readonly type: "credits";
|
|
309
|
+
readonly credits: number;
|
|
310
|
+
} | {
|
|
311
|
+
readonly type: "compute";
|
|
312
|
+
readonly seconds: number;
|
|
313
|
+
} | undefined;
|
|
314
|
+
readonly notices?: readonly {
|
|
315
|
+
readonly type: "other" | "moderated" | "filtered";
|
|
316
|
+
readonly message: string;
|
|
317
|
+
readonly providerMetadata?: {
|
|
318
|
+
readonly [x: string]: {
|
|
319
|
+
readonly [x: string]: unknown;
|
|
320
|
+
};
|
|
321
|
+
} | undefined;
|
|
322
|
+
}[] | undefined;
|
|
323
|
+
}>;
|
|
324
|
+
start: <const Model extends ImageModel>(input: ImageRequestInput<Model> | ImageRequest, options?: RunOptions) => Promise<GenerationHandle<import("./image.js").ImageResponse>>;
|
|
325
|
+
resume: (model: ImageModel, token: unknown, options?: RunOptions) => Promise<GenerationHandle<import("./image.js").ImageResponse>>;
|
|
326
|
+
};
|
|
327
|
+
video: {
|
|
328
|
+
request: typeof import("./video.js").request;
|
|
329
|
+
start: <const Model extends VideoModel>(input: VideoRequestInput<Model> | VideoRequest, options?: RunOptions) => Promise<GenerationHandle<import("./video.js").VideoResponse>>;
|
|
330
|
+
generate: <const Model extends VideoModel>(input: VideoRequestInput<Model> | VideoRequest, options?: AwaitOptions & RunOptions) => Promise<import("./video.js").VideoResponse>;
|
|
331
|
+
resume: (model: VideoModel, token: unknown, options?: RunOptions) => Promise<GenerationHandle<import("./video.js").VideoResponse>>;
|
|
332
|
+
stream: <const Model extends VideoModel>(input: VideoRequestInput<Model> | VideoRequest, options?: AwaitOptions & RunOptions) => AsyncIterable<{
|
|
333
|
+
readonly id: string;
|
|
334
|
+
readonly type: "generation-queued";
|
|
335
|
+
readonly position?: number | undefined;
|
|
336
|
+
} | {
|
|
337
|
+
readonly id: string;
|
|
338
|
+
readonly type: "generation-progress";
|
|
339
|
+
readonly progress?: number | undefined;
|
|
340
|
+
} | {
|
|
341
|
+
readonly type: "video";
|
|
342
|
+
readonly index: number;
|
|
343
|
+
readonly video: Media.Asset;
|
|
344
|
+
} | {
|
|
345
|
+
readonly type: "finish";
|
|
346
|
+
readonly providerMetadata?: {
|
|
347
|
+
readonly [x: string]: {
|
|
348
|
+
readonly [x: string]: unknown;
|
|
349
|
+
};
|
|
350
|
+
} | undefined;
|
|
351
|
+
readonly usage?: {
|
|
352
|
+
readonly type: "tokens";
|
|
353
|
+
readonly input?: number | undefined;
|
|
354
|
+
readonly output?: number | undefined;
|
|
355
|
+
readonly total?: number | undefined;
|
|
356
|
+
readonly details?: {
|
|
357
|
+
readonly [x: string]: unknown;
|
|
358
|
+
} | undefined;
|
|
359
|
+
} | {
|
|
360
|
+
readonly type: "seconds";
|
|
361
|
+
readonly seconds: number;
|
|
362
|
+
} | {
|
|
363
|
+
readonly type: "characters";
|
|
364
|
+
readonly characters: number;
|
|
365
|
+
} | {
|
|
366
|
+
readonly type: "credits";
|
|
367
|
+
readonly credits: number;
|
|
368
|
+
} | {
|
|
369
|
+
readonly type: "compute";
|
|
370
|
+
readonly seconds: number;
|
|
371
|
+
} | undefined;
|
|
372
|
+
readonly notices?: readonly {
|
|
373
|
+
readonly type: "other" | "moderated" | "filtered";
|
|
374
|
+
readonly message: string;
|
|
375
|
+
readonly providerMetadata?: {
|
|
376
|
+
readonly [x: string]: {
|
|
377
|
+
readonly [x: string]: unknown;
|
|
378
|
+
};
|
|
379
|
+
} | undefined;
|
|
380
|
+
}[] | undefined;
|
|
381
|
+
}>;
|
|
382
|
+
};
|
|
383
|
+
speech: {
|
|
384
|
+
request: typeof import("./speech.js").request;
|
|
385
|
+
generate: <const Model extends SpeechModel>(input: SpeechRequestInput<Model> | SpeechRequest, options?: RunOptions) => Promise<import("./speech.js").SpeechResponse>;
|
|
386
|
+
stream: <const Model extends SpeechModel>(input: SpeechRequestInput<Model> | SpeechRequest, options?: RunOptions) => AsyncIterable<{
|
|
387
|
+
readonly id: string;
|
|
388
|
+
readonly type: "generation-queued";
|
|
389
|
+
readonly position?: number | undefined;
|
|
390
|
+
} | {
|
|
391
|
+
readonly id: string;
|
|
392
|
+
readonly type: "generation-progress";
|
|
393
|
+
readonly progress?: number | undefined;
|
|
394
|
+
} | {
|
|
395
|
+
readonly type: "audio-delta";
|
|
396
|
+
readonly chunk: Uint8Array<ArrayBufferLike>;
|
|
397
|
+
} | {
|
|
398
|
+
readonly type: "timestamps";
|
|
399
|
+
readonly items: readonly {
|
|
400
|
+
readonly text: string;
|
|
401
|
+
readonly startSeconds: number;
|
|
402
|
+
readonly endSeconds: number;
|
|
403
|
+
}[];
|
|
245
404
|
} | {
|
|
246
405
|
readonly type: "finish";
|
|
406
|
+
readonly audio: Media.Asset;
|
|
247
407
|
readonly providerMetadata?: {
|
|
248
408
|
readonly [x: string]: {
|
|
249
409
|
readonly [x: string]: unknown;
|
|
@@ -281,12 +441,100 @@ export declare const make: (options?: Options) => {
|
|
|
281
441
|
}[] | undefined;
|
|
282
442
|
}>;
|
|
283
443
|
};
|
|
444
|
+
transcription: {
|
|
445
|
+
request: typeof import("./transcription.js").request;
|
|
446
|
+
generate: <const Model extends TranscriptionModel>(input: TranscriptionRequestInput<Model> | TranscriptionRequest, options?: AwaitOptions & RunOptions) => Promise<import("./transcription.js").TranscriptionResponse>;
|
|
447
|
+
stream: <const Model extends TranscriptionModel>(input: TranscriptionRequestInput<Model> | TranscriptionRequest, options?: AwaitOptions & RunOptions) => AsyncIterable<{
|
|
448
|
+
readonly id: string;
|
|
449
|
+
readonly type: "generation-queued";
|
|
450
|
+
readonly position?: number | undefined;
|
|
451
|
+
} | {
|
|
452
|
+
readonly id: string;
|
|
453
|
+
readonly type: "generation-progress";
|
|
454
|
+
readonly progress?: number | undefined;
|
|
455
|
+
} | {
|
|
456
|
+
readonly type: "text-delta";
|
|
457
|
+
readonly delta: string;
|
|
458
|
+
} | {
|
|
459
|
+
readonly type: "segment";
|
|
460
|
+
readonly segment: {
|
|
461
|
+
readonly text: string;
|
|
462
|
+
readonly startSeconds: number;
|
|
463
|
+
readonly endSeconds: number;
|
|
464
|
+
readonly speaker?: string | undefined;
|
|
465
|
+
};
|
|
466
|
+
} | {
|
|
467
|
+
readonly type: "finish";
|
|
468
|
+
readonly text: string;
|
|
469
|
+
readonly durationSeconds?: number | undefined;
|
|
470
|
+
readonly providerMetadata?: {
|
|
471
|
+
readonly [x: string]: {
|
|
472
|
+
readonly [x: string]: unknown;
|
|
473
|
+
};
|
|
474
|
+
} | undefined;
|
|
475
|
+
readonly usage?: {
|
|
476
|
+
readonly type: "tokens";
|
|
477
|
+
readonly input?: number | undefined;
|
|
478
|
+
readonly output?: number | undefined;
|
|
479
|
+
readonly total?: number | undefined;
|
|
480
|
+
readonly details?: {
|
|
481
|
+
readonly [x: string]: unknown;
|
|
482
|
+
} | undefined;
|
|
483
|
+
} | {
|
|
484
|
+
readonly type: "seconds";
|
|
485
|
+
readonly seconds: number;
|
|
486
|
+
} | {
|
|
487
|
+
readonly type: "characters";
|
|
488
|
+
readonly characters: number;
|
|
489
|
+
} | {
|
|
490
|
+
readonly type: "credits";
|
|
491
|
+
readonly credits: number;
|
|
492
|
+
} | {
|
|
493
|
+
readonly type: "compute";
|
|
494
|
+
readonly seconds: number;
|
|
495
|
+
} | undefined;
|
|
496
|
+
readonly notices?: readonly {
|
|
497
|
+
readonly type: "other" | "moderated" | "filtered";
|
|
498
|
+
readonly message: string;
|
|
499
|
+
readonly providerMetadata?: {
|
|
500
|
+
readonly [x: string]: {
|
|
501
|
+
readonly [x: string]: unknown;
|
|
502
|
+
};
|
|
503
|
+
} | undefined;
|
|
504
|
+
}[] | undefined;
|
|
505
|
+
readonly language?: string | undefined;
|
|
506
|
+
readonly segments?: readonly {
|
|
507
|
+
readonly text: string;
|
|
508
|
+
readonly startSeconds: number;
|
|
509
|
+
readonly endSeconds: number;
|
|
510
|
+
readonly speaker?: string | undefined;
|
|
511
|
+
}[] | undefined;
|
|
512
|
+
readonly words?: readonly {
|
|
513
|
+
readonly text: string;
|
|
514
|
+
readonly startSeconds: number;
|
|
515
|
+
readonly endSeconds: number;
|
|
516
|
+
readonly speaker?: string | undefined;
|
|
517
|
+
readonly confidence?: number | undefined;
|
|
518
|
+
}[] | undefined;
|
|
519
|
+
}>;
|
|
520
|
+
start: <const Model extends TranscriptionModel>(input: TranscriptionRequestInput<Model> | TranscriptionRequest, options?: RunOptions) => Promise<GenerationHandle<import("./transcription.js").TranscriptionResponse>>;
|
|
521
|
+
resume: (model: TranscriptionModel, token: unknown, options?: RunOptions) => Promise<GenerationHandle<import("./transcription.js").TranscriptionResponse>>;
|
|
522
|
+
};
|
|
284
523
|
dispose: () => Promise<void>;
|
|
285
524
|
};
|
|
286
525
|
export type Client = ReturnType<typeof make>;
|
|
287
526
|
/** Default client over `RequestExecutor.fetchLayer` for scripts; the runtime builds its layer on first use. */
|
|
288
527
|
export declare const ai: {
|
|
289
528
|
run: <A, E>(effect: Effect.Effect<A, E, Services>, options?: RunOptions) => Promise<A>;
|
|
529
|
+
/** Decoded asset bytes, downloading `url` sources through the executor. */
|
|
530
|
+
bytes: (asset: Media.Asset, options?: RunOptions) => Promise<Uint8Array<ArrayBufferLike>>;
|
|
531
|
+
base64: (asset: Media.Asset, options?: RunOptions) => Promise<string>;
|
|
532
|
+
/** Pull a `url` asset into owned bytes before the provider URL expires. */
|
|
533
|
+
materialize: (asset: Media.Asset, options?: RunOptions) => Promise<Media.Asset>;
|
|
534
|
+
/** Read a file into an asset like `Media.file`: sniffed media type, then the extension's. */
|
|
535
|
+
file: (path: string, options?: Media.AssetOptions & RunOptions) => Promise<Media.Asset>;
|
|
536
|
+
/** Write an asset's bytes like `Media.write`, downloading `url` sources through the executor. */
|
|
537
|
+
write: (asset: Media.Asset, path: string, options?: RunOptions) => Promise<void>;
|
|
290
538
|
llm: {
|
|
291
539
|
request: <const SelectedLanguageModel extends LanguageModel>(input: RequestInput<SelectedLanguageModel>) => LLMRequest<SelectedLanguageModel>;
|
|
292
540
|
generate: <const Model extends LanguageModel>(input: RequestInput<Model> | LLMRequest, options?: RunOptions) => Promise<import("./index.js").LLMResponse>;
|
|
@@ -461,7 +709,7 @@ export declare const ai: {
|
|
|
461
709
|
readonly namespace?: string | undefined;
|
|
462
710
|
} | {
|
|
463
711
|
readonly type: "media";
|
|
464
|
-
readonly media:
|
|
712
|
+
readonly media: Media.Asset;
|
|
465
713
|
readonly providerMetadata?: {
|
|
466
714
|
readonly [x: string]: {
|
|
467
715
|
readonly [x: string]: unknown;
|
|
@@ -505,11 +753,81 @@ export declare const ai: {
|
|
|
505
753
|
};
|
|
506
754
|
image: {
|
|
507
755
|
request: typeof import("./image.js").request;
|
|
508
|
-
generate: <const Model extends ImageModel>(input: ImageRequestInput<Model> | ImageRequest, options?: RunOptions) => Promise<import("./image.js").ImageResponse>;
|
|
509
|
-
stream: <const Model extends ImageModel>(input: ImageRequestInput<Model> | ImageRequest, options?: RunOptions) => AsyncIterable<{
|
|
756
|
+
generate: <const Model extends ImageModel>(input: ImageRequestInput<Model> | ImageRequest, options?: AwaitOptions & RunOptions) => Promise<import("./image.js").ImageResponse>;
|
|
757
|
+
stream: <const Model extends ImageModel>(input: ImageRequestInput<Model> | ImageRequest, options?: AwaitOptions & RunOptions) => AsyncIterable<{
|
|
758
|
+
readonly id: string;
|
|
759
|
+
readonly type: "generation-queued";
|
|
760
|
+
readonly position?: number | undefined;
|
|
761
|
+
} | {
|
|
762
|
+
readonly id: string;
|
|
763
|
+
readonly type: "generation-progress";
|
|
764
|
+
readonly progress?: number | undefined;
|
|
765
|
+
} | {
|
|
766
|
+
readonly type: "image-partial";
|
|
767
|
+
readonly index: number;
|
|
768
|
+
readonly image: Media.Asset;
|
|
769
|
+
} | {
|
|
510
770
|
readonly type: "image";
|
|
511
771
|
readonly index: number;
|
|
512
|
-
readonly image:
|
|
772
|
+
readonly image: Media.Asset;
|
|
773
|
+
} | {
|
|
774
|
+
readonly type: "finish";
|
|
775
|
+
readonly providerMetadata?: {
|
|
776
|
+
readonly [x: string]: {
|
|
777
|
+
readonly [x: string]: unknown;
|
|
778
|
+
};
|
|
779
|
+
} | undefined;
|
|
780
|
+
readonly usage?: {
|
|
781
|
+
readonly type: "tokens";
|
|
782
|
+
readonly input?: number | undefined;
|
|
783
|
+
readonly output?: number | undefined;
|
|
784
|
+
readonly total?: number | undefined;
|
|
785
|
+
readonly details?: {
|
|
786
|
+
readonly [x: string]: unknown;
|
|
787
|
+
} | undefined;
|
|
788
|
+
} | {
|
|
789
|
+
readonly type: "seconds";
|
|
790
|
+
readonly seconds: number;
|
|
791
|
+
} | {
|
|
792
|
+
readonly type: "characters";
|
|
793
|
+
readonly characters: number;
|
|
794
|
+
} | {
|
|
795
|
+
readonly type: "credits";
|
|
796
|
+
readonly credits: number;
|
|
797
|
+
} | {
|
|
798
|
+
readonly type: "compute";
|
|
799
|
+
readonly seconds: number;
|
|
800
|
+
} | undefined;
|
|
801
|
+
readonly notices?: readonly {
|
|
802
|
+
readonly type: "other" | "moderated" | "filtered";
|
|
803
|
+
readonly message: string;
|
|
804
|
+
readonly providerMetadata?: {
|
|
805
|
+
readonly [x: string]: {
|
|
806
|
+
readonly [x: string]: unknown;
|
|
807
|
+
};
|
|
808
|
+
} | undefined;
|
|
809
|
+
}[] | undefined;
|
|
810
|
+
}>;
|
|
811
|
+
start: <const Model extends ImageModel>(input: ImageRequestInput<Model> | ImageRequest, options?: RunOptions) => Promise<GenerationHandle<import("./image.js").ImageResponse>>;
|
|
812
|
+
resume: (model: ImageModel, token: unknown, options?: RunOptions) => Promise<GenerationHandle<import("./image.js").ImageResponse>>;
|
|
813
|
+
};
|
|
814
|
+
video: {
|
|
815
|
+
request: typeof import("./video.js").request;
|
|
816
|
+
start: <const Model extends VideoModel>(input: VideoRequestInput<Model> | VideoRequest, options?: RunOptions) => Promise<GenerationHandle<import("./video.js").VideoResponse>>;
|
|
817
|
+
generate: <const Model extends VideoModel>(input: VideoRequestInput<Model> | VideoRequest, options?: AwaitOptions & RunOptions) => Promise<import("./video.js").VideoResponse>;
|
|
818
|
+
resume: (model: VideoModel, token: unknown, options?: RunOptions) => Promise<GenerationHandle<import("./video.js").VideoResponse>>;
|
|
819
|
+
stream: <const Model extends VideoModel>(input: VideoRequestInput<Model> | VideoRequest, options?: AwaitOptions & RunOptions) => AsyncIterable<{
|
|
820
|
+
readonly id: string;
|
|
821
|
+
readonly type: "generation-queued";
|
|
822
|
+
readonly position?: number | undefined;
|
|
823
|
+
} | {
|
|
824
|
+
readonly id: string;
|
|
825
|
+
readonly type: "generation-progress";
|
|
826
|
+
readonly progress?: number | undefined;
|
|
827
|
+
} | {
|
|
828
|
+
readonly type: "video";
|
|
829
|
+
readonly index: number;
|
|
830
|
+
readonly video: Media.Asset;
|
|
513
831
|
} | {
|
|
514
832
|
readonly type: "finish";
|
|
515
833
|
readonly providerMetadata?: {
|
|
@@ -549,6 +867,146 @@ export declare const ai: {
|
|
|
549
867
|
}[] | undefined;
|
|
550
868
|
}>;
|
|
551
869
|
};
|
|
870
|
+
speech: {
|
|
871
|
+
request: typeof import("./speech.js").request;
|
|
872
|
+
generate: <const Model extends SpeechModel>(input: SpeechRequestInput<Model> | SpeechRequest, options?: RunOptions) => Promise<import("./speech.js").SpeechResponse>;
|
|
873
|
+
stream: <const Model extends SpeechModel>(input: SpeechRequestInput<Model> | SpeechRequest, options?: RunOptions) => AsyncIterable<{
|
|
874
|
+
readonly id: string;
|
|
875
|
+
readonly type: "generation-queued";
|
|
876
|
+
readonly position?: number | undefined;
|
|
877
|
+
} | {
|
|
878
|
+
readonly id: string;
|
|
879
|
+
readonly type: "generation-progress";
|
|
880
|
+
readonly progress?: number | undefined;
|
|
881
|
+
} | {
|
|
882
|
+
readonly type: "audio-delta";
|
|
883
|
+
readonly chunk: Uint8Array<ArrayBufferLike>;
|
|
884
|
+
} | {
|
|
885
|
+
readonly type: "timestamps";
|
|
886
|
+
readonly items: readonly {
|
|
887
|
+
readonly text: string;
|
|
888
|
+
readonly startSeconds: number;
|
|
889
|
+
readonly endSeconds: number;
|
|
890
|
+
}[];
|
|
891
|
+
} | {
|
|
892
|
+
readonly type: "finish";
|
|
893
|
+
readonly audio: Media.Asset;
|
|
894
|
+
readonly providerMetadata?: {
|
|
895
|
+
readonly [x: string]: {
|
|
896
|
+
readonly [x: string]: unknown;
|
|
897
|
+
};
|
|
898
|
+
} | undefined;
|
|
899
|
+
readonly usage?: {
|
|
900
|
+
readonly type: "tokens";
|
|
901
|
+
readonly input?: number | undefined;
|
|
902
|
+
readonly output?: number | undefined;
|
|
903
|
+
readonly total?: number | undefined;
|
|
904
|
+
readonly details?: {
|
|
905
|
+
readonly [x: string]: unknown;
|
|
906
|
+
} | undefined;
|
|
907
|
+
} | {
|
|
908
|
+
readonly type: "seconds";
|
|
909
|
+
readonly seconds: number;
|
|
910
|
+
} | {
|
|
911
|
+
readonly type: "characters";
|
|
912
|
+
readonly characters: number;
|
|
913
|
+
} | {
|
|
914
|
+
readonly type: "credits";
|
|
915
|
+
readonly credits: number;
|
|
916
|
+
} | {
|
|
917
|
+
readonly type: "compute";
|
|
918
|
+
readonly seconds: number;
|
|
919
|
+
} | undefined;
|
|
920
|
+
readonly notices?: readonly {
|
|
921
|
+
readonly type: "other" | "moderated" | "filtered";
|
|
922
|
+
readonly message: string;
|
|
923
|
+
readonly providerMetadata?: {
|
|
924
|
+
readonly [x: string]: {
|
|
925
|
+
readonly [x: string]: unknown;
|
|
926
|
+
};
|
|
927
|
+
} | undefined;
|
|
928
|
+
}[] | undefined;
|
|
929
|
+
}>;
|
|
930
|
+
};
|
|
931
|
+
transcription: {
|
|
932
|
+
request: typeof import("./transcription.js").request;
|
|
933
|
+
generate: <const Model extends TranscriptionModel>(input: TranscriptionRequestInput<Model> | TranscriptionRequest, options?: AwaitOptions & RunOptions) => Promise<import("./transcription.js").TranscriptionResponse>;
|
|
934
|
+
stream: <const Model extends TranscriptionModel>(input: TranscriptionRequestInput<Model> | TranscriptionRequest, options?: AwaitOptions & RunOptions) => AsyncIterable<{
|
|
935
|
+
readonly id: string;
|
|
936
|
+
readonly type: "generation-queued";
|
|
937
|
+
readonly position?: number | undefined;
|
|
938
|
+
} | {
|
|
939
|
+
readonly id: string;
|
|
940
|
+
readonly type: "generation-progress";
|
|
941
|
+
readonly progress?: number | undefined;
|
|
942
|
+
} | {
|
|
943
|
+
readonly type: "text-delta";
|
|
944
|
+
readonly delta: string;
|
|
945
|
+
} | {
|
|
946
|
+
readonly type: "segment";
|
|
947
|
+
readonly segment: {
|
|
948
|
+
readonly text: string;
|
|
949
|
+
readonly startSeconds: number;
|
|
950
|
+
readonly endSeconds: number;
|
|
951
|
+
readonly speaker?: string | undefined;
|
|
952
|
+
};
|
|
953
|
+
} | {
|
|
954
|
+
readonly type: "finish";
|
|
955
|
+
readonly text: string;
|
|
956
|
+
readonly durationSeconds?: number | undefined;
|
|
957
|
+
readonly providerMetadata?: {
|
|
958
|
+
readonly [x: string]: {
|
|
959
|
+
readonly [x: string]: unknown;
|
|
960
|
+
};
|
|
961
|
+
} | undefined;
|
|
962
|
+
readonly usage?: {
|
|
963
|
+
readonly type: "tokens";
|
|
964
|
+
readonly input?: number | undefined;
|
|
965
|
+
readonly output?: number | undefined;
|
|
966
|
+
readonly total?: number | undefined;
|
|
967
|
+
readonly details?: {
|
|
968
|
+
readonly [x: string]: unknown;
|
|
969
|
+
} | undefined;
|
|
970
|
+
} | {
|
|
971
|
+
readonly type: "seconds";
|
|
972
|
+
readonly seconds: number;
|
|
973
|
+
} | {
|
|
974
|
+
readonly type: "characters";
|
|
975
|
+
readonly characters: number;
|
|
976
|
+
} | {
|
|
977
|
+
readonly type: "credits";
|
|
978
|
+
readonly credits: number;
|
|
979
|
+
} | {
|
|
980
|
+
readonly type: "compute";
|
|
981
|
+
readonly seconds: number;
|
|
982
|
+
} | undefined;
|
|
983
|
+
readonly notices?: readonly {
|
|
984
|
+
readonly type: "other" | "moderated" | "filtered";
|
|
985
|
+
readonly message: string;
|
|
986
|
+
readonly providerMetadata?: {
|
|
987
|
+
readonly [x: string]: {
|
|
988
|
+
readonly [x: string]: unknown;
|
|
989
|
+
};
|
|
990
|
+
} | undefined;
|
|
991
|
+
}[] | undefined;
|
|
992
|
+
readonly language?: string | undefined;
|
|
993
|
+
readonly segments?: readonly {
|
|
994
|
+
readonly text: string;
|
|
995
|
+
readonly startSeconds: number;
|
|
996
|
+
readonly endSeconds: number;
|
|
997
|
+
readonly speaker?: string | undefined;
|
|
998
|
+
}[] | undefined;
|
|
999
|
+
readonly words?: readonly {
|
|
1000
|
+
readonly text: string;
|
|
1001
|
+
readonly startSeconds: number;
|
|
1002
|
+
readonly endSeconds: number;
|
|
1003
|
+
readonly speaker?: string | undefined;
|
|
1004
|
+
readonly confidence?: number | undefined;
|
|
1005
|
+
}[] | undefined;
|
|
1006
|
+
}>;
|
|
1007
|
+
start: <const Model extends TranscriptionModel>(input: TranscriptionRequestInput<Model> | TranscriptionRequest, options?: RunOptions) => Promise<GenerationHandle<import("./transcription.js").TranscriptionResponse>>;
|
|
1008
|
+
resume: (model: TranscriptionModel, token: unknown, options?: RunOptions) => Promise<GenerationHandle<import("./transcription.js").TranscriptionResponse>>;
|
|
1009
|
+
};
|
|
552
1010
|
dispose: () => Promise<void>;
|
|
553
1011
|
};
|
|
554
1012
|
export * as AI from "./promise.js";
|
package/dist/promise.js
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
import { Effect, Layer, ManagedRuntime, Stream } from "effect";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { AIClient } from "./ai-client.js";
|
|
3
|
+
import { Image } from "./image.js";
|
|
4
4
|
import { LLM } from "./index.js";
|
|
5
|
-
import {
|
|
5
|
+
import { Media } from "./media.js";
|
|
6
|
+
import { tryRequest } from "./media-model.js";
|
|
6
7
|
import { RequestExecutor } from "./route/executor.js";
|
|
7
|
-
import { LanguageModel, LLMRequest } from "./schema/index.js";
|
|
8
|
+
import { AIError, InvalidRequestError, LanguageModel, LLMRequest } from "./schema/index.js";
|
|
9
|
+
import { Speech } from "./speech.js";
|
|
10
|
+
import { Transcription, } from "./transcription.js";
|
|
11
|
+
import { fileMediaType } from "./utils/media-type.js";
|
|
12
|
+
import { Video } from "./video.js";
|
|
8
13
|
const abortEffect = (signal) => signal === undefined
|
|
9
14
|
? Effect.never
|
|
10
15
|
: Effect.callback((resume) => {
|
|
@@ -17,28 +22,81 @@ const abortEffect = (signal) => signal === undefined
|
|
|
17
22
|
return Effect.sync(() => signal.removeEventListener("abort", onAbort));
|
|
18
23
|
});
|
|
19
24
|
export const make = (options = {}) => {
|
|
20
|
-
const runtime = ManagedRuntime.make(
|
|
21
|
-
/** Run any package Effect (for example `
|
|
25
|
+
const runtime = ManagedRuntime.make(AIClient.layerWith(options.layer ?? RequestExecutor.fetchLayer));
|
|
26
|
+
/** Run any package Effect (for example `LLMClient.compact(...)`) inside this runtime. */
|
|
22
27
|
const run = (effect, options) => runtime.runPromise(effect, { signal: options?.signal });
|
|
23
28
|
const iterate = (stream, options) => Stream.toAsyncIterable(Stream.unwrap(runtime.contextEffect.pipe(Effect.map((context) => stream.pipe(Stream.interruptWhen(abortEffect(options?.signal)), Stream.provideContext(context))))));
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
29
|
+
const handle = (generation) => ({
|
|
30
|
+
...generation.snapshot,
|
|
31
|
+
token: generation.token,
|
|
32
|
+
await: (options) => run(generation.await({ poll: options?.poll }), options),
|
|
33
|
+
events: (options) => iterate(generation.events({ poll: options?.poll }), options),
|
|
34
|
+
result: (options) => run(generation.result(), options),
|
|
35
|
+
refresh: (options) => run(generation.refresh(), options).then(handle),
|
|
36
|
+
cancel: (options) => run(generation.cancel(), options),
|
|
37
|
+
});
|
|
38
|
+
const llmRequest = (input) => input instanceof LLMRequest ? Effect.succeed(input) : tryRequest(() => LLM.request(input));
|
|
27
39
|
return {
|
|
28
40
|
run,
|
|
41
|
+
/** Decoded asset bytes, downloading `url` sources through the executor. */
|
|
42
|
+
bytes: (asset, options) => run(asset.bytes(), options),
|
|
43
|
+
base64: (asset, options) => run(asset.base64(), options),
|
|
44
|
+
/** Pull a `url` asset into owned bytes before the provider URL expires. */
|
|
45
|
+
materialize: (asset, options) => run(asset.materialize(), options),
|
|
46
|
+
/** Read a file into an asset like `Media.file`: sniffed media type, then the extension's. */
|
|
47
|
+
file: async (path, options) => {
|
|
48
|
+
const { readFile } = await import("node:fs/promises");
|
|
49
|
+
return run(Effect.tryPromise({
|
|
50
|
+
try: (signal) => readFile(path, { signal }),
|
|
51
|
+
catch: (cause) => fileError(`Failed to read media file ${path}`, cause),
|
|
52
|
+
}).pipe(Effect.map((buffer) => {
|
|
53
|
+
const data = new Uint8Array(buffer);
|
|
54
|
+
return Media.bytes(data, fileMediaType(data, path), options);
|
|
55
|
+
})), options);
|
|
56
|
+
},
|
|
57
|
+
/** Write an asset's bytes like `Media.write`, downloading `url` sources through the executor. */
|
|
58
|
+
write: async (asset, path, options) => {
|
|
59
|
+
const { writeFile } = await import("node:fs/promises");
|
|
60
|
+
return run(asset.bytes().pipe(Effect.flatMap((data) => Effect.tryPromise({
|
|
61
|
+
try: (signal) => writeFile(path, data, { signal }),
|
|
62
|
+
catch: (cause) => fileError(`Failed to write media file ${path}`, cause),
|
|
63
|
+
}))), options);
|
|
64
|
+
},
|
|
29
65
|
llm: {
|
|
30
66
|
request: LLM.request,
|
|
31
|
-
generate: (input, options) => run(
|
|
32
|
-
stream: (input, options) => iterate(
|
|
67
|
+
generate: (input, options) => run(Effect.flatMap(llmRequest(input), LLM.generate), options),
|
|
68
|
+
stream: (input, options) => iterate(Stream.unwrap(Effect.map(llmRequest(input), LLM.stream)), options),
|
|
33
69
|
},
|
|
34
70
|
image: {
|
|
35
71
|
request: Image.request,
|
|
36
|
-
generate: (input, options) => run(Image.generate(
|
|
37
|
-
stream: (input, options) => iterate(Image.stream(
|
|
72
|
+
generate: (input, options) => run(Image.generate(input, { poll: options?.poll }), options),
|
|
73
|
+
stream: (input, options) => iterate(Image.stream(input, { poll: options?.poll }), options),
|
|
74
|
+
start: (input, options) => run(Image.start(input), options).then(handle),
|
|
75
|
+
resume: (model, token, options) => run(Image.resume(model, token), options).then(handle),
|
|
76
|
+
},
|
|
77
|
+
video: {
|
|
78
|
+
request: Video.request,
|
|
79
|
+
start: (input, options) => run(Video.start(input), options).then(handle),
|
|
80
|
+
generate: (input, options) => run(Video.generate(input, { poll: options?.poll }), options),
|
|
81
|
+
resume: (model, token, options) => run(Video.resume(model, token), options).then(handle),
|
|
82
|
+
stream: (input, options) => iterate(Video.stream(input, { poll: options?.poll }), options),
|
|
83
|
+
},
|
|
84
|
+
speech: {
|
|
85
|
+
request: Speech.request,
|
|
86
|
+
generate: (input, options) => run(Speech.generate(input), options),
|
|
87
|
+
stream: (input, options) => iterate(Speech.stream(input), options),
|
|
88
|
+
},
|
|
89
|
+
transcription: {
|
|
90
|
+
request: Transcription.request,
|
|
91
|
+
generate: (input, options) => run(Transcription.generate(input, { poll: options?.poll }), options),
|
|
92
|
+
stream: (input, options) => iterate(Transcription.stream(input, { poll: options?.poll }), options),
|
|
93
|
+
start: (input, options) => run(Transcription.start(input), options).then(handle),
|
|
94
|
+
resume: (model, token, options) => run(Transcription.resume(model, token), options).then(handle),
|
|
38
95
|
},
|
|
39
96
|
dispose: () => runtime.dispose(),
|
|
40
97
|
};
|
|
41
98
|
};
|
|
99
|
+
const fileError = (message, cause) => new AIError({ reason: new InvalidRequestError({ message, cause }) });
|
|
42
100
|
/** Default client over `RequestExecutor.fetchLayer` for scripts; the runtime builds its layer on first use. */
|
|
43
101
|
export const ai = make();
|
|
44
102
|
export * as AI from "./promise.js";
|