@glin_1/miniabc 1.0.0 → 1.1.0
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/channel.ts +141 -15
package/package.json
CHANGED
package/src/channel.ts
CHANGED
|
@@ -179,21 +179,120 @@ export const miniabcPlugin: ChannelPlugin<ResolvedMiniABCAccount> = {
|
|
|
179
179
|
},
|
|
180
180
|
},
|
|
181
181
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
182
|
+
gateway: {
|
|
183
|
+
startAccount: async (ctx) => {
|
|
184
|
+
const { account, abortSignal, log, cfg } = ctx;
|
|
185
|
+
|
|
186
|
+
log?.info(`[miniabc:${account.accountId}] Starting gateway — botId=${account.botId}, enabled=${account.enabled}`);
|
|
187
|
+
console.log(`[miniabc:channel] startAccount: accountId=${account.accountId}, botId=${account.botId}`);
|
|
188
|
+
|
|
189
|
+
// 建立 WebSocket 连接接收任务通知
|
|
190
|
+
const wsUrl = account.wsUrl || `${account.platformUrl.replace('https://', 'wss://').replace('http://', 'ws://')}/ws/openclaw`;
|
|
191
|
+
|
|
192
|
+
const ws = new WebSocket(wsUrl);
|
|
193
|
+
let heartbeatInterval: NodeJS.Timeout | null = null;
|
|
194
|
+
|
|
195
|
+
// 创建一个 Promise 来保持函数运行,直到 WebSocket 关闭或 abort
|
|
196
|
+
return new Promise<void>((resolve, reject) => {
|
|
197
|
+
ws.onopen = () => {
|
|
198
|
+
log?.info(`[miniabc:${account.accountId}] WebSocket connected to ${wsUrl}`);
|
|
199
|
+
console.log(`[miniabc] WebSocket connected to ${wsUrl}`);
|
|
200
|
+
|
|
201
|
+
// 发送认证
|
|
202
|
+
ws.send(JSON.stringify({
|
|
203
|
+
type: 'auth',
|
|
204
|
+
payload: { botId: account.botId, token: account.token }
|
|
205
|
+
}));
|
|
206
|
+
|
|
207
|
+
// 设置心跳
|
|
208
|
+
heartbeatInterval = setInterval(() => {
|
|
209
|
+
if (ws.readyState === WebSocket.OPEN) {
|
|
210
|
+
ws.send(JSON.stringify({ type: 'heartbeat' }));
|
|
211
|
+
}
|
|
212
|
+
}, 30000);
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
ws.onmessage = (event) => {
|
|
216
|
+
try {
|
|
217
|
+
const data = JSON.parse(event.data as string);
|
|
218
|
+
log?.debug(`[miniabc:${account.accountId}] WebSocket message: ${data.type}`);
|
|
219
|
+
|
|
220
|
+
switch (data.type) {
|
|
221
|
+
case 'new_task':
|
|
222
|
+
// 收到新任务,触发 inbound 事件
|
|
223
|
+
log?.info(`[miniabc:${account.accountId}] New task received: ${data.payload.task.id}`);
|
|
224
|
+
ctx.onInbound({
|
|
225
|
+
type: 'message',
|
|
226
|
+
channel: 'miniabc',
|
|
227
|
+
accountId: account.accountId,
|
|
228
|
+
from: { id: data.payload.task.publisher_id, name: 'TaskPublisher' },
|
|
229
|
+
text: `新任务: ${data.payload.task.content}`,
|
|
230
|
+
raw: data.payload.task,
|
|
231
|
+
});
|
|
232
|
+
break;
|
|
233
|
+
case 'new_message':
|
|
234
|
+
// 收到新消息
|
|
235
|
+
log?.info(`[miniabc:${account.accountId}] New message received`);
|
|
236
|
+
ctx.onInbound({
|
|
237
|
+
type: 'message',
|
|
238
|
+
channel: 'miniabc',
|
|
239
|
+
accountId: account.accountId,
|
|
240
|
+
from: { id: data.payload.message.bot_id, name: 'System' },
|
|
241
|
+
text: data.payload.message.content,
|
|
242
|
+
raw: data.payload.message,
|
|
243
|
+
});
|
|
244
|
+
break;
|
|
245
|
+
case 'auth_success':
|
|
246
|
+
log?.info(`[miniabc:${account.accountId}] WebSocket authenticated: ${data.payload.botId}`);
|
|
247
|
+
ctx.setStatus({
|
|
248
|
+
...ctx.getStatus(),
|
|
249
|
+
running: true,
|
|
250
|
+
connected: true,
|
|
251
|
+
lastConnectedAt: Date.now(),
|
|
252
|
+
});
|
|
253
|
+
break;
|
|
254
|
+
case 'pong':
|
|
255
|
+
// Heartbeat response
|
|
256
|
+
break;
|
|
257
|
+
}
|
|
258
|
+
} catch (e) {
|
|
259
|
+
log?.error(`[miniabc:${account.accountId}] WebSocket message parse error: ${e}`);
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
ws.onerror = (error) => {
|
|
264
|
+
log?.error(`[miniabc:${account.accountId}] WebSocket error: ${error}`);
|
|
265
|
+
ctx.setStatus({
|
|
266
|
+
...ctx.getStatus(),
|
|
267
|
+
lastError: String(error),
|
|
268
|
+
});
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
ws.onclose = () => {
|
|
272
|
+
log?.info(`[miniabc:${account.accountId}] WebSocket disconnected`);
|
|
273
|
+
if (heartbeatInterval) {
|
|
274
|
+
clearInterval(heartbeatInterval);
|
|
275
|
+
}
|
|
276
|
+
ctx.setStatus({
|
|
277
|
+
...ctx.getStatus(),
|
|
278
|
+
running: false,
|
|
279
|
+
connected: false,
|
|
280
|
+
});
|
|
281
|
+
resolve();
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
// 监听 abort 信号
|
|
285
|
+
if (abortSignal) {
|
|
286
|
+
abortSignal.addEventListener('abort', () => {
|
|
287
|
+
log?.info(`[miniabc:${account.accountId}] Gateway stopped by abort signal`);
|
|
288
|
+
if (heartbeatInterval) {
|
|
289
|
+
clearInterval(heartbeatInterval);
|
|
290
|
+
}
|
|
291
|
+
ws.close();
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
});
|
|
295
|
+
},
|
|
197
296
|
},
|
|
198
297
|
|
|
199
298
|
sendText: async ({ account, target, text }) => {
|
|
@@ -223,4 +322,31 @@ export const miniabcPlugin: ChannelPlugin<ResolvedMiniABCAccount> = {
|
|
|
223
322
|
|
|
224
323
|
throw new Error("Direct messaging not supported.");
|
|
225
324
|
},
|
|
325
|
+
|
|
326
|
+
status: {
|
|
327
|
+
defaultRuntime: {
|
|
328
|
+
accountId: DEFAULT_ACCOUNT_ID,
|
|
329
|
+
running: false,
|
|
330
|
+
connected: false,
|
|
331
|
+
lastConnectedAt: null,
|
|
332
|
+
lastError: null,
|
|
333
|
+
lastInboundAt: null,
|
|
334
|
+
lastOutboundAt: null,
|
|
335
|
+
},
|
|
336
|
+
buildChannelSummary: ({ snapshot }: { snapshot: Record<string, unknown> }) => ({
|
|
337
|
+
configured: snapshot.configured ?? false,
|
|
338
|
+
running: snapshot.running ?? false,
|
|
339
|
+
connected: snapshot.connected ?? false,
|
|
340
|
+
lastConnectedAt: snapshot.lastConnectedAt ?? null,
|
|
341
|
+
lastError: snapshot.lastError ?? null,
|
|
342
|
+
}),
|
|
343
|
+
buildAccountSnapshot: ({ account, runtime }: { account?: ResolvedMiniABCAccount; runtime?: Record<string, unknown> }) => ({
|
|
344
|
+
accountId: account?.accountId ?? DEFAULT_ACCOUNT_ID,
|
|
345
|
+
name: account?.name,
|
|
346
|
+
enabled: account?.enabled ?? false,
|
|
347
|
+
configured: Boolean(account?.botId && account?.token),
|
|
348
|
+
platformUrl: account?.platformUrl,
|
|
349
|
+
...(runtime ?? {}),
|
|
350
|
+
}),
|
|
351
|
+
},
|
|
226
352
|
};
|