Web AppProjects

AI Avatar Live Commerce Platform

An AI avatar live commerce platform for Thai SMEs. It is an integration/orchestration layer on top of an existing Avatar Engine, adding Live Studio, a real-time Console, an auto-reply pipeline and a multi-tenant back office to close the loop. Built as a monorepo (Next.js 16 · NestJS/Fastify · Drizzle/Postgres RLS) with TDD + contract-first

12 min read

An operating system for AI avatar live selling. It is an integration / orchestration layer on top of an existing Avatar Engine (TTS · lip-sync · render · RAG) that adds the missing parts: Live Studio, a real-time Console, an auto-reply pipeline and a multi-tenant back office, closing the loop from setting the scene to closing the sale

Next.js 16React 19NestJS 11FastifyDrizzle ORMPostgreSQL + RLSZitadel OIDCSSEWebRTCpnpm monorepo
Empathize · Define

The problem

Why can't SME shops keep a live stream going "continuously" when every live hour is a chance to sell?

Live dashboard showing 3 streams an AI avatar is broadcasting at the same time, with viewer counts, comments and how many the AI answered
The real live dashboard: avatars can broadcast in several rooms at once, so the shop keeps selling in hours when no human host is around (UAT test data)

Human hosts can only stream so long. They need breaks, nobody is around after hours, and many small shops have no host at all. Every hour without a stream is sales that simply disappear. Meanwhile most AI avatar tools on the market can only "record a video", not "go live", and they have no commerce loop: they can't answer live comments, switch products or close a sale

The starting brief, before building the AI avatar live platform (positioning: support the host, not replace them)

Hosts can only stream limited hours

People need rest and sleep, so streaming after hours or late at night is almost impossible. Idle hours with no stream are demand left on the table, especially for shops without a regular host

Multiple host shifts are expensive

Covering the whole day means hiring several people across shifts, which SMEs can't afford. So they only stream at certain times and miss the hours with good traffic but no host

Comments pile up faster than anyone can answer

At peak, dozens of customers ask about price, stock and promotions at once. A slow answer loses the sale, and a wrong or overstated claim about a product carries legal risk

AI avatar tools are async, not live

Existing avatar video tools can't interact in real time and have no live commerce loop (product switching, CTA timing, live comment replies), so they can't close sales on a real live stream

Many shops and workspaces, data must stay strictly separate

A platform where an agency manages many clients can't leak even one row across shops. Doing isolation by hand is easy to get wrong and hard to audit


Ideate

The solution

Don't build a new AI core. Build on the existing Avatar Engine and fill in what's missing to close the live commerce loop

Risk words page, grouping banned word sets by business type, with stats on blocked comments and the most frequently triggered words
Risk Filter in the back office: risk word sets per business type, blocked before the avatar says an overstated claim

Integration layer on top of the Avatar Engine

The existing engine already has TTS / lip-sync / render / RAG. The AI avatar live platform wires the trigger logic, feeds script and product context, and manages the session lifecycle, without rebuilding a duplicate avatar core

Live Studio for scene setup + streaming from real devices

Compose scenes, layers and products free-form on one canvas, then broadcast from the shop's own devices (phone-as-camera). The avatar speaks from a script tied to each product

Auto-reply pipeline with a Risk Filter

Comment → intent detection → Risk Keyword block before replying → Reply Burst priority queue (price/stock jump ahead) so the avatar answers live within a set latency budget

Live Console real-time (SSE)

Managers watch avatar status, stream health, the comment feed and the risk panel live, through an SSE stream that survives reconnects, keeps order and drops duplicate events

Multi-tenant data isolation at the database level

Every query is bound to workspace_id, with Postgres RLS as the last line of defense (Model B tenancy: authority lives in a central hub, identity is handled separately by Zitadel). Isolation is non-negotiable and covered by negative tests


Prototype

How it works, in 4 steps

From preparing the avatar and products to closing the sale and reviewing results

Avatar library for choosing the presenter avatar to use in a live room
Step 1, setup: pick an avatar from the library the engine provides
1

Setup

Pick an avatar/voice, build the product catalog + risk words, set the live schedule

2

Compose Studio

Arrange scenes, layers and products + write the script yourself or let AI generate it

3

Broadcast

Stream from real devices, with the avatar following a script tied to each product

4

Engage + recap

Auto-reply to comments + monitor in the Console → Post-Live Recap


Prototype

Tech Stack & Architecture

Monorepo (pnpm workspaces): Next.js PWA + NestJS/Fastify API + Drizzle/Postgres, plus external integrations

Workspace overview page showing the team, data library and live schedule of a single workspace
The back office of one workspace: every record is bound to workspace_id and fenced by RLS
Web PWA for shops / managers / agencies (Next.js 16 · React 19)
Live Studio Live Console Back Office Onboarding
↕
Backend API — NestJS 11 + Fastify · Live Orchestrator
workspace / catalog script / risk-word live-orchestrator auth / audit
↕
Integrations — Avatar Engine · Zitadel · Hub (tenancy) · Comment source
avatar-engine-contracts hub-integration comment-consumer SSE console
↕
PostgreSQL + Drizzle (RLS) · MinIO/S3 · Terraform · Docker Compose
workspace_id scoping drizzle migrations media-asset audit-log
Why this stack?

NestJS 11 + Fastify: the Live Orchestrator is a long-running process (route handlers don't fit), and DI with one module per feature lets the team scale · Drizzle ORM: TS-first, the schema is TypeScript so it matches FE type sharing, and it stays close to raw SQL, which suits multi-tenant queries and audits · PostgreSQL + RLS: 100% data isolation at the database level · Zitadel (identity) + Model B tenancy: identity is separated from workspace/entitlement authority (which lives in the central hub) · pnpm workspaces + OpenAPI → generated client (with a contract drift gate in CI): type safety across packages without a complicated build matrix


Prototype

What I built: 8 core capabilities

The full loop, from the back office to the live stream and the post-live recap

Live Studio composing a vertical 9:16 scene with the avatar, product card, scene list, layers and a script that has passed the risk word check
Live Studio: arrange scenes, layers and products, attach scripts, and get risk word warnings before approval

Multi-tenant Workspace & Roles

4 roles (Owner/Manager/Admin/Agency) + workspace scoping on every query + RLS. Agencies can see several workspaces without data mixing

Catalog & Live Schedule

Product/SKU/Product Set CRUD + Live Schedule tied to a LiveSession state machine with 8 states (Live/Paused/Completed/Interrupted, etc.)

Live Studio (free-form)

Free-form scene compositor: scenes ≤5, layers ≤10/scene, 9 layer types + custom upload + auto-switch when a script ends

AI Script Generation

8 script types + multiple contents per slot (1-5) + a 5-state lifecycle gated by role + a risk check per content + rotation/cool-down/shuffle

AI Avatar Live Broadcasting

Phone-as-camera PWA + WebRTC composition + a 20-minute session cap + preload & seamless handover so the stream keeps running without hiccups

Comment Auto-Reply

Intent detection + Risk Keyword block before replying + Reply Burst ≤6/batch with P1/P2/P3 priority + per-script TTL drop

Live Console (SSE)

Live monitoring: session list + Control Room + comment feed + risk panel over an SSE stream that auto-reconnects and keeps order

Post-Live Recap

Aggregates AI outcomes per session and joins them with raw comments pulled from the source, so it can rebuild from source instead of relying on snapshots alone


Prototype

What the Live Console looks like

A manager opens the Console and sees avatar status, the comment feed and the risk panel live on one screen

Live control room showing status, comments, the share of AI replies, the burst queue and blocked risk words
The real live control room (captured during UAT while connecting a stream). Below is a mock of comments flowing in
LIVE Avatar: Mint Uptime 18:24 auto-reply 42
Risk Word: detected before replying, so the avatar is blocked from saying it
Comment: "Will it cure me for good if I take it?" · matches the overstated-claim word group
blocked logged → audit
CommentIntentAction
How much is this one? price Reply Burst · P1 jumps ahead auto
Is size L still available? stock Reply Burst · P1 auto
I want the same color as yours chit-chat P3 · dropped when the next script starts queued
# Directive Override — the manager tells the avatar to cut in right away
directive --force-cta product=SKU-4821 --say 'Add it to your cart now, only 5 left'

Test & Iterate

Hacking the problems

6 engineering problems I had to design solutions for: multi-tenancy, real time, integration lifecycle

Script library, split into pending approval and in use, with a script type filter
Script library: a pending → active lifecycle controlled by role and a risk check per content

Not a single row can leak across workspaces

Agencies see many shops in one dashboard. One badly scoped query leaks data across clients, which is non-negotiable

Fix: enforce workspace_id scoping on every query + Postgres RLS as the last line of defense in the DB, an ESLint guard that blocks unscoped queries, and a negative isolation test proving nothing leaks. Isolation is part of the Definition of Done, not left for hardening at the end

Who owns tenancy data? (Model B)

The ecosystem has several products. If each one stores its own workspaces, memberships and subscriptions, they drift out of sync and entitlements go wrong

Fix: clear separation of duties. The central hub is the authority for tenancy/entitlement (resolving the workspace + verifying permissions through the Hub API), Zitadel handles identity (login) only, and the AI avatar live platform mirrors memberships locally. A Prism mock of the Hub means devs don't have to run the real hub every time

Comments pile up, but the avatar can only answer one at a time

At peak, dozens of comments arrive at once. It can't, and shouldn't, answer all of them. It has to pick the important ones first within a limited time budget

Fix: a Reply Burst priority queue. Price/stock (P1) jumps ahead of chit-chat (P3), capped at ≤6 per batch, and a per-script TTL drop discards comments that have waited too long once the next script starts. The queue stays small and replies land at the right moment

The Console has to be live, but connections keep dropping and reconnecting

Mobile live streams have shaky connections. If the stream drops and events go missing, repeat or arrive out of order, the manager reads the wrong status and makes the wrong call

Fix: design the SSE console stream to auto-reconnect, keep ordering, dedupe repeated events and apply backpressure when the client falls behind. The reliability contract is written as a separate NFR, not best-effort

Integrating with external engines and services that don't exist yet

The Avatar Engine, comment source and some Hub endpoints weren't ready when development started. Waiting for them before writing the integration would block the whole pipeline

Fix: contract-first. Write network-free contract tests from the spec first (red-first even while the implementation is pending), run an OpenAPI/contract drift gate in CI Tier 1 on every PR, and use Prism mocks so devs can work in parallel without the real services

The engine caps sessions at 20 minutes, but the live stream must keep going

The Avatar Engine closes a session every 20 minutes. If it just ends, the picture stutters and the audio cuts out, and viewers notice immediately

Fix: the AI avatar live platform manages the session lifecycle. It preloads the next session ≥90s before the cap (or ≥30s before a scene switch) and does a seamless handover within a budget of audio ≤200ms / visual ≤100ms while keeping pose continuity. Viewers never know a session switched behind the scenes

Define

Deliberate design decisions

What I chose "not to build" matters as much as what I built

Avatar picker in Live Studio, filtered by gender, language and type
Pick an avatar from the existing engine right inside Studio: integrate, don't rebuild the engine

The AI avatar live platform doesn't build a new avatar core. TTS, lip-sync, render, RAG, ≤2s latency and GPU pooling already exist in the Avatar Engine, so the focus is the integration layer: orchestration + the commerce loop + a multi-tenant back office + a real-time console. The positioning was sharp from day one: support the host, not replace them, unlocking hours people can't cover. A clear scope means a focused implementation that's easy to maintain

Integrate, don't rebuild the engine

Build on the existing engine instead of duplicating the avatar core

Isolation = non-negotiable

RLS + workspace scoping + negative tests on every PR

TDD + contract-first

red → green → refactor · tests are part of the DoD

Support the host, not replace them

Unlock the hours people can't cover: a clear positioning


Level Up · Reflect

Knowledge domains that got stronger

5 areas where my level genuinely moved up through this project

All recent live streams page, showing peak viewers, comments and AI replies per stream, with trends
Live history: viewers, comments and AI replies per session for the Post-Live Recap (test data)
1. Multi-tenant SaaS architecture, from "knowing the concept" to "designing isolation I can prove":

I now understand layered data isolation: workspace_id scoping in the application, Postgres RLS in the database, an ESLint guard that catches mistakes while writing code, and negative tests proving nothing leaks, plus the tenancy authority split (Model B: authority in the hub, identity in Zitadel). Lesson: good isolation is several testable layers, not good intentions

2. Real-time systems — latency budget, priority queue, stream reliability:

I designed Reply Burst as a priority queue (P1/P2/P3 + TTL drop) so replies land on time within the latency budget, and an SSE console stream that survives reconnects, keeps ordering, dedupes and applies backpressure. Lesson: reliable real time means designing the failure modes (drops, duplicates, falling behind) from the start, not best-effort

3. Monorepo + contract-driven development:

I set up pnpm workspaces (apps + shared packages: db/auth/types/api-client/engine-contracts), generated the client from OpenAPI with a drift gate, and wrote network-free (red-first) contract tests against external services that didn't exist yet. Lesson: a contract is the agreement that lets teams and services move in parallel without blocking each other

4. Integration architecture, an adapter over a 3rd-party engine + session lifecycle:

I designed a layer that wraps the existing Avatar Engine without touching its core, handling preload/handover so the 20-minute session cap looks continuous (audio ≤200ms / visual ≤100ms / pose continuity), with an adapter pattern ready for new providers. Lesson: making existing pieces fit "seamlessly" is harder than writing from scratch, and worth far more

5. Backend engineering with NestJS + Fastify + Drizzle:

I laid out one module per feature (21 modules) + DI, chose Fastify because the Live Orchestrator is a long-running process, and used Drizzle (a TS-first ORM close to raw SQL) with migration discipline (versioned · idempotent · fail-loud when the schema doesn't match). Lesson: structure enforced by conventions + guards lets a solo dev run a large system without it falling apart

21
NestJS API modules (module-per-feature)
43
Screens in the web app (Next.js routes)
12
Database tables (Drizzle schema)
7
Epics, from Foundation to Pilot

Tan · Natpakan Sirirat

Product Designer & AI Builder — designing user experiences and building AI tools that actually work.

0%