@@ -3,41 +3,25 @@ import { RequestExecutor } from "@opencode/ai/route"
33import { Credential } from "@opencode/core/credential"
44import { Integration } from "@opencode/core/integration"
55import { Voice } from "@opencode/core/voice"
6- import { ConfigProvider , Effect , Layer } from "effect"
6+ import { Effect , Layer } from "effect"
77import { HttpClient , type HttpClientRequest , HttpClientResponse } from "effect/http"
88import { testEffect } from "./lib/effect"
99
1010const it = testEffect ( Layer . empty )
1111
12- const keyCred = ( id : string , key : string ) =>
13- new Credential . Info ( {
14- id : Credential . ID . make ( id ) ,
15- integrationID : Integration . ID . make ( "openai" ) ,
16- label : id ,
17- value : Credential . Key . make ( { type : "key" , key } ) ,
18- } )
19-
20- const oauthCred = new Credential . Info ( {
21- id : Credential . ID . make ( "oauth" ) ,
22- integrationID : Integration . ID . make ( "openai" ) ,
23- label : "ChatGPT" ,
24- value : Credential . OAuth . make ( {
25- type : "oauth" ,
26- methodID : Integration . MethodID . make ( "chatgpt" ) ,
27- refresh : "rt" ,
28- access : "at_oauth" ,
29- expires : Date . now ( ) + 3600_000 ,
30- } ) ,
12+ const credential = ( id : string , method : "key" | "oauth" ) => ( {
13+ type : "credential" as const ,
14+ id : Credential . ID . make ( id ) ,
15+ label : id ,
16+ method,
3117} )
3218
33- it . effect ( "resolves credentials (active key, stored key over OAuth, env fallback, unavailable error)" , ( ) =>
19+ const keys : Record < string , string > = { active : "sk-active" , stored : "sk-stored" , XAI_API_KEY : "xai-env-key" }
20+
21+ it . effect ( "uses the first key or env connection Integration lists and never a ChatGPT sign-in" , ( ) =>
3422 Effect . gen ( function * ( ) {
3523 const auths : string [ ] = [ ]
36- const record = ( req : HttpClientRequest . HttpClientRequest ) => {
37- auths . push ( req . headers . authorization ?? "" )
38- return Response . json ( { session : { id : "s1" } , transport : { sdp : "v=0\r\no=answer\r\n" } } )
39- }
40- const call = ( model : string , stored : ReadonlyArray < Credential . Info > , onReq = record ) =>
24+ const call = ( model : string , connections : Integration . Info [ "connections" ] ) =>
4125 Effect . flatMap ( Voice . Service , ( voice ) =>
4226 voice . session ( {
4327 model,
@@ -48,12 +32,54 @@ it.effect("resolves credentials (active key, stored key over OAuth, env fallback
4832 Voice . layer . pipe (
4933 Layer . provide (
5034 Layer . merge (
51- Layer . mock ( Credential . Service , { list : ( ) => Effect . succeed ( [ ...stored ] ) } ) ,
35+ Layer . mock ( Integration . Service , {
36+ revision : ( ) => 0 ,
37+ get : ( id ) => Effect . succeed ( { id, name : id , methods : [ ] , connections } ) ,
38+ connection : {
39+ active : ( ) => Effect . die ( "unused" ) ,
40+ resolve : ( connection ) =>
41+ connection . type === "credential" && connection . method === "oauth"
42+ ? Effect . die ( "a ChatGPT sign-in was resolved" )
43+ : Effect . succeed (
44+ Credential . Key . make ( {
45+ type : "key" ,
46+ key : keys [ connection . type === "env" ? connection . name : connection . id ] ,
47+ } ) ,
48+ ) ,
49+ key : ( ) => Effect . die ( "unused" ) ,
50+ external : ( ) => Effect . die ( "unused" ) ,
51+ activate : ( ) => Effect . die ( "unused" ) ,
52+ update : ( ) => Effect . die ( "unused" ) ,
53+ remove : ( ) => Effect . die ( "unused" ) ,
54+ status : ( ) => Effect . die ( "unused" ) ,
55+ } ,
56+ oauth : {
57+ connect : ( ) => Effect . die ( "unused" ) ,
58+ status : ( ) => Effect . die ( "unused" ) ,
59+ complete : ( ) => Effect . die ( "unused" ) ,
60+ cancel : ( ) => Effect . die ( "unused" ) ,
61+ } ,
62+ command : {
63+ connect : ( ) => Effect . die ( "unused" ) ,
64+ status : ( ) => Effect . die ( "unused" ) ,
65+ cancel : ( ) => Effect . die ( "unused" ) ,
66+ } ,
67+ } ) ,
5268 RequestExecutor . layer . pipe (
5369 Layer . provide (
5470 Layer . succeed (
5571 HttpClient . HttpClient ,
56- HttpClient . make ( ( req ) => Effect . sync ( ( ) => HttpClientResponse . fromWeb ( req , onReq ( req ) ) ) ) ,
72+ HttpClient . make ( ( req : HttpClientRequest . HttpClientRequest ) =>
73+ Effect . sync ( ( ) => {
74+ auths . push ( req . headers . authorization ?? "" )
75+ return HttpClientResponse . fromWeb (
76+ req ,
77+ model . startsWith ( "grok-" )
78+ ? Response . json ( { value : "ek_xai" , expires_at : 1760000999 } )
79+ : Response . json ( { transport : { sdp : "v=0\r\no=answer\r\n" } } ) ,
80+ )
81+ } ) ,
82+ ) ,
5783 ) ,
5884 ) ,
5985 ) ,
@@ -63,25 +89,19 @@ it.effect("resolves credentials (active key, stored key over OAuth, env fallback
6389 ) ,
6490 )
6591
66- // Active credential is last in `Credential.list`: active key wins, and OAuth is skipped for stored key.
67- yield * call ( "gpt-live-1" , [ keyCred ( "old" , "sk-old" ) , keyCred ( "active" , "sk-active" ) ] )
68- yield * call ( "gpt-live-1" , [ keyCred ( "stored" , "sk-stored" ) , oauthCred ] )
69- expect ( auths ) . toEqual ( [ "Bearer sk-active" , "Bearer sk-stored" ] )
70-
71- const oauthOnly = yield * call ( "gpt-live-1" , [ oauthCred ] ) . pipe ( Effect . flip )
72- expect ( oauthOnly . _tag ) . toBe ( "Voice.UnavailableError" )
73- expect ( oauthOnly . message ) . toContain ( "OpenAI API key" )
74-
75- const grok = yield * call ( "grok-voice-3.0" , [ ] , ( req ) => {
76- auths . push ( req . headers . authorization ?? "" )
77- return Response . json ( { value : "ek_xai" , expires_at : 1760000999 } )
78- } )
79- expect ( auths . at ( - 1 ) ) . toBe ( "Bearer xai-env-key" )
92+ yield * call ( "gpt-live-1" , [ credential ( "active" , "key" ) , credential ( "stored" , "key" ) ] )
93+ yield * call ( "gpt-live-1" , [ credential ( "chatgpt" , "oauth" ) , credential ( "stored" , "key" ) ] )
94+ const grok = yield * call ( "grok-voice-3.0" , [ { type : "env" , name : "XAI_API_KEY" } ] )
95+ expect ( auths ) . toEqual ( [ "Bearer sk-active" , "Bearer sk-stored" , "Bearer xai-env-key" ] )
8096 expect ( grok ) . toEqual ( {
8197 type : "websocket" ,
8298 url : "wss://api.x.ai/v1/realtime?model=grok-voice-3.0" ,
8399 token : "ek_xai" ,
84100 expiresAt : 1760000999 ,
85101 } )
86- } ) . pipe ( Effect . provide ( ConfigProvider . layer ( ConfigProvider . fromEnv ( { env : { XAI_API_KEY : "xai-env-key" } } ) ) ) ) ,
102+
103+ const signInOnly = yield * call ( "gpt-live-1" , [ credential ( "chatgpt" , "oauth" ) ] ) . pipe ( Effect . flip )
104+ expect ( signInOnly . _tag ) . toBe ( "Voice.UnavailableError" )
105+ expect ( signInOnly . message ) . toContain ( "OpenAI API key" )
106+ } ) ,
87107)
0 commit comments