The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { useAtomValue } from "@effect/atom-react"; |
| 2 | import { deriveReportedModelSelection } from "@t3tools/client-runtime/state/thread-execution"; |
| 3 | |
| 4 | import { appAtomRegistry } from "./atom-registry"; |
| 5 | import type { |
| 6 | EnvironmentProject, |
| 7 | EnvironmentThread, |
| 8 | EnvironmentThreadShell, |
| 9 | } from "@t3tools/client-runtime/state/shell"; |
| 10 | import { |
| 11 | allowsPreviewTunnel, |
| 12 | EnvironmentId, |
| 13 | ThreadId, |
| 14 | type ComposerContextRecord, |
| 15 | type ScopedProjectRef, |
| 16 | type ScopedThreadRef, |
| 17 | type ServerConfig, |
| 18 | } from "@t3tools/contracts"; |
| 19 | import { scopeThreadRef } from "@t3tools/client-runtime/environment"; |
| 20 | import { |
| 21 | guessThreadLinkRef, |
| 22 | hasThreadLinks, |
| 23 | parseThreadLinkHref, |
| 24 | threadLinkCandidates, |
| 25 | threadLinkRefs, |
| 26 | type ThreadLink, |
| 27 | } from "@t3tools/shared/threadLinks"; |
| 28 | import { relabelThreadLinks } from "@t3tools/shared/threadLinksCopy"; |
| 29 | import * as Schema from "effect/Schema"; |
| 30 | import { AsyncResult, Atom } from "effect/reactivity"; |
| 31 | import { msg, useGT, useMessages } from "gt-react-native"; |
| 32 | |
| 33 | import { threadChipTargets } from "../lib/enrichedLinkPresentation"; |
| 34 | import { resolveProjectChipIcon } from "../lib/projectIcon"; |
| 35 | import { projectGlyphImageAtom, projectGlyphKey } from "../native/ProjectChipIconImages"; |
| 36 | import { projectFaviconUrlAtom } from "./assets"; |
| 37 | import { orchestrationEnvironment } from "./orchestration"; |
| 38 | import { environmentProjects } from "./projects"; |
| 39 | import { environmentServerConfigsAtom, serverEnvironment } from "./server"; |
| 40 | import { environmentThreadDetails, environmentThreadShells } from "./threads"; |
| 41 | |
| 42 | const EMPTY_PROJECT_ATOM = Atom.make<EnvironmentProject | null>(null).pipe( |
| 43 | Atom.withLabel("mobile-project:empty"), |
| 44 | ); |
| 45 | const EMPTY_THREAD_SHELL_ATOM = Atom.make<EnvironmentThreadShell | null>(null).pipe( |
| 46 | Atom.withLabel("mobile-thread-shell:empty"), |
| 47 | ); |
| 48 | const EMPTY_SERVER_CONFIG_ATOM = Atom.make<ServerConfig | null>(null).pipe( |
| 49 | Atom.withLabel("mobile-server-config:empty"), |
| 50 | ); |
| 51 | const EMPTY_THREAD_ATOM = Atom.make<EnvironmentThread | null>(null).pipe( |
| 52 | Atom.withLabel("mobile-thread:empty"), |
| 53 | ); |
| 54 | /** Stands for the localized fallback title, resolved where the link renders. */ |
| 55 | const FALLBACK_THREAD_TITLE = Symbol("fallback-thread-title"); |
| 56 | type ThreadLinkTitle = string | typeof FALLBACK_THREAD_TITLE; |
| 57 | |
| 58 | const EMPTY_THREAD_LINK_TITLES_ATOM = Atom.make<ReadonlyMap<string, ThreadLinkTitle>>( |
| 59 | new Map(), |
| 60 | ).pipe(Atom.withLabel("mobile-thread-link-titles:empty")); |
| 61 | |
| 62 | const decodeThreadKey = Schema.decodeUnknownSync(Schema.Tuple([EnvironmentId, ThreadId])); |
| 63 | const threadKey = (ref: ScopedThreadRef) => JSON.stringify([ref.environmentId, ref.threadId]); |
| 64 | |
| 65 | // Omitted and archived targets need only a metadata lookup, not their conversation history. |
| 66 | const threadTitleAtom = Atom.family((key: string) => { |
| 67 | const ref = scopeThreadRef(...decodeThreadKey(JSON.parse(key))); |
| 68 | return Atom.make((get): ThreadLinkTitle | null => { |
| 69 | const shell = get(environmentThreadShells.threadShellAtom(ref)); |
| 70 | if (shell !== null) return shell.title; |
| 71 | const titles = get( |
| 72 | orchestrationEnvironment.threadLinkMetadata({ |
| 73 | environmentId: ref.environmentId, |
| 74 | input: { threadIds: [ref.threadId] }, |
| 75 | }), |
| 76 | ); |
| 77 | return AsyncResult.isSuccess(titles) |
| 78 | ? (titles.value[ref.threadId]?.title ?? (titles.waiting ? FALLBACK_THREAD_TITLE : null)) |
| 79 | : FALLBACK_THREAD_TITLE; |
| 80 | }).pipe(Atom.withLabel(`mobile-thread-title:${key}`)); |
| 81 | }); |
| 82 | |
| 83 | const decodeThreadKeys = Schema.decodeUnknownSync(Schema.Array(Schema.String)); |
| 84 | |
| 85 | const threadLinkTitlesAtom = Atom.family((key: string) => { |
| 86 | const refKeys = decodeThreadKeys(JSON.parse(key)); |
| 87 | return Atom.make((get) => { |
| 88 | const titles = new Map<string, ThreadLinkTitle>(); |
| 89 | for (const refKey of refKeys) { |
| 90 | const title = get(threadTitleAtom(refKey)); |
| 91 | if (title !== null) titles.set(refKey, title); |
| 92 | } |
| 93 | return titles; |
| 94 | }).pipe(Atom.withLabel(`mobile-thread-link-titles:${key}`)); |
| 95 | }); |
| 96 | |
| 97 | const decodeThreadChipTargets = Schema.decodeUnknownSync( |
| 98 | Schema.Array( |
| 99 | Schema.Struct({ |
| 100 | href: Schema.String, |
| 101 | environmentId: EnvironmentId, |
| 102 | threadId: ThreadId, |
| 103 | link: Schema.Boolean, |
| 104 | }), |
| 105 | ), |
| 106 | ); |
| 107 | |
| 108 | const threadProjectChipIconAtom = Atom.family((key: string) => { |
| 109 | const ref = scopeThreadRef(...decodeThreadKey(JSON.parse(key))); |
| 110 | return Atom.make((get): string | null => { |
| 111 | const shell = get(environmentThreadShells.threadShellAtom(ref)); |
| 112 | const metadata = |
| 113 | shell ?? |
| 114 | AsyncResult.getOrElse( |
| 115 | get( |
| 116 | orchestrationEnvironment.threadLinkMetadata({ |
| 117 | environmentId: ref.environmentId, |
| 118 | input: { threadIds: [ref.threadId] }, |
| 119 | }), |
| 120 | ), |
| 121 | () => null, |
| 122 | )?.[ref.threadId]; |
| 123 | const project = metadata?.projectId |
| 124 | ? get( |
| 125 | environmentProjects.projectAtom({ |
| 126 | environmentId: ref.environmentId, |
| 127 | projectId: metadata.projectId, |
| 128 | }), |
| 129 | ) |
| 130 | : null; |
| 131 | const icon = resolveProjectChipIcon( |
| 132 | project, |
| 133 | get(serverEnvironment.configValueAtom(ref.environmentId))?.scratchWorkspaceRoot, |
| 134 | () => |
| 135 | project |
| 136 | ? get( |
| 137 | projectFaviconUrlAtom({ |
| 138 | environmentId: ref.environmentId, |
| 139 | cwd: project.workspaceRoot, |
| 140 | faviconPath: project.faviconPath, |
| 141 | }), |
| 142 | ) |
| 143 | : null, |
| 144 | ); |
| 145 | if (!icon) return null; |
| 146 | if (icon.kind === "favicon") return icon.uri; |
| 147 | return AsyncResult.getOrElse(get(projectGlyphImageAtom(projectGlyphKey(icon))), () => null); |
| 148 | }).pipe(Atom.withLabel(`mobile-thread-project-chip-icon:${key}`)); |
| 149 | }); |
| 150 | |
| 151 | const EMPTY_THREAD_CHIP_ICONS: ReadonlyMap<string, string> = new Map(); |
| 152 | const EMPTY_THREAD_CHIP_ICONS_ATOM = Atom.make(EMPTY_THREAD_CHIP_ICONS).pipe( |
| 153 | Atom.withLabel("mobile-thread-chip-icons:empty"), |
| 154 | ); |
| 155 | |
| 156 | const threadChipIconsAtom = Atom.family((key: string) => { |
| 157 | const targets = decodeThreadChipTargets(JSON.parse(key)); |
| 158 | return Atom.make((get) => { |
| 159 | const icons = new Map<string, string>(); |
| 160 | for (const target of targets) { |
| 161 | const link = target.link ? parseThreadLinkHref(target.href) : null; |
| 162 | const ref = |
| 163 | (link ? resolveThreadLink(get, link, target.environmentId) : null) ?? |
| 164 | scopeThreadRef(target.environmentId, target.threadId); |
| 165 | const icon = get(threadProjectChipIconAtom(threadKey(ref))); |
| 166 | if (icon) icons.set(target.href, icon); |
| 167 | } |
| 168 | return icons; |
| 169 | }).pipe(Atom.withLabel(`mobile-thread-chip-icons:${key}`)); |
| 170 | }); |
| 171 | |
| 172 | export function waitForProject( |
| 173 | ref: ScopedProjectRef, |
| 174 | timeoutMs = 10_000, |
| 175 | ): Promise<EnvironmentProject | null> { |
| 176 | const atom = environmentProjects.projectAtom(ref); |
| 177 | const current = appAtomRegistry.get(atom); |
| 178 | if (current !== null) return Promise.resolve(current); |
| 179 | return new Promise((resolve) => { |
| 180 | let unsubscribe: (() => void) | null = null; |
| 181 | const timeout = setTimeout(() => { |
| 182 | unsubscribe?.(); |
| 183 | resolve(null); |
| 184 | }, timeoutMs); |
| 185 | const finish = (project: EnvironmentProject | null) => { |
| 186 | if (project === null) return; |
| 187 | clearTimeout(timeout); |
| 188 | unsubscribe?.(); |
| 189 | resolve(project); |
| 190 | }; |
| 191 | unsubscribe = appAtomRegistry.subscribe(atom, finish); |
| 192 | finish(appAtomRegistry.get(atom)); |
| 193 | }); |
| 194 | } |
| 195 | |
| 196 | export function useProjects(): ReadonlyArray<EnvironmentProject> { |
| 197 | return useAtomValue(environmentProjects.projectsAtom); |
| 198 | } |
| 199 | |
| 200 | export function useThreadShells(): ReadonlyArray<EnvironmentThreadShell> { |
| 201 | return useAtomValue(environmentThreadShells.threadShellsAtom); |
| 202 | } |
| 203 | |
| 204 | /** Thread shells minus those the tag filter hides, for lists that show threads to the user. */ |
| 205 | export function useVisibleThreadShells(): ReadonlyArray<EnvironmentThreadShell> { |
| 206 | return useAtomValue(environmentThreadShells.visibleThreadShellsAtom); |
| 207 | } |
| 208 | |
| 209 | /** Tasks linked environments run for a listed thread, by thread key; lists show them under it. */ |
| 210 | export function useDelegatedUnderParent(): ReadonlySet<string> { |
| 211 | return useAtomValue(environmentThreadShells.delegatedUnderParentAtom); |
| 212 | } |
| 213 | |
| 214 | export function useNavigationThreadShells(): ReadonlyArray<EnvironmentThreadShell> { |
| 215 | return useAtomValue(environmentThreadShells.navigationThreadShellsAtom); |
| 216 | } |
| 217 | |
| 218 | export function useProject(ref: ScopedProjectRef | null): EnvironmentProject | null { |
| 219 | return useAtomValue(ref === null ? EMPTY_PROJECT_ATOM : environmentProjects.projectAtom(ref)); |
| 220 | } |
| 221 | |
| 222 | export function useThreadShell(ref: ScopedThreadRef | null): EnvironmentThreadShell | null { |
| 223 | return useAtomValue( |
| 224 | ref === null ? EMPTY_THREAD_SHELL_ATOM : environmentThreadShells.threadShellAtom(ref), |
| 225 | ); |
| 226 | } |
| 227 | |
| 228 | /** Streaming timestamps, item counts, and the latest message can be stale during a run. */ |
| 229 | export function useQuietThreadShell(ref: ScopedThreadRef | null): EnvironmentThreadShell | null { |
| 230 | return useAtomValue( |
| 231 | ref === null ? EMPTY_THREAD_SHELL_ATOM : environmentThreadShells.quietThreadShellAtom(ref), |
| 232 | ); |
| 233 | } |
| 234 | |
| 235 | export function useChildThreadInputs(ref: ScopedThreadRef) { |
| 236 | return useAtomValue(environmentThreadShells.childThreadInputsAtom(ref)); |
| 237 | } |
| 238 | |
| 239 | export function useSideChats(ref: ScopedThreadRef) { |
| 240 | return useAtomValue(environmentThreadShells.sideChatsAtom(ref)); |
| 241 | } |
| 242 | |
| 243 | const NO_LINKED_ENVIRONMENTS: ReadonlyMap<EnvironmentId, string> = new Map(); |
| 244 | |
| 245 | /** `linked` names the machines `environmentId` links to, whose threads open read-only. */ |
| 246 | export function useLiveThreadLinkLabels( |
| 247 | markdown: string, |
| 248 | environmentId: EnvironmentId, |
| 249 | linked = NO_LINKED_ENVIRONMENTS, |
| 250 | ): string { |
| 251 | const resolve = useMessages(); |
| 252 | const gt = useGT(); |
| 253 | const titles = useAtomValue( |
| 254 | hasThreadLinks(markdown) |
| 255 | ? threadLinkTitlesAtom(JSON.stringify(threadLinkRefs(markdown, environmentId).map(threadKey))) |
| 256 | : EMPTY_THREAD_LINK_TITLES_ATOM, |
| 257 | ); |
| 258 | return relabelThreadLinks( |
| 259 | markdown, |
| 260 | environmentId, |
| 261 | (ref, label) => { |
| 262 | const title = titles.get(threadKey(ref)); |
| 263 | if (typeof title === "string") return title; |
| 264 | // A thread only the message's environment can reach keeps its written name. |
| 265 | const machine = linked.get(ref.environmentId); |
| 266 | if (machine !== undefined) { |
| 267 | return label?.trim() || gt("Thread on {label}", { label: machine }); |
| 268 | } |
| 269 | return title && resolve(msg("Thread", { $context: "fallback title of a linked thread" })); |
| 270 | }, |
| 271 | resolve, |
| 272 | ); |
| 273 | } |
| 274 | |
| 275 | /** Keyed reads; every thread is read only to place a v1 link its message's environment lacks. */ |
| 276 | export function resolveThreadLink( |
| 277 | read: <A>(atom: Atom.Atom<A>) => A, |
| 278 | link: ThreadLink, |
| 279 | messageEnvironmentId: EnvironmentId, |
| 280 | ): ScopedThreadRef | null { |
| 281 | const known = threadLinkCandidates(link, messageEnvironmentId).find((ref) => { |
| 282 | if (read(environmentThreadShells.threadShellAtom(ref)) !== null) return true; |
| 283 | const metadata = read( |
| 284 | orchestrationEnvironment.threadLinkMetadata({ |
| 285 | environmentId: ref.environmentId, |
| 286 | input: { threadIds: [ref.threadId] }, |
| 287 | }), |
| 288 | ); |
| 289 | return AsyncResult.getOrElse(metadata, () => null)?.[ref.threadId] !== undefined; |
| 290 | }); |
| 291 | if (known) return known; |
| 292 | return link.environmentId === null |
| 293 | ? guessThreadLinkRef(link, read(environmentThreadShells.threadRefsAtom)) |
| 294 | : scopeThreadRef(link.environmentId, link.threadId); |
| 295 | } |
| 296 | |
| 297 | export function useThreadChipIcons( |
| 298 | environmentId: EnvironmentId, |
| 299 | markdown: string, |
| 300 | records?: ReadonlyArray<ComposerContextRecord>, |
| 301 | ): ReadonlyMap<string, string> { |
| 302 | const targets = threadChipTargets(environmentId, markdown, records); |
| 303 | return useAtomValue( |
| 304 | targets.length === 0 |
| 305 | ? EMPTY_THREAD_CHIP_ICONS_ATOM |
| 306 | : threadChipIconsAtom(JSON.stringify(targets)), |
| 307 | ); |
| 308 | } |
| 309 | |
| 310 | export function useEnvironmentServerConfig( |
| 311 | environmentId: EnvironmentId | null, |
| 312 | ): ServerConfig | null { |
| 313 | return useAtomValue( |
| 314 | environmentId === null |
| 315 | ? EMPTY_SERVER_CONFIG_ATOM |
| 316 | : serverEnvironment.configValueAtom(environmentId), |
| 317 | ); |
| 318 | } |
| 319 | |
| 320 | /** Whether the environment turned its preview tunnel off; false until its config loads. */ |
| 321 | export function useEnvironmentPreviewTunnelOff(environmentId: EnvironmentId | null): boolean { |
| 322 | const config = useEnvironmentServerConfig(environmentId); |
| 323 | return config !== null && !allowsPreviewTunnel(config); |
| 324 | } |
| 325 | |
| 326 | export function useServerConfigs(): ReadonlyMap<EnvironmentId, ServerConfig> { |
| 327 | return useAtomValue(environmentServerConfigsAtom); |
| 328 | } |
| 329 | |
| 330 | const selectReportedModelSelection = (thread: EnvironmentThread | null) => |
| 331 | thread === null ? null : deriveReportedModelSelection(thread.projection); |
| 332 | |
| 333 | export function useThreadReportedModelSelection(ref: ScopedThreadRef | null) { |
| 334 | return useAtomValue( |
| 335 | ref === null ? EMPTY_THREAD_ATOM : environmentThreadDetails.threadAtom(ref), |
| 336 | selectReportedModelSelection, |
| 337 | ); |
| 338 | } |