@harrylabsj/kiwi 0.12.2 → 0.12.4
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/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/server/merchant-handler.d.ts +0 -6
- package/dist/a2a/server/merchant-handler.js +591 -474
- package/dist/a2a/server/merchant-handler.js.map +1 -1
- 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 +37 -1
- 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/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 +1 -8
- package/dist/buyer-core/a2a-knp.js +15 -2
- package/dist/buyer-core/a2a-knp.js.map +1 -1
- package/dist/buyer-core/a2a-negotiator.d.ts +24 -19
- package/dist/buyer-core/a2a-negotiator.js +90 -24
- package/dist/buyer-core/a2a-negotiator.js.map +1 -1
- package/dist/buyer-core/a2a-quote-fetcher.d.ts +15 -3
- package/dist/buyer-core/a2a-quote-fetcher.js +131 -55
- 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/protocol-recovery.d.ts +67 -0
- package/dist/buyer-core/protocol-recovery.js +328 -0
- package/dist/buyer-core/protocol-recovery.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.js +5 -2
- 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/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.js +12 -0
- package/dist/cloud/product-import-tabular.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/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 +5 -19
- package/dist/discovery/catalog-source/kiwi-source.js +15 -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/order-record.js +25 -3
- package/dist/handoff/order-record.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.js +3 -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/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/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 +1 -18
- 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/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 +1 -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/negotiation-observer.d.ts +3 -0
- package/dist/merchant/negotiation-observer.js +33 -19
- package/dist/merchant/negotiation-observer.js.map +1 -1
- package/dist/merchant/operations-report.js +7 -3
- package/dist/merchant/operations-report.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.js +13 -7
- 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/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 +63 -27
- 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 +32 -8
- package/scripts/check-install-toolchain.mjs +69 -0
|
@@ -22,6 +22,7 @@ import { toMinorUnits as losslessToMinorUnits } from "../../protocol/legacy-shop
|
|
|
22
22
|
import { WORKBENCH_CURRENCY_TABLE_VERSION } from "../../merchant/application/money.js";
|
|
23
23
|
import { calculateWorkbenchQuote } from "../../merchant/quote-calculator.js";
|
|
24
24
|
import { createNegotiationPhase, isTerminalPhase, transitionPhase, } from "../../negotiation/state/phase.js";
|
|
25
|
+
import { NegotiationValidationError } from "../../negotiation/domain/common.js";
|
|
25
26
|
export const MERCHANT_CAPABILITY = "com.harrylabsj.kiwi.shopping.negotiation";
|
|
26
27
|
export const MERCHANT_SKU = "SKU-001";
|
|
27
28
|
export const MERCHANT_CURRENCY = "CNY";
|
|
@@ -34,6 +35,25 @@ export const MERCHANT_QUANTITY = 200;
|
|
|
34
35
|
* 省略 delivery_before——明确未知,绝不报静态/过期日期)。固定日期常量已从
|
|
35
36
|
* 生产 terms 路径移除。
|
|
36
37
|
*/
|
|
38
|
+
/**
|
|
39
|
+
* review 2-3(A319 返修):把商家自产的无条件 offer/counter_offer 归一为
|
|
40
|
+
* evaluateConditionalOffer 的**合法形状**——`conditions` 必须存在(数组,
|
|
41
|
+
* 无条件即空数组)。此前归一对象缺 conditions,accept 路径 evaluator 的
|
|
42
|
+
* conditions.forEach 直接 TypeError(RPC -32603),阻断直报价/还价接受、
|
|
43
|
+
* 错 digest 拒绝与 Ledger 重启恢复(A317 独验 4 红同根因)。
|
|
44
|
+
* 真实 conditional_offer(自带 conditions)**不经此归一**,原样登记。
|
|
45
|
+
*/
|
|
46
|
+
function normalizeDirectOfferAsConditional(offerId, rawTerms, quantity) {
|
|
47
|
+
return {
|
|
48
|
+
conditional: {
|
|
49
|
+
type: "conditional_offer",
|
|
50
|
+
offer_id: offerId,
|
|
51
|
+
base_terms: rawTerms,
|
|
52
|
+
conditions: [],
|
|
53
|
+
},
|
|
54
|
+
quantity,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
37
57
|
export function resolveDeliveryBefore(merchantPolicy, now) {
|
|
38
58
|
const leadDays = merchantPolicy?.delivery_lead_days;
|
|
39
59
|
if (leadDays === undefined)
|
|
@@ -193,6 +213,25 @@ export function createMerchantHandler(options) {
|
|
|
193
213
|
// conditionalByNegotiation 隐式表达,规范转换表不是接收消息的权威门)。
|
|
194
214
|
const phaseStateByNegotiation = new Map();
|
|
195
215
|
// 产生商业承诺的动作:终态后一律拒绝;text-only(clarification)放行。
|
|
216
|
+
const activeObjectByNegotiation = new Map();
|
|
217
|
+
const boundPeers = new Map();
|
|
218
|
+
const requestTails = new Map();
|
|
219
|
+
const serialize = async (key, fn) => {
|
|
220
|
+
const before = requestTails.get(key) ?? Promise.resolve();
|
|
221
|
+
let release;
|
|
222
|
+
const turn = new Promise((resolve) => { release = resolve; });
|
|
223
|
+
const tail = before.then(() => turn);
|
|
224
|
+
requestTails.set(key, tail);
|
|
225
|
+
await before;
|
|
226
|
+
try {
|
|
227
|
+
return await fn();
|
|
228
|
+
}
|
|
229
|
+
finally {
|
|
230
|
+
release();
|
|
231
|
+
if (requestTails.get(key) === tail)
|
|
232
|
+
requestTails.delete(key);
|
|
233
|
+
}
|
|
234
|
+
};
|
|
196
235
|
const COMMERCIAL_ACTIONS = new Set([
|
|
197
236
|
"inquiry",
|
|
198
237
|
"rfq",
|
|
@@ -200,6 +239,9 @@ export function createMerchantHandler(options) {
|
|
|
200
239
|
"counter_offer",
|
|
201
240
|
"conditional_offer",
|
|
202
241
|
"accept_nonbinding",
|
|
242
|
+
"withdraw",
|
|
243
|
+
"decline",
|
|
244
|
+
"cancel",
|
|
203
245
|
]);
|
|
204
246
|
/** 审查 BUG-10:把入站 action 映射到相位机事件并推进;非法转换(不在
|
|
205
247
|
* 转换表内的组合)fail-closed 返回 false(调用方 decline state_conflict)。
|
|
@@ -275,7 +317,7 @@ export function createMerchantHandler(options) {
|
|
|
275
317
|
state_transition: { from_phase: effectiveFrom, to_phase: toPhase },
|
|
276
318
|
identity: {
|
|
277
319
|
sender_identity: sender,
|
|
278
|
-
counterparty_identity: counterparty,
|
|
320
|
+
counterparty_identity: boundPeers.get(negotiationId) ?? counterparty,
|
|
279
321
|
actor: "merchant",
|
|
280
322
|
},
|
|
281
323
|
capability: { capability: MERCHANT_CAPABILITY, protocol_version: "1.0" },
|
|
@@ -413,7 +455,7 @@ export function createMerchantHandler(options) {
|
|
|
413
455
|
in_reply_to: reply.in_reply_to,
|
|
414
456
|
identity: {
|
|
415
457
|
sender_identity: sender,
|
|
416
|
-
counterparty_identity: counterparty,
|
|
458
|
+
counterparty_identity: boundPeers.get(reply.negotiation_id) ?? counterparty,
|
|
417
459
|
actor: reply.actor,
|
|
418
460
|
},
|
|
419
461
|
capability: {
|
|
@@ -425,6 +467,12 @@ export function createMerchantHandler(options) {
|
|
|
425
467
|
outcome: { kind: "ok" },
|
|
426
468
|
occurred_at: reply.created_at,
|
|
427
469
|
});
|
|
470
|
+
if (["offer", "counter_offer", "conditional_offer"].includes(reply.action)) {
|
|
471
|
+
activeObjectByNegotiation.set(reply.negotiation_id, {
|
|
472
|
+
offerId: String(reply.payload.offer_id),
|
|
473
|
+
messageId: reply.message_id, actor: reply.actor,
|
|
474
|
+
});
|
|
475
|
+
}
|
|
428
476
|
};
|
|
429
477
|
// 审查 BUG-03:从持久 Ledger 恢复状态(必须在 return 之前执行)——重启后
|
|
430
478
|
// 终态不得重开、已发出的 conditional offer 必须继续可被接受(此前
|
|
@@ -457,9 +505,18 @@ export function createMerchantHandler(options) {
|
|
|
457
505
|
envelope?.action === "counter_offer" ||
|
|
458
506
|
envelope?.action === "conditional_offer") {
|
|
459
507
|
const offerId = envelope.payload?.offer_id;
|
|
460
|
-
if (typeof offerId === "string" && offerId !== "")
|
|
508
|
+
if (typeof offerId === "string" && offerId !== "") {
|
|
461
509
|
activeOfferId = offerId;
|
|
510
|
+
if (event.message_id !== undefined)
|
|
511
|
+
activeObjectByNegotiation.set(negotiationId, {
|
|
512
|
+
offerId, messageId: event.message_id, actor: "merchant",
|
|
513
|
+
});
|
|
514
|
+
}
|
|
462
515
|
}
|
|
516
|
+
// review 2-3(A316 校准):重启恢复同样登记普通 offer/counter_offer——
|
|
517
|
+
// 归一为 {type:"conditional_offer", offer_id, base_terms}(与在途登记
|
|
518
|
+
// 同形),accept 按 offer_id 强匹配;只恢复 conditional 的旧行为使
|
|
519
|
+
// 重启后直报价/还价接受仍 offer_unknown。
|
|
463
520
|
if (envelope?.action === "conditional_offer") {
|
|
464
521
|
const payload = envelope.payload;
|
|
465
522
|
if (payload?.offer_id !== undefined) {
|
|
@@ -469,10 +526,20 @@ export function createMerchantHandler(options) {
|
|
|
469
526
|
};
|
|
470
527
|
}
|
|
471
528
|
}
|
|
529
|
+
else if (envelope?.action === "offer" || envelope?.action === "counter_offer") {
|
|
530
|
+
const payload = envelope.payload;
|
|
531
|
+
const rawTerms = envelope.action === "offer" ? payload?.terms : payload?.proposed_terms;
|
|
532
|
+
if (payload?.offer_id !== undefined && payload.offer_id !== "" && rawTerms !== undefined) {
|
|
533
|
+
// review 2-3(A319 返修):Ledger 恢复归一与在途登记同形(含
|
|
534
|
+
// conditions: [])——重启后直报价/还价接受不再 TypeError。
|
|
535
|
+
lastConditional = normalizeDirectOfferAsConditional(payload.offer_id, rawTerms, rawTerms.items?.[0]
|
|
536
|
+
?.quantity?.value ?? MERCHANT_QUANTITY);
|
|
537
|
+
}
|
|
538
|
+
}
|
|
472
539
|
}
|
|
473
540
|
// 审查 P1-07(跨重启残留,独立验收发现):终态相位同样必须恢复——相位
|
|
474
541
|
// 取自链上 state_transition 事实(非臆造)。否则 cancel/withdraw/decline
|
|
475
|
-
//
|
|
542
|
+
// 重启后若不恢复终态会让 advancePhase 从全新 OPEN 状态推进:
|
|
476
543
|
// 重放的 cancel 被当作新动作接受,并落 from_phase 伪造为 OPEN 的幻影
|
|
477
544
|
// 终态转换。恢复后由相位机 fail-closed(state_conflict),与进程内一致。
|
|
478
545
|
phaseStateByNegotiation.set(negotiationId, {
|
|
@@ -498,495 +565,545 @@ export function createMerchantHandler(options) {
|
|
|
498
565
|
return {
|
|
499
566
|
name: "kiwi-agent-serve-merchant",
|
|
500
567
|
async handle(ctx) {
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
568
|
+
return serialize(ctx.envelope.negotiation_id, async () => {
|
|
569
|
+
const envelope = ctx.envelope;
|
|
570
|
+
const negotiationId = envelope.negotiation_id;
|
|
571
|
+
const inReplyTo = envelope.message_id;
|
|
572
|
+
const binding = ledger.ensureCounterpartyBinding({
|
|
573
|
+
negotiationId, localIdentity: sender, senderIdentity: ctx.senderIdentity, actor: envelope.actor,
|
|
574
|
+
});
|
|
575
|
+
if (binding === "mismatch")
|
|
576
|
+
return { kind: "declined", reasonCode: "authorization_failed" };
|
|
577
|
+
if (binding === "unresolved")
|
|
578
|
+
return {
|
|
579
|
+
kind: "error", protocolCode: "reconciliation_required", message: "legacy negotiation owner is not proven",
|
|
580
|
+
};
|
|
581
|
+
boundPeers.set(negotiationId, ctx.senderIdentity);
|
|
582
|
+
// 审查 P2-D:终态不得以同一 negotiation_id 重开(§17.4/§21.2)。
|
|
583
|
+
if (closedNegotiations.has(negotiationId) && COMMERCIAL_ACTIONS.has(envelope.action)) {
|
|
516
584
|
return declineReply("state_conflict");
|
|
517
585
|
}
|
|
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");
|
|
586
|
+
if ((envelope.action === "withdraw" || envelope.action === "decline") &&
|
|
587
|
+
envelope.payload.scope === "offer") {
|
|
588
|
+
const target = envelope.payload;
|
|
589
|
+
const activeId = phaseStateByNegotiation.get(negotiationId)?.active_offer_id;
|
|
590
|
+
const object = activeObjectByNegotiation.get(negotiationId);
|
|
591
|
+
if (object === undefined || object.offerId !== activeId ||
|
|
592
|
+
target.target_message_id !== object.messageId ||
|
|
593
|
+
(target.target_offer_id !== undefined && target.target_offer_id !== activeId)) {
|
|
594
|
+
return { kind: "declined", reasonCode: "offer_unknown" };
|
|
756
595
|
}
|
|
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);
|
|
596
|
+
const expectedAuthor = envelope.action === "withdraw" ? "buyer" : "merchant";
|
|
597
|
+
if (object.actor !== expectedAuthor)
|
|
598
|
+
return { kind: "declined", reasonCode: "authorization_failed" };
|
|
830
599
|
}
|
|
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) {
|
|
600
|
+
// 审查 BUG-10:每个入站 action 经规范转换表推进相位——非法转换
|
|
601
|
+
// fail-closed(state_conflict)。accept 的推进在 §15 校验通过后
|
|
602
|
+
// (见 accept 分支:被拒的 accept 不得进入 AGREEMENT_REACHED)。
|
|
603
|
+
const phaseEvent = actionToPhaseEvent(envelope);
|
|
604
|
+
const isAcceptAction = envelope.action === "accept_nonbinding";
|
|
605
|
+
if (phaseEvent !== undefined && !isAcceptAction) {
|
|
606
|
+
const advanced = await advancePhase(negotiationId, phaseEvent);
|
|
607
|
+
if (!advanced) {
|
|
864
608
|
return declineReply("state_conflict");
|
|
865
609
|
}
|
|
866
|
-
|
|
867
|
-
|
|
610
|
+
if (["offer", "counter_offer", "conditional_offer"].includes(envelope.action)) {
|
|
611
|
+
activeObjectByNegotiation.set(negotiationId, {
|
|
612
|
+
offerId: String(envelope.payload.offer_id),
|
|
613
|
+
messageId: envelope.message_id, actor: envelope.actor,
|
|
614
|
+
});
|
|
615
|
+
}
|
|
868
616
|
}
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
if (
|
|
882
|
-
return
|
|
617
|
+
// merchant 定价是**确定性**的(不依赖 LLM):floor / 促销是 merchant 自己的
|
|
618
|
+
// 可配置策略。per-SKU 私有 floor(major→minor lossless;SKU 未列出用全局
|
|
619
|
+
// 默认)。审查 P1:底价已配置但无法无损换算(如 85.005)→ 返回 null,
|
|
620
|
+
// 调用方必须 decline 该 SKU 报价——绝不落到 0(等于关闭底价护栏,
|
|
621
|
+
// fail-open 可低于底价成交),与商品价 lossy 即拒绝的方向保持一致。
|
|
622
|
+
/** 数量校验(审查 P2):数量存在时必须是正整数——0/负数/小数按结构化
|
|
623
|
+
* 无效拒绝,不进 offer terms(否则批量价/条件成交按无意义数量求值);
|
|
624
|
+
* 未报数量沿用 MERCHANT_QUANTITY 缺省口径(不变)。 */
|
|
625
|
+
const invalidQuantity = (raw) => raw !== undefined && (typeof raw !== "number" || !Number.isInteger(raw) || raw <= 0);
|
|
626
|
+
const floorMinorForSku = (sku) => {
|
|
627
|
+
const mp = policyOf();
|
|
628
|
+
const floorValue = mp?.price_floors?.[sku] ?? mp?.min_unit_price_private;
|
|
629
|
+
if (floorValue === undefined)
|
|
630
|
+
return 0;
|
|
631
|
+
const conv = losslessToMinorUnits(floorValue, 2);
|
|
632
|
+
return conv.lossless ? conv.amount_minor : null;
|
|
633
|
+
};
|
|
634
|
+
/** 权威交期(V2 §8.5 P0-1):配置了 merchant_policy.delivery_lead_days 才
|
|
635
|
+
* 报具体 delivery_before(报价时间+天数,动态计算不过期);未配置 → undefined,
|
|
636
|
+
* terms 省略 delivery_before(明确未知)。 */
|
|
637
|
+
const deliveryBefore = () => resolveDeliveryBefore(policyOf(), now());
|
|
638
|
+
/** 买家还价(major→minor;KNP 里 buyer counter 的 unit_price)。 */
|
|
639
|
+
const clampToBounds = (minor, floor, list) => Math.min(list, Math.max(minor, floor));
|
|
640
|
+
/**
|
|
641
|
+
* 对买家可见的**自动折扣边界**(公开策略值:max_auto_discount_percent)。
|
|
642
|
+
*
|
|
643
|
+
* 安全要点(T045「不泄露底价」):商家对买家的任何报价都必须 clamp 到
|
|
644
|
+
* **公开边界**而不是 clamp 到私有底价——把还价 clamp 到底价再回给买家,
|
|
645
|
+
* 等于一次极低还价即可探出私有底价(一次探测即泄露精确值)。
|
|
646
|
+
* 私有底价只用来判断「这个价能不能自动报」,绝不出现在返回的条款里。
|
|
647
|
+
*/
|
|
648
|
+
/**
|
|
649
|
+
* 运行中规则的摘要(T047):接受成交前必须重验——商家改了规则/促销/底价后,
|
|
650
|
+
* 旧 conditional 的许可即失效,不能按旧价成交(设计 §10.2「审批时商品/规则
|
|
651
|
+
* 已变化 → 使旧候选失效或重新确认,不能执行过期许可」)。
|
|
652
|
+
*/
|
|
653
|
+
const policyDigest = () => contentDigest((policyOf() ?? {}));
|
|
654
|
+
/** 商品事实指纹(T047):价格/币种变化同样使旧许可失效。 */
|
|
655
|
+
const productFingerprint = (facts) => contentDigest({
|
|
656
|
+
sku: facts.sku,
|
|
657
|
+
priceMinor: facts.priceMinor,
|
|
658
|
+
currency: facts.currency,
|
|
659
|
+
});
|
|
660
|
+
const publicDiscountBoundMinor = (listMinor, sku) => {
|
|
661
|
+
const policy = policyOf();
|
|
662
|
+
const autoRaw = policy?.max_auto_discount_percent;
|
|
663
|
+
const auto = typeof autoRaw === "number" && Number.isFinite(autoRaw) && autoRaw > 0 ? autoRaw : 0;
|
|
664
|
+
// 促销本身是**公开条款**(promos[sku].bulk_discount_percent),因此公开边界
|
|
665
|
+
// 取其与自动折扣的较大者;未公布任何折扣时边界 = list(不自动折扣)。
|
|
666
|
+
const promoPct = policy?.promos?.[sku]?.bulk_discount_percent ?? 0;
|
|
667
|
+
const pct = Math.max(auto, Number.isFinite(promoPct) && promoPct > 0 ? promoPct : 0);
|
|
668
|
+
return applyDiscountPercentMinor(listMinor, pct);
|
|
669
|
+
};
|
|
670
|
+
/**
|
|
671
|
+
* 商品事实闸门(T046):数据过期或数量超过可得库存 → 明确不可报价。
|
|
672
|
+
* 设计 §10.1:「数据到期后停止相关自动报价或注明需人工确认,不把库存未知
|
|
673
|
+
* 说成有货」。**不在这里做任何兜底定价**(演示价已在配置层被云端拒绝)。
|
|
674
|
+
*/
|
|
675
|
+
const factsUnusable = (facts, quantity) => {
|
|
676
|
+
if (facts.valid_until !== undefined) {
|
|
677
|
+
const until = Date.parse(facts.valid_until);
|
|
678
|
+
if (!Number.isFinite(until) || until < Date.parse(now()))
|
|
679
|
+
return true;
|
|
883
680
|
}
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
return declineReply("offer_expired");
|
|
898
|
-
}
|
|
681
|
+
if (facts.stock !== undefined && quantity > facts.stock)
|
|
682
|
+
return true;
|
|
683
|
+
return false;
|
|
684
|
+
};
|
|
685
|
+
switch (envelope.action) {
|
|
686
|
+
case "inquiry": {
|
|
687
|
+
// 入站消息由 A2A pipeline 统一落 message_received(§22);这里不再
|
|
688
|
+
// 重复落账——此前 appendSent(envelope) 把买家的 inquiry 记为 merchant
|
|
689
|
+
// 自己"发送"(sender=merchant),审计语义错位且重复。
|
|
690
|
+
const resolved = await resolveProductOrDecline(MERCHANT_SKU);
|
|
691
|
+
if ("declined" in resolved)
|
|
692
|
+
return resolved.declined;
|
|
693
|
+
return textReply(`We carry ${resolved.product.title ?? MERCHANT_SKU} at ${(resolved.product.priceMinor / 100).toFixed(2)} ${resolved.product.currency}/piece; ask for delivery details.`);
|
|
899
694
|
}
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
695
|
+
case "rfq": {
|
|
696
|
+
const payload = envelope.payload;
|
|
697
|
+
const sku = payload.items?.[0]?.sku ?? MERCHANT_SKU;
|
|
698
|
+
const quantityRaw = payload.items?.[0]?.quantity?.value;
|
|
699
|
+
if (invalidQuantity(quantityRaw))
|
|
700
|
+
return declineReply("schema_invalid");
|
|
701
|
+
const quantity = quantityRaw ?? MERCHANT_QUANTITY;
|
|
702
|
+
const floorMinor = floorMinorForSku(sku);
|
|
703
|
+
if (floorMinor === null)
|
|
704
|
+
return declineReply("temporarily_unavailable");
|
|
705
|
+
const resolved = await resolveProductOrDecline(sku);
|
|
706
|
+
if ("declined" in resolved)
|
|
707
|
+
return resolved.declined;
|
|
708
|
+
const product = resolved.product;
|
|
709
|
+
const { priceMinor, currency, note, handoff_destination } = product;
|
|
710
|
+
if (factsUnusable(product, quantity))
|
|
711
|
+
return declineReply("temporarily_unavailable");
|
|
712
|
+
const promotion = resolvePromotionQuote(sku, quantity, priceMinor, currency, floorMinor);
|
|
713
|
+
if (promotion.kind === "unavailable")
|
|
714
|
+
return declineReply("temporarily_unavailable");
|
|
715
|
+
if (promotion.kind === "invalid")
|
|
716
|
+
return declineReply("approval_required");
|
|
717
|
+
// 确定性:offer = list 价(公开价)。list < floor 是配置矛盾——此时
|
|
718
|
+
// 用 floor 兜底会把私有底价直接报给买家(T045 泄露面),因此拒绝自动报价。
|
|
719
|
+
const effectivePriceMinor = promotion.kind === "applied" ? promotion.priceMinor : priceMinor;
|
|
720
|
+
if (effectivePriceMinor < floorMinor)
|
|
721
|
+
return declineReply("approval_required");
|
|
722
|
+
const reply = seedEnvelope({
|
|
723
|
+
negotiation_id: negotiationId,
|
|
724
|
+
in_reply_to: inReplyTo,
|
|
725
|
+
actor: "merchant",
|
|
726
|
+
action: "offer",
|
|
727
|
+
created_at: now(),
|
|
728
|
+
payload: {
|
|
729
|
+
type: "offer",
|
|
730
|
+
offer_id: newOfferId(),
|
|
731
|
+
terms: offerTerms({
|
|
732
|
+
sku,
|
|
733
|
+
priceMinor: effectivePriceMinor,
|
|
734
|
+
quantity,
|
|
735
|
+
currency,
|
|
736
|
+
handoff_destination,
|
|
737
|
+
deliveryBefore: deliveryBefore(),
|
|
738
|
+
...(promotion.kind === "applied" ? { validUntilCap: promotion.endsAt } : {}),
|
|
739
|
+
}, now()),
|
|
740
|
+
},
|
|
741
|
+
...(note !== undefined ? { public_message: note } : {}),
|
|
742
|
+
});
|
|
743
|
+
// 审查 BUG-10:merchant 侧相位由自己的出站动作推进——rfq 的 offer
|
|
744
|
+
// 回复是 OPEN→OFFER_OPEN 的边(入站侧 rfq 是起始动作无事件)。
|
|
745
|
+
// 审查 P2:推进被拒(非法转换)→ 不落账不发出,防链上事实与相位机分裂。
|
|
746
|
+
const advanced = await advancePhase(negotiationId, {
|
|
747
|
+
type: "offer",
|
|
748
|
+
offer_id: String(reply.payload.offer_id ?? ""),
|
|
749
|
+
});
|
|
750
|
+
if (!advanced)
|
|
751
|
+
return declineReply("state_conflict");
|
|
752
|
+
// review 2-3:直报价登记为可接受对象——此前只有 counter_offer 分支
|
|
753
|
+
// 登记,买家 accept 直报价必得 offer_unknown(§15/§21.2 主路径在
|
|
754
|
+
// 对外合规实现上互操作断裂)。以 base_terms 规约形状存储,accept
|
|
755
|
+
// 路径的 evaluateConditionalOffer 对无条件条件即返回 base terms。
|
|
756
|
+
// review 2-3(返修):登记对象必须带 offer_id——accept 路径读
|
|
757
|
+
// stored.conditional.offer_id 与 accept 的 offer_id 强匹配(A314
|
|
758
|
+
// 源码核:原返修缺 offer_id,直报价接受仍 offer_unknown)。
|
|
759
|
+
conditionalByNegotiation.set(negotiationId, normalizeDirectOfferAsConditional(String(reply.payload.offer_id ?? ""), reply.payload.terms ?? {}, quantity));
|
|
760
|
+
await appendSent(reply);
|
|
761
|
+
return envelopeReply(reply);
|
|
903
762
|
}
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
763
|
+
case "offer": {
|
|
764
|
+
// 商家还价:对 buyer 的 offer 回 counter_offer(真实商品价)。
|
|
765
|
+
const buyerOffer = envelope.payload;
|
|
766
|
+
const sku = buyerOffer.terms?.items?.[0]?.sku ?? MERCHANT_SKU;
|
|
767
|
+
const quantityRaw = buyerOffer.terms?.items?.[0]?.quantity?.value;
|
|
768
|
+
if (invalidQuantity(quantityRaw))
|
|
769
|
+
return declineReply("schema_invalid");
|
|
770
|
+
const quantity = quantityRaw ?? MERCHANT_QUANTITY;
|
|
771
|
+
const floorMinor = floorMinorForSku(sku);
|
|
772
|
+
if (floorMinor === null)
|
|
773
|
+
return declineReply("temporarily_unavailable");
|
|
774
|
+
const resolved = await resolveProductOrDecline(sku);
|
|
775
|
+
if ("declined" in resolved)
|
|
776
|
+
return resolved.declined;
|
|
777
|
+
const product = resolved.product;
|
|
778
|
+
const { priceMinor, currency, note, handoff_destination } = product;
|
|
779
|
+
if (factsUnusable(product, quantity))
|
|
780
|
+
return declineReply("temporarily_unavailable");
|
|
781
|
+
const promotion = resolvePromotionQuote(sku, quantity, priceMinor, currency, floorMinor);
|
|
782
|
+
if (promotion.kind === "unavailable")
|
|
783
|
+
return declineReply("temporarily_unavailable");
|
|
784
|
+
if (promotion.kind === "invalid")
|
|
785
|
+
return declineReply("approval_required");
|
|
786
|
+
// 确定性:counter = list 价(公开价);list < floor 时拒绝自动报价
|
|
787
|
+
// (用 floor 兜底=把底价报给买家)。
|
|
788
|
+
const effectivePriceMinor = promotion.kind === "applied" ? promotion.priceMinor : priceMinor;
|
|
789
|
+
if (effectivePriceMinor < floorMinor)
|
|
790
|
+
return declineReply("approval_required");
|
|
791
|
+
const reply = seedEnvelope({
|
|
792
|
+
negotiation_id: negotiationId,
|
|
793
|
+
in_reply_to: inReplyTo,
|
|
794
|
+
actor: "merchant",
|
|
795
|
+
action: "counter_offer",
|
|
796
|
+
created_at: now(),
|
|
797
|
+
payload: {
|
|
798
|
+
type: "counter_offer",
|
|
799
|
+
offer_id: newOfferId(),
|
|
800
|
+
responding_to_offer_id: buyerOffer.offer_id ?? "",
|
|
801
|
+
proposed_terms: offerTerms({
|
|
802
|
+
sku,
|
|
803
|
+
priceMinor: effectivePriceMinor,
|
|
804
|
+
quantity,
|
|
805
|
+
currency,
|
|
806
|
+
handoff_destination,
|
|
807
|
+
deliveryBefore: deliveryBefore(),
|
|
808
|
+
...(promotion.kind === "applied" ? { validUntilCap: promotion.endsAt } : {}),
|
|
809
|
+
}, now()),
|
|
810
|
+
},
|
|
811
|
+
...(note !== undefined ? { public_message: note } : {}),
|
|
812
|
+
});
|
|
813
|
+
// 审查 P2:推进被拒 → 不落账不发出(防 OFFER_OPEN 下重复还价分裂)。
|
|
814
|
+
const advanced = await advancePhase(negotiationId, {
|
|
815
|
+
type: "counter_offer",
|
|
816
|
+
offer_id: String(reply.payload.offer_id ?? ""),
|
|
817
|
+
});
|
|
818
|
+
if (!advanced)
|
|
819
|
+
return declineReply("state_conflict");
|
|
820
|
+
// review 2-3:还价(counter_offer)同样登记——买家接受直还价走通主路径。
|
|
821
|
+
conditionalByNegotiation.set(negotiationId, normalizeDirectOfferAsConditional(String(reply.payload.offer_id ?? ""), reply.payload.proposed_terms ?? {}, quantity));
|
|
822
|
+
await appendSent(reply);
|
|
823
|
+
return envelopeReply(reply);
|
|
911
824
|
}
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
const
|
|
916
|
-
if (
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
825
|
+
case "counter_offer": {
|
|
826
|
+
const counter = envelope.payload;
|
|
827
|
+
const sku = counter.proposed_terms?.items?.[0]?.sku ?? MERCHANT_SKU;
|
|
828
|
+
const quantityRaw = counter.proposed_terms?.items?.[0]?.quantity?.value;
|
|
829
|
+
if (invalidQuantity(quantityRaw))
|
|
830
|
+
return declineReply("schema_invalid");
|
|
831
|
+
const quantity = quantityRaw ?? MERCHANT_QUANTITY;
|
|
832
|
+
const buyerCounterMinor = counter.proposed_terms?.items?.[0]?.unit_price?.amount_minor;
|
|
833
|
+
const floorMinor = floorMinorForSku(sku);
|
|
834
|
+
if (floorMinor === null)
|
|
835
|
+
return declineReply("temporarily_unavailable");
|
|
836
|
+
const resolved = await resolveProductOrDecline(sku);
|
|
837
|
+
if ("declined" in resolved)
|
|
838
|
+
return resolved.declined;
|
|
839
|
+
const product = resolved.product;
|
|
840
|
+
const { priceMinor, currency, note, handoff_destination } = product;
|
|
841
|
+
if (factsUnusable(product, quantity))
|
|
842
|
+
return declineReply("temporarily_unavailable");
|
|
843
|
+
const authorityPromotion = resolvePromotionQuote(sku, quantity, priceMinor, currency, floorMinor);
|
|
844
|
+
if (authorityPromotion.kind === "unavailable") {
|
|
845
|
+
return declineReply("temporarily_unavailable");
|
|
846
|
+
}
|
|
847
|
+
if (authorityPromotion.kind === "invalid")
|
|
848
|
+
return declineReply("approval_required");
|
|
849
|
+
// 确定性(无 LLM):买家还价只在**公开折扣边界**内响应——
|
|
850
|
+
// 边界 = list×(1-max_auto_discount_percent),是公开策略值;
|
|
851
|
+
// 高于 list 压到 list,低于公开边界抬到公开边界。
|
|
852
|
+
// 关键:绝不把还价 clamp 到私有底价再回给买家(那样一次极低还价
|
|
853
|
+
// 即可探出底价精确值,T045)。
|
|
854
|
+
const publicBoundMinor = publicDiscountBoundMinor(priceMinor, sku);
|
|
855
|
+
const policyResponsiveMinor = typeof buyerCounterMinor === "number" && Number.isFinite(buyerCounterMinor)
|
|
856
|
+
? clampToBounds(buyerCounterMinor, publicBoundMinor, priceMinor)
|
|
857
|
+
: priceMinor;
|
|
858
|
+
const responsiveMinor = authorityPromotion.kind === "applied"
|
|
859
|
+
? Math.min(policyResponsiveMinor, authorityPromotion.priceMinor)
|
|
860
|
+
: policyResponsiveMinor;
|
|
861
|
+
if (responsiveMinor < floorMinor) {
|
|
862
|
+
// 公开边界低于私有底价:该区间不能自动报价,交人工/审批处理,且不返回价格。
|
|
923
863
|
return declineReply("approval_required");
|
|
924
864
|
}
|
|
865
|
+
// 可配置促销(merchant_policy.promos[sku]):买满 bulk_threshold 台,
|
|
866
|
+
// 批量价 = min(还价响应, list×(1-d%/100)),同样 clamp 到公开边界;
|
|
867
|
+
// 若该价低于私有底价则**不挂这条条件**(不广告无法兑现的价)。
|
|
868
|
+
const promo = policyOf()?.promos?.[sku];
|
|
869
|
+
const bulkThreshold = promo?.bulk_threshold ?? DEFAULT_BULK_THRESHOLD;
|
|
870
|
+
const bulkMinor = promo !== undefined
|
|
871
|
+
? clampToBounds(Math.min(responsiveMinor, applyDiscountPercentMinor(priceMinor, promo.bulk_discount_percent ?? 0)), publicBoundMinor, priceMinor)
|
|
872
|
+
: undefined;
|
|
873
|
+
const promotionalMinor = bulkMinor !== undefined && bulkMinor < responsiveMinor && bulkMinor >= floorMinor
|
|
874
|
+
? bulkMinor
|
|
875
|
+
: undefined;
|
|
876
|
+
const reply = seedEnvelope({
|
|
877
|
+
negotiation_id: negotiationId,
|
|
878
|
+
in_reply_to: inReplyTo,
|
|
879
|
+
actor: "merchant",
|
|
880
|
+
action: "conditional_offer",
|
|
881
|
+
created_at: now(),
|
|
882
|
+
payload: {
|
|
883
|
+
type: "conditional_offer",
|
|
884
|
+
offer_id: newOfferId(),
|
|
885
|
+
responding_to_offer_id: counter.offer_id,
|
|
886
|
+
// 许可绑定(T047):本次报价依据的规则摘要与商品事实指纹;accept 时重验。
|
|
887
|
+
policy_digest: policyDigest(),
|
|
888
|
+
product_fingerprint: productFingerprint({ sku, priceMinor, currency }),
|
|
889
|
+
// base = 还价响应(已 clamp 到 [floor, list])——单台即得到该价。
|
|
890
|
+
base_terms: offerTerms({
|
|
891
|
+
sku,
|
|
892
|
+
priceMinor: responsiveMinor,
|
|
893
|
+
quantity,
|
|
894
|
+
currency,
|
|
895
|
+
handoff_destination,
|
|
896
|
+
deliveryBefore: deliveryBefore(),
|
|
897
|
+
...(authorityPromotion.kind === "applied"
|
|
898
|
+
? { validUntilCap: authorityPromotion.endsAt }
|
|
899
|
+
: {}),
|
|
900
|
+
}, now()),
|
|
901
|
+
// conditions 可空(§13:空 conditions 等价 base_terms)。促销价低于
|
|
902
|
+
// 私有底价时不挂条件——不广告无法兑现的价,也不借 then_terms 泄露底价。
|
|
903
|
+
conditions: promotionalMinor !== undefined
|
|
904
|
+
? [
|
|
905
|
+
{
|
|
906
|
+
when: {
|
|
907
|
+
all: [
|
|
908
|
+
{ field: "aggregate.total_quantity", op: "gte", value: bulkThreshold },
|
|
909
|
+
],
|
|
910
|
+
},
|
|
911
|
+
then_terms: offerTerms({
|
|
912
|
+
sku,
|
|
913
|
+
priceMinor: promotionalMinor,
|
|
914
|
+
quantity,
|
|
915
|
+
currency,
|
|
916
|
+
handoff_destination,
|
|
917
|
+
deliveryBefore: deliveryBefore(),
|
|
918
|
+
}, now()),
|
|
919
|
+
},
|
|
920
|
+
]
|
|
921
|
+
: [],
|
|
922
|
+
},
|
|
923
|
+
...(note !== undefined ? { public_message: note } : {}),
|
|
924
|
+
});
|
|
925
|
+
conditionalByNegotiation.set(negotiationId, {
|
|
926
|
+
conditional: reply.payload,
|
|
927
|
+
quantity,
|
|
928
|
+
});
|
|
929
|
+
// 审查 P2:推进被拒 → 不落账不发出。
|
|
930
|
+
const advanced = await advancePhase(negotiationId, {
|
|
931
|
+
type: "conditional_offer",
|
|
932
|
+
offer_id: String(reply.payload.offer_id ?? ""),
|
|
933
|
+
});
|
|
934
|
+
if (!advanced)
|
|
935
|
+
return declineReply("state_conflict");
|
|
936
|
+
await appendSent(reply);
|
|
937
|
+
return envelopeReply(reply);
|
|
925
938
|
}
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
+
case "clarification": {
|
|
940
|
+
const questions = envelope.payload.questions ?? [];
|
|
941
|
+
// 方案B(协议完整):商家的应答是结构化 clarification_response
|
|
942
|
+
// envelope(§8.2),in_reply_to 引用被回答的澄清消息(§8.5/§14
|
|
943
|
+
// 强制,finalizeEnvelope 校验)。payload 形状规范未冻结(§14),
|
|
944
|
+
// 携带 answers 便于对端结构化消费;人类可读文本走 public_message。
|
|
945
|
+
// 交期口径(V2 §8.5 P0-1):配置了权威交期才报具体日期;未配置
|
|
946
|
+
// 明确告知需与商家确认,绝不报静态/过期日期。
|
|
947
|
+
const promised = deliveryBefore();
|
|
948
|
+
const answerText = promised !== undefined
|
|
949
|
+
? `delivery before ${promised}, payment terms negotiable (nonbinding)`
|
|
950
|
+
: "delivery timing to be confirmed with the merchant, payment terms negotiable (nonbinding)";
|
|
951
|
+
const reply = seedEnvelope({
|
|
952
|
+
negotiation_id: negotiationId,
|
|
953
|
+
in_reply_to: inReplyTo,
|
|
954
|
+
actor: "merchant",
|
|
955
|
+
action: "clarification_response",
|
|
956
|
+
created_at: now(),
|
|
957
|
+
payload: {
|
|
958
|
+
type: "clarification_response",
|
|
959
|
+
answers: questions.map((q) => ({
|
|
960
|
+
field: q.field ?? "…",
|
|
961
|
+
answer: answerText,
|
|
962
|
+
})),
|
|
963
|
+
},
|
|
964
|
+
public_message: `${answerText}.`,
|
|
965
|
+
});
|
|
966
|
+
// 出站应答即恢复(restore 边,§21.2):顶层推进已把相位挂到
|
|
967
|
+
// AWAITING_CLARIFICATION,clarification_response 同轮弹回
|
|
968
|
+
// resume_phase(OFFER_OPEN/OPEN),否则「问一句再 accept」的
|
|
969
|
+
// happy path 会被相位机永久 state_conflict。恢复失败 fail-closed。
|
|
970
|
+
const restored = await advancePhase(negotiationId, { type: "clarification_response" });
|
|
971
|
+
if (!restored) {
|
|
972
|
+
return declineReply("state_conflict");
|
|
973
|
+
}
|
|
974
|
+
await appendSent(reply);
|
|
975
|
+
return envelopeReply(reply);
|
|
939
976
|
}
|
|
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
|
-
|
|
977
|
+
case "accept_nonbinding": {
|
|
978
|
+
const stored = conditionalByNegotiation.get(negotiationId);
|
|
979
|
+
const acceptPayload = envelope.payload;
|
|
980
|
+
const acceptedOfferId = acceptPayload.offer_id ?? "";
|
|
981
|
+
// 成交价必须来自本磋商已发出的 conditional_offer:无前置 conditional
|
|
982
|
+
// 或 accept 的 offer_id 与所存 conditional 不匹配 → 拒绝成交。此前
|
|
983
|
+
// 无前置时回退基础价照样产出 agreement,从未发出的 offer_id 也照样
|
|
984
|
+
// 成交(agreement 指向不存在的 offer,审计溯源断裂)。
|
|
985
|
+
const storedOfferId = stored?.conditional?.offer_id ?? "";
|
|
986
|
+
const acceptedConditional = stored !== undefined && acceptedOfferId !== "" && acceptedOfferId === storedOfferId
|
|
987
|
+
? stored
|
|
988
|
+
: undefined;
|
|
989
|
+
if (acceptedConditional === undefined) {
|
|
990
|
+
return declineReply("offer_unknown");
|
|
991
|
+
}
|
|
992
|
+
// KNP §15(审查 P2-C,实验复现):terms_digest 必须是 agreed terms
|
|
993
|
+
// 的 canonical digest——错误 digest 此前也产出 agreement。任一检查
|
|
994
|
+
// 失败都不得创建 agreement。
|
|
995
|
+
// review 3-15(返修,实际实施):evaluator 对冲突规则抛
|
|
996
|
+
// NegotiationValidationError("condition_conflict")——不捕获会被管线
|
|
997
|
+
// 收敛成 -32603 internal error。已知协议校验错误映射为对应协议码
|
|
998
|
+
// decline;**未知错误原样上抛**(claim 不清、结果按 unknown 对账,
|
|
999
|
+
// 绝不假纯失败)。
|
|
1000
|
+
let agreedTerms;
|
|
1001
|
+
try {
|
|
1002
|
+
agreedTerms = evaluateConditionalOffer(acceptedConditional.conditional, {
|
|
1003
|
+
"aggregate.total_quantity": acceptedConditional.quantity,
|
|
1004
|
+
});
|
|
1005
|
+
}
|
|
1006
|
+
catch (err) {
|
|
1007
|
+
if (err instanceof NegotiationValidationError) {
|
|
1008
|
+
return declineReply(err.code === "condition_conflict" ? "condition_conflict" : "schema_invalid");
|
|
1009
|
+
}
|
|
1010
|
+
throw err;
|
|
1011
|
+
}
|
|
1012
|
+
// 审查 P3:§11 offer 过期校验——valid_until 已过 → 拒绝(此前
|
|
1013
|
+
// valid_until 硬编码 2099 永不生效,陈旧报价可被无限期接受)。
|
|
1014
|
+
const validUntil = agreedTerms?.valid_until;
|
|
1015
|
+
if (validUntil !== undefined) {
|
|
1016
|
+
const validUntilMs = Date.parse(validUntil);
|
|
1017
|
+
const nowMs = Date.parse(now());
|
|
1018
|
+
if (!Number.isFinite(validUntilMs) || !Number.isFinite(nowMs) || validUntilMs < nowMs) {
|
|
1019
|
+
return declineReply("offer_expired");
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
const presentedDigest = acceptPayload.terms_digest ?? "";
|
|
1023
|
+
if (presentedDigest === "" || presentedDigest !== contentDigest(agreedTerms)) {
|
|
1024
|
+
return declineReply("terms_digest_mismatch");
|
|
1025
|
+
}
|
|
1026
|
+
// T047:许可有效性重验——规则或商品事实在报价之后发生变化,旧许可即失效。
|
|
1027
|
+
// 必须在相位推进/产出协议之前判定(拒绝不得留下任何终态副作用)。
|
|
1028
|
+
const acceptedConditionalPayload = acceptedConditional.conditional;
|
|
1029
|
+
const currentPolicyDigest = policyDigest();
|
|
1030
|
+
if (acceptedConditionalPayload.policy_digest !== undefined &&
|
|
1031
|
+
acceptedConditionalPayload.policy_digest !== currentPolicyDigest) {
|
|
1032
|
+
return declineReply("approval_required");
|
|
1033
|
+
}
|
|
1034
|
+
const acceptedSku = acceptedConditionalPayload.base_terms?.items?.[0]?.sku;
|
|
1035
|
+
if (acceptedConditionalPayload.product_fingerprint !== undefined &&
|
|
1036
|
+
acceptedSku !== undefined) {
|
|
1037
|
+
const refreshed = await resolveProductOrDecline(acceptedSku, { force: true });
|
|
1038
|
+
if ("declined" in refreshed ||
|
|
1039
|
+
productFingerprint({
|
|
1040
|
+
sku: acceptedSku,
|
|
1041
|
+
priceMinor: refreshed.product.priceMinor,
|
|
1042
|
+
currency: refreshed.product.currency,
|
|
1043
|
+
}) !== acceptedConditionalPayload.product_fingerprint) {
|
|
1044
|
+
// 商品价/币种变化或商品源已不可用 → 旧许可失效,需重新确认。
|
|
1045
|
+
return declineReply("approval_required");
|
|
1046
|
+
}
|
|
1047
|
+
}
|
|
1048
|
+
// 审查 P1-C:相位机是权威守卫——§15 校验通过后、构建协议之前先
|
|
1049
|
+
// 推进相位并检查返回值(BUG-10 语义不变:被拒的 accept 不得进入
|
|
1050
|
+
// AGREEMENT_REACHED)。推进失败(如 AWAITING_CLARIFICATION 下直接
|
|
1051
|
+
// accept)fail-closed:不返回协议、不进 closedNegotiations、不删
|
|
1052
|
+
// conditional,无任何终态副作用——此前忽略返回值,协议照发但相位
|
|
1053
|
+
// 机拒绝推进;进程内 closedNegotiations 挡住二次 accept,重启恢复
|
|
1054
|
+
// 后相位重建为 AWAITING_CLARIFICATION,二次 accept 产出重复协议。
|
|
1055
|
+
const advanced = await advancePhase(negotiationId, {
|
|
1056
|
+
type: "accept_nonbinding",
|
|
1057
|
+
offer_id: acceptedOfferId,
|
|
1058
|
+
});
|
|
1059
|
+
if (!advanced) {
|
|
1060
|
+
return declineReply("state_conflict");
|
|
1061
|
+
}
|
|
1062
|
+
const agreement = buildAgreement({
|
|
1063
|
+
negotiation_id: negotiationId,
|
|
1064
|
+
accepted_offer_id: acceptedOfferId,
|
|
1065
|
+
agreed_terms: agreedTerms,
|
|
1066
|
+
created_at: now(),
|
|
1067
|
+
});
|
|
1068
|
+
const artifactPart = { kind: "data", data: { agreement } };
|
|
1069
|
+
const message = {
|
|
1070
|
+
role: "agent",
|
|
1071
|
+
parts: [{ kind: "text", text: "Agreement reached (nonbinding)." }],
|
|
1072
|
+
messageId: newMessageId(),
|
|
1073
|
+
};
|
|
1074
|
+
// 协商终态:conditional 不再需要(评审项 L3:此前永久累积)。
|
|
1075
|
+
conditionalByNegotiation.delete(negotiationId);
|
|
968
1076
|
closedNegotiations.add(negotiationId);
|
|
969
|
-
|
|
1077
|
+
return {
|
|
1078
|
+
kind: "accepted",
|
|
1079
|
+
taskState: "completed",
|
|
1080
|
+
artifactParts: [artifactPart],
|
|
1081
|
+
message,
|
|
1082
|
+
};
|
|
970
1083
|
}
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
1084
|
+
case "withdraw":
|
|
1085
|
+
case "decline": {
|
|
1086
|
+
// review 2-2(A316 校准撤回):handle 顶部已按 scope 统一推进相位
|
|
1087
|
+
//(actionToPhaseEvent → OFFER_OPEN→OPEN / 终态边),原发现「相位
|
|
1088
|
+
// 不推进」不成立;本分支**不再二次推进**(二次推进反而让合法
|
|
1089
|
+
// decline 在 OPEN 下得到 state_conflict)。scope=offer 只清
|
|
1090
|
+
// conditional;scope=negotiation 进终态集合。
|
|
1091
|
+
const scope = envelope.payload.scope ?? "offer";
|
|
1092
|
+
conditionalByNegotiation.delete(negotiationId);
|
|
1093
|
+
if (scope !== "offer") {
|
|
1094
|
+
closedNegotiations.add(negotiationId);
|
|
1095
|
+
}
|
|
1096
|
+
return textReply(`${envelope.action === "withdraw" ? "Withdrawn" : "Declined"} (scope=${scope}).`);
|
|
979
1097
|
}
|
|
980
|
-
|
|
1098
|
+
case "cancel":
|
|
1099
|
+
conditionalByNegotiation.delete(negotiationId);
|
|
1100
|
+
closedNegotiations.add(negotiationId);
|
|
1101
|
+
await advancePhase(negotiationId, { type: "cancel" });
|
|
1102
|
+
return textReply("Negotiation cancelled.");
|
|
1103
|
+
default:
|
|
1104
|
+
return { kind: "declined", reasonCode: "unsupported_action" };
|
|
981
1105
|
}
|
|
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
|
-
}
|
|
1106
|
+
});
|
|
990
1107
|
},
|
|
991
1108
|
};
|
|
992
1109
|
}
|