- 添加项目配置文档 (AGENTS.md) - 创建 package.json,包含环信 SDK 及测试依赖 - 添加环信 polyfill 工具模块 - 添加双用户消息互发测试脚本 - 添加 polyfill 测试脚本 Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
9.6 KiB
CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
Build and Development Commands
# Development - starts Vite dev server (port 5175) + Electron app with hot reload
npm run electron:dev
# Build production bundle (TypeScript + Vite)
npm run build
# Lint with ESLint
npm run lint
# Run memory extractor tests (Node.js built-in test runner)
npm run test:memory
# Compile Electron main process only
npm run compile:electron
# Package for distribution (platform-specific)
npm run dist:mac # macOS (.dmg)
npm run dist:win # Windows (.exe)
npm run dist:linux # Linux (.AppImage)
Requirements: Node.js >=24 <25. Windows builds require PortableGit (see README.md for setup).
Architecture Overview
OriginClawAI is an Electron + React desktop application with two primary modes:
- Cowork Mode - AI-assisted coding sessions using Claude Agent SDK with tool execution
- Artifacts System - Rich preview of code outputs (HTML, SVG, React, Mermaid)
Uses strict process isolation with IPC communication.
Process Model
Main Process (src/main/main.ts):
- Window lifecycle management
- SQLite storage via
sql.js(src/main/sqliteStore.ts) - Cowork session runner (
src/main/libs/coworkRunner.ts) - executes Claude Agent SDK - IPC handlers for store, cowork, and API operations
- Security: context isolation enabled, node integration disabled, sandbox enabled
Preload Script (src/main/preload.ts):
- Exposes
window.electronAPI viacontextBridge - Includes
coworknamespace for session management and streaming events
Renderer Process (React in src/renderer/):
- All UI and business logic
- Communicates with main process exclusively through IPC
Key Directories
src/main/
├── main.ts # Entry point, IPC handlers
├── sqliteStore.ts # SQLite database (kv + cowork tables)
├── coworkStore.ts # Cowork session/message CRUD operations
└── libs/
├── coworkRunner.ts # Claude Agent SDK execution engine
├── coworkVmRunner.ts # Sandbox VM execution mode
├── claudeSdk.ts # SDK loader utilities
├── coworkMemoryExtractor.ts # Extracts memory changes from conversations
└── coworkMemoryJudge.ts # Validates memory candidates with scoring/LLM
src/renderer/
├── types/cowork.ts # Cowork type definitions
├── store/slices/
│ ├── coworkSlice.ts # Cowork sessions and streaming state
│ └── artifactSlice.ts # Artifacts state
├── services/
│ ├── cowork.ts # Cowork service (IPC wrapper, Redux integration)
│ ├── api.ts # LLM API with SSE streaming
│ └── artifactParser.ts # Artifact detection and parsing
├── components/
│ ├── cowork/ # Cowork UI components
│ │ ├── CoworkView.tsx # Main cowork interface
│ │ ├── CoworkSessionList.tsx # Session sidebar
│ │ ├── CoworkSessionDetail.tsx # Message display
│ │ └── CoworkPermissionModal.tsx # Tool permission UI
│ └── artifacts/ # Artifact renderers
SKILLs/ # Custom skill definitions for cowork sessions
├── skills.config.json # Skill enable/order configuration
├── docx/ # Word document generation skill
├── xlsx/ # Excel skill
├── pptx/ # PowerPoint skill
└── ...
Data Flow
- Initialization:
src/renderer/App.tsx→coworkService.init()→ loads config/sessions via IPC → sets up stream listeners - Cowork Session: User sends prompt →
coworkService.startSession()→ IPC to main →CoworkRunner.startSession()→ Claude Agent SDK execution → streaming events back to renderer via IPC → Redux updates - Tool Permissions: Claude requests tool use →
CoworkRunneremitspermissionRequest→ UI showsCoworkPermissionModal→ user approves/denies → result sent back to SDK - Persistence: Cowork sessions stored in SQLite (
cowork_sessions,cowork_messagestables)
Cowork System
The Cowork feature provides AI-assisted coding sessions:
Execution Modes (CoworkExecutionMode):
auto- Automatically choose based on contextlocal- Run tools directly on the local machinesandbox- Run tools in isolated VM environment
Memory System: Automatically extracts and manages user memories from conversations:
coworkMemoryExtractor.ts- Detects explicit remember/forget commands (Chinese/English) and implicitly extracts personal facts using signal patterns (profile, preferences, ownership). Uses guard levels (strict/standard/relaxed) with confidence thresholds.coworkMemoryJudge.ts- Validates memory candidates with rule-based scoring and optional LLM secondary judgment for borderline cases. Includes TTL-based caching for LLM results.
Stream Events (IPC from main to renderer):
message- New message added to sessionmessageUpdate- Streaming content update for existing messagepermissionRequest- Tool needs user approvalcomplete- Session execution finishederror- Session encountered an error
Key IPC Channels:
cowork:startSession,cowork:continueSession,cowork:stopSessioncowork:getSession,cowork:listSessions,cowork:deleteSessioncowork:respondToPermission,cowork:getConfig,cowork:setConfig
Key Patterns
- Streaming responses:
apiService.chat()uses SSE withonProgresscallback for real-time message updates - Cowork streaming: Uses IPC event listeners (
onStreamMessage,onStreamMessageUpdate, etc.) for bidirectional communication - Markdown rendering:
react-markdownwithremark-gfm,remark-math,rehype-katexfor GitHub markdown and LaTeX - Theme system: Class-based Tailwind dark mode, applies
darkclass to<html>element - i18n: Simple key-value translation in
services/i18n.ts, supports Chinese (default) and English. Language auto-detected from system locale on first run. - Path alias:
@maps tosrc/renderer/in Vite config for imports. - Skills: Custom skill definitions in
SKILLs/directory, configured viaskills.config.json
Artifacts System
The Artifacts feature provides rich preview of code outputs similar to Claude's artifacts:
Supported Types:
html- Full HTML pages rendered in sandboxed iframesvg- SVG graphics with DOMPurify sanitization and zoom controlsmermaid- Flowcharts, sequence diagrams, class diagrams via Mermaid.jsreact- React/JSX components compiled with Babel in isolated iframecode- Syntax highlighted code with line numbers
Detection Methods:
- Explicit markers:
```artifact:html title="My Page" - Heuristic detection: Analyzes code block language and content patterns
UI Components:
- Right-side panel (300-800px resizable width)
- Header with type icon, title, copy/download/close buttons
- Artifact badges in messages to switch between artifacts
Security:
- HTML:
sandbox="allow-scripts"with noallow-same-origin - SVG: DOMPurify removes all script content
- React: Completely isolated iframe with no network access
- Mermaid:
securityLevel: 'strict'configuration
Configuration
- App config stored in SQLite
kvtable - Cowork config stored in
cowork_configtable (workingDirectory, systemPrompt, executionMode) - Cowork sessions and messages stored in
cowork_sessionsandcowork_messagestables - Database file:
originclawai.sqlitein user data directory
TypeScript Configuration
tsconfig.json: React/renderer code (ES2020, ESNext modules)electron-tsconfig.json: Electron main process (CommonJS output todist-electron/)
Key Dependencies
@anthropic-ai/claude-agent-sdk- Claude Agent SDK for cowork sessionssql.js- SQLite database for persistencereact-markdown,remark-gfm,rehype-katex- Markdown rendering with math supportmermaid- Diagram renderingdompurify- SVG/HTML sanitization
Coding Style & Naming Conventions
- Use TypeScript, functional React components, and Hooks; keep logic in
src/renderer/services/when it is not UI-specific. - Match existing formatting: 2-space indentation, single quotes, and semicolons.
- Naming:
PascalCasefor components (e.g.,Chat.tsx),camelCasefor functions/vars, and*Slice.tsfor Redux slices. - Tailwind CSS is the primary styling approach; prefer utility classes over bespoke CSS.
Testing Guidelines
- Tests use Node.js built-in
node:testmodule (no Jest/Mocha/Vitest). - Run tests:
npm run test:memory(compiles Electron main process first, then runstests/coworkMemoryExtractor.test.mjs). - Test files live in
tests/directory and import compiled output fromdist-electron/. - Validate UI changes manually by running
npm run electron:devand exercising key flows:- Cowork: start session, send prompts, approve/deny tool permissions, stop session
- Artifacts: preview HTML, SVG, Mermaid diagrams, React components
- Settings: theme switching, language switching
- Keep console warnings/errors clean; lint via
npm run lintbefore submitting.
Commit & Pull Request Guidelines
- Recent history uses conventional prefixes like
feat:,refactor:, andchore:; older commits includefeature:andInitial commit. - Prefer
type: short imperative summary(e.g.,feat: add artifact toolbar actions). - PRs should include a concise description, linked issue if applicable, and screenshots for UI changes.
- Call out any Electron-specific behavior changes (IPC, storage, windowing) in the PR description.