@harrylabsj/kiwi 0.12.31 → 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 +34 -0
- package/dist/a2a/server/merchant-handler.js +439 -19
- 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/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/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 +57 -5
- package/dist/buyer-core/service.js +275 -50
- 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 +13 -2
- package/dist/cli.js.map +1 -1
- package/dist/cloud/bootstrap.js +141 -7
- package/dist/cloud/bootstrap.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/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 +10 -0
- package/dist/http/merchant-management/api.js +443 -0
- 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 +2 -2
- 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 +7 -1
- 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 +1 -0
- package/dist/merchant/workbench-server.js +176 -2
- package/dist/merchant/workbench-server.js.map +1 -1
- 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-runtime/manager.d.ts +2 -0
- package/dist/merchant-runtime/manager.js +5 -0
- package/dist/merchant-runtime/manager.js.map +1 -1
- package/package.json +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";
|
|
@@ -113,12 +117,17 @@ export function requestedDeliveryBeforeOf(payload) {
|
|
|
113
117
|
* 时限与商家已配置的权威交期对齐;解析不出就不猜。
|
|
114
118
|
*/
|
|
115
119
|
export function requestedDeliveryDaysOf(text) {
|
|
116
|
-
|
|
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);
|
|
117
126
|
if (numeric !== null) {
|
|
118
127
|
const days = Number(numeric[1]);
|
|
119
128
|
return Number.isSafeInteger(days) && days > 0 ? days : undefined;
|
|
120
129
|
}
|
|
121
|
-
const chinese = /([一二两三四五六七八九十]+)\s*(?:个)?\s*(?:工作日|天|日)\s*(?:内|以内|到|到达|送到|送达|发到|发货|到货)?/.exec(text);
|
|
130
|
+
const chinese = /([一二两三四五六七八九十]+)\s*(?:个)?\s*(?:工作日|天|日(?=\s*(?:以)?内))\s*(?:内|以内|到|到达|送到|送达|发到|发货|到货)?/.exec(text);
|
|
122
131
|
if (chinese === null)
|
|
123
132
|
return undefined;
|
|
124
133
|
const digits = {
|
|
@@ -318,7 +327,7 @@ class ProductSourceUnavailableError extends Error {
|
|
|
318
327
|
*/
|
|
319
328
|
/**
|
|
320
329
|
* 单位校验:商品**没有单位就拒绝**(fail-closed,不知道按什么计量就不报价);
|
|
321
|
-
*
|
|
330
|
+
* 有单位时接受商家显式别名及内建的 box/盒、piece/个/件 1:1 写法。
|
|
322
331
|
* 不做“个/件 = 盒”的自动换算,也不改变数量/单价/金额。
|
|
323
332
|
*/
|
|
324
333
|
function unitMismatch(productUnit, requestedUnit, policy) {
|
|
@@ -333,6 +342,11 @@ const declineReply = (reasonCode = "state_conflict", message) => ({
|
|
|
333
342
|
...(message !== undefined ? { message } : {}),
|
|
334
343
|
taskState: "completed",
|
|
335
344
|
});
|
|
345
|
+
function unitDecline(productUnit) {
|
|
346
|
+
return declineReply("field_unsupported", productUnit?.trim()
|
|
347
|
+
? "询价数量单位与商品销售单位不匹配,请核对单位及包装数量后重新询价。"
|
|
348
|
+
: "商品尚未提供销售单位,请联系商家确认后重新询价。");
|
|
349
|
+
}
|
|
336
350
|
/** AC09/D03:统计入站商务动作的商品行数(rfq/offer/counter_offer)。 */
|
|
337
351
|
function multiItemCountOf(envelope) {
|
|
338
352
|
const payload = typeof envelope.payload === "object" && envelope.payload !== null
|
|
@@ -411,6 +425,190 @@ export function createMerchantHandler(options) {
|
|
|
411
425
|
"decline",
|
|
412
426
|
"cancel",
|
|
413
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
|
+
};
|
|
414
612
|
/** 审查 BUG-10:把入站 action 映射到相位机事件并推进;非法转换(不在
|
|
415
613
|
* 转换表内的组合)fail-closed 返回 false(调用方 decline state_conflict)。
|
|
416
614
|
* clarification 由商家的结构化 clarification_response 应答同轮恢复,
|
|
@@ -522,6 +720,124 @@ export function createMerchantHandler(options) {
|
|
|
522
720
|
occurred_at: now(),
|
|
523
721
|
});
|
|
524
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
|
+
});
|
|
525
841
|
// 每 negotiation 已解析的真实商品价(sku → {priceMinor, currency}),带
|
|
526
842
|
// TTL(评审项 L3:此前永久累积、价格永不刷新——长驻 merchant 节点内存
|
|
527
843
|
// 单调增长且价目陈旧)。
|
|
@@ -657,6 +973,30 @@ export function createMerchantHandler(options) {
|
|
|
657
973
|
endsAt: value.endsAt,
|
|
658
974
|
};
|
|
659
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
|
+
};
|
|
660
1000
|
const appendSent = async (reply, snapshot) => {
|
|
661
1001
|
await ledger.append({
|
|
662
1002
|
event_kind: "message_sent",
|
|
@@ -680,6 +1020,7 @@ export function createMerchantHandler(options) {
|
|
|
680
1020
|
});
|
|
681
1021
|
if (snapshot !== undefined)
|
|
682
1022
|
quoteSources.set(reply.negotiation_id, { offer_id: String(reply.payload.offer_id), snapshot });
|
|
1023
|
+
recordEntryReceipt(reply);
|
|
683
1024
|
if (["offer", "counter_offer", "conditional_offer"].includes(reply.action)) {
|
|
684
1025
|
activeObjectByNegotiation.set(reply.negotiation_id, {
|
|
685
1026
|
offerId: String(reply.payload.offer_id),
|
|
@@ -784,6 +1125,28 @@ export function createMerchantHandler(options) {
|
|
|
784
1125
|
}
|
|
785
1126
|
return {
|
|
786
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,
|
|
787
1150
|
async handle(ctx) {
|
|
788
1151
|
return serialize(ctx.envelope.negotiation_id, async () => {
|
|
789
1152
|
const envelope = ctx.envelope;
|
|
@@ -809,6 +1172,17 @@ export function createMerchantHandler(options) {
|
|
|
809
1172
|
if (COMMERCIAL_ACTIONS.has(envelope.action) &&
|
|
810
1173
|
(closedNegotiations.has(negotiationId) ||
|
|
811
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
|
+
}
|
|
812
1186
|
return declineReply("state_conflict");
|
|
813
1187
|
}
|
|
814
1188
|
if ((envelope.action === "withdraw" || envelope.action === "decline") &&
|
|
@@ -990,6 +1364,14 @@ export function createMerchantHandler(options) {
|
|
|
990
1364
|
}
|
|
991
1365
|
// P0a:inquiry 与普通 A2A 文本统一走接待引擎——结构化 SKU 是协议
|
|
992
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
|
+
}
|
|
993
1375
|
if (options.reception !== undefined) {
|
|
994
1376
|
try {
|
|
995
1377
|
const received = await options.reception.engine.respond({
|
|
@@ -1011,9 +1393,25 @@ export function createMerchantHandler(options) {
|
|
|
1011
1393
|
}
|
|
1012
1394
|
: {}),
|
|
1013
1395
|
});
|
|
1014
|
-
|
|
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);
|
|
1015
1410
|
}
|
|
1016
|
-
catch {
|
|
1411
|
+
catch (error) {
|
|
1412
|
+
if (error instanceof StreamGateError) {
|
|
1413
|
+
return { kind: "error", protocolCode: "temporarily_unavailable", message: "reception stream busy" };
|
|
1414
|
+
}
|
|
1017
1415
|
// 接待引擎不可用:回退确定性组装(下述既有路径)。
|
|
1018
1416
|
}
|
|
1019
1417
|
}
|
|
@@ -1126,7 +1524,7 @@ export function createMerchantHandler(options) {
|
|
|
1126
1524
|
const product = resolved.product;
|
|
1127
1525
|
const { priceMinor, currency } = product;
|
|
1128
1526
|
if (unitMismatch(product.unit, requestedUnit, unitPolicy))
|
|
1129
|
-
return
|
|
1527
|
+
return unitDecline(product.unit);
|
|
1130
1528
|
const handoff_destination = product.stock === undefined || product.source_evidence === undefined ? undefined : product.handoff_destination;
|
|
1131
1529
|
if (factsUnusable(product, quantity))
|
|
1132
1530
|
return declineReply("temporarily_unavailable");
|
|
@@ -1200,6 +1598,9 @@ export function createMerchantHandler(options) {
|
|
|
1200
1598
|
// 源码核:原返修缺 offer_id,直报价接受仍 offer_unknown)。
|
|
1201
1599
|
conditionalByNegotiation.set(negotiationId, normalizeDirectOfferAsConditional(String(reply.payload.offer_id ?? ""), reply.payload.terms ?? {}, quantity));
|
|
1202
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);
|
|
1203
1604
|
return envelopeReply(reply);
|
|
1204
1605
|
}
|
|
1205
1606
|
case "offer": {
|
|
@@ -1220,7 +1621,7 @@ export function createMerchantHandler(options) {
|
|
|
1220
1621
|
const product = resolved.product;
|
|
1221
1622
|
const { priceMinor, currency } = product;
|
|
1222
1623
|
if (unitMismatch(product.unit, requestedUnit, unitPolicy))
|
|
1223
|
-
return
|
|
1624
|
+
return unitDecline(product.unit);
|
|
1224
1625
|
const handoff_destination = product.stock === undefined || product.source_evidence === undefined ? undefined : product.handoff_destination;
|
|
1225
1626
|
if (factsUnusable(product, quantity))
|
|
1226
1627
|
return declineReply("temporarily_unavailable");
|
|
@@ -1280,6 +1681,7 @@ export function createMerchantHandler(options) {
|
|
|
1280
1681
|
// review 2-3:还价(counter_offer)同样登记——买家接受直还价走通主路径。
|
|
1281
1682
|
conditionalByNegotiation.set(negotiationId, normalizeDirectOfferAsConditional(String(reply.payload.offer_id ?? ""), reply.payload.proposed_terms ?? {}, quantity));
|
|
1282
1683
|
await appendSent(reply, snapshotFor(sku, quantity, product, promotion));
|
|
1684
|
+
await indexHistoryOffer(ctx, reply, product);
|
|
1283
1685
|
return envelopeReply(reply);
|
|
1284
1686
|
}
|
|
1285
1687
|
case "counter_offer": {
|
|
@@ -1300,7 +1702,7 @@ export function createMerchantHandler(options) {
|
|
|
1300
1702
|
const product = resolved.product;
|
|
1301
1703
|
const { priceMinor, currency } = product;
|
|
1302
1704
|
if (unitMismatch(product.unit, requestedUnit, unitPolicy))
|
|
1303
|
-
return
|
|
1705
|
+
return unitDecline(product.unit);
|
|
1304
1706
|
const handoff_destination = product.stock === undefined || product.source_evidence === undefined ? undefined : product.handoff_destination;
|
|
1305
1707
|
if (factsUnusable(product, quantity))
|
|
1306
1708
|
return declineReply("temporarily_unavailable");
|
|
@@ -1494,6 +1896,7 @@ export function createMerchantHandler(options) {
|
|
|
1494
1896
|
if (!advanced)
|
|
1495
1897
|
return declineReply("state_conflict");
|
|
1496
1898
|
await appendSent(reply, snapshotFor(sku, quantity, product, authorityPromotion));
|
|
1899
|
+
await indexHistoryOffer(ctx, reply, product);
|
|
1497
1900
|
return envelopeReply(reply);
|
|
1498
1901
|
}
|
|
1499
1902
|
case "clarification": {
|
|
@@ -1700,24 +2103,41 @@ export function createMerchantHandler(options) {
|
|
|
1700
2103
|
}
|
|
1701
2104
|
// 审查 P1-C:相位机是权威守卫——§15 校验通过后、构建协议之前先
|
|
1702
2105
|
// 推进相位并检查返回值(BUG-10 语义不变:被拒的 accept 不得进入
|
|
1703
|
-
// AGREEMENT_REACHED
|
|
1704
|
-
//
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
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
|
+
}
|
|
1708
2132
|
const advanced = await advancePhase(negotiationId, {
|
|
1709
2133
|
type: "accept_nonbinding",
|
|
1710
2134
|
offer_id: acceptedOfferId,
|
|
1711
2135
|
});
|
|
1712
2136
|
if (!advanced) {
|
|
2137
|
+
// OFFER_OPEN 前置判定后推进不会失败;防御性兜底保持 fail-closed。
|
|
1713
2138
|
return declineReply("state_conflict");
|
|
1714
2139
|
}
|
|
1715
|
-
|
|
1716
|
-
negotiation_id: negotiationId,
|
|
1717
|
-
accepted_offer_id: acceptedOfferId,
|
|
1718
|
-
agreed_terms: agreedTerms,
|
|
1719
|
-
created_at: now(),
|
|
1720
|
-
});
|
|
2140
|
+
recordAgreementReceipt(negotiationId, agreement, ctx.identityVerified === true ? ctx.senderIdentity : "");
|
|
1721
2141
|
const artifactPart = { kind: "data", data: { agreement } };
|
|
1722
2142
|
const message = {
|
|
1723
2143
|
role: "agent",
|