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