1+ import { Dialog } from "@base-ui/react/dialog" ;
2+ import { relayOrigin } from "../../features/communities/destination" ;
3+ import { LocalInventoryAction } from "./LocalInventoryAction" ;
14import type { useIdentityNames } from "../../features/identity-names/react" ;
25import {
36 useAgentControl ,
@@ -49,6 +52,7 @@ export function AgentControlPanel({
4952 remove : ( agent : AgentView ) => void ,
5053 importedId : string | null ,
5154 label : ( agent : AgentView ) => string ,
55+ onUseHere : ( pubkey : string ) => void ,
5256 ) => ReactNode ;
5357} ) {
5458 const [ adding , setAdding ] = useState < {
@@ -57,6 +61,17 @@ export function AgentControlPanel({
5761 source ?: AgentView ;
5862 initialSettings ?: CloneSettings ;
5963 } | null > ( null ) ;
64+ const [ localPending , setLocalPending ] = useState ( false ) ;
65+ const [ handover , setHandover ] = useState < {
66+ pubkey : string ;
67+ destination : string ;
68+ } | null > ( null ) ;
69+ useEffect ( ( ) => {
70+ // A handover belongs to the community in which its action was selected.
71+ setHandover ( ( current ) =>
72+ current ?. destination === importDestination ? current : null ,
73+ ) ;
74+ } , [ importDestination ] ) ;
6075 const [ importSections , setImportSections ] = useState < string [ ] > ( [ ] ) ;
6176 const [ importedId , setImportedId ] = useState < string | null > ( null ) ;
6277 const [ selected , setSelected ] = useState < {
@@ -76,7 +91,10 @@ export function AgentControlPanel({
7691 } ) ;
7792 const remove = ( agent : AgentView ) => setDeleting ( agent . id ) ;
7893 useAgentControlRefresh ( control ) ;
79- const state = useAgentControl ( control ) ;
94+ const nativeState = useAgentControl ( control ) ;
95+ const state = localPending
96+ ? { ...nativeState , busy : true , pendingCredentialWrite : true }
97+ : nativeState ;
8098 useEffect ( ( ) => {
8199 if (
82100 state . data ?. agents . some (
@@ -99,6 +117,16 @@ export function AgentControlPanel({
99117 ...facts ,
100118 { pubkey : agent . pubkey , name : agent . name , isAgent : true } ,
101119 ] ) ?? agent . name ;
120+ const localSource =
121+ handover &&
122+ ( state . data ?. agents . find (
123+ ( agent ) =>
124+ agent . pubkey === handover . pubkey &&
125+ ! ! agent . relayUrl &&
126+ ! ! handover . destination &&
127+ relayOrigin ( agent . relayUrl ) === relayOrigin ( handover . destination ) ,
128+ ) ??
129+ state . data ?. agents . find ( ( agent ) => agent . pubkey === handover . pubkey ) ) ;
102130 // Route selection takes precedence over card-local editing. Never guess among
103131 // multiple native records for the same public identity in this community.
104132 const routed =
@@ -141,6 +169,7 @@ export function AgentControlPanel({
141169 < Button
142170 variant = "primary"
143171 aria-haspopup = "dialog"
172+ disabled = { localPending }
144173 onClick = { ( ) =>
145174 setAdding ( {
146175 destination : importDestination ,
@@ -171,7 +200,9 @@ export function AgentControlPanel({
171200 ) }
172201 { state . busy && < p role = "status" > Waiting for the host to confirm…</ p > }
173202 { children ? (
174- children ( state , edit , duplicate , remove , importedId , label )
203+ children ( state , edit , duplicate , remove , importedId , label , ( pubkey ) =>
204+ setHandover ( { pubkey, destination : importDestination } ) ,
205+ )
175206 ) : (
176207 < div className = "agent-grid" >
177208 { state . data ?. agents . map ( ( agent ) => (
@@ -231,6 +262,50 @@ export function AgentControlPanel({
231262 ] }
232263 />
233264 ) }
265+ { state . data && handover && handover . destination === importDestination && (
266+ < Dialog . Root
267+ open
268+ modal = { false }
269+ onOpenChange = { ( open ) => {
270+ if ( ! open && ! state . busy ) setHandover ( null ) ;
271+ } }
272+ >
273+ < Dialog . Portal >
274+ < Dialog . Popup
275+ data-buzz-ui = ""
276+ className = "buzz-dialog agent-controls agent-dialog text-body"
277+ >
278+ < Dialog . Title className = "text-heading" >
279+ Set up agent here
280+ </ Dialog . Title >
281+ < Dialog . Description className = "text-body-sm text-secondary" >
282+ Set up the imported agent in this community. It will not start
283+ yet.
284+ </ Dialog . Description >
285+ { localSource && state . data . localInventoryActions ? (
286+ < LocalInventoryAction
287+ key = { `${ handover . pubkey } :${ handover . destination } :${ createOwner } ` }
288+ control = { control }
289+ agent = { localSource || undefined }
290+ destination = { handover . destination }
291+ owner = { createOwner ?? "" }
292+ disabled = { nativeState . busy || state . status !== "ready" }
293+ onPending = { setLocalPending }
294+ onUsed = { ( ) => setHandover ( null ) }
295+ />
296+ ) : (
297+ < p >
298+ This app cannot set up this agent yet. Import it first. If it
299+ is already imported, update and restart the desktop app.
300+ </ p >
301+ ) }
302+ < Button disabled = { state . busy } onClick = { ( ) => setHandover ( null ) } >
303+ Close
304+ </ Button >
305+ </ Dialog . Popup >
306+ </ Dialog . Portal >
307+ </ Dialog . Root >
308+ ) }
234309 { adding && (
235310 < AgentCreateDialog
236311 control = { control }
0 commit comments