All files / gitwork/src/core openclaw-message.js

0% Statements 0/49
0% Branches 0/34
0% Functions 0/12
0% Lines 0/47

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
/**
 * OpenClaw Message Tool Integration
 * 集成 OpenClaw message 工具接口调用能力
 * 
 * 功能增强:
 * 1. 指挥其他 Agent 工作
 * 2. 发送任务指令
 * 3. 进度跟踪与报告
 * 4. 多通道消息发送
 * 5. 富媒体支持
 * 
 * 注意:OpenClaw message 工具是内置工具,直接调用 message() 函数即可
 * 不需要通过 CLI 命令调用
 */
 
/**
 * 发送消息到 OpenClaw 通道
 * @param {object} options - 消息选项
 * @param {string} options.channel - 目标通道(qqbot, telegram 等)
 * @param {string} options.target - 目标用户/群聊 ID
 * @param {string} options.message - 消息内容
 * @param {string} [options.media] - 媒体 URL 或本地路径
 * @param {string} [options.filename] - 文件名
 * @param {string} [options.contentType] - 内容类型
 * @returns {Promise<object>} - 消息发送结果
 */
async function sendMessage(options) {
    const {
        channel,
        target,
        message,
        media,
        filename,
        contentType
    } = options;
 
    // 构建消息参数
    const messageParams = {
        action: 'send',
        channel,
        target,
        message
    };
 
    if (media) {
        messageParams.media = media;
        if (filename) messageParams.filename = filename;
        if (contentType) messageParams.contentType = contentType;
    }
 
    // 调用 OpenClaw message 工具(内置工具函数)
    try {
        const result = await message(messageParams);
        return {
            success: true,
            result
        };
    } catch (error) {
        return {
            success: false,
            error: error.message
        };
    }
}
 
/**
 * 指挥 Agent 执行任务
 * @param {object} options - 任务指令选项
 * @param {string} options.agentId - Agent 标识
 * @param {string} options.task - 任务描述
 * @param {string} options.channel - 目标通道
 * @param {string} options.target - 目标用户/群聊 ID
 * @param {object} [options.params] - 任务参数
 * @returns {Promise<object>} - 发送结果
 */
async function assignTaskToAgent(options) {
    const { agentId, task, channel, target, params = {} } = options;
 
    const taskPayload = {
        agent: agentId,
        task: task,
        params: params,
        timestamp: new Date().toISOString(),
        priority: params.priority || 'normal'
    };
 
    const messageText = `🤖 Agent 任务指令
目标 Agent: ${agentId}
任务:${task}
参数:${JSON.stringify(params)}
优先级:${taskPayload.priority}
时间:${new Date().toLocaleString('zh-CN')}`;
 
    return await sendMessage({
        channel,
        target,
        message: messageText
    });
}
 
/**
 * 发送进度更新
 * @param {object} options - 进度更新选项
 * @param {string} options.channel - 目标通道
 * @param {string} options.target - 目标用户/群聊 ID
 * @param {number} options.taskId - 任务 ID
 * @param {number} options.progress - 进度百分比
 * @param {string} options.status - 状态
 * @param {string} [options.agentId] - 执行 Agent ID
 * @returns {Promise<object>} - 发送结果
 */
async function sendProgressUpdate(options) {
    const { channel, target, taskId, progress, status, agentId } = options;
 
    const progressBar = '█'.repeat(Math.floor(progress / 5)) + '░'.repeat(20 - Math.floor(progress / 5));
    
    const messageText = `📊 任务进度更新
任务 ID: ${taskId}
执行 Agent: ${agentId || 'N/A'}
进度:[${progressBar}] ${progress}%
状态:${status}
时间:${new Date().toLocaleString('zh-CN')}`;
 
    return await sendMessage({
        channel,
        target,
        message: messageText
    });
}
 
/**
 * 发送任务完成通知
 * @param {object} options - 通知选项
 * @param {string} options.channel - 目标通道
 * @param {string} options.target - 目标用户/群聊 ID
 * @param {number} options.taskId - 任务 ID
 * @param {string} options.taskName - 任务名称
 * @param {string} options.agentName - Agent 名称
 * @param {string} [options.result] - 任务结果
 * @param {string} [options.duration] - 执行时长
 * @returns {Promise<object>} - 发送结果
 */
async function sendTaskCompletion(options) {
    const { channel, target, taskId, taskName, agentName, result, duration } = options;
 
    const messageText = `✅ 任务完成
任务 ID: ${taskId}
任务名称:${taskName}
执行 Agent: ${agentName}
执行时长:${duration || 'N/A'}
结果:${result || '成功'}
时间:${new Date().toLocaleString('zh-CN')}`;
 
    return await sendMessage({
        channel,
        target,
        message: messageText
    });
}
 
/**
 * 发送错误通知
 * @param {object} options - 错误选项
 * @param {string} options.channel - 目标通道
 * @param {string} options.target - 目标用户/群聊 ID
 * @param {number} options.taskId - 任务 ID
 * @param {string} options.taskName - 任务名称
 * @param {string} options.error - 错误信息
 * @param {string} [options.agentId] - 执行 Agent ID
 * @returns {Promise<object>} - 发送结果
 */
async function sendErrorNotification(options) {
    const { channel, target, taskId, taskName, error, agentId } = options;
 
    const messageText = `❌ 任务失败
任务 ID: ${taskId}
任务名称:${taskName}
执行 Agent: ${agentId || 'N/A'}
错误:${error}
时间:${new Date().toLocaleString('zh-CN')}`;
 
    return await sendMessage({
        channel,
        target,
        message: messageText
    });
}
 
/**
 * 发送项目报告
 * @param {object} options - 报告选项
 * @param {string} options.channel - 目标通道
 * @param {string} options.target - 目标用户/群聊 ID
 * @param {string} options.report - 报告内容
 * @param {string} [options.projectName] - 项目名称
 * @returns {Promise<object>} - 发送结果
 */
async function sendProjectReport(options) {
    const { channel, target, report, projectName } = options;
 
    const filename = `${projectName || 'project'}-report-${Date.now()}.md`;
    
    return await sendMessage({
        channel,
        target,
        message: `📋 ${projectName || '项目'}报告已生成`,
        media: report,
        filename,
        contentType: 'text/markdown'
    });
}
 
/**
 * 发送图片
 * @param {object} options - 图片选项
 * @param {string} options.channel - 目标通道
 * @param {string} options.target - 目标用户/群聊 ID
 * @param {string} options.imageUrl - 图片 URL 或本地路径
 * @param {string} [options.caption] - 图片说明
 * @returns {Promise<object>} - 发送结果
 */
async function sendImage(options) {
    const { channel, target, imageUrl, caption = '' } = options;
 
    return await sendMessage({
        channel,
        target,
        message: caption,
        media: imageUrl,
        contentType: 'image'
    });
}
 
/**
 * 发送语音
 * @param {object} options - 语音选项
 * @param {string} options.channel - 目标通道
 * @param {string} options.target - 目标用户/群聊 ID
 * @param {string} options.audioPath - 音频文件路径
 * @returns {Promise<object>} - 发送结果
 */
async function sendVoice(options) {
    const { channel, target, audioPath } = options;
 
    return await sendMessage({
        channel,
        target,
        message: '',
        media: audioPath,
        contentType: 'audio'
    });
}
 
/**
 * 发送文件
 * @param {object} options - 文件选项
 * @param {string} options.channel - 目标通道
 * @param {string} options.target - 目标用户/群聊 ID
 * @param {string} options.filePath - 文件路径或 URL
 * @param {string} [options.filename] - 文件名
 * @returns {Promise<object>} - 发送结果
 */
async function sendFile(options) {
    const { channel, target, filePath, filename } = options;
 
    return await sendMessage({
        channel,
        target,
        message: `📎 文件:${filename || require('path').basename(filePath)}`,
        media: filePath,
        filename: filename || require('path').basename(filePath),
        contentType: 'file'
    });
}
 
/**
 * 发送 Agent 状态报告
 * @param {object} options - 状态报告选项
 * @param {string} options.channel - 目标通道
 * @param {string} options.target - 目标用户/群聊 ID
 * @param {string} options.agentId - Agent ID
 * @param {string} options.status - 状态(idle, busy, error)
 * @param {number} [options.tasksCompleted] - 完成任务数
 * @param {number} [options.tasksFailed] - 失败任务数
 * @returns {Promise<object>} - 发送结果
 */
async function sendAgentStatusReport(options) {
    const { channel, target, agentId, status, tasksCompleted = 0, tasksFailed = 0 } = options;
 
    const statusEmoji = {
        idle: '🟢',
        busy: '🟡',
        error: '🔴'
    }[status] || '⚪';
 
    const messageText = `📊 Agent 状态报告
Agent ID: ${agentId}
状态:${statusEmoji} ${status}
完成任务:${tasksCompleted}
失败任务:${tasksFailed}
时间:${new Date().toLocaleString('zh-CN')}`;
 
    return await sendMessage({
        channel,
        target,
        message: messageText
    });
}
 
/**
 * 发送多 Agent 协作状态
 * @param {object} options - 协作状态选项
 * @param {string} options.channel - 目标通道
 * @param {string} options.target - 目标用户/群聊 ID
 * @param {Array} options.agents - Agent 状态列表
 * @param {string} options.projectName - 项目名称
 * @returns {Promise<object>} - 发送结果
 */
async function sendMultiAgentStatus(options) {
    const { channel, target, agents, projectName } = options;
 
    let messageText = `🔄 ${projectName} 多 Agent 协作状态\n\n`;
    
    agents.forEach((agent, index) => {
        const statusEmoji = {
            idle: '🟢',
            busy: '🟡',
            error: '🔴'
        }[agent.status] || '⚪';
        
        messageText += `${index + 1}. ${statusEmoji} ${agent.name} (${agent.status})\n`;
        if (agent.currentTask) {
            messageText += `   当前任务:${agent.currentTask}\n`;
        }
    });
 
    messageText += `\n时间:${new Date().toLocaleString('zh-CN')}`;
 
    return await sendMessage({
        channel,
        target,
        message: messageText
    });
}
 
// 导出所有消息发送函数
module.exports = {
    sendMessage,
    assignTaskToAgent,
    sendProgressUpdate,
    sendTaskCompletion,
    sendErrorNotification,
    sendProjectReport,
    sendImage,
    sendVoice,
    sendFile,
    sendAgentStatusReport,
    sendMultiAgentStatus
};