@@ -22,14 +22,18 @@ import {
2222 VERCEL_AI_GATEWAY_DEFAULT_MODEL_REF ,
2323 XIAOMI_DEFAULT_MODEL_REF ,
2424 ZAI_DEFAULT_MODEL_REF ,
25+ XAI_DEFAULT_MODEL_REF ,
2526} from "./onboard-auth.credentials.js" ;
2627import {
2728 buildMoonshotModelDefinition ,
29+ buildXaiModelDefinition ,
2830 KIMI_CODING_MODEL_REF ,
2931 MOONSHOT_BASE_URL ,
3032 MOONSHOT_CN_BASE_URL ,
3133 MOONSHOT_DEFAULT_MODEL_ID ,
3234 MOONSHOT_DEFAULT_MODEL_REF ,
35+ XAI_BASE_URL ,
36+ XAI_DEFAULT_MODEL_ID ,
3337} from "./onboard-auth.models.js" ;
3438
3539export function applyZaiConfig ( cfg : OpenClawConfig ) : OpenClawConfig {
@@ -588,6 +592,71 @@ export function applyVeniceConfig(cfg: OpenClawConfig): OpenClawConfig {
588592 } ;
589593}
590594
595+ export function applyXaiProviderConfig ( cfg : OpenClawConfig ) : OpenClawConfig {
596+ const models = { ...cfg . agents ?. defaults ?. models } ;
597+ models [ XAI_DEFAULT_MODEL_REF ] = {
598+ ...models [ XAI_DEFAULT_MODEL_REF ] ,
599+ alias : models [ XAI_DEFAULT_MODEL_REF ] ?. alias ?? "Grok" ,
600+ } ;
601+
602+ const providers = { ...cfg . models ?. providers } ;
603+ const existingProvider = providers . xai ;
604+ const existingModels = Array . isArray ( existingProvider ?. models ) ? existingProvider . models : [ ] ;
605+ const defaultModel = buildXaiModelDefinition ( ) ;
606+ const hasDefaultModel = existingModels . some ( ( model ) => model . id === XAI_DEFAULT_MODEL_ID ) ;
607+ const mergedModels = hasDefaultModel ? existingModels : [ ...existingModels , defaultModel ] ;
608+ const { apiKey : existingApiKey , ...existingProviderRest } = ( existingProvider ?? { } ) as Record <
609+ string ,
610+ unknown
611+ > as { apiKey ?: string } ;
612+ const resolvedApiKey = typeof existingApiKey === "string" ? existingApiKey : undefined ;
613+ const normalizedApiKey = resolvedApiKey ?. trim ( ) ;
614+ providers . xai = {
615+ ...existingProviderRest ,
616+ baseUrl : XAI_BASE_URL ,
617+ api : "openai-completions" ,
618+ ...( normalizedApiKey ? { apiKey : normalizedApiKey } : { } ) ,
619+ models : mergedModels . length > 0 ? mergedModels : [ defaultModel ] ,
620+ } ;
621+
622+ return {
623+ ...cfg ,
624+ agents : {
625+ ...cfg . agents ,
626+ defaults : {
627+ ...cfg . agents ?. defaults ,
628+ models,
629+ } ,
630+ } ,
631+ models : {
632+ mode : cfg . models ?. mode ?? "merge" ,
633+ providers,
634+ } ,
635+ } ;
636+ }
637+
638+ export function applyXaiConfig ( cfg : OpenClawConfig ) : OpenClawConfig {
639+ const next = applyXaiProviderConfig ( cfg ) ;
640+ const existingModel = next . agents ?. defaults ?. model ;
641+ return {
642+ ...next ,
643+ agents : {
644+ ...next . agents ,
645+ defaults : {
646+ ...next . agents ?. defaults ,
647+ model : {
648+ ...( existingModel && "fallbacks" in ( existingModel as Record < string , unknown > )
649+ ? {
650+ fallbacks : ( existingModel as { fallbacks ?: string [ ] } ) . fallbacks ,
651+ }
652+ : undefined ) ,
653+ primary : XAI_DEFAULT_MODEL_REF ,
654+ } ,
655+ } ,
656+ } ,
657+ } ;
658+ }
659+
591660export function applyAuthProfileConfig (
592661 cfg : OpenClawConfig ,
593662 params : {
0 commit comments