@chbo297/infoflow 2026.3.4 → 2026.3.7

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/actions.ts CHANGED
@@ -8,16 +8,292 @@ import type { ChannelMessageActionAdapter, ChannelMessageActionName } from "open
8
8
  import { extractToolSend, jsonResult, readStringParam } from "openclaw/plugin-sdk";
9
9
  import { resolveInfoflowAccount } from "./accounts.js";
10
10
  import { logVerbose } from "./logging.js";
11
- import { sendInfoflowMessage } from "./send.js";
11
+ import { prepareInfoflowImageBase64, sendInfoflowImageMessage } from "./media.js";
12
+ import {
13
+ sendInfoflowMessage,
14
+ recallInfoflowGroupMessage,
15
+ recallInfoflowPrivateMessage,
16
+ } from "./send.js";
17
+ import {
18
+ findSentMessage,
19
+ querySentMessages,
20
+ removeRecalledMessages,
21
+ } from "./sent-message-store.js";
12
22
  import { normalizeInfoflowTarget } from "./targets.js";
13
- import type { InfoflowMessageContentItem } from "./types.js";
23
+ import type { InfoflowMessageContentItem, InfoflowOutboundReply } from "./types.js";
24
+
25
+ // Recall result hint constants — reused across single/batch, group/private recall paths
26
+ const RECALL_OK_HINT = "Recall succeeded. output only NO_REPLY with no other text.";
27
+ const RECALL_FAIL_HINT = "Recall failed. Send a brief reply stating only the failure reason.";
28
+ const RECALL_PARTIAL_HINT =
29
+ "Some recalls failed. Send a brief reply stating only the failure reason(s).";
14
30
 
15
31
  export const infoflowMessageActions: ChannelMessageActionAdapter = {
16
- listActions: (): ChannelMessageActionName[] => ["send"],
32
+ listActions: (): ChannelMessageActionName[] => ["send", "delete"],
17
33
 
18
34
  extractToolSend: ({ args }) => extractToolSend(args, "sendMessage"),
19
35
 
20
36
  handleAction: async ({ action, params, cfg, accountId }) => {
37
+ // -----------------------------------------------------------------------
38
+ // delete (群消息撤回) — Mode A: by messageId, Mode B: by count
39
+ // -----------------------------------------------------------------------
40
+ if (action === "delete") {
41
+ const rawTo = readStringParam(params, "to", { required: true });
42
+ if (!rawTo) {
43
+ throw new Error("delete requires a target (to).");
44
+ }
45
+ const to = normalizeInfoflowTarget(rawTo) ?? rawTo;
46
+ const target = to.replace(/^infoflow:/i, "");
47
+
48
+ const account = resolveInfoflowAccount({ cfg, accountId: accountId ?? undefined });
49
+ if (!account.config.appKey || !account.config.appSecret) {
50
+ throw new Error("Infoflow appKey/appSecret not configured.");
51
+ }
52
+
53
+ const messageId = readStringParam(params, "messageId");
54
+ // Default to count=1 (recall latest message) when neither messageId nor count is provided
55
+ const countStr = readStringParam(params, "count") ?? (messageId ? undefined : "1");
56
+
57
+ const groupMatch = target.match(/^group:(\d+)/i);
58
+
59
+ if (groupMatch) {
60
+ // -----------------------------------------------------------------
61
+ // 群消息撤回
62
+ // -----------------------------------------------------------------
63
+ const groupId = Number(groupMatch[1]);
64
+
65
+ // Mode A: single message recall by messageId
66
+ if (messageId) {
67
+ let msgseqid = readStringParam(params, "msgseqid") ?? "";
68
+ if (!msgseqid) {
69
+ const stored = findSentMessage(account.accountId, messageId);
70
+ if (stored?.msgseqid) {
71
+ msgseqid = stored.msgseqid;
72
+ }
73
+ }
74
+ if (!msgseqid) {
75
+ throw new Error(
76
+ "delete requires msgseqid (not found in store; provide it explicitly or send messages first).",
77
+ );
78
+ }
79
+
80
+ const result = await recallInfoflowGroupMessage({
81
+ account,
82
+ groupId,
83
+ messageid: messageId,
84
+ msgseqid,
85
+ });
86
+
87
+ if (result.ok) {
88
+ try {
89
+ removeRecalledMessages(account.accountId, [messageId]);
90
+ } catch {
91
+ // ignore cleanup errors
92
+ }
93
+ }
94
+
95
+ return jsonResult({
96
+ ok: result.ok,
97
+ channel: "infoflow",
98
+ to,
99
+ ...(result.error ? { error: result.error } : {}),
100
+ _hint: result.ok ? RECALL_OK_HINT : RECALL_FAIL_HINT,
101
+ });
102
+ }
103
+
104
+ // Mode B: batch recall by count
105
+ if (countStr) {
106
+ const count = Number(countStr);
107
+ if (!Number.isFinite(count) || count < 1) {
108
+ throw new Error("count must be a positive integer.");
109
+ }
110
+
111
+ const records = querySentMessages(account.accountId, {
112
+ target: `group:${groupId}`,
113
+ count,
114
+ });
115
+ // Filter to records that have msgseqid (required for group recall)
116
+ const recallable = records.filter((r) => r.msgseqid);
117
+
118
+ if (recallable.length === 0) {
119
+ return jsonResult({
120
+ ok: true,
121
+ channel: "infoflow",
122
+ to,
123
+ recalled: 0,
124
+ message: "No recallable messages found in store.",
125
+ _hint: "No messages found to recall. Briefly inform the user.",
126
+ });
127
+ }
128
+
129
+ let succeeded = 0;
130
+ let failed = 0;
131
+ const recalledIds: string[] = [];
132
+ const details: Array<{
133
+ messageid: string;
134
+ digest: string;
135
+ ok: boolean;
136
+ error?: string;
137
+ }> = [];
138
+
139
+ for (const record of recallable) {
140
+ const result = await recallInfoflowGroupMessage({
141
+ account,
142
+ groupId,
143
+ messageid: record.messageid,
144
+ msgseqid: record.msgseqid,
145
+ });
146
+
147
+ if (result.ok) {
148
+ succeeded++;
149
+ recalledIds.push(record.messageid);
150
+ details.push({ messageid: record.messageid, digest: record.digest, ok: true });
151
+ } else {
152
+ failed++;
153
+ details.push({
154
+ messageid: record.messageid,
155
+ digest: record.digest,
156
+ ok: false,
157
+ error: result.error,
158
+ });
159
+ }
160
+ }
161
+
162
+ if (recalledIds.length > 0) {
163
+ try {
164
+ removeRecalledMessages(account.accountId, recalledIds);
165
+ } catch {
166
+ // ignore cleanup errors
167
+ }
168
+ }
169
+
170
+ return jsonResult({
171
+ ok: failed === 0,
172
+ channel: "infoflow",
173
+ to,
174
+ recalled: succeeded,
175
+ failed,
176
+ total: recallable.length,
177
+ details,
178
+ _hint: failed === 0 ? RECALL_OK_HINT : RECALL_PARTIAL_HINT,
179
+ });
180
+ }
181
+ } else {
182
+ // -----------------------------------------------------------------
183
+ // 私聊消息撤回
184
+ // -----------------------------------------------------------------
185
+ const appAgentId = account.config.appAgentId;
186
+ if (!appAgentId) {
187
+ throw new Error(
188
+ "Infoflow private message recall requires appAgentId configuration. " +
189
+ "Set channels.infoflow.appAgentId to your application ID (如流企业后台的应用ID).",
190
+ );
191
+ }
192
+
193
+ // Mode A: single message recall by messageId (msgkey)
194
+ if (messageId) {
195
+ const result = await recallInfoflowPrivateMessage({
196
+ account,
197
+ msgkey: messageId,
198
+ appAgentId,
199
+ });
200
+
201
+ if (result.ok) {
202
+ try {
203
+ removeRecalledMessages(account.accountId, [messageId]);
204
+ } catch {
205
+ // ignore cleanup errors
206
+ }
207
+ }
208
+
209
+ return jsonResult({
210
+ ok: result.ok,
211
+ channel: "infoflow",
212
+ to,
213
+ ...(result.error ? { error: result.error } : {}),
214
+ _hint: result.ok ? RECALL_OK_HINT : RECALL_FAIL_HINT,
215
+ });
216
+ }
217
+
218
+ // Mode B: batch recall by count
219
+ if (countStr) {
220
+ const count = Number(countStr);
221
+ if (!Number.isFinite(count) || count < 1) {
222
+ throw new Error("count must be a positive integer.");
223
+ }
224
+
225
+ const records = querySentMessages(account.accountId, { target, count });
226
+ // 私聊消息的 msgseqid 为空,只需要有 messageid (即 msgkey) 即可撤回
227
+ const recallable = records.filter((r) => r.messageid);
228
+
229
+ if (recallable.length === 0) {
230
+ return jsonResult({
231
+ ok: true,
232
+ channel: "infoflow",
233
+ to,
234
+ recalled: 0,
235
+ message: "No recallable messages found in store.",
236
+ _hint: "No messages found to recall. Briefly inform the user.",
237
+ });
238
+ }
239
+
240
+ let succeeded = 0;
241
+ let failed = 0;
242
+ const recalledIds: string[] = [];
243
+ const details: Array<{
244
+ messageid: string;
245
+ digest: string;
246
+ ok: boolean;
247
+ error?: string;
248
+ }> = [];
249
+
250
+ for (const record of recallable) {
251
+ const result = await recallInfoflowPrivateMessage({
252
+ account,
253
+ msgkey: record.messageid,
254
+ appAgentId,
255
+ });
256
+
257
+ if (result.ok) {
258
+ succeeded++;
259
+ recalledIds.push(record.messageid);
260
+ details.push({ messageid: record.messageid, digest: record.digest, ok: true });
261
+ } else {
262
+ failed++;
263
+ details.push({
264
+ messageid: record.messageid,
265
+ digest: record.digest,
266
+ ok: false,
267
+ error: result.error,
268
+ });
269
+ }
270
+ }
271
+
272
+ if (recalledIds.length > 0) {
273
+ try {
274
+ removeRecalledMessages(account.accountId, recalledIds);
275
+ } catch {
276
+ // ignore cleanup errors
277
+ }
278
+ }
279
+
280
+ return jsonResult({
281
+ ok: failed === 0,
282
+ channel: "infoflow",
283
+ to,
284
+ recalled: succeeded,
285
+ failed,
286
+ total: recallable.length,
287
+ details,
288
+ _hint: failed === 0 ? RECALL_OK_HINT : RECALL_PARTIAL_HINT,
289
+ });
290
+ }
291
+ }
292
+ }
293
+
294
+ // -----------------------------------------------------------------------
295
+ // send
296
+ // -----------------------------------------------------------------------
21
297
  if (action !== "send") {
22
298
  throw new Error(`Action "${action}" is not supported for Infoflow.`);
23
299
  }
@@ -42,6 +318,19 @@ export const infoflowMessageActions: ChannelMessageActionAdapter = {
42
318
  const isGroup = /^group:\d+$/i.test(to);
43
319
  const contents: InfoflowMessageContentItem[] = [];
44
320
 
321
+ // Infoflow reply-to params (group only)
322
+ const replyToMessageId = readStringParam(params, "replyToMessageId");
323
+ const replyToPreview = readStringParam(params, "replyToPreview");
324
+ const replyTypeRaw = readStringParam(params, "replyType");
325
+ const replyTo: InfoflowOutboundReply | undefined =
326
+ replyToMessageId && isGroup
327
+ ? {
328
+ messageid: replyToMessageId,
329
+ preview: replyToPreview ?? undefined,
330
+ replytype: replyTypeRaw === "2" ? "2" : "1",
331
+ }
332
+ : undefined;
333
+
45
334
  // Build AT content nodes (group messages only)
46
335
  if (isGroup) {
47
336
  if (atAll) {
@@ -79,7 +368,59 @@ export const infoflowMessageActions: ChannelMessageActionAdapter = {
79
368
  }
80
369
 
81
370
  if (mediaUrl) {
82
- contents.push({ type: "link", content: mediaUrl });
371
+ logVerbose(
372
+ `[infoflow:action:send] to=${to}, atAll=${atAll}, mentionUserIds=${mentionUserIdsRaw ?? "none"}`,
373
+ );
374
+
375
+ // Send text+mentions first (if any)
376
+ if (contents.length > 0) {
377
+ await sendInfoflowMessage({
378
+ cfg,
379
+ to,
380
+ contents,
381
+ accountId: accountId ?? undefined,
382
+ replyTo,
383
+ });
384
+ }
385
+
386
+ // Try native image send, fallback to link
387
+ try {
388
+ const prepared = await prepareInfoflowImageBase64({ mediaUrl });
389
+ if (prepared.isImage) {
390
+ const imgResult = await sendInfoflowImageMessage({
391
+ cfg,
392
+ to,
393
+ base64Image: prepared.base64,
394
+ accountId: accountId ?? undefined,
395
+ replyTo: contents.length > 0 ? undefined : replyTo,
396
+ });
397
+ return jsonResult({
398
+ ok: imgResult.ok,
399
+ channel: "infoflow",
400
+ to,
401
+ messageId: imgResult.messageId ?? (imgResult.ok ? "sent" : "failed"),
402
+ ...(imgResult.error ? { error: imgResult.error } : {}),
403
+ });
404
+ }
405
+ } catch {
406
+ // fallback to link below
407
+ }
408
+
409
+ // Non-image or native send failed → send as link
410
+ const linkResult = await sendInfoflowMessage({
411
+ cfg,
412
+ to,
413
+ contents: [{ type: "link", content: mediaUrl }],
414
+ accountId: accountId ?? undefined,
415
+ replyTo: contents.length > 0 ? undefined : replyTo,
416
+ });
417
+ return jsonResult({
418
+ ok: linkResult.ok,
419
+ channel: "infoflow",
420
+ to,
421
+ messageId: linkResult.messageId ?? (linkResult.ok ? "sent" : "failed"),
422
+ ...(linkResult.error ? { error: linkResult.error } : {}),
423
+ });
83
424
  }
84
425
 
85
426
  if (contents.length === 0) {
@@ -95,6 +436,7 @@ export const infoflowMessageActions: ChannelMessageActionAdapter = {
95
436
  to,
96
437
  contents,
97
438
  accountId: accountId ?? undefined,
439
+ replyTo,
98
440
  });
99
441
 
100
442
  return jsonResult({