opencode-usage-insights 0.4.0 → 0.4.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
@@ -1804,7 +1804,9 @@ function otherUsageRows(db, range, filters) {
1804
1804
  }
1805
1805
  var STEP_TIME = "COALESCE(created_at, recorded_at)";
1806
1806
  var MODEL_KEY_SEPARATOR = "#";
1807
- var MODEL_KEY_SQL = `COALESCE(model_id, '(unknown)') || '${MODEL_KEY_SEPARATOR}' || COALESCE(variant, '')`;
1807
+ function modelKeySQL(prefix = "") {
1808
+ return `COALESCE(${prefix}model_id, '(unknown)') || '${MODEL_KEY_SEPARATOR}' || COALESCE(${prefix}variant, '')`;
1809
+ }
1808
1810
  function modelKey(modelID, variant) {
1809
1811
  return `${modelID}${MODEL_KEY_SEPARATOR}${variant ?? ""}`;
1810
1812
  }
@@ -1844,24 +1846,24 @@ function rangeWhere(range, column = STEP_TIME) {
1844
1846
  }
1845
1847
  return { where: clauses.length > 0 ? `WHERE ${clauses.join(" AND ")}` : "", params };
1846
1848
  }
1847
- function buildWhere(range, filters, column = STEP_TIME) {
1849
+ function buildWhere(range, filters, column = STEP_TIME, prefix = "") {
1848
1850
  const rangeClause = rangeWhere(range, column);
1849
1851
  const clauses = rangeClause.where === "" ? [] : [rangeClause.where.slice("WHERE ".length)];
1850
1852
  const params = [...rangeClause.params];
1851
1853
  if (filters?.modelID !== undefined && filters.modelID !== "") {
1852
- clauses.push("model_id = ?");
1854
+ clauses.push(`${prefix}model_id = ?`);
1853
1855
  params.push(filters.modelID);
1854
1856
  }
1855
1857
  if (filters?.modelKey !== undefined && filters.modelKey !== "") {
1856
- clauses.push(`${MODEL_KEY_SQL} = ?`);
1858
+ clauses.push(`${modelKeySQL(prefix)} = ?`);
1857
1859
  params.push(filters.modelKey);
1858
1860
  }
1859
1861
  if (filters?.provider !== undefined && filters.provider !== "") {
1860
- clauses.push("provider_id = ?");
1862
+ clauses.push(`${prefix}provider_id = ?`);
1861
1863
  params.push(filters.provider);
1862
1864
  }
1863
1865
  if (filters?.sessionID !== undefined && filters.sessionID !== "") {
1864
- clauses.push("session_id = ?");
1866
+ clauses.push(`${prefix}session_id = ?`);
1865
1867
  params.push(filters.sessionID);
1866
1868
  }
1867
1869
  return { where: clauses.length > 0 ? `WHERE ${clauses.join(" AND ")}` : "", params };
@@ -1977,7 +1979,7 @@ function sessions(db, options) {
1977
1979
  if (options?.sessionIDs !== undefined && options.sessionIDs.length === 0)
1978
1980
  return [];
1979
1981
  const range = options?.range;
1980
- const filter = buildWhere(range, options?.filters, "COALESCE(st.created_at, st.recorded_at)");
1982
+ const filter = buildWhere(range, options?.filters, "COALESCE(st.created_at, st.recorded_at)", "st.");
1981
1983
  const limit = options?.limit ?? 20;
1982
1984
  const clauses = filter.where === "" ? [] : [filter.where.slice("WHERE ".length)];
1983
1985
  const params = [...filter.params];
@@ -2041,8 +2043,8 @@ function sessions(db, options) {
2041
2043
  function childSessionIDs(db, parentID) {
2042
2044
  return db.query("SELECT session_id FROM sessions WHERE parent_id = ? ORDER BY session_id").all(parentID).map((row) => row.session_id);
2043
2045
  }
2044
- function sessionUsageOf(db, sessionIDs) {
2045
- return sessions(db, { sessionIDs, limit: sessionIDs.length });
2046
+ function sessionUsageOf(db, sessionIDs, filters) {
2047
+ return sessions(db, { sessionIDs, limit: sessionIDs.length, filters });
2046
2048
  }
2047
2049
  function turnFinished(finish) {
2048
2050
  return finish !== null && finish !== "" && finish !== "tool-calls";
@@ -3196,15 +3198,16 @@ function viewBar(view, width = 200, views = CLIENT_VIEWS) {
3196
3198
  const index = views.indexOf(view);
3197
3199
  return { text: `[${index === -1 ? "?" : key(index)} ${view}] \xB7 1-9 0 or [ ] to switch`, tone: "muted" };
3198
3200
  }
3199
- function keysFor(of) {
3201
+ function keysFor(of, range) {
3200
3202
  if (of === QUOTA_VIEW)
3201
3203
  return "r refresh \xB7 t range \xB7 0 view \xB7 ? help \xB7 the quotas come from OpenCode Go";
3202
3204
  if (of === "db")
3203
3205
  return "\u2191\u2193 pick a retention \xB7 enter prune \xB7 the last row wipes \xB7 e backfill \xB7 v vacuum \xB7 r refresh \xB7 1-9 0 view \xB7 ? help";
3204
3206
  if (of === "periods")
3205
3207
  return "\u2191\u2193 move \xB7 enter drill \xB7 b bucket \xB7 t range \xB7 m/p filter \xB7 x clear \xB7 1-9 0 view \xB7 ? help";
3206
- if (of === "turns")
3207
- return "\u2191\u2193 move \xB7 a scope \xB7 t range \xB7 m/p filter \xB7 x clear \xB7 1-9 0 view \xB7 ? help";
3208
+ if (of === "turns") {
3209
+ return range === "session" ? "\u2191\u2193 move \xB7 t range \xB7 m/p filter \xB7 x clear \xB7 1-9 0 view \xB7 ? help" : "\u2191\u2193 move \xB7 a scope \xB7 t range \xB7 m/p filter \xB7 x clear \xB7 1-9 0 view \xB7 ? help";
3210
+ }
3208
3211
  return "\u2191\u2193 move \xB7 enter drill \xB7 s sort \xB7 t range \xB7 m/p filter \xB7 x clear \xB7 1-9 0 view \xB7 ? help";
3209
3212
  }
3210
3213
  function filterSummary(filters) {
@@ -3226,7 +3229,7 @@ function helpLines(width) {
3226
3229
  { text: " t shift+t previous / next range" },
3227
3230
  { text: " s shift+s cycle sort column \xB7 reverse" },
3228
3231
  { text: " b periods bucket: day \u2192 week \u2192 month" },
3229
- { text: " a turns scope: this session \u2194 all sessions" },
3232
+ { text: " a turns scope: this session \u2194 all sessions (time ranges)" },
3230
3233
  { text: " m p filter by model / provider (x clears)" },
3231
3234
  { text: " r re-read the database \xB7 f full screen \xB7 escape close" },
3232
3235
  { text: " e db: backfill list-price estimates for the $0 steps" },
@@ -4227,7 +4230,8 @@ function build(request) {
4227
4230
  const usage = overview(db, range, filters);
4228
4231
  const sessionUsage = request.sessionID === "" ? undefined : overview(db, undefined, { ...request.filters, sessionID: request.sessionID });
4229
4232
  const childIDs = request.sessionID === "" ? [] : childSessionIDs(db, request.sessionID);
4230
- const children = childIDs.length === 0 ? [] : sessionUsageOf(db, childIDs);
4233
+ const childFilters = { ...request.filters, sessionID: undefined };
4234
+ const children = childIDs.length === 0 ? [] : sessionUsageOf(db, childIDs, childFilters);
4231
4235
  return overviewLines({
4232
4236
  usage,
4233
4237
  session: sessionUsage,
@@ -4291,9 +4295,10 @@ function build(request) {
4291
4295
  }
4292
4296
  ];
4293
4297
  case "turns": {
4294
- const sessionID = request.turnsScope === "session" ? request.sessionID : undefined;
4298
+ const scope = scoped ? "session" : request.turnsScope;
4299
+ const sessionID = scope === "session" ? request.sessionID : undefined;
4295
4300
  const rows = sortTurns(turns(db, { sessionID, range, filters, limit: 300 }), request.sort, request.sortDesc, estimates);
4296
- const titles = request.turnsScope === "all" ? new Map(sessions(db, { range, filters, limit: 200 }).map((row) => [row.sessionID, row.title ?? row.sessionID])) : undefined;
4301
+ const titles = scope === "all" ? new Map(sessions(db, { range, filters, limit: 200 }).map((row) => [row.sessionID, row.title ?? row.sessionID])) : undefined;
4297
4302
  return turnsLines(rows.slice(0, 60), width, { ...request.format, estimates, sessionTitles: titles });
4298
4303
  }
4299
4304
  }
@@ -5284,4 +5289,4 @@ export {
5284
5289
  src_default as default
5285
5290
  };
5286
5291
 
5287
- //# debugId=416DE53B4E3B355564756E2164756E21
5292
+ //# debugId=E4A1A33EDF4C638464756E2164756E21