SEO技术

SEO技术

Products

当前位置:首页 > SEO技术 >

BoxAgnts:如何实现Agent多轮对话与工具技能调用?

96SEO 2026-08-06 05:24 4


如果你只和 ChatGPT 聊过天你可能会觉得 AI Agent 就是“把 prompt 发给 API,把回复显示来”。

实际痛点:

BoxAgnts:如何实现Agent多轮对话与工具技能调用?
  • 在真实项目中,单轮对话根本无法完成复杂的编辑、搜索或部署任务。
  • 工具调用往往不透明,开发者难以追踪哪一步触发了哪个工具。
  • 有限,长时间交互会导致历史信息被截断或遗失。
  • 模型经常因 token 限制被强行中止,导致业务流程中断。
  • 不同阶段需要不同模型或权限,却没有统一的切换机制。

下面通过 BoxAgnts 中一个完整的 Agent 交互流程,展示如何解决这些痛点:

使用者输入:"帮我读一下 config.toml。把 port 改成 8080"
· 使用者消息加入对话历史
· 建立 system prompt
· 调用 LLM API → 流式接收响应
· AI 决定调用工具:tool_use
· 执行 read 工具
· 工具结果注入对话历史
·
调用 API → AI 分析配置
· AI 决定调用工具:tool_use
· 执行 edit 工具
· 工具结果注入对话
·
调用 API → AI 回复:"已将端口从 8000 改为 8080"
· end_turn → 对话结束

这个过程涉及 5 次 API 调用、2 次工具执行、流式推送、上下文管理

Agent 定义:给 Agent 一个“身份”

在开始推理循环之前,需要先定义 Agent 的“角色”。BoxAgnts 内置了三个预置 Agent:

// boxagnts-workspace/src/config.rs
pub struct AgentDefinition {
pub description: Option,// 描述
pub model: Option,// 模型覆盖
pub temperature: Option,// 温度覆盖
pub prompt: Option,// 程序提示前缀
pub access: String,// 权限:full / read-only / search-only
pub visible: bool,// 是否在 @agent 自动补全中显示
pub max_turns: Option,// 最大轮次覆盖
pub color: Option。// 终端显示颜色
}

预置的三个 Agent 角色:

Agent权限prompt 特征适用场景
buildfull"You are build agent. Focus on implementing..."编码、修改文件
planread-only"You are plan agent. You can read files and analyze..."代码分析、架构设计
exploresearch-only"Fast search-only agent for code exploration"快速搜索、文件定位

Agent prompt 如何注入

Agent 定义中的 prompt 字段会在查询循环启动时被注入到 system prompt 的最前面:

// boxagnts-query/src/query.rs
if let Some = config.agent_definition {
if let Some = agent.prompt {
patched.system_prompt = Some(match &config.system_prompt {
Some => format!,None => agent_prompt.clone,});}
}

Agent 可以覆盖模型和最大轮次:

let effective_model = if let Some = config.agent_definition {
agent.model.clone.unwrap_or_else)
} else {
config.model.clone
};let effective_max_turns = config.agent_definition
.as_ref
.and_n
.unwrap_or;

这代表着使用者可以在同一会话中和权限——规划阶段使用只读慢思考模型。执行阶段切换到全权限快速模型,从根本上缓解 “单一模型难兼顾所有需求” 的痛点。

run_query_loop:Agent 的心脏

run_query_loop 是 BoxAgnts 中最主要的函数,位于 boxagnts-query crate 中:

pub async fn run_query_loop(
说到client,&AnthropicClient,// API 客户端
messages: &mut Vec。// 对话历史
tools这方面,&,// 工具集合
tool_ctx: &ToolContext,// 沙箱配置
再看config,&QueryConfig,// 循环配置
cost_tracker: Arc,// 成本追踪,防止预算超支
event_tx: Option,// 实时事件推送
cancel_token: CancellationToken,// 使用者可随时中断循环
pending_messages: Option<&mut Vec,// 插入式使用者消息
) -> QueryOutcome {…}

The signature itself is a design document—each parameter solves a concrete production problem:

参数设计意图 / 痛点对应方法
clientL​LM 提供商可插拔;统一入口避免多处硬编码,
&mut Vec 对话历史直接就地修改,避免复制开销;保持上下文完整,
& 动态加载任意数量工具;增加功能不改主要原因,
*其他参数均对应“实时取消”“预算控制”“并发插入消息”等生产需求*

主循环的五步节拍

┌─────────────────────────────────────────────┐│ loop { ││ ││ ① 检查终止条件 ││ · turn> max_turns?→ EndTurn ││ · cancel_token?说起来,→ Cancelled ││ · budget exceeded?话说回来,→ BudgetExceeded ││ ││ ② 预处理消息 ││ · drain pending_messages queue ││ · apply_tool_result_budget ││ · auto_compact ││ ││ ③ 建立 system prompt + 调用 LLM API ││ · 注入 Agent 定义 / AGENTS.md ││ · 建立 CreateMessageRequest ││ · 流式接收 StreamEvent ││ · 累积 text / thinking / tool_use blocks││ ││ ④ 处理响应 ││ · end_turn → 返回 ││ · tool_use → 并行执行工具 → 注入结果 → 继续 ││ · max_tokens → 恢复输出 → 继续 ││ ││ ⑤ 错误恢复 ││ · overloaded → 切换 fallback model ││ · stream stall → retry │└─────────────────────────────────────────────┘

System Prompt 建立:Agent 的“世界观”

A well‑structured system prompt gives model a stable “worldview”。从而降低每轮 token 消耗并提高可靠性。

fn build_system_prompt -> SystemPrompt {
let opts = SystemPromptOptions {
custom_system_prompt: config.system_prompt.clone,append_system_prompt: config.append_system_prompt.clone。output_style: config.output_style,custom_output_style_prompt: config.output_style_prompt.clone,working_directory: config.working_directory.clone,..Default::default
};let text = boxagnts_core::system_prompt::build_system_prompt;
SystemPrompt::Text
}

The prompt is layered:


┌──────────────────────────────────────┐
│ Agent 角色定义 ← AgentDefinition.prompt
├──────────────────────────────────────┤
│ 主要能力声明 │
│ • 可用工具列表 ← 动态生成自 tools 参数 |
│ • 技能列表 ← SkillTool 自动发现 |
│ • 输出格式要求 |
│ • 安全边界 |
├──────────────────────────────────────┤
│ AGENTS.md 内容 ← 项目级行为规范 |
├──────────────────────────────────────┤
│ 动态边界标记 |
│ --- 以上缓存。以下不缓存 --- |
├──────────────────────────────────────┤
│ 会话特定信息 ← 工作目录、时间等 |
└──────────────────────────────────────┘

The “---以上缓存---” line leverages Anthropic’s prompt caching,dramatically cutting token cost for repeated calls.

max_tokens 恢复:Agent 的“断点续传”

If model hits its token limit,a naïve implementation would abort task entirely. BoxAgnts instead automatically resumes from cut‑off point.

// boxagnts-query/src/query.rs
const MAX_TOKENS_RECOVERY_LIMIT: u32 = /* configurable */;const MAX_TOKENS_RECOVERY_MSG: &'static str =
"Output token limit hit. Resume directly — no apology,no recap of what \
you were doing. Pick up mid-thought if that is where cut happened. \
Break remaining work into smaller pieces.";

  1. Add partial assistant reply to history.
  2. Add a syntic user message containing MAXTOKENSRECOVERY_MSG.
  3. The next loop call continues generation exactly where it left off.
  4. The explicit “no apology。no recap” instruction prevents model from inserting useless filler sentences.
  5. "

    auto_compact:当上下文太长时

    L​LM context windows are finite. When conversation history grows beyond a configurable fraction of window,BoxAgnts automatically compacts it by summarizing older turns.

    // boxagnts-query/src/compact.rs
    const AUTOCOMPACT_TRIGGER_FRACTION: f64 = /* e.g.,0.75 */;const WARNING_PCT: f64 = /* e.g.,0.80 */;const CRITICAL_PCT: f64 = /* e.g.,0.90 */;

    The summarization uses an auxiliary LLM with a strict “NO_TOOLS_PREAMBLE” to guarantee that no tool calls happen during compression:

    
    CRITICAL:
    Respond with TEXT ONLY. Do NOT call any tools.
    - Do NOT use Read,Bash,Grep,Glob,Edit,Write,or ANY or tool.
    - You already have all context you need in conversation above.
    - Tool calls will be REJECTED and will waste your only turn.
    

    The result replaces early messages while preserving most recent few turns verbatim.

    Tool 执行:从 AI 决定到运行结果

    If LLM returns "tool_use"。BoxAgnts enters tool execution phase:

    
    ┌──────────────────────────────────────────────┐
    | Phase : 顺序执行 PreToolUse 预处理 |
    | |
    ├──────────────────────────────────────────────┤
    | Phase : 并行执行非阻塞工具 |
    | join_all → 所有工具并发运行 |
    | |
    └──────────────────────────────────────────────┘
    

    The crucial design choice is to inject tool results back into conversation as **user** messages:

    • The Assistant initiated "tool_use".
    • The system replies with "tool_result".
    • The model treats this as normal user feedback and proceeds to next reasoning step.
    • This pattern keeps whole pipeline within standard chat schema—no special side‑channel needed.
    • )

    execute_tool:工具分发的主要

    
    // boxagnts-query/src/lib.rs

    async fn execute_tool( 说到name,&str,input:&Value,tools:&,ctx:&ToolContext,) -> ToolResult { let tool = tools.iter.find == name);match tool { Some=>{ debug!,tool.execute,ctx).await } None=>{ warn!,ToolResult::error) } } }

    The implementation is deliberately simple—a linear scan over a small vector is negligible compared to network latency and sandbox startup time.

    托管 Agent 模式:Manager‑Executor 架构

    If a single Agent cannot handle a complex request alone,BoxAgnts offers a managed‑agent mode that separates planning from execution.

    
    ┌───────────────────┐
    │   Manager Agent  │
    ├─ Opus 等强模型   ┤
    |   只做规划和分配   |
    └─────────▲─────────┘
    |
    ┌─────────────╂─────────────╂─────────────┐
    ▼             ▼             ▼
    ┌───────────┐ ┌───────────┐ ┌───────────�╗
    | Executor A | | Executor B | | Executor C |
    | | | | | |
    | 子任务 A   | | 子任务 B   | | 子任务 C   |
    └───────────�╝ └──────────�╝ └──────────�╝ 

    The Manager’s system prompt contains explicit directives so it never calls tools directly—it only spawns Executors via an internal agent tool.

    Managed‑Agent Prompt 示例

    
    pub fn managedagentsystem_prompt->String{
    format!(r#"

    Managed Agent Mode

    You are THE MANAGER in a manager‑executor architecture.

    Your Role

    • Coordinate work but DO NOT execute tasks yourself.
    • Delegate implementation to executor agents.
    • Each executor runs model {executor_model} with up to {max_turns} turns.
    • You may launch up to {max_concurrent} executors in parallel.

    Workflow

    1️⃣ Analyze user request & break into sub‑tasks. 2️⃣ Spawn executors via agent tool. 3️⃣ Review results – if insufficient spawn follow‑ups. 4️⃣ Synsize all outcomes into final response."#。...)) }

    Skill 程序:让 Agent 学会“专业技能”

    A Tool gives an Agent physical capabilities . A Skill provides domain expertise—e.g.,code‑review guidelines or CSS refactoring advice.

    Skill 文件结构

    
    app/extensions/skills/
    ├── code-review/
    │   └── SKILL.md # 静态模板 + $ARGUMENTS 占位符
    ├── css-refactor-advisor/
    ├── current-wear/
    ...
    

    Each SKILL.md may contain front‑matter metadata followed by markdown content.

    SkillTool 实现

    
    pub struct SkillTool;

    impl Tool for SkillTool { fn name->&'static str{ "skill-tool" }

    async fn execute->ToolResult{ let params :SkillInput = serdejson::fromvalue?,if params.skill=="list"{ return listskills.await;} let =findandreadskill.await?,其实,let content = strip_frontmatter;let rendered = match ¶ms.args{ Some=>content.replace。None=>content.replace,};ToolResult::success } }

    双层搜索方法

    
    async fn skillsearchdirs->Vec{
    let mut dirs = vec!,// 项目级优先
    dirs.push.await.join );// 全局备用
    dirs
    }
    

    Thus developers can override global skills with project‑specific versions—a direct answer to “在不同项目里需要不同规则,却不想改代码”的痛点。

    $ARGUMENTS 占位符

    示例 SKILL.md 内容:

    `markdown

    至于请审查,$ARGUMENTS

    至于检查要点,* 函数是否过长 * 是否存在未处理的 Result/Option * 是否有不必要的 .clone * 命名是否符合 Rust 惯例 `

    When invoked as:

    json { "skill":"code-review","args":"src/main.rs" }

    $ARGUMENTS placeholder becomes src/main.rs。turning static knowledge into a dynamic advisory function.

    流式推送的观点是,让使用者看到 Agent 在“思考”

    The query loop emits granular events through an unbounded channel which can be streamed to UI dashboards or TUI clients:

    
    pub enum QueryEvent{
    Token{ text:String },// 每个 token 实时推送
    ToolStart{toolname:String,toolid:u64,input:String},ToolEnd{toolname:String,toolid:u64,result:String},Status,// 状态文字,例如 “正在压缩上下文…”
    }
    

    These events enable transparent debugging—developers no longer face “AI 突然卡死,我不知道它在干什么”的困惑。

    小结

    An AI Agent’s multi‑turn dialogue is essentially a robust control loop:

    System Prompt ➜ API Call ➜ Stream Parse ➜ Detect Tool Use ➜ Execute Tool ➜ Inject Result ➜ Loop again ⟲ until end_turn

    Each mechanism in BoxAgnts directly addresses real production pains:

    Model overload / rate limits fallback_model switching Complex tasks exceeding single-agent capacity Managed‑Agent mode – Manager + Executors Lack of domain expertise Skill system – parametric knowledge injection Slow feedback loop for users Event streaming via QueryEvent
    Pain Point Solved By Diverse roles & models needed per stage Agent Definition System – role + model overrides Prompt length growth causing cost explosion Layered System Prompt + Caching Output truncation at token limit max_tokens Recovery logic Context window overflow auto_compact structural summary Tool result accumulation bloat tool_result_budget trimming

    Implementing se mechanisms transforms an “AI 能跑” prototype into a production‑ready “AI 能可靠工作” platform.


标签: Agent

SEO优化服务概述

作为专业的SEO优化服务提供商,我们致力于通过科学、系统的搜索引擎优化策略,帮助企业在百度、Google等搜索引擎中获得更高的排名和流量。我们的服务涵盖网站结构优化、内容优化、技术SEO和链接建设等多个维度。

百度官方合作伙伴 白帽SEO技术 数据驱动优化 效果长期稳定

SEO优化核心服务

网站技术SEO

  • 网站结构优化 - 提升网站爬虫可访问性
  • 页面速度优化 - 缩短加载时间,提高用户体验
  • 移动端适配 - 确保移动设备友好性
  • HTTPS安全协议 - 提升网站安全性与信任度
  • 结构化数据标记 - 增强搜索结果显示效果

内容优化服务

  • 关键词研究与布局 - 精准定位目标关键词
  • 高质量内容创作 - 原创、专业、有价值的内容
  • Meta标签优化 - 提升点击率和相关性
  • 内容更新策略 - 保持网站内容新鲜度
  • 多媒体内容优化 - 图片、视频SEO优化

外链建设策略

  • 高质量外链获取 - 权威网站链接建设
  • 品牌提及监控 - 追踪品牌在线曝光
  • 行业目录提交 - 提升网站基础权威
  • 社交媒体整合 - 增强内容传播力
  • 链接质量分析 - 避免低质量链接风险

SEO服务方案对比

服务项目 基础套餐 标准套餐 高级定制
关键词优化数量 10-20个核心词 30-50个核心词+长尾词 80-150个全方位覆盖
内容优化 基础页面优化 全站内容优化+每月5篇原创 个性化内容策略+每月15篇原创
技术SEO 基本技术检查 全面技术优化+移动适配 深度技术重构+性能优化
外链建设 每月5-10条 每月20-30条高质量外链 每月50+条多渠道外链
数据报告 月度基础报告 双周详细报告+分析 每周深度报告+策略调整
效果保障 3-6个月见效 2-4个月见效 1-3个月快速见效

SEO优化实施流程

我们的SEO优化服务遵循科学严谨的流程,确保每一步都基于数据分析和行业最佳实践:

1

网站诊断分析

全面检测网站技术问题、内容质量、竞争对手情况,制定个性化优化方案。

2

关键词策略制定

基于用户搜索意图和商业目标,制定全面的关键词矩阵和布局策略。

3

技术优化实施

解决网站技术问题,优化网站结构,提升页面速度和移动端体验。

4

内容优化建设

创作高质量原创内容,优化现有页面,建立内容更新机制。

5

外链建设推广

获取高质量外部链接,建立品牌在线影响力,提升网站权威度。

6

数据监控调整

持续监控排名、流量和转化数据,根据效果调整优化策略。

SEO优化常见问题

SEO优化一般需要多长时间才能看到效果?
SEO是一个渐进的过程,通常需要3-6个月才能看到明显效果。具体时间取决于网站现状、竞争程度和优化强度。我们的标准套餐一般在2-4个月内开始显现效果,高级定制方案可能在1-3个月内就能看到初步成果。
你们使用白帽SEO技术还是黑帽技术?
我们始终坚持使用白帽SEO技术,遵循搜索引擎的官方指南。我们的优化策略注重长期效果和可持续性,绝不使用任何可能导致网站被惩罚的违规手段。作为百度官方合作伙伴,我们承诺提供安全、合规的SEO服务。
SEO优化后效果能持续多久?
通过我们的白帽SEO策略获得的排名和流量具有长期稳定性。一旦网站达到理想排名,只需适当的维护和更新,效果可以持续数年。我们提供优化后维护服务,确保您的网站长期保持竞争优势。
你们提供SEO优化效果保障吗?
我们提供基于数据的SEO效果承诺。根据服务套餐不同,我们承诺在约定时间内将核心关键词优化到指定排名位置,或实现约定的自然流量增长目标。所有承诺都会在服务合同中明确约定,并提供详细的KPI衡量标准。

SEO优化效果数据

基于我们服务的客户数据统计,平均优化效果如下:

+85%
自然搜索流量提升
+120%
关键词排名数量
+60%
网站转化率提升
3-6月
平均见效周期

行业案例 - 制造业

  • 优化前:日均自然流量120,核心词无排名
  • 优化6个月后:日均自然流量950,15个核心词首页排名
  • 效果提升:流量增长692%,询盘量增加320%

行业案例 - 电商

  • 优化前:月均自然订单50单,转化率1.2%
  • 优化4个月后:月均自然订单210单,转化率2.8%
  • 效果提升:订单增长320%,转化率提升133%

行业案例 - 教育

  • 优化前:月均咨询量35个,主要依赖付费广告
  • 优化5个月后:月均咨询量180个,自然流量占比65%
  • 效果提升:咨询量增长414%,营销成本降低57%

为什么选择我们的SEO服务

专业团队

  • 10年以上SEO经验专家带队
  • 百度、Google认证工程师
  • 内容创作、技术开发、数据分析多领域团队
  • 持续培训保持技术领先

数据驱动

  • 自主研发SEO分析工具
  • 实时排名监控系统
  • 竞争对手深度分析
  • 效果可视化报告

透明合作

  • 清晰的服务内容和价格
  • 定期进展汇报和沟通
  • 效果数据实时可查
  • 灵活的合同条款

我们的SEO服务理念

我们坚信,真正的SEO优化不仅仅是追求排名,而是通过提供优质内容、优化用户体验、建立网站权威,最终实现可持续的业务增长。我们的目标是与客户建立长期合作关系,共同成长。

提交需求或反馈

Demand feedback