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