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

toolPresentation.ts

131 lines4.1 KB

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