@harrylabsj/kiwi 0.12.3 → 0.12.5
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/kiwi-contracts.lock.json +1 -1
- package/dist/a2a/client/client.d.ts +3 -1
- package/dist/a2a/client/client.js +23 -14
- package/dist/a2a/client/client.js.map +1 -1
- package/dist/a2a/client/types.d.ts +1 -1
- package/dist/a2a/client/url-policy.d.ts +2 -1
- package/dist/a2a/client/url-policy.js +5 -2
- package/dist/a2a/client/url-policy.js.map +1 -1
- package/dist/a2a/negotiate.js +68 -27
- package/dist/a2a/negotiate.js.map +1 -1
- package/dist/a2a/node.d.ts +5 -1
- package/dist/a2a/node.js +68 -30
- package/dist/a2a/node.js.map +1 -1
- package/dist/a2a/server/merchant-handler.d.ts +4 -6
- package/dist/a2a/server/merchant-handler.js +679 -484
- package/dist/a2a/server/merchant-handler.js.map +1 -1
- package/dist/a2a/server/merchant-product-facts.d.ts +32 -0
- package/dist/a2a/server/merchant-product-facts.js +57 -0
- package/dist/a2a/server/merchant-product-facts.js.map +1 -0
- package/dist/a2a/server/pipeline.d.ts +1 -0
- package/dist/a2a/server/pipeline.js +48 -10
- package/dist/a2a/server/pipeline.js.map +1 -1
- package/dist/a2a/task/poller.d.ts +3 -0
- package/dist/a2a/task/poller.js +3 -0
- package/dist/a2a/task/poller.js.map +1 -1
- package/dist/agent/buyer/buyer-tools.d.ts +1 -1
- package/dist/agent/buyer/buyer-tools.js +322 -4
- package/dist/agent/buyer/buyer-tools.js.map +1 -1
- package/dist/agent/buyer/scheduler.d.ts +3 -0
- package/dist/agent/buyer/scheduler.js +25 -5
- package/dist/agent/buyer/scheduler.js.map +1 -1
- package/dist/agent/buyer/search-loop.js +6 -2
- package/dist/agent/buyer/search-loop.js.map +1 -1
- package/dist/agent/buyer/task-store.d.ts +12 -0
- package/dist/agent/buyer/task-store.js +101 -14
- package/dist/agent/buyer/task-store.js.map +1 -1
- package/dist/agent/kernel.d.ts +11 -0
- package/dist/agent/kernel.js +345 -273
- package/dist/agent/kernel.js.map +1 -1
- package/dist/agent/kiwi-tool.d.ts +11 -12
- package/dist/agent/merchant/action-candidate.d.ts +7 -0
- package/dist/agent/merchant/action-candidate.js +54 -9
- package/dist/agent/merchant/action-candidate.js.map +1 -1
- package/dist/agent/merchant/types.d.ts +4 -9
- package/dist/agent/merchant/types.js +14 -0
- package/dist/agent/merchant/types.js.map +1 -1
- package/dist/agent/negotiation-chat.js +36 -8
- package/dist/agent/negotiation-chat.js.map +1 -1
- package/dist/agent/session.js +8 -0
- package/dist/agent/session.js.map +1 -1
- package/dist/agent/write-gate.d.ts +4 -0
- package/dist/agent/write-gate.js +22 -20
- package/dist/agent/write-gate.js.map +1 -1
- package/dist/buyer-core/a2a-knp.d.ts +2 -8
- package/dist/buyer-core/a2a-knp.js +16 -3
- package/dist/buyer-core/a2a-knp.js.map +1 -1
- package/dist/buyer-core/a2a-negotiator.d.ts +26 -19
- package/dist/buyer-core/a2a-negotiator.js +105 -38
- package/dist/buyer-core/a2a-negotiator.js.map +1 -1
- package/dist/buyer-core/a2a-quote-fetcher.d.ts +17 -3
- package/dist/buyer-core/a2a-quote-fetcher.js +141 -57
- package/dist/buyer-core/a2a-quote-fetcher.js.map +1 -1
- package/dist/buyer-core/build-service.d.ts +2 -0
- package/dist/buyer-core/build-service.js +28 -2
- package/dist/buyer-core/build-service.js.map +1 -1
- package/dist/buyer-core/errors.js +1 -1
- package/dist/buyer-core/negotiator.d.ts +0 -15
- package/dist/buyer-core/negotiator.js +3 -0
- package/dist/buyer-core/negotiator.js.map +1 -1
- package/dist/buyer-core/product-summary.d.ts +4 -0
- package/dist/buyer-core/product-summary.js +6 -0
- package/dist/buyer-core/product-summary.js.map +1 -1
- package/dist/buyer-core/protocol-recovery.d.ts +78 -0
- package/dist/buyer-core/protocol-recovery.js +368 -0
- package/dist/buyer-core/protocol-recovery.js.map +1 -0
- package/dist/buyer-core/receive-gate.d.ts +11 -0
- package/dist/buyer-core/receive-gate.js +107 -0
- package/dist/buyer-core/receive-gate.js.map +1 -0
- package/dist/buyer-core/service.d.ts +53 -2
- package/dist/buyer-core/service.js +584 -74
- package/dist/buyer-core/service.js.map +1 -1
- package/dist/buyer-core/store.d.ts +27 -2
- package/dist/buyer-core/store.js +131 -26
- package/dist/buyer-core/store.js.map +1 -1
- package/dist/cli.js +208 -21
- package/dist/cli.js.map +1 -1
- package/dist/cloud/binding/enrollment-challenge.d.ts +1 -0
- package/dist/cloud/binding/enrollment-challenge.js +84 -33
- package/dist/cloud/binding/enrollment-challenge.js.map +1 -1
- package/dist/cloud/binding/enrollment-heartbeat.d.ts +2 -0
- package/dist/cloud/binding/enrollment-heartbeat.js +6 -2
- package/dist/cloud/binding/enrollment-heartbeat.js.map +1 -1
- package/dist/cloud/binding/proofs.d.ts +5 -0
- package/dist/cloud/binding/proofs.js +14 -1
- package/dist/cloud/binding/proofs.js.map +1 -1
- package/dist/cloud/binding/runtime-challenge.d.ts +2 -0
- package/dist/cloud/binding/runtime-challenge.js +20 -8
- package/dist/cloud/binding/runtime-challenge.js.map +1 -1
- package/dist/cloud/binding/store-lock.d.ts +13 -0
- package/dist/cloud/binding/store-lock.js +77 -0
- package/dist/cloud/binding/store-lock.js.map +1 -0
- package/dist/cloud/bootstrap.d.ts +4 -0
- package/dist/cloud/bootstrap.js +74 -57
- package/dist/cloud/bootstrap.js.map +1 -1
- package/dist/cloud/catalog-client.d.ts +4 -0
- package/dist/cloud/catalog-client.js +106 -29
- package/dist/cloud/catalog-client.js.map +1 -1
- package/dist/cloud/connect-service.d.ts +6 -0
- package/dist/cloud/connect-service.js +360 -120
- package/dist/cloud/connect-service.js.map +1 -1
- package/dist/cloud/file-listing-publication.js +6 -0
- package/dist/cloud/file-listing-publication.js.map +1 -1
- package/dist/cloud/main.d.ts +3 -0
- package/dist/cloud/main.js +47 -11
- package/dist/cloud/main.js.map +1 -1
- package/dist/cloud/onboarding/catalog-pipeline.d.ts +32 -1
- package/dist/cloud/onboarding/catalog-pipeline.js +80 -19
- package/dist/cloud/onboarding/catalog-pipeline.js.map +1 -1
- package/dist/cloud/onboarding/store.js +12 -4
- package/dist/cloud/onboarding/store.js.map +1 -1
- package/dist/cloud/product-import-tabular.d.ts +1 -1
- package/dist/cloud/product-import-tabular.js +275 -36
- package/dist/cloud/product-import-tabular.js.map +1 -1
- package/dist/cloud/product-source.d.ts +6 -1
- package/dist/cloud/product-source.js +47 -15
- package/dist/cloud/product-source.js.map +1 -1
- package/dist/commerce/data-source.d.ts +4 -0
- package/dist/commerce/data-source.js.map +1 -1
- package/dist/commerce/http-client.d.ts +30 -1
- package/dist/commerce/http-client.js +11 -7
- package/dist/commerce/http-client.js.map +1 -1
- package/dist/commerce/shopping-cli-source.js +4 -0
- package/dist/commerce/shopping-cli-source.js.map +1 -1
- package/dist/commerce/types.d.ts +2 -1
- package/dist/commerce/types.js.map +1 -1
- package/dist/counterparty/a2a-direct/index.js +2 -20
- package/dist/counterparty/a2a-direct/index.js.map +1 -1
- package/dist/counterparty/channel.d.ts +2 -1
- package/dist/counterparty/channel.js.map +1 -1
- package/dist/counterparty/shopping-cli-hosted/index.js +2 -24
- package/dist/counterparty/shopping-cli-hosted/index.js.map +1 -1
- package/dist/demo/demo-runner.d.ts +1 -1
- package/dist/demo/demo-runner.js +3 -6
- package/dist/demo/demo-runner.js.map +1 -1
- package/dist/discovery/catalog-source/cloud-card.d.ts +4 -3
- package/dist/discovery/catalog-source/cloud-card.js +28 -25
- package/dist/discovery/catalog-source/cloud-card.js.map +1 -1
- package/dist/discovery/catalog-source/kiwi-source.d.ts +7 -19
- package/dist/discovery/catalog-source/kiwi-source.js +19 -12
- package/dist/discovery/catalog-source/kiwi-source.js.map +1 -1
- package/dist/discovery/catalog-source/register.js +22 -12
- package/dist/discovery/catalog-source/register.js.map +1 -1
- package/dist/discovery/catalog-source/source.d.ts +3 -17
- package/dist/discovery/catalog-source/source.js +11 -8
- package/dist/discovery/catalog-source/source.js.map +1 -1
- package/dist/discovery/catalog-source/types.d.ts +3 -2
- package/dist/discovery/catalog-source/types.js +0 -15
- package/dist/discovery/catalog-source/types.js.map +1 -1
- package/dist/discovery/merchant-subscriptions.d.ts +4 -0
- package/dist/discovery/merchant-subscriptions.js +72 -23
- package/dist/discovery/merchant-subscriptions.js.map +1 -1
- package/dist/discovery/resolve.d.ts +7 -3
- package/dist/discovery/resolve.js +62 -30
- package/dist/discovery/resolve.js.map +1 -1
- package/dist/discovery/ucp/resolver.d.ts +3 -17
- package/dist/discovery/ucp/resolver.js +9 -3
- package/dist/discovery/ucp/resolver.js.map +1 -1
- package/dist/fanout/disclosure.js +27 -10
- package/dist/fanout/disclosure.js.map +1 -1
- package/dist/fanout/orchestrator.js +38 -24
- package/dist/fanout/orchestrator.js.map +1 -1
- package/dist/fs/atomic-write.d.ts +11 -0
- package/dist/fs/atomic-write.js +67 -10
- package/dist/fs/atomic-write.js.map +1 -1
- package/dist/handoff/index.d.ts +2 -1
- package/dist/handoff/index.js +1 -0
- package/dist/handoff/index.js.map +1 -1
- package/dist/handoff/launch-marketplace-policy.d.ts +149 -0
- package/dist/handoff/launch-marketplace-policy.js +301 -0
- package/dist/handoff/launch-marketplace-policy.js.map +1 -0
- package/dist/handoff/marketplace-product-link.d.ts +18 -0
- package/dist/handoff/marketplace-product-link.js +71 -0
- package/dist/handoff/marketplace-product-link.js.map +1 -0
- package/dist/handoff/order-record.js +25 -3
- package/dist/handoff/order-record.js.map +1 -1
- package/dist/handoff/transaction.d.ts +30 -0
- package/dist/handoff/transaction.js +64 -0
- package/dist/handoff/transaction.js.map +1 -1
- package/dist/handoff/ucp-checkout/channel.js +2 -1
- package/dist/handoff/ucp-checkout/channel.js.map +1 -1
- package/dist/handoff/ucp-checkout/client.d.ts +1 -0
- package/dist/handoff/ucp-checkout/client.js +48 -37
- package/dist/handoff/ucp-checkout/client.js.map +1 -1
- package/dist/handoff/url-safety.d.ts +3 -0
- package/dist/handoff/url-safety.js +7 -0
- package/dist/handoff/url-safety.js.map +1 -1
- package/dist/http/bounded-ip-rate-limit.d.ts +10 -0
- package/dist/http/bounded-ip-rate-limit.js +37 -0
- package/dist/http/bounded-ip-rate-limit.js.map +1 -0
- package/dist/http/cli.js +23 -0
- package/dist/http/cli.js.map +1 -1
- package/dist/http/merchant-feed-api.js +8 -1
- package/dist/http/merchant-feed-api.js.map +1 -1
- package/dist/http/merchant-management/api.js +20 -1
- package/dist/http/merchant-management/api.js.map +1 -1
- package/dist/http/merchant-management/page.js +176 -45
- package/dist/http/merchant-management/page.js.map +1 -1
- package/dist/http/merchant-management/reconciliation-worker.js +5 -2
- package/dist/http/merchant-management/reconciliation-worker.js.map +1 -1
- package/dist/http/merchant-management/trusted-page.js +1 -1
- package/dist/http/merchant-management/trusted-page.js.map +1 -1
- package/dist/http/merchant-server.js +2 -1
- package/dist/http/merchant-server.js.map +1 -1
- package/dist/http/server.d.ts +15 -0
- package/dist/http/server.js +107 -9
- package/dist/http/server.js.map +1 -1
- package/dist/i18n.d.ts +1 -0
- package/dist/i18n.js +3 -0
- package/dist/i18n.js.map +1 -1
- package/dist/lifecycle/close.d.ts +24 -0
- package/dist/lifecycle/close.js +131 -0
- package/dist/lifecycle/close.js.map +1 -0
- package/dist/mcp/merchant-rfq-tools.d.ts +6 -4
- package/dist/mcp/merchant-rfq-tools.js +2 -0
- package/dist/mcp/merchant-rfq-tools.js.map +1 -1
- package/dist/mcp/merchant-runtime-assembly.js +7 -22
- package/dist/mcp/merchant-runtime-assembly.js.map +1 -1
- package/dist/mcp/merchant-server.d.ts +0 -22
- package/dist/mcp/merchant-server.js +25 -2
- package/dist/mcp/merchant-server.js.map +1 -1
- package/dist/mcp/tools.js +17 -16
- package/dist/mcp/tools.js.map +1 -1
- package/dist/mcp/types.d.ts +1 -1
- package/dist/mcp/types.js +1 -1
- package/dist/merchant/ai-runtime/config.d.ts +99 -0
- package/dist/merchant/ai-runtime/config.js +187 -0
- package/dist/merchant/ai-runtime/config.js.map +1 -0
- package/dist/merchant/ai-runtime/gate.d.ts +142 -0
- package/dist/merchant/ai-runtime/gate.js +343 -0
- package/dist/merchant/ai-runtime/gate.js.map +1 -0
- package/dist/merchant/ai-runtime/owner-authoritative-receipts.d.ts +5 -0
- package/dist/merchant/ai-runtime/owner-authoritative-receipts.js +53 -0
- package/dist/merchant/ai-runtime/owner-authoritative-receipts.js.map +1 -0
- package/dist/merchant/ai-runtime/owner-budget-gate.d.ts +10 -0
- package/dist/merchant/ai-runtime/owner-budget-gate.js +37 -0
- package/dist/merchant/ai-runtime/owner-budget-gate.js.map +1 -0
- package/dist/merchant/ai-runtime/owner-business-config.d.ts +33 -0
- package/dist/merchant/ai-runtime/owner-business-config.js +93 -0
- package/dist/merchant/ai-runtime/owner-business-config.js.map +1 -0
- package/dist/merchant/ai-runtime/owner-business-host.d.ts +24 -0
- package/dist/merchant/ai-runtime/owner-business-host.js +114 -0
- package/dist/merchant/ai-runtime/owner-business-host.js.map +1 -0
- package/dist/merchant/ai-runtime/owner-business-proof-contract.d.ts +26 -0
- package/dist/merchant/ai-runtime/owner-business-proof-contract.js +2 -0
- package/dist/merchant/ai-runtime/owner-business-proof-contract.js.map +1 -0
- package/dist/merchant/ai-runtime/owner-committed-proof.d.ts +55 -0
- package/dist/merchant/ai-runtime/owner-committed-proof.js +141 -0
- package/dist/merchant/ai-runtime/owner-committed-proof.js.map +1 -0
- package/dist/merchant/ai-runtime/owner-factory-config.d.ts +30 -0
- package/dist/merchant/ai-runtime/owner-factory-config.js +99 -0
- package/dist/merchant/ai-runtime/owner-factory-config.js.map +1 -0
- package/dist/merchant/ai-runtime/owner-file-grant.d.ts +71 -0
- package/dist/merchant/ai-runtime/owner-file-grant.js +118 -0
- package/dist/merchant/ai-runtime/owner-file-grant.js.map +1 -0
- package/dist/merchant/ai-runtime/owner-local-readonly-host.d.ts +17 -0
- package/dist/merchant/ai-runtime/owner-local-readonly-host.js +116 -0
- package/dist/merchant/ai-runtime/owner-local-readonly-host.js.map +1 -0
- package/dist/merchant/ai-runtime/owner-operation-reader.d.ts +24 -0
- package/dist/merchant/ai-runtime/owner-operation-reader.js +62 -0
- package/dist/merchant/ai-runtime/owner-operation-reader.js.map +1 -0
- package/dist/merchant/ai-runtime/owner-provider-guard.d.ts +9 -0
- package/dist/merchant/ai-runtime/owner-provider-guard.js +127 -0
- package/dist/merchant/ai-runtime/owner-provider-guard.js.map +1 -0
- package/dist/merchant/ai-runtime/owner-readonly-facts.d.ts +26 -0
- package/dist/merchant/ai-runtime/owner-readonly-facts.js +72 -0
- package/dist/merchant/ai-runtime/owner-readonly-facts.js.map +1 -0
- package/dist/merchant/ai-runtime/owner-sdk-tools.d.ts +43 -0
- package/dist/merchant/ai-runtime/owner-sdk-tools.js +83 -0
- package/dist/merchant/ai-runtime/owner-sdk-tools.js.map +1 -0
- package/dist/merchant/ai-runtime/owner-session.d.ts +341 -0
- package/dist/merchant/ai-runtime/owner-session.js +1280 -0
- package/dist/merchant/ai-runtime/owner-session.js.map +1 -0
- package/dist/merchant/ai-runtime/owner-storage-admission.d.ts +35 -0
- package/dist/merchant/ai-runtime/owner-storage-admission.js +324 -0
- package/dist/merchant/ai-runtime/owner-storage-admission.js.map +1 -0
- package/dist/merchant/ai-runtime/owner-trusted-factory.d.ts +44 -0
- package/dist/merchant/ai-runtime/owner-trusted-factory.js +185 -0
- package/dist/merchant/ai-runtime/owner-trusted-factory.js.map +1 -0
- package/dist/merchant/ai-runtime/sqlite-budget-store.d.ts +68 -0
- package/dist/merchant/ai-runtime/sqlite-budget-store.js +335 -0
- package/dist/merchant/ai-runtime/sqlite-budget-store.js.map +1 -0
- package/dist/merchant/ai-runtime/status.d.ts +64 -0
- package/dist/merchant/ai-runtime/status.js +56 -0
- package/dist/merchant/ai-runtime/status.js.map +1 -0
- package/dist/merchant/application/service.d.ts +5 -1
- package/dist/merchant/application/service.js +7 -8
- package/dist/merchant/application/service.js.map +1 -1
- package/dist/merchant/grant-store.js +9 -4
- package/dist/merchant/grant-store.js.map +1 -1
- package/dist/merchant/promotion-store.js +1 -0
- package/dist/merchant/promotion-store.js.map +1 -1
- package/dist/merchant/stats-store.d.ts +3 -0
- package/dist/merchant/stats-store.js +28 -8
- package/dist/merchant/stats-store.js.map +1 -1
- package/dist/merchant/workbench-service.d.ts +1 -0
- package/dist/merchant/workbench-service.js +28 -11
- package/dist/merchant/workbench-service.js.map +1 -1
- package/dist/merchant-core/commands.d.ts +3 -0
- package/dist/merchant-core/commands.js +39 -2
- package/dist/merchant-core/commands.js.map +1 -1
- package/dist/merchant-core/executor.d.ts +2 -0
- package/dist/merchant-core/executor.js +1 -1
- package/dist/merchant-core/executor.js.map +1 -1
- package/dist/merchant-core/operations.d.ts +2 -2
- package/dist/merchant-core/operations.js +11 -9
- package/dist/merchant-core/operations.js.map +1 -1
- package/dist/merchant-core/product-import.js +1 -1
- package/dist/merchant-core/product-import.js.map +1 -1
- package/dist/merchant-core/rfq/data-source-adapter.d.ts +2 -10
- package/dist/merchant-core/rfq/data-source-adapter.js +21 -9
- package/dist/merchant-core/rfq/data-source-adapter.js.map +1 -1
- package/dist/merchant-core/rfq/pending-candidates.d.ts +14 -0
- package/dist/merchant-core/rfq/pending-candidates.js +11 -0
- package/dist/merchant-core/rfq/pending-candidates.js.map +1 -0
- package/dist/merchant-core/rfq/release-coordinator.d.ts +9 -2
- package/dist/merchant-core/rfq/release-coordinator.js +32 -14
- package/dist/merchant-core/rfq/release-coordinator.js.map +1 -1
- package/dist/merchant-core/rfq/repository.d.ts +4 -7
- package/dist/merchant-core/rfq/repository.js +10 -24
- package/dist/merchant-core/rfq/repository.js.map +1 -1
- package/dist/merchant-core/rfq/service.d.ts +5 -2
- package/dist/merchant-core/rfq/service.js +4 -3
- package/dist/merchant-core/rfq/service.js.map +1 -1
- package/dist/merchant-gateway/entry-server.js +25 -1
- package/dist/merchant-gateway/entry-server.js.map +1 -1
- package/dist/merchant-gateway/pinned-fetch.d.ts +9 -2
- package/dist/merchant-gateway/pinned-fetch.js +68 -29
- package/dist/merchant-gateway/pinned-fetch.js.map +1 -1
- package/dist/merchant-runtime/manager.d.ts +5 -2
- package/dist/merchant-runtime/manager.js +40 -11
- package/dist/merchant-runtime/manager.js.map +1 -1
- package/dist/negotiation/context-map/store.js +4 -13
- package/dist/negotiation/context-map/store.js.map +1 -1
- package/dist/negotiation/domain/common.js +33 -2
- package/dist/negotiation/domain/common.js.map +1 -1
- package/dist/negotiation/domain/envelope.d.ts +2 -5
- package/dist/negotiation/domain/envelope.js +36 -9
- package/dist/negotiation/domain/envelope.js.map +1 -1
- package/dist/negotiation/domain/objects.js +14 -0
- package/dist/negotiation/domain/objects.js.map +1 -1
- package/dist/negotiation/idempotency/store.d.ts +4 -1
- package/dist/negotiation/idempotency/store.js +80 -52
- package/dist/negotiation/idempotency/store.js.map +1 -1
- package/dist/negotiation/idempotency/types.d.ts +7 -1
- package/dist/negotiation/idempotency/types.js +1 -1
- package/dist/negotiation/idempotency/types.js.map +1 -1
- package/dist/negotiation/jcs.js +11 -16
- package/dist/negotiation/jcs.js.map +1 -1
- package/dist/negotiation/ledger/store.d.ts +15 -9
- package/dist/negotiation/ledger/store.js +201 -65
- package/dist/negotiation/ledger/store.js.map +1 -1
- package/dist/negotiation/recovery/recover.js +30 -30
- package/dist/negotiation/recovery/recover.js.map +1 -1
- package/dist/negotiation/recovery/types.d.ts +3 -3
- package/dist/net/request-budget.d.ts +17 -0
- package/dist/net/request-budget.js +63 -0
- package/dist/net/request-budget.js.map +1 -0
- package/dist/operator/controller.d.ts +1 -1
- package/dist/operator/controller.js +23 -12
- package/dist/operator/controller.js.map +1 -1
- package/dist/operator/runner.d.ts +69 -0
- package/dist/operator/runner.js +260 -39
- package/dist/operator/runner.js.map +1 -1
- package/dist/operator/strategy.d.ts +2 -0
- package/dist/operator/strategy.js +11 -0
- package/dist/operator/strategy.js.map +1 -1
- package/dist/privacy/workbench-retention.js +28 -10
- package/dist/privacy/workbench-retention.js.map +1 -1
- package/dist/product-init.js +25 -4
- package/dist/product-init.js.map +1 -1
- package/dist/product-publish.js +3 -102
- package/dist/product-publish.js.map +1 -1
- package/dist/runtime/foreground.d.ts +1 -1
- package/dist/runtime/foreground.js +3 -0
- package/dist/runtime/foreground.js.map +1 -1
- package/dist/runtime/model.js +4 -0
- package/dist/runtime/model.js.map +1 -1
- package/dist/supervisor/manage.js +109 -35
- package/dist/supervisor/manage.js.map +1 -1
- package/dist/trust/identity/jws.js +46 -5
- package/dist/trust/identity/jws.js.map +1 -1
- package/dist/trust/identity/message-signature.js +15 -2
- package/dist/trust/identity/message-signature.js.map +1 -1
- package/dist/trust/records/store.d.ts +8 -1
- package/dist/trust/records/store.js +68 -6
- package/dist/trust/records/store.js.map +1 -1
- package/dist/weixin/channel.d.ts +4 -0
- package/dist/weixin/channel.js +17 -3
- package/dist/weixin/channel.js.map +1 -1
- package/dist/weixin/cli-weixin.js +8 -2
- package/dist/weixin/cli-weixin.js.map +1 -1
- package/package.json +30 -7
- package/scripts/check-install-toolchain.mjs +93 -0
|
@@ -13,6 +13,18 @@
|
|
|
13
13
|
* See the License for the specific language governing permissions and
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
|
+
/**
|
|
17
|
+
* 生产 merchant A2A handler(`kiwi agent serve` 用)——脚本化 KNP 磋商:
|
|
18
|
+
*
|
|
19
|
+
* rfq/inquiry → offer;offer → counter_offer(还价到 DEAL 价);
|
|
20
|
+
* counter_offer → conditional_offer;clarification → 文字澄清应答;
|
|
21
|
+
* accept_nonbinding → agreement artifact(三副作用 flag 恒 false);
|
|
22
|
+
* withdraw/decline/cancel → 文字确认。
|
|
23
|
+
*
|
|
24
|
+
* 每个出站回复落 merchant 侧 Ledger(append-only,§22)。这是 `kiwi agent serve`
|
|
25
|
+
* 的确定性 merchant 行为;未来可替换为 LLM 驱动的 Negotiation Engine(同一 handler 接缝)。
|
|
26
|
+
*/
|
|
27
|
+
import { checkedProductStock, checkedSourceEvidence, quoteFactsStillValid, recoverQuoteSourceSnapshot } from "./merchant-product-facts.js";
|
|
16
28
|
import { LedgerStore } from "../../negotiation/ledger/index.js";
|
|
17
29
|
import { newAgreementId, newExchangeId, newMessageId, newOfferId, } from "../../negotiation/domain/identifiers.js";
|
|
18
30
|
import { finalizeEnvelope } from "../../negotiation/domain/envelope.js";
|
|
@@ -22,6 +34,7 @@ import { toMinorUnits as losslessToMinorUnits } from "../../protocol/legacy-shop
|
|
|
22
34
|
import { WORKBENCH_CURRENCY_TABLE_VERSION } from "../../merchant/application/money.js";
|
|
23
35
|
import { calculateWorkbenchQuote } from "../../merchant/quote-calculator.js";
|
|
24
36
|
import { createNegotiationPhase, isTerminalPhase, transitionPhase, } from "../../negotiation/state/phase.js";
|
|
37
|
+
import { NegotiationValidationError } from "../../negotiation/domain/common.js";
|
|
25
38
|
export const MERCHANT_CAPABILITY = "com.harrylabsj.kiwi.shopping.negotiation";
|
|
26
39
|
export const MERCHANT_SKU = "SKU-001";
|
|
27
40
|
export const MERCHANT_CURRENCY = "CNY";
|
|
@@ -34,6 +47,25 @@ export const MERCHANT_QUANTITY = 200;
|
|
|
34
47
|
* 省略 delivery_before——明确未知,绝不报静态/过期日期)。固定日期常量已从
|
|
35
48
|
* 生产 terms 路径移除。
|
|
36
49
|
*/
|
|
50
|
+
/**
|
|
51
|
+
* review 2-3(A319 返修):把商家自产的无条件 offer/counter_offer 归一为
|
|
52
|
+
* evaluateConditionalOffer 的**合法形状**——`conditions` 必须存在(数组,
|
|
53
|
+
* 无条件即空数组)。此前归一对象缺 conditions,accept 路径 evaluator 的
|
|
54
|
+
* conditions.forEach 直接 TypeError(RPC -32603),阻断直报价/还价接受、
|
|
55
|
+
* 错 digest 拒绝与 Ledger 重启恢复(A317 独验 4 红同根因)。
|
|
56
|
+
* 真实 conditional_offer(自带 conditions)**不经此归一**,原样登记。
|
|
57
|
+
*/
|
|
58
|
+
function normalizeDirectOfferAsConditional(offerId, rawTerms, quantity) {
|
|
59
|
+
return {
|
|
60
|
+
conditional: {
|
|
61
|
+
type: "conditional_offer",
|
|
62
|
+
offer_id: offerId,
|
|
63
|
+
base_terms: rawTerms,
|
|
64
|
+
conditions: [],
|
|
65
|
+
},
|
|
66
|
+
quantity,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
37
69
|
export function resolveDeliveryBefore(merchantPolicy, now) {
|
|
38
70
|
const leadDays = merchantPolicy?.delivery_lead_days;
|
|
39
71
|
if (leadDays === undefined)
|
|
@@ -65,8 +97,13 @@ export function dataSourceProductSource(dataSource) {
|
|
|
65
97
|
if (fact === undefined || fact.price_minor === undefined || fact.currency === undefined) {
|
|
66
98
|
throw new Error(`product source has no price for SKU ${sku}`);
|
|
67
99
|
}
|
|
100
|
+
const extended = fact;
|
|
68
101
|
return {
|
|
102
|
+
sku: fact.sku,
|
|
69
103
|
price: fact.price_minor / 100,
|
|
104
|
+
...(extended.unit !== undefined ? { unit: extended.unit } : {}),
|
|
105
|
+
...(extended.valid_until !== undefined ? { valid_until: extended.valid_until } : {}),
|
|
106
|
+
...(extended.source_evidence !== undefined ? { source_evidence: extended.source_evidence } : {}),
|
|
70
107
|
currency: fact.currency,
|
|
71
108
|
...(fact.title !== undefined ? { title: fact.title } : {}),
|
|
72
109
|
...(fact.stock !== undefined ? { stock: fact.stock } : {}),
|
|
@@ -101,9 +138,9 @@ function offerTerms(opts, now) {
|
|
|
101
138
|
return {
|
|
102
139
|
items: [
|
|
103
140
|
{
|
|
104
|
-
sku: opts.sku
|
|
105
|
-
quantity: { value: opts.quantity, unit:
|
|
106
|
-
unit_price: { currency: opts.currency
|
|
141
|
+
sku: opts.sku,
|
|
142
|
+
quantity: { value: opts.quantity, unit: opts.unit },
|
|
143
|
+
unit_price: { currency: opts.currency, amount_minor: opts.priceMinor },
|
|
107
144
|
},
|
|
108
145
|
],
|
|
109
146
|
// V2 §8.5 P0-1:只报权威交期(delivery_lead_days 动态计算);未配置则省略。
|
|
@@ -183,6 +220,7 @@ export function createMerchantHandler(options) {
|
|
|
183
220
|
const policyOf = () => typeof options.merchantPolicy === "function"
|
|
184
221
|
? options.merchantPolicy()
|
|
185
222
|
: options.merchantPolicy;
|
|
223
|
+
const quoteSources = new Map();
|
|
186
224
|
const conditionalByNegotiation = new Map();
|
|
187
225
|
// 审查 P2-D:终态(AGREEMENT_REACHED / WITHDRAWN / DECLINED / CANCELLED)
|
|
188
226
|
// 不得以同一 negotiation_id 重开(§17.4/§21.2)——运行时此前无任何终态
|
|
@@ -193,6 +231,25 @@ export function createMerchantHandler(options) {
|
|
|
193
231
|
// conditionalByNegotiation 隐式表达,规范转换表不是接收消息的权威门)。
|
|
194
232
|
const phaseStateByNegotiation = new Map();
|
|
195
233
|
// 产生商业承诺的动作:终态后一律拒绝;text-only(clarification)放行。
|
|
234
|
+
const activeObjectByNegotiation = new Map();
|
|
235
|
+
const boundPeers = new Map();
|
|
236
|
+
const requestTails = new Map();
|
|
237
|
+
const serialize = async (key, fn) => {
|
|
238
|
+
const before = requestTails.get(key) ?? Promise.resolve();
|
|
239
|
+
let release;
|
|
240
|
+
const turn = new Promise((resolve) => { release = resolve; });
|
|
241
|
+
const tail = before.then(() => turn);
|
|
242
|
+
requestTails.set(key, tail);
|
|
243
|
+
await before;
|
|
244
|
+
try {
|
|
245
|
+
return await fn();
|
|
246
|
+
}
|
|
247
|
+
finally {
|
|
248
|
+
release();
|
|
249
|
+
if (requestTails.get(key) === tail)
|
|
250
|
+
requestTails.delete(key);
|
|
251
|
+
}
|
|
252
|
+
};
|
|
196
253
|
const COMMERCIAL_ACTIONS = new Set([
|
|
197
254
|
"inquiry",
|
|
198
255
|
"rfq",
|
|
@@ -200,6 +257,9 @@ export function createMerchantHandler(options) {
|
|
|
200
257
|
"counter_offer",
|
|
201
258
|
"conditional_offer",
|
|
202
259
|
"accept_nonbinding",
|
|
260
|
+
"withdraw",
|
|
261
|
+
"decline",
|
|
262
|
+
"cancel",
|
|
203
263
|
]);
|
|
204
264
|
/** 审查 BUG-10:把入站 action 映射到相位机事件并推进;非法转换(不在
|
|
205
265
|
* 转换表内的组合)fail-closed 返回 false(调用方 decline state_conflict)。
|
|
@@ -275,7 +335,7 @@ export function createMerchantHandler(options) {
|
|
|
275
335
|
state_transition: { from_phase: effectiveFrom, to_phase: toPhase },
|
|
276
336
|
identity: {
|
|
277
337
|
sender_identity: sender,
|
|
278
|
-
counterparty_identity: counterparty,
|
|
338
|
+
counterparty_identity: boundPeers.get(negotiationId) ?? counterparty,
|
|
279
339
|
actor: "merchant",
|
|
280
340
|
},
|
|
281
341
|
capability: { capability: MERCHANT_CAPABILITY, protocol_version: "1.0" },
|
|
@@ -288,7 +348,6 @@ export function createMerchantHandler(options) {
|
|
|
288
348
|
// 单调增长且价目陈旧)。
|
|
289
349
|
const PRICE_CACHE_TTL_MS = 10 * 60 * 1000;
|
|
290
350
|
const priceBySku = new Map();
|
|
291
|
-
/** 从真实商品源解析 SKU 价目;源不可用/查不到时回退演示价并返回注记。 */
|
|
292
351
|
const resolveProduct = async (sku, resolveOptions = {}) => {
|
|
293
352
|
const cached = priceBySku.get(sku);
|
|
294
353
|
// force=true(成交前重验,T047)绕过缓存:许可有效性必须按**当前**商品事实
|
|
@@ -305,10 +364,18 @@ export function createMerchantHandler(options) {
|
|
|
305
364
|
// 静默改写为 1999/2000 进入报价(P2-P 已修数据源路径,此处是生产
|
|
306
365
|
// merchant handler 的剩余舍入点)。无法精确表达 → fail-closed
|
|
307
366
|
// (抛错 → resolveProduct 回退演示价并注记)。
|
|
367
|
+
if (product.sku !== undefined && product.sku !== sku)
|
|
368
|
+
throw new TypeError("Source SKU mismatch");
|
|
308
369
|
const converted = losslessToMinorUnits(product.price, 2);
|
|
309
370
|
if (!converted.lossless) {
|
|
310
371
|
throw new Error(`商品源价格 ${product.price} 超出 ${MERCHANT_CURRENCY} 两位小数精度(lossy)`);
|
|
311
372
|
}
|
|
373
|
+
const stock = checkedProductStock(product.stock);
|
|
374
|
+
const evidence = checkedSourceEvidence(product.source_evidence);
|
|
375
|
+
if (converted.amount_minor < 0 || typeof product.currency !== "string" || product.currency.trim() === "")
|
|
376
|
+
throw new TypeError("Invalid source price/currency");
|
|
377
|
+
if (product.unit !== undefined && (typeof product.unit !== "string" || product.unit.trim() === ""))
|
|
378
|
+
throw new TypeError("Invalid source unit");
|
|
312
379
|
const resolved = {
|
|
313
380
|
priceMinor: converted.amount_minor,
|
|
314
381
|
currency: product.currency,
|
|
@@ -316,7 +383,9 @@ export function createMerchantHandler(options) {
|
|
|
316
383
|
...(product.handoff_destination !== undefined
|
|
317
384
|
? { handoff_destination: product.handoff_destination }
|
|
318
385
|
: {}),
|
|
319
|
-
...(
|
|
386
|
+
...(stock !== undefined ? { stock } : {}),
|
|
387
|
+
...(product.unit !== undefined ? { unit: product.unit } : {}),
|
|
388
|
+
...(evidence !== undefined ? { source_evidence: evidence } : {}),
|
|
320
389
|
...(product.title !== undefined ? { title: product.title } : {}),
|
|
321
390
|
...(product.valid_until !== undefined ? { valid_until: product.valid_until } : {}),
|
|
322
391
|
};
|
|
@@ -335,11 +404,14 @@ export function createMerchantHandler(options) {
|
|
|
335
404
|
return {
|
|
336
405
|
priceMinor: offerPriceMinor,
|
|
337
406
|
currency: MERCHANT_CURRENCY,
|
|
338
|
-
|
|
407
|
+
unit: "piece",
|
|
408
|
+
note: `商品源不可用(${sku}),本回合使用 synthetic 演示价;库存待确认`,
|
|
339
409
|
};
|
|
340
410
|
}
|
|
341
411
|
}
|
|
342
|
-
|
|
412
|
+
if (!allowDemoPriceFallback)
|
|
413
|
+
throw new ProductSourceUnavailableError(sku, new Error("No product source"));
|
|
414
|
+
return { priceMinor: offerPriceMinor, currency: MERCHANT_CURRENCY, unit: "piece", note: "synthetic demo; needs_confirmation" };
|
|
343
415
|
};
|
|
344
416
|
/** resolveProduct 的 fail-closed 包装:商品源不可用且未开 demo 回退时返回
|
|
345
417
|
* null(调用方 decline temporarily_unavailable),否则返回解析结果。 */
|
|
@@ -404,7 +476,7 @@ export function createMerchantHandler(options) {
|
|
|
404
476
|
endsAt: value.endsAt,
|
|
405
477
|
};
|
|
406
478
|
};
|
|
407
|
-
const appendSent = async (reply) => {
|
|
479
|
+
const appendSent = async (reply, snapshot) => {
|
|
408
480
|
await ledger.append({
|
|
409
481
|
event_kind: "message_sent",
|
|
410
482
|
negotiation_id: reply.negotiation_id,
|
|
@@ -413,7 +485,7 @@ export function createMerchantHandler(options) {
|
|
|
413
485
|
in_reply_to: reply.in_reply_to,
|
|
414
486
|
identity: {
|
|
415
487
|
sender_identity: sender,
|
|
416
|
-
counterparty_identity: counterparty,
|
|
488
|
+
counterparty_identity: boundPeers.get(reply.negotiation_id) ?? counterparty,
|
|
417
489
|
actor: reply.actor,
|
|
418
490
|
},
|
|
419
491
|
capability: {
|
|
@@ -422,9 +494,17 @@ export function createMerchantHandler(options) {
|
|
|
422
494
|
},
|
|
423
495
|
wire_digest: reply.digest,
|
|
424
496
|
wire_payload: reply,
|
|
425
|
-
outcome: { kind: "ok" },
|
|
497
|
+
outcome: { kind: "ok", ...(snapshot !== undefined ? { result: { merchant_quote_source: { offer_id: String(reply.payload.offer_id), snapshot } } } : {}) },
|
|
426
498
|
occurred_at: reply.created_at,
|
|
427
499
|
});
|
|
500
|
+
if (snapshot !== undefined)
|
|
501
|
+
quoteSources.set(reply.negotiation_id, { offer_id: String(reply.payload.offer_id), snapshot });
|
|
502
|
+
if (["offer", "counter_offer", "conditional_offer"].includes(reply.action)) {
|
|
503
|
+
activeObjectByNegotiation.set(reply.negotiation_id, {
|
|
504
|
+
offerId: String(reply.payload.offer_id),
|
|
505
|
+
messageId: reply.message_id, actor: reply.actor,
|
|
506
|
+
});
|
|
507
|
+
}
|
|
428
508
|
};
|
|
429
509
|
// 审查 BUG-03:从持久 Ledger 恢复状态(必须在 return 之前执行)——重启后
|
|
430
510
|
// 终态不得重开、已发出的 conditional offer 必须继续可被接受(此前
|
|
@@ -452,27 +532,52 @@ export function createMerchantHandler(options) {
|
|
|
452
532
|
}
|
|
453
533
|
if (event.event_kind !== "message_sent")
|
|
454
534
|
continue;
|
|
535
|
+
if (event.identity.sender_identity === sender && event.outcome.kind === "ok") {
|
|
536
|
+
const saved = event.outcome.result?.merchant_quote_source;
|
|
537
|
+
const snapshot = recoverQuoteSourceSnapshot(saved?.snapshot);
|
|
538
|
+
if (snapshot !== undefined && typeof saved?.offer_id === "string")
|
|
539
|
+
quoteSources.set(negotiationId, { offer_id: saved.offer_id, snapshot });
|
|
540
|
+
}
|
|
455
541
|
const envelope = event.wire_payload;
|
|
456
542
|
if (envelope?.action === "offer" ||
|
|
457
543
|
envelope?.action === "counter_offer" ||
|
|
458
544
|
envelope?.action === "conditional_offer") {
|
|
459
545
|
const offerId = envelope.payload?.offer_id;
|
|
460
|
-
if (typeof offerId === "string" && offerId !== "")
|
|
546
|
+
if (typeof offerId === "string" && offerId !== "") {
|
|
461
547
|
activeOfferId = offerId;
|
|
548
|
+
if (event.message_id !== undefined)
|
|
549
|
+
activeObjectByNegotiation.set(negotiationId, {
|
|
550
|
+
offerId, messageId: event.message_id, actor: "merchant",
|
|
551
|
+
});
|
|
552
|
+
}
|
|
462
553
|
}
|
|
554
|
+
// review 2-3(A316 校准):重启恢复同样登记普通 offer/counter_offer——
|
|
555
|
+
// 归一为 {type:"conditional_offer", offer_id, base_terms}(与在途登记
|
|
556
|
+
// 同形),accept 按 offer_id 强匹配;只恢复 conditional 的旧行为使
|
|
557
|
+
// 重启后直报价/还价接受仍 offer_unknown。
|
|
463
558
|
if (envelope?.action === "conditional_offer") {
|
|
464
559
|
const payload = envelope.payload;
|
|
465
560
|
if (payload?.offer_id !== undefined) {
|
|
466
561
|
lastConditional = {
|
|
467
562
|
conditional: envelope.payload,
|
|
468
|
-
quantity: payload.base_terms?.items?.[0]?.quantity?.value ??
|
|
563
|
+
quantity: payload.base_terms?.items?.[0]?.quantity?.value ?? Number.NaN,
|
|
469
564
|
};
|
|
470
565
|
}
|
|
471
566
|
}
|
|
567
|
+
else if (envelope?.action === "offer" || envelope?.action === "counter_offer") {
|
|
568
|
+
const payload = envelope.payload;
|
|
569
|
+
const rawTerms = envelope.action === "offer" ? payload?.terms : payload?.proposed_terms;
|
|
570
|
+
if (payload?.offer_id !== undefined && payload.offer_id !== "" && rawTerms !== undefined) {
|
|
571
|
+
// review 2-3(A319 返修):Ledger 恢复归一与在途登记同形(含
|
|
572
|
+
// conditions: [])——重启后直报价/还价接受不再 TypeError。
|
|
573
|
+
lastConditional = normalizeDirectOfferAsConditional(payload.offer_id, rawTerms, rawTerms.items?.[0]
|
|
574
|
+
?.quantity?.value ?? Number.NaN);
|
|
575
|
+
}
|
|
576
|
+
}
|
|
472
577
|
}
|
|
473
578
|
// 审查 P1-07(跨重启残留,独立验收发现):终态相位同样必须恢复——相位
|
|
474
579
|
// 取自链上 state_transition 事实(非臆造)。否则 cancel/withdraw/decline
|
|
475
|
-
//
|
|
580
|
+
// 重启后若不恢复终态会让 advancePhase 从全新 OPEN 状态推进:
|
|
476
581
|
// 重放的 cancel 被当作新动作接受,并落 from_phase 伪造为 OPEN 的幻影
|
|
477
582
|
// 终态转换。恢复后由相位机 fail-closed(state_conflict),与进程内一致。
|
|
478
583
|
phaseStateByNegotiation.set(negotiationId, {
|
|
@@ -489,6 +594,7 @@ export function createMerchantHandler(options) {
|
|
|
489
594
|
});
|
|
490
595
|
if (isTerminalPhase(phase)) {
|
|
491
596
|
closedNegotiations.add(negotiationId);
|
|
597
|
+
quoteSources.delete(negotiationId);
|
|
492
598
|
continue; // 终态后 conditional 已删除,不恢复
|
|
493
599
|
}
|
|
494
600
|
if (lastConditional !== undefined) {
|
|
@@ -498,495 +604,584 @@ export function createMerchantHandler(options) {
|
|
|
498
604
|
return {
|
|
499
605
|
name: "kiwi-agent-serve-merchant",
|
|
500
606
|
async handle(ctx) {
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
607
|
+
return serialize(ctx.envelope.negotiation_id, async () => {
|
|
608
|
+
const envelope = ctx.envelope;
|
|
609
|
+
const negotiationId = envelope.negotiation_id;
|
|
610
|
+
const inReplyTo = envelope.message_id;
|
|
611
|
+
const binding = ledger.ensureCounterpartyBinding({
|
|
612
|
+
negotiationId, localIdentity: sender, senderIdentity: ctx.senderIdentity, actor: envelope.actor,
|
|
613
|
+
});
|
|
614
|
+
if (binding === "mismatch")
|
|
615
|
+
return { kind: "declined", reasonCode: "authorization_failed" };
|
|
616
|
+
if (binding === "unresolved")
|
|
617
|
+
return {
|
|
618
|
+
kind: "error", protocolCode: "reconciliation_required", message: "legacy negotiation owner is not proven",
|
|
619
|
+
};
|
|
620
|
+
boundPeers.set(negotiationId, ctx.senderIdentity);
|
|
621
|
+
// 审查 P2-D:终态不得以同一 negotiation_id 重开(§17.4/§21.2)。
|
|
622
|
+
if (closedNegotiations.has(negotiationId) && COMMERCIAL_ACTIONS.has(envelope.action)) {
|
|
516
623
|
return declineReply("state_conflict");
|
|
517
624
|
}
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
const invalidQuantity = (raw) => raw !== undefined && (typeof raw !== "number" || !Number.isInteger(raw) || raw <= 0);
|
|
528
|
-
const floorMinorForSku = (sku) => {
|
|
529
|
-
const mp = policyOf();
|
|
530
|
-
const floorValue = mp?.price_floors?.[sku] ?? mp?.min_unit_price_private;
|
|
531
|
-
if (floorValue === undefined)
|
|
532
|
-
return 0;
|
|
533
|
-
const conv = losslessToMinorUnits(floorValue, 2);
|
|
534
|
-
return conv.lossless ? conv.amount_minor : null;
|
|
535
|
-
};
|
|
536
|
-
/** 权威交期(V2 §8.5 P0-1):配置了 merchant_policy.delivery_lead_days 才
|
|
537
|
-
* 报具体 delivery_before(报价时间+天数,动态计算不过期);未配置 → undefined,
|
|
538
|
-
* terms 省略 delivery_before(明确未知)。 */
|
|
539
|
-
const deliveryBefore = () => resolveDeliveryBefore(policyOf(), now());
|
|
540
|
-
/** 买家还价(major→minor;KNP 里 buyer counter 的 unit_price)。 */
|
|
541
|
-
const clampToBounds = (minor, floor, list) => Math.min(list, Math.max(minor, floor));
|
|
542
|
-
/**
|
|
543
|
-
* 对买家可见的**自动折扣边界**(公开策略值:max_auto_discount_percent)。
|
|
544
|
-
*
|
|
545
|
-
* 安全要点(T045「不泄露底价」):商家对买家的任何报价都必须 clamp 到
|
|
546
|
-
* **公开边界**而不是 clamp 到私有底价——把还价 clamp 到底价再回给买家,
|
|
547
|
-
* 等于一次极低还价即可探出私有底价(一次探测即泄露精确值)。
|
|
548
|
-
* 私有底价只用来判断「这个价能不能自动报」,绝不出现在返回的条款里。
|
|
549
|
-
*/
|
|
550
|
-
/**
|
|
551
|
-
* 运行中规则的摘要(T047):接受成交前必须重验——商家改了规则/促销/底价后,
|
|
552
|
-
* 旧 conditional 的许可即失效,不能按旧价成交(设计 §10.2「审批时商品/规则
|
|
553
|
-
* 已变化 → 使旧候选失效或重新确认,不能执行过期许可」)。
|
|
554
|
-
*/
|
|
555
|
-
const policyDigest = () => contentDigest((policyOf() ?? {}));
|
|
556
|
-
/** 商品事实指纹(T047):价格/币种变化同样使旧许可失效。 */
|
|
557
|
-
const productFingerprint = (facts) => contentDigest({
|
|
558
|
-
sku: facts.sku,
|
|
559
|
-
priceMinor: facts.priceMinor,
|
|
560
|
-
currency: facts.currency,
|
|
561
|
-
});
|
|
562
|
-
const publicDiscountBoundMinor = (listMinor, sku) => {
|
|
563
|
-
const policy = policyOf();
|
|
564
|
-
const autoRaw = policy?.max_auto_discount_percent;
|
|
565
|
-
const auto = typeof autoRaw === "number" && Number.isFinite(autoRaw) && autoRaw > 0 ? autoRaw : 0;
|
|
566
|
-
// 促销本身是**公开条款**(promos[sku].bulk_discount_percent),因此公开边界
|
|
567
|
-
// 取其与自动折扣的较大者;未公布任何折扣时边界 = list(不自动折扣)。
|
|
568
|
-
const promoPct = policy?.promos?.[sku]?.bulk_discount_percent ?? 0;
|
|
569
|
-
const pct = Math.max(auto, Number.isFinite(promoPct) && promoPct > 0 ? promoPct : 0);
|
|
570
|
-
return applyDiscountPercentMinor(listMinor, pct);
|
|
571
|
-
};
|
|
572
|
-
/**
|
|
573
|
-
* 商品事实闸门(T046):数据过期或数量超过可得库存 → 明确不可报价。
|
|
574
|
-
* 设计 §10.1:「数据到期后停止相关自动报价或注明需人工确认,不把库存未知
|
|
575
|
-
* 说成有货」。**不在这里做任何兜底定价**(演示价已在配置层被云端拒绝)。
|
|
576
|
-
*/
|
|
577
|
-
const factsUnusable = (facts, quantity) => {
|
|
578
|
-
if (facts.valid_until !== undefined) {
|
|
579
|
-
const until = Date.parse(facts.valid_until);
|
|
580
|
-
if (!Number.isFinite(until) || until < Date.parse(now()))
|
|
581
|
-
return true;
|
|
582
|
-
}
|
|
583
|
-
if (facts.stock !== undefined && quantity > facts.stock)
|
|
584
|
-
return true;
|
|
585
|
-
return false;
|
|
586
|
-
};
|
|
587
|
-
switch (envelope.action) {
|
|
588
|
-
case "inquiry": {
|
|
589
|
-
// 入站消息由 A2A pipeline 统一落 message_received(§22);这里不再
|
|
590
|
-
// 重复落账——此前 appendSent(envelope) 把买家的 inquiry 记为 merchant
|
|
591
|
-
// 自己"发送"(sender=merchant),审计语义错位且重复。
|
|
592
|
-
const resolved = await resolveProductOrDecline(MERCHANT_SKU);
|
|
593
|
-
if ("declined" in resolved)
|
|
594
|
-
return resolved.declined;
|
|
595
|
-
return textReply(`We carry ${resolved.product.title ?? MERCHANT_SKU} at ${(resolved.product.priceMinor / 100).toFixed(2)} ${resolved.product.currency}/piece; ask for delivery details.`);
|
|
596
|
-
}
|
|
597
|
-
case "rfq": {
|
|
598
|
-
const payload = envelope.payload;
|
|
599
|
-
const sku = payload.items?.[0]?.sku ?? MERCHANT_SKU;
|
|
600
|
-
const quantityRaw = payload.items?.[0]?.quantity?.value;
|
|
601
|
-
if (invalidQuantity(quantityRaw))
|
|
602
|
-
return declineReply("schema_invalid");
|
|
603
|
-
const quantity = quantityRaw ?? MERCHANT_QUANTITY;
|
|
604
|
-
const floorMinor = floorMinorForSku(sku);
|
|
605
|
-
if (floorMinor === null)
|
|
606
|
-
return declineReply("temporarily_unavailable");
|
|
607
|
-
const resolved = await resolveProductOrDecline(sku);
|
|
608
|
-
if ("declined" in resolved)
|
|
609
|
-
return resolved.declined;
|
|
610
|
-
const product = resolved.product;
|
|
611
|
-
const { priceMinor, currency, note, handoff_destination } = product;
|
|
612
|
-
if (factsUnusable(product, quantity))
|
|
613
|
-
return declineReply("temporarily_unavailable");
|
|
614
|
-
const promotion = resolvePromotionQuote(sku, quantity, priceMinor, currency, floorMinor);
|
|
615
|
-
if (promotion.kind === "unavailable")
|
|
616
|
-
return declineReply("temporarily_unavailable");
|
|
617
|
-
if (promotion.kind === "invalid")
|
|
618
|
-
return declineReply("approval_required");
|
|
619
|
-
// 确定性:offer = list 价(公开价)。list < floor 是配置矛盾——此时
|
|
620
|
-
// 用 floor 兜底会把私有底价直接报给买家(T045 泄露面),因此拒绝自动报价。
|
|
621
|
-
const effectivePriceMinor = promotion.kind === "applied" ? promotion.priceMinor : priceMinor;
|
|
622
|
-
if (effectivePriceMinor < floorMinor)
|
|
623
|
-
return declineReply("approval_required");
|
|
624
|
-
const reply = seedEnvelope({
|
|
625
|
-
negotiation_id: negotiationId,
|
|
626
|
-
in_reply_to: inReplyTo,
|
|
627
|
-
actor: "merchant",
|
|
628
|
-
action: "offer",
|
|
629
|
-
created_at: now(),
|
|
630
|
-
payload: {
|
|
631
|
-
type: "offer",
|
|
632
|
-
offer_id: newOfferId(),
|
|
633
|
-
terms: offerTerms({
|
|
634
|
-
sku,
|
|
635
|
-
priceMinor: effectivePriceMinor,
|
|
636
|
-
quantity,
|
|
637
|
-
currency,
|
|
638
|
-
handoff_destination,
|
|
639
|
-
deliveryBefore: deliveryBefore(),
|
|
640
|
-
...(promotion.kind === "applied" ? { validUntilCap: promotion.endsAt } : {}),
|
|
641
|
-
}, now()),
|
|
642
|
-
},
|
|
643
|
-
...(note !== undefined ? { public_message: note } : {}),
|
|
644
|
-
});
|
|
645
|
-
// 审查 BUG-10:merchant 侧相位由自己的出站动作推进——rfq 的 offer
|
|
646
|
-
// 回复是 OPEN→OFFER_OPEN 的边(入站侧 rfq 是起始动作无事件)。
|
|
647
|
-
// 审查 P2:推进被拒(非法转换)→ 不落账不发出,防链上事实与相位机分裂。
|
|
648
|
-
const advanced = await advancePhase(negotiationId, {
|
|
649
|
-
type: "offer",
|
|
650
|
-
offer_id: String(reply.payload.offer_id ?? ""),
|
|
651
|
-
});
|
|
652
|
-
if (!advanced)
|
|
653
|
-
return declineReply("state_conflict");
|
|
654
|
-
await appendSent(reply);
|
|
655
|
-
return envelopeReply(reply);
|
|
656
|
-
}
|
|
657
|
-
case "offer": {
|
|
658
|
-
// 商家还价:对 buyer 的 offer 回 counter_offer(真实商品价)。
|
|
659
|
-
const buyerOffer = envelope.payload;
|
|
660
|
-
const sku = buyerOffer.terms?.items?.[0]?.sku ?? MERCHANT_SKU;
|
|
661
|
-
const quantityRaw = buyerOffer.terms?.items?.[0]?.quantity?.value;
|
|
662
|
-
if (invalidQuantity(quantityRaw))
|
|
663
|
-
return declineReply("schema_invalid");
|
|
664
|
-
const quantity = quantityRaw ?? MERCHANT_QUANTITY;
|
|
665
|
-
const floorMinor = floorMinorForSku(sku);
|
|
666
|
-
if (floorMinor === null)
|
|
667
|
-
return declineReply("temporarily_unavailable");
|
|
668
|
-
const resolved = await resolveProductOrDecline(sku);
|
|
669
|
-
if ("declined" in resolved)
|
|
670
|
-
return resolved.declined;
|
|
671
|
-
const product = resolved.product;
|
|
672
|
-
const { priceMinor, currency, note, handoff_destination } = product;
|
|
673
|
-
if (factsUnusable(product, quantity))
|
|
674
|
-
return declineReply("temporarily_unavailable");
|
|
675
|
-
const promotion = resolvePromotionQuote(sku, quantity, priceMinor, currency, floorMinor);
|
|
676
|
-
if (promotion.kind === "unavailable")
|
|
677
|
-
return declineReply("temporarily_unavailable");
|
|
678
|
-
if (promotion.kind === "invalid")
|
|
679
|
-
return declineReply("approval_required");
|
|
680
|
-
// 确定性:counter = list 价(公开价);list < floor 时拒绝自动报价
|
|
681
|
-
// (用 floor 兜底=把底价报给买家)。
|
|
682
|
-
const effectivePriceMinor = promotion.kind === "applied" ? promotion.priceMinor : priceMinor;
|
|
683
|
-
if (effectivePriceMinor < floorMinor)
|
|
684
|
-
return declineReply("approval_required");
|
|
685
|
-
const reply = seedEnvelope({
|
|
686
|
-
negotiation_id: negotiationId,
|
|
687
|
-
in_reply_to: inReplyTo,
|
|
688
|
-
actor: "merchant",
|
|
689
|
-
action: "counter_offer",
|
|
690
|
-
created_at: now(),
|
|
691
|
-
payload: {
|
|
692
|
-
type: "counter_offer",
|
|
693
|
-
offer_id: newOfferId(),
|
|
694
|
-
responding_to_offer_id: buyerOffer.offer_id ?? "",
|
|
695
|
-
proposed_terms: offerTerms({
|
|
696
|
-
sku,
|
|
697
|
-
priceMinor: effectivePriceMinor,
|
|
698
|
-
quantity,
|
|
699
|
-
currency,
|
|
700
|
-
handoff_destination,
|
|
701
|
-
deliveryBefore: deliveryBefore(),
|
|
702
|
-
...(promotion.kind === "applied" ? { validUntilCap: promotion.endsAt } : {}),
|
|
703
|
-
}, now()),
|
|
704
|
-
},
|
|
705
|
-
...(note !== undefined ? { public_message: note } : {}),
|
|
706
|
-
});
|
|
707
|
-
// 审查 P2:推进被拒 → 不落账不发出(防 OFFER_OPEN 下重复还价分裂)。
|
|
708
|
-
const advanced = await advancePhase(negotiationId, {
|
|
709
|
-
type: "counter_offer",
|
|
710
|
-
offer_id: String(reply.payload.offer_id ?? ""),
|
|
711
|
-
});
|
|
712
|
-
if (!advanced)
|
|
713
|
-
return declineReply("state_conflict");
|
|
714
|
-
await appendSent(reply);
|
|
715
|
-
return envelopeReply(reply);
|
|
716
|
-
}
|
|
717
|
-
case "counter_offer": {
|
|
718
|
-
const counter = envelope.payload;
|
|
719
|
-
const sku = counter.proposed_terms?.items?.[0]?.sku ?? MERCHANT_SKU;
|
|
720
|
-
const quantityRaw = counter.proposed_terms?.items?.[0]?.quantity?.value;
|
|
721
|
-
if (invalidQuantity(quantityRaw))
|
|
722
|
-
return declineReply("schema_invalid");
|
|
723
|
-
const quantity = quantityRaw ?? MERCHANT_QUANTITY;
|
|
724
|
-
const buyerCounterMinor = counter.proposed_terms?.items?.[0]?.unit_price?.amount_minor;
|
|
725
|
-
const floorMinor = floorMinorForSku(sku);
|
|
726
|
-
if (floorMinor === null)
|
|
727
|
-
return declineReply("temporarily_unavailable");
|
|
728
|
-
const resolved = await resolveProductOrDecline(sku);
|
|
729
|
-
if ("declined" in resolved)
|
|
730
|
-
return resolved.declined;
|
|
731
|
-
const product = resolved.product;
|
|
732
|
-
const { priceMinor, currency, note, handoff_destination } = product;
|
|
733
|
-
if (factsUnusable(product, quantity))
|
|
734
|
-
return declineReply("temporarily_unavailable");
|
|
735
|
-
const authorityPromotion = resolvePromotionQuote(sku, quantity, priceMinor, currency, floorMinor);
|
|
736
|
-
if (authorityPromotion.kind === "unavailable") {
|
|
737
|
-
return declineReply("temporarily_unavailable");
|
|
738
|
-
}
|
|
739
|
-
if (authorityPromotion.kind === "invalid")
|
|
740
|
-
return declineReply("approval_required");
|
|
741
|
-
// 确定性(无 LLM):买家还价只在**公开折扣边界**内响应——
|
|
742
|
-
// 边界 = list×(1-max_auto_discount_percent),是公开策略值;
|
|
743
|
-
// 高于 list 压到 list,低于公开边界抬到公开边界。
|
|
744
|
-
// 关键:绝不把还价 clamp 到私有底价再回给买家(那样一次极低还价
|
|
745
|
-
// 即可探出底价精确值,T045)。
|
|
746
|
-
const publicBoundMinor = publicDiscountBoundMinor(priceMinor, sku);
|
|
747
|
-
const policyResponsiveMinor = typeof buyerCounterMinor === "number" && Number.isFinite(buyerCounterMinor)
|
|
748
|
-
? clampToBounds(buyerCounterMinor, publicBoundMinor, priceMinor)
|
|
749
|
-
: priceMinor;
|
|
750
|
-
const responsiveMinor = authorityPromotion.kind === "applied"
|
|
751
|
-
? Math.min(policyResponsiveMinor, authorityPromotion.priceMinor)
|
|
752
|
-
: policyResponsiveMinor;
|
|
753
|
-
if (responsiveMinor < floorMinor) {
|
|
754
|
-
// 公开边界低于私有底价:该区间不能自动报价,交人工/审批处理,且不返回价格。
|
|
755
|
-
return declineReply("approval_required");
|
|
625
|
+
if ((envelope.action === "withdraw" || envelope.action === "decline") &&
|
|
626
|
+
envelope.payload.scope === "offer") {
|
|
627
|
+
const target = envelope.payload;
|
|
628
|
+
const activeId = phaseStateByNegotiation.get(negotiationId)?.active_offer_id;
|
|
629
|
+
const object = activeObjectByNegotiation.get(negotiationId);
|
|
630
|
+
if (object === undefined || object.offerId !== activeId ||
|
|
631
|
+
target.target_message_id !== object.messageId ||
|
|
632
|
+
(target.target_offer_id !== undefined && target.target_offer_id !== activeId)) {
|
|
633
|
+
return { kind: "declined", reasonCode: "offer_unknown" };
|
|
756
634
|
}
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
const promo = policyOf()?.promos?.[sku];
|
|
761
|
-
const bulkThreshold = promo?.bulk_threshold ?? DEFAULT_BULK_THRESHOLD;
|
|
762
|
-
const bulkMinor = promo !== undefined
|
|
763
|
-
? clampToBounds(Math.min(responsiveMinor, applyDiscountPercentMinor(priceMinor, promo.bulk_discount_percent ?? 0)), publicBoundMinor, priceMinor)
|
|
764
|
-
: undefined;
|
|
765
|
-
const promotionalMinor = bulkMinor !== undefined && bulkMinor < responsiveMinor && bulkMinor >= floorMinor
|
|
766
|
-
? bulkMinor
|
|
767
|
-
: undefined;
|
|
768
|
-
const reply = seedEnvelope({
|
|
769
|
-
negotiation_id: negotiationId,
|
|
770
|
-
in_reply_to: inReplyTo,
|
|
771
|
-
actor: "merchant",
|
|
772
|
-
action: "conditional_offer",
|
|
773
|
-
created_at: now(),
|
|
774
|
-
payload: {
|
|
775
|
-
type: "conditional_offer",
|
|
776
|
-
offer_id: newOfferId(),
|
|
777
|
-
responding_to_offer_id: counter.offer_id,
|
|
778
|
-
// 许可绑定(T047):本次报价依据的规则摘要与商品事实指纹;accept 时重验。
|
|
779
|
-
policy_digest: policyDigest(),
|
|
780
|
-
product_fingerprint: productFingerprint({ sku, priceMinor, currency }),
|
|
781
|
-
// base = 还价响应(已 clamp 到 [floor, list])——单台即得到该价。
|
|
782
|
-
base_terms: offerTerms({
|
|
783
|
-
sku,
|
|
784
|
-
priceMinor: responsiveMinor,
|
|
785
|
-
quantity,
|
|
786
|
-
currency,
|
|
787
|
-
handoff_destination,
|
|
788
|
-
deliveryBefore: deliveryBefore(),
|
|
789
|
-
...(authorityPromotion.kind === "applied"
|
|
790
|
-
? { validUntilCap: authorityPromotion.endsAt }
|
|
791
|
-
: {}),
|
|
792
|
-
}, now()),
|
|
793
|
-
// conditions 可空(§13:空 conditions 等价 base_terms)。促销价低于
|
|
794
|
-
// 私有底价时不挂条件——不广告无法兑现的价,也不借 then_terms 泄露底价。
|
|
795
|
-
conditions: promotionalMinor !== undefined
|
|
796
|
-
? [
|
|
797
|
-
{
|
|
798
|
-
when: {
|
|
799
|
-
all: [
|
|
800
|
-
{ field: "aggregate.total_quantity", op: "gte", value: bulkThreshold },
|
|
801
|
-
],
|
|
802
|
-
},
|
|
803
|
-
then_terms: offerTerms({
|
|
804
|
-
sku,
|
|
805
|
-
priceMinor: promotionalMinor,
|
|
806
|
-
quantity,
|
|
807
|
-
currency,
|
|
808
|
-
handoff_destination,
|
|
809
|
-
deliveryBefore: deliveryBefore(),
|
|
810
|
-
}, now()),
|
|
811
|
-
},
|
|
812
|
-
]
|
|
813
|
-
: [],
|
|
814
|
-
},
|
|
815
|
-
...(note !== undefined ? { public_message: note } : {}),
|
|
816
|
-
});
|
|
817
|
-
conditionalByNegotiation.set(negotiationId, {
|
|
818
|
-
conditional: reply.payload,
|
|
819
|
-
quantity,
|
|
820
|
-
});
|
|
821
|
-
// 审查 P2:推进被拒 → 不落账不发出。
|
|
822
|
-
const advanced = await advancePhase(negotiationId, {
|
|
823
|
-
type: "conditional_offer",
|
|
824
|
-
offer_id: String(reply.payload.offer_id ?? ""),
|
|
825
|
-
});
|
|
826
|
-
if (!advanced)
|
|
827
|
-
return declineReply("state_conflict");
|
|
828
|
-
await appendSent(reply);
|
|
829
|
-
return envelopeReply(reply);
|
|
635
|
+
const expectedAuthor = envelope.action === "withdraw" ? "buyer" : "merchant";
|
|
636
|
+
if (object.actor !== expectedAuthor)
|
|
637
|
+
return { kind: "declined", reasonCode: "authorization_failed" };
|
|
830
638
|
}
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
const promised = deliveryBefore();
|
|
840
|
-
const answerText = promised !== undefined
|
|
841
|
-
? `delivery before ${promised}, payment terms negotiable (nonbinding)`
|
|
842
|
-
: "delivery timing to be confirmed with the merchant, payment terms negotiable (nonbinding)";
|
|
843
|
-
const reply = seedEnvelope({
|
|
844
|
-
negotiation_id: negotiationId,
|
|
845
|
-
in_reply_to: inReplyTo,
|
|
846
|
-
actor: "merchant",
|
|
847
|
-
action: "clarification_response",
|
|
848
|
-
created_at: now(),
|
|
849
|
-
payload: {
|
|
850
|
-
type: "clarification_response",
|
|
851
|
-
answers: questions.map((q) => ({
|
|
852
|
-
field: q.field ?? "…",
|
|
853
|
-
answer: answerText,
|
|
854
|
-
})),
|
|
855
|
-
},
|
|
856
|
-
public_message: `${answerText}.`,
|
|
857
|
-
});
|
|
858
|
-
// 出站应答即恢复(restore 边,§21.2):顶层推进已把相位挂到
|
|
859
|
-
// AWAITING_CLARIFICATION,clarification_response 同轮弹回
|
|
860
|
-
// resume_phase(OFFER_OPEN/OPEN),否则「问一句再 accept」的
|
|
861
|
-
// happy path 会被相位机永久 state_conflict。恢复失败 fail-closed。
|
|
862
|
-
const restored = await advancePhase(negotiationId, { type: "clarification_response" });
|
|
863
|
-
if (!restored) {
|
|
639
|
+
// 审查 BUG-10:每个入站 action 经规范转换表推进相位——非法转换
|
|
640
|
+
// fail-closed(state_conflict)。accept 的推进在 §15 校验通过后
|
|
641
|
+
// (见 accept 分支:被拒的 accept 不得进入 AGREEMENT_REACHED)。
|
|
642
|
+
const phaseEvent = actionToPhaseEvent(envelope);
|
|
643
|
+
const isAcceptAction = envelope.action === "accept_nonbinding";
|
|
644
|
+
if (phaseEvent !== undefined && !isAcceptAction) {
|
|
645
|
+
const advanced = await advancePhase(negotiationId, phaseEvent);
|
|
646
|
+
if (!advanced) {
|
|
864
647
|
return declineReply("state_conflict");
|
|
865
648
|
}
|
|
866
|
-
|
|
867
|
-
|
|
649
|
+
if (["offer", "counter_offer", "conditional_offer"].includes(envelope.action)) {
|
|
650
|
+
activeObjectByNegotiation.set(negotiationId, {
|
|
651
|
+
offerId: String(envelope.payload.offer_id),
|
|
652
|
+
messageId: envelope.message_id, actor: envelope.actor,
|
|
653
|
+
});
|
|
654
|
+
}
|
|
868
655
|
}
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
if (
|
|
882
|
-
return
|
|
656
|
+
// merchant 定价是**确定性**的(不依赖 LLM):floor / 促销是 merchant 自己的
|
|
657
|
+
// 可配置策略。per-SKU 私有 floor(major→minor lossless;SKU 未列出用全局
|
|
658
|
+
// 默认)。审查 P1:底价已配置但无法无损换算(如 85.005)→ 返回 null,
|
|
659
|
+
// 调用方必须 decline 该 SKU 报价——绝不落到 0(等于关闭底价护栏,
|
|
660
|
+
// fail-open 可低于底价成交),与商品价 lossy 即拒绝的方向保持一致。
|
|
661
|
+
/** 数量校验(审查 P2):数量存在时必须是正整数——0/负数/小数按结构化
|
|
662
|
+
* 无效拒绝,不进 offer terms(否则批量价/条件成交按无意义数量求值);
|
|
663
|
+
* 未报数量沿用 MERCHANT_QUANTITY 缺省口径(不变)。 */
|
|
664
|
+
const invalidQuantity = (raw) => (typeof raw !== "number" || !Number.isSafeInteger(raw) || raw <= 0);
|
|
665
|
+
const floorMinorForSku = (sku) => {
|
|
666
|
+
const mp = policyOf();
|
|
667
|
+
const floorValue = mp?.price_floors?.[sku] ?? mp?.min_unit_price_private;
|
|
668
|
+
if (floorValue === undefined)
|
|
669
|
+
return 0;
|
|
670
|
+
const conv = losslessToMinorUnits(floorValue, 2);
|
|
671
|
+
return conv.lossless ? conv.amount_minor : null;
|
|
672
|
+
};
|
|
673
|
+
/** 权威交期(V2 §8.5 P0-1):配置了 merchant_policy.delivery_lead_days 才
|
|
674
|
+
* 报具体 delivery_before(报价时间+天数,动态计算不过期);未配置 → undefined,
|
|
675
|
+
* terms 省略 delivery_before(明确未知)。 */
|
|
676
|
+
const deliveryBefore = () => resolveDeliveryBefore(policyOf(), now());
|
|
677
|
+
/** 买家还价(major→minor;KNP 里 buyer counter 的 unit_price)。 */
|
|
678
|
+
const clampToBounds = (minor, floor, list) => Math.min(list, Math.max(minor, floor));
|
|
679
|
+
/**
|
|
680
|
+
* 对买家可见的**自动折扣边界**(公开策略值:max_auto_discount_percent)。
|
|
681
|
+
*
|
|
682
|
+
* 安全要点(T045「不泄露底价」):商家对买家的任何报价都必须 clamp 到
|
|
683
|
+
* **公开边界**而不是 clamp 到私有底价——把还价 clamp 到底价再回给买家,
|
|
684
|
+
* 等于一次极低还价即可探出私有底价(一次探测即泄露精确值)。
|
|
685
|
+
* 私有底价只用来判断「这个价能不能自动报」,绝不出现在返回的条款里。
|
|
686
|
+
*/
|
|
687
|
+
/**
|
|
688
|
+
* 运行中规则的摘要(T047):接受成交前必须重验——商家改了规则/促销/底价后,
|
|
689
|
+
* 旧 conditional 的许可即失效,不能按旧价成交(设计 §10.2「审批时商品/规则
|
|
690
|
+
* 已变化 → 使旧候选失效或重新确认,不能执行过期许可」)。
|
|
691
|
+
*/
|
|
692
|
+
const policyDigest = () => contentDigest((policyOf() ?? {}));
|
|
693
|
+
/** 商品事实指纹(T047):价格/币种变化同样使旧许可失效。 */
|
|
694
|
+
const productFingerprint = (facts) => contentDigest({
|
|
695
|
+
sku: facts.sku,
|
|
696
|
+
priceMinor: facts.priceMinor,
|
|
697
|
+
currency: facts.currency,
|
|
698
|
+
});
|
|
699
|
+
const publicDiscountBoundMinor = (listMinor, sku) => {
|
|
700
|
+
const policy = policyOf();
|
|
701
|
+
const autoRaw = policy?.max_auto_discount_percent;
|
|
702
|
+
const auto = typeof autoRaw === "number" && Number.isFinite(autoRaw) && autoRaw > 0 ? autoRaw : 0;
|
|
703
|
+
// 促销本身是**公开条款**(promos[sku].bulk_discount_percent),因此公开边界
|
|
704
|
+
// 取其与自动折扣的较大者;未公布任何折扣时边界 = list(不自动折扣)。
|
|
705
|
+
const promoPct = policy?.promos?.[sku]?.bulk_discount_percent ?? 0;
|
|
706
|
+
const pct = Math.max(auto, Number.isFinite(promoPct) && promoPct > 0 ? promoPct : 0);
|
|
707
|
+
return applyDiscountPercentMinor(listMinor, pct);
|
|
708
|
+
};
|
|
709
|
+
/**
|
|
710
|
+
* 商品事实闸门(T046):数据过期或数量超过可得库存 → 明确不可报价。
|
|
711
|
+
* 设计 §10.1:「数据到期后停止相关自动报价或注明需人工确认,不把库存未知
|
|
712
|
+
* 说成有货」。**不在这里做任何兜底定价**(演示价已在配置层被云端拒绝)。
|
|
713
|
+
*/
|
|
714
|
+
const factsUnusable = (facts, quantity) => {
|
|
715
|
+
if (facts.valid_until !== undefined) {
|
|
716
|
+
const until = Date.parse(facts.valid_until);
|
|
717
|
+
if (!Number.isFinite(until) || until <= Date.parse(now()))
|
|
718
|
+
return true;
|
|
883
719
|
}
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
720
|
+
if (facts.stock !== undefined && quantity > facts.stock)
|
|
721
|
+
return true;
|
|
722
|
+
return false;
|
|
723
|
+
};
|
|
724
|
+
const snapshotFor = (sku, quantity, product, promotion) => ({ ...product, sku, quantity, policy_digest: policyDigest(), promotion });
|
|
725
|
+
const quoteValidity = (product, promotionEnd) => {
|
|
726
|
+
const ends = [product.valid_until, promotionEnd].filter((v) => v !== undefined);
|
|
727
|
+
return ends.length === 0 ? {} : { validUntilCap: ends.reduce((a, b) => Date.parse(a) <= Date.parse(b) ? a : b) };
|
|
728
|
+
};
|
|
729
|
+
const quoteMessage = (product) => product.stock === undefined || product.source_evidence === undefined
|
|
730
|
+
? { public_message: "needs_confirmation:库存或来源证据待确认;仅非承诺价格提案,须重新报价/确认后接受。" }
|
|
731
|
+
: product.note === undefined ? {} : { public_message: product.note };
|
|
732
|
+
switch (envelope.action) {
|
|
733
|
+
case "inquiry": {
|
|
734
|
+
return textReply("Please provide a product SKU, quantity and declared unit for a source-backed quote.");
|
|
899
735
|
}
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
736
|
+
case "rfq": {
|
|
737
|
+
const payload = envelope.payload;
|
|
738
|
+
const sku = payload.items?.[0]?.sku;
|
|
739
|
+
const requestedUnit = payload.items?.[0]?.quantity?.unit;
|
|
740
|
+
const quantityRaw = payload.items?.[0]?.quantity?.value;
|
|
741
|
+
if (typeof sku !== "string" || sku.trim() === "" || typeof requestedUnit !== "string" || requestedUnit.trim() === "" || invalidQuantity(quantityRaw))
|
|
742
|
+
return declineReply("schema_invalid");
|
|
743
|
+
const quantity = quantityRaw;
|
|
744
|
+
const floorMinor = floorMinorForSku(sku);
|
|
745
|
+
if (floorMinor === null)
|
|
746
|
+
return declineReply("temporarily_unavailable");
|
|
747
|
+
const resolved = await resolveProductOrDecline(sku);
|
|
748
|
+
if ("declined" in resolved)
|
|
749
|
+
return resolved.declined;
|
|
750
|
+
const product = resolved.product;
|
|
751
|
+
const { priceMinor, currency } = product;
|
|
752
|
+
if (product.unit === undefined || product.unit !== requestedUnit)
|
|
753
|
+
return declineReply("field_unsupported");
|
|
754
|
+
const handoff_destination = product.stock === undefined || product.source_evidence === undefined ? undefined : product.handoff_destination;
|
|
755
|
+
if (factsUnusable(product, quantity))
|
|
756
|
+
return declineReply("temporarily_unavailable");
|
|
757
|
+
const promotion = resolvePromotionQuote(sku, quantity, priceMinor, currency, floorMinor);
|
|
758
|
+
if (promotion.kind === "unavailable")
|
|
759
|
+
return declineReply("temporarily_unavailable");
|
|
760
|
+
if (promotion.kind === "invalid")
|
|
761
|
+
return declineReply("approval_required");
|
|
762
|
+
// 确定性:offer = list 价(公开价)。list < floor 是配置矛盾——此时
|
|
763
|
+
// 用 floor 兜底会把私有底价直接报给买家(T045 泄露面),因此拒绝自动报价。
|
|
764
|
+
const effectivePriceMinor = promotion.kind === "applied" ? promotion.priceMinor : priceMinor;
|
|
765
|
+
if (effectivePriceMinor < floorMinor)
|
|
766
|
+
return declineReply("approval_required");
|
|
767
|
+
const reply = seedEnvelope({
|
|
768
|
+
negotiation_id: negotiationId,
|
|
769
|
+
in_reply_to: inReplyTo,
|
|
770
|
+
actor: "merchant",
|
|
771
|
+
action: "offer",
|
|
772
|
+
created_at: now(),
|
|
773
|
+
payload: {
|
|
774
|
+
type: "offer",
|
|
775
|
+
offer_id: newOfferId(),
|
|
776
|
+
terms: offerTerms({
|
|
777
|
+
sku,
|
|
778
|
+
unit: product.unit,
|
|
779
|
+
priceMinor: effectivePriceMinor,
|
|
780
|
+
quantity,
|
|
781
|
+
currency,
|
|
782
|
+
handoff_destination,
|
|
783
|
+
deliveryBefore: deliveryBefore(),
|
|
784
|
+
...quoteValidity(product, promotion.kind === "applied" ? promotion.endsAt : undefined),
|
|
785
|
+
}, now()),
|
|
786
|
+
},
|
|
787
|
+
...quoteMessage(product),
|
|
788
|
+
});
|
|
789
|
+
// 审查 BUG-10:merchant 侧相位由自己的出站动作推进——rfq 的 offer
|
|
790
|
+
// 回复是 OPEN→OFFER_OPEN 的边(入站侧 rfq 是起始动作无事件)。
|
|
791
|
+
// 审查 P2:推进被拒(非法转换)→ 不落账不发出,防链上事实与相位机分裂。
|
|
792
|
+
const advanced = await advancePhase(negotiationId, {
|
|
793
|
+
type: "offer",
|
|
794
|
+
offer_id: String(reply.payload.offer_id ?? ""),
|
|
795
|
+
});
|
|
796
|
+
if (!advanced)
|
|
797
|
+
return declineReply("state_conflict");
|
|
798
|
+
// review 2-3:直报价登记为可接受对象——此前只有 counter_offer 分支
|
|
799
|
+
// 登记,买家 accept 直报价必得 offer_unknown(§15/§21.2 主路径在
|
|
800
|
+
// 对外合规实现上互操作断裂)。以 base_terms 规约形状存储,accept
|
|
801
|
+
// 路径的 evaluateConditionalOffer 对无条件条件即返回 base terms。
|
|
802
|
+
// review 2-3(返修):登记对象必须带 offer_id——accept 路径读
|
|
803
|
+
// stored.conditional.offer_id 与 accept 的 offer_id 强匹配(A314
|
|
804
|
+
// 源码核:原返修缺 offer_id,直报价接受仍 offer_unknown)。
|
|
805
|
+
conditionalByNegotiation.set(negotiationId, normalizeDirectOfferAsConditional(String(reply.payload.offer_id ?? ""), reply.payload.terms ?? {}, quantity));
|
|
806
|
+
await appendSent(reply, snapshotFor(sku, quantity, product, promotion));
|
|
807
|
+
return envelopeReply(reply);
|
|
903
808
|
}
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
809
|
+
case "offer": {
|
|
810
|
+
// 商家还价:对 buyer 的 offer 回 counter_offer(真实商品价)。
|
|
811
|
+
const buyerOffer = envelope.payload;
|
|
812
|
+
const sku = buyerOffer.terms?.items?.[0]?.sku;
|
|
813
|
+
const requestedUnit = buyerOffer.terms?.items?.[0]?.quantity?.unit;
|
|
814
|
+
const quantityRaw = buyerOffer.terms?.items?.[0]?.quantity?.value;
|
|
815
|
+
if (typeof sku !== "string" || sku.trim() === "" || typeof requestedUnit !== "string" || requestedUnit.trim() === "" || invalidQuantity(quantityRaw))
|
|
816
|
+
return declineReply("schema_invalid");
|
|
817
|
+
const quantity = quantityRaw;
|
|
818
|
+
const floorMinor = floorMinorForSku(sku);
|
|
819
|
+
if (floorMinor === null)
|
|
820
|
+
return declineReply("temporarily_unavailable");
|
|
821
|
+
const resolved = await resolveProductOrDecline(sku);
|
|
822
|
+
if ("declined" in resolved)
|
|
823
|
+
return resolved.declined;
|
|
824
|
+
const product = resolved.product;
|
|
825
|
+
const { priceMinor, currency } = product;
|
|
826
|
+
if (product.unit === undefined || product.unit !== requestedUnit)
|
|
827
|
+
return declineReply("field_unsupported");
|
|
828
|
+
const handoff_destination = product.stock === undefined || product.source_evidence === undefined ? undefined : product.handoff_destination;
|
|
829
|
+
if (factsUnusable(product, quantity))
|
|
830
|
+
return declineReply("temporarily_unavailable");
|
|
831
|
+
const promotion = resolvePromotionQuote(sku, quantity, priceMinor, currency, floorMinor);
|
|
832
|
+
if (promotion.kind === "unavailable")
|
|
833
|
+
return declineReply("temporarily_unavailable");
|
|
834
|
+
if (promotion.kind === "invalid")
|
|
835
|
+
return declineReply("approval_required");
|
|
836
|
+
// 确定性:counter = list 价(公开价);list < floor 时拒绝自动报价
|
|
837
|
+
// (用 floor 兜底=把底价报给买家)。
|
|
838
|
+
const effectivePriceMinor = promotion.kind === "applied" ? promotion.priceMinor : priceMinor;
|
|
839
|
+
if (effectivePriceMinor < floorMinor)
|
|
840
|
+
return declineReply("approval_required");
|
|
841
|
+
const reply = seedEnvelope({
|
|
842
|
+
negotiation_id: negotiationId,
|
|
843
|
+
in_reply_to: inReplyTo,
|
|
844
|
+
actor: "merchant",
|
|
845
|
+
action: "counter_offer",
|
|
846
|
+
created_at: now(),
|
|
847
|
+
payload: {
|
|
848
|
+
type: "counter_offer",
|
|
849
|
+
offer_id: newOfferId(),
|
|
850
|
+
responding_to_offer_id: buyerOffer.offer_id ?? "",
|
|
851
|
+
proposed_terms: offerTerms({
|
|
852
|
+
sku,
|
|
853
|
+
unit: product.unit,
|
|
854
|
+
priceMinor: effectivePriceMinor,
|
|
855
|
+
quantity,
|
|
856
|
+
currency,
|
|
857
|
+
handoff_destination,
|
|
858
|
+
deliveryBefore: deliveryBefore(),
|
|
859
|
+
...quoteValidity(product, promotion.kind === "applied" ? promotion.endsAt : undefined),
|
|
860
|
+
}, now()),
|
|
861
|
+
},
|
|
862
|
+
...quoteMessage(product),
|
|
863
|
+
});
|
|
864
|
+
// 审查 P2:推进被拒 → 不落账不发出(防 OFFER_OPEN 下重复还价分裂)。
|
|
865
|
+
const advanced = await advancePhase(negotiationId, {
|
|
866
|
+
type: "counter_offer",
|
|
867
|
+
offer_id: String(reply.payload.offer_id ?? ""),
|
|
868
|
+
});
|
|
869
|
+
if (!advanced)
|
|
870
|
+
return declineReply("state_conflict");
|
|
871
|
+
// review 2-3:还价(counter_offer)同样登记——买家接受直还价走通主路径。
|
|
872
|
+
conditionalByNegotiation.set(negotiationId, normalizeDirectOfferAsConditional(String(reply.payload.offer_id ?? ""), reply.payload.proposed_terms ?? {}, quantity));
|
|
873
|
+
await appendSent(reply, snapshotFor(sku, quantity, product, promotion));
|
|
874
|
+
return envelopeReply(reply);
|
|
911
875
|
}
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
const
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
876
|
+
case "counter_offer": {
|
|
877
|
+
const counter = envelope.payload;
|
|
878
|
+
const sku = counter.proposed_terms?.items?.[0]?.sku;
|
|
879
|
+
const requestedUnit = counter.proposed_terms?.items?.[0]?.quantity?.unit;
|
|
880
|
+
const quantityRaw = counter.proposed_terms?.items?.[0]?.quantity?.value;
|
|
881
|
+
if (typeof sku !== "string" || sku.trim() === "" || typeof requestedUnit !== "string" || requestedUnit.trim() === "" || invalidQuantity(quantityRaw))
|
|
882
|
+
return declineReply("schema_invalid");
|
|
883
|
+
const quantity = quantityRaw;
|
|
884
|
+
const buyerCounterMinor = counter.proposed_terms?.items?.[0]?.unit_price?.amount_minor;
|
|
885
|
+
const floorMinor = floorMinorForSku(sku);
|
|
886
|
+
if (floorMinor === null)
|
|
887
|
+
return declineReply("temporarily_unavailable");
|
|
888
|
+
const resolved = await resolveProductOrDecline(sku);
|
|
889
|
+
if ("declined" in resolved)
|
|
890
|
+
return resolved.declined;
|
|
891
|
+
const product = resolved.product;
|
|
892
|
+
const { priceMinor, currency } = product;
|
|
893
|
+
if (product.unit === undefined || product.unit !== requestedUnit)
|
|
894
|
+
return declineReply("field_unsupported");
|
|
895
|
+
const handoff_destination = product.stock === undefined || product.source_evidence === undefined ? undefined : product.handoff_destination;
|
|
896
|
+
if (factsUnusable(product, quantity))
|
|
897
|
+
return declineReply("temporarily_unavailable");
|
|
898
|
+
const authorityPromotion = resolvePromotionQuote(sku, quantity, priceMinor, currency, floorMinor);
|
|
899
|
+
if (authorityPromotion.kind === "unavailable") {
|
|
900
|
+
return declineReply("temporarily_unavailable");
|
|
901
|
+
}
|
|
902
|
+
if (authorityPromotion.kind === "invalid")
|
|
903
|
+
return declineReply("approval_required");
|
|
904
|
+
// 确定性(无 LLM):买家还价只在**公开折扣边界**内响应——
|
|
905
|
+
// 边界 = list×(1-max_auto_discount_percent),是公开策略值;
|
|
906
|
+
// 高于 list 压到 list,低于公开边界抬到公开边界。
|
|
907
|
+
// 关键:绝不把还价 clamp 到私有底价再回给买家(那样一次极低还价
|
|
908
|
+
// 即可探出底价精确值,T045)。
|
|
909
|
+
const publicBoundMinor = publicDiscountBoundMinor(priceMinor, sku);
|
|
910
|
+
const policyResponsiveMinor = typeof buyerCounterMinor === "number" && Number.isFinite(buyerCounterMinor)
|
|
911
|
+
? clampToBounds(buyerCounterMinor, publicBoundMinor, priceMinor)
|
|
912
|
+
: priceMinor;
|
|
913
|
+
const responsiveMinor = authorityPromotion.kind === "applied"
|
|
914
|
+
? Math.min(policyResponsiveMinor, authorityPromotion.priceMinor)
|
|
915
|
+
: policyResponsiveMinor;
|
|
916
|
+
if (responsiveMinor < floorMinor) {
|
|
917
|
+
// 公开边界低于私有底价:该区间不能自动报价,交人工/审批处理,且不返回价格。
|
|
923
918
|
return declineReply("approval_required");
|
|
924
919
|
}
|
|
920
|
+
// 可配置促销(merchant_policy.promos[sku]):买满 bulk_threshold 台,
|
|
921
|
+
// 批量价 = min(还价响应, list×(1-d%/100)),同样 clamp 到公开边界;
|
|
922
|
+
// 若该价低于私有底价则**不挂这条条件**(不广告无法兑现的价)。
|
|
923
|
+
const promo = policyOf()?.promos?.[sku];
|
|
924
|
+
const bulkThreshold = promo?.bulk_threshold ?? DEFAULT_BULK_THRESHOLD;
|
|
925
|
+
const bulkMinor = promo !== undefined
|
|
926
|
+
? clampToBounds(Math.min(responsiveMinor, applyDiscountPercentMinor(priceMinor, promo.bulk_discount_percent ?? 0)), publicBoundMinor, priceMinor)
|
|
927
|
+
: undefined;
|
|
928
|
+
const promotionalMinor = bulkMinor !== undefined && bulkMinor < responsiveMinor && bulkMinor >= floorMinor
|
|
929
|
+
? bulkMinor
|
|
930
|
+
: undefined;
|
|
931
|
+
const reply = seedEnvelope({
|
|
932
|
+
negotiation_id: negotiationId,
|
|
933
|
+
in_reply_to: inReplyTo,
|
|
934
|
+
actor: "merchant",
|
|
935
|
+
action: "conditional_offer",
|
|
936
|
+
created_at: now(),
|
|
937
|
+
payload: {
|
|
938
|
+
type: "conditional_offer",
|
|
939
|
+
offer_id: newOfferId(),
|
|
940
|
+
responding_to_offer_id: counter.offer_id,
|
|
941
|
+
// 许可绑定(T047):本次报价依据的规则摘要与商品事实指纹;accept 时重验。
|
|
942
|
+
policy_digest: policyDigest(),
|
|
943
|
+
product_fingerprint: productFingerprint({ sku, priceMinor, currency }),
|
|
944
|
+
// base = 还价响应(已 clamp 到 [floor, list])——单台即得到该价。
|
|
945
|
+
base_terms: offerTerms({
|
|
946
|
+
sku,
|
|
947
|
+
unit: product.unit,
|
|
948
|
+
priceMinor: responsiveMinor,
|
|
949
|
+
quantity,
|
|
950
|
+
currency,
|
|
951
|
+
handoff_destination,
|
|
952
|
+
deliveryBefore: deliveryBefore(),
|
|
953
|
+
...quoteValidity(product, authorityPromotion.kind === "applied" ? authorityPromotion.endsAt : undefined),
|
|
954
|
+
}, now()),
|
|
955
|
+
// conditions 可空(§13:空 conditions 等价 base_terms)。促销价低于
|
|
956
|
+
// 私有底价时不挂条件——不广告无法兑现的价,也不借 then_terms 泄露底价。
|
|
957
|
+
conditions: promotionalMinor !== undefined
|
|
958
|
+
? [
|
|
959
|
+
{
|
|
960
|
+
when: {
|
|
961
|
+
all: [
|
|
962
|
+
{ field: "aggregate.total_quantity", op: "gte", value: bulkThreshold },
|
|
963
|
+
],
|
|
964
|
+
},
|
|
965
|
+
then_terms: offerTerms({
|
|
966
|
+
sku,
|
|
967
|
+
unit: product.unit,
|
|
968
|
+
priceMinor: promotionalMinor,
|
|
969
|
+
quantity,
|
|
970
|
+
currency,
|
|
971
|
+
handoff_destination,
|
|
972
|
+
deliveryBefore: deliveryBefore(),
|
|
973
|
+
...quoteValidity(product, authorityPromotion.kind === "applied" ? authorityPromotion.endsAt : undefined),
|
|
974
|
+
}, now()),
|
|
975
|
+
},
|
|
976
|
+
]
|
|
977
|
+
: [],
|
|
978
|
+
},
|
|
979
|
+
...quoteMessage(product),
|
|
980
|
+
});
|
|
981
|
+
conditionalByNegotiation.set(negotiationId, {
|
|
982
|
+
conditional: reply.payload,
|
|
983
|
+
quantity,
|
|
984
|
+
});
|
|
985
|
+
// 审查 P2:推进被拒 → 不落账不发出。
|
|
986
|
+
const advanced = await advancePhase(negotiationId, {
|
|
987
|
+
type: "conditional_offer",
|
|
988
|
+
offer_id: String(reply.payload.offer_id ?? ""),
|
|
989
|
+
});
|
|
990
|
+
if (!advanced)
|
|
991
|
+
return declineReply("state_conflict");
|
|
992
|
+
await appendSent(reply, snapshotFor(sku, quantity, product, authorityPromotion));
|
|
993
|
+
return envelopeReply(reply);
|
|
925
994
|
}
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
995
|
+
case "clarification": {
|
|
996
|
+
const questions = envelope.payload.questions ?? [];
|
|
997
|
+
// 方案B(协议完整):商家的应答是结构化 clarification_response
|
|
998
|
+
// envelope(§8.2),in_reply_to 引用被回答的澄清消息(§8.5/§14
|
|
999
|
+
// 强制,finalizeEnvelope 校验)。payload 形状规范未冻结(§14),
|
|
1000
|
+
// 携带 answers 便于对端结构化消费;人类可读文本走 public_message。
|
|
1001
|
+
// 交期口径(V2 §8.5 P0-1):配置了权威交期才报具体日期;未配置
|
|
1002
|
+
// 明确告知需与商家确认,绝不报静态/过期日期。
|
|
1003
|
+
const promised = deliveryBefore();
|
|
1004
|
+
const answerText = promised !== undefined
|
|
1005
|
+
? `delivery before ${promised}, payment terms negotiable (nonbinding)`
|
|
1006
|
+
: "delivery timing to be confirmed with the merchant, payment terms negotiable (nonbinding)";
|
|
1007
|
+
const reply = seedEnvelope({
|
|
1008
|
+
negotiation_id: negotiationId,
|
|
1009
|
+
in_reply_to: inReplyTo,
|
|
1010
|
+
actor: "merchant",
|
|
1011
|
+
action: "clarification_response",
|
|
1012
|
+
created_at: now(),
|
|
1013
|
+
payload: {
|
|
1014
|
+
type: "clarification_response",
|
|
1015
|
+
answers: questions.map((q) => ({
|
|
1016
|
+
field: q.field ?? "…",
|
|
1017
|
+
answer: answerText,
|
|
1018
|
+
})),
|
|
1019
|
+
},
|
|
1020
|
+
public_message: `${answerText}.`,
|
|
1021
|
+
});
|
|
1022
|
+
// 出站应答即恢复(restore 边,§21.2):顶层推进已把相位挂到
|
|
1023
|
+
// AWAITING_CLARIFICATION,clarification_response 同轮弹回
|
|
1024
|
+
// resume_phase(OFFER_OPEN/OPEN),否则「问一句再 accept」的
|
|
1025
|
+
// happy path 会被相位机永久 state_conflict。恢复失败 fail-closed。
|
|
1026
|
+
const restored = await advancePhase(negotiationId, { type: "clarification_response" });
|
|
1027
|
+
if (!restored) {
|
|
1028
|
+
return declineReply("state_conflict");
|
|
1029
|
+
}
|
|
1030
|
+
await appendSent(reply);
|
|
1031
|
+
return envelopeReply(reply);
|
|
939
1032
|
}
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
1033
|
+
case "accept_nonbinding": {
|
|
1034
|
+
const stored = conditionalByNegotiation.get(negotiationId);
|
|
1035
|
+
const acceptPayload = envelope.payload;
|
|
1036
|
+
const acceptedOfferId = acceptPayload.offer_id ?? "";
|
|
1037
|
+
// 成交价必须来自本磋商已发出的 conditional_offer:无前置 conditional
|
|
1038
|
+
// 或 accept 的 offer_id 与所存 conditional 不匹配 → 拒绝成交。此前
|
|
1039
|
+
// 无前置时回退基础价照样产出 agreement,从未发出的 offer_id 也照样
|
|
1040
|
+
// 成交(agreement 指向不存在的 offer,审计溯源断裂)。
|
|
1041
|
+
const storedOfferId = stored?.conditional?.offer_id ?? "";
|
|
1042
|
+
const acceptedConditional = stored !== undefined && acceptedOfferId !== "" && acceptedOfferId === storedOfferId
|
|
1043
|
+
? stored
|
|
1044
|
+
: undefined;
|
|
1045
|
+
if (acceptedConditional === undefined) {
|
|
1046
|
+
return declineReply("offer_unknown");
|
|
1047
|
+
}
|
|
1048
|
+
// KNP §15(审查 P2-C,实验复现):terms_digest 必须是 agreed terms
|
|
1049
|
+
// 的 canonical digest——错误 digest 此前也产出 agreement。任一检查
|
|
1050
|
+
// 失败都不得创建 agreement。
|
|
1051
|
+
// review 3-15(返修,实际实施):evaluator 对冲突规则抛
|
|
1052
|
+
// NegotiationValidationError("condition_conflict")——不捕获会被管线
|
|
1053
|
+
// 收敛成 -32603 internal error。已知协议校验错误映射为对应协议码
|
|
1054
|
+
// decline;**未知错误原样上抛**(claim 不清、结果按 unknown 对账,
|
|
1055
|
+
// 绝不假纯失败)。
|
|
1056
|
+
let agreedTerms;
|
|
1057
|
+
try {
|
|
1058
|
+
agreedTerms = evaluateConditionalOffer(acceptedConditional.conditional, {
|
|
1059
|
+
"aggregate.total_quantity": acceptedConditional.quantity,
|
|
1060
|
+
});
|
|
1061
|
+
}
|
|
1062
|
+
catch (err) {
|
|
1063
|
+
if (err instanceof NegotiationValidationError) {
|
|
1064
|
+
return declineReply(err.code === "condition_conflict" ? "condition_conflict" : "schema_invalid");
|
|
1065
|
+
}
|
|
1066
|
+
throw err;
|
|
1067
|
+
}
|
|
1068
|
+
// 审查 P3:§11 offer 过期校验——valid_until 已过 → 拒绝(此前
|
|
1069
|
+
// valid_until 硬编码 2099 永不生效,陈旧报价可被无限期接受)。
|
|
1070
|
+
const validUntil = agreedTerms?.valid_until;
|
|
1071
|
+
if (validUntil !== undefined) {
|
|
1072
|
+
const validUntilMs = Date.parse(validUntil);
|
|
1073
|
+
const nowMs = Date.parse(now());
|
|
1074
|
+
if (!Number.isFinite(validUntilMs) || !Number.isFinite(nowMs) || validUntilMs <= nowMs) {
|
|
1075
|
+
return declineReply("offer_expired");
|
|
1076
|
+
}
|
|
1077
|
+
}
|
|
1078
|
+
const presentedDigest = acceptPayload.terms_digest ?? "";
|
|
1079
|
+
if (presentedDigest === "" || presentedDigest !== contentDigest(agreedTerms)) {
|
|
1080
|
+
return declineReply("terms_digest_mismatch");
|
|
1081
|
+
}
|
|
1082
|
+
// T047:许可有效性重验——规则或商品事实在报价之后发生变化,旧许可即失效。
|
|
1083
|
+
// 必须在相位推进/产出协议之前判定(拒绝不得留下任何终态副作用)。
|
|
1084
|
+
const acceptedConditionalPayload = acceptedConditional.conditional;
|
|
1085
|
+
const saved = quoteSources.get(negotiationId);
|
|
1086
|
+
if (saved === undefined || saved.offer_id !== acceptedOfferId)
|
|
1087
|
+
return declineReply("offer_unknown");
|
|
1088
|
+
const agreedItem = agreedTerms.items?.[0];
|
|
1089
|
+
if (agreedItem?.sku !== saved.snapshot.sku || agreedItem.quantity?.value !== saved.snapshot.quantity || agreedItem.quantity?.unit !== saved.snapshot.unit)
|
|
1090
|
+
return declineReply("approval_required");
|
|
1091
|
+
const refreshedFacts = await resolveProductOrDecline(saved.snapshot.sku, { force: true });
|
|
1092
|
+
if ("declined" in refreshedFacts)
|
|
1093
|
+
return declineReply("approval_required");
|
|
1094
|
+
if (!quoteFactsStillValid(saved.snapshot, refreshedFacts.product, Date.parse(now())))
|
|
1095
|
+
return declineReply("approval_required");
|
|
1096
|
+
if (saved.snapshot.policy_digest !== policyDigest())
|
|
1097
|
+
return declineReply("approval_required");
|
|
1098
|
+
const currentFloor = floorMinorForSku(saved.snapshot.sku);
|
|
1099
|
+
if (currentFloor === null)
|
|
1100
|
+
return declineReply("approval_required");
|
|
1101
|
+
const currentPromotion = resolvePromotionQuote(saved.snapshot.sku, saved.snapshot.quantity, refreshedFacts.product.priceMinor, refreshedFacts.product.currency, currentFloor);
|
|
1102
|
+
if (contentDigest(currentPromotion) !== contentDigest(saved.snapshot.promotion))
|
|
1103
|
+
return declineReply("approval_required");
|
|
1104
|
+
const currentPolicyDigest = policyDigest();
|
|
1105
|
+
if (acceptedConditionalPayload.policy_digest !== undefined &&
|
|
1106
|
+
acceptedConditionalPayload.policy_digest !== currentPolicyDigest) {
|
|
1107
|
+
return declineReply("approval_required");
|
|
1108
|
+
}
|
|
1109
|
+
const acceptedSku = acceptedConditionalPayload.base_terms?.items?.[0]?.sku;
|
|
1110
|
+
if (acceptedConditionalPayload.product_fingerprint !== undefined &&
|
|
1111
|
+
acceptedSku !== undefined) {
|
|
1112
|
+
const refreshed = refreshedFacts;
|
|
1113
|
+
if ("declined" in refreshed ||
|
|
1114
|
+
productFingerprint({
|
|
1115
|
+
sku: acceptedSku,
|
|
1116
|
+
priceMinor: refreshed.product.priceMinor,
|
|
1117
|
+
currency: refreshed.product.currency,
|
|
1118
|
+
}) !== acceptedConditionalPayload.product_fingerprint) {
|
|
1119
|
+
// 商品价/币种变化或商品源已不可用 → 旧许可失效,需重新确认。
|
|
1120
|
+
return declineReply("approval_required");
|
|
1121
|
+
}
|
|
1122
|
+
}
|
|
1123
|
+
// 审查 P1-C:相位机是权威守卫——§15 校验通过后、构建协议之前先
|
|
1124
|
+
// 推进相位并检查返回值(BUG-10 语义不变:被拒的 accept 不得进入
|
|
1125
|
+
// AGREEMENT_REACHED)。推进失败(如 AWAITING_CLARIFICATION 下直接
|
|
1126
|
+
// accept)fail-closed:不返回协议、不进 closedNegotiations、不删
|
|
1127
|
+
// conditional,无任何终态副作用——此前忽略返回值,协议照发但相位
|
|
1128
|
+
// 机拒绝推进;进程内 closedNegotiations 挡住二次 accept,重启恢复
|
|
1129
|
+
// 后相位重建为 AWAITING_CLARIFICATION,二次 accept 产出重复协议。
|
|
1130
|
+
const advanced = await advancePhase(negotiationId, {
|
|
1131
|
+
type: "accept_nonbinding",
|
|
1132
|
+
offer_id: acceptedOfferId,
|
|
1133
|
+
});
|
|
1134
|
+
if (!advanced) {
|
|
1135
|
+
return declineReply("state_conflict");
|
|
1136
|
+
}
|
|
1137
|
+
const agreement = buildAgreement({
|
|
1138
|
+
negotiation_id: negotiationId,
|
|
1139
|
+
accepted_offer_id: acceptedOfferId,
|
|
1140
|
+
agreed_terms: agreedTerms,
|
|
1141
|
+
created_at: now(),
|
|
1142
|
+
});
|
|
1143
|
+
const artifactPart = { kind: "data", data: { agreement } };
|
|
1144
|
+
const message = {
|
|
1145
|
+
role: "agent",
|
|
1146
|
+
parts: [{ kind: "text", text: "Agreement reached (nonbinding)." }],
|
|
1147
|
+
messageId: newMessageId(),
|
|
1148
|
+
};
|
|
1149
|
+
// 协商终态:conditional 不再需要(评审项 L3:此前永久累积)。
|
|
1150
|
+
conditionalByNegotiation.delete(negotiationId);
|
|
1151
|
+
quoteSources.delete(negotiationId);
|
|
968
1152
|
closedNegotiations.add(negotiationId);
|
|
969
|
-
|
|
1153
|
+
return {
|
|
1154
|
+
kind: "accepted",
|
|
1155
|
+
taskState: "completed",
|
|
1156
|
+
artifactParts: [artifactPart],
|
|
1157
|
+
message,
|
|
1158
|
+
};
|
|
970
1159
|
}
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
1160
|
+
case "withdraw":
|
|
1161
|
+
case "decline": {
|
|
1162
|
+
// review 2-2(A316 校准撤回):handle 顶部已按 scope 统一推进相位
|
|
1163
|
+
//(actionToPhaseEvent → OFFER_OPEN→OPEN / 终态边),原发现「相位
|
|
1164
|
+
// 不推进」不成立;本分支**不再二次推进**(二次推进反而让合法
|
|
1165
|
+
// decline 在 OPEN 下得到 state_conflict)。scope=offer 只清
|
|
1166
|
+
// conditional;scope=negotiation 进终态集合。
|
|
1167
|
+
const scope = envelope.payload.scope ?? "offer";
|
|
1168
|
+
conditionalByNegotiation.delete(negotiationId);
|
|
1169
|
+
quoteSources.delete(negotiationId);
|
|
1170
|
+
if (scope !== "offer") {
|
|
1171
|
+
closedNegotiations.add(negotiationId);
|
|
1172
|
+
}
|
|
1173
|
+
return textReply(`${envelope.action === "withdraw" ? "Withdrawn" : "Declined"} (scope=${scope}).`);
|
|
979
1174
|
}
|
|
980
|
-
|
|
1175
|
+
case "cancel":
|
|
1176
|
+
conditionalByNegotiation.delete(negotiationId);
|
|
1177
|
+
quoteSources.delete(negotiationId);
|
|
1178
|
+
closedNegotiations.add(negotiationId);
|
|
1179
|
+
await advancePhase(negotiationId, { type: "cancel" });
|
|
1180
|
+
return textReply("Negotiation cancelled.");
|
|
1181
|
+
default:
|
|
1182
|
+
return { kind: "declined", reasonCode: "unsupported_action" };
|
|
981
1183
|
}
|
|
982
|
-
|
|
983
|
-
conditionalByNegotiation.delete(negotiationId);
|
|
984
|
-
closedNegotiations.add(negotiationId);
|
|
985
|
-
await advancePhase(negotiationId, { type: "cancel" });
|
|
986
|
-
return textReply("Negotiation cancelled.");
|
|
987
|
-
default:
|
|
988
|
-
return { kind: "declined", reasonCode: "unsupported_action" };
|
|
989
|
-
}
|
|
1184
|
+
});
|
|
990
1185
|
},
|
|
991
1186
|
};
|
|
992
1187
|
}
|