@harrylabsj/kiwi 0.12.4 → 0.12.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/a2a/node.d.ts +5 -1
- package/dist/a2a/node.js +68 -30
- package/dist/a2a/node.js.map +1 -1
- package/dist/a2a/server/merchant-handler.d.ts +4 -0
- package/dist/a2a/server/merchant-handler.js +123 -45
- package/dist/a2a/server/merchant-handler.js.map +1 -1
- package/dist/a2a/server/merchant-product-facts.d.ts +32 -0
- package/dist/a2a/server/merchant-product-facts.js +57 -0
- package/dist/a2a/server/merchant-product-facts.js.map +1 -0
- package/dist/agent/buyer/buyer-tools.js +285 -3
- package/dist/agent/buyer/buyer-tools.js.map +1 -1
- package/dist/agent/merchant/types.d.ts +4 -9
- package/dist/agent/merchant/types.js +14 -0
- package/dist/agent/merchant/types.js.map +1 -1
- package/dist/buyer-core/a2a-knp.d.ts +1 -0
- package/dist/buyer-core/a2a-knp.js +1 -1
- package/dist/buyer-core/a2a-knp.js.map +1 -1
- package/dist/buyer-core/a2a-negotiator.d.ts +2 -0
- package/dist/buyer-core/a2a-negotiator.js +20 -19
- package/dist/buyer-core/a2a-negotiator.js.map +1 -1
- package/dist/buyer-core/a2a-quote-fetcher.d.ts +2 -0
- package/dist/buyer-core/a2a-quote-fetcher.js +13 -5
- package/dist/buyer-core/a2a-quote-fetcher.js.map +1 -1
- package/dist/buyer-core/product-summary.d.ts +4 -0
- package/dist/buyer-core/product-summary.js +6 -0
- package/dist/buyer-core/product-summary.js.map +1 -1
- package/dist/buyer-core/protocol-recovery.d.ts +12 -1
- package/dist/buyer-core/protocol-recovery.js +59 -19
- package/dist/buyer-core/protocol-recovery.js.map +1 -1
- package/dist/buyer-core/receive-gate.d.ts +11 -0
- package/dist/buyer-core/receive-gate.js +107 -0
- package/dist/buyer-core/receive-gate.js.map +1 -0
- package/dist/cloud/bootstrap.d.ts +4 -0
- package/dist/cloud/bootstrap.js +69 -55
- package/dist/cloud/bootstrap.js.map +1 -1
- package/dist/cloud/file-listing-publication.js +6 -0
- package/dist/cloud/file-listing-publication.js.map +1 -1
- package/dist/cloud/main.d.ts +3 -0
- package/dist/cloud/main.js +47 -11
- package/dist/cloud/main.js.map +1 -1
- package/dist/cloud/product-import-tabular.d.ts +1 -1
- package/dist/cloud/product-import-tabular.js +263 -36
- package/dist/cloud/product-import-tabular.js.map +1 -1
- package/dist/cloud/product-source.d.ts +6 -1
- package/dist/cloud/product-source.js +47 -15
- package/dist/cloud/product-source.js.map +1 -1
- package/dist/commerce/data-source.d.ts +4 -0
- package/dist/commerce/data-source.js.map +1 -1
- package/dist/commerce/shopping-cli-source.js +4 -0
- package/dist/commerce/shopping-cli-source.js.map +1 -1
- package/dist/discovery/catalog-source/kiwi-source.d.ts +2 -0
- package/dist/discovery/catalog-source/kiwi-source.js +4 -0
- package/dist/discovery/catalog-source/kiwi-source.js.map +1 -1
- package/dist/handoff/index.d.ts +2 -1
- package/dist/handoff/index.js +1 -0
- package/dist/handoff/index.js.map +1 -1
- package/dist/handoff/launch-marketplace-policy.d.ts +149 -0
- package/dist/handoff/launch-marketplace-policy.js +301 -0
- package/dist/handoff/launch-marketplace-policy.js.map +1 -0
- package/dist/handoff/marketplace-product-link.d.ts +18 -0
- package/dist/handoff/marketplace-product-link.js +71 -0
- package/dist/handoff/marketplace-product-link.js.map +1 -0
- package/dist/handoff/transaction.d.ts +30 -0
- package/dist/handoff/transaction.js +64 -0
- package/dist/handoff/transaction.js.map +1 -1
- package/dist/handoff/url-safety.d.ts +3 -0
- package/dist/handoff/url-safety.js +4 -0
- package/dist/handoff/url-safety.js.map +1 -1
- package/dist/http/merchant-management/page.js +176 -45
- package/dist/http/merchant-management/page.js.map +1 -1
- package/dist/i18n.d.ts +1 -0
- package/dist/i18n.js +3 -0
- package/dist/i18n.js.map +1 -1
- package/dist/lifecycle/close.d.ts +24 -0
- package/dist/lifecycle/close.js +131 -0
- package/dist/lifecycle/close.js.map +1 -0
- package/dist/mcp/merchant-runtime-assembly.js +6 -4
- package/dist/mcp/merchant-runtime-assembly.js.map +1 -1
- package/dist/mcp/tools.js +17 -16
- package/dist/mcp/tools.js.map +1 -1
- package/dist/merchant/application/service.d.ts +4 -0
- package/dist/merchant/application/service.js.map +1 -1
- package/dist/merchant/stats-store.d.ts +3 -0
- package/dist/merchant/stats-store.js +15 -1
- package/dist/merchant/stats-store.js.map +1 -1
- package/dist/merchant-core/rfq/data-source-adapter.d.ts +2 -10
- package/dist/merchant-core/rfq/data-source-adapter.js +21 -9
- package/dist/merchant-core/rfq/data-source-adapter.js.map +1 -1
- package/dist/merchant-gateway/pinned-fetch.js +5 -2
- package/dist/merchant-gateway/pinned-fetch.js.map +1 -1
- package/package.json +10 -11
- package/scripts/check-install-toolchain.mjs +64 -40
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/** Internal source facts. No new public KNP/Listing schema is introduced. */
|
|
2
|
+
export interface ProductSourceEvidence {
|
|
3
|
+
system: string;
|
|
4
|
+
/** An upstream version or explicitly declared content-derived version, never read time. */
|
|
5
|
+
source_version?: string;
|
|
6
|
+
table_digest?: string;
|
|
7
|
+
updated_at?: string;
|
|
8
|
+
retrieved_at?: string;
|
|
9
|
+
}
|
|
10
|
+
export interface ResolvedProductFacts {
|
|
11
|
+
priceMinor: number;
|
|
12
|
+
currency: string;
|
|
13
|
+
unit?: string;
|
|
14
|
+
stock?: number;
|
|
15
|
+
handoff_destination?: string;
|
|
16
|
+
valid_until?: string;
|
|
17
|
+
source_evidence?: ProductSourceEvidence;
|
|
18
|
+
title?: string;
|
|
19
|
+
note?: string;
|
|
20
|
+
}
|
|
21
|
+
export interface QuoteSourceSnapshot extends ResolvedProductFacts {
|
|
22
|
+
sku: string;
|
|
23
|
+
quantity: number;
|
|
24
|
+
policy_digest: string;
|
|
25
|
+
promotion: Record<string, unknown>;
|
|
26
|
+
}
|
|
27
|
+
export declare function checkedProductStock(stock: unknown): number | undefined;
|
|
28
|
+
export declare function checkedSourceEvidence(value: unknown): ProductSourceEvidence | undefined;
|
|
29
|
+
/** Observation times/table-wide digest are provenance, not quote-value equality. */
|
|
30
|
+
export declare function quoteFactsStillValid(snapshot: QuoteSourceSnapshot, current: ResolvedProductFacts, nowMs: number): boolean;
|
|
31
|
+
/** Recover only complete, original hash-linked ledger snapshots; no new source lookup. */
|
|
32
|
+
export declare function recoverQuoteSourceSnapshot(value: unknown): QuoteSourceSnapshot | undefined;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
export function checkedProductStock(stock) {
|
|
2
|
+
if (stock === undefined)
|
|
3
|
+
return undefined;
|
|
4
|
+
if (typeof stock !== "number" || !Number.isSafeInteger(stock) || stock < 0) {
|
|
5
|
+
throw new TypeError("Invalid source stock fact");
|
|
6
|
+
}
|
|
7
|
+
return stock;
|
|
8
|
+
}
|
|
9
|
+
export function checkedSourceEvidence(value) {
|
|
10
|
+
if (value === undefined)
|
|
11
|
+
return undefined;
|
|
12
|
+
if (value === null || typeof value !== "object" || Array.isArray(value))
|
|
13
|
+
throw new TypeError("Invalid source evidence");
|
|
14
|
+
const raw = value;
|
|
15
|
+
if (typeof raw.system !== "string" || raw.system.trim() === "")
|
|
16
|
+
throw new TypeError("Missing source system");
|
|
17
|
+
const result = { system: raw.system };
|
|
18
|
+
for (const key of ["source_version", "table_digest", "updated_at", "retrieved_at"]) {
|
|
19
|
+
if (raw[key] !== undefined) {
|
|
20
|
+
if (typeof raw[key] !== "string" || raw[key].trim() === "")
|
|
21
|
+
throw new TypeError("Invalid source evidence field");
|
|
22
|
+
result[key] = raw[key];
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return result;
|
|
26
|
+
}
|
|
27
|
+
/** Observation times/table-wide digest are provenance, not quote-value equality. */
|
|
28
|
+
export function quoteFactsStillValid(snapshot, current, nowMs) {
|
|
29
|
+
if (snapshot.stock === undefined || current.stock === undefined || current.stock < snapshot.quantity)
|
|
30
|
+
return false;
|
|
31
|
+
if (snapshot.unit === undefined || snapshot.unit !== current.unit || snapshot.priceMinor !== current.priceMinor || snapshot.currency !== current.currency || snapshot.title !== current.title)
|
|
32
|
+
return false;
|
|
33
|
+
if (snapshot.handoff_destination !== current.handoff_destination || snapshot.valid_until !== current.valid_until)
|
|
34
|
+
return false;
|
|
35
|
+
if (current.valid_until !== undefined && (!Number.isFinite(Date.parse(current.valid_until)) || Date.parse(current.valid_until) <= nowMs))
|
|
36
|
+
return false;
|
|
37
|
+
if (snapshot.source_evidence === undefined || current.source_evidence === undefined)
|
|
38
|
+
return false;
|
|
39
|
+
return snapshot.source_evidence?.system === current.source_evidence?.system && snapshot.source_evidence?.source_version === current.source_evidence?.source_version;
|
|
40
|
+
}
|
|
41
|
+
/** Recover only complete, original hash-linked ledger snapshots; no new source lookup. */
|
|
42
|
+
export function recoverQuoteSourceSnapshot(value) {
|
|
43
|
+
if (value === null || typeof value !== "object" || Array.isArray(value))
|
|
44
|
+
return undefined;
|
|
45
|
+
const v = value;
|
|
46
|
+
if (typeof v.sku !== "string" || v.sku.trim() === "" || !Number.isSafeInteger(v.quantity) || v.quantity <= 0 || typeof v.unit !== "string" || v.unit.trim() === "" || !Number.isSafeInteger(v.priceMinor) || v.priceMinor < 0 || typeof v.currency !== "string" || v.currency.trim() === "" || typeof v.policy_digest !== "string" || v.promotion === null || typeof v.promotion !== "object")
|
|
47
|
+
return undefined;
|
|
48
|
+
try {
|
|
49
|
+
checkedProductStock(v.stock);
|
|
50
|
+
checkedSourceEvidence(v.source_evidence);
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
return v;
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=merchant-product-facts.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"merchant-product-facts.js","sourceRoot":"","sources":["../../../src/a2a/server/merchant-product-facts.ts"],"names":[],"mappings":"AA6BA,MAAM,UAAU,mBAAmB,CA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KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QAAE,OAAO,SAAS,CAAC;IAC1F,MAAM,CAAC,GAAG,KAA4B,CAAC;IACvC,IAAI,OAAO,CAAC,CAAC,GAAG,KAAK,QAAQ,IAAI,CAAC,CAAC,GAAG,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,QAAQ,IAAI,CAAC,IAAI,OAAO,CAAC,CAAC,IAAI,KAAK,QAAQ,IAAI,CAAC,CAAC,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,UAAU,GAAG,CAAC,IAAI,OAAO,CAAC,CAAC,QAAQ,KAAK,QAAQ,IAAI,CAAC,CAAC,QAAQ,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,OAAO,CAAC,CAAC,aAAa,KAAK,QAAQ,IAAI,CAAC,CAAC,SAAS,KAAK,IAAI,IAAI,OAAO,CAAC,CAAC,SAAS,KAAK,QAAQ;QAAE,OAAO,SAAS,CAAC;IAChZ,IAAI,CAAC;QAAC,mBAAmB,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;QAAC,qBAAqB,CAAC,CAAC,CAAC,eAAe,CAAC,CAAC;IAAC,CAAC;IAAC,MAAM,CAAC;QAAC,OAAO,SAAS,CAAC;IAAC,CAAC;IAC3G,OAAO,CAAC,CAAC;AACX,CAAC"}
|
|
@@ -19,7 +19,11 @@
|
|
|
19
19
|
* idempotency); the model never touches SQL or tokens.
|
|
20
20
|
*/
|
|
21
21
|
import { createHash } from "node:crypto";
|
|
22
|
-
import { HandoffIdempotencyStore, HandoffEventStore, createHandoffCandidate,
|
|
22
|
+
import { HandoffIdempotencyStore, HandoffEventStore, createHandoffCandidate, executeHandoff, validateDestination, validateExternalDestinationUrl, } from "../../handoff/index.js";
|
|
23
|
+
import { buildMarketplaceSourceBinding, MarketplaceLaunchPolicyError, parseMarketplaceTradeUrl, sameMarketplaceTarget, } from "../../handoff/launch-marketplace-policy.js";
|
|
24
|
+
import { isUrlDestinationType } from "../../handoff/destination.js";
|
|
25
|
+
import { CatalogSourceError } from "../../discovery/catalog-source/errors.js";
|
|
26
|
+
import { CommerceError } from "../../commerce/data-source.js";
|
|
23
27
|
import { toMinorUnits as losslessToMinorUnits } from "../../protocol/legacy-shopping-negotiation/money.js";
|
|
24
28
|
import { contentDigest } from "../../negotiation/jcs.js";
|
|
25
29
|
import { requireScopeCredential } from "../merchant/credential-broker.js";
|
|
@@ -1193,6 +1197,21 @@ export function buildBuyerTools(deps) {
|
|
|
1193
1197
|
if (!Number.isFinite(expiresInHours) || expiresInHours <= 0) {
|
|
1194
1198
|
return textResult("expires_in_hours 必须是正数(小时)。");
|
|
1195
1199
|
}
|
|
1200
|
+
// ── A651 包四:三平台市场形态 URL 目的地的可信交易来源门(创建期)──
|
|
1201
|
+
// 缺正常来源事实 → 固定 refusal,**不创建可交付候选**(fail-closed)。
|
|
1202
|
+
// listing_id 只取任务 shortlist 线索;权威以 configured CatalogSource
|
|
1203
|
+
// 新鲜重读为准,模型/搜索结果的 source 标签不自证。政策只覆盖三平台
|
|
1204
|
+
//(PACK4 §1):非市场形态 URL 保持原防钓鱼路径,不在此门内。
|
|
1205
|
+
if (isUrlDestinationType(destination.type) && marketplaceTradeRef(destination.ref) !== undefined) {
|
|
1206
|
+
const refusal = await resolveTradeSourceRefusal(deps, {
|
|
1207
|
+
taskId: p.task_id,
|
|
1208
|
+
agreement,
|
|
1209
|
+
destinationRef: destination.ref,
|
|
1210
|
+
nowMs: Date.parse(deps.now()),
|
|
1211
|
+
});
|
|
1212
|
+
if (refusal !== undefined)
|
|
1213
|
+
return textResult(refusal, { status: "needs_configuration" });
|
|
1214
|
+
}
|
|
1196
1215
|
// 审计链:stale 候选之后的新候选链接到被它取代的候选
|
|
1197
1216
|
//(supersedes_candidate_id;此前从不写入,stale 后的新候选是审计孤儿)。
|
|
1198
1217
|
const lastStale = [...handoff.ledger.events(agreement.negotiation_id).map((event) => handoff.ledger.resolvePayload(event))]
|
|
@@ -1219,6 +1238,35 @@ export function buildBuyerTools(deps) {
|
|
|
1219
1238
|
: {}),
|
|
1220
1239
|
requires_user_action: true,
|
|
1221
1240
|
});
|
|
1241
|
+
// ── A651 包四:可信来源 sidecar(批准证据与执行门输入)──────────
|
|
1242
|
+
// 在候选 digest 定型后构造,绑定 task/negotiation/agreement/terms/
|
|
1243
|
+
// candidate 与本次可信读取快照;随批准链持久化,执行期强制重核。
|
|
1244
|
+
// 仅三平台市场形态目的地需要 binding(政策范围)。
|
|
1245
|
+
let launchSourceBinding;
|
|
1246
|
+
if (isUrlDestinationType(destination.type) &&
|
|
1247
|
+
marketplaceTradeRef(destination.ref) !== undefined &&
|
|
1248
|
+
deps.catalogSource !== undefined) {
|
|
1249
|
+
const hintListingId = launchListingHint(deps, p.task_id, agreedTermsSku(agreement.agreed_terms), ownerAgentIdFromIdentity(agreement.merchant_identity_ref));
|
|
1250
|
+
if (hintListingId !== undefined) {
|
|
1251
|
+
try {
|
|
1252
|
+
launchSourceBinding = buildMarketplaceSourceBinding({
|
|
1253
|
+
catalogOrigin: deps.catalogSource.origin,
|
|
1254
|
+
listing: await readTrustedListing(deps, hintListingId),
|
|
1255
|
+
taskId: p.task_id,
|
|
1256
|
+
negotiationId: agreement.negotiation_id,
|
|
1257
|
+
agreementId: agreement.agreement_id,
|
|
1258
|
+
termsDigest: candidate.terms_digest,
|
|
1259
|
+
candidateDigest: candidate.candidate_digest,
|
|
1260
|
+
observedAt: deps.now(),
|
|
1261
|
+
});
|
|
1262
|
+
}
|
|
1263
|
+
catch {
|
|
1264
|
+
// 第二次读取失败 = 创建与绑定之间来源已不可稳定重读:不进入
|
|
1265
|
+
// 审批链(fail-closed; refusal 语义与创建期门一致)。
|
|
1266
|
+
return textResult("可信交易来源在候选创建后无法稳定重读;未创建可交付候选,可稍后重试。", { status: "needs_configuration" });
|
|
1267
|
+
}
|
|
1268
|
+
}
|
|
1269
|
+
}
|
|
1222
1270
|
const outcome = await routeWriteCandidate({
|
|
1223
1271
|
mode: deps.mode,
|
|
1224
1272
|
approvals: deps.approvals,
|
|
@@ -1228,7 +1276,11 @@ export function buildBuyerTools(deps) {
|
|
|
1228
1276
|
}, {
|
|
1229
1277
|
tool: "handoff_agreement",
|
|
1230
1278
|
task_id: p.task_id,
|
|
1231
|
-
arguments: {
|
|
1279
|
+
arguments: {
|
|
1280
|
+
task_id: p.task_id,
|
|
1281
|
+
candidate,
|
|
1282
|
+
...(launchSourceBinding !== undefined ? { launch_source_binding: launchSourceBinding } : {}),
|
|
1283
|
+
},
|
|
1232
1284
|
preconditions: { agreement_bound: true },
|
|
1233
1285
|
risk: "handoff_delivery",
|
|
1234
1286
|
execute: (approvedArgs) => executeHandoffForCandidate(deps, approvedArgs),
|
|
@@ -1483,6 +1535,169 @@ function declaredHandoffHost(agreement, candidate) {
|
|
|
1483
1535
|
return undefined;
|
|
1484
1536
|
}
|
|
1485
1537
|
}
|
|
1538
|
+
/**
|
|
1539
|
+
* A651 包四:可信交易来源接线(launch-marketplace-policy 的 buyer 侧宿主依赖)。
|
|
1540
|
+
*
|
|
1541
|
+
* 职责边界(PACK4-READONLY-PLAN §2/§3/§5):
|
|
1542
|
+
* - listing_id 只作选择线索(任务 shortlist 候选行);权威事实以 configured
|
|
1543
|
+
* CatalogSource.getListing **新鲜重读**为准;模型 shortlist 的 source 标签、
|
|
1544
|
+
* URL、verified:true 一律不自证。
|
|
1545
|
+
* - sidecar(MarketplaceSourceBinding)由可信读取形成,绑定任务/磋商/协议/
|
|
1546
|
+
* 条款/候选 digest,随批准链持久化;执行期 executeHandoff 强制门以新鲜
|
|
1547
|
+
* 重读逐项重核——来源变更 → stale(原审批流程新确认),无证据 →
|
|
1548
|
+
* needs_configuration(不交付)。
|
|
1549
|
+
* - 证明边界:仅证明「本次正常 Catalog 声明的 SKU 对应协议交付一致的目的地
|
|
1550
|
+
* URI/平台ID」,不是平台商品真值/价格/库存/下单/支付/预留。
|
|
1551
|
+
*/
|
|
1552
|
+
/** agreement 首行 SKU(当前协议实际接受商品;无单一行 → undefined)。 */
|
|
1553
|
+
function agreedTermsSku(agreed_terms) {
|
|
1554
|
+
const items = agreed_terms?.items;
|
|
1555
|
+
if (!Array.isArray(items) || items.length !== 1)
|
|
1556
|
+
return undefined;
|
|
1557
|
+
const sku = items[0]?.sku;
|
|
1558
|
+
return typeof sku === "string" && sku !== "" ? sku : undefined;
|
|
1559
|
+
}
|
|
1560
|
+
/** merchant:{catalogAgentId} 形态的协议归属 → catalog owner agent id。 */
|
|
1561
|
+
function ownerAgentIdFromIdentity(identityRef) {
|
|
1562
|
+
const prefix = "merchant:";
|
|
1563
|
+
return identityRef.startsWith(prefix) && identityRef.length > prefix.length
|
|
1564
|
+
? identityRef.slice(prefix.length)
|
|
1565
|
+
: undefined;
|
|
1566
|
+
}
|
|
1567
|
+
/** ListingRecord → 受控快照投影(结构满足 MarketplaceListingSnapshot;不携带
|
|
1568
|
+
* cost/floor/私有库存等未列字段)。 */
|
|
1569
|
+
function listingSnapshot(listing) {
|
|
1570
|
+
return {
|
|
1571
|
+
listing_id: listing.listing_id,
|
|
1572
|
+
listing_type: listing.listing_type,
|
|
1573
|
+
owner_agent_id: listing.owner_agent_id,
|
|
1574
|
+
merchant_id: listing.merchant_id,
|
|
1575
|
+
...(listing.source_product_ref !== undefined ? { source_product_ref: listing.source_product_ref } : {}),
|
|
1576
|
+
...(listing.source_revision !== undefined ? { source_revision: listing.source_revision } : {}),
|
|
1577
|
+
...(listing.handoff_destination_ref !== undefined ? { handoff_destination_ref: listing.handoff_destination_ref } : {}),
|
|
1578
|
+
listing_digest: listing.listing_digest,
|
|
1579
|
+
publication_state: listing.publication_state,
|
|
1580
|
+
listing_freshness_state: listing.listing_freshness_state,
|
|
1581
|
+
fresh_until: listing.fresh_until,
|
|
1582
|
+
updated_at: listing.updated_at,
|
|
1583
|
+
};
|
|
1584
|
+
}
|
|
1585
|
+
/** 批准链上带回的 sidecar 形状检查(不合法 = 无证据 → needs_configuration)。 */
|
|
1586
|
+
function extractSourceBinding(value) {
|
|
1587
|
+
if (typeof value !== "object" || value === null)
|
|
1588
|
+
return undefined;
|
|
1589
|
+
const b = value;
|
|
1590
|
+
if (b.binding_version !== "marketplace_source_binding/1")
|
|
1591
|
+
return undefined;
|
|
1592
|
+
if (typeof b.listing_id !== "string" || b.listing_id === "")
|
|
1593
|
+
return undefined;
|
|
1594
|
+
return value;
|
|
1595
|
+
}
|
|
1596
|
+
/** 三平台市场形态判定(吞掉分类错误;非市场形态 → undefined,保持原路径)。 */
|
|
1597
|
+
function marketplaceTradeRef(ref) {
|
|
1598
|
+
try {
|
|
1599
|
+
return parseMarketplaceTradeUrl(ref);
|
|
1600
|
+
}
|
|
1601
|
+
catch {
|
|
1602
|
+
return undefined;
|
|
1603
|
+
}
|
|
1604
|
+
}
|
|
1605
|
+
/**
|
|
1606
|
+
* listing_id 选择线索(PACK4 §2:只作线索,权威以重读为准):优先与协议
|
|
1607
|
+
* SKU/商家归属一致的 shortlist 候选行,避免误拿无关连接器候选。
|
|
1608
|
+
*/
|
|
1609
|
+
function launchListingHint(deps, taskId, agreedSku, ownerAgentId) {
|
|
1610
|
+
const rows = deps.store
|
|
1611
|
+
.listCandidates(taskId)
|
|
1612
|
+
.filter((c) => c.external_product_id !== "");
|
|
1613
|
+
const matched = (agreedSku !== undefined ? rows.find((c) => c.sku === agreedSku) : undefined) ??
|
|
1614
|
+
(ownerAgentId !== undefined ? rows.find((c) => c.owner_agent_id === ownerAgentId) : undefined) ??
|
|
1615
|
+
rows[0];
|
|
1616
|
+
const hint = matched?.external_product_id;
|
|
1617
|
+
return hint !== undefined && hint !== "" ? hint : undefined;
|
|
1618
|
+
}
|
|
1619
|
+
/**
|
|
1620
|
+
* CatalogSource 读取的瞬时/硬失败翻译:request_failed/request_timeout →
|
|
1621
|
+
* CommerceError("request_failed")(执行门按 probe_failed 可重试);其余原样
|
|
1622
|
+
* 传播(执行门按 needs_configuration 固定拒绝)。
|
|
1623
|
+
*/
|
|
1624
|
+
async function readTrustedListing(deps, listingId) {
|
|
1625
|
+
const catalogSource = deps.catalogSource;
|
|
1626
|
+
if (catalogSource === undefined) {
|
|
1627
|
+
throw new CommerceError("invalid_input", "kiwi-catalog listing source is not configured");
|
|
1628
|
+
}
|
|
1629
|
+
try {
|
|
1630
|
+
return listingSnapshot(await catalogSource.getListing(listingId));
|
|
1631
|
+
}
|
|
1632
|
+
catch (err) {
|
|
1633
|
+
if (err instanceof CatalogSourceError && (err.code === "request_failed" || err.code === "request_timeout")) {
|
|
1634
|
+
throw new CommerceError("request_failed", "trade source re-read temporarily unavailable");
|
|
1635
|
+
}
|
|
1636
|
+
throw err;
|
|
1637
|
+
}
|
|
1638
|
+
}
|
|
1639
|
+
/**
|
|
1640
|
+
* 创建期可信来源解析与协议一致性核验。返回固定 refusal 文案(undefined =
|
|
1641
|
+
* 来源事实成立);调用方据此**不创建**可交付候选(经理裁定 2026-10-13)。
|
|
1642
|
+
*/
|
|
1643
|
+
async function resolveTradeSourceRefusal(deps, input) {
|
|
1644
|
+
const catalogSource = deps.catalogSource;
|
|
1645
|
+
if (catalogSource === undefined) {
|
|
1646
|
+
return "URL 交易目的地需要可信 catalog 来源验证(needs_configuration/unverified):当前环境未配置 kiwi-catalog listing 源,不创建交接候选。";
|
|
1647
|
+
}
|
|
1648
|
+
const sku = agreedTermsSku(input.agreement.agreed_terms);
|
|
1649
|
+
if (sku === undefined) {
|
|
1650
|
+
return "协议条款缺少单一商品 SKU,无法绑定可信交易来源(needs_configuration);不创建交接候选。";
|
|
1651
|
+
}
|
|
1652
|
+
const ownerAgentId = ownerAgentIdFromIdentity(input.agreement.merchant_identity_ref);
|
|
1653
|
+
if (ownerAgentId === undefined) {
|
|
1654
|
+
return "协议商家归属无法映射到 catalog owner(needs_configuration);不创建交接候选。";
|
|
1655
|
+
}
|
|
1656
|
+
// listing_id 只是选择线索;权威以重读为准。
|
|
1657
|
+
const hint = launchListingHint(deps, input.taskId, sku, ownerAgentId);
|
|
1658
|
+
if (hint === undefined) {
|
|
1659
|
+
return "任务没有 shortlist listing 线索,无法定位可信交易来源(needs_configuration);不创建交接候选。";
|
|
1660
|
+
}
|
|
1661
|
+
let listing;
|
|
1662
|
+
try {
|
|
1663
|
+
listing = await readTrustedListing(deps, hint);
|
|
1664
|
+
}
|
|
1665
|
+
catch (err) {
|
|
1666
|
+
if (err instanceof CommerceError && err.code === "request_failed") {
|
|
1667
|
+
return "可信交易来源暂时不可读(catalog 暂不可达);未创建交接候选,可稍后重试。";
|
|
1668
|
+
}
|
|
1669
|
+
return "可信交易来源读取失败(listing 不存在或响应不合法);不创建交接候选。";
|
|
1670
|
+
}
|
|
1671
|
+
// 权威事实 vs 协议声明——逐项精确匹配;模型线索不一致即拒。
|
|
1672
|
+
if (listing.listing_type !== "product" || listing.publication_state !== "ACTIVE" || listing.listing_freshness_state !== "FRESH") {
|
|
1673
|
+
return "可信交易来源不是 ACTIVE/FRESH 的 product listing;不创建交接候选。";
|
|
1674
|
+
}
|
|
1675
|
+
const freshUntilMs = Date.parse(listing.fresh_until);
|
|
1676
|
+
if (!Number.isFinite(freshUntilMs) || freshUntilMs <= input.nowMs) {
|
|
1677
|
+
return "可信交易来源新鲜度已过(fresh_until);不创建交接候选。";
|
|
1678
|
+
}
|
|
1679
|
+
if (listing.source_product_ref !== sku) {
|
|
1680
|
+
return "可信交易来源 SKU 与协议接受商品不一致;不创建交接候选。";
|
|
1681
|
+
}
|
|
1682
|
+
if (listing.owner_agent_id !== ownerAgentId) {
|
|
1683
|
+
return "可信交易来源 owner 与协议商家归属不一致;不创建交接候选。";
|
|
1684
|
+
}
|
|
1685
|
+
if (listing.handoff_destination_ref !== input.destinationRef) {
|
|
1686
|
+
return "可信交易来源声明的成交入口与协议目的地不一致;不创建交接候选。";
|
|
1687
|
+
}
|
|
1688
|
+
// URL 交易目的地必须是三平台确切形态(P4 校准:不做自有域 fallback)——
|
|
1689
|
+
// binding 只为可平台绑定的市场入口形成;错误 ID/额外 query/混淆形态在此拒。
|
|
1690
|
+
try {
|
|
1691
|
+
parseMarketplaceTradeUrl(input.destinationRef);
|
|
1692
|
+
}
|
|
1693
|
+
catch (err) {
|
|
1694
|
+
if (err instanceof MarketplaceLaunchPolicyError) {
|
|
1695
|
+
return "协议目的地不是受支持平台的确切交易形态(jd/taobao/tmall);不创建交接候选。";
|
|
1696
|
+
}
|
|
1697
|
+
return "协议目的地不是合法 https URL;不创建交接候选。";
|
|
1698
|
+
}
|
|
1699
|
+
return undefined;
|
|
1700
|
+
}
|
|
1486
1701
|
function agreementFromTask(events) {
|
|
1487
1702
|
let merchantDomain;
|
|
1488
1703
|
for (let i = events.length - 1; i >= 0; i--) {
|
|
@@ -1551,6 +1766,10 @@ function handoffResultText(result) {
|
|
|
1551
1766
|
return textResult(`交接未执行:${result.reason}。`, { status: "rejected" });
|
|
1552
1767
|
case "expired":
|
|
1553
1768
|
return textResult(`交接未执行:候选已过期。`, { status: "expired" });
|
|
1769
|
+
case "needs_configuration":
|
|
1770
|
+
// A651 包四强制门:市场形态 URL 目的地缺可信交易来源上下文——固定
|
|
1771
|
+
// 拒绝、不交付;候选生命周期未动,补齐来源后可重试。
|
|
1772
|
+
return textResult(`交接未执行:${result.reason}。`, { status: "needs_configuration" });
|
|
1554
1773
|
}
|
|
1555
1774
|
}
|
|
1556
1775
|
/** 批准后的实际执行:agreementReader 从任务事件重读(§10 revalidation)。 */
|
|
@@ -1568,6 +1787,32 @@ async function executeHandoffForCandidate(deps, args) {
|
|
|
1568
1787
|
error: "任务记录缺少 agreement 快照,交接中止(fail-closed)",
|
|
1569
1788
|
};
|
|
1570
1789
|
}
|
|
1790
|
+
// ── A651 包四:三平台市场形态 URL 目的地的强制交易门输入 ─────────────
|
|
1791
|
+
// 审批链带回的 sidecar + 执行期 configured CatalogSource 新鲜重读;市场
|
|
1792
|
+
// 形态目的地无 sidecar(或不配置来源)→ executeHandoff 固定
|
|
1793
|
+
// needs_configuration,旧授权不迁移。readSource 是 normal host 依赖,不
|
|
1794
|
+
// 暴露给模型参数。非市场形态 URL 保持原防钓鱼路径(不走本门)。
|
|
1795
|
+
let launchContext;
|
|
1796
|
+
if (isUrlDestinationType(a.candidate.destination_type) && marketplaceTradeRef(a.candidate.destination_ref) !== undefined) {
|
|
1797
|
+
const binding = extractSourceBinding(a.launch_source_binding);
|
|
1798
|
+
if (binding === undefined || deps.catalogSource === undefined) {
|
|
1799
|
+
return {
|
|
1800
|
+
ok: false,
|
|
1801
|
+
status: "needs_configuration",
|
|
1802
|
+
error: "URL 交易目的地缺可信交易来源上下文(needs_configuration/unverified):不交付。",
|
|
1803
|
+
};
|
|
1804
|
+
}
|
|
1805
|
+
const catalogSource = deps.catalogSource;
|
|
1806
|
+
const bindingListingId = binding.listing_id;
|
|
1807
|
+
launchContext = {
|
|
1808
|
+
binding,
|
|
1809
|
+
catalogOrigin: catalogSource.origin,
|
|
1810
|
+
taskId: a.task_id,
|
|
1811
|
+
agreedSku: agreedTermsSku(agreement.agreed_terms) ?? "",
|
|
1812
|
+
ownerAgentId: ownerAgentIdFromIdentity(agreement.merchant_identity_ref) ?? "",
|
|
1813
|
+
readSource: () => readTrustedListing(deps, bindingListingId),
|
|
1814
|
+
};
|
|
1815
|
+
}
|
|
1571
1816
|
// created 事件在审批通过后才落链:被 /reject 的候选不在 Ledger 留下悬空的
|
|
1572
1817
|
// PROPOSED(此前先落链,/reject 后 /handoff 永远显示"从未获批"的候选,
|
|
1573
1818
|
// 且 LLM 重复调用会堆积无限候选事件与审批候选)。
|
|
@@ -1586,6 +1831,22 @@ async function executeHandoffForCandidate(deps, args) {
|
|
|
1586
1831
|
// 放行该外部主机(如 item.jd.com);LLM 现编的 destination_ref 仍限制在
|
|
1587
1832
|
// merchant 声明域(anti-phishing)。
|
|
1588
1833
|
const declaredHost = declaredHandoffHost(agreement, a.candidate);
|
|
1834
|
+
// A651 包四:三平台绑定时**每一跳**(首跳 + 全部重定向)强制「同平台同
|
|
1835
|
+
// opaque 商品 ID」——declaredHost OR 白名单只能证 host,不能证商品身份;
|
|
1836
|
+
// onHop 在既有 scheme/host/SSRF 守卫之后调用,只能抛错收紧,不能放行。
|
|
1837
|
+
const launchBinding = launchContext !== undefined ? extractSourceBinding(launchContext.binding) : undefined;
|
|
1838
|
+
const launchTarget = (() => {
|
|
1839
|
+
if (launchBinding === undefined)
|
|
1840
|
+
return undefined;
|
|
1841
|
+
try {
|
|
1842
|
+
return parseMarketplaceTradeUrl(a.candidate.destination_ref);
|
|
1843
|
+
}
|
|
1844
|
+
catch {
|
|
1845
|
+
// 正常接线不可达(binding 恒为三平台形态,verify 门先拒 ref 不等);
|
|
1846
|
+
// 防御:手造状态只落到 host 门,不放松 verify 强制门。
|
|
1847
|
+
return undefined;
|
|
1848
|
+
}
|
|
1849
|
+
})();
|
|
1589
1850
|
const result = await executeHandoff({
|
|
1590
1851
|
candidate: a.candidate,
|
|
1591
1852
|
ledger: handoff.ledger,
|
|
@@ -1605,7 +1866,28 @@ async function executeHandoffForCandidate(deps, args) {
|
|
|
1605
1866
|
negotiation_id: agreement.negotiation_id,
|
|
1606
1867
|
agreed_terms: agreement.agreed_terms,
|
|
1607
1868
|
}),
|
|
1608
|
-
urlSafety:
|
|
1869
|
+
urlSafety: (url) => validateExternalDestinationUrl(url, {
|
|
1870
|
+
...(agreement.merchant_domain !== undefined ? { expectedHost: agreement.merchant_domain } : {}),
|
|
1871
|
+
...(declaredHost !== undefined ? { allowlist: [declaredHost] } : {}),
|
|
1872
|
+
allowHttp: false,
|
|
1873
|
+
...(launchTarget !== undefined
|
|
1874
|
+
? {
|
|
1875
|
+
onHop: (hopUrl) => {
|
|
1876
|
+
let parsed;
|
|
1877
|
+
try {
|
|
1878
|
+
parsed = parseMarketplaceTradeUrl(hopUrl);
|
|
1879
|
+
}
|
|
1880
|
+
catch {
|
|
1881
|
+
throw new CommerceError("invalid_input", "destination hop left the marketplace trade form (same platform, same product id required)");
|
|
1882
|
+
}
|
|
1883
|
+
if (!sameMarketplaceTarget(parsed, launchTarget)) {
|
|
1884
|
+
throw new CommerceError("invalid_input", "destination redirect changed platform product identity");
|
|
1885
|
+
}
|
|
1886
|
+
},
|
|
1887
|
+
}
|
|
1888
|
+
: {}),
|
|
1889
|
+
}),
|
|
1890
|
+
...(launchContext !== undefined ? { launchContext } : {}),
|
|
1609
1891
|
now: deps.now,
|
|
1610
1892
|
});
|
|
1611
1893
|
const rendered = handoffResultText(result);
|