第38章 自定义AI代理(Custom Agents)
自定义代理 自定义代理通过一份简短的 YAML 文件定义,无需编写命令式代码,也无需继承框架基类,YAML 文件本身就是代理。平台内置的 Web 界面、持久会话、团队协作、部署基础设施及上下文策略等功能均可直接使用。
通过编程代理创建 推荐的创建方式是让编程代理用自然语言帮你构建:
你:帮我建一个文档审查代理。使用 Claude 作为模型,允许访问文件系统和 GitHub,并遵循 docs/STYLE.md 中的样式指南。
代理:已创建“docs-reviewer”代理,正在以该代理开启新会话。你随时可从会话下拉列表中切换。
代理在后台生成 YAML 并注册,方便后续复用。如需调整配置,可直接编辑该 YAML 文件。
配置文件 每个自定义代理独立存放在一个目录中,根目录下需包含 config.yaml,目录可放置于磁盘任意位置,通过以下命令运行:
omni run ./my-agent/ # 指向包含 config.yaml 的目录
可配置项 配置文件支持以下配置节:
Harness Harness 是执行代理循环的运行时,修改一行配置即可切换运行时。
executor: type: omnigent config: harness: claude
模型与凭证 选择驱动 Harness 的 LLM。
executor: type: omnigent config: harness: claude model: claude-sonnet-4-6
提示词与技能 系统提示词可内联书写,也可指向外部文件。
prompt: You are a concise coding assistant.
# 或从文件加载: instructions: AGENTS.md
MCP 与工具 接入 MCP 服务器、Python 函数或子代理。
tools: github: type: mcp command: uv args: [run, python, -m, github_mcp] summarize: type: function callable: my_package.tools.summarize_file
策略 以声明式 YAML 定义护栏策略。
policies: rate_limit: type: function handler: omnigent.policies.builtins.safety.max_tool_calls_per_session factory_params: limit: 50
综合示例
spec_version: 1 name: coding_agent prompt: | You are a coding agent. Inspect files before editing, run targeted tests, and summarize changes with validation results.
executor: type: omnigent config: harness: claude model: claude-sonnet-4-6
os_env: type: caller_process cwd: . sandbox: write_paths: [.] allow_network: true
tools: repo_search: type: function callable: my_package.tools.repo_search
policies: rate_limit: type: function handler: omnigent.policies.builtins.safety.max_tool_calls_per_session factory_params: limit: 50