@harrylabsj/kiwi 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.
- package/dist/a2a/node.js +2 -0
- package/dist/a2a/node.js.map +1 -1
- package/dist/a2a/server/merchant-handler.js +31 -0
- package/dist/a2a/server/merchant-handler.js.map +1 -1
- package/dist/a2a/server/negotiation-approvals.d.ts +14 -1
- package/dist/a2a/server/negotiation-approvals.js +32 -0
- package/dist/a2a/server/negotiation-approvals.js.map +1 -1
- package/dist/a2a/server/pipeline.d.ts +2 -0
- package/dist/a2a/server/pipeline.js +3 -3
- package/dist/a2a/server/pipeline.js.map +1 -1
- package/dist/a2a/server/server.d.ts +3 -0
- package/dist/a2a/server/server.js +17 -1
- package/dist/a2a/server/server.js.map +1 -1
- package/dist/a2a/server/types.d.ts +6 -0
- package/dist/buyer-core/a2a-knp.d.ts +14 -0
- package/dist/buyer-core/a2a-knp.js +25 -0
- package/dist/buyer-core/a2a-knp.js.map +1 -1
- package/dist/buyer-core/a2a-negotiator.d.ts +15 -0
- package/dist/buyer-core/a2a-negotiator.js +173 -2
- package/dist/buyer-core/a2a-negotiator.js.map +1 -1
- package/dist/buyer-core/service.d.ts +41 -0
- package/dist/buyer-core/service.js +110 -10
- package/dist/buyer-core/service.js.map +1 -1
- package/dist/buyer-core/store.d.ts +16 -0
- package/dist/buyer-core/store.js +32 -0
- package/dist/buyer-core/store.js.map +1 -1
- package/dist/cli.js +203 -0
- package/dist/cli.js.map +1 -1
- package/dist/http/server.js +5 -0
- package/dist/http/server.js.map +1 -1
- package/dist/mcp/merchant-ops-server.d.ts +38 -0
- package/dist/mcp/merchant-ops-server.js +283 -0
- package/dist/mcp/merchant-ops-server.js.map +1 -0
- package/dist/mcp/merchant-runtime-assembly.js +28 -0
- package/dist/mcp/merchant-runtime-assembly.js.map +1 -1
- package/dist/mcp/merchant-server.d.ts +16 -0
- package/dist/mcp/merchant-server.js +24 -0
- package/dist/mcp/merchant-server.js.map +1 -1
- package/dist/mcp/tools.js +4 -3
- package/dist/mcp/tools.js.map +1 -1
- package/dist/merchant/negotiation-observer.d.ts +35 -0
- package/dist/merchant/negotiation-observer.js +168 -57
- package/dist/merchant/negotiation-observer.js.map +1 -1
- package/dist/merchant/negotiation-ops.d.ts +77 -0
- package/dist/merchant/negotiation-ops.js +203 -0
- package/dist/merchant/negotiation-ops.js.map +1 -0
- package/dist/merchant/watch/detect.d.ts +37 -0
- package/dist/merchant/watch/detect.js +147 -0
- package/dist/merchant/watch/detect.js.map +1 -0
- package/dist/merchant/watch/notify.d.ts +48 -0
- package/dist/merchant/watch/notify.js +108 -0
- package/dist/merchant/watch/notify.js.map +1 -0
- package/dist/merchant/watch/service.d.ts +43 -0
- package/dist/merchant/watch/service.js +167 -0
- package/dist/merchant/watch/service.js.map +1 -0
- package/dist/merchant/watch/state.d.ts +49 -0
- package/dist/merchant/watch/state.js +68 -0
- package/dist/merchant/watch/state.js.map +1 -0
- package/dist/merchant/watch/weixin.d.ts +54 -0
- package/dist/merchant/watch/weixin.js +271 -0
- package/dist/merchant/watch/weixin.js.map +1 -0
- package/dist/merchant-runtime/expire-offers.d.ts +30 -0
- package/dist/merchant-runtime/expire-offers.js +112 -0
- package/dist/merchant-runtime/expire-offers.js.map +1 -0
- package/dist/merchant-runtime/jobs.d.ts +3 -0
- package/dist/merchant-runtime/jobs.js +9 -0
- package/dist/merchant-runtime/jobs.js.map +1 -1
- package/dist/merchant-runtime/services.d.ts +0 -8
- package/dist/merchant-runtime/services.js +38 -0
- package/dist/merchant-runtime/services.js.map +1 -1
- package/package.json +3 -1
package/dist/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();
|