Stockear

An inventory and invoicing workspace for small businesses. I built flexible product schemas, conversational inventory tools, and supplier-document extraction, alongside ARCA electronic invoicing.

These historical architecture notes describe a FastAPI/Astro design and have not been reconciled with the current Next.js codebase. Current product screenshots are available in Selected Work.

Three Things That Make Stockear Unique

Most portfolio projects pick one domain. Stockear spans AI, SaaS infrastructure, and regulatory compliance — simultaneously.

Intelligent Chat Interface

Ask your inventory natural questions.

Behind the scenes, embeddings (VoyageAI + Google GenAI) power semantic search over your product catalog. The agent orchestrates tool calls — CRUD, search, invoice generation — in a single conversational turn.

  • Vector semantic search
  • Multi-tool agent orchestration
  • Groq LLM with parallel tool calls
  • Context-aware responses

SaaS Metering at Scale

Per-tenant quotas without leaky abstractions.

The historical design models workspace isolation and subscription tiers, with dependency-layer quota checks before protected business logic.

  • Per-workspace schema isolation
  • DI-injected QuotaVerifier
  • Subscription tier enforcement
  • Usage tracking & audit log

Argentine Fiscal Compliance

AFIP-integrated voucher emission.

The historical design describes ARCA/AFIP invoice generation through a hexagonal billing module, with separate adapters for testing and tax-service integration.

  • arca_arg AFIP integration
  • Hexagonal billing isolation
  • Mock adapter for testing
  • Staging + production environments

Architecture Overview

Four connected layers — each with clear boundaries and defined responsibilities.

🎨

Frontend Layer

Built with Astro 6 + React islands for maximum performance. Static shell with selective hydration — only interactive components ship JavaScript. Lucia Auth handles session management with secure HTTP-only cookies.

Rendering diagram…

Technical Decisions

Not every choice is obvious. These architectural decisions explain the why behind Stockear's design.

ADR-01DDDClean Architecture

Hexagonal Architecture in Billing

Argentine tax regulations (AFIP/ARCA) change frequently. The billing module needs to be testable without real API calls and must remain decoupled from the HTTP framework.

Decision

Apply ports & adapters (hexagonal) pattern to the billing domain. Domain logic has zero imports from FastAPI, Motor, or arca_arg.

Rationale

  • Pure domain logic is unit-testable without hitting AFIP staging endpoints
  • Adapter swap between mock/staging/production without changing domain code
  • Regulatory changes isolated to adapter implementations, not core rules
  • Conforms to Dependency Inversion Principle at architectural level

Trade-offs

✓Tax rules testable via mock adapters
⚠More interface boilerplate to maintain
✓AFIP integration decoupled — safe to refactor independently
⚠Slightly higher conceptual overhead for new contributors
DDDClean ArchitectureDependency InversionAdapter Pattern
ADR-02Plugin ArchitectureStrategy Pattern

Agent-First Design with Tool Registry

The AI chat interface needs to call multiple backend tools (inventory CRUD, semantic search, invoice generation). Hardcoding tool dispatch would create tight coupling and make adding new capabilities expensive.

Decision

Implement a Tool Registry where each tool is a self-describing callable with a schema. The agent reads the registry at runtime and routes LLM-requested tool calls dynamically.

Rationale

  • New tools added by registering — zero changes to core agent code
  • Tool schemas drive LLM function-calling automatically
  • Registry pattern is naturally testable — tools are just functions
  • Enables parallel tool execution for multi-step agent tasks

Trade-offs

✓Extensibility without core modification
⚠Tool discovery overhead at agent initialization
✓Clean separation between orchestration and implementation
⚠Schema drift if tool signatures change without registry update
Plugin ArchitectureStrategy PatternFunction Calling (LLM)OpenAPI Schema
ADR-03Dependency InjectionCross-Cutting Concerns

Per-Tenant Quota Gating via Dependency Injection

Every API endpoint must respect per-tenant subscription limits. Scattering quota checks in business logic would lead to inconsistencies and bypasses.

Decision

Inject a QuotaVerifier as a FastAPI dependency into every protected endpoint. The verifier reads tenant subscription from MongoDB and raises HTTP 429 before business logic executes.

Rationale

  • Quota enforcement is a cross-cutting concern — belongs in DI, not business logic
  • New endpoints automatically protected by including the dependency
  • Tenant-aware: each workspace has independent quota counters
  • Testable by mocking the QuotaVerifier dependency in unit tests

Trade-offs

✓Shared dependency centralizes quota checks on endpoints that include it
⚠Extra MongoDB read on every protected request
✓Quota logic centralized — change once, applies everywhere
⚠DI chain can be complex for nested dependencies
Dependency InjectionCross-Cutting ConcernsMiddlewareAOP
ADR-04Event SourcingAppend-Only Log

MongoDB as Event Store for Audit Trail

Argentine fiscal compliance requires an immutable log of all transactions, schema changes, and agent actions. The system must be auditable and replayable.

Decision

Use MongoDB as an append-only event store. All significant actions (chat messages, invoice emissions, schema changes, quota events) are written as immutable event documents.

Rationale

  • MongoDB's document model maps naturally to heterogeneous event types
  • Append-only event design intended to preserve an audit trail
  • Events queryable for compliance reports without separate audit DB
  • Event replay enables debugging of agent sequences post-incident

Trade-offs

✓Single DB for operational data AND audit — lower infra cost
⚠Not a purpose-built event store (no Kafka/EventStore features)
✓Rich query language for compliance report generation
⚠Event schema evolution requires careful migration planning
Event SourcingAppend-Only LogCQRSAudit Trail

Deployment Architecture

From browser to compliance authority — every layer mapped out.

🌐

User Browser

Astro + React Islands

Static Astro shell with selective React hydration. Zero JS for static sections.

REST API / HTTPS
▲

Vercel Edge

SSR + Static Assets

Astro deployed to Vercel. SSR pages rendered at edge; static assets globally cached.

HTTP Proxy
⚙️

FastAPI Backend

Python · Agent · Tools

Main orchestration layer: agent, tool registry, quota enforcement, business logic.

MongoDB Wire Protocol
🍃

MongoDB Atlas

Per-Tenant Schemas

Each workspace gets an isolated schema. Also acts as event store for audit trail.

arca_arg SDK
🧾

AFIP / ARCA

Argentine Tax Authority

Legal invoice emission via hexagonal adapter. Staging and production environments.

Google GenAI API
🧠

AI Embeddings

Google GenAI + VoyageAI

Embedding generation for semantic search. Vectors stored in MongoDB for retrieval.