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