Open History to see the last commit
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 };
34}
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 };
45}
46
47export interface ThreadChipTarget {
48 readonly href: string;
49 readonly environmentId: EnvironmentId;
50 readonly threadId: ThreadId;
51 readonly link: boolean;
52}
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:\/\/v1\/[^\s)]+)\)/g)) {
62 const threadId = parseThreadLinkHref(href);
63 if (threadId) targets.set(href, { href, environmentId, threadId, link: true });
64 }
65 }
66 for (const record of records ?? []) {
67 if (record.kind !== "thread" || !("threadId" in record)) continue;
68 const href = formatComposerContextHref("thread", record.contextId);
69 targets.set(href, {
70 href,
71 environmentId: record.environmentId,
72 threadId: record.threadId,
73 link: false,
74 });
75 }
76 return [...targets.values()];
77}
78
79export function enrichedLinkVariantPattern(url: string) {
80 return `^${url.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}$`;
81}
82
83export const ENRICHED_INLINE_FILE_LINK_REGEX =
84 /^(?!\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+)?)$/;
85
86export function enrichedSkillLinkRegex(skills?: ReadonlyArray<SelectableMarkdownSkill>) {
87 const names = [...new Set(skills?.map((skill) => skill.name) ?? [])].filter(Boolean);
88 if (names.length === 0) return null;
89 names.sort((a, b) => b.length - a.length);
90 const alternatives = names.map((name) => name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
91 return new RegExp(
92 `(?<![A-Za-z0-9_-])(?<!\\p{Sc})\\p{Sc}(?:${alternatives.join("|")})(?![A-Za-z0-9_-])`,
93 "u",
94 );
95}
96
97export function enrichedSkillDisplayName(skill: SelectableMarkdownSkill) {
98 return (
99 skill.displayName?.trim() ||
100 skill.name
101 .split(/[\s:_-]+/)
102 .filter(Boolean)
103 .map((word) => word.charAt(0).toUpperCase() + word.slice(1))
104 .join(" ")
105 );
106}
107
108export function enrichedSkillReference(
109 url: string,
110 skills?: ReadonlyArray<SelectableMarkdownSkill>,
111) {
112 const match = /^\p{Sc}(.+)$/u.exec(url);
113 return match ? skills?.find((skill) => skill.name === match[1]) : undefined;
114}
115
116export function enrichedSkillInstructionsPath(skill: SelectableMarkdownSkill) {
117 return skill.path && !/^[a-z][a-z\d+.-]*:\/\//i.test(skill.path) ? skill.path : undefined;
118}