Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

24.0 KB
Raw Blame
import { sql } from 'drizzle-orm';
import {
  bigint,
  boolean,
  foreignKey,
  index,
  integer,
  jsonb,
  pgTable,
  text,
  unique,
  uniqueIndex,
  uuid,
} from 'drizzle-orm/pg-core';

import {
  type QuotaAccountCredentialRef,
  QuotaAccountStatus,
  QuotaBindingRole,
  QuotaCredentialMode,
} from '../types/agentQuota';
import { amountNumeric, createdAt, timestamps, timestamptz, varchar255 } from './_helpers';
import { agents } from './agent';
import { agentOperations } from './agentOperations';
import { devices } from './device';
import { messages } from './message';
import { topics } from './topic';
import { users } from './user';
import { workspaces } from './workspace';

// ─────────────────────────────────────────────────────────────────────────────
// agent_provider_accounts β€” identity + credential vault (the pivot entity)
// ─────────────────────────────────────────────────────────────────────────────

/**
 * One row per external provider account (a Claude Code / Codex subscription).
 * This is simultaneously the identity, the credential holder, and the partition
 * key every quota-data table hangs off. Modeled on `user_connectors`: encrypted
 * `credentials`, `tokenExpiresAt` promoted for refresh indexing, layered
 * user / workspace scope.
 */
export const agentProviderAccounts = pgTable(
  'agent_provider_accounts',
  {
    id: uuid('id').defaultRandom().primaryKey(),

    userId: text('user_id')
      .references(() => users.id, { onDelete: 'cascade' })
      .notNull(),
    workspaceId: text('workspace_id').references(() => workspaces.id, { onDelete: 'cascade' }),

    // Visibility WITHIN a workspace (same contract as devices / agents):
    //   - 'public'  β†’ every member sees the subscription (without its identity
    //     fields) and agents can draw on it: a team account on a shared machine.
    //   - 'private' β†’ only its creator sees and uses it. Logins do not grant
    //     access: the server cannot verify a reported login, so sharing is the
    //     creator's call (publish).
    //   Ignored for personal rows (`workspace_id IS NULL`).
    // Ingestion creates workspace rows as 'private' (`upsertByIdentity`, which
    // dedupes by identity across visibility); the column default only covers
    // rows added by hand and the ones that pre-date that change.
    visibility: text('visibility', { enum: ['private', 'public'] })
      .default('public')
      .notNull(),

    provider: text('provider').notNull(),

    // ── Identity (populated from provider once observed) ──────────────────────
    /** Anthropic `oauthAccount.accountUuid` / Codex `tokens.account_id`. */
    externalAccountId: text('external_account_id'),
    email: text('email'),
    displayName: text('display_name'),
    organizationId: text('organization_id'),
    /** e.g. `max` / `pro`. */
    planTier: text('plan_tier'),
    /** e.g. `default_claude_max_20x`. */
    rateLimitTier: text('rate_limit_tier'),

    /** User-nameable label shown in the switcher. */
    label: varchar255('label'),

    // ── Credential ────────────────────────────────────────────────────────────
    credentialMode: text('credential_mode').notNull().default(QuotaCredentialMode.referenced),
    /** Encrypted `QuotaAccountCredentials` (managed mode only). */
    credentials: text('credentials'),
    /** Plaintext `QuotaAccountCredentialRef` (referenced mode only). */
    credentialRef: jsonb('credential_ref').$type<QuotaAccountCredentialRef>(),
    /** Promoted out of the encrypted blob so the refresh worker can index it. */
    tokenExpiresAt: timestamptz('token_expires_at'),

    status: text('status').notNull().default(QuotaAccountStatus.active),
    enabled: boolean('enabled').notNull().default(true),
    lastValidatedAt: timestamptz('last_validated_at'),

    metadata: jsonb('metadata').$type<Record<string, unknown>>(),

    ...timestamps,
  },
  (t) => [
    index('agent_provider_accounts_user_id_idx').on(t.userId),
    index('agent_provider_accounts_workspace_id_idx').on(t.workspaceId),
    index('agent_provider_accounts_workspace_visibility_idx').on(
      t.workspaceId,
      t.visibility,
      t.userId,
    ),
    index('agent_provider_accounts_token_expires_at_idx').on(t.tokenExpiresAt),
    /** One row per real external account per user (personal mode). */
    uniqueIndex('agent_provider_accounts_identity_unique')
      .on(t.userId, t.provider, t.externalAccountId)
      .where(sql`${t.externalAccountId} IS NOT NULL and ${t.workspaceId} is null`),
    /**
     * One row per real external account per workspace β€” deliberately NOT keyed on
     * userId: whoever on the team observes the account first owns the row, and a
     * teammate seeing the same login updates it instead of adding a second copy.
     * Two rows for one account would make the load balancer count its capacity twice.
     */
    uniqueIndex('agent_provider_accounts_identity_workspace_unique')
      .on(t.provider, t.externalAccountId, t.workspaceId)
      .where(sql`${t.externalAccountId} IS NOT NULL and ${t.workspaceId} is not null`),
  ],
);

export type NewAgentProviderAccount = typeof agentProviderAccounts.$inferInsert;
export type AgentProviderAccountItem = typeof agentProviderAccounts.$inferSelect;

// ─────────────────────────────────────────────────────────────────────────────
// agent_account_bindings β€” agent β†’ account pool (enables LB + UI switching)
// ─────────────────────────────────────────────────────────────────────────────

export const agentAccountBindings = pgTable(
  'agent_account_bindings',
  {
    id: uuid('id').defaultRandom().primaryKey(),

    agentId: text('agent_id')
      .references(() => agents.id, { onDelete: 'cascade' })
      .notNull(),
    accountId: uuid('account_id')
      .references(() => agentProviderAccounts.id, { onDelete: 'cascade' })
      .notNull(),

    userId: text('user_id')
      .references(() => users.id, { onDelete: 'cascade' })
      .notNull(),
    workspaceId: text('workspace_id').references(() => workspaces.id, { onDelete: 'cascade' }),

    role: text('role').notNull().default(QuotaBindingRole.pool),
    /** Lower = preferred when ordering the pool. */
    priority: integer('priority').notNull().default(0),
    /** Weighted round-robin weight within the pool. */
    weight: integer('weight').notNull().default(1),
    enabled: boolean('enabled').notNull().default(true),

    ...timestamps,
  },
  (t) => [
    uniqueIndex('agent_account_bindings_agent_account_unique').on(t.agentId, t.accountId),
    /** At most one pinned account per agent. */
    uniqueIndex('agent_account_bindings_agent_pinned_unique')
      .on(t.agentId)
      .where(sql`${t.role} = 'pinned'`),
    index('agent_account_bindings_agent_id_idx').on(t.agentId),
    index('agent_account_bindings_account_id_idx').on(t.accountId),
    index('agent_account_bindings_user_id_idx').on(t.userId),
    index('agent_account_bindings_workspace_id_idx').on(t.workspaceId),
  ],
);

export type NewAgentAccountBinding = typeof agentAccountBindings.$inferInsert;
export type AgentAccountBindingItem = typeof agentAccountBindings.$inferSelect;

// ─────────────────────────────────────────────────────────────────────────────
// agent_provider_account_logins β€” which devices an account is signed in on
// ─────────────────────────────────────────────────────────────────────────────

/**
 * An account row is the real provider account (one per scope, so its quota is
 * counted once); a login is that account signed in on one device. Several
 * members can sign in to the same account, each on their own machine, and
 * every login points at the same account row. Written by the desktop sampler
 * whenever it reports a reading from a device it can resolve.
 *
 * Logins decide where an account can run (a login only exists on its machine).
 * They do not decide who may see a `private` account β€” a login is reported by
 * the device and cannot be verified, so only the account's creator sees it.
 */
export const agentProviderAccountLogins = pgTable(
  'agent_provider_account_logins',
  {
    id: uuid('id').defaultRandom().primaryKey(),

    accountId: uuid('account_id')
      .references(() => agentProviderAccounts.id, { onDelete: 'cascade' })
      .notNull(),
    deviceId: uuid('device_id')
      .references(() => devices.id, { onDelete: 'cascade' })
      .notNull(),

    /** The member whose sampler last reported this login. */
    userId: text('user_id')
      .references(() => users.id, { onDelete: 'cascade' })
      .notNull(),
    workspaceId: text('workspace_id').references(() => workspaces.id, { onDelete: 'cascade' }),

    firstSeenAt: timestamptz('first_seen_at').notNull().defaultNow(),
    lastSeenAt: timestamptz('last_seen_at').notNull().defaultNow(),

    ...timestamps,
  },
  (t) => [
    /** One login per account per device; the target of the device-binding foreign key. */
    unique('agent_provider_account_logins_account_device_unique').on(t.accountId, t.deviceId),
    index('agent_provider_account_logins_device_id_idx').on(t.deviceId),
    index('agent_provider_account_logins_user_id_idx').on(t.userId),
    index('agent_provider_account_logins_workspace_id_idx').on(t.workspaceId),
  ],
);

export type NewAgentProviderAccountLogin = typeof agentProviderAccountLogins.$inferInsert;
export type AgentProviderAccountLoginItem = typeof agentProviderAccountLogins.$inferSelect;

// ─────────────────────────────────────────────────────────────────────────────
// agent_device_account_bindings β€” per-device account choice for an agent
// ─────────────────────────────────────────────────────────────────────────────

/**
 * Which account an agent uses on one device, overriding the agent-wide pool
 * (`agent_account_bindings`). A login lives on one machine, so the choice has
 * to be per device: an account signed in on device A must not make the same
 * agent fail on device B. A device without a row here balances across the pool,
 * restricted to the accounts it holds a login for.
 *
 * The (account, device) pair must be a login on that device, enforced by the
 * composite foreign key: signing out (deleting the login) removes the binding.
 */
export const agentDeviceAccountBindings = pgTable(
  'agent_device_account_bindings',
  {
    id: uuid('id').defaultRandom().primaryKey(),

    agentId: text('agent_id')
      .references(() => agents.id, { onDelete: 'cascade' })
      .notNull(),

    /** The member who chose the account. */
    userId: text('user_id')
      .references(() => users.id, { onDelete: 'cascade' })
      .notNull(),
    workspaceId: text('workspace_id').references(() => workspaces.id, { onDelete: 'cascade' }),

    deviceId: uuid('device_id')
      .references(() => devices.id, { onDelete: 'cascade' })
      .notNull(),
    accountId: uuid('account_id')
      .references(() => agentProviderAccounts.id, { onDelete: 'cascade' })
      .notNull(),

    ...timestamps,
  },
  (t) => [
    foreignKey({
      columns: [t.accountId, t.deviceId],
      foreignColumns: [agentProviderAccountLogins.accountId, agentProviderAccountLogins.deviceId],
      name: 'agent_device_account_bindings_login_fk',
    }).onDelete('cascade'),
    /** At most one account per agent on each device. */
    uniqueIndex('agent_device_account_bindings_agent_device_unique').on(t.agentId, t.deviceId),
    index('agent_device_account_bindings_device_id_idx').on(t.deviceId),
    index('agent_device_account_bindings_account_device_idx').on(t.accountId, t.deviceId),
    index('agent_device_account_bindings_user_id_idx').on(t.userId),
    index('agent_device_account_bindings_workspace_id_idx').on(t.workspaceId),
  ],
);

export type NewAgentDeviceAccountBinding = typeof agentDeviceAccountBindings.$inferInsert;
export type AgentDeviceAccountBindingItem = typeof agentDeviceAccountBindings.$inferSelect;

// ─────────────────────────────────────────────────────────────────────────────
// agent_quota_snapshots β€” append-only provider observation (the instrument)
// ─────────────────────────────────────────────────────────────────────────────

/**
 * Time series of the provider's authoritative utilization readings. This is the
 * only place the Ξ”utilization ↔ Ξ”usage pairing can be measured, so it is the
 * instrument the calibration reads. Append-only; a row is written only when the
 * reading changes.
 */
export const agentQuotaSnapshots = pgTable(
  'agent_quota_snapshots',
  {
    id: uuid('id').defaultRandom().primaryKey(),

    accountId: uuid('account_id')
      .references(() => agentProviderAccounts.id, { onDelete: 'cascade' })
      .notNull(),
    userId: text('user_id')
      .references(() => users.id, { onDelete: 'cascade' })
      .notNull(),
    workspaceId: text('workspace_id').references(() => workspaces.id, { onDelete: 'cascade' }),
    /** Which device reported it; lets multi-device readings dedupe by account. */
    deviceId: uuid('device_id').references(() => devices.id, { onDelete: 'set null' }),

    /** Raw `limits[].kind`, e.g. `session` / `weekly_all` / `weekly_scoped`. */
    limitType: text('limit_type').notNull(),
    /** Model display name for scoped windows (e.g. `Fable`); `''` otherwise. */
    scopeKey: text('scope_key').notNull().default(''),

    resetsAt: timestamptz('resets_at'),
    /** Integer percent 0..100 as reported by the provider. */
    utilization: integer('utilization').notNull(),
    severity: text('severity'),
    isActive: boolean('is_active'),

    capturedAt: timestamptz('captured_at').notNull().defaultNow(),
    /** Full raw limit object for forensic/debug. */
    raw: jsonb('raw').$type<Record<string, unknown>>(),

    createdAt: createdAt(),
  },
  (t) => [
    index('agent_quota_snapshots_account_type_scope_idx').on(
      t.accountId,
      t.limitType,
      t.scopeKey,
      t.capturedAt,
    ),
    index('agent_quota_snapshots_account_captured_idx').on(t.accountId, t.capturedAt),
    index('agent_quota_snapshots_resets_at_idx').on(t.resetsAt),
    index('agent_quota_snapshots_workspace_id_idx').on(t.workspaceId),
    index('agent_quota_snapshots_user_id_idx').on(t.userId),
  ],
);

// ─────────────────────────────────────────────────────────────────────────────
// agent_quota_usage_ledger β€” append-only per-turn consumption (our side)
// ─────────────────────────────────────────────────────────────────────────────

/**
 * Our own consumption, one row per assistant turn, stamped with the account
 * that was active. Independent from the snapshot series; the two are crossed to
 * calibrate capacity. `accountId` is nullable so historical import (which cannot
 * know the account) can still land, tagged null and excluded from calibration.
 */
export const agentQuotaUsageLedger = pgTable(
  'agent_quota_usage_ledger',
  {
    id: uuid('id').defaultRandom().primaryKey(),

    accountId: uuid('account_id').references(() => agentProviderAccounts.id, {
      onDelete: 'set null',
    }),
    userId: text('user_id')
      .references(() => users.id, { onDelete: 'cascade' })
      .notNull(),
    workspaceId: text('workspace_id').references(() => workspaces.id, { onDelete: 'cascade' }),

    provider: text('provider').notNull(),
    model: text('model'),
    occurredAt: timestamptz('occurred_at').notNull(),

    inputTokens: integer('input_tokens'),
    outputTokens: integer('output_tokens'),
    cacheReadTokens: integer('cache_read_tokens'),
    cacheWriteTokens: integer('cache_write_tokens'),
    reasoningTokens: integer('reasoning_tokens'),

    costUsd: amountNumeric('cost_usd'),
    costSource: text('cost_source'),

    // ── Provenance links (for attribution / dedupe) ──────────────────────────
    messageId: text('message_id').references(() => messages.id, { onDelete: 'set null' }),
    operationId: text('operation_id').references(() => agentOperations.id, {
      onDelete: 'set null',
    }),
    topicId: text('topic_id').references(() => topics.id, { onDelete: 'set null' }),
    agentId: text('agent_id').references(() => agents.id, { onDelete: 'set null' }),
    /** Idempotency key (e.g. `${messageId}:${turnIndex}`) for at-most-once import. */
    externalEventId: text('external_event_id'),

    metadata: jsonb('metadata').$type<Record<string, unknown>>(),

    createdAt: createdAt(),
  },
  (t) => [
    index('agent_quota_usage_ledger_account_occurred_idx').on(t.accountId, t.occurredAt),
    index('agent_quota_usage_ledger_account_model_occurred_idx').on(
      t.accountId,
      t.model,
      t.occurredAt,
    ),
    index('agent_quota_usage_ledger_message_id_idx').on(t.messageId),
    index('agent_quota_usage_ledger_operation_id_idx').on(t.operationId),
    index('agent_quota_usage_ledger_workspace_id_idx').on(t.workspaceId),
    index('agent_quota_usage_ledger_user_id_idx').on(t.userId),
    uniqueIndex('agent_quota_usage_ledger_external_event_unique')
      .on(t.externalEventId)
      .where(sql`${t.externalEventId} IS NOT NULL`),
  ],
);

export type NewAgentQuotaUsageLedger = typeof agentQuotaUsageLedger.$inferInsert;
export type AgentQuotaUsageLedgerItem = typeof agentQuotaUsageLedger.$inferSelect;

// ─────────────────────────────────────────────────────────────────────────────
// agent_quota_calibrations β€” learned capacity per (account, kind, scope)
// ─────────────────────────────────────────────────────────────────────────────

export const agentQuotaCalibrations = pgTable(
  'agent_quota_calibrations',
  {
    id: uuid('id').defaultRandom().primaryKey(),

    accountId: uuid('account_id')
      .references(() => agentProviderAccounts.id, { onDelete: 'cascade' })
      .notNull(),
    userId: text('user_id')
      .references(() => users.id, { onDelete: 'cascade' })
      .notNull(),
    workspaceId: text('workspace_id').references(() => workspaces.id, { onDelete: 'cascade' }),

    limitType: text('limit_type').notNull(),
    scopeKey: text('scope_key').notNull().default(''),

    /** Capacity in provider-equivalent USD (the stable unit). */
    capacityUsd: amountNumeric('capacity_usd').notNull(),
    /** Derived token equivalent at the observed model mix (drifts; display-only). */
    capacityTokensEquivalent: bigint('capacity_tokens_equivalent', { mode: 'number' }),
    /** Cost share by model over the calibration sample. */
    modelMix: jsonb('model_mix').$type<Record<string, number>>(),

    sampleCount: integer('sample_count').notNull(),
    /** 0..1 confidence, grows with samples + fit quality. */
    confidence: amountNumeric('confidence'),
    /** e.g. `theil_sen` / `ratio`. */
    method: text('method'),
    windowSeconds: integer('window_seconds'),

    calibratedAt: timestamptz('calibrated_at').notNull().defaultNow(),
    metadata: jsonb('metadata').$type<Record<string, unknown>>(),

    createdAt: createdAt(),
  },
  (t) => [
    index('agent_quota_calibrations_account_type_scope_idx').on(
      t.accountId,
      t.limitType,
      t.scopeKey,
      t.calibratedAt,
    ),
    index('agent_quota_calibrations_workspace_id_idx').on(t.workspaceId),
    index('agent_quota_calibrations_user_id_idx').on(t.userId),
  ],
);

export type NewAgentQuotaCalibration = typeof agentQuotaCalibrations.$inferInsert;
export type AgentQuotaCalibrationItem = typeof agentQuotaCalibrations.$inferSelect;

// ─────────────────────────────────────────────────────────────────────────────
// agent_quota_windows β€” per-window projection (LB + UI read model)
// ─────────────────────────────────────────────────────────────────────────────

/**
 * One row per concrete reset window, keyed by `resets_at` (the provider's own
 * natural key). Fully derivable from snapshots + ledger + calibrations; kept
 * materialized so the QuotaMenu and the load balancer can read it cheaply.
 */
export const agentQuotaWindows = pgTable(
  'agent_quota_windows',
  {
    id: uuid('id').defaultRandom().primaryKey(),

    accountId: uuid('account_id')
      .references(() => agentProviderAccounts.id, { onDelete: 'cascade' })
      .notNull(),
    userId: text('user_id')
      .references(() => users.id, { onDelete: 'cascade' })
      .notNull(),
    workspaceId: text('workspace_id').references(() => workspaces.id, { onDelete: 'cascade' }),

    limitType: text('limit_type').notNull(),
    scopeKey: text('scope_key').notNull().default(''),

    /** Natural key: provider-reported window reset instant. */
    resetsAt: timestamptz('resets_at').notNull(),
    windowStartAt: timestamptz('window_start_at').notNull(),
    windowSeconds: integer('window_seconds').notNull(),

    /** Max utilization seen β€” monotonic within a window, so robust to gaps. */
    peakUtilization: integer('peak_utilization').notNull().default(0),
    lastUtilization: integer('last_utilization'),
    /** First real 429 observed inside this window, if any. */
    rateLimitedAt: timestamptz('rate_limited_at'),

    observedCostUsd: amountNumeric('observed_cost_usd'),
    observedTokens: bigint('observed_tokens', { mode: 'number' }),
    estimatedCapacityUsd: amountNumeric('estimated_capacity_usd'),
    /**
     * Ξ”utilization > 0 while our ledger saw β‰ˆ0 spend β†’ an external consumer (CLI
     * outside LobeHub) moved the meter. Excluded from calibration.
     */
    contaminated: boolean('contaminated').notNull().default(false),

    firstSeenAt: timestamptz('first_seen_at'),
    lastSeenAt: timestamptz('last_seen_at'),

    ...timestamps,
  },
  (t) => [
    uniqueIndex('agent_quota_windows_natural_key_unique').on(
      t.accountId,
      t.limitType,
      t.scopeKey,
      t.resetsAt,
    ),
    index('agent_quota_windows_account_resets_idx').on(t.accountId, t.resetsAt),
    index('agent_quota_windows_user_id_idx').on(t.userId),
    index('agent_quota_windows_workspace_id_idx').on(t.workspaceId),
  ],
);

export type NewAgentQuotaWindow = typeof agentQuotaWindows.$inferInsert;
export type AgentQuotaWindowItem = typeof agentQuotaWindows.$inferSelect;