mm_os 3.3.0 → 4.0.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 (380) hide show
  1. package/LICENSE +21 -201
  2. package/README.md +491 -99
  3. package/README_EN.md +498 -0
  4. package/adapter/adapter.js +431 -0
  5. package/adapter/custom_persistence.js +660 -0
  6. package/adapter/mqtt.js +273 -0
  7. package/adapter/socket.js +113 -0
  8. package/adapter/web.js +67 -0
  9. package/adapter/websocket.js +146 -0
  10. package/com/api/com.json +5 -0
  11. package/{core/com → com}/api/config.tpl.json +8 -8
  12. package/com/api/drive.js +708 -0
  13. package/com/api/index.js +198 -0
  14. package/com/api/oauth.js +200 -0
  15. package/com/api/script.tpl.js +32 -0
  16. package/com/cmd/README.md +11 -0
  17. package/com/cmd/com.json +5 -0
  18. package/com/cmd/config.tpl.json +122 -0
  19. package/com/cmd/drive.js +1548 -0
  20. package/com/cmd/index.js +1066 -0
  21. package/com/cmd/msg.json +48 -0
  22. package/com/cmd/nlp.js +525 -0
  23. package/com/cmd/script.tpl.js +32 -0
  24. package/com/db/com.json +5 -0
  25. package/com/db/drive.js +1999 -0
  26. package/com/db/index.js +242 -0
  27. package/com/event/com.json +5 -0
  28. package/{core/com → com}/event/config.tpl.json +8 -8
  29. package/com/event/drive.js +59 -0
  30. package/com/event/index.js +409 -0
  31. package/com/event/script.tpl.js +23 -0
  32. package/com/mqtt/com.json +5 -0
  33. package/{core/com → com}/mqtt/config.tpl.json +3 -5
  34. package/com/mqtt/drive.js +676 -0
  35. package/com/mqtt/index.js +822 -0
  36. package/com/mqtt/mm_mqtt.js +425 -0
  37. package/com/mqtt/script.tpl.js +723 -0
  38. package/com/nav/com.json +5 -0
  39. package/com/nav/config.tpl.json +84 -0
  40. package/com/nav/drive.js +702 -0
  41. package/com/nav/index.js +231 -0
  42. package/{core/com → com}/nav/tpl/admin_pc/page_config.vue +280 -280
  43. package/{core/com → com}/nav/tpl/admin_pc/page_config_form.vue +194 -194
  44. package/com/nav/tpl/admin_pc/page_form.vue +180 -0
  45. package/com/nav/tpl/admin_pc/page_view.vue +124 -0
  46. package/com/nav/tpl/dev_pc/page_default.vue +247 -0
  47. package/com/nav/tpl/dev_pc/page_type.vue +313 -0
  48. package/com/nav/tpl/home_pc/page_default.vue +234 -0
  49. package/com/nav/tpl/home_pc/page_form.vue +137 -0
  50. package/com/nav/tpl/home_pc/page_list.vue +234 -0
  51. package/com/nav/tpl/home_pc/page_nav.vue +221 -0
  52. package/com/nav/tpl/home_pc/page_type.vue +234 -0
  53. package/com/nav/tpl/home_pc/page_view.vue +125 -0
  54. package/com/nav/tpl/home_phone/page_channel.vue +234 -0
  55. package/com/nav/tpl/home_phone/page_default.vue +234 -0
  56. package/com/nav/tpl/home_phone/page_form.vue +137 -0
  57. package/com/nav/tpl/home_phone/page_nav.vue +237 -0
  58. package/com/nav/tpl/home_phone/page_type.vue +234 -0
  59. package/com/nav/tpl/home_phone/page_view.vue +125 -0
  60. package/com/nav/viewmodel.js +446 -0
  61. package/com/param/com.json +5 -0
  62. package/{core/com → com}/param/config.tpl.json +7 -1
  63. package/com/param/drive.js +502 -0
  64. package/com/param/index.js +155 -0
  65. package/com/param/script.tpl.js +12 -0
  66. package/com/pendant/com.json +5 -0
  67. package/{core/com/component → com/pendant}/config.tpl.json +15 -13
  68. package/com/pendant/drive.js +204 -0
  69. package/com/pendant/index.js +441 -0
  70. package/com/pendant/pendant.html +16 -0
  71. package/com/pendant/script.tpl.js +18 -0
  72. package/com/socket/com.json +5 -0
  73. package/com/socket/config.tpl.json +12 -0
  74. package/com/socket/drive.js +651 -0
  75. package/com/socket/index.js +351 -0
  76. package/com/socket/script.tpl.js +41 -0
  77. package/com/sql/com.json +5 -0
  78. package/{core/com → com}/sql/config.tpl.json +13 -9
  79. package/com/sql/drive.js +1259 -0
  80. package/com/sql/index.js +150 -0
  81. package/com/sql/script.tpl.js +47 -0
  82. package/com/static/com.json +5 -0
  83. package/{core/com → com}/static/config.tpl.json +10 -6
  84. package/com/static/drive.js +194 -0
  85. package/com/static/index.js +226 -0
  86. package/com/static/script.tpl.js +28 -0
  87. package/com/task/com.json +5 -0
  88. package/{core/com → com}/task/config.tpl.json +4 -6
  89. package/com/task/drive.js +405 -0
  90. package/com/task/index.js +148 -0
  91. package/com/task/script.tpl.js +37 -0
  92. package/com/template/com.json +5 -0
  93. package/com/template/config.tpl.json +16 -0
  94. package/com/template/drive.js +80 -0
  95. package/com/template/index.js +141 -0
  96. package/com.js +156 -0
  97. package/common/README.md +2 -0
  98. package/common/handler/msg/handler.json +22 -0
  99. package/common/handler/msg/index.js +23 -0
  100. package/common/handler/player/handler.json +22 -0
  101. package/common/handler/player/index.js +287 -0
  102. package/common/handler/user/handler.json +22 -0
  103. package/common/handler/user/index.js +23 -0
  104. package/common/middleware/web_after/index.js +29 -0
  105. package/common/middleware/web_after/middleware.json +9 -0
  106. package/common/middleware/web_base/index.js +113 -0
  107. package/common/middleware/web_base/middleware.json +19 -0
  108. package/common/middleware/web_before/index.js +33 -0
  109. package/common/middleware/web_before/middleware.json +9 -0
  110. package/common/middleware/web_cors/index.js +87 -0
  111. package/common/middleware/web_cors/middleware.json +24 -0
  112. package/common/middleware/web_error/index.js +119 -0
  113. package/common/middleware/web_error/middleware.json +18 -0
  114. package/common/middleware/web_ip/index.js +15 -0
  115. package/common/middleware/web_ip/middleware.json +14 -0
  116. package/common/middleware/web_logger/index.js +156 -0
  117. package/common/middleware/web_logger/middleware.json +14 -0
  118. package/common/middleware/web_main/index.js +24 -0
  119. package/common/middleware/web_main/middleware.json +9 -0
  120. package/common/middleware/web_static/index.js +73 -0
  121. package/common/middleware/web_static/middleware.json +54 -0
  122. package/common/middleware/web_waf/index.js +385 -0
  123. package/common/middleware/web_waf/middleware.json +13 -0
  124. package/common/model/msg/index.js +88 -0
  125. package/common/model/msg/model.json +401 -0
  126. package/common/model/player/index.js +63 -0
  127. package/common/model/player/model.json +185 -0
  128. package/common/model/user/index.js +11 -0
  129. package/common/model/user/model.json +219 -0
  130. package/core/app/config.tpl.json +67 -0
  131. package/core/app/index.js +632 -0
  132. package/core/app/script.tpl.js +52 -0
  133. package/core/channel/index.js +899 -0
  134. package/core/channel/matcher.js +585 -0
  135. package/core/com/config.tpl.json +16 -0
  136. package/core/com/index.js +74 -0
  137. package/core/com/script.tpl.js +5 -0
  138. package/core/component/component.js +42 -0
  139. package/core/component/config.tpl.json +63 -0
  140. package/core/component/index.js +273 -0
  141. package/core/component/script.tpl.js +19 -0
  142. package/core/controller/config.tpl.json +14 -0
  143. package/core/controller/index.js +373 -0
  144. package/core/controller/script.tpl.js +27 -0
  145. package/core/factory/config.tpl.json +14 -0
  146. package/core/factory/entity.js +275 -0
  147. package/core/factory/index.js +241 -0
  148. package/core/factory/script.tpl.js +16 -0
  149. package/core/game/bat/index.js +137 -0
  150. package/core/game/bat/world.js +622 -0
  151. package/core/game/config.tpl.json +16 -0
  152. package/core/game/entity_admin.js +230 -0
  153. package/core/game/index.js +186 -0
  154. package/core/handler/config.tpl.json +22 -0
  155. package/core/handler/index.js +181 -0
  156. package/core/handler/script.tpl.js +23 -0
  157. package/core/logic/config.tpl.json +14 -0
  158. package/core/logic/index.js +59 -0
  159. package/core/logic/script.tpl.js +19 -0
  160. package/core/middleware/config.tpl.json +16 -0
  161. package/core/middleware/index.js +125 -0
  162. package/core/middleware/script.tpl.js +37 -0
  163. package/core/mod/config.tpl.json +22 -0
  164. package/core/mod/index.js +130 -0
  165. package/core/mod/script.tpl.js +34 -0
  166. package/core/model/config.tpl.json +219 -0
  167. package/core/model/index.js +272 -0
  168. package/core/model/model.js +27 -0
  169. package/core/model/script.tpl.js +20 -0
  170. package/core/notifier/config.tpl.json +14 -0
  171. package/core/notifier/index.js +77 -0
  172. package/core/notifier/script.tpl.js +20 -0
  173. package/core/plugin/config.tpl.json +24 -0
  174. package/core/plugin/index.js +232 -0
  175. package/core/plugin/script.tpl.js +51 -0
  176. package/core/pusher/config.tpl.json +14 -0
  177. package/core/pusher/index.js +161 -0
  178. package/core/pusher/script.tpl.js +20 -0
  179. package/core/room/bat/index.js +170 -0
  180. package/core/room/bat/room.js +524 -0
  181. package/core/room/config.tpl.json +20 -0
  182. package/core/room/index.js +249 -0
  183. package/core/room/room.js +61 -0
  184. package/core/scene/config.tpl.json +14 -0
  185. package/core/scene/index.js +466 -0
  186. package/core/scene/loop.js +1255 -0
  187. package/core/scene/map.js +28 -0
  188. package/core/scene/script.tpl.js +22 -0
  189. package/core/sender/config.tpl.json +14 -0
  190. package/core/sender/index.js +79 -0
  191. package/core/sender/script.tpl.js +20 -0
  192. package/core/service/config.tpl.json +14 -0
  193. package/core/service/index.js +100 -0
  194. package/core/service/script.tpl.js +25 -0
  195. package/core/store/config.tpl.json +26 -0
  196. package/core/store/index.js +1755 -0
  197. package/core/store/script.tpl.js +22 -0
  198. package/core/store/sql.js +1464 -0
  199. package/core/system/config.tpl.json +18 -0
  200. package/core/system/index.js +312 -0
  201. package/core/system/script.tpl.js +77 -0
  202. package/core/view/config.tpl.json +14 -0
  203. package/core/view/index.js +91 -0
  204. package/core/view/script.tpl.js +20 -0
  205. package/core/zone/bat/index.js +725 -0
  206. package/core/zone/config.tpl.json +54 -0
  207. package/core/zone/index.js +614 -0
  208. package/core/zone/script.tpl.js +10 -0
  209. package/core/zone/zone_bat.js +136 -0
  210. package/core//345/237/272/347/261/273/346/250/241/345/235/227/346/270/205/345/215/225.md +24 -0
  211. package/index.js +17 -314
  212. package/os.js +57 -0
  213. package/package.json +60 -58
  214. package/server.js +598 -0
  215. package/README.en.md +0 -36
  216. package/conf.json +0 -3
  217. package/core/base/mqtt/index.js +0 -1107
  218. package/core/base/mqtt/lib.js +0 -40
  219. package/core/base/web/index.js +0 -243
  220. package/core/com/api/com.json +0 -4
  221. package/core/com/api/drive.js +0 -668
  222. package/core/com/api/index.js +0 -108
  223. package/core/com/api/oauth.js +0 -158
  224. package/core/com/api/script.js +0 -32
  225. package/core/com/app/README.md +0 -3
  226. package/core/com/app/com.json +0 -4
  227. package/core/com/app/config.tpl.json +0 -16
  228. package/core/com/app/drive.js +0 -309
  229. package/core/com/app/index.js +0 -211
  230. package/core/com/app/script.js +0 -155
  231. package/core/com/cmd/com.json +0 -4
  232. package/core/com/cmd/config.tpl.json +0 -66
  233. package/core/com/cmd/drive.js +0 -513
  234. package/core/com/cmd/index.js +0 -354
  235. package/core/com/cmd/old/5w2h.js +0 -54
  236. package/core/com/cmd/old/drive.js +0 -423
  237. package/core/com/cmd/script.js +0 -11
  238. package/core/com/component/README.md +0 -3
  239. package/core/com/component/com.json +0 -4
  240. package/core/com/component/component.html +0 -16
  241. package/core/com/component/drive.js +0 -197
  242. package/core/com/component/index.js +0 -312
  243. package/core/com/component/script.js +0 -18
  244. package/core/com/db/com.json +0 -4
  245. package/core/com/db/drive.js +0 -1160
  246. package/core/com/db/index.js +0 -176
  247. package/core/com/event/com.json +0 -4
  248. package/core/com/event/drive.js +0 -133
  249. package/core/com/event/index.js +0 -345
  250. package/core/com/event/script.js +0 -26
  251. package/core/com/eventer/com.js +0 -477
  252. package/core/com/eventer/com.json +0 -4
  253. package/core/com/middleware/com.js +0 -153
  254. package/core/com/middleware/com.json +0 -4
  255. package/core/com/middleware/config.tpl.json +0 -8
  256. package/core/com/middleware/script.js +0 -9
  257. package/core/com/mqtt/com.json +0 -4
  258. package/core/com/mqtt/drive.js +0 -600
  259. package/core/com/mqtt/index.js +0 -572
  260. package/core/com/mqtt/mm_mqtt.js +0 -330
  261. package/core/com/mqtt/script.js +0 -604
  262. package/core/com/msg/com.js +0 -296
  263. package/core/com/msg/com.json +0 -4
  264. package/core/com/nav/com.json +0 -4
  265. package/core/com/nav/config.tpl.json +0 -75
  266. package/core/com/nav/drive.js +0 -549
  267. package/core/com/nav/index.js +0 -182
  268. package/core/com/nav/tpl/admin_pc/page_form.vue +0 -180
  269. package/core/com/nav/tpl/admin_pc/page_view.vue +0 -124
  270. package/core/com/nav/tpl/dev_pc/page_default.vue +0 -247
  271. package/core/com/nav/tpl/dev_pc/page_type.vue +0 -313
  272. package/core/com/nav/tpl/home_pc/page_default.vue +0 -234
  273. package/core/com/nav/tpl/home_pc/page_form.vue +0 -137
  274. package/core/com/nav/tpl/home_pc/page_list.vue +0 -234
  275. package/core/com/nav/tpl/home_pc/page_nav.vue +0 -221
  276. package/core/com/nav/tpl/home_pc/page_type.vue +0 -234
  277. package/core/com/nav/tpl/home_pc/page_view.vue +0 -125
  278. package/core/com/nav/tpl/home_phone/page_channel.vue +0 -234
  279. package/core/com/nav/tpl/home_phone/page_default.vue +0 -234
  280. package/core/com/nav/tpl/home_phone/page_form.vue +0 -137
  281. package/core/com/nav/tpl/home_phone/page_nav.vue +0 -237
  282. package/core/com/nav/tpl/home_phone/page_type.vue +0 -234
  283. package/core/com/nav/tpl/home_phone/page_view.vue +0 -125
  284. package/core/com/nav/viewmodel.js +0 -296
  285. package/core/com/param/drive.js +0 -366
  286. package/core/com/param/index.js +0 -80
  287. package/core/com/param/script.js +0 -12
  288. package/core/com/param/test.js +0 -98
  289. package/core/com/plugin/README.md +0 -3
  290. package/core/com/plugin/com.json +0 -4
  291. package/core/com/plugin/config.tpl.json +0 -26
  292. package/core/com/plugin/drive.js +0 -536
  293. package/core/com/plugin/index.js +0 -259
  294. package/core/com/plugin/script.js +0 -213
  295. package/core/com/rpc/com.json +0 -4
  296. package/core/com/rpc/drive.js +0 -160
  297. package/core/com/rpc/index.js +0 -87
  298. package/core/com/rpc/rpc.js +0 -118
  299. package/core/com/socket/com.json +0 -4
  300. package/core/com/socket/config.tpl.json +0 -14
  301. package/core/com/socket/drive.js +0 -403
  302. package/core/com/socket/index.js +0 -62
  303. package/core/com/socket/script.js +0 -42
  304. package/core/com/sql/drive.js +0 -1087
  305. package/core/com/sql/index.js +0 -83
  306. package/core/com/sql/script.js +0 -48
  307. package/core/com/static/com.json +0 -4
  308. package/core/com/static/drive.js +0 -220
  309. package/core/com/static/index.js +0 -149
  310. package/core/com/static/script.js +0 -28
  311. package/core/com/task/com.json +0 -4
  312. package/core/com/task/drive.js +0 -403
  313. package/core/com/task/index.js +0 -110
  314. package/core/com/task/script.js +0 -37
  315. package/core/com/timer/com.js +0 -217
  316. package/core/com/timer/com.json +0 -4
  317. package/core/com/tpl/com.js +0 -19
  318. package/core/com/tpl/com.json +0 -4
  319. package/lib/actions.js +0 -50
  320. package/lib/base.js +0 -361
  321. package/lib/com.js +0 -29
  322. package/lib/ref.js +0 -121
  323. package/middleware/mqtt_base/index.js +0 -10
  324. package/middleware/mqtt_base/middleware.json +0 -10
  325. package/middleware/performance/index.js +0 -151
  326. package/middleware/performance/middleware.json +0 -16
  327. package/middleware/security_audit/index.js +0 -549
  328. package/middleware/security_audit/middleware.json +0 -48
  329. package/middleware/security_headers/index.js +0 -487
  330. package/middleware/security_headers/middleware.json +0 -45
  331. package/middleware/waf/index.js +0 -348
  332. package/middleware/waf/middleware.json +0 -10
  333. package/middleware/waf_ddos/index.js +0 -520
  334. package/middleware/waf_ddos/middleware.json +0 -38
  335. package/middleware/waf_ip/index.js +0 -379
  336. package/middleware/waf_ip/middleware.json +0 -49
  337. package/middleware/waf_xss/index.js +0 -269
  338. package/middleware/waf_xss/middleware.json +0 -18
  339. package/middleware/web_after/index.js +0 -33
  340. package/middleware/web_after/middleware.json +0 -9
  341. package/middleware/web_base/index.js +0 -90
  342. package/middleware/web_base/middleware.json +0 -9
  343. package/middleware/web_before/index.js +0 -27
  344. package/middleware/web_before/middleware.json +0 -9
  345. package/middleware/web_check/index.js +0 -28
  346. package/middleware/web_check/middleware.json +0 -9
  347. package/middleware/web_main/index.js +0 -28
  348. package/middleware/web_main/middleware.json +0 -9
  349. package/middleware/web_proxy/index.js +0 -37
  350. package/middleware/web_proxy/middleware.json +0 -9
  351. package/middleware/web_render/index.js +0 -87
  352. package/middleware/web_render/middleware.json +0 -9
  353. package/middleware/web_socket/index.js +0 -34
  354. package/middleware/web_socket/middleware.json +0 -9
  355. package/middleware/web_static/index.js +0 -115
  356. package/middleware/web_static/middleware.json +0 -9
  357. /package/{core/com → com}/api/README.md +0 -0
  358. /package/{core/com → com}/db/README.md +0 -0
  359. /package/{core/com → com}/event/README.md +0 -0
  360. /package/{core/com → com}/mqtt/README.md +0 -0
  361. /package/{core/com → com}/nav/README.md +0 -0
  362. /package/{core/com → com}/nav/tpl/admin_pc/page_channel.vue +0 -0
  363. /package/{core/com → com}/nav/tpl/admin_pc/page_default.vue +0 -0
  364. /package/{core/com → com}/nav/tpl/admin_pc/page_lang.vue +0 -0
  365. /package/{core/com → com}/nav/tpl/admin_pc/page_nav.vue +0 -0
  366. /package/{core/com → com}/nav/tpl/admin_pc/page_table.vue +0 -0
  367. /package/{core/com → com}/nav/tpl/admin_pc/page_type.vue +0 -0
  368. /package/{core/com → com}/nav/tpl/dev_pc/page_channel.vue +0 -0
  369. /package/{core/com → com}/nav/tpl/dev_pc/page_config.vue +0 -0
  370. /package/{core/com → com}/nav/tpl/dev_pc/page_form.vue +0 -0
  371. /package/{core/com → com}/nav/tpl/dev_pc/page_nav.vue +0 -0
  372. /package/{core/com → com}/nav/tpl/dev_pc/page_table.vue +0 -0
  373. /package/{core/com → com}/nav/tpl/home_pc/page_channel.vue +0 -0
  374. /package/{core/com → com}/nav/tpl/home_phone/page_list.vue +0 -0
  375. /package/{core/com → com}/param/README.md +0 -0
  376. /package/{core/com/cmd → com/pendant}/README.md +0 -0
  377. /package/{core/com → com}/socket/README.md +0 -0
  378. /package/{core/com → com}/sql/README.md +0 -0
  379. /package/{core/com → com}/static/README.md +0 -0
  380. /package/{core/com → com}/task/README.md +0 -0
@@ -0,0 +1,48 @@
1
+ {
2
+ // 消息ID
3
+ "message_id": 0,
4
+ // 结束状态 0-正常 1-结束
5
+ "end": 0,
6
+ // 阶段 1-9阶段
7
+ "stage": 1,
8
+ // 微信/公众号消息ID
9
+ "msgid": "",
10
+ // 更新时间
11
+ "update_time": "",
12
+ // 创建时间
13
+ "create_time": "",
14
+ // 群组
15
+ "group": "",
16
+ // 消息类型 text-文本 image-图片 voice-语音 video-视频 file-文件 location-位置 link-链接 event-事件 other-其他
17
+ "msg_type": "text",
18
+ // 媒体ID,图片、语音、视频、文件
19
+ "media_id": "",
20
+ // 发信人姓名
21
+ "name": "",
22
+ // 一般情况下,1永久会话/群、2临时会话/群
23
+ "type": 1,
24
+ // 指令
25
+ "cmd": "",
26
+ // 应用ID
27
+ "appid": "",
28
+ // 发信人
29
+ "from_user": "",
30
+ // 收信人
31
+ "to_user": "",
32
+ // 机器人
33
+ "robot": "",
34
+ // 会话ID,用于判断是否是同一个会话
35
+ "chatid": "",
36
+ // 发信人头像
37
+ "avatar": "",
38
+ // 除指令外,过滤、抽取后的词
39
+ "keyword": "",
40
+ // 表单,用于记录用户已填参数,json格式
41
+ "form": "",
42
+ // 聊天内容
43
+ "content": "",
44
+ // 消息传递数据,json格式
45
+ "data": "",
46
+ // 备注信息
47
+ "note": ""
48
+ }
package/com/cmd/nlp.js ADDED
@@ -0,0 +1,525 @@
1
+ require('mm_expand');
2
+ const { Nlp } = require('@nlpjs/basic');
3
+
4
+ /**
5
+ * 自然语言处理类
6
+ */
7
+ class NLP {
8
+ static config = {
9
+ languages: ['zh', 'en'],
10
+ language: 'zh',
11
+ dir: './nlp',
12
+ // 模型文件
13
+ model: './model.nlp',
14
+ // 正则表达式文件
15
+ regex: './regex.json'
16
+ };
17
+
18
+ /**
19
+ * 构造函数
20
+ * @param {Object} config - 配置参数
21
+ */
22
+ constructor(config) {
23
+ this.config = {
24
+ ...NLP.config,
25
+ ...config
26
+ };
27
+ this._preset();
28
+ }
29
+ }
30
+
31
+ /**
32
+ * 获取模型文件路径
33
+ * @returns {string} 模型文件路径
34
+ */
35
+ NLP.prototype.getModelFile = function () {
36
+ return this.config.model.fullname(this.config.dir);
37
+ };
38
+
39
+ /**
40
+ * 获取正则表达式文件路径
41
+ * @returns {string} 正则表达式文件路径
42
+ */
43
+ NLP.prototype.getRegexFile = function () {
44
+ return this.config.regex.fullname(this.config.dir);
45
+ };
46
+
47
+ /**
48
+ * 预设
49
+ * @returns {string} 语言
50
+ */
51
+ NLP.prototype._preset = function () {
52
+ console.log('[_init] 开始初始化NLP管理器');
53
+ console.log('[_init] 配置:', JSON.stringify(this.config, null, 2));
54
+
55
+ // 初始化NLP管理器
56
+ this.nlp = new Nlp(this.config);
57
+ console.log('[_init] NLP管理器创建成功');
58
+
59
+ this.load();
60
+ console.log('[_init] 初始化完成');
61
+ };
62
+
63
+ /**
64
+ * 加载模型
65
+ * @param {string} model_file - 模型文件路径
66
+ * @param {string} regex_file - 正则表达式文件路径
67
+ */
68
+ NLP.prototype.load = function (model_file, regex_file) {
69
+ this.loadRegex(regex_file);
70
+ this.loadModel(model_file);
71
+ };
72
+
73
+ /**
74
+ * 加载模型
75
+ * @param {string} file - 模型文件路径
76
+ */
77
+ NLP.prototype.loadModel = async function (file) {
78
+ let f = file || this.getModelFile();
79
+ console.log(`[loadModel] 加载模型文件: ${f}`);
80
+ let json = f.loadJson();
81
+ if (json) {
82
+ this.nlp.fromJSON(json);
83
+ }
84
+ };
85
+
86
+ /**
87
+ * 加载正则表达式
88
+ * @param {string} file - 正则表达式文件路径
89
+ */
90
+ NLP.prototype.loadRegex = function (file) {
91
+ let f = file || this.getRegexFile();
92
+ console.log(`[loadRegex] 加载正则表达式文件: ${f}`);
93
+ this.regex = f.loadJson();
94
+ };
95
+
96
+ /**
97
+ * 保存模型
98
+ * @param {string} model_file - 模型文件路径
99
+ * @param {string} regex_file - 正则表达式文件路径
100
+ */
101
+ NLP.prototype.save = function (model_file, regex_file) {
102
+ console.log(`[save] 开始保存模型...`);
103
+ // 保存模型
104
+ this.saveModel(model_file);
105
+ console.log(`[save] 模型保存成功`);
106
+
107
+ // 保存正则表达式
108
+ this.saveRegex(regex_file);
109
+ console.log(`[save] 正则表达式保存成功`);
110
+ };
111
+
112
+ /**
113
+ * 保存模型
114
+ * @param {string} file - 模型文件路径
115
+ */
116
+ NLP.prototype.saveModel = function (file) {
117
+ let model = this.nlp.toJSON();
118
+ let f = file || this.getModelFile();
119
+ console.log(`[saveModel] 保存模型文件: ${f}`);
120
+ f.saveJson(model);
121
+ };
122
+
123
+ /**
124
+ * 保存正则表达式
125
+ * @param {string} file - 正则表达式文件路径
126
+ */
127
+ NLP.prototype.saveRegex = function (file) {
128
+ let f = file || this.getRegexFile();
129
+ console.log(`[saveRegex] 保存正则表达式文件: ${f}`);
130
+ f.saveJson(this.regex);
131
+ };
132
+
133
+ /**
134
+ * 添加文档
135
+ * @param {string} doc - 文档
136
+ * @param {string} intent - 指令
137
+ */
138
+ NLP.prototype.addDocument = function (doc, intent) {
139
+ console.log(`[addDocument] 添加文档: "${doc}" -> "${intent}"`);
140
+ console.log(`[addDocument] 语言配置: ${this.config.language}`);
141
+
142
+ try {
143
+ // 明确指定locale
144
+ this.nlp.addDocument(this.config.language, doc, intent);
145
+ console.log(`[addDocument] 文档添加完成`);
146
+ } catch (error) {
147
+ console.log(`[addDocument] 添加文档失败:`, error.message);
148
+ throw error;
149
+ }
150
+ };
151
+
152
+ /**
153
+ * 添加命名实体识别文本
154
+ * @param {string} name - 命名实体名称
155
+ * @param {string} value - 值
156
+ * @param {Array} langs - 语言数组
157
+ * @param {Array} values - 值数组
158
+ */
159
+ NLP.prototype.addNamedEntityText = function (name, value, langs, values) {
160
+ this.nlp.addNamedEntityText(name, value, langs, values);
161
+ };
162
+
163
+ /**
164
+ * 处理文本
165
+ * @param {string} text - 输入文本
166
+ * @returns {Object} 响应对象
167
+ */
168
+ NLP.prototype.process = async function (text) {
169
+ let res = await this.nlp.process(this.config.language, text);
170
+ return res;
171
+ };
172
+
173
+ /**
174
+ * 提取参数词汇
175
+ * @param {Object} params - 参数
176
+ * @returns {Array} 词汇数组
177
+ */
178
+ NLP.prototype._extractWords = function (params) {
179
+ let words = [];
180
+
181
+ if (!Array.isArray(params)) {
182
+ return words;
183
+ }
184
+
185
+ for (let i = 0; i < params.length; i++) {
186
+ let param = params[i];
187
+ if (!param || !param.example) {
188
+ continue;
189
+ }
190
+ let arr = param.example.split(';');
191
+ for (let n = 0; n < arr.length; n++) {
192
+ let text = arr[n];
193
+ let values = text.split('|');
194
+ // 提取所有值,而不仅仅是第一个
195
+ for (let k = 0; k < values.length; k++) {
196
+ let value = values[k].trim();
197
+ if (value) {
198
+ words.push({
199
+ name: param.name,
200
+ value,
201
+ values
202
+ });
203
+ }
204
+ }
205
+ }
206
+ }
207
+ return words;
208
+ };
209
+
210
+ /**
211
+ * 训练文档
212
+ * @param {string} intent - 指令
213
+ * @param {Array} examples - 示例数组
214
+ * @param {Array} words - 词汇数组
215
+ */
216
+ NLP.prototype._trainDocuments = function (intent, examples, words) {
217
+ if (!Array.isArray(examples)) {
218
+ return;
219
+ }
220
+
221
+ for (const tpl of examples) {
222
+ if (tpl.indexOf('](') === -1) {
223
+ let docs = this.toDocs(tpl, words);
224
+ for (const doc of docs) {
225
+ this.addDocument(doc, intent);
226
+ }
227
+ }
228
+ else {
229
+ this.addDocument(tpl, intent);
230
+ }
231
+ }
232
+ };
233
+
234
+ /**
235
+ * 训练命名实体识别模型
236
+ * @param {Array} words - 词汇数组
237
+ * @param {string} lang - 语言
238
+ */
239
+ NLP.prototype._trainNamedEntities = function (words, lang) {
240
+ if (!Array.isArray(words)) {
241
+ return;
242
+ }
243
+
244
+ for (const word of words) {
245
+ let { name, value, values } = word;
246
+ this.addNamedEntityText(name, value, [lang], values);
247
+ }
248
+ };
249
+
250
+ /**
251
+ * 训练模型
252
+ * @param {string} intent - 指令,格式xxx_xxx
253
+ * @param {string} example - 示例组
254
+ * @param {Object} params - 参数
255
+ */
256
+ NLP.prototype.train = async function (intent, example, params) {
257
+ console.log(`[train] 开始训练模型,意图: "${intent}"`);
258
+ console.log(`[train] 示例组: "${example}"`);
259
+ console.log(`[train] 参数:`, JSON.stringify(params, null, 2));
260
+
261
+ // 参数校验
262
+ if (typeof intent !== 'string') {
263
+ throw new TypeError('指令必须是字符串');
264
+ }
265
+ if (typeof example !== 'string') {
266
+ throw new TypeError('示例组必须是字符串');
267
+ }
268
+ if (!Array.isArray(params)) {
269
+ throw new TypeError('参数必须是数组');
270
+ }
271
+
272
+ try {
273
+ let lang = this.config.language;
274
+ let examples = example.split(';');
275
+ console.log(`[train] 拆分后的示例:`, examples);
276
+
277
+ let words = this._extractWords(params);
278
+ console.log(`[train] 提取的词汇:`, words);
279
+
280
+ this._trainDocuments(intent, examples, words);
281
+ console.log(`[train] 文档训练完成`);
282
+
283
+ this._trainNamedEntities(words, lang);
284
+ console.log(`[train] 命名实体训练完成`);
285
+
286
+ // 训练模型
287
+ console.log(`[train] 开始模型训练...`);
288
+ try {
289
+ await this.nlp.train({
290
+ trainByDomain: false,
291
+ useNeural: false,
292
+ log: false
293
+ });
294
+ console.log(`[train] 模型训练完成`);
295
+ } catch (error) {
296
+ // 如果训练过程中出现文件保存错误,继续执行
297
+ if (error.message.includes('File cannot be written in web')) {
298
+ console.log(`[train] 模型训练完成(文件保存错误已忽略)`);
299
+ } else {
300
+ throw error;
301
+ }
302
+ }
303
+ } catch (error) {
304
+ console.log(`[train] 训练失败:`, error.message);
305
+ console.log(`[train] 错误堆栈:`, error.stack);
306
+ // 异步方法返回相应类型值,这里返回undefined
307
+ return undefined;
308
+ }
309
+ };
310
+
311
+ /**
312
+ * 转为文档
313
+ * @param {string} tpl - 模板
314
+ * @param {Array} words - 词汇数组
315
+ */
316
+ NLP.prototype.toDocs = function (tpl, words) {
317
+ let docs = [tpl]; // 先添加原始模板
318
+
319
+ // 将大白话转为训练文档,例如:“我想听邓紫棋的喜欢你”转换为“我想听[邓紫棋](singer)的[喜欢你](music_name)”
320
+ // 正确的逻辑:模板包含value字段的词,替换为values中的词加上标注
321
+
322
+ // 第一步:收集所有词汇的替换信息
323
+ let word_replacements = [];
324
+
325
+ for (let i = 0; i < words.length; i++) {
326
+ let word = words[i];
327
+ if (!word || !word.value) continue;
328
+ let { name, value, values } = word;
329
+
330
+ // 检查模板是否包含value字段的词
331
+ if (!tpl.includes(value)) {
332
+ continue;
333
+ }
334
+
335
+ let replacements = [];
336
+
337
+ // 处理每个词汇的values数组
338
+ for (let j = 0; j < values.length; j++) {
339
+ let value_str = values[j];
340
+ let value_arr = value_str.split('|');
341
+
342
+ for (let k = 0; k < value_arr.length; k++) {
343
+ let val = value_arr[k].trim();
344
+ if (val) {
345
+ replacements.push({
346
+ name: name,
347
+ original_value: value, // 模板中实际出现的词
348
+ replacement_value: val, // 要替换成的词
349
+ replacement: `[${val}](${name})`
350
+ });
351
+ }
352
+ }
353
+ }
354
+
355
+ if (replacements.length > 0) {
356
+ word_replacements.push(replacements);
357
+ }
358
+ }
359
+
360
+ // 第二步:生成所有词汇值的笛卡尔积组合
361
+ if (word_replacements.length > 0) {
362
+ let combinations = this._generateCartesianProduct(word_replacements);
363
+
364
+ for (let combination of combinations) {
365
+ let doc = tpl;
366
+
367
+ for (let replacement of combination) {
368
+ // 将模板中的original_value替换为replacement
369
+ doc = doc.replace(replacement.original_value, replacement.replacement);
370
+ }
371
+
372
+ if (!docs.includes(doc)) {
373
+ docs.push(doc);
374
+ }
375
+ }
376
+ }
377
+
378
+ return docs;
379
+ };
380
+
381
+ /**
382
+ * 生成笛卡尔积
383
+ * @param {Array} arrays - 输入数组的数组
384
+ * @returns {Array} 笛卡尔积结果
385
+ */
386
+ NLP.prototype._generateCartesianProduct = function (arrays) {
387
+ if (arrays.length === 0) return [[]];
388
+
389
+ let result = [];
390
+
391
+ function cartesianHelper(arr, index, current) {
392
+ if (index === arrays.length) {
393
+ result.push([...current]);
394
+ return;
395
+ }
396
+
397
+ for (let i = 0; i < arrays[index].length; i++) {
398
+ current.push(arrays[index][i]);
399
+ cartesianHelper(arr, index + 1, current);
400
+ current.pop();
401
+ }
402
+ }
403
+
404
+ cartesianHelper(arrays, 0, []);
405
+ return result;
406
+ };
407
+
408
+ /**
409
+ * 生成正则表达式
410
+ * @returns {Object} 正则表达式对象
411
+ */
412
+ NLP.prototype.genRegex = function (examples) {
413
+ // 生成正则表达式
414
+ let regex = {};
415
+ for (const example of examples) {
416
+ let res = this.process(example);
417
+ let intent = res.intent;
418
+ if (!regex[intent]) {
419
+ regex[intent] = [];
420
+ }
421
+ let rules = this.buildRule(example, res);
422
+ regex[intent].push(...rules);
423
+ }
424
+ return regex;
425
+ };
426
+
427
+ /**
428
+ * 构建正则表达式
429
+ * @param {string} text - 文本
430
+ * @param {Object} res - 处理结果
431
+ * @returns {array} 正则表达式数组
432
+ */
433
+ NLP.prototype.buildRule = function (text, res) {
434
+ let rules = [];
435
+ for (const entity of res.entities) {
436
+ let regex =
437
+ rules.push({
438
+ // 正则表达式
439
+ regex,
440
+ // 参数名
441
+ props: []
442
+ });
443
+ }
444
+ return rules;
445
+ };
446
+
447
+ /**
448
+ * 获取正则表达式数组
449
+ * @returns {array} 正则表达式数组
450
+ */
451
+ NLP.prototype._getRegexs = function () {
452
+ let regexs = ['[a-zA-Z0-9_]+', '[\\u4e00-\\u9fa5]+', '[\\s\\S]+', '\\d+', '\\w+', '\\S+', '(.*)'];
453
+ return regexs;
454
+ };
455
+
456
+ /**
457
+ * 获取符合的正则表达式
458
+ * @param {string} text - 文本
459
+ * @returns {array} 符合的正则表达式数组
460
+ */
461
+ NLP.prototype._extractRegexs = function (text) {
462
+ let regexs = this._getRegexs();
463
+ let arr = [];
464
+ for (const re of regexs) {
465
+ let m = text.match(re);
466
+ if (m) {
467
+ arr.push(re);
468
+ }
469
+ }
470
+ return arr;
471
+ };
472
+
473
+ /**
474
+ * 通过正则表达式提取关键词
475
+ * @param {string} regex - 正则表达式
476
+ * @param {string} text - 文本
477
+ * @returns {array} 关键词数组
478
+ */
479
+ NLP.prototype._extractKeywords = function (regex, text) {
480
+ let matches = text.match(regex);
481
+ return matches;
482
+ };
483
+
484
+ /**
485
+ * 聊天
486
+ * @param {string} text - 输入文本
487
+ * @returns {Object} 响应对象
488
+ */
489
+ NLP.prototype.chat = async function (text) {
490
+ // 先尝试能否有输出
491
+ let res = await this.process(text);
492
+ // 如果没有尝试正则匹配
493
+ if (!res.intent) {
494
+ let matches = this._tryMatches(text);
495
+ }
496
+ return res;
497
+ };
498
+
499
+ /**
500
+ * 尝试匹配正则表达式
501
+ * @param {string} text - 文本
502
+ * @returns {array} 符合的正则表达式数组
503
+ */
504
+ NLP.prototype._tryMatches = function (text) {
505
+ let dict = this.regex;
506
+ let intent = null;
507
+ let words = [];
508
+ for (const key of dict) {
509
+ let regexs = dict[key];
510
+ for (let i = 0; i < regexs.length; i++) {
511
+ let matches = this._extractKeywords(regexs[i], text);
512
+ if (matches) {
513
+ intent = key;
514
+ words.push(...matches);
515
+ break;
516
+ }
517
+ }
518
+ }
519
+ return {
520
+ intent,
521
+ words
522
+ };
523
+ };
524
+
525
+ module.exports = { NLP };
@@ -0,0 +1,32 @@
1
+ module.exports = {
2
+ /**
3
+ * 聊天方式执行指令
4
+ * @param {object} msg 消息
5
+ * @param {object} db 数据管理器,如: { next: async function{}, ret: {} }
6
+ * @returns {object} 执行结果
7
+ */
8
+ async chat(msg, db) {
9
+ let ret = await this.main(msg, db);
10
+ let content = `${ret.location}${ret.datetime}的天气是${ret.weather},温度${ret.temperature}度,湿度${ret.humidity},风力${ret.wind}
11
+ `;
12
+ return content;
13
+ },
14
+
15
+ /**
16
+ * 执行指令主函数
17
+ * @param {object} msg 消息
18
+ * @param {object} db 数据管理器,如: { next: async function{}, ret: {} }
19
+ * @returns {object} 执行结果
20
+ */
21
+ async main(msg, db) {
22
+ let form = msg.form;
23
+ return {
24
+ "location": "梅州",
25
+ "datetime": "2023-08-25 12:00",
26
+ "weather": "晴朗",
27
+ "temperature": "25",
28
+ "humidity": "60%",
29
+ "wind": "3级"
30
+ }
31
+ }
32
+ };
@@ -0,0 +1,5 @@
1
+ {
2
+ "name": "DB",
3
+ "title": "数据管理器",
4
+ "state": 1
5
+ }