@koinzhang/paseo-plugin-activity 0.5.0 → 0.6.0
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/CHANGELOG.md +44 -1
- package/README.md +2 -2
- package/client/activity-heatmap.tsx +17 -25
- package/client/agent-creations.tsx +207 -0
- package/client/color-mix.test.ts +27 -0
- package/client/color-mix.ts +38 -0
- package/client/fit-text.test.ts +41 -0
- package/client/fit-text.ts +33 -0
- package/client/global-surface.tsx +104 -31
- package/client/hourly-activity-timeline.tsx +392 -0
- package/client/measured-width.ts +72 -0
- package/client/range.test.ts +44 -0
- package/client/range.ts +14 -0
- package/client/rank-color.test.ts +108 -14
- package/client/rank-color.ts +261 -48
- package/client/usage-stats.tsx +116 -5
- package/client/workspace/panel.tsx +88 -2
- package/client/workspace/rank-section.tsx +150 -11
- package/index.server.ts +30 -0
- package/package.json +2 -2
- package/server/background-sync.test.ts +43 -0
- package/server/background-sync.ts +9 -0
- package/server/handlers-query.test.ts +225 -0
- package/server/handlers.ts +117 -0
- package/shared/activity.test.ts +123 -6
- package/shared/activity.ts +103 -5
- package/shared/classify.test.ts +10 -0
- package/shared/classify.ts +10 -0
- package/shared/explorer-agent-display.test.ts +61 -0
- package/shared/explorer-agent-display.ts +21 -2
- package/shared/format.test.ts +28 -3
- package/shared/format.ts +22 -2
- package/shared/insights.test.ts +113 -70
- package/shared/insights.ts +53 -39
- package/shared/usage.test.ts +209 -0
- package/shared/usage.ts +283 -1
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { describe, it } from "node:test";
|
|
3
|
+
import { fixedWindowFrom, rangeFrom } from "./range.ts";
|
|
4
|
+
|
|
5
|
+
describe("fixedWindowFrom", () => {
|
|
6
|
+
it("starts at local midnight so 30 days yield 30 buckets including today", () => {
|
|
7
|
+
const now = new Date(2026, 2, 10, 23, 30);
|
|
8
|
+
const start = new Date(fixedWindowFrom(30, now));
|
|
9
|
+
assert.equal(start.getFullYear(), 2026);
|
|
10
|
+
assert.equal(start.getMonth(), 1);
|
|
11
|
+
assert.equal(start.getDate(), 9); // Mar 10 minus 29 days
|
|
12
|
+
assert.equal(start.getHours(), 0);
|
|
13
|
+
assert.equal(start.getMinutes(), 0);
|
|
14
|
+
assert.equal(start.getSeconds(), 0);
|
|
15
|
+
assert.equal(start.getMilliseconds(), 0);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("keeps a single day for one-day and degenerate windows", () => {
|
|
19
|
+
const now = new Date(2026, 2, 10, 8, 0);
|
|
20
|
+
for (const days of [1, 0, -5]) {
|
|
21
|
+
const start = new Date(fixedWindowFrom(days, now));
|
|
22
|
+
assert.equal(start.getDate(), 10);
|
|
23
|
+
assert.equal(start.getHours(), 0);
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("crosses month, year and DST boundaries without drifting", () => {
|
|
28
|
+
assert.equal(new Date(fixedWindowFrom(30, new Date(2026, 0, 5, 12))).getDate(), 7); // Dec 7
|
|
29
|
+
assert.equal(new Date(fixedWindowFrom(30, new Date(2026, 0, 5, 12))).getMonth(), 11);
|
|
30
|
+
const dst = new Date(fixedWindowFrom(30, new Date(2026, 2, 30, 12)));
|
|
31
|
+
assert.equal(dst.getDate(), 1); // Mar 1
|
|
32
|
+
assert.equal(dst.getHours(), 0);
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
describe("rangeFrom", () => {
|
|
37
|
+
it("returns no lower bound for all time and local midnight for today", () => {
|
|
38
|
+
assert.equal(rangeFrom("all"), undefined);
|
|
39
|
+
const today = new Date(rangeFrom("today")!);
|
|
40
|
+
const now = new Date();
|
|
41
|
+
assert.equal(today.getDate(), now.getDate());
|
|
42
|
+
assert.equal(today.getHours(), 0);
|
|
43
|
+
});
|
|
44
|
+
});
|
package/client/range.ts
CHANGED
|
@@ -18,3 +18,17 @@ export function rangeFrom(id: RangeId): string | undefined {
|
|
|
18
18
|
const days = id === "7d" ? 7 : 30;
|
|
19
19
|
return new Date(Date.now() - days * 24 * 60 * 60 * 1000).toISOString();
|
|
20
20
|
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Local-midnight start of a fixed `days`-day window ending today (050).
|
|
24
|
+
* `days = 30` yields exactly 30 local-day buckets including today; the window
|
|
25
|
+
* does not follow the range chips.
|
|
26
|
+
*/
|
|
27
|
+
export function fixedWindowFrom(days: number, now: Date = new Date()): string {
|
|
28
|
+
const start = new Date(
|
|
29
|
+
now.getFullYear(),
|
|
30
|
+
now.getMonth(),
|
|
31
|
+
now.getDate() - (Math.max(1, Math.floor(days)) - 1),
|
|
32
|
+
);
|
|
33
|
+
return start.toISOString();
|
|
34
|
+
}
|
|
@@ -1,15 +1,56 @@
|
|
|
1
1
|
import assert from "node:assert/strict";
|
|
2
2
|
import { test } from "node:test";
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
brandColor,
|
|
5
|
+
chartColorScheme,
|
|
6
|
+
creationProviderColors,
|
|
7
|
+
entityColor,
|
|
8
|
+
knownProviderSlotCount,
|
|
9
|
+
providerColor,
|
|
10
|
+
} from "./rank-color.ts";
|
|
4
11
|
|
|
5
|
-
const
|
|
12
|
+
const LIGHT_SURFACE = "#f4f4f5";
|
|
13
|
+
const DARK_SURFACE = "#18181b";
|
|
6
14
|
|
|
7
|
-
test("
|
|
8
|
-
assert.equal(
|
|
15
|
+
test("chartColorScheme reads light vs dark from surface luminance", () => {
|
|
16
|
+
assert.equal(chartColorScheme(LIGHT_SURFACE), "light");
|
|
17
|
+
assert.equal(chartColorScheme(DARK_SURFACE), "dark");
|
|
18
|
+
assert.equal(chartColorScheme("not-a-color"), "dark");
|
|
9
19
|
});
|
|
10
20
|
|
|
11
|
-
test("
|
|
12
|
-
|
|
21
|
+
test("providerColor is stable and switches with scheme", () => {
|
|
22
|
+
assert.equal(providerColor("claude", "light"), providerColor("claude", "light"));
|
|
23
|
+
assert.equal(providerColor("claude/opus", "dark"), providerColor("claude", "dark"));
|
|
24
|
+
assert.notEqual(providerColor("claude", "light"), providerColor("claude", "dark"));
|
|
25
|
+
assert.match(providerColor("claude", "light"), /^#[0-9a-f]{6}$/i);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test("providerColor covers known Paseo providers without falling through", () => {
|
|
29
|
+
const known = [
|
|
30
|
+
"claude",
|
|
31
|
+
"codex",
|
|
32
|
+
"cursor",
|
|
33
|
+
"opencode",
|
|
34
|
+
"pi",
|
|
35
|
+
"omp",
|
|
36
|
+
"copilot",
|
|
37
|
+
"gemini",
|
|
38
|
+
"codebuddy-code",
|
|
39
|
+
"devin",
|
|
40
|
+
"hermes",
|
|
41
|
+
"kiro",
|
|
42
|
+
];
|
|
43
|
+
for (const id of known) {
|
|
44
|
+
assert.match(providerColor(id, "light"), /^#[0-9a-f]{6}$/i);
|
|
45
|
+
}
|
|
46
|
+
assert.equal(providerColor("codebuddy-code", "dark"), providerColor("codebuddy", "dark"));
|
|
47
|
+
assert.ok(knownProviderSlotCount() >= 40, `expected many slots, got ${knownProviderSlotCount()}`);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test("entityColor hashes MCP keys into the soft palette", () => {
|
|
51
|
+
assert.equal(entityColor("github", "light"), entityColor("github", "light"));
|
|
52
|
+
assert.notEqual(entityColor("github", "light"), entityColor("github", "dark"));
|
|
53
|
+
const keys = [
|
|
13
54
|
"github",
|
|
14
55
|
"playwright",
|
|
15
56
|
"context7",
|
|
@@ -19,17 +60,70 @@ test("mcpServerColor spreads servers across distinct colors", () => {
|
|
|
19
60
|
"figma",
|
|
20
61
|
"slack",
|
|
21
62
|
];
|
|
22
|
-
const colors = new Set(
|
|
63
|
+
const colors = new Set(keys.map((key) => entityColor(key, "light")));
|
|
23
64
|
assert.ok(colors.size >= 6, `expected >= 6 distinct colors, got ${colors.size}`);
|
|
24
65
|
});
|
|
25
66
|
|
|
26
|
-
test("
|
|
27
|
-
assert.
|
|
28
|
-
assert.equal(
|
|
67
|
+
test("providerColor uses brand accents for Claude / Codex / Cursor / OpenCode / Pi / Oh My Pi", () => {
|
|
68
|
+
assert.equal(providerColor("claude", "light"), "#d97757");
|
|
69
|
+
assert.equal(providerColor("claude", "dark"), "#c46845");
|
|
70
|
+
assert.equal(providerColor("codex", "light"), "#4d9eef");
|
|
71
|
+
assert.equal(providerColor("codex", "dark"), "#0169cc");
|
|
72
|
+
assert.equal(providerColor("cursor", "light"), "#d94816");
|
|
73
|
+
assert.equal(providerColor("cursor", "dark"), "#b83900");
|
|
74
|
+
assert.equal(providerColor("opencode", "light"), "#74a2ec");
|
|
75
|
+
assert.equal(providerColor("opencode", "dark"), "#5a86d4");
|
|
76
|
+
assert.equal(providerColor("pi", "light"), "#f1be58");
|
|
77
|
+
assert.equal(providerColor("pi", "dark"), "#4d9abf");
|
|
78
|
+
assert.equal(providerColor("omp", "light"), "#9a90f5");
|
|
79
|
+
assert.equal(providerColor("omp", "dark"), "#7f73f2");
|
|
80
|
+
assert.equal(providerColor("copilot", "light"), "#5fed83");
|
|
81
|
+
assert.equal(providerColor("copilot", "dark"), "#077124");
|
|
82
|
+
assert.equal(providerColor("codebuddy", "light"), "#6c4dff");
|
|
83
|
+
assert.equal(providerColor("codebuddy", "dark"), "#5a3fd9");
|
|
84
|
+
assert.equal(providerColor("codebuddy-code", "light"), providerColor("codebuddy", "light"));
|
|
85
|
+
assert.equal(providerColor("gemini", "light"), "#8E75B2");
|
|
86
|
+
assert.equal(providerColor("cline", "light"), "#9663F1");
|
|
87
|
+
assert.equal(providerColor("minimax-code", "light"), providerColor("minimax", "light"));
|
|
88
|
+
assert.equal(providerColor("codewhale", "dark"), "#345c9d");
|
|
89
|
+
assert.notEqual(providerColor("omp", "light"), providerColor("pi", "light"));
|
|
90
|
+
assert.notEqual(providerColor("claude", "light"), providerColor("claude", "dark"));
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
test("creationProviderColors keeps brand colours even for the window leader", () => {
|
|
94
|
+
const map = creationProviderColors(["claude", "codex", "cursor"], "light");
|
|
95
|
+
assert.equal(map.get("claude"), providerColor("claude", "light"));
|
|
96
|
+
assert.equal(map.get("codex"), providerColor("codex", "light"));
|
|
97
|
+
assert.equal(map.get("cursor"), providerColor("cursor", "light"));
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test("creationProviderColors uses soft palette when the leader has no brand colour", () => {
|
|
101
|
+
const map = creationProviderColors(["goose", "claude", "codex"], "light");
|
|
102
|
+
assert.equal(map.get("goose"), providerColor("goose", "light"));
|
|
103
|
+
assert.equal(brandColor("goose", "light"), null);
|
|
104
|
+
assert.equal(map.get("claude"), providerColor("claude", "light"));
|
|
105
|
+
assert.equal(map.get("codex"), providerColor("codex", "light"));
|
|
29
106
|
});
|
|
30
107
|
|
|
31
|
-
test("
|
|
32
|
-
const
|
|
33
|
-
assert.
|
|
34
|
-
assert.
|
|
108
|
+
test("creationProviderColors with a single branded provider keeps the brand", () => {
|
|
109
|
+
const map = creationProviderColors(["pi"], "dark");
|
|
110
|
+
assert.equal(map.get("pi"), providerColor("pi", "dark"));
|
|
111
|
+
assert.equal(map.size, 1);
|
|
35
112
|
});
|
|
113
|
+
|
|
114
|
+
test("creationProviderColors with a single unbranded provider uses soft palette", () => {
|
|
115
|
+
const map = creationProviderColors(["goose"], "dark");
|
|
116
|
+
assert.equal(map.get("goose"), providerColor("goose", "dark"));
|
|
117
|
+
assert.equal(brandColor("goose", "dark"), null);
|
|
118
|
+
assert.equal(map.size, 1);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
test("unknown future ACP ids fall back to soft CHART_PALETTE (stable hash)", () => {
|
|
122
|
+
assert.equal(brandColor("brand-new-acp-agent", "light"), null);
|
|
123
|
+
const a = providerColor("brand-new-acp-agent", "light");
|
|
124
|
+
const b = providerColor("brand-new-acp-agent", "light");
|
|
125
|
+
const other = providerColor("another-future-acp", "light");
|
|
126
|
+
assert.equal(a, b);
|
|
127
|
+
assert.match(a, /^#[0-9a-f]{6}$/i);
|
|
128
|
+
assert.notEqual(a, other);
|
|
129
|
+
});
|
package/client/rank-color.ts
CHANGED
|
@@ -1,5 +1,190 @@
|
|
|
1
|
+
import { brandProviderId, normalizeProvider } from "../shared/classify.ts";
|
|
2
|
+
|
|
3
|
+
export type ChartColorScheme = "light" | "dark";
|
|
4
|
+
|
|
1
5
|
type Rgb = [number, number, number];
|
|
2
6
|
|
|
7
|
+
/**
|
|
8
|
+
* Soft dashboard fills (Paseo identity-color family): low chroma, even luminance
|
|
9
|
+
* so stacked bars identify without shouting. Light UI uses the brighter set;
|
|
10
|
+
* dark UI uses the deeper set.
|
|
11
|
+
*/
|
|
12
|
+
const CHART_PALETTE: Record<ChartColorScheme, readonly string[]> = {
|
|
13
|
+
// Soft fills swapped vs surface: light UI uses the brighter set, dark UI the deeper set.
|
|
14
|
+
light: [
|
|
15
|
+
"#a392d5", // violet
|
|
16
|
+
"#6aa6ce", // sky
|
|
17
|
+
"#6cae96", // emerald
|
|
18
|
+
"#cc8f64", // orange
|
|
19
|
+
"#d87da3", // pink
|
|
20
|
+
"#9299d5", // indigo
|
|
21
|
+
"#6cacab", // teal
|
|
22
|
+
"#d88381", // red
|
|
23
|
+
"#b29d64", // amber
|
|
24
|
+
"#7ba1d5", // blue
|
|
25
|
+
"#9ab87a", // olive
|
|
26
|
+
"#b89ac8", // mauve
|
|
27
|
+
],
|
|
28
|
+
dark: [
|
|
29
|
+
"#6d49b5", // violet
|
|
30
|
+
"#3d6985", // sky
|
|
31
|
+
"#3e6e5d", // emerald
|
|
32
|
+
"#845838", // orange
|
|
33
|
+
"#974168", // pink
|
|
34
|
+
"#5251c2", // indigo
|
|
35
|
+
"#3e6d6c", // teal
|
|
36
|
+
"#9c4243", // red
|
|
37
|
+
"#716239", // amber
|
|
38
|
+
"#39649e", // blue
|
|
39
|
+
"#5a7a4a", // olive
|
|
40
|
+
"#7a5a8a", // mauve
|
|
41
|
+
],
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Known Paseo provider ids (builtin + ACP catalog + normalize aliases).
|
|
46
|
+
* Sources: `packages/protocol/src/provider-manifest.ts` builtins,
|
|
47
|
+
* `packages/app/src/data/acp-provider-catalog.ts`, and `normalizeProvider`
|
|
48
|
+
* aliases (`codebuddy-code` → `codebuddy`, `omp` → `pi`).
|
|
49
|
+
*/
|
|
50
|
+
const KNOWN_PROVIDER_IDS = [
|
|
51
|
+
"agoragentic-acp",
|
|
52
|
+
"amp-acp",
|
|
53
|
+
"auggie",
|
|
54
|
+
"autohand",
|
|
55
|
+
"claude",
|
|
56
|
+
"cline",
|
|
57
|
+
"codebuddy",
|
|
58
|
+
"codebuddy-code",
|
|
59
|
+
"codewhale",
|
|
60
|
+
"codex",
|
|
61
|
+
"copilot",
|
|
62
|
+
"cortex-code",
|
|
63
|
+
"corust-agent",
|
|
64
|
+
"crow-cli",
|
|
65
|
+
"cursor",
|
|
66
|
+
"deepagents",
|
|
67
|
+
"devin",
|
|
68
|
+
"dimcode",
|
|
69
|
+
"dirac",
|
|
70
|
+
"factory-droid",
|
|
71
|
+
"fast-agent",
|
|
72
|
+
"gemini",
|
|
73
|
+
"gjc",
|
|
74
|
+
"glm-acp-agent",
|
|
75
|
+
"goose",
|
|
76
|
+
"grok",
|
|
77
|
+
"hermes",
|
|
78
|
+
"junie",
|
|
79
|
+
"kilo",
|
|
80
|
+
"kimi",
|
|
81
|
+
"kiro",
|
|
82
|
+
"minimax",
|
|
83
|
+
"minimax-code",
|
|
84
|
+
"minion-code",
|
|
85
|
+
"mistral-vibe",
|
|
86
|
+
"nova",
|
|
87
|
+
"omp",
|
|
88
|
+
"opencode",
|
|
89
|
+
"pi",
|
|
90
|
+
"poolside",
|
|
91
|
+
"qoder",
|
|
92
|
+
"qwen-code",
|
|
93
|
+
"sigit",
|
|
94
|
+
"stakpak",
|
|
95
|
+
"traecli",
|
|
96
|
+
"vtcode",
|
|
97
|
+
] as const;
|
|
98
|
+
|
|
99
|
+
/** High-traffic providers get the first palette slots so stacks stay distinct. */
|
|
100
|
+
const PRIORITY_PROVIDERS = [
|
|
101
|
+
"claude",
|
|
102
|
+
"codex",
|
|
103
|
+
"cursor",
|
|
104
|
+
"opencode",
|
|
105
|
+
"pi",
|
|
106
|
+
"gemini",
|
|
107
|
+
"copilot",
|
|
108
|
+
"kiro",
|
|
109
|
+
"kimi",
|
|
110
|
+
"grok",
|
|
111
|
+
"goose",
|
|
112
|
+
"qwen-code",
|
|
113
|
+
] as const;
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Official-ish brand accents for providers with a known chromatic identity.
|
|
117
|
+
* Always preferred over the soft chart palette when present. Providers without
|
|
118
|
+
* an entry (and any future ACP id Paseo adds) fall through to CHART_PALETTE.
|
|
119
|
+
*/
|
|
120
|
+
const PROVIDER_BRAND: Record<string, Record<ChartColorScheme, string>> = {
|
|
121
|
+
// Anthropic coral — dark is a step deeper than light
|
|
122
|
+
claude: { light: "#d97757", dark: "#c46845" },
|
|
123
|
+
// Codex brand blue
|
|
124
|
+
codex: { light: "#4d9eef", dark: "#0169cc" },
|
|
125
|
+
// Cursor orange — light softened vs neon brand #f54e00 for chart fills
|
|
126
|
+
cursor: { light: "#d94816", dark: "#b83900" },
|
|
127
|
+
// OpenCode blue — dark is a step deeper than light
|
|
128
|
+
opencode: { light: "#74a2ec", dark: "#5a86d4" },
|
|
129
|
+
// Pi
|
|
130
|
+
pi: { light: "#f1be58", dark: "#4d9abf" },
|
|
131
|
+
// Oh My Pi — light is a step brighter than dark
|
|
132
|
+
omp: { light: "#9a90f5", dark: "#7f73f2" },
|
|
133
|
+
// GitHub Copilot
|
|
134
|
+
copilot: { light: "#5fed83", dark: "#077124" },
|
|
135
|
+
// CodeBuddy purple — dark is a step deeper than light
|
|
136
|
+
codebuddy: { light: "#6c4dff", dark: "#5a3fd9" },
|
|
137
|
+
|
|
138
|
+
// --- High-confidence ACP / catalog brands ---
|
|
139
|
+
cline: { light: "#9663F1", dark: "#7b51c6" },
|
|
140
|
+
kiro: { light: "#9046FF", dark: "#7639d1" },
|
|
141
|
+
kilo: { light: "#F8F676", dark: "#cbca61" },
|
|
142
|
+
"factory-droid": { light: "#EF6F2E", dark: "#c45b26" },
|
|
143
|
+
"mistral-vibe": { light: "#FA520F", dark: "#cd430c" },
|
|
144
|
+
"qwen-code": { light: "#615CED", dark: "#504bc2" },
|
|
145
|
+
traecli: { light: "#32F08C", dark: "#29c573" },
|
|
146
|
+
poolside: { light: "#4137FF", dark: "#352dd1" },
|
|
147
|
+
minimax: { light: "#FF6452", dark: "#d15243" },
|
|
148
|
+
"minimax-code": { light: "#FF6452", dark: "#d15243" },
|
|
149
|
+
junie: { light: "#47E054", dark: "#3ab845" },
|
|
150
|
+
kimi: { light: "#007CFF", dark: "#0066d1" },
|
|
151
|
+
gemini: { light: "#8E75B2", dark: "#746092" },
|
|
152
|
+
"cortex-code": { light: "#29B5E8", dark: "#2294be" },
|
|
153
|
+
deepagents: { light: "#7FC8FF", dark: "#68a4d1" },
|
|
154
|
+
"glm-acp-agent": { light: "#3859FF", dark: "#2e49d1" },
|
|
155
|
+
"amp-acp": { light: "#F34E3F", dark: "#c74034" },
|
|
156
|
+
hermes: { light: "#0000F2", dark: "#0000c6" },
|
|
157
|
+
qoder: { light: "#8B5CF6", dark: "#724bca" },
|
|
158
|
+
auggie: { light: "#1AA049", dark: "#15833c" },
|
|
159
|
+
gjc: { light: "#FF6A3D", dark: "#d15732" },
|
|
160
|
+
dirac: { light: "#F59E0B", dark: "#c98209" },
|
|
161
|
+
devin: { light: "#31B388", dark: "#289370" },
|
|
162
|
+
|
|
163
|
+
// --- Medium-confidence (favicon / weak site sample) ---
|
|
164
|
+
codewhale: { light: "#4070C0", dark: "#345c9d" },
|
|
165
|
+
nova: { light: "#222F3E", dark: "#1c2733" },
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
/** Normalized id → palette slot (aliases that normalize together share a slot). */
|
|
169
|
+
const PROVIDER_SLOT = (() => {
|
|
170
|
+
const slots = new Map<string, number>();
|
|
171
|
+
let next = 0;
|
|
172
|
+
const claim = (raw: string) => {
|
|
173
|
+
const id = normalizeProvider(raw);
|
|
174
|
+
if (slots.has(id)) return;
|
|
175
|
+
slots.set(id, next);
|
|
176
|
+
next += 1;
|
|
177
|
+
};
|
|
178
|
+
for (const id of PRIORITY_PROVIDERS) claim(id);
|
|
179
|
+
for (const id of [...KNOWN_PROVIDER_IDS].sort()) claim(id);
|
|
180
|
+
for (const raw of KNOWN_PROVIDER_IDS) {
|
|
181
|
+
const id = normalizeProvider(raw);
|
|
182
|
+
const slot = slots.get(id);
|
|
183
|
+
if (slot != null) slots.set(raw, slot);
|
|
184
|
+
}
|
|
185
|
+
return slots;
|
|
186
|
+
})();
|
|
187
|
+
|
|
3
188
|
function parseHex(value: string): Rgb | null {
|
|
4
189
|
const hex = value.trim().replace(/^#/, "");
|
|
5
190
|
const full = hex.length === 3 ? hex.split("").map((c) => c + c).join("") : hex;
|
|
@@ -11,59 +196,87 @@ function parseHex(value: string): Rgb | null {
|
|
|
11
196
|
];
|
|
12
197
|
}
|
|
13
198
|
|
|
14
|
-
function
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
const bb = b / 255;
|
|
18
|
-
const max = Math.max(rr, gg, bb);
|
|
19
|
-
const min = Math.min(rr, gg, bb);
|
|
20
|
-
const lightness = (max + min) / 2;
|
|
21
|
-
if (max === min) return [0, 0, lightness];
|
|
22
|
-
const delta = max - min;
|
|
23
|
-
const saturation = lightness > 0.5 ? delta / (2 - max - min) : delta / (max + min);
|
|
24
|
-
const hue =
|
|
25
|
-
max === rr
|
|
26
|
-
? ((gg - bb) / delta + (gg < bb ? 6 : 0)) / 6
|
|
27
|
-
: max === gg
|
|
28
|
-
? ((bb - rr) / delta + 2) / 6
|
|
29
|
-
: ((rr - gg) / delta + 4) / 6;
|
|
30
|
-
return [hue * 360, saturation, lightness];
|
|
199
|
+
function channelLuminance(channel: number): number {
|
|
200
|
+
const c = channel / 255;
|
|
201
|
+
return c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
|
|
31
202
|
}
|
|
32
203
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
h < 60
|
|
40
|
-
? [chroma, second, 0]
|
|
41
|
-
: h < 120
|
|
42
|
-
? [second, chroma, 0]
|
|
43
|
-
: h < 180
|
|
44
|
-
? [0, chroma, second]
|
|
45
|
-
: h < 240
|
|
46
|
-
? [0, second, chroma]
|
|
47
|
-
: h < 300
|
|
48
|
-
? [second, 0, chroma]
|
|
49
|
-
: [chroma, 0, second];
|
|
50
|
-
return `#${[r, g, b]
|
|
51
|
-
.map((value) => Math.round((value + offset) * 255).toString(16).padStart(2, "0"))
|
|
52
|
-
.join("")}`;
|
|
204
|
+
/** WCAG relative luminance of a hex surface (0 = black, 1 = white). */
|
|
205
|
+
function relativeLuminance(hex: string): number | null {
|
|
206
|
+
const rgb = parseHex(hex);
|
|
207
|
+
if (!rgb) return null;
|
|
208
|
+
const [r, g, b] = rgb;
|
|
209
|
+
return 0.2126 * channelLuminance(r) + 0.7152 * channelLuminance(g) + 0.0722 * channelLuminance(b);
|
|
53
210
|
}
|
|
54
211
|
|
|
55
|
-
|
|
212
|
+
/**
|
|
213
|
+
* Infer light/dark chart palette from the theme surface.
|
|
214
|
+
* PluginTheme does not expose `appearance` / `colorScheme` (only `colors`), so
|
|
215
|
+
* luminance of `surface0` is the reliable signal.
|
|
216
|
+
*/
|
|
217
|
+
export function chartColorScheme(surface0: string): ChartColorScheme {
|
|
218
|
+
const luminance = relativeLuminance(surface0);
|
|
219
|
+
if (luminance == null) return "dark";
|
|
220
|
+
return luminance > 0.45 ? "light" : "dark";
|
|
221
|
+
}
|
|
56
222
|
|
|
57
|
-
|
|
58
|
-
const rgb = parseHex(accent);
|
|
59
|
-
if (!rgb) return accent;
|
|
223
|
+
function hashKey(key: string): number {
|
|
60
224
|
let hash = 0;
|
|
61
|
-
for (let i = 0; i <
|
|
62
|
-
hash = (hash * 31 +
|
|
225
|
+
for (let i = 0; i < key.length; i += 1) {
|
|
226
|
+
hash = (hash * 31 + key.charCodeAt(i)) >>> 0;
|
|
227
|
+
}
|
|
228
|
+
return hash;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function paletteColor(slot: number, scheme: ChartColorScheme): string {
|
|
232
|
+
const palette = CHART_PALETTE[scheme];
|
|
233
|
+
return palette[((slot % palette.length) + palette.length) % palette.length]!;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Stable colour for a provider id:
|
|
238
|
+
* 1. `PROVIDER_BRAND` when present
|
|
239
|
+
* 2. else soft `CHART_PALETTE` — known ids get a reserved slot; unknown /
|
|
240
|
+
* future ACP ids hash into the same palette (no theme accent, no invented brands)
|
|
241
|
+
*/
|
|
242
|
+
export function providerColor(provider: string, scheme: ChartColorScheme): string {
|
|
243
|
+
const brand = brandColor(provider, scheme);
|
|
244
|
+
if (brand) return brand;
|
|
245
|
+
const id = normalizeProvider(provider);
|
|
246
|
+
const known = PROVIDER_SLOT.get(id) ?? PROVIDER_SLOT.get(provider.toLowerCase());
|
|
247
|
+
if (known != null) return paletteColor(known, scheme);
|
|
248
|
+
return paletteColor(hashKey(id), scheme);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** Brand fill for a known provider, or `null` when it falls back to the soft palette. */
|
|
252
|
+
export function brandColor(provider: string, scheme: ChartColorScheme): string | null {
|
|
253
|
+
return PROVIDER_BRAND[brandProviderId(provider)]?.[scheme] ?? null;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Histogram stack colours: branded providers keep brand accents; everything
|
|
258
|
+
* else (unbranded catalog + future ACP ids) uses soft `CHART_PALETTE`.
|
|
259
|
+
*/
|
|
260
|
+
export function creationProviderColors(
|
|
261
|
+
rankedProviders: readonly string[],
|
|
262
|
+
scheme: ChartColorScheme,
|
|
263
|
+
): Map<string, string> {
|
|
264
|
+
const colors = new Map<string, string>();
|
|
265
|
+
for (const provider of rankedProviders) {
|
|
266
|
+
colors.set(provider, providerColor(provider, scheme));
|
|
63
267
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
268
|
+
return colors;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Soft colour for MCP servers / arbitrary keys — same palette as providers,
|
|
273
|
+
* hash-mapped (no 1:1 name table).
|
|
274
|
+
*/
|
|
275
|
+
export function entityColor(key: string, scheme: ChartColorScheme): string {
|
|
276
|
+
return paletteColor(hashKey(key), scheme);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** Test / docs helper: how many unique normalized provider slots we reserve. */
|
|
280
|
+
export function knownProviderSlotCount(): number {
|
|
281
|
+
return new Set(KNOWN_PROVIDER_IDS.map((id) => normalizeProvider(id))).size;
|
|
69
282
|
}
|
package/client/usage-stats.tsx
CHANGED
|
@@ -1,4 +1,111 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useState } from "react";
|
|
2
|
+
import { Text, View, type TextStyle } from "react-native";
|
|
3
|
+
import { fitFontSize } from "./fit-text.ts";
|
|
4
|
+
|
|
5
|
+
/** Hidden measuring box wide enough to never clamp the text (051). */
|
|
6
|
+
const MEASURE_WIDTH = 1000;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Single-line text that shrinks instead of wrapping (051). KPI tiles are
|
|
10
|
+
* ~130px wide, so 18px values like "Cursor · 61%" used to wrap onto two lines
|
|
11
|
+
* and push the labels out of line. The hidden copy measures the intrinsic width
|
|
12
|
+
* at `base` (an unconstrained box, since a constrained one reports the clamped
|
|
13
|
+
* width), then the visible text is scaled to fit. `numberOfLines={1}` stays as
|
|
14
|
+
* the hard guard: a bad measurement can only ellipsize, never wrap.
|
|
15
|
+
*/
|
|
16
|
+
function FitText({ text, available, base, min, lineHeight, color, weight, tabular }: {
|
|
17
|
+
text: string;
|
|
18
|
+
available: number;
|
|
19
|
+
base: number;
|
|
20
|
+
min?: number;
|
|
21
|
+
lineHeight: number;
|
|
22
|
+
color: string;
|
|
23
|
+
weight?: "500";
|
|
24
|
+
tabular?: boolean;
|
|
25
|
+
}) {
|
|
26
|
+
// The hidden copy renders at `fontSize` and reports its width; the fit is
|
|
27
|
+
// derived from that single measurement, so a tile resize re-fits without
|
|
28
|
+
// needing a new measurement.
|
|
29
|
+
const [measured, setMeasured] = useState({ size: base, width: 0 });
|
|
30
|
+
const fontSize = fitFontSize(measured, available, { base, min });
|
|
31
|
+
const style: TextStyle = {
|
|
32
|
+
color,
|
|
33
|
+
fontSize,
|
|
34
|
+
lineHeight,
|
|
35
|
+
fontWeight: weight,
|
|
36
|
+
fontVariant: tabular ? ["tabular-nums"] : undefined,
|
|
37
|
+
};
|
|
38
|
+
return (
|
|
39
|
+
<>
|
|
40
|
+
<View
|
|
41
|
+
pointerEvents="none"
|
|
42
|
+
style={{ position: "absolute", left: 0, top: 0, width: 0, height: 0, overflow: "hidden" }}
|
|
43
|
+
>
|
|
44
|
+
<View style={{ position: "absolute", left: 0, top: 0, width: MEASURE_WIDTH, flexDirection: "row", opacity: 0 }}>
|
|
45
|
+
<Text
|
|
46
|
+
numberOfLines={1}
|
|
47
|
+
onLayout={(event) =>
|
|
48
|
+
setMeasured({ size: fontSize, width: event.nativeEvent.layout.width })
|
|
49
|
+
}
|
|
50
|
+
style={{ ...style, flexShrink: 0 }}
|
|
51
|
+
>
|
|
52
|
+
{text}
|
|
53
|
+
</Text>
|
|
54
|
+
</View>
|
|
55
|
+
</View>
|
|
56
|
+
<Text numberOfLines={1} style={style}>
|
|
57
|
+
{text}
|
|
58
|
+
</Text>
|
|
59
|
+
</>
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function StatTile({ item, colors, dense, compact, bordered }: {
|
|
64
|
+
item: { label: string; value: string };
|
|
65
|
+
colors: { border: string; foreground: string; foregroundMuted: string };
|
|
66
|
+
dense: boolean;
|
|
67
|
+
compact: boolean;
|
|
68
|
+
bordered: boolean;
|
|
69
|
+
}) {
|
|
70
|
+
const [width, setWidth] = useState(0);
|
|
71
|
+
const padding = dense ? 8 : 12;
|
|
72
|
+
const available = Math.max(0, width - padding * 2);
|
|
73
|
+
return (
|
|
74
|
+
<View
|
|
75
|
+
onLayout={(event) => setWidth(event.nativeEvent.layout.width)}
|
|
76
|
+
style={{
|
|
77
|
+
position: "relative",
|
|
78
|
+
flexGrow: 1,
|
|
79
|
+
flexBasis: dense ? 84 : compact ? 100 : 120,
|
|
80
|
+
minWidth: 0,
|
|
81
|
+
alignItems: "center",
|
|
82
|
+
paddingHorizontal: padding,
|
|
83
|
+
paddingVertical: dense ? 2 : 4,
|
|
84
|
+
gap: dense ? 4 : 6,
|
|
85
|
+
borderLeftWidth: bordered ? 1 : 0,
|
|
86
|
+
borderLeftColor: colors.border,
|
|
87
|
+
}}
|
|
88
|
+
>
|
|
89
|
+
<FitText
|
|
90
|
+
text={item.value}
|
|
91
|
+
available={available}
|
|
92
|
+
base={18}
|
|
93
|
+
lineHeight={22}
|
|
94
|
+
color={colors.foreground}
|
|
95
|
+
weight="500"
|
|
96
|
+
tabular
|
|
97
|
+
/>
|
|
98
|
+
<FitText
|
|
99
|
+
text={item.label}
|
|
100
|
+
available={available}
|
|
101
|
+
base={12}
|
|
102
|
+
min={10}
|
|
103
|
+
lineHeight={16}
|
|
104
|
+
color={colors.foregroundMuted}
|
|
105
|
+
/>
|
|
106
|
+
</View>
|
|
107
|
+
);
|
|
108
|
+
}
|
|
2
109
|
|
|
3
110
|
export function UsageStats({ items, colors, compact, dense }: {
|
|
4
111
|
items: ReadonlyArray<{ label: string; value: string }>;
|
|
@@ -10,10 +117,14 @@ export function UsageStats({ items, colors, compact, dense }: {
|
|
|
10
117
|
return (
|
|
11
118
|
<View style={{ flexDirection: "row", flexWrap: "wrap", borderWidth: 1, borderColor: colors.border, borderRadius: 20, paddingVertical: dense ? 6 : compact ? 8 : 18, rowGap: dense ? 10 : 16 }}>
|
|
12
119
|
{items.map((item, index) => (
|
|
13
|
-
<
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
120
|
+
<StatTile
|
|
121
|
+
key={item.label}
|
|
122
|
+
item={item}
|
|
123
|
+
colors={colors}
|
|
124
|
+
dense={dense === true}
|
|
125
|
+
compact={compact}
|
|
126
|
+
bordered={index > 0 && !compact}
|
|
127
|
+
/>
|
|
17
128
|
))}
|
|
18
129
|
</View>
|
|
19
130
|
);
|