add-agent
Quickly add a new isolated Agent with a dedicated Telegram Bot.
Trigger
User says something like:
"Add a new agent, ID is marketing, name is Marketing Assistant, telegram Bot Token is xxx, allowFrom is 123456789, responsible for content marketing and social media"
Steps
Once a new agent request is received, execute in the following order:
1. Extract Parameters
Extract the following fields from user input:
- -
AGENT_ID: English ID (e.g. marketing) - INLINECODE1 : Agent name (e.g. Marketing Assistant, Alice, WorkBot, etc.)
- INLINECODE2 : Telegram Bot Token
- INLINECODE3 : allowFrom numeric ID (e.g. 123456789)
- INLINECODE4 : Role description (e.g. responsible for content marketing and social media)
If any field is missing, ask the user to provide it before continuing.
2. Detect Installation Directory
Read the current openclaw.json path, extract the actual paths from the existing main agent in agents.list to derive:
- -
STATE_DIR: e.g. INLINECODE6 - INLINECODE7 : main agent workspace path
- INLINECODE8 : main agent agentDir path
- INLINECODE9 : INLINECODE10
- INLINECODE11 : INLINECODE12
3. Backup Config File
CODEBLOCK0
4. Run openclaw agents add
CODEBLOCK1
This automatically initializes the workspace directory structure, agentDir, and default files like SOUL.md and AGENTS.md.
5. Copy Auth, Skills and USER.md
CODEBLOCK2
6. Generate Persona Files
Overwrite ${NEW_WORKSPACE}/SOUL.md:
CODEBLOCK3
Overwrite ${NEW_WORKSPACE}/AGENTS.md:
CODEBLOCK4
7. Update openclaw.json
Read the current config and append the following:
Add to agents.list:
CODEBLOCK5
Add to bindings:
CODEBLOCK6
Add to channels.telegram.accounts:
CODEBLOCK7
Handle tools config (check before writing):
Check whether the tools field exists:
- - If
tools does not exist, add the full block:
CODEBLOCK8
- - If
tools exists but has no agentToAgent, add it:
CODEBLOCK9
- - If
agentToAgent already exists, only append "${AGENT_ID}" to the allow array (no duplicates)
- - If
sessions.visibility does not exist, add it:
CODEBLOCK10
8. Validate JSON
CODEBLOCK11
If validation fails, stop immediately and restore from backup:
cp ${CONFIG_PATH}.bak.* ${CONFIG_PATH}
Report the exact error to the user.
9. Fix File Permissions
CODEBLOCK13
10. Report Completion
Reply to the user:
CODEBLOCK14
add-agent
快速添加一个带有专用Telegram Bot的独立新Agent。
触发条件
用户说出类似以下内容:
添加一个新Agent,ID为marketing,名称为Marketing Assistant,Telegram Bot Token为xxx,allowFrom为123456789,负责内容营销和社交媒体
执行步骤
收到新Agent请求后,按以下顺序执行:
1. 提取参数
从用户输入中提取以下字段:
- - AGENTID:英文ID(例如 marketing)
- AGENTNAME:Agent名称(例如 Marketing Assistant、Alice、WorkBot 等)
- BOTTOKEN:Telegram Bot Token
- ALLOWFROM:allowFrom数字ID(例如 123456789)
- DESCRIPTION:角色描述(例如 负责内容营销和社交媒体)
如果缺少任何字段,请先要求用户提供后再继续。
2. 检测安装目录
读取当前openclaw.json路径,从agents.list中现有主Agent提取实际路径,以推导出:
- - STATEDIR:例如 /home/openclaw/.openclaw
- MAINWORKSPACE:主Agent工作空间路径
- MAINAGENTDIR:主Agent的agentDir路径
- NEWWORKSPACE:${STATEDIR}/workspace-${AGENTID}
- NEWAGENTDIR:${STATEDIR}/agents/${AGENT_ID}/agent
3. 备份配置文件
bash
cp ${CONFIG
PATH} ${CONFIGPATH}.bak.$(date +%Y%m%d%H%M%S)
4. 运行openclaw agents add
bash
openclaw agents add ${AGENT_ID}
这将自动初始化工作空间目录结构、agentDir以及SOUL.md和AGENTS.md等默认文件。
5. 复制认证、技能和USER.md
bash
复制认证配置文件
cp ${MAIN
AGENTDIR}/auth-profiles.json \
${NEW
AGENTDIR}/auth-profiles.json
复制技能
cp -r ${MAIN_WORKSPACE}/skills/ \
${NEW_WORKSPACE}/skills/
复制USER.md
cp ${MAIN_WORKSPACE}/USER.md \
${NEW_WORKSPACE}/USER.md
6. 生成角色文件
覆盖 ${NEW_WORKSPACE}/SOUL.md:
markdown
${AGENT_NAME}
身份
你是 ${AGENT_NAME},${DESCRIPTION}。
你的搭档是主Agent。你们协作完成任务。
核心职责
${DESCRIPTION}
个性特点
- - 行动导向:立即分解任务并提供清晰的执行步骤
- 主动汇报:完成任务后向主Agent报告结果
- 专业:对所有输出保持高标准
规则
- - 未经确认不得执行高风险操作
- 在执行需要手动审批的操作前,务必通知用户
覆盖 ${NEW_WORKSPACE}/AGENTS.md:
markdown
${AGENT_NAME} Agent配置
系统中的其他Agent
- - main:主Agent,负责日常对话、任务协调和决策
- ${AGENT_ID}(你自己):${DESCRIPTION}
协作规则
从main接收任务
- 1. 确认任务目标和优先级
- 分解执行步骤
- 执行并记录结果
- 完成后向main报告结果
何时主动联系main
- - 需要最终用户确认的操作
- 任务完成后的结果报告
- 上报异常或意外情况
7. 更新openclaw.json
读取当前配置并追加以下内容:
添加到agents.list:
json
{
id: ${AGENT_ID},
name: ${AGENT_NAME},
workspace: ${NEW_WORKSPACE},
agentDir: ${NEWAGENTDIR}
}
添加到bindings:
json
{
agentId: ${AGENT_ID},
match: {
channel: telegram,
accountId: ${AGENT_ID}
}
}
添加到channels.telegram.accounts:
json
${AGENT_ID}: {
enabled: true,
botToken: ${BOT_TOKEN},
dmPolicy: pairing,
allowFrom: [${ALLOW_FROM}],
groupPolicy: allowlist,
streaming: off
}
处理tools配置(写入前检查):
检查tools字段是否存在:
json
tools: {
agentToAgent: {
enabled: true,
allow: [main, ${AGENT_ID}]
},
sessions: {
visibility: all
}
}
- - 如果tools存在但没有agentToAgent,添加它:
json
agentToAgent: {
enabled: true,
allow: [main, ${AGENT_ID}]
}
- - 如果agentToAgent已存在,仅将${AGENT_ID}追加到allow数组中(不重复)
- - 如果sessions.visibility不存在,添加它:
json
sessions: {
visibility: all
}
8. 验证JSON
bash
cat ${CONFIG_PATH} | python3 -m json.tool
如果验证失败,立即停止并从备份恢复:
bash
cp ${CONFIGPATH}.bak.* ${CONFIGPATH}
向用户报告具体错误。
9. 修复文件权限
bash
chown -R $(stat -c %U:%G ${MAIN
WORKSPACE}) ${NEWWORKSPACE}/
chown -R $(stat -c %U:%G ${MAIN
AGENTDIR}) ${NEW
AGENTDIR}/
10. 报告完成
回复用户:
✅ Agent ${AGENTNAME} (${AGENTID}) 创建成功!
已完成:
- - openclaw agents add 初始化完成
- openclaw.json 已更新
- 从主Agent复制了认证、技能和USER.md
- 生成了SOUL.md / AGENTS.md
- 配置了agentToAgent通信
- JSON验证通过
⚠️ 需要手动执行的步骤:
- 1. 重启Gateway:
openclaw gateway restart
- 2. 验证绑定:
openclaw agents list --bindings
- 3. 打开Telegram,找到新Bot并发送/start完成配对