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 type { PromptLinkRepository } from "@t3tools/shared/promptLinks"; |
| 2 | import { msg } from "gt-react-native"; |
| 3 | |
| 4 | import { m, type MessageResolver } from "../../i18n/messages"; |
| 5 | |
| 6 | const PROMPT_LINK_PARAM_KEYS = ["prompt", "repo", "pr", "provider", "model"] as const; |
| 7 | |
| 8 | type RouteParams = Readonly<Record<string, unknown>> | undefined; |
| 9 | |
| 10 | export function promptLinkSearchFromParams( |
| 11 | params: RouteParams, |
| 12 | keys: ReadonlyArray<string> = PROMPT_LINK_PARAM_KEYS, |
| 13 | ): string | null { |
| 14 | const search = new URLSearchParams(); |
| 15 | for (const key of keys) { |
| 16 | const raw = params?.[key]; |
| 17 | const value = Array.isArray(raw) ? raw[0] : raw; |
| 18 | if (typeof value === "string") search.set(key, value); |
| 19 | } |
| 20 | return search.size > 0 ? search.toString() : null; |
| 21 | } |
| 22 | |
| 23 | export function withoutPromptLinkParams(params: object | undefined): Record<string, unknown> { |
| 24 | return Object.fromEntries( |
| 25 | Object.entries(params ?? {}).filter( |
| 26 | ([key]) => !(PROMPT_LINK_PARAM_KEYS as ReadonlyArray<string>).includes(key), |
| 27 | ), |
| 28 | ); |
| 29 | } |
| 30 | |
| 31 | export function promptLinkNotice( |
| 32 | input: { |
| 33 | readonly hiddenCharacters: number; |
| 34 | readonly modelUnavailable?: boolean; |
| 35 | readonly pullRequestUnavailable?: number | null; |
| 36 | }, |
| 37 | resolve: MessageResolver = m, |
| 38 | ): string { |
| 39 | return [ |
| 40 | input.hiddenCharacters > 0 |
| 41 | ? resolve( |
| 42 | msg( |
| 43 | "This prompt had hidden characters, it may be malicious. They were removed. Read it before you send.", |
| 44 | ), |
| 45 | ) |
| 46 | : resolve(msg("This prompt came from a link. Read it before you send.")), |
| 47 | input.modelUnavailable |
| 48 | ? resolve(msg("The requested model is not available, so your default is used.")) |
| 49 | : null, |
| 50 | input.pullRequestUnavailable != null |
| 51 | ? resolve( |
| 52 | msg( |
| 53 | "Pull request #{number} could not be loaded, so this task starts from your default branch.", |
| 54 | { number: input.pullRequestUnavailable }, |
| 55 | ), |
| 56 | ) |
| 57 | : null, |
| 58 | ] |
| 59 | .filter((line) => line !== null) |
| 60 | .join("\n\n"); |
| 61 | } |
| 62 | |
| 63 | export function linkedThreadDraftAction(currentText: string, prompt: string) { |
| 64 | if (currentText.trim().length === 0) return "fill" as const; |
| 65 | return currentText === prompt ? ("keep" as const) : ("ask" as const); |
| 66 | } |
| 67 | |
| 68 | export function promptLinkScratchEnvironmentId<Id extends string>( |
| 69 | environmentIdsMostRecentFirst: ReadonlyArray<Id>, |
| 70 | scratchEnvironmentIds: ReadonlyArray<Id>, |
| 71 | ): Id | null { |
| 72 | return ( |
| 73 | environmentIdsMostRecentFirst.find((id) => scratchEnvironmentIds.includes(id)) ?? |
| 74 | scratchEnvironmentIds[0] ?? |
| 75 | null |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | export function latestThreadEnvironmentIds<Id extends string>( |
| 80 | threads: ReadonlyArray<{ readonly environmentId: Id; readonly updatedAt: string }>, |
| 81 | ): ReadonlyArray<Id> { |
| 82 | const latest = new Map<Id, string>(); |
| 83 | for (const thread of threads) { |
| 84 | if (thread.updatedAt > (latest.get(thread.environmentId) ?? "")) { |
| 85 | latest.set(thread.environmentId, thread.updatedAt); |
| 86 | } |
| 87 | } |
| 88 | return Array.from(latest.keys()).sort((left, right) => |
| 89 | (latest.get(right) ?? "").localeCompare(latest.get(left) ?? ""), |
| 90 | ); |
| 91 | } |
| 92 | |
| 93 | export const promptLinkRepositoryName = (repository: PromptLinkRepository) => |
| 94 | `${repository.owner}/${repository.name}`; |
| 95 | |
| 96 | /** |
| 97 | * Advances a parsed link at each navigation state; `resolve` is the link to apply. The first |
| 98 | * state after the parse must hold the link's screen. A link that never lands there, or whose |
| 99 | * screen is left before its target is ready, is dropped so it cannot apply on a later visit. |
| 100 | */ |
| 101 | export function stepPendingPromptLink<Link>( |
| 102 | link: Link, |
| 103 | input: { readonly screenOnStack: boolean; readonly ready: boolean }, |
| 104 | ): { readonly pending: Link | null; readonly resolve: Link | null } { |
| 105 | if (!input.screenOnStack) return { pending: null, resolve: null }; |
| 106 | if (input.ready) return { pending: null, resolve: link }; |
| 107 | return { pending: link, resolve: null }; |
| 108 | } |