diff --git a/ui/src/components/AgentMultiSelect.tsx b/ui/src/components/AgentMultiSelect.tsx index 5d14afab69d..96da6eb522f 100644 --- a/ui/src/components/AgentMultiSelect.tsx +++ b/ui/src/components/AgentMultiSelect.tsx @@ -1,6 +1,6 @@ import { AgentAvatar, type AvatarAgent } from "@/components/AgentAvatar"; import { useEffect, useMemo, useState, type ComponentProps, type ReactNode } from "react"; -import { ChevronRight } from "lucide-react"; +import { ChevronRight, Plus } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Checkbox } from "@/components/ui/checkbox"; import { Input } from "@/components/ui/input"; @@ -23,6 +23,7 @@ export function AgentSelect({ emptyMessage = "No agents yet.", disabled = false, triggerClassName, + addChip = false, id, }: { agents: AgentMultiSelectOption[]; @@ -32,6 +33,7 @@ export function AgentSelect({ emptyMessage?: string; disabled?: boolean; triggerClassName?: string; + addChip?: boolean; id?: string; }) { const [open, setOpen] = useState(false); @@ -61,16 +63,20 @@ export function AgentSelect({ variant="outline" size="sm" className={cn("w-full justify-between", triggerClassName)} + aria-label={addChip ? "Add agent" : undefined} + title={addChip ? "Add agent" : undefined} disabled={disabled} > + {addChip ? : <> {selectedAgent && } {selectedAgent?.name ?? placeholder} + } - +
{ onChange(agent.id); + setFilter(""); setOpen(false); }} > diff --git a/ui/src/components/agents/AgentIdentityChip.tsx b/ui/src/components/agents/AgentIdentityChip.tsx index 295c8e57dd8..b7fca1af94f 100644 --- a/ui/src/components/agents/AgentIdentityChip.tsx +++ b/ui/src/components/agents/AgentIdentityChip.tsx @@ -1,8 +1,7 @@ -import type { Agent } from "@paperclipai/shared"; -import { AgentAvatar } from "@/components/AgentAvatar"; +import { AgentAvatar, type AvatarAgent } from "@/components/AgentAvatar"; export const agentIdentityChipClass = "agent-identity-chip inline-flex min-w-0 max-w-full items-center gap-1.5 rounded-md px-2 py-1 text-xs"; -export function AgentIdentityChipContent({ agent }: { agent: Agent }) { +export function AgentIdentityChipContent({ agent }: { agent: AvatarAgent & { name: string } }) { return <>{agent.name}; } diff --git a/ui/src/features/connections/ConnectionAccessDefaults.test.tsx b/ui/src/features/connections/ConnectionAccessDefaults.test.tsx new file mode 100644 index 00000000000..4183b99eb5f --- /dev/null +++ b/ui/src/features/connections/ConnectionAccessDefaults.test.tsx @@ -0,0 +1,61 @@ +// @vitest-environment jsdom +import { useState } from "react"; +import { flushSync } from "react-dom"; +import { createRoot } from "react-dom/client"; +import { afterEach, expect, it } from "vitest"; +import { ConnectionAccessDefaults } from "./ConnectionSetupFlow"; + +const agents = ["Alpha", "Beta", "Gamma"].map(name => ({ id: name, name })); +const host = document.createElement("div"); +let root: ReturnType; + +function Access({ disabled = false }) { + const [ids, setIds] = useState(new Set(["Alpha"])); + return {}} + installChoice="specific" setInstallChoice={() => {}} installAgentIds={ids} setInstallAgentIds={setIds} />; +} + +function render(disabled = false) { + document.body.append(host); + root = createRoot(host); + flushSync(() => root.render()); +} + +function button(label: string) { + const result = [...document.querySelectorAll("button")].find(el => + (el.getAttribute("aria-label") ?? el.textContent?.trim()) === label); + if (!result) throw new Error(`Missing button: ${label}`); + return result; +} + +function click(label: string) { + flushSync(() => button(label).click()); +} + +afterEach(() => { flushSync(() => root.unmount()); host.remove(); }); + +it("clears the search after adding and shares chip edits with the expanded picker", () => { + render(); + click("Add agent"); + const search = document.querySelector('input[placeholder="Filter agents"]')!; + flushSync(() => { + Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")!.set!.call(search, "Beta"); + search.dispatchEvent(new Event("input", { bubbles: true })); + }); + click("Select Beta"); + click("Add agent"); + expect(document.querySelector('input[placeholder="Filter agents"]')?.value).toBe(""); + click("Select Gamma"); + click("Remove Alpha"); + click("Change"); + expect(button("2 agents selected")).toBeTruthy(); + click("Remove Beta"); + expect(button("1 agent selected")).toBeTruthy(); +}); + +it("disables adding and hides removal when editing is disabled", () => { + render(true); + expect(host.querySelector('[aria-label="Add agent"]')).toBeNull(); + expect(host.querySelector('[aria-label="Remove Alpha"]')).toBeNull(); +}); diff --git a/ui/src/features/connections/ConnectionSetupFlow.tsx b/ui/src/features/connections/ConnectionSetupFlow.tsx index bca0a9c712d..4e212f7bbb0 100644 --- a/ui/src/features/connections/ConnectionSetupFlow.tsx +++ b/ui/src/features/connections/ConnectionSetupFlow.tsx @@ -46,6 +46,7 @@ import { Search, UserRound, UsersRound, + X, } from "lucide-react"; import type { AppDefinition, @@ -93,9 +94,9 @@ import { toolsApi } from "@/api/tools"; import { agentsApi } from "@/api/agents"; import { appCopyFor, credentialFieldLabel } from "@/lib/app-gallery-copy"; import { JsonSchemaForm, validateJsonSchemaForm, type JsonSchemaNode } from "@/components/JsonSchemaForm"; -import { credentialAccessBadges } from "./credential-access-badges"; -import { Avatar, AvatarFallback, AvatarGroup, AvatarGroupCount } from "@/components/ui/avatar"; -import { AgentMultiSelect, type AgentMultiSelectOption } from "@/components/AgentMultiSelect"; +import { partitionCredentialAgents } from "./credential-access-badges"; +import { AgentIdentityChipContent, agentIdentityChipClass } from "@/components/agents/AgentIdentityChip"; +import { AgentMultiSelect, AgentSelect, type AgentMultiSelectOption } from "@/components/AgentMultiSelect"; import { describeError, errorStatus } from "@/api/errors"; import { InlineBanner } from "@/components/InlineBanner"; import { useQueryView } from "@/components/QueryView"; @@ -4687,10 +4688,11 @@ export function ConnectionAccessDefaults({ const [open, setOpen] = useState(false); const expanded = open || forceOpen; const { installChoice, installAgentIds } = accessProps; - const { visible, remaining } = useMemo( - () => credentialAccessBadges(flat ? agents ?? [] : [], installAgentIds, installChoice === "all"), + const { selected, available } = useMemo( + () => partitionCredentialAgents(flat && installChoice === "specific" ? agents ?? [] : [], installAgentIds), [flat, agents, installAgentIds, installChoice], ); + const editable = !disabled && !accessProps.lockedAgentId && !accessProps.preserveAgentAccess && accessProps.grantKind !== "agent"; const ownerLabel = accessProps.grantKind === "organization" ? "Organization" : "You"; return ( @@ -4698,21 +4700,24 @@ export function ConnectionAccessDefaults({

{sentence}

- {flat && - - - {accessProps.grantKind === "organization" ? : } - - - {visible.map((agent, index) => ( - - {Array.from(agent.name.trim())[0]?.toLocaleUpperCase()} - - ))} - {remaining > 0 && +{remaining}} - } + {flat &&
+ + {accessProps.grantKind === "organization" ? : } + {ownerLabel} + + {installChoice === "all" && Any agent} + {selected.map(agent => + + {editable && } + )} + {editable && installChoice === "specific" && accessProps.setInstallAgentIds(new Set(installAgentIds).add(id))} />} +
}