Skip to content

armgit / crm

Public Imported from GitHub
0

Comp AI CRM is an open source, CRM designed for AI agents. Agentic-first CRM.

TypeScript

fix(agent): declare granted write actions in draft access summary (#93)

github-actions[bot]ad4f9f31c81f
diff --git a/apps/agent/agent/subagents/agent_builder/lib/draft-input.ts b/apps/agent/agent/subagents/agent_builder/lib/draft-input.ts
index a25f2c43..e2d55ea5 100644
--- a/apps/agent/agent/subagents/agent_builder/lib/draft-input.ts
+++ b/apps/agent/agent/subagents/agent_builder/lib/draft-input.ts
@@ -45,6 +45,13 @@ export const builderDraftToolInput = z.object({
type BuilderDraftToolInput = z.infer<typeof builderDraftToolInput>;
+const ACTIVITY_ACCESS = {
+ NOTE: "Write notes on CRM records",
+ TASK: "Create tasks on CRM records",
+} as const;
+
+const ACTIVITY_ORDER = ["NOTE", "TASK"] as const;
+
const INTEGRATIONS = {
gmail: { kind: "integration", id: "google:gmail", label: "Gmail" },
calendar: {
@@ -59,6 +66,11 @@ export function draftInputFromTool(
): DraftAgentInput {
const { integrations: requestedIntegrations, ...draft } = input;
const integrations = [...new Set(requestedIntegrations)];
+ const activityTypes = new Set(
+ input.actions.flatMap((entry) =>
+ entry.type === "crm.activity.create" ? entry.activityTypes : [],
+ ),
+ );
const access = [
input.recordScope === "WORKSPACE"
? "Read workspace CRM records"
@@ -68,6 +80,9 @@ export function draftInputFromTool(
? "Read connected Gmail messages"
: "Read connected Google Calendar events",
),
+ ...ACTIVITY_ORDER.filter((type) => activityTypes.has(type)).map(
+ (type) => ACTIVITY_ACCESS[type],
+ ),
];
return {
diff --git a/apps/agent/test/custom-agent-runtime.spec.ts b/apps/agent/test/custom-agent-runtime.spec.ts
index fde444e8..c1e2d467 100644
--- a/apps/agent/test/custom-agent-runtime.spec.ts
+++ b/apps/agent/test/custom-agent-runtime.spec.ts
@@ -459,3 +459,95 @@ describe("agent builder draft input", () => {
expect(parsed.actions[0]?.summary).toBe("Write a reviewable renewal brief");
});
});
+
+describe("agent builder draft access", () => {
+ const draft = (actions: unknown[]) =>
+ builderDraftToolInput.parse({
+ name: "Handoff",
+ description: "Hand new customers to onboarding.",
+ instructions:
+ "Run on demand. Read closed-won deals and record the handoff for onboarding.",
+ trigger: {
+ type: "MANUAL",
+ name: "Run handoff",
+ summary: "Run for new customers",
+ },
+ recordScope: "WORKSPACE",
+ resources: [],
+ integrations: [],
+ actions,
+ });
+
+ it("declares the writes a draft was granted, not just its reads", () => {
+ expect(
+ draftInputFromTool(
+ draft([
+ {
+ type: "crm.activity.create",
+ provider: "crm",
+ summary: "Log the handoff brief",
+ activityTypes: ["NOTE", "TASK"],
+ },
+ ]),
+ ).access,
+ ).toEqual([
+ "Read workspace CRM records",
+ "Write notes on CRM records",
+ "Create tasks on CRM records",
+ ]);
+ });
+
+ it("only declares the activity types actually granted", () => {
+ expect(
+ draftInputFromTool(
+ draft([
+ {
+ type: "crm.activity.create",
+ provider: "crm",
+ summary: "Log the handoff brief",
+ activityTypes: ["NOTE"],
+ },
+ ]),
+ ).access,
+ ).toEqual(["Read workspace CRM records", "Write notes on CRM records"]);
+ });
+
+ it("keeps a read-only draft read-only", () => {
+ expect(
+ draftInputFromTool(
+ draft([
+ {
+ type: "run.summary",
+ provider: "crm",
+ summary: "Return a run summary",
+ },
+ ]),
+ ).access,
+ ).toEqual(["Read workspace CRM records"]);
+ });
+
+ it("does not repeat an activity type granted by two actions", () => {
+ expect(
+ draftInputFromTool(
+ draft([
+ {
+ type: "crm.activity.create",
+ provider: "crm",
+ summary: "Log the handoff brief",
+ activityTypes: ["NOTE"],
+ },
+ {
+ type: "crm.activity.create",
+ provider: "crm",
+ summary: "Log a follow-up",
+ activityTypes: ["NOTE", "TASK"],
+ },
+ ]),
+ ).access,
+ ).toEqual([
+ "Read workspace CRM records",
+ "Write notes on CRM records",
+ "Create tasks on CRM records",
+ ]);
+ });
+});