The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { |
| 2 | ComposerContextClipboardFragment, |
| 3 | ScopedThreadRef, |
| 4 | ThreadPullRequestLink, |
| 5 | } from "@t3tools/contracts"; |
| 6 | import { threadContextRecord } from "@t3tools/shared/composerContextReferences"; |
| 7 | import { hostedThreadLink } from "@t3tools/shared/threadLinks"; |
| 8 | |
| 9 | import { resolveThreadCurrentPullRequestLink } from "./threadPullRequests.ts"; |
| 10 | import { mFallback, msg } from "gt-i18n"; |
| 11 | import type { MFunctionType } from "gt-i18n/types"; |
| 12 | |
| 13 | export interface ThreadReferenceCopyTarget { |
| 14 | readonly kind: "pull-request" | "thread"; |
| 15 | /** The plain text: a PR URL, or a link that opens the thread from anywhere. */ |
| 16 | readonly value: string; |
| 17 | /** What a toast or palette row shows for the copy. */ |
| 18 | readonly description: string; |
| 19 | /** T3's clipboard format, which pastes a thread back into a composer as a chip. */ |
| 20 | readonly fragment?: ComposerContextClipboardFragment; |
| 21 | readonly clipboardTarget: string; |
| 22 | readonly successTitle: string; |
| 23 | readonly failureTitle: string; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * A link that opens the thread from anywhere, through `hostedAppUrl`, this build's hosted app. It |
| 28 | * pastes back as a chip into any composer whose agent reads that thread's machine. |
| 29 | */ |
| 30 | export function threadChipCopyTarget( |
| 31 | threadRef: ScopedThreadRef, |
| 32 | title: string, |
| 33 | hostedAppUrl?: string, |
| 34 | resolve: MFunctionType = mFallback, |
| 35 | ): ThreadReferenceCopyTarget { |
| 36 | const record = threadContextRecord(threadRef, title); |
| 37 | return { |
| 38 | kind: "thread", |
| 39 | value: hostedThreadLink(threadRef, hostedAppUrl), |
| 40 | description: record.title, |
| 41 | fragment: { version: 1, source: { environmentId: threadRef.environmentId }, records: [record] }, |
| 42 | clipboardTarget: "thread", |
| 43 | successTitle: resolve(msg("Thread copied")), |
| 44 | failureTitle: resolve(msg("Failed to copy thread")), |
| 45 | }; |
| 46 | } |
| 47 | |
| 48 | export function resolveThreadReferenceCopyTarget( |
| 49 | input: { |
| 50 | readonly threadRef: ScopedThreadRef; |
| 51 | readonly title: string; |
| 52 | /** Undefined means no PR panel; null means its URL is not available yet. */ |
| 53 | readonly openPanelPullRequestUrl?: string | null | undefined; |
| 54 | readonly pullRequests?: ReadonlyArray<ThreadPullRequestLink> | undefined; |
| 55 | readonly linkedPullRequestUrl?: string | null; |
| 56 | readonly hostedAppUrl?: string; |
| 57 | }, |
| 58 | resolve: MFunctionType = mFallback, |
| 59 | ): ThreadReferenceCopyTarget | null { |
| 60 | if (input.openPanelPullRequestUrl === null) return null; |
| 61 | const pullRequestUrl = |
| 62 | input.openPanelPullRequestUrl ?? |
| 63 | resolveThreadCurrentPullRequestLink(input.pullRequests ?? [])?.url ?? |
| 64 | input.linkedPullRequestUrl; |
| 65 | return pullRequestUrl |
| 66 | ? { |
| 67 | kind: "pull-request", |
| 68 | value: pullRequestUrl, |
| 69 | description: pullRequestUrl, |
| 70 | clipboardTarget: "pull request link", |
| 71 | successTitle: resolve(msg("PR link copied")), |
| 72 | failureTitle: resolve(msg("Failed to copy PR link")), |
| 73 | } |
| 74 | : threadChipCopyTarget(input.threadRef, input.title, input.hostedAppUrl, resolve); |
| 75 | } |