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, 03:37 UTC
| 1 | import { scopeThreadRef } from "@t3tools/client-runtime/environment"; |
| 2 | import type { EnvironmentShellStatus } from "@t3tools/client-runtime/state/shell"; |
| 3 | import type { EnvironmentThreadState } from "@t3tools/client-runtime/state/threads"; |
| 4 | import type { EnvironmentId, ScopedThreadRef, ThreadId } from "@t3tools/contracts"; |
| 5 | import * as Option from "effect/Option"; |
| 6 | import type { DraftId } from "./composerDraftStore"; |
| 7 | |
| 8 | export type ThreadRouteTarget = |
| 9 | | { |
| 10 | kind: "server"; |
| 11 | threadRef: ScopedThreadRef; |
| 12 | } |
| 13 | | { |
| 14 | kind: "draft"; |
| 15 | draftId: DraftId; |
| 16 | }; |
| 17 | |
| 18 | type DraftThreadRouteState = { |
| 19 | environmentId: EnvironmentId; |
| 20 | threadId: ThreadId; |
| 21 | promotedTo?: ScopedThreadRef | null; |
| 22 | }; |
| 23 | |
| 24 | export type ThreadRouteRenderState = "loading" | "ready" | "missing"; |
| 25 | |
| 26 | export function isThreadRouteSnapshotAuthoritative( |
| 27 | status: EnvironmentShellStatus | null | undefined, |
| 28 | ): boolean { |
| 29 | return status === "live"; |
| 30 | } |
| 31 | |
| 32 | export function resolveThreadRouteRenderState(input: { |
| 33 | bootstrapComplete: boolean; |
| 34 | serverThreadExists: boolean; |
| 35 | serverThreadDeleted: boolean; |
| 36 | unlistedThreadDetail: EnvironmentThreadState; |
| 37 | draftThreadExists: boolean; |
| 38 | }): ThreadRouteRenderState { |
| 39 | if (input.draftThreadExists || (input.serverThreadExists && !input.serverThreadDeleted)) { |
| 40 | return "ready"; |
| 41 | } |
| 42 | if (!input.bootstrapComplete) { |
| 43 | return "loading"; |
| 44 | } |
| 45 | if (input.serverThreadExists || input.unlistedThreadDetail.status === "deleted") { |
| 46 | return "missing"; |
| 47 | } |
| 48 | // Shell membership can lag a new thread; its detail decides. |
| 49 | const projection = Option.getOrNull(input.unlistedThreadDetail.data); |
| 50 | if (projection === null) { |
| 51 | return "loading"; |
| 52 | } |
| 53 | return projection.thread.deletedAt !== null || projection.thread.archivedAt !== null |
| 54 | ? "missing" |
| 55 | : "ready"; |
| 56 | } |
| 57 | |
| 58 | export function buildThreadRouteParams(ref: ScopedThreadRef): { |
| 59 | environmentId: EnvironmentId; |
| 60 | threadId: ThreadId; |
| 61 | } { |
| 62 | return { |
| 63 | environmentId: ref.environmentId, |
| 64 | threadId: ref.threadId, |
| 65 | }; |
| 66 | } |
| 67 | |
| 68 | /** The router keeps reserved characters encoded in location.pathname. */ |
| 69 | export function isThreadRoutePathname(pathname: string, ref: ScopedThreadRef): boolean { |
| 70 | const parts = pathname.replace(/\/$/, "").split("/"); |
| 71 | if (parts.length !== 3 || parts[0] !== "") return false; |
| 72 | try { |
| 73 | return ( |
| 74 | decodeURIComponent(parts[1]!) === ref.environmentId && |
| 75 | decodeURIComponent(parts[2]!) === ref.threadId |
| 76 | ); |
| 77 | } catch { |
| 78 | return false; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | export function buildDraftThreadRouteParams(draftId: DraftId): { |
| 83 | draftId: DraftId; |
| 84 | } { |
| 85 | return { draftId }; |
| 86 | } |
| 87 | |
| 88 | export function resolveThreadRouteRef( |
| 89 | params: Partial<Record<"environmentId" | "threadId", string | undefined>>, |
| 90 | ): ScopedThreadRef | null { |
| 91 | if (!params.environmentId || !params.threadId) { |
| 92 | return null; |
| 93 | } |
| 94 | |
| 95 | return scopeThreadRef(params.environmentId as EnvironmentId, params.threadId as ThreadId); |
| 96 | } |
| 97 | |
| 98 | export function resolveThreadRouteTarget( |
| 99 | params: Partial<Record<"environmentId" | "threadId" | "draftId", string | undefined>>, |
| 100 | ): ThreadRouteTarget | null { |
| 101 | if (params.environmentId && params.threadId) { |
| 102 | return { |
| 103 | kind: "server", |
| 104 | threadRef: scopeThreadRef(params.environmentId as EnvironmentId, params.threadId as ThreadId), |
| 105 | }; |
| 106 | } |
| 107 | |
| 108 | if (!params.draftId) { |
| 109 | return null; |
| 110 | } |
| 111 | |
| 112 | return { |
| 113 | kind: "draft", |
| 114 | draftId: params.draftId as DraftId, |
| 115 | }; |
| 116 | } |
| 117 | |
| 118 | export function resolveActiveThreadRouteRef( |
| 119 | target: ThreadRouteTarget | null, |
| 120 | draftThread: DraftThreadRouteState | null, |
| 121 | promotedDraftRedirect: ScopedThreadRef | null = null, |
| 122 | ): ScopedThreadRef | null { |
| 123 | if (target?.kind === "server") { |
| 124 | return target.threadRef; |
| 125 | } |
| 126 | if (target?.kind !== "draft") { |
| 127 | return null; |
| 128 | } |
| 129 | return draftThread?.promotedTo ?? promotedDraftRedirect; |
| 130 | } |