$ show CherryHQ/cherry-studio
CherryStudio is a cross-platform desktop AI assistant built with Electron and React. It provides a unified chat interface that supports dozens of LLM providers (OpenAI, Anthropic, Google, Groq, local models via Ollama, etc.) through a pluggable provider registry and the Vercel AI SDK. Beyond chat, it offers knowledge base management with RAG (vector indexing via sqlite-vec, chunking, embedding), file processing (OCR with Tesseract/PaddleOCR, document parsing), web search, image generation, MCP (Model Context Protocol) tool integration, and code CLI management (Claude Code, OpenAI Codex). The application is organized as a pnpm monorepo with separate packages for the UI component library, AI core runtime, provider registry, and tracing infrastructure. Architecturally, the app follows an Electron main/renderer process split with typed IPC communication using Zod schemas. The main process handles AI inference, database operations (SQLite via Drizzle ORM), file management, and native integrations. The renderer is a React SPA using TanStack Router, SWR for data fetching, and a custom UI component library with extensive icon sets for providers and models. The AI runtime layer abstracts provider-specific details through adapters and supports streaming, tool calls, agent sessions, and context compaction. Key data flows include: user input → composer → IPC → AI runtime → provider API → streaming response → renderer. Knowledge base ingestion follows: file upload → OCR/parsing → chunking → embedding → vector store. The application also supports offline usage with local embedding models and ONNX runtime. External dependencies include native binaries for OCR and vector search, and the AI SDK ecosystem.
The LLM identified 5 files worth highlighting for reuse:
Implements a context compaction strategy for AI conversations. It intelligently summarizes or truncates older messages to fit within token limits while preserving essential context. The algorithm uses a sliding window approach with configurable thresholds and supports multiple compaction strategies (summarization, truncation, offloading). This is a self-contained module that could be extracted into any AI chat application needing context window management.
A React component that renders markdown incrementally as it streams from an AI response. It uses a custom parser that can handle partial tokens and progressively renders headings, code blocks, math (LaTeX), and Mermaid diagrams. The component supports plugins for syntax highlighting, SVG scaling, and heading anchors. This is a highly reusable pattern for any chat application that needs real-time markdown rendering with minimal flicker.
A pure function that flattens a nested tree structure into a flat array with depth and visibility metadata. It handles expanded/collapsed state, indentation, and supports both depth-first and breadth-first traversal. The algorithm is generic and can be reused in any tree UI component (file explorers, nested menus, org charts) without framework dependencies.
Derives reasoning model families from provider metadata using heuristic rules and a manually curated family map. It classifies models into families (e.g., 'deepseek-r1', 'o1', 'claude-thinking') based on model ID patterns and provider-specific overrides. This module encapsulates complex reasoning model detection logic that would otherwise be scattered across the codebase, and can be reused by any application needing to identify reasoning model capabilities.
A pure utility function that appends an internal agent continuation message after a text-only assistant response. It guards against mutation by returning the same reference for no-op cases and uses structured cloning for safety. This pattern is reusable in any agent system that needs to inject hidden continuation messages without affecting the visible conversation history.