@zhin.js/core 1.5.16 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (334) hide show
  1. package/README.md +78 -195
  2. package/lib/ai-completion-contract.d.ts +135 -0
  3. package/lib/ai-completion-contract.js +69 -0
  4. package/lib/built/ai-access.d.ts +2 -21
  5. package/lib/built/ai-access.js +1 -46
  6. package/lib/built/ai-outbound/index.d.ts +0 -2
  7. package/lib/built/ai-outbound/index.js +0 -2
  8. package/lib/built/ai-outbound/prompt.d.ts +2 -0
  9. package/lib/built/ai-outbound/prompt.js +2 -0
  10. package/lib/built/ai-outbound/types.d.ts +1 -10
  11. package/lib/built/ai-trigger.d.ts +39 -78
  12. package/lib/built/ai-trigger.js +3 -254
  13. package/lib/built/common-adapter-tools.d.ts +21 -6
  14. package/lib/built/common-adapter-tools.js +28 -17
  15. package/lib/built/generic-segment-mapper.js +4 -1
  16. package/lib/built/html-segment-fallback.d.ts +6 -7
  17. package/lib/built/html-segment-fallback.js +1 -16
  18. package/lib/built/html-to-text.d.ts +0 -3
  19. package/lib/built/html-to-text.js +131 -40
  20. package/lib/built/interaction-messages.d.ts +1 -0
  21. package/lib/built/interaction-messages.js +30 -0
  22. package/lib/built/interactive-segments/action.d.ts +3 -4
  23. package/lib/built/interactive-segments/action.js +2 -5
  24. package/lib/built/interactive-segments/index.d.ts +0 -2
  25. package/lib/built/interactive-segments/index.js +0 -2
  26. package/lib/built/interactive-segments/keyboard-segment.d.ts +0 -1
  27. package/lib/built/interactive-segments/keyboard-segment.js +0 -1
  28. package/lib/built/interactive-segments/onebot-keyboard.d.ts +0 -1
  29. package/lib/built/interactive-segments/onebot-keyboard.js +0 -2
  30. package/lib/built/interactive-segments/resolve.d.ts +2 -3
  31. package/lib/built/interactive-segments/resolve.js +21 -14
  32. package/lib/built/interactive-segments/types.d.ts +2 -7
  33. package/lib/built/interactive-segments/types.js +22 -2
  34. package/lib/built/login-assist.d.ts +330 -25
  35. package/lib/built/login-assist.js +1156 -75
  36. package/lib/built/login-captcha.d.ts +62 -0
  37. package/lib/built/login-captcha.js +107 -0
  38. package/lib/built/message-enrich.d.ts +24 -27
  39. package/lib/built/message-enrich.js +53 -49
  40. package/lib/built/outbound-content-arrays.d.ts +4 -0
  41. package/lib/built/outbound-content-arrays.js +32 -0
  42. package/lib/built/outbound-media-trust.d.ts +46 -0
  43. package/lib/built/outbound-media-trust.js +245 -0
  44. package/lib/built/outbound-media-utils.d.ts +5 -3
  45. package/lib/built/outbound-media-utils.js +5 -3
  46. package/lib/built/readable-link.d.ts +4 -0
  47. package/lib/built/readable-link.js +32 -0
  48. package/lib/built/rich-segments/markdown-to-text.d.ts +1 -2
  49. package/lib/built/rich-segments/markdown-to-text.js +141 -17
  50. package/lib/built/roles.d.ts +141 -12
  51. package/lib/built/roles.js +253 -52
  52. package/lib/built/segment-contract/assert.js +8 -2
  53. package/lib/built/segment-contract/delivery.js +1 -0
  54. package/lib/built/segment-contract/index.d.ts +3 -3
  55. package/lib/built/segment-contract/index.js +2 -2
  56. package/lib/built/segment-contract/json-schema.d.ts +39 -5
  57. package/lib/built/segment-contract/json-schema.js +207 -24
  58. package/lib/built/segment-contract/preview.js +8 -1
  59. package/lib/built/segment-contract/text.js +1 -1
  60. package/lib/built/segment-contract/types.d.ts +2 -2
  61. package/lib/built/segment-contract/validate.d.ts +1553 -224
  62. package/lib/built/segment-contract/validate.js +39 -3
  63. package/lib/built/tool-access.d.ts +5 -0
  64. package/lib/built/tool-access.js +17 -0
  65. package/lib/built/user-interaction.d.ts +5 -4
  66. package/lib/built/user-interaction.js +64 -52
  67. package/lib/errors.d.ts +1 -18
  68. package/lib/errors.js +1 -26
  69. package/lib/feature/command-public-api.d.ts +5 -0
  70. package/lib/feature/command-public-api.js +5 -0
  71. package/lib/feature/command.d.ts +2 -0
  72. package/lib/feature/command.js +1 -0
  73. package/lib/feature/component.d.ts +1 -0
  74. package/lib/feature/component.js +1 -0
  75. package/lib/feature/handler.d.ts +13 -7
  76. package/lib/feature/handler.js +2 -1
  77. package/lib/feature/middleware-public-api.d.ts +6 -0
  78. package/lib/feature/middleware-public-api.js +6 -0
  79. package/lib/feature/middleware.d.ts +22 -2
  80. package/lib/feature/middleware.js +7 -2
  81. package/lib/im-scene.d.ts +4 -3
  82. package/lib/im-scene.js +9 -9
  83. package/lib/im-session-id.d.ts +20 -12
  84. package/lib/im-session-id.js +8 -8
  85. package/lib/index.d.ts +20 -48
  86. package/lib/index.js +15 -41
  87. package/lib/jsx/intrinsic-elements.d.ts +83 -0
  88. package/lib/jsx/message-renderer.d.ts +13 -0
  89. package/lib/jsx/message-renderer.js +128 -0
  90. package/lib/jsx/message-tags.d.ts +133 -0
  91. package/lib/jsx/message-tags.js +50 -0
  92. package/lib/jsx-dev-runtime.d.ts +2 -2
  93. package/lib/jsx-dev-runtime.js +1 -2
  94. package/lib/jsx-runtime.d.ts +2 -12
  95. package/lib/jsx-runtime.js +1 -10
  96. package/lib/jsx.d.ts +6 -30
  97. package/lib/jsx.js +3 -93
  98. package/lib/message.d.ts +0 -40
  99. package/lib/message.js +1 -27
  100. package/lib/models/system-log.d.ts +0 -2
  101. package/lib/models/system-log.js +1 -8
  102. package/lib/models/user.d.ts +0 -2
  103. package/lib/models/user.js +1 -7
  104. package/lib/notice.d.ts +38 -33
  105. package/lib/notice.js +23 -7
  106. package/lib/plugin-runtime/im/command-feedback.d.ts +59 -0
  107. package/lib/plugin-runtime/im/command-feedback.js +148 -0
  108. package/lib/plugin-runtime/im/contracts.d.ts +217 -32
  109. package/lib/plugin-runtime/im/contracts.js +141 -23
  110. package/lib/plugin-runtime/im/conversation-runtime.d.ts +42 -0
  111. package/lib/plugin-runtime/im/conversation-runtime.js +451 -0
  112. package/lib/plugin-runtime/im/endpoint-runtime.d.ts +112 -0
  113. package/lib/plugin-runtime/im/endpoint-runtime.js +434 -0
  114. package/lib/plugin-runtime/im/feature-gate.d.ts +21 -0
  115. package/lib/plugin-runtime/im/feature-gate.js +88 -0
  116. package/lib/plugin-runtime/im/host/component-host-installer.d.ts +2 -0
  117. package/lib/plugin-runtime/im/host/component-host-installer.js +14 -0
  118. package/lib/plugin-runtime/im/host/conversation-migration.d.ts +5 -0
  119. package/lib/plugin-runtime/im/host/conversation-migration.js +75 -0
  120. package/lib/plugin-runtime/im/host/database-config.d.ts +10 -0
  121. package/lib/plugin-runtime/im/host/database-config.js +152 -0
  122. package/lib/plugin-runtime/im/host/database-console-schema.d.ts +79 -0
  123. package/lib/plugin-runtime/im/host/database-console-schema.js +477 -0
  124. package/lib/plugin-runtime/im/host/database-console-sql.d.ts +66 -0
  125. package/lib/plugin-runtime/im/host/database-console-sql.js +459 -0
  126. package/lib/plugin-runtime/im/host/database-host-installer.d.ts +60 -0
  127. package/lib/plugin-runtime/im/host/database-host-installer.js +1166 -0
  128. package/lib/plugin-runtime/im/host/database-state.d.ts +9 -0
  129. package/lib/plugin-runtime/im/host/database-state.js +91 -0
  130. package/lib/plugin-runtime/im/host/index.d.ts +8 -0
  131. package/lib/plugin-runtime/im/host/index.js +8 -0
  132. package/lib/plugin-runtime/im/host/locale-host-installer.d.ts +3 -0
  133. package/lib/plugin-runtime/im/host/locale-host-installer.js +60 -0
  134. package/lib/plugin-runtime/im/host/outbound-host-installer.d.ts +8 -0
  135. package/lib/plugin-runtime/im/host/outbound-host-installer.js +153 -0
  136. package/lib/plugin-runtime/im/host/sender-enrichment.d.ts +34 -0
  137. package/lib/plugin-runtime/im/host/sender-enrichment.js +82 -0
  138. package/lib/plugin-runtime/im/im-runtime.d.ts +40 -111
  139. package/lib/plugin-runtime/im/im-runtime.js +235 -1059
  140. package/lib/plugin-runtime/im/inbound-dedup.d.ts +84 -0
  141. package/lib/plugin-runtime/im/inbound-dedup.js +322 -0
  142. package/lib/plugin-runtime/im/inbound-origin.d.ts +150 -0
  143. package/lib/plugin-runtime/im/inbound-origin.js +183 -0
  144. package/lib/plugin-runtime/im/inbound-runtime.d.ts +59 -0
  145. package/lib/plugin-runtime/im/inbound-runtime.js +478 -0
  146. package/lib/plugin-runtime/im/inbound-spine.d.ts +196 -0
  147. package/lib/plugin-runtime/im/inbound-spine.js +335 -0
  148. package/lib/plugin-runtime/im/index.d.ts +15 -4
  149. package/lib/plugin-runtime/im/index.js +11 -4
  150. package/lib/plugin-runtime/im/interaction-runtime.d.ts +50 -0
  151. package/lib/plugin-runtime/im/interaction-runtime.js +390 -0
  152. package/lib/plugin-runtime/im/interactive-router.d.ts +81 -0
  153. package/lib/plugin-runtime/im/interactive-router.js +382 -0
  154. package/lib/plugin-runtime/im/message-diagnostics.d.ts +70 -0
  155. package/lib/plugin-runtime/im/message-diagnostics.js +182 -0
  156. package/lib/plugin-runtime/im/message-dispatcher.d.ts +18 -4
  157. package/lib/plugin-runtime/im/message-dispatcher.js +138 -20
  158. package/lib/plugin-runtime/im/message-events.d.ts +85 -0
  159. package/lib/plugin-runtime/im/message-events.js +113 -0
  160. package/lib/plugin-runtime/im/outbound-delivery-runtime.d.ts +39 -0
  161. package/lib/plugin-runtime/im/outbound-delivery-runtime.js +450 -0
  162. package/lib/plugin-runtime/im/outbound-renderer.d.ts +52 -3
  163. package/lib/plugin-runtime/im/outbound-renderer.js +188 -8
  164. package/lib/plugin-runtime/im/outbound-segments.d.ts +29 -5
  165. package/lib/plugin-runtime/im/outbound-segments.js +130 -36
  166. package/lib/plugin-runtime/im/progressive-reply.d.ts +58 -0
  167. package/lib/plugin-runtime/im/progressive-reply.js +501 -0
  168. package/lib/plugin-runtime/im/public-api.d.ts +1 -1
  169. package/lib/request.d.ts +27 -36
  170. package/lib/request.js +14 -6
  171. package/lib/side-event/base.d.ts +64 -44
  172. package/lib/side-event/base.js +72 -19
  173. package/lib/side-event/index.d.ts +1 -1
  174. package/lib/side-event/index.js +1 -1
  175. package/lib/side-event/normalize.d.ts +7 -38
  176. package/lib/side-event/normalize.js +4 -109
  177. package/lib/side-event/scene-events.d.ts +40 -0
  178. package/lib/side-event/scene-events.js +147 -0
  179. package/lib/side-event/types.d.ts +4 -13
  180. package/lib/side-event/types.js +1 -55
  181. package/lib/system-event.d.ts +17 -12
  182. package/lib/system-event.js +8 -6
  183. package/lib/types.d.ts +23 -167
  184. package/lib/utils.d.ts +50 -33
  185. package/lib/utils.js +12 -185
  186. package/lib/venue/contracts.d.ts +21 -0
  187. package/lib/venue/contracts.js +3 -0
  188. package/lib/venue/floor-controller.d.ts +11 -0
  189. package/lib/venue/floor-controller.js +63 -0
  190. package/lib/venue/index.d.ts +13 -0
  191. package/lib/venue/index.js +11 -0
  192. package/lib/venue/membership-port.d.ts +23 -0
  193. package/lib/venue/membership-port.js +141 -0
  194. package/lib/venue/projector.d.ts +38 -0
  195. package/lib/venue/projector.js +45 -0
  196. package/lib/venue/registry.d.ts +16 -0
  197. package/lib/venue/registry.js +63 -0
  198. package/lib/venue/router.d.ts +19 -0
  199. package/lib/venue/router.js +101 -0
  200. package/lib/venue/send-limiter.d.ts +17 -0
  201. package/lib/venue/send-limiter.js +73 -0
  202. package/lib/venue/validator.d.ts +6 -0
  203. package/lib/venue/validator.js +160 -0
  204. package/package.json +24 -50
  205. package/lib/adapter.d.ts +0 -145
  206. package/lib/adapter.js +0 -377
  207. package/lib/built/adapter-process.d.ts +0 -58
  208. package/lib/built/adapter-process.js +0 -128
  209. package/lib/built/agent-preset.d.ts +0 -64
  210. package/lib/built/agent-preset.js +0 -79
  211. package/lib/built/ai-outbound/adapter-access.d.ts +0 -3
  212. package/lib/built/ai-outbound/adapter-access.js +0 -9
  213. package/lib/built/ai-outbound/extensions/qq-message.d.ts +0 -3
  214. package/lib/built/ai-outbound/extensions/qq-message.js +0 -23
  215. package/lib/built/authorization.d.ts +0 -10
  216. package/lib/built/authorization.js +0 -76
  217. package/lib/built/command.d.ts +0 -72
  218. package/lib/built/command.js +0 -105
  219. package/lib/built/component.d.ts +0 -67
  220. package/lib/built/component.js +0 -97
  221. package/lib/built/config.d.ts +0 -98
  222. package/lib/built/config.js +0 -372
  223. package/lib/built/connect-endpoint-instance.d.ts +0 -17
  224. package/lib/built/connect-endpoint-instance.js +0 -42
  225. package/lib/built/database.d.ts +0 -68
  226. package/lib/built/database.js +0 -106
  227. package/lib/built/dispatcher.d.ts +0 -141
  228. package/lib/built/dispatcher.js +0 -383
  229. package/lib/built/endpoint-commands.d.ts +0 -3
  230. package/lib/built/endpoint-commands.js +0 -99
  231. package/lib/built/endpoint-lifecycle-service.d.ts +0 -28
  232. package/lib/built/endpoint-lifecycle-service.js +0 -242
  233. package/lib/built/endpoint-lifecycle.d.ts +0 -27
  234. package/lib/built/endpoint-lifecycle.js +0 -36
  235. package/lib/built/endpoint-manager.d.ts +0 -34
  236. package/lib/built/html-renderer-loader.d.ts +0 -13
  237. package/lib/built/html-renderer-loader.js +0 -33
  238. package/lib/built/inbound-pipeline.d.ts +0 -37
  239. package/lib/built/inbound-pipeline.js +0 -63
  240. package/lib/built/inbound-runner.d.ts +0 -22
  241. package/lib/built/inbound-runner.js +0 -34
  242. package/lib/built/interactive-segment-contract.d.ts +0 -6
  243. package/lib/built/interactive-segment-contract.js +0 -9
  244. package/lib/built/interactive-segments/fallback-store.d.ts +0 -29
  245. package/lib/built/interactive-segments/fallback-store.js +0 -63
  246. package/lib/built/interactive-segments/handlers.d.ts +0 -16
  247. package/lib/built/interactive-segments/handlers.js +0 -74
  248. package/lib/built/introspection-format.d.ts +0 -10
  249. package/lib/built/introspection-format.js +0 -28
  250. package/lib/built/management-command-guard.d.ts +0 -15
  251. package/lib/built/management-command-guard.js +0 -25
  252. package/lib/built/message-filter.d.ts +0 -176
  253. package/lib/built/message-filter.js +0 -239
  254. package/lib/built/optional-peer-import.d.ts +0 -3
  255. package/lib/built/optional-peer-import.js +0 -33
  256. package/lib/built/outbound-media-contract.d.ts +0 -22
  257. package/lib/built/outbound-media-contract.js +0 -10
  258. package/lib/built/permission.d.ts +0 -51
  259. package/lib/built/permission.js +0 -99
  260. package/lib/built/permit-check.d.ts +0 -7
  261. package/lib/built/permit-check.js +0 -35
  262. package/lib/built/permit-parse.d.ts +0 -16
  263. package/lib/built/permit-parse.js +0 -32
  264. package/lib/built/platform-permit.d.ts +0 -20
  265. package/lib/built/platform-permit.js +0 -91
  266. package/lib/built/queue-im-field-contract.d.ts +0 -32
  267. package/lib/built/queue-im-field-contract.js +0 -67
  268. package/lib/built/rich-segments/adapter-policies.d.ts +0 -9
  269. package/lib/built/rich-segments/adapter-policies.js +0 -27
  270. package/lib/built/rich-segments/base.d.ts +0 -11
  271. package/lib/built/rich-segments/base.js +0 -24
  272. package/lib/built/rich-segments/builtins.d.ts +0 -4
  273. package/lib/built/rich-segments/builtins.js +0 -40
  274. package/lib/built/rich-segments/capabilities.d.ts +0 -6
  275. package/lib/built/rich-segments/capabilities.js +0 -38
  276. package/lib/built/rich-segments/html-segment.d.ts +0 -15
  277. package/lib/built/rich-segments/html-segment.js +0 -62
  278. package/lib/built/rich-segments/index.d.ts +0 -13
  279. package/lib/built/rich-segments/index.js +0 -13
  280. package/lib/built/rich-segments/markdown-segment.d.ts +0 -14
  281. package/lib/built/rich-segments/markdown-segment.js +0 -58
  282. package/lib/built/rich-segments/qrcode-segment.d.ts +0 -18
  283. package/lib/built/rich-segments/qrcode-segment.js +0 -73
  284. package/lib/built/rich-segments/registry.d.ts +0 -16
  285. package/lib/built/rich-segments/registry.js +0 -52
  286. package/lib/built/rich-segments/resolve.d.ts +0 -5
  287. package/lib/built/rich-segments/resolve.js +0 -87
  288. package/lib/built/rich-segments/tts-segment.d.ts +0 -13
  289. package/lib/built/rich-segments/tts-segment.js +0 -51
  290. package/lib/built/rich-segments/types.d.ts +0 -79
  291. package/lib/built/rich-segments/types.js +0 -17
  292. package/lib/built/runtime-io.d.ts +0 -9
  293. package/lib/built/runtime-io.js +0 -67
  294. package/lib/built/schedule.d.ts +0 -54
  295. package/lib/built/schedule.js +0 -112
  296. package/lib/built/schema-endpoint-manager.d.ts +0 -17
  297. package/lib/built/schema-endpoint-manager.js +0 -73
  298. package/lib/built/schema-feature.d.ts +0 -77
  299. package/lib/built/schema-feature.js +0 -130
  300. package/lib/built/skill.d.ts +0 -120
  301. package/lib/built/skill.js +0 -151
  302. package/lib/built/speech-loader.d.ts +0 -14
  303. package/lib/built/speech-loader.js +0 -32
  304. package/lib/built/tool.d.ts +0 -151
  305. package/lib/built/tool.js +0 -388
  306. package/lib/command.d.ts +0 -50
  307. package/lib/command.js +0 -106
  308. package/lib/component.d.ts +0 -26
  309. package/lib/component.js +0 -583
  310. package/lib/endpoint-capabilities.d.ts +0 -46
  311. package/lib/endpoint-capabilities.js +0 -55
  312. package/lib/endpoint.d.ts +0 -19
  313. package/lib/endpoint.js +0 -3
  314. package/lib/host-plugin-registry.d.ts +0 -8
  315. package/lib/host-plugin-registry.js +0 -12
  316. package/lib/plugin-context.d.ts +0 -27
  317. package/lib/plugin-context.js +0 -64
  318. package/lib/plugin-runtime/im/interactive.d.ts +0 -26
  319. package/lib/plugin-runtime/im/interactive.js +0 -46
  320. package/lib/plugin-runtime/im/login-assist-host.d.ts +0 -6
  321. package/lib/plugin-runtime/im/login-assist-host.js +0 -6
  322. package/lib/plugin-runtime/im/message-bus.d.ts +0 -38
  323. package/lib/plugin-runtime/im/message-bus.js +0 -40
  324. package/lib/plugin-runtime/im/service-tokens.d.ts +0 -18
  325. package/lib/plugin-runtime/im/service-tokens.js +0 -13
  326. package/lib/plugin.d.ts +0 -268
  327. package/lib/plugin.js +0 -583
  328. package/lib/schema-interaction.d.ts +0 -117
  329. package/lib/schema-interaction.js +0 -247
  330. package/lib/side-event/dispatch.d.ts +0 -15
  331. package/lib/side-event/dispatch.js +0 -140
  332. package/lib/tool-zod.d.ts +0 -43
  333. package/lib/tool-zod.js +0 -184
  334. /package/lib/{built/endpoint-manager.js → jsx/intrinsic-elements.js} +0 -0
@@ -0,0 +1,152 @@
1
+ const SUPPORTED_DIALECTS = new Set(['memory', 'mongodb', 'mysql', 'pg', 'redis', 'sqlite']);
2
+ function configurationError(path, expectation, envExample) {
3
+ const envHint = envExample
4
+ ? `若这里引用环境变量,请在项目根目录 .env 中设置 ${envExample}。`
5
+ : '若这里引用环境变量,请检查项目根目录 .env 中的对应变量。';
6
+ return new TypeError(`数据库配置 ${path} ${expectation}。${envHint}也可运行 \`zhin setup --database\` 重新配置。`);
7
+ }
8
+ function normalizePort(value, path, envExample) {
9
+ if (value === undefined)
10
+ return undefined;
11
+ const normalized = typeof value === 'number'
12
+ ? value
13
+ : typeof value === 'string' && /^\d+$/u.test(value.trim())
14
+ ? Number(value.trim())
15
+ : Number.NaN;
16
+ if (!Number.isInteger(normalized) || normalized < 1 || normalized > 65535) {
17
+ throw configurationError(path, '必须是 1-65535 的整数,当前值为空或无效', envExample);
18
+ }
19
+ return normalized;
20
+ }
21
+ function normalizeOptionalText(config, key, path, envExample) {
22
+ if (!(key in config))
23
+ return;
24
+ const value = config[key];
25
+ if (typeof value !== 'string' || value.trim() === '') {
26
+ throw configurationError(path, '不能为空', envExample);
27
+ }
28
+ config[key] = value.trim();
29
+ }
30
+ function normalizeRequiredText(config, key, path, envExample) {
31
+ if (!(key in config))
32
+ config[key] = undefined;
33
+ normalizeOptionalText(config, key, path, envExample);
34
+ }
35
+ function normalizeRedisDatabase(value) {
36
+ if (value === undefined)
37
+ return undefined;
38
+ const normalized = typeof value === 'number'
39
+ ? value
40
+ : typeof value === 'string' && /^\d+$/u.test(value.trim())
41
+ ? Number(value.trim())
42
+ : Number.NaN;
43
+ if (!Number.isInteger(normalized) || normalized < 0) {
44
+ throw configurationError('database.database', '必须是大于或等于 0 的整数', 'REDIS_DB=0');
45
+ }
46
+ return normalized;
47
+ }
48
+ /**
49
+ * `database.console` (ADR 0059 §4): the restricted account raw Console SQL runs on, PostgreSQL /
50
+ * MySQL only. Both fields required; the password belongs in `.env`
51
+ * (`password: ${ZHIN_DB_CONSOLE_PASSWORD}`). Other dialects need none and ignore the block.
52
+ */
53
+ function normalizeConsoleCredential(config) {
54
+ if (!('console' in config) || config.console === undefined)
55
+ return;
56
+ const value = config.console;
57
+ if (!value || typeof value !== 'object' || Array.isArray(value))
58
+ throw configurationError('database.console', '必须是对象 { user, password }');
59
+ const block = { ...value };
60
+ // `${ZHIN_DB_CONSOLE_*}` not set (yet): no restricted account — raw SQL stays off, the rest works.
61
+ if ((block.user === undefined || block.user === '') && (block.password === undefined || block.password === '')) {
62
+ delete config.console;
63
+ return;
64
+ }
65
+ normalizeRequiredText(block, 'user', 'database.console.user', 'ZHIN_DB_CONSOLE_USER=zhin_console_xxxxxxxx');
66
+ if (typeof block.password !== 'string' || block.password === '') {
67
+ throw configurationError('database.console.password', '不能为空', 'ZHIN_DB_CONSOLE_PASSWORD=…(zhin db console-role 生成)');
68
+ }
69
+ if (String(block.user) === String(config.user ?? '')) {
70
+ throw configurationError('database.console.user', '不能与主账号 database.user 相同(原始 SQL 只在受限账号上执行)');
71
+ }
72
+ for (const key of Object.keys(block)) {
73
+ if (key !== 'user' && key !== 'password')
74
+ throw configurationError(`database.console.${key}`, '不支持(只有 user 与 password)');
75
+ }
76
+ config.console = Object.freeze({ user: block.user, password: block.password });
77
+ }
78
+ /**
79
+ * Validate and normalize the expanded Root database configuration before a
80
+ * third-party driver sees it. Dotenv expansion produces strings, while the
81
+ * network clients require numeric ports and otherwise surface opaque NaN
82
+ * socket errors.
83
+ */
84
+ export function normalizeDatabaseHostConfig(input) {
85
+ const dialect = input.dialect;
86
+ if (!SUPPORTED_DIALECTS.has(dialect)) {
87
+ throw new TypeError(`数据库配置 database.dialect 不支持 ${JSON.stringify(dialect)};`
88
+ + '可选值为 sqlite、mysql、pg、mongodb、redis、memory。');
89
+ }
90
+ const config = { ...input, dialect };
91
+ // Only PostgreSQL / MySQL use a restricted Console account.
92
+ if (dialect !== 'pg' && dialect !== 'mysql')
93
+ delete config.console;
94
+ switch (dialect) {
95
+ case 'mysql':
96
+ normalizeOptionalText(config, 'host', 'database.host', 'DB_HOST=127.0.0.1');
97
+ normalizeOptionalText(config, 'user', 'database.user', 'DB_USER=root');
98
+ normalizeOptionalText(config, 'database', 'database.database', 'DB_DATABASE=zhin_bot');
99
+ normalizeOptionalText(config, 'uri', 'database.uri');
100
+ config.port = normalizePort(config.port, 'database.port', 'DB_PORT=3306');
101
+ if (config.port === undefined)
102
+ delete config.port;
103
+ normalizeConsoleCredential(config);
104
+ break;
105
+ case 'pg':
106
+ normalizeOptionalText(config, 'host', 'database.host', 'DB_HOST=127.0.0.1');
107
+ normalizeOptionalText(config, 'user', 'database.user', 'DB_USER=postgres');
108
+ normalizeOptionalText(config, 'database', 'database.database', 'DB_DATABASE=zhin_bot');
109
+ normalizeOptionalText(config, 'connectionString', 'database.connectionString');
110
+ config.port = normalizePort(config.port, 'database.port', 'DB_PORT=5432');
111
+ if (config.port === undefined)
112
+ delete config.port;
113
+ normalizeConsoleCredential(config);
114
+ break;
115
+ case 'mongodb':
116
+ normalizeRequiredText(config, 'url', 'database.url', 'DB_URL=mongodb://127.0.0.1:27017');
117
+ normalizeRequiredText(config, 'dbName', 'database.dbName', 'DB_NAME=zhin_bot');
118
+ if (!/^mongodb(?:\+srv)?:\/\//u.test(String(config.url))) {
119
+ throw configurationError('database.url', '必须使用 mongodb:// 或 mongodb+srv://');
120
+ }
121
+ break;
122
+ case 'redis': {
123
+ normalizeOptionalText(config, 'url', 'database.url', 'REDIS_URL=redis://127.0.0.1:6379');
124
+ if (config.socket !== undefined) {
125
+ if (!config.socket || typeof config.socket !== 'object' || Array.isArray(config.socket)) {
126
+ throw configurationError('database.socket', '必须是对象');
127
+ }
128
+ const socket = { ...config.socket };
129
+ normalizeOptionalText(socket, 'host', 'database.socket.host', 'REDIS_HOST=127.0.0.1');
130
+ socket.port = normalizePort(socket.port, 'database.socket.port', 'REDIS_PORT=6379');
131
+ if (socket.port === undefined)
132
+ delete socket.port;
133
+ config.socket = Object.freeze(socket);
134
+ }
135
+ config.database = normalizeRedisDatabase(config.database);
136
+ if (config.database === undefined)
137
+ delete config.database;
138
+ break;
139
+ }
140
+ case 'sqlite':
141
+ if (config.filename === undefined)
142
+ config.filename = './data/bot.db';
143
+ normalizeOptionalText(config, 'filename', 'database.filename');
144
+ // ADR 0059 §2: the instance's connection never attaches another file (.zhin/gateway.db);
145
+ // not configurable.
146
+ config.denyAttach = true;
147
+ break;
148
+ case 'memory':
149
+ break;
150
+ }
151
+ return Object.freeze(config);
152
+ }
@@ -0,0 +1,79 @@
1
+ /**
2
+ * Console schema editor (`db:schema:*`): validates a structured change and renders the DDL the
3
+ * Host will run, so the Console previews exactly what is applied. Identifiers of new objects are
4
+ * restricted to `[A-Za-z_][A-Za-z0-9_]*`; existing names are quoted with their quotes escaped.
5
+ */
6
+ import type { DatabaseHostSchemaPlan, DatabaseHostTableSchema } from '@zhin.js/plugin-runtime';
7
+ import { type ConsoleSqlDialect } from './database-console-sql.js';
8
+ export declare class ConsoleSchemaRefusal extends Error {
9
+ readonly code: 'invalid' | 'unsupported' | 'protected_table' | 'not_found' | 'confirm_required';
10
+ constructor(code: 'invalid' | 'unsupported' | 'protected_table' | 'not_found' | 'confirm_required', message: string);
11
+ }
12
+ /** The subset of a `@zhin.js/database` dialect the renderer needs. */
13
+ export interface SchemaDialect {
14
+ readonly name: string;
15
+ mapColumnType(type: string): string;
16
+ formatDefaultValue(value: unknown): string;
17
+ /** The dialect's own column DDL (`@zhin.js/database`): text keys, `(expr)` defaults, identity. */
18
+ formatColumnDefinition?(field: string, column: Record<string, unknown>): string;
19
+ }
20
+ /** A MySQL column as `information_schema.columns` describes it (for `MODIFY COLUMN`). */
21
+ export interface MysqlColumnDefinition {
22
+ readonly name: string;
23
+ /** Full type, e.g. `varchar(64)`, `int unsigned`, `enum('a','b')`. */
24
+ readonly columnType: string;
25
+ readonly dataType: string;
26
+ readonly nullable: boolean;
27
+ /** `COLUMN_DEFAULT`: the literal value, or the expression when `extra` has `DEFAULT_GENERATED`. */
28
+ readonly defaultValue: string | null;
29
+ /** `EXTRA`: `auto_increment`, `on update CURRENT_TIMESTAMP`, `DEFAULT_GENERATED`, `VIRTUAL GENERATED`, `INVISIBLE`, … */
30
+ readonly extra: string;
31
+ readonly comment: string;
32
+ readonly characterSet: string | null;
33
+ readonly collation: string | null;
34
+ readonly generation: string | null;
35
+ /** The column is part of an index (a TEXT type would need a key length). */
36
+ readonly indexed: boolean;
37
+ /**
38
+ * MariaDB writes `COLUMN_DEFAULT` differently (10.2.7+): a literal as a quoted SQL literal
39
+ * (`'it''s'`), an expression bare (`current_timestamp()`, `uuid()`), an explicit `DEFAULT NULL`
40
+ * as the string `NULL`; MySQL shows the bare literal and marks expressions `DEFAULT_GENERATED`.
41
+ */
42
+ readonly mariadb?: boolean;
43
+ }
44
+ /** Reads one MySQL column definition (parameterised). */
45
+ export declare function readMysqlColumnDefinition(query: (sql: string, params?: unknown[]) => Promise<unknown>, table: string, column: string): Promise<MysqlColumnDefinition | undefined>;
46
+ /**
47
+ * `MODIFY COLUMN` restates the whole column, so everything the change does not touch is copied
48
+ * from `information_schema`: the full type (`varchar(64)`, `unsigned`, enum values), character set
49
+ * and collation, `ON UPDATE` / `AUTO_INCREMENT` / `INVISIBLE`, the comment, and the default — an
50
+ * expression default (`CURRENT_TIMESTAMP`, `(uuid())`) stays an expression, a literal stays a
51
+ * literal (escaped), and TEXT / JSON literals become `DEFAULT ('…')`.
52
+ */
53
+ export declare function renderMysqlModify(current: MysqlColumnDefinition, change: {
54
+ readonly type?: string;
55
+ readonly nullable?: boolean;
56
+ readonly default?: string | number | boolean | null;
57
+ readonly dropDefault?: boolean;
58
+ }, host: SchemaDialect): string;
59
+ export declare function quoteConsoleIdentifier(dialect: ConsoleSqlDialect, name: string): string;
60
+ export interface SchemaPlanContext {
61
+ readonly dialect: ConsoleSqlDialect;
62
+ readonly host: SchemaDialect;
63
+ /** True when the table exists (declared, discovered or physical). */
64
+ hasTable(table: string): boolean;
65
+ /** True when plugin code declares the table (its columns are read by that code). */
66
+ isDeclared?(table: string): boolean;
67
+ /** MySQL: the full current definition of a column, for `MODIFY COLUMN`. */
68
+ mysqlColumn?(table: string, column: string): Promise<MysqlColumnDefinition | undefined>;
69
+ /** Current columns of an existing table, for checks and MySQL `MODIFY`. */
70
+ describe(table: string): Promise<DatabaseHostTableSchema>;
71
+ }
72
+ /** Validates a change and renders its DDL; throws {@link ConsoleSchemaRefusal}. */
73
+ export declare function planConsoleSchemaChange(input: unknown, context: SchemaPlanContext): Promise<DatabaseHostSchemaPlan>;
74
+ /** Zhin column type of a native SQL type (best effort, for filters and the schema form). */
75
+ export declare function zhinColumnType(native: string): string;
76
+ /** Reads the physical columns and indexes of a table. */
77
+ export declare function introspectConsoleTable(dialect: ConsoleSqlDialect, query: (sql: string, params?: unknown[]) => Promise<unknown>, table: string): Promise<DatabaseHostTableSchema>;
78
+ /** Physical base tables of the current database / schema. */
79
+ export declare function listPhysicalTables(dialect: ConsoleSqlDialect, query: (sql: string, params?: unknown[]) => Promise<unknown>): Promise<string[]>;