@@ -25,8 +25,11 @@ import {
2525import { useEnvironments } from "../../state/environments" ;
2626import { useProjects } from "../../state/entities" ;
2727import { agentRosterQuery } from "../../state/agents" ;
28- import { useEnvironmentQuery } from "../../state/query" ;
28+ import { formatEnvironmentQueryError , useEnvironmentQuery } from "../../state/query" ;
2929import { EMPTY_SERVER_PROVIDERS } from "../../state/server" ;
30+ import { appAtomRegistry } from "../../rpc/atomRegistry" ;
31+ import { Button } from "../ui/button" ;
32+ import { RefreshIcon } from "../ui/refresh-icon" ;
3033import { resolveEnvModeLabel , WORKTREE_SUBMODULES_LABELS } from "../BranchToolbar.logic" ;
3134import { ProviderModelPicker } from "../chat/ProviderModelPicker" ;
3235import { AgentPicker } from "../chat/AgentPicker" ;
@@ -156,9 +159,19 @@ export function ProjectDefaultsSettings({ category }: { category: ProjectSetting
156159 updateSettings ( { defaultModelSelection : value } ) ;
157160 } ;
158161
162+ interface TargetRosterState {
163+ readonly candidate : ( typeof targets ) [ number ] ;
164+ readonly queryAtom : ReturnType < typeof agentRosterQuery > | null ;
165+ readonly status : "pending" | "success" | "failure" ;
166+ readonly error : string | null ;
167+ readonly isPending : boolean ;
168+ readonly isSuccess : boolean ;
169+ readonly agents : ReadonlyArray < AgentSummary > | null ;
170+ }
171+
159172 const targetRostersAtom = useMemo (
160173 ( ) =>
161- Atom . make ( ( get ) =>
174+ Atom . make ( ( get ) : ReadonlyArray < TargetRosterState > =>
162175 targets . map ( ( candidate ) => {
163176 const candidateProjectId =
164177 candidate . projectId ??
@@ -167,6 +180,11 @@ export function ProjectDefaultsSettings({ category }: { category: ProjectSetting
167180 if ( ! candidateProjectId ) {
168181 return {
169182 candidate,
183+ queryAtom : null ,
184+ status : "success" ,
185+ error : null ,
186+ isPending : false ,
187+ isSuccess : true ,
170188 agents : [ ] as ReadonlyArray < AgentSummary > ,
171189 } ;
172190 }
@@ -175,12 +193,18 @@ export function ProjectDefaultsSettings({ category }: { category: ProjectSetting
175193 input : { projectId : candidateProjectId } ,
176194 } ) ;
177195 const result = get ( queryAtom ) ;
178- const agents = Option . getOrElse (
179- AsyncResult . value ( result ) ,
180- ( ) => [ ] as readonly AgentSummary [ ] ,
181- ) ;
196+ const isPending = result . waiting || result . _tag === "Initial" ;
197+ const isSuccess = result . _tag === "Success" ;
198+ const error =
199+ result . _tag === "Failure" ? formatEnvironmentQueryError ( result . cause ) : null ;
200+ const agents = Option . getOrNull ( AsyncResult . value ( result ) ) ;
182201 return {
183202 candidate,
203+ queryAtom,
204+ status : isPending ? "pending" : result . _tag === "Failure" ? "failure" : "success" ,
205+ error,
206+ isPending,
207+ isSuccess,
184208 agents,
185209 } ;
186210 } ) ,
@@ -191,18 +215,45 @@ export function ProjectDefaultsSettings({ category }: { category: ProjectSetting
191215
192216 function agentDisabledReason ( agentId : AgentId ) : string | null {
193217 for ( const targetRoster of targetRosters ) {
194- if ( ! targetRoster . agents . some ( ( agent ) => agent . id === agentId ) ) {
218+ if (
219+ targetRoster . agents !== null &&
220+ ! targetRoster . agents . some ( ( agent ) => agent . id === agentId )
221+ ) {
195222 return `This agent is unavailable on ${ targetRoster . candidate . label ?? "a selected target" } . Select that target to choose its agent separately.` ;
196223 }
197224 }
198225 return null ;
199226 }
200227
201228 const setAgent = ( value : AgentId | null ) => {
202- const reason = value ? agentDisabledReason ( value ) : null ;
203- if ( reason ) {
204- toastManager . add ( { type : "error" , title : "Default agent not saved" , description : reason } ) ;
205- return ;
229+ if ( value ) {
230+ const pendingTarget = targetRosters . find (
231+ ( target ) => target . isPending && target . agents === null ,
232+ ) ;
233+ if ( pendingTarget ) {
234+ toastManager . add ( {
235+ type : "error" ,
236+ title : "Default agent not saved" ,
237+ description : `Loading custom agents for ${ pendingTarget . candidate . label ?? "a selected target" } . Please wait before saving.` ,
238+ } ) ;
239+ return ;
240+ }
241+ const failedTarget = targetRosters . find (
242+ ( target ) => target . error !== null && target . agents === null ,
243+ ) ;
244+ if ( failedTarget ) {
245+ toastManager . add ( {
246+ type : "error" ,
247+ title : "Default agent not saved" ,
248+ description : `Failed to load custom agents for ${ failedTarget . candidate . label ?? "a selected target" } : ${ failedTarget . error } ` ,
249+ } ) ;
250+ return ;
251+ }
252+ const reason = agentDisabledReason ( value ) ;
253+ if ( reason ) {
254+ toastManager . add ( { type : "error" , title : "Default agent not saved" , description : reason } ) ;
255+ return ;
256+ }
206257 }
207258 updateSettings ( { defaultAgentId : value } ) ;
208259 } ;
@@ -278,6 +329,37 @@ export function ProjectDefaultsSettings({ category }: { category: ProjectSetting
278329 }
279330 />
280331 ) ;
332+ const isAgentRosterPending =
333+ ! unavailable &&
334+ ( agentRoster . isPending || targetRosters . some ( ( r ) => r . isPending && r . agents === null ) ) ;
335+ const agentRosterError = ! unavailable
336+ ? ( agentRoster . error ??
337+ targetRosters . find ( ( r ) => r . error !== null && r . agents === null ) ?. error ??
338+ null )
339+ : null ;
340+
341+ const refreshAgentRosters = ( ) => {
342+ agentRoster . refresh ( ) ;
343+ for ( const target of targetRosters ) {
344+ if ( target . queryAtom ) {
345+ appAtomRegistry . refresh ( target . queryAtom ) ;
346+ }
347+ }
348+ } ;
349+
350+ const agentStatus =
351+ unavailable || mixedAgent || agentSource === "project"
352+ ? undefined
353+ : agentRosterError
354+ ? `Failed to load custom agents: ${ agentRosterError } `
355+ : isAgentRosterPending
356+ ? "Loading custom agents…"
357+ : settings . defaultAgentId === null
358+ ? isProjectScope
359+ ? "No custom agent"
360+ : "Automatic"
361+ : undefined ;
362+
281363 const agentRow = (
282364 < SettingsRow
283365 serverScoped
@@ -290,37 +372,37 @@ export function ProjectDefaultsSettings({ category }: { category: ProjectSetting
290372 ? "Default custom agent for new threads in this project."
291373 : "Default custom agent for new threads. Projects can override it."
292374 }
293- status = {
294- unavailable || mixedAgent || agentSource === "project"
295- ? undefined
296- : settings . defaultAgentId === null
297- ? isProjectScope
298- ? "No custom agent"
299- : "Automatic"
300- : undefined
301- }
375+ status = { agentStatus }
302376 resetAction = {
303377 settings . defaultAgentId !== null ? (
304378 < SettingResetButton label = "default agent" onClick = { ( ) => setAgent ( null ) } />
305379 ) : null
306380 }
307381 control = {
308- < AgentPicker
309- agents = { ( agentRoster . data ?? [ ] ) as ReadonlyArray < AgentSummary > }
310- selectedAgentId = { settings . defaultAgentId }
311- onSelectAgent = { ( agentId ) => setAgent ( agentId ?? null ) }
312- allowNone
313- allowDefault = { false }
314- noneLabel = { isProjectScope ? "No custom agent" : "Automatic" }
315- noneDescription = {
316- isProjectScope
317- ? "New threads in this project do not use a custom agent by default."
318- : "New threads do not use a custom agent by default."
319- }
320- getAgentDisabledReason = { agentDisabledReason }
321- disabled = { unavailable }
322- triggerClassName = { SETTINGS_PICKER_TRIGGER_CLASSNAME }
323- />
382+ < div className = "flex items-center justify-end gap-1.5" >
383+ { agentRosterError ? (
384+ < Button size = "xs" variant = "outline" onClick = { refreshAgentRosters } className = "gap-1" >
385+ < RefreshIcon size = "xs" />
386+ Retry
387+ </ Button >
388+ ) : null }
389+ < AgentPicker
390+ agents = { ( agentRoster . data ?? [ ] ) as ReadonlyArray < AgentSummary > }
391+ selectedAgentId = { settings . defaultAgentId }
392+ onSelectAgent = { ( agentId ) => setAgent ( agentId ?? null ) }
393+ allowNone
394+ allowDefault = { false }
395+ noneLabel = { isProjectScope ? "No custom agent" : "Automatic" }
396+ noneDescription = {
397+ isProjectScope
398+ ? "New threads in this project do not use a custom agent by default."
399+ : "New threads do not use a custom agent by default."
400+ }
401+ getAgentDisabledReason = { agentDisabledReason }
402+ disabled = { unavailable || ( isAgentRosterPending && agentRoster . data === null ) }
403+ triggerClassName = { SETTINGS_PICKER_TRIGGER_CLASSNAME }
404+ />
405+ </ div >
324406 }
325407 />
326408 ) ;
0 commit comments