1+ import * as Path from "effect/Path" ;
2+ import { PROVIDER_SEND_TURN_MAX_IMAGE_BYTES } from "@t3tools/contracts" ;
13import { OpenCode , type OpenCodeClient } from "@opencode/client/effect" ;
24import { OpenCodeEvent } from "@opencode/protocol/groups/event" ;
35import * as Context from "effect/Context" ;
@@ -50,9 +52,43 @@ export type OpenCode2StreamEvent =
5052 | OpenCode2UnreadableTerminal
5153 | OpenCode2UnreadableStart ;
5254
55+ export class OpenCode2ImageError extends Schema . TaggedError < OpenCode2ImageError > ( ) (
56+ "OpenCode2ImageError" ,
57+ { cause : Schema . Defect ( ) } ,
58+ ) { }
59+
60+ const IMAGE_MIME : Readonly < Record < string , string > > = {
61+ ".png" : "image/png" ,
62+ ".jpg" : "image/jpeg" ,
63+ ".jpeg" : "image/jpeg" ,
64+ ".webp" : "image/webp" ,
65+ ".gif" : "image/gif" ,
66+ } ;
67+ export const imagePreviewMime = ( extension : string ) => IMAGE_MIME [ extension . toLowerCase ( ) ] ;
68+
69+ const NativeImageJob = Schema . Struct ( {
70+ kind : Schema . Literal ( "image" ) ,
71+ id : Schema . String ,
72+ sessionID : Schema . String ,
73+ status : Schema . Literals ( [ "running" , "completed" , "error" , "cancelled" ] ) ,
74+ path : Schema . String ,
75+ model : Schema . String ,
76+ } ) ;
77+ const decodeJobs = Schema . decodeUnknownEffect (
78+ Schema . Struct ( { data : Schema . Array ( Schema . Unknown ) } ) ,
79+ ) ;
80+ const decodeImageJob = Schema . decodeUnknownOption ( NativeImageJob ) ;
81+
5382/** events resolves on subscription acceptance; subscribe before work because the stream is volatile. */
5483export interface OpenCode2Api {
5584 readonly client : OpenCodeClient ;
85+ readonly imageJobs : (
86+ sessionID : string ,
87+ ) => Effect . Effect < ReadonlyArray < typeof NativeImageJob . Type > , OpenCode2ImageError > ;
88+ readonly readImage : (
89+ path : string ,
90+ directory : string ,
91+ ) => Effect . Effect < { uri : string ; mime : string } , OpenCode2ImageError > ;
5692 readonly events : Effect . Effect <
5793 Stream . Stream <
5894 OpenCode2StreamEvent ,
@@ -202,6 +238,7 @@ const fillCompactionRecents = HttpClient.transformResponse(
202238/** @public */
203239export const make = Effect . gen ( function * ( ) {
204240 const httpClient = yield * HttpClient . HttpClient ;
241+ const paths = yield * Path . Path . pipe ( Effect . provide ( Path . layer ) ) ;
205242 return OpenCode2Client . of ( {
206243 connect : Effect . fn ( "OpenCode2Client.connect" ) ( function ( { baseUrl, password } ) {
207244 const authenticated = HttpClient . mapRequest (
@@ -212,6 +249,51 @@ export const make = Effect.gen(function* () {
212249 Effect . provideService ( HttpClient . HttpClient , fillCompactionRecents ( authenticated ) ) ,
213250 Effect . map ( ( client ) => ( {
214251 client,
252+ imageJobs : ( sessionID : string ) =>
253+ authenticated
254+ . get ( `${ baseUrl } /api/experimental/job` , {
255+ urlParams : { sessionID, kind : "image" } ,
256+ } )
257+ . pipe (
258+ Effect . flatMap ( HttpClientResponse . filterStatusOk ) ,
259+ Effect . flatMap ( ( response ) => response . json ) ,
260+ Effect . flatMap ( decodeJobs ) ,
261+ Effect . map ( ( page ) =>
262+ page . data . flatMap ( ( item ) => {
263+ const job = decodeImageJob ( item ) ;
264+ return Option . isSome ( job ) && job . value . sessionID === sessionID
265+ ? [ job . value ]
266+ : [ ] ;
267+ } ) ,
268+ ) ,
269+ Effect . mapError ( ( cause ) => new OpenCode2ImageError ( { cause } ) ) ,
270+ ) ,
271+ readImage : Effect . fn ( "OpenCode2Client.readImage" ) (
272+ function * ( path : string , directory : string ) {
273+ const mime = imagePreviewMime ( paths . extname ( path ) ) ;
274+ if ( mime === undefined )
275+ return yield * new OpenCode2ImageError ( { cause : "Unsupported image format" } ) ;
276+ const relative = paths . relative ( directory , path ) ;
277+ // OpenCode confines reads to the requested location, including its own temp images.
278+ const outside =
279+ relative === ".." ||
280+ relative . startsWith ( `..${ paths . sep } ` ) ||
281+ paths . isAbsolute ( relative ) ;
282+ const location = outside ? paths . dirname ( path ) : directory ;
283+ const file = outside ? paths . basename ( path ) : relative ;
284+ const response = yield * authenticated
285+ . get ( `${ baseUrl } /api/fs/read/${ encodeURIComponent ( file ) } ` , {
286+ urlParams : { "location[directory]" : location } ,
287+ } )
288+ . pipe ( Effect . flatMap ( HttpClientResponse . filterStatusOk ) ) ;
289+ const bytes = yield * response . arrayBuffer ;
290+ if ( bytes . byteLength > PROVIDER_SEND_TURN_MAX_IMAGE_BYTES )
291+ return yield * new OpenCode2ImageError ( { cause : "Image exceeds preview limit" } ) ;
292+ return { uri : `data:${ mime } ;base64,${ Buffer . from ( bytes ) . toString ( "base64" ) } ` , mime } ;
293+ } ,
294+ Effect . timeout ( "10 seconds" ) ,
295+ Effect . mapError ( ( cause ) => new OpenCode2ImageError ( { cause } ) ) ,
296+ ) ,
215297 events : readEvents . pipe (
216298 Effect . provideService (
217299 HttpClient . HttpClient ,
0 commit comments