oc-flight-deck 0.6.2 → 0.6.6

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.
@@ -0,0 +1,130 @@
1
+ // Per-line appearance for the rail: colour roles and text attributes.
2
+ //
3
+ // A config file names a role, not a raw theme token: `default`, `subdued`,
4
+ // `warning`, `error`, `success`, `info`. This module maps a role onto the host
5
+ // theme at render time. The host renamed its text tokens in 2.0.16
6
+ // (`default` -> `base`, `subdued` -> `muted`, feedback `default` -> `base`), so
7
+ // each role prefers the name this plugin has always read and falls back to the
8
+ // renamed one. On a host that provides neither, the colour stays `undefined`
9
+ // and the renderer's own default stands — which is exactly what an absent
10
+ // token has always done.
11
+ //
12
+ // Everything is a table lookup, so the config surface is trivial to test.
13
+ // `attributeMask` is the one place that reads @opentui/core, because the bits
14
+ // must be the vendored renderer's own constants — never re-derived, never ANSI.
15
+
16
+ import { TextAttributes } from "@opentui/core";
17
+
18
+ /** Colours a config may name. Roles, not raw theme tokens. */
19
+ export const STYLE_COLORS = ["default", "subdued", "warning", "error", "success", "info"] as const;
20
+
21
+ export type StyleColor = (typeof STYLE_COLORS)[number];
22
+
23
+ /**
24
+ * Text attributes a config may name, exactly the vendored renderer's enum
25
+ * minus `NONE` (an empty list is how "no attributes" is written).
26
+ */
27
+ export const STYLE_ATTRIBUTES = [
28
+ "bold",
29
+ "dim",
30
+ "italic",
31
+ "underline",
32
+ "blink",
33
+ "inverse",
34
+ "hidden",
35
+ "strikethrough",
36
+ ] as const;
37
+
38
+ export type StyleAttribute = (typeof STYLE_ATTRIBUTES)[number];
39
+
40
+ /** One line's look: a named colour plus zero or more attributes. */
41
+ export interface LineStyle {
42
+ readonly color: StyleColor;
43
+ readonly attributes: readonly StyleAttribute[];
44
+ }
45
+
46
+ /** The `style` config section, resolved. */
47
+ export interface StyleConfig {
48
+ /** The fixed branding/separator lines above the live rows. */
49
+ readonly lines: LineStyle;
50
+ readonly rows: {
51
+ /** Every live row inherits this unless it has its own entry. */
52
+ readonly wildcard: LineStyle;
53
+ /** Per-row entries, only for known row names. */
54
+ readonly overrides: Readonly<Partial<Record<string, LineStyle>>>;
55
+ };
56
+ }
57
+
58
+ /**
59
+ * The shipped look: fixed lines in the theme's primary text colour, live rows
60
+ * subdued, no attributes. This is what an absent `style` section must keep.
61
+ */
62
+ export const DEFAULT_STYLE: StyleConfig = {
63
+ lines: { color: "default", attributes: [] },
64
+ rows: { wildcard: { color: "subdued", attributes: [] }, overrides: {} },
65
+ };
66
+
67
+ /** The style for one live row: its own entry when it has one, else the wildcard. */
68
+ export function rowStyle(style: StyleConfig, field: string): LineStyle {
69
+ return style.rows.overrides[field] ?? style.rows.wildcard;
70
+ }
71
+
72
+ /**
73
+ * The host theme surface the rail reads. Structural on purpose: ./config.ts
74
+ * must not import a renderer, and tests can pass plain values.
75
+ *
76
+ * Both naming generations are declared because the host renamed the text tokens
77
+ * in 2.0.16; a host provides one or the other.
78
+ */
79
+ export interface ThemeLike {
80
+ readonly text?: {
81
+ readonly default?: unknown;
82
+ readonly subdued?: unknown;
83
+ readonly base?: unknown;
84
+ readonly muted?: unknown;
85
+ readonly feedback?: Readonly<
86
+ Partial<Record<"error" | "warning" | "success" | "info", { readonly default?: unknown; readonly base?: unknown }>>
87
+ >;
88
+ };
89
+ }
90
+
91
+ /**
92
+ * The theme colour for a named role, or `undefined` when the host provides
93
+ * neither the current nor the renamed token.
94
+ */
95
+ export function themeColor(color: StyleColor, theme: ThemeLike): unknown {
96
+ const text = theme.text;
97
+ if (text === undefined) return undefined;
98
+ switch (color) {
99
+ case "default":
100
+ return text.default ?? text.base;
101
+ case "subdued":
102
+ return text.subdued ?? text.muted;
103
+ default: {
104
+ const feedback = text.feedback?.[color];
105
+ return feedback?.default ?? feedback?.base;
106
+ }
107
+ }
108
+ }
109
+
110
+ /**
111
+ * Renderer bits for each name, read from the vendored enum rather than
112
+ * re-derived, so a renderer change cannot silently desync the config surface.
113
+ */
114
+ const ATTRIBUTE_BITS: Readonly<Record<StyleAttribute, number>> = {
115
+ bold: TextAttributes.BOLD,
116
+ dim: TextAttributes.DIM,
117
+ italic: TextAttributes.ITALIC,
118
+ underline: TextAttributes.UNDERLINE,
119
+ blink: TextAttributes.BLINK,
120
+ inverse: TextAttributes.INVERSE,
121
+ hidden: TextAttributes.HIDDEN,
122
+ strikethrough: TextAttributes.STRIKETHROUGH,
123
+ };
124
+
125
+ /** The renderer bitmask for a list of attributes, or `undefined` when there are none. */
126
+ export function attributeMask(attributes: readonly StyleAttribute[]): number | undefined {
127
+ let mask = 0;
128
+ for (const attribute of attributes) mask |= ATTRIBUTE_BITS[attribute];
129
+ return mask === 0 ? undefined : mask;
130
+ }
@@ -0,0 +1,156 @@
1
+ // Session throughput: output tokens per second, measured over the active work
2
+ // the host actually recorded rather than over the wall clock.
3
+ //
4
+ // Pure: no clock, no I/O, no rendering. Untrusted sample fields are coerced
5
+ // through ./coerce.js so a garbage value is skipped rather than guessed at.
6
+ //
7
+ // Two rates live here. `sessionThroughput` is the whole-lifetime average, kept
8
+ // as the metric of last resort for a host that exposes no per-message
9
+ // timestamps. `unionSpanThroughput` is the active-work average: output tokens
10
+ // divided by the union of the assistant turns' own spans, so idle between turns
11
+ // is never in the denominator and the figure freezes once everything settles.
12
+
13
+ import { asCount, asText } from "./coerce.js";
14
+
15
+ /**
16
+ * Overall session throughput: output tokens over the session's lifetime.
17
+ *
18
+ * Unlike the active-work average, this is the whole conversation's average, so
19
+ * it includes every subagent and every pause. A just-started session is floored
20
+ * at one second so it cannot divide by a sliver of time and flash an absurd
21
+ * rate. Returns `undefined` when there is nothing to divide.
22
+ */
23
+ export function sessionThroughput(outputTokens: unknown, elapsedMs: unknown): number | undefined {
24
+ const output = asCount(outputTokens);
25
+ const elapsed = asCount(elapsedMs);
26
+ if (output === undefined || output <= 0) return undefined;
27
+ if (elapsed === undefined || elapsed <= 0) return undefined;
28
+ return output / (Math.max(elapsed, 1_000) / 1_000);
29
+ }
30
+
31
+ /**
32
+ * One assistant turn's contribution to the active-work average.
33
+ *
34
+ * `key` is the turn's identity (see `turnKey`); `tokens` is its output count;
35
+ * `start` and `end` are its span on the host's epoch clock. Every field is
36
+ * untrusted and coerced before use.
37
+ */
38
+ export interface ThroughputSpan {
39
+ readonly key?: unknown;
40
+ readonly tokens?: unknown;
41
+ readonly start?: unknown;
42
+ readonly end?: unknown;
43
+ }
44
+
45
+ /**
46
+ * Resolve one assistant turn's span from its timestamp rungs.
47
+ *
48
+ * `start` is `time.created`. `end` is `time.completed`, else `time.streamed`,
49
+ * else the caller's `nowMs` for a turn still in flight. A clock that steps
50
+ * backwards is clamped to a zero-length span rather than a negative one, so
51
+ * skew can never subtract time from the union. Returns `undefined` when there
52
+ * is no usable start — the turn is skipped, never guessed at.
53
+ */
54
+ export function turnSpan(
55
+ created: unknown,
56
+ completed: unknown,
57
+ streamed: unknown,
58
+ nowMs: unknown,
59
+ ): { readonly start: number; readonly end: number } | undefined {
60
+ const start = asCount(created);
61
+ if (start === undefined) return undefined;
62
+ const ended = asCount(completed) ?? asCount(streamed) ?? asCount(nowMs);
63
+ if (ended === undefined) return undefined;
64
+ return { start, end: Math.max(start, ended) };
65
+ }
66
+
67
+ /**
68
+ * Identity of one assistant turn: the host's own message id when it carries
69
+ * one, otherwise the tuple `(created, completed, output)` that defines the
70
+ * record. Retries, replays and re-sent records share a key, so they contribute
71
+ * their tokens once and their time once.
72
+ */
73
+ export function turnKey(id: unknown, created: unknown, completed: unknown, output: unknown): string {
74
+ const own = asText(id);
75
+ if (own !== undefined) return `id:${own}`;
76
+ return `t:${asCount(created) ?? "?"}:${asCount(completed) ?? "?"}:${asCount(output) ?? "?"}`;
77
+ }
78
+
79
+ /** Output tokens and wall time the accepted turns actually cover. */
80
+ export interface SpanTotals {
81
+ readonly tokens: number;
82
+ readonly unionMs: number;
83
+ }
84
+
85
+ /**
86
+ * Sum the tokens of distinct turns and the union length of their spans.
87
+ *
88
+ * De-duplicates by `key` first, so a re-sent record adds its tokens once and
89
+ * enters the union once — a dropped duplicate drops both sides together.
90
+ * Spans are clamped (`end >= start`), then sorted and merged so overlapping
91
+ * and touching turns count once and input order cannot change the result. A
92
+ * turn with no usable token count contributes to neither side: the numerator
93
+ * and the denominator always cover exactly the same turns.
94
+ */
95
+ export function unionSpanTotals(spans: readonly ThroughputSpan[]): SpanTotals {
96
+ const seen = new Set<string>();
97
+ const accepted: Array<{ start: number; end: number }> = [];
98
+ let tokens = 0;
99
+
100
+ for (const span of spans) {
101
+ const key = asText(span.key);
102
+ if (key === undefined || seen.has(key)) continue;
103
+
104
+ const start = asCount(span.start);
105
+ const end = asCount(span.end);
106
+ const output = asCount(span.tokens);
107
+ if (start === undefined || end === undefined) continue;
108
+ if (output === undefined || output <= 0) continue;
109
+
110
+ seen.add(key);
111
+ accepted.push({ start, end: Math.max(start, end) });
112
+ tokens += output;
113
+ }
114
+
115
+ // Pre-sort so the merge cannot depend on the order the host returned them.
116
+ accepted.sort((a, b) => a.start - b.start || a.end - b.end);
117
+ const first = accepted[0];
118
+ if (first === undefined) return { tokens, unionMs: 0 };
119
+
120
+ let unionMs = 0;
121
+ let openStart = first.start;
122
+ let openEnd = first.end;
123
+ for (let index = 1; index < accepted.length; index += 1) {
124
+ const next = accepted[index];
125
+ if (next === undefined) continue;
126
+ // Touching spans (`next.start === openEnd`) merge as well as overlapping
127
+ // ones: a turn that starts exactly when another ends is continuous work.
128
+ if (next.start <= openEnd) {
129
+ if (next.end > openEnd) openEnd = next.end;
130
+ } else {
131
+ unionMs += openEnd - openStart;
132
+ openStart = next.start;
133
+ openEnd = next.end;
134
+ }
135
+ }
136
+ unionMs += openEnd - openStart;
137
+
138
+ return { tokens, unionMs };
139
+ }
140
+
141
+ /**
142
+ * Active-work throughput: output tokens of the scope divided by the union of
143
+ * its active turn spans. Idle time between turns is not in the denominator, so
144
+ * the figure stops moving once everything settles instead of decaying or
145
+ * hiding. A turn still in flight ends at the caller's `now`, so the value keeps
146
+ * climbing while work happens.
147
+ *
148
+ * Floored at one second, the same floor the lifetime average uses, so a sliver
149
+ * of time cannot flash an absurd rate. Returns `undefined` when there is
150
+ * nothing to divide — no positive output, or no span with positive length.
151
+ */
152
+ export function unionSpanThroughput(spans: readonly ThroughputSpan[]): number | undefined {
153
+ const { tokens, unionMs } = unionSpanTotals(spans);
154
+ if (tokens <= 0 || unionMs <= 0) return undefined;
155
+ return tokens / (Math.max(unionMs, 1_000) / 1_000);
156
+ }
package/src/tui/ticker.ts CHANGED
@@ -54,7 +54,7 @@ export interface TickerHost {
54
54
  };
55
55
  }
56
56
 
57
- export interface Ticker {
57
+ interface Ticker {
58
58
  /**
59
59
  * Current frame, read from the host's store.
60
60
  *
@@ -78,7 +78,7 @@ export const TICKER_KEY = "flight-deck.frame";
78
78
  * instead of ten times. The timer itself keeps firing (cheap); it is the store
79
79
  * write, which wakes the host's reactive graph, that is skipped.
80
80
  */
81
- export const IDLE_REFRESH_MS = 1_000;
81
+ const IDLE_REFRESH_MS = 1_000;
82
82
 
83
83
  // Untrusted store values are normalised here rather than at the render, so a
84
84
  // corrupt frame can never reach the spinner's array index.