@@ -19,6 +19,8 @@ import { GetUsableModelsResponseSchema } from "./gen/agent_pb";
1919const CURSOR_GET_USABLE_MODELS_PATH = "/agent.v1.AgentService/GetUsableModels" ;
2020const CURSOR_DISCOVERY_CLIENT_VERSION = "cli-2026.02.13-41ac335" ;
2121const CURSOR_MODEL_DISCOVERY_MAX_BYTES = 4 * 1024 * 1024 ;
22+ type CursorUsableModelsFetcher = ( opts : CursorUsableModelsOptions ) => Promise < CursorUsableModelsResult > ;
23+ let cursorUsableModelsFetcherForTests : CursorUsableModelsFetcher | null = null ;
2224
2325export interface CursorUsableModelsOptions {
2426 apiKey : string ;
@@ -31,6 +33,11 @@ export type CursorUsableModelsResult =
3133 | { ok : true ; models : string [ ] }
3234 | { ok : false ; error : "auth" | "http" | "transport" | "timeout" | "decode" | "empty" | "too_large" ; detail ?: string } ;
3335
36+ /** Test-only seam for management connectivity probes; production callers retain the HTTP/2 path. */
37+ export function setFetchCursorUsableModelsForTests ( next : CursorUsableModelsFetcher | null ) : void {
38+ cursorUsableModelsFetcherForTests = next ;
39+ }
40+
3441const RETRYABLE_DISCOVERY_ERRORS = new Set ( [ "timeout" , "transport" ] ) ;
3542const DISCOVERY_RETRY_TIMEOUT_MS = 3_000 ;
3643
@@ -42,10 +49,37 @@ const DISCOVERY_RETRY_TIMEOUT_MS = 3_000;
4249 * devlog 260723_cursor_context_continuity/030).
4350 */
4451export async function fetchCursorUsableModels ( opts : CursorUsableModelsOptions ) : Promise < CursorUsableModelsResult > {
45- const first = await fetchCursorUsableModelsOnce ( opts ) ;
52+ if ( cursorUsableModelsFetcherForTests ) return cursorUsableModelsFetcherForTests ( opts ) ;
53+ const resolved = resolveCursorDiscoveryBaseUrl ( opts . baseUrl ?? "https://api2.cursor.sh" ) ;
54+ if ( ! resolved . ok ) return resolved ;
55+ const first = await fetchCursorUsableModelsOnce ( { ...opts , baseUrl : resolved . baseUrl } ) ;
4656 if ( first . ok || ! RETRYABLE_DISCOVERY_ERRORS . has ( first . error ) ) return first ;
4757 await new Promise ( resolve => setTimeout ( resolve , 250 + Math . floor ( Math . random ( ) * 250 ) ) ) ;
48- return fetchCursorUsableModelsOnce ( { ...opts , timeoutMs : Math . min ( opts . timeoutMs ?? 8000 , DISCOVERY_RETRY_TIMEOUT_MS ) } ) ;
58+ return fetchCursorUsableModelsOnce ( {
59+ ...opts ,
60+ baseUrl : resolved . baseUrl ,
61+ timeoutMs : Math . min ( opts . timeoutMs ?? 8000 , DISCOVERY_RETRY_TIMEOUT_MS ) ,
62+ } ) ;
63+ }
64+
65+ function resolveCursorDiscoveryBaseUrl ( raw : string ) : { ok : true ; baseUrl : string } | Extract < CursorUsableModelsResult , { ok : false } > {
66+ const baseUrl = raw . replace ( / \/ + $ / , "" ) ;
67+ let parsed : URL ;
68+ try {
69+ parsed = new URL ( baseUrl ) ;
70+ } catch {
71+ return { ok : false , error : "transport" , detail : "Cursor discovery URL is invalid" } ;
72+ }
73+ if ( parsed . protocol === "https:" ) return { ok : true , baseUrl } ;
74+ // Local h2c fixtures (and an operator loopback proxy) never leave the machine.
75+ // Anything else with a Bearer token must be HTTPS, matching providerOutbound POST.
76+ if ( parsed . protocol === "http:" ) {
77+ const host = parsed . hostname . replace ( / ^ \[ | \] $ / g, "" ) . toLowerCase ( ) ;
78+ if ( host === "127.0.0.1" || host === "::1" || host === "localhost" || host . endsWith ( ".localhost" ) ) {
79+ return { ok : true , baseUrl } ;
80+ }
81+ }
82+ return { ok : false , error : "transport" , detail : "Cursor discovery URL must use HTTPS" } ;
4983}
5084
5185async function fetchCursorUsableModelsOnce ( opts : CursorUsableModelsOptions ) : Promise < CursorUsableModelsResult > {
0 commit comments