# goatninja10/unify-ai architecture

> Architecture diagram of the GitHub repository goatninja10/unify-ai, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-07-24). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/goatninja10/unify-ai
- Repository: https://github.com/goatninja10/unify-ai

## Overview

##### Project Type and Purpose
- This is a full-stack web application built with Next.js (frontend + backend API routes) that provides a unified interface for interacting with multiple large language models (LLMs).
- It integrates headless browser automation (Playwright) for the Scira model and calls external AI services (Together AI for DeepSeek R1 and Meta-LLaMA).
- It persists user sessions and chat history in a local SQLite database via Prisma ORM.

##### File Structure Analysis
- Top-level:
- app/ – Next.js “app” directory containing UI pages, layouts, and API routes under app/api.
- components/ui/ – ShadCN/Tailwind UI primitives.
- lib/ – utility functions and type definitions.
- prisma/ – database schema, migration files, and dev.db.
- public/, styles/, tailwind.config.ts – styling and static assets.
- next.config.mjs, tsconfig.json – framework and TypeScript config.

- Key patterns:
- API routes for ai-chat, auth (NextAuth), health.
- Prisma for data access.
- Playwright wrapper invoked in the ai-chat route.
- Markdown processing pipeline using remark plugins.

##### README Insights
- Multi-model modes: scira, deepseek, chained, meta-llama.
- Chaining logic (Scira → cleanup → Together AI).
- Tech stack: Next.js 15, React 19, Tailwind, ShadCN, Playwright, remark, Prisma, SQLite.

##### Main Components
A. Client Browser (Next.js React SPA)
B. Next.js Server (Node.js)
1. API Layer
- /api/ai-chat route
- /api/auth routes (NextAuth)
- /api/health route
2. Service Modules
- Playwright Scira Scraper
- Markdown Processor (remark-gfm, remark-math, cleanup logic)
- Together AI Client for DeepSeek R1 & Meta-LLaMA
3. Database Access
- Prisma ORM → SQLite (dev.db)
C. External Services
- Scira website (via headless Chromium)
- Together AI endpoints (DeepSeek R1, Meta-LLaMA)

##### Relationships and Data Flows
- User in browser interacts with React components → triggers ai-chat fetch.
- Next.js API route receives POST /api/ai-chat.
- Depending on mode:
- If “scira” or “chained”: spawn Playwright to scrape Scira; handle timeouts.
- Clean up Scira’s markdown output.
- If “deepseek” or “chained”: forward cleaned output to Together AI.
- Optional Meta-LLaMA calls for meta-llama modes.
- Aggregate responses, store conversation record via Prisma.
- Return JSON with per-model responses and final output.
- Frontend renders finalOutput, history, metrics.

##### Architectural Patterns and Design Principles
- Clear separation of concerns: UI layer, API layer, service wrappers, data access.
- Modular service clients for each LLM integration.
- Single database for user/session/chat persistence.
- Error handling and retry logic encapsulated in service modules.
- Pipeline composition for chained operations.

##### Final Advice
- Conditional paths (scira only vs. deepseek vs. chained).

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Client
    BrowserUI["Browser UI\n(Next.js React SPA)"]:::frontend

    %% Next.js Server
    subgraph "Next.js Server"
        subgraph "API Layer"
            AIChat["/api/ai-chat"]:::backend
            Auth["/api/auth/[...nextauth]"]:::backend
            Register["/api/auth/register"]:::backend
            Health["/api/health"]:::backend
        end
        subgraph "Service Modules"
            Playwright["Playwright Scira Scraper"]:::backend
            Markdown["Markdown Processor"]:::backend
            Together["Together AI Client"]:::backend
        end
        subgraph "Database Access"
            Prisma["Prisma ORM"]:::backend
            SQLite["SQLite Database"]:::db
        end
    end

    %% External Services
    SciraExt["Scira Website"]:::external
    TogetherExt["Together AI"]:::external

    %% Flows
    BrowserUI -->|UI interactions| AIChat
    BrowserUI -->|auth requests| Auth
    AIChat -->|calls| Playwright
    Playwright -->|scrape| SciraExt
    AIChat -->|cleanup pipeline| Markdown
    Markdown -->|cleaned markdown| Together
    Together -->|API call| TogetherExt
    AIChat -->|read/write| Prisma
    Prisma -->|connect| SQLite
    Auth -->|session store| Prisma
    AIChat -->|response JSON| BrowserUI

    %% Click Events
    click BrowserUI "https://github.com/goatninja10/unify-ai/blob/main/app/layout.tsx"
    click BrowserUI "https://github.com/goatninja10/unify-ai/blob/main/app/page.tsx"
    click BrowserUI "https://github.com/goatninja10/unify-ai/blob/main/app/main/page.tsx"
    click BrowserUI "https://github.com/goatninja10/unify-ai/tree/main/components/ui/"
    click AIChat "https://github.com/goatninja10/unify-ai/blob/main/app/api/ai-chat/route.ts"
    click Auth "https://github.com/goatninja10/unify-ai/blob/main/app/api/auth/[...nextauth]/route.ts"
    click Register "https://github.com/goatninja10/unify-ai/blob/main/app/api/auth/register/route.ts"
    click Health "https://github.com/goatninja10/unify-ai/blob/main/app/api/health/route.ts"
    click Playwright "https://github.com/goatninja10/unify-ai/blob/main/lib/utils.ts"
    click Markdown "https://github.com/goatninja10/unify-ai/blob/main/lib/process-cleanup.ts"
    click Together "https://github.com/goatninja10/unify-ai/blob/main/lib/utils.ts"
    click Prisma "https://github.com/goatninja10/unify-ai/blob/main/prisma/schema.prisma"
    click SQLite "https://github.com/goatninja10/unify-ai/blob/main/prisma/dev.db"

    %% Styles
    classDef frontend fill:#f9f,stroke:#333,stroke-width:1px
    classDef backend fill:#bbf,stroke:#333,stroke-width:1px
    classDef db fill:#bfb,stroke:#333,stroke-width:1px
    classDef external fill:#eee,stroke:#333,stroke-width:1px
```
