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

toolPresentation.ts

123 lines3.8 KB

1import type {
2 ToolActivityIcon,
3 ToolActivityNativeAppReference,
4 ToolActivitySource,
5 ToolActivitySurface,
6} from "@t3tools/contracts";
7​
8export interface ExtractedToolActivityPresentation {
9 readonly toolSurface?: ToolActivitySurface;
10 readonly toolIcon?: ToolActivityIcon;
11 readonly toolSource?: ToolActivitySource;
13​
14function asRecord(value: unknown): Record<string, unknown> | undefined {
15 return value !== null && typeof value === "object" && !Array.isArray(value)
16 ? (value as Record<string, unknown>)
17 : undefined;
19​
20function trimmedString(value: unknown, maxLength: number): string | undefined {
21 if (typeof value !== "string") return undefined;
22 const trimmed = value.trim();
23 return trimmed && trimmed.length <= maxLength ? trimmed : undefined;
25​
26function imageUrl(value: unknown): string | undefined {
27 const raw = trimmedString(value, 4096);
28 if (!raw) return undefined;
29 try {
30 const url = new URL(raw);
31 return url.protocol === "http:" || url.protocol === "https:" || url.protocol === "data:"
32 ? url.href
33 : undefined;
34 } catch {
35 return undefined;
36 }
38​
39function pageUrl(value: unknown): string | undefined {
40 const raw = trimmedString(value, 4096);
41 if (!raw) return undefined;
42 try {
43 const url = new URL(raw);
44 return url.protocol === "http:" || url.protocol === "https:" ? url.href : undefined;
45 } catch {
46 return undefined;
47 }
49​
50function nativeAppReference(value: unknown): ToolActivityNativeAppReference | undefined {
51 const app = asRecord(value);
52 const appId = trimmedString(app?.appId, 512);
53 if (app?._tag === "app-id" && appId && /^[A-Za-z0-9._-]+$/u.test(appId)) {
54 return { _tag: "app-id", appId };
55 }
56 const displayName = trimmedString(app?.displayName, 160);
57 if (app?._tag === "display-name" && displayName) {
58 return { _tag: "display-name", displayName };
59 }
60 return undefined;
62​
63function activityIcon(value: unknown): ToolActivityIcon | undefined {
64 const icon = asRecord(value);
65 if (icon?._tag === "website") {
66 const resolvedPageUrl = pageUrl(icon.pageUrl);
67 const faviconUrl = imageUrl(icon.faviconUrl);
68 const faviconUrlDark = imageUrl(icon.faviconUrlDark);
69 if (resolvedPageUrl) {
70 return {
71 _tag: "website",
72 pageUrl: resolvedPageUrl,
73 ...(faviconUrl ? { faviconUrl } : {}),
74 ...(faviconUrlDark ? { faviconUrlDark } : {}),
75 };
76 }
77 }
78 if (icon?._tag === "native-app") {
79 const app = nativeAppReference(icon.app);
80 if (app) return { _tag: "native-app", app };
81 }
82 if (icon?._tag === "themed-logo") {
83 const logoUrl = imageUrl(icon.logoUrl);
84 const logoUrlDark = imageUrl(icon.logoUrlDark);
85 if (logoUrl) {
86 return {
87 _tag: "themed-logo",
88 logoUrl,
89 ...(logoUrlDark ? { logoUrlDark } : {}),
90 };
91 }
92 }
93 return undefined;
95​
96function activitySource(value: unknown): ToolActivitySource | undefined {
97 const source = asRecord(value);
98 const key = trimmedString(source?.key, 512);
99 const name = trimmedString(source?.name, 160);
100 const kind = source?.kind;
101 if (!key || !name || (kind !== "browser" && kind !== "computer" && kind !== "integration")) {
102 return undefined;
103 }
104 const icon = activityIcon(source?.icon);
105 return { key, name, kind, ...(icon ? { icon } : {}) };
107​
108export function extractToolActivityPresentation(
109 payloadValue: unknown,
110): ExtractedToolActivityPresentation {
111 const payload = asRecord(payloadValue);
112 const toolSurface =
113 payload?.toolSurface === "browser" || payload?.toolSurface === "computer"
114 ? payload.toolSurface
115 : undefined;
116 const toolIcon = activityIcon(payload?.toolIcon);
117 const toolSource = activitySource(payload?.toolSource);
118 return {
119 ...(toolSurface ? { toolSurface } : {}),
120 ...(toolIcon ? { toolIcon } : {}),
121 ...(toolSource ? { toolSource } : {}),
122 };