@openclaw-channel/socket-chat 1.0.4 → 1.0.6
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/README.md +9 -0
- package/package.json +1 -1
- package/src/channel.ts +4 -4
- package/src/outbound.test.ts +25 -26
- package/src/outbound.ts +8 -8
package/README.md
CHANGED
package/package.json
CHANGED
package/src/channel.ts
CHANGED
|
@@ -242,7 +242,7 @@ export const socketChatPlugin: ChannelPlugin<ResolvedSocketChatAccount> = {
|
|
|
242
242
|
normalizeTarget: normalizeSocketChatTarget,
|
|
243
243
|
targetResolver: {
|
|
244
244
|
looksLikeId: looksLikeSocketChatTargetId,
|
|
245
|
-
hint: "<contactId|group:groupId|group:groupId
|
|
245
|
+
hint: "<contactId|group:groupId|group:groupId|userId1,userId2>",
|
|
246
246
|
},
|
|
247
247
|
},
|
|
248
248
|
|
|
@@ -383,10 +383,10 @@ export const socketChatPlugin: ChannelPlugin<ResolvedSocketChatAccount> = {
|
|
|
383
383
|
// -------------------------------------------------------------------------
|
|
384
384
|
agentPrompt: {
|
|
385
385
|
messageToolHints: () => [
|
|
386
|
-
"- socket-chat: to send to a group, use target format `group:<groupId
|
|
387
|
-
"To @mention users in a group: `group:<groupId
|
|
386
|
+
"- socket-chat: to send to a group, use target format `group:<groupId>` (groupId may contain @, e.g. `group:17581395450@chatroom`). " +
|
|
387
|
+
"To @mention users in a group: `group:<groupId>|<userId1>,<userId2>`.",
|
|
388
388
|
"- socket-chat: to send an image, provide a public HTTP URL as the media parameter.",
|
|
389
|
-
"- socket-chat: direct messages use the sender's contactId
|
|
389
|
+
"- socket-chat: direct messages use the sender's contactId as the target.",
|
|
390
390
|
],
|
|
391
391
|
},
|
|
392
392
|
};
|
package/src/outbound.test.ts
CHANGED
|
@@ -18,20 +18,20 @@ describe("parseSocketChatTarget", () => {
|
|
|
18
18
|
});
|
|
19
19
|
|
|
20
20
|
it("parses a group target without mentions", () => {
|
|
21
|
-
const result = parseSocketChatTarget("group:
|
|
22
|
-
expect(result).toEqual({ isGroup: true, groupId: "
|
|
21
|
+
const result = parseSocketChatTarget("group:17581395450@chatroom");
|
|
22
|
+
expect(result).toEqual({ isGroup: true, groupId: "17581395450@chatroom" });
|
|
23
23
|
});
|
|
24
24
|
|
|
25
25
|
it("parses a group target with single mention", () => {
|
|
26
|
-
const result = parseSocketChatTarget("group:
|
|
27
|
-
expect(result).toEqual({ isGroup: true, groupId: "
|
|
26
|
+
const result = parseSocketChatTarget("group:17581395450@chatroom|wxid_a");
|
|
27
|
+
expect(result).toEqual({ isGroup: true, groupId: "17581395450@chatroom", mentionIds: ["wxid_a"] });
|
|
28
28
|
});
|
|
29
29
|
|
|
30
30
|
it("parses a group target with multiple mentions", () => {
|
|
31
|
-
const result = parseSocketChatTarget("group:
|
|
31
|
+
const result = parseSocketChatTarget("group:17581395450@chatroom|wxid_a,wxid_b,wxid_c");
|
|
32
32
|
expect(result).toEqual({
|
|
33
33
|
isGroup: true,
|
|
34
|
-
groupId: "
|
|
34
|
+
groupId: "17581395450@chatroom",
|
|
35
35
|
mentionIds: ["wxid_a", "wxid_b", "wxid_c"],
|
|
36
36
|
});
|
|
37
37
|
});
|
|
@@ -42,7 +42,7 @@ describe("parseSocketChatTarget", () => {
|
|
|
42
42
|
});
|
|
43
43
|
|
|
44
44
|
it("filters empty mention ids", () => {
|
|
45
|
-
const result = parseSocketChatTarget("group:
|
|
45
|
+
const result = parseSocketChatTarget("group:17581395450@chatroom|wxid_a,,wxid_b");
|
|
46
46
|
expect(result.isGroup).toBe(true);
|
|
47
47
|
if (result.isGroup) {
|
|
48
48
|
expect(result.mentionIds).toEqual(["wxid_a", "wxid_b"]);
|
|
@@ -57,7 +57,8 @@ describe("parseSocketChatTarget", () => {
|
|
|
57
57
|
describe("normalizeSocketChatTarget", () => {
|
|
58
58
|
it("returns the target unchanged for native IDs", () => {
|
|
59
59
|
expect(normalizeSocketChatTarget("wxid_abc")).toBe("wxid_abc");
|
|
60
|
-
expect(normalizeSocketChatTarget("group:
|
|
60
|
+
expect(normalizeSocketChatTarget("group:17581395450@chatroom")).toBe("group:17581395450@chatroom");
|
|
61
|
+
expect(normalizeSocketChatTarget("group:17581395450@chatroom|wxid_a")).toBe("group:17581395450@chatroom|wxid_a");
|
|
61
62
|
});
|
|
62
63
|
|
|
63
64
|
it("strips socket-chat: prefix (case-insensitive)", () => {
|
|
@@ -81,25 +82,23 @@ describe("normalizeSocketChatTarget", () => {
|
|
|
81
82
|
// ---------------------------------------------------------------------------
|
|
82
83
|
|
|
83
84
|
describe("looksLikeSocketChatTargetId", () => {
|
|
84
|
-
it("
|
|
85
|
+
it("accepts any non-empty id", () => {
|
|
85
86
|
expect(looksLikeSocketChatTargetId("wxid_abc123")).toBe(true);
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
it("recognizes roomid_ prefix", () => {
|
|
89
87
|
expect(looksLikeSocketChatTargetId("roomid_xyz")).toBe(true);
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
it("recognizes group: prefix", () => {
|
|
93
88
|
expect(looksLikeSocketChatTargetId("group:roomid_xxx")).toBe(true);
|
|
89
|
+
expect(looksLikeSocketChatTargetId("alice")).toBe(true);
|
|
90
|
+
expect(looksLikeSocketChatTargetId("user@example.com")).toBe(true);
|
|
94
91
|
});
|
|
95
92
|
|
|
96
|
-
it("rejects
|
|
97
|
-
expect(looksLikeSocketChatTargetId("alice")).toBe(false);
|
|
98
|
-
expect(looksLikeSocketChatTargetId("user@example.com")).toBe(false);
|
|
93
|
+
it("rejects empty string", () => {
|
|
99
94
|
expect(looksLikeSocketChatTargetId("")).toBe(false);
|
|
100
95
|
});
|
|
101
96
|
|
|
102
|
-
it("
|
|
97
|
+
it("rejects whitespace-only string", () => {
|
|
98
|
+
expect(looksLikeSocketChatTargetId(" ")).toBe(false);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("trims before checking", () => {
|
|
103
102
|
expect(looksLikeSocketChatTargetId(" wxid_abc")).toBe(true);
|
|
104
103
|
});
|
|
105
104
|
});
|
|
@@ -118,24 +117,24 @@ describe("buildTextPayload", () => {
|
|
|
118
117
|
});
|
|
119
118
|
|
|
120
119
|
it("builds a group text payload", () => {
|
|
121
|
-
const payload = buildTextPayload("group:
|
|
120
|
+
const payload = buildTextPayload("group:17581395450@chatroom", "hi group");
|
|
122
121
|
expect(payload.isGroup).toBe(true);
|
|
123
|
-
expect(payload.groupId).toBe("
|
|
122
|
+
expect(payload.groupId).toBe("17581395450@chatroom");
|
|
124
123
|
expect(payload.messages).toEqual([{ type: 1, content: "hi group" }]);
|
|
125
124
|
});
|
|
126
125
|
|
|
127
126
|
it("extracts mentions from group target string", () => {
|
|
128
|
-
const payload = buildTextPayload("group:
|
|
127
|
+
const payload = buildTextPayload("group:17581395450@chatroom|wxid_a,wxid_b", "hi");
|
|
129
128
|
expect(payload.mentionIds).toEqual(["wxid_a", "wxid_b"]);
|
|
130
129
|
});
|
|
131
130
|
|
|
132
131
|
it("allows explicit override of mentionIds", () => {
|
|
133
|
-
const payload = buildTextPayload("group:
|
|
132
|
+
const payload = buildTextPayload("group:17581395450@chatroom", "hi", { mentionIds: ["wxid_override"] });
|
|
134
133
|
expect(payload.mentionIds).toEqual(["wxid_override"]);
|
|
135
134
|
});
|
|
136
135
|
|
|
137
136
|
it("sets mentionIds to undefined when empty", () => {
|
|
138
|
-
const payload = buildTextPayload("group:
|
|
137
|
+
const payload = buildTextPayload("group:17581395450@chatroom", "hi", { mentionIds: [] });
|
|
139
138
|
expect(payload.mentionIds).toBeUndefined();
|
|
140
139
|
});
|
|
141
140
|
});
|
|
@@ -169,8 +168,8 @@ describe("buildMediaPayload", () => {
|
|
|
169
168
|
});
|
|
170
169
|
|
|
171
170
|
it("builds group media payload", () => {
|
|
172
|
-
const payload = buildMediaPayload("group:
|
|
171
|
+
const payload = buildMediaPayload("group:17581395450@chatroom", "https://img.example.com/photo.jpg");
|
|
173
172
|
expect(payload.isGroup).toBe(true);
|
|
174
|
-
expect(payload.groupId).toBe("
|
|
173
|
+
expect(payload.groupId).toBe("17581395450@chatroom");
|
|
175
174
|
});
|
|
176
175
|
});
|
package/src/outbound.ts
CHANGED
|
@@ -9,19 +9,19 @@ import type { SocketChatMqttConfig, SocketChatOutboundPayload } from "./types.js
|
|
|
9
9
|
*
|
|
10
10
|
* 格式约定:
|
|
11
11
|
* - 私聊:contactId,例如 "wxid_abc123"
|
|
12
|
-
* - 群聊:以 "group:" 前缀,例如 "group:
|
|
13
|
-
* - 群聊带
|
|
12
|
+
* - 群聊:以 "group:" 前缀,例如 "group:17581395450@chatroom"
|
|
13
|
+
* - 群聊带 mention:用 "|" 分隔 groupId 与 mentionIds,例如 "group:17581395450@chatroom|wxid_a,wxid_b"
|
|
14
14
|
*/
|
|
15
15
|
export function parseSocketChatTarget(to: string): Omit<SocketChatOutboundPayload, "messages"> {
|
|
16
16
|
const trimmed = to.trim();
|
|
17
17
|
|
|
18
18
|
if (trimmed.startsWith("group:")) {
|
|
19
19
|
const withoutPrefix = trimmed.slice("group:".length);
|
|
20
|
-
const
|
|
21
|
-
if (
|
|
22
|
-
const groupId = withoutPrefix.slice(0,
|
|
20
|
+
const pipeIdx = withoutPrefix.indexOf("|");
|
|
21
|
+
if (pipeIdx !== -1) {
|
|
22
|
+
const groupId = withoutPrefix.slice(0, pipeIdx);
|
|
23
23
|
const mentionIds = withoutPrefix
|
|
24
|
-
.slice(
|
|
24
|
+
.slice(pipeIdx + 1)
|
|
25
25
|
.split(",")
|
|
26
26
|
.map((s) => s.trim())
|
|
27
27
|
.filter(Boolean);
|
|
@@ -46,10 +46,10 @@ export function normalizeSocketChatTarget(raw: string): string | undefined {
|
|
|
46
46
|
|
|
47
47
|
/**
|
|
48
48
|
* 判断字符串是否像一个 socket-chat 原生 ID
|
|
49
|
-
*
|
|
49
|
+
* socket-chat 的 chatId 格式不固定,任何非空字符串均视为原生 ID
|
|
50
50
|
*/
|
|
51
51
|
export function looksLikeSocketChatTargetId(s: string): boolean {
|
|
52
|
-
return
|
|
52
|
+
return s.trim().length > 0;
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
/**
|