@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
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2026 harrylabsj
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
const MAX_RESPONSE_BYTES = 256 * 1024;
|
|
17
|
+
const SEND_TIMEOUT_MS = 5_000;
|
|
18
|
+
export class WecomWebhookChannel {
|
|
19
|
+
options;
|
|
20
|
+
name = "wecom";
|
|
21
|
+
url;
|
|
22
|
+
constructor(webhookUrl, options = {}) {
|
|
23
|
+
this.options = options;
|
|
24
|
+
let parsed;
|
|
25
|
+
try {
|
|
26
|
+
parsed = new URL(webhookUrl);
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
throw new Error("企业微信 webhook URL 不合法");
|
|
30
|
+
}
|
|
31
|
+
if (parsed.protocol !== "https:") {
|
|
32
|
+
throw new Error("企业微信 webhook 必须是 https");
|
|
33
|
+
}
|
|
34
|
+
if (parsed.username !== "" || parsed.password !== "") {
|
|
35
|
+
throw new Error("企业微信 webhook 不允许 userinfo");
|
|
36
|
+
}
|
|
37
|
+
const allowed = options.allowedHosts ?? ["qyapi.weixin.qq.com"];
|
|
38
|
+
if (!allowed.includes(parsed.hostname)) {
|
|
39
|
+
throw new Error(`企业微信 webhook 主机不在白名单:${parsed.hostname}`);
|
|
40
|
+
}
|
|
41
|
+
this.url = parsed;
|
|
42
|
+
}
|
|
43
|
+
async send(text) {
|
|
44
|
+
const fetchImpl = this.options.fetchImpl ?? fetch;
|
|
45
|
+
const response = await fetchImpl(this.url, {
|
|
46
|
+
method: "POST",
|
|
47
|
+
headers: { "content-type": "application/json" },
|
|
48
|
+
body: JSON.stringify({ msgtype: "text", text: { content: text } }),
|
|
49
|
+
redirect: "error",
|
|
50
|
+
signal: AbortSignal.timeout(SEND_TIMEOUT_MS),
|
|
51
|
+
});
|
|
52
|
+
const body = await response.text();
|
|
53
|
+
if (Buffer.byteLength(body) > MAX_RESPONSE_BYTES) {
|
|
54
|
+
throw new Error("企业微信 webhook 响应过大");
|
|
55
|
+
}
|
|
56
|
+
if (!response.ok) {
|
|
57
|
+
throw new Error(`企业微信 webhook HTTP ${response.status}`);
|
|
58
|
+
}
|
|
59
|
+
let parsed;
|
|
60
|
+
try {
|
|
61
|
+
parsed = JSON.parse(body);
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
throw new Error("企业微信 webhook 响应不是 JSON");
|
|
65
|
+
}
|
|
66
|
+
const errcode = parsed.errcode;
|
|
67
|
+
if (typeof errcode === "number" && errcode !== 0) {
|
|
68
|
+
throw new Error(`企业微信 webhook errcode ${errcode}`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
export class IlinkWatchChannel {
|
|
73
|
+
client;
|
|
74
|
+
credentials;
|
|
75
|
+
recipients;
|
|
76
|
+
contextTokenFor;
|
|
77
|
+
name = "weixin";
|
|
78
|
+
constructor(client, credentials, recipients, contextTokenFor) {
|
|
79
|
+
this.client = client;
|
|
80
|
+
this.credentials = credentials;
|
|
81
|
+
this.recipients = recipients;
|
|
82
|
+
this.contextTokenFor = contextTokenFor;
|
|
83
|
+
}
|
|
84
|
+
async send(text) {
|
|
85
|
+
if (this.recipients.length === 0) {
|
|
86
|
+
throw new Error("个人微信通道没有授权接收人");
|
|
87
|
+
}
|
|
88
|
+
const failures = [];
|
|
89
|
+
for (const userId of this.recipients) {
|
|
90
|
+
try {
|
|
91
|
+
await this.client.sendMessage({
|
|
92
|
+
to_user_id: userId,
|
|
93
|
+
text: text.length > 2000 ? `${text.slice(0, 2000)}…` : text,
|
|
94
|
+
...(this.contextTokenFor(userId) !== undefined
|
|
95
|
+
? { context_token: this.contextTokenFor(userId) }
|
|
96
|
+
: {}),
|
|
97
|
+
}, this.credentials);
|
|
98
|
+
}
|
|
99
|
+
catch (err) {
|
|
100
|
+
failures.push(`${userId}: ${err instanceof Error ? err.message : String(err)}`);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (failures.length > 0) {
|
|
104
|
+
throw new Error(`个人微信发送失败(${failures.length}/${this.recipients.length})`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
//# sourceMappingURL=notify.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"notify.js","sourceRoot":"","sources":["../../../src/merchant/watch/notify.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAcH,MAAM,kBAAkB,GAAG,GAAG,GAAG,IAAI,CAAC;AACtC,MAAM,eAAe,GAAG,KAAK,CAAC;AAO9B,MAAM,OAAO,mBAAmB;IAMX;IALV,IAAI,GAAG,OAAO,CAAC;IACP,GAAG,CAAM;IAE1B,YACE,UAAkB,EACD,UAA0E,EAAE;QAA5E,YAAO,GAAP,OAAO,CAAqE;QAE7F,IAAI,MAAW,CAAC;QAChB,IAAI,CAAC;YACH,MAAM,GAAG,IAAI,GAAG,CAAC,UAAU,CAAC,CAAC;QAC/B,CAAC;QAAC,MAAM,CAAC;YACP,MAAM,IAAI,KAAK,CAAC,sBAAsB,CAAC,CAAC;QAC1C,CAAC;QACD,IAAI,MAAM,CAAC,QAAQ,KAAK,QAAQ,EAAE,CAAC;YACjC,MAAM,IAAI,KAAK,CAAC,wBAAwB,CAAC,CAAC;QAC5C,CAAC;QACD,IAAI,MAAM,CAAC,QAAQ,KAAK,EAAE,IAAI,MAAM,CAAC,QAAQ,KAAK,EAAE,EAAE,CAAC;YACrD,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAC;QAC/C,CAAC;QACD,MAAM,OAAO,GAAG,OAAO,CAAC,YAAY,IAAI,CAAC,qBAAqB,CAAC,CAAC;QAChE,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC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AAG,MAAM,KAAK,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;YAClF,CAAC;QACH,CAAC;QACD,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACxB,MAAM,IAAI,KAAK,CAAC,YAAY,QAAQ,CAAC,MAAM,IAAI,IAAI,CAAC,UAAU,CAAC,MAAM,GAAG,CAAC,CAAC;QAC5E,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2026 harrylabsj
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import type { NegotiationOps } from "../negotiation-ops.js";
|
|
17
|
+
import type { WatchChannel } from "./notify.js";
|
|
18
|
+
export interface MerchantWatchServiceOptions {
|
|
19
|
+
dataDir: string;
|
|
20
|
+
ops: NegotiationOps;
|
|
21
|
+
channels: readonly WatchChannel[];
|
|
22
|
+
intervalMs?: number;
|
|
23
|
+
now?: () => Date;
|
|
24
|
+
log?: (line: string) => void;
|
|
25
|
+
}
|
|
26
|
+
export declare class MerchantWatchService {
|
|
27
|
+
private readonly options;
|
|
28
|
+
private readonly statePath;
|
|
29
|
+
private readonly channels;
|
|
30
|
+
private readonly intervalMs;
|
|
31
|
+
private readonly now;
|
|
32
|
+
private readonly log;
|
|
33
|
+
private timer;
|
|
34
|
+
private running;
|
|
35
|
+
private ticking;
|
|
36
|
+
constructor(options: MerchantWatchServiceOptions);
|
|
37
|
+
start(): void;
|
|
38
|
+
stop(): void;
|
|
39
|
+
tick(): Promise<void>;
|
|
40
|
+
private runTick;
|
|
41
|
+
private deliver;
|
|
42
|
+
private retryQueue;
|
|
43
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2026 harrylabsj
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* watch 守护循环:轮询磋商/审批快照 → 检测可行动事件 → 多渠道投递。
|
|
18
|
+
*
|
|
19
|
+
* 投递语义至少一次:发送成功/失败的渠道分别记账;失败进持久化队列,
|
|
20
|
+
* 5s/30s/2m/10m 退避,5 次后放弃并记日志。单渠道失败不影响其他渠道。
|
|
21
|
+
*/
|
|
22
|
+
import path from "node:path";
|
|
23
|
+
import { detectNotifications } from "./detect.js";
|
|
24
|
+
import { loadWatchState, saveWatchState, watchStatePath, } from "./state.js";
|
|
25
|
+
const RETRY_DELAYS_MS = [5_000, 30_000, 120_000, 600_000];
|
|
26
|
+
const MAX_ATTEMPTS = 5;
|
|
27
|
+
export class MerchantWatchService {
|
|
28
|
+
options;
|
|
29
|
+
statePath;
|
|
30
|
+
channels;
|
|
31
|
+
intervalMs;
|
|
32
|
+
now;
|
|
33
|
+
log;
|
|
34
|
+
timer;
|
|
35
|
+
running = false;
|
|
36
|
+
ticking = null;
|
|
37
|
+
constructor(options) {
|
|
38
|
+
this.options = options;
|
|
39
|
+
this.channels = options.channels;
|
|
40
|
+
this.intervalMs = Math.max(5_000, options.intervalMs ?? 10_000);
|
|
41
|
+
this.now = options.now ?? (() => new Date());
|
|
42
|
+
this.log = options.log ?? ((line) => process.stderr.write(`${line}\n`));
|
|
43
|
+
this.statePath = watchStatePath(path.resolve(options.dataDir));
|
|
44
|
+
}
|
|
45
|
+
start() {
|
|
46
|
+
if (this.running)
|
|
47
|
+
return;
|
|
48
|
+
this.running = true;
|
|
49
|
+
void this.tick();
|
|
50
|
+
this.timer = setInterval(() => void this.tick(), this.intervalMs);
|
|
51
|
+
this.timer.unref();
|
|
52
|
+
}
|
|
53
|
+
stop() {
|
|
54
|
+
this.running = false;
|
|
55
|
+
if (this.timer !== undefined)
|
|
56
|
+
clearInterval(this.timer);
|
|
57
|
+
this.timer = undefined;
|
|
58
|
+
}
|
|
59
|
+
tick() {
|
|
60
|
+
if (!this.running)
|
|
61
|
+
return Promise.resolve();
|
|
62
|
+
if (this.ticking !== null)
|
|
63
|
+
return this.ticking;
|
|
64
|
+
this.ticking = this.runTick().finally(() => {
|
|
65
|
+
this.ticking = null;
|
|
66
|
+
});
|
|
67
|
+
return this.ticking;
|
|
68
|
+
}
|
|
69
|
+
async runTick() {
|
|
70
|
+
let state;
|
|
71
|
+
try {
|
|
72
|
+
state = loadWatchState(this.statePath, this.now().toISOString());
|
|
73
|
+
}
|
|
74
|
+
catch (err) {
|
|
75
|
+
this.log(`[watch] ${err instanceof Error ? err.message : String(err)}`);
|
|
76
|
+
this.running = false;
|
|
77
|
+
if (this.timer !== undefined)
|
|
78
|
+
clearInterval(this.timer);
|
|
79
|
+
this.timer = undefined;
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
let snapshot;
|
|
83
|
+
try {
|
|
84
|
+
snapshot = this.options.ops.snapshot();
|
|
85
|
+
}
|
|
86
|
+
catch (err) {
|
|
87
|
+
this.log(`[watch] 读取磋商快照失败:${err instanceof Error ? err.message : String(err)}`);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
const detected = detectNotifications(state, snapshot, {
|
|
91
|
+
channels: this.channels.map((channel) => channel.name),
|
|
92
|
+
now: this.now(),
|
|
93
|
+
});
|
|
94
|
+
state = detected.next;
|
|
95
|
+
for (const notification of detected.notifications) {
|
|
96
|
+
await this.deliver(state, notification);
|
|
97
|
+
}
|
|
98
|
+
await this.retryQueue(state);
|
|
99
|
+
state.updated_at = this.now().toISOString();
|
|
100
|
+
try {
|
|
101
|
+
saveWatchState(this.statePath, state);
|
|
102
|
+
}
|
|
103
|
+
catch (err) {
|
|
104
|
+
this.log(`[watch] 状态写入失败:${err instanceof Error ? err.message : String(err)}`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
async deliver(state, notification) {
|
|
108
|
+
const failed = {};
|
|
109
|
+
for (const channel of this.channels) {
|
|
110
|
+
const text = notification.texts[channel.name];
|
|
111
|
+
if (text === undefined)
|
|
112
|
+
continue;
|
|
113
|
+
try {
|
|
114
|
+
await channel.send(text);
|
|
115
|
+
}
|
|
116
|
+
catch (err) {
|
|
117
|
+
failed[channel.name] = text;
|
|
118
|
+
this.log(`[watch] ${channel.name} 投递失败 ${notification.key}:${err instanceof Error ? err.message : String(err)}`);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
if (Object.keys(failed).length > 0) {
|
|
122
|
+
state.queue.push({
|
|
123
|
+
key: notification.key,
|
|
124
|
+
texts: failed,
|
|
125
|
+
attempts: 0,
|
|
126
|
+
next_at: new Date(this.now().getTime() + RETRY_DELAYS_MS[0]).toISOString(),
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
async retryQueue(state) {
|
|
131
|
+
const now = this.now().getTime();
|
|
132
|
+
const pending = [];
|
|
133
|
+
for (const item of state.queue) {
|
|
134
|
+
if (Date.parse(item.next_at) > now) {
|
|
135
|
+
pending.push(item);
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
const failed = {};
|
|
139
|
+
for (const [name, text] of Object.entries(item.texts)) {
|
|
140
|
+
const channel = this.channels.find((candidate) => candidate.name === name);
|
|
141
|
+
if (channel === undefined)
|
|
142
|
+
continue;
|
|
143
|
+
try {
|
|
144
|
+
await channel.send(text);
|
|
145
|
+
}
|
|
146
|
+
catch {
|
|
147
|
+
failed[name] = text;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (Object.keys(failed).length === 0)
|
|
151
|
+
continue;
|
|
152
|
+
const attempts = item.attempts + 1;
|
|
153
|
+
if (attempts >= MAX_ATTEMPTS) {
|
|
154
|
+
this.log(`[watch] 放弃投递 ${item.key}(${attempts} 次失败)`);
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
pending.push({
|
|
158
|
+
...item,
|
|
159
|
+
texts: failed,
|
|
160
|
+
attempts,
|
|
161
|
+
next_at: new Date(now + RETRY_DELAYS_MS[Math.min(attempts, RETRY_DELAYS_MS.length - 1)]).toISOString(),
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
state.queue = pending;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
//# sourceMappingURL=service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"service.js","sourceRoot":"","sources":["../../../src/merchant/watch/service.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH;;;;;GAKG;AAEH,OAAO,IAAI,MAAM,WAAW,CAAC;AAG7B,OAAO,EAAE,mBAAmB,EAA0B,MAAM,aAAa,CAAC;AAE1E,OAAO,EACL,cAAc,EACd,cAAc,EACd,cAAc,GAGf,MAAM,YAAY,CAAC;AAEpB,MAAM,eAAe,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,OAA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|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
export interface WatchNegotiationState {
|
|
17
|
+
last_event_digest: string;
|
|
18
|
+
agreement: boolean;
|
|
19
|
+
last_notified_at?: string;
|
|
20
|
+
}
|
|
21
|
+
export interface WatchApprovalState {
|
|
22
|
+
status: string;
|
|
23
|
+
updated_at: string;
|
|
24
|
+
negotiation_id?: string;
|
|
25
|
+
sku?: string;
|
|
26
|
+
notified_at?: string;
|
|
27
|
+
}
|
|
28
|
+
export interface WatchQueueItem {
|
|
29
|
+
key: string;
|
|
30
|
+
/** 每个渠道各自的文本(已渲染;0600 文件内可能含金额)。 */
|
|
31
|
+
texts: Record<string, string>;
|
|
32
|
+
attempts: number;
|
|
33
|
+
next_at: string;
|
|
34
|
+
}
|
|
35
|
+
export interface WatchState {
|
|
36
|
+
version: 1;
|
|
37
|
+
updated_at: string;
|
|
38
|
+
initialized_at?: string;
|
|
39
|
+
negotiations: Record<string, WatchNegotiationState>;
|
|
40
|
+
approvals: Record<string, WatchApprovalState>;
|
|
41
|
+
queue: WatchQueueItem[];
|
|
42
|
+
}
|
|
43
|
+
export declare function emptyWatchState(now: string): WatchState;
|
|
44
|
+
export declare class WatchStateError extends Error {
|
|
45
|
+
constructor(message: string);
|
|
46
|
+
}
|
|
47
|
+
export declare function watchStatePath(dataDir: string): string;
|
|
48
|
+
export declare function loadWatchState(file: string, now: string): WatchState;
|
|
49
|
+
export declare function saveWatchState(file: string, state: WatchState): void;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2026 harrylabsj
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* watch 去重/投递状态(`<dataDir>/watch/state.json`,0600 原子写)。
|
|
18
|
+
*
|
|
19
|
+
* 损坏状态 fail-closed:不从头回放历史(会把 61 场旧磋商重新通知一遍),
|
|
20
|
+
* 由调用方提示修复或删除文件。目录 0700。
|
|
21
|
+
*/
|
|
22
|
+
import { chmodSync, existsSync, mkdirSync, readFileSync } from "node:fs";
|
|
23
|
+
import path from "node:path";
|
|
24
|
+
import { writeFileAtomic } from "../../fs/atomic-write.js";
|
|
25
|
+
export function emptyWatchState(now) {
|
|
26
|
+
return { version: 1, updated_at: now, negotiations: {}, approvals: {}, queue: [] };
|
|
27
|
+
}
|
|
28
|
+
export class WatchStateError extends Error {
|
|
29
|
+
constructor(message) {
|
|
30
|
+
super(message);
|
|
31
|
+
this.name = "WatchStateError";
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
export function watchStatePath(dataDir) {
|
|
35
|
+
return path.join(dataDir, "watch", "state.json");
|
|
36
|
+
}
|
|
37
|
+
export function loadWatchState(file, now) {
|
|
38
|
+
if (!existsSync(file))
|
|
39
|
+
return emptyWatchState(now);
|
|
40
|
+
let parsed;
|
|
41
|
+
try {
|
|
42
|
+
parsed = JSON.parse(readFileSync(file, "utf8"));
|
|
43
|
+
}
|
|
44
|
+
catch (err) {
|
|
45
|
+
throw new WatchStateError(`watch 状态文件损坏(${file});修复或删除后重启:${err instanceof Error ? err.message : String(err)}`);
|
|
46
|
+
}
|
|
47
|
+
if (parsed === null || typeof parsed !== "object") {
|
|
48
|
+
throw new WatchStateError(`watch 状态文件损坏(${file});修复或删除后重启`);
|
|
49
|
+
}
|
|
50
|
+
const raw = parsed;
|
|
51
|
+
if (raw.version !== 1 || typeof raw.negotiations !== "object" || typeof raw.approvals !== "object") {
|
|
52
|
+
throw new WatchStateError(`watch 状态文件版本/形状不受支持(${file})`);
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
version: 1,
|
|
56
|
+
updated_at: typeof raw.updated_at === "string" ? raw.updated_at : now,
|
|
57
|
+
...(typeof raw.initialized_at === "string" ? { initialized_at: raw.initialized_at } : {}),
|
|
58
|
+
negotiations: raw.negotiations ?? {},
|
|
59
|
+
approvals: raw.approvals ?? {},
|
|
60
|
+
queue: Array.isArray(raw.queue) ? raw.queue : [],
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
export function saveWatchState(file, state) {
|
|
64
|
+
mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 });
|
|
65
|
+
chmodSync(path.dirname(file), 0o700);
|
|
66
|
+
writeFileAtomic(file, `${JSON.stringify(state, null, 2)}\n`, { mode: 0o600 });
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=state.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"state.js","sourceRoot":"","sources":["../../../src/merchant/watch/state.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH;;;;;GAKG;AAEH,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACzE,OAAO,IAAI,MAAM,WAAW,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
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import { type BotCredentials } from "../../weixin/types.js";
|
|
17
|
+
import type { NegotiationOps } from "../negotiation-ops.js";
|
|
18
|
+
import { type WatchChannel } from "./notify.js";
|
|
19
|
+
export interface WeixinOpsPaths {
|
|
20
|
+
credentials: string;
|
|
21
|
+
sync: string;
|
|
22
|
+
context: string;
|
|
23
|
+
}
|
|
24
|
+
export declare function weixinOpsPaths(dataDir: string): WeixinOpsPaths;
|
|
25
|
+
export interface WeixinOpsBotOptions {
|
|
26
|
+
dataDir: string;
|
|
27
|
+
allowUsers?: readonly string[];
|
|
28
|
+
forceRelogin?: boolean;
|
|
29
|
+
apiBaseUrl?: string;
|
|
30
|
+
noQr?: boolean;
|
|
31
|
+
log?: (line: string) => void;
|
|
32
|
+
}
|
|
33
|
+
export declare class WeixinOpsBot {
|
|
34
|
+
private readonly client;
|
|
35
|
+
private readonly paths;
|
|
36
|
+
private readonly allowUsers;
|
|
37
|
+
private readonly log;
|
|
38
|
+
private credentials;
|
|
39
|
+
private syncBuf;
|
|
40
|
+
private seen;
|
|
41
|
+
private contexts;
|
|
42
|
+
private stopped;
|
|
43
|
+
private constructor();
|
|
44
|
+
static open(options: WeixinOpsBotOptions): Promise<WeixinOpsBot>;
|
|
45
|
+
credentialsRef(): BotCredentials;
|
|
46
|
+
recipients(): string[];
|
|
47
|
+
channel(): WatchChannel;
|
|
48
|
+
send(text: string): Promise<void>;
|
|
49
|
+
stop(): void;
|
|
50
|
+
/** 长轮询 + 命令处理;返回 false 表示凭证失效需重新扫码。 */
|
|
51
|
+
run(service: NegotiationOps): Promise<boolean>;
|
|
52
|
+
private process;
|
|
53
|
+
private handleCommand;
|
|
54
|
+
}
|