# Code Feeder

> 当用户需将代码提供给外部 AI（重构/分析/修复）时激活。自动收集代码、记录意图、生成上下文文档。支持增量合并。

- Skill: `tools-only/code-feeder` (Agent Skill, multi-file: 3 files)
- Install (CLI): `npx skillmds add tools-only/code-feeder`
- Raw SKILL.md: https://api.skillmd.com/api/skills/tools-only/code-feeder/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: tools-only (https://skillmd.com/u/tools-only)
- Updated: 2026-09-08
- Page: https://skillmd.com/skills/tools-only/code-feeder

---


# Code Feeder Skill

**目标**：生成包含**用户意图**和**关键代码**的高质量 Markdown 文档。
**脚本路径**：`~/.claude/skills/code-feeder/scripts/` (若不存在则搜索 `code_collector.py`)

## 🧠 核心法则

1.  **意图第一**：必须询问收集目的（如"重构登录"），并写入文档。
2.  **精准采集**：根据意图挑选代码。小文件批量导入，大文件提取片段。
3.  **闭环补全**：工具反馈"跳过"的文件，**必须**用 `--append` 模式提取关键片段补全。

## ⚙️ 模式决策

| 模式 | 标志 | 适用场景 | 关键参数 |
| :--- | :--- | :--- | :--- |
| **Batch** | `--mode batch` | 小文件/配置文件 | `--files f1 f2` |
| **Snippets** | `--mode snippets` | 大文件(>500KB)/特定函数 | `--target f1 --ranges [...]` |
| **Append** | `--append` | 补全漏掉的文件/增量更新 | (配合 Snippets 使用) |

## 📋 执行流程

### 1. 确认意图
若未提供，询问："收集代码的具体目的是什么？关注哪些模块？"

### 2. 检测与规划
```bash
python {script_dir}/detect_project.py "{project_path}"
```
*根据输出忽略无关目录，聚焦核心文件。*

### 3. 执行收集
**A. 批量模式 (首选)**
```bash
python {script_dir}/code_collector.py "{project_path}" \
  --mode batch --files src/main.py config.json ... \
  --intent "{user_intent}" --output "ctx.md"
```

**B. 片段模式 (针对大文件/特定逻辑)**
*支持类型: `function`, `class`, `method`, `lines`*
```bash
python {script_dir}/code_collector.py "{project_path}" \
  --mode snippets --target "src/heavy.js" \
  --ranges '[{{"type":"function","name":"login"}}, {{"type":"lines","start":1,"end":50}}]' \
  --intent "{user_intent}" --output "ctx.md"
```

### 4. 补全 (Critical)
检查输出中的 **"⚠️ 跳过的文件"**。若文件重要，**立即**用追加模式提取：
```bash
python {script_dir}/code_collector.py "{project_path}" \
  --mode snippets --target "skipped_file.py" \
  --ranges '[{{"type":"function","name":"core_logic"}}]' \
  --output "ctx.md" --append
```

### 5. 审核与手动修补
**Read** 生成的文档，检查完整性与正确性：
- **关键缺失**：若工具无法提取（如复杂正则失效）但代码至关重要，**必须手动 Read 文件内容**，并按文件中同样的格式手动追加到对应区域的位置，并更新相关目录树和统计信息。
- **结构检查**：确认目录树和统计信息正常。

### 6. 交付
告知用户文档路径，并确认已包含意图和核心逻辑。

## ⚠️ 异常指南
- **二进制文件**：仅在文档中注明存在，不可读取。
- **提取失败**：若函数提取失败，降级使用 `lines` 行号范围提取。
