Skip to content

Commit d741465

Browse files
loganjLarryklopez4212
authored
Set up incomplete inventory identities through a working Use here dialog (#287)
Signed-off-by: Larry <627498bd4bd1f281a16431e3c6cce3b5c25b6692798c78672298aefbf2f8f8b5@buzz.block.builderlab.xyz> Signed-off-by: klopez4212 <[email protected]> Co-authored-by: Larry <627498bd4bd1f281a16431e3c6cce3b5c25b6692798c78672298aefbf2f8f8b5@buzz.block.builderlab.xyz> Co-authored-by: klopez4212 <[email protected]>
1 parent 0a49827 commit d741465

15 files changed

Lines changed: 723 additions & 19 deletions
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// @vitest-environment jsdom
2+
import { stubAvatarBrowserApis } from "../../features/agents/avatar-testing";
3+
stubAvatarBrowserApis();
4+
import "@testing-library/jest-dom/vitest";
5+
import { afterEach, expect, it, vi } from "vitest";
6+
import {
7+
cleanup,
8+
fireEvent,
9+
render,
10+
screen,
11+
waitFor,
12+
within,
13+
} from "@testing-library/react";
14+
import * as communityApi from "../../features/communities/api";
15+
import { createAgentControl } from "../../features/agents/control";
16+
import { controlFixture } from "../../features/agents/control-testing";
17+
import { AgentControlPanel } from "./AgentControlPanel";
18+
19+
const disposals: (() => void)[] = [];
20+
afterEach(() => {
21+
cleanup();
22+
for (const dispose of disposals.splice(0)) dispose();
23+
vi.restoreAllMocks();
24+
});
25+
26+
// Exercise the control panel callbacks before the replacement inventory mounts.
27+
function setup(
28+
_mode: string,
29+
configure: (f: ReturnType<typeof controlFixture>) => void,
30+
) {
31+
const f = controlFixture();
32+
configure(f);
33+
const control = createAgentControl(f.host);
34+
disposals.push(() => control.dispose());
35+
render(
36+
<AgentControlPanel
37+
control={control}
38+
importDestination="https://relay.example.test"
39+
createOwner={"de".repeat(32)}
40+
>
41+
{(state, _edit, _duplicate, _remove, _importedId, _label, onUseHere) =>
42+
state.status === "ready" && (
43+
<>
44+
<article aria-label="Agent Fixture agent">
45+
<button type="button" onClick={() => onUseHere("ab".repeat(32))}>
46+
Use here
47+
</button>
48+
</article>
49+
</>
50+
)
51+
}
52+
</AgentControlPanel>,
53+
);
54+
return { f };
55+
}
56+
57+
it("dialog actions choose the destination record when several local setups share a key", async () => {
58+
const request = vi
59+
.spyOn(communityApi, "communityRequest")
60+
.mockImplementation(async (_destination, route) =>
61+
route === "agent-inventory"
62+
? { identities: [] }
63+
: {
64+
pubkey: "ab".repeat(32),
65+
relayUrl: "wss://relay.example.test",
66+
owner: "de".repeat(32),
67+
signature: "fixture",
68+
},
69+
);
70+
const { f } = setup("connected", (fixture) => {
71+
fixture.data.parked = [];
72+
fixture.agent.configured = false;
73+
fixture.agent.enabled = false;
74+
fixture.agent.status = "stopped";
75+
fixture.data.agents.unshift({
76+
...structuredClone(fixture.agent),
77+
id: "first-other-community",
78+
relayUrl: "wss://elsewhere.example",
79+
name: "Other saved name",
80+
systemPrompt: "Other instructions",
81+
});
82+
});
83+
const card = await screen.findByRole("article", {
84+
name: "Agent Fixture agent",
85+
});
86+
fireEvent.click(within(card).getByRole("button", { name: "Use here" }));
87+
const setupDialog = await screen.findByRole("dialog", {
88+
name: "Set up agent here",
89+
});
90+
fireEvent.click(
91+
within(setupDialog).getByRole("button", { name: "Use here" }),
92+
);
93+
await waitFor(() =>
94+
expect(
95+
screen.queryByRole("dialog", { name: "Set up agent here" }),
96+
).toBeNull(),
97+
);
98+
expect(
99+
f.calls.find((call) => call.action === "configure")?.payload,
100+
).toMatchObject({ id: "fixture-agent" });
101+
expect(request).toHaveBeenCalledWith(
102+
"https://relay.example.test",
103+
"resolve-agent-community",
104+
{ pubkey: f.agent.pubkey, owner: "de".repeat(32), confirmed: true },
105+
);
106+
expect(
107+
f.data.agents.find((agent) => agent.id === "first-other-community")
108+
?.configured,
109+
).toBe(false);
110+
expect(f.agent.enabled).toBe(false);
111+
});

‎src/bundled/agents/AgentControlPanel.tsx‎

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { Dialog } from "@base-ui/react/dialog";
2+
import { relayOrigin } from "../../features/communities/destination";
3+
import { LocalInventoryAction } from "./LocalInventoryAction";
14
import type { useIdentityNames } from "../../features/identity-names/react";
25
import {
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}

‎src/bundled/agents/AgentsPage.test.tsx‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2408,9 +2408,11 @@ it("Use here retries owner confirmation and keeps setup stopped until a separate
24082408
if (!card) throw Error("Imported card missing");
24092409
expect(within(card).getByRole("button", { name: "Start" })).toBeDisabled();
24102410
fireEvent.click(within(card).getByRole("button", { name: "Use here" }));
2411-
await within(card).findByText("Confirmation unavailable");
2411+
const dialog = screen.getByRole("dialog", { name: "Set up agent here" });
2412+
fireEvent.click(within(dialog).getByRole("button", { name: "Use here" }));
2413+
await within(dialog).findByText("Confirmation unavailable");
24122414
expect(f.calls.some((call) => call.action === "configure")).toBe(false);
2413-
fireEvent.click(within(card).getByRole("button", { name: "Use here" }));
2415+
fireEvent.click(within(dialog).getByRole("button", { name: "Use here" }));
24142416
await waitFor(() =>
24152417
expect(within(card).getByRole("button", { name: "Start" })).toBeEnabled(),
24162418
);

‎src/bundled/agents/AgentsPage.tsx‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,15 @@ export function AgentsPage({
140140
: undefined
141141
}
142142
>
143-
{(state, edit, duplicate, remove, importedId, label) =>
143+
{(
144+
state,
145+
edit,
146+
duplicate,
147+
remove,
148+
importedId,
149+
label,
150+
onUseHere,
151+
) =>
144152
state.status === "unavailable" ? (
145153
library
146154
) : state.data?.parked !== undefined ? (
@@ -153,9 +161,11 @@ export function AgentsPage({
153161
importedId={importedId}
154162
control={control}
155163
connection={connection}
164+
onUseHere={onUseHere}
156165
/>
157166
) : (
158167
<ManagedAgents
168+
onUseHere={onUseHere}
159169
key={`${connection.scope}:${connection.generation}`}
160170
state={state}
161171
label={label}
@@ -198,6 +208,7 @@ function ManagedAgents({
198208
label,
199209
destination,
200210
headerActions,
211+
onUseHere,
201212
}: {
202213
label(agent: AgentView): string;
203214
state: AgentControlState;
@@ -209,6 +220,7 @@ function ManagedAgents({
209220
connection: RelaySnapshot;
210221
destination: string;
211222
headerActions: HTMLElement | null;
223+
onUseHere(pubkey: string): void;
212224
}) {
213225
const library = connection.session.agentLibrary;
214226
const snapshot = useSyncExternalStore(
@@ -251,6 +263,7 @@ function ManagedAgents({
251263
>
252264
<ManagedAgentActions
253265
agent={agent}
266+
onUseHere={onUseHere}
254267
state={state}
255268
control={control}
256269
imported={agent.id === importedId}

0 commit comments

Comments
 (0)