|
| 1 | +import { useAtomValue } from "@effect/atom-react"; |
| 2 | +import { StackActions, useNavigation } from "@react-navigation/native"; |
| 3 | +import { scopeThreadRef } from "@t3tools/client-runtime/environment"; |
| 4 | +import { |
| 5 | + resolveWorkflowOpening, |
| 6 | + workflowOpeningHeadline, |
| 7 | + workflowOpeningParentThreadId, |
| 8 | + type RuntimeSubagent, |
| 9 | +} from "@t3tools/client-runtime/state/subagentRuntime"; |
| 10 | +import type { EnvironmentId, ThreadId } from "@t3tools/contracts"; |
| 11 | +import { useState } from "react"; |
| 12 | +import { Pressable, View } from "react-native"; |
| 13 | + |
| 14 | +import { AppText as Text } from "../../components/ui/AppText"; |
| 15 | +import { environmentThreadDetails } from "../../state/threads"; |
| 16 | +import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider"; |
| 17 | +import { useMarkdownCodeHighlight } from "./markdownCodeHighlightState"; |
| 18 | +import { SubagentRow } from "./SubagentRow"; |
| 19 | +import { WorkLogBlock } from "./work-log-layout"; |
| 20 | + |
| 21 | +// A script longer than this many lines shows its head until opened. |
| 22 | +const FOLDED_SCRIPT_LINES = 10; |
| 23 | + |
| 24 | +/** The workflow opening for a thread, or null; the parent's detail loads only for a workflow. */ |
| 25 | +export function useWorkflowOpening(environmentId: EnvironmentId, threadId: ThreadId) { |
| 26 | + const threadAtom = environmentThreadDetails.threadAtom(scopeThreadRef(environmentId, threadId)); |
| 27 | + // Selected as a primitive so an ordinary thread's updates never change the result. |
| 28 | + const parentId = useAtomValue(threadAtom, (thread) => |
| 29 | + thread === null |
| 30 | + ? null |
| 31 | + : workflowOpeningParentThreadId({ |
| 32 | + thread: thread.projection.thread, |
| 33 | + messages: thread.projection.messages, |
| 34 | + }), |
| 35 | + ); |
| 36 | + const projection = useAtomValue(threadAtom, (thread) => |
| 37 | + parentId === null ? null : (thread?.projection ?? null), |
| 38 | + ); |
| 39 | + const parentSubagents = useAtomValue( |
| 40 | + environmentThreadDetails.threadAtom(scopeThreadRef(environmentId, parentId ?? threadId)), |
| 41 | + (parent) => (parentId === null ? null : (parent?.projection.subagents ?? null)), |
| 42 | + ); |
| 43 | + return projection === null |
| 44 | + ? null |
| 45 | + : resolveWorkflowOpening({ |
| 46 | + thread: projection.thread, |
| 47 | + messages: projection.messages, |
| 48 | + parentSubagents, |
| 49 | + }); |
| 50 | +} |
| 51 | + |
| 52 | +function memberRowSubagent( |
| 53 | + member: RuntimeSubagent, |
| 54 | + opening: WorkflowOpeningValue, |
| 55 | + coordinatorThreadId: ThreadId, |
| 56 | +) { |
| 57 | + return { |
| 58 | + // A member's parent thread is the coordinator's thread, the one showing this opening. |
| 59 | + threadId: coordinatorThreadId, |
| 60 | + childThreadId: member.childThreadId as ThreadId | null, |
| 61 | + model: member.model, |
| 62 | + driver: opening.driver!, |
| 63 | + providerInstanceId: opening.providerInstanceId!, |
| 64 | + title: member.title, |
| 65 | + prompt: member.progress ?? member.title, |
| 66 | + status: member.status, |
| 67 | + progress: undefined, |
| 68 | + result: member.result ?? member.error, |
| 69 | + }; |
| 70 | +} |
| 71 | + |
| 72 | +type WorkflowOpeningValue = NonNullable<ReturnType<typeof useWorkflowOpening>>; |
| 73 | + |
| 74 | +/** Replaces a workflow coordinator's script message: the plan, its agents, then the script. */ |
| 75 | +export function ThreadWorkflowOpening(props: { |
| 76 | + readonly environmentId: EnvironmentId; |
| 77 | + readonly threadId: ThreadId; |
| 78 | + readonly opening: WorkflowOpeningValue; |
| 79 | +}) { |
| 80 | + const navigation = useNavigation(); |
| 81 | + const { opening } = props; |
| 82 | + const headline = workflowOpeningHeadline(opening); |
| 83 | + const canListMembers = opening.driver !== null && opening.providerInstanceId !== null; |
| 84 | + const phases = (opening.group?.phases ?? []).filter((phase) => phase.members.length > 0); |
| 85 | + const unphased = opening.group?.unphasedMembers ?? []; |
| 86 | + const renderMember = (member: RuntimeSubagent) => { |
| 87 | + const threadId = member.childThreadId; |
| 88 | + return ( |
| 89 | + <Pressable |
| 90 | + key={member.id} |
| 91 | + accessibilityRole={threadId === null ? undefined : "link"} |
| 92 | + accessibilityHint={threadId === null ? undefined : "Opens this agent's thread"} |
| 93 | + disabled={threadId === null} |
| 94 | + onPress={() => { |
| 95 | + if (threadId !== null) |
| 96 | + navigation.dispatch( |
| 97 | + StackActions.push("Thread", { |
| 98 | + environmentId: String(props.environmentId), |
| 99 | + threadId, |
| 100 | + }), |
| 101 | + ); |
| 102 | + }} |
| 103 | + className="rounded-lg px-3 py-3 active:bg-subtle" |
| 104 | + > |
| 105 | + <SubagentRow |
| 106 | + environmentId={props.environmentId} |
| 107 | + subagent={memberRowSubagent(member, opening, props.threadId)} |
| 108 | + elapsed={null} |
| 109 | + /> |
| 110 | + </Pressable> |
| 111 | + ); |
| 112 | + }; |
| 113 | + return ( |
| 114 | + <WorkLogBlock> |
| 115 | + <View className="mb-5 gap-3"> |
| 116 | + <View className="flex-row items-start gap-2"> |
| 117 | + <View className="min-w-0 flex-1 gap-0.5"> |
| 118 | + <Text className="font-t3-medium text-2xs text-foreground-muted">Workflow started</Text> |
| 119 | + <Text numberOfLines={1} className="font-t3-medium text-sm text-foreground"> |
| 120 | + {headline.title} |
| 121 | + </Text> |
| 122 | + {headline.description ? ( |
| 123 | + <Text className="text-xs text-foreground-muted">{headline.description}</Text> |
| 124 | + ) : null} |
| 125 | + </View> |
| 126 | + {headline.agents ? ( |
| 127 | + <Text className="shrink-0 text-2xs tabular-nums text-foreground-muted"> |
| 128 | + {headline.agents} |
| 129 | + </Text> |
| 130 | + ) : null} |
| 131 | + </View> |
| 132 | + {canListMembers |
| 133 | + ? phases.map((phase) => ( |
| 134 | + <View key={phase.index} className="gap-px"> |
| 135 | + <Text className="px-1 pt-1 font-t3-medium text-2xs text-foreground-muted"> |
| 136 | + {phase.title} · {phase.settledCount}/{phase.members.length} |
| 137 | + </Text> |
| 138 | + {phase.members.map(renderMember)} |
| 139 | + </View> |
| 140 | + )) |
| 141 | + : null} |
| 142 | + {canListMembers ? unphased.map(renderMember) : null} |
| 143 | + <WorkflowScript script={opening.script} /> |
| 144 | + </View> |
| 145 | + </WorkLogBlock> |
| 146 | + ); |
| 147 | +} |
| 148 | + |
| 149 | +/** The script as highlighted code; a long one shows its head until opened. */ |
| 150 | +function WorkflowScript(props: { readonly script: string }) { |
| 151 | + const [expanded, setExpanded] = useState(false); |
| 152 | + const { themeAppearance } = useAppearancePreferences(); |
| 153 | + const script = props.script.trimEnd(); |
| 154 | + const lines = useMarkdownCodeHighlight({ |
| 155 | + code: script, |
| 156 | + language: "javascript", |
| 157 | + theme: themeAppearance, |
| 158 | + enabled: true, |
| 159 | + }); |
| 160 | + const lineCount = script.split("\n").length; |
| 161 | + const canFold = lineCount > FOLDED_SCRIPT_LINES; |
| 162 | + const shownLines = canFold && !expanded ? FOLDED_SCRIPT_LINES : lineCount; |
| 163 | + // Tokens are keyed by their offset into the script, like the work log's REPL code. |
| 164 | + let offset = 0; |
| 165 | + const highlighted = lines?.slice(0, shownLines).flatMap((line, lineIndex) => { |
| 166 | + const separator = lineIndex > 0 ? "\n" : ""; |
| 167 | + offset += separator.length; |
| 168 | + return [ |
| 169 | + separator, |
| 170 | + ...line.map((token) => { |
| 171 | + const start = offset; |
| 172 | + offset += token.content.length; |
| 173 | + return ( |
| 174 | + <Text key={start} style={token.color ? { color: token.color } : undefined}> |
| 175 | + {token.content} |
| 176 | + </Text> |
| 177 | + ); |
| 178 | + }), |
| 179 | + ]; |
| 180 | + }); |
| 181 | + return ( |
| 182 | + <View className="gap-1"> |
| 183 | + <View className="rounded-lg bg-subtle px-3 py-2.5"> |
| 184 | + <Text selectable className="font-mono text-2xs leading-normal text-foreground"> |
| 185 | + {highlighted ?? script.split("\n").slice(0, shownLines).join("\n")} |
| 186 | + </Text> |
| 187 | + </View> |
| 188 | + {canFold ? ( |
| 189 | + <Pressable |
| 190 | + accessibilityRole="button" |
| 191 | + accessibilityState={{ expanded }} |
| 192 | + onPress={() => setExpanded((value) => !value)} |
| 193 | + className="self-start py-1 active:opacity-70" |
| 194 | + > |
| 195 | + <Text className="font-t3-medium text-xs text-foreground-muted"> |
| 196 | + {expanded ? "Show less" : "Show full script"} |
| 197 | + </Text> |
| 198 | + </Pressable> |
| 199 | + ) : null} |
| 200 | + </View> |
| 201 | + ); |
| 202 | +} |
0 commit comments