@swestash/agent-lens 0.1.0 → 0.2.0

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.
@@ -1875,9 +1875,10 @@ var ClaudeCodeAdapter = class {
1875
1875
  result_summary: null
1876
1876
  });
1877
1877
  } else if (b.type === "tool_result" && asString(b.tool_use_id)) {
1878
+ const isAskUser = tur && tur.questions && tur.answers && typeof tur.answers === "object";
1878
1879
  toolResults.push({
1879
1880
  tool_use_id: b.tool_use_id,
1880
- result_summary: summarizeResult(b.content),
1881
+ result_summary: isAskUser ? JSON.stringify({ answers: tur.answers, annotations: tur.annotations ?? {} }) : summarizeResult(b.content),
1881
1882
  ...tur ? {
1882
1883
  status: asString(tur.status),
1883
1884
  agent_type: asString(tur.agentType),
@@ -2858,21 +2859,47 @@ function listSessions(db, f) {
2858
2859
  }
2859
2860
  const whereSql = where.length ? `WHERE ${where.join(" AND ")}` : "";
2860
2861
  const total = db.prepare(`SELECT COUNT(*) n FROM sessions s ${whereSql}`).get(...params).n;
2861
- const rows = db.prepare(`SELECT s.id, s.ai_title, s.slug, s.source_id, s.is_sidechain, s.started_at, s.ended_at,
2862
+ const baseSelect = `SELECT s.id, s.ai_title, s.slug, s.source_id, s.is_sidechain, s.started_at, s.ended_at,
2862
2863
  s.duration_ms, s.event_count, s.turn_count, p.path AS project_path,
2863
2864
  (SELECT GROUP_CONCAT(DISTINCT model) FROM token_usage t WHERE t.session_id = s.id) AS models
2864
2865
  FROM sessions s LEFT JOIN projects p ON p.id = s.project_id
2865
- ${whereSql}
2866
- ORDER BY s.started_at DESC
2867
- LIMIT ? OFFSET ?`).all(...params, f.limit, f.offset);
2868
- const ids = rows.map((r) => r.id);
2866
+ ${whereSql}`;
2867
+ const NATIVE_ORDER = {
2868
+ started: "s.started_at",
2869
+ title: "COALESCE(s.ai_title, s.slug)",
2870
+ turns: "s.turn_count",
2871
+ duration: "s.duration_ms"
2872
+ };
2873
+ const sortKey = f.sort ?? "started";
2874
+ const sqlDir = f.dir === "asc" ? "ASC" : "DESC";
2875
+ let rows;
2876
+ if (sortKey === "tokens" || sortKey === "cost") {
2877
+ const all = db.prepare(baseSelect).all(...params);
2878
+ attachSessionCost(db, all);
2879
+ const sign = f.dir === "asc" ? 1 : -1;
2880
+ all.sort((a, b) => {
2881
+ const c = (sortKey === "tokens" ? a.tokens - b.tokens : a.cost - b.cost) * sign;
2882
+ return c !== 0 ? c : String(a.id).localeCompare(String(b.id));
2883
+ });
2884
+ rows = all.slice(f.offset, f.offset + f.limit);
2885
+ } else {
2886
+ const orderCol = NATIVE_ORDER[sortKey] ?? NATIVE_ORDER.started;
2887
+ rows = db.prepare(`${baseSelect} ORDER BY ${orderCol} ${sqlDir}, s.id ASC LIMIT ? OFFSET ?`).all(...params, f.limit, f.offset);
2888
+ attachSessionCost(db, rows);
2889
+ }
2890
+ return { total, sessions: rows };
2891
+ }
2892
+ function attachSessionCost(db, rows) {
2869
2893
  const costBySession = /* @__PURE__ */ new Map();
2870
- if (ids.length) {
2871
- const ph = ids.map(() => "?").join(",");
2894
+ const ids = rows.map((r) => r.id);
2895
+ const CHUNK3 = 800;
2896
+ for (let i = 0; i < ids.length; i += CHUNK3) {
2897
+ const batch = ids.slice(i, i + CHUNK3);
2898
+ const ph = batch.map(() => "?").join(",");
2872
2899
  const usage = db.prepare(`SELECT session_id, model,
2873
2900
  SUM(input_tokens) i, SUM(output_tokens) o,
2874
2901
  SUM(cache_creation_input_tokens) cw, SUM(cache_read_input_tokens) cr
2875
- FROM token_usage WHERE session_id IN (${ph}) GROUP BY session_id, model`).all(...ids);
2902
+ FROM token_usage WHERE session_id IN (${ph}) GROUP BY session_id, model`).all(...batch);
2876
2903
  for (const u of usage) {
2877
2904
  const acc = costBySession.get(u.session_id) ?? { tokens: 0, cost: 0, split: { input: 0, output: 0, cache_creation: 0, cache_read: 0 } };
2878
2905
  acc.tokens += u.i + u.o + u.cw + u.cr;
@@ -2896,7 +2923,6 @@ function listSessions(db, f) {
2896
2923
  r.cost = c ? Number(c.cost.toFixed(4)) : 0;
2897
2924
  r.title = r.ai_title || r.slug || null;
2898
2925
  }
2899
- return { total, sessions: rows };
2900
2926
  }
2901
2927
  function getSession(db, id) {
2902
2928
  const session = db.prepare(`SELECT s.*, p.path AS project_path FROM sessions s
@@ -3058,8 +3084,11 @@ function getWorkflow(db, runId) {
3058
3084
  function listSkills(db, f = {}) {
3059
3085
  const where = ["tc.tool_name = 'Skill'", "tc.skill_name IS NOT NULL"];
3060
3086
  const params = [];
3061
- if (f.q && f.q.trim())
3062
- where.push("tc.skill_name LIKE ?"), params.push(`%${f.q.trim()}%`);
3087
+ if (f.q && f.q.trim()) {
3088
+ const like = `%${f.q.trim()}%`;
3089
+ where.push("(tc.skill_name LIKE ? OR EXISTS (SELECT 1 FROM skills sk WHERE sk.name = tc.skill_name AND sk.body LIKE ?))");
3090
+ params.push(like, like);
3091
+ }
3063
3092
  if (f.source)
3064
3093
  where.push("s.source_id = ?"), params.push(f.source);
3065
3094
  if (f.project)
@@ -3346,6 +3375,8 @@ async function createApp(db, opts = {}) {
3346
3375
  from: q.from,
3347
3376
  to: q.to,
3348
3377
  kind: q.kind === "main" || q.kind === "subagent" ? q.kind : void 0,
3378
+ sort: ["started", "title", "turns", "tokens", "cost", "duration"].find((s) => s === q.sort),
3379
+ dir: q.dir === "asc" ? "asc" : q.dir === "desc" ? "desc" : void 0,
3349
3380
  limit: Math.min(Number(q.limit) || 50, 200),
3350
3381
  offset: Number(q.offset) || 0
3351
3382
  });