Comp AI CRM is an open source, CRM designed for AI agents. Agentic-first CRM.
TypeScript
feat(companies): resolve a favicon from the domain
grimd8730c2ba251
diff --git a/apps/agent/agent/lib/brand-mapping.ts b/apps/agent/agent/lib/brand-mapping.tsindex 292b36d3..91812206 100644--- a/apps/agent/agent/lib/brand-mapping.ts+++ b/apps/agent/agent/lib/brand-mapping.ts@@ -5,9 +5,12 @@ import type { Brand } from "./context-dev";* What the agent is allowed to change about a company.** Every field Context.dev returns is nullable, and a human's typed-in value- * always outranks a guess: this only ever *fills gaps*. Nothing here overwrites- * something that is already set, which is why every assignment goes through- * `fill`.+ * always outranks a guess: this only ever *fills gaps*, which is why almost+ * every assignment goes through `fill`.+ *+ * The two exceptions are `name` and `iconUrl`, and both are exceptions for the+ * same reason — their existing value was written by us rather than by a person,+ * so there is nobody to overrule. Each is marked at the assignment.*/export type BrandUpdate = Prisma.CompanyUpdateInput;@@ -143,9 +146,9 @@ function clean(value: string | null | undefined): string | null {/*** Builds the Prisma update for a brand lookup.*- * Returns only the fields that are currently empty and that the lookup actually- * found, so re-enriching a company a rep has been editing does not undo their- * work.+ * Returns the fields the lookup actually found that are currently empty — plus+ * the two the agent owns outright — so re-enriching a company a rep has been+ * editing does not undo their work.*/export function brandToUpdate(brand: Brand,@@ -173,7 +176,16 @@ export function brandToUpdate(fill("logoUrl", pickLogo(brand.logos, "logo", "light"));fill("logoDarkUrl", pickLogo(brand.logos, "logo", "dark"));- fill("iconUrl", pickIcon(brand.logos)?.url ?? null);++ // The second non-null field the agent may replace, and for the same reason+ // as the name: `iconUrl` has another writer. The API derives a favicon from+ // the domain so a new company is not a grey square while we work, and this+ // icon is the curated one — `fill` would see a non-null value and skip,+ // leaving the stand-in in place permanently. No human can set `iconUrl`+ // (it is not in `companyUpdateInput`), so there is nobody to overrule.+ const icon = pickIcon(brand.logos)?.url ?? null;+ if (icon) update.iconUrl = icon;+fill("iconDarkUrl", pickLogo(brand.logos, "icon", "dark"));fill("iconTone", iconTone(brand.logos));diff --git a/apps/api/src/companies/companies.module.ts b/apps/api/src/companies/companies.module.tsindex 236b68e9..38745d46 100644--- a/apps/api/src/companies/companies.module.ts+++ b/apps/api/src/companies/companies.module.ts@@ -4,10 +4,16 @@ import { TrpcModule } from "../trpc/trpc.module";import { CompaniesRouter } from "./companies.router";import { CompaniesService } from "./companies.service";import { CompanyDirectoryService } from "./company-directory.service";+import { FaviconService } from "./favicon.service";@Module({imports: [TrpcModule, AgentModule],- providers: [CompaniesService, CompanyDirectoryService, CompaniesRouter],- exports: [CompaniesService, CompanyDirectoryService],+ providers: [+ CompaniesService,+ CompanyDirectoryService,+ CompaniesRouter,+ FaviconService,+ ],+ exports: [CompaniesService, CompanyDirectoryService, FaviconService],})export class CompaniesModule {}diff --git a/apps/api/src/companies/companies.service.ts b/apps/api/src/companies/companies.service.tsindex acf918ff..18fec888 100644--- a/apps/api/src/companies/companies.service.ts+++ b/apps/api/src/companies/companies.service.ts@@ -32,6 +32,7 @@ import type {CompanyUpdateInput,} from "./companies.contracts";import { normalizeDomain } from "./domain";+import { FaviconService } from "./favicon.service";const OWNER_SELECT = {id: true,@@ -106,6 +107,7 @@ export class CompaniesService {@InjectDatabase() private readonly db: Db,private readonly agent: AgentTriggerService,private readonly queue: AgentQueueService,+ private readonly favicon: FaviconService,) {}async list(input: CompanyListInput): Promise<ListResult<CompanyRow>> {@@ -321,6 +323,11 @@ export class CompaniesService {// agent's call.await this.agent.companyCreated(company.id);+ // Not awaited, for the same reason: the icon is worth a second or two of+ // somebody else's origin being slow, and the form is not. The list polls+ // while the row is enriching, so it lands without a reload.+ void this.favicon.backfill(company.id, company.domain);+return company;}@@ -367,6 +374,11 @@ export class CompaniesService {if (current && current.domain !== domain) {data.enrichmentStatus = "PENDING";data.enrichmentError = null;+ // The icon is one of the things that was about a different company,+ // and it is the one a rep can see. Cleared so the row shows a+ // placeholder until the new domain answers, rather than confidently+ // showing the old company's mark.+ data.iconUrl = null;}}@@ -382,6 +394,7 @@ export class CompaniesService {id,"Domain changed — anything we knew was about a different company",);+ void this.favicon.backfill(id, updated.domain);}return updated;diff --git a/apps/api/src/companies/favicon.service.ts b/apps/api/src/companies/favicon.service.tsnew file mode 100644index 00000000..51f14566--- /dev/null+++ b/apps/api/src/companies/favicon.service.ts@@ -0,0 +1,58 @@+import type { Db } from "@crm/db";+import { resolveFavicon } from "@crm/db/favicon";+import { Injectable, Logger } from "@nestjs/common";+import { InjectDatabase } from "../database/database.constants";++/**+ * Stores the domain's own favicon as a stand-in icon.+ *+ * The resolving is in `@crm/db/favicon`, shared with the seed. This is only the+ * write, and the write is the part with a rule in it: the agent's Context.dev+ * icon is the curated one and always wins, so ours is never allowed to replace+ * it, and the agent is free to replace ours (see `brandToUpdate`).+ *+ * Sitting in the API at all is a deliberate exception to `docs/api.md`: there+ * is nothing here to decide. No vendor, no key, no matching, no scoring — so+ * there is no second judgement that can drift from the agent's, which is the+ * failure that rule exists to prevent.+ */+@Injectable()+export class FaviconService {+ private readonly logger = new Logger(FaviconService.name);++ constructor(@InjectDatabase() private readonly db: Db) {}++ /**+ * Resolve and store, without ever throwing at the caller.+ *+ * Callers fire this and move on, so a rejected promise here would be an+ * unhandled one. A company with no icon is the state we are already in.+ */+ async backfill(companyId: string, domain: string | null): Promise<boolean> {+ try {+ const iconUrl = await resolveFavicon(domain);+ if (!iconUrl) return false;++ // `iconUrl: null` in the filter, so an icon the agent found while we+ // were fetching is never overwritten by ours.+ const { count } = await this.db.company.updateMany({+ where: { id: companyId, iconUrl: null },+ data: { iconUrl },+ });++ if (count > 0) {+ this.logger.log({ message: "Favicon resolved", companyId, iconUrl });+ }++ return count > 0;+ } catch (error) {+ this.logger.debug({+ message: "Favicon lookup failed",+ companyId,+ domain,+ error: error instanceof Error ? error.message : String(error),+ });+ return false;+ }+ }+}diff --git a/packages/db/package.json b/packages/db/package.jsonindex f41fe246..d8f71753 100644--- a/packages/db/package.json+++ b/packages/db/package.json@@ -6,7 +6,8 @@"exports": {".": "./src/index.ts","./client": "./src/client.ts",- "./enums": "./src/generated/prisma/enums.ts"+ "./enums": "./src/generated/prisma/enums.ts",+ "./favicon": "./src/favicon.ts"},"scripts": {"build": "prisma generate",diff --git a/packages/db/prisma/seed.ts b/packages/db/prisma/seed.tsindex 0e9952a8..d067482e 100644--- a/packages/db/prisma/seed.ts+++ b/packages/db/prisma/seed.ts@@ -1,4 +1,5 @@import { db } from "../src/client";+import { resolveFavicon } from "../src/favicon";import { ActivityType, DealStage } from "../src/generated/prisma/enums";/**@@ -401,12 +402,49 @@ async function seedCompanies(createdAt: daysFromNow(-integer(30, 400), 12),},update: {},- select: { id: true, name: true, domain: true },+ select: { id: true, name: true, domain: true, iconUrl: true },});companies.push({ ...row, domain: row.domain ?? company.domain });}- return companies;+ await seedIcons(companies);++ return companies.map(({ iconUrl: _, ...company }) => company);+}++/**+ * The favicon each seeded company serves, so a fresh clone is a list of real+ * companies rather than a column of grey squares.+ *+ * Resolved here rather than hard-coded because these URLs rot — half of them+ * carry a deploy hash — and a dead `<img>` is worse than the initials it+ * replaced. Icons the agent has since found are left alone, and a machine with+ * no network simply seeds without them: every failure is a `null`, so the seed+ * cannot be broken by somebody else's origin being down.+ */+async function seedIcons(+ companies: { id: string; domain: string | null; iconUrl: string | null }[],+): Promise<void> {+ const missing = companies.filter(+ (company) => company.iconUrl === null && company.domain,+ );+ if (missing.length === 0) return;++ // Sequential: this walks fifteen origins that did not ask to be walked, and+ // all at once is a burst that looks like a scan.+ let resolved = 0;+ for (const company of missing) {+ const iconUrl = await resolveFavicon(company.domain);+ if (!iconUrl) continue;++ await db.company.updateMany({+ where: { id: company.id, iconUrl: null },+ data: { iconUrl },+ });+ resolved += 1;+ }++ console.log(`Resolved ${resolved} of ${missing.length} company icons.`);}type SeededContact = { id: string; companyId: string };diff --git a/packages/db/src/favicon.ts b/packages/db/src/favicon.tsnew file mode 100644index 00000000..08e9ade6--- /dev/null+++ b/packages/db/src/favicon.ts@@ -0,0 +1,103 @@+const TIMEOUT_MS = 5_000;+const MAX_HTML_BYTES = 512_000;++const ICON_REL = /^(shortcut )?icon$|^apple-touch-icon(-precomposed)?$/i;++function iconsFromHtml(html: string, base: URL): string[] {+ const found: { href: string; size: number }[] = [];++ for (const [tag] of html.matchAll(/<link\b[^>]*>/gi)) {+ const rel = /\brel\s*=\s*["']?([^"'>]+)/i.exec(tag)?.[1]?.trim();+ if (!rel || !ICON_REL.test(rel)) continue;++ const href = /\bhref\s*=\s*["']?([^"'\s>]+)/i.exec(tag)?.[1]?.trim();+ if (!href) continue;++ // "32x32" → 32. Unsized icons sort last rather than first: a declared+ // size is usually a real raster, and an undeclared one is often a 16px+ // .ico that looks like mud beside the artwork in the next row.+ const sizes = /\bsizes\s*=\s*["']?(\d+)/i.exec(tag)?.[1];++ try {+ found.push({+ href: new URL(href, base).toString(),+ size: Number(sizes ?? 0),+ });+ } catch {+ // A malformed href is one skipped icon, not a failed resolve.+ }+ }++ return found.sort((a, b) => b.size - a.size).map((icon) => icon.href);+}++async function request(url: string, method: "GET" | "HEAD" = "GET") {+ try {+ return await fetch(url, {+ method,+ signal: AbortSignal.timeout(TIMEOUT_MS),+ redirect: "follow",+ // Some origins serve a different page, or none, to an unknown agent.+ headers: { "user-agent": "Mozilla/5.0 (compatible; CRM/1.0)" },+ });+ } catch {+ return null;+ }+}++/** The page's HTML and the URL it ended up at, after redirects. */+async function readPage(url: URL): Promise<{ body: string; url: URL } | null> {+ const response = await request(url.toString());+ if (!response?.ok) return null;++ if (!(response.headers.get("content-type") ?? "").includes("html")) {+ return null;+ }++ const body = (await response.text()).slice(0, MAX_HTML_BYTES);+ return { body, url: new URL(response.url || url.toString()) };+}++/**+ * Whether a URL actually serves an image.+ *+ * Checked because a great many sites answer `/favicon.ico` with a 200 and an+ * HTML error page, and storing that URL puts a broken image in every table row+ * — the one outcome worse than the placeholder it replaced.+ */+async function servesImage(url: string): Promise<boolean> {+ const response = await request(url, "HEAD");+ if (!response?.ok) return false;++ return (response.headers.get("content-type") ?? "").startsWith("image/");+}++/**+ * The best icon a domain advertises, or `null`.+ *+ * Never throws and never guesses: `null` is a real answer and the safe one,+ * because a company with no icon is the state we were already in.+ */+export async function resolveFavicon(+ domain: string | null | undefined,+): Promise<string | null> {+ if (!domain) return null;++ try {+ const base = new URL(`https://${domain}`);+ const page = await readPage(base);++ const candidates = page ? iconsFromHtml(page.body, page.url) : [];+ // Every site is entitled to this path whether it advertises it or not,+ // so it is the last candidate rather than a separate branch.+ candidates.push(new URL("/favicon.ico", page?.url ?? base).toString());++ for (const candidate of candidates) {+ if (await servesImage(candidate)) return candidate;+ }+ } catch {+ // A domain that does not parse is a null, like one that does not answer.+ }++ return null;+}