@planet-matrix/mobius-model 0.5.0 → 0.9.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 (379) hide show
  1. package/CHANGELOG.md +61 -0
  2. package/README.md +123 -36
  3. package/dist/index.js +715 -4
  4. package/dist/index.js.map +981 -13
  5. package/oxlint.config.ts +6 -0
  6. package/package.json +36 -18
  7. package/src/abort/README.md +92 -0
  8. package/src/abort/abort-manager.ts +278 -0
  9. package/src/abort/abort-signal-listener-manager.ts +81 -0
  10. package/src/abort/index.ts +2 -0
  11. package/src/ai/README.md +1 -0
  12. package/src/ai/ai.ts +107 -0
  13. package/src/ai/chat-completion-ai/aihubmix-chat-completion.ts +78 -0
  14. package/src/ai/chat-completion-ai/chat-completion-ai.ts +270 -0
  15. package/src/ai/chat-completion-ai/chat-completion.ts +189 -0
  16. package/src/ai/chat-completion-ai/index.ts +7 -0
  17. package/src/ai/chat-completion-ai/lingyiwanwu-chat-completion.ts +78 -0
  18. package/src/ai/chat-completion-ai/ohmygpt-chat-completion.ts +78 -0
  19. package/src/ai/chat-completion-ai/openai-next-chat-completion.ts +78 -0
  20. package/src/ai/embedding-ai/embedding-ai.ts +63 -0
  21. package/src/ai/embedding-ai/embedding.ts +50 -0
  22. package/src/ai/embedding-ai/index.ts +4 -0
  23. package/src/ai/embedding-ai/openai-next-embedding.ts +23 -0
  24. package/src/ai/index.ts +4 -0
  25. package/src/aio/README.md +100 -0
  26. package/src/aio/content.ts +141 -0
  27. package/src/aio/index.ts +3 -0
  28. package/src/aio/json.ts +127 -0
  29. package/src/aio/prompt.ts +246 -0
  30. package/src/basic/README.md +72 -116
  31. package/src/basic/error.ts +19 -5
  32. package/src/basic/function.ts +83 -64
  33. package/src/basic/index.ts +1 -0
  34. package/src/basic/is.ts +152 -71
  35. package/src/basic/promise.ts +29 -8
  36. package/src/basic/schedule.ts +111 -0
  37. package/src/basic/stream.ts +135 -25
  38. package/src/basic/string.ts +2 -33
  39. package/src/color/README.md +105 -0
  40. package/src/color/index.ts +3 -0
  41. package/src/color/internal.ts +42 -0
  42. package/src/color/rgb/analyze.ts +236 -0
  43. package/src/color/rgb/construct.ts +130 -0
  44. package/src/color/rgb/convert.ts +227 -0
  45. package/src/color/rgb/derive.ts +303 -0
  46. package/src/color/rgb/index.ts +6 -0
  47. package/src/color/rgb/internal.ts +208 -0
  48. package/src/color/rgb/parse.ts +302 -0
  49. package/src/color/rgb/serialize.ts +144 -0
  50. package/src/color/types.ts +57 -0
  51. package/src/color/xyz/analyze.ts +80 -0
  52. package/src/color/xyz/construct.ts +19 -0
  53. package/src/color/xyz/convert.ts +71 -0
  54. package/src/color/xyz/index.ts +3 -0
  55. package/src/color/xyz/internal.ts +23 -0
  56. package/src/credential/README.md +107 -0
  57. package/src/credential/api-key.ts +158 -0
  58. package/src/credential/bearer.ts +73 -0
  59. package/src/credential/index.ts +4 -0
  60. package/src/credential/json-web-token.ts +96 -0
  61. package/src/credential/password.ts +170 -0
  62. package/src/cron/README.md +86 -0
  63. package/src/cron/cron.ts +87 -0
  64. package/src/cron/index.ts +1 -0
  65. package/src/css/README.md +93 -0
  66. package/src/css/class.ts +559 -0
  67. package/src/css/index.ts +1 -0
  68. package/src/drizzle/README.md +1 -0
  69. package/src/drizzle/drizzle.ts +1 -0
  70. package/src/drizzle/helper.ts +47 -0
  71. package/src/drizzle/index.ts +5 -0
  72. package/src/drizzle/infer.ts +52 -0
  73. package/src/drizzle/kysely.ts +8 -0
  74. package/src/drizzle/pagination.ts +200 -0
  75. package/src/email/README.md +1 -0
  76. package/src/email/index.ts +1 -0
  77. package/src/email/resend.ts +25 -0
  78. package/src/encoding/README.md +66 -79
  79. package/src/encoding/base64.ts +13 -4
  80. package/src/environment/README.md +97 -0
  81. package/src/environment/basic.ts +26 -0
  82. package/src/environment/device.ts +311 -0
  83. package/src/environment/feature.ts +285 -0
  84. package/src/environment/geo.ts +337 -0
  85. package/src/environment/index.ts +7 -0
  86. package/src/environment/runtime.ts +400 -0
  87. package/src/environment/snapshot.ts +60 -0
  88. package/src/environment/variable.ts +239 -0
  89. package/src/event/README.md +90 -0
  90. package/src/event/class-event-proxy.ts +229 -0
  91. package/src/event/common.ts +29 -0
  92. package/src/event/event-manager.ts +203 -0
  93. package/src/event/index.ts +4 -0
  94. package/src/event/instance-event-proxy.ts +187 -0
  95. package/src/event/internal.ts +24 -0
  96. package/src/exception/README.md +96 -0
  97. package/src/exception/browser.ts +219 -0
  98. package/src/exception/index.ts +4 -0
  99. package/src/exception/nodejs.ts +169 -0
  100. package/src/exception/normalize.ts +106 -0
  101. package/src/exception/types.ts +99 -0
  102. package/src/form/README.md +25 -0
  103. package/src/form/index.ts +1 -0
  104. package/src/form/inputor-controller/base.ts +874 -0
  105. package/src/form/inputor-controller/boolean.ts +39 -0
  106. package/src/form/inputor-controller/file.ts +39 -0
  107. package/src/form/inputor-controller/form.ts +181 -0
  108. package/src/form/inputor-controller/helper.ts +117 -0
  109. package/src/form/inputor-controller/index.ts +17 -0
  110. package/src/form/inputor-controller/multi-select.ts +99 -0
  111. package/src/form/inputor-controller/number.ts +116 -0
  112. package/src/form/inputor-controller/select.ts +109 -0
  113. package/src/form/inputor-controller/text.ts +82 -0
  114. package/src/http/READMD.md +1 -0
  115. package/src/http/api/api-core.ts +84 -0
  116. package/src/http/api/api-handler.ts +79 -0
  117. package/src/http/api/api-host.ts +47 -0
  118. package/src/http/api/api-result.ts +56 -0
  119. package/src/http/api/api-schema.ts +154 -0
  120. package/src/http/api/api-server.ts +130 -0
  121. package/src/http/api/api-test.ts +142 -0
  122. package/src/http/api/api-type.ts +37 -0
  123. package/src/http/api/api.ts +81 -0
  124. package/src/http/api/index.ts +11 -0
  125. package/src/http/api-adapter/api-core-node-http.ts +260 -0
  126. package/src/http/api-adapter/api-host-node-http.ts +156 -0
  127. package/src/http/api-adapter/api-result-arktype.ts +297 -0
  128. package/src/http/api-adapter/api-result-zod.ts +286 -0
  129. package/src/http/api-adapter/index.ts +5 -0
  130. package/src/http/bin/gen-api-list/gen-api-list.ts +126 -0
  131. package/src/http/bin/gen-api-list/index.ts +1 -0
  132. package/src/http/bin/gen-api-test/gen-api-test.ts +136 -0
  133. package/src/http/bin/gen-api-test/index.ts +1 -0
  134. package/src/http/bin/gen-api-type/calc-code.ts +25 -0
  135. package/src/http/bin/gen-api-type/gen-api-type.ts +127 -0
  136. package/src/http/bin/gen-api-type/index.ts +2 -0
  137. package/src/http/bin/index.ts +2 -0
  138. package/src/http/index.ts +3 -0
  139. package/src/huawei/README.md +1 -0
  140. package/src/huawei/index.ts +2 -0
  141. package/src/huawei/moderation/index.ts +1 -0
  142. package/src/huawei/moderation/moderation.ts +355 -0
  143. package/src/huawei/obs/esdk-obs-nodejs.d.ts +87 -0
  144. package/src/huawei/obs/index.ts +1 -0
  145. package/src/huawei/obs/obs.ts +42 -0
  146. package/src/identifier/README.md +92 -0
  147. package/src/identifier/id.ts +119 -0
  148. package/src/identifier/index.ts +2 -0
  149. package/src/identifier/uuid.ts +187 -0
  150. package/src/index.ts +33 -1
  151. package/src/json/README.md +92 -0
  152. package/src/json/index.ts +1 -0
  153. package/src/json/repair.ts +18 -0
  154. package/src/log/README.md +79 -0
  155. package/src/log/index.ts +5 -0
  156. package/src/log/log-emitter.ts +72 -0
  157. package/src/log/log-record.ts +10 -0
  158. package/src/log/log-scheduler.ts +74 -0
  159. package/src/log/log-type.ts +8 -0
  160. package/src/log/logger.ts +554 -0
  161. package/src/openai/README.md +1 -0
  162. package/src/openai/index.ts +1 -0
  163. package/src/openai/openai.ts +510 -0
  164. package/src/orchestration/README.md +91 -0
  165. package/src/orchestration/coordination/barrier.ts +214 -0
  166. package/src/orchestration/coordination/count-down-latch.ts +215 -0
  167. package/src/orchestration/coordination/errors.ts +98 -0
  168. package/src/orchestration/coordination/index.ts +16 -0
  169. package/src/orchestration/coordination/internal/wait-constraints.ts +95 -0
  170. package/src/orchestration/coordination/internal/wait-queue.ts +109 -0
  171. package/src/orchestration/coordination/keyed-lock.ts +168 -0
  172. package/src/orchestration/coordination/mutex.ts +257 -0
  173. package/src/orchestration/coordination/permit.ts +127 -0
  174. package/src/orchestration/coordination/read-write-lock.ts +444 -0
  175. package/src/orchestration/coordination/semaphore.ts +280 -0
  176. package/src/orchestration/dispatching/dispatcher.ts +83 -0
  177. package/src/orchestration/dispatching/index.ts +2 -0
  178. package/src/orchestration/dispatching/selector/base-selector.ts +39 -0
  179. package/src/orchestration/dispatching/selector/down-count-selector.ts +119 -0
  180. package/src/orchestration/dispatching/selector/index.ts +2 -0
  181. package/src/orchestration/index.ts +3 -0
  182. package/src/orchestration/scheduling/index.ts +2 -0
  183. package/src/orchestration/scheduling/scheduler.ts +103 -0
  184. package/src/orchestration/scheduling/task.ts +32 -0
  185. package/src/random/README.md +56 -86
  186. package/src/random/base.ts +66 -0
  187. package/src/random/index.ts +5 -1
  188. package/src/random/random-boolean.ts +40 -0
  189. package/src/random/random-integer.ts +60 -0
  190. package/src/random/random-number.ts +72 -0
  191. package/src/random/random-string.ts +66 -0
  192. package/src/reactor/README.md +4 -0
  193. package/src/reactor/reactor-core/primitive.ts +9 -9
  194. package/src/reactor/reactor-core/reactive-system.ts +5 -5
  195. package/src/request/README.md +108 -0
  196. package/src/request/fetch/base.ts +108 -0
  197. package/src/request/fetch/browser.ts +285 -0
  198. package/src/request/fetch/general.ts +20 -0
  199. package/src/request/fetch/index.ts +4 -0
  200. package/src/request/fetch/nodejs.ts +285 -0
  201. package/src/request/index.ts +2 -0
  202. package/src/request/request/base.ts +250 -0
  203. package/src/request/request/general.ts +64 -0
  204. package/src/request/request/index.ts +3 -0
  205. package/src/request/request/resource.ts +68 -0
  206. package/src/result/README.md +4 -0
  207. package/src/result/controller.ts +54 -0
  208. package/src/result/either.ts +193 -0
  209. package/src/result/index.ts +2 -0
  210. package/src/route/README.md +105 -0
  211. package/src/route/adapter/browser.ts +122 -0
  212. package/src/route/adapter/driver.ts +56 -0
  213. package/src/route/adapter/index.ts +2 -0
  214. package/src/route/index.ts +3 -0
  215. package/src/route/router/index.ts +2 -0
  216. package/src/route/router/route.ts +630 -0
  217. package/src/route/router/router.ts +1642 -0
  218. package/src/route/uri/hash.ts +308 -0
  219. package/src/route/uri/index.ts +7 -0
  220. package/src/route/uri/pathname.ts +376 -0
  221. package/src/route/uri/search.ts +413 -0
  222. package/src/singleton/README.md +79 -0
  223. package/src/singleton/factory.ts +55 -0
  224. package/src/singleton/index.ts +2 -0
  225. package/src/singleton/manager.ts +204 -0
  226. package/src/socket/README.md +105 -0
  227. package/src/socket/client/index.ts +2 -0
  228. package/src/socket/client/socket-unit.ts +660 -0
  229. package/src/socket/client/socket.ts +203 -0
  230. package/src/socket/common/index.ts +2 -0
  231. package/src/socket/common/socket-unit-common.ts +23 -0
  232. package/src/socket/common/socket-unit-heartbeat.ts +427 -0
  233. package/src/socket/index.ts +3 -0
  234. package/src/socket/server/index.ts +3 -0
  235. package/src/socket/server/server.ts +183 -0
  236. package/src/socket/server/socket-unit.ts +449 -0
  237. package/src/socket/server/socket.ts +264 -0
  238. package/src/storage/README.md +107 -0
  239. package/src/storage/index.ts +1 -0
  240. package/src/storage/table.ts +449 -0
  241. package/src/timer/README.md +86 -0
  242. package/src/timer/expiration/expiration-manager.ts +594 -0
  243. package/src/timer/expiration/index.ts +3 -0
  244. package/src/timer/expiration/min-heap.ts +208 -0
  245. package/src/timer/expiration/remaining-manager.ts +241 -0
  246. package/src/timer/index.ts +1 -0
  247. package/src/tube/README.md +99 -0
  248. package/src/tube/helper.ts +138 -0
  249. package/src/tube/index.ts +2 -0
  250. package/src/tube/tube.ts +880 -0
  251. package/src/type/README.md +54 -307
  252. package/src/type/class.ts +2 -2
  253. package/src/type/index.ts +14 -14
  254. package/src/type/is.ts +265 -2
  255. package/src/type/object.ts +37 -0
  256. package/src/type/string.ts +7 -2
  257. package/src/type/tuple.ts +6 -6
  258. package/src/type/union.ts +16 -0
  259. package/src/web/README.md +77 -0
  260. package/src/web/capture.ts +35 -0
  261. package/src/web/clipboard.ts +97 -0
  262. package/src/web/dom.ts +117 -0
  263. package/src/web/download.ts +16 -0
  264. package/src/web/event.ts +46 -0
  265. package/src/web/index.ts +10 -0
  266. package/src/web/local-storage.ts +113 -0
  267. package/src/web/location.ts +28 -0
  268. package/src/web/permission.ts +172 -0
  269. package/src/web/script-loader.ts +432 -0
  270. package/src/weixin/README.md +1 -0
  271. package/src/weixin/index.ts +2 -0
  272. package/src/weixin/official-account/authorization.ts +159 -0
  273. package/src/weixin/official-account/index.ts +2 -0
  274. package/src/weixin/official-account/js-api.ts +134 -0
  275. package/src/weixin/open/index.ts +1 -0
  276. package/src/weixin/open/oauth2.ts +133 -0
  277. package/tests/unit/abort/abort-manager.spec.ts +225 -0
  278. package/tests/unit/abort/abort-signal-listener-manager.spec.ts +62 -0
  279. package/tests/unit/ai/ai.spec.ts +85 -0
  280. package/tests/unit/aio/content.spec.ts +105 -0
  281. package/tests/unit/aio/json.spec.ts +147 -0
  282. package/tests/unit/aio/prompt.spec.ts +111 -0
  283. package/tests/unit/basic/array.spec.ts +1 -1
  284. package/tests/unit/basic/error.spec.ts +16 -4
  285. package/tests/unit/basic/schedule.spec.ts +74 -0
  286. package/tests/unit/basic/stream.spec.ts +91 -38
  287. package/tests/unit/basic/string.spec.ts +0 -9
  288. package/tests/unit/color/rgb/analyze.spec.ts +110 -0
  289. package/tests/unit/color/rgb/construct.spec.ts +56 -0
  290. package/tests/unit/color/rgb/convert.spec.ts +60 -0
  291. package/tests/unit/color/rgb/derive.spec.ts +103 -0
  292. package/tests/unit/color/rgb/parse.spec.ts +66 -0
  293. package/tests/unit/color/rgb/serialize.spec.ts +46 -0
  294. package/tests/unit/color/xyz/analyze.spec.ts +33 -0
  295. package/tests/unit/color/xyz/construct.spec.ts +10 -0
  296. package/tests/unit/color/xyz/convert.spec.ts +18 -0
  297. package/tests/unit/credential/api-key.spec.ts +37 -0
  298. package/tests/unit/credential/bearer.spec.ts +23 -0
  299. package/tests/unit/credential/json-web-token.spec.ts +23 -0
  300. package/tests/unit/credential/password.spec.ts +41 -0
  301. package/tests/unit/cron/cron.spec.ts +84 -0
  302. package/tests/unit/css/class.spec.ts +157 -0
  303. package/tests/unit/environment/basic.spec.ts +20 -0
  304. package/tests/unit/environment/device.spec.ts +146 -0
  305. package/tests/unit/environment/feature.spec.ts +388 -0
  306. package/tests/unit/environment/geo.spec.ts +111 -0
  307. package/tests/unit/environment/runtime.spec.ts +364 -0
  308. package/tests/unit/environment/snapshot.spec.ts +4 -0
  309. package/tests/unit/environment/variable.spec.ts +190 -0
  310. package/tests/unit/event/class-event-proxy.spec.ts +225 -0
  311. package/tests/unit/event/event-manager.spec.ts +246 -0
  312. package/tests/unit/event/instance-event-proxy.spec.ts +187 -0
  313. package/tests/unit/exception/browser.spec.ts +213 -0
  314. package/tests/unit/exception/nodejs.spec.ts +144 -0
  315. package/tests/unit/exception/normalize.spec.ts +57 -0
  316. package/tests/unit/form/inputor-controller/base.spec.ts +458 -0
  317. package/tests/unit/form/inputor-controller/boolean.spec.ts +30 -0
  318. package/tests/unit/form/inputor-controller/file.spec.ts +27 -0
  319. package/tests/unit/form/inputor-controller/form.spec.ts +120 -0
  320. package/tests/unit/form/inputor-controller/helper.spec.ts +67 -0
  321. package/tests/unit/form/inputor-controller/multi-select.spec.ts +34 -0
  322. package/tests/unit/form/inputor-controller/number.spec.ts +36 -0
  323. package/tests/unit/form/inputor-controller/select.spec.ts +49 -0
  324. package/tests/unit/form/inputor-controller/text.spec.ts +34 -0
  325. package/tests/unit/http/api/api-core-host.spec.ts +207 -0
  326. package/tests/unit/http/api/api-schema.spec.ts +120 -0
  327. package/tests/unit/http/api/api-server.spec.ts +363 -0
  328. package/tests/unit/http/api/api-test.spec.ts +117 -0
  329. package/tests/unit/http/api/api.spec.ts +121 -0
  330. package/tests/unit/http/api-adapter/node-http.spec.ts +191 -0
  331. package/tests/unit/identifier/id.spec.ts +71 -0
  332. package/tests/unit/identifier/uuid.spec.ts +85 -0
  333. package/tests/unit/json/repair.spec.ts +11 -0
  334. package/tests/unit/log/log-emitter.spec.ts +33 -0
  335. package/tests/unit/log/log-scheduler.spec.ts +40 -0
  336. package/tests/unit/log/log-type.spec.ts +7 -0
  337. package/tests/unit/log/logger.spec.ts +237 -0
  338. package/tests/unit/openai/openai.spec.ts +64 -0
  339. package/tests/unit/orchestration/coordination/barrier.spec.ts +96 -0
  340. package/tests/unit/orchestration/coordination/count-down-latch.spec.ts +63 -0
  341. package/tests/unit/orchestration/coordination/errors.spec.ts +29 -0
  342. package/tests/unit/orchestration/coordination/keyed-lock.spec.ts +109 -0
  343. package/tests/unit/orchestration/coordination/mutex.spec.ts +132 -0
  344. package/tests/unit/orchestration/coordination/permit.spec.ts +43 -0
  345. package/tests/unit/orchestration/coordination/read-write-lock.spec.ts +154 -0
  346. package/tests/unit/orchestration/coordination/semaphore.spec.ts +135 -0
  347. package/tests/unit/orchestration/dispatching/dispatcher.spec.ts +41 -0
  348. package/tests/unit/orchestration/dispatching/selector/down-count-selector.spec.ts +81 -0
  349. package/tests/unit/orchestration/scheduling/scheduler.spec.ts +103 -0
  350. package/tests/unit/random/base.spec.ts +58 -0
  351. package/tests/unit/random/random-boolean.spec.ts +25 -0
  352. package/tests/unit/random/random-integer.spec.ts +32 -0
  353. package/tests/unit/random/random-number.spec.ts +33 -0
  354. package/tests/unit/random/random-string.spec.ts +22 -0
  355. package/tests/unit/reactor/alien-signals-effect.spec.ts +11 -10
  356. package/tests/unit/reactor/preact-signal.spec.ts +1 -2
  357. package/tests/unit/request/fetch/browser.spec.ts +222 -0
  358. package/tests/unit/request/fetch/general.spec.ts +43 -0
  359. package/tests/unit/request/fetch/nodejs.spec.ts +225 -0
  360. package/tests/unit/request/request/base.spec.ts +385 -0
  361. package/tests/unit/request/request/general.spec.ts +161 -0
  362. package/tests/unit/route/router/route.spec.ts +431 -0
  363. package/tests/unit/route/router/router.spec.ts +407 -0
  364. package/tests/unit/route/uri/hash.spec.ts +72 -0
  365. package/tests/unit/route/uri/pathname.spec.ts +147 -0
  366. package/tests/unit/route/uri/search.spec.ts +107 -0
  367. package/tests/unit/singleton/singleton.spec.ts +49 -0
  368. package/tests/unit/socket/client.spec.ts +208 -0
  369. package/tests/unit/socket/server.spec.ts +135 -0
  370. package/tests/unit/socket/socket-unit-heartbeat.spec.ts +214 -0
  371. package/tests/unit/storage/table.spec.ts +620 -0
  372. package/tests/unit/timer/expiration/expiration-manager.spec.ts +464 -0
  373. package/tests/unit/timer/expiration/min-heap.spec.ts +71 -0
  374. package/tests/unit/timer/expiration/remaining-manager.spec.ts +234 -0
  375. package/tests/unit/tube/helper.spec.ts +139 -0
  376. package/tests/unit/tube/tube.spec.ts +501 -0
  377. package/.oxlintrc.json +0 -5
  378. package/src/random/uuid.ts +0 -103
  379. package/tests/unit/random/uuid.spec.ts +0 -37
@@ -0,0 +1,246 @@
1
+ /**
2
+ * 表示普通文本块。
3
+ */
4
+ export interface PromptBlockText {
5
+ type: "text"
6
+ content: string
7
+ }
8
+
9
+ /**
10
+ * 创建一个普通文本块。
11
+ */
12
+ export const text = (content: string): PromptBlockText => ({
13
+ type: "text",
14
+ content,
15
+ })
16
+
17
+ /**
18
+ * 表示空行块。
19
+ */
20
+ export interface PromptBlockEmptyLine {
21
+ type: "empty-line"
22
+ }
23
+
24
+ /**
25
+ * 创建一个空行块。
26
+ */
27
+ export const emptyLine = (): PromptBlockEmptyLine => ({
28
+ type: "empty-line",
29
+ })
30
+
31
+ /**
32
+ * 表示作用域内容块。
33
+ */
34
+ export interface PromptBlockScopeContent {
35
+ type: "scope-content"
36
+ content: string
37
+ }
38
+
39
+ /**
40
+ * 创建一个作用域内容块,并自动包裹三引号边界。
41
+ */
42
+ export const scopeContent = (content: string): PromptBlockScopeContent => ({
43
+ type: "scope-content",
44
+ content: `"""
45
+ ${content}
46
+ """`,
47
+ })
48
+
49
+ /**
50
+ * 表示有序列表项块。
51
+ */
52
+ export interface PromptBlockOrderedListItem {
53
+ type: "ordered-list-item"
54
+ content: string
55
+ }
56
+
57
+ /**
58
+ * 创建一个有序列表项块。
59
+ */
60
+ export const orderedListItem = (content: string): PromptBlockOrderedListItem => ({
61
+ type: "ordered-list-item",
62
+ content,
63
+ })
64
+
65
+ /**
66
+ * 表示无序列表项块。
67
+ */
68
+ export interface PromptBlockUnorderedListItem {
69
+ type: "unordered-list-item"
70
+ content: string
71
+ }
72
+
73
+ /**
74
+ * 创建一个无序列表项块。
75
+ */
76
+ export const unorderedListItem = (content: string): PromptBlockUnorderedListItem => ({
77
+ type: "unordered-list-item",
78
+ content,
79
+ })
80
+
81
+ /**
82
+ * 表示 Prompt 可接受的基础块类型。
83
+ */
84
+ export type PromptBlock =
85
+ | PromptBlockText
86
+ | PromptBlockEmptyLine
87
+ | PromptBlockScopeContent
88
+ | PromptBlockOrderedListItem
89
+ | PromptBlockUnorderedListItem
90
+
91
+ /**
92
+ * 定义用于创建 Prompt 块的辅助函数集合。
93
+ */
94
+ export type CreateBlockHelpers = {
95
+ text: typeof text
96
+ emptyLine: typeof emptyLine
97
+ scopeContent: typeof scopeContent
98
+ orderedListItem: typeof orderedListItem
99
+ unorderedListItem: typeof unorderedListItem
100
+ }
101
+
102
+ /**
103
+ * 提供一组可直接复用的 Prompt 块创建辅助函数。
104
+ */
105
+ export const CREATE_BLOCK_HELPERS: CreateBlockHelpers = {
106
+ text,
107
+ emptyLine,
108
+ scopeContent,
109
+ orderedListItem,
110
+ unorderedListItem,
111
+ }
112
+
113
+ /**
114
+ * 定义 Prompt 的构造选项。
115
+ */
116
+ export interface PromptOptions {
117
+ debug?: boolean
118
+ }
119
+
120
+ /**
121
+ * 以块组合的方式构建 Prompt 文本。
122
+ */
123
+ export class Prompt {
124
+ private promptBlocks: PromptBlock[]
125
+
126
+ constructor(options: PromptOptions) {
127
+ void options
128
+ this.promptBlocks = []
129
+ }
130
+
131
+ /**
132
+ * 使用辅助函数创建并追加一个 Prompt 块。
133
+ */
134
+ addBlock(creator: (helpers: CreateBlockHelpers) => PromptBlock): this {
135
+ this.promptBlocks.push(creator(CREATE_BLOCK_HELPERS))
136
+ return this
137
+ }
138
+
139
+ /**
140
+ * 直接追加一个文本块。
141
+ */
142
+ addText(content: string): this {
143
+ this.promptBlocks.push(CREATE_BLOCK_HELPERS.text(content))
144
+ return this
145
+ }
146
+
147
+ /**
148
+ * 追加一个空行块。
149
+ */
150
+ addEmptyLine(): this {
151
+ this.promptBlocks.push(CREATE_BLOCK_HELPERS.emptyLine())
152
+ return this
153
+ }
154
+
155
+ /**
156
+ * 追加一个作用域内容块。
157
+ */
158
+ addScopeContent(content: string): this {
159
+ this.promptBlocks.push(CREATE_BLOCK_HELPERS.scopeContent(content))
160
+ return this
161
+ }
162
+
163
+ /**
164
+ * 追加一个有序列表项块。
165
+ */
166
+ addOrderedListItem(content: string): this {
167
+ this.promptBlocks.push(CREATE_BLOCK_HELPERS.orderedListItem(content))
168
+ return this
169
+ }
170
+
171
+ /**
172
+ * 追加一个无序列表项块。
173
+ */
174
+ addUnorderedListItem(content: string): this {
175
+ this.promptBlocks.push(CREATE_BLOCK_HELPERS.unorderedListItem(content))
176
+ return this
177
+ }
178
+
179
+ /**
180
+ * 将当前 Prompt 块序列渲染为字符串。
181
+ *
182
+ * 当前实现支持基础文本、空行、作用域内容以及简单的有序/无序列表,且列表不支持嵌套。
183
+ */
184
+ async getPromptInString(): Promise<string> {
185
+ const orderedListContext = {
186
+ enabled: false,
187
+ index: 1,
188
+ }
189
+ const resetOrderedListContext = (): void => {
190
+ orderedListContext.enabled = false
191
+ orderedListContext.index = 1
192
+ }
193
+ const unorderedListContext = {
194
+ enabled: false,
195
+ }
196
+ const resetUnorderedListContext = (): void => {
197
+ unorderedListContext.enabled = false
198
+ }
199
+
200
+ // TODO: 目前有序列表和无序列表不支持嵌套。
201
+ const promptInString = this.promptBlocks.map((block) => {
202
+ switch (block.type) {
203
+ case "text": {
204
+ resetOrderedListContext()
205
+ resetUnorderedListContext()
206
+ return block.content
207
+ }
208
+ case "empty-line": {
209
+ resetOrderedListContext()
210
+ resetUnorderedListContext()
211
+ return "\r\n"
212
+ }
213
+ case "scope-content": {
214
+ resetOrderedListContext()
215
+ resetUnorderedListContext()
216
+ return block.content
217
+ }
218
+ case "ordered-list-item": {
219
+ resetUnorderedListContext()
220
+ if (orderedListContext.enabled === false) {
221
+ orderedListContext.enabled = true
222
+ const currentIndex = orderedListContext.index
223
+ orderedListContext.index = currentIndex + 1
224
+ return `${currentIndex}. ${block.content}`
225
+ }
226
+ const currentIndex = orderedListContext.index
227
+ orderedListContext.index = currentIndex + 1
228
+ return `${currentIndex}. ${block.content}`
229
+ }
230
+ case "unordered-list-item": {
231
+ resetOrderedListContext()
232
+ if (unorderedListContext.enabled === false) {
233
+ unorderedListContext.enabled = true
234
+ return `- ${block.content}`
235
+ }
236
+ return `- ${block.content}`
237
+ }
238
+ default: {
239
+ throw new Error(`Unknown block type: ${String(block)}`)
240
+ }
241
+ }
242
+ }).join("\r\n")
243
+
244
+ return await Promise.resolve(promptInString)
245
+ }
246
+ }
@@ -1,144 +1,100 @@
1
1
  # Basic
2
2
 
3
- Runtime utilities for JavaScript primitives and built-in objects. This module focuses on safe, predictable helpers and type guards that you can use in everyday code.
3
+ ## Description
4
4
 
5
- ## For Users
5
+ Basic 模块提供面向 JavaScript 运行时值(runtime value)的通用基础能力,用于承载一组稳定、可组合、低假设的便捷操作。
6
6
 
7
- This module provides runtime utilities organized by domain so you can quickly locate the right tool for a specific problem.
7
+ 这里所说的“运行时值”,既包括 primitive value,也包括 JavaScript 运行时原生提供的常见对象与接口,例如 `Function`、`Promise`、`ReadableStream`、`Date`、`Error`、`RegExp` 等。
8
8
 
9
- ### 1. Domain Areas
9
+ 它关注的不是某个具体业务领域,而是围绕运行时值展开、可以长期复用的基础处理能力。
10
10
 
11
- 1. Helper: Shared runtime helpers used across other domains.
12
- 2. Is: Runtime predicates and type guards (primitives, objects, iterables, DOM/browser targets, and more).
13
- 3. String: String generation, casing conversion, slicing, splitting, and truncation helpers.
14
- 4. Number: Numeric normalization, range constraint, parity checks, and randomization helpers.
15
- 5. Boolean: Boolean conversion and logical operation helpers.
16
- 6. BigInt: BigInt-oriented helpers.
17
- 7. Symbol: Symbol creation, inspection, and conversion helpers.
18
- 8. Array: Array-oriented helpers and transformations.
19
- 9. Object: Object field selection/exclusion, Date-field timestamp conversion, and object utility helpers.
20
- 10. Function: Function composition and execution-control helpers (once, debounce, throttle, memoize, etc.).
21
- 11. Temporal: Date/time formatting, relative-time, and humanization helpers.
22
- 12. Error: Error detection and exception-stringify helpers.
23
- 13. RegExp: RegExp-based validation helpers.
24
- 14. Promise: Promise control-flow, queueing, retry, interval, and forever-loop helpers.
25
- 15. Stream: ReadableStream construction, consumption, and transform helpers.
26
- 16. Enhance: Runtime enhancement helpers (for example, BigInt JSON serialization support).
11
+ ## For Understanding
27
12
 
28
- For a full list of exports, see the domain files (Helper, Is, String, Number, Boolean, BigInt, Symbol, Array, Object, Function, Temporal, Error, RegExp, Promise, Stream, Enhance).
13
+ 理解 Basic 模块时,首先要把握它的核心边界:这里承载的是“运行时直接提供的值如何被方便、稳定地处理”,而不是“围绕某个自定义对象系统、业务模型或宿主入口建立长期语义”。只要某项能力的主要价值在于处理字符串、数字、布尔值、BigInt、符号(Symbol)、数组、对象、函数、Promise、Error、正则表达式(regular expression)、时间值、流(stream)等运行时值,它通常就有机会属于 Basic。
29
14
 
30
- ## For Contributors
15
+ 从这个角度看,`function`、`promise`、`stream` `boolean`、`string`、`object`、`array`、`number` 没有本质区别,都是运行时已经给出的基础对象形态。
31
16
 
32
- This guide documents the conventions and best practices for implementing runtime utilities in this module. All implementations should follow these patterns for consistency and maintainability.
17
+ 反过来说,如果某项能力的重点已经变成注册全局监听器、读取运行时上下文、感知平台能力、消费宿主级事件源,或围绕某类自定义对象建立独立问题域,那么它通常就不再属于 Basic。Basic 关心的是运行时值的通用处理语义,而不是环境级语义、业务对象语义或更高层的领域模型语义。
33
18
 
34
- ### 1. Documentation and Comments
19
+ 这也意味着 Basic 不是“随手工具箱”。一个能力只有在满足以下条件时,才适合进入这个模块:
35
20
 
36
- #### 1.1 JSDoc Comment Format
21
+ - 它表达的是稳定、清楚、可复用的值级问题。
22
+ - 它对调用方前提的假设足够少,能在不同项目中长期成立。
23
+ - 它适合通过清楚的输入输出语义被组合,而不是依赖隐式上下文、特定业务约定或某个自定义对象体系的内部状态。
37
24
 
38
- Every exported function should have a JSDoc comment following this structure:
25
+ ## For Using
39
26
 
40
- ```typescript
41
- /**
42
- * Brief one-line description of what the function does.
43
- *
44
- * @example
45
- * ```
46
- * // Expect: true
47
- * const example1 = isString("hello")
48
- * // Expect: false
49
- * const example2 = isString(123)
50
- * ```
51
- *
52
- * @see {@link https://……}
53
- */
54
- export const isString = (value: unknown): value is string => {
55
- ...
56
- }
57
- ```
27
+ 当你需要把分散在业务代码中的基础判断、值转换、容器处理,以及围绕函数、Promise、流等运行时原生对象的便捷操作整理成稳定能力时,可以使用这个模块。它适合放在业务逻辑与语言原生能力之间,作为一层更可复用、更可测试的基础模型层。
58
28
 
59
- **Documentation Rules:**
60
- - First line: Clear, concise description starting with a verb (Check, Get, Convert, etc.)
61
- - Add a blank line after the description
62
- - Use `@example` tag followed by triple backticks
63
- - Include multiple cases showing different scenarios
64
- - Use comment format: `// Expect: <result>`
65
- - Assign example results to variables like `example1`, `example2` to keep examples readable
66
- - Place `@see`(if has) after the `@example` block, separated by a blank line
67
- - Prefer deterministic examples; avoid randomness or time-dependent output in docs
68
- - If a function returns a non-scalar, show the expected shape or key properties
29
+ 当前公共能力大致可以按以下几类理解:
69
30
 
70
- ### 2. Runtime Implementation Patterns
31
+ - 值判断与基础守卫:例如 primitive、对象、数组、空值、数字特征等运行时判断,用于减少重复分支。
32
+ - 基础值处理:例如字符串、数字、布尔值、BigInt、符号等常见值类型的转换、裁剪、计算与格式化。
33
+ - 容器处理:例如数组与对象的筛选、映射、分区、裁剪、合并、字段选择与标准化,适合在进入业务模型前先整理数据形态。
34
+ - 函数相关:例如一次性调用、防抖、节流、组合、管道、记忆化、条件执行等围绕函数值展开的便捷能力。
35
+ - 异步与流处理:例如 Promise 队列、重试、轮询、失败结果表达,以及围绕 `ReadableStream` 的基础消费与转换。
36
+ - 时间、错误与正则辅助:例如时间格式化、异常字符串化、常见模式判断等值级辅助能力。
37
+ - 显式增强能力:如果某个能力会对内建对象做增强,它必须保持显式调用、行为可审查,并且不应成为其它公共能力成立的隐含前提。
71
38
 
72
- #### 2.1 Prefer Type Predicates
39
+ 如果你的问题已经需要环境感知、平台能力探测、全局异常监听,或需要围绕某类自定义对象建立独立问题域,应优先考虑 Environment、Exception 或其它更贴近问题域的模块,而不是继续把能力堆进 Basic。
73
40
 
74
- Use type predicates for validation functions to maximize type safety:
41
+ ## For Contributing
75
42
 
76
- ```typescript
77
- export const isDate = (value: unknown): value is Date => {
78
- return Object.prototype.toString.call(value) === "[object Date]"
79
- }
80
- ```
43
+ 贡献 Basic 模块时,首要任务不是补齐一个方便函数,而是确认这项能力是否真的表达了稳定、可复用的运行时基础语义。这里应长期承载的是对运行时值的清楚整理,而不是对某个调用点的临时修补。
81
44
 
82
- #### 2.2 Accept `unknown`
45
+ 在扩展时,应优先遵守以下模块边界:
83
46
 
84
- Public utilities should accept `unknown` unless there is a strong reason to require a narrower input type.
47
+ - 只收纳围绕运行时值展开的判断、转换、组合、格式化与其它稳定便捷操作。
48
+ - 不把环境探测、宿主监听、全局状态操纵或平台接入逻辑放入 Basic。
49
+ - 不为了省几行调用代码就公开一次性便捷包装;只有值得长期承诺的公共语义才应进入模块。
50
+ - 如果某项能力依赖显式增强内建对象,必须让增强保持可选,而不是让整个模块隐式建立在原型修改之上。
51
+ - 若新增能力主要是在围绕某类自定义对象、某套业务状态机、某个框架生命周期或某个宿主入口建立独立问题域,那么它通常应进入更贴近该问题域的模块,而不是留在 Basic。
85
52
 
86
- #### 2.3 Avoid Side Effects
53
+ ### JSDoc 注释格式要求
87
54
 
88
- Helpers should be deterministic and should not mutate input values unless explicitly documented.
55
+ - 每个公开导出的目标(类型、函数、变量、类等)都应包含 JSDoc 注释,让人在不跳转实现的情况下就能理解用途。
56
+ - JSDoc 注释第一行应为清晰且简洁的描述,该描述优先使用中文(英文也可以)。
57
+ - 如果描述后还有其他内容,应在描述后加一个空行。
58
+ - 如果有示例,应使用 `@example` 标签,后接三重反引号代码块(不带语言标识)。
59
+ - 如果有示例,应包含多个场景,展示不同用法,尤其要覆盖常见组合方式或边界输入。
60
+ - 如果有示例,应使用注释格式说明每个场景:`// Expect: <result>`。
61
+ - 如果有示例,应将结果赋值给 `example1`、`example2` 之类的变量,以保持示例易读。
62
+ - 如果有示例,`// Expect: <result>` 应该位于 `example1`、`example2` 之前,以保持示例的逻辑清晰。
63
+ - 如果有示例,应优先使用确定性示例;避免断言精确的随机输出。
64
+ - 如果函数返回结构化字符串,应展示其预期格式特征。
65
+ - 如果有参考资料,应将 `@see` 放在 `@example` 代码块之后,并用一个空行分隔。
89
66
 
90
- #### 2.4 Helper Placement
67
+ ### 实现规范要求
91
68
 
92
- - If a utility function uses helper variables or helper functions, place those helpers immediately before the utility function.
93
- - Do not add blank lines between a helper and the utility function it supports.
94
- - Helper variables and functions must be prefixed with `internal`.
95
- - Helper variables and functions must never be exported.
69
+ - 不同程序元素之间使用一个空行分隔,保持结构清楚。这里的程序元素,通常指函数、类型、常量,以及直接服务于它们的辅助元素。
70
+ - 某程序元素独占的辅助元素与该程序元素本身视为一个整体,不要在它们之间添加空行。
71
+ - 程序元素的辅助元素应该放置在该程序元素的上方,以保持阅读时的逻辑顺序。
72
+ - 若辅助元素被多个程序元素共享,则应将其视为独立的程序元素,放在这些程序元素中第一个相关目标的上方,并与后续程序元素之间保留一个空行。
73
+ - 辅助元素也应该像其它程序元素一样,保持清晰的命名和适当的注释,以便在需要阅读实现细节时能够快速理解它们的作用和使用方式。
74
+ - 辅助元素的命名必须以前缀 `internal` 开头(或 `Internal`,大小写不敏感)。
75
+ - 辅助元素永远不要公开导出。
76
+ - 被模块内多个不同文件中的程序元素共享的辅助元素,应该放在一个单独的文件中,例如 `./src/basic/internal.ts`;若当前没有必要,不必为了形式强行拆分。
77
+ - 模块内可以包含子模块。只有当某个子目录表达一个稳定、可单独理解、且可能被父模块重导出的子问题域时,才应将其视为子模块。
78
+ - 子模块包含多个文件时,应该为其单独创建子文件夹,并为其创建单独的 Barrel 文件;父模块的 Barrel 文件再重导出子模块的 Barrel 文件。
79
+ - 子模块不需要有自己的 `README.md`。
80
+ - 子模块可以有自己的 `internal.ts` 文件,多个子模块共享的辅助元素应该放在父模块的 `internal.ts` 文件中,单个子模块共享的辅助元素应该放在该子模块的 `internal.ts` 文件中。
81
+ - 对模块依赖关系的要求(通常是不循环依赖或不反向依赖)与对 DRY 的要求可能产生冲突。此时,若复用的代码数量不大,可以适当牺牲 DRY,复制粘贴并保留必要的注释说明;若复用的代码数量较大,则可以将其抽象到新的文件或子模块中,如 `common.ts`,并在需要的地方导入使用。
82
+ - Basic 的实现应优先保证输入输出语义直接、低副作用、可组合,而不是追求隐式魔法、业务耦合或依赖特定框架约定的包装。
96
83
 
97
- #### 2.5 Spacing
84
+ ### 导出策略要求
98
85
 
99
- - Separate different utility functions with a single blank line.
86
+ - 保持内部辅助项和内部符号为私有,不要让外部接入依赖临时性的内部结构。
87
+ - 每个模块都应有一个用于重导出所有公共 API 的 Barrel 文件。
88
+ - Barrel 文件应命名为 `index.ts`,放在模块目录根部,并且所有公共 API 都应从该文件导出。
89
+ - 新增公共能力时,应优先检查它是否表达稳定、清楚且值得长期维护的运行时基础语义,而不是某段实现细节、业务约定或一次性便捷暴露;仅在确认需要长期对外承诺时再加入 Barrel 导出。
100
90
 
101
- ### 3. Naming Conventions
91
+ ### 测试要求
102
92
 
103
- #### 3.1 Function Name Format
104
-
105
- All functions should follow a consistent naming pattern:
106
-
107
- **Format:** `<domain><Operation><Qualifier>`
108
-
109
- - `is*` for predicates and checks (for example, `isPlainObject`, `isFiniteNumber`)
110
- - `string*` for string utilities (`stringRandom`, `stringTruncate`)
111
- - `number*` for number utilities (`numberClamp`, `numberInRange`)
112
- - `boolean*`, `date*`, `regexp*`, `error*`, `symbol*`, `bigint*` for other domains
113
-
114
- #### 3.2 Common Verb Prefixes
115
-
116
- - `is*` - Returns boolean for validation
117
- - `has*` - Returns boolean for existence checks
118
- - `get*` / `extract*` - Returns a value
119
- - `to*` - Conversion operations
120
-
121
- ### 4. Export Strategy
122
-
123
- ```typescript
124
- // Always export public functions
125
- export const publicFunction = () => {}
126
-
127
- // Never export internal helpers
128
- const internalHelper = () => {}
129
- ```
130
-
131
- Keep domain exports in their files and re-export them via the barrel in `index.ts`.
132
-
133
- ### 5. Common Pitfalls to Avoid
134
-
135
- 1. Do not rely on `typeof` alone for objects (for example, `typeof null === "object"`).
136
- 2. Do not confuse `NaN` with numbers that cannot be parsed.
137
- 3. Do not treat boxed objects (`new String("x")`) as primitives without checks.
138
- 4. Do not mutate inputs unless the function name and docs make it explicit.
139
- 5. Do not introduce locale-dependent behavior without documenting it.
140
-
141
- ### 6. Testing Requirements
142
-
143
- - Write one test per function.
144
- - If multiple cases are needed, include them within the same test.
93
+ - 若程序元素是函数,则只为该函数编写一个测试,如果该函数需要测试多个用例,应放在同一个测试中。
94
+ - 若程序元素是类,则至少要为该类的每一个方法编写一个测试,如果该方法需要测试多个用例,应放在同一个测试中。
95
+ - 若程序元素是类,除了为该类的每一个方法编写至少一个测试之外,还可以为该类编写任意多个测试,以覆盖该类的不同使用场景或边界情况。
96
+ - 若编写测试时需要用到辅助元素(Mock 或 Spy 等),可以在测试文件中直接定义这些辅助元素。若辅助元素较为简单,则可以直接放在每一个测试内部,优先保证每个测试的独立性,而不是追求极致 DRY;若辅助元素较为复杂或需要在多个测试中复用,则可以放在测试文件顶部,供该测试文件中的所有测试使用。
97
+ - 测试顺序应与源文件中被测试目标的原始顺序保持一致。
98
+ - 若该模块不需要测试,必须在说明文件中明确说明该模块不需要测试,并说明理由。一般来说,只有在该模块没有可执行的公共函数、只承载类型层表达,或其语义已被上层模块的测试完整覆盖且重复测试几乎不再带来额外价值时,才适合这样处理。
99
+ - 模块的单元测试文件目录是 `./tests/unit/basic`,若模块包含子模块,则子模块的单元测试文件目录为 `./tests/unit/basic/<sub-module-name>`。
100
+ - 对带有时间、随机、异步调度或流消费语义的能力,应显式控制前提,避免把不稳定行为写成稳定断言。
@@ -39,13 +39,27 @@ export const errorIsNetworkError = (error: unknown): error is Error => {
39
39
  }
40
40
 
41
41
  /**
42
- * Stringifies an exception into a readable format.
42
+ * Stringifies an error into a readable format.
43
43
  */
44
- export const errorStringifyException = (exception: unknown): string => {
45
- if (isError(exception)) {
46
- return `${exception.name}: ${exception.message}`
44
+ export const errorStringify = (error: unknown): string => {
45
+ if (isError(error)) {
46
+ return `${error.name}: ${error.message}`
47
47
  }
48
48
  else {
49
- return `${String(exception)}`
49
+ return `${String(error)}`
50
+ }
51
+ }
52
+
53
+ /**
54
+ * Converts a value to an Error object. If the value is already an Error,
55
+ * it is returned as is. Otherwise, a new Error is created with the
56
+ * stringified value as its message.
57
+ */
58
+ export const toError = (value: unknown): Error => {
59
+ if (isError(value)) {
60
+ return value
61
+ }
62
+ else {
63
+ return new Error(String(value))
50
64
  }
51
65
  }