Open History to see the last commit
1import * as NodeCrypto from "node:crypto";
2import type {
3 ToolActivityIcon,
4 ToolActivityNativeAppReference,
5 ToolActivitySource,
6} from "@t3tools/contracts";
7import type * as EffectCodexSchema from "effect-codex-app-server/schema";
8
9import {
10 mcpToolPresentation as integrationToolPresentation,
11 normalizeMcpHttpUrl as normalizedHttpUrl,
12 normalizeMcpText as normalizedDisplayName,
13} from "./McpToolPresentation.ts";
14
15type CodexLifecycleItem = EffectCodexSchema.V2ItemCompletedNotification["item"];
16
17function asUnknownRecord(value: unknown): Record<string, unknown> | undefined {
18 return value && typeof value === "object" ? (value as Record<string, unknown>) : undefined;
19}
20
21function normalizedImageUrl(value: unknown): string | undefined {
22 if (typeof value !== "string" || value.length > 4096) return undefined;
23 try {
24 const url = new URL(value);
25 return url.protocol === "http:" || url.protocol === "https:" || url.protocol === "data:"
26 ? url.href
27 : undefined;
28 } catch {
29 return undefined;
30 }
31}
32
33function normalizedAppId(value: unknown): string | undefined {
34 if (typeof value !== "string") return undefined;
35 const appId = value.trim();
36 return appId.length > 0 && appId.length <= 512 && /^[A-Za-z0-9._-]+$/u.test(appId)
37 ? appId
38 : undefined;
39}
40
41function normalizedSourceKeyPart(value: string): string {
42 return value.trim().toLowerCase();
43}
44
45function nativeAppSourceKey(appId: string): string {
46 const key = `native-app:${appId.toLowerCase()}`;
47 if (key.length <= 512) return key;
48 const digest = NodeCrypto.createHash("sha256").update(key).digest("hex");
49 return `${key.slice(0, 512 - digest.length - 1)}:${digest}`;
50}
51
52function browserDisplayName(value: unknown): string | undefined {
53 const normalized = normalizedDisplayName(value)?.toLowerCase();
54 if (!normalized) return undefined;
55 if (normalized.includes("chrome") || normalized === "chromium") return "Chrome";
56 if (normalized.includes("edge")) return "Microsoft Edge";
57 if (normalized.includes("firefox")) return "Firefox";
58 if (normalized.includes("safari")) return "Safari";
59 if (normalized.includes("arc")) return "Arc";
60 if (normalized === "iab" || normalized.includes("in-app")) return "Browser";
61 return normalizedDisplayName(value);
62}
63
64function browserNativeAppReference(name: string): ToolActivityNativeAppReference | undefined {
65 switch (name) {
66 case "Chrome":
67 return { _tag: "display-name", displayName: "Google Chrome" };
68 case "Microsoft Edge":
69 case "Firefox":
70 case "Safari":
71 case "Arc":
72 return { _tag: "display-name", displayName: name };
73 default:
74 return undefined;
75 }
76}
77
78function appDisplayNameFromId(appId: string): string | undefined {
79 const knownNames: Readonly<Record<string, string>> = {
80 "com.apple.finder": "Finder",
81 "com.apple.safari": "Safari",
82 "com.google.chrome": "Chrome",
83 "com.microsoft.edgemac": "Microsoft Edge",
84 "org.mozilla.firefox": "Firefox",
85 "company.thebrowser.browser": "Arc",
86 };
87 return knownNames[appId.toLowerCase()];
88}
89
90function nativeAppReference(value: unknown): ToolActivityNativeAppReference | undefined {
91 const app = asUnknownRecord(value);
92 if (app?.kind === "appId") {
93 const appId = normalizedAppId(app.appId);
94 return appId ? { _tag: "app-id", appId } : undefined;
95 }
96 if (app?.kind === "displayName") {
97 const displayName = normalizedDisplayName(app.displayName);
98 return displayName ? { _tag: "display-name", displayName } : undefined;
99 }
100 return undefined;
101}
102
103function themedLogoIcon(
104 ...records: ReadonlyArray<Record<string, unknown> | undefined>
105): ToolActivityIcon | undefined {
106 for (const record of records) {
107 const logoUrl = normalizedImageUrl(record?.logoUrl);
108 if (!logoUrl) continue;
109 const logoUrlDark = normalizedImageUrl(record?.logoUrlDark ?? record?.logoDarkUrl);
110 return {
111 _tag: "themed-logo",
112 logoUrl,
113 ...(logoUrlDark ? { logoUrlDark } : {}),
114 };
115 }
116 return undefined;
117}
118
119export interface McpToolPresentation {
120 readonly title?: string;
121 readonly toolSurface?: "browser" | "computer";
122 readonly toolIcon?: ToolActivityIcon;
123 readonly toolSource?: ToolActivitySource;
124}
125
126export function mcpToolPresentation(
127 item: Extract<CodexLifecycleItem, { readonly type: "mcpToolCall" }>,
128): McpToolPresentation {
129 const result = asUnknownRecord(item.result);
130 const metadata = asUnknownRecord(result?._meta);
131 const surface = asUnknownRecord(metadata?.["codex/toolSurface"]);
132 const sourceMetadata = asUnknownRecord(metadata?.source);
133 const appContext = asUnknownRecord(item.appContext);
134 const sourceLogo = themedLogoIcon(surface, sourceMetadata, appContext);
135 if (surface?.kind === "browserUse") {
136 const screenshot = asUnknownRecord(surface.screenshot);
137 const browserUse = asUnknownRecord(metadata?.browser_use);
138 const openTabs = Array.isArray(surface.openTabs) ? surface.openTabs : [];
139 const latestOpenTab = openTabs
140 .toReversed()
141 .map(asUnknownRecord)
142 .find((tab) => normalizedHttpUrl(tab?.url) !== undefined);
143 const selectedPage = [
144 { record: screenshot, url: screenshot?.pageUrl },
145 { record: browserUse, url: browserUse?.url },
146 { record: latestOpenTab, url: latestOpenTab?.url },
147 ]
148 .map((candidate) => ({ ...candidate, pageUrl: normalizedHttpUrl(candidate.url) }))
149 .find((candidate) => candidate.pageUrl !== undefined);
150 const pageUrl = selectedPage?.pageUrl;
151 const faviconUrl = normalizedImageUrl(
152 selectedPage?.record?.faviconUrl ?? selectedPage?.record?.favIconUrl,
153 );
154 const faviconUrlDark = normalizedImageUrl(
155 selectedPage?.record?.faviconUrlDark ?? selectedPage?.record?.favIconUrlDark,
156 );
157 const name =
158 browserDisplayName(appContext?.appName) ??
159 browserDisplayName(surface.browserFamily) ??
160 browserDisplayName(surface.backend) ??
161 "Browser";
162 const nativeBrowserIcon = browserNativeAppReference(name);
163 const sourceIcon =
164 sourceLogo ??
165 (nativeBrowserIcon ? ({ _tag: "native-app", app: nativeBrowserIcon } as const) : undefined);
166 const sourceKeyPart = normalizedSourceKeyPart(name) || "browser";
167 return {
168 toolSurface: "browser",
169 ...(pageUrl
170 ? {
171 toolIcon: {
172 _tag: "website",
173 pageUrl,
174 ...(faviconUrl ? { faviconUrl } : {}),
175 ...(faviconUrlDark ? { faviconUrlDark } : {}),
176 } as const,
177 }
178 : {}),
179 toolSource: {
180 key: `browser-use:${sourceKeyPart}`,
181 name,
182 kind: name === "Browser" ? "browser" : "integration",
183 ...(sourceIcon ? { icon: sourceIcon } : {}),
184 },
185 };
186 }
187 if (surface?.kind === "computerUse") {
188 const app = nativeAppReference(surface.app);
189 const args = asUnknownRecord(item.arguments);
190 const argumentAppName =
191 normalizedDisplayName(args?.appName) ??
192 normalizedDisplayName(args?.application) ??
193 normalizedDisplayName(typeof args?.app === "string" ? args.app : undefined);
194 const name =
195 normalizedDisplayName(appContext?.appName) ??
196 argumentAppName ??
197 (app?._tag === "display-name" ? app.displayName : undefined) ??
198 (app?._tag === "app-id" ? appDisplayNameFromId(app.appId) : undefined) ??
199 "Computer Use";
200 const sourceIcon = sourceLogo ?? (app ? ({ _tag: "native-app", app } as const) : undefined);
201 const sourceKey = app
202 ? app._tag === "app-id"
203 ? nativeAppSourceKey(app.appId)
204 : `native-app-name:${normalizedSourceKeyPart(app.displayName)}`
205 : "computer-use";
206 return {
207 toolSurface: "computer",
208 ...(app ? { toolIcon: { _tag: "native-app", app } as const } : {}),
209 toolSource: {
210 key: sourceKey,
211 name,
212 kind: "computer",
213 ...(sourceIcon ? { icon: sourceIcon } : {}),
214 },
215 };
216 }
217
218 return integrationToolPresentation({
219 serverName: appContext?.connectorId ?? item.server,
220 toolName: item.tool,
221 title: appContext?.actionName,
222 serverDisplayName: appContext?.appName,
223 source: sourceMetadata,
224 iconUrl: sourceLogo?._tag === "themed-logo" ? sourceLogo.logoUrl : undefined,
225 iconUrlDark: sourceLogo?._tag === "themed-logo" ? sourceLogo.logoUrlDark : undefined,
226 });
227}