How langchain-ai/open-canvas works
Project type and purpose
• Open Canvas is a full-stack, monorepo web application for collaborative document editing powered by autonomous agents.
• Frontend: Next.js web app with serverless API routes, React components, Supabase auth.
• Backend/Agents: LangGraph server (“apps/agents”) implements agent graphs (open-canvas, summarizer, reflection, web-search nodes) that orchestrate LLM calls and memory.
Top-level components
• Client (Browser)
• Frontend Web App (“apps/web”)
– React UI, Next.js pages & API routes
– Workers for streaming graph results
– Supabase client for auth & user data
• Agent Service (“apps/agents”)
– LangGraph server hosting agent graphs
– Node implementations (generateArtifact, reflection, summarizer, web-search, etc.)
– Shared logic in “packages/shared”
– LLM providers (OpenAI, Anthropic, Google GenAI, Fireworks, Ollama)
– Memory store (built-in LangGraph memory, persisted per thread)
• Shared libraries
– “packages/shared”: prompts, models, types, utils
• Evals service (optional)
– “packages/evals” for testing/benchmarking agents
• External services
– Supabase (authentication, user profiles, maybe DB)
– LLM APIs
– FireCrawl (web scraping), ExaSearch, Groq (transcription)
– LangSmith (observability)
Relationships and interactions
A. User → Browser → Next.js frontend
• Renders React UI, handles user input (text/code editor, chats, quick actions).
• Calls Next.js API routes under /api/* for persistence (store/get artifacts, feedback).
• Calls frontend worker (“graph-stream”) to stream agent responses.
B. Frontend API routes → Agent Service (LangGraph)
• The worker or API route sends HTTPS requests to LangGraph server (https://localhost:54367).
• Payload: graph name (e.g. “open-canvas”), initial inputs (current artifact text, user ID, memory tokens).
C. Agent Service internals (deep dive)
1. API receives request → LangGraph CLI routes to appropriate graph.
2. Graph orchestration runs a sequence of nodes:
– generate-artifact: validates input, calls LLM with prompt templates (from packages/shared).
– reflection: optionally runs reflection agent to update memory.
– summarizer, generate-path, thread-title nodes as needed.
3. Each node implementation uses the Langchain SDK (Anthropic, OpenAI clients) or custom tool (FireCrawl).
4. Memory node reads/writes to the graph’s memory store (built-in DB).
5. Final output artifact + updated memory emitted as a streaming response (via SSE).
D. Stream back to Frontend
• Frontend graph-stream worker ingests SSE chunks, updates GraphContext, displays incremental messages in chat UI.
E. Persistence & versioning
• After response complete, Next.js route persists artifact version to Supabase (or LangGraph’s DB).
• Artifacts have version history; UI can fetch previous versions via /api/store/get.
F. Observability & tracing
• LangSmith collects trace data from LangGraph server.
Architectural patterns & design principles
• Separation of concerns: UI vs. API vs. agent orchestration.
• Microservice style: Next.js serverless functions vs. standalone LangGraph server.
• Graph-based orchestration of AI workflows (LangGraph).
• Shared code in monorepo packages for types, prompts, models.
• Streaming data flows (SSE/web workers).
Extra depth on backend response generation
1. Graph request arrives → LangGraph orchestrator
2. Node A: load state (artifact + memory)
3. Node B: format prompt (packages/shared/prompts)
4. Node C: call LLM API (openai.com, anthropic.com)
5. Node D: parse LLM output → transform to artifact delta
6. Node E: update memory store with reflections
7. Node F: return SSE stream of text tokens back to worker
• Calls to multiple model providers via a “Model Provider” sub-module.
• Plug-in points: adding a new model provider or custom tool node.