@harrylabsj/kiwi 0.12.30 → 0.12.32
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/dist/a2a/node.d.ts +1 -0
- package/dist/a2a/node.js +170 -2
- package/dist/a2a/node.js.map +1 -1
- package/dist/a2a/reception/agent-runtime.d.ts +32 -1
- package/dist/a2a/reception/agent-runtime.js +420 -11
- package/dist/a2a/reception/agent-runtime.js.map +1 -1
- package/dist/a2a/reception/customer-memory-tools.d.ts +73 -0
- package/dist/a2a/reception/customer-memory-tools.js +17 -0
- package/dist/a2a/reception/customer-memory-tools.js.map +1 -0
- package/dist/a2a/reception/engine.d.ts +12 -0
- package/dist/a2a/reception/engine.js +42 -1
- package/dist/a2a/reception/engine.js.map +1 -1
- package/dist/a2a/reception/publish.d.ts +14 -1
- package/dist/a2a/reception/publish.js +62 -1
- package/dist/a2a/reception/publish.js.map +1 -1
- package/dist/a2a/reception/question-spec.d.ts +91 -0
- package/dist/a2a/reception/question-spec.js +260 -0
- package/dist/a2a/reception/question-spec.js.map +1 -0
- package/dist/a2a/reception/reply-outbox.d.ts +6 -0
- package/dist/a2a/reception/reply-outbox.js +44 -0
- package/dist/a2a/reception/reply-outbox.js.map +1 -1
- package/dist/a2a/reception/responder.d.ts +13 -1
- package/dist/a2a/reception/responder.js +26 -3
- package/dist/a2a/reception/responder.js.map +1 -1
- package/dist/a2a/reception/skill-catalog.d.ts +72 -0
- package/dist/a2a/reception/skill-catalog.js +85 -0
- package/dist/a2a/reception/skill-catalog.js.map +1 -0
- package/dist/a2a/reception/skill-contract.d.ts +70 -0
- package/dist/a2a/reception/skill-contract.js +183 -0
- package/dist/a2a/reception/skill-contract.js.map +1 -0
- package/dist/a2a/reception/types.d.ts +12 -0
- package/dist/a2a/server/customer-identity-gateway.d.ts +111 -0
- package/dist/a2a/server/customer-identity-gateway.js +441 -0
- package/dist/a2a/server/customer-identity-gateway.js.map +1 -0
- package/dist/a2a/server/merchant-handler.d.ts +56 -1
- package/dist/a2a/server/merchant-handler.js +659 -44
- package/dist/a2a/server/merchant-handler.js.map +1 -1
- package/dist/a2a/server/pipeline.js +74 -29
- package/dist/a2a/server/pipeline.js.map +1 -1
- package/dist/a2a/server/types.d.ts +2 -0
- package/dist/a2a/unit-alias.d.ts +3 -3
- package/dist/a2a/unit-alias.js +9 -5
- package/dist/a2a/unit-alias.js.map +1 -1
- package/dist/agent/buyer/buyer-tools.js +1 -2
- package/dist/agent/buyer/buyer-tools.js.map +1 -1
- package/dist/agent/buyer/search-loop.js +0 -1
- package/dist/agent/buyer/search-loop.js.map +1 -1
- package/dist/auth/merchant-sessions.d.ts +3 -0
- package/dist/auth/merchant-sessions.js +21 -5
- package/dist/auth/merchant-sessions.js.map +1 -1
- package/dist/buyer-core/a2a-knp.d.ts +4 -2
- package/dist/buyer-core/a2a-knp.js +5 -2
- package/dist/buyer-core/a2a-knp.js.map +1 -1
- package/dist/buyer-core/a2a-negotiator.d.ts +1 -0
- package/dist/buyer-core/a2a-negotiator.js +18 -11
- package/dist/buyer-core/a2a-negotiator.js.map +1 -1
- package/dist/buyer-core/a2a-quote-fetcher.d.ts +25 -0
- package/dist/buyer-core/a2a-quote-fetcher.js +82 -4
- package/dist/buyer-core/a2a-quote-fetcher.js.map +1 -1
- package/dist/buyer-core/a2a-quote-history.d.ts +94 -0
- package/dist/buyer-core/a2a-quote-history.js +149 -0
- package/dist/buyer-core/a2a-quote-history.js.map +1 -0
- package/dist/buyer-core/build-service.d.ts +19 -0
- package/dist/buyer-core/build-service.js +30 -1
- package/dist/buyer-core/build-service.js.map +1 -1
- package/dist/buyer-core/errors.d.ts +1 -1
- package/dist/buyer-core/errors.js +1 -0
- package/dist/buyer-core/errors.js.map +1 -1
- package/dist/buyer-core/merchant-index.js +0 -1
- package/dist/buyer-core/merchant-index.js.map +1 -1
- package/dist/buyer-core/open-inquiry-signer.d.ts +1 -0
- package/dist/buyer-core/open-inquiry-signer.js +5 -2
- package/dist/buyer-core/open-inquiry-signer.js.map +1 -1
- package/dist/buyer-core/protocol-recovery.d.ts +2 -1
- package/dist/buyer-core/protocol-recovery.js +14 -4
- package/dist/buyer-core/protocol-recovery.js.map +1 -1
- package/dist/buyer-core/service.d.ts +58 -5
- package/dist/buyer-core/service.js +279 -53
- package/dist/buyer-core/service.js.map +1 -1
- package/dist/buyer-core/store.d.ts +8 -0
- package/dist/buyer-core/store.js +37 -7
- package/dist/buyer-core/store.js.map +1 -1
- package/dist/cli.js +136 -34
- package/dist/cli.js.map +1 -1
- package/dist/cloud/bootstrap.d.ts +1 -1
- package/dist/cloud/bootstrap.js +162 -35
- package/dist/cloud/bootstrap.js.map +1 -1
- package/dist/cloud/product-import-tabular.js +13 -5
- package/dist/cloud/product-import-tabular.js.map +1 -1
- package/dist/cloud/trade-platforms-client.d.ts +30 -0
- package/dist/cloud/trade-platforms-client.js +34 -1
- package/dist/cloud/trade-platforms-client.js.map +1 -1
- package/dist/commerce/receipt/dispatcher.d.ts +42 -0
- package/dist/commerce/receipt/dispatcher.js +68 -0
- package/dist/commerce/receipt/dispatcher.js.map +1 -0
- package/dist/commerce/receipt/receipts.d.ts +140 -0
- package/dist/commerce/receipt/receipts.js +247 -0
- package/dist/commerce/receipt/receipts.js.map +1 -0
- package/dist/config/profile.d.ts +2 -2
- package/dist/handoff/order-receipts.d.ts +62 -0
- package/dist/handoff/order-receipts.js +71 -0
- package/dist/handoff/order-receipts.js.map +1 -0
- package/dist/handoff/trade-platform-rules.js +5 -2
- package/dist/handoff/trade-platform-rules.js.map +1 -1
- package/dist/handoff/ucp-checkout/cart-channel.d.ts +2 -1
- package/dist/handoff/ucp-checkout/cart-channel.js +1 -0
- package/dist/handoff/ucp-checkout/cart-channel.js.map +1 -1
- package/dist/handoff/ucp-checkout/channel.d.ts +6 -0
- package/dist/handoff/ucp-checkout/channel.js +44 -0
- package/dist/handoff/ucp-checkout/channel.js.map +1 -1
- package/dist/http/cli.js +6 -0
- package/dist/http/cli.js.map +1 -1
- package/dist/http/merchant-management/api.d.ts +11 -1
- package/dist/http/merchant-management/api.js +471 -3
- package/dist/http/merchant-management/api.js.map +1 -1
- package/dist/http/merchant-management/draft-store.js +8 -6
- package/dist/http/merchant-management/draft-store.js.map +1 -1
- package/dist/http/merchant-management/page.js +83 -22
- package/dist/http/merchant-management/page.js.map +1 -1
- package/dist/mcp/cli.js +6 -0
- package/dist/mcp/cli.js.map +1 -1
- package/dist/mcp/merchant-runtime-assembly.d.ts +20 -0
- package/dist/mcp/merchant-runtime-assembly.js +93 -3
- package/dist/mcp/merchant-runtime-assembly.js.map +1 -1
- package/dist/mcp/merchant-server.d.ts +5 -0
- package/dist/mcp/merchant-server.js +12 -0
- package/dist/mcp/merchant-server.js.map +1 -1
- package/dist/mcp/tools.js +12 -3
- package/dist/mcp/tools.js.map +1 -1
- package/dist/merchant/application/actor.d.ts +1 -1
- package/dist/merchant/application/actor.js +15 -0
- package/dist/merchant/application/actor.js.map +1 -1
- package/dist/merchant/workbench-server.d.ts +38 -0
- package/dist/merchant/workbench-server.js +428 -0
- package/dist/merchant/workbench-server.js.map +1 -0
- package/dist/merchant-core/budget/background-budget.d.ts +132 -0
- package/dist/merchant-core/budget/background-budget.js +308 -0
- package/dist/merchant-core/budget/background-budget.js.map +1 -0
- package/dist/merchant-core/context-governance/actor-context.d.ts +56 -0
- package/dist/merchant-core/context-governance/actor-context.js +76 -0
- package/dist/merchant-core/context-governance/actor-context.js.map +1 -0
- package/dist/merchant-core/context-governance/authority.d.ts +39 -0
- package/dist/merchant-core/context-governance/authority.js +28 -0
- package/dist/merchant-core/context-governance/authority.js.map +1 -0
- package/dist/merchant-core/context-governance/context-guard.d.ts +104 -0
- package/dist/merchant-core/context-governance/context-guard.js +194 -0
- package/dist/merchant-core/context-governance/context-guard.js.map +1 -0
- package/dist/merchant-core/context-governance/data-use-grant.d.ts +91 -0
- package/dist/merchant-core/context-governance/data-use-grant.js +211 -0
- package/dist/merchant-core/context-governance/data-use-grant.js.map +1 -0
- package/dist/merchant-core/context-governance/feature-flags.d.ts +32 -0
- package/dist/merchant-core/context-governance/feature-flags.js +83 -0
- package/dist/merchant-core/context-governance/feature-flags.js.map +1 -0
- package/dist/merchant-core/context-governance/pilot-readiness.d.ts +39 -0
- package/dist/merchant-core/context-governance/pilot-readiness.js +141 -0
- package/dist/merchant-core/context-governance/pilot-readiness.js.map +1 -0
- package/dist/merchant-core/context-governance/source-governance.d.ts +157 -0
- package/dist/merchant-core/context-governance/source-governance.js +492 -0
- package/dist/merchant-core/context-governance/source-governance.js.map +1 -0
- package/dist/merchant-core/context-governance/types.d.ts +62 -0
- package/dist/merchant-core/context-governance/types.js +92 -0
- package/dist/merchant-core/context-governance/types.js.map +1 -0
- package/dist/merchant-core/customer-delivery/binding.d.ts +55 -0
- package/dist/merchant-core/customer-delivery/binding.js +121 -0
- package/dist/merchant-core/customer-delivery/binding.js.map +1 -0
- package/dist/merchant-core/customer-delivery/channel.d.ts +75 -0
- package/dist/merchant-core/customer-delivery/channel.js +157 -0
- package/dist/merchant-core/customer-delivery/channel.js.map +1 -0
- package/dist/merchant-core/customer-delivery/consent-repository.d.ts +50 -0
- package/dist/merchant-core/customer-delivery/consent-repository.js +160 -0
- package/dist/merchant-core/customer-delivery/consent-repository.js.map +1 -0
- package/dist/merchant-core/customer-delivery/dispatcher-loop.d.ts +68 -0
- package/dist/merchant-core/customer-delivery/dispatcher-loop.js +132 -0
- package/dist/merchant-core/customer-delivery/dispatcher-loop.js.map +1 -0
- package/dist/merchant-core/customer-delivery/dispatcher.d.ts +118 -0
- package/dist/merchant-core/customer-delivery/dispatcher.js +166 -0
- package/dist/merchant-core/customer-delivery/dispatcher.js.map +1 -0
- package/dist/merchant-core/customer-delivery/email-adapter.d.ts +31 -0
- package/dist/merchant-core/customer-delivery/email-adapter.js +90 -0
- package/dist/merchant-core/customer-delivery/email-adapter.js.map +1 -0
- package/dist/merchant-core/customer-delivery/gate.d.ts +37 -0
- package/dist/merchant-core/customer-delivery/gate.js +196 -0
- package/dist/merchant-core/customer-delivery/gate.js.map +1 -0
- package/dist/merchant-core/customer-delivery/reconciliation.d.ts +13 -0
- package/dist/merchant-core/customer-delivery/reconciliation.js +70 -0
- package/dist/merchant-core/customer-delivery/reconciliation.js.map +1 -0
- package/dist/merchant-core/customer-delivery/repository.d.ts +55 -0
- package/dist/merchant-core/customer-delivery/repository.js +235 -0
- package/dist/merchant-core/customer-delivery/repository.js.map +1 -0
- package/dist/merchant-core/customer-delivery/smtp-adapter.d.ts +30 -0
- package/dist/merchant-core/customer-delivery/smtp-adapter.js +183 -0
- package/dist/merchant-core/customer-delivery/smtp-adapter.js.map +1 -0
- package/dist/merchant-core/customer-delivery/stream-gate.d.ts +45 -0
- package/dist/merchant-core/customer-delivery/stream-gate.js +123 -0
- package/dist/merchant-core/customer-delivery/stream-gate.js.map +1 -0
- package/dist/merchant-core/customer-delivery/types.d.ts +103 -0
- package/dist/merchant-core/customer-delivery/types.js +32 -0
- package/dist/merchant-core/customer-delivery/types.js.map +1 -0
- package/dist/merchant-core/customer-delivery/weixin-adapter.d.ts +35 -0
- package/dist/merchant-core/customer-delivery/weixin-adapter.js +56 -0
- package/dist/merchant-core/customer-delivery/weixin-adapter.js.map +1 -0
- package/dist/merchant-core/customer-memory/auto-identity.d.ts +43 -0
- package/dist/merchant-core/customer-memory/auto-identity.js +163 -0
- package/dist/merchant-core/customer-memory/auto-identity.js.map +1 -0
- package/dist/merchant-core/customer-memory/grants.d.ts +72 -0
- package/dist/merchant-core/customer-memory/grants.js +224 -0
- package/dist/merchant-core/customer-memory/grants.js.map +1 -0
- package/dist/merchant-core/customer-memory/history.d.ts +51 -0
- package/dist/merchant-core/customer-memory/history.js +252 -0
- package/dist/merchant-core/customer-memory/history.js.map +1 -0
- package/dist/merchant-core/customer-memory/identity.d.ts +66 -0
- package/dist/merchant-core/customer-memory/identity.js +249 -0
- package/dist/merchant-core/customer-memory/identity.js.map +1 -0
- package/dist/merchant-core/customer-memory/maintenance.d.ts +51 -0
- package/dist/merchant-core/customer-memory/maintenance.js +67 -0
- package/dist/merchant-core/customer-memory/maintenance.js.map +1 -0
- package/dist/merchant-core/customer-memory/memory.d.ts +119 -0
- package/dist/merchant-core/customer-memory/memory.js +502 -0
- package/dist/merchant-core/customer-memory/memory.js.map +1 -0
- package/dist/merchant-core/customer-memory/private-transport.d.ts +10 -0
- package/dist/merchant-core/customer-memory/private-transport.js +108 -0
- package/dist/merchant-core/customer-memory/private-transport.js.map +1 -0
- package/dist/merchant-core/customer-memory/quote-history.d.ts +160 -0
- package/dist/merchant-core/customer-memory/quote-history.js +392 -0
- package/dist/merchant-core/customer-memory/quote-history.js.map +1 -0
- package/dist/merchant-core/customer-memory/reception-provider.d.ts +69 -0
- package/dist/merchant-core/customer-memory/reception-provider.js +250 -0
- package/dist/merchant-core/customer-memory/reception-provider.js.map +1 -0
- package/dist/merchant-core/customer-memory/reception-writer.d.ts +19 -0
- package/dist/merchant-core/customer-memory/reception-writer.js +32 -0
- package/dist/merchant-core/customer-memory/reception-writer.js.map +1 -0
- package/dist/merchant-core/customer-memory/types.d.ts +111 -0
- package/dist/merchant-core/customer-memory/types.js +58 -0
- package/dist/merchant-core/customer-memory/types.js.map +1 -0
- package/dist/merchant-core/followup/repository.d.ts +2 -1
- package/dist/merchant-core/followup/repository.js +29 -23
- package/dist/merchant-core/followup/repository.js.map +1 -1
- package/dist/merchant-core/learning/evaluation-fixtures.d.ts +38 -0
- package/dist/merchant-core/learning/evaluation-fixtures.js +83 -0
- package/dist/merchant-core/learning/evaluation-fixtures.js.map +1 -0
- package/dist/merchant-core/learning/evaluation-suite-v1.d.ts +56 -0
- package/dist/merchant-core/learning/evaluation-suite-v1.js +129 -0
- package/dist/merchant-core/learning/evaluation-suite-v1.js.map +1 -0
- package/dist/merchant-core/learning/evaluation.d.ts +84 -0
- package/dist/merchant-core/learning/evaluation.js +581 -0
- package/dist/merchant-core/learning/evaluation.js.map +1 -0
- package/dist/merchant-core/learning/initial-skills.d.ts +36 -0
- package/dist/merchant-core/learning/initial-skills.js +67 -0
- package/dist/merchant-core/learning/initial-skills.js.map +1 -0
- package/dist/merchant-core/learning/precheck.d.ts +33 -0
- package/dist/merchant-core/learning/precheck.js +56 -0
- package/dist/merchant-core/learning/precheck.js.map +1 -0
- package/dist/merchant-core/learning/real-evaluation-runner.d.ts +52 -0
- package/dist/merchant-core/learning/real-evaluation-runner.js +167 -0
- package/dist/merchant-core/learning/real-evaluation-runner.js.map +1 -0
- package/dist/merchant-core/learning/reception-skill-catalog.d.ts +39 -0
- package/dist/merchant-core/learning/reception-skill-catalog.js +145 -0
- package/dist/merchant-core/learning/reception-skill-catalog.js.map +1 -0
- package/dist/merchant-core/learning/recovery-gateway.d.ts +27 -0
- package/dist/merchant-core/learning/recovery-gateway.js +106 -0
- package/dist/merchant-core/learning/recovery-gateway.js.map +1 -0
- package/dist/merchant-core/learning/repository.d.ts +130 -0
- package/dist/merchant-core/learning/repository.js +588 -0
- package/dist/merchant-core/learning/repository.js.map +1 -0
- package/dist/merchant-core/learning/service.d.ts +262 -0
- package/dist/merchant-core/learning/service.js +1498 -0
- package/dist/merchant-core/learning/service.js.map +1 -0
- package/dist/merchant-core/learning/test-dispatch-gateway.d.ts +43 -0
- package/dist/merchant-core/learning/test-dispatch-gateway.js +145 -0
- package/dist/merchant-core/learning/test-dispatch-gateway.js.map +1 -0
- package/dist/merchant-core/learning/types.d.ts +217 -0
- package/dist/merchant-core/learning/types.js +52 -0
- package/dist/merchant-core/learning/types.js.map +1 -0
- package/dist/merchant-core/migrations/ledgers.d.ts +40 -0
- package/dist/merchant-core/migrations/ledgers.js +824 -0
- package/dist/merchant-core/migrations/ledgers.js.map +1 -0
- package/dist/merchant-core/migrations/runner.d.ts +55 -0
- package/dist/merchant-core/migrations/runner.js +114 -0
- package/dist/merchant-core/migrations/runner.js.map +1 -0
- package/dist/merchant-core/operations-jobs/advice-runner.d.ts +63 -0
- package/dist/merchant-core/operations-jobs/advice-runner.js +199 -0
- package/dist/merchant-core/operations-jobs/advice-runner.js.map +1 -0
- package/dist/merchant-core/operations-jobs/pilot-evidence.d.ts +64 -0
- package/dist/merchant-core/operations-jobs/pilot-evidence.js +109 -0
- package/dist/merchant-core/operations-jobs/pilot-evidence.js.map +1 -0
- package/dist/merchant-core/operations-jobs/pilot-metrics.d.ts +70 -0
- package/dist/merchant-core/operations-jobs/pilot-metrics.js +98 -0
- package/dist/merchant-core/operations-jobs/pilot-metrics.js.map +1 -0
- package/dist/merchant-core/operations-jobs/projection.d.ts +41 -0
- package/dist/merchant-core/operations-jobs/projection.js +158 -0
- package/dist/merchant-core/operations-jobs/projection.js.map +1 -0
- package/dist/merchant-core/operations-jobs/scheduler.d.ts +68 -0
- package/dist/merchant-core/operations-jobs/scheduler.js +233 -0
- package/dist/merchant-core/operations-jobs/scheduler.js.map +1 -0
- package/dist/merchant-core/operations-jobs/service.d.ts +260 -0
- package/dist/merchant-core/operations-jobs/service.js +922 -0
- package/dist/merchant-core/operations-jobs/service.js.map +1 -0
- package/dist/merchant-core/ops/facade.d.ts +10 -0
- package/dist/merchant-core/ops/facade.js +52 -0
- package/dist/merchant-core/ops/facade.js.map +1 -1
- package/dist/merchant-core/ops/schema.d.ts +1 -1
- package/dist/merchant-core/ops/schema.js +119 -2
- package/dist/merchant-core/ops/schema.js.map +1 -1
- package/dist/merchant-core/ops/surface.d.ts +37 -0
- package/dist/merchant-core/ops/surface.js +240 -0
- package/dist/merchant-core/ops/surface.js.map +1 -1
- package/dist/merchant-core/ownership/instance-ownership.d.ts +122 -0
- package/dist/merchant-core/ownership/instance-ownership.js +236 -0
- package/dist/merchant-core/ownership/instance-ownership.js.map +1 -0
- package/dist/merchant-core/ownership/runtime.d.ts +175 -0
- package/dist/merchant-core/ownership/runtime.js +422 -0
- package/dist/merchant-core/ownership/runtime.js.map +1 -0
- package/dist/merchant-core/storage/transaction.d.ts +102 -7
- package/dist/merchant-core/storage/transaction.js +274 -3
- package/dist/merchant-core/storage/transaction.js.map +1 -1
- package/dist/merchant-credential-rotation.d.ts +4 -1
- package/dist/merchant-credential-rotation.js +12 -8
- package/dist/merchant-credential-rotation.js.map +1 -1
- package/dist/merchant-gateway/catalog-tools.js +4 -2
- package/dist/merchant-gateway/catalog-tools.js.map +1 -1
- package/dist/merchant-runtime/manager.d.ts +2 -0
- package/dist/merchant-runtime/manager.js +5 -0
- package/dist/merchant-runtime/manager.js.map +1 -1
- package/dist/product-buyer.d.ts +1 -0
- package/dist/product-buyer.js +0 -1
- package/dist/product-buyer.js.map +1 -1
- package/dist/product-cli.js +11 -1
- package/dist/product-cli.js.map +1 -1
- package/package.json +5 -3
- package/skills/kiwi-buyer/SKILL.md +1 -1
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
* 每个出站回复落 merchant 侧 Ledger(append-only,§22)。这是 `kiwi agent serve`
|
|
25
25
|
* 的确定性 merchant 行为;未来可替换为 LLM 驱动的 Negotiation Engine(同一 handler 接缝)。
|
|
26
26
|
*/
|
|
27
|
+
import { StreamGateError } from "../../merchant-core/customer-delivery/stream-gate.js";
|
|
27
28
|
import { BoundedMap, BoundedSet } from "../../runtime/bounded-memory.js";
|
|
28
29
|
import { checkedProductStock, checkedSourceEvidence, quoteFactsStillValid, recoverQuoteSourceSnapshot } from "./merchant-product-facts.js";
|
|
29
30
|
import { LedgerStore } from "../../negotiation/ledger/index.js";
|
|
@@ -35,6 +36,9 @@ import { toMinorUnits as losslessToMinorUnits } from "../../protocol/legacy-shop
|
|
|
35
36
|
import { WORKBENCH_CURRENCY_TABLE_VERSION } from "../../merchant/application/money.js";
|
|
36
37
|
import { ADVISOR_BUYER_MESSAGE_MAX_CHARS, safeAdvisorMessage, } from "./negotiation-advisor.js";
|
|
37
38
|
import { calculateWorkbenchQuote } from "../../merchant/quote-calculator.js";
|
|
39
|
+
import { isOpenInquiryIdentity } from "../open-inquiry-identity.js";
|
|
40
|
+
import { QuoteHistoryStore, delegationFromMetadata, parseHistoryQueryExtension, isQuoteHistoryRecall, quoteHistoryNote, validateDelegationShape, } from "../../merchant-core/customer-memory/quote-history.js";
|
|
41
|
+
import { thumbprintFromOpenInquiryIdentity, } from "../../commerce/receipt/receipts.js";
|
|
38
42
|
import { createNegotiationPhase, isTerminalPhase, transitionPhase, } from "../../negotiation/state/phase.js";
|
|
39
43
|
import { NegotiationValidationError } from "../../negotiation/domain/common.js";
|
|
40
44
|
import { resolveUnitConversion, unitConversionPolicy, } from "../unit-alias.js";
|
|
@@ -75,6 +79,105 @@ export function resolveDeliveryBefore(merchantPolicy, now) {
|
|
|
75
79
|
return undefined;
|
|
76
80
|
return new Date(Date.parse(now) + leadDays * 86_400_000).toISOString();
|
|
77
81
|
}
|
|
82
|
+
const CJK_TEXT_RE = /[一-鿿]/;
|
|
83
|
+
/** 买家文本是否中文(只影响回复语言,不影响任何事实/价格)。 */
|
|
84
|
+
export function prefersChineseText(text) {
|
|
85
|
+
return text !== undefined && CJK_TEXT_RE.test(text);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* 回复语言:有中文即中文;纯英文文本用英文;没有任何语言线索时按 Kiwi
|
|
89
|
+
* 中文市场默认中文(CNY/中文商家)。只影响措辞,不影响事实。
|
|
90
|
+
*/
|
|
91
|
+
export function replyLanguageOf(...texts) {
|
|
92
|
+
const joined = texts
|
|
93
|
+
.filter((text) => text !== undefined && text.trim() !== "")
|
|
94
|
+
.join(" ")
|
|
95
|
+
.trim();
|
|
96
|
+
if (joined === "")
|
|
97
|
+
return "zh";
|
|
98
|
+
if (CJK_TEXT_RE.test(joined))
|
|
99
|
+
return "zh";
|
|
100
|
+
return /[A-Za-z]/.test(joined) ? "en" : "zh";
|
|
101
|
+
}
|
|
102
|
+
/** 从买家 RFQ 的 requested_terms.delivery_before 取合法 ISO 时间。 */
|
|
103
|
+
export function requestedDeliveryBeforeOf(payload) {
|
|
104
|
+
if (typeof payload !== "object" || payload === null)
|
|
105
|
+
return undefined;
|
|
106
|
+
const requested = payload.requested_terms;
|
|
107
|
+
if (typeof requested !== "object" || requested === null)
|
|
108
|
+
return undefined;
|
|
109
|
+
const value = requested.delivery_before;
|
|
110
|
+
if (typeof value !== "string" || value.trim() === "")
|
|
111
|
+
return undefined;
|
|
112
|
+
const parsed = Date.parse(value);
|
|
113
|
+
return Number.isFinite(parsed) ? new Date(parsed).toISOString() : undefined;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* 买家文本里的相对交期(“7天到中国”“两周内送达”)。只用于把买家的
|
|
117
|
+
* 时限与商家已配置的权威交期对齐;解析不出就不猜。
|
|
118
|
+
*/
|
|
119
|
+
export function requestedDeliveryDaysOf(text) {
|
|
120
|
+
// 日历日期与相对期限混在一起时无法确认买家的真实截止时间,不作推断。
|
|
121
|
+
// 单独“18日”也可能是日期;只有“18日内”等明确时长才按相对日数处理。
|
|
122
|
+
if (/(?:[\d一二两三四五六七八九十]+\s*月\s*[\d一二两三四五六七八九十]+\s*[日号]?|\d{4}[-/.]\d{1,2}[-/.]\d{1,2}|(?<!工作)[\d一二两三四五六七八九十]+\s*日(?!\s*(?:以)?内))/.test(text)) {
|
|
123
|
+
return undefined;
|
|
124
|
+
}
|
|
125
|
+
const numeric = /(?<!\d)(\d{1,3})\s*(?:个)?\s*(?:工作日|天|日(?=\s*(?:以)?内))\s*(?:内|以内|到|到达|送到|送达|发到|发货|到货)?/.exec(text);
|
|
126
|
+
if (numeric !== null) {
|
|
127
|
+
const days = Number(numeric[1]);
|
|
128
|
+
return Number.isSafeInteger(days) && days > 0 ? days : undefined;
|
|
129
|
+
}
|
|
130
|
+
const chinese = /([一二两三四五六七八九十]+)\s*(?:个)?\s*(?:工作日|天|日(?=\s*(?:以)?内))\s*(?:内|以内|到|到达|送到|送达|发到|发货|到货)?/.exec(text);
|
|
131
|
+
if (chinese === null)
|
|
132
|
+
return undefined;
|
|
133
|
+
const digits = {
|
|
134
|
+
一: 1, 二: 2, 两: 2, 三: 3, 四: 4, 五: 5, 六: 6, 七: 7, 八: 8, 九: 9, 十: 10,
|
|
135
|
+
};
|
|
136
|
+
const raw = chinese[1];
|
|
137
|
+
if (raw === "十")
|
|
138
|
+
return 10;
|
|
139
|
+
const tenIndex = raw.indexOf("十");
|
|
140
|
+
if (tenIndex === -1)
|
|
141
|
+
return raw.length === 1 ? digits[raw] : undefined;
|
|
142
|
+
const tens = tenIndex === 0 ? 1 : digits[raw.slice(0, tenIndex)];
|
|
143
|
+
const ones = tenIndex === raw.length - 1 ? 0 : digits[raw.slice(tenIndex + 1)];
|
|
144
|
+
return tens === undefined || ones === undefined ? undefined : tens * 10 + ones;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* 交期应答:买家给了明确时限时,必须回答“能否满足”,不能只说“待确认”。
|
|
148
|
+
* 事实只来自 merchant_policy.delivery_lead_days 或“未配置”这一事实。
|
|
149
|
+
*/
|
|
150
|
+
export function deliveryCommitmentNote(requestedBefore, promisedBefore, language) {
|
|
151
|
+
if (requestedBefore === undefined)
|
|
152
|
+
return undefined;
|
|
153
|
+
const requestedDay = requestedBefore.slice(0, 10);
|
|
154
|
+
if (promisedBefore === undefined) {
|
|
155
|
+
return language === "zh"
|
|
156
|
+
? `您要求不晚于 ${requestedDay} 到货;本店尚未配置可承诺的交期,无法确认该时限,请与商家确认。`
|
|
157
|
+
: `You asked for delivery by ${requestedDay}; this merchant has no committed lead time configured, so that deadline cannot be confirmed.`;
|
|
158
|
+
}
|
|
159
|
+
const promisedDay = promisedBefore.slice(0, 10);
|
|
160
|
+
if (Date.parse(promisedBefore) <= Date.parse(requestedBefore)) {
|
|
161
|
+
return language === "zh"
|
|
162
|
+
? `您要求不晚于 ${requestedDay} 到货;当前预计交期不晚于 ${promisedDay},可以满足。`
|
|
163
|
+
: `You asked for delivery by ${requestedDay}; the current estimate is by ${promisedDay}, which meets it.`;
|
|
164
|
+
}
|
|
165
|
+
return language === "zh"
|
|
166
|
+
? `您要求不晚于 ${requestedDay} 到货;当前预计交期不晚于 ${promisedDay},无法承诺该时限。`
|
|
167
|
+
: `You asked for delivery by ${requestedDay}; the current estimate is by ${promisedDay}, so that deadline cannot be committed.`;
|
|
168
|
+
}
|
|
169
|
+
/** 从买家问题推断商业主题的确定性兜底(模型不可用时仍按问题作答)。 */
|
|
170
|
+
export function inferInquiryTopicsFromText(text) {
|
|
171
|
+
const topics = new Set();
|
|
172
|
+
if (/价|多少钱|报价|便宜|优惠|折扣|price|cost|discount/i.test(text))
|
|
173
|
+
topics.add("price");
|
|
174
|
+
if (/库存|现货|有货|缺货|stock|inventory|availability/i.test(text))
|
|
175
|
+
topics.add("stock");
|
|
176
|
+
if (/交期|发货|送达|到货|多久|几天|多少天|\d+\s*(?:天|日)|delivery|eta|ship|lead.?time|arrive/i.test(text)) {
|
|
177
|
+
topics.add("delivery");
|
|
178
|
+
}
|
|
179
|
+
return [...topics];
|
|
180
|
+
}
|
|
78
181
|
/** 批量促销默认门槛(merchant_policy.promos[sku].bulk_threshold 缺省值)。 */
|
|
79
182
|
export const DEFAULT_BULK_THRESHOLD = 100;
|
|
80
183
|
/**
|
|
@@ -224,7 +327,7 @@ class ProductSourceUnavailableError extends Error {
|
|
|
224
327
|
*/
|
|
225
328
|
/**
|
|
226
329
|
* 单位校验:商品**没有单位就拒绝**(fail-closed,不知道按什么计量就不报价);
|
|
227
|
-
*
|
|
330
|
+
* 有单位时接受商家显式别名及内建的 box/盒、piece/个/件 1:1 写法。
|
|
228
331
|
* 不做“个/件 = 盒”的自动换算,也不改变数量/单价/金额。
|
|
229
332
|
*/
|
|
230
333
|
function unitMismatch(productUnit, requestedUnit, policy) {
|
|
@@ -239,6 +342,11 @@ const declineReply = (reasonCode = "state_conflict", message) => ({
|
|
|
239
342
|
...(message !== undefined ? { message } : {}),
|
|
240
343
|
taskState: "completed",
|
|
241
344
|
});
|
|
345
|
+
function unitDecline(productUnit) {
|
|
346
|
+
return declineReply("field_unsupported", productUnit?.trim()
|
|
347
|
+
? "询价数量单位与商品销售单位不匹配,请核对单位及包装数量后重新询价。"
|
|
348
|
+
: "商品尚未提供销售单位,请联系商家确认后重新询价。");
|
|
349
|
+
}
|
|
242
350
|
/** AC09/D03:统计入站商务动作的商品行数(rfq/offer/counter_offer)。 */
|
|
243
351
|
function multiItemCountOf(envelope) {
|
|
244
352
|
const payload = typeof envelope.payload === "object" && envelope.payload !== null
|
|
@@ -317,6 +425,190 @@ export function createMerchantHandler(options) {
|
|
|
317
425
|
"decline",
|
|
318
426
|
"cancel",
|
|
319
427
|
]);
|
|
428
|
+
/**
|
|
429
|
+
* K1:验签开放询价身份的本店自动建档。仅在业务边界(正式受理 RFQ /
|
|
430
|
+
* 判定为有效询价)调用;写者不可用时静默继续(询价不被阻断、不承诺
|
|
431
|
+
* 已建档),spooled writer 会可靠重试。匿名/静态凭据/未验签不进入。
|
|
432
|
+
* 返回本店客户 subject(建档不可用时 undefined)。
|
|
433
|
+
*/
|
|
434
|
+
const registerVerifiedCustomerIdentity = async (ctx, source) => {
|
|
435
|
+
const identity = options.customerIdentity;
|
|
436
|
+
if (identity === undefined)
|
|
437
|
+
return undefined;
|
|
438
|
+
if (ctx.identityVerified !== true)
|
|
439
|
+
return undefined;
|
|
440
|
+
if (!isOpenInquiryIdentity(ctx.senderIdentity))
|
|
441
|
+
return undefined;
|
|
442
|
+
try {
|
|
443
|
+
const outcome = await identity.writer.ensure({
|
|
444
|
+
merchant_id: identity.merchantId,
|
|
445
|
+
verified_actor: ctx.senderIdentity,
|
|
446
|
+
source,
|
|
447
|
+
negotiation_id: ctx.envelope.negotiation_id,
|
|
448
|
+
message_id: ctx.envelope.message_id,
|
|
449
|
+
});
|
|
450
|
+
if (outcome.status === "registered")
|
|
451
|
+
return { subject_id: outcome.subject_id };
|
|
452
|
+
return undefined;
|
|
453
|
+
}
|
|
454
|
+
catch {
|
|
455
|
+
// Writer 实现不应抛出(失败收敛为 unavailable);防御性兜底不阻断询价。
|
|
456
|
+
return undefined;
|
|
457
|
+
}
|
|
458
|
+
};
|
|
459
|
+
/**
|
|
460
|
+
* K2:校验并登记 message metadata 携带的委托声明。失败一律静默降级为
|
|
461
|
+
* 未委托(旧客户端/未授权/过期均正常询价,不带出历史)。委托生效以
|
|
462
|
+
* 验签身份对应的本店客户主体为授权主体。
|
|
463
|
+
*/
|
|
464
|
+
const applyDelegationFromMetadata = async (ctx, subjectId) => {
|
|
465
|
+
const history = options.quoteHistory;
|
|
466
|
+
if (history === undefined)
|
|
467
|
+
return;
|
|
468
|
+
if (ctx.identityVerified !== true || !isOpenInquiryIdentity(ctx.senderIdentity))
|
|
469
|
+
return;
|
|
470
|
+
if (subjectId === undefined)
|
|
471
|
+
return;
|
|
472
|
+
const raw = delegationFromMetadata(ctx.message.metadata);
|
|
473
|
+
if (raw === undefined)
|
|
474
|
+
return;
|
|
475
|
+
const merchantId = options.customerIdentity?.merchantId;
|
|
476
|
+
if (merchantId === undefined)
|
|
477
|
+
return;
|
|
478
|
+
const validated = validateDelegationShape(raw, merchantId, now());
|
|
479
|
+
if (!validated.ok)
|
|
480
|
+
return;
|
|
481
|
+
try {
|
|
482
|
+
await history.store.applyDelegation({
|
|
483
|
+
merchant_id: merchantId,
|
|
484
|
+
buyer_actor: ctx.senderIdentity,
|
|
485
|
+
subject_id: subjectId,
|
|
486
|
+
delegation: validated.delegation,
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
catch {
|
|
490
|
+
// 委托存储失败按未委托处理(静默;下一轮带新 message ID 的 RFQ 可重试)。
|
|
491
|
+
}
|
|
492
|
+
};
|
|
493
|
+
const indexHistoryOffer = async (ctx, reply, product) => {
|
|
494
|
+
const store = options.quoteHistory?.store;
|
|
495
|
+
const merchantId = options.customerIdentity?.merchantId;
|
|
496
|
+
if (store === undefined || merchantId === undefined ||
|
|
497
|
+
ctx.identityVerified !== true || !isOpenInquiryIdentity(ctx.senderIdentity))
|
|
498
|
+
return;
|
|
499
|
+
const grant = store.checkActive({ merchant_id: merchantId, buyer_actor: ctx.senderIdentity });
|
|
500
|
+
if (grant === undefined)
|
|
501
|
+
return;
|
|
502
|
+
const payload = reply.payload;
|
|
503
|
+
const terms = payload.terms ?? payload.proposed_terms ?? payload.base_terms;
|
|
504
|
+
const item = terms?.items?.[0];
|
|
505
|
+
if (payload.offer_id === undefined || terms === undefined || item?.unit_price === undefined)
|
|
506
|
+
return;
|
|
507
|
+
try {
|
|
508
|
+
await store.indexOffer({
|
|
509
|
+
merchant_id: merchantId, subject_id: grant.subject_id,
|
|
510
|
+
negotiation_id: reply.negotiation_id, offer_id: payload.offer_id, message_id: reply.message_id,
|
|
511
|
+
sku: item.sku, quantity: item.quantity.value, unit: item.quantity.unit,
|
|
512
|
+
currency: item.unit_price.currency, unit_price_minor: item.unit_price.amount_minor,
|
|
513
|
+
terms_digest: contentDigest(terms),
|
|
514
|
+
spec: {
|
|
515
|
+
...(product.source_evidence !== undefined ? {
|
|
516
|
+
source_evidence: product.source_evidence,
|
|
517
|
+
...(product.title !== undefined ? { title: product.title } : {}),
|
|
518
|
+
...(product.unit !== undefined ? { product_unit: product.unit } : {}),
|
|
519
|
+
...(product.valid_until !== undefined ? { product_valid_until: product.valid_until } : {}),
|
|
520
|
+
} : {}),
|
|
521
|
+
},
|
|
522
|
+
...(terms.valid_until !== undefined ? { valid_until: terms.valid_until } : {}),
|
|
523
|
+
quoted_at: reply.created_at,
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
catch {
|
|
527
|
+
// History is optional when the merchant writer is temporarily unavailable.
|
|
528
|
+
}
|
|
529
|
+
};
|
|
530
|
+
/**
|
|
531
|
+
* K2:历史查询扩展入口(KNP inquiry subject.kiwi_history_query)。
|
|
532
|
+
* 未验签 / 未委托 / 扩展不支持时返回 undefined(走原接待路径,无泄漏)。
|
|
533
|
+
* 唯一入口返回结构化候选及来源;本次价格仍走正式 RFQ。
|
|
534
|
+
*/
|
|
535
|
+
const resolveQuoteHistoryReply = async (ctx) => {
|
|
536
|
+
const history = options.quoteHistory;
|
|
537
|
+
const merchantId = options.customerIdentity?.merchantId;
|
|
538
|
+
if (history === undefined || merchantId === undefined)
|
|
539
|
+
return undefined;
|
|
540
|
+
if (ctx.identityVerified !== true || !isOpenInquiryIdentity(ctx.senderIdentity))
|
|
541
|
+
return undefined;
|
|
542
|
+
const inquiry = ctx.envelope.payload;
|
|
543
|
+
const extension = parseHistoryQueryExtension(inquiry?.subject);
|
|
544
|
+
if (extension === undefined)
|
|
545
|
+
return undefined;
|
|
546
|
+
const authorization = history.store.checkActive({
|
|
547
|
+
merchant_id: merchantId,
|
|
548
|
+
buyer_actor: ctx.senderIdentity,
|
|
549
|
+
});
|
|
550
|
+
if (authorization === undefined)
|
|
551
|
+
return undefined;
|
|
552
|
+
const hits = history.store.queryHistory({
|
|
553
|
+
merchant_id: merchantId,
|
|
554
|
+
subject_id: authorization.subject_id,
|
|
555
|
+
...(extension.skus !== undefined ? { skus: extension.skus } : {}),
|
|
556
|
+
...(extension.limit !== undefined ? { limit: extension.limit } : {}),
|
|
557
|
+
});
|
|
558
|
+
if (history.store.checkActive({
|
|
559
|
+
merchant_id: merchantId, buyer_actor: ctx.senderIdentity,
|
|
560
|
+
})?.subject_id !== authorization.subject_id)
|
|
561
|
+
return undefined;
|
|
562
|
+
const historyData = {
|
|
563
|
+
type: "kiwi_quote_history",
|
|
564
|
+
version: 1,
|
|
565
|
+
hits: hits.map((hit) => ({
|
|
566
|
+
negotiation_id: hit.negotiation_id,
|
|
567
|
+
offer_id: hit.offer_id,
|
|
568
|
+
offer_version: hit.offer_version,
|
|
569
|
+
sku: hit.sku,
|
|
570
|
+
quantity: hit.quantity,
|
|
571
|
+
unit: hit.unit,
|
|
572
|
+
price: {
|
|
573
|
+
currency: hit.currency,
|
|
574
|
+
amount_minor: hit.unit_price_minor,
|
|
575
|
+
},
|
|
576
|
+
terms_digest: hit.terms_digest,
|
|
577
|
+
spec: hit.spec,
|
|
578
|
+
quoted_at: hit.quoted_at,
|
|
579
|
+
...(hit.valid_until !== undefined ? { valid_until: hit.valid_until } : {}),
|
|
580
|
+
})),
|
|
581
|
+
};
|
|
582
|
+
const summary = hits.length === 0
|
|
583
|
+
? "本店没有可引用的已发报价历史;请直接发送正式询价(SKU、数量、单位)。"
|
|
584
|
+
: `找到 ${hits.length} 条上次报价记录(含时间与有效期);本次价格请以正式 RFQ 返回为准。`;
|
|
585
|
+
const reply = seedEnvelope({
|
|
586
|
+
negotiation_id: ctx.envelope.negotiation_id,
|
|
587
|
+
in_reply_to: ctx.envelope.message_id,
|
|
588
|
+
actor: "merchant",
|
|
589
|
+
action: "clarification_response",
|
|
590
|
+
created_at: now(),
|
|
591
|
+
payload: {
|
|
592
|
+
type: "clarification_response",
|
|
593
|
+
answers: [
|
|
594
|
+
{ code: "quote_history", value: hits.length > 0 ? "available" : "empty", note: summary },
|
|
595
|
+
],
|
|
596
|
+
},
|
|
597
|
+
public_message: summary,
|
|
598
|
+
});
|
|
599
|
+
return {
|
|
600
|
+
kind: "accepted",
|
|
601
|
+
taskState: "completed",
|
|
602
|
+
message: {
|
|
603
|
+
role: "agent",
|
|
604
|
+
parts: [
|
|
605
|
+
{ kind: "data", data: { knp_envelope: reply } },
|
|
606
|
+
{ kind: "data", data: historyData },
|
|
607
|
+
],
|
|
608
|
+
messageId: reply.message_id,
|
|
609
|
+
},
|
|
610
|
+
};
|
|
611
|
+
};
|
|
320
612
|
/** 审查 BUG-10:把入站 action 映射到相位机事件并推进;非法转换(不在
|
|
321
613
|
* 转换表内的组合)fail-closed 返回 false(调用方 decline state_conflict)。
|
|
322
614
|
* clarification 由商家的结构化 clarification_response 应答同轮恢复,
|
|
@@ -428,6 +720,124 @@ export function createMerchantHandler(options) {
|
|
|
428
720
|
occurred_at: now(),
|
|
429
721
|
});
|
|
430
722
|
};
|
|
723
|
+
// 买家识别与交易回执 · K4:商家协议持久化。
|
|
724
|
+
// 有效 accept 在相位推进前预写 `agreement_issued` 证据事件(商家协议号、
|
|
725
|
+
// 被接受报价、条款摘要、已验签买方绑定与完整协议结果)。相位与协议无法在
|
|
726
|
+
// 两次 append 间原子提交,因此按“预写证据补齐”:任一中断点重试都复用
|
|
727
|
+
// 同一商家协议号与摘要;终态但无证据的历史窗口保持拒绝(不生成替代协议)。
|
|
728
|
+
/** 查找本磋商已落账的协议证据(offer_id 与 terms_digest 强匹配)。 */
|
|
729
|
+
const findAgreementEvidence = (negotiationId, acceptedOfferId, termsDigest) => {
|
|
730
|
+
if (acceptedOfferId === "" || termsDigest === "")
|
|
731
|
+
return undefined;
|
|
732
|
+
try {
|
|
733
|
+
for (const event of ledger.events(negotiationId)) {
|
|
734
|
+
if (event.event_kind !== "system")
|
|
735
|
+
continue;
|
|
736
|
+
const result = event.outcome.kind === "ok" ? event.outcome.result : undefined;
|
|
737
|
+
if (result === undefined || result["kind"] !== "agreement_issued")
|
|
738
|
+
continue;
|
|
739
|
+
const agreement = result["agreement"];
|
|
740
|
+
if (agreement === null || typeof agreement !== "object")
|
|
741
|
+
continue;
|
|
742
|
+
const artifact = agreement;
|
|
743
|
+
const evidenceOffer = String(result["accepted_offer_id"] ?? artifact["accepted_offer_id"] ?? "");
|
|
744
|
+
if (evidenceOffer !== acceptedOfferId)
|
|
745
|
+
continue;
|
|
746
|
+
if (String(artifact["terms_digest"] ?? "") !== termsDigest)
|
|
747
|
+
continue;
|
|
748
|
+
return {
|
|
749
|
+
agreement: artifact,
|
|
750
|
+
buyer_binding: String(result["buyer_binding"] ?? ""),
|
|
751
|
+
accept_message_id: result["accept_message_id"],
|
|
752
|
+
accept_wire_digest: result["accept_wire_digest"],
|
|
753
|
+
};
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
catch {
|
|
757
|
+
// 账本不可读:按无证据处理(后续路径各自 fail-closed)。
|
|
758
|
+
}
|
|
759
|
+
return undefined;
|
|
760
|
+
};
|
|
761
|
+
/** 预写协议证据(system 事件,append 原子)。 */
|
|
762
|
+
const appendAgreementEvidence = async (input) => {
|
|
763
|
+
await ledger.append({
|
|
764
|
+
event_kind: "system",
|
|
765
|
+
negotiation_id: input.negotiationId,
|
|
766
|
+
agreement_id: String(input.agreement["agreement_id"] ?? ""),
|
|
767
|
+
terms_digest: String(input.agreement["terms_digest"] ?? ""),
|
|
768
|
+
identity: {
|
|
769
|
+
sender_identity: sender,
|
|
770
|
+
counterparty_identity: boundPeers.get(input.negotiationId) ?? counterparty,
|
|
771
|
+
actor: "merchant",
|
|
772
|
+
},
|
|
773
|
+
capability: { capability: MERCHANT_CAPABILITY, protocol_version: "1.0" },
|
|
774
|
+
outcome: {
|
|
775
|
+
kind: "ok",
|
|
776
|
+
result: {
|
|
777
|
+
kind: "agreement_issued",
|
|
778
|
+
agreement: input.agreement,
|
|
779
|
+
accepted_offer_id: input.acceptedOfferId,
|
|
780
|
+
buyer_binding: input.buyerBinding,
|
|
781
|
+
accept_message_id: input.acceptMessageId,
|
|
782
|
+
accept_wire_digest: input.acceptWireDigest,
|
|
783
|
+
},
|
|
784
|
+
},
|
|
785
|
+
occurred_at: now(),
|
|
786
|
+
});
|
|
787
|
+
};
|
|
788
|
+
const recordAgreementReceipt = (negotiationId, agreement, identity) => {
|
|
789
|
+
const receipts = options.commerceReceipts;
|
|
790
|
+
const binding = thumbprintFromOpenInquiryIdentity(identity);
|
|
791
|
+
if (receipts === undefined || binding === undefined)
|
|
792
|
+
return;
|
|
793
|
+
const agreementId = String(agreement.agreement_id);
|
|
794
|
+
void receipts.dispatcher.enqueueAndDeliver({
|
|
795
|
+
event_id: `merchant-agreement:${agreementId}`,
|
|
796
|
+
event_type: "merchant_agreement_issued",
|
|
797
|
+
merchant_id: receipts.merchantId,
|
|
798
|
+
negotiation_id: negotiationId,
|
|
799
|
+
merchant_agreement_id: agreementId,
|
|
800
|
+
accepted_offer_id: String(agreement.accepted_offer_id),
|
|
801
|
+
terms_digest: String(agreement.terms_digest),
|
|
802
|
+
buyer_binding: binding,
|
|
803
|
+
occurred_at: String(agreement.created_at),
|
|
804
|
+
}).catch(() => { });
|
|
805
|
+
};
|
|
806
|
+
const recoverReceipts = () => {
|
|
807
|
+
if (options.commerceReceipts === undefined)
|
|
808
|
+
return;
|
|
809
|
+
for (const negotiationId of ledger.listNegotiations()) {
|
|
810
|
+
try {
|
|
811
|
+
const events = ledger.events(negotiationId);
|
|
812
|
+
const terminal = [...events].reverse().find((event) => event.state_transition !== undefined);
|
|
813
|
+
for (const event of events) {
|
|
814
|
+
if (event.event_kind === "message_sent" && event.wire_payload !== undefined) {
|
|
815
|
+
recordEntryReceipt(event.wire_payload);
|
|
816
|
+
}
|
|
817
|
+
if (terminal?.state_transition?.to_phase !== "AGREEMENT_REACHED" || event.event_kind !== "system")
|
|
818
|
+
continue;
|
|
819
|
+
const result = event.outcome.kind === "ok" ? event.outcome.result : undefined;
|
|
820
|
+
if (result?.kind === "agreement_issued" && result.agreement !== null && typeof result.agreement === "object") {
|
|
821
|
+
recordAgreementReceipt(negotiationId, result.agreement, String(result.buyer_binding ?? ""));
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
catch {
|
|
826
|
+
// One unreadable negotiation must not prevent recovery of later entries.
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
};
|
|
830
|
+
/** 终态重放:证据命中时按原协议幂等返回(B6)。 */
|
|
831
|
+
const replayedAgreementResult = (agreement) => ({
|
|
832
|
+
kind: "accepted",
|
|
833
|
+
taskState: "completed",
|
|
834
|
+
artifactParts: [{ kind: "data", data: { agreement } }],
|
|
835
|
+
message: {
|
|
836
|
+
role: "agent",
|
|
837
|
+
parts: [{ kind: "text", text: "Agreement reached (nonbinding)." }],
|
|
838
|
+
messageId: newMessageId(),
|
|
839
|
+
},
|
|
840
|
+
});
|
|
431
841
|
// 每 negotiation 已解析的真实商品价(sku → {priceMinor, currency}),带
|
|
432
842
|
// TTL(评审项 L3:此前永久累积、价格永不刷新——长驻 merchant 节点内存
|
|
433
843
|
// 单调增长且价目陈旧)。
|
|
@@ -563,6 +973,30 @@ export function createMerchantHandler(options) {
|
|
|
563
973
|
endsAt: value.endsAt,
|
|
564
974
|
};
|
|
565
975
|
};
|
|
976
|
+
const recordEntryReceipt = (reply) => {
|
|
977
|
+
// K6:带成交入口的报价正式发出(message_sent 已落账)→ 上报“商家已
|
|
978
|
+
// 提供入口”(可早于协议,按报价引用关联)。预置公开链接与被拒绝审批
|
|
979
|
+
// 不触发——本函数只在正式报价发布路径上调用。上报失败不影响报价。
|
|
980
|
+
if (options.commerceReceipts !== undefined && ["offer", "counter_offer", "conditional_offer"].includes(reply.action)) {
|
|
981
|
+
const payload = reply.payload;
|
|
982
|
+
const termsPayload = payload.terms ?? payload.proposed_terms ?? payload.base_terms;
|
|
983
|
+
if (termsPayload !== undefined && termsPayload.handoff_destination !== undefined) {
|
|
984
|
+
const entry = {
|
|
985
|
+
event_id: `merchant-entry:${reply.message_id}`,
|
|
986
|
+
event_type: "merchant_entry_offered",
|
|
987
|
+
merchant_id: options.commerceReceipts.merchantId,
|
|
988
|
+
entry_type: "quote_handoff",
|
|
989
|
+
quote_ref: {
|
|
990
|
+
negotiation_id: reply.negotiation_id,
|
|
991
|
+
offer_id: String(reply.payload.offer_id ?? ""),
|
|
992
|
+
terms_digest: contentDigest(termsPayload),
|
|
993
|
+
},
|
|
994
|
+
occurred_at: reply.created_at,
|
|
995
|
+
};
|
|
996
|
+
void options.commerceReceipts.dispatcher.enqueueAndDeliver(entry).catch(() => { });
|
|
997
|
+
}
|
|
998
|
+
}
|
|
999
|
+
};
|
|
566
1000
|
const appendSent = async (reply, snapshot) => {
|
|
567
1001
|
await ledger.append({
|
|
568
1002
|
event_kind: "message_sent",
|
|
@@ -586,6 +1020,7 @@ export function createMerchantHandler(options) {
|
|
|
586
1020
|
});
|
|
587
1021
|
if (snapshot !== undefined)
|
|
588
1022
|
quoteSources.set(reply.negotiation_id, { offer_id: String(reply.payload.offer_id), snapshot });
|
|
1023
|
+
recordEntryReceipt(reply);
|
|
589
1024
|
if (["offer", "counter_offer", "conditional_offer"].includes(reply.action)) {
|
|
590
1025
|
activeObjectByNegotiation.set(reply.negotiation_id, {
|
|
591
1026
|
offerId: String(reply.payload.offer_id),
|
|
@@ -690,6 +1125,28 @@ export function createMerchantHandler(options) {
|
|
|
690
1125
|
}
|
|
691
1126
|
return {
|
|
692
1127
|
name: "kiwi-agent-serve-merchant",
|
|
1128
|
+
async recoverAccept(ctx) {
|
|
1129
|
+
if (ctx.envelope.action !== "accept_nonbinding" || ctx.identityVerified !== true)
|
|
1130
|
+
return undefined;
|
|
1131
|
+
const { negotiation_id, message_id, digest } = ctx.envelope;
|
|
1132
|
+
const payload = ctx.envelope.payload;
|
|
1133
|
+
const evidence = findAgreementEvidence(negotiation_id, payload.offer_id, payload.terms_digest);
|
|
1134
|
+
if (evidence === undefined || evidence.buyer_binding !== ctx.senderIdentity ||
|
|
1135
|
+
evidence.accept_message_id !== message_id || evidence.accept_wire_digest !== digest)
|
|
1136
|
+
return undefined;
|
|
1137
|
+
syncPhaseFromLedger(negotiation_id);
|
|
1138
|
+
const phase = phaseStateByNegotiation.get(negotiation_id)?.phase;
|
|
1139
|
+
if (phase === "OFFER_OPEN") {
|
|
1140
|
+
boundPeers.set(negotiation_id, ctx.senderIdentity);
|
|
1141
|
+
await appendPhaseTransition(negotiation_id, "AGREEMENT_REACHED", phase);
|
|
1142
|
+
}
|
|
1143
|
+
else if (phase !== "AGREEMENT_REACHED") {
|
|
1144
|
+
return undefined;
|
|
1145
|
+
}
|
|
1146
|
+
recordAgreementReceipt(negotiation_id, evidence.agreement, evidence.buyer_binding);
|
|
1147
|
+
return replayedAgreementResult(evidence.agreement);
|
|
1148
|
+
},
|
|
1149
|
+
recoverReceipts,
|
|
693
1150
|
async handle(ctx) {
|
|
694
1151
|
return serialize(ctx.envelope.negotiation_id, async () => {
|
|
695
1152
|
const envelope = ctx.envelope;
|
|
@@ -715,6 +1172,17 @@ export function createMerchantHandler(options) {
|
|
|
715
1172
|
if (COMMERCIAL_ACTIONS.has(envelope.action) &&
|
|
716
1173
|
(closedNegotiations.has(negotiationId) ||
|
|
717
1174
|
(syncedPhase !== undefined && isTerminalPhase(syncedPhase)))) {
|
|
1175
|
+
// K4:accept 重放是幂等恢复不是重开——预写证据命中(同 offer、同摘要)
|
|
1176
|
+
// 时按原商家协议号与摘要返回原协议;无证据(历史窗口/伪造重放)仍拒绝。
|
|
1177
|
+
if (envelope.action === "accept_nonbinding" && syncedPhase === "AGREEMENT_REACHED") {
|
|
1178
|
+
const payload = envelope.payload;
|
|
1179
|
+
const evidence = findAgreementEvidence(negotiationId, typeof payload.offer_id === "string" ? payload.offer_id : "", typeof payload.terms_digest === "string" ? payload.terms_digest : "");
|
|
1180
|
+
if (evidence !== undefined && (evidence.buyer_binding === "" ||
|
|
1181
|
+
(ctx.identityVerified === true && evidence.buyer_binding === ctx.senderIdentity))) {
|
|
1182
|
+
recordAgreementReceipt(negotiationId, evidence.agreement, evidence.buyer_binding);
|
|
1183
|
+
return replayedAgreementResult(evidence.agreement);
|
|
1184
|
+
}
|
|
1185
|
+
}
|
|
718
1186
|
return declineReply("state_conflict");
|
|
719
1187
|
}
|
|
720
1188
|
if ((envelope.action === "withdraw" || envelope.action === "decline") &&
|
|
@@ -896,6 +1364,14 @@ export function createMerchantHandler(options) {
|
|
|
896
1364
|
}
|
|
897
1365
|
// P0a:inquiry 与普通 A2A 文本统一走接待引擎——结构化 SKU 是协议
|
|
898
1366
|
// 数据(权威),自由文本只是补充;引擎故障回退既有确定性路径。
|
|
1367
|
+
// K2:带历史查询扩展的 inquiry 先走授权历史查询(未委托/未验签/
|
|
1368
|
+
// 扩展不支持时返回 undefined → 走原接待路径)。
|
|
1369
|
+
const historyReply = await resolveQuoteHistoryReply(ctx);
|
|
1370
|
+
if (historyReply !== undefined)
|
|
1371
|
+
return historyReply;
|
|
1372
|
+
if (parseHistoryQueryExtension(ctx.envelope.payload.subject) !== undefined) {
|
|
1373
|
+
return textReply("历史报价不可用,请提供商品规格后继续询价。");
|
|
1374
|
+
}
|
|
899
1375
|
if (options.reception !== undefined) {
|
|
900
1376
|
try {
|
|
901
1377
|
const received = await options.reception.engine.respond({
|
|
@@ -917,9 +1393,25 @@ export function createMerchantHandler(options) {
|
|
|
917
1393
|
}
|
|
918
1394
|
: {}),
|
|
919
1395
|
});
|
|
920
|
-
|
|
1396
|
+
// K1:接待引擎判定为有效询价后才建档(空文本/无关消息的澄清
|
|
1397
|
+
// 不建档)。结构化 inquiry(带 sku)由引擎判 recognizedInquiry。
|
|
1398
|
+
if (received.recognizedInquiry) {
|
|
1399
|
+
const registered = await registerVerifiedCustomerIdentity(ctx, "inquiry");
|
|
1400
|
+
await applyDelegationFromMetadata(ctx, registered?.subject_id);
|
|
1401
|
+
}
|
|
1402
|
+
const history = options.quoteHistory?.store;
|
|
1403
|
+
const merchantId = options.customerIdentity?.merchantId;
|
|
1404
|
+
const historyNote = history !== undefined && merchantId !== undefined &&
|
|
1405
|
+
ctx.identityVerified === true && isOpenInquiryIdentity(ctx.senderIdentity) &&
|
|
1406
|
+
isQuoteHistoryRecall(buyerMessage ?? "")
|
|
1407
|
+
? quoteHistoryNote(history, { merchantId, actor: ctx.senderIdentity, sku: structuredSku })
|
|
1408
|
+
: "";
|
|
1409
|
+
return textReply(received.text + historyNote);
|
|
921
1410
|
}
|
|
922
|
-
catch {
|
|
1411
|
+
catch (error) {
|
|
1412
|
+
if (error instanceof StreamGateError) {
|
|
1413
|
+
return { kind: "error", protocolCode: "temporarily_unavailable", message: "reception stream busy" };
|
|
1414
|
+
}
|
|
923
1415
|
// 接待引擎不可用:回退确定性组装(下述既有路径)。
|
|
924
1416
|
}
|
|
925
1417
|
}
|
|
@@ -936,13 +1428,18 @@ export function createMerchantHandler(options) {
|
|
|
936
1428
|
}
|
|
937
1429
|
}
|
|
938
1430
|
const sku = structuredSku ?? interpreted?.sku;
|
|
1431
|
+
const language = interpreted?.language ?? replyLanguageOf(buyerMessage);
|
|
939
1432
|
if (sku === undefined) {
|
|
940
|
-
return textReply(
|
|
1433
|
+
return textReply(language === "zh"
|
|
1434
|
+
? "请提供商品 SKU、数量和单位,我才能按商品源给你正式报价。"
|
|
1435
|
+
: "Please provide a product SKU, quantity and declared unit for a source-backed quote.");
|
|
941
1436
|
}
|
|
942
1437
|
const topics = new Set(structuredTopics);
|
|
943
1438
|
for (const topic of interpreted?.topics ?? [])
|
|
944
1439
|
topics.add(topic);
|
|
945
|
-
|
|
1440
|
+
// 模型/解释器不可用时仍按买家原文识别主题,避免把具体问题答成模板。
|
|
1441
|
+
for (const topic of inferInquiryTopicsFromText(buyerMessage ?? ""))
|
|
1442
|
+
topics.add(topic);
|
|
946
1443
|
const resolved = await resolveProductOrDecline(sku);
|
|
947
1444
|
if ("declined" in resolved) {
|
|
948
1445
|
// 询价不是商业承诺:商品源确认不了就要求核对 SKU,不发 decline 信封。
|
|
@@ -978,13 +1475,32 @@ export function createMerchantHandler(options) {
|
|
|
978
1475
|
else if (topics.has("stock")) {
|
|
979
1476
|
lines.push(language === "zh" ? "库存状态待确认。" : "Stock status needs confirmation.");
|
|
980
1477
|
}
|
|
981
|
-
if (
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
1478
|
+
if (askAll || topics.has("delivery")) {
|
|
1479
|
+
const requestedDays = requestedDeliveryDaysOf(buyerMessage ?? "");
|
|
1480
|
+
if (delivery !== undefined) {
|
|
1481
|
+
const canMeet = requestedDays !== undefined &&
|
|
1482
|
+
Date.parse(delivery) <= Date.parse(now()) + requestedDays * 86_400_000;
|
|
1483
|
+
lines.push(requestedDays === undefined
|
|
1484
|
+
? language === "zh"
|
|
1485
|
+
? `预计交期不晚于 ${delivery.slice(0, 10)}。`
|
|
1486
|
+
: `Estimated delivery is by ${delivery.slice(0, 10)}.`
|
|
1487
|
+
: canMeet
|
|
1488
|
+
? language === "zh"
|
|
1489
|
+
? `您要求 ${requestedDays} 天到货;预计交期不晚于 ${delivery.slice(0, 10)},可以满足。`
|
|
1490
|
+
: `You asked for delivery within ${requestedDays} days; the estimate is by ${delivery.slice(0, 10)}, which meets it.`
|
|
1491
|
+
: language === "zh"
|
|
1492
|
+
? `您要求 ${requestedDays} 天到货;预计交期不晚于 ${delivery.slice(0, 10)},无法承诺 ${requestedDays} 天送达。`
|
|
1493
|
+
: `You asked for delivery within ${requestedDays} days; the estimate is by ${delivery.slice(0, 10)}, so that deadline cannot be committed.`);
|
|
1494
|
+
}
|
|
1495
|
+
else {
|
|
1496
|
+
lines.push(requestedDays !== undefined
|
|
1497
|
+
? language === "zh"
|
|
1498
|
+
? `您要求 ${requestedDays} 天到货;本店尚未配置可承诺的交期,无法确认 ${requestedDays} 天送达,请与商家确认。`
|
|
1499
|
+
: `You asked for delivery within ${requestedDays} days; this merchant has no committed lead time configured, so ${requestedDays}-day delivery cannot be confirmed.`
|
|
1500
|
+
: language === "zh"
|
|
1501
|
+
? "本店尚未配置可承诺的交期,交期待确认;请与商家确认。"
|
|
1502
|
+
: "Delivery timing needs confirmation with the merchant.");
|
|
1503
|
+
}
|
|
988
1504
|
}
|
|
989
1505
|
lines.push(language === "zh"
|
|
990
1506
|
? "以上为非约束性信息;请发送 RFQ(SKU、数量、单位)获取正式报价。"
|
|
@@ -1008,7 +1524,7 @@ export function createMerchantHandler(options) {
|
|
|
1008
1524
|
const product = resolved.product;
|
|
1009
1525
|
const { priceMinor, currency } = product;
|
|
1010
1526
|
if (unitMismatch(product.unit, requestedUnit, unitPolicy))
|
|
1011
|
-
return
|
|
1527
|
+
return unitDecline(product.unit);
|
|
1012
1528
|
const handoff_destination = product.stock === undefined || product.source_evidence === undefined ? undefined : product.handoff_destination;
|
|
1013
1529
|
if (factsUnusable(product, quantity))
|
|
1014
1530
|
return declineReply("temporarily_unavailable");
|
|
@@ -1036,7 +1552,12 @@ export function createMerchantHandler(options) {
|
|
|
1036
1552
|
})
|
|
1037
1553
|
: undefined;
|
|
1038
1554
|
const quoteNote = quoteMessage(product);
|
|
1039
|
-
|
|
1555
|
+
// 买家在 RFQ 里声明了到货时限时必须逐条回应,不能只回价格。
|
|
1556
|
+
const language = replyLanguageOf(envelope.public_message, buyerMessage);
|
|
1557
|
+
const deliveryNote = deliveryCommitmentNote(requestedDeliveryBeforeOf(envelope.payload), deliveryBefore(), language);
|
|
1558
|
+
const publicMessage = [advisedMessage, quoteNote.public_message, deliveryNote]
|
|
1559
|
+
.filter((part) => part !== undefined && part.trim() !== "")
|
|
1560
|
+
.join(" ") || undefined;
|
|
1040
1561
|
const reply = seedEnvelope({
|
|
1041
1562
|
negotiation_id: negotiationId,
|
|
1042
1563
|
in_reply_to: inReplyTo,
|
|
@@ -1077,6 +1598,9 @@ export function createMerchantHandler(options) {
|
|
|
1077
1598
|
// 源码核:原返修缺 offer_id,直报价接受仍 offer_unknown)。
|
|
1078
1599
|
conditionalByNegotiation.set(negotiationId, normalizeDirectOfferAsConditional(String(reply.payload.offer_id ?? ""), reply.payload.terms ?? {}, quantity));
|
|
1079
1600
|
await appendSent(reply, snapshotFor(sku, quantity, product, promotion));
|
|
1601
|
+
const registeredSubject = await registerVerifiedCustomerIdentity(ctx, "rfq");
|
|
1602
|
+
await applyDelegationFromMetadata(ctx, registeredSubject?.subject_id);
|
|
1603
|
+
await indexHistoryOffer(ctx, reply, product);
|
|
1080
1604
|
return envelopeReply(reply);
|
|
1081
1605
|
}
|
|
1082
1606
|
case "offer": {
|
|
@@ -1097,7 +1621,7 @@ export function createMerchantHandler(options) {
|
|
|
1097
1621
|
const product = resolved.product;
|
|
1098
1622
|
const { priceMinor, currency } = product;
|
|
1099
1623
|
if (unitMismatch(product.unit, requestedUnit, unitPolicy))
|
|
1100
|
-
return
|
|
1624
|
+
return unitDecline(product.unit);
|
|
1101
1625
|
const handoff_destination = product.stock === undefined || product.source_evidence === undefined ? undefined : product.handoff_destination;
|
|
1102
1626
|
if (factsUnusable(product, quantity))
|
|
1103
1627
|
return declineReply("temporarily_unavailable");
|
|
@@ -1157,6 +1681,7 @@ export function createMerchantHandler(options) {
|
|
|
1157
1681
|
// review 2-3:还价(counter_offer)同样登记——买家接受直还价走通主路径。
|
|
1158
1682
|
conditionalByNegotiation.set(negotiationId, normalizeDirectOfferAsConditional(String(reply.payload.offer_id ?? ""), reply.payload.proposed_terms ?? {}, quantity));
|
|
1159
1683
|
await appendSent(reply, snapshotFor(sku, quantity, product, promotion));
|
|
1684
|
+
await indexHistoryOffer(ctx, reply, product);
|
|
1160
1685
|
return envelopeReply(reply);
|
|
1161
1686
|
}
|
|
1162
1687
|
case "counter_offer": {
|
|
@@ -1177,7 +1702,7 @@ export function createMerchantHandler(options) {
|
|
|
1177
1702
|
const product = resolved.product;
|
|
1178
1703
|
const { priceMinor, currency } = product;
|
|
1179
1704
|
if (unitMismatch(product.unit, requestedUnit, unitPolicy))
|
|
1180
|
-
return
|
|
1705
|
+
return unitDecline(product.unit);
|
|
1181
1706
|
const handoff_destination = product.stock === undefined || product.source_evidence === undefined ? undefined : product.handoff_destination;
|
|
1182
1707
|
if (factsUnusable(product, quantity))
|
|
1183
1708
|
return declineReply("temporarily_unavailable");
|
|
@@ -1279,6 +1804,34 @@ export function createMerchantHandler(options) {
|
|
|
1279
1804
|
stock_available: product.stock !== undefined && quantity <= product.stock,
|
|
1280
1805
|
extraQuantities: [bulkThreshold],
|
|
1281
1806
|
});
|
|
1807
|
+
// 买家还价低于公开价但商家没有自动折扣时,不能只回一个相同价格——
|
|
1808
|
+
// 必须明确说明“为什么没降”,否则买家会认为商家没看懂还价。
|
|
1809
|
+
const counterMinor = typeof buyerCounterMinor === "number" && Number.isFinite(buyerCounterMinor)
|
|
1810
|
+
? buyerCounterMinor
|
|
1811
|
+
: undefined;
|
|
1812
|
+
const counterBelowPublic = authorizedMinor === undefined &&
|
|
1813
|
+
counterMinor !== undefined &&
|
|
1814
|
+
counterMinor < priceMinor &&
|
|
1815
|
+
counterMinor < publicBoundMinor;
|
|
1816
|
+
const counterLanguage = replyLanguageOf(envelope.public_message, buyerMessage);
|
|
1817
|
+
const counterNote = counterBelowPublic && counterMinor !== undefined
|
|
1818
|
+
? publicBoundMinor >= priceMinor
|
|
1819
|
+
? counterLanguage === "zh"
|
|
1820
|
+
? `您的还价 ${(counterMinor / 100).toFixed(2)} ${currency} 低于本店当前公开报价 ${(priceMinor / 100).toFixed(2)} ${currency};本店未配置自动折扣,本次仍按公开价报价。如需按数量或规格重新核价,请说明。`
|
|
1821
|
+
: `Your counter of ${(counterMinor / 100).toFixed(2)} ${currency} is below the merchant's current public price of ${(priceMinor / 100).toFixed(2)} ${currency}; no automatic discount is configured, so this reply keeps the public price.`
|
|
1822
|
+
: counterLanguage === "zh"
|
|
1823
|
+
? `您的还价 ${(counterMinor / 100).toFixed(2)} ${currency} 低于本店可自动确认的折扣范围;本次按可自动确认的价格报价,更低价需商家确认。`
|
|
1824
|
+
: `Your counter of ${(counterMinor / 100).toFixed(2)} ${currency} is below the merchant's automatic discount range; this reply uses the lowest automatically confirmable price, and any lower price needs merchant confirmation.`
|
|
1825
|
+
: undefined;
|
|
1826
|
+
const quoteNote = quoteMessage(product);
|
|
1827
|
+
const counterPublicMessage = [
|
|
1828
|
+
// 库存/来源未确认时的 needs_confirmation 安全文案必须保留。
|
|
1829
|
+
quoteNote.public_message,
|
|
1830
|
+
advisedMessage,
|
|
1831
|
+
counterNote,
|
|
1832
|
+
]
|
|
1833
|
+
.filter((part) => part !== undefined && part.trim() !== "")
|
|
1834
|
+
.join(" ") || undefined;
|
|
1282
1835
|
const reply = seedEnvelope({
|
|
1283
1836
|
negotiation_id: negotiationId,
|
|
1284
1837
|
in_reply_to: inReplyTo,
|
|
@@ -1327,8 +1880,8 @@ export function createMerchantHandler(options) {
|
|
|
1327
1880
|
]
|
|
1328
1881
|
: [],
|
|
1329
1882
|
},
|
|
1330
|
-
...(
|
|
1331
|
-
? { public_message:
|
|
1883
|
+
...(counterPublicMessage !== undefined
|
|
1884
|
+
? { public_message: counterPublicMessage }
|
|
1332
1885
|
: quoteMessage(product)),
|
|
1333
1886
|
});
|
|
1334
1887
|
conditionalByNegotiation.set(negotiationId, {
|
|
@@ -1343,6 +1896,7 @@ export function createMerchantHandler(options) {
|
|
|
1343
1896
|
if (!advanced)
|
|
1344
1897
|
return declineReply("state_conflict");
|
|
1345
1898
|
await appendSent(reply, snapshotFor(sku, quantity, product, authorityPromotion));
|
|
1899
|
+
await indexHistoryOffer(ctx, reply, product);
|
|
1346
1900
|
return envelopeReply(reply);
|
|
1347
1901
|
}
|
|
1348
1902
|
case "clarification": {
|
|
@@ -1358,31 +1912,75 @@ export function createMerchantHandler(options) {
|
|
|
1358
1912
|
// 所问。这里只回答**可公开且确定**的事实;价格边界、库存精确值
|
|
1359
1913
|
// 不在这里披露(价格仍走 counter_offer 的公开折扣边界)。
|
|
1360
1914
|
const promised = deliveryBefore();
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1915
|
+
// 语言只看人类可读文本(买家摘要/原文/问题理由),不看协议字段名。
|
|
1916
|
+
const questionText = questions
|
|
1917
|
+
.map((q) => q.reason ?? "")
|
|
1918
|
+
.filter((reason) => reason.trim() !== "")
|
|
1919
|
+
.join(" ");
|
|
1920
|
+
const clarificationText = [
|
|
1921
|
+
envelope.public_message,
|
|
1922
|
+
buyerMessage,
|
|
1923
|
+
questionText,
|
|
1924
|
+
]
|
|
1925
|
+
.filter((part) => part !== undefined && part.trim() !== "")
|
|
1926
|
+
.join(" ");
|
|
1927
|
+
const language = replyLanguageOf(clarificationText);
|
|
1928
|
+
const requestedDays = requestedDeliveryDaysOf(clarificationText);
|
|
1929
|
+
const deliveryAnswer = () => {
|
|
1930
|
+
if (promised === undefined) {
|
|
1931
|
+
return requestedDays !== undefined
|
|
1932
|
+
? language === "zh"
|
|
1933
|
+
? `您要求 ${requestedDays} 天到货;本店尚未配置可承诺的交期,无法确认 ${requestedDays} 天送达,请与商家确认`
|
|
1934
|
+
: `You asked for delivery within ${requestedDays} days; this merchant has no committed lead time configured, so ${requestedDays}-day delivery cannot be confirmed`
|
|
1935
|
+
: language === "zh"
|
|
1936
|
+
? "本店尚未配置可承诺的交期,交期待确认,请与商家确认"
|
|
1937
|
+
: "delivery timing is not configured and needs confirmation with the merchant";
|
|
1938
|
+
}
|
|
1939
|
+
const promisedDay = promised.slice(0, 10);
|
|
1940
|
+
if (requestedDays === undefined) {
|
|
1941
|
+
return language === "zh"
|
|
1942
|
+
? `预计交期不晚于 ${promisedDay}`
|
|
1943
|
+
: `delivery before ${promised}`;
|
|
1944
|
+
}
|
|
1945
|
+
const canMeet = Date.parse(promised) <= Date.parse(now()) + requestedDays * 86_400_000;
|
|
1946
|
+
if (canMeet) {
|
|
1947
|
+
return language === "zh"
|
|
1948
|
+
? `您要求 ${requestedDays} 天到货;预计交期不晚于 ${promisedDay},可以满足`
|
|
1949
|
+
: `you asked for delivery within ${requestedDays} days; the estimate is by ${promised}, which meets it`;
|
|
1950
|
+
}
|
|
1951
|
+
return language === "zh"
|
|
1952
|
+
? `您要求 ${requestedDays} 天到货;预计交期不晚于 ${promisedDay},无法承诺 ${requestedDays} 天送达`
|
|
1953
|
+
: `you asked for delivery within ${requestedDays} days; the estimate is by ${promised}, so that deadline cannot be committed`;
|
|
1954
|
+
};
|
|
1955
|
+
const answerFor = (field, reason) => {
|
|
1956
|
+
const key = `${field} ${reason}`.toLowerCase();
|
|
1957
|
+
if (/delivery|eta|ship|lead.?time|交期|发货|送达|到货|几天|多少天|\d+\s*(?:天|日)|arrive/.test(key)) {
|
|
1958
|
+
return deliveryAnswer();
|
|
1367
1959
|
}
|
|
1368
1960
|
if (/stock|inventory|availability|库存|现货/.test(key)) {
|
|
1369
|
-
return
|
|
1961
|
+
return language === "zh"
|
|
1962
|
+
? "库存状态需商家确认;报价按当前可得库存计算"
|
|
1963
|
+
: "stock level needs merchant confirmation; the quote assumes currently available inventory";
|
|
1370
1964
|
}
|
|
1371
1965
|
if (/price|discount|价格|优惠|便宜/.test(key)) {
|
|
1372
|
-
return
|
|
1966
|
+
return language === "zh"
|
|
1967
|
+
? "报价按本店当前公开价执行;更大折扣需商家确认(非约束性)"
|
|
1968
|
+
: "the price follows the published quote; larger discounts need merchant confirmation (nonbinding)";
|
|
1373
1969
|
}
|
|
1374
1970
|
if (/alternative|model|sku|spec|型号|规格|替代/.test(key)) {
|
|
1375
|
-
return
|
|
1971
|
+
return language === "zh"
|
|
1972
|
+
? "只能自动报价已列出的型号;其他规格需商家确认"
|
|
1973
|
+
: "only listed models can be quoted automatically; other specifications need merchant confirmation";
|
|
1376
1974
|
}
|
|
1377
|
-
return
|
|
1378
|
-
?
|
|
1379
|
-
: "
|
|
1975
|
+
return language === "zh"
|
|
1976
|
+
? "已记录您的问题;其他条款需商家确认(非约束性)"
|
|
1977
|
+
: "your question is recorded; other terms need merchant confirmation (nonbinding)";
|
|
1380
1978
|
};
|
|
1381
1979
|
const answers = questions.map((q) => ({
|
|
1382
1980
|
field: q.field ?? "…",
|
|
1383
|
-
answer: answerFor(q.field ?? ""),
|
|
1981
|
+
answer: answerFor(q.field ?? "", q.reason ?? ""),
|
|
1384
1982
|
}));
|
|
1385
|
-
const publicMessage = Array.from(new Set(answers.map((a) => a.answer))).join("; ");
|
|
1983
|
+
const publicMessage = Array.from(new Set(answers.map((a) => a.answer))).join(language === "zh" ? ";" : "; ");
|
|
1386
1984
|
const reply = seedEnvelope({
|
|
1387
1985
|
negotiation_id: negotiationId,
|
|
1388
1986
|
in_reply_to: inReplyTo,
|
|
@@ -1393,7 +1991,7 @@ export function createMerchantHandler(options) {
|
|
|
1393
1991
|
type: "clarification_response",
|
|
1394
1992
|
answers,
|
|
1395
1993
|
},
|
|
1396
|
-
public_message: `${publicMessage}
|
|
1994
|
+
public_message: `${publicMessage}${language === "zh" ? "。" : "."}`,
|
|
1397
1995
|
});
|
|
1398
1996
|
// 出站应答即恢复(restore 边,§21.2):顶层推进已把相位挂到
|
|
1399
1997
|
// AWAITING_CLARIFICATION,clarification_response 同轮弹回
|
|
@@ -1505,24 +2103,41 @@ export function createMerchantHandler(options) {
|
|
|
1505
2103
|
}
|
|
1506
2104
|
// 审查 P1-C:相位机是权威守卫——§15 校验通过后、构建协议之前先
|
|
1507
2105
|
// 推进相位并检查返回值(BUG-10 语义不变:被拒的 accept 不得进入
|
|
1508
|
-
// AGREEMENT_REACHED
|
|
1509
|
-
//
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
2106
|
+
// AGREEMENT_REACHED)。accept 的唯一合法来源相位是 OFFER_OPEN,
|
|
2107
|
+
// 先判相位:拒绝路径不产生任何协议证据(无终态副作用)。
|
|
2108
|
+
const phaseBeforeAccept = phaseStateByNegotiation.get(negotiationId)?.phase ?? "OPEN";
|
|
2109
|
+
if (phaseBeforeAccept !== "OFFER_OPEN") {
|
|
2110
|
+
return declineReply("state_conflict");
|
|
2111
|
+
}
|
|
2112
|
+
// K4:预写证据补齐——先复用/生成协议,落账证据,再推进相位。
|
|
2113
|
+
// 任一中断点重试都复用同一商家协议号与摘要(B6)。
|
|
2114
|
+
const priorEvidence = findAgreementEvidence(negotiationId, acceptedOfferId, presentedDigest);
|
|
2115
|
+
const agreement = priorEvidence?.agreement ??
|
|
2116
|
+
buildAgreement({
|
|
2117
|
+
negotiation_id: negotiationId,
|
|
2118
|
+
accepted_offer_id: acceptedOfferId,
|
|
2119
|
+
agreed_terms: agreedTerms,
|
|
2120
|
+
created_at: now(),
|
|
2121
|
+
});
|
|
2122
|
+
if (priorEvidence === undefined) {
|
|
2123
|
+
await appendAgreementEvidence({
|
|
2124
|
+
negotiationId,
|
|
2125
|
+
agreement,
|
|
2126
|
+
acceptedOfferId,
|
|
2127
|
+
buyerBinding: ctx.identityVerified === true ? ctx.senderIdentity : "",
|
|
2128
|
+
acceptMessageId: envelope.message_id,
|
|
2129
|
+
acceptWireDigest: envelope.digest,
|
|
2130
|
+
});
|
|
2131
|
+
}
|
|
1513
2132
|
const advanced = await advancePhase(negotiationId, {
|
|
1514
2133
|
type: "accept_nonbinding",
|
|
1515
2134
|
offer_id: acceptedOfferId,
|
|
1516
2135
|
});
|
|
1517
2136
|
if (!advanced) {
|
|
2137
|
+
// OFFER_OPEN 前置判定后推进不会失败;防御性兜底保持 fail-closed。
|
|
1518
2138
|
return declineReply("state_conflict");
|
|
1519
2139
|
}
|
|
1520
|
-
|
|
1521
|
-
negotiation_id: negotiationId,
|
|
1522
|
-
accepted_offer_id: acceptedOfferId,
|
|
1523
|
-
agreed_terms: agreedTerms,
|
|
1524
|
-
created_at: now(),
|
|
1525
|
-
});
|
|
2140
|
+
recordAgreementReceipt(negotiationId, agreement, ctx.identityVerified === true ? ctx.senderIdentity : "");
|
|
1526
2141
|
const artifactPart = { kind: "data", data: { agreement } };
|
|
1527
2142
|
const message = {
|
|
1528
2143
|
role: "agent",
|