All files / src/cli format-event.ts

79.78% Statements 75/94
64.13% Branches 93/145
100% Functions 3/3
84.52% Lines 71/84

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      1x               1x                 2x 2x 2x 2x                     2x 2x 2x         13x 13x   12x 12x 12x   2x 2x     10x   13x 1x 1x 1x 1x     9x   3x     3x 3x 3x 3x 3x 3x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x       3x     6x   2x     2x 2x 2x 2x 2x 2x     2x     4x   2x     2x 2x 2x 2x 2x 2x 2x 2x 1x 1x   2x 2x 1x 1x 1x     2x         2x     2x 1x      
import pc from "picocolors";
 
function asErrorText(value: unknown): string {
  Eif (typeof value === "string") return value;
  if (typeof value !== "object" || value === null || Array.isArray(value)) return "";
  const obj = value as Record<string, unknown>;
  const message =
    (typeof obj.message === "string" && obj.message) ||
    (typeof obj.error === "string" && obj.error) ||
    (typeof obj.code === "string" && obj.code) ||
    "";
  Iif (message) return message;
  try {
    return JSON.stringify(obj);
  } catch {
    return "";
  }
}
 
function printToolResult(block: Record<string, unknown>): void {
  const isError = block.is_error === true;
  let text = "";
  if (typeof block.content === "string") {
    text = block.content;
  } else if (EArray.isArray(block.content)) {
    const parts: string[] = [];
    for (const part of block.content) {
      if (typeof part !== "object" || part === null || Array.isArray(part)) continue;
      const record = part as Record<string, unknown>;
      if (typeof record.text === "string") parts.push(record.text);
    }
    text = parts.join("\n");
  }
 
  console.log((isError ? pc.red : pc.cyan)(`tool_result${isError ? " (error)" : ""}`));
  Eif (text) {
    console.log((isError ? pc.red : pc.gray)(text));
  }
}
 
export function printClaudeStreamEvent(raw: string, debug: boolean): void {
  const line = raw.trim();
  if (!line) return;
 
  let parsed: Record<string, unknown> | null = null;
  try {
    parsed = JSON.parse(line) as Record<string, unknown>;
  } catch {
    console.log(line);
    return;
  }
 
  const type = typeof parsed.type === "string" ? parsed.type : "";
 
  if (type === "system" && parsed.subtype === "init") {
    const model = typeof parsed.model === "string" ? parsed.model : "unknown";
    const sessionId = typeof parsed.session_id === "string" ? parsed.session_id : "";
    console.log(pc.blue(`Claude initialized (model: ${model}${sessionId ? `, session: ${sessionId}` : ""})`));
    return;
  }
 
  if (type === "assistant") {
    const message =
      typeof parsed.message === "object" && parsed.message !== null && !Array.isArray(parsed.message)
        ? (parsed.message as Record<string, unknown>)
        : {};
    const content = Array.isArray(message.content) ? message.content : [];
    for (const blockRaw of content) {
      Iif (typeof blockRaw !== "object" || blockRaw === null || Array.isArray(blockRaw)) continue;
      const block = blockRaw as Record<string, unknown>;
      const blockType = typeof block.type === "string" ? block.type : "";
      if (blockType === "text") {
        const text = typeof block.text === "string" ? block.text : "";
        Eif (text) console.log(pc.green(`assistant: ${text}`));
      } else if (blockType === "thinking") {
        const text = typeof block.thinking === "string" ? block.thinking : "";
        Eif (text) console.log(pc.gray(`thinking: ${text}`));
      } else if (EblockType === "tool_use") {
        const name = typeof block.name === "string" ? block.name : "unknown";
        console.log(pc.yellow(`tool_call: ${name}`));
        Eif (block.input !== undefined) {
          console.log(pc.gray(JSON.stringify(block.input, null, 2)));
        }
      }
    }
    return;
  }
 
  if (type === "user") {
    const message =
      typeof parsed.message === "object" && parsed.message !== null && !Array.isArray(parsed.message)
        ? (parsed.message as Record<string, unknown>)
        : {};
    const content = Array.isArray(message.content) ? message.content : [];
    for (const blockRaw of content) {
      Iif (typeof blockRaw !== "object" || blockRaw === null || Array.isArray(blockRaw)) continue;
      const block = blockRaw as Record<string, unknown>;
      Eif (typeof block.type === "string" && block.type === "tool_result") {
        printToolResult(block);
      }
    }
    return;
  }
 
  if (type === "result") {
    const usage =
      typeof parsed.usage === "object" && parsed.usage !== null && !Array.isArray(parsed.usage)
        ? (parsed.usage as Record<string, unknown>)
        : {};
    const input = Number(usage.input_tokens ?? 0);
    const output = Number(usage.output_tokens ?? 0);
    const cached = Number(usage.cache_read_input_tokens ?? 0);
    const cost = Number(parsed.total_cost_usd ?? 0);
    const subtype = typeof parsed.subtype === "string" ? parsed.subtype : "";
    const isError = parsed.is_error === true;
    const resultText = typeof parsed.result === "string" ? parsed.result : "";
    if (resultText) {
      console.log(pc.green("result:"));
      console.log(resultText);
    }
    const errors = Array.isArray(parsed.errors) ? parsed.errors.map(asErrorText).filter(Boolean) : [];
    if (subtype.startsWith("error") || isError || errors.length > 0) {
      console.log(pc.red(`claude_result: subtype=${subtype || "unknown"} is_error=${isError ? "true" : "false"}`));
      Eif (errors.length > 0) {
        console.log(pc.red(`claude_errors: ${errors.join(" | ")}`));
      }
    }
    console.log(
      pc.blue(
        `tokens: in=${Number.isFinite(input) ? input : 0} out=${Number.isFinite(output) ? output : 0} cached=${Number.isFinite(cached) ? cached : 0} cost=$${Number.isFinite(cost) ? cost.toFixed(6) : "0.000000"}`,
      ),
    );
    return;
  }
 
  if (debug) {
    console.log(pc.gray(line));
  }
}