Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
227 changes: 105 additions & 122 deletions apps/mobile/src/j5/agents/AgentLibrarySettingsScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -327,6 +324,83 @@ function OwnerAgentLibrarySettingsScreen() {
: presentAgentPersonaCatalog(catalog.data, providers ?? []);

const viewedPersona = personas.find((persona) => persona.personaId === viewing);
const sheetActions: ReadonlyArray<PersonaSheetActionRow> =
viewedPersona === undefined
? []
: personaSheetActions(
{
removed: viewedPersona.removed,
editable: viewedPersona.edit !== null && canOperate,
},
otherEnvironments.length > 0,
).flatMap((action): ReadonlyArray<PersonaSheetActionRow> => {
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(
Expand Down Expand Up @@ -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)}
/>
))
)}
Expand Down Expand Up @@ -549,14 +606,15 @@ function OwnerAgentLibrarySettingsScreen() {
/>
) : null}
{viewedPersona && effectiveEnvironmentId ? (
<AgentDetailsModal
<PersonaSheet
key={`${effectiveEnvironmentId}:${viewedPersona.personaId}`}
environmentId={effectiveEnvironmentId}
persona={viewedPersona}
usageLine={(() => {
const entry = usageById.get(viewedPersona.personaId);
return entry === undefined ? null : presentAgentPersonaUsage(entry).line;
})()}
actions={sheetActions}
onClose={() => setViewing(null)}
/>
) : null}
Expand Down Expand Up @@ -615,47 +673,37 @@ function InfoRow(props: {

type PresentedPersona = ReturnType<typeof presentAgentPersonaCatalog>[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 (
<View
className={cn(
"flex-row items-center gap-2 py-2 pl-4 pr-1",
"flex-row items-center gap-2 py-2 pl-4 pr-4",
props.borderTop && "border-t border-border-subtle",
)}
>
<Pressable
accessibilityRole="button"
accessibilityLabel={
onEdit ? `Edit ${persona.displayName}` : `Details for ${persona.displayName}`
marker === null ? persona.displayName : `${persona.displayName}, ${marker.label}`
}
accessibilityHint={persona.description}
accessibilityHint="Opens its details and actions"
disabled={props.busy}
onPress={onEdit ?? props.onDetails}
onPress={props.onOpen}
className="min-w-0 flex-1 gap-0.5 py-1 active:opacity-70"
>
<Text
Expand All @@ -667,17 +715,17 @@ function PersonaRow(props: {
>
{persona.displayName}
</Text>
{persona.removed ? <Text className="text-sm text-foreground-muted">Removed</Text> : null}
{persona.overridesFolder !== null ? (
<Text className="text-sm text-foreground-muted">
{persona.originLabel}. {AGENT_PERSONA_OVERRIDE_NOTE}
</Text>
) : null}
{unavailable && persona.blockedReasons.join("; ") ? (
<Text className="text-sm text-danger-foreground">
{persona.blockedReasons.join("; ")}
{marker === null ? null : (
<Text
className={cn(
"text-sm",
marker.tone === "danger" ? "text-danger-foreground" : "text-foreground-muted",
)}
numberOfLines={1}
>
{marker.label}
</Text>
) : null}
)}
</Pressable>
{persona.removed ? null : (
<ThemedSwitch
Expand All @@ -689,71 +737,6 @@ function PersonaRow(props: {
onValueChange={props.onToggle}
/>
)}
<Menu
actions={actions.map((action) => {
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 });
}
}}
>
<Pressable
accessibilityRole="button"
accessibilityLabel={`More actions for ${persona.displayName}`}
className="size-11 items-center justify-center"
>
<SymbolView
name="ellipsis"
size={18}
tintColorClassName="accent-icon"
type="monochrome"
/>
</Pressable>
</Menu>
</View>
);
}
Expand Down
Loading
Loading