@harrylabsj/kiwi-merchant-cloud 0.12.31 → 0.12.32

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 (314) hide show
  1. package/app/a2a/node.d.ts +1 -0
  2. package/app/a2a/node.js +170 -2
  3. package/app/a2a/node.js.map +1 -1
  4. package/app/a2a/reception/agent-runtime.d.ts +32 -1
  5. package/app/a2a/reception/agent-runtime.js +420 -11
  6. package/app/a2a/reception/agent-runtime.js.map +1 -1
  7. package/app/a2a/reception/customer-memory-tools.d.ts +73 -0
  8. package/app/a2a/reception/customer-memory-tools.js +17 -0
  9. package/app/a2a/reception/customer-memory-tools.js.map +1 -0
  10. package/app/a2a/reception/engine.d.ts +12 -0
  11. package/app/a2a/reception/engine.js +42 -1
  12. package/app/a2a/reception/engine.js.map +1 -1
  13. package/app/a2a/reception/publish.d.ts +14 -1
  14. package/app/a2a/reception/publish.js +62 -1
  15. package/app/a2a/reception/publish.js.map +1 -1
  16. package/app/a2a/reception/question-spec.d.ts +91 -0
  17. package/app/a2a/reception/question-spec.js +260 -0
  18. package/app/a2a/reception/question-spec.js.map +1 -0
  19. package/app/a2a/reception/reply-outbox.d.ts +6 -0
  20. package/app/a2a/reception/reply-outbox.js +44 -0
  21. package/app/a2a/reception/reply-outbox.js.map +1 -1
  22. package/app/a2a/reception/responder.d.ts +13 -1
  23. package/app/a2a/reception/responder.js +26 -3
  24. package/app/a2a/reception/responder.js.map +1 -1
  25. package/app/a2a/reception/skill-catalog.d.ts +72 -0
  26. package/app/a2a/reception/skill-catalog.js +85 -0
  27. package/app/a2a/reception/skill-catalog.js.map +1 -0
  28. package/app/a2a/reception/skill-contract.d.ts +70 -0
  29. package/app/a2a/reception/skill-contract.js +183 -0
  30. package/app/a2a/reception/skill-contract.js.map +1 -0
  31. package/app/a2a/reception/types.d.ts +12 -0
  32. package/app/a2a/server/customer-identity-gateway.d.ts +111 -0
  33. package/app/a2a/server/customer-identity-gateway.js +441 -0
  34. package/app/a2a/server/customer-identity-gateway.js.map +1 -0
  35. package/app/a2a/server/merchant-handler.d.ts +34 -0
  36. package/app/a2a/server/merchant-handler.js +439 -19
  37. package/app/a2a/server/merchant-handler.js.map +1 -1
  38. package/app/a2a/server/pipeline.js +74 -29
  39. package/app/a2a/server/pipeline.js.map +1 -1
  40. package/app/a2a/server/types.d.ts +2 -0
  41. package/app/a2a/unit-alias.d.ts +3 -3
  42. package/app/a2a/unit-alias.js +9 -5
  43. package/app/a2a/unit-alias.js.map +1 -1
  44. package/app/auth/merchant-sessions.d.ts +3 -0
  45. package/app/auth/merchant-sessions.js +21 -5
  46. package/app/auth/merchant-sessions.js.map +1 -1
  47. package/app/buyer-core/a2a-knp.d.ts +4 -2
  48. package/app/buyer-core/a2a-knp.js +5 -2
  49. package/app/buyer-core/a2a-knp.js.map +1 -1
  50. package/app/buyer-core/a2a-negotiator.d.ts +1 -0
  51. package/app/buyer-core/a2a-negotiator.js +18 -11
  52. package/app/buyer-core/a2a-negotiator.js.map +1 -1
  53. package/app/buyer-core/a2a-quote-fetcher.d.ts +25 -0
  54. package/app/buyer-core/a2a-quote-fetcher.js +82 -4
  55. package/app/buyer-core/a2a-quote-fetcher.js.map +1 -1
  56. package/app/buyer-core/a2a-quote-history.d.ts +94 -0
  57. package/app/buyer-core/a2a-quote-history.js +149 -0
  58. package/app/buyer-core/a2a-quote-history.js.map +1 -0
  59. package/app/buyer-core/build-service.d.ts +19 -0
  60. package/app/buyer-core/build-service.js +30 -1
  61. package/app/buyer-core/build-service.js.map +1 -1
  62. package/app/buyer-core/errors.d.ts +1 -1
  63. package/app/buyer-core/errors.js +1 -0
  64. package/app/buyer-core/errors.js.map +1 -1
  65. package/app/buyer-core/open-inquiry-signer.d.ts +1 -0
  66. package/app/buyer-core/open-inquiry-signer.js +5 -2
  67. package/app/buyer-core/open-inquiry-signer.js.map +1 -1
  68. package/app/buyer-core/protocol-recovery.d.ts +2 -1
  69. package/app/buyer-core/protocol-recovery.js +14 -4
  70. package/app/buyer-core/protocol-recovery.js.map +1 -1
  71. package/app/buyer-core/service.d.ts +57 -5
  72. package/app/buyer-core/service.js +275 -50
  73. package/app/buyer-core/service.js.map +1 -1
  74. package/app/buyer-core/store.d.ts +8 -0
  75. package/app/buyer-core/store.js +37 -7
  76. package/app/buyer-core/store.js.map +1 -1
  77. package/app/cli.js +13 -2
  78. package/app/cli.js.map +1 -1
  79. package/app/cloud/bootstrap.js +141 -7
  80. package/app/cloud/bootstrap.js.map +1 -1
  81. package/app/cloud-sample/products.json +3 -3
  82. package/app/commerce/receipt/dispatcher.d.ts +42 -0
  83. package/app/commerce/receipt/dispatcher.js +68 -0
  84. package/app/commerce/receipt/dispatcher.js.map +1 -0
  85. package/app/commerce/receipt/receipts.d.ts +140 -0
  86. package/app/commerce/receipt/receipts.js +247 -0
  87. package/app/commerce/receipt/receipts.js.map +1 -0
  88. package/app/config/profile.d.ts +2 -2
  89. package/app/handoff/order-receipts.d.ts +62 -0
  90. package/app/handoff/order-receipts.js +71 -0
  91. package/app/handoff/order-receipts.js.map +1 -0
  92. package/app/handoff/ucp-checkout/cart-channel.d.ts +2 -1
  93. package/app/handoff/ucp-checkout/cart-channel.js +1 -0
  94. package/app/handoff/ucp-checkout/cart-channel.js.map +1 -1
  95. package/app/handoff/ucp-checkout/channel.d.ts +6 -0
  96. package/app/handoff/ucp-checkout/channel.js +44 -0
  97. package/app/handoff/ucp-checkout/channel.js.map +1 -1
  98. package/app/http/cli.js +6 -0
  99. package/app/http/cli.js.map +1 -1
  100. package/app/http/merchant-management/api.d.ts +10 -0
  101. package/app/http/merchant-management/api.js +443 -0
  102. package/app/http/merchant-management/api.js.map +1 -1
  103. package/app/http/merchant-management/draft-store.js +8 -6
  104. package/app/http/merchant-management/draft-store.js.map +1 -1
  105. package/app/http/merchant-management/page.js +2 -2
  106. package/app/mcp/cli.js +6 -0
  107. package/app/mcp/cli.js.map +1 -1
  108. package/app/mcp/merchant-runtime-assembly.d.ts +20 -0
  109. package/app/mcp/merchant-runtime-assembly.js +93 -3
  110. package/app/mcp/merchant-runtime-assembly.js.map +1 -1
  111. package/app/mcp/merchant-server.d.ts +5 -0
  112. package/app/mcp/merchant-server.js +12 -0
  113. package/app/mcp/merchant-server.js.map +1 -1
  114. package/app/mcp/tools.js +7 -1
  115. package/app/mcp/tools.js.map +1 -1
  116. package/app/merchant/application/actor.d.ts +1 -1
  117. package/app/merchant/application/actor.js +15 -0
  118. package/app/merchant/application/actor.js.map +1 -1
  119. package/app/merchant/workbench-server.d.ts +1 -0
  120. package/app/merchant/workbench-server.js +176 -2
  121. package/app/merchant/workbench-server.js.map +1 -1
  122. package/app/merchant-core/budget/background-budget.d.ts +132 -0
  123. package/app/merchant-core/budget/background-budget.js +308 -0
  124. package/app/merchant-core/budget/background-budget.js.map +1 -0
  125. package/app/merchant-core/context-governance/actor-context.d.ts +56 -0
  126. package/app/merchant-core/context-governance/actor-context.js +76 -0
  127. package/app/merchant-core/context-governance/actor-context.js.map +1 -0
  128. package/app/merchant-core/context-governance/authority.d.ts +39 -0
  129. package/app/merchant-core/context-governance/authority.js +28 -0
  130. package/app/merchant-core/context-governance/authority.js.map +1 -0
  131. package/app/merchant-core/context-governance/context-guard.d.ts +104 -0
  132. package/app/merchant-core/context-governance/context-guard.js +194 -0
  133. package/app/merchant-core/context-governance/context-guard.js.map +1 -0
  134. package/app/merchant-core/context-governance/data-use-grant.d.ts +91 -0
  135. package/app/merchant-core/context-governance/data-use-grant.js +211 -0
  136. package/app/merchant-core/context-governance/data-use-grant.js.map +1 -0
  137. package/app/merchant-core/context-governance/feature-flags.d.ts +32 -0
  138. package/app/merchant-core/context-governance/feature-flags.js +83 -0
  139. package/app/merchant-core/context-governance/feature-flags.js.map +1 -0
  140. package/app/merchant-core/context-governance/pilot-readiness.d.ts +39 -0
  141. package/app/merchant-core/context-governance/pilot-readiness.js +141 -0
  142. package/app/merchant-core/context-governance/pilot-readiness.js.map +1 -0
  143. package/app/merchant-core/context-governance/source-governance.d.ts +157 -0
  144. package/app/merchant-core/context-governance/source-governance.js +492 -0
  145. package/app/merchant-core/context-governance/source-governance.js.map +1 -0
  146. package/app/merchant-core/context-governance/types.d.ts +62 -0
  147. package/app/merchant-core/context-governance/types.js +92 -0
  148. package/app/merchant-core/context-governance/types.js.map +1 -0
  149. package/app/merchant-core/customer-delivery/binding.d.ts +55 -0
  150. package/app/merchant-core/customer-delivery/binding.js +121 -0
  151. package/app/merchant-core/customer-delivery/binding.js.map +1 -0
  152. package/app/merchant-core/customer-delivery/channel.d.ts +75 -0
  153. package/app/merchant-core/customer-delivery/channel.js +157 -0
  154. package/app/merchant-core/customer-delivery/channel.js.map +1 -0
  155. package/app/merchant-core/customer-delivery/consent-repository.d.ts +50 -0
  156. package/app/merchant-core/customer-delivery/consent-repository.js +160 -0
  157. package/app/merchant-core/customer-delivery/consent-repository.js.map +1 -0
  158. package/app/merchant-core/customer-delivery/dispatcher-loop.d.ts +68 -0
  159. package/app/merchant-core/customer-delivery/dispatcher-loop.js +132 -0
  160. package/app/merchant-core/customer-delivery/dispatcher-loop.js.map +1 -0
  161. package/app/merchant-core/customer-delivery/dispatcher.d.ts +118 -0
  162. package/app/merchant-core/customer-delivery/dispatcher.js +166 -0
  163. package/app/merchant-core/customer-delivery/dispatcher.js.map +1 -0
  164. package/app/merchant-core/customer-delivery/email-adapter.d.ts +31 -0
  165. package/app/merchant-core/customer-delivery/email-adapter.js +90 -0
  166. package/app/merchant-core/customer-delivery/email-adapter.js.map +1 -0
  167. package/app/merchant-core/customer-delivery/gate.d.ts +37 -0
  168. package/app/merchant-core/customer-delivery/gate.js +196 -0
  169. package/app/merchant-core/customer-delivery/gate.js.map +1 -0
  170. package/app/merchant-core/customer-delivery/reconciliation.d.ts +13 -0
  171. package/app/merchant-core/customer-delivery/reconciliation.js +70 -0
  172. package/app/merchant-core/customer-delivery/reconciliation.js.map +1 -0
  173. package/app/merchant-core/customer-delivery/repository.d.ts +55 -0
  174. package/app/merchant-core/customer-delivery/repository.js +235 -0
  175. package/app/merchant-core/customer-delivery/repository.js.map +1 -0
  176. package/app/merchant-core/customer-delivery/smtp-adapter.d.ts +30 -0
  177. package/app/merchant-core/customer-delivery/smtp-adapter.js +183 -0
  178. package/app/merchant-core/customer-delivery/smtp-adapter.js.map +1 -0
  179. package/app/merchant-core/customer-delivery/stream-gate.d.ts +45 -0
  180. package/app/merchant-core/customer-delivery/stream-gate.js +123 -0
  181. package/app/merchant-core/customer-delivery/stream-gate.js.map +1 -0
  182. package/app/merchant-core/customer-delivery/types.d.ts +103 -0
  183. package/app/merchant-core/customer-delivery/types.js +32 -0
  184. package/app/merchant-core/customer-delivery/types.js.map +1 -0
  185. package/app/merchant-core/customer-delivery/weixin-adapter.d.ts +35 -0
  186. package/app/merchant-core/customer-delivery/weixin-adapter.js +56 -0
  187. package/app/merchant-core/customer-delivery/weixin-adapter.js.map +1 -0
  188. package/app/merchant-core/customer-memory/auto-identity.d.ts +43 -0
  189. package/app/merchant-core/customer-memory/auto-identity.js +163 -0
  190. package/app/merchant-core/customer-memory/auto-identity.js.map +1 -0
  191. package/app/merchant-core/customer-memory/grants.d.ts +72 -0
  192. package/app/merchant-core/customer-memory/grants.js +224 -0
  193. package/app/merchant-core/customer-memory/grants.js.map +1 -0
  194. package/app/merchant-core/customer-memory/history.d.ts +51 -0
  195. package/app/merchant-core/customer-memory/history.js +252 -0
  196. package/app/merchant-core/customer-memory/history.js.map +1 -0
  197. package/app/merchant-core/customer-memory/identity.d.ts +66 -0
  198. package/app/merchant-core/customer-memory/identity.js +249 -0
  199. package/app/merchant-core/customer-memory/identity.js.map +1 -0
  200. package/app/merchant-core/customer-memory/maintenance.d.ts +51 -0
  201. package/app/merchant-core/customer-memory/maintenance.js +67 -0
  202. package/app/merchant-core/customer-memory/maintenance.js.map +1 -0
  203. package/app/merchant-core/customer-memory/memory.d.ts +119 -0
  204. package/app/merchant-core/customer-memory/memory.js +502 -0
  205. package/app/merchant-core/customer-memory/memory.js.map +1 -0
  206. package/app/merchant-core/customer-memory/private-transport.d.ts +10 -0
  207. package/app/merchant-core/customer-memory/private-transport.js +108 -0
  208. package/app/merchant-core/customer-memory/private-transport.js.map +1 -0
  209. package/app/merchant-core/customer-memory/quote-history.d.ts +160 -0
  210. package/app/merchant-core/customer-memory/quote-history.js +392 -0
  211. package/app/merchant-core/customer-memory/quote-history.js.map +1 -0
  212. package/app/merchant-core/customer-memory/reception-provider.d.ts +69 -0
  213. package/app/merchant-core/customer-memory/reception-provider.js +250 -0
  214. package/app/merchant-core/customer-memory/reception-provider.js.map +1 -0
  215. package/app/merchant-core/customer-memory/reception-writer.d.ts +19 -0
  216. package/app/merchant-core/customer-memory/reception-writer.js +32 -0
  217. package/app/merchant-core/customer-memory/reception-writer.js.map +1 -0
  218. package/app/merchant-core/customer-memory/types.d.ts +111 -0
  219. package/app/merchant-core/customer-memory/types.js +58 -0
  220. package/app/merchant-core/customer-memory/types.js.map +1 -0
  221. package/app/merchant-core/followup/repository.d.ts +2 -1
  222. package/app/merchant-core/followup/repository.js +29 -23
  223. package/app/merchant-core/followup/repository.js.map +1 -1
  224. package/app/merchant-core/learning/evaluation-fixtures.d.ts +38 -0
  225. package/app/merchant-core/learning/evaluation-fixtures.js +83 -0
  226. package/app/merchant-core/learning/evaluation-fixtures.js.map +1 -0
  227. package/app/merchant-core/learning/evaluation-suite-v1.d.ts +56 -0
  228. package/app/merchant-core/learning/evaluation-suite-v1.js +129 -0
  229. package/app/merchant-core/learning/evaluation-suite-v1.js.map +1 -0
  230. package/app/merchant-core/learning/evaluation.d.ts +84 -0
  231. package/app/merchant-core/learning/evaluation.js +581 -0
  232. package/app/merchant-core/learning/evaluation.js.map +1 -0
  233. package/app/merchant-core/learning/initial-skills.d.ts +36 -0
  234. package/app/merchant-core/learning/initial-skills.js +67 -0
  235. package/app/merchant-core/learning/initial-skills.js.map +1 -0
  236. package/app/merchant-core/learning/precheck.d.ts +33 -0
  237. package/app/merchant-core/learning/precheck.js +56 -0
  238. package/app/merchant-core/learning/precheck.js.map +1 -0
  239. package/app/merchant-core/learning/real-evaluation-runner.d.ts +52 -0
  240. package/app/merchant-core/learning/real-evaluation-runner.js +167 -0
  241. package/app/merchant-core/learning/real-evaluation-runner.js.map +1 -0
  242. package/app/merchant-core/learning/reception-skill-catalog.d.ts +39 -0
  243. package/app/merchant-core/learning/reception-skill-catalog.js +145 -0
  244. package/app/merchant-core/learning/reception-skill-catalog.js.map +1 -0
  245. package/app/merchant-core/learning/recovery-gateway.d.ts +27 -0
  246. package/app/merchant-core/learning/recovery-gateway.js +106 -0
  247. package/app/merchant-core/learning/recovery-gateway.js.map +1 -0
  248. package/app/merchant-core/learning/repository.d.ts +130 -0
  249. package/app/merchant-core/learning/repository.js +588 -0
  250. package/app/merchant-core/learning/repository.js.map +1 -0
  251. package/app/merchant-core/learning/service.d.ts +262 -0
  252. package/app/merchant-core/learning/service.js +1498 -0
  253. package/app/merchant-core/learning/service.js.map +1 -0
  254. package/app/merchant-core/learning/test-dispatch-gateway.d.ts +43 -0
  255. package/app/merchant-core/learning/test-dispatch-gateway.js +145 -0
  256. package/app/merchant-core/learning/test-dispatch-gateway.js.map +1 -0
  257. package/app/merchant-core/learning/types.d.ts +217 -0
  258. package/app/merchant-core/learning/types.js +52 -0
  259. package/app/merchant-core/learning/types.js.map +1 -0
  260. package/app/merchant-core/migrations/ledgers.d.ts +40 -0
  261. package/app/merchant-core/migrations/ledgers.js +824 -0
  262. package/app/merchant-core/migrations/ledgers.js.map +1 -0
  263. package/app/merchant-core/migrations/runner.d.ts +55 -0
  264. package/app/merchant-core/migrations/runner.js +114 -0
  265. package/app/merchant-core/migrations/runner.js.map +1 -0
  266. package/app/merchant-core/operations-jobs/advice-runner.d.ts +63 -0
  267. package/app/merchant-core/operations-jobs/advice-runner.js +199 -0
  268. package/app/merchant-core/operations-jobs/advice-runner.js.map +1 -0
  269. package/app/merchant-core/operations-jobs/pilot-evidence.d.ts +64 -0
  270. package/app/merchant-core/operations-jobs/pilot-evidence.js +109 -0
  271. package/app/merchant-core/operations-jobs/pilot-evidence.js.map +1 -0
  272. package/app/merchant-core/operations-jobs/pilot-metrics.d.ts +70 -0
  273. package/app/merchant-core/operations-jobs/pilot-metrics.js +98 -0
  274. package/app/merchant-core/operations-jobs/pilot-metrics.js.map +1 -0
  275. package/app/merchant-core/operations-jobs/projection.d.ts +41 -0
  276. package/app/merchant-core/operations-jobs/projection.js +158 -0
  277. package/app/merchant-core/operations-jobs/projection.js.map +1 -0
  278. package/app/merchant-core/operations-jobs/scheduler.d.ts +68 -0
  279. package/app/merchant-core/operations-jobs/scheduler.js +233 -0
  280. package/app/merchant-core/operations-jobs/scheduler.js.map +1 -0
  281. package/app/merchant-core/operations-jobs/service.d.ts +260 -0
  282. package/app/merchant-core/operations-jobs/service.js +922 -0
  283. package/app/merchant-core/operations-jobs/service.js.map +1 -0
  284. package/app/merchant-core/ops/facade.d.ts +10 -0
  285. package/app/merchant-core/ops/facade.js +52 -0
  286. package/app/merchant-core/ops/facade.js.map +1 -1
  287. package/app/merchant-core/ops/schema.d.ts +1 -1
  288. package/app/merchant-core/ops/schema.js +119 -2
  289. package/app/merchant-core/ops/schema.js.map +1 -1
  290. package/app/merchant-core/ops/surface.d.ts +37 -0
  291. package/app/merchant-core/ops/surface.js +240 -0
  292. package/app/merchant-core/ops/surface.js.map +1 -1
  293. package/app/merchant-core/ownership/instance-ownership.d.ts +122 -0
  294. package/app/merchant-core/ownership/instance-ownership.js +236 -0
  295. package/app/merchant-core/ownership/instance-ownership.js.map +1 -0
  296. package/app/merchant-core/ownership/runtime.d.ts +175 -0
  297. package/app/merchant-core/ownership/runtime.js +422 -0
  298. package/app/merchant-core/ownership/runtime.js.map +1 -0
  299. package/app/merchant-core/storage/transaction.d.ts +102 -7
  300. package/app/merchant-core/storage/transaction.js +274 -3
  301. package/app/merchant-core/storage/transaction.js.map +1 -1
  302. package/app/merchant-credential-rotation.d.ts +4 -1
  303. package/app/merchant-credential-rotation.js +12 -8
  304. package/app/merchant-credential-rotation.js.map +1 -1
  305. package/app/merchant-runtime/manager.d.ts +2 -0
  306. package/app/merchant-runtime/manager.js +5 -0
  307. package/app/merchant-runtime/manager.js.map +1 -1
  308. package/build-manifest.json +1202 -242
  309. package/deployment-lock.json +2 -2
  310. package/package.json +1 -1
  311. package/prepare.mjs +5 -1
  312. package/runtime-package.json +1 -1
  313. package/shipping-sbom.cdx.json +6 -6
  314. package/shipping-source.json +12 -12
@@ -0,0 +1,123 @@
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 / M6-5)。
18
+ *
19
+ * 接待回复与经营通知共用同一 stream_key(merchant + 渠道 + 会话/收件范围),
20
+ * 同一时刻只允许一条在途。UNKNOWN 未核对前保持占用;结果确认后才释放。
21
+ * 持久实例使用共享 SQLite 租约;投递结果仍以各自 outbox 为准。
22
+ */
23
+ import { constants, fchmodSync, fstatSync, openSync, closeSync } from "node:fs";
24
+ import path from "node:path";
25
+ import { DatabaseSync } from "node:sqlite";
26
+ export class StreamGateError extends Error {
27
+ code;
28
+ streamKey;
29
+ constructor(streamKey) {
30
+ super(`发送流 ${streamKey} 已有在途通知`);
31
+ this.name = "StreamGateError";
32
+ this.code = "stream_busy";
33
+ this.streamKey = streamKey;
34
+ }
35
+ }
36
+ export class StreamGate {
37
+ db;
38
+ holders = new Map();
39
+ constructor(db) {
40
+ this.db = db;
41
+ db?.exec(`
42
+ CREATE TABLE IF NOT EXISTS delivery_stream_leases (
43
+ stream_key TEXT PRIMARY KEY, holder_id TEXT NOT NULL, lease_expires_at TEXT NOT NULL
44
+ )
45
+ `);
46
+ }
47
+ static forDataDir(dataDir) {
48
+ const file = path.join(dataDir, "delivery-streams.sqlite");
49
+ const fd = openSync(file, constants.O_CREAT | constants.O_NOFOLLOW | constants.O_RDWR, 0o600);
50
+ try {
51
+ const stat = fstatSync(fd);
52
+ if (!stat.isFile() || stat.nlink !== 1)
53
+ throw new Error("Invalid delivery stream database");
54
+ fchmodSync(fd, 0o600);
55
+ }
56
+ finally {
57
+ closeSync(fd);
58
+ }
59
+ const db = new DatabaseSync(file);
60
+ try {
61
+ db.exec("PRAGMA busy_timeout = 3000");
62
+ return new StreamGate(db);
63
+ }
64
+ catch (error) {
65
+ db.close();
66
+ throw error;
67
+ }
68
+ }
69
+ close() {
70
+ this.db?.close();
71
+ }
72
+ /** 申请进入发送流;已有未过期 holder 时拒绝。 */
73
+ enter(input) {
74
+ if (!Number.isSafeInteger(input.lease_ms) || input.lease_ms <= 0) {
75
+ throw new Error("lease_ms must be a positive integer");
76
+ }
77
+ const expiresAt = new Date(Date.parse(input.now) + input.lease_ms).toISOString();
78
+ if (this.db !== undefined) {
79
+ const result = this.db.prepare(`INSERT INTO delivery_stream_leases (stream_key, holder_id, lease_expires_at)
80
+ VALUES (?, ?, ?) ON CONFLICT(stream_key) DO UPDATE SET
81
+ holder_id = excluded.holder_id, lease_expires_at = excluded.lease_expires_at
82
+ WHERE delivery_stream_leases.lease_expires_at <= ?
83
+ OR delivery_stream_leases.holder_id = excluded.holder_id`).run(input.stream_key, input.holder_id, expiresAt, input.now);
84
+ if (result.changes === 0)
85
+ throw new StreamGateError(input.stream_key);
86
+ return;
87
+ }
88
+ const current = this.holders.get(input.stream_key);
89
+ if (current !== undefined &&
90
+ Date.parse(current.lease_expires_at) > Date.parse(input.now) &&
91
+ current.holder_id !== input.holder_id) {
92
+ throw new StreamGateError(input.stream_key);
93
+ }
94
+ this.holders.set(input.stream_key, {
95
+ holder_id: input.holder_id,
96
+ lease_expires_at: expiresAt,
97
+ });
98
+ }
99
+ /** 结果确定(SENT/not_sent)后释放;UNKNOWN 不释放。 */
100
+ leave(stream_key, holder_id) {
101
+ if (this.db !== undefined) {
102
+ this.db.prepare("DELETE FROM delivery_stream_leases WHERE stream_key = ? AND holder_id = ?")
103
+ .run(stream_key, holder_id);
104
+ return;
105
+ }
106
+ const current = this.holders.get(stream_key);
107
+ if (current?.holder_id === holder_id)
108
+ this.holders.delete(stream_key);
109
+ }
110
+ isHeld(stream_key, now) {
111
+ if (this.db !== undefined) {
112
+ return this.db.prepare("SELECT 1 FROM delivery_stream_leases WHERE stream_key = ? AND lease_expires_at > ?")
113
+ .get(stream_key, now) !== undefined;
114
+ }
115
+ const current = this.holders.get(stream_key);
116
+ return current !== undefined && Date.parse(current.lease_expires_at) > Date.parse(now);
117
+ }
118
+ }
119
+ /** 接待与经营通知共用的 stream key 约定。 */
120
+ export function streamKey(input) {
121
+ return `${input.merchant_id}:${input.channel}:${input.scope}`;
122
+ }
123
+ //# sourceMappingURL=stream-gate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stream-gate.js","sourceRoot":"","sources":["../../../src/merchant-core/customer-delivery/stream-gate.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH;;;;;;GAMG;AAEH,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,SAAS,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAChF,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAI3C,MAAM,OAAO,eAAgB,SAAQ,KAAK;IAC/B,IAAI,CAAsB;IAC1B,SAAS,CAAS;IAC3B,YAAY,SAAiB;QAC3B,KAAK,CAAC,OAAO,SAAS,SAAS,CAAC,CAAC;QACjC,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAC;QAC9B,IAA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B,CAAC,CAAC;IACL,CAAC;IAED,0CAA0C;IAC1C,KAAK,CAAC,UAAkB,EAAE,SAAiB;QACzC,IAAI,IAAI,CAAC,EAAE,KAAK,SAAS,EAAE,CAAC;YAC1B,IAAI,CAAC,EAAE,CAAC,OAAO,CAAC,2EAA2E,CAAC;iBACzF,GAAG,CAAC,UAAU,EAAE,SAAS,CAAC,CAAC;YAC9B,OAAO;QACT,CAAC;QACD,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;QAC7C,IAAI,OAAO,EAAE,SAAS,KAAK,SAAS;YAAE,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;IACxE,CAAC;IAED,MAAM,CAAC,UAAkB,EAAE,GAAW;QACpC,IAAI,IAAI,CAAC,EAAE,KAAK,SAAS,EAAE,CAAC;YAC1B,OAAO,IAAI,CAAC,EAAE,CAAC,OAAO,CAAC,oFAAoF,CAAC;iBACzG,GAAG,CAAC,UAAU,EAAE,GAAG,CAAC,KAAK,SAAS,CAAC;QACxC,CAAC;QACD,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;QAC7C,OAAO,OAAO,KAAK,SAAS,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,gBAAgB,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IACzF,CAAC;CACF;AAED,gCAAgC;AAChC,MAAM,UAAU,SAAS,CAAC,KAIzB;IACC,OAAO,GAAG,KAAK,CAAC,WAAW,IAAI,KAAK,CAAC,OAAO,IAAI,KAAK,CAAC,KAAK,EAAE,CAAC;AAChE,CAAC"}
@@ -0,0 +1,103 @@
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 客户外发发送门(设计 v1.1 §16.3 / M6)。
18
+ *
19
+ * P0 的 `customer_delivery_enabled` 固定关闭,本模块只在 P1 验收后启用。
20
+ * 发送前必须同时具备:可信收件人绑定、目的/频率授权、退订入口、有效期、
21
+ * 冻结消息与持久回执;任一缺失都保持工作台草稿,不出网。
22
+ *
23
+ * 本文件只定义不可变输入与错误码;所有字段都是服务端注入的引用,模型
24
+ * 不能自报收件人、渠道或授权。
25
+ */
26
+ import type { GovernanceFeatureFlags } from "../context-governance/feature-flags.js";
27
+ export declare const DELIVERY_CHANNELS: readonly ["wechat", "sms", "email", "in_app"];
28
+ export type DeliveryChannel = (typeof DELIVERY_CHANNELS)[number];
29
+ export declare const DELIVERY_PURPOSES: readonly ["arrival_notification", "status_notification", "service_reply"];
30
+ export type DeliveryPurpose = (typeof DELIVERY_PURPOSES)[number];
31
+ /** 发送状态与既有 followup_outbox 对齐:PENDING / SENT / UNKNOWN。 */
32
+ export declare const DELIVERY_STATUSES: readonly ["PENDING", "SENT", "UNKNOWN"];
33
+ export type DeliveryStatus = (typeof DELIVERY_STATUSES)[number];
34
+ export interface RecipientBinding {
35
+ readonly binding_id: string;
36
+ readonly merchant_id: string;
37
+ readonly channel: DeliveryChannel;
38
+ /** 渠道地址的脱敏引用;不得是明文手机号/邮箱。 */
39
+ readonly address_ref: string;
40
+ readonly verified_at: string;
41
+ readonly revoked_at: string | null;
42
+ readonly binding_epoch: number;
43
+ }
44
+ export interface NotificationSubscription {
45
+ readonly subscription_id: string;
46
+ readonly merchant_id: string;
47
+ readonly binding_id: string;
48
+ readonly purpose: DeliveryPurpose;
49
+ /** 授权频率上限;0 表示不允许主动通知。 */
50
+ readonly max_per_day: number;
51
+ readonly granted_at: string;
52
+ readonly expires_at: string | null;
53
+ readonly unsubscribed_at: string | null;
54
+ readonly grant_epoch: number;
55
+ }
56
+ export interface DeliverySourceGrant {
57
+ readonly grant_id: string;
58
+ readonly grant_epoch: number;
59
+ readonly purpose: string;
60
+ readonly status: "ACTIVE" | "REVOKED" | "EXPIRED";
61
+ readonly expires_at: string | null;
62
+ }
63
+ /** 已冻结的待发送载荷;审批后修改任何字段都必须重新准备。 */
64
+ export interface PreparedDelivery {
65
+ readonly delivery_id: string;
66
+ readonly merchant_id: string;
67
+ readonly stream_key: string;
68
+ readonly binding_id: string;
69
+ readonly binding_epoch: number;
70
+ readonly channel: DeliveryChannel;
71
+ readonly purpose: DeliveryPurpose;
72
+ readonly payload: Readonly<Record<string, unknown>>;
73
+ readonly payload_digest: string;
74
+ readonly seq: number;
75
+ readonly valid_until: string;
76
+ readonly subscription_id: string;
77
+ readonly subscription_epoch: number;
78
+ readonly source_grants: readonly DeliverySourceGrant[];
79
+ readonly created_at: string;
80
+ readonly status: DeliveryStatus;
81
+ }
82
+ export interface PrepareDeliveryInput {
83
+ readonly flags: GovernanceFeatureFlags;
84
+ readonly merchant_id: string;
85
+ readonly binding: RecipientBinding | undefined;
86
+ readonly subscription: NotificationSubscription | undefined;
87
+ readonly source_grants: readonly DeliverySourceGrant[];
88
+ readonly payload: Record<string, unknown>;
89
+ readonly purpose: DeliveryPurpose;
90
+ readonly seq: number;
91
+ readonly valid_until: string;
92
+ readonly now: string;
93
+ /** 当日同一 stream 已发送数量(服务端从持久回执统计,不由模型提供)。 */
94
+ readonly sent_today: number;
95
+ /** 同一 stream 是否已有在途(PENDING/UNKNOWN)发送。 */
96
+ readonly stream_busy: boolean;
97
+ readonly delivery_id: string;
98
+ }
99
+ export type DeliveryGateErrorCode = "feature_disabled" | "channel_unknown" | "consent_missing" | "address_invalid" | "epoch_mismatch" | "invalid_transition" | "binding_missing" | "binding_revoked" | "binding_unverified" | "binding_mismatch" | "subscription_missing" | "subscription_revoked" | "subscription_expired" | "subscription_mismatch" | "frequency_exceeded" | "grant_revoked" | "grant_expired" | "payload_invalid" | "seq_invalid" | "valid_until_invalid" | "stream_busy";
100
+ export declare class DeliveryGateError extends Error {
101
+ readonly code: DeliveryGateErrorCode;
102
+ constructor(code: DeliveryGateErrorCode, message: string);
103
+ }
@@ -0,0 +1,32 @@
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 const DELIVERY_CHANNELS = ["wechat", "sms", "email", "in_app"];
17
+ export const DELIVERY_PURPOSES = [
18
+ "arrival_notification",
19
+ "status_notification",
20
+ "service_reply",
21
+ ];
22
+ /** 发送状态与既有 followup_outbox 对齐:PENDING / SENT / UNKNOWN。 */
23
+ export const DELIVERY_STATUSES = ["PENDING", "SENT", "UNKNOWN"];
24
+ export class DeliveryGateError extends Error {
25
+ code;
26
+ constructor(code, message) {
27
+ super(message);
28
+ this.name = "DeliveryGateError";
29
+ this.code = code;
30
+ }
31
+ }
32
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/merchant-core/customer-delivery/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAeH,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,QAAQ,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,CAAU,CAAC;AAG/E,MAAM,CAAC,MAAM,iBAAiB,GAAG;IAC/B,sBAAsB;IACtB,qBAAqB;IACrB,eAAe;CACP,CAAC;AAGX,2DAA2D;AAC3D,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,SAAS,EAAE,MAAM,EAAE,SAAS,CAAU,CAAC;AAgGzE,MAAM,OAAO,iBAAkB,SAAQ,KAAK;IACjC,IAAI,CAAwB;IACrC,YAAY,IAA2B,EAAE,OAAe;QACtD,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,mBAAmB,CAAC;QAChC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;CACF"}
@@ -0,0 +1,35 @@
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
+ * 微信 iLink 发送适配器(M6-6)。
18
+ *
19
+ * iLink `sendmessage` 没有业务幂等键:成功只表示平台接受;超时/网络错误
20
+ * 视为 UNKNOWN,不能凭猜测重发。调用方必须先用绑定/订阅确认收件人。
21
+ */
22
+ import type { DeliveryChannelAdapter } from "./channel.js";
23
+ import type { SendMessageInput } from "../../weixin/types.js";
24
+ import type { BotCredentials } from "../../weixin/types.js";
25
+ export interface WeixinSender {
26
+ sendMessage(input: SendMessageInput, credentials: BotCredentials): Promise<void>;
27
+ }
28
+ export interface WeixinChannelAdapterOptions {
29
+ readonly sender: WeixinSender;
30
+ readonly credentials: BotCredentials;
31
+ /** 仅测试注入;生产必须由调用方按授权绑定传入 recipient。 */
32
+ readonly recipient_user_id?: string;
33
+ readonly max_text_chars?: number;
34
+ }
35
+ export declare function createWeixinChannelAdapter(options: WeixinChannelAdapterOptions): DeliveryChannelAdapter;
@@ -0,0 +1,56 @@
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
+ function deterministicRejection(err) {
18
+ const code = err?.code;
19
+ return (code === "validation" ||
20
+ code === "not_configured" ||
21
+ code === "auth" ||
22
+ code === "qr_expired" ||
23
+ code === "protocol");
24
+ }
25
+ export function createWeixinChannelAdapter(options) {
26
+ const maxChars = options.max_text_chars ?? 2000;
27
+ return {
28
+ async send({ delivery, recipient }) {
29
+ if (delivery.channel !== "wechat") {
30
+ return { outcome: "not_sent", reason: "channel_mismatch" };
31
+ }
32
+ const to = recipient ?? options.recipient_user_id;
33
+ if (typeof to !== "string" || to.trim() === "") {
34
+ return { outcome: "not_sent", reason: "recipient_unresolved" };
35
+ }
36
+ const title = typeof delivery.payload.title === "string" ? delivery.payload.title : "";
37
+ const body = typeof delivery.payload.body === "string" ? delivery.payload.body : "";
38
+ const text = [title, body].filter((part) => part.trim() !== "").join("\n").slice(0, maxChars);
39
+ if (text === "")
40
+ return { outcome: "not_sent", reason: "payload_empty" };
41
+ try {
42
+ await options.sender.sendMessage({ to_user_id: to, text }, options.credentials);
43
+ // iLink 成功响应只证明平台接受,不等于客户已读。
44
+ return { outcome: "accepted", receipt: { channel: "weixin", accepted: true } };
45
+ }
46
+ catch (err) {
47
+ // P2-7:确定性拒绝归 not_sent,避免一条 4xx 让整条流永久 UNKNOWN。
48
+ if (deterministicRejection(err)) {
49
+ return { outcome: "not_sent", reason: `weixin_${String(err.code)}` };
50
+ }
51
+ return { outcome: "unknown" };
52
+ }
53
+ },
54
+ };
55
+ }
56
+ //# sourceMappingURL=weixin-adapter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"weixin-adapter.js","sourceRoot":"","sources":["../../../src/merchant-core/customer-delivery/weixin-adapter.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAyBH,yCAAyC;AACzC,SAAS,sBAAsB,CAAC,GAAY;IAC1C,MAAM,IAAI,GAAI,GAAiC,EAAE,IAAI,CAAC;IACtD,OAAO,CACL,IAAI,KAAK,YAAY;QACrB,IAAI,KAAK,gBAAgB;QACzB,IAAI,KAAK,MAAM;QACf,IAAI,KAAK,YAAY;QACrB,IAAI,KAAK,UAAU,CACpB,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,0BAA0B,CACxC,OAAoC;IAEpC,MAAM,QAAQ,GAAG,OAAO,CAAC,cAAc,IAAI,IAAI,CAAC;IAChD,OAAO;QACL,KAAK,CAAC,IAAI,CAAC,EAAE,QAAQ,EAAE,SAAS,EAAE;YAChC,IAAI,QAAQ,CAAC,OAAO,KAAK,QAAQ,EAAE,CAAC;gBAClC,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,kBAAkB,EAAE,CAAC;YAC7D,CAAC;YACD,MAAM,EAAE,GAAG,SAAS,IAAI,OAAO,CAAC,iBAAiB,CAAC;YAClD,IAAI,OAAO,EAAE,KAAK,QAAQ,IAAI,EAAE,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC;gBAC/C,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,sBAAsB,EAAE,CAAC;YACjE,CAAC;YACD,MAAM,KAAK,GAAG,OAAO,QAAQ,CAAC,OAAO,CAAC,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,QA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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 type { DatabaseSync } from "node:sqlite";
17
+ /** 自动建档动作的稳定结果。 */
18
+ export interface VerifiedCustomerIdentity {
19
+ readonly subject_id: string;
20
+ /** 本次是否新建了本店客户主体(复用为 false)。 */
21
+ readonly created: boolean;
22
+ /** 当前生效(或本次续绑)的绑定 epoch;REVOKED 未续绑时为 undefined。 */
23
+ readonly binding_epoch?: number;
24
+ /** 本次是否新建了绑定(复用既有 ACTIVE 绑定 / 未续绑为 false)。 */
25
+ readonly binding_created?: boolean;
26
+ }
27
+ /**
28
+ * 幂等自动建档:同一 (merchant_id, verified_actor) 恒返回同一 subject_id。
29
+ * 调用方必须已确认 `identityVerified === true` 且属于受支持的开放询价身份;
30
+ * 本函数不做验签——那是传输层(open-inquiry verifier)的职责。
31
+ */
32
+ export declare function ensureVerifiedCustomerIdentity(db: DatabaseSync, input: {
33
+ merchant_id: string;
34
+ verified_actor: string;
35
+ now: string;
36
+ }): VerifiedCustomerIdentity;
37
+ /**
38
+ * 只读查询:本店客户号(不产生任何写入)。接待/历史读取走此入口,
39
+ * 与 `currentSubjectForActor` 的会话级绑定查询分开。
40
+ */
41
+ export declare function findVerifiedCustomerIdentity(db: DatabaseSync, merchantId: string, verifiedActor: string): {
42
+ subject_id: string;
43
+ } | undefined;
@@ -0,0 +1,163 @@
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
+ * 首询自动分配本店客户 ID(买家识别与交易回执 · K1,设计 §2.2)。
18
+ *
19
+ * 以「商家 ID + 验签所得 actor」为唯一查找键,在一个事务内创建或复用
20
+ * `agent_scope` 主体;其 subject_id 即本店客户 ID。当前 subject_bindings 的
21
+ * actor 索引不唯一且绑定可过期,因此不拿“当前有效绑定”兼任长期客户号——
22
+ * 长期关系由 verified_customer_identities 映射承担(迁移 v4),绑定只是
23
+ * 该映射当次会话的会话级证据:
24
+ *
25
+ * - 映射不存在:创建主体 + epoch 1 绑定 + 映射(同一事务)。
26
+ * - 映射存在且最新绑定 ACTIVE 未过期:只复用,不新建(并发/重试零副作用)。
27
+ * - 映射存在但绑定过期:同一事务内续绑(epoch = max+1)——客户号稳定,
28
+ * “绑定过期不新建客户号”(A6)。
29
+ * - 映射存在且最新绑定 REVOKED:不自动重建绑定(再次建档不得恢复已撤销
30
+ * 授权,设计 §2);映射与客户号保留,跨次读取由授权检查另行拒绝。
31
+ *
32
+ * 全部写入走 `inImmediateTransaction`;唯一主键保证并发建档只产生一个主体。
33
+ */
34
+ import { randomUUID } from "node:crypto";
35
+ import { createActorContext } from "../context-governance/actor-context.js";
36
+ import { inImmediateTransaction } from "../storage/transaction.js";
37
+ import { CustomerIdentityService } from "./identity.js";
38
+ import { CustomerMemoryError } from "./types.js";
39
+ const IDENTITY_BINDING_TTL_MS = 30 * 24 * 60 * 60 * 1000;
40
+ function requireActorId(actor) {
41
+ if (typeof actor !== "string" || actor.length === 0 || actor.length > 256) {
42
+ throw new CustomerMemoryError("validation", "verified_actor_invalid", "verified actor 必须是 1..256 字符");
43
+ }
44
+ // eslint-disable-next-line no-control-regex
45
+ if (/[\u0000-\u001f\u007f]/.test(actor)) {
46
+ throw new CustomerMemoryError("validation", "verified_actor_invalid", "verified actor 含控制字符");
47
+ }
48
+ return actor;
49
+ }
50
+ function requireMerchantId(merchantId) {
51
+ if (typeof merchantId !== "string" || merchantId.length === 0 || merchantId.length > 128) {
52
+ throw new CustomerMemoryError("validation", "merchant_id_invalid", "merchant_id 必须是 1..128 字符");
53
+ }
54
+ return merchantId;
55
+ }
56
+ /**
57
+ * 幂等自动建档:同一 (merchant_id, verified_actor) 恒返回同一 subject_id。
58
+ * 调用方必须已确认 `identityVerified === true` 且属于受支持的开放询价身份;
59
+ * 本函数不做验签——那是传输层(open-inquiry verifier)的职责。
60
+ */
61
+ export function ensureVerifiedCustomerIdentity(db, input) {
62
+ const merchantId = requireMerchantId(input.merchant_id);
63
+ const actor = requireActorId(input.verified_actor);
64
+ const stamp = normalizeNow(input.now);
65
+ return inImmediateTransaction(db, () => {
66
+ const existing = db
67
+ .prepare("SELECT subject_id, binding_epoch FROM verified_customer_identities WHERE merchant_id = ? AND verified_actor = ?")
68
+ .get(merchantId, actor);
69
+ if (existing !== undefined) {
70
+ return rebindExisting(db, merchantId, actor, existing.subject_id, stamp);
71
+ }
72
+ // 新建档:主体 + epoch 1 绑定 + 映射,一个事务。
73
+ const identity = new CustomerIdentityService({ db, now: () => stamp });
74
+ const systemActor = createActorContext({
75
+ merchant_id: merchantId,
76
+ actor_id: "system:open-inquiry-identity",
77
+ auth_strength: "verified",
78
+ subject_binding: "organization",
79
+ capabilities: ["memory:write"],
80
+ });
81
+ const subject = identity.createSubject({
82
+ merchant_id: merchantId,
83
+ actor: systemActor,
84
+ subject_kind: "agent_scope",
85
+ });
86
+ const bindingEpoch = 1;
87
+ db.prepare(`INSERT INTO subject_bindings
88
+ (binding_id, merchant_id, subject_id, actor_id, issuer, assurance_class,
89
+ proof_ref, binding_epoch, status, expires_at, created_at, updated_at)
90
+ VALUES (?, ?, ?, ?, 'open-inquiry', 'verified-key', ?, ?, 'ACTIVE', ?, ?, ?)`).run(`bind_${randomUUID()}`, merchantId, subject.subject_id, actor, `sha256:${actor}`, bindingEpoch, new Date(Date.parse(stamp) + IDENTITY_BINDING_TTL_MS).toISOString(), stamp, stamp);
91
+ db.prepare(`INSERT INTO verified_customer_identities
92
+ (merchant_id, verified_actor, subject_id, binding_epoch, created_at, updated_at)
93
+ VALUES (?, ?, ?, ?, ?, ?)`).run(merchantId, actor, subject.subject_id, bindingEpoch, stamp, stamp);
94
+ return {
95
+ subject_id: subject.subject_id,
96
+ created: true,
97
+ binding_epoch: bindingEpoch,
98
+ binding_created: true,
99
+ };
100
+ });
101
+ }
102
+ /**
103
+ * 已有映射时的复用/续绑判定。REVOKED 绑定不自动重建;过期绑定在同一事务内
104
+ * 续绑(epoch 递增,旧主体绑定不复用)。
105
+ */
106
+ function rebindExisting(db, merchantId, actor, subjectId, stamp) {
107
+ const latest = db
108
+ .prepare(`SELECT binding_epoch, status, expires_at FROM subject_bindings
109
+ WHERE merchant_id = ? AND actor_id = ? AND subject_id = ?
110
+ ORDER BY binding_epoch DESC LIMIT 1`)
111
+ .get(merchantId, actor, subjectId);
112
+ if (latest === undefined) {
113
+ // 映射存在但没有任何绑定行(历史数据/手工清理):补一条会话绑定。
114
+ return insertRebinding(db, merchantId, actor, subjectId, latestEpoch(db, merchantId, actor, subjectId), stamp);
115
+ }
116
+ if (latest.status === "REVOKED") {
117
+ return { subject_id: subjectId, created: false, binding_created: false };
118
+ }
119
+ if (latest.expires_at > stamp) {
120
+ return {
121
+ subject_id: subjectId,
122
+ created: false,
123
+ binding_epoch: latest.binding_epoch,
124
+ binding_created: false,
125
+ };
126
+ }
127
+ return insertRebinding(db, merchantId, actor, subjectId, latest.binding_epoch, stamp);
128
+ }
129
+ function latestEpoch(db, merchantId, actor, subjectId) {
130
+ const row = db
131
+ .prepare(`SELECT COALESCE(MAX(binding_epoch), 0) AS epoch FROM subject_bindings
132
+ WHERE merchant_id = ? AND actor_id = ? AND subject_id = ?`)
133
+ .get(merchantId, actor, subjectId);
134
+ return Number(row.epoch);
135
+ }
136
+ function insertRebinding(db, merchantId, actor, subjectId, previousEpoch, stamp) {
137
+ const bindingEpoch = previousEpoch + 1;
138
+ db.prepare(`INSERT INTO subject_bindings
139
+ (binding_id, merchant_id, subject_id, actor_id, issuer, assurance_class,
140
+ proof_ref, binding_epoch, status, expires_at, created_at, updated_at)
141
+ VALUES (?, ?, ?, ?, 'open-inquiry', 'verified-key', ?, ?, 'ACTIVE', ?, ?, ?)`).run(`bind_${randomUUID()}`, merchantId, subjectId, actor, `sha256:${actor}`, bindingEpoch, new Date(Date.parse(stamp) + IDENTITY_BINDING_TTL_MS).toISOString(), stamp, stamp);
142
+ db.prepare(`UPDATE verified_customer_identities SET binding_epoch = ?, updated_at = ?
143
+ WHERE merchant_id = ? AND verified_actor = ?`).run(bindingEpoch, stamp, merchantId, actor);
144
+ return { subject_id: subjectId, created: false, binding_epoch: bindingEpoch, binding_created: true };
145
+ }
146
+ function normalizeNow(now) {
147
+ const parsed = Date.parse(now);
148
+ if (!Number.isFinite(parsed)) {
149
+ throw new CustomerMemoryError("validation", "now_invalid", "时钟输出必须是可解析的 RFC3339 时间");
150
+ }
151
+ return new Date(parsed).toISOString();
152
+ }
153
+ /**
154
+ * 只读查询:本店客户号(不产生任何写入)。接待/历史读取走此入口,
155
+ * 与 `currentSubjectForActor` 的会话级绑定查询分开。
156
+ */
157
+ export function findVerifiedCustomerIdentity(db, merchantId, verifiedActor) {
158
+ const row = db
159
+ .prepare("SELECT subject_id FROM verified_customer_identities WHERE merchant_id = ? AND verified_actor = ?")
160
+ .get(requireMerchantId(merchantId), requireActorId(verifiedActor));
161
+ return row;
162
+ }
163
+ //# sourceMappingURL=auto-identity.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auto-identity.js","sourceRoot":"","sources":["../../../src/merchant-core/customer-memory/auto-identity.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH;;;;;;;;;;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AO,IAAI,IAAI,CAAC,MAAM,CAAC,CAAC,WAAW,EAAE,CAAC;AACxC,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,4BAA4B,CAC1C,EAAgB,EAChB,UAAkB,EAClB,aAAqB;IAErB,MAAM,GAAG,GAAG,EAAE;SACX,OAAO,CACN,kGAAkG,CACnG;SACA,GAAG,CAAC,iBAAiB,CAAC,UAAU,CAAC,EAAE,cAAc,CAAC,aAAa,CAAC,CAEtD,CAAC;IACd,OAAO,GAAG,CAAC;AACb,CAAC"}
@@ -0,0 +1,72 @@
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 type { DatabaseSync } from "node:sqlite";
17
+ import type { ActorContext } from "../context-governance/actor-context.js";
18
+ import { type DataUsePurpose } from "../context-governance/types.js";
19
+ import { type DataUseGrant } from "../context-governance/data-use-grant.js";
20
+ export interface GrantRequestRecord {
21
+ readonly grant_request_id: string;
22
+ readonly merchant_id: string;
23
+ readonly subject_id: string;
24
+ readonly purpose: DataUsePurpose;
25
+ readonly resource_scope_id: string;
26
+ readonly grant_ttl_seconds: number;
27
+ readonly expires_at: string;
28
+ readonly consumed_at: string | null;
29
+ readonly created_at: string;
30
+ }
31
+ export declare class GrantRequestService {
32
+ private readonly db;
33
+ private readonly now;
34
+ private readonly grants;
35
+ constructor(options: {
36
+ db: DatabaseSync;
37
+ now: () => string;
38
+ });
39
+ /** 当前 actor 的有效绑定主体;未绑定/已撤销/过期返回 undefined。 */
40
+ private boundSubject;
41
+ private assertScopeUsable;
42
+ /**
43
+ * 创建授权请求:返回一次性短语与卡片引用(各存摘要)。请求 TTL 默认 15
44
+ * 分钟;授权 TTL 默认 90 天,受 90 天上限(试点值)。
45
+ */
46
+ createGrantRequest(input: {
47
+ merchant_id: string;
48
+ actor: ActorContext;
49
+ subject_id: string;
50
+ purpose: DataUsePurpose;
51
+ resource_scope_id: string;
52
+ request_ttl_seconds?: number;
53
+ grant_ttl_seconds?: number;
54
+ }): {
55
+ grant_request: GrantRequestRecord;
56
+ confirmation_phrase: string;
57
+ confirmation_ref: string;
58
+ };
59
+ /**
60
+ * 一次性确认:phrase 或 confirmation_ref 任一匹配即可;消费后重放/过期/
61
+ * 错误引用一律拒绝,不创建 grant。确认方必须是已绑定到请求主体的客户
62
+ * 会话,且不能是请求签发者本人。
63
+ */
64
+ confirmGrant(input: {
65
+ merchant_id: string;
66
+ actor: ActorContext;
67
+ grant_request_id: string;
68
+ confirmation_phrase?: string;
69
+ confirmation_ref?: string;
70
+ }): DataUseGrant;
71
+ getGrantRequest(merchantId: string, requestId: string): GrantRequestRecord | undefined;
72
+ }