@zhin.js/console-contract 1.1.3 → 1.1.5
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/lib/page.d.ts +441 -0
- package/lib/page.js +866 -1
- package/package.json +1 -1
- package/src/page.ts +1532 -1
package/lib/page.js
CHANGED
|
@@ -4,7 +4,19 @@ export function normalizeClientModuleArtifact(value, label) {
|
|
|
4
4
|
const hash = optionalString(input.hash, `${label} hash`);
|
|
5
5
|
if (!module || !hash)
|
|
6
6
|
throw new TypeError(`${label} must include module and hash`);
|
|
7
|
-
|
|
7
|
+
const blueprint = isConsoleBlueprint(input.blueprint) ? input.blueprint : undefined;
|
|
8
|
+
return Object.freeze({
|
|
9
|
+
module,
|
|
10
|
+
hash,
|
|
11
|
+
metadata: input.metadata,
|
|
12
|
+
...(blueprint !== undefined ? { blueprint } : {}),
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
export function isConsoleBlueprint(value) {
|
|
16
|
+
if (!value || (typeof value !== 'object' && typeof value !== 'function'))
|
|
17
|
+
return false;
|
|
18
|
+
const kind = value.kind;
|
|
19
|
+
return kind === 'zhin.console.page-blueprint' || kind === 'zhin.console.adapter-page';
|
|
8
20
|
}
|
|
9
21
|
export function definePage(metadata = {}) {
|
|
10
22
|
return normalizePageInput(metadata);
|
|
@@ -129,3 +141,856 @@ function pageTitleTranslations(value) {
|
|
|
129
141
|
}
|
|
130
142
|
return Object.freeze(output);
|
|
131
143
|
}
|
|
144
|
+
export function isDeclarativeValueExpr(value) {
|
|
145
|
+
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
146
|
+
return false;
|
|
147
|
+
const rec = value;
|
|
148
|
+
return (typeof rec.$path === 'string' ||
|
|
149
|
+
typeof rec.$count === 'string' ||
|
|
150
|
+
(rec.$sum !== null && typeof rec.$sum === 'object') ||
|
|
151
|
+
(rec.$format !== undefined && Array.isArray(rec.$args ?? rec.args)) ||
|
|
152
|
+
(rec.$map !== null && typeof rec.$map === 'object') ||
|
|
153
|
+
(rec.$join !== null && typeof rec.$join === 'object') ||
|
|
154
|
+
Array.isArray(rec.$coalesce));
|
|
155
|
+
}
|
|
156
|
+
export function resolveDataPath(data, path) {
|
|
157
|
+
if (!data || typeof data !== 'object' || !path)
|
|
158
|
+
return undefined;
|
|
159
|
+
const record = data;
|
|
160
|
+
if (Object.prototype.hasOwnProperty.call(record, path))
|
|
161
|
+
return record[path];
|
|
162
|
+
const segments = path.split('.').filter(Boolean);
|
|
163
|
+
let cursor = data;
|
|
164
|
+
for (const seg of segments) {
|
|
165
|
+
if (cursor === null || cursor === undefined)
|
|
166
|
+
return undefined;
|
|
167
|
+
if (Array.isArray(cursor) && seg === 'length')
|
|
168
|
+
return cursor.length;
|
|
169
|
+
if (typeof cursor !== 'object')
|
|
170
|
+
return undefined;
|
|
171
|
+
cursor = Object.prototype.hasOwnProperty.call(cursor, seg)
|
|
172
|
+
? cursor[seg]
|
|
173
|
+
: undefined;
|
|
174
|
+
}
|
|
175
|
+
return cursor;
|
|
176
|
+
}
|
|
177
|
+
export function evaluateDeclarativeValue(exprOrKey, data, locale = 'en') {
|
|
178
|
+
if (exprOrKey === null || exprOrKey === undefined)
|
|
179
|
+
return undefined;
|
|
180
|
+
if (typeof exprOrKey === 'string') {
|
|
181
|
+
return resolveDataPath(data, exprOrKey);
|
|
182
|
+
}
|
|
183
|
+
if (!isDeclarativeValueExpr(exprOrKey)) {
|
|
184
|
+
return exprOrKey;
|
|
185
|
+
}
|
|
186
|
+
const rec = exprOrKey;
|
|
187
|
+
if (typeof rec.$path === 'string') {
|
|
188
|
+
const val = resolveDataPath(data, rec.$path);
|
|
189
|
+
return val !== undefined && val !== null && val !== '' ? val : rec.fallback;
|
|
190
|
+
}
|
|
191
|
+
if (typeof rec.$count === 'string') {
|
|
192
|
+
const target = resolveDataPath(data, rec.$count);
|
|
193
|
+
if (Array.isArray(target))
|
|
194
|
+
return target.length;
|
|
195
|
+
if (target && typeof target === 'object')
|
|
196
|
+
return Object.keys(target).length;
|
|
197
|
+
return typeof target === 'number' ? target : 0;
|
|
198
|
+
}
|
|
199
|
+
if (rec.$sum && typeof rec.$sum === 'object') {
|
|
200
|
+
const sumSpec = rec.$sum;
|
|
201
|
+
const list = sumSpec.path ? resolveDataPath(data, sumSpec.path) : undefined;
|
|
202
|
+
if (!Array.isArray(list) || !sumSpec.field)
|
|
203
|
+
return 0;
|
|
204
|
+
return list.reduce((acc, item) => {
|
|
205
|
+
let raw = resolveDataPath(item, sumSpec.field);
|
|
206
|
+
if (raw === undefined && sumSpec.field === 'pendingCount') {
|
|
207
|
+
raw = resolveDataPath(item, 'drafts.length');
|
|
208
|
+
}
|
|
209
|
+
const n = Number(raw);
|
|
210
|
+
return acc + (Number.isFinite(n) ? n : 0);
|
|
211
|
+
}, 0);
|
|
212
|
+
}
|
|
213
|
+
const formatArgs = Array.isArray(rec.args)
|
|
214
|
+
? rec.args
|
|
215
|
+
: Array.isArray(rec.$args)
|
|
216
|
+
? rec.$args
|
|
217
|
+
: undefined;
|
|
218
|
+
if (rec.$format !== undefined && formatArgs) {
|
|
219
|
+
const template = resolveDeclarativeText(rec.$format, locale);
|
|
220
|
+
const args = formatArgs.map((p) => resolveDataPath(data, p));
|
|
221
|
+
if (args.length > 0 && args.every((v) => v === undefined || v === null || v === '')) {
|
|
222
|
+
return undefined;
|
|
223
|
+
}
|
|
224
|
+
return template.replace(/\{(\d+)\}/gu, (_m, idx) => {
|
|
225
|
+
const v = args[Number(idx)];
|
|
226
|
+
return v === undefined || v === null ? '' : String(v);
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
if (rec.$map && typeof rec.$map === 'object') {
|
|
230
|
+
const mapSpec = rec.$map;
|
|
231
|
+
const rawVal = mapSpec.path ? resolveDataPath(data, mapSpec.path) : undefined;
|
|
232
|
+
if (rawVal !== undefined && rawVal !== null && mapSpec.cases) {
|
|
233
|
+
const key = String(rawVal);
|
|
234
|
+
if (Object.prototype.hasOwnProperty.call(mapSpec.cases, key)) {
|
|
235
|
+
return resolveDeclarativeText(mapSpec.cases[key], locale);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
if (mapSpec.default !== undefined)
|
|
239
|
+
return resolveDeclarativeText(mapSpec.default, locale);
|
|
240
|
+
return rawVal;
|
|
241
|
+
}
|
|
242
|
+
if (rec.$join && typeof rec.$join === 'object') {
|
|
243
|
+
const joinSpec = rec.$join;
|
|
244
|
+
const list = joinSpec.path ? resolveDataPath(data, joinSpec.path) : undefined;
|
|
245
|
+
if (!Array.isArray(list) || list.length === 0) {
|
|
246
|
+
return joinSpec.empty !== undefined ? resolveDeclarativeText(joinSpec.empty, locale) : '—';
|
|
247
|
+
}
|
|
248
|
+
return list.map((item) => String(item ?? '')).join(joinSpec.sep ?? ', ');
|
|
249
|
+
}
|
|
250
|
+
if (Array.isArray(rec.$coalesce)) {
|
|
251
|
+
for (const candidate of rec.$coalesce) {
|
|
252
|
+
const val = resolveDataPath(data, candidate);
|
|
253
|
+
if (val !== undefined && val !== null && val !== '')
|
|
254
|
+
return val;
|
|
255
|
+
}
|
|
256
|
+
return rec.fallback;
|
|
257
|
+
}
|
|
258
|
+
return undefined;
|
|
259
|
+
}
|
|
260
|
+
export function resolveDeclarativeText(text, preferredLocale = 'en') {
|
|
261
|
+
if (!text)
|
|
262
|
+
return '';
|
|
263
|
+
if (typeof text === 'string')
|
|
264
|
+
return text;
|
|
265
|
+
if (preferredLocale === 'en') {
|
|
266
|
+
return text.en ?? text['zh-CN'] ?? text.zh;
|
|
267
|
+
}
|
|
268
|
+
return text['zh-CN'] ?? text.zh ?? text.en ?? '';
|
|
269
|
+
}
|
|
270
|
+
export function buildBlueprintFromSpec(input) {
|
|
271
|
+
if (input?.kind === 'zhin.console.page-blueprint') {
|
|
272
|
+
const bp = input;
|
|
273
|
+
return Object.freeze({
|
|
274
|
+
kind: 'zhin.console.page-blueprint',
|
|
275
|
+
version: 1,
|
|
276
|
+
title: bp.title,
|
|
277
|
+
...(bp.titleI18n ? { titleI18n: Object.freeze({ ...bp.titleI18n }) } : {}),
|
|
278
|
+
...(bp.description !== undefined ? { description: bp.description } : {}),
|
|
279
|
+
dataSource: Object.freeze({ action: bp.dataSource?.action ?? 'status' }),
|
|
280
|
+
sections: Object.freeze([...(bp.sections ?? [])]),
|
|
281
|
+
...(bp.spec !== undefined ? { spec: bp.spec } : {}),
|
|
282
|
+
...(bp.reviewSpec !== undefined ? { reviewSpec: bp.reviewSpec } : {}),
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
const spec = input;
|
|
286
|
+
const title = resolveDeclarativeText(spec.title, 'en');
|
|
287
|
+
const titleI18n = typeof spec.title === 'object' && spec.title !== null
|
|
288
|
+
? {
|
|
289
|
+
'zh-CN': spec.title['zh-CN'] ?? spec.title.zh,
|
|
290
|
+
...(spec.title.en ? { en: spec.title.en } : {}),
|
|
291
|
+
}
|
|
292
|
+
: undefined;
|
|
293
|
+
const description = spec.description ? resolveDeclarativeText(spec.description, 'en') : undefined;
|
|
294
|
+
const dataSource = Object.freeze({ action: spec.statusMethod ?? 'status' });
|
|
295
|
+
const sections = [];
|
|
296
|
+
if (Array.isArray(spec.stats) && spec.stats.length > 0) {
|
|
297
|
+
sections.push(Object.freeze({
|
|
298
|
+
type: 'stats',
|
|
299
|
+
id: 'runtime-stats',
|
|
300
|
+
title: 'Runtime Stats',
|
|
301
|
+
items: Object.freeze(spec.stats.map((stat, idx) => Object.freeze({
|
|
302
|
+
key: stat.key ?? (typeof stat.value === 'string' ? stat.value : `stat-${idx}`),
|
|
303
|
+
label: resolveDeclarativeText(stat.label, 'en'),
|
|
304
|
+
...(typeof stat.value === 'string' ? { dataKey: stat.value } : {}),
|
|
305
|
+
...(isDeclarativeValueExpr(stat.value) ? { expr: stat.value } : {}),
|
|
306
|
+
}))),
|
|
307
|
+
}));
|
|
308
|
+
}
|
|
309
|
+
for (const [idx, sec] of (spec.sections ?? []).entries()) {
|
|
310
|
+
const id = sec.id ?? `section-${idx}`;
|
|
311
|
+
const secTitle = resolveDeclarativeText(sec.title, 'en');
|
|
312
|
+
if (sec.columns !== undefined || sec.items !== undefined) {
|
|
313
|
+
const preferredRowKey = sec.columns?.find((c) => c.key === 'id' || c.key === 'key' || c.key === 'ownerKey')?.key ?? 'id';
|
|
314
|
+
sections.push(Object.freeze({
|
|
315
|
+
type: 'table',
|
|
316
|
+
id,
|
|
317
|
+
title: secTitle,
|
|
318
|
+
...(typeof sec.items === 'string' ? { dataKey: sec.items } : {}),
|
|
319
|
+
rowKey: preferredRowKey,
|
|
320
|
+
columns: Object.freeze((sec.columns ?? []).map((col) => Object.freeze({
|
|
321
|
+
key: col.key,
|
|
322
|
+
label: resolveDeclarativeText(col.label, 'en'),
|
|
323
|
+
}))),
|
|
324
|
+
}));
|
|
325
|
+
}
|
|
326
|
+
else if (sec.entries !== undefined) {
|
|
327
|
+
sections.push(Object.freeze({
|
|
328
|
+
type: 'entries',
|
|
329
|
+
id,
|
|
330
|
+
title: secTitle,
|
|
331
|
+
...(Array.isArray(sec.entries)
|
|
332
|
+
? {
|
|
333
|
+
entries: Object.freeze(sec.entries.map((e) => {
|
|
334
|
+
const inferredDataKey = e.dataKey ??
|
|
335
|
+
(isDeclarativeValueExpr(e.value) &&
|
|
336
|
+
typeof e.value.$path === 'string'
|
|
337
|
+
? e.value.$path
|
|
338
|
+
: undefined);
|
|
339
|
+
return Object.freeze({
|
|
340
|
+
label: resolveDeclarativeText(e.label, 'en'),
|
|
341
|
+
value: isDeclarativeValueExpr(e.value) ? '' : String(e.value ?? ''),
|
|
342
|
+
...(inferredDataKey ? { dataKey: inferredDataKey } : {}),
|
|
343
|
+
...(isDeclarativeValueExpr(e.value) ? { expr: e.value } : {}),
|
|
344
|
+
});
|
|
345
|
+
})),
|
|
346
|
+
}
|
|
347
|
+
: {}),
|
|
348
|
+
}));
|
|
349
|
+
}
|
|
350
|
+
else {
|
|
351
|
+
sections.push(Object.freeze({
|
|
352
|
+
type: 'custom',
|
|
353
|
+
id,
|
|
354
|
+
...(secTitle ? { title: secTitle } : {}),
|
|
355
|
+
}));
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
if (Array.isArray(spec.actions) && spec.actions.length > 0) {
|
|
359
|
+
sections.push(Object.freeze({
|
|
360
|
+
type: 'actions',
|
|
361
|
+
id: 'actions',
|
|
362
|
+
title: 'Actions',
|
|
363
|
+
items: Object.freeze(spec.actions.map((act) => Object.freeze({
|
|
364
|
+
label: resolveDeclarativeText(act.label, 'en'),
|
|
365
|
+
action: act.name,
|
|
366
|
+
name: act.name,
|
|
367
|
+
...(act.description
|
|
368
|
+
? { description: resolveDeclarativeText(act.description, 'en') }
|
|
369
|
+
: {}),
|
|
370
|
+
...(act.variant ? { variant: act.variant } : {}),
|
|
371
|
+
...(act.confirm !== undefined ? { confirm: act.confirm } : {}),
|
|
372
|
+
...(act.confirmLabel !== undefined ? { confirmLabel: act.confirmLabel } : {}),
|
|
373
|
+
...(act.fields !== undefined ? { fields: act.fields } : {}),
|
|
374
|
+
...(act.params !== undefined ? { params: act.params } : {}),
|
|
375
|
+
...(act.disabled !== undefined ? { disabled: act.disabled } : {}),
|
|
376
|
+
...(act.successMessage !== undefined ? { successMessage: act.successMessage } : {}),
|
|
377
|
+
}))),
|
|
378
|
+
}));
|
|
379
|
+
}
|
|
380
|
+
const rawReviewSpec = spec.reviewSpec;
|
|
381
|
+
return Object.freeze({
|
|
382
|
+
kind: 'zhin.console.page-blueprint',
|
|
383
|
+
version: 1,
|
|
384
|
+
title,
|
|
385
|
+
...(titleI18n ? { titleI18n: Object.freeze(titleI18n) } : {}),
|
|
386
|
+
...(description !== undefined ? { description } : {}),
|
|
387
|
+
dataSource,
|
|
388
|
+
sections: Object.freeze(sections),
|
|
389
|
+
spec,
|
|
390
|
+
...(rawReviewSpec !== undefined ? { reviewSpec: rawReviewSpec } : {}),
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
export function definePageBlueprint(spec) {
|
|
394
|
+
const blueprint = buildBlueprintFromSpec(spec);
|
|
395
|
+
function BlueprintPage(_props) {
|
|
396
|
+
return blueprint;
|
|
397
|
+
}
|
|
398
|
+
return Object.freeze(Object.assign(BlueprintPage, blueprint));
|
|
399
|
+
}
|
|
400
|
+
export const createDeclarativePage = definePageBlueprint;
|
|
401
|
+
export function defineAdapterPage(adapterKey) {
|
|
402
|
+
const validKey = optionalString(adapterKey, 'Adapter page adapterKey');
|
|
403
|
+
if (!validKey)
|
|
404
|
+
throw new TypeError('Adapter page adapterKey must be a non-empty string');
|
|
405
|
+
const blueprint = Object.freeze({
|
|
406
|
+
kind: 'zhin.console.adapter-page',
|
|
407
|
+
version: 1,
|
|
408
|
+
adapterKey: validKey,
|
|
409
|
+
dataSource: Object.freeze({ action: 'status' }),
|
|
410
|
+
});
|
|
411
|
+
function AdapterConsoleRoute(_props) {
|
|
412
|
+
return blueprint;
|
|
413
|
+
}
|
|
414
|
+
return Object.freeze(Object.assign(AdapterConsoleRoute, blueprint));
|
|
415
|
+
}
|
|
416
|
+
export const createAdapterConsolePage = defineAdapterPage;
|
|
417
|
+
export function defineCommentReviewPage(reviewSpec) {
|
|
418
|
+
const zhTitle = `${reviewSpec.platform.zh}评论审批`;
|
|
419
|
+
const enTitle = `${reviewSpec.platform.en} comment review`;
|
|
420
|
+
const loginActions = reviewSpec.loginMode === 'cookie-check'
|
|
421
|
+
? [{ name: 'login-check', label: { zh: '重新检查登录', en: 'Check login again' } }]
|
|
422
|
+
: [
|
|
423
|
+
{ name: 'login', label: { zh: '扫码登录', en: 'Sign in with QR code' }, variant: 'primary' },
|
|
424
|
+
{ name: 'login-cancel', label: { zh: '取消扫码', en: 'Cancel QR login' }, variant: 'ghost' },
|
|
425
|
+
{ name: 'logout', label: { zh: '退出登录', en: 'Sign out' }, variant: 'secondary' },
|
|
426
|
+
];
|
|
427
|
+
const blueprint = buildBlueprintFromSpec({
|
|
428
|
+
pluginId: reviewSpec.adapter,
|
|
429
|
+
title: { zh: zhTitle, 'zh-CN': zhTitle, en: enTitle },
|
|
430
|
+
mark: reviewSpec.mark,
|
|
431
|
+
statusMethod: 'status',
|
|
432
|
+
pollMs: reviewSpec.pollMs ?? 10_000,
|
|
433
|
+
...(reviewSpec.dictionary ? { dictionary: reviewSpec.dictionary } : {}),
|
|
434
|
+
stats: [
|
|
435
|
+
{
|
|
436
|
+
key: 'endpoints',
|
|
437
|
+
label: { zh: '账号数', en: 'Accounts' },
|
|
438
|
+
value: { $count: 'endpoints' },
|
|
439
|
+
},
|
|
440
|
+
{
|
|
441
|
+
key: 'pendingDrafts',
|
|
442
|
+
label: { zh: '待审草稿', en: 'Pending drafts' },
|
|
443
|
+
value: { $sum: { path: 'endpoints', field: 'pendingCount' } },
|
|
444
|
+
},
|
|
445
|
+
],
|
|
446
|
+
sections: [
|
|
447
|
+
{
|
|
448
|
+
id: 'endpoints',
|
|
449
|
+
title: { zh: '评论账号与待审草稿', en: 'Accounts & Pending Drafts' },
|
|
450
|
+
items: 'endpoints',
|
|
451
|
+
columns: [
|
|
452
|
+
{ key: 'id', label: { zh: '账号', en: 'Account' } },
|
|
453
|
+
{ key: 'sendMode', label: { zh: '发送模式', en: 'Send Mode' } },
|
|
454
|
+
{ key: 'pendingCount', label: { zh: '待审数', en: 'Pending' } },
|
|
455
|
+
],
|
|
456
|
+
},
|
|
457
|
+
],
|
|
458
|
+
actions: [
|
|
459
|
+
...loginActions,
|
|
460
|
+
{
|
|
461
|
+
name: 'edit',
|
|
462
|
+
label: { zh: '修改草稿', en: 'Edit draft' },
|
|
463
|
+
fields: [
|
|
464
|
+
{ key: 'id', label: { zh: '草稿 ID', en: 'Draft ID' }, required: true },
|
|
465
|
+
{ key: 'message', label: { zh: '回复内容', en: 'Reply content' }, required: true },
|
|
466
|
+
],
|
|
467
|
+
},
|
|
468
|
+
{
|
|
469
|
+
name: 'send',
|
|
470
|
+
label: { zh: '发送草稿', en: 'Send draft' },
|
|
471
|
+
variant: 'primary',
|
|
472
|
+
fields: [{ key: 'id', label: { zh: '草稿 ID', en: 'Draft ID' }, required: true }],
|
|
473
|
+
},
|
|
474
|
+
{
|
|
475
|
+
name: 'drop',
|
|
476
|
+
label: { zh: '丢弃草稿', en: 'Discard draft' },
|
|
477
|
+
variant: 'danger',
|
|
478
|
+
confirm: { zh: '确认丢弃该草稿?', en: 'Discard this draft?' },
|
|
479
|
+
fields: [{ key: 'id', label: { zh: '草稿 ID', en: 'Draft ID' }, required: true }],
|
|
480
|
+
},
|
|
481
|
+
],
|
|
482
|
+
});
|
|
483
|
+
const enriched = Object.freeze({
|
|
484
|
+
...blueprint,
|
|
485
|
+
reviewSpec,
|
|
486
|
+
});
|
|
487
|
+
function CommentReviewBlueprintPage(_props) {
|
|
488
|
+
return enriched;
|
|
489
|
+
}
|
|
490
|
+
return Object.freeze(Object.assign(CommentReviewBlueprintPage, enriched));
|
|
491
|
+
}
|
|
492
|
+
export const createCommentReviewPage = defineCommentReviewPage;
|
|
493
|
+
function createProjectionContext(data, locale, pluginId, dictionary) {
|
|
494
|
+
const t = (key, ...values) => {
|
|
495
|
+
const template = locale === 'en' && dictionary?.[key] ? dictionary[key] : key;
|
|
496
|
+
return template.replace(/\{(\d+)\}/gu, (_m, idx) => String(values[Number(idx)] ?? ''));
|
|
497
|
+
};
|
|
498
|
+
return {
|
|
499
|
+
pluginId,
|
|
500
|
+
locale,
|
|
501
|
+
t,
|
|
502
|
+
resource: {
|
|
503
|
+
state: data === undefined ? 'empty' : 'ready',
|
|
504
|
+
data,
|
|
505
|
+
error: undefined,
|
|
506
|
+
lastSuccessAt: Date.now(),
|
|
507
|
+
refreshing: false,
|
|
508
|
+
refresh: async () => true,
|
|
509
|
+
mutate: () => undefined,
|
|
510
|
+
},
|
|
511
|
+
call: async () => undefined,
|
|
512
|
+
refresh: async () => true,
|
|
513
|
+
notify: () => undefined,
|
|
514
|
+
};
|
|
515
|
+
}
|
|
516
|
+
function formatRenderableValue(value) {
|
|
517
|
+
if (value === null || value === undefined)
|
|
518
|
+
return '—';
|
|
519
|
+
if (typeof value === 'string')
|
|
520
|
+
return value || '—';
|
|
521
|
+
if (typeof value === 'number' || typeof value === 'boolean')
|
|
522
|
+
return String(value);
|
|
523
|
+
if (Array.isArray(value))
|
|
524
|
+
return value.map((item) => formatRenderableValue(item)).join(', ');
|
|
525
|
+
if (typeof value === 'object') {
|
|
526
|
+
const record = value;
|
|
527
|
+
if (typeof record.label === 'string')
|
|
528
|
+
return record.label;
|
|
529
|
+
if (typeof record.text === 'string')
|
|
530
|
+
return record.text;
|
|
531
|
+
try {
|
|
532
|
+
return JSON.stringify(value);
|
|
533
|
+
}
|
|
534
|
+
catch {
|
|
535
|
+
return String(value);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
return String(value);
|
|
539
|
+
}
|
|
540
|
+
/**
|
|
541
|
+
* Projects any Server-Driven Console Blueprint (`definePageBlueprint`, `defineAdapterPage`,
|
|
542
|
+
* `defineCommentReviewPage`, or `DeclarativePageSpec`) + runtime status payload into a
|
|
543
|
+
* structured IM Card / Markdown / Segment payload ready for `Message.$reply` or `Adapter.sendMessage`.
|
|
544
|
+
*/
|
|
545
|
+
export function projectBlueprintToImCard(input, data, options = {}) {
|
|
546
|
+
const locale = options.locale ?? 'zh-CN';
|
|
547
|
+
const maxRows = Math.max(1, options.maxRows ?? 10);
|
|
548
|
+
const recordData = (data && typeof data === 'object' ? data : {});
|
|
549
|
+
if (input?.kind === 'zhin.console.adapter-page') {
|
|
550
|
+
const adapterBp = input;
|
|
551
|
+
const pluginId = options.pluginId ?? adapterBp.adapterKey;
|
|
552
|
+
const title = locale === 'zh-CN'
|
|
553
|
+
? `${adapterBp.adapterKey} 适配器状态`
|
|
554
|
+
: `${adapterBp.adapterKey} Adapter Status`;
|
|
555
|
+
const bots = Array.isArray(recordData.bots)
|
|
556
|
+
? recordData.bots
|
|
557
|
+
: Array.isArray(recordData.endpoints)
|
|
558
|
+
? recordData.endpoints
|
|
559
|
+
: [];
|
|
560
|
+
const onlineCount = bots.filter((b) => b?.online === true || b?.connected === true || b?.status === 'online').length;
|
|
561
|
+
const stats = [
|
|
562
|
+
{
|
|
563
|
+
key: 'adapter',
|
|
564
|
+
label: locale === 'zh-CN' ? '适配器' : 'Adapter',
|
|
565
|
+
value: adapterBp.adapterKey,
|
|
566
|
+
},
|
|
567
|
+
{
|
|
568
|
+
key: 'endpoints',
|
|
569
|
+
label: locale === 'zh-CN' ? '实例数' : 'Instances',
|
|
570
|
+
value: String(bots.length),
|
|
571
|
+
},
|
|
572
|
+
{
|
|
573
|
+
key: 'online',
|
|
574
|
+
label: locale === 'zh-CN' ? '在线数' : 'Online',
|
|
575
|
+
value: String(onlineCount),
|
|
576
|
+
},
|
|
577
|
+
];
|
|
578
|
+
const columns = [
|
|
579
|
+
{ key: 'id', label: locale === 'zh-CN' ? '账号/端点' : 'Endpoint' },
|
|
580
|
+
{ key: 'status', label: locale === 'zh-CN' ? '状态' : 'Status' },
|
|
581
|
+
];
|
|
582
|
+
const rows = bots.slice(0, maxRows).map((b, i) => [
|
|
583
|
+
String(b?.id ?? b?.selfId ?? b?.name ?? `#${i + 1}`),
|
|
584
|
+
String(b?.status ??
|
|
585
|
+
(b?.online === true || b?.connected === true
|
|
586
|
+
? locale === 'zh-CN'
|
|
587
|
+
? '在线'
|
|
588
|
+
: 'online'
|
|
589
|
+
: locale === 'zh-CN'
|
|
590
|
+
? '离线'
|
|
591
|
+
: 'offline')),
|
|
592
|
+
]);
|
|
593
|
+
const tables = bots.length > 0
|
|
594
|
+
? [
|
|
595
|
+
{
|
|
596
|
+
id: 'endpoints',
|
|
597
|
+
title: locale === 'zh-CN' ? '端点列表' : 'Endpoints',
|
|
598
|
+
columns,
|
|
599
|
+
rows,
|
|
600
|
+
totalRows: bots.length,
|
|
601
|
+
},
|
|
602
|
+
]
|
|
603
|
+
: [];
|
|
604
|
+
const mdLines = [
|
|
605
|
+
`### ${title}`,
|
|
606
|
+
stats.map((s) => `- **${s.label}**: ${s.value}`).join('\n'),
|
|
607
|
+
];
|
|
608
|
+
if (tables.length > 0) {
|
|
609
|
+
for (const tbl of tables) {
|
|
610
|
+
mdLines.push(`\n#### ${tbl.title}`);
|
|
611
|
+
mdLines.push(`| ${tbl.columns.map((c) => c.label).join(' | ')} |`);
|
|
612
|
+
mdLines.push(`| ${tbl.columns.map(() => '---').join(' | ')} |`);
|
|
613
|
+
for (const r of tbl.rows) {
|
|
614
|
+
mdLines.push(`| ${r.join(' | ')} |`);
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
const markdown = mdLines.filter(Boolean).join('\n');
|
|
619
|
+
const segments = [
|
|
620
|
+
{
|
|
621
|
+
type: 'card',
|
|
622
|
+
data: {
|
|
623
|
+
title,
|
|
624
|
+
pluginId,
|
|
625
|
+
stats,
|
|
626
|
+
tables,
|
|
627
|
+
},
|
|
628
|
+
},
|
|
629
|
+
{
|
|
630
|
+
type: 'markdown',
|
|
631
|
+
data: { content: markdown },
|
|
632
|
+
},
|
|
633
|
+
];
|
|
634
|
+
return Object.freeze({
|
|
635
|
+
kind: 'zhin.im.blueprint-card',
|
|
636
|
+
pluginId,
|
|
637
|
+
title,
|
|
638
|
+
stats: Object.freeze(stats),
|
|
639
|
+
tables: Object.freeze(tables),
|
|
640
|
+
entries: Object.freeze([]),
|
|
641
|
+
actions: Object.freeze([]),
|
|
642
|
+
markdown,
|
|
643
|
+
segments: Object.freeze(segments),
|
|
644
|
+
});
|
|
645
|
+
}
|
|
646
|
+
const bp = input?.kind === 'zhin.console.page-blueprint'
|
|
647
|
+
? input
|
|
648
|
+
: buildBlueprintFromSpec(input);
|
|
649
|
+
const spec = bp.spec;
|
|
650
|
+
const pluginId = options.pluginId ?? spec?.pluginId ?? 'plugin';
|
|
651
|
+
const ctx = createProjectionContext(data, locale, pluginId, spec?.dictionary);
|
|
652
|
+
const title = (locale === 'zh-CN' ? bp.titleI18n?.['zh-CN'] : bp.titleI18n?.en) ??
|
|
653
|
+
(spec?.title ? resolveDeclarativeText(spec.title, locale) : bp.title);
|
|
654
|
+
const description = spec?.description
|
|
655
|
+
? resolveDeclarativeText(spec.description, locale)
|
|
656
|
+
: bp.description;
|
|
657
|
+
const stats = [];
|
|
658
|
+
const rawStats = typeof spec?.stats === 'function'
|
|
659
|
+
? spec.stats((data ?? {}), ctx)
|
|
660
|
+
: Array.isArray(spec?.stats)
|
|
661
|
+
? spec.stats
|
|
662
|
+
: undefined;
|
|
663
|
+
if (rawStats) {
|
|
664
|
+
for (const [idx, s] of rawStats.entries()) {
|
|
665
|
+
const val = typeof s.value === 'function'
|
|
666
|
+
? s.value((data ?? {}), ctx)
|
|
667
|
+
: evaluateDeclarativeValue(s.value, recordData, locale);
|
|
668
|
+
const hint = typeof s.hint === 'function'
|
|
669
|
+
? formatRenderableValue(s.hint((data ?? {})))
|
|
670
|
+
: isDeclarativeValueExpr(s.hint)
|
|
671
|
+
? formatRenderableValue(evaluateDeclarativeValue(s.hint, recordData, locale))
|
|
672
|
+
: s.hint
|
|
673
|
+
? resolveDeclarativeText(s.hint, locale)
|
|
674
|
+
: undefined;
|
|
675
|
+
const badge = s.badge
|
|
676
|
+
? typeof s.badge.label === 'function'
|
|
677
|
+
? formatRenderableValue(s.badge.label((data ?? {})))
|
|
678
|
+
: isDeclarativeValueExpr(s.badge.label)
|
|
679
|
+
? formatRenderableValue(evaluateDeclarativeValue(s.badge.label, recordData, locale))
|
|
680
|
+
: resolveDeclarativeText(s.badge.label, locale)
|
|
681
|
+
: undefined;
|
|
682
|
+
stats.push(Object.freeze({
|
|
683
|
+
key: s.key ?? `stat-${idx}`,
|
|
684
|
+
label: resolveDeclarativeText(s.label, locale),
|
|
685
|
+
value: formatRenderableValue(val),
|
|
686
|
+
...(hint ? { hint } : {}),
|
|
687
|
+
...(badge ? { badge } : {}),
|
|
688
|
+
}));
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
else {
|
|
692
|
+
for (const sec of bp.sections) {
|
|
693
|
+
if (sec.type === 'stats') {
|
|
694
|
+
for (const item of sec.items) {
|
|
695
|
+
const val = item.expr
|
|
696
|
+
? evaluateDeclarativeValue(item.expr, recordData, locale)
|
|
697
|
+
: item.dataKey
|
|
698
|
+
? resolveDataPath(recordData, item.dataKey)
|
|
699
|
+
: item.value;
|
|
700
|
+
stats.push(Object.freeze({
|
|
701
|
+
key: item.key,
|
|
702
|
+
label: item.label,
|
|
703
|
+
value: `${formatRenderableValue(val)}${item.suffix ?? ''}`,
|
|
704
|
+
}));
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
const tables = [];
|
|
710
|
+
const entries = [];
|
|
711
|
+
if (Array.isArray(spec?.sections)) {
|
|
712
|
+
for (const [idx, sec] of spec.sections.entries()) {
|
|
713
|
+
const id = sec.id ?? `section-${idx}`;
|
|
714
|
+
const secTitle = resolveDeclarativeText(sec.title, locale) || id;
|
|
715
|
+
if (sec.items !== undefined) {
|
|
716
|
+
const rawItems = typeof sec.items === 'function'
|
|
717
|
+
? sec.items((data ?? {}))
|
|
718
|
+
: resolveDataPath(recordData, sec.items);
|
|
719
|
+
const list = Array.isArray(rawItems) ? rawItems : [];
|
|
720
|
+
const cols = sec.columns && sec.columns.length > 0
|
|
721
|
+
? sec.columns.map((c) => ({
|
|
722
|
+
key: c.key,
|
|
723
|
+
label: resolveDeclarativeText(c.label, locale),
|
|
724
|
+
render: c.render,
|
|
725
|
+
}))
|
|
726
|
+
: list[0] && typeof list[0] === 'object'
|
|
727
|
+
? Object.keys(list[0])
|
|
728
|
+
.slice(0, 5)
|
|
729
|
+
.map((k) => ({ key: k, label: k, render: undefined }))
|
|
730
|
+
: [{ key: 'value', label: locale === 'zh-CN' ? '内容' : 'Value', render: undefined }];
|
|
731
|
+
const rows = list.slice(0, maxRows).map((row, rowIdx) => {
|
|
732
|
+
if (sec.renderItem && (!sec.columns || sec.columns.length === 0)) {
|
|
733
|
+
return [formatRenderableValue(sec.renderItem(row, rowIdx, ctx))];
|
|
734
|
+
}
|
|
735
|
+
const rec = (row && typeof row === 'object' ? row : { value: row });
|
|
736
|
+
return cols.map((c) => {
|
|
737
|
+
const cellVal = resolveDataPath(rec, c.key);
|
|
738
|
+
return c.render
|
|
739
|
+
? formatRenderableValue(c.render(cellVal, row, ctx))
|
|
740
|
+
: formatRenderableValue(cellVal);
|
|
741
|
+
});
|
|
742
|
+
});
|
|
743
|
+
const emptyText = sec.emptyText ? resolveDeclarativeText(sec.emptyText, locale) : undefined;
|
|
744
|
+
tables.push(Object.freeze({
|
|
745
|
+
id,
|
|
746
|
+
title: secTitle,
|
|
747
|
+
columns: Object.freeze(cols.map(({ key, label }) => Object.freeze({ key, label }))),
|
|
748
|
+
rows: Object.freeze(rows.map((r) => Object.freeze(r))),
|
|
749
|
+
totalRows: list.length,
|
|
750
|
+
...(emptyText ? { emptyText } : {}),
|
|
751
|
+
}));
|
|
752
|
+
}
|
|
753
|
+
else if (sec.entries !== undefined) {
|
|
754
|
+
const rawEntries = typeof sec.entries === 'function'
|
|
755
|
+
? sec.entries((data ?? {}), ctx)
|
|
756
|
+
: sec.entries;
|
|
757
|
+
const items = Array.isArray(rawEntries)
|
|
758
|
+
? rawEntries.map((entry) => {
|
|
759
|
+
const entryRecord = entry;
|
|
760
|
+
const resolvedValue = isDeclarativeValueExpr(entryRecord.expr)
|
|
761
|
+
? evaluateDeclarativeValue(entryRecord.expr, recordData, locale)
|
|
762
|
+
: isDeclarativeValueExpr(entryRecord.value)
|
|
763
|
+
? evaluateDeclarativeValue(entryRecord.value, recordData, locale)
|
|
764
|
+
: entryRecord.dataKey
|
|
765
|
+
? resolveDataPath(recordData, entryRecord.dataKey)
|
|
766
|
+
: entryRecord.value;
|
|
767
|
+
return Object.freeze({
|
|
768
|
+
label: typeof entryRecord.label === 'object' &&
|
|
769
|
+
entryRecord.label !== null &&
|
|
770
|
+
('zh' in entryRecord.label || 'en' in entryRecord.label || 'zh-CN' in entryRecord.label)
|
|
771
|
+
? resolveDeclarativeText(entryRecord.label, locale)
|
|
772
|
+
: formatRenderableValue(entryRecord.label),
|
|
773
|
+
value: formatRenderableValue(resolvedValue),
|
|
774
|
+
});
|
|
775
|
+
})
|
|
776
|
+
: Object.entries(rawEntries ?? {}).map(([k, v]) => Object.freeze({
|
|
777
|
+
label: k,
|
|
778
|
+
value: formatRenderableValue(isDeclarativeValueExpr(v) ? evaluateDeclarativeValue(v, recordData, locale) : v),
|
|
779
|
+
}));
|
|
780
|
+
entries.push(Object.freeze({
|
|
781
|
+
id,
|
|
782
|
+
title: secTitle,
|
|
783
|
+
items: Object.freeze(items),
|
|
784
|
+
}));
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
else {
|
|
789
|
+
for (const sec of bp.sections) {
|
|
790
|
+
if (sec.type === 'table') {
|
|
791
|
+
const rawItems = sec.dataKey ? resolveDataPath(recordData, sec.dataKey) : undefined;
|
|
792
|
+
const list = Array.isArray(rawItems) ? rawItems : [];
|
|
793
|
+
const rows = list.slice(0, maxRows).map((row) => {
|
|
794
|
+
const rec = (row && typeof row === 'object' ? row : {});
|
|
795
|
+
return sec.columns.map((c) => formatRenderableValue(resolveDataPath(rec, c.key)));
|
|
796
|
+
});
|
|
797
|
+
tables.push(Object.freeze({
|
|
798
|
+
id: sec.id,
|
|
799
|
+
title: sec.title,
|
|
800
|
+
columns: sec.columns,
|
|
801
|
+
rows: Object.freeze(rows.map((r) => Object.freeze(r))),
|
|
802
|
+
totalRows: list.length,
|
|
803
|
+
}));
|
|
804
|
+
}
|
|
805
|
+
else if (sec.type === 'entries' && Array.isArray(sec.entries)) {
|
|
806
|
+
entries.push(Object.freeze({
|
|
807
|
+
id: sec.id,
|
|
808
|
+
title: sec.title,
|
|
809
|
+
items: Object.freeze(sec.entries.map((entry) => {
|
|
810
|
+
const resolvedValue = entry.expr
|
|
811
|
+
? evaluateDeclarativeValue(entry.expr, recordData, locale)
|
|
812
|
+
: isDeclarativeValueExpr(entry.value)
|
|
813
|
+
? evaluateDeclarativeValue(entry.value, recordData, locale)
|
|
814
|
+
: entry.dataKey
|
|
815
|
+
? resolveDataPath(recordData, entry.dataKey)
|
|
816
|
+
: entry.value;
|
|
817
|
+
return Object.freeze({
|
|
818
|
+
label: formatRenderableValue(entry.label),
|
|
819
|
+
value: formatRenderableValue(resolvedValue),
|
|
820
|
+
});
|
|
821
|
+
})),
|
|
822
|
+
}));
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
const actions = [];
|
|
827
|
+
if (Array.isArray(spec?.actions)) {
|
|
828
|
+
for (const act of spec.actions) {
|
|
829
|
+
const disabled = typeof act.disabled === 'function' ? act.disabled(data) : Boolean(act.disabled);
|
|
830
|
+
const desc = act.description ? resolveDeclarativeText(act.description, locale) : undefined;
|
|
831
|
+
actions.push(Object.freeze({
|
|
832
|
+
name: act.name,
|
|
833
|
+
label: resolveDeclarativeText(act.label, locale),
|
|
834
|
+
rpcMethod: `plugin.${pluginId}.${act.name}`,
|
|
835
|
+
...(desc ? { description: desc } : {}),
|
|
836
|
+
variant: act.variant ?? 'secondary',
|
|
837
|
+
disabled,
|
|
838
|
+
}));
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
else {
|
|
842
|
+
for (const sec of bp.sections) {
|
|
843
|
+
if (sec.type === 'actions') {
|
|
844
|
+
for (const item of sec.items) {
|
|
845
|
+
actions.push(Object.freeze({
|
|
846
|
+
name: item.name ?? item.action,
|
|
847
|
+
label: item.label,
|
|
848
|
+
rpcMethod: `plugin.${pluginId}.${item.action}`,
|
|
849
|
+
...(item.description ? { description: item.description } : {}),
|
|
850
|
+
variant: item.variant ?? 'secondary',
|
|
851
|
+
disabled: false,
|
|
852
|
+
}));
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
const mdLines = [`### ${title}`];
|
|
858
|
+
if (description)
|
|
859
|
+
mdLines.push(description);
|
|
860
|
+
if (stats.length > 0) {
|
|
861
|
+
mdLines.push('');
|
|
862
|
+
for (const s of stats) {
|
|
863
|
+
const badgeSuffix = s.badge ? ` [${s.badge}]` : '';
|
|
864
|
+
const hintSuffix = s.hint ? ` (${s.hint})` : '';
|
|
865
|
+
mdLines.push(`- **${s.label}**: ${s.value}${badgeSuffix}${hintSuffix}`);
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
for (const entrySec of entries) {
|
|
869
|
+
mdLines.push(`\n#### ${entrySec.title}`);
|
|
870
|
+
for (const item of entrySec.items) {
|
|
871
|
+
mdLines.push(`- **${item.label}**: ${item.value}`);
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
for (const tbl of tables) {
|
|
875
|
+
mdLines.push(`\n#### ${tbl.title} (${tbl.totalRows})`);
|
|
876
|
+
if (tbl.rows.length === 0) {
|
|
877
|
+
mdLines.push(`_${tbl.emptyText ?? (locale === 'zh-CN' ? '暂无数据' : 'No data')}_`);
|
|
878
|
+
}
|
|
879
|
+
else {
|
|
880
|
+
mdLines.push(`| ${tbl.columns.map((c) => c.label).join(' | ')} |`);
|
|
881
|
+
mdLines.push(`| ${tbl.columns.map(() => '---').join(' | ')} |`);
|
|
882
|
+
for (const r of tbl.rows) {
|
|
883
|
+
mdLines.push(`| ${r.join(' | ')} |`);
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
if (actions.length > 0) {
|
|
888
|
+
mdLines.push(`\n> ${locale === 'zh-CN' ? '可执行操作' : 'Available Actions'}: ${actions
|
|
889
|
+
.map((a) => `\`${a.label}\` (${a.name})`)
|
|
890
|
+
.join(' · ')}`);
|
|
891
|
+
}
|
|
892
|
+
const markdown = mdLines.join('\n');
|
|
893
|
+
const segments = [
|
|
894
|
+
{
|
|
895
|
+
type: 'card',
|
|
896
|
+
data: {
|
|
897
|
+
title,
|
|
898
|
+
...(description ? { description } : {}),
|
|
899
|
+
pluginId,
|
|
900
|
+
stats,
|
|
901
|
+
tables,
|
|
902
|
+
entries,
|
|
903
|
+
actions,
|
|
904
|
+
},
|
|
905
|
+
},
|
|
906
|
+
{
|
|
907
|
+
type: 'markdown',
|
|
908
|
+
data: { content: markdown },
|
|
909
|
+
},
|
|
910
|
+
...actions
|
|
911
|
+
.filter((a) => !a.disabled)
|
|
912
|
+
.map((a) => ({
|
|
913
|
+
type: 'button',
|
|
914
|
+
data: {
|
|
915
|
+
id: a.rpcMethod,
|
|
916
|
+
label: a.label,
|
|
917
|
+
action: a.name,
|
|
918
|
+
variant: a.variant,
|
|
919
|
+
},
|
|
920
|
+
})),
|
|
921
|
+
];
|
|
922
|
+
return Object.freeze({
|
|
923
|
+
kind: 'zhin.im.blueprint-card',
|
|
924
|
+
pluginId,
|
|
925
|
+
title,
|
|
926
|
+
...(description ? { description } : {}),
|
|
927
|
+
stats: Object.freeze(stats),
|
|
928
|
+
tables: Object.freeze(tables),
|
|
929
|
+
entries: Object.freeze(entries),
|
|
930
|
+
actions: Object.freeze(actions),
|
|
931
|
+
markdown,
|
|
932
|
+
segments: Object.freeze(segments),
|
|
933
|
+
});
|
|
934
|
+
}
|
|
935
|
+
/**
|
|
936
|
+
* Projects any Server-Driven Console Blueprint (`definePageBlueprint`, `defineAdapterPage`,
|
|
937
|
+
* `defineCommentReviewPage`, or `DeclarativePageSpec`) + runtime status payload into a
|
|
938
|
+
* formatted CLI TUI panel string.
|
|
939
|
+
*/
|
|
940
|
+
export function projectBlueprintToCliPanel(input, data, options = {}) {
|
|
941
|
+
const card = projectBlueprintToImCard(input, data, options);
|
|
942
|
+
const width = Math.max(44, options.width ?? 72);
|
|
943
|
+
const innerWidth = width - 4;
|
|
944
|
+
const hr = '─'.repeat(width - 2);
|
|
945
|
+
const padLine = (text) => {
|
|
946
|
+
const clean = text.replace(/\r?\n/gu, ' ').slice(0, innerWidth);
|
|
947
|
+
const pad = Math.max(0, innerWidth - clean.length);
|
|
948
|
+
return `│ ${clean}${' '.repeat(pad)} │`;
|
|
949
|
+
};
|
|
950
|
+
const lines = [`┌${hr}┐`, padLine(`[${card.pluginId}] ${card.title}`)];
|
|
951
|
+
if (card.description) {
|
|
952
|
+
lines.push(padLine(card.description));
|
|
953
|
+
}
|
|
954
|
+
lines.push(`├${hr}┤`);
|
|
955
|
+
if (card.stats.length > 0) {
|
|
956
|
+
const statSummary = card.stats
|
|
957
|
+
.map((s) => `${s.label}: ${s.value}${s.badge ? ` [${s.badge}]` : ''}`)
|
|
958
|
+
.join(' │ ');
|
|
959
|
+
lines.push(padLine(statSummary));
|
|
960
|
+
lines.push(`├${hr}┤`);
|
|
961
|
+
}
|
|
962
|
+
for (const entrySec of card.entries) {
|
|
963
|
+
lines.push(padLine(`■ ${entrySec.title}`));
|
|
964
|
+
for (const item of entrySec.items) {
|
|
965
|
+
lines.push(padLine(` ${item.label}: ${item.value}`));
|
|
966
|
+
}
|
|
967
|
+
lines.push(`├${hr}┤`);
|
|
968
|
+
}
|
|
969
|
+
for (const tbl of card.tables) {
|
|
970
|
+
lines.push(padLine(`■ ${tbl.title} (${tbl.totalRows})`));
|
|
971
|
+
if (tbl.rows.length === 0) {
|
|
972
|
+
lines.push(padLine(` (${tbl.emptyText ?? 'empty'})`));
|
|
973
|
+
}
|
|
974
|
+
else {
|
|
975
|
+
lines.push(padLine(` ${tbl.columns.map((c) => c.label).join(' | ')}`));
|
|
976
|
+
for (const row of tbl.rows) {
|
|
977
|
+
lines.push(padLine(` ${row.join(' | ')}`));
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
lines.push(`├${hr}┤`);
|
|
981
|
+
}
|
|
982
|
+
if (card.actions.length > 0) {
|
|
983
|
+
const actionBar = card.actions
|
|
984
|
+
.map((a) => (a.disabled ? `(${a.label})` : `[${a.label} -> ${a.name}]`))
|
|
985
|
+
.join(' ');
|
|
986
|
+
lines.push(padLine(`Actions: ${actionBar}`));
|
|
987
|
+
}
|
|
988
|
+
else {
|
|
989
|
+
// Remove trailing separator if no actions
|
|
990
|
+
if (lines[lines.length - 1] === `├${hr}┤`) {
|
|
991
|
+
lines.pop();
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
lines.push(`└${hr}┘`);
|
|
995
|
+
return lines.join('\n');
|
|
996
|
+
}
|