@opencode/ai 0.0.0-dev-377 → 0.0.0-dev-393
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 +75 -76
- package/dist/effect.d.ts +45 -3
- package/dist/effect.js +24 -3
- package/dist/index.d.ts +468 -1158
- package/dist/index.js +22 -20
- package/dist/llm.d.ts +2 -1
- package/dist/llm.js +2 -0
- package/dist/protocols/xai-responses.js +1 -1
- package/dist/route/client.d.ts +0 -2
- package/dist/route/client.js +1 -5
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -7,34 +7,33 @@ realtime voice are [experimental](#experimental).
|
|
|
7
7
|
and `ai.write` load `node:fs/promises` on first use, so no Effect `FileSystem` is needed:
|
|
8
8
|
|
|
9
9
|
```ts
|
|
10
|
-
import {
|
|
10
|
+
import { ai } from "@opencode/ai"
|
|
11
11
|
import { OpenAI } from "@opencode/ai/providers"
|
|
12
12
|
|
|
13
13
|
const openai = OpenAI.configure({ apiKey: process.env.OPENAI_API_KEY })
|
|
14
14
|
|
|
15
|
-
const ai = AI.make()
|
|
16
15
|
const input = { model: openai.responses("gpt-4o-mini"), prompt: "Say hello." }
|
|
17
|
-
const response = await ai.
|
|
16
|
+
const response = await ai.generate(input)
|
|
18
17
|
console.log(response.text)
|
|
19
18
|
const generated = await ai.image.generate({ model: openai.image("gpt-image-2"), prompt: "A lighthouse" })
|
|
20
19
|
await ai.write(generated.image, "./lighthouse.png") // also ai.file(path), ai.bytes(asset), ai.base64(asset), ai.materialize(asset)
|
|
21
|
-
for await (const event of ai.
|
|
20
|
+
for await (const event of ai.stream(ai.request(input))) {
|
|
22
21
|
// LLMEvent
|
|
23
22
|
}
|
|
24
|
-
await ai.dispose()
|
|
25
23
|
```
|
|
26
24
|
|
|
27
|
-
The Effect API lives at `@opencode/ai/effect`. Its methods
|
|
25
|
+
The Effect API lives at `@opencode/ai/effect`. Its `ai` has the same namespaces and methods, returning `Effect` or
|
|
26
|
+
`Stream`:
|
|
28
27
|
|
|
29
28
|
```ts
|
|
30
29
|
import { Effect } from "effect"
|
|
31
|
-
import {
|
|
30
|
+
import { ai, AIClient } from "@opencode/ai/effect"
|
|
32
31
|
import { OpenAI } from "@opencode/ai/providers"
|
|
33
32
|
|
|
34
33
|
const openai = OpenAI.configure({ apiKey: process.env.OPENAI_API_KEY })
|
|
35
34
|
|
|
36
35
|
const program = Effect.gen(function* () {
|
|
37
|
-
const response = yield*
|
|
36
|
+
const response = yield* ai.generate({
|
|
38
37
|
model: openai.responses("gpt-4o-mini"), // `.chat(...)` selects the Chat Completions API instead
|
|
39
38
|
system: "You are concise.",
|
|
40
39
|
prompt: "Say hello in one short sentence.",
|
|
@@ -47,8 +46,8 @@ const program = Effect.gen(function* () {
|
|
|
47
46
|
await Effect.runPromise(program.pipe(Effect.provide(AIClient.layer)))
|
|
48
47
|
```
|
|
49
48
|
|
|
50
|
-
Run `
|
|
51
|
-
`
|
|
49
|
+
Run `ai.stream(...)` instead of `generate` when you want incremental `LLMEvent`s. Both accept input or a prebuilt
|
|
50
|
+
`ai.request(...)`. The event stream is provider-neutral — same shape across OpenAI Chat, OpenAI Responses,
|
|
52
51
|
Anthropic Messages, Gemini, Bedrock Converse, and any OpenAI-compatible deployment.
|
|
53
52
|
|
|
54
53
|
### Tool loop
|
|
@@ -63,7 +62,7 @@ them. `tool` takes a plain `async` execute; a throw or rejection goes back to th
|
|
|
63
62
|
```ts
|
|
64
63
|
import { readFile } from "node:fs/promises"
|
|
65
64
|
import { Schema } from "effect"
|
|
66
|
-
import { tool } from "@opencode/ai"
|
|
65
|
+
import { ai, tool } from "@opencode/ai"
|
|
67
66
|
|
|
68
67
|
const read_file = tool({
|
|
69
68
|
description: "Read a UTF-8 file.",
|
|
@@ -71,7 +70,7 @@ const read_file = tool({
|
|
|
71
70
|
execute: ({ path }, { signal }) => readFile(path, { encoding: "utf8", signal }),
|
|
72
71
|
})
|
|
73
72
|
|
|
74
|
-
const events = ai.
|
|
73
|
+
const events = ai.loop({
|
|
75
74
|
model,
|
|
76
75
|
prompt: "Summarize package.json.",
|
|
77
76
|
tools: { read_file },
|
|
@@ -109,7 +108,7 @@ history works with or without `freeform`.
|
|
|
109
108
|
unvalidated `unknown` object:
|
|
110
109
|
|
|
111
110
|
```ts
|
|
112
|
-
const { object } = await ai.
|
|
111
|
+
const { object } = await ai.generateObject({
|
|
113
112
|
model,
|
|
114
113
|
prompt: "Pick a city.",
|
|
115
114
|
schema: Schema.Struct({ city: Schema.String }),
|
|
@@ -124,12 +123,12 @@ are visible too. Raw events are off by default, and `generate` ignores them. Tra
|
|
|
124
123
|
|
|
125
124
|
```ts
|
|
126
125
|
import { Stream } from "effect"
|
|
127
|
-
import {
|
|
126
|
+
import { ai, LLMEvent } from "@opencode/ai/effect"
|
|
128
127
|
|
|
129
128
|
const flagged = (data: unknown) =>
|
|
130
129
|
typeof data === "object" && data !== null && "type" in data && data.type === "moderation.flagged"
|
|
131
130
|
|
|
132
|
-
const events =
|
|
131
|
+
const events = ai.stream({ model, prompt: "Say hello.", includeRaw: true }).pipe(
|
|
133
132
|
Stream.map((event) =>
|
|
134
133
|
event.type === "raw" && flagged(event.data) ? LLMEvent.providerError({ message: "Flagged by moderation" }) : event,
|
|
135
134
|
),
|
|
@@ -148,7 +147,7 @@ live under `providerMetadata.vertex`. The default is still full-history replay w
|
|
|
148
147
|
|
|
149
148
|
```ts
|
|
150
149
|
const model = Google.configure({ apiKey }).interactions("gemini-3.8-flash")
|
|
151
|
-
const response = yield*
|
|
150
|
+
const response = yield* ai.generate({
|
|
152
151
|
model,
|
|
153
152
|
prompt: "Say hello.",
|
|
154
153
|
providerOptions: { thinkingLevel: "low", thinkingSummaries: "auto", store: true },
|
|
@@ -167,14 +166,14 @@ Raw usage is preserved in `usage.providerMetadata.google`. `inputTokens` follows
|
|
|
167
166
|
continuation. Explicit caches, hosted tools, and generated media are not supported by this initial protocol.
|
|
168
167
|
|
|
169
168
|
The same configured facade names image models (and the experimental video, speech, and transcription models).
|
|
170
|
-
`
|
|
169
|
+
`ai.image.generate` resolves the provider's image route from the model and returns `Media.Asset`s with lazily decoded bytes:
|
|
171
170
|
|
|
172
171
|
```ts
|
|
173
172
|
import { NodeFileSystem } from "@effect/platform-node"
|
|
174
|
-
import {
|
|
173
|
+
import { ai, Media } from "@opencode/ai/effect"
|
|
175
174
|
|
|
176
175
|
const image = Effect.gen(function* () {
|
|
177
|
-
const response = yield*
|
|
176
|
+
const response = yield* ai.image.generate({
|
|
178
177
|
model: openai.image("gpt-image-2"),
|
|
179
178
|
prompt: "A robot tending a rooftop garden",
|
|
180
179
|
aspect: "1:1",
|
|
@@ -210,12 +209,12 @@ const everything = AIClient.layerWith(logged) // or AI.make({ layer: logged })
|
|
|
210
209
|
select the same API, and `responses` selects its Responses API; credentials default to `VENICE_API_KEY`.
|
|
211
210
|
|
|
212
211
|
```ts
|
|
213
|
-
import {
|
|
212
|
+
import { ai } from "@opencode/ai/effect"
|
|
214
213
|
import { Venice } from "@opencode/ai/providers"
|
|
215
214
|
import { Effect } from "effect"
|
|
216
215
|
|
|
217
216
|
const program = Effect.gen(function* () {
|
|
218
|
-
const response = yield*
|
|
217
|
+
const response = yield* ai.generate({
|
|
219
218
|
model: Venice.configure({ apiKey: process.env.VENICE_API_KEY }).chat("qwen3-6-27b"),
|
|
220
219
|
prompt: "Explain this design.",
|
|
221
220
|
providerOptions: {
|
|
@@ -246,10 +245,10 @@ values, or a `score` on ordered levels, each with the probability of every optio
|
|
|
246
245
|
|
|
247
246
|
```ts
|
|
248
247
|
import { Effect } from "effect"
|
|
249
|
-
import {
|
|
248
|
+
import { ai, DecisionClient, Media } from "@opencode/ai/effect"
|
|
250
249
|
import { OpenAI } from "@opencode/ai/providers"
|
|
251
250
|
|
|
252
|
-
const program =
|
|
251
|
+
const program = ai.decision.generate({
|
|
253
252
|
model: OpenAI.configure().decision("gpt-6-luna"),
|
|
254
253
|
input: [
|
|
255
254
|
{ type: "text", text: "I was charged twice. Please refund the duplicate payment." },
|
|
@@ -317,7 +316,7 @@ Chat Completions (`.model` or `.chat`), Anthropic-compatible Messages (`.message
|
|
|
317
316
|
Responses (`.responses`). These routes use HTTP/SSE.
|
|
318
317
|
|
|
319
318
|
```ts
|
|
320
|
-
import {
|
|
319
|
+
import { ai } from "@opencode/ai/effect"
|
|
321
320
|
import { Alibaba } from "@opencode/ai/providers"
|
|
322
321
|
|
|
323
322
|
const alibaba = Alibaba.configure({
|
|
@@ -326,7 +325,7 @@ const alibaba = Alibaba.configure({
|
|
|
326
325
|
// workspaceID: "llm-your-workspace", // use a workspace-dedicated endpoint
|
|
327
326
|
})
|
|
328
327
|
|
|
329
|
-
const request =
|
|
328
|
+
const request = ai.request({
|
|
330
329
|
model: alibaba.model("qwen3.8-max"),
|
|
331
330
|
prompt: "Explain this design.",
|
|
332
331
|
providerOptions: { reasoningEffort: "medium" },
|
|
@@ -397,11 +396,11 @@ hours. A complete `baseURL` for images includes `/api/v1`.
|
|
|
397
396
|
the existing `.image(...)` selector provides image generation.
|
|
398
397
|
|
|
399
398
|
```ts
|
|
400
|
-
import {
|
|
399
|
+
import { ai } from "@opencode/ai/effect"
|
|
401
400
|
import { ZAI, ZAICodingPlan } from "@opencode/ai/providers"
|
|
402
401
|
|
|
403
402
|
const zai = ZAI.configure({ apiKey: process.env.ZAI_API_KEY })
|
|
404
|
-
const request =
|
|
403
|
+
const request = ai.request({
|
|
405
404
|
model: zai.model("glm-5.3"), // also zai.chat("glm-5.3")
|
|
406
405
|
prompt: "Explain this design.",
|
|
407
406
|
providerOptions: {
|
|
@@ -411,7 +410,7 @@ const request = LLM.request({
|
|
|
411
410
|
})
|
|
412
411
|
|
|
413
412
|
const coding = ZAICodingPlan.configure({ apiKey: process.env.ZAI_API_KEY })
|
|
414
|
-
const messages =
|
|
413
|
+
const messages = ai.request({
|
|
415
414
|
model: coding.messages("glm-5.3"),
|
|
416
415
|
prompt: "Explain this design.",
|
|
417
416
|
providerOptions: { effort: "high" },
|
|
@@ -463,24 +462,24 @@ Package entrypoints are `@opencode/ai/providers/zai`, `zai/chat`, `zai-coding-pl
|
|
|
463
462
|
Moonshot defaults to Chat Completions, with Messages and Responses selectors for Kimi K3:
|
|
464
463
|
|
|
465
464
|
```ts
|
|
466
|
-
import {
|
|
465
|
+
import { ai } from "@opencode/ai/effect"
|
|
467
466
|
import { Moonshot } from "@opencode/ai/providers"
|
|
468
467
|
|
|
469
468
|
const moonshot = Moonshot.configure({ apiKey: process.env.MOONSHOT_API_KEY })
|
|
470
469
|
|
|
471
|
-
const request =
|
|
470
|
+
const request = ai.request({
|
|
472
471
|
model: moonshot.model("kimi-k3"), // also moonshot.chat("kimi-k3")
|
|
473
472
|
prompt: "Explain the tradeoffs in this design.",
|
|
474
473
|
providerOptions: { reasoningEffort: "high" },
|
|
475
474
|
})
|
|
476
475
|
|
|
477
|
-
const messages =
|
|
476
|
+
const messages = ai.request({
|
|
478
477
|
model: moonshot.messages("kimi-k3"),
|
|
479
478
|
prompt: "Explain the tradeoffs in this design.",
|
|
480
479
|
providerOptions: { effort: "high" },
|
|
481
480
|
})
|
|
482
481
|
|
|
483
|
-
const responses =
|
|
482
|
+
const responses = ai.request({
|
|
484
483
|
model: moonshot.responses("kimi-k3"),
|
|
485
484
|
prompt: "Explain the tradeoffs in this design.",
|
|
486
485
|
providerOptions: { reasoningEffort: "high" },
|
|
@@ -525,18 +524,18 @@ MiniMax defaults to its Messages API and reads `MINIMAX_API_KEY` when `apiKey` i
|
|
|
525
524
|
|
|
526
525
|
```ts
|
|
527
526
|
import { Effect } from "effect"
|
|
528
|
-
import {
|
|
527
|
+
import { ai, AIClient } from "@opencode/ai/effect"
|
|
529
528
|
import { MiniMax } from "@opencode/ai/providers"
|
|
530
529
|
|
|
531
530
|
const minimax = MiniMax.configure({ apiKey: process.env.MINIMAX_API_KEY })
|
|
532
|
-
const request =
|
|
531
|
+
const request = ai.request({
|
|
533
532
|
model: minimax.model("MiniMax-M3"), // also minimax.messages("MiniMax-M3")
|
|
534
533
|
prompt: "What is 173 multiplied by 219?",
|
|
535
534
|
providerOptions: { thinking: { type: "adaptive" } },
|
|
536
535
|
generation: { maxTokens: 1536 },
|
|
537
536
|
})
|
|
538
537
|
|
|
539
|
-
const response = await Effect.runPromise(
|
|
538
|
+
const response = await Effect.runPromise(ai.generate(request).pipe(Effect.provide(AIClient.layer)))
|
|
540
539
|
console.log(response.text)
|
|
541
540
|
```
|
|
542
541
|
|
|
@@ -567,7 +566,7 @@ Use Meta's direct [Model API](https://dev.meta.ai/docs/overview) with `META_API_
|
|
|
567
566
|
import { Meta } from "@opencode/ai/providers"
|
|
568
567
|
|
|
569
568
|
const meta = Meta.configure() // or Meta.configure({ apiKey })
|
|
570
|
-
const request =
|
|
569
|
+
const request = ai.request({
|
|
571
570
|
model: meta.responses("muse-spark-1.3"), // meta.model(...) also selects Responses
|
|
572
571
|
prompt: "What is 173 multiplied by 219? Reply with the integer.",
|
|
573
572
|
providerOptions: { reasoningEffort: "low" },
|
|
@@ -601,19 +600,19 @@ To include search result lists, set `include: ["reasoning.encrypted_content", "w
|
|
|
601
600
|
Messages exposes hosted search calls; the recorded Messages API stream does not supply structured
|
|
602
601
|
citations or separate result blocks. Retain `response.message` for either API's continuation.
|
|
603
602
|
|
|
604
|
-
Use `
|
|
603
|
+
Use `ai.image.generate` for one-off generation or editing:
|
|
605
604
|
|
|
606
605
|
```ts
|
|
607
|
-
import {
|
|
606
|
+
import { ai, Media } from "@opencode/ai/effect"
|
|
608
607
|
|
|
609
|
-
const generation =
|
|
608
|
+
const generation = ai.image.generate({
|
|
610
609
|
model: meta.image("muse-image-1.0"),
|
|
611
610
|
prompt: "A flat black square on a white background.",
|
|
612
611
|
n: 1,
|
|
613
612
|
providerOptions: { reasoningStrength: "low" },
|
|
614
613
|
})
|
|
615
614
|
|
|
616
|
-
const edit =
|
|
615
|
+
const edit = ai.image.generate({
|
|
617
616
|
model: meta.image("muse-image-1.0"),
|
|
618
617
|
prompt: "Make the square purple.",
|
|
619
618
|
images: [Media.bytes(imageBytes, "image/webp")],
|
|
@@ -633,20 +632,20 @@ Meta Responses is explicitly HTTP/SSE-only and does not use WebSockets, even whe
|
|
|
633
632
|
|
|
634
633
|
## Image generation
|
|
635
634
|
|
|
636
|
-
Use `
|
|
635
|
+
Use `ai.image.generate` with an image model for direct asset generation. `ai.image.request` mirrors `ai.request`: the
|
|
637
636
|
model comes from the facade's `.image(...)` selector (mirroring `.responses(...)`), common fields
|
|
638
637
|
(`images`, `n`, `aspect`, `resolution`) lower natively or fail typed, and `providerOptions` is inferred from the
|
|
639
638
|
selected model. `aspect` is a ratio such as `"16:9"`; `resolution` is a size tier (`"1K"` is about 1024×1024 pixels
|
|
640
639
|
at that ratio, `"2K"` about 2048×2048). Exact sizes, seeds, masks, and output formats are provider options:
|
|
641
640
|
|
|
642
641
|
```ts
|
|
643
|
-
import {
|
|
642
|
+
import { ai, Media } from "@opencode/ai/effect"
|
|
644
643
|
import { OpenAI } from "@opencode/ai/providers"
|
|
645
644
|
|
|
646
645
|
const openai = OpenAI.configure({ apiKey: process.env.OPENAI_API_KEY })
|
|
647
646
|
|
|
648
647
|
const program = Effect.gen(function* () {
|
|
649
|
-
const response = yield*
|
|
648
|
+
const response = yield* ai.image.generate({
|
|
650
649
|
model: openai.image("gpt-image-2"),
|
|
651
650
|
prompt: "A robot tending a rooftop garden",
|
|
652
651
|
n: 2,
|
|
@@ -681,7 +680,7 @@ table before swapping only the `model`:
|
|
|
681
680
|
| Cloudflare Workers AI | 1 | width × height² | width × height² | ✗ | `width`, `height`, `seed` |
|
|
682
681
|
|
|
683
682
|
✓ lowers natively; ✗ fails whenever the field is set (including `n: 1`); `1` means `n > 1` fails. "width × height"
|
|
684
|
-
computes a size the provider accepts and fails when the request falls outside its limits. ¹ `
|
|
683
|
+
computes a size the provider accepts and fails when the request falls outside its limits. ¹ `ai.image.stream` on OpenAI
|
|
685
684
|
generates one image. On OpenAI, `gpt-image-2` and later get a computed size; older models get the nearest of their
|
|
686
685
|
fixed sizes. `providerOptions.size` replaces `aspect` and `resolution`, so setting both fails. Vercel AI Gateway
|
|
687
686
|
forwards `outputFormat` but does not apply it, so the returned bytes keep the upstream model's own format. ² Workers AI
|
|
@@ -703,7 +702,7 @@ Pass ordered image inputs to the same method for editing, composition, or image-
|
|
|
703
702
|
|
|
704
703
|
```ts
|
|
705
704
|
const composed = Effect.gen(function* () {
|
|
706
|
-
const response = yield*
|
|
705
|
+
const response = yield* ai.image.generate({
|
|
707
706
|
model,
|
|
708
707
|
prompt: "Combine these product photos into one studio scene",
|
|
709
708
|
images: [
|
|
@@ -727,7 +726,7 @@ uses multipart for byte/data-URL edits and its JSON reference body for URL or fi
|
|
|
727
726
|
provider option on the routes that support it (OpenAI):
|
|
728
727
|
|
|
729
728
|
```ts
|
|
730
|
-
const inpainted =
|
|
729
|
+
const inpainted = ai.image.generate({
|
|
731
730
|
model: openai.image("gpt-image-2"),
|
|
732
731
|
prompt,
|
|
733
732
|
images: [Media.bytes(sourceBytes, "image/png")],
|
|
@@ -743,7 +742,7 @@ not accept image inputs. These cases fail with a typed `AIError` before network
|
|
|
743
742
|
Provider-native image options belong to each request. Raw `http.body` fields have final precedence over them:
|
|
744
743
|
|
|
745
744
|
```ts
|
|
746
|
-
const medium =
|
|
745
|
+
const medium = ai.image.generate({
|
|
747
746
|
model: openai.image("gpt-image-2"),
|
|
748
747
|
prompt,
|
|
749
748
|
providerOptions: { quality: "medium" },
|
|
@@ -756,7 +755,7 @@ xAI image models use the same request API with xAI-native controls:
|
|
|
756
755
|
```ts
|
|
757
756
|
import { XAI } from "@opencode/ai/providers"
|
|
758
757
|
|
|
759
|
-
const xai =
|
|
758
|
+
const xai = ai.image.generate({
|
|
760
759
|
model: XAI.configure({ apiKey }).image("any-model-id"),
|
|
761
760
|
prompt,
|
|
762
761
|
n: 2,
|
|
@@ -776,7 +775,7 @@ Google's current Gemini image models use the same direct API:
|
|
|
776
775
|
import { Google } from "@opencode/ai/providers"
|
|
777
776
|
|
|
778
777
|
const googleProgram = Effect.gen(function* () {
|
|
779
|
-
const response = yield*
|
|
778
|
+
const response = yield* ai.image.generate({
|
|
780
779
|
model: Google.configure({ apiKey }).image("any-model-id"),
|
|
781
780
|
prompt: "A robot tending a rooftop garden",
|
|
782
781
|
aspect: "16:9",
|
|
@@ -804,7 +803,7 @@ Z.ai image models infer open Z.ai-native options from the selected model:
|
|
|
804
803
|
```ts
|
|
805
804
|
import { ZAI } from "@opencode/ai/providers"
|
|
806
805
|
|
|
807
|
-
const zai =
|
|
806
|
+
const zai = ai.image.generate({
|
|
808
807
|
model: ZAI.configure({ apiKey }).image("any-model-id"),
|
|
809
808
|
prompt,
|
|
810
809
|
providerOptions: {
|
|
@@ -825,7 +824,7 @@ persist the bytes promptly if they must remain available.
|
|
|
825
824
|
```ts
|
|
826
825
|
import { MiniMax } from "@opencode/ai/providers"
|
|
827
826
|
|
|
828
|
-
const minimax =
|
|
827
|
+
const minimax = ai.image.generate({
|
|
829
828
|
model: MiniMax.configure({ apiKey }).image("image-01"),
|
|
830
829
|
prompt,
|
|
831
830
|
n: 2,
|
|
@@ -842,14 +841,14 @@ error.
|
|
|
842
841
|
|
|
843
842
|
### Partial images
|
|
844
843
|
|
|
845
|
-
OpenAI's GPT image models stream previews. `
|
|
846
|
-
and emits `image-partial` events before each final `image`; `
|
|
844
|
+
OpenAI's GPT image models stream previews. `ai.image.stream` sends `stream: true` with `partialImages` (0–3, default 2)
|
|
845
|
+
and emits `image-partial` events before each final `image`; `ai.image.generate` keeps the plain JSON request:
|
|
847
846
|
|
|
848
847
|
```ts
|
|
849
848
|
import { Stream } from "effect"
|
|
850
849
|
import { ImageEvent } from "@opencode/ai/effect"
|
|
851
850
|
|
|
852
|
-
const previews =
|
|
851
|
+
const previews = ai.image.stream({
|
|
853
852
|
model: openai.image("gpt-image-2"),
|
|
854
853
|
prompt: "A lighthouse at dusk",
|
|
855
854
|
providerOptions: { partialImages: 2 },
|
|
@@ -865,8 +864,8 @@ Conversational image generation remains part of the LLM interaction. OpenAI Resp
|
|
|
865
864
|
|
|
866
865
|
```ts
|
|
867
866
|
const program = Effect.gen(function* () {
|
|
868
|
-
const response = yield*
|
|
869
|
-
|
|
867
|
+
const response = yield* ai.generate(
|
|
868
|
+
ai.request({
|
|
870
869
|
model: OpenAI.configure({ apiKey }).responses("gpt-5"),
|
|
871
870
|
prompt: "Design a solarpunk rooftop garden, then show me.",
|
|
872
871
|
tools: [OpenAI.imageGeneration({ quality: "high" })],
|
|
@@ -883,19 +882,19 @@ The hosted result is represented as a provider-executed tool call and a tool res
|
|
|
883
882
|
|
|
884
883
|
Everything below is exported from `@opencode/ai/effect` unless noted.
|
|
885
884
|
|
|
886
|
-
- **`
|
|
887
|
-
- **`
|
|
888
|
-
- **`
|
|
885
|
+
- **`ai.request({...})`** — build a provider-neutral `LLMRequest`. Accepts ergonomic inputs (`system: string`, `prompt: string`) that normalize into the canonical Schema classes.
|
|
886
|
+
- **`ai.generate` / `ai.stream`** — run direct input or an `LLMRequest` through `LLMClient` for one-import use.
|
|
887
|
+
- **`ai.loop`** — stream model calls, running `tool(...)` tools between them until the model stops calling tools.
|
|
889
888
|
- **`Message.user(...)` / `Message.assistant(...)` / `Message.tool(...)`** — message constructors from the canonical schema model.
|
|
890
889
|
- **`LanguageModel.make(...)` / `ToolCallPart.make(...)` / `ToolResultPart.make(...)` / `ToolDefinition.make(...)`** — model and tool-related constructors from the canonical schema model.
|
|
891
890
|
- **`LLMEvent.is.*`** — typed guards (`is.textDelta`, `is.toolCall`, `is.finish`, …) for filtering streams.
|
|
892
|
-
- **`
|
|
891
|
+
- **`ai.image.request` / `generate` / `stream` / `start` / `resume`** — images over inline, streaming (partial previews), and queued routes through a provider-neutral request and response model.
|
|
893
892
|
- **`ImageClient`** — Effect service and layer for image execution, parallel to `LLMClient`.
|
|
894
893
|
- **`Media`** — the shared asset type (`Media.Asset`, `Media.Source`) and constructors used by messages, tool results, and media requests.
|
|
895
894
|
- **`Generation`** — provider-neutral handle for an in-flight media generation (`await`, `refresh`, `cancel`, `events`) used by queued media routes.
|
|
896
|
-
- **`
|
|
895
|
+
- **`ai.decision.request` / `ai.decision.generate`** — typed predicate, choice, and score questions over text, JSON, and media input; `DecisionClient` is its Effect service and layer.
|
|
897
896
|
- **`AIClient.layer` / `AIClient.layerWith(executor)`** — every modality client plus the request executor in one layer.
|
|
898
|
-
- **`@opencode/ai`** (Promise root) —
|
|
897
|
+
- **`@opencode/ai`** (Promise root) — a default `ai` with the text calls (`request`, `generate`, `stream`, `loop`, `generateObject`, `compact`, `canCompact`) at the top level plus `image` and `decision` (plus `experimental.video`, `experimental.speech`, and `experimental.transcription`) as Promise / `AsyncIterable` APIs, `run(effect)` for any package Effect, plus `file`, `write`, `bytes`, `base64`, and `materialize` for assets. `AI.make({ layer?, clients? })` builds a client over a custom executor or test layers. The same `ai` from `@opencode/ai/effect` returns `Effect` and `Stream`.
|
|
899
898
|
- **`@opencode/ai/experimental`** — APIs that may change without compatibility guarantees: `Video`, `Speech`, `Transcription`, `Extraction`, and `Realtime`. See [Experimental](#experimental).
|
|
900
899
|
|
|
901
900
|
## Experimental
|
|
@@ -1250,25 +1249,25 @@ This is different from prompt caching, server-side history storage, or truncatio
|
|
|
1250
1249
|
|
|
1251
1250
|
### Explicit compaction
|
|
1252
1251
|
|
|
1253
|
-
`
|
|
1252
|
+
`ai.compact(request)` (equivalently, `{ mechanism: "endpoint" }`) is the caller-controlled operation for OpenAI, Azure, and xAI Responses. It performs exactly one HTTP call to `/responses/compact`, using the selected route's endpoint, credentials, query, and HTTP middleware. It returns a `CompactionResponse` with `replacement: Message[]` and optional `usage`, not a normal generation response. This mechanism does not accept a WebSocket executor.
|
|
1254
1253
|
|
|
1255
1254
|
Prefer this operation, where supported, when the application owns compaction policy and durable context updates.
|
|
1256
1255
|
|
|
1257
1256
|
```ts
|
|
1258
1257
|
const compacted = Effect.gen(function* () {
|
|
1259
|
-
const result = yield*
|
|
1258
|
+
const result = yield* ai.compact(request)
|
|
1260
1259
|
const next = LLMRequest.update(request, {
|
|
1261
1260
|
messages: result.replacement,
|
|
1262
1261
|
})
|
|
1263
|
-
return yield*
|
|
1262
|
+
return yield* ai.generate(next)
|
|
1264
1263
|
})
|
|
1265
1264
|
```
|
|
1266
1265
|
|
|
1267
|
-
From the Promise API, `await ai.
|
|
1266
|
+
From the Promise API, `await ai.compact(request)` takes the same request and options (plus `signal`) and returns the same result.
|
|
1268
1267
|
|
|
1269
1268
|
`replacement` replaces the complete input window. Do not append it to the original transcript or extract only the encrypted item: the provider may retain additional messages in its output. Retained user and assistant messages remain ordinary messages with typed text, media, or reasoning parts, in their original order. Provider-specific message IDs, status, and phase use `providerMetadata`, not a raw output array hidden in an assistant message. Unsupported returned item types fail explicitly.
|
|
1270
1269
|
|
|
1271
|
-
The selected model carries explicit-compaction capability through request construction and updates. Calls using unsupported routes fail type checking. When the model is selected dynamically, narrow the request with `
|
|
1270
|
+
The selected model carries explicit-compaction capability through request construction and updates. Calls using unsupported routes fail type checking. When the model is selected dynamically, narrow the request with `ai.canCompact(request)` before calling `ai.compact`; a model or route switch does not inherit the old capability. Runtime validation still rejects unsupported calls from untyped consumers. Capability describes the route's API, not whether every model or custom deployment supports the operation.
|
|
1272
1271
|
|
|
1273
1272
|
Generation-only body overlays such as `stream` and `store` are not sent to the compact endpoint. Supported compact controls such as service tier and prompt-cache settings preserve request defaults and HTTP-overlay precedence. Retained image and file detail settings survive serialization and replay.
|
|
1274
1273
|
|
|
@@ -1280,7 +1279,7 @@ OpenAI Responses also exposes a separate, explicitly selected mechanism:
|
|
|
1280
1279
|
|
|
1281
1280
|
```ts
|
|
1282
1281
|
const checkpoint = Effect.gen(function* () {
|
|
1283
|
-
const result = yield*
|
|
1282
|
+
const result = yield* ai.compact(request, {
|
|
1284
1283
|
mechanism: "trigger",
|
|
1285
1284
|
webSocket, // Optional: without it, the request uses HTTP/SSE.
|
|
1286
1285
|
})
|
|
@@ -1303,8 +1302,8 @@ Trigger support is separate from endpoint support. Only the OpenAI Responses rou
|
|
|
1303
1302
|
|
|
1304
1303
|
```ts
|
|
1305
1304
|
const narrowed = Effect.gen(function* () {
|
|
1306
|
-
if (
|
|
1307
|
-
const result = yield*
|
|
1305
|
+
if (ai.canCompact(request, { mechanism: "trigger" })) {
|
|
1306
|
+
const result = yield* ai.compact(request, { mechanism: "trigger" })
|
|
1308
1307
|
}
|
|
1309
1308
|
})
|
|
1310
1309
|
```
|
|
@@ -1319,10 +1318,10 @@ Enable OpenAI compaction with typed provider options:
|
|
|
1319
1318
|
|
|
1320
1319
|
```ts
|
|
1321
1320
|
import { Effect } from "effect"
|
|
1322
|
-
import {
|
|
1321
|
+
import { ai, LLMClient, LLMRequest, Message } from "@opencode/ai/effect"
|
|
1323
1322
|
import { OpenAI } from "@opencode/ai/providers"
|
|
1324
1323
|
|
|
1325
|
-
const request =
|
|
1324
|
+
const request = ai.request({
|
|
1326
1325
|
model: OpenAI.configure({ apiKey }).responses("gpt-5.3-codex"),
|
|
1327
1326
|
messages,
|
|
1328
1327
|
providerOptions: {
|
|
@@ -1398,7 +1397,7 @@ Requests below a provider's minimum cacheable size simply do not produce a reusa
|
|
|
1398
1397
|
### Opting out
|
|
1399
1398
|
|
|
1400
1399
|
```ts
|
|
1401
|
-
|
|
1400
|
+
ai.request({
|
|
1402
1401
|
model,
|
|
1403
1402
|
system,
|
|
1404
1403
|
prompt: "one-off question",
|
|
@@ -1424,7 +1423,7 @@ An object with placement fields places exactly those breakpoints. An object with
|
|
|
1424
1423
|
`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.
|
|
1425
1424
|
|
|
1426
1425
|
```ts
|
|
1427
|
-
|
|
1426
|
+
ai.request({ model, system, prompt, cache: { ttl: "1h" } })
|
|
1428
1427
|
```
|
|
1429
1428
|
|
|
1430
1429
|
| Protocol | How `ttl` is sent |
|
|
@@ -1442,7 +1441,7 @@ OpenAI's two fields cover different models: `prompt_cache_retention` (`"in_memor
|
|
|
1442
1441
|
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.
|
|
1443
1442
|
|
|
1444
1443
|
```ts
|
|
1445
|
-
|
|
1444
|
+
ai.request({
|
|
1446
1445
|
model,
|
|
1447
1446
|
system: [
|
|
1448
1447
|
{ type: "text", text: "stable system prompt", cache: { type: "ephemeral" } },
|
|
@@ -1567,7 +1566,7 @@ Route/provider defaults are overridden by request-level values for each axis.
|
|
|
1567
1566
|
The selected model supplies the provider-specific option type, so per-request overrides stay flat while the canonical runtime request remains provider-neutral:
|
|
1568
1567
|
|
|
1569
1568
|
```ts
|
|
1570
|
-
|
|
1569
|
+
ai.request({
|
|
1571
1570
|
model,
|
|
1572
1571
|
prompt,
|
|
1573
1572
|
providerOptions: {
|
package/dist/effect.d.ts
CHANGED
|
@@ -1,3 +1,47 @@
|
|
|
1
|
+
import { LLM } from "./llm.js";
|
|
2
|
+
/** The Effect API: the same shape as `ai` from `@opencode/ai`, returning `Effect` and `Stream`. */
|
|
3
|
+
export declare const ai: {
|
|
4
|
+
readonly request: typeof LLM.request;
|
|
5
|
+
readonly generate: typeof LLM.generate;
|
|
6
|
+
readonly stream: typeof LLM.stream;
|
|
7
|
+
readonly loop: typeof LLM.loop;
|
|
8
|
+
readonly generateObject: typeof LLM.generateObject;
|
|
9
|
+
readonly compact: typeof LLM.compact;
|
|
10
|
+
readonly canCompact: typeof LLM.canCompact;
|
|
11
|
+
readonly image: {
|
|
12
|
+
readonly request: typeof import("./image.js").request;
|
|
13
|
+
readonly generate: typeof import("./image.js").generate;
|
|
14
|
+
readonly stream: typeof import("./image.js").stream;
|
|
15
|
+
readonly start: typeof import("./image.js").start;
|
|
16
|
+
readonly resume: typeof import("./image.js").resume;
|
|
17
|
+
};
|
|
18
|
+
readonly decision: {
|
|
19
|
+
readonly request: typeof import("./decision.js").request;
|
|
20
|
+
readonly generate: typeof import("./decision.js").generate;
|
|
21
|
+
};
|
|
22
|
+
/** Experimental: may change without compatibility guarantees. */
|
|
23
|
+
readonly experimental: {
|
|
24
|
+
readonly speech: {
|
|
25
|
+
readonly request: typeof import("./speech.js").request;
|
|
26
|
+
readonly generate: typeof import("./speech.js").generate;
|
|
27
|
+
readonly stream: typeof import("./speech.js").stream;
|
|
28
|
+
};
|
|
29
|
+
readonly transcription: {
|
|
30
|
+
readonly request: typeof import("./transcription.js").request;
|
|
31
|
+
readonly generate: typeof import("./transcription.js").generate;
|
|
32
|
+
readonly stream: typeof import("./transcription.js").stream;
|
|
33
|
+
readonly start: typeof import("./transcription.js").start;
|
|
34
|
+
readonly resume: typeof import("./transcription.js").resume;
|
|
35
|
+
};
|
|
36
|
+
readonly video: {
|
|
37
|
+
readonly request: typeof import("./video.js").request;
|
|
38
|
+
readonly start: typeof import("./video.js").start;
|
|
39
|
+
readonly generate: typeof import("./video.js").generate;
|
|
40
|
+
readonly resume: typeof import("./video.js").resume;
|
|
41
|
+
readonly stream: typeof import("./video.js").stream;
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
};
|
|
1
45
|
export { AIClient } from "./ai-client.js";
|
|
2
46
|
export { LLMClient } from "./route/client.js";
|
|
3
47
|
export { ImageClient } from "./image-client.js";
|
|
@@ -9,11 +53,9 @@ export { ProtocolID, RouteID, ModelID, ProviderID, ResponseID, ContentBlockID, T
|
|
|
9
53
|
export type { GenerationOptionsFields, GenerationOptionsInput, LanguageModelInput, LanguageModelProviderOptions, ToolDefinitionInput, ToolNamespaceInput, ToolNamespaceEntryInput, ToolEntryInput, UsageInput, } from "./schema/index.js";
|
|
10
54
|
export { ImageAspectRatio, ImageEvent, ImageModel, ImageModelSchema, ImageRequest, ImageResponse } from "./image.js";
|
|
11
55
|
export type { ImageModelOptions, ImageOptions, ImageRequestFor, ImageRequestInput, ImageResolution, ImageRoute, } from "./image.js";
|
|
12
|
-
export { Image } from "./image.js";
|
|
13
56
|
export { DecisionClient } from "./decision-client.js";
|
|
14
57
|
export { ChoiceAnswer, ChoiceQuestion, ChoiceValue, DecisionAnswer, DecisionJsonPart, DecisionMediaPart, DecisionModel, DecisionModelCompatibility, DecisionModelSchema, DecisionPart, DecisionQuestion, DecisionRequest, DecisionResponse, DecisionTextPart, DecisionUsage, ErrorAnswer, ErrorReason, PredicateAnswer, PredicateQuestion, ScoreAnswer, ScoreQuestion, } from "./decision.js";
|
|
15
58
|
export type { AnswerFor, AnswersFor, DecisionInput, DecisionModelOptions, DecisionOptions, DecisionQuestions, DecisionRequestFor, DecisionRequestInput, DecisionResponseFor, DecisionModelRoute, } from "./decision.js";
|
|
16
|
-
export { Decision } from "./decision.js";
|
|
17
59
|
export { DecisionRoute } from "./route/decision.js";
|
|
18
60
|
export { Media } from "./media.js";
|
|
19
61
|
export { Generation } from "./generation.js";
|
|
@@ -22,5 +64,5 @@ export { ToolFailure, emptyInputJsonSchema, toDefinitions, tool } from "./tool.j
|
|
|
22
64
|
export { ToolRuntime } from "./tool-runtime.js";
|
|
23
65
|
export type { DispatchResult as ToolDispatchResult, ToolSettlement } from "./tool-runtime.js";
|
|
24
66
|
export type { ExecutableTool, Tool, ToolExecute, ToolExecuteContext, ToolModelOutputInput, ToolOptions, Tools, ToolSchema, ToolToModelOutput, } from "./tool.js";
|
|
25
|
-
export
|
|
67
|
+
export type { LLM } from "./llm.js";
|
|
26
68
|
export type { Definition as ProviderPackageDefinition, Settings as ProviderPackageSettings, } from "./provider-package.js";
|
package/dist/effect.js
CHANGED
|
@@ -1,3 +1,27 @@
|
|
|
1
|
+
import { Decision } from "./decision.js";
|
|
2
|
+
import { Image } from "./image.js";
|
|
3
|
+
import { LLM } from "./llm.js";
|
|
4
|
+
import { Speech } from "./speech.js";
|
|
5
|
+
import { Transcription } from "./transcription.js";
|
|
6
|
+
import { Video } from "./video.js";
|
|
7
|
+
/** The Effect API: the same shape as `ai` from `@opencode/ai`, returning `Effect` and `Stream`. */
|
|
8
|
+
export const ai = {
|
|
9
|
+
request: LLM.request,
|
|
10
|
+
generate: LLM.generate,
|
|
11
|
+
stream: LLM.stream,
|
|
12
|
+
loop: LLM.loop,
|
|
13
|
+
generateObject: LLM.generateObject,
|
|
14
|
+
compact: LLM.compact,
|
|
15
|
+
canCompact: LLM.canCompact,
|
|
16
|
+
image: Image,
|
|
17
|
+
decision: Decision,
|
|
18
|
+
/** Experimental: may change without compatibility guarantees. */
|
|
19
|
+
experimental: {
|
|
20
|
+
speech: Speech,
|
|
21
|
+
transcription: Transcription,
|
|
22
|
+
video: Video,
|
|
23
|
+
},
|
|
24
|
+
};
|
|
1
25
|
export { AIClient } from "./ai-client.js";
|
|
2
26
|
export { LLMClient } from "./route/client.js";
|
|
3
27
|
export { ImageClient } from "./image-client.js";
|
|
@@ -6,13 +30,10 @@ export { ProviderPackage } from "./provider-package.js";
|
|
|
6
30
|
export { isContextOverflow, isContextOverflowFailure, isRetryable } from "./provider-error.js";
|
|
7
31
|
export { ProtocolID, RouteID, ModelID, ProviderID, ResponseID, ContentBlockID, ToolCallID, JsonSchema, ProviderOptions, ProviderMetadata, HttpTimeout, RequestEncoding, HttpOptions, GenerationOptions, LanguageModelDefaults, ReasoningEfforts, ReasoningEffort, LanguageModelCompatibility, LanguageModel, LanguageModelSchema, CacheTTL, CacheHint, CachePolicyObject, CachePolicy, MessageRole, SystemPart, TextPart, MediaPart, ToolResultValue, ToolOutput, ToolCallPart, ToolResultPart, ReasoningPart, CompactionPart, EffortPart, ContentPart, Message, Freeform, ToolDefinition, ToolNamespace, ToolEntry, ToolChoice, LLMRequest, FinishReason, Usage, MediaUsage, CompactionResponse, CompactionCheckpointResponse, StepStart, TextStart, TextDelta, TextEnd, ReasoningStart, ReasoningDelta, ReasoningEnd, ToolInputStart, ToolInputDelta, ToolInputEnd, ToolInputError, ToolCall, ToolResult, ToolError, FinishReasonDetails, StepFinish, Finish, MediaEvent, ProviderErrorEvent, LLMEvent, LLMResponse, HttpContext, HttpRateLimitDetails, InvalidRequestError, UnsupportedOperationError, ProviderConfigurationError, NoRouteError, AuthenticationError, RateLimitError, QuotaExceededError, ContentPolicyError, ProviderInternalError, TransportType, TransportOperation, TransportError, InvalidProviderOutputError, UnknownProviderError, TimeoutError, AIErrorReason, AIError, } from "./schema/index.js";
|
|
8
32
|
export { ImageAspectRatio, ImageEvent, ImageModel, ImageModelSchema, ImageRequest, ImageResponse } from "./image.js";
|
|
9
|
-
export { Image } from "./image.js";
|
|
10
33
|
export { DecisionClient } from "./decision-client.js";
|
|
11
34
|
export { ChoiceAnswer, ChoiceQuestion, ChoiceValue, DecisionAnswer, DecisionJsonPart, DecisionMediaPart, DecisionModel, DecisionModelCompatibility, DecisionModelSchema, DecisionPart, DecisionQuestion, DecisionRequest, DecisionResponse, DecisionTextPart, DecisionUsage, ErrorAnswer, ErrorReason, PredicateAnswer, PredicateQuestion, ScoreAnswer, ScoreQuestion, } from "./decision.js";
|
|
12
|
-
export { Decision } from "./decision.js";
|
|
13
35
|
export { DecisionRoute } from "./route/decision.js";
|
|
14
36
|
export { Media } from "./media.js";
|
|
15
37
|
export { Generation } from "./generation.js";
|
|
16
38
|
export { ToolFailure, emptyInputJsonSchema, toDefinitions, tool } from "./tool.js";
|
|
17
39
|
export { ToolRuntime } from "./tool-runtime.js";
|
|
18
|
-
export * as LLM from "./llm.js";
|