11import { z } from "zod"
22import { useQuery } from "@tanstack/react-query"
33
4- import type { ModelInfo } from "@roo-code/types"
5-
64export const openRouterModelSchema = z . object ( {
75 id : z . string ( ) ,
86 name : z . string ( ) ,
9- description : z . string ( ) ,
10- created : z . number ( ) ,
11- context_length : z . number ( ) ,
12- pricing : z . object ( {
13- prompt : z . string ( ) . optional ( ) ,
14- completion : z . string ( ) . optional ( ) ,
15- } ) ,
16- top_provider : z
17- . object ( {
18- max_completion_tokens : z . number ( ) . nullish ( ) ,
19- } )
20- . optional ( ) ,
21- architecture : z
22- . object ( {
23- modality : z . string ( ) ,
24- } )
25- . optional ( ) ,
267} )
278
28- export type OpenRouterModel = z . infer < typeof openRouterModelSchema > & { modelInfo : ModelInfo }
9+ export type OpenRouterModel = z . infer < typeof openRouterModelSchema >
2910
3011export const getOpenRouterModels = async ( ) : Promise < OpenRouterModel [ ] > => {
3112 const response = await fetch ( "https://openrouter.ai/api/v1/models" )
3213
3314 if ( ! response . ok ) {
34- console . error ( "Failed to fetch OpenRouter models" )
3515 return [ ]
3616 }
3717
@@ -42,19 +22,11 @@ export const getOpenRouterModels = async (): Promise<OpenRouterModel[]> => {
4222 return [ ]
4323 }
4424
45- return result . data . data
46- . sort ( ( a , b ) => a . name . localeCompare ( b . name ) )
47- . map ( ( rawModel ) => ( {
48- ...rawModel ,
49- modelInfo : {
50- contextWindow : rawModel . context_length ,
51- supportsPromptCache : false ,
52- } ,
53- } ) )
25+ return result . data . data . sort ( ( a , b ) => a . name . localeCompare ( b . name ) )
5426}
5527
5628export const useOpenRouterModels = ( ) =>
57- useQuery < OpenRouterModel [ ] > ( {
29+ useQuery ( {
5830 queryKey : [ "getOpenRouterModels" ] ,
5931 queryFn : getOpenRouterModels ,
6032 } )
0 commit comments