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

toolPresentation.ts

155 lines5.1 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​
14/**
15 * Keeps the bounded resource metadata that providers attach to an otherwise
16 * generic tool row. MCP rows retain their existing focused payload shape.
17 */
18export function extractToolActivityData(payloadValue: unknown): unknown {
19 const payload = asRecord(payloadValue);
20 const data = asRecord(payload?.data);
21 if (!data) return undefined;
22 if (payload?.itemType === "mcp_tool_call") {
23 return typeof data.toolName === "string" ? (data.item ?? data) : data.item;
24 }
25 return "resource" in data ? data : undefined;
27​
28type ToolActivityDataEntry = { readonly itemType?: string; readonly toolData?: unknown };
29​
30/** Checks expandability without serializing payloads on collapsed rows. */
31export function hasToolActivityData(entry: ToolActivityDataEntry): boolean {
32 return entry.itemType === "mcp_tool_call"
33 ? entry.toolData !== undefined
34 : asRecord(asRecord(entry.toolData)?.resource) !== undefined;
36​
37/** Shared expanded detail for MCP calls and bounded generic resource metadata. */
38export function toolActivityDataBody(entry: ToolActivityDataEntry): string | undefined {
39 if (!hasToolActivityData(entry)) return undefined;
40 if (entry.itemType === "mcp_tool_call") {
41 return `MCP call\n${JSON.stringify(entry.toolData, null, 2)}`;
42 }
43 return `Resource\n${JSON.stringify(asRecord(entry.toolData)?.resource, null, 2)}`;
45​
46function asRecord(value: unknown): Record<string, unknown> | undefined {
47 return value !== null && typeof value === "object" && !Array.isArray(value)
48 ? (value as Record<string, unknown>)
49 : undefined;
51​
52function trimmedString(value: unknown, maxLength: number): string | undefined {
53 if (typeof value !== "string") return undefined;
54 const trimmed = value.trim();
55 return trimmed && trimmed.length <= maxLength ? trimmed : undefined;
57​
58function imageUrl(value: unknown): string | undefined {
59 const raw = trimmedString(value, 4096);
60 if (!raw) return undefined;
61 try {
62 const url = new URL(raw);
63 return url.protocol === "http:" || url.protocol === "https:" || url.protocol === "data:"
64 ? url.href
65 : undefined;
66 } catch {
67 return undefined;
68 }
70​
71function pageUrl(value: unknown): string | undefined {
72 const raw = trimmedString(value, 4096);
73 if (!raw) return undefined;
74 try {
75 const url = new URL(raw);
76 return url.protocol === "http:" || url.protocol === "https:" ? url.href : undefined;
77 } catch {
78 return undefined;
79 }
81​
82function nativeAppReference(value: unknown): ToolActivityNativeAppReference | undefined {
83 const app = asRecord(value);
84 const appId = trimmedString(app?.appId, 512);
85 if (app?._tag === "app-id" && appId && /^[A-Za-z0-9._-]+$/u.test(appId)) {
86 return { _tag: "app-id", appId };
87 }
88 const displayName = trimmedString(app?.displayName, 160);
89 if (app?._tag === "display-name" && displayName) {
90 return { _tag: "display-name", displayName };
91 }
92 return undefined;
94​
95function activityIcon(value: unknown): ToolActivityIcon | undefined {
96 const icon = asRecord(value);
97 if (icon?._tag === "website") {
98 const resolvedPageUrl = pageUrl(icon.pageUrl);
99 const faviconUrl = imageUrl(icon.faviconUrl);
100 const faviconUrlDark = imageUrl(icon.faviconUrlDark);
101 if (resolvedPageUrl) {
102 return {
103 _tag: "website",
104 pageUrl: resolvedPageUrl,
105 ...(faviconUrl ? { faviconUrl } : {}),
106 ...(faviconUrlDark ? { faviconUrlDark } : {}),
107 };
108 }
109 }
110 if (icon?._tag === "native-app") {
111 const app = nativeAppReference(icon.app);
112 if (app) return { _tag: "native-app", app };
113 }
114 if (icon?._tag === "themed-logo") {
115 const logoUrl = imageUrl(icon.logoUrl);
116 const logoUrlDark = imageUrl(icon.logoUrlDark);
117 if (logoUrl) {
118 return {
119 _tag: "themed-logo",
120 logoUrl,
121 ...(logoUrlDark ? { logoUrlDark } : {}),
122 };
123 }
124 }
125 return undefined;
127​
128function activitySource(value: unknown): ToolActivitySource | undefined {
129 const source = asRecord(value);
130 const key = trimmedString(source?.key, 512);
131 const name = trimmedString(source?.name, 160);
132 const kind = source?.kind;
133 if (!key || !name || (kind !== "browser" && kind !== "computer" && kind !== "integration")) {
134 return undefined;
135 }
136 const icon = activityIcon(source?.icon);
137 return { key, name, kind, ...(icon ? { icon } : {}) };
139​
140export function extractToolActivityPresentation(
141 payloadValue: unknown,
142): ExtractedToolActivityPresentation {
143 const payload = asRecord(payloadValue);
144 const toolSurface =
145 payload?.toolSurface === "browser" || payload?.toolSurface === "computer"
146 ? payload.toolSurface
147 : undefined;
148 const toolIcon = activityIcon(payload?.toolIcon);
149 const toolSource = activitySource(payload?.toolSource);
150 return {
151 ...(toolSurface ? { toolSurface } : {}),
152 ...(toolIcon ? { toolIcon } : {}),
153 ...(toolSource ? { toolSource } : {}),
154 };