oc-metricboard 0.1.13 → 0.1.15

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,66 @@
1
+ import { estimateTokens } from "./metrics";
2
+ import { parseAssistantTextDelta } from "./event-shapes";
3
+ import { currentRequest } from "./request-state";
4
+ import { eventAggregateID, eventID, eventProperties, stringEventProperty } from "./event-bus";
5
+ import { recordLiveTokens } from "./live-speed";
6
+ import { ensureTurn, recordTurnFirstToken } from "./turn-state";
7
+ function partEstimateKey(sessionID, messageID, partID) {
8
+ return `${sessionID}:${messageID}:${partID}`;
9
+ }
10
+ function applyTokenDelta(ctx, sessionID, messageID, deltaTokens, now) {
11
+ const { actions, state } = ctx;
12
+ if (state.turns.get(sessionID)?.finalizedSteps.has(messageID))
13
+ return;
14
+ const current = currentRequest({ state, actions, sessionID, messageID, now });
15
+ if (current.firstTokenTime === null)
16
+ current.firstTokenTime = now;
17
+ recordTurnFirstToken(ensureTurn(state.turns, sessionID, now), now);
18
+ if (deltaTokens > 0) {
19
+ current.estimatedOutputTokens += deltaTokens;
20
+ current.lastDeltaTime = now;
21
+ recordLiveTokens(state.liveSpeeds, sessionID, messageID, deltaTokens, now);
22
+ }
23
+ current.isStreaming = true;
24
+ actions.notify();
25
+ }
26
+ function applyAssistantText(ctx, sessionID, messageID, partID, text, now) {
27
+ const { actions, config, state } = ctx;
28
+ const key = partEstimateKey(sessionID, messageID, partID);
29
+ const nextTokens = estimateTokens(text, config.estimationRatio);
30
+ const previousTokens = state.partTokenEstimates.get(key) ?? 0;
31
+ const deltaTokens = Math.max(0, nextTokens - previousTokens);
32
+ state.partTokenEstimates.set(key, Math.max(previousTokens, nextTokens));
33
+ applyTokenDelta(ctx, sessionID, messageID, deltaTokens, now);
34
+ }
35
+ export function applyAssistantProgress(ctx, sessionID, part, now) {
36
+ applyAssistantText(ctx, sessionID, part.messageID, part.partID, part.text, now);
37
+ }
38
+ export function applyAssistantDelta(ctx, event) {
39
+ const { actions, state } = ctx;
40
+ const sessionID = stringEventProperty(event, "sessionID");
41
+ const fallbackMessageID = sessionID.length > 0 ? state.assistantMessageIds.get(sessionID) ?? eventID(event) : eventID(event);
42
+ const text = parseAssistantTextDelta(eventProperties(event), fallbackMessageID);
43
+ if (!text)
44
+ return;
45
+ const aggregateID = eventAggregateID(event);
46
+ if (aggregateID.length > 0 && aggregateID !== text.sessionID) {
47
+ const aggregateAliases = state.sessionAliases.get(aggregateID) ?? new Set();
48
+ aggregateAliases.add(text.sessionID);
49
+ state.sessionAliases.set(aggregateID, aggregateAliases);
50
+ const sessionAliases = state.sessionAliases.get(text.sessionID) ?? new Set();
51
+ sessionAliases.add(aggregateID);
52
+ state.sessionAliases.set(text.sessionID, sessionAliases);
53
+ }
54
+ const now = performance.now();
55
+ actions.startSessionTiming(text.sessionID, now);
56
+ const key = partEstimateKey(text.sessionID, text.messageID, text.partID);
57
+ const deltaTokens = estimateTokens(text.delta, ctx.config.estimationRatio);
58
+ state.partTokenEstimates.set(key, (state.partTokenEstimates.get(key) ?? 0) + deltaTokens);
59
+ applyTokenDelta(ctx, text.sessionID, text.messageID, deltaTokens, now);
60
+ }
61
+ export function applyAssistantPartDelta(ctx, sessionID, messageID, partID, delta, now) {
62
+ const key = partEstimateKey(sessionID, messageID, partID);
63
+ const deltaTokens = estimateTokens(delta, ctx.config.estimationRatio);
64
+ ctx.state.partTokenEstimates.set(key, (ctx.state.partTokenEstimates.get(key) ?? 0) + deltaTokens);
65
+ applyTokenDelta(ctx, sessionID, messageID, deltaTokens, now);
66
+ }
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Pick the text color for the sidebar header badge (a bold label drawn on a
3
+ * `theme.accent` background).
4
+ *
5
+ * Primary rule: paint the theme's own `background` color as the label, giving
6
+ * the inverse-of-panel look consistent with other sidebar plugins.
7
+ *
8
+ * Fallback rule: `theme.background` can be unusable as a label color when:
9
+ * 1. Transparent background (alpha < 0.5). Drawing transparent text on the
10
+ * accent renders the label invisible.
11
+ * 2. Background ~= accent (per-channel distance < 0.06). No contrast.
12
+ * In either case we fall back to a black/white pick by accent luminance.
13
+ *
14
+ * Accepts the minimal `{ r, g, b, a? }` shape so this stays a pure, trivially
15
+ * testable function independent of the native color class.
16
+ */
17
+ const MIN_OPAQUE_ALPHA = 0.5;
18
+ const MIN_CHANNEL_DISTANCE = 0.06;
19
+ const LIGHT_ACCENT_LUMINANCE = 0.5;
20
+ function srgbChannelToLinear(c) {
21
+ return c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
22
+ }
23
+ function relativeLuminance(bg) {
24
+ return (0.2126 * srgbChannelToLinear(bg.r) +
25
+ 0.7152 * srgbChannelToLinear(bg.g) +
26
+ 0.0722 * srgbChannelToLinear(bg.b));
27
+ }
28
+ function nearlyEqual(a, b) {
29
+ return (Math.abs(a.r - b.r) < MIN_CHANNEL_DISTANCE &&
30
+ Math.abs(a.g - b.g) < MIN_CHANNEL_DISTANCE &&
31
+ Math.abs(a.b - b.b) < MIN_CHANNEL_DISTANCE);
32
+ }
33
+ /**
34
+ * Pure black/white pick by accent luminance. Used as the badge fallback.
35
+ */
36
+ export function readableTextColorOn(bg) {
37
+ return relativeLuminance(bg) < LIGHT_ACCENT_LUMINANCE ? "#ffffff" : "#000000";
38
+ }
39
+ /**
40
+ * Badge label color on the accent: the theme background when it is usable,
41
+ * else a guaranteed-visible black/white fallback. Returns the passed-in
42
+ * `background` reference unchanged on the primary path.
43
+ */
44
+ export function badgeTextColor(accent, background) {
45
+ const alpha = background.a ?? 1;
46
+ if (alpha >= MIN_OPAQUE_ALPHA && !nearlyEqual(accent, background)) {
47
+ return background;
48
+ }
49
+ return readableTextColorOn(accent);
50
+ }
@@ -0,0 +1 @@
1
+ export {};