11module OpenRouterCLIProxyAPI
22
33using OpenRouter
4- using OpenRouter: add_provider, ChatCompletionSchema, AnthropicSchema, ProviderEndpoint,
4+ using OpenRouter: add_provider, set_provider!, ChatCompletionSchema, AnthropicSchema, ProviderEndpoint,
55 get_global_cache, Pricing, ZERO_PRICING
66
77export inject_cli_proxy_endpoints!, cli_proxy_model_transform, setup_cli_proxy!
@@ -47,14 +47,11 @@ cli_proxy_model_transform(model_id::AbstractString) = get(MODEL_MAP_REVERSE, mod
4747# ============ Endpoint Injection ============
4848
4949"""
50- inject_cli_proxy_endpoints!(provider_name::String="cli_proxy_api"; mutate::Bool=false )
50+ inject_cli_proxy_endpoints!(provider_name::String="cli_proxy_api")
5151
52- Inject cli_proxy_api endpoints into cached models.
53-
54- - `mutate=false` (default): Adds new endpoint, preserves originals
55- - `mutate=true`: Overwrites all existing endpoints to point to cli_proxy_api
52+ Inject cli_proxy_api endpoints into cached models (adds new endpoint, preserves originals).
5653"""
57- function inject_cli_proxy_endpoints! (provider_name:: String = " cli_proxy_api" ; mutate :: Bool = false )
54+ function inject_cli_proxy_endpoints! (provider_name:: String = " cli_proxy_api" )
5855 cache = get_global_cache ()
5956 count = 0
6057
@@ -66,54 +63,65 @@ function inject_cli_proxy_endpoints!(provider_name::String="cli_proxy_api"; muta
6663 continue
6764 end
6865
69- if mutate
70- # Mutate all existing endpoints to point to cli_proxy_api
71- for ep in cached. endpoints. endpoints
72- ep. provider_name = provider_name
73- ep. tag = " $(ep. tag) >$(provider_name) "
74- ep. name = native_id
75- ep. model_name = native_id
76- end
77- count += length (cached. endpoints. endpoints)
66+ # Add new endpoint without overwriting
67+ existing = findfirst (ep -> ep. provider_name == provider_name, cached. endpoints. endpoints)
68+ if existing != = nothing
69+ continue
70+ end
71+
72+ base_pricing = if ! isempty (cached. endpoints. endpoints)
73+ cached. endpoints. endpoints[1 ]. pricing
7874 else
79- # Add new endpoint without overwriting
80- existing = findfirst (ep -> ep. provider_name == provider_name, cached. endpoints. endpoints)
81- if existing != = nothing
82- continue
83- end
84-
85- base_pricing = if ! isempty (cached. endpoints. endpoints)
86- cached. endpoints. endpoints[1 ]. pricing
87- else
88- ZERO_PRICING
89- end
90-
91- new_endpoint = ProviderEndpoint (;
92- name = native_id,
93- model_name = native_id,
94- context_length = cached. model. context_length,
95- pricing = base_pricing,
96- provider_name = provider_name,
97- tag = provider_name,
98- quantization = nothing ,
99- max_completion_tokens = nothing ,
100- max_prompt_tokens = nothing ,
101- supported_parameters = nothing ,
102- uptime_last_30m = nothing ,
103- supports_implicit_caching = nothing ,
104- status = nothing
105- )
106-
107- push! (cached. endpoints. endpoints, new_endpoint)
108- count += 1
75+ ZERO_PRICING
10976 end
77+
78+ new_endpoint = ProviderEndpoint (;
79+ name = native_id,
80+ model_name = native_id,
81+ context_length = cached. model. context_length,
82+ pricing = base_pricing,
83+ provider_name = provider_name,
84+ tag = provider_name,
85+ quantization = nothing ,
86+ max_completion_tokens = nothing ,
87+ max_prompt_tokens = nothing ,
88+ supported_parameters = nothing ,
89+ uptime_last_30m = nothing ,
90+ supports_implicit_caching = nothing ,
91+ status = nothing
92+ )
93+
94+ push! (cached. endpoints. endpoints, new_endpoint)
95+ count += 1
11096 end
11197
112- action = mutate ? " Mutated" : " Injected"
113- @info " $action $count cli_proxy_api endpoints"
98+ @info " Injected $count cli_proxy_api endpoints"
11499 return count
115100end
116101
102+ # ============ Provider Override ============
103+
104+ """
105+ override_providers!(base_url, api_key_env_var)
106+
107+ Override anthropic and openai providers to route through cli_proxy_api.
108+ """
109+ function override_providers! (base_url:: String , api_key_env_var:: String )
110+ for name in (" anthropic" , " openai" )
111+ set_provider! (
112+ name,
113+ base_url,
114+ " Bearer" ,
115+ api_key_env_var,
116+ Dict {String,String} (),
117+ cli_proxy_model_transform,
118+ ChatCompletionSchema (),
119+ " $name (overridden to cli_proxy_api)"
120+ )
121+ end
122+ @info " Overrode anthropic and openai providers to route through cli_proxy_api"
123+ end
124+
117125# ============ Setup ============
118126
119127"""
@@ -128,14 +136,16 @@ Complete setup for CLI proxy:
1281361. Register the cli_proxy_api provider
1291372. Inject endpoints into all mapped models
130138
131- Set `mutate=true` to overwrite existing endpoints instead of adding new ones.
139+ Set `mutate=true` to override original providers (anthropic, openai) to route through cli_proxy_api.
140+ This allows `anthropic:anthropic/claude-sonnet-4.5` to transparently route through the proxy.
132141"""
133142function setup_cli_proxy! (;
134143 base_url:: String = " http://localhost:8317/v1" ,
135144 api_key_env_var:: String = " CLIPROXYAPI_API_KEY" ,
136145 provider_name:: String = " cli_proxy_api" ,
137146 mutate:: Bool = false
138147)
148+ # Always register cli_proxy_api provider
139149 add_provider (
140150 provider_name,
141151 base_url,
@@ -147,7 +157,11 @@ function setup_cli_proxy!(;
147157 " CLI Proxy API - routes to local proxy"
148158 )
149159
150- count = inject_cli_proxy_endpoints! (provider_name; mutate)
160+ if mutate
161+ override_providers! (base_url, api_key_env_var)
162+ end
163+
164+ count = mutate ? 0 : inject_cli_proxy_endpoints! (provider_name)
151165
152166 @info " CLI Proxy setup complete" provider_name base_url mutate
153167 return count
0 commit comments