ProjectsCase Study

ZNPak.ai

Production-grade reusable AI SaaS core—workspaces, SSE assistant, secure attachments, pgvector RAG, and monorepo packages for future vertical products.

ZNPak.ai platform overview
Industry
AI SaaS · Platform engineering
Category
Multi-tenant AI foundation
Role
Full Stack Product Engineer — platform packages, assistant V1, security model
Timeline
2026
Status
Foundation shipped (~Priority 01–03 core)
Stack
Next.js · Turborepo · Supabase · OpenAI · pgvector · Stripe-ready · Vitest · Playwright

Executive Overview

ZNPak.ai exists so the next LinkedIn AI, Resume AI, or Instagram AI app is mostly config and prompts—not a rewrite of auth, chat persistence, uploads, and RAG.

apps/web demonstrates the generic assistant: streamed SSE responses, conversation CRUD, multimodal attachments with magic-byte validation, and workspace-scoped knowledge retrieval.

Security is treated as platform law: service role stays server-only, workspace membership gates every AI and file route, retrieved chunks are untrusted data—not prompt overrides.

Billing and Stripe webhooks are wired in packages with provider flags; the example product can run free-tier while the same core sells paid plans later.

Business Problem

Existing workflow

Teams spin a new Next.js app per AI idea, copy-paste Supabase auth, then diverge on upload safety, idempotent chat, and RAG isolation until merges become impossible.

Pain points

  • Vertical products need the same workspace boundary, not one-off SQL.
  • Client-side keys and client-provided user ids fail audits on day one.
  • Duplicate chat sends double-bill tokens and confuse users after flaky networks.
  • RAG without injection discipline turns uploaded PDFs into instruction overrides.

Why it matters

A shared AI core only pays off if every vertical inherits the same trust model—RLS, rate limits, and orchestration boundaries included.

Solution

Platform layers vertical apps plug into:

  • Identity & workspaces

    Supabase Auth, workspace_members, requireWorkspaceAccess, and RLS on user-owned tables.

  • Streaming assistant

    POST /api/ai/chat → orchestrator with SSE, conversation persistence, retry/cancel, and chat idempotency keys.

  • Knowledge & attachments

    Workspace-scoped documents in Storage, pgvector chunks, retrieval wrapped as untrusted content; 3-step attachment upload with MIME validation.

  • Packages, not copy-paste

    @znpak/ai, database, auth, storage, billing, validation—apps import orchestration instead of forking it.

New AI products start from a hardened base: same authz, same upload rules, same streaming contract—only product config changes.

Core Features

Assistant product

Streaming chat

SSE stream from the OpenAI orchestrator with client retry and AbortController cancel.

NeedUsers judge AI products on responsiveness and recoverability.

ValueProduction chat UX out of the box.

Secure attachments

Intent → signed upload → magic-byte check before multimodal prompts.

NeedArbitrary files are untrusted input to the model.

ValueSafer multimodal assistant without public buckets.

Knowledge (RAG)

Workspace documents

Upload, chunk, embed into pgvector; retrieve inside orchestrator stream with source events.

NeedVertical SaaS needs private corpora per customer workspace.

ValueGrounded answers without a second RAG stack per product.

Injection-aware prompts

Retrieved text wrapped in explicit untrusted markers in system rules.

NeedUploaded content must not rewrite platform safety instructions.

ValueRAG that survives security review.

Platform packages

AI orchestrator

Provider abstraction, prompt registry, context engine, tools—database injected from the app.

Needpackages/ai must not import apps/web.

ValueClean dependency direction for many future apps.

Billing interface

Stripe checkout + webhook verification hooks; provider flag for free mode during foundation phase.

NeedMonetization should not require a rewrite when a vertical launches.

ValueSame core sells free example and paid verticals.

User Journey

Workspace member asks with a file

  1. 01

    Authenticate and pass workspace membership checks.

  2. 02

    Upload attachment through intent + validated bytes; wait until ready.

  3. 03

    Send chat with idempotency key; stream assistant reply with usage tracking.

Technical Highlights

Chat idempotency table

assistant service checks chat_idempotency before persisting duplicate user sends when clients retry after network blips.

Business valueFewer duplicate charges and confusing twin messages.

Database-backed rate limits

Production RateLimiter uses rate_limit_events via service role—not in-memory maps on serverless.

Business valueConsistent throttling across AI and knowledge upload routes.

Layered monorepo boundaries

apps/web → server services → @znpak packages → Supabase/OpenAI; core packages never import application code.

Business valueFuture apps/<product> folders stay thin.

Challenges & Solutions

ChallengeSolution
Flaky mobile networks replayed POST /api/ai/chat and created duplicate assistant rows for one user gesture.Client-generated idempotency keys with chat_idempotency upsert—skipped on explicit user retry.
Workspace RAG needed pgvector and Storage without letting retrieved PDF text override system safety rules.Retriever output wrapped in <untrusted-retrieved-content> with documented injection policy in docs/security.md.
Letting each vertical fork auth or upload validation would rot the foundation within one product launch.Single packages/auth, storage, and validation surfaces; verticals only supply config/product.ts and prompts.

Tech Stack

Monorepo
pnpm, Turborepo, apps/web + packages/*
App
Next.js App Router, server services, Zod on APIs
Data
Supabase Postgres, RLS, pgvector, private Storage buckets
AI
@znpak/ai orchestrator, OpenAI provider, SSE streaming
Quality
Vitest unit suite, Playwright smoke/E2E paths

Project Gallery

Recommended capture order for the case-study gallery.

  1. 01Assistant chat — streaming reply
  2. 02Attachment upload progress
  3. 03Knowledge documents list
  4. 04Workspace dashboard shell

Key Learnings

Platform

The generic assistant in apps/web is the contract test—if a feature is not good enough for the example app, verticals should not inherit it.

Security

RAG is a data channel, not a second system prompt—mark retrieved content untrusted or audits fail.

Architecture

Keep packages/ai free of database imports; inject usage and retrieval from the app so orchestration stays portable.

Future Improvements

PriorityImprovementBusiness value
HighFirst vertical app (e.g. Resume AI) on apps/<product> config onlyProves the foundation story with a market-facing SKU.
MediumEnable Stripe provider in production billing configTurns limits and plans from schema into revenue without new core code.

Building an AI SaaS that needs workspaces, files, and RAG done right?

ZNPak.ai is a case study in a reusable AI core—not a demo chat page. If you want the same platform discipline for your product line, start a conversation.

Start a conversation
Role

Full Stack Product Engineer

Client

Personal platform / AI SaaS foundation

Date

2026

Collaborators

Solo build

Next Project

Apna Contacts

View next