@harrylabsj/kiwi-merchant-cloud 0.12.15 → 0.12.18

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.
Files changed (76) hide show
  1. package/app/a2a/node.js +2 -0
  2. package/app/a2a/node.js.map +1 -1
  3. package/app/a2a/server/merchant-handler.js +31 -0
  4. package/app/a2a/server/merchant-handler.js.map +1 -1
  5. package/app/a2a/server/negotiation-approvals.d.ts +14 -1
  6. package/app/a2a/server/negotiation-approvals.js +32 -0
  7. package/app/a2a/server/negotiation-approvals.js.map +1 -1
  8. package/app/a2a/server/pipeline.d.ts +2 -0
  9. package/app/a2a/server/pipeline.js +3 -3
  10. package/app/a2a/server/pipeline.js.map +1 -1
  11. package/app/a2a/server/server.d.ts +3 -0
  12. package/app/a2a/server/server.js +17 -1
  13. package/app/a2a/server/server.js.map +1 -1
  14. package/app/a2a/server/types.d.ts +6 -0
  15. package/app/buyer-core/a2a-knp.d.ts +14 -0
  16. package/app/buyer-core/a2a-knp.js +25 -0
  17. package/app/buyer-core/a2a-knp.js.map +1 -1
  18. package/app/buyer-core/a2a-negotiator.d.ts +15 -0
  19. package/app/buyer-core/a2a-negotiator.js +173 -2
  20. package/app/buyer-core/a2a-negotiator.js.map +1 -1
  21. package/app/buyer-core/service.d.ts +41 -0
  22. package/app/buyer-core/service.js +110 -10
  23. package/app/buyer-core/service.js.map +1 -1
  24. package/app/buyer-core/store.d.ts +16 -0
  25. package/app/buyer-core/store.js +32 -0
  26. package/app/buyer-core/store.js.map +1 -1
  27. package/app/cli.js +203 -0
  28. package/app/cli.js.map +1 -1
  29. package/app/cloud-sample/products.json +3 -3
  30. package/app/http/server.js +5 -0
  31. package/app/http/server.js.map +1 -1
  32. package/app/mcp/merchant-ops-server.d.ts +38 -0
  33. package/app/mcp/merchant-ops-server.js +283 -0
  34. package/app/mcp/merchant-ops-server.js.map +1 -0
  35. package/app/mcp/merchant-runtime-assembly.js +28 -0
  36. package/app/mcp/merchant-runtime-assembly.js.map +1 -1
  37. package/app/mcp/merchant-server.d.ts +16 -0
  38. package/app/mcp/merchant-server.js +24 -0
  39. package/app/mcp/merchant-server.js.map +1 -1
  40. package/app/mcp/tools.js +4 -3
  41. package/app/mcp/tools.js.map +1 -1
  42. package/app/merchant/negotiation-observer.d.ts +35 -0
  43. package/app/merchant/negotiation-observer.js +168 -57
  44. package/app/merchant/negotiation-observer.js.map +1 -1
  45. package/app/merchant/negotiation-ops.d.ts +77 -0
  46. package/app/merchant/negotiation-ops.js +203 -0
  47. package/app/merchant/negotiation-ops.js.map +1 -0
  48. package/app/merchant/watch/detect.d.ts +37 -0
  49. package/app/merchant/watch/detect.js +147 -0
  50. package/app/merchant/watch/detect.js.map +1 -0
  51. package/app/merchant/watch/notify.d.ts +48 -0
  52. package/app/merchant/watch/notify.js +108 -0
  53. package/app/merchant/watch/notify.js.map +1 -0
  54. package/app/merchant/watch/service.d.ts +43 -0
  55. package/app/merchant/watch/service.js +167 -0
  56. package/app/merchant/watch/service.js.map +1 -0
  57. package/app/merchant/watch/state.d.ts +49 -0
  58. package/app/merchant/watch/state.js +68 -0
  59. package/app/merchant/watch/state.js.map +1 -0
  60. package/app/merchant/watch/weixin.d.ts +54 -0
  61. package/app/merchant/watch/weixin.js +271 -0
  62. package/app/merchant/watch/weixin.js.map +1 -0
  63. package/app/merchant-runtime/expire-offers.d.ts +30 -0
  64. package/app/merchant-runtime/expire-offers.js +112 -0
  65. package/app/merchant-runtime/expire-offers.js.map +1 -0
  66. package/app/merchant-runtime/jobs.d.ts +3 -0
  67. package/app/merchant-runtime/jobs.js +9 -0
  68. package/app/merchant-runtime/jobs.js.map +1 -1
  69. package/app/merchant-runtime/services.d.ts +0 -8
  70. package/app/merchant-runtime/services.js +38 -0
  71. package/app/merchant-runtime/services.js.map +1 -1
  72. package/build-manifest.json +224 -104
  73. package/package.json +1 -1
  74. package/runtime-package.json +1 -1
  75. package/shipping-sbom.cdx.json +6 -6
  76. package/shipping-source.json +12 -12
package/app/cli.js CHANGED
@@ -34,6 +34,11 @@ import { chmodSync, copyFileSync, existsSync, mkdirSync, readFileSync, realpathS
34
34
  import { homedir, userInfo } from "node:os";
35
35
  import path from "node:path";
36
36
  import { NegotiationApprovalStore } from "./a2a/server/negotiation-approvals.js";
37
+ import { createNegotiationOps } from "./merchant/negotiation-ops.js";
38
+ import { MerchantWatchService } from "./merchant/watch/service.js";
39
+ import { WecomWebhookChannel } from "./merchant/watch/notify.js";
40
+ import { WeixinOpsBot } from "./merchant/watch/weixin.js";
41
+ import { loadWatchState, watchStatePath } from "./merchant/watch/state.js";
37
42
  import { fileURLToPath } from "node:url";
38
43
  import { Writable } from "node:stream";
39
44
  import { startA2aNode } from "./a2a/node.js";
@@ -69,6 +74,7 @@ import { buildRuntimeServiceSpecs } from "./merchant-runtime/services.js";
69
74
  import { collectMerchantHealth } from "./merchant-runtime/health.js";
70
75
  import { MerchantJobs, standardMerchantJobs } from "./merchant-runtime/jobs.js";
71
76
  import { runBackup } from "./merchant-runtime/backup.js";
77
+ import { expireOverdueOffers } from "./merchant-runtime/expire-offers.js";
72
78
  import { connectMerchant } from "./cloud/merchant-connect.js";
73
79
  import { rotateEnrollmentSigningKey } from "./cloud/binding/enrollment-key-recovery.js";
74
80
  import { credentialFingerprint, rotateMerchantCredential } from "./merchant-credential-rotation.js";
@@ -1412,6 +1418,8 @@ async function routeMerchant(sub, args) {
1412
1418
  return await cmdMerchantDoctor(args);
1413
1419
  if (sub === "approvals")
1414
1420
  return await cmdMerchantApprovals(args);
1421
+ if (sub === "watch")
1422
+ return await cmdMerchantWatch(args);
1415
1423
  process.stderr.write(`unknown merchant command: ${sub}\n`);
1416
1424
  return EXIT.CONFIG;
1417
1425
  }
@@ -1458,6 +1466,21 @@ async function cmdMerchantApprovals(args) {
1458
1466
  printJson({ ok: true, approval: record });
1459
1467
  return EXIT.OK;
1460
1468
  }
1469
+ if (action === "reject") {
1470
+ const approvalId = args.command[3];
1471
+ if (approvalId === undefined) {
1472
+ process.stderr.write("用法:kiwi merchant approvals reject <approval_id> [reason]\n");
1473
+ return EXIT.CONFIG;
1474
+ }
1475
+ const reason = args.command.slice(4).join(" ").trim();
1476
+ const record = store.reject({
1477
+ approval_id: approvalId,
1478
+ ...(reason !== "" ? { reason } : {}),
1479
+ rejected_by: profile.agent_id,
1480
+ });
1481
+ printJson({ ok: true, approval: record });
1482
+ return EXIT.OK;
1483
+ }
1461
1484
  process.stderr.write(`unknown approvals command: ${action}\n`);
1462
1485
  return EXIT.CONFIG;
1463
1486
  }
@@ -1466,6 +1489,177 @@ async function cmdMerchantApprovals(args) {
1466
1489
  return EXIT.CONFIG;
1467
1490
  }
1468
1491
  }
1492
+ /**
1493
+ * `kiwi merchant watch`:磋商/审批通知守护(企业微信群机器人 + 可选个人微信)。
1494
+ *
1495
+ * watch serve 前台守护(runtime 受管子进程用)
1496
+ * watch login 扫码绑定独立 ops bot 凭证
1497
+ * watch relogin 重新扫码
1498
+ * watch test 向已配置渠道发一条测试消息(不含业务数据)
1499
+ * watch status 渠道与去重状态摘要
1500
+ */
1501
+ async function cmdMerchantWatch(args) {
1502
+ const profile = requireProfileOrDefault(args);
1503
+ if (profile.role !== "merchant") {
1504
+ process.stderr.write("kiwi merchant watch 需要 merchant profile(role: merchant)\n");
1505
+ return EXIT.CONFIG;
1506
+ }
1507
+ const action = args.command[2] ?? "status";
1508
+ const dataDir = resolveServeDataDir(args.dataDir, profile.agent_id, profile.merchant_runtime?.data_dir);
1509
+ const approvals = NegotiationApprovalStore.forDataDir(dataDir, () => new Date().toISOString());
1510
+ const ops = createNegotiationOps({
1511
+ ledgerDir: path.join(dataDir, "a2a"),
1512
+ approvals,
1513
+ merchantId: profile.owner_id,
1514
+ version: PRODUCT_VERSION,
1515
+ });
1516
+ const webhook = (process.env.KIWI_MERCHANT_WATCH_WECOM_WEBHOOK ?? "").trim();
1517
+ const weixinEnabled = process.env.KIWI_MERCHANT_WATCH_WEIXIN === "1";
1518
+ const allowUsers = (process.env.KIWI_MERCHANT_WATCH_WEIXIN_ALLOW ?? "")
1519
+ .split(",")
1520
+ .map((value) => value.trim())
1521
+ .filter((value) => value !== "");
1522
+ const allowed = [...new Set([...(profile.weixin?.allow_users ?? []), ...allowUsers])];
1523
+ const openBot = async (relogin) => WeixinOpsBot.open({
1524
+ dataDir,
1525
+ allowUsers: allowed,
1526
+ forceRelogin: relogin,
1527
+ ...(process.env.KIWI_MERCHANT_WATCH_WEIXIN_BASE_URL !== undefined
1528
+ ? { apiBaseUrl: process.env.KIWI_MERCHANT_WATCH_WEIXIN_BASE_URL }
1529
+ : {}),
1530
+ });
1531
+ if (action === "login" || action === "relogin") {
1532
+ try {
1533
+ const bot = await openBot(action === "relogin");
1534
+ process.stdout.write(`微信 ops bot 已绑定:${bot.credentialsRef().ilink_user_id}\n`);
1535
+ bot.stop();
1536
+ return EXIT.OK;
1537
+ }
1538
+ catch (err) {
1539
+ process.stderr.write(`微信扫码失败:${err instanceof Error ? err.message : String(err)}\n`);
1540
+ return EXIT.AUTH;
1541
+ }
1542
+ }
1543
+ const channels = [];
1544
+ if (webhook !== "")
1545
+ channels.push(new WecomWebhookChannel(webhook));
1546
+ if (action === "status") {
1547
+ let bot = "disabled";
1548
+ if (weixinEnabled) {
1549
+ try {
1550
+ bot = existsSync(path.join(dataDir, "weixin-ops-credentials.json"))
1551
+ ? `enabled (${allowed.length + 1} recipient(s))`
1552
+ : "enabled 但未扫码";
1553
+ }
1554
+ catch {
1555
+ bot = "unknown";
1556
+ }
1557
+ }
1558
+ let state;
1559
+ try {
1560
+ const loaded = loadWatchState(watchStatePath(dataDir), new Date().toISOString());
1561
+ state = `${Object.keys(loaded.negotiations).length} negotiation(s), ${Object.keys(loaded.approvals).length} approval(s), queue ${loaded.queue.length}`;
1562
+ }
1563
+ catch (err) {
1564
+ state = err instanceof Error ? err.message : String(err);
1565
+ }
1566
+ process.stdout.write(`${JSON.stringify({
1567
+ ok: true,
1568
+ data_dir: dataDir,
1569
+ channels: { wecom: webhook !== "" ? "configured" : "off", weixin: bot },
1570
+ state,
1571
+ }, null, 2)}\n`);
1572
+ return EXIT.OK;
1573
+ }
1574
+ if (action === "test") {
1575
+ if (channels.length === 0 && !weixinEnabled) {
1576
+ process.stderr.write("没有配置通知渠道(KIWI_MERCHANT_WATCH_WECOM_WEBHOOK / KIWI_MERCHANT_WATCH_WEIXIN=1)\n");
1577
+ return EXIT.CONFIG;
1578
+ }
1579
+ try {
1580
+ let sent = 0;
1581
+ for (const channel of channels) {
1582
+ await channel.send("Kiwi 商家通知测试:通道已接通。");
1583
+ sent += 1;
1584
+ }
1585
+ if (weixinEnabled) {
1586
+ const bot = await openBot(false);
1587
+ await bot.send("Kiwi 商家通知测试:个人微信通道已接通。");
1588
+ bot.stop();
1589
+ sent += 1;
1590
+ }
1591
+ process.stdout.write(`测试消息已发送(${sent} 个渠道)\n`);
1592
+ return EXIT.OK;
1593
+ }
1594
+ catch (err) {
1595
+ process.stderr.write(`测试消息发送失败:${err instanceof Error ? err.message : String(err)}\n`);
1596
+ return EXIT.TRANSIENT;
1597
+ }
1598
+ }
1599
+ if (action !== "serve") {
1600
+ process.stderr.write(`unknown watch command: ${action}\n`);
1601
+ return EXIT.CONFIG;
1602
+ }
1603
+ if (channels.length === 0 && !weixinEnabled) {
1604
+ process.stderr.write("没有配置通知渠道,拒绝启动 watch\n");
1605
+ return EXIT.CONFIG;
1606
+ }
1607
+ let bot;
1608
+ if (weixinEnabled) {
1609
+ if (!existsSync(path.join(dataDir, "weixin-ops-credentials.json"))) {
1610
+ process.stderr.write("个人微信已启用但未扫码:先运行 kiwi merchant watch login\n");
1611
+ return EXIT.CONFIG;
1612
+ }
1613
+ try {
1614
+ bot = await openBot(false);
1615
+ channels.push(bot.channel());
1616
+ }
1617
+ catch (err) {
1618
+ process.stderr.write(`个人微信通道启动失败:${err instanceof Error ? err.message : String(err)}\n`);
1619
+ return EXIT.AUTH;
1620
+ }
1621
+ }
1622
+ const service = new MerchantWatchService({
1623
+ dataDir,
1624
+ ops,
1625
+ channels,
1626
+ intervalMs: Math.max(5_000, Number(process.env.KIWI_MERCHANT_WATCH_INTERVAL_SECONDS ?? "10") * 1_000),
1627
+ });
1628
+ let stopping = false;
1629
+ const shutdown = () => {
1630
+ if (stopping)
1631
+ return;
1632
+ stopping = true;
1633
+ service.stop();
1634
+ bot?.stop();
1635
+ };
1636
+ process.on("SIGINT", shutdown);
1637
+ process.on("SIGTERM", shutdown);
1638
+ try {
1639
+ service.start();
1640
+ if (bot !== undefined) {
1641
+ void bot.run(ops).then((ok) => {
1642
+ if (!ok)
1643
+ process.stderr.write("[watch] 个人微信通道已停止;企业微信通道继续\n");
1644
+ });
1645
+ }
1646
+ process.stderr.write(`[watch] started: ${channels.map((channel) => channel.name).join(",")}\n`);
1647
+ await new Promise((resolve) => {
1648
+ const timer = setInterval(() => {
1649
+ if (stopping) {
1650
+ clearInterval(timer);
1651
+ resolve();
1652
+ }
1653
+ }, 500);
1654
+ });
1655
+ return EXIT.OK;
1656
+ }
1657
+ finally {
1658
+ process.removeListener("SIGINT", shutdown);
1659
+ process.removeListener("SIGTERM", shutdown);
1660
+ bot?.stop();
1661
+ }
1662
+ }
1469
1663
  async function cmdMerchantDoctor(args) {
1470
1664
  try {
1471
1665
  const profile = requireProfileOrDefault(args);
@@ -1754,8 +1948,15 @@ async function cmdMerchantRuntime(args) {
1754
1948
  backupsDir: path.join(dirs.merchantDataDir, "backups"),
1755
1949
  });
1756
1950
  },
1951
+ expireOffers: async () => {
1952
+ const result = await expireOverdueOffers({ dataDir: dirs.merchantDataDir });
1953
+ if (result.expired > 0) {
1954
+ process.stderr.write(`[kiwi] expired ${result.expired} overdue offer(s)\n`);
1955
+ }
1956
+ },
1757
1957
  healthIntervalMs: 60_000,
1758
1958
  backupIntervalMs: 5 * 60_000,
1959
+ offerExpiryIntervalMs: 5 * 60_000,
1759
1960
  })) {
1760
1961
  jobs.register(job);
1761
1962
  }
@@ -1802,6 +2003,8 @@ async function cmdMerchantRuntime(args) {
1802
2003
  });
1803
2004
  }
1804
2005
  jobs.start();
2006
+ // 启动即扫一次过期报价(不等首个 5 分钟间隔)。
2007
+ void jobs.runOnce("offer-expiry");
1805
2008
  if (rotationScheduleStatus.enabled) {
1806
2009
  firstRotationCheck = setTimeout(() => { void jobs.runOnce("merchant-credential-rotation"); }, 120_000);
1807
2010
  firstRotationCheck.unref();