Open History to see the last commit
1import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
2import { isAttachmentOnlyBootstrapPrompt } from "@t3tools/shared/composerContextReferences";
3import {
4 type ModelSelection,
5 repositoryGroupingKeyOf,
6 type ServerProvider,
7} from "@t3tools/contracts";
8import {
9 buildNewThreadLink,
10 parsePromptLinkRepository,
11 PROMPT_LINK_MAX_PROMPT_LENGTH,
12 type PromptLinkRepository,
13} from "@t3tools/shared/promptLinks";
14
15import { type ComposerThreadTarget, useComposerDraftStore } from "~/composerDraftStore";
16import { writeTextToClipboard } from "~/hooks/useCopyToClipboard";
17import { configuredHostedAppUrl } from "~/hostedPairing";
18
19import { stackedThreadToast, toastManager } from "../ui/toast";
20
21/** A shareable link to a prompt on the hosted app, or null when the prompt cannot fit in one. */
22export function promptLinkFor(
23 prompt: string,
24 context?: {
25 readonly repository: PromptLinkRepository | null;
26 readonly provider: string | null;
27 readonly model: string | null;
28 },
29): string | null {
30 const trimmed = isAttachmentOnlyBootstrapPrompt(prompt) ? "" : prompt.trim();
31 if (trimmed.length === 0 || trimmed.length > PROMPT_LINK_MAX_PROMPT_LENGTH) return null;
32 return buildNewThreadLink({ prompt: trimmed, ...context, origin: configuredHostedAppUrl() });
33}
34
35export function promptLinkRepositoryOf(
36 project: Pick<EnvironmentProject, "repositoryIdentity"> | undefined,
37): PromptLinkRepository | null {
38 const identity = project?.repositoryIdentity;
39 if (identity == null) return null;
40 const key = repositoryGroupingKeyOf(identity);
41 return key.startsWith("github.com/")
42 ? parsePromptLinkRepository(key.slice("github.com/".length))
43 : null;
44}
45
46export function composerPromptLink(input: {
47 readonly target: ComposerThreadTarget;
48 readonly project: EnvironmentProject | undefined;
49 readonly providers: ReadonlyArray<ServerProvider>;
50 readonly threadModelSelection: ModelSelection | null;
51 readonly withContext: boolean;
52}): string | null {
53 const draft = useComposerDraftStore.getState().getComposerDraft(input.target);
54 if (!draft) return null;
55 if (!input.withContext) return promptLinkFor(draft.prompt);
56 const selection =
57 (draft.activeProvider ? draft.modelSelectionByProvider[draft.activeProvider] : undefined) ??
58 input.threadModelSelection;
59 const provider = selection
60 ? (input.providers.find((candidate) => candidate.instanceId === selection.instanceId)?.driver ??
61 null)
62 : null;
63 return promptLinkFor(draft.prompt, {
64 repository: promptLinkRepositoryOf(input.project),
65 provider,
66 model: provider === null ? null : (selection?.model ?? null),
67 });
68}
69
70export async function copyPromptLink(link: string): Promise<void> {
71 try {
72 if (!(await writeTextToClipboard(link))) return;
73 toastManager.add({ type: "success", title: "Prompt link copied" });
74 } catch (error) {
75 toastManager.add(
76 stackedThreadToast({
77 type: "error",
78 title: "Could not copy the prompt link",
79 description: error instanceof Error ? error.message : "An error occurred.",
80 }),
81 );
82 }
83}