Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

copyPromptLink.ts

85 lines3.1 KB

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";
18import { draftModelSelectionApplies } from "~/lib/chatThreadActions";
19​
20import { stackedThreadToast, toastManager } from "../ui/toast";
21​
22/** A shareable link to a prompt on the hosted app, or null when the prompt cannot fit in one. */
23export function promptLinkFor(
24 prompt: string,
25 context?: {
26 readonly repository: PromptLinkRepository | null;
27 readonly provider: string | null;
28 readonly model: string | null;
29 },
30): string | null {
31 const trimmed = isAttachmentOnlyBootstrapPrompt(prompt) ? "" : prompt.trim();
32 if (trimmed.length === 0 || trimmed.length > PROMPT_LINK_MAX_PROMPT_LENGTH) return null;
33 return buildNewThreadLink({ prompt: trimmed, ...context, origin: configuredHostedAppUrl() });
35​
36export function promptLinkRepositoryOf(
37 project: Pick<EnvironmentProject, "repositoryIdentity"> | undefined,
38): PromptLinkRepository | null {
39 const identity = project?.repositoryIdentity;
40 if (identity == null) return null;
41 const key = repositoryGroupingKeyOf(identity);
42 return key.startsWith("github.com/")
43 ? parsePromptLinkRepository(key.slice("github.com/".length))
44 : null;
46​
47export function composerPromptLink(input: {
48 readonly target: ComposerThreadTarget;
49 readonly project: EnvironmentProject | undefined;
50 readonly providers: ReadonlyArray<ServerProvider>;
51 readonly threadModelSelection: ModelSelection | null;
52 readonly threadStarted: boolean;
53 readonly withContext: boolean;
54}): string | null {
55 const draft = useComposerDraftStore.getState().getComposerDraft(input.target);
56 if (!draft) return null;
57 if (!input.withContext) return promptLinkFor(draft.prompt);
58 const selection =
59 (draft.activeProvider && draftModelSelectionApplies(draft, input.threadStarted)
60 ? draft.modelSelectionByProvider[draft.activeProvider]
61 : undefined) ?? input.threadModelSelection;
62 const provider = selection
63 ? (input.providers.find((candidate) => candidate.instanceId === selection.instanceId)?.driver ??
64 null)
65 : null;
66 return promptLinkFor(draft.prompt, {
67 repository: promptLinkRepositoryOf(input.project),
68 provider,
69 model: provider === null ? null : (selection?.model ?? null),
70 });
72​
73export async function copyPromptLink(link: string): Promise<void> {
74 try {
75 await writeTextToClipboard(link);
76 } catch (error) {
77 toastManager.add(
78 stackedThreadToast({
79 type: "error",
80 title: "Could not copy the prompt link",
81 description: error instanceof Error ? error.message : "An error occurred.",
82 }),
83 );
84 }