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