Skip to content

AI Platform Architecture

Cross-cutting AI Assistant capability for EloSync workspaces. Product surface is licensed as the billable ai marketplace module; prepaid credit packs are additive SKUs that depend on ai.

Goals

  • Answer business questions using trusted workspace context (modules, permissions, timezone).
  • Expose read-only tools gated by module entitlement and Spatie permissions (including cross-module search_workspace).
  • Require explicit confirmation before low-risk writes (for example create_task).
  • Support Platform billing (Central provider + credit wallet) or BYOK (tenant-owned API key).

High-level layout

text
Tenant UI / API
    └── AIGateway (orchestration)
            ├── AiConfigResolver (platform | byok | central)
            ├── AiCreditWalletService (included + prepaid buckets)
            ├── AIToolRegistry + LaravelToolAdapter (permission-aware tools)
            ├── PendingAiActionService (write confirmation)
            ├── EloSyncBusinessAgent (laravel/ai structured output)
            ├── AiUsageRecorder + PlatformAuditService
            └── ContextBuilder (workspace + page context)

Billing modes

ModeSourceCredits
PlatformCentral ai_api_key when ai_allow_platformBurns wallet credits per token usage
BYOKTenant ai_api_key when ai_mode=byok and ai_allow_byokNo platform credit burn
CentralCentral key (operator/test flows)N/A for tenant chat

Workspace setting ai_mode defaults to platform. Tenant keys never appear in admin API payloads (masked as ********).

Credit wallet

Each workspace has one ai_wallets row:

  • included_remaining — monthly allowance from the AI module subscription; expires on rollover.
  • prepaid_balance — purchased packs (ai-credits-* SKUs); carries over month to month.

Ledger entries in ai_credit_ledgers record grants, burns, expirations, and purchase idempotency (reference = subscription).

Scheduled command: ai:rollover-monthly-credits (see AI credits guide).

Authorization layers

  1. Module gatemodule:ai on tenant routes.
  2. Permissionsai.use, ai.confirm, ai.manage (see config/tenant-permissions.php).
  3. Tool registry — each tool declares module() + permissions(); destructive tools are excluded.
  4. Policies — Lead Copilot endpoints call Gate::authorize('view', $lead).
  5. Tenant isolationBelongsToTenant global scope; foreign conversation IDs return 404.

Data model (tenant DB)

TablePurpose
ai_conversationsUser-owned chat threads
ai_messagesUser/assistant turns + structured JSON
ai_pending_actionsWrite proposals awaiting confirmation
ai_walletsDual-balance credit store
ai_credit_ledgerImmutable wallet movements
ai_usage_eventsToken/credit telemetry
ai_tool_executionsTool audit trail

Lead Copilot

Focused endpoints on a single lead (summarize, next-action, draft-follow-up) reuse the gateway, wallet gate, and structured agent schema without tool calling.

Official documentation for the EloSync SaaS Platform.