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