status.mjs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/env node
  2. /**
  3. * status.mjs — 进度/成果查询(AgentTaskStatus + AgentDeliverable)
  4. *
  5. * 视角:
  6. * node status.mjs mine --agent <agentId> 本 agent 最近任务+交付
  7. * node status.mjs all [--agent <agentId>] [--limit N] 全集群(masterKey 视角)
  8. *
  9. * 输出 JSON:tasks(四态时间线倒序)+ deliverables(成果时间线倒序)。
  10. */
  11. import {
  12. resolveCredentials, failNoCredentials, queryObjects,
  13. } from './parse-client.mjs';
  14. const argv = process.argv.slice(2);
  15. const scope = argv[0] === 'all' ? 'all' : 'mine';
  16. // 选项从 argv[1] 起按对解析: --agent x --limit 20
  17. const args = {};
  18. for (let i = 1; i < argv.length; i += 2) {
  19. const key = (argv[i] || '').replace(/^--/, '');
  20. args[key] = argv[i + 1];
  21. }
  22. const limit = Math.min(parseInt(args.limit || '20', 10) || 20, 200);
  23. const cred = resolveCredentials();
  24. if (!cred) failNoCredentials();
  25. const taskWhere = args.agent ? { agentId: args.agent } : {};
  26. const delivWhere = args.agent ? { agentId: args.agent } : {};
  27. const [tasks, deliverables] = await Promise.all([
  28. queryObjects('AgentTaskStatus', cred, { where: taskWhere, limit, order: '-updatedAt' }),
  29. queryObjects('AgentDeliverable', cred, { where: delivWhere, limit, order: '-deliveredAt' }),
  30. ]);
  31. const fmtTask = (t) => ({
  32. agentId: t.agentId, taskId: t.taskId, taskName: t.taskName,
  33. status: t.status,
  34. startedAt: t.startedAt && t.startedAt.iso,
  35. endedAt: t.endedAt && t.endedAt.iso,
  36. heartbeatAt: t.heartbeatAt && t.heartbeatAt.iso,
  37. resultNote: t.resultNote,
  38. });
  39. const fmtDeliv = (d) => ({
  40. agentId: d.agentId, taskId: d.taskId, title: d.title,
  41. project: d.project, deliverableType: d.deliverableType,
  42. url: d.url, summary: d.summary,
  43. deliveredAt: d.deliveredAt && d.deliveredAt.iso,
  44. });
  45. console.log(JSON.stringify({
  46. ok: true,
  47. scope,
  48. agentId: args.agent || null,
  49. taskCount: tasks.length,
  50. deliverableCount: deliverables.length,
  51. tasks: tasks.map(fmtTask),
  52. deliverables: deliverables.map(fmtDeliv),
  53. }, null, 2));