Open History to see the last commit
1import type {
2 ComposerContextClipboardFragment,
3 ScopedThreadRef,
4 ThreadPullRequestLink,
5} from "@t3tools/contracts";
6import {
7 formatComposerContextReference,
8 threadContextRecord,
9} from "@t3tools/shared/composerContextReferences";
10
11import { resolveThreadCurrentPullRequestLink } from "./threadPullRequests.ts";
12
13export interface ThreadReferenceCopyTarget {
14 readonly kind: "pull-request" | "thread";
15 /** The plain text: a PR URL, or a thread chip's reference link. */
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/** A thread chip pastes into any composer whose agent reads that thread's machine. */
27export function threadChipCopyTarget(
28 threadRef: ScopedThreadRef,
29 title: string,
30): ThreadReferenceCopyTarget {
31 const record = threadContextRecord(threadRef, title);
32 return {
33 kind: "thread",
34 value: formatComposerContextReference(record),
35 description: record.title,
36 fragment: { version: 1, source: { environmentId: threadRef.environmentId }, records: [record] },
37 clipboardTarget: "thread",
38 successTitle: "Thread copied",
39 failureTitle: "Failed to copy thread",
40 };
41}
42
43export function resolveThreadReferenceCopyTarget(input: {
44 readonly threadRef: ScopedThreadRef;
45 readonly title: string;
46 /** Undefined means no PR panel; null means its URL is not available yet. */
47 readonly openPanelPullRequestUrl?: string | null | undefined;
48 readonly pullRequests?: ReadonlyArray<ThreadPullRequestLink> | undefined;
49 readonly linkedPullRequestUrl?: string | null;
50}): ThreadReferenceCopyTarget | null {
51 if (input.openPanelPullRequestUrl === null) return null;
52 const pullRequestUrl =
53 input.openPanelPullRequestUrl ??
54 resolveThreadCurrentPullRequestLink(input.pullRequests ?? [])?.url ??
55 input.linkedPullRequestUrl;
56 return pullRequestUrl
57 ? {
58 kind: "pull-request",
59 value: pullRequestUrl,
60 description: pullRequestUrl,
61 clipboardTarget: "pull request link",
62 successTitle: "PR link copied",
63 failureTitle: "Failed to copy PR link",
64 }
65 : threadChipCopyTarget(input.threadRef, input.title);
66}