opencode-usage-insights 0.1.2 → 0.1.3

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/line.d.ts CHANGED
@@ -66,10 +66,6 @@ export declare function toTranscript(messages: readonly RuntimeMessage[], price?
66
66
  * agent's output. Walking backwards costs what the last turns hold.
67
67
  */
68
68
  export declare function toTranscriptTail(messages: readonly RuntimeMessage[], segments?: number, price?: PriceFn | undefined): TranscriptMessage[];
69
- /**
70
- * Session-wide accumulated usage: the host's tokens and charged cost, plus the
71
- * list-price estimate the plugin recorded for the session's steps.
72
- */
73
69
  export declare function sessionTotals(info: RuntimeSessionInfo | undefined, listCost?: number): Totals | undefined;
74
70
  /**
75
71
  * The telemetry line as renderable parts: each segment plus the muted
@@ -170,7 +170,7 @@ export declare function cacheLines(data: {
170
170
  gap?: GapReport | undefined;
171
171
  models?: readonly CacheModelRow[] | undefined;
172
172
  }, width: number): PanelLine[];
173
- export declare function periodLines(rows: readonly PeriodRow[], width: number, granularity: PeriodGranularity, limit?: number, estimates?: boolean): PanelLine[];
173
+ export declare function periodLines(rows: readonly PeriodRow[], width: number, granularity: PeriodGranularity, limit?: number, estimates?: boolean, totalSessions?: number): PanelLine[];
174
174
  export declare function latencyLines(report: LatencyReport, width: number, groups?: readonly LatencyGroupRow[] | undefined, limit?: number, estimates?: boolean): PanelLine[];
175
175
  export declare function sessionsLines(rows: readonly SessionUsage[], width: number, options?: {
176
176
  limit?: number | undefined;
@@ -28,6 +28,8 @@ export type TokenUsageLike = {
28
28
  export type RecorderEventData = {
29
29
  sessionID?: string | undefined;
30
30
  assistantMessageID?: string | undefined;
31
+ /** Host usage source: `generate`, `compaction` or `title`. */
32
+ source?: string | undefined;
31
33
  inboxID?: string | undefined;
32
34
  item?: {
33
35
  type?: string | undefined;
@@ -47,6 +49,8 @@ export type RecorderEventData = {
47
49
  title?: string | undefined;
48
50
  };
49
51
  export type RecorderEvent = {
52
+ /** Host event ID: the idempotency key for usage and compaction rows. */
53
+ id?: string | undefined;
50
54
  type: string;
51
55
  created?: number | undefined;
52
56
  data?: RecorderEventData | undefined;
@@ -19,7 +19,7 @@
19
19
  * `test/reports.test.ts` holds every report to those rules.
20
20
  */
21
21
  import type { Database } from "bun:sqlite";
22
- import { type Filters, type LatencyStats, type Range, type Usage } from "./queries";
22
+ import { type Filters, type LatencyStats, type OtherUsageRow, type Range, type Usage } from "./queries";
23
23
  /** The step's own clock, the expression the `steps_at` index covers. */
24
24
  export declare const STEP_AT = "COALESCE(created_at, recorded_at)";
25
25
  type Options = {
@@ -266,6 +266,8 @@ export type PeriodRow = {
266
266
  sessions: number;
267
267
  usage: Usage;
268
268
  cache: CacheStats;
269
+ /** Compaction/title money included in `usage.cost`, when the period has any. */
270
+ otherCost?: number | undefined;
269
271
  };
270
272
  /** ISO-8601 week number and its Monday, in local time. */
271
273
  export declare function isoWeek(date: Date): {
@@ -273,12 +275,20 @@ export declare function isoWeek(date: Date): {
273
275
  week: number;
274
276
  monday: Date;
275
277
  };
278
+ /** Local end (exclusive) of the period that starts at `at`. */
279
+ export declare function periodEnd(at: number, granularity: PeriodGranularity): number;
276
280
  /** Key, label and local start of the period `at` falls in. */
277
281
  export declare function periodStart(at: number, granularity: PeriodGranularity): {
278
282
  key: string;
279
283
  label: string;
280
284
  start: number;
281
285
  };
282
- /** Totals per local calendar day, week or month, oldest first. */
283
- export declare function periodsOf(rows: readonly StepRow[], granularity: PeriodGranularity): PeriodRow[];
286
+ /**
287
+ * Totals per local calendar day, week or month, oldest first.
288
+ *
289
+ * `otherRows` are the host's non-step usage records (compaction and title
290
+ * generations). Their cost is added to the bucket they fall in, because the
291
+ * session's own total includes it; the token buckets stay step-based.
292
+ */
293
+ export declare function periodsOf(rows: readonly StepRow[], granularity: PeriodGranularity, otherRows?: readonly OtherUsageRow[]): PeriodRow[];
284
294
  export {};
@@ -14,9 +14,9 @@ import { Database } from "bun:sqlite";
14
14
  * - `openStatsDbWrite` is a short-lived maintenance connection (prune, vacuum)
15
15
  * that gives up quickly instead of holding the recorder up.
16
16
  */
17
- export declare const SCHEMA_VERSION = 10;
17
+ export declare const SCHEMA_VERSION = 11;
18
18
  /** Tables carried by `exportNdjson` / `importNdjson`, in import-safe order. */
19
- export declare const TABLES: readonly ["sessions", "prompts", "events", "steps"];
19
+ export declare const TABLES: readonly ["sessions", "prompts", "events", "steps", "usage"];
20
20
  export type TableName = (typeof TABLES)[number];
21
21
  export type StepRecord = {
22
22
  messageID: string;
@@ -57,6 +57,23 @@ export type EventRecord = {
57
57
  at?: number | undefined;
58
58
  data?: unknown;
59
59
  };
60
+ /**
61
+ * Usage with no assistant step behind it (a compaction or a title generation).
62
+ * `id` is the host event ID: it is the primary key, so a replay is ignored
63
+ * instead of counted again.
64
+ */
65
+ export type UsageRecord = {
66
+ id: string;
67
+ sessionID: string;
68
+ source: string;
69
+ at?: number | undefined;
70
+ cost?: number | undefined;
71
+ input?: number | undefined;
72
+ output?: number | undefined;
73
+ reasoning?: number | undefined;
74
+ cacheRead?: number | undefined;
75
+ cacheWrite?: number | undefined;
76
+ };
60
77
  export type SessionRecord = {
61
78
  sessionID: string;
62
79
  parentID?: string | undefined;
@@ -92,6 +109,12 @@ export declare function openStatsDbWrite(path: string): Database;
92
109
  * absent instead of becoming a grouping key.
93
110
  */
94
111
  export declare function recordStep(db: Database, record: StepRecord): void;
112
+ /**
113
+ * Insert usage that has no assistant step of its own. The host event ID is the
114
+ * primary key: a replayed event is ignored, so the totals never double count a
115
+ * compaction or a title generation delivered more than once.
116
+ */
117
+ export declare function recordUsage(db: Database, record: UsageRecord): void;
95
118
  export declare function recordPrompt(db: Database, record: PromptRecord): void;
96
119
  export declare function recordEvent(db: Database, record: EventRecord): void;
97
120
  export declare function upsertSession(db: Database, record: SessionRecord): void;
@@ -36,6 +36,8 @@ export type PruneResult = {
36
36
  steps: number;
37
37
  prompts: number;
38
38
  events: number;
39
+ /** Compaction/title usage records deleted with the steps they belong to. */
40
+ usage: number;
39
41
  };
40
42
  /**
41
43
  * Delete recorded usage older than `cutoff`. Steps, their prompts and the
@@ -69,6 +69,31 @@ export declare function hasEstimate(usage: Pick<Usage, "listCost">): boolean;
69
69
  * estimate (or a database without the column) answers 0.
70
70
  */
71
71
  export declare function sessionListCost(db: Database, sessionID: string): number;
72
+ /**
73
+ * Money the host reported with no assistant step behind it: a compaction
74
+ * summary or a generated title. It is part of the session's own cost, so the
75
+ * totals that claim to describe a session include it.
76
+ */
77
+ export type OtherUsage = {
78
+ /** Host usage records behind the number. */
79
+ entries: number;
80
+ cost: number;
81
+ };
82
+ export declare const EMPTY_OTHER: OtherUsage;
83
+ /** Non-step usage in the selection. */
84
+ export declare function otherUsage(db: Database, range?: Range | undefined, filters?: Filters | undefined): OtherUsage;
85
+ /** Non-step usage per session, for the session rows. */
86
+ export declare function otherUsageBySession(db: Database, range?: Range | undefined, filters?: Filters | undefined): Map<string, OtherUsage>;
87
+ export type OtherUsageRow = {
88
+ sessionID: string;
89
+ at: number;
90
+ cost: number;
91
+ };
92
+ /**
93
+ * Non-step usage as one row per host record, so calendar buckets can fold it
94
+ * into their periods with the same local-time rule the steps use.
95
+ */
96
+ export declare function otherUsageRows(db: Database, range?: Range | undefined, filters?: Filters | undefined): OtherUsageRow[];
72
97
  export declare const STEP_TIME = "COALESCE(created_at, recorded_at)";
73
98
  /**
74
99
  * The identity of a model *and* its variant, in SQL: `modelKey` says a variant
@@ -96,6 +121,8 @@ export declare function cacheHitRatio(usage: Pick<Usage, "input" | "cached" | "c
96
121
  export type Overview = Usage & {
97
122
  sessions: number;
98
123
  prompts: number;
124
+ /** Compaction/title money included in `cost`, when the range has any. */
125
+ otherCost?: number | undefined;
99
126
  firstAt: number | undefined;
100
127
  lastAt: number | undefined;
101
128
  cacheHitRatio: number | undefined;
@@ -176,6 +203,8 @@ export type SessionUsage = Usage & {
176
203
  providers: string[];
177
204
  models: string[];
178
205
  prompts: number;
206
+ /** Compaction/title money included in `cost`, when the session has any. */
207
+ otherCost?: number | undefined;
179
208
  firstAt: number | undefined;
180
209
  lastAt: number | undefined;
181
210
  cacheHitRatio: number | undefined;
@@ -205,7 +234,7 @@ export type TurnUsage = {
205
234
  /** Creation time of the prompt that opened the turn, when it was recorded. */
206
235
  promptAt: number | undefined;
207
236
  endAt: number | undefined;
208
- /** Time between the previous prompt of the session and this turn's prompt. */
237
+ /** Time between the previous finished turn's prompt and this turn's prompt. */
209
238
  promptGapMs: number | undefined;
210
239
  providerID: string | undefined;
211
240
  modelID: string | undefined;
@@ -232,10 +261,11 @@ export type TurnUsage = {
232
261
  * before them; a turn whose last step asked for tools is dropped, because it is
233
262
  * still running — the same rule the telemetry line follows.
234
263
  *
235
- * For one session the read is bounded by a window: only the steps at or after
236
- * the `limit`-th newest prompt are loaded, so a session with tens of thousands
237
- * of steps costs the same as a short one. The extra prompt is kept as the
238
- * reference the oldest returned turn measures its gap against.
264
+ * For one session the read is bounded by a window of the newest prompts. The
265
+ * window grows until it holds `limit` finished turns: a prompt can be empty
266
+ * (the host records a synthetic system-reminder next to the user prompt) and
267
+ * the newest turn can still be running, so a fixed one-prompt margin is not
268
+ * enough. `complete` means the window covers every prompt of the session.
239
269
  */
240
270
  export declare function turns(db: Database, options?: {
241
271
  sessionID?: string | undefined;
package/dist/tui.d.ts CHANGED
@@ -38,7 +38,7 @@ export type Options = {
38
38
  labels?: Partial<LineLabels> | undefined;
39
39
  /** Show the per-prompt history in the session sidebar (default true). */
40
40
  sidebar?: boolean | undefined;
41
- /** How many turns the sidebar history shows (default 5). */
41
+ /** How many turns the sidebar history shows (default 8). */
42
42
  sidebarTurns?: number | undefined;
43
43
  /**
44
44
  * Show the `/usage` statistics panel and its command (default true).