@zhin.js/core 1.5.16 → 1.7.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.
Files changed (334) hide show
  1. package/README.md +78 -195
  2. package/lib/ai-completion-contract.d.ts +135 -0
  3. package/lib/ai-completion-contract.js +69 -0
  4. package/lib/built/ai-access.d.ts +2 -21
  5. package/lib/built/ai-access.js +1 -46
  6. package/lib/built/ai-outbound/index.d.ts +0 -2
  7. package/lib/built/ai-outbound/index.js +0 -2
  8. package/lib/built/ai-outbound/prompt.d.ts +2 -0
  9. package/lib/built/ai-outbound/prompt.js +2 -0
  10. package/lib/built/ai-outbound/types.d.ts +1 -10
  11. package/lib/built/ai-trigger.d.ts +39 -78
  12. package/lib/built/ai-trigger.js +3 -254
  13. package/lib/built/common-adapter-tools.d.ts +21 -6
  14. package/lib/built/common-adapter-tools.js +28 -17
  15. package/lib/built/generic-segment-mapper.js +4 -1
  16. package/lib/built/html-segment-fallback.d.ts +6 -7
  17. package/lib/built/html-segment-fallback.js +1 -16
  18. package/lib/built/html-to-text.d.ts +0 -3
  19. package/lib/built/html-to-text.js +131 -40
  20. package/lib/built/interaction-messages.d.ts +1 -0
  21. package/lib/built/interaction-messages.js +30 -0
  22. package/lib/built/interactive-segments/action.d.ts +3 -4
  23. package/lib/built/interactive-segments/action.js +2 -5
  24. package/lib/built/interactive-segments/index.d.ts +0 -2
  25. package/lib/built/interactive-segments/index.js +0 -2
  26. package/lib/built/interactive-segments/keyboard-segment.d.ts +0 -1
  27. package/lib/built/interactive-segments/keyboard-segment.js +0 -1
  28. package/lib/built/interactive-segments/onebot-keyboard.d.ts +0 -1
  29. package/lib/built/interactive-segments/onebot-keyboard.js +0 -2
  30. package/lib/built/interactive-segments/resolve.d.ts +2 -3
  31. package/lib/built/interactive-segments/resolve.js +21 -14
  32. package/lib/built/interactive-segments/types.d.ts +2 -7
  33. package/lib/built/interactive-segments/types.js +22 -2
  34. package/lib/built/login-assist.d.ts +330 -25
  35. package/lib/built/login-assist.js +1156 -75
  36. package/lib/built/login-captcha.d.ts +62 -0
  37. package/lib/built/login-captcha.js +107 -0
  38. package/lib/built/message-enrich.d.ts +24 -27
  39. package/lib/built/message-enrich.js +53 -49
  40. package/lib/built/outbound-content-arrays.d.ts +4 -0
  41. package/lib/built/outbound-content-arrays.js +32 -0
  42. package/lib/built/outbound-media-trust.d.ts +46 -0
  43. package/lib/built/outbound-media-trust.js +245 -0
  44. package/lib/built/outbound-media-utils.d.ts +5 -3
  45. package/lib/built/outbound-media-utils.js +5 -3
  46. package/lib/built/readable-link.d.ts +4 -0
  47. package/lib/built/readable-link.js +32 -0
  48. package/lib/built/rich-segments/markdown-to-text.d.ts +1 -2
  49. package/lib/built/rich-segments/markdown-to-text.js +141 -17
  50. package/lib/built/roles.d.ts +141 -12
  51. package/lib/built/roles.js +253 -52
  52. package/lib/built/segment-contract/assert.js +8 -2
  53. package/lib/built/segment-contract/delivery.js +1 -0
  54. package/lib/built/segment-contract/index.d.ts +3 -3
  55. package/lib/built/segment-contract/index.js +2 -2
  56. package/lib/built/segment-contract/json-schema.d.ts +39 -5
  57. package/lib/built/segment-contract/json-schema.js +207 -24
  58. package/lib/built/segment-contract/preview.js +8 -1
  59. package/lib/built/segment-contract/text.js +1 -1
  60. package/lib/built/segment-contract/types.d.ts +2 -2
  61. package/lib/built/segment-contract/validate.d.ts +1553 -224
  62. package/lib/built/segment-contract/validate.js +39 -3
  63. package/lib/built/tool-access.d.ts +5 -0
  64. package/lib/built/tool-access.js +17 -0
  65. package/lib/built/user-interaction.d.ts +5 -4
  66. package/lib/built/user-interaction.js +64 -52
  67. package/lib/errors.d.ts +1 -18
  68. package/lib/errors.js +1 -26
  69. package/lib/feature/command-public-api.d.ts +5 -0
  70. package/lib/feature/command-public-api.js +5 -0
  71. package/lib/feature/command.d.ts +2 -0
  72. package/lib/feature/command.js +1 -0
  73. package/lib/feature/component.d.ts +1 -0
  74. package/lib/feature/component.js +1 -0
  75. package/lib/feature/handler.d.ts +13 -7
  76. package/lib/feature/handler.js +2 -1
  77. package/lib/feature/middleware-public-api.d.ts +6 -0
  78. package/lib/feature/middleware-public-api.js +6 -0
  79. package/lib/feature/middleware.d.ts +22 -2
  80. package/lib/feature/middleware.js +7 -2
  81. package/lib/im-scene.d.ts +4 -3
  82. package/lib/im-scene.js +9 -9
  83. package/lib/im-session-id.d.ts +20 -12
  84. package/lib/im-session-id.js +8 -8
  85. package/lib/index.d.ts +20 -48
  86. package/lib/index.js +15 -41
  87. package/lib/jsx/intrinsic-elements.d.ts +83 -0
  88. package/lib/jsx/message-renderer.d.ts +13 -0
  89. package/lib/jsx/message-renderer.js +128 -0
  90. package/lib/jsx/message-tags.d.ts +133 -0
  91. package/lib/jsx/message-tags.js +50 -0
  92. package/lib/jsx-dev-runtime.d.ts +2 -2
  93. package/lib/jsx-dev-runtime.js +1 -2
  94. package/lib/jsx-runtime.d.ts +2 -12
  95. package/lib/jsx-runtime.js +1 -10
  96. package/lib/jsx.d.ts +6 -30
  97. package/lib/jsx.js +3 -93
  98. package/lib/message.d.ts +0 -40
  99. package/lib/message.js +1 -27
  100. package/lib/models/system-log.d.ts +0 -2
  101. package/lib/models/system-log.js +1 -8
  102. package/lib/models/user.d.ts +0 -2
  103. package/lib/models/user.js +1 -7
  104. package/lib/notice.d.ts +38 -33
  105. package/lib/notice.js +23 -7
  106. package/lib/plugin-runtime/im/command-feedback.d.ts +59 -0
  107. package/lib/plugin-runtime/im/command-feedback.js +148 -0
  108. package/lib/plugin-runtime/im/contracts.d.ts +217 -32
  109. package/lib/plugin-runtime/im/contracts.js +141 -23
  110. package/lib/plugin-runtime/im/conversation-runtime.d.ts +42 -0
  111. package/lib/plugin-runtime/im/conversation-runtime.js +451 -0
  112. package/lib/plugin-runtime/im/endpoint-runtime.d.ts +112 -0
  113. package/lib/plugin-runtime/im/endpoint-runtime.js +434 -0
  114. package/lib/plugin-runtime/im/feature-gate.d.ts +21 -0
  115. package/lib/plugin-runtime/im/feature-gate.js +88 -0
  116. package/lib/plugin-runtime/im/host/component-host-installer.d.ts +2 -0
  117. package/lib/plugin-runtime/im/host/component-host-installer.js +14 -0
  118. package/lib/plugin-runtime/im/host/conversation-migration.d.ts +5 -0
  119. package/lib/plugin-runtime/im/host/conversation-migration.js +75 -0
  120. package/lib/plugin-runtime/im/host/database-config.d.ts +10 -0
  121. package/lib/plugin-runtime/im/host/database-config.js +152 -0
  122. package/lib/plugin-runtime/im/host/database-console-schema.d.ts +79 -0
  123. package/lib/plugin-runtime/im/host/database-console-schema.js +477 -0
  124. package/lib/plugin-runtime/im/host/database-console-sql.d.ts +66 -0
  125. package/lib/plugin-runtime/im/host/database-console-sql.js +459 -0
  126. package/lib/plugin-runtime/im/host/database-host-installer.d.ts +60 -0
  127. package/lib/plugin-runtime/im/host/database-host-installer.js +1166 -0
  128. package/lib/plugin-runtime/im/host/database-state.d.ts +9 -0
  129. package/lib/plugin-runtime/im/host/database-state.js +91 -0
  130. package/lib/plugin-runtime/im/host/index.d.ts +8 -0
  131. package/lib/plugin-runtime/im/host/index.js +8 -0
  132. package/lib/plugin-runtime/im/host/locale-host-installer.d.ts +3 -0
  133. package/lib/plugin-runtime/im/host/locale-host-installer.js +60 -0
  134. package/lib/plugin-runtime/im/host/outbound-host-installer.d.ts +8 -0
  135. package/lib/plugin-runtime/im/host/outbound-host-installer.js +153 -0
  136. package/lib/plugin-runtime/im/host/sender-enrichment.d.ts +34 -0
  137. package/lib/plugin-runtime/im/host/sender-enrichment.js +82 -0
  138. package/lib/plugin-runtime/im/im-runtime.d.ts +40 -111
  139. package/lib/plugin-runtime/im/im-runtime.js +235 -1059
  140. package/lib/plugin-runtime/im/inbound-dedup.d.ts +84 -0
  141. package/lib/plugin-runtime/im/inbound-dedup.js +322 -0
  142. package/lib/plugin-runtime/im/inbound-origin.d.ts +150 -0
  143. package/lib/plugin-runtime/im/inbound-origin.js +183 -0
  144. package/lib/plugin-runtime/im/inbound-runtime.d.ts +59 -0
  145. package/lib/plugin-runtime/im/inbound-runtime.js +478 -0
  146. package/lib/plugin-runtime/im/inbound-spine.d.ts +196 -0
  147. package/lib/plugin-runtime/im/inbound-spine.js +335 -0
  148. package/lib/plugin-runtime/im/index.d.ts +15 -4
  149. package/lib/plugin-runtime/im/index.js +11 -4
  150. package/lib/plugin-runtime/im/interaction-runtime.d.ts +50 -0
  151. package/lib/plugin-runtime/im/interaction-runtime.js +390 -0
  152. package/lib/plugin-runtime/im/interactive-router.d.ts +81 -0
  153. package/lib/plugin-runtime/im/interactive-router.js +382 -0
  154. package/lib/plugin-runtime/im/message-diagnostics.d.ts +70 -0
  155. package/lib/plugin-runtime/im/message-diagnostics.js +182 -0
  156. package/lib/plugin-runtime/im/message-dispatcher.d.ts +18 -4
  157. package/lib/plugin-runtime/im/message-dispatcher.js +138 -20
  158. package/lib/plugin-runtime/im/message-events.d.ts +85 -0
  159. package/lib/plugin-runtime/im/message-events.js +113 -0
  160. package/lib/plugin-runtime/im/outbound-delivery-runtime.d.ts +39 -0
  161. package/lib/plugin-runtime/im/outbound-delivery-runtime.js +450 -0
  162. package/lib/plugin-runtime/im/outbound-renderer.d.ts +52 -3
  163. package/lib/plugin-runtime/im/outbound-renderer.js +188 -8
  164. package/lib/plugin-runtime/im/outbound-segments.d.ts +29 -5
  165. package/lib/plugin-runtime/im/outbound-segments.js +130 -36
  166. package/lib/plugin-runtime/im/progressive-reply.d.ts +58 -0
  167. package/lib/plugin-runtime/im/progressive-reply.js +501 -0
  168. package/lib/plugin-runtime/im/public-api.d.ts +1 -1
  169. package/lib/request.d.ts +27 -36
  170. package/lib/request.js +14 -6
  171. package/lib/side-event/base.d.ts +64 -44
  172. package/lib/side-event/base.js +72 -19
  173. package/lib/side-event/index.d.ts +1 -1
  174. package/lib/side-event/index.js +1 -1
  175. package/lib/side-event/normalize.d.ts +7 -38
  176. package/lib/side-event/normalize.js +4 -109
  177. package/lib/side-event/scene-events.d.ts +40 -0
  178. package/lib/side-event/scene-events.js +147 -0
  179. package/lib/side-event/types.d.ts +4 -13
  180. package/lib/side-event/types.js +1 -55
  181. package/lib/system-event.d.ts +17 -12
  182. package/lib/system-event.js +8 -6
  183. package/lib/types.d.ts +23 -167
  184. package/lib/utils.d.ts +50 -33
  185. package/lib/utils.js +12 -185
  186. package/lib/venue/contracts.d.ts +21 -0
  187. package/lib/venue/contracts.js +3 -0
  188. package/lib/venue/floor-controller.d.ts +11 -0
  189. package/lib/venue/floor-controller.js +63 -0
  190. package/lib/venue/index.d.ts +13 -0
  191. package/lib/venue/index.js +11 -0
  192. package/lib/venue/membership-port.d.ts +23 -0
  193. package/lib/venue/membership-port.js +141 -0
  194. package/lib/venue/projector.d.ts +38 -0
  195. package/lib/venue/projector.js +45 -0
  196. package/lib/venue/registry.d.ts +16 -0
  197. package/lib/venue/registry.js +63 -0
  198. package/lib/venue/router.d.ts +19 -0
  199. package/lib/venue/router.js +101 -0
  200. package/lib/venue/send-limiter.d.ts +17 -0
  201. package/lib/venue/send-limiter.js +73 -0
  202. package/lib/venue/validator.d.ts +6 -0
  203. package/lib/venue/validator.js +160 -0
  204. package/package.json +24 -50
  205. package/lib/adapter.d.ts +0 -145
  206. package/lib/adapter.js +0 -377
  207. package/lib/built/adapter-process.d.ts +0 -58
  208. package/lib/built/adapter-process.js +0 -128
  209. package/lib/built/agent-preset.d.ts +0 -64
  210. package/lib/built/agent-preset.js +0 -79
  211. package/lib/built/ai-outbound/adapter-access.d.ts +0 -3
  212. package/lib/built/ai-outbound/adapter-access.js +0 -9
  213. package/lib/built/ai-outbound/extensions/qq-message.d.ts +0 -3
  214. package/lib/built/ai-outbound/extensions/qq-message.js +0 -23
  215. package/lib/built/authorization.d.ts +0 -10
  216. package/lib/built/authorization.js +0 -76
  217. package/lib/built/command.d.ts +0 -72
  218. package/lib/built/command.js +0 -105
  219. package/lib/built/component.d.ts +0 -67
  220. package/lib/built/component.js +0 -97
  221. package/lib/built/config.d.ts +0 -98
  222. package/lib/built/config.js +0 -372
  223. package/lib/built/connect-endpoint-instance.d.ts +0 -17
  224. package/lib/built/connect-endpoint-instance.js +0 -42
  225. package/lib/built/database.d.ts +0 -68
  226. package/lib/built/database.js +0 -106
  227. package/lib/built/dispatcher.d.ts +0 -141
  228. package/lib/built/dispatcher.js +0 -383
  229. package/lib/built/endpoint-commands.d.ts +0 -3
  230. package/lib/built/endpoint-commands.js +0 -99
  231. package/lib/built/endpoint-lifecycle-service.d.ts +0 -28
  232. package/lib/built/endpoint-lifecycle-service.js +0 -242
  233. package/lib/built/endpoint-lifecycle.d.ts +0 -27
  234. package/lib/built/endpoint-lifecycle.js +0 -36
  235. package/lib/built/endpoint-manager.d.ts +0 -34
  236. package/lib/built/html-renderer-loader.d.ts +0 -13
  237. package/lib/built/html-renderer-loader.js +0 -33
  238. package/lib/built/inbound-pipeline.d.ts +0 -37
  239. package/lib/built/inbound-pipeline.js +0 -63
  240. package/lib/built/inbound-runner.d.ts +0 -22
  241. package/lib/built/inbound-runner.js +0 -34
  242. package/lib/built/interactive-segment-contract.d.ts +0 -6
  243. package/lib/built/interactive-segment-contract.js +0 -9
  244. package/lib/built/interactive-segments/fallback-store.d.ts +0 -29
  245. package/lib/built/interactive-segments/fallback-store.js +0 -63
  246. package/lib/built/interactive-segments/handlers.d.ts +0 -16
  247. package/lib/built/interactive-segments/handlers.js +0 -74
  248. package/lib/built/introspection-format.d.ts +0 -10
  249. package/lib/built/introspection-format.js +0 -28
  250. package/lib/built/management-command-guard.d.ts +0 -15
  251. package/lib/built/management-command-guard.js +0 -25
  252. package/lib/built/message-filter.d.ts +0 -176
  253. package/lib/built/message-filter.js +0 -239
  254. package/lib/built/optional-peer-import.d.ts +0 -3
  255. package/lib/built/optional-peer-import.js +0 -33
  256. package/lib/built/outbound-media-contract.d.ts +0 -22
  257. package/lib/built/outbound-media-contract.js +0 -10
  258. package/lib/built/permission.d.ts +0 -51
  259. package/lib/built/permission.js +0 -99
  260. package/lib/built/permit-check.d.ts +0 -7
  261. package/lib/built/permit-check.js +0 -35
  262. package/lib/built/permit-parse.d.ts +0 -16
  263. package/lib/built/permit-parse.js +0 -32
  264. package/lib/built/platform-permit.d.ts +0 -20
  265. package/lib/built/platform-permit.js +0 -91
  266. package/lib/built/queue-im-field-contract.d.ts +0 -32
  267. package/lib/built/queue-im-field-contract.js +0 -67
  268. package/lib/built/rich-segments/adapter-policies.d.ts +0 -9
  269. package/lib/built/rich-segments/adapter-policies.js +0 -27
  270. package/lib/built/rich-segments/base.d.ts +0 -11
  271. package/lib/built/rich-segments/base.js +0 -24
  272. package/lib/built/rich-segments/builtins.d.ts +0 -4
  273. package/lib/built/rich-segments/builtins.js +0 -40
  274. package/lib/built/rich-segments/capabilities.d.ts +0 -6
  275. package/lib/built/rich-segments/capabilities.js +0 -38
  276. package/lib/built/rich-segments/html-segment.d.ts +0 -15
  277. package/lib/built/rich-segments/html-segment.js +0 -62
  278. package/lib/built/rich-segments/index.d.ts +0 -13
  279. package/lib/built/rich-segments/index.js +0 -13
  280. package/lib/built/rich-segments/markdown-segment.d.ts +0 -14
  281. package/lib/built/rich-segments/markdown-segment.js +0 -58
  282. package/lib/built/rich-segments/qrcode-segment.d.ts +0 -18
  283. package/lib/built/rich-segments/qrcode-segment.js +0 -73
  284. package/lib/built/rich-segments/registry.d.ts +0 -16
  285. package/lib/built/rich-segments/registry.js +0 -52
  286. package/lib/built/rich-segments/resolve.d.ts +0 -5
  287. package/lib/built/rich-segments/resolve.js +0 -87
  288. package/lib/built/rich-segments/tts-segment.d.ts +0 -13
  289. package/lib/built/rich-segments/tts-segment.js +0 -51
  290. package/lib/built/rich-segments/types.d.ts +0 -79
  291. package/lib/built/rich-segments/types.js +0 -17
  292. package/lib/built/runtime-io.d.ts +0 -9
  293. package/lib/built/runtime-io.js +0 -67
  294. package/lib/built/schedule.d.ts +0 -54
  295. package/lib/built/schedule.js +0 -112
  296. package/lib/built/schema-endpoint-manager.d.ts +0 -17
  297. package/lib/built/schema-endpoint-manager.js +0 -73
  298. package/lib/built/schema-feature.d.ts +0 -77
  299. package/lib/built/schema-feature.js +0 -130
  300. package/lib/built/skill.d.ts +0 -120
  301. package/lib/built/skill.js +0 -151
  302. package/lib/built/speech-loader.d.ts +0 -14
  303. package/lib/built/speech-loader.js +0 -32
  304. package/lib/built/tool.d.ts +0 -151
  305. package/lib/built/tool.js +0 -388
  306. package/lib/command.d.ts +0 -50
  307. package/lib/command.js +0 -106
  308. package/lib/component.d.ts +0 -26
  309. package/lib/component.js +0 -583
  310. package/lib/endpoint-capabilities.d.ts +0 -46
  311. package/lib/endpoint-capabilities.js +0 -55
  312. package/lib/endpoint.d.ts +0 -19
  313. package/lib/endpoint.js +0 -3
  314. package/lib/host-plugin-registry.d.ts +0 -8
  315. package/lib/host-plugin-registry.js +0 -12
  316. package/lib/plugin-context.d.ts +0 -27
  317. package/lib/plugin-context.js +0 -64
  318. package/lib/plugin-runtime/im/interactive.d.ts +0 -26
  319. package/lib/plugin-runtime/im/interactive.js +0 -46
  320. package/lib/plugin-runtime/im/login-assist-host.d.ts +0 -6
  321. package/lib/plugin-runtime/im/login-assist-host.js +0 -6
  322. package/lib/plugin-runtime/im/message-bus.d.ts +0 -38
  323. package/lib/plugin-runtime/im/message-bus.js +0 -40
  324. package/lib/plugin-runtime/im/service-tokens.d.ts +0 -18
  325. package/lib/plugin-runtime/im/service-tokens.js +0 -13
  326. package/lib/plugin.d.ts +0 -268
  327. package/lib/plugin.js +0 -583
  328. package/lib/schema-interaction.d.ts +0 -117
  329. package/lib/schema-interaction.js +0 -247
  330. package/lib/side-event/dispatch.d.ts +0 -15
  331. package/lib/side-event/dispatch.js +0 -140
  332. package/lib/tool-zod.d.ts +0 -43
  333. package/lib/tool-zod.js +0 -184
  334. /package/lib/{built/endpoint-manager.js → jsx/intrinsic-elements.js} +0 -0
@@ -1,239 +0,0 @@
1
- /**
2
- * MessageFilterFeature — 消息过滤引擎
3
- *
4
- * 设计理念:
5
- * 将过滤规则视为 Feature Item,与命令 (CommandFeature)、权限 (PermissionFeature) 同构,
6
- * 遵循框架的 add/remove/extensions/toJSON 范式,支持插件级 CRUD 和生命周期自动回收。
7
- *
8
- * 核心特性:
9
- * - 基于优先级的规则引擎(first-match-wins,类似防火墙规则)
10
- * - 多维匹配:scope / adapter / endpoint / channel / sender
11
- * - 支持精确匹配、通配符 `*`、正则 `/pattern/`
12
- * - 通过 Dispatcher Guardrail 集成,在消息调度第一阶段拦截
13
- * - 插件通过 `addFilterRule()` 动态注册规则,卸载时自动清理
14
- */
15
- import { Feature } from '@zhin.js/kernel';
16
- // ============================================================================
17
- // 规则工厂
18
- // ============================================================================
19
- export var FilterRules;
20
- (function (FilterRules) {
21
- /** 创建拒绝规则 */
22
- function deny(name, conditions) {
23
- return { ...conditions, name, action: 'deny' };
24
- }
25
- FilterRules.deny = deny;
26
- /** 创建放行规则 */
27
- function allow(name, conditions) {
28
- return { ...conditions, name, action: 'allow' };
29
- }
30
- FilterRules.allow = allow;
31
- /**
32
- * 创建黑名单规则组
33
- * @returns 一条 deny 规则
34
- */
35
- function blacklist(scope, ids, name) {
36
- return {
37
- name: name ?? `${scope}-blacklist`,
38
- action: 'deny',
39
- scopes: [scope],
40
- channels: ids,
41
- };
42
- }
43
- FilterRules.blacklist = blacklist;
44
- /**
45
- * 创建白名单规则组
46
- * @returns [allow 规则, deny-catch-all 规则]
47
- */
48
- function whitelist(scope, ids, name) {
49
- const baseName = name ?? `${scope}-whitelist`;
50
- return [
51
- { name: baseName, action: 'allow', scopes: [scope], channels: ids, priority: 1 },
52
- { name: `${baseName}-deny-rest`, action: 'deny', scopes: [scope], priority: -100 },
53
- ];
54
- }
55
- FilterRules.whitelist = whitelist;
56
- })(FilterRules || (FilterRules = {}));
57
- // ============================================================================
58
- // Feature 实现
59
- // ============================================================================
60
- export class MessageFilterFeature extends Feature {
61
- name = 'message-filter';
62
- icon = 'Filter';
63
- desc = '消息过滤';
64
- /** 按规则名称索引 */
65
- byName = new Map();
66
- #defaultPolicy = 'allow';
67
- #sortedCache = null;
68
- constructor(config) {
69
- super();
70
- if (config)
71
- this.#loadConfig(config);
72
- }
73
- // ---- 公共 API ----
74
- /** 默认策略 */
75
- get defaultPolicy() { return this.#defaultPolicy; }
76
- set defaultPolicy(policy) { this.#defaultPolicy = policy; }
77
- /** 获取按优先级排序的启用规则列表(带缓存) */
78
- get sortedRules() {
79
- if (!this.#sortedCache) {
80
- this.#sortedCache = [...this.items]
81
- .filter(r => r.enabled !== false)
82
- .sort((a, b) => (b.priority ?? 0) - (a.priority ?? 0));
83
- }
84
- return this.#sortedCache;
85
- }
86
- /** 按名称查询规则 */
87
- getRule(name) {
88
- return this.byName.get(name);
89
- }
90
- /**
91
- * 检测消息是否应该被处理
92
- *
93
- * 遍历按优先级排序的规则列表,返回第一个匹配的规则结果。
94
- * 若无规则匹配,按默认策略决定。
95
- */
96
- test(message) {
97
- for (const rule of this.sortedRules) {
98
- if (this.#matchRule(rule, message)) {
99
- return {
100
- allowed: rule.action === 'allow',
101
- matchedRule: rule.name,
102
- reason: `matched rule "${rule.name}" → ${rule.action}`,
103
- };
104
- }
105
- }
106
- return {
107
- allowed: this.#defaultPolicy === 'allow',
108
- matchedRule: null,
109
- reason: `no rule matched → default policy: ${this.#defaultPolicy}`,
110
- };
111
- }
112
- // ---- Feature 重写 ----
113
- /** 添加规则,维护 byName 索引 */
114
- add(rule, pluginName) {
115
- this.byName.set(rule.name, rule);
116
- this.#sortedCache = null;
117
- return super.add(rule, pluginName);
118
- }
119
- /** 移除规则,清理 byName 索引 */
120
- remove(rule, pluginName) {
121
- this.byName.delete(rule.name);
122
- this.#sortedCache = null;
123
- return super.remove(rule, pluginName);
124
- }
125
- /** 清理所有规则注册(热重载时由 Plugin.stop() 调用) */
126
- dispose() {
127
- this.byName.clear();
128
- this.#sortedCache = null;
129
- }
130
- /** 序列化为 JSON(供 Web 控制台展示) */
131
- toJSON(pluginName) {
132
- const list = pluginName ? this.getByPlugin(pluginName) : this.items;
133
- return {
134
- name: this.name,
135
- icon: this.icon,
136
- desc: this.desc,
137
- count: list.length,
138
- items: list.map(r => ({
139
- name: r.name,
140
- description: r.description,
141
- action: r.action,
142
- priority: r.priority ?? 0,
143
- enabled: r.enabled !== false,
144
- scopes: r.scopes,
145
- adapters: r.adapters?.map(p => p instanceof RegExp ? p.source : p),
146
- endpoints: r.endpoints?.map(p => p instanceof RegExp ? p.source : p),
147
- channels: r.channels?.map(p => p instanceof RegExp ? p.source : p),
148
- senders: r.senders?.map(p => p instanceof RegExp ? p.source : p),
149
- })),
150
- };
151
- }
152
- /** 插件扩展方法 */
153
- get extensions() {
154
- const feature = this;
155
- return {
156
- addFilterRule(rule) {
157
- const plugin = this;
158
- const dispose = feature.add(rule, plugin.name);
159
- plugin.recordFeatureContribution(feature.name, rule.name);
160
- plugin.onDispose(dispose);
161
- return dispose;
162
- },
163
- testFilter(message) {
164
- return feature.test(message);
165
- },
166
- setDefaultFilterPolicy(policy) {
167
- feature.defaultPolicy = policy;
168
- },
169
- };
170
- }
171
- // ============================================================================
172
- // 配置加载
173
- // ============================================================================
174
- #loadConfig(config) {
175
- if (config.default_policy) {
176
- this.#defaultPolicy = config.default_policy;
177
- }
178
- if (config.rules) {
179
- for (const rc of config.rules) {
180
- this.add(this.#parseRuleConfig(rc), '__config__');
181
- }
182
- }
183
- }
184
- // ============================================================================
185
- // 规则匹配
186
- // ============================================================================
187
- /** 解析配置中的规则字符串为 FilterPattern */
188
- #parseRuleConfig(rc) {
189
- return {
190
- name: rc.name,
191
- description: rc.description,
192
- action: rc.action,
193
- priority: rc.priority,
194
- enabled: rc.enabled,
195
- scopes: rc.scopes,
196
- adapters: rc.adapters?.map(p => this.#parsePattern(p)),
197
- endpoints: rc.endpoints?.map(p => this.#parsePattern(p)),
198
- channels: rc.channels?.map(p => this.#parsePattern(p)),
199
- senders: rc.senders?.map(p => this.#parsePattern(p)),
200
- };
201
- }
202
- /** 解析单个 pattern:"/regex/flags" → RegExp,其余为精确字符串 */
203
- #parsePattern(pattern) {
204
- const match = pattern.match(/^\/(.+)\/([gimsuy]*)$/);
205
- if (match) {
206
- return new RegExp(match[1], match[2]);
207
- }
208
- return pattern;
209
- }
210
- /** 检查规则是否匹配消息(所有条件 AND,条件内 OR) */
211
- #matchRule(rule, message) {
212
- if (rule.scopes?.length && !rule.scopes.includes(message.$channel.type)) {
213
- return false;
214
- }
215
- if (rule.adapters?.length && !this.#matchAny(rule.adapters, String(message.$adapter))) {
216
- return false;
217
- }
218
- if (rule.endpoints?.length && !this.#matchAny(rule.endpoints, String(message.$endpoint))) {
219
- return false;
220
- }
221
- if (rule.channels?.length && !this.#matchAny(rule.channels, String(message.$channel.id))) {
222
- return false;
223
- }
224
- if (rule.senders?.length && !this.#matchAny(rule.senders, String(message.$sender.id))) {
225
- return false;
226
- }
227
- return true;
228
- }
229
- /** 检查值是否与任一 pattern 匹配 */
230
- #matchAny(patterns, value) {
231
- return patterns.some(p => {
232
- if (p instanceof RegExp)
233
- return p.test(value);
234
- if (p === '*')
235
- return true;
236
- return p === value;
237
- });
238
- }
239
- }
@@ -1,3 +0,0 @@
1
- /** 从应用根(ZHIN_PROJECT_ROOT / cwd)解析 optional peer,避免 core 包目录下 dynamic import 失败 */
2
- export declare function resolveOptionalPeerPackage(packageName: string): string | undefined;
3
- export declare function importOptionalPeerPackage<T = Record<string, unknown>>(packageName: string): Promise<T>;
@@ -1,33 +0,0 @@
1
- import { createRequire } from 'node:module';
2
- import path from 'node:path';
3
- import { pathToFileURL } from 'node:url';
4
- /** 从应用根(ZHIN_PROJECT_ROOT / cwd)解析 optional peer,避免 core 包目录下 dynamic import 失败 */
5
- export function resolveOptionalPeerPackage(packageName) {
6
- const roots = new Set();
7
- const envRoot = process.env.ZHIN_PROJECT_ROOT?.trim();
8
- if (envRoot)
9
- roots.add(path.resolve(envRoot));
10
- roots.add(process.cwd());
11
- for (const root of roots) {
12
- try {
13
- const req = createRequire(path.join(root, 'package.json'));
14
- return req.resolve(packageName);
15
- }
16
- catch {
17
- // try next root
18
- }
19
- }
20
- try {
21
- return createRequire(import.meta.url).resolve(packageName);
22
- }
23
- catch {
24
- return undefined;
25
- }
26
- }
27
- export async function importOptionalPeerPackage(packageName) {
28
- const resolved = resolveOptionalPeerPackage(packageName);
29
- if (resolved) {
30
- return import(pathToFileURL(resolved).href);
31
- }
32
- return import(packageName);
33
- }
@@ -1,22 +0,0 @@
1
- import type { SendContent } from '../types.js';
2
- /**
3
- * 出站两阶段契约(Adapter 实现参考):
4
- *
5
- * 1. **resolveRichSegments**(Adapter.renderSendMessage 首步)
6
- * 语义段(html / tts / qrcode / markdown)→ 标准 IM 段(image / audio / text)
7
- *
8
- * 2. **materializeOutboundMedia**(Endpoint.$sendMessage 前,各 adapter 可选)
9
- * 标准段中的 base64 / 本地路径 → 平台 URL 或上传后的引用
10
- */
11
- export interface OutboundMediaUploadContext {
12
- /** 上传二进制,返回平台可发送的 URL */
13
- uploadBuffer?(buffer: Buffer, mimeHint?: string): Promise<string>;
14
- /** 上传本地文件路径 */
15
- uploadFile?(filePath: string): Promise<string>;
16
- outboundDir?: string;
17
- }
18
- export type OutboundMediaMaterializer = (content: SendContent, ctx: OutboundMediaUploadContext) => Promise<SendContent>;
19
- /** 契约测试:adapter 须声明 outboundRichSegmentPolicy */
20
- export declare function assertAdapterDeclaresRichSegmentPolicy(adapterClass: {
21
- outboundRichSegmentPolicy?: Record<string, string>;
22
- }): void;
@@ -1,10 +0,0 @@
1
- /** 契约测试:adapter 须声明 outboundRichSegmentPolicy */
2
- export function assertAdapterDeclaresRichSegmentPolicy(adapterClass) {
3
- if (!adapterClass.outboundRichSegmentPolicy) {
4
- throw new Error('Adapter must declare static outboundRichSegmentPolicy');
5
- }
6
- const policy = adapterClass.outboundRichSegmentPolicy;
7
- if (typeof policy !== 'object' || Object.keys(policy).length === 0) {
8
- throw new Error('outboundRichSegmentPolicy must be a non-empty object');
9
- }
10
- }
@@ -1,51 +0,0 @@
1
- /**
2
- * PermissionFeature
3
- * 权限管理服务,继承自 Feature 抽象类
4
- */
5
- import { Feature, FeatureJSON } from "@zhin.js/kernel";
6
- import type { MaybePromise, RegisteredAdapter, AdapterMessage } from "../types.js";
7
- import { Message as MessageClass } from "../message.js";
8
- export type PermissionSubject = MessageClass<AdapterMessage<RegisteredAdapter>>;
9
- export type PermissionItem<T extends RegisteredAdapter = RegisteredAdapter> = {
10
- name: string | RegExp;
11
- check: PermissionChecker<T>;
12
- };
13
- export type PermissionChecker<T extends RegisteredAdapter = RegisteredAdapter> = (name: string, message: MessageClass<AdapterMessage<T>>) => MaybePromise<boolean>;
14
- /**
15
- * PermissionFeature 扩展方法类型
16
- */
17
- export interface PermissionContextExtensions {
18
- addPermission(permission: PermissionItem): () => void;
19
- }
20
- declare module "../plugin.js" {
21
- namespace Plugin {
22
- interface Extensions extends PermissionContextExtensions {
23
- }
24
- interface Contexts {
25
- permission: PermissionFeature;
26
- }
27
- }
28
- }
29
- export declare class PermissionFeature extends Feature<PermissionItem> {
30
- readonly name: "permission";
31
- readonly icon = "Shield";
32
- readonly desc = "\u6743\u9650";
33
- constructor();
34
- /**
35
- * 检查权限(subject 为 Message 通讯上下文)
36
- */
37
- check(name: string, subject: PermissionSubject): Promise<boolean>;
38
- /**
39
- * 序列化为 JSON
40
- */
41
- toJSON(pluginName?: string): FeatureJSON;
42
- /**
43
- * 提供给 Plugin.prototype 的扩展方法
44
- */
45
- get extensions(): {
46
- addPermission(permission: PermissionItem): () => void;
47
- };
48
- }
49
- export declare namespace Permissions {
50
- function define<T extends RegisteredAdapter = RegisteredAdapter>(name: string | RegExp, check: PermissionChecker<T>): PermissionItem<T>;
51
- }
@@ -1,99 +0,0 @@
1
- /**
2
- * PermissionFeature
3
- * 权限管理服务,继承自 Feature 抽象类
4
- */
5
- import { Feature } from "@zhin.js/kernel";
6
- import logger, { formatCompact } from "@zhin.js/logger";
7
- import { isBuiltinPermit, parsePermitName } from "./permit-parse.js";
8
- import { checkBuiltinPermit } from "./permit-check.js";
9
- import { senderRolesFromMessage } from "./message-enrich.js";
10
- function rolesForPermitCheck(message) {
11
- return senderRolesFromMessage(message);
12
- }
13
- function auditPermitDenied(permit, message, reason) {
14
- logger.debug(formatCompact({
15
- op: 'permit_denied',
16
- permit,
17
- reason,
18
- senderId: message.$sender.id,
19
- adapter: message.$adapter,
20
- endpoint: message.$endpoint,
21
- }));
22
- }
23
- export class PermissionFeature extends Feature {
24
- name = 'permission';
25
- icon = 'Shield';
26
- desc = '权限';
27
- constructor() {
28
- super();
29
- const builtinRe = /^(adapter|group|private|channel|user|role)\([^)]*\)$/;
30
- this.add(Permissions.define(builtinRe, (name, message) => {
31
- const parsed = parsePermitName(name);
32
- if (parsed && parsed.kind !== 'role') {
33
- return checkBuiltinPermit(name, message, ['user']);
34
- }
35
- return checkBuiltinPermit(name, message, rolesForPermitCheck(message));
36
- }), '__built-in__');
37
- }
38
- /**
39
- * 检查权限(subject 为 Message 通讯上下文)
40
- */
41
- async check(name, subject) {
42
- const message = subject;
43
- if (isBuiltinPermit(name)) {
44
- const parsed = parsePermitName(name);
45
- if (parsed && parsed.kind !== 'role') {
46
- if (checkBuiltinPermit(name, message, ['user']))
47
- return true;
48
- }
49
- else if (checkBuiltinPermit(name, message, rolesForPermitCheck(message))) {
50
- return true;
51
- }
52
- }
53
- for (const permission of this.items) {
54
- const passed = await permission.check(name, message);
55
- if (passed)
56
- return true;
57
- }
58
- auditPermitDenied(name, message);
59
- return false;
60
- }
61
- /**
62
- * 序列化为 JSON
63
- */
64
- toJSON(pluginName) {
65
- const list = pluginName ? this.getByPlugin(pluginName) : this.items;
66
- return {
67
- name: this.name,
68
- icon: this.icon,
69
- desc: this.desc,
70
- count: list.length,
71
- items: list.map(p => ({
72
- name: p.name instanceof RegExp ? p.name.source : p.name,
73
- })),
74
- };
75
- }
76
- /**
77
- * 提供给 Plugin.prototype 的扩展方法
78
- */
79
- get extensions() {
80
- const feature = this;
81
- return {
82
- addPermission(permission) {
83
- const plugin = this;
84
- const dispose = feature.add(permission, plugin.name);
85
- const permName = permission.name instanceof RegExp ? permission.name.source : permission.name;
86
- plugin.recordFeatureContribution(feature.name, permName);
87
- plugin.onDispose(dispose);
88
- return dispose;
89
- },
90
- };
91
- }
92
- }
93
- export var Permissions;
94
- (function (Permissions) {
95
- function define(name, check) {
96
- return { name, check };
97
- }
98
- Permissions.define = define;
99
- })(Permissions || (Permissions = {}));
@@ -1,7 +0,0 @@
1
- /**
2
- * 内置 permit 同步校验(adapter/group/private/channel/user/role)
3
- */
4
- import type { Message } from '../message.js';
5
- import { type SenderRole } from './roles.js';
6
- export declare function checkBuiltinPermit(name: string, message: Message<any>, roles: readonly SenderRole[]): boolean;
7
- export declare function checkBuiltinPermitList(permits: readonly string[], message: Message<any>, roles: readonly SenderRole[]): boolean;
@@ -1,35 +0,0 @@
1
- import { isFrameworkSenderRole, roleSatisfies } from './roles.js';
2
- import { parsePermitName } from './permit-parse.js';
3
- function channelIdMatches(id, channelId) {
4
- return id === '' || id === '*' || channelId === id;
5
- }
6
- export function checkBuiltinPermit(name, message, roles) {
7
- const parsed = parsePermitName(name);
8
- if (!parsed)
9
- return false;
10
- switch (parsed.kind) {
11
- case 'adapter':
12
- return parsed.values.some((v) => message.$adapter === v);
13
- case 'group':
14
- if (message.$channel?.type !== 'group')
15
- return false;
16
- return parsed.values.some((id) => channelIdMatches(id, String(message.$channel.id)));
17
- case 'private':
18
- if (message.$channel?.type !== 'private')
19
- return false;
20
- return parsed.values.some((id) => channelIdMatches(id, String(message.$channel.id)));
21
- case 'channel':
22
- if (message.$channel?.type !== 'channel')
23
- return false;
24
- return parsed.values.some((id) => channelIdMatches(id, String(message.$channel.id)));
25
- case 'user':
26
- return parsed.values.some((id) => String(message.$sender.id) === id);
27
- case 'role':
28
- return parsed.values.some((req) => isFrameworkSenderRole(req) && roleSatisfies(roles, [req]));
29
- default:
30
- return false;
31
- }
32
- }
33
- export function checkBuiltinPermitList(permits, message, roles) {
34
- return permits.every((p) => checkBuiltinPermit(p, message, roles));
35
- }
@@ -1,16 +0,0 @@
1
- /**
2
- * Permit 语法解析:type(a,b,c) 括号内逗号为 OR;链式 .permit().permit() 为 AND(由调用方保证)。
3
- */
4
- export type PermitKind = 'adapter' | 'group' | 'private' | 'channel' | 'user' | 'role';
5
- export interface ParsedPermit {
6
- kind: PermitKind;
7
- values: string[];
8
- }
9
- export declare function parsePermitName(name: string): ParsedPermit | null;
10
- export declare function isBuiltinPermit(name: string): boolean;
11
- export interface ParsedPlatformPermit {
12
- adapter: string;
13
- perm: string;
14
- }
15
- export declare function parsePlatformPermitName(name: string): ParsedPlatformPermit | null;
16
- export declare function isPlatformPermit(name: string): boolean;
@@ -1,32 +0,0 @@
1
- /**
2
- * Permit 语法解析:type(a,b,c) 括号内逗号为 OR;链式 .permit().permit() 为 AND(由调用方保证)。
3
- */
4
- const PERMIT_RE = /^(adapter|group|private|channel|user|role)\(([^)]*)\)$/;
5
- export function parsePermitName(name) {
6
- const m = name.match(PERMIT_RE);
7
- if (!m)
8
- return null;
9
- const kind = m[1];
10
- const inner = m[2].trim();
11
- if (!inner)
12
- return { kind, values: [''] };
13
- const values = inner.split(',').map((v) => v.trim()).filter((v) => v.length > 0);
14
- return { kind, values: values.length > 0 ? values : [''] };
15
- }
16
- export function isBuiltinPermit(name) {
17
- return PERMIT_RE.test(name);
18
- }
19
- const PLATFORM_PERMIT_RE = /^platform\(([^,)]+),([^)]*)\)$/;
20
- export function parsePlatformPermitName(name) {
21
- const m = name.match(PLATFORM_PERMIT_RE);
22
- if (!m)
23
- return null;
24
- const adapter = m[1].trim();
25
- const perm = m[2].trim();
26
- if (!adapter || !perm)
27
- return null;
28
- return { adapter, perm };
29
- }
30
- export function isPlatformPermit(name) {
31
- return PLATFORM_PERMIT_RE.test(name);
32
- }
@@ -1,20 +0,0 @@
1
- /**
2
- * Platform permit — 适配器注册的平台级权限校验(场景治理、频道身份等)
3
- */
4
- import type { Message } from '../message.js';
5
- export type PlatformPermitChecker = (perm: string, message: Message<any>) => boolean;
6
- export declare function registerPlatformPermitChecker(adapter: string, checker: PlatformPermitChecker): () => void;
7
- export declare function clearPlatformPermitCheckers(): void;
8
- export declare function checkPlatformPermit(name: string, message: Message<any>): boolean;
9
- export declare function checkPlatformPermitList(permits: readonly string[], message: Message<any>): boolean;
10
- /** OneBot / QQ 系 sender.role 归一化 */
11
- export declare function normalizeQqSenderRole(role?: string): 'owner' | 'admin' | undefined;
12
- /** 从 OneBot sender 对象写入 MessageSender.role / permissions */
13
- export declare function applyQqSenderRoleToMessageSender(sender: {
14
- role?: string;
15
- permissions?: string[];
16
- }, role?: string): void;
17
- /** 常见 IM 平台场景身份:$sender.role 为 owner / admin */
18
- export declare function createSceneRolePlatformChecker(): PlatformPermitChecker;
19
- /** 为适配器注册默认场景治理 platform checker(幂等) */
20
- export declare function registerDefaultScenePlatformPermitChecker(adapter: string): () => void;
@@ -1,91 +0,0 @@
1
- import { parsePlatformPermitName, isPlatformPermit } from './permit-parse.js';
2
- const checkers = new Map();
3
- const defaultSceneRegistrations = new Map();
4
- export function registerPlatformPermitChecker(adapter, checker) {
5
- const key = String(adapter);
6
- const registrations = checkers.get(key) ?? [];
7
- registrations.push(checker);
8
- checkers.set(key, registrations);
9
- return () => {
10
- const current = checkers.get(key);
11
- if (!current)
12
- return;
13
- const index = current.lastIndexOf(checker);
14
- if (index >= 0)
15
- current.splice(index, 1);
16
- if (current.length === 0)
17
- checkers.delete(key);
18
- };
19
- }
20
- export function clearPlatformPermitCheckers() {
21
- checkers.clear();
22
- defaultSceneRegistrations.clear();
23
- }
24
- export function checkPlatformPermit(name, message) {
25
- const parsed = parsePlatformPermitName(name);
26
- if (!parsed)
27
- return false;
28
- if (String(message.$adapter) !== parsed.adapter)
29
- return false;
30
- const registrations = checkers.get(parsed.adapter);
31
- const checker = registrations?.[registrations.length - 1];
32
- if (!checker)
33
- return false;
34
- return checker(parsed.perm, message);
35
- }
36
- export function checkPlatformPermitList(permits, message) {
37
- return permits.every((p) => isPlatformPermit(p) && checkPlatformPermit(p, message));
38
- }
39
- /** OneBot / QQ 系 sender.role 归一化 */
40
- export function normalizeQqSenderRole(role) {
41
- if (!role || role === 'member')
42
- return undefined;
43
- if (role === 'owner' || role === 'admin')
44
- return role;
45
- return undefined;
46
- }
47
- /** 从 OneBot sender 对象写入 MessageSender.role / permissions */
48
- export function applyQqSenderRoleToMessageSender(sender, role) {
49
- const normalized = normalizeQqSenderRole(role);
50
- if (!normalized)
51
- return;
52
- sender.role = normalized;
53
- sender.permissions = [normalized];
54
- }
55
- /** 常见 IM 平台场景身份:$sender.role 为 owner / admin */
56
- export function createSceneRolePlatformChecker() {
57
- return (perm, message) => {
58
- const role = message.$sender?.role;
59
- if (perm === 'scene_admin')
60
- return role === 'admin' || role === 'owner';
61
- if (perm === 'scene_owner')
62
- return role === 'owner';
63
- return false;
64
- };
65
- }
66
- /** 为适配器注册默认场景治理 platform checker(幂等) */
67
- export function registerDefaultScenePlatformPermitChecker(adapter) {
68
- const key = String(adapter);
69
- const existing = defaultSceneRegistrations.get(key);
70
- if (existing) {
71
- existing.references += 1;
72
- return () => releaseDefaultSceneChecker(key, existing);
73
- }
74
- const checker = createSceneRolePlatformChecker();
75
- const registration = {
76
- checker,
77
- dispose: registerPlatformPermitChecker(key, checker),
78
- references: 1,
79
- };
80
- defaultSceneRegistrations.set(key, registration);
81
- return () => releaseDefaultSceneChecker(key, registration);
82
- }
83
- function releaseDefaultSceneChecker(key, registration) {
84
- if (defaultSceneRegistrations.get(key) !== registration)
85
- return;
86
- registration.references -= 1;
87
- if (registration.references > 0)
88
- return;
89
- defaultSceneRegistrations.delete(key);
90
- registration.dispose();
91
- }