@@ -321,9 +321,166 @@ async function fetchAWFReflect(options) {
321321 }
322322}
323323
324+ /**
325+ * Returns true when the model name matches well-known Anthropic naming patterns:
326+ * "claude-*" prefix, or "-opus", "-haiku", or "-sonnet" as a segment or suffix.
327+ *
328+ * @param {string } model - Lower-cased, trimmed model name.
329+ * @returns {boolean }
330+ */
331+ function isAnthropicModelName ( model ) {
332+ return model . startsWith ( "claude-" ) || model . includes ( "-opus-" ) || model . endsWith ( "-opus" ) || model . includes ( "-haiku-" ) || model . endsWith ( "-haiku" ) || model . includes ( "-sonnet-" ) || model . endsWith ( "-sonnet" ) ;
333+ }
334+
335+ /**
336+ * Returns true when the model name matches well-known OpenAI naming patterns:
337+ * "gpt-*" prefix, or o1/o3/o4 reasoning models.
338+ *
339+ * @param {string } model - Lower-cased, trimmed model name.
340+ * @returns {boolean }
341+ */
342+ function isOpenAIModelName ( model ) {
343+ return model . startsWith ( "gpt-" ) || / ^ o [ 1 3 4 ] [ - . ] / . test ( model ) || model === "o1" || model === "o3" || model === "o4" ;
344+ }
345+
346+ /**
347+ * Look up a model entry in the models.json catalog, case-insensitively.
348+ *
349+ * @param {object | null | undefined } modelsJson
350+ * @param {string } modelName
351+ * @param {string | null | undefined } [providerName]
352+ * @returns {object | null }
353+ */
354+ function getCatalogModelEntry ( modelsJson , modelName , providerName ) {
355+ const model = String ( modelName || "" )
356+ . toLowerCase ( )
357+ . trim ( ) ;
358+ const provider = String ( providerName || "" )
359+ . toLowerCase ( )
360+ . trim ( ) ;
361+ if ( ! model || modelsJson == null || typeof modelsJson !== "object" || Array . isArray ( modelsJson ) ) {
362+ return null ;
363+ }
364+ const providers = modelsJson . providers ;
365+ if ( ! providers || typeof providers !== "object" || Array . isArray ( providers ) ) {
366+ return null ;
367+ }
368+ const providerEntries = provider
369+ ? Object . entries ( providers ) . filter (
370+ ( [ name ] ) =>
371+ String ( name || "" )
372+ . toLowerCase ( )
373+ . trim ( ) === provider
374+ )
375+ : Object . entries ( providers ) ;
376+ for ( const [ , providerData ] of providerEntries ) {
377+ const models = providerData && typeof providerData === "object" ? providerData . models : null ;
378+ if ( ! models || typeof models !== "object" || Array . isArray ( models ) ) continue ;
379+ for ( const [ catalogModel , catalogEntry ] of Object . entries ( models ) ) {
380+ if (
381+ String ( catalogModel || "" )
382+ . toLowerCase ( )
383+ . trim ( ) === model
384+ ) {
385+ return catalogEntry && typeof catalogEntry === "object" && ! Array . isArray ( catalogEntry ) ? catalogEntry : null ;
386+ }
387+ }
388+ }
389+ return null ;
390+ }
391+
392+ /**
393+ * Infer the Copilot SDK provider type for a given endpoint provider name and model name.
394+ *
395+ * The SDK's `ProviderConfig.type` field determines which API format the SDK uses when
396+ * communicating with the `baseUrl` endpoint:
397+ * - "anthropic" — Anthropic Messages API (required for direct Anthropic API endpoints)
398+ * - "azure" — Azure OpenAI API
399+ * - "openai" — OpenAI-compatible API (default; used for Copilot, OpenAI, Gemini, etc.)
400+ *
401+ * Resolution order:
402+ * 1. Endpoint provider name mapping (most authoritative for BYOK endpoints).
403+ * 2. Model catalog lookup via the `modelsJson` catalog (explicit `provider_type` field).
404+ * 3. Well-known model name heuristics (e.g. "claude-*" → "anthropic", "gpt-*" → "openai").
405+ * 4. Default: "openai".
406+ *
407+ * @param {string } endpointProvider - The `provider` field from the AWF reflect endpoint entry.
408+ * @param {string } modelName - The resolved model name to use for heuristic fallback.
409+ * @param {object | null | undefined } catalogEntryOrModelsJson - Matching models.json catalog entry or full catalog (optional).
410+ * @returns {"openai" | "azure" | "anthropic" }
411+ */
412+ function inferProviderTypeForModel ( endpointProvider , modelName , catalogEntryOrModelsJson ) {
413+ // 1. Endpoint provider name mapping.
414+ const ep = String ( endpointProvider || "" )
415+ . toLowerCase ( )
416+ . trim ( ) ;
417+ if ( ep === "anthropic" ) return "anthropic" ;
418+ if ( ep === "azure" || ep === "azure-openai" || ep === "azure_openai" ) return "azure" ;
419+ if ( ep === "openai" ) return "openai" ;
420+ // For "copilot", "github-copilot", and unknown providers, fall through to model-based lookup.
421+
422+ const model = String ( modelName || "" )
423+ . toLowerCase ( )
424+ . trim ( ) ;
425+ const catalogEntry =
426+ catalogEntryOrModelsJson && typeof catalogEntryOrModelsJson === "object" && ! Array . isArray ( catalogEntryOrModelsJson ) && "providers" in catalogEntryOrModelsJson
427+ ? getCatalogModelEntry ( catalogEntryOrModelsJson , model )
428+ : catalogEntryOrModelsJson ;
429+
430+ // 2. Model catalog lookup.
431+ if ( model ) {
432+ const pt = catalogEntry && typeof catalogEntry . provider_type === "string" ? catalogEntry . provider_type . trim ( ) : "" ;
433+ if ( pt === "anthropic" || pt === "azure" || pt === "openai" ) return /** @type {"openai" | "azure" | "anthropic" } */ pt ;
434+ }
435+
436+ // 3. Well-known model name heuristics.
437+ if ( model ) {
438+ if ( isAnthropicModelName ( model ) ) return "anthropic" ;
439+ if ( isOpenAIModelName ( model ) ) return "openai" ;
440+ }
441+
442+ // 4. Default.
443+ return "openai" ;
444+ }
445+
446+ /**
447+ * Infer the SDK wire API for a model.
448+ *
449+ * Resolution order:
450+ * 1. For Anthropic provider types: undefined (wireApi ignored by SDK).
451+ * 2. `models.json` explicit `wire_api`/`wireApi`.
452+ * 3. Heuristic default for OpenAI/Azure-compatible models: "completions".
453+ *
454+ * @param {"openai" | "azure" | "anthropic" } providerType
455+ * @param {string } modelName
456+ * @param {object | null | undefined } catalogEntryOrModelsJson
457+ * @returns {"completions" | "responses" | undefined }
458+ */
459+ function inferWireApiForModel ( providerType , modelName , catalogEntryOrModelsJson ) {
460+ if ( providerType === "anthropic" ) {
461+ return undefined ;
462+ }
463+ const model = String ( modelName || "" ) . trim ( ) ;
464+ if ( ! model ) return undefined ;
465+ const catalogEntry =
466+ catalogEntryOrModelsJson && typeof catalogEntryOrModelsJson === "object" && ! Array . isArray ( catalogEntryOrModelsJson ) && "providers" in catalogEntryOrModelsJson
467+ ? getCatalogModelEntry ( catalogEntryOrModelsJson , model )
468+ : catalogEntryOrModelsJson ;
469+ // Keep the camelCase fallback for defensive compatibility with injected catalog
470+ // objects that bypass the normalized models.json pipeline.
471+ const rawWireApi = typeof catalogEntry ?. wire_api === "string" ? catalogEntry . wire_api : typeof catalogEntry ?. wireApi === "string" ? catalogEntry . wireApi : "" ;
472+ const normalizedWireApi = String ( rawWireApi || "" )
473+ . toLowerCase ( )
474+ . trim ( ) ;
475+ if ( normalizedWireApi === "responses" || normalizedWireApi === "completions" ) {
476+ return /** @type {"responses" | "completions" } */ normalizedWireApi ;
477+ }
478+ return "completions" ;
479+ }
480+
324481/**
325482 * Resolve Copilot SDK BYOK custom provider configuration from AWF /reflect data.
326- * Chooses a configured endpoint and maps it to an OpenAI-compatible provider base URL.
483+ * Chooses a configured endpoint and maps it to a provider base URL and type .
327484 * Returns null when no suitable endpoint is found (e.g. no reflect data, or endpoints not
328485 * configured).
329486 *
@@ -333,9 +490,10 @@ async function fetchAWFReflect(options) {
333490 * model?: string,
334491 * provider?: string,
335492 * reflectData: object | null | undefined,
493+ * modelsJson?: object | null,
336494 * logger?: (msg: string) => void,
337495 * }} [options]
338- * @returns {{ model: string, provider: { type: "openai", baseUrl: string } } | null }
496+ * @returns {{ model: string, provider: { type: "openai" | "azure" | "anthropic" , baseUrl: string, wireApi?: "completions" | "responses" } } | null }
339497 */
340498function resolveCopilotSDKCustomProviderFromReflect ( options ) {
341499 const configuredModel = typeof options ?. model === "string" ? options . model . trim ( ) : "" ;
@@ -386,10 +544,20 @@ function resolveCopilotSDKCustomProviderFromReflect(options) {
386544 return null ;
387545 }
388546
389- logger ( `sdk-mode: custom provider resolved from awf-reflect (provider=${ String ( endpoint . provider || "unknown" ) } baseUrl=${ baseUrl } model=${ model } )` ) ;
547+ const endpointProvider = String ( endpoint . provider || "" ) ;
548+ const catalogProviderName =
549+ String ( endpointProvider || "" )
550+ . toLowerCase ( )
551+ . trim ( ) === "copilot"
552+ ? "github-copilot"
553+ : endpointProvider ;
554+ const catalogEntry = getCatalogModelEntry ( options ?. modelsJson ?? null , model , catalogProviderName ) ;
555+ const providerType = inferProviderTypeForModel ( endpointProvider , model , catalogEntry ) ;
556+ const wireApi = inferWireApiForModel ( providerType , model , catalogEntry ) ;
557+ logger ( `sdk-mode: custom provider resolved from awf-reflect (provider=${ String ( endpoint . provider || "unknown" ) } type=${ providerType } baseUrl=${ baseUrl } model=${ model } ${ wireApi ? ` wireApi=${ wireApi } ` : "" } )` ) ;
390558 return {
391559 model,
392- provider : { type : "openai" , baseUrl } ,
560+ provider : { type : providerType , baseUrl, ... ( wireApi ? { wireApi } : { } ) } ,
393561 } ;
394562}
395563
@@ -407,6 +575,9 @@ if (typeof module !== "undefined" && module.exports) {
407575 extractModelIds,
408576 fetchAWFReflect,
409577 fetchModelsFromUrl,
578+ getCatalogModelEntry,
579+ inferProviderTypeForModel,
580+ inferWireApiForModel,
410581 resolveCopilotSDKCustomProviderFromReflect,
411582 } ;
412583}
0 commit comments