@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/plugin.js
DELETED
|
@@ -1,583 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Plugin 类 — IM 特化插件,继承 PluginBase
|
|
3
|
-
*
|
|
4
|
-
* 在 PluginBase 的 DI/生命周期/事件基础上增加:
|
|
5
|
-
* - Context 类型安全(Plugin.Contexts 映射)
|
|
6
|
-
* - 生命周期事件类型安全(Plugin.Lifecycle)
|
|
7
|
-
* - 中间件系统、适配器管理、useContext 等
|
|
8
|
-
*/
|
|
9
|
-
import * as fs from "node:fs";
|
|
10
|
-
import * as path from "node:path";
|
|
11
|
-
import { pathToFileURL } from "node:url";
|
|
12
|
-
import { formatCompact } from "@zhin.js/logger";
|
|
13
|
-
import { compose, remove, resolveEntry } from "./utils.js";
|
|
14
|
-
import { ensureInteractiveMiddleware, registerInteractiveHandler as registerInteractiveHandlerCore, } from "./built/interactive-segments/handlers.js";
|
|
15
|
-
import { Adapter } from "./adapter.js";
|
|
16
|
-
import { Feature, PluginBase, resolvePluginResolveDir as _resolvePluginResolveDir, pluginCreateRequire as _pluginCreateRequire, getFileHash, watchFile, registerExtension, unregisterExtensions, installExtensionProxy } from '@zhin.js/kernel';
|
|
17
|
-
import { storage } from "./plugin-context.js";
|
|
18
|
-
export { getPlugin, markPluginRuntimeActive, isPluginRuntimeActive, resetPluginRuntimeFlag } from "./plugin-context.js";
|
|
19
|
-
export { storage, getCurrentFile } from "./plugin-context.js";
|
|
20
|
-
export { setHostRootPlugin, getHostRootPlugin } from "./host-plugin-registry.js";
|
|
21
|
-
const contextsKey = Symbol("contexts");
|
|
22
|
-
function resolvePluginResolveDir(parent) {
|
|
23
|
-
return _resolvePluginResolveDir(parent);
|
|
24
|
-
}
|
|
25
|
-
const loadedModules = new Map();
|
|
26
|
-
function pluginCreateRequire() {
|
|
27
|
-
return _pluginCreateRequire();
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* @deprecated **已删除**。请使用 `definePlugin` + 约定目录(`zhin runtime start`)。
|
|
31
|
-
* @throws 总是抛出——仅保留签名供编译期过渡。
|
|
32
|
-
*/
|
|
33
|
-
export function usePlugin() {
|
|
34
|
-
throw new Error('usePlugin() has been removed. Use `definePlugin` + convention directories instead. '
|
|
35
|
-
+ 'See docs/contributing/public-api-surface.md');
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Plugin 类 - IM 特化插件,继承 PluginBase
|
|
39
|
-
*
|
|
40
|
-
* 类型窄化:
|
|
41
|
-
* - children: Plugin[] (而非 PluginBase[])
|
|
42
|
-
* - parent: Plugin | undefined
|
|
43
|
-
* - root: Plugin
|
|
44
|
-
* - $contexts: Map<string, Context<any>>
|
|
45
|
-
* - dispatch/broadcast: Plugin.Lifecycle 类型安全
|
|
46
|
-
*/
|
|
47
|
-
export class Plugin extends PluginBase {
|
|
48
|
-
static [contextsKey] = [];
|
|
49
|
-
#cachedName;
|
|
50
|
-
#manifest;
|
|
51
|
-
adapters = [];
|
|
52
|
-
// children/parent 继承 PluginBase 的类型(PluginBase[] / PluginBase | undefined),
|
|
53
|
-
// 具体的 Plugin 类型通过 getter/builder 方法按需窄化
|
|
54
|
-
get root() {
|
|
55
|
-
return super.root;
|
|
56
|
-
}
|
|
57
|
-
// 默认中间件:将入站消息交给 compose 链末端的 MessageDispatcher(命令/AI 路由在 dispatcher 内完成)
|
|
58
|
-
#messageMiddleware = async (_message, next) => {
|
|
59
|
-
await next();
|
|
60
|
-
};
|
|
61
|
-
#middlewares = [this.#messageMiddleware];
|
|
62
|
-
get middleware() {
|
|
63
|
-
return compose(this.#middlewares);
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* 构造函数
|
|
67
|
-
*/
|
|
68
|
-
constructor(filePath = "", parent) {
|
|
69
|
-
super(filePath, parent);
|
|
70
|
-
// 自动添加到父节点 — PluginBase constructor 已处理,
|
|
71
|
-
// 但 PluginBase 使用 PluginBase[],这里需要确保正确
|
|
72
|
-
// (super 已调用 parent.children.push(this),类型已通过 declare 窄化)
|
|
73
|
-
// 绑定方法以支持解构使用
|
|
74
|
-
this.$bindMethods();
|
|
75
|
-
}
|
|
76
|
-
// 标记是否已绑定方法
|
|
77
|
-
#methodsBound = false;
|
|
78
|
-
/**
|
|
79
|
-
* 添加中间件
|
|
80
|
-
* 中间件用于处理消息流转
|
|
81
|
-
*/
|
|
82
|
-
addMiddleware(middleware, name) {
|
|
83
|
-
this.#middlewares.push(middleware);
|
|
84
|
-
const dispose = () => remove(this.#middlewares, middleware);
|
|
85
|
-
this.onDispose(dispose);
|
|
86
|
-
return dispose;
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* 注册交互式 action 回调处理器(自动安装根中间件,优先于 Dispatcher)
|
|
90
|
-
*/
|
|
91
|
-
registerInteractiveHandler(prefix, handler) {
|
|
92
|
-
ensureInteractiveMiddleware((mw) => this.root.addMiddleware(mw));
|
|
93
|
-
const dispose = registerInteractiveHandlerCore(prefix, handler);
|
|
94
|
-
this.onDispose(dispose);
|
|
95
|
-
return dispose;
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* 插件名称
|
|
99
|
-
*/
|
|
100
|
-
get name() {
|
|
101
|
-
if (this.#cachedName)
|
|
102
|
-
return this.#cachedName;
|
|
103
|
-
let name = path
|
|
104
|
-
.relative(process.cwd(), this.filePath)
|
|
105
|
-
.replace(/\?t=\d+$/, "")
|
|
106
|
-
.replace(/\\/g, "/")
|
|
107
|
-
.replace(/\/index\.(js|ts)x?$/, "")
|
|
108
|
-
.replace(/\/(lib|src|dist)$/, "");
|
|
109
|
-
const nodeModulesIndex = name.indexOf("node_modules/");
|
|
110
|
-
if (nodeModulesIndex !== -1)
|
|
111
|
-
name = name.slice(nodeModulesIndex + "node_modules/".length);
|
|
112
|
-
const slash = name.lastIndexOf("/");
|
|
113
|
-
if (slash !== -1)
|
|
114
|
-
name = name.slice(slash + 1);
|
|
115
|
-
name = name.replace(/\.(js|ts)x?$/, "");
|
|
116
|
-
this.#cachedName = name;
|
|
117
|
-
return this.#cachedName;
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* 插件清单(从 plugin.yml 或 package.json 延迟读取)
|
|
121
|
-
*/
|
|
122
|
-
get manifest() {
|
|
123
|
-
if (this.#manifest !== undefined)
|
|
124
|
-
return this.#manifest ?? undefined;
|
|
125
|
-
if (!this.filePath) {
|
|
126
|
-
this.#manifest = null;
|
|
127
|
-
return undefined;
|
|
128
|
-
}
|
|
129
|
-
const dir = path.dirname(this.filePath);
|
|
130
|
-
// 优先读取 plugin.yml
|
|
131
|
-
const ymlPath = path.join(dir, 'plugin.yml');
|
|
132
|
-
if (fs.existsSync(ymlPath)) {
|
|
133
|
-
try {
|
|
134
|
-
const content = fs.readFileSync(ymlPath, 'utf-8');
|
|
135
|
-
const match = content.match(/^name:\s*(.+)$/m);
|
|
136
|
-
const descMatch = content.match(/^description:\s*(.+)$/m);
|
|
137
|
-
const verMatch = content.match(/^version:\s*(.+)$/m);
|
|
138
|
-
if (match) {
|
|
139
|
-
this.#manifest = {
|
|
140
|
-
name: match[1].trim(),
|
|
141
|
-
description: descMatch?.[1]?.trim(),
|
|
142
|
-
version: verMatch?.[1]?.trim(),
|
|
143
|
-
};
|
|
144
|
-
return this.#manifest;
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
catch { /* ignore */ }
|
|
148
|
-
}
|
|
149
|
-
// Fallback: package.json
|
|
150
|
-
const pkgPath = path.join(dir, 'package.json');
|
|
151
|
-
if (fs.existsSync(pkgPath)) {
|
|
152
|
-
try {
|
|
153
|
-
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
|
|
154
|
-
if (pkg.name) {
|
|
155
|
-
this.#manifest = {
|
|
156
|
-
name: pkg.name,
|
|
157
|
-
description: pkg.description,
|
|
158
|
-
version: pkg.version,
|
|
159
|
-
};
|
|
160
|
-
return this.#manifest;
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
catch { /* ignore */ }
|
|
164
|
-
}
|
|
165
|
-
this.#manifest = null;
|
|
166
|
-
return undefined;
|
|
167
|
-
}
|
|
168
|
-
// 根插件类型窄化已在 interface Plugin 声明
|
|
169
|
-
/**
|
|
170
|
-
* 上下文收集 — 使用类型窄化的 inject 替代直接 Map 访问
|
|
171
|
-
*/
|
|
172
|
-
useContext(...args) {
|
|
173
|
-
const contexts = args.slice(0, -1);
|
|
174
|
-
const sideEffect = args[args.length - 1];
|
|
175
|
-
const contextReadyCallback = async () => {
|
|
176
|
-
if (sideEffect.finished)
|
|
177
|
-
return;
|
|
178
|
-
sideEffect.finished = true;
|
|
179
|
-
const args = contexts.map(item => this.inject(item));
|
|
180
|
-
const dispose = await sideEffect(...args);
|
|
181
|
-
if (!dispose)
|
|
182
|
-
return;
|
|
183
|
-
const disposeFn = async (name) => {
|
|
184
|
-
if (!contexts.includes(name))
|
|
185
|
-
return;
|
|
186
|
-
await dispose(this.inject(name));
|
|
187
|
-
this.off('context.dispose', disposeFn);
|
|
188
|
-
sideEffect.finished = false;
|
|
189
|
-
};
|
|
190
|
-
this.on('context.dispose', disposeFn);
|
|
191
|
-
const cleanupOnDispose = () => {
|
|
192
|
-
this.off('context.dispose', disposeFn);
|
|
193
|
-
dispose(this.inject(contexts[0]));
|
|
194
|
-
};
|
|
195
|
-
this.once('dispose', cleanupOnDispose);
|
|
196
|
-
};
|
|
197
|
-
const onContextMounted = async (name) => {
|
|
198
|
-
if (!this.#contextsIsReady(contexts) || !(contexts).includes(name))
|
|
199
|
-
return;
|
|
200
|
-
await contextReadyCallback();
|
|
201
|
-
};
|
|
202
|
-
this.on('context.mounted', onContextMounted);
|
|
203
|
-
// 插件销毁时移除 context.mounted 监听器
|
|
204
|
-
this.once('dispose', () => this.off('context.mounted', onContextMounted));
|
|
205
|
-
if (!this.#contextsIsReady(contexts))
|
|
206
|
-
return;
|
|
207
|
-
contextReadyCallback();
|
|
208
|
-
}
|
|
209
|
-
inject(name) {
|
|
210
|
-
const resolved = name === 'cron' ? 'schedule' : name;
|
|
211
|
-
const context = this.root.contexts.get(resolved);
|
|
212
|
-
return context?.value;
|
|
213
|
-
}
|
|
214
|
-
injectAdapter(name) {
|
|
215
|
-
const value = this.inject(name);
|
|
216
|
-
return value instanceof Adapter ? value : undefined;
|
|
217
|
-
}
|
|
218
|
-
#contextsIsReady(contexts) {
|
|
219
|
-
if (!contexts.length)
|
|
220
|
-
return true;
|
|
221
|
-
return contexts.every(name => this.contextIsReady(name));
|
|
222
|
-
}
|
|
223
|
-
contextIsReady(name) {
|
|
224
|
-
try {
|
|
225
|
-
return !!this.inject(name);
|
|
226
|
-
}
|
|
227
|
-
catch {
|
|
228
|
-
return false;
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
// ============================================================================
|
|
232
|
-
// 生命周期方法
|
|
233
|
-
// ============================================================================
|
|
234
|
-
/**
|
|
235
|
-
* 启动插件
|
|
236
|
-
* 覆盖 PluginBase.start() 以支持 Context 挂载失败回滚
|
|
237
|
-
*/
|
|
238
|
-
async start(t) {
|
|
239
|
-
if (this.started)
|
|
240
|
-
return;
|
|
241
|
-
// 先挂载所有 Context(带回滚),再标记 started
|
|
242
|
-
await this.mountAllContexts();
|
|
243
|
-
this.started = true;
|
|
244
|
-
await this.broadcast("mounted");
|
|
245
|
-
for (const child of this.children) {
|
|
246
|
-
await child.start(t);
|
|
247
|
-
}
|
|
248
|
-
if (t) {
|
|
249
|
-
this.logger.debug(formatCompact({ name: this.name, reload_ms: Date.now() - t }));
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
/**
|
|
253
|
-
* 覆盖 mountAllContexts:支持部分失败回滚
|
|
254
|
-
*/
|
|
255
|
-
async mountAllContexts() {
|
|
256
|
-
const mountedContexts = [];
|
|
257
|
-
for (const [name, context] of this.$contexts) {
|
|
258
|
-
try {
|
|
259
|
-
await this.mountContext(context);
|
|
260
|
-
mountedContexts.push({ name, context });
|
|
261
|
-
}
|
|
262
|
-
catch (e) {
|
|
263
|
-
this.logger.warn(`Context "${name}" mount failed: ${e}, rolling back ${mountedContexts.length} mounted contexts`);
|
|
264
|
-
for (let i = mountedContexts.length - 1; i >= 0; i--) {
|
|
265
|
-
const { name: mName, context: mCtx } = mountedContexts[i];
|
|
266
|
-
try {
|
|
267
|
-
if (mCtx.extensions)
|
|
268
|
-
unregisterExtensions(Object.keys(mCtx.extensions));
|
|
269
|
-
if (typeof mCtx.dispose === 'function')
|
|
270
|
-
await mCtx.dispose(mCtx.value);
|
|
271
|
-
}
|
|
272
|
-
catch (disposeErr) {
|
|
273
|
-
this.logger.warn(`Rollback dispose for "${mName}" failed: ${disposeErr}`);
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
throw e;
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
for (const { name } of mountedContexts) {
|
|
280
|
-
this.dispatch('context.mounted', name);
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
/**
|
|
284
|
-
* 获取插件提供的功能
|
|
285
|
-
* 从各个服务中获取数据
|
|
286
|
-
*/
|
|
287
|
-
get features() {
|
|
288
|
-
const commandService = this.inject('command');
|
|
289
|
-
const componentService = this.inject('component');
|
|
290
|
-
const scheduleService = this.inject('schedule');
|
|
291
|
-
return {
|
|
292
|
-
commands: commandService ? commandService.items.map(c => c.pattern) : [],
|
|
293
|
-
components: componentService ? componentService.getAllNames() : [],
|
|
294
|
-
schedules: scheduleService ? scheduleService.items.map(s => s.id) : [],
|
|
295
|
-
middlewares: this.#middlewares.map((m, i) => m.name || `middleware_${i}`),
|
|
296
|
-
};
|
|
297
|
-
}
|
|
298
|
-
/**
|
|
299
|
-
* 获取插件功能摘要(数组形式)
|
|
300
|
-
* 返回各功能类型的名称和数量
|
|
301
|
-
*/
|
|
302
|
-
getFeatures() {
|
|
303
|
-
const result = [];
|
|
304
|
-
const f = this.features;
|
|
305
|
-
if (f.commands.length > 0)
|
|
306
|
-
result.push({ name: 'command', count: f.commands.length });
|
|
307
|
-
if (f.components.length > 0)
|
|
308
|
-
result.push({ name: 'component', count: f.components.length });
|
|
309
|
-
if (f.schedules.length > 0)
|
|
310
|
-
result.push({ name: 'schedule', count: f.schedules.length });
|
|
311
|
-
// #middlewares includes the default command middleware, only count user-added ones
|
|
312
|
-
const userMiddlewareCount = this.#middlewares.length - 1; // subtract default #messageMiddleware
|
|
313
|
-
if (userMiddlewareCount > 0)
|
|
314
|
-
result.push({ name: 'middleware', count: userMiddlewareCount });
|
|
315
|
-
// Tool count is now tracked via ToolFeature, not Plugin#tools
|
|
316
|
-
return result;
|
|
317
|
-
}
|
|
318
|
-
info() {
|
|
319
|
-
return {
|
|
320
|
-
[this.name]: {
|
|
321
|
-
features: this.features,
|
|
322
|
-
children: this.children.map(child => child.info())
|
|
323
|
-
}
|
|
324
|
-
};
|
|
325
|
-
}
|
|
326
|
-
/**
|
|
327
|
-
* 停止插件
|
|
328
|
-
* 委托 PluginBase.stop() 处理通用清理,仅补充 IM 特化逻辑
|
|
329
|
-
*/
|
|
330
|
-
async stop() {
|
|
331
|
-
if (!this.started)
|
|
332
|
-
return;
|
|
333
|
-
this.logger.debug(`Stopping plugin "${this.name}"`);
|
|
334
|
-
// 通用清理(子插件、contexts、disposables、loadedModules 等)
|
|
335
|
-
await super.stop();
|
|
336
|
-
// IM 特化:重置中间件(保留默认消息中间件)
|
|
337
|
-
this.#middlewares.length = 1;
|
|
338
|
-
}
|
|
339
|
-
// ============================================================================
|
|
340
|
-
// 生命周期钩子
|
|
341
|
-
// ============================================================================
|
|
342
|
-
onMounted(callback) {
|
|
343
|
-
this.on("mounted", callback);
|
|
344
|
-
}
|
|
345
|
-
// onDispose 继承自 PluginBase
|
|
346
|
-
// ============================================================================
|
|
347
|
-
// 事件广播
|
|
348
|
-
// ============================================================================
|
|
349
|
-
/**
|
|
350
|
-
* dispatch - 向上冒泡到父插件,或者在根节点广播(类型安全版本)
|
|
351
|
-
*/
|
|
352
|
-
async dispatch(name, ...args) {
|
|
353
|
-
if (this.parent) {
|
|
354
|
-
return this.parent.dispatch(name, ...args);
|
|
355
|
-
}
|
|
356
|
-
return this.broadcast(name, ...args);
|
|
357
|
-
}
|
|
358
|
-
/**
|
|
359
|
-
* broadcast - 向下广播到所有子插件(类型安全版本)
|
|
360
|
-
*/
|
|
361
|
-
async broadcast(name, ...args) {
|
|
362
|
-
const listeners = this.listeners(name);
|
|
363
|
-
for (const listener of listeners) {
|
|
364
|
-
try {
|
|
365
|
-
await listener(...args);
|
|
366
|
-
}
|
|
367
|
-
catch (e) {
|
|
368
|
-
this.logger.warn(`Broadcast "${String(name)}" listener error: ${e}`);
|
|
369
|
-
}
|
|
370
|
-
}
|
|
371
|
-
for (const child of this.children) {
|
|
372
|
-
await child.broadcast(name, ...args);
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
// ============================================================================
|
|
376
|
-
// 依赖注入
|
|
377
|
-
// ============================================================================
|
|
378
|
-
/**
|
|
379
|
-
* 注册上下文(类型安全版本)
|
|
380
|
-
*/
|
|
381
|
-
provide(target) {
|
|
382
|
-
if (target instanceof Feature) {
|
|
383
|
-
const feature = target;
|
|
384
|
-
const ctx = {
|
|
385
|
-
name: feature.name,
|
|
386
|
-
description: feature.desc,
|
|
387
|
-
value: feature,
|
|
388
|
-
mounted: feature.mounted
|
|
389
|
-
? async (plugin) => {
|
|
390
|
-
await feature.mounted(plugin);
|
|
391
|
-
return feature;
|
|
392
|
-
}
|
|
393
|
-
: undefined,
|
|
394
|
-
dispose: feature.dispose
|
|
395
|
-
? async () => { await feature.dispose(); }
|
|
396
|
-
: undefined,
|
|
397
|
-
extensions: feature.extensions,
|
|
398
|
-
};
|
|
399
|
-
return this.provide(ctx);
|
|
400
|
-
}
|
|
401
|
-
const context = target;
|
|
402
|
-
if (!Plugin[contextsKey].includes(context.name)) {
|
|
403
|
-
Plugin[contextsKey].push(context.name);
|
|
404
|
-
}
|
|
405
|
-
this.logger.debug(`Context "${context.name}" provided`);
|
|
406
|
-
this.$contexts.set(context.name, context);
|
|
407
|
-
// 注册扩展方法到共享 registry,确保后续 import 的插件可用
|
|
408
|
-
if (context.extensions) {
|
|
409
|
-
installExtensionProxy(Plugin.prototype);
|
|
410
|
-
for (const [name, fn] of Object.entries(context.extensions)) {
|
|
411
|
-
if (typeof fn === 'function') {
|
|
412
|
-
registerExtension(name, fn);
|
|
413
|
-
}
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
|
-
return this;
|
|
417
|
-
}
|
|
418
|
-
// ============================================================================
|
|
419
|
-
// 插件加载
|
|
420
|
-
// ============================================================================
|
|
421
|
-
/**
|
|
422
|
-
* 导入插件(Plugin 类型窄化版本)
|
|
423
|
-
*/
|
|
424
|
-
async import(entry, t) {
|
|
425
|
-
if (!entry)
|
|
426
|
-
throw new Error(`import plugin failed: entry is empty`);
|
|
427
|
-
const resolved = resolveEntry(path.isAbsolute(entry) ?
|
|
428
|
-
entry :
|
|
429
|
-
path.resolve(path.dirname(this.filePath), entry)) || entry;
|
|
430
|
-
let realPath;
|
|
431
|
-
try {
|
|
432
|
-
realPath = fs.realpathSync(resolved);
|
|
433
|
-
}
|
|
434
|
-
catch {
|
|
435
|
-
realPath = resolved;
|
|
436
|
-
}
|
|
437
|
-
// 避免重复加载同一路径的插件
|
|
438
|
-
const normalized = realPath.replace(/\?t=\d+$/, '').replace(/\\/g, '/');
|
|
439
|
-
const existing = this.children.find(child => child.filePath.replace(/\?t=\d+$/, '').replace(/\\/g, '/') === normalized);
|
|
440
|
-
if (existing) {
|
|
441
|
-
this.logger.warn(`Plugin "${entry}" already loaded, skipping...`);
|
|
442
|
-
if (this.started && !existing.started)
|
|
443
|
-
await existing.start(t);
|
|
444
|
-
return existing;
|
|
445
|
-
}
|
|
446
|
-
const plugin = await Plugin.create(realPath, this);
|
|
447
|
-
if (this.started)
|
|
448
|
-
await plugin.start(t);
|
|
449
|
-
if (process.env.NODE_ENV === 'development') {
|
|
450
|
-
plugin.watch((p) => p.reload());
|
|
451
|
-
}
|
|
452
|
-
return plugin;
|
|
453
|
-
}
|
|
454
|
-
/**
|
|
455
|
-
* 重载插件(Plugin 类型窄化版本)
|
|
456
|
-
*/
|
|
457
|
-
async reload(plugin = this) {
|
|
458
|
-
const p = plugin;
|
|
459
|
-
// A burst of file events for one logical save (or an overlapping edit
|
|
460
|
-
// while a reload is still in flight) must not start a second concurrent
|
|
461
|
-
// stop()+import() cycle for the same plugin.
|
|
462
|
-
if (p._reloading)
|
|
463
|
-
return;
|
|
464
|
-
p._reloading = true;
|
|
465
|
-
try {
|
|
466
|
-
this.logger.info(formatCompact({ name: p.name, reload: true }));
|
|
467
|
-
const now = Date.now();
|
|
468
|
-
if (!p.parent) {
|
|
469
|
-
// 根插件重载 = 退出进程(由 CLI 重启)
|
|
470
|
-
process.exit(51);
|
|
471
|
-
return;
|
|
472
|
-
}
|
|
473
|
-
const entry = p.filePath;
|
|
474
|
-
const parent = p.parent;
|
|
475
|
-
await p.stop();
|
|
476
|
-
let fresh;
|
|
477
|
-
try {
|
|
478
|
-
fresh = await parent.import(entry, now);
|
|
479
|
-
}
|
|
480
|
-
catch (err) {
|
|
481
|
-
this.logger.error(formatCompact({
|
|
482
|
-
name: p.name,
|
|
483
|
-
reload: false,
|
|
484
|
-
error: err instanceof Error ? err.message : String(err),
|
|
485
|
-
hint: 'full_restart',
|
|
486
|
-
}));
|
|
487
|
-
throw err;
|
|
488
|
-
}
|
|
489
|
-
await fresh.broadcast('mounted');
|
|
490
|
-
this.logger.debug(formatCompact({ name: fresh.name, reload_ms: Date.now() - now }));
|
|
491
|
-
}
|
|
492
|
-
finally {
|
|
493
|
-
p._reloading = false;
|
|
494
|
-
}
|
|
495
|
-
}
|
|
496
|
-
/**
|
|
497
|
-
* 监听文件变化(Plugin 类型窄化版本)
|
|
498
|
-
*/
|
|
499
|
-
watch(callback, recursive = false) {
|
|
500
|
-
if (!this.filePath || this.filePath.includes("node_modules"))
|
|
501
|
-
return;
|
|
502
|
-
const unwatch = watchFile(this.filePath, (() => {
|
|
503
|
-
let debounceTimer = null;
|
|
504
|
-
return () => {
|
|
505
|
-
if (debounceTimer)
|
|
506
|
-
clearTimeout(debounceTimer);
|
|
507
|
-
debounceTimer = setTimeout(() => {
|
|
508
|
-
debounceTimer = null;
|
|
509
|
-
const newHash = getFileHash(this.filePath);
|
|
510
|
-
if (newHash === this.fileHash)
|
|
511
|
-
return;
|
|
512
|
-
this.logger.debug(`Plugin "${this.name}" file changed, reloading...`);
|
|
513
|
-
callback(this);
|
|
514
|
-
this.fileHash = newHash;
|
|
515
|
-
}, 300);
|
|
516
|
-
};
|
|
517
|
-
})());
|
|
518
|
-
this.on("dispose", unwatch);
|
|
519
|
-
if (recursive) {
|
|
520
|
-
for (const child of this.children) {
|
|
521
|
-
child.watch(callback, recursive);
|
|
522
|
-
}
|
|
523
|
-
}
|
|
524
|
-
}
|
|
525
|
-
// ============================================================================
|
|
526
|
-
// 辅助方法
|
|
527
|
-
// ============================================================================
|
|
528
|
-
// 核心方法列表(需要绑定的方法)
|
|
529
|
-
static #coreMethods = new Set([
|
|
530
|
-
'addMiddleware', 'useContext', 'inject', 'contextIsReady',
|
|
531
|
-
'start', 'stop', 'onMounted', 'onDispose',
|
|
532
|
-
'dispatch', 'broadcast', 'provide', 'import', 'reload', 'watch', 'info'
|
|
533
|
-
]);
|
|
534
|
-
/**
|
|
535
|
-
* 自动绑定核心方法(只在构造函数中调用一次)
|
|
536
|
-
*/
|
|
537
|
-
$bindMethods() {
|
|
538
|
-
if (this.#methodsBound)
|
|
539
|
-
return;
|
|
540
|
-
this.#methodsBound = true;
|
|
541
|
-
const proto = Object.getPrototypeOf(this);
|
|
542
|
-
for (const key of Plugin.#coreMethods) {
|
|
543
|
-
const value = proto[key];
|
|
544
|
-
if (typeof value === "function") {
|
|
545
|
-
this[key] = value.bind(this);
|
|
546
|
-
}
|
|
547
|
-
}
|
|
548
|
-
}
|
|
549
|
-
// ============================================================================
|
|
550
|
-
// 静态方法
|
|
551
|
-
// ============================================================================
|
|
552
|
-
/**
|
|
553
|
-
* 创建插件实例(异步加载)
|
|
554
|
-
*/
|
|
555
|
-
static async create(entry, parent) {
|
|
556
|
-
entry = path.resolve(resolvePluginResolveDir(parent), entry);
|
|
557
|
-
const entryFile = fs.existsSync(entry) ? entry : pluginCreateRequire().resolve(entry);
|
|
558
|
-
const realPath = fs.realpathSync(entryFile);
|
|
559
|
-
// 检查模块是否已加载(跳过热重载后已 stop 的僵尸实例)
|
|
560
|
-
const existing = loadedModules.get(realPath);
|
|
561
|
-
if (existing?.started) {
|
|
562
|
-
return existing;
|
|
563
|
-
}
|
|
564
|
-
if (existing) {
|
|
565
|
-
loadedModules.delete(realPath);
|
|
566
|
-
}
|
|
567
|
-
const plugin = new Plugin(realPath, parent);
|
|
568
|
-
plugin.fileHash = getFileHash(entryFile);
|
|
569
|
-
// 先记录,防止循环依赖时重复加载
|
|
570
|
-
loadedModules.set(realPath, plugin);
|
|
571
|
-
// 注意:?t= cache-busting 会导致 Node ESM 模块缓存累积旧条目。
|
|
572
|
-
// 这是 Node.js ESM 的已知限制——没有公开 API 可以逐出 ESM 模块缓存。
|
|
573
|
-
// 在生产环境中插件只加载一次,不会触发此问题;
|
|
574
|
-
// 开发环境中热重载会累积,但仅影响开发进程,重启即释放。
|
|
575
|
-
await storage.run(plugin, async () => {
|
|
576
|
-
await import(`${pathToFileURL(entryFile).href}?t=${Date.now()}`);
|
|
577
|
-
});
|
|
578
|
-
return plugin;
|
|
579
|
-
}
|
|
580
|
-
}
|
|
581
|
-
export function defineContext(options) {
|
|
582
|
-
return options;
|
|
583
|
-
}
|
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
import { AdapterMessage, RegisteredAdapter } from './types.js';
|
|
2
|
-
import { Plugin } from './plugin.js';
|
|
3
|
-
import { Message } from './message.js';
|
|
4
|
-
import { Schema } from '@zhin.js/schema';
|
|
5
|
-
/**
|
|
6
|
-
* Prompt类:用于实现机器人与用户的交互式提问与输入收集。
|
|
7
|
-
* 支持文本、数字、确认、列表、选项、Schema等多种输入类型,自动处理超时、默认值、格式化等。
|
|
8
|
-
* 典型用法:await new SchemaInteraction(plugin, event).text('请输入内容')
|
|
9
|
-
* @template P 适配器类型
|
|
10
|
-
*/
|
|
11
|
-
export declare class SchemaInteraction<P extends RegisteredAdapter> {
|
|
12
|
-
private plugin;
|
|
13
|
-
private event;
|
|
14
|
-
/**
|
|
15
|
-
* 构造函数
|
|
16
|
-
* @param plugin 所属插件实例
|
|
17
|
-
* @param event 当前消息事件
|
|
18
|
-
*/
|
|
19
|
-
constructor(plugin: Plugin, event: Message<AdapterMessage<P>>);
|
|
20
|
-
/**
|
|
21
|
-
* 获取当前会话唯一标识(适配器-机器人-频道-用户)
|
|
22
|
-
*/
|
|
23
|
-
private getChannelAddress;
|
|
24
|
-
/**
|
|
25
|
-
* 通用提问方法,支持自定义格式化、超时、默认值等
|
|
26
|
-
* @param config 提问配置
|
|
27
|
-
*/
|
|
28
|
-
private collect;
|
|
29
|
-
/**
|
|
30
|
-
* 注册一次性消息中间件,等待用户输入或超时
|
|
31
|
-
* @param callback 输入回调
|
|
32
|
-
* @param timeout 超时时间(默认3分钟)
|
|
33
|
-
* @param timeoutText 超时提示
|
|
34
|
-
*/
|
|
35
|
-
middleware(callback: (input: string | Error) => any, timeout?: number, timeoutText?: string): void;
|
|
36
|
-
/**
|
|
37
|
-
* 文本输入
|
|
38
|
-
*/
|
|
39
|
-
text(tips: string, timeout?: number, defaultValue?: string, timeoutText?: string): Promise<string>;
|
|
40
|
-
/**
|
|
41
|
-
* 任意输入
|
|
42
|
-
*/
|
|
43
|
-
any(tips: string, timeout?: number, defaultValue?: string, timeoutText?: string): Promise<string>;
|
|
44
|
-
/**
|
|
45
|
-
* 数字输入
|
|
46
|
-
*/
|
|
47
|
-
number(tips: string, timeout?: number, defaultValue?: number, timeoutText?: string): Promise<number>;
|
|
48
|
-
/**
|
|
49
|
-
* 确认输入(如 yes/no)
|
|
50
|
-
*/
|
|
51
|
-
confirm(tips: string, condition?: string, timeout?: number, defaultValue?: boolean, timeoutText?: string): Promise<boolean>;
|
|
52
|
-
/**
|
|
53
|
-
* 列表输入,支持多值分隔
|
|
54
|
-
*/
|
|
55
|
-
list<T extends SchemaInteraction.SingleType = 'text'>(tips: string, config?: SchemaInteraction.ListConfig<T>, timeoutText?: string): Promise<SchemaInteraction.Result<T>[]>;
|
|
56
|
-
/**
|
|
57
|
-
* 返回常量值(用于Schema)
|
|
58
|
-
*/
|
|
59
|
-
const<T = any>(value: T): Promise<T>;
|
|
60
|
-
/**
|
|
61
|
-
* 选项选择,支持单选/多选
|
|
62
|
-
*/
|
|
63
|
-
pick<T extends SchemaInteraction.SingleType, M extends boolean = false>(tips: string, config: SchemaInteraction.PickConfig<T, M>, timeoutText?: string): Promise<SchemaInteraction.PickResult<T, M>>;
|
|
64
|
-
/**
|
|
65
|
-
* 基于Schema的选项选择
|
|
66
|
-
*/
|
|
67
|
-
pickValueWithSchema<T extends Schema>(schema: T): Promise<Schema.Types<T>>;
|
|
68
|
-
/**
|
|
69
|
-
* 批量Schema输入
|
|
70
|
-
*/
|
|
71
|
-
getValueWithSchemas<T extends Record<string, Schema>>(schemas: T): Promise<{
|
|
72
|
-
[K in keyof T]: Schema.Types<T[K]>;
|
|
73
|
-
}>;
|
|
74
|
-
/**
|
|
75
|
-
* 单个Schema输入,自动分发到不同类型
|
|
76
|
-
*/
|
|
77
|
-
getValueWithSchema<T extends Schema>(schema: T): Promise<Schema.Types<T>>;
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* Prompt命名空间:类型辅助定义
|
|
81
|
-
*/
|
|
82
|
-
export declare namespace SchemaInteraction {
|
|
83
|
-
interface SingleMap {
|
|
84
|
-
text: string;
|
|
85
|
-
number: number;
|
|
86
|
-
boolean: boolean;
|
|
87
|
-
}
|
|
88
|
-
export interface ListConfig<T extends SingleType> {
|
|
89
|
-
type: T;
|
|
90
|
-
defaultValue?: SingleMap[T][];
|
|
91
|
-
separator?: string;
|
|
92
|
-
timeout?: number;
|
|
93
|
-
}
|
|
94
|
-
export interface PickConfig<T extends SingleType = SingleType, M extends boolean = false> {
|
|
95
|
-
type: T;
|
|
96
|
-
defaultValue?: PickResult<T, M>;
|
|
97
|
-
separator?: string;
|
|
98
|
-
timeout?: number;
|
|
99
|
-
options: PickOption<T>[];
|
|
100
|
-
multiple?: M;
|
|
101
|
-
}
|
|
102
|
-
export type PickOption<T extends SingleType = 'text'> = {
|
|
103
|
-
label: string;
|
|
104
|
-
value: SingleMap[T];
|
|
105
|
-
};
|
|
106
|
-
export type PickResult<T extends SingleType, M extends boolean> = M extends true ? Result<T>[] : Result<T>;
|
|
107
|
-
export type SingleType = keyof SingleMap;
|
|
108
|
-
export type Result<T extends SingleType> = SingleMap[T];
|
|
109
|
-
export type Config<R = any> = {
|
|
110
|
-
tips: string;
|
|
111
|
-
defaultValue?: R;
|
|
112
|
-
timeout?: number;
|
|
113
|
-
timeoutText?: string;
|
|
114
|
-
format: (input: string) => R;
|
|
115
|
-
};
|
|
116
|
-
export {};
|
|
117
|
-
}
|