MathyAgent is a TypeScript monorepo for a structured math-solving agent. It turns text questions or screenshot inputs into validated math solution objects, then renders the result as readable steps, LaTeX, graphs, and verification output in a quiet academic workbench UI.
The project is built around a strict boundary between AI model calls and product rendering: model providers are hidden behind a ModelAdapter, API responses are validated with shared Zod schemas, and the frontend renders structured fields instead of parsing free-form model text.
- Structured math contracts for recognized problems, solution steps, final answers, LaTeX blocks, graph specs, and verification results.
- Provider adapter layer for fake, Yunwu-compatible, and DeepSeek-compatible model flows.
- Node
httpAPI with endpoints for solving, history lookup, and health checks. - React 19 + Vite web app with Chinese-first chat workflow, history sidebar, text input, image URL input, and pasted screenshot support.
- KaTeX-backed LaTeX rendering through a dedicated renderer component.
- SVG graph rendering from explicit
GraphSpecdata, including 2D and 3D structured graph contracts. - File-backed local solve history for development without introducing a database dependency.
- Vitest coverage across shared schemas, API behavior, adapters, repositories, and frontend rendering.
- TypeScript monorepo with npm workspaces
- React 19, Vite, and global CSS
- Node.js
httpAPI server - Zod shared validation schemas
- Vitest and jsdom for testing
- KaTeX for LaTeX rendering
apps/web/ React frontend
services/api/ HTTP API, solver orchestration, repositories, adapters
packages/shared/ Shared contracts, schemas, and adapter interfaces
docs/ Architecture notes
flowchart LR
User["User input: text or screenshot"] --> Web["apps/web"]
Web --> Api["services/api"]
Api --> Solver["SolveOrchestrator"]
Solver --> Adapter["ModelAdapter"]
Adapter --> Provider["AI provider"]
Solver --> Repository["SolveRepository"]
Api --> Web
Web --> Latex["LatexBlockView"]
Web --> Graph["GraphSpecView"]
Web --> Verify["VerificationPanel"]
Key design rules:
- AI provider calls go through
ModelAdapter. - Route handlers and frontend code never call providers directly.
- Every solve result includes a
VerificationResult. - LaTeX is represented as
LatexBlockwithdisplayMode: true. - Graphs are represented as
GraphSpec; the frontend does not infer graphs from prose. - Shared Zod schemas validate adapter output before it reaches the UI.
- Node.js 20 or newer
- npm 10 or newer
npm installCreate a local .env from the example file:
cp .env.example .envThe default configuration uses the fake provider and does not require an API key:
MODEL_PROVIDER=fake
PORT=3001For a real provider, set MODEL_PROVIDER, MODEL_NAME, MODEL_BASE_URL, and the matching API key in .env. Do not commit real secrets.
Start the API:
npm run dev:apiStart the web app in another terminal:
npm run dev:webBy default:
- API:
http://127.0.0.1:3001 - Web:
http://127.0.0.1:5173
npm run typecheck
npm test
npm run buildWorkspace-specific scripts are also available:
npm run dev -w @mathyagent/api
npm run dev -w @mathyagent/webReturns service status.
Solves a math problem from recognized text or an image URL.
{
"uploadId": "upload_123",
"recognizedText": "解方程:x + 1 = 2",
"locale": "zh-CN"
}The response is a validated SolveResult containing:
problem: recognized and normalized problem fieldssolution: summary, ordered steps, final answer, LaTeX blocks, and graph specsverification: verification status, confidence, checks, and issues
Returns a previous solve result by ID.
Returns locally persisted solve history.
- Durable contracts belong in
packages/shared. - Math renderers belong in
apps/web/src/components/math. - Solver workflow belongs in
services/api/src/services/solver. - Provider-specific code belongs in
services/api/src/model-adapters. - Local history is written to
.mathyagent/and is intentionally ignored by Git.
- Never commit
.envor provider keys. - Keep model prompts and adapter responses server-side.
- Do not expose provider keys to frontend code.
- Validate provider responses with shared schemas before returning them to clients.
MathyAgent is an MVP-stage structured math-agent workbench. The current implementation prioritizes clean contracts, safe provider boundaries, deterministic rendering, and a runnable local development loop.
Released under the MIT License. See LICENSE for details.