77 normalizeTaskSettings ,
88 type TaskSettings ,
99 type SubagentAiDefaults ,
10+ type SubagentAiDefaultsEntry ,
1011} from "@/common/types/tasks" ;
1112import { BUILT_IN_SUBAGENTS } from "@/common/constants/agents" ;
1213import type { ThinkingLevel } from "@/common/types/thinking" ;
@@ -19,7 +20,33 @@ import {
1920 SelectTrigger ,
2021 SelectValue ,
2122} from "@/browser/components/ui/select" ;
22- import { enforceThinkingPolicy , getThinkingPolicyForModel } from "@/browser/utils/thinking/policy" ;
23+ import { enforceThinkingPolicy , getThinkingPolicyForModel } from "@/common/utils/thinking/policy" ;
24+
25+ const INHERIT = "__inherit__" ;
26+ const ALL_THINKING_LEVELS = [ "off" , "low" , "medium" , "high" , "xhigh" ] as const ;
27+
28+ function updateSubagentDefaultEntry (
29+ previous : SubagentAiDefaults ,
30+ agentType : string ,
31+ update : ( entry : SubagentAiDefaultsEntry ) => void
32+ ) : SubagentAiDefaults {
33+ const next = { ...previous } ;
34+ const existing = next [ agentType ] ?? { } ;
35+ const updated : SubagentAiDefaultsEntry = { ...existing } ;
36+ update ( updated ) ;
37+
38+ if ( updated . modelString && updated . thinkingLevel ) {
39+ updated . thinkingLevel = enforceThinkingPolicy ( updated . modelString , updated . thinkingLevel ) ;
40+ }
41+
42+ if ( ! updated . modelString && ! updated . thinkingLevel ) {
43+ delete next [ agentType ] ;
44+ } else {
45+ next [ agentType ] = updated ;
46+ }
47+
48+ return next ;
49+ }
2350
2451export function TasksSection ( ) {
2552 const { api } = useAPI ( ) ;
@@ -30,6 +57,10 @@ export function TasksSection() {
3057 const [ saveError , setSaveError ] = useState < string | null > ( null ) ;
3158 const saveTimerRef = useRef < ReturnType < typeof setTimeout > | null > ( null ) ;
3259 const savingRef = useRef ( false ) ;
60+ const pendingSaveRef = useRef < {
61+ taskSettings : TaskSettings ;
62+ subagentAiDefaults : SubagentAiDefaults ;
63+ } | null > ( null ) ;
3364
3465 const { models, hiddenModels } = useModelsFromSettings ( ) ;
3566
@@ -74,23 +105,36 @@ export function TasksSection() {
74105 if ( ! api ) return ;
75106 if ( ! loaded ) return ;
76107 if ( loadFailed ) return ;
77- if ( savingRef . current ) return ;
108+
109+ pendingSaveRef . current = { taskSettings, subagentAiDefaults } ;
78110
79111 if ( saveTimerRef . current ) {
80112 clearTimeout ( saveTimerRef . current ) ;
81113 saveTimerRef . current = null ;
82114 }
83115
84116 saveTimerRef . current = setTimeout ( ( ) => {
85- savingRef . current = true ;
86- void api . config
87- . saveConfig ( { taskSettings, subagentAiDefaults } )
88- . catch ( ( error : unknown ) => {
89- setSaveError ( error instanceof Error ? error . message : String ( error ) ) ;
90- } )
91- . finally ( ( ) => {
92- savingRef . current = false ;
93- } ) ;
117+ const flush = ( ) => {
118+ if ( savingRef . current ) return ;
119+ if ( ! api ) return ;
120+
121+ const payload = pendingSaveRef . current ;
122+ if ( ! payload ) return ;
123+
124+ pendingSaveRef . current = null ;
125+ savingRef . current = true ;
126+ void api . config
127+ . saveConfig ( payload )
128+ . catch ( ( error : unknown ) => {
129+ setSaveError ( error instanceof Error ? error . message : String ( error ) ) ;
130+ } )
131+ . finally ( ( ) => {
132+ savingRef . current = false ;
133+ flush ( ) ;
134+ } ) ;
135+ } ;
136+
137+ flush ( ) ;
94138 } , 400 ) ;
95139
96140 return ( ) => {
@@ -111,57 +155,29 @@ export function TasksSection() {
111155 setTaskSettings ( ( prev ) => normalizeTaskSettings ( { ...prev , maxTaskNestingDepth : parsed } ) ) ;
112156 } ;
113157
114- const INHERIT = "__inherit__" ;
115-
116158 const setSubagentModel = ( agentType : string , value : string ) => {
117- setSubagentAiDefaults ( ( prev ) => {
118- const next = { ...prev } ;
119- const existing = next [ agentType ] ?? { } ;
120- const updated = { ...existing } ;
121-
122- if ( value === INHERIT ) {
123- delete updated . modelString ;
124- } else {
125- updated . modelString = value ;
126- }
127-
128- if ( updated . modelString && updated . thinkingLevel ) {
129- updated . thinkingLevel = enforceThinkingPolicy ( updated . modelString , updated . thinkingLevel ) ;
130- }
131-
132- if ( ! updated . modelString && ! updated . thinkingLevel ) {
133- delete next [ agentType ] ;
134- } else {
135- next [ agentType ] = updated ;
136- }
137-
138- return next ;
139- } ) ;
159+ setSubagentAiDefaults ( ( prev ) =>
160+ updateSubagentDefaultEntry ( prev , agentType , ( updated ) => {
161+ if ( value === INHERIT ) {
162+ delete updated . modelString ;
163+ } else {
164+ updated . modelString = value ;
165+ }
166+ } )
167+ ) ;
140168 } ;
141169
142170 const setSubagentThinking = ( agentType : string , value : string ) => {
143- setSubagentAiDefaults ( ( prev ) => {
144- const next = { ...prev } ;
145- const existing = next [ agentType ] ?? { } ;
146- const updated = { ...existing } ;
147-
148- if ( value === INHERIT ) {
149- delete updated . thinkingLevel ;
150- } else {
151- const requested = value as ThinkingLevel ;
152- updated . thinkingLevel = updated . modelString
153- ? enforceThinkingPolicy ( updated . modelString , requested )
154- : requested ;
155- }
171+ setSubagentAiDefaults ( ( prev ) =>
172+ updateSubagentDefaultEntry ( prev , agentType , ( updated ) => {
173+ if ( value === INHERIT ) {
174+ delete updated . thinkingLevel ;
175+ return ;
176+ }
156177
157- if ( ! updated . modelString && ! updated . thinkingLevel ) {
158- delete next [ agentType ] ;
159- } else {
160- next [ agentType ] = updated ;
161- }
162-
163- return next ;
164- } ) ;
178+ updated . thinkingLevel = value as ThinkingLevel ;
179+ } )
180+ ) ;
165181 } ;
166182
167183 return (
@@ -224,9 +240,7 @@ export function TasksSection() {
224240 const modelValue = entry ?. modelString ?? INHERIT ;
225241 const thinkingValue = entry ?. thinkingLevel ?? INHERIT ;
226242 const allowedThinkingLevels =
227- modelValue !== INHERIT
228- ? getThinkingPolicyForModel ( modelValue )
229- : ( [ "off" , "low" , "medium" , "high" , "xhigh" ] as const ) ;
243+ modelValue !== INHERIT ? getThinkingPolicyForModel ( modelValue ) : ALL_THINKING_LEVELS ;
230244
231245 return (
232246 < div
0 commit comments