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
88 changes: 88 additions & 0 deletions apps/mobile/src/j5/agents/AgentDetailsModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
import type { AgentPersonaCatalogRow } from "@t3tools/client-runtime/j5/agent-personas";
import type { AgentPersonaModelTarget, EnvironmentId } from "@t3tools/contracts";
import { useEffect, useState } from "react";
import { Modal, Pressable, ScrollView, View } from "react-native";
import { SafeAreaView } from "react-native-safe-area-context";

import { AppText as Text } from "../../components/ui/AppText";
import { ListRowLayout } from "../../components/ui/ListRowLayout";
import { SettingsSection } from "../../features/settings/components/SettingsSection";
import { cn } from "../../lib/cn";
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: {
readonly environmentId: EnvironmentId;
readonly persona: AgentPersonaCatalogRow;
readonly usageLine: string | null;
readonly onClose: () => void;
}) {
const [models, setModels] = useState<
| { readonly status: "loading" }
| { readonly status: "failed" }
| {
readonly status: "loaded";
readonly route: readonly [AgentPersonaModelTarget, AgentPersonaModelTarget];
}
>({ status: "loading" });
const read = useAtomCommand(agentPersonaEnvironment.readAgentPersona, { reportFailure: false });
const personaId = props.persona.personaId;
// oxlint-disable-next-line t3code/react-effect-budget -- loads the persona's models once when the sheet opens; the read is a command, not an atom.
useEffect(() => {
let cancelled = false;
void read({ environmentId: props.environmentId, input: { personaId } })
.then((result) => {
if (cancelled) return;
if (result._tag === "Failure") throw squashAtomCommandFailure(result);
setModels({ status: "loaded", route: result.value.definition.modelRoute });
})
.catch(() => {
if (!cancelled) setModels({ status: "failed" });
});
return () => {
cancelled = true;
};
}, [props.environmentId, personaId, read]);
const rows = personaDetailRows(props.persona, models, props.usageLine);
return (
<Modal
visible
animationType="slide"
presentationStyle="pageSheet"
onRequestClose={props.onClose}
>
{/* SafeAreaView takes no className, so the theme surface and flex go on a View. */}
<View className="flex-1 bg-sheet">
<SafeAreaView style={{ flex: 1 }}>
<View className="flex-row items-center justify-between gap-3 px-5 py-3">
<Text className="min-w-0 shrink text-xl font-t3-semibold" numberOfLines={1}>
{props.persona.displayName}
</Text>
<Pressable accessibilityRole="button" onPress={props.onClose} className="px-3 py-2">
<Text>Done</Text>
</Pressable>
</View>
<ScrollView contentContainerClassName="px-5 pb-6">
<SettingsSection>
{rows.map((row, index) => (
<View
key={`${row.label}:${row.value}`}
className={cn(index > 0 && "border-t border-border-subtle")}
>
<ListRowLayout
label={row.label}
description={row.value}
{...(row.tone === undefined ? {} : { tone: row.tone })}
/>
</View>
))}
</SettingsSection>
</ScrollView>
</SafeAreaView>
</View>
</Modal>
);
}
103 changes: 48 additions & 55 deletions apps/mobile/src/j5/agents/AgentLibrarySettingsScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
agentPersonaDuplicateDraft,
type AgentPersonaCreateDraft,
} from "@t3tools/client-runtime/j5/agent-personas";
import { AgentDetailsModal } from "./AgentDetailsModal";
import { AgentEditorModal } from "./AgentEditorModal";
import { AgentFolderPickerModal } from "./AgentFolderPickerModal";
import { sendWhenReconnected } from "./watchEnvironmentSessions";
Expand Down Expand Up @@ -141,6 +142,7 @@ function OwnerAgentLibrarySettingsScreen() {
} | null>(null);
const [notification, setNotification] = useState<AgentLibraryNotification | null>(null);
const [creating, setCreating] = useState<{ initial?: AgentPersonaCreateDraft } | null>(null);
const [viewing, setViewing] = useState<string | null>(null);
const dismissNotification = useCallback(() => setNotification(null), []);
const importAgents = useAtomCommand(agentPersonaEnvironment.importAgentPersonas, {
reportFailure: false,
Expand Down Expand Up @@ -314,6 +316,7 @@ function OwnerAgentLibrarySettingsScreen() {
? []
: presentAgentPersonaCatalog(catalog.data);

const viewedPersona = personas.find((persona) => persona.personaId === viewing);
const folders = librarySources.data?.folders ?? [];
const environmentLabel =
connectedEnvironments.find(
Expand Down Expand Up @@ -427,17 +430,13 @@ function OwnerAgentLibrarySettingsScreen() {
<PersonaRow
key={persona.personaId}
persona={persona}
usageLine={(() => {
const entry = usageById.get(persona.personaId);
return entry === undefined ? null : presentAgentPersonaUsage(entry).line;
})()}
borderTop={index > 0}
locked={locked}
busy={busy}
otherEnvironments={otherEnvironments}
onToggle={(enabled) => void toggleAgent(persona.personaId, enabled)}
onEdit={
persona.edit && effectiveEnvironmentId
persona.edit && effectiveEnvironmentId && canOperate
? () => {
if (persona.edit && effectiveEnvironmentId)
setEditing({
Expand All @@ -447,6 +446,7 @@ function OwnerAgentLibrarySettingsScreen() {
}
: null
}
onDetails={() => setViewing(persona.personaId)}
onDuplicate={() => void duplicatePersona(persona.personaId)}
onExport={() => void exportPersona(persona.personaId)}
onCopy={(target) => void copyPersona(persona.personaId, target)}
Expand Down Expand Up @@ -547,6 +547,18 @@ function OwnerAgentLibrarySettingsScreen() {
}}
/>
) : null}
{viewedPersona && effectiveEnvironmentId ? (
<AgentDetailsModal
key={`${effectiveEnvironmentId}:${viewedPersona.personaId}`}
environmentId={effectiveEnvironmentId}
persona={viewedPersona}
usageLine={(() => {
const entry = usageById.get(viewedPersona.personaId);
return entry === undefined ? null : presentAgentPersonaUsage(entry).line;
})()}
onClose={() => setViewing(null)}
/>
) : null}
{editing ? (
<AgentEditorModal
key={`${editing.environmentId}:${editing.initial.personaId}`}
Expand Down Expand Up @@ -619,7 +631,6 @@ type PresentedPersona = ReturnType<typeof presentAgentPersonaCatalog>[number];
/** One persona as a settings row: name and status, its switch, and the rest in a menu. */
function PersonaRow(props: {
readonly persona: PresentedPersona;
readonly usageLine: string | null;
readonly borderTop: boolean;
readonly locked: boolean;
readonly busy: boolean;
Expand All @@ -629,78 +640,57 @@ function PersonaRow(props: {
}>;
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;
}) {
const { persona } = props;
const status = [
persona.removed ? "Removed" : persona.availabilityLabel,
persona.originLabel,
persona.unsandboxed ? "Full access" : null,
props.usageLine,
]
.filter(Boolean)
.join(" · ");
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 content = (
<>
<Text
textRole="body"
className={
persona.enabled && !persona.removed ? "text-foreground" : "text-foreground-muted"
}
numberOfLines={1}
>
{persona.displayName}
</Text>
<Text className="text-sm text-foreground-muted" numberOfLines={1}>
{status}
</Text>
{unavailable && persona.blockedReasons[0] ? (
<Text className="text-sm text-danger-foreground" numberOfLines={2}>
{persona.blockedReasons[0]}
</Text>
) : null}
</>
);
return (
<View
className={cn(
"flex-row items-center gap-2 py-2 pl-4 pr-1",
props.borderTop && "border-t border-border-subtle",
)}
>
{onEdit ? (
<Pressable
accessibilityRole="button"
accessibilityLabel={`Edit ${persona.displayName}`}
accessibilityHint={persona.description}
disabled={props.locked}
onPress={onEdit}
className="min-w-0 flex-1 gap-0.5 py-1 active:opacity-70"
>
{content}
</Pressable>
) : (
<View
accessible
accessibilityLabel={`${persona.displayName}, ${status}`}
accessibilityHint={persona.description}
className="min-w-0 flex-1 gap-0.5 py-1"
<Pressable
accessibilityRole="button"
accessibilityLabel={
onEdit ? `Edit ${persona.displayName}` : `Details for ${persona.displayName}`
}
accessibilityHint={persona.description}
disabled={props.busy}
onPress={onEdit ?? props.onDetails}
className="min-w-0 flex-1 gap-0.5 py-1 active:opacity-70"
>
<Text
textRole="body"
className={
persona.enabled && !persona.removed ? "text-foreground" : "text-foreground-muted"
}
numberOfLines={1}
>
{content}
</View>
)}
{persona.displayName}
</Text>
{persona.removed ? <Text className="text-sm text-foreground-muted">Removed</Text> : null}
{unavailable && persona.blockedReasons[0] ? (
<Text className="text-sm text-danger-foreground" numberOfLines={2}>
{persona.blockedReasons[0]}
</Text>
) : null}
</Pressable>
{persona.removed ? null : (
<ThemedSwitch
// React Native's Switch sets alignSelf: flex-start, which ignores the row's centring.
style={{ alignSelf: "center" }}
value={persona.enabled}
disabled={props.locked}
accessibilityLabel={`Enable ${persona.displayName}`}
Expand All @@ -712,6 +702,8 @@ function PersonaRow(props: {
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,
Expand Down Expand Up @@ -743,6 +735,7 @@ function PersonaRow(props: {
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();
Expand Down
67 changes: 67 additions & 0 deletions apps/mobile/src/j5/agents/personaDetails.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import type { AgentPersonaModelTarget } from "@t3tools/contracts";
import { describe, expect, it } from "vite-plus/test";

import { personaDetailRows } from "./personaDetails";

const persona = {
origin: { kind: "folder", path: "/srv/personas/luna.yaml" },
originLabel: "Folder · personas",
authority: "Read only (default), Full access",
description: "Reviews changes.",
availabilityLabel: "Available",
blockedReasons: [],
route: "Codex · gpt-6-luna · primary",
} as unknown as Parameters<typeof personaDetailRows>[0];

const target = (driver: string, model: string, reasoningEffort: string) =>
({ driver, model, reasoningEffort }) as unknown as AgentPersonaModelTarget;

const rowsOf = (rows: ReturnType<typeof personaDetailRows>) =>
rows.map(({ label, value }) => [label, value]);

describe("personaDetailRows", () => {
it("shows a folder persona's path, access, both models and description", () => {
expect(
rowsOf(
personaDetailRows(
persona,
{
status: "loaded",
route: [target("codex", "gpt-6-luna", "low"), target("claudeAgent", "haiku", "low")],
},
null,
),
),
).toEqual([
["Status", "Available"],
["Source", "/srv/personas/luna.yaml"],
["Access", "Read only (default), Full access"],
["Primary model", "Codex · gpt-6-luna · low"],
["Fallback model", "Claude · haiku · low"],
["Description", "Reviews changes."],
]);
});

it("falls back to the catalog's route when the definition can't be read", () => {
const rows = rowsOf(personaDetailRows(persona, { status: "failed" }, null));
expect(rows).toContainEqual(["Model", "Codex · gpt-6-luna · primary"]);
expect(rows.map(([label]) => label)).not.toContain("Primary model");
});

it("lists every blocked reason in danger text, and usage when there is any", () => {
const rows = personaDetailRows(
{
...persona,
availabilityLabel: "Blocked",
blockedReasons: ["Primary is off", "Fallback is off"],
},
{ status: "loading" },
"3 tasks",
);
expect(rows.filter(({ tone }) => tone === "danger").map(({ value }) => value)).toEqual([
"Primary is off",
"Fallback is off",
]);
expect(rows.at(-1)).toEqual({ label: "Usage", value: "3 tasks" });
});
});
Loading
Loading