@opencode/ai 2.1.3 → 2.2.0
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 +283 -114
- package/dist/ai-client.d.ts +2 -2
- package/dist/ai-client.js +2 -1
- package/dist/cache-policy.d.ts +4 -0
- package/dist/cache-policy.js +20 -6
- package/dist/decision-client.d.ts +21 -0
- package/dist/decision-client.js +55 -0
- package/dist/decision.d.ts +472 -0
- package/dist/decision.js +158 -0
- package/dist/effect.d.ts +38 -0
- package/dist/effect.js +27 -0
- package/dist/effort-updates.d.ts +2 -2
- package/dist/experimental/cloudflare-run-extraction.d.ts +13 -0
- package/dist/experimental/cloudflare-run-extraction.js +83 -0
- package/dist/experimental/extraction.d.ts +183 -0
- package/dist/experimental/extraction.js +102 -0
- package/dist/experimental/realtime.d.ts +7 -2
- package/dist/experimental/realtime.js +5 -5
- package/dist/experimental.d.ts +2 -3
- package/dist/experimental.js +1 -2
- package/dist/image-client.d.ts +13 -22
- package/dist/image-client.js +0 -1
- package/dist/image.d.ts +154 -324
- package/dist/image.js +4 -11
- package/dist/index.d.ts +1426 -34
- package/dist/index.js +127 -24
- package/dist/llm.d.ts +16 -1
- package/dist/llm.js +34 -0
- package/dist/media-client.d.ts +2 -1
- package/dist/media-model.d.ts +3 -0
- package/dist/media-model.js +4 -1
- package/dist/media.d.ts +0 -7
- package/dist/media.js +0 -6
- package/dist/protocols/alibaba-chat.d.ts +32 -12
- package/dist/protocols/alibaba-images.d.ts +22 -0
- package/dist/protocols/alibaba-images.js +77 -0
- package/dist/protocols/alibaba-messages.d.ts +62 -27
- package/dist/protocols/alibaba-responses.d.ts +41 -38
- package/dist/protocols/anthropic-messages.d.ts +196 -94
- package/dist/protocols/anthropic-messages.js +47 -16
- package/dist/protocols/bedrock-converse.d.ts +140 -47
- package/dist/protocols/bedrock-converse.js +114 -18
- package/dist/protocols/bedrock-event-stream.js +1 -0
- package/dist/protocols/bfl-images.d.ts +9 -0
- package/dist/protocols/bfl-images.js +17 -15
- package/dist/protocols/cartesia-speech.d.ts +32 -50
- package/dist/protocols/cloudflare-run-decisions.d.ts +9 -0
- package/dist/protocols/cloudflare-run-decisions.js +36 -0
- package/dist/protocols/cloudflare-workers-ai-images.d.ts +23 -0
- package/dist/protocols/cloudflare-workers-ai-images.js +80 -0
- package/dist/protocols/cohere-chat.d.ts +7 -8
- package/dist/protocols/deepgram-speech.d.ts +32 -50
- package/dist/protocols/elevenlabs-speech.d.ts +32 -50
- package/dist/protocols/fal-images.d.ts +10 -1
- package/dist/protocols/fal-images.js +16 -25
- package/dist/protocols/fal-video.js +1 -1
- package/dist/protocols/gemini.d.ts +37 -34
- package/dist/protocols/gemini.js +13 -8
- package/dist/protocols/google-images.d.ts +2 -2
- package/dist/protocols/google-images.js +8 -37
- package/dist/protocols/google-interactions.d.ts +18 -14
- package/dist/protocols/google-interactions.js +14 -7
- package/dist/protocols/google-speech.d.ts +32 -50
- package/dist/protocols/google-speech.js +0 -1
- package/dist/protocols/google-transcription.d.ts +32 -50
- package/dist/protocols/google-transcription.js +0 -1
- package/dist/protocols/google-video.d.ts +1 -1
- package/dist/protocols/google-video.js +2 -7
- package/dist/protocols/index.d.ts +3 -0
- package/dist/protocols/index.js +3 -0
- package/dist/protocols/meta-images.d.ts +7 -1
- package/dist/protocols/meta-images.js +10 -8
- package/dist/protocols/meta-messages.d.ts +73 -38
- package/dist/protocols/meta-messages.js +14 -1
- package/dist/protocols/meta-responses.d.ts +84 -78
- package/dist/protocols/meta-responses.js +29 -1
- package/dist/protocols/mistral-chat.d.ts +81 -78
- package/dist/protocols/mistral-chat.js +2 -1
- package/dist/protocols/open-responses-channel.js +47 -46
- package/dist/protocols/open-responses-continuation.d.ts +8 -2
- package/dist/protocols/open-responses-continuation.js +3 -1
- package/dist/protocols/open-responses.d.ts +426 -396
- package/dist/protocols/open-responses.js +16 -25
- package/dist/protocols/openai-chat.d.ts +764 -52
- package/dist/protocols/openai-chat.js +38 -25
- package/dist/protocols/openai-compatible-chat.d.ts +35 -15
- package/dist/protocols/openai-compatible-responses.d.ts +44 -42
- package/dist/protocols/openai-decisions.d.ts +22 -0
- package/dist/protocols/openai-decisions.js +128 -0
- package/dist/protocols/openai-images.d.ts +53 -56
- package/dist/protocols/openai-images.js +100 -34
- package/dist/protocols/openai-responses.d.ts +322 -264
- package/dist/protocols/openai-responses.js +33 -3
- package/dist/protocols/openai-speech.d.ts +32 -50
- package/dist/protocols/openai-speech.js +15 -5
- package/dist/protocols/openai-transcription.d.ts +32 -50
- package/dist/protocols/openai-transcription.js +15 -11
- package/dist/protocols/replicate-images.js +1 -1
- package/dist/protocols/runway-video.d.ts +1 -1
- package/dist/protocols/runway-video.js +1 -1
- package/dist/protocols/shared.d.ts +9 -1
- package/dist/protocols/shared.js +13 -2
- package/dist/protocols/stability-images.d.ts +5 -0
- package/dist/protocols/stability-images.js +4 -9
- package/dist/protocols/system-one.d.ts +101 -0
- package/dist/protocols/system-one.js +230 -0
- package/dist/protocols/utils/bedrock-cache.d.ts +2 -2
- package/dist/protocols/utils/bedrock-cache.js +7 -6
- package/dist/protocols/utils/cache.d.ts +14 -3
- package/dist/protocols/utils/cache.js +11 -4
- package/dist/protocols/utils/gemini-generate-content.d.ts +3 -3
- package/dist/protocols/utils/gemini-generate-content.js +10 -16
- package/dist/protocols/utils/gemini-json-schema.d.ts +5 -0
- package/dist/protocols/utils/gemini-json-schema.js +3 -3
- package/dist/protocols/utils/image-sizing.d.ts +31 -0
- package/dist/protocols/utils/image-sizing.js +73 -0
- package/dist/protocols/utils/media-input.d.ts +4 -5
- package/dist/protocols/utils/media-input.js +8 -5
- package/dist/protocols/utils/partial-json-options.d.ts +0 -1
- package/dist/protocols/utils/partial-json-options.js +0 -1
- package/dist/protocols/utils/speech-stream.d.ts +0 -1
- package/dist/protocols/utils/speech-stream.js +0 -1
- package/dist/protocols/utils/tool-schema.js +15 -5
- package/dist/protocols/utils/tool-stream.d.ts +10 -1
- package/dist/protocols/venice-chat.d.ts +71 -53
- package/dist/protocols/venice-chat.js +7 -6
- package/dist/protocols/xai-images.d.ts +1 -2
- package/dist/protocols/xai-images.js +9 -2
- package/dist/protocols/xai-responses.d.ts +88 -78
- package/dist/protocols/xai-responses.js +28 -2
- package/dist/protocols/xai-video.d.ts +1 -1
- package/dist/protocols/xai-video.js +11 -8
- package/dist/protocols/zai-chat.d.ts +54 -30
- package/dist/protocols/zai-chat.js +8 -3
- package/dist/protocols/zai-images.d.ts +3 -1
- package/dist/protocols/zai-images.js +12 -11
- package/dist/protocols/zai-messages.d.ts +72 -35
- package/dist/protocols/zai-messages.js +3 -2
- package/dist/provider-error.js +24 -7
- package/dist/providers/alibaba/images.d.ts +2 -0
- package/dist/providers/alibaba/images.js +1 -0
- package/dist/providers/alibaba.d.ts +146 -85
- package/dist/providers/alibaba.js +16 -8
- package/dist/providers/amazon-bedrock-mantle.d.ts +277 -218
- package/dist/providers/amazon-bedrock.d.ts +48 -28
- package/dist/providers/amazon-bedrock.js +11 -11
- package/dist/providers/anthropic-compatible.d.ts +64 -29
- package/dist/providers/anthropic.d.ts +64 -29
- package/dist/providers/azure/images.d.ts +2 -0
- package/dist/providers/azure/images.js +1 -0
- package/dist/providers/azure.d.ts +156 -115
- package/dist/providers/azure.js +19 -1
- package/dist/providers/baseten/decisions.d.ts +2 -0
- package/dist/providers/baseten/decisions.js +1 -0
- package/dist/providers/baseten.d.ts +75 -25
- package/dist/providers/baseten.js +41 -8
- package/dist/providers/cerebras.d.ts +66 -24
- package/dist/providers/cloudflare-ai-gateway/decisions.d.ts +2 -0
- package/dist/providers/cloudflare-ai-gateway/decisions.js +1 -0
- package/dist/providers/cloudflare-ai-gateway.d.ts +409 -272
- package/dist/providers/cloudflare-ai-gateway.js +33 -6
- package/dist/providers/cloudflare-workers-ai/decisions.d.ts +2 -0
- package/dist/providers/cloudflare-workers-ai/decisions.js +1 -0
- package/dist/providers/cloudflare-workers-ai/images.d.ts +2 -0
- package/dist/providers/cloudflare-workers-ai/images.js +1 -0
- package/dist/providers/cloudflare-workers-ai.d.ts +79 -25
- package/dist/providers/cloudflare-workers-ai.js +45 -9
- package/dist/providers/cohere.d.ts +117 -75
- package/dist/providers/deepinfra.d.ts +66 -24
- package/dist/providers/deepseek.d.ts +66 -24
- package/dist/providers/digitalocean/decisions.d.ts +2 -0
- package/dist/providers/digitalocean/decisions.js +1 -0
- package/dist/providers/digitalocean/images.d.ts +2 -0
- package/dist/providers/digitalocean/images.js +1 -0
- package/dist/providers/digitalocean.d.ts +108 -37
- package/dist/providers/digitalocean.js +39 -8
- package/dist/providers/fireworks.d.ts +66 -24
- package/dist/providers/google/images.d.ts +2 -0
- package/dist/providers/google/images.js +1 -0
- package/dist/providers/google-vertex/images.d.ts +2 -0
- package/dist/providers/google-vertex/images.js +1 -0
- package/dist/providers/google-vertex-chat.d.ts +33 -12
- package/dist/providers/google-vertex-messages.d.ts +64 -29
- package/dist/providers/google-vertex-mistral.d.ts +17 -17
- package/dist/providers/google-vertex-responses.d.ts +38 -35
- package/dist/providers/google-vertex.d.ts +20 -18
- package/dist/providers/google-vertex.js +32 -15
- package/dist/providers/google.d.ts +22 -21
- package/dist/providers/google.js +15 -8
- package/dist/providers/groq.d.ts +66 -28
- package/dist/providers/groq.js +0 -6
- package/dist/providers/meta.d.ts +144 -85
- package/dist/providers/minimax.d.ts +145 -86
- package/dist/providers/mistral.d.ts +28 -28
- package/dist/providers/moonshot.d.ts +277 -218
- package/dist/providers/open-responses-options.d.ts +0 -1
- package/dist/providers/open-responses-options.js +1 -1
- package/dist/providers/openai/images.d.ts +2 -0
- package/dist/providers/openai/images.js +1 -0
- package/dist/providers/openai-compatible/images.d.ts +2 -0
- package/dist/providers/openai-compatible/images.js +1 -0
- package/dist/providers/openai-compatible-responses.d.ts +38 -35
- package/dist/providers/openai-compatible.d.ts +36 -13
- package/dist/providers/openai-compatible.js +22 -10
- package/dist/providers/openai-options.d.ts +3 -2
- package/dist/providers/openai-options.js +0 -1
- package/dist/providers/openai.d.ts +146 -109
- package/dist/providers/openai.js +20 -4
- package/dist/providers/opencode/decisions.d.ts +2 -0
- package/dist/providers/opencode/decisions.js +1 -0
- package/dist/providers/opencode.d.ts +9 -10
- package/dist/providers/opencode.js +10 -8
- package/dist/providers/openrouter/decisions.d.ts +2 -0
- package/dist/providers/openrouter/decisions.js +1 -0
- package/dist/providers/openrouter/images.d.ts +2 -0
- package/dist/providers/openrouter/images.js +1 -0
- package/dist/providers/openrouter.d.ts +128 -47
- package/dist/providers/openrouter.js +38 -16
- package/dist/providers/togetherai.d.ts +66 -24
- package/dist/providers/typesafe-ai/decisions.d.ts +2 -0
- package/dist/providers/typesafe-ai/decisions.js +1 -0
- package/dist/providers/typesafe-ai.d.ts +9 -10
- package/dist/providers/typesafe-ai.js +10 -8
- package/dist/providers/venice.d.ts +214 -20
- package/dist/providers/vercel-ai-gateway/decisions.d.ts +2 -0
- package/dist/providers/vercel-ai-gateway/decisions.js +1 -0
- package/dist/providers/vercel-ai-gateway/images.d.ts +2 -0
- package/dist/providers/vercel-ai-gateway/images.js +1 -0
- package/dist/providers/vercel-ai-gateway.d.ts +10 -16
- package/dist/providers/vercel-ai-gateway.js +31 -85
- package/dist/providers/xai/images.d.ts +2 -0
- package/dist/providers/xai/images.js +1 -0
- package/dist/providers/xai.d.ts +40 -17
- package/dist/providers/xai.js +15 -8
- package/dist/providers/zai/images.d.ts +2 -0
- package/dist/providers/zai/images.js +1 -0
- package/dist/providers/zai-coding-plan.d.ts +224 -85
- package/dist/providers/zai.d.ts +110 -23
- package/dist/providers/zai.js +15 -8
- package/dist/route/auth-options.d.ts +0 -3
- package/dist/route/auth.d.ts +0 -2
- package/dist/route/auth.js +0 -2
- package/dist/route/client.d.ts +2 -4
- package/dist/route/client.js +29 -8
- package/dist/route/decision.d.ts +58 -0
- package/dist/route/decision.js +75 -0
- package/dist/route/executor-service.js +1 -1
- package/dist/route/executor.d.ts +1 -1
- package/dist/route/framing.d.ts +2 -0
- package/dist/route/index.d.ts +3 -0
- package/dist/route/index.js +3 -0
- package/dist/route/media-protocol.d.ts +1 -0
- package/dist/route/media-protocol.js +12 -8
- package/dist/route/media.d.ts +5 -1
- package/dist/route/media.js +6 -2
- package/dist/route/transport/http.js +1 -0
- package/dist/route/transport/index.d.ts +1 -0
- package/dist/schema/events.d.ts +148 -6
- package/dist/schema/events.js +20 -0
- package/dist/schema/messages.d.ts +6 -2
- package/dist/schema/messages.js +3 -0
- package/dist/schema/options.d.ts +5 -3
- package/dist/schema/options.js +8 -3
- package/dist/speech-client.d.ts +13 -22
- package/dist/speech-client.js +0 -1
- package/dist/speech.d.ts +148 -283
- package/dist/speech.js +2 -4
- package/dist/testing.d.ts +25 -24
- package/dist/testing.js +3 -21
- package/dist/transcription-client.d.ts +13 -22
- package/dist/transcription.d.ts +148 -283
- package/dist/transcription.js +1 -1
- package/dist/utils/media-type.js +22 -1
- package/dist/video-client.d.ts +13 -22
- package/dist/video-client.js +0 -1
- package/dist/video.d.ts +115 -250
- package/dist/video.js +3 -5
- package/package.json +516 -13
- package/dist/experimental/evaluation-client.d.ts +0 -21
- package/dist/experimental/evaluation-client.js +0 -64
- package/dist/experimental/evaluation.d.ts +0 -242
- package/dist/experimental/evaluation.js +0 -115
- package/dist/experimental/system-one.d.ts +0 -16
- package/dist/experimental/system-one.js +0 -135
- package/dist/promise.d.ts +0 -1021
- package/dist/promise.js +0 -101
- package/dist/provider.d.ts +0 -23
- package/dist/provider.js +0 -2
package/README.md
CHANGED
|
@@ -1,10 +1,33 @@
|
|
|
1
1
|
# @opencode/ai
|
|
2
2
|
|
|
3
|
-
Schema-first APIs for text, images, video, speech, and
|
|
3
|
+
Schema-first APIs for text, images, video, speech, transcription, and decisions, built with Effect.
|
|
4
|
+
|
|
5
|
+
`@opencode/ai` is the Promise API: one managed runtime behind the LLM and media methods, plus asset helpers. `ai.file`
|
|
6
|
+
and `ai.write` load `node:fs/promises` on first use, so no Effect `FileSystem` is needed:
|
|
7
|
+
|
|
8
|
+
```ts
|
|
9
|
+
import { AI } from "@opencode/ai"
|
|
10
|
+
import { OpenAI } from "@opencode/ai/providers"
|
|
11
|
+
|
|
12
|
+
const openai = OpenAI.configure({ apiKey: process.env.OPENAI_API_KEY })
|
|
13
|
+
|
|
14
|
+
const ai = AI.make()
|
|
15
|
+
const input = { model: openai.responses("gpt-4o-mini"), prompt: "Say hello." }
|
|
16
|
+
const response = await ai.llm.generate(input)
|
|
17
|
+
console.log(response.text)
|
|
18
|
+
const generated = await ai.image.generate({ model: openai.image("gpt-image-2"), prompt: "A lighthouse" })
|
|
19
|
+
await ai.write(generated.image, "./lighthouse.png") // also ai.file(path), ai.bytes(asset), ai.base64(asset), ai.materialize(asset)
|
|
20
|
+
for await (const event of ai.llm.stream(ai.llm.request(input))) {
|
|
21
|
+
// LLMEvent
|
|
22
|
+
}
|
|
23
|
+
await ai.dispose()
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
The Effect API lives at `@opencode/ai/effect`. Its methods return `Effect` or `Stream`:
|
|
4
27
|
|
|
5
28
|
```ts
|
|
6
29
|
import { Effect } from "effect"
|
|
7
|
-
import { AIClient, LLM } from "@opencode/ai"
|
|
30
|
+
import { AIClient, LLM } from "@opencode/ai/effect"
|
|
8
31
|
import { OpenAI } from "@opencode/ai/providers"
|
|
9
32
|
|
|
10
33
|
const openai = OpenAI.configure({ apiKey: process.env.OPENAI_API_KEY })
|
|
@@ -27,6 +50,70 @@ Run `LLM.stream(...)` instead of `generate` when you want incremental `LLMEvent`
|
|
|
27
50
|
`LLM.request(...)`. The event stream is provider-neutral — same shape across OpenAI Chat, OpenAI Responses,
|
|
28
51
|
Anthropic Messages, Gemini, Bedrock Converse, and any OpenAI-compatible deployment.
|
|
29
52
|
|
|
53
|
+
### Tool loop
|
|
54
|
+
|
|
55
|
+
`stream` runs one model call; `loop` runs the model's tool calls and calls the model again until it stops calling
|
|
56
|
+
tools. Events from every step arrive on one stream, with a `tool-result` after each tool runs. `until` is checked
|
|
57
|
+
after each step that has tool calls, with every step's `LLMResponse`; returning `true` ends the loop without running
|
|
58
|
+
them. `AI.tool` takes a plain `async` execute; a throw or rejection goes back to the model as an error result, and
|
|
59
|
+
`signal` aborts when the loop is interrupted.
|
|
60
|
+
`Tool.make` from `@opencode/ai/effect` is the Effect form, with a validated `success` schema:
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
import { readFile } from "node:fs/promises"
|
|
64
|
+
import { Schema } from "effect"
|
|
65
|
+
import { AI } from "@opencode/ai"
|
|
66
|
+
|
|
67
|
+
const read_file = AI.tool({
|
|
68
|
+
description: "Read a UTF-8 file.",
|
|
69
|
+
parameters: Schema.Struct({ path: Schema.String }),
|
|
70
|
+
execute: ({ path }, { signal }) => readFile(path, { encoding: "utf8", signal }),
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
const events = ai.llm.loop({
|
|
74
|
+
model,
|
|
75
|
+
prompt: "Summarize package.json.",
|
|
76
|
+
tools: { read_file },
|
|
77
|
+
until: ({ steps }) => steps.length >= 8,
|
|
78
|
+
})
|
|
79
|
+
for await (const event of events) {
|
|
80
|
+
if (event.type === "text-delta") process.stdout.write(event.text)
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### Structured output
|
|
85
|
+
|
|
86
|
+
`generateObject` forces a tool call and decodes its input against `schema`; pass `jsonSchema` instead for an
|
|
87
|
+
unvalidated `unknown` object:
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
const { object } = await ai.llm.generateObject({
|
|
91
|
+
model,
|
|
92
|
+
prompt: "Pick a city.",
|
|
93
|
+
schema: Schema.Struct({ city: Schema.String }),
|
|
94
|
+
})
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Raw provider events
|
|
98
|
+
|
|
99
|
+
Set `includeRaw: true` on a request to also receive every provider frame as `{ type: "raw", data }`, in order and
|
|
100
|
+
before the events parsed from that frame. `data` is the parsed JSON frame, so frames this package doesn't handle yet
|
|
101
|
+
are visible too. Raw events are off by default, and `generate` ignores them. Translate a frame with a stream map:
|
|
102
|
+
|
|
103
|
+
```ts
|
|
104
|
+
import { Stream } from "effect"
|
|
105
|
+
import { LLM, LLMEvent } from "@opencode/ai/effect"
|
|
106
|
+
|
|
107
|
+
const flagged = (data: unknown) =>
|
|
108
|
+
typeof data === "object" && data !== null && "type" in data && data.type === "moderation.flagged"
|
|
109
|
+
|
|
110
|
+
const events = LLM.stream({ model, prompt: "Say hello.", includeRaw: true }).pipe(
|
|
111
|
+
Stream.map((event) =>
|
|
112
|
+
event.type === "raw" && flagged(event.data) ? LLMEvent.providerError({ message: "Flagged by moderation" }) : event,
|
|
113
|
+
),
|
|
114
|
+
)
|
|
115
|
+
```
|
|
116
|
+
|
|
30
117
|
### Google Interactions
|
|
31
118
|
|
|
32
119
|
`Google.configure({ apiKey }).interactions(modelID)` selects the Interactions API; `.model(modelID)` still selects
|
|
@@ -62,13 +149,14 @@ provider's image route from the model and returns `Media.Asset`s with lazily dec
|
|
|
62
149
|
|
|
63
150
|
```ts
|
|
64
151
|
import { NodeFileSystem } from "@effect/platform-node"
|
|
65
|
-
import { Image, Media } from "@opencode/ai"
|
|
152
|
+
import { Image, Media } from "@opencode/ai/effect"
|
|
66
153
|
|
|
67
154
|
const image = Effect.gen(function* () {
|
|
68
155
|
const response = yield* Image.generate({
|
|
69
156
|
model: openai.image("gpt-image-2"),
|
|
70
157
|
prompt: "A robot tending a rooftop garden",
|
|
71
|
-
|
|
158
|
+
aspect: "1:1",
|
|
159
|
+
resolution: "1K",
|
|
72
160
|
providerOptions: { quality: "high" }, // typed per image model
|
|
73
161
|
})
|
|
74
162
|
yield* Media.write(response.image, "./garden.png")
|
|
@@ -94,30 +182,13 @@ const logged = RequestExecutor.middleware((request, next) =>
|
|
|
94
182
|
const everything = AIClient.layerWith(logged) // or AI.make({ layer: logged })
|
|
95
183
|
```
|
|
96
184
|
|
|
97
|
-
Prefer promises? `@opencode/ai/promise` exposes the same LLM and media APIs over one managed runtime, plus asset
|
|
98
|
-
helpers; `ai.file` and `ai.write` load `node:fs/promises` on first use, so no Effect `FileSystem` is needed:
|
|
99
|
-
|
|
100
|
-
```ts
|
|
101
|
-
import { AI } from "@opencode/ai/promise"
|
|
102
|
-
|
|
103
|
-
const ai = AI.make()
|
|
104
|
-
const input = { model: openai.responses("gpt-4o-mini"), prompt: "Say hello." }
|
|
105
|
-
const text = await ai.llm.generate(input)
|
|
106
|
-
const generated = await ai.image.generate({ model: openai.image("gpt-image-2"), prompt: "A lighthouse" })
|
|
107
|
-
await ai.write(generated.image, "./lighthouse.png") // also ai.file(path), ai.bytes(asset), ai.base64(asset), ai.materialize(asset)
|
|
108
|
-
for await (const event of ai.llm.stream(ai.llm.request(input))) {
|
|
109
|
-
// LLMEvent
|
|
110
|
-
}
|
|
111
|
-
await ai.dispose()
|
|
112
|
-
```
|
|
113
|
-
|
|
114
185
|
## Venice AI
|
|
115
186
|
|
|
116
187
|
`Venice` provides native Chat Completions with streaming tools and reasoning. `model` and `chat`
|
|
117
188
|
select the same API; credentials default to `VENICE_API_KEY`.
|
|
118
189
|
|
|
119
190
|
```ts
|
|
120
|
-
import { LLM } from "@opencode/ai"
|
|
191
|
+
import { LLM } from "@opencode/ai/effect"
|
|
121
192
|
import { Venice } from "@opencode/ai/providers"
|
|
122
193
|
import { Effect } from "effect"
|
|
123
194
|
|
|
@@ -146,57 +217,108 @@ usage reads Venice's `cache_creation_input_tokens` field.
|
|
|
146
217
|
The native package entrypoint is `@opencode/ai/providers/venice`. Image generation, embeddings, Responses, and
|
|
147
218
|
client-side E2EE are not implemented by this provider.
|
|
148
219
|
|
|
149
|
-
##
|
|
220
|
+
## Decisions
|
|
150
221
|
|
|
151
|
-
|
|
152
|
-
|
|
222
|
+
Decision models answer typed questions about an input: a yes/no `predicate` (a probability), a `choice` from listed
|
|
223
|
+
values, or a `score` on ordered levels, each with the probability of every option.
|
|
153
224
|
|
|
154
225
|
```ts
|
|
155
226
|
import { Effect } from "effect"
|
|
156
|
-
import {
|
|
157
|
-
import {
|
|
158
|
-
|
|
159
|
-
const model = TypeSafeAI.configure().experimental.evaluation("jev-latest")
|
|
227
|
+
import { Decision, DecisionClient, Media } from "@opencode/ai/effect"
|
|
228
|
+
import { OpenAI } from "@opencode/ai/providers"
|
|
160
229
|
|
|
161
|
-
const program =
|
|
162
|
-
model,
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
230
|
+
const program = Decision.generate({
|
|
231
|
+
model: OpenAI.configure().decision("gpt-6-luna"),
|
|
232
|
+
input: [
|
|
233
|
+
{ type: "text", text: "I was charged twice. Please refund the duplicate payment." },
|
|
234
|
+
{ type: "media", media: Media.url("https://example.com/receipt.png", { mediaType: "image/png" }) },
|
|
235
|
+
],
|
|
236
|
+
questions: [
|
|
237
|
+
{ name: "refund", type: "predicate", instructions: "Is the customer asking for a refund?" },
|
|
238
|
+
{
|
|
239
|
+
name: "department",
|
|
166
240
|
type: "choice",
|
|
167
241
|
instructions: "Which team should handle this?",
|
|
168
|
-
|
|
242
|
+
choices: [{ value: "billing", description: "Payments and refunds" }, { value: "technical" }],
|
|
169
243
|
},
|
|
170
|
-
|
|
244
|
+
{
|
|
245
|
+
name: "urgency",
|
|
171
246
|
type: "score",
|
|
172
247
|
instructions: "How urgent is this?",
|
|
173
|
-
|
|
248
|
+
levels: [{ label: "Can wait" }, { label: "Needs prompt attention" }, { label: "Blocking revenue" }],
|
|
174
249
|
},
|
|
175
|
-
|
|
176
|
-
},
|
|
250
|
+
],
|
|
177
251
|
})
|
|
178
252
|
|
|
179
|
-
const response = await Effect.runPromise(program.pipe(Effect.provide(
|
|
180
|
-
|
|
181
|
-
console.log(response.answers.department.choice)
|
|
182
|
-
console.log(response.answers.refund.probability)
|
|
253
|
+
const response = await Effect.runPromise(program.pipe(Effect.provide(DecisionClient.fetchLayer)))
|
|
254
|
+
const [refund, department] = response.answers
|
|
183
255
|
```
|
|
184
256
|
|
|
185
|
-
`
|
|
186
|
-
`
|
|
257
|
+
`input` is a JSON object or an ordered list of `text`, `json` and `media` parts. Answers come back in question order.
|
|
258
|
+
A question the host declines is `{ name, type: "error", reason, message? }`, with `reason` such as `"refusal"`; the
|
|
259
|
+
other answers still come back.
|
|
260
|
+
`response.usage` has `inputTokens`, `outputTokens`, `totalTokens` and the host's reported `cost` when it sends one;
|
|
261
|
+
the host's raw usage stays in `providerMetadata`.
|
|
262
|
+
Media goes wherever the host reads it, and a kind the host has no place for fails the call. Host-specific fields go in
|
|
263
|
+
`providerOptions`, typed by the selected model (OpenAI `promptCacheKey` and `safetyIdentifier`, OpenRouter
|
|
264
|
+
`provider` routing, Baseten `max_len`).
|
|
187
265
|
|
|
188
266
|
```ts
|
|
189
|
-
import {
|
|
267
|
+
import {
|
|
268
|
+
Baseten,
|
|
269
|
+
CloudflareAIGateway,
|
|
270
|
+
CloudflareWorkersAI,
|
|
271
|
+
DigitalOcean,
|
|
272
|
+
OpenCode,
|
|
273
|
+
OpenRouter,
|
|
274
|
+
TypeSafeAI,
|
|
275
|
+
VercelAIGateway,
|
|
276
|
+
} from "@opencode/ai/providers"
|
|
277
|
+
|
|
278
|
+
TypeSafeAI.configure().decision("jev-latest")
|
|
279
|
+
OpenCode.configure().decision("jev-1.13")
|
|
280
|
+
OpenRouter.configure().decision("typesafe/jev-1.13")
|
|
281
|
+
VercelAIGateway.configure().decision("typesafe-ai/jev")
|
|
282
|
+
Baseten.configure().decision("decision-models/baseten-elephant-9b-latest")
|
|
283
|
+
DigitalOcean.configure().decision("typesafe-jev-1.13.0")
|
|
284
|
+
CloudflareWorkersAI.configure({ accountId }).decision("@cf/cloudflare/clef")
|
|
285
|
+
CloudflareAIGateway.configure({ accountId }).decision("typesafe/jev")
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
OpenAI and Vercel AI Gateway use OpenAI's `/v1/decisions` API. TypeSafe, OpenCode, OpenRouter, Baseten and
|
|
289
|
+
DigitalOcean use System One (`/systemone`), and Cloudflare sends the System One body through `/ai/run`.
|
|
190
290
|
|
|
191
|
-
|
|
192
|
-
|
|
291
|
+
### Extraction (experimental)
|
|
292
|
+
|
|
293
|
+
Extraction models read typed values out of text instead of answering questions with probabilities. A field of type
|
|
294
|
+
`text`, `number`, `date`, `time`, `datetime`, `duration`, `list`, `estimate` or `forecast` gets an answer typed to
|
|
295
|
+
match; other types are sent and answered as-is. Only Cloudflare's Clairic serves it today, so it lives under
|
|
296
|
+
`@opencode/ai/experimental`.
|
|
297
|
+
|
|
298
|
+
```ts
|
|
299
|
+
import { ai } from "@opencode/ai"
|
|
300
|
+
import { Extraction } from "@opencode/ai/experimental"
|
|
301
|
+
import { CloudflareWorkersAI } from "@opencode/ai/providers"
|
|
302
|
+
|
|
303
|
+
const response = await ai.run(
|
|
304
|
+
Extraction.generate({
|
|
305
|
+
model: CloudflareWorkersAI.configure({ accountId }).experimental.extraction("@cf/cloudflare/clairic"),
|
|
306
|
+
input: "Invoice INV-2291, issued 2026-09-02, net 30. Total $1,080.00. Contact: Dana Ruiz, Priya Shah.",
|
|
307
|
+
fields: {
|
|
308
|
+
invoiceNumber: { type: "text" },
|
|
309
|
+
dueDate: { type: "date", instructions: "Issue date plus the net payment days" },
|
|
310
|
+
total: { type: "number", decimals: 2, unit: "USD" },
|
|
311
|
+
contacts: { type: "list", items: "text" },
|
|
312
|
+
},
|
|
313
|
+
}),
|
|
314
|
+
)
|
|
315
|
+
response.answers.total.value // 1080, or null when the input does not say
|
|
193
316
|
```
|
|
194
317
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
the provider's rounded probabilities are returned unchanged.
|
|
318
|
+
Value answers carry `guess`, `nullProbability` and `confidence`; text answers carry a `span` into `input`. Estimates and
|
|
319
|
+
forecasts return `median`, `p10` and `p90`. Field options other than `type`, `instructions` and `nullThreshold` go to
|
|
320
|
+
Clairic as written. Effect users provide `RequestExecutor.fetchLayer` (or `AIClient.layer`). Images are not supported
|
|
321
|
+
yet.
|
|
200
322
|
|
|
201
323
|
## Alibaba Cloud Model Studio
|
|
202
324
|
|
|
@@ -205,7 +327,7 @@ Chat Completions (`.model` or `.chat`), Anthropic-compatible Messages (`.message
|
|
|
205
327
|
Responses (`.responses`). These routes use HTTP/SSE.
|
|
206
328
|
|
|
207
329
|
```ts
|
|
208
|
-
import { LLM } from "@opencode/ai"
|
|
330
|
+
import { LLM } from "@opencode/ai/effect"
|
|
209
331
|
import { Alibaba } from "@opencode/ai/providers"
|
|
210
332
|
|
|
211
333
|
const alibaba = Alibaba.configure({
|
|
@@ -270,13 +392,22 @@ Package entrypoints are `@opencode/ai/providers/alibaba`, `alibaba/chat`, `aliba
|
|
|
270
392
|
and `alibaba/responses`. Live recordings cover all three APIs in Singapore; regional URL construction
|
|
271
393
|
is unit-tested for all six regions.
|
|
272
394
|
|
|
395
|
+
### Images
|
|
396
|
+
|
|
397
|
+
`alibaba.image(...)` uses the native multimodal generation API (`/api/v1/services/aigc/multimodal-generation/generation`)
|
|
398
|
+
for Qwen-Image (`qwen-image-2.0`, `qwen-image-2.0-pro`) and Wan (`wan2.7-image`, `wan2.7-image-pro`). `aspect` and
|
|
399
|
+
`resolution` lower to a `W*H` size, `providerOptions.size` sets it exactly, and `images` are sent as URLs or data URLs
|
|
400
|
+
for editing. Native `parameters` include `size`, `seed`, `negativePrompt`, `promptExtend`, `watermark`, `thinkingMode`,
|
|
401
|
+
and `enableSequential`; raw keys such as `bbox_list` pass through. Results are URLs that Model Studio keeps for 24
|
|
402
|
+
hours. A complete `baseURL` for images includes `/api/v1`.
|
|
403
|
+
|
|
273
404
|
## Z.AI
|
|
274
405
|
|
|
275
406
|
`ZAI` uses the standard API. Chat Completions is the default language-model API;
|
|
276
407
|
the existing `.image(...)` selector provides image generation.
|
|
277
408
|
|
|
278
409
|
```ts
|
|
279
|
-
import { LLM } from "@opencode/ai"
|
|
410
|
+
import { LLM } from "@opencode/ai/effect"
|
|
280
411
|
import { ZAI, ZAICodingPlan } from "@opencode/ai/providers"
|
|
281
412
|
|
|
282
413
|
const zai = ZAI.configure({ apiKey: process.env.ZAI_API_KEY })
|
|
@@ -342,7 +473,7 @@ Package entrypoints are `@opencode/ai/providers/zai`, `zai/chat`, `zai-coding-pl
|
|
|
342
473
|
Moonshot defaults to Chat Completions, with Messages and Responses selectors for Kimi K3:
|
|
343
474
|
|
|
344
475
|
```ts
|
|
345
|
-
import { LLM } from "@opencode/ai"
|
|
476
|
+
import { LLM } from "@opencode/ai/effect"
|
|
346
477
|
import { Moonshot } from "@opencode/ai/providers"
|
|
347
478
|
|
|
348
479
|
const moonshot = Moonshot.configure({ apiKey: process.env.MOONSHOT_API_KEY })
|
|
@@ -404,7 +535,7 @@ MiniMax defaults to its Messages API and reads `MINIMAX_API_KEY` when `apiKey` i
|
|
|
404
535
|
|
|
405
536
|
```ts
|
|
406
537
|
import { Effect } from "effect"
|
|
407
|
-
import { AIClient, LLM } from "@opencode/ai"
|
|
538
|
+
import { AIClient, LLM } from "@opencode/ai/effect"
|
|
408
539
|
import { MiniMax } from "@opencode/ai/providers"
|
|
409
540
|
|
|
410
541
|
const minimax = MiniMax.configure({ apiKey: process.env.MINIMAX_API_KEY })
|
|
@@ -481,7 +612,7 @@ citations or separate result blocks. Retain `response.message` for either API's
|
|
|
481
612
|
Use `Image.generate` for one-off generation or editing:
|
|
482
613
|
|
|
483
614
|
```ts
|
|
484
|
-
import { Image, Media } from "@opencode/ai"
|
|
615
|
+
import { Image, Media } from "@opencode/ai/effect"
|
|
485
616
|
|
|
486
617
|
const generation = Image.generate({
|
|
487
618
|
model: meta.image("muse-image-1.0"),
|
|
@@ -494,13 +625,13 @@ const edit = Image.generate({
|
|
|
494
625
|
model: meta.image("muse-image-1.0"),
|
|
495
626
|
prompt: "Make the square purple.",
|
|
496
627
|
images: [Media.bytes(imageBytes, "image/webp")],
|
|
497
|
-
|
|
498
|
-
providerOptions: { reasoningStrength: "low" },
|
|
628
|
+
providerOptions: { reasoningStrength: "low", outputFormat: "png" },
|
|
499
629
|
})
|
|
500
630
|
```
|
|
501
631
|
|
|
502
|
-
The default image format is WEBP; `
|
|
503
|
-
returns a signed URL. `
|
|
632
|
+
The default image format is WEBP; `outputFormat` also accepts PNG/JPEG and `responseFormat: "url"`
|
|
633
|
+
returns a signed URL. `aspect` lowers to Meta's `size`, which is only an aspect-ratio hint: Meta renders about 2.5
|
|
634
|
+
megapixels whatever you ask, so `resolution` is ignored. For conversational images, select
|
|
504
635
|
`meta.responses("muse-image-1.0")` with `tools: [Meta.imageGeneration({ reasoningStrength: "low" })]`.
|
|
505
636
|
Generated images are provider-executed tool results with file content. Retain `response.message` to
|
|
506
637
|
replay the signed image handle on the next request. Muse Image accepts only the `image_generation` tool.
|
|
@@ -512,11 +643,12 @@ Meta Responses is explicitly HTTP/SSE-only and does not use WebSockets, even whe
|
|
|
512
643
|
|
|
513
644
|
Use `Image.generate` with an image model for direct asset generation. `Image.request` mirrors `LLM.request`: the
|
|
514
645
|
model comes from the facade's `.image(...)` selector (mirroring `.responses(...)`), common fields
|
|
515
|
-
(`images`, `
|
|
516
|
-
`
|
|
646
|
+
(`images`, `n`, `aspect`, `resolution`) lower natively or fail typed, and `providerOptions` is inferred from the
|
|
647
|
+
selected model. `aspect` is a ratio such as `"16:9"`; `resolution` is a size tier (`"1K"` is about 1024×1024 pixels
|
|
648
|
+
at that ratio, `"2K"` about 2048×2048). Exact sizes, seeds, masks, and output formats are provider options:
|
|
517
649
|
|
|
518
650
|
```ts
|
|
519
|
-
import { Image, Media } from "@opencode/ai"
|
|
651
|
+
import { Image, Media } from "@opencode/ai/effect"
|
|
520
652
|
import { OpenAI } from "@opencode/ai/providers"
|
|
521
653
|
|
|
522
654
|
const openai = OpenAI.configure({ apiKey: process.env.OPENAI_API_KEY })
|
|
@@ -526,9 +658,10 @@ const program = Effect.gen(function* () {
|
|
|
526
658
|
model: openai.image("gpt-image-2"),
|
|
527
659
|
prompt: "A robot tending a rooftop garden",
|
|
528
660
|
n: 2,
|
|
529
|
-
|
|
530
|
-
|
|
661
|
+
aspect: "16:9",
|
|
662
|
+
resolution: "2K",
|
|
531
663
|
providerOptions: {
|
|
664
|
+
outputFormat: "webp",
|
|
532
665
|
quality: "high", // inferred from the OpenAI image model
|
|
533
666
|
future_option: true, // unknown native options pass through unchanged
|
|
534
667
|
},
|
|
@@ -538,24 +671,34 @@ const program = Effect.gen(function* () {
|
|
|
538
671
|
})
|
|
539
672
|
```
|
|
540
673
|
|
|
541
|
-
Common fields are portable in shape, not in support.
|
|
542
|
-
call rather than
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
|
546
|
-
|
|
|
547
|
-
|
|
|
548
|
-
|
|
|
549
|
-
|
|
|
550
|
-
| Meta | ✓ | ✓ (hint)
|
|
551
|
-
|
|
|
552
|
-
|
|
|
553
|
-
|
|
|
554
|
-
|
|
|
555
|
-
|
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
674
|
+
Common fields are portable in shape, not in support. Some routes approximate `aspect` or `resolution` with the nearest
|
|
675
|
+
size they offer; others fail with a typed `AIError` before any network call rather than dropping them, so check this
|
|
676
|
+
table before swapping only the `model`:
|
|
677
|
+
|
|
678
|
+
| Provider | `n` | `aspect` | `resolution` | `images` | Moved to `providerOptions` |
|
|
679
|
+
| --------------------- | --- | -------------------- | --------------------- | -------------------------------- | -------------------------------------------------- |
|
|
680
|
+
| OpenAI | ✓¹ | nearest size | nearest size | ✓ | `size`, `outputFormat`, `mask` |
|
|
681
|
+
| Google (Gemini) | 1 | ✓ | ✓ (`imageSize`) | ✓ (no public URLs) | `seed` |
|
|
682
|
+
| xAI | ✓ | ✓ | `1k`/`2k` | ✓ | none |
|
|
683
|
+
| Meta | ✓ | ✓ (hint) | ignored | ✓ | `size`, `outputFormat` |
|
|
684
|
+
| Z.ai | ✗ | width × height | width × height | ✗ | `size` |
|
|
685
|
+
| Alibaba | ✓ | width × height | width × height | ✓ | `size`, `seed` |
|
|
686
|
+
| Black Forest Labs | 1 | per model | width × height models | per model (1–8) | `width`, `height`, `seed`, `output_format`, `mask` |
|
|
687
|
+
| fal | ✓ | per model | per model | 1 (several on `/edit`, `/multi`) | `image_size`, `seed`, `output_format`, `mask` |
|
|
688
|
+
| Replicate | ✗ | ✗ | ✗ | ✗ (use `providerOptions`) | everything |
|
|
689
|
+
| Stability `image` | 1 | ✓ | ✗ | 1 (not on `core`) | `seed`, `output_format` |
|
|
690
|
+
| Stability `upscale()` | ✗ | ✗ | ✗ | exactly 1 (required) | `seed`, `output_format` |
|
|
691
|
+
| Vercel AI Gateway | ✓ | as OpenAI³ | as OpenAI³ | per model | as OpenAI |
|
|
692
|
+
| Cloudflare Workers AI | 1 | width × height² | width × height² | ✗ | `width`, `height`, `seed` |
|
|
693
|
+
|
|
694
|
+
✓ lowers natively; ✗ fails whenever the field is set (including `n: 1`); `1` means `n > 1` fails. "width × height"
|
|
695
|
+
computes a size the provider accepts and fails when the request falls outside its limits. ¹ `Image.stream` on OpenAI
|
|
696
|
+
generates one image. On OpenAI, `gpt-image-2` and later get a computed size; older models get the nearest of their
|
|
697
|
+
fixed sizes. `providerOptions.size` replaces `aspect` and `resolution`, so setting both fails. Vercel AI Gateway
|
|
698
|
+
forwards `outputFormat` but does not apply it, so the returned bytes keep the upstream model's own format. ² Workers AI
|
|
699
|
+
`@cf/black-forest-labs/flux-1-schnell` takes no size, and the provider rejects it. ³ Non-OpenAI gateway models
|
|
700
|
+
(`bfl/flux-2-pro`) get a computed width × height, which the gateway passes through; models with a fixed list, such as
|
|
701
|
+
Recraft, reject sizes outside it.
|
|
559
702
|
|
|
560
703
|
`Media.Asset` is the one asset type shared by image requests, image responses, LLM messages, and tool results.
|
|
561
704
|
`asset.source` is the serializable `Media.Source` (`bytes`, `base64`, `url`, or `ref`); `asset.bytes()`,
|
|
@@ -587,15 +730,15 @@ only forward refs that belong to their own provider (OpenAI, xAI, and Gemini ima
|
|
|
587
730
|
returns a ref yet, and `asset.bytes()` / `materialize()` on a ref fail by design. Raw strings are not accepted as
|
|
588
731
|
image inputs, avoiding ambiguity between base64, URLs, and provider IDs. Empty or omitted `images` uses text-to-image generation; a
|
|
589
732
|
non-empty array selects the provider's edit behavior (see the table above for routes that limit the count). OpenAI
|
|
590
|
-
uses multipart for byte/data-URL edits and its JSON reference body for URL or file-ID edits.
|
|
591
|
-
|
|
733
|
+
uses multipart for byte/data-URL edits and its JSON reference body for URL or file-ID edits. Inpainting is a
|
|
734
|
+
provider option on the routes that support it (OpenAI, fal, and BFL `flux-pro-1.0-fill`):
|
|
592
735
|
|
|
593
736
|
```ts
|
|
594
737
|
const inpainted = Image.generate({
|
|
595
738
|
model: openai.image("gpt-image-2"),
|
|
596
739
|
prompt,
|
|
597
740
|
images: [Media.bytes(sourceBytes, "image/png")],
|
|
598
|
-
mask: Media.bytes(maskBytes, "image/png"),
|
|
741
|
+
providerOptions: { mask: Media.bytes(maskBytes, "image/png") },
|
|
599
742
|
})
|
|
600
743
|
```
|
|
601
744
|
|
|
@@ -624,9 +767,9 @@ const xai = Image.generate({
|
|
|
624
767
|
model: XAI.configure({ apiKey }).image("any-model-id"),
|
|
625
768
|
prompt,
|
|
626
769
|
n: 2,
|
|
627
|
-
|
|
770
|
+
aspect: "16:9",
|
|
771
|
+
resolution: "1K",
|
|
628
772
|
providerOptions: {
|
|
629
|
-
resolution: "1k",
|
|
630
773
|
responseFormat: "b64_json",
|
|
631
774
|
future_option: true,
|
|
632
775
|
},
|
|
@@ -643,10 +786,10 @@ const googleProgram = Effect.gen(function* () {
|
|
|
643
786
|
const response = yield* Image.generate({
|
|
644
787
|
model: Google.configure({ apiKey }).image("any-model-id"),
|
|
645
788
|
prompt: "A robot tending a rooftop garden",
|
|
646
|
-
|
|
647
|
-
|
|
789
|
+
aspect: "16:9",
|
|
790
|
+
resolution: "2K",
|
|
648
791
|
providerOptions: {
|
|
649
|
-
|
|
792
|
+
seed: 42,
|
|
650
793
|
thinkingLevel: "HIGH",
|
|
651
794
|
includeThoughts: true,
|
|
652
795
|
futureOption: true,
|
|
@@ -691,7 +834,7 @@ and emits `image-partial` events before each final `image`; `Image.generate` kee
|
|
|
691
834
|
|
|
692
835
|
```ts
|
|
693
836
|
import { Stream } from "effect"
|
|
694
|
-
import { ImageEvent } from "@opencode/ai"
|
|
837
|
+
import { ImageEvent } from "@opencode/ai/effect"
|
|
695
838
|
|
|
696
839
|
const previews = Image.stream({
|
|
697
840
|
model: openai.image("gpt-image-2"),
|
|
@@ -714,7 +857,7 @@ import { BlackForestLabs, Stability } from "@opencode/ai/providers"
|
|
|
714
857
|
const bfl = BlackForestLabs.configure({ apiKey: process.env.BFL_API_KEY })
|
|
715
858
|
|
|
716
859
|
const submit = Effect.gen(function* () {
|
|
717
|
-
const generation = yield* Image.start({ model: bfl.image("flux-2-pro"), prompt,
|
|
860
|
+
const generation = yield* Image.start({ model: bfl.image("flux-2-pro"), prompt, aspect: "4:3" })
|
|
718
861
|
persist({ provider: "black-forest-labs", modelID: "flux-2-pro", token: generation.token })
|
|
719
862
|
})
|
|
720
863
|
|
|
@@ -770,12 +913,12 @@ The hosted result is represented as a provider-executed tool call and a tool res
|
|
|
770
913
|
|
|
771
914
|
Video mirrors `Image` with one difference: every provider is asynchronous, so the route is a submit-then-poll
|
|
772
915
|
`Generation`. Models come from `.video(...)` selectors on the `Google` (Veo), `XAI`, `Fal`, and `Runway` facades.
|
|
773
|
-
Common fields (`frames`, `references`, `video`, `durationSeconds`, `
|
|
916
|
+
Common fields (`frames`, `references`, `video`, `durationSeconds`, `aspect`, `resolution`, `audio`, `n`, `seed`,
|
|
774
917
|
`negativePrompt`) lower natively or fail with a typed `AIError` before any network call; provider-native controls live
|
|
775
918
|
under `providerOptions`, inferred from the selected model.
|
|
776
919
|
|
|
777
920
|
```ts
|
|
778
|
-
import { Video } from "@opencode/ai"
|
|
921
|
+
import { Video } from "@opencode/ai/effect"
|
|
779
922
|
import { Google, Runway } from "@opencode/ai/providers"
|
|
780
923
|
|
|
781
924
|
const google = Google.configure({ apiKey: process.env.GOOGLE_GENERATIVE_AI_API_KEY })
|
|
@@ -786,7 +929,7 @@ const program = Effect.gen(function* () {
|
|
|
786
929
|
{
|
|
787
930
|
model: google.video("veo-3.1-generate-preview"),
|
|
788
931
|
prompt: "Panning wide shot of a calico kitten sleeping in the sunshine",
|
|
789
|
-
|
|
932
|
+
aspect: "16:9",
|
|
790
933
|
resolution: "1080p",
|
|
791
934
|
durationSeconds: 8,
|
|
792
935
|
providerOptions: { personGeneration: "allow_adult" },
|
|
@@ -823,8 +966,9 @@ failures (rate limits, provider 5xx, network errors) with backoff that honors `r
|
|
|
823
966
|
`poll.timeout`; submits and cancels never retry. Interrupting a wait (or aborting its `signal`) does not cancel the
|
|
824
967
|
provider job, which keeps running and billing: call `cancel()` to stop it. Failed,
|
|
825
968
|
cancelled, and expired generations fail typed with the provider's terminal document on `reason.body`; moderation
|
|
826
|
-
outcomes (Veo `raiMediaFilteredReasons`, xAI `respect_moderation`, Runway `SAFETY.*` codes)
|
|
827
|
-
|
|
969
|
+
outcomes (Veo `raiMediaFilteredReasons`, xAI `respect_moderation`, Runway `SAFETY.*` codes) fail with a
|
|
970
|
+
`ContentPolicy` reason when no video comes back; when one still does, Veo and xAI keep the raw outcome in
|
|
971
|
+
`providerMetadata`.
|
|
828
972
|
|
|
829
973
|
Provider notes:
|
|
830
974
|
|
|
@@ -836,7 +980,7 @@ Provider notes:
|
|
|
836
980
|
- **fal** endpoints are model-specific: `durationSeconds`, `references`, and `frames.last` fail typed and belong in
|
|
837
981
|
`providerOptions` under the model's own names (`duration: "8s"`, `end_image_url`, …). Auth is
|
|
838
982
|
`Authorization: Key <FAL_KEY>`.
|
|
839
|
-
- **Runway** expects pixel ratios in `
|
|
983
|
+
- **Runway** expects pixel ratios in `aspect` for most models (`"1280:720"`), pins `X-Runway-Version`, reports
|
|
840
984
|
`usage: { type: "credits" }`, and its output URLs expire after 24–48 hours.
|
|
841
985
|
|
|
842
986
|
The promise client exposes the same surface: `ai.video.start(...)` resolves to a handle with `await`, `events`,
|
|
@@ -847,7 +991,7 @@ aborting rejects the Promise or throws from the `for await` loop with `signal.re
|
|
|
847
991
|
unless `abort(reason)` passed one), while `break` stops a stream without throwing.
|
|
848
992
|
|
|
849
993
|
```ts
|
|
850
|
-
import { ai } from "@opencode/ai
|
|
994
|
+
import { ai } from "@opencode/ai"
|
|
851
995
|
|
|
852
996
|
const generation = await ai.video.start({ model, prompt })
|
|
853
997
|
for await (const event of generation.events({ poll: { interval: 10_000 } })) console.log(event.type)
|
|
@@ -864,7 +1008,7 @@ selectors on the `OpenAI`, `Google` (Gemini TTS), `ElevenLabs`, `Cartesia`, and
|
|
|
864
1008
|
before any network call; provider-native controls live under `providerOptions`, inferred from the selected model.
|
|
865
1009
|
|
|
866
1010
|
```ts
|
|
867
|
-
import { Media, Speech, SpeechClient, SpeechEvent } from "@opencode/ai"
|
|
1011
|
+
import { Media, Speech, SpeechClient, SpeechEvent } from "@opencode/ai/effect"
|
|
868
1012
|
import { ElevenLabs, OpenAI } from "@opencode/ai/providers"
|
|
869
1013
|
|
|
870
1014
|
const openai = OpenAI.configure({ apiKey: process.env.OPENAI_API_KEY })
|
|
@@ -929,7 +1073,7 @@ Provider notes:
|
|
|
929
1073
|
The promise client mirrors the Effect API; `ai.speech.stream` is an `AsyncIterable`.
|
|
930
1074
|
|
|
931
1075
|
```ts
|
|
932
|
-
import { ai } from "@opencode/ai
|
|
1076
|
+
import { ai } from "@opencode/ai"
|
|
933
1077
|
|
|
934
1078
|
const response = await ai.speech.generate({ model, text: "Hello from OpenCode.", voice: "coral" })
|
|
935
1079
|
await ai.write(response.audio, "hello.mp3")
|
|
@@ -951,7 +1095,7 @@ any network call; a route may return more than asked.
|
|
|
951
1095
|
|
|
952
1096
|
```ts
|
|
953
1097
|
import { Console, Effect, Stream } from "effect"
|
|
954
|
-
import { Media, Transcription, TranscriptionEvent } from "@opencode/ai"
|
|
1098
|
+
import { Media, Transcription, TranscriptionEvent } from "@opencode/ai/effect"
|
|
955
1099
|
import { AssemblyAI, Deepgram, OpenAI } from "@opencode/ai/providers"
|
|
956
1100
|
|
|
957
1101
|
const openai = OpenAI.configure({ apiKey: process.env.OPENAI_API_KEY })
|
|
@@ -1012,8 +1156,11 @@ const transcript = await generation.await({ poll: { interval: 3_000 } })
|
|
|
1012
1156
|
|
|
1013
1157
|
## Public API
|
|
1014
1158
|
|
|
1159
|
+
Everything below is exported from `@opencode/ai/effect` unless noted.
|
|
1160
|
+
|
|
1015
1161
|
- **`LLM.request({...})`** — build a provider-neutral `LLMRequest`. Accepts ergonomic inputs (`system: string`, `prompt: string`) that normalize into the canonical Schema classes.
|
|
1016
1162
|
- **`LLM.generate` / `LLM.stream`** — run direct input or an `LLMRequest` through `LLMClient` for one-import use.
|
|
1163
|
+
- **`LLM.loop`** — stream model calls, running `Tool.make(...)` tools between them until the model stops calling tools.
|
|
1017
1164
|
- **`Message.user(...)` / `Message.assistant(...)` / `Message.tool(...)`** — message constructors from the canonical schema model.
|
|
1018
1165
|
- **`LanguageModel.make(...)` / `ToolCallPart.make(...)` / `ToolResultPart.make(...)` / `ToolDefinition.make(...)`** — model and tool-related constructors from the canonical schema model.
|
|
1019
1166
|
- **`LLMEvent.is.*`** — typed guards (`is.textDelta`, `is.toolCall`, `is.finish`, …) for filtering streams.
|
|
@@ -1024,8 +1171,10 @@ const transcript = await generation.await({ poll: { interval: 3_000 } })
|
|
|
1024
1171
|
- **`Video.request` / `generate` / `stream` / `start` / `resume`** — queued video generation through a provider-neutral request; `VideoClient` is its Effect service and layer.
|
|
1025
1172
|
- **`Speech.request` / `Speech.generate` / `Speech.stream`** — text-to-speech through a provider-neutral request; `SpeechClient` is its Effect service and layer.
|
|
1026
1173
|
- **`Transcription.request` / `generate` / `stream` / `start` / `resume`** — speech-to-text over inline, streaming, and queued routes; `TranscriptionClient` is its Effect service and layer.
|
|
1174
|
+
- **`Decision.request` / `Decision.generate`** — typed predicate, choice, and score questions over text, JSON, and media input; `DecisionClient` is its Effect service and layer.
|
|
1027
1175
|
- **`AIClient.layer` / `AIClient.layerWith(executor)`** — every modality client plus the request executor in one layer.
|
|
1028
|
-
- **`@opencode/ai
|
|
1176
|
+
- **`@opencode/ai`** (Promise root) — `AI.make({ layer? })` and a default `ai` client exposing `llm`, `image`, `video`, `speech`, `transcription`, and `decision` as Promise / `AsyncIterable` APIs, `run(effect)` for any package Effect, plus `file`, `write`, `bytes`, `base64`, and `materialize` for assets.
|
|
1177
|
+
- **`@opencode/ai/experimental`** — APIs that may still change: `Extraction` (typed values read out of text) and `Realtime` (`Realtime.connect` starts an OpenAI, xAI, or Gemini voice session and returns a WebRTC answer SDP or a short-lived WebSocket URL and token).
|
|
1029
1178
|
|
|
1030
1179
|
## Testing
|
|
1031
1180
|
|
|
@@ -1150,7 +1299,7 @@ Enable OpenAI compaction with typed provider options:
|
|
|
1150
1299
|
|
|
1151
1300
|
```ts
|
|
1152
1301
|
import { Effect } from "effect"
|
|
1153
|
-
import { LLM, LLMClient, LLMRequest, Message } from "@opencode/ai"
|
|
1302
|
+
import { LLM, LLMClient, LLMRequest, Message } from "@opencode/ai/effect"
|
|
1154
1303
|
import { OpenAI } from "@opencode/ai/providers"
|
|
1155
1304
|
|
|
1156
1305
|
const request = LLM.request({
|
|
@@ -1173,7 +1322,7 @@ const continued = Effect.gen(function* () {
|
|
|
1173
1322
|
A compaction part has `provider` and exactly one representation: `encrypted` for Responses, or `text` for Anthropic. Responses also preserves the optional checkpoint `id`. These fields survive message serialization without becoming visible assistant text. Sending a checkpoint to another provider or an incompatible API fails rather than silently losing context.
|
|
1174
1323
|
|
|
1175
1324
|
```ts
|
|
1176
|
-
import { CompactionPart, ProviderID } from "@opencode/ai"
|
|
1325
|
+
import { CompactionPart, ProviderID } from "@opencode/ai/effect"
|
|
1177
1326
|
|
|
1178
1327
|
CompactionPart.make({ provider: ProviderID.make("openai"), id: "cmp_123", encrypted: "..." })
|
|
1179
1328
|
CompactionPart.make({ provider: ProviderID.make("anthropic"), text: "Summary of the conversation..." })
|
|
@@ -1244,13 +1393,33 @@ cache: {
|
|
|
1244
1393
|
tools?: boolean,
|
|
1245
1394
|
system?: boolean,
|
|
1246
1395
|
messages?: "latest-user-message" | "latest-assistant" | { tail: number },
|
|
1247
|
-
|
|
1396
|
+
ttl?: CacheTTL, // "5m", "30m", "1h", "12h", "24h", ...
|
|
1248
1397
|
}
|
|
1249
1398
|
```
|
|
1250
1399
|
|
|
1400
|
+
An object with placement fields places exactly those breakpoints. An object with only `ttl` keeps automatic placement.
|
|
1401
|
+
|
|
1402
|
+
### Cache lifetime
|
|
1403
|
+
|
|
1404
|
+
`ttl` is a duration in models.dev's format (`<number>` followed by `ms`, `s`, `m`, `h` or `d`). It is sent to the provider as given, so use one the provider accepts. Leaving it unset keeps the provider default.
|
|
1405
|
+
|
|
1406
|
+
```ts
|
|
1407
|
+
LLM.request({ model, system, prompt, cache: { ttl: "1h" } })
|
|
1408
|
+
```
|
|
1409
|
+
|
|
1410
|
+
| Protocol | How `ttl` is sent |
|
|
1411
|
+
| ------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------ |
|
|
1412
|
+
| Anthropic Messages family, Meta Messages | `ttl` on every `cache_control` marker |
|
|
1413
|
+
| Bedrock Converse | `ttl` on every `cachePoint`; older Claude releases that only cache for 5m drop `"1h"` |
|
|
1414
|
+
| OpenRouter, DigitalOcean, Alibaba Chat markers | `ttl` on every `cache_control` marker |
|
|
1415
|
+
| OpenAI Responses (OpenAI, Azure, Cloudflare AI Gateway) | `"24h"` → `prompt_cache_retention: "24h"`; anything else → `prompt_cache_options: { ttl }` |
|
|
1416
|
+
| Chat Completions without markers (OpenAI-compatible, Moonshot, DeepInfra) | same mapping as OpenAI Responses |
|
|
1417
|
+
|
|
1418
|
+
OpenAI's two fields cover different models: `prompt_cache_retention` (`"in_memory"` or `"24h"`) is the only control before GPT-5.6, and GPT-5.6+ reads `prompt_cache_options.ttl` (`"30m"` or `"12h"`). Sending `prompt_cache_options` to an older model returns a 400. `"in_memory"` is not a duration; send it with an HTTP body overlay.
|
|
1419
|
+
|
|
1251
1420
|
### Manual hints
|
|
1252
1421
|
|
|
1253
|
-
Inline `CacheHint` on any text / system / tool / tool-result part overrides automatic placement. The auto policy preserves manual hints, counts them against Anthropic and Bedrock's four-breakpoint limit, and only fills the remaining slots.
|
|
1422
|
+
Inline `CacheHint` (`{ type: "ephemeral", ttl? }`) on any text / system / tool / tool-result part overrides automatic placement. The auto policy preserves manual hints, counts them against Anthropic and Bedrock's four-breakpoint limit, and only fills the remaining slots.
|
|
1254
1423
|
|
|
1255
1424
|
```ts
|
|
1256
1425
|
LLM.request({
|
|
@@ -1283,12 +1452,12 @@ import { OpenAI, CloudflareAIGateway } from "@opencode/ai/providers"
|
|
|
1283
1452
|
|
|
1284
1453
|
const openai = OpenAI.configure({ apiKey: process.env.OPENAI_API_KEY }).responses("gpt-4o-mini")
|
|
1285
1454
|
const gateway = CloudflareAIGateway.configure({
|
|
1286
|
-
accountId
|
|
1455
|
+
accountId,
|
|
1287
1456
|
gatewayApiKey: process.env.CLOUDFLARE_API_TOKEN,
|
|
1288
1457
|
}).model("workers-ai/@cf/meta/llama-3.1-8b-instruct")
|
|
1289
1458
|
```
|
|
1290
1459
|
|
|
1291
|
-
Included LLM providers: OpenAI, Anthropic, Google (Gemini), Google Vertex, Amazon Bedrock, Azure OpenAI, Baseten, Cerebras, Cohere, Cloudflare AI Gateway, Cloudflare Workers AI, DeepInfra, DeepSeek, Fireworks, Groq, Mistral, OpenRouter, TogetherAI, Vercel AI Gateway,
|
|
1460
|
+
Included LLM providers: OpenAI, Anthropic, Google (Gemini), Google Vertex, Amazon Bedrock (Converse and Mantle), Azure OpenAI, Alibaba, Baseten, Cerebras, Cohere, Cloudflare AI Gateway, Cloudflare Workers AI, DeepInfra, DeepSeek, DigitalOcean, Fireworks, Groq, Meta, MiniMax, Mistral, Moonshot, OpenRouter, TogetherAI, Venice, Vercel AI Gateway, xAI, Z.ai, and Z.ai Coding Plan. TypeSafe AI and OpenCode expose decision models only. Generic Chat Completions, Responses, and Anthropic Messages-compatible entrypoints support custom endpoints.
|
|
1292
1461
|
|
|
1293
1462
|
Each named provider owns its module, endpoint, authentication, and route setup. Providers with the same wire format compose the shared protocol directly:
|
|
1294
1463
|
|
|
@@ -1393,10 +1562,10 @@ Compose a route with `Route.make({ protocol, endpoint, auth, framing, ... })`. T
|
|
|
1393
1562
|
|
|
1394
1563
|
## Effect
|
|
1395
1564
|
|
|
1396
|
-
This package is built on Effect. Public methods return `Effect` or `Stream`; provide `AIClient.layer` (or `AIClient.layerWith(executor)`) for every modality, then import the provider/protocol modules for the routes you use. The example at `example/tutorial.ts` is a runnable walkthrough.
|
|
1565
|
+
This package is built on Effect. Public methods in `@opencode/ai/effect` return `Effect` or `Stream`; provide `AIClient.layer` (or `AIClient.layerWith(executor)`) for every modality, then import the provider/protocol modules for the routes you use. The example at `example/tutorial.ts` is a runnable walkthrough.
|
|
1397
1566
|
|
|
1398
1567
|
## See also
|
|
1399
1568
|
|
|
1400
1569
|
- `AGENTS.md` — architecture, route construction, contributor guide
|
|
1401
|
-
- `example/tutorial.ts` — runnable end-to-end walkthrough
|
|
1570
|
+
- `example/tutorial.ts` — runnable end-to-end walkthrough (`bun example/tutorial.ts <example>`)
|
|
1402
1571
|
- `test/provider/*.test.ts` — fixture-first protocol tests; `*.recorded.test.ts` files cover live cassettes
|