@openclaw/matrix 2026.2.17 → 2026.2.19

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/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Changelog
2
2
 
3
- ## 2026.2.17
3
+ ## 2026.2.19
4
4
 
5
5
  ### Changes
6
6
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openclaw/matrix",
3
- "version": "2026.2.17",
3
+ "version": "2026.2.19",
4
4
  "description": "OpenClaw Matrix channel plugin",
5
5
  "type": "module",
6
6
  "dependencies": {
@@ -51,4 +51,24 @@ describe("matrix directory live", () => {
51
51
 
52
52
  expect(resolveMatrixAuth).toHaveBeenCalledWith({ cfg, accountId: "assistant" });
53
53
  });
54
+
55
+ it("returns no peer results for empty query without resolving auth", async () => {
56
+ const result = await listMatrixDirectoryPeersLive({
57
+ cfg,
58
+ query: " ",
59
+ });
60
+
61
+ expect(result).toEqual([]);
62
+ expect(resolveMatrixAuth).not.toHaveBeenCalled();
63
+ });
64
+
65
+ it("returns no group results for empty query without resolving auth", async () => {
66
+ const result = await listMatrixDirectoryGroupsLive({
67
+ cfg,
68
+ query: "",
69
+ });
70
+
71
+ expect(result).toEqual([]);
72
+ expect(resolveMatrixAuth).not.toHaveBeenCalled();
73
+ });
54
74
  });
@@ -22,6 +22,15 @@ type MatrixAliasLookup = {
22
22
  room_id?: string;
23
23
  };
24
24
 
25
+ type MatrixDirectoryLiveParams = {
26
+ cfg: unknown;
27
+ accountId?: string | null;
28
+ query?: string | null;
29
+ limit?: number | null;
30
+ };
31
+
32
+ type MatrixResolvedAuth = Awaited<ReturnType<typeof resolveMatrixAuth>>;
33
+
25
34
  async function fetchMatrixJson<T>(params: {
26
35
  homeserver: string;
27
36
  path: string;
@@ -48,17 +57,42 @@ function normalizeQuery(value?: string | null): string {
48
57
  return value?.trim().toLowerCase() ?? "";
49
58
  }
50
59
 
51
- export async function listMatrixDirectoryPeersLive(params: {
52
- cfg: unknown;
53
- accountId?: string | null;
54
- query?: string | null;
55
- limit?: number | null;
56
- }): Promise<ChannelDirectoryEntry[]> {
60
+ function resolveMatrixDirectoryLimit(limit?: number | null): number {
61
+ return typeof limit === "number" && limit > 0 ? limit : 20;
62
+ }
63
+
64
+ async function resolveMatrixDirectoryContext(
65
+ params: MatrixDirectoryLiveParams,
66
+ ): Promise<{ query: string; auth: MatrixResolvedAuth } | null> {
57
67
  const query = normalizeQuery(params.query);
58
68
  if (!query) {
59
- return [];
69
+ return null;
60
70
  }
61
71
  const auth = await resolveMatrixAuth({ cfg: params.cfg as never, accountId: params.accountId });
72
+ return { query, auth };
73
+ }
74
+
75
+ function createGroupDirectoryEntry(params: {
76
+ id: string;
77
+ name: string;
78
+ handle?: string;
79
+ }): ChannelDirectoryEntry {
80
+ return {
81
+ kind: "group",
82
+ id: params.id,
83
+ name: params.name,
84
+ handle: params.handle,
85
+ } satisfies ChannelDirectoryEntry;
86
+ }
87
+
88
+ export async function listMatrixDirectoryPeersLive(
89
+ params: MatrixDirectoryLiveParams,
90
+ ): Promise<ChannelDirectoryEntry[]> {
91
+ const context = await resolveMatrixDirectoryContext(params);
92
+ if (!context) {
93
+ return [];
94
+ }
95
+ const { query, auth } = context;
62
96
  const res = await fetchMatrixJson<MatrixUserDirectoryResponse>({
63
97
  homeserver: auth.homeserver,
64
98
  accessToken: auth.accessToken,
@@ -66,7 +100,7 @@ export async function listMatrixDirectoryPeersLive(params: {
66
100
  method: "POST",
67
101
  body: {
68
102
  search_term: query,
69
- limit: typeof params.limit === "number" && params.limit > 0 ? params.limit : 20,
103
+ limit: resolveMatrixDirectoryLimit(params.limit),
70
104
  },
71
105
  });
72
106
  const results = res.results ?? [];
@@ -121,42 +155,26 @@ async function fetchMatrixRoomName(
121
155
  }
122
156
  }
123
157
 
124
- export async function listMatrixDirectoryGroupsLive(params: {
125
- cfg: unknown;
126
- accountId?: string | null;
127
- query?: string | null;
128
- limit?: number | null;
129
- }): Promise<ChannelDirectoryEntry[]> {
130
- const query = normalizeQuery(params.query);
131
- if (!query) {
158
+ export async function listMatrixDirectoryGroupsLive(
159
+ params: MatrixDirectoryLiveParams,
160
+ ): Promise<ChannelDirectoryEntry[]> {
161
+ const context = await resolveMatrixDirectoryContext(params);
162
+ if (!context) {
132
163
  return [];
133
164
  }
134
- const auth = await resolveMatrixAuth({ cfg: params.cfg as never, accountId: params.accountId });
135
- const limit = typeof params.limit === "number" && params.limit > 0 ? params.limit : 20;
165
+ const { query, auth } = context;
166
+ const limit = resolveMatrixDirectoryLimit(params.limit);
136
167
 
137
168
  if (query.startsWith("#")) {
138
169
  const roomId = await resolveMatrixRoomAlias(auth.homeserver, auth.accessToken, query);
139
170
  if (!roomId) {
140
171
  return [];
141
172
  }
142
- return [
143
- {
144
- kind: "group",
145
- id: roomId,
146
- name: query,
147
- handle: query,
148
- } satisfies ChannelDirectoryEntry,
149
- ];
173
+ return [createGroupDirectoryEntry({ id: roomId, name: query, handle: query })];
150
174
  }
151
175
 
152
176
  if (query.startsWith("!")) {
153
- return [
154
- {
155
- kind: "group",
156
- id: query,
157
- name: query,
158
- } satisfies ChannelDirectoryEntry,
159
- ];
177
+ return [createGroupDirectoryEntry({ id: query, name: query })];
160
178
  }
161
179
 
162
180
  const joined = await fetchMatrixJson<MatrixJoinedRoomsResponse>({
@@ -0,0 +1,15 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { resolveMatrixActionLimit } from "./limits.js";
3
+
4
+ describe("resolveMatrixActionLimit", () => {
5
+ it("uses fallback for non-finite values", () => {
6
+ expect(resolveMatrixActionLimit(undefined, 20)).toBe(20);
7
+ expect(resolveMatrixActionLimit(Number.NaN, 20)).toBe(20);
8
+ });
9
+
10
+ it("normalizes finite numbers to positive integers", () => {
11
+ expect(resolveMatrixActionLimit(7.9, 20)).toBe(7);
12
+ expect(resolveMatrixActionLimit(0, 20)).toBe(1);
13
+ expect(resolveMatrixActionLimit(-3, 20)).toBe(1);
14
+ });
15
+ });
@@ -0,0 +1,6 @@
1
+ export function resolveMatrixActionLimit(raw: unknown, fallback: number): number {
2
+ if (typeof raw !== "number" || !Number.isFinite(raw)) {
3
+ return fallback;
4
+ }
5
+ return Math.max(1, Math.floor(raw));
6
+ }
@@ -1,5 +1,6 @@
1
1
  import { resolveMatrixRoomId, sendMessageMatrix } from "../send.js";
2
2
  import { resolveActionClient } from "./client.js";
3
+ import { resolveMatrixActionLimit } from "./limits.js";
3
4
  import { summarizeMatrixRawEvent } from "./summary.js";
4
5
  import {
5
6
  EventType,
@@ -95,10 +96,7 @@ export async function readMatrixMessages(
95
96
  const { client, stopOnDone } = await resolveActionClient(opts);
96
97
  try {
97
98
  const resolvedRoom = await resolveMatrixRoomId(client, roomId);
98
- const limit =
99
- typeof opts.limit === "number" && Number.isFinite(opts.limit)
100
- ? Math.max(1, Math.floor(opts.limit))
101
- : 20;
99
+ const limit = resolveMatrixActionLimit(opts.limit, 20);
102
100
  const token = opts.before?.trim() || opts.after?.trim() || undefined;
103
101
  const dir = opts.after ? "f" : "b";
104
102
  // @vector-im/matrix-bot-sdk uses doRequest for room messages
@@ -0,0 +1,74 @@
1
+ import type { MatrixClient } from "@vector-im/matrix-bot-sdk";
2
+ import { describe, expect, it, vi } from "vitest";
3
+ import { listMatrixPins, pinMatrixMessage, unpinMatrixMessage } from "./pins.js";
4
+
5
+ function createPinsClient(seedPinned: string[], knownBodies: Record<string, string> = {}) {
6
+ let pinned = [...seedPinned];
7
+ const getRoomStateEvent = vi.fn(async () => ({ pinned: [...pinned] }));
8
+ const sendStateEvent = vi.fn(
9
+ async (_roomId: string, _type: string, _key: string, payload: any) => {
10
+ pinned = [...payload.pinned];
11
+ },
12
+ );
13
+ const getEvent = vi.fn(async (_roomId: string, eventId: string) => {
14
+ const body = knownBodies[eventId];
15
+ if (!body) {
16
+ throw new Error("missing");
17
+ }
18
+ return {
19
+ event_id: eventId,
20
+ sender: "@alice:example.org",
21
+ type: "m.room.message",
22
+ origin_server_ts: 123,
23
+ content: { msgtype: "m.text", body },
24
+ };
25
+ });
26
+
27
+ return {
28
+ client: {
29
+ getRoomStateEvent,
30
+ sendStateEvent,
31
+ getEvent,
32
+ stop: vi.fn(),
33
+ } as unknown as MatrixClient,
34
+ getPinned: () => pinned,
35
+ sendStateEvent,
36
+ };
37
+ }
38
+
39
+ describe("matrix pins actions", () => {
40
+ it("pins a message once even when asked twice", async () => {
41
+ const { client, getPinned, sendStateEvent } = createPinsClient(["$a"]);
42
+
43
+ const first = await pinMatrixMessage("!room:example.org", "$b", { client });
44
+ const second = await pinMatrixMessage("!room:example.org", "$b", { client });
45
+
46
+ expect(first.pinned).toEqual(["$a", "$b"]);
47
+ expect(second.pinned).toEqual(["$a", "$b"]);
48
+ expect(getPinned()).toEqual(["$a", "$b"]);
49
+ expect(sendStateEvent).toHaveBeenCalledTimes(2);
50
+ });
51
+
52
+ it("unpinds only the selected message id", async () => {
53
+ const { client, getPinned } = createPinsClient(["$a", "$b", "$c"]);
54
+
55
+ const result = await unpinMatrixMessage("!room:example.org", "$b", { client });
56
+
57
+ expect(result.pinned).toEqual(["$a", "$c"]);
58
+ expect(getPinned()).toEqual(["$a", "$c"]);
59
+ });
60
+
61
+ it("lists pinned ids and summarizes only resolvable events", async () => {
62
+ const { client } = createPinsClient(["$a", "$missing"], { $a: "hello" });
63
+
64
+ const result = await listMatrixPins("!room:example.org", { client });
65
+
66
+ expect(result.pinned).toEqual(["$a", "$missing"]);
67
+ expect(result.events).toEqual([
68
+ expect.objectContaining({
69
+ eventId: "$a",
70
+ body: "hello",
71
+ }),
72
+ ]);
73
+ });
74
+ });
@@ -4,23 +4,22 @@ import { fetchEventSummary, readPinnedEvents } from "./summary.js";
4
4
  import {
5
5
  EventType,
6
6
  type MatrixActionClientOpts,
7
+ type MatrixActionClient,
7
8
  type MatrixMessageSummary,
8
9
  type RoomPinnedEventsEventContent,
9
10
  } from "./types.js";
10
11
 
11
- export async function pinMatrixMessage(
12
+ type ActionClient = MatrixActionClient["client"];
13
+
14
+ async function withResolvedPinRoom<T>(
12
15
  roomId: string,
13
- messageId: string,
14
- opts: MatrixActionClientOpts = {},
15
- ): Promise<{ pinned: string[] }> {
16
+ opts: MatrixActionClientOpts,
17
+ run: (client: ActionClient, resolvedRoom: string) => Promise<T>,
18
+ ): Promise<T> {
16
19
  const { client, stopOnDone } = await resolveActionClient(opts);
17
20
  try {
18
21
  const resolvedRoom = await resolveMatrixRoomId(client, roomId);
19
- const current = await readPinnedEvents(client, resolvedRoom);
20
- const next = current.includes(messageId) ? current : [...current, messageId];
21
- const payload: RoomPinnedEventsEventContent = { pinned: next };
22
- await client.sendStateEvent(resolvedRoom, EventType.RoomPinnedEvents, "", payload);
23
- return { pinned: next };
22
+ return await run(client, resolvedRoom);
24
23
  } finally {
25
24
  if (stopOnDone) {
26
25
  client.stop();
@@ -28,33 +27,46 @@ export async function pinMatrixMessage(
28
27
  }
29
28
  }
30
29
 
31
- export async function unpinMatrixMessage(
30
+ async function updateMatrixPins(
32
31
  roomId: string,
33
32
  messageId: string,
34
- opts: MatrixActionClientOpts = {},
33
+ opts: MatrixActionClientOpts,
34
+ update: (current: string[]) => string[],
35
35
  ): Promise<{ pinned: string[] }> {
36
- const { client, stopOnDone } = await resolveActionClient(opts);
37
- try {
38
- const resolvedRoom = await resolveMatrixRoomId(client, roomId);
36
+ return await withResolvedPinRoom(roomId, opts, async (client, resolvedRoom) => {
39
37
  const current = await readPinnedEvents(client, resolvedRoom);
40
- const next = current.filter((id) => id !== messageId);
38
+ const next = update(current);
41
39
  const payload: RoomPinnedEventsEventContent = { pinned: next };
42
40
  await client.sendStateEvent(resolvedRoom, EventType.RoomPinnedEvents, "", payload);
43
41
  return { pinned: next };
44
- } finally {
45
- if (stopOnDone) {
46
- client.stop();
47
- }
48
- }
42
+ });
43
+ }
44
+
45
+ export async function pinMatrixMessage(
46
+ roomId: string,
47
+ messageId: string,
48
+ opts: MatrixActionClientOpts = {},
49
+ ): Promise<{ pinned: string[] }> {
50
+ return await updateMatrixPins(roomId, messageId, opts, (current) =>
51
+ current.includes(messageId) ? current : [...current, messageId],
52
+ );
53
+ }
54
+
55
+ export async function unpinMatrixMessage(
56
+ roomId: string,
57
+ messageId: string,
58
+ opts: MatrixActionClientOpts = {},
59
+ ): Promise<{ pinned: string[] }> {
60
+ return await updateMatrixPins(roomId, messageId, opts, (current) =>
61
+ current.filter((id) => id !== messageId),
62
+ );
49
63
  }
50
64
 
51
65
  export async function listMatrixPins(
52
66
  roomId: string,
53
67
  opts: MatrixActionClientOpts = {},
54
68
  ): Promise<{ pinned: string[]; events: MatrixMessageSummary[] }> {
55
- const { client, stopOnDone } = await resolveActionClient(opts);
56
- try {
57
- const resolvedRoom = await resolveMatrixRoomId(client, roomId);
69
+ return await withResolvedPinRoom(roomId, opts, async (client, resolvedRoom) => {
58
70
  const pinned = await readPinnedEvents(client, resolvedRoom);
59
71
  const events = (
60
72
  await Promise.all(
@@ -68,9 +80,5 @@ export async function listMatrixPins(
68
80
  )
69
81
  ).filter((event): event is MatrixMessageSummary => Boolean(event));
70
82
  return { pinned, events };
71
- } finally {
72
- if (stopOnDone) {
73
- client.stop();
74
- }
75
- }
83
+ });
76
84
  }
@@ -0,0 +1,109 @@
1
+ import type { MatrixClient } from "@vector-im/matrix-bot-sdk";
2
+ import { describe, expect, it, vi } from "vitest";
3
+ import { listMatrixReactions, removeMatrixReactions } from "./reactions.js";
4
+
5
+ function createReactionsClient(params: {
6
+ chunk: Array<{
7
+ event_id?: string;
8
+ sender?: string;
9
+ key?: string;
10
+ }>;
11
+ userId?: string | null;
12
+ }) {
13
+ const doRequest = vi.fn(async (_method: string, _path: string, _query: any) => ({
14
+ chunk: params.chunk.map((item) => ({
15
+ event_id: item.event_id ?? "",
16
+ sender: item.sender ?? "",
17
+ content: item.key
18
+ ? {
19
+ "m.relates_to": {
20
+ rel_type: "m.annotation",
21
+ event_id: "$target",
22
+ key: item.key,
23
+ },
24
+ }
25
+ : {},
26
+ })),
27
+ }));
28
+ const getUserId = vi.fn(async () => params.userId ?? null);
29
+ const redactEvent = vi.fn(async () => undefined);
30
+
31
+ return {
32
+ client: {
33
+ doRequest,
34
+ getUserId,
35
+ redactEvent,
36
+ stop: vi.fn(),
37
+ } as unknown as MatrixClient,
38
+ doRequest,
39
+ redactEvent,
40
+ };
41
+ }
42
+
43
+ describe("matrix reaction actions", () => {
44
+ it("aggregates reactions by key and unique sender", async () => {
45
+ const { client, doRequest } = createReactionsClient({
46
+ chunk: [
47
+ { event_id: "$1", sender: "@alice:example.org", key: "👍" },
48
+ { event_id: "$2", sender: "@bob:example.org", key: "👍" },
49
+ { event_id: "$3", sender: "@alice:example.org", key: "👎" },
50
+ { event_id: "$4", sender: "@bot:example.org" },
51
+ ],
52
+ userId: "@bot:example.org",
53
+ });
54
+
55
+ const result = await listMatrixReactions("!room:example.org", "$msg", { client, limit: 2.9 });
56
+
57
+ expect(doRequest).toHaveBeenCalledWith(
58
+ "GET",
59
+ expect.stringContaining("/rooms/!room%3Aexample.org/relations/%24msg/"),
60
+ expect.objectContaining({ limit: 2 }),
61
+ );
62
+ expect(result).toEqual(
63
+ expect.arrayContaining([
64
+ expect.objectContaining({
65
+ key: "👍",
66
+ count: 2,
67
+ users: expect.arrayContaining(["@alice:example.org", "@bob:example.org"]),
68
+ }),
69
+ expect.objectContaining({
70
+ key: "👎",
71
+ count: 1,
72
+ users: ["@alice:example.org"],
73
+ }),
74
+ ]),
75
+ );
76
+ });
77
+
78
+ it("removes only current-user reactions matching emoji filter", async () => {
79
+ const { client, redactEvent } = createReactionsClient({
80
+ chunk: [
81
+ { event_id: "$1", sender: "@me:example.org", key: "👍" },
82
+ { event_id: "$2", sender: "@me:example.org", key: "👎" },
83
+ { event_id: "$3", sender: "@other:example.org", key: "👍" },
84
+ ],
85
+ userId: "@me:example.org",
86
+ });
87
+
88
+ const result = await removeMatrixReactions("!room:example.org", "$msg", {
89
+ client,
90
+ emoji: "👍",
91
+ });
92
+
93
+ expect(result).toEqual({ removed: 1 });
94
+ expect(redactEvent).toHaveBeenCalledTimes(1);
95
+ expect(redactEvent).toHaveBeenCalledWith("!room:example.org", "$1");
96
+ });
97
+
98
+ it("returns removed=0 when current user id is unavailable", async () => {
99
+ const { client, redactEvent } = createReactionsClient({
100
+ chunk: [{ event_id: "$1", sender: "@me:example.org", key: "👍" }],
101
+ userId: null,
102
+ });
103
+
104
+ const result = await removeMatrixReactions("!room:example.org", "$msg", { client });
105
+
106
+ expect(result).toEqual({ removed: 0 });
107
+ expect(redactEvent).not.toHaveBeenCalled();
108
+ });
109
+ });
@@ -1,5 +1,6 @@
1
1
  import { resolveMatrixRoomId } from "../send.js";
2
2
  import { resolveActionClient } from "./client.js";
3
+ import { resolveMatrixActionLimit } from "./limits.js";
3
4
  import {
4
5
  EventType,
5
6
  RelationType,
@@ -9,6 +10,23 @@ import {
9
10
  type ReactionEventContent,
10
11
  } from "./types.js";
11
12
 
13
+ function getReactionsPath(roomId: string, messageId: string): string {
14
+ return `/_matrix/client/v1/rooms/${encodeURIComponent(roomId)}/relations/${encodeURIComponent(messageId)}/${RelationType.Annotation}/${EventType.Reaction}`;
15
+ }
16
+
17
+ async function listReactionEvents(
18
+ client: NonNullable<MatrixActionClientOpts["client"]>,
19
+ roomId: string,
20
+ messageId: string,
21
+ limit: number,
22
+ ): Promise<MatrixRawEvent[]> {
23
+ const res = (await client.doRequest("GET", getReactionsPath(roomId, messageId), {
24
+ dir: "b",
25
+ limit,
26
+ })) as { chunk: MatrixRawEvent[] };
27
+ return res.chunk;
28
+ }
29
+
12
30
  export async function listMatrixReactions(
13
31
  roomId: string,
14
32
  messageId: string,
@@ -17,18 +35,10 @@ export async function listMatrixReactions(
17
35
  const { client, stopOnDone } = await resolveActionClient(opts);
18
36
  try {
19
37
  const resolvedRoom = await resolveMatrixRoomId(client, roomId);
20
- const limit =
21
- typeof opts.limit === "number" && Number.isFinite(opts.limit)
22
- ? Math.max(1, Math.floor(opts.limit))
23
- : 100;
24
- // @vector-im/matrix-bot-sdk uses doRequest for relations
25
- const res = (await client.doRequest(
26
- "GET",
27
- `/_matrix/client/v1/rooms/${encodeURIComponent(resolvedRoom)}/relations/${encodeURIComponent(messageId)}/${RelationType.Annotation}/${EventType.Reaction}`,
28
- { dir: "b", limit },
29
- )) as { chunk: MatrixRawEvent[] };
38
+ const limit = resolveMatrixActionLimit(opts.limit, 100);
39
+ const chunk = await listReactionEvents(client, resolvedRoom, messageId, limit);
30
40
  const summaries = new Map<string, MatrixReactionSummary>();
31
- for (const event of res.chunk) {
41
+ for (const event of chunk) {
32
42
  const content = event.content as ReactionEventContent;
33
43
  const key = content["m.relates_to"]?.key;
34
44
  if (!key) {
@@ -62,17 +72,13 @@ export async function removeMatrixReactions(
62
72
  const { client, stopOnDone } = await resolveActionClient(opts);
63
73
  try {
64
74
  const resolvedRoom = await resolveMatrixRoomId(client, roomId);
65
- const res = (await client.doRequest(
66
- "GET",
67
- `/_matrix/client/v1/rooms/${encodeURIComponent(resolvedRoom)}/relations/${encodeURIComponent(messageId)}/${RelationType.Annotation}/${EventType.Reaction}`,
68
- { dir: "b", limit: 200 },
69
- )) as { chunk: MatrixRawEvent[] };
75
+ const chunk = await listReactionEvents(client, resolvedRoom, messageId, 200);
70
76
  const userId = await client.getUserId();
71
77
  if (!userId) {
72
78
  return { removed: 0 };
73
79
  }
74
80
  const targetEmoji = opts.emoji?.trim();
75
- const toRemove = res.chunk
81
+ const toRemove = chunk
76
82
  .filter((event) => event.sender === userId)
77
83
  .filter((event) => {
78
84
  if (!targetEmoji) {
@@ -1,9 +1,9 @@
1
+ import { spawn } from "node:child_process";
1
2
  import fs from "node:fs";
2
3
  import { createRequire } from "node:module";
3
4
  import path from "node:path";
4
5
  import { fileURLToPath } from "node:url";
5
6
  import type { RuntimeEnv } from "openclaw/plugin-sdk";
6
- import { getMatrixRuntime } from "../runtime.js";
7
7
 
8
8
  const MATRIX_SDK_PACKAGE = "@vector-im/matrix-bot-sdk";
9
9
 
@@ -22,6 +22,85 @@ function resolvePluginRoot(): string {
22
22
  return path.resolve(currentDir, "..", "..");
23
23
  }
24
24
 
25
+ type CommandResult = {
26
+ code: number;
27
+ stdout: string;
28
+ stderr: string;
29
+ };
30
+
31
+ async function runFixedCommandWithTimeout(params: {
32
+ argv: string[];
33
+ cwd: string;
34
+ timeoutMs: number;
35
+ env?: NodeJS.ProcessEnv;
36
+ }): Promise<CommandResult> {
37
+ return await new Promise((resolve) => {
38
+ const [command, ...args] = params.argv;
39
+ if (!command) {
40
+ resolve({
41
+ code: 1,
42
+ stdout: "",
43
+ stderr: "command is required",
44
+ });
45
+ return;
46
+ }
47
+
48
+ const proc = spawn(command, args, {
49
+ cwd: params.cwd,
50
+ env: { ...process.env, ...params.env },
51
+ stdio: ["ignore", "pipe", "pipe"],
52
+ });
53
+
54
+ let stdout = "";
55
+ let stderr = "";
56
+ let settled = false;
57
+ let timer: NodeJS.Timeout | null = null;
58
+
59
+ const finalize = (result: CommandResult) => {
60
+ if (settled) {
61
+ return;
62
+ }
63
+ settled = true;
64
+ if (timer) {
65
+ clearTimeout(timer);
66
+ }
67
+ resolve(result);
68
+ };
69
+
70
+ proc.stdout?.on("data", (chunk: Buffer | string) => {
71
+ stdout += chunk.toString();
72
+ });
73
+ proc.stderr?.on("data", (chunk: Buffer | string) => {
74
+ stderr += chunk.toString();
75
+ });
76
+
77
+ timer = setTimeout(() => {
78
+ proc.kill("SIGKILL");
79
+ finalize({
80
+ code: 124,
81
+ stdout,
82
+ stderr: stderr || `command timed out after ${params.timeoutMs}ms`,
83
+ });
84
+ }, params.timeoutMs);
85
+
86
+ proc.on("error", (err) => {
87
+ finalize({
88
+ code: 1,
89
+ stdout,
90
+ stderr: err.message,
91
+ });
92
+ });
93
+
94
+ proc.on("close", (code) => {
95
+ finalize({
96
+ code: code ?? 1,
97
+ stdout,
98
+ stderr,
99
+ });
100
+ });
101
+ });
102
+ }
103
+
25
104
  export async function ensureMatrixSdkInstalled(params: {
26
105
  runtime: RuntimeEnv;
27
106
  confirm?: (message: string) => Promise<boolean>;
@@ -42,7 +121,8 @@ export async function ensureMatrixSdkInstalled(params: {
42
121
  ? ["pnpm", "install"]
43
122
  : ["npm", "install", "--omit=dev", "--silent"];
44
123
  params.runtime.log?.(`matrix: installing dependencies via ${command[0]} (${root})…`);
45
- const result = await getMatrixRuntime().system.runCommandWithTimeout(command, {
124
+ const result = await runFixedCommandWithTimeout({
125
+ argv: command,
46
126
  cwd: root,
47
127
  timeoutMs: 300_000,
48
128
  env: { COREPACK_ENABLE_DOWNLOAD_PROMPT: "0" },
@@ -0,0 +1,154 @@
1
+ import { describe, expect, it, vi } from "vitest";
2
+
3
+ // Mock the runtime before importing resolveMentions
4
+ vi.mock("../../runtime.js", () => ({
5
+ getMatrixRuntime: () => ({
6
+ channel: {
7
+ mentions: {
8
+ matchesMentionPatterns: (text: string, patterns: RegExp[]) =>
9
+ patterns.some((p) => p.test(text)),
10
+ },
11
+ },
12
+ }),
13
+ }));
14
+
15
+ import { resolveMentions } from "./mentions.js";
16
+
17
+ describe("resolveMentions", () => {
18
+ const userId = "@bot:matrix.org";
19
+ const mentionRegexes = [/@bot/i];
20
+
21
+ describe("m.mentions field", () => {
22
+ it("detects mention via m.mentions.user_ids", () => {
23
+ const result = resolveMentions({
24
+ content: {
25
+ msgtype: "m.text",
26
+ body: "hello",
27
+ "m.mentions": { user_ids: ["@bot:matrix.org"] },
28
+ },
29
+ userId,
30
+ text: "hello",
31
+ mentionRegexes,
32
+ });
33
+ expect(result.wasMentioned).toBe(true);
34
+ expect(result.hasExplicitMention).toBe(true);
35
+ });
36
+
37
+ it("detects room mention via m.mentions.room", () => {
38
+ const result = resolveMentions({
39
+ content: {
40
+ msgtype: "m.text",
41
+ body: "hello everyone",
42
+ "m.mentions": { room: true },
43
+ },
44
+ userId,
45
+ text: "hello everyone",
46
+ mentionRegexes,
47
+ });
48
+ expect(result.wasMentioned).toBe(true);
49
+ });
50
+ });
51
+
52
+ describe("formatted_body matrix.to links", () => {
53
+ it("detects mention in formatted_body with plain user ID", () => {
54
+ const result = resolveMentions({
55
+ content: {
56
+ msgtype: "m.text",
57
+ body: "Bot: hello",
58
+ formatted_body: '<a href="https://matrix.to/#/@bot:matrix.org">Bot</a>: hello',
59
+ },
60
+ userId,
61
+ text: "Bot: hello",
62
+ mentionRegexes: [],
63
+ });
64
+ expect(result.wasMentioned).toBe(true);
65
+ });
66
+
67
+ it("detects mention in formatted_body with URL-encoded user ID", () => {
68
+ const result = resolveMentions({
69
+ content: {
70
+ msgtype: "m.text",
71
+ body: "Bot: hello",
72
+ formatted_body: '<a href="https://matrix.to/#/%40bot%3Amatrix.org">Bot</a>: hello',
73
+ },
74
+ userId,
75
+ text: "Bot: hello",
76
+ mentionRegexes: [],
77
+ });
78
+ expect(result.wasMentioned).toBe(true);
79
+ });
80
+
81
+ it("detects mention with single quotes in href", () => {
82
+ const result = resolveMentions({
83
+ content: {
84
+ msgtype: "m.text",
85
+ body: "Bot: hello",
86
+ formatted_body: "<a href='https://matrix.to/#/@bot:matrix.org'>Bot</a>: hello",
87
+ },
88
+ userId,
89
+ text: "Bot: hello",
90
+ mentionRegexes: [],
91
+ });
92
+ expect(result.wasMentioned).toBe(true);
93
+ });
94
+
95
+ it("does not detect mention for different user ID", () => {
96
+ const result = resolveMentions({
97
+ content: {
98
+ msgtype: "m.text",
99
+ body: "Other: hello",
100
+ formatted_body: '<a href="https://matrix.to/#/@other:matrix.org">Other</a>: hello',
101
+ },
102
+ userId,
103
+ text: "Other: hello",
104
+ mentionRegexes: [],
105
+ });
106
+ expect(result.wasMentioned).toBe(false);
107
+ });
108
+
109
+ it("does not false-positive on partial user ID match", () => {
110
+ const result = resolveMentions({
111
+ content: {
112
+ msgtype: "m.text",
113
+ body: "Bot2: hello",
114
+ formatted_body: '<a href="https://matrix.to/#/@bot2:matrix.org">Bot2</a>: hello',
115
+ },
116
+ userId: "@bot:matrix.org",
117
+ text: "Bot2: hello",
118
+ mentionRegexes: [],
119
+ });
120
+ expect(result.wasMentioned).toBe(false);
121
+ });
122
+ });
123
+
124
+ describe("regex patterns", () => {
125
+ it("detects mention via regex pattern in body text", () => {
126
+ const result = resolveMentions({
127
+ content: {
128
+ msgtype: "m.text",
129
+ body: "hey @bot can you help?",
130
+ },
131
+ userId,
132
+ text: "hey @bot can you help?",
133
+ mentionRegexes,
134
+ });
135
+ expect(result.wasMentioned).toBe(true);
136
+ });
137
+ });
138
+
139
+ describe("no mention", () => {
140
+ it("returns false when no mention is present", () => {
141
+ const result = resolveMentions({
142
+ content: {
143
+ msgtype: "m.text",
144
+ body: "hello world",
145
+ },
146
+ userId,
147
+ text: "hello world",
148
+ mentionRegexes,
149
+ });
150
+ expect(result.wasMentioned).toBe(false);
151
+ expect(result.hasExplicitMention).toBe(false);
152
+ });
153
+ });
154
+ });
@@ -4,12 +4,36 @@ import { getMatrixRuntime } from "../../runtime.js";
4
4
  type MessageContentWithMentions = {
5
5
  msgtype: string;
6
6
  body: string;
7
+ formatted_body?: string;
7
8
  "m.mentions"?: {
8
9
  user_ids?: string[];
9
10
  room?: boolean;
10
11
  };
11
12
  };
12
13
 
14
+ /**
15
+ * Check if the formatted_body contains a matrix.to mention link for the given user ID.
16
+ * Many Matrix clients (including Element) use HTML links in formatted_body instead of
17
+ * or in addition to the m.mentions field.
18
+ */
19
+ function checkFormattedBodyMention(formattedBody: string | undefined, userId: string): boolean {
20
+ if (!formattedBody || !userId) {
21
+ return false;
22
+ }
23
+ // Escape special regex characters in the user ID (e.g., @user:matrix.org)
24
+ const escapedUserId = userId.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
25
+ // Match matrix.to links with the user ID, handling both URL-encoded and plain formats
26
+ // Example: href="https://matrix.to/#/@user:matrix.org" or href="https://matrix.to/#/%40user%3Amatrix.org"
27
+ const plainPattern = new RegExp(`href=["']https://matrix\\.to/#/${escapedUserId}["']`, "i");
28
+ if (plainPattern.test(formattedBody)) {
29
+ return true;
30
+ }
31
+ // Also check URL-encoded version (@ -> %40, : -> %3A)
32
+ const encodedUserId = encodeURIComponent(userId).replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
33
+ const encodedPattern = new RegExp(`href=["']https://matrix\\.to/#/${encodedUserId}["']`, "i");
34
+ return encodedPattern.test(formattedBody);
35
+ }
36
+
13
37
  export function resolveMentions(params: {
14
38
  content: MessageContentWithMentions;
15
39
  userId?: string | null;
@@ -20,9 +44,16 @@ export function resolveMentions(params: {
20
44
  const mentionedUsers = Array.isArray(mentions?.user_ids)
21
45
  ? new Set(mentions.user_ids)
22
46
  : new Set<string>();
47
+
48
+ // Check formatted_body for matrix.to mention links (legacy/alternative mention format)
49
+ const mentionedInFormattedBody = params.userId
50
+ ? checkFormattedBodyMention(params.content.formatted_body, params.userId)
51
+ : false;
52
+
23
53
  const wasMentioned =
24
54
  Boolean(mentions?.room) ||
25
55
  (params.userId ? mentionedUsers.has(params.userId) : false) ||
56
+ mentionedInFormattedBody ||
26
57
  getMatrixRuntime().channel.mentions.matchesMentionPatterns(
27
58
  params.text ?? "",
28
59
  params.mentionRegexes,
@@ -1,6 +1,6 @@
1
1
  import type { ChannelDirectoryEntry } from "openclaw/plugin-sdk";
2
2
  import { describe, expect, it, vi, beforeEach } from "vitest";
3
- import { listMatrixDirectoryPeersLive } from "./directory-live.js";
3
+ import { listMatrixDirectoryGroupsLive, listMatrixDirectoryPeersLive } from "./directory-live.js";
4
4
  import { resolveMatrixTargets } from "./resolve-targets.js";
5
5
 
6
6
  vi.mock("./directory-live.js", () => ({
@@ -11,6 +11,7 @@ vi.mock("./directory-live.js", () => ({
11
11
  describe("resolveMatrixTargets (users)", () => {
12
12
  beforeEach(() => {
13
13
  vi.mocked(listMatrixDirectoryPeersLive).mockReset();
14
+ vi.mocked(listMatrixDirectoryGroupsLive).mockReset();
14
15
  });
15
16
 
16
17
  it("resolves exact unique display name matches", async () => {
@@ -45,4 +46,22 @@ describe("resolveMatrixTargets (users)", () => {
45
46
  expect(result?.resolved).toBe(false);
46
47
  expect(result?.note).toMatch(/use full Matrix ID/i);
47
48
  });
49
+
50
+ it("prefers exact group matches over first partial result", async () => {
51
+ const matches: ChannelDirectoryEntry[] = [
52
+ { kind: "group", id: "!one:example.org", name: "General", handle: "#general" },
53
+ { kind: "group", id: "!two:example.org", name: "Team", handle: "#team" },
54
+ ];
55
+ vi.mocked(listMatrixDirectoryGroupsLive).mockResolvedValue(matches);
56
+
57
+ const [result] = await resolveMatrixTargets({
58
+ cfg: {},
59
+ inputs: ["#team"],
60
+ kind: "group",
61
+ });
62
+
63
+ expect(result?.resolved).toBe(true);
64
+ expect(result?.id).toBe("!two:example.org");
65
+ expect(result?.note).toBe("multiple matches; chose first");
66
+ });
48
67
  });
@@ -6,6 +6,22 @@ import type {
6
6
  } from "openclaw/plugin-sdk";
7
7
  import { listMatrixDirectoryGroupsLive, listMatrixDirectoryPeersLive } from "./directory-live.js";
8
8
 
9
+ function findExactDirectoryMatches(
10
+ matches: ChannelDirectoryEntry[],
11
+ query: string,
12
+ ): ChannelDirectoryEntry[] {
13
+ const normalized = query.trim().toLowerCase();
14
+ if (!normalized) {
15
+ return [];
16
+ }
17
+ return matches.filter((match) => {
18
+ const id = match.id.trim().toLowerCase();
19
+ const name = match.name?.trim().toLowerCase();
20
+ const handle = match.handle?.trim().toLowerCase();
21
+ return normalized === id || normalized === name || normalized === handle;
22
+ });
23
+ }
24
+
9
25
  function pickBestGroupMatch(
10
26
  matches: ChannelDirectoryEntry[],
11
27
  query: string,
@@ -13,19 +29,8 @@ function pickBestGroupMatch(
13
29
  if (matches.length === 0) {
14
30
  return undefined;
15
31
  }
16
- const normalized = query.trim().toLowerCase();
17
- if (normalized) {
18
- const exact = matches.find((match) => {
19
- const name = match.name?.trim().toLowerCase();
20
- const handle = match.handle?.trim().toLowerCase();
21
- const id = match.id.trim().toLowerCase();
22
- return name === normalized || handle === normalized || id === normalized;
23
- });
24
- if (exact) {
25
- return exact;
26
- }
27
- }
28
- return matches[0];
32
+ const [exact] = findExactDirectoryMatches(matches, query);
33
+ return exact ?? matches[0];
29
34
  }
30
35
 
31
36
  function pickBestUserMatch(
@@ -35,16 +40,7 @@ function pickBestUserMatch(
35
40
  if (matches.length === 0) {
36
41
  return undefined;
37
42
  }
38
- const normalized = query.trim().toLowerCase();
39
- if (!normalized) {
40
- return undefined;
41
- }
42
- const exact = matches.filter((match) => {
43
- const id = match.id.trim().toLowerCase();
44
- const name = match.name?.trim().toLowerCase();
45
- const handle = match.handle?.trim().toLowerCase();
46
- return normalized === id || normalized === name || normalized === handle;
47
- });
43
+ const exact = findExactDirectoryMatches(matches, query);
48
44
  if (exact.length === 1) {
49
45
  return exact[0];
50
46
  }
@@ -59,12 +55,7 @@ function describeUserMatchFailure(matches: ChannelDirectoryEntry[], query: strin
59
55
  if (!normalized) {
60
56
  return "empty input";
61
57
  }
62
- const exact = matches.filter((match) => {
63
- const id = match.id.trim().toLowerCase();
64
- const name = match.name?.trim().toLowerCase();
65
- const handle = match.handle?.trim().toLowerCase();
66
- return normalized === id || normalized === name || normalized === handle;
67
- });
58
+ const exact = findExactDirectoryMatches(matches, normalized);
68
59
  if (exact.length === 0) {
69
60
  return "no exact match; use full Matrix ID";
70
61
  }