@harrylabsj/kiwi 0.10.0 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/contracts/card-publication/0.1.2/request.schema.json +78 -0
- package/contracts/kiwi-contracts.lock.json +2 -2
- package/contracts/manifest.json +127 -1
- package/contracts/merchant-management/1.0/README.md +109 -0
- package/contracts/merchant-management/1.0/approval-decision.schema.json +16 -0
- package/contracts/merchant-management/1.0/approval-preview.schema.json +26 -0
- package/contracts/merchant-management/1.0/connector-delivery-manifest.schema.json +78 -0
- package/contracts/merchant-management/1.0/management-error.schema.json +30 -0
- package/contracts/merchant-management/1.0/operation-receipt.schema.json +28 -0
- package/contracts/merchant-management/1.0/runtime-management-descriptor.schema.json +33 -0
- package/contracts/merchant-management/1.0/runtime-status.schema.json +42 -0
- package/contracts/merchant-rfq/0.1.0/delivery-record.schema.json +206 -0
- package/contracts/merchant-rfq/0.1.0/fact-snapshot.schema.json +118 -0
- package/contracts/merchant-rfq/0.1.0/handoff-record.schema.json +251 -0
- package/contracts/merchant-rfq/0.1.0/pricing-input.schema.json +139 -0
- package/contracts/merchant-rfq/0.1.0/quote-revision.schema.json +153 -0
- package/contracts/merchant-rfq/0.1.0/release-request.schema.json +121 -0
- package/contracts/merchant-rfq/0.1.0/rfq-case.schema.json +357 -0
- package/contracts/merchant-rfq/0.1.0/source-record.schema.json +162 -0
- package/contracts/merchant-workbench/1.0/README.md +6 -0
- package/contracts/merchant-workbench/1.0/money.schema.json +19 -0
- package/contracts/merchant-workbench/1.0/problem.schema.json +73 -0
- package/contracts/runtime-binding/0.1.2/binding-request.schema.json +73 -0
- package/contracts/runtime-binding/0.1.2/claims.schema.json +100 -0
- package/contracts/runtime-binding/0.1.2/document.schema.json +161 -0
- package/contracts/vectors/binding-thumbprint.json +15 -0
- package/dist/a2a/node.d.ts +96 -2
- package/dist/a2a/node.js +164 -91
- package/dist/a2a/node.js.map +1 -1
- package/dist/a2a/server/errors.d.ts +8 -0
- package/dist/a2a/server/errors.js +8 -0
- package/dist/a2a/server/errors.js.map +1 -1
- package/dist/a2a/server/merchant-handler.d.ts +13 -0
- package/dist/a2a/server/merchant-handler.js +267 -34
- package/dist/a2a/server/merchant-handler.js.map +1 -1
- package/dist/a2a/server/pipeline.d.ts +15 -10
- package/dist/a2a/server/pipeline.js +48 -18
- package/dist/a2a/server/pipeline.js.map +1 -1
- package/dist/a2a/server/server.d.ts +20 -1
- package/dist/a2a/server/server.js +87 -13
- package/dist/a2a/server/server.js.map +1 -1
- package/dist/a2a/server/task-registry.d.ts +31 -11
- package/dist/a2a/server/task-registry.js +65 -20
- package/dist/a2a/server/task-registry.js.map +1 -1
- package/dist/a2a/server/types.d.ts +23 -0
- package/dist/a2a/signing-key.d.ts +6 -0
- package/dist/a2a/signing-key.js +12 -0
- package/dist/a2a/signing-key.js.map +1 -1
- package/dist/agent/buyer/buyer-tools.js +1 -1
- package/dist/agent/buyer/buyer-tools.js.map +1 -1
- package/dist/agent/host/web-renderer.d.ts +5 -0
- package/dist/agent/host/web-renderer.js +25 -6
- package/dist/agent/host/web-renderer.js.map +1 -1
- package/dist/agent/kernel.js +6 -6
- package/dist/agent/kernel.js.map +1 -1
- package/dist/agent/memory/schema.d.ts +1 -1
- package/dist/agent/memory/schema.js +190 -1
- package/dist/agent/memory/schema.js.map +1 -1
- package/dist/agent/memory/store.js +3 -1
- package/dist/agent/memory/store.js.map +1 -1
- package/dist/agent/merchant/action-candidate.d.ts +1 -0
- package/dist/agent/merchant/action-candidate.js +42 -24
- package/dist/agent/merchant/action-candidate.js.map +1 -1
- package/dist/agent/merchant/fake-merchant-client.d.ts +30 -1
- package/dist/agent/merchant/fake-merchant-client.js +151 -2
- package/dist/agent/merchant/fake-merchant-client.js.map +1 -1
- package/dist/agent/merchant/intelligence/default-backend.js +2 -2
- package/dist/agent/merchant/intelligence/default-backend.js.map +1 -1
- package/dist/agent/merchant/merchant-client.d.ts +38 -5
- package/dist/agent/merchant/merchant-client.js +103 -8
- package/dist/agent/merchant/merchant-client.js.map +1 -1
- package/dist/agent/merchant/types.d.ts +107 -0
- package/dist/agent/merchant/types.js +69 -0
- package/dist/agent/merchant/types.js.map +1 -1
- package/dist/alerts/external-delivery.d.ts +78 -0
- package/dist/alerts/external-delivery.js +256 -0
- package/dist/alerts/external-delivery.js.map +1 -0
- package/dist/auth/app-oauth-callback.d.ts +38 -0
- package/dist/auth/app-oauth-callback.js +119 -0
- package/dist/auth/app-oauth-callback.js.map +1 -0
- package/dist/auth/merchant-oauth.js +15 -41
- package/dist/auth/merchant-oauth.js.map +1 -1
- package/dist/auth/merchant-pairing.d.ts +51 -0
- package/dist/auth/merchant-pairing.js +66 -16
- package/dist/auth/merchant-pairing.js.map +1 -1
- package/dist/auth/merchant-sessions.d.ts +7 -1
- package/dist/auth/merchant-sessions.js +25 -3
- package/dist/auth/merchant-sessions.js.map +1 -1
- package/dist/buyer-core/build-service.d.ts +3 -1
- package/dist/buyer-core/build-service.js +4 -1
- package/dist/buyer-core/build-service.js.map +1 -1
- package/dist/buyer-core/merchant-index.d.ts +20 -0
- package/dist/buyer-core/merchant-index.js +83 -12
- package/dist/buyer-core/merchant-index.js.map +1 -1
- package/dist/buyer-core/network-search.d.ts +92 -0
- package/dist/buyer-core/network-search.js +144 -0
- package/dist/buyer-core/network-search.js.map +1 -0
- package/dist/buyer-core/product-summary.d.ts +71 -0
- package/dist/buyer-core/product-summary.js +63 -0
- package/dist/buyer-core/product-summary.js.map +1 -0
- package/dist/buyer-core/service.d.ts +26 -0
- package/dist/buyer-core/service.js +38 -8
- package/dist/buyer-core/service.js.map +1 -1
- package/dist/cli.js +21 -242
- package/dist/cli.js.map +1 -1
- package/dist/cloud/binding/audit.d.ts +72 -0
- package/dist/cloud/binding/audit.js +137 -0
- package/dist/cloud/binding/audit.js.map +1 -0
- package/dist/cloud/binding/issuance.d.ts +76 -0
- package/dist/cloud/binding/issuance.js +121 -0
- package/dist/cloud/binding/issuance.js.map +1 -0
- package/dist/cloud/binding/proofs.d.ts +110 -0
- package/dist/cloud/binding/proofs.js +200 -0
- package/dist/cloud/binding/proofs.js.map +1 -0
- package/dist/cloud/binding/runtime-challenge.d.ts +48 -0
- package/dist/cloud/binding/runtime-challenge.js +178 -0
- package/dist/cloud/binding/runtime-challenge.js.map +1 -0
- package/dist/cloud/bootstrap.d.ts +63 -0
- package/dist/cloud/bootstrap.js +937 -0
- package/dist/cloud/bootstrap.js.map +1 -0
- package/dist/cloud/config.d.ts +91 -0
- package/dist/cloud/config.js +229 -0
- package/dist/cloud/config.js.map +1 -0
- package/dist/cloud/http-router.d.ts +61 -0
- package/dist/cloud/http-router.js +127 -0
- package/dist/cloud/http-router.js.map +1 -0
- package/dist/cloud/main.d.ts +16 -0
- package/dist/cloud/main.js +64 -0
- package/dist/cloud/main.js.map +1 -0
- package/dist/cloud/onboarding/local-fallback.d.ts +86 -0
- package/dist/cloud/onboarding/local-fallback.js +98 -0
- package/dist/cloud/onboarding/local-fallback.js.map +1 -0
- package/dist/cloud/onboarding/steps.d.ts +95 -0
- package/dist/cloud/onboarding/steps.js +175 -0
- package/dist/cloud/onboarding/steps.js.map +1 -0
- package/dist/cloud/onboarding/store.d.ts +136 -0
- package/dist/cloud/onboarding/store.js +472 -0
- package/dist/cloud/onboarding/store.js.map +1 -0
- package/dist/cloud/onboarding/types.d.ts +132 -0
- package/dist/cloud/onboarding/types.js +152 -0
- package/dist/cloud/onboarding/types.js.map +1 -0
- package/dist/cloud/product-source.d.ts +97 -0
- package/dist/cloud/product-source.js +226 -0
- package/dist/cloud/product-source.js.map +1 -0
- package/dist/cloud/readiness.d.ts +49 -0
- package/dist/cloud/readiness.js +54 -0
- package/dist/cloud/readiness.js.map +1 -0
- package/dist/commerce/data-source.d.ts +5 -0
- package/dist/commerce/data-source.js.map +1 -1
- package/dist/config/provider-kind.d.ts +26 -0
- package/dist/config/provider-kind.js +19 -0
- package/dist/config/provider-kind.js.map +1 -0
- package/dist/contracts/cloud-contracts.d.ts +23 -0
- package/dist/contracts/cloud-contracts.js +90 -0
- package/dist/contracts/cloud-contracts.js.map +1 -0
- package/dist/counterparty/a2a-direct/index.d.ts +3 -0
- package/dist/counterparty/a2a-direct/index.js +22 -6
- package/dist/counterparty/a2a-direct/index.js.map +1 -1
- package/dist/counterparty/registry.d.ts +2 -0
- package/dist/counterparty/registry.js +2 -0
- package/dist/counterparty/registry.js.map +1 -1
- package/dist/counterparty/shopping-cli-hosted/index.d.ts +3 -0
- package/dist/counterparty/shopping-cli-hosted/index.js +9 -2
- package/dist/counterparty/shopping-cli-hosted/index.js.map +1 -1
- package/dist/discovery/catalog-source/buyer-follows.js +2 -2
- package/dist/discovery/catalog-source/buyer-follows.js.map +1 -1
- package/dist/discovery/catalog-source/cloud-card.d.ts +184 -0
- package/dist/discovery/catalog-source/cloud-card.js +379 -0
- package/dist/discovery/catalog-source/cloud-card.js.map +1 -0
- package/dist/discovery/catalog-source/cloud-hosting.d.ts +71 -0
- package/dist/discovery/catalog-source/cloud-hosting.js +70 -0
- package/dist/discovery/catalog-source/cloud-hosting.js.map +1 -0
- package/dist/discovery/catalog-source/errors.d.ts +12 -2
- package/dist/discovery/catalog-source/errors.js +14 -1
- package/dist/discovery/catalog-source/errors.js.map +1 -1
- package/dist/discovery/catalog-source/index.d.ts +2 -0
- package/dist/discovery/catalog-source/index.js +2 -0
- package/dist/discovery/catalog-source/index.js.map +1 -1
- package/dist/discovery/catalog-source/kiwi-source.js +2 -2
- package/dist/discovery/catalog-source/kiwi-source.js.map +1 -1
- package/dist/discovery/catalog-source/merchant-publications.d.ts +2 -2
- package/dist/discovery/catalog-source/merchant-publications.js +10 -5
- package/dist/discovery/catalog-source/merchant-publications.js.map +1 -1
- package/dist/discovery/catalog-source/source.js +2 -2
- package/dist/discovery/catalog-source/source.js.map +1 -1
- package/dist/discovery/merchant-subscriptions.d.ts +45 -0
- package/dist/discovery/merchant-subscriptions.js +319 -0
- package/dist/discovery/merchant-subscriptions.js.map +1 -0
- package/dist/discovery/resolve.d.ts +38 -0
- package/dist/discovery/resolve.js +100 -7
- package/dist/discovery/resolve.js.map +1 -1
- package/dist/fanout/orchestrator.d.ts +2 -0
- package/dist/fanout/orchestrator.js +4 -1
- package/dist/fanout/orchestrator.js.map +1 -1
- package/dist/handoff/ledger.d.ts +1 -0
- package/dist/handoff/ledger.js +3 -0
- package/dist/handoff/ledger.js.map +1 -1
- package/dist/handoff/transaction.js +1 -1
- package/dist/handoff/transaction.js.map +1 -1
- package/dist/http/merchant-engagement-api.d.ts +10 -0
- package/dist/http/merchant-engagement-api.js +138 -0
- package/dist/http/merchant-engagement-api.js.map +1 -0
- package/dist/http/merchant-feed-api.d.ts +7 -0
- package/dist/http/merchant-feed-api.js +129 -0
- package/dist/http/merchant-feed-api.js.map +1 -0
- package/dist/http/merchant-follow-api.d.ts +15 -0
- package/dist/http/merchant-follow-api.js +192 -0
- package/dist/http/merchant-follow-api.js.map +1 -0
- package/dist/http/merchant-management/api.d.ts +241 -0
- package/dist/http/merchant-management/api.js +2785 -0
- package/dist/http/merchant-management/api.js.map +1 -0
- package/dist/http/merchant-management/draft-store.d.ts +51 -0
- package/dist/http/merchant-management/draft-store.js +95 -0
- package/dist/http/merchant-management/draft-store.js.map +1 -0
- package/dist/http/merchant-management/event-projection.d.ts +41 -0
- package/dist/http/merchant-management/event-projection.js +190 -0
- package/dist/http/merchant-management/event-projection.js.map +1 -0
- package/dist/http/merchant-management/operation-store.d.ts +97 -0
- package/dist/http/merchant-management/operation-store.js +252 -0
- package/dist/http/merchant-management/operation-store.js.map +1 -0
- package/dist/http/merchant-management/page.d.ts +29 -0
- package/dist/http/merchant-management/page.js +346 -0
- package/dist/http/merchant-management/page.js.map +1 -0
- package/dist/http/merchant-management/problem-client.d.ts +19 -0
- package/dist/http/merchant-management/problem-client.js +55 -0
- package/dist/http/merchant-management/problem-client.js.map +1 -0
- package/dist/http/merchant-management/problem.d.ts +74 -0
- package/dist/http/merchant-management/problem.js +112 -0
- package/dist/http/merchant-management/problem.js.map +1 -0
- package/dist/http/merchant-management/reconciliation-worker.d.ts +95 -0
- package/dist/http/merchant-management/reconciliation-worker.js +397 -0
- package/dist/http/merchant-management/reconciliation-worker.js.map +1 -0
- package/dist/http/merchant-management/service-state.d.ts +86 -0
- package/dist/http/merchant-management/service-state.js +217 -0
- package/dist/http/merchant-management/service-state.js.map +1 -0
- package/dist/http/merchant-management/trusted-page.d.ts +3 -0
- package/dist/http/merchant-management/trusted-page.js +73 -0
- package/dist/http/merchant-management/trusted-page.js.map +1 -0
- package/dist/http/merchant-management/webauthn-confirmation.d.ts +148 -0
- package/dist/http/merchant-management/webauthn-confirmation.js +517 -0
- package/dist/http/merchant-management/webauthn-confirmation.js.map +1 -0
- package/dist/http/merchant-privacy-api.d.ts +9 -0
- package/dist/http/merchant-privacy-api.js +96 -0
- package/dist/http/merchant-privacy-api.js.map +1 -0
- package/dist/mcp/merchant-rfq-resources.d.ts +37 -0
- package/dist/mcp/merchant-rfq-resources.js +132 -0
- package/dist/mcp/merchant-rfq-resources.js.map +1 -0
- package/dist/mcp/merchant-rfq-tools.d.ts +61 -0
- package/dist/mcp/merchant-rfq-tools.js +584 -0
- package/dist/mcp/merchant-rfq-tools.js.map +1 -0
- package/dist/mcp/merchant-runtime-assembly.d.ts +74 -0
- package/dist/mcp/merchant-runtime-assembly.js +473 -0
- package/dist/mcp/merchant-runtime-assembly.js.map +1 -0
- package/dist/mcp/merchant-server.d.ts +64 -1
- package/dist/mcp/merchant-server.js +245 -23
- package/dist/mcp/merchant-server.js.map +1 -1
- package/dist/mcp/tools.js +8 -2
- package/dist/mcp/tools.js.map +1 -1
- package/dist/merchant/application/actor.d.ts +85 -0
- package/dist/merchant/application/actor.js +162 -0
- package/dist/merchant/application/actor.js.map +1 -0
- package/dist/merchant/application/money.d.ts +35 -0
- package/dist/merchant/application/money.js +124 -0
- package/dist/merchant/application/money.js.map +1 -0
- package/dist/merchant/application/service.d.ts +228 -0
- package/dist/merchant/application/service.js +262 -0
- package/dist/merchant/application/service.js.map +1 -0
- package/dist/merchant/clock-safety.d.ts +50 -0
- package/dist/merchant/clock-safety.js +153 -0
- package/dist/merchant/clock-safety.js.map +1 -0
- package/dist/merchant/engagement-store.d.ts +32 -0
- package/dist/merchant/engagement-store.js +119 -0
- package/dist/merchant/engagement-store.js.map +1 -0
- package/dist/merchant/exact-product-executors.d.ts +11 -0
- package/dist/merchant/exact-product-executors.js +132 -0
- package/dist/merchant/exact-product-executors.js.map +1 -0
- package/dist/merchant/feed-executors.d.ts +17 -0
- package/dist/merchant/feed-executors.js +151 -0
- package/dist/merchant/feed-executors.js.map +1 -0
- package/dist/merchant/feed-store.d.ts +141 -0
- package/dist/merchant/feed-store.js +496 -0
- package/dist/merchant/feed-store.js.map +1 -0
- package/dist/merchant/follow-store.d.ts +66 -0
- package/dist/merchant/follow-store.js +295 -0
- package/dist/merchant/follow-store.js.map +1 -0
- package/dist/merchant/grant-executors.d.ts +11 -0
- package/dist/merchant/grant-executors.js +136 -0
- package/dist/merchant/grant-executors.js.map +1 -0
- package/dist/merchant/grant-store.d.ts +125 -0
- package/dist/merchant/grant-store.js +313 -0
- package/dist/merchant/grant-store.js.map +1 -0
- package/dist/merchant/inventory-executors.d.ts +47 -0
- package/dist/merchant/inventory-executors.js +91 -0
- package/dist/merchant/inventory-executors.js.map +1 -0
- package/dist/merchant/listing-executors.d.ts +51 -0
- package/dist/merchant/listing-executors.js +100 -0
- package/dist/merchant/listing-executors.js.map +1 -0
- package/dist/merchant/operation-receipts.d.ts +57 -0
- package/dist/merchant/operation-receipts.js +138 -0
- package/dist/merchant/operation-receipts.js.map +1 -0
- package/dist/merchant/promotion-broadcast-recovery.d.ts +22 -0
- package/dist/merchant/promotion-broadcast-recovery.js +74 -0
- package/dist/merchant/promotion-broadcast-recovery.js.map +1 -0
- package/dist/merchant/promotion-broadcast-workflow.d.ts +42 -0
- package/dist/merchant/promotion-broadcast-workflow.js +171 -0
- package/dist/merchant/promotion-broadcast-workflow.js.map +1 -0
- package/dist/merchant/promotion-executors.d.ts +18 -0
- package/dist/merchant/promotion-executors.js +133 -0
- package/dist/merchant/promotion-executors.js.map +1 -0
- package/dist/merchant/promotion-store.d.ts +114 -0
- package/dist/merchant/promotion-store.js +325 -0
- package/dist/merchant/promotion-store.js.map +1 -0
- package/dist/merchant/promotion-time.d.ts +19 -0
- package/dist/merchant/promotion-time.js +162 -0
- package/dist/merchant/promotion-time.js.map +1 -0
- package/dist/merchant/quote-calculator.d.ts +32 -0
- package/dist/merchant/quote-calculator.js +70 -0
- package/dist/merchant/quote-calculator.js.map +1 -0
- package/dist/merchant/service-control-executors.d.ts +15 -0
- package/dist/merchant/service-control-executors.js +52 -0
- package/dist/merchant/service-control-executors.js.map +1 -0
- package/dist/merchant/workbench-service.d.ts +1 -0
- package/dist/merchant/workbench-service.js +11 -1
- package/dist/merchant/workbench-service.js.map +1 -1
- package/dist/merchant-admin/pending-page.d.ts +104 -0
- package/dist/merchant-admin/pending-page.js +20 -0
- package/dist/merchant-admin/pending-page.js.map +1 -1
- package/dist/merchant-admin/rfq-page.d.ts +105 -0
- package/dist/merchant-admin/rfq-page.js +176 -0
- package/dist/merchant-admin/rfq-page.js.map +1 -0
- package/dist/merchant-core/commands.d.ts +27 -0
- package/dist/merchant-core/commands.js +90 -1
- package/dist/merchant-core/commands.js.map +1 -1
- package/dist/merchant-core/executor.d.ts +27 -3
- package/dist/merchant-core/executor.js +13 -4
- package/dist/merchant-core/executor.js.map +1 -1
- package/dist/merchant-core/rfq/artifacts.d.ts +78 -0
- package/dist/merchant-core/rfq/artifacts.js +219 -0
- package/dist/merchant-core/rfq/artifacts.js.map +1 -0
- package/dist/merchant-core/rfq/data-source-adapter.d.ts +49 -0
- package/dist/merchant-core/rfq/data-source-adapter.js +128 -0
- package/dist/merchant-core/rfq/data-source-adapter.js.map +1 -0
- package/dist/merchant-core/rfq/delivery.d.ts +43 -0
- package/dist/merchant-core/rfq/delivery.js +57 -0
- package/dist/merchant-core/rfq/delivery.js.map +1 -0
- package/dist/merchant-core/rfq/fact-resolver.d.ts +76 -0
- package/dist/merchant-core/rfq/fact-resolver.js +220 -0
- package/dist/merchant-core/rfq/fact-resolver.js.map +1 -0
- package/dist/merchant-core/rfq/handoff.d.ts +54 -0
- package/dist/merchant-core/rfq/handoff.js +71 -0
- package/dist/merchant-core/rfq/handoff.js.map +1 -0
- package/dist/merchant-core/rfq/policy.d.ts +77 -0
- package/dist/merchant-core/rfq/policy.js +104 -0
- package/dist/merchant-core/rfq/policy.js.map +1 -0
- package/dist/merchant-core/rfq/pricing.d.ts +48 -0
- package/dist/merchant-core/rfq/pricing.js +150 -0
- package/dist/merchant-core/rfq/pricing.js.map +1 -0
- package/dist/merchant-core/rfq/quote-revisions.d.ts +52 -0
- package/dist/merchant-core/rfq/quote-revisions.js +126 -0
- package/dist/merchant-core/rfq/quote-revisions.js.map +1 -0
- package/dist/merchant-core/rfq/release-coordinator.d.ts +99 -0
- package/dist/merchant-core/rfq/release-coordinator.js +355 -0
- package/dist/merchant-core/rfq/release-coordinator.js.map +1 -0
- package/dist/merchant-core/rfq/repository.d.ts +398 -0
- package/dist/merchant-core/rfq/repository.js +855 -0
- package/dist/merchant-core/rfq/repository.js.map +1 -0
- package/dist/merchant-core/rfq/service.d.ts +398 -0
- package/dist/merchant-core/rfq/service.js +1036 -0
- package/dist/merchant-core/rfq/service.js.map +1 -0
- package/dist/merchant-core/rfq/source-records.d.ts +97 -0
- package/dist/merchant-core/rfq/source-records.js +349 -0
- package/dist/merchant-core/rfq/source-records.js.map +1 -0
- package/dist/merchant-core/rfq/types.d.ts +197 -0
- package/dist/merchant-core/rfq/types.js +110 -0
- package/dist/merchant-core/rfq/types.js.map +1 -0
- package/dist/merchant-core/service.d.ts +118 -1
- package/dist/merchant-core/service.js +202 -7
- package/dist/merchant-core/service.js.map +1 -1
- package/dist/merchant-core/storage/clock-skew-alerts.d.ts +21 -0
- package/dist/merchant-core/storage/clock-skew-alerts.js +67 -0
- package/dist/merchant-core/storage/clock-skew-alerts.js.map +1 -0
- package/dist/merchant-core/storage/leased-job.d.ts +42 -0
- package/dist/merchant-core/storage/leased-job.js +38 -0
- package/dist/merchant-core/storage/leased-job.js.map +1 -0
- package/dist/merchant-core/storage/operation-lifecycle.d.ts +78 -0
- package/dist/merchant-core/storage/operation-lifecycle.js +111 -0
- package/dist/merchant-core/storage/operation-lifecycle.js.map +1 -0
- package/dist/merchant-core/storage/redact.d.ts +29 -0
- package/dist/merchant-core/storage/redact.js +34 -0
- package/dist/merchant-core/storage/redact.js.map +1 -0
- package/dist/merchant-core/storage/schema.d.ts +24 -0
- package/dist/merchant-core/storage/schema.js +22 -0
- package/dist/merchant-core/storage/schema.js.map +1 -0
- package/dist/merchant-core/storage/transaction.d.ts +47 -0
- package/dist/merchant-core/storage/transaction.js +28 -0
- package/dist/merchant-core/storage/transaction.js.map +1 -0
- package/dist/merchant-gateway/entry-server.js +10 -0
- package/dist/merchant-gateway/entry-server.js.map +1 -1
- package/dist/negotiation/domain/objects.d.ts +7 -0
- package/dist/negotiation/domain/objects.js +8 -0
- package/dist/negotiation/domain/objects.js.map +1 -1
- package/dist/negotiation/idempotency/store.d.ts +23 -0
- package/dist/negotiation/idempotency/store.js +72 -1
- package/dist/negotiation/idempotency/store.js.map +1 -1
- package/dist/negotiation/idempotency/types.d.ts +16 -0
- package/dist/negotiation/idempotency/types.js +2 -0
- package/dist/negotiation/idempotency/types.js.map +1 -1
- package/dist/negotiation/lease/store.d.ts +18 -22
- package/dist/negotiation/lease/store.js +133 -88
- package/dist/negotiation/lease/store.js.map +1 -1
- package/dist/negotiation/ledger/event.d.ts +6 -0
- package/dist/negotiation/ledger/event.js +1 -0
- package/dist/negotiation/ledger/event.js.map +1 -1
- package/dist/negotiation/ledger/index.d.ts +4 -2
- package/dist/negotiation/ledger/index.js +1 -0
- package/dist/negotiation/ledger/index.js.map +1 -1
- package/dist/negotiation/ledger/payload-segments.d.ts +51 -0
- package/dist/negotiation/ledger/payload-segments.js +247 -0
- package/dist/negotiation/ledger/payload-segments.js.map +1 -0
- package/dist/negotiation/ledger/store.d.ts +34 -0
- package/dist/negotiation/ledger/store.js +162 -0
- package/dist/negotiation/ledger/store.js.map +1 -1
- package/dist/negotiation/recovery/recover.js +1 -1
- package/dist/negotiation/recovery/recover.js.map +1 -1
- package/dist/privacy/ledger-payload-deletion.d.ts +10 -0
- package/dist/privacy/ledger-payload-deletion.js +31 -0
- package/dist/privacy/ledger-payload-deletion.js.map +1 -0
- package/dist/privacy/local-backup-deletion.d.ts +13 -0
- package/dist/privacy/local-backup-deletion.js +115 -0
- package/dist/privacy/local-backup-deletion.js.map +1 -0
- package/dist/privacy/workbench-retention.d.ts +105 -0
- package/dist/privacy/workbench-retention.js +501 -0
- package/dist/privacy/workbench-retention.js.map +1 -0
- package/dist/runtime/model.d.ts +1 -1
- package/dist/runtime/model.js +3 -3
- package/dist/runtime/model.js.map +1 -1
- package/dist/trust/binding/claims.d.ts +92 -0
- package/dist/trust/binding/claims.js +175 -0
- package/dist/trust/binding/claims.js.map +1 -0
- package/dist/trust/binding/thumbprint.d.ts +35 -0
- package/dist/trust/binding/thumbprint.js +88 -0
- package/dist/trust/binding/thumbprint.js.map +1 -0
- package/dist/trust/binding/verify.d.ts +84 -0
- package/dist/trust/binding/verify.js +121 -0
- package/dist/trust/binding/verify.js.map +1 -0
- package/dist/trust/identity/auth-verifier.d.ts +9 -0
- package/dist/trust/identity/auth-verifier.js +10 -2
- package/dist/trust/identity/auth-verifier.js.map +1 -1
- package/dist/trust/identity/jws.d.ts +22 -1
- package/dist/trust/identity/jws.js +31 -2
- package/dist/trust/identity/jws.js.map +1 -1
- package/package.json +12 -4
- package/skills/kiwi-buyer/SKILL.md +33 -2
|
@@ -0,0 +1,855 @@
|
|
|
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
|
+
* RFQ 持久化(设计 v0.1.1 §10.1/§10.2/§10.3)。
|
|
18
|
+
*
|
|
19
|
+
* - 表结构为迁移 MIGRATION_8(src/agent/memory/schema.ts,编号迁移 +
|
|
20
|
+
* schema_version);本层只读写,不建表。
|
|
21
|
+
* - 单商家单 owner 写(与审批候选同一 state.sqlite);跨商家读取一律
|
|
22
|
+
* fail-closed(TENANT_MISMATCH,不泄露对象存在性)。
|
|
23
|
+
* - 状态转移用条件 UPDATE(CAS)串行化:只有一个调用方能完成转移,
|
|
24
|
+
* 并发失败返回 version_conflict / approval_stale。
|
|
25
|
+
* - 幂等:merchant+principal+operation+key 唯一;同键同请求摘要重放
|
|
26
|
+
* 同一结果,同键不同摘要 IDEMPOTENCY_CONFLICT(摘要 = rfq-canonical-json-v1)。
|
|
27
|
+
*/
|
|
28
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
29
|
+
import { canonicalJson, rfqContentDigest, RfqError, } from "./types.js";
|
|
30
|
+
export const RFQ_SCHEMA_VERSION = 8;
|
|
31
|
+
function nowIsoOrThrow(value, name) {
|
|
32
|
+
if (typeof value !== "string" || Number.isNaN(Date.parse(value))) {
|
|
33
|
+
throw new RfqError("validation", `${name} 必须是 RFC 3339 时间戳`);
|
|
34
|
+
}
|
|
35
|
+
return value;
|
|
36
|
+
}
|
|
37
|
+
export class RfqRepository {
|
|
38
|
+
db;
|
|
39
|
+
merchantId;
|
|
40
|
+
now;
|
|
41
|
+
constructor(deps) {
|
|
42
|
+
this.db = deps.db;
|
|
43
|
+
this.merchantId = deps.merchantId;
|
|
44
|
+
this.now = deps.now;
|
|
45
|
+
}
|
|
46
|
+
inTx = false;
|
|
47
|
+
/** 事务执行(可重入:内层调用加入外层事务——内层失败抛错由外层统一回滚)。 */
|
|
48
|
+
tx(work) {
|
|
49
|
+
if (this.inTx)
|
|
50
|
+
return work();
|
|
51
|
+
this.inTx = true;
|
|
52
|
+
this.db.exec("BEGIN");
|
|
53
|
+
try {
|
|
54
|
+
const result = work();
|
|
55
|
+
this.db.exec("COMMIT");
|
|
56
|
+
return result;
|
|
57
|
+
}
|
|
58
|
+
catch (err) {
|
|
59
|
+
this.db.exec("ROLLBACK");
|
|
60
|
+
throw err;
|
|
61
|
+
}
|
|
62
|
+
finally {
|
|
63
|
+
this.inTx = false;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* 跨仓库单元事务(§10.2 UnitOfWork port):生产装配中审批候选 store 与本
|
|
68
|
+
* 仓库共用同一 SQLite 连接,work 内的候选登记与本仓库写在同一
|
|
69
|
+
* BEGIN/COMMIT 内全部提交或全部回滚。work 只做本地同步 DB 写(await 仅
|
|
70
|
+
* 微任务桥接,无外部 I/O),事务不跨外部调用。
|
|
71
|
+
*/
|
|
72
|
+
async runInTransactionAsync(work) {
|
|
73
|
+
if (this.inTx)
|
|
74
|
+
return await work();
|
|
75
|
+
this.inTx = true;
|
|
76
|
+
this.db.exec("BEGIN");
|
|
77
|
+
try {
|
|
78
|
+
const result = await work();
|
|
79
|
+
this.db.exec("COMMIT");
|
|
80
|
+
return result;
|
|
81
|
+
}
|
|
82
|
+
catch (err) {
|
|
83
|
+
this.db.exec("ROLLBACK");
|
|
84
|
+
throw err;
|
|
85
|
+
}
|
|
86
|
+
finally {
|
|
87
|
+
this.inTx = false;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
id(prefix) {
|
|
91
|
+
return `${prefix}_${randomUUID()}`;
|
|
92
|
+
}
|
|
93
|
+
// ---- 审计 ---------------------------------------------------------------
|
|
94
|
+
appendAudit(input) {
|
|
95
|
+
this.db
|
|
96
|
+
.prepare("INSERT INTO rfq_audit_events (at, actor, operation, object_digest, result, trace_id) VALUES (?, ?, ?, ?, ?, ?)")
|
|
97
|
+
.run(this.now(), input.actor, input.operation, input.objectDigest, input.result, input.traceId);
|
|
98
|
+
}
|
|
99
|
+
// ---- Case 与需求 revision ----------------------------------------------
|
|
100
|
+
createCase() {
|
|
101
|
+
const caseId = this.id("rfqcase");
|
|
102
|
+
const now = this.now();
|
|
103
|
+
return this.tx(() => {
|
|
104
|
+
this.db
|
|
105
|
+
.prepare("INSERT INTO rfq_cases (case_id, merchant_id, current_revision, stage, version, created_at, updated_at) VALUES (?, ?, 0, 'NEW', 1, ?, ?)")
|
|
106
|
+
.run(caseId, this.merchantId, now, now);
|
|
107
|
+
return this.getCase(caseId);
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
listCases(limit) {
|
|
111
|
+
const rows = this.db
|
|
112
|
+
.prepare("SELECT case_id FROM rfq_cases WHERE merchant_id = ? ORDER BY updated_at DESC LIMIT ?")
|
|
113
|
+
.all(this.merchantId, limit);
|
|
114
|
+
return rows.map((r) => this.getCase(r.case_id));
|
|
115
|
+
}
|
|
116
|
+
getCase(caseId) {
|
|
117
|
+
const row = this.db
|
|
118
|
+
.prepare("SELECT * FROM rfq_cases WHERE case_id = ? AND merchant_id = ?")
|
|
119
|
+
.get(caseId, this.merchantId);
|
|
120
|
+
if (row === undefined)
|
|
121
|
+
return undefined;
|
|
122
|
+
return {
|
|
123
|
+
case_id: String(row.case_id),
|
|
124
|
+
merchant_id: String(row.merchant_id),
|
|
125
|
+
current_revision: Number(row.current_revision),
|
|
126
|
+
current_quote_id: row.current_quote_id === null ? null : String(row.current_quote_id),
|
|
127
|
+
current_quote_revision: row.current_quote_revision === null ? null : Number(row.current_quote_revision),
|
|
128
|
+
stage: String(row.stage),
|
|
129
|
+
version: Number(row.version),
|
|
130
|
+
created_at: String(row.created_at),
|
|
131
|
+
updated_at: String(row.updated_at),
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
/** 期望版本 CAS 推进(任何关键变更都必须带 expected_revision)。 */
|
|
135
|
+
assertCaseVersion(caseId, expectedVersion) {
|
|
136
|
+
const current = this.getCase(caseId);
|
|
137
|
+
if (current === undefined) {
|
|
138
|
+
throw new RfqError("not_found", `未知询盘 ${caseId}`);
|
|
139
|
+
}
|
|
140
|
+
if (current.version !== expectedVersion) {
|
|
141
|
+
throw new RfqError("version_conflict", `询盘 ${caseId} 已变化(version ${current.version} ≠ 期望 ${expectedVersion})`);
|
|
142
|
+
}
|
|
143
|
+
return current;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* 新需求 revision:单调递增;stage 按 current_revision 重算(v0.1.1 §8.1)。
|
|
147
|
+
* blockers 不落库(只用于本次 stage 判定);读取端一律按 fields 用
|
|
148
|
+
* computeBlockers 重算(确定性),getCaseRevision 返回空数组是既定语义。
|
|
149
|
+
*/
|
|
150
|
+
createCaseRevision(input) {
|
|
151
|
+
return this.tx(() => {
|
|
152
|
+
const current = this.assertCaseVersion(input.caseId, input.expectedVersion);
|
|
153
|
+
const revisionNo = current.current_revision + 1;
|
|
154
|
+
const now = this.now();
|
|
155
|
+
this.db
|
|
156
|
+
.prepare("INSERT INTO rfq_case_revisions (case_id, revision, fields_json, source_ids_json, created_at) VALUES (?, ?, ?, ?, ?)")
|
|
157
|
+
.run(input.caseId, revisionNo, canonicalJson(input.fields), canonicalJson(input.sourceIds), now);
|
|
158
|
+
const stage = input.blockers.length === 0 ? "READY" : "NEEDS_CLARIFICATION";
|
|
159
|
+
// stage 判定对象始终是 current_revision;不继承旧 revision 的 READY/PRICED。
|
|
160
|
+
this.db
|
|
161
|
+
.prepare("UPDATE rfq_cases SET current_revision = ?, current_quote_id = ?, current_quote_revision = ?, stage = ?, version = version + 1, updated_at = ? WHERE case_id = ? AND version = ?")
|
|
162
|
+
.run(revisionNo, input.quoteId ?? null, input.quoteRevision ?? null, stage, now, input.caseId, input.expectedVersion);
|
|
163
|
+
const updated = this.getCase(input.caseId);
|
|
164
|
+
if (updated.version !== current.version + 1) {
|
|
165
|
+
throw new RfqError("version_conflict", `询盘 ${input.caseId} 并发修改,revision 未创建`);
|
|
166
|
+
}
|
|
167
|
+
return {
|
|
168
|
+
case: updated,
|
|
169
|
+
revision: {
|
|
170
|
+
case_id: input.caseId,
|
|
171
|
+
revision: revisionNo,
|
|
172
|
+
fields: input.fields,
|
|
173
|
+
blockers: input.blockers,
|
|
174
|
+
source_ids: input.sourceIds,
|
|
175
|
+
created_at: now,
|
|
176
|
+
},
|
|
177
|
+
};
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
getCaseRevision(caseId, revision) {
|
|
181
|
+
const row = this.db
|
|
182
|
+
.prepare("SELECT * FROM rfq_case_revisions WHERE case_id = ? AND revision = ?")
|
|
183
|
+
.get(caseId, revision);
|
|
184
|
+
if (row === undefined)
|
|
185
|
+
return undefined;
|
|
186
|
+
return {
|
|
187
|
+
case_id: caseId,
|
|
188
|
+
revision,
|
|
189
|
+
fields: JSON.parse(String(row.fields_json)),
|
|
190
|
+
blockers: [],
|
|
191
|
+
source_ids: JSON.parse(String(row.source_ids_json)),
|
|
192
|
+
created_at: String(row.created_at),
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
/** 终态推进(CLOSED/CANCELLED):只能由操作者显式触发;未决审批候选由服务层失效。 */
|
|
196
|
+
closeCase(input) {
|
|
197
|
+
return this.tx(() => {
|
|
198
|
+
const current = this.assertCaseVersion(input.caseId, input.expectedVersion);
|
|
199
|
+
if (current.stage === "CLOSED" || current.stage === "CANCELLED") {
|
|
200
|
+
throw new RfqError("case_closed", `询盘 ${input.caseId} 已是终态 ${current.stage}`);
|
|
201
|
+
}
|
|
202
|
+
this.db
|
|
203
|
+
.prepare("UPDATE rfq_cases SET stage = ?, version = version + 1, updated_at = ? WHERE case_id = ? AND version = ?")
|
|
204
|
+
.run(input.stage, this.now(), input.caseId, input.expectedVersion);
|
|
205
|
+
return this.getCase(input.caseId);
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
/** current_quote 指针更新(版本替代/失效/回退 READY 时同步维护)。 */
|
|
209
|
+
updateCurrentQuote(input) {
|
|
210
|
+
return this.tx(() => this.updateCurrentQuoteInTx(input));
|
|
211
|
+
}
|
|
212
|
+
updateCurrentQuoteInTx(input) {
|
|
213
|
+
this.assertCaseVersion(input.caseId, input.expectedVersion);
|
|
214
|
+
this.db
|
|
215
|
+
.prepare("UPDATE rfq_cases SET current_quote_id = ?, current_quote_revision = ?, stage = COALESCE(?, stage), version = version + 1, updated_at = ? WHERE case_id = ? AND version = ?")
|
|
216
|
+
.run(input.quoteId, input.quoteRevision, input.stage ?? null, this.now(), input.caseId, input.expectedVersion);
|
|
217
|
+
return this.getCase(input.caseId);
|
|
218
|
+
}
|
|
219
|
+
// ---- 来源(内容摘要去重:仅同一 case 内) --------------------------------
|
|
220
|
+
createSource(input) {
|
|
221
|
+
const sha = `sha256:${createHash("sha256").update(input.content, "utf8").digest("hex")}`;
|
|
222
|
+
return this.tx(() => {
|
|
223
|
+
const existing = this.db
|
|
224
|
+
.prepare("SELECT * FROM rfq_sources WHERE case_id = ? AND content_sha256 = ?")
|
|
225
|
+
.get(input.caseId, sha);
|
|
226
|
+
if (existing !== undefined) {
|
|
227
|
+
return { source: this.sourceRow(existing), deduplicated: true };
|
|
228
|
+
}
|
|
229
|
+
const sourceId = this.id("src");
|
|
230
|
+
this.db
|
|
231
|
+
.prepare("INSERT INTO rfq_sources (source_id, case_id, merchant_id, kind, content_sha256, content, received_at, submitted_by, synthetic) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)")
|
|
232
|
+
.run(sourceId, input.caseId, this.merchantId, input.kind, sha, input.content, nowIsoOrThrow(input.receivedAt, "received_at"), input.submittedBy, input.synthetic ? 1 : 0);
|
|
233
|
+
return {
|
|
234
|
+
source: {
|
|
235
|
+
source_id: sourceId,
|
|
236
|
+
case_id: input.caseId,
|
|
237
|
+
merchant_id: this.merchantId,
|
|
238
|
+
kind: input.kind,
|
|
239
|
+
content_sha256: sha,
|
|
240
|
+
content: input.content,
|
|
241
|
+
received_at: input.receivedAt,
|
|
242
|
+
submitted_by: input.submittedBy,
|
|
243
|
+
synthetic: input.synthetic,
|
|
244
|
+
},
|
|
245
|
+
deduplicated: false,
|
|
246
|
+
};
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
sourceRow(row) {
|
|
250
|
+
return {
|
|
251
|
+
source_id: String(row.source_id),
|
|
252
|
+
case_id: String(row.case_id),
|
|
253
|
+
merchant_id: String(row.merchant_id),
|
|
254
|
+
kind: String(row.kind),
|
|
255
|
+
content_sha256: String(row.content_sha256),
|
|
256
|
+
content: String(row.content),
|
|
257
|
+
received_at: String(row.received_at),
|
|
258
|
+
submitted_by: String(row.submitted_by),
|
|
259
|
+
synthetic: Number(row.synthetic) === 1,
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
getSource(sourceId) {
|
|
263
|
+
const row = this.db
|
|
264
|
+
.prepare("SELECT * FROM rfq_sources WHERE source_id = ? AND merchant_id = ?")
|
|
265
|
+
.get(sourceId, this.merchantId);
|
|
266
|
+
return row === undefined ? undefined : this.sourceRow(row);
|
|
267
|
+
}
|
|
268
|
+
// ---- 事实快照 -----------------------------------------------------------
|
|
269
|
+
saveSnapshot(input) {
|
|
270
|
+
this.db
|
|
271
|
+
.prepare("INSERT INTO rfq_fact_snapshots (snapshot_id, case_id, case_revision, merchant_id, fields_json, content_fingerprint, complete, fetched_at, synthetic) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)")
|
|
272
|
+
.run(input.snapshotId, input.caseId, input.caseRevision, this.merchantId, canonicalJson(input.fields), input.contentFingerprint, input.complete ? 1 : 0, input.fetchedAt, input.synthetic ? 1 : 0);
|
|
273
|
+
}
|
|
274
|
+
getSnapshot(snapshotId) {
|
|
275
|
+
const row = this.db
|
|
276
|
+
.prepare("SELECT * FROM rfq_fact_snapshots WHERE snapshot_id = ? AND merchant_id = ?")
|
|
277
|
+
.get(snapshotId, this.merchantId);
|
|
278
|
+
if (row === undefined)
|
|
279
|
+
return undefined;
|
|
280
|
+
return {
|
|
281
|
+
snapshot_id: String(row.snapshot_id),
|
|
282
|
+
case_id: String(row.case_id),
|
|
283
|
+
case_revision: Number(row.case_revision),
|
|
284
|
+
fields_json: String(row.fields_json),
|
|
285
|
+
content_fingerprint: String(row.content_fingerprint),
|
|
286
|
+
fetched_at: String(row.fetched_at),
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
/** 询盘当前最新快照(refreshFacts 的指纹变化检测用)。 */
|
|
290
|
+
getLatestSnapshotForCase(caseId, excludeSnapshotId) {
|
|
291
|
+
const row = this.db
|
|
292
|
+
.prepare("SELECT snapshot_id, content_fingerprint FROM rfq_fact_snapshots WHERE case_id = ? AND merchant_id = ? AND snapshot_id <> ? ORDER BY fetched_at DESC, snapshot_id DESC LIMIT 1")
|
|
293
|
+
.get(caseId, this.merchantId, excludeSnapshotId);
|
|
294
|
+
return row === undefined
|
|
295
|
+
? undefined
|
|
296
|
+
: { snapshot_id: String(row.snapshot_id), content_fingerprint: String(row.content_fingerprint) };
|
|
297
|
+
}
|
|
298
|
+
// ---- 报价版本与事件 ------------------------------------------------------
|
|
299
|
+
createQuote(input) {
|
|
300
|
+
const digestInput = {
|
|
301
|
+
quote_id: input.quoteId,
|
|
302
|
+
revision: input.revision,
|
|
303
|
+
case_id: input.caseId,
|
|
304
|
+
case_revision: input.caseRevision,
|
|
305
|
+
pricing_input: input.pricingInput,
|
|
306
|
+
pricing_output: input.pricingOutput,
|
|
307
|
+
valid_until: input.validUntil,
|
|
308
|
+
fact_fingerprint: input.factFingerprint,
|
|
309
|
+
policy_version: input.policyVersion,
|
|
310
|
+
};
|
|
311
|
+
const digest = rfqContentDigest(digestInput);
|
|
312
|
+
const now = this.now();
|
|
313
|
+
this.db
|
|
314
|
+
.prepare(`INSERT INTO rfq_quote_revisions
|
|
315
|
+
(quote_id, revision, case_id, case_revision, merchant_id, snapshot_id, fact_fingerprint,
|
|
316
|
+
pricing_input_json, pricing_output_json, projection_json, content_digest, policy_version,
|
|
317
|
+
valid_until, status, created_at, updated_at)
|
|
318
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'VALIDATED', ?, ?)`)
|
|
319
|
+
.run(input.quoteId, input.revision, input.caseId, input.caseRevision, this.merchantId, input.snapshotId, input.factFingerprint, canonicalJson(input.pricingInput), canonicalJson(input.pricingOutput), canonicalJson(input.projection), digest, input.policyVersion, nowIsoOrThrow(input.validUntil, "valid_until"), now, now);
|
|
320
|
+
this.appendQuoteEvent({
|
|
321
|
+
quoteId: input.quoteId,
|
|
322
|
+
revision: input.revision,
|
|
323
|
+
event: "VALIDATED",
|
|
324
|
+
actor: input.actor,
|
|
325
|
+
reason: "确定性校验通过(创建即 VALIDATED;不可变内容 + 可重算输出)",
|
|
326
|
+
});
|
|
327
|
+
return this.getQuote(input.quoteId, input.revision);
|
|
328
|
+
}
|
|
329
|
+
appendQuoteEvent(input) {
|
|
330
|
+
this.db
|
|
331
|
+
.prepare("INSERT INTO rfq_quote_events (event_id, quote_id, revision, event, actor, reason, at) VALUES (?, ?, ?, ?, ?, ?, ?)")
|
|
332
|
+
.run(this.id("ev"), input.quoteId, input.revision, input.event, input.actor, input.reason ?? null, this.now());
|
|
333
|
+
}
|
|
334
|
+
getQuote(quoteId, revision) {
|
|
335
|
+
const row = this.db
|
|
336
|
+
.prepare("SELECT * FROM rfq_quote_revisions WHERE quote_id = ? AND revision = ? AND merchant_id = ?")
|
|
337
|
+
.get(quoteId, revision, this.merchantId);
|
|
338
|
+
if (row === undefined)
|
|
339
|
+
return undefined;
|
|
340
|
+
return this.quoteRow(row);
|
|
341
|
+
}
|
|
342
|
+
quoteRow(row) {
|
|
343
|
+
return {
|
|
344
|
+
quote_id: String(row.quote_id),
|
|
345
|
+
revision: Number(row.revision),
|
|
346
|
+
case_id: String(row.case_id),
|
|
347
|
+
case_revision: Number(row.case_revision),
|
|
348
|
+
merchant_id: String(row.merchant_id),
|
|
349
|
+
snapshot_id: String(row.snapshot_id),
|
|
350
|
+
fact_fingerprint: String(row.fact_fingerprint),
|
|
351
|
+
pricing_input_json: String(row.pricing_input_json),
|
|
352
|
+
pricing_output_json: String(row.pricing_output_json),
|
|
353
|
+
projection_json: String(row.projection_json),
|
|
354
|
+
content_digest: String(row.content_digest),
|
|
355
|
+
policy_version: String(row.policy_version),
|
|
356
|
+
valid_until: String(row.valid_until),
|
|
357
|
+
status: String(row.status),
|
|
358
|
+
status_reason: row.status_reason === null ? null : String(row.status_reason),
|
|
359
|
+
created_at: String(row.created_at),
|
|
360
|
+
updated_at: String(row.updated_at),
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
listQuotesForCase(caseId) {
|
|
364
|
+
const rows = this.db
|
|
365
|
+
.prepare("SELECT * FROM rfq_quote_revisions WHERE case_id = ? AND merchant_id = ? ORDER BY created_at, revision")
|
|
366
|
+
.all(caseId, this.merchantId);
|
|
367
|
+
return rows.map((r) => this.quoteRow(r));
|
|
368
|
+
}
|
|
369
|
+
listQuoteEvents(quoteId, revision) {
|
|
370
|
+
const rows = this.db
|
|
371
|
+
.prepare("SELECT * FROM rfq_quote_events WHERE quote_id = ? AND revision = ? ORDER BY at, event_id")
|
|
372
|
+
.all(quoteId, revision);
|
|
373
|
+
return rows.map((r) => ({
|
|
374
|
+
event_id: String(r.event_id),
|
|
375
|
+
quote_id: String(r.quote_id),
|
|
376
|
+
revision: Number(r.revision),
|
|
377
|
+
event: String(r.event),
|
|
378
|
+
actor: String(r.actor),
|
|
379
|
+
reason: r.reason === null ? null : String(r.reason),
|
|
380
|
+
at: String(r.at),
|
|
381
|
+
}));
|
|
382
|
+
}
|
|
383
|
+
/** 报价状态 CAS 转移:只允许 FROM 状态集合内的转移(独立事务)。 */
|
|
384
|
+
transitionQuote(input) {
|
|
385
|
+
return this.tx(() => this.transitionQuoteInTx(input));
|
|
386
|
+
}
|
|
387
|
+
/** 事务内版本(供 activateReleaseAtomic 等复合操作复用;不开新事务)。 */
|
|
388
|
+
transitionQuoteInTx(input) {
|
|
389
|
+
const current = this.getQuote(input.quoteId, input.revision);
|
|
390
|
+
if (current === undefined) {
|
|
391
|
+
throw new RfqError("not_found", `未知报价 ${input.quoteId}@${input.revision}`);
|
|
392
|
+
}
|
|
393
|
+
if (!input.from.includes(current.status)) {
|
|
394
|
+
throw new RfqError("approval_stale", `报价 ${input.quoteId}@${input.revision} 状态为 ${current.status},不能转移到 ${input.to}`);
|
|
395
|
+
}
|
|
396
|
+
const result = this.db
|
|
397
|
+
.prepare("UPDATE rfq_quote_revisions SET status = ?, status_reason = ?, updated_at = ? WHERE quote_id = ? AND revision = ? AND status = ?")
|
|
398
|
+
.run(input.to, input.reason ?? null, this.now(), input.quoteId, input.revision, current.status);
|
|
399
|
+
if (Number(result.changes) !== 1) {
|
|
400
|
+
throw new RfqError("approval_stale", `报价 ${input.quoteId}@${input.revision} 并发转移失败`);
|
|
401
|
+
}
|
|
402
|
+
this.appendQuoteEvent({
|
|
403
|
+
quoteId: input.quoteId,
|
|
404
|
+
revision: input.revision,
|
|
405
|
+
event: input.to,
|
|
406
|
+
actor: input.actor,
|
|
407
|
+
reason: input.reason,
|
|
408
|
+
});
|
|
409
|
+
return this.getQuote(input.quoteId, input.revision);
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* 版本替代:当前报价 → SUPERSEDED,并清空 case 指针。stage 由调用方给定
|
|
413
|
+
* (修订后的新阶段 / 事实失效回退 READY)——不硬编码,避免覆盖 revision
|
|
414
|
+
* 重算结果(v0.1.1 §8.1:stage 判定对象始终是 current_revision)。
|
|
415
|
+
*/
|
|
416
|
+
supersedeCurrentQuote(input) {
|
|
417
|
+
this.tx(() => {
|
|
418
|
+
const kase = this.assertCaseVersion(input.caseId, input.expectedVersion);
|
|
419
|
+
if (kase.current_quote_id !== null && kase.current_quote_revision !== null) {
|
|
420
|
+
const quote = this.getQuote(kase.current_quote_id, kase.current_quote_revision);
|
|
421
|
+
if (quote !== undefined && ["DRAFT", "VALIDATED", "PENDING_APPROVAL", "APPROVED"].includes(quote.status)) {
|
|
422
|
+
this.transitionQuoteInTx({
|
|
423
|
+
quoteId: quote.quote_id,
|
|
424
|
+
revision: quote.revision,
|
|
425
|
+
from: [quote.status],
|
|
426
|
+
to: "SUPERSEDED",
|
|
427
|
+
actor: input.actor,
|
|
428
|
+
reason: input.reason,
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
this.updateCurrentQuoteInTx({
|
|
433
|
+
caseId: input.caseId,
|
|
434
|
+
expectedVersion: kase.version,
|
|
435
|
+
quoteId: null,
|
|
436
|
+
quoteRevision: null,
|
|
437
|
+
stage: input.stage,
|
|
438
|
+
});
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
/** EXPIRED 惰性判定(服务端时钟;读取与前置校验时调用)。 */
|
|
442
|
+
expireDueQuote(quote, actor = "system") {
|
|
443
|
+
if (quote.status !== "PENDING_APPROVAL" && quote.status !== "APPROVED")
|
|
444
|
+
return quote;
|
|
445
|
+
if (Date.parse(quote.valid_until) > Date.parse(this.now()))
|
|
446
|
+
return quote;
|
|
447
|
+
return this.transitionQuote({
|
|
448
|
+
quoteId: quote.quote_id,
|
|
449
|
+
revision: quote.revision,
|
|
450
|
+
from: [quote.status],
|
|
451
|
+
to: "EXPIRED",
|
|
452
|
+
actor,
|
|
453
|
+
reason: "超过报价有效期(服务端时钟惰性判定)",
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
// ---- 发布与产物 ----------------------------------------------------------
|
|
457
|
+
saveArtifact(row) {
|
|
458
|
+
this.db
|
|
459
|
+
.prepare(`INSERT INTO rfq_artifacts
|
|
460
|
+
(artifact_id, merchant_id, quote_id, quote_revision, content_sha256, template_version, content_type, relative_path, activated, created_at)
|
|
461
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`)
|
|
462
|
+
.run(row.artifact_id, row.merchant_id, row.quote_id, row.quote_revision, row.content_sha256, row.template_version, row.content_type, row.relative_path, row.activated ? 1 : 0, row.created_at);
|
|
463
|
+
}
|
|
464
|
+
getArtifact(artifactId) {
|
|
465
|
+
const row = this.db
|
|
466
|
+
.prepare("SELECT * FROM rfq_artifacts WHERE artifact_id = ? AND merchant_id = ?")
|
|
467
|
+
.get(artifactId, this.merchantId);
|
|
468
|
+
if (row === undefined)
|
|
469
|
+
return undefined;
|
|
470
|
+
return {
|
|
471
|
+
artifact_id: String(row.artifact_id),
|
|
472
|
+
merchant_id: String(row.merchant_id),
|
|
473
|
+
quote_id: String(row.quote_id),
|
|
474
|
+
quote_revision: Number(row.quote_revision),
|
|
475
|
+
content_sha256: String(row.content_sha256),
|
|
476
|
+
template_version: String(row.template_version),
|
|
477
|
+
content_type: String(row.content_type),
|
|
478
|
+
relative_path: String(row.relative_path),
|
|
479
|
+
activated: Number(row.activated) === 1,
|
|
480
|
+
created_at: String(row.created_at),
|
|
481
|
+
};
|
|
482
|
+
}
|
|
483
|
+
activateArtifact(artifactId) {
|
|
484
|
+
const result = this.db
|
|
485
|
+
.prepare("UPDATE rfq_artifacts SET activated = 1 WHERE artifact_id = ? AND merchant_id = ?")
|
|
486
|
+
.run(artifactId, this.merchantId);
|
|
487
|
+
if (Number(result.changes) !== 1) {
|
|
488
|
+
throw new RfqError("not_found", `未知产物 ${artifactId}`);
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
/** 有效发布(未撤销/未停用)引用的产物相对路径——清理时必须保留的文件。 */
|
|
492
|
+
listValidReleaseArtifactPaths() {
|
|
493
|
+
const rows = this.db
|
|
494
|
+
.prepare(`SELECT DISTINCT a.relative_path AS relative_path
|
|
495
|
+
FROM rfq_artifacts a
|
|
496
|
+
JOIN rfq_release_requests r ON r.artifact_id = a.artifact_id AND r.merchant_id = a.merchant_id
|
|
497
|
+
WHERE a.merchant_id = ? AND r.status IN ('PENDING_APPROVAL', 'APPROVED', 'EXPORTED')`)
|
|
498
|
+
.all(this.merchantId);
|
|
499
|
+
return rows.map((r) => r.relative_path);
|
|
500
|
+
}
|
|
501
|
+
/**
|
|
502
|
+
* 删除无有效发布引用且未激活的过期产物行(文件由 artifacts store 回收;
|
|
503
|
+
* §10.4 临时产物 TTL)。返回删除行数。
|
|
504
|
+
*/
|
|
505
|
+
deleteUnreferencedUnactivatedArtifacts(cutoffIso, referencedPaths) {
|
|
506
|
+
const preserved = [...referencedPaths];
|
|
507
|
+
const referencedClause = preserved.length > 0
|
|
508
|
+
? ` AND relative_path NOT IN (${preserved.map(() => "?").join(", ")})`
|
|
509
|
+
: "";
|
|
510
|
+
const result = this.db
|
|
511
|
+
.prepare(`DELETE FROM rfq_artifacts
|
|
512
|
+
WHERE merchant_id = ? AND activated = 0 AND created_at < ?${referencedClause}`)
|
|
513
|
+
.run(this.merchantId, cutoffIso, ...preserved);
|
|
514
|
+
return Number(result.changes);
|
|
515
|
+
}
|
|
516
|
+
createRelease(row) {
|
|
517
|
+
this.db
|
|
518
|
+
.prepare(`INSERT INTO rfq_release_requests
|
|
519
|
+
(release_id, merchant_id, quote_id, quote_revision, candidate_id, artifact_id, artifact_sha256,
|
|
520
|
+
public_projection_digest, recipient_ref, policy_version, fact_fingerprint, status, created_at, updated_at)
|
|
521
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'PENDING_APPROVAL', ?, ?)`)
|
|
522
|
+
.run(row.release_id, row.merchant_id, row.quote_id, row.quote_revision, row.candidate_id, row.artifact_id, row.artifact_sha256, row.public_projection_digest, row.recipient_ref, row.policy_version, row.fact_fingerprint, row.created_at, row.created_at);
|
|
523
|
+
}
|
|
524
|
+
getRelease(releaseId) {
|
|
525
|
+
const row = this.db
|
|
526
|
+
.prepare("SELECT * FROM rfq_release_requests WHERE release_id = ? AND merchant_id = ?")
|
|
527
|
+
.get(releaseId, this.merchantId);
|
|
528
|
+
if (row === undefined)
|
|
529
|
+
return undefined;
|
|
530
|
+
return {
|
|
531
|
+
release_id: String(row.release_id),
|
|
532
|
+
merchant_id: String(row.merchant_id),
|
|
533
|
+
quote_id: String(row.quote_id),
|
|
534
|
+
quote_revision: Number(row.quote_revision),
|
|
535
|
+
candidate_id: String(row.candidate_id),
|
|
536
|
+
artifact_id: String(row.artifact_id),
|
|
537
|
+
artifact_sha256: String(row.artifact_sha256),
|
|
538
|
+
public_projection_digest: String(row.public_projection_digest),
|
|
539
|
+
recipient_ref: String(row.recipient_ref),
|
|
540
|
+
policy_version: String(row.policy_version),
|
|
541
|
+
fact_fingerprint: String(row.fact_fingerprint),
|
|
542
|
+
status: String(row.status),
|
|
543
|
+
created_at: String(row.created_at),
|
|
544
|
+
updated_at: String(row.updated_at),
|
|
545
|
+
};
|
|
546
|
+
}
|
|
547
|
+
transitionRelease(releaseId, from, to) {
|
|
548
|
+
return this.tx(() => {
|
|
549
|
+
const current = this.getRelease(releaseId);
|
|
550
|
+
if (current === undefined)
|
|
551
|
+
throw new RfqError("not_found", `未知发布 ${releaseId}`);
|
|
552
|
+
if (!from.includes(current.status)) {
|
|
553
|
+
throw new RfqError("approval_stale", `发布 ${releaseId} 状态为 ${current.status},不能转移到 ${to}`);
|
|
554
|
+
}
|
|
555
|
+
this.db
|
|
556
|
+
.prepare("UPDATE rfq_release_requests SET status = ?, updated_at = ? WHERE release_id = ? AND merchant_id = ? AND status = ?")
|
|
557
|
+
.run(to, this.now(), releaseId, this.merchantId, current.status);
|
|
558
|
+
return this.getRelease(releaseId);
|
|
559
|
+
});
|
|
560
|
+
}
|
|
561
|
+
/** 候选登记后回填 candidate_id(prepare 与命令记录同一请求内完成)。 */
|
|
562
|
+
updateReleaseCandidate(releaseId, candidateId) {
|
|
563
|
+
const result = this.db
|
|
564
|
+
.prepare("UPDATE rfq_release_requests SET candidate_id = ?, updated_at = ? WHERE release_id = ? AND merchant_id = ?")
|
|
565
|
+
.run(candidateId, this.now(), releaseId, this.merchantId);
|
|
566
|
+
if (Number(result.changes) !== 1)
|
|
567
|
+
throw new RfqError("not_found", `未知发布 ${releaseId}`);
|
|
568
|
+
}
|
|
569
|
+
listReleasesForQuote(quoteId, revision) {
|
|
570
|
+
const rows = this.db
|
|
571
|
+
.prepare("SELECT release_id FROM rfq_release_requests WHERE merchant_id = ? AND quote_id = ? AND quote_revision = ? ORDER BY created_at")
|
|
572
|
+
.all(this.merchantId, quoteId, revision);
|
|
573
|
+
return rows.map((r) => this.getRelease(r.release_id));
|
|
574
|
+
}
|
|
575
|
+
/** 移交包登记(批准后执行器写入;PACKET_READY 起步)。 */
|
|
576
|
+
saveHandoffPacket(input) {
|
|
577
|
+
const packet = JSON.parse(input.packetJson);
|
|
578
|
+
this.db
|
|
579
|
+
.prepare(`INSERT INTO rfq_handoffs
|
|
580
|
+
(handoff_id, merchant_id, quote_id, quote_revision, origin_kind, target_ref, intent_evidence_ref,
|
|
581
|
+
packet_digest, packet_json, status, receipt_json, recorded_by, created_at, updated_at)
|
|
582
|
+
VALUES (?, ?, ?, ?, 'manual_quote', ?, ?, ?, ?, 'PACKET_READY', NULL, ?, ?, ?)`)
|
|
583
|
+
.run(input.handoffId, this.merchantId, packet.origin.quote_id, packet.origin.revision, packet.target_ref, packet.intent_evidence_ref, input.digest, input.packetJson, input.actor, this.now(), this.now());
|
|
584
|
+
}
|
|
585
|
+
listReleasesByStatus(status) {
|
|
586
|
+
const rows = this.db
|
|
587
|
+
.prepare("SELECT * FROM rfq_release_requests WHERE merchant_id = ? AND status = ? ORDER BY created_at")
|
|
588
|
+
.all(this.merchantId, status);
|
|
589
|
+
return rows.map((r) => this.getRelease(String(r.release_id)));
|
|
590
|
+
}
|
|
591
|
+
/**
|
|
592
|
+
* 原子激活(§9.2 C;§10.2:候选、release、产物权限在同一写事务内):
|
|
593
|
+
* 报价 PENDING_APPROVAL → APPROVED、release → APPROVED、产物可下载。
|
|
594
|
+
* 任何一步失败整体回滚,绝不出现「批准已记录但文件不可下载」的中间态。
|
|
595
|
+
*/
|
|
596
|
+
activateReleaseAtomic(input) {
|
|
597
|
+
return this.tx(() => {
|
|
598
|
+
const release = this.getRelease(input.releaseId);
|
|
599
|
+
if (release === undefined)
|
|
600
|
+
throw new RfqError("not_found", `未知发布 ${input.releaseId}`);
|
|
601
|
+
const quote = this.getQuote(release.quote_id, release.quote_revision);
|
|
602
|
+
if (quote === undefined) {
|
|
603
|
+
throw new RfqError("not_found", `未知报价 ${release.quote_id}@${release.quote_revision}`);
|
|
604
|
+
}
|
|
605
|
+
if (quote.status !== "PENDING_APPROVAL") {
|
|
606
|
+
throw new RfqError("approval_stale", `报价状态为 ${quote.status},不是 PENDING_APPROVAL`);
|
|
607
|
+
}
|
|
608
|
+
// 事务内复查 case 终态与需求 revision(§8.1/§9.2 C:与外层校验构成防御纵深)。
|
|
609
|
+
const kase = this.getCase(quote.case_id);
|
|
610
|
+
if (kase === undefined) {
|
|
611
|
+
throw new RfqError("not_found", `未知询盘 ${quote.case_id}`);
|
|
612
|
+
}
|
|
613
|
+
if (kase.stage === "CLOSED" || kase.stage === "CANCELLED") {
|
|
614
|
+
throw new RfqError("case_closed", `询盘 ${quote.case_id} 已是终态 ${kase.stage};批准失效`);
|
|
615
|
+
}
|
|
616
|
+
if (kase.current_revision !== quote.case_revision) {
|
|
617
|
+
throw new RfqError("approval_stale", "需求已更新(case_revision 变化);旧批准失效");
|
|
618
|
+
}
|
|
619
|
+
// 前置重验:报价有效期(服务端时钟)。
|
|
620
|
+
if (Date.parse(quote.valid_until) <= Date.parse(this.now())) {
|
|
621
|
+
throw new RfqError("approval_stale", "报价已过有效期,批准失效");
|
|
622
|
+
}
|
|
623
|
+
this.transitionQuoteInTx({
|
|
624
|
+
quoteId: quote.quote_id,
|
|
625
|
+
revision: quote.revision,
|
|
626
|
+
from: ["PENDING_APPROVAL"],
|
|
627
|
+
to: "APPROVED",
|
|
628
|
+
actor: input.actor,
|
|
629
|
+
reason: "可信批准 + 一次性凭证消费 + 事实与策略重验通过",
|
|
630
|
+
});
|
|
631
|
+
const releaseUpdate = this.db
|
|
632
|
+
.prepare("UPDATE rfq_release_requests SET status = 'APPROVED', updated_at = ? WHERE release_id = ? AND merchant_id = ? AND status = 'PENDING_APPROVAL'")
|
|
633
|
+
.run(this.now(), input.releaseId, this.merchantId);
|
|
634
|
+
if (Number(releaseUpdate.changes) !== 1) {
|
|
635
|
+
// 理论不可达(同事务快照内 release 已读为 PENDING_APPROVAL);防御纵深。
|
|
636
|
+
throw new RfqError("approval_stale", `发布 ${input.releaseId} 并发转移失败`);
|
|
637
|
+
}
|
|
638
|
+
this.activateArtifact(release.artifact_id);
|
|
639
|
+
// current 指针指向已批准版本;stage → PRICED(判定对象=当前指针)。
|
|
640
|
+
this.updateCurrentQuoteInTx({
|
|
641
|
+
caseId: quote.case_id,
|
|
642
|
+
expectedVersion: input.caseExpectedVersion,
|
|
643
|
+
quoteId: quote.quote_id,
|
|
644
|
+
quoteRevision: quote.revision,
|
|
645
|
+
stage: "PRICED",
|
|
646
|
+
});
|
|
647
|
+
const after = this.getRelease(input.releaseId);
|
|
648
|
+
return { release: after, quote: this.getQuote(quote.quote_id, quote.revision) };
|
|
649
|
+
});
|
|
650
|
+
}
|
|
651
|
+
/** 下载记录(EXPORTED 唯一触发通道:可信管理页认证主体的成功取走)。 */
|
|
652
|
+
markQuoteExported(input) {
|
|
653
|
+
return this.transitionQuote({
|
|
654
|
+
quoteId: input.quoteId,
|
|
655
|
+
revision: input.revision,
|
|
656
|
+
from: ["APPROVED"],
|
|
657
|
+
to: "EXPORTED",
|
|
658
|
+
actor: input.actor,
|
|
659
|
+
reason: "可信管理页认证主体成功取走产物(摘要校验通过)",
|
|
660
|
+
});
|
|
661
|
+
}
|
|
662
|
+
// ---- 发送与移交 ----------------------------------------------------------
|
|
663
|
+
createDelivery(row) {
|
|
664
|
+
this.db
|
|
665
|
+
.prepare(`INSERT INTO rfq_delivery_records
|
|
666
|
+
(delivery_id, merchant_id, quote_id, quote_revision, release_id, status, channel, evidence_ref, recorded_by, recorded_at)
|
|
667
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`)
|
|
668
|
+
.run(row.delivery_id, row.merchant_id, row.quote_id, row.quote_revision, row.release_id, row.status, row.channel, row.evidence_ref, row.recorded_by, row.recorded_at);
|
|
669
|
+
return row;
|
|
670
|
+
}
|
|
671
|
+
createHandoff(row) {
|
|
672
|
+
this.db
|
|
673
|
+
.prepare(`INSERT INTO rfq_handoffs
|
|
674
|
+
(handoff_id, merchant_id, quote_id, quote_revision, origin_kind, target_ref, intent_evidence_ref,
|
|
675
|
+
packet_digest, packet_json, status, receipt_json, recorded_by, created_at, updated_at)
|
|
676
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`)
|
|
677
|
+
.run(row.handoff_id, row.merchant_id, row.quote_id, row.quote_revision, row.origin_kind, row.target_ref, row.intent_evidence_ref, row.packet_digest, row.packet_json, row.status, row.receipt_json, row.recorded_by, row.created_at, row.created_at);
|
|
678
|
+
return row;
|
|
679
|
+
}
|
|
680
|
+
getHandoff(handoffId) {
|
|
681
|
+
const row = this.db
|
|
682
|
+
.prepare("SELECT * FROM rfq_handoffs WHERE handoff_id = ? AND merchant_id = ?")
|
|
683
|
+
.get(handoffId, this.merchantId);
|
|
684
|
+
if (row === undefined)
|
|
685
|
+
return undefined;
|
|
686
|
+
return {
|
|
687
|
+
handoff_id: String(row.handoff_id),
|
|
688
|
+
merchant_id: String(row.merchant_id),
|
|
689
|
+
quote_id: String(row.quote_id),
|
|
690
|
+
quote_revision: Number(row.quote_revision),
|
|
691
|
+
origin_kind: String(row.origin_kind),
|
|
692
|
+
target_ref: String(row.target_ref),
|
|
693
|
+
intent_evidence_ref: String(row.intent_evidence_ref),
|
|
694
|
+
packet_digest: String(row.packet_digest),
|
|
695
|
+
packet_json: String(row.packet_json),
|
|
696
|
+
status: String(row.status),
|
|
697
|
+
receipt_json: row.receipt_json === null ? null : String(row.receipt_json),
|
|
698
|
+
recorded_by: String(row.recorded_by),
|
|
699
|
+
created_at: String(row.created_at),
|
|
700
|
+
updated_at: String(row.updated_at),
|
|
701
|
+
};
|
|
702
|
+
}
|
|
703
|
+
/** 移交状态推进(PACKET_READY → OWNER_RECORDED;其余通道后续接入)。 */
|
|
704
|
+
updateHandoffStatus(handoffId, status, actor) {
|
|
705
|
+
return this.tx(() => {
|
|
706
|
+
const current = this.getHandoff(handoffId);
|
|
707
|
+
if (current === undefined)
|
|
708
|
+
throw new RfqError("not_found", `未知移交 ${handoffId}`);
|
|
709
|
+
this.db
|
|
710
|
+
.prepare("UPDATE rfq_handoffs SET status = ?, recorded_by = ?, updated_at = ? WHERE handoff_id = ? AND merchant_id = ? AND status = ?")
|
|
711
|
+
.run(status, actor, this.now(), handoffId, this.merchantId, current.status);
|
|
712
|
+
return this.getHandoff(handoffId);
|
|
713
|
+
});
|
|
714
|
+
}
|
|
715
|
+
// ---- 幂等 ---------------------------------------------------------------
|
|
716
|
+
/**
|
|
717
|
+
* 幂等包装(§10.3):同键同请求摘要重放同一结果;同键不同摘要冲突。
|
|
718
|
+
* 请求摘要在执行前登记(防并发双执行);结果成功后回填。
|
|
719
|
+
*/
|
|
720
|
+
withIdempotency(input) {
|
|
721
|
+
if (input.key.trim() === "") {
|
|
722
|
+
throw new RfqError("validation", "idempotency_key 不能为空");
|
|
723
|
+
}
|
|
724
|
+
const digest = rfqContentDigest(input.request ?? null);
|
|
725
|
+
return this.tx(() => {
|
|
726
|
+
const existing = this.db
|
|
727
|
+
.prepare("SELECT request_digest, result_json FROM rfq_idempotency WHERE merchant_id = ? AND principal_id = ? AND operation = ? AND idem_key = ?")
|
|
728
|
+
.get(this.merchantId, input.principalId, input.operation, input.key);
|
|
729
|
+
if (existing !== undefined) {
|
|
730
|
+
if (existing.request_digest !== digest) {
|
|
731
|
+
throw new RfqError("idempotency_conflict", "同幂等键不同请求内容(IDEMPOTENCY_CONFLICT)");
|
|
732
|
+
}
|
|
733
|
+
if (existing.result_json === null) {
|
|
734
|
+
throw new RfqError("operation_unknown", "相同幂等键的请求仍在执行中;请查询原作业结果,禁止盲重放");
|
|
735
|
+
}
|
|
736
|
+
return { result: input.deserialize(existing.result_json), replayed: true };
|
|
737
|
+
}
|
|
738
|
+
this.db
|
|
739
|
+
.prepare("INSERT INTO rfq_idempotency (merchant_id, principal_id, operation, idem_key, request_digest, result_json, created_at) VALUES (?, ?, ?, ?, ?, NULL, ?)")
|
|
740
|
+
.run(this.merchantId, input.principalId, input.operation, input.key, digest, this.now());
|
|
741
|
+
const result = input.run();
|
|
742
|
+
this.db
|
|
743
|
+
.prepare("UPDATE rfq_idempotency SET result_json = ? WHERE merchant_id = ? AND principal_id = ? AND operation = ? AND idem_key = ?")
|
|
744
|
+
.run(JSON.stringify(input.serialize(result)), this.merchantId, input.principalId, input.operation, input.key);
|
|
745
|
+
return { result, replayed: false };
|
|
746
|
+
});
|
|
747
|
+
}
|
|
748
|
+
// ---- 作业 ----------------------------------------------------------------
|
|
749
|
+
/**
|
|
750
|
+
* 异步幂等(§10.3/§10.4):tombstone 先行登记(同键同摘要可继续,不同
|
|
751
|
+
* 摘要冲突,重复并发按「结果尚未确定」处理),异步工作完成后回填结果。
|
|
752
|
+
* 崩溃留下的 tombstone 无结果 = OPERATION_UNKNOWN——先查询原作业,禁止
|
|
753
|
+
* 盲重放。prepare_release / 激活类关键幂等不设 30 天清理(跟随报价保留)。
|
|
754
|
+
*/
|
|
755
|
+
async withIdempotencyAsync(input) {
|
|
756
|
+
if (input.key.trim() === "") {
|
|
757
|
+
throw new RfqError("validation", "idempotency_key 不能为空");
|
|
758
|
+
}
|
|
759
|
+
const digest = rfqContentDigest(input.request ?? null);
|
|
760
|
+
const existing = this.db
|
|
761
|
+
.prepare("SELECT request_digest, result_json FROM rfq_idempotency WHERE merchant_id = ? AND principal_id = ? AND operation = ? AND idem_key = ?")
|
|
762
|
+
.get(this.merchantId, input.principalId, input.operation, input.key);
|
|
763
|
+
if (existing !== undefined) {
|
|
764
|
+
if (existing.request_digest !== digest) {
|
|
765
|
+
throw new RfqError("idempotency_conflict", "同幂等键不同请求内容(IDEMPOTENCY_CONFLICT)");
|
|
766
|
+
}
|
|
767
|
+
if (existing.result_json === null) {
|
|
768
|
+
throw new RfqError("operation_unknown", "相同幂等键的请求尚未完成;请先查询原作业/发布状态,禁止盲重放");
|
|
769
|
+
}
|
|
770
|
+
return { result: input.deserialize(existing.result_json), replayed: true };
|
|
771
|
+
}
|
|
772
|
+
this.tx(() => {
|
|
773
|
+
// tx 内复查(并发窗口内第二个请求走冲突路径而非双执行)。
|
|
774
|
+
const again = this.db
|
|
775
|
+
.prepare("SELECT request_digest FROM rfq_idempotency WHERE merchant_id = ? AND principal_id = ? AND operation = ? AND idem_key = ?")
|
|
776
|
+
.get(this.merchantId, input.principalId, input.operation, input.key);
|
|
777
|
+
if (again !== undefined) {
|
|
778
|
+
throw new RfqError(again.request_digest === digest ? "operation_unknown" : "idempotency_conflict", again.request_digest === digest
|
|
779
|
+
? "相同幂等键的请求正在执行中;请先查询原作业结果"
|
|
780
|
+
: "同幂等键不同请求内容(IDEMPOTENCY_CONFLICT)");
|
|
781
|
+
}
|
|
782
|
+
this.db
|
|
783
|
+
.prepare("INSERT INTO rfq_idempotency (merchant_id, principal_id, operation, idem_key, request_digest, result_json, created_at) VALUES (?, ?, ?, ?, ?, NULL, ?)")
|
|
784
|
+
.run(this.merchantId, input.principalId, input.operation, input.key, digest, this.now());
|
|
785
|
+
});
|
|
786
|
+
let result;
|
|
787
|
+
try {
|
|
788
|
+
result = await input.run();
|
|
789
|
+
}
|
|
790
|
+
catch (err) {
|
|
791
|
+
// 确定性失败(run() 抛出):结果已确定为失败而非未知——清理 tombstone,
|
|
792
|
+
// 与同步路径语义一致(失败不留残键;同键可修正后重试)。进程崩溃留下的
|
|
793
|
+
// tombstone 无结果仍按 OPERATION_UNKNOWN 处理(§10.3)。
|
|
794
|
+
this.db
|
|
795
|
+
.prepare("DELETE FROM rfq_idempotency WHERE merchant_id = ? AND principal_id = ? AND operation = ? AND idem_key = ? AND result_json IS NULL")
|
|
796
|
+
.run(this.merchantId, input.principalId, input.operation, input.key);
|
|
797
|
+
throw err;
|
|
798
|
+
}
|
|
799
|
+
this.tx(() => {
|
|
800
|
+
this.db
|
|
801
|
+
.prepare("UPDATE rfq_idempotency SET result_json = ? WHERE merchant_id = ? AND principal_id = ? AND operation = ? AND idem_key = ?")
|
|
802
|
+
.run(JSON.stringify(input.serialize(result)), this.merchantId, input.principalId, input.operation, input.key);
|
|
803
|
+
});
|
|
804
|
+
return { result, replayed: false };
|
|
805
|
+
}
|
|
806
|
+
/**
|
|
807
|
+
* 幂等记录保留清理(§10.3:建议保留 30 天;prepare/激活类关键幂等跟随
|
|
808
|
+
* 对应报价保留策略,不清理)。返回删除条数。
|
|
809
|
+
*/
|
|
810
|
+
pruneIdempotency(input) {
|
|
811
|
+
const cutoff = new Date(Date.parse(this.now()) - input.olderThanDays * 86_400_000).toISOString();
|
|
812
|
+
const preserved = [...input.preserveOperations];
|
|
813
|
+
const placeholders = preserved.map(() => "?").join(", ");
|
|
814
|
+
const result = this.db
|
|
815
|
+
.prepare(`DELETE FROM rfq_idempotency WHERE created_at < ?${placeholders ? ` AND operation NOT IN (${placeholders})` : ""}`)
|
|
816
|
+
.run(cutoff, ...preserved);
|
|
817
|
+
return Number(result.changes);
|
|
818
|
+
}
|
|
819
|
+
createJob(operation) {
|
|
820
|
+
const jobId = this.id("job");
|
|
821
|
+
const now = this.now();
|
|
822
|
+
this.db
|
|
823
|
+
.prepare("INSERT INTO rfq_jobs (job_id, merchant_id, operation, status, progress, created_at, updated_at) VALUES (?, ?, ?, 'QUEUED', 0, ?, ?)")
|
|
824
|
+
.run(jobId, this.merchantId, operation, now, now);
|
|
825
|
+
return this.getJob(jobId);
|
|
826
|
+
}
|
|
827
|
+
markJob(jobId, status, patch) {
|
|
828
|
+
const result = this.db
|
|
829
|
+
.prepare("UPDATE rfq_jobs SET status = ?, progress = ?, error_code = ?, result_json = ?, checkpoint_json = ?, updated_at = ? WHERE job_id = ? AND merchant_id = ?")
|
|
830
|
+
.run(status, patch?.progress ?? 0, patch?.errorCode ?? null, patch?.result === undefined ? null : JSON.stringify(patch.result), patch?.checkpoint === undefined ? null : JSON.stringify(patch.checkpoint), this.now(), jobId, this.merchantId);
|
|
831
|
+
if (Number(result.changes) !== 1)
|
|
832
|
+
throw new RfqError("not_found", `未知作业 ${jobId}`);
|
|
833
|
+
return this.getJob(jobId);
|
|
834
|
+
}
|
|
835
|
+
getJob(jobId) {
|
|
836
|
+
const row = this.db
|
|
837
|
+
.prepare("SELECT * FROM rfq_jobs WHERE job_id = ? AND merchant_id = ?")
|
|
838
|
+
.get(jobId, this.merchantId);
|
|
839
|
+
if (row === undefined)
|
|
840
|
+
return undefined;
|
|
841
|
+
return {
|
|
842
|
+
job_id: String(row.job_id),
|
|
843
|
+
merchant_id: String(row.merchant_id),
|
|
844
|
+
operation: String(row.operation),
|
|
845
|
+
status: String(row.status),
|
|
846
|
+
progress: Number(row.progress),
|
|
847
|
+
checkpoint_json: row.checkpoint_json === null ? null : String(row.checkpoint_json),
|
|
848
|
+
error_code: row.error_code === null ? null : String(row.error_code),
|
|
849
|
+
result_json: row.result_json === null ? null : String(row.result_json),
|
|
850
|
+
created_at: String(row.created_at),
|
|
851
|
+
updated_at: String(row.updated_at),
|
|
852
|
+
};
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
//# sourceMappingURL=repository.js.map
|