Comp AI CRM is an open source, CRM designed for AI agents. Agentic-first CRM.
TypeScript
feat(app): CMP-12 review agent drafts before deployment (#63)
grim51a4a1184328
diff --git a/apps/api/src/agent/agent-definitions.service.ts b/apps/api/src/agent/agent-definitions.service.tsindex 6ab1a048..89d97466 100644--- a/apps/api/src/agent/agent-definitions.service.ts+++ b/apps/api/src/agent/agent-definitions.service.ts@@ -88,6 +88,20 @@ export class AgentDefinitionsService {deployedAt: true,},},+ versions: {+ where: { status: { in: ["DRAFT", "READY"] } },+ orderBy: { number: "desc" },+ take: 1,+ select: {+ id: true,+ number: true,+ status: true,+ manifest: true,+ modelId: true,+ sandboxPolicy: true,+ sourceConversationId: true,+ },+ },triggers: {orderBy: { createdAt: "asc" },select: {@@ -105,25 +119,27 @@ export class AgentDefinitionsService {});if (!row) throw new NotFoundException(`No agent with id ${id}.`);+ const { versions, ...agent } = row;return {- ...row,- canManage: row.createdById === userId || (await this.canAdmin(userId)),- createdAt: row.createdAt.toISOString(),- updatedAt: row.updatedAt.toISOString(),- currentVersion: row.currentVersion+ ...agent,+ canManage: agent.createdById === userId || (await this.canAdmin(userId)),+ createdAt: agent.createdAt.toISOString(),+ updatedAt: agent.updatedAt.toISOString(),+ currentVersion: agent.currentVersion? {- ...row.currentVersion,- approvedAt: row.currentVersion.approvedAt?.toISOString() ?? null,- deployedAt: row.currentVersion.deployedAt?.toISOString() ?? null,+ ...agent.currentVersion,+ approvedAt: agent.currentVersion.approvedAt?.toISOString() ?? null,+ deployedAt: agent.currentVersion.deployedAt?.toISOString() ?? null,}: null,- triggers: row.triggers.map((trigger) => ({+ reviewVersion: agent.status === "DRAFT" ? (versions[0] ?? null) : null,+ triggers: agent.triggers.map((trigger) => ({...trigger,nextRunAt: trigger.nextRunAt?.toISOString() ?? null,lastRunAt: trigger.lastRunAt?.toISOString() ?? null,})),- runCount: row._count.runs,+ runCount: agent._count.runs,};}diff --git a/apps/api/test/agent-lifecycle.spec.ts b/apps/api/test/agent-lifecycle.spec.tsindex 97e6627e..c66d0156 100644--- a/apps/api/test/agent-lifecycle.spec.ts+++ b/apps/api/test/agent-lifecycle.spec.ts@@ -151,7 +151,14 @@ describe("agent lifecycle", () => {description: null,status: "DRAFT",});- expect((await agents.byId(agentId, userId)).canManage).toBe(true);+ expect(await agents.byId(agentId, userId)).toMatchObject({+ canManage: true,+ reviewVersion: {+ id: versionId,+ number: 1,+ status: "READY",+ },+ });expect((await agents.list(userId)).some((agent) => agent.id === agentId),).toBe(false);diff --git a/apps/app/components/agent-builder/agent-builder-chat.tsx b/apps/app/components/agent-builder/agent-builder-chat.tsxindex 89d5fe03..887f65c2 100644--- a/apps/app/components/agent-builder/agent-builder-chat.tsx+++ b/apps/app/components/agent-builder/agent-builder-chat.tsx@@ -1052,40 +1052,20 @@ function ReviewAgentCard({conversation: Conversation;versionId: string;}) {- const trpc = useTRPC();- const queryClient = useQueryClient();+ const workspaceUrl = useWorkspaceUrl();const version = conversation.createdVersions.find((candidate) => candidate.id === versionId,) as DraftVersion | undefined;const agent = conversation.agent;const manifest = manifestOf(version?.manifest);- const deploy = useMutation(- trpc.agents.deploy.mutationOptions({- onSuccess: async () => {- await Promise.all([- queryClient.invalidateQueries({- queryKey: trpc.conversations.builderById.pathKey(),- }),- queryClient.invalidateQueries({- queryKey: trpc.conversations.builderList.pathKey(),- }),- queryClient.invalidateQueries({- queryKey: trpc.agents.list.pathKey(),- }),- ]);- toast.success("Agent deployed to the team.");- },- onError: (error) => toast.error(error.message),- }),- );if (!version || !agent) return null;return (<div className="flex flex-col gap-5"><p className="max-w-[640px] text-pretty text-sm leading-5">- I’ve drafted the agent. Its files and configuration stay private to you- until you deploy it.+ I’ve drafted the agent. Review its details while it stays private to+ you.</p><div className="overflow-hidden rounded-lg border bg-card"><div className="border-b px-4 py-3 sm:px-5 sm:py-4">@@ -1109,49 +1089,18 @@ function ReviewAgentCard({</p><div className="flex min-h-14 flex-col items-stretch gap-3 border-t px-4 py-3 sm:flex-row sm:items-center sm:justify-between sm:gap-4 sm:px-5"><p className="text-pretty text-muted-foreground text-xs">- Deploying approves this version, scope, and its listed actions for- the whole team.+ Inspect this version, scope, and its listed actions before making it+ available to the team.</p>- <div className="flex flex-wrap justify-end gap-1 sm:shrink-0">- <Button- variant="ghost"- onClick={() =>- document- .querySelector<HTMLTextAreaElement>(- 'textarea[aria-label="Message the agent builder"]',- )- ?.focus()- }- >- Change details- </Button>- <Button- disabled={deploy.isPending}- aria-busy={deploy.isPending}- onClick={() =>- deploy.mutate({- id: agent.id,- versionId: version.id,- clientRequestId: crypto.randomUUID(),- })- }- >- <AsyncButtonContent- status={- deploy.isPending- ? "pending"- : deploy.isError- ? "error"- : deploy.isSuccess- ? "success"- : "idle"- }- pendingLabel="Deploying"- successLabel="Deployed"- errorLabel="Try again"+ <div className="sm:shrink-0">+ <Button asChild>+ <Link+ href={workspaceUrl(`/agents/${agent.id}`)}+ transitionTypes={["nav-forward"]}>- Deploy agent- </AsyncButtonContent>+ View agent details+ <Icon icon={ArrowRight} data-icon="inline-end" />+ </Link></Button></div></div>@@ -1354,7 +1303,7 @@ function ChatLoading() {</div></div><span role="status" className="sr-only">- Loading chat+ Loading chat…</span></main>);diff --git a/apps/app/components/agent-builder/team-agent-detail.tsx b/apps/app/components/agent-builder/team-agent-detail.tsxindex 5f168d8c..2b49b4a3 100644--- a/apps/app/components/agent-builder/team-agent-detail.tsx+++ b/apps/app/components/agent-builder/team-agent-detail.tsx@@ -28,9 +28,9 @@ import {DropdownMenuTrigger,} from "@crm/ui/components/dropdown-menu";import { Icon } from "@crm/ui/components/icon";-import { Skeleton } from "@crm/ui/components/skeleton";import { cn } from "@crm/ui/lib/utils";import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";+import Link from "next/link";import { useRouter } from "next/navigation";import { useState } from "react";import { toast } from "sonner";@@ -49,6 +49,7 @@ import { useWorkspaceUrl } from "@/lib/use-workspace-url";type AgentTab = "overview" | "runs" | "activity";type AgentDetail = RouterOutputs["agents"]["byId"];+type ReviewVersion = AgentDetail["reviewVersion"];type Runs = RouterOutputs["agents"]["history"];type Activity = RouterOutputs["agents"]["activity"];type RunRow = Omit<Runs[number], "events"> & {@@ -93,7 +94,9 @@ export function TeamAgentDetail({}) {const trpc = useTRPC();const queryClient = useQueryClient();- const [tab, setTab] = useState<AgentTab>("runs");+ const [tab, setTab] = useState<AgentTab>(() =>+ initialAgent.status === "DRAFT" ? "overview" : "runs",+ );const agent = useQuery({...trpc.agents.byId.queryOptions({ id: agentId }),initialData: initialAgent,@@ -161,26 +164,6 @@ export function TeamAgentDetail({action: () => resume.mutateAsync({ id: agentId }),});- if (agent.isPending) {- return (- <PageShell className="min-h-0" aria-busy="true">- <PageShellHeader>- <PageShellHeading>- <Skeleton className="h-8 w-72 max-w-full" />- <Skeleton className="h-4 w-[520px] max-w-full" />- </PageShellHeading>- </PageShellHeader>- <PageShellContent>- <Skeleton className="h-9 w-full" />- <Skeleton className="h-56 w-full" />- </PageShellContent>- <span role="status" className="sr-only">- Loading agent- </span>- </PageShell>- );- }-if (agent.isError) {return (<PageShell>@@ -194,7 +177,26 @@ export function TeamAgentDetail({);}- const data = agent.data;+ const data = agent.data ?? initialAgent;+ const isDraft = data.status === "DRAFT";+ const reviewManifest = recordOf(+ (+ data.reviewVersion as unknown as {+ manifest: unknown;+ } | null+ )?.manifest,+ );+ const displayedName = isDraft+ ? 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.");+ const displayedVersionNumber =+ data.currentVersion?.number ?? data.reviewVersion?.number;const nextRun = data.triggers.find((trigger) => trigger.enabled)?.nextRunAt;return (@@ -202,42 +204,55 @@ export function TeamAgentDetail({<PageShellHeader className="[&>div]:grid-cols-1 sm:[&>div]:grid-cols-[minmax(0,1fr)_auto]"><PageShellHeading><PageShellTitle className="wrap-break-word">- {data.name}+ {displayedName}</PageShellTitle><PageShellDescription className="wrap-break-word leading-6">- <span className="block">- {data.description ?? "A durable team automation."}- </span>+ <span className="block">{displayedDescription}</span><span className="mt-2 block text-xs">- Created by {data.createdBy.name} · Team agent · Version{" "}- {data.currentVersion?.number ?? "—"}+ Created by {data.createdBy.name} ·{" "}+ {isDraft ? "Private draft" : "Team agent"} · Version{" "}+ {displayedVersionNumber ?? "—"}</span></PageShellDescription></PageShellHeading><PageShellActions className="col-start-1 row-start-3 justify-self-start sm:col-start-2 sm:row-start-1 sm:justify-self-end"><div className="flex min-w-0 flex-col items-start gap-2 sm:items-end">- <span className="text-muted-foreground text-xs">Next run</span>+ <span className="text-muted-foreground text-xs">+ {isDraft ? "Visibility" : "Next run"}+ </span><span className="font-mono text-sm">- {nextRun ? formatDate(nextRun) : "Manual only"}+ {isDraft+ ? "Private draft"+ : nextRun+ ? formatDate(nextRun)+ : "Manual only"}</span><div className="mt-1 flex flex-wrap gap-2">- <Button- variant="outline"- disabled={data.status !== "LIVE" || runAction.pending}- aria-busy={runAction.pending}- onClick={() => runAction.run()}- >- <AsyncButtonContent- status={runAction.status}- pendingLabel="Queueing"- successLabel="Queued"- errorLabel="Try again"+ {isDraft && data.canManage ? (+ <DraftAgentActions+ agentId={data.id}+ name={displayedName}+ version={data.reviewVersion}+ />+ ) : (+ <Button+ variant="outline"+ disabled={data.status !== "LIVE" || runAction.pending}+ aria-busy={runAction.pending}+ onClick={() => runAction.run()}>- <Icon icon={Play} data-icon="inline-start" />- Run now- </AsyncButtonContent>- </Button>- {data.canManage && data.status === "LIVE" ? (+ <AsyncButtonContent+ status={runAction.status}+ pendingLabel="Queueing"+ successLabel="Queued"+ errorLabel="Try again"+ >+ <Icon icon={Play} data-icon="inline-start" />+ Run now+ </AsyncButtonContent>+ </Button>+ )}+ {!isDraft && data.canManage && data.status === "LIVE" ? (<Buttonvariant="outline"disabled={pauseAction.pending}@@ -255,7 +270,7 @@ export function TeamAgentDetail({</AsyncButtonContent></Button>) : null}- {data.canManage && data.status === "PAUSED" ? (+ {!isDraft && data.canManage && data.status === "PAUSED" ? (<Buttonvariant="outline"disabled={resumeAction.pending}@@ -273,7 +288,7 @@ export function TeamAgentDetail({</AsyncButtonContent></Button>) : null}- {data.canManage ? (+ {!isDraft && data.canManage ? (<DeleteAgentAction agentId={data.id} name={data.name} />) : null}</div>@@ -348,6 +363,87 @@ export function TeamAgentDetail({);}+function DraftAgentActions({+ agentId,+ name,+ version,+}: {+ agentId: string;+ name: string;+ version: ReviewVersion;+}) {+ const trpc = useTRPC();+ const queryClient = useQueryClient();+ const workspaceUrl = useWorkspaceUrl();+ const deployable =+ version?.status === "READY" || version?.status === "DEPLOYED";+ const deploy = useMutation(+ trpc.agents.deploy.mutationOptions({+ onSuccess: async () => {+ await Promise.all([+ queryClient.invalidateQueries({+ queryKey: trpc.agents.byId.pathKey(),+ }),+ queryClient.invalidateQueries({+ queryKey: trpc.agents.list.pathKey(),+ }),+ queryClient.invalidateQueries({+ queryKey: trpc.agents.activity.pathKey(),+ }),+ queryClient.invalidateQueries({+ queryKey: trpc.conversations.builderById.pathKey(),+ }),+ queryClient.invalidateQueries({+ queryKey: trpc.conversations.builderList.pathKey(),+ }),+ ]);+ toast.success("Agent deployed to the team.");+ },+ onError: (error) => toast.error(error.message),+ }),+ );+ const deployAction = useAsyncAction({+ action: async () => {+ if (!version || !deployable) return;+ await deploy.mutateAsync({+ id: agentId,+ versionId: version.id,+ clientRequestId: crypto.randomUUID(),+ });+ },+ });++ return (+ <>+ {version?.sourceConversationId ? (+ <Button variant="outline" asChild>+ <Link+ href={workspaceUrl(`/chat/${version.sourceConversationId}`)}+ transitionTypes={["nav-back"]}+ >+ Change details+ </Link>+ </Button>+ ) : null}+ <Button+ disabled={!deployable || deployAction.pending}+ aria-busy={deployAction.pending}+ onClick={() => deployAction.run()}+ >+ <AsyncButtonContent+ status={deployAction.status}+ pendingLabel="Deploying"+ successLabel="Deployed"+ errorLabel="Try again"+ >+ Deploy agent+ </AsyncButtonContent>+ </Button>+ <DeleteAgentAction agentId={agentId} name={name} />+ </>+ );+}+function DeleteAgentAction({agentId,name,@@ -479,18 +575,35 @@ function TabButton({}function AgentOverview({ agent }: { agent: AgentDetail }) {- const version = agent.currentVersion as {- manifest: unknown;- sandboxPolicy: unknown;- modelId: string;- } | null;- const manifest = recordOf(version?.manifest);- const sandbox = recordOf(version?.sandboxPolicy);+ const agentVersions = agent as unknown as {+ currentVersion: unknown;+ reviewVersion: unknown;+ };+ const version = recordOf(+ agentVersions.currentVersion ?? agentVersions.reviewVersion,+ );+ const manifest = recordOf(version.manifest);+ const sandbox = recordOf(version.sandboxPolicy);+ const trigger = recordOf(manifest.trigger);+ const actions = Array.isArray(manifest.actions)+ ? manifest.actions.map(recordOf)+ : [];+ const actionSummaries = actions+ .map((action) =>+ textOf(action.summary, textOf(action.type, "Configured action")),+ )+ .filter(Boolean);+ const access = Array.isArray(manifest.access)+ ? manifest.access.filter(+ (item): item is string =>+ typeof item === "string" && Boolean(item.trim()),+ )+ : [];return (<div className="overflow-hidden rounded-lg border bg-card"><DetailRow label="Status" value={agent.status} />- <DetailRow label="Model" value={version?.modelId ?? "—"} />+ <DetailRow label="Model" value={textOf(version.modelId, "—")} /><DetailRowlabel="Execution"value={textOf(@@ -509,9 +622,17 @@ function AgentOverview({ agent }: { agent: AgentDetail }) {label="Triggers"value={agent.triggers.map((trigger) => trigger.name).join(" · ") ||- "Manual only"+ textOf(trigger.summary, "Manual only")}/>+ <DetailRow+ label="Actions"+ value={actionSummaries.join(" · ") || "No external actions"}+ />+ <DetailRow+ label="Access"+ value={access.join(" · ") || "Bounded CRM read access"}+ /></div>);}