Skip to content

Repository files navigation

Cogent

AI agent framework with an interactive terminal UI (Ink/React). The core loop: user input → query rewriting → memory retrieval → context compaction → LLM → tool execution → repeat.

Quick Start

git clone https://github.com/LongPPPP/Coding-Agent.git && cd cogent
npm install
cp .env.example .env
# Edit .env — set LLM_API_KEY to your API key
npm run dev

Prerequisites: Node.js ≥ 18, an API key from a provider with an OpenAI-compatible endpoint (DeepSeek by default).

Commands

Command Description
npm run dev Run with tsx hot-reload
npm run build TypeScript compilation (tsc)
npm start Run compiled JS (node dist/index.js)
npm run pico Run pico task-completion benchmark
PICO_FILTER=text-edit npm run pico Filter benchmark tasks

Configuration

Copy .env.example.env and edit. Key variables:

Variable Default Description
LLM_API_KEY Required — API key for LLM access
MODEL_NAME deepseek-v4-flash Primary model for agent reasoning
MODEL_BASE_URL https://api.deepseek.com OpenAI-compatible API endpoint
AGENT_MAX_ITERATIONS 30 Max LLM iterations per agent run
PROJECT_RULES_FILE COGENT.md Project-level instructions injected into agent prompt

All config is validated at startup — warnings are printed to console if any values look wrong. Full reference: see .env.example for every variable, and src/config.ts for the typed schema.

Project Structure

src/
├── main.tsx              # Ink CLI entry point
├── config.ts             # Configuration (env vars → typed frozen object)
├── agent.ts              # Agent execution loop
├── Tool.ts               # Tool system registry and dispatch
├── commands.ts           # CLI slash-commands (/clear, /exit, /status, ...)
├── db.ts                 # SQLite database init
├── type.ts               # Shared TypeScript types
├── compact/              # Context compaction (microCompact + AutoCompactor)
├── memory/               # Memory system (SQLite + vector embeddings + FTS5)
├── tools/                # Built-in tools
│   ├── FileReadTool/     # File reading with line-range support
│   ├── FileEditTool/     # Exact-string replacement editing
│   ├── FileWriteTool/    # File creation/overwrite
│   ├── GlobTool/         # Fast file pattern matching
│   ├── TodoWriteTool/    # Task list management
│   ├── AgentTool/        # Sub-agent spawning
│   └── PowerShellTool/   # Shell command execution
├── utils/                # LLM client, logger, token counting, sanitizers
├── App/                  # CLI app state management
├── components/           # Ink UI components
├── benchmark/            # Benchmark suite
│   └── pico/             # Task-completion benchmark with fixtures
└── test/                 # Unit and integration tests

Architecture

Agent Loop

Up to AGENT_MAX_ITERATIONS iterations per run. Each iteration: microCompact (rule-based trim) → AutoCompactor (at 85% context, LLM summarizes non-system messages) → LLM call → tool dispatch → inject results → repeat. On no tool calls, finishes and saves conversation to memory.

Memory System

SQLite with vector embeddings (all-MiniLM-L6-v2 via @xenova/transformers) + FTS5 full-text search. Composite retrieval scoring: cosine similarity (45%), BM25 (25%), context/language match (15%), access frequency (10%), time decay (5%). Conversation summaries saved as knowledge after each agent run.

Tool System

Central toolsPool Map registers tools by name + aliases. Each tool defines: schema, execution, result formatting, and security constraints. Results exceeding size limits are saved to disk with a preview returned to the LLM.

Context Compaction

Two-stage: microCompact() (rule-based, strips oldest messages beyond token/age thresholds) then AutoCompactor.compact() (LLM-powered summarization). Full transcripts saved before compression.

CLI

Ink-based React TUI with states: input, thinking (agent status panel with live tool tracking), confirm (tool approval), done (result display). Todo panel shows plan progress. Slash-commands: /clear, /exit, /status, /help, /model, /context.

Key Design Details

  • Token counting via tiktoken; agent logs via Pino
  • Memory DB at data/agent-memory.db (SQLite WAL mode)
  • TypeScript strict mode, ES2020 target, ESNext module resolution
  • Config is deeply frozen at runtime — no accidental mutations

License

ISC

About

Cogent是一款本地代码修改agent,使用ink(react)和TypeScript构建。支持文件的查找、搜索与修改,通过提示词约束和Unicode清洗,拥有一定的防注入能力。同时该agent还具备上下文清理和压缩以及兜底的功能,保证任务完成的同时,还能够有效减少幻觉和Token消耗

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages