Repository navigation
refactor(ai): take fetch-typed middleware in the Promise Media constructors - #67665
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
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
middlewareoption ofMedia.from,bytes,base64,url,ref,parseDataUrlandfromDataUrlgoes fromHttpMiddlewaretoFetchMiddleware. Promise callers passing an Effect middleware get a type error and need to rewrite it in the fetch style, or importMediafrom@opencode/ai/effect. - Changed:
Media.AssetOptionsnow hasmiddleware?: FetchMiddleware, and so doesai.file(path, options). - Removed, breaking:
Media.fileandMedia.writeare no longer in the PromiseMedia. Callers move toai.fileandai.write. - Unchanged: the
FetchMiddlewareexport (now defined insrc/fetch-middleware.ts),Media.Asset, the schemas,make,imageand the other helpers.
6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The Promise API's
Media.url(url, { middleware })now takes aFetchMiddleware, the same fetch-typed hook asRunOptions.middlewarefrom #67425. Before, Promise callers had to write an EffectHttpMiddlewareto 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/ainow exports its ownMedianamespace (src/promise-media.ts). It wraps the constructorsfrom,bytes,base64,url,ref,parseDataUrlandfromDataUrlso theirmiddlewareoption is aFetchMiddleware, converted with the existingfromFetchadapter. The adapter moved unchanged fromsrc/index.tstosrc/fetch-middleware.tsso both modules can use it.media.ts:Asset, the schemas,make,imageand the other helpers. Assets are the same class instances, so they still work in messages,instanceof Media.Assetand the Effect API.Media.fileandMedia.writeare left out of the PromiseMediabecause they return Effects. Promise callers already haveai.fileandai.write.ai.file(path, options)now takes the PromiseAssetOptionstoo.Breaking changes (
@opencode/aionly)HttpMiddlewareto aMediaconstructor now gets a type error. Rewrite it as aFetchMiddleware, or importMediafrom@opencode/ai/effect.Media.fileandMedia.writeare no longer on the PromiseMedia. Useai.fileandai.write.Trade-offs
Media.Assetis still the shared class, so on the Promise sideasset.bytes(),stream(),base64(),dataUrl()andmaterialize()still return Effects, andasset.middlewareholds 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 toai.bytes(asset),ai.base64,ai.materializeandai.writeinstead.Verification
test/http-hooks.test.ts: a Promiseurlasset 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, andai.writewrites the transformed bytes.test/promise-media.types.ts: the PromiseMediahas nofile/write, none of its functions return anEffectorStream, and no constructor accepts anHttpMiddleware.bun typecheck,bun run typecheck:tscandbun testinpackages/ai, andbun run checkfrom the root, all pass.