11import { createOpenAICompatible } from "@ai-sdk/openai-compatible"
2- import { AIError , LLM , LanguageModel , Message } from "@opencode/ai"
2+ import { LLM , LanguageModel , Message } from "@opencode/ai"
33import { LLMClient , RequestExecutor } from "@opencode/ai/route"
4- import { HttpClientRequest } from "effect/http"
54import { expect } from "bun:test"
6- import { Effect , Layer , Result } from "effect"
5+ import { Effect , Layer } from "effect"
76import { AISDK } from "../src/aisdk"
87import { Model } from "@opencode/core/model"
98import { Provider } from "@opencode/core/provider"
@@ -21,185 +20,25 @@ const client = LLMClient.layer.pipe(
2120const chunk =
2221 'data: {"id":"r","object":"chat.completion.chunk","created":0,"model":"api-model","choices":[{"index":0,"delta":{"content":"ok"},"finish_reason":"stop"}]}\n\ndata: [DONE]\n\n'
2322
24- // Returning the original route from AISDK.route.with must fail the overridden cases.
25- for ( const baseURL of [
26- undefined ,
27- "https://replacement.example.invalid/v1" ,
28- "https://replacement.example.invalid/custom/v2/" ,
29- ] ) {
30- it . effect ( `keeps the AI SDK request endpoint local to the model route: ${ baseURL ?? "unchanged" } ` , ( ) =>
31- Effect . gen ( function * ( ) {
32- const sent : string [ ] = [ ]
33- const aisdk = yield * AISDK . Service
34- yield * aisdk . hook . sdk ( ( event ) => {
35- event . sdk = createOpenAICompatible ( {
36- ...event . options ,
37- name : String ( event . options . name ) ,
38- baseURL : String ( event . options . baseURL ) ,
39- } )
40- } )
41- const resolved = yield * aisdk . model ( {
42- ...Model . Info . make ( {
43- ...Model . Info . default ( Provider . ID . make ( "probe" ) , Model . ID . make ( "api-model" ) ) ,
44- package : Provider . aisdk ( "@ai-sdk/openai-compatible" ) ,
45- } ) ,
46- settings : {
47- baseURL : "https://original.example.invalid/v1" ,
48- apiKey : "test" ,
49- fetch : Object . assign (
50- async ( input : string | URL | Request ) => {
51- sent . push ( input instanceof Request ? input . url : String ( input ) )
52- return new Response ( chunk , { headers : { "content-type" : "text/event-stream" } } )
53- } ,
54- { preconnect : fetch . preconnect } ,
55- ) ,
56- } ,
57- } )
58- const selected =
59- baseURL === undefined
60- ? resolved
61- : LanguageModel . update ( resolved , {
62- route : resolved . route . with ( { endpoint : { baseURL } } ) ,
63- } )
64- yield * LLMClient . generate ( LLM . request ( { model : selected , messages : [ Message . user ( "hello" ) ] } ) ) . pipe (
65- Effect . provide ( client ) ,
66- )
67- yield * LLMClient . generate ( LLM . request ( { model : resolved , messages : [ Message . user ( "control" ) ] } ) ) . pipe (
68- Effect . provide ( client ) ,
69- )
70- expect ( sent ) . toEqual ( [
71- `${ ( baseURL ?? "https://original.example.invalid/v1" ) . replace ( / \/ $ / , "" ) } /chat/completions` ,
72- "https://original.example.invalid/v1/chat/completions" ,
73- ] )
74- } ) ,
75- )
76- }
77-
78- it . effect ( "caches overridden endpoints separately while retaining SDK and request hooks" , ( ) =>
23+ it . effect ( "overrides the AI SDK request URL without changing the original model" , ( ) =>
7924 Effect . gen ( function * ( ) {
80- const aisdk = yield * AISDK . Service
81- const loaded : string [ ] = [ ]
82- const sent : Array < { url : string ; headers : Headers } > = [ ]
83- yield * aisdk . hook . sdk ( ( event ) => {
84- loaded . push ( String ( event . options . baseURL ) )
85- event . sdk = createOpenAICompatible ( {
86- ...event . options ,
87- name : String ( event . options . name ) ,
88- baseURL : String ( event . options . baseURL ) ,
89- } )
90- } )
91- const original = yield * aisdk . model ( {
92- ...Model . Info . make ( {
93- ...Model . Info . default ( Provider . ID . make ( "probe" ) , Model . ID . make ( "api-model" ) ) ,
94- package : Provider . aisdk ( "@ai-sdk/openai-compatible" ) ,
95- } ) ,
96- headers : { "x-config" : "retained" } ,
97- settings : {
98- baseURL : "https://original.example.invalid/v1" ,
99- apiKey : "test" ,
100- fetch : Object . assign (
101- async ( input : string | URL | Request , init ?: RequestInit ) => {
102- sent . push ( {
103- url : input instanceof Request ? input . url : String ( input ) ,
104- headers : new Headers ( init ?. headers ) ,
105- } )
106- return new Response ( chunk , { headers : { "content-type" : "text/event-stream" } } )
107- } ,
108- { preconnect : fetch . preconnect } ,
109- ) ,
110- } ,
111- } )
112- const first = LanguageModel . update ( original , {
113- route : original . route . with ( { endpoint : { baseURL : "https://first.example.invalid/v2" } } ) ,
114- } )
115- const second = LanguageModel . update ( first , {
116- route : first . route . with ( { endpoint : { baseURL : "https://second.example.invalid/v3" } } ) ,
117- } )
118- for ( const selected of [ first , second , first , original ] ) {
119- yield * LLMClient . generate ( LLM . request ( { model : selected , prompt : "hello" } ) , {
120- http : ( request , handler ) => handler ( HttpClientRequest . setHeader ( request , "x-hook" , "retained" ) ) ,
121- } ) . pipe ( Effect . provide ( client ) )
122- }
123- expect ( loaded ) . toEqual ( [
124- "https://original.example.invalid/v1" ,
125- "https://first.example.invalid/v2" ,
126- "https://second.example.invalid/v3" ,
127- ] )
128- expect ( sent . map ( ( request ) => request . url ) ) . toEqual ( [
129- "https://first.example.invalid/v2/chat/completions" ,
130- "https://second.example.invalid/v3/chat/completions" ,
131- "https://first.example.invalid/v2/chat/completions" ,
132- "https://original.example.invalid/v1/chat/completions" ,
133- ] )
134- expect ( sent . every ( ( request ) => request . headers . get ( "authorization" ) === "Bearer test" ) ) . toBe ( true )
135- expect ( sent . every ( ( request ) => request . headers . get ( "x-config" ) === "retained" ) ) . toBe ( true )
136- expect ( sent . every ( ( request ) => request . headers . get ( "x-hook" ) === "retained" ) ) . toBe ( true )
137- } ) ,
138- )
139-
140- it . effect ( "overrides an SDK default endpoint and reruns language hooks without changing the original" , ( ) =>
141- Effect . gen ( function * ( ) {
142- const aisdk = yield * AISDK . Service
14325 const sent : string [ ] = [ ]
144- const languages : unknown [ ] = [ ]
145- yield * aisdk . hook . sdk ( ( event ) => {
146- event . sdk = createOpenAICompatible ( {
147- ...event . options ,
148- name : String ( event . options . name ) ,
149- baseURL : String ( event . options . baseURL ?? "https://default.example.invalid/v1" ) ,
150- } )
151- } )
152- yield * aisdk . hook . language ( ( event ) => {
153- languages . push ( event . model . settings ?. baseURL )
154- } )
155- const original = yield * aisdk . model ( {
156- ...Model . Info . make ( {
157- ...Model . Info . default ( Provider . ID . make ( "probe" ) , Model . ID . make ( "api-model" ) ) ,
158- package : Provider . aisdk ( "@ai-sdk/openai-compatible" ) ,
159- } ) ,
160- settings : {
161- fetch : Object . assign (
162- async ( input : string | URL | Request ) => {
163- sent . push ( input instanceof Request ? input . url : String ( input ) )
164- return new Response ( chunk , { headers : { "content-type" : "text/event-stream" } } )
165- } ,
166- { preconnect : fetch . preconnect } ,
167- ) ,
168- } ,
169- } )
170- const changed = LanguageModel . update ( original , {
171- route : original . route . with ( { endpoint : { baseURL : "https://override.example.invalid/v2" } } ) ,
172- } )
173- for ( const selected of [ changed , original ] ) {
174- yield * LLMClient . generate ( LLM . request ( { model : selected , prompt : "hello" } ) ) . pipe ( Effect . provide ( client ) )
175- }
176- expect ( languages ) . toEqual ( [ undefined , "https://override.example.invalid/v2" ] )
177- expect ( sent ) . toEqual ( [
178- "https://override.example.invalid/v2/chat/completions" ,
179- "https://default.example.invalid/v1/chat/completions" ,
180- ] )
181- } ) ,
182- )
183-
184- it . effect ( "reports overridden SDK initialization failures without poisoning the original model" , ( ) =>
185- Effect . gen ( function * ( ) {
18626 const aisdk = yield * AISDK . Service
187- const sent : string [ ] = [ ]
18827 yield * aisdk . hook . sdk ( ( event ) => {
189- if ( event . options . baseURL === "https://broken.example.invalid/v2" ) throw new Error ( "SDK initialization failed" )
19028 event . sdk = createOpenAICompatible ( {
19129 ...event . options ,
19230 name : String ( event . options . name ) ,
19331 baseURL : String ( event . options . baseURL ) ,
19432 } )
19533 } )
196- const original = yield * aisdk . model ( {
34+ const resolved = yield * aisdk . model ( {
19735 ...Model . Info . make ( {
19836 ...Model . Info . default ( Provider . ID . make ( "probe" ) , Model . ID . make ( "api-model" ) ) ,
19937 package : Provider . aisdk ( "@ai-sdk/openai-compatible" ) ,
20038 } ) ,
20139 settings : {
20240 baseURL : "https://original.example.invalid/v1" ,
41+ apiKey : "test" ,
20342 fetch : Object . assign (
20443 async ( input : string | URL | Request ) => {
20544 sent . push ( input instanceof Request ? input . url : String ( input ) )
@@ -209,17 +48,18 @@ it.effect("reports overridden SDK initialization failures without poisoning the
20948 ) ,
21049 } ,
21150 } )
212- const changed = LanguageModel . update ( original , {
213- route : original . route . with ( { endpoint : { baseURL : "https://broken .example.invalid/v2 " } } ) ,
51+ const selected = LanguageModel . update ( resolved , {
52+ route : resolved . route . with ( { endpoint : { baseURL : "https://replacement .example.invalid/custom/v2/ " } } ) ,
21453 } )
215- const result = yield * LLMClient . generate ( LLM . request ( { model : changed , prompt : "hello" } ) ) . pipe (
54+ yield * LLMClient . generate ( LLM . request ( { model : selected , messages : [ Message . user ( "hello" ) ] } ) ) . pipe (
55+ Effect . provide ( client ) ,
56+ )
57+ yield * LLMClient . generate ( LLM . request ( { model : resolved , messages : [ Message . user ( "control" ) ] } ) ) . pipe (
21658 Effect . provide ( client ) ,
217- Effect . result ,
21859 )
219- expect ( Result . isFailure ( result ) ) . toBe ( true )
220- if ( Result . isFailure ( result ) ) expect ( result . failure ) . toBeInstanceOf ( AIError )
221- expect ( sent ) . toEqual ( [ ] )
222- yield * LLMClient . generate ( LLM . request ( { model : original , prompt : "control" } ) ) . pipe ( Effect . provide ( client ) )
223- expect ( sent ) . toEqual ( [ "https://original.example.invalid/v1/chat/completions" ] )
60+ expect ( sent ) . toEqual ( [
61+ "https://replacement.example.invalid/custom/v2/chat/completions" ,
62+ "https://original.example.invalid/v1/chat/completions" ,
63+ ] )
22464 } ) ,
22565)
0 commit comments