feat: 添加 Agent 人设文件生成功能
新增功能: - 创建 Agent 后自动生成 SOUL.md 和 AGENTS.md - 根据绑定模式生成不同的职责描述 - 支持模板变量替换 文件变更: - index.js: 添加 generateAgentFiles() 和 generateResponsibilities() - templates/agents.md.template: 简化模板语法 - templates/soul.md.template: 添加会话隔离说明
This commit is contained in:
71
index.js
71
index.js
@@ -157,6 +157,58 @@ function createAgent(agentId) {
|
||||
}
|
||||
}
|
||||
|
||||
// 生成 Agent 人设文件
|
||||
function generateAgentFiles(options) {
|
||||
log.step(2, 6, '生成 Agent 人设文件');
|
||||
|
||||
const workspacePath = `/home/admin/.openclaw/workspace-${options.agentId}`;
|
||||
const templatesPath = path.join(__dirname, 'templates');
|
||||
|
||||
try {
|
||||
// 读取模板
|
||||
const soulTemplate = fs.readFileSync(path.join(templatesPath, 'soul.md.template'), 'utf8');
|
||||
const agentsTemplate = fs.readFileSync(path.join(templatesPath, 'agents.md.template'), 'utf8');
|
||||
|
||||
// 替换模板变量
|
||||
const soulContent = soulTemplate
|
||||
.replace(/{{agentName}}/g, options.agentName)
|
||||
.replace(/{{description}}/g, options.description || '帮助用户完成任务')
|
||||
.replace(/{{agentType}}/g, options.bindingMode === 'none' ? '后台 Agent' : '前台 Agent');
|
||||
|
||||
const agentsContent = agentsTemplate
|
||||
.replace(/{{agentName}}/g, options.agentName)
|
||||
.replace(/{{responsibilities}}/g, generateResponsibilities(options.bindingMode));
|
||||
|
||||
// 写入文件
|
||||
fs.writeFileSync(path.join(workspacePath, 'SOUL.md'), soulContent);
|
||||
fs.writeFileSync(path.join(workspacePath, 'AGENTS.md'), agentsContent);
|
||||
|
||||
log.success(`已生成 SOUL.md 和 AGENTS.md`);
|
||||
return true;
|
||||
} catch (err) {
|
||||
log.error(`生成人设文件失败:${err.message}`);
|
||||
log.warning('Agent 已创建,但人设文件未生成,可手动创建');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// 生成职责描述
|
||||
function generateResponsibilities(bindingMode) {
|
||||
if (bindingMode === 'none') {
|
||||
return `1. 接收其他 Agent 分配的任务
|
||||
2. 执行专业领域的工作
|
||||
3. 返回处理结果`;
|
||||
} else if (bindingMode === 'account') {
|
||||
return `1. 响应用户的飞书消息
|
||||
2. 处理用户请求
|
||||
3. 提供专业领域的帮助`;
|
||||
} else {
|
||||
return `1. 响应群聊中的消息
|
||||
2. 协助群组讨论
|
||||
3. 提供专业领域的建议`;
|
||||
}
|
||||
}
|
||||
|
||||
// 配置飞书账户(整合 feishu-agent-binding 功能)
|
||||
function configureFeishuAccount(options) {
|
||||
if (!options.newBot) return true;
|
||||
@@ -457,6 +509,18 @@ async function interactiveMode() {
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// 步骤 2.5:生成 Agent 人设文件
|
||||
const filesGenerated = generateAgentFiles({
|
||||
agentId,
|
||||
agentName,
|
||||
description,
|
||||
bindingMode
|
||||
});
|
||||
if (!filesGenerated) {
|
||||
log.warning('人设文件生成失败,但 Agent 已创建');
|
||||
// 不回退,继续执行
|
||||
}
|
||||
|
||||
// 步骤 3:更新配置
|
||||
const configUpdated = updateConfig(options);
|
||||
if (!configUpdated) {
|
||||
@@ -613,6 +677,13 @@ ${colors.bold}示例:${colors.reset}
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// 步骤 2.5:生成 Agent 人设文件
|
||||
const filesGenerated = generateAgentFiles(agentOptions);
|
||||
if (!filesGenerated) {
|
||||
log.warning('人设文件生成失败,但 Agent 已创建');
|
||||
// 不回退,继续执行
|
||||
}
|
||||
|
||||
// 步骤 3:更新配置
|
||||
const configUpdated = updateConfig(agentOptions);
|
||||
if (!configUpdated) {
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
# AGENTS.md - {{agentName}} 工作指令
|
||||
|
||||
## 职责范围
|
||||
{{#each responsibilities}}
|
||||
{{this}}
|
||||
{{/each}}
|
||||
{{responsibilities}}
|
||||
|
||||
## 工作流程
|
||||
1. 接收用户请求
|
||||
1. 接收用户/Agent 请求
|
||||
2. 分析请求类型
|
||||
3. 调用相应工具或技能
|
||||
4. 返回结果
|
||||
|
||||
Reference in New Issue
Block a user