@opencode/ai 0.0.0-dev-21801 → 0.0.0-dev-21803

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/dist/promise.js DELETED
@@ -1,106 +0,0 @@
1
- import { Effect, Layer, ManagedRuntime, Stream } from "effect";
2
- import { AIClient } from "./ai-client.js";
3
- import { Decision } from "./decision.js";
4
- import { Image } from "./image.js";
5
- import { LLM } from "./index.js";
6
- import { Media } from "./media.js";
7
- import { RequestExecutor } from "./route/executor.js";
8
- import { AIError, InvalidRequestError, LanguageModel } 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";
13
- // Fails with `signal.reason` so aborted calls reject and aborted streams throw like `fetch`: an `AbortError` by default.
14
- const abortEffect = (signal) => signal === undefined
15
- ? Effect.never
16
- : Effect.callback((resume) => {
17
- if (signal.aborted) {
18
- resume(Effect.fail(signal.reason));
19
- return;
20
- }
21
- const onAbort = () => resume(Effect.fail(signal.reason));
22
- signal.addEventListener("abort", onAbort, { once: true });
23
- return Effect.sync(() => signal.removeEventListener("abort", onAbort));
24
- });
25
- export const make = (options = {}) => {
26
- const runtime = ManagedRuntime.make(AIClient.layerWith(options.layer ?? RequestExecutor.fetchLayer));
27
- /** Run any package Effect (for example `LLMClient.compact(...)`) inside this runtime. */
28
- const run = (effect, options) => runtime.runPromise(Effect.raceFirst(effect, abortEffect(options?.signal)));
29
- const iterate = (stream, options) => Stream.toAsyncIterable(Stream.unwrap(runtime.contextEffect.pipe(Effect.map((context) => stream.pipe(Stream.interruptWhen(abortEffect(options?.signal)), Stream.provideContext(context))))));
30
- const handle = (generation) => ({
31
- ...generation.snapshot,
32
- token: generation.token,
33
- await: (options) => run(generation.await({ poll: options?.poll }), options),
34
- events: (options) => iterate(generation.events({ poll: options?.poll }), options),
35
- result: (options) => run(generation.result(), options),
36
- refresh: (options) => run(generation.refresh(), options).then(handle),
37
- cancel: (options) => run(generation.cancel(), options),
38
- });
39
- return {
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
- },
65
- llm: {
66
- request: LLM.request,
67
- generate: (input, options) => run(LLM.generate(input), options),
68
- stream: (input, options) => iterate(LLM.stream(input), options),
69
- },
70
- image: {
71
- request: Image.request,
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),
95
- },
96
- decision: {
97
- request: Decision.request,
98
- generate: (input, options) => run(Decision.generate(input), options),
99
- },
100
- dispose: () => runtime.dispose(),
101
- };
102
- };
103
- const fileError = (message, cause) => new AIError({ reason: new InvalidRequestError({ message, cause }) });
104
- /** Default client over `RequestExecutor.fetchLayer` for scripts; the runtime builds its layer on first use. */
105
- export const ai = make();
106
- export * as AI from "./promise.js";