@@ -12,22 +12,32 @@ import {
1212// offer ≥256K context; users can raise it per model via the flag.
1313const DEFAULT_CONTEXT_WINDOW = 256000 ;
1414
15+ const PROVIDER_ID = "bailian-cli" ;
16+
17+ function readPrimary ( defaults : Record < string , unknown > ) : string | undefined {
18+ const model = defaults . model ;
19+ if ( ! model || typeof model !== "object" ) return undefined ;
20+ const primary = ( model as Record < string , unknown > ) . primary ;
21+ return typeof primary === "string" && primary . trim ( ) !== "" ? primary . trim ( ) : undefined ;
22+ }
23+
1524export default {
1625 label : "OpenClaw" ,
1726 write ( { baseUrl, apiKey, model, contextWindow } ) {
1827 const configPath = join ( homedir ( ) , ".openclaw" , "openclaw.json" ) ;
28+ const warnings : string [ ] = [ ] ;
29+ const modelRef = `${ PROVIDER_ID } /${ model } ` ;
1930
2031 backup ( configPath ) ;
2132 const config = readJson ( configPath ) ;
2233
23- // models.providers["bailian-cli"]
34+ // models.providers["bailian-cli"] — upsert without removing other providers
35+ // (e.g. an existing working bailian-token-plan setup).
2436 const models = ( config . models ?? { } ) as Record < string , unknown > ;
2537 models . mode = "merge" ;
2638 const providers = ( models . providers ?? { } ) as Record < string , unknown > ;
27- const api = isAnthropicEndpoint ( baseUrl )
28- ? "anthropic-messages"
29- : "openai-completions" ;
30- providers [ "bailian-cli" ] = {
39+ const api = isAnthropicEndpoint ( baseUrl ) ? "anthropic-messages" : "openai-completions" ;
40+ providers [ PROVIDER_ID ] = {
3141 baseUrl,
3242 apiKey,
3343 api,
@@ -43,14 +53,27 @@ export default {
4353 models . providers = providers ;
4454 config . models = models ;
4555
46- // agents.defaults — select the model and register it in the allowlist.
56+ // agents.defaults — register the model in the allow-list. Only set primary
57+ // when unset, or when primary already points at bailian-cli (reconfigure).
58+ // Never steal primary away from another provider such as bailian-token-plan.
4759 const agents = ( config . agents ?? { } ) as Record < string , unknown > ;
4860 const defaults = ( agents . defaults ?? { } ) as Record < string , unknown > ;
49- const primary = `bailian-cli/${ model } ` ;
50- defaults . model = { primary } ;
51- const allowlist = ( defaults . models ?? { } ) as Record < string , unknown > ;
52- allowlist [ primary ] = allowlist [ primary ] ?? { } ;
53- defaults . models = allowlist ;
61+ const allowedModels = ( defaults . models ?? { } ) as Record < string , unknown > ;
62+ allowedModels [ modelRef ] = allowedModels [ modelRef ] ?? { } ;
63+ defaults . models = allowedModels ;
64+
65+ const existingPrimary = readPrimary ( defaults ) ;
66+ if ( ! existingPrimary ) {
67+ defaults . model = { primary : modelRef } ;
68+ } else if ( existingPrimary . startsWith ( `${ PROVIDER_ID } /` ) ) {
69+ defaults . model = { primary : modelRef } ;
70+ } else {
71+ warnings . push (
72+ `Left existing primary model unchanged ("${ existingPrimary } "). ` +
73+ `Added provider "${ PROVIDER_ID } " — switch to "${ modelRef } " in OpenClaw if you want to use it.` ,
74+ ) ;
75+ }
76+
5477 agents . defaults = defaults ;
5578 config . agents = agents ;
5679
@@ -60,6 +83,7 @@ export default {
6083 paths : [ configPath ] ,
6184 nextStep :
6285 "Run `openclaw gateway restart`, then `openclaw` to start using OpenClaw with DashScope." ,
86+ warnings : warnings . length > 0 ? warnings : undefined ,
6387 } ;
6488 } ,
6589} satisfies AgentDef ;
0 commit comments