aicq-chat-plugin 3.8.1 → 3.9.1

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/src/channel.js CHANGED
@@ -1,613 +1,616 @@
1
- /**
2
- * AICQ Channel Plugin — Core Channel Logic
3
- *
4
- * Uses the official OpenClaw Channel Plugin SDK:
5
- * createChatChannelPlugin + createChannelPluginBase
6
- *
7
- * Architecture: In-process Channel (no sidecar, no independent port)
8
- *
9
- * The runtime store is a mutable object populated by registerFull() in
10
- * index.js. This keeps the channel-plugin object safe to import during
11
- * setup-only / discovery modes without pulling in transport clients or
12
- * database handles.
13
- */
14
-
15
- import {
16
- createChatChannelPlugin,
17
- createChannelPluginBase,
18
- } from "openclaw/plugin-sdk/channel-core";
19
-
20
- // ── Mutable runtime store ────────────────────────────────────────────
21
- // Populated lazily by the registerFull() callback in index.js.
22
- // Adapters that need runtime state check these before acting.
23
- export const runtime = {
24
- db: null,
25
- identity: null,
26
- serverClient: null,
27
- handshake: null,
28
- chat: null,
29
- dataDir: null,
30
- serverUrl: null,
31
- handleGateway: null,
32
- _initialized: false,
33
- };
34
-
35
- // ── Template variable resolver ───────────────────────────────────────
36
- // OpenClaw stores accountId as-is (e.g. "{{agent.id}}") in config.
37
- // Plugins must resolve template variables at runtime.
38
- //
39
- // The default agent ID in OpenClaw is "main" (DEFAULT_AGENT_ID).
40
- // When cfg.agents.list is empty/undefined (no explicit agent config),
41
- // the implicit default agent "main" is used.
42
-
43
- const OPENCLAW_DEFAULT_AGENT_ID = "main";
44
-
45
- function resolveTemplateVar(cfg, value) {
46
- if (typeof value !== "string") return value;
47
- const match = value.match(/^\{\{(\w[\w.]*)\}\}$/);
48
- if (!match) return value;
49
-
50
- const tmplPath = match[1]; // e.g. "agent.id"
51
- if (tmplPath === "agent.id") {
52
- // Strategy: look for explicit agents in config first
53
- const agents = cfg.agents?.list;
54
- if (Array.isArray(agents) && agents.length > 0) {
55
- // Use the default=true agent, or the first one
56
- const defaultAgent = agents.find((a) => a.default) || agents[0];
57
- if (defaultAgent?.id) return defaultAgent.id;
58
- }
59
- // Fallback: OpenClaw's implicit default agent ID
60
- return OPENCLAW_DEFAULT_AGENT_ID;
61
- }
62
-
63
- return value; // unknown template — return as-is
64
- }
65
-
66
- // ── Resolved account type ────────────────────────────────────────────
67
- // This is the object returned by resolveAccount() and consumed by
68
- // security / pairing / outbound adapters.
69
-
70
- /**
71
- * Read the AICQ channel section from OpenClaw config and return a typed
72
- * account object. This is the setup-safe resolver — no network or DB
73
- * side effects.
74
- */
75
- function resolveAccount(cfg, accountId) {
76
- const section = (cfg.channels || {})["aicq-chat"] || {};
77
- const rawAccountId = accountId || section.accountId || null;
78
-
79
- if (!rawAccountId) {
80
- throw new Error(
81
- "aicq-chat: accountId is required (set channels.aicq-chat.accountId)"
82
- );
83
- }
84
-
85
- // Resolve template variables like {{agent.id}}
86
- const resolvedAccountId = resolveTemplateVar(cfg, rawAccountId);
87
-
88
- // Resolve allowFrom entries (may contain {{agent.id}} or friend IDs)
89
- const rawAllowFrom = section.allowFrom || [];
90
- const resolvedAllowFrom = Array.isArray(rawAllowFrom)
91
- ? rawAllowFrom.map((entry) => resolveTemplateVar(cfg, entry))
92
- : rawAllowFrom;
93
-
94
- return {
95
- accountId: resolvedAccountId,
96
- serverUrl: section.serverUrl || "https://aicq.online",
97
- autoAcceptFriends: section.autoAcceptFriends ?? true,
98
- enabled: section.enabled ?? true,
99
- dmPolicy: section.dmPolicy || "allowlist",
100
- allowFrom: resolvedAllowFrom,
101
- };
102
- }
103
-
104
- /**
105
- * Lightweight account inspection for status / health / setup surfaces.
106
- * Must not materialise secrets or start transports.
107
- */
108
- function inspectAccount(cfg, accountId) {
109
- const section = (cfg.channels || {})["aicq-chat"] || {};
110
- const hasAccountId = Boolean(section.accountId || accountId);
111
- return {
112
- enabled: hasAccountId && section.enabled !== false,
113
- configured: hasAccountId,
114
- accountStatus: hasAccountId ? "available" : "missing",
115
- };
116
- }
117
-
118
- // ── Build the channel plugin ─────────────────────────────────────────
119
-
120
- const _plugin = createChatChannelPlugin({
121
- base: createChannelPluginBase({
122
- id: "aicq-chat",
123
-
124
- setup: {
125
- /**
126
- * Resolve the account ID from setup input.
127
- * Called by the setup wizard when a user configures the channel.
128
- */
129
- resolveAccountId(params) {
130
- const { cfg, accountId, input } = params;
131
- return accountId || input?.accountId || resolveTemplateVar(cfg, "{{agent.id}}");
132
- },
133
-
134
- /**
135
- * Apply the account config after the setup wizard completes.
136
- * Must return the updated OpenClawConfig.
137
- */
138
- applyAccountConfig(params) {
139
- const { cfg, accountId, input } = params;
140
- const section = (cfg.channels || {})["aicq-chat"] || {};
141
- return {
142
- ...cfg,
143
- channels: {
144
- ...(cfg.channels || {}),
145
- "aicq-chat": {
146
- ...section,
147
- accountId: accountId || input?.accountId || "{{agent.id}}",
148
- serverUrl: input?.serverUrl || section.serverUrl || "https://aicq.online",
149
- autoAcceptFriends: input?.autoAcceptFriends ?? section.autoAcceptFriends ?? true,
150
- enabled: true,
151
- dmPolicy: input?.dmPolicy || section.dmPolicy || "allowlist",
152
- allowFrom: input?.allowFrom || section.allowFrom || [],
153
- },
154
- },
155
- };
156
- },
157
-
158
- /**
159
- * Validate setup input before applying.
160
- * Return an error message string or null if valid.
161
- */
162
- validateInput(params) {
163
- return null;
164
- },
165
- },
166
-
167
- // Gateway method descriptors — these are the method names the plugin
168
- // will register via registerFull(). Declaring them here lets OpenClaw
169
- // surface them in discovery / status surfaces before full activation.
170
- gatewayMethodDescriptors: [
171
- "aicq.status",
172
- "aicq.friends.list",
173
- "aicq.friends.add",
174
- "aicq.friends.remove",
175
- "aicq.friends.requests",
176
- "aicq.friends.acceptRequest",
177
- "aicq.friends.rejectRequest",
178
- "aicq.identity.info",
179
- "aicq.agent.create",
180
- "aicq.agent.delete",
181
- "aicq.chat.send",
182
- "aicq.chat.history",
183
- "aicq.chat.delete",
184
- "aicq.chat.streamChunk",
185
- "aicq.chat.streamEnd",
186
- "aicq.chat.sendFile",
187
- "aicq.chat.sendImage",
188
- "aicq.chat.sendFileFromBase64",
189
- "aicq.groups.list",
190
- "aicq.groups.create",
191
- "aicq.groups.join",
192
- "aicq.groups.messages",
193
- "aicq.groups.silent",
194
- "aicq.sessions.list",
195
- ],
196
- }),
197
-
198
- // ── DM Security ──────────────────────────────────────────────────
199
- security: {
200
- dm: {
201
- channelKey: "aicq-chat",
202
- resolvePolicy: (account) => account.dmPolicy,
203
- resolveAllowFrom: (account) => account.allowFrom,
204
- defaultPolicy: "allowlist",
205
- },
206
- },
207
-
208
- // ── Pairing ──────────────────────────────────────────────────────
209
- pairing: {
210
- text: {
211
- idLabel: "AICQ Friend Code",
212
- message: "Share this pairing code with the other party:",
213
- notify: async ({ target, code }) => {
214
- // AICQ pairing codes are shared out-of-band by the operator.
215
- // No automatic notification is sent to the peer.
216
- },
217
- },
218
- },
219
-
220
- // ── Threading ────────────────────────────────────────────────────
221
- threading: {
222
- topLevelReplyToMode: "reply",
223
- },
224
-
225
- // ── Outbound ─────────────────────────────────────────────────────
226
- outbound: {
227
- attachedResults: {
228
- channel: "aicq-chat",
229
-
230
- sendText: async (params) => {
231
- if (!runtime.chat) {
232
- throw new Error("AICQ runtime not initialized — cannot send text");
233
- }
234
- const fromId =
235
- params.from ||
236
- params.accountId ||
237
- (runtime.identity && runtime.identity.listAgents()[0]?.agent_id);
238
- const result = await runtime.chat.sendMessage(
239
- fromId,
240
- params.to,
241
- params.text,
242
- { isGroup: false }
243
- );
244
- return { messageId: result?.message_id || result?.id || "sent" };
245
- },
246
- },
247
-
248
- base: {
249
- sendMedia: async (params) => {
250
- if (!runtime.chat) {
251
- throw new Error("AICQ runtime not initialized — cannot send media");
252
- }
253
- const fromId =
254
- params.from ||
255
- params.accountId ||
256
- (runtime.identity && runtime.identity.listAgents()[0]?.agent_id);
257
- await runtime.chat.sendMessage(
258
- fromId,
259
- params.to,
260
- params.mediaUrl || params.filePath,
261
- { type: params.mediaType || "file", isGroup: false }
262
- );
263
- },
264
- },
265
- },
266
- });
267
-
268
- // ── Gateway adapter: startAccount / stopAccount ───────────────────────
269
- // OpenClaw calls startAccount when the channel is activated (on startup
270
- // or when re-enabled). This is where we initialise the runtime, connect
271
- // to the AICQ signalling server, and wire up inbound message delivery
272
- // via the channelRuntime helpers.
273
-
274
- _plugin.gateway = {
275
- /**
276
- * Start the channel account — connect to the AICQ server and begin
277
- * listening for inbound messages.
278
- */
279
- async startAccount(ctx) {
280
- const { cfg, accountId, account, setStatus, log, abortSignal } = ctx;
281
-
282
- const logger = log || console;
283
- logger.info?.(`[AICQ Channel] startAccount called for ${accountId}`) || console.log(`[AICQ Channel] startAccount called for ${accountId}`);
284
-
285
- // Ensure the runtime (DB, identity, transport) is initialised.
286
- // The runtime is populated by registerFull() in index.js, but startAccount
287
- // may be called before any gateway method is invoked, so we must ensure
288
- // initialization here too.
289
- if (!runtime._initialized && typeof runtime.ensureInitialized === "function") {
290
- try {
291
- await runtime.ensureInitialized();
292
- logger.info?.("[AICQ Channel] Runtime initialized via startAccount") || console.log("[AICQ Channel] Runtime initialized via startAccount");
293
- } catch (e) {
294
- console.error("[AICQ Channel] Runtime initialization failed:", e.message);
295
- setStatus({
296
- accountId,
297
- enabled: true,
298
- configured: true,
299
- running: false,
300
- lastError: `Initialization failed: ${e.message}`,
301
- });
302
- return;
303
- }
304
- }
305
-
306
- // Resolve the agent ID: prefer the resolved accountId from
307
- // resolveAccount (which already handles {{agent.id}}), then
308
- // fall back to the OpenClaw default agent ID.
309
- const agents = cfg.agents?.list;
310
- const agentId = account?.accountId || accountId || OPENCLAW_DEFAULT_AGENT_ID;
311
-
312
- // Ensure we have an identity in the plugin DB
313
- if (runtime.identity) {
314
- const existing = runtime.identity.listAgents();
315
- if (existing.length === 0) {
316
- const agentName = (Array.isArray(agents) && agents.length > 0 && agents[0]?.name)
317
- ? agents[0].name
318
- : "AICQ Agent";
319
- runtime.identity.createAgent(agentId, agentName);
320
- console.log(`[AICQ Channel] Created agent identity: ${agentId}`);
321
- }
322
- }
323
-
324
- // Connect to the AICQ server
325
- if (runtime.serverClient) {
326
- try {
327
- await runtime.serverClient.ensureAuth(agentId);
328
- console.log(`[AICQ Channel] Authenticated as ${agentId}`);
329
-
330
- // Connect WebSocket for real-time messages
331
- if (typeof runtime.serverClient.start === "function") {
332
- await runtime.serverClient.start(agentId);
333
- console.log("[AICQ Channel] WebSocket connected");
334
- } else if (typeof runtime.serverClient.connectWS === "function") {
335
- runtime.serverClient.connectWS();
336
- console.log("[AICQ Channel] WebSocket connecting");
337
- }
338
-
339
- // Sync friends and groups from server
340
- if (runtime.handleGateway) {
341
- try {
342
- await runtime.handleGateway("aicq.friends.list", {});
343
- await runtime.handleGateway("aicq.groups.list", {});
344
- } catch (e) {
345
- console.warn("[AICQ Channel] Initial sync failed:", e.message);
346
- }
347
- }
348
- } catch (e) {
349
- console.error("[AICQ Channel] Failed to connect:", e.message);
350
- }
351
- }
352
-
353
- // Wire up inbound message handling
354
- // Register handlers for AICQ message types. When an inbound message arrives,
355
- // we try the OpenClaw channelRuntime first; if that fails we fall back to
356
- // calling z-ai CLI directly and sending the reply via chat.sendMessage.
357
- if (runtime.serverClient && typeof runtime.serverClient.onMessage === "function") {
358
- const inboundHandler = async (msg) => {
359
- try {
360
- // AICQ server wraps message content in msg.data for "message" type,
361
- // but "relay" type may have fields at the top level.
362
- const data = msg.data || msg;
363
- const fromId = data.from || data.fromId || data.sender_id || msg.from || msg.fromId;
364
- const isGroup = !!(data.isGroup || data.groupId || msg.isGroup || msg.groupId);
365
- const text = data.content || data.text || data.payload || msg.content || msg.text || msg.payload || "";
366
-
367
- console.log(`[AICQ Channel] Inbound message from=${fromId} isGroup=${isGroup} text=${(text || "").substring(0, 80)}`);
368
-
369
- if (!fromId || !text) {
370
- return; // Skip system messages (online_ack, presence, etc.)
371
- }
372
-
373
- // Skip our own messages
374
- if (fromId === runtime.serverClient?.serverAccountId || fromId === agentId) {
375
- return;
376
- }
377
-
378
- // Try channelRuntime (OpenClaw's built-in agent dispatch) first
379
- if (ctx.channelRuntime) {
380
- const { reply, routing } = ctx.channelRuntime;
381
- if (reply && routing) {
382
- try {
383
- const routeResult = await routing.resolveAgentRoute({
384
- channelId: "aicq-chat",
385
- accountId,
386
- fromId,
387
- chatType: isGroup ? "group" : "dm",
388
- });
389
-
390
- if (routeResult?.agentId) {
391
- await reply.dispatchReplyWithBufferedBlockDispatcher({
392
- ctx: {
393
- channelId: "aicq-chat",
394
- accountId,
395
- fromId,
396
- text,
397
- chatType: isGroup ? "group" : "dm",
398
- },
399
- cfg,
400
- dispatcherOptions: {
401
- deliver: async (payload) => {
402
- if (runtime.chat && payload.text) {
403
- await runtime.chat.sendMessage(agentId, fromId, payload.text, { isGroup });
404
- }
405
- },
406
- },
407
- });
408
- return; // Successfully dispatched via channelRuntime
409
- }
410
- } catch (e) {
411
- console.error("[AICQ Channel] channelRuntime dispatch failed, using fallback:", e.message);
412
- }
413
- }
414
- }
415
-
416
- // Fallback: call LLM via z-ai CLI and send reply directly
417
- console.log("[AICQ Channel] Using fallback: direct z-ai CLI call");
418
- try {
419
- const { execFile } = await import('child_process');
420
- const llmResult = await new Promise((resolve, reject) => {
421
- execFile('z-ai', ['chat', '--prompt', text], { timeout: 60000 }, (err, stdout) => {
422
- if (err) reject(err);
423
- else resolve(stdout);
424
- });
425
- });
426
- let replyText = '';
427
- const jsonStart = llmResult.indexOf('{');
428
- if (jsonStart >= 0) {
429
- try {
430
- const parsed = JSON.parse(llmResult.substring(jsonStart));
431
- replyText = parsed.choices?.[0]?.message?.content || llmResult.trim();
432
- } catch { replyText = llmResult.trim(); }
433
- } else { replyText = llmResult.trim(); }
434
-
435
- if (replyText && runtime.chat) {
436
- await runtime.chat.sendMessage(agentId, fromId, replyText, { isGroup });
437
- console.log(`[AICQ Channel] Fallback reply sent to ${fromId}: ${replyText.substring(0, 50)}`);
438
- }
439
- } catch (fallbackErr) {
440
- console.error("[AICQ Channel] Fallback LLM also failed:", fallbackErr.message);
441
- }
442
- } catch (e) {
443
- console.error("[AICQ Channel] Inbound message handling error:", e.message);
444
- }
445
- };
446
-
447
- // Register handler for relevant AICQ message types.
448
- // ServerClient.onMessage(type, handler) requires a type string.
449
- runtime.serverClient.onMessage("relay", inboundHandler);
450
- runtime.serverClient.onMessage("message", inboundHandler);
451
- runtime.serverClient.onMessage("group_message", inboundHandler);
452
- console.log("[AICQ Channel] Inbound message handlers registered (relay, message, group_message)");
453
-
454
- if (ctx.channelRuntime) {
455
- console.log("[AICQ Channel] channelRuntime available AI dispatch enabled (with z-ai CLI fallback)");
456
- } else {
457
- console.log("[AICQ Channel] channelRuntime not available — using z-ai CLI fallback");
458
- }
459
- }
460
-
461
- // Update health status
462
- setStatus({
463
- accountId,
464
- enabled: true,
465
- configured: true,
466
- running: true,
467
- lastStartAt: Date.now(),
468
- lastError: null,
469
- });
470
-
471
- console.log(`[AICQ Channel] Account ${accountId} started successfully`);
472
-
473
- // ── Keep startAccount alive until abort signal ──────────────────
474
- // OpenClaw expects startAccount to be a long-lived task. If it
475
- // resolves immediately, the gateway treats it as an unexpected
476
- // exit and enters a restart loop. We wait on the abort signal.
477
- await new Promise((resolve) => {
478
- if (abortSignal?.aborted) { resolve(); return; }
479
- const onAbort = () => { cleanup(); resolve(); };
480
- const cleanup = () => { abortSignal?.removeEventListener("abort", onAbort); };
481
- abortSignal?.addEventListener("abort", onAbort, { once: true });
482
- });
483
- },
484
-
485
- /**
486
- * Stop the channel account — disconnect and clean up.
487
- */
488
- async stopAccount(ctx) {
489
- const { accountId } = ctx;
490
- console.log(`[AICQ Channel] stopAccount called for ${accountId}`);
491
-
492
- if (runtime.serverClient) {
493
- try {
494
- if (typeof runtime.serverClient.stop === "function") {
495
- runtime.serverClient.stop();
496
- } else if (typeof runtime.serverClient.disconnect === "function") {
497
- runtime.serverClient.disconnect();
498
- }
499
- console.log("[AICQ Channel] WebSocket disconnected");
500
- } catch (e) {
501
- console.warn("[AICQ Channel] Disconnect error:", e.message);
502
- }
503
- }
504
- },
505
- };
506
-
507
- // ── Add config helpers (required by OpenClaw channel loader) ──────────
508
- // createChatChannelPlugin does not auto-attach config helpers,
509
- // but the OpenClaw loader requires plugin.config.listAccountIds
510
- // and plugin.config.resolveAccount for channel registration.
511
-
512
- // resolveTemplateVar is defined at the top of this file.
513
-
514
- _plugin.config = {
515
- /**
516
- * List all account IDs configured for this channel.
517
- * Resolves template variables like {{agent.id}}.
518
- *
519
- * Signature: (cfg: OpenClawConfig) => string[]
520
- */
521
- listAccountIds(cfg) {
522
- const section = (cfg.channels || {})["aicq-chat"] || {};
523
- if (section.accountId) {
524
- const resolved = resolveTemplateVar(cfg, section.accountId);
525
- return [resolved];
526
- }
527
- return [];
528
- },
529
-
530
- /**
531
- * Resolve an account from config. Reuses the setup resolver.
532
- *
533
- * Signature: (cfg: OpenClawConfig, accountId?: string | null) => ResolvedAccount
534
- */
535
- resolveAccount,
536
-
537
- /**
538
- * Lightweight account inspection.
539
- *
540
- * Signature: (cfg: OpenClawConfig, accountId?: string | null) => unknown
541
- */
542
- inspectAccount,
543
-
544
- /**
545
- * Default account ID for this channel.
546
- *
547
- * Signature: (cfg: OpenClawConfig) => string
548
- */
549
- defaultAccountId(cfg) {
550
- const section = (cfg.channels || {})["aicq-chat"] || {};
551
- if (section.accountId) {
552
- return resolveTemplateVar(cfg, section.accountId);
553
- }
554
- return OPENCLAW_DEFAULT_AGENT_ID;
555
- },
556
-
557
- /**
558
- * Check if the account is enabled.
559
- *
560
- * IMPORTANT: OpenClaw calls this with (account, cfg) where `account`
561
- * is the RESOLVED account object from resolveAccount(), not the config.
562
- *
563
- * Signature: (account: ResolvedAccount, cfg: OpenClawConfig) => boolean
564
- */
565
- isEnabled(account, cfg) {
566
- return account.enabled !== false;
567
- },
568
-
569
- /**
570
- * Check if the channel account is configured.
571
- *
572
- * IMPORTANT: OpenClaw calls this with (account, cfg) where `account`
573
- * is the RESOLVED account object from resolveAccount(), not the config.
574
- * Our old code had isConfigured(cfg) which received the account object
575
- * as `cfg`, causing it to always return false — this was the root cause
576
- * of the "not-running" bug.
577
- *
578
- * Signature: (account: ResolvedAccount, cfg: OpenClawConfig) => boolean
579
- */
580
- isConfigured(account, cfg) {
581
- return Boolean(account && account.accountId);
582
- },
583
-
584
- /**
585
- * Return the reason the channel is not configured.
586
- *
587
- * Signature: (account: ResolvedAccount, cfg: OpenClawConfig) => string
588
- */
589
- unconfiguredReason(account, cfg) {
590
- if (!account || !account.accountId) {
591
- return "accountId is required — set channels.aicq-chat.accountId in openclaw.json";
592
- }
593
- return null;
594
- },
595
-
596
- /**
597
- * Describe the account for status surfaces.
598
- *
599
- * IMPORTANT: OpenClaw calls this with (account, cfg) where `account`
600
- * is the RESOLVED account object, not the config.
601
- *
602
- * Signature: (account: ResolvedAccount, cfg: OpenClawConfig) => ChannelAccountSnapshot
603
- */
604
- describeAccount(account, cfg) {
605
- return {
606
- accountId: account?.accountId || null,
607
- label: "AICQ Encrypted Chat",
608
- enabled: account?.enabled !== false,
609
- };
610
- },
611
- };
612
-
613
- export const aicqChatPlugin = _plugin;
1
+ /**
2
+ * AICQ Channel Plugin — Core Channel Logic
3
+ *
4
+ * Uses the official OpenClaw Channel Plugin SDK:
5
+ * createChatChannelPlugin + createChannelPluginBase
6
+ *
7
+ * Architecture: In-process Channel (no sidecar, no independent port)
8
+ *
9
+ * The runtime store is a mutable object populated by registerFull() in
10
+ * index.js. This keeps the channel-plugin object safe to import during
11
+ * setup-only / discovery modes without pulling in transport clients or
12
+ * database handles.
13
+ */
14
+
15
+ import {
16
+ createChatChannelPlugin,
17
+ createChannelPluginBase,
18
+ } from "openclaw/plugin-sdk/channel-core";
19
+
20
+ // ── Mutable runtime store ────────────────────────────────────────────
21
+ // Populated lazily by the registerFull() callback in index.js.
22
+ // Adapters that need runtime state check these before acting.
23
+ export const runtime = {
24
+ db: null,
25
+ identity: null,
26
+ serverClient: null,
27
+ handshake: null,
28
+ chat: null,
29
+ dataDir: null,
30
+ serverUrl: null,
31
+ handleGateway: null,
32
+ _initialized: false,
33
+ };
34
+
35
+ // ── Template variable resolver ───────────────────────────────────────
36
+ // OpenClaw stores accountId as-is (e.g. "{{agent.id}}") in config.
37
+ // Plugins must resolve template variables at runtime.
38
+ //
39
+ // The default agent ID in OpenClaw is "main" (DEFAULT_AGENT_ID).
40
+ // When cfg.agents.list is empty/undefined (no explicit agent config),
41
+ // the implicit default agent "main" is used.
42
+
43
+ const OPENCLAW_DEFAULT_AGENT_ID = "main";
44
+
45
+ function resolveTemplateVar(cfg, value) {
46
+ if (typeof value !== "string") return value;
47
+ const match = value.match(/^\{\{(\w[\w.]*)\}\}$/);
48
+ if (!match) return value;
49
+
50
+ const tmplPath = match[1]; // e.g. "agent.id"
51
+ if (tmplPath === "agent.id") {
52
+ // Strategy: look for explicit agents in config first
53
+ const agents = cfg.agents?.list;
54
+ if (Array.isArray(agents) && agents.length > 0) {
55
+ // Use the default=true agent, or the first one
56
+ const defaultAgent = agents.find((a) => a.default) || agents[0];
57
+ if (defaultAgent?.id) return defaultAgent.id;
58
+ }
59
+ // Fallback: OpenClaw's implicit default agent ID
60
+ return OPENCLAW_DEFAULT_AGENT_ID;
61
+ }
62
+
63
+ return value; // unknown template — return as-is
64
+ }
65
+
66
+ // ── Resolved account type ────────────────────────────────────────────
67
+ // This is the object returned by resolveAccount() and consumed by
68
+ // security / pairing / outbound adapters.
69
+
70
+ /**
71
+ * Read the AICQ channel section from OpenClaw config and return a typed
72
+ * account object. This is the setup-safe resolver — no network or DB
73
+ * side effects.
74
+ */
75
+ function resolveAccount(cfg, accountId) {
76
+ const section = (cfg.channels || {})["aicq-chat"] || {};
77
+ const rawAccountId = accountId || section.accountId || null;
78
+
79
+ if (!rawAccountId) {
80
+ throw new Error(
81
+ "aicq-chat: accountId is required (set channels.aicq-chat.accountId)"
82
+ );
83
+ }
84
+
85
+ // Resolve template variables like {{agent.id}}
86
+ const resolvedAccountId = resolveTemplateVar(cfg, rawAccountId);
87
+
88
+ // Resolve allowFrom entries (may contain {{agent.id}} or friend IDs)
89
+ const rawAllowFrom = section.allowFrom || [];
90
+ const resolvedAllowFrom = Array.isArray(rawAllowFrom)
91
+ ? rawAllowFrom.map((entry) => resolveTemplateVar(cfg, entry))
92
+ : rawAllowFrom;
93
+
94
+ return {
95
+ accountId: resolvedAccountId,
96
+ serverUrl: section.serverUrl || "https://aicq.online",
97
+ autoAcceptFriends: section.autoAcceptFriends ?? true,
98
+ autoAddFriends: section.autoAddFriends || [],
99
+ enabled: section.enabled ?? true,
100
+ dmPolicy: section.dmPolicy || "allowlist",
101
+ allowFrom: resolvedAllowFrom,
102
+ };
103
+ }
104
+
105
+ /**
106
+ * Lightweight account inspection for status / health / setup surfaces.
107
+ * Must not materialise secrets or start transports.
108
+ */
109
+ function inspectAccount(cfg, accountId) {
110
+ const section = (cfg.channels || {})["aicq-chat"] || {};
111
+ const hasAccountId = Boolean(section.accountId || accountId);
112
+ return {
113
+ enabled: hasAccountId && section.enabled !== false,
114
+ configured: hasAccountId,
115
+ accountStatus: hasAccountId ? "available" : "missing",
116
+ };
117
+ }
118
+
119
+ // ── Build the channel plugin ─────────────────────────────────────────
120
+
121
+ const _plugin = createChatChannelPlugin({
122
+ base: createChannelPluginBase({
123
+ id: "aicq-chat",
124
+
125
+ setup: {
126
+ /**
127
+ * Resolve the account ID from setup input.
128
+ * Called by the setup wizard when a user configures the channel.
129
+ */
130
+ resolveAccountId(params) {
131
+ const { cfg, accountId, input } = params;
132
+ return accountId || input?.accountId || resolveTemplateVar(cfg, "{{agent.id}}");
133
+ },
134
+
135
+ /**
136
+ * Apply the account config after the setup wizard completes.
137
+ * Must return the updated OpenClawConfig.
138
+ */
139
+ applyAccountConfig(params) {
140
+ const { cfg, accountId, input } = params;
141
+ const section = (cfg.channels || {})["aicq-chat"] || {};
142
+ return {
143
+ ...cfg,
144
+ channels: {
145
+ ...(cfg.channels || {}),
146
+ "aicq-chat": {
147
+ ...section,
148
+ accountId: accountId || input?.accountId || "{{agent.id}}",
149
+ serverUrl: input?.serverUrl || section.serverUrl || "https://aicq.online",
150
+ autoAcceptFriends: input?.autoAcceptFriends ?? section.autoAcceptFriends ?? true,
151
+ enabled: true,
152
+ dmPolicy: input?.dmPolicy || section.dmPolicy || "allowlist",
153
+ allowFrom: input?.allowFrom || section.allowFrom || [],
154
+ },
155
+ },
156
+ };
157
+ },
158
+
159
+ /**
160
+ * Validate setup input before applying.
161
+ * Return an error message string or null if valid.
162
+ */
163
+ validateInput(params) {
164
+ return null;
165
+ },
166
+ },
167
+
168
+ // Gateway method descriptors these are the method names the plugin
169
+ // will register via registerFull(). Declaring them here lets OpenClaw
170
+ // surface them in discovery / status surfaces before full activation.
171
+ gatewayMethodDescriptors: [
172
+ "aicq.status",
173
+ "aicq.friends.list",
174
+ "aicq.friends.add",
175
+ "aicq.friends.addByNumber",
176
+ "aicq.friends.remove",
177
+ "aicq.friends.requests",
178
+ "aicq.friends.acceptRequest",
179
+ "aicq.friends.rejectRequest",
180
+ "aicq.identity.info",
181
+ "aicq.agent.create",
182
+ "aicq.agent.delete",
183
+ "aicq.chat.send",
184
+ "aicq.chat.history",
185
+ "aicq.chat.delete",
186
+ "aicq.chat.userUpload",
187
+ "aicq.chat.userfiles",
188
+ "aicq.chat.streamChunk",
189
+ "aicq.chat.streamEnd",
190
+ "aicq.groups.list",
191
+ "aicq.groups.create",
192
+ "aicq.groups.join",
193
+ "aicq.groups.messages",
194
+ "aicq.groups.silent",
195
+ "aicq.sessions.list",
196
+ ],
197
+ }),
198
+
199
+ // ── DM Security ──────────────────────────────────────────────────
200
+ security: {
201
+ dm: {
202
+ channelKey: "aicq-chat",
203
+ resolvePolicy: (account) => account.dmPolicy,
204
+ resolveAllowFrom: (account) => account.allowFrom,
205
+ defaultPolicy: "allowlist",
206
+ },
207
+ },
208
+
209
+ // ── Pairing ──────────────────────────────────────────────────────
210
+ pairing: {
211
+ text: {
212
+ idLabel: "AICQ Friend Code",
213
+ message: "Share this pairing code with the other party:",
214
+ notify: async ({ target, code }) => {
215
+ // AICQ pairing codes are shared out-of-band by the operator.
216
+ // No automatic notification is sent to the peer.
217
+ },
218
+ },
219
+ },
220
+
221
+ // ── Threading ────────────────────────────────────────────────────
222
+ threading: {
223
+ topLevelReplyToMode: "reply",
224
+ },
225
+
226
+ // ── Outbound ─────────────────────────────────────────────────────
227
+ outbound: {
228
+ attachedResults: {
229
+ channel: "aicq-chat",
230
+
231
+ sendText: async (params) => {
232
+ if (!runtime.chat) {
233
+ throw new Error("AICQ runtime not initialized — cannot send text");
234
+ }
235
+ const fromId =
236
+ params.from ||
237
+ params.accountId ||
238
+ (runtime.identity && runtime.identity.listAgents()[0]?.agent_id);
239
+ const result = await runtime.chat.sendMessage(
240
+ fromId,
241
+ params.to,
242
+ params.text,
243
+ { isGroup: false }
244
+ );
245
+ return { messageId: result?.message_id || result?.id || "sent" };
246
+ },
247
+ },
248
+
249
+ base: {
250
+ sendMedia: async (params) => {
251
+ if (!runtime.chat) {
252
+ throw new Error("AICQ runtime not initialized — cannot send media");
253
+ }
254
+ const fromId =
255
+ params.from ||
256
+ params.accountId ||
257
+ (runtime.identity && runtime.identity.listAgents()[0]?.agent_id);
258
+ await runtime.chat.sendMessage(
259
+ fromId,
260
+ params.to,
261
+ params.mediaUrl || params.filePath,
262
+ { type: params.mediaType || "file", isGroup: false }
263
+ );
264
+ },
265
+ },
266
+ },
267
+ });
268
+
269
+ // ── Gateway adapter: startAccount / stopAccount ───────────────────────
270
+ // OpenClaw calls startAccount when the channel is activated (on startup
271
+ // or when re-enabled). This is where we initialise the runtime, connect
272
+ // to the AICQ signalling server, and wire up inbound message delivery
273
+ // via the channelRuntime helpers.
274
+
275
+ _plugin.gateway = {
276
+ /**
277
+ * Start the channel account — connect to the AICQ server and begin
278
+ * listening for inbound messages.
279
+ */
280
+ async startAccount(ctx) {
281
+ const { cfg, accountId, account, setStatus, log, abortSignal } = ctx;
282
+
283
+ const logger = log || console;
284
+ logger.info?.(`[AICQ Channel] startAccount called for ${accountId}`) || console.log(`[AICQ Channel] startAccount called for ${accountId}`);
285
+
286
+ // Ensure the runtime (DB, identity, transport) is initialised.
287
+ // The runtime is populated by registerFull() in index.js, but startAccount
288
+ // may be called before any gateway method is invoked, so we must ensure
289
+ // initialization here too.
290
+ if (!runtime._initialized && typeof runtime.ensureInitialized === "function") {
291
+ try {
292
+ await runtime.ensureInitialized();
293
+ logger.info?.("[AICQ Channel] Runtime initialized via startAccount") || console.log("[AICQ Channel] Runtime initialized via startAccount");
294
+ } catch (e) {
295
+ console.error("[AICQ Channel] Runtime initialization failed:", e.message);
296
+ setStatus({
297
+ accountId,
298
+ enabled: true,
299
+ configured: true,
300
+ running: false,
301
+ lastError: `Initialization failed: ${e.message}`,
302
+ });
303
+ return;
304
+ }
305
+ }
306
+
307
+ // Resolve the agent ID: prefer the resolved accountId from
308
+ // resolveAccount (which already handles {{agent.id}}), then
309
+ // fall back to the OpenClaw default agent ID.
310
+ const agents = cfg.agents?.list;
311
+ const agentId = account?.accountId || accountId || OPENCLAW_DEFAULT_AGENT_ID;
312
+
313
+ // Ensure we have an identity in the plugin DB
314
+ if (runtime.identity) {
315
+ const existing = runtime.identity.listAgents();
316
+ if (existing.length === 0) {
317
+ const agentName = (Array.isArray(agents) && agents.length > 0 && agents[0]?.name)
318
+ ? agents[0].name
319
+ : "AICQ Agent";
320
+ runtime.identity.createAgent(agentId, agentName);
321
+ console.log(`[AICQ Channel] Created agent identity: ${agentId}`);
322
+ }
323
+ }
324
+
325
+ // Connect to the AICQ server
326
+ if (runtime.serverClient) {
327
+ try {
328
+ await runtime.serverClient.ensureAuth(agentId);
329
+ console.log(`[AICQ Channel] Authenticated as ${agentId}`);
330
+
331
+ // Connect WebSocket for real-time messages
332
+ if (typeof runtime.serverClient.start === "function") {
333
+ await runtime.serverClient.start(agentId);
334
+ console.log("[AICQ Channel] WebSocket connected");
335
+ } else if (typeof runtime.serverClient.connectWS === "function") {
336
+ runtime.serverClient.connectWS();
337
+ console.log("[AICQ Channel] WebSocket connecting");
338
+ }
339
+
340
+ // Sync friends and groups from server
341
+ if (runtime.handleGateway) {
342
+ try {
343
+ await runtime.handleGateway("aicq.friends.list", {});
344
+ await runtime.handleGateway("aicq.groups.list", {});
345
+ } catch (e) {
346
+ console.warn("[AICQ Channel] Initial sync failed:", e.message);
347
+ }
348
+ }
349
+
350
+ // Auto-add friends from config (autoAddFriends list)
351
+ const autoAddFriends = account?.autoAddFriends || section?.autoAddFriends || [];
352
+ if (Array.isArray(autoAddFriends) && autoAddFriends.length > 0) {
353
+ console.log(`[AICQ Channel] Auto-adding ${autoAddFriends.length} friend(s) from config...`);
354
+ for (const friendEntry of autoAddFriends) {
355
+ try {
356
+ const aicqNumber = typeof friendEntry === 'string' ? friendEntry : friendEntry.number;
357
+ const friendMsg = typeof friendEntry === 'object' ? friendEntry.message : undefined;
358
+ if (!aicqNumber) continue;
359
+ const result = await runtime.handleGateway("aicq.friends.addByNumber", {
360
+ number: aicqNumber,
361
+ message: friendMsg || 'Hi, I\'d like to add you!',
362
+ });
363
+ if (result.error) {
364
+ console.warn(`[AICQ Channel] Auto-add friend ${aicqNumber} failed: ${result.error}`);
365
+ } else {
366
+ console.log(`[AICQ Channel] Auto-add friend ${aicqNumber}: ${result.status}`);
367
+ }
368
+ } catch (e) {
369
+ console.warn(`[AICQ Channel] Auto-add friend failed:`, e.message);
370
+ }
371
+ }
372
+ }
373
+
374
+ // Auto-accept pending friend requests if autoAcceptFriends is true
375
+ if (account?.autoAcceptFriends && runtime.handleGateway) {
376
+ try {
377
+ const pendingResult = await runtime.handleGateway("aicq.friends.requests", {});
378
+ if (pendingResult.requests && pendingResult.requests.length > 0) {
379
+ for (const req of pendingResult.requests) {
380
+ try {
381
+ await runtime.handleGateway("aicq.friends.acceptRequest", { request_id: req.session_id });
382
+ console.log(`[AICQ Channel] Auto-accepted friend request from ${req.requester_id}`);
383
+ } catch (e) {
384
+ console.warn(`[AICQ Channel] Auto-accept failed:`, e.message);
385
+ }
386
+ }
387
+ }
388
+ } catch (e) {
389
+ console.warn("[AICQ Channel] Auto-accept check failed:", e.message);
390
+ }
391
+ }
392
+ } catch (e) {
393
+ console.error("[AICQ Channel] Failed to connect:", e.message);
394
+ }
395
+ }
396
+
397
+ // Wire up inbound message handling via channelRuntime if available
398
+ if (ctx.channelRuntime) {
399
+ const { reply, routing } = ctx.channelRuntime;
400
+ if (reply && routing) {
401
+ console.log("[AICQ Channel] channelRuntime available — AI dispatch enabled");
402
+
403
+ // Set up the onNewMessage callback for the ChatManager
404
+ // This handles both regular text messages and synthetic file notifications
405
+ if (runtime.chat) {
406
+ runtime.chat.setOnNewMessage(async (msg) => {
407
+ try {
408
+ // Skip stream and presence events — not user messages
409
+ if (msg.type === 'stream_chunk' || msg.type === 'stream_end') return;
410
+
411
+ const resolvedAgentId = agentId;
412
+ const fromId = msg.from_id || msg.from || msg.sender_id;
413
+ const isGroup = !!(msg.is_group || msg.isGroup);
414
+ const isFileMsg = !!(msg.local_path || msg._synthetic);
415
+ let textContent = msg.content || msg.text || "";
416
+
417
+ // For file messages, include the local path info in the dispatch text
418
+ if (isFileMsg && msg.local_path) {
419
+ // The content already includes file info (from the synthetic message
420
+ // generated in chat.js), so we just pass it through to the AI
421
+ }
422
+
423
+ const routeResult = await routing.resolveAgentRoute({
424
+ channelId: "aicq-chat",
425
+ accountId,
426
+ fromId,
427
+ chatType: isGroup ? "group" : "dm",
428
+ });
429
+
430
+ if (routeResult?.agentId) {
431
+ await reply.dispatchReplyWithBufferedBlockDispatcher({
432
+ ctx: {
433
+ channelId: "aicq-chat",
434
+ accountId,
435
+ fromId,
436
+ text: textContent,
437
+ chatType: isGroup ? "group" : "dm",
438
+ },
439
+ cfg,
440
+ dispatcherOptions: {
441
+ deliver: async (payload) => {
442
+ if (runtime.chat && payload.text) {
443
+ await runtime.chat.sendMessage(
444
+ resolvedAgentId,
445
+ fromId,
446
+ payload.text,
447
+ { isGroup }
448
+ );
449
+ }
450
+ },
451
+ },
452
+ });
453
+ }
454
+ } catch (e) {
455
+ console.error("[AICQ Channel] Inbound message handling error:", e.message);
456
+ }
457
+ });
458
+ }
459
+ }
460
+ } else {
461
+ console.log("[AICQ Channel] channelRuntime not available — running in standalone mode");
462
+ }
463
+
464
+ // Update health status
465
+ setStatus({
466
+ accountId,
467
+ enabled: true,
468
+ configured: true,
469
+ running: true,
470
+ lastStartAt: Date.now(),
471
+ lastError: null,
472
+ });
473
+
474
+ console.log(`[AICQ Channel] Account ${accountId} started successfully`);
475
+
476
+ // ── Keep startAccount alive until abort signal ──────────────────
477
+ // OpenClaw expects startAccount to be a long-lived task. If it
478
+ // resolves immediately, the gateway treats it as an unexpected
479
+ // exit and enters a restart loop. We wait on the abort signal.
480
+ await new Promise((resolve) => {
481
+ if (abortSignal?.aborted) { resolve(); return; }
482
+ const onAbort = () => { cleanup(); resolve(); };
483
+ const cleanup = () => { abortSignal?.removeEventListener("abort", onAbort); };
484
+ abortSignal?.addEventListener("abort", onAbort, { once: true });
485
+ });
486
+ },
487
+
488
+ /**
489
+ * Stop the channel account — disconnect and clean up.
490
+ */
491
+ async stopAccount(ctx) {
492
+ const { accountId } = ctx;
493
+ console.log(`[AICQ Channel] stopAccount called for ${accountId}`);
494
+
495
+ if (runtime.serverClient) {
496
+ try {
497
+ if (typeof runtime.serverClient.stop === "function") {
498
+ runtime.serverClient.stop();
499
+ } else if (typeof runtime.serverClient.disconnect === "function") {
500
+ runtime.serverClient.disconnect();
501
+ }
502
+ console.log("[AICQ Channel] WebSocket disconnected");
503
+ } catch (e) {
504
+ console.warn("[AICQ Channel] Disconnect error:", e.message);
505
+ }
506
+ }
507
+ },
508
+ };
509
+
510
+ // ── Add config helpers (required by OpenClaw channel loader) ──────────
511
+ // createChatChannelPlugin does not auto-attach config helpers,
512
+ // but the OpenClaw loader requires plugin.config.listAccountIds
513
+ // and plugin.config.resolveAccount for channel registration.
514
+
515
+ // resolveTemplateVar is defined at the top of this file.
516
+
517
+ _plugin.config = {
518
+ /**
519
+ * List all account IDs configured for this channel.
520
+ * Resolves template variables like {{agent.id}}.
521
+ *
522
+ * Signature: (cfg: OpenClawConfig) => string[]
523
+ */
524
+ listAccountIds(cfg) {
525
+ const section = (cfg.channels || {})["aicq-chat"] || {};
526
+ if (section.accountId) {
527
+ const resolved = resolveTemplateVar(cfg, section.accountId);
528
+ return [resolved];
529
+ }
530
+ return [];
531
+ },
532
+
533
+ /**
534
+ * Resolve an account from config. Reuses the setup resolver.
535
+ *
536
+ * Signature: (cfg: OpenClawConfig, accountId?: string | null) => ResolvedAccount
537
+ */
538
+ resolveAccount,
539
+
540
+ /**
541
+ * Lightweight account inspection.
542
+ *
543
+ * Signature: (cfg: OpenClawConfig, accountId?: string | null) => unknown
544
+ */
545
+ inspectAccount,
546
+
547
+ /**
548
+ * Default account ID for this channel.
549
+ *
550
+ * Signature: (cfg: OpenClawConfig) => string
551
+ */
552
+ defaultAccountId(cfg) {
553
+ const section = (cfg.channels || {})["aicq-chat"] || {};
554
+ if (section.accountId) {
555
+ return resolveTemplateVar(cfg, section.accountId);
556
+ }
557
+ return OPENCLAW_DEFAULT_AGENT_ID;
558
+ },
559
+
560
+ /**
561
+ * Check if the account is enabled.
562
+ *
563
+ * IMPORTANT: OpenClaw calls this with (account, cfg) where `account`
564
+ * is the RESOLVED account object from resolveAccount(), not the config.
565
+ *
566
+ * Signature: (account: ResolvedAccount, cfg: OpenClawConfig) => boolean
567
+ */
568
+ isEnabled(account, cfg) {
569
+ return account.enabled !== false;
570
+ },
571
+
572
+ /**
573
+ * Check if the channel account is configured.
574
+ *
575
+ * IMPORTANT: OpenClaw calls this with (account, cfg) where `account`
576
+ * is the RESOLVED account object from resolveAccount(), not the config.
577
+ * Our old code had isConfigured(cfg) which received the account object
578
+ * as `cfg`, causing it to always return false — this was the root cause
579
+ * of the "not-running" bug.
580
+ *
581
+ * Signature: (account: ResolvedAccount, cfg: OpenClawConfig) => boolean
582
+ */
583
+ isConfigured(account, cfg) {
584
+ return Boolean(account && account.accountId);
585
+ },
586
+
587
+ /**
588
+ * Return the reason the channel is not configured.
589
+ *
590
+ * Signature: (account: ResolvedAccount, cfg: OpenClawConfig) => string
591
+ */
592
+ unconfiguredReason(account, cfg) {
593
+ if (!account || !account.accountId) {
594
+ return "accountId is required — set channels.aicq-chat.accountId in openclaw.json";
595
+ }
596
+ return null;
597
+ },
598
+
599
+ /**
600
+ * Describe the account for status surfaces.
601
+ *
602
+ * IMPORTANT: OpenClaw calls this with (account, cfg) where `account`
603
+ * is the RESOLVED account object, not the config.
604
+ *
605
+ * Signature: (account: ResolvedAccount, cfg: OpenClawConfig) => ChannelAccountSnapshot
606
+ */
607
+ describeAccount(account, cfg) {
608
+ return {
609
+ accountId: account?.accountId || null,
610
+ label: "AICQ Encrypted Chat",
611
+ enabled: account?.enabled !== false,
612
+ };
613
+ },
614
+ };
615
+
616
+ export const aicqChatPlugin = _plugin;