22 isSecretReference ,
33 SpeechCleanupReasoning ,
44 type SpeechCleanupModels ,
5+ type SpeechCleanupApiProvider ,
56} from "@t3tools/contracts" ;
67import * as SecretResolver from "@t3tools/secret-provider-core/server/SecretResolver" ;
78import * as Context from "effect/Context" ;
@@ -31,7 +32,9 @@ export class SpeechCleanupApi extends Context.Service<
3132 SpeechCleanupApi ,
3233 {
3334 readonly postProcess : ( prompt : string ) => Effect . Effect < string , SpeechCleanupApiError > ;
34- readonly models : Effect . Effect < SpeechCleanupModels , SpeechCleanupApiError > ;
35+ readonly models : (
36+ provider : SpeechCleanupApiProvider ,
37+ ) => Effect . Effect < SpeechCleanupModels , SpeechCleanupApiError > ;
3538 }
3639> ( ) ( "t3/speech/SpeechCleanupApi" ) { }
3740
@@ -163,59 +166,58 @@ const make = Effect.gen(function* () {
163166 ) ,
164167 ) ;
165168
166- const credential = Effect . fn ( "SpeechCleanupApi.credential" ) ( function * (
167- operation : "post-processing" | "models" ,
169+ const configuration = Effect . fn ( "SpeechCleanupApi.configuration" ) ( function * (
170+ operation : SpeechCleanupApiError [ "operation" ] ,
171+ requestedProvider ?: SpeechCleanupApiProvider ,
168172 ) {
169173 const settings = yield * settingsService . getSettings . pipe (
170174 Effect . mapError (
171175 ( cause ) => new SpeechCleanupApiError ( { operation, reason : "configuration" , cause } ) ,
172176 ) ,
173177 ) ;
174- const selected = settings . speechPostProcessingProvider ;
178+ const selected = requestedProvider ?? settings . speechPostProcessingProvider ;
179+ if ( selected === "agent" || selected === "opencode" )
180+ return yield * new SpeechCleanupApiError ( { operation, reason : "configuration" } ) ;
175181 const provider =
176- selected === "compatible" && settings . speechPostProcessingCompatiblePreset === "openrouter"
182+ requestedProvider === undefined &&
183+ selected === "compatible" &&
184+ settings . speechPostProcessingCompatiblePreset === "openrouter"
177185 ? "openrouter"
178186 : selected ;
179- const slot =
180- provider === "compatible"
181- ? "compatiblePostProcessing"
182- : provider === "anthropic"
183- ? "anthropicPostProcessing"
184- : provider === "openrouter"
185- ? "openrouterPostProcessing"
186- : "openaiPostProcessing" ;
187+ const endpoint = (
188+ provider === "openai"
189+ ? "https://api.openai.com/v1"
190+ : settings . speechPostProcessingApiEndpoints [ provider ] . endpoint
191+ ) . replace ( / \/ + $ / , "" ) ;
192+ const model =
193+ provider === "openai"
194+ ? settings . speechPostProcessingOpenAIModel
195+ : settings . speechPostProcessingApiEndpoints [ provider ] . model ;
196+ if ( ! endpoint || ( operation === "post-processing" && ! model ) )
197+ return yield * new SpeechCleanupApiError ( { operation, reason : "configuration" } ) ;
198+ const slot = `${ provider } PostProcessing` as const ;
187199 const source = settings . voiceApiCredentials [ slot ] ;
188200 if ( isSecretReference ( source ) ) yield * resolver . invalidate ( [ source ] ) ;
189201 const key = yield * requireSettingsSecret ( resolver , source ) . pipe (
190202 Effect . mapError (
191203 ( cause ) => new SpeechCleanupApiError ( { operation, reason : "credential" , cause } ) ,
192204 ) ,
193205 ) ;
194- return { settings, key, provider } ;
206+ if ( ! key && provider !== "compatible" )
207+ return yield * new SpeechCleanupApiError ( { operation, reason : "credential" } ) ;
208+ return { settings, key, provider, endpoint, model } ;
195209 } ) ;
196210
197211 return SpeechCleanupApi . of ( {
198212 postProcess : Effect . fn ( "SpeechCleanupApi.postProcess" ) ( function * ( prompt ) {
199- const { settings, key, provider } = yield * credential ( "post-processing" ) ;
200- if ( ! key && provider !== "compatible" )
201- return yield * new SpeechCleanupApiError ( {
202- operation : "post-processing" ,
203- reason : "credential" ,
204- } ) ;
213+ const { settings, key, provider, endpoint, model } = yield * configuration ( "post-processing" ) ;
205214 if ( provider === "compatible" || provider === "anthropic" || provider === "openrouter" ) {
206- const config = settings . speechPostProcessingApiEndpoints [ provider ] ;
207- if ( ! config . endpoint || ! config . model )
208- return yield * new SpeechCleanupApiError ( {
209- operation : "post-processing" ,
210- reason : "configuration" ,
211- } ) ;
212215 const anthropic = provider === "anthropic" ;
213- const endpoint = config . endpoint . replace ( / \/ + $ / , "" ) ;
214216 let request = HttpClientRequest . post (
215217 `${ endpoint } ${ anthropic ? "/messages" : "/chat/completions" } ` ,
216218 ) . pipe (
217219 HttpClientRequest . bodyJsonUnsafe ( {
218- model : config . model ,
220+ model,
219221 messages : [ { role : "user" , content : prompt } ] ,
220222 ...( anthropic
221223 ? {
@@ -239,10 +241,10 @@ const make = Effect.gen(function* () {
239241 }
240242 return yield * requestText (
241243 "post-processing" ,
242- HttpClientRequest . post ( "https://api.openai.com/v1/ responses" ) . pipe (
244+ HttpClientRequest . post ( ` ${ endpoint } / responses` ) . pipe (
243245 HttpClientRequest . bearerToken ( Redacted . make ( key ) ) ,
244246 HttpClientRequest . bodyJsonUnsafe ( {
245- model : settings . speechPostProcessingOpenAIModel ,
247+ model,
246248 input : prompt ,
247249 store : false ,
248250 ...( settings . speechPostProcessingOpenAIReasoning === null
@@ -255,69 +257,60 @@ const make = Effect.gen(function* () {
255257 "responses" ,
256258 ) ;
257259 } ) ,
258- models : Effect . gen ( function * ( ) {
259- const operation = "models" ;
260- const { settings, key, provider } = yield * credential ( operation ) ;
261- if ( provider === "agent" || provider === "opencode" )
262- return yield * new SpeechCleanupApiError ( { operation, reason : "configuration" } ) ;
263- if ( ! key && provider !== "compatible" )
264- return yield * new SpeechCleanupApiError ( { operation, reason : "credential" } ) ;
265- const endpoint =
266- provider === "openai"
267- ? "https://api.openai.com/v1"
268- : settings . speechPostProcessingApiEndpoints [ provider ] . endpoint . replace ( / \/ + $ / , "" ) ;
269- if ( ! endpoint )
270- return yield * new SpeechCleanupApiError ( { operation, reason : "configuration" } ) ;
271- const models = new Map < string , SpeechCleanupModels [ "models" ] [ number ] > ( ) ;
272- let cursor : string | undefined ;
273- for ( let page = 0 ; page < 10 ; page ++ ) {
274- let request = HttpClientRequest . get ( `${ endpoint } /models` ) ;
275- if ( provider === "anthropic" ) {
276- request = request . pipe (
277- HttpClientRequest . setHeader ( "x-api-key" , key ) ,
278- HttpClientRequest . setHeader ( "anthropic-version" , "2023-06-01" ) ,
279- HttpClientRequest . setUrlParam ( "limit" , "1000" ) ,
260+ models : Effect . fn ( "SpeechCleanupApi.models" ) (
261+ function * ( provider ) {
262+ const operation = "models" ;
263+ const { key, endpoint } = yield * configuration ( operation , provider ) ;
264+ const models = new Map < string , SpeechCleanupModels [ "models" ] [ number ] > ( ) ;
265+ let cursor : string | undefined ;
266+ for ( let page = 0 ; page < 10 ; page ++ ) {
267+ let request = HttpClientRequest . get ( `${ endpoint } /models` ) ;
268+ if ( provider === "anthropic" ) {
269+ request = request . pipe (
270+ HttpClientRequest . setHeader ( "x-api-key" , key ) ,
271+ HttpClientRequest . setHeader ( "anthropic-version" , "2023-06-01" ) ,
272+ HttpClientRequest . setUrlParam ( "limit" , "1000" ) ,
273+ ) ;
274+ if ( cursor ) request = HttpClientRequest . setUrlParam ( request , "after_id" , cursor ) ;
275+ } else if ( key ) request = HttpClientRequest . bearerToken ( request , Redacted . make ( key ) ) ;
276+ const response = yield * http . execute ( request ) . pipe (
277+ Effect . provideService ( FetchHttpClient . RequestInit , { redirect : "error" } ) ,
278+ Effect . provideService ( HttpClient . TracerPropagationEnabled , false ) ,
279+ Effect . mapError ( ( ) => new SpeechCleanupApiError ( { operation, reason : "unavailable" } ) ) ,
280280 ) ;
281- if ( cursor ) request = HttpClientRequest . setUrlParam ( request , "after_id" , cursor ) ;
282- } else if ( key ) request = HttpClientRequest . bearerToken ( request , Redacted . make ( key ) ) ;
283- const response = yield * http . execute ( request ) . pipe (
284- Effect . provideService ( FetchHttpClient . RequestInit , { redirect : "error" } ) ,
285- Effect . provideService ( HttpClient . TracerPropagationEnabled , false ) ,
286- Effect . mapError ( ( ) => new SpeechCleanupApiError ( { operation, reason : "unavailable" } ) ) ,
287- ) ;
288- if ( response . status < 200 || response . status >= 300 )
289- return yield * new SpeechCleanupApiError ( {
290- operation,
291- reason : "http" ,
292- status : response . status ,
293- } ) ;
294- const result = yield * HttpClientResponse . schemaBodyJson ( ModelPage ) ( response ) . pipe (
295- Effect . mapError ( ( ) => new SpeechCleanupApiError ( { operation, reason : "response" } ) ) ,
296- ) ;
297- for ( const model of result . data ) {
298- if ( ! model . id . trim ( ) || model . id . length > 200 ) continue ;
299- const effort = model . capabilities ?. effort ;
300- models . set ( model . id , {
301- id : model . id ,
302- name : model . display_name ?? model . name ?? model . id ,
303- ...( effort
304- ? {
305- reasoningLevels : SpeechCleanupReasoning . literals . filter ( ( level ) => {
306- const capability = effort [ level ] ;
307- return typeof capability === "object" && capability . supported ;
308- } ) ,
309- }
310- : { } ) ,
311- } ) ;
281+ if ( response . status < 200 || response . status >= 300 )
282+ return yield * new SpeechCleanupApiError ( {
283+ operation,
284+ reason : "http" ,
285+ status : response . status ,
286+ } ) ;
287+ const result = yield * HttpClientResponse . schemaBodyJson ( ModelPage ) ( response ) . pipe (
288+ Effect . mapError ( ( ) => new SpeechCleanupApiError ( { operation, reason : "response" } ) ) ,
289+ ) ;
290+ for ( const model of result . data ) {
291+ const id = model . id . trim ( ) ;
292+ if ( ! id || id . length > 200 ) continue ;
293+ const effort = model . capabilities ?. effort ;
294+ models . set ( id , {
295+ id,
296+ name : model . display_name ?. trim ( ) || model . name ?. trim ( ) || id ,
297+ ...( effort
298+ ? {
299+ reasoningLevels : SpeechCleanupReasoning . literals . filter ( ( level ) => {
300+ const capability = effort [ level ] ;
301+ return typeof capability === "object" && capability . supported ;
302+ } ) ,
303+ }
304+ : { } ) ,
305+ } ) ;
306+ }
307+ if ( ! result . has_more )
308+ return { models : [ ...models . values ( ) ] . sort ( ( a , b ) => a . id . localeCompare ( b . id ) ) } ;
309+ if ( provider !== "anthropic" || ! result . last_id || result . last_id === cursor ) break ;
310+ cursor = result . last_id ;
312311 }
313- if ( ! result . has_more )
314- return { models : [ ...models . values ( ) ] . sort ( ( a , b ) => a . id . localeCompare ( b . id ) ) } ;
315- if ( provider !== "anthropic" || ! result . last_id || result . last_id === cursor ) break ;
316- cursor = result . last_id ;
317- }
318- return yield * new SpeechCleanupApiError ( { operation, reason : "response" } ) ;
319- } ) . pipe (
320- Effect . withSpan ( "SpeechCleanupApi.models" ) ,
312+ return yield * new SpeechCleanupApiError ( { operation, reason : "response" } ) ;
313+ } ,
321314 Effect . timeoutOrElse ( {
322315 duration : "25 seconds" ,
323316 orElse : ( ) =>
0 commit comments