• 简体中文
  • v6.5.0
  • Ripgrep 上下文构建器

    通过 session.grepsession.glob 进行快速代码搜索,可以收集相关的文件和匹配行, 然后将它们注入到提示中 —— 这是在 agent 开始推理之前的一个轻量级检索步骤。当你希望将 agent 限定在大型代码库的某个特定切片,而不是让它从头开始探索时,可以使用这种方式。

    Node.js
    Python
    TypeScript
    import { Agent } from '@a3s-lab/code';
    const agent = await Agent.create('agent.acl');
    const session = agent.session('.');
    // 1. Find candidate files by glob pattern (returns a list of paths).
    const files = await session.glob('src/**/*.ts');
    // 2. Search the workspace for the symbol we care about (returns ripgrep text).
    const hits = await session.grep('createSession');
    // 3. Build a context string and feed it into a focused prompt.
    const context = [
    `Files in scope:\n${files.join('\n')}`,
    `Matches for "createSession":\n${hits}`,
    ].join('\n\n');
    const answer = await session.run(
    `Using only this context, explain how createSession is wired up:\n\n${context}`,
    );
    console.log(answer);

    glob 返回匹配的文件路径列表,而 grep 则以单个字符串的形式返回原始的 ripgrep 输出。 两者都在本地运行并能快速返回,因此你可以在花费一次模型调用之前,链式执行多次搜索来低成本地 组装上下文。当你需要文件的完整内容而不仅仅是匹配行时,可以将它们与 session.readFile 搭配使用。