@zhin.js/core 1.5.15 → 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.
Files changed (328) 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 +13 -11
  35. package/lib/built/login-assist.js +32 -26
  36. package/lib/built/message-enrich.d.ts +24 -27
  37. package/lib/built/message-enrich.js +53 -49
  38. package/lib/built/outbound-content-arrays.d.ts +4 -0
  39. package/lib/built/outbound-content-arrays.js +32 -0
  40. package/lib/built/outbound-media-trust.d.ts +46 -0
  41. package/lib/built/outbound-media-trust.js +245 -0
  42. package/lib/built/outbound-media-utils.d.ts +5 -3
  43. package/lib/built/outbound-media-utils.js +5 -3
  44. package/lib/built/readable-link.d.ts +4 -0
  45. package/lib/built/readable-link.js +32 -0
  46. package/lib/built/rich-segments/markdown-to-text.d.ts +1 -2
  47. package/lib/built/rich-segments/markdown-to-text.js +141 -17
  48. package/lib/built/roles.d.ts +141 -12
  49. package/lib/built/roles.js +253 -52
  50. package/lib/built/segment-contract/assert.js +8 -2
  51. package/lib/built/segment-contract/delivery.js +1 -0
  52. package/lib/built/segment-contract/index.d.ts +3 -3
  53. package/lib/built/segment-contract/index.js +2 -2
  54. package/lib/built/segment-contract/json-schema.d.ts +39 -5
  55. package/lib/built/segment-contract/json-schema.js +207 -24
  56. package/lib/built/segment-contract/preview.js +8 -1
  57. package/lib/built/segment-contract/text.js +1 -1
  58. package/lib/built/segment-contract/types.d.ts +2 -2
  59. package/lib/built/segment-contract/validate.d.ts +1553 -224
  60. package/lib/built/segment-contract/validate.js +39 -3
  61. package/lib/built/tool-access.d.ts +5 -0
  62. package/lib/built/tool-access.js +17 -0
  63. package/lib/built/user-interaction.d.ts +5 -4
  64. package/lib/built/user-interaction.js +64 -52
  65. package/lib/errors.d.ts +1 -18
  66. package/lib/errors.js +1 -26
  67. package/lib/feature/command-public-api.d.ts +5 -0
  68. package/lib/feature/command-public-api.js +5 -0
  69. package/lib/feature/command.d.ts +2 -0
  70. package/lib/feature/command.js +1 -0
  71. package/lib/feature/component.d.ts +1 -0
  72. package/lib/feature/component.js +1 -0
  73. package/lib/feature/handler.d.ts +13 -7
  74. package/lib/feature/handler.js +2 -1
  75. package/lib/feature/middleware-public-api.d.ts +6 -0
  76. package/lib/feature/middleware-public-api.js +6 -0
  77. package/lib/feature/middleware.d.ts +22 -2
  78. package/lib/feature/middleware.js +7 -2
  79. package/lib/im-scene.d.ts +4 -3
  80. package/lib/im-scene.js +9 -9
  81. package/lib/im-session-id.d.ts +20 -12
  82. package/lib/im-session-id.js +8 -8
  83. package/lib/index.d.ts +19 -48
  84. package/lib/index.js +14 -41
  85. package/lib/jsx/intrinsic-elements.d.ts +83 -0
  86. package/lib/jsx/message-renderer.d.ts +13 -0
  87. package/lib/jsx/message-renderer.js +128 -0
  88. package/lib/jsx/message-tags.d.ts +133 -0
  89. package/lib/jsx/message-tags.js +50 -0
  90. package/lib/jsx-dev-runtime.d.ts +2 -2
  91. package/lib/jsx-dev-runtime.js +1 -2
  92. package/lib/jsx-runtime.d.ts +2 -12
  93. package/lib/jsx-runtime.js +1 -10
  94. package/lib/jsx.d.ts +6 -30
  95. package/lib/jsx.js +3 -93
  96. package/lib/message.d.ts +0 -40
  97. package/lib/message.js +1 -27
  98. package/lib/models/system-log.d.ts +0 -2
  99. package/lib/models/system-log.js +1 -8
  100. package/lib/models/user.d.ts +0 -2
  101. package/lib/models/user.js +1 -7
  102. package/lib/notice.d.ts +38 -33
  103. package/lib/notice.js +23 -7
  104. package/lib/plugin-runtime/im/command-feedback.d.ts +59 -0
  105. package/lib/plugin-runtime/im/command-feedback.js +148 -0
  106. package/lib/plugin-runtime/im/contracts.d.ts +217 -32
  107. package/lib/plugin-runtime/im/contracts.js +141 -23
  108. package/lib/plugin-runtime/im/conversation-runtime.d.ts +42 -0
  109. package/lib/plugin-runtime/im/conversation-runtime.js +451 -0
  110. package/lib/plugin-runtime/im/endpoint-runtime.d.ts +89 -0
  111. package/lib/plugin-runtime/im/endpoint-runtime.js +381 -0
  112. package/lib/plugin-runtime/im/feature-gate.d.ts +21 -0
  113. package/lib/plugin-runtime/im/feature-gate.js +88 -0
  114. package/lib/plugin-runtime/im/host/component-host-installer.d.ts +2 -0
  115. package/lib/plugin-runtime/im/host/component-host-installer.js +14 -0
  116. package/lib/plugin-runtime/im/host/conversation-migration.d.ts +5 -0
  117. package/lib/plugin-runtime/im/host/conversation-migration.js +75 -0
  118. package/lib/plugin-runtime/im/host/database-config.d.ts +10 -0
  119. package/lib/plugin-runtime/im/host/database-config.js +114 -0
  120. package/lib/plugin-runtime/im/host/database-host-installer.d.ts +29 -0
  121. package/lib/plugin-runtime/im/host/database-host-installer.js +442 -0
  122. package/lib/plugin-runtime/im/host/database-state.d.ts +9 -0
  123. package/lib/plugin-runtime/im/host/database-state.js +91 -0
  124. package/lib/plugin-runtime/im/host/index.d.ts +8 -0
  125. package/lib/plugin-runtime/im/host/index.js +8 -0
  126. package/lib/plugin-runtime/im/host/locale-host-installer.d.ts +3 -0
  127. package/lib/plugin-runtime/im/host/locale-host-installer.js +60 -0
  128. package/lib/plugin-runtime/im/host/outbound-host-installer.d.ts +8 -0
  129. package/lib/plugin-runtime/im/host/outbound-host-installer.js +153 -0
  130. package/lib/plugin-runtime/im/host/sender-enrichment.d.ts +34 -0
  131. package/lib/plugin-runtime/im/host/sender-enrichment.js +82 -0
  132. package/lib/plugin-runtime/im/im-runtime.d.ts +40 -111
  133. package/lib/plugin-runtime/im/im-runtime.js +234 -1059
  134. package/lib/plugin-runtime/im/inbound-dedup.d.ts +84 -0
  135. package/lib/plugin-runtime/im/inbound-dedup.js +322 -0
  136. package/lib/plugin-runtime/im/inbound-origin.d.ts +150 -0
  137. package/lib/plugin-runtime/im/inbound-origin.js +183 -0
  138. package/lib/plugin-runtime/im/inbound-runtime.d.ts +59 -0
  139. package/lib/plugin-runtime/im/inbound-runtime.js +478 -0
  140. package/lib/plugin-runtime/im/inbound-spine.d.ts +196 -0
  141. package/lib/plugin-runtime/im/inbound-spine.js +335 -0
  142. package/lib/plugin-runtime/im/index.d.ts +15 -4
  143. package/lib/plugin-runtime/im/index.js +11 -4
  144. package/lib/plugin-runtime/im/interaction-runtime.d.ts +50 -0
  145. package/lib/plugin-runtime/im/interaction-runtime.js +390 -0
  146. package/lib/plugin-runtime/im/interactive-router.d.ts +81 -0
  147. package/lib/plugin-runtime/im/interactive-router.js +382 -0
  148. package/lib/plugin-runtime/im/message-diagnostics.d.ts +70 -0
  149. package/lib/plugin-runtime/im/message-diagnostics.js +182 -0
  150. package/lib/plugin-runtime/im/message-dispatcher.d.ts +18 -4
  151. package/lib/plugin-runtime/im/message-dispatcher.js +138 -20
  152. package/lib/plugin-runtime/im/message-events.d.ts +85 -0
  153. package/lib/plugin-runtime/im/message-events.js +113 -0
  154. package/lib/plugin-runtime/im/outbound-delivery-runtime.d.ts +39 -0
  155. package/lib/plugin-runtime/im/outbound-delivery-runtime.js +450 -0
  156. package/lib/plugin-runtime/im/outbound-renderer.d.ts +52 -3
  157. package/lib/plugin-runtime/im/outbound-renderer.js +188 -8
  158. package/lib/plugin-runtime/im/outbound-segments.d.ts +29 -5
  159. package/lib/plugin-runtime/im/outbound-segments.js +130 -36
  160. package/lib/plugin-runtime/im/progressive-reply.d.ts +58 -0
  161. package/lib/plugin-runtime/im/progressive-reply.js +501 -0
  162. package/lib/plugin-runtime/im/public-api.d.ts +1 -1
  163. package/lib/request.d.ts +27 -36
  164. package/lib/request.js +14 -6
  165. package/lib/side-event/base.d.ts +64 -44
  166. package/lib/side-event/base.js +72 -19
  167. package/lib/side-event/index.d.ts +1 -1
  168. package/lib/side-event/index.js +1 -1
  169. package/lib/side-event/normalize.d.ts +7 -38
  170. package/lib/side-event/normalize.js +4 -109
  171. package/lib/side-event/scene-events.d.ts +40 -0
  172. package/lib/side-event/scene-events.js +147 -0
  173. package/lib/side-event/types.d.ts +4 -13
  174. package/lib/side-event/types.js +1 -55
  175. package/lib/system-event.d.ts +17 -12
  176. package/lib/system-event.js +8 -6
  177. package/lib/types.d.ts +23 -167
  178. package/lib/utils.d.ts +50 -33
  179. package/lib/utils.js +12 -185
  180. package/lib/venue/contracts.d.ts +21 -0
  181. package/lib/venue/contracts.js +3 -0
  182. package/lib/venue/floor-controller.d.ts +11 -0
  183. package/lib/venue/floor-controller.js +63 -0
  184. package/lib/venue/index.d.ts +13 -0
  185. package/lib/venue/index.js +11 -0
  186. package/lib/venue/membership-port.d.ts +23 -0
  187. package/lib/venue/membership-port.js +141 -0
  188. package/lib/venue/projector.d.ts +38 -0
  189. package/lib/venue/projector.js +45 -0
  190. package/lib/venue/registry.d.ts +16 -0
  191. package/lib/venue/registry.js +63 -0
  192. package/lib/venue/router.d.ts +19 -0
  193. package/lib/venue/router.js +101 -0
  194. package/lib/venue/send-limiter.d.ts +17 -0
  195. package/lib/venue/send-limiter.js +73 -0
  196. package/lib/venue/validator.d.ts +6 -0
  197. package/lib/venue/validator.js +160 -0
  198. package/package.json +24 -50
  199. package/lib/adapter.d.ts +0 -145
  200. package/lib/adapter.js +0 -377
  201. package/lib/built/adapter-process.d.ts +0 -58
  202. package/lib/built/adapter-process.js +0 -128
  203. package/lib/built/agent-preset.d.ts +0 -64
  204. package/lib/built/agent-preset.js +0 -79
  205. package/lib/built/ai-outbound/adapter-access.d.ts +0 -3
  206. package/lib/built/ai-outbound/adapter-access.js +0 -9
  207. package/lib/built/ai-outbound/extensions/qq-message.d.ts +0 -3
  208. package/lib/built/ai-outbound/extensions/qq-message.js +0 -23
  209. package/lib/built/authorization.d.ts +0 -10
  210. package/lib/built/authorization.js +0 -76
  211. package/lib/built/command.d.ts +0 -72
  212. package/lib/built/command.js +0 -105
  213. package/lib/built/component.d.ts +0 -67
  214. package/lib/built/component.js +0 -97
  215. package/lib/built/config.d.ts +0 -98
  216. package/lib/built/config.js +0 -372
  217. package/lib/built/connect-endpoint-instance.d.ts +0 -17
  218. package/lib/built/connect-endpoint-instance.js +0 -42
  219. package/lib/built/database.d.ts +0 -68
  220. package/lib/built/database.js +0 -106
  221. package/lib/built/dispatcher.d.ts +0 -141
  222. package/lib/built/dispatcher.js +0 -383
  223. package/lib/built/endpoint-commands.d.ts +0 -3
  224. package/lib/built/endpoint-commands.js +0 -99
  225. package/lib/built/endpoint-lifecycle-service.d.ts +0 -28
  226. package/lib/built/endpoint-lifecycle-service.js +0 -242
  227. package/lib/built/endpoint-lifecycle.d.ts +0 -27
  228. package/lib/built/endpoint-lifecycle.js +0 -36
  229. package/lib/built/endpoint-manager.d.ts +0 -34
  230. package/lib/built/html-renderer-loader.d.ts +0 -13
  231. package/lib/built/html-renderer-loader.js +0 -33
  232. package/lib/built/inbound-pipeline.d.ts +0 -37
  233. package/lib/built/inbound-pipeline.js +0 -63
  234. package/lib/built/inbound-runner.d.ts +0 -22
  235. package/lib/built/inbound-runner.js +0 -34
  236. package/lib/built/interactive-segment-contract.d.ts +0 -6
  237. package/lib/built/interactive-segment-contract.js +0 -9
  238. package/lib/built/interactive-segments/fallback-store.d.ts +0 -29
  239. package/lib/built/interactive-segments/fallback-store.js +0 -63
  240. package/lib/built/interactive-segments/handlers.d.ts +0 -16
  241. package/lib/built/interactive-segments/handlers.js +0 -74
  242. package/lib/built/introspection-format.d.ts +0 -10
  243. package/lib/built/introspection-format.js +0 -28
  244. package/lib/built/management-command-guard.d.ts +0 -15
  245. package/lib/built/management-command-guard.js +0 -25
  246. package/lib/built/message-filter.d.ts +0 -176
  247. package/lib/built/message-filter.js +0 -239
  248. package/lib/built/optional-peer-import.d.ts +0 -3
  249. package/lib/built/optional-peer-import.js +0 -33
  250. package/lib/built/outbound-media-contract.d.ts +0 -22
  251. package/lib/built/outbound-media-contract.js +0 -10
  252. package/lib/built/permission.d.ts +0 -51
  253. package/lib/built/permission.js +0 -99
  254. package/lib/built/permit-check.d.ts +0 -7
  255. package/lib/built/permit-check.js +0 -35
  256. package/lib/built/permit-parse.d.ts +0 -16
  257. package/lib/built/permit-parse.js +0 -32
  258. package/lib/built/platform-permit.d.ts +0 -20
  259. package/lib/built/platform-permit.js +0 -91
  260. package/lib/built/queue-im-field-contract.d.ts +0 -32
  261. package/lib/built/queue-im-field-contract.js +0 -67
  262. package/lib/built/rich-segments/adapter-policies.d.ts +0 -9
  263. package/lib/built/rich-segments/adapter-policies.js +0 -27
  264. package/lib/built/rich-segments/base.d.ts +0 -11
  265. package/lib/built/rich-segments/base.js +0 -24
  266. package/lib/built/rich-segments/builtins.d.ts +0 -4
  267. package/lib/built/rich-segments/builtins.js +0 -40
  268. package/lib/built/rich-segments/capabilities.d.ts +0 -6
  269. package/lib/built/rich-segments/capabilities.js +0 -38
  270. package/lib/built/rich-segments/html-segment.d.ts +0 -15
  271. package/lib/built/rich-segments/html-segment.js +0 -62
  272. package/lib/built/rich-segments/index.d.ts +0 -13
  273. package/lib/built/rich-segments/index.js +0 -13
  274. package/lib/built/rich-segments/markdown-segment.d.ts +0 -14
  275. package/lib/built/rich-segments/markdown-segment.js +0 -58
  276. package/lib/built/rich-segments/qrcode-segment.d.ts +0 -18
  277. package/lib/built/rich-segments/qrcode-segment.js +0 -73
  278. package/lib/built/rich-segments/registry.d.ts +0 -16
  279. package/lib/built/rich-segments/registry.js +0 -52
  280. package/lib/built/rich-segments/resolve.d.ts +0 -5
  281. package/lib/built/rich-segments/resolve.js +0 -87
  282. package/lib/built/rich-segments/tts-segment.d.ts +0 -13
  283. package/lib/built/rich-segments/tts-segment.js +0 -51
  284. package/lib/built/rich-segments/types.d.ts +0 -79
  285. package/lib/built/rich-segments/types.js +0 -17
  286. package/lib/built/runtime-io.d.ts +0 -9
  287. package/lib/built/runtime-io.js +0 -67
  288. package/lib/built/schedule.d.ts +0 -54
  289. package/lib/built/schedule.js +0 -112
  290. package/lib/built/schema-endpoint-manager.d.ts +0 -17
  291. package/lib/built/schema-endpoint-manager.js +0 -73
  292. package/lib/built/schema-feature.d.ts +0 -77
  293. package/lib/built/schema-feature.js +0 -130
  294. package/lib/built/skill.d.ts +0 -120
  295. package/lib/built/skill.js +0 -151
  296. package/lib/built/speech-loader.d.ts +0 -14
  297. package/lib/built/speech-loader.js +0 -32
  298. package/lib/built/tool.d.ts +0 -151
  299. package/lib/built/tool.js +0 -388
  300. package/lib/command.d.ts +0 -50
  301. package/lib/command.js +0 -106
  302. package/lib/component.d.ts +0 -26
  303. package/lib/component.js +0 -583
  304. package/lib/endpoint-capabilities.d.ts +0 -46
  305. package/lib/endpoint-capabilities.js +0 -55
  306. package/lib/endpoint.d.ts +0 -19
  307. package/lib/endpoint.js +0 -3
  308. package/lib/host-plugin-registry.d.ts +0 -8
  309. package/lib/host-plugin-registry.js +0 -12
  310. package/lib/plugin-context.d.ts +0 -27
  311. package/lib/plugin-context.js +0 -64
  312. package/lib/plugin-runtime/im/interactive.d.ts +0 -26
  313. package/lib/plugin-runtime/im/interactive.js +0 -46
  314. package/lib/plugin-runtime/im/login-assist-host.d.ts +0 -6
  315. package/lib/plugin-runtime/im/login-assist-host.js +0 -6
  316. package/lib/plugin-runtime/im/message-bus.d.ts +0 -38
  317. package/lib/plugin-runtime/im/message-bus.js +0 -40
  318. package/lib/plugin-runtime/im/service-tokens.d.ts +0 -18
  319. package/lib/plugin-runtime/im/service-tokens.js +0 -13
  320. package/lib/plugin.d.ts +0 -268
  321. package/lib/plugin.js +0 -583
  322. package/lib/schema-interaction.d.ts +0 -117
  323. package/lib/schema-interaction.js +0 -247
  324. package/lib/side-event/dispatch.d.ts +0 -15
  325. package/lib/side-event/dispatch.js +0 -140
  326. package/lib/tool-zod.d.ts +0 -43
  327. package/lib/tool-zod.js +0 -184
  328. /package/lib/{built/endpoint-manager.js → jsx/intrinsic-elements.js} +0 -0
package/lib/adapter.d.ts DELETED
@@ -1,145 +0,0 @@
1
- import { Endpoint } from "./endpoint.js";
2
- import { DEFAULT_ENDPOINT_CAPABILITIES, type EndpointCapability } from "./endpoint-capabilities.js";
3
- import type { EndpointManager } from "./built/endpoint-manager.js";
4
- import { Plugin } from "./plugin.js";
5
- import { EventEmitter } from "node:events";
6
- import { Message } from "./message.js";
7
- import { Notice } from "./notice.js";
8
- import { Request } from "./request.js";
9
- import { EditMessageOptions, SendOptions } from "./types.js";
10
- import { type OutboundRichSegmentPolicy } from "./built/rich-segments/index.js";
11
- import { type InteractivePolicy } from "./built/interactive-segments/index.js";
12
- import { type AiOutboundCapabilities, type AiOutboundExtensionDefinition } from "./built/ai-outbound/index.js";
13
- import type { Schema } from '@zhin.js/schema';
14
- /**
15
- * Adapter类:适配器抽象,管理多平台Bot实例。
16
- * 负责根据配置启动/关闭各平台机器人,统一异常处理。
17
- *
18
- * 适配器可以提供 AI 工具,供 AI 服务调用。
19
- */
20
- export declare abstract class Adapter<R extends Endpoint = Endpoint, const Caps extends readonly EndpointCapability[] = typeof DEFAULT_ENDPOINT_CAPABILITIES> extends EventEmitter<Adapter.Lifecycle> {
21
- #private;
22
- plugin: Plugin;
23
- name: keyof Plugin.Contexts;
24
- config: Adapter.EndpointConfig<R>[];
25
- endpoints: Map<string, R>;
26
- private recallMessageHandler;
27
- /** Adapter 支持的能力上限(子类覆盖) */
28
- static readonly capabilities: readonly EndpointCapability[];
29
- /** 出站富媒体段渲染策略(子类 override static) */
30
- static outboundRichSegmentPolicy: OutboundRichSegmentPolicy;
31
- /** 交互段出站策略:native 保留按钮段,text 降级为编号文本 */
32
- static interactivePolicy: InteractivePolicy;
33
- /** AI 结构化出站能力声明(mentions / richSegments / interactive) */
34
- static aiOutboundCapabilities: AiOutboundCapabilities;
35
- /** 平台特有 extensions schema + 解析(对齐各 SDK 文档子集) */
36
- static aiOutboundExtensions?: readonly AiOutboundExtensionDefinition[];
37
- protected getInteractivePolicy(): InteractivePolicy;
38
- /** 并发上限,0 表示不限制(默认) */
39
- static DEFAULT_MAX_CONCURRENT_MESSAGES: number;
40
- get maxConcurrentMessages(): number;
41
- /** 当前正在处理的消息数 */
42
- get pendingMessages(): number;
43
- /**
44
- * 创建一个适配器运行实例。
45
- * @param plugin 拥有该适配器的插件实例
46
- * @param name 适配器名称(如 'process'、'qq' 等)
47
- * @param config Endpoint 配置列表
48
- */
49
- constructor(plugin: Plugin, name: keyof Plugin.Contexts, config: Adapter.EndpointConfig<R>[]);
50
- /** 入站消息管线(替代 emit override 的隐式管线) */
51
- private inboundPipeline;
52
- /**
53
- * 重写 emit:拦截 message.receive/notice.receive/request.receive 事件,
54
- * 将入站处理委托给 InboundMessagePipeline。
55
- */
56
- emit: EventEmitter<Adapter.Lifecycle>['emit'];
57
- abstract createEndpoint(config: Adapter.EndpointConfig<R>): R;
58
- get logger(): import("@zhin.js/logger").Logger;
59
- binding(plugin: Plugin): void;
60
- /**
61
- * 同步等待入站管线结束(middleware → dispatcher → 生命周期 → 观察者)。
62
- * 用于 QQ 等需在被动回复完成后再 ack 交互的平台事件。
63
- */
64
- receiveMessageAwait(message: Message): Promise<boolean>;
65
- /** 运行时 Endpoint 管理(add/remove/edit/start/stop);未实现则 core 尝试 endpointConfigSchema 通用向导 */
66
- getEndpointManager?(): EndpointManager | null;
67
- /** 通用 schema 驱动 add/edit 时的字段定义 */
68
- getEndpointConfigSchema?(): Schema | undefined;
69
- /** 热连接失败时是否建议重启进程 */
70
- getEndpointNeedsRestart?(): boolean;
71
- /**
72
- * 出站富媒体能力(Publisher 按此过滤/降级;各 adapter 可覆盖)。
73
- */
74
- getOutboundMediaCapabilities(): {
75
- image?: boolean;
76
- audio?: boolean;
77
- video?: boolean;
78
- file?: boolean;
79
- maxAttachmentBytes?: number;
80
- };
81
- protected getOutboundRichSegmentPolicy(): OutboundRichSegmentPolicy;
82
- /**
83
- * 出站两阶段:
84
- * 1. resolveRichSegments — 语义段 → 标准 IM 段(image/audio/text)
85
- * 2. Endpoint materializeOutboundMedia — base64/本地路径 → 平台 URL(各 adapter 可选)
86
- * @see docs/essentials/rich-segment-adapters.md
87
- */
88
- protected renderSendMessage(options: SendOptions): Promise<SendOptions>;
89
- sendMessage(options: SendOptions): Promise<string>;
90
- /**
91
- * `call.recallMessage` 事件链(prompt.ts 超时撤回等)的统一出口:
92
- * 经 canonical `EndpointControl` 端口撤回。
93
- */
94
- private recallEndpointMessage;
95
- /**
96
- * 编辑已发送的消息。
97
- * - 如果 Endpoint 的 control 端口实现了 edit,调用平台编辑 API
98
- * - 否则 fallback 到发送新消息
99
- * @returns 消息 ID(编辑时返回原 ID,fallback 时返回新消息 ID)
100
- */
101
- editMessage(options: EditMessageOptions): Promise<string>;
102
- start(): Promise<void>;
103
- /**
104
- * 停止适配器,断开并移除所有 Endpoint 实例
105
- */
106
- stop(): Promise<void>;
107
- }
108
- export interface Adapters {
109
- }
110
- /** Per-adapter live Endpoint class map for HandlerContext.$endpoint() typing. */
111
- export interface AdapterEndpoints {
112
- }
113
- export declare namespace Adapter {
114
- type Factory<R extends Adapter = Adapter> = {
115
- new (plugin: Plugin, name: string, config: Adapter.EndpointConfig<Adapter.InferEndpoint<R>>[]): R;
116
- };
117
- interface Lifecycle {
118
- 'message.receive': [Message];
119
- 'message.private.receive': [Message];
120
- 'message.group.receive': [Message];
121
- 'message.channel.receive': [Message];
122
- 'notice.receive': [Notice];
123
- 'request.receive': [Request];
124
- 'call.recallMessage': [string, string];
125
- }
126
- /**
127
- * 适配器工厂注册表
128
- * 灵感来源于 zhinjs/next 的 Adapter.Registry
129
- */
130
- const Registry: Map<string, Factory<Adapter<Endpoint, readonly EndpointCapability[]>>>;
131
- type InferEndpoint<R extends Adapter = Adapter> = R extends Adapter<infer T> ? T : never;
132
- type EndpointConfig<T extends Endpoint> = T extends Endpoint<infer R> ? R : never;
133
- type EndpointMessage<T extends Endpoint> = T extends Endpoint<infer _L, infer R> ? R : never;
134
- /**
135
- * 注册适配器工厂
136
- *
137
- * @param name 适配器名称
138
- * @param factory 适配器工厂函数
139
- * @example
140
- * ```typescript
141
- * Adapter.register('icqq', IcqqAdapter);
142
- * ```
143
- */
144
- function register(name: string, factory: Factory): void;
145
- }
package/lib/adapter.js DELETED
@@ -1,377 +0,0 @@
1
- import { assertOutbound, DEFAULT_ENDPOINT_CAPABILITIES, hasInbound, } from "./endpoint-capabilities.js";
2
- import { endpointControlOf } from "@zhin.js/adapter";
3
- import { connectEndpointInstance, disconnectEndpointInstance } from "./built/connect-endpoint-instance.js";
4
- import { EventEmitter } from "node:events";
5
- import { getOutboundReplyStore } from "./built/dispatcher.js";
6
- import { DEFAULT_OUTBOUND_RICH_SEGMENT_POLICY, resolveRichSegments, } from "./built/rich-segments/index.js";
7
- import { DEFAULT_INTERACTIVE_POLICY, collectKeyboardFallbackMaps, keyboardFallbackStore, resolveKeyboardSegments, } from "./built/interactive-segments/index.js";
8
- import { DEFAULT_AI_OUTBOUND_CAPABILITIES, } from "./built/ai-outbound/index.js";
9
- import { createRichSegmentRenderContext } from "./built/rich-segments/capabilities.js";
10
- import { collectOutboundMediaKinds } from "./built/outbound-media-utils.js";
11
- import { segment } from "./utils.js";
12
- import { InboundMessagePipeline } from "./built/inbound-pipeline.js";
13
- import { formatCompact, truncatePreview, formatContentChainLog, CONTENT_CHAIN_STAGE } from '@zhin.js/logger';
14
- /**
15
- * Adapter类:适配器抽象,管理多平台Bot实例。
16
- * 负责根据配置启动/关闭各平台机器人,统一异常处理。
17
- *
18
- * 适配器可以提供 AI 工具,供 AI 服务调用。
19
- */
20
- export class Adapter extends EventEmitter {
21
- plugin;
22
- name;
23
- config;
24
- endpoints = new Map();
25
- recallMessageHandler;
26
- /** Adapter 支持的能力上限(子类覆盖) */
27
- static capabilities = DEFAULT_ENDPOINT_CAPABILITIES;
28
- /** 出站富媒体段渲染策略(子类 override static) */
29
- static outboundRichSegmentPolicy = DEFAULT_OUTBOUND_RICH_SEGMENT_POLICY;
30
- /** 交互段出站策略:native 保留按钮段,text 降级为编号文本 */
31
- static interactivePolicy = DEFAULT_INTERACTIVE_POLICY;
32
- /** AI 结构化出站能力声明(mentions / richSegments / interactive) */
33
- static aiOutboundCapabilities = DEFAULT_AI_OUTBOUND_CAPABILITIES;
34
- /** 平台特有 extensions schema + 解析(对齐各 SDK 文档子集) */
35
- static aiOutboundExtensions;
36
- getInteractivePolicy() {
37
- return this.constructor.interactivePolicy;
38
- }
39
- /** 入站消息并发计数 */
40
- #pendingMessages = 0;
41
- /** 并发上限,0 表示不限制(默认) */
42
- static DEFAULT_MAX_CONCURRENT_MESSAGES = 0;
43
- get maxConcurrentMessages() {
44
- try {
45
- const configService = this.plugin?.root?.inject('config');
46
- const appConfig = configService?.getPrimary?.();
47
- return appConfig?.max_concurrent_messages ?? Adapter.DEFAULT_MAX_CONCURRENT_MESSAGES;
48
- }
49
- catch {
50
- return Adapter.DEFAULT_MAX_CONCURRENT_MESSAGES;
51
- }
52
- }
53
- /** 当前正在处理的消息数 */
54
- get pendingMessages() {
55
- return this.#pendingMessages;
56
- }
57
- /**
58
- * 创建一个适配器运行实例。
59
- * @param plugin 拥有该适配器的插件实例
60
- * @param name 适配器名称(如 'process'、'qq' 等)
61
- * @param config Endpoint 配置列表
62
- */
63
- constructor(plugin, name, config) {
64
- super();
65
- this.plugin = plugin;
66
- this.name = name;
67
- this.config = config;
68
- this.recallMessageHandler = (endpoint_id, id) => this.recallEndpointMessage(endpoint_id, id);
69
- this.on('call.recallMessage', this.recallMessageHandler);
70
- this.inboundPipeline = new InboundMessagePipeline({
71
- getPlugin: () => this.plugin,
72
- logger: this.logger,
73
- getMaxConcurrentMessages: () => this.maxConcurrentMessages,
74
- getPendingMessages: () => this.#pendingMessages,
75
- decrementPending: () => {
76
- // stop() may zero the counter while in-flight receives still finish;
77
- // never let the budget go negative.
78
- this.#pendingMessages = Math.max(0, this.#pendingMessages - 1);
79
- },
80
- });
81
- }
82
- /** 入站消息管线(替代 emit override 的隐式管线) */
83
- inboundPipeline;
84
- /**
85
- * 重写 emit:拦截 message.receive/notice.receive/request.receive 事件,
86
- * 将入站处理委托给 InboundMessagePipeline。
87
- */
88
- emit = ((event, ...args) => {
89
- if (event === 'notice.receive' || event === 'request.receive') {
90
- return this.inboundPipeline.bridgeNoticeOrRequest(event, args, () => EventEmitter.prototype.emit.call(this, event, ...args));
91
- }
92
- if (event !== 'message.receive') {
93
- return EventEmitter.prototype.emit.call(this, event, ...args);
94
- }
95
- const message = args[0];
96
- // 背压控制:同步返回 false 表示丢弃
97
- if (this.inboundPipeline.shouldDropDueToBackpressure()) {
98
- this.logger.warn(formatCompact({ op: 'backpressure_drop', limit: this.maxConcurrentMessages }));
99
- return false;
100
- }
101
- // 同步计数,保证背压判断的准确性
102
- this.#pendingMessages++;
103
- // 异步入站管线(middleware → dispatcher → 生命周期 → 观察者)
104
- this.inboundPipeline.receive(message, () => {
105
- EventEmitter.prototype.emit.call(this, event, ...args);
106
- });
107
- return true;
108
- });
109
- get logger() {
110
- if (!this.plugin)
111
- throw new Error("Adapter is not associated with any plugin");
112
- return this.plugin.logger;
113
- }
114
- binding(plugin) {
115
- this.plugin = plugin;
116
- }
117
- /**
118
- * 同步等待入站管线结束(middleware → dispatcher → 生命周期 → 观察者)。
119
- * 用于 QQ 等需在被动回复完成后再 ack 交互的平台事件。
120
- */
121
- async receiveMessageAwait(message) {
122
- if (this.inboundPipeline.shouldDropDueToBackpressure()) {
123
- this.logger.warn(formatCompact({ drop: 'concurrency', limit: this.maxConcurrentMessages }));
124
- return false;
125
- }
126
- this.#pendingMessages++;
127
- try {
128
- await this.inboundPipeline.receive(message, () => {
129
- EventEmitter.prototype.emit.call(this, 'message.receive', message);
130
- });
131
- return true;
132
- }
133
- finally {
134
- // Inbound pipeline errors must not leak the concurrency budget; the
135
- // non-await path uses InboundMessagePipeline.decrementPending in finally.
136
- this.#pendingMessages = Math.max(0, this.#pendingMessages - 1);
137
- }
138
- }
139
- /**
140
- * 出站富媒体能力(Publisher 按此过滤/降级;各 adapter 可覆盖)。
141
- */
142
- getOutboundMediaCapabilities() {
143
- return {
144
- image: true,
145
- audio: true,
146
- video: true,
147
- file: true,
148
- maxAttachmentBytes: 26_214_400,
149
- };
150
- }
151
- getOutboundRichSegmentPolicy() {
152
- return this.constructor.outboundRichSegmentPolicy;
153
- }
154
- /**
155
- * 出站两阶段:
156
- * 1. resolveRichSegments — 语义段 → 标准 IM 段(image/audio/text)
157
- * 2. Endpoint materializeOutboundMedia — base64/本地路径 → 平台 URL(各 adapter 可选)
158
- * @see docs/essentials/rich-segment-adapters.md
159
- */
160
- async renderSendMessage(options) {
161
- if (options.content != null) {
162
- options = {
163
- ...options,
164
- content: await resolveRichSegments(options.content, this.getOutboundRichSegmentPolicy(), createRichSegmentRenderContext({
165
- getConfig: () => {
166
- const cfg = this.plugin.root.inject('config')?.getPrimary();
167
- return cfg;
168
- },
169
- warn: (msg) => this.logger.warn(msg),
170
- logContentChain: (fields) => {
171
- this.logger.debug(formatContentChainLog({ ...fields, adapter: this.name }));
172
- },
173
- })),
174
- };
175
- // 'text' 策略端点:keyboard 即将降级为编号文本,先把有效 fallback
176
- // 映射写入中央存储(频道键与入站回跳解析一致),供数字回跳路由。
177
- if (this.getInteractivePolicy() === 'text') {
178
- const channelKey = `${this.name}-${options.endpoint}-${options.type}:${options.id}`;
179
- for (const map of collectKeyboardFallbackMaps(options.content)) {
180
- keyboardFallbackStore.remember(channelKey, map);
181
- }
182
- }
183
- options = {
184
- ...options,
185
- content: resolveKeyboardSegments(options.content, this.getInteractivePolicy()),
186
- };
187
- const mediaKinds = collectOutboundMediaKinds(options.content);
188
- if (mediaKinds.length > 0) {
189
- this.logger.debug(formatContentChainLog({
190
- stage: CONTENT_CHAIN_STAGE.OUTBOUND,
191
- adapter: this.name,
192
- endpoint: options.endpoint,
193
- media: mediaKinds.join(','),
194
- }));
195
- }
196
- }
197
- const fns = this.plugin.root.listeners('before.sendMessage');
198
- for (const fn of fns) {
199
- const result = await fn(options);
200
- if (result)
201
- options = result;
202
- }
203
- return options;
204
- }
205
- async sendMessage(options) {
206
- options = await this.renderSendMessage(options);
207
- const endpoint = this.endpoints.get(options.endpoint);
208
- if (!endpoint)
209
- throw new Error(`Endpoint ${options.endpoint} not found`);
210
- assertOutbound(endpoint);
211
- this.logger.debug(formatCompact({
212
- send: `${options.type}(${options.id})`,
213
- endpoint: options.endpoint,
214
- preview: truncatePreview(segment.raw(options.content)),
215
- }));
216
- const messageId = await endpoint.$sendMessage(options);
217
- const replyStore = getOutboundReplyStore();
218
- this.plugin.root.dispatch('message.send', {
219
- adapter: this.name,
220
- options,
221
- messageId,
222
- replySource: replyStore?.source,
223
- replyMessage: replyStore?.message,
224
- });
225
- return messageId;
226
- }
227
- /**
228
- * `call.recallMessage` 事件链(prompt.ts 超时撤回等)的统一出口:
229
- * 经 canonical `EndpointControl` 端口撤回。
230
- */
231
- async recallEndpointMessage(endpointKey, messageId) {
232
- const endpoint = this.endpoints.get(endpointKey);
233
- if (!endpoint)
234
- throw new Error(`Endpoint ${endpointKey} not found`);
235
- assertOutbound(endpoint);
236
- const control = endpointControlOf(endpoint);
237
- if (!control?.recall) {
238
- throw new Error(`Endpoint ${endpointKey} does not support recall`);
239
- }
240
- this.logger.debug(formatCompact({ op: 'recall_message', msgId: messageId, endpoint: endpointKey }));
241
- await control.recall({
242
- conversation: {
243
- endpoint: { id: endpointKey, adapter: String(this.name) },
244
- kind: 'private',
245
- id: endpointKey,
246
- },
247
- id: messageId,
248
- });
249
- }
250
- /**
251
- * 编辑已发送的消息。
252
- * - 如果 Endpoint 的 control 端口实现了 edit,调用平台编辑 API
253
- * - 否则 fallback 到发送新消息
254
- * @returns 消息 ID(编辑时返回原 ID,fallback 时返回新消息 ID)
255
- */
256
- async editMessage(options) {
257
- const endpoint = this.endpoints.get(options.endpoint);
258
- if (!endpoint)
259
- throw new Error(`Endpoint ${options.endpoint} not found`);
260
- assertOutbound(endpoint);
261
- const control = endpointControlOf(endpoint);
262
- if (control?.edit) {
263
- const rendered = await this.renderSendMessage({
264
- context: options.context,
265
- endpoint: options.endpoint,
266
- id: options.id,
267
- type: options.type,
268
- content: options.content,
269
- });
270
- await control.edit({
271
- conversation: {
272
- endpoint: { id: options.endpoint, adapter: String(this.name) },
273
- kind: options.type === 'group' ? 'group' : 'private',
274
- id: options.id,
275
- },
276
- id: options.messageId,
277
- }, rendered.content);
278
- this.logger.debug(formatCompact({
279
- edit: `${options.type}(${options.id})`,
280
- endpoint: options.endpoint,
281
- messageId: options.messageId,
282
- }));
283
- return options.messageId;
284
- }
285
- this.logger.debug(formatCompact({
286
- editFallback: 'sendMessage',
287
- endpoint: options.endpoint,
288
- reason: 'control.edit not implemented',
289
- }));
290
- return this.sendMessage({
291
- context: options.context,
292
- endpoint: options.endpoint,
293
- id: options.id,
294
- type: options.type,
295
- content: options.content,
296
- });
297
- }
298
- async start() {
299
- if (this.endpoints.size > 0) {
300
- await this.stop();
301
- }
302
- const rootAdapters = this.plugin.root.adapters;
303
- if (!rootAdapters.some((n) => String(n) === String(this.name))) {
304
- rootAdapters.push(this.name);
305
- }
306
- if (!this.config?.length)
307
- return;
308
- for (const config of this.config) {
309
- const endpoint = await connectEndpointInstance({
310
- plugin: this.plugin,
311
- adapter: this,
312
- config: config,
313
- });
314
- this.logger.debug(formatCompact({ connect: endpoint.$id, adapter: this.name }));
315
- this.endpoints.set(endpoint.$id, endpoint);
316
- }
317
- this.logger.debug(formatCompact({ adapter: this.name }));
318
- }
319
- /**
320
- * 停止适配器,断开并移除所有 Endpoint 实例
321
- */
322
- async stop() {
323
- const errors = [];
324
- for (const [id, endpoint] of this.endpoints) {
325
- try {
326
- if (hasInbound(endpoint)) {
327
- await disconnectEndpointInstance(this.plugin, this, endpoint);
328
- }
329
- this.logger.debug(formatCompact({ disconnect: id, adapter: this.name }));
330
- }
331
- catch (error) {
332
- errors.push(error instanceof Error ? error : new Error(String(error)));
333
- this.logger.error(`endpoint ${id} of adapter ${this.name} disconnect failed:`, error);
334
- }
335
- }
336
- // 无论是否有错误,始终完成清理
337
- this.endpoints.clear();
338
- // Drop in-flight concurrency counter so a subsequent start() does not
339
- // inherit a stale backpressure budget from the previous generation.
340
- this.#pendingMessages = 0;
341
- // 从 adapters 数组中移除(可能因重复 start 出现多条同名,需全部删掉)
342
- const rootAdapters = this.plugin.root.adapters;
343
- for (let i = rootAdapters.length - 1; i >= 0; i--) {
344
- if (rootAdapters[i] === this.name)
345
- rootAdapters.splice(i, 1);
346
- }
347
- // 移除所有事件监听器
348
- this.removeAllListeners();
349
- this.recallMessageHandler = (endpoint_id, id) => this.recallEndpointMessage(endpoint_id, id);
350
- this.on('call.recallMessage', this.recallMessageHandler);
351
- this.logger.debug(formatCompact({ stop: this.name }));
352
- if (errors.length) {
353
- throw new AggregateError(errors, `adapter ${this.name}: ${errors.length} endpoint(s) failed to disconnect`);
354
- }
355
- }
356
- }
357
- (function (Adapter) {
358
- /**
359
- * 适配器工厂注册表
360
- * 灵感来源于 zhinjs/next 的 Adapter.Registry
361
- */
362
- Adapter.Registry = new Map();
363
- /**
364
- * 注册适配器工厂
365
- *
366
- * @param name 适配器名称
367
- * @param factory 适配器工厂函数
368
- * @example
369
- * ```typescript
370
- * Adapter.register('icqq', IcqqAdapter);
371
- * ```
372
- */
373
- function register(name, factory) {
374
- Adapter.Registry.set(name, factory);
375
- }
376
- Adapter.register = register;
377
- })(Adapter || (Adapter = {}));
@@ -1,58 +0,0 @@
1
- import { Adapter } from "../adapter.js";
2
- import { Plugin } from "../plugin.js";
3
- import { Endpoint } from "../endpoint.js";
4
- import { Message } from "../message.js";
5
- import type { SendOptions } from "../types.js";
6
- import type { OutboundRichSegmentPolicy } from "./rich-segments/types.js";
7
- export declare class ProcessEndpoint implements Endpoint<{
8
- owner?: string;
9
- }, {
10
- content: string;
11
- ts: number;
12
- }> {
13
- #private;
14
- adapter: ProcessAdapter;
15
- $config: {
16
- owner?: string;
17
- };
18
- $id: string;
19
- get logger(): import("@zhin.js/logger").Logger;
20
- $connected: boolean;
21
- constructor(adapter: ProcessAdapter, $config?: {
22
- owner?: string;
23
- });
24
- $listenInput(chunk: string): void;
25
- connect(): Promise<void>;
26
- disconnect(): Promise<void>;
27
- $formatMessage(event: {
28
- content: string;
29
- ts: number;
30
- }): Message<{
31
- content: string;
32
- ts: number;
33
- }>;
34
- get control(): {
35
- recall: (message: {
36
- id: string;
37
- }) => Promise<void>;
38
- };
39
- $recallMessage(id: string): Promise<void>;
40
- $sendMessage(options: SendOptions): Promise<string>;
41
- $connect(): Promise<void>;
42
- $disconnect(): Promise<void>;
43
- }
44
- export declare class ProcessAdapter extends Adapter<ProcessEndpoint> {
45
- static readonly capabilities: readonly ["inbound", "outbound"];
46
- static outboundRichSegmentPolicy: OutboundRichSegmentPolicy;
47
- constructor(plugin: Plugin);
48
- /**
49
- * process 出站由 $sendMessage 写入 stdout;父类 INFO preview 会与终端输出重复,故仅 debug 元数据。
50
- */
51
- sendMessage(options: SendOptions): Promise<string>;
52
- createEndpoint(config: Adapter.EndpointConfig<ProcessEndpoint>): ProcessEndpoint;
53
- }
54
- declare module '../adapter.js' {
55
- interface Adapters {
56
- process: ProcessAdapter;
57
- }
58
- }