@hcmai/cli 0.15.0 → 0.15.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -4600,7 +4600,8 @@ import {
4600
4600
  fetchConversationTimeline as fetchConversationTimeline2,
4601
4601
  fetchConversationTimelineStrict,
4602
4602
  fetchRecentConversations,
4603
- findLastAssistantSeq,
4603
+ findLastAssistantTarget,
4604
+ SUPERSEDED_STATUS,
4604
4605
  CliError as CliError16,
4605
4606
  CliErrorCode as CliErrorCode15,
4606
4607
  parseInteractionRequest as parseInteractionRequest2,
@@ -4719,21 +4720,39 @@ function eventScope(value) {
4719
4720
  if (value.subLoop === false) return "root";
4720
4721
  return "-";
4721
4722
  }
4723
+ function toolState(tool) {
4724
+ return String(tool?.state ?? "").toLowerCase();
4725
+ }
4722
4726
  function eventStatus(value) {
4723
- if (typeof value.status === "string" && value.status.trim()) return oneLine(value.status, 18);
4727
+ if (typeof value.status === "string" && value.status.trim()) return oneLine(value.status, 26);
4724
4728
  if (value.live === true) return "live";
4725
4729
  const tools = value.tools;
4726
4730
  if (Array.isArray(tools) && tools.length) {
4727
- if (tools.some((tool) => String(tool.state ?? "").toLowerCase() === "failed")) {
4728
- return "failed";
4729
- }
4730
- if (tools.some((tool) => String(tool.state ?? "").toLowerCase() === "running")) {
4731
- return "running";
4732
- }
4731
+ if (tools.some((tool) => toolState(tool) === "error")) return "failed";
4732
+ if (tools.some((tool) => toolState(tool) === "halted")) return "halted";
4733
+ if (tools.some((tool) => toolState(tool) === "running")) return "running";
4733
4734
  return "done";
4734
4735
  }
4735
4736
  return "-";
4736
4737
  }
4738
+ function asRecord(value) {
4739
+ return value && typeof value === "object" && !Array.isArray(value) ? value : void 0;
4740
+ }
4741
+ function readSubToolStats(tool) {
4742
+ const t = asRecord(tool);
4743
+ if (!t) return void 0;
4744
+ const candidate = asRecord(t.subToolStats);
4745
+ if (!candidate) return void 0;
4746
+ const total = candidate.total;
4747
+ const failed2 = candidate.failed;
4748
+ if (typeof total !== "number" || typeof failed2 !== "number") return void 0;
4749
+ return { total, failed: failed2, errorCodes: candidate.errorCodes };
4750
+ }
4751
+ function formatSubToolStats(tool) {
4752
+ const stats = readSubToolStats(tool);
4753
+ if (!stats) return "";
4754
+ return `(sub ${stats.total} tool${stats.total === 1 ? "" : "s"} \xB7 ${stats.failed} failed)`;
4755
+ }
4737
4756
  function stableJson(value) {
4738
4757
  try {
4739
4758
  return JSON.stringify(value);
@@ -4741,19 +4760,31 @@ function stableJson(value) {
4741
4760
  return String(value ?? "");
4742
4761
  }
4743
4762
  }
4744
- function eventPreview(value) {
4763
+ function firstText(value) {
4745
4764
  for (const key2 of ["text", "content", "summary", "question", "detail", "message"]) {
4746
- if (typeof value[key2] === "string" && value[key2].trim()) return oneLine(value[key2], 86);
4765
+ const v = value[key2];
4766
+ if (typeof v === "string" && v.trim()) return v;
4767
+ }
4768
+ return void 0;
4769
+ }
4770
+ function guardRedirectPreview(value) {
4771
+ const cause = typeof value.cause === "string" && value.cause.trim() ? value.cause : "";
4772
+ const body = firstText(value) ?? "";
4773
+ return oneLine(`\u27F2 \u6253\u56DE ${body}${cause ? ` \xB7 ${cause}` : ""}`, 86);
4774
+ }
4775
+ function eventPreview(value) {
4776
+ if (value.kind === "guard-redirect") return guardRedirectPreview(value);
4777
+ const text = firstText(value);
4778
+ if (text !== void 0) {
4779
+ const superseded = value.kind === "chat-text" && value.status === SUPERSEDED_STATUS;
4780
+ return superseded ? `\u2298 ${oneLine(text, 84)}` : oneLine(text, 86);
4747
4781
  }
4748
4782
  const tools = value.tools;
4749
4783
  if (Array.isArray(tools) && tools.length) {
4750
- return oneLine(
4751
- tools.map((tool) => {
4752
- const t = tool;
4753
- return `${String(t.toolName ?? t.name ?? "-")}:${String(t.state ?? t.status ?? "-")}`;
4754
- }).join(" \xB7 "),
4755
- 86
4756
- );
4784
+ const list = tools;
4785
+ const prefix = list.map((t) => formatSubToolStats(t)).filter((s) => s !== "").join(" ");
4786
+ const body = list.map((t) => `${String(t.toolName ?? t.name ?? "-")}:${String(t.state ?? t.status ?? "-")}`).join(" \xB7 ");
4787
+ return oneLine(prefix ? `${prefix} ${body}` : body, 86);
4757
4788
  }
4758
4789
  const activityLog = value.activityLog;
4759
4790
  if (Array.isArray(activityLog) && activityLog.length) {
@@ -4769,6 +4800,24 @@ function eventPreview(value) {
4769
4800
  if (part && typeof part === "object") return oneLine(stableJson(part), 86);
4770
4801
  return oneLine(stableJson(value), 86);
4771
4802
  }
4803
+ function buildEventRows(items) {
4804
+ return items.map((item) => {
4805
+ const value = item;
4806
+ return {
4807
+ SEQ: value.seq ?? "-",
4808
+ KIND: oneLine(value.kind, 18),
4809
+ AGENT: eventAgent(value),
4810
+ SCOPE: eventScope(value),
4811
+ STATUS: eventStatus(value),
4812
+ EVENT: eventPreview(value)
4813
+ };
4814
+ });
4815
+ }
4816
+ function lastAssistantMissDetail(target) {
4817
+ const head = target.reason === "superseded-only" ? "\u672C\u8F6E\u7684\u7EC8\u7B54\u88AB\u5F15\u64CE\u5B88\u536B\u6253\u56DE\uFF08SUPERSEDED\uFF09\uFF0C\u4ECE\u672A\u4EA4\u4ED8\u7ED9\u7528\u6237" : target.reason === "no-answer-in-turn" ? "\u672C\u8F6E\u6CA1\u6709\u5DF2\u4EA4\u4ED8\u7684\u7EC8\u7B54\uFF08\u4E2D\u65AD\u6216\u4ECD\u5728\u8DD1\uFF09" : "\u8BE5\u4F1A\u8BDD\u7684 timeline \u6CA1\u6709\u53EF\u7528\u6761\u76EE";
4818
+ if (target.earlierSeq === void 0) return head;
4819
+ return `${head}\uFF1B\u8DE8\u8F6E\u56DE\u9000\u4F1A\u8FDE\u672C\u8F6E\u63D0\u95EE\u4E00\u8D77\u4F5C\u5E9F\uFF0C\u4E0D\u81EA\u52A8\u505A\u2014\u2014\u786E\u9700\u56DE\u9000\u5230\u4E0A\u4E00\u8F6E\u8BF7\u663E\u5F0F\u4F20 --regenerate-from ${target.earlierSeq}`;
4820
+ }
4772
4821
  async function printConversationEvents(conversationId, args) {
4773
4822
  const ctx = await getActiveAuthContextFromCli(args);
4774
4823
  const client3 = HcmClient11.fromAuthContext(ctx);
@@ -4790,18 +4839,7 @@ async function printConversationEvents(conversationId, args) {
4790
4839
  process.stdout.write("No timeline events.\n");
4791
4840
  return;
4792
4841
  }
4793
- const rows2 = items.map((item) => {
4794
- const value = item;
4795
- return {
4796
- SEQ: value.seq ?? "-",
4797
- KIND: oneLine(value.kind, 18),
4798
- AGENT: eventAgent(value),
4799
- SCOPE: eventScope(value),
4800
- STATUS: eventStatus(value),
4801
- EVENT: eventPreview(value)
4802
- };
4803
- });
4804
- process.stdout.write(formatRows3(rows2, { format: "table" }) + "\n");
4842
+ process.stdout.write(formatRows3(buildEventRows(items), { format: "table" }) + "\n");
4805
4843
  process.stdout.write(`Showing ${items.length} event${items.length === 1 ? "" : "s"}.
4806
4844
  `);
4807
4845
  process.stdout.write(`raw: hcm chat --events ${conversationId} --raw
@@ -4914,14 +4952,14 @@ async function chatCommand(prompt, args) {
4914
4952
  }
4915
4953
  if (regenerateFromLastAssistant) {
4916
4954
  const items = await fetchConversationTimeline2(client3.raw(), resumeConversationId);
4917
- const lastSeq = findLastAssistantSeq(items);
4918
- if (lastSeq == null) {
4955
+ const target = findLastAssistantTarget(items);
4956
+ if (target.seq == null) {
4919
4957
  throw new CliError16({
4920
4958
  code: CliErrorCode15.BUSINESS_RULE_VIOLATION,
4921
- message: `--regenerate-from last-assistant \u627E\u4E0D\u5230\u53EF\u91CD\u8DD1\u7684 chat-text seq: conversation=${resumeConversationId}`
4959
+ message: `--regenerate-from last-assistant \u627E\u4E0D\u5230\u53EF\u91CD\u8DD1\u7684 chat-text seq: conversation=${resumeConversationId}\uFF08${lastAssistantMissDetail(target)}\uFF09`
4922
4960
  });
4923
4961
  }
4924
- regenerateFromSeq = lastSeq;
4962
+ regenerateFromSeq = target.seq;
4925
4963
  }
4926
4964
  const ws = new WsClient2({
4927
4965
  endpoint: wsUrl,
@@ -5474,9 +5512,9 @@ function printLlmTraceTree(nodes) {
5474
5512
  const root = nodes[0];
5475
5513
  const childCount = nodes.length - 1;
5476
5514
  if (!root.rows.length && childCount === 0) {
5477
- process.stdout.write(`No LLM calls for conversation ${root.convId}.
5478
- `);
5479
- process.stdout.write("next: \u786E\u8BA4 convId \u6B63\u786E\uFF0C\u6216\u8BE5\u4F1A\u8BDD\u5C1A\u65E0 LLM \u8C03\u7528\u843D\u5E93\uFF08dev always-on\uFF0CADR-134 D2\uFF09\u3002\n");
5515
+ for (const line of describeLlmTraceEmpty(root.convId, root.envelope)) {
5516
+ process.stdout.write(line + "\n");
5517
+ }
5480
5518
  return;
5481
5519
  }
5482
5520
  for (const node of nodes) {
@@ -5499,14 +5537,80 @@ ${" ".repeat(node.depth)}\u2514\u2500 \u59D4\u6D3E \u2192 ${node.label} \xB7 ${
5499
5537
  if (table.length) {
5500
5538
  process.stdout.write(formatRows4(table, { format: "table" }) + "\n");
5501
5539
  } else {
5502
- process.stdout.write("\uFF08\u65E0 LLM \u8C03\u7528\uFF09\n");
5540
+ const why = envelopeReason(node.envelope);
5541
+ process.stdout.write(`\uFF08\u65E0 LLM \u8C03\u7528${why ? ` \xB7 ${short(why, 60)}` : ""}\uFF09
5542
+ `);
5503
5543
  }
5504
5544
  }
5505
5545
  process.stdout.write("\nnext: hcm trace --conv <convId> --raw\uFF08\u770B\u5B8C\u6574\u9010\u8C03\u7528 JSON\uFF09\n");
5506
5546
  }
5547
+ function normalizeLlmTraceBody(body) {
5548
+ if (Array.isArray(body)) return { rows: body };
5549
+ if (body === null || body === void 0 || body === "") return { rows: [] };
5550
+ if (body && typeof body === "object") {
5551
+ const obj = body;
5552
+ if (Array.isArray(obj.rows)) {
5553
+ return { rows: obj.rows, envelope: obj };
5554
+ }
5555
+ }
5556
+ throw new Error("llm-trace \u54CD\u5E94\u65E2\u4E0D\u662F\u6570\u7EC4\u4E5F\u4E0D\u662F\u542B rows \u7684\u4FE1\u5C01\uFF0C\u65E0\u6CD5\u5224\u5B9A\u884C\u6570");
5557
+ }
5558
+ function asRecord2(value) {
5559
+ return value && typeof value === "object" && !Array.isArray(value) ? value : void 0;
5560
+ }
5561
+ function asText(value) {
5562
+ return typeof value === "string" && value.trim() ? value.trim() : void 0;
5563
+ }
5564
+ function envelopeReason(envelope) {
5565
+ return envelope ? asText(envelope.reason) : void 0;
5566
+ }
5567
+ function unfold(text) {
5568
+ return text.replace(/\s+/g, " ").trim();
5569
+ }
5570
+ var WRITER_LOSS_KEYS = ["failed", "discardedNoTenant", "discardedQueueFull"];
5571
+ function writerCountsLine(envelope) {
5572
+ const writer = asRecord2(asRecord2(envelope.capture)?.writer);
5573
+ if (!writer) return void 0;
5574
+ const parts = Object.entries(writer).filter(([, v]) => typeof v === "number" || typeof v === "string").map(([k, v]) => `${k}=${v}`);
5575
+ if (!parts.length) return void 0;
5576
+ const lossy = WRITER_LOSS_KEYS.some((k) => typeof writer[k] === "number" && writer[k] > 0);
5577
+ if (!lossy && asText(envelope.code) !== "CAPTURE_MISSING_DESPITE_JOURNAL") return void 0;
5578
+ return `writer: ${parts.join(" ")}`;
5579
+ }
5580
+ function describeLlmTraceEmpty(convId, envelope) {
5581
+ const lines = [`No LLM calls for conversation ${convId}.`];
5582
+ if (!envelope) {
5583
+ lines.push("\u540E\u7AEF\u672A\u8FD4\u56DE\u81EA\u9648\uFF0C\u539F\u56E0\u65E0\u6CD5\u5224\u5B9A\u3002");
5584
+ lines.push(
5585
+ "next: \u5148\u6838\u5BF9 convId \u4E0E\u5F53\u524D\u767B\u5F55\u79DF\u6237/\u8EAB\u4EFD\uFF08--env/--as\uFF09\uFF1B\u82E5\u786E\u8BA4\u65E0\u8BEF\uFF0C\u5347\u7EA7\u540E\u7AEF\u81F3\u542B #2539 \u7684\u7248\u672C\u53EF\u62FF\u5230\u670D\u52A1\u7AEF\u81EA\u9648\u3002"
5586
+ );
5587
+ return lines;
5588
+ }
5589
+ const facts = [];
5590
+ const code = asText(envelope.code);
5591
+ const reason = envelopeReason(envelope);
5592
+ const nextStep = asText(envelope.nextStep);
5593
+ const tenantId = asText(asRecord2(envelope.scope)?.tenantId);
5594
+ const visible = asRecord2(envelope.conversation)?.visible;
5595
+ const retention = asText(asRecord2(envelope.capture)?.retention);
5596
+ const writerLine = writerCountsLine(envelope);
5597
+ if (code) lines.push(`code: ${code}`);
5598
+ if (reason) lines.push(`reason: ${unfold(reason)}`);
5599
+ if (tenantId) facts.push(`tenant ${tenantId}`);
5600
+ if (typeof visible === "boolean") facts.push(`conversation: ${visible ? "visible" : "not visible"}`);
5601
+ if (retention) facts.push(`retention ${retention}`);
5602
+ if (facts.length) lines.push(facts.join(" \xB7 "));
5603
+ if (writerLine) lines.push(writerLine);
5604
+ if (nextStep) lines.push(`next: ${unfold(nextStep)}`);
5605
+ if (lines.length === 1) lines.push("\u670D\u52A1\u7AEF\u8FD4\u56DE\u4E86\u4FE1\u5C01\u4F46\u6CA1\u7ED9\u539F\u56E0\uFF0C\u539F\u56E0\u65E0\u6CD5\u5224\u5B9A\u3002");
5606
+ return lines;
5607
+ }
5507
5608
  async function fetchLlmTrace(http, convId) {
5508
- const resp = await http.get(`/api/moirai/conversations/${encodeURIComponent(convId)}/llm-trace`);
5509
- return resp.data ?? [];
5609
+ const resp = await http.get(
5610
+ `/api/moirai/conversations/${encodeURIComponent(convId)}/llm-trace`,
5611
+ { params: { envelope: "true" } }
5612
+ );
5613
+ return normalizeLlmTraceBody(resp.data);
5510
5614
  }
5511
5615
  function extractChildConvs(items) {
5512
5616
  const out = [];
@@ -5546,7 +5650,8 @@ async function gatherLlmTraceTree(http, rootConvId, maxDepth = 3) {
5546
5650
  async function walk(convId, depth, label) {
5547
5651
  if (visited.has(convId) || depth > maxDepth) return;
5548
5652
  visited.add(convId);
5549
- nodes.push({ convId, depth, label, rows: await fetchLlmTrace(http, convId) });
5653
+ const body = await fetchLlmTrace(http, convId);
5654
+ nodes.push({ convId, depth, label, rows: body.rows, envelope: body.envelope });
5550
5655
  if (depth === maxDepth) return;
5551
5656
  for (const c of await fetchChildConvs(http, convId)) {
5552
5657
  await walk(c.childConvId, depth + 1, c.label);
@@ -5562,9 +5667,14 @@ async function traceCommand(traceId, args) {
5562
5667
  const convId = args.conv ?? (args.llmCalls ? traceId ?? void 0 : void 0);
5563
5668
  if (convId) {
5564
5669
  if (args.raw || args.output === "json" || args.output === "yaml") {
5565
- const rows2 = await fetchLlmTrace(http, convId);
5670
+ const body = await fetchLlmTrace(http, convId);
5566
5671
  const fmt = args.raw ? "json" : args.output;
5567
- process.stdout.write(formatObject10(rows2, { format: fmt }) + "\n");
5672
+ process.stdout.write(formatObject10(body.rows, { format: fmt }) + "\n");
5673
+ if (!body.rows.length) {
5674
+ for (const line of describeLlmTraceEmpty(convId, body.envelope)) {
5675
+ process.stderr.write(line + "\n");
5676
+ }
5677
+ }
5568
5678
  process.exitCode = 0;
5569
5679
  return;
5570
5680
  }
@@ -9084,7 +9194,7 @@ async function workspaceCreateCommand(workspaceKey, args) {
9084
9194
  workspaceKey: normalizedWorkspaceKey,
9085
9195
  description: args.description ?? ""
9086
9196
  });
9087
- const created = asRecord(unwrapData(resp.data));
9197
+ const created = asRecord3(unwrapData(resp.data));
9088
9198
  const id = stringOptional(created.id);
9089
9199
  let activated = false;
9090
9200
  if (args.activate) {
@@ -10085,7 +10195,7 @@ function stringRequired(value, field) {
10085
10195
  function stringOptional(value) {
10086
10196
  return typeof value === "string" && value.trim() ? value : void 0;
10087
10197
  }
10088
- function asRecord(value) {
10198
+ function asRecord3(value) {
10089
10199
  return value && typeof value === "object" ? value : {};
10090
10200
  }
10091
10201
  function unwrapData(value) {