@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
package/lib/built/tool.js DELETED
@@ -1,388 +0,0 @@
1
- /**
2
- * ToolFeature — 统一的工具管理服务
3
- * 工具仅面向 AI(通过 MCP 注册),不再与 Command 互转
4
- */
5
- import { Feature } from "@zhin.js/kernel";
6
- import { toPermissionSubject } from '@zhin.js/permission';
7
- /**
8
- * 检查工具是否可被当前 Message 通讯上下文访问。
9
- * `host` 为 PermissionHost 实例;无 host 且有 permissions → deny。
10
- */
11
- export async function canAccessTool(tool, message, host) {
12
- if (!message)
13
- return !tool.platforms?.length && !tool.scopes?.length && !tool.permissions?.length;
14
- const adapter = String(message.$adapter);
15
- const scope = (message.$channel?.type || 'private');
16
- if (tool.platforms?.length && !tool.platforms.includes(adapter)) {
17
- return false;
18
- }
19
- if (tool.scopes?.length && !tool.scopes.includes(scope)) {
20
- return false;
21
- }
22
- if (!tool.permissions?.length)
23
- return true;
24
- if (!host)
25
- return false;
26
- const subject = toPermissionSubject(message);
27
- return host.checkAll(tool.permissions, subject);
28
- }
29
- // ============================================================================
30
- // Tool 工具函数
31
- // ============================================================================
32
- /**
33
- * 从参数定义中提取参数信息
34
- */
35
- export function extractParamInfo(parameters) {
36
- if (!parameters.properties)
37
- return [];
38
- const required = parameters.required || [];
39
- return Object.entries(parameters.properties).map(([name, schema]) => ({
40
- name,
41
- type: schema.type,
42
- required: required.includes(name),
43
- description: schema.description,
44
- default: schema.default,
45
- enum: schema.enum,
46
- }));
47
- }
48
- /**
49
- * 定义工具的辅助函数(提供类型推断)
50
- */
51
- export function defineTool(tool) {
52
- return tool;
53
- }
54
- /**
55
- * ZhinTool 类
56
- * 提供链式调用风格来定义工具
57
- */
58
- export class ZhinTool {
59
- #name;
60
- #description = '';
61
- /** 有序的参数列表 */
62
- #params = [];
63
- #execute;
64
- #platforms = [];
65
- #scopes = [];
66
- #permissions = [];
67
- #tags = [];
68
- #keywords = [];
69
- #hidden = false;
70
- #source;
71
- #preExecutable = false;
72
- #kind;
73
- constructor(name) {
74
- this.#name = name;
75
- }
76
- get name() {
77
- return this.#name;
78
- }
79
- get description() {
80
- return this.#description;
81
- }
82
- get params() {
83
- return [...this.#params];
84
- }
85
- desc(description) {
86
- this.#description = description;
87
- return this;
88
- }
89
- param(name, schema, required = false) {
90
- const existingIndex = this.#params.findIndex(p => p.name === name);
91
- if (existingIndex >= 0) {
92
- this.#params[existingIndex] = { name, schema, required };
93
- }
94
- else {
95
- this.#params.push({ name, schema, required });
96
- }
97
- return this;
98
- }
99
- platform(...platforms) {
100
- this.#platforms.push(...platforms);
101
- return this;
102
- }
103
- scope(...scopes) {
104
- this.#scopes.push(...scopes);
105
- return this;
106
- }
107
- permit(...permissions) {
108
- this.#permissions.push(...permissions);
109
- return this;
110
- }
111
- tag(...tags) {
112
- this.#tags.push(...tags);
113
- return this;
114
- }
115
- keyword(...keywords) {
116
- this.#keywords.push(...keywords);
117
- return this;
118
- }
119
- hidden(value = true) {
120
- this.#hidden = value;
121
- return this;
122
- }
123
- /**
124
- * 标记此工具允许被预执行(opt-in)。
125
- * 仅适用于无副作用的只读工具(如获取系统状态、读取配置等)。
126
- * 默认为 false,即不预执行。
127
- */
128
- preExec(value = true) {
129
- this.#preExecutable = value;
130
- return this;
131
- }
132
- /** 设置工具分类(如 file / shell / web),用于展示与 TOOLS.md 协同 */
133
- kind(value) {
134
- this.#kind = value;
135
- return this;
136
- }
137
- execute(callback) {
138
- this.#execute = callback;
139
- return this;
140
- }
141
- #buildParameters() {
142
- const properties = {};
143
- const required = [];
144
- const sortedParams = [...this.#params].sort((a, b) => {
145
- if (a.required && !b.required)
146
- return -1;
147
- if (!a.required && b.required)
148
- return 1;
149
- return 0;
150
- });
151
- for (const param of sortedParams) {
152
- properties[param.name] = param.schema;
153
- if (param.required) {
154
- required.push(param.name);
155
- }
156
- }
157
- return {
158
- type: 'object',
159
- properties,
160
- required: required.length > 0 ? required : undefined,
161
- };
162
- }
163
- toTool() {
164
- if (!this.#execute) {
165
- throw new Error(`Tool "${this.#name}" has no execute() defined`);
166
- }
167
- const tool = {
168
- name: this.#name,
169
- description: this.#description,
170
- parameters: this.#buildParameters(),
171
- execute: this.#execute,
172
- };
173
- if (this.#platforms.length > 0)
174
- tool.platforms = this.#platforms;
175
- if (this.#scopes.length > 0)
176
- tool.scopes = this.#scopes;
177
- if (this.#permissions.length > 0)
178
- tool.permissions = this.#permissions;
179
- if (this.#tags.length > 0)
180
- tool.tags = this.#tags;
181
- if (this.#hidden)
182
- tool.hidden = this.#hidden;
183
- if (this.#source)
184
- tool.source = this.#source;
185
- if (this.#keywords.length > 0)
186
- tool.keywords = this.#keywords;
187
- if (this.#preExecutable)
188
- tool.preExecutable = true;
189
- if (this.#kind)
190
- tool.kind = this.#kind;
191
- return tool;
192
- }
193
- toJSON() {
194
- const json = {
195
- name: this.#name,
196
- description: this.#description,
197
- parameters: this.#buildParameters(),
198
- };
199
- if (this.#platforms.length > 0)
200
- json.platforms = this.#platforms;
201
- if (this.#scopes.length > 0)
202
- json.scopes = this.#scopes;
203
- if (this.#permissions.length > 0)
204
- json.permissions = [...this.#permissions];
205
- if (this.#tags.length > 0)
206
- json.tags = this.#tags;
207
- return json;
208
- }
209
- get help() {
210
- const lines = [this.#name];
211
- if (this.#description)
212
- lines.push(` ${this.#description}`);
213
- if (this.#params.length > 0) {
214
- lines.push(' 参数:');
215
- for (const param of this.#params) {
216
- const required = param.required ? '(必填)' : '(可选)';
217
- const desc = param.schema.description || '';
218
- lines.push(` ${param.name}: ${param.schema.type} ${required} ${desc}`);
219
- }
220
- }
221
- if (this.#permissions.length > 0) {
222
- lines.push(` 权限: ${this.#permissions.join(', ')}`);
223
- }
224
- if (this.#platforms.length > 0) {
225
- lines.push(` 平台: ${this.#platforms.join(', ')}`);
226
- }
227
- if (this.#scopes.length > 0) {
228
- lines.push(` 场景: ${this.#scopes.join(', ')}`);
229
- }
230
- return lines.join('\n');
231
- }
232
- toString() {
233
- return `[ZhinTool: ${this.#name}] ${this.#description}`;
234
- }
235
- }
236
- export function isZhinTool(obj) {
237
- return obj instanceof ZhinTool;
238
- }
239
- // ============================================================================
240
- // ToolFeature 实现
241
- // ============================================================================
242
- export class ToolFeature extends Feature {
243
- name = 'tool';
244
- icon = 'Wrench';
245
- desc = '工具';
246
- /** 按名称索引 */
247
- byName = new Map();
248
- /** 工具到插件名的映射 */
249
- #toolPluginMap = new Map();
250
- /**
251
- * 添加工具(仅注册到 ToolFeature,不再生成 Command)
252
- * @param toolInput 工具或 ZhinTool 实例
253
- * @param pluginName 注册插件名
254
- */
255
- addTool(toolInput, pluginName) {
256
- const zhinTool = isZhinTool(toolInput) ? toolInput : null;
257
- const tool = zhinTool ? zhinTool.toTool() : toolInput;
258
- // Re-register (e.g. adapter hot-reload) must not leave duplicate entries in items[]
259
- if (this.byName.has(tool.name)) {
260
- this.removeTool(tool.name);
261
- }
262
- const toolWithSource = {
263
- ...tool,
264
- source: tool.source || `plugin:${pluginName}`,
265
- tags: [...(tool.tags || []), 'plugin', pluginName],
266
- };
267
- this.byName.set(tool.name, toolWithSource);
268
- this.#toolPluginMap.set(tool.name, pluginName);
269
- // Use Feature.add for item tracking
270
- const baseDispose = super.add(toolWithSource, pluginName);
271
- return () => {
272
- this.removeTool(tool.name);
273
- baseDispose();
274
- };
275
- }
276
- /**
277
- * 移除工具
278
- */
279
- removeTool(name) {
280
- const tool = this.byName.get(name);
281
- if (!tool)
282
- return false;
283
- this.byName.delete(name);
284
- this.#toolPluginMap.delete(name);
285
- // 移除 item
286
- super.remove(tool);
287
- return true;
288
- }
289
- /**
290
- * 获取工具
291
- */
292
- get(name) {
293
- return this.byName.get(name);
294
- }
295
- /**
296
- * 获取所有工具
297
- */
298
- getAll() {
299
- return [...this.items];
300
- }
301
- /**
302
- * 根据标签过滤工具
303
- */
304
- getByTags(tags) {
305
- return this.items.filter(tool => tags.some(tag => tool.tags?.includes(tag)));
306
- }
307
- /**
308
- * 执行工具
309
- */
310
- async execute(name, args, message) {
311
- const tool = this.byName.get(name);
312
- if (!tool) {
313
- throw new Error(`Tool "${name}" not found`);
314
- }
315
- return tool.execute(args, message);
316
- }
317
- /**
318
- * 根据 Message 通讯上下文过滤工具
319
- */
320
- async filterByContext(tools, message, host) {
321
- const results = await Promise.all(tools.map(async (tool) => ({
322
- tool,
323
- allowed: await canAccessTool(tool, message, host),
324
- })));
325
- return results.filter((r) => r.allowed).map((r) => r.tool);
326
- }
327
- /**
328
- * 按插件名获取工具
329
- */
330
- getToolsByPlugin(pluginName) {
331
- const result = [];
332
- for (const [toolName, pName] of this.#toolPluginMap) {
333
- if (pName === pluginName) {
334
- const tool = this.byName.get(toolName);
335
- if (tool)
336
- result.push(tool);
337
- }
338
- }
339
- return result;
340
- }
341
- /**
342
- * 生命周期: 销毁时清理所有工具
343
- */
344
- dispose() {
345
- this.byName.clear();
346
- this.#toolPluginMap.clear();
347
- }
348
- /**
349
- * 兼容旧接口:tools Map
350
- */
351
- get tools() {
352
- return this.byName;
353
- }
354
- /**
355
- * 序列化为 JSON
356
- */
357
- toJSON(pluginName) {
358
- const list = pluginName ? this.getByPlugin(pluginName) : this.items;
359
- return {
360
- name: this.name,
361
- icon: this.icon,
362
- desc: this.desc,
363
- count: list.length,
364
- items: list.map(t => ({
365
- name: t.name,
366
- desc: t.description,
367
- platforms: t.platforms,
368
- tags: t.tags,
369
- })),
370
- };
371
- }
372
- /**
373
- * 提供给 Plugin.prototype 的扩展方法
374
- */
375
- get extensions() {
376
- const feature = this;
377
- return {
378
- addTool(tool) {
379
- const plugin = this;
380
- const toolObj = isZhinTool(tool) ? tool.toTool() : tool;
381
- const dispose = feature.addTool(tool, plugin.name);
382
- plugin.recordFeatureContribution(feature.name, toolObj.name);
383
- plugin.onDispose(dispose);
384
- return dispose;
385
- },
386
- };
387
- }
388
- }
package/lib/command.d.ts DELETED
@@ -1,50 +0,0 @@
1
- import { MatchResult, SegmentMatcher } from 'segment-matcher';
2
- import { AdapterMessage, SendContent, RegisteredAdapter, MaybePromise } from './types.js';
3
- import type { Message } from './message.js';
4
- import { type PermissionHost } from '@zhin.js/permission';
5
- type ConstructFirstParam<T extends new (...args: any[]) => any> = T extends new (...args: [infer U, ...any[]]) => any ? U : never;
6
- type ConstructSecondParam<T extends new (...args: any[]) => any> = T extends new (...args: [any, infer V, ...any[]]) => any ? V : never;
7
- /**
8
- * MessageCommand类:经典命令系统(segment-matcher)。
9
- *
10
- * @deprecated 新插件请用 `defineCommand`(`zhin.js/command`)+ `commands/` 约定目录。
11
- * 本类仍供 Agent init / game-kit hub / legacy `CommandFeature` 使用;计划随经典
12
- * Plugin 路径一并退役。见 `docs/contributing/public-api-surface.md`。
13
- */
14
- export declare class MessageCommand<T extends RegisteredAdapter = RegisteredAdapter> extends SegmentMatcher {
15
- #private;
16
- get helpInfo(): MessageCommand.HelpInfo;
17
- constructor(C: ConstructFirstParam<typeof SegmentMatcher>, P?: ConstructSecondParam<typeof SegmentMatcher>);
18
- get help(): string;
19
- desc(...desc: string[]): MessageCommand<T>;
20
- usage(...usage: string[]): MessageCommand<T>;
21
- examples(...examples: string[]): MessageCommand<T>;
22
- /**
23
- * 注册命令回调
24
- * @param callback 命令处理函数
25
- */
26
- action(callback: MessageCommand.Callback<T>): MessageCommand<T>;
27
- permit(...permissions: string[]): MessageCommand<T>;
28
- /** 命令要求的 permit 列表(空表示所有人可用) */
29
- get requiredPermits(): readonly string[];
30
- /** 与 handle 一致的 permit 校验,供帮助菜单等场景过滤不可见命令 */
31
- checkPermits(message: Message<AdapterMessage<T>>, host: PermissionHost | null): Promise<boolean>;
32
- /**
33
- * 处理消息,自动匹配命令并执行回调
34
- * @param message 消息对象
35
- * @param host PermissionHost(null 时有 permit 的命令 fail-closed)
36
- * @returns 命令返回内容或undefined
37
- */
38
- handle(message: Message<AdapterMessage<T>>, host: PermissionHost | null): Promise<SendContent | undefined>;
39
- }
40
- export declare namespace MessageCommand {
41
- type Callback<T extends RegisteredAdapter> = (message: Message<AdapterMessage<T>>, result: MatchResult) => SendContent | undefined | Promise<SendContent | undefined>;
42
- type Checker<T extends RegisteredAdapter> = (message: Message<AdapterMessage<T>>) => MaybePromise<boolean>;
43
- type HelpInfo = {
44
- pattern: string;
45
- desc: string[];
46
- usage: string[];
47
- examples: string[];
48
- };
49
- }
50
- export {};
package/lib/command.js DELETED
@@ -1,106 +0,0 @@
1
- import { SegmentMatcher } from 'segment-matcher';
2
- import { toPermissionSubject } from '@zhin.js/permission';
3
- /**
4
- * MessageCommand类:经典命令系统(segment-matcher)。
5
- *
6
- * @deprecated 新插件请用 `defineCommand`(`zhin.js/command`)+ `commands/` 约定目录。
7
- * 本类仍供 Agent init / game-kit hub / legacy `CommandFeature` 使用;计划随经典
8
- * Plugin 路径一并退役。见 `docs/contributing/public-api-surface.md`。
9
- */
10
- export class MessageCommand extends SegmentMatcher {
11
- #callbacks = [];
12
- #desc = [];
13
- #usage = [];
14
- #examples = [];
15
- #permissions = [];
16
- get helpInfo() {
17
- return {
18
- pattern: this.pattern,
19
- desc: this.#desc,
20
- usage: this.#usage,
21
- examples: this.#examples
22
- };
23
- }
24
- constructor(C, P = {}) {
25
- super(C, {
26
- at: ['qq', 'user_id'],
27
- face: ['id'],
28
- mention: ['qq', 'user_id'],
29
- html: ['content'],
30
- markdown: ['content'],
31
- image: ['file', 'src', 'url'],
32
- video: ['file', 'src', 'url'],
33
- audio: ['file', 'src', 'url'],
34
- file: ['file', 'src', 'url'],
35
- reply: ['id', 'reply_id', 'message_id'],
36
- quote: ['id', 'reply_id', 'message_id'],
37
- forward: ['id', 'res_id', 'message_id'],
38
- location: ['latitude', 'longitude'],
39
- ...P,
40
- });
41
- }
42
- get help() {
43
- return [
44
- this.pattern,
45
- ...this.#desc,
46
- ...this.#usage,
47
- ...this.#examples
48
- ].join("\n");
49
- }
50
- desc(...desc) {
51
- this.#desc.push(...desc);
52
- return this;
53
- }
54
- usage(...usage) {
55
- this.#usage.push(...usage);
56
- return this;
57
- }
58
- examples(...examples) {
59
- this.#examples.push(...examples);
60
- return this;
61
- }
62
- /**
63
- * 注册命令回调
64
- * @param callback 命令处理函数
65
- */
66
- action(callback) {
67
- this.#callbacks.push(callback);
68
- return this;
69
- }
70
- permit(...permissions) {
71
- this.#permissions.push(...permissions);
72
- return this;
73
- }
74
- /** 命令要求的 permit 列表(空表示所有人可用) */
75
- get requiredPermits() {
76
- return this.#permissions;
77
- }
78
- /** 与 handle 一致的 permit 校验,供帮助菜单等场景过滤不可见命令 */
79
- async checkPermits(message, host) {
80
- if (!this.#permissions.length)
81
- return true;
82
- if (!host)
83
- return false;
84
- const subject = toPermissionSubject(message);
85
- return host.checkAll(this.#permissions, subject);
86
- }
87
- /**
88
- * 处理消息,自动匹配命令并执行回调
89
- * @param message 消息对象
90
- * @param host PermissionHost(null 时有 permit 的命令 fail-closed)
91
- * @returns 命令返回内容或undefined
92
- */
93
- async handle(message, host) {
94
- if (!(await this.checkPermits(message, host))) {
95
- return;
96
- }
97
- const matched = this.match(message.$content);
98
- if (!matched)
99
- return;
100
- for (const handler of this.#callbacks) {
101
- const result = await handler(message, matched);
102
- if (result)
103
- return result;
104
- }
105
- }
106
- }
@@ -1,26 +0,0 @@
1
- import { Dict, SendContent, SendOptions } from './types.js';
2
- export declare const CapWithChild: unique symbol;
3
- export declare const CapWithClose: unique symbol;
4
- export type Component<P = any> = {
5
- (props: P, context: ComponentContext): SendContent | Promise<SendContent>;
6
- name: string;
7
- };
8
- export interface ComponentContext {
9
- render: (template: string, context?: Partial<ComponentContext>) => Promise<SendContent>;
10
- props: Readonly<Dict>;
11
- parent?: Readonly<ComponentContext>;
12
- root: string;
13
- children?: string;
14
- getValue: (template: string) => any;
15
- compile: (template: string) => string;
16
- }
17
- export declare function defineComponent<P = any>(component: Component<P>, name?: string): Component<P>;
18
- export declare function matchComponent<P = any>(comp: Component<P>, template: string): string;
19
- export declare function getProps<P = any>(comp: Component<P>, template: string, context?: ComponentContext): P;
20
- export declare function createComponentContext(props?: Dict, parent?: ComponentContext, root?: string): ComponentContext;
21
- export declare function renderComponent<P = any>(component: Component<P>, template: string, context: ComponentContext): Promise<SendContent>;
22
- export declare function renderComponents(componentMap: Map<string, Component>, options: SendOptions, customContext?: ComponentContext): Promise<SendOptions>;
23
- export declare const Fragment: Component<{
24
- children?: SendContent;
25
- }>;
26
- export declare const Fetch: Component<any>;