Comp AI CRM is an open source, CRM designed for AI agents. Agentic-first CRM.
TypeScript
feat(brand-mapping): introduce fillable function and enhance brand update logic
Lewis Carhartaad59457baca
diff --git a/apps/agent/agent/lib/brand-mapping.ts b/apps/agent/agent/lib/brand-mapping.tsindex 7c3f426c..1d6994d0 100644--- a/apps/agent/agent/lib/brand-mapping.ts+++ b/apps/agent/agent/lib/brand-mapping.ts@@ -98,6 +98,12 @@ function clean(value: string | null | undefined): string | null {return trimmed ? trimmed : null;}+function fillable(key: string, current: CompanySnapshot): boolean {+ if (key === "iconUrl") return true;+ if (key === "name") return current.nameIsPlaceholder;+ return current[key as keyof CompanySnapshot] === null;+}+export function brandToUpdate(brand: Brand,current: CompanySnapshot,@@ -108,23 +114,19 @@ export function brandToUpdate(key: K,value: string | null,) => {- if (value && current[key as keyof CompanySnapshot] === null) {+ if (value && fillable(key, current)) {(update as Record<string, unknown>)[key] = value;}};- const title = clean(brand.title);- if (title && current.nameIsPlaceholder) {- update.name = title;- }+ fill("name", clean(brand.title));fill("description", clean(brand.description) ?? clean(brand.slogan));fill("logoUrl", pickLogo(brand.logos, "logo", "light"));fill("logoDarkUrl", pickLogo(brand.logos, "logo", "dark"));- const icon = pickIcon(brand.logos)?.url ?? null;- if (icon) update.iconUrl = icon;+ fill("iconUrl", pickIcon(brand.logos)?.url ?? null);fill("iconDarkUrl", pickLogo(brand.logos, "icon", "dark"));fill("iconTone", iconTone(brand.logos));@@ -156,6 +158,21 @@ export function brandToUpdate(return update;}+export function stillFillable(+ update: BrandUpdate,+ current: CompanySnapshot,+): BrandUpdate {+ const next: BrandUpdate = {};++ for (const [key, value] of Object.entries(update)) {+ if (fillable(key, current)) {+ (next as Record<string, unknown>)[key] = value;+ }+ }++ return next;+}+export function filledFields(update: BrandUpdate): string[] {return Object.keys(update);}diff --git a/apps/agent/agent/lib/brand.ts b/apps/agent/agent/lib/brand.tsindex 48b7667e..c0efd8ba 100644--- a/apps/agent/agent/lib/brand.ts+++ b/apps/agent/agent/lib/brand.ts@@ -1,6 +1,6 @@import { db, EnrichmentStatus } from "@crm/db";import { mirrorBrandImages } from "./brand-images";-import { brandToUpdate, filledFields } from "./brand-mapping";+import { brandToUpdate, filledFields, stillFillable } from "./brand-mapping";import { brandByDomain, contextDevEnabled } from "./context-dev";export type BrandResult = {@@ -50,10 +50,6 @@ export async function runBrand({fresh?: boolean;spend?: Spend;}): Promise<BrandResult> {- if (!contextDevEnabled()) {- return { enriched: false, reason: "Context.dev is not configured." };- }-const company = await db.company.findUnique({where: { id: companyId },select: COMPANY_FIELDS,@@ -61,6 +57,13 @@ export async function runBrand({if (!company) return { enriched: false, reason: "No such company." };+ if (!contextDevEnabled()) {+ const reason =+ "Context.dev is not configured, so there is nowhere to look.";+ await settle(companyId, EnrichmentStatus.SKIPPED, reason);+ return { enriched: false, reason };+ }+if (!company.domain) {await settle(companyId, EnrichmentStatus.SKIPPED, "No domain to look up.");return { enriched: false, reason: "No domain on this company." };@@ -93,32 +96,52 @@ export async function runBrand({};}- const update = brandToUpdate(result.brand, {- ...company,- nameIsPlaceholder: company.name === company.domain,- });- const filled = filledFields(update);+ const update = brandToUpdate(result.brand, snapshot(company));const { mirrored } = await mirrorBrandImages(companyId, update);- await db.$transaction([- db.company.update({ where: { id: companyId }, data: update }),- db.companyEnrichment.upsert({- where: { companyId },- create: { companyId, raw: result.raw as object },- update: { raw: result.raw as object, fetchedAt: new Date() },- }),- db.company.update({+ const filled = await db.$transaction(async (tx) => {+ const current = await tx.company.findUnique({+ where: { id: companyId },+ select: COMPANY_FIELDS,+ });++ if (!current) return null;++ const data = stillFillable(update, snapshot(current));++ await tx.company.update({where: { id: companyId },data: {+ ...data,enrichmentStatus: EnrichmentStatus.COMPLETE,enrichedAt: new Date(),enrichmentError: null,},- }),- ]);+ });++ await tx.companyEnrichment.upsert({+ where: { companyId },+ create: { companyId, raw: result.raw as object },+ update: { raw: result.raw as object, fetchedAt: new Date() },+ });++ return filledFields(data);+ });++ if (!filled) return { enriched: false, reason: "No such company." };++ return {+ enriched: true,+ filled,+ mirrored: mirrored.filter((slot) => filled.includes(slot)),+ };+}- return { enriched: true, filled, mirrored };+function snapshot<T extends { name: string; domain: string | null }>(+ company: T,+) {+ return { ...company, nameIsPlaceholder: company.name === company.domain };}export function brandOutcome(result: BrandResult): string {diff --git a/apps/agent/agent/lib/dispatch.ts b/apps/agent/agent/lib/dispatch.tsindex 09f7f037..ee0f6bda 100644--- a/apps/agent/agent/lib/dispatch.ts+++ b/apps/agent/agent/lib/dispatch.ts@@ -11,6 +11,7 @@ import {type LeasedTask,noteSession,retireExhausted,+ type TaskSubject,} from "./tasks";export const VISIBLE_BATCH = 60;@@ -18,65 +19,85 @@ export const VISIBLE_CONCURRENCY = 6;export const VISIBLE_LEASE_MS = 2 * 60_000;export const RESEARCH_BATCH = 12;+export const RESEARCH_LEASE_MS = 30 * 60_000;export async function retireAbandoned(): Promise<void> {+ let abandoned: TaskSubject[] = [];+try {- for (const abandoned of await retireExhausted()) {- await settle(- abandoned,- EnrichmentStatus.FAILED,- "Research was attempted several times and never completed.",- );- }- } catch {}+ abandoned = await retireExhausted();+ } catch {+ return;+ }++ for (const task of abandoned) {+ await settle(+ task,+ EnrichmentStatus.FAILED,+ "Research was attempted several times and never completed.",+ ).catch(() => {});+ }}export async function runVisibleLane(): Promise<number> {- const tasks = await claimDue(- VISIBLE_BATCH,- { only: DIRECT_KINDS },- VISIBLE_LEASE_MS,- );+ let handled = 0;- if (tasks.length === 0) return 0;+ while (handled < VISIBLE_BATCH) {+ const tasks = await claimDue(+ Math.min(VISIBLE_CONCURRENCY, VISIBLE_BATCH - handled),+ { only: DIRECT_KINDS },+ VISIBLE_LEASE_MS,+ );- await runLimited(VISIBLE_CONCURRENCY, tasks, async (task) => {- try {- if (task.kind === "brand" && task.companyId) {- const result = await runBrand({ companyId: task.companyId });- await completeTask(task.id, brandOutcome(result));- return;- }+ if (tasks.length === 0) break;- if (task.kind === "portrait" && task.contactId) {- const portrait = await runPortrait({- contactId: task.contactId,- spend: () => ({ ok: true }),- });-- await completeTask(- task.id,- portrait.stored- ? `Picture stored from ${portrait.source}.`- : (portrait.reason ?? "No picture found."),- );- return;- }+ await runLimited(VISIBLE_CONCURRENCY, tasks, runDirect);+ handled += tasks.length;+ }- await completeTask(task.id, "The record this names is gone.");- } catch (error) {- const reason = error instanceof Error ? error.message : String(error);- await settle(task, EnrichmentStatus.FAILED, reason).catch(() => {});+ return handled;+}++async function runDirect(task: LeasedTask): Promise<void> {+ try {+ if (task.kind === "brand" && task.companyId) {+ const result = await runBrand({ companyId: task.companyId });+ if (result.retryable) return;++ await completeTask(task.id, brandOutcome(result));+ return;}- });- return tasks.length;+ if (task.kind === "portrait" && task.contactId) {+ const portrait = await runPortrait({+ contactId: task.contactId,+ spend: () => ({ ok: true }),+ });++ await completeTask(+ task.id,+ portrait.stored+ ? `Picture stored from ${portrait.source}.`+ : (portrait.reason ?? "No picture found."),+ );+ return;+ }++ await completeTask(task.id, "The record this names is gone.");+ } catch (error) {+ const reason = error instanceof Error ? error.message : String(error);+ await settle(task, EnrichmentStatus.FAILED, reason).catch(() => {});+ }}export async function runResearchLane(start: (task: LeasedTask) => Promise<{ id: string }>,): Promise<number> {- const tasks = await claimDue(RESEARCH_BATCH, { except: DIRECT_KINDS });+ const tasks = await claimDue(+ RESEARCH_BATCH,+ { except: DIRECT_KINDS },+ RESEARCH_LEASE_MS,+ );if (tasks.length === 0) return 0;await Promise.all(diff --git a/apps/agent/agent/lib/pool.ts b/apps/agent/agent/lib/pool.tsindex 0d0ab8ab..93a1ef67 100644--- a/apps/agent/agent/lib/pool.ts+++ b/apps/agent/agent/lib/pool.ts@@ -12,15 +12,25 @@ export function collapsing<A extends unknown[]>(active = run(...args);+ let failure: { error: unknown } | null = null;+try {await active;+ } catch (error) {+ failure = { error };} finally {active = null;}const next = trailing;trailing = null;- if (next) await invoke(...next);++ if (next) {+ const catchUp = invoke(...next);+ await (failure ? catchUp.catch(() => {}) : catchUp);+ }++ if (failure) throw failure.error;};return invoke;@@ -32,18 +42,10 @@ export async function runLimited<T>(run: (item: T) => Promise<void>,): Promise<void> {const width = Math.max(1, Math.min(concurrency, items.length));- let next = 0;+ const queue = items[Symbol.iterator]();const workers = Array.from({ length: width }, async () => {- while (true) {- const index = next++;- if (index >= items.length) return;-- const item = items[index];- if (item === undefined) return;-- await run(item);- }+ for (const item of queue) await run(item);});await Promise.all(workers);diff --git a/apps/agent/test/brand-mapping.spec.ts b/apps/agent/test/brand-mapping.spec.tsindex fdfdf997..a73a9fec 100644--- a/apps/agent/test/brand-mapping.spec.ts+++ b/apps/agent/test/brand-mapping.spec.ts@@ -2,6 +2,7 @@ import { describe, expect, it } from "bun:test";import {brandToUpdate,type CompanySnapshot,+ stillFillable,} from "../agent/lib/brand-mapping";import type { Brand } from "../agent/lib/context-dev";@@ -142,3 +143,50 @@ describe("brandToUpdate", () => {expect(update.description).toBe("Payments, simplified.");});});++describe("stillFillable", () => {+ it("drops a field a rep typed while the lookup was in flight", () => {+ const update = brandToUpdate(+ {+ description: "Payments infrastructure for the internet.",+ phone: "+1 888 963 8955",+ },+ emptyCompany(),+ );++ const data = stillFillable(+ update,+ emptyCompany({ description: "Our biggest account — handle with care." }),+ );++ expect(data.description).toBeUndefined();+ expect(data.phone).toBe("+1 888 963 8955");+ });++ it("keeps the icon, which the agent owns rather than shares", () => {+ const update = brandToUpdate(+ { logos: [{ url: "https://cdn/icon.svg", mode: "light", type: "icon" }] },+ emptyCompany(),+ );++ const data = stillFillable(+ update,+ emptyCompany({ iconUrl: "https://acme.test/favicon.ico" }),+ );++ expect(data.iconUrl).toBe("https://cdn/icon.svg");+ });++ it("drops the name once the placeholder has been answered", () => {+ const update = brandToUpdate(+ { title: "Stripe" },+ emptyCompany({ name: "stripe.com", nameIsPlaceholder: true }),+ );++ expect(update.name).toBe("Stripe");++ expect(+ stillFillable(update, emptyCompany({ name: "Stripe Inc" })).name,+ ).toBeUndefined();+ });+});diff --git a/apps/agent/test/pool.spec.ts b/apps/agent/test/pool.spec.tsindex 51138935..3a35df7b 100644--- a/apps/agent/test/pool.spec.ts+++ b/apps/agent/test/pool.spec.ts@@ -1,5 +1,5 @@import { describe, expect, it } from "bun:test";-import { runLimited } from "../agent/lib/pool";+import { collapsing, runLimited } from "../agent/lib/pool";describe("runLimited", () => {it("runs every item", async () => {@@ -12,6 +12,17 @@ describe("runLimited", () => {expect(seen.sort((a, b) => a - b)).toEqual([1, 2, 3, 4, 5, 6, 7]);});+ it("runs an item whose value is undefined", async () => {+ const seen: (number | undefined)[] = [];++ await runLimited(2, [1, undefined, 3], async (n) => {+ seen.push(n);+ });++ expect(seen).toHaveLength(3);+ expect(seen).toContain(3);+ });+it("never exceeds the width it was given", async () => {let running = 0;let peak = 0;@@ -54,3 +65,51 @@ describe("runLimited", () => {expect(peak).toBeLessThanOrEqual(2);});});++describe("collapsing", () => {+ it("still runs a poke that arrived during a failed drain", async () => {+ const runs: number[] = [];+ let release = () => {};+ const gate = new Promise<void>((resolve) => {+ release = resolve;+ });++ const drain = collapsing(async (n: number) => {+ runs.push(n);+ if (n === 1) {+ await gate;+ throw new Error("the drain failed");+ }+ });++ const first = drain(1);+ const second = drain(2).catch(() => {});+ release();++ await expect(first).rejects.toThrow("the drain failed");+ await second;++ expect(runs).toEqual([1, 2]);+ });++ it("folds every poke during a drain into one trailing run", async () => {+ const runs: number[] = [];+ let release = () => {};+ const gate = new Promise<void>((resolve) => {+ release = resolve;+ });++ const drain = collapsing(async (n: number) => {+ runs.push(n);+ if (n === 1) await gate;+ });++ const first = drain(1);+ void drain(2);+ void drain(3);+ release();+ await first;++ expect(runs).toEqual([1, 3]);+ });+});diff --git a/apps/app/test/onboarding-gate.spec.ts b/apps/app/test/onboarding-gate.spec.tsindex bbcc59e6..f463b79e 100644--- a/apps/app/test/onboarding-gate.spec.ts+++ b/apps/app/test/onboarding-gate.spec.ts@@ -11,12 +11,18 @@ afterEach(() => {globalThis.fetch = realFetch;});+function stub(handler: () => Promise<Response>) {+ globalThis.fetch = handler as unknown as typeof fetch;+}+function answerWith(body: unknown, status = 200) {- globalThis.fetch = (async () =>- new Response(JSON.stringify(body), {- status,- headers: { "content-type": "application/json" },- })) as typeof fetch;+ stub(+ async () =>+ new Response(JSON.stringify(body), {+ status,+ headers: { "content-type": "application/json" },+ }),+ );}function request(pathname: string, cookies: string[] = []) {@@ -58,9 +64,9 @@ describe("readOnboardingGate", () => {"unknown",);- globalThis.fetch = (async () => {+ stub(async () => {throw new Error("connect ECONNREFUSED");- }) as typeof fetch;+ });expect(await readOnboardingGate(request("/", [SESSION_COOKIE]))).toBe("unknown",);@@ -96,12 +102,12 @@ describe("proxy", () => {it("asks once, then remembers", async () => {let calls = 0;- globalThis.fetch = (async () => {+ stub(async () => {calls += 1;return new Response(JSON.stringify(workspace({ onboarded: true, canRename: true })),);- }) as typeof fetch;+ });const first = await proxy(request("/companies", [SESSION_COOKIE]));const marker = first.cookies.get(ONBOARDING_COOKIE);@@ -142,9 +148,9 @@ describe("proxy", () => {});it("fails open when the API is unreachable", async () => {- globalThis.fetch = (async () => {+ stub(async () => {throw new Error("connect ECONNREFUSED");- }) as typeof fetch;+ });const response = await proxy(request("/companies", [SESSION_COOKIE]));