Skip to content

refactor(ai): take fetch-typed middleware in the Promise Media constructors - #67665

Merged
rekram1-node merged 1 commit into
devfrom
media-fetch-middleware
Dec 2, 2026
Merged

rekram1-node merged 1 commit into
devfrom
media-fetch-middleware

Conversation

@rekram1-node

@rekram1-node rekram1-node commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

The Promise API's Media.url(url, { middleware }) now takes a FetchMiddleware, the same fetch-typed hook as RunOptions.middleware from #67425. Before, Promise callers had to write an Effect HttpMiddleware to authenticate an asset download. The Effect API is unchanged.

 import { ai, Media } from "@opencode/ai"

 const asset = Media.url("https://files.example.com/report.pdf", {
-  middleware: (request, next) => next(HttpClientRequest.setHeader(request, "authorization", `Bearer ${token}`)),
+  middleware: (request, next) => {
+    request.headers.set("authorization", `Bearer ${token}`)
+    return next(request)
+  },
 })
 await ai.write(asset, "./report.pdf")

How it works

  • @opencode/ai now exports its own Media namespace (src/promise-media.ts). It wraps the constructors from, bytes, base64, url, ref, parseDataUrl and fromDataUrl so their middleware option is a FetchMiddleware, converted with the existing fromFetch adapter. The adapter moved unchanged from src/index.ts to src/fetch-middleware.ts so both modules can use it.
  • The rest is re-exported from the shared media.ts: Asset, the schemas, make, image and the other helpers. Assets are the same class instances, so they still work in messages, instanceof Media.Asset and the Effect API.
  • Media.file and Media.write are left out of the Promise Media because they return Effects. Promise callers already have ai.file and ai.write.
  • ai.file(path, options) now takes the Promise AssetOptions too.

Breaking changes (@opencode/ai only)

  • A Promise caller that passes an Effect HttpMiddleware to a Media constructor now gets a type error. Rewrite it as a FetchMiddleware, or import Media from @opencode/ai/effect.
  • Media.file and Media.write are no longer on the Promise Media. Use ai.file and ai.write.

Trade-offs

Media.Asset is still the shared class, so on the Promise side asset.bytes(), stream(), base64(), dataUrl() and materialize() still return Effects, and asset.middleware holds the converted Effect middleware. Hiding them would mean a second asset type, and every message, media part and response type takes the class, so it wouldn't fit in a reviewable PR. The README points Promise callers to ai.bytes(asset), ai.base64, ai.materialize and ai.write instead.

Verification

  • test/http-hooks.test.ts: a Promise url asset sends its download through its own fetch middleware (header injection) and not the caller's. A middleware can also transform the response body as a stream: it gets the body unread, and ai.write writes the transformed bytes.
  • test/promise-media.types.ts: the Promise Media has no file/write, none of its functions return an Effect or Stream, and no constructor accepts an HttpMiddleware.
  • bun typecheck, bun run typecheck:tsc and bun test in packages/ai, and bun run check from the root, all pass.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found nothing that should block this. With the PR checked out, I ran the Promise entry from source against a local HTTP server: a Media.url asset with a fetch middleware sent its auth header, and ai.bytes and ai.write returned the right bytes. asset instanceof Media.Asset still holds. I also confirmed that test/promise-media.types.ts fails if an Effect-returning file is added back to the Promise Media. The full packages/ai test suite and typecheck:tsc pass.

Public API changes (@opencode/ai only; @opencode/ai/effect is unchanged):

  • Changed, breaking: the middleware option of Media.from, bytes, base64, url, ref, parseDataUrl and fromDataUrl goes from HttpMiddleware to FetchMiddleware. Promise callers passing an Effect middleware get a type error and need to rewrite it in the fetch style, or import Media from @opencode/ai/effect.
  • Changed: Media.AssetOptions now has middleware?: FetchMiddleware, and so does ai.file(path, options).
  • Removed, breaking: Media.file and Media.write are no longer in the Promise Media. Callers move to ai.file and ai.write.
  • Unchanged: the FetchMiddleware export (now defined in src/fetch-middleware.ts), Media.Asset, the schemas, make, image and the other helpers.

@rekram1-node
rekram1-node merged commit 7f31692 into dev Dec 2, 2026
15 checks passed
@rekram1-node
rekram1-node deleted the media-fetch-middleware branch December 2, 2026 04:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant