@microvac/plugin-openui-cards 0.0.0-stage → 0.1.0-rc.22
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/LICENSE +22 -0
- package/lib/client.js +294 -0
- package/package.json +42 -4
- package/src/charts.tsx +175 -0
- package/src/client.tsx +513 -0
- package/src/index.js +52 -0
- package/src/library.js +240 -0
- package/src/microvac-ui.d.ts +63 -0
- package/src/validate.js +232 -0
- package/README.md +0 -3
package/package.json
CHANGED
|
@@ -1,6 +1,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@microvac/plugin-openui-cards",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0-rc.22",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"description": "Microvac plugin: rich decision and presentation cards written by agents in OpenUI Lang (ADR 0015).",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./src/index.js",
|
|
9
|
+
"./client": "./lib/client.js",
|
|
10
|
+
"./package.json": "./package.json"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"src",
|
|
14
|
+
"lib"
|
|
15
|
+
],
|
|
16
|
+
"microvac": {
|
|
17
|
+
"ui": "^1.4.0"
|
|
18
|
+
},
|
|
19
|
+
"dsh": {
|
|
20
|
+
"client": {
|
|
21
|
+
"platform": "web",
|
|
22
|
+
"inject": [],
|
|
23
|
+
"external": [
|
|
24
|
+
"@microvac/ui"
|
|
25
|
+
],
|
|
26
|
+
"immediately": true,
|
|
27
|
+
"minify": true
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@deepseek-ai/schemastery": "3.20.0-alpha.2",
|
|
32
|
+
"@openuidev/lang-core": "1.2.0",
|
|
33
|
+
"zod": "^4.6.6"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build:client": "node ../../scripts/microvac/build-client-plugin.mjs .",
|
|
37
|
+
"build:skill": "node scripts/generate-skill.mjs",
|
|
38
|
+
"typecheck": "tsc --noEmit"
|
|
39
|
+
},
|
|
40
|
+
"repository": {
|
|
41
|
+
"type": "git",
|
|
42
|
+
"url": "https://github.com/microvac-ai/microvac"
|
|
43
|
+
}
|
|
44
|
+
}
|
package/src/charts.tsx
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bar and line charts of the card library, in plain SVG (OpenUI's react-ui
|
|
3
|
+
* charts are not used: pre-1.0, d3 and zustand). They scale to the card's
|
|
4
|
+
* width, use the host's chart tokens and read well on a 400 px phone.
|
|
5
|
+
*/
|
|
6
|
+
import type { ReactNode } from "react";
|
|
7
|
+
|
|
8
|
+
export interface ChartSeries {
|
|
9
|
+
name: string;
|
|
10
|
+
values: number[];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface ChartProps {
|
|
14
|
+
labels: string[];
|
|
15
|
+
series: ChartSeries[];
|
|
16
|
+
title?: string | null;
|
|
17
|
+
unit?: string | null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const WIDTH = 360;
|
|
21
|
+
const HEIGHT = 200;
|
|
22
|
+
const PAD = { top: 12, right: 8, bottom: 28, left: 40 };
|
|
23
|
+
const COLORS = ["var(--chart-1)", "var(--chart-2)", "var(--chart-3)", "var(--chart-4)", "var(--chart-5)", "var(--primary)"];
|
|
24
|
+
|
|
25
|
+
function color(index: number) {
|
|
26
|
+
return COLORS[index % COLORS.length];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Rounded bounds and four ticks, zero included. */
|
|
30
|
+
export function scale(series: ChartSeries[]) {
|
|
31
|
+
const all = series.flatMap((item) => item.values).filter((value) => Number.isFinite(value));
|
|
32
|
+
const min = Math.min(0, ...all);
|
|
33
|
+
const max = Math.max(0, ...all);
|
|
34
|
+
const span = max - min || 1;
|
|
35
|
+
const step = niceStep(span / 4);
|
|
36
|
+
const low = Math.floor(min / step) * step;
|
|
37
|
+
const high = Math.ceil(max / step) * step || step;
|
|
38
|
+
const ticks: number[] = [];
|
|
39
|
+
for (let tick = low; tick <= high + step / 2; tick += step) ticks.push(Number(tick.toPrecision(12)));
|
|
40
|
+
return { low, high, ticks };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function niceStep(raw: number) {
|
|
44
|
+
const power = 10 ** Math.floor(Math.log10(raw));
|
|
45
|
+
const unit = raw / power;
|
|
46
|
+
return (unit <= 1 ? 1 : unit <= 2 ? 2 : unit <= 5 ? 5 : 10) * power;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function formatNumber(value: number) {
|
|
50
|
+
const size = Math.abs(value);
|
|
51
|
+
if (size >= 1e9) return `${Number((value / 1e9).toFixed(1))}G`;
|
|
52
|
+
if (size >= 1e6) return `${Number((value / 1e6).toFixed(1))}M`;
|
|
53
|
+
if (size >= 1e4) return `${Number((value / 1e3).toFixed(1))}k`;
|
|
54
|
+
return String(Number(value.toFixed(2)));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function Frame({ title, unit, series, children, ticks, y }: ChartProps & { children: ReactNode; ticks: number[]; y(value: number): number }) {
|
|
58
|
+
const plotWidth = WIDTH - PAD.left - PAD.right;
|
|
59
|
+
return (
|
|
60
|
+
<figure className="grid min-w-0 gap-1.5">
|
|
61
|
+
{title ? (
|
|
62
|
+
<figcaption className="text-sm font-medium">
|
|
63
|
+
{title}
|
|
64
|
+
{unit ? <span className="font-normal text-muted-foreground"> ({unit})</span> : null}
|
|
65
|
+
</figcaption>
|
|
66
|
+
) : null}
|
|
67
|
+
<svg viewBox={`0 0 ${WIDTH} ${HEIGHT}`} className="h-auto w-full text-muted-foreground" role="img" aria-label={title ?? "Chart"}>
|
|
68
|
+
{ticks.map((tick) => (
|
|
69
|
+
<g key={tick}>
|
|
70
|
+
<line x1={PAD.left} x2={PAD.left + plotWidth} y1={y(tick)} y2={y(tick)} stroke="currentColor" strokeOpacity={tick === 0 ? 0.6 : 0.2} />
|
|
71
|
+
<text x={PAD.left - 4} y={y(tick)} dy="0.32em" textAnchor="end" fontSize="10" fill="currentColor">
|
|
72
|
+
{formatNumber(tick)}
|
|
73
|
+
</text>
|
|
74
|
+
</g>
|
|
75
|
+
))}
|
|
76
|
+
{children}
|
|
77
|
+
</svg>
|
|
78
|
+
{series.length > 1 || !title ? (
|
|
79
|
+
<ul className="flex flex-wrap gap-x-3 gap-y-1 text-xs text-muted-foreground">
|
|
80
|
+
{series.map((item, index) => (
|
|
81
|
+
<li key={index} className="flex items-center gap-1.5">
|
|
82
|
+
<span aria-hidden="true" className="inline-block size-2.5 rounded-sm" style={{ background: color(index) }} />
|
|
83
|
+
{item.name}
|
|
84
|
+
</li>
|
|
85
|
+
))}
|
|
86
|
+
</ul>
|
|
87
|
+
) : null}
|
|
88
|
+
</figure>
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Up to one x label every `every` labels, so they do not overlap at 360 px. */
|
|
93
|
+
function labelEvery(count: number) {
|
|
94
|
+
return Math.max(1, Math.ceil(count / 8));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function shortLabel(label: string) {
|
|
98
|
+
return label.length > 10 ? `${label.slice(0, 9)}…` : label;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function BarChart(props: ChartProps) {
|
|
102
|
+
const { labels, series } = props;
|
|
103
|
+
const { low, high, ticks } = scale(series);
|
|
104
|
+
const plotWidth = WIDTH - PAD.left - PAD.right;
|
|
105
|
+
const plotHeight = HEIGHT - PAD.top - PAD.bottom;
|
|
106
|
+
const y = (value: number) => PAD.top + ((high - value) / (high - low)) * plotHeight;
|
|
107
|
+
const group = plotWidth / labels.length;
|
|
108
|
+
const bar = Math.max(1, (group * 0.8) / Math.max(1, series.length));
|
|
109
|
+
const every = labelEvery(labels.length);
|
|
110
|
+
return (
|
|
111
|
+
<Frame {...props} ticks={ticks} y={y}>
|
|
112
|
+
{labels.map((label, labelIndex) => (
|
|
113
|
+
<g key={labelIndex}>
|
|
114
|
+
{series.map((item, seriesIndex) => {
|
|
115
|
+
const value = item.values[labelIndex] ?? 0;
|
|
116
|
+
const top = y(Math.max(0, value));
|
|
117
|
+
return (
|
|
118
|
+
<rect
|
|
119
|
+
key={seriesIndex}
|
|
120
|
+
x={PAD.left + labelIndex * group + group * 0.1 + seriesIndex * bar}
|
|
121
|
+
y={top}
|
|
122
|
+
width={bar}
|
|
123
|
+
height={Math.max(0, Math.abs(y(value) - y(0)))}
|
|
124
|
+
fill={color(seriesIndex)}
|
|
125
|
+
>
|
|
126
|
+
<title>{`${item.name} · ${label}: ${formatNumber(value)}${props.unit ? ` ${props.unit}` : ""}`}</title>
|
|
127
|
+
</rect>
|
|
128
|
+
);
|
|
129
|
+
})}
|
|
130
|
+
{labelIndex % every === 0 ? (
|
|
131
|
+
<text x={PAD.left + labelIndex * group + group / 2} y={HEIGHT - 10} textAnchor="middle" fontSize="10" fill="currentColor">
|
|
132
|
+
{shortLabel(label)}
|
|
133
|
+
</text>
|
|
134
|
+
) : null}
|
|
135
|
+
</g>
|
|
136
|
+
))}
|
|
137
|
+
</Frame>
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function LineChart(props: ChartProps) {
|
|
142
|
+
const { labels, series } = props;
|
|
143
|
+
const { low, high, ticks } = scale(series);
|
|
144
|
+
const plotWidth = WIDTH - PAD.left - PAD.right;
|
|
145
|
+
const plotHeight = HEIGHT - PAD.top - PAD.bottom;
|
|
146
|
+
const y = (value: number) => PAD.top + ((high - value) / (high - low)) * plotHeight;
|
|
147
|
+
const x = (index: number) => PAD.left + (labels.length === 1 ? plotWidth / 2 : (index / (labels.length - 1)) * plotWidth);
|
|
148
|
+
const every = labelEvery(labels.length);
|
|
149
|
+
return (
|
|
150
|
+
<Frame {...props} ticks={ticks} y={y}>
|
|
151
|
+
{series.map((item, seriesIndex) => (
|
|
152
|
+
<g key={seriesIndex}>
|
|
153
|
+
<polyline
|
|
154
|
+
fill="none"
|
|
155
|
+
stroke={color(seriesIndex)}
|
|
156
|
+
strokeWidth="2"
|
|
157
|
+
points={item.values.map((value, index) => `${x(index)},${y(value)}`).join(" ")}
|
|
158
|
+
/>
|
|
159
|
+
{item.values.map((value, index) => (
|
|
160
|
+
<circle key={index} cx={x(index)} cy={y(value)} r="2.5" fill={color(seriesIndex)}>
|
|
161
|
+
<title>{`${item.name} · ${labels[index] ?? ""}: ${formatNumber(value)}${props.unit ? ` ${props.unit}` : ""}`}</title>
|
|
162
|
+
</circle>
|
|
163
|
+
))}
|
|
164
|
+
</g>
|
|
165
|
+
))}
|
|
166
|
+
{labels.map((label, index) =>
|
|
167
|
+
index % every === 0 ? (
|
|
168
|
+
<text key={index} x={x(index)} y={HEIGHT - 10} textAnchor="middle" fontSize="10" fill="currentColor">
|
|
169
|
+
{shortLabel(label)}
|
|
170
|
+
</text>
|
|
171
|
+
) : null,
|
|
172
|
+
)}
|
|
173
|
+
</Frame>
|
|
174
|
+
);
|
|
175
|
+
}
|