import { makeFunctionReference } from "convex/server"; import { assertNoUnknownFlags, type CrmCliContext, callQuery, out, shift, } from "./crm"; import { getFlag } from "./lib/cli-args"; const getContext = makeFunctionReference<"query">( "functions/crm/contextDiagnostics:getForEntry", ); // Keep the shipped CLI independent of application-only Convex source files. type ContextPage = { record: { label: string }; generation: { state: string }; scopeLimited: boolean; sourceLimited: boolean; sources: Array<{ occurredAt: number; kind: string; title: string; association: string; usedByLastBrief: boolean; sourceTable: string; sourceId: string; }>; mailboxes: Array<{ email: string; label: string }>; nextCursor: string | null; }; /** Read-only: inspecting a sync never starts a generation or connector call. */ export async function runContextCommand(ctx: CrmCliContext): Promise { const objectName = shift(ctx.args, "object name"); const entryId = shift(ctx.args, "entry id"); const cursor = getFlag(ctx.args, "--cursor"); assertNoUnknownFlags(ctx.args, "crm context"); const result = (await callQuery(ctx, getContext, { objectName, entryId, ...(cursor ? { cursor } : {}), })) as ContextPage; if (ctx.jsonOutput) { out(ctx, result); return; } const lines = [ `${result.record.label} ยท Brief ${result.generation.state}`, "Bounded snapshot of sources you can access; complete history is not certified.", ...(result.scopeLimited ? ["Related-record scope reached its limit."] : []), ...(result.sourceLimited ? ["Source inspection reached its read limit."] : []), ...result.sources.map( (row) => `${new Date(row.occurredAt).toISOString()} ${row.kind} ${row.title} [${row.association}; ${row.usedByLastBrief ? "used by last Brief" : "not in last Brief"}] ${row.sourceTable}:${row.sourceId}`, ), ...result.mailboxes.map((row) => `${row.email}: ${row.label}`), ...(result.nextCursor ? [`More sources: repeat with --cursor '${result.nextCursor}'`] : []), ]; out(ctx, lines.join("\n")); }