import React, { memo, useEffect, useMemo, useRef, useState } from 'react'
import {
Animated,
Easing,
Modal,
Platform,
Pressable,
ScrollView,
StyleSheet,
Text,
useWindowDimensions,
View,
} from 'react-native'
import Svg, { Circle, Path } from 'react-native-svg'
import { ChevronDown, ChevronRight, ChevronUp, Layers, Users, X } from 'lucide-react-native'
import { strings } from '../../locales/i18n'
import type { RemoteSession, TeamMemberSnapshot, TeamTaskSnapshot } from '../../types'
import * as theme from '../../ui/theme'
import type { ThemeColors } from '../../ui/theme'
import { useTheme } from '../../ui/theme-context'
import { useThemedStyles } from '../../ui/use-themed-styles'
const { radius, spacing } = theme
const typography = theme.type
import { resolveSessionDisplayTitle } from '../session-title'
import { createStyles as createLegacyStyles } from './styles'
function SimpleModalSurface({ onClose, colors, children }: { onClose: () => void; colors: ThemeColors; children: React.ReactNode }) {
return (
{children}
)
}
/** Split a team member label into role and task ("你是X。任务:Y" / "You are X. Task: Y"). */
export function parseAgentLabel(raw: string | undefined): { role?: string; task: string } {
const text = (raw ?? '').replace(/\s+/gu, ' ').trim()
const zh = /^你是(.+?)(?:。|,|,|:)(?:你的任务|任务)[::]\s*(.+)$/u.exec(text)
if (zh?.[1] !== undefined && zh[2] !== undefined) return { role: zh[1].trim(), task: zh[2].trim() }
const en = /^You are (?:an?|the)\s+(.+?)(?:\.|。)\s*(?:Your task|Task)[::]\s*(.+)$/iu.exec(text)
if (en?.[1] !== undefined && en[2] !== undefined) return { role: en[1].trim(), task: en[2].trim() }
return { task: text }
}
/** Pure derivation of subagent statistics */
export function formatSubagentStats(sessions: readonly RemoteSession[]): {
runningCount: number
totalCount: number
statsText: string
} {
const runningCount = sessions.filter(s => s.running === true).length
const totalCount = sessions.length
return {
runningCount,
totalCount,
statsText: runningCount > 0 ? `${runningCount}/${totalCount}` : `${totalCount}`,
}
}
/** Pure derivation of agent-team statistics */
export function formatTeamStats(
members: readonly TeamMemberSnapshot[],
sessions: readonly RemoteSession[] = [],
): {
activeCount: number
totalCount: number
statsText: string
} {
const activeCount = members.filter(member => {
if (member.phase === 'provisioning') return true
const session = sessions.find(s => s.sessionId === member.id)
return session?.running === true
}).length
const totalCount = members.length
return {
activeCount,
totalCount,
statsText: activeCount > 0 ? `${activeCount}/${totalCount}` : `${totalCount}`,
}
}
export type AgentGlyphStatus = 'running' | 'provisioning' | 'failed' | 'completed' | 'idle' | 'pending'
/**
* Status glyph matching TaskStatusGlyph / JobStatusGlyph visual semantics:
* - running: blue animated rotating spinner
* - provisioning: orange animated rotating pulse spinner
* - failed: red circular badge with exclamation / cross
* - completed: green circular badge with checkmark
* - idle / pending: muted circular outline
*/
export const AgentStatusGlyph = memo(function AgentStatusGlyph({
status,
colors,
size = 16,
testID,
}: {
status: AgentGlyphStatus | string
colors: ThemeColors
size?: number
testID?: string
}) {
const spinAnim = useRef(new Animated.Value(0)).current
useEffect(() => {
if (status !== 'running' && status !== 'provisioning') return
const animation = Animated.loop(
Animated.timing(spinAnim, {
toValue: 1,
duration: status === 'provisioning' ? 2000 : 1500,
easing: Easing.linear,
useNativeDriver: Platform.OS !== 'web',
isInteraction: false,
}),
)
animation.start()
return () => animation.stop()
}, [status, spinAnim])
if (status === 'completed') {
return (
)
}
if (status === 'running') {
const spin = spinAnim.interpolate({
inputRange: [0, 1],
outputRange: ['0deg', '360deg'],
})
return (
)
}
if (status === 'provisioning') {
const spin = spinAnim.interpolate({
inputRange: [0, 1],
outputRange: ['0deg', '360deg'],
})
return (
)
}
if (status === 'failed') {
return (
)
}
// idle or pending: static circle
return (
)
})
export interface SubagentsCardProps {
sessions: readonly RemoteSession[]
expanded?: boolean
onToggle?: () => void
onClose?: () => void
defaultExpanded?: boolean
maxHeight?: number
onOpen?: (session: RemoteSession) => void
testID?: string
}
/**
* Collapsible Subagents Card matching TaskProgressChecklist / BackgroundJobsCard style:
* - Card container with border, rounded corners and card background
* - Collapsible header (minHeight: 44) with Layers icon, title, running/total badge and chevron
* - Expandable scroll view listing child sessions with glyphs, role/task parse and navigation chevron
*/
export const SubagentsCard = memo(function SubagentsCard({
sessions,
expanded,
onToggle,
onClose,
defaultExpanded = false,
maxHeight,
onOpen,
testID = 'subagents-card',
}: SubagentsCardProps) {
const { colors } = useTheme()
const styles = useThemedStyles(createCardStyles)
const copy = strings.sessionActivity
const { height } = useWindowDimensions()
const [internalOpen, setInternalOpen] = useState(defaultExpanded)
const isControlled = expanded !== undefined
const isOpen = isControlled ? expanded : internalOpen
const handleToggle = () => {
if (!isControlled) {
setInternalOpen(prev => !prev)
}
onToggle?.()
}
if (!sessions || sessions.length === 0) return null
const { totalCount, statsText } = useMemo(() => formatSubagentStats(sessions), [sessions])
const listMaxHeight = maxHeight ?? Math.min(260, height * 0.32)
const title = copy.subagents || strings.chat.subagentsTitle(totalCount).split(' · ')[0]
return (
[styles.header, pressed && styles.pressed]}
testID={`${testID}-header`}
>
{title}
{statsText}
{onClose ? (
) : isOpen ? (
) : (
)}
{isOpen && (
{sessions.map((item, index) => {
const parsed = parseAgentLabel(resolveSessionDisplayTitle(item) ?? item.title)
const role = (parsed.role && parsed.role.trim().length > 0) ? parsed.role.trim() : (item.title && item.title.trim().length > 0) ? item.title.trim() : strings.sessions.child
const isRunning = item.running === true
const glyphStatus = isRunning ? 'running' : 'idle'
const stateLabel = isRunning ? strings.status.running : strings.sessions.child
return (
onOpen?.(item)}
style={({ pressed }) => [styles.row, pressed && styles.rowPressed]}
testID={`${testID}-item-${index}`}
>
{role}
{stateLabel}
{parsed.task.length > 0 && (
{parsed.task}
)}
)
})}
)}
)
})
export interface AgentTeamCardProps {
members: readonly TeamMemberSnapshot[]
tasks?: readonly TeamTaskSnapshot[]
sessions?: readonly RemoteSession[]
expanded?: boolean
onToggle?: () => void
onClose?: () => void
defaultExpanded?: boolean
maxHeight?: number
onOpen?: (session: RemoteSession) => void
testID?: string
}
/**
* Collapsible Agent Team Card matching TaskProgressChecklist / BackgroundJobsCard style:
* - Card container with border, rounded corners and card background
* - Collapsible header (minHeight: 44) with Users icon, title, active/total badge and chevron
* - Expandable scroll view listing members with glyphs, claimed tasks, error details and navigation chevron
*/
export const AgentTeamCard = memo(function AgentTeamCard({
members,
tasks = [],
sessions = [],
expanded,
onToggle,
onClose,
defaultExpanded = false,
maxHeight,
onOpen,
testID = 'agent-team-card',
}: AgentTeamCardProps) {
const { colors } = useTheme()
const styles = useThemedStyles(createCardStyles)
const copy = strings.sessionActivity
const { height } = useWindowDimensions()
const [internalOpen, setInternalOpen] = useState(defaultExpanded)
const isControlled = expanded !== undefined
const isOpen = isControlled ? expanded : internalOpen
const handleToggle = () => {
if (!isControlled) {
setInternalOpen(prev => !prev)
}
onToggle?.()
}
if (!members || members.length === 0) return null
const { totalCount, statsText } = useMemo(
() => formatTeamStats(members, sessions),
[members, sessions],
)
const listMaxHeight = maxHeight ?? Math.min(260, height * 0.32)
const title = copy.team || strings.chat.teamTitle(totalCount).split(' · ')[0]
const taskStatusLabel = (status: TeamTaskSnapshot['status']) =>
status === 'completed'
? strings.chat.teamTaskCompleted
: status === 'in_progress'
? strings.chat.teamTaskInProgress
: strings.chat.teamTaskPending
return (
[styles.header, pressed && styles.pressed]}
testID={`${testID}-header`}
>
{title}
{statsText}
{onClose ? (
) : isOpen ? (
) : (
)}
{isOpen && (
{members.map((member, index) => {
const session = sessions.find(item => item.sessionId === member.id)
const owned = tasks.filter(task => task.ownerId === member.id && task.status !== 'deleted')
const active = owned.find(task => task.status === 'in_progress')
?? owned.find(task => task.status === 'pending')
?? owned[0]
const liveRunning = session?.running === true
let glyphStatus: AgentGlyphStatus = 'idle'
let stateLabel = strings.chat.teamIdle
let stateStyle = styles.statusIdle
if (member.phase === 'failed') {
glyphStatus = 'failed'
stateLabel = strings.chat.teamPhaseFailed
stateStyle = styles.statusFailed
} else if (member.phase === 'provisioning') {
glyphStatus = 'provisioning'
stateLabel = strings.chat.teamPhaseProvisioning
stateStyle = styles.statusProvisioning
} else if (liveRunning) {
glyphStatus = 'running'
stateLabel = strings.status.running
stateStyle = styles.statusRunning
}
const description = typeof member.description === 'string' ? member.description.trim() : ''
const role = description.length > 0
? description
: member.name === 'lead'
? strings.chat.teamLead
: typeof member.name === 'string' && member.name.length > 0
? member.name
: member.id
const content = (
<>
{role}
{stateLabel}
{active !== undefined && typeof active.subject === 'string' && (
{active.subject} · {taskStatusLabel(active.status)}
)}
{owned.length > 1 && (
{strings.chat.teamMoreTasks(owned.length)}
)}
{member.phase === 'failed' && typeof member.error === 'string' && member.error.length > 0 && (
{member.error}
)}
{session !== undefined ? (
) : (
)}
>
)
return session === undefined ? (
{content}
) : (
onOpen?.(session)}
style={({ pressed }) => [styles.row, pressed && styles.rowPressed]}
testID={`${testID}-item-${index}`}
>
{content}
)
})}
)}
)
})
/** Legacy popover listing the session's subagent (child) conversations (retained for backward compatibility). */
export function ChildAgentPanel({ visible, sessions, onClose, onOpen }: {
visible: boolean
sessions: RemoteSession[]
onClose: () => void
onOpen: (session: RemoteSession) => void
}) {
const { colors } = useTheme()
const styles = useThemedStyles(createLegacyStyles)
const listMaxHeight = 360
return (
{strings.chat.subagentsTitle(sessions.length)}
{sessions.map(item => {
const parsed = parseAgentLabel(resolveSessionDisplayTitle(item) ?? item.title)
const role = parsed.role ?? item.title ?? strings.sessions.child
return (
onOpen(item)}
style={({ pressed }) => [styles.subagentRow, pressed && styles.resendRowPressed]}
>
{role}
{item.running
? {strings.status.running}
: {strings.sessions.child}}
{parsed.task.length > 0 && (
{parsed.task}
)}
)
})}
)
}
/** Legacy agent-team popover (retained for backward compatibility). */
export function TeamAgentPanel({ visible, members, tasks, sessions, onClose, onOpen }: {
visible: boolean
members: TeamMemberSnapshot[]
tasks: TeamTaskSnapshot[]
sessions: RemoteSession[]
onClose: () => void
onOpen: (session: RemoteSession) => void
}) {
const { colors } = useTheme()
const styles = useThemedStyles(createLegacyStyles)
const listMaxHeight = 360
const taskStatusLabel = (status: TeamTaskSnapshot['status']) => status === 'completed'
? strings.chat.teamTaskCompleted
: status === 'in_progress'
? strings.chat.teamTaskInProgress
: strings.chat.teamTaskPending
return (
{strings.chat.teamTitle(members.length)}
{members.map(member => {
const session = sessions.find(item => item.sessionId === member.id)
const owned = tasks.filter(task => task.ownerId === member.id && task.status !== 'deleted')
const active = owned.find(task => task.status === 'in_progress')
?? owned.find(task => task.status === 'pending')
?? owned[0]
const liveRunning = session?.running === true
const stateLabel = member.phase === 'failed'
? strings.chat.teamPhaseFailed
: member.phase === 'provisioning'
? strings.chat.teamPhaseProvisioning
: liveRunning
? strings.status.running
: strings.chat.teamIdle
const stateStyle = member.phase === 'failed'
? styles.subagentRunningFailed
: member.phase === 'provisioning'
? styles.subagentProvisioning
: liveRunning
? styles.subagentRunning
: styles.subagentIdle
const description = typeof member.description === 'string' ? member.description.trim() : ''
const role = description.length > 0
? description
: member.name === 'lead' ? strings.chat.teamLead
: typeof member.name === 'string' && member.name.length > 0 ? member.name : member.id
const row = (
{role}
{stateLabel}
{active !== undefined && typeof active.subject === 'string' && (
{active.subject} · {taskStatusLabel(active.status)}
)}
{owned.length > 1 && (
{strings.chat.teamMoreTasks(owned.length)}
)}
{member.phase === 'failed' && typeof member.error === 'string' && member.error.length > 0 && (
{member.error}
)}
)
return session === undefined
?
{row}
: onOpen(session)}
style={({ pressed }) => [styles.subagentRow, pressed && styles.resendRowPressed]}
>
{row}
})}
)
}
const createCardStyles = (colors: ThemeColors) =>
StyleSheet.create({
card: {
borderRadius: radius.xl,
backgroundColor: colors.card,
borderWidth: 1,
borderColor: colors.border,
overflow: 'hidden',
},
header: {
minHeight: 44,
paddingHorizontal: spacing.sm,
paddingVertical: spacing.xs,
flexDirection: 'row',
alignItems: 'center',
gap: spacing.xs,
},
pressed: {
backgroundColor: colors.surfaceStrong,
},
leadIcon: {
width: 20,
height: 20,
alignItems: 'center',
justifyContent: 'center',
},
title: {
...typography.smallStrong,
color: colors.ink,
flexShrink: 1,
},
progressContainer: {
flex: 1,
flexDirection: 'row',
alignItems: 'center',
gap: spacing.xs,
paddingLeft: spacing.xxs,
},
progress: {
...typography.small,
color: colors.muted,
fontVariant: ['tabular-nums'],
},
chevron: {
width: 20,
height: 20,
alignItems: 'center',
justifyContent: 'center',
},
contentWrap: {
paddingBottom: spacing.xs,
},
divider: {
height: StyleSheet.hairlineWidth,
backgroundColor: colors.separator,
marginHorizontal: spacing.sm,
marginBottom: spacing.xs,
},
list: {
paddingHorizontal: spacing.sm,
},
row: {
flexDirection: 'row',
alignItems: 'flex-start',
gap: spacing.xs,
paddingVertical: spacing.xs,
minHeight: 48,
borderBottomWidth: StyleSheet.hairlineWidth,
borderBottomColor: colors.separator,
},
rowPressed: {
backgroundColor: colors.surfaceStrong,
},
glyphWrap: {
width: 20,
height: 20,
alignItems: 'center',
justifyContent: 'center',
marginTop: 2,
},
rowCopy: {
flex: 1,
gap: 2,
},
rowTitleLine: {
flexDirection: 'row',
alignItems: 'center',
gap: spacing.xs,
},
rowRole: {
...typography.smallStrong,
color: colors.ink,
flexShrink: 1,
},
rowTask: {
...typography.small,
color: colors.muted,
lineHeight: 18,
},
statusRunning: {
...typography.caption,
color: colors.primary,
},
statusProvisioning: {
...typography.caption,
color: colors.warning ?? '#F59E0B',
},
statusFailed: {
...typography.caption,
color: colors.danger ?? '#EF4444',
},
statusIdle: {
...typography.caption,
color: colors.muted,
},
errorText: {
color: colors.danger ?? '#EF4444',
},
})