open-api.js 38.3 KB
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 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
/**
 * Open API Routes — Skill-as-a-Service 对外接口
 *
 * 公开端点: install.md, SKILL.md, docs, blog
 * 认证端点: skills/search, skills/:name, skills/download, skills/create, jobs, feedback, updates
 */
const express = require('express');
const fs = require('fs');
const path = require('path');
const router = express.Router();
const { requireApiKey } = require('../middleware/api-key-auth');
const { apiAccessLogger } = require('../middleware/api-access-logger');
const { SkillRegistry } = require('../services/skill-registry');
const { SkillPackager } = require('../services/skill-packager');
const { JobManager } = require('../services/job-manager');
const config = require('../config');
const { resolveSkillDir } = require('../services/skill-dir-manager');

const skillsDir = config.skillsCodeDir;
const registry = new SkillRegistry(skillsDir);
const packager = new SkillPackager(skillsDir);
const jobManager = new JobManager({ storePath: path.resolve(__dirname, '../../.data/jobs-openapi.json') });

const h = (fn) => async (req, res) => {
  try { await fn(req, res); } catch (e) { console.error(`[OpenAPI] ${req.method} ${req.path}:`, e.message); if (!res.headersSent) res.status(500).json({ error: e.message }); }
};

// ─── 公开端点 ───

router.get('/install.md', h((req, res) => {
  const host = req.get('host') || 'localhost:3000';
  const baseUrl = `${req.protocol || 'http'}://${host}/api/open`;
  const allSkills = registry.listSkills().filter(s => s.status === 'enable');
  const fmtList = (arr) => arr.map(s => `  - **${s.skillName}**: ${s.description || '无描述'}`).join('\n');
  const layerSection = (label, arr) => arr.length > 0 ? [`### ${label}(${arr.length} 个)`, fmtList(arr), ''] : [];

  const md = [
    '---', 'name: arenask', 'description: ArenaSK 安装入口。下载连接器 Skill 后即可搜索、下载、安装 AI Skills。',
    'version: 1.0.0', `api_base: ${baseUrl}`, '---', '',
    '# ArenaSK Skill Store', '',
    '> **🤖 本文档面向 AI Agent。所有操作由你(AI)自主执行,无需人类手动操作。**',
    '> **你拥有 shell 和文件读写能力,直接执行以下命令即可。**', '', '---', '',
    '## 第一步:安装 ArenaSK 连接器', '', '下载 `arenask` Skill 到你的本地工作区:', '',
    '```bash', 'mkdir -p <WORKSPACE>/skills/arenask',
    `curl -s "${baseUrl}/skills/arenask/SKILL.md" > <WORKSPACE>/skills/arenask/SKILL.md`, '```', '',
    '将 `<WORKSPACE>` 替换为你的实际工作区路径。', '',
    '## 第二步:配置 API 凭证', '',
    '提示用户登录 ArenaSK 网站 (如 https://arenask.com),点击右上角头像下拉菜单复制专属的 **API Key**。', '然后将其保存到本地:', '',
    '```bash', 'mkdir -p <WORKSPACE>/.lemon',
    `cat > <WORKSPACE>/.lemon/credentials.json << 'EOF'`, '{', `  "api_base": "${baseUrl}",`, '  "api_key": "<用户提供的_API_KEY>"', '}', 'EOF', '```', '',
    '## 第三步:使用', '', '**每次需要搜索、下载或管理 Skills 时:**', '',
    '1. **阅读** `<WORKSPACE>/skills/arenask/SKILL.md`', '2. **按照 SKILL.md 中的指令**完成操作', '',
    '> ⚠️ **重要:不要记忆或缓存 API 调用方式。**',
    '> 每次都重新阅读 SKILL.md,它包含智能搜索匹配、错误处理和依赖解析逻辑。', '', '---', '',
    '## Skill Store 能力一览', '',
    '| 能力 | 说明 |', '|---|---|',
    '| **搜索** | 自然语言描述需求,智能匹配最佳 Skill |',
    '| **下载安装** | 一键下载 Skill 包并自动解析 L1→L2→L3 依赖树 |',
    '| **创建新 Skill** | 描述需求,异步创建全新 AI Skill |',
    '| **反馈改进** | 对已有 Skill 提出改进建议 |',
    '| **检查更新** | 查询已安装 Skill 的最新版本 |', '',
    `## 当前可用 Skills(${allSkills.length} 个)`, '',
    ...layerSection('L3 场景方案', allSkills.filter(s => s.layer === 3)),
    ...layerSection('L2 复合技能', allSkills.filter(s => s.layer === 2)),
    ...layerSection('L1 原子技能', allSkills.filter(s => s.layer === 1)),
    ...layerSection('其他', allSkills.filter(s => !s.layer)),
    '', '> 详细操作请安装后阅读本地 `skills/arenask/SKILL.md`。',
  ].join('\n');
  res.set('Content-Type', 'text/markdown; charset=utf-8').send(md);
}));

router.get('/skills/:name/SKILL.md', h((req, res) => {
  const dir = resolveSkillDir(req.params.name);
  const p = dir ? path.join(dir, 'SKILL.md') : null;
  if (!p || !fs.existsSync(p)) return res.status(404).send(`# Error\n\nSkill "${req.params.name}" not found.`);
  res.set('Content-Type', 'text/markdown; charset=utf-8').send(fs.readFileSync(p, 'utf-8'));
}));

router.get('/docs', h((req, res) => {
  const docsDir = path.resolve(__dirname, '../../docs');
  if (!fs.existsSync(docsDir)) return res.json([]);
  function scan(dir, prefix = '') {
    const items = [];
    for (const e of fs.readdirSync(dir, { withFileTypes: true })) {
      const rel = prefix ? `${prefix}/${e.name}` : e.name;
      if (e.isDirectory()) items.push(...scan(path.join(dir, e.name), rel));
      else if (e.name.endsWith('.md')) items.push(rel);
    }
    return items;
  }
  const rootDocs = ['README.md', 'CLAUDE.md', 'skilltree.md']
    .filter(f => fs.existsSync(path.resolve(__dirname, '../../', f))).map(f => `_root/${f}`);
  res.json([...rootDocs, ...scan(docsDir)]);
}));

router.get('/docs/*', h((req, res) => {
  const docPath = req.params[0];
  if (!docPath) return res.status(400).send('Missing doc path');
  const base = path.resolve(__dirname, '../..');
  const filePath = docPath.startsWith('_root/')
    ? path.resolve(base, docPath.replace('_root/', ''))
    : path.resolve(base, 'docs/', docPath);
  if (!filePath.startsWith(base)) return res.status(403).send('Forbidden');
  if (!fs.existsSync(filePath)) return res.status(404).send('Not found');
  res.set('Content-Type', 'text/markdown; charset=utf-8').send(fs.readFileSync(filePath, 'utf-8'));
}));

// ─── Blog ───

function _parseFrontmatter(content) {
  const m = content.match(/^---\n([\s\S]*?)\n---\n([\s\S]*)$/);
  if (!m) return { meta: {}, body: content };
  const meta = {};
  m[1].split('\n').forEach(line => {
    const kv = line.match(/^(\w[\w_]*?):\s*(.+)$/);
    if (!kv) return;
    let val = kv[2].trim().replace(/^['"]|['"]$/g, '');
    if (val.startsWith('[') && val.endsWith(']')) {
      try { val = JSON.parse(val); } catch { val = val.slice(1, -1).split(',').map(s => s.trim().replace(/^['"]|['"]$/g, '')); }
    }
    meta[kv[1]] = val;
  });
  return { meta, body: m[2] };
}

router.get('/blog', h((req, res) => {
  const blogDir = path.resolve(__dirname, '../../../data/blog');
  if (!fs.existsSync(blogDir)) return res.json({ posts: [], total: 0 });
  const limit = Math.min(parseInt(req.query.limit) || 20, 100);
  const offset = parseInt(req.query.offset) || 0;
  let posts = fs.readdirSync(blogDir).filter(f => f.endsWith('.md')).sort((a, b) => b.localeCompare(a))
    .map(f => ({ slug: f.replace(/\.md$/, ''), ..._parseFrontmatter(fs.readFileSync(path.join(blogDir, f), 'utf-8')).meta }));
  if (req.query.tag) posts = posts.filter(p => Array.isArray(p.tags) && p.tags.includes(req.query.tag));
  res.json({ posts: posts.slice(offset, offset + limit), total: posts.length, limit, offset });
}));

router.get('/blog/posts/:slug', h((req, res) => {
  const blogDir = path.resolve(__dirname, '../../../data/blog');
  const filePath = path.join(blogDir, `${req.params.slug}.md`);
  if (!filePath.startsWith(blogDir)) return res.status(403).json({ error: 'Forbidden' });
  if (!fs.existsSync(filePath)) return res.status(404).json({ error: 'Post not found' });
  const { meta, body } = _parseFrontmatter(fs.readFileSync(filePath, 'utf-8'));
  res.json({ slug: req.params.slug, ...meta, content: body });
}));

router.get('/blog/images/:filename', h((req, res) => {
  const imgDir = path.resolve(__dirname, '../../../data/blog/images');
  const filePath = path.join(imgDir, req.params.filename);
  if (!filePath.startsWith(imgDir) || !fs.existsSync(filePath)) return res.status(404).send('Not found');
  res.sendFile(filePath);
}));

// ─── 认证端点 ───
router.use(apiAccessLogger);
router.use(requireApiKey);

// ── Skill Name Generation ──
async function _generateSkillName(userQuery) {
  const { execFile } = require('child_process');
  const prompt = `从以下用户需求中,提取一个简洁的英文 snake_case 格式的 Skill 名称(2-4个单词,全小写,下划线连接)。只输出名称本身。\n示例:\n- "帮我找一个能够读取微博热搜的 skill" → weibo_hot_trends\n- "写七言绝句" → chinese_poetry\n用户需求:${userQuery}`;
  return new Promise((resolve) => {
    const timeout = setTimeout(() => resolve(_sanitizeName(userQuery)), 15000);
    execFile('claude', ['--print', '-p', prompt], { timeout: 15000 }, (err, stdout) => {
      clearTimeout(timeout);
      if (err || !stdout?.trim()) return resolve(_sanitizeName(userQuery));
      resolve(stdout.trim().split('\n')[0].replace(/['"`]/g, '').replace(/[^a-zA-Z0-9_]/g, '').toLowerCase() || _sanitizeName(userQuery));
    });
  });
}

function _sanitizeName(query) {
  const ascii = query.replace(/[^a-zA-Z0-9\s]/g, '').trim();
  return ascii.length > 3 ? ascii.toLowerCase().replace(/\s+/g, '_').substring(0, 40) : `skill_${Date.now()}`;
}

// ── Skill Search ──
router.get('/skills/search', h(async (req, res) => {
  const q = String(req.query.q || '');
  if (!q) return res.status(400).json({ error: 'Bad Request', message: 'Query parameter "q" is required.' });

  const results = registry.searchSkills(q, {
    domain: req.query.domain, type: req.query.type,
    limit: Math.min(parseInt(req.query.limit || '20') || 20, 100),
  });
  if (results.length > 0) return res.json({ results, total: results.length, query: q });

  // 未命中 → 自动创建
  const skillName = await _generateSkillName(q);
  const job = jobManager.createJob({ domain: String(req.query.domain || skillName), scope: '', description: `根据用户需求 "${q}" 自动创建`, keywords: [skillName, ...q.split(/\s+/).slice(0, 5)], apiKey: req.apiKey, user_id: req.user?.id });
  job.skillName = skillName;
  _runSkillCreation(job).catch(err => { jobManager.updateJob(job.jobId, { status: 'failed', error: err.message, progress: '自动创建失败' }); });
  res.json({ results: [], total: 0, query: q, autoCreate: { jobId: job.jobId, skillName, status: job.status, message: `未找到匹配 Skill,已启动创建「${skillName}」。轮询 GET /api/open/jobs/${job.jobId} 查看进度。` } });
}));

router.get('/skills/:name', h((req, res) => {
  const skill = registry.getSkill(req.params.name);
  skill ? res.json(SkillRegistry.toPublicInfo(skill)) : res.status(404).json({ error: 'Skill not found' });
}));

// ── Download ──
router.get('/skills/:name/download', h(async (req, res) => {
  const skill = registry.getSkill(req.params.name);
  if (!skill) return res.status(404).json({ error: 'Skill not found' });
  if (skill.status !== 'enable') return res.status(403).json({ error: 'Skill not available', message: `Status: ${skill.status}` });

  res.setHeader('Content-Type', 'application/zip');
  res.setHeader('Content-Disposition', `attachment; filename="${req.params.name}.zip"`);
  await packager.packageSkillZip(req.params.name, skill, res);

  registry.incrementUseCount(req.params.name);
  registry.incrementDownloadCount(req.params.name);
  const skillDb = require('../services/skill-db');
  skillDb.recordDownload(req.params.name, {
    userId: req.user?.id || 'system', userEmail: req.user?.email || 'system_api_key',
    version: skill.version, ipAddress: req.headers['x-forwarded-for'] || req.socket.remoteAddress,
  });
}));

// ── Create ──
router.post('/skills/create', h(async (req, res) => {
  const { domain, scope, description, keywords, webhookUrl } = req.body;
  if (!domain && !description) return res.status(400).json({ error: 'Bad Request', message: 'At least "domain" or "description" is required.' });

  const existing = registry.searchSkills(String(domain || description), { limit: 3 });
  const job = jobManager.createJob({ domain, scope, description, keywords, webhookUrl, apiKey: req.apiKey, user_id: req.user?.id });
  _runSkillCreation(job).catch(err => { jobManager.updateJob(job.jobId, { status: 'failed', error: err.message, progress: '创建失败' }); });
  res.status(202).json({ jobId: job.jobId, status: job.status, message: `Skill creation job queued. Poll GET /api/open/jobs/${job.jobId} for status.`, similarSkills: existing.length > 0 ? existing.map(s => s.skillName) : undefined });
}));

// ── Jobs ──
router.get('/jobs', h((req, res) => {
  res.json({ jobs: jobManager.listJobs({ apiKey: req.apiKey, limit: parseInt(req.query.limit) || 20 }), total: jobManager.listJobs({ apiKey: req.apiKey }).length });
}));

router.get('/jobs/:jobId', h((req, res) => {
  const job = jobManager.getPublicJob(req.params.jobId);
  job ? res.json(job) : res.status(404).json({ error: 'Job not found' });
}));

router.get('/jobs/:jobId/result', h(async (req, res) => {
  const job = jobManager.getJob(req.params.jobId);
  if (!job) return res.status(404).json({ error: 'Job not found' });
  if (job.status !== 'completed') return res.status(409).json({ error: 'Job not completed', status: job.status, progress: job.progress });
  if (!job.skillName) return res.status(500).json({ error: 'No skill name recorded' });
  res.setHeader('Content-Type', 'application/zip');
  res.setHeader('Content-Disposition', `attachment; filename="${job.skillName}.zip"`);
  await packager.packageSkillZip(job.skillName, registry.getSkill(job.skillName) || {}, res);
}));

// ── Feedback ──
router.post('/skills/:name/feedback', h(async (req, res) => {
  const { feedback, score } = req.body;
  if (!feedback || typeof feedback !== 'string') return res.status(400).json({ error: 'feedback (string) is required' });
  const skill = registry.getSkill(req.params.name);
  if (!skill) return res.status(404).json({ error: `Skill "${req.params.name}" not found` });

  const job = jobManager.createJob({ domain: skill.domain || '', scope: skill.scope || '', description: `[Feedback] ${feedback.substring(0, 100)}`, keywords: skill.keywords || [], apiKey: req.apiKey });
  job.skillName = req.params.name;

  const feedbackDir = path.join(skillsDir, req.params.name, 'feedback');
  fs.mkdirSync(feedbackDir, { recursive: true });
  fs.appendFileSync(path.join(feedbackDir, 'feedback_log.jsonl'), JSON.stringify({ ts: new Date().toISOString(), jobId: job.jobId, feedback, userScore: score || null, apiKey: req.apiKey ? req.apiKey.substring(0, 8) + '***' : null }) + '\n');

  _runFeedbackIteration(job, feedback, skill).catch(err => console.error(`[OpenAPI] Feedback error:`, err));
  res.status(202).json({ jobId: job.jobId, message: '反馈已接收,迭代任务已启动', currentVersion: skill.version });
}));

// ── Updates ──
router.get('/skills/:name/updates', h((req, res) => {
  const skill = registry.getSkill(req.params.name);
  if (!skill) return res.status(404).json({ error: `Skill "${req.params.name}" not found` });
  const hasUpdate = req.query.currentVersion && req.query.currentVersion !== skill.version;
  let changelog = '';
  if (hasUpdate) {
    try { changelog = fs.readFileSync(path.join(skillsDir, req.params.name, 'changelog.md'), 'utf-8'); } catch {}
    registry.recordUpdate(req.params.name, req.query.currentVersion, skill.version);
  }
  res.json({ skillName: req.params.name, latestVersion: skill.version, currentScore: skill.currentScore, hasUpdate: !!hasUpdate, changelog: hasUpdate ? changelog : undefined, downloadCount: skill.downloadCount || 0, updateCount: skill.updateCount || 0 });
}));

// ── Company Agent Invocation ──
router.post('/companies/:companyId/agents/:agentId/invoke', h(async (req, res) => {
  const { companyId, agentId } = req.params;
  const { task, contextFiles, webhookUrl } = req.body;

  if (!task || typeof task !== 'string') {
    return res.status(400).json({ error: 'Bad Request', message: 'The "task" field is required and must be a string.' });
  }

  // Create an async job
  const job = jobManager.createJob({ 
    description: `Invoke Agent ${agentId} in Company ${companyId}`, 
    webhookUrl, 
    apiKey: req.apiKey, 
    user_id: req.user?.id 
  });
  
  // Custom tracking fields
  job.companyId = companyId;
  job.agentId = agentId;
  jobManager.updateJob(job.jobId, { status: 'queued', progress: 'Task is waiting for execution queue...' });

  // Run in background without blocking
  _runAgentInvocation(job, { companyId, agentId, task, contextFiles }).catch(err => {
    jobManager.updateJob(job.jobId, { status: 'failed', error: err.message, progress: `Invocation failed: ${err.message}` });
  });

  res.status(202).json({
    jobId: job.jobId,
    status: job.status,
    message: `Agent invocation job queued. Poll GET /api/open/jobs/${job.jobId} for status.`
  });
}));

// ── Global (System-level) Agent Invocation ──
// 面向不挂任何公司的全局 agent(master / agent-creator / h3-video-prompt 这类系统级
// specialist)。company-scoped 的 /companies/:companyId/agents/:agentId/invoke 强制查
// company_agents「雇没雇」,全局 agent 从来不属于任何公司,套那条路一定 404。
// 这条不查雇佣关系,只查本体是否存在,会话落公司态目录的 `_global` 保留位
// (agent-locator 的既定行为,跟 heartbeat-service/agent-chat.js 里的全局 agent 是同一回事)。
router.post('/agents/:agentId/invoke', h(async (req, res) => {
  const { agentId } = req.params;
  const { task, contextFiles, webhookUrl } = req.body;

  if (!task || typeof task !== 'string') {
    return res.status(400).json({ error: 'Bad Request', message: 'The "task" field is required and must be a string.' });
  }
  if (agentId.includes(':')) {
    return res.status(400).json({ error: 'Bad Request', message: 'This endpoint is for global agents only (no "companyId:role" prefix). Use /companies/:companyId/agents/:agentId/invoke for company-hired agents.' });
  }
  const locator = require('../services/agent-locator');
  if (!fs.existsSync(path.join(locator.bodyDir(agentId), 'config.json'))) {
    return res.status(404).json({ error: `Agent "${agentId}" not found` });
  }

  const job = jobManager.createJob({
    description: `Invoke global agent ${agentId}`,
    webhookUrl,
    apiKey: req.apiKey,
    user_id: req.user?.id
  });
  job.agentId = agentId;
  jobManager.updateJob(job.jobId, { status: 'queued', progress: 'Task is waiting for execution queue...' });

  _runGlobalAgentInvocation(job, { agentId, task, contextFiles }).catch(err => {
    jobManager.updateJob(job.jobId, { status: 'failed', error: err.message, progress: `Invocation failed: ${err.message}` });
  });

  res.status(202).json({
    jobId: job.jobId,
    status: job.status,
    message: `Agent invocation job queued. Poll GET /api/open/jobs/${job.jobId} for status.`
  });
}));

// ── Pipelines (简化版) ──
// 读取 .data/pipelines.json
let pipelinesData = { pipelines: [] };
const pipelinesPath = path.resolve(__dirname, '../../.data/pipelines.json');
try { pipelinesData = JSON.parse(fs.readFileSync(pipelinesPath, 'utf-8')); } catch {}

router.get('/pipelines', h((req, res) => {
  const limit = parseInt(req.query.limit) || 20;
  res.json({ results: pipelinesData.pipelines.slice(0, limit), total: pipelinesData.pipelines.length });
}));

router.post('/pipelines', h((req, res) => {
  const { name, description, config } = req.body;
  const newPipeline = { id: `p-${Date.now()}`, name, description, config, status: 'idle', createdAt: new Date().toISOString() };
  pipelinesData.pipelines.push(newPipeline);
  fs.writeFileSync(pipelinesPath, JSON.stringify(pipelinesData, null, 2));
  res.status(201).json(newPipeline);
}));

router.get('/pipelines/:pipelineId', h((req, res) => {
  const pipeline = pipelinesData.pipelines.find(p => p.id === req.params.pipelineId);
  pipeline ? res.json(pipeline) : res.status(404).json({ error: 'Pipeline not found' });
}));

router.put('/pipelines/:pipelineId', h((req, res) => {
  const idx = pipelinesData.pipelines.findIndex(p => p.id === req.params.pipelineId);
  if (idx === -1) return res.status(404).json({ error: 'Pipeline not found' });
  const { name, description, config } = req.body;
  pipelinesData.pipelines[idx] = { ...pipelinesData.pipelines[idx], name, description, config };
  fs.writeFileSync(pipelinesPath, JSON.stringify(pipelinesData, null, 2));
  res.json(pipelinesData.pipelines[idx]);
}));

router.delete('/pipelines/:pipelineId', h((req, res) => {
  const idx = pipelinesData.pipelines.findIndex(p => p.id === req.params.pipelineId);
  if (idx === -1) return res.status(404).json({ error: 'Pipeline not found' });
  pipelinesData.pipelines.splice(idx, 1);
  fs.writeFileSync(pipelinesPath, JSON.stringify(pipelinesData, null, 2));
  res.status(204).send();
}));

router.post('/pipelines/:pipelineId/run', h((req, res) => {
  const pipeline = pipelinesData.pipelines.find(p => p.id === req.params.pipelineId);
  if (!pipeline) return res.status(404).json({ error: 'Pipeline not found' });
  pipeline.status = 'running';
  pipeline.lastRun = new Date().toISOString();
  fs.writeFileSync(pipelinesPath, JSON.stringify(pipelinesData, null, 2));
  // TODO: 实际执行 Pipeline 逻辑
  res.json({ message: 'Pipeline started', pipeline });
}));

// ══════════════════════════════════════
// Internal Pipelines
// ══════════════════════════════════════

async function _runFeedbackIteration(job, feedback, skill) {
  const { ConversationLogger } = require('../services/conversation-logger');
  const taskMgr = require('../services/skill-task-manager');
  const { randomUUID } = require('crypto');
  const { resolveTrainedSkillDir } = require('../services/skill-dir-manager');
  const skillName = job.skillName;
  const userId = job.user_id || null;

  // 优先从训练目录解析 skill,回退 data/skills/
  const skillDir = (userId && resolveTrainedSkillDir(skillName, userId))
    || resolveSkillDir(skillName)
    || path.join(skillsDir, skillName);

  const conversationsDir = path.resolve(__dirname, '../../conversations');
  const userConvDir = userId ? path.join(conversationsDir, `user-${userId}`) : conversationsDir;
  const conversationId = `api-feedback-${job.jobId}`;
  const logger = new ConversationLogger(userConvDir, conversationId, `msg-${Date.now()}-${randomUUID().substring(0, 8)}`, { userId });

  taskMgr.initTask(skillDir, { skillName, domain: skill.domain, scope: skill.scope, description: feedback, source: 'feedback', jobId: job.jobId, feedback, pipelineType: 'feedback' });
  logger.ensureMeta(`[Feedback] ${skillName}: ${feedback.substring(0, 80)}`);
  logger.logUserMessage(`[Feedback] ${feedback}`);

  try {
    const { loadConfigFromSkills } = require('../src/core/skill-config-loader');
    const backends = JSON.parse(fs.readFileSync(path.join(__dirname, '../config/backends.json'), 'utf-8'));
    const AgentManager = require('../src/core/agent-manager');
    const agentWorkDir = path.dirname(skillDir);
    const manager = new AgentManager(loadConfigFromSkills(skillsDir, backends), agentWorkDir);
    const onData = (agent) => (chunk) => { if (chunk?.trim()) logger.logStreamChunk(agent, chunk); };

    let existingKnowhow = '', existingBenchmark = '';
    try { existingKnowhow = fs.readFileSync(path.join(skillDir, 'hunt', 'knowhow.md'), 'utf-8'); } catch {}
    try { existingKnowhow = existingKnowhow || fs.readFileSync(path.join(skillDir, 'hunter', 'knowhow.md'), 'utf-8'); } catch {}
    try { existingBenchmark = fs.readFileSync(path.join(skillDir, 'digest', 'benchmark.md'), 'utf-8'); } catch {}

    // Step 1: Re-Hunt
    taskMgr.updateStep(skillDir, 're_hunt', 'running');
    jobManager.updateJob(job.jobId, { status: 'hunting', progress: '根据反馈重新猎取知识...', skillName, conversationId });
    await manager.callAgent('l3-hunt', `用户对 Skill "${skillName}" 反馈:\n> ${feedback}\n\n请结合已有知识补充猎取。现有 knowhow 摘要:\n${existingKnowhow.substring(0, 500)}`, { onData: onData('l3-hunt') });
    taskMgr.updateStep(skillDir, 're_hunt', 'done');

    // Step 2: Re-Digest
    taskMgr.updateStep(skillDir, 're_digest', 'running');
    jobManager.updateJob(job.jobId, { status: 'digesting', progress: '迭代评估基准...' });
    await manager.callAgent('l2-digest', `请根据用户反馈迭代 Skill "${skillName}" 的 benchmark。反馈: ${feedback}\n现有 benchmark 摘要:\n${existingBenchmark.substring(0, 300)}`, { onData: onData('l2-digest') });
    taskMgr.updateStep(skillDir, 're_digest', 'done');

    // Step 3: Evolve
    taskMgr.updateStep(skillDir, 'evolve', 'running');
    jobManager.updateJob(job.jobId, { status: 'evolving', progress: '进化优化中 (目标 ≥95分)...' });
    const evolveResult = await manager.callAgent('innovator-evolver', `请对 Skill "${skillName}" 进行进化优化。反馈: ${feedback}\n通过阈值: 95 分, 最大迭代: 10 次`, { onData: onData('innovator-evolver') });
    taskMgr.updateStep(skillDir, 'evolve', 'done');

    // Step 4: Publish
    taskMgr.updateStep(skillDir, 'publish', 'running');
    jobManager.updateJob(job.jobId, { status: 'publishing', progress: '发布新版本...' });
    const scoreMatch = (evolveResult.output || '').match(/最终分数[::]\s*(\d+)/);
    const finalScore = scoreMatch ? parseInt(scoreMatch[1]) : null;
    const updatedSkill = registry.bumpVersion(skillName, 'minor', finalScore);

    const changelogPath = path.join(skillDir, 'changelog.md');
    let existing = ''; try { existing = fs.readFileSync(changelogPath, 'utf-8'); } catch {}
    fs.writeFileSync(changelogPath, `## v${updatedSkill.version} (${new Date().toISOString().substring(0, 10)})\n\n**触发**: 用户反馈\n> ${feedback}\n\n**改进**: Re-Hunt → Re-Digest → Evolve\n${finalScore ? `**评分**: ${finalScore}/100` : ''}\n\n---\n\n${existing}`, 'utf-8');
    taskMgr.updateStep(skillDir, 'publish', 'done');

    const { InnovatorService } = require('../services/innovator-service');
    new InnovatorService({ skillsDir, skillsDataDir: config.skillsDataDir, trainedSkillsDir: config.trainedSkillsDir }).autoRegisterNewSkills();

    logger.logResult({ type: 'feedback_iteration', jobId: job.jobId, skillName, newVersion: updatedSkill.version, finalScore, feedback });
    jobManager.updateJob(job.jobId, { status: 'completed', progress: `迭代完成,新版本 v${updatedSkill.version}`, conversationId, result: { skillName, newVersion: updatedSkill.version, finalScore, feedback } });
    try { await manager.closeAllSessions(); } catch {}
  } catch (err) {
    logger.logError(err.message);
    const nextStep = taskMgr.getNextPendingStep(skillDir);
    if (nextStep) taskMgr.updateStep(skillDir, nextStep, 'failed', err.message);
    jobManager.updateJob(job.jobId, { status: 'failed', error: err.message, progress: `迭代失败: ${err.message}` });
    throw err;
  }
}

async function _runSkillCreation(job, resumeFromStep = null) {
  const { InnovatorService } = require('../services/innovator-service');
  const { ConversationLogger } = require('../services/conversation-logger');
  const taskMgr = require('../services/skill-task-manager');
  const { randomUUID } = require('crypto');
  const { initTrainedSkillDir } = require('../services/skill-dir-manager');

  const userId = job.user_id || null;
  const suggestedName = job.skillName || (job.domain || job.description || 'new_skill').toLowerCase().replace(/[^a-z0-9\u4e00-\u9fff]+/g, '_').replace(/^_|_$/g, '').substring(0, 40);
  job.skillName = suggestedName;

  // 有 userId → 写入训练目录;无 → 回退 data/skills/
  const skillDir = userId
    ? initTrainedSkillDir(suggestedName, userId)
    : path.join(skillsDir, suggestedName);

  const conversationsDir = path.resolve(__dirname, '../../conversations');
  const userConvDir = userId ? path.join(conversationsDir, `user-${userId}`) : conversationsDir;
  const conversationId = `api-create-${job.jobId}`;
  const logger = new ConversationLogger(userConvDir, conversationId, `msg-${Date.now()}-${randomUUID().substring(0, 8)}`, { userId });
  const isResume = !!resumeFromStep;

  if (!isResume) taskMgr.initTask(skillDir, { skillName: suggestedName, domain: job.domain, scope: job.scope, description: job.description, keywords: job.keywords, source: job.source || 'api', jobId: job.jobId, userId: String(userId || '') });
  logger.ensureMeta(`[API] 创建 Skill: ${suggestedName}${isResume ? ' (恢复)' : ''}`);
  logger.logUserMessage(`${isResume ? '[恢复]' : '[Open API]'} Skill 创建:${suggestedName}`);

  try {
    jobManager.updateJob(job.jobId, { status: 'hunting', progress: isResume ? `恢复中 (从 ${resumeFromStep})...` : '正在猎取领域知识...', skillName: suggestedName, conversationId });
    try {
      registry.registerSkill({
        skillName: suggestedName, description: job.description || `${job.domain} 相关 Skill`,
        domain: job.domain || '', scope: job.scope || '', keywords: job.keywords || [],
        directory: skillDir, userId: String(userId || ''), storageType: userId ? 'trained' : 'marketplace',
      });
    } catch (err) {
      if (err.message.includes('already exists')) registry.updateSkill(suggestedName, { status: 'training', directory: skillDir });
      else throw err;
    }

    const { loadConfigFromSkills } = require('../src/core/skill-config-loader');
    const backends = JSON.parse(fs.readFileSync(path.join(__dirname, '../config/backends.json'), 'utf-8'));
    const AgentManager = require('../src/core/agent-manager');
    const agentWorkDir = path.dirname(skillDir);
    const manager = new AgentManager(loadConfigFromSkills(skillsDir, backends), agentWorkDir);
    const onData = (agent) => (chunk) => { if (chunk?.trim()) logger.logStreamChunk(agent, chunk); };
    const startIdx = resumeFromStep ? ['hunt', 'digest', 'skill_create'].indexOf(resumeFromStep) : 0;

    if (startIdx <= 0) {
      taskMgr.updateStep(skillDir, 'hunt', 'running');
      jobManager.updateJob(job.jobId, { status: 'hunting', progress: '正在猎取领域知识 (Hunt)...' });
      await manager.callAgent('l3-hunt', `请为 ${suggestedName} 猎取知识。领域: ${job.domain || '未指定'}, 描述: ${job.description || '未指定'}`, { onData: onData('l3-hunt') });
      taskMgr.updateStep(skillDir, 'hunt', 'done');
    }
    if (startIdx <= 1) {
      taskMgr.updateStep(skillDir, 'digest', 'running');
      jobManager.updateJob(job.jobId, { status: 'digesting', progress: '正在消化提炼知识 (Digest)...' });
      await manager.callAgent('l2-digest', `请消化 ${suggestedName}/hunter/ 下的知识,生成 benchmark。`, { onData: onData('l2-digest') });
      taskMgr.updateStep(skillDir, 'digest', 'done');
    }
    if (startIdx <= 2) {
      taskMgr.updateStep(skillDir, 'skill_create', 'running');
      jobManager.updateJob(job.jobId, { status: 'creating', progress: '正在生成 SKILL.md...' });
      const allSkills = registry.listSkills();
      const availableAtoms = allSkills.filter(s => s.layer === 1 || s.layer === 2).map(s => `  - ${s.skillName} (L${s.layer}): ${s.description || '无描述'}`).join('\n');
      await manager.callAgent('skill-creator', `请为 ${suggestedName} 创建 SKILL.md。描述: ${job.description || '未指定'}\n可用 L1/L2 技能:\n${availableAtoms}`, { onData: onData('skill-creator') });
      taskMgr.updateStep(skillDir, 'skill_create', 'done');
    }

    const innovator = new InnovatorService({ skillsDir, skillsDataDir: config.skillsDataDir, trainedSkillsDir: config.trainedSkillsDir });
    innovator.autoRegisterNewSkills();
    logger.logResult({ type: 'api_skill_creation', jobId: job.jobId, skillName: suggestedName, resumed: isResume });
    jobManager.updateJob(job.jobId, { status: 'completed', progress: 'Skill 创建完成', conversationId, result: { skillName: suggestedName, output: 'Skill 创建完成' } });
    registry.updateSkill(suggestedName, { status: 'enable' });
    try { await manager.closeAllSessions(); } catch {}
  } catch (err) {
    logger.logError(err.message);
    const nextStep = taskMgr.getNextPendingStep(skillDir);
    if (nextStep) taskMgr.updateStep(skillDir, nextStep, 'failed', err.message);
    jobManager.updateJob(job.jobId, { status: 'failed', error: err.message, progress: `创建失败: ${err.message}` });
    try { registry.updateSkill(suggestedName, { status: 'enable' }); } catch {}
    throw err;
  }
}

async function _runAgentInvocation(job, payload) {
  const { companyId, agentId, task, contextFiles } = payload;

  // 「这家公司有没有这个 agent」查 company_agents(28 批次 4-19)。
  // 原来判的是公司私有目录存不存在 —— 那个目录整体没了,再判就是无条件报 404。
  const locator = require('../services/agent-locator');
  const { listCompanyAgents } = require('../services/company-service');
  const hired = listCompanyAgents(companyId).find((r) => r.role_name === agentId);
  if (!hired) {
    throw new Error(`Agent not hired by company: ${companyId}:${agentId}`);
  }
  // 附件落公司态目录(长期,跨任务),不落任务态 —— 外部系统传进来的上下文
  // 不属于某一次 run,run 目录会按保留策略清掉(28 §5.6)。
  const agentDir = locator.companyDir(hired.system_agent_id, companyId);
  const namespacedId = `${companyId}:${agentId}`;

  await _executeAgentInvocation(job, { agentDir, namespacedId, task, contextFiles });
}

// 全局 agent(不挂任何公司,落 agent-locator 的 `_global` 保留位)版本 —— 无雇佣关系可查,
// 本体存在性已经在路由层校验过。
async function _runGlobalAgentInvocation(job, payload) {
  const { agentId, task, contextFiles } = payload;
  const locator = require('../services/agent-locator');
  const agentDir = locator.companyDir(agentId); // 不传 companyId 落 _global
  await _executeAgentInvocation(job, { agentDir, namespacedId: agentId, task, contextFiles });
}

// 两条 invoke 路径共用的核心:处理附件、跑 AgentManager、写 job 结果。
async function _executeAgentInvocation(job, { agentDir, namespacedId, task, contextFiles }) {
  const path = require('path');
  const fs = require('fs');
  const { loadConfigFromSkills } = require('../src/core/skill-config-loader');
  const AgentManager = require('../src/core/agent-manager');
  const config = require('../config');
  // 复用 agent-chat.js 的会话存储,让 API 调用也能在网页 AgentChat 里追溯
  // (否则 job 结果只落 .data/jobs-openapi.json,网页永远看不到)。
  const agentChat = require('./agent-chat');
  const convId = `conv-${Date.now().toString(36)}`;

  jobManager.updateJob(job.jobId, { status: 'running', progress: 'Initializing agent session...' });

  agentChat.saveMeta(namespacedId, convId, {
    conversationId: convId,
    agentId: namespacedId,
    title: task.slice(0, 50),
    type: 'api_invoke',
    createdAt: new Date().toISOString(),
    status: 'running',
    stats: { messageCount: 1, toolCallCount: 0 },
    jobId: job.jobId,
  });
  agentChat.logMessage(namespacedId, convId, 'user', task);
  agentChat.appendHistory(namespacedId, convId, { role: 'user', content: task, messageId: `job-${job.jobId}`, timestamp: new Date().toISOString() });

  let enrichedTask = task;

  // Process Context Files
  if (Array.isArray(contextFiles) && contextFiles.length > 0) {
    jobManager.updateJob(job.jobId, { progress: `Processing ${contextFiles.length} context files...` });
    
    let contextBlock = '\n\n---\n📎 **附件上下文** (外部系统传入的数据):\n\n';
    const uploadDir = path.join(agentDir, 'uploads', job.jobId);
    fs.mkdirSync(uploadDir, { recursive: true });

    for (const file of contextFiles) {
      if (file.name && file.content) {
        // Sanitize file name
        const safeName = file.name.replace(/[\\/:*?"<>|]/g, '_').slice(0, 100);
        const filePath = path.join(uploadDir, safeName);
        fs.writeFileSync(filePath, String(file.content), 'utf-8');
        
        // Truncate for prompt injection if massive
        const fileContent = String(file.content);
        const displayContent = fileContent.length > 50000 
          ? fileContent.substring(0, 50000) + '\\n... (截断)' 
          : fileContent;
          
        contextBlock += `### 📄 ${safeName}\n\`\`\`\n${displayContent}\n\`\`\`\n\n`;
      }
    }
    
    enrichedTask = contextBlock + '\n\n---\n' + enrichedTask;
  }

  // Set up AgentManager
  // We initialize the base config and instantiate manager
  const backendsDir = path.join(__dirname, '../config/backends.json');
  const backends = fs.existsSync(backendsDir) ? JSON.parse(fs.readFileSync(backendsDir, 'utf-8')) : {};
  const skillsDir = config.skillsCodeDir;
  const rootDir = path.resolve(__dirname, '../../');
  
  const fullSystemConfig = loadConfigFromSkills(skillsDir, backends);
  const manager = new AgentManager(fullSystemConfig, rootDir);

  // Execute via AgentManager
  jobManager.updateJob(job.jobId, { status: 'running', progress: 'Executing agent task...' });

  let fullOutput = '';
  const onData = (agent) => (chunk) => {
     if (chunk?.trim()) {
       fullOutput += chunk;
     }
  };

  try {
    const result = await manager.callAgent(namespacedId, enrichedTask, { onData: onData(namespacedId) });
    const output = result.output || fullOutput || '(Empty Output)';

    jobManager.updateJob(job.jobId, {
      status: 'completed',
      progress: 'Invocation completed',
      result: {
        output
      }
    });

    agentChat.logMessage(namespacedId, convId, 'agent', output);
    agentChat.appendHistory(namespacedId, convId, { role: 'assistant', content: output, messageId: `job-${job.jobId}-out`, timestamp: new Date().toISOString() });
    const meta = agentChat.loadMeta(namespacedId, convId);
    if (meta) { meta.status = 'completed'; agentChat.saveMeta(namespacedId, convId, meta); }
    agentChat.flushLogBufferForConv(namespacedId, convId);

    try { await manager.closeAllSessions(); } catch {}
  } catch (err) {
    jobManager.updateJob(job.jobId, { status: 'failed', error: err.message, progress: `Agent execution failed: ${err.message}` });
    const meta = agentChat.loadMeta(namespacedId, convId);
    if (meta) { meta.status = 'failed'; agentChat.saveMeta(namespacedId, convId, meta); }
    agentChat.flushLogBufferForConv(namespacedId, convId);
    try { await manager.closeAllSessions(); } catch {}
    throw err;
  }
}

module.exports = router;
module.exports._jobManager = jobManager;
module.exports._runSkillCreation = _runSkillCreation;
module.exports._runFeedbackIteration = _runFeedbackIteration;
module.exports._runAgentInvocation = _runAgentInvocation;
module.exports._runGlobalAgentInvocation = _runGlobalAgentInvocation;