@zhin.js/core 1.1.34 → 1.1.37

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 (326) hide show
  1. package/README.md +84 -142
  2. package/lib/built/ai-access.d.ts +36 -0
  3. package/lib/built/ai-access.js +46 -0
  4. package/lib/built/ai-outbound/extensions/onebot-keyboard.d.ts +2 -0
  5. package/lib/built/ai-outbound/extensions/onebot-keyboard.js +67 -0
  6. package/lib/built/ai-outbound/extensions/qq-message.d.ts +3 -0
  7. package/lib/built/ai-outbound/extensions/qq-message.js +23 -0
  8. package/lib/built/ai-outbound/index.d.ts +8 -0
  9. package/lib/built/ai-outbound/index.js +8 -0
  10. package/lib/built/ai-outbound/parse.d.ts +13 -0
  11. package/lib/built/ai-outbound/parse.js +104 -0
  12. package/lib/built/ai-outbound/plain-mention-rewrite.d.ts +6 -0
  13. package/lib/built/ai-outbound/plain-mention-rewrite.js +37 -0
  14. package/lib/built/ai-outbound/prompt.d.ts +11 -0
  15. package/lib/built/ai-outbound/prompt.js +44 -0
  16. package/lib/built/ai-outbound/resolve.d.ts +4 -0
  17. package/lib/built/ai-outbound/resolve.js +64 -0
  18. package/lib/built/ai-outbound/structured-detect.d.ts +3 -0
  19. package/lib/built/ai-outbound/structured-detect.js +6 -0
  20. package/lib/built/ai-outbound/types.d.ts +49 -0
  21. package/lib/built/ai-outbound/types.js +5 -0
  22. package/lib/built/ai-trigger.d.ts +12 -17
  23. package/lib/built/ai-trigger.js +39 -70
  24. package/lib/built/common-adapter-tools.d.ts +33 -29
  25. package/lib/built/common-adapter-tools.js +79 -67
  26. package/lib/built/generated-qrcode.d.ts +24 -0
  27. package/lib/built/generated-qrcode.js +45 -0
  28. package/lib/built/generic-segment-mapper.d.ts +4 -0
  29. package/lib/built/generic-segment-mapper.js +190 -0
  30. package/lib/built/html-segment-fallback.d.ts +2 -5
  31. package/lib/built/html-segment-fallback.js +0 -16
  32. package/lib/built/html-to-text.d.ts +0 -1
  33. package/lib/built/html-to-text.js +15 -15
  34. package/lib/built/interactive-segments/action.d.ts +17 -0
  35. package/lib/built/interactive-segments/action.js +69 -0
  36. package/lib/built/interactive-segments/button-spec.d.ts +10 -0
  37. package/lib/built/interactive-segments/button-spec.js +21 -0
  38. package/lib/built/interactive-segments/index.d.ts +6 -0
  39. package/lib/built/interactive-segments/index.js +6 -0
  40. package/lib/built/interactive-segments/keyboard-segment.d.ts +10 -0
  41. package/lib/built/interactive-segments/keyboard-segment.js +11 -0
  42. package/lib/built/interactive-segments/onebot-keyboard.d.ts +6 -0
  43. package/lib/built/interactive-segments/onebot-keyboard.js +49 -0
  44. package/lib/built/interactive-segments/resolve.d.ts +14 -0
  45. package/lib/built/interactive-segments/resolve.js +109 -0
  46. package/lib/built/interactive-segments/types.d.ts +45 -0
  47. package/lib/built/interactive-segments/types.js +9 -0
  48. package/lib/built/login-assist.d.ts +43 -16
  49. package/lib/built/login-assist.js +113 -11
  50. package/lib/built/message-enrich.d.ts +32 -0
  51. package/lib/built/message-enrich.js +67 -0
  52. package/lib/built/outbound-media-utils.d.ts +9 -0
  53. package/lib/built/outbound-media-utils.js +62 -0
  54. package/lib/built/queue-im-field-contract.d.ts +7 -2
  55. package/lib/built/queue-im-field-contract.js +13 -4
  56. package/lib/built/rich-segments/markdown-to-text.d.ts +3 -0
  57. package/lib/built/rich-segments/markdown-to-text.js +27 -0
  58. package/lib/built/roles.d.ts +4 -5
  59. package/lib/built/roles.js +4 -5
  60. package/lib/built/segment-contract/assert.d.ts +7 -0
  61. package/lib/built/segment-contract/assert.js +39 -0
  62. package/lib/built/segment-contract/delivery.d.ts +5 -0
  63. package/lib/built/segment-contract/delivery.js +40 -0
  64. package/lib/built/segment-contract/image.d.ts +5 -0
  65. package/lib/built/segment-contract/image.js +10 -0
  66. package/lib/built/segment-contract/index.d.ts +10 -0
  67. package/lib/built/segment-contract/index.js +9 -0
  68. package/lib/built/segment-contract/json-schema.d.ts +28 -0
  69. package/lib/built/segment-contract/json-schema.js +128 -0
  70. package/lib/built/segment-contract/media.d.ts +12 -0
  71. package/lib/built/segment-contract/media.js +22 -0
  72. package/lib/built/segment-contract/mention.d.ts +10 -0
  73. package/lib/built/segment-contract/mention.js +26 -0
  74. package/lib/built/segment-contract/preview.d.ts +3 -0
  75. package/lib/built/segment-contract/preview.js +159 -0
  76. package/lib/built/segment-contract/text.d.ts +7 -0
  77. package/lib/built/segment-contract/text.js +34 -0
  78. package/lib/built/segment-contract/types.d.ts +58 -0
  79. package/lib/built/segment-contract/types.js +1 -0
  80. package/lib/built/segment-contract/validate.d.ts +229 -0
  81. package/lib/built/segment-contract/validate.js +172 -0
  82. package/lib/built/tool-access.d.ts +5 -0
  83. package/lib/built/tool-access.js +17 -0
  84. package/lib/built/user-interaction.d.ts +30 -0
  85. package/lib/built/user-interaction.js +244 -0
  86. package/lib/component.d.ts +0 -1
  87. package/lib/component.js +30 -14
  88. package/lib/errors.d.ts +2 -3
  89. package/lib/errors.js +4 -5
  90. package/lib/feature/adapter.d.ts +2 -0
  91. package/lib/feature/adapter.js +2 -0
  92. package/lib/feature/command.d.ts +2 -0
  93. package/lib/feature/command.js +2 -0
  94. package/lib/feature/component.d.ts +2 -0
  95. package/lib/feature/component.js +2 -0
  96. package/lib/feature/handler.d.ts +48 -0
  97. package/lib/feature/handler.js +26 -0
  98. package/lib/feature/middleware.d.ts +2 -0
  99. package/lib/feature/middleware.js +2 -0
  100. package/lib/im-scene.d.ts +29 -0
  101. package/lib/im-scene.js +75 -0
  102. package/lib/im-session-id.d.ts +39 -0
  103. package/lib/im-session-id.js +47 -0
  104. package/lib/index.d.ts +29 -36
  105. package/lib/index.js +27 -41
  106. package/lib/jsx-dev-runtime.d.ts +0 -1
  107. package/lib/jsx-dev-runtime.js +0 -1
  108. package/lib/jsx-runtime.d.ts +1 -1
  109. package/lib/jsx-runtime.js +0 -1
  110. package/lib/jsx.d.ts +19 -22
  111. package/lib/jsx.js +0 -1
  112. package/lib/message.d.ts +17 -12
  113. package/lib/message.js +16 -13
  114. package/lib/models/system-log.d.ts +0 -3
  115. package/lib/models/system-log.js +1 -9
  116. package/lib/models/user.d.ts +0 -3
  117. package/lib/models/user.js +1 -8
  118. package/lib/notice.d.ts +16 -60
  119. package/lib/notice.js +1 -4
  120. package/lib/plugin-runtime/im/contracts.d.ts +191 -0
  121. package/lib/plugin-runtime/im/contracts.js +169 -0
  122. package/lib/plugin-runtime/im/conversation-runtime.d.ts +19 -0
  123. package/lib/plugin-runtime/im/conversation-runtime.js +312 -0
  124. package/lib/plugin-runtime/im/endpoint-runtime.d.ts +79 -0
  125. package/lib/plugin-runtime/im/endpoint-runtime.js +162 -0
  126. package/lib/plugin-runtime/im/im-runtime.d.ts +84 -0
  127. package/lib/plugin-runtime/im/im-runtime.js +201 -0
  128. package/lib/plugin-runtime/im/inbound-runtime.d.ts +31 -0
  129. package/lib/plugin-runtime/im/inbound-runtime.js +252 -0
  130. package/lib/plugin-runtime/im/index.d.ts +11 -0
  131. package/lib/plugin-runtime/im/index.js +9 -0
  132. package/lib/plugin-runtime/im/ingress-route.d.ts +10 -0
  133. package/lib/plugin-runtime/im/ingress-route.js +10 -0
  134. package/lib/plugin-runtime/im/interaction-runtime.d.ts +23 -0
  135. package/lib/plugin-runtime/im/interaction-runtime.js +181 -0
  136. package/lib/plugin-runtime/im/interactive-router.d.ts +17 -0
  137. package/lib/plugin-runtime/im/interactive-router.js +98 -0
  138. package/lib/plugin-runtime/im/login-assist-host.d.ts +6 -0
  139. package/lib/plugin-runtime/im/login-assist-host.js +6 -0
  140. package/lib/plugin-runtime/im/message-bus.d.ts +38 -0
  141. package/lib/plugin-runtime/im/message-bus.js +40 -0
  142. package/lib/plugin-runtime/im/message-dispatcher.d.ts +18 -0
  143. package/lib/plugin-runtime/im/message-dispatcher.js +107 -0
  144. package/lib/plugin-runtime/im/message-events.d.ts +26 -0
  145. package/lib/plugin-runtime/im/message-events.js +61 -0
  146. package/lib/plugin-runtime/im/outbound-delivery-runtime.d.ts +19 -0
  147. package/lib/plugin-runtime/im/outbound-delivery-runtime.js +165 -0
  148. package/lib/plugin-runtime/im/outbound-renderer.d.ts +7 -0
  149. package/lib/plugin-runtime/im/outbound-renderer.js +70 -0
  150. package/lib/plugin-runtime/im/outbound-segments.d.ts +71 -0
  151. package/lib/plugin-runtime/im/outbound-segments.js +219 -0
  152. package/lib/plugin-runtime/im/public-api.d.ts +6 -0
  153. package/lib/plugin-runtime/im/public-api.js +6 -0
  154. package/lib/plugin-runtime/im/runtime-middleware.d.ts +3 -0
  155. package/lib/plugin-runtime/im/runtime-middleware.js +13 -0
  156. package/lib/request.d.ts +17 -62
  157. package/lib/request.js +0 -4
  158. package/lib/side-event/base.d.ts +51 -0
  159. package/lib/side-event/base.js +30 -0
  160. package/lib/side-event/dispatch.d.ts +15 -0
  161. package/lib/side-event/dispatch.js +140 -0
  162. package/lib/side-event/index.d.ts +4 -0
  163. package/lib/side-event/index.js +4 -0
  164. package/lib/side-event/normalize.d.ts +38 -0
  165. package/lib/side-event/normalize.js +111 -0
  166. package/lib/side-event/types.d.ts +23 -0
  167. package/lib/side-event/types.js +55 -0
  168. package/lib/system-event.d.ts +14 -0
  169. package/lib/system-event.js +7 -0
  170. package/lib/types.d.ts +35 -193
  171. package/lib/types.js +0 -1
  172. package/lib/utils.d.ts +81 -6
  173. package/lib/utils.js +54 -25
  174. package/package.json +94 -16
  175. package/lib/adapter.d.ts +0 -96
  176. package/lib/adapter.d.ts.map +0 -1
  177. package/lib/adapter.js +0 -217
  178. package/lib/adapter.js.map +0 -1
  179. package/lib/agent-prompt.d.ts +0 -37
  180. package/lib/agent-prompt.d.ts.map +0 -1
  181. package/lib/agent-prompt.js +0 -2
  182. package/lib/agent-prompt.js.map +0 -1
  183. package/lib/bot.d.ts +0 -38
  184. package/lib/bot.d.ts.map +0 -1
  185. package/lib/bot.js +0 -2
  186. package/lib/bot.js.map +0 -1
  187. package/lib/built/adapter-process.d.ts +0 -43
  188. package/lib/built/adapter-process.d.ts.map +0 -1
  189. package/lib/built/adapter-process.js +0 -81
  190. package/lib/built/adapter-process.js.map +0 -1
  191. package/lib/built/agent-preset.d.ts +0 -65
  192. package/lib/built/agent-preset.d.ts.map +0 -1
  193. package/lib/built/agent-preset.js +0 -80
  194. package/lib/built/agent-preset.js.map +0 -1
  195. package/lib/built/ai-trigger.d.ts.map +0 -1
  196. package/lib/built/ai-trigger.js.map +0 -1
  197. package/lib/built/command.d.ts +0 -68
  198. package/lib/built/command.d.ts.map +0 -1
  199. package/lib/built/command.js +0 -92
  200. package/lib/built/command.js.map +0 -1
  201. package/lib/built/common-adapter-tools.d.ts.map +0 -1
  202. package/lib/built/common-adapter-tools.js.map +0 -1
  203. package/lib/built/component.d.ts +0 -69
  204. package/lib/built/component.d.ts.map +0 -1
  205. package/lib/built/component.js +0 -99
  206. package/lib/built/component.js.map +0 -1
  207. package/lib/built/config.d.ts +0 -99
  208. package/lib/built/config.d.ts.map +0 -1
  209. package/lib/built/config.js +0 -355
  210. package/lib/built/config.js.map +0 -1
  211. package/lib/built/cron.d.ts +0 -75
  212. package/lib/built/cron.d.ts.map +0 -1
  213. package/lib/built/cron.js +0 -122
  214. package/lib/built/cron.js.map +0 -1
  215. package/lib/built/database.d.ts +0 -66
  216. package/lib/built/database.d.ts.map +0 -1
  217. package/lib/built/database.js +0 -100
  218. package/lib/built/database.js.map +0 -1
  219. package/lib/built/dispatcher.d.ts +0 -139
  220. package/lib/built/dispatcher.d.ts.map +0 -1
  221. package/lib/built/dispatcher.js +0 -361
  222. package/lib/built/dispatcher.js.map +0 -1
  223. package/lib/built/html-segment-fallback.d.ts.map +0 -1
  224. package/lib/built/html-segment-fallback.js.map +0 -1
  225. package/lib/built/html-to-text.d.ts.map +0 -1
  226. package/lib/built/html-to-text.js.map +0 -1
  227. package/lib/built/inbound-runner.d.ts +0 -23
  228. package/lib/built/inbound-runner.d.ts.map +0 -1
  229. package/lib/built/inbound-runner.js +0 -37
  230. package/lib/built/inbound-runner.js.map +0 -1
  231. package/lib/built/login-assist.d.ts.map +0 -1
  232. package/lib/built/login-assist.js.map +0 -1
  233. package/lib/built/mcp-mesh-registrar.d.ts +0 -9
  234. package/lib/built/mcp-mesh-registrar.d.ts.map +0 -1
  235. package/lib/built/mcp-mesh-registrar.js +0 -17
  236. package/lib/built/mcp-mesh-registrar.js.map +0 -1
  237. package/lib/built/message-filter.d.ts +0 -177
  238. package/lib/built/message-filter.d.ts.map +0 -1
  239. package/lib/built/message-filter.js +0 -241
  240. package/lib/built/message-filter.js.map +0 -1
  241. package/lib/built/permission.d.ts +0 -55
  242. package/lib/built/permission.d.ts.map +0 -1
  243. package/lib/built/permission.js +0 -112
  244. package/lib/built/permission.js.map +0 -1
  245. package/lib/built/prepend-quote-context.d.ts +0 -35
  246. package/lib/built/prepend-quote-context.d.ts.map +0 -1
  247. package/lib/built/prepend-quote-context.js +0 -131
  248. package/lib/built/prepend-quote-context.js.map +0 -1
  249. package/lib/built/queue-im-field-contract.d.ts.map +0 -1
  250. package/lib/built/queue-im-field-contract.js.map +0 -1
  251. package/lib/built/roles.d.ts.map +0 -1
  252. package/lib/built/roles.js.map +0 -1
  253. package/lib/built/runtime-io.d.ts +0 -10
  254. package/lib/built/runtime-io.d.ts.map +0 -1
  255. package/lib/built/runtime-io.js +0 -68
  256. package/lib/built/runtime-io.js.map +0 -1
  257. package/lib/built/schema-feature.d.ts +0 -78
  258. package/lib/built/schema-feature.d.ts.map +0 -1
  259. package/lib/built/schema-feature.js +0 -132
  260. package/lib/built/schema-feature.js.map +0 -1
  261. package/lib/built/skill.d.ts +0 -121
  262. package/lib/built/skill.d.ts.map +0 -1
  263. package/lib/built/skill.js +0 -152
  264. package/lib/built/skill.js.map +0 -1
  265. package/lib/built/tool.d.ts +0 -150
  266. package/lib/built/tool.d.ts.map +0 -1
  267. package/lib/built/tool.js +0 -383
  268. package/lib/built/tool.js.map +0 -1
  269. package/lib/command.d.ts +0 -42
  270. package/lib/command.d.ts.map +0 -1
  271. package/lib/command.js +0 -75
  272. package/lib/command.js.map +0 -1
  273. package/lib/component.d.ts.map +0 -1
  274. package/lib/component.js.map +0 -1
  275. package/lib/errors.d.ts.map +0 -1
  276. package/lib/errors.js.map +0 -1
  277. package/lib/index.d.ts.map +0 -1
  278. package/lib/index.js.map +0 -1
  279. package/lib/jsx-dev-runtime.d.ts.map +0 -1
  280. package/lib/jsx-dev-runtime.js.map +0 -1
  281. package/lib/jsx-runtime.d.ts.map +0 -1
  282. package/lib/jsx-runtime.js.map +0 -1
  283. package/lib/jsx.d.ts.map +0 -1
  284. package/lib/jsx.js.map +0 -1
  285. package/lib/message-quote.d.ts +0 -11
  286. package/lib/message-quote.d.ts.map +0 -1
  287. package/lib/message-quote.js +0 -91
  288. package/lib/message-quote.js.map +0 -1
  289. package/lib/message.d.ts.map +0 -1
  290. package/lib/message.js.map +0 -1
  291. package/lib/models/system-log.d.ts.map +0 -1
  292. package/lib/models/system-log.js.map +0 -1
  293. package/lib/models/user.d.ts.map +0 -1
  294. package/lib/models/user.js.map +0 -1
  295. package/lib/notice.d.ts.map +0 -1
  296. package/lib/notice.js.map +0 -1
  297. package/lib/plugin.d.ts +0 -258
  298. package/lib/plugin.d.ts.map +0 -1
  299. package/lib/plugin.js +0 -688
  300. package/lib/plugin.js.map +0 -1
  301. package/lib/prompt.d.ts +0 -118
  302. package/lib/prompt.d.ts.map +0 -1
  303. package/lib/prompt.js +0 -244
  304. package/lib/prompt.js.map +0 -1
  305. package/lib/request.d.ts.map +0 -1
  306. package/lib/request.js.map +0 -1
  307. package/lib/scheduler/scheduler.d.ts +0 -49
  308. package/lib/scheduler/scheduler.d.ts.map +0 -1
  309. package/lib/scheduler/scheduler.js +0 -352
  310. package/lib/scheduler/scheduler.js.map +0 -1
  311. package/lib/scheduler/types.d.ts +0 -71
  312. package/lib/scheduler/types.d.ts.map +0 -1
  313. package/lib/scheduler/types.js +0 -8
  314. package/lib/scheduler/types.js.map +0 -1
  315. package/lib/tool-zod.d.ts +0 -28
  316. package/lib/tool-zod.d.ts.map +0 -1
  317. package/lib/tool-zod.js +0 -98
  318. package/lib/tool-zod.js.map +0 -1
  319. package/lib/types-generator.d.ts +0 -6
  320. package/lib/types-generator.d.ts.map +0 -1
  321. package/lib/types-generator.js +0 -72
  322. package/lib/types-generator.js.map +0 -1
  323. package/lib/types.d.ts.map +0 -1
  324. package/lib/types.js.map +0 -1
  325. package/lib/utils.d.ts.map +0 -1
  326. package/lib/utils.js.map +0 -1
@@ -0,0 +1,69 @@
1
+ import { ACTION_SEGMENT_TYPE, isActionSegment, } from './types.js';
2
+ export function actionSegment(data) {
3
+ return { type: ACTION_SEGMENT_TYPE, data };
4
+ }
5
+ export function getActionFromMessage(message) {
6
+ for (const item of message.$content ?? []) {
7
+ if (typeof item === 'string')
8
+ continue;
9
+ if (isActionSegment(item))
10
+ return item.data;
11
+ }
12
+ return undefined;
13
+ }
14
+ /** 入站互动 action(按钮点击等),不应计入用户发言/消息统计 */
15
+ export function isActionMessage(message) {
16
+ return getActionFromMessage(message) != null;
17
+ }
18
+ const PAYLOAD_PATTERN = /^[a-z0-9_]+:[^:\s]+:[a-z0-9_-]+$/i;
19
+ /** 去掉 @bot、XML at 段、首尾空白(QQ 指令预填文本归一化) */
20
+ export function stripInteractiveCommandText(raw) {
21
+ let text = raw.trim();
22
+ text = stripXmlAtSegments(text);
23
+ text = text.replace(/^@\S+\s+/u, '');
24
+ text = text.replace(/\s+/g, ' ').trim();
25
+ return text;
26
+ }
27
+ function stripXmlAtSegments(text) {
28
+ let out = '';
29
+ let cursor = 0;
30
+ while (cursor < text.length) {
31
+ const start = text.toLowerCase().indexOf('<at ', cursor);
32
+ if (start < 0)
33
+ break;
34
+ const end = text.indexOf('/>', start + 4);
35
+ if (end < 0)
36
+ break;
37
+ out += text.slice(cursor, start) + ' ';
38
+ cursor = end + 2;
39
+ }
40
+ return out + text.slice(cursor);
41
+ }
42
+ /** 从文本降级输入解析 payload(需在 active session 的 fallback.map 中查找) */
43
+ export function resolveTextFallbackPayload(raw, map) {
44
+ const trimmed = raw.trim();
45
+ if (map[trimmed])
46
+ return map[trimmed];
47
+ const match = trimmed.match(/^(?:ttt\s+)?(\d)$/i);
48
+ if (match && map[match[1]])
49
+ return map[match[1]];
50
+ return undefined;
51
+ }
52
+ /**
53
+ * 从用户文本入站解析 interactive payload:
54
+ * 归一化 → 直接匹配 prefix:session:id → fallback 数字映射
55
+ */
56
+ export function resolvePayloadFromText(raw, map) {
57
+ const normalized = stripInteractiveCommandText(raw);
58
+ if (!normalized)
59
+ return undefined;
60
+ if (PAYLOAD_PATTERN.test(normalized))
61
+ return normalized;
62
+ if (map && Object.keys(map).length > 0) {
63
+ return resolveTextFallbackPayload(normalized, map);
64
+ }
65
+ return undefined;
66
+ }
67
+ export function formatActionAsRaw(data) {
68
+ return data.payload;
69
+ }
@@ -0,0 +1,10 @@
1
+ import type { ButtonData } from './types.js';
2
+ /** 键盘布局单元:`segment.button()` 产物,仅用于组装 `segment.keyboard()` */
3
+ export declare class ButtonSpec {
4
+ readonly data: ButtonData;
5
+ constructor(data: ButtonData);
6
+ }
7
+ export type KeyboardRowInput = Array<ButtonSpec | ButtonData>;
8
+ export declare function isButtonLike(value: unknown): value is ButtonSpec | ButtonData;
9
+ export declare function normalizeButton(btn: ButtonSpec | ButtonData): ButtonData;
10
+ export declare function normalizeKeyboardRows(rows: KeyboardRowInput[]): ButtonData[][];
@@ -0,0 +1,21 @@
1
+ /** 键盘布局单元:`segment.button()` 产物,仅用于组装 `segment.keyboard()` */
2
+ export class ButtonSpec {
3
+ data;
4
+ constructor(data) {
5
+ this.data = data;
6
+ }
7
+ }
8
+ export function isButtonLike(value) {
9
+ if (value == null || typeof value !== 'object')
10
+ return false;
11
+ if (value instanceof ButtonSpec)
12
+ return true;
13
+ const v = value;
14
+ return typeof v.id === 'string' && typeof v.label === 'string' && typeof v.payload === 'string';
15
+ }
16
+ export function normalizeButton(btn) {
17
+ return btn instanceof ButtonSpec ? btn.data : btn;
18
+ }
19
+ export function normalizeKeyboardRows(rows) {
20
+ return rows.map((row) => row.map(normalizeButton));
21
+ }
@@ -0,0 +1,6 @@
1
+ export * from './types.js';
2
+ export * from './button-spec.js';
3
+ export * from './action.js';
4
+ export * from './resolve.js';
5
+ export { KeyboardSegment } from './keyboard-segment.js';
6
+ export * from './onebot-keyboard.js';
@@ -0,0 +1,6 @@
1
+ export * from './types.js';
2
+ export * from './button-spec.js';
3
+ export * from './action.js';
4
+ export * from './resolve.js';
5
+ export { KeyboardSegment } from './keyboard-segment.js';
6
+ export * from './onebot-keyboard.js';
@@ -0,0 +1,10 @@
1
+ import { type KeyboardSegmentData } from './types.js';
2
+ export declare class KeyboardSegment {
3
+ readonly data: KeyboardSegmentData;
4
+ readonly segmentType: "keyboard";
5
+ constructor(data: KeyboardSegmentData);
6
+ toElement(): {
7
+ type: "keyboard";
8
+ data: KeyboardSegmentData;
9
+ };
10
+ }
@@ -0,0 +1,11 @@
1
+ import { KEYBOARD_SEGMENT_TYPE } from './types.js';
2
+ export class KeyboardSegment {
3
+ data;
4
+ segmentType = KEYBOARD_SEGMENT_TYPE;
5
+ constructor(data) {
6
+ this.data = data;
7
+ }
8
+ toElement() {
9
+ return { type: this.segmentType, data: this.data };
10
+ }
11
+ }
@@ -0,0 +1,6 @@
1
+ import type { MessageSegment, SendContent } from '../../types.js';
2
+ import { type KeyboardSegmentData } from './types.js';
3
+ /** OneBot / QQ 系 keyboard 段(各 adapter 在 $sendMessage 中识别) */
4
+ export declare function keyboardToOneBotSegment(data: KeyboardSegmentData): MessageSegment;
5
+ /** 出站前将 core keyboard 段展开为 OneBot keyboard(保留前置 text 等段) */
6
+ export declare function expandKeyboardSegmentsInContent(content: SendContent): SendContent;
@@ -0,0 +1,49 @@
1
+ import { isKeyboardSegment } from './types.js';
2
+ import { KeyboardSegment } from './keyboard-segment.js';
3
+ /** OneBot / QQ 系 keyboard 段(各 adapter 在 $sendMessage 中识别) */
4
+ export function keyboardToOneBotSegment(data) {
5
+ return {
6
+ type: 'keyboard',
7
+ data: {
8
+ rows: data.rows.map((row) => row.map((btn) => ({
9
+ label: btn.label,
10
+ data: btn.payload,
11
+ enter: false,
12
+ action_type: 'callback',
13
+ disable: !!btn.disabled,
14
+ }))),
15
+ },
16
+ };
17
+ }
18
+ function asKeyboardData(item) {
19
+ if (item instanceof KeyboardSegment)
20
+ return item.data;
21
+ if (isKeyboardSegment(item))
22
+ return item.data;
23
+ return null;
24
+ }
25
+ /** 出站前将 core keyboard 段展开为 OneBot keyboard(保留前置 text 等段) */
26
+ export function expandKeyboardSegmentsInContent(content) {
27
+ const items = Array.isArray(content) ? content : [content];
28
+ const out = [];
29
+ for (const item of items) {
30
+ if (typeof item === 'string') {
31
+ out.push(item);
32
+ continue;
33
+ }
34
+ if (typeof item.type !== 'string') {
35
+ continue;
36
+ }
37
+ const data = asKeyboardData(item);
38
+ if (data) {
39
+ out.push(keyboardToOneBotSegment(data));
40
+ continue;
41
+ }
42
+ out.push(item);
43
+ }
44
+ if (out.length === 0)
45
+ return { type: 'text', data: { text: '' } };
46
+ if (out.length === 1)
47
+ return out[0];
48
+ return out;
49
+ }
@@ -0,0 +1,14 @@
1
+ import type { SendContent } from '../../types.js';
2
+ import { type InteractivePolicy, type KeyboardSegmentData } from './types.js';
3
+ /**
4
+ * keyboard 的有效 fallback 映射:显式 `fallback.map` 优先;否则按按钮顺序
5
+ * 自动编号(与 {@link renderKeyboardAsText} 的自动编号一致,含 disabled 按钮)。
6
+ */
7
+ export declare function effectiveKeyboardFallbackMap(data: KeyboardSegmentData): Record<string, string>;
8
+ /** 收集 content 中所有 keyboard 段的有效 fallback 映射(文本降级前调用)。 */
9
+ export declare function collectKeyboardFallbackMaps(content: SendContent | undefined): Record<string, string>[];
10
+ /** keyboard 段 → 编号文本('text' 策略端点的中央降级渲染)。 */
11
+ export declare function renderKeyboardAsText(data: KeyboardSegmentData): string;
12
+ export declare function hasKeyboardSegment(content: SendContent | undefined): boolean;
13
+ /** 按 Adapter interactivePolicy 将 keyboard 段转为 native 保留或文本降级 */
14
+ export declare function resolveKeyboardSegments(content: SendContent, policy: InteractivePolicy): SendContent;
@@ -0,0 +1,109 @@
1
+ import { segment } from '../../utils.js';
2
+ import { KeyboardSegment } from './keyboard-segment.js';
3
+ import { KEYBOARD_SEGMENT_TYPE, isKeyboardSegment, } from './types.js';
4
+ function asKeyboardData(item) {
5
+ if (item instanceof KeyboardSegment)
6
+ return item.data;
7
+ if (isKeyboardSegment(item))
8
+ return item.data;
9
+ return null;
10
+ }
11
+ function toKeyboardElement(data) {
12
+ return { type: KEYBOARD_SEGMENT_TYPE, data };
13
+ }
14
+ function asArray(content) {
15
+ return Array.isArray(content) ? content : [content];
16
+ }
17
+ function packSegments(out) {
18
+ if (out.length === 0)
19
+ return { type: 'text', data: { text: '' } };
20
+ if (out.length === 1)
21
+ return out[0];
22
+ return out;
23
+ }
24
+ /**
25
+ * keyboard 的有效 fallback 映射:显式 `fallback.map` 优先;否则按按钮顺序
26
+ * 自动编号(与 {@link renderKeyboardAsText} 的自动编号一致,含 disabled 按钮)。
27
+ */
28
+ export function effectiveKeyboardFallbackMap(data) {
29
+ const map = data.fallback?.map ?? {};
30
+ if (Object.keys(map).length > 0)
31
+ return { ...map };
32
+ const out = {};
33
+ data.rows.flat().forEach((btn, idx) => {
34
+ out[String(idx + 1)] = btn.payload;
35
+ });
36
+ return out;
37
+ }
38
+ /** 收集 content 中所有 keyboard 段的有效 fallback 映射(文本降级前调用)。 */
39
+ export function collectKeyboardFallbackMaps(content) {
40
+ if (content == null)
41
+ return [];
42
+ const maps = [];
43
+ for (const item of asArray(content)) {
44
+ if (typeof item === 'string')
45
+ continue;
46
+ const data = asKeyboardData(item);
47
+ if (data)
48
+ maps.push(effectiveKeyboardFallbackMap(data));
49
+ }
50
+ return maps;
51
+ }
52
+ /** keyboard 段 → 编号文本('text' 策略端点的中央降级渲染)。 */
53
+ export function renderKeyboardAsText(data) {
54
+ const lines = [];
55
+ const flat = data.rows.flat();
56
+ if (data.fallback?.hint) {
57
+ lines.push(data.fallback.hint);
58
+ }
59
+ else {
60
+ lines.push('请选择(回复数字):');
61
+ }
62
+ const map = data.fallback?.map ?? {};
63
+ const entries = Object.keys(map).length > 0
64
+ ? Object.entries(map).sort(([a], [b]) => Number(a) - Number(b))
65
+ : flat.map((btn, idx) => [String(idx + 1), btn.payload]);
66
+ for (const [key, payload] of entries) {
67
+ const btn = flat.find((b) => b.payload === payload);
68
+ const label = btn?.label ?? '·';
69
+ lines.push(`${key}. ${label}${btn?.disabled ? ' (不可用)' : ''}`);
70
+ }
71
+ return lines.join('\n');
72
+ }
73
+ export function hasKeyboardSegment(content) {
74
+ if (content == null)
75
+ return false;
76
+ return asArray(content).some((item) => {
77
+ if (typeof item === 'string')
78
+ return false;
79
+ return item instanceof KeyboardSegment || isKeyboardSegment(item);
80
+ });
81
+ }
82
+ /** 按 Adapter interactivePolicy 将 keyboard 段转为 native 保留或文本降级 */
83
+ export function resolveKeyboardSegments(content, policy) {
84
+ if (!hasKeyboardSegment(content))
85
+ return content;
86
+ const items = asArray(content);
87
+ const out = [];
88
+ for (const item of items) {
89
+ if (typeof item === 'string') {
90
+ out.push(item);
91
+ continue;
92
+ }
93
+ const data = item instanceof KeyboardSegment
94
+ ? item.data
95
+ : isKeyboardSegment(item)
96
+ ? item.data
97
+ : null;
98
+ if (!data) {
99
+ out.push(item);
100
+ continue;
101
+ }
102
+ if (policy === 'native') {
103
+ out.push(toKeyboardElement(data));
104
+ continue;
105
+ }
106
+ out.push(segment.text(renderKeyboardAsText(data)));
107
+ }
108
+ return packSegments(out);
109
+ }
@@ -0,0 +1,45 @@
1
+ import type { MessageElement } from '../../types.js';
2
+ export declare const KEYBOARD_SEGMENT_TYPE: "keyboard";
3
+ export declare const ACTION_SEGMENT_TYPE: "action";
4
+ export type ButtonStyle = 'primary' | 'danger' | 'secondary';
5
+ /** callback:平台回调 action 入站;command:QQ 指令预填后文本入站 */
6
+ export type ButtonInteractionMode = 'callback' | 'command';
7
+ export interface ButtonCommandOptions {
8
+ /** QQ action.enter — 仅单聊;预填后自动发送 */
9
+ enter?: boolean;
10
+ /** QQ action.reply — 预填时引用原消息 */
11
+ reply?: boolean;
12
+ }
13
+ export type ButtonData = {
14
+ id: string;
15
+ label: string;
16
+ payload: string;
17
+ disabled?: boolean;
18
+ style?: ButtonStyle;
19
+ /** 默认 callback */
20
+ mode?: ButtonInteractionMode;
21
+ command?: ButtonCommandOptions;
22
+ };
23
+ export type KeyboardFallback = {
24
+ hint: string;
25
+ map: Record<string, string>;
26
+ };
27
+ export type KeyboardSegmentData = {
28
+ rows: ButtonData[][];
29
+ fallback?: KeyboardFallback;
30
+ };
31
+ export type ActionSegmentData = {
32
+ id: string;
33
+ payload: string;
34
+ sourceMessageId?: string;
35
+ };
36
+ export type InteractivePolicy = 'native' | 'text';
37
+ export declare const DEFAULT_INTERACTIVE_POLICY: InteractivePolicy;
38
+ export declare function isKeyboardSegment(item: MessageElement): item is MessageElement & {
39
+ type: typeof KEYBOARD_SEGMENT_TYPE;
40
+ data: KeyboardSegmentData;
41
+ };
42
+ export declare function isActionSegment(item: MessageElement): item is MessageElement & {
43
+ type: typeof ACTION_SEGMENT_TYPE;
44
+ data: ActionSegmentData;
45
+ };
@@ -0,0 +1,9 @@
1
+ export const KEYBOARD_SEGMENT_TYPE = 'keyboard';
2
+ export const ACTION_SEGMENT_TYPE = 'action';
3
+ export const DEFAULT_INTERACTIVE_POLICY = 'text';
4
+ export function isKeyboardSegment(item) {
5
+ return item != null && typeof item === 'object' && 'type' in item && item.type === KEYBOARD_SEGMENT_TYPE;
6
+ }
7
+ export function isActionSegment(item) {
8
+ return item != null && typeof item === 'object' && 'type' in item && item.type === ACTION_SEGMENT_TYPE;
9
+ }
@@ -6,10 +6,12 @@
6
6
  * 待办未消费前会一直保留,刷新页面后仍可继续消费。
7
7
  *
8
8
  * 事件:
9
- * bot.login.pending — 有新待办时触发,payload: PendingLoginTask
9
+ * endpoint.login.pending — 有新待办时触发,payload: PendingLoginTask
10
+ * endpoint.login.expired — 超时自动取消时触发,payload: PendingLoginTask
10
11
  */
11
- import type { Plugin } from '../plugin.js';
12
- export type LoginAssistType = 'qrcode' | 'sms' | 'slider' | 'device' | 'other';
12
+ export type LoginAssistType = 'qrcode' | 'sms' | 'slider' | 'device' | 'auth' | 'other';
13
+ /** 默认 5 分钟;扫码/滑块长期挂起会泄漏 Promise 与 Map 条目 */
14
+ export declare const DEFAULT_LOGIN_ASSIST_TIMEOUT_MS: number;
13
15
  export interface PendingLoginTaskPayload {
14
16
  /** 说明文案(如「请扫码登录」) */
15
17
  message?: string;
@@ -23,20 +25,48 @@ export interface PendingLoginTaskPayload {
23
25
  export interface PendingLoginTask {
24
26
  id: string;
25
27
  adapter: string;
26
- botId: string;
28
+ endpointKey: string;
27
29
  type: LoginAssistType;
28
30
  payload: PendingLoginTaskPayload;
29
31
  createdAt: number;
32
+ /** 到期时间戳(ms);无超时则为 undefined */
33
+ expiresAt?: number;
34
+ }
35
+ export interface WaitForInputOptions {
36
+ /** Endpoint-instance owner. Only this owner may cancel the task during rollback/retirement. */
37
+ owner: object;
38
+ /**
39
+ * 超时毫秒。默认 {@link DEFAULT_LOGIN_ASSIST_TIMEOUT_MS}。
40
+ * 传 `0` / `Infinity` / 负数表示不超时(仅测试或显式长驻场景)。
41
+ */
42
+ timeoutMs?: number;
43
+ }
44
+ export type LoginAssistEventName = 'endpoint.login.pending' | 'endpoint.login.resolved' | 'endpoint.login.expired';
45
+ /** Minimal bus shared by the runtime host and lightweight test emitters. */
46
+ export interface LoginAssistBus {
47
+ emit(name: LoginAssistEventName, task: PendingLoginTask): void;
30
48
  }
31
49
  export declare class LoginAssist {
32
- private readonly plugin;
50
+ #private;
51
+ private readonly bus;
33
52
  private readonly pending;
53
+ private readonly local;
34
54
  private idSeq;
35
- constructor(plugin: Plugin);
55
+ /** 构造时默认超时;可被 waitForInput options 覆盖 */
56
+ private readonly defaultTimeoutMs;
57
+ constructor(bus?: LoginAssistBus | null, options?: {
58
+ defaultTimeoutMs?: number;
59
+ });
36
60
  /**
37
- * 生产者:等待用户输入后 resolve。会发出 bot.login.pending 事件,未消费前可被 listPending 拉取(刷新后可继续消费)。
61
+ * Subscribe without requiring a Plugin EventEmitter.
62
+ * Returns unsubscribe.
38
63
  */
39
- waitForInput(adapter: string, botId: string, type: LoginAssistType, payload?: PendingLoginTaskPayload): Promise<string | Record<string, unknown>>;
64
+ subscribe(name: LoginAssistEventName, listener: (task: PendingLoginTask) => void): () => void;
65
+ /**
66
+ * 生产者:等待用户输入后 resolve。会发出 endpoint.login.pending 事件,未消费前可被 listPending 拉取(刷新后可继续消费)。
67
+ * 超时后 reject 并 emit `endpoint.login.expired`。
68
+ */
69
+ waitForInput(adapter: string, endpointKey: string, type: LoginAssistType, payload: PendingLoginTaskPayload, options: WaitForInputOptions): Promise<string | Record<string, unknown>>;
40
70
  /**
41
71
  * 消费者:提交结果,对应 waitForInput 的 Promise 会 resolve。
42
72
  */
@@ -45,17 +75,14 @@ export declare class LoginAssist {
45
75
  * 消费者:取消某条待办,对应 Promise 会 reject。
46
76
  */
47
77
  cancel(id: string, reason?: string): boolean;
78
+ /**
79
+ * Cancel pending tasks created by one exact Endpoint instance.
80
+ */
81
+ cancelOwned(owner: object, reason?: string): number;
48
82
  /**
49
83
  * 列出未消费的待办(供 Web/CLI 展示,刷新后仍可拉取同一列表)。
50
84
  */
51
85
  listPending(): PendingLoginTask[];
86
+ hasPending(adapter?: string, endpointKey?: string): boolean;
52
87
  dispose(): void;
53
88
  }
54
- declare module '../plugin.js' {
55
- namespace Plugin {
56
- interface Contexts {
57
- loginAssist: LoginAssist;
58
- }
59
- }
60
- }
61
- //# sourceMappingURL=login-assist.d.ts.map
@@ -6,34 +6,77 @@
6
6
  * 待办未消费前会一直保留,刷新页面后仍可继续消费。
7
7
  *
8
8
  * 事件:
9
- * bot.login.pending — 有新待办时触发,payload: PendingLoginTask
9
+ * endpoint.login.pending — 有新待办时触发,payload: PendingLoginTask
10
+ * endpoint.login.expired — 超时自动取消时触发,payload: PendingLoginTask
10
11
  */
12
+ /** 默认 5 分钟;扫码/滑块长期挂起会泄漏 Promise 与 Map 条目 */
13
+ export const DEFAULT_LOGIN_ASSIST_TIMEOUT_MS = 5 * 60 * 1000;
11
14
  // ============================================================================
12
15
  // LoginAssist 服务
13
16
  // ============================================================================
14
17
  export class LoginAssist {
15
- plugin;
18
+ bus;
16
19
  pending = new Map();
20
+ local = new Map();
17
21
  idSeq = 0;
18
- constructor(plugin) {
19
- this.plugin = plugin;
22
+ /** 构造时默认超时;可被 waitForInput options 覆盖 */
23
+ defaultTimeoutMs;
24
+ constructor(bus, options) {
25
+ this.bus = bus ?? null;
26
+ this.defaultTimeoutMs = options?.defaultTimeoutMs ?? DEFAULT_LOGIN_ASSIST_TIMEOUT_MS;
20
27
  }
21
28
  /**
22
- * 生产者:等待用户输入后 resolve。会发出 bot.login.pending 事件,未消费前可被 listPending 拉取(刷新后可继续消费)。
29
+ * Subscribe without requiring a Plugin EventEmitter.
30
+ * Returns unsubscribe.
23
31
  */
24
- waitForInput(adapter, botId, type, payload = {}) {
32
+ subscribe(name, listener) {
33
+ let set = this.local.get(name);
34
+ if (!set) {
35
+ set = new Set();
36
+ this.local.set(name, set);
37
+ }
38
+ set.add(listener);
39
+ return () => {
40
+ set.delete(listener);
41
+ };
42
+ }
43
+ /**
44
+ * 生产者:等待用户输入后 resolve。会发出 endpoint.login.pending 事件,未消费前可被 listPending 拉取(刷新后可继续消费)。
45
+ * 超时后 reject 并 emit `endpoint.login.expired`。
46
+ */
47
+ waitForInput(adapter, endpointKey, type, payload, options) {
25
48
  const id = `login-${Date.now()}-${++this.idSeq}`;
49
+ const timeoutMs = resolveTimeoutMs(options?.timeoutMs, this.defaultTimeoutMs);
50
+ const createdAt = Date.now();
26
51
  const task = {
27
52
  id,
28
53
  adapter,
29
- botId,
54
+ endpointKey,
30
55
  type,
31
56
  payload: { message: payload.message ?? '', ...payload },
32
- createdAt: Date.now(),
57
+ createdAt,
58
+ ...(timeoutMs != null ? { expiresAt: createdAt + timeoutMs } : {}),
33
59
  };
34
60
  const promise = new Promise((resolve, reject) => {
35
- this.pending.set(id, { task, resolve, reject });
36
- this.plugin.emit('bot.login.pending', task);
61
+ const entry = { task, owner: options.owner, resolve, reject };
62
+ if (timeoutMs != null) {
63
+ entry.timer = setTimeout(() => {
64
+ if (!this.pending.has(id))
65
+ return;
66
+ this.pending.delete(id);
67
+ try {
68
+ this.#notify('endpoint.login.expired', task);
69
+ }
70
+ catch {
71
+ /* observer failure must not leave the producer hanging forever */
72
+ }
73
+ reject(new Error(`LoginAssist task timed out after ${timeoutMs}ms`));
74
+ }, timeoutMs);
75
+ // Node: 不阻止进程退出
76
+ entry.timer.unref?.();
77
+ }
78
+ this.pending.set(id, entry);
79
+ this.#notify('endpoint.login.pending', task);
37
80
  });
38
81
  return promise;
39
82
  }
@@ -45,7 +88,10 @@ export class LoginAssist {
45
88
  if (!entry)
46
89
  return false;
47
90
  this.pending.delete(id);
91
+ if (entry.timer)
92
+ clearTimeout(entry.timer);
48
93
  entry.resolve(value);
94
+ this.#notify('endpoint.login.resolved', entry.task);
49
95
  return true;
50
96
  }
51
97
  /**
@@ -56,20 +102,76 @@ export class LoginAssist {
56
102
  if (!entry)
57
103
  return false;
58
104
  this.pending.delete(id);
105
+ if (entry.timer)
106
+ clearTimeout(entry.timer);
59
107
  entry.reject(new Error(reason));
108
+ this.#notify('endpoint.login.resolved', entry.task);
60
109
  return true;
61
110
  }
111
+ /**
112
+ * Cancel pending tasks created by one exact Endpoint instance.
113
+ */
114
+ cancelOwned(owner, reason = 'superseded') {
115
+ let count = 0;
116
+ for (const [id, entry] of [...this.pending]) {
117
+ if (entry.owner !== owner)
118
+ continue;
119
+ this.pending.delete(id);
120
+ if (entry.timer)
121
+ clearTimeout(entry.timer);
122
+ entry.reject(new Error(reason));
123
+ this.#notify('endpoint.login.resolved', entry.task);
124
+ count += 1;
125
+ }
126
+ return count;
127
+ }
62
128
  /**
63
129
  * 列出未消费的待办(供 Web/CLI 展示,刷新后仍可拉取同一列表)。
64
130
  */
65
131
  listPending() {
66
132
  return [...this.pending.values()].map((e) => e.task);
67
133
  }
134
+ hasPending(adapter, endpointKey) {
135
+ if (adapter == null)
136
+ return this.pending.size > 0;
137
+ for (const entry of this.pending.values()) {
138
+ if (entry.task.adapter !== adapter)
139
+ continue;
140
+ if (endpointKey != null && entry.task.endpointKey !== endpointKey)
141
+ continue;
142
+ return true;
143
+ }
144
+ return false;
145
+ }
68
146
  dispose() {
69
147
  for (const [, entry] of this.pending) {
148
+ if (entry.timer)
149
+ clearTimeout(entry.timer);
70
150
  entry.reject(new Error('LoginAssist disposed'));
71
151
  }
72
152
  this.pending.clear();
153
+ this.local.clear();
154
+ }
155
+ #notify(name, task) {
156
+ try {
157
+ this.bus?.emit(name, task);
158
+ }
159
+ catch {
160
+ /* bus observers must not break producer */
161
+ }
162
+ for (const listener of this.local.get(name) ?? []) {
163
+ try {
164
+ listener(task);
165
+ }
166
+ catch {
167
+ /* same */
168
+ }
169
+ }
73
170
  }
74
171
  }
75
- //# sourceMappingURL=login-assist.js.map
172
+ function resolveTimeoutMs(explicit, fallback) {
173
+ const raw = explicit === undefined ? fallback : explicit;
174
+ if (!Number.isFinite(raw) || raw <= 0)
175
+ return undefined;
176
+ return raw;
177
+ }