|
| 1 | +export type AIThreadBrowserActivityStatus = "completed" | "failed" | "running"; |
| 2 | + |
| 3 | +interface AIThreadBrowserActivityCall { |
| 4 | + args: unknown; |
| 5 | + method: string; |
| 6 | + result?: unknown; |
| 7 | +} |
| 8 | + |
| 9 | +interface AIThreadBrowserDestination { |
| 10 | + site: string; |
| 11 | + title?: string; |
| 12 | + url: string; |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * Turns low-level CDP traffic into one user-facing destination summary. |
| 17 | + * |
| 18 | + * Only explicit navigations count. Protocol setup such as `attachToTarget` |
| 19 | + * carries no useful destination, and successful traffic without a destination |
| 20 | + * is omitted instead of rendering a generic "Browsed web" row. |
| 21 | + */ |
| 22 | +export function summarizeAIThreadBrowserActivity( |
| 23 | + calls: readonly AIThreadBrowserActivityCall[], |
| 24 | + status: AIThreadBrowserActivityStatus, |
| 25 | +): string | undefined { |
| 26 | + const destinations = getBrowserDestinations(calls); |
| 27 | + const sites = unique(destinations.map((destination) => destination.site)); |
| 28 | + |
| 29 | + if (status === "failed") { |
| 30 | + if (sites.length === 1) { |
| 31 | + return `Browser failed on ${sites[0]}`; |
| 32 | + } |
| 33 | + return sites.length > 1 ? `Browser failed across ${sites.length} sites` : "Browser failed"; |
| 34 | + } |
| 35 | + |
| 36 | + if (sites.length === 0) { |
| 37 | + return undefined; |
| 38 | + } |
| 39 | + |
| 40 | + const verb = status === "running" ? "Browsing" : "Browsed"; |
| 41 | + if (sites.length === 1) { |
| 42 | + const pageCount = unique(destinations.map((destination) => destination.url)).length; |
| 43 | + const pageTitles = unique( |
| 44 | + destinations |
| 45 | + .map((destination) => destination.title) |
| 46 | + .filter((title): title is string => Boolean(title)), |
| 47 | + ); |
| 48 | + if (pageCount === 1 && pageTitles.length === 1 && pageTitles[0] !== sites[0]) { |
| 49 | + return `${verb} ${sites[0]} — ${pageTitles[0]}`; |
| 50 | + } |
| 51 | + return pageCount > 1 ? `${verb} ${pageCount} pages on ${sites[0]}` : `${verb} ${sites[0]}`; |
| 52 | + } |
| 53 | + if (sites.length === 2) { |
| 54 | + return `${verb} ${sites[0]} and ${sites[1]}`; |
| 55 | + } |
| 56 | + return `${verb} ${sites.length} sites`; |
| 57 | +} |
| 58 | + |
| 59 | +function getBrowserDestinations(calls: readonly AIThreadBrowserActivityCall[]) { |
| 60 | + const destinations = calls.flatMap((call) => { |
| 61 | + if (call.method !== "send") { |
| 62 | + return []; |
| 63 | + } |
| 64 | + |
| 65 | + const args = asRecord(call.args); |
| 66 | + const method = getString(args.method); |
| 67 | + if (method !== "Target.createTarget" && method !== "Page.navigate") { |
| 68 | + return []; |
| 69 | + } |
| 70 | + |
| 71 | + const url = getString(asRecord(args.params).url); |
| 72 | + const destination = url ? getBrowserDestination(url) : undefined; |
| 73 | + return destination ? [destination] : []; |
| 74 | + }); |
| 75 | + |
| 76 | + if (unique(destinations.map((destination) => destination.url)).length === 1) { |
| 77 | + const title = getLastEvaluatedPageTitle(calls); |
| 78 | + if (title) { |
| 79 | + for (const destination of destinations) { |
| 80 | + destination.title ??= title; |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return destinations; |
| 86 | +} |
| 87 | + |
| 88 | +function getLastEvaluatedPageTitle(calls: readonly AIThreadBrowserActivityCall[]) { |
| 89 | + for (let index = calls.length - 1; index >= 0; index -= 1) { |
| 90 | + const call = calls[index]; |
| 91 | + const args = asRecord(call?.args); |
| 92 | + if (call?.method !== "send" || getString(args.method) !== "Runtime.evaluate") { |
| 93 | + continue; |
| 94 | + } |
| 95 | + |
| 96 | + const params = asRecord(args.params); |
| 97 | + const expression = getString(params.expression); |
| 98 | + if (!expression?.includes("document.title")) { |
| 99 | + continue; |
| 100 | + } |
| 101 | + |
| 102 | + const value = asRecord(asRecord(call.result).result).value; |
| 103 | + const title = |
| 104 | + typeof value === "string" |
| 105 | + ? formatPageTitle(value) |
| 106 | + : formatPageTitle(getString(asRecord(value).title)); |
| 107 | + if (title) { |
| 108 | + return title; |
| 109 | + } |
| 110 | + } |
| 111 | + return undefined; |
| 112 | +} |
| 113 | + |
| 114 | +function getBrowserDestination(value: string): AIThreadBrowserDestination | undefined { |
| 115 | + try { |
| 116 | + const url = new URL(value); |
| 117 | + if (url.protocol !== "http:" && url.protocol !== "https:") { |
| 118 | + return undefined; |
| 119 | + } |
| 120 | + |
| 121 | + url.hash = ""; |
| 122 | + const hostname = url.hostname.toLowerCase().replace(/^www\./, ""); |
| 123 | + return { |
| 124 | + site: formatBrowserSite(hostname), |
| 125 | + url: url.toString(), |
| 126 | + }; |
| 127 | + } catch { |
| 128 | + return undefined; |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +function formatPageTitle(value: string | undefined) { |
| 133 | + const title = value?.replace(/\s+/g, " ").trim(); |
| 134 | + if (!title) { |
| 135 | + return undefined; |
| 136 | + } |
| 137 | + return title.length > 80 ? `${title.slice(0, 79).trimEnd()}…` : title; |
| 138 | +} |
| 139 | + |
| 140 | +function formatBrowserSite(hostname: string) { |
| 141 | + if (hostname === "instructure.com" || hostname.endsWith(".instructure.com")) { |
| 142 | + const tenant = hostname.slice(0, -".instructure.com".length).split(".").filter(Boolean).at(-1); |
| 143 | + return tenant && tenant !== "canvas" ? `${formatSiteWord(tenant)} Canvas` : "Canvas"; |
| 144 | + } |
| 145 | + return hostname; |
| 146 | +} |
| 147 | + |
| 148 | +function formatSiteWord(value: string) { |
| 149 | + return value.length <= 5 |
| 150 | + ? value.toUpperCase() |
| 151 | + : `${value.charAt(0).toUpperCase()}${value.slice(1)}`; |
| 152 | +} |
| 153 | + |
| 154 | +function unique<T>(values: readonly T[]) { |
| 155 | + return [...new Set(values)]; |
| 156 | +} |
| 157 | + |
| 158 | +function asRecord(value: unknown): Record<string, unknown> { |
| 159 | + return value && typeof value === "object" && !Array.isArray(value) |
| 160 | + ? (value as Record<string, unknown>) |
| 161 | + : {}; |
| 162 | +} |
| 163 | + |
| 164 | +function getString(value: unknown) { |
| 165 | + return typeof value === "string" && value.trim() ? value.trim() : undefined; |
| 166 | +} |
0 commit comments