Expand data model with companies, approvals, costs, and heartbeats

Add new DB schemas: companies, agent_api_keys, approvals, cost_events,
heartbeat_runs, issue_comments. Add corresponding shared types and
validators. Update existing schemas (agents, goals, issues, projects)
with new fields for company association, budgets, and richer metadata.
Generate initial Drizzle migration. Update seed data.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Forgotten
2026-02-17 09:07:22 -06:00
parent fade29fc3e
commit 8c830eae70
41 changed files with 4464 additions and 121 deletions

View File

@@ -2,9 +2,13 @@ export const API_PREFIX = "/api";
export const API = {
health: `${API_PREFIX}/health`,
companies: `${API_PREFIX}/companies`,
agents: `${API_PREFIX}/agents`,
projects: `${API_PREFIX}/projects`,
issues: `${API_PREFIX}/issues`,
goals: `${API_PREFIX}/goals`,
approvals: `${API_PREFIX}/approvals`,
costs: `${API_PREFIX}/costs`,
activity: `${API_PREFIX}/activity`,
dashboard: `${API_PREFIX}/dashboard`,
} as const;

View File

@@ -1,12 +1,33 @@
export const AGENT_STATUSES = ["active", "idle", "offline", "error"] as const;
export const COMPANY_STATUSES = ["active", "paused", "archived"] as const;
export type CompanyStatus = (typeof COMPANY_STATUSES)[number];
export const AGENT_STATUSES = [
"active",
"paused",
"idle",
"running",
"error",
"terminated",
] as const;
export type AgentStatus = (typeof AGENT_STATUSES)[number];
export const AGENT_CONTEXT_MODES = ["thin", "fat"] as const;
export type AgentContextMode = (typeof AGENT_CONTEXT_MODES)[number];
export const AGENT_ADAPTER_TYPES = ["process", "http"] as const;
export type AgentAdapterType = (typeof AGENT_ADAPTER_TYPES)[number];
export const AGENT_ROLES = [
"ceo",
"cto",
"cmo",
"cfo",
"engineer",
"designer",
"pm",
"qa",
"devops",
"researcher",
"general",
] as const;
export type AgentRole = (typeof AGENT_ROLES)[number];
@@ -17,17 +38,44 @@ export const ISSUE_STATUSES = [
"in_progress",
"in_review",
"done",
"blocked",
"cancelled",
] as const;
export type IssueStatus = (typeof ISSUE_STATUSES)[number];
export const ISSUE_PRIORITIES = [
"critical",
"high",
"medium",
"low",
] as const;
export const ISSUE_PRIORITIES = ["critical", "high", "medium", "low"] as const;
export type IssuePriority = (typeof ISSUE_PRIORITIES)[number];
export const GOAL_LEVELS = ["company", "team", "agent", "task"] as const;
export type GoalLevel = (typeof GOAL_LEVELS)[number];
export const GOAL_STATUSES = ["planned", "active", "achieved", "cancelled"] as const;
export type GoalStatus = (typeof GOAL_STATUSES)[number];
export const PROJECT_STATUSES = [
"backlog",
"planned",
"in_progress",
"completed",
"cancelled",
] as const;
export type ProjectStatus = (typeof PROJECT_STATUSES)[number];
export const APPROVAL_TYPES = ["hire_agent", "approve_ceo_strategy"] as const;
export type ApprovalType = (typeof APPROVAL_TYPES)[number];
export const APPROVAL_STATUSES = ["pending", "approved", "rejected", "cancelled"] as const;
export type ApprovalStatus = (typeof APPROVAL_STATUSES)[number];
export const HEARTBEAT_INVOCATION_SOURCES = ["scheduler", "manual", "callback"] as const;
export type HeartbeatInvocationSource = (typeof HEARTBEAT_INVOCATION_SOURCES)[number];
export const HEARTBEAT_RUN_STATUSES = [
"queued",
"running",
"succeeded",
"failed",
"cancelled",
"timed_out",
] as const;
export type HeartbeatRunStatus = (typeof HEARTBEAT_RUN_STATUSES)[number];

View File

@@ -1,41 +1,85 @@
export {
COMPANY_STATUSES,
AGENT_STATUSES,
AGENT_CONTEXT_MODES,
AGENT_ADAPTER_TYPES,
AGENT_ROLES,
ISSUE_STATUSES,
ISSUE_PRIORITIES,
GOAL_LEVELS,
GOAL_STATUSES,
PROJECT_STATUSES,
APPROVAL_TYPES,
APPROVAL_STATUSES,
HEARTBEAT_INVOCATION_SOURCES,
HEARTBEAT_RUN_STATUSES,
type CompanyStatus,
type AgentStatus,
type AgentContextMode,
type AgentAdapterType,
type AgentRole,
type IssueStatus,
type IssuePriority,
type GoalLevel,
type GoalStatus,
type ProjectStatus,
type ApprovalType,
type ApprovalStatus,
type HeartbeatInvocationSource,
type HeartbeatRunStatus,
} from "./constants.js";
export type {
Company,
Agent,
AgentKeyCreated,
Project,
Issue,
IssueComment,
Goal,
Approval,
CostEvent,
CostSummary,
HeartbeatRun,
DashboardSummary,
ActivityEvent,
} from "./types/index.js";
export {
createCompanySchema,
updateCompanySchema,
type CreateCompany,
type UpdateCompany,
createAgentSchema,
updateAgentSchema,
createAgentKeySchema,
type CreateAgent,
type UpdateAgent,
type CreateAgentKey,
createProjectSchema,
updateProjectSchema,
type CreateProject,
type UpdateProject,
createIssueSchema,
updateIssueSchema,
checkoutIssueSchema,
addIssueCommentSchema,
type CreateIssue,
type UpdateIssue,
type CheckoutIssue,
type AddIssueComment,
createGoalSchema,
updateGoalSchema,
type CreateGoal,
type UpdateGoal,
createApprovalSchema,
resolveApprovalSchema,
type CreateApproval,
type ResolveApproval,
createCostEventSchema,
updateBudgetSchema,
type CreateCostEvent,
type UpdateBudget,
} from "./validators/index.js";
export { API_PREFIX, API } from "./api.js";

View File

@@ -1,5 +1,8 @@
export interface ActivityEvent {
id: string;
companyId: string;
actorType: "agent" | "user" | "system";
actorId: string;
action: string;
entityType: string;
entityId: string;

View File

@@ -1,15 +1,33 @@
import type { AgentRole, AgentStatus } from "../constants.js";
import type {
AgentAdapterType,
AgentContextMode,
AgentRole,
AgentStatus,
} from "../constants.js";
export interface Agent {
id: string;
companyId: string;
name: string;
role: AgentRole;
title: string | null;
status: AgentStatus;
budgetCents: number;
spentCents: number;
lastHeartbeat: Date | null;
reportsTo: string | null;
capabilities: string | null;
adapterType: AgentAdapterType;
adapterConfig: Record<string, unknown>;
contextMode: AgentContextMode;
budgetMonthlyCents: number;
spentMonthlyCents: number;
lastHeartbeatAt: Date | null;
metadata: Record<string, unknown> | null;
createdAt: Date;
updatedAt: Date;
}
export interface AgentKeyCreated {
id: string;
name: string;
token: string;
createdAt: Date;
}

View File

@@ -0,0 +1,16 @@
import type { ApprovalStatus, ApprovalType } from "../constants.js";
export interface Approval {
id: string;
companyId: string;
type: ApprovalType;
requestedByAgentId: string | null;
requestedByUserId: string | null;
status: ApprovalStatus;
payload: Record<string, unknown>;
decisionNote: string | null;
decidedByUserId: string | null;
decidedAt: Date | null;
createdAt: Date;
updatedAt: Date;
}

View File

@@ -0,0 +1,12 @@
import type { CompanyStatus } from "../constants.js";
export interface Company {
id: string;
name: string;
description: string | null;
status: CompanyStatus;
budgetMonthlyCents: number;
spentMonthlyCents: number;
createdAt: Date;
updatedAt: Date;
}

View File

@@ -0,0 +1,23 @@
export interface CostEvent {
id: string;
companyId: string;
agentId: string;
issueId: string | null;
projectId: string | null;
goalId: string | null;
billingCode: string | null;
provider: string;
model: string;
inputTokens: number;
outputTokens: number;
costCents: number;
occurredAt: Date;
createdAt: Date;
}
export interface CostSummary {
companyId: string;
monthSpendCents: number;
monthBudgetCents: number;
monthUtilizationPercent: number;
}

View File

@@ -0,0 +1,22 @@
export interface DashboardSummary {
companyId: string;
agents: {
active: number;
running: number;
paused: number;
error: number;
};
tasks: {
open: number;
inProgress: number;
blocked: number;
done: number;
};
costs: {
monthSpendCents: number;
monthBudgetCents: number;
monthUtilizationPercent: number;
};
pendingApprovals: number;
staleTasks: number;
}

View File

@@ -1,12 +1,14 @@
import type { GoalLevel } from "../constants.js";
import type { GoalLevel, GoalStatus } from "../constants.js";
export interface Goal {
id: string;
companyId: string;
title: string;
description: string | null;
level: GoalLevel;
status: GoalStatus;
parentId: string | null;
ownerId: string | null;
ownerAgentId: string | null;
createdAt: Date;
updatedAt: Date;
}

View File

@@ -0,0 +1,19 @@
import type {
HeartbeatInvocationSource,
HeartbeatRunStatus,
} from "../constants.js";
export interface HeartbeatRun {
id: string;
companyId: string;
agentId: string;
invocationSource: HeartbeatInvocationSource;
status: HeartbeatRunStatus;
startedAt: Date | null;
finishedAt: Date | null;
error: string | null;
externalRunId: string | null;
contextSnapshot: Record<string, unknown> | null;
createdAt: Date;
updatedAt: Date;
}

View File

@@ -1,5 +1,10 @@
export type { Agent } from "./agent.js";
export type { Company } from "./company.js";
export type { Agent, AgentKeyCreated } from "./agent.js";
export type { Project } from "./project.js";
export type { Issue } from "./issue.js";
export type { Issue, IssueComment } from "./issue.js";
export type { Goal } from "./goal.js";
export type { Approval } from "./approval.js";
export type { CostEvent, CostSummary } from "./cost.js";
export type { HeartbeatRun } from "./heartbeat.js";
export type { DashboardSummary } from "./dashboard.js";
export type { ActivityEvent } from "./activity.js";

View File

@@ -2,13 +2,33 @@ import type { IssuePriority, IssueStatus } from "../constants.js";
export interface Issue {
id: string;
companyId: string;
projectId: string | null;
goalId: string | null;
parentId: string | null;
title: string;
description: string | null;
status: IssueStatus;
priority: IssuePriority;
projectId: string | null;
assigneeId: string | null;
goalId: string | null;
assigneeAgentId: string | null;
createdByAgentId: string | null;
createdByUserId: string | null;
requestDepth: number;
billingCode: string | null;
startedAt: Date | null;
completedAt: Date | null;
cancelledAt: Date | null;
createdAt: Date;
updatedAt: Date;
}
export interface IssueComment {
id: string;
companyId: string;
issueId: string;
authorAgentId: string | null;
authorUserId: string | null;
body: string;
createdAt: Date;
updatedAt: Date;
}

View File

@@ -1,7 +1,14 @@
import type { ProjectStatus } from "../constants.js";
export interface Project {
id: string;
companyId: string;
goalId: string | null;
name: string;
description: string | null;
status: ProjectStatus;
leadAgentId: string | null;
targetDate: string | null;
createdAt: Date;
updatedAt: Date;
}

View File

@@ -1,11 +1,21 @@
import { z } from "zod";
import { AGENT_ROLES, AGENT_STATUSES } from "../constants.js";
import {
AGENT_ADAPTER_TYPES,
AGENT_CONTEXT_MODES,
AGENT_ROLES,
AGENT_STATUSES,
} from "../constants.js";
export const createAgentSchema = z.object({
name: z.string().min(1),
role: z.enum(AGENT_ROLES),
budgetCents: z.number().int().nonnegative().optional().default(0),
role: z.enum(AGENT_ROLES).optional().default("general"),
title: z.string().optional().nullable(),
reportsTo: z.string().uuid().optional().nullable(),
capabilities: z.string().optional().nullable(),
adapterType: z.enum(AGENT_ADAPTER_TYPES).optional().default("process"),
adapterConfig: z.record(z.unknown()).optional().default({}),
contextMode: z.enum(AGENT_CONTEXT_MODES).optional().default("thin"),
budgetMonthlyCents: z.number().int().nonnegative().optional().default(0),
metadata: z.record(z.unknown()).optional().nullable(),
});
@@ -15,6 +25,13 @@ export const updateAgentSchema = createAgentSchema
.partial()
.extend({
status: z.enum(AGENT_STATUSES).optional(),
spentMonthlyCents: z.number().int().nonnegative().optional(),
});
export type UpdateAgent = z.infer<typeof updateAgentSchema>;
export const createAgentKeySchema = z.object({
name: z.string().min(1).default("default"),
});
export type CreateAgentKey = z.infer<typeof createAgentKeySchema>;

View File

@@ -0,0 +1,17 @@
import { z } from "zod";
import { APPROVAL_TYPES } from "../constants.js";
export const createApprovalSchema = z.object({
type: z.enum(APPROVAL_TYPES),
requestedByAgentId: z.string().uuid().optional().nullable(),
payload: z.record(z.unknown()),
});
export type CreateApproval = z.infer<typeof createApprovalSchema>;
export const resolveApprovalSchema = z.object({
decisionNote: z.string().optional().nullable(),
decidedByUserId: z.string().optional().default("board"),
});
export type ResolveApproval = z.infer<typeof resolveApprovalSchema>;

View File

@@ -0,0 +1,19 @@
import { z } from "zod";
import { COMPANY_STATUSES } from "../constants.js";
export const createCompanySchema = z.object({
name: z.string().min(1),
description: z.string().optional().nullable(),
budgetMonthlyCents: z.number().int().nonnegative().optional().default(0),
});
export type CreateCompany = z.infer<typeof createCompanySchema>;
export const updateCompanySchema = createCompanySchema
.partial()
.extend({
status: z.enum(COMPANY_STATUSES).optional(),
spentMonthlyCents: z.number().int().nonnegative().optional(),
});
export type UpdateCompany = z.infer<typeof updateCompanySchema>;

View File

@@ -0,0 +1,23 @@
import { z } from "zod";
export const createCostEventSchema = z.object({
agentId: z.string().uuid(),
issueId: z.string().uuid().optional().nullable(),
projectId: z.string().uuid().optional().nullable(),
goalId: z.string().uuid().optional().nullable(),
billingCode: z.string().optional().nullable(),
provider: z.string().min(1),
model: z.string().min(1),
inputTokens: z.number().int().nonnegative().optional().default(0),
outputTokens: z.number().int().nonnegative().optional().default(0),
costCents: z.number().int().nonnegative(),
occurredAt: z.string().datetime(),
});
export type CreateCostEvent = z.infer<typeof createCostEventSchema>;
export const updateBudgetSchema = z.object({
budgetMonthlyCents: z.number().int().nonnegative(),
});
export type UpdateBudget = z.infer<typeof updateBudgetSchema>;

View File

@@ -1,12 +1,13 @@
import { z } from "zod";
import { GOAL_LEVELS } from "../constants.js";
import { GOAL_LEVELS, GOAL_STATUSES } from "../constants.js";
export const createGoalSchema = z.object({
title: z.string().min(1),
description: z.string().optional().nullable(),
level: z.enum(GOAL_LEVELS),
level: z.enum(GOAL_LEVELS).optional().default("task"),
status: z.enum(GOAL_STATUSES).optional().default("planned"),
parentId: z.string().uuid().optional().nullable(),
ownerId: z.string().uuid().optional().nullable(),
ownerAgentId: z.string().uuid().optional().nullable(),
});
export type CreateGoal = z.infer<typeof createGoalSchema>;

View File

@@ -1,8 +1,17 @@
export {
createCompanySchema,
updateCompanySchema,
type CreateCompany,
type UpdateCompany,
} from "./company.js";
export {
createAgentSchema,
updateAgentSchema,
createAgentKeySchema,
type CreateAgent,
type UpdateAgent,
type CreateAgentKey,
} from "./agent.js";
export {
@@ -15,8 +24,12 @@ export {
export {
createIssueSchema,
updateIssueSchema,
checkoutIssueSchema,
addIssueCommentSchema,
type CreateIssue,
type UpdateIssue,
type CheckoutIssue,
type AddIssueComment,
} from "./issue.js";
export {
@@ -25,3 +38,17 @@ export {
type CreateGoal,
type UpdateGoal,
} from "./goal.js";
export {
createApprovalSchema,
resolveApprovalSchema,
type CreateApproval,
type ResolveApproval,
} from "./approval.js";
export {
createCostEventSchema,
updateBudgetSchema,
type CreateCostEvent,
type UpdateBudget,
} from "./cost.js";

View File

@@ -2,13 +2,16 @@ import { z } from "zod";
import { ISSUE_PRIORITIES, ISSUE_STATUSES } from "../constants.js";
export const createIssueSchema = z.object({
projectId: z.string().uuid().optional().nullable(),
goalId: z.string().uuid().optional().nullable(),
parentId: z.string().uuid().optional().nullable(),
title: z.string().min(1),
description: z.string().optional().nullable(),
status: z.enum(ISSUE_STATUSES).optional().default("backlog"),
priority: z.enum(ISSUE_PRIORITIES).optional().default("medium"),
projectId: z.string().uuid().optional().nullable(),
assigneeId: z.string().uuid().optional().nullable(),
goalId: z.string().uuid().optional().nullable(),
assigneeAgentId: z.string().uuid().optional().nullable(),
requestDepth: z.number().int().nonnegative().optional().default(0),
billingCode: z.string().optional().nullable(),
});
export type CreateIssue = z.infer<typeof createIssueSchema>;
@@ -16,3 +19,16 @@ export type CreateIssue = z.infer<typeof createIssueSchema>;
export const updateIssueSchema = createIssueSchema.partial();
export type UpdateIssue = z.infer<typeof updateIssueSchema>;
export const checkoutIssueSchema = z.object({
agentId: z.string().uuid(),
expectedStatuses: z.array(z.enum(ISSUE_STATUSES)).nonempty(),
});
export type CheckoutIssue = z.infer<typeof checkoutIssueSchema>;
export const addIssueCommentSchema = z.object({
body: z.string().min(1),
});
export type AddIssueComment = z.infer<typeof addIssueCommentSchema>;

View File

@@ -1,8 +1,13 @@
import { z } from "zod";
import { PROJECT_STATUSES } from "../constants.js";
export const createProjectSchema = z.object({
goalId: z.string().uuid().optional().nullable(),
name: z.string().min(1),
description: z.string().optional().nullable(),
status: z.enum(PROJECT_STATUSES).optional().default("backlog"),
leadAgentId: z.string().uuid().optional().nullable(),
targetDate: z.string().optional().nullable(),
});
export type CreateProject = z.infer<typeof createProjectSchema>;