@zhin.js/console-contract 1.1.2 → 1.1.4
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/navigation.d.ts +3 -0
- package/lib/page.d.ts +321 -0
- package/lib/page.js +695 -1
- package/package.json +1 -1
- package/src/navigation.ts +3 -0
- package/src/page.ts +1178 -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')
|
|
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);
|
|
@@ -14,11 +26,13 @@ export function normalizePageMetadata(localName, metadata) {
|
|
|
14
26
|
const input = metadata === undefined ? {} : requireRecord(metadata, 'Page metadata');
|
|
15
27
|
assertKnownMetadataKeys(input);
|
|
16
28
|
const title = optionalString(input.title, 'Page title') ?? titleFromName(localName);
|
|
29
|
+
const titleI18n = pageTitleTranslations(input.titleI18n);
|
|
17
30
|
const icon = optionalString(input.icon, 'Page icon');
|
|
18
31
|
const order = optionalNumber(input.order, 'Page order') ?? 100;
|
|
19
32
|
const hideInNav = optionalBoolean(input.hideInNav, 'Page hideInNav') ?? false;
|
|
20
33
|
return Object.freeze({
|
|
21
34
|
title,
|
|
35
|
+
...(titleI18n === undefined ? {} : { titleI18n }),
|
|
22
36
|
icon,
|
|
23
37
|
order,
|
|
24
38
|
hideInNav,
|
|
@@ -29,6 +43,7 @@ export function normalizePageMetadata(localName, metadata) {
|
|
|
29
43
|
function assertKnownMetadataKeys(input) {
|
|
30
44
|
const known = new Set([
|
|
31
45
|
'title',
|
|
46
|
+
'titleI18n',
|
|
32
47
|
'icon',
|
|
33
48
|
'order',
|
|
34
49
|
'hideInNav',
|
|
@@ -61,6 +76,7 @@ function normalizePageInput(metadata) {
|
|
|
61
76
|
const normalized = normalizePageMetadata('page', metadata);
|
|
62
77
|
return Object.freeze({
|
|
63
78
|
title: metadata.title === undefined ? undefined : normalized.title,
|
|
79
|
+
...(normalized.titleI18n === undefined ? {} : { titleI18n: normalized.titleI18n }),
|
|
64
80
|
icon: normalized.icon,
|
|
65
81
|
order: metadata.order === undefined ? undefined : normalized.order,
|
|
66
82
|
hideInNav: metadata.hideInNav === undefined ? undefined : normalized.hideInNav,
|
|
@@ -110,3 +126,681 @@ function stringList(value, label) {
|
|
|
110
126
|
});
|
|
111
127
|
return Object.freeze([...new Set(result)].sort());
|
|
112
128
|
}
|
|
129
|
+
function pageTitleTranslations(value) {
|
|
130
|
+
if (value === undefined)
|
|
131
|
+
return undefined;
|
|
132
|
+
const input = requireRecord(value, 'Page titleI18n');
|
|
133
|
+
const output = {};
|
|
134
|
+
for (const [locale, title] of Object.entries(input)) {
|
|
135
|
+
if (locale !== 'zh-CN' && locale !== 'en')
|
|
136
|
+
throw new TypeError(`Unsupported Page titleI18n locale: ${locale}`);
|
|
137
|
+
const text = optionalString(title, `Page titleI18n.${locale}`);
|
|
138
|
+
if (text === undefined)
|
|
139
|
+
throw new TypeError(`Page titleI18n.${locale} must be a non-empty string`);
|
|
140
|
+
output[locale] = text;
|
|
141
|
+
}
|
|
142
|
+
return Object.freeze(output);
|
|
143
|
+
}
|
|
144
|
+
export function resolveDeclarativeText(text, preferredLocale = 'en') {
|
|
145
|
+
if (!text)
|
|
146
|
+
return '';
|
|
147
|
+
if (typeof text === 'string')
|
|
148
|
+
return text;
|
|
149
|
+
if (preferredLocale === 'en') {
|
|
150
|
+
return text.en ?? text['zh-CN'] ?? text.zh;
|
|
151
|
+
}
|
|
152
|
+
return text['zh-CN'] ?? text.zh ?? text.en ?? '';
|
|
153
|
+
}
|
|
154
|
+
export function buildBlueprintFromSpec(input) {
|
|
155
|
+
if (input?.kind === 'zhin.console.page-blueprint') {
|
|
156
|
+
const bp = input;
|
|
157
|
+
return Object.freeze({
|
|
158
|
+
kind: 'zhin.console.page-blueprint',
|
|
159
|
+
version: 1,
|
|
160
|
+
title: bp.title,
|
|
161
|
+
...(bp.titleI18n ? { titleI18n: Object.freeze({ ...bp.titleI18n }) } : {}),
|
|
162
|
+
...(bp.description !== undefined ? { description: bp.description } : {}),
|
|
163
|
+
dataSource: Object.freeze({ action: bp.dataSource?.action ?? 'status' }),
|
|
164
|
+
sections: Object.freeze([...(bp.sections ?? [])]),
|
|
165
|
+
...(bp.spec !== undefined ? { spec: bp.spec } : {}),
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
const spec = input;
|
|
169
|
+
const title = resolveDeclarativeText(spec.title, 'en');
|
|
170
|
+
const titleI18n = typeof spec.title === 'object' && spec.title !== null
|
|
171
|
+
? {
|
|
172
|
+
'zh-CN': spec.title['zh-CN'] ?? spec.title.zh,
|
|
173
|
+
...(spec.title.en ? { en: spec.title.en } : {}),
|
|
174
|
+
}
|
|
175
|
+
: undefined;
|
|
176
|
+
const description = spec.description ? resolveDeclarativeText(spec.description, 'en') : undefined;
|
|
177
|
+
const dataSource = Object.freeze({ action: spec.statusMethod ?? 'status' });
|
|
178
|
+
const sections = [];
|
|
179
|
+
if (Array.isArray(spec.stats) && spec.stats.length > 0) {
|
|
180
|
+
sections.push(Object.freeze({
|
|
181
|
+
type: 'stats',
|
|
182
|
+
id: 'runtime-stats',
|
|
183
|
+
title: 'Runtime Stats',
|
|
184
|
+
items: Object.freeze(spec.stats.map((stat, idx) => Object.freeze({
|
|
185
|
+
key: stat.key ?? (typeof stat.value === 'string' ? stat.value : `stat-${idx}`),
|
|
186
|
+
label: resolveDeclarativeText(stat.label, 'en'),
|
|
187
|
+
...(typeof stat.value === 'string' ? { dataKey: stat.value } : {}),
|
|
188
|
+
}))),
|
|
189
|
+
}));
|
|
190
|
+
}
|
|
191
|
+
for (const [idx, sec] of (spec.sections ?? []).entries()) {
|
|
192
|
+
const id = sec.id ?? `section-${idx}`;
|
|
193
|
+
const secTitle = resolveDeclarativeText(sec.title, 'en');
|
|
194
|
+
if (sec.columns !== undefined || sec.items !== undefined) {
|
|
195
|
+
sections.push(Object.freeze({
|
|
196
|
+
type: 'table',
|
|
197
|
+
id,
|
|
198
|
+
title: secTitle,
|
|
199
|
+
...(typeof sec.items === 'string' ? { dataKey: sec.items } : {}),
|
|
200
|
+
rowKey: sec.columns?.[0]?.key ?? 'id',
|
|
201
|
+
columns: Object.freeze((sec.columns ?? []).map((col) => Object.freeze({
|
|
202
|
+
key: col.key,
|
|
203
|
+
label: resolveDeclarativeText(col.label, 'en'),
|
|
204
|
+
}))),
|
|
205
|
+
}));
|
|
206
|
+
}
|
|
207
|
+
else if (sec.entries !== undefined) {
|
|
208
|
+
sections.push(Object.freeze({
|
|
209
|
+
type: 'entries',
|
|
210
|
+
id,
|
|
211
|
+
title: secTitle,
|
|
212
|
+
}));
|
|
213
|
+
}
|
|
214
|
+
else {
|
|
215
|
+
sections.push(Object.freeze({
|
|
216
|
+
type: 'custom',
|
|
217
|
+
id,
|
|
218
|
+
...(secTitle ? { title: secTitle } : {}),
|
|
219
|
+
}));
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
if (Array.isArray(spec.actions) && spec.actions.length > 0) {
|
|
223
|
+
sections.push(Object.freeze({
|
|
224
|
+
type: 'actions',
|
|
225
|
+
id: 'actions',
|
|
226
|
+
title: 'Actions',
|
|
227
|
+
items: Object.freeze(spec.actions.map((act) => Object.freeze({
|
|
228
|
+
label: resolveDeclarativeText(act.label, 'en'),
|
|
229
|
+
action: act.name,
|
|
230
|
+
name: act.name,
|
|
231
|
+
...(act.description
|
|
232
|
+
? { description: resolveDeclarativeText(act.description, 'en') }
|
|
233
|
+
: {}),
|
|
234
|
+
...(act.variant ? { variant: act.variant } : {}),
|
|
235
|
+
}))),
|
|
236
|
+
}));
|
|
237
|
+
}
|
|
238
|
+
return Object.freeze({
|
|
239
|
+
kind: 'zhin.console.page-blueprint',
|
|
240
|
+
version: 1,
|
|
241
|
+
title,
|
|
242
|
+
...(titleI18n ? { titleI18n: Object.freeze(titleI18n) } : {}),
|
|
243
|
+
...(description !== undefined ? { description } : {}),
|
|
244
|
+
dataSource,
|
|
245
|
+
sections: Object.freeze(sections),
|
|
246
|
+
spec,
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
export function definePageBlueprint(spec) {
|
|
250
|
+
const blueprint = buildBlueprintFromSpec(spec);
|
|
251
|
+
function BlueprintPage(_props) {
|
|
252
|
+
return blueprint;
|
|
253
|
+
}
|
|
254
|
+
return Object.freeze(Object.assign(BlueprintPage, blueprint));
|
|
255
|
+
}
|
|
256
|
+
export const createDeclarativePage = definePageBlueprint;
|
|
257
|
+
export function defineAdapterPage(adapterKey) {
|
|
258
|
+
const validKey = optionalString(adapterKey, 'Adapter page adapterKey');
|
|
259
|
+
if (!validKey)
|
|
260
|
+
throw new TypeError('Adapter page adapterKey must be a non-empty string');
|
|
261
|
+
const blueprint = Object.freeze({
|
|
262
|
+
kind: 'zhin.console.adapter-page',
|
|
263
|
+
version: 1,
|
|
264
|
+
adapterKey: validKey,
|
|
265
|
+
dataSource: Object.freeze({ action: 'status' }),
|
|
266
|
+
});
|
|
267
|
+
function AdapterConsoleRoute(_props) {
|
|
268
|
+
return blueprint;
|
|
269
|
+
}
|
|
270
|
+
return Object.freeze(Object.assign(AdapterConsoleRoute, blueprint));
|
|
271
|
+
}
|
|
272
|
+
export const createAdapterConsolePage = defineAdapterPage;
|
|
273
|
+
export function defineCommentReviewPage(reviewSpec) {
|
|
274
|
+
const zhTitle = `${reviewSpec.platform.zh}评论审批`;
|
|
275
|
+
const enTitle = `${reviewSpec.platform.en} comment review`;
|
|
276
|
+
const loginActions = reviewSpec.loginMode === 'cookie-check'
|
|
277
|
+
? [{ name: 'login-check', label: { zh: '重新检查登录', en: 'Check login again' } }]
|
|
278
|
+
: [
|
|
279
|
+
{ name: 'login', label: { zh: '扫码登录', en: 'Sign in with QR code' }, variant: 'primary' },
|
|
280
|
+
{ name: 'login-cancel', label: { zh: '取消扫码', en: 'Cancel QR login' }, variant: 'ghost' },
|
|
281
|
+
{ name: 'logout', label: { zh: '退出登录', en: 'Sign out' }, variant: 'secondary' },
|
|
282
|
+
];
|
|
283
|
+
const blueprint = buildBlueprintFromSpec({
|
|
284
|
+
pluginId: reviewSpec.adapter,
|
|
285
|
+
title: { zh: zhTitle, 'zh-CN': zhTitle, en: enTitle },
|
|
286
|
+
mark: reviewSpec.mark,
|
|
287
|
+
statusMethod: 'status',
|
|
288
|
+
pollMs: reviewSpec.pollMs ?? 10_000,
|
|
289
|
+
...(reviewSpec.dictionary ? { dictionary: reviewSpec.dictionary } : {}),
|
|
290
|
+
stats: [
|
|
291
|
+
{
|
|
292
|
+
key: 'endpoints',
|
|
293
|
+
label: { zh: '账号数', en: 'Accounts' },
|
|
294
|
+
value: (data) => (Array.isArray(data?.endpoints) ? data.endpoints.length : 0),
|
|
295
|
+
},
|
|
296
|
+
{
|
|
297
|
+
key: 'pendingDrafts',
|
|
298
|
+
label: { zh: '待审草稿', en: 'Pending drafts' },
|
|
299
|
+
value: (data) => (data?.endpoints ?? []).reduce((sum, ep) => sum + (typeof ep?.pendingCount === 'number' ? ep.pendingCount : (ep?.drafts?.length ?? 0)), 0),
|
|
300
|
+
},
|
|
301
|
+
],
|
|
302
|
+
sections: [
|
|
303
|
+
{
|
|
304
|
+
id: 'endpoints',
|
|
305
|
+
title: { zh: '评论账号与待审草稿', en: 'Accounts & Pending Drafts' },
|
|
306
|
+
items: 'endpoints',
|
|
307
|
+
columns: [
|
|
308
|
+
{ key: 'id', label: { zh: '账号', en: 'Account' } },
|
|
309
|
+
{ key: 'sendMode', label: { zh: '发送模式', en: 'Send Mode' } },
|
|
310
|
+
{ key: 'pendingCount', label: { zh: '待审数', en: 'Pending' } },
|
|
311
|
+
],
|
|
312
|
+
},
|
|
313
|
+
],
|
|
314
|
+
actions: [
|
|
315
|
+
...loginActions,
|
|
316
|
+
{
|
|
317
|
+
name: 'edit',
|
|
318
|
+
label: { zh: '修改草稿', en: 'Edit draft' },
|
|
319
|
+
fields: [
|
|
320
|
+
{ key: 'id', label: { zh: '草稿 ID', en: 'Draft ID' }, required: true },
|
|
321
|
+
{ key: 'message', label: { zh: '回复内容', en: 'Reply content' }, required: true },
|
|
322
|
+
],
|
|
323
|
+
},
|
|
324
|
+
{
|
|
325
|
+
name: 'send',
|
|
326
|
+
label: { zh: '发送草稿', en: 'Send draft' },
|
|
327
|
+
variant: 'primary',
|
|
328
|
+
fields: [{ key: 'id', label: { zh: '草稿 ID', en: 'Draft ID' }, required: true }],
|
|
329
|
+
},
|
|
330
|
+
{
|
|
331
|
+
name: 'drop',
|
|
332
|
+
label: { zh: '丢弃草稿', en: 'Discard draft' },
|
|
333
|
+
variant: 'danger',
|
|
334
|
+
confirm: { zh: '确认丢弃该草稿?', en: 'Discard this draft?' },
|
|
335
|
+
fields: [{ key: 'id', label: { zh: '草稿 ID', en: 'Draft ID' }, required: true }],
|
|
336
|
+
},
|
|
337
|
+
],
|
|
338
|
+
});
|
|
339
|
+
const enriched = Object.freeze({
|
|
340
|
+
...blueprint,
|
|
341
|
+
reviewSpec,
|
|
342
|
+
});
|
|
343
|
+
function CommentReviewBlueprintPage(_props) {
|
|
344
|
+
return enriched;
|
|
345
|
+
}
|
|
346
|
+
return Object.freeze(Object.assign(CommentReviewBlueprintPage, enriched));
|
|
347
|
+
}
|
|
348
|
+
export const createCommentReviewPage = defineCommentReviewPage;
|
|
349
|
+
function createProjectionContext(data, locale, pluginId, dictionary) {
|
|
350
|
+
const t = (key, ...values) => {
|
|
351
|
+
const template = locale === 'en' && dictionary?.[key] ? dictionary[key] : key;
|
|
352
|
+
return template.replace(/\{(\d+)\}/gu, (_m, idx) => String(values[Number(idx)] ?? ''));
|
|
353
|
+
};
|
|
354
|
+
return {
|
|
355
|
+
pluginId,
|
|
356
|
+
locale,
|
|
357
|
+
t,
|
|
358
|
+
resource: {
|
|
359
|
+
state: data === undefined ? 'empty' : 'ready',
|
|
360
|
+
data,
|
|
361
|
+
error: undefined,
|
|
362
|
+
lastSuccessAt: Date.now(),
|
|
363
|
+
refreshing: false,
|
|
364
|
+
refresh: async () => true,
|
|
365
|
+
mutate: () => undefined,
|
|
366
|
+
},
|
|
367
|
+
call: async () => undefined,
|
|
368
|
+
refresh: async () => true,
|
|
369
|
+
notify: () => undefined,
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
function formatRenderableValue(value) {
|
|
373
|
+
if (value === null || value === undefined)
|
|
374
|
+
return '—';
|
|
375
|
+
if (typeof value === 'string')
|
|
376
|
+
return value || '—';
|
|
377
|
+
if (typeof value === 'number' || typeof value === 'boolean')
|
|
378
|
+
return String(value);
|
|
379
|
+
if (Array.isArray(value))
|
|
380
|
+
return value.map((item) => formatRenderableValue(item)).join(', ');
|
|
381
|
+
if (typeof value === 'object') {
|
|
382
|
+
const record = value;
|
|
383
|
+
if (typeof record.label === 'string')
|
|
384
|
+
return record.label;
|
|
385
|
+
if (typeof record.text === 'string')
|
|
386
|
+
return record.text;
|
|
387
|
+
try {
|
|
388
|
+
return JSON.stringify(value);
|
|
389
|
+
}
|
|
390
|
+
catch {
|
|
391
|
+
return String(value);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
return String(value);
|
|
395
|
+
}
|
|
396
|
+
/**
|
|
397
|
+
* Projects any Server-Driven Console Blueprint (`definePageBlueprint`, `defineAdapterPage`,
|
|
398
|
+
* `defineCommentReviewPage`, or `DeclarativePageSpec`) + runtime status payload into a
|
|
399
|
+
* structured IM Card / Markdown / Segment payload ready for `Message.$reply` or `Adapter.sendMessage`.
|
|
400
|
+
*/
|
|
401
|
+
export function projectBlueprintToImCard(input, data, options = {}) {
|
|
402
|
+
const locale = options.locale ?? 'zh-CN';
|
|
403
|
+
const maxRows = Math.max(1, options.maxRows ?? 10);
|
|
404
|
+
const recordData = (data && typeof data === 'object' ? data : {});
|
|
405
|
+
if (input?.kind === 'zhin.console.adapter-page') {
|
|
406
|
+
const adapterBp = input;
|
|
407
|
+
const pluginId = options.pluginId ?? adapterBp.adapterKey;
|
|
408
|
+
const title = locale === 'zh-CN'
|
|
409
|
+
? `${adapterBp.adapterKey} 适配器状态`
|
|
410
|
+
: `${adapterBp.adapterKey} Adapter Status`;
|
|
411
|
+
const bots = Array.isArray(recordData.bots)
|
|
412
|
+
? recordData.bots
|
|
413
|
+
: Array.isArray(recordData.endpoints)
|
|
414
|
+
? recordData.endpoints
|
|
415
|
+
: [];
|
|
416
|
+
const onlineCount = bots.filter((b) => b?.online === true || b?.connected === true || b?.status === 'online').length;
|
|
417
|
+
const stats = [
|
|
418
|
+
{
|
|
419
|
+
key: 'adapter',
|
|
420
|
+
label: locale === 'zh-CN' ? '适配器' : 'Adapter',
|
|
421
|
+
value: adapterBp.adapterKey,
|
|
422
|
+
},
|
|
423
|
+
{
|
|
424
|
+
key: 'endpoints',
|
|
425
|
+
label: locale === 'zh-CN' ? '实例数' : 'Instances',
|
|
426
|
+
value: String(bots.length),
|
|
427
|
+
},
|
|
428
|
+
{
|
|
429
|
+
key: 'online',
|
|
430
|
+
label: locale === 'zh-CN' ? '在线数' : 'Online',
|
|
431
|
+
value: String(onlineCount),
|
|
432
|
+
},
|
|
433
|
+
];
|
|
434
|
+
const columns = [
|
|
435
|
+
{ key: 'id', label: locale === 'zh-CN' ? '账号/端点' : 'Endpoint' },
|
|
436
|
+
{ key: 'status', label: locale === 'zh-CN' ? '状态' : 'Status' },
|
|
437
|
+
];
|
|
438
|
+
const rows = bots.slice(0, maxRows).map((b, i) => [
|
|
439
|
+
String(b?.id ?? b?.selfId ?? b?.name ?? `#${i + 1}`),
|
|
440
|
+
String(b?.status ??
|
|
441
|
+
(b?.online === true || b?.connected === true
|
|
442
|
+
? locale === 'zh-CN'
|
|
443
|
+
? '在线'
|
|
444
|
+
: 'online'
|
|
445
|
+
: locale === 'zh-CN'
|
|
446
|
+
? '离线'
|
|
447
|
+
: 'offline')),
|
|
448
|
+
]);
|
|
449
|
+
const tables = bots.length > 0
|
|
450
|
+
? [
|
|
451
|
+
{
|
|
452
|
+
id: 'endpoints',
|
|
453
|
+
title: locale === 'zh-CN' ? '端点列表' : 'Endpoints',
|
|
454
|
+
columns,
|
|
455
|
+
rows,
|
|
456
|
+
totalRows: bots.length,
|
|
457
|
+
},
|
|
458
|
+
]
|
|
459
|
+
: [];
|
|
460
|
+
const mdLines = [
|
|
461
|
+
`### ${title}`,
|
|
462
|
+
stats.map((s) => `- **${s.label}**: ${s.value}`).join('\n'),
|
|
463
|
+
];
|
|
464
|
+
if (tables.length > 0) {
|
|
465
|
+
for (const tbl of tables) {
|
|
466
|
+
mdLines.push(`\n#### ${tbl.title}`);
|
|
467
|
+
mdLines.push(`| ${tbl.columns.map((c) => c.label).join(' | ')} |`);
|
|
468
|
+
mdLines.push(`| ${tbl.columns.map(() => '---').join(' | ')} |`);
|
|
469
|
+
for (const r of tbl.rows) {
|
|
470
|
+
mdLines.push(`| ${r.join(' | ')} |`);
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
const markdown = mdLines.filter(Boolean).join('\n');
|
|
475
|
+
const segments = [
|
|
476
|
+
{
|
|
477
|
+
type: 'card',
|
|
478
|
+
data: {
|
|
479
|
+
title,
|
|
480
|
+
pluginId,
|
|
481
|
+
stats,
|
|
482
|
+
tables,
|
|
483
|
+
},
|
|
484
|
+
},
|
|
485
|
+
{
|
|
486
|
+
type: 'markdown',
|
|
487
|
+
data: { content: markdown },
|
|
488
|
+
},
|
|
489
|
+
];
|
|
490
|
+
return Object.freeze({
|
|
491
|
+
kind: 'zhin.im.blueprint-card',
|
|
492
|
+
pluginId,
|
|
493
|
+
title,
|
|
494
|
+
stats: Object.freeze(stats),
|
|
495
|
+
tables: Object.freeze(tables),
|
|
496
|
+
entries: Object.freeze([]),
|
|
497
|
+
actions: Object.freeze([]),
|
|
498
|
+
markdown,
|
|
499
|
+
segments: Object.freeze(segments),
|
|
500
|
+
});
|
|
501
|
+
}
|
|
502
|
+
const bp = input?.kind === 'zhin.console.page-blueprint'
|
|
503
|
+
? input
|
|
504
|
+
: buildBlueprintFromSpec(input);
|
|
505
|
+
const spec = bp.spec;
|
|
506
|
+
const pluginId = options.pluginId ?? spec?.pluginId ?? 'plugin';
|
|
507
|
+
const ctx = createProjectionContext(data, locale, pluginId, spec?.dictionary);
|
|
508
|
+
const title = (locale === 'zh-CN' ? bp.titleI18n?.['zh-CN'] : bp.titleI18n?.en) ??
|
|
509
|
+
(spec?.title ? resolveDeclarativeText(spec.title, locale) : bp.title);
|
|
510
|
+
const description = spec?.description
|
|
511
|
+
? resolveDeclarativeText(spec.description, locale)
|
|
512
|
+
: bp.description;
|
|
513
|
+
const stats = [];
|
|
514
|
+
const rawStats = typeof spec?.stats === 'function'
|
|
515
|
+
? spec.stats((data ?? {}), ctx)
|
|
516
|
+
: Array.isArray(spec?.stats)
|
|
517
|
+
? spec.stats
|
|
518
|
+
: undefined;
|
|
519
|
+
if (rawStats) {
|
|
520
|
+
for (const [idx, s] of rawStats.entries()) {
|
|
521
|
+
const val = typeof s.value === 'function'
|
|
522
|
+
? s.value((data ?? {}), ctx)
|
|
523
|
+
: recordData[s.value];
|
|
524
|
+
const hint = typeof s.hint === 'function'
|
|
525
|
+
? formatRenderableValue(s.hint((data ?? {})))
|
|
526
|
+
: s.hint
|
|
527
|
+
? resolveDeclarativeText(s.hint, locale)
|
|
528
|
+
: undefined;
|
|
529
|
+
const badge = s.badge
|
|
530
|
+
? typeof s.badge.label === 'function'
|
|
531
|
+
? formatRenderableValue(s.badge.label((data ?? {})))
|
|
532
|
+
: resolveDeclarativeText(s.badge.label, locale)
|
|
533
|
+
: undefined;
|
|
534
|
+
stats.push(Object.freeze({
|
|
535
|
+
key: s.key ?? `stat-${idx}`,
|
|
536
|
+
label: resolveDeclarativeText(s.label, locale),
|
|
537
|
+
value: formatRenderableValue(val),
|
|
538
|
+
...(hint ? { hint } : {}),
|
|
539
|
+
...(badge ? { badge } : {}),
|
|
540
|
+
}));
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
else {
|
|
544
|
+
for (const sec of bp.sections) {
|
|
545
|
+
if (sec.type === 'stats') {
|
|
546
|
+
for (const item of sec.items) {
|
|
547
|
+
const val = item.dataKey ? recordData[item.dataKey] : item.value;
|
|
548
|
+
stats.push(Object.freeze({
|
|
549
|
+
key: item.key,
|
|
550
|
+
label: item.label,
|
|
551
|
+
value: `${formatRenderableValue(val)}${item.suffix ?? ''}`,
|
|
552
|
+
}));
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
const tables = [];
|
|
558
|
+
const entries = [];
|
|
559
|
+
if (Array.isArray(spec?.sections)) {
|
|
560
|
+
for (const [idx, sec] of spec.sections.entries()) {
|
|
561
|
+
const id = sec.id ?? `section-${idx}`;
|
|
562
|
+
const secTitle = resolveDeclarativeText(sec.title, locale) || id;
|
|
563
|
+
if (sec.items !== undefined) {
|
|
564
|
+
const rawItems = typeof sec.items === 'function'
|
|
565
|
+
? sec.items((data ?? {}))
|
|
566
|
+
: recordData[sec.items];
|
|
567
|
+
const list = Array.isArray(rawItems) ? rawItems : [];
|
|
568
|
+
const cols = sec.columns && sec.columns.length > 0
|
|
569
|
+
? sec.columns.map((c) => ({
|
|
570
|
+
key: c.key,
|
|
571
|
+
label: resolveDeclarativeText(c.label, locale),
|
|
572
|
+
render: c.render,
|
|
573
|
+
}))
|
|
574
|
+
: list[0] && typeof list[0] === 'object'
|
|
575
|
+
? Object.keys(list[0])
|
|
576
|
+
.slice(0, 5)
|
|
577
|
+
.map((k) => ({ key: k, label: k, render: undefined }))
|
|
578
|
+
: [{ key: 'value', label: locale === 'zh-CN' ? '内容' : 'Value', render: undefined }];
|
|
579
|
+
const rows = list.slice(0, maxRows).map((row, rowIdx) => {
|
|
580
|
+
if (sec.renderItem && (!sec.columns || sec.columns.length === 0)) {
|
|
581
|
+
return [formatRenderableValue(sec.renderItem(row, rowIdx, ctx))];
|
|
582
|
+
}
|
|
583
|
+
const rec = (row && typeof row === 'object' ? row : { value: row });
|
|
584
|
+
return cols.map((c) => c.render
|
|
585
|
+
? formatRenderableValue(c.render(rec[c.key], row, ctx))
|
|
586
|
+
: formatRenderableValue(rec[c.key]));
|
|
587
|
+
});
|
|
588
|
+
const emptyText = sec.emptyText ? resolveDeclarativeText(sec.emptyText, locale) : undefined;
|
|
589
|
+
tables.push(Object.freeze({
|
|
590
|
+
id,
|
|
591
|
+
title: secTitle,
|
|
592
|
+
columns: Object.freeze(cols.map(({ key, label }) => Object.freeze({ key, label }))),
|
|
593
|
+
rows: Object.freeze(rows.map((r) => Object.freeze(r))),
|
|
594
|
+
totalRows: list.length,
|
|
595
|
+
...(emptyText ? { emptyText } : {}),
|
|
596
|
+
}));
|
|
597
|
+
}
|
|
598
|
+
else if (sec.entries !== undefined) {
|
|
599
|
+
const rawEntries = sec.entries((data ?? {}), ctx);
|
|
600
|
+
const items = Array.isArray(rawEntries)
|
|
601
|
+
? rawEntries.map((entry) => Object.freeze({
|
|
602
|
+
label: formatRenderableValue(entry.label),
|
|
603
|
+
value: formatRenderableValue(entry.value),
|
|
604
|
+
}))
|
|
605
|
+
: Object.entries(rawEntries ?? {}).map(([k, v]) => Object.freeze({
|
|
606
|
+
label: k,
|
|
607
|
+
value: formatRenderableValue(v),
|
|
608
|
+
}));
|
|
609
|
+
entries.push(Object.freeze({
|
|
610
|
+
id,
|
|
611
|
+
title: secTitle,
|
|
612
|
+
items: Object.freeze(items),
|
|
613
|
+
}));
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
else {
|
|
618
|
+
for (const sec of bp.sections) {
|
|
619
|
+
if (sec.type === 'table') {
|
|
620
|
+
const rawItems = sec.dataKey ? recordData[sec.dataKey] : undefined;
|
|
621
|
+
const list = Array.isArray(rawItems) ? rawItems : [];
|
|
622
|
+
const rows = list.slice(0, maxRows).map((row) => {
|
|
623
|
+
const rec = (row && typeof row === 'object' ? row : {});
|
|
624
|
+
return sec.columns.map((c) => formatRenderableValue(rec[c.key]));
|
|
625
|
+
});
|
|
626
|
+
tables.push(Object.freeze({
|
|
627
|
+
id: sec.id,
|
|
628
|
+
title: sec.title,
|
|
629
|
+
columns: sec.columns,
|
|
630
|
+
rows: Object.freeze(rows.map((r) => Object.freeze(r))),
|
|
631
|
+
totalRows: list.length,
|
|
632
|
+
}));
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
const actions = [];
|
|
637
|
+
if (Array.isArray(spec?.actions)) {
|
|
638
|
+
for (const act of spec.actions) {
|
|
639
|
+
const disabled = typeof act.disabled === 'function' ? act.disabled(data) : Boolean(act.disabled);
|
|
640
|
+
const desc = act.description ? resolveDeclarativeText(act.description, locale) : undefined;
|
|
641
|
+
actions.push(Object.freeze({
|
|
642
|
+
name: act.name,
|
|
643
|
+
label: resolveDeclarativeText(act.label, locale),
|
|
644
|
+
rpcMethod: `plugin.${pluginId}.${act.name}`,
|
|
645
|
+
...(desc ? { description: desc } : {}),
|
|
646
|
+
variant: act.variant ?? 'secondary',
|
|
647
|
+
disabled,
|
|
648
|
+
}));
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
else {
|
|
652
|
+
for (const sec of bp.sections) {
|
|
653
|
+
if (sec.type === 'actions') {
|
|
654
|
+
for (const item of sec.items) {
|
|
655
|
+
actions.push(Object.freeze({
|
|
656
|
+
name: item.name ?? item.action,
|
|
657
|
+
label: item.label,
|
|
658
|
+
rpcMethod: `plugin.${pluginId}.${item.action}`,
|
|
659
|
+
...(item.description ? { description: item.description } : {}),
|
|
660
|
+
variant: item.variant ?? 'secondary',
|
|
661
|
+
disabled: false,
|
|
662
|
+
}));
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
const mdLines = [`### ${title}`];
|
|
668
|
+
if (description)
|
|
669
|
+
mdLines.push(description);
|
|
670
|
+
if (stats.length > 0) {
|
|
671
|
+
mdLines.push('');
|
|
672
|
+
for (const s of stats) {
|
|
673
|
+
const badgeSuffix = s.badge ? ` [${s.badge}]` : '';
|
|
674
|
+
const hintSuffix = s.hint ? ` (${s.hint})` : '';
|
|
675
|
+
mdLines.push(`- **${s.label}**: ${s.value}${badgeSuffix}${hintSuffix}`);
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
for (const entrySec of entries) {
|
|
679
|
+
mdLines.push(`\n#### ${entrySec.title}`);
|
|
680
|
+
for (const item of entrySec.items) {
|
|
681
|
+
mdLines.push(`- **${item.label}**: ${item.value}`);
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
for (const tbl of tables) {
|
|
685
|
+
mdLines.push(`\n#### ${tbl.title} (${tbl.totalRows})`);
|
|
686
|
+
if (tbl.rows.length === 0) {
|
|
687
|
+
mdLines.push(`_${tbl.emptyText ?? (locale === 'zh-CN' ? '暂无数据' : 'No data')}_`);
|
|
688
|
+
}
|
|
689
|
+
else {
|
|
690
|
+
mdLines.push(`| ${tbl.columns.map((c) => c.label).join(' | ')} |`);
|
|
691
|
+
mdLines.push(`| ${tbl.columns.map(() => '---').join(' | ')} |`);
|
|
692
|
+
for (const r of tbl.rows) {
|
|
693
|
+
mdLines.push(`| ${r.join(' | ')} |`);
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
if (actions.length > 0) {
|
|
698
|
+
mdLines.push(`\n> ${locale === 'zh-CN' ? '可执行操作' : 'Available Actions'}: ${actions
|
|
699
|
+
.map((a) => `\`${a.label}\` (${a.name})`)
|
|
700
|
+
.join(' · ')}`);
|
|
701
|
+
}
|
|
702
|
+
const markdown = mdLines.join('\n');
|
|
703
|
+
const segments = [
|
|
704
|
+
{
|
|
705
|
+
type: 'card',
|
|
706
|
+
data: {
|
|
707
|
+
title,
|
|
708
|
+
...(description ? { description } : {}),
|
|
709
|
+
pluginId,
|
|
710
|
+
stats,
|
|
711
|
+
tables,
|
|
712
|
+
entries,
|
|
713
|
+
actions,
|
|
714
|
+
},
|
|
715
|
+
},
|
|
716
|
+
{
|
|
717
|
+
type: 'markdown',
|
|
718
|
+
data: { content: markdown },
|
|
719
|
+
},
|
|
720
|
+
...actions
|
|
721
|
+
.filter((a) => !a.disabled)
|
|
722
|
+
.map((a) => ({
|
|
723
|
+
type: 'button',
|
|
724
|
+
data: {
|
|
725
|
+
id: a.rpcMethod,
|
|
726
|
+
label: a.label,
|
|
727
|
+
action: a.name,
|
|
728
|
+
variant: a.variant,
|
|
729
|
+
},
|
|
730
|
+
})),
|
|
731
|
+
];
|
|
732
|
+
return Object.freeze({
|
|
733
|
+
kind: 'zhin.im.blueprint-card',
|
|
734
|
+
pluginId,
|
|
735
|
+
title,
|
|
736
|
+
...(description ? { description } : {}),
|
|
737
|
+
stats: Object.freeze(stats),
|
|
738
|
+
tables: Object.freeze(tables),
|
|
739
|
+
entries: Object.freeze(entries),
|
|
740
|
+
actions: Object.freeze(actions),
|
|
741
|
+
markdown,
|
|
742
|
+
segments: Object.freeze(segments),
|
|
743
|
+
});
|
|
744
|
+
}
|
|
745
|
+
/**
|
|
746
|
+
* Projects any Server-Driven Console Blueprint (`definePageBlueprint`, `defineAdapterPage`,
|
|
747
|
+
* `defineCommentReviewPage`, or `DeclarativePageSpec`) + runtime status payload into a
|
|
748
|
+
* formatted CLI TUI panel string.
|
|
749
|
+
*/
|
|
750
|
+
export function projectBlueprintToCliPanel(input, data, options = {}) {
|
|
751
|
+
const card = projectBlueprintToImCard(input, data, options);
|
|
752
|
+
const width = Math.max(44, options.width ?? 72);
|
|
753
|
+
const innerWidth = width - 4;
|
|
754
|
+
const hr = '─'.repeat(width - 2);
|
|
755
|
+
const padLine = (text) => {
|
|
756
|
+
const clean = text.replace(/\r?\n/gu, ' ').slice(0, innerWidth);
|
|
757
|
+
const pad = Math.max(0, innerWidth - clean.length);
|
|
758
|
+
return `│ ${clean}${' '.repeat(pad)} │`;
|
|
759
|
+
};
|
|
760
|
+
const lines = [`┌${hr}┐`, padLine(`[${card.pluginId}] ${card.title}`)];
|
|
761
|
+
if (card.description) {
|
|
762
|
+
lines.push(padLine(card.description));
|
|
763
|
+
}
|
|
764
|
+
lines.push(`├${hr}┤`);
|
|
765
|
+
if (card.stats.length > 0) {
|
|
766
|
+
const statSummary = card.stats
|
|
767
|
+
.map((s) => `${s.label}: ${s.value}${s.badge ? ` [${s.badge}]` : ''}`)
|
|
768
|
+
.join(' │ ');
|
|
769
|
+
lines.push(padLine(statSummary));
|
|
770
|
+
lines.push(`├${hr}┤`);
|
|
771
|
+
}
|
|
772
|
+
for (const entrySec of card.entries) {
|
|
773
|
+
lines.push(padLine(`■ ${entrySec.title}`));
|
|
774
|
+
for (const item of entrySec.items) {
|
|
775
|
+
lines.push(padLine(` ${item.label}: ${item.value}`));
|
|
776
|
+
}
|
|
777
|
+
lines.push(`├${hr}┤`);
|
|
778
|
+
}
|
|
779
|
+
for (const tbl of card.tables) {
|
|
780
|
+
lines.push(padLine(`■ ${tbl.title} (${tbl.totalRows})`));
|
|
781
|
+
if (tbl.rows.length === 0) {
|
|
782
|
+
lines.push(padLine(` (${tbl.emptyText ?? 'empty'})`));
|
|
783
|
+
}
|
|
784
|
+
else {
|
|
785
|
+
lines.push(padLine(` ${tbl.columns.map((c) => c.label).join(' | ')}`));
|
|
786
|
+
for (const row of tbl.rows) {
|
|
787
|
+
lines.push(padLine(` ${row.join(' | ')}`));
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
lines.push(`├${hr}┤`);
|
|
791
|
+
}
|
|
792
|
+
if (card.actions.length > 0) {
|
|
793
|
+
const actionBar = card.actions
|
|
794
|
+
.map((a) => (a.disabled ? `(${a.label})` : `[${a.label} -> ${a.name}]`))
|
|
795
|
+
.join(' ');
|
|
796
|
+
lines.push(padLine(`Actions: ${actionBar}`));
|
|
797
|
+
}
|
|
798
|
+
else {
|
|
799
|
+
// Remove trailing separator if no actions
|
|
800
|
+
if (lines[lines.length - 1] === `├${hr}┤`) {
|
|
801
|
+
lines.pop();
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
lines.push(`└${hr}┘`);
|
|
805
|
+
return lines.join('\n');
|
|
806
|
+
}
|