@harrylabsj/kiwi 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.
- package/README.md +8 -0
- package/dist/a2a/inquiry-nonce-store.d.ts +12 -1
- package/dist/a2a/inquiry-nonce-store.js +49 -17
- package/dist/a2a/inquiry-nonce-store.js.map +1 -1
- package/dist/a2a/negotiate.d.ts +30 -1
- package/dist/a2a/negotiate.js +121 -32
- package/dist/a2a/negotiate.js.map +1 -1
- package/dist/a2a/node.d.ts +4 -0
- package/dist/a2a/node.js +182 -4
- package/dist/a2a/node.js.map +1 -1
- package/dist/a2a/open-inquiry-session.d.ts +1 -0
- package/dist/a2a/open-inquiry-session.js +3 -1
- package/dist/a2a/open-inquiry-session.js.map +1 -1
- package/dist/a2a/open-inquiry-verifier.js +2 -2
- package/dist/a2a/open-inquiry-verifier.js.map +1 -1
- package/dist/a2a/reception/agent-runtime.d.ts +32 -1
- package/dist/a2a/reception/agent-runtime.js +420 -11
- package/dist/a2a/reception/agent-runtime.js.map +1 -1
- package/dist/a2a/reception/customer-memory-tools.d.ts +73 -0
- package/dist/a2a/reception/customer-memory-tools.js +17 -0
- package/dist/a2a/reception/customer-memory-tools.js.map +1 -0
- package/dist/a2a/reception/engine.d.ts +12 -0
- package/dist/a2a/reception/engine.js +42 -1
- package/dist/a2a/reception/engine.js.map +1 -1
- package/dist/a2a/reception/publish.d.ts +14 -1
- package/dist/a2a/reception/publish.js +62 -1
- package/dist/a2a/reception/publish.js.map +1 -1
- package/dist/a2a/reception/question-spec.d.ts +91 -0
- package/dist/a2a/reception/question-spec.js +260 -0
- package/dist/a2a/reception/question-spec.js.map +1 -0
- package/dist/a2a/reception/reply-outbox.d.ts +6 -0
- package/dist/a2a/reception/reply-outbox.js +44 -0
- package/dist/a2a/reception/reply-outbox.js.map +1 -1
- package/dist/a2a/reception/responder.d.ts +13 -1
- package/dist/a2a/reception/responder.js +26 -3
- package/dist/a2a/reception/responder.js.map +1 -1
- package/dist/a2a/reception/skill-catalog.d.ts +72 -0
- package/dist/a2a/reception/skill-catalog.js +85 -0
- package/dist/a2a/reception/skill-catalog.js.map +1 -0
- package/dist/a2a/reception/skill-contract.d.ts +70 -0
- package/dist/a2a/reception/skill-contract.js +183 -0
- package/dist/a2a/reception/skill-contract.js.map +1 -0
- package/dist/a2a/reception/types.d.ts +12 -0
- package/dist/a2a/server/customer-identity-gateway.d.ts +111 -0
- package/dist/a2a/server/customer-identity-gateway.js +441 -0
- package/dist/a2a/server/customer-identity-gateway.js.map +1 -0
- package/dist/a2a/server/errors.d.ts +4 -3
- package/dist/a2a/server/errors.js +10 -2
- package/dist/a2a/server/errors.js.map +1 -1
- package/dist/a2a/server/merchant-handler.d.ts +75 -1
- package/dist/a2a/server/merchant-handler.js +676 -86
- package/dist/a2a/server/merchant-handler.js.map +1 -1
- package/dist/a2a/server/merchant-procurement.d.ts +90 -0
- package/dist/a2a/server/merchant-procurement.js +75 -0
- package/dist/a2a/server/merchant-procurement.js.map +1 -0
- package/dist/a2a/server/merchant-product-facts.d.ts +2 -0
- package/dist/a2a/server/merchant-product-facts.js.map +1 -1
- package/dist/a2a/server/negotiation-approvals.d.ts +11 -0
- package/dist/a2a/server/negotiation-approvals.js +43 -0
- package/dist/a2a/server/negotiation-approvals.js.map +1 -1
- package/dist/a2a/server/pipeline.js +78 -30
- package/dist/a2a/server/pipeline.js.map +1 -1
- package/dist/a2a/server/server.js +4 -3
- package/dist/a2a/server/server.js.map +1 -1
- package/dist/a2a/server/types.d.ts +4 -1
- package/dist/a2a/unit-alias.d.ts +3 -3
- package/dist/a2a/unit-alias.js +9 -5
- package/dist/a2a/unit-alias.js.map +1 -1
- package/dist/agent/alipay-native-view.d.ts +27 -0
- package/dist/agent/alipay-native-view.js +113 -0
- package/dist/agent/alipay-native-view.js.map +1 -0
- package/dist/agent/buyer/buyer-tools.d.ts +20 -0
- package/dist/agent/buyer/buyer-tools.js +241 -48
- package/dist/agent/buyer/buyer-tools.js.map +1 -1
- package/dist/agent/buyer/types.d.ts +1 -1
- package/dist/agent/buyer/types.js.map +1 -1
- package/dist/agent/chat-tui.js +16 -1
- package/dist/agent/chat-tui.js.map +1 -1
- package/dist/agent/kernel-builder.d.ts +2 -0
- package/dist/agent/kernel-builder.js +14 -0
- package/dist/agent/kernel-builder.js.map +1 -1
- package/dist/agent/kernel.d.ts +8 -0
- package/dist/agent/kernel.js +67 -1
- package/dist/agent/kernel.js.map +1 -1
- package/dist/auth/merchant-sessions.d.ts +3 -0
- package/dist/auth/merchant-sessions.js +21 -5
- package/dist/auth/merchant-sessions.js.map +1 -1
- package/dist/buyer-core/a2a-accept-terms.d.ts +11 -0
- package/dist/buyer-core/a2a-accept-terms.js +28 -0
- package/dist/buyer-core/a2a-accept-terms.js.map +1 -0
- package/dist/buyer-core/a2a-knp.d.ts +7 -2
- package/dist/buyer-core/a2a-knp.js +8 -5
- package/dist/buyer-core/a2a-knp.js.map +1 -1
- package/dist/buyer-core/a2a-negotiator.d.ts +17 -0
- package/dist/buyer-core/a2a-negotiator.js +70 -26
- package/dist/buyer-core/a2a-negotiator.js.map +1 -1
- package/dist/buyer-core/a2a-quote-fetcher.d.ts +25 -15
- package/dist/buyer-core/a2a-quote-fetcher.js +92 -19
- package/dist/buyer-core/a2a-quote-fetcher.js.map +1 -1
- package/dist/buyer-core/a2a-quote-history.d.ts +94 -0
- package/dist/buyer-core/a2a-quote-history.js +149 -0
- package/dist/buyer-core/a2a-quote-history.js.map +1 -0
- package/dist/buyer-core/build-service.d.ts +19 -0
- package/dist/buyer-core/build-service.js +30 -1
- package/dist/buyer-core/build-service.js.map +1 -1
- package/dist/buyer-core/errors.d.ts +1 -1
- package/dist/buyer-core/errors.js +1 -0
- package/dist/buyer-core/errors.js.map +1 -1
- package/dist/buyer-core/merchant-index.d.ts +2 -2
- package/dist/buyer-core/merchant-index.js +23 -5
- package/dist/buyer-core/merchant-index.js.map +1 -1
- package/dist/buyer-core/open-inquiry-signer.d.ts +1 -0
- package/dist/buyer-core/open-inquiry-signer.js +5 -2
- package/dist/buyer-core/open-inquiry-signer.js.map +1 -1
- package/dist/buyer-core/procurement/cases.d.ts +441 -0
- package/dist/buyer-core/procurement/cases.js +457 -0
- package/dist/buyer-core/procurement/cases.js.map +1 -0
- package/dist/buyer-core/procurement/quote-comparison.d.ts +137 -0
- package/dist/buyer-core/procurement/quote-comparison.js +106 -0
- package/dist/buyer-core/procurement/quote-comparison.js.map +1 -0
- package/dist/buyer-core/procurement/quote-normalizer.d.ts +45 -0
- package/dist/buyer-core/procurement/quote-normalizer.js +153 -0
- package/dist/buyer-core/procurement/quote-normalizer.js.map +1 -0
- package/dist/buyer-core/procurement/requirements.d.ts +50 -0
- package/dist/buyer-core/procurement/requirements.js +43 -0
- package/dist/buyer-core/procurement/requirements.js.map +1 -0
- package/dist/buyer-core/procurement/source-observation.d.ts +57 -0
- package/dist/buyer-core/procurement/source-observation.js +54 -0
- package/dist/buyer-core/procurement/source-observation.js.map +1 -0
- package/dist/buyer-core/procurement/store.d.ts +21 -0
- package/dist/buyer-core/procurement/store.js +68 -0
- package/dist/buyer-core/procurement/store.js.map +1 -0
- package/dist/buyer-core/protocol-recovery.d.ts +10 -1
- package/dist/buyer-core/protocol-recovery.js +31 -6
- package/dist/buyer-core/protocol-recovery.js.map +1 -1
- package/dist/buyer-core/quote-quantity.d.ts +5 -0
- package/dist/buyer-core/quote-quantity.js +15 -0
- package/dist/buyer-core/quote-quantity.js.map +1 -0
- package/dist/buyer-core/service.d.ts +128 -8
- package/dist/buyer-core/service.js +543 -75
- package/dist/buyer-core/service.js.map +1 -1
- package/dist/buyer-core/store.d.ts +21 -0
- package/dist/buyer-core/store.js +62 -10
- package/dist/buyer-core/store.js.map +1 -1
- package/dist/cli.js +26 -19
- package/dist/cli.js.map +1 -1
- package/dist/cloud/alipay-collection-http.d.ts +44 -0
- package/dist/cloud/alipay-collection-http.js +111 -0
- package/dist/cloud/alipay-collection-http.js.map +1 -0
- package/dist/cloud/bootstrap.js +218 -19
- package/dist/cloud/bootstrap.js.map +1 -1
- package/dist/cloud/http-router.d.ts +2 -0
- package/dist/cloud/http-router.js +5 -0
- package/dist/cloud/http-router.js.map +1 -1
- package/dist/commerce/receipt/dispatcher.d.ts +43 -0
- package/dist/commerce/receipt/dispatcher.js +86 -0
- package/dist/commerce/receipt/dispatcher.js.map +1 -0
- package/dist/commerce/receipt/receipts.d.ts +147 -0
- package/dist/commerce/receipt/receipts.js +285 -0
- package/dist/commerce/receipt/receipts.js.map +1 -0
- package/dist/config/profile.d.ts +2 -2
- package/dist/discovery/catalog-source/buyer-follows.d.ts +3 -3
- package/dist/discovery/catalog-source/buyer-follows.js +6 -3
- package/dist/discovery/catalog-source/buyer-follows.js.map +1 -1
- package/dist/discovery/catalog-source/kiwi-record.d.ts +2 -0
- package/dist/discovery/catalog-source/kiwi-record.js.map +1 -1
- package/dist/discovery/catalog-source/kiwi-source.js +4 -0
- package/dist/discovery/catalog-source/kiwi-source.js.map +1 -1
- package/dist/handoff/alipay-collection.d.ts +23 -0
- package/dist/handoff/alipay-collection.js +70 -0
- package/dist/handoff/alipay-collection.js.map +1 -0
- package/dist/handoff/order-receipts.d.ts +62 -0
- package/dist/handoff/order-receipts.js +72 -0
- package/dist/handoff/order-receipts.js.map +1 -0
- package/dist/handoff/ucp-checkout/cart-channel.d.ts +2 -1
- package/dist/handoff/ucp-checkout/cart-channel.js +1 -0
- package/dist/handoff/ucp-checkout/cart-channel.js.map +1 -1
- package/dist/handoff/ucp-checkout/channel.d.ts +7 -0
- package/dist/handoff/ucp-checkout/channel.js +79 -10
- package/dist/handoff/ucp-checkout/channel.js.map +1 -1
- package/dist/http/cli.js +6 -0
- package/dist/http/cli.js.map +1 -1
- package/dist/http/merchant-management/api.d.ts +18 -0
- package/dist/http/merchant-management/api.js +752 -3
- package/dist/http/merchant-management/api.js.map +1 -1
- package/dist/http/merchant-management/draft-store.js +8 -6
- package/dist/http/merchant-management/draft-store.js.map +1 -1
- package/dist/http/merchant-management/page.d.ts +1 -0
- package/dist/http/merchant-management/page.js +335 -5
- package/dist/http/merchant-management/page.js.map +1 -1
- package/dist/http/server.js +9 -2
- package/dist/http/server.js.map +1 -1
- package/dist/mcp/cli.d.ts +3 -1
- package/dist/mcp/cli.js +18 -2
- package/dist/mcp/cli.js.map +1 -1
- package/dist/mcp/merchant-runtime-assembly.d.ts +20 -0
- package/dist/mcp/merchant-runtime-assembly.js +94 -11
- package/dist/mcp/merchant-runtime-assembly.js.map +1 -1
- package/dist/mcp/merchant-server.d.ts +5 -0
- package/dist/mcp/merchant-server.js +12 -0
- package/dist/mcp/merchant-server.js.map +1 -1
- package/dist/mcp/procurement-tools.d.ts +10 -0
- package/dist/mcp/procurement-tools.js +173 -0
- package/dist/mcp/procurement-tools.js.map +1 -0
- package/dist/mcp/server.d.ts +4 -0
- package/dist/mcp/server.js +63 -9
- package/dist/mcp/server.js.map +1 -1
- package/dist/mcp/tools.d.ts +4 -1
- package/dist/mcp/tools.js +83 -28
- package/dist/mcp/tools.js.map +1 -1
- package/dist/mcp/types.d.ts +1 -0
- package/dist/mcp/types.js.map +1 -1
- package/dist/merchant/application/actor.d.ts +3 -1
- package/dist/merchant/application/actor.js +21 -0
- package/dist/merchant/application/actor.js.map +1 -1
- package/dist/merchant/negotiation-ops.d.ts +2 -0
- package/dist/merchant/negotiation-ops.js +4 -0
- package/dist/merchant/negotiation-ops.js.map +1 -1
- package/dist/merchant/weixin-channel.d.ts +68 -0
- package/dist/merchant/weixin-channel.js +313 -0
- package/dist/merchant/weixin-channel.js.map +1 -0
- package/dist/merchant/workbench-server.d.ts +1 -0
- package/dist/merchant/workbench-server.js +189 -2
- package/dist/merchant/workbench-server.js.map +1 -1
- package/dist/merchant-core/budget/background-budget.d.ts +132 -0
- package/dist/merchant-core/budget/background-budget.js +308 -0
- package/dist/merchant-core/budget/background-budget.js.map +1 -0
- package/dist/merchant-core/collection/alipay-store.d.ts +22 -0
- package/dist/merchant-core/collection/alipay-store.js +125 -0
- package/dist/merchant-core/collection/alipay-store.js.map +1 -0
- package/dist/merchant-core/commercial/calculate.js +6 -6
- package/dist/merchant-core/commercial/calculate.js.map +1 -1
- package/dist/merchant-core/context-governance/actor-context.d.ts +56 -0
- package/dist/merchant-core/context-governance/actor-context.js +76 -0
- package/dist/merchant-core/context-governance/actor-context.js.map +1 -0
- package/dist/merchant-core/context-governance/authority.d.ts +39 -0
- package/dist/merchant-core/context-governance/authority.js +28 -0
- package/dist/merchant-core/context-governance/authority.js.map +1 -0
- package/dist/merchant-core/context-governance/context-guard.d.ts +104 -0
- package/dist/merchant-core/context-governance/context-guard.js +194 -0
- package/dist/merchant-core/context-governance/context-guard.js.map +1 -0
- package/dist/merchant-core/context-governance/data-use-grant.d.ts +91 -0
- package/dist/merchant-core/context-governance/data-use-grant.js +211 -0
- package/dist/merchant-core/context-governance/data-use-grant.js.map +1 -0
- package/dist/merchant-core/context-governance/feature-flags.d.ts +32 -0
- package/dist/merchant-core/context-governance/feature-flags.js +83 -0
- package/dist/merchant-core/context-governance/feature-flags.js.map +1 -0
- package/dist/merchant-core/context-governance/pilot-readiness.d.ts +39 -0
- package/dist/merchant-core/context-governance/pilot-readiness.js +141 -0
- package/dist/merchant-core/context-governance/pilot-readiness.js.map +1 -0
- package/dist/merchant-core/context-governance/source-governance.d.ts +157 -0
- package/dist/merchant-core/context-governance/source-governance.js +492 -0
- package/dist/merchant-core/context-governance/source-governance.js.map +1 -0
- package/dist/merchant-core/context-governance/types.d.ts +62 -0
- package/dist/merchant-core/context-governance/types.js +92 -0
- package/dist/merchant-core/context-governance/types.js.map +1 -0
- package/dist/merchant-core/customer-delivery/binding.d.ts +55 -0
- package/dist/merchant-core/customer-delivery/binding.js +121 -0
- package/dist/merchant-core/customer-delivery/binding.js.map +1 -0
- package/dist/merchant-core/customer-delivery/channel.d.ts +75 -0
- package/dist/merchant-core/customer-delivery/channel.js +157 -0
- package/dist/merchant-core/customer-delivery/channel.js.map +1 -0
- package/dist/merchant-core/customer-delivery/consent-repository.d.ts +50 -0
- package/dist/merchant-core/customer-delivery/consent-repository.js +160 -0
- package/dist/merchant-core/customer-delivery/consent-repository.js.map +1 -0
- package/dist/merchant-core/customer-delivery/dispatcher-loop.d.ts +68 -0
- package/dist/merchant-core/customer-delivery/dispatcher-loop.js +132 -0
- package/dist/merchant-core/customer-delivery/dispatcher-loop.js.map +1 -0
- package/dist/merchant-core/customer-delivery/dispatcher.d.ts +118 -0
- package/dist/merchant-core/customer-delivery/dispatcher.js +166 -0
- package/dist/merchant-core/customer-delivery/dispatcher.js.map +1 -0
- package/dist/merchant-core/customer-delivery/email-adapter.d.ts +31 -0
- package/dist/merchant-core/customer-delivery/email-adapter.js +90 -0
- package/dist/merchant-core/customer-delivery/email-adapter.js.map +1 -0
- package/dist/merchant-core/customer-delivery/gate.d.ts +37 -0
- package/dist/merchant-core/customer-delivery/gate.js +196 -0
- package/dist/merchant-core/customer-delivery/gate.js.map +1 -0
- package/dist/merchant-core/customer-delivery/reconciliation.d.ts +13 -0
- package/dist/merchant-core/customer-delivery/reconciliation.js +70 -0
- package/dist/merchant-core/customer-delivery/reconciliation.js.map +1 -0
- package/dist/merchant-core/customer-delivery/repository.d.ts +55 -0
- package/dist/merchant-core/customer-delivery/repository.js +235 -0
- package/dist/merchant-core/customer-delivery/repository.js.map +1 -0
- package/dist/merchant-core/customer-delivery/smtp-adapter.d.ts +30 -0
- package/dist/merchant-core/customer-delivery/smtp-adapter.js +183 -0
- package/dist/merchant-core/customer-delivery/smtp-adapter.js.map +1 -0
- package/dist/merchant-core/customer-delivery/stream-gate.d.ts +45 -0
- package/dist/merchant-core/customer-delivery/stream-gate.js +123 -0
- package/dist/merchant-core/customer-delivery/stream-gate.js.map +1 -0
- package/dist/merchant-core/customer-delivery/types.d.ts +103 -0
- package/dist/merchant-core/customer-delivery/types.js +32 -0
- package/dist/merchant-core/customer-delivery/types.js.map +1 -0
- package/dist/merchant-core/customer-delivery/weixin-adapter.d.ts +35 -0
- package/dist/merchant-core/customer-delivery/weixin-adapter.js +56 -0
- package/dist/merchant-core/customer-delivery/weixin-adapter.js.map +1 -0
- package/dist/merchant-core/customer-memory/auto-identity.d.ts +43 -0
- package/dist/merchant-core/customer-memory/auto-identity.js +163 -0
- package/dist/merchant-core/customer-memory/auto-identity.js.map +1 -0
- package/dist/merchant-core/customer-memory/grants.d.ts +72 -0
- package/dist/merchant-core/customer-memory/grants.js +224 -0
- package/dist/merchant-core/customer-memory/grants.js.map +1 -0
- package/dist/merchant-core/customer-memory/history.d.ts +51 -0
- package/dist/merchant-core/customer-memory/history.js +252 -0
- package/dist/merchant-core/customer-memory/history.js.map +1 -0
- package/dist/merchant-core/customer-memory/identity.d.ts +66 -0
- package/dist/merchant-core/customer-memory/identity.js +249 -0
- package/dist/merchant-core/customer-memory/identity.js.map +1 -0
- package/dist/merchant-core/customer-memory/maintenance.d.ts +51 -0
- package/dist/merchant-core/customer-memory/maintenance.js +67 -0
- package/dist/merchant-core/customer-memory/maintenance.js.map +1 -0
- package/dist/merchant-core/customer-memory/memory.d.ts +119 -0
- package/dist/merchant-core/customer-memory/memory.js +502 -0
- package/dist/merchant-core/customer-memory/memory.js.map +1 -0
- package/dist/merchant-core/customer-memory/private-transport.d.ts +10 -0
- package/dist/merchant-core/customer-memory/private-transport.js +108 -0
- package/dist/merchant-core/customer-memory/private-transport.js.map +1 -0
- package/dist/merchant-core/customer-memory/quote-history.d.ts +160 -0
- package/dist/merchant-core/customer-memory/quote-history.js +392 -0
- package/dist/merchant-core/customer-memory/quote-history.js.map +1 -0
- package/dist/merchant-core/customer-memory/reception-provider.d.ts +69 -0
- package/dist/merchant-core/customer-memory/reception-provider.js +250 -0
- package/dist/merchant-core/customer-memory/reception-provider.js.map +1 -0
- package/dist/merchant-core/customer-memory/reception-writer.d.ts +19 -0
- package/dist/merchant-core/customer-memory/reception-writer.js +32 -0
- package/dist/merchant-core/customer-memory/reception-writer.js.map +1 -0
- package/dist/merchant-core/customer-memory/types.d.ts +111 -0
- package/dist/merchant-core/customer-memory/types.js +58 -0
- package/dist/merchant-core/customer-memory/types.js.map +1 -0
- package/dist/merchant-core/followup/repository.d.ts +2 -1
- package/dist/merchant-core/followup/repository.js +29 -23
- package/dist/merchant-core/followup/repository.js.map +1 -1
- package/dist/merchant-core/learning/evaluation-fixtures.d.ts +38 -0
- package/dist/merchant-core/learning/evaluation-fixtures.js +83 -0
- package/dist/merchant-core/learning/evaluation-fixtures.js.map +1 -0
- package/dist/merchant-core/learning/evaluation-suite-v1.d.ts +56 -0
- package/dist/merchant-core/learning/evaluation-suite-v1.js +129 -0
- package/dist/merchant-core/learning/evaluation-suite-v1.js.map +1 -0
- package/dist/merchant-core/learning/evaluation.d.ts +84 -0
- package/dist/merchant-core/learning/evaluation.js +581 -0
- package/dist/merchant-core/learning/evaluation.js.map +1 -0
- package/dist/merchant-core/learning/initial-skills.d.ts +36 -0
- package/dist/merchant-core/learning/initial-skills.js +67 -0
- package/dist/merchant-core/learning/initial-skills.js.map +1 -0
- package/dist/merchant-core/learning/precheck.d.ts +33 -0
- package/dist/merchant-core/learning/precheck.js +56 -0
- package/dist/merchant-core/learning/precheck.js.map +1 -0
- package/dist/merchant-core/learning/real-evaluation-runner.d.ts +52 -0
- package/dist/merchant-core/learning/real-evaluation-runner.js +167 -0
- package/dist/merchant-core/learning/real-evaluation-runner.js.map +1 -0
- package/dist/merchant-core/learning/reception-skill-catalog.d.ts +39 -0
- package/dist/merchant-core/learning/reception-skill-catalog.js +145 -0
- package/dist/merchant-core/learning/reception-skill-catalog.js.map +1 -0
- package/dist/merchant-core/learning/recovery-gateway.d.ts +27 -0
- package/dist/merchant-core/learning/recovery-gateway.js +106 -0
- package/dist/merchant-core/learning/recovery-gateway.js.map +1 -0
- package/dist/merchant-core/learning/repository.d.ts +130 -0
- package/dist/merchant-core/learning/repository.js +588 -0
- package/dist/merchant-core/learning/repository.js.map +1 -0
- package/dist/merchant-core/learning/service.d.ts +262 -0
- package/dist/merchant-core/learning/service.js +1498 -0
- package/dist/merchant-core/learning/service.js.map +1 -0
- package/dist/merchant-core/learning/test-dispatch-gateway.d.ts +43 -0
- package/dist/merchant-core/learning/test-dispatch-gateway.js +145 -0
- package/dist/merchant-core/learning/test-dispatch-gateway.js.map +1 -0
- package/dist/merchant-core/learning/types.d.ts +217 -0
- package/dist/merchant-core/learning/types.js +52 -0
- package/dist/merchant-core/learning/types.js.map +1 -0
- package/dist/merchant-core/migrations/ledgers.d.ts +40 -0
- package/dist/merchant-core/migrations/ledgers.js +824 -0
- package/dist/merchant-core/migrations/ledgers.js.map +1 -0
- package/dist/merchant-core/migrations/runner.d.ts +55 -0
- package/dist/merchant-core/migrations/runner.js +114 -0
- package/dist/merchant-core/migrations/runner.js.map +1 -0
- package/dist/merchant-core/operating/repository.d.ts +0 -1
- package/dist/merchant-core/operating/repository.js +2 -17
- package/dist/merchant-core/operating/repository.js.map +1 -1
- package/dist/merchant-core/operations-jobs/advice-runner.d.ts +63 -0
- package/dist/merchant-core/operations-jobs/advice-runner.js +199 -0
- package/dist/merchant-core/operations-jobs/advice-runner.js.map +1 -0
- package/dist/merchant-core/operations-jobs/pilot-evidence.d.ts +64 -0
- package/dist/merchant-core/operations-jobs/pilot-evidence.js +109 -0
- package/dist/merchant-core/operations-jobs/pilot-evidence.js.map +1 -0
- package/dist/merchant-core/operations-jobs/pilot-metrics.d.ts +70 -0
- package/dist/merchant-core/operations-jobs/pilot-metrics.js +98 -0
- package/dist/merchant-core/operations-jobs/pilot-metrics.js.map +1 -0
- package/dist/merchant-core/operations-jobs/projection.d.ts +41 -0
- package/dist/merchant-core/operations-jobs/projection.js +158 -0
- package/dist/merchant-core/operations-jobs/projection.js.map +1 -0
- package/dist/merchant-core/operations-jobs/scheduler.d.ts +68 -0
- package/dist/merchant-core/operations-jobs/scheduler.js +233 -0
- package/dist/merchant-core/operations-jobs/scheduler.js.map +1 -0
- package/dist/merchant-core/operations-jobs/service.d.ts +260 -0
- package/dist/merchant-core/operations-jobs/service.js +922 -0
- package/dist/merchant-core/operations-jobs/service.js.map +1 -0
- package/dist/merchant-core/opportunity/repository.d.ts +0 -1
- package/dist/merchant-core/opportunity/repository.js +2 -17
- package/dist/merchant-core/opportunity/repository.js.map +1 -1
- package/dist/merchant-core/ops/facade.d.ts +10 -0
- package/dist/merchant-core/ops/facade.js +52 -0
- package/dist/merchant-core/ops/facade.js.map +1 -1
- package/dist/merchant-core/ops/schema.d.ts +1 -1
- package/dist/merchant-core/ops/schema.js +119 -2
- package/dist/merchant-core/ops/schema.js.map +1 -1
- package/dist/merchant-core/ops/surface.d.ts +37 -0
- package/dist/merchant-core/ops/surface.js +240 -0
- package/dist/merchant-core/ops/surface.js.map +1 -1
- package/dist/merchant-core/orders/model.d.ts +1 -8
- package/dist/merchant-core/orders/model.js +10 -2
- package/dist/merchant-core/orders/model.js.map +1 -1
- package/dist/merchant-core/orders/repository.d.ts +0 -1
- package/dist/merchant-core/orders/repository.js +3 -18
- package/dist/merchant-core/orders/repository.js.map +1 -1
- package/dist/merchant-core/orders/service.js +34 -8
- package/dist/merchant-core/orders/service.js.map +1 -1
- package/dist/merchant-core/ownership/instance-ownership.d.ts +122 -0
- package/dist/merchant-core/ownership/instance-ownership.js +236 -0
- package/dist/merchant-core/ownership/instance-ownership.js.map +1 -0
- package/dist/merchant-core/ownership/runtime.d.ts +175 -0
- package/dist/merchant-core/ownership/runtime.js +422 -0
- package/dist/merchant-core/ownership/runtime.js.map +1 -0
- package/dist/merchant-core/proposal/engine.js +2 -0
- package/dist/merchant-core/proposal/engine.js.map +1 -1
- package/dist/merchant-core/proposal/publish.d.ts +0 -6
- package/dist/merchant-core/proposal/publish.js +37 -3
- package/dist/merchant-core/proposal/publish.js.map +1 -1
- package/dist/merchant-core/proposal/repository.d.ts +0 -7
- package/dist/merchant-core/proposal/repository.js +8 -17
- package/dist/merchant-core/proposal/repository.js.map +1 -1
- package/dist/merchant-core/proposal/types.d.ts +1 -0
- package/dist/merchant-core/proposal/types.js.map +1 -1
- package/dist/merchant-core/rfq/policy.d.ts +1 -0
- package/dist/merchant-core/rfq/policy.js +6 -7
- package/dist/merchant-core/rfq/policy.js.map +1 -1
- package/dist/merchant-core/rfq/repository.d.ts +0 -1
- package/dist/merchant-core/rfq/repository.js +2 -17
- package/dist/merchant-core/rfq/repository.js.map +1 -1
- package/dist/merchant-core/rfq/service.d.ts +2 -1
- package/dist/merchant-core/rfq/service.js +2 -4
- package/dist/merchant-core/rfq/service.js.map +1 -1
- package/dist/merchant-core/storage/transaction.d.ts +95 -7
- package/dist/merchant-core/storage/transaction.js +251 -2
- package/dist/merchant-core/storage/transaction.js.map +1 -1
- package/dist/merchant-credential-rotation.d.ts +4 -1
- package/dist/merchant-credential-rotation.js +12 -8
- package/dist/merchant-credential-rotation.js.map +1 -1
- package/dist/merchant-runtime/manager.d.ts +2 -0
- package/dist/merchant-runtime/manager.js +5 -0
- package/dist/merchant-runtime/manager.js.map +1 -1
- package/dist/negotiation/domain/objects.d.ts +2 -0
- package/dist/negotiation/domain/objects.js +3 -0
- package/dist/negotiation/domain/objects.js.map +1 -1
- package/dist/negotiation/ledger/store.d.ts +4 -0
- package/dist/negotiation/ledger/store.js +20 -0
- package/dist/negotiation/ledger/store.js.map +1 -1
- package/dist/product-init.d.ts +1 -0
- package/dist/product-init.js +4 -1
- package/dist/product-init.js.map +1 -1
- package/dist/trust/identity/message-signature.d.ts +1 -1
- package/dist/trust/identity/message-signature.js +6 -1
- package/dist/trust/identity/message-signature.js.map +1 -1
- package/dist/trust/identity/nonce.d.ts +5 -0
- package/dist/trust/identity/nonce.js +2 -0
- package/dist/trust/identity/nonce.js.map +1 -1
- package/dist/weixin/ilink-client.d.ts +3 -2
- package/dist/weixin/ilink-client.js +9 -2
- package/dist/weixin/ilink-client.js.map +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,224 @@
|
|
|
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
|
+
* 客户授权请求与一次性确认(设计 v1.1 §9.2 / D-M0-02 / M3-1)。
|
|
18
|
+
*
|
|
19
|
+
* 两方流程(48 小时审查 P0-1):商家侧(memory:write)创建请求并取得
|
|
20
|
+
* 一次性短语/卡片引用,经带外渠道送达已验证的客户联系方式;客户会话
|
|
21
|
+
* (customer:self)回传短语或卡片引用完成确认。签发者不得自我确认,且
|
|
22
|
+
* 确认方必须已绑定到请求主体——短语本身不能替代主体绑定。
|
|
23
|
+
*/
|
|
24
|
+
import { randomBytes, randomUUID } from "node:crypto";
|
|
25
|
+
import { contentDigest } from "../../negotiation/jcs.js";
|
|
26
|
+
import { hasCapability } from "../context-governance/actor-context.js";
|
|
27
|
+
import { DATA_USE_PURPOSES, GOVERNANCE_POLICY_VERSION, } from "../context-governance/types.js";
|
|
28
|
+
import { DataUseGrantStore, } from "../context-governance/data-use-grant.js";
|
|
29
|
+
import { inImmediateTransaction } from "../storage/transaction.js";
|
|
30
|
+
import { CustomerMemoryError } from "./types.js";
|
|
31
|
+
const DEFAULT_REQUEST_TTL_SECONDS = 900;
|
|
32
|
+
const MAX_GRANT_TTL_SECONDS = 90 * 24 * 60 * 60;
|
|
33
|
+
function text(value, field, max = 128) {
|
|
34
|
+
if (typeof value !== "string" || value.length === 0 || value.length > max) {
|
|
35
|
+
throw new CustomerMemoryError("validation", `${field}_invalid`, `${field} 必须是 1..${max} 字符`);
|
|
36
|
+
}
|
|
37
|
+
// eslint-disable-next-line no-control-regex
|
|
38
|
+
if (/[\u0000-\u001f\u007f]/.test(value)) {
|
|
39
|
+
throw new CustomerMemoryError("validation", `${field}_invalid`, `${field} 含控制字符`);
|
|
40
|
+
}
|
|
41
|
+
return value;
|
|
42
|
+
}
|
|
43
|
+
function requireActor(actor, merchantId, capability) {
|
|
44
|
+
if (actor.merchant_id !== merchantId) {
|
|
45
|
+
throw new CustomerMemoryError("access_denied", "actor_merchant_mismatch", "ActorContext 与商家作用域不一致");
|
|
46
|
+
}
|
|
47
|
+
if (actor.policy_version !== GOVERNANCE_POLICY_VERSION) {
|
|
48
|
+
throw new CustomerMemoryError("validation", "policy_version_mismatch", "治理策略版本不一致");
|
|
49
|
+
}
|
|
50
|
+
if (!hasCapability(actor, capability)) {
|
|
51
|
+
throw new CustomerMemoryError("access_denied", "capability_missing", `actor 缺少 ${capability}`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
function nowUtc(now) {
|
|
55
|
+
const ms = Date.parse(now());
|
|
56
|
+
if (!Number.isFinite(ms)) {
|
|
57
|
+
throw new CustomerMemoryError("validation", "now_invalid", "时钟输出必须是可解析的 RFC3339 时间");
|
|
58
|
+
}
|
|
59
|
+
return new Date(ms).toISOString();
|
|
60
|
+
}
|
|
61
|
+
export class GrantRequestService {
|
|
62
|
+
db;
|
|
63
|
+
now;
|
|
64
|
+
grants;
|
|
65
|
+
constructor(options) {
|
|
66
|
+
this.db = options.db;
|
|
67
|
+
this.now = options.now;
|
|
68
|
+
this.grants = new DataUseGrantStore({ db: options.db, now: options.now });
|
|
69
|
+
}
|
|
70
|
+
/** 当前 actor 的有效绑定主体;未绑定/已撤销/过期返回 undefined。 */
|
|
71
|
+
boundSubject(merchantId, actorId, stamp) {
|
|
72
|
+
const row = this.db
|
|
73
|
+
.prepare(`SELECT subject_id FROM subject_bindings
|
|
74
|
+
WHERE merchant_id = ? AND actor_id = ? AND status = 'ACTIVE' AND expires_at > ?
|
|
75
|
+
ORDER BY binding_epoch DESC LIMIT 1`)
|
|
76
|
+
.get(merchantId, actorId, stamp);
|
|
77
|
+
return row?.subject_id;
|
|
78
|
+
}
|
|
79
|
+
assertScopeUsable(merchantId, scopeId) {
|
|
80
|
+
const row = this.db
|
|
81
|
+
.prepare("SELECT state FROM data_scopes WHERE merchant_id = ? AND erase_scope_id = ?")
|
|
82
|
+
.get(merchantId, scopeId);
|
|
83
|
+
// 未登记 scope 允许首次授权时建立(见下方 INSERT);已封存/删除的 scope
|
|
84
|
+
// 不能被新的 ACTIVE grant 复活。
|
|
85
|
+
if (row !== undefined && row.state !== "ACTIVE") {
|
|
86
|
+
throw new CustomerMemoryError("access_denied", "scope_sealed", "数据域已封存,不能新建授权");
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* 创建授权请求:返回一次性短语与卡片引用(各存摘要)。请求 TTL 默认 15
|
|
91
|
+
* 分钟;授权 TTL 默认 90 天,受 90 天上限(试点值)。
|
|
92
|
+
*/
|
|
93
|
+
createGrantRequest(input) {
|
|
94
|
+
const merchantId = text(input.merchant_id, "merchant_id");
|
|
95
|
+
// P0-1:只有商家侧(memory:write)能签发授权请求。客户会话拿到短语
|
|
96
|
+
// 就等于拿到证明——证明必须由商家经带外渠道送达,不能由客户自签自发。
|
|
97
|
+
requireActor(input.actor, merchantId, "memory:write");
|
|
98
|
+
if (!DATA_USE_PURPOSES.includes(input.purpose)) {
|
|
99
|
+
throw new CustomerMemoryError("validation", "purpose_unknown", "未知数据用途");
|
|
100
|
+
}
|
|
101
|
+
const subjectId = text(input.subject_id, "subject_id");
|
|
102
|
+
const subject = this.db
|
|
103
|
+
.prepare("SELECT status FROM customer_subjects WHERE merchant_id = ? AND subject_id = ?")
|
|
104
|
+
.get(merchantId, subjectId);
|
|
105
|
+
if (subject === undefined || subject.status !== "ACTIVE") {
|
|
106
|
+
throw new CustomerMemoryError("not_found", "subject_not_found", "主体不存在或不可见");
|
|
107
|
+
}
|
|
108
|
+
const scopeId = text(input.resource_scope_id, "resource_scope_id");
|
|
109
|
+
const requestTtl = input.request_ttl_seconds ?? DEFAULT_REQUEST_TTL_SECONDS;
|
|
110
|
+
if (!Number.isSafeInteger(requestTtl) || requestTtl < 60 || requestTtl > 3600) {
|
|
111
|
+
throw new CustomerMemoryError("validation", "request_ttl_invalid", "请求 TTL 必须为 60..3600 秒");
|
|
112
|
+
}
|
|
113
|
+
const grantTtl = input.grant_ttl_seconds ?? MAX_GRANT_TTL_SECONDS;
|
|
114
|
+
if (!Number.isSafeInteger(grantTtl) || grantTtl < 60 || grantTtl > MAX_GRANT_TTL_SECONDS) {
|
|
115
|
+
throw new CustomerMemoryError("validation", "grant_ttl_invalid", "授权 TTL 超出允许范围");
|
|
116
|
+
}
|
|
117
|
+
const stamp = nowUtc(this.now);
|
|
118
|
+
const grantRequestId = `greq_${randomUUID()}`;
|
|
119
|
+
const confirmationPhrase = `确认授权-${randomBytes(3).toString("hex").toUpperCase()}`;
|
|
120
|
+
const confirmationRef = `gref_${randomBytes(18).toString("base64url")}`;
|
|
121
|
+
const expiresAt = new Date(Date.parse(stamp) + requestTtl * 1000).toISOString();
|
|
122
|
+
this.db
|
|
123
|
+
.prepare(`INSERT INTO grant_requests
|
|
124
|
+
(grant_request_id, merchant_id, subject_id, purpose, resource_scope_id,
|
|
125
|
+
grant_ttl_seconds, phrase_digest, confirmation_ref_digest, expires_at,
|
|
126
|
+
consumed_at, created_at, issuer_actor_id)
|
|
127
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NULL, ?, ?)`)
|
|
128
|
+
.run(grantRequestId, merchantId, subjectId, input.purpose, scopeId, grantTtl, contentDigest({ purpose: "grant_phrase", phrase: confirmationPhrase }), contentDigest({ purpose: "grant_ref", ref: confirmationRef }), expiresAt, stamp, input.actor.actor_id);
|
|
129
|
+
return {
|
|
130
|
+
grant_request: {
|
|
131
|
+
grant_request_id: grantRequestId,
|
|
132
|
+
merchant_id: merchantId,
|
|
133
|
+
subject_id: subjectId,
|
|
134
|
+
purpose: input.purpose,
|
|
135
|
+
resource_scope_id: scopeId,
|
|
136
|
+
grant_ttl_seconds: grantTtl,
|
|
137
|
+
expires_at: expiresAt,
|
|
138
|
+
consumed_at: null,
|
|
139
|
+
created_at: stamp,
|
|
140
|
+
},
|
|
141
|
+
confirmation_phrase: confirmationPhrase,
|
|
142
|
+
confirmation_ref: confirmationRef,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* 一次性确认:phrase 或 confirmation_ref 任一匹配即可;消费后重放/过期/
|
|
147
|
+
* 错误引用一律拒绝,不创建 grant。确认方必须是已绑定到请求主体的客户
|
|
148
|
+
* 会话,且不能是请求签发者本人。
|
|
149
|
+
*/
|
|
150
|
+
confirmGrant(input) {
|
|
151
|
+
const merchantId = text(input.merchant_id, "merchant_id");
|
|
152
|
+
requireActor(input.actor, merchantId, "customer:self");
|
|
153
|
+
const requestId = text(input.grant_request_id, "grant_request_id");
|
|
154
|
+
if ((input.confirmation_phrase === undefined) === (input.confirmation_ref === undefined)) {
|
|
155
|
+
throw new CustomerMemoryError("validation", "confirmation_required", "必须提供短语或卡片引用之一");
|
|
156
|
+
}
|
|
157
|
+
return inImmediateTransaction(this.db, () => {
|
|
158
|
+
const row = this.db
|
|
159
|
+
.prepare(`SELECT grant_requests.grant_request_id, grant_requests.merchant_id,
|
|
160
|
+
grant_requests.subject_id, grant_requests.purpose,
|
|
161
|
+
grant_requests.resource_scope_id, grant_requests.grant_ttl_seconds,
|
|
162
|
+
grant_requests.expires_at, grant_requests.consumed_at,
|
|
163
|
+
grant_requests.created_at, grant_requests.phrase_digest,
|
|
164
|
+
grant_requests.confirmation_ref_digest, grant_requests.issuer_actor_id
|
|
165
|
+
FROM grant_requests
|
|
166
|
+
WHERE grant_requests.merchant_id = ? AND grant_requests.grant_request_id = ?`)
|
|
167
|
+
.get(merchantId, requestId);
|
|
168
|
+
const phraseOk = input.confirmation_phrase !== undefined &&
|
|
169
|
+
row?.phrase_digest === contentDigest({ purpose: "grant_phrase", phrase: input.confirmation_phrase });
|
|
170
|
+
const refOk = input.confirmation_ref !== undefined &&
|
|
171
|
+
row?.confirmation_ref_digest === contentDigest({ purpose: "grant_ref", ref: input.confirmation_ref });
|
|
172
|
+
if (row === undefined || (!phraseOk && !refOk)) {
|
|
173
|
+
throw new CustomerMemoryError("confirmation_invalid", "confirmation_mismatch", "确认短语或引用不匹配");
|
|
174
|
+
}
|
|
175
|
+
if (row.consumed_at !== null) {
|
|
176
|
+
throw new CustomerMemoryError("confirmation_consumed", "grant_request_consumed", "授权请求已消费");
|
|
177
|
+
}
|
|
178
|
+
const stamp = nowUtc(this.now);
|
|
179
|
+
if (row.expires_at <= stamp) {
|
|
180
|
+
throw new CustomerMemoryError("confirmation_expired", "grant_request_expired", "授权请求已过期");
|
|
181
|
+
}
|
|
182
|
+
// P0-1:同一 actor 既是签发者又是确认者 → 证明不成立;旧版自签自发
|
|
183
|
+
// 记录(issuer_actor_id 为 NULL)同样不可确认。
|
|
184
|
+
if (row.issuer_actor_id === null || row.issuer_actor_id === input.actor.actor_id) {
|
|
185
|
+
throw new CustomerMemoryError("access_denied", "issuer_cannot_confirm", "授权请求必须由已绑定的客户会话确认,签发者不能自我确认");
|
|
186
|
+
}
|
|
187
|
+
const boundSubject = this.boundSubject(merchantId, input.actor.actor_id, stamp);
|
|
188
|
+
if (boundSubject !== row.subject_id) {
|
|
189
|
+
throw new CustomerMemoryError("access_denied", "subject_not_bound", "当前会话未绑定到该授权主体");
|
|
190
|
+
}
|
|
191
|
+
const subjectRow = this.db
|
|
192
|
+
.prepare("SELECT status FROM customer_subjects WHERE merchant_id = ? AND subject_id = ?")
|
|
193
|
+
.get(merchantId, row.subject_id);
|
|
194
|
+
if (subjectRow === undefined || subjectRow.status !== "ACTIVE") {
|
|
195
|
+
throw new CustomerMemoryError("not_found", "subject_not_found", "主体不存在或不可见");
|
|
196
|
+
}
|
|
197
|
+
this.assertScopeUsable(merchantId, row.resource_scope_id);
|
|
198
|
+
this.db
|
|
199
|
+
.prepare("UPDATE grant_requests SET consumed_at = ? WHERE merchant_id = ? AND grant_request_id = ?")
|
|
200
|
+
.run(stamp, merchantId, requestId);
|
|
201
|
+
// 授权确认即建立资源域(未封存时);已封存 scope 不被复活。
|
|
202
|
+
this.db
|
|
203
|
+
.prepare(`INSERT INTO data_scopes
|
|
204
|
+
(merchant_id, erase_scope_id, data_epoch, state, sealed_at, updated_at)
|
|
205
|
+
VALUES (?, ?, 1, 'ACTIVE', NULL, ?)
|
|
206
|
+
ON CONFLICT(merchant_id, erase_scope_id) DO NOTHING`)
|
|
207
|
+
.run(merchantId, row.resource_scope_id, stamp);
|
|
208
|
+
const expiresAt = new Date(Date.parse(stamp) + row.grant_ttl_seconds * 1000).toISOString();
|
|
209
|
+
return this.grants.createGrant({
|
|
210
|
+
merchant_id: merchantId,
|
|
211
|
+
subject_id: row.subject_id,
|
|
212
|
+
purpose: row.purpose,
|
|
213
|
+
resource_scope_id: row.resource_scope_id,
|
|
214
|
+
expires_at: expiresAt,
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
getGrantRequest(merchantId, requestId) {
|
|
219
|
+
return this.db
|
|
220
|
+
.prepare("SELECT * FROM grant_requests WHERE merchant_id = ? AND grant_request_id = ?")
|
|
221
|
+
.get(text(merchantId, "merchant_id"), text(requestId, "grant_request_id"));
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
//# sourceMappingURL=grants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grants.js","sourceRoot":"","sources":["../../../src/merchant-core/customer-memory/grants.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH;;;;;;;GAOG;AAEH,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAGtD,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CAAC,EAAE;aACX,OAAO,CAAC,6EAA6E,CAAC;aACtF,GAAG,CAAC,IAAI,CAAC,UAAU,EAAE,aAAa,CAAC,EAAE,IAAI,CAAC,SAAS,EAAE,kBAAkB,CAAC,CAE9D,CAAC;IAChB,CAAC;CACF"}
|
|
@@ -0,0 +1,51 @@
|
|
|
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 HistorySearchResult } from "./types.js";
|
|
19
|
+
export declare class HistorySearchService {
|
|
20
|
+
private readonly db;
|
|
21
|
+
private readonly now;
|
|
22
|
+
private readonly grants;
|
|
23
|
+
constructor(options: {
|
|
24
|
+
db: DatabaseSync;
|
|
25
|
+
now: () => string;
|
|
26
|
+
});
|
|
27
|
+
/** 索引历史投影(由 worker/系统调用);必须仍是历史语义(is_current=false)。 */
|
|
28
|
+
indexHistoryDocument(input: {
|
|
29
|
+
merchant_id: string;
|
|
30
|
+
subject_id: string;
|
|
31
|
+
kind: string;
|
|
32
|
+
sku?: string;
|
|
33
|
+
occurred_at: string;
|
|
34
|
+
source_version: number;
|
|
35
|
+
text: string;
|
|
36
|
+
grant_id: string;
|
|
37
|
+
erase_scope_id: string;
|
|
38
|
+
is_current?: boolean;
|
|
39
|
+
}): string;
|
|
40
|
+
search(input: {
|
|
41
|
+
merchant_id: string;
|
|
42
|
+
actor: ActorContext;
|
|
43
|
+
subject_id: string;
|
|
44
|
+
resource_scope_id: string;
|
|
45
|
+
query: string;
|
|
46
|
+
cursor?: string;
|
|
47
|
+
limit?: number;
|
|
48
|
+
}): HistorySearchResult;
|
|
49
|
+
private createCursor;
|
|
50
|
+
private readCursor;
|
|
51
|
+
}
|
|
@@ -0,0 +1,252 @@
|
|
|
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
|
+
* 客户历史检索(设计 v1.1 §11 / M3-3)。
|
|
18
|
+
*
|
|
19
|
+
* 先做硬过滤(merchant/subject/grant/epoch/未封存/未过期),再用 FTS5
|
|
20
|
+
* trigram 做中文子串检索(不足 3 字回退 LIKE)。每次最多 5 片段、正文合计
|
|
21
|
+
* 不超过 2000 字;查询 ≤256 字;游标 5 分钟且绑定 grant/query。
|
|
22
|
+
* 检索不可用返回 `unavailable`,不能伪装成 `no_match`。
|
|
23
|
+
*/
|
|
24
|
+
import { randomUUID } from "node:crypto";
|
|
25
|
+
import { contentDigest } from "../../negotiation/jcs.js";
|
|
26
|
+
import { hasCapability } from "../context-governance/actor-context.js";
|
|
27
|
+
import { DataUseGrantStore } from "../context-governance/data-use-grant.js";
|
|
28
|
+
import { GOVERNANCE_POLICY_VERSION } from "../context-governance/types.js";
|
|
29
|
+
import { inImmediateTransaction } from "../storage/transaction.js";
|
|
30
|
+
import { CustomerMemoryError, } from "./types.js";
|
|
31
|
+
const MAX_QUERY_CHARS = 256;
|
|
32
|
+
const MAX_HITS = 5;
|
|
33
|
+
const MAX_HIT_TEXT_CHARS = 2_000;
|
|
34
|
+
const CURSOR_TTL_MS = 5 * 60 * 1000;
|
|
35
|
+
function text(value, field, max = 128) {
|
|
36
|
+
if (typeof value !== "string" || value.length === 0 || value.length > max) {
|
|
37
|
+
throw new CustomerMemoryError("validation", `${field}_invalid`, `${field} 必须是 1..${max} 字符`);
|
|
38
|
+
}
|
|
39
|
+
// eslint-disable-next-line no-control-regex
|
|
40
|
+
if (/[\u0000-\u001f\u007f]/.test(value)) {
|
|
41
|
+
throw new CustomerMemoryError("validation", `${field}_invalid`, `${field} 含控制字符`);
|
|
42
|
+
}
|
|
43
|
+
return value;
|
|
44
|
+
}
|
|
45
|
+
function nowUtc(now) {
|
|
46
|
+
const ms = Date.parse(now());
|
|
47
|
+
if (!Number.isFinite(ms)) {
|
|
48
|
+
throw new CustomerMemoryError("validation", "now_invalid", "时钟输出必须是可解析的 RFC3339 时间");
|
|
49
|
+
}
|
|
50
|
+
return new Date(ms).toISOString();
|
|
51
|
+
}
|
|
52
|
+
function requireActor(actor, merchantId) {
|
|
53
|
+
if (actor.merchant_id !== merchantId) {
|
|
54
|
+
throw new CustomerMemoryError("access_denied", "actor_merchant_mismatch", "ActorContext 与商家作用域不一致");
|
|
55
|
+
}
|
|
56
|
+
if (actor.policy_version !== GOVERNANCE_POLICY_VERSION) {
|
|
57
|
+
throw new CustomerMemoryError("validation", "policy_version_mismatch", "治理策略版本不一致");
|
|
58
|
+
}
|
|
59
|
+
if (!hasCapability(actor, "customer:self") && !hasCapability(actor, "memory:read")) {
|
|
60
|
+
throw new CustomerMemoryError("access_denied", "capability_missing", "缺少 customer:self/memory:read");
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
function ftsQuery(query) {
|
|
64
|
+
// trigram 查询用引号包裹,内部引号双写,避免 FTS 语法注入。
|
|
65
|
+
return `"${query.replaceAll('"', '""')}"`;
|
|
66
|
+
}
|
|
67
|
+
function escapeLike(query) {
|
|
68
|
+
return query.replaceAll("\\", "\\\\").replaceAll("%", "\\%").replaceAll("_", "\\_");
|
|
69
|
+
}
|
|
70
|
+
export class HistorySearchService {
|
|
71
|
+
db;
|
|
72
|
+
now;
|
|
73
|
+
grants;
|
|
74
|
+
constructor(options) {
|
|
75
|
+
this.db = options.db;
|
|
76
|
+
this.now = options.now;
|
|
77
|
+
this.grants = new DataUseGrantStore({ db: options.db, now: options.now });
|
|
78
|
+
}
|
|
79
|
+
/** 索引历史投影(由 worker/系统调用);必须仍是历史语义(is_current=false)。 */
|
|
80
|
+
indexHistoryDocument(input) {
|
|
81
|
+
const merchantId = text(input.merchant_id, "merchant_id");
|
|
82
|
+
const subjectId = text(input.subject_id, "subject_id");
|
|
83
|
+
if (input.is_current === true) {
|
|
84
|
+
throw new CustomerMemoryError("validation", "history_current_rejected", "历史索引不能标记为当前事实");
|
|
85
|
+
}
|
|
86
|
+
if (!Number.isSafeInteger(input.source_version) || input.source_version < 1) {
|
|
87
|
+
throw new CustomerMemoryError("validation", "source_version_invalid", "source_version 必须是正整数");
|
|
88
|
+
}
|
|
89
|
+
const body = text(input.text, "text", 20_000);
|
|
90
|
+
const grant = this.grants.getActiveGrant({
|
|
91
|
+
merchant_id: merchantId,
|
|
92
|
+
subject_id: subjectId,
|
|
93
|
+
purpose: "history_recall",
|
|
94
|
+
resource_scope_id: input.erase_scope_id,
|
|
95
|
+
});
|
|
96
|
+
if (grant === undefined) {
|
|
97
|
+
throw new CustomerMemoryError("grant_unavailable", "grant_not_active", "缺少历史检索授权");
|
|
98
|
+
}
|
|
99
|
+
const stamp = nowUtc(this.now);
|
|
100
|
+
const documentId = `hist_${randomUUID()}`;
|
|
101
|
+
return inImmediateTransaction(this.db, () => {
|
|
102
|
+
this.db
|
|
103
|
+
.prepare(`INSERT INTO history_index
|
|
104
|
+
(document_id, merchant_id, subject_id, kind, sku, occurred_at, source_version,
|
|
105
|
+
is_current, text, grant_id, grant_epoch, erase_scope_id, purge_at, created_at)
|
|
106
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, 0, ?, ?, ?, ?, ?, ?)`)
|
|
107
|
+
.run(documentId, merchantId, subjectId, text(input.kind, "kind", 64), input.sku === undefined ? null : text(input.sku, "sku", 128), text(input.occurred_at, "occurred_at", 64), input.source_version, body, grant.grant_id, grant.grant_epoch, text(input.erase_scope_id, "erase_scope_id"), new Date(Date.parse(stamp) + 90 * 24 * 60 * 60 * 1000).toISOString(), stamp);
|
|
108
|
+
this.db
|
|
109
|
+
.prepare("INSERT INTO history_fts (document_id, merchant_id, subject_id, text) VALUES (?, ?, ?, ?)")
|
|
110
|
+
.run(documentId, merchantId, subjectId, body);
|
|
111
|
+
return documentId;
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
search(input) {
|
|
115
|
+
const merchantId = text(input.merchant_id, "merchant_id");
|
|
116
|
+
requireActor(input.actor, merchantId);
|
|
117
|
+
const subjectId = text(input.subject_id, "subject_id");
|
|
118
|
+
const scopeId = text(input.resource_scope_id, "resource_scope_id");
|
|
119
|
+
const query = text(input.query, "query", MAX_QUERY_CHARS);
|
|
120
|
+
const limit = input.limit ?? MAX_HITS;
|
|
121
|
+
if (!Number.isSafeInteger(limit) || limit < 1 || limit > MAX_HITS) {
|
|
122
|
+
throw new CustomerMemoryError("validation", "limit_invalid", "limit 必须在 1..5");
|
|
123
|
+
}
|
|
124
|
+
const grant = this.grants.getActiveGrant({
|
|
125
|
+
merchant_id: merchantId,
|
|
126
|
+
subject_id: subjectId,
|
|
127
|
+
purpose: "history_recall",
|
|
128
|
+
resource_scope_id: scopeId,
|
|
129
|
+
});
|
|
130
|
+
if (grant === undefined) {
|
|
131
|
+
throw new CustomerMemoryError("grant_unavailable", "grant_not_active", "缺少历史检索授权");
|
|
132
|
+
}
|
|
133
|
+
// P0-1 配套:客户会话必须绑定到被检索主体;持 grant 本身不足以跨主体读取。
|
|
134
|
+
if (!(hasCapability(input.actor, "memory:write") && input.actor.subject_binding !== "none")) {
|
|
135
|
+
const bound = this.db
|
|
136
|
+
.prepare(`SELECT subject_id FROM subject_bindings
|
|
137
|
+
WHERE merchant_id = ? AND actor_id = ? AND status = 'ACTIVE' AND expires_at > ?
|
|
138
|
+
ORDER BY binding_epoch DESC LIMIT 1`)
|
|
139
|
+
.get(merchantId, input.actor.actor_id, nowUtc(this.now));
|
|
140
|
+
if (bound === undefined || bound.subject_id !== subjectId) {
|
|
141
|
+
throw new CustomerMemoryError("access_denied", "subject_not_bound", "当前会话未绑定到该历史主体");
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
const stamp = nowUtc(this.now);
|
|
145
|
+
const queryDigest = contentDigest({ query, subject: subjectId, scope: scopeId });
|
|
146
|
+
let offset = 0;
|
|
147
|
+
if (input.cursor !== undefined) {
|
|
148
|
+
const cursor = this.readCursor(text(input.cursor, "cursor"));
|
|
149
|
+
if (cursor === undefined ||
|
|
150
|
+
cursor.merchant_id !== merchantId ||
|
|
151
|
+
cursor.subject_id !== subjectId ||
|
|
152
|
+
cursor.grant_id !== grant.grant_id ||
|
|
153
|
+
cursor.grant_epoch !== grant.grant_epoch ||
|
|
154
|
+
cursor.query_digest !== queryDigest ||
|
|
155
|
+
cursor.expires_at <= stamp) {
|
|
156
|
+
throw new CustomerMemoryError("validation", "cursor_stale", "游标已失效;请重新查询");
|
|
157
|
+
}
|
|
158
|
+
offset = cursor.offset;
|
|
159
|
+
}
|
|
160
|
+
try {
|
|
161
|
+
const useFts = [...query].length >= 3;
|
|
162
|
+
const rows = (useFts
|
|
163
|
+
? this.db
|
|
164
|
+
.prepare(`SELECT h.document_id, h.source_version, h.kind, h.sku, h.occurred_at, h.text
|
|
165
|
+
FROM history_index h
|
|
166
|
+
JOIN history_fts ON history_fts.document_id = h.document_id
|
|
167
|
+
WHERE h.merchant_id = ? AND h.subject_id = ?
|
|
168
|
+
AND h.grant_id = ? AND h.grant_epoch = ?
|
|
169
|
+
AND h.purge_at > ?
|
|
170
|
+
AND NOT EXISTS (
|
|
171
|
+
SELECT 1 FROM data_scopes ds
|
|
172
|
+
WHERE ds.merchant_id = h.merchant_id
|
|
173
|
+
AND ds.erase_scope_id = h.erase_scope_id AND ds.state = 'SEALED')
|
|
174
|
+
AND history_fts MATCH ?
|
|
175
|
+
ORDER BY h.occurred_at DESC, h.document_id ASC
|
|
176
|
+
LIMIT ? OFFSET ?`)
|
|
177
|
+
.all(merchantId, subjectId, grant.grant_id, grant.grant_epoch, stamp, ftsQuery(query), limit + 1, offset)
|
|
178
|
+
: this.db
|
|
179
|
+
.prepare(`SELECT h.document_id, h.source_version, h.kind, h.sku, h.occurred_at, h.text
|
|
180
|
+
FROM history_index h
|
|
181
|
+
WHERE h.merchant_id = ? AND h.subject_id = ?
|
|
182
|
+
AND h.grant_id = ? AND h.grant_epoch = ?
|
|
183
|
+
AND h.purge_at > ?
|
|
184
|
+
AND NOT EXISTS (
|
|
185
|
+
SELECT 1 FROM data_scopes ds
|
|
186
|
+
WHERE ds.merchant_id = h.merchant_id
|
|
187
|
+
AND ds.erase_scope_id = h.erase_scope_id AND ds.state = 'SEALED')
|
|
188
|
+
AND h.text LIKE ? ESCAPE '\\'
|
|
189
|
+
ORDER BY h.occurred_at DESC, h.document_id ASC
|
|
190
|
+
LIMIT ? OFFSET ?`)
|
|
191
|
+
.all(merchantId, subjectId, grant.grant_id, grant.grant_epoch, stamp, `%${escapeLike(query)}%`, limit + 1, offset));
|
|
192
|
+
const hasMore = rows.length > limit;
|
|
193
|
+
const page = rows.slice(0, limit);
|
|
194
|
+
let budget = MAX_HIT_TEXT_CHARS;
|
|
195
|
+
const hits = [];
|
|
196
|
+
for (const row of page) {
|
|
197
|
+
const snippet = row.text.slice(0, 400);
|
|
198
|
+
if (snippet.length > budget)
|
|
199
|
+
break;
|
|
200
|
+
budget -= snippet.length;
|
|
201
|
+
hits.push({
|
|
202
|
+
document_ref: row.document_id,
|
|
203
|
+
source_version: row.source_version,
|
|
204
|
+
historical: true,
|
|
205
|
+
is_current: false,
|
|
206
|
+
kind: row.kind,
|
|
207
|
+
...(row.sku !== null ? { sku: row.sku } : {}),
|
|
208
|
+
occurred_at: row.occurred_at,
|
|
209
|
+
snippet,
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
if (hits.length === 0)
|
|
213
|
+
return { status: "no_match", hits: [] };
|
|
214
|
+
let nextCursor;
|
|
215
|
+
if (hasMore) {
|
|
216
|
+
nextCursor = this.createCursor({
|
|
217
|
+
merchantId,
|
|
218
|
+
subjectId,
|
|
219
|
+
grantId: grant.grant_id,
|
|
220
|
+
grantEpoch: grant.grant_epoch,
|
|
221
|
+
queryDigest,
|
|
222
|
+
offset: offset + hits.length,
|
|
223
|
+
stamp,
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
return { status: "ok", hits, ...(nextCursor !== undefined ? { next_cursor: nextCursor } : {}) };
|
|
227
|
+
}
|
|
228
|
+
catch (err) {
|
|
229
|
+
// 不向调用方泄露 SQL/路径;错误类别保留在 reason 供诊断/告警。
|
|
230
|
+
return {
|
|
231
|
+
status: "unavailable",
|
|
232
|
+
reason: err instanceof Error ? err.message.slice(0, 200) : "history_store_error",
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
createCursor(input) {
|
|
237
|
+
const cursorId = `hcur_${randomUUID()}`;
|
|
238
|
+
this.db
|
|
239
|
+
.prepare(`INSERT INTO history_cursors
|
|
240
|
+
(cursor_id, merchant_id, subject_id, grant_id, grant_epoch, query_digest,
|
|
241
|
+
offset, expires_at, created_at)
|
|
242
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`)
|
|
243
|
+
.run(cursorId, input.merchantId, input.subjectId, input.grantId, input.grantEpoch, input.queryDigest, input.offset, new Date(Date.parse(input.stamp) + CURSOR_TTL_MS).toISOString(), input.stamp);
|
|
244
|
+
return cursorId;
|
|
245
|
+
}
|
|
246
|
+
readCursor(cursorId) {
|
|
247
|
+
return this.db
|
|
248
|
+
.prepare("SELECT * FROM history_cursors WHERE cursor_id = ?")
|
|
249
|
+
.get(cursorId);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
//# sourceMappingURL=history.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"history.js","sourceRoot":"","sources":["../../../src/merchant-core/customer-memory/history.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH;;;;;;;GAOG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAGzC,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAEzD,OAAO,EAAE,aAAa,EAAE,MAAM,wCAAwC,CAAC;AACvE,OAAO,EAAE,iBAAiB,EAAE,MAAM,yCAAyC,CAAC;AAC5E,OAAO,EAAE,yBAAyB,EAAE,MAAM,gCAAgC,CAAC;AAC3E,OAAO,EAAE,sBAAsB,EAAE,MAAM,2BAA2B,CAAC;AACnE,OAAO,EACL,mBAAmB,GAGpB,MAAM,YAAY,CAAC;AAEpB,MAAM,eAAe,GAAG,GAAG,CAAC;AAC5B,MAAM,QAAQ,GAAG,CAAC,CAAC;AACnB,MAAM,kBAAkB,GAAG,KAAK,CAAC;AACjC,MAAM,aAAa,GAAG,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC;AAEpC,SAAS,IAAI,CAAC,KAAa,EAAE,KAAa,EAAE,GAAG,GAAG,GAAG;IACnD,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,IAAI,KAAK,CAAC,MAAM,GAAG,GAAG,EAAE,CAAC;QAC1E,MAAM,IAAI,mBAAmB,CAAC,YAAY,EAAE,GAAG,KAA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CAAC,KAQpB;QACC,MAAM,QAAQ,GAAG,QAAQ,UAAU,EAAE,EAAE,CAAC;QACxC,IAAI,CAAC,EAAE;aACJ,OAAO,CACN;;;4CAGoC,CACrC;aACA,GAAG,CACF,QAAQ,EACR,KAAK,CAAC,UAAU,EAChB,KAAK,CAAC,SAAS,EACf,KAAK,CAAC,OAAO,EACb,KAAK,CAAC,UAAU,EAChB,KAAK,CAAC,WAAW,EACjB,KAAK,CAAC,MAAM,EACZ,IAAI,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC,GAAG,aAAa,CAAC,CAAC,WAAW,EAAE,EAC/D,KAAK,CAAC,KAAK,CACZ,CAAC;QACJ,OAAO,QAAQ,CAAC;IAClB,CAAC;IAEO,UAAU,CAAC,QAAgB;QASjC,OAAO,IAAI,CAAC,EAAE;aACX,OAAO,CAAC,mDAAmD,CAAC;aAC5D,GAAG,CAAC,QAAQ,CAUF,CAAC;IAChB,CAAC;CACF"}
|
|
@@ -0,0 +1,66 @@
|
|
|
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 CustomerSubject, type CustomerSubjectKind, type SubjectBinding } from "./types.js";
|
|
19
|
+
export declare class CustomerIdentityService {
|
|
20
|
+
private readonly db;
|
|
21
|
+
private readonly now;
|
|
22
|
+
constructor(options: {
|
|
23
|
+
db: DatabaseSync;
|
|
24
|
+
now: () => string;
|
|
25
|
+
});
|
|
26
|
+
createSubject(input: {
|
|
27
|
+
merchant_id: string;
|
|
28
|
+
actor: ActorContext;
|
|
29
|
+
subject_kind: CustomerSubjectKind;
|
|
30
|
+
}): CustomerSubject;
|
|
31
|
+
getSubject(merchantId: string, subjectId: string): CustomerSubject | undefined;
|
|
32
|
+
linkChallenge(input: {
|
|
33
|
+
merchant_id: string;
|
|
34
|
+
actor: ActorContext;
|
|
35
|
+
source_session_id: string;
|
|
36
|
+
target_subject_id: string;
|
|
37
|
+
scope?: readonly string[];
|
|
38
|
+
ttl_seconds?: number;
|
|
39
|
+
}): {
|
|
40
|
+
challenge_id: string;
|
|
41
|
+
proof: string;
|
|
42
|
+
expires_at: string;
|
|
43
|
+
};
|
|
44
|
+
linkConfirm(input: {
|
|
45
|
+
merchant_id: string;
|
|
46
|
+
actor: ActorContext;
|
|
47
|
+
challenge_id: string;
|
|
48
|
+
source_session_id: string;
|
|
49
|
+
proof: string;
|
|
50
|
+
expires_at?: string;
|
|
51
|
+
}): SubjectBinding;
|
|
52
|
+
revokeBinding(input: {
|
|
53
|
+
merchant_id: string;
|
|
54
|
+
actor: ActorContext;
|
|
55
|
+
binding_id: string;
|
|
56
|
+
expected_epoch: number;
|
|
57
|
+
}): SubjectBinding;
|
|
58
|
+
/** 当前会话 actor 的有效绑定(最新 epoch);undefined = 未绑定/已撤销/过期。 */
|
|
59
|
+
currentSubjectForActor(merchantId: string, actorId: string): {
|
|
60
|
+
subject_id: string;
|
|
61
|
+
binding_id: string;
|
|
62
|
+
binding_epoch: number;
|
|
63
|
+
} | undefined;
|
|
64
|
+
currentBindingEpoch(merchantId: string, actorId: string): number | undefined;
|
|
65
|
+
listBindings(merchantId: string, actor: ActorContext, subjectId?: string): SubjectBinding[];
|
|
66
|
+
}
|