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 type { OrchestrationV2AppThread, SideThreadOpenTarget } from "@t3tools/contracts"; |
| 2 | |
| 3 | /** |
| 4 | * A thread that opens beside the one you are in. `linked` lives on a linked environment this client |
| 5 | * has no connection to; the others are threads of a connected environment. |
| 6 | */ |
| 7 | export type SideThreadKind = "side-chat" | "subagent" | "provider-subagent" | "linked"; |
| 8 | |
| 9 | /** |
| 10 | * Which side thread `thread` is, or null for a thread that opens like any other. A row that does |
| 11 | * not carry how the thread was created cannot tell a provider's own subagent from the app's. |
| 12 | */ |
| 13 | export function sideThreadKindOf( |
| 14 | thread: |
| 15 | | (Pick<OrchestrationV2AppThread, "lineage"> & |
| 16 | Partial<Pick<OrchestrationV2AppThread, "creationSource">>) |
| 17 | | null |
| 18 | | undefined, |
| 19 | ): Exclude<SideThreadKind, "linked"> | null { |
| 20 | if (!thread) return null; |
| 21 | if (thread.lineage.sideChat === true && thread.lineage.parentThreadId !== null) { |
| 22 | return "side-chat"; |
| 23 | } |
| 24 | if (thread.lineage.relationshipToParent !== "subagent") return null; |
| 25 | return thread.creationSource === "provider" ? "provider-subagent" : "subagent"; |
| 26 | } |
| 27 | |
| 28 | export type SideThreadComposer = |
| 29 | /** The normal composer. */ |
| 30 | | "composer" |
| 31 | /** A bar that says the thread takes no messages. */ |
| 32 | | "read-only" |
| 33 | /** The same bar, offering to connect to the environment the thread is on. */ |
| 34 | | "read-only-connect"; |
| 35 | |
| 36 | /** What stands at the bottom of a side thread, in the panel and on the main screen alike. */ |
| 37 | export function sideThreadComposer(kind: SideThreadKind): SideThreadComposer { |
| 38 | switch (kind) { |
| 39 | case "linked": |
| 40 | return "read-only-connect"; |
| 41 | case "provider-subagent": |
| 42 | return "read-only"; |
| 43 | case "side-chat": |
| 44 | case "subagent": |
| 45 | return "composer"; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * How the user asked to open a side thread: a plain click or Enter, the same with the modifier |
| 51 | * (or a middle click), or one of the two places named outright, as a context menu does. |
| 52 | */ |
| 53 | export type SideThreadOpenIntent = "primary" | "alternate" | SideThreadOpenTarget; |
| 54 | |
| 55 | /** |
| 56 | * Where a side thread opens. The preference names the plain click's place and the modifier takes |
| 57 | * the other; without a panel to open in, everything goes to the main view. |
| 58 | */ |
| 59 | export function resolveSideThreadOpenTarget(input: { |
| 60 | readonly preference: SideThreadOpenTarget; |
| 61 | readonly intent: SideThreadOpenIntent; |
| 62 | readonly panelAvailable: boolean; |
| 63 | }): SideThreadOpenTarget { |
| 64 | if (!input.panelAvailable) return "main"; |
| 65 | switch (input.intent) { |
| 66 | case "primary": |
| 67 | return input.preference; |
| 68 | case "alternate": |
| 69 | return input.preference === "panel" ? "main" : "panel"; |
| 70 | default: |
| 71 | return input.intent; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /** The intent of a click, an auxiliary click or a key press on a side thread's row. */ |
| 76 | export function sideThreadOpenIntentOf( |
| 77 | event: { |
| 78 | readonly metaKey: boolean; |
| 79 | readonly ctrlKey: boolean; |
| 80 | /** Absent on key events. */ |
| 81 | readonly button?: number; |
| 82 | }, |
| 83 | mac: boolean, |
| 84 | ): Extract<SideThreadOpenIntent, "primary" | "alternate"> { |
| 85 | if (event.button === 1) return "alternate"; |
| 86 | return (mac ? event.metaKey : event.ctrlKey) ? "alternate" : "primary"; |
| 87 | } |
| 88 | |
| 89 | /** Where a side thread opens on mobile: the inspector pane where the layout has one. */ |
| 90 | export function resolveMobileSideThreadPresentation(input: { |
| 91 | readonly inspectorAvailable: boolean; |
| 92 | }): "inspector" | "full-screen" { |
| 93 | return input.inspectorAvailable ? "inspector" : "full-screen"; |
| 94 | } |