@gakr-gakr/qqbot 0.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.
Files changed (149) hide show
  1. package/api.ts +56 -0
  2. package/autobot.plugin.json +167 -0
  3. package/channel-plugin-api.ts +1 -0
  4. package/index.ts +33 -0
  5. package/package.json +64 -0
  6. package/runtime-api.ts +9 -0
  7. package/secret-contract-api.ts +5 -0
  8. package/setup-entry.ts +13 -0
  9. package/setup-plugin-api.ts +3 -0
  10. package/skills/qqbot-channel/SKILL.md +262 -0
  11. package/skills/qqbot-channel/references/api_references.md +521 -0
  12. package/skills/qqbot-media/SKILL.md +37 -0
  13. package/skills/qqbot-remind/SKILL.md +153 -0
  14. package/src/bridge/approval/capability.ts +225 -0
  15. package/src/bridge/approval/handler-runtime.ts +204 -0
  16. package/src/bridge/bootstrap.ts +135 -0
  17. package/src/bridge/channel-entry.ts +18 -0
  18. package/src/bridge/commands/framework-context-adapter.ts +60 -0
  19. package/src/bridge/commands/framework-registration.ts +66 -0
  20. package/src/bridge/commands/from-parser.ts +60 -0
  21. package/src/bridge/commands/result-dispatcher.ts +76 -0
  22. package/src/bridge/config-shared.ts +132 -0
  23. package/src/bridge/config.ts +176 -0
  24. package/src/bridge/gateway.ts +178 -0
  25. package/src/bridge/logger.ts +31 -0
  26. package/src/bridge/narrowing.ts +31 -0
  27. package/src/bridge/plugin-version.ts +102 -0
  28. package/src/bridge/runtime.ts +25 -0
  29. package/src/bridge/sdk-adapter.ts +164 -0
  30. package/src/bridge/setup/finalize.ts +144 -0
  31. package/src/bridge/setup/surface.ts +34 -0
  32. package/src/bridge/tools/channel.ts +58 -0
  33. package/src/bridge/tools/index.ts +15 -0
  34. package/src/bridge/tools/remind.ts +91 -0
  35. package/src/channel.setup.ts +33 -0
  36. package/src/channel.ts +399 -0
  37. package/src/config-schema.ts +84 -0
  38. package/src/engine/access/index.ts +2 -0
  39. package/src/engine/access/resolve-policy.ts +30 -0
  40. package/src/engine/access/sender-match.ts +55 -0
  41. package/src/engine/access/types.ts +2 -0
  42. package/src/engine/adapter/audio.port.ts +27 -0
  43. package/src/engine/adapter/commands.port.ts +22 -0
  44. package/src/engine/adapter/history.port.ts +52 -0
  45. package/src/engine/adapter/index.ts +76 -0
  46. package/src/engine/adapter/mention-gate.port.ts +50 -0
  47. package/src/engine/adapter/types.ts +38 -0
  48. package/src/engine/api/api-client.ts +212 -0
  49. package/src/engine/api/media-chunked.ts +644 -0
  50. package/src/engine/api/media.ts +218 -0
  51. package/src/engine/api/messages.ts +293 -0
  52. package/src/engine/api/retry.ts +217 -0
  53. package/src/engine/api/routes.ts +95 -0
  54. package/src/engine/api/token.ts +277 -0
  55. package/src/engine/approval/index.ts +224 -0
  56. package/src/engine/commands/builtin/log-helpers.ts +341 -0
  57. package/src/engine/commands/builtin/register-all.ts +17 -0
  58. package/src/engine/commands/builtin/register-approve.ts +201 -0
  59. package/src/engine/commands/builtin/register-basic.ts +95 -0
  60. package/src/engine/commands/builtin/register-clear-storage.ts +187 -0
  61. package/src/engine/commands/builtin/register-logs.ts +20 -0
  62. package/src/engine/commands/builtin/register-streaming.ts +138 -0
  63. package/src/engine/commands/builtin/state.ts +31 -0
  64. package/src/engine/commands/slash-command-auth.ts +88 -0
  65. package/src/engine/commands/slash-command-handler.ts +168 -0
  66. package/src/engine/commands/slash-command-test-support.ts +39 -0
  67. package/src/engine/commands/slash-commands-impl.ts +61 -0
  68. package/src/engine/commands/slash-commands.ts +202 -0
  69. package/src/engine/config/credential-backup.ts +108 -0
  70. package/src/engine/config/credentials.ts +76 -0
  71. package/src/engine/config/group.ts +227 -0
  72. package/src/engine/config/resolve.ts +283 -0
  73. package/src/engine/config/setup-logic.ts +84 -0
  74. package/src/engine/gateway/active-cfg.ts +52 -0
  75. package/src/engine/gateway/codec.ts +47 -0
  76. package/src/engine/gateway/constants.ts +117 -0
  77. package/src/engine/gateway/event-dispatcher.ts +177 -0
  78. package/src/engine/gateway/gateway-connection.ts +356 -0
  79. package/src/engine/gateway/gateway.ts +267 -0
  80. package/src/engine/gateway/inbound-attachments.ts +360 -0
  81. package/src/engine/gateway/inbound-context.ts +82 -0
  82. package/src/engine/gateway/inbound-pipeline.ts +171 -0
  83. package/src/engine/gateway/interaction-handler.ts +345 -0
  84. package/src/engine/gateway/message-queue.ts +404 -0
  85. package/src/engine/gateway/outbound-dispatch.ts +590 -0
  86. package/src/engine/gateway/reconnect.ts +199 -0
  87. package/src/engine/gateway/stages/access-stage.ts +99 -0
  88. package/src/engine/gateway/stages/assembly-stage.ts +156 -0
  89. package/src/engine/gateway/stages/content-stage.ts +77 -0
  90. package/src/engine/gateway/stages/envelope-stage.ts +144 -0
  91. package/src/engine/gateway/stages/group-gate-stage.ts +223 -0
  92. package/src/engine/gateway/stages/index.ts +18 -0
  93. package/src/engine/gateway/stages/quote-stage.ts +113 -0
  94. package/src/engine/gateway/stages/refidx-stage.ts +62 -0
  95. package/src/engine/gateway/stages/stub-contexts.ts +77 -0
  96. package/src/engine/gateway/types.ts +230 -0
  97. package/src/engine/gateway/typing-keepalive.ts +102 -0
  98. package/src/engine/gateway/ws-client.ts +16 -0
  99. package/src/engine/group/activation.ts +88 -0
  100. package/src/engine/group/history.ts +321 -0
  101. package/src/engine/group/mention.ts +114 -0
  102. package/src/engine/group/message-gating.ts +108 -0
  103. package/src/engine/messaging/decode-media-path.ts +82 -0
  104. package/src/engine/messaging/media-source.ts +210 -0
  105. package/src/engine/messaging/media-type-detect.ts +27 -0
  106. package/src/engine/messaging/outbound-audio-port.ts +38 -0
  107. package/src/engine/messaging/outbound-deliver.ts +810 -0
  108. package/src/engine/messaging/outbound-media-send.ts +658 -0
  109. package/src/engine/messaging/outbound-reply.ts +27 -0
  110. package/src/engine/messaging/outbound-result-helpers.ts +54 -0
  111. package/src/engine/messaging/outbound-types.ts +47 -0
  112. package/src/engine/messaging/outbound.ts +485 -0
  113. package/src/engine/messaging/reply-dispatcher.ts +597 -0
  114. package/src/engine/messaging/reply-limiter.ts +164 -0
  115. package/src/engine/messaging/sender.ts +741 -0
  116. package/src/engine/messaging/streaming-c2c.ts +1192 -0
  117. package/src/engine/messaging/streaming-media-send.ts +544 -0
  118. package/src/engine/messaging/target-parser.ts +104 -0
  119. package/src/engine/ref/format-message-ref.ts +142 -0
  120. package/src/engine/ref/format-ref-entry.ts +27 -0
  121. package/src/engine/ref/store.ts +211 -0
  122. package/src/engine/ref/types.ts +27 -0
  123. package/src/engine/session/known-users.ts +138 -0
  124. package/src/engine/session/session-store.ts +207 -0
  125. package/src/engine/tools/channel-api.ts +244 -0
  126. package/src/engine/tools/remind-logic.ts +377 -0
  127. package/src/engine/types.ts +313 -0
  128. package/src/engine/utils/attachment-tags.ts +174 -0
  129. package/src/engine/utils/audio.ts +525 -0
  130. package/src/engine/utils/data-paths.ts +38 -0
  131. package/src/engine/utils/diagnostics.ts +93 -0
  132. package/src/engine/utils/file-utils.ts +215 -0
  133. package/src/engine/utils/format.ts +70 -0
  134. package/src/engine/utils/image-size.ts +249 -0
  135. package/src/engine/utils/log.ts +77 -0
  136. package/src/engine/utils/media-tags.ts +177 -0
  137. package/src/engine/utils/payload.ts +157 -0
  138. package/src/engine/utils/platform.ts +265 -0
  139. package/src/engine/utils/request-context.ts +60 -0
  140. package/src/engine/utils/string-normalize.ts +91 -0
  141. package/src/engine/utils/stt.ts +103 -0
  142. package/src/engine/utils/text-parsing.ts +155 -0
  143. package/src/engine/utils/upload-cache.ts +96 -0
  144. package/src/engine/utils/voice-text.ts +15 -0
  145. package/src/exec-approvals.ts +237 -0
  146. package/src/qqbot-test-support.ts +29 -0
  147. package/src/secret-contract.ts +82 -0
  148. package/src/types.ts +210 -0
  149. package/tsconfig.json +16 -0
@@ -0,0 +1,199 @@
1
+ /**
2
+ * WebSocket reconnection state machine and close-code handler.
3
+ *
4
+ * Encapsulates the reconnect delay scheduling, quick-disconnect detection,
5
+ * and close-code interpretation that both plugin versions share.
6
+ *
7
+ * Zero external dependencies — uses only the constants from `./constants.ts`.
8
+ */
9
+
10
+ import type { EngineLogger } from "../types.js";
11
+ import {
12
+ RECONNECT_DELAYS,
13
+ RATE_LIMIT_DELAY,
14
+ MAX_RECONNECT_ATTEMPTS,
15
+ MAX_QUICK_DISCONNECT_COUNT,
16
+ QUICK_DISCONNECT_THRESHOLD,
17
+ GatewayCloseCode,
18
+ } from "./constants.js";
19
+
20
+ /** Actions the caller should take after processing a close event. */
21
+ interface CloseAction {
22
+ /** Whether to schedule a reconnect. */
23
+ shouldReconnect: boolean;
24
+ /** Custom delay override (ms), or undefined to use the default backoff. */
25
+ reconnectDelay?: number;
26
+ /** Whether the session is invalidated and should be cleared. */
27
+ clearSession: boolean;
28
+ /** Whether the token should be refreshed before reconnecting. */
29
+ refreshToken: boolean;
30
+ /** Whether the bot is fatally blocked (offline/banned) and should stop. */
31
+ fatal: boolean;
32
+ /** Human-readable description of the close reason. */
33
+ reason: string;
34
+ }
35
+
36
+ /**
37
+ * Reconnection state machine.
38
+ *
39
+ * Usage:
40
+ * ```ts
41
+ * const rs = new ReconnectState('account-1', log);
42
+ * // On successful connect:
43
+ * rs.onConnected();
44
+ * // On close:
45
+ * const action = rs.handleClose(code);
46
+ * if (action.shouldReconnect) {
47
+ * const delay = rs.getNextDelay(action.reconnectDelay);
48
+ * setTimeout(connect, delay);
49
+ * }
50
+ * ```
51
+ */
52
+ export class ReconnectState {
53
+ private attempts = 0;
54
+ private lastConnectTime = 0;
55
+ private quickDisconnectCount = 0;
56
+
57
+ constructor(
58
+ private readonly accountId: string,
59
+ private readonly log?: EngineLogger,
60
+ ) {}
61
+
62
+ /** Call when a WebSocket connection is successfully established. */
63
+ onConnected(): void {
64
+ this.attempts = 0;
65
+ this.lastConnectTime = Date.now();
66
+ }
67
+
68
+ /** Whether reconnection attempts are exhausted. */
69
+ isExhausted(): boolean {
70
+ return this.attempts >= MAX_RECONNECT_ATTEMPTS;
71
+ }
72
+
73
+ /**
74
+ * Compute the next reconnect delay and increment the attempt counter.
75
+ *
76
+ * @param customDelay Override from `CloseAction.reconnectDelay`.
77
+ * @returns Delay in milliseconds.
78
+ */
79
+ getNextDelay(customDelay?: number): number {
80
+ const delay =
81
+ customDelay ?? RECONNECT_DELAYS[Math.min(this.attempts, RECONNECT_DELAYS.length - 1)];
82
+ this.attempts++;
83
+ this.log?.debug?.(`Reconnecting ${this.accountId} in ${delay}ms (attempt ${this.attempts})`);
84
+ return delay;
85
+ }
86
+
87
+ /**
88
+ * Interpret a WebSocket close code and return the appropriate action.
89
+ */
90
+ handleClose(code: number, isAborted: boolean): CloseAction {
91
+ // Fatal: bot offline or banned.
92
+ if (
93
+ code === GatewayCloseCode.INSUFFICIENT_INTENTS ||
94
+ code === GatewayCloseCode.DISALLOWED_INTENTS
95
+ ) {
96
+ const reason =
97
+ code === GatewayCloseCode.INSUFFICIENT_INTENTS ? "offline/sandbox-only" : "banned";
98
+ this.log?.error(`Bot is ${reason}. Please contact QQ platform.`);
99
+ return {
100
+ shouldReconnect: false,
101
+ clearSession: false,
102
+ refreshToken: false,
103
+ fatal: true,
104
+ reason,
105
+ };
106
+ }
107
+
108
+ // Invalid token.
109
+ if (code === GatewayCloseCode.AUTH_FAILED) {
110
+ this.log?.info(`Invalid token (4004), will refresh token and reconnect`);
111
+ return {
112
+ shouldReconnect: !isAborted,
113
+ clearSession: false,
114
+ refreshToken: true,
115
+ fatal: false,
116
+ reason: "invalid token (4004)",
117
+ };
118
+ }
119
+
120
+ // Rate limited.
121
+ if (code === GatewayCloseCode.RATE_LIMITED) {
122
+ this.log?.info(`Rate limited (4008), waiting ${RATE_LIMIT_DELAY}ms`);
123
+ return {
124
+ shouldReconnect: !isAborted,
125
+ reconnectDelay: RATE_LIMIT_DELAY,
126
+ clearSession: false,
127
+ refreshToken: false,
128
+ fatal: false,
129
+ reason: "rate limited (4008)",
130
+ };
131
+ }
132
+
133
+ // Session invalid / seq invalid / session timeout.
134
+ if (
135
+ code === GatewayCloseCode.INVALID_SESSION ||
136
+ code === GatewayCloseCode.SEQ_OUT_OF_RANGE ||
137
+ code === GatewayCloseCode.SESSION_TIMEOUT
138
+ ) {
139
+ const codeDesc: Record<number, string> = {
140
+ [GatewayCloseCode.INVALID_SESSION]: "session no longer valid",
141
+ [GatewayCloseCode.SEQ_OUT_OF_RANGE]: "invalid seq on resume",
142
+ [GatewayCloseCode.SESSION_TIMEOUT]: "session timed out",
143
+ };
144
+ this.log?.info(`Error ${code} (${codeDesc[code]}), will re-identify`);
145
+ return {
146
+ shouldReconnect: !isAborted,
147
+ clearSession: true,
148
+ refreshToken: true,
149
+ fatal: false,
150
+ reason: codeDesc[code],
151
+ };
152
+ }
153
+
154
+ // Internal server errors.
155
+ if (code >= GatewayCloseCode.SERVER_ERROR_START && code <= GatewayCloseCode.SERVER_ERROR_END) {
156
+ this.log?.info(`Internal error (${code}), will re-identify`);
157
+ return {
158
+ shouldReconnect: !isAborted && code !== GatewayCloseCode.NORMAL,
159
+ clearSession: true,
160
+ refreshToken: true,
161
+ fatal: false,
162
+ reason: `internal error (${code})`,
163
+ };
164
+ }
165
+
166
+ // Quick disconnect detection.
167
+ const connectionDuration = Date.now() - this.lastConnectTime;
168
+ if (connectionDuration < QUICK_DISCONNECT_THRESHOLD && this.lastConnectTime > 0) {
169
+ this.quickDisconnectCount++;
170
+ this.log?.debug?.(
171
+ `Quick disconnect detected (${connectionDuration}ms), count: ${this.quickDisconnectCount}`,
172
+ );
173
+
174
+ if (this.quickDisconnectCount >= MAX_QUICK_DISCONNECT_COUNT) {
175
+ this.log?.error(`Too many quick disconnects. This may indicate a permission issue.`);
176
+ this.quickDisconnectCount = 0;
177
+ return {
178
+ shouldReconnect: !isAborted && code !== 1000,
179
+ reconnectDelay: RATE_LIMIT_DELAY,
180
+ clearSession: false,
181
+ refreshToken: false,
182
+ fatal: false,
183
+ reason: "too many quick disconnects",
184
+ };
185
+ }
186
+ } else {
187
+ this.quickDisconnectCount = 0;
188
+ }
189
+
190
+ // Default: reconnect with backoff.
191
+ return {
192
+ shouldReconnect: !isAborted && code !== GatewayCloseCode.NORMAL,
193
+ clearSession: false,
194
+ refreshToken: false,
195
+ fatal: false,
196
+ reason: `close code ${code}`,
197
+ };
198
+ }
199
+ }
@@ -0,0 +1,99 @@
1
+ import type { QQBotInboundAccess } from "../../adapter/index.js";
2
+ import type { InboundContext, InboundPipelineDeps } from "../inbound-context.js";
3
+ import type { QueuedMessage } from "../message-queue.js";
4
+ import { buildBlockedInboundContext } from "./stub-contexts.js";
5
+
6
+ type AccessStageResult =
7
+ | {
8
+ kind: "allow";
9
+ isGroupChat: boolean;
10
+ peerId: string;
11
+ qualifiedTarget: string;
12
+ fromAddress: string;
13
+ route: { sessionKey: string; accountId: string; agentId?: string };
14
+ access: QQBotInboundAccess;
15
+ }
16
+ | { kind: "block"; context: InboundContext };
17
+
18
+ export async function runAccessStage(
19
+ event: QueuedMessage,
20
+ deps: InboundPipelineDeps,
21
+ ): Promise<AccessStageResult> {
22
+ const { account, cfg, runtime, log } = deps;
23
+
24
+ const isGroupChat = event.type === "guild" || event.type === "group";
25
+ const peerId = resolvePeerId(event, isGroupChat);
26
+ const qualifiedTarget = buildQualifiedTarget(event, isGroupChat);
27
+
28
+ const route = runtime.channel.routing.resolveAgentRoute({
29
+ cfg,
30
+ channel: "qqbot",
31
+ accountId: account.accountId,
32
+ peer: { kind: isGroupChat ? "group" : "direct", id: peerId },
33
+ });
34
+
35
+ const access = await deps.adapters.access.resolveInboundAccess({
36
+ cfg,
37
+ accountId: account.accountId,
38
+ isGroup: isGroupChat,
39
+ senderId: event.senderId,
40
+ conversationId: peerId,
41
+ allowFrom: account.config?.allowFrom,
42
+ groupAllowFrom: account.config?.groupAllowFrom,
43
+ dmPolicy: account.config?.dmPolicy,
44
+ groupPolicy: account.config?.groupPolicy,
45
+ });
46
+
47
+ if (access.senderAccess.decision !== "allow") {
48
+ log?.info(
49
+ `Blocked qqbot inbound: decision=${access.senderAccess.decision} reasonCode=${access.senderAccess.reasonCode} ` +
50
+ `senderId=${event.senderId} accountId=${account.accountId} isGroup=${isGroupChat}`,
51
+ );
52
+ return {
53
+ kind: "block",
54
+ context: buildBlockedInboundContext({
55
+ event,
56
+ route,
57
+ isGroupChat,
58
+ peerId,
59
+ qualifiedTarget,
60
+ fromAddress: qualifiedTarget,
61
+ access,
62
+ }),
63
+ };
64
+ }
65
+
66
+ return {
67
+ kind: "allow",
68
+ isGroupChat,
69
+ peerId,
70
+ qualifiedTarget,
71
+ fromAddress: qualifiedTarget,
72
+ route,
73
+ access,
74
+ };
75
+ }
76
+
77
+ // ─────────────────────────── Internal helpers ───────────────────────────
78
+
79
+ function resolvePeerId(event: QueuedMessage, isGroupChat: boolean): string {
80
+ if (event.type === "guild") {
81
+ return event.channelId ?? "unknown";
82
+ }
83
+ if (event.type === "group") {
84
+ return event.groupOpenid ?? "unknown";
85
+ }
86
+ if (isGroupChat) {
87
+ return "unknown";
88
+ }
89
+ return event.senderId;
90
+ }
91
+
92
+ function buildQualifiedTarget(event: QueuedMessage, isGroupChat: boolean): string {
93
+ if (isGroupChat) {
94
+ return event.type === "guild"
95
+ ? `qqbot:channel:${event.channelId}`
96
+ : `qqbot:group:${event.groupOpenid}`;
97
+ }
98
+ return event.type === "dm" ? `qqbot:dm:${event.guildId}` : `qqbot:c2c:${event.senderId}`;
99
+ }
@@ -0,0 +1,156 @@
1
+ /**
2
+ * Assembly stage — build the user-turn string the AI sees.
3
+ *
4
+ * Responsible for:
5
+ * - Rendering merged turns (preceding messages in a begin/end block
6
+ * + a "current" message).
7
+ * - Attaching the sender label + (@you) suffix for group chat.
8
+ * - Prepending the group's buffered history via
9
+ * {@link buildPendingHistoryContext} when the current turn is
10
+ * `@`-activated.
11
+ * - Handing out the plain `agentBody` for DM-style turns.
12
+ *
13
+ * The envelope rendering (Web UI body + dynamic ctx block) lives in
14
+ * `envelope-stage.ts`; this stage only produces text that the model
15
+ * sees directly.
16
+ */
17
+
18
+ import {
19
+ buildMergedMessageContext,
20
+ formatAttachmentTags,
21
+ formatMessageContent,
22
+ type HistoryEntry,
23
+ } from "../../group/history.js";
24
+ import type { InboundGroupInfo, InboundPipelineDeps } from "../inbound-context.js";
25
+ import type { QueuedMessage } from "../message-queue.js";
26
+
27
+ // ─────────────────────────── buildUserMessage ───────────────────────────
28
+
29
+ interface BuildUserMessageInput {
30
+ event: QueuedMessage;
31
+ userContent: string;
32
+ quotePart: string;
33
+ isGroupChat: boolean;
34
+ groupInfo?: InboundGroupInfo;
35
+ }
36
+
37
+ /**
38
+ * Compose the user-turn string. For merged group turns, renders a
39
+ * preceding block and a current-message suffix; for single turns,
40
+ * prefixes the sender label and (@you) suffix as appropriate.
41
+ */
42
+ export function buildUserMessage(input: BuildUserMessageInput): string {
43
+ const { event, userContent, quotePart, isGroupChat, groupInfo } = input;
44
+
45
+ // ---- Merged group turn ----
46
+ if (groupInfo?.isMerged && groupInfo.mergedMessages?.length) {
47
+ const preceding = groupInfo.mergedMessages.slice(0, -1);
48
+ const lastMsg = groupInfo.mergedMessages[groupInfo.mergedMessages.length - 1];
49
+ const atYouTag = groupInfo.gate.effectiveWasMentioned ? " (@you)" : "";
50
+
51
+ const envelopeParts = preceding.map((m) => `[${formatSenderLabel(m)}] ${formatSub(m)}`);
52
+ const lastPart = `[${formatSenderLabel(lastMsg)}] ${formatSub(lastMsg)}${atYouTag}`;
53
+
54
+ return buildMergedMessageContext({
55
+ precedingParts: envelopeParts,
56
+ currentMessage: lastPart,
57
+ });
58
+ }
59
+
60
+ // ---- Single-message turn ----
61
+ const isAtYouTag = isGroupChat ? (groupInfo?.gate.effectiveWasMentioned ? " (@you)" : "") : "";
62
+ const senderPrefix =
63
+ event.type === "group" ? `[${formatSenderLabelFrom(event.senderName, event.senderId)}] ` : "";
64
+
65
+ return senderPrefix
66
+ ? `${senderPrefix}${quotePart}${userContent}${isAtYouTag}`
67
+ : `${quotePart}${userContent}`;
68
+ }
69
+
70
+ // ─────────────────────────── buildAgentBody ───────────────────────────
71
+
72
+ interface BuildAgentBodyInput {
73
+ event: QueuedMessage;
74
+ userContent: string;
75
+ userMessage: string;
76
+ dynamicCtx: string;
77
+ isGroupChat: boolean;
78
+ groupInfo?: InboundGroupInfo;
79
+ deps: InboundPipelineDeps;
80
+ }
81
+
82
+ /**
83
+ * Compose the final `agentBody` the AI receives.
84
+ *
85
+ * Prepends buffered non-@ chatter via
86
+ * {@link buildPendingHistoryContext} when the current turn is
87
+ * `@`-activated in a group. Slash-commands bypass all decoration so
88
+ * the command parser sees verbatim input.
89
+ */
90
+ export function buildAgentBody(input: BuildAgentBodyInput): string {
91
+ const { event, userContent, userMessage, dynamicCtx, groupInfo, deps } = input;
92
+
93
+ // Slash commands: strip all decoration so the command parser sees raw input.
94
+ if (userContent.startsWith("/")) {
95
+ return userContent;
96
+ }
97
+
98
+ const base = `${dynamicCtx}${userMessage}`;
99
+
100
+ // Non-group or group-without-history: no mixing in.
101
+ if (event.type !== "group" || !event.groupOpenid || !deps.groupHistories || !groupInfo) {
102
+ return base;
103
+ }
104
+
105
+ const envelopeOpts = deps.runtime.channel.reply.resolveEnvelopeFormatOptions(deps.cfg);
106
+ return deps.adapters.history.buildPendingHistoryContext({
107
+ historyMap: deps.groupHistories,
108
+ historyKey: event.groupOpenid,
109
+ limit: groupInfo.historyLimit,
110
+ currentMessage: base,
111
+ formatEntry: (entry) => formatHistoryEntry(entry as HistoryEntry, deps, envelopeOpts),
112
+ });
113
+ }
114
+
115
+ // ─────────────────────────── Internal ───────────────────────────
116
+
117
+ function formatSub(m: QueuedMessage): string {
118
+ return formatMessageContent({
119
+ content: m.content ?? "",
120
+ chatType: m.type,
121
+ mentions: m.mentions as never,
122
+ attachments: m.attachments,
123
+ });
124
+ }
125
+
126
+ function formatSenderLabel(m: QueuedMessage): string {
127
+ return formatSenderLabelFrom(m.senderName, m.senderId);
128
+ }
129
+
130
+ /**
131
+ * Render a "Nick (openid)" label. When `name` already includes `id`
132
+ * (e.g. the label was pre-formatted upstream), avoid double-wrapping.
133
+ */
134
+ function formatSenderLabelFrom(name: string | undefined, id: string): string {
135
+ if (!name) {
136
+ return id;
137
+ }
138
+ return name.includes(id) ? name : `${name} (${id})`;
139
+ }
140
+
141
+ function formatHistoryEntry(
142
+ entry: HistoryEntry,
143
+ deps: InboundPipelineDeps,
144
+ envelopeOpts: unknown,
145
+ ): string {
146
+ const attachmentDesc = formatAttachmentTags(entry.attachments);
147
+ const bodyWithAttachments = attachmentDesc ? `${entry.body} ${attachmentDesc}` : entry.body;
148
+ return deps.runtime.channel.reply.formatInboundEnvelope({
149
+ channel: "qqbot",
150
+ from: entry.sender,
151
+ timestamp: entry.timestamp,
152
+ body: bodyWithAttachments,
153
+ chatType: "group",
154
+ envelope: envelopeOpts,
155
+ });
156
+ }
@@ -0,0 +1,77 @@
1
+ /**
2
+ * Content stage — build the user-visible message body.
3
+ *
4
+ * Responsible for:
5
+ * 1. Parsing QQ emoji tags (`<faceType=...>` → `[Emoji: name]`)
6
+ * 2. Appending attachment info + voice transcripts
7
+ * 3. Stripping `<@openid>` mention tags in group messages
8
+ * 4. Replacing `<@openid>` → `@nickname` in DMs (best-effort)
9
+ *
10
+ * Pure function: same input → same output, no I/O.
11
+ */
12
+
13
+ import { stripMentionText } from "../../group/mention.js";
14
+ import { parseFaceTags } from "../../utils/text-parsing.js";
15
+ import { formatVoiceText } from "../../utils/voice-text.js";
16
+ import type { QueuedMention, QueuedMessage } from "../message-queue.js";
17
+
18
+ // ─────────────────────────── Types ───────────────────────────
19
+
20
+ /** Input for {@link buildUserContent}. */
21
+ interface ContentStageInput {
22
+ event: QueuedMessage;
23
+ /** `attachmentInfo` from the attachment stage — appended verbatim. */
24
+ attachmentInfo: string;
25
+ /** Voice transcripts collected from the attachment stage. */
26
+ voiceTranscripts: string[];
27
+ }
28
+
29
+ /** Output of {@link buildUserContent}. */
30
+ interface ContentStageOutput {
31
+ /** `parseFaceTags(event.content)`. */
32
+ parsedContent: string;
33
+ /** Full user-visible content (parsed + voice + attachments + mention cleanup). */
34
+ userContent: string;
35
+ }
36
+
37
+ // ─────────────────────────── Stage ───────────────────────────
38
+
39
+ /**
40
+ * Build both the raw-parsed content and the fully composed user-visible
41
+ * body that downstream stages feed to the AI and to the envelope.
42
+ */
43
+ export function buildUserContent(input: ContentStageInput): ContentStageOutput {
44
+ const { event, attachmentInfo, voiceTranscripts } = input;
45
+
46
+ const parsedContent = parseFaceTags(event.content);
47
+ const voiceText = formatVoiceText(voiceTranscripts);
48
+
49
+ let userContent = voiceText
50
+ ? (parsedContent.trim() ? `${parsedContent}\n${voiceText}` : voiceText) + attachmentInfo
51
+ : parsedContent + attachmentInfo;
52
+
53
+ // Mention cleanup — only for events with mentions attached.
54
+ if (event.type === "group" && event.mentions?.length) {
55
+ userContent = stripMentionText(userContent, event.mentions as never) ?? userContent;
56
+ } else if (event.mentions?.length) {
57
+ userContent = replaceMentionsWithNicknames(userContent, event.mentions);
58
+ }
59
+
60
+ return { parsedContent, userContent };
61
+ }
62
+
63
+ // ─────────────────────────── Internal ───────────────────────────
64
+
65
+ function replaceMentionsWithNicknames(text: string, mentions: QueuedMention[]): string {
66
+ let out = text;
67
+ for (const m of mentions) {
68
+ if (m.member_openid && m.username) {
69
+ out = out.replace(new RegExp(`<@${escapeRegex(m.member_openid)}>`, "g"), `@${m.username}`);
70
+ }
71
+ }
72
+ return out;
73
+ }
74
+
75
+ function escapeRegex(str: string): string {
76
+ return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
77
+ }
@@ -0,0 +1,144 @@
1
+ /**
2
+ * Envelope stage — render the Web UI body, the dynamic-context block,
3
+ * the final group system prompt, and the media classification arrays.
4
+ *
5
+ * All logic here is presentation-layer glue: it combines fields built by
6
+ * earlier stages into the display-friendly strings the outbound
7
+ * dispatcher needs. No decisions / gating.
8
+ */
9
+
10
+ import type { ProcessedAttachments } from "../inbound-attachments.js";
11
+ import type { InboundGroupInfo, InboundPipelineDeps, ReplyToInfo } from "../inbound-context.js";
12
+ import type { QueuedMessage } from "../message-queue.js";
13
+
14
+ // ─────────────────────────── Envelope body ───────────────────────────
15
+
16
+ interface BuildBodyInput {
17
+ event: QueuedMessage;
18
+ deps: InboundPipelineDeps;
19
+ userContent: string;
20
+ isGroupChat: boolean;
21
+ imageUrls: string[];
22
+ }
23
+
24
+ /** Format the inbound envelope (Web UI body). */
25
+ export function buildBody(input: BuildBodyInput): string {
26
+ const { event, deps, userContent, isGroupChat, imageUrls } = input;
27
+ const envelopeOptions = deps.runtime.channel.reply.resolveEnvelopeFormatOptions(deps.cfg);
28
+ return deps.runtime.channel.reply.formatInboundEnvelope({
29
+ channel: "qqbot",
30
+ from: event.senderName ?? event.senderId,
31
+ timestamp: new Date(event.timestamp).getTime(),
32
+ body: userContent,
33
+ chatType: isGroupChat ? "group" : "direct",
34
+ sender: { id: event.senderId, name: event.senderName },
35
+ envelope: envelopeOptions,
36
+ ...(imageUrls.length > 0 ? { imageUrls } : {}),
37
+ });
38
+ }
39
+
40
+ // ─────────────────────────── Quote / dynamic ctx ───────────────────────────
41
+
42
+ /** Render the `[Quoted message begins]...[ends]` block (empty if no reply-to). */
43
+ export function buildQuotePart(replyTo?: ReplyToInfo): string {
44
+ if (!replyTo) {
45
+ return "";
46
+ }
47
+ return replyTo.body
48
+ ? `[Quoted message begins]\n${replyTo.body}\n[Quoted message ends]\n`
49
+ : `[Quoted message begins]\nOriginal content unavailable\n[Quoted message ends]\n`;
50
+ }
51
+
52
+ interface BuildDynamicCtxInput {
53
+ imageUrls: string[];
54
+ uniqueVoicePaths: string[];
55
+ uniqueVoiceUrls: string[];
56
+ uniqueVoiceAsrReferTexts: string[];
57
+ }
58
+
59
+ /** Render the per-message dynamic metadata block (images / voice / ASR). */
60
+ export function buildDynamicCtx(input: BuildDynamicCtxInput): string {
61
+ const lines: string[] = [];
62
+ if (input.imageUrls.length > 0) {
63
+ lines.push(`- Images: ${input.imageUrls.join(", ")}`);
64
+ }
65
+ if (input.uniqueVoicePaths.length > 0 || input.uniqueVoiceUrls.length > 0) {
66
+ lines.push(`- Voice: ${[...input.uniqueVoicePaths, ...input.uniqueVoiceUrls].join(", ")}`);
67
+ }
68
+ if (input.uniqueVoiceAsrReferTexts.length > 0) {
69
+ lines.push(`- ASR: ${input.uniqueVoiceAsrReferTexts.join(" | ")}`);
70
+ }
71
+ return lines.length > 0 ? lines.join("\n") + "\n\n" : "";
72
+ }
73
+
74
+ // ─────────────────────────── System prompt ───────────────────────────
75
+
76
+ /** Combine account-level system prompt with group-specific prompts. */
77
+ export function buildGroupSystemPrompt(
78
+ accountSystemInstruction: string,
79
+ groupInfo: InboundGroupInfo | undefined,
80
+ ): string | undefined {
81
+ const parts: string[] = [];
82
+ if (accountSystemInstruction) {
83
+ parts.push(accountSystemInstruction);
84
+ }
85
+ if (groupInfo?.display.introHint) {
86
+ parts.push(groupInfo.display.introHint);
87
+ }
88
+ if (groupInfo?.display.behaviorPrompt) {
89
+ parts.push(groupInfo.display.behaviorPrompt);
90
+ }
91
+ const combined = parts.filter(Boolean).join("\n");
92
+ return combined || undefined;
93
+ }
94
+
95
+ // ─────────────────────────── Media classification ───────────────────────────
96
+
97
+ interface MediaClassification {
98
+ localMediaPaths: string[];
99
+ localMediaTypes: string[];
100
+ remoteMediaUrls: string[];
101
+ remoteMediaTypes: string[];
102
+ uniqueVoicePaths: string[];
103
+ uniqueVoiceUrls: string[];
104
+ uniqueVoiceAsrReferTexts: string[];
105
+ voiceMediaTypes: string[];
106
+ hasAsrReferFallback: boolean;
107
+ voiceTranscriptSources: string[];
108
+ }
109
+
110
+ /** Classify image URLs into local vs remote and de-duplicate voice arrays. */
111
+ export function classifyMedia(processed: ProcessedAttachments): MediaClassification {
112
+ const localMediaPaths: string[] = [];
113
+ const localMediaTypes: string[] = [];
114
+ const remoteMediaUrls: string[] = [];
115
+ const remoteMediaTypes: string[] = [];
116
+ for (let i = 0; i < processed.imageUrls.length; i++) {
117
+ const u = processed.imageUrls[i];
118
+ const t = processed.imageMediaTypes[i] ?? "image/png";
119
+ if (u.startsWith("http://") || u.startsWith("https://")) {
120
+ remoteMediaUrls.push(u);
121
+ remoteMediaTypes.push(t);
122
+ } else {
123
+ localMediaPaths.push(u);
124
+ localMediaTypes.push(t);
125
+ }
126
+ }
127
+
128
+ const uniqueVoicePaths = [...new Set(processed.voiceAttachmentPaths)];
129
+ const uniqueVoiceUrls = [...new Set(processed.voiceAttachmentUrls)];
130
+ const voiceMediaTypes = [...uniqueVoicePaths, ...uniqueVoiceUrls].map(() => "audio/wav");
131
+
132
+ return {
133
+ localMediaPaths,
134
+ localMediaTypes,
135
+ remoteMediaUrls,
136
+ remoteMediaTypes,
137
+ uniqueVoicePaths,
138
+ uniqueVoiceUrls,
139
+ uniqueVoiceAsrReferTexts: [...new Set(processed.voiceAsrReferTexts)].filter(Boolean),
140
+ voiceMediaTypes,
141
+ hasAsrReferFallback: processed.voiceTranscriptSources.includes("asr"),
142
+ voiceTranscriptSources: processed.voiceTranscriptSources,
143
+ };
144
+ }