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

enrichedLinkPresentation.ts

125 lines4.4 KB

1import type { ComposerContextRecord, EnvironmentId, ThreadId } from "@t3tools/contracts";
2import {
3 composerChipSizeSuffix,
4 contextChipIcon,
5 contextChipPresentation,
6 contextRecordFileName,
7 type ContextChipIconRef,
8 UNAVAILABLE_CONTEXT_CHIP_PRESENTATION,
9} from "./composerChipPresentation";
10import type { SelectableMarkdownSkill } from "../native/SelectableMarkdownText.types";
11import {
12 formatComposerContextHref,
13 parseComposerContextHref,
14} from "@t3tools/shared/composerContextReferences";
15import { hasThreadLinks, parseThreadLinkHref } from "@t3tools/shared/threadLinks";
16​
17export function enrichedContextLinkPresentation(
18 href: string,
19 records?: ReadonlyArray<ComposerContextRecord>,
20): { accent?: string; icon: ContextChipIconRef; label?: string } | undefined {
21 const reference = parseComposerContextHref(href);
22 if (!reference) return undefined;
23 const record = records?.find((candidate) => candidate.contextId === reference.contextId);
24 const presentation =
25 records && !record
26 ? UNAVAILABLE_CONTEXT_CHIP_PRESENTATION
27 : contextChipPresentation(reference.kind, record);
28 const size = composerChipSizeSuffix(record);
29 return {
30 ...(presentation.accent ? { accent: presentation.accent } : {}),
31 icon: contextChipIcon(presentation, contextRecordFileName(record)),
32 ...(record && size ? { label: `${record.label} · ${size}` } : {}),
33 };
35​
36export function enrichedThreadLinkPresentation(
37 href: string,
38): { accent?: string; icon: ContextChipIconRef } | undefined {
39 if (!parseThreadLinkHref(href)) return undefined;
40 const presentation = contextChipPresentation("thread");
41 return {
42 ...(presentation.accent ? { accent: presentation.accent } : {}),
43 icon: contextChipIcon(presentation, ""),
44 };
46​
47export interface ThreadChipTarget {
48 readonly href: string;
49 readonly environmentId: EnvironmentId;
50 readonly threadId: ThreadId;
51 readonly link: boolean;
53​
54export function threadChipTargets(
55 environmentId: EnvironmentId,
56 markdown: string,
57 records?: ReadonlyArray<ComposerContextRecord>,
58): ReadonlyArray<ThreadChipTarget> {
59 const targets = new Map<string, ThreadChipTarget>();
60 if (hasThreadLinks(markdown)) {
61 for (const [, href = ""] of markdown.matchAll(/\]\((t3-thread:\/\/v[12]\/[^\s)]+)\)/g)) {
62 const link = parseThreadLinkHref(href);
63 if (link) {
64 targets.set(href, {
65 href,
66 environmentId: link.environmentId ?? environmentId,
67 threadId: link.threadId,
68 link: true,
69 });
70 }
71 }
72 }
73 for (const record of records ?? []) {
74 if (record.kind !== "thread" || !("threadId" in record)) continue;
75 const href = formatComposerContextHref("thread", record.contextId);
76 targets.set(href, {
77 href,
78 environmentId: record.environmentId,
79 threadId: record.threadId,
80 link: false,
81 });
82 }
83 return [...targets.values()];
85​
86export function enrichedLinkVariantPattern(url: string) {
87 return `^${url.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}$`;
89​
90export const ENRICHED_INLINE_FILE_LINK_REGEX =
91 /^(?!\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`]+|(?:[A-Za-z0-9._-]+[\\/])+[A-Za-z0-9._-]+\.[A-Za-z0-9_-]+(?::\d+){0,2}|[A-Za-z0-9._-]+\.[A-Za-z0-9_-]+:\d+(?::\d+)?)$/;
92​
93export function enrichedSkillLinkRegex(skills?: ReadonlyArray<SelectableMarkdownSkill>) {
94 const names = [...new Set(skills?.map((skill) => skill.name) ?? [])].filter(Boolean);
95 if (names.length === 0) return null;
96 names.sort((a, b) => b.length - a.length);
97 const alternatives = names.map((name) => name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
98 return new RegExp(
99 `(?<![A-Za-z0-9_-])(?<!\\p{Sc})\\p{Sc}(?:${alternatives.join("|")})(?![A-Za-z0-9_-])`,
100 "u",
101 );
103​
104export function enrichedSkillDisplayName(skill: SelectableMarkdownSkill) {
105 return (
106 skill.displayName?.trim() ||
107 skill.name
108 .split(/[\s:_-]+/)
109 .filter(Boolean)
110 .map((word) => word.charAt(0).toUpperCase() + word.slice(1))
111 .join(" ")
112 );
114​
115export 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;
122​
123export function enrichedSkillInstructionsPath(skill: SelectableMarkdownSkill) {
124 return skill.path && !/^[a-z][a-z\d+.-]*:\/\//i.test(skill.path) ? skill.path : undefined;