/** * OpenClaw Skill 完整流程测试 * 模拟 OpenClaw 读取 api-config.json → 构造请求 → 提交任务 → 轮询结果 */ const fs = require('fs'); const path = require('path'); const TOKEN = "Bearer r:f0333969e312a40e4703e8fe4ed1c600"; // ========== Step 0: 读取 skill 配置 ========== function loadSkillConfig(skillDir) { const configPath = path.join(__dirname, skillDir, 'api-config.json'); const config = JSON.parse(fs.readFileSync(configPath, 'utf-8')); console.log(`[OpenClaw] 加载 skill: ${config.displayName} (${config.name})`); console.log(`[OpenClaw] 端点: ${config.endpoint.method} ${config.endpoint.url}`); console.log(`[OpenClaw] 必填参数: ${config.parameters.required.join(', ')}`); return config; } // ========== Step 1: 根据 api-config 构造并发送请求 ========== async function executeSkill(config, inputParams) { console.log(`\n[OpenClaw] 执行 skill: ${config.name}`); console.log(`[OpenClaw] 输入参数:`, JSON.stringify(inputParams, null, 2)); // 验证必填参数 for (const req of config.parameters.required) { if (!(req in inputParams)) { throw new Error(`缺少必填参数: ${req}`); } } // 构造请求 const response = await fetch(config.endpoint.url, { method: config.endpoint.method, headers: config.endpoint.headers, body: JSON.stringify(inputParams) }); const result = await response.json(); console.log(`[OpenClaw] 响应:`, JSON.stringify(result, null, 2)); return result; } // ========== Step 2: 轮询任务状态 ========== async function pollTaskResult(taskQueryConfig, workId, routerName, maxAttempts = 20, intervalMs = 3000) { console.log(`\n[OpenClaw] 开始轮询任务 workId=${workId}, routerName=${routerName}`); for (let i = 1; i <= maxAttempts; i++) { const result = await executeSkill(taskQueryConfig, { workId: workId, routerName: routerName, token: TOKEN }); if (result.code === 200 && result.data) { console.log(`[OpenClaw] 轮询 #${i}: tip="${result.data.tip}", isFinish=${result.data.isFinish}`); if (result.data.isFinish === true) { console.log(`[OpenClaw] ✅ 任务完成!`); return result; } } if (i < maxAttempts) { console.log(`[OpenClaw] 等待 ${intervalMs}ms 后重试...`); await new Promise(resolve => setTimeout(resolve, intervalMs)); } } console.log(`[OpenClaw] ⚠️ 轮询超时,已达最大尝试次数 ${maxAttempts}`); return null; } // ========== 主测试流程 ========== async function testImgV4Flow() { console.log("=".repeat(60)); console.log("测试1: jimeng-img-v4 图片生成4.0 完整流程"); console.log("=".repeat(60)); // Step 0: 加载两个 skill 配置 const imgConfig = loadSkillConfig('jimeng-img-v4'); const queryConfig = loadSkillConfig('jimeng-task-query'); // Step 1: 提交图片生成任务 const genResult = await executeSkill(imgConfig, { prompt: "一只可爱的橘猫在阳光下打盹,水彩画风格", sizeDate: { width: 1024, height: 1024 }, scale: 0.6, force_single: true, token: TOKEN }); if (genResult.code !== 200 || !genResult.data?.workId) { console.log("[OpenClaw] ❌ 任务提交失败"); return; } const workId = genResult.data.workId; console.log(`[OpenClaw] 任务已提交, workId = ${workId}`); // Step 2: 轮询任务结果 const taskResult = await pollTaskResult(queryConfig, workId, 'getImgV4'); if (taskResult) { console.log(`\n[OpenClaw] 🎉 jimeng-img-v4 完整流程测试通过!`); console.log(`[OpenClaw] workId: ${workId}`); console.log(`[OpenClaw] 可通过查询 ImagineWork 表 (objectId=${workId}) 获取 images 字段`); } } async function testText2ImgV3Flow() { console.log("\n" + "=".repeat(60)); console.log("测试2: jimeng-text2img-v3 文生图3.0 完整流程"); console.log("=".repeat(60)); const imgConfig = loadSkillConfig('jimeng-text2img-v3'); const queryConfig = loadSkillConfig('jimeng-task-query'); const genResult = await executeSkill(imgConfig, { prompt: "春天的樱花树下,一位穿和服的少女,唯美水彩风", use_pre_llm: true, sizeDate: { width: 1328, height: 1328 }, token: TOKEN }); if (genResult.code !== 200 || !genResult.data?.workId) { console.log("[OpenClaw] ❌ 任务提交失败"); return; } const workId = genResult.data.workId; console.log(`[OpenClaw] 任务已提交, workId = ${workId}`); const taskResult = await pollTaskResult(queryConfig, workId, 'getText2ImgV3'); if (taskResult) { console.log(`\n[OpenClaw] 🎉 jimeng-text2img-v3 完整流程测试通过!`); console.log(`[OpenClaw] workId: ${workId}`); } } async function main() { console.log("╔══════════════════════════════════════════════════════════╗"); console.log("║ OpenClaw Jimeng Skill 完整流程测试 ║"); console.log("╚══════════════════════════════════════════════════════════╝\n"); try { await testImgV4Flow(); await testText2ImgV3Flow(); } catch (err) { console.error("[OpenClaw] 测试出错:", err.message); } console.log("\n" + "=".repeat(60)); console.log("全部测试完成"); console.log("=".repeat(60)); } main();