The open-source app everyone uses to manage agents at work
TypeScript acpxagent-orchestrationagent-skillsagentic-aiai-agentsai-engineeringai-governanceai-toolsautonomous-agentsclaudeclaude-codeclaude-skillscodexhermesllmmcpmulti-agentopenclawproject-managementworkflow-automation
import { sql } from "drizzle-orm";
import { check, pgTable, uuid, text, timestamp, integer, jsonb, index, uniqueIndex, foreignKey } from "drizzle-orm/pg-core";
import { companies } from "./companies.js";
import { projects } from "./projects.js";
import { agents } from "./agents.js";
import { companySecretProviderConfigs } from "./company_secret_provider_configs.js";
import { userSecretDefinitions } from "./user_secret_definitions.js";
import { plugins } from "./plugins.js";
export const companySecrets = pgTable(
"company_secrets",
{
id: uuid("id").primaryKey().defaultRandom(),
companyId: uuid("company_id").notNull().references(() => companies.id),
scope: text("scope").notNull().default("company"),
ownerUserId: text("owner_user_id"),
ownerAgentId: uuid("owner_agent_id"),
ownerProjectId: uuid("owner_project_id"),
ownerPluginId: uuid("owner_plugin_id").references(() => plugins.id, { onDelete: "cascade" }),
userSecretDefinitionId: uuid("user_secret_definition_id").references(() => userSecretDefinitions.id, { onDelete: "set null" }),
key: text("key").notNull(),
name: text("name").notNull(),
provider: text("provider").notNull().default("local_encrypted"),
status: text("status").notNull().default("active"),
managedMode: text("managed_mode").notNull().default("paperclip_managed"),
externalRef: text("external_ref"),
providerConfigId: uuid("provider_config_id").references(() => companySecretProviderConfigs.id, { onDelete: "set null" }),
providerMetadata: jsonb("provider_metadata").$type<Record<string, unknown>>(),
aiSessionEpoch: integer("ai_session_epoch").notNull().default(0),
latestVersion: integer("latest_version").notNull().default(1),
description: text("description"),
lastResolvedAt: timestamp("last_resolved_at", { withTimezone: true }),
lastRotatedAt: timestamp("last_rotated_at", { withTimezone: true }),
deletedAt: timestamp("deleted_at", { withTimezone: true }),
createdByAgentId: uuid("created_by_agent_id").references(() => agents.id, { onDelete: "set null" }),
createdByUserId: text("created_by_user_id"),
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow(),
},
(table) => ({
companyIdx: index("company_secrets_company_idx").on(table.companyId),
companyScopeIdx: index("company_secrets_company_scope_idx").on(table.companyId, table.scope),
companyOwnerIdx: index("company_secrets_company_owner_idx").on(table.companyId, table.ownerUserId),
userDefinitionOwnerIdx: index("company_secrets_user_definition_owner_idx").on(
table.companyId,
table.userSecretDefinitionId,
table.ownerUserId,
),
companyProviderIdx: index("company_secrets_company_provider_idx").on(table.companyId, table.provider),
agentOwner: foreignKey({ columns: [table.companyId, table.ownerAgentId], foreignColumns: [agents.companyId, agents.id] }).onDelete("cascade"),
projectOwner: foreignKey({ columns: [table.companyId, table.ownerProjectId], foreignColumns: [projects.companyId, projects.id] }).onDelete("cascade"),
projectInstanceOwnerKeyUq: uniqueIndex("company_secrets_project_instance_owner_key_uq").on(table.companyId, table.ownerProjectId, table.ownerPluginId, table.key)
.where(sql`${table.scope} = 'instance' and ${table.deletedAt} is null`),
instanceOwnerKeyUq: uniqueIndex("company_secrets_instance_owner_key_uq").on(table.companyId, table.ownerAgentId, table.ownerPluginId, table.key)
.where(sql`${table.scope} = 'instance' and ${table.deletedAt} is null`),
providerConfigIdx: index("company_secrets_provider_config_idx").on(table.providerConfigId),
companyNameUq: uniqueIndex("company_secrets_company_name_uq")
.on(table.companyId, table.name)
.where(sql`${table.scope} = 'company' and ${table.deletedAt} is null`),
companyKeyUq: uniqueIndex("company_secrets_company_key_uq")
.on(table.companyId, table.key)
.where(sql`${table.scope} = 'company' and ${table.deletedAt} is null`),
userDefinitionOwnerUq: uniqueIndex("company_secrets_user_definition_owner_uq")
.on(table.companyId, table.userSecretDefinitionId, table.ownerUserId)
.where(sql`${table.scope} = 'user' and ${table.deletedAt} is null`),
scopeShapeCheck: check(
"company_secrets_scope_shape_check",
sql`(
${table.scope} = 'company'
and ${table.ownerUserId} is null
and ${table.userSecretDefinitionId} is null
and ${table.ownerAgentId} is null and ${table.ownerProjectId} is null and ${table.ownerPluginId} is null
) or (
${table.scope} = 'user'
and ${table.ownerUserId} is not null
and ${table.userSecretDefinitionId} is not null
and ${table.ownerAgentId} is null and ${table.ownerProjectId} is null and ${table.ownerPluginId} is null
) or (
${table.scope} = 'instance'
and ${table.ownerUserId} is null and ${table.userSecretDefinitionId} is null
and (${table.ownerAgentId} is not null) <> (${table.ownerProjectId} is not null)
and ${table.ownerPluginId} is not null
)`,
),
}),
);