How hbai-ltd/toonflow-app works
1. What this repo is
Toonflow is a Node.js/TypeScript backend for an AI short-drama production system, packaged with a built-in web UI and an Electron desktop shell. The backend is the core product; the front-end source lives elsewhere, but compiled UI assets are shipped in data/web.
2. Top-level runtime shape
The application starts from src/app.ts, with shared bootstrapping in src/core.ts, routing in src/router.ts, environment parsing in src/env.ts, and error/logging helpers in src/err.ts and src/logger.ts. The server is an Express 5 API serving both JSON endpoints and static assets.
3. Primary user-facing domains
The API is organized by business areas under src/routes/: project, novel, script, production, assets, artStyle, task, modelSelect, setting, login, and migrate. These map directly to the product workflow: create a project, ingest a novel, derive script/event structure, generate storyboard/assets, and assemble output video.
4. Creative pipeline
The main data flow is:
novel import → chapter/event extraction → script generation/adaptation → production planning/storyboard → asset generation → video assembly/export.
The repo supports this through dedicated routes such as novel/getNovelData, novel/event/generateEvents, script/extractAssets, production/storyboard/*, and production/workbench/*.
5. Agent architecture
There are two agent subsystems in src/agents/: scriptAgent and productionAgent. Each has its own index.ts and tools.ts, and the README describes a three-layer pattern: decision, execution, and supervision. The routes under src/routes/scriptAgent and src/routes/agents expose agent plan/state and memory operations to the UI.
6. Persistent agent memory
Agent memory is not just chat history; it is a long-lived retrieval layer backed by local embeddings and SQLite. The ONNX model bundle in data/models/all-MiniLM-L6-v2 supports semantic recall via src/utils/agent/embedding.ts and src/utils/agent/memory.ts, with database setup in src/lib/initDB.ts and repair/migration helpers in src/lib/fixDB.ts.
7. Skill and prompt system
Prompt logic is externalized into Markdown skill files under data/skills/. There are separate skill collections for story genres and art styles, plus common production/script skill docs. This makes the behavior of the agents configurable without changing source code, and it is surfaced by settings routes such as setting/skillManagement/* and prompt-management endpoints.
8. Asset and media handling
Assets are a first-class subsystem, with routes for uploading, saving, generating, polling, deleting, and batch operations under src/routes/assets and src/routes/assetsGenerate. The repo also includes a video asset (data/assets/ending.mp4) and a static OSS-oriented storage pattern, suggesting generated media is persisted locally and/or exposed via a configured base URL.
9. Model/vendor integration
Model access is abstracted through settings and selection endpoints rather than hardcoded providers. The repo uses Vercel AI SDK to support multiple LLM vendors, plus a local ONNX path for embeddings and a video provider wrapper under src/utils/ai/video/owned/volcengine.ts. The setting/vendorConfig routes indicate vendors can be added, enabled, tested, and even configured with editable TypeScript logic.
10. Real-time coordination
src/socket/ contains Socket.IO handlers for live updates, especially for agent workflows. This is likely used for progress reporting, memory/chat state, and long-running production tasks such as image generation or video generation, which would otherwise be too slow for simple request/response polling alone.
11. Data and persistence boundary
SQLite is the main local persistence layer, wrapped by utilities in src/utils/db.ts and typed via src/types/database.d.ts. The repository stores durable runtime data under data/ rather than relying on external infrastructure: databases, model files, web assets, skills, and generated media are all local filesystem concerns.
12. Packaging and deployment
The repo is designed to run in three modes: development backend, Electron desktop app, or server/Docker deployment. Dockerfile, electron-builder.yml, scripts/build.ts, and the GitHub Actions workflows show a dual packaging model: a server artifact under data/serve/ for headless deployment, and a desktop distributable for end users.
13. Important architectural boundary
The key boundary is between the orchestration layer and the content-generation layer. Express routes and sockets manage state, persistence, and user actions; agents, prompt skills, embeddings, and vendor adapters perform the AI work. That separation is what allows the same backend to support both local desktop use and remote server deployment.
14. Diagramming hint
For an architecture diagram, place data/skills, data/models, data/web, and SQLite/filesystem storage as internal resources; place LLM/image/video providers as external services; and center the workflow around project → novel → scriptAgent → productionAgent → assets/video export, with Socket.IO as the real-time control channel.
Main entry points: router.ts Router · core.ts Core boot · setting Settings API · production Production API
Components 29
Runtime & API
Express server bootstrap, routing, and request lifecycle.
- App entry · Main process entrypoint that starts the application.
src/app.ts - Core boot · Shared startup and wiring logic.
src/core.ts - Router · Top-level Express route composition.
src/router.ts - Env config · Environment and runtime configuration loading.
src/env.ts - Errors · Central error utilities and normalization.
src/err.ts - Logger · Application logging helpers.
src/logger.ts
AI Agents
Script and production agent orchestration and memory.
- Agents API · Shared agent memory and cleanup endpoints.
src/routes/agents - Script agent API · Script agent plan/state endpoints for the UI.
src/routes/scriptAgent - Script agent · Script reasoning, adaptation, and supervision loop.
src/agents/scriptAgent/index.ts - Production agent · Storyboard and production planning loop.
src/agents/productionAgent/index.ts - Agent memory · Embedding-backed long-lived agent memory layer.
src/utils/agent/memory.ts - Embeddings · Local semantic embedding utilities for memory search.
src/utils/agent/embedding.ts
Creative Workflow
Project, novel, script, production, and asset domains.
- Project API · Project setup, manuals, and project metadata.
src/routes/project - Novel API · Novel import, indexing, and event extraction.
src/routes/novel - Script API · Script generation, updates, exports, and asset extraction.
src/routes/script - Production API · Storyboard, image flow, and workbench/video assembly.
src/routes/production - Assets API · Asset CRUD, upload, polling, and material management.
src/routes/assets - Asset gen · Batch image generation and prompt polishing endpoints.
src/routes/assetsGenerate - Task API · Task categories, task details, and project-linked task views.
src/routes/task
Settings & Ops
Model/vendor config, prompts, skills, login, migration, and maintenance.
- Settings API · Model, prompt, skill, login, deploy, and maintenance settings.
src/routes/setting
Local Data
Shipped assets, skill docs, model bundles, and runtime storage.
- DB init · Database initialization and repair helpers.
src/lib/initDB.ts - SQLite utils · Shared database access wrapper.
src/utils/db.ts - Skill docs · Markdown skill collections for story, art, production, and agents.
data/skills - Embedding model · Local MiniLM ONNX model used for recall embeddings.
data/models/all-MiniLM-L6-v2 - Web UI bundle · Compiled browser UI served by the backend.
data/web - Media assets · Local persisted media such as shipped or generated assets.
data/assets
External Services
Third-party AI and delivery services used by the backend.
- LLM vendors · Configured model providers accessed through the vendor layer.
- Video provider · Video generation provider integration.
- Filesystem · Durable local storage for DB, assets, skills, models, and served files.
Connections 39
- App entry → Core boot: starts · Application entry calls shared boot logic.
- Core boot → Env config: loads · Bootstrapping reads runtime configuration.
- Core boot → Logger: initializes · Sets up logging during startup.
- Core boot → Errors: wires · Registers centralized error handling.
- Core boot → Router: mounts · Attaches the HTTP router and middleware stack.
- Router → Web UI bundle: serves · Express serves the compiled UI bundle.
- Router → Project API: routes · Project endpoints are mounted under the API.
- Router → Novel API: routes · Novel workflow endpoints are mounted.
- Router → Script API: routes · Script workflow endpoints are mounted.
- Router → Production API: routes · Production workflow endpoints are mounted.
- Router → Assets API: routes · Asset management endpoints are mounted.
- Router → Asset gen: routes · Asset generation endpoints are mounted.
- Router → Task API: routes · Task endpoints are mounted.
- Router → Agents API: routes · Shared agent memory endpoints are mounted.
- Router → Script agent API: routes · Script agent UI endpoints are mounted.
- Router → Settings API: routes · Administrative and configuration endpoints are mounted.
- Novel API → Script API: feeds · Novel ingestion flows into script generation.
- Script API → Production API: hands off · Script outputs become production planning inputs.
- Production API → Assets API: uses · Production work produces and manages media assets.
- Assets API → Media assets: stores · Asset endpoints persist media locally.
- Asset gen → LLM vendors: requests · Prompt polishing and image generation can call model vendors.
- Production API → Video provider: renders · Video workbench integrates with a video provider.
- Agents API → Agent memory: manages · Memory endpoints read and clear long-lived agent memory.
- Script agent API → Script agent: controls · UI endpoints expose script-agent planning state.
- Settings API → Skill docs: edits · Skill management reads and updates prompt assets.
- Settings API → LLM vendors: configures · Vendor settings manage provider configuration and tests.
- Settings API → DB init: administers · DB and migration settings act on local persistence.
- Script agent → Skill docs: uses · Script agent behavior is driven by skill prompts.
- Production agent → Skill docs: uses · Production agent behavior is driven by skill prompts.
- Script agent → Agent memory: recalls · Script agent persists and retrieves memory.
- Production agent → Agent memory: recalls · Production agent persists and retrieves memory.
- Agent memory → Embeddings: indexes · Memory search relies on embedding generation.
- Embeddings → Embedding model: loads · Embedding utilities load the local ONNX model bundle.
- DB init → SQLite utils: wraps · Database lifecycle helpers sit on top of the DB utility.
- SQLite utils → Filesystem: persists · SQLite data lives in local filesystem storage.
- Web UI bundle → Filesystem: served from · UI assets are packaged on disk.
- Skill docs → Filesystem: stored on · Prompt skill docs are local filesystem assets.
- Embedding model → Filesystem: stored on · Embedding model bundle is a local filesystem asset.
- Media assets → Filesystem: stored on · Generated and shipped media persist locally.