Files
openclaw/extensions/qqbot/src/engine/gateway/message-queue.test.ts
cxy 5ccf179a34 feat(qqbot): group chat support, C2C streaming, chunked media upload, and architecture refactor (#70624)
* feat(qqbot): implement unified media upload handling and introduce chunked upload support

This commit enhances the media upload functionality by introducing a unified `sendMedia` method that consolidates the previous separate methods for sending images, voice messages, videos, and files. Key changes include:

- Added `uploadChunked` function for future chunked media uploads, currently marked as not implemented.
- Introduced `MediaSource` abstraction to handle various media types (URLs, base64, local files, buffers) uniformly.
- Updated existing media handling logic to utilize the new `sendMedia` method, ensuring consistent media processing across different types.
- Enhanced error handling and validation for media uploads, including MIME type checks and file size limits.

These changes aim to streamline the media upload process and prepare for future enhancements in handling larger files through chunked uploads.

* feat(qqbot): enhance media upload capabilities with chunked upload support

This commit updates the media upload functionality by implementing chunked upload support for larger files. Key changes include:

- Revised the `SKILL.md` documentation to clarify media file size limits and local file path requirements.
- Introduced a new test suite for the chunked media upload functionality, ensuring robust error handling and upload processes.
- Updated the media handling logic to enforce per-file-type upload ceilings, allowing for seamless integration of chunked uploads.
- Enhanced error handling for daily upload limits, providing user-friendly messages when limits are exceeded.

These improvements aim to streamline the media upload process and accommodate larger files effectively.

* feat(qqbot): add C2C streaming API support for message delivery

This commit introduces support for the QQ C2C official `stream_messages` API, enabling single-message typing-style updates. Key changes include:

- Updated the configuration schema to include a new `c2cStreamApi` boolean option for enabling the C2C streaming API.
- Enhanced the `QQBotAccountConfig` interface to accommodate the new streaming option.
- Implemented a `StreamingController` to manage the lifecycle of C2C stream messages, ensuring proper handling of media tags and message boundaries.
- Updated the outbound dispatch logic to utilize the new streaming capabilities, allowing for more dynamic message delivery in one-to-one chats.

These enhancements aim to improve the responsiveness and interactivity of message delivery within the QQBot framework.

* feat(qqbot): implement group chat support and unify adapter/DI architecture

- Implement group message history tracking with pending history buffer
  (record on skip, render on @-mention reply)
- Add mention detection and gating: explicit @bot, implicit quote-reply,
  ignoreOtherMentions, configurable activation mode (mention/always)
- Add group activation resolution with session store persistence
- Add message queue with per-peer FIFO and group message merging
  (batch multiple rapid messages into one merged payload)
- Add deliver debounce to merge rapid outbound text bursts into
  single messages, with media flush and maxWait cap
- Add group config resolution: per-group prompt, history limit,
  wildcard and specific group overrides
- Enrich history attachments with local paths from processAttachments
  so that history context renders downloaded paths instead of ephemeral
  QQ CDN URLs

- Merge ports/ directory into adapter/ as single entry point
- Expand EngineAdapters to 5 required ports: history, mentionGate,
  audioConvert, outboundAudio, commands
- Remove global register/get singletons in favor of constructor
  injection and one-time init
- Add createEngineAdapters() in bridge/gateway.ts as single assembly point

- Extract monolithic buildInboundContext into 11 discrete stages:
  access, content, quote, refidx, group-gate, envelope, assembly
- Extract group chat modules: history, mention, activation,
  message-gating, deliver-debounce
- Extract config/group.ts, utils/attachment-tags.ts

* feat(qqbot): add /bot-streaming command for C2C message streaming control

This commit introduces the `/bot-streaming` command, allowing users to enable or disable streaming for message delivery in C2C chats. Key changes include:

- Implementation of the `isStreamingConfigEnabled` function to check the current streaming configuration.
- Command handler for `/bot-streaming` that provides usage instructions and manages the streaming state.
- Updates to the command's response messages to inform users of the current streaming status and how to toggle it.

These enhancements aim to improve user experience by providing a straightforward way to manage streaming message delivery in private chats.

* feat(qqbot): extract interaction handler and add remote config query/update support

- Extract INTERACTION_CREATE handler from gateway.ts into a dedicated
  interaction-handler.ts module for better separation of concerns
- Add config query (type=2001) and config update (type=2002) interaction
  branches that read/write claw_cfg via runtime.config API
- Register INTERACTION intent (1<<26) in FULL_INTENTS to receive
  INTERACTION_CREATE events from the gateway
- Add InteractionType constants (CONFIG_QUERY, CONFIG_UPDATE)
- Extend GatewayPluginRuntime with optional config API (loadConfig,
  writeConfigFile) for interaction handler access
- Add QQBotAccountConfigView interface for typed config field access
- Extend acknowledgeInteraction to accept optional data payload for
  rich ACK responses (e.g. claw_cfg snapshot)
- Export getFrameworkVersion from slash-commands-impl for version
  reporting in config snapshots
- Remove unused eslint-disable directive in streaming-media-send.ts

* feat(qqbot): enhance account management and logging capabilities

- Introduced `toGatewayAccount` function to map resolved QQBot accounts to the engine's gateway account structure.
- Added `persistAccountCredentialSnapshot` function to streamline credential backup during gateway events.
- Updated the `qqbotPlugin` to utilize the new account mapping and credential persistence functions, improving the handling of account data.
- Enhanced logging functionality by modifying the `EngineLogger` interface to support metadata in log messages.
- Implemented new commands for managing logs and clearing storage, providing users with better control over their data and system resources.
- Registered multiple built-in commands for improved user interaction, including `/bot-logs` for exporting logs and `/bot-clear-storage` for managing downloaded files.
- Updated configuration schemas to reflect new options and improve clarity for users.

* fix(qqbot): resolve oxlint errors and update raw-fetch allowlist

- Replace unnecessary `else` after `return` in outbound-media-send.ts (6 occurrences)
- Use `Number.parseInt` instead of global `parseInt` in outbound.ts and streaming-media-send.ts
- Use `Number.isNaN` instead of global `isNaN` in register-basic.ts
- Prefer `**` over `Math.pow` in media-chunked.ts
- Convert interface with call signature to function type in commands.port.ts
- Update api-client.ts allowlist line number (108→124) and add media-chunked.ts:552 to raw-fetch allowlist

* docs(qqbot): translate streaming-c2c.ts header comments to English

* feat(qqbot): add voiceMediaTypes

* feat: restore dispatch changes

* fix(qqbot): align test files with updated engine interfaces after rebase

- inbound-attachments.test: replace removed registerAudioConvertAdapter
  with AudioConvertPort, pass audioConvert in ProcessContext
- inbound-pipeline.self-echo.test: add required adapters field to
  InboundPipelineDeps mock (history, mentionGate, audioConvert,
  outboundAudio, commands)
- outbound-dispatch.test: add required skipped field to InboundContext

* fix(qqbot): update test assertions to match refactored engine interfaces

- inbound-pipeline.self-echo.test: self-echo blocking was moved upstream;
  update test to expect non-blocked pipeline behavior
- outbound-dispatch.test: TTS voice path now uses unified sendMedia
  instead of sendVoiceMessage; add sendMedia mock and update assertion
- format-ref-entry.test: attachment format changed from [image: ...]
  to MEDIA: tag syntax via renderAttachmentTags; update expected output

* refactor(qqbot): migrate from deprecated config API to current/replaceConfigFile

Replace all usages of deprecated runtime config methods:
- loadConfig() → current()
- writeConfigFile(cfg) → replaceConfigFile({ nextConfig, afterWrite })

Updated files:
- bridge/narrowing.ts: writeOpenClawConfigThroughRuntime
- adapter/commands.port.ts: ApproveRuntimeGetter type signature
- commands/builtin/register-approve.ts: loadExecConfig, writeExecConfig, reset
- commands/builtin/register-streaming.ts: config read/write
- gateway/interaction-handler.ts: config query/update handlers
- gateway/types.ts: GatewayPluginRuntime.config interface

* feat(qqbot): update package.json

* fix(qqbot): replace deprecated config-runtime import with config-types subpath

Bundled plugin lint requires focused plugin-sdk subpaths.
- gateway.ts: openclaw/plugin-sdk/config-runtime → config-types
- narrowing.ts: openclaw/plugin-sdk/config-runtime → config-types

* feat(qqbot): group chat support, C2C streaming, chunked media upload, and architecture refactor (#70624) (thanks @cxyhhhhh)

---------

Co-authored-by: Bobby <zkd8907@live.com>
Co-authored-by: sliverp <870080352@qq.com>
2026-04-27 23:19:12 +08:00

283 lines
11 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { createMessageQueue, mergeGroupMessages, type QueuedMessage } from "./message-queue.js";
function groupMsg(overrides: Partial<QueuedMessage> = {}): QueuedMessage {
return {
type: "group",
senderId: "U1",
senderName: "Alice",
content: "hello",
messageId: "M1",
timestamp: "2026-01-01T00:00:00Z",
groupOpenid: "G1",
...overrides,
};
}
describe("engine/gateway/message-queue", () => {
describe("mergeGroupMessages", () => {
it("returns the single message unchanged", () => {
const m = groupMsg();
const merged = mergeGroupMessages([m]);
expect(merged).toBe(m);
});
it("joins content with sender prefix per line", () => {
const merged = mergeGroupMessages([
groupMsg({ senderName: "A", content: "hi" }),
groupMsg({ senderName: "B", content: "yo" }),
]);
expect(merged.content).toBe("[A]: hi\n[B]: yo");
expect(merged.merge?.count).toBe(2);
expect(merged.merge?.messages).toHaveLength(2);
});
it("takes messageId / msgIdx / timestamp from the last message", () => {
const merged = mergeGroupMessages([
groupMsg({ messageId: "M1", msgIdx: "I1", timestamp: "T1" }),
groupMsg({ messageId: "M2", msgIdx: "I2", timestamp: "T2" }),
]);
expect(merged.messageId).toBe("M2");
expect(merged.msgIdx).toBe("I2");
expect(merged.timestamp).toBe("T2");
});
it("takes refMsgIdx from the first message", () => {
const merged = mergeGroupMessages([
groupMsg({ refMsgIdx: "R1" }),
groupMsg({ refMsgIdx: "R2" }),
]);
expect(merged.refMsgIdx).toBe("R1");
});
it("concatenates attachments in order", () => {
const merged = mergeGroupMessages([
groupMsg({
attachments: [{ content_type: "image/png", url: "a" }],
}),
groupMsg({
attachments: [
{ content_type: "image/png", url: "b" },
{ content_type: "image/png", url: "c" },
],
}),
]);
expect(merged.attachments?.map((a) => a.url)).toEqual(["a", "b", "c"]);
});
it("deduplicates mentions by member/user openid", () => {
const merged = mergeGroupMessages([
groupMsg({ mentions: [{ member_openid: "X" }, { member_openid: "Y" }] }),
groupMsg({ mentions: [{ member_openid: "X" }, { member_openid: "Z" }] }),
]);
expect(merged.mentions?.map((m) => m.member_openid)).toEqual(["X", "Y", "Z"]);
});
it("flags merged turn as @bot when ANY source was GROUP_AT_MESSAGE_CREATE", () => {
const merged = mergeGroupMessages([
groupMsg({ eventType: "GROUP_MESSAGE_CREATE" }),
groupMsg({ eventType: "GROUP_AT_MESSAGE_CREATE" }),
]);
expect(merged.eventType).toBe("GROUP_AT_MESSAGE_CREATE");
});
it("keeps last eventType when no @bot event was present", () => {
const merged = mergeGroupMessages([
groupMsg({ eventType: "GROUP_MESSAGE_CREATE" }),
groupMsg({ eventType: "GROUP_MESSAGE_CREATE" }),
]);
expect(merged.eventType).toBe("GROUP_MESSAGE_CREATE");
});
it("marks as bot only when every source is a bot", () => {
expect(
mergeGroupMessages([groupMsg({ senderIsBot: true }), groupMsg({ senderIsBot: false })])
.senderIsBot,
).toBe(false);
expect(
mergeGroupMessages([groupMsg({ senderIsBot: true }), groupMsg({ senderIsBot: true })])
.senderIsBot,
).toBe(true);
});
});
describe("createMessageQueue enqueue / evict", () => {
it("uses group peerId for group messages", () => {
const q = createMessageQueue({ accountId: "a", isAborted: () => true });
expect(q.getMessagePeerId(groupMsg({ groupOpenid: "G9" }))).toBe("group:G9");
});
it("uses dm peerId for c2c messages", () => {
const q = createMessageQueue({ accountId: "a", isAborted: () => true });
expect(
q.getMessagePeerId({
...groupMsg(),
type: "c2c",
groupOpenid: undefined,
senderId: "U9",
}),
).toBe("dm:U9");
});
it("enqueue without processor still drains (no-op when fn is null)", async () => {
// When no processor is attached, drain shifts messages but does
// nothing with them. The queue ends empty on the next microtask.
const q = createMessageQueue({ accountId: "a", isAborted: () => false });
q.enqueue(groupMsg({ messageId: "M1" }));
q.enqueue(groupMsg({ messageId: "M2" }));
await Promise.resolve();
await Promise.resolve();
expect(q.getSnapshot("group:G1").senderPending).toBe(0);
});
it("group overflow evicts a bot message first (eviction is synchronous)", () => {
// Use isAborted=true so drain exits immediately on the first
// microtask. Our `eviction` logic runs synchronously inside
// enqueue, BEFORE drain kicks in, so the 4th enqueue still has to
// evict even though we never actually process anything.
const q = createMessageQueue({
accountId: "a",
isAborted: () => true,
groupQueueSize: 3,
});
// Fill the queue to the cap (3), then enqueue one more to trigger
// eviction. The first three enqueues trigger drainUserQueue which
// synchronously deletes the empty queue in its finally block when
// isAborted=true. We bypass that by calling enqueue then reading
// inside the same synchronous tick via getSnapshot is NOT viable,
// so we instead observe the eviction by counting what ends up
// visible after the queue has stabilized.
q.enqueue(groupMsg({ messageId: "H1" }));
q.enqueue(groupMsg({ messageId: "B1", senderIsBot: true }));
q.enqueue(groupMsg({ messageId: "H2" }));
q.enqueue(groupMsg({ messageId: "H3" }));
// With isAborted=true the drain deletes the queue after each
// enqueue, so the snapshot just confirms we didn't throw. The
// actual eviction logic is covered by the "group overflow via
// processor" scenario below.
expect(q.getSnapshot("group:G1").senderPending).toBe(0);
});
it("group overflow drops bot messages first (via processor)", async () => {
const seen: QueuedMessage[] = [];
let gate!: (value?: unknown) => void;
const blocker = new Promise((res) => {
gate = res;
});
const q = createMessageQueue({
accountId: "a",
isAborted: () => false,
groupQueueSize: 3,
});
q.startProcessor(async (msg) => {
seen.push(msg);
// Hold the processor until we've filled the queue to capacity.
await blocker;
});
// First enqueue starts processing immediately (blocker held).
q.enqueue(groupMsg({ messageId: "First" }));
await Promise.resolve();
// Now fill the queue with 3 more (cap=3).
q.enqueue(groupMsg({ messageId: "H1" }));
q.enqueue(groupMsg({ messageId: "B1", senderIsBot: true }));
q.enqueue(groupMsg({ messageId: "H2" }));
expect(q.getSnapshot("group:G1").senderPending).toBe(3);
// 5th enqueue → eviction. Bot message (B1) should be the victim.
q.enqueue(groupMsg({ messageId: "H3" }));
const peerQueueIds = q.getSnapshot("group:G1");
expect(peerQueueIds.senderPending).toBe(3);
// Release the processor and drain.
gate();
await new Promise((res) => setTimeout(res, 0));
const seenIds = seen.map((m) => m.messageId);
expect(seenIds).toContain("First");
// The bot message should NOT have been processed — it was evicted.
// (Note: The first batch ran merged, so the exact count of calls
// varies; we only assert the bot message id never appeared.)
const mergedCall = seen.find((m) => (m.merge?.count ?? 0) > 1);
if (mergedCall) {
expect(mergedCall.merge?.messages.map((m) => m.messageId)).not.toContain("B1");
} else {
expect(seenIds).not.toContain("B1");
}
});
it("clearUserQueue drops buffered items before drain runs", () => {
// Use a processor that never resolves so enqueued messages stay
// buffered behind a single active worker — then clearUserQueue
// should drop the rest.
let release!: () => void;
const blocker = new Promise<void>((res) => {
release = res;
});
const q = createMessageQueue({ accountId: "a", isAborted: () => false });
q.startProcessor(async () => {
await blocker;
});
q.enqueue(groupMsg({ messageId: "M1" }));
q.enqueue(groupMsg({ messageId: "M2" }));
q.enqueue(groupMsg({ messageId: "M3" }));
// First message is being processed; remaining two are queued.
expect(q.getSnapshot("group:G1").senderPending).toBeGreaterThanOrEqual(0);
const dropped = q.clearUserQueue("group:G1");
expect(dropped).toBeGreaterThanOrEqual(0);
release();
});
});
describe("drainGroupBatch merging", () => {
it("merges multiple normal group messages into one processor call", async () => {
const seen: QueuedMessage[] = [];
let aborted = false;
const q = createMessageQueue({
accountId: "a",
isAborted: () => aborted,
});
q.startProcessor(async (msg) => {
seen.push(msg);
});
// Enqueue three normal group messages synchronously so they batch
// before the drain loop kicks in — the first enqueue starts the
// drain, but the synchronous enqueues land before the first await.
q.enqueue(groupMsg({ messageId: "M1", content: "hi" }));
q.enqueue(groupMsg({ messageId: "M2", content: "yo" }));
q.enqueue(groupMsg({ messageId: "M3", content: "!!" }));
// Allow microtasks to flush.
await Promise.resolve();
await Promise.resolve();
aborted = true;
// Depending on timing the first message may have been processed solo;
// what we guarantee is that the total processor calls are fewer than
// three and the remaining messages were merged.
expect(seen.length).toBeGreaterThanOrEqual(1);
expect(seen.length).toBeLessThan(3);
const mergedCall = seen.find((m) => (m.merge?.count ?? 0) > 1);
expect(mergedCall).toBeDefined();
expect(mergedCall?.content).toContain("[Alice]:");
});
it("processes slash commands independently from regular messages", async () => {
const seen: QueuedMessage[] = [];
let aborted = false;
const q = createMessageQueue({
accountId: "a",
isAborted: () => aborted,
});
q.startProcessor(async (msg) => {
seen.push(msg);
});
q.enqueue(groupMsg({ messageId: "M1", content: "hi" }));
q.enqueue(groupMsg({ messageId: "M2", content: "/stop" }));
q.enqueue(groupMsg({ messageId: "M3", content: "yo" }));
await Promise.resolve();
await Promise.resolve();
aborted = true;
// Command should appear as its own call (not merged with the others).
const cmdCall = seen.find((m) => m.content === "/stop");
expect(cmdCall).toBeDefined();
expect(cmdCall?.merge).toBeUndefined();
});
});
});