Skip to content

Instantly share code, notes, and snippets.

@laiso
Created January 10, 2026 03:37
Show Gist options
  • Select an option

  • Save laiso/fea0f9a9a3fb0a49eded8a167f412f64 to your computer and use it in GitHub Desktop.

Select an option

Save laiso/fea0f9a9a3fb0a49eded8a167f412f64 to your computer and use it in GitHub Desktop.
claude code logs , https://github.com/blacksmithgu/obsidian-dataview , copy from .claude/projects/**.jsonl

const PATH = "logs/sample.jsonl";
const N = 400;

const raw = await dv.io.load(PATH);
if (typeof raw !== "string") {
  dv.paragraph(`load failed: ${PATH}`);
  return;
}

const esc = (s) =>
  String(s)
    .replace(/&/g, "&")
    .replace(/</g, "&lt;")
    .replace(/>/g, "&gt;");

const rows = raw
  .split("\n")
  .filter(Boolean)
  .slice(-N)
  .map(l => { try { return JSON.parse(l); } catch { return null; } })
  .filter(Boolean);

function textOf(r) {
  const c = r.message?.content;
  if (!Array.isArray(c)) return "";

  const texts = c
    .filter(b => b.type === "text")
    .map(b => b.text ?? "")
    .join("\n")
    .trim();
  if (texts) return texts;

  const tr = c.find(b => b?.type === "tool_result");
  if (tr) {
    if (typeof tr.content === "string") return tr.content;
    if (Array.isArray(tr.content)) {
      return tr.content
        .map(x => (typeof x === "string" ? x : x?.text ?? ""))
        .filter(Boolean)
        .join("\n");
    }
    try { return JSON.stringify(tr.content, null, 2); } catch { return ""; }
  }

  const tool = c.find(b => b.type === "tool_use");
  if (tool) {
    return `tool_use: ${tool.name}`;
  }

  return "";
}

const cell = (t) => {
  if (!t) return "";
  const preview = t.replace(/\n/g, " ").slice(0, 80);
  return `<details>
    <summary>${esc(preview)}${t.length > 80 ? "…" : ""}</summary>
    <pre>${esc(t)}</pre>
  </details>`;
};

dv.table(["time","type","role","text"],
  rows
    .filter(r => r.type === "user" || r.type === "assistant")
    .map(r => {
      const t = textOf(r);
      if (!t || !t.trim()) return null;
      return [r.timestamp ?? "", r.type, r.message?.role ?? r.type, cell(t)];
    })
    .filter(Boolean)
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment