App RouterFirebase authPrisma + PostgresS3 syncNeo4j graphRazorpay billingAzure Vision OCRGemini / OpenAI / Groq

Codebase map

InsdrWiki Docs

This repository is a hybrid product: a public funnel, an authenticated document workspace, an AI agent backend, an OCR and locate pipeline, and a usage and billing control plane. The landing page tells one story, but the operational center of the app is the synced file tree, chat agent, graph viewer, and REST surface around them.

What it actually does

  • Browses a user-scoped file tree and lets you edit files in place.
  • Chats with a Pi-powered agent that can inspect, transform, and export sessions.
  • Splits PDFs into page images, OCRs those pages, and finds passages on demand.
  • Builds and queries a graph of documents and relationships in Neo4j.
  • Tracks usage, credits, subscriptions, and API cost across the whole stack.

Main systems

Frontend

App Router pages, explorer, chat, locate, cost.

Backend

Route handlers, SSE agent sessions, REST tools.

Data

Prisma tables, S3 objects, OCR artifacts, graph edges.

Payments

Firebase auth plus Razorpay subscriptions and credits.

Overview

What this repository is doing

The codebase combines a public acquisition funnel with a private workspace that syncs files, runs AI, and keeps usage and billing in sync.

Public surface

The landing page, signup flow, subscription page, and credit pack page are all geared toward getting a user into the product and keeping the credit meter moving.

Workspace surface

The authenticated workspace is where the product actually lives: file tree, inline editor, graph view, chat panel, upload flows, and OCR-backed document navigation.

Routing

User-facing pages

These are the routes people actually see. Each one is backed by a page file under app/.

/

Landing page

Public marketing funnel and the first entry point into the product.

/signup

Signup

Firebase sign-up overlay that provisions the user record and free credits.

/subscribe

Subscribe

Razorpay subscription checkout with trial setup and billing state updates.

/credits

Credits

One-time credit pack purchase flow with Razorpay order creation and verification.

/app

Workspace

Authenticated file explorer, editor, graph view, and chat panel.

/manual

Manual parser

Parses pasted text or JSON into locate-compatible video and PDF matches.

/locate

Locate

OCR-backed page browser and passage finder for scanned pages and notes.

/cost

Cost analysis

Token, cost, and credit accounting across sessions and API calls.

/view/[sessionId]

Session trace

Redirects to the local session viewer for a single chat session.

Backend

API route groups

Most of the app's behavior is routed through app/api/*. These groups are the main clusters the UI and agent depend on.

Identity and billing

/api/auth/session/api/provision/api/subscribe/api/subscription/status/api/credits/api/credits/purchase/api/credits/verify/api/webhook/razorpay

Workspace storage

/api/sync/api/files/upload/api/files/save/api/files/content/api/files/serve/api/files/move/api/files/delete/api/files/split-pdf/api/textbook

Locate, OCR, graph

/api/locate/api/locate/find/api/locate/embed/api/vision/api/vision/polygons/api/graph

Agent and history

/api/agent/api/chat/api/chat/export/api/cost/api/manual

Workspace

Files, explorer, and the main editing loop

The file explorer is a lightweight file manager with direct edit, move, rename, duplicate, and sync operations.

Core UI files

  • app/app/page.tsx mounts the workspace.
  • app/components/file-explorer/FileExplorer.tsx owns the layout.
  • app/components/chat/ChatPanel.tsx handles agent chat.

Key abilities

  • Rename, move, delete, duplicate, and create files or folders.
  • Upload textbook PDFs and handwritten notes.
  • Preview text, code, images, PDFs, videos, and graphs.

Shortcuts

Cmd/Ctrl+B

Toggle the file explorer sidebar.

Cmd/Ctrl+S

Save every dirty file in the workspace.

Shift+Tab

Cycle the thinking level in chat.

/chat ...

First message can restart the agent in general mode.

Agent

Chat, sessions, and the SSE control plane

The agent backend is separated into route handling, session management, custom tools, and request-scoped context. That separation is intentional.

Agent files

  • app/api/agent/route.ts exposes SSE plus session commands.
  • app/api/agent/sessions.ts owns lifecycle, persistence, and tool wiring.
  • app/api/agent/custom-tools.ts defines the tool registry.
  • app/api/agent/context.ts injects request-scoped uid and session ids.

Session flow

start

Create a new in-memory agent session and attach a DB session id.

resume

Recreate an in-memory session for an existing DB session.

command

Send a model, thinking, or prompt command to the live agent.

kill

Terminate an agent session.

abort

Abort the currently running turn.

Locate

OCR, passage matching, and manual parsing

One of the most distinctive parts of the app: split PDFs into page images, OCR those pages, and then find the best contiguous passage for a query.

Locate flow

  • /api/files/split-pdf renders PDFs into numbered page PNGs.
  • /api/vision/polygons OCRs page images and writes boundary JSON.
  • /api/locate/embed precomputes embeddings for scoped pages.
  • /api/locate/find asks Gemini to select one contiguous match.

Manual parser

The /manual page feeds free-form text into a Groq-backed parser that extracts PDF and video matches into the same locate result shape used by chat and the file explorer.

Useful when you want to paste a transcript, a YouTube link, or OCR-like notes and immediately jump to the matching media.

Graph

Graph view and data model

The app stores both relational records and graph edges. The graph viewer is a visual front end over Neo4j.

Graph surfaces

  • /api/graph returns nodes and edges for the viewer.
  • app/components/file-explorer/viewer/GraphViewer.tsx renders the force graph.
  • Tools like graph_query, cypher_query, and insert_relationship mutate it.

Prisma schema

  • User, CreditTransaction, ChatSession, and ChatMessage power billing and chat history.
  • FsNode, FsSnapshot, SyncEvent, and TreeIndex support file indexing and sync history.
  • ApiCallLog tracks token usage and provider/model costs for reporting.

Billing

Auth, credits, subscription, and webhook flow

Firebase provides identity, Prisma stores the ledger, and Razorpay drives both subscriptions and credit purchases.

Auth session

/api/auth/session turns a Firebase ID token into a session cookie, and lib/auth-context.tsx syncs that cookie on sign-in and clears it on sign-out.

Provisioning

/api/provision creates the S3 userdata prefix and grants the signup bonus credits on first sign-in.

Payments

Subscription and one-time credit purchases both go through Razorpay, with webhook-driven status updates for subscription state.

REST tools

Tools that can be run over REST

The agent tool registry is directly exposed through /api/tools and /api/tools/[tool], so the same capabilities available to the agent can be called from HTTP.

Invocation pattern

GET  /api/tools
POST /api/tools/[tool]

// body
{
  "args": { ... }
}

The handler returns { ok, result } and parses JSON when the tool returns JSON text. That makes it easy to script the same operations the agent uses.

Example calls

curl http://localhost:3000/api/tools

curl -X POST http://localhost:3000/api/tools/split_pdf_to_pages \
  -H 'Content-Type: application/json' \
  -d '{"args":{"pdf_path":"Notes/demo.pdf","wait":false}}'

curl -X POST http://localhost:3000/api/tools/graph_status \
  -H 'Content-Type: application/json' \
  -d '{"args":{}}'

Model control

set_primary_model_profileselect_subagent_model_profile

Ingest and reading

ingest_directorysplit_pdf_to_pageslist_pdf_pagesparse_page_boundariesread_documentread_boundary

Persistence and graph

upsert_documentpatch_documentinsert_relationshipsql_querygraph_querycypher_querygraph_statusgraph_search

Retrieval and analysis

keyword_searchsubagent_searchweb_searchweb_extractanalyze_content

Batch variants

read_documents_batchread_boundaries_batchsql_queries_batchcypher_queries_batchkeyword_searches_batchsubagent_searches_batchgraph_searches_batchupsert_documents_batchpatch_documents_batchinsert_relationships_batchanalyze_contents_batch

Repo map

Other code and support files worth knowing

These files explain the repo beyond the UI. They are useful when you want to understand the product model or the offline tooling.

Framework and config

  • Next.js 16 App Router is the routing model.
  • next.config.ts ignores TS build errors for the external agent package.
  • A rewrite maps /models.json to /gemini/models.json.
  • serverExternalPackages includes Pi, Prisma adapter, Postgres, and Neo4j packages.

Reference docs

  • AGENTS.md and CLAUDE.md describe how the agent should treat the repo.
  • system.md, llmwiki.md, and sdk.md explain the knowledge-base and SDK patterns.
  • ref/ contains auth notes, SDK references, and debug artifacts.

Offline tooling

  • utils/sync_userdata.py, utils/migrate_postgres.py, and utils/run_cypher.py support data movement.
  • prisma/*.sql appears to hold import and migration helpers.
  • public/userdata/ contains the real corpus, OCR artifacts, transcripts, notes, and media.

Finish

Operational summary

If you remember one thing from this repo, remember this: it is a document workspace with an agent and a billing loop, not just a chat demo.

The codebase is centered on turning uploaded or synced content into a searchable, navigable knowledge system. Users sign in with Firebase, receive credits, browse and edit files, chat with an agent, run OCR and locate flows over PDFs and notes, and inspect costs and graph data as the workspace grows.

The agent stack is especially important: app/api/agent/sessions.ts handles lifecycle and persistence, app/api/agent/custom-tools.ts exposes the toolbelt, and app/api/tools gives you a REST path to invoke those same tools directly.