Comp AI CRM is an open source, CRM designed for AI agents. Agentic-first CRM.
TypeScript
refactor: parse every remaining I/O boundary into a domain type (CMP-82) (#151)
grim3fb9922b63aa
diff --git a/.oxlintrc.json b/.oxlintrc.jsonindex ca8b61ea..8a779c4e 100644--- a/.oxlintrc.json+++ b/.oxlintrc.json@@ -11,6 +11,8 @@"**/.next/**","**/.eve/**","**/.scratch/**",+ ".agents/**",+ ".claude/**","apps/api/src/generated/**","packages/db/src/generated/**","packages/ui/src/components/**",@@ -41,6 +43,27 @@"rules": {"anti-slop/no-chained-type-assertions": "off"}+ },+ {+ "files": ["packages/validation/src/**"],+ "rules": {+ "anti-slop/no-unknown-parameters": "off"+ }+ },+ {+ "files": [+ "packages/telemetry/src/**",+ "apps/api/src/logging/**",+ "packages/db/src/fields.ts",+ "packages/db/src/fields-shape.ts",+ "apps/api/src/fields/**",+ "apps/app/components/crm/inline-field.tsx"+ ],+ "rules": {+ "anti-slop/no-unsafe-dictionary-type": "off",+ "anti-slop/no-unknown-parameters": "off",+ "anti-slop/no-runtime-typeof": "off"+ }}]}diff --git a/AGENTS.md b/AGENTS.mdindex a5c78b96..2be0b388 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -232,7 +232,10 @@ const slack = manifest.actions.find(const id = slack?.destination.id;```-`apps/agent/agent/lib/agent-manifest.ts` is the pattern. Rules that follow from+`packages/validation/src/agent-manifest.ts` is the pattern. A shape that crosses+a package boundary — a `Json` column two apps read, a payload one app writes and+another consumes — lives in `packages/validation/src`, one module per shape, and+is imported by subpath (`@crm/validation/agent-manifest`). Rules that follow fromit:- The schema describes what is **actually stored**, not the loosest thing thatdiff --git a/apps/agent/agent/channels/crm.ts b/apps/agent/agent/channels/crm.tsindex 0128065c..fabb45c4 100644--- a/apps/agent/agent/channels/crm.ts+++ b/apps/agent/agent/channels/crm.ts@@ -2,7 +2,9 @@ import { timingSafeEqual } from "node:crypto";import { EnrichmentStatus, Prisma } from "@crm/db";import { MAX_ATTEMPTS } from "@crm/db/agent-tasks";import { schemas } from "@crm/validation";+import { eveTurnFailure } from "@crm/validation/eve-stream";import { defineChannel, GET, POST } from "eve/channels";+import { z } from "zod";import { persistBuilderInputRequest } from "../lib/builder-input";import { verifyKey } from "../lib/context-dev";import {@@ -26,7 +28,7 @@ import {} from "../lib/dispatch";import { DISPATCH } from "../lib/dispatch-config";import { settle } from "../lib/enrichment";-import { finishRun } from "../lib/run-runtime";+import { finishRun, runResultOf } from "../lib/run-runtime";import { attribute } from "../lib/session-purpose";import { createSlackChannel } from "../lib/slack-membership";import { completeTask, taskSubject } from "../lib/tasks";@@ -34,6 +36,28 @@ import { completeTask, taskSubject } from "../lib/tasks";const TASK_MARKER = "task:";const STALE_QUEUE_MS = DISPATCH.sweep.staleQueueMs;+type InternalDispatchPrincipal = {+ readonly authenticator: string;+ readonly principalId: string;+ readonly principalType: string;+} | null;++const identifier = z.string().trim().min(1).nullable().catch(null);++const cancelRunRequest = z.object({ runId: identifier }).catch({ runId: null });++const verifyKeyRequest = z+ .object({ apiKey: identifier })+ .catch({ apiKey: null });++const receiveTarget = z+ .object({+ builderSubmissionId: z.string().nullable().catch(null),+ runId: z.string().nullable().catch(null),+ taskId: z.string().nullable().catch(null),+ })+ .catch({ builderSubmissionId: null, runId: null, taskId: null });+function authorised(request: Request): boolean {const secret = process.env.AGENT_BRIDGE_SECRET?.trim();if (!secret) return false;@@ -140,10 +164,9 @@ export default defineChannel({return new Response("Unauthorized", { status: 401 });}- const body = (await request.json().catch(() => null)) as {- runId?: unknown;- } | null;- const runId = typeof body?.runId === "string" ? body.runId.trim() : null;+ const { runId } = cancelRunRequest.parse(+ await request.json().catch(() => null),+ );if (!runId) {return Response.json({ error: "No run id was sent." }, { status: 400 });}@@ -184,12 +207,9 @@ export default defineChannel({return new Response("Unauthorized", { status: 401 });}- const body = (await request.json().catch(() => null)) as {- apiKey?: unknown;- } | null;-- const apiKey =- typeof body?.apiKey === "string" ? body.apiKey.trim() : null;+ const { apiKey } = verifyKeyRequest.parse(+ await request.json().catch(() => null),+ );if (!apiKey) {return Response.json(@@ -249,9 +269,7 @@ export default defineChannel({async "turn.failed"(data, channel) {const taskId = taskFromToken(channel.continuationToken);const reason =- typeof data === "object" && data && "message" in data- ? String((data as { message: unknown }).message)- : "The agent turn failed.";+ eveTurnFailure.parse(data).message ?? "The agent turn failed.";if (taskId) {const subject = await taskSubject(taskId);@@ -300,7 +318,7 @@ export default defineChannel({try {await finishRun(runId, {summary: run.summary ?? "The agent run completed.",- result: recordOf(run.result),+ result: runResultOf(run.result),});} catch (error) {await failRun(@@ -357,47 +375,32 @@ export default defineChannel({},async receive(input, { send }) {- const builderSubmissionId =- typeof input.target?.builderSubmissionId === "string"- ? input.target.builderSubmissionId- : null;- if (builderSubmissionId) {+ const target = receiveTarget.parse(input.target);+ if (target.builderSubmissionId) {assertInternalDispatchAuth(input.auth);- return dispatchBuilderSubmission(builderSubmissionId, send);+ return dispatchBuilderSubmission(target.builderSubmissionId, send);}- const runId =- typeof input.target?.runId === "string" ? input.target.runId : null;- if (runId) {+ if (target.runId) {assertInternalDispatchAuth(input.auth);- return dispatchAgentRun(runId, send);+ return dispatchAgentRun(target.runId, send);}- const taskId =- typeof input.target?.taskId === "string" ? input.target.taskId : null;-return send(input.message, {auth: input.auth,- continuationToken: taskId- ? taskToken(taskId)+ continuationToken: target.taskId+ ? taskToken(target.taskId): `crm:adhoc:${crypto.randomUUID()}`,});},});-function assertInternalDispatchAuth(value: unknown): void {- const auth = recordOf(value);+function assertInternalDispatchAuth(auth: InternalDispatchPrincipal): void {if (- auth.authenticator !== "app" ||+ auth?.authenticator !== "app" ||auth.principalType !== "runtime" ||auth.principalId !== "eve:app") {throw new Error("Internal agent dispatch requires Eve app authentication.");}}--function recordOf(value: unknown): Record<string, unknown> {- return value && typeof value === "object" && !Array.isArray(value)- ? (value as Record<string, unknown>)- : {};-}diff --git a/apps/agent/agent/hooks/activity.ts b/apps/agent/agent/hooks/activity.tsindex ac6e5105..2e661bd1 100644--- a/apps/agent/agent/hooks/activity.ts+++ b/apps/agent/agent/hooks/activity.ts@@ -1,7 +1,11 @@import { defineHook, type HookEvent } from "eve/hooks";+import { z } from "zod";type ActionRequest = HookEvent<"actions.requested">["data"]["actions"][number];type ActionResult = HookEvent<"action.result">["data"]["result"];+type ActionInput = ActionRequest["input"];++const inputText = z.string().nullable().catch(null);const SHOW_CONTENT = process.env.NODE_ENV !== "production";const MAX_IN_FLIGHT = 256;@@ -16,14 +20,14 @@ function line(symbol: string, text: string): void {console.error(`[agent] ${symbol} ${text}`);}-function preview(input: unknown): string {- if (!SHOW_CONTENT || typeof input !== "object" || input === null) return "";+function preview(input: ActionInput): string {+ if (!SHOW_CONTENT) return "";const parts: string[] = [];for (const [key, value] of Object.entries(input)) {if (value === null || value === undefined) continue;- const text = typeof value === "string" ? value : JSON.stringify(value);+ const text = inputText.parse(value) ?? JSON.stringify(value);parts.push(`${key}=${truncate(text ?? String(value), 48)}`);}diff --git a/apps/agent/agent/hooks/audit.ts b/apps/agent/agent/hooks/audit.tsindex 1cb42a0e..e2b59e9b 100644--- a/apps/agent/agent/hooks/audit.ts+++ b/apps/agent/agent/hooks/audit.ts@@ -1,10 +1,29 @@import { db, Prisma } from "@crm/db";import { defineHook } from "eve/hooks";+import { z } from "zod";import { isTransportOnlyEvent } from "../lib/event-persistence";import { currentFocus } from "../lib/focus";import { lockAgentRun } from "../lib/run-state";import { attribute, purposeOf } from "../lib/session-purpose";+const finiteNumber = z.number().refine(Number.isFinite).nullable().catch(null);++const stepUsage = z+ .object({+ usage: z+ .object({+ inputTokens: finiteNumber,+ outputTokens: finiteNumber,+ costUsd: finiteNumber,+ })+ .catch({ inputTokens: null, outputTokens: null, costUsd: null }),+ })+ .catch({ usage: { inputTokens: null, outputTokens: null, costUsd: null } });++const completedMessage = z+ .object({ message: z.string().nullable().catch(null) })+ .catch({ message: null });+export default defineHook({events: {async "*"(event, ctx) {@@ -13,7 +32,9 @@ export default defineHook({if (!id || isTransportOnlyEvent(event.type)) return;try {- const data = ("data" in event ? (event.data ?? {}) : {}) as object;+ const data = (+ "data" in event ? (event.data ?? {}) : {}+ ) as Prisma.InputJsonObject;const emittedAt = event.meta?.at ? new Date(event.meta.at) : new Date();const purpose = purposeOf(ctx);const conversationId =@@ -86,7 +107,7 @@ async function persistRunEvent(tx: Prisma.TransactionClient,eventId: string,type: string,- data: object,+ data: Prisma.InputJsonObject,emittedAt: Date,ctx: Parameters<typeof purposeOf>[0] & { session: { id: string } },) {@@ -128,17 +149,13 @@ async function persistRunEvent(runId,sequence,type,- data: data as Prisma.InputJsonValue,+ data,emittedAt,},});if (type === "step.completed") {- const usage = recordOf(data).usage;- const values = recordOf(usage);- const inputTokens = numberOf(values.inputTokens);- const outputTokens = numberOf(values.outputTokens);- const costUsd = numberOf(values.costUsd);+ const { inputTokens, outputTokens, costUsd } = stepUsage.parse(data).usage;const current = await tx.agentRun.findUniqueOrThrow({where: { id: runId },select: { inputTokens: true, outputTokens: true, costUsd: true },@@ -161,8 +178,8 @@ async function persistRunEvent(}if (type === "message.completed") {- const message = recordOf(data).message;- if (typeof message === "string" && message.trim()) {+ const { message } = completedMessage.parse(data);+ if (message?.trim()) {await tx.agentRun.updateMany({where: { id: runId, status: "RUNNING" },data: { summary: message.slice(0, 1000) },@@ -174,13 +191,3 @@ async function persistRunEvent(function isRootSession(ctx: Parameters<typeof purposeOf>[0]): boolean {return !("parent" in ctx.session) || !ctx.session.parent;}--function recordOf(value: unknown): Record<string, unknown> {- return value && typeof value === "object" && !Array.isArray(value)- ? (value as Record<string, unknown>)- : {};-}--function numberOf(value: unknown): number | null {- return typeof value === "number" && Number.isFinite(value) ? value : null;-}diff --git a/apps/agent/agent/hooks/telemetry.ts b/apps/agent/agent/hooks/telemetry.tsindex d203f847..da537aeb 100644--- a/apps/agent/agent/hooks/telemetry.ts+++ b/apps/agent/agent/hooks/telemetry.ts@@ -2,6 +2,13 @@ import { db } from "@crm/db";import { readAgentModel } from "@crm/db/settings";import { agentError, modelError } from "@crm/telemetry";import { defineHook } from "eve/hooks";+import { z } from "zod";++type SessionPrincipal = {+ readonly attributes?: Readonly<Record<string, string | readonly string[]>>;+} | null;++const attributeText = z.string().trim().min(1).nullable().catch(null);let modelId: string | null = null;@@ -27,11 +34,8 @@ const MODEL_CODES = ["unauthorized",];-function taskKind(- auth: { attributes?: Record<string, unknown> } | null,-): string | null {- const kind = auth?.attributes?.taskKind;- return typeof kind === "string" && kind.trim() ? kind.trim() : null;+function taskKind(auth: SessionPrincipal): string | null {+ return attributeText.parse(auth?.attributes?.taskKind);}function looksLikeModel(code: string): boolean {diff --git a/apps/agent/agent/instructions/task.ts b/apps/agent/agent/instructions/task.tsindex e617aab0..36a15096 100644--- a/apps/agent/agent/instructions/task.ts+++ b/apps/agent/agent/instructions/task.ts@@ -1,9 +1,19 @@import { defineDynamic, defineInstructions } from "eve/instructions";+import { z } from "zod";import { focusOn, setBudget } from "../lib/focus";import { sessionPreamble } from "../lib/preamble";import { RESEARCH_INSTRUCTIONS } from "../lib/research-instructions";import { attribute, purposeOf } from "../lib/session-purpose";+const attributeText = z.string().trim().min(1).nullable().catch(null);++const attributeNumber = z+ .union([z.string(), z.number()])+ .transform(Number)+ .refine(Number.isFinite)+ .nullable()+ .catch(null);+export default defineDynamic({events: {"session.started": async (_event, ctx) => {@@ -19,21 +29,21 @@ export default defineDynamic({}const attributes = ctx.session.auth.current?.attributes ?? {};- const budget = asNumber(attributes.budget);- const kind = asString(attributes.taskKind);+ const budget = attributeNumber.parse(attributes.budget);+ const kind = attributeText.parse(attributes.taskKind);if (budget) setBudget(budget);const { markdown, focus } = await sessionPreamble({- contactId: asString(attributes.contactId),- companyId: asString(attributes.companyId),- dealId: asString(attributes.dealId),+ contactId: attributeText.parse(attributes.contactId),+ companyId: attributeText.parse(attributes.companyId),+ dealId: attributeText.parse(attributes.dealId),},{dispatched: Boolean(kind),kind,- reason: asString(attributes.reason),+ reason: attributeText.parse(attributes.reason),budget,},);@@ -71,12 +81,3 @@ export function builderTaskMarkdown(? `Before any other work, call set_chat_title once. Summarize the user's first message as a polished title of three to seven words in sentence case. Capture the intent, remove slash-command syntax and filler, and do not use quotation marks or ending punctuation.\n\n${task}`: task;}--function asString(value: unknown): string | null {- return typeof value === "string" && value.trim() ? value.trim() : null;-}--function asNumber(value: unknown): number | null {- const parsed = typeof value === "string" ? Number(value) : value;- return typeof parsed === "number" && Number.isFinite(parsed) ? parsed : null;-}diff --git a/apps/agent/agent/lib/accounts.ts b/apps/agent/agent/lib/accounts.tsindex 459c39b2..742d3773 100644--- a/apps/agent/agent/lib/accounts.ts+++ b/apps/agent/agent/lib/accounts.ts@@ -1,8 +1,16 @@import { ActivityType, db, EmailDirection } from "@crm/db";+import { z } from "zod";import { isDerivedName } from "./names";const BODY_LIMIT = 4000;+const stageChangeMeta = z+ .object({+ from: z.string().nullable().catch(null),+ to: z.string().nullable().catch(null),+ })+ .catch({ from: null, to: null });+export type AccountThread = {subject: string | null;contact: { id: string; name: string } | null;@@ -487,10 +495,10 @@ export async function readDealHistory(role,})),stageHistory: stageChanges.map((change) => {- const meta = (change.meta ?? {}) as { from?: unknown; to?: unknown };+ const meta = stageChangeMeta.parse(change.meta);return {- from: typeof meta.from === "string" ? meta.from : null,- to: typeof meta.to === "string" ? meta.to : null,+ from: meta.from,+ to: meta.to,at: change.createdAt.toISOString(),};}),diff --git a/apps/agent/agent/lib/agent-actions.ts b/apps/agent/agent/lib/agent-actions.tsindex a2993a50..8f403b4a 100644--- a/apps/agent/agent/lib/agent-actions.ts+++ b/apps/agent/agent/lib/agent-actions.ts@@ -1,11 +1,7 @@-export const AGENT_ACTION_TYPES = {- CRM_ACTIVITY_CREATE: "crm.activity.create",- RUN_SUMMARY: "run.summary",- SLACK_MESSAGE_POST: "slack.message.post",-} as const;--export type AgentActionType =- (typeof AGENT_ACTION_TYPES)[keyof typeof AGENT_ACTION_TYPES];+import {+ AGENT_ACTION_TYPES,+ type AgentActionType,+} from "@crm/validation/agent-manifest";export const AGENT_ACTION_EXECUTORS = {[AGENT_ACTION_TYPES.CRM_ACTIVITY_CREATE]: "create_crm_activity",@@ -13,12 +9,14 @@ export const AGENT_ACTION_EXECUTORS = {[AGENT_ACTION_TYPES.SLACK_MESSAGE_POST]: "post_slack_message",} as const satisfies Record<AgentActionType, string>;-export function isAgentActionType(value: unknown): value is AgentActionType {- return Object.hasOwn(AGENT_ACTION_EXECUTORS, String(value));+export function isAgentActionType(value: string): value is AgentActionType {+ return Object.hasOwn(AGENT_ACTION_EXECUTORS, value);}+export type AgentActionDependencyId = "slack";+export type AgentActionDependency = {- readonly id: string;+ readonly id: AgentActionDependencyId;readonly label: string;readonly resourceId: string;readonly fix: string;diff --git a/apps/agent/agent/lib/blank-facts.ts b/apps/agent/agent/lib/blank-facts.tsindex f0595249..f2088c01 100644--- a/apps/agent/agent/lib/blank-facts.ts+++ b/apps/agent/agent/lib/blank-facts.ts@@ -79,13 +79,12 @@ export async function sweepBlankFacts(for (const group of groupByField(proposals)) {const [best] = group;const field = best.field as FactField;- const contact = best.contact as FactSubject;+ const contact: FactSubject = best.contact;const column = factColumn(field);const current = applied.get(key(best.contactId, field));if (!fillsBlank({ field, contact, hasAgentFact: current !== undefined })) {- const value =- current ?? (column ? (contact[column] as string | null) : null);+ const value = current ?? (column ? contact[column] : null);const stale = redundant(group, value);sweep.waiting += group.length - stale.length;diff --git a/apps/agent/agent/lib/brand-images.ts b/apps/agent/agent/lib/brand-images.tsindex f00f2fe5..df017e75 100644--- a/apps/agent/agent/lib/brand-images.ts+++ b/apps/agent/agent/lib/brand-images.ts@@ -1,6 +1,9 @@import type { Prisma } from "@crm/db";import { blobEnabled, mirror } from "@crm/db/blob";import { COMPANY_IMAGE_FIELDS } from "@crm/db/images";+import { z } from "zod";++const mirrorableUrl = z.string().regex(/\S/).nullable().catch(null);export async function mirrorBrandImages(companyId: string,@@ -12,7 +15,7 @@ export async function mirrorBrandImages(await Promise.all(COMPANY_IMAGE_FIELDS.map(async (slot) => {- const source = plain(update[slot]);+ const source = mirrorableUrl.parse(update[slot]);if (!source) return;const stored = await mirror(source, `companies/${companyId}/${slot}`);@@ -25,7 +28,3 @@ export async function mirrorBrandImages(return { update, mirrored };}--function plain(value: unknown): string | null {- return typeof value === "string" && value.trim() ? value : null;-}diff --git a/apps/agent/agent/lib/brand-mapping.ts b/apps/agent/agent/lib/brand-mapping.tsindex 1d6994d0..0c92ef8f 100644--- a/apps/agent/agent/lib/brand-mapping.ts+++ b/apps/agent/agent/lib/brand-mapping.ts@@ -115,7 +115,7 @@ export function brandToUpdate(value: string | null,) => {if (value && fillable(key, current)) {- (update as Record<string, unknown>)[key] = value;+ update[key] = value;}};@@ -162,15 +162,9 @@ 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;+ return Object.fromEntries(+ Object.entries(update).filter(([key]) => fillable(key, current)),+ );}export function filledFields(update: BrandUpdate): string[] {diff --git a/apps/agent/agent/lib/builder-input.ts b/apps/agent/agent/lib/builder-input.tsindex 6e97f263..a3dbbed7 100644--- a/apps/agent/agent/lib/builder-input.ts+++ b/apps/agent/agent/lib/builder-input.ts@@ -1,6 +1,7 @@import { db, type Prisma } from "@crm/db";import { lockIdempotencyKey } from "@crm/db/idempotency";import { type InputRequested, parse, schemas } from "@crm/validation";+import type { ChannelEvents } from "eve/channels";import {builderIdFromToken,builderToken,@@ -12,8 +13,12 @@ const BUILDER_INPUT = {idPrefix: "builder-input",} as const;+type InputRequestedEvent = Parameters<+ NonNullable<ChannelEvents["input.requested"]>+>[0];+export async function persistBuilderInputRequest(- data: unknown,+ data: InputRequestedEvent,continuationToken: string | undefined,authenticatedConversationId?: string | null,): Promise<boolean> {diff --git a/apps/agent/agent/lib/builder-runtime.ts b/apps/agent/agent/lib/builder-runtime.tsindex 8a20494c..4a2a2f9a 100644--- a/apps/agent/agent/lib/builder-runtime.ts+++ b/apps/agent/agent/lib/builder-runtime.ts@@ -7,7 +7,9 @@ import {} from "@crm/db/crm-events";import { readAgentModel } from "@crm/db/settings";import { WORKSPACE_ID } from "@crm/db/workspace";-import { AGENT_ACTION_TYPES, actionDependency } from "./agent-actions";+import { AGENT_ACTION_TYPES } from "@crm/validation/agent-manifest";+import { z } from "zod";+import { actionDependency } from "./agent-actions";import { requestStaleSlackInventorySync } from "./slack-people";const GMAIL_SCOPE = "https://www.googleapis.com/auth/gmail.readonly";@@ -19,6 +21,19 @@ export type BuilderResource = {label: string;};+const taggedResource = z+ .object({+ kind: z.enum(["integration", "company", "contact", "deal"]),+ id: z.string(),+ label: z.string(),+ })+ .nullable()+ .catch(null);++const taggedResources = z+ .object({ resources: z.array(taggedResource).catch([]) })+ .catch({ resources: [] });+export type DraftTrigger = {type: "MANUAL" | "SCHEDULE" | "EVENT";name: string;@@ -71,11 +86,11 @@ export const BUILDER_ARTIFACT_PATHS = [export type BuilderArtifactPath = (typeof BUILDER_ARTIFACT_PATHS)[number];-const ARTIFACT_LANGUAGES: Record<BuilderArtifactPath, string> = {+const ARTIFACT_LANGUAGES = {"agent/README.md": "markdown","agent/instructions.md": "markdown","agent/manifest.json": "json",-};+} satisfies Record<BuilderArtifactPath, string>;export async function writeBuilderArtifact(conversationId: string,@@ -247,7 +262,7 @@ export async function saveBuilderDraft(credentials: "app-runtime-only",summary: "Isolated sandbox · deny-all network · bounded CRM tools",};- const files = artifactFiles(input, manifest);+ const files = artifactFiles(input, JSON.stringify(manifest, null, 2));for (const file of files) assertSafeArtifact(file.content);return db.$transaction(async (tx) => {@@ -777,25 +792,10 @@ async function missingResourceIds(resources: BuilderResource[]) {return described.filter((resource) => !resource.record);}-function resourcesOf(value: unknown): BuilderResource[] {- if (!value || typeof value !== "object" || !("resources" in value)) return [];- const resources = (value as { resources?: unknown }).resources;- if (!Array.isArray(resources)) return [];-- return resources.flatMap((resource) => {- if (!resource || typeof resource !== "object") return [];- const row = resource as Record<string, unknown>;- if (- !["integration", "company", "contact", "deal"].includes(- String(row.kind),- ) ||- typeof row.id !== "string" ||- typeof row.label !== "string"- ) {- return [];- }- return [resource as BuilderResource];- });+function resourcesOf(value: Prisma.JsonValue): BuilderResource[] {+ return taggedResources+ .parse(value)+ .resources.filter((resource) => resource !== null);}function uniqueResources(resources: BuilderResource[]): BuilderResource[] {@@ -815,7 +815,7 @@ function scheduleDate(trigger: DraftTrigger, now: Date): Date | null {return parsed > now ? parsed : null;}-function artifactFiles(input: DraftAgentInput, manifest: object) {+function artifactFiles(input: DraftAgentInput, manifestJson: string) {const triggerSummary = input.triggers.map((trigger) => `- ${trigger.summary}`).join("\n");@@ -833,7 +833,7 @@ function artifactFiles(input: DraftAgentInput, manifest: object) {{path: "agent/manifest.json" as const,language: ARTIFACT_LANGUAGES["agent/manifest.json"],- content: `${JSON.stringify(manifest, null, 2)}\n`,+ content: `${manifestJson}\n`,},];}diff --git a/apps/agent/agent/lib/capabilities.ts b/apps/agent/agent/lib/capabilities.tsindex cef15dcc..6660bf77 100644--- a/apps/agent/agent/lib/capabilities.ts+++ b/apps/agent/agent/lib/capabilities.ts@@ -75,11 +75,13 @@ export async function enabled(id: string): Promise<boolean> {);}-export function unavailable(env: string): {+export type UnavailableCapability = {ok: false;configured: false;reason: string;-} {+};++export function unavailable(env: string): UnavailableCapability {return {ok: false,configured: false,diff --git a/apps/agent/agent/lib/context-dev.ts b/apps/agent/agent/lib/context-dev.tsindex 6f093c43..cd981fe9 100644--- a/apps/agent/agent/lib/context-dev.ts+++ b/apps/agent/agent/lib/context-dev.ts@@ -1,7 +1,25 @@import ContextDev from "context.dev";import { APIError } from "context.dev/core/error";+import { z } from "zod";import { contextDevKey } from "./capabilities";+export type JsonSchema = {+ type?:+ | "array"+ | "boolean"+ | "integer"+ | "null"+ | "number"+ | "object"+ | "string";+ description?: string;+ properties?: Record<string, JsonSchema>;+ items?: JsonSchema;+ required?: string[];+ enum?: (string | number | boolean | null)[];+ additionalProperties?: boolean | JsonSchema;+};+export type Brand = {domain?: string | null;title?: string | null;@@ -99,16 +117,16 @@ export async function verifyKey(key: string): Promise<KeyCheck> {}}-export function classifyKey(error: unknown): KeyCheck {- if (!(error instanceof APIError)) {- return { outcome: "unknown", reason: describe(error) };+export function classifyKey(cause: unknown): KeyCheck {+ if (!(cause instanceof APIError)) {+ return { outcome: "unknown", reason: describe(cause) };}- if (error.status === undefined) {- return { outcome: "unknown", reason: describe(error) };+ if (cause.status === undefined) {+ return { outcome: "unknown", reason: describe(cause) };}- if (error.status === 401 && !recognisedKeyFailure(error)) {+ if (cause.status === 401 && !recognisedKeyFailure(cause)) {return {outcome: "invalid",reason: "Context did not recognise that API key.",@@ -122,12 +140,13 @@ export async function brandByDomain(domain: string,maxAgeMs?: number,): Promise<LookupResult> {- return lookup({+ const params = {type: "by_domain",domain,timeoutMS: TIMEOUT_MS,- ...(maxAgeMs === undefined ? {} : { maxAgeMs }),- });+ } as const;++ return lookup(maxAgeMs === undefined ? params : { ...params, maxAgeMs });}export async function brandByEmail(email: string): Promise<LookupResult> {@@ -145,7 +164,7 @@ export async function prefetch(domain: string): Promise<void> {export async function extract(url: string,- schema: Record<string, unknown>,+ schema: JsonSchema,instructions: string,): Promise<{ outcome: "found"; data: unknown } | { outcome: "failed"; reason: string }@@ -181,15 +200,18 @@ export async function search(return { outcome: "failed", reason: "Context.dev is not configured." };}+ const params = {+ query,+ numResults: Math.max(options.limit ?? 10, 10),+ markdownOptions: { enabled: true },+ };+try {- const response = await api.web.search({- query,- numResults: Math.max(options.limit ?? 10, 10),- markdownOptions: { enabled: true },- ...(options.excludeDomains- ? { excludeDomains: options.excludeDomains }- : {}),- });+ const response = await api.web.search(+ options.excludeDomains+ ? { ...params, excludeDomains: options.excludeDomains }+ : params,+ );const results = (response.results ?? []).map((result) => ({url: result.url ?? null,@@ -227,14 +249,14 @@ async function lookup(}}-function classify(error: unknown): LookupResult {- if (!(error instanceof APIError)) {- return { outcome: "failed", reason: describe(error), retryable: true };+function classify(cause: unknown): LookupResult {+ if (!(cause instanceof APIError)) {+ return { outcome: "failed", reason: describe(cause), retryable: true };}- const code = errorCode(error);+ const code = errorCode(cause);- if (error.status === 400) {+ if (cause.status === 400) {if (code === "NOT_FOUND" || code === "WEBSITE_ACCESS_ERROR") {return {outcome: "skipped",@@ -244,42 +266,46 @@ function classify(error: unknown): LookupResult {: "The site could not be reached.",};}- return { outcome: "failed", reason: describe(error), retryable: false };+ return { outcome: "failed", reason: describe(cause), retryable: false };}- if (error.status === 422) {+ if (cause.status === 422) {return {outcome: "skipped",reason: "That is a personal or disposable email address.",};}- if (error.status === 401 || error.status === 403) {- return { outcome: "failed", reason: describe(error), retryable: false };+ if (cause.status === 401 || cause.status === 403) {+ return { outcome: "failed", reason: describe(cause), retryable: false };}- if (error.status === 408 || error.status === 429) {- return { outcome: "failed", reason: describe(error), retryable: true };+ if (cause.status === 408 || cause.status === 429) {+ return { outcome: "failed", reason: describe(cause), retryable: true };}return {outcome: "failed",- reason: describe(error),- retryable: (error.status ?? 500) >= 500,+ reason: describe(cause),+ retryable: (cause.status ?? 500) >= 500,};}+const apiErrorBody = z+ .object({+ error_code: z.string().optional().catch(undefined),+ message: z.string().optional().catch(undefined),+ })+ .catch({});+function errorCode(error: APIError): string | undefined {- const body = error.error as { error_code?: unknown } | undefined;- return typeof body?.error_code === "string" ? body.error_code : undefined;+ return apiErrorBody.parse(error.error).error_code;}function recognisedKeyFailure(error: APIError): boolean {- const body = error.error as- | { error_code?: unknown; message?: unknown }- | undefined;- const detail = [body?.error_code, body?.message, error.message]- .filter((value): value is string => typeof value === "string")+ const body = apiErrorBody.parse(error.error);+ const detail = [body.error_code, body.message, error.message]+ .filter((value) => value !== undefined).join(" ");return /(usage|credit|quota|allowance|billing|rate.?limit|limit.?exceeded|insufficient.?permission)/i.test(@@ -287,9 +313,9 @@ function recognisedKeyFailure(error: APIError): boolean {);}-function describe(error: unknown): string {- if (error instanceof APIError) {- return `${error.status ?? "?"} ${errorCode(error) ?? error.message}`;+function describe(cause: unknown): string {+ if (cause instanceof APIError) {+ return `${cause.status ?? "?"} ${errorCode(cause) ?? cause.message}`;}- return error instanceof Error ? error.message : String(error);+ return cause instanceof Error ? cause.message : String(cause);}diff --git a/apps/agent/agent/lib/crm.ts b/apps/agent/agent/lib/crm.tsindex 91599187..0c0e5e82 100644--- a/apps/agent/agent/lib/crm.ts+++ b/apps/agent/agent/lib/crm.ts@@ -1,4 +1,4 @@-import { db, EnrichmentStatus } from "@crm/db";+import { db, EnrichmentStatus, type Prisma } from "@crm/db";import { domainOf, isDerivedName } from "./names";import type { Person } from "./socials";@@ -340,9 +340,7 @@ export async function setEnrichmentStatus(data: {enrichmentStatus: status,enrichmentError: error ?? null,- ...(status === EnrichmentStatus.COMPLETE- ? { enrichedAt: new Date() }- : {}),+ enrichedAt: status === EnrichmentStatus.COMPLETE ? new Date() : undefined,},});}@@ -351,7 +349,7 @@ export async function writeTimelineNote(contactId: string,subject: string,body: string,- meta: Record<string, unknown> = {},+ meta: Prisma.InputJsonObject = {},): Promise<string | null> {const contact = await db.contact.findUnique({where: { id: contactId },diff --git a/apps/agent/agent/lib/custom-agent-dispatch.ts b/apps/agent/agent/lib/custom-agent-dispatch.tsindex e351939f..aff74d24 100644--- a/apps/agent/agent/lib/custom-agent-dispatch.ts+++ b/apps/agent/agent/lib/custom-agent-dispatch.ts@@ -1,7 +1,10 @@import { db, Prisma } from "@crm/db";-import { CRM_EVENT_CATALOG, isCrmEventType } from "@crm/db/crm-events";+import { CRM_EVENT_CATALOG } from "@crm/db/crm-events";import { lockIdempotencyKey } from "@crm/db/idempotency";+import { crmEventTask } from "@crm/validation/agent-events";+import { readAgentTriggerConfig } from "@crm/validation/agent-manifest";import type { SendFn } from "eve/channels";+import { z } from "zod";import { DISPATCH } from "./dispatch-config";import { DEPENDENCY_UNAVAILABLE, runDependencyFailure } from "./run-preflight";import {@@ -17,6 +20,32 @@ const MAX_BUILDER_ATTEMPTS = DISPATCH.builder.maxAttempts;const BUILDER_LEASE_MS = DISPATCH.builder.leaseMs;const RUN_DELIVERY_LEASE_MS = DISPATCH.run.deliveryLeaseMs;+type BuilderMessageParts = Extract<Parameters<SendFn>[0], readonly unknown[]>;++const trimmedText = z.string().trim().catch("");++const builderInputResponse = z+ .object({+ requestId: trimmedText,+ optionId: trimmedText,+ text: trimmedText,+ })+ .catch({ requestId: "", optionId: "", text: "" });++const builderSubmissionMessage = z+ .object({+ text: z.string().catch(""),+ resources: z+ .array(z.object({ label: z.string().catch("") }).catch({ label: "" }))+ .catch([]),+ inputResponse: builderInputResponse,+ })+ .catch({+ text: "",+ resources: [],+ inputResponse: { requestId: "", optionId: "", text: "" },+ });+export async function pendingBuilderSubmissionIds(): Promise<string[]> {await recoverBuilderSubmissions();const rows = await db.agentConversationSubmission.findMany({@@ -219,7 +248,9 @@ export async function queueDueAgentRuns(now = new Date()): Promise<number> {for (const trigger of triggers) {if (!trigger.nextRunAt) continue;const scheduledAt = trigger.nextRunAt;- const intervalMinutes = intervalOf(trigger.config);+ const intervalMinutes = readAgentTriggerConfig(+ trigger.config,+ ).intervalMinutes;const nextRunAt = advance(scheduledAt, intervalMinutes, now);const idempotencyKey = `${trigger.id}:${scheduledAt.toISOString()}`;const claimed = await db.$transaction(async (tx) => {@@ -271,29 +302,22 @@ export async function queueEventAgentRuns("id" | "contactId" | "companyId" | "dealId" | "payload">,): Promise<number> {- const payload = recordOf(task.payload);- const eventType = payload.type;- const record = recordOf(payload.record);- const recordKind = textOf(record.kind);- const recordId = textOf(record.id);- const occurredAt = textOf(payload.occurredAt);+ const parsed = crmEventTask.safeParse(task.payload);+ if (!parsed.success) {+ throw new Error("The queued agent event is invalid.");+ }++ const { type: eventType, occurredAt, data } = parsed.data;+ const recordKind = CRM_EVENT_CATALOG[eventType].recordKind;+ const recordId = parsed.data.record.id;const occurredAtDate = new Date(occurredAt);const taskRecordId =recordKind === "contact"? task.contactId: recordKind === "company"? task.companyId- : recordKind === "deal"- ? task.dealId- : null;- if (- !isCrmEventType(eventType) ||- CRM_EVENT_CATALOG[eventType].recordKind !== recordKind ||- !recordId ||- taskRecordId !== recordId ||- !occurredAt ||- Number.isNaN(occurredAtDate.getTime())- ) {+ : task.dealId;+ if (taskRecordId !== recordId) {throw new Error("The queued agent event is invalid.");}@@ -314,7 +338,7 @@ export async function queueEventAgentRuns(let matched = 0;for (const trigger of triggers) {- if (recordOf(trigger.config).event !== eventType) continue;+ if (readAgentTriggerConfig(trigger.config).event !== eventType) continue;const idempotencyKey = `event:${task.id}:trigger:${trigger.id}`;const queued = await db.$transaction(async (tx) => {@@ -340,13 +364,9 @@ export async function queueEventAgentRuns(idempotencyKey,correlationId: `trigger:${trigger.id}:event:${task.id}`,input: {- event: {- type: eventType,- occurredAt,- data: recordOf(payload.data),- },+ event: { type: eventType, occurredAt, data },record: { kind: recordKind, id: recordId },- } as Prisma.InputJsonValue,+ },events: {create: {sequence: 0,@@ -812,14 +832,11 @@ async function recoverAgentRuns() {export function builderDeliveryMessage(submissionId: string,- value: unknown,+ value: Prisma.JsonValue,attachments: readonly BuilderDeliveryAttachment[] = [],): Parameters<SendFn>[0] {- const message = recordOf(value);- const inputResponse = recordOf(message.inputResponse);- const requestId = textOf(inputResponse.requestId);- const optionId = textOf(inputResponse.optionId);- const responseText = textOf(inputResponse.text);+ const message = builderSubmissionMessage.parse(value);+ const { requestId, optionId, text: responseText } = message.inputResponse;if (requestId && (optionId || responseText)) {return {inputResponses: [@@ -831,18 +848,19 @@ export function builderDeliveryMessage(};}- const text = typeof message.text === "string" ? message.text : "";- const resources = Array.isArray(message.resources) ? message.resources : [];+ const labels = message.resources+ .map((resource) => resource.label)+ .filter(Boolean);const context = [`Submission id: ${submissionId}`,- resources.length > 0- ? `Tagged resources: ${resources.map(resourceLabel).filter(Boolean).join(", ")}`+ message.resources.length > 0+ ? `Tagged resources: ${labels.join(", ")}`: null,].filter(Boolean).join("\n");- const parts: Array<Record<string, unknown>> = [- { type: "text", text: `${context}\n\n${text}` },+ const parts: BuilderMessageParts = [+ { type: "text", text: `${context}\n\n${message.text}` },];for (const attachment of attachments) {@@ -854,18 +872,16 @@ export function builderDeliveryMessage(});}- return parts as Parameters<SendFn>[0];+ return parts;}export function builderCommandType(commandType: string,- value: unknown,+ value: Prisma.JsonValue,): string {- const inputResponse = recordOf(recordOf(value).inputResponse);- return textOf(inputResponse.requestId) &&- (textOf(inputResponse.optionId) || textOf(inputResponse.text))- ? "CREATE_AGENT"- : commandType;+ const { requestId, optionId, text } =+ builderSubmissionMessage.parse(value).inputResponse;+ return requestId && (optionId || text) ? "CREATE_AGENT" : commandType;}type BuilderDeliveryAttachment = {@@ -874,24 +890,6 @@ type BuilderDeliveryAttachment = {content: Uint8Array;};-function resourceLabel(value: unknown): string | null {- const row = recordOf(value);- return typeof row.label === "string" ? row.label : null;-}--function textOf(value: unknown): string {- return typeof value === "string" ? value.trim() : "";-}--function intervalOf(value: unknown): number {- const interval = recordOf(value).intervalMinutes;- return typeof interval === "number" &&- Number.isFinite(interval) &&- interval >= 1- ? Math.min(interval, 525_600)- : 1440;-}-function advance(from: Date, intervalMinutes: number, now: Date): Date {const intervalMs = intervalMinutes * 60_000;const missed = Math.max(@@ -908,9 +906,3 @@ function idFromToken(token: string | undefined, marker: string): string | null {const id = token.slice(index + marker.length);return id || null;}--function recordOf(value: unknown): Record<string, unknown> {- return value && typeof value === "object" && !Array.isArray(value)- ? (value as Record<string, unknown>)- : {};-}diff --git a/apps/agent/agent/lib/dispatch.ts b/apps/agent/agent/lib/dispatch.tsindex 5f0b9a04..eaf70d22 100644--- a/apps/agent/agent/lib/dispatch.ts+++ b/apps/agent/agent/lib/dispatch.ts@@ -252,20 +252,23 @@ export async function linkSession(return false;}-function reasonOf(error: unknown): string {- return error instanceof Error ? error.message : String(error);+function reasonOf(cause: unknown): string {+ return cause instanceof Error ? cause.message : String(cause);}export function taskAuth(task: LeasedTask, base: AppAuth = APP_AUTH): AppAuth {+ const records: Record<string, string> = {};+ if (task.contactId) records.contactId = task.contactId;+ if (task.companyId) records.companyId = task.companyId;+ if (task.dealId) records.dealId = task.dealId;+return {...base,attributes: {taskKind: task.kind,reason: task.reason,budget: String(task.budget),- ...(task.contactId ? { contactId: task.contactId } : {}),- ...(task.companyId ? { companyId: task.companyId } : {}),- ...(task.dealId ? { dealId: task.dealId } : {}),+ ...records,},};}diff --git a/apps/agent/agent/lib/enrichment.ts b/apps/agent/agent/lib/enrichment.tsindex 601a9e5f..06efad02 100644--- a/apps/agent/agent/lib/enrichment.ts+++ b/apps/agent/agent/lib/enrichment.ts@@ -32,7 +32,7 @@ async function write(const data = {enrichmentStatus: status,enrichmentError: error,- ...(status === EnrichmentStatus.COMPLETE ? { enrichedAt: new Date() } : {}),+ enrichedAt: status === EnrichmentStatus.COMPLETE ? new Date() : undefined,};const guard: SettleGuard = onlyIfRunningdiff --git a/apps/agent/agent/lib/evidence.ts b/apps/agent/agent/lib/evidence.tsindex 67bcb4d3..fcc3a5a8 100644--- a/apps/agent/agent/lib/evidence.ts+++ b/apps/agent/agent/lib/evidence.ts@@ -19,7 +19,7 @@ type Weighting = {label: string;};-export const WEIGHTS: Record<EvidenceKind, Weighting> = {+export const WEIGHTS = {"profile.email-match": {weight: 0.95,primary: true,@@ -75,7 +75,7 @@ export const WEIGHTS: Record<EvidenceKind, Weighting> = {primary: false,label: "another source disagrees",},-};+} satisfies Record<EvidenceKind, Weighting>;export type Evidence = {kind: EvidenceKind;diff --git a/apps/agent/agent/lib/facts.ts b/apps/agent/agent/lib/facts.tsindex 3daf8989..10eef699 100644--- a/apps/agent/agent/lib/facts.ts+++ b/apps/agent/agent/lib/facts.ts@@ -18,15 +18,17 @@ const FIELDS = {export type FactField = keyof typeof FIELDS;+export type FactColumn = NonNullable<(typeof FIELDS)[FactField]["column"]>;+export const FACT_FIELDS = Object.keys(FIELDS) as FactField[];export type FactSubject = {email: string | null;firstName: string;lastName: string | null;-} & Record<string, unknown>;+} & { [Column in FactColumn]: string | null };-export function factColumn(field: FactField): string | null {+export function factColumn(field: FactField): FactColumn | null {return FIELDS[field].column;}@@ -310,12 +312,8 @@ function humanOwns({hasAgentFact,}: {field: FactField;- column: string | null;- contact: {- email: string | null;- firstName: string;- lastName: string | null;- } & Record<string, unknown>;+ column: FactColumn | null;+ contact: FactSubject;hasAgentFact: boolean;}): boolean {if (field === "name") {@@ -334,8 +332,8 @@ function isEmpty({hasAgentFact,}: {field: FactField;- column: string | null;- contact: Record<string, unknown>;+ column: FactColumn | null;+ contact: FactSubject;hasAgentFact: boolean;}): boolean {if (hasAgentFact) return false;@@ -345,10 +343,10 @@ function isEmpty({return !contact[column];}-const HOST_ALIASES: Record<string, string> = {- "twitter.com": "x.com",- "mobile.twitter.com": "x.com",-};+const HOST_ALIASES = new Map([+ ["twitter.com", "x.com"],+ ["mobile.twitter.com", "x.com"],+]);export function sameValue(a: string, b: string): boolean {return canonicalValue(a) === canonicalValue(b);@@ -363,7 +361,7 @@ export function canonicalValue(value: string): string {const host = url.host.replace(/^www\./, "");const path = url.pathname.replace(/\/+$/, "");- return `${HOST_ALIASES[host] ?? host}${path}`;+ return `${HOST_ALIASES.get(host) ?? host}${path}`;}function asWebUrl(value: string): URL | null {diff --git a/apps/agent/agent/lib/focus.ts b/apps/agent/agent/lib/focus.tsindex 439acbd8..2217c237 100644--- a/apps/agent/agent/lib/focus.ts+++ b/apps/agent/agent/lib/focus.ts@@ -10,10 +10,12 @@ export const focus = defineState("crm.focus", () => ({exhausted: false,}));-export function currentFocus(): {+export type CurrentFocus = {contactId: string | null;sessionId: string | null;-} {+};++export function currentFocus(): CurrentFocus {try {const state = focus.get();return { contactId: state.contactId, sessionId: state.sessionId };diff --git a/apps/agent/agent/lib/linkdapi.ts b/apps/agent/agent/lib/linkdapi.tsindex 1b3be0cd..aab557d1 100644--- a/apps/agent/agent/lib/linkdapi.ts+++ b/apps/agent/agent/lib/linkdapi.ts@@ -1,6 +1,33 @@+import { z } from "zod";+const HOST = "linkdapi-best-unofficial-linkedin-api.p.rapidapi.com";const TIMEOUT_MS = 20_000;+const json = z.json();+const text = z.string().trim().min(1).nullable().catch(null);+const rawText = z.string().nullable().catch(null);+const count = z.number().nullable().catch(null);+const fields = z.record(z.string(), json).catch({});+const optionalFields = z.record(z.string(), json).nullable().catch(null);+const rows = z.array(json).nullable().catch(null);++const envelope = z+ .object({+ success: z.boolean().nullable().catch(null),+ data: json.catch(null),+ })+ .catch({ success: null, data: null });++const experienceEnvelope = z+ .object({ experience: rows, experiences: rows })+ .catch({ experience: null, experiences: null });++const companyLookup = z.object({ companies: rows }).catch({ companies: null });++type Json = z.infer<typeof json>;++export type LinkedinFields = z.infer<typeof fields>;+export type Profile = {slug: string;profileUrl: string;@@ -64,29 +91,29 @@ export function slugFromProfileUrl(raw: string | null): string | null {}export async function getProfile(slug: string): Promise<Outcome<Profile>> {- const result = await call<RawProfile>("/api/v1/profile/overview", {- username: slug,- });+ const result = await call("/api/v1/profile/overview", { username: slug });if (!result.ok) return result;- const d = result.data;+ const d = fields.parse(result.data);return {ok: true,data: {slug,profileUrl: `https://www.linkedin.com/in/${slug}`,- fullName: str(d.fullName),- firstName: str(d.firstName),- lastName: str(d.lastName),- headline: str(d.headline),- location: str(d.location),- urn: str(d.urn),- followerCount: int(d.followerCount),- connectionsCount: int(d.connectionsCount),+ fullName: text.parse(d.fullName),+ firstName: text.parse(d.firstName),+ lastName: text.parse(d.lastName),+ headline: text.parse(d.headline),+ location: text.parse(d.location),+ urn: text.parse(d.urn),+ followerCount: count.parse(d.followerCount),+ connectionsCount: count.parse(d.connectionsCount),photoUrl: profilePhotoUrl(d),- positions: (d.CurrentPositions ?? []).flatMap((p) =>- p?.name ? [{ name: p.name, url: str(p.url) }] : [],- ),+ positions: (rows.parse(d.CurrentPositions) ?? []).flatMap((entry) => {+ const position = fields.parse(entry);+ const name = rawText.parse(position.name);+ return name ? [{ name, url: text.parse(position.url) }] : [];+ }),},};}@@ -94,70 +121,73 @@ export async function getProfile(slug: string): Promise<Outcome<Profile>> {export async function getExperience(urn: string,): Promise<Outcome<Experience[]>> {- const result = await call<RawExperience>("/api/v1/profile/full-experience", {- urn,- });+ const result = await call("/api/v1/profile/full-experience", { urn });if (!result.ok) return result;- const payload = result.data;- const rows: RawExperienceRow[] = Array.isArray(payload)- ? payload- : (payload.experience ?? payload.experiences ?? []);+ return { ok: true, data: experienceRows(result.data).map(toExperience) };+}++function experienceRows(payload: Json): Json[] {+ if (Array.isArray(payload)) return payload;+ const envelope = experienceEnvelope.parse(payload);+ return envelope.experience ?? envelope.experiences ?? [];+}++function toExperience(row: Json): Experience {+ const entry = fields.parse(row);return {- ok: true,- data: rows.map(- (row): Experience => ({- title: str(row?.title),- company: str(row?.companyName ?? row?.company),- dateRange: str(row?.dateRange ?? row?.duration),- location: str(row?.location),- }),- ),+ title: text.parse(entry.title),+ company: text.parse(entry.companyName ?? entry.company),+ dateRange: text.parse(entry.dateRange ?? entry.duration),+ location: text.parse(entry.location),};}export async function lookupCompany(query: string,): Promise<Outcome<{ id: string; displayName: string }[]>> {- const result = await call<RawLookup>("/api/v1/companies/name-lookup", {- query,- });+ const result = await call("/api/v1/companies/name-lookup", { query });if (!result.ok) return result;+ const companies = companyLookup.parse(result.data).companies ?? [];return {ok: true,- data: (result.data.companies ?? []).flatMap((c) =>- c?.id ? [{ id: c.id, displayName: c.displayName ?? c.id }] : [],- ),+ data: companies.flatMap((entry) => {+ const company = fields.parse(entry);+ const id = rawText.parse(company.id);+ return id+ ? [{ id, displayName: rawText.parse(company.displayName) ?? id }]+ : [];+ }),};}export async function getCompany(nameOrId: string): Promise<Outcome<Company>> {const numeric = /^\d+$/.test(nameOrId);- const result = await call<RawCompany>("/api/v1/companies/company/info", {+ const result = await call("/api/v1/companies/company/info", {[numeric ? "id" : "name"]: nameOrId,});if (!result.ok) return result;- const d = result.data;+ const d = fields.parse(result.data);return {ok: true,data: {- id: str(d.id),- name: str(d.name),- universalName: str(d.universalName),- tagline: str(d.tagline),- description: str(d.description),- linkedinUrl: str(d.linkedinUrl),+ id: text.parse(d.id),+ name: text.parse(d.name),+ universalName: text.parse(d.universalName),+ tagline: text.parse(d.tagline),+ description: text.parse(d.description),+ linkedinUrl: text.parse(d.linkedinUrl),},};}-async function call<T>(+async function call(path: string,params: Record<string, string>,-): Promise<Outcome<T>> {+): Promise<Outcome<Json>> {const apiKey = key();if (!apiKey) return { ok: false, missing: false, reason: "No RAPIDAPI_KEY." };@@ -177,44 +207,29 @@ async function call<T>(return { ok: false, missing: false, reason: `HTTP ${response.status}` };}- const body = (await response.json()) as {- success?: boolean;- data?: T | null;- };+ const body = envelope.parse(await response.json());- if (body.success !== true || body.data == null) {+ if (body.success !== true || body.data === null) {return { ok: false, missing: true };}return { ok: true, data: body.data };- } catch (error) {- const aborted = error instanceof Error && error.name === "AbortError";+ } catch (cause) {+ const aborted = cause instanceof Error && cause.name === "AbortError";return {ok: false,missing: false,reason: aborted? `Timed out after ${TIMEOUT_MS}ms.`- : error instanceof Error- ? error.message- : String(error),+ : cause instanceof Error+ ? cause.message+ : String(cause),};} finally {clearTimeout(timer);}}-type RawProfile = {- fullName?: unknown;- firstName?: unknown;- lastName?: unknown;- headline?: unknown;- location?: unknown;- urn?: unknown;- followerCount?: unknown;- connectionsCount?: unknown;- CurrentPositions?: { name?: string; url?: unknown }[] | null;-} & Record<string, unknown>;-const PHOTO_KEYS = ["profilePictureURL","profilePicture",@@ -229,8 +244,8 @@ const PHOTO_KEYS = ["avatar",];-export function profilePhotoUrl(raw: Record<string, unknown>): string | null {- const byLowerKey = new Map<string, unknown>();+export function profilePhotoUrl(raw: LinkedinFields): string | null {+ const byLowerKey = new Map<string, Json>();for (const [key, value] of Object.entries(raw)) {byLowerKey.set(key.toLowerCase(), value);}@@ -251,9 +266,7 @@ export function profilePhotoUrl(raw: Record<string, unknown>): string | null {return null;}-function firstUrl(value: unknown): string | null {- if (typeof value === "string") return str(value);-+function firstUrl(value: Json | undefined): string | null {if (Array.isArray(value)) {for (const entry of [...value].reverse()) {const found = firstUrl(entry);@@ -262,43 +275,16 @@ function firstUrl(value: unknown): string | null {return null;}- if (value && typeof value === "object") {- const record = value as Record<string, unknown>;+ const direct = text.parse(value);+ if (direct) return direct;++ const nested = optionalFields.parse(value);+ if (nested) {for (const key of ["url", "displayUrl", "src", "large", "original"]) {- const found = firstUrl(record[key]);+ const found = firstUrl(nested[key]);if (found) return found;}}return null;}--type RawExperienceRow = {- title?: unknown;- companyName?: unknown;- company?: unknown;- dateRange?: unknown;- duration?: unknown;- location?: unknown;-};--type RawExperience =- | RawExperienceRow[]- | { experience?: RawExperienceRow[]; experiences?: RawExperienceRow[] };-type RawLookup = { companies?: { id?: string; displayName?: string }[] | null };-type RawCompany = {- id?: unknown;- name?: unknown;- universalName?: unknown;- tagline?: unknown;- description?: unknown;- linkedinUrl?: unknown;-};--function str(value: unknown): string | null {- return typeof value === "string" && value.trim() ? value.trim() : null;-}--function int(value: unknown): number | null {- return typeof value === "number" && Number.isFinite(value) ? value : null;-}diff --git a/apps/agent/agent/lib/perplexity.ts b/apps/agent/agent/lib/perplexity.tsindex 4f1c235d..1e5acca4 100644--- a/apps/agent/agent/lib/perplexity.ts+++ b/apps/agent/agent/lib/perplexity.ts@@ -44,7 +44,7 @@ export async function ask(: []),{ role: "user", content: question },],- ...(options.domains ? { search_domain_filter: options.domains } : {}),+ search_domain_filter: options.domains,}),});diff --git a/apps/agent/agent/lib/portrait-sources.ts b/apps/agent/agent/lib/portrait-sources.tsindex 269e92e5..d56ea6f9 100644--- a/apps/agent/agent/lib/portrait-sources.ts+++ b/apps/agent/agent/lib/portrait-sources.ts@@ -1,4 +1,5 @@-import { extract } from "./context-dev";+import { z } from "zod";+import { extract, type JsonSchema } from "./context-dev";import { getProfile, slugFromProfileUrl } from "./linkdapi";import { namesMatch } from "./names";@@ -71,7 +72,7 @@ export async function findPortrait(return { found: false, tried };}-const TEAM_SCHEMA = {+const TEAM_SCHEMA: JsonSchema = {type: "object",properties: {people: {@@ -93,6 +94,21 @@ const TEAM_SCHEMA = {required: ["people"],};+const teamPage = z+ .object({+ people: z+ .array(+ z+ .object({+ name: z.string().nullable().catch(null),+ photoUrl: z.string().nullable().catch(null),+ })+ .catch({ name: null, photoUrl: null }),+ )+ .catch([]),+ })+ .catch({ people: [] });+async function fromEmployerSite(subject: PortraitSubject,): Promise<PortraitCandidate | null> {@@ -106,20 +122,13 @@ async function fromEmployerSite(if (result.outcome !== "found") return null;- const people = (result.data as { people?: unknown } | null)?.people;- if (!Array.isArray(people)) return null;-- for (const entry of people) {- if (!entry || typeof entry !== "object") continue;- const row = entry as Record<string, unknown>;-- const name = typeof row.name === "string" ? row.name : null;- const photo = typeof row.photoUrl === "string" ? row.photoUrl : null;- if (!name || !photo) continue;+ for (const person of teamPage.parse(result.data).people) {+ const { name, photoUrl } = person;+ if (!name || !photoUrl) continue;if (!namesMatch(name, subject.name)) continue;try {- const parsed = new URL(photo);+ const parsed = new URL(photoUrl);if (parsed.protocol !== "https:" && parsed.protocol !== "http:") continue;return { source: "employer-site", url: parsed.toString() };} catch {}diff --git a/apps/agent/agent/lib/run-preflight.ts b/apps/agent/agent/lib/run-preflight.tsindex c1d71468..8a89e34b 100644--- a/apps/agent/agent/lib/run-preflight.ts+++ b/apps/agent/agent/lib/run-preflight.ts@@ -1,22 +1,25 @@import { db } from "@crm/db";-import { actionDependency } from "./agent-actions";import {type AgentManifest,InvalidAgentManifest,parseAgentManifest,-} from "./agent-manifest";+} from "@crm/validation/agent-manifest";+import {+ type AgentActionDependencyId,+ actionDependency,+} from "./agent-actions";import { slackConnected } from "./slack-connection";export const DEPENDENCY_UNAVAILABLE = "DEPENDENCY_UNAVAILABLE";-const CHECKS: Record<string, () => Promise<boolean>> = {+const CHECKS = {slack: slackConnected,-};+} satisfies Record<AgentActionDependencyId, () => Promise<boolean>>;export async function missingRunDependencies(manifest: AgentManifest,): Promise<string[]> {- const required = new Map<string, string>();+ const required = new Map<AgentActionDependencyId, string>();for (const action of manifest.actions) {const dependency = actionDependency(action.type);@@ -25,8 +28,7 @@ export async function missingRunDependencies(const missing: string[] = [];for (const [id, fix] of required) {- const check = CHECKS[id];- if (check && !(await check())) missing.push(fix);+ if (!(await CHECKS[id]())) missing.push(fix);}return missing;diff --git a/apps/agent/agent/lib/run-runtime.ts b/apps/agent/agent/lib/run-runtime.tsindex 75262e01..bc9d9c2f 100644--- a/apps/agent/agent/lib/run-runtime.ts+++ b/apps/agent/agent/lib/run-runtime.ts@@ -2,13 +2,14 @@ import { createHash, randomUUID } from "node:crypto";import { ActivityType, db, type Prisma } from "@crm/db";import type { AgentActionStatus, AgentTriggerType } from "@crm/db/enums";import { lockIdempotencyKey } from "@crm/db/idempotency";-import { readCompanyHistory, readDealHistory } from "./accounts";import {- AGENT_ACTION_EXECUTORS,AGENT_ACTION_TYPES,- isAgentActionType,-} from "./agent-actions";-import { parseAgentManifest } from "./agent-manifest";+ type AgentManifestResource,+ parseAgentManifest,+} from "@crm/validation/agent-manifest";+import { z } from "zod";+import { readCompanyHistory, readDealHistory } from "./accounts";+import { AGENT_ACTION_EXECUTORS, isAgentActionType } from "./agent-actions";import { readCrmHistory } from "./crm";import { DISPATCH } from "./dispatch-config";import { searchCrm } from "./lookup";@@ -24,13 +25,64 @@ const NO_ACTION_TRIGGER_TYPES = new Set<AgentTriggerType>(DISPATCH.run.noActionTriggerTypes,);-type RunResource = {- kind: "integration" | "company" | "contact" | "deal";+type RunRecordScope = "SELECTED" | "WORKSPACE";++const json = z.json();++type Json = z.infer<typeof json>;++const runResult = z.record(z.string(), json);++export type RunResult = z.infer<typeof runResult>;++const storedRunResult = runResult.catch({});++export type SlackRunDestination = {+ kind: "channel" | "user";id: string;label: string;};-type RunRecordScope = "SELECTED" | "WORKSPACE";+type RunDataScope = {+ mode: RunRecordScope;+ resources: AgentManifestResource[];+};++export type RunHistorySources = {+ gmail: boolean;+ calendar: boolean;+};++type SlackRequestBody = Record<string, Json>;++type HashableRequest = Record<string, string | boolean | null>;++const optionalText = z.string().nullable().catch(null);++const slackActionMetadata = z+ .object({ clientMessageId: z.string().min(1).nullable().catch(null) })+ .catch({ clientMessageId: null });++const slackEnvelope = z+ .object({ ok: z.boolean().nullable().catch(null), error: optionalText })+ .catch({ ok: null, error: null });++const slackOpenedConversation = z+ .object({+ channel: z+ .object({ id: z.string().min(1).nullable().catch(null) })+ .nullable()+ .catch(null),+ })+ .catch({ channel: null });++const slackPostedMessage = z+ .object({ channel: optionalText, ts: optionalText })+ .catch({ channel: null, ts: null });++const noActionResult = z+ .object({ noActionNeeded: optionalText })+ .catch({ noActionNeeded: null });type RunActionRow = {id: string;@@ -381,8 +433,8 @@ export async function postRunSlackMessage(const { actionId, claimedAt } = claim;try {await assertRunActive(runId);- const clientMessageId = recordOf(claim.metadata).clientMessageId;- if (typeof clientMessageId !== "string" || !clientMessageId) {+ const { clientMessageId } = slackActionMetadata.parse(claim.metadata);+ if (!clientMessageId) {throw new Error("This Slack action is missing its replay key.");}const accessToken = await slackAccessToken();@@ -540,7 +592,7 @@ async function failRunAction(export async function sendSlackMessage(accessToken: string,- destination: { kind: "channel" | "user"; id: string; label: string },+ destination: SlackRunDestination,text: string,clientMessageId: string,options: {@@ -552,37 +604,40 @@ export async function sendSlackMessage(const { fetcher = fetch, abortSignal, beforePost } = options;let channel = destination.id;if (destination.kind === "user") {- const opened = await slackApiRequest(- fetcher,- accessToken,- "conversations.open",- { users: destination.id, return_im: true },- abortSignal,+ const opened = slackOpenedConversation.parse(+ await slackApiRequest(+ fetcher,+ accessToken,+ "conversations.open",+ { users: destination.id, return_im: true },+ abortSignal,+ ),);- const conversation = recordOf(opened.channel);- if (typeof conversation.id !== "string" || !conversation.id) {+ if (!opened.channel?.id) {throw new Error("Slack did not return a direct-message channel.");}- channel = conversation.id;+ channel = opened.channel.id;}await beforePost?.();- const data = await slackApiRequest(- fetcher,- accessToken,- "chat.postMessage",- {- channel,- text,- client_msg_id: clientMessageId,- },- abortSignal,+ const posted = slackPostedMessage.parse(+ await slackApiRequest(+ fetcher,+ accessToken,+ "chat.postMessage",+ {+ channel,+ text,+ client_msg_id: clientMessageId,+ },+ abortSignal,+ ),);- if (typeof data.channel !== "string" || typeof data.ts !== "string") {+ if (posted.channel === null || posted.ts === null) {throw new Error("Slack returned an incomplete message receipt.");}- return { channel: data.channel, ts: data.ts };+ return { channel: posted.channel, ts: posted.ts };}async function assertRunActive(runId: string): Promise<void> {@@ -641,9 +696,9 @@ async function slackApiRequest(fetcher: typeof fetch,accessToken: string,method: string,- body: Record<string, unknown>,+ body: SlackRequestBody,abortSignal?: AbortSignal,-): Promise<Record<string, unknown>> {+): Promise<Json> {const response = await fetcher(`https://slack.com/api/${method}`, {method: "POST",headers: {@@ -655,9 +710,10 @@ async function slackApiRequest(});if (!response.ok) throw new Error("Slack message delivery failed.");- const data = recordOf(await response.json());- if (data.ok !== true) {- const reason = typeof data.error === "string" ? data.error : "rejected";+ const data = json.catch(null).parse(await response.json());+ const envelope = slackEnvelope.parse(data);+ if (envelope.ok !== true) {+ const reason = envelope.error ?? "rejected";if (reason === "not_in_channel") {throw new Error("The Slack bot is not in the selected channel. Invite the app to that channel and retry the run.",@@ -684,7 +740,7 @@ export async function stageRunResult(runId: string,input: {summary: string;- result?: Record<string, unknown> | null;+ result?: RunResult | null;noActionNeeded?: { reason: string } | null;},) {@@ -698,12 +754,10 @@ export async function stageRunResult(if (refusal) throw new Error(refusal);}- const result = {- ...(input.result ?? {}),- ...(input.noActionNeeded- ? { noActionNeeded: input.noActionNeeded.reason }- : {}),- };+ const result: RunResult = { ...(input.result ?? {}) };+ if (input.noActionNeeded) {+ result.noActionNeeded = input.noActionNeeded.reason;+ }await tx.agentRun.update({where: { id: runId },@@ -717,18 +771,19 @@ export async function stageRunResult(});}-export function runReportedNoActionNeeded(result: unknown): boolean {- return (- typeof result === "object" &&- result !== null &&- !Array.isArray(result) &&- typeof (result as Record<string, unknown>).noActionNeeded === "string"- );+export function runResultOf(value: Prisma.JsonValue): RunResult {+ return storedRunResult.parse(value);+}++export function runReportedNoActionNeeded(+ result: RunResult | null | undefined,+): boolean {+ return noActionResult.parse(result).noActionNeeded !== null;}export async function finishRun(runId: string,- input: { summary: string; result?: Record<string, unknown> | null },+ input: { summary: string; result?: RunResult | null },) {return db.$transaction(async (tx) => {const run = await lockAgentRun(tx, runId);@@ -834,7 +889,7 @@ async function requiredActionFailure(});for (const action of external) {- const type = typeof action.type === "string" ? action.type : "unknown";+ const type = action.type;const rows = recorded.filter((row) => row.type === type);if (rows.some((row) => row.status === "SUCCEEDED")) continue;@@ -857,10 +912,7 @@ async function requiredActionFailure(type,provider:type === AGENT_ACTION_TYPES.SLACK_MESSAGE_POST ? "slack" : "crm",- summary:- typeof action.summary === "string"- ? action.summary- : `Perform ${type}`,+ summary: action.summary,status: "FAILED",idempotencyKey: `run:${run.id}:required:${type}`,requestHash: hashRequest({ type, required: true }),@@ -928,12 +980,9 @@ async function failLockedRun(return { id: run.id, status: "FAILED" as const };}-function manifestDataScope(value: unknown): {- mode: RunRecordScope;- resources: RunResource[];-} {+function manifestDataScope(value: Prisma.JsonValue): RunDataScope {const scope = parseAgentManifest(value).dataScope;- const resources = scope.resources as RunResource[];+ const resources = scope.resources;const records = resources.filter((resource) => resource.kind !== "integration",);@@ -946,24 +995,23 @@ function manifestDataScope(value: unknown): {return { mode: scope.mode, resources };}-function manifestActions(value: unknown) {+function manifestActions(value: Prisma.JsonValue) {return parseAgentManifest(value).actions;}-function externalManifestActions(value: unknown) {+function externalManifestActions(value: Prisma.JsonValue) {return manifestActions(value).filter((action) => action.type !== AGENT_ACTION_TYPES.RUN_SUMMARY,);}function assertActivityAllowed(- manifest: unknown,+ manifest: Prisma.JsonValue,activityType: "NOTE" | "TASK",) {const allowed = manifestActions(manifest).some((action) =>- action.type === "crm.activity.create" &&- Array.isArray(action.activityTypes) &&+ action.type === AGENT_ACTION_TYPES.CRM_ACTIVITY_CREATE &&action.activityTypes.includes(activityType),);if (!allowed) {@@ -973,11 +1021,9 @@ function assertActivityAllowed(}}-export function approvedSlackDestination(manifest: unknown): {- kind: "channel" | "user";- id: string;- label: string;-} {+export function approvedSlackDestination(+ manifest: Prisma.JsonValue,+): SlackRunDestination {const scope = manifestDataScope(manifest);if (!scope.resources.some(@@ -988,26 +1034,17 @@ export function approvedSlackDestination(manifest: unknown): {throw new Error("Agent version does not allow Slack.");}- const destinations = manifestActions(manifest).flatMap((action) => {- if (action.type !== "slack.message.post") return [];- const destination = recordOf(action.destination);- if (- !["channel", "user"].includes(String(destination.kind)) ||- typeof destination.id !== "string" ||- !destination.id ||- typeof destination.label !== "string" ||- !destination.label- ) {- return [];- }- return [- {- kind: destination.kind as "channel" | "user",- id: destination.id,- label: destination.label,- },- ];- });+ const destinations = manifestActions(manifest).flatMap((action) =>+ action.type === AGENT_ACTION_TYPES.SLACK_MESSAGE_POST+ ? [+ {+ kind: action.destination.kind,+ id: action.destination.id,+ label: action.destination.label,+ },+ ]+ : [],+ );const [destination] = destinations;if (!destination || destinations.length !== 1) {throw new Error(@@ -1020,7 +1057,7 @@ export function approvedSlackDestination(manifest: unknown): {function assertResourceAllowed(mode: RunRecordScope,- resources: RunResource[],+ resources: AgentManifestResource[],input: { kind: "contact" | "company" | "deal"; id: string },) {if (mode === "WORKSPACE") return;@@ -1039,10 +1076,9 @@ function assertResourceAllowed();}-export function allowedHistorySources(resources: RunResource[]): {- gmail: boolean;- calendar: boolean;-} {+export function allowedHistorySources(+ resources: AgentManifestResource[],+): RunHistorySources {const integrations = new Set(resources.filter((resource) => resource.kind === "integration")@@ -1100,12 +1136,6 @@ async function targetRecord(kind: "company" | "contact" | "deal", id: string) {: null;}-function recordOf(value: unknown): Record<string, unknown> {- return value && typeof value === "object" && !Array.isArray(value)- ? (value as Record<string, unknown>)- : {};-}-function actionRequestHash(input: {type: "NOTE" | "TASK";targetKind: "company" | "contact" | "deal";@@ -1124,7 +1154,7 @@ function actionRequestHash(input: {});}-function hashRequest(input: Record<string, unknown>): string {+function hashRequest(input: HashableRequest): string {return createHash("sha256").update(JSON.stringify(input)).digest("hex");}diff --git a/apps/agent/agent/lib/session-purpose.ts b/apps/agent/agent/lib/session-purpose.tsindex 2b85f12d..f3b29288 100644--- a/apps/agent/agent/lib/session-purpose.ts+++ b/apps/agent/agent/lib/session-purpose.ts@@ -1,18 +1,24 @@+import { z } from "zod";+export type SessionPurpose = "builder" | "team-agent" | "research";+type SessionAttributes = Readonly<Record<string, string | readonly string[]>>;+type PurposeContext = {readonly session: {readonly auth: {readonly current: {- readonly attributes: Readonly<Record<string, unknown>>;+ readonly attributes: SessionAttributes;} | null;readonly initiator: {- readonly attributes: Readonly<Record<string, unknown>>;+ readonly attributes: SessionAttributes;} | null;};};};+const attributeText = z.string().trim().min(1).nullable().catch(null);+export function purposeOf(ctx: PurposeContext): SessionPurpose {const purpose = attribute(ctx, "purpose");if (purpose === "builder" || purpose === "team-agent") return purpose;@@ -20,13 +26,10 @@ export function purposeOf(ctx: PurposeContext): SessionPurpose {}export function attribute(ctx: PurposeContext, key: string): string | null {- const current = ctx.session.auth.current?.attributes[key];- if (typeof current === "string" && current.trim()) return current.trim();-- const initiator = ctx.session.auth.initiator?.attributes[key];- return typeof initiator === "string" && initiator.trim()- ? initiator.trim()- : null;+ return (+ attributeText.parse(ctx.session.auth.current?.attributes[key]) ??+ attributeText.parse(ctx.session.auth.initiator?.attributes[key])+ );}export function requireAttribute(ctx: PurposeContext, key: string): string {diff --git a/apps/agent/agent/lib/slack-join-task.ts b/apps/agent/agent/lib/slack-join-task.tsindex b2482bab..27d29c65 100644--- a/apps/agent/agent/lib/slack-join-task.ts+++ b/apps/agent/agent/lib/slack-join-task.ts@@ -1,7 +1,10 @@+import type { Prisma } from "@crm/db";import { parse, schemas } from "@crm/validation";import { joinSlackChannel } from "./slack-membership";-export async function runSlackChannelJoin(value: unknown): Promise<string> {+export async function runSlackChannelJoin(+ value: Prisma.JsonValue,+): Promise<string> {const { channelId, channelName } = parse(schemas.slack.joinPayload,value,diff --git a/apps/agent/agent/lib/socials.ts b/apps/agent/agent/lib/socials.tsindex 2364ebd5..a59e5906 100644--- a/apps/agent/agent/lib/socials.ts+++ b/apps/agent/agent/lib/socials.ts@@ -1,3 +1,4 @@+import { z } from "zod";import type { Evidence } from "./evidence";import {looksLikeSameCompany,@@ -6,6 +7,27 @@ import {} from "./names";import { ask } from "./perplexity";+const text = z.string().trim().min(1).nullable().catch(null);+const rawText = z.string().nullable().catch(null);++const githubAccount = z+ .object({+ login: rawText,+ name: text,+ company: text,+ blog: text,+ bio: text,+ type: rawText,+ })+ .catch({+ login: null,+ name: null,+ company: null,+ blog: null,+ bio: null,+ type: null,+ });+export type Network = "x" | "github";export type SocialProfile = {@@ -159,18 +181,16 @@ async function fetchGithubUser(handle: string,): Promise<{ ok: true; user: GithubUser } | { ok: false; reason: string }> {const token = process.env.GITHUB_TOKEN;+ const headers = new Headers({+ accept: "application/vnd.github+json",+ "user-agent": "comp-ai-crm-research-agent",+ });+ if (token) headers.set("authorization", `Bearer ${token}`);try {const response = await fetch(`https://api.github.com/users/${encodeURIComponent(handle)}`,- {- headers: {- accept: "application/vnd.github+json",- "user-agent": "comp-ai-crm-research-agent",- ...(token ? { authorization: `Bearer ${token}` } : {}),- },- signal: AbortSignal.timeout(15_000),- },+ { headers, signal: AbortSignal.timeout(15_000) },);if (response.status === 404) {@@ -186,23 +206,23 @@ async function fetchGithubUser(return { ok: false, reason: `GitHub returned HTTP ${response.status}.` };}- const body = (await response.json()) as Record<string, unknown>;+ const account = githubAccount.parse(await response.json());return {ok: true,user: {- login: String(body.login ?? handle),- name: str(body.name),- company: str(body.company),- blog: str(body.blog),- bio: str(body.bio),- type: String(body.type ?? "User"),+ login: account.login ?? handle,+ name: account.name,+ company: account.company,+ blog: account.blog,+ bio: account.bio,+ type: account.type ?? "User",},};- } catch (error) {+ } catch (cause) {return {ok: false,- reason: error instanceof Error ? error.message : String(error),+ reason: cause instanceof Error ? cause.message : String(cause),};}}@@ -396,7 +416,3 @@ export async function findSocialCandidates(return { candidates, citations: answer.data.citations };}--function str(value: unknown): string | null {- return typeof value === "string" && value.trim() ? value.trim() : null;-}diff --git a/apps/agent/agent/lib/tasks.ts b/apps/agent/agent/lib/tasks.tsindex 89ab2d51..a29637f9 100644--- a/apps/agent/agent/lib/tasks.ts+++ b/apps/agent/agent/lib/tasks.ts@@ -94,7 +94,7 @@ export async function completeTask(data: {finishedAt: new Date(),outcome: outcome.slice(0, 500),- ...(sessionId ? { sessionId } : {}),+ sessionId: sessionId || undefined,},});diff --git a/apps/agent/agent/subagents/agent_builder/lib/draft-input.ts b/apps/agent/agent/subagents/agent_builder/lib/draft-input.tsindex 453e3cf3..67d1d926 100644--- a/apps/agent/agent/subagents/agent_builder/lib/draft-input.ts+++ b/apps/agent/agent/subagents/agent_builder/lib/draft-input.ts@@ -1,6 +1,6 @@import { CRM_EVENT_TYPES } from "@crm/db/crm-events";+import { AGENT_ACTION_TYPES } from "@crm/validation/agent-manifest";import { z } from "zod";-import { AGENT_ACTION_TYPES } from "../../../lib/agent-actions";import type { DraftAgentInput } from "../../../lib/builder-runtime";const recordResource = z.object({diff --git a/apps/agent/agent/subagents/agent_runner/tools/finish_run.ts b/apps/agent/agent/subagents/agent_runner/tools/finish_run.tsindex f182fa00..57e30143 100644--- a/apps/agent/agent/subagents/agent_runner/tools/finish_run.ts+++ b/apps/agent/agent/subagents/agent_runner/tools/finish_run.ts@@ -8,7 +8,7 @@ export default defineTool({"Finish this run successfully with its concise summary and structured result. Set noActionNeeded when the trigger fired but this run's condition was not met, so none of the declared actions applied — an agent that watches for something is expected to do nothing when that thing did not happen.",inputSchema: z.object({summary: z.string().trim().min(1).max(1000),- result: z.record(z.string(), z.unknown()).nullish(),+ result: z.record(z.string(), z.json()).nullish(),noActionNeeded: z.object({reason: z.string().trim().min(1).max(500),diff --git a/apps/agent/agent/tools/enrich_company.ts b/apps/agent/agent/tools/enrich_company.tsindex 58fc2acd..02502f5d 100644--- a/apps/agent/agent/tools/enrich_company.ts+++ b/apps/agent/agent/tools/enrich_company.ts@@ -24,9 +24,7 @@ export default defineTool({return {enriched: false as const,reason: result.reason,- ...(result.retryable === undefined- ? {}- : { retryable: result.retryable }),+ retryable: result.retryable,};}diff --git a/apps/agent/agent/tools/identify_contact.ts b/apps/agent/agent/tools/identify_contact.tsindex e31f7d28..c6953fd9 100644--- a/apps/agent/agent/tools/identify_contact.ts+++ b/apps/agent/agent/tools/identify_contact.ts@@ -44,7 +44,7 @@ export default defineTool({band: result.band,score: Number(result.score.toFixed(2)),rationale: result.rationale,- ...(result.reason ? { reason: result.reason } : {}),+ reason: result.reason || undefined,};},});diff --git a/apps/agent/agent/tools/record_fact.ts b/apps/agent/agent/tools/record_fact.tsindex 899400f2..efe6612d 100644--- a/apps/agent/agent/tools/record_fact.ts+++ b/apps/agent/agent/tools/record_fact.ts@@ -64,7 +64,7 @@ export default defineTool({band: result.band,score: Number(result.score.toFixed(2)),rationale: result.rationale,- ...(result.reason ? { reason: result.reason } : {}),+ reason: result.reason || undefined,};},});diff --git a/apps/agent/agent/tools/research_company.ts b/apps/agent/agent/tools/research_company.tsindex 05b09a71..fd6ea10c 100644--- a/apps/agent/agent/tools/research_company.ts+++ b/apps/agent/agent/tools/research_company.ts@@ -1,10 +1,10 @@import { ActivityType, db } from "@crm/db";import { defineTool } from "eve/tools";import { z } from "zod";-import { extract } from "../lib/context-dev";+import { extract, type JsonSchema } from "../lib/context-dev";import { spend } from "../lib/focus";-const RESEARCH_SCHEMA = {+const RESEARCH_SCHEMA: JsonSchema = {type: "object",properties: {positioning: {@@ -31,13 +31,43 @@ const RESEARCH_SCHEMA = {},},required: ["positioning"],-} as const;+};const RESEARCH_INSTRUCTIONS ="Read this company's marketing site and answer as a salesperson preparing " +"for a first call. Be specific and factual; leave a field empty rather than " +"guessing.";+const briefText = z.string().trim().min(1).nullable().catch(null);++const briefList = z+ .array(z.string().nullable().catch(null))+ .transform((items) => items.filter((item) => item !== null))+ .catch([]);++const briefScalar = z+ .union([z.string(), z.number(), z.boolean()])+ .nullable()+ .catch(null);++const researchBrief = z+ .object({+ positioning: briefText,+ pricingModel: briefText,+ targetCustomer: briefText,+ notableCustomers: briefList,+ recentNews: briefList,+ })+ .catch({+ positioning: null,+ pricingModel: null,+ targetCustomer: null,+ notableCustomers: [],+ recentNews: [],+ });++type ResearchBrief = z.infer<typeof researchBrief>;+export default defineTool({description:"Read a company's marketing site and write a research brief to its timeline: positioning, pricing, who they sell to, notable customers, recent news.",@@ -72,11 +102,7 @@ export default defineTool({const charge = spend(2);if (!charge.ok) return { written: false as const, reason: charge.reason };- const result = await extract(- url,- RESEARCH_SCHEMA as unknown as Record<string, unknown>,- RESEARCH_INSTRUCTIONS,- );+ const result = await extract(url, RESEARCH_SCHEMA, RESEARCH_INSTRUCTIONS);if (result.outcome === "failed") {return { written: false as const, reason: result.reason };@@ -90,11 +116,15 @@ export default defineTool({if (!author)return { written: false as const, reason: "No user to attribute to." };+ const scalar = briefScalar.parse(result.data);const activity = await db.activity.create({data: {type: ActivityType.ENRICHMENT,subject: `Research brief — ${company.name}`,- body: formatBrief(result.data),+ body:+ scalar === null+ ? formatBrief(researchBrief.parse(result.data))+ : String(scalar),occurredAt: new Date(),companyId: company.id,createdById: author,@@ -117,40 +147,21 @@ export default defineTool({},});-function formatBrief(data: unknown): string {- if (typeof data !== "object" || data === null) return String(data ?? "");-- const brief = data as {- positioning?: unknown;- pricingModel?: unknown;- targetCustomer?: unknown;- notableCustomers?: unknown;- recentNews?: unknown;- };-+function formatBrief(brief: ResearchBrief): string {const lines: string[] = [];- const text = (value: unknown) =>- typeof value === "string" && value.trim() ? value.trim() : null;- const list = (value: unknown) =>- Array.isArray(value)- ? value.filter((item): item is string => typeof item === "string")- : [];- const positioning = text(brief.positioning);- if (positioning) lines.push(positioning);+ if (brief.positioning) lines.push(brief.positioning);+ if (brief.pricingModel) lines.push(`Pricing: ${brief.pricingModel}`);+ if (brief.targetCustomer) lines.push(`Sells to: ${brief.targetCustomer}`);- const pricing = text(brief.pricingModel);- if (pricing) lines.push(`Pricing: ${pricing}`);-- const target = text(brief.targetCustomer);- if (target) lines.push(`Sells to: ${target}`);-- const customers = list(brief.notableCustomers);- if (customers.length > 0) lines.push(`Customers: ${customers.join(", ")}`);+ if (brief.notableCustomers.length > 0) {+ lines.push(`Customers: ${brief.notableCustomers.join(", ")}`);+ }- const news = list(brief.recentNews);- if (news.length > 0) {- lines.push(`Recently:\n${news.map((item) => `• ${item}`).join("\n")}`);+ if (brief.recentNews.length > 0) {+ lines.push(+ `Recently:\n${brief.recentNews.map((item) => `• ${item}`).join("\n")}`,+ );}return lines.join("\n\n");diff --git a/apps/agent/evals/agent-builder.eval.ts b/apps/agent/evals/agent-builder.eval.tsindex f32c620e..6612ee90 100644--- a/apps/agent/evals/agent-builder.eval.ts+++ b/apps/agent/evals/agent-builder.eval.ts@@ -1,4 +1,5 @@import { db } from "@crm/db";+import { agentManifest } from "@crm/validation/agent-manifest";import { defineEval } from "eve/evals";import { equals, satisfies } from "eve/evals/expect";@@ -62,8 +63,8 @@ export default defineEval({sessionId = await waitForBuilderSession(conversation.id, t.signal);await t.require(sessionId,- satisfies(- (value) => typeof value === "string",+ satisfies<string | null>(+ (value) => value !== null,"builder session started",),);@@ -98,15 +99,12 @@ export default defineEval({t.check(saved.agent?.versions[0]?.manifest,satisfies((value) => {- const manifest = recordOf(value);- const scope = recordOf(manifest.dataScope);- const actions = Array.isArray(manifest.actions)- ? manifest.actions.map(recordOf)- : [];+ const parsed = agentManifest.safeParse(value);return (- scope.mode === "WORKSPACE" &&- actions.length === 1 &&- actions[0]?.type === "run.summary"+ parsed.success &&+ parsed.data.dataScope.mode === "WORKSPACE" &&+ parsed.data.actions.length === 1 &&+ parsed.data.actions[0]?.type === "run.summary");}, "saved manifest is workspace-scoped and side-effect-free"),);@@ -166,9 +164,3 @@ async function cleanupBuilderEval(}await db.user.deleteMany({ where: { id: userId } });}--function recordOf(value: unknown): Record<string, unknown> {- return value && typeof value === "object" && !Array.isArray(value)- ? (value as Record<string, unknown>)- : {};-}diff --git a/apps/agent/scripts/backfill-brand-images.ts b/apps/agent/scripts/backfill-brand-images.tsindex 7b8d3168..8f5a9a3c 100644--- a/apps/agent/scripts/backfill-brand-images.ts+++ b/apps/agent/scripts/backfill-brand-images.ts@@ -1,8 +1,15 @@import { db } from "@crm/db";import { blobEnabled, isMirrored, mirror } from "@crm/db/blob";-import { COMPANY_IMAGE_FIELDS } from "@crm/db/images";+import { COMPANY_IMAGE_FIELDS, type CompanyImageField } from "@crm/db/images";+import { z } from "zod";import { brandToUpdate } from "../agent/lib/brand-mapping";+type CompanyImagePatch = Partial<+ Record<CompanyImageField | "iconTone", string>+>;++const storedText = z.string().nullable().catch(null);+if (!blobEnabled()) {console.warn("No BLOB_READ_WRITE_TOKEN — icon tone and dark artwork will still be " +@@ -54,14 +61,12 @@ for (const row of rows) {nameIsPlaceholder: false,});- const patch: Record<string, string> = {- ...(typeof update.iconDarkUrl === "string"- ? { iconDarkUrl: update.iconDarkUrl }- : {}),- ...(typeof update.iconTone === "string"- ? { iconTone: update.iconTone }- : {}),- };+ const patch: CompanyImagePatch = {};+ const iconDarkUrl = storedText.parse(update.iconDarkUrl);+ const iconTone = storedText.parse(update.iconTone);++ if (iconDarkUrl !== null) patch.iconDarkUrl = iconDarkUrl;+ if (iconTone !== null) patch.iconTone = iconTone;for (const slot of COMPANY_IMAGE_FIELDS) {const current = row.company[slot];diff --git a/apps/agent/test/channel-auth.spec.ts b/apps/agent/test/channel-auth.spec.tsindex 0108b5d8..06fa37ae 100644--- a/apps/agent/test/channel-auth.spec.ts+++ b/apps/agent/test/channel-auth.spec.ts@@ -9,11 +9,21 @@ import { isAutomated } from "../agent/lib/approval";const SECRET = "test-secret-at-least-long-enough-to-be-a-secret";const auth = repFromCrm(SECRET);-async function mint(- claims: Record<string, unknown>,- secret = SECRET,-): Promise<string> {- const encode = (value: object) =>+type BridgeHeader = { alg: string; typ: string };++type BridgeClaims = {+ iss: string;+ aud: string;+ sub: string | undefined;+ email: string;+ name: string;+ iat: number;+ nbf: number;+ exp: number;+};++async function mint(claims: BridgeClaims, secret = SECRET): Promise<string> {+ const encode = (value: BridgeClaims | BridgeHeader) =>Buffer.from(JSON.stringify(value)).toString("base64url");const signingInput = `${encode({ alg: "HS256", typ: "JWT" })}.${encode(claims)}`;@@ -40,7 +50,7 @@ function request(token: string | null): Request {});}-function claims(overrides: Record<string, unknown> = {}) {+function claims(overrides: Partial<BridgeClaims> = {}): BridgeClaims {const now = Math.floor(Date.now() / 1000);return {iss: BRIDGE_ISSUER,diff --git a/apps/agent/test/durable-agent-runtime.integration.spec.ts b/apps/agent/test/durable-agent-runtime.integration.spec.tsindex 669f10f7..27f74db7 100644--- a/apps/agent/test/durable-agent-runtime.integration.spec.ts+++ b/apps/agent/test/durable-agent-runtime.integration.spec.ts@@ -1,6 +1,7 @@import { afterAll, afterEach, beforeAll, describe, expect, it } from "bun:test";import { db } from "@crm/db";import type { SendFn } from "eve/channels";+import { z } from "zod";import audit from "../agent/hooks/audit";import {builderToken,@@ -15,9 +16,12 @@ import {import {createRunActivity,finishRun,+ runResultOf,stageRunResult,} from "../agent/lib/run-runtime";+const attachmentBytes = z.object({ data: z.instanceof(Uint8Array) });+const suffix = crypto.randomUUID();const userId = `durable-runtime-user-${suffix}`;const domain = `durable-${suffix}.example.test`;@@ -503,8 +507,8 @@ describe("durable custom-agent runtime", () => {select: { id: true, submissions: { select: { id: true } } },});builderConversationIds.push(conversation.id);- let delivered: unknown;- const send = (async (input: unknown) => {+ let delivered: Parameters<SendFn>[0] | null = null;+ const send = (async (input: Parameters<SendFn>[0]) => {delivered = input;return { id: `durable-session-${suffix}-attachment` };}) as unknown as SendFn;@@ -520,7 +524,7 @@ describe("durable custom-agent runtime", () => {mediaType: "text/plain",filename: "brief.txt",});- expect(Buffer.from(recordOf(parts[1]).data as Uint8Array)).toEqual(content);+ expect(Buffer.from(attachmentBytes.parse(parts[1]).data)).toEqual(content);});it("ingests a replayed Eve event and its usage exactly once", async () => {@@ -536,7 +540,7 @@ describe("durable custom-agent runtime", () => {session: {id: string;auth: {- current: { attributes: Record<string, unknown> };+ current: { attributes: Record<string, string> };initiator: null;};};@@ -590,7 +594,7 @@ describe("durable custom-agent runtime", () => {id: string;parent?: unknown;auth: {- current: { attributes: Record<string, unknown> };+ current: { attributes: Record<string, string> };initiator: null;};};@@ -689,7 +693,7 @@ describe("durable custom-agent runtime", () => {await satisfyRequiredActivity(run.id, "staged-success");await finishRun(run.id, {summary: staged.summary ?? "Staged safely",- result: staged.result as Record<string, unknown>,+ result: runResultOf(staged.result),});expect(await db.agentRun.findUniqueOrThrow({ where: { id: run.id } }),@@ -766,7 +770,7 @@ describe("durable custom-agent runtime", () => {const finished = await finishRun(run.id, {summary: staged.summary ?? "The deal was already closed",- result: staged.result as Record<string, unknown>,+ result: runResultOf(staged.result),});expect(finished).toEqual({ id: run.id, status: "SUCCEEDED" });expect(await db.agentAction.count({ where: { runId: run.id } })).toBe(0);@@ -848,9 +852,3 @@ describe("durable custom-agent runtime", () => {).toBe(0);});});--function recordOf(value: unknown): Record<string, unknown> {- return value && typeof value === "object" && !Array.isArray(value)- ? (value as Record<string, unknown>)- : {};-}diff --git a/apps/agent/test/e2e/dispatch.e2e.ts b/apps/agent/test/e2e/dispatch.e2e.tsindex 10e0db9e..6ae37376 100644--- a/apps/agent/test/e2e/dispatch.e2e.ts+++ b/apps/agent/test/e2e/dispatch.e2e.ts@@ -205,7 +205,7 @@ async function main() {} finally {const failure = await cleanUp(agentId, companyId, dealId, taskId).then(() => null,- (error: unknown) => reasonOf(error),+ (cause: unknown) => reasonOf(cause),);record("cleanup leaves no seeded rows",diff --git a/apps/agent/test/e2e/e2e-agents.ts b/apps/agent/test/e2e/e2e-agents.tsindex 5bb131ff..c70427d3 100644--- a/apps/agent/test/e2e/e2e-agents.ts+++ b/apps/agent/test/e2e/e2e-agents.ts@@ -49,6 +49,6 @@ export async function removeEventRuns(return runIds.length;}-export function reasonOf(error: unknown): string {- return error instanceof Error ? error.message : String(error);+export function reasonOf(cause: unknown): string {+ return cause instanceof Error ? cause.message : String(cause);}diff --git a/apps/agent/test/e2e/slack-join.e2e.ts b/apps/agent/test/e2e/slack-join.e2e.tsindex 5874daf2..be17cc4e 100644--- a/apps/agent/test/e2e/slack-join.e2e.ts+++ b/apps/agent/test/e2e/slack-join.e2e.ts@@ -97,8 +97,8 @@ async function main() {type: "slack.channel.join",channelId: E2E.slackJoin.bogusChannelId,channelName: E2E.slackJoin.bogusChannelName,- }).catch((error: unknown) =>- error instanceof Error ? error.message : String(error),+ }).catch((cause: unknown) =>+ cause instanceof Error ? cause.message : String(cause),);record("a channel that does not exist is refused",diff --git a/apps/agent/test/slack-people.integration.spec.ts b/apps/agent/test/slack-people.integration.spec.tsindex ce618b6a..91fc768d 100644--- a/apps/agent/test/slack-people.integration.spec.ts+++ b/apps/agent/test/slack-people.integration.spec.ts@@ -62,7 +62,21 @@ afterEach(async () => {}});-function slackReply(body: unknown): Response {+type SlackChannelReply = {+ id: string;+ name: string;+ is_member: boolean;+ unknown?: number;+};++type SlackListReply = {+ ok: boolean;+ error?: string;+ channels?: SlackChannelReply[];+ response_metadata?: { next_cursor: string };+};++function slackReply(body: SlackListReply): Response {return new Response(JSON.stringify(body), {status: 200,headers: { "content-type": "application/json" },@@ -187,7 +201,10 @@ describe("persistSlackChannels", () => {describe("refreshSlackChannels", () => {it("aborts a stalled Slack list request", async () => {let signal: AbortSignal | null = null;- globalThis.fetch = (async (_input: unknown, init?: RequestInit) => {+ globalThis.fetch = (async (+ _input: RequestInfo | URL,+ init?: RequestInit,+ ) => {signal = init?.signal ?? null;return slackReply({ ok: true, channels: [] });}) as typeof fetch;diff --git a/apps/api/src/activities/activities.service.ts b/apps/api/src/activities/activities.service.tsindex 23510a35..5fc2353b 100644--- a/apps/api/src/activities/activities.service.ts+++ b/apps/api/src/activities/activities.service.ts@@ -1,4 +1,5 @@import { ActivityType, type Db, type Prisma } from "@crm/db";+import { activityMeta } from "@crm/validation/activity-meta";import {BadRequestException,Injectable,@@ -80,7 +81,8 @@ export class ActivitiesService {const rows = await this.db.activity.findMany({where,take: input.limit + 1,- ...(input.cursor ? { cursor: { id: input.cursor }, skip: 1 } : {}),+ cursor: input.cursor ? { id: input.cursor } : undefined,+ skip: input.cursor ? 1 : undefined,orderBy: [{ occurredAt: { sort: "desc", nulls: "last" } },{ id: "desc" },@@ -273,7 +275,7 @@ function serializeEntry(entry: Entry) {dueAt: entry.dueAt?.toISOString() ?? null,completedAt: entry.completedAt?.toISOString() ?? null,createdAt: entry.createdAt.toISOString(),- meta: entry.meta as Record<string, unknown> | null,+ meta: activityMeta.parse(entry.meta),emailThread: entry.emailThread? {diff --git a/apps/api/src/agent/agent-definitions.service.ts b/apps/api/src/agent/agent-definitions.service.tsindex 9d6521fd..a211d571 100644--- a/apps/api/src/agent/agent-definitions.service.ts+++ b/apps/api/src/agent/agent-definitions.service.ts@@ -1,11 +1,13 @@import type { Db, Prisma } from "@crm/db";import type { AgentDefinitionStatus } from "@crm/db/enums";import { schemas } from "@crm/validation";+import { readAgentManifestSummary } from "@crm/validation/agent-manifest";import {BadRequestException,Injectable,NotFoundException,} from "@nestjs/common";+import { z } from "zod";import { InjectDatabase } from "../database/database.constants";import { AgentAccessService } from "./agent-access.service";import { AgentTriggerService } from "./agent-trigger.service";@@ -20,6 +22,18 @@ import {const INSTRUCTIONS_PATH = "agent/instructions.md";+type VersionMetadata = {+ name?: string;+ description?: string | null;+};++const capabilityName = z.string().nullable().catch(null);++const versionValidation = z+ .object({ capabilities: z.array(z.json()).catch([]) })+ .catchall(z.json())+ .catch({ capabilities: [] });+@Injectable()export class AgentDefinitionsService {constructor(@@ -131,6 +145,7 @@ export class AgentDefinitionsService {if (!row) throw new NotFoundException(`No agent with id ${id}.`);const { versions, ...agent } = row;+ const draft = versions[0];return {...agent,@@ -144,7 +159,10 @@ export class AgentDefinitionsService {deployedAt: agent.currentVersion.deployedAt?.toISOString() ?? null,}: null,- reviewVersion: agent.status === "DRAFT" ? (versions[0] ?? null) : null,+ reviewVersion:+ agent.status === "DRAFT" && draft+ ? { ...draft, manifest: readAgentManifestSummary(draft.manifest) }+ : null,triggers: agent.triggers.map((trigger) => ({...trigger,nextRunAt: trigger.nextRunAt?.toISOString() ?? null,@@ -152,7 +170,7 @@ export class AgentDefinitionsService {})),runCount: agent._count.runs,capabilities: readCapabilities(- agent.currentVersion?.manifest ?? versions[0]?.manifest,+ agent.currentVersion?.manifest ?? draft?.manifest,),};}@@ -793,28 +811,22 @@ export class AgentDefinitionsService {}}-function versionMetadata(manifest: unknown): {- name?: string;- description?: string | null;-} {- if (!manifest || typeof manifest !== "object" || Array.isArray(manifest)) {- return {};- }-- const record = manifest as Record<string, unknown>;- const name = typeof record.name === "string" ? record.name.trim() : "";+function versionMetadata(manifest: Prisma.JsonValue): VersionMetadata {+ const summary = readAgentManifestSummary(manifest);+ const name = summary.name?.trim() ?? "";const description =- typeof record.description === "string"- ? record.description.trim() || null- : undefined;+ summary.description === undefined+ ? undefined+ : summary.description.trim() || null;- return {- ...(name ? { name } : {}),- ...(description !== undefined ? { description } : {}),- };+ const metadata: VersionMetadata = {};+ if (name) metadata.name = name;+ if (description !== undefined) metadata.description = description;++ return metadata;}-function readCapabilities(manifest: unknown) {+function readCapabilities(manifest: Prisma.JsonValue | undefined) {const parsed = schemas.agents.capabilities.safeParse(manifest);if (!parsed.success) {@@ -857,28 +869,24 @@ function reviseSummary(input: {}function reviseValidation(- validation: unknown,+ validation: Prisma.JsonValue,before: string[],after: string[],): Prisma.InputJsonValue {const removed = before.filter((type) => !after.includes(type));- const base =- validation && typeof validation === "object" && !Array.isArray(validation)- ? (validation as Record<string, unknown>)- : {};+ const base = versionValidation.parse(validation);- const capabilities = Array.isArray(base.capabilities)- ? base.capabilities.filter(- (entry) => typeof entry === "string" && !removed.includes(entry),- )- : [];+ const capabilities = base.capabilities.flatMap((entry) => {+ const name = capabilityName.parse(entry);+ return name !== null && !removed.includes(name) ? [name] : [];+ });return {...base,status: "passed",checkedAt: new Date().toISOString(),capabilities,- } as Prisma.InputJsonValue;+ };}async function nextVersionNumber(diff --git a/apps/api/src/agent/agent-queue.service.ts b/apps/api/src/agent/agent-queue.service.tsindex 2089400d..e1bfa2c8 100644--- a/apps/api/src/agent/agent-queue.service.ts+++ b/apps/api/src/agent/agent-queue.service.ts@@ -1,4 +1,4 @@-import type { Db } from "@crm/db";+import type { Db, Prisma } from "@crm/db";import { Injectable } from "@nestjs/common";import { InjectDatabase } from "../database/database.constants";@@ -18,12 +18,12 @@ export class AgentQueueService {companyId?: string;contactId?: string;}): Promise<boolean> {+ const where: Prisma.AgentTaskWhereInput = { finishedAt: null };+ if (subject.companyId) where.companyId = subject.companyId;+ if (subject.contactId) where.contactId = subject.contactId;+const row = await this.db.agentTask.findFirst({- where: {- finishedAt: null,- ...(subject.companyId ? { companyId: subject.companyId } : {}),- ...(subject.contactId ? { contactId: subject.contactId } : {}),- },+ where,select: { id: true },});diff --git a/apps/api/src/agent/agent-trigger.service.ts b/apps/api/src/agent/agent-trigger.service.tsindex 1d5928dd..4b800513 100644--- a/apps/api/src/agent/agent-trigger.service.ts+++ b/apps/api/src/agent/agent-trigger.service.ts@@ -316,11 +316,13 @@ export class AgentTriggerService {finishedAt: null,[subject]: { in: ids },},- select: { [subject]: true },+ select: { companyId: true, contactId: true },});const taken = new Set(- outstanding.map((row) => (row as Record<string, unknown>)[subject]),+ outstanding.map((row) =>+ subject === "contactId" ? row.contactId : row.companyId,+ ),);const fresh = ids.filter((id) => !taken.has(id));diff --git a/apps/api/src/agent/research-key.service.ts b/apps/api/src/agent/research-key.service.tsindex fd9e3edb..83808cf8 100644--- a/apps/api/src/agent/research-key.service.ts+++ b/apps/api/src/agent/research-key.service.ts@@ -1,8 +1,16 @@import { Injectable, Logger } from "@nestjs/common";+import { z } from "zod";import { bridge } from "./bridge";const VERIFY_TIMEOUT_MS = 20_000;+const verifyAnswer = z+ .object({+ outcome: z.string().nullable().catch(null),+ reason: z.string().nullable().catch(null),+ })+ .catch({ outcome: null, reason: null });+export type KeyCheck =| { outcome: "valid" }| { outcome: "invalid"; reason: string }@@ -44,26 +52,18 @@ export class ResearchKeyService {return this.cannotTell(`The agent answered ${response.status}.`);}- const body = (await response.json()) as {- outcome?: string;- reason?: string;- };+ const body = verifyAnswer.parse(await response.json());if (body.outcome === "valid") return { outcome: "valid" };if (body.outcome === "invalid") {return {outcome: "invalid",- reason:- typeof body.reason === "string" && body.reason- ? body.reason- : "Context did not recognise that API key.",+ reason: body.reason || "Context did not recognise that API key.",};}- return this.cannotTell(- typeof body.reason === "string" ? body.reason : "No answer.",- );+ return this.cannotTell(body.reason ?? "No answer.");} catch (error) {return this.cannotTell(error instanceof Error ? error.message : String(error),diff --git a/apps/api/src/backfill/backfill.service.ts b/apps/api/src/backfill/backfill.service.tsindex 8f4b9607..303c8237 100644--- a/apps/api/src/backfill/backfill.service.ts+++ b/apps/api/src/backfill/backfill.service.ts@@ -273,12 +273,14 @@ export class BackfillService implements OnModuleInit {.map((row) => row.companyId).filter((id): id is string => id !== null);- return {+ const where: Prisma.CompanyWhereInput = {domain: { not: null },logoUrl: null,iconUrl: null,- ...(recentlyChecked.length > 0 ? { id: { notIn: recentlyChecked } } : {}),};+ if (recentlyChecked.length > 0) where.id = { notIn: recentlyChecked };++ return where;}/**@@ -311,15 +313,17 @@ export class BackfillService implements OnModuleInit {.map((row) => row.contactId).filter((id): id is string => id !== null);- return {+ const where: Prisma.ContactWhereInput = {imageUrl: null,- ...(recentlyChecked.length > 0 ? { id: { notIn: recentlyChecked } } : {}),OR: [{ linkedinUrl: { not: null } },{ githubUrl: { not: null } },{ company: { domain: { not: null } } },],};+ if (recentlyChecked.length > 0) where.id = { notIn: recentlyChecked };++ return where;}private contactsNeverResearched(): Prisma.ContactWhereInput {diff --git a/apps/api/src/backfill/image-mirror.service.ts b/apps/api/src/backfill/image-mirror.service.tsindex c6f46c85..1f295de7 100644--- a/apps/api/src/backfill/image-mirror.service.ts+++ b/apps/api/src/backfill/image-mirror.service.ts@@ -1,11 +1,27 @@import type { Db, Prisma } from "@crm/db";import { blobEnabled, mirror } from "@crm/db/blob";-import { BLOB_HOST_SUFFIX, COMPANY_IMAGE_FIELDS } from "@crm/db/images";+import {+ BLOB_HOST_SUFFIX,+ COMPANY_IMAGE_FIELDS,+ type CompanyImageField,+} from "@crm/db/images";import { Injectable, Logger } from "@nestjs/common";import { InjectDatabase } from "../database/database.constants";const MAX_PER_SWEEP = 25;+type CompanyImageRow = Record<CompanyImageField, string | null>;++const EXTERNAL_CONTACT_IMAGE: Prisma.ContactWhereInput = {+ imageUrl: { not: null },+ NOT: { imageUrl: { contains: BLOB_HOST_SUFFIX } },+};++const EXTERNAL_USER_IMAGE: Prisma.UserWhereInput = {+ image: { not: null },+ NOT: { image: { contains: BLOB_HOST_SUFFIX } },+};+export type ImageMirrorResult = {scanned: number;copied: number;@@ -44,7 +60,7 @@ export class ImageMirrorService {private async sweepCompanies(): Promise<ImageMirrorResult> {const rows = await this.db.company.findMany({where: {- OR: COMPANY_IMAGE_FIELDS.map((field) => external(field)),+ OR: COMPANY_IMAGE_FIELDS.map(externalCompanyImage),},orderBy: { createdAt: "asc" },take: MAX_PER_SWEEP,@@ -86,7 +102,7 @@ export class ImageMirrorService {private async sweepContacts(): Promise<ImageMirrorResult> {const rows = await this.db.contact.findMany({- where: external("imageUrl"),+ where: EXTERNAL_CONTACT_IMAGE,orderBy: { createdAt: "asc" },take: MAX_PER_SWEEP,select: { id: true, imageUrl: true },@@ -113,7 +129,7 @@ export class ImageMirrorService {private async sweepUsers(): Promise<ImageMirrorResult> {const rows = await this.db.user.findMany({- where: external("image"),+ where: EXTERNAL_USER_IMAGE,take: MAX_PER_SWEEP,select: { id: true, image: true },});@@ -138,20 +154,21 @@ export class ImageMirrorService {}}-function external<T extends string>(- field: T,-): Record<T, { not: null }> & { NOT: Record<T, { contains: string }> } {- return {- ...({ [field]: { not: null } } as Record<T, { not: null }>),- NOT: { [field]: { contains: BLOB_HOST_SUFFIX } } as Record<- T,- { contains: string }- >,- };+function externalCompanyImage(+ field: CompanyImageField,+): Prisma.CompanyWhereInput {+ const mirrored: Prisma.CompanyWhereInput = {};+ mirrored[field] = { contains: BLOB_HOST_SUFFIX };++ const where: Prisma.CompanyWhereInput = { NOT: mirrored };+ where[field] = { not: null };++ return where;}-function unchanged(row: Record<string, unknown>): Prisma.CompanyWhereInput {- return Object.fromEntries(- COMPANY_IMAGE_FIELDS.map((field) => [field, row[field] ?? null]),- );+function unchanged(row: CompanyImageRow): Prisma.CompanyWhereInput {+ const where: Prisma.CompanyWhereInput = {};+ for (const field of COMPANY_IMAGE_FIELDS) where[field] = row[field] ?? null;++ return where;}diff --git a/apps/api/src/companies/companies.service.ts b/apps/api/src/companies/companies.service.tsindex 80e79dc7..cc70757f 100644--- a/apps/api/src/companies/companies.service.ts+++ b/apps/api/src/companies/companies.service.ts@@ -29,6 +29,7 @@ import {FACET_ALL,FACET_UNASSIGNED,type ListResult,+ type OrderByColumns,ownerFilter,paginate,resolveOrderBy,@@ -75,10 +76,7 @@ export type CompanyRow = {fields: Record<string, string | number | boolean | null>;};-const SORTABLE: Record<- string,- (dir: Prisma.SortOrder) => Prisma.CompanyOrderByWithRelationInput-> = {+const SORTABLE: OrderByColumns<Prisma.CompanyOrderByWithRelationInput> = {name: (dir) => ({ name: dir }),domain: (dir) => ({ domain: dir }),industry: (dir) => ({ industry: dir }),@@ -606,17 +604,17 @@ export class CompaniesService {};}- private translate(error: unknown, id: string): unknown {- if (error instanceof PrismaNamespace.PrismaClientKnownRequestError) {- if (error.code === "P2025") {- return new NotFoundException(`No company with id ${id}.`);+ private translate(cause: unknown, id: string): never {+ if (cause instanceof PrismaNamespace.PrismaClientKnownRequestError) {+ if (cause.code === "P2025") {+ throw new NotFoundException(`No company with id ${id}.`);}- if (error.code === "P2002") {- return new ConflictException(+ if (cause.code === "P2002") {+ throw new ConflictException("Another company already uses that domain.",);}}- return error;+ throw cause;}}diff --git a/apps/api/src/config/env.validation.ts b/apps/api/src/config/env.validation.tsindex 2f81dc72..08cb676c 100644--- a/apps/api/src/config/env.validation.ts+++ b/apps/api/src/config/env.validation.ts@@ -128,9 +128,9 @@ export class EnvironmentVariables {CRM_TELEMETRY_DISABLED?: string;}-export function validateEnv(- config: Record<string, unknown>,-): EnvironmentVariables {+export type RawEnvironment = Record<string, string | undefined>;++export function validateEnv(config: RawEnvironment): EnvironmentVariables {const validated = plainToInstance(EnvironmentVariables, config, {enableImplicitConversion: true,exposeDefaultValues: true,diff --git a/apps/api/src/contacts/contacts.service.ts b/apps/api/src/contacts/contacts.service.tsindex 6518d060..fabdbd56 100644--- a/apps/api/src/contacts/contacts.service.ts+++ b/apps/api/src/contacts/contacts.service.ts@@ -29,6 +29,7 @@ import {FACET_ALL,FACET_UNASSIGNED,type ListResult,+ type OrderByColumns,ownerFilter,paginate,resolveOrderBy,@@ -61,7 +62,9 @@ const COMPANY_SELECT = {const NO_COMPANY = "none";-const FACT_COLUMNS: Record<string, string | undefined> = {+type FactColumns = Record<string, string | undefined>;++const FACT_COLUMNS: FactColumns = {title: "title",linkedinUrl: "linkedinUrl",twitterUrl: "twitterUrl",@@ -95,10 +98,7 @@ export type ContactRow = {fields: Record<string, string | number | boolean | null>;};-const SORTABLE: Record<- string,- (dir: Prisma.SortOrder) => Prisma.ContactOrderByWithRelationInput[]-> = {+const SORTABLE: OrderByColumns<Prisma.ContactOrderByWithRelationInput[]> = {name: (dir) => [{ lastName: dir }, { firstName: dir }],email: (dir) => [{ email: dir }],title: (dir) => [{ title: dir }, { lastName: "asc" }],@@ -403,7 +403,9 @@ export class ContactsService {if (input.firstName !== undefined) data.firstName = input.firstName.trim();if (input.lastName !== undefined)data.lastName = blankToNull(input.lastName);- if (input.email !== undefined) data.email = normalizeEmail(input.email);+ const email =+ input.email === undefined ? null : normalizeEmail(input.email);+ if (input.email !== undefined) data.email = email;if (input.phone !== undefined) data.phone = blankToNull(input.phone);if (input.title !== undefined) data.title = blankToNull(input.title);if (input.linkedinUrl !== undefined) {@@ -438,8 +440,8 @@ export class ContactsService {select: { id: true, firstName: true, lastName: true },});- if (typeof data.email === "string") {- await this.allowAgain(tx, data.email);+ if (email !== null) {+ await this.allowAgain(tx, email);}return updated;@@ -753,17 +755,17 @@ export class ContactsService {};}- private translate(error: unknown, id: string): unknown {- if (error instanceof PrismaNamespace.PrismaClientKnownRequestError) {- if (error.code === "P2025") {- return new NotFoundException(`No contact with id ${id}.`);+ private translate(cause: unknown, id: string): never {+ if (cause instanceof PrismaNamespace.PrismaClientKnownRequestError) {+ if (cause.code === "P2025") {+ throw new NotFoundException(`No contact with id ${id}.`);}- if (error.code === "P2002") {- return new ConflictException(+ if (cause.code === "P2002") {+ throw new ConflictException("Another contact already uses that email address.",);}}- return error;+ throw cause;}}diff --git a/apps/api/src/conversations/conversation-attachments.ts b/apps/api/src/conversations/conversation-attachments.tsindex 4dc8d4fa..9eacc375 100644--- a/apps/api/src/conversations/conversation-attachments.ts+++ b/apps/api/src/conversations/conversation-attachments.ts@@ -1,4 +1,7 @@import type { Prisma } from "@crm/db";+import { z } from "zod";++const builderMessageFields = z.record(z.string(), z.json()).catch({});export type StoredBuilderAttachment = {id: string;@@ -12,9 +15,8 @@ export function builderMessageWithAttachments(attachments: StoredBuilderAttachment[],shareToken?: string,): Prisma.JsonObject {- const message = recordOf(value);return {- ...message,+ ...builderMessageFields.parse(value),attachments: attachments.map((attachment) => ({id: attachment.id,name: attachment.name,@@ -24,7 +26,7 @@ export function builderMessageWithAttachments(? attachmentUrl(attachment.id, shareToken): null,})),- } as Prisma.JsonObject;+ };}export function isPreviewableImage(mediaType: string): boolean {@@ -37,9 +39,3 @@ function attachmentUrl(id: string, shareToken?: string): string {const path = `/api/conversations/attachments/${encodeURIComponent(id)}`;return shareToken ? `${path}?share=${encodeURIComponent(shareToken)}` : path;}--function recordOf(value: unknown): Record<string, unknown> {- return value && typeof value === "object" && !Array.isArray(value)- ? (value as Record<string, unknown>)- : {};-}diff --git a/apps/api/src/conversations/conversations.service.ts b/apps/api/src/conversations/conversations.service.tsindex 3004ce88..7ee80a4c 100644--- a/apps/api/src/conversations/conversations.service.ts+++ b/apps/api/src/conversations/conversations.service.ts@@ -1,5 +1,10 @@import { WORKSPACE_ID } from "@crm/auth";import { type Db, type Prisma, Prisma as PrismaNamespace } from "@crm/db";+import { readAgentManifestSummary } from "@crm/validation/agent-manifest";+import {+ type BuilderQuestion,+ builderQuestion,+} from "@crm/validation/builder-question";import {BadRequestException,Injectable,@@ -51,6 +56,10 @@ export interface BuilderConversationSummary {} | null;}+type ReplayedRequest = {+ id: string;+};+type ExistingBuilderRequest = {id: string;conversationId: string;@@ -396,6 +405,7 @@ export class ConversationsService {: null,createdVersions: row.createdVersions.map((version) => ({...version,+ manifest: readAgentManifestSummary(version.manifest),createdAt: version.createdAt.toISOString(),})),builderArtifacts: row.builderArtifacts.map((artifact) => ({@@ -585,8 +595,7 @@ export class ConversationsService {throw new BadRequestException("Choose an answer before submitting.");}- const displayText =- typeof selected?.label === "string" ? selected.label : answer;+ const displayText = selected?.label ?? answer;const inputResponse = input.optionId? { requestId: input.requestId, optionId: input.optionId }: { requestId: input.requestId, text: input.text };@@ -1082,7 +1091,7 @@ export class ConversationsService {private replayBuilderCreation(existing: ExistingBuilderRequest,userId: string,- ): { id: string } {+ ): ReplayedRequest {if (existing.conversation.userId !== userId ||existing.conversation.kind !== "BUILDER"@@ -1097,7 +1106,7 @@ export class ConversationsService {existing: ExistingBuilderRequest,conversationId: string,userId: string,- ): { id: string } {+ ): ReplayedRequest {if (existing.conversationId !== conversationId ||existing.submittedById !== userId@@ -1109,78 +1118,15 @@ export class ConversationsService {}}-function isUniqueConstraint(error: unknown): boolean {+function isUniqueConstraint(cause: unknown): boolean {return (- error instanceof PrismaNamespace.PrismaClientKnownRequestError &&- error.code === "P2002"+ cause instanceof PrismaNamespace.PrismaClientKnownRequestError &&+ cause.code === "P2002");}-function recordOf(value: unknown): Record<string, unknown> {- return value && typeof value === "object" && !Array.isArray(value)- ? (value as Record<string, unknown>)- : {};-}--function arrayOf(value: unknown): unknown[] {- return Array.isArray(value) ? value : [];-}--function pendingBuilderQuestionOf(value: unknown) {- const request = recordOf(value);- if (- request.kind !== "question" ||- typeof request.requestId !== "string" ||- !request.requestId ||- typeof request.prompt !== "string" ||- !request.prompt- ) {- return null;- }-- const display = ["confirmation", "select", "text"].includes(- String(request.display),- )- ? (request.display as "confirmation" | "select" | "text")- : undefined;- const options = arrayOf(request.options).flatMap((value) => {- const option = recordOf(value);- if (- typeof option.id !== "string" ||- !option.id ||- typeof option.label !== "string" ||- !option.label- ) {- return [];- }- const style = ["danger", "default", "primary"].includes(- String(option.style),- )- ? (option.style as "danger" | "default" | "primary")- : undefined;-- return [- {- id: option.id,- label: option.label,- description:- typeof option.description === "string"- ? option.description- : undefined,- style,- },- ];- });-- return {- kind: "question" as const,- requestId: request.requestId,- prompt: request.prompt,- display,- options,- allowFreeform:- request.allowFreeform === true ||- display === "text" ||- options.length === 0,- };+function pendingBuilderQuestionOf(+ value: Prisma.JsonValue,+): BuilderQuestion | null {+ return builderQuestion.parse(value);}diff --git a/apps/api/src/crm/enrichment-log.service.ts b/apps/api/src/crm/enrichment-log.service.tsindex 73f33569..c5087d8e 100644--- a/apps/api/src/crm/enrichment-log.service.ts+++ b/apps/api/src/crm/enrichment-log.service.ts@@ -1,4 +1,5 @@import { ActivityType, type Db } from "@crm/db";+import type { ActivityMetaFields } from "@crm/validation/activity-meta";import { Injectable } from "@nestjs/common";import { InjectDatabase } from "../database/database.constants";import { ActivityStampService } from "./activity-stamp.service";@@ -8,7 +9,7 @@ export type EnrichmentEvent = {contactId?: string | null;subject: string;body?: string | null;- meta?: Record<string, unknown>;+ meta?: ActivityMetaFields;};@Injectable()diff --git a/apps/api/src/currency/rates.service.ts b/apps/api/src/currency/rates.service.tsindex eefab65a..a37c31f9 100644--- a/apps/api/src/currency/rates.service.ts+++ b/apps/api/src/currency/rates.service.ts@@ -11,6 +11,7 @@ import {writeRatesRefreshedAt,} from "@crm/db/settings";import { Injectable, Logger } from "@nestjs/common";+import { z } from "zod";import { InjectDatabase } from "../database/database.constants";export const RATES_PROVIDER = "open.er-api.com";@@ -31,17 +32,29 @@ export interface RateRefresh {reason: string | null;}-interface OpenExchangeResponse {- result?: unknown;- base_code?: unknown;- time_last_update_unix?: unknown;- rates?: Record<string, unknown>;- "error-type"?: unknown;-}--function parseAsOf(value: unknown): Date | null {- if (typeof value !== "number" || !Number.isFinite(value)) return null;- const date = new Date(value * 1000);+const UNREADABLE_FEED = {+ result: "",+ time_last_update_unix: null,+ rates: {},+ "error-type": null,+};++const openExchangeResponse = z+ .object({+ result: z.string().catch(""),+ time_last_update_unix: z+ .number()+ .refine(Number.isFinite)+ .nullable()+ .catch(null),+ rates: z.record(z.string(), z.json()).catch({}),+ "error-type": z.string().nullable().catch(null),+ })+ .catch(UNREADABLE_FEED);++function parseAsOf(seconds: number | null): Date | null {+ if (seconds === null) return null;+ const date = new Date(seconds * 1000);return Number.isNaN(date.getTime()) ? null : date;}@@ -179,15 +192,14 @@ export class RatesService {return null;}- const body = (await response.json()) as OpenExchangeResponse;+ const body = openExchangeResponse.parse(await response.json());if (body.result !== "success") {this.logger.warn({message: "Exchange rate provider refused the request",base,attempt,- errorType:- typeof body["error-type"] === "string" ? body["error-type"] : null,+ errorType: body["error-type"],});return null;}@@ -195,7 +207,7 @@ export class RatesService {const asOf = parseAsOf(body.time_last_update_unix) ?? new Date();const rates = new Map<string, Prisma.Decimal>();- for (const [code, value] of Object.entries(body.rates ?? {})) {+ for (const [code, value] of Object.entries(body.rates)) {const quoteCurrency = normalizeCurrency(code);if (!isCurrencyCode(quoteCurrency)) continue;if (quoteCurrency === normalizeCurrency(base)) continue;diff --git a/apps/api/src/dashboard/dashboard.service.ts b/apps/api/src/dashboard/dashboard.service.tsindex f338fd46..75065fc2 100644--- a/apps/api/src/dashboard/dashboard.service.ts+++ b/apps/api/src/dashboard/dashboard.service.ts@@ -1,5 +1,6 @@import { ActivityType, type Db, DealStage } from "@crm/db";import { OPEN_DEAL_STAGES } from "@crm/db/deal-stage";+import { activityMeta } from "@crm/validation/activity-meta";import { Injectable } from "@nestjs/common";import { toCents } from "../crm/values";import { ConversionService } from "../currency/conversion.service";@@ -282,7 +283,7 @@ export class DashboardService {recentActivity: recentActivity.map(({ createdAt, meta, ...entry }) => ({...entry,createdAt: createdAt.toISOString(),- meta: meta as Record<string, unknown> | null,+ meta: activityMeta.parse(meta),})),};}diff --git a/apps/api/src/deals/deals.service.ts b/apps/api/src/deals/deals.service.tsindex 5bb09fec..3f5b23d0 100644--- a/apps/api/src/deals/deals.service.ts+++ b/apps/api/src/deals/deals.service.ts@@ -38,6 +38,7 @@ import {FACET_ALL,FACET_UNASSIGNED,type ListResult,+ type OrderByColumns,paginate,resolveOrderBy,} from "../trpc/list-input";@@ -83,10 +84,7 @@ const CONTACT_SELECT = {const LOSING = new Set<DealStage>(LOSING_DEAL_STAGES);-const SORTABLE: Record<- string,- (dir: Prisma.SortOrder) => Prisma.DealOrderByWithRelationInput[]-> = {+const SORTABLE: OrderByColumns<Prisma.DealOrderByWithRelationInput[]> = {name: (dir) => [{ name: dir }],company: (dir) => [{ company: { name: dir } }, { name: "asc" }],stage: (dir) => [{ stage: dir }, { expectedCloseDate: "asc" }],@@ -709,26 +707,26 @@ export class DealsService {};}- private translate(error: unknown, id: string): unknown {+ private translate(cause: unknown, id: string): never {if (- error instanceof PrismaNamespace.PrismaClientKnownRequestError &&- error.code === "P2025"+ cause instanceof PrismaNamespace.PrismaClientKnownRequestError &&+ cause.code === "P2025") {- return new NotFoundException(`No deal with id ${id}.`);+ throw new NotFoundException(`No deal with id ${id}.`);}- return this.translateRelations(error);+ return this.translateRelations(cause);}- private translateRelations(error: unknown): unknown {+ private translateRelations(cause: unknown): never {if (- error instanceof PrismaNamespace.PrismaClientKnownRequestError &&- (error.code === "P2003" || error.code === "P2025")+ cause instanceof PrismaNamespace.PrismaClientKnownRequestError &&+ (cause.code === "P2003" || cause.code === "P2025")) {- return new BadRequestException(+ throw new BadRequestException("That company or owner does not exist any more.",);}- return error;+ throw cause;}}diff --git a/apps/api/src/fields/fields.service.ts b/apps/api/src/fields/fields.service.tsindex ceb09a81..0f589dfb 100644--- a/apps/api/src/fields/fields.service.ts+++ b/apps/api/src/fields/fields.service.ts@@ -48,7 +48,7 @@ export class FieldsService {includeArchived: boolean,): Promise<SerializedField[]> {const definitions = await this.db.fieldDefinition.findMany({- where: { entity, ...(includeArchived ? {} : { archivedAt: null }) },+ where: { entity, archivedAt: includeArchived ? undefined : null },include: WITH_OPTIONS,orderBy: { position: "asc" },});@@ -421,15 +421,15 @@ export class FieldsService {}}- private translate(error: unknown): unknown {+ private translate(cause: unknown): never {if (- error instanceof PrismaNamespace.PrismaClientKnownRequestError &&- error.code === "P2025"+ cause instanceof PrismaNamespace.PrismaClientKnownRequestError &&+ cause.code === "P2025") {- return new NotFoundException("That field does not exist.");+ throw new NotFoundException("That field does not exist.");}- return error;+ throw cause;}}diff --git a/apps/api/src/google/conversation.service.ts b/apps/api/src/google/conversation.service.tsindex b995c74d..232b84f5 100644--- a/apps/api/src/google/conversation.service.ts+++ b/apps/api/src/google/conversation.service.ts@@ -1,7 +1,18 @@-import type { Db } from "@crm/db";+import type { Db, Prisma } from "@crm/db";import { Injectable, NotFoundException } from "@nestjs/common";+import { z } from "zod";import { InjectDatabase } from "../database/database.constants";+const storedRecipient = z.object({+ email: z.string(),+ name: z.string().nullable().catch(null),+ kind: z.string().catch("to"),+});++type StoredRecipient = z.infer<typeof storedRecipient>;++const storedRecipients = z.array(z.json()).catch([]);+@Injectable()export class ConversationService {constructor(@InjectDatabase() private readonly db: Db) {}@@ -142,22 +153,9 @@ export class ConversationService {}}-function recipientsOf(- value: unknown,-): { email: string; name: string | null; kind: string }[] {- if (!Array.isArray(value)) return [];-- return value.flatMap((entry) => {- if (typeof entry !== "object" || entry === null) return [];- const record = entry as Record<string, unknown>;- if (typeof record.email !== "string") return [];-- return [- {- email: record.email,- name: typeof record.name === "string" ? record.name : null,- kind: typeof record.kind === "string" ? record.kind : "to",- },- ];+function recipientsOf(value: Prisma.JsonValue): StoredRecipient[] {+ return storedRecipients.parse(value).flatMap((entry) => {+ const parsed = storedRecipient.safeParse(entry);+ return parsed.success ? [parsed.data] : [];});}diff --git a/apps/api/src/logging/all-exceptions.filter.ts b/apps/api/src/logging/all-exceptions.filter.tsindex 6282a5e8..fb8f4603 100644--- a/apps/api/src/logging/all-exceptions.filter.ts+++ b/apps/api/src/logging/all-exceptions.filter.ts@@ -74,26 +74,31 @@ function describe(exception: unknown): string {return typeof exception === "string" ? exception : "Unknown exception";}+interface ErrorBody {+ statusCode?: number;+ message?: unknown;+ requestId?: string;+ [field: string]: unknown;+}+function body(exception: unknown,status: number,requestId: string | undefined,-): Record<string, unknown> {- const withRequestId = requestId ? { requestId } : {};+): ErrorBody {+ const reported = exceptionBody(exception, status);++ return requestId ? { ...reported, requestId } : reported;+}+function exceptionBody(exception: unknown, status: number): ErrorBody {if (!(exception instanceof HttpException)) {- return {- statusCode: status,- message: "Internal server error",- ...withRequestId,- };+ return { statusCode: status, message: "Internal server error" };}const original = exception.getResponse();- if (typeof original === "string") {- return { statusCode: status, message: original, ...withRequestId };- }-- return { ...(original as Record<string, unknown>), ...withRequestId };+ return typeof original === "string"+ ? { statusCode: status, message: original }+ : { ...original };}diff --git a/apps/api/src/logging/context-logger.ts b/apps/api/src/logging/context-logger.tsindex b3030e39..13d23912 100644--- a/apps/api/src/logging/context-logger.ts+++ b/apps/api/src/logging/context-logger.ts@@ -67,11 +67,16 @@ export class ContextLogger extends ConsoleLogger {return record;}- return {+ const correlated: JsonLogRecord = {...record,requestId: request.requestId,- ...(request.userId ? { userId: request.userId } : {}),};++ if (!request.userId) {+ return correlated;+ }++ return { ...correlated, userId: request.userId };}protected override stringifyMessage(diff --git a/apps/api/src/logging/prisma-log.bridge.ts b/apps/api/src/logging/prisma-log.bridge.tsindex 3a0b9508..11f52bd3 100644--- a/apps/api/src/logging/prisma-log.bridge.ts+++ b/apps/api/src/logging/prisma-log.bridge.ts@@ -12,11 +12,10 @@ export class PrismaLogBridge implements OnModuleInit, OnApplicationShutdown {onModuleInit(): void {setPrismaLogSink(({ level, message, target, durationMs }) => {- const payload = {- message,- target,- ...(durationMs === undefined ? {} : { durationMs }),- };+ const payload =+ durationMs === undefined+ ? { message, target }+ : { message, target, durationMs };if (level === "error") {this.logger.error(payload);diff --git a/apps/api/src/mailbox/mailbox.constants.ts b/apps/api/src/mailbox/mailbox.constants.tsindex 4e062580..a9228d8b 100644--- a/apps/api/src/mailbox/mailbox.constants.ts+++ b/apps/api/src/mailbox/mailbox.constants.ts@@ -37,14 +37,14 @@ export function isMicrosoftSyncSource(return (MICROSOFT_SYNC_SOURCES as readonly string[]).includes(source);}-export const SCOPE_FOR_SOURCE: Record<SyncSource, string> = {+export const SCOPE_FOR_SOURCE = {calendar: CALENDAR_SCOPE,gmail: GMAIL_SCOPE,outlook: OUTLOOK_MAIL_SCOPE,-};+} satisfies Record<SyncSource, string>;-export const PROVIDER_FOR_SOURCE: Record<SyncSource, MailboxProviderId> = {+export const PROVIDER_FOR_SOURCE = {calendar: GOOGLE_PROVIDER_ID,gmail: GOOGLE_PROVIDER_ID,outlook: MICROSOFT_PROVIDER_ID,-};+} satisfies Record<SyncSource, MailboxProviderId>;diff --git a/apps/api/src/mailbox/participants.ts b/apps/api/src/mailbox/participants.tsindex fcb9c1dc..4da7a5ed 100644--- a/apps/api/src/mailbox/participants.ts+++ b/apps/api/src/mailbox/participants.ts@@ -188,10 +188,12 @@ export function dominantDomain(return best;}-export function splitName(- name: string | null,- email: string,-): { firstName: string; lastName: string | null } {+export type PersonName = {+ firstName: string;+ lastName: string | null;+};++export function splitName(name: string | null, email: string): PersonName {const cleaned = name?.trim().replace(/\s+/g, " ") ?? "";if (cleaned && !cleaned.includes("@")) {diff --git a/apps/api/src/mailbox/sync-state.service.ts b/apps/api/src/mailbox/sync-state.service.tsindex d669983b..ca56462b 100644--- a/apps/api/src/mailbox/sync-state.service.ts+++ b/apps/api/src/mailbox/sync-state.service.ts@@ -2,6 +2,7 @@ import {type Db,GoogleSyncStatus,type MailboxSyncModel as MailboxSync,+ type Prisma,} from "@crm/db";import { Injectable, Logger } from "@nestjs/common";import { InjectDatabase } from "../database/database.constants";@@ -25,9 +26,10 @@ export class SyncStateService {userId: string,sources?: readonly SyncSource[],): Promise<MailboxSync[]> {- return this.db.mailboxSync.findMany({- where: { userId, ...(sources ? { source: { in: [...sources] } } : {}) },- });+ const where: Prisma.MailboxSyncWhereInput = { userId };+ if (sources) where.source = { in: [...sources] };++ return this.db.mailboxSync.findMany({ where });}async due(now: Date): Promise<MailboxSync[]> {@@ -164,9 +166,10 @@ export class SyncStateService {}async remove(userId: string, source?: SyncSource): Promise<void> {- await this.db.mailboxSync.deleteMany({- where: { userId, ...(source ? { source } : {}) },- });+ const where: Prisma.MailboxSyncWhereInput = { userId };+ if (source) where.source = source;++ await this.db.mailboxSync.deleteMany({ where });}}diff --git a/apps/api/src/mailbox/thread-writer.service.ts b/apps/api/src/mailbox/thread-writer.service.tsindex 57d131b2..9b06692c 100644--- a/apps/api/src/mailbox/thread-writer.service.ts+++ b/apps/api/src/mailbox/thread-writer.service.ts@@ -172,17 +172,15 @@ export class ThreadWriterService {const firstMessageAt = stats._min.sentAt ?? parsed.sentAt;const lastMessageAt = stats._max.sentAt ?? parsed.sentAt;- await tx.emailThread.update({- where: { id: record.id },- data: {- messageCount: stats._count._all,- firstMessageAt,- lastMessageAt,- ...(parsed.sentAt <= firstMessageAt- ? { subject: parsed.subject }- : {}),- },- });+ const data: Prisma.EmailThreadUpdateInput = {+ messageCount: stats._count._all,+ firstMessageAt,+ lastMessageAt,+ };++ if (parsed.sentAt <= firstMessageAt) data.subject = parsed.subject;++ await tx.emailThread.update({ where: { id: record.id }, data });return this.project(tx, record.id, row.userId, {subject: parsed.subject ?? "(no subject)",@@ -204,12 +202,12 @@ export class ThreadWriterService {}private async storedElsewhere(- error: unknown,+ cause: unknown,rfcMessageId: string,): Promise<boolean> {const duplicate =- error instanceof PrismaNamespace.PrismaClientKnownRequestError &&- error.code === "P2002";+ cause instanceof PrismaNamespace.PrismaClientKnownRequestError &&+ cause.code === "P2002";if (!duplicate) return false;const winner = await this.db.emailMessage.findFirst({diff --git a/apps/api/src/main.ts b/apps/api/src/main.tsindex f6e3bead..af118dec 100644--- a/apps/api/src/main.ts+++ b/apps/api/src/main.ts@@ -15,10 +15,10 @@ async function bootstrap() {});}-void bootstrap().catch((error: unknown) => {+void bootstrap().catch((cause: unknown) => {new Logger("Bootstrap").fatal({ message: "API failed to start" },- error instanceof Error ? error.stack : String(error),+ cause instanceof Error ? cause.stack : String(cause),);process.exit(1);});diff --git a/apps/api/src/settings/model-catalog.service.ts b/apps/api/src/settings/model-catalog.service.tsindex 5f7c515d..13ebcbbf 100644--- a/apps/api/src/settings/model-catalog.service.ts+++ b/apps/api/src/settings/model-catalog.service.ts@@ -1,6 +1,7 @@import { CACHE_MANAGER } from "@nestjs/cache-manager";import { Inject, Injectable, Logger } from "@nestjs/common";import type { Cache } from "cache-manager";+import { z } from "zod";const CATALOG_URL = "https://ai-gateway.vercel.sh/v1/models";@@ -18,29 +19,47 @@ export interface CatalogModel {pricing: { input: number; output: number } | null;}-interface GatewayModel {- id?: unknown;- name?: unknown;- owned_by?: unknown;- type?: unknown;- tags?: unknown;- context_window?: unknown;- pricing?: { input?: unknown; output?: unknown } | null;-}+const gatewayRate = z+ .union([z.number(), z.string()])+ .transform((value) => Number(value))+ .refine((value) => Number.isFinite(value))+ .nullable()+ .catch(null);++const gatewayModel = z.object({+ id: z.string(),+ name: z.string().catch(""),+ owned_by: z.string().catch(""),+ type: z.string().catch(""),+ tags: z.array(z.json()).catch([]),+ context_window: z.number(),+ pricing: z+ .object({ input: gatewayRate, output: gatewayRate })+ .nullable()+ .catch(null),+});++type GatewayModel = z.infer<typeof gatewayModel>;++const gatewayCatalog = z+ .object({ data: z.array(z.json()).catch([]) })+ .catch({ data: [] });-function rate(value: unknown): number | null {- const parsed = typeof value === "string" ? Number(value) : value;- return typeof parsed === "number" && Number.isFinite(parsed) ? parsed : null;+function usable(model: GatewayModel): boolean {+ return model.type === "language" && model.tags.includes("tool-use");}-function usable(model: GatewayModel): boolean {- const tags = Array.isArray(model.tags) ? model.tags : [];- return (- typeof model.id === "string" &&- model.type === "language" &&- tags.includes("tool-use") &&- typeof model.context_window === "number"- );+function toCatalogModel(model: GatewayModel): CatalogModel {+ const input = model.pricing?.input ?? null;+ const output = model.pricing?.output ?? null;++ return {+ id: model.id,+ name: model.name || model.id,+ provider: model.owned_by || (model.id.split("/")[0] ?? model.id),+ contextWindowTokens: model.context_window,+ pricing: input !== null && output !== null ? { input, output } : null,+ };}@Injectable()@@ -80,26 +99,13 @@ export class ModelCatalogService {return null;}- const body = (await response.json()) as { data?: unknown };- const rows = Array.isArray(body.data)- ? (body.data as GatewayModel[])- : [];-- const models = rows.filter(usable).map((model): CatalogModel => {- const id = model.id as string;- const input = rate(model.pricing?.input);- const output = rate(model.pricing?.output);-- return {- id,- name: typeof model.name === "string" && model.name ? model.name : id,- provider:- typeof model.owned_by === "string" && model.owned_by- ? model.owned_by- : (id.split("/")[0] ?? id),- contextWindowTokens: model.context_window as number,- pricing: input !== null && output !== null ? { input, output } : null,- };+ const body = gatewayCatalog.parse(await response.json());++ const models = body.data.flatMap((entry) => {+ const parsed = gatewayModel.safeParse(entry);+ return parsed.success && usable(parsed.data)+ ? [toCatalogModel(parsed.data)]+ : [];});models.sort(diff --git a/apps/api/src/settings/settings.service.ts b/apps/api/src/settings/settings.service.tsindex ebb6e1b8..71c55e24 100644--- a/apps/api/src/settings/settings.service.ts+++ b/apps/api/src/settings/settings.service.ts@@ -133,10 +133,10 @@ export class SettingsService {});}})- .catch((error: unknown) => {+ .catch((cause: unknown) => {this.logger.warn({ message: "Could not queue the waiting research" },- error instanceof Error ? error.stack : String(error),+ cause instanceof Error ? cause.stack : String(cause),);});diff --git a/apps/api/src/slack/slack-connection.service.ts b/apps/api/src/slack/slack-connection.service.tsindex b58e3d4d..ab8d6321 100644--- a/apps/api/src/slack/slack-connection.service.ts+++ b/apps/api/src/slack/slack-connection.service.ts@@ -3,7 +3,7 @@ import {isSlackConfigured,WORKSPACE_ID,} from "@crm/auth";-import type { Db } from "@crm/db";+import type { Db, Prisma } from "@crm/db";import { schemas } from "@crm/validation";import {BadRequestException,@@ -177,17 +177,16 @@ export class SlackConnectionService {const take = input.limit ?? SLACK.channels.pageSize;const needle = input.query?.trim() ?? "";+ const where: Prisma.SlackChannelWhereInput = { available: true };+ if (needle) where.name = { contains: needle, mode: "insensitive" };+const [rows, grant, sync] = await Promise.all([this.db.slackChannel.findMany({- where: {- available: true,- ...(needle- ? { name: { contains: needle, mode: "insensitive" } }- : {}),- },+ where,orderBy: [{ isMember: "desc" }, { name: "asc" }, { id: "asc" }],take: take + 1,- ...(input.cursor ? { cursor: { id: input.cursor }, skip: 1 } : {}),+ cursor: input.cursor ? { id: input.cursor } : undefined,+ skip: input.cursor ? 1 : undefined,select: {id: true,name: true,diff --git a/apps/api/src/sso/sso.service.ts b/apps/api/src/sso/sso.service.tsindex 6537ba08..bdb09d26 100644--- a/apps/api/src/sso/sso.service.ts+++ b/apps/api/src/sso/sso.service.ts@@ -19,8 +19,14 @@ import {Logger,} from "@nestjs/common";import { APIError } from "better-auth/api";+import { z } from "zod";import { InjectDatabase } from "../database/database.constants";-import { type ListResult, paginate, resolveOrderBy } from "../trpc/list-input";+import {+ type ListResult,+ type OrderByColumns,+ paginate,+ resolveOrderBy,+} from "../trpc/list-input";import type {DeleteSsoProviderInput,RegisterSsoProviderInput,@@ -65,23 +71,20 @@ type ProviderRow = Prisma.SsoProviderGetPayload<{select: typeof PROVIDER_SELECT;}>;-const SORTABLE: Record<- string,- (dir: "asc" | "desc") => Prisma.SsoProviderOrderByWithRelationInput-> = {+const SORTABLE: OrderByColumns<Prisma.SsoProviderOrderByWithRelationInput> = {providerId: (dir) => ({ providerId: dir }),domain: (dir) => ({ domain: dir }),issuer: (dir) => ({ issuer: dir }),};-const STATUS_BY_CODE: Record<string, number> = {- BAD_REQUEST: 400,- UNAUTHORIZED: 401,- FORBIDDEN: 403,- NOT_FOUND: 404,- CONFLICT: 409,- UNPROCESSABLE_ENTITY: 400,-};+const STATUS_BY_CODE = new Map<string, number>([+ ["BAD_REQUEST", 400],+ ["UNAUTHORIZED", 401],+ ["FORBIDDEN", 403],+ ["NOT_FOUND", 404],+ ["CONFLICT", 409],+ ["UNPROCESSABLE_ENTITY", 400],+]);function splitDomains(value: string): string[] {return value@@ -96,27 +99,28 @@ function splitDomains(value: string): string[] {.filter(Boolean);}-function lastFour(clientId: unknown): string | null {- return typeof clientId === "string" && clientId.length >= 4- ? clientId.slice(-4)- : null;+const oidcConfig = z+ .object({ clientId: z.string().catch("") })+ .catch({ clientId: "" });++type OidcConfig = z.infer<typeof oidcConfig>;++function lastFour(clientId: string): string | null {+ return clientId.length >= 4 ? clientId.slice(-4) : null;}-function parseConfig(value: string | null): Record<string, unknown> | null {+function readOidcConfig(value: string | null): OidcConfig | null {if (!value) return null;try {- const parsed: unknown = JSON.parse(value);- return parsed && typeof parsed === "object"- ? (parsed as Record<string, unknown>)- : null;+ return oidcConfig.parse(JSON.parse(value));} catch {return null;}}function toProvider(row: ProviderRow): SsoProvider {- const oidc = parseConfig(row.oidcConfig);+ const oidc = readOidcConfig(row.oidcConfig);return {providerId: row.providerId,@@ -270,11 +274,11 @@ export class SsoService {} catch (error) {if (error instanceof APIError) {const status =- STATUS_BY_CODE[error.body?.code ?? ""] ?? error.statusCode;+ STATUS_BY_CODE.get(error.body?.code ?? "") ?? error.statusCode;throw new HttpException(error.body?.message ?? "The identity provider could not be saved.",- typeof status === "number" ? status : 400,+ status,);}diff --git a/apps/api/src/telemetry/rollup.service.ts b/apps/api/src/telemetry/rollup.service.tsindex 928cd990..20ee5b66 100644--- a/apps/api/src/telemetry/rollup.service.ts+++ b/apps/api/src/telemetry/rollup.service.ts@@ -587,14 +587,16 @@ function isSet(name: string): boolean {return Boolean(process.env[name]?.trim());}-function byKind(rows: Counted[]): Record<string, number> {+type CountsByKey = Record<string, number>;++function byKind(rows: Counted[]): CountsByKey {return merge(rows.map((row) => ({ ...row, key: permittedTaskKind(row.key) })),);}-function merge(rows: Counted[]): Record<string, number> {- const counts: Record<string, number> = {};+function merge(rows: Counted[]): CountsByKey {+ const counts: CountsByKey = {};for (const row of rows) {counts[row.key] = (counts[row.key] ?? 0) + row.count;@@ -603,12 +605,9 @@ function merge(rows: Counted[]): Record<string, number> {return counts;}-function countsOf(- rows: Counted[],- keys: readonly string[],-): Record<string, number> {+function countsOf(rows: Counted[], keys: readonly string[]): CountsByKey {const merged = merge(rows);- const complete: Record<string, number> = {};+ const complete: CountsByKey = {};for (const key of keys) complete[key] = merged[key] ?? 0;diff --git a/apps/api/src/tracking/tracking-filing.service.ts b/apps/api/src/tracking/tracking-filing.service.tsindex ceca0b12..16aa5427 100644--- a/apps/api/src/tracking/tracking-filing.service.ts+++ b/apps/api/src/tracking/tracking-filing.service.ts@@ -20,10 +20,12 @@ import {} from "../mailbox/participants";import { TrackingCounterService } from "./tracking-counter.service";+type TouchColumns = Record<string, string | Date | null>;+function columns(touch: Touch | undefined,prefix: "first" | "last",-): Record<string, string | Date | null> {+): TouchColumns {if (!touch) return {};return {@@ -144,12 +146,12 @@ export class TrackingFilingService {}private async raced(- error: unknown,+ cause: unknown,email: string,): Promise<{ id: string } | null> {if (- !(error instanceof Prisma.PrismaClientKnownRequestError) ||- error.code !== "P2002"+ !(cause instanceof Prisma.PrismaClientKnownRequestError) ||+ cause.code !== "P2002") {return null;}diff --git a/apps/api/src/tracking/tracking-ingest.service.ts b/apps/api/src/tracking/tracking-ingest.service.tsindex bffd39ea..e4f265b1 100644--- a/apps/api/src/tracking/tracking-ingest.service.ts+++ b/apps/api/src/tracking/tracking-ingest.service.ts@@ -13,6 +13,7 @@ import {type TrackingConfig,} from "@crm/db/tracking";import { Injectable, Logger } from "@nestjs/common";+import { z } from "zod";import { normalizeEmail } from "../crm/values";import { InjectDatabase } from "../database/database.constants";import { TrackingConfigService } from "./tracking-config.service";@@ -35,6 +36,21 @@ const CARD = /^[0-9 -]{12,25}$/;const ADDRESS = /^[^\s@]+@[^\s@.]+\.[^\s@]+$/;+const fieldText = z.string().nullable().catch(null);++export type FormFields = Record<string, string>;++type StoredTouch = {+ source: string;+ medium: string;+ campaign: string | null;+ term: string | null;+ content: string | null;+ referrer: string | null;+ landing: string | null;+ at: string;+};+export interface IncomingEvent {type: string;host: string;@@ -42,7 +58,7 @@ export interface IncomingEvent {referrer?: string;label?: string;at?: number;- fields?: Record<string, string>;+ fields?: FormFields;touch?: RawTouch;firstTouch?: RawTouch;}@@ -250,7 +266,7 @@ function arriving(touch: RawTouch): RawTouch {};}-function stored(touch: Touch): Record<string, string | null> {+function stored(touch: Touch): StoredTouch {return {source: touch.source,medium: touch.medium,@@ -267,7 +283,7 @@ function scripted(events: IncomingEvent[]): boolean {if (events.length < 3) return false;const stamps = events.flatMap((event) =>- typeof event.at === "number" && Number.isFinite(event.at) ? [event.at] : [],+ event.at !== undefined && Number.isFinite(event.at) ? [event.at] : [],);if (stamps.length !== events.length) return false;@@ -277,7 +293,7 @@ function scripted(events: IncomingEvent[]): boolean {function occurredAt(at: number | undefined): Date {const now = Date.now();- if (typeof at !== "number" || !Number.isFinite(at)) return new Date(now);+ if (at === undefined || !Number.isFinite(at)) return new Date(now);const bounded = Math.min(Math.max(at, now - 86_400_000), now);@@ -289,28 +305,27 @@ function trim(value: string, max: number): string {}function sanitizeId(value: string | undefined): string | null {- if (typeof value !== "string") return null;-- const trimmed = value.trim();+ const trimmed = fieldText.parse(value)?.trim() ?? "";return /^[a-zA-Z0-9_-]{8,64}$/.test(trimmed) ? trimmed : null;}-function clean(fields: Record<string, string>): Record<string, string> {- const kept: Record<string, string> = {};+function clean(fields: FormFields): FormFields {+ const kept: FormFields = {};for (const [key, value] of Object.entries(fields).slice(0, 40)) {- if (typeof value !== "string") continue;+ const text = fieldText.parse(value);+ if (text === null) continue;if (SENSITIVE.test(key)) continue;- if (CARD.test(value.trim())) continue;+ if (CARD.test(text.trim())) continue;- kept[trim(key, 64)] = trim(value, 512);+ kept[trim(key, 64)] = trim(text, 512);}return kept;}-function emailFrom(fields: Record<string, string>): string | null {+function emailFrom(fields: FormFields): string | null {for (const [key, value] of Object.entries(fields)) {if (!/mail/i.test(key)) continue;const email = address(value);@@ -331,7 +346,7 @@ function address(value: string): string | null {return email && ADDRESS.test(email) ? email : null;}-function nameFrom(fields: Record<string, string>): string | null {+function nameFrom(fields: FormFields): string | null {const first = pick(fields, /^(first[\s_-]?name|fname|given)/i);const last = pick(fields, /^(last[\s_-]?name|lname|surname|family)/i);@@ -340,7 +355,7 @@ function nameFrom(fields: Record<string, string>): string | null {return pick(fields, /^(full[\s_-]?name|name)$/i) ?? pick(fields, /name/i);}-function pick(fields: Record<string, string>, pattern: RegExp): string | null {+function pick(fields: FormFields, pattern: RegExp): string | null {for (const [key, value] of Object.entries(fields)) {if (pattern.test(key) && value.trim()) return value.trim();}diff --git a/apps/api/src/tracking/tracking.controller.ts b/apps/api/src/tracking/tracking.controller.tsindex 1c96016c..21ccbf23 100644--- a/apps/api/src/tracking/tracking.controller.ts+++ b/apps/api/src/tracking/tracking.controller.ts@@ -21,6 +21,7 @@ import {import { ConfigService } from "@nestjs/config";import { AllowAnonymous } from "@thallesp/nestjs-better-auth";import type { Response } from "express";+import { z } from "zod";import type { EnvironmentVariables } from "../config/env.validation";import { InjectDatabase } from "../database/database.constants";import { TrackingConfigService } from "./tracking-config.service";@@ -35,6 +36,18 @@ const SWEEP_BATCH = 10_000;const MAX_SWEEP_PASSES = 50;+const parsedBody = z+ .union([+ z.string().transform((text) => ({ text, json: null })),+ z+ .union([z.array(z.json()), z.looseObject({})])+ .transform((json) => ({ text: null, json })),+ ])+ .nullable()+ .catch(null);++const trackingRequest = z.object({ body: parsedBody }).catch({ body: null });+@Controller("api/t")export class TrackingController {private readonly logger = new Logger(TrackingController.name);@@ -203,12 +216,14 @@ async function read(request: IncomingMessage,limit: number,): Promise<string | null> {- const existing = (request as { body?: unknown }).body;- if (typeof existing === "string") {- return existing.length > limit ? null : existing;- }- if (existing && typeof existing === "object") {- return JSON.stringify(existing);+ const existing = trackingRequest.parse(request).body;++ if (existing !== null) {+ if (existing.text !== null) {+ return existing.text.length > limit ? null : existing.text;+ }++ return JSON.stringify(existing.json);}return new Promise((resolve) => {diff --git a/apps/api/src/trpc/error-formatter.ts b/apps/api/src/trpc/error-formatter.tsindex f0855e92..6bcb8d13 100644--- a/apps/api/src/trpc/error-formatter.ts+++ b/apps/api/src/trpc/error-formatter.ts@@ -1,9 +1,21 @@import type { TRPCDefaultErrorShape, TRPCErrorFormatter } from "@trpc/server";+import { z } from "zod";-interface Issue {- message?: unknown;- path?: unknown;-}+const issueShape = z+ .object({+ message: z.string().catch(""),+ path: z.array(z.unknown()).catch([]),+ })+ .catch({ message: "", path: [] });++type Issue = z.infer<typeof issueShape>;++const pathSegment = z.string().nullable().catch(null);++const failedParse = z+ .object({ issues: z.array(issueShape).min(1) })+ .nullable()+ .catch(null);/*** tRPC stringifies a failed input parse into the whole `ZodError`, so a form@@ -16,29 +28,23 @@ interface Issue {* matching and put the JSON back on screen.*/function issuesIn(cause: unknown): Issue[] | null {- if (typeof cause !== "object" || cause === null) return null;-- const issues = (cause as { issues?: unknown }).issues;-- return Array.isArray(issues) && issues.length > 0- ? (issues as Issue[])- : null;+ return failedParse.parse(cause)?.issues ?? null;}function sentence(issue: Issue): string | null {- if (typeof issue.message !== "string" || issue.message.trim() === "") {- return null;- }-const message = issue.message.trim();+ if (message === "") return null;// Zod's own defaults ("Required", "Invalid input") name nothing, so they are// only useful with the field in front of them. Ours are whole sentences.if (/[.!?]$/.test(message)) return message;- const field = Array.isArray(issue.path)- ? issue.path.filter((part) => typeof part === "string").at(-1)- : undefined;+ const field = issue.path+ .flatMap((part) => {+ const segment = pathSegment.parse(part);+ return segment === null ? [] : [segment];+ })+ .at(-1);return field ? `${field}: ${message}` : message;}diff --git a/apps/api/src/trpc/list-input.ts b/apps/api/src/trpc/list-input.tsindex e2b46fc9..e207d96e 100644--- a/apps/api/src/trpc/list-input.ts+++ b/apps/api/src/trpc/list-input.ts@@ -10,7 +10,9 @@ export const listInput = z.object({export type ListInput = z.infer<typeof listInput>;-type FacetCounts = Record<string, Record<string, number>>;+export type FacetCount = Record<string, number>;++type FacetCounts = Record<string, FacetCount>;export type ListResult<TRow> = {rows: TRow[];@@ -18,19 +20,27 @@ export type ListResult<TRow> = {facetCounts: FacetCounts;};-export function paginate(input: Pick<ListInput, "page" | "pageSize">): {+export type Page = {skip: number;take: number;-} {+};++export function paginate(input: Pick<ListInput, "page" | "pageSize">): Page {return {skip: (input.page - 1) * input.pageSize,take: input.pageSize,};}+export type SortDirection = ListInput["dir"];++export interface OrderByColumns<TOrderBy> {+ [column: string]: (dir: SortDirection) => TOrderBy;+}+export function resolveOrderBy<TOrderBy>(input: Pick<ListInput, "sort" | "dir">,- columns: Record<string, (dir: "asc" | "desc") => TOrderBy>,+ columns: OrderByColumns<TOrderBy>,fallback: TOrderBy,): TOrderBy {const column = columns[input.sort];@@ -42,8 +52,8 @@ export function countsByKey<TGroup extends { _count: { _all: number } } & {[K in TKey]?: string | null;},->(groups: TGroup[], key: TKey, nullKey?: string): Record<string, number> {- const counts: Record<string, number> = {};+>(groups: TGroup[], key: TKey, nullKey?: string): FacetCount {+ const counts: FacetCount = {};for (const group of groups) {const value = group[key] ?? nullKey;diff --git a/apps/api/src/workspace/workspace.service.ts b/apps/api/src/workspace/workspace.service.tsindex e498f6d8..8e49e6f2 100644--- a/apps/api/src/workspace/workspace.service.ts+++ b/apps/api/src/workspace/workspace.service.ts@@ -24,6 +24,7 @@ import {countsByKey,FACET_ALL,type ListResult,+ type OrderByColumns,paginate,resolveOrderBy,} from "../trpc/list-input";@@ -65,10 +66,7 @@ const MEMBER_SELECT = {type MemberRow = Prisma.MemberGetPayload<{ select: typeof MEMBER_SELECT }>;-const SORTABLE: Record<- string,- (dir: Prisma.SortOrder) => Prisma.MemberOrderByWithRelationInput-> = {+const SORTABLE: OrderByColumns<Prisma.MemberOrderByWithRelationInput> = {name: (dir) => ({ user: { name: dir } }),email: (dir) => ({ user: { email: dir } }),role: (dir) => ({ role: dir }),diff --git a/apps/api/test/agent-runs.spec.ts b/apps/api/test/agent-runs.spec.tsindex 024b5952..22d4d8c4 100644--- a/apps/api/test/agent-runs.spec.ts+++ b/apps/api/test/agent-runs.spec.ts@@ -582,7 +582,10 @@ describe("cancelling a run", () => {const realFetch = globalThis.fetch;const realSecret = process.env.AGENT_BRIDGE_SECRET;process.env.AGENT_BRIDGE_SECRET = "run-cancel-test";- globalThis.fetch = (async (_url: unknown, init?: RequestInit) => {+ globalThis.fetch = (async (+ _url: string | URL | Request,+ init?: RequestInit,+ ) => {const body = JSON.parse(String(init?.body)) as { runId: string };asked.push(body.runId);return new Response(null, { status });diff --git a/apps/api/test/conversation-sharing.spec.ts b/apps/api/test/conversation-sharing.spec.tsindex c1443504..75f1f60d 100644--- a/apps/api/test/conversation-sharing.spec.ts+++ b/apps/api/test/conversation-sharing.spec.ts@@ -2,9 +2,14 @@ import { afterAll, beforeAll, describe, expect, it } from "bun:test";import { DEFAULT_WORKSPACE_NAME, WORKSPACE_ID } from "@crm/auth";import { db } from "@crm/db";import { workspaceSlug } from "@crm/db/workspace";+import { z } from "zod";import { ConversationSharingService } from "../src/conversations/conversation-sharing.service";import { ConversationsService } from "../src/conversations/conversations.service";+const record = z.record(z.string(), z.unknown()).catch({});++const list = z.array(z.unknown()).catch([]);+const suffix = crypto.randomUUID();const userId = `share-user-${suffix}`;const outsiderId = `share-outsider-${suffix}`;@@ -173,8 +178,8 @@ describe("conversation sharing", () => {it("authorizes attachment bytes with the active share token only", async () => {const { token } = await service.create(conversationId, userId);const shared = await service.resolve(token, viewerId);- const message = recordOf(shared.submissions[0]?.message);- const attachment = recordOf(arrayOf(message.attachments)[0]);+ const message = record.parse(shared.submissions[0]?.message);+ const attachment = record.parse(list.parse(message.attachments)[0]);expect(attachment.previewUrl).toBe(`/api/conversations/attachments/${attachmentId}?share=${encodeURIComponent(token)}`,);@@ -214,13 +219,3 @@ describe("conversation sharing", () => {expect(unavailable).toBeDefined();});});--function recordOf(value: unknown): Record<string, unknown> {- return value && typeof value === "object" && !Array.isArray(value)- ? (value as Record<string, unknown>)- : {};-}--function arrayOf(value: unknown): unknown[] {- return Array.isArray(value) ? value : [];-}diff --git a/apps/api/test/conversations.spec.ts b/apps/api/test/conversations.spec.tsindex de0d3ab1..25c99ca6 100644--- a/apps/api/test/conversations.spec.ts+++ b/apps/api/test/conversations.spec.ts@@ -2,6 +2,7 @@ import { afterAll, beforeAll, describe, expect, it } from "bun:test";import { DEFAULT_WORKSPACE_NAME, WORKSPACE_ID } from "@crm/auth";import { db } from "@crm/db";import { workspaceSlug } from "@crm/db/workspace";+import { z } from "zod";import {builderConversationCreateInput,conversationListInput,@@ -9,6 +10,12 @@ import {} from "../src/conversations/conversations.contracts";import { ConversationsService } from "../src/conversations/conversations.service";+const record = z.record(z.string(), z.unknown()).catch({});++const list = z.array(z.unknown()).catch([]);++const text = z.string().nullable().catch(null);+const suffix = process.env.TEST_RUN_ID ?? "conversations-spec";const email = `conversation.subject.${suffix}@example.test`;const userId = `user-${suffix}`;@@ -449,8 +456,10 @@ describe("ConversationsService", () => {const submission = detail.submissions.find((row) => row.clientRequestId === input.clientRequestId,);- const message = recordOf(submission?.message);- const attachments = arrayOf(message.attachments).map(recordOf);+ const message = record.parse(submission?.message);+ const attachments = list+ .parse(message.attachments)+ .map((entry) => record.parse(entry));expect(JSON.stringify(message)).not.toContain("contentBase64");expect(attachments).toEqual([@@ -467,8 +476,8 @@ describe("ConversationsService", () => {previewUrl: null,}),]);- const imageId = attachments[0]?.id;- if (typeof imageId !== "string") throw new Error("Missing attachment id");+ const imageId = text.parse(attachments[0]?.id);+ if (imageId === null) throw new Error("Missing attachment id");const image = await service.attachment(imageId, userId);expect(Buffer.from(image.content)).toEqual(imageBytes);expect(image).toMatchObject({@@ -515,9 +524,9 @@ describe("ConversationsService", () => {userId,);const original = await service.builderById(conversation.id, userId);- const originalMessage = recordOf(original.submissions[0]?.message);- const originalAttachment = recordOf(- arrayOf(originalMessage.attachments)[0],+ const originalMessage = record.parse(original.submissions[0]?.message);+ const originalAttachment = record.parse(+ list.parse(originalMessage.attachments)[0],);await service.submitBuilder(@@ -541,8 +550,10 @@ describe("ConversationsService", () => {);const detail = await service.builderById(conversation.id, userId);- const retryMessage = recordOf(detail.submissions.at(-1)?.message);- const retryAttachment = recordOf(arrayOf(retryMessage.attachments)[0]);+ const retryMessage = record.parse(detail.submissions.at(-1)?.message);+ const retryAttachment = record.parse(+ list.parse(retryMessage.attachments)[0],+ );expect(retryAttachment.id).not.toBe(originalAttachment.id);const stored = await service.attachment(String(retryAttachment.id), userId);expect(Buffer.from(stored.content)).toEqual(bytes);@@ -578,8 +589,8 @@ describe("ConversationsService", () => {userId,);const sourceDetail = await service.builderById(source.id, userId);- const sourceMessage = recordOf(sourceDetail.submissions[0]?.message);- const attachment = recordOf(arrayOf(sourceMessage.attachments)[0]);+ const sourceMessage = record.parse(sourceDetail.submissions[0]?.message);+ const attachment = record.parse(list.parse(sourceMessage.attachments)[0]);let submitError: unknown;try {@@ -842,13 +853,3 @@ describe("ConversationsService", () => {).toBe(1);});});--function recordOf(value: unknown): Record<string, unknown> {- return value && typeof value === "object" && !Array.isArray(value)- ? (value as Record<string, unknown>)- : {};-}--function arrayOf(value: unknown): unknown[] {- return Array.isArray(value) ? value : [];-}diff --git a/apps/api/test/error-formatter.spec.ts b/apps/api/test/error-formatter.spec.tsindex 6cd27af7..f6126cdd 100644--- a/apps/api/test/error-formatter.spec.ts+++ b/apps/api/test/error-formatter.spec.ts@@ -3,7 +3,7 @@ import { z } from "zod";import { setResearchKeyInput } from "../src/settings/settings.contracts";import { readableInputError } from "../src/trpc/error-formatter";-const causeOf = (schema: z.ZodType, value: unknown) => {+const causeOf = (schema: z.ZodType, value: z.core.util.JSONType) => {const result = schema.safeParse(value);if (result.success) throw new Error("expected the parse to fail");return result.error;diff --git a/apps/api/test/logging.spec.ts b/apps/api/test/logging.spec.tsindex 0367b66b..20664959 100644--- a/apps/api/test/logging.spec.ts+++ b/apps/api/test/logging.spec.ts@@ -121,11 +121,13 @@ describe("request context", () => {});});+type MiddlewareRun = {+ requestId: string;+ seen: string | undefined;+};+describe("RequestLoggerMiddleware", () => {- function run(headers: Record<string, string>): {- requestId: string;- seen: string | undefined;- } {+ function run(headers: Record<string, string>): MiddlewareRun {const middleware = new RequestLoggerMiddleware();let requestId = "";let seen: string | undefined;diff --git a/apps/api/test/mailbox-api-client.spec.ts b/apps/api/test/mailbox-api-client.spec.tsindex 89315ae9..25688194 100644--- a/apps/api/test/mailbox-api-client.spec.ts+++ b/apps/api/test/mailbox-api-client.spec.ts@@ -1,4 +1,5 @@import { afterEach, describe, expect, it } from "bun:test";+import type { z } from "zod";import { MailboxApiClient } from "../src/mailbox/mailbox-api.client";const realFetch = globalThis.fetch;@@ -9,7 +10,7 @@ afterEach(() => {function stub(status: number,- body: unknown,+ body: z.core.util.JSONType,headers: Record<string, string> = {},): void {globalThis.fetch = (async () =>diff --git a/apps/api/test/outlook-sync.spec.ts b/apps/api/test/outlook-sync.spec.tsindex 89b33985..38b2adb2 100644--- a/apps/api/test/outlook-sync.spec.ts+++ b/apps/api/test/outlook-sync.spec.ts@@ -1,5 +1,6 @@import { describe, expect, it } from "bun:test";import type { MailboxSyncModel as MailboxSync } from "@crm/db";+import type { SyncSource } from "../src/mailbox/mailbox.constants";import type { MailboxTokenService } from "../src/mailbox/mailbox-token.service";import type { SyncStateService } from "../src/mailbox/sync-state.service";import type {@@ -18,6 +19,13 @@ type NotOk =const ok = <T>(data: T): Ok<T> => ({ outcome: "ok", data });+type StoreOptions = { mailbox: string; origin: SyncSource };++type GraphPage = {+ value: GraphMessage[];+ "@odata.nextLink"?: string;+};+const row = {id: "sync-1",userId: "user-1",@@ -51,6 +59,13 @@ function harness(options: {const pages = options.pages ?? [[]];let index = 0;+ const page = (at: number): GraphPage => {+ const body: GraphPage = { value: pages[at] ?? [] };+ if (at + 1 < pages.length) body["@odata.nextLink"] = `next-${at + 1}`;++ return body;+ };+const graph = {async me() {if (options.meDelayMs) {@@ -66,19 +81,11 @@ function harness(options: {},async listMessages() {index = 0;- return ok({- value: pages[0] ?? [],- ...(pages.length > 1 ? { "@odata.nextLink": "next-1" } : {}),- });+ return ok(page(0));},async nextPage() {index += 1;- return ok({- value: pages[index] ?? [],- ...(index + 1 < pages.length- ? { "@odata.nextLink": `next-${index + 1}` }- : {}),- });+ return ok(page(index));},} as unknown as GraphClient;@@ -109,7 +116,11 @@ function harness(options: {async context() {return {};},- async store(_row: MailboxSync, _options: unknown, parsed: IncomingMessage) {+ async store(+ _row: MailboxSync,+ _options: StoreOptions,+ parsed: IncomingMessage,+ ) {stored.push(parsed);return true;},diff --git a/apps/api/test/slack-channels.spec.ts b/apps/api/test/slack-channels.spec.tsindex d9e6ef0e..558a22e5 100644--- a/apps/api/test/slack-channels.spec.ts+++ b/apps/api/test/slack-channels.spec.ts@@ -6,7 +6,10 @@ const realFetch = globalThis.fetch;const realSecret = process.env.AGENT_BRIDGE_SECRET;function agentAnswers(status: number, body: string | null) {- globalThis.fetch = (async (_url: unknown, _init?: RequestInit) =>+ globalThis.fetch = (async (+ _url: string | URL | Request,+ _init?: RequestInit,+ ) =>new Response(body, {status,headers: { "content-type": "application/json" },diff --git a/apps/api/test/sso.spec.ts b/apps/api/test/sso.spec.tsindex 46486d7e..6f28a3cc 100644--- a/apps/api/test/sso.spec.ts+++ b/apps/api/test/sso.spec.ts@@ -20,8 +20,10 @@ const LIST = {pageSize: 25,};+type Seen = { providerWhere?: unknown };+function service(role: string | null, rows: Row[] = []) {- const seen: { providerWhere?: unknown } = {};+ const seen: Seen = {};const db = {member: {diff --git a/apps/app/app/(app)/[slug]/(agent-builder)/agents/[agentId]/page.tsx b/apps/app/app/(app)/[slug]/(agent-builder)/agents/[agentId]/page.tsxindex baaf9e59..1f5aa61c 100644--- a/apps/app/app/(app)/[slug]/(agent-builder)/agents/[agentId]/page.tsx+++ b/apps/app/app/(app)/[slug]/(agent-builder)/agents/[agentId]/page.tsx@@ -31,13 +31,9 @@ async function PrefetchedTeamAgent({const client = getServerTrpcClient();const [agent, runs, activity] = await Promise.all([- client.agents.byId.query({ id: agentId }).catch(nullIfMissing),- client.agents.history- .query({ id: agentId, limit: 50 })- .catch(nullIfMissing),- client.agents.activity- .query({ id: agentId, limit: 100 })- .catch(nullIfMissing),+ nullIfMissing(client.agents.byId.query({ id: agentId })),+ nullIfMissing(client.agents.history.query({ id: agentId, limit: 50 })),+ nullIfMissing(client.agents.activity.query({ id: agentId, limit: 100 })),]);if (!agent || !runs || !activity) notFound();diff --git a/apps/app/app/(app)/[slug]/(agent-builder)/chat/[chatId]/page.tsx b/apps/app/app/(app)/[slug]/(agent-builder)/chat/[chatId]/page.tsxindex 68014cd9..21e32bed 100644--- a/apps/app/app/(app)/[slug]/(agent-builder)/chat/[chatId]/page.tsx+++ b/apps/app/app/(app)/[slug]/(agent-builder)/chat/[chatId]/page.tsx@@ -30,16 +30,16 @@ async function PrefetchedAgentChat({const client = getServerTrpcClient();if (isSharedChatToken(chatId)) {- const shared = await client.conversations.shared- .query({ token: chatId })- .catch(nullIfMissing);+ const shared = await nullIfMissing(+ client.conversations.shared.query({ token: chatId }),+ );return <AgentBuilderChat conversationId={chatId} initialData={shared} />;}- const conversation = await client.conversations.builderById- .query({ id: chatId })- .catch(nullIfMissing);+ const conversation = await nullIfMissing(+ client.conversations.builderById.query({ id: chatId }),+ );if (!conversation) notFound();diff --git a/apps/app/app/(app)/[slug]/(agent-builder)/missing-record.ts b/apps/app/app/(app)/[slug]/(agent-builder)/missing-record.tsindex 66d6f58a..e14a5a16 100644--- a/apps/app/app/(app)/[slug]/(agent-builder)/missing-record.ts+++ b/apps/app/app/(app)/[slug]/(agent-builder)/missing-record.ts@@ -1,9 +1,13 @@import { TRPCClientError } from "@trpc/client";-export function nullIfMissing(error: unknown): null {- if (error instanceof TRPCClientError && error.data?.code === "NOT_FOUND") {- return null;- }+export async function nullIfMissing<T>(query: Promise<T>): Promise<T | null> {+ try {+ return await query;+ } catch (error) {+ if (error instanceof TRPCClientError && error.data?.code === "NOT_FOUND") {+ return null;+ }- throw error;+ throw error;+ }}diff --git a/apps/app/app/(app)/[slug]/layout.tsx b/apps/app/app/(app)/[slug]/layout.tsxindex b993c211..863d0da8 100644--- a/apps/app/app/(app)/[slug]/layout.tsx+++ b/apps/app/app/(app)/[slug]/layout.tsx@@ -40,20 +40,25 @@ export default function AppLayout({);}+async function loadWorkspace() {+ try {+ return await getServerQueryClient().fetchQuery(+ getServerTrpc().workspace.get.queryOptions(),+ );+ } catch (error) {+ unstable_rethrow(error);+ return null;+ }+}+async function WorkspaceHeader({params,}: Pick<LayoutProps<"/[slug]">, "params">) {await connection();- const workspacePromise = getServerQueryClient()- .fetchQuery(getServerTrpc().workspace.get.queryOptions())- .catch((error: unknown) => {- unstable_rethrow(error);- return null;- });const [{ user }, { slug }, workspace] = await Promise.all([requireMailboxAccess(),params,- workspacePromise,+ loadWorkspace(),]);if (workspace && workspace.slug !== slug) notFound();diff --git a/apps/app/app/(app)/[slug]/overview-scope.tsx b/apps/app/app/(app)/[slug]/overview-scope.tsxindex 8a0ceb9d..dcc0632c 100644--- a/apps/app/app/(app)/[slug]/overview-scope.tsx+++ b/apps/app/app/(app)/[slug]/overview-scope.tsx@@ -8,10 +8,10 @@ import {overviewParsers,} from "./overview-search-params";-const LABELS: Record<OverviewScope, string> = {+const LABELS = {me: "Me",everyone: "Everyone",-};+} satisfies Record<OverviewScope, string>;function isScope(value: string): value is OverviewScope {return (OVERVIEW_SCOPES as readonly string[]).includes(value);diff --git a/apps/app/app/(app)/[slug]/sales-dashboard.tsx b/apps/app/app/(app)/[slug]/sales-dashboard.tsxindex cba5acee..d4a3162d 100644--- a/apps/app/app/(app)/[slug]/sales-dashboard.tsx+++ b/apps/app/app/(app)/[slug]/sales-dashboard.tsx@@ -58,11 +58,8 @@ export function SalesDashboard({ summary }: { summary: Summary }) {} = summary;const money = (cents: number) => formatMoneyCompact(cents, reportingCurrency);- const exact = (value: unknown) =>- formatMoney(- typeof value === "number" ? value : Number(value),- reportingCurrency,- );+ const exact = (value: number | string) =>+ formatMoney(Number(value), reportingCurrency);const hasTrend = trend.some((point) => point.won > 0 || point.created > 0);diff --git a/apps/app/app/(app)/[slug]/settings/connections/google-connection.tsx b/apps/app/app/(app)/[slug]/settings/connections/google-connection.tsxindex bc9adb38..20f3fca8 100644--- a/apps/app/app/(app)/[slug]/settings/connections/google-connection.tsx+++ b/apps/app/app/(app)/[slug]/settings/connections/google-connection.tsx@@ -123,10 +123,12 @@ function GoogleUnavailable() {);}-const CONNECT_ERRORS: Record<string, string> = {- "email_doesn't_match":+const CONNECT_ERRORS = new Map([+ [+ "email_doesn't_match","That Google account has a different email address to the one you sign in with, so it cannot be attached to your account. Connect the Google account that matches your sign-in address.",-};+ ],+]);function ConnectGoogle({slug,@@ -196,7 +198,7 @@ function ConnectGoogle({<Icon icon={Warning} /><AlertTitle>Google did not finish connecting</AlertTitle><AlertDescription>- {CONNECT_ERRORS[connectError] ??+ {CONNECT_ERRORS.get(connectError) ??"Google returned an error before the connection was made. Try again."}</AlertDescription></Alert>diff --git a/apps/app/app/(app)/[slug]/settings/connections/microsoft-connection.tsx b/apps/app/app/(app)/[slug]/settings/connections/microsoft-connection.tsxindex 9418fe80..6be4afa5 100644--- a/apps/app/app/(app)/[slug]/settings/connections/microsoft-connection.tsx+++ b/apps/app/app/(app)/[slug]/settings/connections/microsoft-connection.tsx@@ -42,10 +42,12 @@ import { useTRPC } from "@/lib/trpc/client";const AUTO_CREATE = "Add the company and contact when you reply to someone new";-const CONNECT_ERRORS: Record<string, string> = {- "email_doesn't_match":+const CONNECT_ERRORS = new Map([+ [+ "email_doesn't_match","That Microsoft account has a different email address to the one you sign in with, so it cannot be attached to your account. Connect the Microsoft account that matches your sign-in address.",-};+ ],+]);function MicrosoftUnavailable() {return (@@ -134,7 +136,7 @@ function ConnectMicrosoft({<Icon icon={Warning} /><AlertTitle>Microsoft did not finish connecting</AlertTitle><AlertDescription>- {CONNECT_ERRORS[connectError] ??+ {CONNECT_ERRORS.get(connectError) ??"Microsoft returned an error before the connection was made. Try again."}</AlertDescription></Alert>diff --git a/apps/app/app/(app)/[slug]/settings/connections/slack/slack-connect-button.tsx b/apps/app/app/(app)/[slug]/settings/connections/slack/slack-connect-button.tsxindex 919e7958..74b7fc38 100644--- a/apps/app/app/(app)/[slug]/settings/connections/slack/slack-connect-button.tsx+++ b/apps/app/app/(app)/[slug]/settings/connections/slack/slack-connect-button.tsx@@ -5,17 +5,28 @@ import { Button } from "@crm/ui/components/button";import { useState } from "react";import { toast } from "sonner";-const CONNECT_ERRORS: Record<string, string> = {- access_denied: "Slack installation was cancelled before access was granted.",- account_already_linked_to_different_user:+const CONNECT_ERRORS = new Map([+ [+ "access_denied",+ "Slack installation was cancelled before access was granted.",+ ],+ [+ "account_already_linked_to_different_user","That Slack installer is already linked to another CRM account.",- "email_doesn't_match":+ ],+ [+ "email_doesn't_match","The Slack installer's email must match the CRM account you are signed in with.",- oauth_code_verification_failed:+ ],+ [+ "oauth_code_verification_failed","Slack rejected the app credentials or redirect URL. Check the client ID, client secret, and OAuth redirect URL, then try again.",- user_info_is_missing:+ ],+ [+ "user_info_is_missing","Slack did not return the installer's profile. Confirm the app has users:read and users:read.email, reinstall it, then try again.",-};+ ],+]);async function startSlackOAuth(slug: string) {try {@@ -77,7 +88,7 @@ export function SlackConnectButton({</Button>{connectError ? (<p role="alert" className="max-w-sm text-destructive text-xs">- {CONNECT_ERRORS[connectError] ??+ {CONNECT_ERRORS.get(connectError) ??`Slack could not be connected (${connectError.replaceAll("_", " ")}).`}</p>) : null}diff --git a/apps/app/app/(landing)/grant-access/grant-access.tsx b/apps/app/app/(landing)/grant-access/grant-access.tsxindex acf72981..9f5ee1fa 100644--- a/apps/app/app/(landing)/grant-access/grant-access.tsx+++ b/apps/app/app/(landing)/grant-access/grant-access.tsx@@ -10,10 +10,17 @@ import GoogleLogo from "@crm/ui/components/brand-logos/google";import MicrosoftLogo from "@crm/ui/components/brand-logos/microsoft";import { Button } from "@crm/ui/components/button";import { Spinner } from "@crm/ui/components/spinner";+import type { FC, SVGProps } from "react";import { useState } from "react";import { toast } from "sonner";import { signOutAndRedirect } from "@/lib/sign-out";+type ProviderGrant = {+ label: string;+ scopes: readonly string[];+ Logo: FC<SVGProps<SVGSVGElement>>;+};+const PROVIDERS = {google: {label: "Grant Google access",@@ -25,7 +32,7 @@ const PROVIDERS = {scopes: [...MICROSOFT_SYNC_SCOPES],Logo: MicrosoftLogo,},-} as const satisfies Record<MailboxProviderId, unknown>;+} as const satisfies Record<MailboxProviderId, ProviderGrant>;export function GrantAccess({providers,diff --git a/apps/app/app/(landing)/grant-access/page.tsx b/apps/app/app/(landing)/grant-access/page.tsxindex f5ee181d..39d306de 100644--- a/apps/app/app/(landing)/grant-access/page.tsx+++ b/apps/app/app/(landing)/grant-access/page.tsx@@ -1,4 +1,4 @@-import { mailboxGrantsNeeded } from "@crm/auth";+import { type MailboxProviderId, mailboxGrantsNeeded } from "@crm/auth";import type { Metadata } from "next";import { redirect } from "next/navigation";import { AuthHeading, AuthShell } from "@/components/auth-shell";@@ -11,12 +11,12 @@ export const metadata: Metadata = {export const instant = false;-const DESCRIPTION: Record<string, string> = {+const DESCRIPTION = {google:"This CRM reads your Gmail and Calendar so meetings and email threads show up on the right company. It is read-only — nothing is ever sent on your behalf.",microsoft:"This CRM reads your Outlook mail so email threads show up on the right company. It is read-only — nothing is ever sent on your behalf.",-};+} satisfies Record<MailboxProviderId, string>;const BOTH ="This CRM reads your mail and calendar so meetings and email threads show up on the right company. It is read-only — nothing is ever sent on your behalf.";diff --git a/apps/app/app/(landing)/sign-in/page.tsx b/apps/app/app/(landing)/sign-in/page.tsxindex 0a7929fc..5b0e451a 100644--- a/apps/app/app/(landing)/sign-in/page.tsx+++ b/apps/app/app/(landing)/sign-in/page.tsx@@ -30,6 +30,16 @@ async function signInOptions(): Promise<SignInOptions | null> {}}+async function currentSession() {+ try {+ return await getSession();+ } catch (error) {+ unstable_rethrow(error);+ console.error("Sign-in: could not read the session.", error);+ return null;+ }+}+export default function SignInPage({ searchParams }: PageProps<"/sign-in">) {return (<AuthShell>@@ -51,11 +61,7 @@ async function SignIn({searchParams,}: Pick<PageProps<"/sign-in">, "searchParams">) {const [session, options, { method }] = await Promise.all([- getSession().catch((error: unknown) => {- unstable_rethrow(error);- console.error("Sign-in: could not read the session.", error);- return null;- }),+ currentSession(),signInOptions(),searchParams,]);diff --git a/apps/app/app/(landing)/sign-in/social-sign-in.tsx b/apps/app/app/(landing)/sign-in/social-sign-in.tsxindex 021d2249..882f89c1 100644--- a/apps/app/app/(landing)/sign-in/social-sign-in.tsx+++ b/apps/app/app/(landing)/sign-in/social-sign-in.tsx@@ -6,13 +6,19 @@ import GoogleLogo from "@crm/ui/components/brand-logos/google";import MicrosoftLogo from "@crm/ui/components/brand-logos/microsoft";import { Button } from "@crm/ui/components/button";import { Spinner } from "@crm/ui/components/spinner";+import type { FC, SVGProps } from "react";import { useState } from "react";import { toast } from "sonner";+type ProviderChoice = {+ label: string;+ Logo: FC<SVGProps<SVGSVGElement>>;+};+const PROVIDERS = {google: { label: "Continue with Google", Logo: GoogleLogo },microsoft: { label: "Continue with Microsoft", Logo: MicrosoftLogo },-} as const satisfies Record<MailboxProviderId, unknown>;+} as const satisfies Record<MailboxProviderId, ProviderChoice>;export function SocialSignIn({ provider }: { provider: MailboxProviderId }) {const [pending, setPending] = useState(false);diff --git a/apps/app/app/t/[site]/route.ts b/apps/app/app/t/[site]/route.tsindex 082350e7..98e5e16f 100644--- a/apps/app/app/t/[site]/route.ts+++ b/apps/app/app/t/[site]/route.ts@@ -35,14 +35,14 @@ export async function GET(`${origin}/api/t/e`,);- return new Response(source, {- headers: {- "content-type": "application/javascript; charset=utf-8",- "cache-control": `public, max-age=${CONFIG_MAX_AGE_SECONDS}, s-maxage=${CONFIG_MAX_AGE_SECONDS}`,- "x-content-type-options": "nosniff",- ...(payload.hash ? { etag: `"${payload.hash}"` } : {}),- },+ const headers = new Headers({+ "content-type": "application/javascript; charset=utf-8",+ "cache-control": `public, max-age=${CONFIG_MAX_AGE_SECONDS}, s-maxage=${CONFIG_MAX_AGE_SECONDS}`,+ "x-content-type-options": "nosniff",});+ if (payload.hash) headers.set("etag", `"${payload.hash}"`);++ return new Response(source, { headers });}function empty(): Response {diff --git a/apps/app/components/agent-builder/agent-builder-chat.tsx b/apps/app/components/agent-builder/agent-builder-chat.tsxindex f377fe5f..b2e3626e 100644--- a/apps/app/components/agent-builder/agent-builder-chat.tsx+++ b/apps/app/components/agent-builder/agent-builder-chat.tsx@@ -35,12 +35,14 @@ import { Reasoning } from "@crm/ui/components/reasoning";import { ThinkingIndicator } from "@crm/ui/components/thinking-indicator";import { useMountEffect } from "@crm/ui/hooks/use-mount-effect";import { cn } from "@crm/ui/lib/utils";+import type { AgentManifestSummary } from "@crm/validation/agent-manifest";import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";import { Client, type MessageStreamEvent } from "eve/client";import type { EveMessage, EveMessageInputRequest } from "eve/react";import Link from "next/link";import { Fragment, type ReactNode, useState } from "react";import { toast } from "sonner";+import { z } from "zod";import {AgentClarificationComposer,type ClarificationResponse,@@ -100,27 +102,87 @@ const BUILDER_STEP_ARTIFACTS = ["agent/manifest.json","agent/README.md",] as const;-type DraftVersion = {- id: string;- status: string;- manifest: unknown;-};+const submissionResource = z.object({+ kind: z.enum(["integration", "company", "contact", "deal"]),+ id: z.string().min(1),+ label: z.string().min(1),+ detail: z.string().nullable().optional().catch(null),+ imageUrl: z.string().nullable().optional().catch(null),+});++const storedAttachment = z.object({+ id: z.string().min(1),+ name: z.string().min(1),+ type: z.string().min(1),+ size: z.number(),+ previewUrl: z.string().nullable().optional().catch(null),+});++const uploadAttachment = z.object({+ name: z.string().min(1),+ type: z.string().min(1),+ size: z.number(),+ contentBase64: z.string().min(1),+ previewUrl: z.string().nullable().optional().catch(null),+});++const builderMessage = z+ .object({+ text: z.string().nullable().catch(null),+ resources: z.array(submissionResource).catch([]),+ attachments: z+ .array(z.union([storedAttachment, uploadAttachment]))+ .catch([]),+ inputResponse: z+ .object({ requestId: z.string().min(1) })+ .nullable()+ .catch(null),+ })+ .catch({+ text: null,+ resources: [],+ attachments: [],+ inputResponse: null,+ });-type BuilderSubmission = {- id: string;- createdAt: string;- clientRequestId?: string | null;- commandType: "CHAT" | "CREATE_AGENT";- message: unknown;- status: string;- errorMessage: string | null;-};+type BuilderMessage = z.infer<typeof builderMessage>;++const builderSubmissions = z.array(+ z.object({+ id: z.string(),+ createdAt: z.string(),+ clientRequestId: z.string().nullable().catch(null),+ commandType: z.enum(["CHAT", "CREATE_AGENT"]),+ message: builderMessage,+ status: z.string(),+ errorMessage: z.string().nullable().catch(null),+ }),+);++type BuilderSubmission = z.infer<typeof builderSubmissions>[number];++const streamEventShape = z.object({+ type: z.string().min(1),+ meta: z.object({ id: z.string().min(1), at: z.string().min(1) }),+});++const streamEvents = z+ .array(+ z+ .custom<MessageStreamEvent>(+ (value) => streamEventShape.safeParse(value).success,+ )+ .nullable()+ .catch(null),+ )+ .catch([])+ .transform((events) => events.filter((event) => event !== null));type PendingSubmission = {clientRequestId: string;createdAt: string;commandType: "CHAT" | "CREATE_AGENT";- message: unknown;+ message: BuilderMessage;};export function AgentBuilderChat({@@ -232,7 +294,7 @@ export function AgentBuilderChat({}const data = conversation.data ?? (initialData as Conversation);- const submissions = data.submissions as BuilderSubmission[];+ const submissions = builderSubmissions.parse(data.submissions);const confirmedRequestIds = new Set(submissions.map((submission) => submission.clientRequestId)@@ -241,8 +303,7 @@ export function AgentBuilderChat({const pendingSubmissions = sending.filter((item) => !confirmedRequestIds.has(item.clientRequestId),);- const persistedEvents = (events.data ??- []) as unknown as MessageStreamEvent[];+ const persistedEvents = streamEvents.parse(events.data ?? []);const streamKey = builderSessionStreamKey(data.sessionId,submissions.at(-1)?.id ?? null,@@ -287,6 +348,7 @@ export function AgentBuilderChat({text: prompt.message,resources: prompt.resources,attachments: prompt.attachments,+ inputResponse: null,},},]);@@ -387,6 +449,7 @@ export function AgentBuilderChat({submission={{id: item.clientRequestId,createdAt: item.createdAt,+ clientRequestId: item.clientRequestId,commandType: item.commandType,message: item.message,status: "PENDING",@@ -539,13 +602,15 @@ function BuilderEventFollower({}};- void follow()- .catch((error: unknown) => {+ void (async () => {+ try {+ await follow();+ } catch (error) {if (!controller.signal.aborted) console.error(error);- })- .finally(() => {+ } finally {if (!controller.signal.aborted) onEnded();- });+ }+ })();return () => controller.abort();});@@ -601,9 +666,8 @@ function SharedAgentChat({}: {conversation: SharedConversation;}) {- const submissions =- conversation.submissions as unknown as BuilderSubmission[];- const events = conversation.events as unknown as MessageStreamEvent[];+ const submissions = builderSubmissions.parse(conversation.submissions);+ const events = streamEvents.parse(conversation.events);const messages = messagesFromEvents(events);const timeline = conversationTimeline(submissions, events, messages);const answeredQuestionIds = questionResponseIds(submissions);@@ -745,13 +809,14 @@ function UserSubmission({error: string | null;sending?: boolean;}) {- const message = builderMessageOf(submission.message);+ const message = submission.message;+ const messageText = message.text ?? "Message unavailable";const command =submission.commandType === "CREATE_AGENT"- ? consumeBuilderCommand(message.text)+ ? consumeBuilderCommand(messageText): null;- const text = command?.body ?? message.text;- const response = inputResponseOf(submission.message);+ const text = command?.body ?? messageText;+ const response = message.inputResponse;return (<div@@ -1188,11 +1253,11 @@ function ReviewAgentCard({const workspaceUrl = useWorkspaceUrl();const version = conversation.createdVersions.find((candidate) => candidate.id === versionId,- ) as DraftVersion | undefined;+ );const agent = conversation.agent;- const manifest = manifestOf(version?.manifest);if (!version || !agent) return null;+ const manifest = manifestOf(version.manifest);return (<div className="flex flex-col gap-5">@@ -1430,19 +1495,6 @@ const RESOURCE_ICONS = {deal: Partnership,} as const;-function builderMessageOf(message: unknown) {- const row = recordOf(message);- return {- text: typeof row.text === "string" ? row.text : "Message unavailable",- resources: Array.isArray(row.resources)- ? (row.resources as BuilderPrompt["resources"])- : [],- attachments: Array.isArray(row.attachments)- ? (row.attachments as BuilderPrompt["attachments"])- : [],- };-}-function hasQueuedQuestionResponse(submissions: BuilderSubmission[],requestId: string,@@ -1452,7 +1504,7 @@ function hasQueuedQuestionResponse(return false;}- return inputResponseOf(submission.message)?.requestId === requestId;+ return submission.message.inputResponse?.requestId === requestId;});}@@ -1465,36 +1517,25 @@ function questionResponseIds(return [];}- const response = inputResponseOf(submission.message);+ const response = submission.message.inputResponse;return response ? [response.requestId] : [];}),);}-function inputResponseOf(message: unknown): { requestId: string } | null {- const response = recordOf(recordOf(message).inputResponse);- return typeof response.requestId === "string" && response.requestId- ? { requestId: response.requestId }- : null;-}-function retryPromptOf(submission: BuilderSubmission | undefined,): BuilderPrompt | null {if (!submission) return null;- const row = recordOf(submission.message);- if (Object.keys(recordOf(row.inputResponse)).length > 0) return null;- if (typeof row.text !== "string" || !row.text.trim()) return null;+ const { message } = submission;+ if (message.inputResponse) return null;+ if (!message.text?.trim()) return null;return {commandType: submission.commandType,- message: row.text,- resources: Array.isArray(row.resources)- ? (row.resources as BuilderPrompt["resources"])- : [],- attachments: Array.isArray(row.attachments)- ? (row.attachments as BuilderPrompt["attachments"])- : [],+ message: message.text,+ resources: message.resources,+ attachments: message.attachments,};}@@ -1510,30 +1551,11 @@ function sharedConversationNeedsPolling(return !eventStreamSettled(conversation.events);}-function manifestOf(value: unknown) {- const manifest = recordOf(value);- const triggers = Array.isArray(manifest.triggers)- ? manifest.triggers.map(recordOf)- : [];- const dataScope = recordOf(manifest.dataScope);- const actions = Array.isArray(manifest.actions)- ? manifest.actions.map(recordOf)- : [];- const access = Array.isArray(manifest.access)- ? manifest.access.filter((item): item is string => typeof item === "string")- : [];-+function manifestOf(manifest: AgentManifestSummary) {return {- name:- typeof manifest.name === "string" && manifest.name.trim()- ? manifest.name.trim()- : null,- description:- typeof manifest.description === "string" && manifest.description.trim()- ? manifest.description.trim()- : null,+ name: manifest.name?.trim() || null,trigger:- triggers+ manifest.triggers.map((trigger) =>trigger.type === "MANUAL"? "On demand"@@ -1543,25 +1565,22 @@ function manifestOf(value: unknown) {),).join(" · ") || "On demand",- looksAt: textOf(dataScope.summary, "CRM records in the approved scope"),+ looksAt: textOf(+ manifest.dataScope.summary,+ "CRM records in the approved scope",+ ),action: compactSummary(- actions[0]?.summary,+ manifest.actions[0]?.summary,"Perform the requested team action",),- access,+ access: manifest.access,};}-function compactSummary(value: unknown, fallback: string): string {+function compactSummary(value: string | undefined, fallback: string): string {return textOf(value, fallback).replace(/\s+\([^()]+\)\s*\.?$/, "");}-function recordOf(value: unknown): Record<string, unknown> {- return value && typeof value === "object" && !Array.isArray(value)- ? (value as Record<string, unknown>)- : {};-}--function textOf(value: unknown, fallback: string): string {- return typeof value === "string" && value.trim() ? value : fallback;+function textOf(value: string | undefined, fallback: string): string {+ return value?.trim() ? value : fallback;}diff --git a/apps/app/components/agent-builder/agent-builder-sidebar.tsx b/apps/app/components/agent-builder/agent-builder-sidebar.tsxindex b8502b4d..7b58eba0 100644--- a/apps/app/components/agent-builder/agent-builder-sidebar.tsx+++ b/apps/app/components/agent-builder/agent-builder-sidebar.tsx@@ -234,18 +234,16 @@ function groupConversations(conversations: Conversation[], now: number) {if (!now) return [];const labels: ChatDateGroup[] = ["Today", "Yesterday", "Last 7 days"];- const items: Record<ChatDateGroup, Conversation[]> = {- Today: [],- Yesterday: [],- "Last 7 days": [],- };+ const items = new Map<ChatDateGroup, Conversation[]>(+ labels.map((label) => [label, []]),+ );for (const conversation of conversations) {const label = chatDateGroup(conversation.lastMessageAt, now);- if (label) items[label].push(conversation);+ if (label) items.get(label)?.push(conversation);}return labels- .map((label) => ({ label, items: items[label] }))+ .map((label) => ({ label, items: items.get(label) ?? [] })).filter((group) => group.items.length > 0);}diff --git a/apps/app/components/agent-builder/agent-capabilities.tsx b/apps/app/components/agent-builder/agent-capabilities.tsxindex 9fe8fa03..1f016172 100644--- a/apps/app/components/agent-builder/agent-capabilities.tsx+++ b/apps/app/components/agent-builder/agent-capabilities.tsx@@ -22,27 +22,21 @@ import {} from "@/components/slack/channel-picker";import { useSlackChannels } from "@/components/slack/use-slack-channels";import { useTRPC } from "@/lib/trpc/client";+import type { RouterOutputs } from "@/lib/trpc/types";import { CreateChannelDialog } from "./create-channel-dialog";-export type Resource = { id: string; kind: string; label: string };+export type Capabilities = RouterOutputs["agents"]["byId"]["capabilities"];-export type Capabilities = {- readable: boolean;- problem: string | null;- channel: { kind: "channel" | "user"; id: string; label: string } | null;- actions: Array<{ type: string; provider: string; summary: string }>;- dataScope: {- mode: "SELECTED" | "WORKSPACE";- summary: string;- resources: Resource[];- } | null;-};+export type Resource = Extract<+ Capabilities,+ { readable: true }+>["dataScope"]["resources"][number];-const ACTION_LABELS: Record<string, string> = {- "slack.message.post": "Post a message",- "crm.activity.create": "Write a note or task on the record",- "run.summary": "Write a summary of the run",-};+const ACTION_LABELS = new Map([+ ["slack.message.post", "Post a message"],+ ["crm.activity.create", "Write a note or task on the record"],+ ["run.summary", "Write a summary of the run"],+]);export function AgentCapabilities({agentId,@@ -136,29 +130,22 @@ export function AgentCapabilities({revise.mutate({id: agentId,clientRequestId: crypto.randomUUID(),- ...(channelChanged && picked- ? { channel: { id: picked.id, name: picked.name } }- : {}),- ...(actionsChanged- ? {- actions: capabilities.actions- .map((action) => action.type)- .filter((type) => !off.includes(type)),- }- : {}),- ...(scopeChanged- ? {- resources: shownResources.map((resource) => ({- id: resource.id,- kind: resource.kind as- | "company"- | "contact"- | "deal"- | "integration",- label: resource.label,- })),- }- : {}),+ channel:+ channelChanged && picked+ ? { id: picked.id, name: picked.name }+ : undefined,+ actions: actionsChanged+ ? capabilities.actions+ .map((action) => action.type)+ .filter((type) => !off.includes(type))+ : undefined,+ resources: scopeChanged+ ? shownResources.map((resource) => ({+ id: resource.id,+ kind: resource.kind,+ label: resource.label,+ }))+ : undefined,});};@@ -207,7 +194,7 @@ export function AgentCapabilities({><div className="min-w-0 flex-1"><p className="text-sm">- {ACTION_LABELS[action.type] ?? action.type}+ {ACTION_LABELS.get(action.type) ?? action.type}</p><p className="text-muted-foreground text-xs">{action.summary || action.provider}diff --git a/apps/app/components/agent-builder/agent-composer.tsx b/apps/app/components/agent-builder/agent-composer.tsxindex df161292..4bed19ab 100644--- a/apps/app/components/agent-builder/agent-composer.tsx+++ b/apps/app/components/agent-builder/agent-composer.tsx@@ -1457,12 +1457,12 @@ function ResourceResultsSkeleton() {);}-const RESOURCE_ICONS: Record<BuilderResource["kind"], CarbonIcon> = {+const RESOURCE_ICONS = {integration: Application,company: Building,contact: User,deal: Partnership,-};+} satisfies Record<BuilderResource["kind"], CarbonIcon>;function ResourceButton({icon,diff --git a/apps/app/components/agent-builder/agent-history.tsx b/apps/app/components/agent-builder/agent-history.tsxindex 089032f6..7df33a1e 100644--- a/apps/app/components/agent-builder/agent-history.tsx+++ b/apps/app/components/agent-builder/agent-history.tsx@@ -19,23 +19,33 @@ import { Button } from "@crm/ui/components/button";import { Icon } from "@crm/ui/components/icon";import { cn } from "@crm/ui/lib/utils";import { useState } from "react";+import { z } from "zod";import { runFailureReason } from "@/lib/agent-run-failure";import type { RouterOutputs } from "@/lib/trpc/types";type Runs = RouterOutputs["agents"]["history"];type Activity = RouterOutputs["agents"]["activity"];-type RunRow = Omit<Runs[number], "events"> & {- events: Array<{- id: string;- type: string;- data: unknown;- emittedAt: string;- }>;-};-type ActivityRow = Omit<Activity[number], "before" | "after"> & {- before: unknown;- after: unknown;-};+type RunRow = Runs[number];+type AuditRow = Activity[number];++const runEvents = z.array(+ z.object({+ id: z.string(),+ type: z.string(),+ data: z.json(),+ emittedAt: z.string(),+ }),+);++type RunEvent = z.infer<typeof runEvents>[number];++const eventSummary = z+ .object({ summary: z.string().refine((text) => text.trim().length > 0) })+ .transform((event) => event.summary)+ .nullable()+ .catch(null);++const auditChange = z.object({ before: z.json(), after: z.json() });const DATE_FORMATTER = new Intl.DateTimeFormat("en-US", {month: "short",@@ -180,9 +190,7 @@ export function AgentRuns({) : null}</div>- {expanded === run.id ? (- <ExpandedRun run={run as unknown as RunRow} />- ) : null}+ {expanded === run.id ? <ExpandedRun run={run} /> : null}</div>))}@@ -225,8 +233,9 @@ export function AgentRuns({}function ExpandedRun({ run }: { run: RunRow }) {+ const events = runEvents.parse(run.events);const timeline = [- ...run.events.map((event) => ({+ ...events.map((event) => ({kind: "event" as const,at: event.emittedAt,event,@@ -261,7 +270,7 @@ function ExpandedRun({ run }: { run: RunRow }) {{formatTime(entry.at)}</span><span className="min-w-0 flex-1 wrap-break-word text-sm">- {eventLabel(entry.event.type, entry.event.data)}+ {eventLabel(entry.event)}</span><span className="hidden shrink-0 font-mono text-muted-foreground text-xs sm:inline">event@@ -294,8 +303,8 @@ function ExpandedRun({ run }: { run: RunRow }) {)}{run.eventsTruncated ? (<div className="flex min-h-9 items-center border-t px-4 py-2 text-warning text-xs sm:px-5">- Showing the first {run.events.length} of {run.totalEvents} steps.- This run is too long to display in full.+ Showing the first {events.length} of {run.totalEvents} steps. This+ run is too long to display in full.</div>) : null}</div>@@ -327,8 +336,7 @@ function RunMeta({export function AgentActivity({ activity }: { activity: Activity }) {const [kind, setKind] = useState("ALL");- const rows = activity as unknown as ActivityRow[];- const visible = rows.filter(+ const visible = activity.filter((event) => kind === "ALL" || event.type.startsWith(kind),);@@ -374,9 +382,9 @@ export function AgentActivity({ activity }: { activity: Activity }) {<span className="block wrap-break-word text-sm">{event.summary}</span>- {changeDetail(event.before, event.after) ? (+ {changeDetail(event) ? (<span className="block max-w-full whitespace-pre-wrap wrap-break-word font-mono text-muted-foreground text-xs">- {changeDetail(event.before, event.after)}+ {changeDetail(event)}</span>) : null}</span>@@ -400,16 +408,6 @@ export function AgentActivity({ activity }: { activity: Activity }) {);}-function recordOf(value: unknown): Record<string, unknown> {- return value && typeof value === "object" && !Array.isArray(value)- ? (value as Record<string, unknown>)- : {};-}--function textOf(value: unknown, fallback: string): string {- return typeof value === "string" && value.trim() ? value : fallback;-}-function humanStatus(value: string): string {return value.toLowerCase()@@ -434,19 +432,22 @@ function duration(startedAt: string | null, finishedAt: string | null): string {return `${Math.max(0, milliseconds / 1000).toFixed(1)}s`;}-function eventLabel(type: string, data: unknown): string {- const payload = recordOf(data);- return textOf(payload.summary, humanStatus(type.replace(/\./g, " ")));+function eventLabel(event: RunEvent): string {+ return (+ eventSummary.parse(event.data) ??+ humanStatus(event.type.replace(/\./g, " "))+ );}-function changeDetail(before: unknown, after: unknown): string | null {+function changeDetail(event: AuditRow): string | null {+ const { before, after } = auditChange.parse(event);if (!before && !after) return null;const previous = JSON.stringify(before);const next = JSON.stringify(after);return previous && next ? `${previous} → ${next}` : next || previous;}-function exportJson(name: string, value: unknown) {+function exportJson(name: string, value: readonly AuditRow[]) {const url = URL.createObjectURL(new Blob([JSON.stringify(value, null, 2)], { type: "application/json" }),);diff --git a/apps/app/components/agent-builder/agent-result.tsx b/apps/app/components/agent-builder/agent-result.tsxindex 29ebcac4..b857e355 100644--- a/apps/app/components/agent-builder/agent-result.tsx+++ b/apps/app/components/agent-builder/agent-result.tsx@@ -1,4 +1,5 @@import { Skeleton } from "@crm/ui/components/skeleton";+import type { EveToolOutput } from "@crm/validation/eve-tool";import type { ReactNode } from "react";import { anchorResults } from "@/lib/agent-results";import {@@ -22,7 +23,7 @@ function defineResult<T>({skeleton,}: {tool: string;- validate: (output: unknown) => T | null;+ validate: (output: EveToolOutput) => T | null;group?: (results: readonly { itemId: string; value: T }[],) => readonly { itemId: string; value: T }[];@@ -58,22 +59,27 @@ const listSkeleton = (</div>);-const REGISTRY: Record<string, ResultEntry> = {- list_deals: defineResult<DealListResult>({- tool: "list_deals",- validate: dealListResultOf,- group: groupDealListPages,- render: (result, key) => <DealListResultTable key={key} result={result} />,- skeleton: listSkeleton,- }),-};+const REGISTRY = new Map<string, ResultEntry>([+ [+ "list_deals",+ defineResult<DealListResult>({+ tool: "list_deals",+ validate: dealListResultOf,+ group: groupDealListPages,+ render: (result, key) => (+ <DealListResultTable key={key} result={result} />+ ),+ skeleton: listSkeleton,+ }),+ ],+]);export function hasAgentResult(tool: string): boolean {- return tool in REGISTRY;+ return REGISTRY.has(tool);}export function agentResultSkeleton(tool: string): ReactNode {- return REGISTRY[tool]?.skeleton ?? null;+ return REGISTRY.get(tool)?.skeleton ?? null;}export function agentResultsByItem(@@ -81,7 +87,7 @@ export function agentResultsByItem(): Map<string, ReactNode[]> {const rendered = new Map<string, ReactNode[]>();- for (const entry of Object.values(REGISTRY)) {+ for (const entry of REGISTRY.values()) {for (const [itemId, nodes] of entry.anchor(items)) {const bucket = rendered.get(itemId);if (bucket) bucket.push(...nodes);diff --git a/apps/app/components/agent-builder/team-agent-detail.tsx b/apps/app/components/agent-builder/team-agent-detail.tsxindex e85f0cd3..4bfb6777 100644--- a/apps/app/components/agent-builder/team-agent-detail.tsx+++ b/apps/app/components/agent-builder/team-agent-detail.tsx@@ -43,7 +43,7 @@ import {import { useTRPC } from "@/lib/trpc/client";import type { RouterOutputs } from "@/lib/trpc/types";import { useWorkspaceUrl } from "@/lib/use-workspace-url";-import { AgentCapabilities, type Capabilities } from "./agent-capabilities";+import { AgentCapabilities } from "./agent-capabilities";import { AgentCode } from "./agent-code";import { AgentRunsDrawer } from "./agent-runs-drawer";@@ -183,22 +183,14 @@ export function TeamAgentDetail({const data = agent.data ?? initialAgent;const isDraft = data.status === "DRAFT";- const reviewManifest = recordOf(- (- data.reviewVersion as unknown as {- manifest: unknown;- } | null- )?.manifest,- );+ const reviewManifest = data.reviewVersion?.manifest;+ const fallbackDescription = data.description ?? "A durable team automation.";const displayedName = isDraft- ? textOf(reviewManifest.name, data.name)+ ? textOf(reviewManifest?.name, data.name): data.name;const displayedDescription = isDraft- ? textOf(- reviewManifest.description,- data.description ?? "A durable team automation.",- )- : (data.description ?? "A durable team automation.");+ ? textOf(reviewManifest?.description, fallbackDescription)+ : fallbackDescription;const displayedVersionNumber =data.currentVersion?.number ?? data.reviewVersion?.number;const enabledTriggers = data.triggers.filter((trigger) => trigger.enabled);@@ -531,8 +523,7 @@ function DeleteAgentAction({}function AgentOverview({ agent }: { agent: AgentDetail }) {- const detail = agent as unknown as { capabilities?: Capabilities };- const capabilities = detail.capabilities;+ const { capabilities } = agent;const deployed = agent.currentVersion !== null;const canEdit = agent.canManage && deployed;@@ -577,14 +568,8 @@ function _DetailRow({ label, value }: { label: string; value: ReactNode }) {);}-function recordOf(value: unknown): Record<string, unknown> {- return value && typeof value === "object" && !Array.isArray(value)- ? (value as Record<string, unknown>)- : {};-}--function textOf(value: unknown, fallback: string): string {- return typeof value === "string" && value.trim() ? value : fallback;+function textOf(value: string | undefined, fallback: string): string {+ return value?.trim() ? value : fallback;}function formatDate(value: string): string {diff --git a/apps/app/components/crm/agent-panel.tsx b/apps/app/components/crm/agent-panel.tsxindex da2cec42..61990ef0 100644--- a/apps/app/components/crm/agent-panel.tsx+++ b/apps/app/components/crm/agent-panel.tsx@@ -375,17 +375,17 @@ function Failure({ message }: { message: string }) {);}-const TONE_ICONS: Record<Tone, CarbonIcon> = {+const TONE_ICONS = {neutral: CircleDash,success: Checkmark,warning: Warning,-};+} satisfies Record<Tone, CarbonIcon>;-const SOURCE_ICONS: Record<Source["network"], CarbonIcon> = {+const SOURCE_ICONS = {linkedin: LogoLinkedin,github: LogoGithub,web: Document,-};+} satisfies Record<Source["network"], CarbonIcon>;function Item({ item }: { item: TranscriptItem }) {if (item.kind === "said") {@@ -503,14 +503,14 @@ function useSavedConversation({const persist = useEffectEvent(() => {save.mutate({- ...(contactId ? { contactId } : {}),- ...(companyId ? { companyId } : {}),- ...(dealId ? { dealId } : {}),+ contactId: contactId || undefined,+ companyId: companyId || undefined,+ dealId: dealId || undefined,sessionId: sessionId ?? "",continuationToken: token,streamIndex,messageCount: messages,- ...(isNew ? { title: opening.current ?? undefined } : {}),+ title: isNew ? (opening.current ?? undefined) : undefined,},{onSuccess: () => {diff --git a/apps/app/components/crm/fields/field-editor.tsx b/apps/app/components/crm/fields/field-editor.tsxindex 35ac13df..e5afe92d 100644--- a/apps/app/components/crm/fields/field-editor.tsx+++ b/apps/app/components/crm/fields/field-editor.tsx@@ -67,11 +67,11 @@ import {} from "./fields-copy";import { type FieldEntity, kindOf } from "./fields-entity";-const COVERAGE_NOUN: Record<FieldEntity, string> = {+const COVERAGE_NOUN = {COMPANY: "companies",CONTACT: "contacts",DEAL: "deals",-};+} satisfies Record<FieldEntity, string>;type FieldRecord = RouterOutputs["fields"]["list"][number];@@ -85,7 +85,7 @@ type Draft = {showOnTable: boolean;};-const TYPE_HINTS: Record<string, string> = {+const TYPE_HINTS = {TEXT: "Text — a short line",LONG_TEXT: "Long text — a paragraph",NUMBER: "Number",@@ -96,7 +96,7 @@ const TYPE_HINTS: Record<string, string> = {EMAIL: "Email",PHONE: "Phone",USER: "User — someone in the workspace",-};+} satisfies Record<(typeof FIELD_TYPES)[number], string>;function optionId(option: { id?: string }, index: number): string {return option.id ?? `draft-${index}`;diff --git a/apps/app/components/crm/fields/fields-copy.ts b/apps/app/components/crm/fields/fields-copy.tsindex 4c809845..9c2648f6 100644--- a/apps/app/components/crm/fields/fields-copy.ts+++ b/apps/app/components/crm/fields/fields-copy.ts@@ -3,11 +3,11 @@ import type { FieldEntity } from "./fields-entity";export const SHEET_TITLE = "Fields";-const SUBTITLE: Record<RecordKind, string> = {+const SUBTITLE = {company: "This shapes every company in your CRM.",contact: "This shapes every contact in your CRM.",deal: "This shapes every deal in your CRM.",-};+} satisfies Record<RecordKind, string>;export function subtitleFor(kind: RecordKind): string {return SUBTITLE[kind];@@ -57,17 +57,17 @@ export const SAVE = "Save changes";export const ARCHIVE = "Archive";export const FILL_REST = "Fill the rest";-const SHEET_PLACEMENT: Record<FieldEntity, string> = {+const SHEET_PLACEMENT = {COMPANY: "Show on the company sheet",CONTACT: "Show on the contact sheet",DEAL: "Show on the deal sheet",-};+} satisfies Record<FieldEntity, string>;-const TABLE_PLACEMENT: Record<FieldEntity, string> = {+const TABLE_PLACEMENT = {COMPANY: "Offer as a column on the Companies table",CONTACT: "Offer as a column on the Contacts table",DEAL: "Offer as a column on the Deals table",-};+} satisfies Record<FieldEntity, string>;export function sheetPlacement(entity: FieldEntity): string {return SHEET_PLACEMENT[entity];diff --git a/apps/app/components/crm/fields/fields-entity.ts b/apps/app/components/crm/fields/fields-entity.tsindex a7e1c88a..290de09f 100644--- a/apps/app/components/crm/fields/fields-entity.ts+++ b/apps/app/components/crm/fields/fields-entity.ts@@ -2,17 +2,17 @@ import type { RecordKind } from "@/components/crm/record-sheet/record-stack";export type FieldEntity = "COMPANY" | "CONTACT" | "DEAL";-const TO_ENTITY: Record<RecordKind, FieldEntity> = {+const TO_ENTITY = {company: "COMPANY",contact: "CONTACT",deal: "DEAL",-};+} satisfies Record<RecordKind, FieldEntity>;-const TO_KIND: Record<FieldEntity, RecordKind> = {+const TO_KIND = {COMPANY: "company",CONTACT: "contact",DEAL: "deal",-};+} satisfies Record<FieldEntity, RecordKind>;export function entityOf(kind: RecordKind): FieldEntity {return TO_ENTITY[kind];diff --git a/apps/app/components/crm/fields/standard-fields.ts b/apps/app/components/crm/fields/standard-fields.tsindex 55774f5c..5e7cc071 100644--- a/apps/app/components/crm/fields/standard-fields.ts+++ b/apps/app/components/crm/fields/standard-fields.ts@@ -1,6 +1,6 @@import type { FieldEntity } from "./fields-entity";-export const STANDARD_FIELDS: Record<FieldEntity, readonly string[]> = {+export const STANDARD_FIELDS = {COMPANY: ["Name","Domain",@@ -31,4 +31,4 @@ export const STANDARD_FIELDS: Record<FieldEntity, readonly string[]> = {"Owner","Stage",],-};+} satisfies Record<FieldEntity, readonly string[]>;diff --git a/apps/app/components/crm/record-sheet/record-actions.tsx b/apps/app/components/crm/record-sheet/record-actions.tsxindex 1e13c630..d826c36f 100644--- a/apps/app/components/crm/record-sheet/record-actions.tsx+++ b/apps/app/components/crm/record-sheet/record-actions.tsx@@ -31,11 +31,11 @@ import {useRecordStack,} from "./record-stack";-const NOUN: Record<RecordKind, string> = {+const NOUN = {company: "company",contact: "contact",deal: "deal",-};+} satisfies Record<RecordKind, string>;function useDeleteRecord(record: RecordRef) {const trpc = useTRPC();diff --git a/apps/app/components/crm/record-sheet/record-stack.ts b/apps/app/components/crm/record-sheet/record-stack.tsindex e4d8bec2..31dcaff3 100644--- a/apps/app/components/crm/record-sheet/record-stack.ts+++ b/apps/app/components/crm/record-sheet/record-stack.ts@@ -22,10 +22,10 @@ const RECORD_FORMS = ["contact", "deal"] as const;export type RecordForm = (typeof RECORD_FORMS)[number];-const FORM_TAB: Record<RecordForm, string> = {+const FORM_TAB = {contact: "contacts",deal: "deals",-};+} satisfies Record<RecordForm, string>;const params = {record: parseAsArrayOf(parseAsString, ",").withDefault([]),diff --git a/apps/app/components/crm/timeline/activity-composer.tsx b/apps/app/components/crm/timeline/activity-composer.tsxindex 6ef2d612..6cebb991 100644--- a/apps/app/components/crm/timeline/activity-composer.tsx+++ b/apps/app/components/crm/timeline/activity-composer.tsx@@ -34,13 +34,13 @@ const dueFormat = new Intl.DateTimeFormat("en-US", {day: "numeric",});-const PLACEHOLDER: Record<ComposableType, string> = {+const PLACEHOLDER = {NOTE: "Log a note, call, email, meeting or task…",CALL: "What came out of the call?",EMAIL: "What was said?",MEETING: "What came out of the meeting?",TASK: "What needs doing?",-};+} satisfies Record<ComposableType, string>;export function ActivityComposer({ anchor }: { anchor: TimelineAnchor }) {const trpc = useTRPC();diff --git a/apps/app/components/crm/timeline/timeline-entry.tsx b/apps/app/components/crm/timeline/timeline-entry.tsxindex 8a86f98a..0a9d730a 100644--- a/apps/app/components/crm/timeline/timeline-entry.tsx+++ b/apps/app/components/crm/timeline/timeline-entry.tsx@@ -1,10 +1,12 @@"use client";+import { DealStage } from "@crm/db/enums";import { Checkbox } from "@crm/ui/components/checkbox";import { StatusIndicator } from "@crm/ui/components/status-indicator";import { cn } from "@crm/ui/lib/utils";import { useMutation } from "@tanstack/react-query";import { toast } from "sonner";+import { z } from "zod";import { RecordLink } from "@/components/crm/record-sheet/record-link";import { LocalDateTime, LocalRelativeTime } from "@/components/local-date-time";import { activityLabel } from "@/lib/activity-presentation";@@ -25,11 +27,10 @@ const TIME_OPTIONS: Intl.DateTimeFormatOptions = {minute: "2-digit",};-function stageChange(meta: Record<string, unknown> | null) {- const from = typeof meta?.from === "string" ? meta.from : null;- const to = typeof meta?.to === "string" ? meta.to : null;- return from && to ? { from, to } : null;-}+const stageChange = z+ .object({ from: z.enum(DealStage), to: z.enum(DealStage) })+ .nullable()+ .catch(null);function anchorId(anchor: TimelineAnchor): string {if ("companyId" in anchor) return anchor.companyId;@@ -62,7 +63,8 @@ export function TimelineEntry({entry.dueAt !== null &&new Date(entry.dueAt) < new Date();- const change = entry.type === "STAGE_CHANGE" ? stageChange(entry.meta) : null;+ const change =+ entry.type === "STAGE_CHANGE" ? stageChange.parse(entry.meta) : null;const when = entry.occurredAt ?? entry.createdAt;const synced = entry.meta?.synced === true;@@ -73,7 +75,7 @@ export function TimelineEntry({: entry.createdBy.name;const headline = change- ? `${dealStageLabel(change.from as never)} → ${dealStageLabel(change.to as never)}`+ ? `${dealStageLabel(change.from)} → ${dealStageLabel(change.to)}`: entry.subject;const here = anchorId(anchor);diff --git a/apps/app/components/crm/timeline/timeline.tsx b/apps/app/components/crm/timeline/timeline.tsxindex d2fde05f..e84d12c8 100644--- a/apps/app/components/crm/timeline/timeline.tsx+++ b/apps/app/components/crm/timeline/timeline.tsx@@ -31,19 +31,16 @@ export type TimelineAnchor =| { contactId: string }| { dealId: string };-const TAB_LABELS: Record<TimelineTab, string> = {+const TAB_LABELS = {all: "All",notes: "Notes",email: "Email",meetings: "Meetings",upcoming: "Upcoming",done: "Done",-};+} satisfies Record<TimelineTab, string>;-const EMPTY_STATES: Record<- TimelineTab,- { title: string; description: string }-> = {+const EMPTY_STATES = {all: {title: "Nothing has happened yet",description:@@ -73,16 +70,16 @@ const EMPTY_STATES: Record<title: "Nothing finished yet",description: "Tasks move here once you tick them off.",},-};+} satisfies Record<TimelineTab, { title: string; description: string }>;-const EMPTY_ICONS: Record<TimelineTab, CarbonIcon> = {+const EMPTY_ICONS = {all: Time,notes: Chat,email: Email,meetings: Events,upcoming: Task,done: Checkmark,-};+} satisfies Record<TimelineTab, CarbonIcon>;const dayFormat = new Intl.DateTimeFormat("en-US", {weekday: "short",diff --git a/apps/app/components/inline-script.tsx b/apps/app/components/inline-script.tsxindex ea27ff04..3884979b 100644--- a/apps/app/components/inline-script.tsx+++ b/apps/app/components/inline-script.tsx@@ -1,7 +1,7 @@export function InlineScript({ html }: { html: string }) {return (<script- type={typeof window === "undefined" ? "text/javascript" : "text/plain"}+ type={globalThis.window === undefined ? "text/javascript" : "text/plain"}suppressHydrationWarning>{html}diff --git a/apps/app/components/landing/analytics.tsx b/apps/app/components/landing/analytics.tsxindex 67ff6a67..f0a2205c 100644--- a/apps/app/components/landing/analytics.tsx+++ b/apps/app/components/landing/analytics.tsx@@ -32,7 +32,7 @@ export function captureLanding(event: "setup_prompt_copied" | "github_star_clicked",location: CtaLocation,): void {- if (typeof window === "undefined") return;+ if (globalThis.window === undefined) return;if (!analyticsAllowed(window.location.hostname)) return;import("posthog-js")diff --git a/apps/app/lib/activity-presentation.ts b/apps/app/lib/activity-presentation.tsindex 47d04445..3f199188 100644--- a/apps/app/lib/activity-presentation.ts+++ b/apps/app/lib/activity-presentation.ts@@ -8,16 +8,20 @@ import Task from "@carbon/icons-react/es/Task";import type { ActivityType } from "@crm/db/enums";import type { CarbonIcon } from "@crm/ui/components/icon";-const PRESENTATION: Record<ActivityType, { icon: CarbonIcon; label: string }> =- {- NOTE: { icon: Chat, label: "Note" },- CALL: { icon: Phone, label: "Call" },- EMAIL: { icon: Email, label: "Email" },- MEETING: { icon: Events, label: "Meeting" },- TASK: { icon: Task, label: "Task" },- STAGE_CHANGE: { icon: ArrowRight, label: "Stage change" },- ENRICHMENT: { icon: MagicWand, label: "Enrichment" },- };+type ActivityPresentation = Record<+ ActivityType,+ { icon: CarbonIcon; label: string }+>;++const PRESENTATION: ActivityPresentation = {+ NOTE: { icon: Chat, label: "Note" },+ CALL: { icon: Phone, label: "Call" },+ EMAIL: { icon: Email, label: "Email" },+ MEETING: { icon: Events, label: "Meeting" },+ TASK: { icon: Task, label: "Task" },+ STAGE_CHANGE: { icon: ArrowRight, label: "Stage change" },+ ENRICHMENT: { icon: MagicWand, label: "Enrichment" },+};export function activityLabel(type: ActivityType): string {return PRESENTATION[type].label;diff --git a/apps/app/lib/agent-bridge.ts b/apps/app/lib/agent-bridge.tsindex 699cb5b4..9b57449b 100644--- a/apps/app/lib/agent-bridge.ts+++ b/apps/app/lib/agent-bridge.ts@@ -9,6 +9,20 @@ export function bridgeConfigured(): boolean {return Boolean(process.env.AGENT_BRIDGE_SECRET);}+type BridgeClaims = {+ iss: string;+ aud: string;+ sub: string;+ email: string;+ name: string;+ contactId?: string;+ companyId?: string;+ dealId?: string;+ iat: number;+ nbf: number;+ exp: number;+};+export async function mintBridgeToken(user: {id: string;@@ -23,44 +37,43 @@ export async function mintBridgeToken(const now = Math.floor(Date.now() / 1000);const header = { alg: "HS256", typ: "JWT" };- const payload = {+ const payload: BridgeClaims = {iss: ISSUER,aud: AUDIENCE,sub: user.id,email: user.email,name: user.name,- ...(record.contactId ? { contactId: record.contactId } : {}),- ...(record.companyId ? { companyId: record.companyId } : {}),- ...(record.dealId ? { dealId: record.dealId } : {}),iat: now,nbf: now - 5,exp: now + TTL_SECONDS,};- const signingInput = `${base64url(JSON.stringify(header))}.${base64url(- JSON.stringify(payload),+ if (record.contactId) payload.contactId = record.contactId;+ if (record.companyId) payload.companyId = record.companyId;+ if (record.dealId) payload.dealId = record.dealId;++ const signingInput = `${base64url(encode(JSON.stringify(header)))}.${base64url(+ encode(JSON.stringify(payload)),)}`;const key = await crypto.subtle.importKey("raw",- new TextEncoder().encode(secret),+ encode(secret),{ name: "HMAC", hash: "SHA-256" },false,["sign"],);- const signature = await crypto.subtle.sign(- "HMAC",- key,- new TextEncoder().encode(signingInput),- );+ const signature = await crypto.subtle.sign("HMAC", key, encode(signingInput));return `${signingInput}.${base64url(new Uint8Array(signature))}`;}-function base64url(input: string | Uint8Array): string {- const bytes =- typeof input === "string" ? new TextEncoder().encode(input) : input;+function encode(input: string) {+ return new TextEncoder().encode(input);+}++function base64url(bytes: Uint8Array): string {let binary = "";for (const byte of bytes) binary += String.fromCharCode(byte);return btoa(binary)diff --git a/apps/app/lib/agent-builder-state.ts b/apps/app/lib/agent-builder-state.tsindex 99a0c388..2fddb79f 100644--- a/apps/app/lib/agent-builder-state.ts+++ b/apps/app/lib/agent-builder-state.ts@@ -1,3 +1,9 @@+import {+ type EveStreamEvent,+ eveRequestedActions,+ eveSettledAction,+} from "@crm/validation/eve-stream";+export type BuilderArtifactState = {id: string;versionId: string | null;@@ -59,20 +65,18 @@ export function builderSessionStreamKey(}export function agentBuilderCallIsActive(- events: readonly { type: string; data?: unknown }[],+ events: readonly EveStreamEvent[],): boolean {const activeCallIds = new Set<string>();for (const event of events) {- const data = recordOf(event.data);if (event.type === "actions.requested") {- for (const value of arrayOf(data.actions)) {- const action = recordOf(value);+ for (const action of eveRequestedActions.parse(event.data).actions) {if (action.kind === "subagent-call" &&(action.name === "agent_builder" ||action.subagentName === "agent_builder") &&- typeof action.callId === "string"+ action.callId !== null) {activeCallIds.add(action.callId);}@@ -80,11 +84,12 @@ export function agentBuilderCallIsActive(}if (event.type === "action.result" || event.type === "subagent.completed") {- const result = recordOf(data.result);- const action = recordOf(data.action);- const callId = [data.callId, result.callId, action.callId].find(- (value): value is string => typeof value === "string",- );+ const settled = eveSettledAction.parse(event.data);+ const callId = [+ settled.callId,+ settled.result.callId,+ settled.action.callId,+ ].find((value) => value !== null);if (callId) activeCallIds.delete(callId);}@@ -151,13 +156,3 @@ export function latestCompletedArtifactVersionId()?.versionId ?? null);}--function recordOf(value: unknown): Record<string, unknown> {- return value && typeof value === "object" && !Array.isArray(value)- ? (value as Record<string, unknown>)- : {};-}--function arrayOf(value: unknown): unknown[] {- return Array.isArray(value) ? value : [];-}diff --git a/apps/app/lib/agent-record.ts b/apps/app/lib/agent-record.tsindex f3d17a41..c4e216cc 100644--- a/apps/app/lib/agent-record.ts+++ b/apps/app/lib/agent-record.ts@@ -13,7 +13,17 @@ type RecordCopy = {suggestions: string[];};-const COPY: Record<AgentRecordKind, RecordCopy> = {+type RecordCopyByKind = Record<AgentRecordKind, RecordCopy>;++export type AgentRecordHeader = Record<string, string>;++export type AgentRecordFilter = {+ contactId?: string;+ companyId?: string;+ dealId?: string;+};++const COPY: RecordCopyByKind = {contact: {header: "x-crm-contact",field: "contactId",@@ -59,15 +69,11 @@ export function recordCopy(kind: AgentRecordKind): RecordCopy {return COPY[kind];}-export function recordHeader(record: AgentRecord): Record<string, string> {+export function recordHeader(record: AgentRecord): AgentRecordHeader {return { [COPY[record.kind].header]: record.id };}-export function recordFilter(record: AgentRecord): {- contactId?: string;- companyId?: string;- dealId?: string;-} {+export function recordFilter(record: AgentRecord): AgentRecordFilter {return { [COPY[record.kind].field]: record.id };}diff --git a/apps/app/lib/agent-results.ts b/apps/app/lib/agent-results.tsindex b70410b4..279cf6f5 100644--- a/apps/app/lib/agent-results.ts+++ b/apps/app/lib/agent-results.ts@@ -1,3 +1,4 @@+import type { EveToolOutput } from "@crm/validation/eve-tool";import type { TranscriptItem } from "./agent-transcript";export type AnchoredResult<T> = { itemId: string; value: T };@@ -10,7 +11,7 @@ export function anchorResults<T>({}: {items: readonly TranscriptItem[];tool: string;- validate: (output: unknown) => T | null;+ validate: (output: EveToolOutput) => T | null;group?: (results: readonly AnchoredResult<T>[],) => readonly AnchoredResult<T>[];diff --git a/apps/app/lib/agent-run-failure.ts b/apps/app/lib/agent-run-failure.tsindex 390732b7..23c65ae9 100644--- a/apps/app/lib/agent-run-failure.ts+++ b/apps/app/lib/agent-run-failure.ts@@ -1,4 +1,6 @@-const REASONS: Record<string, string> = {+type RunFailureReasons = Record<string, string>;++const REASONS: RunFailureReasons = {ACTION_NOT_PERFORMED:"The agent finished without doing what it was built to do. Open the run to see which step it skipped.",NO_EXECUTOR:diff --git a/apps/app/lib/agent-tool-display.ts b/apps/app/lib/agent-tool-display.tsindex 90016b5b..9c83394c 100644--- a/apps/app/lib/agent-tool-display.ts+++ b/apps/app/lib/agent-tool-display.ts@@ -1,4 +1,12 @@-const ARTIFACT_NAMES: Record<string, string> = {+import {+ type EveToolFields,+ type EveToolInput,+ eveToolText,+} from "@crm/validation/eve-tool";++type ArtifactNames = Record<string, string>;++const ARTIFACT_NAMES: ArtifactNames = {"agent/instructions.md": "instructions","agent/manifest.json": "the manifest","agent/README.md": "the readme",@@ -6,28 +14,29 @@ const ARTIFACT_NAMES: Record<string, string> = {type LabelInput = {tool: string;- input: Record<string, unknown> | null;+ input: EveToolInput;label: string;pending: boolean;};-const INPUT_LABELS: Record<- string,- (input: Record<string, unknown>, pending: boolean) => string | null-> = {+type ToolInputLabel = (input: EveToolFields, pending: boolean) => string | null;++type ToolInputLabels = Record<string, ToolInputLabel>;++const INPUT_LABELS: ToolInputLabels = {write_agent_file: (input, pending) => {- const path = typeof input.path === "string" ? input.path : null;+ const path = eveToolText.parse(input.path);if (!path) return null;const name = ARTIFACT_NAMES[path] ?? path;return pending ? `Writing ${name}` : `Wrote ${name}`;},save_agent_draft: (input, pending) => {- const name = typeof input.name === "string" ? input.name.trim() : "";+ const name = eveToolText.parse(input.name).trim();const verb = pending ? "Saving draft" : "Saved draft";return name ? `${verb} · ${name}` : verb;},set_chat_title: (input, pending) => {- const title = typeof input.title === "string" ? input.title.trim() : "";+ const title = eveToolText.parse(input.title).trim();const verb = pending ? "Naming this chat" : "Named this chat";return title ? `${verb} · ${title}` : verb;},diff --git a/apps/app/lib/agent-transcript.ts b/apps/app/lib/agent-transcript.tsindex 8c8ef744..2898c35b 100644--- a/apps/app/lib/agent-transcript.ts+++ b/apps/app/lib/agent-transcript.ts@@ -1,3 +1,16 @@+import {+ type EveStreamEvent,+ eveTurnFailure,+ eveTurnReference,+} from "@crm/validation/eve-stream";+import {+ type EveToolInput,+ type EveToolOutcome,+ type EveToolOutput,+ eveToolInput,+ eveToolOutcome,+ eveToolOutput,+} from "@crm/validation/eve-tool";import type { MessageStreamEvent } from "eve/client";import {defaultMessageReducer,@@ -5,6 +18,7 @@ import {type EveMessageInputRequest,type EveMessagePart,} from "eve/react";+import { z } from "zod";export type TranscriptItem =| { kind: "said"; id: string; mine: boolean; text: string }@@ -18,8 +32,8 @@ export type TranscriptItem =kind: "did";id: string;label: string;- input: Record<string, unknown> | null;- output: unknown;+ input: EveToolInput;+ output: EveToolOutput;tone: Tone;pending: boolean;sources: Source[];@@ -40,12 +54,9 @@ export type AgentTurnFailure = {kind: "rate-limit" | "restricted" | "credits" | "unknown";};-type AgentStreamEvent = {- type: string;- data?: unknown;-};+type ToolVerbs = Record<string, string>;-const VERBS: Record<string, string> = {+const VERBS: ToolVerbs = {read_crm_history: "Read our emails and meetings with them",read_company_history: "Read everything we have on the company",read_deal_history: "Read the deal and where it has been",@@ -140,8 +151,8 @@ export function conversationTimeline<const turnTimes = new Map<string, number>();for (const event of events) {- const turnId = stringOf(- recordOf("data" in event ? event.data : undefined).turnId,+ const { turnId } = eveTurnReference.parse(+ "data" in event ? event.data : undefined,);if (!turnId || turnTimes.has(turnId)) continue;turnTimes.set(turnId, timestampOf(event.meta.at));@@ -161,7 +172,7 @@ export function conversationTimeline<id: `assistant:${message.id}`,message,at:- turnTimes.get(stringOf(recordOf(message.metadata).turnId) ?? "") ??+ turnTimes.get(message.metadata?.turnId ?? "") ??Number.POSITIVE_INFINITY,index,});@@ -268,18 +279,13 @@ function partId(part: EveMessagePart,index: number,): string {- const callId =- "toolCallId" in part && typeof part.toolCallId === "string"- ? part.toolCallId- : null;+ const callId = "toolCallId" in part ? part.toolCallId : null;return callId ? `${messageId}:${callId}` : `${messageId}:${index}`;}export function toolName(part: EveMessagePart): string {- if (part.type === "dynamic-tool" && "toolName" in part) {- return String(part.toolName);- }+ if (part.type === "dynamic-tool") return part.toolName;return part.type.replace(/^tool-/, "");}@@ -288,15 +294,15 @@ export const TOOL_VERBS = VERBS;export function describe(part: EveMessagePart): string {const tool = toolName(part);const verb = VERBS[tool] ?? humanise(tool);- const reason = output(part)?.reason;+ const reason = outcome(part)?.reason ?? null;- return typeof reason === "string" ? `${verb} — ${reason}` : verb;+ return reason === null ? verb : `${verb} — ${reason}`;}export function outcomeTone(part: EveMessagePart): Tone {if ("state" in part && part.state === "output-error") return "warning";- const result = output(part);+ const result = outcome(part);if (!result) return "neutral";if (result.applied === true || result.written === true) return "success";@@ -306,15 +312,12 @@ export function outcomeTone(part: EveMessagePart): Tone {}export function sourcesOf(part: EveMessagePart): Source[] {- const result = output(part);+ const result = outcome(part);if (!result) return [];const urls = new Set<string>();- for (const key of ["sourceUrl", "profileUrl", "url"]) {- const value = result[key];- if (typeof value === "string" && /^https?:\/\//.test(value)) {- urls.add(value);- }+ for (const link of [result.sourceUrl, result.profileUrl, result.url]) {+ if (link !== null) urls.add(link);}return [...urls].map((url) => {@@ -345,7 +348,7 @@ export function pendingQuestion(messages: readonly EveMessage[]) {}export function latestTurnFailure(- events: readonly AgentStreamEvent[],+ events: readonly EveStreamEvent[],): AgentTurnFailure | null {for (let index = events.length - 1; index >= 0; index -= 1) {const event = events[index];@@ -357,12 +360,11 @@ export function latestTurnFailure(continue;}- const data = recordOf(event.data);- const message = typeof data.message === "string" ? data.message : "";- const code = typeof data.code === "string" ? data.code : "AGENT_FAILED";+ const failure = eveTurnFailure.parse(event.data);+ const message = failure.message ?? "";return {- code,+ code: failure.code ?? "AGENT_FAILED",kind: /free tier users do not have access|RestrictedModelsError/i.test(message,)@@ -380,32 +382,25 @@ export function latestTurnFailure(return null;}-function output(part: EveMessagePart): Record<string, unknown> | null {- return "output" in part && part.output && typeof part.output === "object"- ? (part.output as Record<string, unknown>)- : null;+function payloadOf(part: EveMessagePart) {+ return "output" in part ? part.output : undefined;}-function input(part: EveMessagePart): Record<string, unknown> | null {- return "input" in part && part.input && typeof part.input === "object"- ? (part.input as Record<string, unknown>)- : null;+function output(part: EveMessagePart): EveToolOutput {+ return eveToolOutput.parse(payloadOf(part));}-function errorTextOf(part: EveMessagePart): string | null {- if (!("errorText" in part)) return null;- const text = part.errorText;- return typeof text === "string" && text.trim() ? text : null;+function outcome(part: EveMessagePart): EveToolOutcome {+ return eveToolOutcome.parse(payloadOf(part));}-function recordOf(value: unknown): Record<string, unknown> {- return value && typeof value === "object" && !Array.isArray(value)- ? (value as Record<string, unknown>)- : {};+function input(part: EveMessagePart): EveToolInput {+ return eveToolInput.parse("input" in part ? part.input : undefined);}-function stringOf(value: unknown): string | null {- return typeof value === "string" && value ? value : null;+function errorTextOf(part: EveMessagePart): string | null {+ const text = "errorText" in part ? part.errorText : undefined;+ return text?.trim() ? text : null;}function timestampOf(value: string): number {@@ -459,35 +454,57 @@ export type DealListResult = {hasMore: boolean;};-export function dealListResultOf(value: unknown): DealListResult | null {- const result = recordOf(value);- const asOf = stringOf(result.asOf);- const criteria = recordOf(result.criteria);- const status = stringOf(criteria.status);- const inactiveForDays = nullableNumberOf(criteria.inactiveForDays);- const companyId = nullableStringOf(criteria.companyId);- const ownerId = nullableStringOf(criteria.ownerId);- const rows = Array.isArray(result.deals) ? result.deals : null;-- if (- !asOf ||- !status ||- inactiveForDays === undefined ||- companyId === undefined ||- ownerId === undefined ||- !rows- )- return null;-- const deals = rows.map(dealListItemOf);- if (deals.some((deal) => deal === null)) return null;-- return {- asOf,- criteria: { status, inactiveForDays, companyId, ownerId },- deals: deals as DealListItem[],- hasMore: result.hasMore === true,- };+const requiredText = z.string().min(1);++const finiteNumber = z.number().refine((value) => Number.isFinite(value));++const optionalText = z.string().min(1).nullable().catch(null);++const dealListItem = z.object({+ id: requiredText,+ name: requiredText,+ stage: requiredText,+ amount: finiteNumber.nullable(),+ currency: requiredText,+ company: z.object({+ id: requiredText,+ name: requiredText,+ domain: optionalText,+ iconUrl: optionalText,+ iconDarkUrl: optionalText,+ iconTone: optionalText,+ logoUrl: optionalText,+ }),+ owner: z+ .object({+ id: requiredText,+ name: requiredText,+ email: requiredText,+ image: optionalText,+ })+ .nullable(),+ daysSinceLastActivity: finiteNumber,+ neverActive: z.boolean().catch(false),+ expectedCloseDate: requiredText.nullable(),+});++const dealListResult = z+ .object({+ asOf: requiredText,+ criteria: z.object({+ status: requiredText,+ inactiveForDays: finiteNumber.nullable(),+ companyId: requiredText.nullable(),+ ownerId: requiredText.nullable(),+ }),+ deals: z.array(dealListItem),+ hasMore: z.boolean().catch(false),+ })+ .nullable()+ .catch(null);++export function dealListResultOf(value: EveToolOutput): DealListResult | null {+ return dealListResult.parse(value);}export function groupDealListPages(@@ -567,11 +584,13 @@ function stripMarkdownTables(markdown: string): string {.trim();}-export function splitMarkdownTable(markdown: string): {+export type MarkdownTableSplit = {after: string;before: string;found: boolean;-} {+};++export function splitMarkdownTable(markdown: string): MarkdownTableSplit {const lines = markdown.split("\n");for (let index = 0; index < lines.length - 1; index += 1) {@@ -596,83 +615,6 @@ export function splitMarkdownTable(markdown: string): {return { before: "", after: normaliseMarkdown(markdown), found: false };}-function dealListItemOf(value: unknown): DealListItem | null {- const deal = recordOf(value);- const company = recordOf(deal.company);- const owner = deal.owner === null ? null : recordOf(deal.owner);- const id = stringOf(deal.id);- const name = stringOf(deal.name);- const stage = stringOf(deal.stage);- const currency = stringOf(deal.currency);- const companyId = stringOf(company.id);- const companyName = stringOf(company.name);- const daysSinceLastActivity = numberOf(deal.daysSinceLastActivity);- const amount = nullableNumberOf(deal.amount);- const expectedCloseDate = nullableStringOf(deal.expectedCloseDate);-- if (- !id ||- !name ||- !stage ||- !currency ||- !companyId ||- !companyName ||- daysSinceLastActivity === null ||- amount === undefined ||- expectedCloseDate === undefined- ) {- return null;- }-- const parsedOwner = owner- ? {- id: stringOf(owner.id),- name: stringOf(owner.name),- email: stringOf(owner.email),- image: nullableStringOf(owner.image) ?? null,- }- : null;- if (- parsedOwner &&- (!parsedOwner.id || !parsedOwner.name || !parsedOwner.email)- ) {- return null;- }-- return {- id,- name,- stage,- amount,- currency,- company: {- id: companyId,- name: companyName,- domain: nullableStringOf(company.domain) ?? null,- iconUrl: nullableStringOf(company.iconUrl) ?? null,- iconDarkUrl: nullableStringOf(company.iconDarkUrl) ?? null,- iconTone: nullableStringOf(company.iconTone) ?? null,- logoUrl: nullableStringOf(company.logoUrl) ?? null,- },- owner: parsedOwner as DealListItem["owner"],- daysSinceLastActivity,- neverActive: deal.neverActive === true,- expectedCloseDate,- };-}--function numberOf(value: unknown): number | null {- return typeof value === "number" && Number.isFinite(value) ? value : null;-}--function nullableNumberOf(value: unknown): number | null | undefined {- return value === null ? null : (numberOf(value) ?? undefined);-}--function nullableStringOf(value: unknown): string | null | undefined {- return value === null ? null : (stringOf(value) ?? undefined);-}-function isMarkdownTableSeparator(line: string): boolean {return /^\s*\|?\s*:?-{3,}:?(?:\s*\|\s*:?-{3,}:?)+\s*\|?\s*$/.test(line);}@@ -688,6 +630,11 @@ function normaliseMarkdown(markdown: string): string {export const NEW_THREAD = "new";+export type ResolvedThread<T> = {+ openId: string | null;+ current: T | null;+};+export function resolveThread<T extends { id: string }>({conversations,fromUrl,@@ -696,7 +643,7 @@ export function resolveThread<T extends { id: string }>({conversations: readonly T[];fromUrl: string | null;landedOn: string | null;-}): { openId: string | null; current: T | null } {+}): ResolvedThread<T> {const openId = fromUrl ?? landedOn;if (!openId || openId === NEW_THREAD) return { openId, current: null };diff --git a/apps/app/lib/deal-stage.ts b/apps/app/lib/deal-stage.tsindex 3da33ccd..758f08f2 100644--- a/apps/app/lib/deal-stage.ts+++ b/apps/app/lib/deal-stage.ts@@ -11,7 +11,12 @@ const ORDER = [DealStage.UNQUALIFIED_TO_BUY,] as const;-const PRESENTATION: Record<DealStage, { label: string; tone: StatusTone }> = {+type DealStagePresentation = Record<+ DealStage,+ { label: string; tone: StatusTone }+>;++const PRESENTATION: DealStagePresentation = {DEMO_BOOKED: { label: "Demo booked", tone: "neutral" },QUALIFIED_TO_BUY: { label: "Qualified to buy", tone: "info" },DECISION_MAKER_BOUGHT_IN: { label: "Decision maker in", tone: "info" },diff --git a/apps/app/lib/enrichment-status.ts b/apps/app/lib/enrichment-status.tsindex 4264ee42..7024158d 100644--- a/apps/app/lib/enrichment-status.ts+++ b/apps/app/lib/enrichment-status.ts@@ -1,10 +1,12 @@import type { EnrichmentStatus } from "@crm/db/enums";import type { StatusTone } from "@crm/ui/components/status-indicator";-const PRESENTATION: Record<+type EnrichmentPresentation = Record<EnrichmentStatus,{ label: string; tone: StatusTone; busy?: boolean }-> = {+>;++const PRESENTATION: EnrichmentPresentation = {PENDING: { label: "Not researched", tone: "neutral" },RUNNING: { label: "Researching", tone: "info", busy: true },COMPLETE: { label: "Enriched", tone: "success" },diff --git a/apps/app/lib/onboarding.ts b/apps/app/lib/onboarding.tsindex fd4c184c..ea827378 100644--- a/apps/app/lib/onboarding.ts+++ b/apps/app/lib/onboarding.ts@@ -1,4 +1,5 @@import type { NextRequest } from "next/server";+import { z } from "zod";import { API_URL } from "@/lib/env";export const ONBOARDING_PATH = "/onboarding";@@ -9,10 +10,23 @@ const GATE_TIMEOUT_MS = 2_000;export type Gate = "settled" | "required" | "unknown";-async function read<T>(- request: NextRequest,- procedure: string,-): Promise<T | null> {+const procedureResult = z+ .object({ result: z.object({ data: z.json() }).catch({ data: null }) })+ .catch({ result: { data: null } });++const workspaceAnswer = z+ .object({+ onboarded: z.boolean().nullable().catch(null),+ canRename: z.boolean().nullable().catch(null),+ slug: z.string().min(1).nullable().catch(null),+ })+ .catch({ onboarded: null, canRename: null, slug: null });++const researchKeyAnswer = z+ .object({ configured: z.boolean().nullable().catch(null) })+ .catch({ configured: null });++async function read(request: NextRequest, procedure: string) {const cookie = request.headers.get("cookie");if (!cookie) return null;@@ -26,9 +40,7 @@ async function read<T>(if (!response.ok) return null;- const body = (await response.json()) as { result?: { data?: T } };-- return body.result?.data ?? null;+ return procedureResult.parse(await response.json()).result.data;} catch {return null;}@@ -39,31 +51,29 @@ export type WorkspaceGate = { gate: Gate; slug: string | null };export async function readWorkspaceGate(request: NextRequest,): Promise<WorkspaceGate> {- const workspace = await read<{- onboarded?: boolean;- canRename?: boolean;- slug?: string;- }>(request, "workspace.get");+ const workspace = workspaceAnswer.parse(await read(request, "workspace.get"));- const slug = workspace?.slug ? workspace.slug : null;+ const slug = workspace.slug;- if (typeof workspace?.onboarded !== "boolean") {+ if (workspace.onboarded === null) {return { gate: "unknown", slug };}return {- gate: workspace.onboarded || !workspace.canRename ? "settled" : "required",+ gate:+ workspace.onboarded || workspace.canRename !== true+ ? "settled"+ : "required",slug,};}export async function readResearchGate(request: NextRequest): Promise<Gate> {- const key = await read<{ configured?: boolean }>(- request,- "settings.researchKey",+ const { configured } = researchKeyAnswer.parse(+ await read(request, "settings.researchKey"),);- if (typeof key?.configured !== "boolean") return "unknown";+ if (configured === null) return "unknown";- return key.configured ? "settled" : "required";+ return configured ? "settled" : "required";}diff --git a/apps/app/lib/social-links.ts b/apps/app/lib/social-links.tsindex cfe73aff..7315b5f7 100644--- a/apps/app/lib/social-links.ts+++ b/apps/app/lib/social-links.ts@@ -35,10 +35,13 @@ const CONTACT_LINKS: SocialLink<ContactLinks>[] = [{ key: "githubUrl", label: "GitHub", icon: LogoGithub },];-function present<T>(record: T, links: SocialLink<T>[]) {+function present<T extends Record<string, string | null>>(+ record: T,+ links: SocialLink<T>[],+) {return links.flatMap((link) => {const href = record[link.key];- return typeof href === "string" && href ? [{ ...link, href }] : [];+ return href ? [{ ...link, href }] : [];});}diff --git a/apps/app/lib/trpc/query-client.ts b/apps/app/lib/trpc/query-client.tsindex e38f3e89..6a08171d 100644--- a/apps/app/lib/trpc/query-client.ts+++ b/apps/app/lib/trpc/query-client.ts@@ -1,4 +1,13 @@import { QueryClient } from "@tanstack/react-query";+import { z } from "zod";++const queryFailure = z+ .object({+ data: z+ .object({ httpStatus: z.number().nullable().catch(null) })+ .catch({ httpStatus: null }),+ })+ .catch({ data: { httpStatus: null } });export function makeQueryClient(): QueryClient {return new QueryClient({@@ -8,15 +17,10 @@ export function makeQueryClient(): QueryClient {});}-function retryQuery(failureCount: number, error: unknown): boolean {- const data =- error && typeof error === "object" && "data" in error ? error.data : null;- const status =- data && typeof data === "object" && "httpStatus" in data- ? data.httpStatus- : null;+function retryQuery(failureCount: number, error: Error): boolean {+ const { httpStatus } = queryFailure.parse(error).data;- if (typeof status === "number" && status >= 400 && status < 500) {+ if (httpStatus !== null && httpStatus >= 400 && httpStatus < 500) {return false;}@@ -26,7 +30,7 @@ function retryQuery(failureCount: number, error: unknown): boolean {let browserQueryClient: QueryClient | undefined;export function getQueryClient(): QueryClient {- if (typeof window === "undefined") {+ if (globalThis.window === undefined) {return makeQueryClient();}browserQueryClient ??= makeQueryClient();diff --git a/apps/app/package.json b/apps/app/package.jsonindex 76ccf5d3..48519e3c 100644--- a/apps/app/package.json+++ b/apps/app/package.json@@ -34,7 +34,8 @@"posthog-js": "^1.413.2","react": "19.2.4","react-dom": "19.2.4",- "sonner": "^2.0.7"+ "sonner": "^2.0.7",+ "zod": "^4.4.3"},"devDependencies": {"@crm/typescript-config": "workspace:*",diff --git a/apps/app/test/agent-results.spec.ts b/apps/app/test/agent-results.spec.tsindex 0e781baf..f66c5ef4 100644--- a/apps/app/test/agent-results.spec.ts+++ b/apps/app/test/agent-results.spec.ts@@ -30,7 +30,7 @@ const page = (status: string, ids: string[]) => ({const did = (id: string,- output: unknown,+ output: Extract<TranscriptItem, { kind: "did" }>["output"],extra: Partial<Extract<TranscriptItem, { kind: "did" }>> = {},): TranscriptItem => ({kind: "did",diff --git a/apps/app/test/agent-transcript.spec.ts b/apps/app/test/agent-transcript.spec.tsindex 147a9365..78c28024 100644--- a/apps/app/test/agent-transcript.spec.ts+++ b/apps/app/test/agent-transcript.spec.ts@@ -1,5 +1,6 @@import { describe, expect, it } from "bun:test";import { readdirSync } from "node:fs";+import type { EveToolInput, EveToolOutput } from "@crm/validation/eve-tool";import type { EveMessage } from "eve/react";import {conversationTimeline,@@ -31,10 +32,15 @@ const message = (metadata: turnId ? { turnId } : undefined,}) as unknown as EveMessage;-const tool = (- toolName: string,- extra: Record<string, unknown> = {},-): Record<string, unknown> => ({+type ToolPartFixture = {+ errorText?: string;+ input?: EveToolInput;+ output?: EveToolOutput;+ state?: string;+ toolCallId?: string;+};++const tool = (toolName: string, extra: ToolPartFixture = {}) => ({type: "dynamic-tool",toolName,state: "output-available",@@ -163,6 +169,32 @@ describe("toTranscript", () => {});describe("deal list presentation", () => {+ const deal = {+ id: "deal-1",+ name: "Notion — expansion",+ stage: "CONTRACT_SENT",+ amount: 14_000,+ currency: "USD",+ company: {+ id: "company-1",+ name: "Notion",+ domain: "notion.so",+ iconUrl: "https://cdn.example.test/notion.png",+ iconDarkUrl: null,+ iconTone: "opaque",+ logoUrl: "https://cdn.example.test/notion.svg",+ },+ owner: {+ id: "user-1",+ name: "Priya Raman",+ email: "[email protected]",+ image: "https://cdn.example.test/priya.png",+ },+ daysSinceLastActivity: 201,+ neverActive: true,+ expectedCloseDate: "2026-09-03T19:50:06.111Z",+ };+const output = {asOf: "2026-08-06T01:14:05.025Z",criteria: {@@ -171,33 +203,7 @@ describe("deal list presentation", () => {companyId: null,ownerId: null,},- deals: [- {- id: "deal-1",- name: "Notion — expansion",- stage: "CONTRACT_SENT",- amount: 14_000,- currency: "USD",- company: {- id: "company-1",- name: "Notion",- domain: "notion.so",- iconUrl: "https://cdn.example.test/notion.png",- iconDarkUrl: null,- iconTone: "opaque",- logoUrl: "https://cdn.example.test/notion.svg",- },- owner: {- id: "user-1",- name: "Priya Raman",- email: "[email protected]",- image: "https://cdn.example.test/priya.png",- },- daysSinceLastActivity: 201,- neverActive: true,- expectedCloseDate: "2026-09-03T19:50:06.111Z",- },- ],+ deals: [deal],hasMore: false,};@@ -223,10 +229,7 @@ describe("deal list presentation", () => {const second = dealListResultOf({...output,asOf: "2026-08-06T01:15:00.000Z",- deals: [- output.deals[0],- { ...output.deals[0], id: "deal-2", name: "Linear — Comp AI" },- ],+ deals: [deal, { ...deal, id: "deal-2", name: "Linear — Comp AI" }],});if (!first || !second) throw new Error("Expected valid deal list results");diff --git a/apps/app/test/onboarding-gate.spec.ts b/apps/app/test/onboarding-gate.spec.tsindex b1bd45e1..0480f142 100644--- a/apps/app/test/onboarding-gate.spec.ts+++ b/apps/app/test/onboarding-gate.spec.ts@@ -27,14 +27,18 @@ function stub(handler: (url: string) => Promise<Response>) {handler(String(input))) as unknown as typeof fetch;}-function json(body: unknown, status = 200) {+type GateResponse =+ | { result: { data: unknown } }+ | { error: { message: string } };++function json(body: GateResponse, status = 200) {return new Response(JSON.stringify(body), {status,headers: { "content-type": "application/json" },});}-function answerWith(body: unknown, status = 200) {+function answerWith(body: GateResponse, status = 200) {stub(async () => json(body, status));}diff --git a/apps/app/test/tracking-bundle.spec.ts b/apps/app/test/tracking-bundle.spec.tsindex f8d7eabd..036af41e 100644--- a/apps/app/test/tracking-bundle.spec.ts+++ b/apps/app/test/tracking-bundle.spec.ts@@ -61,7 +61,7 @@ function inject(currentScript: script,querySelector: () => script,getElementById: () => (existing ? script : null),- createElement: () => ({}) as Record<string, unknown>,+ createElement: () => ({}),head: {appendChild: (node: { src: string }) => injected.push(node.src),},diff --git a/bun.lock b/bun.lockindex 83eb5885..f3d583b6 100644--- a/bun.lock+++ b/bun.lock@@ -101,6 +101,7 @@"react": "19.2.4","react-dom": "19.2.4","sonner": "^2.0.7",+ "zod": "^4.4.3",},"devDependencies": {"@crm/typescript-config": "workspace:*",@@ -234,6 +235,7 @@"name": "@crm/validation","version": "0.0.0","dependencies": {+ "@crm/db": "workspace:*","zod": "^4.4.3",},"devDependencies": {diff --git a/packages/auth/src/auth.ts b/packages/auth/src/auth.tsindex 9bb79565..5bdc5120 100644--- a/packages/auth/src/auth.ts+++ b/packages/auth/src/auth.ts@@ -1,5 +1,6 @@import { sso } from "@better-auth/sso";import { db } from "@crm/db";+import { schemas } from "@crm/validation";import { type BetterAuthOptions, betterAuth } from "better-auth";import { prismaAdapter } from "better-auth/adapters/prisma";import { APIError } from "better-auth/api";@@ -34,15 +35,18 @@ const slackRedirectUri = new URL().toString();if (env.google) {- socialProviders.google = {+ const google: NonNullable<typeof socialProviders.google> = {...env.google,scope: [...SYNC_SCOPES],accessType: "offline",-- ...(primaryWorkspaceDomain() ? { hd: primaryWorkspaceDomain() } : {}),};++ const hostedDomain = primaryWorkspaceDomain();+ if (hostedDomain) google.hd = hostedDomain;++ socialProviders.google = google;}if (env.microsoft) {@@ -148,30 +152,30 @@ export const auth = betterAuth({}),},);- const raw = recordValue(await response.json());- const accessToken = stringValue(raw, "access_token");- if (!response.ok || raw.ok !== true || !accessToken) {- const reason = stringValue(raw, "error") ?? "rejected";+ const grant = schemas.slack.oauthAccess.parse(+ await response.json(),+ );+ if (!response.ok || !grant.ok || !grant.access_token) {throw new APIError("BAD_REQUEST", {- message: `Slack authorization failed (${reason}).`,+ message: `Slack authorization failed (${grant.error ?? "rejected"}).`,});}- await rememberSlackInstall(raw);+ await rememberSlackInstall(grant);return {- accessToken,- tokenType: stringValue(raw, "token_type"),- scopes: (stringValue(raw, "scope") ?? "")+ accessToken: grant.access_token,+ tokenType: grant.token_type,+ scopes: (grant.scope ?? "").split(",").map((scope) => scope.trim()).filter(Boolean),- raw,+ raw: grant,};},getUserInfo: async (tokens) => {try {- const installer = objectValue(tokens.raw, "authed_user");- const userId = stringValue(installer, "id");+ const granted = schemas.slack.oauthAccess.parse(tokens.raw);+ const userId = granted.authed_user?.id;if (!tokens.accessToken || !userId) return null;const userResponse = await fetch(`https://slack.com/api/users.info?user=${encodeURIComponent(userId)}`,@@ -181,21 +185,19 @@ export const auth = betterAuth({},},);- const profile = recordValue(await userResponse.json());- if (!userResponse.ok || profile.ok !== true) return null;- const user = objectValue(profile, "user");- const details = objectValue(user, "profile");- const email = stringValue(details, "email");+ const profile = schemas.slack.userInfo.parse(+ await userResponse.json(),+ );+ if (!userResponse.ok || !profile.ok) return null;+ const details = profile.user.profile;+ const email = details.email;if (!email) return null;return {id: userId,- name:- stringValue(details, "real_name") ??- stringValue(user, "name") ??- email,+ name: details.real_name ?? profile.user.name ?? email,email,emailVerified: true,- image: stringValue(details, "image_512"),+ image: details.image_512,};} catch {return null;@@ -298,21 +300,3 @@ async function replaceSlackAccount(account: {await replaceSlackConnection(account);await queueSlackInventorySync();}--function objectValue(value: unknown, key: string): Record<string, unknown> {- return recordValue(recordValue(value)[key]);-}--function recordValue(value: unknown): Record<string, unknown> {- return value && typeof value === "object" && !Array.isArray(value)- ? (value as Record<string, unknown>)- : {};-}--function stringValue(value: unknown, key: string): string | undefined {- if (!value || typeof value !== "object" || Array.isArray(value)) {- return undefined;- }- const nested = Reflect.get(value, key);- return typeof nested === "string" && nested.length > 0 ? nested : undefined;-}diff --git a/packages/auth/src/client.ts b/packages/auth/src/client.tsindex 5e6d3e25..50159629 100644--- a/packages/auth/src/client.ts+++ b/packages/auth/src/client.ts@@ -3,7 +3,7 @@ import { genericOAuthClient } from "better-auth/client/plugins";import { createAuthClient } from "better-auth/react";export const authClient = createAuthClient({- baseURL: typeof window === "undefined" ? undefined : window.location.origin,+ baseURL: globalThis.window?.location.origin,plugins: [ssoClient(), genericOAuthClient()],});diff --git a/packages/auth/src/scopes.ts b/packages/auth/src/scopes.tsindex 45acaa37..86cb9a31 100644--- a/packages/auth/src/scopes.ts+++ b/packages/auth/src/scopes.ts@@ -19,10 +19,10 @@ export const OUTLOOK_MAIL_SCOPE = "Mail.Read";export const SYNC_SCOPES = [GMAIL_SCOPE, CALENDAR_SCOPE] as const;export const MICROSOFT_SYNC_SCOPES = [OUTLOOK_MAIL_SCOPE] as const;-export const SYNC_SCOPES_FOR: Record<MailboxProviderId, readonly string[]> = {+export const SYNC_SCOPES_FOR = {[GOOGLE_PROVIDER_ID]: SYNC_SCOPES,[MICROSOFT_PROVIDER_ID]: MICROSOFT_SYNC_SCOPES,-};+} satisfies Record<MailboxProviderId, readonly string[]>;export const REQUIRED_SCOPES = [...IDENTITY_SCOPES, ...SYNC_SCOPES] as const;diff --git a/packages/auth/src/slack-connect.ts b/packages/auth/src/slack-connect.tsindex 59a07c72..f8fa9a69 100644--- a/packages/auth/src/slack-connect.ts+++ b/packages/auth/src/slack-connect.ts@@ -1,4 +1,5 @@import { db } from "@crm/db";+import type { JsonValue } from "@crm/db/json";import {APIError,createAuthMiddleware,@@ -61,13 +62,13 @@ export const slackConnectGuard = createAuthMiddleware(async (ctx) => {}});-function startsSlackConnect(path: string, body: unknown): boolean {+function startsSlackConnect(path: string, body: JsonValue): boolean {if (!SLACK_CONNECT_START_PATHS.includes(path)) return false;const parsed = connectStartBody.safeParse(body);return parsed.success && parsed.data.providerId === SLACK_PROVIDER_ID;}-function completesSlackConnect(path: string, params: unknown): boolean {+function completesSlackConnect(path: string, params: JsonValue): boolean {if (!path.startsWith(OAUTH_CALLBACK_PATH)) return false;const parsed = callbackParams.safeParse(params);return parsed.success && parsed.data.providerId === SLACK_PROVIDER_ID;diff --git a/packages/auth/src/slack-grant.ts b/packages/auth/src/slack-grant.tsindex a166cd1d..c1e3f033 100644--- a/packages/auth/src/slack-grant.ts+++ b/packages/auth/src/slack-grant.ts@@ -1,15 +1,12 @@import { db } from "@crm/db";import { lockIdempotencyKey } from "@crm/db/idempotency";-import { schemas } from "@crm/validation";+import type { OauthAccess } from "@crm/validation";import { SLACK_PROVIDER_ID } from "./scopes";import { SLACK_CONNECTION } from "./slack-config";-export async function rememberSlackInstall(raw: unknown): Promise<void> {- const parsed = schemas.slack.installation.safeParse(raw);- if (!parsed.success) return;-- const { team, authed_user: installer } = parsed.data;- if (!installer) return;+export async function rememberSlackInstall(grant: OauthAccess): Promise<void> {+ const { team, authed_user: installer } = grant;+ if (!team || !installer) return;const install = {teamId: team.id,diff --git a/packages/auth/src/slack-scopes.ts b/packages/auth/src/slack-scopes.tsindex 5b34e6ed..898036c9 100644--- a/packages/auth/src/slack-scopes.ts+++ b/packages/auth/src/slack-scopes.ts@@ -174,10 +174,12 @@ export function summariseSlackScopes(}).filter((group) => group.total > 0);}-export function slackScopeDrift(granted: readonly string[]): {+export type SlackScopeDrift = {extra: SlackScope[];missing: SlackScope[];-} {+};++export function slackScopeDrift(granted: readonly string[]): SlackScopeDrift {const held = new Set(granted);return {extra: describeSlackScopes(diff --git a/packages/db/package.json b/packages/db/package.jsonindex 9ef44818..4935913b 100644--- a/packages/db/package.json+++ b/packages/db/package.json@@ -19,6 +19,7 @@"./fx": "./src/fx.ts","./images": "./src/images.ts","./idempotency": "./src/idempotency.ts",+ "./json": "./src/json.ts","./safe-fetch": "./src/safe-fetch.ts","./settings": "./src/settings.ts","./slack-inventory": "./src/slack-inventory.ts",diff --git a/packages/db/prisma/seed.ts b/packages/db/prisma/seed.tsindex 6ad15c6a..1ba1dcc0 100644--- a/packages/db/prisma/seed.ts+++ b/packages/db/prisma/seed.ts@@ -314,7 +314,9 @@ const EMAIL_SUBJECTS = ["Intro to your implementation lead",] as const;-const TRANSLITERATIONS: Record<string, string> = {+type Transliterations = Record<string, string>;++const TRANSLITERATIONS: Transliterations = {ø: "o",æ: "ae",œ: "oe",@@ -476,7 +478,9 @@ type SeededDeal = {closed: boolean;};-const SEED_RATES: Record<string, number> = {+type SeedRates = Record<string, number>;++const SEED_RATES: SeedRates = {EUR: 1.09,GBP: 1.27,CAD: 0.73,diff --git a/packages/db/src/attribution.ts b/packages/db/src/attribution.tsindex ba923005..490f8c0a 100644--- a/packages/db/src/attribution.ts+++ b/packages/db/src/attribution.ts@@ -34,7 +34,7 @@ export interface Touch {at: Date;}-const SEARCH: Record<string, string> = {+const SEARCH = {"google.": "Google","bing.": "Bing","duckduckgo.": "DuckDuckGo",@@ -44,9 +44,9 @@ const SEARCH: Record<string, string> = {"ecosia.": "Ecosia","startpage.": "Startpage","search.brave.": "Brave Search",-};+} satisfies Record<string, string>;-const SOCIAL: Record<string, string> = {+const SOCIAL = {"linkedin.": "LinkedIn","lnkd.in": "LinkedIn","twitter.": "X",@@ -63,13 +63,13 @@ const SOCIAL: Record<string, string> = {"producthunt.": "Product Hunt","github.": "GitHub","slack.": "Slack",-};+} satisfies Record<string, string>;-const MAIL: Record<string, string> = {+const MAIL = {"mail.google.": "Gmail","outlook.": "Outlook","mail.yahoo.": "Yahoo Mail",-};+} satisfies Record<string, string>;const SECOND_LEVEL = new Set(["co",diff --git a/packages/db/src/blob.ts b/packages/db/src/blob.tsindex cbe5aa42..eda2b6c4 100644--- a/packages/db/src/blob.ts+++ b/packages/db/src/blob.ts@@ -7,7 +7,9 @@ export { isMirrored, isOptimizable } from "./images";const MAX_BYTES = 3 * 1024 * 1024;const TIMEOUT_MS = 15_000;-const ALLOWED: Record<string, string> = {+type ExtensionByMediaType = Record<string, string>;++const ALLOWED: ExtensionByMediaType = {"image/jpeg": "jpg","image/png": "png","image/webp": "webp",diff --git a/packages/db/src/crm-events.ts b/packages/db/src/crm-events.tsindex 4772cc63..48a0d035 100644--- a/packages/db/src/crm-events.ts+++ b/packages/db/src/crm-events.ts@@ -45,7 +45,3 @@ export const CRM_EVENT_TYPES = Object.keys(CRM_EVENT_CATALOG) as [CrmEventType,...CrmEventType[],];--export function isCrmEventType(value: unknown): value is CrmEventType {- return typeof value === "string" && Object.hasOwn(CRM_EVENT_CATALOG, value);-}diff --git a/packages/db/src/fields-shape.ts b/packages/db/src/fields-shape.tsindex 95c51095..e546e101 100644--- a/packages/db/src/fields-shape.ts+++ b/packages/db/src/fields-shape.ts@@ -25,7 +25,9 @@ export type FieldValueColumn =| "optionId"| "userId";-const COLUMNS: Record<FieldTypeName, FieldValueColumn> = {+export type FieldValueWrite = Partial<Record<FieldValueColumn, unknown>>;++const COLUMNS = {TEXT: "text",LONG_TEXT: "text",URL: "text",@@ -36,9 +38,9 @@ const COLUMNS: Record<FieldTypeName, FieldValueColumn> = {CHECKBOX: "bool",SELECT: "optionId",USER: "userId",-};+} as const satisfies Record<FieldTypeName, FieldValueColumn>;-const TYPE_LABELS: Record<FieldTypeName, string> = {+const TYPE_LABELS = {TEXT: "Text",LONG_TEXT: "Long text",NUMBER: "Number",@@ -49,7 +51,7 @@ const TYPE_LABELS: Record<FieldTypeName, string> = {EMAIL: "Email",PHONE: "Phone",USER: "User",-};+} as const satisfies Record<FieldTypeName, string>;export function columnFor(type: FieldTypeName): FieldValueColumn {return COLUMNS[type];diff --git a/packages/db/src/fields.ts b/packages/db/src/fields.tsindex fcfc3b64..8b0b88ea 100644--- a/packages/db/src/fields.ts+++ b/packages/db/src/fields.ts@@ -3,6 +3,7 @@ import {type FieldValueColumn,FieldValueError,type FieldValueJson,+ type FieldValueWrite,recordColumn,typeLabel,} from "./fields-shape";@@ -99,7 +100,7 @@ const ISO_DATE_TIME =export function coerceValue(definition: FieldDefinitionWithOptions,input: unknown,-): Partial<Record<FieldValueColumn, unknown>> {+): FieldValueWrite {const blank =input === null ||input === undefined ||diff --git a/packages/db/src/index.ts b/packages/db/src/index.tsindex 722a426f..2dcfa219 100644--- a/packages/db/src/index.ts+++ b/packages/db/src/index.ts@@ -11,5 +11,7 @@ export type * from "./generated/prisma/models";export type {ContactBriefSections,FactEvidence,+ JsonObject,+ JsonValue,WorkspaceProfileSections,} from "./json";diff --git a/packages/db/src/json.ts b/packages/db/src/json.tsindex bcc672a8..9c1664c6 100644--- a/packages/db/src/json.ts+++ b/packages/db/src/json.ts@@ -1,3 +1,25 @@+import type { Prisma } from "./generated/prisma/client";++export type JsonValue = Prisma.JsonValue;++export type JsonObject = Prisma.JsonObject;++export function jsonObject(value: JsonValue | undefined): JsonObject {+ return isJsonObject(value) ? value : {};+}++export function jsonText(value: JsonValue | undefined): string | undefined {+ return isJsonText(value) ? value : undefined;+}++function isJsonObject(value: JsonValue | undefined): value is JsonObject {+ return value instanceof Object && !Array.isArray(value);+}++function isJsonText(value: JsonValue | undefined): value is string {+ return String(value) === value;+}+export type FactEvidence = {kind: string;detail: string;diff --git a/packages/db/src/tracking.ts b/packages/db/src/tracking.tsindex 5757de70..dc3c6fea 100644--- a/packages/db/src/tracking.ts+++ b/packages/db/src/tracking.ts@@ -86,8 +86,10 @@ export function mintSiteId(): string {return `${SITE_ID_PREFIX}${randomBytes(4).toString("hex")}`;}+const SITE_ID_SHAPE = /^cmp_[0-9a-f]{8}$/;+export function isSiteId(value: string | null | undefined): value is string {- return typeof value === "string" && /^cmp_[0-9a-f]{8}$/.test(value);+ return SITE_ID_SHAPE.test(value ?? "");}export function loaderUrl(appUrl: string): string {@@ -142,10 +144,12 @@ export function configHash(config: TrackingConfig): string {return createHash("sha256").update(canonical).digest("hex").slice(0, 12);}+const CONFIG_HASH_SHAPE = /^[0-9a-f]{12}$/;+export function isConfigHash(value: string | null | undefined,): value is string {- return typeof value === "string" && /^[0-9a-f]{12}$/.test(value);+ return CONFIG_HASH_SHAPE.test(value ?? "");}export function normalizeHost(input: string | null | undefined): string | null {diff --git a/packages/db/src/workspace.ts b/packages/db/src/workspace.tsindex c3a7ffc7..b49c748c 100644--- a/packages/db/src/workspace.ts+++ b/packages/db/src/workspace.ts@@ -1,5 +1,11 @@import type { Db } from "./client";-import type { WorkspaceProfileSections } from "./json";+import {+ type JsonObject,+ type JsonValue,+ jsonObject,+ jsonText,+ type WorkspaceProfileSections,+} from "./json";export const WORKSPACE_ID = "workspace";@@ -42,30 +48,26 @@ export const MAX_NARRATIVE = 320;export const MAX_LINE = 140;export function isOnboarded(metadata: string | null): boolean {- return typeof readMetadata(metadata).onboardedAt === "string";+ return jsonText(readMetadata(metadata).onboardedAt) !== undefined;}export function markOnboarded(metadata: string | null, at: Date): string {const current = readMetadata(metadata);return JSON.stringify(- typeof current.onboardedAt === "string"- ? current- : { ...current, onboardedAt: at.toISOString() },+ jsonText(current.onboardedAt) === undefined+ ? { ...current, onboardedAt: at.toISOString() }+ : current,);}-function readMetadata(metadata: string | null): Record<string, unknown> {+function readMetadata(metadata: string | null): JsonObject {if (!metadata) return {};try {- const parsed: unknown = JSON.parse(metadata);+ const parsed: JsonValue = JSON.parse(metadata);- return typeof parsed === "object" &&- parsed !== null &&- !Array.isArray(parsed)- ? (parsed as Record<string, unknown>)- : {};+ return jsonObject(parsed);} catch {return {};}@@ -213,14 +215,9 @@ function clamp(value: string | undefined, max: number): string | undefined {return trimmed.length <= max ? trimmed : `${trimmed.slice(0, max - 1)}…`;}-function readSections(value: unknown): WorkspaceProfileSections {- if (typeof value !== "object" || value === null) return {};-- const record = value as Record<string, unknown>;- const text = (key: string) =>- typeof record[key] === "string" && record[key].trim()- ? record[key].trim()- : undefined;+function readSections(value: JsonValue): WorkspaceProfileSections {+ const record = jsonObject(value);+ const text = (key: string) => jsonText(record[key])?.trim() || undefined;return trimSections({sells: text("sells"),diff --git a/packages/db/test/currency.spec.ts b/packages/db/test/currency.spec.tsindex 9fec3349..de9043ab 100644--- a/packages/db/test/currency.spec.ts+++ b/packages/db/test/currency.spec.ts@@ -19,7 +19,9 @@ type Row = {provider: string | null;};-function fakeDb(rows: Row[]): { db: Db; calls: number } {+type FakeDb = { db: Db; calls: number };++function fakeDb(rows: Row[]): FakeDb {const state = { calls: 0 };const db = {diff --git a/packages/env/src/index.ts b/packages/env/src/index.tsindex bb608fd1..d9fee179 100644--- a/packages/env/src/index.ts+++ b/packages/env/src/index.ts@@ -24,8 +24,7 @@ function isWorkspaceRoot(directory: string): boolean {try {const parsed: unknown = JSON.parse(readFileSync(manifest, "utf8"));return (- typeof parsed === "object" &&- parsed !== null &&+ parsed instanceof Object &&"workspaces" in parsed &&parsed.workspaces !== undefined);@@ -34,8 +33,10 @@ function isWorkspaceRoot(directory: string): boolean {}}-export function parseEnv(source: string): Record<string, string> {- const values: Record<string, string> = {};+type EnvValues = Record<string, string>;++export function parseEnv(source: string): EnvValues {+ const values: EnvValues = {};for (const rawLine of source.split(/\r?\n/)) {const line = rawLine.trim();@@ -78,7 +79,7 @@ export function loadRootEnv(): void {const root = findWorkspaceRoot(process.cwd());if (!root) return;- const merged: Record<string, string> = {};+ const merged: EnvValues = {};const runtimeFs = process.getBuiltinModule("node:fs");for (const file of FILES) {diff --git a/packages/env/test/root.spec.ts b/packages/env/test/root.spec.tsindex adf300b8..46090304 100644--- a/packages/env/test/root.spec.ts+++ b/packages/env/test/root.spec.ts@@ -11,8 +11,7 @@ function findRoot(start: string): string | null {try {const parsed: unknown = JSON.parse(readFileSync(manifest, "utf8"));if (- typeof parsed === "object" &&- parsed !== null &&+ parsed instanceof Object &&"workspaces" in parsed &&parsed.workspaces !== undefined) {@@ -52,11 +51,7 @@ describe("finding the workspace root", () => {const manifest: unknown = JSON.parse(readFileSync(join(repoRoot, "apps", "api", "package.json"), "utf8"),);- expect(- typeof manifest === "object" &&- manifest !== null &&- "workspaces" in manifest,- ).toBe(false);+ expect(manifest instanceof Object && "workspaces" in manifest).toBe(false);});});diff --git a/packages/telemetry/src/allowlist.ts b/packages/telemetry/src/allowlist.tsindex 1c6d7492..d2881bb1 100644--- a/packages/telemetry/src/allowlist.ts+++ b/packages/telemetry/src/allowlist.ts@@ -98,10 +98,12 @@ export type PropertyValue =export type Properties = Partial<Record<AllowedProperty, PropertyValue>>;+export type PermittedProperties = Record<string, PropertyValue>;+export function permitted(properties: Record<string, unknown>,-): Record<string, PropertyValue> {- const kept: Record<string, PropertyValue> = {};+): PermittedProperties {+ const kept: PermittedProperties = {};for (const [name, value] of Object.entries(properties)) {if (!ALLOWED.has(name)) continue;@@ -239,11 +241,11 @@ export function permittedSyncSource(source: string | null | undefined): string {const MAILBOX_SYNC = "mailbox_sync";-const SYNC_ERROR_SOURCES: Record<TelemetrySyncSource, string> = {+const SYNC_ERROR_SOURCES = {gmail: "google_sync",calendar: "google_sync",outlook: "microsoft_sync",-};+} as const satisfies Record<TelemetrySyncSource, string>;export function permittedSyncErrorSource(source: string | null | undefined,diff --git a/packages/telemetry/src/client.ts b/packages/telemetry/src/client.tsindex ad0e47c4..77c1943c 100644--- a/packages/telemetry/src/client.ts+++ b/packages/telemetry/src/client.ts@@ -9,6 +9,8 @@ import { commitSha } from "./version";const FLUSH_TIMEOUT_MS = 3_000;+type CaptureMessage = Parameters<PostHog["capture"]>[0];+type Debug = (message: string) => void;let debug: Debug = () => {};@@ -73,22 +75,24 @@ export function resetTelemetryClient(): void {async function payload(properties: Properties,-): Promise<Record<string, unknown> | null> {+): Promise<Omit<CaptureMessage, "event"> | null> {const install = await readInstall();if (!install) return null;const sha = commitSha();+ const identity: Properties = {+ crm_version: install.version,+ days_since_install: daysSince(install.createdAt),+ is_vercel: Boolean(process.env.VERCEL),+ };++ if (sha) identity.git_commit_sha = sha;+return {distinctId: install.uuid,properties: {- ...permitted({- crm_version: install.version,- days_since_install: daysSince(install.createdAt),- is_vercel: Boolean(process.env.VERCEL),- ...(sha ? { git_commit_sha: sha } : {}),- ...properties,- }),+ ...permitted({ ...identity, ...properties }),$ip: null,$process_person_profile: false,},@@ -125,12 +129,10 @@ async function send(const message = await payload(properties);if (!message) return false;- const full = {- ...message,- event,- ...(at ? { timestamp: at } : {}),- ...(uuid ? { uuid } : {}),- } as Parameters<PostHog["capture"]>[0];+ const full: CaptureMessage = { ...message, event };++ if (at) full.timestamp = at;+ if (uuid) full.uuid = uuid;if (immediate) {const before = failures;diff --git a/packages/telemetry/test/client.integration.spec.ts b/packages/telemetry/test/client.integration.spec.tsindex acbea24a..3693287e 100644--- a/packages/telemetry/test/client.integration.spec.ts+++ b/packages/telemetry/test/client.integration.spec.ts@@ -1,5 +1,11 @@import { afterEach, beforeEach, describe, expect, it } from "bun:test";import { db } from "@crm/db";+import {+ type JsonObject,+ type JsonValue,+ jsonObject,+ jsonText,+} from "@crm/db/json";import { captureNow, resetTelemetryClient } from "../src/client";import { milestone } from "../src/events";import { forgetInstall, readInstall, stableUuid } from "../src/install";@@ -12,12 +18,15 @@ const real = {doNotTrack: process.env.DO_NOT_TRACK,};-let calls: { url: string; body: unknown }[] = [];+let calls: { url: string; body: JsonValue }[] = [];function stubFetch(): void {calls = [];- globalThis.fetch = (async (input: unknown, init?: { body?: unknown }) => {+ globalThis.fetch = (async (+ input: RequestInfo | URL,+ init?: { body?: BodyInit | null },+ ) => {calls.push({ url: String(input), body: await decode(init?.body) });return new Response(JSON.stringify({ status: 1 }), {@@ -27,17 +36,20 @@ function stubFetch(): void {}) as typeof fetch;}-async function decode(body: unknown): Promise<unknown> {+async function decode(body: BodyInit | null | undefined): Promise<JsonValue> {if (body === undefined || body === null) return null;- if (typeof body === "string") return JSON.parse(body);+ if (body instanceof Blob)+ return unzip(new Uint8Array(await body.arrayBuffer()));+ if (body instanceof ArrayBuffer) return unzip(new Uint8Array(body));+ if (ArrayBuffer.isView(body)) {+ return unzip(new Uint8Array(body.buffer, body.byteOffset, body.byteLength));+ }- const bytes =- body instanceof Blob- ? new Uint8Array(await body.arrayBuffer())- : new Uint8Array(body as ArrayBufferLike);+ return JSON.parse(String(body));+}- const text = new TextDecoder().decode(Bun.gunzipSync(bytes));- return JSON.parse(text);+function unzip(bytes: Uint8Array): JsonValue {+ return JSON.parse(new TextDecoder().decode(Bun.gunzipSync(bytes)));}beforeEach(() => {@@ -239,26 +251,30 @@ describe("milestone", () => {});type Captured = {- event: string;- distinct_id: string;- uuid?: string;- properties: Record<string, unknown>;+ event: string | undefined;+ distinct_id: string | undefined;+ uuid: string | undefined;+ properties: JsonObject;};-function eventOf(body: unknown): Captured {- const batch = (body as { batch?: Captured[] })?.batch;- const event = batch?.[0] ?? (body as Captured);+function eventOf(body: JsonValue | undefined): Captured {+ const sent = jsonObject(body);+ const batch = sent.batch;+ const first = Array.isArray(batch) ? batch[0] : undefined;+ const event = jsonObject(first ?? sent);return {- event: event?.event,- distinct_id: event?.distinct_id,- uuid: event?.uuid,- properties: event?.properties ?? {},+ event: jsonText(event.event),+ distinct_id: jsonText(event.distinct_id),+ uuid: jsonText(event.uuid),+ properties: jsonObject(event.properties),};}function sentSteps(): string[] {- return calls.map((call) => eventOf(call.body).event).filter(Boolean);+ return calls+ .map((call) => eventOf(call.body).event)+ .filter((step) => step !== undefined);}async function installUuid(): Promise<string> {diff --git a/packages/ui/src/lib/dither.ts b/packages/ui/src/lib/dither.tsindex dd4a8c84..040efaa5 100644--- a/packages/ui/src/lib/dither.ts+++ b/packages/ui/src/lib/dither.ts@@ -1,11 +1,11 @@export type Bloom = "off" | "low" | "high" | "aura";-const BLOOM_CLASS: Record<Bloom, string> = {+const BLOOM_CLASS = {off: "",low: "bloom-low",high: "bloom-high",aura: "bloom-aura",-};+} as const satisfies Record<Bloom, string>;export function bloomClass(bloom: Bloom = "off"): string {return BLOOM_CLASS[bloom];diff --git a/packages/validation/package.json b/packages/validation/package.jsonindex 82b9178c..105f3b29 100644--- a/packages/validation/package.json+++ b/packages/validation/package.json@@ -4,7 +4,13 @@"private": true,"type": "module","exports": {- ".": "./src/index.ts"+ ".": "./src/index.ts",+ "./activity-meta": "./src/activity-meta.ts",+ "./agent-events": "./src/agent-events.ts",+ "./agent-manifest": "./src/agent-manifest.ts",+ "./builder-question": "./src/builder-question.ts",+ "./eve-stream": "./src/eve-stream.ts",+ "./eve-tool": "./src/eve-tool.ts"},"scripts": {"check-types": "tsc --noEmit",@@ -13,6 +19,7 @@"clean": "rm -rf .turbo node_modules"},"dependencies": {+ "@crm/db": "workspace:*","zod": "^4.4.3"},"devDependencies": {diff --git a/packages/validation/src/activity-meta.ts b/packages/validation/src/activity-meta.tsnew file mode 100644index 00000000..fb9c89ea--- /dev/null+++ b/packages/validation/src/activity-meta.ts@@ -0,0 +1,9 @@+import { z } from "zod";++export const activityMetaFields = z.record(z.string(), z.json());++export type ActivityMetaFields = z.infer<typeof activityMetaFields>;++export const activityMeta = activityMetaFields.nullable().catch(null);++export type ActivityMeta = z.infer<typeof activityMeta>;diff --git a/packages/validation/src/agent-events.ts b/packages/validation/src/agent-events.tsnew file mode 100644index 00000000..d4ce5bb2--- /dev/null+++ b/packages/validation/src/agent-events.ts@@ -0,0 +1,22 @@+import { CRM_EVENT_CATALOG, CRM_EVENT_TYPES } from "@crm/db/crm-events";+import { z } from "zod";++export const crmEventTask = z+ .object({+ type: z.enum(CRM_EVENT_TYPES),+ record: z.object({+ kind: z.string().trim(),+ id: z.string().trim().min(1),+ }),+ occurredAt: z+ .string()+ .trim()+ .min(1)+ .refine((value) => !Number.isNaN(new Date(value).getTime())),+ data: z.record(z.string(), z.json()).catch({}),+ })+ .refine(+ (task) => CRM_EVENT_CATALOG[task.type].recordKind === task.record.kind,+ );++export type CrmEventTask = z.infer<typeof crmEventTask>;diff --git a/apps/agent/agent/lib/agent-manifest.ts b/packages/validation/src/agent-manifest.tssimilarity index 54%rename from apps/agent/agent/lib/agent-manifest.tsrename to packages/validation/src/agent-manifest.tsindex f8d8b661..9a496e8d 100644--- a/apps/agent/agent/lib/agent-manifest.ts+++ b/packages/validation/src/agent-manifest.ts@@ -1,6 +1,20 @@import { CRM_EVENT_TYPES } from "@crm/db/crm-events";import { z } from "zod";-import { AGENT_ACTION_TYPES } from "./agent-actions";++export const AGENT_ACTION_TYPES = {+ CRM_ACTIVITY_CREATE: "crm.activity.create",+ RUN_SUMMARY: "run.summary",+ SLACK_MESSAGE_POST: "slack.message.post",+} as const;++export type AgentActionType =+ (typeof AGENT_ACTION_TYPES)[keyof typeof AGENT_ACTION_TYPES];++export const AGENT_TRIGGER_INTERVAL_MINUTES = {+ min: 1,+ max: 525_600,+ fallback: 1_440,+} as const;const slackDestination = z.object({kind: z.enum(["channel", "user"]),@@ -32,6 +46,15 @@ export const agentManifestAction = z.discriminatedUnion("type", [}),]);+export const agentScheduleTriggerConfig = z.object({+ nextRunAt: z.string(),+ intervalMinutes: z.number().int().min(AGENT_TRIGGER_INTERVAL_MINUTES.min),+});++export const agentEventTriggerConfig = z.object({+ event: z.enum(CRM_EVENT_TYPES),+});+export const agentManifestTrigger = z.discriminatedUnion("type", [z.object({type: z.literal("MANUAL"),@@ -43,16 +66,13 @@ export const agentManifestTrigger = z.discriminatedUnion("type", [type: z.literal("SCHEDULE"),name: z.string(),summary: z.string(),- config: z.object({- nextRunAt: z.string(),- intervalMinutes: z.number().int().min(1),- }),+ config: agentScheduleTriggerConfig,}),z.object({type: z.literal("EVENT"),name: z.string(),summary: z.string(),- config: z.object({ event: z.enum(CRM_EVENT_TYPES) }),+ config: agentEventTriggerConfig,}),]);@@ -90,6 +110,7 @@ export const agentManifest = zexport type SlackDestination = z.infer<typeof slackDestination>;export type AgentManifestAction = z.infer<typeof agentManifestAction>;export type AgentManifestTrigger = z.infer<typeof agentManifestTrigger>;+export type AgentManifestResource = z.infer<typeof agentManifestResource>;export type AgentManifest = z.infer<typeof agentManifest>;export class InvalidAgentManifest extends Error {@@ -109,3 +130,56 @@ export function parseAgentManifest(value: unknown): AgentManifest {.join("; "),);}++export const agentTriggerConfig = z.object({+ intervalMinutes: z+ .number()+ .min(AGENT_TRIGGER_INTERVAL_MINUTES.min)+ .transform((minutes) =>+ Math.min(minutes, AGENT_TRIGGER_INTERVAL_MINUTES.max),+ )+ .catch(AGENT_TRIGGER_INTERVAL_MINUTES.fallback),+ event: z.enum(CRM_EVENT_TYPES).nullable().catch(null),+});++export type AgentTriggerConfig = z.infer<typeof agentTriggerConfig>;++const UNREADABLE_TRIGGER_CONFIG: AgentTriggerConfig = {+ intervalMinutes: AGENT_TRIGGER_INTERVAL_MINUTES.fallback,+ event: null,+};++export function readAgentTriggerConfig(value: unknown): AgentTriggerConfig {+ const parsed = agentTriggerConfig.safeParse(value);+ return parsed.success ? parsed.data : UNREADABLE_TRIGGER_CONFIG;+}++const optionalText = z.string().optional().catch(undefined);++export const agentManifestSummary = z.object({+ name: optionalText,+ description: optionalText,+ access: z+ .array(z.string().nullable().catch(null))+ .catch([])+ .transform((entries) => entries.filter((entry) => entry !== null)),+ triggers: z+ .array(z.object({ type: optionalText, summary: optionalText }).catch({}))+ .catch([]),+ actions: z.array(z.object({ summary: optionalText }).catch({})).catch([]),+ dataScope: z.object({ summary: optionalText }).catch({}),+});++export type AgentManifestSummary = z.infer<typeof agentManifestSummary>;++const UNREADABLE_MANIFEST_SUMMARY: AgentManifestSummary = {+ access: [],+ triggers: [],+ actions: [],+ dataScope: {},+};++export function readAgentManifestSummary(value: unknown): AgentManifestSummary {+ const parsed = agentManifestSummary.safeParse(value);+ return parsed.success ? parsed.data : UNREADABLE_MANIFEST_SUMMARY;+}diff --git a/packages/validation/src/builder-question.ts b/packages/validation/src/builder-question.tsnew file mode 100644index 00000000..d412bcd8--- /dev/null+++ b/packages/validation/src/builder-question.ts@@ -0,0 +1,43 @@+import { z } from "zod";++const optionalText = z.string().optional().catch(undefined);++const builderQuestionOption = z.object({+ id: z.string().min(1),+ label: z.string().min(1),+ description: optionalText,+ style: z.enum(["danger", "default", "primary"]).optional().catch(undefined),+});++export type BuilderQuestionOption = z.infer<typeof builderQuestionOption>;++const askedQuestion = z+ .object({+ kind: z.literal("question"),+ requestId: z.string().min(1),+ prompt: z.string().min(1),+ display: z+ .enum(["confirmation", "select", "text"])+ .optional()+ .catch(undefined),+ options: z+ .array(builderQuestionOption.nullable().catch(null))+ .catch([])+ .transform((entries) => entries.filter((entry) => entry !== null)),+ allowFreeform: z.boolean().optional().catch(undefined),+ })+ .transform((question) => ({+ kind: question.kind,+ requestId: question.requestId,+ prompt: question.prompt,+ display: question.display,+ options: question.options,+ allowFreeform:+ question.allowFreeform === true ||+ question.display === "text" ||+ question.options.length === 0,+ }));++export type BuilderQuestion = z.infer<typeof askedQuestion>;++export const builderQuestion = askedQuestion.nullable().catch(null);diff --git a/packages/validation/src/eve-stream.ts b/packages/validation/src/eve-stream.tsnew file mode 100644index 00000000..00f182a3--- /dev/null+++ b/packages/validation/src/eve-stream.ts@@ -0,0 +1,49 @@+import { z } from "zod";++export type EveStreamEvent = {+ type: string;+ data?: unknown;+};++const text = z.string().nullable().catch(null);++const identifier = z.string().min(1).nullable().catch(null);++export const eveTurnReference = z+ .object({ turnId: identifier })+ .catch({ turnId: null });++export type EveTurnReference = z.infer<typeof eveTurnReference>;++export const eveTurnFailure = z+ .object({ code: text, message: text })+ .catch({ code: null, message: null });++export type EveTurnFailure = z.infer<typeof eveTurnFailure>;++const eveRequestedAction = z+ .object({+ kind: text,+ name: text,+ subagentName: text,+ callId: text,+ })+ .catch({ kind: null, name: null, subagentName: null, callId: null });++export const eveRequestedActions = z+ .object({ actions: z.array(eveRequestedAction).catch([]) })+ .catch({ actions: [] });++export type EveRequestedActions = z.infer<typeof eveRequestedActions>;++const eveCallReference = z.object({ callId: text }).catch({ callId: null });++export const eveSettledAction = z+ .object({+ callId: text,+ result: eveCallReference,+ action: eveCallReference,+ })+ .catch({ callId: null, result: { callId: null }, action: { callId: null } });++export type EveSettledAction = z.infer<typeof eveSettledAction>;diff --git a/packages/validation/src/eve-tool.ts b/packages/validation/src/eve-tool.tsnew file mode 100644index 00000000..76c4eb80--- /dev/null+++ b/packages/validation/src/eve-tool.ts@@ -0,0 +1,40 @@+import { z } from "zod";++const json = z.json();++const eveToolFields = z.record(z.string(), json);++export type EveToolFields = z.infer<typeof eveToolFields>;++export const eveToolInput = eveToolFields.nullable().catch(null);++export type EveToolInput = z.infer<typeof eveToolInput>;++export const eveToolOutput = eveToolFields.nullable().catch(null);++export type EveToolOutput = z.infer<typeof eveToolOutput>;++export const eveToolText = z.string().catch("");++const flag = z.boolean().nullable().catch(null);++const link = z+ .string()+ .regex(/^https?:\/\//)+ .nullable()+ .catch(null);++export const eveToolOutcome = z+ .object({+ reason: z.string().nullable().catch(null),+ applied: flag,+ written: flag,+ stored: flag,+ sourceUrl: link,+ profileUrl: link,+ url: link,+ })+ .nullable()+ .catch(null);++export type EveToolOutcome = z.infer<typeof eveToolOutcome>;diff --git a/packages/validation/src/index.ts b/packages/validation/src/index.tsindex 97e8de82..0cf43c9c 100644--- a/packages/validation/src/index.ts+++ b/packages/validation/src/index.ts@@ -1,9 +1,36 @@import type { ZodType, z } from "zod";+import * as activityMeta from "./activity-meta";+import * as agentEvents from "./agent-events";+import * as agentManifest from "./agent-manifest";import * as agents from "./agents";+import * as builderQuestion from "./builder-question";+import * as eveStream from "./eve-stream";+import * as eveTool from "./eve-tool";import * as slack from "./slack";-export const schemas = { agents, slack } as const;+export const schemas = {+ activityMeta,+ agentEvents,+ agentManifest,+ agents,+ builderQuestion,+ eveStream,+ eveTool,+ slack,+} as const;+export type { ActivityMeta, ActivityMetaFields } from "./activity-meta";+export type { CrmEventTask } from "./agent-events";+export type {+ AgentActionType,+ AgentManifest,+ AgentManifestAction,+ AgentManifestResource,+ AgentManifestSummary,+ AgentManifestTrigger,+ AgentTriggerConfig,+ SlackDestination,+} from "./agent-manifest";export type {Handoff,HandoffChannel,@@ -12,7 +39,24 @@ export type {InputRequested,Permission,} from "./agents";-export type { AuthTest, Installation, JoinPayload, Reply } from "./slack";+export type {+ BuilderQuestion,+ BuilderQuestionOption,+} from "./builder-question";+export type {+ EveRequestedActions,+ EveSettledAction,+ EveStreamEvent,+ EveTurnFailure,+ EveTurnReference,+} from "./eve-stream";+export type {+ EveToolFields,+ EveToolInput,+ EveToolOutcome,+ EveToolOutput,+} from "./eve-tool";+export type { AuthTest, JoinPayload, OauthAccess, Reply } from "./slack";export class InvalidInput extends Error {override readonly name = "InvalidInput";diff --git a/packages/validation/src/slack.ts b/packages/validation/src/slack.tsindex b594f45c..7d276a21 100644--- a/packages/validation/src/slack.ts+++ b/packages/validation/src/slack.ts@@ -32,22 +32,53 @@ export const authTest = reply.extend({user_id: z.string().trim().min(1).optional(),});-export const installation = z.object({- team: z.object({- id: z.string().trim().min(1),- name: z.string().trim().min(1).optional(),- }),- authed_user: z- .object({- id: z.string().trim().min(1),- access_token: z.string().trim().min(1).optional(),- scope: z.string().trim().optional(),- })- .optional(),-});+const present = z.string().min(1).optional().catch(undefined);++export const oauthAccess = z+ .object({+ ok: z.boolean().catch(false),+ error: present,+ access_token: present,+ token_type: present,+ scope: z.string().optional().catch(undefined),+ team: z+ .object({+ id: z.string().trim().min(1),+ name: z.string().trim().min(1).optional(),+ })+ .optional()+ .catch(undefined),+ authed_user: z+ .object({+ id: z.string().trim().min(1),+ access_token: z.string().trim().min(1).optional(),+ scope: z.string().trim().optional(),+ })+ .optional()+ .catch(undefined),+ })+ .catch({ ok: false });++export const userInfo = z+ .object({+ ok: z.boolean().catch(false),+ user: z+ .object({+ name: present,+ profile: z+ .object({+ email: present,+ real_name: present,+ image_512: present,+ })+ .catch({}),+ })+ .catch({ profile: {} }),+ })+ .catch({ ok: false, user: { profile: {} } });export type CreatePayload = z.infer<typeof createPayload>;export type JoinPayload = z.infer<typeof joinPayload>;export type Reply = z.infer<typeof reply>;export type AuthTest = z.infer<typeof authTest>;-export type Installation = z.infer<typeof installation>;+export type OauthAccess = z.infer<typeof oauthAccess>;