@zhin.js/core 1.5.16 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
@@ -0,0 +1,335 @@
1
+ import { bindOperationInput, operationInputBinding } from '@zhin.js/feature-kit';
2
+ import { formatCompact, getLogger } from '@zhin.js/logger';
3
+ import { isMiddlewareIndex, middlewareFeatureId, } from '@zhin.js/middleware';
4
+ import { createToken, projectLocaleToken, resolveLocale, userLocalePreferencesToken, } from '@zhin.js/plugin-runtime';
5
+ import { handlerFeatureId, isHandlerIndex } from '../../feature/handler.js';
6
+ import { gatedHandlerIndex, gatedMiddlewareIndex } from './feature-gate.js';
7
+ import { RuntimeNotice } from '../../notice.js';
8
+ import { RuntimeRequest } from '../../request.js';
9
+ import { RuntimeSystemEvent } from '../../system-event.js';
10
+ import { RuntimeMessage, } from './contracts.js';
11
+ import { formatConversationLog } from './message-events.js';
12
+ export { arbitrateRoomInboundDeduplication, buildRoomInboundRouteInput, resolveRoomMembership, } from './inbound-dedup.js';
13
+ const logger = getLogger('im.inbound');
14
+ export const ingressRouteToken = createToken('zhin.im.ingress-route');
15
+ export function resolveIngressRoute(snapshot) {
16
+ const candidate = snapshot.resources.get(snapshot.root)?.get(ingressRouteToken.id);
17
+ return candidate
18
+ && typeof candidate === 'object'
19
+ && typeof candidate.route === 'function'
20
+ ? candidate
21
+ : undefined;
22
+ }
23
+ /** @public */
24
+ export const groupRuleViolationToken = createToken('zhin.im.group-rule-violations');
25
+ /** @public */
26
+ export const memberWelcomeOwnerToken = createToken('zhin.im.member-welcome-owner');
27
+ /** Runs the generation-owned middleware projection or the terminal directly. */
28
+ export async function runRuntimeMiddleware(snapshot, input, terminal, target) {
29
+ const index = gatedMiddlewareIndex(input, snapshot);
30
+ if (index)
31
+ await index.run(input, terminal, target, snapshot);
32
+ else
33
+ await terminal();
34
+ }
35
+ function middlewareProjection(snapshot) {
36
+ const projection = snapshot.projections.get(middlewareFeatureId);
37
+ return isMiddlewareIndex(projection) ? projection : undefined;
38
+ }
39
+ /** Legacy outbound middleware sees one complete reply unless its author explicitly supports revisions. */
40
+ export function supportsIncrementalOutbound(snapshot, adapter) {
41
+ return middlewareProjection(snapshot)?.list().every(record => record.target !== 'outbound'
42
+ || record.adapter !== undefined && record.adapter !== adapter || record.incremental === true) ?? true;
43
+ }
44
+ export function projectLocale(snapshot) {
45
+ return resolveLocale(snapshot.resources.get(snapshot.root)?.get(projectLocaleToken.id)
46
+ ?? snapshot.config.get(snapshot.root)?.locale);
47
+ }
48
+ /** Resolve once per operation; callbacks retain the original operation's language. */
49
+ export async function messageLocale(snapshot, conversation, senderId) {
50
+ const preferences = snapshot.resources.get(snapshot.root)?.get(userLocalePreferencesToken.id);
51
+ return senderId && preferences
52
+ ? await preferences.get(conversation.endpoint, senderId) ?? projectLocale(snapshot)
53
+ : projectLocale(snapshot);
54
+ }
55
+ export async function executeInboundTurnSpine(envelope, control) {
56
+ const startedAt = Date.now();
57
+ const { message, lease, requester, conversationSequence, diagnostics } = envelope;
58
+ const turnId = envelope.turnId ?? `${message.id}:${conversationSequence ?? 0}`;
59
+ const finish = async (dispatch, stage, replied, reply) => {
60
+ const record = Object.freeze({
61
+ turnId,
62
+ conversation: message.conversation,
63
+ ...(message.sender?.id ? { senderId: message.sender.id } : {}),
64
+ stage,
65
+ matched: dispatch.matched,
66
+ ...(dispatch.command ? { command: dispatch.command } : {}),
67
+ ...(dispatch.owner ? { owner: dispatch.owner } : {}),
68
+ replied,
69
+ durationMs: Math.max(0, Date.now() - startedAt),
70
+ });
71
+ await envelope.onTurnComplete?.(record);
72
+ return {
73
+ dispatch,
74
+ stage,
75
+ record,
76
+ ...(reply ? { reply } : {}),
77
+ };
78
+ };
79
+ const ingressRoute = envelope.ingressRoute ?? resolveIngressRoute(lease.value);
80
+ const preRouted = await ingressRoute?.preRoute?.(message, lease, requester, conversationSequence) === true;
81
+ if (preRouted) {
82
+ diagnostics?.route('agent');
83
+ const dispatch = Object.freeze({ matched: true, command: 'pre-route', owner: requester });
84
+ const replied = envelope.hasReplied();
85
+ await control?.reportDispatch({
86
+ matched: true,
87
+ path: 'handler',
88
+ handlerId: 'pre-route',
89
+ replied,
90
+ });
91
+ return finish(dispatch, 'pre-route', replied);
92
+ }
93
+ if (ingressRoute?.shouldRouteBeforeDispatch?.(message) === true) {
94
+ const handled = await ingressRoute.route(message, lease, requester, conversationSequence);
95
+ if (handled) {
96
+ diagnostics?.route('agent');
97
+ const dispatch = Object.freeze({ matched: true, command: 'ai', owner: requester });
98
+ const replied = envelope.hasReplied();
99
+ await control?.reportDispatch({
100
+ matched: true,
101
+ path: 'handler',
102
+ handlerId: 'ai',
103
+ replied,
104
+ });
105
+ return finish(dispatch, 'before-dispatch-route', replied);
106
+ }
107
+ }
108
+ const repliedBeforeHandlers = envelope.hasReplied();
109
+ diagnostics?.step('handlers', 'started');
110
+ await envelope.runHandlers();
111
+ diagnostics?.step('handlers', 'completed');
112
+ const repliedDuringHandlers = !repliedBeforeHandlers && envelope.hasReplied();
113
+ const interactive = await envelope.dispatchInteractive();
114
+ if (interactive)
115
+ diagnostics?.route('interaction');
116
+ const dispatched = interactive
117
+ ? undefined
118
+ : await envelope.dispatchCommand((stage, status) => diagnostics?.step(stage, status));
119
+ let result = interactive ?? dispatched.dispatch;
120
+ let stage = interactive
121
+ ? 'interactive'
122
+ : dispatched?.dispatch.matched
123
+ ? 'command'
124
+ : 'none';
125
+ if (!result.matched && ingressRoute) {
126
+ logger.debug(formatCompact({
127
+ op: 'unmatched',
128
+ conv: formatConversationLog(message.conversation),
129
+ }));
130
+ const handled = await ingressRoute.route(message, lease, requester, conversationSequence);
131
+ if (handled) {
132
+ diagnostics?.route('agent');
133
+ result = Object.freeze({ matched: true, command: 'ai', owner: requester });
134
+ stage = 'agent-ingress';
135
+ }
136
+ }
137
+ const response = dispatched?.reply ?? (interactive?.value !== undefined && interactive.owner
138
+ ? { owner: interactive.owner, content: interactive.value }
139
+ : undefined);
140
+ const replied = envelope.hasReplied() || response !== undefined;
141
+ if (interactive) {
142
+ await control?.reportDispatch({
143
+ matched: true,
144
+ path: 'button',
145
+ handlerId: interactive.command ?? 'interactive',
146
+ replied,
147
+ });
148
+ }
149
+ else if (dispatched?.dispatch.matched) {
150
+ await control?.reportDispatch({
151
+ matched: true,
152
+ path: 'command',
153
+ ...(dispatched.dispatch.command ? { handlerId: dispatched.dispatch.command } : {}),
154
+ replied,
155
+ });
156
+ }
157
+ else if (result.matched) {
158
+ await control?.reportDispatch({
159
+ matched: true,
160
+ path: 'handler',
161
+ ...(result.command ? { handlerId: result.command } : {}),
162
+ replied,
163
+ });
164
+ }
165
+ else if (repliedDuringHandlers) {
166
+ stage = 'handlers';
167
+ const handlerName = envelope.findEventHandlerName?.('message.receive') ?? 'message.receive';
168
+ await control?.reportDispatch({
169
+ matched: true,
170
+ path: 'handler',
171
+ handlerId: handlerName,
172
+ replied: true,
173
+ });
174
+ }
175
+ else {
176
+ await control?.reportDispatch({
177
+ matched: false,
178
+ path: 'none',
179
+ replied,
180
+ });
181
+ }
182
+ return finish(result, stage, replied, response);
183
+ }
184
+ export async function runInboundMiddlewareAndSpine(envelope, diagnostics) {
185
+ const { message, lease } = envelope;
186
+ const terminal = (control) => executeInboundTurnSpine(envelope, control);
187
+ const middleware = gatedMiddlewareIndex(message, lease.value);
188
+ if (isMiddlewareIndex(middleware))
189
+ diagnostics?.step('middleware', 'entered');
190
+ const outcome = isMiddlewareIndex(middleware)
191
+ ? await middleware.runInbound(message, terminal, {
192
+ stop: (owner) => {
193
+ diagnostics?.route('middleware');
194
+ diagnostics?.step('middleware', 'stopped');
195
+ return { dispatch: { matched: true, command: 'middleware', owner } };
196
+ },
197
+ replace: (owner, value, downstream) => {
198
+ diagnostics?.step('middleware', 'replaced');
199
+ if (!downstream?.dispatch.matched)
200
+ diagnostics?.route('middleware');
201
+ return {
202
+ dispatch: downstream?.dispatch.matched
203
+ ? downstream.dispatch
204
+ : { matched: true, command: 'middleware', owner },
205
+ reply: { owner, content: value },
206
+ };
207
+ },
208
+ }, lease.value)
209
+ : await terminal();
210
+ if (isMiddlewareIndex(middleware))
211
+ diagnostics?.step('middleware', 'completed');
212
+ return outcome;
213
+ }
214
+ export async function dispatchInboundHandlers({ lease, event, args, signal = lease.stopSignal, interactions, deliver, captureInteraction, }) {
215
+ const snapshot = lease.value;
216
+ // ADR 0050: only events carrying an endpoint are gated; others keep the full index.
217
+ const index = gatedHandlerIndex(args[0], snapshot);
218
+ if (!index)
219
+ return;
220
+ const options = {
221
+ snapshot,
222
+ signal,
223
+ resolveInteraction: (name, interactionArgs, owner) => {
224
+ const eventContext = interactionArgs[0];
225
+ const payload = eventContext?.payload;
226
+ if (name === 'message.receive' && payload instanceof RuntimeMessage) {
227
+ return interactions.createFromUnknown(bindOperationInput(payload, owner, snapshot));
228
+ }
229
+ if (name === 'notice.receive' || name === 'request.receive') {
230
+ return createInteractionForSideEvent({
231
+ payload: payload,
232
+ snapshot,
233
+ requester: owner,
234
+ signal,
235
+ interactions,
236
+ deliver,
237
+ ...(captureInteraction ? { retain: () => captureInteraction(owner, payload.conversation) } : {}),
238
+ });
239
+ }
240
+ return undefined;
241
+ },
242
+ };
243
+ await index.dispatch(event, args, options);
244
+ }
245
+ export function createInteractionForSideEvent({ payload, snapshot, requester, signal, interactions, deliver, retain, }) {
246
+ const conversation = payload.conversation;
247
+ if (!conversation)
248
+ return undefined;
249
+ const source = Object.freeze({
250
+ conversation,
251
+ ...(payload.actor?.id
252
+ ? { sender: Object.freeze({ id: payload.actor.id }) }
253
+ : {}),
254
+ $reply: (content) => deliver({
255
+ conversation,
256
+ requester,
257
+ content,
258
+ }, snapshot),
259
+ });
260
+ return interactions.create(source, undefined, { snapshot, owner: requester, signal, retain });
261
+ }
262
+ export async function dispatchInboundInteractive({ message, requester, admission, snapshot, interactions, }) {
263
+ const handled = await interactions.dispatch(message, admission, snapshot);
264
+ return handled
265
+ ? Object.freeze({ matched: true, command: 'interactive', owner: requester })
266
+ : undefined;
267
+ }
268
+ export async function dispatchCanonicalEndpointEvent(input) {
269
+ const { source, context, assertActive, recordNotice, recordRequest, dispatch } = input;
270
+ if (source.payload.type !== source.name.split('.')[0]) {
271
+ throw new TypeError('Endpoint event payload type does not match its gateway event');
272
+ }
273
+ switch (source.payload.type) {
274
+ case 'notice': {
275
+ const notice = new RuntimeNotice(source.payload, context);
276
+ await recordNotice(notice);
277
+ await dispatch(notice);
278
+ break;
279
+ }
280
+ case 'request':
281
+ await withRequestActionScope(source.payload, async (reqInput) => {
282
+ const request = new RuntimeRequest(reqInput, context);
283
+ const recording = Promise.resolve(recordRequest?.(request)).catch(() => undefined);
284
+ try {
285
+ await dispatch(request);
286
+ }
287
+ finally {
288
+ await recording;
289
+ }
290
+ }, assertActive);
291
+ break;
292
+ case 'system':
293
+ await dispatch(new RuntimeSystemEvent(source.payload, context));
294
+ break;
295
+ }
296
+ }
297
+ export function interactionResult(requester) {
298
+ return Object.freeze({ matched: true, command: 'interaction', owner: requester });
299
+ }
300
+ export async function withRequestActionScope(request, dispatch, assertOperation) {
301
+ let active = true;
302
+ const actions = new Set();
303
+ const run = (action) => {
304
+ if (!active)
305
+ throw new Error('Request action port expired with its generation operation');
306
+ const operation = Promise.resolve().then(() => { assertOperation(); return action(); });
307
+ actions.add(operation);
308
+ void operation.then(() => actions.delete(operation), () => actions.delete(operation));
309
+ return operation;
310
+ };
311
+ const scoped = Object.freeze({ ...request,
312
+ $approve: async (remark) => run(() => request.$approve(remark)),
313
+ $reject: async (reason) => run(() => request.$reject(reason)),
314
+ });
315
+ try {
316
+ await dispatch(scoped);
317
+ }
318
+ finally {
319
+ active = false;
320
+ await Promise.allSettled([...actions]);
321
+ }
322
+ }
323
+ export function handlers(snapshot) {
324
+ const projection = snapshot.projections.get(handlerFeatureId);
325
+ return isHandlerIndex(projection) ? projection : undefined;
326
+ }
327
+ export function withEndpointEventPayload(source, payload) {
328
+ return Object.freeze({
329
+ name: source.name,
330
+ payload,
331
+ endpoint: source.endpoint,
332
+ client: source.client,
333
+ [operationInputBinding]: (owner, snapshot) => withEndpointEventPayload(source, bindOperationInput(payload, owner, snapshot)),
334
+ });
335
+ }
@@ -1,9 +1,20 @@
1
1
  export * from './contracts.js';
2
+ export { EndpointRuntime } from './endpoint-runtime.js';
3
+ export type { CaptureEndpointOperationInput, CapturedEndpointOperation, EndpointRuntimeDetail, EndpointRuntimeSummary, RuntimeEndpointEditInput, RuntimeEndpointMessageInput, RuntimeEndpointReactionInput, RuntimeEndpointRemoveReactionInput, RuntimeEndpointSendInput, RuntimeEndpointTypingInput, } from './endpoint-runtime.js';
4
+ export * from './inbound-spine.js';
5
+ export * from './host/index.js';
6
+ export { isMessageOrigin, roomMembershipToken } from './inbound-origin.js';
7
+ export type { MessageOrigin, RoomInboundRouteInput, RoomMembershipPort, RoomRouteDecision, } from './inbound-origin.js';
2
8
  export * from './im-runtime.js';
3
- export * from './interactive.js';
4
- export * from './login-assist-host.js';
5
- export * from './message-bus.js';
9
+ export * from './command-feedback.js';
6
10
  export * from './message-dispatcher.js';
11
+ export type { RuntimeMessageDiagnostic, RuntimeMessageDiagnosticStep, RuntimeMessageDiagnosticDelivery, RuntimeMessageDiagnosticsReader, } from './message-diagnostics.js';
12
+ export { MessageBus, messageBusToken, type EndpointEvent, type MessageBusEventMap, type MessageBusEventName, type RuntimeMessageEvent, type RuntimeMessageEventSource, type RuntimeMessageRendering, type RuntimeSceneEventSignal, type RuntimeSceneEventSource, type OutboundFallbackReason, } from './message-events.js';
7
13
  export * from './outbound-renderer.js';
8
14
  export * from './outbound-segments.js';
9
- export * from './service-tokens.js';
15
+ export { renderToSegments, isNativeMessageTag, type MessageRenderOptions } from '../../jsx/message-renderer.js';
16
+ export type { CoreReplyStream, OpenReplyStreamOptions, ReplyStreamSnapshot, ConfirmedReplyContent } from './progressive-reply.js';
17
+ export * from '../../notice.js';
18
+ export * from '../../request.js';
19
+ export * from '../../system-event.js';
20
+ export * from '../../venue/index.js';
@@ -1,9 +1,16 @@
1
1
  export * from './contracts.js';
2
+ export { EndpointRuntime } from './endpoint-runtime.js';
3
+ export * from './inbound-spine.js';
4
+ export * from './host/index.js';
5
+ export { isMessageOrigin, roomMembershipToken } from './inbound-origin.js';
2
6
  export * from './im-runtime.js';
3
- export * from './interactive.js';
4
- export * from './login-assist-host.js';
5
- export * from './message-bus.js';
7
+ export * from './command-feedback.js';
6
8
  export * from './message-dispatcher.js';
9
+ export { MessageBus, messageBusToken, } from './message-events.js';
7
10
  export * from './outbound-renderer.js';
8
11
  export * from './outbound-segments.js';
9
- export * from './service-tokens.js';
12
+ export { renderToSegments, isNativeMessageTag } from '../../jsx/message-renderer.js';
13
+ export * from '../../notice.js';
14
+ export * from '../../request.js';
15
+ export * from '../../system-event.js';
16
+ export * from '../../venue/index.js';
@@ -0,0 +1,50 @@
1
+ import { type RuntimeSnapshot, type Locale } from '@zhin.js/plugin-runtime';
2
+ import { type ConversationRef, type DeliveryReceipt, type MessageRef, type UserInteraction } from '@zhin.js/im-contract';
3
+ import { type InteractionOperationScope, type Message, type MessageSenderRef, type SendContent, type SendRequest } from './contracts.js';
4
+ import { RuntimeInteractiveRouter, type JsxButtonBatch, type PluginIncarnationRef, type RememberFallbackContext } from './interactive-router.js';
5
+ import type { Segment } from '../../built/segment-contract/types.js';
6
+ export { isPluginIncarnationAlive, type PluginIncarnationRef } from './interactive-router.js';
7
+ /** A live conversational prompt; its callbacks still require the original subject and generation. */
8
+ export interface PendingUserInteraction {
9
+ readonly id: string;
10
+ readonly generation: number;
11
+ readonly conversation: ConversationRef;
12
+ readonly subjectId: string;
13
+ readonly expiresAt: number;
14
+ readonly segments: SendContent;
15
+ }
16
+ export interface UserInteractionSource {
17
+ readonly locale?: Locale;
18
+ readonly generation?: number;
19
+ readonly conversation: ConversationRef;
20
+ readonly sender?: MessageSenderRef;
21
+ readonly $reply: (content: SendContent) => Promise<DeliveryReceipt>;
22
+ }
23
+ /** Owns one ImRuntime's conversational claims, actions, and keyboard fallback state. */
24
+ export declare class RuntimeInteractionCoordinator<Admission = never> {
25
+ #private;
26
+ /** `markPrompt` keeps a delivered prompt out of conversation context, like its answer. */
27
+ constructor(router?: RuntimeInteractiveRouter<Admission>, markPrompt?: (message: MessageRef) => void);
28
+ register(prefix: string, handler: (message: Message) => Promise<boolean> | boolean, admission?: Admission): () => void;
29
+ rememberFallback(conversation: ConversationRef, generation: number, map: Record<string, string>, ttlMsOrContext?: number | RememberFallbackContext | PluginIncarnationRef): Record<string, string>;
30
+ dispatch(message: Message, admission?: Admission, snapshot?: RuntimeSnapshot): Promise<boolean>;
31
+ prepareButtons(request: SendRequest, snapshot: RuntimeSnapshot): JsxButtonBatch;
32
+ resolveClaim(message: Message, snapshot?: RuntimeSnapshot): boolean;
33
+ /**
34
+ * Probe used by Room routing: is this message the answer to something pending on this exact
35
+ * (Endpoint-scoped) conversation? True when the sender has a pending conversational claim, or
36
+ * when the message itself resolves to a button of a live keyboard fallback. A live keyboard
37
+ * alone does not count. Changes no state.
38
+ */
39
+ hasPendingAnswer(conversation: ConversationRef, senderId: string, generation: number, input?: {
40
+ readonly content: string;
41
+ readonly segments?: readonly Segment[];
42
+ }, snapshot?: RuntimeSnapshot): boolean;
43
+ /** Read-only current prompt; never re-delivers, renews its deadline or accepts an answer. */
44
+ readPendingInteraction(conversation: ConversationRef, subjectId: string, generation: number, snapshot?: RuntimeSnapshot): PendingUserInteraction | undefined;
45
+ createForMessage(message: Message, bind?: {
46
+ readonly subjectId: string;
47
+ }): UserInteraction | undefined;
48
+ createFromUnknown(source: unknown): UserInteraction | undefined;
49
+ create(source: UserInteractionSource, subjectId?: string, operation?: InteractionOperationScope): UserInteraction;
50
+ }