@harrylabsj/kiwi-merchant-cloud 0.12.31 → 0.12.33
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/app/a2a/inquiry-nonce-store.d.ts +12 -1
- package/app/a2a/inquiry-nonce-store.js +49 -17
- package/app/a2a/inquiry-nonce-store.js.map +1 -1
- package/app/a2a/negotiate.d.ts +30 -1
- package/app/a2a/negotiate.js +121 -32
- package/app/a2a/negotiate.js.map +1 -1
- package/app/a2a/node.d.ts +4 -0
- package/app/a2a/node.js +182 -4
- package/app/a2a/node.js.map +1 -1
- package/app/a2a/open-inquiry-session.d.ts +1 -0
- package/app/a2a/open-inquiry-session.js +3 -1
- package/app/a2a/open-inquiry-session.js.map +1 -1
- package/app/a2a/open-inquiry-verifier.js +2 -2
- package/app/a2a/open-inquiry-verifier.js.map +1 -1
- package/app/a2a/reception/agent-runtime.d.ts +32 -1
- package/app/a2a/reception/agent-runtime.js +420 -11
- package/app/a2a/reception/agent-runtime.js.map +1 -1
- package/app/a2a/reception/customer-memory-tools.d.ts +73 -0
- package/app/a2a/reception/customer-memory-tools.js +17 -0
- package/app/a2a/reception/customer-memory-tools.js.map +1 -0
- package/app/a2a/reception/engine.d.ts +12 -0
- package/app/a2a/reception/engine.js +42 -1
- package/app/a2a/reception/engine.js.map +1 -1
- package/app/a2a/reception/publish.d.ts +14 -1
- package/app/a2a/reception/publish.js +62 -1
- package/app/a2a/reception/publish.js.map +1 -1
- package/app/a2a/reception/question-spec.d.ts +91 -0
- package/app/a2a/reception/question-spec.js +260 -0
- package/app/a2a/reception/question-spec.js.map +1 -0
- package/app/a2a/reception/reply-outbox.d.ts +6 -0
- package/app/a2a/reception/reply-outbox.js +44 -0
- package/app/a2a/reception/reply-outbox.js.map +1 -1
- package/app/a2a/reception/responder.d.ts +13 -1
- package/app/a2a/reception/responder.js +26 -3
- package/app/a2a/reception/responder.js.map +1 -1
- package/app/a2a/reception/skill-catalog.d.ts +72 -0
- package/app/a2a/reception/skill-catalog.js +85 -0
- package/app/a2a/reception/skill-catalog.js.map +1 -0
- package/app/a2a/reception/skill-contract.d.ts +70 -0
- package/app/a2a/reception/skill-contract.js +183 -0
- package/app/a2a/reception/skill-contract.js.map +1 -0
- package/app/a2a/reception/types.d.ts +12 -0
- package/app/a2a/server/customer-identity-gateway.d.ts +111 -0
- package/app/a2a/server/customer-identity-gateway.js +441 -0
- package/app/a2a/server/customer-identity-gateway.js.map +1 -0
- package/app/a2a/server/errors.d.ts +4 -3
- package/app/a2a/server/errors.js +10 -2
- package/app/a2a/server/errors.js.map +1 -1
- package/app/a2a/server/merchant-handler.d.ts +75 -1
- package/app/a2a/server/merchant-handler.js +676 -86
- package/app/a2a/server/merchant-handler.js.map +1 -1
- package/app/a2a/server/merchant-procurement.d.ts +90 -0
- package/app/a2a/server/merchant-procurement.js +75 -0
- package/app/a2a/server/merchant-procurement.js.map +1 -0
- package/app/a2a/server/merchant-product-facts.d.ts +2 -0
- package/app/a2a/server/merchant-product-facts.js.map +1 -1
- package/app/a2a/server/negotiation-approvals.d.ts +11 -0
- package/app/a2a/server/negotiation-approvals.js +43 -0
- package/app/a2a/server/negotiation-approvals.js.map +1 -1
- package/app/a2a/server/pipeline.js +78 -30
- package/app/a2a/server/pipeline.js.map +1 -1
- package/app/a2a/server/server.js +4 -3
- package/app/a2a/server/server.js.map +1 -1
- package/app/a2a/server/types.d.ts +4 -1
- package/app/a2a/unit-alias.d.ts +3 -3
- package/app/a2a/unit-alias.js +9 -5
- package/app/a2a/unit-alias.js.map +1 -1
- package/app/agent/alipay-native-view.d.ts +27 -0
- package/app/agent/alipay-native-view.js +113 -0
- package/app/agent/alipay-native-view.js.map +1 -0
- package/app/agent/buyer/buyer-tools.d.ts +20 -0
- package/app/agent/buyer/buyer-tools.js +241 -48
- package/app/agent/buyer/buyer-tools.js.map +1 -1
- package/app/agent/buyer/types.d.ts +1 -1
- package/app/agent/buyer/types.js.map +1 -1
- package/app/agent/chat-tui.js +16 -1
- package/app/agent/chat-tui.js.map +1 -1
- package/app/agent/kernel-builder.d.ts +2 -0
- package/app/agent/kernel-builder.js +14 -0
- package/app/agent/kernel-builder.js.map +1 -1
- package/app/agent/kernel.d.ts +8 -0
- package/app/agent/kernel.js +67 -1
- package/app/agent/kernel.js.map +1 -1
- package/app/auth/merchant-sessions.d.ts +3 -0
- package/app/auth/merchant-sessions.js +21 -5
- package/app/auth/merchant-sessions.js.map +1 -1
- package/app/buyer-core/a2a-accept-terms.d.ts +11 -0
- package/app/buyer-core/a2a-accept-terms.js +28 -0
- package/app/buyer-core/a2a-accept-terms.js.map +1 -0
- package/app/buyer-core/a2a-knp.d.ts +7 -2
- package/app/buyer-core/a2a-knp.js +8 -5
- package/app/buyer-core/a2a-knp.js.map +1 -1
- package/app/buyer-core/a2a-negotiator.d.ts +17 -0
- package/app/buyer-core/a2a-negotiator.js +70 -26
- package/app/buyer-core/a2a-negotiator.js.map +1 -1
- package/app/buyer-core/a2a-quote-fetcher.d.ts +25 -15
- package/app/buyer-core/a2a-quote-fetcher.js +92 -19
- package/app/buyer-core/a2a-quote-fetcher.js.map +1 -1
- package/app/buyer-core/a2a-quote-history.d.ts +94 -0
- package/app/buyer-core/a2a-quote-history.js +149 -0
- package/app/buyer-core/a2a-quote-history.js.map +1 -0
- package/app/buyer-core/build-service.d.ts +19 -0
- package/app/buyer-core/build-service.js +30 -1
- package/app/buyer-core/build-service.js.map +1 -1
- package/app/buyer-core/errors.d.ts +1 -1
- package/app/buyer-core/errors.js +1 -0
- package/app/buyer-core/errors.js.map +1 -1
- package/app/buyer-core/merchant-index.d.ts +2 -2
- package/app/buyer-core/merchant-index.js +23 -5
- package/app/buyer-core/merchant-index.js.map +1 -1
- package/app/buyer-core/open-inquiry-signer.d.ts +1 -0
- package/app/buyer-core/open-inquiry-signer.js +5 -2
- package/app/buyer-core/open-inquiry-signer.js.map +1 -1
- package/app/buyer-core/procurement/cases.d.ts +441 -0
- package/app/buyer-core/procurement/cases.js +457 -0
- package/app/buyer-core/procurement/cases.js.map +1 -0
- package/app/buyer-core/procurement/quote-comparison.d.ts +137 -0
- package/app/buyer-core/procurement/quote-comparison.js +106 -0
- package/app/buyer-core/procurement/quote-comparison.js.map +1 -0
- package/app/buyer-core/procurement/quote-normalizer.d.ts +45 -0
- package/app/buyer-core/procurement/quote-normalizer.js +153 -0
- package/app/buyer-core/procurement/quote-normalizer.js.map +1 -0
- package/app/buyer-core/procurement/requirements.d.ts +50 -0
- package/app/buyer-core/procurement/requirements.js +43 -0
- package/app/buyer-core/procurement/requirements.js.map +1 -0
- package/app/buyer-core/procurement/source-observation.d.ts +57 -0
- package/app/buyer-core/procurement/source-observation.js +54 -0
- package/app/buyer-core/procurement/source-observation.js.map +1 -0
- package/app/buyer-core/procurement/store.d.ts +21 -0
- package/app/buyer-core/procurement/store.js +68 -0
- package/app/buyer-core/procurement/store.js.map +1 -0
- package/app/buyer-core/protocol-recovery.d.ts +10 -1
- package/app/buyer-core/protocol-recovery.js +31 -6
- package/app/buyer-core/protocol-recovery.js.map +1 -1
- package/app/buyer-core/quote-quantity.d.ts +5 -0
- package/app/buyer-core/quote-quantity.js +15 -0
- package/app/buyer-core/quote-quantity.js.map +1 -0
- package/app/buyer-core/service.d.ts +128 -8
- package/app/buyer-core/service.js +543 -75
- package/app/buyer-core/service.js.map +1 -1
- package/app/buyer-core/store.d.ts +21 -0
- package/app/buyer-core/store.js +62 -10
- package/app/buyer-core/store.js.map +1 -1
- package/app/cli.js +26 -19
- package/app/cli.js.map +1 -1
- package/app/cloud/alipay-collection-http.d.ts +44 -0
- package/app/cloud/alipay-collection-http.js +111 -0
- package/app/cloud/alipay-collection-http.js.map +1 -0
- package/app/cloud/bootstrap.js +218 -19
- package/app/cloud/bootstrap.js.map +1 -1
- package/app/cloud/http-router.d.ts +2 -0
- package/app/cloud/http-router.js +5 -0
- package/app/cloud/http-router.js.map +1 -1
- package/app/cloud-sample/products.json +3 -3
- package/app/commerce/receipt/dispatcher.d.ts +43 -0
- package/app/commerce/receipt/dispatcher.js +86 -0
- package/app/commerce/receipt/dispatcher.js.map +1 -0
- package/app/commerce/receipt/receipts.d.ts +147 -0
- package/app/commerce/receipt/receipts.js +285 -0
- package/app/commerce/receipt/receipts.js.map +1 -0
- package/app/config/profile.d.ts +2 -2
- package/app/discovery/catalog-source/buyer-follows.d.ts +3 -3
- package/app/discovery/catalog-source/buyer-follows.js +6 -3
- package/app/discovery/catalog-source/buyer-follows.js.map +1 -1
- package/app/discovery/catalog-source/kiwi-record.d.ts +2 -0
- package/app/discovery/catalog-source/kiwi-record.js.map +1 -1
- package/app/discovery/catalog-source/kiwi-source.js +4 -0
- package/app/discovery/catalog-source/kiwi-source.js.map +1 -1
- package/app/handoff/alipay-collection.d.ts +23 -0
- package/app/handoff/alipay-collection.js +70 -0
- package/app/handoff/alipay-collection.js.map +1 -0
- package/app/handoff/order-receipts.d.ts +62 -0
- package/app/handoff/order-receipts.js +72 -0
- package/app/handoff/order-receipts.js.map +1 -0
- package/app/handoff/ucp-checkout/cart-channel.d.ts +2 -1
- package/app/handoff/ucp-checkout/cart-channel.js +1 -0
- package/app/handoff/ucp-checkout/cart-channel.js.map +1 -1
- package/app/handoff/ucp-checkout/channel.d.ts +7 -0
- package/app/handoff/ucp-checkout/channel.js +79 -10
- package/app/handoff/ucp-checkout/channel.js.map +1 -1
- package/app/http/cli.js +6 -0
- package/app/http/cli.js.map +1 -1
- package/app/http/merchant-management/api.d.ts +18 -0
- package/app/http/merchant-management/api.js +752 -3
- package/app/http/merchant-management/api.js.map +1 -1
- package/app/http/merchant-management/draft-store.js +8 -6
- package/app/http/merchant-management/draft-store.js.map +1 -1
- package/app/http/merchant-management/page.d.ts +1 -0
- package/app/http/merchant-management/page.js +335 -5
- package/app/http/merchant-management/page.js.map +1 -1
- package/app/http/server.js +9 -2
- package/app/http/server.js.map +1 -1
- package/app/mcp/cli.d.ts +3 -1
- package/app/mcp/cli.js +18 -2
- package/app/mcp/cli.js.map +1 -1
- package/app/mcp/merchant-runtime-assembly.d.ts +20 -0
- package/app/mcp/merchant-runtime-assembly.js +94 -11
- package/app/mcp/merchant-runtime-assembly.js.map +1 -1
- package/app/mcp/merchant-server.d.ts +5 -0
- package/app/mcp/merchant-server.js +12 -0
- package/app/mcp/merchant-server.js.map +1 -1
- package/app/mcp/procurement-tools.d.ts +10 -0
- package/app/mcp/procurement-tools.js +173 -0
- package/app/mcp/procurement-tools.js.map +1 -0
- package/app/mcp/server.d.ts +4 -0
- package/app/mcp/server.js +63 -9
- package/app/mcp/server.js.map +1 -1
- package/app/mcp/tools.d.ts +4 -1
- package/app/mcp/tools.js +83 -28
- package/app/mcp/tools.js.map +1 -1
- package/app/mcp/types.d.ts +1 -0
- package/app/mcp/types.js.map +1 -1
- package/app/merchant/application/actor.d.ts +3 -1
- package/app/merchant/application/actor.js +21 -0
- package/app/merchant/application/actor.js.map +1 -1
- package/app/merchant/negotiation-ops.d.ts +2 -0
- package/app/merchant/negotiation-ops.js +4 -0
- package/app/merchant/negotiation-ops.js.map +1 -1
- package/app/merchant/weixin-channel.d.ts +68 -0
- package/app/merchant/weixin-channel.js +313 -0
- package/app/merchant/weixin-channel.js.map +1 -0
- package/app/merchant/workbench-server.d.ts +1 -0
- package/app/merchant/workbench-server.js +189 -2
- package/app/merchant/workbench-server.js.map +1 -1
- package/app/merchant-core/budget/background-budget.d.ts +132 -0
- package/app/merchant-core/budget/background-budget.js +308 -0
- package/app/merchant-core/budget/background-budget.js.map +1 -0
- package/app/merchant-core/collection/alipay-store.d.ts +22 -0
- package/app/merchant-core/collection/alipay-store.js +125 -0
- package/app/merchant-core/collection/alipay-store.js.map +1 -0
- package/app/merchant-core/commercial/calculate.js +6 -6
- package/app/merchant-core/commercial/calculate.js.map +1 -1
- package/app/merchant-core/context-governance/actor-context.d.ts +56 -0
- package/app/merchant-core/context-governance/actor-context.js +76 -0
- package/app/merchant-core/context-governance/actor-context.js.map +1 -0
- package/app/merchant-core/context-governance/authority.d.ts +39 -0
- package/app/merchant-core/context-governance/authority.js +28 -0
- package/app/merchant-core/context-governance/authority.js.map +1 -0
- package/app/merchant-core/context-governance/context-guard.d.ts +104 -0
- package/app/merchant-core/context-governance/context-guard.js +194 -0
- package/app/merchant-core/context-governance/context-guard.js.map +1 -0
- package/app/merchant-core/context-governance/data-use-grant.d.ts +91 -0
- package/app/merchant-core/context-governance/data-use-grant.js +211 -0
- package/app/merchant-core/context-governance/data-use-grant.js.map +1 -0
- package/app/merchant-core/context-governance/feature-flags.d.ts +32 -0
- package/app/merchant-core/context-governance/feature-flags.js +83 -0
- package/app/merchant-core/context-governance/feature-flags.js.map +1 -0
- package/app/merchant-core/context-governance/pilot-readiness.d.ts +39 -0
- package/app/merchant-core/context-governance/pilot-readiness.js +141 -0
- package/app/merchant-core/context-governance/pilot-readiness.js.map +1 -0
- package/app/merchant-core/context-governance/source-governance.d.ts +157 -0
- package/app/merchant-core/context-governance/source-governance.js +492 -0
- package/app/merchant-core/context-governance/source-governance.js.map +1 -0
- package/app/merchant-core/context-governance/types.d.ts +62 -0
- package/app/merchant-core/context-governance/types.js +92 -0
- package/app/merchant-core/context-governance/types.js.map +1 -0
- package/app/merchant-core/customer-delivery/binding.d.ts +55 -0
- package/app/merchant-core/customer-delivery/binding.js +121 -0
- package/app/merchant-core/customer-delivery/binding.js.map +1 -0
- package/app/merchant-core/customer-delivery/channel.d.ts +75 -0
- package/app/merchant-core/customer-delivery/channel.js +157 -0
- package/app/merchant-core/customer-delivery/channel.js.map +1 -0
- package/app/merchant-core/customer-delivery/consent-repository.d.ts +50 -0
- package/app/merchant-core/customer-delivery/consent-repository.js +160 -0
- package/app/merchant-core/customer-delivery/consent-repository.js.map +1 -0
- package/app/merchant-core/customer-delivery/dispatcher-loop.d.ts +68 -0
- package/app/merchant-core/customer-delivery/dispatcher-loop.js +132 -0
- package/app/merchant-core/customer-delivery/dispatcher-loop.js.map +1 -0
- package/app/merchant-core/customer-delivery/dispatcher.d.ts +118 -0
- package/app/merchant-core/customer-delivery/dispatcher.js +166 -0
- package/app/merchant-core/customer-delivery/dispatcher.js.map +1 -0
- package/app/merchant-core/customer-delivery/email-adapter.d.ts +31 -0
- package/app/merchant-core/customer-delivery/email-adapter.js +90 -0
- package/app/merchant-core/customer-delivery/email-adapter.js.map +1 -0
- package/app/merchant-core/customer-delivery/gate.d.ts +37 -0
- package/app/merchant-core/customer-delivery/gate.js +196 -0
- package/app/merchant-core/customer-delivery/gate.js.map +1 -0
- package/app/merchant-core/customer-delivery/reconciliation.d.ts +13 -0
- package/app/merchant-core/customer-delivery/reconciliation.js +70 -0
- package/app/merchant-core/customer-delivery/reconciliation.js.map +1 -0
- package/app/merchant-core/customer-delivery/repository.d.ts +55 -0
- package/app/merchant-core/customer-delivery/repository.js +235 -0
- package/app/merchant-core/customer-delivery/repository.js.map +1 -0
- package/app/merchant-core/customer-delivery/smtp-adapter.d.ts +30 -0
- package/app/merchant-core/customer-delivery/smtp-adapter.js +183 -0
- package/app/merchant-core/customer-delivery/smtp-adapter.js.map +1 -0
- package/app/merchant-core/customer-delivery/stream-gate.d.ts +45 -0
- package/app/merchant-core/customer-delivery/stream-gate.js +123 -0
- package/app/merchant-core/customer-delivery/stream-gate.js.map +1 -0
- package/app/merchant-core/customer-delivery/types.d.ts +103 -0
- package/app/merchant-core/customer-delivery/types.js +32 -0
- package/app/merchant-core/customer-delivery/types.js.map +1 -0
- package/app/merchant-core/customer-delivery/weixin-adapter.d.ts +35 -0
- package/app/merchant-core/customer-delivery/weixin-adapter.js +56 -0
- package/app/merchant-core/customer-delivery/weixin-adapter.js.map +1 -0
- package/app/merchant-core/customer-memory/auto-identity.d.ts +43 -0
- package/app/merchant-core/customer-memory/auto-identity.js +163 -0
- package/app/merchant-core/customer-memory/auto-identity.js.map +1 -0
- package/app/merchant-core/customer-memory/grants.d.ts +72 -0
- package/app/merchant-core/customer-memory/grants.js +224 -0
- package/app/merchant-core/customer-memory/grants.js.map +1 -0
- package/app/merchant-core/customer-memory/history.d.ts +51 -0
- package/app/merchant-core/customer-memory/history.js +252 -0
- package/app/merchant-core/customer-memory/history.js.map +1 -0
- package/app/merchant-core/customer-memory/identity.d.ts +66 -0
- package/app/merchant-core/customer-memory/identity.js +249 -0
- package/app/merchant-core/customer-memory/identity.js.map +1 -0
- package/app/merchant-core/customer-memory/maintenance.d.ts +51 -0
- package/app/merchant-core/customer-memory/maintenance.js +67 -0
- package/app/merchant-core/customer-memory/maintenance.js.map +1 -0
- package/app/merchant-core/customer-memory/memory.d.ts +119 -0
- package/app/merchant-core/customer-memory/memory.js +502 -0
- package/app/merchant-core/customer-memory/memory.js.map +1 -0
- package/app/merchant-core/customer-memory/private-transport.d.ts +10 -0
- package/app/merchant-core/customer-memory/private-transport.js +108 -0
- package/app/merchant-core/customer-memory/private-transport.js.map +1 -0
- package/app/merchant-core/customer-memory/quote-history.d.ts +160 -0
- package/app/merchant-core/customer-memory/quote-history.js +392 -0
- package/app/merchant-core/customer-memory/quote-history.js.map +1 -0
- package/app/merchant-core/customer-memory/reception-provider.d.ts +69 -0
- package/app/merchant-core/customer-memory/reception-provider.js +250 -0
- package/app/merchant-core/customer-memory/reception-provider.js.map +1 -0
- package/app/merchant-core/customer-memory/reception-writer.d.ts +19 -0
- package/app/merchant-core/customer-memory/reception-writer.js +32 -0
- package/app/merchant-core/customer-memory/reception-writer.js.map +1 -0
- package/app/merchant-core/customer-memory/types.d.ts +111 -0
- package/app/merchant-core/customer-memory/types.js +58 -0
- package/app/merchant-core/customer-memory/types.js.map +1 -0
- package/app/merchant-core/followup/repository.d.ts +2 -1
- package/app/merchant-core/followup/repository.js +29 -23
- package/app/merchant-core/followup/repository.js.map +1 -1
- package/app/merchant-core/learning/evaluation-fixtures.d.ts +38 -0
- package/app/merchant-core/learning/evaluation-fixtures.js +83 -0
- package/app/merchant-core/learning/evaluation-fixtures.js.map +1 -0
- package/app/merchant-core/learning/evaluation-suite-v1.d.ts +56 -0
- package/app/merchant-core/learning/evaluation-suite-v1.js +129 -0
- package/app/merchant-core/learning/evaluation-suite-v1.js.map +1 -0
- package/app/merchant-core/learning/evaluation.d.ts +84 -0
- package/app/merchant-core/learning/evaluation.js +581 -0
- package/app/merchant-core/learning/evaluation.js.map +1 -0
- package/app/merchant-core/learning/initial-skills.d.ts +36 -0
- package/app/merchant-core/learning/initial-skills.js +67 -0
- package/app/merchant-core/learning/initial-skills.js.map +1 -0
- package/app/merchant-core/learning/precheck.d.ts +33 -0
- package/app/merchant-core/learning/precheck.js +56 -0
- package/app/merchant-core/learning/precheck.js.map +1 -0
- package/app/merchant-core/learning/real-evaluation-runner.d.ts +52 -0
- package/app/merchant-core/learning/real-evaluation-runner.js +167 -0
- package/app/merchant-core/learning/real-evaluation-runner.js.map +1 -0
- package/app/merchant-core/learning/reception-skill-catalog.d.ts +39 -0
- package/app/merchant-core/learning/reception-skill-catalog.js +145 -0
- package/app/merchant-core/learning/reception-skill-catalog.js.map +1 -0
- package/app/merchant-core/learning/recovery-gateway.d.ts +27 -0
- package/app/merchant-core/learning/recovery-gateway.js +106 -0
- package/app/merchant-core/learning/recovery-gateway.js.map +1 -0
- package/app/merchant-core/learning/repository.d.ts +130 -0
- package/app/merchant-core/learning/repository.js +588 -0
- package/app/merchant-core/learning/repository.js.map +1 -0
- package/app/merchant-core/learning/service.d.ts +262 -0
- package/app/merchant-core/learning/service.js +1498 -0
- package/app/merchant-core/learning/service.js.map +1 -0
- package/app/merchant-core/learning/test-dispatch-gateway.d.ts +43 -0
- package/app/merchant-core/learning/test-dispatch-gateway.js +145 -0
- package/app/merchant-core/learning/test-dispatch-gateway.js.map +1 -0
- package/app/merchant-core/learning/types.d.ts +217 -0
- package/app/merchant-core/learning/types.js +52 -0
- package/app/merchant-core/learning/types.js.map +1 -0
- package/app/merchant-core/migrations/ledgers.d.ts +40 -0
- package/app/merchant-core/migrations/ledgers.js +824 -0
- package/app/merchant-core/migrations/ledgers.js.map +1 -0
- package/app/merchant-core/migrations/runner.d.ts +55 -0
- package/app/merchant-core/migrations/runner.js +114 -0
- package/app/merchant-core/migrations/runner.js.map +1 -0
- package/app/merchant-core/operating/repository.d.ts +0 -1
- package/app/merchant-core/operating/repository.js +2 -17
- package/app/merchant-core/operating/repository.js.map +1 -1
- package/app/merchant-core/operations-jobs/advice-runner.d.ts +63 -0
- package/app/merchant-core/operations-jobs/advice-runner.js +199 -0
- package/app/merchant-core/operations-jobs/advice-runner.js.map +1 -0
- package/app/merchant-core/operations-jobs/pilot-evidence.d.ts +64 -0
- package/app/merchant-core/operations-jobs/pilot-evidence.js +109 -0
- package/app/merchant-core/operations-jobs/pilot-evidence.js.map +1 -0
- package/app/merchant-core/operations-jobs/pilot-metrics.d.ts +70 -0
- package/app/merchant-core/operations-jobs/pilot-metrics.js +98 -0
- package/app/merchant-core/operations-jobs/pilot-metrics.js.map +1 -0
- package/app/merchant-core/operations-jobs/projection.d.ts +41 -0
- package/app/merchant-core/operations-jobs/projection.js +158 -0
- package/app/merchant-core/operations-jobs/projection.js.map +1 -0
- package/app/merchant-core/operations-jobs/scheduler.d.ts +68 -0
- package/app/merchant-core/operations-jobs/scheduler.js +233 -0
- package/app/merchant-core/operations-jobs/scheduler.js.map +1 -0
- package/app/merchant-core/operations-jobs/service.d.ts +260 -0
- package/app/merchant-core/operations-jobs/service.js +922 -0
- package/app/merchant-core/operations-jobs/service.js.map +1 -0
- package/app/merchant-core/opportunity/repository.d.ts +0 -1
- package/app/merchant-core/opportunity/repository.js +2 -17
- package/app/merchant-core/opportunity/repository.js.map +1 -1
- package/app/merchant-core/ops/facade.d.ts +10 -0
- package/app/merchant-core/ops/facade.js +52 -0
- package/app/merchant-core/ops/facade.js.map +1 -1
- package/app/merchant-core/ops/schema.d.ts +1 -1
- package/app/merchant-core/ops/schema.js +119 -2
- package/app/merchant-core/ops/schema.js.map +1 -1
- package/app/merchant-core/ops/surface.d.ts +37 -0
- package/app/merchant-core/ops/surface.js +240 -0
- package/app/merchant-core/ops/surface.js.map +1 -1
- package/app/merchant-core/orders/model.d.ts +1 -8
- package/app/merchant-core/orders/model.js +10 -2
- package/app/merchant-core/orders/model.js.map +1 -1
- package/app/merchant-core/orders/repository.d.ts +0 -1
- package/app/merchant-core/orders/repository.js +3 -18
- package/app/merchant-core/orders/repository.js.map +1 -1
- package/app/merchant-core/orders/service.js +34 -8
- package/app/merchant-core/orders/service.js.map +1 -1
- package/app/merchant-core/ownership/instance-ownership.d.ts +122 -0
- package/app/merchant-core/ownership/instance-ownership.js +236 -0
- package/app/merchant-core/ownership/instance-ownership.js.map +1 -0
- package/app/merchant-core/ownership/runtime.d.ts +175 -0
- package/app/merchant-core/ownership/runtime.js +422 -0
- package/app/merchant-core/ownership/runtime.js.map +1 -0
- package/app/merchant-core/proposal/engine.js +2 -0
- package/app/merchant-core/proposal/engine.js.map +1 -1
- package/app/merchant-core/proposal/publish.d.ts +0 -6
- package/app/merchant-core/proposal/publish.js +37 -3
- package/app/merchant-core/proposal/publish.js.map +1 -1
- package/app/merchant-core/proposal/repository.d.ts +0 -7
- package/app/merchant-core/proposal/repository.js +8 -17
- package/app/merchant-core/proposal/repository.js.map +1 -1
- package/app/merchant-core/proposal/types.d.ts +1 -0
- package/app/merchant-core/proposal/types.js.map +1 -1
- package/app/merchant-core/rfq/policy.d.ts +1 -0
- package/app/merchant-core/rfq/policy.js +6 -7
- package/app/merchant-core/rfq/policy.js.map +1 -1
- package/app/merchant-core/rfq/repository.d.ts +0 -1
- package/app/merchant-core/rfq/repository.js +2 -17
- package/app/merchant-core/rfq/repository.js.map +1 -1
- package/app/merchant-core/rfq/service.d.ts +2 -1
- package/app/merchant-core/rfq/service.js +2 -4
- package/app/merchant-core/rfq/service.js.map +1 -1
- package/app/merchant-core/storage/transaction.d.ts +95 -7
- package/app/merchant-core/storage/transaction.js +251 -2
- package/app/merchant-core/storage/transaction.js.map +1 -1
- package/app/merchant-credential-rotation.d.ts +4 -1
- package/app/merchant-credential-rotation.js +12 -8
- package/app/merchant-credential-rotation.js.map +1 -1
- package/app/merchant-runtime/manager.d.ts +2 -0
- package/app/merchant-runtime/manager.js +5 -0
- package/app/merchant-runtime/manager.js.map +1 -1
- package/app/negotiation/domain/objects.d.ts +2 -0
- package/app/negotiation/domain/objects.js +3 -0
- package/app/negotiation/domain/objects.js.map +1 -1
- package/app/negotiation/ledger/store.d.ts +4 -0
- package/app/negotiation/ledger/store.js +20 -0
- package/app/negotiation/ledger/store.js.map +1 -1
- package/app/product-init.d.ts +1 -0
- package/app/product-init.js +4 -1
- package/app/product-init.js.map +1 -1
- package/app/trust/identity/message-signature.d.ts +1 -1
- package/app/trust/identity/message-signature.js +6 -1
- package/app/trust/identity/message-signature.js.map +1 -1
- package/app/trust/identity/nonce.d.ts +5 -0
- package/app/trust/identity/nonce.js +2 -0
- package/app/trust/identity/nonce.js.map +1 -1
- package/app/weixin/ilink-client.d.ts +3 -2
- package/app/weixin/ilink-client.js +9 -2
- package/app/weixin/ilink-client.js.map +1 -1
- package/build-manifest.json +1672 -487
- package/deployment-lock.json +2 -2
- package/package.json +1 -1
- package/prepare.mjs +5 -1
- package/runtime-package.json +1 -1
- package/shipping-sbom.cdx.json +6 -6
- package/shipping-source.json +12 -12
|
@@ -0,0 +1,194 @@
|
|
|
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
|
+
* ContextGuard(TOP1-3 设计 v1.1 §4.3 / §17.3)。
|
|
18
|
+
*
|
|
19
|
+
* 读取前、每次工具调用前、每次 provider attempt 派发前和最终发布前,检查
|
|
20
|
+
* 权限、grant epoch、来源可用性与版本;失败抛类型化错误,旧快照/旧游标/
|
|
21
|
+
* 旧上下文一律失效(丢弃结果重算,不是删引用标记)。
|
|
22
|
+
*
|
|
23
|
+
* 只提供守卫,不实现业务逻辑;撤权、封存、接管共享同一份版本检查——
|
|
24
|
+
* "当前版本"由 ContextVersionAuthority 提供,各域存储在后续里程碑接入。
|
|
25
|
+
*
|
|
26
|
+
* 检查范围 = 快照绑定的域:快照里没出现的域不查询(grant_versions 空、
|
|
27
|
+
* memory_set_version 0、skill_bindings 空 …);authority 对某域回答
|
|
28
|
+
* `undefined` 视为"该对象已撤销/封存/失效"——fail-closed。
|
|
29
|
+
*/
|
|
30
|
+
import { ContextGovernanceError, } from "./types.js";
|
|
31
|
+
import { requireCapability, } from "./actor-context.js";
|
|
32
|
+
function fail(checkpoint, code, reason) {
|
|
33
|
+
throw new ContextGovernanceError(code, `${checkpoint}.${reason}`);
|
|
34
|
+
}
|
|
35
|
+
function validateSnapshotShape(snapshot, now, checkpoint) {
|
|
36
|
+
// 审计要求:形状错误的原因码保留实际检查点(publish.validation.…),不固定为 read。
|
|
37
|
+
if (typeof snapshot.snapshot_id !== "string" || snapshot.snapshot_id.length === 0) {
|
|
38
|
+
fail(checkpoint, "validation", "snapshot_id_missing");
|
|
39
|
+
}
|
|
40
|
+
const issued = Date.parse(snapshot.issued_at);
|
|
41
|
+
if (!Number.isFinite(issued)) {
|
|
42
|
+
fail(checkpoint, "validation", "issued_at_invalid");
|
|
43
|
+
}
|
|
44
|
+
if (issued > Date.parse(now)) {
|
|
45
|
+
fail(checkpoint, "validation", "issued_at_future");
|
|
46
|
+
}
|
|
47
|
+
if (!Number.isSafeInteger(snapshot.binding_epoch) || snapshot.binding_epoch < 0) {
|
|
48
|
+
fail(checkpoint, "validation", "binding_epoch_invalid");
|
|
49
|
+
}
|
|
50
|
+
if (!Number.isSafeInteger(snapshot.memory_set_version) || snapshot.memory_set_version < 0) {
|
|
51
|
+
fail(checkpoint, "validation", "memory_set_version_invalid");
|
|
52
|
+
}
|
|
53
|
+
if (!Number.isSafeInteger(snapshot.conversation_revision) || snapshot.conversation_revision < 0) {
|
|
54
|
+
fail(checkpoint, "validation", "conversation_revision_invalid");
|
|
55
|
+
}
|
|
56
|
+
for (const [grantId, epoch] of Object.entries(snapshot.grant_versions)) {
|
|
57
|
+
if (!Number.isSafeInteger(epoch) || epoch < 1) {
|
|
58
|
+
fail(checkpoint, "validation", `grant_epoch_invalid:${grantId}`);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
for (const [scopeKey, epoch] of Object.entries(snapshot.data_scope_epochs)) {
|
|
62
|
+
if (!Number.isSafeInteger(epoch) || epoch < 1) {
|
|
63
|
+
fail(checkpoint, "validation", `data_scope_epoch_invalid:${scopeKey}`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
for (const binding of snapshot.skill_bindings) {
|
|
67
|
+
if (typeof binding.skill_key !== "string" ||
|
|
68
|
+
binding.skill_key.length === 0 ||
|
|
69
|
+
typeof binding.release_id !== "string" ||
|
|
70
|
+
binding.release_id.length === 0 ||
|
|
71
|
+
typeof binding.compiled_artifact_hash !== "string" ||
|
|
72
|
+
binding.compiled_artifact_hash.length === 0 ||
|
|
73
|
+
!Number.isSafeInteger(binding.generation) ||
|
|
74
|
+
binding.generation < 1 ||
|
|
75
|
+
!Number.isSafeInteger(binding.revocation_epoch) ||
|
|
76
|
+
binding.revocation_epoch < 0) {
|
|
77
|
+
fail(checkpoint, "validation", "skill_binding_invalid");
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
export class ContextGuard {
|
|
82
|
+
authority;
|
|
83
|
+
now;
|
|
84
|
+
constructor(options) {
|
|
85
|
+
this.authority = options.authority;
|
|
86
|
+
this.now = options.now;
|
|
87
|
+
}
|
|
88
|
+
/** 读取前检查:能力(必填)+ 主体绑定 + grant/来源/版本一致性。 */
|
|
89
|
+
checkRead(input) {
|
|
90
|
+
this.check("read", input);
|
|
91
|
+
}
|
|
92
|
+
/** 工具调用前检查:同 read;工具名进 trace。 */
|
|
93
|
+
checkToolCall(input) {
|
|
94
|
+
this.check("tool_call", input);
|
|
95
|
+
}
|
|
96
|
+
/** provider attempt 派发前检查:重试/压缩/摘要分支同样要走(§4.3)。 */
|
|
97
|
+
checkProviderAttempt(input) {
|
|
98
|
+
this.check("provider_attempt", input);
|
|
99
|
+
}
|
|
100
|
+
/** 发布前检查(发布临界区的版本半边):全部域必须仍一致。 */
|
|
101
|
+
checkPublish(input) {
|
|
102
|
+
this.check("publish", input);
|
|
103
|
+
}
|
|
104
|
+
check(checkpoint, input) {
|
|
105
|
+
const now = this.now();
|
|
106
|
+
validateSnapshotShape(input.snapshot, now, checkpoint);
|
|
107
|
+
// §4.3:四个检查点"分别检查权限"——capability 必填,无旁路。
|
|
108
|
+
if (input.capability === undefined) {
|
|
109
|
+
fail(checkpoint, "validation", "capability_required");
|
|
110
|
+
}
|
|
111
|
+
try {
|
|
112
|
+
requireCapability(input.actor, input.capability);
|
|
113
|
+
}
|
|
114
|
+
catch (err) {
|
|
115
|
+
if (err instanceof ContextGovernanceError && err.code === "access_denied") {
|
|
116
|
+
fail(checkpoint, "access_denied", `capability_missing:${input.capability}`);
|
|
117
|
+
}
|
|
118
|
+
throw err;
|
|
119
|
+
}
|
|
120
|
+
const merchantId = input.actor.merchant_id;
|
|
121
|
+
const snapshot = input.snapshot;
|
|
122
|
+
// 主体绑定一致性(§9.1/§9.2):绑定态 actor 必须在快照中携带 binding_epoch,
|
|
123
|
+
// 匿名 actor 不能携带;否则快照可绕过绑定撤销失效检查。
|
|
124
|
+
if ((input.actor.subject_binding === "none") !== (snapshot.binding_epoch === 0)) {
|
|
125
|
+
fail(checkpoint, "validation", "binding_state_inconsistent");
|
|
126
|
+
}
|
|
127
|
+
if (snapshot.binding_epoch > 0) {
|
|
128
|
+
const current = this.authority.currentBindingEpoch(merchantId, input.actor.actor_id);
|
|
129
|
+
// 撤销绑定递增 binding_epoch(§9.2):旧快照在此统一失效。
|
|
130
|
+
if (current === undefined) {
|
|
131
|
+
fail(checkpoint, "stale_context", "binding_gone");
|
|
132
|
+
}
|
|
133
|
+
if (current !== snapshot.binding_epoch) {
|
|
134
|
+
fail(checkpoint, "stale_context", "binding_epoch_changed");
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
for (const [grantId, boundEpoch] of Object.entries(snapshot.grant_versions)) {
|
|
138
|
+
const current = this.authority.currentGrantEpoch(merchantId, grantId);
|
|
139
|
+
if (current === undefined) {
|
|
140
|
+
fail(checkpoint, "stale_context", `grant_gone:${grantId}`);
|
|
141
|
+
}
|
|
142
|
+
if (current !== boundEpoch) {
|
|
143
|
+
fail(checkpoint, "stale_context", `grant_epoch_changed:${grantId}`);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
for (const [scopeKey, boundEpoch] of Object.entries(snapshot.data_scope_epochs)) {
|
|
147
|
+
const current = this.authority.currentDataScopeEpoch(merchantId, scopeKey);
|
|
148
|
+
if (current === undefined) {
|
|
149
|
+
fail(checkpoint, "stale_context", `data_scope_gone:${scopeKey}`);
|
|
150
|
+
}
|
|
151
|
+
if (current !== boundEpoch) {
|
|
152
|
+
fail(checkpoint, "stale_context", `data_scope_epoch_changed:${scopeKey}`);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (snapshot.memory_set_version > 0) {
|
|
156
|
+
const current = this.authority.currentMemorySetVersion(merchantId);
|
|
157
|
+
if (current === undefined || current !== snapshot.memory_set_version) {
|
|
158
|
+
fail(checkpoint, "stale_context", "memory_set_version_changed");
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
if (snapshot.conversation_revision > 0) {
|
|
162
|
+
const current = this.authority.currentConversationRevision(merchantId);
|
|
163
|
+
if (current === undefined || current !== snapshot.conversation_revision) {
|
|
164
|
+
fail(checkpoint, "stale_context", "conversation_revision_changed");
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
const currentConfig = this.authority.currentConfigVersion();
|
|
168
|
+
if (snapshot.config_version !== "" && currentConfig !== snapshot.config_version) {
|
|
169
|
+
fail(checkpoint, "stale_context", "config_version_changed");
|
|
170
|
+
}
|
|
171
|
+
for (const binding of snapshot.skill_bindings) {
|
|
172
|
+
const current = this.authority.currentSkillBinding(merchantId, binding.skill_key);
|
|
173
|
+
if (current === undefined) {
|
|
174
|
+
fail(checkpoint, "stale_context", `skill_unbound:${binding.skill_key}`);
|
|
175
|
+
}
|
|
176
|
+
if (current.release_id !== binding.release_id ||
|
|
177
|
+
current.generation !== binding.generation ||
|
|
178
|
+
current.revocation_epoch !== binding.revocation_epoch) {
|
|
179
|
+
fail(checkpoint, "stale_context", `skill_version_changed:${binding.skill_key}`);
|
|
180
|
+
}
|
|
181
|
+
// 冻结产物必须与权威值逐值一致(§7.1"快照中的确切产物"):同 release
|
|
182
|
+
// 不同 hash 的绑定在此拒绝,防止旧产物借新版本号通过。
|
|
183
|
+
if (current.compiled_artifact_hash !== binding.compiled_artifact_hash) {
|
|
184
|
+
fail(checkpoint, "stale_context", `skill_artifact_changed:${binding.skill_key}`);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
for (const ref of input.source_refs ?? []) {
|
|
188
|
+
if (!this.authority.sourceAvailable(merchantId, ref)) {
|
|
189
|
+
fail(checkpoint, "source_unavailable", `source:${ref.object_id}`);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
//# sourceMappingURL=context-guard.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context-guard.js","sourceRoot":"","sources":["../../../src/merchant-core/context-governance/context-guard.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH;;;;;;;;;;;;;GAa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AO,CAAC,SAAS,EAAE,CAAC,CAAC;YACnF,CAAC;QACH,CAAC;QAED,KAAK,MAAM,GAAG,IAAI,KAAK,CAAC,WAAW,IAAI,EAAE,EAAE,CAAC;YAC1C,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,eAAe,CAAC,UAAU,EAAE,GAAG,CAAC,EAAE,CAAC;gBACrD,IAAI,CAAC,UAAU,EAAE,oBAAoB,EAAE,UAAU,GAAG,CAAC,SAAS,EAAE,CAAC,CAAC;YACpE,CAAC;QACH,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,91 @@
|
|
|
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 DataGrantStatus, type DataUsePurpose } from "./types.js";
|
|
18
|
+
export interface DataUseGrant {
|
|
19
|
+
readonly grant_id: string;
|
|
20
|
+
readonly merchant_id: string;
|
|
21
|
+
readonly subject_id: string;
|
|
22
|
+
readonly purpose: DataUsePurpose;
|
|
23
|
+
readonly resource_scope_id: string;
|
|
24
|
+
readonly grant_epoch: number;
|
|
25
|
+
readonly status: DataGrantStatus;
|
|
26
|
+
readonly expires_at: string;
|
|
27
|
+
readonly policy_version: string;
|
|
28
|
+
readonly created_at: string;
|
|
29
|
+
readonly updated_at: string;
|
|
30
|
+
}
|
|
31
|
+
export interface DataUseGrantStoreOptions {
|
|
32
|
+
db: DatabaseSync;
|
|
33
|
+
now: () => string;
|
|
34
|
+
}
|
|
35
|
+
export declare class DataUseGrantStore {
|
|
36
|
+
private readonly db;
|
|
37
|
+
private readonly now;
|
|
38
|
+
constructor(options: DataUseGrantStoreOptions);
|
|
39
|
+
/**
|
|
40
|
+
* 服务器时钟规范化为定宽 UTC(与存储形式一致,SQL 字符串比较才可靠)。
|
|
41
|
+
* 时钟是服务端可信注入:偏移/秒级形式一律规范化,只拒绝不可解析值。
|
|
42
|
+
*/
|
|
43
|
+
private nowUtc;
|
|
44
|
+
/**
|
|
45
|
+
* 授予一个数据用途。purpose/scope 决定用途边界:记忆授权不等于历史检索
|
|
46
|
+
* 或经验提炼授权。撤销/过期后重新授权产生新 grant_id 与递增 epoch。
|
|
47
|
+
*/
|
|
48
|
+
createGrant(input: {
|
|
49
|
+
merchant_id: string;
|
|
50
|
+
subject_id: string;
|
|
51
|
+
purpose: DataUsePurpose;
|
|
52
|
+
resource_scope_id: string;
|
|
53
|
+
expires_at: string;
|
|
54
|
+
}): DataUseGrant;
|
|
55
|
+
/**
|
|
56
|
+
* 撤销授权:CAS 在 expected_epoch 上;epoch 不匹配或已非 ACTIVE 抛
|
|
57
|
+
* version_conflict(§17.3:回读当前状态,不覆盖)。撤销本身递增
|
|
58
|
+
* grant_epoch(§17.1 `memory.revokeGrant` 返回新 grant_epoch)——递增值取
|
|
59
|
+
* 该 scope 当前最大 epoch +1,避免与同 scope 其他记录冲突;被撤记录的旧
|
|
60
|
+
* epoch 空出,旧快照绑定值此后必然失配。重新授权的 epoch 从递增后再 +1。
|
|
61
|
+
*/
|
|
62
|
+
revokeGrant(input: {
|
|
63
|
+
merchant_id: string;
|
|
64
|
+
grant_id: string;
|
|
65
|
+
expected_epoch: number;
|
|
66
|
+
}): DataUseGrant;
|
|
67
|
+
/** 读取当前有效授权:状态 ACTIVE 且未过期;无效统一返回 undefined。 */
|
|
68
|
+
getActiveGrant(input: {
|
|
69
|
+
merchant_id: string;
|
|
70
|
+
subject_id: string;
|
|
71
|
+
purpose: DataUsePurpose;
|
|
72
|
+
resource_scope_id: string;
|
|
73
|
+
}): DataUseGrant | undefined;
|
|
74
|
+
/** 把已到期的 ACTIVE 行翻为 EXPIRED(幂等;读取路径本就按时间过滤)。 */
|
|
75
|
+
expireDue(): number;
|
|
76
|
+
/**
|
|
77
|
+
* 主体作用域列表(客户自助/工作台):最小字段,仅当前商家;不返回其他
|
|
78
|
+
* 商家或其他主体的记录。
|
|
79
|
+
*/
|
|
80
|
+
listBySubject(input: {
|
|
81
|
+
merchant_id: string;
|
|
82
|
+
subject_id: string;
|
|
83
|
+
}): Array<Pick<DataUseGrant, "grant_id" | "purpose" | "resource_scope_id" | "grant_epoch" | "status" | "expires_at">>;
|
|
84
|
+
/**
|
|
85
|
+
* grant 当前有效 epoch(守卫用):仅当存在、ACTIVE 且未过期时返回 epoch,
|
|
86
|
+
* 否则 undefined——撤销、封存与到期在守卫语义下等价(旧快照失效)。
|
|
87
|
+
*/
|
|
88
|
+
currentGrantEpoch(merchantId: string, grantId: string): number | undefined;
|
|
89
|
+
/** 作用域读取:merchant 必须匹配;不存在时返回 undefined(不泄露存在性)。 */
|
|
90
|
+
private requireGrant;
|
|
91
|
+
}
|
|
@@ -0,0 +1,211 @@
|
|
|
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
|
+
* DataUseGrant 存储(TOP1-3 设计 v1.1 §4.2 / §9.2 / §18.1)。
|
|
18
|
+
*
|
|
19
|
+
* 授权、撤销、epoch 递增、过期都在服务端完成:
|
|
20
|
+
* - 创建:同 (merchant, subject, purpose, resource_scope) 的 epoch 取历史
|
|
21
|
+
* 最大值 +1(撤销/过期后的重新授权是新 grant_id、新 epoch,不复活旧记录);
|
|
22
|
+
* - 撤销:CAS 校验 expected_epoch,ACTIVE → REVOKED;
|
|
23
|
+
* - 过期:读取按 `status='ACTIVE' AND expires_at > now` 过滤(附录 C1:
|
|
24
|
+
* now 等于 expires_at 即失效);`expireDue` 把到期行翻为 EXPIRED 便于观测。
|
|
25
|
+
*
|
|
26
|
+
* 查询一律带 merchant/subject 作用域(§18.1:禁止无作用域 getById 给接待)。
|
|
27
|
+
* 表结构在 customer_memory 账本 v1(见 ../migrations/ledgers.ts)。
|
|
28
|
+
*/
|
|
29
|
+
import { randomUUID } from "node:crypto";
|
|
30
|
+
import { inImmediateTransaction } from "../storage/transaction.js";
|
|
31
|
+
import { ContextGovernanceError, DATA_USE_PURPOSES, GOVERNANCE_POLICY_VERSION, assertOpaqueId, } from "./types.js";
|
|
32
|
+
function rowToGrant(row) {
|
|
33
|
+
return {
|
|
34
|
+
grant_id: row.grant_id,
|
|
35
|
+
merchant_id: row.merchant_id,
|
|
36
|
+
subject_id: row.subject_id,
|
|
37
|
+
purpose: row.purpose,
|
|
38
|
+
resource_scope_id: row.resource_scope_id,
|
|
39
|
+
grant_epoch: row.grant_epoch,
|
|
40
|
+
status: row.status,
|
|
41
|
+
expires_at: row.expires_at,
|
|
42
|
+
policy_version: row.policy_version,
|
|
43
|
+
created_at: row.created_at,
|
|
44
|
+
updated_at: row.updated_at,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* 时间列以 UTC RFC3339 存储(工程约束:所有时间 RFC3339 UTC)。
|
|
49
|
+
* - 拒绝非 UTC 偏移(如 +08:00):SQL 侧按字符串比较到期时间,偏移形式
|
|
50
|
+
* 会破坏字典序与时刻序的一致性;
|
|
51
|
+
* - 返回 `toISOString()` 定宽形式(.sssZ):"…00Z" 与 "…00.000Z" 精度不同
|
|
52
|
+
* 也会破坏字典序(到期等号误判),写入前统一规范化。
|
|
53
|
+
*/
|
|
54
|
+
function assertUtcRfc3339(value, field) {
|
|
55
|
+
if (!value.endsWith("Z")) {
|
|
56
|
+
throw new ContextGovernanceError("validation", `${field}_not_utc`, `${field} 必须是 UTC RFC3339(Z 结尾),收到 ${value}`);
|
|
57
|
+
}
|
|
58
|
+
const ms = Date.parse(value);
|
|
59
|
+
if (!Number.isFinite(ms)) {
|
|
60
|
+
throw new ContextGovernanceError("validation", `${field}_invalid`, `${field} 必须是 RFC3339 时间`);
|
|
61
|
+
}
|
|
62
|
+
return new Date(ms).toISOString();
|
|
63
|
+
}
|
|
64
|
+
export class DataUseGrantStore {
|
|
65
|
+
db;
|
|
66
|
+
now;
|
|
67
|
+
constructor(options) {
|
|
68
|
+
this.db = options.db;
|
|
69
|
+
this.now = options.now;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* 服务器时钟规范化为定宽 UTC(与存储形式一致,SQL 字符串比较才可靠)。
|
|
73
|
+
* 时钟是服务端可信注入:偏移/秒级形式一律规范化,只拒绝不可解析值。
|
|
74
|
+
*/
|
|
75
|
+
nowUtc() {
|
|
76
|
+
const ms = Date.parse(this.now());
|
|
77
|
+
if (!Number.isFinite(ms)) {
|
|
78
|
+
throw new ContextGovernanceError("validation", "now_invalid", "时钟输出必须是可解析的 RFC3339 时间");
|
|
79
|
+
}
|
|
80
|
+
return new Date(ms).toISOString();
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* 授予一个数据用途。purpose/scope 决定用途边界:记忆授权不等于历史检索
|
|
84
|
+
* 或经验提炼授权。撤销/过期后重新授权产生新 grant_id 与递增 epoch。
|
|
85
|
+
*/
|
|
86
|
+
createGrant(input) {
|
|
87
|
+
const merchantId = assertOpaqueId(input.merchant_id, "merchant_id");
|
|
88
|
+
const subjectId = assertOpaqueId(input.subject_id, "subject_id");
|
|
89
|
+
const scopeId = assertOpaqueId(input.resource_scope_id, "resource_scope_id");
|
|
90
|
+
if (!DATA_USE_PURPOSES.includes(input.purpose)) {
|
|
91
|
+
throw new ContextGovernanceError("validation", "purpose_unknown", `purpose "${input.purpose}" 不在固定注册表中`);
|
|
92
|
+
}
|
|
93
|
+
const nowValue = this.nowUtc();
|
|
94
|
+
const expiresAt = assertUtcRfc3339(input.expires_at, "expires_at");
|
|
95
|
+
if (Date.parse(expiresAt) <= Date.parse(nowValue)) {
|
|
96
|
+
// 附录 C1:now 等于 expires_at 即失效——创建即过期没有意义,直接拒绝。
|
|
97
|
+
throw new ContextGovernanceError("validation", "expires_at_not_future", "expires_at 必须晚于当前时间");
|
|
98
|
+
}
|
|
99
|
+
return inImmediateTransaction(this.db, () => {
|
|
100
|
+
const row = this.db
|
|
101
|
+
.prepare(`SELECT MAX(grant_epoch) AS max_epoch FROM data_grants
|
|
102
|
+
WHERE merchant_id = ? AND subject_id = ? AND purpose = ? AND resource_scope_id = ?`)
|
|
103
|
+
.get(merchantId, subjectId, input.purpose, scopeId);
|
|
104
|
+
const epoch = (row.max_epoch ?? 0) + 1;
|
|
105
|
+
const grantId = `dug_${randomUUID()}`;
|
|
106
|
+
// 审计列(created_at/updated_at)与到期列同构:统一规范化定宽 UTC。
|
|
107
|
+
const stamp = this.nowUtc();
|
|
108
|
+
this.db
|
|
109
|
+
.prepare(`INSERT INTO data_grants
|
|
110
|
+
(grant_id, merchant_id, subject_id, purpose, resource_scope_id, grant_epoch,
|
|
111
|
+
status, expires_at, policy_version, created_at, updated_at)
|
|
112
|
+
VALUES (?, ?, ?, ?, ?, ?, 'ACTIVE', ?, ?, ?, ?)`)
|
|
113
|
+
.run(grantId, merchantId, subjectId, input.purpose, scopeId, epoch, expiresAt, GOVERNANCE_POLICY_VERSION, stamp, stamp);
|
|
114
|
+
return this.requireGrant(merchantId, grantId);
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* 撤销授权:CAS 在 expected_epoch 上;epoch 不匹配或已非 ACTIVE 抛
|
|
119
|
+
* version_conflict(§17.3:回读当前状态,不覆盖)。撤销本身递增
|
|
120
|
+
* grant_epoch(§17.1 `memory.revokeGrant` 返回新 grant_epoch)——递增值取
|
|
121
|
+
* 该 scope 当前最大 epoch +1,避免与同 scope 其他记录冲突;被撤记录的旧
|
|
122
|
+
* epoch 空出,旧快照绑定值此后必然失配。重新授权的 epoch 从递增后再 +1。
|
|
123
|
+
*/
|
|
124
|
+
revokeGrant(input) {
|
|
125
|
+
const merchantId = assertOpaqueId(input.merchant_id, "merchant_id");
|
|
126
|
+
const grantId = assertOpaqueId(input.grant_id, "grant_id");
|
|
127
|
+
if (!Number.isSafeInteger(input.expected_epoch) || input.expected_epoch < 1) {
|
|
128
|
+
throw new ContextGovernanceError("validation", "expected_epoch_invalid", "expected_epoch 必须是正整数");
|
|
129
|
+
}
|
|
130
|
+
return inImmediateTransaction(this.db, () => {
|
|
131
|
+
const existing = this.requireGrant(merchantId, grantId);
|
|
132
|
+
if (existing.grant_epoch !== input.expected_epoch || existing.status !== "ACTIVE") {
|
|
133
|
+
throw new ContextGovernanceError("version_conflict", "grant_epoch_mismatch", `grant ${grantId} 已变化(epoch=${existing.grant_epoch}, status=${existing.status})`);
|
|
134
|
+
}
|
|
135
|
+
this.db
|
|
136
|
+
.prepare(`UPDATE data_grants SET
|
|
137
|
+
grant_epoch = (SELECT COALESCE(MAX(grant_epoch), 0) + 1 FROM data_grants
|
|
138
|
+
WHERE merchant_id = ? AND subject_id = ? AND purpose = ?
|
|
139
|
+
AND resource_scope_id = ?),
|
|
140
|
+
status = 'REVOKED', updated_at = ?
|
|
141
|
+
WHERE merchant_id = ? AND grant_id = ? AND grant_epoch = ? AND status = 'ACTIVE'`)
|
|
142
|
+
.run(merchantId, existing.subject_id, existing.purpose, existing.resource_scope_id, this.nowUtc(), merchantId, grantId, input.expected_epoch);
|
|
143
|
+
return this.requireGrant(merchantId, grantId);
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
/** 读取当前有效授权:状态 ACTIVE 且未过期;无效统一返回 undefined。 */
|
|
147
|
+
getActiveGrant(input) {
|
|
148
|
+
const now = this.nowUtc();
|
|
149
|
+
const row = this.db
|
|
150
|
+
.prepare(`SELECT * FROM data_grants
|
|
151
|
+
WHERE merchant_id = ? AND subject_id = ? AND purpose = ? AND resource_scope_id = ?
|
|
152
|
+
AND status = 'ACTIVE' AND expires_at > ?
|
|
153
|
+
ORDER BY grant_epoch DESC LIMIT 1`)
|
|
154
|
+
.get(input.merchant_id, input.subject_id, input.purpose, input.resource_scope_id, now);
|
|
155
|
+
return row === undefined ? undefined : rowToGrant(row);
|
|
156
|
+
}
|
|
157
|
+
/** 把已到期的 ACTIVE 行翻为 EXPIRED(幂等;读取路径本就按时间过滤)。 */
|
|
158
|
+
expireDue() {
|
|
159
|
+
const now = this.nowUtc();
|
|
160
|
+
return inImmediateTransaction(this.db, () => {
|
|
161
|
+
const result = this.db
|
|
162
|
+
.prepare(`UPDATE data_grants SET status = 'EXPIRED', updated_at = ?
|
|
163
|
+
WHERE status = 'ACTIVE' AND expires_at <= ?`)
|
|
164
|
+
.run(now, now);
|
|
165
|
+
return Number(result.changes);
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* 主体作用域列表(客户自助/工作台):最小字段,仅当前商家;不返回其他
|
|
170
|
+
* 商家或其他主体的记录。
|
|
171
|
+
*/
|
|
172
|
+
listBySubject(input) {
|
|
173
|
+
const rows = this.db
|
|
174
|
+
.prepare(`SELECT grant_id, purpose, resource_scope_id, grant_epoch, status, expires_at
|
|
175
|
+
FROM data_grants
|
|
176
|
+
WHERE merchant_id = ? AND subject_id = ?
|
|
177
|
+
ORDER BY created_at DESC LIMIT 200`)
|
|
178
|
+
.all(input.merchant_id, input.subject_id);
|
|
179
|
+
return rows.map((row) => ({
|
|
180
|
+
grant_id: row.grant_id,
|
|
181
|
+
purpose: row.purpose,
|
|
182
|
+
resource_scope_id: row.resource_scope_id,
|
|
183
|
+
grant_epoch: row.grant_epoch,
|
|
184
|
+
status: row.status,
|
|
185
|
+
expires_at: row.expires_at,
|
|
186
|
+
}));
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* grant 当前有效 epoch(守卫用):仅当存在、ACTIVE 且未过期时返回 epoch,
|
|
190
|
+
* 否则 undefined——撤销、封存与到期在守卫语义下等价(旧快照失效)。
|
|
191
|
+
*/
|
|
192
|
+
currentGrantEpoch(merchantId, grantId) {
|
|
193
|
+
const now = this.nowUtc();
|
|
194
|
+
const row = this.db
|
|
195
|
+
.prepare(`SELECT grant_epoch FROM data_grants
|
|
196
|
+
WHERE merchant_id = ? AND grant_id = ? AND status = 'ACTIVE' AND expires_at > ?`)
|
|
197
|
+
.get(merchantId, grantId, now);
|
|
198
|
+
return row === undefined ? undefined : row.grant_epoch;
|
|
199
|
+
}
|
|
200
|
+
/** 作用域读取:merchant 必须匹配;不存在时返回 undefined(不泄露存在性)。 */
|
|
201
|
+
requireGrant(merchantId, grantId) {
|
|
202
|
+
const row = this.db
|
|
203
|
+
.prepare("SELECT * FROM data_grants WHERE merchant_id = ? AND grant_id = ?")
|
|
204
|
+
.get(merchantId, grantId);
|
|
205
|
+
if (row === undefined) {
|
|
206
|
+
throw new ContextGovernanceError("access_denied", "grant_not_found", "授权不存在或不可见");
|
|
207
|
+
}
|
|
208
|
+
return rowToGrant(row);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
//# sourceMappingURL=data-use-grant.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"data-use-grant.js","sourceRoot":"","sources":["../../../src/merchant-core/context-governance/data-use-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CAAC,SAAS,CAAC,CAAC,CAAC,GAAG,CAAC,WAAW,CAAC;IACzD,CAAC;IAED,oDAAoD;IAC5C,YAAY,CAAC,UAAkB,EAAE,OAAe;QACtD,MAAM,GAAG,GAAG,IAAI,CAAC,EAAE;aAChB,OAAO,CAAC,kEAAkE,CAAC;aAC3E,GAAG,CAAC,UAAU,EAAE,OAAO,CAAoC,CAAC;QAC/D,IAAI,GAAG,KAAK,SAAS,EAAE,CAAC;YACtB,MAAM,IAAI,sBAAsB,CAAC,eAAe,EAAE,iBAAiB,EAAE,WAAW,CAAC,CAAC;QACpF,CAAC;QACD,OAAO,UAAU,CAAC,GAAG,CAAC,CAAC;IACzB,CAAC;CACF"}
|
|
@@ -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 declare const GOVERNANCE_FLAG_KEYS: readonly ["learning_enabled", "memory_enabled", "jobs_enabled", "customer_delivery_enabled"];
|
|
17
|
+
export type GovernanceFlagKey = (typeof GOVERNANCE_FLAG_KEYS)[number];
|
|
18
|
+
export interface GovernanceFeatureFlags {
|
|
19
|
+
readonly learning_enabled: boolean;
|
|
20
|
+
readonly memory_enabled: boolean;
|
|
21
|
+
readonly jobs_enabled: boolean;
|
|
22
|
+
readonly customer_delivery_enabled: boolean;
|
|
23
|
+
}
|
|
24
|
+
export declare const GOVERNANCE_FLAGS_DEFAULT: Readonly<GovernanceFeatureFlags>;
|
|
25
|
+
export declare function assertFeatureEnabled(flags: GovernanceFeatureFlags, key: GovernanceFlagKey): void;
|
|
26
|
+
/**
|
|
27
|
+
* 从配置对象严格解析开关。缺省键取默认 false;未知键、非布尔值抛错。
|
|
28
|
+
*/
|
|
29
|
+
export declare function parseGovernanceFeatureFlags(input: Record<string, unknown> | undefined): GovernanceFeatureFlags;
|
|
30
|
+
export declare const GOVERNANCE_FLAG_ENV_NAMES: Readonly<Record<GovernanceFlagKey, string>>;
|
|
31
|
+
/** 从环境变量解析开关;非 "true"/"false" 的非空值一律抛错(启动失败)。 */
|
|
32
|
+
export declare function loadGovernanceFeatureFlagsFromEnv(env?: Record<string, string | undefined>): GovernanceFeatureFlags;
|
|
@@ -0,0 +1,83 @@
|
|
|
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
|
+
* TOP1-3 功能开关(设计 v1.1 §20.1 / D-M0-09 参数冻结:收尾决议 §1.2)。
|
|
18
|
+
*
|
|
19
|
+
* - `learning_enabled` / `memory_enabled` / `jobs_enabled` 默认 false,
|
|
20
|
+
* 显式试点启用;关闭时不改变现有路径行为。
|
|
21
|
+
* - `customer_delivery_enabled` P0 **固定关闭**:显式传 true 直接启动失败,
|
|
22
|
+
* 不存在"P0 试开"路径(P1 另行验收,见 §16.3 / M6)。
|
|
23
|
+
* - 严格解析:值只接受布尔(配置对象)或 "true"/"false"(环境变量);
|
|
24
|
+
* 未知字段、非法值一律抛错——非法配置启动失败,不静默回落。
|
|
25
|
+
* 变化须递增 policy_version,不通过环境变量按请求临时切换。
|
|
26
|
+
*/
|
|
27
|
+
import { ContextGovernanceError } from "./types.js";
|
|
28
|
+
export const GOVERNANCE_FLAG_KEYS = [
|
|
29
|
+
"learning_enabled",
|
|
30
|
+
"memory_enabled",
|
|
31
|
+
"jobs_enabled",
|
|
32
|
+
"customer_delivery_enabled",
|
|
33
|
+
];
|
|
34
|
+
export const GOVERNANCE_FLAGS_DEFAULT = Object.freeze({
|
|
35
|
+
learning_enabled: false,
|
|
36
|
+
memory_enabled: false,
|
|
37
|
+
jobs_enabled: false,
|
|
38
|
+
customer_delivery_enabled: false,
|
|
39
|
+
});
|
|
40
|
+
export function assertFeatureEnabled(flags, key) {
|
|
41
|
+
if (flags[key] !== true) {
|
|
42
|
+
throw new ContextGovernanceError("unsupported_capability", `feature_disabled:${key}`, `功能 ${key} 未启用;保持现有路径行为`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* 从配置对象严格解析开关。缺省键取默认 false;未知键、非布尔值抛错。
|
|
47
|
+
*/
|
|
48
|
+
export function parseGovernanceFeatureFlags(input) {
|
|
49
|
+
const flags = { ...GOVERNANCE_FLAGS_DEFAULT };
|
|
50
|
+
if (input !== undefined) {
|
|
51
|
+
for (const [key, value] of Object.entries(input)) {
|
|
52
|
+
if (!GOVERNANCE_FLAG_KEYS.includes(key)) {
|
|
53
|
+
throw new ContextGovernanceError("validation", `flag_unknown:${key}`, `未知功能开关 "${key}"(严格 schema,未知字段拒绝)`);
|
|
54
|
+
}
|
|
55
|
+
if (typeof value !== "boolean") {
|
|
56
|
+
throw new ContextGovernanceError("validation", `flag_invalid:${key}`, `功能开关 ${key} 必须是布尔值,收到 ${typeof value}`);
|
|
57
|
+
}
|
|
58
|
+
flags[key] = value;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return Object.freeze(flags);
|
|
62
|
+
}
|
|
63
|
+
export const GOVERNANCE_FLAG_ENV_NAMES = Object.freeze({
|
|
64
|
+
learning_enabled: "KIWI_GOVERNANCE_LEARNING_ENABLED",
|
|
65
|
+
memory_enabled: "KIWI_GOVERNANCE_MEMORY_ENABLED",
|
|
66
|
+
jobs_enabled: "KIWI_GOVERNANCE_JOBS_ENABLED",
|
|
67
|
+
customer_delivery_enabled: "KIWI_GOVERNANCE_CUSTOMER_DELIVERY_ENABLED",
|
|
68
|
+
});
|
|
69
|
+
/** 从环境变量解析开关;非 "true"/"false" 的非空值一律抛错(启动失败)。 */
|
|
70
|
+
export function loadGovernanceFeatureFlagsFromEnv(env = process.env) {
|
|
71
|
+
const input = {};
|
|
72
|
+
for (const key of GOVERNANCE_FLAG_KEYS) {
|
|
73
|
+
const raw = env[GOVERNANCE_FLAG_ENV_NAMES[key]];
|
|
74
|
+
if (raw === undefined || raw.trim() === "")
|
|
75
|
+
continue;
|
|
76
|
+
if (raw !== "true" && raw !== "false") {
|
|
77
|
+
throw new ContextGovernanceError("validation", `flag_env_invalid:${GOVERNANCE_FLAG_ENV_NAMES[key]}`, `${GOVERNANCE_FLAG_ENV_NAMES[key]} 只接受 "true"/"false",收到 "${raw}"`);
|
|
78
|
+
}
|
|
79
|
+
input[key] = raw === "true";
|
|
80
|
+
}
|
|
81
|
+
return parseGovernanceFeatureFlags(input);
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=feature-flags.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"feature-flags.js","sourceRoot":"","sources":["../../../src/merchant-core/context-governance/feature-flags.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH;;;;;;;;;;GAUG;AAEH,OAAO,EAAE,sBAAsB,EAAE,MAA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