@openclaw/qqbot 2026.5.2 → 2026.5.3-beta.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.
Files changed (203) hide show
  1. package/dist/api.js +646 -0
  2. package/dist/approval-cg0SVahb.js +94 -0
  3. package/dist/channel-N6Y_Rcjp.js +551 -0
  4. package/dist/channel-plugin-api.js +2 -0
  5. package/dist/channel.setup-ByCRAe5H.js +25 -0
  6. package/dist/config-D6545NkC.js +94 -0
  7. package/dist/config-schema-DFcjQw73.js +309 -0
  8. package/dist/exec-approvals-COUsM6wZ.js +138 -0
  9. package/dist/gateway-CmSUJKSt.js +6726 -0
  10. package/dist/handler-runtime-Bqm6N0WG.js +120 -0
  11. package/dist/index.js +29 -0
  12. package/dist/narrowing-BoieBTIU.js +25 -0
  13. package/dist/outbound-BJfhwrPg.js +1357 -0
  14. package/dist/request-context-DXtpwWui.js +921 -0
  15. package/dist/resolve-D_06fV6-.js +284 -0
  16. package/dist/runtime-BJAS3eXW.js +18 -0
  17. package/dist/runtime-api.js +2 -0
  18. package/dist/secret-contract-api.js +56 -0
  19. package/dist/sender-p-B14eLG.js +1987 -0
  20. package/dist/setup-entry.js +15 -0
  21. package/dist/setup-plugin-api.js +2 -0
  22. package/dist/string-normalize-Ci6NM5DE.js +98 -0
  23. package/dist/target-parser-Y0prnrXD.js +245 -0
  24. package/package.json +15 -6
  25. package/api.ts +0 -56
  26. package/channel-plugin-api.ts +0 -1
  27. package/index.ts +0 -29
  28. package/runtime-api.ts +0 -9
  29. package/setup-entry.ts +0 -9
  30. package/setup-plugin-api.ts +0 -3
  31. package/src/bridge/approval/capability.ts +0 -237
  32. package/src/bridge/approval/handler-runtime.ts +0 -204
  33. package/src/bridge/bootstrap.ts +0 -135
  34. package/src/bridge/channel-entry.ts +0 -18
  35. package/src/bridge/commands/framework-context-adapter.ts +0 -60
  36. package/src/bridge/commands/framework-registration.ts +0 -47
  37. package/src/bridge/commands/from-parser.test.ts +0 -86
  38. package/src/bridge/commands/from-parser.ts +0 -60
  39. package/src/bridge/commands/result-dispatcher.ts +0 -76
  40. package/src/bridge/config-shared.ts +0 -132
  41. package/src/bridge/config.ts +0 -111
  42. package/src/bridge/gateway.ts +0 -174
  43. package/src/bridge/logger.ts +0 -31
  44. package/src/bridge/narrowing.ts +0 -31
  45. package/src/bridge/plugin-version.test.ts +0 -146
  46. package/src/bridge/plugin-version.ts +0 -102
  47. package/src/bridge/runtime.ts +0 -25
  48. package/src/bridge/sdk-adapter.ts +0 -131
  49. package/src/bridge/setup/finalize.ts +0 -144
  50. package/src/bridge/setup/surface.ts +0 -34
  51. package/src/bridge/tools/channel.ts +0 -58
  52. package/src/bridge/tools/index.ts +0 -15
  53. package/src/bridge/tools/remind.test.ts +0 -124
  54. package/src/bridge/tools/remind.ts +0 -91
  55. package/src/channel.setup.ts +0 -33
  56. package/src/channel.ts +0 -288
  57. package/src/command-auth.test.ts +0 -62
  58. package/src/config-schema.ts +0 -84
  59. package/src/config.test.ts +0 -364
  60. package/src/engine/access/access-control.test.ts +0 -198
  61. package/src/engine/access/access-control.ts +0 -226
  62. package/src/engine/access/index.ts +0 -16
  63. package/src/engine/access/resolve-policy.test.ts +0 -59
  64. package/src/engine/access/resolve-policy.ts +0 -57
  65. package/src/engine/access/sender-match.test.ts +0 -60
  66. package/src/engine/access/sender-match.ts +0 -55
  67. package/src/engine/access/types.ts +0 -53
  68. package/src/engine/adapter/audio.port.ts +0 -27
  69. package/src/engine/adapter/commands.port.ts +0 -22
  70. package/src/engine/adapter/history.port.ts +0 -52
  71. package/src/engine/adapter/index.ts +0 -139
  72. package/src/engine/adapter/mention-gate.port.ts +0 -50
  73. package/src/engine/adapter/types.ts +0 -38
  74. package/src/engine/api/api-client.ts +0 -212
  75. package/src/engine/api/media-chunked.test.ts +0 -336
  76. package/src/engine/api/media-chunked.ts +0 -622
  77. package/src/engine/api/media.ts +0 -218
  78. package/src/engine/api/messages.ts +0 -293
  79. package/src/engine/api/retry.ts +0 -217
  80. package/src/engine/api/routes.ts +0 -95
  81. package/src/engine/api/token.ts +0 -271
  82. package/src/engine/approval/index.test.ts +0 -22
  83. package/src/engine/approval/index.ts +0 -224
  84. package/src/engine/commands/builtin/log-helpers.ts +0 -319
  85. package/src/engine/commands/builtin/register-all.ts +0 -17
  86. package/src/engine/commands/builtin/register-approve.ts +0 -201
  87. package/src/engine/commands/builtin/register-basic.ts +0 -95
  88. package/src/engine/commands/builtin/register-clear-storage.ts +0 -187
  89. package/src/engine/commands/builtin/register-logs.ts +0 -20
  90. package/src/engine/commands/builtin/register-streaming.ts +0 -137
  91. package/src/engine/commands/builtin/state.ts +0 -31
  92. package/src/engine/commands/slash-command-auth.ts +0 -48
  93. package/src/engine/commands/slash-command-handler.ts +0 -146
  94. package/src/engine/commands/slash-commands-impl.test.ts +0 -8
  95. package/src/engine/commands/slash-commands-impl.ts +0 -61
  96. package/src/engine/commands/slash-commands.ts +0 -199
  97. package/src/engine/config/credential-backup.test.ts +0 -88
  98. package/src/engine/config/credential-backup.ts +0 -107
  99. package/src/engine/config/credentials.ts +0 -76
  100. package/src/engine/config/group.test.ts +0 -234
  101. package/src/engine/config/group.ts +0 -299
  102. package/src/engine/config/resolve.test.ts +0 -152
  103. package/src/engine/config/resolve.ts +0 -283
  104. package/src/engine/config/setup-logic.ts +0 -84
  105. package/src/engine/engine-import-boundary.test.ts +0 -73
  106. package/src/engine/gateway/codec.ts +0 -47
  107. package/src/engine/gateway/constants.ts +0 -117
  108. package/src/engine/gateway/event-dispatcher.ts +0 -177
  109. package/src/engine/gateway/gateway-connection.ts +0 -371
  110. package/src/engine/gateway/gateway.ts +0 -291
  111. package/src/engine/gateway/inbound-attachments.test.ts +0 -126
  112. package/src/engine/gateway/inbound-attachments.ts +0 -360
  113. package/src/engine/gateway/inbound-context.ts +0 -195
  114. package/src/engine/gateway/inbound-pipeline.self-echo.test.ts +0 -218
  115. package/src/engine/gateway/inbound-pipeline.ts +0 -235
  116. package/src/engine/gateway/interaction-handler.ts +0 -220
  117. package/src/engine/gateway/message-queue.test.ts +0 -282
  118. package/src/engine/gateway/message-queue.ts +0 -499
  119. package/src/engine/gateway/outbound-dispatch.test.ts +0 -231
  120. package/src/engine/gateway/outbound-dispatch.ts +0 -575
  121. package/src/engine/gateway/reconnect.ts +0 -199
  122. package/src/engine/gateway/stages/access-stage.ts +0 -132
  123. package/src/engine/gateway/stages/assembly-stage.ts +0 -156
  124. package/src/engine/gateway/stages/content-stage.test.ts +0 -77
  125. package/src/engine/gateway/stages/content-stage.ts +0 -77
  126. package/src/engine/gateway/stages/envelope-stage.test.ts +0 -152
  127. package/src/engine/gateway/stages/envelope-stage.ts +0 -144
  128. package/src/engine/gateway/stages/group-gate-stage.ts +0 -292
  129. package/src/engine/gateway/stages/index.ts +0 -18
  130. package/src/engine/gateway/stages/quote-stage.ts +0 -113
  131. package/src/engine/gateway/stages/refidx-stage.ts +0 -62
  132. package/src/engine/gateway/stages/stub-contexts.ts +0 -116
  133. package/src/engine/gateway/types.ts +0 -264
  134. package/src/engine/gateway/typing-keepalive.ts +0 -79
  135. package/src/engine/group/activation.test.ts +0 -114
  136. package/src/engine/group/activation.ts +0 -147
  137. package/src/engine/group/history.test.ts +0 -314
  138. package/src/engine/group/history.ts +0 -321
  139. package/src/engine/group/mention.test.ts +0 -141
  140. package/src/engine/group/mention.ts +0 -197
  141. package/src/engine/group/message-gating.test.ts +0 -188
  142. package/src/engine/group/message-gating.ts +0 -216
  143. package/src/engine/messaging/decode-media-path.ts +0 -82
  144. package/src/engine/messaging/media-source.ts +0 -215
  145. package/src/engine/messaging/media-type-detect.ts +0 -37
  146. package/src/engine/messaging/outbound-audio-port.ts +0 -38
  147. package/src/engine/messaging/outbound-deliver.ts +0 -810
  148. package/src/engine/messaging/outbound-media-send.ts +0 -702
  149. package/src/engine/messaging/outbound-reply.ts +0 -27
  150. package/src/engine/messaging/outbound-result-helpers.ts +0 -54
  151. package/src/engine/messaging/outbound-types.ts +0 -45
  152. package/src/engine/messaging/outbound.ts +0 -485
  153. package/src/engine/messaging/reply-dispatcher.ts +0 -597
  154. package/src/engine/messaging/reply-limiter.ts +0 -164
  155. package/src/engine/messaging/sender.ts +0 -729
  156. package/src/engine/messaging/streaming-c2c.ts +0 -1192
  157. package/src/engine/messaging/streaming-media-send.ts +0 -544
  158. package/src/engine/messaging/target-parser.ts +0 -104
  159. package/src/engine/ref/format-message-ref.ts +0 -142
  160. package/src/engine/ref/format-ref-entry.test.ts +0 -60
  161. package/src/engine/ref/format-ref-entry.ts +0 -27
  162. package/src/engine/ref/store.ts +0 -208
  163. package/src/engine/ref/types.ts +0 -27
  164. package/src/engine/session/known-users.ts +0 -137
  165. package/src/engine/session/session-store.ts +0 -204
  166. package/src/engine/tools/channel-api.ts +0 -244
  167. package/src/engine/tools/remind-logic.test.ts +0 -280
  168. package/src/engine/tools/remind-logic.ts +0 -377
  169. package/src/engine/types.ts +0 -313
  170. package/src/engine/utils/attachment-tags.test.ts +0 -186
  171. package/src/engine/utils/attachment-tags.ts +0 -174
  172. package/src/engine/utils/audio.test.ts +0 -250
  173. package/src/engine/utils/audio.ts +0 -585
  174. package/src/engine/utils/data-paths.ts +0 -38
  175. package/src/engine/utils/diagnostics.ts +0 -109
  176. package/src/engine/utils/file-utils.test.ts +0 -72
  177. package/src/engine/utils/file-utils.ts +0 -225
  178. package/src/engine/utils/format.test.ts +0 -68
  179. package/src/engine/utils/format.ts +0 -70
  180. package/src/engine/utils/image-size.test.ts +0 -158
  181. package/src/engine/utils/image-size.ts +0 -249
  182. package/src/engine/utils/log.test.ts +0 -28
  183. package/src/engine/utils/log.ts +0 -61
  184. package/src/engine/utils/media-tags.test.ts +0 -32
  185. package/src/engine/utils/media-tags.ts +0 -177
  186. package/src/engine/utils/payload.test.ts +0 -68
  187. package/src/engine/utils/payload.ts +0 -145
  188. package/src/engine/utils/platform-storage-laziness.test.ts +0 -63
  189. package/src/engine/utils/platform.test.ts +0 -148
  190. package/src/engine/utils/platform.ts +0 -343
  191. package/src/engine/utils/request-context.ts +0 -60
  192. package/src/engine/utils/string-normalize.ts +0 -91
  193. package/src/engine/utils/stt.test.ts +0 -104
  194. package/src/engine/utils/stt.ts +0 -100
  195. package/src/engine/utils/text-parsing.test.ts +0 -29
  196. package/src/engine/utils/text-parsing.ts +0 -155
  197. package/src/engine/utils/upload-cache.ts +0 -96
  198. package/src/engine/utils/voice-text.ts +0 -15
  199. package/src/exec-approvals.ts +0 -218
  200. package/src/manifest-schema.test.ts +0 -56
  201. package/src/qqbot-test-support.ts +0 -29
  202. package/src/types.ts +0 -210
  203. package/tsconfig.json +0 -16
@@ -1,114 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import { resolveGroupActivation, type SessionStoreReader } from "./activation.js";
3
-
4
- describe("engine/group/activation", () => {
5
- describe("resolveGroupActivation — no reader", () => {
6
- it("maps configRequireMention=true → mention", () => {
7
- expect(
8
- resolveGroupActivation({
9
- cfg: {},
10
- agentId: "main",
11
- sessionKey: "s",
12
- configRequireMention: true,
13
- }),
14
- ).toBe("mention");
15
- });
16
-
17
- it("maps configRequireMention=false → always", () => {
18
- expect(
19
- resolveGroupActivation({
20
- cfg: {},
21
- agentId: "main",
22
- sessionKey: "s",
23
- configRequireMention: false,
24
- }),
25
- ).toBe("always");
26
- });
27
- });
28
-
29
- describe("resolveGroupActivation — with reader", () => {
30
- const makeReader = (
31
- store: Record<string, { groupActivation?: string }> | null,
32
- ): SessionStoreReader => ({
33
- read: () => store,
34
- });
35
-
36
- it("honours explicit session-store override (mention)", () => {
37
- const reader = makeReader({ k1: { groupActivation: "mention" } });
38
- expect(
39
- resolveGroupActivation({
40
- cfg: {},
41
- agentId: "main",
42
- sessionKey: "k1",
43
- configRequireMention: false,
44
- sessionStoreReader: reader,
45
- }),
46
- ).toBe("mention");
47
- });
48
-
49
- it("honours explicit session-store override (always)", () => {
50
- const reader = makeReader({ k1: { groupActivation: "always" } });
51
- expect(
52
- resolveGroupActivation({
53
- cfg: {},
54
- agentId: "main",
55
- sessionKey: "k1",
56
- configRequireMention: true,
57
- sessionStoreReader: reader,
58
- }),
59
- ).toBe("always");
60
- });
61
-
62
- it("ignores override when the key is absent", () => {
63
- const reader = makeReader({});
64
- expect(
65
- resolveGroupActivation({
66
- cfg: {},
67
- agentId: "main",
68
- sessionKey: "MISSING",
69
- configRequireMention: true,
70
- sessionStoreReader: reader,
71
- }),
72
- ).toBe("mention");
73
- });
74
-
75
- it("ignores reader errors (null) and falls back", () => {
76
- const reader = makeReader(null);
77
- expect(
78
- resolveGroupActivation({
79
- cfg: {},
80
- agentId: "main",
81
- sessionKey: "k1",
82
- configRequireMention: false,
83
- sessionStoreReader: reader,
84
- }),
85
- ).toBe("always");
86
- });
87
-
88
- it("ignores invalid activation values", () => {
89
- const reader = makeReader({ k1: { groupActivation: "weird-mode" } });
90
- expect(
91
- resolveGroupActivation({
92
- cfg: {},
93
- agentId: "main",
94
- sessionKey: "k1",
95
- configRequireMention: true,
96
- sessionStoreReader: reader,
97
- }),
98
- ).toBe("mention");
99
- });
100
-
101
- it("normalizes whitespace / case", () => {
102
- const reader = makeReader({ k1: { groupActivation: " Always " } });
103
- expect(
104
- resolveGroupActivation({
105
- cfg: {},
106
- agentId: "main",
107
- sessionKey: "k1",
108
- configRequireMention: true,
109
- sessionStoreReader: reader,
110
- }),
111
- ).toBe("always");
112
- });
113
- });
114
- });
@@ -1,147 +0,0 @@
1
- /**
2
- * Group activation mode — how the bot decides whether to respond in a group.
3
- *
4
- * Resolution chain:
5
- * 1. session store override (`/activation` command writes per-session
6
- * `groupActivation` value) — highest priority
7
- * 2. per-group `requireMention` config
8
- * 3. `"mention"` default (require @-bot to respond)
9
- *
10
- * File I/O is isolated in the default node-based reader so the gating
11
- * logic itself stays a pure function, testable without touching disk.
12
- *
13
- * Note: the implicit-mention predicate (quoting a bot message counts as
14
- * @-ing the bot) lives in `./mention.ts` alongside the other mention
15
- * helpers — see `resolveImplicitMention` there.
16
- */
17
-
18
- import fs from "node:fs";
19
- import path from "node:path";
20
-
21
- // ────────────────────────── Types ──────────────────────────
22
-
23
- /** High-level activation outcome. */
24
- export type GroupActivationMode = "mention" | "always";
25
-
26
- /**
27
- * Pluggable reader that returns parsed session-store contents.
28
- *
29
- * A return value of `null` means "no override available" (file missing,
30
- * parse error, or reader disabled). Implementations must **not** throw —
31
- * the gating pipeline treats any failure as "fall back to the config
32
- * default".
33
- */
34
- export interface SessionStoreReader {
35
- read(params: {
36
- cfg: Record<string, unknown>;
37
- agentId: string;
38
- }): Record<string, { groupActivation?: string }> | null;
39
- }
40
-
41
- // ────────────────────────── groupActivation ──────────────────────────
42
-
43
- /**
44
- * Resolve the effective activation mode for one inbound message.
45
- *
46
- * Order of precedence:
47
- * 1. `store[sessionKey].groupActivation` (read via the injected reader)
48
- * 2. config-level `requireMention` (maps to `"mention"` / `"always"`)
49
- * 3. `"mention"` (safe default)
50
- */
51
- export function resolveGroupActivation(params: {
52
- cfg: Record<string, unknown>;
53
- agentId: string;
54
- sessionKey: string;
55
- configRequireMention: boolean;
56
- /** Pluggable reader; omit to disable the session-store override. */
57
- sessionStoreReader?: SessionStoreReader;
58
- }): GroupActivationMode {
59
- const fallback: GroupActivationMode = params.configRequireMention ? "mention" : "always";
60
-
61
- const store = params.sessionStoreReader?.read({
62
- cfg: params.cfg,
63
- agentId: params.agentId,
64
- });
65
- if (!store) {
66
- return fallback;
67
- }
68
-
69
- const entry = store[params.sessionKey];
70
- if (!entry?.groupActivation) {
71
- return fallback;
72
- }
73
-
74
- const normalized = entry.groupActivation.trim().toLowerCase();
75
- if (normalized === "mention" || normalized === "always") {
76
- return normalized;
77
- }
78
- return fallback;
79
- }
80
-
81
- // ────────────────────────── Default node reader ──────────────────────────
82
-
83
- /**
84
- * Resolve the on-disk path to the agent-sessions file.
85
- *
86
- * Priority:
87
- * 1. `cfg.session.store` (supports `{agentId}` placeholder and `~` expansion)
88
- * 2. `$OPENCLAW_STATE_DIR` / `$CLAWDBOT_STATE_DIR`
89
- * 3. `~/.openclaw/agents/{agentId}/sessions/sessions.json`
90
- */
91
- function resolveSessionStorePath(
92
- cfg: Record<string, unknown>,
93
- agentId: string | undefined,
94
- ): string {
95
- const resolvedAgentId = agentId || "default";
96
-
97
- const session =
98
- typeof cfg.session === "object" && cfg.session !== null
99
- ? (cfg.session as { store?: unknown })
100
- : undefined;
101
- const rawStore = typeof session?.store === "string" ? session.store : undefined;
102
-
103
- if (rawStore) {
104
- let expanded = rawStore;
105
- if (expanded.includes("{agentId}")) {
106
- expanded = expanded.replaceAll("{agentId}", resolvedAgentId);
107
- }
108
- if (expanded.startsWith("~")) {
109
- const home = process.env.HOME || process.env.USERPROFILE || "";
110
- expanded = expanded.replace(/^~/, home);
111
- }
112
- return path.resolve(expanded);
113
- }
114
-
115
- const stateDir =
116
- process.env.OPENCLAW_STATE_DIR?.trim() ||
117
- process.env.CLAWDBOT_STATE_DIR?.trim() ||
118
- path.join(process.env.HOME || process.env.USERPROFILE || "", ".openclaw");
119
- return path.join(stateDir, "agents", resolvedAgentId, "sessions", "sessions.json");
120
- }
121
-
122
- /**
123
- * Create the default, production-ready session-store reader.
124
- *
125
- * Reads the file synchronously on every call. The overhead is acceptable
126
- * because activation mode is only resolved once per group message and
127
- * the sessions file is typically a handful of kilobytes.
128
- *
129
- * Any I/O or JSON error is swallowed and returned as `null` so the
130
- * gating pipeline falls back to the config default.
131
- */
132
- export function createNodeSessionStoreReader(): SessionStoreReader {
133
- return {
134
- read: ({ cfg, agentId }) => {
135
- try {
136
- const storePath = resolveSessionStorePath(cfg, agentId);
137
- if (!fs.existsSync(storePath)) {
138
- return null;
139
- }
140
- const raw = fs.readFileSync(storePath, "utf-8");
141
- return JSON.parse(raw) as Record<string, { groupActivation?: string }>;
142
- } catch {
143
- return null;
144
- }
145
- },
146
- };
147
- }
@@ -1,314 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import {
3
- buildMergedMessageContext,
4
- buildPendingHistoryContext,
5
- clearPendingHistory,
6
- formatAttachmentTags,
7
- formatMessageContent,
8
- inferAttachmentType,
9
- recordPendingHistoryEntry,
10
- toAttachmentSummaries,
11
- type HistoryEntry,
12
- } from "./history.js";
13
-
14
- function makeMap(): Map<string, HistoryEntry[]> {
15
- return new Map();
16
- }
17
-
18
- function entry(sender: string, body: string, extras: Partial<HistoryEntry> = {}): HistoryEntry {
19
- return { sender, body, ...extras };
20
- }
21
-
22
- describe("engine/group/history", () => {
23
- describe("inferAttachmentType", () => {
24
- it("maps image/* → image", () => {
25
- expect(inferAttachmentType("image/png")).toBe("image");
26
- });
27
-
28
- it("maps voice / audio / silk / amr → voice", () => {
29
- expect(inferAttachmentType("voice")).toBe("voice");
30
- expect(inferAttachmentType("audio/mpeg")).toBe("voice");
31
- expect(inferAttachmentType("application/silk")).toBe("voice");
32
- expect(inferAttachmentType("audio/amr")).toBe("voice");
33
- });
34
-
35
- it("maps video / application / text → their category", () => {
36
- expect(inferAttachmentType("video/mp4")).toBe("video");
37
- expect(inferAttachmentType("application/pdf")).toBe("file");
38
- expect(inferAttachmentType("text/plain")).toBe("file");
39
- });
40
-
41
- it("unknown content types fall back to unknown", () => {
42
- expect(inferAttachmentType()).toBe("unknown");
43
- expect(inferAttachmentType("weird/thing")).toBe("unknown");
44
- });
45
- });
46
-
47
- describe("toAttachmentSummaries", () => {
48
- it("returns undefined for empty input", () => {
49
- expect(toAttachmentSummaries()).toBeUndefined();
50
- expect(toAttachmentSummaries([])).toBeUndefined();
51
- });
52
-
53
- it("normalizes raw fields", () => {
54
- const result = toAttachmentSummaries([
55
- {
56
- content_type: "image/png",
57
- filename: "a.png",
58
- url: "https://x/a.png",
59
- },
60
- {
61
- content_type: "voice",
62
- asr_refer_text: "hello",
63
- },
64
- ]);
65
- expect(result).toEqual([
66
- { type: "image", filename: "a.png", transcript: undefined, url: "https://x/a.png" },
67
- { type: "voice", filename: undefined, transcript: "hello", url: undefined },
68
- ]);
69
- });
70
- });
71
-
72
- describe("formatAttachmentTags", () => {
73
- it("returns empty string for empty input", () => {
74
- expect(formatAttachmentTags()).toBe("");
75
- expect(formatAttachmentTags([])).toBe("");
76
- });
77
-
78
- it("renders MEDIA:path for entries with a source", () => {
79
- expect(formatAttachmentTags([{ type: "image", localPath: "/tmp/a.png" }])).toBe(
80
- "MEDIA:/tmp/a.png",
81
- );
82
- expect(formatAttachmentTags([{ type: "image", url: "https://x/b.png" }])).toBe(
83
- "MEDIA:https://x/b.png",
84
- );
85
- });
86
-
87
- it("inlines transcript for voice w/ source", () => {
88
- expect(
89
- formatAttachmentTags([{ type: "voice", localPath: "/tmp/v.wav", transcript: "hi" }]),
90
- ).toBe('MEDIA:/tmp/v.wav (transcript: "hi")');
91
- });
92
-
93
- it("uses descriptive tags when no source is available", () => {
94
- expect(formatAttachmentTags([{ type: "image" }])).toBe("[image]");
95
- expect(formatAttachmentTags([{ type: "image", filename: "a.png" }])).toBe("[image: a.png]");
96
- expect(formatAttachmentTags([{ type: "voice" }])).toBe("[voice]");
97
- expect(formatAttachmentTags([{ type: "voice", transcript: "t" }])).toBe(
98
- '[voice (transcript: "t")]',
99
- );
100
- expect(formatAttachmentTags([{ type: "video" }])).toBe("[video]");
101
- expect(formatAttachmentTags([{ type: "file", filename: "b.pdf" }])).toBe("[file: b.pdf]");
102
- expect(formatAttachmentTags([{ type: "unknown" }])).toBe("[attachment]");
103
- });
104
-
105
- it("joins multiple entries with newline", () => {
106
- expect(
107
- formatAttachmentTags([
108
- { type: "image", localPath: "/tmp/a.png" },
109
- { type: "voice", transcript: "hi" },
110
- ]),
111
- ).toBe('MEDIA:/tmp/a.png\n[voice (transcript: "hi")]');
112
- });
113
- });
114
-
115
- describe("formatMessageContent", () => {
116
- it("passes content through parseFaceTags (no-op for plain text)", () => {
117
- // parseFaceTags only rewrites the `<faceType=...>` tag form; plain
118
- // text must round-trip unchanged so regressions in the pipeline
119
- // don't silently mangle user input.
120
- expect(formatMessageContent({ content: "hello world" })).toBe("hello world");
121
- });
122
-
123
- it("strips mentions only for group chat", () => {
124
- expect(
125
- formatMessageContent({
126
- content: "<@X>hi",
127
- chatType: "group",
128
- mentions: [{ member_openid: "X", is_you: true }],
129
- }),
130
- ).toBe("hi");
131
- // Non-group: strip is NOT applied.
132
- expect(
133
- formatMessageContent({
134
- content: "<@X>hi",
135
- chatType: "c2c",
136
- mentions: [{ member_openid: "X", is_you: true }],
137
- }),
138
- ).toBe("<@X>hi");
139
- });
140
-
141
- it("appends attachment tags", () => {
142
- expect(
143
- formatMessageContent({
144
- content: "see",
145
- attachments: [{ content_type: "image/png", url: "https://x/a.png" }],
146
- }),
147
- ).toBe("see MEDIA:https://x/a.png");
148
- });
149
- });
150
-
151
- describe("recordPendingHistoryEntry / buildPendingHistoryContext", () => {
152
- it("no-ops when limit is 0", () => {
153
- const map = makeMap();
154
- const entries = recordPendingHistoryEntry({
155
- historyMap: map,
156
- historyKey: "G",
157
- entry: entry("A", "hi"),
158
- limit: 0,
159
- });
160
- expect(entries).toEqual([]);
161
- expect(map.size).toBe(0);
162
- });
163
-
164
- it("no-ops when entry is null", () => {
165
- const map = makeMap();
166
- recordPendingHistoryEntry({
167
- historyMap: map,
168
- historyKey: "G",
169
- entry: null,
170
- limit: 10,
171
- });
172
- expect(map.size).toBe(0);
173
- });
174
-
175
- it("appends and caps at the limit", () => {
176
- const map = makeMap();
177
- for (let i = 0; i < 5; i++) {
178
- recordPendingHistoryEntry({
179
- historyMap: map,
180
- historyKey: "G",
181
- entry: entry("A", `m${i}`),
182
- limit: 3,
183
- });
184
- }
185
- const entries = map.get("G")!;
186
- expect(entries).toHaveLength(3);
187
- expect(entries.map((e) => e.body)).toEqual(["m2", "m3", "m4"]);
188
- });
189
-
190
- it("builds a history-wrapped message when entries exist", () => {
191
- const map = makeMap();
192
- recordPendingHistoryEntry({
193
- historyMap: map,
194
- historyKey: "G",
195
- entry: entry("Alice", "hello"),
196
- limit: 10,
197
- });
198
- const out = buildPendingHistoryContext({
199
- historyMap: map,
200
- historyKey: "G",
201
- limit: 10,
202
- currentMessage: "[Bob] @bot",
203
- formatEntry: (e) => `${e.sender}: ${e.body}`,
204
- });
205
- expect(out).toContain("[Chat messages since your last reply — CONTEXT ONLY]");
206
- expect(out).toContain("Alice: hello");
207
- expect(out).toContain("[CURRENT MESSAGE — reply to this]");
208
- expect(out.endsWith("[Bob] @bot")).toBe(true);
209
- });
210
-
211
- it("returns current message unchanged when buffer is empty or disabled", () => {
212
- const map = makeMap();
213
- expect(
214
- buildPendingHistoryContext({
215
- historyMap: map,
216
- historyKey: "G",
217
- limit: 10,
218
- currentMessage: "hi",
219
- formatEntry: () => "x",
220
- }),
221
- ).toBe("hi");
222
- expect(
223
- buildPendingHistoryContext({
224
- historyMap: map,
225
- historyKey: "G",
226
- limit: 0,
227
- currentMessage: "hi",
228
- formatEntry: () => "x",
229
- }),
230
- ).toBe("hi");
231
- });
232
- });
233
-
234
- describe("LRU eviction across groups", () => {
235
- it("evicts oldest keys past the implicit cap (smoke check)", () => {
236
- const map = makeMap();
237
- // Just ensure the cache doesn't explode. The hard cap (1000) is an
238
- // implementation detail; here we confirm the data structure keeps
239
- // re-inserting without error at modest volume.
240
- for (let i = 0; i < 100; i++) {
241
- recordPendingHistoryEntry({
242
- historyMap: map,
243
- historyKey: `G${i}`,
244
- entry: entry("A", `m${i}`),
245
- limit: 1,
246
- });
247
- }
248
- expect(map.size).toBe(100);
249
- });
250
-
251
- it("refreshes LRU ordering on subsequent writes to the same key", () => {
252
- const map = makeMap();
253
- recordPendingHistoryEntry({
254
- historyMap: map,
255
- historyKey: "OLD",
256
- entry: entry("A", "1"),
257
- limit: 5,
258
- });
259
- recordPendingHistoryEntry({
260
- historyMap: map,
261
- historyKey: "NEW",
262
- entry: entry("A", "2"),
263
- limit: 5,
264
- });
265
- recordPendingHistoryEntry({
266
- historyMap: map,
267
- historyKey: "OLD",
268
- entry: entry("A", "3"),
269
- limit: 5,
270
- });
271
- // After re-writing OLD, its iteration order should come last.
272
- const keys = [...map.keys()];
273
- expect(keys).toEqual(["NEW", "OLD"]);
274
- });
275
- });
276
-
277
- describe("buildMergedMessageContext", () => {
278
- it("returns current message unchanged when no preceding parts", () => {
279
- expect(buildMergedMessageContext({ precedingParts: [], currentMessage: "hi" })).toBe("hi");
280
- });
281
-
282
- it("wraps preceding parts with tags", () => {
283
- const out = buildMergedMessageContext({
284
- precedingParts: ["a", "b"],
285
- currentMessage: "c",
286
- });
287
- expect(out).toContain("[Merged earlier messages — CONTEXT ONLY]");
288
- expect(out).toContain("a\nb");
289
- expect(out).toContain("[CURRENT MESSAGE — reply using the context above]");
290
- expect(out.endsWith("c")).toBe(true);
291
- });
292
- });
293
-
294
- describe("clearPendingHistory", () => {
295
- it("resets the buffer to empty", () => {
296
- const map = makeMap();
297
- recordPendingHistoryEntry({
298
- historyMap: map,
299
- historyKey: "G",
300
- entry: entry("A", "m"),
301
- limit: 5,
302
- });
303
- clearPendingHistory({ historyMap: map, historyKey: "G", limit: 5 });
304
- expect(map.get("G")).toEqual([]);
305
- });
306
-
307
- it("no-ops when disabled", () => {
308
- const map = makeMap();
309
- map.set("G", [entry("A", "m")]);
310
- clearPendingHistory({ historyMap: map, historyKey: "G", limit: 0 });
311
- expect(map.get("G")).toHaveLength(1);
312
- });
313
- });
314
- });