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.
@@ -182,7 +182,7 @@ export declare function usageBuckets(usage: {
182
182
  * active view always keeps its full name.
183
183
  */
184
184
  export declare function viewBar(view: PanelView, width?: number, views?: readonly PanelView[]): PanelLine;
185
- export declare function keysFor(of: PanelView): string;
185
+ export declare function keysFor(of: PanelView, range?: RangeKey): string;
186
186
  export declare function filterSummary(filters: {
187
187
  modelID?: string | undefined;
188
188
  providerID?: string | undefined;
@@ -1,13 +1,13 @@
1
1
  export declare const UsageInsightsSettings: {
2
2
  readonly id: "usage-insights.settings";
3
3
  readonly methods: {
4
- readonly get: {
5
- readonly input: {
4
+ get: {
5
+ input: {
6
6
  readonly type: "object";
7
7
  readonly properties: {};
8
8
  readonly additionalProperties: false;
9
9
  };
10
- readonly output: {
10
+ output: {
11
11
  readonly type: "object";
12
12
  readonly properties: {
13
13
  readonly settings: {
@@ -155,8 +155,8 @@ export declare const UsageInsightsSettings: {
155
155
  readonly additionalProperties: false;
156
156
  };
157
157
  };
158
- readonly set: {
159
- readonly input: {
158
+ set: {
159
+ input: {
160
160
  readonly type: "object";
161
161
  readonly properties: {
162
162
  readonly patch: {
@@ -289,7 +289,7 @@ export declare const UsageInsightsSettings: {
289
289
  readonly required: readonly ["patch"];
290
290
  readonly additionalProperties: false;
291
291
  };
292
- readonly output: {
292
+ output: {
293
293
  readonly type: "object";
294
294
  readonly properties: {
295
295
  readonly settings: {
@@ -437,13 +437,13 @@ export declare const UsageInsightsSettings: {
437
437
  readonly additionalProperties: false;
438
438
  };
439
439
  };
440
- readonly reset: {
441
- readonly input: {
440
+ reset: {
441
+ input: {
442
442
  readonly type: "object";
443
443
  readonly properties: {};
444
444
  readonly additionalProperties: false;
445
445
  };
446
- readonly output: {
446
+ output: {
447
447
  readonly type: "object";
448
448
  readonly properties: {
449
449
  readonly settings: {
@@ -593,8 +593,8 @@ export declare const UsageInsightsSettings: {
593
593
  };
594
594
  };
595
595
  readonly events: {
596
- readonly updated: {
597
- readonly schema: {
596
+ updated: {
597
+ schema: {
598
598
  readonly type: "object";
599
599
  readonly properties: {
600
600
  readonly settings: {
@@ -99,16 +99,17 @@ export declare const STEP_TIME = "COALESCE(created_at, recorded_at)";
99
99
  * its family. The JS twin is `modelKey` below; both have to agree.
100
100
  */
101
101
  export declare const MODEL_KEY_SEPARATOR = "#";
102
- export declare const MODEL_KEY_SQL = "COALESCE(model_id, '(unknown)') || '#' || COALESCE(variant, '')";
103
102
  /** `modelKey(modelID, variant)`: the family, with the variant as a suffix. */
104
103
  export declare function modelKey(modelID: string, variant: string | undefined): string;
105
104
  /** Human label of a model key or a model id and variant. */
106
105
  export declare function modelLabel(modelID: string, variant: string | undefined): string;
107
106
  /**
108
107
  * `WHERE` for a range plus the panel filters. Shared with the analysis layer so
109
- * a filtered view and a drill-down agree on what is in scope.
108
+ * a filtered view and a drill-down agree on what is in scope. `prefix` qualifies
109
+ * the filter columns; a query that joins another table must pass its `steps`
110
+ * alias (`"st."`), or a column the two tables share is ambiguous.
110
111
  */
111
- export declare function buildWhere(range: Range | undefined, filters?: Filters | undefined, column?: string): {
112
+ export declare function buildWhere(range: Range | undefined, filters?: Filters | undefined, column?: string, prefix?: string): {
112
113
  where: string;
113
114
  params: (string | number)[];
114
115
  };
@@ -208,7 +209,7 @@ export declare function sessions(db: Database, options?: {
208
209
  /** Sessions whose parent is `parentID`: the subagents of one session. */
209
210
  export declare function childSessionIDs(db: Database, parentID: string): string[];
210
211
  /** Session usage rows for a specific set of sessions (subagent rollups). */
211
- export declare function sessionUsageOf(db: Database, sessionIDs: readonly string[]): SessionUsage[];
212
+ export declare function sessionUsageOf(db: Database, sessionIDs: readonly string[], filters?: Filters): SessionUsage[];
212
213
  export type TurnDrop = {
213
214
  messageID: string;
214
215
  tokens: number;
@@ -956,7 +956,9 @@ function otherUsageRows(db, range, filters) {
956
956
  }
957
957
  var STEP_TIME = "COALESCE(created_at, recorded_at)";
958
958
  var MODEL_KEY_SEPARATOR = "#";
959
- var MODEL_KEY_SQL = `COALESCE(model_id, '(unknown)') || '${MODEL_KEY_SEPARATOR}' || COALESCE(variant, '')`;
959
+ function modelKeySQL(prefix = "") {
960
+ return `COALESCE(${prefix}model_id, '(unknown)') || '${MODEL_KEY_SEPARATOR}' || COALESCE(${prefix}variant, '')`;
961
+ }
960
962
  function modelKey(modelID, variant) {
961
963
  return `${modelID}${MODEL_KEY_SEPARATOR}${variant ?? ""}`;
962
964
  }
@@ -996,24 +998,24 @@ function rangeWhere(range, column = STEP_TIME) {
996
998
  }
997
999
  return { where: clauses.length > 0 ? `WHERE ${clauses.join(" AND ")}` : "", params };
998
1000
  }
999
- function buildWhere(range, filters, column = STEP_TIME) {
1001
+ function buildWhere(range, filters, column = STEP_TIME, prefix = "") {
1000
1002
  const rangeClause = rangeWhere(range, column);
1001
1003
  const clauses = rangeClause.where === "" ? [] : [rangeClause.where.slice("WHERE ".length)];
1002
1004
  const params = [...rangeClause.params];
1003
1005
  if (filters?.modelID !== undefined && filters.modelID !== "") {
1004
- clauses.push("model_id = ?");
1006
+ clauses.push(`${prefix}model_id = ?`);
1005
1007
  params.push(filters.modelID);
1006
1008
  }
1007
1009
  if (filters?.modelKey !== undefined && filters.modelKey !== "") {
1008
- clauses.push(`${MODEL_KEY_SQL} = ?`);
1010
+ clauses.push(`${modelKeySQL(prefix)} = ?`);
1009
1011
  params.push(filters.modelKey);
1010
1012
  }
1011
1013
  if (filters?.provider !== undefined && filters.provider !== "") {
1012
- clauses.push("provider_id = ?");
1014
+ clauses.push(`${prefix}provider_id = ?`);
1013
1015
  params.push(filters.provider);
1014
1016
  }
1015
1017
  if (filters?.sessionID !== undefined && filters.sessionID !== "") {
1016
- clauses.push("session_id = ?");
1018
+ clauses.push(`${prefix}session_id = ?`);
1017
1019
  params.push(filters.sessionID);
1018
1020
  }
1019
1021
  return { where: clauses.length > 0 ? `WHERE ${clauses.join(" AND ")}` : "", params };
@@ -1129,7 +1131,7 @@ function sessions(db, options) {
1129
1131
  if (options?.sessionIDs !== undefined && options.sessionIDs.length === 0)
1130
1132
  return [];
1131
1133
  const range = options?.range;
1132
- const filter = buildWhere(range, options?.filters, "COALESCE(st.created_at, st.recorded_at)");
1134
+ const filter = buildWhere(range, options?.filters, "COALESCE(st.created_at, st.recorded_at)", "st.");
1133
1135
  const limit = options?.limit ?? 20;
1134
1136
  const clauses = filter.where === "" ? [] : [filter.where.slice("WHERE ".length)];
1135
1137
  const params = [...filter.params];
@@ -1193,8 +1195,8 @@ function sessions(db, options) {
1193
1195
  function childSessionIDs(db, parentID) {
1194
1196
  return db.query("SELECT session_id FROM sessions WHERE parent_id = ? ORDER BY session_id").all(parentID).map((row) => row.session_id);
1195
1197
  }
1196
- function sessionUsageOf(db, sessionIDs) {
1197
- return sessions(db, { sessionIDs, limit: sessionIDs.length });
1198
+ function sessionUsageOf(db, sessionIDs, filters) {
1199
+ return sessions(db, { sessionIDs, limit: sessionIDs.length, filters });
1198
1200
  }
1199
1201
  function turnFinished(finish) {
1200
1202
  return finish !== null && finish !== "" && finish !== "tool-calls";
@@ -2348,15 +2350,16 @@ function viewBar(view, width = 200, views = CLIENT_VIEWS) {
2348
2350
  const index = views.indexOf(view);
2349
2351
  return { text: `[${index === -1 ? "?" : key(index)} ${view}] \xB7 1-9 0 or [ ] to switch`, tone: "muted" };
2350
2352
  }
2351
- function keysFor(of) {
2353
+ function keysFor(of, range) {
2352
2354
  if (of === QUOTA_VIEW)
2353
2355
  return "r refresh \xB7 t range \xB7 0 view \xB7 ? help \xB7 the quotas come from OpenCode Go";
2354
2356
  if (of === "db")
2355
2357
  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";
2356
2358
  if (of === "periods")
2357
2359
  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";
2358
- if (of === "turns")
2359
- return "\u2191\u2193 move \xB7 a scope \xB7 t range \xB7 m/p filter \xB7 x clear \xB7 1-9 0 view \xB7 ? help";
2360
+ if (of === "turns") {
2361
+ 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";
2362
+ }
2360
2363
  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";
2361
2364
  }
2362
2365
  function filterSummary(filters) {
@@ -2378,7 +2381,7 @@ function helpLines(width) {
2378
2381
  { text: " t shift+t previous / next range" },
2379
2382
  { text: " s shift+s cycle sort column \xB7 reverse" },
2380
2383
  { text: " b periods bucket: day \u2192 week \u2192 month" },
2381
- { text: " a turns scope: this session \u2194 all sessions" },
2384
+ { text: " a turns scope: this session \u2194 all sessions (time ranges)" },
2382
2385
  { text: " m p filter by model / provider (x clears)" },
2383
2386
  { text: " r re-read the database \xB7 f full screen \xB7 escape close" },
2384
2387
  { text: " e db: backfill list-price estimates for the $0 steps" },
@@ -3379,7 +3382,8 @@ function build(request) {
3379
3382
  const usage = overview(db, range, filters);
3380
3383
  const sessionUsage = request.sessionID === "" ? undefined : overview(db, undefined, { ...request.filters, sessionID: request.sessionID });
3381
3384
  const childIDs = request.sessionID === "" ? [] : childSessionIDs(db, request.sessionID);
3382
- const children = childIDs.length === 0 ? [] : sessionUsageOf(db, childIDs);
3385
+ const childFilters = { ...request.filters, sessionID: undefined };
3386
+ const children = childIDs.length === 0 ? [] : sessionUsageOf(db, childIDs, childFilters);
3383
3387
  return overviewLines({
3384
3388
  usage,
3385
3389
  session: sessionUsage,
@@ -3443,9 +3447,10 @@ function build(request) {
3443
3447
  }
3444
3448
  ];
3445
3449
  case "turns": {
3446
- const sessionID = request.turnsScope === "session" ? request.sessionID : undefined;
3450
+ const scope = scoped ? "session" : request.turnsScope;
3451
+ const sessionID = scope === "session" ? request.sessionID : undefined;
3447
3452
  const rows = sortTurns(turns(db, { sessionID, range, filters, limit: 300 }), request.sort, request.sortDesc, estimates);
3448
- const titles = request.turnsScope === "all" ? new Map(sessions(db, { range, filters, limit: 200 }).map((row) => [row.sessionID, row.title ?? row.sessionID])) : undefined;
3453
+ const titles = scope === "all" ? new Map(sessions(db, { range, filters, limit: 200 }).map((row) => [row.sessionID, row.title ?? row.sessionID])) : undefined;
3449
3454
  return turnsLines(rows.slice(0, 60), width, { ...request.format, estimates, sessionTitles: titles });
3450
3455
  }
3451
3456
  }
@@ -4102,4 +4107,4 @@ scope.onmessage = (event) => {
4102
4107
  }
4103
4108
  };
4104
4109
 
4105
- //# debugId=E499AA8C5F2EFBAF64756E2164756E21
4110
+ //# debugId=B70242210F34271964756E2164756E21