codn_ts
Version:
智能代码分析工具 - 支持语义搜索、调用链分析和代码结构可视化,对大模型/AI agent 友好
281 lines (220 loc) • 6.28 kB
Markdown
[](https://www.typescriptlang.org/)
[](https://nodejs.org/)
[](https://github.com/dweb-lab/codn_ts)
**codn_ts** 是一个基于 TypeScript 的智能代码分析工具,支持作为库和 CLI 工具使用,对大模型/AI agent 友好。
- **5维度加权评分系统**:精确匹配、语义关键词、上下文相关性、依赖关系、使用频率
- **50+ 语义类别**:认证、数据处理、网络、数据库、文件操作、错误处理等
- **LSP 集成**:准确的代码结构分析
- **JSON 输出**:AI 友好的结构化数据
- **文件/符号/内容/语义搜索**
- **引用关系分析**:函数/类/变量引用关系
- **符号引用分析**:`cod ref` 支持精准分析任意符号的调用方/被调方
- **诊断检查**:`cod check` 基于 LSP 输出语法/类型错误和警告
- **调用链可视化**:生成 Mermaid 图表
- **项目统计**:代码质量分析
- **目录列表**、**文件查看**、**语言检测**
- **MCP 协议支持**:可被 Claude Desktop/Agent 等 AI 工具远程调用
```bash
npm install -g codn_ts
npm install codn_ts
```
```bash
cod search "authentication" --semantic --json
cod analyze --json
cod ref -s "main" --json
cod check
cod check --file src/index.ts --json
cod analyze --call-chain --from "main" --format mermaid --output callchain.md
cod ls --long --json
cod cat src/semantic_search.ts --json
```
```bash
cod search "functionName"
cod search "authentication" --semantic
cod search "api" --type file,symbol,content
cod search "database" --semantic --json
```
```bash
cod analyze
cod analyze --call-chain --from "main"
cod analyze --call-chain --format mermaid --output diagram.md
cod analyze --json
```
```bash
cod ref -s "authenticateUser"
cod ref -s "UserService" --json
cod ref -s "main" --format mermaid --output ref.md
```
```bash
cod check
cod check --file src/index.ts
cod check --json
```
```bash
cod ls
cod ls --long
cod ls --recursive
cod ls --json
```
```bash
cod cat src/index.ts
cod cat src/index.ts --line-numbers
cod cat src/index.ts --json
```
```typescript
import {
searchProject,
analyzeProjectReferences,
listDirectory,
readFile,
semanticSearch
} from 'codn_ts';
// 语义搜索
const results = await semanticSearch(projectPath, "authentication", {
semantic: true,
json: true
});
// 分析引用关系
const references = await analyzeProjectReferences(projectPath);
// 搜索项目
const searchResults = await searchProject(projectPath, "api", {
types: ['file', 'symbol', 'content']
});
// 列出目录
const files = await listDirectory(projectPath, {
long: true,
recursive: true
});
// 读取文件
const content = await readFile(filePath, {
lineNumbers: true
});
```
```
src/
├── index.ts
├── cli.ts
├── search.ts
├── semantic_search.ts
├── callChain.ts
├── ls.ts
├── cat.ts
└── utils/
├── lsp_core.ts
├── os_utils.ts
├── reference_analyzer.ts
└── symbol_utils.ts
```
```bash
npm test
npm run test:coverage
```
当前测试状态:
- ✅ 57个测试用例全部通过
- ✅ 核心模块覆盖率:96%+
- ✅ 生产就绪质量
```bash
git clone https://github.com/dweb-lab/codn_ts.git
cd codn_ts
npm install
npm run build
```
```bash
npm run dev
npm run format
npm run lint
```
```typescript
// 为 AI 提供代码上下文
const context = await semanticSearch(projectPath, userQuery, {
semantic: true,
json: true,
limit: 10
});
```
```bash
cod analyze --call-chain --from "processPayment" --format mermaid
```
```bash
cod search "deprecatedAPI" --semantic --type symbol,content
```
```bash
cod analyze --call-chain --format mermaid --output docs/architecture.md
```
- `--semantic`: 启用语义搜索
- `--type`: 指定搜索类型 (file|symbol|content)
- `--limit`: 限制结果数量
- `--json`: JSON 格式输出
- `--call-chain`: 生成调用链
- `--from`: 指定起始函数
- `--format`: 输出格式 (json|mermaid|text)
- `--output`: 输出文件路径
我们欢迎贡献!请查看我们的 [贡献指南](CONTRIBUTING.md)。
MIT © askender
- [x] 核心搜索功能
- [x] 语义搜索
- [x] 调用链分析
- [x] CLI 工具
- [x] 测试覆盖
- [ ] Web 可视化界面
- [ ] 插件系统
- [ ] 多语言支持
- [ ] 企业级功能
- 支持 MCP 协议,可通过 HTTP JSON-RPC 远程调用 `codn_ref`(符号引用分析)、`codn_check`(诊断检查)等能力
- 适配 Claude Desktop/Agent、MCP Inspector 等 AI 工具生态
- 详见 CLI_GUIDE.md/ API.md 示例