Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

thread-workflow-opening.tsx

197 lines7.0 KB

1import { useAtomValue } from "@effect/atom-react";
2import { StackActions, useNavigation } from "@react-navigation/native";
3import { scopeThreadRef } from "@t3tools/client-runtime/environment";
4import {
5 resolveWorkflowOpening,
6 workflowOpeningHeadline,
7 workflowOpeningParentThreadId,
8 type RuntimeSubagent,
9} from "@t3tools/client-runtime/state/subagentRuntime";
10import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
11import { useState } from "react";
12import { Pressable, View } from "react-native";
13​
14import { AppText as Text } from "../../components/ui/AppText";
15import { environmentThreadDetails } from "../../state/threads";
16import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider";
17import { useMarkdownCodeHighlight } from "./markdownCodeHighlightState";
18import { SubagentRow } from "./SubagentRow";
19import { WorkLogBlock } from "./work-log-layout";
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 });
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 };
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 navigation = useNavigation();
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 navigation.dispatch(
94 StackActions.push("Thread", {
95 environmentId: String(props.environmentId),
96 threadId,
97 }),
98 );
99 }}
100 className="rounded-lg px-3 py-3 active:bg-subtle"
101 >
102 <SubagentRow
103 environmentId={props.environmentId}
104 subagent={memberRowSubagent(member, opening, props.threadId)}
105 elapsed={null}
106 />
107 </Pressable>
108 );
109 };
110 return (
111 <WorkLogBlock>
112 <View className="mb-5 gap-3">
113 <View className="flex-row items-start gap-2">
114 <View className="min-w-0 flex-1 gap-0.5">
115 <Text className="font-t3-medium text-2xs text-foreground-muted">Workflow started</Text>
116 <Text numberOfLines={1} className="font-t3-medium text-sm text-foreground">
117 {headline.title}
118 </Text>
119 {headline.description ? (
120 <Text className="text-xs text-foreground-muted">{headline.description}</Text>
121 ) : null}
122 </View>
123 {headline.agents ? (
124 <Text className="shrink-0 text-2xs tabular-nums text-foreground-muted">
125 {headline.agents}
126 </Text>
127 ) : null}
128 </View>
129 {canListMembers
130 ? phases.map((phase) => (
131 <View key={phase.index} className="gap-px">
132 <Text className="px-1 pt-1 font-t3-medium text-2xs text-foreground-muted">
133 {phase.title} · {phase.settledCount}/{phase.members.length}
134 </Text>
135 {phase.members.map(renderMember)}
136 </View>
137 ))
138 : null}
139 {canListMembers ? unphased.map(renderMember) : null}
140 <WorkflowScript script={opening.script} />
141 </View>
142 </WorkLogBlock>
143 );
145​
146function WorkflowScript(props: { readonly script: string }) {
147 const [expanded, setExpanded] = useState(false);
148 const { themeAppearance } = useAppearancePreferences();
149 const script = props.script.trimEnd();
150 const lines = useMarkdownCodeHighlight({
151 code: script,
152 language: "javascript",
153 theme: themeAppearance,
154 enabled: true,
155 });
156 const lineCount = script.split("\n").length;
157 const canFold = lineCount > FOLDED_SCRIPT_LINES;
158 const shownLines = canFold && !expanded ? FOLDED_SCRIPT_LINES : lineCount;
159 let offset = 0;
160 const highlighted = lines?.slice(0, shownLines).flatMap((line, lineIndex) => {
161 const separator = lineIndex > 0 ? "\n" : "";
162 offset += separator.length;
163 return [
164 separator,
165 ...line.map((token) => {
166 const start = offset;
167 offset += token.content.length;
168 return (
169 <Text key={start} style={token.color ? { color: token.color } : undefined}>
170 {token.content}
171 </Text>
172 );
173 }),
174 ];
175 });
176 return (
177 <View className="gap-1">
178 <View className="rounded-lg bg-subtle px-3 py-2.5">
179 <Text selectable className="font-mono text-2xs leading-normal text-foreground">
180 {highlighted ?? script.split("\n").slice(0, shownLines).join("\n")}
181 </Text>
182 </View>
183 {canFold ? (
184 <Pressable
185 accessibilityRole="button"
186 accessibilityState={{ expanded }}
187 onPress={() => setExpanded((value) => !value)}
188 className="self-start py-1 active:opacity-70"
189 >
190 <Text className="font-t3-medium text-xs text-foreground-muted">
191 {expanded ? "Show less" : "Show full script"}
192 </Text>
193 </Pressable>
194 ) : null}
195 </View>
196 );