11import { Clock , Effect , Redacted , Schema } from "effect"
22import { HttpClientRequest } from "effect/http"
33import { RequestExecutor } from "../route/executor.js"
4- import { AIError , InvalidProviderOutputError , InvalidRequestError } from "../schema/index.js"
4+ import { AIError , InvalidProviderOutputError , InvalidRequestError , type RequestTimeout } from "../schema/index.js"
55import { encodeJson } from "../utils/json.js"
66
77const OPENAI_BASE_URL = "https://api.openai.com/v1"
@@ -27,6 +27,7 @@ export interface RealtimeConnectInput {
2727 readonly baseURL ?: string
2828 /** Headers that host needs besides the API key, such as OpenCode's workspace. */
2929 readonly headers ?: Readonly < Record < string , string > >
30+ readonly timeout ?: RequestTimeout
3031 readonly session ?: RealtimeSession
3132 readonly transport : RealtimeTransportInput
3233}
@@ -69,16 +70,23 @@ export const connect = Effect.fn("Realtime.connect")(function* (input: RealtimeC
6970 input . apiKey ,
7071 { session, transport : input . transport } ,
7172 LiveSessionResponse ,
72- { headers : input . headers } ,
73+ { headers : input . headers , timeout : input . timeout } ,
7374 )
7475 return { type : "webrtc" as const , sdp : data . transport . sdp }
7576 }
7677 case "openai-realtime" : {
7778 if ( input . transport . type === "websocket" )
78- return yield * clientSecret ( resolved . id , OPENAI_BASE_URL , input . model , input . apiKey , {
79- expires_after : { anchor : "created_at" , seconds : 60 } ,
80- session,
81- } )
79+ return yield * clientSecret (
80+ resolved . id ,
81+ OPENAI_BASE_URL ,
82+ input . model ,
83+ input . apiKey ,
84+ {
85+ expires_after : { anchor : "created_at" , seconds : 60 } ,
86+ session,
87+ } ,
88+ input . timeout ,
89+ )
8290 const form = new FormData ( )
8391 form . append ( "sdp" , input . transport . sdp )
8492 form . append ( "session" , encodeJson ( session ) )
@@ -89,13 +97,21 @@ export const connect = Effect.fn("Realtime.connect")(function* (input: RealtimeC
8997 input . apiKey ,
9098 form ,
9199 Schema . String ,
100+ { timeout : input . timeout } ,
92101 )
93102 return { type : "webrtc" as const , sdp }
94103 }
95104 case "xai-realtime" :
96- return yield * clientSecret ( resolved . id , XAI_BASE_URL , input . model , input . apiKey , {
97- expires_after : { seconds : 60 } ,
98- } )
105+ return yield * clientSecret (
106+ resolved . id ,
107+ XAI_BASE_URL ,
108+ input . model ,
109+ input . apiKey ,
110+ {
111+ expires_after : { seconds : 60 } ,
112+ } ,
113+ input . timeout ,
114+ )
99115 case "gemini-live" : {
100116 const now = yield * Clock . currentTimeMillis
101117 const expiresAt = now + 60_000
@@ -110,7 +126,7 @@ export const connect = Effect.fn("Realtime.connect")(function* (input: RealtimeC
110126 bidiGenerateContentSetup : { ...input . session , model : `models/${ input . model } ` } ,
111127 } ,
112128 AuthTokenResponse ,
113- { authHeader : "x-goog-api-key" } ,
129+ { authHeader : "x-goog-api-key" , timeout : input . timeout } ,
114130 )
115131 return {
116132 type : "websocket" as const ,
@@ -158,11 +174,18 @@ const post = <A>(
158174 apiKey : Redacted . Redacted ,
159175 body : Record < string , unknown > | FormData ,
160176 response : Schema . Codec < A , string > ,
161- options : { readonly authHeader ?: string ; readonly headers ?: Readonly < Record < string , string > > } = { } ,
177+ options : {
178+ readonly authHeader ?: string
179+ readonly headers ?: Readonly < Record < string , string > >
180+ readonly timeout ?: RequestTimeout
181+ } = { } ,
162182) =>
163183 Effect . gen ( function * ( ) {
164184 const executor = yield * RequestExecutor . Service
165- const res = yield * executor . execute (
185+ const res = yield * RequestExecutor . withTimeout (
186+ executor . execute ,
187+ options . timeout ,
188+ ) (
166189 HttpClientRequest . post ( url ) . pipe (
167190 HttpClientRequest . setHeaders ( options . headers ?? { } ) ,
168191 options . authHeader
@@ -176,7 +199,7 @@ const post = <A>(
176199 const http = RequestExecutor . responseHttp ( res )
177200 const fail = ( message : string , cause ?: unknown , raw ?: string ) =>
178201 new AIError ( { reason : new InvalidProviderOutputError ( { route, message, body : raw , http, cause } ) } )
179- const text = yield * res . text . pipe ( Effect . mapError ( ( cause ) => fail ( `Failed to read ${ route } response` , cause ) ) )
202+ const text = yield * RequestExecutor . responseText ( res )
180203 return yield * Schema . decodeEffect ( response ) ( text ) . pipe (
181204 Effect . mapError ( ( cause ) => fail ( `${ route } returned an invalid response` , cause , text ) ) ,
182205 )
@@ -188,8 +211,9 @@ const clientSecret = (
188211 model : string ,
189212 apiKey : Redacted . Redacted ,
190213 body : Record < string , unknown > ,
214+ timeout ?: RequestTimeout ,
191215) =>
192- post ( route , `${ baseURL } /realtime/client_secrets` , apiKey , body , ClientSecretResponse ) . pipe (
216+ post ( route , `${ baseURL } /realtime/client_secrets` , apiKey , body , ClientSecretResponse , { timeout } ) . pipe (
193217 Effect . map (
194218 ( data ) : RealtimeTransport => ( {
195219 type : "websocket" ,
0 commit comments