11// Resolves one concrete agent owner for onboarding auth, model, workspace, and session effects.
22import {
3+ listAgentEntries ,
34 resolveAgentDir ,
45 resolveAgentWorkspaceDir ,
56 resolveMutableAgentEntry ,
67 resolveSoleAgentId ,
8+ toAgentEntriesRecord ,
79} from "../agents/agent-scope-config.js" ;
810import { tryResolveLegacyCompatibilityAgentId } from "../config/legacy.default-agent-owner.js" ;
911import {
@@ -12,6 +14,7 @@ import {
1214 resolveAgentModelFallbackValues ,
1315} from "../config/model-input.js" ;
1416import type { OptionalBootstrapFileName } from "../config/types.agent-defaults.js" ;
17+ import type { AgentEntryConfig } from "../config/types.agents.js" ;
1518import type { OpenClawConfig } from "../config/types.openclaw.js" ;
1619import { formatErrorMessage } from "../infra/errors.js" ;
1720import { applyPrimaryModel } from "../plugins/provider-model-primary.js" ;
@@ -66,15 +69,37 @@ export async function ensureOnboardingAgentWorkspace(
6669 }
6770}
6871
72+ function replaceOnboardingAgentEntry (
73+ config : OpenClawConfig ,
74+ updated : OpenClawConfig ,
75+ target : OnboardingAgentTarget ,
76+ nextEntry : AgentEntryConfig ,
77+ ) : OpenClawConfig {
78+ const entries = listAgentEntries ( config ) ;
79+ const index = entries . findIndex ( ( entry ) => normalizeAgentId ( entry . id ) === target . agentId ) ;
80+ const nextEntries = [ ...entries ] ;
81+ const replacement = { id : index >= 0 ? entries [ index ] ! . id : target . agentId , ...nextEntry } ;
82+ if ( index >= 0 ) {
83+ nextEntries [ index ] = replacement ;
84+ } else {
85+ nextEntries . push ( replacement ) ;
86+ }
87+ const { list : _list , entries : _entries , ...agents } = config . agents ?? { } ;
88+ return {
89+ ...updated ,
90+ agents : {
91+ ...agents ,
92+ entries : toAgentEntriesRecord ( nextEntries ) ,
93+ } ,
94+ } ;
95+ }
96+
6997export function applyOnboardingPrimaryModel (
7098 config : OpenClawConfig ,
7199 target : OnboardingAgentTarget ,
72100 model : string ,
73101) : OpenClawConfig {
74- const authoredEntryKey = Object . keys ( config . agents ?. entries ?? { } ) . find (
75- ( key ) => normalizeAgentId ( key ) === target . agentId ,
76- ) ;
77- const entry = authoredEntryKey ? config . agents ?. entries ?. [ authoredEntryKey ] : undefined ;
102+ const entry = resolveMutableAgentEntry ( config , target . agentId ) ;
78103 if ( entry ?. model === undefined && config . agents ?. ownership !== "explicit" ) {
79104 return applyPrimaryModel ( config , model ) ;
80105 }
@@ -84,26 +109,17 @@ export function applyOnboardingPrimaryModel(
84109 normalizeAgentModelRefForConfig ( fallback ) ,
85110 ) ;
86111 const models = normalizeAgentModelMapForConfig ( entry ?. models ?? { } ) ;
87- return {
88- ...config ,
89- agents : {
90- ...config . agents ,
91- entries : {
92- ...config . agents ?. entries ,
93- [ authoredEntryKey ?? target . agentId ] : {
94- ...entry ,
95- model : {
96- ...( fallbackValues . length > 0 ? { fallbacks : fallbackValues } : { } ) ,
97- primary,
98- } ,
99- models : {
100- ...models ,
101- [ primary ] : models [ primary ] ?? { } ,
102- } ,
103- } ,
104- } ,
112+ return replaceOnboardingAgentEntry ( config , config , target , {
113+ ...entry ,
114+ model : {
115+ ...( fallbackValues . length > 0 ? { fallbacks : fallbackValues } : { } ) ,
116+ primary,
105117 } ,
106- } ;
118+ models : {
119+ ...models ,
120+ [ primary ] : models [ primary ] ?? { } ,
121+ } ,
122+ } ) ;
107123}
108124
109125/** Apply a model-default mutation to one agent without flattening it globally. */
@@ -140,9 +156,6 @@ export function projectAgentModelDefaults(
140156 }
141157 const updatedDefaults = updated . agents ?. defaults ;
142158 const { model : _model , models : _models , modelPolicy : _modelPolicy , ...entryRest } = entry ?? { } ;
143- const authoredEntryKey = Object . keys ( config . agents ?. entries ?? { } ) . find (
144- ( key ) => normalizeAgentId ( key ) === target . agentId ,
145- ) ;
146159 const nextEntry = {
147160 ...entryRest ,
148161 ...( updatedDefaults ?. model !== undefined ? { model : updatedDefaults . model } : { } ) ,
@@ -151,14 +164,5 @@ export function projectAgentModelDefaults(
151164 ? { modelPolicy : updatedDefaults . modelPolicy }
152165 : { } ) ,
153166 } ;
154- return {
155- ...updated ,
156- agents : {
157- ...config . agents ,
158- entries : {
159- ...config . agents ?. entries ,
160- [ authoredEntryKey ?? target . agentId ] : nextEntry ,
161- } ,
162- } ,
163- } ;
167+ return replaceOnboardingAgentEntry ( config , updated , target , nextEntry ) ;
164168}
0 commit comments