@harrylabsj/kiwi-merchant-cloud 0.12.31 → 0.12.33

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 (473) hide show
  1. package/app/a2a/inquiry-nonce-store.d.ts +12 -1
  2. package/app/a2a/inquiry-nonce-store.js +49 -17
  3. package/app/a2a/inquiry-nonce-store.js.map +1 -1
  4. package/app/a2a/negotiate.d.ts +30 -1
  5. package/app/a2a/negotiate.js +121 -32
  6. package/app/a2a/negotiate.js.map +1 -1
  7. package/app/a2a/node.d.ts +4 -0
  8. package/app/a2a/node.js +182 -4
  9. package/app/a2a/node.js.map +1 -1
  10. package/app/a2a/open-inquiry-session.d.ts +1 -0
  11. package/app/a2a/open-inquiry-session.js +3 -1
  12. package/app/a2a/open-inquiry-session.js.map +1 -1
  13. package/app/a2a/open-inquiry-verifier.js +2 -2
  14. package/app/a2a/open-inquiry-verifier.js.map +1 -1
  15. package/app/a2a/reception/agent-runtime.d.ts +32 -1
  16. package/app/a2a/reception/agent-runtime.js +420 -11
  17. package/app/a2a/reception/agent-runtime.js.map +1 -1
  18. package/app/a2a/reception/customer-memory-tools.d.ts +73 -0
  19. package/app/a2a/reception/customer-memory-tools.js +17 -0
  20. package/app/a2a/reception/customer-memory-tools.js.map +1 -0
  21. package/app/a2a/reception/engine.d.ts +12 -0
  22. package/app/a2a/reception/engine.js +42 -1
  23. package/app/a2a/reception/engine.js.map +1 -1
  24. package/app/a2a/reception/publish.d.ts +14 -1
  25. package/app/a2a/reception/publish.js +62 -1
  26. package/app/a2a/reception/publish.js.map +1 -1
  27. package/app/a2a/reception/question-spec.d.ts +91 -0
  28. package/app/a2a/reception/question-spec.js +260 -0
  29. package/app/a2a/reception/question-spec.js.map +1 -0
  30. package/app/a2a/reception/reply-outbox.d.ts +6 -0
  31. package/app/a2a/reception/reply-outbox.js +44 -0
  32. package/app/a2a/reception/reply-outbox.js.map +1 -1
  33. package/app/a2a/reception/responder.d.ts +13 -1
  34. package/app/a2a/reception/responder.js +26 -3
  35. package/app/a2a/reception/responder.js.map +1 -1
  36. package/app/a2a/reception/skill-catalog.d.ts +72 -0
  37. package/app/a2a/reception/skill-catalog.js +85 -0
  38. package/app/a2a/reception/skill-catalog.js.map +1 -0
  39. package/app/a2a/reception/skill-contract.d.ts +70 -0
  40. package/app/a2a/reception/skill-contract.js +183 -0
  41. package/app/a2a/reception/skill-contract.js.map +1 -0
  42. package/app/a2a/reception/types.d.ts +12 -0
  43. package/app/a2a/server/customer-identity-gateway.d.ts +111 -0
  44. package/app/a2a/server/customer-identity-gateway.js +441 -0
  45. package/app/a2a/server/customer-identity-gateway.js.map +1 -0
  46. package/app/a2a/server/errors.d.ts +4 -3
  47. package/app/a2a/server/errors.js +10 -2
  48. package/app/a2a/server/errors.js.map +1 -1
  49. package/app/a2a/server/merchant-handler.d.ts +75 -1
  50. package/app/a2a/server/merchant-handler.js +676 -86
  51. package/app/a2a/server/merchant-handler.js.map +1 -1
  52. package/app/a2a/server/merchant-procurement.d.ts +90 -0
  53. package/app/a2a/server/merchant-procurement.js +75 -0
  54. package/app/a2a/server/merchant-procurement.js.map +1 -0
  55. package/app/a2a/server/merchant-product-facts.d.ts +2 -0
  56. package/app/a2a/server/merchant-product-facts.js.map +1 -1
  57. package/app/a2a/server/negotiation-approvals.d.ts +11 -0
  58. package/app/a2a/server/negotiation-approvals.js +43 -0
  59. package/app/a2a/server/negotiation-approvals.js.map +1 -1
  60. package/app/a2a/server/pipeline.js +78 -30
  61. package/app/a2a/server/pipeline.js.map +1 -1
  62. package/app/a2a/server/server.js +4 -3
  63. package/app/a2a/server/server.js.map +1 -1
  64. package/app/a2a/server/types.d.ts +4 -1
  65. package/app/a2a/unit-alias.d.ts +3 -3
  66. package/app/a2a/unit-alias.js +9 -5
  67. package/app/a2a/unit-alias.js.map +1 -1
  68. package/app/agent/alipay-native-view.d.ts +27 -0
  69. package/app/agent/alipay-native-view.js +113 -0
  70. package/app/agent/alipay-native-view.js.map +1 -0
  71. package/app/agent/buyer/buyer-tools.d.ts +20 -0
  72. package/app/agent/buyer/buyer-tools.js +241 -48
  73. package/app/agent/buyer/buyer-tools.js.map +1 -1
  74. package/app/agent/buyer/types.d.ts +1 -1
  75. package/app/agent/buyer/types.js.map +1 -1
  76. package/app/agent/chat-tui.js +16 -1
  77. package/app/agent/chat-tui.js.map +1 -1
  78. package/app/agent/kernel-builder.d.ts +2 -0
  79. package/app/agent/kernel-builder.js +14 -0
  80. package/app/agent/kernel-builder.js.map +1 -1
  81. package/app/agent/kernel.d.ts +8 -0
  82. package/app/agent/kernel.js +67 -1
  83. package/app/agent/kernel.js.map +1 -1
  84. package/app/auth/merchant-sessions.d.ts +3 -0
  85. package/app/auth/merchant-sessions.js +21 -5
  86. package/app/auth/merchant-sessions.js.map +1 -1
  87. package/app/buyer-core/a2a-accept-terms.d.ts +11 -0
  88. package/app/buyer-core/a2a-accept-terms.js +28 -0
  89. package/app/buyer-core/a2a-accept-terms.js.map +1 -0
  90. package/app/buyer-core/a2a-knp.d.ts +7 -2
  91. package/app/buyer-core/a2a-knp.js +8 -5
  92. package/app/buyer-core/a2a-knp.js.map +1 -1
  93. package/app/buyer-core/a2a-negotiator.d.ts +17 -0
  94. package/app/buyer-core/a2a-negotiator.js +70 -26
  95. package/app/buyer-core/a2a-negotiator.js.map +1 -1
  96. package/app/buyer-core/a2a-quote-fetcher.d.ts +25 -15
  97. package/app/buyer-core/a2a-quote-fetcher.js +92 -19
  98. package/app/buyer-core/a2a-quote-fetcher.js.map +1 -1
  99. package/app/buyer-core/a2a-quote-history.d.ts +94 -0
  100. package/app/buyer-core/a2a-quote-history.js +149 -0
  101. package/app/buyer-core/a2a-quote-history.js.map +1 -0
  102. package/app/buyer-core/build-service.d.ts +19 -0
  103. package/app/buyer-core/build-service.js +30 -1
  104. package/app/buyer-core/build-service.js.map +1 -1
  105. package/app/buyer-core/errors.d.ts +1 -1
  106. package/app/buyer-core/errors.js +1 -0
  107. package/app/buyer-core/errors.js.map +1 -1
  108. package/app/buyer-core/merchant-index.d.ts +2 -2
  109. package/app/buyer-core/merchant-index.js +23 -5
  110. package/app/buyer-core/merchant-index.js.map +1 -1
  111. package/app/buyer-core/open-inquiry-signer.d.ts +1 -0
  112. package/app/buyer-core/open-inquiry-signer.js +5 -2
  113. package/app/buyer-core/open-inquiry-signer.js.map +1 -1
  114. package/app/buyer-core/procurement/cases.d.ts +441 -0
  115. package/app/buyer-core/procurement/cases.js +457 -0
  116. package/app/buyer-core/procurement/cases.js.map +1 -0
  117. package/app/buyer-core/procurement/quote-comparison.d.ts +137 -0
  118. package/app/buyer-core/procurement/quote-comparison.js +106 -0
  119. package/app/buyer-core/procurement/quote-comparison.js.map +1 -0
  120. package/app/buyer-core/procurement/quote-normalizer.d.ts +45 -0
  121. package/app/buyer-core/procurement/quote-normalizer.js +153 -0
  122. package/app/buyer-core/procurement/quote-normalizer.js.map +1 -0
  123. package/app/buyer-core/procurement/requirements.d.ts +50 -0
  124. package/app/buyer-core/procurement/requirements.js +43 -0
  125. package/app/buyer-core/procurement/requirements.js.map +1 -0
  126. package/app/buyer-core/procurement/source-observation.d.ts +57 -0
  127. package/app/buyer-core/procurement/source-observation.js +54 -0
  128. package/app/buyer-core/procurement/source-observation.js.map +1 -0
  129. package/app/buyer-core/procurement/store.d.ts +21 -0
  130. package/app/buyer-core/procurement/store.js +68 -0
  131. package/app/buyer-core/procurement/store.js.map +1 -0
  132. package/app/buyer-core/protocol-recovery.d.ts +10 -1
  133. package/app/buyer-core/protocol-recovery.js +31 -6
  134. package/app/buyer-core/protocol-recovery.js.map +1 -1
  135. package/app/buyer-core/quote-quantity.d.ts +5 -0
  136. package/app/buyer-core/quote-quantity.js +15 -0
  137. package/app/buyer-core/quote-quantity.js.map +1 -0
  138. package/app/buyer-core/service.d.ts +128 -8
  139. package/app/buyer-core/service.js +543 -75
  140. package/app/buyer-core/service.js.map +1 -1
  141. package/app/buyer-core/store.d.ts +21 -0
  142. package/app/buyer-core/store.js +62 -10
  143. package/app/buyer-core/store.js.map +1 -1
  144. package/app/cli.js +26 -19
  145. package/app/cli.js.map +1 -1
  146. package/app/cloud/alipay-collection-http.d.ts +44 -0
  147. package/app/cloud/alipay-collection-http.js +111 -0
  148. package/app/cloud/alipay-collection-http.js.map +1 -0
  149. package/app/cloud/bootstrap.js +218 -19
  150. package/app/cloud/bootstrap.js.map +1 -1
  151. package/app/cloud/http-router.d.ts +2 -0
  152. package/app/cloud/http-router.js +5 -0
  153. package/app/cloud/http-router.js.map +1 -1
  154. package/app/cloud-sample/products.json +3 -3
  155. package/app/commerce/receipt/dispatcher.d.ts +43 -0
  156. package/app/commerce/receipt/dispatcher.js +86 -0
  157. package/app/commerce/receipt/dispatcher.js.map +1 -0
  158. package/app/commerce/receipt/receipts.d.ts +147 -0
  159. package/app/commerce/receipt/receipts.js +285 -0
  160. package/app/commerce/receipt/receipts.js.map +1 -0
  161. package/app/config/profile.d.ts +2 -2
  162. package/app/discovery/catalog-source/buyer-follows.d.ts +3 -3
  163. package/app/discovery/catalog-source/buyer-follows.js +6 -3
  164. package/app/discovery/catalog-source/buyer-follows.js.map +1 -1
  165. package/app/discovery/catalog-source/kiwi-record.d.ts +2 -0
  166. package/app/discovery/catalog-source/kiwi-record.js.map +1 -1
  167. package/app/discovery/catalog-source/kiwi-source.js +4 -0
  168. package/app/discovery/catalog-source/kiwi-source.js.map +1 -1
  169. package/app/handoff/alipay-collection.d.ts +23 -0
  170. package/app/handoff/alipay-collection.js +70 -0
  171. package/app/handoff/alipay-collection.js.map +1 -0
  172. package/app/handoff/order-receipts.d.ts +62 -0
  173. package/app/handoff/order-receipts.js +72 -0
  174. package/app/handoff/order-receipts.js.map +1 -0
  175. package/app/handoff/ucp-checkout/cart-channel.d.ts +2 -1
  176. package/app/handoff/ucp-checkout/cart-channel.js +1 -0
  177. package/app/handoff/ucp-checkout/cart-channel.js.map +1 -1
  178. package/app/handoff/ucp-checkout/channel.d.ts +7 -0
  179. package/app/handoff/ucp-checkout/channel.js +79 -10
  180. package/app/handoff/ucp-checkout/channel.js.map +1 -1
  181. package/app/http/cli.js +6 -0
  182. package/app/http/cli.js.map +1 -1
  183. package/app/http/merchant-management/api.d.ts +18 -0
  184. package/app/http/merchant-management/api.js +752 -3
  185. package/app/http/merchant-management/api.js.map +1 -1
  186. package/app/http/merchant-management/draft-store.js +8 -6
  187. package/app/http/merchant-management/draft-store.js.map +1 -1
  188. package/app/http/merchant-management/page.d.ts +1 -0
  189. package/app/http/merchant-management/page.js +335 -5
  190. package/app/http/merchant-management/page.js.map +1 -1
  191. package/app/http/server.js +9 -2
  192. package/app/http/server.js.map +1 -1
  193. package/app/mcp/cli.d.ts +3 -1
  194. package/app/mcp/cli.js +18 -2
  195. package/app/mcp/cli.js.map +1 -1
  196. package/app/mcp/merchant-runtime-assembly.d.ts +20 -0
  197. package/app/mcp/merchant-runtime-assembly.js +94 -11
  198. package/app/mcp/merchant-runtime-assembly.js.map +1 -1
  199. package/app/mcp/merchant-server.d.ts +5 -0
  200. package/app/mcp/merchant-server.js +12 -0
  201. package/app/mcp/merchant-server.js.map +1 -1
  202. package/app/mcp/procurement-tools.d.ts +10 -0
  203. package/app/mcp/procurement-tools.js +173 -0
  204. package/app/mcp/procurement-tools.js.map +1 -0
  205. package/app/mcp/server.d.ts +4 -0
  206. package/app/mcp/server.js +63 -9
  207. package/app/mcp/server.js.map +1 -1
  208. package/app/mcp/tools.d.ts +4 -1
  209. package/app/mcp/tools.js +83 -28
  210. package/app/mcp/tools.js.map +1 -1
  211. package/app/mcp/types.d.ts +1 -0
  212. package/app/mcp/types.js.map +1 -1
  213. package/app/merchant/application/actor.d.ts +3 -1
  214. package/app/merchant/application/actor.js +21 -0
  215. package/app/merchant/application/actor.js.map +1 -1
  216. package/app/merchant/negotiation-ops.d.ts +2 -0
  217. package/app/merchant/negotiation-ops.js +4 -0
  218. package/app/merchant/negotiation-ops.js.map +1 -1
  219. package/app/merchant/weixin-channel.d.ts +68 -0
  220. package/app/merchant/weixin-channel.js +313 -0
  221. package/app/merchant/weixin-channel.js.map +1 -0
  222. package/app/merchant/workbench-server.d.ts +1 -0
  223. package/app/merchant/workbench-server.js +189 -2
  224. package/app/merchant/workbench-server.js.map +1 -1
  225. package/app/merchant-core/budget/background-budget.d.ts +132 -0
  226. package/app/merchant-core/budget/background-budget.js +308 -0
  227. package/app/merchant-core/budget/background-budget.js.map +1 -0
  228. package/app/merchant-core/collection/alipay-store.d.ts +22 -0
  229. package/app/merchant-core/collection/alipay-store.js +125 -0
  230. package/app/merchant-core/collection/alipay-store.js.map +1 -0
  231. package/app/merchant-core/commercial/calculate.js +6 -6
  232. package/app/merchant-core/commercial/calculate.js.map +1 -1
  233. package/app/merchant-core/context-governance/actor-context.d.ts +56 -0
  234. package/app/merchant-core/context-governance/actor-context.js +76 -0
  235. package/app/merchant-core/context-governance/actor-context.js.map +1 -0
  236. package/app/merchant-core/context-governance/authority.d.ts +39 -0
  237. package/app/merchant-core/context-governance/authority.js +28 -0
  238. package/app/merchant-core/context-governance/authority.js.map +1 -0
  239. package/app/merchant-core/context-governance/context-guard.d.ts +104 -0
  240. package/app/merchant-core/context-governance/context-guard.js +194 -0
  241. package/app/merchant-core/context-governance/context-guard.js.map +1 -0
  242. package/app/merchant-core/context-governance/data-use-grant.d.ts +91 -0
  243. package/app/merchant-core/context-governance/data-use-grant.js +211 -0
  244. package/app/merchant-core/context-governance/data-use-grant.js.map +1 -0
  245. package/app/merchant-core/context-governance/feature-flags.d.ts +32 -0
  246. package/app/merchant-core/context-governance/feature-flags.js +83 -0
  247. package/app/merchant-core/context-governance/feature-flags.js.map +1 -0
  248. package/app/merchant-core/context-governance/pilot-readiness.d.ts +39 -0
  249. package/app/merchant-core/context-governance/pilot-readiness.js +141 -0
  250. package/app/merchant-core/context-governance/pilot-readiness.js.map +1 -0
  251. package/app/merchant-core/context-governance/source-governance.d.ts +157 -0
  252. package/app/merchant-core/context-governance/source-governance.js +492 -0
  253. package/app/merchant-core/context-governance/source-governance.js.map +1 -0
  254. package/app/merchant-core/context-governance/types.d.ts +62 -0
  255. package/app/merchant-core/context-governance/types.js +92 -0
  256. package/app/merchant-core/context-governance/types.js.map +1 -0
  257. package/app/merchant-core/customer-delivery/binding.d.ts +55 -0
  258. package/app/merchant-core/customer-delivery/binding.js +121 -0
  259. package/app/merchant-core/customer-delivery/binding.js.map +1 -0
  260. package/app/merchant-core/customer-delivery/channel.d.ts +75 -0
  261. package/app/merchant-core/customer-delivery/channel.js +157 -0
  262. package/app/merchant-core/customer-delivery/channel.js.map +1 -0
  263. package/app/merchant-core/customer-delivery/consent-repository.d.ts +50 -0
  264. package/app/merchant-core/customer-delivery/consent-repository.js +160 -0
  265. package/app/merchant-core/customer-delivery/consent-repository.js.map +1 -0
  266. package/app/merchant-core/customer-delivery/dispatcher-loop.d.ts +68 -0
  267. package/app/merchant-core/customer-delivery/dispatcher-loop.js +132 -0
  268. package/app/merchant-core/customer-delivery/dispatcher-loop.js.map +1 -0
  269. package/app/merchant-core/customer-delivery/dispatcher.d.ts +118 -0
  270. package/app/merchant-core/customer-delivery/dispatcher.js +166 -0
  271. package/app/merchant-core/customer-delivery/dispatcher.js.map +1 -0
  272. package/app/merchant-core/customer-delivery/email-adapter.d.ts +31 -0
  273. package/app/merchant-core/customer-delivery/email-adapter.js +90 -0
  274. package/app/merchant-core/customer-delivery/email-adapter.js.map +1 -0
  275. package/app/merchant-core/customer-delivery/gate.d.ts +37 -0
  276. package/app/merchant-core/customer-delivery/gate.js +196 -0
  277. package/app/merchant-core/customer-delivery/gate.js.map +1 -0
  278. package/app/merchant-core/customer-delivery/reconciliation.d.ts +13 -0
  279. package/app/merchant-core/customer-delivery/reconciliation.js +70 -0
  280. package/app/merchant-core/customer-delivery/reconciliation.js.map +1 -0
  281. package/app/merchant-core/customer-delivery/repository.d.ts +55 -0
  282. package/app/merchant-core/customer-delivery/repository.js +235 -0
  283. package/app/merchant-core/customer-delivery/repository.js.map +1 -0
  284. package/app/merchant-core/customer-delivery/smtp-adapter.d.ts +30 -0
  285. package/app/merchant-core/customer-delivery/smtp-adapter.js +183 -0
  286. package/app/merchant-core/customer-delivery/smtp-adapter.js.map +1 -0
  287. package/app/merchant-core/customer-delivery/stream-gate.d.ts +45 -0
  288. package/app/merchant-core/customer-delivery/stream-gate.js +123 -0
  289. package/app/merchant-core/customer-delivery/stream-gate.js.map +1 -0
  290. package/app/merchant-core/customer-delivery/types.d.ts +103 -0
  291. package/app/merchant-core/customer-delivery/types.js +32 -0
  292. package/app/merchant-core/customer-delivery/types.js.map +1 -0
  293. package/app/merchant-core/customer-delivery/weixin-adapter.d.ts +35 -0
  294. package/app/merchant-core/customer-delivery/weixin-adapter.js +56 -0
  295. package/app/merchant-core/customer-delivery/weixin-adapter.js.map +1 -0
  296. package/app/merchant-core/customer-memory/auto-identity.d.ts +43 -0
  297. package/app/merchant-core/customer-memory/auto-identity.js +163 -0
  298. package/app/merchant-core/customer-memory/auto-identity.js.map +1 -0
  299. package/app/merchant-core/customer-memory/grants.d.ts +72 -0
  300. package/app/merchant-core/customer-memory/grants.js +224 -0
  301. package/app/merchant-core/customer-memory/grants.js.map +1 -0
  302. package/app/merchant-core/customer-memory/history.d.ts +51 -0
  303. package/app/merchant-core/customer-memory/history.js +252 -0
  304. package/app/merchant-core/customer-memory/history.js.map +1 -0
  305. package/app/merchant-core/customer-memory/identity.d.ts +66 -0
  306. package/app/merchant-core/customer-memory/identity.js +249 -0
  307. package/app/merchant-core/customer-memory/identity.js.map +1 -0
  308. package/app/merchant-core/customer-memory/maintenance.d.ts +51 -0
  309. package/app/merchant-core/customer-memory/maintenance.js +67 -0
  310. package/app/merchant-core/customer-memory/maintenance.js.map +1 -0
  311. package/app/merchant-core/customer-memory/memory.d.ts +119 -0
  312. package/app/merchant-core/customer-memory/memory.js +502 -0
  313. package/app/merchant-core/customer-memory/memory.js.map +1 -0
  314. package/app/merchant-core/customer-memory/private-transport.d.ts +10 -0
  315. package/app/merchant-core/customer-memory/private-transport.js +108 -0
  316. package/app/merchant-core/customer-memory/private-transport.js.map +1 -0
  317. package/app/merchant-core/customer-memory/quote-history.d.ts +160 -0
  318. package/app/merchant-core/customer-memory/quote-history.js +392 -0
  319. package/app/merchant-core/customer-memory/quote-history.js.map +1 -0
  320. package/app/merchant-core/customer-memory/reception-provider.d.ts +69 -0
  321. package/app/merchant-core/customer-memory/reception-provider.js +250 -0
  322. package/app/merchant-core/customer-memory/reception-provider.js.map +1 -0
  323. package/app/merchant-core/customer-memory/reception-writer.d.ts +19 -0
  324. package/app/merchant-core/customer-memory/reception-writer.js +32 -0
  325. package/app/merchant-core/customer-memory/reception-writer.js.map +1 -0
  326. package/app/merchant-core/customer-memory/types.d.ts +111 -0
  327. package/app/merchant-core/customer-memory/types.js +58 -0
  328. package/app/merchant-core/customer-memory/types.js.map +1 -0
  329. package/app/merchant-core/followup/repository.d.ts +2 -1
  330. package/app/merchant-core/followup/repository.js +29 -23
  331. package/app/merchant-core/followup/repository.js.map +1 -1
  332. package/app/merchant-core/learning/evaluation-fixtures.d.ts +38 -0
  333. package/app/merchant-core/learning/evaluation-fixtures.js +83 -0
  334. package/app/merchant-core/learning/evaluation-fixtures.js.map +1 -0
  335. package/app/merchant-core/learning/evaluation-suite-v1.d.ts +56 -0
  336. package/app/merchant-core/learning/evaluation-suite-v1.js +129 -0
  337. package/app/merchant-core/learning/evaluation-suite-v1.js.map +1 -0
  338. package/app/merchant-core/learning/evaluation.d.ts +84 -0
  339. package/app/merchant-core/learning/evaluation.js +581 -0
  340. package/app/merchant-core/learning/evaluation.js.map +1 -0
  341. package/app/merchant-core/learning/initial-skills.d.ts +36 -0
  342. package/app/merchant-core/learning/initial-skills.js +67 -0
  343. package/app/merchant-core/learning/initial-skills.js.map +1 -0
  344. package/app/merchant-core/learning/precheck.d.ts +33 -0
  345. package/app/merchant-core/learning/precheck.js +56 -0
  346. package/app/merchant-core/learning/precheck.js.map +1 -0
  347. package/app/merchant-core/learning/real-evaluation-runner.d.ts +52 -0
  348. package/app/merchant-core/learning/real-evaluation-runner.js +167 -0
  349. package/app/merchant-core/learning/real-evaluation-runner.js.map +1 -0
  350. package/app/merchant-core/learning/reception-skill-catalog.d.ts +39 -0
  351. package/app/merchant-core/learning/reception-skill-catalog.js +145 -0
  352. package/app/merchant-core/learning/reception-skill-catalog.js.map +1 -0
  353. package/app/merchant-core/learning/recovery-gateway.d.ts +27 -0
  354. package/app/merchant-core/learning/recovery-gateway.js +106 -0
  355. package/app/merchant-core/learning/recovery-gateway.js.map +1 -0
  356. package/app/merchant-core/learning/repository.d.ts +130 -0
  357. package/app/merchant-core/learning/repository.js +588 -0
  358. package/app/merchant-core/learning/repository.js.map +1 -0
  359. package/app/merchant-core/learning/service.d.ts +262 -0
  360. package/app/merchant-core/learning/service.js +1498 -0
  361. package/app/merchant-core/learning/service.js.map +1 -0
  362. package/app/merchant-core/learning/test-dispatch-gateway.d.ts +43 -0
  363. package/app/merchant-core/learning/test-dispatch-gateway.js +145 -0
  364. package/app/merchant-core/learning/test-dispatch-gateway.js.map +1 -0
  365. package/app/merchant-core/learning/types.d.ts +217 -0
  366. package/app/merchant-core/learning/types.js +52 -0
  367. package/app/merchant-core/learning/types.js.map +1 -0
  368. package/app/merchant-core/migrations/ledgers.d.ts +40 -0
  369. package/app/merchant-core/migrations/ledgers.js +824 -0
  370. package/app/merchant-core/migrations/ledgers.js.map +1 -0
  371. package/app/merchant-core/migrations/runner.d.ts +55 -0
  372. package/app/merchant-core/migrations/runner.js +114 -0
  373. package/app/merchant-core/migrations/runner.js.map +1 -0
  374. package/app/merchant-core/operating/repository.d.ts +0 -1
  375. package/app/merchant-core/operating/repository.js +2 -17
  376. package/app/merchant-core/operating/repository.js.map +1 -1
  377. package/app/merchant-core/operations-jobs/advice-runner.d.ts +63 -0
  378. package/app/merchant-core/operations-jobs/advice-runner.js +199 -0
  379. package/app/merchant-core/operations-jobs/advice-runner.js.map +1 -0
  380. package/app/merchant-core/operations-jobs/pilot-evidence.d.ts +64 -0
  381. package/app/merchant-core/operations-jobs/pilot-evidence.js +109 -0
  382. package/app/merchant-core/operations-jobs/pilot-evidence.js.map +1 -0
  383. package/app/merchant-core/operations-jobs/pilot-metrics.d.ts +70 -0
  384. package/app/merchant-core/operations-jobs/pilot-metrics.js +98 -0
  385. package/app/merchant-core/operations-jobs/pilot-metrics.js.map +1 -0
  386. package/app/merchant-core/operations-jobs/projection.d.ts +41 -0
  387. package/app/merchant-core/operations-jobs/projection.js +158 -0
  388. package/app/merchant-core/operations-jobs/projection.js.map +1 -0
  389. package/app/merchant-core/operations-jobs/scheduler.d.ts +68 -0
  390. package/app/merchant-core/operations-jobs/scheduler.js +233 -0
  391. package/app/merchant-core/operations-jobs/scheduler.js.map +1 -0
  392. package/app/merchant-core/operations-jobs/service.d.ts +260 -0
  393. package/app/merchant-core/operations-jobs/service.js +922 -0
  394. package/app/merchant-core/operations-jobs/service.js.map +1 -0
  395. package/app/merchant-core/opportunity/repository.d.ts +0 -1
  396. package/app/merchant-core/opportunity/repository.js +2 -17
  397. package/app/merchant-core/opportunity/repository.js.map +1 -1
  398. package/app/merchant-core/ops/facade.d.ts +10 -0
  399. package/app/merchant-core/ops/facade.js +52 -0
  400. package/app/merchant-core/ops/facade.js.map +1 -1
  401. package/app/merchant-core/ops/schema.d.ts +1 -1
  402. package/app/merchant-core/ops/schema.js +119 -2
  403. package/app/merchant-core/ops/schema.js.map +1 -1
  404. package/app/merchant-core/ops/surface.d.ts +37 -0
  405. package/app/merchant-core/ops/surface.js +240 -0
  406. package/app/merchant-core/ops/surface.js.map +1 -1
  407. package/app/merchant-core/orders/model.d.ts +1 -8
  408. package/app/merchant-core/orders/model.js +10 -2
  409. package/app/merchant-core/orders/model.js.map +1 -1
  410. package/app/merchant-core/orders/repository.d.ts +0 -1
  411. package/app/merchant-core/orders/repository.js +3 -18
  412. package/app/merchant-core/orders/repository.js.map +1 -1
  413. package/app/merchant-core/orders/service.js +34 -8
  414. package/app/merchant-core/orders/service.js.map +1 -1
  415. package/app/merchant-core/ownership/instance-ownership.d.ts +122 -0
  416. package/app/merchant-core/ownership/instance-ownership.js +236 -0
  417. package/app/merchant-core/ownership/instance-ownership.js.map +1 -0
  418. package/app/merchant-core/ownership/runtime.d.ts +175 -0
  419. package/app/merchant-core/ownership/runtime.js +422 -0
  420. package/app/merchant-core/ownership/runtime.js.map +1 -0
  421. package/app/merchant-core/proposal/engine.js +2 -0
  422. package/app/merchant-core/proposal/engine.js.map +1 -1
  423. package/app/merchant-core/proposal/publish.d.ts +0 -6
  424. package/app/merchant-core/proposal/publish.js +37 -3
  425. package/app/merchant-core/proposal/publish.js.map +1 -1
  426. package/app/merchant-core/proposal/repository.d.ts +0 -7
  427. package/app/merchant-core/proposal/repository.js +8 -17
  428. package/app/merchant-core/proposal/repository.js.map +1 -1
  429. package/app/merchant-core/proposal/types.d.ts +1 -0
  430. package/app/merchant-core/proposal/types.js.map +1 -1
  431. package/app/merchant-core/rfq/policy.d.ts +1 -0
  432. package/app/merchant-core/rfq/policy.js +6 -7
  433. package/app/merchant-core/rfq/policy.js.map +1 -1
  434. package/app/merchant-core/rfq/repository.d.ts +0 -1
  435. package/app/merchant-core/rfq/repository.js +2 -17
  436. package/app/merchant-core/rfq/repository.js.map +1 -1
  437. package/app/merchant-core/rfq/service.d.ts +2 -1
  438. package/app/merchant-core/rfq/service.js +2 -4
  439. package/app/merchant-core/rfq/service.js.map +1 -1
  440. package/app/merchant-core/storage/transaction.d.ts +95 -7
  441. package/app/merchant-core/storage/transaction.js +251 -2
  442. package/app/merchant-core/storage/transaction.js.map +1 -1
  443. package/app/merchant-credential-rotation.d.ts +4 -1
  444. package/app/merchant-credential-rotation.js +12 -8
  445. package/app/merchant-credential-rotation.js.map +1 -1
  446. package/app/merchant-runtime/manager.d.ts +2 -0
  447. package/app/merchant-runtime/manager.js +5 -0
  448. package/app/merchant-runtime/manager.js.map +1 -1
  449. package/app/negotiation/domain/objects.d.ts +2 -0
  450. package/app/negotiation/domain/objects.js +3 -0
  451. package/app/negotiation/domain/objects.js.map +1 -1
  452. package/app/negotiation/ledger/store.d.ts +4 -0
  453. package/app/negotiation/ledger/store.js +20 -0
  454. package/app/negotiation/ledger/store.js.map +1 -1
  455. package/app/product-init.d.ts +1 -0
  456. package/app/product-init.js +4 -1
  457. package/app/product-init.js.map +1 -1
  458. package/app/trust/identity/message-signature.d.ts +1 -1
  459. package/app/trust/identity/message-signature.js +6 -1
  460. package/app/trust/identity/message-signature.js.map +1 -1
  461. package/app/trust/identity/nonce.d.ts +5 -0
  462. package/app/trust/identity/nonce.js +2 -0
  463. package/app/trust/identity/nonce.js.map +1 -1
  464. package/app/weixin/ilink-client.d.ts +3 -2
  465. package/app/weixin/ilink-client.js +9 -2
  466. package/app/weixin/ilink-client.js.map +1 -1
  467. package/build-manifest.json +1672 -487
  468. package/deployment-lock.json +2 -2
  469. package/package.json +1 -1
  470. package/prepare.mjs +5 -1
  471. package/runtime-package.json +1 -1
  472. package/shipping-sbom.cdx.json +6 -6
  473. package/shipping-source.json +12 -12
@@ -0,0 +1,160 @@
1
+ /**
2
+ * Copyright 2026 harrylabsj
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import { randomUUID } from "node:crypto";
17
+ import { createNotificationSubscription, createRecipientBinding, revokeRecipientBinding, unsubscribe, } from "./binding.js";
18
+ import { DeliveryGateError, } from "./types.js";
19
+ function toBinding(row) {
20
+ return {
21
+ binding_id: row.binding_id,
22
+ merchant_id: row.merchant_id,
23
+ channel: row.channel,
24
+ address_ref: row.address_ref,
25
+ verified_at: row.verified_at,
26
+ revoked_at: row.revoked_at,
27
+ binding_epoch: row.binding_epoch,
28
+ };
29
+ }
30
+ function toSubscription(row) {
31
+ return {
32
+ subscription_id: row.subscription_id,
33
+ merchant_id: row.merchant_id,
34
+ binding_id: row.binding_id,
35
+ purpose: row.purpose,
36
+ max_per_day: row.max_per_day,
37
+ granted_at: row.granted_at,
38
+ expires_at: row.expires_at,
39
+ unsubscribed_at: row.unsubscribed_at,
40
+ grant_epoch: row.grant_epoch,
41
+ };
42
+ }
43
+ export class DeliveryConsentRepository {
44
+ db;
45
+ now;
46
+ constructor(options) {
47
+ this.db = options.db;
48
+ this.now = options.now;
49
+ }
50
+ createBinding(input) {
51
+ const binding = createRecipientBinding({
52
+ binding_id: `bind_${randomUUID()}`,
53
+ merchant_id: input.merchant_id,
54
+ channel: input.channel,
55
+ address_ref: input.address_ref,
56
+ proof_ref: input.proof_ref,
57
+ now: this.now(),
58
+ });
59
+ const stamp = this.now();
60
+ this.db
61
+ .prepare(`INSERT INTO customer_delivery_bindings
62
+ (merchant_id, binding_id, channel, address_ref, verified_at, revoked_at,
63
+ binding_epoch, created_at, updated_at)
64
+ VALUES (?, ?, ?, ?, ?, NULL, ?, ?, ?)`)
65
+ .run(binding.merchant_id, binding.binding_id, binding.channel, binding.address_ref, binding.verified_at, binding.binding_epoch, stamp, stamp);
66
+ return binding;
67
+ }
68
+ getBinding(merchantId, bindingId) {
69
+ const row = this.db
70
+ .prepare("SELECT * FROM customer_delivery_bindings WHERE merchant_id = ? AND binding_id = ?")
71
+ .get(merchantId, bindingId);
72
+ return row === undefined ? undefined : toBinding(row);
73
+ }
74
+ listBindings(merchantId) {
75
+ const rows = this.db
76
+ .prepare("SELECT * FROM customer_delivery_bindings WHERE merchant_id = ? ORDER BY created_at DESC")
77
+ .all(merchantId);
78
+ return rows.map(toBinding);
79
+ }
80
+ revokeBinding(merchantId, bindingId, expectedEpoch) {
81
+ const current = this.getBinding(merchantId, bindingId);
82
+ if (current === undefined) {
83
+ throw new DeliveryGateError("binding_missing", "收件人绑定不存在");
84
+ }
85
+ const revoked = revokeRecipientBinding(current, {
86
+ expected_epoch: expectedEpoch,
87
+ now: this.now(),
88
+ });
89
+ const stamp = this.now();
90
+ const result = this.db
91
+ .prepare(`UPDATE customer_delivery_bindings
92
+ SET revoked_at = ?, binding_epoch = ?, updated_at = ?
93
+ WHERE merchant_id = ? AND binding_id = ? AND binding_epoch = ?`)
94
+ .run(revoked.revoked_at, revoked.binding_epoch, stamp, merchantId, bindingId, expectedEpoch);
95
+ // P2-5:并发撤销时 UPDATE 可能匹配 0 行;不能返回按旧读算出的对象冒充成功。
96
+ if (Number(result.changes) !== 1) {
97
+ throw new DeliveryGateError("epoch_mismatch", "收件人绑定已变化");
98
+ }
99
+ return revoked;
100
+ }
101
+ createSubscription(input) {
102
+ const binding = this.getBinding(input.merchant_id, input.binding_id);
103
+ if (binding === undefined) {
104
+ throw new DeliveryGateError("binding_missing", "收件人绑定不存在");
105
+ }
106
+ const subscription = createNotificationSubscription({
107
+ subscription_id: `sub_${randomUUID()}`,
108
+ binding,
109
+ purpose: input.purpose,
110
+ max_per_day: input.max_per_day,
111
+ expires_at: input.expires_at,
112
+ consent_evidence_ref: input.consent_evidence_ref,
113
+ now: this.now(),
114
+ });
115
+ const stamp = this.now();
116
+ this.db
117
+ .prepare(`INSERT INTO customer_delivery_subscriptions
118
+ (merchant_id, subscription_id, binding_id, purpose, max_per_day, granted_at,
119
+ expires_at, unsubscribed_at, grant_epoch, consent_evidence_ref, created_at, updated_at)
120
+ VALUES (?, ?, ?, ?, ?, ?, ?, NULL, ?, ?, ?, ?)`)
121
+ .run(subscription.merchant_id, subscription.subscription_id, subscription.binding_id, subscription.purpose, subscription.max_per_day, subscription.granted_at, subscription.expires_at, subscription.grant_epoch, input.consent_evidence_ref, stamp, stamp);
122
+ return subscription;
123
+ }
124
+ getSubscription(merchantId, subscriptionId) {
125
+ const row = this.db
126
+ .prepare("SELECT * FROM customer_delivery_subscriptions WHERE merchant_id = ? AND subscription_id = ?")
127
+ .get(merchantId, subscriptionId);
128
+ return row === undefined ? undefined : toSubscription(row);
129
+ }
130
+ listSubscriptions(merchantId, bindingId) {
131
+ const rows = (bindingId === undefined
132
+ ? this.db
133
+ .prepare("SELECT * FROM customer_delivery_subscriptions WHERE merchant_id = ? ORDER BY created_at DESC")
134
+ .all(merchantId)
135
+ : this.db
136
+ .prepare("SELECT * FROM customer_delivery_subscriptions WHERE merchant_id = ? AND binding_id = ? ORDER BY created_at DESC")
137
+ .all(merchantId, bindingId));
138
+ return rows.map(toSubscription);
139
+ }
140
+ unsubscribe(merchantId, subscriptionId, expectedEpoch) {
141
+ const current = this.getSubscription(merchantId, subscriptionId);
142
+ if (current === undefined) {
143
+ throw new DeliveryGateError("subscription_missing", "订阅不存在");
144
+ }
145
+ const updated = unsubscribe(current, {
146
+ expected_epoch: expectedEpoch,
147
+ now: this.now(),
148
+ });
149
+ const result = this.db
150
+ .prepare(`UPDATE customer_delivery_subscriptions
151
+ SET unsubscribed_at = ?, grant_epoch = ?, updated_at = ?
152
+ WHERE merchant_id = ? AND subscription_id = ? AND grant_epoch = ?`)
153
+ .run(updated.unsubscribed_at, updated.grant_epoch, this.now(), merchantId, subscriptionId, expectedEpoch);
154
+ if (Number(result.changes) !== 1) {
155
+ throw new DeliveryGateError("epoch_mismatch", "目的授权已变化");
156
+ }
157
+ return updated;
158
+ }
159
+ }
160
+ //# sourceMappingURL=consent-repository.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"consent-repository.js","sourceRoot":"","sources":["../../../src/merchant-core/customer-delivery/consent-repository.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AASH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,OAAO,EACL,8BAA8B,EAC9B,sBAAsB,EACtB,sBAAsB,EACtB,WAAW,GACZ,MAAM,cAAc,CAAC;AACtB,OAAO,EACL,iBAAiB,GAKlB,MAAM,YAAY,CA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AE;aACnB,OAAO,CACN;;4EAEoE,CACrE;aACA,GAAG,CACF,OAAO,CAAC,eAAe,EACvB,OAAO,CAAC,WAAW,EACnB,IAAI,CAAC,GAAG,EAAE,EACV,UAAU,EACV,cAAc,EACd,aAAa,CACd,CAAC;QACJ,IAAI,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;YACjC,MAAM,IAAI,iBAAiB,CAAC,gBAAgB,EAAE,SAAS,CAAC,CAAC;QAC3D,CAAC;QACD,OAAO,OAAO,CAAC;IACjB,CAAC;CACF"}
@@ -0,0 +1,68 @@
1
+ /**
2
+ * Copyright 2026 harrylabsj
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ /**
17
+ * P1 投递运行时(M6-6):在治理所有权持有进程内定期领取 PENDING 通知并发送。
18
+ *
19
+ * 单流在途、UNKNOWN 阻塞、accepted≠read 均由 `dispatchDelivery`/仓库保证;
20
+ * 本循环只做调度与渠道选择。无对应适配器时保留 PENDING,不算失败。
21
+ */
22
+ import type { DeliveryEvidence } from "./reconciliation.js";
23
+ import type { DeliveryChannelAdapter } from "./channel.js";
24
+ import type { DeliveryRecord, DeliveryStore } from "./dispatcher.js";
25
+ export interface CustomerDeliveryRepositoryLike extends DeliveryStore {
26
+ listDue(merchantId: string, now: string, limit?: number): DeliveryRecord[];
27
+ recoverExpiredClaims?(merchantId: string, now: string): number;
28
+ listByStatus?(merchantId: string, status: "UNKNOWN", limit: number, offset?: number): DeliveryRecord[];
29
+ countUnknown?(merchantId: string): number;
30
+ reconcileUnknown?(record: DeliveryRecord, evidence: DeliveryEvidence): boolean;
31
+ }
32
+ export interface DeliveryDispatcherLoopOptions {
33
+ readonly store: CustomerDeliveryRepositoryLike;
34
+ readonly reconcile?: (record: DeliveryRecord) => Promise<DeliveryEvidence | undefined>;
35
+ readonly streamGate?: import("./stream-gate.js").StreamGate;
36
+ readonly merchant_id: string;
37
+ readonly adapterFor: (delivery: DeliveryRecord) => DeliveryChannelAdapter | undefined;
38
+ /** 发送时复核(P1-4):重读绑定/订阅/频控;抛错则不领取。 */
39
+ readonly authorize?: (delivery: DeliveryRecord) => void;
40
+ /** 按授权绑定解析收件人(P1-5);缺省时适配器必须自行拒绝发送。 */
41
+ readonly resolveRecipient?: (delivery: DeliveryRecord) => string | undefined;
42
+ readonly now: () => string;
43
+ readonly assertOwner?: () => void;
44
+ readonly write?: <T>(work: () => T) => T;
45
+ readonly worker_id?: string;
46
+ readonly interval_ms?: number;
47
+ readonly lease_ms?: number;
48
+ readonly timeout_ms?: number;
49
+ readonly max_per_tick?: number;
50
+ readonly log?: (line: string) => void;
51
+ }
52
+ export declare class DeliveryDispatcherLoop {
53
+ private readonly options;
54
+ private timer;
55
+ /** 重入互斥:SetInterval tick 不重叠(P1-2)。 */
56
+ private ticking;
57
+ private reconciliationOffset;
58
+ private nextUnknownReportAt;
59
+ constructor(options: DeliveryDispatcherLoopOptions);
60
+ start(): void;
61
+ stop(): void;
62
+ tick(): Promise<{
63
+ claimed: number;
64
+ sent: number;
65
+ unknown: number;
66
+ skipped: number;
67
+ }>;
68
+ }
@@ -0,0 +1,132 @@
1
+ /**
2
+ * Copyright 2026 harrylabsj
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import { dispatchDelivery } from "./channel.js";
17
+ export class DeliveryDispatcherLoop {
18
+ options;
19
+ timer;
20
+ /** 重入互斥:SetInterval tick 不重叠(P1-2)。 */
21
+ ticking = false;
22
+ reconciliationOffset = 0;
23
+ nextUnknownReportAt = 0;
24
+ constructor(options) {
25
+ this.options = options;
26
+ }
27
+ start() {
28
+ if (this.timer !== undefined)
29
+ return;
30
+ this.timer = setInterval(() => {
31
+ void this.tick().catch((err) => {
32
+ this.options.log?.(`[delivery dispatcher] tick failed: ${err instanceof Error ? err.message : String(err)}\n`);
33
+ });
34
+ }, this.options.interval_ms ?? 15_000);
35
+ this.timer.unref?.();
36
+ }
37
+ stop() {
38
+ if (this.timer !== undefined) {
39
+ clearInterval(this.timer);
40
+ this.timer = undefined;
41
+ }
42
+ }
43
+ async tick() {
44
+ const result = { claimed: 0, sent: 0, unknown: 0, skipped: 0 };
45
+ // 上一 tick 的渠道调用仍挂起时直接跳过,避免同一循环双重领取。
46
+ if (this.ticking)
47
+ return result;
48
+ this.ticking = true;
49
+ try {
50
+ this.options.assertOwner?.();
51
+ const worker = this.options.worker_id ?? `delivery:${process.pid}`;
52
+ const now = this.options.now();
53
+ const recover = () => this.options.store.recoverExpiredClaims?.(this.options.merchant_id, now) ?? 0;
54
+ result.unknown += this.options.write === undefined ? recover() : this.options.write(recover);
55
+ if (result.unknown > 0) {
56
+ this.options.log?.(`[delivery dispatcher] ${result.unknown} interrupted deliveries require reconciliation\n`);
57
+ }
58
+ if (this.options.reconcile !== undefined) {
59
+ const batchSize = 5;
60
+ const merchantId = this.options.merchant_id;
61
+ const unknown = this.options.store.listByStatus?.(merchantId, "UNKNOWN", batchSize, this.reconciliationOffset) ?? [];
62
+ let settled = 0;
63
+ for (const record of unknown) {
64
+ const evidence = await this.options.reconcile(record);
65
+ if (evidence === undefined)
66
+ continue;
67
+ const settle = () => this.options.store.reconcileUnknown?.(record, evidence) ?? false;
68
+ if (this.options.write === undefined ? settle() : this.options.write(settle))
69
+ settled += 1;
70
+ }
71
+ this.reconciliationOffset = unknown.length < batchSize ? 0 : this.reconciliationOffset + unknown.length - settled;
72
+ }
73
+ if (Date.parse(now) >= this.nextUnknownReportAt) {
74
+ this.nextUnknownReportAt = Date.parse(now) + 60 * 60 * 1000;
75
+ const unknownCount = this.options.store.countUnknown?.(this.options.merchant_id) ?? 0;
76
+ if (unknownCount > 0) {
77
+ this.options.log?.(`[delivery dispatcher] ${unknownCount} deliveries awaiting verification\n`);
78
+ }
79
+ }
80
+ const due = this.options.store.listDue(this.options.merchant_id, now, this.options.max_per_tick ?? 10);
81
+ for (const record of due) {
82
+ const adapter = this.options.adapterFor(record);
83
+ if (adapter === undefined) {
84
+ result.skipped += 1;
85
+ continue;
86
+ }
87
+ const run = () => dispatchDelivery({
88
+ store: this.options.store,
89
+ merchant_id: this.options.merchant_id,
90
+ delivery_id: record.delivery_id,
91
+ worker_id: worker,
92
+ lease_ms: this.options.lease_ms ?? 120_000,
93
+ timeout_ms: this.options.timeout_ms ?? 60_000,
94
+ now: this.options.now(),
95
+ clock: this.options.now,
96
+ adapter,
97
+ ...(this.options.streamGate !== undefined ? { stream_gate: this.options.streamGate } : {}),
98
+ ...(this.options.write !== undefined ? { write: this.options.write } : {}),
99
+ ...(this.options.authorize !== undefined ? { authorize: this.options.authorize } : {}),
100
+ ...(this.options.resolveRecipient !== undefined
101
+ ? { resolveRecipient: this.options.resolveRecipient }
102
+ : {}),
103
+ });
104
+ try {
105
+ // 渠道调用在领取和结算事务之间执行。
106
+ const sent = await run();
107
+ result.claimed += 1;
108
+ if (sent.status === "UNKNOWN") {
109
+ result.unknown += 1;
110
+ this.options.log?.(`[delivery dispatcher] ${sent.delivery_id} requires reconciliation\n`);
111
+ }
112
+ else if (sent.status === "SENT")
113
+ result.sent += 1;
114
+ }
115
+ catch (err) {
116
+ const code = err?.code;
117
+ if (code === "not_acquired" || code === "feature_disabled") {
118
+ this.stop();
119
+ break;
120
+ }
121
+ result.skipped += 1;
122
+ this.options.log?.(`[delivery dispatcher] ${record.delivery_id}: ${err instanceof Error ? err.message : String(err)}\n`);
123
+ }
124
+ }
125
+ return result;
126
+ }
127
+ finally {
128
+ this.ticking = false;
129
+ }
130
+ }
131
+ }
132
+ //# sourceMappingURL=dispatcher-loop.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dispatcher-loop.js","sourceRoot":"","sources":["../../../src/merchant-core/customer-delivery/dispatcher-loop.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAYH,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AA+BhD,MAAM,OAAO,sBAAsB;IAChB,OAAO,CAAgC;IAChD,KAAK,CAA6C;IAC1D,uCAAuC;IAC/B,OAAO,GAAG,KAAK,CAAC;IAChB,oBAAoB,GAAG,CAAC,CAAC;IACzB,mBAAmB,GAAG,CAAC,CAAC;IAEhC,YAAY,OAAsC;QAChD,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACzB,CAAC;IAED,KAAK;QACH,IAAI,IAAI,CAAC,KAAK,KAAK,SAAS;YAAE,OAAO;QACrC,IAAI,CAAC,KAAK,GAAG,WAAW,CAAC,GAAG,EAAE;YAC5B,KAAK,IAAI,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,GAAY,EAAE,EAAE;gBACtC,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,sCAAsC,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;YACjH,CAAC,CAAC,CAAC;QACL,CAAC,EAAE,IAAI,CAAC,OAAO,CAAC,WAAW,IAAI,MAAM,CAAC,CAAC;QACvC,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,EAAE,CAAC;IACvB,CAAC;IA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ACR,CAAC;oBACD,MAAM,CAAC,OAAO,IAAI,CAAC,CAAC;oBACpB,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,yBAAyB,MAAM,CAAC,WAAW,KAAK,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;gBAC3H,CAAC;YACH,CAAC;YACD,OAAO,MAAM,CAAC;QAChB,CAAC;gBAAS,CAAC;YACT,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC;QACvB,CAAC;IACH,CAAC;CACF"}
@@ -0,0 +1,118 @@
1
+ /**
2
+ * Copyright 2026 harrylabsj
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ /**
17
+ * P1 发送领取/回执/未知结果(设计 §16.3 / C13–C14)。
18
+ *
19
+ * 与持久化的关系由 `DeliveryStore` 抽象;本文件只定义状态迁移,便于用
20
+ * 内存存储做确定性验收。关键不变量:
21
+ * - 同一 stream 只允许一条在途(PENDING 且已领取,或 UNKNOWN 未核对)。
22
+ * - accepted ≠ read;read 必须另有证据。
23
+ * - 发送开始后结果不明进入 UNKNOWN 并阻塞该流,不得换 ID 重做。
24
+ * - 只有渠道明确支持同 ID 幂等恢复且已证实未执行,才能按原 ID 重试。
25
+ */
26
+ import type { DeliveryStatus, PreparedDelivery } from "./types.js";
27
+ export interface DeliveryRecord extends PreparedDelivery {
28
+ readonly status: DeliveryStatus;
29
+ readonly lease_owner?: string;
30
+ readonly lease_expires_at?: string;
31
+ readonly started_at?: string;
32
+ /** 已尝试发送次数(含当前在途)。 */
33
+ readonly attempts?: number;
34
+ /** not_sent 退避:早于该时刻不重新领取。 */
35
+ readonly next_attempt_at?: string;
36
+ readonly provider_request_id?: string;
37
+ readonly receipt?: Readonly<Record<string, unknown>>;
38
+ readonly accepted_at?: string;
39
+ readonly read_at?: string;
40
+ readonly read_evidence_ref?: string;
41
+ readonly blocked_reason?: string;
42
+ }
43
+ export interface DeliveryStore {
44
+ get(merchantId: string, deliveryId: string): DeliveryRecord | undefined;
45
+ /** 返回该 stream 上已在途/未知的记录(不含当前 delivery)。 */
46
+ findBlocking(merchantId: string, streamKey: string, deliveryId: string): DeliveryRecord | undefined;
47
+ save(record: DeliveryRecord): void;
48
+ /** 持久存储可提供写事务;领取/复核在同一 IMMEDIATE 事务内执行。 */
49
+ transaction?<T>(work: () => T): T;
50
+ }
51
+ export type DeliveryDispatchErrorCode = "not_found" | "not_pending" | "stream_busy" | "lease_lost" | "lease_busy" | "backoff" | "expired" | "invalid_transition" | "read_requires_evidence";
52
+ export declare class DeliveryDispatchError extends Error {
53
+ readonly code: DeliveryDispatchErrorCode;
54
+ constructor(code: DeliveryDispatchErrorCode, message: string);
55
+ }
56
+ export interface ClaimDeliveryInput {
57
+ readonly store: DeliveryStore;
58
+ readonly merchant_id: string;
59
+ readonly delivery_id: string;
60
+ readonly worker_id: string;
61
+ readonly lease_ms: number;
62
+ readonly now: string;
63
+ /**
64
+ * 领取前的发送时复核(P1-4):重读绑定/订阅/授权并抛错阻止发送。
65
+ * 在同一事务内由调用方提供;抛错即不领取。
66
+ */
67
+ readonly authorize?: (record: DeliveryRecord) => void;
68
+ }
69
+ /**
70
+ * 领取一条 PENDING 通知;同 stream 已有在途/UNKNOWN 时拒绝。
71
+ * 已开始外发的记录须先核对上一轮结果。
72
+ */
73
+ export declare function claimDelivery(input: ClaimDeliveryInput): DeliveryRecord;
74
+ /**
75
+ * not_sent 释放:必须仍持有未过期租约(P2-1)。租约已被其他 worker 接管时
76
+ * 抛 `lease_lost`,不得把对方在途记录无条件重置为 PENDING。
77
+ */
78
+ export declare function releaseDelivery(input: {
79
+ store: DeliveryStore;
80
+ merchant_id: string;
81
+ delivery_id: string;
82
+ worker_id: string;
83
+ now: string;
84
+ reason: string;
85
+ next_attempt_at?: string;
86
+ }): DeliveryRecord;
87
+ export interface CompleteDeliveryInput {
88
+ readonly store: DeliveryStore;
89
+ readonly merchant_id: string;
90
+ readonly delivery_id: string;
91
+ readonly worker_id: string;
92
+ readonly outcome: "accepted" | "unknown";
93
+ readonly now: string;
94
+ readonly provider_request_id?: string;
95
+ readonly receipt?: Record<string, unknown>;
96
+ }
97
+ /** 写入渠道结果:accepted → SENT;结果不明 → UNKNOWN(保留 ID/载荷)。 */
98
+ export declare function completeDelivery(input: CompleteDeliveryInput): DeliveryRecord;
99
+ /** accepted 只表示目标系统接收;read 必须另有证据。 */
100
+ export declare function markDeliveryRead(input: {
101
+ store: DeliveryStore;
102
+ merchant_id: string;
103
+ delivery_id: string;
104
+ evidence_ref: string;
105
+ now: string;
106
+ }): DeliveryRecord;
107
+ /**
108
+ * UNKNOWN 核对为“确未执行”后按原 ID/原载荷重试;渠道必须支持同 ID 幂等。
109
+ * 返回 PENDING 记录,等待下一次领取。
110
+ */
111
+ export declare function retryUnknownDelivery(input: {
112
+ store: DeliveryStore;
113
+ merchant_id: string;
114
+ delivery_id: string;
115
+ now: string;
116
+ verified_not_executed: boolean;
117
+ channel_idempotent: boolean;
118
+ }): DeliveryRecord;
@@ -0,0 +1,166 @@
1
+ /**
2
+ * Copyright 2026 harrylabsj
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ export class DeliveryDispatchError extends Error {
17
+ code;
18
+ constructor(code, message) {
19
+ super(message);
20
+ this.name = "DeliveryDispatchError";
21
+ this.code = code;
22
+ }
23
+ }
24
+ /**
25
+ * 领取一条 PENDING 通知;同 stream 已有在途/UNKNOWN 时拒绝。
26
+ * 已开始外发的记录须先核对上一轮结果。
27
+ */
28
+ export function claimDelivery(input) {
29
+ const record = requireRecord(input.store, input.merchant_id, input.delivery_id);
30
+ if (record.status !== "PENDING") {
31
+ throw new DeliveryDispatchError("not_pending", `状态 ${record.status} 不能领取`);
32
+ }
33
+ if (Date.parse(record.valid_until) <= Date.parse(input.now)) {
34
+ throw new DeliveryDispatchError("expired", "valid_until 已到,不发送");
35
+ }
36
+ if (!Number.isSafeInteger(input.lease_ms) || input.lease_ms <= 0) {
37
+ throw new DeliveryDispatchError("invalid_transition", "lease_ms 必须是正整数");
38
+ }
39
+ if (record.started_at !== undefined || record.lease_owner !== undefined) {
40
+ throw new DeliveryDispatchError("lease_busy", "该通知已开始派发,须先核对上一轮的发送结果");
41
+ }
42
+ if (record.next_attempt_at !== undefined && Date.parse(record.next_attempt_at) > Date.parse(input.now)) {
43
+ throw new DeliveryDispatchError("backoff", "尚未到重试时间");
44
+ }
45
+ const blocking = input.store.findBlocking(input.merchant_id, record.stream_key, record.delivery_id);
46
+ if (blocking !== undefined) {
47
+ throw new DeliveryDispatchError("stream_busy", "同一发送流已有未完成通知");
48
+ }
49
+ input.authorize?.(record);
50
+ const claimed = {
51
+ ...record,
52
+ lease_owner: input.worker_id,
53
+ lease_expires_at: new Date(Date.parse(input.now) + input.lease_ms).toISOString(),
54
+ started_at: input.now,
55
+ attempts: (record.attempts ?? 0) + 1,
56
+ };
57
+ input.store.save(claimed);
58
+ return claimed;
59
+ }
60
+ /**
61
+ * not_sent 释放:必须仍持有未过期租约(P2-1)。租约已被其他 worker 接管时
62
+ * 抛 `lease_lost`,不得把对方在途记录无条件重置为 PENDING。
63
+ */
64
+ export function releaseDelivery(input) {
65
+ const record = requireRecord(input.store, input.merchant_id, input.delivery_id);
66
+ if (record.status !== "PENDING") {
67
+ throw new DeliveryDispatchError("invalid_transition", `状态 ${record.status} 不能释放`);
68
+ }
69
+ requireLease(record, input.worker_id, input.now);
70
+ const released = {
71
+ ...record,
72
+ lease_owner: undefined,
73
+ lease_expires_at: undefined,
74
+ started_at: undefined,
75
+ blocked_reason: `not_sent:${input.reason}`,
76
+ ...(input.next_attempt_at !== undefined ? { next_attempt_at: input.next_attempt_at } : {}),
77
+ };
78
+ input.store.save(released);
79
+ return released;
80
+ }
81
+ function requireRecord(store, merchantId, deliveryId) {
82
+ const record = store.get(merchantId, deliveryId);
83
+ if (record === undefined)
84
+ throw new DeliveryDispatchError("not_found", "通知不存在");
85
+ return record;
86
+ }
87
+ function requireLease(record, workerId, now) {
88
+ if (record.lease_owner !== workerId) {
89
+ throw new DeliveryDispatchError("lease_lost", "领取者不匹配");
90
+ }
91
+ if (record.lease_expires_at !== undefined && Date.parse(record.lease_expires_at) <= Date.parse(now)) {
92
+ throw new DeliveryDispatchError("lease_lost", "租约已过期");
93
+ }
94
+ }
95
+ /** 写入渠道结果:accepted → SENT;结果不明 → UNKNOWN(保留 ID/载荷)。 */
96
+ export function completeDelivery(input) {
97
+ const record = requireRecord(input.store, input.merchant_id, input.delivery_id);
98
+ if (record.status !== "PENDING") {
99
+ throw new DeliveryDispatchError("invalid_transition", `状态 ${record.status} 不能回执`);
100
+ }
101
+ requireLease(record, input.worker_id, input.now);
102
+ const completed = input.outcome === "accepted"
103
+ ? {
104
+ ...record,
105
+ status: "SENT",
106
+ accepted_at: input.now,
107
+ ...(input.provider_request_id !== undefined
108
+ ? { provider_request_id: input.provider_request_id }
109
+ : {}),
110
+ ...(input.receipt !== undefined ? { receipt: input.receipt } : {}),
111
+ }
112
+ : {
113
+ ...record,
114
+ status: "UNKNOWN",
115
+ ...(input.provider_request_id !== undefined
116
+ ? { provider_request_id: input.provider_request_id }
117
+ : {}),
118
+ ...(input.receipt !== undefined ? { receipt: input.receipt } : {}),
119
+ };
120
+ input.store.save(completed);
121
+ return completed;
122
+ }
123
+ /** accepted 只表示目标系统接收;read 必须另有证据。 */
124
+ export function markDeliveryRead(input) {
125
+ const record = requireRecord(input.store, input.merchant_id, input.delivery_id);
126
+ if (record.status !== "SENT" || record.accepted_at === undefined) {
127
+ throw new DeliveryDispatchError("invalid_transition", "未 accepted 不能标记 read");
128
+ }
129
+ if (input.evidence_ref.trim() === "") {
130
+ throw new DeliveryDispatchError("read_requires_evidence", "read 必须提供渠道证据");
131
+ }
132
+ const read = {
133
+ ...record,
134
+ read_at: input.now,
135
+ read_evidence_ref: input.evidence_ref,
136
+ };
137
+ input.store.save(read);
138
+ return read;
139
+ }
140
+ /**
141
+ * UNKNOWN 核对为“确未执行”后按原 ID/原载荷重试;渠道必须支持同 ID 幂等。
142
+ * 返回 PENDING 记录,等待下一次领取。
143
+ */
144
+ export function retryUnknownDelivery(input) {
145
+ const record = requireRecord(input.store, input.merchant_id, input.delivery_id);
146
+ if (record.status !== "UNKNOWN") {
147
+ throw new DeliveryDispatchError("invalid_transition", `状态 ${record.status} 不能重试`);
148
+ }
149
+ if (!input.verified_not_executed) {
150
+ throw new DeliveryDispatchError("invalid_transition", "未证实未执行,不能重试");
151
+ }
152
+ if (!input.channel_idempotent) {
153
+ throw new DeliveryDispatchError("invalid_transition", "渠道不支持同 ID 幂等恢复");
154
+ }
155
+ const retried = {
156
+ ...record,
157
+ status: "PENDING",
158
+ lease_owner: undefined,
159
+ lease_expires_at: undefined,
160
+ started_at: undefined,
161
+ blocked_reason: undefined,
162
+ };
163
+ input.store.save(retried);
164
+ return retried;
165
+ }
166
+ //# sourceMappingURL=dispatcher.js.map