@a2hmarket/a2hmarket 0.5.4 → 0.5.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/package.json +1 -1
- package/src/mqtt-transport.ts +8 -1
- package/src/tools/send.ts +13 -2
package/package.json
CHANGED
package/src/mqtt-transport.ts
CHANGED
|
@@ -116,6 +116,13 @@ export class MqttTransport {
|
|
|
116
116
|
}
|
|
117
117
|
|
|
118
118
|
close(): void {
|
|
119
|
+
this.closed = true;
|
|
120
|
+
this.client?.end(false); // graceful close — flush pending packets
|
|
121
|
+
this.client = null;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** Force-close immediately (for reconnect cleanup). */
|
|
125
|
+
forceClose(): void {
|
|
119
126
|
this.closed = true;
|
|
120
127
|
this.client?.end(true);
|
|
121
128
|
this.client = null;
|
|
@@ -137,7 +144,7 @@ export class MqttTransport {
|
|
|
137
144
|
try {
|
|
138
145
|
const cred = await this.tokenClient.getToken(this.clientId, true);
|
|
139
146
|
|
|
140
|
-
this.client?.end(true);
|
|
147
|
+
this.client?.end(true); // force-close old connection for reconnect
|
|
141
148
|
this.client = mqtt.connect(this.brokerUrl, {
|
|
142
149
|
clientId: this.clientId,
|
|
143
150
|
username: cred.username,
|
package/src/tools/send.ts
CHANGED
|
@@ -4,6 +4,8 @@ import { MqttTokenClient } from "../mqtt-token.js";
|
|
|
4
4
|
import { createSendTransport } from "../mqtt-transport.js";
|
|
5
5
|
import { buildEnvelope, signEnvelope } from "../protocol.js";
|
|
6
6
|
|
|
7
|
+
const log = (m: string) => process.stderr.write(`[a2h_send] ${m}\n`);
|
|
8
|
+
|
|
7
9
|
export function registerSendTool(api: OpenClawPluginApi, creds: A2HCredentials) {
|
|
8
10
|
api.registerTool({
|
|
9
11
|
name: "a2h_send",
|
|
@@ -66,27 +68,33 @@ export function registerSendTool(api: OpenClawPluginApi, creds: A2HCredentials)
|
|
|
66
68
|
const signed = signEnvelope(creds.agentKey, envelope);
|
|
67
69
|
|
|
68
70
|
// Send via short-lived MQTT connection
|
|
71
|
+
log(`sending to ${targetAgentId} (broker: ${creds.mqttUrl})`);
|
|
69
72
|
const tokenClient = new MqttTokenClient(creds.apiUrl, creds.agentId, creds.agentKey);
|
|
70
73
|
const transport = createSendTransport(creds.mqttUrl, tokenClient, creds.agentId);
|
|
71
74
|
|
|
72
75
|
try {
|
|
73
76
|
await transport.connect();
|
|
77
|
+
log("mqtt connected");
|
|
78
|
+
|
|
74
79
|
// Retry up to 3 times
|
|
75
80
|
let lastErr: unknown;
|
|
76
81
|
for (let attempt = 0; attempt < 3; attempt++) {
|
|
77
82
|
try {
|
|
78
83
|
await transport.publish(targetAgentId, signed);
|
|
84
|
+
log(`publish ok (attempt ${attempt + 1})`);
|
|
79
85
|
lastErr = null;
|
|
80
86
|
break;
|
|
81
87
|
} catch (err) {
|
|
82
88
|
lastErr = err;
|
|
89
|
+
log(`publish attempt ${attempt + 1} failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
83
90
|
if (attempt < 2) await sleep(1000 * (1 << attempt));
|
|
84
91
|
}
|
|
85
92
|
}
|
|
86
93
|
if (lastErr) throw lastErr;
|
|
87
94
|
|
|
88
|
-
//
|
|
89
|
-
await sleep(
|
|
95
|
+
// Wait for broker to fully process (graceful close needs time to flush)
|
|
96
|
+
await sleep(500);
|
|
97
|
+
log("flush wait done, closing");
|
|
90
98
|
|
|
91
99
|
return {
|
|
92
100
|
result: JSON.stringify(
|
|
@@ -99,6 +107,9 @@ export function registerSendTool(api: OpenClawPluginApi, creds: A2HCredentials)
|
|
|
99
107
|
2
|
|
100
108
|
),
|
|
101
109
|
};
|
|
110
|
+
} catch (err) {
|
|
111
|
+
log(`send failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
112
|
+
throw err;
|
|
102
113
|
} finally {
|
|
103
114
|
transport.close();
|
|
104
115
|
}
|