The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { ComposerContextRecord, EnvironmentId, ThreadId } from "@t3tools/contracts"; |
| 2 | import { |
| 3 | composerChipSizeSuffix, |
| 4 | contextChipIcon, |
| 5 | contextChipPresentation, |
| 6 | contextRecordFileName, |
| 7 | type ContextChipIconRef, |
| 8 | UNAVAILABLE_CONTEXT_CHIP_PRESENTATION, |
| 9 | } from "./composerChipPresentation"; |
| 10 | import { formatComposerModelMention } from "@t3tools/client-runtime/composer-model-mentions"; |
| 11 | import type { |
| 12 | SelectableMarkdownModel, |
| 13 | SelectableMarkdownSkill, |
| 14 | } from "../native/SelectableMarkdownText.types"; |
| 15 | import { |
| 16 | formatComposerContextHref, |
| 17 | parseComposerContextHref, |
| 18 | } from "@t3tools/shared/composerContextReferences"; |
| 19 | import { hasThreadLinks, parseThreadLinkHref } from "@t3tools/shared/threadLinks"; |
| 20 | import { EXTENSIONLESS_FILE_NAMES } from "@t3tools/shared/markdownLinks"; |
| 21 | |
| 22 | export function enrichedContextLinkPresentation( |
| 23 | href: string, |
| 24 | records?: ReadonlyArray<ComposerContextRecord>, |
| 25 | ): { accent?: string; icon: ContextChipIconRef; label?: string } | undefined { |
| 26 | const reference = parseComposerContextHref(href); |
| 27 | if (!reference) return undefined; |
| 28 | const record = records?.find((candidate) => candidate.contextId === reference.contextId); |
| 29 | const presentation = |
| 30 | records && !record |
| 31 | ? UNAVAILABLE_CONTEXT_CHIP_PRESENTATION |
| 32 | : contextChipPresentation(reference.kind, record); |
| 33 | const size = composerChipSizeSuffix(record); |
| 34 | return { |
| 35 | ...(presentation.accent ? { accent: presentation.accent } : {}), |
| 36 | icon: contextChipIcon(presentation, contextRecordFileName(record)), |
| 37 | ...(record && size ? { label: `${record.label} · ${size}` } : {}), |
| 38 | }; |
| 39 | } |
| 40 | |
| 41 | export function enrichedThreadLinkPresentation( |
| 42 | href: string, |
| 43 | ): { accent?: string; icon: ContextChipIconRef } | undefined { |
| 44 | if (!parseThreadLinkHref(href)) return undefined; |
| 45 | const presentation = contextChipPresentation("thread"); |
| 46 | return { |
| 47 | ...(presentation.accent ? { accent: presentation.accent } : {}), |
| 48 | icon: contextChipIcon(presentation, ""), |
| 49 | }; |
| 50 | } |
| 51 | |
| 52 | export interface ThreadChipTarget { |
| 53 | readonly href: string; |
| 54 | readonly environmentId: EnvironmentId; |
| 55 | readonly threadId: ThreadId; |
| 56 | readonly link: boolean; |
| 57 | } |
| 58 | |
| 59 | export function threadChipTargets( |
| 60 | environmentId: EnvironmentId, |
| 61 | markdown: string, |
| 62 | records?: ReadonlyArray<ComposerContextRecord>, |
| 63 | ): ReadonlyArray<ThreadChipTarget> { |
| 64 | const targets = new Map<string, ThreadChipTarget>(); |
| 65 | if (hasThreadLinks(markdown)) { |
| 66 | for (const [, href = ""] of markdown.matchAll(/\]\(<?(t3-thread:\/\/v[12]\/[^\s>)]+)>?\)/g)) { |
| 67 | const link = parseThreadLinkHref(href); |
| 68 | if (link) { |
| 69 | targets.set(href, { |
| 70 | href, |
| 71 | environmentId: link.environmentId ?? environmentId, |
| 72 | threadId: link.threadId, |
| 73 | link: true, |
| 74 | }); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | for (const record of records ?? []) { |
| 79 | if (record.kind !== "thread" || !("threadId" in record)) continue; |
| 80 | const href = formatComposerContextHref("thread", record.contextId); |
| 81 | targets.set(href, { |
| 82 | href, |
| 83 | environmentId: record.environmentId, |
| 84 | threadId: record.threadId, |
| 85 | link: false, |
| 86 | }); |
| 87 | } |
| 88 | return [...targets.values()]; |
| 89 | } |
| 90 | |
| 91 | export function enrichedLinkVariantPattern(url: string) { |
| 92 | return `^${url.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}$`; |
| 93 | } |
| 94 | |
| 95 | const EXTENSIONLESS_FILE_NAME_PATTERN = [...EXTENSIONLESS_FILE_NAMES] |
| 96 | .map((name) => name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")) |
| 97 | .join("|"); |
| 98 | |
| 99 | export const ENRICHED_INLINE_FILE_LINK_REGEX = new RegExp( |
| 100 | String.raw`^(?!\S*:\/\/)(?:(?:\.{1,2}[\\/]|~[\\/]|\/(?:Users|home|tmp|var|etc|opt|mnt|Volumes|private|root|usr|bin|sbin|lib|lib64|srv|dev|proc|sys|run|boot|media|workspace|workspaces)\/|[A-Za-z]:[\\/])[^\s\x60]+|(?:[\p{L}\p{M}\p{N}._-]+[\\/])+(?:[\p{L}\p{M}\p{N}._-]+\.[A-Za-z0-9_-]+|(?:${EXTENSIONLESS_FILE_NAME_PATTERN}))(?::\d+(?::\d+|-\d+)?)?|[\p{L}\p{M}\p{N}._-]+\.[A-Za-z0-9_-]+:\d+(?::\d+|-\d+)?)$`, |
| 101 | "u", |
| 102 | ); |
| 103 | |
| 104 | export function enrichedSkillLinkRegex(skills?: ReadonlyArray<SelectableMarkdownSkill>) { |
| 105 | const names = [...new Set(skills?.map((skill) => skill.name) ?? [])].filter(Boolean); |
| 106 | if (names.length === 0) return null; |
| 107 | names.sort((a, b) => b.length - a.length); |
| 108 | const alternatives = names.map((name) => name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")); |
| 109 | return new RegExp( |
| 110 | `(?<![A-Za-z0-9_-])(?<!\\p{Sc})\\p{Sc}(?:${alternatives.join("|")})(?![A-Za-z0-9_-])`, |
| 111 | "u", |
| 112 | ); |
| 113 | } |
| 114 | |
| 115 | export function enrichedSkillReference( |
| 116 | url: string, |
| 117 | skills?: ReadonlyArray<SelectableMarkdownSkill>, |
| 118 | ) { |
| 119 | const match = /^\p{Sc}(.+)$/u.exec(url); |
| 120 | return match ? skills?.find((skill) => skill.name === match[1]) : undefined; |
| 121 | } |
| 122 | |
| 123 | export function enrichedSkillInstructionsPath(skill: SelectableMarkdownSkill) { |
| 124 | return skill.path && !/^[a-z][a-z\d+.-]*:\/\//i.test(skill.path) ? skill.path : undefined; |
| 125 | } |
| 126 | |
| 127 | export function buildEnrichedModelReferences( |
| 128 | providers: ReadonlyArray<{ |
| 129 | readonly instanceId: string; |
| 130 | readonly driver: string; |
| 131 | readonly displayName?: string | undefined; |
| 132 | readonly iconUrl?: string | undefined; |
| 133 | readonly models: ReadonlyArray<{ |
| 134 | readonly slug: string; |
| 135 | readonly name: string; |
| 136 | readonly subProvider?: string | undefined; |
| 137 | }>; |
| 138 | }>, |
| 139 | sources?: ReadonlySet<string>, |
| 140 | ): ReadonlyArray<SelectableMarkdownModel> { |
| 141 | return providers.flatMap((provider) => |
| 142 | provider.models |
| 143 | .filter( |
| 144 | (model) => |
| 145 | !sources || sources.has(formatComposerModelMention(provider.instanceId, model.slug)), |
| 146 | ) |
| 147 | .map((model) => { |
| 148 | const qualifier = model.subProvider?.trim(); |
| 149 | const label = qualifier |
| 150 | ? model.name |
| 151 | .replace( |
| 152 | new RegExp( |
| 153 | `^${qualifier.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}(?:\\s*[.:/-]\\s*|\\s+)`, |
| 154 | "iu", |
| 155 | ), |
| 156 | "", |
| 157 | ) |
| 158 | .trim() || model.name |
| 159 | : model.name; |
| 160 | return { |
| 161 | source: formatComposerModelMention(provider.instanceId, model.slug), |
| 162 | label, |
| 163 | providerLabel: qualifier || provider.displayName || provider.instanceId, |
| 164 | driver: provider.driver, |
| 165 | iconUrl: provider.iconUrl, |
| 166 | }; |
| 167 | }), |
| 168 | ); |
| 169 | } |
| 170 | |
| 171 | export function enrichedModelLinkRegex(models?: ReadonlyArray<SelectableMarkdownModel>) { |
| 172 | if (!models?.length) return null; |
| 173 | const alternatives = models |
| 174 | .map((model) => model.source) |
| 175 | .sort((a, b) => b.length - a.length) |
| 176 | .map((source) => source.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")); |
| 177 | // LRM and RLM can open a paragraph whose first word runs against the text direction. |
| 178 | return new RegExp( |
| 179 | `(?<![^\\s,(\\["'\\u200E\\u200F])(?:${alternatives.join("|")})(?=$|[\\s,"']|[.;:!?\\)\\]]+(?=$|[\\s,"']))`, |
| 180 | "u", |
| 181 | ); |
| 182 | } |
| 183 | |
| 184 | export function enrichedModelReference( |
| 185 | source: string, |
| 186 | models?: ReadonlyArray<SelectableMarkdownModel>, |
| 187 | ) { |
| 188 | return models?.find((model) => model.source === source); |
| 189 | } |
| 190 | |
| 191 | export function enrichedModelDetails(model: SelectableMarkdownModel) { |
| 192 | return `${model.providerLabel}\n${model.source}`; |
| 193 | } |
| 194 | |
| 195 | export function enrichedTextLinkRegex( |
| 196 | skills?: ReadonlyArray<SelectableMarkdownSkill>, |
| 197 | models?: ReadonlyArray<SelectableMarkdownModel>, |
| 198 | ) { |
| 199 | const patterns = [enrichedSkillLinkRegex(skills), enrichedModelLinkRegex(models)] |
| 200 | .filter((pattern) => pattern !== null) |
| 201 | .map((pattern) => `(?:${pattern.source})`); |
| 202 | return patterns.length ? new RegExp(patterns.join("|"), "u") : null; |
| 203 | } |