Multi-Agent Collaboration for Team and Friends
TypeScript agent-automationagent-orchestrationagent-workflowai-agentsai-collaborationai-memoryai-workforceautonomous-agentsclaude-codeclaude-code-skillscodexcoding-agentmulti-agentmultiple-agentone-person-businessone-person-companyone-person-teamopencodesolopreneur
feat(shared): agent, runtime queries
Gener64635176b3d8
diff --git a/src/shared/src/db/queries/agent.ts b/src/shared/src/db/queries/agent.tsnew file mode 100644index 000000000..a2fda58ca--- /dev/null+++ b/src/shared/src/db/queries/agent.ts@@ -0,0 +1,124 @@+import { eq, and, desc } from "drizzle-orm";+import { agent, agentTaskQueue } from "../schema";+import type { Database } from "../index";++export async function getAgent(db: Database, id: string) {+ const rows = await db.select().from(agent).where(eq(agent.id, id));+ return rows[0] ?? null;+}++export async function getAgentInWorkspace(+ db: Database,+ id: string,+ workspaceId: string+) {+ const rows = await db+ .select()+ .from(agent)+ .where(and(eq(agent.id, id), eq(agent.workspaceId, workspaceId)));+ return rows[0] ?? null;+}++export async function listAgents(db: Database, workspaceId: string) {+ return db+ .select()+ .from(agent)+ .where(eq(agent.workspaceId, workspaceId))+ .orderBy(desc(agent.createdAt));+}++export async function createAgent(+ db: Database,+ data: {+ workspaceId: string;+ name: string;+ description?: string;+ instructions?: string;+ avatarUrl?: string | null;+ runtimeId?: string | null;+ runtimeMode?: string;+ runtimeConfig?: unknown;+ visibility?: string;+ maxConcurrentTasks?: number;+ ownerId?: string | null;+ tools?: unknown;+ triggers?: unknown;+ }+) {+ const rows = await db+ .insert(agent)+ .values({+ workspaceId: data.workspaceId,+ name: data.name,+ description: data.description ?? "",+ instructions: data.instructions ?? "",+ avatarUrl: data.avatarUrl ?? null,+ runtimeId: data.runtimeId ?? null,+ runtimeMode: data.runtimeMode ?? "local",+ runtimeConfig: data.runtimeConfig ?? null,+ visibility: data.visibility ?? "private",+ maxConcurrentTasks: data.maxConcurrentTasks ?? 6,+ ownerId: data.ownerId ?? null,+ tools: data.tools ?? null,+ triggers: data.triggers ?? null,+ })+ .returning();+ return rows[0]!;+}++export async function deleteAgent(+ db: Database,+ id: string,+ workspaceId: string+) {+ return db.transaction(async (tx) => {+ await tx+ .delete(agentTaskQueue)+ .where(eq(agentTaskQueue.agentId, id));+ const rows = await tx+ .delete(agent)+ .where(and(eq(agent.id, id), eq(agent.workspaceId, workspaceId)))+ .returning();+ return rows[0] ?? null;+ });+}++export async function updateAgent(+ db: Database,+ id: string,+ workspaceId: string,+ data: {+ name?: string;+ description?: string;+ instructions?: string;+ runtimeId?: string | null;+ }+) {+ const rows = await db+ .update(agent)+ .set({ ...data, updatedAt: new Date().toISOString() })+ .where(and(eq(agent.id, id), eq(agent.workspaceId, workspaceId)))+ .returning();+ return rows[0] ?? null;+}++export async function updateAgentStatus(+ db: Database,+ id: string,+ status: string+) {+ const rows = await db+ .update(agent)+ .set({ status, updatedAt: new Date().toISOString() })+ .where(eq(agent.id, id))+ .returning();+ return rows[0] ?? null;+}++export async function getAgentByHandle(db: Database, emailHandle: string) {+ const rows = await db+ .select()+ .from(agent)+ .where(eq(agent.emailHandle, emailHandle));+ return rows[0] ?? null;+}diff --git a/src/shared/src/db/queries/runtime.ts b/src/shared/src/db/queries/runtime.tsnew file mode 100644index 000000000..92577168a--- /dev/null+++ b/src/shared/src/db/queries/runtime.ts@@ -0,0 +1,160 @@+import { eq, and, asc, or, lt, isNull } from "drizzle-orm";+import { agentRuntime, agent, agentTaskQueue } from "../schema";+import type { Database } from "../index";++export async function upsertAgentRuntime(+ db: Database,+ data: {+ workspaceId: string;+ daemonId: string;+ name: string;+ runtimeMode: string;+ provider: string;+ status: string;+ deviceInfo: string;+ metadata?: unknown;+ }+) {+ const now = new Date().toISOString();+ const rows = await db+ .insert(agentRuntime)+ .values({+ workspaceId: data.workspaceId,+ daemonId: data.daemonId,+ name: data.name,+ runtimeMode: data.runtimeMode,+ provider: data.provider,+ status: data.status,+ deviceInfo: data.deviceInfo,+ metadata: data.metadata ?? null,+ lastSeenAt: now,+ })+ .onConflictDoUpdate({+ target: [+ agentRuntime.workspaceId,+ agentRuntime.daemonId,+ agentRuntime.provider,+ ],+ set: {+ name: data.name,+ runtimeMode: data.runtimeMode,+ status: data.status,+ deviceInfo: data.deviceInfo,+ metadata: data.metadata ?? null,+ lastSeenAt: now,+ updatedAt: now,+ },+ })+ .returning();+ return rows[0]!;+}++export async function listAgentRuntimes(db: Database, workspaceId: string) {+ return db+ .select()+ .from(agentRuntime)+ .where(eq(agentRuntime.workspaceId, workspaceId))+ .orderBy(asc(agentRuntime.createdAt));+}++export async function getAgentRuntime(db: Database, id: string) {+ const rows = await db+ .select()+ .from(agentRuntime)+ .where(eq(agentRuntime.id, id));+ return rows[0] ?? null;+}++export async function getAgentRuntimeForWorkspace(+ db: Database,+ id: string,+ workspaceId: string+) {+ const rows = await db+ .select()+ .from(agentRuntime)+ .where(+ and(eq(agentRuntime.id, id), eq(agentRuntime.workspaceId, workspaceId))+ );+ return rows[0] ?? null;+}++export async function updateAgentRuntimeHeartbeat(db: Database, id: string) {+ const now = new Date().toISOString();+ const rows = await db+ .update(agentRuntime)+ .set({ lastSeenAt: now, status: "online", updatedAt: now })+ .where(eq(agentRuntime.id, id))+ .returning();+ return rows[0] ?? null;+}++export async function setAgentRuntimeOffline(db: Database, id: string) {+ await db+ .update(agentRuntime)+ .set({ status: "offline", updatedAt: new Date().toISOString() })+ .where(eq(agentRuntime.id, id));+}++export async function deleteRuntimesByDaemonId(+ db: Database,+ daemonId: string,+ workspaceId: string+) {+ // Find runtime IDs to delete+ const runtimes = await db+ .select({ id: agentRuntime.id })+ .from(agentRuntime)+ .where(+ and(+ eq(agentRuntime.daemonId, daemonId),+ eq(agentRuntime.workspaceId, workspaceId)+ )+ );++ if (runtimes.length === 0) return;++ const ids = runtimes.map((r) => r.id);++ // Null out agent references and delete tasks per runtime+ for (const id of ids) {+ await db+ .update(agent)+ .set({ runtimeId: null, updatedAt: new Date().toISOString() })+ .where(eq(agent.runtimeId, id));++ await db+ .delete(agentTaskQueue)+ .where(eq(agentTaskQueue.runtimeId, id));+ }++ // Delete the runtimes+ await db+ .delete(agentRuntime)+ .where(+ and(+ eq(agentRuntime.daemonId, daemonId),+ eq(agentRuntime.workspaceId, workspaceId)+ )+ );+}++export async function markStaleRuntimesOffline(+ db: Database,+ workspaceId: string+) {+ const threshold = new Date(Date.now() - 45 * 1000).toISOString();+ await db+ .update(agentRuntime)+ .set({ status: "offline", updatedAt: new Date().toISOString() })+ .where(+ and(+ eq(agentRuntime.workspaceId, workspaceId),+ eq(agentRuntime.status, "online"),+ or(+ lt(agentRuntime.lastSeenAt, threshold),+ isNull(agentRuntime.lastSeenAt)+ )+ )+ );+}