The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { LinkedThreadTarget } from "@t3tools/client-runtime/linked-thread"; |
| 2 | import { EnvironmentId, ThreadId } from "@t3tools/contracts"; |
| 3 | |
| 4 | /** A linked environment's thread as a right panel tab, read through `viaEnvironmentId`. */ |
| 5 | export type LinkedThreadSurface = { |
| 6 | id: `linked-thread:${string}`; |
| 7 | kind: "linked-thread"; |
| 8 | } & LinkedThreadTarget; |
| 9 | |
| 10 | /** One tab per thread and the environment it is read through, so opening it again focuses it. */ |
| 11 | export function linkedThreadSurface(target: LinkedThreadTarget): LinkedThreadSurface { |
| 12 | return { |
| 13 | id: `linked-thread:${encodeURIComponent(target.viaEnvironmentId)}:${encodeURIComponent(target.environmentId)}:${encodeURIComponent(target.threadId)}`, |
| 14 | kind: "linked-thread", |
| 15 | viaEnvironmentId: target.viaEnvironmentId, |
| 16 | environmentId: target.environmentId, |
| 17 | threadId: target.threadId, |
| 18 | label: target.label, |
| 19 | title: target.title, |
| 20 | }; |
| 21 | } |
| 22 | |
| 23 | /** A saved linked-thread surface with its id rebuilt, or null when its fields are not usable. */ |
| 24 | export function decodeLinkedThreadSurface(surface: object): LinkedThreadSurface | null { |
| 25 | const { viaEnvironmentId, environmentId, threadId, label, title } = surface as Record< |
| 26 | string, |
| 27 | unknown |
| 28 | >; |
| 29 | if ( |
| 30 | typeof viaEnvironmentId !== "string" || |
| 31 | typeof environmentId !== "string" || |
| 32 | typeof threadId !== "string" || |
| 33 | typeof label !== "string" || |
| 34 | viaEnvironmentId === "" || |
| 35 | environmentId === "" || |
| 36 | threadId === "" |
| 37 | ) { |
| 38 | return null; |
| 39 | } |
| 40 | return linkedThreadSurface({ |
| 41 | viaEnvironmentId: EnvironmentId.make(viaEnvironmentId), |
| 42 | environmentId: EnvironmentId.make(environmentId), |
| 43 | threadId: ThreadId.make(threadId), |
| 44 | label, |
| 45 | title: typeof title === "string" && title !== "" ? title : null, |
| 46 | }); |
| 47 | } |
| 48 | |
| 49 | /** A tab's title: the thread's own once known, else where the thread is. */ |
| 50 | export function linkedThreadTitle( |
| 51 | surface: Pick<LinkedThreadTarget, "title">, |
| 52 | fallback: string, |
| 53 | ): string { |
| 54 | return surface.title?.trim() || fallback; |
| 55 | } |
| 56 | |
| 57 | /** The main-screen route of a linked environment's thread, read through `viaEnvironmentId`. */ |
| 58 | export function linkedThreadRouteParams( |
| 59 | target: Pick<LinkedThreadTarget, "viaEnvironmentId" | "environmentId" | "threadId">, |
| 60 | ) { |
| 61 | return { |
| 62 | viaEnvironmentId: target.viaEnvironmentId, |
| 63 | linkedEnvironmentId: target.environmentId, |
| 64 | linkedThreadId: target.threadId, |
| 65 | }; |
| 66 | } |