# fastcontextc **Repository Path**: uesoft/fastcontextc ## Basic Information - **Project Name**: fastcontextc - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-06-22 - **Last Updated**: 2026-06-22 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # FastContext-C > C port of [microsoft/fastcontext](https://github.com/microsoft/fastcontext) — codebase exploration subagent that returns `file:line` citations to the main agent. **状态**:✅ 端到端跑通(GLM-5.2,2 turn 找到目标) ## 为什么有这个 FastContext Python 版(830 行)通过 OpenAI tool calling 让 LLM 用 Read/Glob/Grep 探索仓库,最后输出 `file:line` 给主 agent。 C 版目标: - 把这个能力嵌入 C/C++ LLM agent 平台(AIBuddy、MRAgentC、DSuE_RSI) - 0 Python 依赖,单二进制 / 静态库 - 复用 MRAgentC 的成熟移植模式(curl + popen + json_min) ## 文件结构 ``` ~/fastcontextc/ ├── include/fastcontext.h # 公共 C API ├── main.c # CLI 入口 ├── CMakeLists.txt # CMake 构建(给 AIBuddy 集成用) ├── Makefile # 直接 make ├── examples/ │ └── aibuddy_wrapper.cpp # C++ 集成示例 └── src/ ├── buf.h # 共享的 growable byte buffer ├── agent.{c,h} # fc_agent_t 探索主循环 ├── llm.{c,h} # curl + OpenAI 兼容 chat completions ├── prompts.{c,h} # system prompt + tool schemas ├── tool.{c,h} # Read / Glob / Grep 实现 └── json_min.{c,h} # 极简 JSON 解析器(抄自 MRAgentC) ``` ## 构建 ### Make(最简单) ```bash cd ~/fastcontextc make # → bin/fastcontext ``` ### CMake(给 AIBuddy 集成) ```bash cd ~/fastcontextc mkdir build && cd build cmake .. make ``` ## 使用 ### CLI ```bash export BASE_URL="https://open.bigmodel.cn/api/coding/paas/v4" export API_KEY="your-key" export MODEL="GLM-5.2" ./bin/fastcontext \ --work-dir ~/fastcontext \ --query "Where is the GrepTool ripgrep path defined?" \ --max-turns 4 ``` 输出: ``` === FastContext-C Result === Turns used: 2 /home/ligb/fastcontext/src/fastcontext/agent/tool/grep.py:69-74 (ripgrep path resolution logic) ``` ### 程序化调用(C) ```c #include "fastcontext.h" fc_config_t cfg = { .base_url = "https://...", .api_key = "sk-...", .model = "GLM-5.2", .work_dir = "/path/to/repo", .max_turns = 6, }; fc_agent_t* a = fc_agent_create(&cfg); fc_result_t* r = NULL; fc_explore(a, "Find authentication logic", &r); printf("%s\n", r->final_answer); fc_result_free(r); fc_agent_free(a); ``` ### 程序化调用(C++ / AIBuddy 集成) 见 `examples/aibuddy_wrapper.cpp`,提供 RAII 封装 `aibuddy::tools::FastContextSubagent`。 ## Python → C 模块映射 | Python (`fastcontext/`) | C (`fastcontextc/`) | |---|---| | `cli.py` | `main.c` | | `agent/agent.py` | `src/agent.c` | | `agent/llm.py` | `src/llm.c` | | `agent/context.py` | `src/agent.c`(trajectory 部分)| | `agent/system.md` | `src/prompts.c` | | `agent/tool/read.py` | `src/tool.c` (`tool_read`) | | `agent/tool/glob.py` | `src/tool.c` (`tool_glob`) | | `agent/tool/grep.py` | `src/tool.c` (`tool_grep`) | ## 关键设计 ### async → sync Python 的 `async def acall()` 改成同步 `fc_llm_chat()`,多轮探索用 for 循环 + 检测 `` 终止。curl 本来就是阻塞的,async 在这里没有收益。 ### 工具沙箱 所有 Read/Glob/Grep 都强制 `path` 在 `--work-dir` 内(`sandbox_path()`),防止 LLM 乱读 `/etc/passwd` 之类。 ### HTTP 调用 `curl_post()` 用 mkstemp + popen 模式(MRAgentC 验证过): 1. 把 request body 写到 `/tmp/fc_body_XXXXXX` 2. popen 起一个 `curl --data-binary @tmpfile`,stdout 读响应 3. 关闭 popen 后再 unlink tmpfile ### 与上游 Python 版的差异 - **少 1 个依赖**:不要 aiofiles/openai/pydantic,只要 curl 二进制 + glibc - **少 80% 行数**:830 → ~1400 行 C(含 json_min),但功能等价 - **更快启动**:无 Python 解释器初始化,冷启动 ~5ms ## 集成路径(AIBuddy 三步走) 详见 `~/AIBuddy/FASTCONTEXT_INTEGRATION.md`: 1. **Phase 1(1-2 天)**:AIBuddy 用 exec tool 调起 `bin/fastcontext` 2. **Phase 2(3-5 天)**:链入 `libfastcontextc.a` 作为内部 Tool 3. **Phase 3(长期)**:与 DSuE_RSI 联动,作为小脑的探索器 ## 已知限制 - 没实现并行 tool 调用(Python 版的 asyncio.gather)——C 版串行执行 LLM 一次返回的多个 tool_calls - 没实现 OpenAI 的 streaming response(fastcontext Python 版也不用) - Grep 依赖系统装的 `rg`(已在 sandbox 代码里检测) ## 测试 ```bash # 端到端实测(需要 BASE_URL/API_KEY/MODEL 环境变量) ./bin/fastcontext --work-dir ~/fastcontext --query "..." --max-turns 4 ``` —— WU,2026-06-22,基于 MRAgentC 已验证的 Python→C 移植方法论