|
| 1 | +# SecondOrder Architecture Overview |
| 2 | + |
| 3 | +This document gives two shareable views of the current system: |
| 4 | + |
| 5 | +- the product and system architecture |
| 6 | +- the meta-thinking flow used for harder chat requests |
| 7 | + |
| 8 | +## High-Level System |
| 9 | + |
| 10 | +```mermaid |
| 11 | +flowchart LR |
| 12 | + user["User"] |
| 13 | + marketing["Marketing Site<br/>`/`"] |
| 14 | + chatUi["Chat UI<br/>`/chat` and `/chat/[threadId]`"] |
| 15 | + chatApi["Chat API<br/>`app/api/chat/route.ts`"] |
| 16 | + workflow["Meta Chat Workflow<br/>classify -> plan -> draft -> critique"] |
| 17 | + agent["SecondOrder Agent<br/>final streamed response"] |
| 18 | + memory["Mastra Memory Store<br/>thread history + titles"] |
| 19 | + events["Chat Events Store<br/>feedback + analytics events"] |
| 20 | + models["LLM Models<br/>main agent + planner + critic"] |
| 21 | + storage["Storage Backend<br/>Postgres or LibSQL/local file"] |
| 22 | +
|
| 23 | + user --> marketing |
| 24 | + user --> chatUi |
| 25 | + chatUi --> chatApi |
| 26 | + chatApi --> memory |
| 27 | + chatApi --> workflow |
| 28 | + workflow --> models |
| 29 | + workflow --> agent |
| 30 | + agent --> models |
| 31 | + agent --> memory |
| 32 | + chatApi --> events |
| 33 | + memory --> storage |
| 34 | + events --> storage |
| 35 | + chatApi --> chatUi |
| 36 | + chatUi --> user |
| 37 | +``` |
| 38 | + |
| 39 | +### What this means |
| 40 | + |
| 41 | +- The marketing site explains the product and sends people into the chat experience. |
| 42 | +- The chat UI talks only to the Next.js chat API route. |
| 43 | +- The API route is the orchestration boundary: it validates the request, loads thread history, runs the meta workflow, and streams the final answer back to the UI. |
| 44 | +- Mastra provides the workflow engine, agent runtime, memory, and storage integration. |
| 45 | +- Thread history and titles live in Mastra memory storage. |
| 46 | +- Product analytics events like `thread_started`, `meta_mode_used`, and `feedback_submitted` are recorded separately for measurement. |
| 47 | +- Storage can run on Postgres in production or LibSQL/file-backed storage locally. |
| 48 | + |
| 49 | +## Meta-Thinking Process |
| 50 | + |
| 51 | +```mermaid |
| 52 | +flowchart TD |
| 53 | + request["User message arrives"] |
| 54 | + classify["1. Classify task<br/>simple chat, analysis, planning,<br/>decision, or troubleshooting"] |
| 55 | + gate{"Use meta mode?"} |
| 56 | + direct["Respond directly"] |
| 57 | + plan["2. Planner pass<br/>goal, constraints, plan,<br/>response strategy"] |
| 58 | + draft["3. Draft pass<br/>short draft answer"] |
| 59 | + critique["4. Critic pass<br/>confidence, limitations,<br/>context gaps, adjustments"] |
| 60 | + context["5. Build request context<br/>task type + meta guidance"] |
| 61 | + final["6. SecondOrder agent writes<br/>the final user-visible reply"] |
| 62 | + stream["7. Stream answer to UI<br/>with compact metadata"] |
| 63 | + feedback["8. Capture product events<br/>and user feedback"] |
| 64 | +
|
| 65 | + request --> classify |
| 66 | + classify --> gate |
| 67 | + gate -- "No" --> direct |
| 68 | + direct --> final |
| 69 | + gate -- "Yes" --> plan |
| 70 | + plan --> draft |
| 71 | + draft --> critique |
| 72 | + critique --> context |
| 73 | + context --> final |
| 74 | + final --> stream |
| 75 | + stream --> feedback |
| 76 | +``` |
| 77 | + |
| 78 | +### How to explain this to a friend |
| 79 | + |
| 80 | +- SecondOrder does not always use a heavy workflow. It first decides whether the request is simple or whether it needs a more structured pass. |
| 81 | +- For harder requests, it creates a compact plan before answering. |
| 82 | +- It then critiques that draft so the final response can include better framing, clearer limitations, and missing-context signals. |
| 83 | +- The user does not see the full hidden chain-of-thought. They get a normal answer plus compact, useful signals about how the system approached the task. |
| 84 | +- That makes the product feel like a chat assistant with visible reasoning discipline, not just raw text generation. |
| 85 | + |
| 86 | +## Current Building Blocks |
| 87 | + |
| 88 | +- UI: [`app/page.tsx`](/Users/henry/workspace/secondorder-web/app/page.tsx), [`app/chat/page.tsx`](/Users/henry/workspace/secondorder-web/app/chat/page.tsx), [`app/chat/[threadId]/page.tsx`](/Users/henry/workspace/secondorder-web/app/chat/[threadId]/page.tsx) |
| 89 | +- API: [`app/api/chat/route.ts`](/Users/henry/workspace/secondorder-web/app/api/chat/route.ts) |
| 90 | +- Workflow: [`mastra/workflows/meta-chat-workflow.ts`](/Users/henry/workspace/secondorder-web/mastra/workflows/meta-chat-workflow.ts) |
| 91 | +- Agents and memory: [`mastra/agents.ts`](/Users/henry/workspace/secondorder-web/mastra/agents.ts) |
| 92 | +- Runtime wiring: [`mastra/index.ts`](/Users/henry/workspace/secondorder-web/mastra/index.ts) |
| 93 | +- Storage and events: [`lib/chat/history.ts`](/Users/henry/workspace/secondorder-web/lib/chat/history.ts), [`lib/chat/events.ts`](/Users/henry/workspace/secondorder-web/lib/chat/events.ts), [`lib/chat/storage.ts`](/Users/henry/workspace/secondorder-web/lib/chat/storage.ts) |
0 commit comments