@harrylabsj/kiwi 0.11.0 → 0.12.1
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 +16 -1
- package/contracts/kiwi-contracts.lock.json +1 -1
- package/contracts/manifest.json +2 -2
- package/contracts/runtime-binding/0.1.2/document.schema.json +3 -1
- package/dist/a2a/node.d.ts +2 -0
- package/dist/a2a/node.js +92 -13
- package/dist/a2a/node.js.map +1 -1
- package/dist/a2a/server/merchant-handler.js +31 -17
- package/dist/a2a/server/merchant-handler.js.map +1 -1
- package/dist/a2a/server/pipeline.js +1 -0
- package/dist/a2a/server/pipeline.js.map +1 -1
- package/dist/a2a/server/types.d.ts +1 -0
- package/dist/agent/buyer/buyer-tools.d.ts +2 -2
- package/dist/agent/buyer/buyer-tools.js.map +1 -1
- package/dist/agent/chat-tools.d.ts +2 -11
- package/dist/agent/chat-tools.js.map +1 -1
- package/dist/agent/fake-chat-model.d.ts +3 -3
- package/dist/agent/fake-chat-model.js +3 -5
- package/dist/agent/fake-chat-model.js.map +1 -1
- package/dist/agent/kernel-builder.js +4 -4
- package/dist/agent/kernel-builder.js.map +1 -1
- package/dist/agent/kernel.d.ts +15 -5
- package/dist/agent/kernel.js +143 -67
- package/dist/agent/kernel.js.map +1 -1
- package/dist/agent/kiwi-tool.d.ts +33 -0
- package/dist/agent/kiwi-tool.js +17 -0
- package/dist/agent/kiwi-tool.js.map +1 -0
- package/dist/agent/merchant/merchant-tools.d.ts +2 -17
- package/dist/agent/merchant/merchant-tools.js.map +1 -1
- package/dist/agent/negotiation-chat.d.ts +3 -2
- package/dist/agent/negotiation-chat.js.map +1 -1
- package/dist/agent/session.d.ts +15 -3
- package/dist/agent/session.js +114 -122
- package/dist/agent/session.js.map +1 -1
- package/dist/agent/skills/tools.d.ts +2 -2
- package/dist/agent/skills/tools.js.map +1 -1
- package/dist/cli.d.ts +5 -1
- package/dist/cli.js +239 -32
- package/dist/cli.js.map +1 -1
- package/dist/cloud/binding/enrollment-challenge.d.ts +37 -0
- package/dist/cloud/binding/enrollment-challenge.js +153 -0
- package/dist/cloud/binding/enrollment-challenge.js.map +1 -0
- package/dist/cloud/binding/enrollment-heartbeat.d.ts +26 -0
- package/dist/cloud/binding/enrollment-heartbeat.js +146 -0
- package/dist/cloud/binding/enrollment-heartbeat.js.map +1 -0
- package/dist/cloud/binding/enrollment-key-recovery.d.ts +27 -0
- package/dist/cloud/binding/enrollment-key-recovery.js +127 -0
- package/dist/cloud/binding/enrollment-key-recovery.js.map +1 -0
- package/dist/cloud/binding/runtime-challenge.d.ts +1 -1
- package/dist/cloud/binding/runtime-challenge.js +8 -1
- package/dist/cloud/binding/runtime-challenge.js.map +1 -1
- package/dist/cloud/bootstrap.d.ts +2 -0
- package/dist/cloud/bootstrap.js +324 -18
- package/dist/cloud/bootstrap.js.map +1 -1
- package/dist/cloud/catalog-client.d.ts +305 -0
- package/dist/cloud/catalog-client.js +962 -0
- package/dist/cloud/catalog-client.js.map +1 -0
- package/dist/cloud/config.d.ts +12 -0
- package/dist/cloud/config.js +45 -0
- package/dist/cloud/config.js.map +1 -1
- package/dist/cloud/connect-service.d.ts +144 -0
- package/dist/cloud/connect-service.js +776 -0
- package/dist/cloud/connect-service.js.map +1 -0
- package/dist/cloud/file-listing-publication.d.ts +105 -0
- package/dist/cloud/file-listing-publication.js +436 -0
- package/dist/cloud/file-listing-publication.js.map +1 -0
- package/dist/cloud/http-router.d.ts +2 -0
- package/dist/cloud/http-router.js +8 -0
- package/dist/cloud/http-router.js.map +1 -1
- package/dist/cloud/listing-publisher.d.ts +45 -0
- package/dist/cloud/listing-publisher.js +244 -0
- package/dist/cloud/listing-publisher.js.map +1 -0
- package/dist/cloud/merchant-connect.d.ts +53 -0
- package/dist/cloud/merchant-connect.js +90 -0
- package/dist/cloud/merchant-connect.js.map +1 -0
- package/dist/cloud/merchant-identity.d.ts +14 -0
- package/dist/cloud/merchant-identity.js +76 -0
- package/dist/cloud/merchant-identity.js.map +1 -0
- package/dist/cloud/onboarding/catalog-pipeline.d.ts +135 -0
- package/dist/cloud/onboarding/catalog-pipeline.js +454 -0
- package/dist/cloud/onboarding/catalog-pipeline.js.map +1 -0
- package/dist/cloud/onboarding/steps.js +2 -2
- package/dist/cloud/onboarding/steps.js.map +1 -1
- package/dist/cloud/product-import-tabular.d.ts +100 -0
- package/dist/cloud/product-import-tabular.js +1073 -0
- package/dist/cloud/product-import-tabular.js.map +1 -0
- package/dist/cloud/product-source.d.ts +8 -1
- package/dist/cloud/product-source.js +63 -13
- package/dist/cloud/product-source.js.map +1 -1
- package/dist/config/profile.d.ts +4 -0
- package/dist/config/profile.js +14 -0
- package/dist/config/profile.js.map +1 -1
- package/dist/discovery/catalog-source/cloud-card.d.ts +1 -1
- package/dist/discovery/catalog-source/cloud-card.js +7 -1
- package/dist/discovery/catalog-source/cloud-card.js.map +1 -1
- package/dist/http/merchant-management/api.d.ts +37 -0
- package/dist/http/merchant-management/api.js +379 -7
- package/dist/http/merchant-management/api.js.map +1 -1
- package/dist/http/merchant-management/page.d.ts +4 -1
- package/dist/http/merchant-management/page.js +426 -25
- package/dist/http/merchant-management/page.js.map +1 -1
- package/dist/mcp/merchant-runtime-assembly.js +4 -1
- package/dist/mcp/merchant-runtime-assembly.js.map +1 -1
- package/dist/mcp/merchant-server.d.ts +2 -0
- package/dist/mcp/merchant-server.js +15 -0
- package/dist/mcp/merchant-server.js.map +1 -1
- package/dist/merchant/negotiation-observer.d.ts +92 -0
- package/dist/merchant/negotiation-observer.js +278 -0
- package/dist/merchant/negotiation-observer.js.map +1 -0
- package/dist/merchant/operations-report.d.ts +85 -0
- package/dist/merchant/operations-report.js +227 -0
- package/dist/merchant/operations-report.js.map +1 -0
- package/dist/merchant/stats-store.d.ts +5 -0
- package/dist/merchant/stats-store.js +18 -0
- package/dist/merchant/stats-store.js.map +1 -1
- package/dist/merchant-gateway/catalog-publications.d.ts +28 -0
- package/dist/merchant-gateway/catalog-publications.js +69 -0
- package/dist/merchant-gateway/catalog-publications.js.map +1 -1
- package/dist/merchant-gateway/catalog-tools.js +20 -0
- package/dist/merchant-gateway/catalog-tools.js.map +1 -1
- package/dist/negotiation/lease/store.js +21 -2
- package/dist/negotiation/lease/store.js.map +1 -1
- package/dist/product-cli.d.ts +2 -2
- package/dist/product-cli.js +24 -9
- package/dist/product-cli.js.map +1 -1
- package/dist/product-init.d.ts +4 -1
- package/dist/product-init.js +39 -11
- package/dist/product-init.js.map +1 -1
- package/dist/product-publish.d.ts +8 -2
- package/dist/product-publish.js +222 -142
- package/dist/product-publish.js.map +1 -1
- package/dist/runtime/fake-model.js +9 -3
- package/dist/runtime/fake-model.js.map +1 -1
- package/package.json +10 -5
package/dist/cloud/bootstrap.js
CHANGED
|
@@ -30,11 +30,12 @@
|
|
|
30
30
|
*/
|
|
31
31
|
import { createServer } from "node:http";
|
|
32
32
|
import { randomBytes } from "node:crypto";
|
|
33
|
-
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
33
|
+
import { chmodSync, existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
34
34
|
import path from "node:path";
|
|
35
35
|
import { DatabaseSync } from "node:sqlite";
|
|
36
|
-
import { createA2aNodeCore, createA2aAuthVerifier } from "../a2a/node.js";
|
|
37
|
-
import { loadOrCreateA2aSigningIdentity, toJwsSigningIdentity } from "../a2a/signing-key.js";
|
|
36
|
+
import { a2aCardSigningIdentity, createA2aNodeCore, createA2aAuthVerifier } from "../a2a/node.js";
|
|
37
|
+
import { A2A_SIGNING_KEY_FILE, loadA2aSigningIdentityFromFile, loadOrCreateA2aSigningIdentity, toJwsSigningIdentity } from "../a2a/signing-key.js";
|
|
38
|
+
import { publicKeyThumbprint } from "../trust/binding/thumbprint.js";
|
|
38
39
|
import { loadProfile, ProfileError } from "../config/profile.js";
|
|
39
40
|
import { createMerchantManagementApiHandler } from "../http/merchant-management/api.js";
|
|
40
41
|
import { createMerchantFeedApiHandler } from "../http/merchant-feed-api.js";
|
|
@@ -52,6 +53,8 @@ import { WorkbenchEventProjectionStore } from "../http/merchant-management/event
|
|
|
52
53
|
import { MerchantFeedStore } from "../merchant/feed-store.js";
|
|
53
54
|
import { MerchantFollowStore } from "../merchant/follow-store.js";
|
|
54
55
|
import { MerchantEngagementStore } from "../merchant/engagement-store.js";
|
|
56
|
+
import { createNegotiationObserver } from "../merchant/negotiation-observer.js";
|
|
57
|
+
import { createOperationsReportBuilder } from "../merchant/operations-report.js";
|
|
55
58
|
import { recommendedRetentionPolicy, WorkbenchRetentionStore, } from "../privacy/workbench-retention.js";
|
|
56
59
|
import { createLocalBackupDeletionHandler } from "../privacy/local-backup-deletion.js";
|
|
57
60
|
import { createLedgerPayloadDeletionHandler } from "../privacy/ledger-payload-deletion.js";
|
|
@@ -67,6 +70,10 @@ import { createServiceControlExecutors } from "../merchant/service-control-execu
|
|
|
67
70
|
import { calculateWorkbenchQuote } from "../merchant/quote-calculator.js";
|
|
68
71
|
import { WORKBENCH_CURRENCY_TABLE_VERSION } from "../merchant/application/money.js";
|
|
69
72
|
import { OnboardingStore } from "./onboarding/store.js";
|
|
73
|
+
import { CatalogClient } from "./catalog-client.js";
|
|
74
|
+
import { createEnrollmentChallengeResponder, readEnrollmentStore } from "./binding/enrollment-challenge.js";
|
|
75
|
+
import { startEnrollmentHeartbeat } from "./binding/enrollment-heartbeat.js";
|
|
76
|
+
import { catalogPlatformEvidenceAdapter, reconcileBinding, } from "./onboarding/catalog-pipeline.js";
|
|
70
77
|
import { ManagementError, } from "../merchant/application/service.js";
|
|
71
78
|
import { assembleMerchantRuntime, MerchantAssemblyError, } from "../mcp/merchant-runtime-assembly.js";
|
|
72
79
|
import { createMerchantHttpHandler, DEFAULT_MERCHANT_MCP_PATH as MCP_PATH, } from "../mcp/merchant-server.js";
|
|
@@ -74,8 +81,11 @@ import { PRODUCT_VERSION } from "../product-cli.js";
|
|
|
74
81
|
import { CloudConfigError, assertNoDemoPriceFallback, describeCloudConfig, loadCloudConfig, } from "./config.js";
|
|
75
82
|
import { createChallengeResponder } from "./binding/runtime-challenge.js";
|
|
76
83
|
import { createCloudRouter } from "./http-router.js";
|
|
77
|
-
import { commitProductTable, createFileProductSource, loadProductTableSnapshot, ProductTableError, } from "./product-source.js";
|
|
84
|
+
import { bindProductTableToCatalog, commitProductTable, createFileProductSource, ensureInitialEmptyProductTable, loadProductTableSnapshot, ProductTableError, } from "./product-source.js";
|
|
78
85
|
import { runReadiness } from "./readiness.js";
|
|
86
|
+
import { loadOrCreateMerchantIdentity } from "./merchant-identity.js";
|
|
87
|
+
import { createMerchantConnectionService } from "./connect-service.js";
|
|
88
|
+
import { createFileListingPublicationService } from "./file-listing-publication.js";
|
|
79
89
|
/** 云端 A2A 端点路径(设计 §8.2;与自托管根路径不同,便于同端口分发)。 */
|
|
80
90
|
export const CLOUD_A2A_PATH = "/a2a";
|
|
81
91
|
/** 启动失败:code 稳定可判(配置错误/装配错误/端口占用等)。 */
|
|
@@ -101,6 +111,54 @@ function assertDataDirNotClobbered(dataDir) {
|
|
|
101
111
|
}
|
|
102
112
|
}
|
|
103
113
|
}
|
|
114
|
+
/** 首次部署时把随部署目录下发的一次性管理员凭据导入权威状态目录。 */
|
|
115
|
+
function importAdminBootstrap(artifactRoot, dataDir, expectedIdentity, log) {
|
|
116
|
+
const source = path.join(artifactRoot, "admin-bootstrap.json");
|
|
117
|
+
if (!existsSync(source))
|
|
118
|
+
return;
|
|
119
|
+
const target = path.join(dataDir, "admin-credentials.json");
|
|
120
|
+
if (existsSync(target)) {
|
|
121
|
+
log("[kiwi-cloud] 已忽略管理员引导文件:状态目录已有凭据\n");
|
|
122
|
+
rmSync(source, { force: true });
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
let parsed;
|
|
126
|
+
try {
|
|
127
|
+
parsed = JSON.parse(readFileSync(source, "utf8"));
|
|
128
|
+
}
|
|
129
|
+
catch {
|
|
130
|
+
throw new CloudStartupError("ADMIN_BOOTSTRAP_INVALID", "管理员引导文件不是合法 JSON");
|
|
131
|
+
}
|
|
132
|
+
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed) ||
|
|
133
|
+
typeof parsed.principal_id !== "string" ||
|
|
134
|
+
typeof parsed.merchant_id !== "string" ||
|
|
135
|
+
typeof parsed.password_hash !== "string" ||
|
|
136
|
+
!/^scrypt\$16384\$[A-Za-z0-9_-]+\$[A-Za-z0-9_-]+$/.test(String(parsed.password_hash)) ||
|
|
137
|
+
typeof parsed.created_at !== "string") {
|
|
138
|
+
throw new CloudStartupError("ADMIN_BOOTSTRAP_INVALID", "管理员引导文件格式无效");
|
|
139
|
+
}
|
|
140
|
+
if (parsed.principal_id !== expectedIdentity.agent_id ||
|
|
141
|
+
parsed.merchant_id !== expectedIdentity.owner_id) {
|
|
142
|
+
log("[kiwi-cloud] 已忽略管理员引导文件:身份与状态目录不一致\n");
|
|
143
|
+
rmSync(source, { force: true });
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
mkdirSync(dataDir, { recursive: true, mode: 0o700 });
|
|
147
|
+
try {
|
|
148
|
+
writeFileSync(target, `${JSON.stringify(parsed, null, 2)}\n`, { mode: 0o600, flag: "wx" });
|
|
149
|
+
chmodSync(target, 0o600);
|
|
150
|
+
}
|
|
151
|
+
catch (error) {
|
|
152
|
+
if (error.code === "EEXIST") {
|
|
153
|
+
log("[kiwi-cloud] 已忽略管理员引导文件:状态目录已有凭据\n");
|
|
154
|
+
rmSync(source, { force: true });
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
throw new CloudStartupError("ADMIN_BOOTSTRAP_IMPORT_FAILED", "无法导入管理员引导凭据");
|
|
158
|
+
}
|
|
159
|
+
rmSync(source, { force: true });
|
|
160
|
+
log("[kiwi-cloud] 已导入一次性管理员引导凭据\n");
|
|
161
|
+
}
|
|
104
162
|
function loadOrCreateFeedCursorKey(dataDir) {
|
|
105
163
|
const file = path.join(dataDir, "feed-cursor.key");
|
|
106
164
|
const read = () => {
|
|
@@ -184,6 +242,13 @@ export async function bootstrapCloudRuntime(options = {}) {
|
|
|
184
242
|
// 生产禁演示价回退(设计 §10.2):演示价会让"商品源失联"看起来像有报价。
|
|
185
243
|
assertNoDemoPriceFallback(profile);
|
|
186
244
|
assertDataDirNotClobbered(config.dataDir);
|
|
245
|
+
profile = loadOrCreateMerchantIdentity(config.dataDir, profile, {
|
|
246
|
+
merchantNameNeedsUpdate: config.merchantNameNeedsUpdate,
|
|
247
|
+
});
|
|
248
|
+
if (config.productsFile !== undefined && !config.sample) {
|
|
249
|
+
ensureInitialEmptyProductTable(config.productsFile, profile.owner_id);
|
|
250
|
+
}
|
|
251
|
+
importAdminBootstrap(config.artifactRoot, config.dataDir, profile, log);
|
|
187
252
|
const serviceState = MutableServiceState.fromDeclared(options.env?.KIWI_CLOUD_SERVICE_STATE);
|
|
188
253
|
// 2) 商家面装配(审批/策略/商品/RFQ/工具面);认证走 OAuth(issuer = 公网 origin)。
|
|
189
254
|
let assembly;
|
|
@@ -286,6 +351,7 @@ export async function bootstrapCloudRuntime(options = {}) {
|
|
|
286
351
|
// 公钥随 Agent Card 公开(非秘密),私钥留在状态目录、不出进程。
|
|
287
352
|
const signingKeyDir = config.dataDir;
|
|
288
353
|
const signingIdentity = loadOrCreateA2aSigningIdentity(signingKeyDir, config.publicOrigin);
|
|
354
|
+
const a2aSigningIdentity = a2aCardSigningIdentity(config.a2aAuth.mode, signingIdentity);
|
|
289
355
|
const bearerToken = config.a2aAuth.mode === "bearer"
|
|
290
356
|
? (options.env ?? process.env)[config.a2aAuth.tokenEnv]
|
|
291
357
|
: undefined;
|
|
@@ -320,7 +386,7 @@ export async function bootstrapCloudRuntime(options = {}) {
|
|
|
320
386
|
a2aPath: CLOUD_A2A_PATH,
|
|
321
387
|
dataDir: config.dataDir,
|
|
322
388
|
authVerifier,
|
|
323
|
-
...(
|
|
389
|
+
...(a2aSigningIdentity !== undefined ? { signingIdentity: a2aSigningIdentity } : {}),
|
|
324
390
|
...(fileProductSource !== undefined ? { productSource: fileProductSource.source } : {}),
|
|
325
391
|
...(serviceAvailability !== undefined ? { serviceAvailability } : {}),
|
|
326
392
|
promotionPrice: ({ sku, quantity }) => {
|
|
@@ -336,6 +402,11 @@ export async function bootstrapCloudRuntime(options = {}) {
|
|
|
336
402
|
};
|
|
337
403
|
},
|
|
338
404
|
});
|
|
405
|
+
const persistedIdentity = loadA2aSigningIdentityFromFile(path.join(signingKeyDir, A2A_SIGNING_KEY_FILE));
|
|
406
|
+
if (publicKeyThumbprint(persistedIdentity.publicKeyPem) !== publicKeyThumbprint(signingIdentity.publicKeyPem)) {
|
|
407
|
+
core.close();
|
|
408
|
+
throw new CloudStartupError("SIGNING_KEY_CHANGED_DURING_STARTUP", "Runtime key rotated during startup; retry after the rotation completes.");
|
|
409
|
+
}
|
|
339
410
|
// 4) 就绪检查(无敏感值;stale 商品/未配置探针 SKU 都算未就绪,不冒充可用)。
|
|
340
411
|
const probeSku = config.readinessSku;
|
|
341
412
|
const readiness = async () => await runReadiness({
|
|
@@ -354,17 +425,25 @@ export async function bootstrapCloudRuntime(options = {}) {
|
|
|
354
425
|
products: async () => {
|
|
355
426
|
if (core.productSource === undefined)
|
|
356
427
|
return { ok: false, code: "PRODUCTS_SOURCE_ABSENT" };
|
|
428
|
+
if (fileProductSource !== undefined) {
|
|
429
|
+
try {
|
|
430
|
+
const products = fileProductSource.list();
|
|
431
|
+
if (products.length === 0)
|
|
432
|
+
return { ok: false, code: "PRODUCTS_NOT_CONFIGURED" };
|
|
433
|
+
const sku = probeSku ?? products[0]?.sku;
|
|
434
|
+
if (sku === undefined)
|
|
435
|
+
return { ok: false, code: "PRODUCTS_NOT_CONFIGURED" };
|
|
436
|
+
const check = fileProductSource.describeSku(sku);
|
|
437
|
+
return check.available ? { ok: true } : { ok: false, code: check.code ?? "PRODUCTS_UNAVAILABLE" };
|
|
438
|
+
}
|
|
439
|
+
catch (error) {
|
|
440
|
+
return { ok: false, code: error instanceof ProductTableError ? error.code : "PRODUCTS_UNAVAILABLE" };
|
|
441
|
+
}
|
|
442
|
+
}
|
|
357
443
|
if (probeSku === undefined || probeSku === "") {
|
|
358
444
|
// 未配置探针 SKU:M1 必须用授权真实测试商品,不能凭"进程活着"判就绪。
|
|
359
445
|
return { ok: false, code: "PRODUCTS_PROBE_SKU_UNSET" };
|
|
360
446
|
}
|
|
361
|
-
if (fileProductSource !== undefined) {
|
|
362
|
-
// 文件式商品源:只回可用性原因码(不含价格)。
|
|
363
|
-
const check = fileProductSource.describeSku(probeSku);
|
|
364
|
-
return check.available
|
|
365
|
-
? { ok: true }
|
|
366
|
-
: { ok: false, code: check.code ?? "PRODUCTS_UNAVAILABLE" };
|
|
367
|
-
}
|
|
368
447
|
try {
|
|
369
448
|
const product = await core.productSource.getProduct(probeSku);
|
|
370
449
|
return product !== undefined ? { ok: true } : { ok: false, code: "PRODUCTS_NOT_FOUND" };
|
|
@@ -394,9 +473,148 @@ export async function bootstrapCloudRuntime(options = {}) {
|
|
|
394
473
|
let buyerHandler;
|
|
395
474
|
let reconciliationTimer;
|
|
396
475
|
let clockTimer;
|
|
476
|
+
let bindingReconcileTimer;
|
|
477
|
+
let directConnectionTimer;
|
|
478
|
+
let directConnectionWork;
|
|
479
|
+
let bindingReconcileWork;
|
|
480
|
+
let directConnection;
|
|
481
|
+
let enrollmentHeartbeat;
|
|
482
|
+
let catalogMerchantIdForProducts = () => undefined;
|
|
397
483
|
if (adminOptions !== undefined) {
|
|
398
484
|
managementDb = new DatabaseSync(path.join(config.dataDir, "state.sqlite"));
|
|
399
485
|
serviceState.attachPersistence(managementDb, profile.owner_id);
|
|
486
|
+
// 开通向导通道(M4 §5.4 + P3 §4.6):store 与 /admin 同库;配了 catalog_url 时
|
|
487
|
+
// 接上 Catalog 串联(绑定/发布的权威证据适配器 + DEPLOYED_UNBOUND 后台对账)。
|
|
488
|
+
const onboardingStore = new OnboardingStore(managementDb);
|
|
489
|
+
// 已部署实例自行连接目录,无需伪造平台授权或改写旧部署向导的状态。
|
|
490
|
+
// 活跃旧向导仍由原通道处理,避免两条编排同时改同一份 enrollment。
|
|
491
|
+
if (config.catalogUrl !== undefined && onboardingStore.activeRecord(profile.owner_id) === undefined) {
|
|
492
|
+
directConnection = createMerchantConnectionService({
|
|
493
|
+
dataDir: config.dataDir, catalogUrl: config.catalogUrl, publicOrigin: config.publicOrigin,
|
|
494
|
+
...(options.catalogFetchImpl !== undefined ? { fetchImpl: options.catalogFetchImpl } : {}),
|
|
495
|
+
beforePublish: async (binding) => {
|
|
496
|
+
if (productsFilePath !== undefined && !config.sample) {
|
|
497
|
+
bindProductTableToCatalog(productsFilePath, profile.owner_id, binding.merchantId);
|
|
498
|
+
}
|
|
499
|
+
if (!(await readiness()).ready)
|
|
500
|
+
throw new Error("CATALOG_RUNTIME_NOT_READY");
|
|
501
|
+
},
|
|
502
|
+
});
|
|
503
|
+
const connection = directConnection;
|
|
504
|
+
let directRunning = false;
|
|
505
|
+
const tickConnection = () => {
|
|
506
|
+
if (directRunning)
|
|
507
|
+
return;
|
|
508
|
+
directRunning = true;
|
|
509
|
+
directConnectionWork = connection.reconcile().then(() => undefined)
|
|
510
|
+
.catch(() => log("[kiwi-cloud] 目录连接暂未完成,请在工作台查看状态。\n"))
|
|
511
|
+
.finally(() => { directRunning = false; });
|
|
512
|
+
};
|
|
513
|
+
directConnectionTimer = setInterval(tickConnection, 5_000);
|
|
514
|
+
directConnectionTimer.unref();
|
|
515
|
+
tickConnection();
|
|
516
|
+
}
|
|
517
|
+
catalogMerchantIdForProducts = () => {
|
|
518
|
+
const active = onboardingStore.activeRecord(profile.owner_id);
|
|
519
|
+
if (active === undefined)
|
|
520
|
+
return undefined;
|
|
521
|
+
const state = readEnrollmentStore(config.dataDir).sessions
|
|
522
|
+
.map((session) => session)
|
|
523
|
+
.find((session) => session.owner_ref === active.recordId &&
|
|
524
|
+
["bound", "published"].includes(session.status));
|
|
525
|
+
return typeof state?.merchant_id === "string" && state.merchant_id.trim() !== ""
|
|
526
|
+
? state.merchant_id
|
|
527
|
+
: undefined;
|
|
528
|
+
};
|
|
529
|
+
let onboardingChannel = { store: onboardingStore, enrollmentStateDir: config.dataDir };
|
|
530
|
+
if (config.catalogUrl !== undefined) {
|
|
531
|
+
const catalogPipeline = {
|
|
532
|
+
store: onboardingStore,
|
|
533
|
+
dataDir: config.dataDir,
|
|
534
|
+
client: new CatalogClient({ baseUrl: config.catalogUrl }),
|
|
535
|
+
identity: {
|
|
536
|
+
signingIdentity: toJwsSigningIdentity(signingIdentity),
|
|
537
|
+
keyId: signingIdentity?.keyid ?? config.publicOrigin,
|
|
538
|
+
},
|
|
539
|
+
// runtime_origin 只取自 KIWI_CLOUD_PUBLIC_ORIGIN(loadCloudConfig 已强校验)。
|
|
540
|
+
runtimeOrigin: config.publicOrigin,
|
|
541
|
+
// 单代次实例(与 M2 挑战应答的 currentGeneration 同值);代次切换属 BD-05。
|
|
542
|
+
generation: 1,
|
|
543
|
+
serviceEpoch: 1,
|
|
544
|
+
// 名片与 A2A server 自发的 well-known 卡同源(name/securityScheme 对齐 node.ts)。
|
|
545
|
+
card: {
|
|
546
|
+
name: profile.name ?? "Kiwi 商家",
|
|
547
|
+
description: "采购方 Agent 可发现并向本店发送询价;本服务仅按商家已配置规则报价。",
|
|
548
|
+
providerOrganization: profile.name ?? "商家",
|
|
549
|
+
version: "1.0.0",
|
|
550
|
+
...(a2aSigningIdentity !== undefined
|
|
551
|
+
? {
|
|
552
|
+
securityScheme: {
|
|
553
|
+
name: "kiwi-signature",
|
|
554
|
+
type: "kiwi-http-message-signature",
|
|
555
|
+
keyid: a2aSigningIdentity.keyid,
|
|
556
|
+
publicKeyPem: a2aSigningIdentity.publicKeyPem,
|
|
557
|
+
algorithm: a2aSigningIdentity.algorithm,
|
|
558
|
+
},
|
|
559
|
+
}
|
|
560
|
+
: {}),
|
|
561
|
+
},
|
|
562
|
+
serviceCheck: async () => {
|
|
563
|
+
const catalogMerchantId = catalogMerchantIdForProducts();
|
|
564
|
+
if (catalogMerchantId !== undefined && productsFilePath !== undefined && !config.sample) {
|
|
565
|
+
bindProductTableToCatalog(productsFilePath, profile.owner_id, catalogMerchantId);
|
|
566
|
+
}
|
|
567
|
+
const report = await readiness();
|
|
568
|
+
const failed = Object.entries(report.checks)
|
|
569
|
+
.filter(([, result]) => !result.ok)
|
|
570
|
+
.map(([name]) => name);
|
|
571
|
+
return { ok: report.ready, detail: failed.join(", ") || "not ready" };
|
|
572
|
+
},
|
|
573
|
+
// 后台对账的单次等待窗口:短超时、多次 tick,不长时间占住事件循环。
|
|
574
|
+
awaitOptions: { timeoutMs: 10_000, pollIntervalMs: 2_000 },
|
|
575
|
+
};
|
|
576
|
+
onboardingChannel = {
|
|
577
|
+
store: onboardingStore,
|
|
578
|
+
enrollmentStateDir: config.dataDir,
|
|
579
|
+
platformEvidence: catalogPlatformEvidenceAdapter(catalogPipeline),
|
|
580
|
+
};
|
|
581
|
+
// Catalog device enrollment:授权页确认后,绑定、就绪检查和名片发布由后台
|
|
582
|
+
// 按持久状态续办;商家不需要再次批准同一份公开信息。
|
|
583
|
+
let reconcileRunning = false;
|
|
584
|
+
let awaitingHintLogged = false;
|
|
585
|
+
const bindingReconcileTick = () => {
|
|
586
|
+
if (reconcileRunning)
|
|
587
|
+
return;
|
|
588
|
+
const active = onboardingStore.activeRecord(profile.owner_id);
|
|
589
|
+
if (active === undefined || !["DEPLOYED_UNBOUND", "BOUND", "VERIFYING", "READY_TO_PUBLISH"].includes(active.status))
|
|
590
|
+
return;
|
|
591
|
+
reconcileRunning = true;
|
|
592
|
+
bindingReconcileWork = reconcileBinding(active, catalogPipeline)
|
|
593
|
+
.then((outcome) => {
|
|
594
|
+
if (outcome.kind === "awaiting_portal_confirmation") {
|
|
595
|
+
if (!awaitingHintLogged) {
|
|
596
|
+
log("[kiwi-cloud] 等待商家本人在工作台核对目录授权。\n");
|
|
597
|
+
awaitingHintLogged = true;
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
else {
|
|
601
|
+
awaitingHintLogged = false;
|
|
602
|
+
log(`[kiwi-cloud] 绑定已在门户确认(binding_id=${outcome.confirmed.bindingId}),` +
|
|
603
|
+
`开通记录推进到 ${outcome.record.status}\n`);
|
|
604
|
+
}
|
|
605
|
+
})
|
|
606
|
+
.catch(() => {
|
|
607
|
+
awaitingHintLogged = false;
|
|
608
|
+
log("[kiwi-cloud] 绑定对账暂未完成,请在工作台查看状态。\n");
|
|
609
|
+
})
|
|
610
|
+
.finally(() => {
|
|
611
|
+
reconcileRunning = false;
|
|
612
|
+
});
|
|
613
|
+
};
|
|
614
|
+
bindingReconcileTimer = setInterval(bindingReconcileTick, 30_000);
|
|
615
|
+
bindingReconcileTimer.unref();
|
|
616
|
+
bindingReconcileTick();
|
|
617
|
+
}
|
|
400
618
|
const workbenchConfirmations = new WorkbenchConfirmationStore({ db: managementDb });
|
|
401
619
|
const reconciliationStore = new WorkbenchReconciliationStore({ db: managementDb });
|
|
402
620
|
const clockSafety = new ClockSafetyStore({ db: managementDb, alerts: reconciliationStore });
|
|
@@ -537,6 +755,8 @@ export async function bootstrapCloudRuntime(options = {}) {
|
|
|
537
755
|
};
|
|
538
756
|
merchantApiHandler = createMerchantManagementApiHandler({
|
|
539
757
|
merchantId: profile.owner_id,
|
|
758
|
+
merchantName: profile.name ?? "待设置商家名称",
|
|
759
|
+
merchantNameNeedsUpdate: config.merchantNameNeedsUpdate && profile.name === "待设置商家名称",
|
|
540
760
|
// 单代次实例(与 M2 挑战应答的 currentGeneration 同值);代次切换属 BD-05。
|
|
541
761
|
generation: () => 1,
|
|
542
762
|
runtimeVersion: PRODUCT_VERSION,
|
|
@@ -559,6 +779,13 @@ export async function bootstrapCloudRuntime(options = {}) {
|
|
|
559
779
|
? undefined
|
|
560
780
|
: { version: current.version, digest: current.digest };
|
|
561
781
|
},
|
|
782
|
+
...(productsFilePath !== undefined && config.catalogUrl !== undefined && !config.sample
|
|
783
|
+
? { fileListingPublication: createFileListingPublicationService({
|
|
784
|
+
dataDir: config.dataDir, productsFile: productsFilePath, profile,
|
|
785
|
+
catalogUrl: config.catalogUrl, publicOrigin: config.publicOrigin,
|
|
786
|
+
...(options.catalogFetchImpl !== undefined ? { fetchImpl: options.catalogFetchImpl } : {}),
|
|
787
|
+
}) }
|
|
788
|
+
: {}),
|
|
562
789
|
// 商品投影/导入:仅在配置了商品表文件时可用;ProductTableError → 503
|
|
563
790
|
//(不让「表不可读」伪装成「无商品」)。价格即商品表的 major units(元),
|
|
564
791
|
// 与 MerchantProductSource 契约同单位,不经换算(金额单位红线)。
|
|
@@ -605,6 +832,28 @@ export async function bootstrapCloudRuntime(options = {}) {
|
|
|
605
832
|
const snapshot = loadProductTableSnapshot(productsFilePath, profile.owner_id);
|
|
606
833
|
return { digest: snapshot.digest, records: snapshot.records };
|
|
607
834
|
},
|
|
835
|
+
prepareTable: async (table) => {
|
|
836
|
+
let catalogMerchantId;
|
|
837
|
+
if (directConnection !== undefined) {
|
|
838
|
+
const verified = await directConnection.getVerifiedBinding();
|
|
839
|
+
const summary = await directConnection.getSummary();
|
|
840
|
+
if (verified === null && summary.bindingId !== null) {
|
|
841
|
+
throw new ProductTableError("CATALOG_BINDING_UNAVAILABLE", "目录绑定暂无法核验,商品表保持不变,请稍后重试。");
|
|
842
|
+
}
|
|
843
|
+
catalogMerchantId = verified?.merchantId;
|
|
844
|
+
}
|
|
845
|
+
else {
|
|
846
|
+
catalogMerchantId = catalogMerchantIdForProducts();
|
|
847
|
+
}
|
|
848
|
+
const { merchant_id: _untrustedMerchantId, ...unboundTable } = table;
|
|
849
|
+
return {
|
|
850
|
+
...unboundTable,
|
|
851
|
+
runtime_owner_id: profile.owner_id,
|
|
852
|
+
...(config.sample
|
|
853
|
+
? { merchant_id: profile.owner_id }
|
|
854
|
+
: catalogMerchantId !== undefined ? { merchant_id: catalogMerchantId } : {}),
|
|
855
|
+
};
|
|
856
|
+
},
|
|
608
857
|
commit: (table) => commitProductTable(productsFilePath, profile.owner_id, table),
|
|
609
858
|
},
|
|
610
859
|
}
|
|
@@ -623,10 +872,34 @@ export async function bootstrapCloudRuntime(options = {}) {
|
|
|
623
872
|
}),
|
|
624
873
|
operations: new MerchantManagementOperationStore({ db: managementDb }),
|
|
625
874
|
// M4 §5.4:开通向导与 /admin/onboarding 读**同一份** OnboardingStore(同一个
|
|
626
|
-
// state.sqlite),不复制状态机。`platformEvidence`
|
|
627
|
-
//
|
|
628
|
-
//
|
|
629
|
-
|
|
875
|
+
// state.sqlite),不复制状态机。`platformEvidence` 适配器分两种情况:
|
|
876
|
+
// - 配了 catalog_url(KIWI_CATALOG_URL):由 Catalog 串联(§4.6/P3)接管
|
|
877
|
+
// 「服务检查」与「确认公开信息」两步的权威证据(S3 绑定确认 + S4 发布激活),
|
|
878
|
+
// 其余步骤(平台授权/商品确认)维持未配置 → 503(平台回执能力未落地,
|
|
879
|
+
// 绝不用请求体自报的证据顶上,T029);
|
|
880
|
+
// - 未配 catalog_url:适配器整体不配置,需要权威证据的步骤明确 503。
|
|
881
|
+
onboarding: onboardingChannel,
|
|
882
|
+
...(directConnection !== undefined ? {
|
|
883
|
+
catalogConnection: {
|
|
884
|
+
getSummary: async () => {
|
|
885
|
+
const summary = await directConnection.getSummary();
|
|
886
|
+
return {
|
|
887
|
+
status: summary.status, published: summary.published,
|
|
888
|
+
...(summary.agentId !== null ? { agentId: summary.agentId } : {}),
|
|
889
|
+
...(summary.bindingId !== null ? { bindingId: summary.bindingId } : {}),
|
|
890
|
+
...(summary.bindingExpiresAt !== null ? { bindingExpiresAt: summary.bindingExpiresAt } : {}),
|
|
891
|
+
...(summary.code !== null ? { errorCode: summary.code } : {}),
|
|
892
|
+
};
|
|
893
|
+
},
|
|
894
|
+
getPairing: async () => {
|
|
895
|
+
const pairing = directConnection.getPairing();
|
|
896
|
+
return pairing === null ? null : {
|
|
897
|
+
user_code: pairing.userCode, verification_uri: pairing.verificationUri, expires_at: pairing.expiresAt,
|
|
898
|
+
};
|
|
899
|
+
},
|
|
900
|
+
begin: () => directConnection.begin(),
|
|
901
|
+
},
|
|
902
|
+
} : {}),
|
|
630
903
|
workbenchConfirmations,
|
|
631
904
|
workbenchReconciliation: reconciliationStore,
|
|
632
905
|
workbenchEvents: eventProjectionStore,
|
|
@@ -646,6 +919,12 @@ export async function bootstrapCloudRuntime(options = {}) {
|
|
|
646
919
|
},
|
|
647
920
|
}
|
|
648
921
|
: {}),
|
|
922
|
+
// WP11 会话旁观/运营报告:与 A2A 节点同一事实源(<dataDir>/a2a 账本 +
|
|
923
|
+
// stats.sqlite),只读投影,不新增写路径。
|
|
924
|
+
negotiationObserver: createNegotiationObserver({
|
|
925
|
+
ledgerDir: path.join(config.dataDir, "a2a"),
|
|
926
|
+
}),
|
|
927
|
+
operationsReports: createOperationsReportBuilder({ dataDir: config.dataDir }),
|
|
649
928
|
...(adminOptions.surface.listExactProducts !== undefined &&
|
|
650
929
|
adminOptions.surface.getExactProduct !== undefined
|
|
651
930
|
? {
|
|
@@ -820,8 +1099,19 @@ export async function bootstrapCloudRuntime(options = {}) {
|
|
|
820
1099
|
"content-type": "text/html; charset=utf-8",
|
|
821
1100
|
"cache-control": "no-store",
|
|
822
1101
|
});
|
|
823
|
-
res.end(renderMerchantManagementPage(
|
|
1102
|
+
res.end(renderMerchantManagementPage({
|
|
1103
|
+
productAuthority: fileProductSource !== undefined ? "file" : "exact",
|
|
1104
|
+
catalogConnection: directConnection !== undefined,
|
|
1105
|
+
}));
|
|
824
1106
|
};
|
|
1107
|
+
if (signingIdentity !== undefined) {
|
|
1108
|
+
enrollmentHeartbeat = startEnrollmentHeartbeat({
|
|
1109
|
+
dataDir: config.dataDir,
|
|
1110
|
+
signingIdentity: toJwsSigningIdentity(signingIdentity),
|
|
1111
|
+
isReady: async () => (await readiness()).ready,
|
|
1112
|
+
onError: (err) => log(`[kiwi-cloud] ${err.message}\n`),
|
|
1113
|
+
});
|
|
1114
|
+
}
|
|
825
1115
|
// 5) 单端口路由:A2A 面 / 商家面 / 管理面 / 探针,各自鉴权边界不变。
|
|
826
1116
|
const merchantHandler = createMerchantHttpHandler(assembly.serverOptions);
|
|
827
1117
|
const router = createCloudRouter({
|
|
@@ -837,11 +1127,15 @@ export async function bootstrapCloudRuntime(options = {}) {
|
|
|
837
1127
|
// 绑定挑战应答(M2 §6.3):只签发给本实例的受限结构挑战,一次性、有速率上限。
|
|
838
1128
|
challengeHandler: createChallengeResponder({
|
|
839
1129
|
signingIdentity: toJwsSigningIdentity(signingIdentity),
|
|
840
|
-
expectedMerchantId:
|
|
1130
|
+
expectedMerchantId: catalogMerchantIdForProducts,
|
|
841
1131
|
expectedAgentId: profile.agent_id,
|
|
842
1132
|
currentGeneration: 1,
|
|
843
1133
|
...(options.challengeStore !== undefined ? { store: options.challengeStore } : {}),
|
|
844
1134
|
}),
|
|
1135
|
+
enrollmentChallengeHandler: createEnrollmentChallengeResponder({
|
|
1136
|
+
dataDir: config.dataDir,
|
|
1137
|
+
signingIdentity: toJwsSigningIdentity(signingIdentity),
|
|
1138
|
+
}),
|
|
845
1139
|
version: PRODUCT_VERSION,
|
|
846
1140
|
});
|
|
847
1141
|
const server = createServer(router);
|
|
@@ -867,6 +1161,12 @@ export async function bootstrapCloudRuntime(options = {}) {
|
|
|
867
1161
|
clearInterval(reconciliationTimer);
|
|
868
1162
|
if (clockTimer !== undefined)
|
|
869
1163
|
clearInterval(clockTimer);
|
|
1164
|
+
if (bindingReconcileTimer !== undefined)
|
|
1165
|
+
clearInterval(bindingReconcileTimer);
|
|
1166
|
+
if (directConnectionTimer !== undefined)
|
|
1167
|
+
clearInterval(directConnectionTimer);
|
|
1168
|
+
enrollmentHeartbeat?.stop();
|
|
1169
|
+
await Promise.allSettled([directConnectionWork, bindingReconcileWork]);
|
|
870
1170
|
managementDb?.close();
|
|
871
1171
|
core.close();
|
|
872
1172
|
await assembly.close().catch(() => undefined);
|
|
@@ -891,6 +1191,10 @@ export async function bootstrapCloudRuntime(options = {}) {
|
|
|
891
1191
|
server,
|
|
892
1192
|
readiness,
|
|
893
1193
|
close: async () => {
|
|
1194
|
+
if (bindingReconcileTimer !== undefined)
|
|
1195
|
+
clearInterval(bindingReconcileTimer);
|
|
1196
|
+
if (directConnectionTimer !== undefined)
|
|
1197
|
+
clearInterval(directConnectionTimer);
|
|
894
1198
|
await new Promise((resolve) => {
|
|
895
1199
|
server.close(() => resolve());
|
|
896
1200
|
server.closeAllConnections();
|
|
@@ -900,6 +1204,8 @@ export async function bootstrapCloudRuntime(options = {}) {
|
|
|
900
1204
|
clearInterval(reconciliationTimer);
|
|
901
1205
|
if (clockTimer !== undefined)
|
|
902
1206
|
clearInterval(clockTimer);
|
|
1207
|
+
enrollmentHeartbeat?.stop();
|
|
1208
|
+
await Promise.allSettled([directConnectionWork, bindingReconcileWork]);
|
|
903
1209
|
core.close();
|
|
904
1210
|
await assembly.close().catch(() => undefined);
|
|
905
1211
|
managementDb?.close();
|