diff --git a/apps/mobile/src/j5/agents/AgentLibrarySettingsScreen.tsx b/apps/mobile/src/j5/agents/AgentLibrarySettingsScreen.tsx index 1dddfa16b40bb..1d6d194a59902 100644 --- a/apps/mobile/src/j5/agents/AgentLibrarySettingsScreen.tsx +++ b/apps/mobile/src/j5/agents/AgentLibrarySettingsScreen.tsx @@ -3,15 +3,12 @@ import { LIBRARY_FOR_OWNERS } from "@t3tools/client-runtime/j5/sharing"; import { useOwnedEnvironmentIds, useOwnerSomewhere } from "../sharing/guestAccess"; import { AgentLibraryToast, type AgentLibraryNotification } from "./AgentLibraryToast"; import { AgentImportConflictModal } from "./AgentImportConflictModal"; -import { - AGENT_PERSONA_OVERRIDE_NOTE, - agentPersonaDuplicateDraft, -} from "@t3tools/client-runtime/j5/agent-personas"; -import { AgentDetailsModal } from "./AgentDetailsModal"; +import { agentPersonaDuplicateDraft } from "@t3tools/client-runtime/j5/agent-personas"; +import { PersonaSheet, type PersonaSheetActionRow } from "./PersonaSheet"; import { AgentFolderPickerModal } from "./AgentFolderPickerModal"; import { useSettingsFormNavigation } from "./SettingsFormScreen"; import { sendWhenReconnected } from "./watchEnvironmentSessions"; -import { personaRowActions } from "./personaRowActions"; +import { personaRowMarker, personaSheetActions } from "./personaSheet"; import { Menu } from "../../components/ui/Menu"; import { SymbolView } from "../../components/ui/AppSymbol"; import { @@ -327,6 +324,83 @@ function OwnerAgentLibrarySettingsScreen() { : presentAgentPersonaCatalog(catalog.data, providers ?? []); const viewedPersona = personas.find((persona) => persona.personaId === viewing); + const sheetActions: ReadonlyArray = + viewedPersona === undefined + ? [] + : personaSheetActions( + { + removed: viewedPersona.removed, + editable: viewedPersona.edit !== null && canOperate, + }, + otherEnvironments.length > 0, + ).flatMap((action): ReadonlyArray => { + const id = viewedPersona.personaId; + switch (action) { + case "edit": + return [ + { + key: action, + label: "Edit", + disabled: locked, + onPress: () => { + if (viewedPersona.edit && effectiveEnvironmentId) + navigation.navigate("SettingsPersonaEdit", { + environmentId: effectiveEnvironmentId, + initial: viewedPersona.edit, + }); + }, + }, + ]; + case "duplicate": + return [ + { + key: action, + label: "Duplicate as personal persona", + disabled: locked || viewedPersona.availability === "unsupported", + onPress: () => void duplicatePersona(id), + }, + ]; + case "export": + return [ + { + key: action, + label: "Export YAML", + disabled: busy, + onPress: () => void exportPersona(id), + }, + ]; + case "copy": + return otherEnvironments.map((environment) => ({ + key: `copy:${environment.environmentId}`, + label: `Copy to ${environment.environmentLabel}`, + disabled: busy, + onPress: () => + void copyPersona(id, { + environmentId: environment.environmentId, + label: environment.environmentLabel, + }), + })); + case "remove": + return [ + { + key: action, + label: "Remove", + tone: "danger" as const, + disabled: locked, + onPress: () => void removePersona(id), + }, + ]; + case "restore": + return [ + { + key: action, + label: "Restore", + disabled: locked, + onPress: () => void restorePersona(id), + }, + ]; + } + }); const folders = librarySources.data?.folders ?? []; const environmentLabel = connectedEnvironments.find( @@ -446,25 +520,8 @@ function OwnerAgentLibrarySettingsScreen() { borderTop={index > 0} locked={locked} busy={busy} - otherEnvironments={otherEnvironments} onToggle={(enabled) => void toggleAgent(persona.personaId, enabled)} - onEdit={ - persona.edit && effectiveEnvironmentId && canOperate - ? () => { - if (persona.edit && effectiveEnvironmentId) - navigation.navigate("SettingsPersonaEdit", { - environmentId: effectiveEnvironmentId, - initial: persona.edit, - }); - } - : null - } - onDetails={() => setViewing(persona.personaId)} - onDuplicate={() => void duplicatePersona(persona.personaId)} - onExport={() => void exportPersona(persona.personaId)} - onCopy={(target) => void copyPersona(persona.personaId, target)} - onRemove={() => void removePersona(persona.personaId)} - onRestore={() => void restorePersona(persona.personaId)} + onOpen={() => setViewing(persona.personaId)} /> )) )} @@ -549,7 +606,7 @@ function OwnerAgentLibrarySettingsScreen() { /> ) : null} {viewedPersona && effectiveEnvironmentId ? ( - setViewing(null)} /> ) : null} @@ -615,47 +673,37 @@ function InfoRow(props: { type PresentedPersona = ReturnType[number]; -/** One persona as a settings row: name and status, its switch, and the rest in a menu. */ +/** One persona as a settings row: its name, one status word, and its switch. A tap opens its sheet. */ function PersonaRow(props: { readonly persona: PresentedPersona; readonly borderTop: boolean; readonly locked: boolean; readonly busy: boolean; - readonly otherEnvironments: ReadonlyArray<{ - readonly environmentId: EnvironmentId; - readonly environmentLabel: string; - }>; readonly onToggle: (enabled: boolean) => void; - readonly onEdit: (() => void) | null; - readonly onDetails: () => void; - readonly onDuplicate: () => void; - readonly onExport: () => void; - readonly onCopy: (target: { environmentId: EnvironmentId; label: string }) => void; - readonly onRemove: () => void; - readonly onRestore: () => void; + readonly onOpen: () => void; }) { const { persona } = props; - const unavailable = !persona.removed && persona.availability !== "available"; - const actions = personaRowActions( - { removed: persona.removed, editable: props.onEdit !== null }, - props.otherEnvironments.length > 0, - ); - const onEdit = actions.includes("edit") ? props.onEdit : null; + const marker = personaRowMarker({ + removed: persona.removed, + blocked: + !persona.removed && persona.availability !== "available" && persona.blockedReasons.length > 0, + overrideLabel: persona.overridesFolder === null ? null : persona.originLabel, + }); return ( {persona.displayName} - {persona.removed ? Removed : null} - {persona.overridesFolder !== null ? ( - - {persona.originLabel}. {AGENT_PERSONA_OVERRIDE_NOTE} - - ) : null} - {unavailable && persona.blockedReasons.join("; ") ? ( - - {persona.blockedReasons.join("; ")} + {marker === null ? null : ( + + {marker.label} - ) : null} + )} {persona.removed ? null : ( )} - { - switch (action) { - case "edit": - return { id: action, title: "Edit", attributes: { disabled: props.locked } }; - case "details": - return { id: action, title: "Details" }; - case "duplicate": - return { - id: action, - title: "Duplicate as personal persona", - attributes: { disabled: props.locked || persona.availability === "unsupported" }, - }; - case "export": - return { id: action, title: "Export YAML", attributes: { disabled: props.busy } }; - case "copy": - return { - id: action, - title: "Copy to environment", - attributes: { disabled: props.busy }, - subactions: props.otherEnvironments.map((environment) => ({ - id: `copy:${environment.environmentId}`, - title: environment.environmentLabel, - })), - }; - case "remove": - return { - id: action, - title: "Remove", - attributes: { destructive: true, disabled: props.locked }, - }; - case "restore": - return { id: action, title: "Restore", attributes: { disabled: props.locked } }; - } - })} - onPressAction={({ nativeEvent }) => { - const action = nativeEvent.event; - if (action === "edit") onEdit?.(); - else if (action === "details") props.onDetails(); - else if (action === "duplicate") props.onDuplicate(); - else if (action === "export") props.onExport(); - else if (action === "remove") props.onRemove(); - else if (action === "restore") props.onRestore(); - else if (action.startsWith("copy:")) { - const target = props.otherEnvironments.find( - (environment) => `copy:${environment.environmentId}` === action, - ); - if (target) - props.onCopy({ environmentId: target.environmentId, label: target.environmentLabel }); - } - }} - > - - - - ); } diff --git a/apps/mobile/src/j5/agents/AgentDetailsModal.tsx b/apps/mobile/src/j5/agents/PersonaSheet.tsx similarity index 66% rename from apps/mobile/src/j5/agents/AgentDetailsModal.tsx rename to apps/mobile/src/j5/agents/PersonaSheet.tsx index f74a4a9b631f8..e7bdf2a8b215b 100644 --- a/apps/mobile/src/j5/agents/AgentDetailsModal.tsx +++ b/apps/mobile/src/j5/agents/PersonaSheet.tsx @@ -4,10 +4,11 @@ import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime" import type { AgentPersonaCatalogRow } from "@t3tools/client-runtime/j5/agent-personas"; import type { AgentPersonaModelEntry, EnvironmentId } from "@t3tools/contracts"; import { useEffect, useState } from "react"; -import { Modal, Pressable, ScrollView, View } from "react-native"; +import { Modal, Platform, Pressable, ScrollView, View } from "react-native"; import { SafeAreaView } from "react-native-safe-area-context"; import { AppText as Text } from "../../components/ui/AppText"; +import { ListActionRow } from "../../components/ui/ListActionRow"; import { ListRowLayout } from "../../components/ui/ListRowLayout"; import { SettingsSection } from "../../features/settings/components/SettingsSection"; import { cn } from "../../lib/cn"; @@ -15,11 +16,24 @@ import { useAtomCommand } from "../../state/use-atom-command"; import { agentPersonaEnvironment } from "./agentPersonaAtoms"; import { personaDetailRows } from "./personaDetails"; -/** A persona the person can't edit, such as one from a folder, shown read-only. */ -export function AgentDetailsModal(props: { +export interface PersonaSheetActionRow { + readonly key: string; + readonly label: string; + readonly tone?: "danger"; + readonly disabled: boolean; + readonly onPress: () => void; +} + +/** + * Everything about one persona and what can be done with it, opened by a tap on its row. + * Actions are plain rows, not a Menu: Android's Menu can't draw over a Modal. Each closes the + * sheet first; on iOS it waits for the dismissal, so a share sheet or modal it opens can present. + */ +export function PersonaSheet(props: { readonly environmentId: EnvironmentId; readonly persona: AgentPersonaCatalogRow; readonly usageLine: string | null; + readonly actions: ReadonlyArray; readonly onClose: () => void; }) { const [models, setModels] = useState< @@ -51,14 +65,28 @@ export function AgentDetailsModal(props: { cancelled = true; }; }, [props.environmentId, personaId, read]); + const [leaving, setLeaving] = useState<(() => void) | null>(null); + const leaveThen = (run: () => void) => { + if (Platform.OS !== "ios") { + props.onClose(); + run(); + return; + } + setLeaving(() => run); + }; const providers = useAtomValue(serverEnvironment.providersValueAtom(props.environmentId)); const rows = personaDetailRows(props.persona, models, props.usageLine, providers ?? []); return ( { + if (leaving === null) return; + props.onClose(); + leaving(); + }} > {/* SafeAreaView takes no className, so the theme surface and flex go on a View. */} @@ -71,7 +99,7 @@ export function AgentDetailsModal(props: { Done - + {rows.map((row, index) => ( ))} + {props.actions.length === 0 ? null : ( + + {props.actions.map((action, index) => ( + 0 && "border-t border-border-subtle")} + > + leaveThen(action.onPress)} + {...(action.tone === undefined ? {} : { tone: action.tone })} + /> + + ))} + + )} diff --git a/apps/mobile/src/j5/agents/personaDetails.test.ts b/apps/mobile/src/j5/agents/personaDetails.test.ts index 3c603fad7ef6f..cabd705da8f59 100644 --- a/apps/mobile/src/j5/agents/personaDetails.test.ts +++ b/apps/mobile/src/j5/agents/personaDetails.test.ts @@ -25,6 +25,17 @@ const rowsOf = (rows: ReturnType) => rows.map(({ label, value }) => [label, value]); describe("personaDetailRows", () => { + it("explains an override, after its source", () => { + const override = { + ...persona, + origin: { kind: "imported" }, + originLabel: "Overrides personas", + overridesFolder: "/srv/personas/luna.yaml", + } as unknown as Parameters[0]; + const labels = personaDetailRows(override, { status: "failed" }, null).map((row) => row.label); + expect(labels.slice(0, 4)).toEqual(["Status", "Source", "Override", "Access"]); + }); + it("shows a folder persona's path, access, both models and description", () => { expect( rowsOf( diff --git a/apps/mobile/src/j5/agents/personaDetails.ts b/apps/mobile/src/j5/agents/personaDetails.ts index 0d3f601d57ed3..f913aceaa2d73 100644 --- a/apps/mobile/src/j5/agents/personaDetails.ts +++ b/apps/mobile/src/j5/agents/personaDetails.ts @@ -1,4 +1,5 @@ import { + AGENT_PERSONA_OVERRIDE_NOTE, agentPersonaEntryLabel, type AgentPersonaCatalogRow, } from "@t3tools/client-runtime/j5/agent-personas"; @@ -11,7 +12,7 @@ export interface PersonaDetailRow { } /** - * What the read-only sheet shows for a persona the person can't edit. Models come from the + * What a persona's sheet shows about it. Models come from the * definition, read on open: `null` while it loads, and the catalog's resolved route if it fails. */ export function personaDetailRows( @@ -24,6 +25,7 @@ export function personaDetailRows( | "availabilityLabel" | "blockedReasons" | "route" + | "overridesFolder" >, models: | { readonly status: "loading" } @@ -45,6 +47,7 @@ export function personaDetailRows( tone: "danger" as const, })), ...(source === null ? [] : [{ label: "Source", value: source }]), + ...(persona.overridesFolder ? [{ label: "Override", value: AGENT_PERSONA_OVERRIDE_NOTE }] : []), { label: "Access", value: persona.authority }, ...(models.status === "loaded" ? (models.models ?? models.route).map((entry, index) => { diff --git a/apps/mobile/src/j5/agents/personaRowActions.test.ts b/apps/mobile/src/j5/agents/personaRowActions.test.ts deleted file mode 100644 index 74ee660ada5e3..0000000000000 --- a/apps/mobile/src/j5/agents/personaRowActions.test.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { describe, expect, it } from "vite-plus/test"; - -import { personaRowActions } from "./personaRowActions"; - -describe("personaRowActions", () => { - it("offers only Restore for a removed persona, even an editable one", () => { - expect(personaRowActions({ removed: true, editable: true }, true)).toEqual(["restore"]); - }); - - it("offers Edit and Details only for an editable persona, and Copy only with another environment", () => { - expect(personaRowActions({ removed: false, editable: true }, true)).toEqual([ - "edit", - "details", - "duplicate", - "export", - "copy", - "remove", - ]); - expect(personaRowActions({ removed: false, editable: false }, false)).toEqual([ - "duplicate", - "export", - "remove", - ]); - }); -}); diff --git a/apps/mobile/src/j5/agents/personaRowActions.ts b/apps/mobile/src/j5/agents/personaRowActions.ts deleted file mode 100644 index ad3c57e29f0f7..0000000000000 --- a/apps/mobile/src/j5/agents/personaRowActions.ts +++ /dev/null @@ -1,26 +0,0 @@ -export type PersonaRowAction = - | "edit" - | "details" - | "duplicate" - | "export" - | "copy" - | "remove" - | "restore"; - -/** - * What a Personas settings row offers, in menu order. A removed persona offers only Restore. - * Details is here only where a tap opens the editor; elsewhere the tap already opens the sheet. - */ -export function personaRowActions( - persona: { readonly removed: boolean; readonly editable: boolean }, - hasOtherEnvironments: boolean, -): ReadonlyArray { - if (persona.removed) return ["restore"]; - return [ - ...(persona.editable ? (["edit", "details"] as const) : []), - "duplicate", - "export", - ...(hasOtherEnvironments ? (["copy"] as const) : []), - "remove", - ]; -} diff --git a/apps/mobile/src/j5/agents/personaSheet.test.ts b/apps/mobile/src/j5/agents/personaSheet.test.ts new file mode 100644 index 0000000000000..2ae3b0160bf67 --- /dev/null +++ b/apps/mobile/src/j5/agents/personaSheet.test.ts @@ -0,0 +1,40 @@ +import { describe, expect, it } from "vite-plus/test"; + +import { personaRowMarker, personaSheetActions } from "./personaSheet"; + +describe("personaSheetActions", () => { + it("offers only Restore for a removed persona, even an editable one", () => { + expect(personaSheetActions({ removed: true, editable: true }, true)).toEqual(["restore"]); + }); + + it("offers Edit only for an editable persona, and Copy only with another environment", () => { + expect(personaSheetActions({ removed: false, editable: true }, true)).toEqual([ + "edit", + "duplicate", + "export", + "copy", + "remove", + ]); + expect(personaSheetActions({ removed: false, editable: false }, false)).toEqual([ + "duplicate", + "export", + "remove", + ]); + }); +}); + +describe("personaRowMarker", () => { + it("names one state, removed first, then blocked, then an override", () => { + const all = { removed: true, blocked: true, overrideLabel: "Overrides personas" }; + expect(personaRowMarker(all)).toEqual({ label: "Removed", tone: "muted" }); + expect(personaRowMarker({ ...all, removed: false })).toEqual({ + label: "Blocked", + tone: "danger", + }); + expect(personaRowMarker({ ...all, removed: false, blocked: false })).toEqual({ + label: "Overrides personas", + tone: "muted", + }); + expect(personaRowMarker({ removed: false, blocked: false, overrideLabel: null })).toBeNull(); + }); +}); diff --git a/apps/mobile/src/j5/agents/personaSheet.ts b/apps/mobile/src/j5/agents/personaSheet.ts new file mode 100644 index 0000000000000..a1c7288a1a55b --- /dev/null +++ b/apps/mobile/src/j5/agents/personaSheet.ts @@ -0,0 +1,28 @@ +export type PersonaSheetAction = "edit" | "duplicate" | "export" | "copy" | "remove" | "restore"; + +/** What a persona's sheet offers, in order. A removed persona offers only Restore. */ +export function personaSheetActions( + persona: { readonly removed: boolean; readonly editable: boolean }, + hasOtherEnvironments: boolean, +): ReadonlyArray { + if (persona.removed) return ["restore"]; + return [ + ...(persona.editable ? (["edit"] as const) : []), + "duplicate", + "export", + ...(hasOtherEnvironments ? (["copy"] as const) : []), + "remove", + ]; +} + +/** The one word a Personas row shows under the name; the sheet holds the detail behind it. */ +export function personaRowMarker(persona: { + readonly removed: boolean; + readonly blocked: boolean; + readonly overrideLabel: string | null; +}): { readonly label: string; readonly tone: "muted" | "danger" } | null { + if (persona.removed) return { label: "Removed", tone: "muted" }; + if (persona.blocked) return { label: "Blocked", tone: "danger" }; + if (persona.overrideLabel !== null) return { label: persona.overrideLabel, tone: "muted" }; + return null; +}