@@ -24,7 +24,6 @@ import { ManagedPolicy } from "../../managed-policy.js"
2424import { Model } from "../../model.js"
2525import { Provider } from "../../provider.js"
2626import { Variant } from "../../variant.js"
27- import { Voice } from "../../voice.js"
2827import { WebSearch } from "../../websearch.js"
2928import { ConfigPolicy } from "@opencode/schema/config/policy"
3029import { ConfigProvider } from "@opencode/schema/config/provider"
@@ -65,13 +64,6 @@ const RemoteResponse = Schema.Struct({
6564 Schema . Struct ( { ...Mcp . RemoteConfig . fields , auth : Schema . Literal ( "console" ) . pipe ( Schema . optional ) } ) ,
6665 ) ,
6766 } ) . pipe ( Schema . optional ) ,
68- // Hosted voice models the Console starts for the caller's workspace, the inference base that creates them, and the
69- // workspace's display name, which older Consoles omit.
70- voice : Schema . Struct ( {
71- baseURL : Schema . String ,
72- models : Schema . Array ( Schema . String ) ,
73- workspace : Schema . String . pipe ( Schema . optional ) ,
74- } ) . pipe ( Schema . optional ) ,
7567 // Organization policy compiled for the authenticated caller; omitted when there is none.
7668 // `experimental.policies` remains accepted alongside top-level `policies`.
7769 policies : RemotePolicies ,
@@ -101,10 +93,6 @@ const SsoRequired = Schema.Struct({
10193 connectionId : Schema . String ,
10294} )
10395const InvalidGrant = Schema . Struct ( { error : Schema . Literal ( "invalid_grant" ) } )
104- const LiveSession = Schema . fromJsonString (
105- Schema . Struct ( { session : Schema . Struct ( { id : Schema . String } ) , transport : Schema . Struct ( { sdp : Schema . String } ) } ) ,
106- )
107- const InferenceError = Schema . fromJsonString ( Schema . Struct ( { error : Schema . Struct ( { message : Schema . String } ) } ) )
10896// Console's 401 when the session behind the access token was revoked or expired.
10997const Unauthorized = Schema . Struct ( { _tag : Schema . Literal ( "Unauthorized" ) } )
11098const signedOutMessage = "Reconnect OpenCode Console to continue"
@@ -206,9 +194,7 @@ function oauth(http: HttpClient.HttpClient) {
206194 } satisfies IntegrationOAuthMethodRegistration
207195}
208196
209- export const OpencodePlugin = define <
210- HttpClient . HttpClient | Bus . Service | ManagedPolicy . Service | Voice . Service | Scope . Scope
211- > ( {
197+ export const OpencodePlugin = define < HttpClient . HttpClient | Bus . Service | ManagedPolicy . Service | Scope . Scope > ( {
212198 id : "opencode.provider.opencode" ,
213199 effect : Effect . fn ( function * ( ctx ) {
214200 const disableFree = [ "1" , "true" ] . includes ( process . env . OPENCODE_DISABLE_FREE_MODELS ?. toLowerCase ( ) ?? "" )
@@ -218,7 +204,6 @@ export const OpencodePlugin = define<
218204 // and whether it evaluates the policies it is being sent.
219205 const http = HttpClient . mapRequest ( client , HttpClientRequest . setHeader ( "User-Agent" , App . useragent ( ctx . app ) ) )
220206 const managed = yield * ManagedPolicy . Service
221- const voice = yield * Voice . Service
222207 const scope = yield * Scope . Scope
223208 const loading = Semaphore . makeUnsafe ( 1 )
224209 type ActiveConnection = Effect . Success < ReturnType < typeof ctx . integration . connection . active > >
@@ -277,105 +262,12 @@ export const OpencodePlugin = define<
277262 ) ,
278263 )
279264 } )
280- // The credential of the connection a snapshot was built from, while it is still the active one.
281- const resolveActive = Effect . fnUntraced ( function * ( connection : NonNullable < ActiveConnection > ) {
282- const active = yield * ctx . integration . connection . active ( "opencode" )
283- if ( ! active || IntegrationConnection . key ( active ) !== IntegrationConnection . key ( connection ) )
284- return yield * Effect . fail ( new Error ( "OpenCode Console connection changed" ) )
285- const credential = yield * ctx . integration . connection . resolve ( active )
286- if ( ! credential || credential . type === "external" )
287- return yield * Effect . fail ( new Error ( "OpenCode Console is not connected" ) )
288- return credential
289- } )
290-
291- // Hosted GPT-Live: the server's apps start sessions on the Console's account, billed to the workspace, with
292- // the credential of the connection whose config offered it.
293- const consoleVoice = (
294- connection : NonNullable < ActiveConnection > ,
295- offer : NonNullable < typeof RemoteResponse . Type . voice > ,
296- ) : Voice . Host => {
297- const send = ( path : string , body : unknown ) =>
298- Effect . gen ( function * ( ) {
299- const credential = yield * resolveActive ( connection )
300- const response = yield * HttpClient . withScope ( http ) . execute (
301- HttpClientRequest . post ( `${ offer . baseURL } ${ path } ` ) . pipe (
302- HttpClientRequest . acceptJson ,
303- HttpClientRequest . setHeaders ( credentialHeaders ( credential ) ) ,
304- HttpClientRequest . bodyJsonUnsafe ( body ) ,
305- ) ,
306- )
307- return { status : response . status , text : yield * response . text }
308- } ) . pipe (
309- Effect . provideService ( FetchHttpClient . RequestInit , { redirect : "error" } ) ,
310- Effect . scoped ,
311- Effect . timeoutOrElse ( {
312- duration : Duration . seconds ( 20 ) ,
313- orElse : ( ) => Effect . fail ( new Error ( "OpenCode Console didn't answer in time" ) ) ,
314- } ) ,
315- Effect . mapError (
316- ( cause ) =>
317- new Voice . UnavailableError ( {
318- service : "opencode" ,
319- message : cause instanceof Error ? cause . message : String ( cause ) ,
320- } ) ,
321- ) ,
322- Effect . flatMap ( ( reply ) : Effect . Effect < string , Voice . Error > => {
323- if ( reply . status >= 200 && reply . status < 300 ) return Effect . succeed ( reply . text )
324- if ( reply . status === 401 )
325- return Effect . fail ( new Voice . UnavailableError ( { service : "opencode" , message : `${ signedOutMessage } .` } ) )
326- const message =
327- Option . getOrUndefined ( Schema . decodeUnknownOption ( InferenceError ) ( reply . text ) ) ?. error . message ??
328- `OpenCode Console refused the voice request (HTTP ${ reply . status } )`
329- return Effect . fail (
330- reply . status === 400 || reply . status === 404
331- ? new Voice . InvalidRequestError ( { message } )
332- : new Voice . UnavailableError ( { service : "opencode" , message } ) ,
333- )
334- } ) ,
335- )
336- return {
337- integration : "opencode" ,
338- models : offer . models ,
339- workspace : offer . workspace ,
340- session : ( input ) =>
341- send ( "/live/sessions" , { session : { ...input . session , model : input . model } , transport : input . transport } ) . pipe (
342- Effect . flatMap ( ( text ) =>
343- Schema . decodeUnknownEffect ( LiveSession ) ( text ) . pipe (
344- Effect . mapError (
345- ( ) =>
346- new Voice . UnavailableError ( {
347- service : "opencode" ,
348- message : "OpenCode Console returned an invalid voice session" ,
349- } ) ,
350- ) ,
351- ) ,
352- ) ,
353- Effect . map ( ( created ) => ( {
354- transport : { type : "webrtc" as const , sdp : created . transport . sdp } ,
355- id : created . session . id ,
356- } ) ) ,
357- ) ,
358- instruct : ( input ) =>
359- send ( `/live/sessions/${ encodeURIComponent ( input . id ) } /instructions` , {
360- instructions : input . instructions ,
361- } ) . pipe ( Effect . asVoid ) ,
362- }
363- }
364-
365- // Statements and hosted voice ride on the snapshot, so a credential switch, disconnect, or 404 replaces them too.
265+ // Statements ride on the snapshot, so a credential switch, disconnect, or 404 replaces them too.
366266 const publish = ( next : typeof snapshot ) =>
367- Effect . all (
368- [
369- managed . set ( {
370- statements : [ ...( next . config ?. experimental ?. policies ?? [ ] ) , ...( next . config ?. policies ?? [ ] ) ] ,
371- organization : next . organization ,
372- } ) ,
373- voice . host (
374- next . connection && next . config ?. voice ? consoleVoice ( next . connection , next . config . voice ) : undefined ,
375- ) ,
376- ] ,
377- { discard : true } ,
378- )
267+ managed . set ( {
268+ statements : [ ...( next . config ?. experimental ?. policies ?? [ ] ) , ...( next . config ?. policies ?? [ ] ) ] ,
269+ organization : next . organization ,
270+ } )
379271
380272 yield * ctx . integration . transform ( ( editor ) => {
381273 editor . update ( "opencode" , ( integration ) => {
@@ -507,7 +399,18 @@ export const OpencodePlugin = define<
507399 name : "OpenCode Web Search" ,
508400 execute : ( input ) =>
509401 Effect . gen ( function * ( ) {
510- const credential = yield * resolveActive ( connection )
402+ const active = yield * ctx . integration . connection . active ( "opencode" )
403+ if (
404+ ! active ||
405+ ( connection . type === "credential"
406+ ? active . type !== "credential" || active . id !== connection . id
407+ : active . type !== "env" || active . name !== connection . name )
408+ ) {
409+ return yield * Effect . fail ( new Error ( "OpenCode Console connection changed" ) )
410+ }
411+ const credential = yield * ctx . integration . connection . resolve ( active )
412+ if ( ! credential || credential . type === "external" )
413+ return yield * Effect . fail ( new Error ( "OpenCode Console is not connected" ) )
511414 const metadata = credential . metadata
512415 const orgID = typeof metadata ?. orgID === "string" ? metadata . orgID : undefined
513416 const token = credential . type === "oauth" ? credential . access : credential . key
0 commit comments