@andpipes/widget-sdk 0.1.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/LICENSE +201 -0
- package/dist/chunk-GG2BTLFR.js +53 -0
- package/dist/index.cjs +1043 -0
- package/dist/index.d.cts +494 -0
- package/dist/index.d.ts +494 -0
- package/dist/index.js +918 -0
- package/dist/render-token-limit-5gpQxyG-.d.cts +14 -0
- package/dist/render-token-limit-5gpQxyG-.d.ts +14 -0
- package/dist/render-token.cjs +153 -0
- package/dist/render-token.d.cts +25 -0
- package/dist/render-token.d.ts +25 -0
- package/dist/render-token.js +123 -0
- package/package.json +37 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,1043 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
ADVANCED_WIDGET_TEMPLATES: () => ADVANCED_WIDGET_TEMPLATES,
|
|
34
|
+
CONFIRMATION_PANEL_SCHEMA: () => CONFIRMATION_PANEL_SCHEMA,
|
|
35
|
+
DEFAULT_FORBIDDEN_PII: () => DEFAULT_FORBIDDEN_PII,
|
|
36
|
+
DETAIL_CARD_SCHEMA: () => DETAIL_CARD_SCHEMA,
|
|
37
|
+
INSIGHT_LIST_SCHEMA: () => INSIGHT_LIST_SCHEMA,
|
|
38
|
+
KPI_CARDS_SCHEMA: () => KPI_CARDS_SCHEMA,
|
|
39
|
+
LEGACY_WIDGET_TEMPLATES: () => LEGACY_WIDGET_TEMPLATES,
|
|
40
|
+
RANKING_TABLE_SCHEMA: () => RANKING_TABLE_SCHEMA,
|
|
41
|
+
SEARCH_RESULT_CARDS_SCHEMA: () => SEARCH_RESULT_CARDS_SCHEMA,
|
|
42
|
+
SEMVER_PATTERN: () => SEMVER_PATTERN,
|
|
43
|
+
SETUP_DRAFT_REVIEW_SCHEMA: () => SETUP_DRAFT_REVIEW_SCHEMA,
|
|
44
|
+
TREND_CHART_SCHEMA: () => TREND_CHART_SCHEMA,
|
|
45
|
+
WIDGET_ENVELOPE_V1_SCHEMA: () => WIDGET_ENVELOPE_V1_SCHEMA,
|
|
46
|
+
WIDGET_REGISTRY: () => WIDGET_REGISTRY,
|
|
47
|
+
WIDGET_RENDER_TOKEN_MAX_BYTES: () => WIDGET_RENDER_TOKEN_MAX_BYTES,
|
|
48
|
+
WIDGET_SCHEMA_LEGACY_VERSION: () => WIDGET_SCHEMA_LEGACY_VERSION,
|
|
49
|
+
WIDGET_SCHEMA_VERSION: () => WIDGET_SCHEMA_VERSION,
|
|
50
|
+
WIDGET_SCHEMA_VERSIONS: () => WIDGET_SCHEMA_VERSIONS,
|
|
51
|
+
WIDGET_TEMPLATES: () => WIDGET_TEMPLATES,
|
|
52
|
+
WidgetSchemaError: () => WidgetSchemaError,
|
|
53
|
+
buildConfirmationPanel: () => buildConfirmationPanel,
|
|
54
|
+
buildDetailCard: () => buildDetailCard,
|
|
55
|
+
buildInsightList: () => buildInsightList,
|
|
56
|
+
buildKpiCards: () => buildKpiCards,
|
|
57
|
+
buildRankingTable: () => buildRankingTable,
|
|
58
|
+
buildSearchResultCards: () => buildSearchResultCards,
|
|
59
|
+
buildSetupDraftReview: () => buildSetupDraftReview,
|
|
60
|
+
buildTrendChart: () => buildTrendChart,
|
|
61
|
+
buildWidgetResponse: () => buildWidgetResponse,
|
|
62
|
+
confirmationPanelFallback: () => confirmationPanelFallback,
|
|
63
|
+
detailCardFallback: () => detailCardFallback,
|
|
64
|
+
estimateWidgetRenderTokenBytes: () => estimateWidgetRenderTokenBytes,
|
|
65
|
+
getWidgetTemplateEntry: () => getWidgetTemplateEntry,
|
|
66
|
+
insightListFallback: () => insightListFallback,
|
|
67
|
+
isAdvancedWidgetTemplate: () => isAdvancedWidgetTemplate,
|
|
68
|
+
isWidgetRenderPayloadWithinTokenLimit: () => isWidgetRenderPayloadWithinTokenLimit,
|
|
69
|
+
isWidgetSchemaVersion: () => isWidgetSchemaVersion,
|
|
70
|
+
isWidgetTemplate: () => isWidgetTemplate,
|
|
71
|
+
isWidgetTemplateSupportedAtVersion: () => isWidgetTemplateSupportedAtVersion,
|
|
72
|
+
kpiCardsFallback: () => kpiCardsFallback,
|
|
73
|
+
packageName: () => packageName,
|
|
74
|
+
rankingTableFallback: () => rankingTableFallback,
|
|
75
|
+
redactWidgetPayload: () => redactWidgetPayload,
|
|
76
|
+
searchResultCardsFallback: () => searchResultCardsFallback,
|
|
77
|
+
setupDraftReviewFallback: () => setupDraftReviewFallback,
|
|
78
|
+
trendChartFallback: () => trendChartFallback,
|
|
79
|
+
validateWidgetData: () => validateWidgetData,
|
|
80
|
+
validateWidgetEnvelope: () => validateWidgetEnvelope
|
|
81
|
+
});
|
|
82
|
+
module.exports = __toCommonJS(index_exports);
|
|
83
|
+
|
|
84
|
+
// src/build/builder.ts
|
|
85
|
+
var import_localization9 = require("@andpipes/localization");
|
|
86
|
+
|
|
87
|
+
// src/redact/forbidden.ts
|
|
88
|
+
var DEFAULT_FORBIDDEN_PII = [
|
|
89
|
+
"phone",
|
|
90
|
+
"email",
|
|
91
|
+
"real_name",
|
|
92
|
+
"address",
|
|
93
|
+
"payment_info",
|
|
94
|
+
"token",
|
|
95
|
+
"access_token",
|
|
96
|
+
"refresh_token",
|
|
97
|
+
"id_token",
|
|
98
|
+
"session_token",
|
|
99
|
+
"secret",
|
|
100
|
+
"client_secret",
|
|
101
|
+
"api_key",
|
|
102
|
+
"authorization",
|
|
103
|
+
"password",
|
|
104
|
+
"private_key"
|
|
105
|
+
];
|
|
106
|
+
|
|
107
|
+
// src/redact/redact.ts
|
|
108
|
+
var DEFAULT_PLACEHOLDER = "[REDACTED]";
|
|
109
|
+
var SAFE_LABEL_MAP_KEYS = /* @__PURE__ */ new Set(["fieldLabels", "attributeLabels"]);
|
|
110
|
+
var SEMANTIC_CHANGE_VALUE_KEYS = /* @__PURE__ */ new Set(["before", "after"]);
|
|
111
|
+
function normalizeKey(key) {
|
|
112
|
+
return key.toLowerCase().replace(/[^a-z0-9]/g, "");
|
|
113
|
+
}
|
|
114
|
+
function isPlainObject(value) {
|
|
115
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
116
|
+
}
|
|
117
|
+
function safeAssign(target, key, value) {
|
|
118
|
+
Object.defineProperty(target, key, {
|
|
119
|
+
value,
|
|
120
|
+
writable: true,
|
|
121
|
+
enumerable: true,
|
|
122
|
+
configurable: true
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
function redactWidgetPayload(data, forbidden = DEFAULT_FORBIDDEN_PII, options = {}) {
|
|
126
|
+
const placeholder = options.placeholder === void 0 ? DEFAULT_PLACEHOLDER : options.placeholder;
|
|
127
|
+
const forbiddenSet = new Set(forbidden.map(normalizeKey));
|
|
128
|
+
const redactions = [];
|
|
129
|
+
const seen = /* @__PURE__ */ new WeakSet();
|
|
130
|
+
function walk(value2, path, parentKey) {
|
|
131
|
+
if (Array.isArray(value2)) {
|
|
132
|
+
if (seen.has(value2)) {
|
|
133
|
+
return "[Circular]";
|
|
134
|
+
}
|
|
135
|
+
seen.add(value2);
|
|
136
|
+
const mapped = value2.map((item, index) => walk(item, `${path}[${index}]`, parentKey));
|
|
137
|
+
seen.delete(value2);
|
|
138
|
+
return mapped;
|
|
139
|
+
}
|
|
140
|
+
if (isPlainObject(value2)) {
|
|
141
|
+
if (seen.has(value2)) {
|
|
142
|
+
return "[Circular]";
|
|
143
|
+
}
|
|
144
|
+
seen.add(value2);
|
|
145
|
+
const result = {};
|
|
146
|
+
const semanticField = typeof value2.field === "string" && forbiddenSet.has(normalizeKey(value2.field)) ? value2.field : null;
|
|
147
|
+
const isSafeLabelMap = parentKey !== void 0 && SAFE_LABEL_MAP_KEYS.has(parentKey);
|
|
148
|
+
for (const [key, child] of Object.entries(value2)) {
|
|
149
|
+
const childPath = path.length > 0 ? `${path}.${key}` : key;
|
|
150
|
+
const directForbidden = forbiddenSet.has(normalizeKey(key)) && !isSafeLabelMap;
|
|
151
|
+
const semanticForbidden = semanticField !== null && SEMANTIC_CHANGE_VALUE_KEYS.has(key);
|
|
152
|
+
if (directForbidden || semanticForbidden) {
|
|
153
|
+
redactions.push({ path: childPath, key: semanticField ?? key });
|
|
154
|
+
if (placeholder !== null) {
|
|
155
|
+
safeAssign(result, key, placeholder);
|
|
156
|
+
}
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
safeAssign(result, key, walk(child, childPath, key));
|
|
160
|
+
}
|
|
161
|
+
seen.delete(value2);
|
|
162
|
+
return result;
|
|
163
|
+
}
|
|
164
|
+
return value2;
|
|
165
|
+
}
|
|
166
|
+
const value = walk(data, "");
|
|
167
|
+
return { value, redactions };
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// src/render-token-limit.ts
|
|
171
|
+
var WIDGET_RENDER_TOKEN_MAX_BYTES = 64 * 1024;
|
|
172
|
+
var TOKEN_VERSION_BYTES = 2;
|
|
173
|
+
var TOKEN_SEPARATOR_BYTES = 3;
|
|
174
|
+
var IV_BASE64URL_BYTES = 16;
|
|
175
|
+
var TAG_BASE64URL_BYTES = 22;
|
|
176
|
+
var FIXED_TOKEN_BYTES = TOKEN_VERSION_BYTES + TOKEN_SEPARATOR_BYTES + IV_BASE64URL_BYTES + TAG_BASE64URL_BYTES;
|
|
177
|
+
function estimateWidgetRenderTokenBytesFromPlaintextBytes(plaintextBytes) {
|
|
178
|
+
return FIXED_TOKEN_BYTES + Math.ceil(plaintextBytes * 4 / 3);
|
|
179
|
+
}
|
|
180
|
+
function utf8ByteLength(value) {
|
|
181
|
+
let bytes = 0;
|
|
182
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
183
|
+
const code = value.charCodeAt(index);
|
|
184
|
+
if (code <= 127) {
|
|
185
|
+
bytes += 1;
|
|
186
|
+
} else if (code <= 2047) {
|
|
187
|
+
bytes += 2;
|
|
188
|
+
} else if (code >= 55296 && code <= 56319 && index + 1 < value.length && value.charCodeAt(index + 1) >= 56320 && value.charCodeAt(index + 1) <= 57343) {
|
|
189
|
+
bytes += 4;
|
|
190
|
+
index += 1;
|
|
191
|
+
} else {
|
|
192
|
+
bytes += 3;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return bytes;
|
|
196
|
+
}
|
|
197
|
+
function estimateWidgetRenderTokenBytes(input) {
|
|
198
|
+
try {
|
|
199
|
+
const payload = {
|
|
200
|
+
template: input.template,
|
|
201
|
+
version: input.version,
|
|
202
|
+
structuredContent: input.structuredContent,
|
|
203
|
+
...input.textFallback ? { textFallback: input.textFallback } : {},
|
|
204
|
+
// builderからの検査では最大桁を使い、実encodeより安全側に倒す。
|
|
205
|
+
issuedAt: input.issuedAt ?? Number.MAX_SAFE_INTEGER
|
|
206
|
+
};
|
|
207
|
+
const plaintextBytes = utf8ByteLength(JSON.stringify(payload));
|
|
208
|
+
return estimateWidgetRenderTokenBytesFromPlaintextBytes(plaintextBytes);
|
|
209
|
+
} catch {
|
|
210
|
+
return Number.POSITIVE_INFINITY;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
function isWidgetRenderPayloadWithinTokenLimit(input) {
|
|
214
|
+
return estimateWidgetRenderTokenBytes(input) <= WIDGET_RENDER_TOKEN_MAX_BYTES;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// src/schema/templates.ts
|
|
218
|
+
var LEGACY_WIDGET_TEMPLATES = [
|
|
219
|
+
"kpi_cards",
|
|
220
|
+
"ranking_table",
|
|
221
|
+
"trend_chart",
|
|
222
|
+
"insight_list",
|
|
223
|
+
"confirmation_panel"
|
|
224
|
+
];
|
|
225
|
+
var ADVANCED_WIDGET_TEMPLATES = [
|
|
226
|
+
"detail_card",
|
|
227
|
+
"search_result_cards",
|
|
228
|
+
"setup_draft_review"
|
|
229
|
+
];
|
|
230
|
+
var WIDGET_TEMPLATES = [...LEGACY_WIDGET_TEMPLATES, ...ADVANCED_WIDGET_TEMPLATES];
|
|
231
|
+
function isWidgetTemplate(value) {
|
|
232
|
+
return typeof value === "string" && WIDGET_TEMPLATES.includes(value);
|
|
233
|
+
}
|
|
234
|
+
function isAdvancedWidgetTemplate(value) {
|
|
235
|
+
return typeof value === "string" && ADVANCED_WIDGET_TEMPLATES.includes(value);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// src/schema/envelope.ts
|
|
239
|
+
var WIDGET_SCHEMA_LEGACY_VERSION = "1.0.0";
|
|
240
|
+
var WIDGET_SCHEMA_VERSION = "1.1.0";
|
|
241
|
+
var WIDGET_SCHEMA_VERSIONS = [
|
|
242
|
+
WIDGET_SCHEMA_LEGACY_VERSION,
|
|
243
|
+
WIDGET_SCHEMA_VERSION
|
|
244
|
+
];
|
|
245
|
+
function isWidgetSchemaVersion(value) {
|
|
246
|
+
return typeof value === "string" && WIDGET_SCHEMA_VERSIONS.includes(value);
|
|
247
|
+
}
|
|
248
|
+
function isWidgetTemplateSupportedAtVersion(template, version) {
|
|
249
|
+
if (!isWidgetTemplate(template) || !isWidgetSchemaVersion(version)) {
|
|
250
|
+
return false;
|
|
251
|
+
}
|
|
252
|
+
return version === WIDGET_SCHEMA_VERSION || !isAdvancedWidgetTemplate(template);
|
|
253
|
+
}
|
|
254
|
+
var SEMVER_PATTERN = "^\\d+\\.\\d+\\.\\d+$";
|
|
255
|
+
var WIDGET_ENVELOPE_V1_SCHEMA = {
|
|
256
|
+
$schema: "http://json-schema.org/draft-07/schema#",
|
|
257
|
+
$id: "https://andpipe.dev/schema/widget-envelope/v1.json",
|
|
258
|
+
title: "andpipe Widget envelope v1",
|
|
259
|
+
type: "object",
|
|
260
|
+
required: ["structuredContent", "content", "textFallback", "_meta"],
|
|
261
|
+
properties: {
|
|
262
|
+
structuredContent: { type: "object" },
|
|
263
|
+
content: {
|
|
264
|
+
type: "array",
|
|
265
|
+
minItems: 1,
|
|
266
|
+
items: {
|
|
267
|
+
type: "object",
|
|
268
|
+
required: ["type", "text"],
|
|
269
|
+
properties: {
|
|
270
|
+
type: { const: "text" },
|
|
271
|
+
text: { type: "string", minLength: 1 }
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
},
|
|
275
|
+
textFallback: { type: "string", minLength: 1 },
|
|
276
|
+
_meta: {
|
|
277
|
+
type: "object",
|
|
278
|
+
required: ["widget", "fallback"],
|
|
279
|
+
properties: {
|
|
280
|
+
widget: {
|
|
281
|
+
type: "object",
|
|
282
|
+
required: ["template", "version"],
|
|
283
|
+
properties: {
|
|
284
|
+
template: { type: "string", enum: [...WIDGET_TEMPLATES] },
|
|
285
|
+
version: { type: "string", enum: [...WIDGET_SCHEMA_VERSIONS] }
|
|
286
|
+
}
|
|
287
|
+
},
|
|
288
|
+
fallback: {
|
|
289
|
+
type: "object",
|
|
290
|
+
required: ["type"],
|
|
291
|
+
properties: {
|
|
292
|
+
type: { const: "markdown" }
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
// src/fallback/confirmation-panel.ts
|
|
301
|
+
var import_localization = require("@andpipes/localization");
|
|
302
|
+
|
|
303
|
+
// src/fallback/markdown.ts
|
|
304
|
+
function escapeCell(value) {
|
|
305
|
+
return String(value).replace(/\\/g, "\\\\").replace(/\|/g, "\\|").replace(/\r?\n/g, " ");
|
|
306
|
+
}
|
|
307
|
+
function tableRow(cells) {
|
|
308
|
+
return `| ${cells.map(escapeCell).join(" | ")} |`;
|
|
309
|
+
}
|
|
310
|
+
function markdownTable(headers, rows) {
|
|
311
|
+
const head = tableRow(headers);
|
|
312
|
+
const divider = `| ${headers.map(() => "---").join(" | ")} |`;
|
|
313
|
+
if (rows.length === 0) {
|
|
314
|
+
return `${head}
|
|
315
|
+
${divider}`;
|
|
316
|
+
}
|
|
317
|
+
const body = rows.map(tableRow).join("\n");
|
|
318
|
+
return `${head}
|
|
319
|
+
${divider}
|
|
320
|
+
${body}`;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// src/fallback/confirmation-panel.ts
|
|
324
|
+
function confirmationPanelFallback(data, locale = import_localization.DEFAULT_LOCALE) {
|
|
325
|
+
const text = import_localization.LOCALIZED_TEXT[locale].widget;
|
|
326
|
+
const lines = [`## ${data.title}`];
|
|
327
|
+
if (data.summary) {
|
|
328
|
+
lines.push(data.summary);
|
|
329
|
+
}
|
|
330
|
+
if (data.risk) {
|
|
331
|
+
lines.push(`- risk: ${data.risk}`);
|
|
332
|
+
}
|
|
333
|
+
if (data.changes && data.changes.length > 0) {
|
|
334
|
+
const rows = data.changes.map((change) => [
|
|
335
|
+
change.field,
|
|
336
|
+
change.before ?? "",
|
|
337
|
+
change.after ?? ""
|
|
338
|
+
]);
|
|
339
|
+
lines.push(
|
|
340
|
+
markdownTable(
|
|
341
|
+
[text.tableHeaders.field, text.tableHeaders.before, text.tableHeaders.after],
|
|
342
|
+
rows
|
|
343
|
+
)
|
|
344
|
+
);
|
|
345
|
+
}
|
|
346
|
+
lines.push(
|
|
347
|
+
data.requiresConfirmation ? text.confirmationRequired.replace("{action}", data.action) : text.executionStatement.replace("{action}", data.action)
|
|
348
|
+
);
|
|
349
|
+
return lines.join("\n");
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// src/fallback/detail-card.ts
|
|
353
|
+
var import_localization2 = require("@andpipes/localization");
|
|
354
|
+
function detailCardFallback(data, locale = import_localization2.DEFAULT_LOCALE) {
|
|
355
|
+
const headers = import_localization2.LOCALIZED_TEXT[locale].widget.tableHeaders;
|
|
356
|
+
const lines = [`## ${data.title}`];
|
|
357
|
+
if (data.subtitle) lines.push(data.subtitle);
|
|
358
|
+
if (data.status) lines.push(`- status: ${data.status.label}`);
|
|
359
|
+
if (data.description) lines.push(data.description);
|
|
360
|
+
const rows = Object.entries(data.fields).map(([key, value]) => [
|
|
361
|
+
data.fieldLabels?.[key] ?? key,
|
|
362
|
+
String(value)
|
|
363
|
+
]);
|
|
364
|
+
lines.push(markdownTable([headers.field, headers.value], rows));
|
|
365
|
+
return lines.join("\n");
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// src/fallback/insight-list.ts
|
|
369
|
+
var import_localization3 = require("@andpipes/localization");
|
|
370
|
+
function insightListFallback(data, locale = import_localization3.DEFAULT_LOCALE) {
|
|
371
|
+
const text = import_localization3.LOCALIZED_TEXT[locale].widget;
|
|
372
|
+
const lines = [`## ${data.title ?? text.defaultTitles.insightList}`];
|
|
373
|
+
for (const insight of data.insights) {
|
|
374
|
+
lines.push(`- [${insight.severity}] ${insight.message}`);
|
|
375
|
+
if (insight.recommendation) {
|
|
376
|
+
lines.push(` \u2192 ${insight.recommendation}`);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
return lines.join("\n");
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// src/fallback/kpi-cards.ts
|
|
383
|
+
var import_localization4 = require("@andpipes/localization");
|
|
384
|
+
var DIRECTION_ARROW = {
|
|
385
|
+
up: "\u25B2",
|
|
386
|
+
down: "\u25BC",
|
|
387
|
+
flat: "\u2192"
|
|
388
|
+
};
|
|
389
|
+
function formatDelta(delta) {
|
|
390
|
+
return `${DIRECTION_ARROW[delta.direction]} ${delta.value}`;
|
|
391
|
+
}
|
|
392
|
+
function kpiCardsFallback(data, locale = import_localization4.DEFAULT_LOCALE) {
|
|
393
|
+
const text = import_localization4.LOCALIZED_TEXT[locale].widget;
|
|
394
|
+
const lines = [`## ${data.title ?? text.defaultTitles.kpiCards}`];
|
|
395
|
+
for (const card of data.cards) {
|
|
396
|
+
const unit = card.unit ?? "";
|
|
397
|
+
const delta = card.delta ? `\uFF08${formatDelta(card.delta)}\uFF09` : "";
|
|
398
|
+
const caption = card.caption ? ` \u2014 ${card.caption}` : "";
|
|
399
|
+
lines.push(`- **${card.label}**: ${card.value}${unit}${delta}${caption}`);
|
|
400
|
+
}
|
|
401
|
+
return lines.join("\n");
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
// src/fallback/ranking-table.ts
|
|
405
|
+
var import_localization5 = require("@andpipes/localization");
|
|
406
|
+
function rankingTableFallback(data, locale = import_localization5.DEFAULT_LOCALE) {
|
|
407
|
+
const text = import_localization5.LOCALIZED_TEXT[locale].widget;
|
|
408
|
+
const hasRank = data.rows.some((row) => row.rank !== void 0);
|
|
409
|
+
const headers = [
|
|
410
|
+
...hasRank ? ["#"] : [],
|
|
411
|
+
...data.columns.map((column) => column.label)
|
|
412
|
+
];
|
|
413
|
+
const rows = data.rows.map((row) => [
|
|
414
|
+
...hasRank ? [row.rank ?? ""] : [],
|
|
415
|
+
...data.columns.map((column) => row.cells[column.key] ?? "")
|
|
416
|
+
]);
|
|
417
|
+
return `## ${data.title ?? text.defaultTitles.rankingTable}
|
|
418
|
+
${markdownTable(headers, rows)}`;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
// src/fallback/search-result-cards.ts
|
|
422
|
+
var import_localization6 = require("@andpipes/localization");
|
|
423
|
+
function searchResultCardsFallback(data, locale = import_localization6.DEFAULT_LOCALE) {
|
|
424
|
+
const text = import_localization6.LOCALIZED_TEXT[locale].widget.searchResults;
|
|
425
|
+
const lines = [`## ${data.title ?? text.title}`];
|
|
426
|
+
if (data.query) lines.push(`${text.query}: ${data.query}`);
|
|
427
|
+
if (data.results.length === 0) {
|
|
428
|
+
lines.push(text.empty);
|
|
429
|
+
return lines.join("\n");
|
|
430
|
+
}
|
|
431
|
+
for (const result of data.results) {
|
|
432
|
+
lines.push(`### ${result.title}`);
|
|
433
|
+
if (result.badge) lines.push(`- status: ${result.badge.label}`);
|
|
434
|
+
if (result.summary) lines.push(result.summary);
|
|
435
|
+
for (const [key, value] of Object.entries(result.attributes ?? {})) {
|
|
436
|
+
lines.push(`- **${result.attributeLabels?.[key] ?? key}**: ${String(value)}`);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
return lines.join("\n");
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
// src/fallback/setup-draft-review.ts
|
|
443
|
+
var import_localization7 = require("@andpipes/localization");
|
|
444
|
+
function setupDraftReviewFallback(data, locale = import_localization7.DEFAULT_LOCALE) {
|
|
445
|
+
const text = import_localization7.LOCALIZED_TEXT[locale].widget;
|
|
446
|
+
const lines = [`## ${data.title}`];
|
|
447
|
+
if (data.summary) lines.push(data.summary);
|
|
448
|
+
if (data.status) lines.push(`- status: ${data.status}`);
|
|
449
|
+
for (const warning of data.warnings ?? []) lines.push(`- warning: ${warning}`);
|
|
450
|
+
for (const section of data.sections) {
|
|
451
|
+
lines.push(`### ${section.title}`);
|
|
452
|
+
lines.push(
|
|
453
|
+
markdownTable(
|
|
454
|
+
[text.tableHeaders.field, text.tableHeaders.before, text.tableHeaders.after],
|
|
455
|
+
section.changes.map((change) => [change.field, change.before ?? "", change.after ?? ""])
|
|
456
|
+
)
|
|
457
|
+
);
|
|
458
|
+
}
|
|
459
|
+
lines.push(text.confirmationRequired.replace("{action}", data.action));
|
|
460
|
+
return lines.join("\n");
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
// src/fallback/trend-chart.ts
|
|
464
|
+
var import_localization8 = require("@andpipes/localization");
|
|
465
|
+
function trendChartFallback(data, locale = import_localization8.DEFAULT_LOCALE) {
|
|
466
|
+
const text = import_localization8.LOCALIZED_TEXT[locale].widget;
|
|
467
|
+
const lines = [`## ${data.title ?? text.defaultTitles.trendChart}`];
|
|
468
|
+
const yLabel = data.unit ? `y (${data.unit})` : "y";
|
|
469
|
+
for (const series of data.series) {
|
|
470
|
+
lines.push(`### ${series.name}`);
|
|
471
|
+
const rows = series.points.map((point) => [point.x, point.y]);
|
|
472
|
+
lines.push(markdownTable(["x", yLabel], rows));
|
|
473
|
+
}
|
|
474
|
+
return lines.join("\n");
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
// src/schema/widgets/confirmation-panel.ts
|
|
478
|
+
var CONFIRMATION_PANEL_SCHEMA = {
|
|
479
|
+
$id: "https://andpipe.dev/schema/widget/confirmation-panel/v1.json",
|
|
480
|
+
type: "object",
|
|
481
|
+
required: ["title", "action", "requiresConfirmation"],
|
|
482
|
+
additionalProperties: false,
|
|
483
|
+
properties: {
|
|
484
|
+
title: { type: "string", minLength: 1 },
|
|
485
|
+
action: { type: "string", minLength: 1 },
|
|
486
|
+
requiresConfirmation: { type: "boolean" },
|
|
487
|
+
summary: { type: "string" },
|
|
488
|
+
risk: { type: "string", enum: ["low", "medium", "high"] },
|
|
489
|
+
changes: {
|
|
490
|
+
type: "array",
|
|
491
|
+
items: {
|
|
492
|
+
type: "object",
|
|
493
|
+
required: ["field"],
|
|
494
|
+
additionalProperties: false,
|
|
495
|
+
properties: {
|
|
496
|
+
field: { type: "string" },
|
|
497
|
+
before: { type: "string" },
|
|
498
|
+
after: { type: "string" }
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
};
|
|
504
|
+
|
|
505
|
+
// src/schema/widgets/shared.ts
|
|
506
|
+
var WIDGET_BADGE_SCHEMA = {
|
|
507
|
+
type: "object",
|
|
508
|
+
required: ["label", "tone"],
|
|
509
|
+
additionalProperties: false,
|
|
510
|
+
properties: {
|
|
511
|
+
label: { type: "string", minLength: 1, maxLength: 80 },
|
|
512
|
+
tone: { type: "string", enum: ["neutral", "success", "warning", "danger"] }
|
|
513
|
+
}
|
|
514
|
+
};
|
|
515
|
+
var WIDGET_SCALAR_SCHEMA = {
|
|
516
|
+
type: ["string", "number", "boolean"]
|
|
517
|
+
};
|
|
518
|
+
|
|
519
|
+
// src/schema/widgets/detail-card.ts
|
|
520
|
+
var DETAIL_CARD_SCHEMA = {
|
|
521
|
+
$id: "https://andpipe.dev/schema/widget/detail-card/v1.1.json",
|
|
522
|
+
type: "object",
|
|
523
|
+
required: ["title", "fields"],
|
|
524
|
+
additionalProperties: false,
|
|
525
|
+
properties: {
|
|
526
|
+
title: { type: "string", minLength: 1, maxLength: 200 },
|
|
527
|
+
subtitle: { type: "string", maxLength: 300 },
|
|
528
|
+
description: { type: "string", maxLength: 2e3 },
|
|
529
|
+
fields: {
|
|
530
|
+
type: "object",
|
|
531
|
+
minProperties: 1,
|
|
532
|
+
maxProperties: 24,
|
|
533
|
+
additionalProperties: WIDGET_SCALAR_SCHEMA
|
|
534
|
+
},
|
|
535
|
+
fieldLabels: {
|
|
536
|
+
type: "object",
|
|
537
|
+
maxProperties: 24,
|
|
538
|
+
additionalProperties: { type: "string", minLength: 1, maxLength: 120 }
|
|
539
|
+
},
|
|
540
|
+
status: WIDGET_BADGE_SCHEMA
|
|
541
|
+
}
|
|
542
|
+
};
|
|
543
|
+
|
|
544
|
+
// src/schema/widgets/insight-list.ts
|
|
545
|
+
var INSIGHT_LIST_SCHEMA = {
|
|
546
|
+
$id: "https://andpipe.dev/schema/widget/insight-list/v1.json",
|
|
547
|
+
type: "object",
|
|
548
|
+
required: ["insights"],
|
|
549
|
+
additionalProperties: false,
|
|
550
|
+
properties: {
|
|
551
|
+
title: { type: "string" },
|
|
552
|
+
insights: {
|
|
553
|
+
type: "array",
|
|
554
|
+
minItems: 1,
|
|
555
|
+
items: {
|
|
556
|
+
type: "object",
|
|
557
|
+
required: ["severity", "message"],
|
|
558
|
+
additionalProperties: false,
|
|
559
|
+
properties: {
|
|
560
|
+
severity: { type: "string", enum: ["info", "warning", "critical"] },
|
|
561
|
+
message: { type: "string", minLength: 1 },
|
|
562
|
+
recommendation: { type: "string" }
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
};
|
|
568
|
+
|
|
569
|
+
// src/schema/widgets/kpi-cards.ts
|
|
570
|
+
var KPI_CARDS_SCHEMA = {
|
|
571
|
+
$id: "https://andpipe.dev/schema/widget/kpi-cards/v1.json",
|
|
572
|
+
type: "object",
|
|
573
|
+
required: ["cards"],
|
|
574
|
+
additionalProperties: false,
|
|
575
|
+
properties: {
|
|
576
|
+
title: { type: "string" },
|
|
577
|
+
cards: {
|
|
578
|
+
type: "array",
|
|
579
|
+
minItems: 1,
|
|
580
|
+
items: {
|
|
581
|
+
type: "object",
|
|
582
|
+
required: ["label", "value"],
|
|
583
|
+
additionalProperties: false,
|
|
584
|
+
properties: {
|
|
585
|
+
label: { type: "string", minLength: 1 },
|
|
586
|
+
value: { type: ["string", "number"] },
|
|
587
|
+
unit: { type: "string" },
|
|
588
|
+
delta: {
|
|
589
|
+
type: "object",
|
|
590
|
+
required: ["value", "direction"],
|
|
591
|
+
additionalProperties: false,
|
|
592
|
+
properties: {
|
|
593
|
+
value: { type: "number" },
|
|
594
|
+
direction: { type: "string", enum: ["up", "down", "flat"] }
|
|
595
|
+
}
|
|
596
|
+
},
|
|
597
|
+
caption: { type: "string" }
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
};
|
|
603
|
+
|
|
604
|
+
// src/schema/widgets/ranking-table.ts
|
|
605
|
+
var RANKING_TABLE_SCHEMA = {
|
|
606
|
+
$id: "https://andpipe.dev/schema/widget/ranking-table/v1.json",
|
|
607
|
+
type: "object",
|
|
608
|
+
required: ["columns", "rows"],
|
|
609
|
+
additionalProperties: false,
|
|
610
|
+
properties: {
|
|
611
|
+
title: { type: "string" },
|
|
612
|
+
columns: {
|
|
613
|
+
type: "array",
|
|
614
|
+
minItems: 1,
|
|
615
|
+
items: {
|
|
616
|
+
type: "object",
|
|
617
|
+
required: ["key", "label"],
|
|
618
|
+
additionalProperties: false,
|
|
619
|
+
properties: {
|
|
620
|
+
key: { type: "string", minLength: 1 },
|
|
621
|
+
label: { type: "string" }
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
},
|
|
625
|
+
rows: {
|
|
626
|
+
type: "array",
|
|
627
|
+
items: {
|
|
628
|
+
type: "object",
|
|
629
|
+
required: ["cells"],
|
|
630
|
+
additionalProperties: false,
|
|
631
|
+
properties: {
|
|
632
|
+
rank: { type: "number" },
|
|
633
|
+
// cells は表の列値を自由キーで持つ意図的な領域。値は string | number に限定する。
|
|
634
|
+
cells: {
|
|
635
|
+
type: "object",
|
|
636
|
+
additionalProperties: { type: ["string", "number"] }
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
};
|
|
643
|
+
|
|
644
|
+
// src/schema/widgets/search-result-cards.ts
|
|
645
|
+
var SEARCH_RESULT_CARDS_SCHEMA = {
|
|
646
|
+
$id: "https://andpipe.dev/schema/widget/search-result-cards/v1.1.json",
|
|
647
|
+
type: "object",
|
|
648
|
+
required: ["results"],
|
|
649
|
+
additionalProperties: false,
|
|
650
|
+
properties: {
|
|
651
|
+
title: { type: "string", maxLength: 200 },
|
|
652
|
+
query: { type: "string", maxLength: 500 },
|
|
653
|
+
results: {
|
|
654
|
+
type: "array",
|
|
655
|
+
maxItems: 50,
|
|
656
|
+
items: {
|
|
657
|
+
type: "object",
|
|
658
|
+
required: ["id", "title"],
|
|
659
|
+
additionalProperties: false,
|
|
660
|
+
properties: {
|
|
661
|
+
id: { type: "string", minLength: 1, maxLength: 200 },
|
|
662
|
+
title: { type: "string", minLength: 1, maxLength: 300 },
|
|
663
|
+
summary: { type: "string", maxLength: 1e3 },
|
|
664
|
+
attributes: {
|
|
665
|
+
type: "object",
|
|
666
|
+
maxProperties: 20,
|
|
667
|
+
additionalProperties: WIDGET_SCALAR_SCHEMA
|
|
668
|
+
},
|
|
669
|
+
attributeLabels: {
|
|
670
|
+
type: "object",
|
|
671
|
+
maxProperties: 20,
|
|
672
|
+
additionalProperties: { type: "string", minLength: 1, maxLength: 120 }
|
|
673
|
+
},
|
|
674
|
+
badge: WIDGET_BADGE_SCHEMA
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
};
|
|
680
|
+
|
|
681
|
+
// src/schema/widgets/setup-draft-review.ts
|
|
682
|
+
var SETUP_DRAFT_CHANGE_SCHEMA = {
|
|
683
|
+
type: "object",
|
|
684
|
+
required: ["field"],
|
|
685
|
+
additionalProperties: false,
|
|
686
|
+
properties: {
|
|
687
|
+
field: { type: "string", minLength: 1, maxLength: 200 },
|
|
688
|
+
before: { type: "string", maxLength: 2e3 },
|
|
689
|
+
after: { type: "string", maxLength: 2e3 }
|
|
690
|
+
}
|
|
691
|
+
};
|
|
692
|
+
var SETUP_DRAFT_REVIEW_SCHEMA = {
|
|
693
|
+
$id: "https://andpipe.dev/schema/widget/setup-draft-review/v1.1.json",
|
|
694
|
+
type: "object",
|
|
695
|
+
required: ["title", "action", "requiresConfirmation", "sections"],
|
|
696
|
+
additionalProperties: false,
|
|
697
|
+
properties: {
|
|
698
|
+
title: { type: "string", minLength: 1, maxLength: 200 },
|
|
699
|
+
action: { type: "string", minLength: 1, maxLength: 200 },
|
|
700
|
+
requiresConfirmation: { const: true },
|
|
701
|
+
summary: { type: "string", maxLength: 2e3 },
|
|
702
|
+
status: { type: "string", enum: ["draft", "ready_for_review", "blocked"] },
|
|
703
|
+
warnings: {
|
|
704
|
+
type: "array",
|
|
705
|
+
maxItems: 20,
|
|
706
|
+
items: { type: "string", minLength: 1, maxLength: 500 }
|
|
707
|
+
},
|
|
708
|
+
sections: {
|
|
709
|
+
type: "array",
|
|
710
|
+
minItems: 1,
|
|
711
|
+
maxItems: 20,
|
|
712
|
+
items: {
|
|
713
|
+
type: "object",
|
|
714
|
+
required: ["title", "changes"],
|
|
715
|
+
additionalProperties: false,
|
|
716
|
+
properties: {
|
|
717
|
+
title: { type: "string", minLength: 1, maxLength: 200 },
|
|
718
|
+
changes: {
|
|
719
|
+
type: "array",
|
|
720
|
+
minItems: 1,
|
|
721
|
+
maxItems: 50,
|
|
722
|
+
items: SETUP_DRAFT_CHANGE_SCHEMA
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
};
|
|
729
|
+
|
|
730
|
+
// src/schema/widgets/trend-chart.ts
|
|
731
|
+
var TREND_CHART_SCHEMA = {
|
|
732
|
+
$id: "https://andpipe.dev/schema/widget/trend-chart/v1.json",
|
|
733
|
+
type: "object",
|
|
734
|
+
required: ["series"],
|
|
735
|
+
additionalProperties: false,
|
|
736
|
+
properties: {
|
|
737
|
+
title: { type: "string" },
|
|
738
|
+
unit: { type: "string" },
|
|
739
|
+
series: {
|
|
740
|
+
type: "array",
|
|
741
|
+
minItems: 1,
|
|
742
|
+
items: {
|
|
743
|
+
type: "object",
|
|
744
|
+
required: ["name", "points"],
|
|
745
|
+
additionalProperties: false,
|
|
746
|
+
properties: {
|
|
747
|
+
name: { type: "string", minLength: 1 },
|
|
748
|
+
points: {
|
|
749
|
+
type: "array",
|
|
750
|
+
items: {
|
|
751
|
+
type: "object",
|
|
752
|
+
required: ["x", "y"],
|
|
753
|
+
additionalProperties: false,
|
|
754
|
+
properties: {
|
|
755
|
+
x: { type: "string" },
|
|
756
|
+
y: { type: "number" }
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
};
|
|
765
|
+
|
|
766
|
+
// src/schema/widgets/index.ts
|
|
767
|
+
var WIDGET_REGISTRY_ENTRIES = {
|
|
768
|
+
kpi_cards: {
|
|
769
|
+
schema: KPI_CARDS_SCHEMA,
|
|
770
|
+
fallback: (data, locale) => kpiCardsFallback(data, locale)
|
|
771
|
+
},
|
|
772
|
+
ranking_table: {
|
|
773
|
+
schema: RANKING_TABLE_SCHEMA,
|
|
774
|
+
fallback: (data, locale) => rankingTableFallback(data, locale)
|
|
775
|
+
},
|
|
776
|
+
trend_chart: {
|
|
777
|
+
schema: TREND_CHART_SCHEMA,
|
|
778
|
+
fallback: (data, locale) => trendChartFallback(data, locale)
|
|
779
|
+
},
|
|
780
|
+
insight_list: {
|
|
781
|
+
schema: INSIGHT_LIST_SCHEMA,
|
|
782
|
+
fallback: (data, locale) => insightListFallback(data, locale)
|
|
783
|
+
},
|
|
784
|
+
confirmation_panel: {
|
|
785
|
+
schema: CONFIRMATION_PANEL_SCHEMA,
|
|
786
|
+
fallback: (data, locale) => confirmationPanelFallback(data, locale)
|
|
787
|
+
},
|
|
788
|
+
detail_card: {
|
|
789
|
+
schema: DETAIL_CARD_SCHEMA,
|
|
790
|
+
fallback: (data, locale) => detailCardFallback(data, locale)
|
|
791
|
+
},
|
|
792
|
+
search_result_cards: {
|
|
793
|
+
schema: SEARCH_RESULT_CARDS_SCHEMA,
|
|
794
|
+
fallback: (data, locale) => searchResultCardsFallback(data, locale)
|
|
795
|
+
},
|
|
796
|
+
setup_draft_review: {
|
|
797
|
+
schema: SETUP_DRAFT_REVIEW_SCHEMA,
|
|
798
|
+
fallback: (data, locale) => setupDraftReviewFallback(data, locale)
|
|
799
|
+
}
|
|
800
|
+
};
|
|
801
|
+
var WIDGET_REGISTRY = Object.freeze(
|
|
802
|
+
Object.assign(
|
|
803
|
+
/* @__PURE__ */ Object.create(null),
|
|
804
|
+
WIDGET_REGISTRY_ENTRIES
|
|
805
|
+
)
|
|
806
|
+
);
|
|
807
|
+
function getWidgetTemplateEntry(template) {
|
|
808
|
+
if (!isWidgetTemplate(template)) {
|
|
809
|
+
return void 0;
|
|
810
|
+
}
|
|
811
|
+
return WIDGET_REGISTRY[template];
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
// src/validate/ajv.ts
|
|
815
|
+
var import_ajv = __toESM(require("ajv"), 1);
|
|
816
|
+
var import_ajv_formats = __toESM(require("ajv-formats"), 1);
|
|
817
|
+
function createAjv() {
|
|
818
|
+
const ajv2 = new import_ajv.default({ allErrors: true, strict: false });
|
|
819
|
+
(0, import_ajv_formats.default)(ajv2);
|
|
820
|
+
return ajv2;
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
// src/validate/envelope.ts
|
|
824
|
+
var cached;
|
|
825
|
+
function getValidator() {
|
|
826
|
+
if (!cached) {
|
|
827
|
+
cached = createAjv().compile(WIDGET_ENVELOPE_V1_SCHEMA);
|
|
828
|
+
}
|
|
829
|
+
return cached;
|
|
830
|
+
}
|
|
831
|
+
function validateWidgetEnvelope(value) {
|
|
832
|
+
const validate = getValidator();
|
|
833
|
+
if (!validate(value)) {
|
|
834
|
+
const errors = (validate.errors ?? []).map((error) => {
|
|
835
|
+
const path = error.instancePath.length > 0 ? error.instancePath : "(root)";
|
|
836
|
+
return { path, message: `${path} ${error.message ?? "invalid"}` };
|
|
837
|
+
});
|
|
838
|
+
return { ok: false, errors };
|
|
839
|
+
}
|
|
840
|
+
const widget = value._meta.widget;
|
|
841
|
+
if (!isWidgetTemplateSupportedAtVersion(widget.template, widget.version)) {
|
|
842
|
+
return {
|
|
843
|
+
ok: false,
|
|
844
|
+
errors: [
|
|
845
|
+
{
|
|
846
|
+
path: "/_meta/widget/version",
|
|
847
|
+
message: `/_meta/widget/version is incompatible with template ${String(widget.template)}`
|
|
848
|
+
}
|
|
849
|
+
]
|
|
850
|
+
};
|
|
851
|
+
}
|
|
852
|
+
return { ok: true, errors: [] };
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
// src/validate/widget-data.ts
|
|
856
|
+
var ajv = createAjv();
|
|
857
|
+
var cache = /* @__PURE__ */ new Map();
|
|
858
|
+
function getValidator2(template) {
|
|
859
|
+
const cached2 = cache.get(template);
|
|
860
|
+
if (cached2) {
|
|
861
|
+
return cached2;
|
|
862
|
+
}
|
|
863
|
+
const entry = getWidgetTemplateEntry(template);
|
|
864
|
+
if (!entry) {
|
|
865
|
+
throw new Error(`unreachable unknown widget template: ${template}`);
|
|
866
|
+
}
|
|
867
|
+
const validator = ajv.compile(entry.schema);
|
|
868
|
+
cache.set(template, validator);
|
|
869
|
+
return validator;
|
|
870
|
+
}
|
|
871
|
+
function validateWidgetData(template, value) {
|
|
872
|
+
if (!isWidgetTemplate(template)) {
|
|
873
|
+
const templateName = typeof template === "symbol" ? template.toString() : String(template);
|
|
874
|
+
return {
|
|
875
|
+
ok: false,
|
|
876
|
+
errors: [{ path: "template", message: `\u672A\u77E5\u306E widget template: ${templateName}` }]
|
|
877
|
+
};
|
|
878
|
+
}
|
|
879
|
+
const validate = getValidator2(template);
|
|
880
|
+
if (validate(value)) {
|
|
881
|
+
return { ok: true, errors: [] };
|
|
882
|
+
}
|
|
883
|
+
const errors = (validate.errors ?? []).map((error) => {
|
|
884
|
+
const path = error.instancePath.length > 0 ? error.instancePath : "(root)";
|
|
885
|
+
return { path, message: `${path} ${error.message ?? "invalid"}` };
|
|
886
|
+
});
|
|
887
|
+
return { ok: false, errors };
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
// src/build/builder.ts
|
|
891
|
+
var WidgetSchemaError = class extends Error {
|
|
892
|
+
template;
|
|
893
|
+
issues;
|
|
894
|
+
constructor(template, message, issues = []) {
|
|
895
|
+
super(message);
|
|
896
|
+
this.name = "WidgetSchemaError";
|
|
897
|
+
this.template = describeWidgetTemplate(template);
|
|
898
|
+
this.issues = issues;
|
|
899
|
+
}
|
|
900
|
+
};
|
|
901
|
+
function describeWidgetTemplate(template) {
|
|
902
|
+
return typeof template === "symbol" ? template.toString() : String(template);
|
|
903
|
+
}
|
|
904
|
+
function buildWidgetResponse(input) {
|
|
905
|
+
const { template, data } = input;
|
|
906
|
+
const forbidden = [.../* @__PURE__ */ new Set([...DEFAULT_FORBIDDEN_PII, ...input.forbidden ?? []])];
|
|
907
|
+
const version = input.version ?? WIDGET_SCHEMA_VERSION;
|
|
908
|
+
const locale = input.locale ?? import_localization9.DEFAULT_LOCALE;
|
|
909
|
+
const entry = getWidgetTemplateEntry(template);
|
|
910
|
+
if (!entry) {
|
|
911
|
+
throw new WidgetSchemaError(
|
|
912
|
+
template,
|
|
913
|
+
`\u672A\u77E5\u306E widget template: ${describeWidgetTemplate(template)}`
|
|
914
|
+
);
|
|
915
|
+
}
|
|
916
|
+
if (!isWidgetTemplateSupportedAtVersion(template, version)) {
|
|
917
|
+
throw new WidgetSchemaError(
|
|
918
|
+
template,
|
|
919
|
+
`\u672A\u5BFE\u5FDC\u306E widget template/version: ${describeWidgetTemplate(template)}@${version}`
|
|
920
|
+
);
|
|
921
|
+
}
|
|
922
|
+
const dataCheck = validateWidgetData(template, data);
|
|
923
|
+
if (!dataCheck.ok) {
|
|
924
|
+
throw new WidgetSchemaError(
|
|
925
|
+
template,
|
|
926
|
+
`widget data \u304C ${template} schema \u306B\u9055\u53CD\u3057\u3066\u3044\u307E\u3059`,
|
|
927
|
+
dataCheck.errors.map((error) => error.message)
|
|
928
|
+
);
|
|
929
|
+
}
|
|
930
|
+
const { value: redacted, redactions } = redactWidgetPayload(data, forbidden);
|
|
931
|
+
const textFallback = entry.fallback(redacted, locale);
|
|
932
|
+
if (textFallback.length === 0) {
|
|
933
|
+
throw new WidgetSchemaError(template, `textFallback \u304C\u7A7A\u3067\u3059: ${template}`);
|
|
934
|
+
}
|
|
935
|
+
const content = [{ type: "text", text: textFallback }];
|
|
936
|
+
const envelope = {
|
|
937
|
+
structuredContent: redacted,
|
|
938
|
+
content,
|
|
939
|
+
textFallback,
|
|
940
|
+
_meta: {
|
|
941
|
+
widget: { template, version },
|
|
942
|
+
fallback: { type: "markdown" }
|
|
943
|
+
}
|
|
944
|
+
};
|
|
945
|
+
if (!isWidgetRenderPayloadWithinTokenLimit({
|
|
946
|
+
template,
|
|
947
|
+
version,
|
|
948
|
+
structuredContent: redacted,
|
|
949
|
+
textFallback
|
|
950
|
+
})) {
|
|
951
|
+
throw new WidgetSchemaError(
|
|
952
|
+
template,
|
|
953
|
+
`widget render payload \u304C64 KiB token\u4E0A\u9650\u3092\u8D85\u3048\u3066\u3044\u307E\u3059: ${template}`
|
|
954
|
+
);
|
|
955
|
+
}
|
|
956
|
+
const envelopeCheck = validateWidgetEnvelope(envelope);
|
|
957
|
+
if (!envelopeCheck.ok) {
|
|
958
|
+
throw new WidgetSchemaError(
|
|
959
|
+
template,
|
|
960
|
+
`\u751F\u6210\u3057\u305F envelope \u304C schema \u306B\u9055\u53CD\u3057\u3066\u3044\u307E\u3059: ${template}`,
|
|
961
|
+
envelopeCheck.errors.map((error) => error.message)
|
|
962
|
+
);
|
|
963
|
+
}
|
|
964
|
+
return { envelope, redactions };
|
|
965
|
+
}
|
|
966
|
+
function buildKpiCards(data, options) {
|
|
967
|
+
return buildWidgetResponse({ template: "kpi_cards", data, ...options });
|
|
968
|
+
}
|
|
969
|
+
function buildRankingTable(data, options) {
|
|
970
|
+
return buildWidgetResponse({ template: "ranking_table", data, ...options });
|
|
971
|
+
}
|
|
972
|
+
function buildTrendChart(data, options) {
|
|
973
|
+
return buildWidgetResponse({ template: "trend_chart", data, ...options });
|
|
974
|
+
}
|
|
975
|
+
function buildInsightList(data, options) {
|
|
976
|
+
return buildWidgetResponse({ template: "insight_list", data, ...options });
|
|
977
|
+
}
|
|
978
|
+
function buildConfirmationPanel(data, options) {
|
|
979
|
+
return buildWidgetResponse({ template: "confirmation_panel", data, ...options });
|
|
980
|
+
}
|
|
981
|
+
function buildDetailCard(data, options) {
|
|
982
|
+
return buildWidgetResponse({ template: "detail_card", data, ...options });
|
|
983
|
+
}
|
|
984
|
+
function buildSearchResultCards(data, options) {
|
|
985
|
+
return buildWidgetResponse({ template: "search_result_cards", data, ...options });
|
|
986
|
+
}
|
|
987
|
+
function buildSetupDraftReview(data, options) {
|
|
988
|
+
return buildWidgetResponse({ template: "setup_draft_review", data, ...options });
|
|
989
|
+
}
|
|
990
|
+
|
|
991
|
+
// src/index.ts
|
|
992
|
+
var packageName = "@andpipes/widget-sdk";
|
|
993
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
994
|
+
0 && (module.exports = {
|
|
995
|
+
ADVANCED_WIDGET_TEMPLATES,
|
|
996
|
+
CONFIRMATION_PANEL_SCHEMA,
|
|
997
|
+
DEFAULT_FORBIDDEN_PII,
|
|
998
|
+
DETAIL_CARD_SCHEMA,
|
|
999
|
+
INSIGHT_LIST_SCHEMA,
|
|
1000
|
+
KPI_CARDS_SCHEMA,
|
|
1001
|
+
LEGACY_WIDGET_TEMPLATES,
|
|
1002
|
+
RANKING_TABLE_SCHEMA,
|
|
1003
|
+
SEARCH_RESULT_CARDS_SCHEMA,
|
|
1004
|
+
SEMVER_PATTERN,
|
|
1005
|
+
SETUP_DRAFT_REVIEW_SCHEMA,
|
|
1006
|
+
TREND_CHART_SCHEMA,
|
|
1007
|
+
WIDGET_ENVELOPE_V1_SCHEMA,
|
|
1008
|
+
WIDGET_REGISTRY,
|
|
1009
|
+
WIDGET_RENDER_TOKEN_MAX_BYTES,
|
|
1010
|
+
WIDGET_SCHEMA_LEGACY_VERSION,
|
|
1011
|
+
WIDGET_SCHEMA_VERSION,
|
|
1012
|
+
WIDGET_SCHEMA_VERSIONS,
|
|
1013
|
+
WIDGET_TEMPLATES,
|
|
1014
|
+
WidgetSchemaError,
|
|
1015
|
+
buildConfirmationPanel,
|
|
1016
|
+
buildDetailCard,
|
|
1017
|
+
buildInsightList,
|
|
1018
|
+
buildKpiCards,
|
|
1019
|
+
buildRankingTable,
|
|
1020
|
+
buildSearchResultCards,
|
|
1021
|
+
buildSetupDraftReview,
|
|
1022
|
+
buildTrendChart,
|
|
1023
|
+
buildWidgetResponse,
|
|
1024
|
+
confirmationPanelFallback,
|
|
1025
|
+
detailCardFallback,
|
|
1026
|
+
estimateWidgetRenderTokenBytes,
|
|
1027
|
+
getWidgetTemplateEntry,
|
|
1028
|
+
insightListFallback,
|
|
1029
|
+
isAdvancedWidgetTemplate,
|
|
1030
|
+
isWidgetRenderPayloadWithinTokenLimit,
|
|
1031
|
+
isWidgetSchemaVersion,
|
|
1032
|
+
isWidgetTemplate,
|
|
1033
|
+
isWidgetTemplateSupportedAtVersion,
|
|
1034
|
+
kpiCardsFallback,
|
|
1035
|
+
packageName,
|
|
1036
|
+
rankingTableFallback,
|
|
1037
|
+
redactWidgetPayload,
|
|
1038
|
+
searchResultCardsFallback,
|
|
1039
|
+
setupDraftReviewFallback,
|
|
1040
|
+
trendChartFallback,
|
|
1041
|
+
validateWidgetData,
|
|
1042
|
+
validateWidgetEnvelope
|
|
1043
|
+
});
|