@zhin.js/host-http 1.1.14 → 1.1.16
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/console-rest-pages-helpers.d.ts +23 -0
- package/lib/console-rest-pages-helpers.js +570 -0
- package/lib/console-rest-pages.js +10 -570
- package/lib/console-rpc-handlers.d.ts +21 -0
- package/lib/console-rpc-handlers.js +659 -0
- package/lib/console-rpc.d.ts +1 -2
- package/lib/console-rpc.js +6 -620
- package/lib/host-installer.d.ts +8 -0
- package/lib/host-installer.js +211 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/package.json +4 -4
- package/src/console-rest-pages-helpers.ts +619 -0
- package/src/console-rest-pages.ts +12 -612
- package/src/console-rpc-handlers.ts +608 -0
- package/src/console-rpc.ts +10 -579
- package/src/host-installer.ts +253 -0
- package/src/index.ts +2 -0
package/lib/console-rpc.js
CHANGED
|
@@ -1,55 +1,14 @@
|
|
|
1
|
-
import { CONFIG_PATCH_LIMITS, CONFIG_RPC,
|
|
1
|
+
import { CONFIG_PATCH_LIMITS, CONFIG_RPC, assertDemoConsoleRpcAllowed, } from '@zhin.js/console-protocol';
|
|
2
2
|
import { dispatchExtendedConsoleRpc } from './console-rpc-extended/index.js';
|
|
3
3
|
import { dispatchPluginConsoleRpc } from './console-plugin-rpc.js';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const kind = channelType === 'group' || channelType === 'channel' ? channelType : 'private';
|
|
7
|
-
const wire = parent && typeof parent === 'object'
|
|
8
|
-
? parent
|
|
9
|
-
: undefined;
|
|
10
|
-
const parentKind = wire?.type === 'group' || wire?.type === 'channel' ? wire.type : undefined;
|
|
11
|
-
const parentId = typeof wire?.id === 'string' && wire.id ? wire.id : undefined;
|
|
12
|
-
return {
|
|
13
|
-
kind,
|
|
14
|
-
id: channelId,
|
|
15
|
-
...(parentKind && parentId ? { parent: { kind: parentKind, id: parentId } } : {}),
|
|
16
|
-
};
|
|
17
|
-
}
|
|
4
|
+
import { configurationSaveMessage, dispatchHostResourceConsoleRpc, invalidProjectPatchOperations, } from './console-rpc-handlers.js';
|
|
5
|
+
export { isDemoHttpAllowed } from './console-rpc-handlers.js';
|
|
18
6
|
export function pickRpcReply(message, payloads) {
|
|
19
7
|
const requestId = message.requestId;
|
|
20
8
|
return ((requestId != null ? payloads.find((payload) => payload.requestId === requestId) : undefined)
|
|
21
9
|
?? payloads[payloads.length - 1]
|
|
22
10
|
?? null);
|
|
23
11
|
}
|
|
24
|
-
const FORBIDDEN_PATCH_SEGMENTS = new Set(['__proto__', 'constructor', 'prototype']);
|
|
25
|
-
/**
|
|
26
|
-
* Structural check for `config:patch-project` operations at the RPC boundary, so no project
|
|
27
|
-
* port implementation receives a malformed request. The store repeats the full validation.
|
|
28
|
-
*/
|
|
29
|
-
function invalidProjectPatchOperations(patches) {
|
|
30
|
-
if (patches.length === 0)
|
|
31
|
-
return 'patches 必须是非空数组';
|
|
32
|
-
if (patches.length > CONFIG_PATCH_LIMITS.maxOperations)
|
|
33
|
-
return `一次最多 ${CONFIG_PATCH_LIMITS.maxOperations} 个 patch`;
|
|
34
|
-
for (const [index, item] of patches.entries()) {
|
|
35
|
-
if (!item || typeof item !== 'object' || Array.isArray(item))
|
|
36
|
-
return `patches[${index}] 必须是对象`;
|
|
37
|
-
const operation = item;
|
|
38
|
-
const path = operation.path;
|
|
39
|
-
if (!Array.isArray(path) || path.length === 0 || path.length > CONFIG_PATCH_LIMITS.maxPathDepth
|
|
40
|
-
|| path.some(segment => typeof segment !== 'string' || !segment
|
|
41
|
-
|| segment.length > CONFIG_PATCH_LIMITS.maxSegmentLength || FORBIDDEN_PATCH_SEGMENTS.has(segment))) {
|
|
42
|
-
return `patches[${index}].path 必须是 1–${CONFIG_PATCH_LIMITS.maxPathDepth} 段的有效字符串数组`;
|
|
43
|
-
}
|
|
44
|
-
if (operation.op === 'set') {
|
|
45
|
-
if (!Object.hasOwn(operation, 'value') || operation.value === undefined)
|
|
46
|
-
return `patches[${index}] 的 set 需要 value`;
|
|
47
|
-
}
|
|
48
|
-
else if (operation.op !== 'remove')
|
|
49
|
-
return `patches[${index}].op 只能是 set 或 remove`;
|
|
50
|
-
}
|
|
51
|
-
return undefined;
|
|
52
|
-
}
|
|
53
12
|
export async function dispatchRuntimeConsoleRpc(message, ctx) {
|
|
54
13
|
const payloads = [];
|
|
55
14
|
const emit = (payload) => {
|
|
@@ -404,503 +363,10 @@ export async function dispatchRuntimeConsoleRpc(message, ctx) {
|
|
|
404
363
|
}
|
|
405
364
|
return payloads;
|
|
406
365
|
}
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
const tree = ctx.listProjectFiles ? await ctx.listProjectFiles() : [];
|
|
410
|
-
emit({ requestId, data: { tree } });
|
|
411
|
-
}
|
|
412
|
-
catch (error) {
|
|
413
|
-
emit({
|
|
414
|
-
requestId,
|
|
415
|
-
error: `Failed to build file tree: ${error instanceof Error ? error.message : String(error)}`,
|
|
416
|
-
});
|
|
417
|
-
}
|
|
418
|
-
return payloads;
|
|
419
|
-
}
|
|
420
|
-
case 'files:read': {
|
|
421
|
-
try {
|
|
422
|
-
const filePath = String(message.filePath ?? '');
|
|
423
|
-
if (!filePath) {
|
|
424
|
-
emit({ requestId, error: 'filePath is required' });
|
|
425
|
-
return payloads;
|
|
426
|
-
}
|
|
427
|
-
if (!ctx.readProjectFile) {
|
|
428
|
-
emit({ requestId, error: 'File read is not configured' });
|
|
429
|
-
return payloads;
|
|
430
|
-
}
|
|
431
|
-
const file = await ctx.readProjectFile(filePath);
|
|
432
|
-
emit({ requestId, data: file });
|
|
433
|
-
}
|
|
434
|
-
catch (error) {
|
|
435
|
-
emit({
|
|
436
|
-
requestId,
|
|
437
|
-
error: `Failed to read file: ${error instanceof Error ? error.message : String(error)}`,
|
|
438
|
-
});
|
|
439
|
-
}
|
|
440
|
-
return payloads;
|
|
441
|
-
}
|
|
442
|
-
case 'files:save': {
|
|
443
|
-
try {
|
|
444
|
-
const filePath = String(message.filePath ?? '');
|
|
445
|
-
const content = message.content;
|
|
446
|
-
if (!filePath) {
|
|
447
|
-
emit({ requestId, error: 'filePath is required' });
|
|
448
|
-
return payloads;
|
|
449
|
-
}
|
|
450
|
-
if (typeof content !== 'string') {
|
|
451
|
-
emit({ requestId, error: 'content field is required' });
|
|
452
|
-
return payloads;
|
|
453
|
-
}
|
|
454
|
-
if (!ctx.saveProjectFile) {
|
|
455
|
-
emit({ requestId, error: 'File write is not configured' });
|
|
456
|
-
return payloads;
|
|
457
|
-
}
|
|
458
|
-
await ctx.saveProjectFile(filePath, content);
|
|
459
|
-
emit({ requestId, data: { success: true, message: `文件已保存: ${filePath}` } });
|
|
460
|
-
}
|
|
461
|
-
catch (error) {
|
|
462
|
-
emit({
|
|
463
|
-
requestId,
|
|
464
|
-
error: `Failed to save file: ${error instanceof Error ? error.message : String(error)}`,
|
|
465
|
-
});
|
|
466
|
-
}
|
|
467
|
-
return payloads;
|
|
468
|
-
}
|
|
469
|
-
case 'env:list': {
|
|
470
|
-
try {
|
|
471
|
-
const files = ctx.listEnvFiles ? await ctx.listEnvFiles() : [];
|
|
472
|
-
emit({ requestId, data: { files } });
|
|
473
|
-
}
|
|
474
|
-
catch (error) {
|
|
475
|
-
emit({
|
|
476
|
-
requestId,
|
|
477
|
-
error: `Failed to list env files: ${error instanceof Error ? error.message : String(error)}`,
|
|
478
|
-
});
|
|
479
|
-
}
|
|
480
|
-
return payloads;
|
|
481
|
-
}
|
|
482
|
-
case 'env:get': {
|
|
483
|
-
try {
|
|
484
|
-
const filename = String(message.filename ?? '');
|
|
485
|
-
if (!filename) {
|
|
486
|
-
emit({ requestId, error: 'filename is required' });
|
|
487
|
-
return payloads;
|
|
488
|
-
}
|
|
489
|
-
if (!ctx.readEnvFile) {
|
|
490
|
-
emit({ requestId, error: 'Env read is not configured' });
|
|
491
|
-
return payloads;
|
|
492
|
-
}
|
|
493
|
-
const content = await ctx.readEnvFile(filename);
|
|
494
|
-
emit({ requestId, data: { content } });
|
|
495
|
-
}
|
|
496
|
-
catch (error) {
|
|
497
|
-
emit({
|
|
498
|
-
requestId,
|
|
499
|
-
error: `Failed to read env file: ${error instanceof Error ? error.message : String(error)}`,
|
|
500
|
-
});
|
|
501
|
-
}
|
|
502
|
-
return payloads;
|
|
503
|
-
}
|
|
504
|
-
case 'env:save': {
|
|
505
|
-
try {
|
|
506
|
-
const filename = String(message.filename ?? '');
|
|
507
|
-
const content = message.content;
|
|
508
|
-
if (!filename) {
|
|
509
|
-
emit({ requestId, error: 'filename is required' });
|
|
510
|
-
return payloads;
|
|
511
|
-
}
|
|
512
|
-
if (typeof content !== 'string') {
|
|
513
|
-
emit({ requestId, error: 'content field is required' });
|
|
514
|
-
return payloads;
|
|
515
|
-
}
|
|
516
|
-
if (!ctx.writeEnvFile) {
|
|
517
|
-
emit({ requestId, error: 'Env write is not configured' });
|
|
518
|
-
return payloads;
|
|
519
|
-
}
|
|
520
|
-
await ctx.writeEnvFile(filename, content);
|
|
521
|
-
emit({ requestId, data: { success: true, message: `环境文件已保存: ${filename}` } });
|
|
522
|
-
}
|
|
523
|
-
catch (error) {
|
|
524
|
-
emit({
|
|
525
|
-
requestId,
|
|
526
|
-
error: `Failed to save env file: ${error instanceof Error ? error.message : String(error)}`,
|
|
527
|
-
});
|
|
528
|
-
}
|
|
529
|
-
return payloads;
|
|
530
|
-
}
|
|
531
|
-
case 'schema:get': {
|
|
532
|
-
try {
|
|
533
|
-
const pluginName = typeof message.pluginName === 'string'
|
|
534
|
-
? message.pluginName
|
|
535
|
-
: undefined;
|
|
536
|
-
const schema = ctx.getSchema ? await ctx.getSchema(pluginName) : null;
|
|
537
|
-
emit({ requestId, data: schema });
|
|
538
|
-
}
|
|
539
|
-
catch (error) {
|
|
540
|
-
emit({
|
|
541
|
-
requestId,
|
|
542
|
-
error: `Failed to get schema: ${error instanceof Error ? error.message : String(error)}`,
|
|
543
|
-
});
|
|
544
|
-
}
|
|
545
|
-
return payloads;
|
|
546
|
-
}
|
|
547
|
-
case 'schema:get-all': {
|
|
548
|
-
try {
|
|
549
|
-
const schemas = ctx.getAllSchemas ? await ctx.getAllSchemas() : {};
|
|
550
|
-
emit({ requestId, data: schemas });
|
|
551
|
-
}
|
|
552
|
-
catch (error) {
|
|
553
|
-
emit({
|
|
554
|
-
requestId,
|
|
555
|
-
error: `Failed to get all schemas: ${error instanceof Error ? error.message : String(error)}`,
|
|
556
|
-
});
|
|
557
|
-
}
|
|
558
|
-
return payloads;
|
|
559
|
-
}
|
|
560
|
-
case 'db:info': {
|
|
561
|
-
if (ctx.database) {
|
|
562
|
-
emit({ requestId, data: ctx.database.info() });
|
|
563
|
-
return payloads;
|
|
564
|
-
}
|
|
565
|
-
if (ctx.dbInfo) {
|
|
566
|
-
try {
|
|
567
|
-
emit({ requestId, data: await ctx.dbInfo() });
|
|
568
|
-
}
|
|
569
|
-
catch (error) {
|
|
570
|
-
emit({
|
|
571
|
-
requestId,
|
|
572
|
-
error: error instanceof Error ? error.message : String(error),
|
|
573
|
-
});
|
|
574
|
-
}
|
|
366
|
+
default: {
|
|
367
|
+
if (await dispatchHostResourceConsoleRpc(type, message, ctx, requestId, payloads)) {
|
|
575
368
|
return payloads;
|
|
576
369
|
}
|
|
577
|
-
emit({
|
|
578
|
-
requestId,
|
|
579
|
-
data: { dialect: null, connected: false, message: 'Database not configured on Plugin Runtime Host' },
|
|
580
|
-
});
|
|
581
|
-
return payloads;
|
|
582
|
-
}
|
|
583
|
-
case 'db:tables': {
|
|
584
|
-
try {
|
|
585
|
-
if (ctx.database) {
|
|
586
|
-
emit({ requestId, data: { tables: ctx.database.tables() } });
|
|
587
|
-
return payloads;
|
|
588
|
-
}
|
|
589
|
-
const tables = ctx.dbTables ? await ctx.dbTables() : [];
|
|
590
|
-
emit({ requestId, data: tables });
|
|
591
|
-
}
|
|
592
|
-
catch (error) {
|
|
593
|
-
emit({
|
|
594
|
-
requestId,
|
|
595
|
-
error: error instanceof Error ? error.message : String(error),
|
|
596
|
-
});
|
|
597
|
-
}
|
|
598
|
-
return payloads;
|
|
599
|
-
}
|
|
600
|
-
case 'db:select': {
|
|
601
|
-
const table = stringField(message, 'table');
|
|
602
|
-
if (!table)
|
|
603
|
-
return errorReply(payloads, requestId, 'table is required');
|
|
604
|
-
const page = positiveInteger(message.page, 1);
|
|
605
|
-
const pageSize = Math.min(positiveInteger(message.pageSize, 50), 500);
|
|
606
|
-
const where = recordField(message.where);
|
|
607
|
-
if (!ctx.database) {
|
|
608
|
-
emit({ requestId, data: { rows: [], total: 0, page, pageSize } });
|
|
609
|
-
return payloads;
|
|
610
|
-
}
|
|
611
|
-
try {
|
|
612
|
-
emit({ requestId, data: await ctx.database.select(table, page, pageSize, where) });
|
|
613
|
-
}
|
|
614
|
-
catch (error) {
|
|
615
|
-
emit({ requestId, error: `Failed to select: ${errorMessage(error)}` });
|
|
616
|
-
}
|
|
617
|
-
return payloads;
|
|
618
|
-
}
|
|
619
|
-
case 'db:insert': {
|
|
620
|
-
if (!ctx.database)
|
|
621
|
-
return errorReply(payloads, requestId, 'Database RPC is not available on Plugin Runtime Host');
|
|
622
|
-
const table = stringField(message, 'table');
|
|
623
|
-
const row = recordField(message.row);
|
|
624
|
-
if (!table || !row)
|
|
625
|
-
return errorReply(payloads, requestId, 'table and row are required');
|
|
626
|
-
try {
|
|
627
|
-
await ctx.database.insert(table, row);
|
|
628
|
-
emit({ requestId, data: { success: true } });
|
|
629
|
-
}
|
|
630
|
-
catch (error) {
|
|
631
|
-
emit({ requestId, error: `Failed to insert: ${errorMessage(error)}` });
|
|
632
|
-
}
|
|
633
|
-
return payloads;
|
|
634
|
-
}
|
|
635
|
-
case 'db:update': {
|
|
636
|
-
if (!ctx.database)
|
|
637
|
-
return errorReply(payloads, requestId, 'Database RPC is not available on Plugin Runtime Host');
|
|
638
|
-
const table = stringField(message, 'table');
|
|
639
|
-
const row = recordField(message.row);
|
|
640
|
-
const where = recordField(message.where);
|
|
641
|
-
if (!table || !row || !where) {
|
|
642
|
-
return errorReply(payloads, requestId, 'table, row, and where are required');
|
|
643
|
-
}
|
|
644
|
-
try {
|
|
645
|
-
const affected = await ctx.database.update(table, row, where);
|
|
646
|
-
emit({ requestId, data: { success: true, affected } });
|
|
647
|
-
}
|
|
648
|
-
catch (error) {
|
|
649
|
-
emit({ requestId, error: `Failed to update: ${errorMessage(error)}` });
|
|
650
|
-
}
|
|
651
|
-
return payloads;
|
|
652
|
-
}
|
|
653
|
-
case 'db:delete': {
|
|
654
|
-
if (!ctx.database)
|
|
655
|
-
return errorReply(payloads, requestId, 'Database RPC is not available on Plugin Runtime Host');
|
|
656
|
-
const table = stringField(message, 'table');
|
|
657
|
-
const where = recordField(message.where);
|
|
658
|
-
if (!table || !where)
|
|
659
|
-
return errorReply(payloads, requestId, 'table and where are required');
|
|
660
|
-
try {
|
|
661
|
-
const deleted = await ctx.database.delete(table, where);
|
|
662
|
-
emit({ requestId, data: { success: true, deleted } });
|
|
663
|
-
}
|
|
664
|
-
catch (error) {
|
|
665
|
-
emit({ requestId, error: `Failed to delete: ${errorMessage(error)}` });
|
|
666
|
-
}
|
|
667
|
-
return payloads;
|
|
668
|
-
}
|
|
669
|
-
case 'db:drop-table': {
|
|
670
|
-
if (!ctx.database)
|
|
671
|
-
return errorReply(payloads, requestId, 'Database RPC is not available on Plugin Runtime Host');
|
|
672
|
-
const table = stringField(message, 'table');
|
|
673
|
-
if (!table)
|
|
674
|
-
return errorReply(payloads, requestId, 'table is required');
|
|
675
|
-
try {
|
|
676
|
-
await ctx.database.dropTable(table);
|
|
677
|
-
emit({ requestId, data: { success: true } });
|
|
678
|
-
}
|
|
679
|
-
catch (error) {
|
|
680
|
-
emit({ requestId, error: `Failed to drop table: ${errorMessage(error)}` });
|
|
681
|
-
}
|
|
682
|
-
return payloads;
|
|
683
|
-
}
|
|
684
|
-
case 'db:kv:get': {
|
|
685
|
-
const table = stringField(message, 'table');
|
|
686
|
-
const key = stringField(message, 'key');
|
|
687
|
-
if (!table || !key)
|
|
688
|
-
return errorReply(payloads, requestId, 'table and key are required');
|
|
689
|
-
if (!ctx.database) {
|
|
690
|
-
emit({ requestId, data: { key, value: null } });
|
|
691
|
-
return payloads;
|
|
692
|
-
}
|
|
693
|
-
try {
|
|
694
|
-
emit({ requestId, data: { key, value: await ctx.database.kvGet(table, key) } });
|
|
695
|
-
}
|
|
696
|
-
catch (error) {
|
|
697
|
-
emit({ requestId, error: `Failed to get kv: ${errorMessage(error)}` });
|
|
698
|
-
}
|
|
699
|
-
return payloads;
|
|
700
|
-
}
|
|
701
|
-
case 'db:kv:set': {
|
|
702
|
-
if (!ctx.database)
|
|
703
|
-
return errorReply(payloads, requestId, 'Database RPC is not available on Plugin Runtime Host');
|
|
704
|
-
const table = stringField(message, 'table');
|
|
705
|
-
const key = stringField(message, 'key');
|
|
706
|
-
if (!table || !key)
|
|
707
|
-
return errorReply(payloads, requestId, 'table and key are required');
|
|
708
|
-
try {
|
|
709
|
-
const ttl = typeof message.ttl === 'number' && Number.isFinite(message.ttl)
|
|
710
|
-
? message.ttl
|
|
711
|
-
: undefined;
|
|
712
|
-
await ctx.database.kvSet(table, key, message.value, ttl);
|
|
713
|
-
emit({ requestId, data: { success: true } });
|
|
714
|
-
}
|
|
715
|
-
catch (error) {
|
|
716
|
-
emit({ requestId, error: `Failed to set kv: ${errorMessage(error)}` });
|
|
717
|
-
}
|
|
718
|
-
return payloads;
|
|
719
|
-
}
|
|
720
|
-
case 'db:kv:delete': {
|
|
721
|
-
if (!ctx.database)
|
|
722
|
-
return errorReply(payloads, requestId, 'Database RPC is not available on Plugin Runtime Host');
|
|
723
|
-
const table = stringField(message, 'table');
|
|
724
|
-
const key = stringField(message, 'key');
|
|
725
|
-
if (!table || !key)
|
|
726
|
-
return errorReply(payloads, requestId, 'table and key are required');
|
|
727
|
-
try {
|
|
728
|
-
await ctx.database.kvDelete(table, key);
|
|
729
|
-
emit({ requestId, data: { success: true } });
|
|
730
|
-
}
|
|
731
|
-
catch (error) {
|
|
732
|
-
emit({ requestId, error: `Failed to delete kv: ${errorMessage(error)}` });
|
|
733
|
-
}
|
|
734
|
-
return payloads;
|
|
735
|
-
}
|
|
736
|
-
case 'db:kv:entries': {
|
|
737
|
-
const table = stringField(message, 'table');
|
|
738
|
-
if (!table)
|
|
739
|
-
return errorReply(payloads, requestId, 'table is required');
|
|
740
|
-
if (!ctx.database) {
|
|
741
|
-
emit({ requestId, data: { entries: [] } });
|
|
742
|
-
return payloads;
|
|
743
|
-
}
|
|
744
|
-
try {
|
|
745
|
-
emit({ requestId, data: { entries: await ctx.database.kvEntries(table) } });
|
|
746
|
-
}
|
|
747
|
-
catch (error) {
|
|
748
|
-
emit({ requestId, error: `Failed to get entries: ${errorMessage(error)}` });
|
|
749
|
-
}
|
|
750
|
-
return payloads;
|
|
751
|
-
}
|
|
752
|
-
case 'endpoint.list': {
|
|
753
|
-
try {
|
|
754
|
-
const endpoints = ctx.listEndpoints ? await ctx.listEndpoints() : [];
|
|
755
|
-
emit({ requestId, data: { endpoints } });
|
|
756
|
-
}
|
|
757
|
-
catch (error) {
|
|
758
|
-
emit({
|
|
759
|
-
requestId,
|
|
760
|
-
error: error instanceof Error ? error.message : String(error),
|
|
761
|
-
});
|
|
762
|
-
}
|
|
763
|
-
return payloads;
|
|
764
|
-
}
|
|
765
|
-
case 'endpoint.info': {
|
|
766
|
-
try {
|
|
767
|
-
const data = recordField(message.data) ?? message;
|
|
768
|
-
const adapter = String(data.adapter ?? '');
|
|
769
|
-
const endpointKey = String(data.endpointKey ?? '');
|
|
770
|
-
if (!adapter || !endpointKey) {
|
|
771
|
-
emit({ requestId, error: 'adapter and endpointKey are required' });
|
|
772
|
-
return payloads;
|
|
773
|
-
}
|
|
774
|
-
if (!ctx.getEndpoint) {
|
|
775
|
-
emit({ requestId, error: 'Endpoint registry is not configured' });
|
|
776
|
-
return payloads;
|
|
777
|
-
}
|
|
778
|
-
const endpoint = await ctx.getEndpoint(adapter, endpointKey);
|
|
779
|
-
if (!endpoint) {
|
|
780
|
-
emit({ requestId, error: 'endpoint not found' });
|
|
781
|
-
return payloads;
|
|
782
|
-
}
|
|
783
|
-
emit({ requestId, data: endpoint });
|
|
784
|
-
}
|
|
785
|
-
catch (error) {
|
|
786
|
-
emit({
|
|
787
|
-
requestId,
|
|
788
|
-
error: error instanceof Error ? error.message : String(error),
|
|
789
|
-
});
|
|
790
|
-
}
|
|
791
|
-
return payloads;
|
|
792
|
-
}
|
|
793
|
-
case ENDPOINT_RPC.TEST: {
|
|
794
|
-
const data = recordField(message.data) ?? message;
|
|
795
|
-
const adapter = stringField(data, 'adapter');
|
|
796
|
-
const endpointKey = stringField(data, 'endpointKey');
|
|
797
|
-
if (!adapter || !endpointKey) {
|
|
798
|
-
emit({ requestId, error: 'adapter and endpointKey are required' });
|
|
799
|
-
return payloads;
|
|
800
|
-
}
|
|
801
|
-
if (!ctx.getEndpoint) {
|
|
802
|
-
emit({ requestId, error: 'Endpoint registry is not configured' });
|
|
803
|
-
return payloads;
|
|
804
|
-
}
|
|
805
|
-
const startedAt = Date.now();
|
|
806
|
-
try {
|
|
807
|
-
const endpoint = await ctx.getEndpoint(adapter, endpointKey);
|
|
808
|
-
if (!endpoint) {
|
|
809
|
-
emit({ requestId, data: {
|
|
810
|
-
reachable: false,
|
|
811
|
-
connected: false,
|
|
812
|
-
phase: 'unconfigured',
|
|
813
|
-
latencyMs: Date.now() - startedAt,
|
|
814
|
-
message: 'Endpoint 未注册到当前 generation',
|
|
815
|
-
} });
|
|
816
|
-
return payloads;
|
|
817
|
-
}
|
|
818
|
-
emit({ requestId, data: {
|
|
819
|
-
reachable: endpoint.connected && endpoint.status === 'online',
|
|
820
|
-
connected: endpoint.connected,
|
|
821
|
-
status: endpoint.status,
|
|
822
|
-
admitted: endpoint.admitted,
|
|
823
|
-
transportState: endpoint.transportState,
|
|
824
|
-
phase: endpoint.phase ?? (endpoint.connected ? 'online' : 'pending'),
|
|
825
|
-
pendingLogin: endpoint.pendingLogin ?? false,
|
|
826
|
-
latencyMs: Date.now() - startedAt,
|
|
827
|
-
message: endpoint.connected
|
|
828
|
-
? 'Endpoint 已连接'
|
|
829
|
-
: endpoint.pendingLogin
|
|
830
|
-
? 'Endpoint 等待登录操作'
|
|
831
|
-
: 'Endpoint 当前离线',
|
|
832
|
-
} });
|
|
833
|
-
}
|
|
834
|
-
catch (error) {
|
|
835
|
-
emit({ requestId, data: {
|
|
836
|
-
reachable: false,
|
|
837
|
-
connected: false,
|
|
838
|
-
phase: 'failed',
|
|
839
|
-
latencyMs: Date.now() - startedAt,
|
|
840
|
-
message: error instanceof Error ? error.message : String(error),
|
|
841
|
-
} });
|
|
842
|
-
}
|
|
843
|
-
return payloads;
|
|
844
|
-
}
|
|
845
|
-
case 'endpoint.send_message': {
|
|
846
|
-
try {
|
|
847
|
-
const data = recordField(message.data) ?? message;
|
|
848
|
-
const adapter = String(data.adapter ?? '');
|
|
849
|
-
const endpointKey = String(data.endpointKey ?? '');
|
|
850
|
-
const channelId = String(data.channelId ?? data.id ?? '');
|
|
851
|
-
const channelType = String(data.channelType ?? data.type ?? '');
|
|
852
|
-
const content = data.content;
|
|
853
|
-
if (!adapter || !endpointKey || !channelId || !channelType || content === undefined) {
|
|
854
|
-
emit({
|
|
855
|
-
requestId,
|
|
856
|
-
error: 'adapter, endpointKey, channelId, channelType, and content are required',
|
|
857
|
-
});
|
|
858
|
-
return payloads;
|
|
859
|
-
}
|
|
860
|
-
if (!ctx.sendEndpointMessage) {
|
|
861
|
-
emit({ requestId, error: 'Endpoint send is not configured' });
|
|
862
|
-
return payloads;
|
|
863
|
-
}
|
|
864
|
-
const result = await ctx.sendEndpointMessage({
|
|
865
|
-
adapter,
|
|
866
|
-
endpointKey,
|
|
867
|
-
conversation: wireConversation(channelType, channelId, data.parent),
|
|
868
|
-
content,
|
|
869
|
-
});
|
|
870
|
-
emit({ requestId, data: { messageId: result.messageId } });
|
|
871
|
-
}
|
|
872
|
-
catch (error) {
|
|
873
|
-
emit({
|
|
874
|
-
requestId,
|
|
875
|
-
error: error instanceof Error ? error.message : String(error),
|
|
876
|
-
});
|
|
877
|
-
}
|
|
878
|
-
return payloads;
|
|
879
|
-
}
|
|
880
|
-
case 'system:restart': {
|
|
881
|
-
try {
|
|
882
|
-
if (!ctx.requestRestart) {
|
|
883
|
-
emit({ requestId, error: 'Process restart is not configured' });
|
|
884
|
-
return payloads;
|
|
885
|
-
}
|
|
886
|
-
ctx.publishEvent?.('system:restarting', { timestamp: Date.now() });
|
|
887
|
-
emit({ requestId, data: { success: true, message: '正在重启...' } });
|
|
888
|
-
// Delay so the HTTP response can flush (parity with legacy host-api).
|
|
889
|
-
setTimeout(() => {
|
|
890
|
-
void Promise.resolve(ctx.requestRestart?.()).catch(() => {
|
|
891
|
-
/* restart is best-effort */
|
|
892
|
-
});
|
|
893
|
-
}, 500);
|
|
894
|
-
}
|
|
895
|
-
catch (error) {
|
|
896
|
-
emit({
|
|
897
|
-
requestId,
|
|
898
|
-
error: error instanceof Error ? error.message : String(error),
|
|
899
|
-
});
|
|
900
|
-
}
|
|
901
|
-
return payloads;
|
|
902
|
-
}
|
|
903
|
-
default:
|
|
904
370
|
if (type.startsWith('db:')) {
|
|
905
371
|
emit({
|
|
906
372
|
requestId,
|
|
@@ -910,86 +376,6 @@ export async function dispatchRuntimeConsoleRpc(message, ctx) {
|
|
|
910
376
|
}
|
|
911
377
|
emit({ requestId, error: `Unknown RPC type: ${type || '<empty>'}` });
|
|
912
378
|
return payloads;
|
|
379
|
+
}
|
|
913
380
|
}
|
|
914
381
|
}
|
|
915
|
-
function stringField(message, key) {
|
|
916
|
-
const value = message[key];
|
|
917
|
-
return typeof value === 'string' ? value.trim() : '';
|
|
918
|
-
}
|
|
919
|
-
function recordField(value) {
|
|
920
|
-
return value && typeof value === 'object' && !Array.isArray(value)
|
|
921
|
-
? value
|
|
922
|
-
: undefined;
|
|
923
|
-
}
|
|
924
|
-
function positiveInteger(value, fallback) {
|
|
925
|
-
const parsed = Number(value ?? fallback);
|
|
926
|
-
return Number.isInteger(parsed) && parsed > 0 ? parsed : fallback;
|
|
927
|
-
}
|
|
928
|
-
function errorMessage(error) {
|
|
929
|
-
return error instanceof Error ? error.message : String(error);
|
|
930
|
-
}
|
|
931
|
-
function errorReply(payloads, requestId, error) {
|
|
932
|
-
payloads.push({ requestId, error });
|
|
933
|
-
return payloads;
|
|
934
|
-
}
|
|
935
|
-
/** HTTP paths under apiBase that demo scope may call (ADR 0016). */
|
|
936
|
-
export function isDemoHttpAllowed(method, pathname, apiBase) {
|
|
937
|
-
const normalizedBase = apiBase.replace(/\/$/u, '') || '/api';
|
|
938
|
-
const m = method.toUpperCase();
|
|
939
|
-
// Each inference handler resolves a static authenticated personal binding before reading or mutating anything.
|
|
940
|
-
if (m === 'GET' && ['', '/owners', '/export', '/mutation', '/sessions', '/session'].some(path => pathname === `${normalizedBase}/agent/inference${path}`))
|
|
941
|
-
return true;
|
|
942
|
-
if (m === 'PUT' && pathname === `${normalizedBase}/agent/inference/session`)
|
|
943
|
-
return true;
|
|
944
|
-
if (m === 'POST' && ['/plan', '/apply', '/key/prepare', '/discover', '/profile-schema'].some(path => pathname === `${normalizedBase}/agent/inference${path}`))
|
|
945
|
-
return true;
|
|
946
|
-
if (m === 'GET' && pathname === `${normalizedBase}/events`)
|
|
947
|
-
return true;
|
|
948
|
-
if (m === 'GET' && pathname === `${normalizedBase}/events/history`)
|
|
949
|
-
return true;
|
|
950
|
-
if (m === 'GET' && DEMO_INTROSPECTION_PATHS.has(pathname.slice(normalizedBase.length)))
|
|
951
|
-
return true;
|
|
952
|
-
if (m === 'POST' && pathname === `${normalizedBase}/console/request`)
|
|
953
|
-
return true;
|
|
954
|
-
// Read-only dashboard / plugin inspection routes (legacy host-api parity).
|
|
955
|
-
if (m === 'GET' && pathname === `${normalizedBase}/system/status`)
|
|
956
|
-
return true;
|
|
957
|
-
if (m === 'GET' && pathname === `${normalizedBase}/stats`)
|
|
958
|
-
return true;
|
|
959
|
-
if (m === 'GET' && (pathname === `${normalizedBase}/plugins`
|
|
960
|
-
|| pathname.startsWith(`${normalizedBase}/plugins/`)))
|
|
961
|
-
return true;
|
|
962
|
-
return false;
|
|
963
|
-
}
|
|
964
|
-
function configurationSaveMessage(result) {
|
|
965
|
-
if (result.applicationError === 'superseded')
|
|
966
|
-
return '配置已保存,但该次生效申请已被替代';
|
|
967
|
-
if (result.applicationError === 'invalid')
|
|
968
|
-
return '配置已保存,但配置校验失败,未生效';
|
|
969
|
-
if (result.applicationError)
|
|
970
|
-
return '配置已保存,生效结果未确认';
|
|
971
|
-
if (result.application) {
|
|
972
|
-
if (result.revision && result.application.sourceRevision !== result.revision)
|
|
973
|
-
return '配置已保存,但已被较新修订替代';
|
|
974
|
-
if (result.application.groups.some(group => group.status === 'unknown'))
|
|
975
|
-
return '配置已保存,生效结果未确认';
|
|
976
|
-
if (result.application.groups.some(group => group.status === 'superseded'))
|
|
977
|
-
return '配置已保存,但该次生效申请已被替代';
|
|
978
|
-
if (result.application.groups.some(group => ['failed', 'blocked'].includes(group.status)))
|
|
979
|
-
return '配置已保存,部分配置未生效';
|
|
980
|
-
if (result.restartRequired)
|
|
981
|
-
return '配置已保存,部分配置待重启生效';
|
|
982
|
-
return '配置已保存并已生效';
|
|
983
|
-
}
|
|
984
|
-
return result.restartRequired ? '配置已保存,需重启进程才能生效' : '配置已保存';
|
|
985
|
-
}
|
|
986
|
-
const DEMO_INTROSPECTION_PATHS = new Set([
|
|
987
|
-
'/introspection/commands',
|
|
988
|
-
'/introspection/middlewares',
|
|
989
|
-
'/introspection/components',
|
|
990
|
-
'/introspection/endpoints',
|
|
991
|
-
'/introspection/bindings',
|
|
992
|
-
'/introspection/tools',
|
|
993
|
-
'/introspection/prompt-sections',
|
|
994
|
-
'/introspection/mcp',
|
|
995
|
-
]);
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type ConfigDocumentPort, type RootResourceInstaller, type RuntimeConfigDocument } from '@zhin.js/plugin-runtime';
|
|
2
|
+
import { type HttpHostOptions, type ProcessHttpHost } from './http-host.js';
|
|
3
|
+
export interface ResolvedHttpConfig extends HttpHostOptions {
|
|
4
|
+
/** Additional listeners that mirror the primary Host route surface. */
|
|
5
|
+
readonly listeners?: readonly HttpHostOptions[];
|
|
6
|
+
}
|
|
7
|
+
export declare function resolveHttpConfig(config: RuntimeConfigDocument | ConfigDocumentPort, envOverlay?: Readonly<Record<string, string | undefined>>, projectRoot?: string): Promise<ResolvedHttpConfig>;
|
|
8
|
+
export declare function installHttpHost(host: ProcessHttpHost): RootResourceInstaller;
|