Skip to content

The open-source app everyone uses to manage agents at work

1.9 KB
Raw Blame
import { resourceLifecycleEvents, type Db } from "@paperclipai/db";
import { sql } from "drizzle-orm";
import { DELIVERY_QUEUES, notifyDeliveryWork } from "./delivery-work-notifications.js";

/** Call inside the transaction that creates the resource or approves the hire. */
export async function recordResourceCreationEvent(
  db: Db,
  companyId: string,
  resourceType: "agent" | "project",
  resourceId: string,
): Promise<void> {
  await notifyDeliveryWork(db, DELIVERY_QUEUES.pluginLifecycle);
  await db.insert(resourceLifecycleEvents).values({ companyId, resourceType, resourceId, action: "create" }).onConflictDoNothing({
    target: [resourceLifecycleEvents.companyId, resourceLifecycleEvents.resourceType, resourceLifecycleEvents.resourceId],
    where: sql`${resourceLifecycleEvents.action} = 'create'`,
  });
}

/** Call with the agent row locked, in the same transaction as the status change. */
export async function recordAgentStatusEvent(db: Db, companyId: string, agentId: string, before: string, after: string): Promise<void> {
  if (before === after) return;
  const action = after === "terminated" ? "terminate"
    : after === "paused" ? "pause"
      : before === "paused" && ["active", "idle", "running", "error"].includes(after) ? "resume" : null;
  if (!action) return;
  await notifyDeliveryWork(db, DELIVERY_QUEUES.pluginLifecycle);
  await db.insert(resourceLifecycleEvents).values({ companyId, resourceType: "agent", resourceId: agentId, action });
}

/** Call while holding the project row lock, in the mutation transaction. */
export async function recordProjectLifecycleEvent(db: Db, companyId: string, projectId: string, action: "update" | "archive" | "delete" = "update"): Promise<void> {
  await notifyDeliveryWork(db, DELIVERY_QUEUES.pluginLifecycle);
  await db.insert(resourceLifecycleEvents).values({ companyId, resourceType: "project", resourceId: projectId, action });
}