@absolutejs/ai 0.0.32 → 0.0.34
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/ai/index.js +54 -2
- package/dist/ai/index.js.map +3 -3
- package/dist/ai/ui/index.js +604 -0
- package/dist/ai/ui/index.js.map +12 -0
- package/dist/src/ai/ui/catalog.d.ts +25 -0
- package/dist/src/ai/ui/index.d.ts +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,604 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __name = (target, name) => {
|
|
5
|
+
Object.defineProperty(target, "name", {
|
|
6
|
+
value: name,
|
|
7
|
+
enumerable: false,
|
|
8
|
+
configurable: true
|
|
9
|
+
});
|
|
10
|
+
return target;
|
|
11
|
+
};
|
|
12
|
+
var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name);
|
|
13
|
+
var __typeError = (msg) => {
|
|
14
|
+
throw TypeError(msg);
|
|
15
|
+
};
|
|
16
|
+
var __defNormalProp = (obj, key, value) => (key in obj) ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
17
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
18
|
+
var __privateIn = (member, obj) => Object(obj) !== obj ? __typeError('Cannot use the "in" operator on this value') : member.has(obj);
|
|
19
|
+
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
20
|
+
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
|
|
21
|
+
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
|
|
22
|
+
var __decoratorStart = (base) => [, , , __create(base?.[__knownSymbol("metadata")] ?? null)];
|
|
23
|
+
var __decoratorStrings = ["class", "method", "getter", "setter", "accessor", "field", "value", "get", "set"];
|
|
24
|
+
var __expectFn = (fn) => fn !== undefined && typeof fn !== "function" ? __typeError("Function expected") : fn;
|
|
25
|
+
var __decoratorContext = (kind, name, done, metadata, fns) => ({
|
|
26
|
+
kind: __decoratorStrings[kind],
|
|
27
|
+
name,
|
|
28
|
+
metadata,
|
|
29
|
+
addInitializer: (fn) => done._ ? __typeError("Already initialized") : fns.push(__expectFn(fn || null))
|
|
30
|
+
});
|
|
31
|
+
var __decoratorMetadata = (array, target) => __defNormalProp(target, __knownSymbol("metadata"), array[3]);
|
|
32
|
+
var __runInitializers = (array, flags, self, value) => {
|
|
33
|
+
for (var i = 0, fns = array[flags >> 1], n = fns && fns.length;i < n; i++)
|
|
34
|
+
flags & 1 ? fns[i].call(self) : value = fns[i].call(self, value);
|
|
35
|
+
return value;
|
|
36
|
+
};
|
|
37
|
+
var __decorateElement = (array, flags, name, decorators, target, extra) => {
|
|
38
|
+
var fn, it, done, ctx, access, k = flags & 7, s = !!(flags & 8), p = !!(flags & 16);
|
|
39
|
+
var j = k > 3 ? array.length + 1 : k ? s ? 1 : 2 : 0, key = __decoratorStrings[k + 5];
|
|
40
|
+
var initializers = k > 3 && (array[j - 1] = []), extraInitializers = array[j] || (array[j] = []);
|
|
41
|
+
var desc = k && (!p && !s && (target = target.prototype), k < 5 && (k > 3 || !p) && __getOwnPropDesc(k < 4 ? target : {
|
|
42
|
+
get [name]() {
|
|
43
|
+
return __privateGet(this, extra);
|
|
44
|
+
},
|
|
45
|
+
set [name](x) {
|
|
46
|
+
__privateSet(this, extra, x);
|
|
47
|
+
}
|
|
48
|
+
}, name));
|
|
49
|
+
k ? p && k < 4 && __name(extra, (k > 2 ? "set " : k > 1 ? "get " : "") + name) : __name(target, name);
|
|
50
|
+
for (var i = decorators.length - 1;i >= 0; i--) {
|
|
51
|
+
ctx = __decoratorContext(k, name, done = {}, array[3], extraInitializers);
|
|
52
|
+
if (k) {
|
|
53
|
+
ctx.static = s, ctx.private = p, access = ctx.access = { has: p ? (x) => __privateIn(target, x) : (x) => (name in x) };
|
|
54
|
+
if (k ^ 3)
|
|
55
|
+
access.get = p ? (x) => (k ^ 1 ? __privateGet : __privateMethod)(x, target, k ^ 4 ? extra : desc.get) : (x) => x[name];
|
|
56
|
+
if (k > 2)
|
|
57
|
+
access.set = p ? (x, y) => __privateSet(x, target, y, k ^ 4 ? extra : desc.set) : (x, y) => x[name] = y;
|
|
58
|
+
}
|
|
59
|
+
it = (0, decorators[i])(k ? k < 4 ? p ? extra : desc[key] : k > 4 ? undefined : { get: desc.get, set: desc.set } : target, ctx);
|
|
60
|
+
done._ = 1;
|
|
61
|
+
if (k ^ 4 || it === undefined)
|
|
62
|
+
__expectFn(it) && (k > 4 ? initializers.unshift(it) : k ? p ? extra = it : desc[key] = it : target = it);
|
|
63
|
+
else if (typeof it !== "object" || it === null)
|
|
64
|
+
__typeError("Object expected");
|
|
65
|
+
else
|
|
66
|
+
__expectFn(fn = it.get) && (desc.get = fn), __expectFn(fn = it.set) && (desc.set = fn), __expectFn(fn = it.init) && initializers.unshift(fn);
|
|
67
|
+
}
|
|
68
|
+
return k || __decoratorMetadata(array, target), desc && __defProp(target, name, desc), p ? k ^ 4 ? extra : desc : target;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
// src/ai/ui/uiCards.ts
|
|
72
|
+
var createUiCards = (definitions) => {
|
|
73
|
+
const byName = new Map(definitions.map((definition) => [definition.name, definition]));
|
|
74
|
+
const tools = Object.fromEntries(definitions.map((definition) => [
|
|
75
|
+
definition.name,
|
|
76
|
+
{
|
|
77
|
+
description: definition.description,
|
|
78
|
+
handler: () => definition.ack,
|
|
79
|
+
input: definition.inputSchema
|
|
80
|
+
}
|
|
81
|
+
]));
|
|
82
|
+
const collect = (calls) => {
|
|
83
|
+
const events = [];
|
|
84
|
+
for (const call of calls) {
|
|
85
|
+
const definition = byName.get(call.name);
|
|
86
|
+
if (!definition)
|
|
87
|
+
continue;
|
|
88
|
+
const data = definition.parse(call.input);
|
|
89
|
+
if (data !== null)
|
|
90
|
+
events.push({ card: definition.name, data });
|
|
91
|
+
}
|
|
92
|
+
return events;
|
|
93
|
+
};
|
|
94
|
+
return { collect, has: (name) => byName.has(name), tools };
|
|
95
|
+
};
|
|
96
|
+
// src/ai/ui/catalog.ts
|
|
97
|
+
var CHART_TYPES = ["bar", "line", "donut"];
|
|
98
|
+
var CHART_MAX_SERIES = 8;
|
|
99
|
+
var CHART_MAX_POINTS = 24;
|
|
100
|
+
var TABLE_MAX_COLUMNS = 8;
|
|
101
|
+
var TABLE_MAX_ROWS = 30;
|
|
102
|
+
var STAT_TILES_MAX = 6;
|
|
103
|
+
var UI_ACTIONS_MAX = 3;
|
|
104
|
+
var LABEL_MAX_CHARS = 80;
|
|
105
|
+
var TITLE_MAX_CHARS = 120;
|
|
106
|
+
var CELL_MAX_CHARS = 160;
|
|
107
|
+
var UNIT_MAX_CHARS = 8;
|
|
108
|
+
var ACTION_LABEL_MAX_CHARS = 40;
|
|
109
|
+
var ACTION_TOOL_PATTERN = /^[a-z][a-z0-9_]{1,63}$/;
|
|
110
|
+
var isRecord = (value) => typeof value === "object" && value !== null;
|
|
111
|
+
var cleanString = (value, maxChars) => typeof value === "string" && value.trim().length > 0 ? value.trim().slice(0, maxChars) : null;
|
|
112
|
+
var cleanStringArray = (value, maxItems, maxChars) => {
|
|
113
|
+
if (!Array.isArray(value) || value.length === 0)
|
|
114
|
+
return null;
|
|
115
|
+
const cleaned = [];
|
|
116
|
+
for (const entry of value.slice(0, maxItems)) {
|
|
117
|
+
const text = cleanString(entry, maxChars);
|
|
118
|
+
cleaned.push(text ?? "");
|
|
119
|
+
}
|
|
120
|
+
return cleaned;
|
|
121
|
+
};
|
|
122
|
+
var cleanNumberArray = (value, maxItems) => {
|
|
123
|
+
if (!Array.isArray(value) || value.length === 0)
|
|
124
|
+
return null;
|
|
125
|
+
const cleaned = [];
|
|
126
|
+
for (const entry of value.slice(0, maxItems)) {
|
|
127
|
+
if (typeof entry !== "number" || !Number.isFinite(entry))
|
|
128
|
+
return null;
|
|
129
|
+
cleaned.push(entry);
|
|
130
|
+
}
|
|
131
|
+
return cleaned;
|
|
132
|
+
};
|
|
133
|
+
var parseUiActions = (value) => {
|
|
134
|
+
if (!Array.isArray(value) || value.length === 0)
|
|
135
|
+
return;
|
|
136
|
+
const actions = [];
|
|
137
|
+
for (const raw of value.slice(0, UI_ACTIONS_MAX)) {
|
|
138
|
+
if (!isRecord(raw))
|
|
139
|
+
continue;
|
|
140
|
+
const label = cleanString(raw.label, ACTION_LABEL_MAX_CHARS);
|
|
141
|
+
const tool = typeof raw.tool === "string" && ACTION_TOOL_PATTERN.test(raw.tool) ? raw.tool : null;
|
|
142
|
+
if (!label || !tool || !isRecord(raw.input))
|
|
143
|
+
continue;
|
|
144
|
+
actions.push({ input: raw.input, label, tool });
|
|
145
|
+
}
|
|
146
|
+
return actions.length > 0 ? actions : undefined;
|
|
147
|
+
};
|
|
148
|
+
var ACTIONS_SCHEMA = {
|
|
149
|
+
description: "Optional action buttons under the card (max 3): each invokes one of YOUR tools with a fully-resolved input when the member clicks it. Use real ids you looked up — never placeholders. Approval-gated tools queue their normal approval card.",
|
|
150
|
+
items: {
|
|
151
|
+
properties: {
|
|
152
|
+
input: {
|
|
153
|
+
description: "The exact tool input to send on click",
|
|
154
|
+
type: "object"
|
|
155
|
+
},
|
|
156
|
+
label: {
|
|
157
|
+
description: 'Short button label, e.g. "Create follow-up task"',
|
|
158
|
+
type: "string"
|
|
159
|
+
},
|
|
160
|
+
tool: { description: "The tool name to invoke", type: "string" }
|
|
161
|
+
},
|
|
162
|
+
required: ["label", "tool", "input"],
|
|
163
|
+
type: "object"
|
|
164
|
+
},
|
|
165
|
+
type: "array"
|
|
166
|
+
};
|
|
167
|
+
var parseChartSpec = (input) => {
|
|
168
|
+
if (!isRecord(input))
|
|
169
|
+
return null;
|
|
170
|
+
const type = CHART_TYPES.find((entry) => entry === input.type);
|
|
171
|
+
const title = cleanString(input.title, TITLE_MAX_CHARS);
|
|
172
|
+
const labels = cleanStringArray(input.labels, CHART_MAX_POINTS, LABEL_MAX_CHARS);
|
|
173
|
+
if (!type || !title || !labels)
|
|
174
|
+
return null;
|
|
175
|
+
if (!Array.isArray(input.series) || input.series.length === 0)
|
|
176
|
+
return null;
|
|
177
|
+
const series = [];
|
|
178
|
+
for (const raw of input.series.slice(0, CHART_MAX_SERIES)) {
|
|
179
|
+
if (!isRecord(raw))
|
|
180
|
+
return null;
|
|
181
|
+
const name = cleanString(raw.name, LABEL_MAX_CHARS);
|
|
182
|
+
const values = cleanNumberArray(raw.values, CHART_MAX_POINTS);
|
|
183
|
+
if (!name || !values)
|
|
184
|
+
return null;
|
|
185
|
+
if (values.length !== labels.length)
|
|
186
|
+
return null;
|
|
187
|
+
series.push({ name, values });
|
|
188
|
+
}
|
|
189
|
+
if (type === "donut") {
|
|
190
|
+
const [only] = series;
|
|
191
|
+
if (series.length !== 1 || !only)
|
|
192
|
+
return null;
|
|
193
|
+
if (only.values.some((value) => value < 0))
|
|
194
|
+
return null;
|
|
195
|
+
}
|
|
196
|
+
const spec = { labels, series, title, type };
|
|
197
|
+
const unitPrefix = cleanString(input.unitPrefix, UNIT_MAX_CHARS);
|
|
198
|
+
const unitSuffix = cleanString(input.unitSuffix, UNIT_MAX_CHARS);
|
|
199
|
+
if (unitPrefix)
|
|
200
|
+
spec.unitPrefix = unitPrefix;
|
|
201
|
+
if (unitSuffix)
|
|
202
|
+
spec.unitSuffix = unitSuffix;
|
|
203
|
+
const actions = parseUiActions(input.actions);
|
|
204
|
+
if (actions)
|
|
205
|
+
spec.actions = actions;
|
|
206
|
+
return spec;
|
|
207
|
+
};
|
|
208
|
+
var parseTableSpec = (input) => {
|
|
209
|
+
if (!isRecord(input))
|
|
210
|
+
return null;
|
|
211
|
+
const columns = cleanStringArray(input.columns, TABLE_MAX_COLUMNS, LABEL_MAX_CHARS);
|
|
212
|
+
if (!columns)
|
|
213
|
+
return null;
|
|
214
|
+
if (!Array.isArray(input.rows) || input.rows.length === 0)
|
|
215
|
+
return null;
|
|
216
|
+
const rows = [];
|
|
217
|
+
for (const raw of input.rows.slice(0, TABLE_MAX_ROWS)) {
|
|
218
|
+
const cells = cleanStringArray(raw, TABLE_MAX_COLUMNS, CELL_MAX_CHARS);
|
|
219
|
+
if (!cells)
|
|
220
|
+
return null;
|
|
221
|
+
while (cells.length < columns.length)
|
|
222
|
+
cells.push("");
|
|
223
|
+
rows.push(cells.slice(0, columns.length));
|
|
224
|
+
}
|
|
225
|
+
const spec = { columns, rows };
|
|
226
|
+
const title = cleanString(input.title, TITLE_MAX_CHARS);
|
|
227
|
+
if (title)
|
|
228
|
+
spec.title = title;
|
|
229
|
+
const actions = parseUiActions(input.actions);
|
|
230
|
+
if (actions)
|
|
231
|
+
spec.actions = actions;
|
|
232
|
+
return spec;
|
|
233
|
+
};
|
|
234
|
+
var parseStatTilesSpec = (input) => {
|
|
235
|
+
if (!isRecord(input))
|
|
236
|
+
return null;
|
|
237
|
+
if (!Array.isArray(input.tiles) || input.tiles.length === 0)
|
|
238
|
+
return null;
|
|
239
|
+
const tiles = [];
|
|
240
|
+
for (const raw of input.tiles.slice(0, STAT_TILES_MAX)) {
|
|
241
|
+
if (!isRecord(raw))
|
|
242
|
+
return null;
|
|
243
|
+
const label = cleanString(raw.label, LABEL_MAX_CHARS);
|
|
244
|
+
const value = cleanString(raw.value, LABEL_MAX_CHARS);
|
|
245
|
+
if (!label || !value)
|
|
246
|
+
return null;
|
|
247
|
+
const tile = { label, value };
|
|
248
|
+
const delta = cleanString(raw.delta, LABEL_MAX_CHARS);
|
|
249
|
+
if (delta)
|
|
250
|
+
tile.delta = delta;
|
|
251
|
+
if (raw.deltaDirection === "up" || raw.deltaDirection === "down" || raw.deltaDirection === "flat") {
|
|
252
|
+
tile.deltaDirection = raw.deltaDirection;
|
|
253
|
+
}
|
|
254
|
+
tiles.push(tile);
|
|
255
|
+
}
|
|
256
|
+
const spec = { tiles };
|
|
257
|
+
const actions = parseUiActions(input.actions);
|
|
258
|
+
if (actions)
|
|
259
|
+
spec.actions = actions;
|
|
260
|
+
return spec;
|
|
261
|
+
};
|
|
262
|
+
var SERIES_SCHEMA = {
|
|
263
|
+
properties: {
|
|
264
|
+
name: { description: "Series name (shown in the legend)", type: "string" },
|
|
265
|
+
values: {
|
|
266
|
+
description: "One number per label, same order as labels",
|
|
267
|
+
items: { type: "number" },
|
|
268
|
+
type: "array"
|
|
269
|
+
}
|
|
270
|
+
},
|
|
271
|
+
required: ["name", "values"],
|
|
272
|
+
type: "object"
|
|
273
|
+
};
|
|
274
|
+
var chartCard = {
|
|
275
|
+
ack: "(chart rendered inline — do not repeat its numbers as text; add at most a 1-2 line takeaway)",
|
|
276
|
+
description: "Render a real chart inline in the chat from data you have (tool results, the conversation). Use whenever numbers COMPARE or TREND: revenue by partner (bar), pipeline over time (line), share of a whole (donut). Rules: bar/line take up to 8 series aligned to the same labels; donut takes exactly ONE series of non-negative values (one slice per label). Prefer a chart over a wall of numbers, but never invent data for it.",
|
|
277
|
+
inputSchema: {
|
|
278
|
+
properties: {
|
|
279
|
+
actions: ACTIONS_SCHEMA,
|
|
280
|
+
labels: {
|
|
281
|
+
description: "Category labels — x-axis for bar/line, slice names for donut (max 24)",
|
|
282
|
+
items: { type: "string" },
|
|
283
|
+
type: "array"
|
|
284
|
+
},
|
|
285
|
+
series: {
|
|
286
|
+
description: "Data series (max 8; donut exactly 1)",
|
|
287
|
+
items: SERIES_SCHEMA,
|
|
288
|
+
type: "array"
|
|
289
|
+
},
|
|
290
|
+
title: { description: "Short chart title", type: "string" },
|
|
291
|
+
type: { enum: [...CHART_TYPES], type: "string" },
|
|
292
|
+
unitPrefix: {
|
|
293
|
+
description: 'Prepended to values, e.g. "$"',
|
|
294
|
+
type: "string"
|
|
295
|
+
},
|
|
296
|
+
unitSuffix: {
|
|
297
|
+
description: 'Appended to values, e.g. "%"',
|
|
298
|
+
type: "string"
|
|
299
|
+
}
|
|
300
|
+
},
|
|
301
|
+
required: ["type", "title", "labels", "series"],
|
|
302
|
+
type: "object"
|
|
303
|
+
},
|
|
304
|
+
name: "render_chart",
|
|
305
|
+
parse: parseChartSpec
|
|
306
|
+
};
|
|
307
|
+
var tableCard = {
|
|
308
|
+
ack: "(table rendered inline — do not repeat its rows as text)",
|
|
309
|
+
description: "Render a compact data table inline in the chat (max 8 columns × 30 rows). Use for structured comparisons the member will scan — matches side by side, deal terms, task lists with dates. All cells are strings; format numbers yourself.",
|
|
310
|
+
inputSchema: {
|
|
311
|
+
properties: {
|
|
312
|
+
actions: ACTIONS_SCHEMA,
|
|
313
|
+
columns: {
|
|
314
|
+
description: "Column headers (max 8)",
|
|
315
|
+
items: { type: "string" },
|
|
316
|
+
type: "array"
|
|
317
|
+
},
|
|
318
|
+
rows: {
|
|
319
|
+
description: "Rows of cells, each aligned to columns (max 30)",
|
|
320
|
+
items: { items: { type: "string" }, type: "array" },
|
|
321
|
+
type: "array"
|
|
322
|
+
},
|
|
323
|
+
title: { description: "Optional table title", type: "string" }
|
|
324
|
+
},
|
|
325
|
+
required: ["columns", "rows"],
|
|
326
|
+
type: "object"
|
|
327
|
+
},
|
|
328
|
+
name: "render_table",
|
|
329
|
+
parse: parseTableSpec
|
|
330
|
+
};
|
|
331
|
+
var statTilesCard = {
|
|
332
|
+
ack: "(stat tiles rendered inline — do not repeat the numbers as text)",
|
|
333
|
+
description: "Render a row of headline stat tiles inline in the chat (max 6): a label, a big value, and an optional delta with direction. Use for the 2-4 numbers that ARE the answer — total attributed revenue, pipeline value, credits remaining — instead of burying them in prose.",
|
|
334
|
+
inputSchema: {
|
|
335
|
+
properties: {
|
|
336
|
+
actions: ACTIONS_SCHEMA,
|
|
337
|
+
tiles: {
|
|
338
|
+
description: "The tiles (max 6)",
|
|
339
|
+
items: {
|
|
340
|
+
properties: {
|
|
341
|
+
delta: {
|
|
342
|
+
description: 'Optional change note, e.g. "+12% vs last month"',
|
|
343
|
+
type: "string"
|
|
344
|
+
},
|
|
345
|
+
deltaDirection: { enum: ["up", "down", "flat"], type: "string" },
|
|
346
|
+
label: { description: "What the number is", type: "string" },
|
|
347
|
+
value: {
|
|
348
|
+
description: 'The formatted headline value, e.g. "$42,300"',
|
|
349
|
+
type: "string"
|
|
350
|
+
}
|
|
351
|
+
},
|
|
352
|
+
required: ["label", "value"],
|
|
353
|
+
type: "object"
|
|
354
|
+
},
|
|
355
|
+
type: "array"
|
|
356
|
+
}
|
|
357
|
+
},
|
|
358
|
+
required: ["tiles"],
|
|
359
|
+
type: "object"
|
|
360
|
+
},
|
|
361
|
+
name: "render_stat_tiles",
|
|
362
|
+
parse: parseStatTilesSpec
|
|
363
|
+
};
|
|
364
|
+
var BUILTIN_UI_CARDS = [chartCard, tableCard, statTilesCard];
|
|
365
|
+
// src/ai/ui/svg.ts
|
|
366
|
+
var LIGHT_UI_THEME = {
|
|
367
|
+
grid: "#e4e4e0",
|
|
368
|
+
palette: [
|
|
369
|
+
"#2a78d6",
|
|
370
|
+
"#1baf7a",
|
|
371
|
+
"#eda100",
|
|
372
|
+
"#008300",
|
|
373
|
+
"#4a3aa7",
|
|
374
|
+
"#e34948",
|
|
375
|
+
"#e87ba4",
|
|
376
|
+
"#eb6834"
|
|
377
|
+
],
|
|
378
|
+
surface: "#fcfcfb",
|
|
379
|
+
textPrimary: "#0b0b0b",
|
|
380
|
+
textSecondary: "#52514e"
|
|
381
|
+
};
|
|
382
|
+
var DARK_UI_THEME = {
|
|
383
|
+
grid: "#333331",
|
|
384
|
+
palette: [
|
|
385
|
+
"#3987e5",
|
|
386
|
+
"#199e70",
|
|
387
|
+
"#c98500",
|
|
388
|
+
"#008300",
|
|
389
|
+
"#9085e9",
|
|
390
|
+
"#e66767",
|
|
391
|
+
"#d55181",
|
|
392
|
+
"#d95926"
|
|
393
|
+
],
|
|
394
|
+
surface: "#1a1a19",
|
|
395
|
+
textPrimary: "#ffffff",
|
|
396
|
+
textSecondary: "#c3c2b7"
|
|
397
|
+
};
|
|
398
|
+
var WIDTH = 640;
|
|
399
|
+
var HEIGHT = 340;
|
|
400
|
+
var MARGIN = { bottom: 42, left: 56, right: 16, top: 64 };
|
|
401
|
+
var BAR_END_RADIUS = 4;
|
|
402
|
+
var MARK_GAP = 2;
|
|
403
|
+
var LINE_WIDTH = 2;
|
|
404
|
+
var MAX_DIRECT_LABELED_SERIES = 4;
|
|
405
|
+
var TICK_TARGET = 4;
|
|
406
|
+
var FONT = "system-ui, -apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif";
|
|
407
|
+
var escapeXml = (value) => value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
408
|
+
var formatValue = (value, spec) => {
|
|
409
|
+
const sign = value < 0 ? "-" : "";
|
|
410
|
+
const abs = Math.abs(value);
|
|
411
|
+
const compact = abs >= 1e6 ? `${(abs / 1e6).toFixed(1).replace(/\.0$/, "")}M` : abs >= 1e4 ? `${(abs / 1000).toFixed(1).replace(/\.0$/, "")}k` : abs >= 1000 ? abs.toLocaleString("en-US") : `${Number.isInteger(abs) ? abs : abs.toFixed(1)}`;
|
|
412
|
+
return `${sign}${spec.unitPrefix ?? ""}${compact}${spec.unitSuffix ?? ""}`;
|
|
413
|
+
};
|
|
414
|
+
var niceTicks = (min, max) => {
|
|
415
|
+
const span = max - min || 1;
|
|
416
|
+
const rough = span / TICK_TARGET;
|
|
417
|
+
const power = Math.pow(10, Math.floor(Math.log10(rough)));
|
|
418
|
+
const candidates = [1, 2, 5, 10].map((step2) => step2 * power);
|
|
419
|
+
const step = candidates.find((candidate) => candidate >= rough) ?? candidates[candidates.length - 1] ?? rough;
|
|
420
|
+
const start = Math.floor(min / step) * step;
|
|
421
|
+
const ticks = [];
|
|
422
|
+
for (let tick = start;tick <= max + step / 2; tick += step) {
|
|
423
|
+
ticks.push(Math.abs(tick) < step / 1e6 ? 0 : tick);
|
|
424
|
+
}
|
|
425
|
+
return ticks;
|
|
426
|
+
};
|
|
427
|
+
var headerSvg = (spec, frame) => {
|
|
428
|
+
const { theme } = frame;
|
|
429
|
+
const parts = [
|
|
430
|
+
`<text x="${frame.plotLeft}" y="24" fill="${theme.textPrimary}" font-size="15" font-weight="600">${escapeXml(spec.title)}</text>`
|
|
431
|
+
];
|
|
432
|
+
if (spec.series.length > 1) {
|
|
433
|
+
let x = frame.plotLeft;
|
|
434
|
+
const swatches = spec.series.map((series, index) => {
|
|
435
|
+
const color = theme.palette[index % theme.palette.length];
|
|
436
|
+
const label = escapeXml(series.name);
|
|
437
|
+
const item = `<rect x="${x}" y="38" width="10" height="10" rx="2" fill="${color}"/><text x="${x + 14}" y="47" fill="${theme.textSecondary}" font-size="11">${label}</text>`;
|
|
438
|
+
x += 14 + series.name.length * 6 + 18;
|
|
439
|
+
return item;
|
|
440
|
+
});
|
|
441
|
+
parts.push(...swatches);
|
|
442
|
+
}
|
|
443
|
+
return parts.join("");
|
|
444
|
+
};
|
|
445
|
+
var gridSvg = (ticks, yFor, spec, frame) => ticks.map((tick) => {
|
|
446
|
+
const y = yFor(tick);
|
|
447
|
+
const isZero = tick === 0;
|
|
448
|
+
return `<line x1="${frame.plotLeft}" y1="${y}" x2="${frame.plotRight}" y2="${y}" stroke="${isZero ? frame.theme.textSecondary : frame.theme.grid}" stroke-width="1"/><text x="${frame.plotLeft - 8}" y="${y + 3.5}" fill="${frame.theme.textSecondary}" font-size="10" text-anchor="end">${escapeXml(formatValue(tick, spec))}</text>`;
|
|
449
|
+
}).join("");
|
|
450
|
+
var xLabelsSvg = (spec, xCenter, frame) => {
|
|
451
|
+
const every = Math.ceil(spec.labels.length / 12);
|
|
452
|
+
return spec.labels.map((label, index) => {
|
|
453
|
+
if (index % every !== 0)
|
|
454
|
+
return "";
|
|
455
|
+
const short = label.length > 12 ? `${label.slice(0, 11)}…` : label;
|
|
456
|
+
return `<text x="${xCenter(index)}" y="${frame.plotBottom + 18}" fill="${frame.theme.textSecondary}" font-size="10" text-anchor="middle">${escapeXml(short)}</text>`;
|
|
457
|
+
}).join("");
|
|
458
|
+
};
|
|
459
|
+
var barPath = (x, yValue, yBase, width) => {
|
|
460
|
+
const up = yValue <= yBase;
|
|
461
|
+
const top = Math.min(yValue, yBase);
|
|
462
|
+
const bottom = Math.max(yValue, yBase);
|
|
463
|
+
const radius = Math.min(BAR_END_RADIUS, width / 2, bottom - top);
|
|
464
|
+
if (radius <= 0)
|
|
465
|
+
return "";
|
|
466
|
+
if (up) {
|
|
467
|
+
return `M${x},${bottom} L${x},${top + radius} Q${x},${top} ${x + radius},${top} L${x + width - radius},${top} Q${x + width},${top} ${x + width},${top + radius} L${x + width},${bottom} Z`;
|
|
468
|
+
}
|
|
469
|
+
return `M${x},${top} L${x},${bottom - radius} Q${x},${bottom} ${x + radius},${bottom} L${x + width - radius},${bottom} Q${x + width},${bottom} ${x + width},${bottom - radius} L${x + width},${top} Z`;
|
|
470
|
+
};
|
|
471
|
+
var valueDomain = (spec) => {
|
|
472
|
+
const all = spec.series.flatMap((series) => series.values);
|
|
473
|
+
const min = Math.min(0, ...all);
|
|
474
|
+
const max = Math.max(0, ...all);
|
|
475
|
+
return max === min ? { max: min + 1, min } : { max, min };
|
|
476
|
+
};
|
|
477
|
+
var cartesianSvg = (spec, frame) => {
|
|
478
|
+
const { theme } = frame;
|
|
479
|
+
const domain = valueDomain(spec);
|
|
480
|
+
const ticks = niceTicks(domain.min, domain.max);
|
|
481
|
+
const lo = Math.min(domain.min, ticks[0] ?? domain.min);
|
|
482
|
+
const hi = Math.max(domain.max, ticks[ticks.length - 1] ?? domain.max);
|
|
483
|
+
const yFor = (value) => frame.plotBottom - (value - lo) / (hi - lo) * (frame.plotBottom - frame.plotTop);
|
|
484
|
+
const slot = (frame.plotRight - frame.plotLeft) / spec.labels.length;
|
|
485
|
+
const xCenter = (index) => frame.plotLeft + slot * (index + 0.5);
|
|
486
|
+
const parts = [gridSvg(ticks, yFor, spec, frame)];
|
|
487
|
+
if (spec.type === "bar") {
|
|
488
|
+
const group = Math.min(slot * 0.72, 64);
|
|
489
|
+
const barWidth = Math.max(2, (group - MARK_GAP * (spec.series.length - 1)) / spec.series.length);
|
|
490
|
+
const yBase = yFor(Math.max(lo, Math.min(hi, 0)));
|
|
491
|
+
spec.series.forEach((series, seriesIndex) => {
|
|
492
|
+
const color = theme.palette[seriesIndex % theme.palette.length];
|
|
493
|
+
series.values.forEach((value, index) => {
|
|
494
|
+
const x = xCenter(index) - group / 2 + seriesIndex * (barWidth + MARK_GAP);
|
|
495
|
+
const tooltip = `${escapeXml(series.name)} · ${escapeXml(spec.labels[index] ?? "")}: ${escapeXml(formatValue(value, spec))}`;
|
|
496
|
+
parts.push(`<path d="${barPath(x, yFor(value), yBase, barWidth)}" fill="${color}"><title>${tooltip}</title></path>`);
|
|
497
|
+
});
|
|
498
|
+
});
|
|
499
|
+
const [only] = spec.series;
|
|
500
|
+
if (spec.series.length === 1 && only && spec.labels.length <= 8) {
|
|
501
|
+
only.values.forEach((value, index) => {
|
|
502
|
+
const above = value >= 0;
|
|
503
|
+
parts.push(`<text x="${xCenter(index)}" y="${yFor(value) + (above ? -6 : 14)}" fill="${theme.textSecondary}" font-size="10" text-anchor="middle">${escapeXml(formatValue(value, spec))}</text>`);
|
|
504
|
+
});
|
|
505
|
+
}
|
|
506
|
+
} else {
|
|
507
|
+
spec.series.forEach((series, seriesIndex) => {
|
|
508
|
+
const color = theme.palette[seriesIndex % theme.palette.length];
|
|
509
|
+
const points = series.values.map((value, index) => `${xCenter(index)},${yFor(value)}`);
|
|
510
|
+
parts.push(`<polyline points="${points.join(" ")}" fill="none" stroke="${color}" stroke-width="${LINE_WIDTH}" stroke-linejoin="round" stroke-linecap="round"/>`);
|
|
511
|
+
series.values.forEach((value, index) => {
|
|
512
|
+
const tooltip = `${escapeXml(series.name)} · ${escapeXml(spec.labels[index] ?? "")}: ${escapeXml(formatValue(value, spec))}`;
|
|
513
|
+
parts.push(`<circle cx="${xCenter(index)}" cy="${yFor(value)}" r="4" fill="${color}" stroke="${theme.surface}" stroke-width="2"><title>${tooltip}</title></circle>`);
|
|
514
|
+
});
|
|
515
|
+
const last = series.values[series.values.length - 1];
|
|
516
|
+
if (spec.series.length <= MAX_DIRECT_LABELED_SERIES && last !== undefined) {
|
|
517
|
+
parts.push(`<text x="${frame.plotRight + 4}" y="${yFor(last) + 3.5}" fill="${theme.textSecondary}" font-size="10">${escapeXml(series.name)}</text>`);
|
|
518
|
+
}
|
|
519
|
+
});
|
|
520
|
+
}
|
|
521
|
+
parts.push(xLabelsSvg(spec, xCenter, frame));
|
|
522
|
+
return parts.join("");
|
|
523
|
+
};
|
|
524
|
+
var donutSvg = (spec, frame) => {
|
|
525
|
+
const { theme } = frame;
|
|
526
|
+
const [series] = spec.series;
|
|
527
|
+
if (!series)
|
|
528
|
+
return "";
|
|
529
|
+
const total = series.values.reduce((sum, value) => sum + value, 0);
|
|
530
|
+
if (total <= 0)
|
|
531
|
+
return "";
|
|
532
|
+
const cx = (frame.plotLeft + frame.plotRight) / 2;
|
|
533
|
+
const cy = (frame.plotTop + frame.plotBottom) / 2 + 4;
|
|
534
|
+
const radius = Math.min((frame.plotBottom - frame.plotTop) / 2 - 4, (frame.plotRight - frame.plotLeft) / 4);
|
|
535
|
+
const ring = Math.max(14, radius * 0.34);
|
|
536
|
+
const mid = radius - ring / 2;
|
|
537
|
+
const gapAngle = MARK_GAP / mid;
|
|
538
|
+
const parts = [];
|
|
539
|
+
let angle = -Math.PI / 2;
|
|
540
|
+
series.values.forEach((value, index) => {
|
|
541
|
+
const sweep = value / total * Math.PI * 2;
|
|
542
|
+
const start = angle + gapAngle / 2;
|
|
543
|
+
const end = angle + sweep - gapAngle / 2;
|
|
544
|
+
angle += sweep;
|
|
545
|
+
if (end <= start)
|
|
546
|
+
return;
|
|
547
|
+
const large = end - start > Math.PI ? 1 : 0;
|
|
548
|
+
const x1 = cx + mid * Math.cos(start);
|
|
549
|
+
const y1 = cy + mid * Math.sin(start);
|
|
550
|
+
const x2 = cx + mid * Math.cos(end);
|
|
551
|
+
const y2 = cy + mid * Math.sin(end);
|
|
552
|
+
const color = theme.palette[index % theme.palette.length];
|
|
553
|
+
const share = `${Math.round(value / total * 100)}%`;
|
|
554
|
+
const tooltip = `${escapeXml(spec.labels[index] ?? "")}: ${escapeXml(formatValue(value, spec))} (${share})`;
|
|
555
|
+
parts.push(`<path d="M${x1},${y1} A${mid},${mid} 0 ${large} 1 ${x2},${y2}" fill="none" stroke="${color}" stroke-width="${ring}"><title>${tooltip}</title></path>`);
|
|
556
|
+
});
|
|
557
|
+
parts.push(`<text x="${cx}" y="${cy + 5}" fill="${theme.textPrimary}" font-size="16" font-weight="600" text-anchor="middle">${escapeXml(formatValue(total, spec))}</text>`);
|
|
558
|
+
let x = frame.plotLeft;
|
|
559
|
+
spec.labels.forEach((label, index) => {
|
|
560
|
+
const color = theme.palette[index % theme.palette.length];
|
|
561
|
+
parts.push(`<rect x="${x}" y="38" width="10" height="10" rx="2" fill="${color}"/><text x="${x + 14}" y="47" fill="${theme.textSecondary}" font-size="11">${escapeXml(label)}</text>`);
|
|
562
|
+
x += 14 + label.length * 6 + 18;
|
|
563
|
+
});
|
|
564
|
+
return parts.join("");
|
|
565
|
+
};
|
|
566
|
+
var renderChartSvg = (spec, options = {}) => {
|
|
567
|
+
const base = options.mode === "dark" ? DARK_UI_THEME : LIGHT_UI_THEME;
|
|
568
|
+
const theme = { ...base, ...options.theme };
|
|
569
|
+
const width = options.width ?? WIDTH;
|
|
570
|
+
const height = options.height ?? HEIGHT;
|
|
571
|
+
const frame = {
|
|
572
|
+
plotBottom: height - MARGIN.bottom,
|
|
573
|
+
plotLeft: MARGIN.left,
|
|
574
|
+
plotRight: width - MARGIN.right - (spec.type === "line" ? 64 : 0),
|
|
575
|
+
plotTop: MARGIN.top,
|
|
576
|
+
theme
|
|
577
|
+
};
|
|
578
|
+
const body = spec.type === "donut" ? donutSvg(spec, frame) : cartesianSvg(spec, frame);
|
|
579
|
+
return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${width} ${height}" role="img" aria-label="${escapeXml(spec.title)}" font-family="${FONT}"><rect width="${width}" height="${height}" fill="${theme.surface}" rx="12"/>${spec.type === "donut" ? headerSvg({ ...spec, series: [] }, frame) : headerSvg(spec, frame)}${body}</svg>`;
|
|
580
|
+
};
|
|
581
|
+
export {
|
|
582
|
+
tableCard,
|
|
583
|
+
statTilesCard,
|
|
584
|
+
renderChartSvg,
|
|
585
|
+
parseUiActions,
|
|
586
|
+
parseTableSpec,
|
|
587
|
+
parseStatTilesSpec,
|
|
588
|
+
parseChartSpec,
|
|
589
|
+
createUiCards,
|
|
590
|
+
chartCard,
|
|
591
|
+
UI_ACTIONS_MAX,
|
|
592
|
+
TABLE_MAX_ROWS,
|
|
593
|
+
TABLE_MAX_COLUMNS,
|
|
594
|
+
STAT_TILES_MAX,
|
|
595
|
+
LIGHT_UI_THEME,
|
|
596
|
+
DARK_UI_THEME,
|
|
597
|
+
CHART_TYPES,
|
|
598
|
+
CHART_MAX_SERIES,
|
|
599
|
+
CHART_MAX_POINTS,
|
|
600
|
+
BUILTIN_UI_CARDS
|
|
601
|
+
};
|
|
602
|
+
|
|
603
|
+
//# debugId=361FE26314A51E4B64756E2164756E21
|
|
604
|
+
//# sourceMappingURL=index.js.map
|