@zhin.js/core 1.5.16 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +78 -195
- package/lib/ai-completion-contract.d.ts +135 -0
- package/lib/ai-completion-contract.js +69 -0
- package/lib/built/ai-access.d.ts +2 -21
- package/lib/built/ai-access.js +1 -46
- package/lib/built/ai-outbound/index.d.ts +0 -2
- package/lib/built/ai-outbound/index.js +0 -2
- package/lib/built/ai-outbound/prompt.d.ts +2 -0
- package/lib/built/ai-outbound/prompt.js +2 -0
- package/lib/built/ai-outbound/types.d.ts +1 -10
- package/lib/built/ai-trigger.d.ts +39 -78
- package/lib/built/ai-trigger.js +3 -254
- package/lib/built/common-adapter-tools.d.ts +21 -6
- package/lib/built/common-adapter-tools.js +28 -17
- package/lib/built/generic-segment-mapper.js +4 -1
- package/lib/built/html-segment-fallback.d.ts +6 -7
- package/lib/built/html-segment-fallback.js +1 -16
- package/lib/built/html-to-text.d.ts +0 -3
- package/lib/built/html-to-text.js +131 -40
- package/lib/built/interaction-messages.d.ts +1 -0
- package/lib/built/interaction-messages.js +30 -0
- package/lib/built/interactive-segments/action.d.ts +3 -4
- package/lib/built/interactive-segments/action.js +2 -5
- package/lib/built/interactive-segments/index.d.ts +0 -2
- package/lib/built/interactive-segments/index.js +0 -2
- package/lib/built/interactive-segments/keyboard-segment.d.ts +0 -1
- package/lib/built/interactive-segments/keyboard-segment.js +0 -1
- package/lib/built/interactive-segments/onebot-keyboard.d.ts +0 -1
- package/lib/built/interactive-segments/onebot-keyboard.js +0 -2
- package/lib/built/interactive-segments/resolve.d.ts +2 -3
- package/lib/built/interactive-segments/resolve.js +21 -14
- package/lib/built/interactive-segments/types.d.ts +2 -7
- package/lib/built/interactive-segments/types.js +22 -2
- package/lib/built/login-assist.d.ts +13 -11
- package/lib/built/login-assist.js +32 -26
- package/lib/built/message-enrich.d.ts +24 -27
- package/lib/built/message-enrich.js +53 -49
- package/lib/built/outbound-content-arrays.d.ts +4 -0
- package/lib/built/outbound-content-arrays.js +32 -0
- package/lib/built/outbound-media-trust.d.ts +46 -0
- package/lib/built/outbound-media-trust.js +245 -0
- package/lib/built/outbound-media-utils.d.ts +5 -3
- package/lib/built/outbound-media-utils.js +5 -3
- package/lib/built/readable-link.d.ts +4 -0
- package/lib/built/readable-link.js +32 -0
- package/lib/built/rich-segments/markdown-to-text.d.ts +1 -2
- package/lib/built/rich-segments/markdown-to-text.js +141 -17
- package/lib/built/roles.d.ts +141 -12
- package/lib/built/roles.js +253 -52
- package/lib/built/segment-contract/assert.js +8 -2
- package/lib/built/segment-contract/delivery.js +1 -0
- package/lib/built/segment-contract/index.d.ts +3 -3
- package/lib/built/segment-contract/index.js +2 -2
- package/lib/built/segment-contract/json-schema.d.ts +39 -5
- package/lib/built/segment-contract/json-schema.js +207 -24
- package/lib/built/segment-contract/preview.js +8 -1
- package/lib/built/segment-contract/text.js +1 -1
- package/lib/built/segment-contract/types.d.ts +2 -2
- package/lib/built/segment-contract/validate.d.ts +1553 -224
- package/lib/built/segment-contract/validate.js +39 -3
- package/lib/built/tool-access.d.ts +5 -0
- package/lib/built/tool-access.js +17 -0
- package/lib/built/user-interaction.d.ts +5 -4
- package/lib/built/user-interaction.js +64 -52
- package/lib/errors.d.ts +1 -18
- package/lib/errors.js +1 -26
- package/lib/feature/command-public-api.d.ts +5 -0
- package/lib/feature/command-public-api.js +5 -0
- package/lib/feature/command.d.ts +2 -0
- package/lib/feature/command.js +1 -0
- package/lib/feature/component.d.ts +1 -0
- package/lib/feature/component.js +1 -0
- package/lib/feature/handler.d.ts +13 -7
- package/lib/feature/handler.js +2 -1
- package/lib/feature/middleware-public-api.d.ts +6 -0
- package/lib/feature/middleware-public-api.js +6 -0
- package/lib/feature/middleware.d.ts +22 -2
- package/lib/feature/middleware.js +7 -2
- package/lib/im-scene.d.ts +4 -3
- package/lib/im-scene.js +9 -9
- package/lib/im-session-id.d.ts +20 -12
- package/lib/im-session-id.js +8 -8
- package/lib/index.d.ts +19 -48
- package/lib/index.js +14 -41
- package/lib/jsx/intrinsic-elements.d.ts +83 -0
- package/lib/jsx/message-renderer.d.ts +13 -0
- package/lib/jsx/message-renderer.js +128 -0
- package/lib/jsx/message-tags.d.ts +133 -0
- package/lib/jsx/message-tags.js +50 -0
- package/lib/jsx-dev-runtime.d.ts +2 -2
- package/lib/jsx-dev-runtime.js +1 -2
- package/lib/jsx-runtime.d.ts +2 -12
- package/lib/jsx-runtime.js +1 -10
- package/lib/jsx.d.ts +6 -30
- package/lib/jsx.js +3 -93
- package/lib/message.d.ts +0 -40
- package/lib/message.js +1 -27
- package/lib/models/system-log.d.ts +0 -2
- package/lib/models/system-log.js +1 -8
- package/lib/models/user.d.ts +0 -2
- package/lib/models/user.js +1 -7
- package/lib/notice.d.ts +38 -33
- package/lib/notice.js +23 -7
- package/lib/plugin-runtime/im/command-feedback.d.ts +59 -0
- package/lib/plugin-runtime/im/command-feedback.js +148 -0
- package/lib/plugin-runtime/im/contracts.d.ts +217 -32
- package/lib/plugin-runtime/im/contracts.js +141 -23
- package/lib/plugin-runtime/im/conversation-runtime.d.ts +42 -0
- package/lib/plugin-runtime/im/conversation-runtime.js +451 -0
- package/lib/plugin-runtime/im/endpoint-runtime.d.ts +89 -0
- package/lib/plugin-runtime/im/endpoint-runtime.js +381 -0
- package/lib/plugin-runtime/im/feature-gate.d.ts +21 -0
- package/lib/plugin-runtime/im/feature-gate.js +88 -0
- package/lib/plugin-runtime/im/host/component-host-installer.d.ts +2 -0
- package/lib/plugin-runtime/im/host/component-host-installer.js +14 -0
- package/lib/plugin-runtime/im/host/conversation-migration.d.ts +5 -0
- package/lib/plugin-runtime/im/host/conversation-migration.js +75 -0
- package/lib/plugin-runtime/im/host/database-config.d.ts +10 -0
- package/lib/plugin-runtime/im/host/database-config.js +114 -0
- package/lib/plugin-runtime/im/host/database-host-installer.d.ts +29 -0
- package/lib/plugin-runtime/im/host/database-host-installer.js +442 -0
- package/lib/plugin-runtime/im/host/database-state.d.ts +9 -0
- package/lib/plugin-runtime/im/host/database-state.js +91 -0
- package/lib/plugin-runtime/im/host/index.d.ts +8 -0
- package/lib/plugin-runtime/im/host/index.js +8 -0
- package/lib/plugin-runtime/im/host/locale-host-installer.d.ts +3 -0
- package/lib/plugin-runtime/im/host/locale-host-installer.js +60 -0
- package/lib/plugin-runtime/im/host/outbound-host-installer.d.ts +8 -0
- package/lib/plugin-runtime/im/host/outbound-host-installer.js +153 -0
- package/lib/plugin-runtime/im/host/sender-enrichment.d.ts +34 -0
- package/lib/plugin-runtime/im/host/sender-enrichment.js +82 -0
- package/lib/plugin-runtime/im/im-runtime.d.ts +40 -111
- package/lib/plugin-runtime/im/im-runtime.js +234 -1059
- package/lib/plugin-runtime/im/inbound-dedup.d.ts +84 -0
- package/lib/plugin-runtime/im/inbound-dedup.js +322 -0
- package/lib/plugin-runtime/im/inbound-origin.d.ts +150 -0
- package/lib/plugin-runtime/im/inbound-origin.js +183 -0
- package/lib/plugin-runtime/im/inbound-runtime.d.ts +59 -0
- package/lib/plugin-runtime/im/inbound-runtime.js +478 -0
- package/lib/plugin-runtime/im/inbound-spine.d.ts +196 -0
- package/lib/plugin-runtime/im/inbound-spine.js +335 -0
- package/lib/plugin-runtime/im/index.d.ts +15 -4
- package/lib/plugin-runtime/im/index.js +11 -4
- package/lib/plugin-runtime/im/interaction-runtime.d.ts +50 -0
- package/lib/plugin-runtime/im/interaction-runtime.js +390 -0
- package/lib/plugin-runtime/im/interactive-router.d.ts +81 -0
- package/lib/plugin-runtime/im/interactive-router.js +382 -0
- package/lib/plugin-runtime/im/message-diagnostics.d.ts +70 -0
- package/lib/plugin-runtime/im/message-diagnostics.js +182 -0
- package/lib/plugin-runtime/im/message-dispatcher.d.ts +18 -4
- package/lib/plugin-runtime/im/message-dispatcher.js +138 -20
- package/lib/plugin-runtime/im/message-events.d.ts +85 -0
- package/lib/plugin-runtime/im/message-events.js +113 -0
- package/lib/plugin-runtime/im/outbound-delivery-runtime.d.ts +39 -0
- package/lib/plugin-runtime/im/outbound-delivery-runtime.js +450 -0
- package/lib/plugin-runtime/im/outbound-renderer.d.ts +52 -3
- package/lib/plugin-runtime/im/outbound-renderer.js +188 -8
- package/lib/plugin-runtime/im/outbound-segments.d.ts +29 -5
- package/lib/plugin-runtime/im/outbound-segments.js +130 -36
- package/lib/plugin-runtime/im/progressive-reply.d.ts +58 -0
- package/lib/plugin-runtime/im/progressive-reply.js +501 -0
- package/lib/plugin-runtime/im/public-api.d.ts +1 -1
- package/lib/request.d.ts +27 -36
- package/lib/request.js +14 -6
- package/lib/side-event/base.d.ts +64 -44
- package/lib/side-event/base.js +72 -19
- package/lib/side-event/index.d.ts +1 -1
- package/lib/side-event/index.js +1 -1
- package/lib/side-event/normalize.d.ts +7 -38
- package/lib/side-event/normalize.js +4 -109
- package/lib/side-event/scene-events.d.ts +40 -0
- package/lib/side-event/scene-events.js +147 -0
- package/lib/side-event/types.d.ts +4 -13
- package/lib/side-event/types.js +1 -55
- package/lib/system-event.d.ts +17 -12
- package/lib/system-event.js +8 -6
- package/lib/types.d.ts +23 -167
- package/lib/utils.d.ts +50 -33
- package/lib/utils.js +12 -185
- package/lib/venue/contracts.d.ts +21 -0
- package/lib/venue/contracts.js +3 -0
- package/lib/venue/floor-controller.d.ts +11 -0
- package/lib/venue/floor-controller.js +63 -0
- package/lib/venue/index.d.ts +13 -0
- package/lib/venue/index.js +11 -0
- package/lib/venue/membership-port.d.ts +23 -0
- package/lib/venue/membership-port.js +141 -0
- package/lib/venue/projector.d.ts +38 -0
- package/lib/venue/projector.js +45 -0
- package/lib/venue/registry.d.ts +16 -0
- package/lib/venue/registry.js +63 -0
- package/lib/venue/router.d.ts +19 -0
- package/lib/venue/router.js +101 -0
- package/lib/venue/send-limiter.d.ts +17 -0
- package/lib/venue/send-limiter.js +73 -0
- package/lib/venue/validator.d.ts +6 -0
- package/lib/venue/validator.js +160 -0
- package/package.json +24 -50
- package/lib/adapter.d.ts +0 -145
- package/lib/adapter.js +0 -377
- package/lib/built/adapter-process.d.ts +0 -58
- package/lib/built/adapter-process.js +0 -128
- package/lib/built/agent-preset.d.ts +0 -64
- package/lib/built/agent-preset.js +0 -79
- package/lib/built/ai-outbound/adapter-access.d.ts +0 -3
- package/lib/built/ai-outbound/adapter-access.js +0 -9
- package/lib/built/ai-outbound/extensions/qq-message.d.ts +0 -3
- package/lib/built/ai-outbound/extensions/qq-message.js +0 -23
- package/lib/built/authorization.d.ts +0 -10
- package/lib/built/authorization.js +0 -76
- package/lib/built/command.d.ts +0 -72
- package/lib/built/command.js +0 -105
- package/lib/built/component.d.ts +0 -67
- package/lib/built/component.js +0 -97
- package/lib/built/config.d.ts +0 -98
- package/lib/built/config.js +0 -372
- package/lib/built/connect-endpoint-instance.d.ts +0 -17
- package/lib/built/connect-endpoint-instance.js +0 -42
- package/lib/built/database.d.ts +0 -68
- package/lib/built/database.js +0 -106
- package/lib/built/dispatcher.d.ts +0 -141
- package/lib/built/dispatcher.js +0 -383
- package/lib/built/endpoint-commands.d.ts +0 -3
- package/lib/built/endpoint-commands.js +0 -99
- package/lib/built/endpoint-lifecycle-service.d.ts +0 -28
- package/lib/built/endpoint-lifecycle-service.js +0 -242
- package/lib/built/endpoint-lifecycle.d.ts +0 -27
- package/lib/built/endpoint-lifecycle.js +0 -36
- package/lib/built/endpoint-manager.d.ts +0 -34
- package/lib/built/html-renderer-loader.d.ts +0 -13
- package/lib/built/html-renderer-loader.js +0 -33
- package/lib/built/inbound-pipeline.d.ts +0 -37
- package/lib/built/inbound-pipeline.js +0 -63
- package/lib/built/inbound-runner.d.ts +0 -22
- package/lib/built/inbound-runner.js +0 -34
- package/lib/built/interactive-segment-contract.d.ts +0 -6
- package/lib/built/interactive-segment-contract.js +0 -9
- package/lib/built/interactive-segments/fallback-store.d.ts +0 -29
- package/lib/built/interactive-segments/fallback-store.js +0 -63
- package/lib/built/interactive-segments/handlers.d.ts +0 -16
- package/lib/built/interactive-segments/handlers.js +0 -74
- package/lib/built/introspection-format.d.ts +0 -10
- package/lib/built/introspection-format.js +0 -28
- package/lib/built/management-command-guard.d.ts +0 -15
- package/lib/built/management-command-guard.js +0 -25
- package/lib/built/message-filter.d.ts +0 -176
- package/lib/built/message-filter.js +0 -239
- package/lib/built/optional-peer-import.d.ts +0 -3
- package/lib/built/optional-peer-import.js +0 -33
- package/lib/built/outbound-media-contract.d.ts +0 -22
- package/lib/built/outbound-media-contract.js +0 -10
- package/lib/built/permission.d.ts +0 -51
- package/lib/built/permission.js +0 -99
- package/lib/built/permit-check.d.ts +0 -7
- package/lib/built/permit-check.js +0 -35
- package/lib/built/permit-parse.d.ts +0 -16
- package/lib/built/permit-parse.js +0 -32
- package/lib/built/platform-permit.d.ts +0 -20
- package/lib/built/platform-permit.js +0 -91
- package/lib/built/queue-im-field-contract.d.ts +0 -32
- package/lib/built/queue-im-field-contract.js +0 -67
- package/lib/built/rich-segments/adapter-policies.d.ts +0 -9
- package/lib/built/rich-segments/adapter-policies.js +0 -27
- package/lib/built/rich-segments/base.d.ts +0 -11
- package/lib/built/rich-segments/base.js +0 -24
- package/lib/built/rich-segments/builtins.d.ts +0 -4
- package/lib/built/rich-segments/builtins.js +0 -40
- package/lib/built/rich-segments/capabilities.d.ts +0 -6
- package/lib/built/rich-segments/capabilities.js +0 -38
- package/lib/built/rich-segments/html-segment.d.ts +0 -15
- package/lib/built/rich-segments/html-segment.js +0 -62
- package/lib/built/rich-segments/index.d.ts +0 -13
- package/lib/built/rich-segments/index.js +0 -13
- package/lib/built/rich-segments/markdown-segment.d.ts +0 -14
- package/lib/built/rich-segments/markdown-segment.js +0 -58
- package/lib/built/rich-segments/qrcode-segment.d.ts +0 -18
- package/lib/built/rich-segments/qrcode-segment.js +0 -73
- package/lib/built/rich-segments/registry.d.ts +0 -16
- package/lib/built/rich-segments/registry.js +0 -52
- package/lib/built/rich-segments/resolve.d.ts +0 -5
- package/lib/built/rich-segments/resolve.js +0 -87
- package/lib/built/rich-segments/tts-segment.d.ts +0 -13
- package/lib/built/rich-segments/tts-segment.js +0 -51
- package/lib/built/rich-segments/types.d.ts +0 -79
- package/lib/built/rich-segments/types.js +0 -17
- package/lib/built/runtime-io.d.ts +0 -9
- package/lib/built/runtime-io.js +0 -67
- package/lib/built/schedule.d.ts +0 -54
- package/lib/built/schedule.js +0 -112
- package/lib/built/schema-endpoint-manager.d.ts +0 -17
- package/lib/built/schema-endpoint-manager.js +0 -73
- package/lib/built/schema-feature.d.ts +0 -77
- package/lib/built/schema-feature.js +0 -130
- package/lib/built/skill.d.ts +0 -120
- package/lib/built/skill.js +0 -151
- package/lib/built/speech-loader.d.ts +0 -14
- package/lib/built/speech-loader.js +0 -32
- package/lib/built/tool.d.ts +0 -151
- package/lib/built/tool.js +0 -388
- package/lib/command.d.ts +0 -50
- package/lib/command.js +0 -106
- package/lib/component.d.ts +0 -26
- package/lib/component.js +0 -583
- package/lib/endpoint-capabilities.d.ts +0 -46
- package/lib/endpoint-capabilities.js +0 -55
- package/lib/endpoint.d.ts +0 -19
- package/lib/endpoint.js +0 -3
- package/lib/host-plugin-registry.d.ts +0 -8
- package/lib/host-plugin-registry.js +0 -12
- package/lib/plugin-context.d.ts +0 -27
- package/lib/plugin-context.js +0 -64
- package/lib/plugin-runtime/im/interactive.d.ts +0 -26
- package/lib/plugin-runtime/im/interactive.js +0 -46
- package/lib/plugin-runtime/im/login-assist-host.d.ts +0 -6
- package/lib/plugin-runtime/im/login-assist-host.js +0 -6
- package/lib/plugin-runtime/im/message-bus.d.ts +0 -38
- package/lib/plugin-runtime/im/message-bus.js +0 -40
- package/lib/plugin-runtime/im/service-tokens.d.ts +0 -18
- package/lib/plugin-runtime/im/service-tokens.js +0 -13
- package/lib/plugin.d.ts +0 -268
- package/lib/plugin.js +0 -583
- package/lib/schema-interaction.d.ts +0 -117
- package/lib/schema-interaction.js +0 -247
- package/lib/side-event/dispatch.d.ts +0 -15
- package/lib/side-event/dispatch.js +0 -140
- package/lib/tool-zod.d.ts +0 -43
- package/lib/tool-zod.js +0 -184
- /package/lib/{built/endpoint-manager.js → jsx/intrinsic-elements.js} +0 -0
package/lib/built/component.js
DELETED
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ComponentFeature
|
|
3
|
-
* 管理所有插件注册的组件,继承自 Feature 抽象类
|
|
4
|
-
*/
|
|
5
|
-
import { Feature } from '@zhin.js/kernel';
|
|
6
|
-
import { renderComponents } from "../component.js";
|
|
7
|
-
/**
|
|
8
|
-
* 组件服务 Feature
|
|
9
|
-
*/
|
|
10
|
-
export class ComponentFeature extends Feature {
|
|
11
|
-
name = 'component';
|
|
12
|
-
icon = 'Box';
|
|
13
|
-
desc = '组件';
|
|
14
|
-
/** 按名称索引 */
|
|
15
|
-
byName = new Map();
|
|
16
|
-
/** 内部状态:消息渲染监听器 & 宿主插件 */
|
|
17
|
-
#listener;
|
|
18
|
-
#rootPlugin;
|
|
19
|
-
/**
|
|
20
|
-
* 添加组件
|
|
21
|
-
*/
|
|
22
|
-
add(component, pluginName) {
|
|
23
|
-
this.byName.set(component.name, component);
|
|
24
|
-
return super.add(component, pluginName);
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* 移除组件
|
|
28
|
-
*/
|
|
29
|
-
remove(component) {
|
|
30
|
-
this.byName.delete(component.name);
|
|
31
|
-
return super.remove(component);
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* 获取所有组件名称
|
|
35
|
-
*/
|
|
36
|
-
getAllNames() {
|
|
37
|
-
return Array.from(this.byName.keys());
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* 按名称获取组件
|
|
41
|
-
*/
|
|
42
|
-
get(name) {
|
|
43
|
-
return this.byName.get(name);
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* 生命周期: 挂载时注册消息渲染监听器
|
|
47
|
-
*/
|
|
48
|
-
mounted(plugin) {
|
|
49
|
-
const p = plugin;
|
|
50
|
-
this.#rootPlugin = p;
|
|
51
|
-
this.#listener = (options) => {
|
|
52
|
-
return renderComponents(this.byName, options);
|
|
53
|
-
};
|
|
54
|
-
p.root.on('before.sendMessage', this.#listener);
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* 生命周期: 销毁时移除监听器
|
|
58
|
-
*/
|
|
59
|
-
dispose() {
|
|
60
|
-
if (this.#listener && this.#rootPlugin) {
|
|
61
|
-
this.#rootPlugin.root.off('before.sendMessage', this.#listener);
|
|
62
|
-
this.#listener = undefined;
|
|
63
|
-
}
|
|
64
|
-
this.byName.clear();
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* 序列化为 JSON
|
|
68
|
-
*/
|
|
69
|
-
toJSON(pluginName) {
|
|
70
|
-
const list = pluginName ? this.getByPlugin(pluginName) : this.items;
|
|
71
|
-
return {
|
|
72
|
-
name: this.name,
|
|
73
|
-
icon: this.icon,
|
|
74
|
-
desc: this.desc,
|
|
75
|
-
count: list.length,
|
|
76
|
-
items: list.map(c => ({
|
|
77
|
-
name: c.name,
|
|
78
|
-
type: 'component',
|
|
79
|
-
})),
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* 提供给 Plugin.prototype 的扩展方法
|
|
84
|
-
*/
|
|
85
|
-
get extensions() {
|
|
86
|
-
const feature = this;
|
|
87
|
-
return {
|
|
88
|
-
addComponent(component) {
|
|
89
|
-
const plugin = this;
|
|
90
|
-
const dispose = feature.add(component, plugin.name);
|
|
91
|
-
plugin.recordFeatureContribution(feature.name, component.name);
|
|
92
|
-
plugin.onDispose(dispose);
|
|
93
|
-
return dispose;
|
|
94
|
-
},
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
}
|
package/lib/built/config.d.ts
DELETED
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
import { Schema } from "@zhin.js/schema";
|
|
2
|
-
import { Feature, FeatureJSON } from "@zhin.js/kernel";
|
|
3
|
-
/** Deno / Node 统一的工作目录(`Deno.chdir` 后应与 `process.cwd()` 一致) */
|
|
4
|
-
export declare function runtimeCwd(): string;
|
|
5
|
-
export declare class ConfigLoader<T extends object> {
|
|
6
|
-
#private;
|
|
7
|
-
filename: string;
|
|
8
|
-
initial: T;
|
|
9
|
-
schema?: Schema<T> | undefined;
|
|
10
|
-
get data(): T;
|
|
11
|
-
get raw(): T;
|
|
12
|
-
get extension(): string;
|
|
13
|
-
/** `load()` / `patchKey()` 使用的绝对路径 */
|
|
14
|
-
get resolvedPath(): string;
|
|
15
|
-
constructor(filename: string, initial: T, schema?: Schema<T> | undefined);
|
|
16
|
-
resolvePath(baseDir?: string): string;
|
|
17
|
-
load(baseDir?: string): void;
|
|
18
|
-
save(fullPath?: string): void;
|
|
19
|
-
/**
|
|
20
|
-
* 精准修改单个顶层 key 并写回文件
|
|
21
|
-
* 保留注释、格式和未修改字段(如 ${VAR} 环境变量引用)
|
|
22
|
-
*/
|
|
23
|
-
patchKey(key: string, value: any): void;
|
|
24
|
-
}
|
|
25
|
-
/** 解析 endpoint 配置中的 `${ENV}` 引用(运行时 add 后热连接用) */
|
|
26
|
-
export declare function resolveEndpointConfigEnv(config: Record<string, unknown>): Record<string, unknown>;
|
|
27
|
-
export declare function mergeConfigDefaults<T>(defaults: T, overrides: unknown): T;
|
|
28
|
-
export declare namespace ConfigLoader {
|
|
29
|
-
const supportedExtensions: string[];
|
|
30
|
-
/**
|
|
31
|
-
* 自动发现配置文件(按优先级:yml > yaml > json > toml)
|
|
32
|
-
*/
|
|
33
|
-
function discover(basename: string, cwd?: string): string | null;
|
|
34
|
-
function load<T extends object>(filename: string, initial?: T, schema?: Schema<T>, baseDir?: string): ConfigLoader<T>;
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* 配置项声明记录
|
|
38
|
-
*/
|
|
39
|
-
export interface ConfigRecord {
|
|
40
|
-
key: string;
|
|
41
|
-
defaultValue: any;
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* ConfigFeature 扩展方法类型
|
|
45
|
-
*/
|
|
46
|
-
export interface ConfigContextExtensions {
|
|
47
|
-
/** 声明插件配置项(key + 默认值),如果配置文件中不存在则写入默认值 */
|
|
48
|
-
addConfig(key: string, defaultValue: any): () => void;
|
|
49
|
-
}
|
|
50
|
-
declare module "../plugin.js" {
|
|
51
|
-
namespace Plugin {
|
|
52
|
-
interface Extensions extends ConfigContextExtensions {
|
|
53
|
-
}
|
|
54
|
-
interface Contexts {
|
|
55
|
-
config: ConfigFeature;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
export declare class ConfigFeature extends Feature<ConfigRecord> {
|
|
60
|
-
#private;
|
|
61
|
-
readonly name: "config";
|
|
62
|
-
readonly icon = "Settings";
|
|
63
|
-
readonly desc = "\u914D\u7F6E";
|
|
64
|
-
/** 内部配置文件管理 */
|
|
65
|
-
readonly configs: Map<string, ConfigLoader<any>>;
|
|
66
|
-
/** 获取主配置文件名 */
|
|
67
|
-
get primaryFile(): string;
|
|
68
|
-
/**
|
|
69
|
-
* 加载配置文件
|
|
70
|
-
*/
|
|
71
|
-
load<T extends object>(filename: string, initial?: Partial<T>, schema?: Schema<T>, baseDir?: string): ConfigLoader<T>;
|
|
72
|
-
/**
|
|
73
|
-
* 获取配置数据(代理模式,自动保存)
|
|
74
|
-
*/
|
|
75
|
-
get<T extends object>(filename: string, initial?: Partial<T>, schema?: Schema<T>): T;
|
|
76
|
-
/**
|
|
77
|
-
* 获取主配置文件数据(第一个加载的配置文件)
|
|
78
|
-
*/
|
|
79
|
-
getPrimary<T extends object>(): T;
|
|
80
|
-
/**
|
|
81
|
-
* 获取原始配置数据
|
|
82
|
-
*/
|
|
83
|
-
getRaw<T extends object>(filename: string, initial?: Partial<T>, schema?: Schema<T>): T;
|
|
84
|
-
/**
|
|
85
|
-
* 更新配置文件内容
|
|
86
|
-
*/
|
|
87
|
-
set<T extends object>(filename: string, data: T): void;
|
|
88
|
-
/**
|
|
89
|
-
* 序列化为 JSON
|
|
90
|
-
*/
|
|
91
|
-
toJSON(pluginName?: string): FeatureJSON;
|
|
92
|
-
/**
|
|
93
|
-
* 提供给 Plugin.prototype 的扩展方法
|
|
94
|
-
*/
|
|
95
|
-
get extensions(): {
|
|
96
|
-
addConfig(key: string, defaultValue: any): () => void;
|
|
97
|
-
};
|
|
98
|
-
}
|
package/lib/built/config.js
DELETED
|
@@ -1,372 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ConfigFeature
|
|
3
|
-
* 配置管理服务,继承自 Feature 抽象类
|
|
4
|
-
* 保留原有 ConfigLoader / ConfigService 逻辑,增加 addConfig 扩展
|
|
5
|
-
*/
|
|
6
|
-
import path from "node:path";
|
|
7
|
-
import fs from "node:fs";
|
|
8
|
-
import { stringify as stringifyYaml, parse as parseYaml, parseDocument } from "yaml";
|
|
9
|
-
import { parse as parseToml, stringify as stringifyToml } from "smol-toml";
|
|
10
|
-
import { Feature } from "@zhin.js/kernel";
|
|
11
|
-
/** Deno / Node 统一的工作目录(`Deno.chdir` 后应与 `process.cwd()` 一致) */
|
|
12
|
-
export function runtimeCwd() {
|
|
13
|
-
const g = globalThis;
|
|
14
|
-
return g.Deno?.cwd?.() ?? process.cwd();
|
|
15
|
-
}
|
|
16
|
-
function envLookup(key) {
|
|
17
|
-
const g = globalThis;
|
|
18
|
-
return g.Deno?.env.get(key) ?? g.process?.env?.[key];
|
|
19
|
-
}
|
|
20
|
-
// ============================================================================
|
|
21
|
-
// ConfigLoader(保持不变)
|
|
22
|
-
// ============================================================================
|
|
23
|
-
export class ConfigLoader {
|
|
24
|
-
filename;
|
|
25
|
-
initial;
|
|
26
|
-
schema;
|
|
27
|
-
#data;
|
|
28
|
-
/** `load()` 时解析的绝对路径,避免 proxy `save` 写到 cwd 下错误目录 */
|
|
29
|
-
#resolvedPath = '';
|
|
30
|
-
get data() {
|
|
31
|
-
return this.#proxy(this.#data, this);
|
|
32
|
-
}
|
|
33
|
-
get raw() {
|
|
34
|
-
return this.#data;
|
|
35
|
-
}
|
|
36
|
-
get extension() {
|
|
37
|
-
return path.extname(this.filename).toLowerCase();
|
|
38
|
-
}
|
|
39
|
-
/** `load()` / `patchKey()` 使用的绝对路径 */
|
|
40
|
-
get resolvedPath() {
|
|
41
|
-
return this.#resolvedPath || this.resolvePath();
|
|
42
|
-
}
|
|
43
|
-
constructor(filename, initial, schema) {
|
|
44
|
-
this.filename = filename;
|
|
45
|
-
this.initial = initial;
|
|
46
|
-
this.schema = schema;
|
|
47
|
-
this.#data = this.initial;
|
|
48
|
-
}
|
|
49
|
-
#proxy(data, loader) {
|
|
50
|
-
return new Proxy(data, {
|
|
51
|
-
get(target, prop, receiver) {
|
|
52
|
-
if (typeof prop === 'symbol' || prop === 'constructor' || prop === 'prototype') {
|
|
53
|
-
return Reflect.get(target, prop, receiver);
|
|
54
|
-
}
|
|
55
|
-
const result = Reflect.get(target, prop, receiver);
|
|
56
|
-
if (typeof result === 'function') {
|
|
57
|
-
return result;
|
|
58
|
-
}
|
|
59
|
-
if (result instanceof Object && result !== null && typeof result !== 'function') {
|
|
60
|
-
return loader.#proxy(result, loader);
|
|
61
|
-
}
|
|
62
|
-
if (typeof result === 'string') {
|
|
63
|
-
if (result.startsWith('\\${') && result.endsWith('}'))
|
|
64
|
-
return result.slice(1);
|
|
65
|
-
if (/^\$\{(.*)\}$/.test(result)) {
|
|
66
|
-
const content = result.slice(2, -1);
|
|
67
|
-
// 支持 bash 风格的默认值语法:${VAR:-default} 和 ${VAR:=default}
|
|
68
|
-
let key;
|
|
69
|
-
let defaultValue;
|
|
70
|
-
const bashDefaultMatch = content.match(/^([^:}]+):[-=](.*)$/);
|
|
71
|
-
if (bashDefaultMatch) {
|
|
72
|
-
// ${VAR:-default} 或 ${VAR:=default}
|
|
73
|
-
key = bashDefaultMatch[1];
|
|
74
|
-
defaultValue = bashDefaultMatch[2];
|
|
75
|
-
}
|
|
76
|
-
else {
|
|
77
|
-
key = content;
|
|
78
|
-
defaultValue = undefined;
|
|
79
|
-
}
|
|
80
|
-
return envLookup(key) ?? defaultValue ?? loader.initial[key] ?? result;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
return result;
|
|
84
|
-
},
|
|
85
|
-
set(target, prop, value, receiver) {
|
|
86
|
-
const result = Reflect.set(target, prop, value, receiver);
|
|
87
|
-
loader.save();
|
|
88
|
-
return result;
|
|
89
|
-
},
|
|
90
|
-
deleteProperty(target, prop) {
|
|
91
|
-
const result = Reflect.deleteProperty(target, prop);
|
|
92
|
-
loader.save();
|
|
93
|
-
return result;
|
|
94
|
-
}
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
resolvePath(baseDir = runtimeCwd()) {
|
|
98
|
-
return path.isAbsolute(this.filename)
|
|
99
|
-
? this.filename
|
|
100
|
-
: path.resolve(baseDir, this.filename);
|
|
101
|
-
}
|
|
102
|
-
load(baseDir) {
|
|
103
|
-
const fullPath = baseDir !== undefined
|
|
104
|
-
? this.resolvePath(baseDir)
|
|
105
|
-
: (this.#resolvedPath || this.resolvePath());
|
|
106
|
-
this.#resolvedPath = fullPath;
|
|
107
|
-
if (!fs.existsSync(fullPath)) {
|
|
108
|
-
try {
|
|
109
|
-
this.save(fullPath);
|
|
110
|
-
}
|
|
111
|
-
catch {
|
|
112
|
-
// Workers 等无 fs.writeFileSync 的环境:使用内存 defaults
|
|
113
|
-
const hasDefaults = this.initial != null && typeof this.initial === "object" && Object.keys(this.initial).length > 0;
|
|
114
|
-
if (hasDefaults) {
|
|
115
|
-
const merged = mergeConfigDefaults(this.initial, {});
|
|
116
|
-
this.#data = this.schema ? this.schema(merged) : merged;
|
|
117
|
-
return;
|
|
118
|
-
}
|
|
119
|
-
throw new Error(`Config file missing and cannot create: ${fullPath}`);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
const content = fs.readFileSync(fullPath, "utf-8");
|
|
123
|
-
let rawConfig;
|
|
124
|
-
switch (this.extension) {
|
|
125
|
-
case ".json":
|
|
126
|
-
rawConfig = JSON.parse(content);
|
|
127
|
-
break;
|
|
128
|
-
case ".yaml":
|
|
129
|
-
case ".yml":
|
|
130
|
-
rawConfig = parseYaml(content);
|
|
131
|
-
break;
|
|
132
|
-
case ".toml":
|
|
133
|
-
rawConfig = parseToml(content);
|
|
134
|
-
break;
|
|
135
|
-
}
|
|
136
|
-
const merged = mergeConfigDefaults(this.initial, rawConfig || {});
|
|
137
|
-
if (this.schema) {
|
|
138
|
-
this.#data = this.schema(merged);
|
|
139
|
-
}
|
|
140
|
-
else {
|
|
141
|
-
this.#data = merged;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
save(fullPath) {
|
|
145
|
-
if (fullPath) {
|
|
146
|
-
this.#resolvedPath = fullPath;
|
|
147
|
-
}
|
|
148
|
-
const target = this.#resolvedPath || this.resolvePath();
|
|
149
|
-
if (!target) {
|
|
150
|
-
throw new Error('ConfigLoader: call load() before save()');
|
|
151
|
-
}
|
|
152
|
-
switch (this.extension) {
|
|
153
|
-
case ".json":
|
|
154
|
-
fs.writeFileSync(target, JSON.stringify(this.#data, null, 2));
|
|
155
|
-
break;
|
|
156
|
-
case ".yaml":
|
|
157
|
-
case ".yml":
|
|
158
|
-
fs.writeFileSync(target, stringifyYaml(this.#data));
|
|
159
|
-
break;
|
|
160
|
-
case ".toml":
|
|
161
|
-
fs.writeFileSync(target, stringifyToml(this.#data));
|
|
162
|
-
break;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
/**
|
|
166
|
-
* 精准修改单个顶层 key 并写回文件
|
|
167
|
-
* 保留注释、格式和未修改字段(如 ${VAR} 环境变量引用)
|
|
168
|
-
*/
|
|
169
|
-
patchKey(key, value) {
|
|
170
|
-
const fullPath = this.#resolvedPath || this.resolvePath();
|
|
171
|
-
switch (this.extension) {
|
|
172
|
-
case ".yaml":
|
|
173
|
-
case ".yml": {
|
|
174
|
-
const content = fs.readFileSync(fullPath, 'utf-8');
|
|
175
|
-
const doc = parseDocument(content);
|
|
176
|
-
doc.set(key, value);
|
|
177
|
-
fs.writeFileSync(fullPath, doc.toString());
|
|
178
|
-
break;
|
|
179
|
-
}
|
|
180
|
-
case ".json": {
|
|
181
|
-
const content = fs.readFileSync(fullPath, 'utf-8');
|
|
182
|
-
const obj = JSON.parse(content);
|
|
183
|
-
obj[key] = value;
|
|
184
|
-
fs.writeFileSync(fullPath, JSON.stringify(obj, null, 2));
|
|
185
|
-
break;
|
|
186
|
-
}
|
|
187
|
-
case ".toml": {
|
|
188
|
-
const content = fs.readFileSync(fullPath, 'utf-8');
|
|
189
|
-
const obj = parseToml(content);
|
|
190
|
-
obj[key] = value;
|
|
191
|
-
fs.writeFileSync(fullPath, stringifyToml(obj));
|
|
192
|
-
break;
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
// 同步更新内存
|
|
196
|
-
this.#data[key] = value;
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
function isPlainObject(value) {
|
|
200
|
-
return !!value
|
|
201
|
-
&& typeof value === 'object'
|
|
202
|
-
&& !Array.isArray(value)
|
|
203
|
-
&& Object.getPrototypeOf(value) === Object.prototype;
|
|
204
|
-
}
|
|
205
|
-
/** 解析 endpoint 配置中的 `${ENV}` 引用(运行时 add 后热连接用) */
|
|
206
|
-
export function resolveEndpointConfigEnv(config) {
|
|
207
|
-
const out = { ...config };
|
|
208
|
-
for (const [key, value] of Object.entries(out)) {
|
|
209
|
-
if (typeof value !== 'string')
|
|
210
|
-
continue;
|
|
211
|
-
const match = value.match(/^\$\{([^}]+)\}$/);
|
|
212
|
-
if (!match)
|
|
213
|
-
continue;
|
|
214
|
-
const envKey = match[1];
|
|
215
|
-
const resolved = envLookup(envKey);
|
|
216
|
-
if (resolved != null && resolved !== '') {
|
|
217
|
-
out[key] = resolved;
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
return out;
|
|
221
|
-
}
|
|
222
|
-
export function mergeConfigDefaults(defaults, overrides) {
|
|
223
|
-
if (Array.isArray(defaults)) {
|
|
224
|
-
return (Array.isArray(overrides) ? overrides : defaults);
|
|
225
|
-
}
|
|
226
|
-
if (!isPlainObject(defaults)) {
|
|
227
|
-
return (overrides === undefined ? defaults : overrides);
|
|
228
|
-
}
|
|
229
|
-
if (!isPlainObject(overrides)) {
|
|
230
|
-
return defaults;
|
|
231
|
-
}
|
|
232
|
-
const result = { ...defaults };
|
|
233
|
-
for (const [key, value] of Object.entries(overrides)) {
|
|
234
|
-
result[key] = key in result
|
|
235
|
-
? mergeConfigDefaults(result[key], value)
|
|
236
|
-
: value;
|
|
237
|
-
}
|
|
238
|
-
return result;
|
|
239
|
-
}
|
|
240
|
-
(function (ConfigLoader) {
|
|
241
|
-
ConfigLoader.supportedExtensions = [".json", ".yaml", ".yml", ".toml"];
|
|
242
|
-
/**
|
|
243
|
-
* 自动发现配置文件(按优先级:yml > yaml > json > toml)
|
|
244
|
-
*/
|
|
245
|
-
function discover(basename, cwd = runtimeCwd()) {
|
|
246
|
-
for (const ext of ['.yml', '.yaml', '.json', '.toml']) {
|
|
247
|
-
const filename = `${basename}${ext}`;
|
|
248
|
-
if (fs.existsSync(path.resolve(cwd, filename))) {
|
|
249
|
-
return filename;
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
return null;
|
|
253
|
-
}
|
|
254
|
-
ConfigLoader.discover = discover;
|
|
255
|
-
function load(filename, initial, schema, baseDir) {
|
|
256
|
-
const result = new ConfigLoader(filename, initial ?? {}, schema);
|
|
257
|
-
result.load(baseDir);
|
|
258
|
-
return result;
|
|
259
|
-
}
|
|
260
|
-
ConfigLoader.load = load;
|
|
261
|
-
})(ConfigLoader || (ConfigLoader = {}));
|
|
262
|
-
export class ConfigFeature extends Feature {
|
|
263
|
-
name = 'config';
|
|
264
|
-
icon = 'Settings';
|
|
265
|
-
desc = '配置';
|
|
266
|
-
/** 内部配置文件管理 */
|
|
267
|
-
configs = new Map();
|
|
268
|
-
/** 主配置文件名(第一个加载的配置文件) */
|
|
269
|
-
#primaryConfigFile = '';
|
|
270
|
-
/** 获取主配置文件名 */
|
|
271
|
-
get primaryFile() {
|
|
272
|
-
return this.#primaryConfigFile;
|
|
273
|
-
}
|
|
274
|
-
/**
|
|
275
|
-
* 加载配置文件
|
|
276
|
-
*/
|
|
277
|
-
load(filename, initial, schema, baseDir) {
|
|
278
|
-
const ext = path.extname(filename).toLowerCase();
|
|
279
|
-
if (!ConfigLoader.supportedExtensions.includes(ext)) {
|
|
280
|
-
throw new Error(`不支持的配置文件格式: ${ext}`);
|
|
281
|
-
}
|
|
282
|
-
const config = ConfigLoader.load(filename, initial, schema, baseDir);
|
|
283
|
-
this.configs.set(filename, config);
|
|
284
|
-
if (!this.#primaryConfigFile) {
|
|
285
|
-
this.#primaryConfigFile = filename;
|
|
286
|
-
}
|
|
287
|
-
return config;
|
|
288
|
-
}
|
|
289
|
-
/**
|
|
290
|
-
* 获取配置数据(代理模式,自动保存)
|
|
291
|
-
*/
|
|
292
|
-
get(filename, initial, schema) {
|
|
293
|
-
if (!this.configs.has(filename))
|
|
294
|
-
this.load(filename, initial, schema);
|
|
295
|
-
const config = this.configs.get(filename);
|
|
296
|
-
if (!config)
|
|
297
|
-
throw new Error(`配置文件 ${filename} 未加载`);
|
|
298
|
-
return config.data;
|
|
299
|
-
}
|
|
300
|
-
/**
|
|
301
|
-
* 获取主配置文件数据(第一个加载的配置文件)
|
|
302
|
-
*/
|
|
303
|
-
getPrimary() {
|
|
304
|
-
if (!this.#primaryConfigFile)
|
|
305
|
-
throw new Error('没有加载任何配置文件');
|
|
306
|
-
return this.get(this.#primaryConfigFile);
|
|
307
|
-
}
|
|
308
|
-
/**
|
|
309
|
-
* 获取原始配置数据
|
|
310
|
-
*/
|
|
311
|
-
getRaw(filename, initial, schema) {
|
|
312
|
-
if (!this.configs.has(filename))
|
|
313
|
-
this.load(filename, initial, schema);
|
|
314
|
-
const config = this.configs.get(filename);
|
|
315
|
-
if (!config)
|
|
316
|
-
throw new Error(`配置文件 ${filename} 未加载`);
|
|
317
|
-
return config.raw;
|
|
318
|
-
}
|
|
319
|
-
/**
|
|
320
|
-
* 更新配置文件内容
|
|
321
|
-
*/
|
|
322
|
-
set(filename, data) {
|
|
323
|
-
const config = this.configs.get(filename);
|
|
324
|
-
if (!config)
|
|
325
|
-
throw new Error(`配置文件 ${filename} 未加载`);
|
|
326
|
-
Object.assign(config.raw, data);
|
|
327
|
-
config.save(path.resolve(runtimeCwd(), filename));
|
|
328
|
-
}
|
|
329
|
-
/**
|
|
330
|
-
* 序列化为 JSON
|
|
331
|
-
*/
|
|
332
|
-
toJSON(pluginName) {
|
|
333
|
-
const list = pluginName ? this.getByPlugin(pluginName) : this.items;
|
|
334
|
-
return {
|
|
335
|
-
name: this.name,
|
|
336
|
-
icon: this.icon,
|
|
337
|
-
desc: this.desc,
|
|
338
|
-
count: list.length,
|
|
339
|
-
items: list.map(r => ({
|
|
340
|
-
name: r.key,
|
|
341
|
-
// 不暴露 defaultValue 以防止泄露密钥/令牌
|
|
342
|
-
})),
|
|
343
|
-
};
|
|
344
|
-
}
|
|
345
|
-
/**
|
|
346
|
-
* 提供给 Plugin.prototype 的扩展方法
|
|
347
|
-
*/
|
|
348
|
-
get extensions() {
|
|
349
|
-
const feature = this;
|
|
350
|
-
return {
|
|
351
|
-
addConfig(key, defaultValue) {
|
|
352
|
-
const plugin = this;
|
|
353
|
-
// 尝试写入主配置文件(如果 key 不存在)
|
|
354
|
-
if (feature.#primaryConfigFile) {
|
|
355
|
-
const config = feature.configs.get(feature.#primaryConfigFile);
|
|
356
|
-
if (config) {
|
|
357
|
-
const raw = config.raw;
|
|
358
|
-
if (!(key in raw)) {
|
|
359
|
-
raw[key] = defaultValue;
|
|
360
|
-
config.save(path.resolve(runtimeCwd(), feature.#primaryConfigFile));
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
}
|
|
364
|
-
const record = { key, defaultValue };
|
|
365
|
-
const dispose = feature.add(record, plugin.name);
|
|
366
|
-
plugin.recordFeatureContribution(feature.name, key);
|
|
367
|
-
plugin.onDispose(dispose);
|
|
368
|
-
return dispose;
|
|
369
|
-
},
|
|
370
|
-
};
|
|
371
|
-
}
|
|
372
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import type { Adapter } from '../adapter.js';
|
|
2
|
-
import type { Endpoint } from '../endpoint.js';
|
|
3
|
-
import type { Plugin } from '../plugin.js';
|
|
4
|
-
import { type EndpointCapabilitiesConfig } from '../endpoint-capabilities.js';
|
|
5
|
-
export interface ConnectEndpointInstanceOptions {
|
|
6
|
-
plugin: Plugin;
|
|
7
|
-
adapter: Adapter;
|
|
8
|
-
config: EndpointCapabilitiesConfig & Record<string, unknown>;
|
|
9
|
-
}
|
|
10
|
-
/**
|
|
11
|
-
* 按 Endpoint capabilities 连接或注册单个实例(connectEndpoints 与 Adapter.start 共用)。
|
|
12
|
-
*/
|
|
13
|
-
export declare function connectEndpointInstance(options: ConnectEndpointInstanceOptions): Promise<Endpoint>;
|
|
14
|
-
/**
|
|
15
|
-
* 按 Endpoint capabilities 断开单个实例。
|
|
16
|
-
*/
|
|
17
|
-
export declare function disconnectEndpointInstance(plugin: Plugin, adapter: Adapter, endpoint: Endpoint): Promise<void>;
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { assertInbound, getAdapterCapabilities, hasInbound, registerEndpointCapabilities, resolveEndpointCapabilities, } from '../endpoint-capabilities.js';
|
|
2
|
-
import { emitEndpointLifecycle } from './endpoint-lifecycle.js';
|
|
3
|
-
import { resolveEndpointConfigEnv } from './config.js';
|
|
4
|
-
/**
|
|
5
|
-
* 按 Endpoint capabilities 连接或注册单个实例(connectEndpoints 与 Adapter.start 共用)。
|
|
6
|
-
*/
|
|
7
|
-
export async function connectEndpointInstance(options) {
|
|
8
|
-
const { plugin, adapter, config } = options;
|
|
9
|
-
const adapterCaps = getAdapterCapabilities(adapter);
|
|
10
|
-
const caps = resolveEndpointCapabilities(adapterCaps, config.capabilities);
|
|
11
|
-
const resolvedConfig = resolveEndpointConfigEnv(config);
|
|
12
|
-
const endpoint = adapter.createEndpoint(resolvedConfig);
|
|
13
|
-
registerEndpointCapabilities(endpoint, caps);
|
|
14
|
-
if (caps.includes('inbound')) {
|
|
15
|
-
assertInbound(endpoint);
|
|
16
|
-
try {
|
|
17
|
-
await endpoint.$connect();
|
|
18
|
-
await emitEndpointLifecycle(plugin, adapter, endpoint, 'connect');
|
|
19
|
-
}
|
|
20
|
-
catch (error) {
|
|
21
|
-
await emitEndpointLifecycle(plugin, adapter, endpoint, 'error', {
|
|
22
|
-
error: error instanceof Error ? error.message : String(error),
|
|
23
|
-
phase: 'connect',
|
|
24
|
-
});
|
|
25
|
-
throw error;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
else {
|
|
29
|
-
endpoint.$connected = true;
|
|
30
|
-
}
|
|
31
|
-
return endpoint;
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* 按 Endpoint capabilities 断开单个实例。
|
|
35
|
-
*/
|
|
36
|
-
export async function disconnectEndpointInstance(plugin, adapter, endpoint) {
|
|
37
|
-
if (!hasInbound(endpoint))
|
|
38
|
-
return;
|
|
39
|
-
assertInbound(endpoint);
|
|
40
|
-
await endpoint.$disconnect();
|
|
41
|
-
await emitEndpointLifecycle(plugin, adapter, endpoint, 'disconnect');
|
|
42
|
-
}
|
package/lib/built/database.d.ts
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* DatabaseFeature
|
|
3
|
-
* 数据库服务,管理数据模型定义,继承自 Feature 抽象类
|
|
4
|
-
*/
|
|
5
|
-
import { Definition, Database } from "@zhin.js/database";
|
|
6
|
-
import { DatabaseConfig, Models } from "../types.js";
|
|
7
|
-
import { Feature, FeatureJSON, type PluginLike } from '@zhin.js/kernel';
|
|
8
|
-
/**
|
|
9
|
-
* 模型定义记录
|
|
10
|
-
*/
|
|
11
|
-
export interface ModelRecord {
|
|
12
|
-
name: string;
|
|
13
|
-
definition: Definition<any>;
|
|
14
|
-
}
|
|
15
|
-
declare module "../plugin.js" {
|
|
16
|
-
namespace Plugin {
|
|
17
|
-
interface Extensions {
|
|
18
|
-
defineModel<K extends keyof Models>(name: K, definition: Definition<Models[K]>): void;
|
|
19
|
-
}
|
|
20
|
-
interface Contexts {
|
|
21
|
-
database: DatabaseFeature;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
type DatabaseAfterStartHook = (db: Database<any, Models, any>) => void | Promise<void>;
|
|
26
|
-
/** 在 `db.start()` 完成之后、`context.mounted` 发出之前执行(用于 schema 迁移) */
|
|
27
|
-
export declare function onDatabaseAfterStart(hook: DatabaseAfterStartHook): void;
|
|
28
|
-
export declare class DatabaseFeature extends Feature<ModelRecord> {
|
|
29
|
-
readonly name: "database";
|
|
30
|
-
readonly icon = "Database";
|
|
31
|
-
readonly desc = "\u6570\u636E\u6A21\u578B";
|
|
32
|
-
/** 内部数据库实例 */
|
|
33
|
-
readonly db: Database<any, Models, any>;
|
|
34
|
-
/** 按模型名索引 */
|
|
35
|
-
readonly byName: Map<string, ModelRecord>;
|
|
36
|
-
constructor(config: DatabaseConfig);
|
|
37
|
-
get models(): Database.Models<Models, any, any>;
|
|
38
|
-
define<K extends keyof Models>(name: K, definition: Definition<Models[K]>): void;
|
|
39
|
-
start(): Promise<void>;
|
|
40
|
-
stop(): Promise<void>;
|
|
41
|
-
/**
|
|
42
|
-
* 添加模型定义
|
|
43
|
-
*/
|
|
44
|
-
add(record: ModelRecord, pluginName: string): () => void;
|
|
45
|
-
/**
|
|
46
|
-
* 移除模型定义
|
|
47
|
-
*/
|
|
48
|
-
remove(record: ModelRecord): boolean;
|
|
49
|
-
/**
|
|
50
|
-
* 生命周期: 启动数据库
|
|
51
|
-
*/
|
|
52
|
-
mounted(plugin: PluginLike): Promise<void>;
|
|
53
|
-
/**
|
|
54
|
-
* 生命周期: 停止数据库
|
|
55
|
-
*/
|
|
56
|
-
dispose(): Promise<void>;
|
|
57
|
-
/**
|
|
58
|
-
* 序列化为 JSON
|
|
59
|
-
*/
|
|
60
|
-
toJSON(pluginName?: string): FeatureJSON;
|
|
61
|
-
/**
|
|
62
|
-
* 提供给 Plugin.prototype 的扩展方法
|
|
63
|
-
*/
|
|
64
|
-
get extensions(): {
|
|
65
|
-
defineModel<K extends keyof Models>(name: K, definition: Definition<Models[K]>): void;
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
export {};
|