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

thread-workflow-opening.tsx

198 lines7.1 KB

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