@openclaw/zalouser 2026.3.13 → 2026.5.2-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.
- package/README.md +4 -3
- package/api.ts +9 -0
- package/channel-plugin-api.ts +3 -0
- package/contract-api.ts +2 -0
- package/doctor-contract-api.ts +1 -0
- package/index.ts +29 -24
- package/openclaw.plugin.json +293 -1
- package/package.json +38 -11
- package/runtime-api.ts +67 -0
- package/secret-contract-api.ts +4 -0
- package/setup-entry.ts +9 -0
- package/setup-plugin-api.ts +2 -0
- package/src/accounts.runtime.ts +1 -0
- package/src/accounts.test-mocks.ts +7 -3
- package/src/accounts.test.ts +53 -1
- package/src/accounts.ts +38 -24
- package/src/channel-api.ts +20 -0
- package/src/channel.adapters.ts +391 -0
- package/src/channel.directory.test.ts +47 -40
- package/src/channel.runtime.ts +12 -0
- package/src/channel.sendpayload.test.ts +41 -23
- package/src/channel.setup.test.ts +33 -0
- package/src/channel.setup.ts +12 -0
- package/src/channel.test.ts +231 -20
- package/src/channel.ts +176 -685
- package/src/config-schema.ts +5 -5
- package/src/directory.ts +54 -0
- package/src/doctor-contract.ts +156 -0
- package/src/doctor.test.ts +77 -0
- package/src/doctor.ts +37 -0
- package/src/group-policy.test.ts +4 -4
- package/src/group-policy.ts +4 -2
- package/src/monitor.account-scope.test.ts +2 -1
- package/src/monitor.group-gating.test.ts +162 -8
- package/src/monitor.ts +233 -173
- package/src/probe.ts +3 -2
- package/src/qr-temp-file.ts +1 -1
- package/src/reaction.ts +5 -2
- package/src/runtime.ts +6 -3
- package/src/security-audit.test.ts +80 -0
- package/src/security-audit.ts +71 -0
- package/src/send.test.ts +2 -2
- package/src/send.ts +3 -3
- package/src/session-route.ts +121 -0
- package/src/setup-core.ts +33 -0
- package/src/setup-surface.test.ts +363 -0
- package/src/setup-surface.ts +470 -0
- package/src/setup-test-helpers.ts +42 -0
- package/src/shared.ts +92 -0
- package/src/status-issues.test.ts +1 -13
- package/src/status-issues.ts +8 -2
- package/src/test-helpers.ts +1 -1
- package/src/text-styles.test.ts +1 -1
- package/src/text-styles.ts +5 -2
- package/src/tool.test.ts +66 -3
- package/src/tool.ts +76 -14
- package/src/types.ts +3 -3
- package/src/zalo-js.credentials.test.ts +465 -0
- package/src/zalo-js.test-mocks.ts +89 -0
- package/src/zalo-js.ts +491 -274
- package/src/zca-client.test.ts +24 -0
- package/src/zca-client.ts +24 -58
- package/src/zca-constants.ts +55 -0
- package/test-api.ts +21 -0
- package/tsconfig.json +16 -0
- package/CHANGELOG.md +0 -107
- package/src/onboarding.ts +0 -340
package/src/config-schema.ts
CHANGED
|
@@ -3,12 +3,12 @@ import {
|
|
|
3
3
|
buildCatchallMultiAccountChannelSchema,
|
|
4
4
|
DmPolicySchema,
|
|
5
5
|
GroupPolicySchema,
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
MarkdownConfigSchema,
|
|
7
|
+
ToolPolicySchema,
|
|
8
|
+
} from "openclaw/plugin-sdk/channel-config-schema";
|
|
9
|
+
import { z } from "openclaw/plugin-sdk/zod";
|
|
9
10
|
|
|
10
11
|
const groupConfigSchema = z.object({
|
|
11
|
-
allow: z.boolean().optional(),
|
|
12
12
|
enabled: z.boolean().optional(),
|
|
13
13
|
requireMention: z.boolean().optional(),
|
|
14
14
|
tools: ToolPolicySchema,
|
|
@@ -24,7 +24,7 @@ const zalouserAccountSchema = z.object({
|
|
|
24
24
|
allowFrom: AllowFromListSchema,
|
|
25
25
|
historyLimit: z.number().int().min(0).optional(),
|
|
26
26
|
groupAllowFrom: AllowFromListSchema,
|
|
27
|
-
groupPolicy: GroupPolicySchema.optional(),
|
|
27
|
+
groupPolicy: GroupPolicySchema.optional().default("allowlist"),
|
|
28
28
|
groups: z.object({}).catchall(groupConfigSchema).optional(),
|
|
29
29
|
messagePrefix: z.string().optional(),
|
|
30
30
|
responsePrefix: z.string().optional(),
|
package/src/directory.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { resolveZalouserAccountSync } from "./accounts.js";
|
|
2
|
+
import type { ChannelDirectoryEntry, OpenClawConfig } from "./channel-api.js";
|
|
3
|
+
import { parseZalouserDirectoryGroupId } from "./session-route.js";
|
|
4
|
+
|
|
5
|
+
type ZalouserDirectoryDeps = {
|
|
6
|
+
listZaloGroupMembers: (
|
|
7
|
+
profile: string,
|
|
8
|
+
groupId: string,
|
|
9
|
+
) => Promise<
|
|
10
|
+
Array<{
|
|
11
|
+
userId: string;
|
|
12
|
+
displayName?: string | null;
|
|
13
|
+
avatar?: string | null;
|
|
14
|
+
}>
|
|
15
|
+
>;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
function mapUser(params: {
|
|
19
|
+
id: string;
|
|
20
|
+
name?: string | null;
|
|
21
|
+
avatarUrl?: string | null;
|
|
22
|
+
raw?: unknown;
|
|
23
|
+
}): ChannelDirectoryEntry {
|
|
24
|
+
return {
|
|
25
|
+
kind: "user",
|
|
26
|
+
id: params.id,
|
|
27
|
+
name: params.name ?? undefined,
|
|
28
|
+
avatarUrl: params.avatarUrl ?? undefined,
|
|
29
|
+
raw: params.raw,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export async function listZalouserDirectoryGroupMembers(
|
|
34
|
+
params: {
|
|
35
|
+
cfg: OpenClawConfig;
|
|
36
|
+
accountId?: string;
|
|
37
|
+
groupId: string;
|
|
38
|
+
limit?: number;
|
|
39
|
+
},
|
|
40
|
+
deps: ZalouserDirectoryDeps,
|
|
41
|
+
) {
|
|
42
|
+
const account = resolveZalouserAccountSync({ cfg: params.cfg, accountId: params.accountId });
|
|
43
|
+
const normalizedGroupId = parseZalouserDirectoryGroupId(params.groupId);
|
|
44
|
+
const members = await deps.listZaloGroupMembers(account.profile, normalizedGroupId);
|
|
45
|
+
const rows = members.map((member) =>
|
|
46
|
+
mapUser({
|
|
47
|
+
id: member.userId,
|
|
48
|
+
name: member.displayName,
|
|
49
|
+
avatarUrl: member.avatar ?? null,
|
|
50
|
+
raw: member,
|
|
51
|
+
}),
|
|
52
|
+
);
|
|
53
|
+
return typeof params.limit === "number" && params.limit > 0 ? rows.slice(0, params.limit) : rows;
|
|
54
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ChannelDoctorConfigMutation,
|
|
3
|
+
ChannelDoctorLegacyConfigRule,
|
|
4
|
+
} from "openclaw/plugin-sdk/channel-contract";
|
|
5
|
+
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-types";
|
|
6
|
+
|
|
7
|
+
type ZalouserChannelsConfig = NonNullable<OpenClawConfig["channels"]>;
|
|
8
|
+
|
|
9
|
+
function asObjectRecord(value: unknown): Record<string, unknown> | null {
|
|
10
|
+
return value && typeof value === "object" && !Array.isArray(value)
|
|
11
|
+
? (value as Record<string, unknown>)
|
|
12
|
+
: null;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function hasLegacyZalouserGroupAllowAlias(value: unknown): boolean {
|
|
16
|
+
const group = asObjectRecord(value);
|
|
17
|
+
return Boolean(group && typeof group.allow === "boolean");
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function hasLegacyZalouserGroupAllowAliases(value: unknown): boolean {
|
|
21
|
+
const groups = asObjectRecord(value);
|
|
22
|
+
return Boolean(
|
|
23
|
+
groups && Object.values(groups).some((group) => hasLegacyZalouserGroupAllowAlias(group)),
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function hasLegacyZalouserAccountGroupAllowAliases(value: unknown): boolean {
|
|
28
|
+
const accounts = asObjectRecord(value);
|
|
29
|
+
if (!accounts) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
return Object.values(accounts).some((account) => {
|
|
33
|
+
const accountRecord = asObjectRecord(account);
|
|
34
|
+
return Boolean(accountRecord && hasLegacyZalouserGroupAllowAliases(accountRecord.groups));
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function normalizeZalouserGroupAllowAliases(params: {
|
|
39
|
+
groups: Record<string, unknown>;
|
|
40
|
+
pathPrefix: string;
|
|
41
|
+
changes: string[];
|
|
42
|
+
}): { groups: Record<string, unknown>; changed: boolean } {
|
|
43
|
+
let changed = false;
|
|
44
|
+
const nextGroups: Record<string, unknown> = { ...params.groups };
|
|
45
|
+
for (const [groupId, groupValue] of Object.entries(params.groups)) {
|
|
46
|
+
const group = asObjectRecord(groupValue);
|
|
47
|
+
if (!group || typeof group.allow !== "boolean") {
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
const nextGroup = { ...group };
|
|
51
|
+
if (typeof nextGroup.enabled !== "boolean") {
|
|
52
|
+
nextGroup.enabled = group.allow;
|
|
53
|
+
}
|
|
54
|
+
delete nextGroup.allow;
|
|
55
|
+
nextGroups[groupId] = nextGroup;
|
|
56
|
+
changed = true;
|
|
57
|
+
params.changes.push(
|
|
58
|
+
`Moved ${params.pathPrefix}.${groupId}.allow → ${params.pathPrefix}.${groupId}.enabled (${String(nextGroup.enabled)}).`,
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
return { groups: nextGroups, changed };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function normalizeZalouserCompatibilityConfig(cfg: OpenClawConfig): ChannelDoctorConfigMutation {
|
|
65
|
+
const channels = asObjectRecord(cfg.channels);
|
|
66
|
+
const zalouser = asObjectRecord(channels?.zalouser);
|
|
67
|
+
if (!zalouser) {
|
|
68
|
+
return { config: cfg, changes: [] };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const changes: string[] = [];
|
|
72
|
+
let updatedZalouser: Record<string, unknown> = zalouser;
|
|
73
|
+
let changed = false;
|
|
74
|
+
|
|
75
|
+
const groups = asObjectRecord(updatedZalouser.groups);
|
|
76
|
+
if (groups) {
|
|
77
|
+
const normalized = normalizeZalouserGroupAllowAliases({
|
|
78
|
+
groups,
|
|
79
|
+
pathPrefix: "channels.zalouser.groups",
|
|
80
|
+
changes,
|
|
81
|
+
});
|
|
82
|
+
if (normalized.changed) {
|
|
83
|
+
updatedZalouser = { ...updatedZalouser, groups: normalized.groups };
|
|
84
|
+
changed = true;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const accounts = asObjectRecord(updatedZalouser.accounts);
|
|
89
|
+
if (accounts) {
|
|
90
|
+
let accountsChanged = false;
|
|
91
|
+
const nextAccounts: Record<string, unknown> = { ...accounts };
|
|
92
|
+
for (const [accountId, accountValue] of Object.entries(accounts)) {
|
|
93
|
+
const account = asObjectRecord(accountValue);
|
|
94
|
+
if (!account) {
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
const accountGroups = asObjectRecord(account.groups);
|
|
98
|
+
if (!accountGroups) {
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
const normalized = normalizeZalouserGroupAllowAliases({
|
|
102
|
+
groups: accountGroups,
|
|
103
|
+
pathPrefix: `channels.zalouser.accounts.${accountId}.groups`,
|
|
104
|
+
changes,
|
|
105
|
+
});
|
|
106
|
+
if (!normalized.changed) {
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
nextAccounts[accountId] = {
|
|
110
|
+
...account,
|
|
111
|
+
groups: normalized.groups,
|
|
112
|
+
};
|
|
113
|
+
accountsChanged = true;
|
|
114
|
+
}
|
|
115
|
+
if (accountsChanged) {
|
|
116
|
+
updatedZalouser = { ...updatedZalouser, accounts: nextAccounts };
|
|
117
|
+
changed = true;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (!changed) {
|
|
122
|
+
return { config: cfg, changes: [] };
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return {
|
|
126
|
+
config: {
|
|
127
|
+
...cfg,
|
|
128
|
+
channels: {
|
|
129
|
+
...cfg.channels,
|
|
130
|
+
zalouser: updatedZalouser as ZalouserChannelsConfig["zalouser"],
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
changes,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export const legacyConfigRules: ChannelDoctorLegacyConfigRule[] = [
|
|
138
|
+
{
|
|
139
|
+
path: ["channels", "zalouser", "groups"],
|
|
140
|
+
message:
|
|
141
|
+
'channels.zalouser.groups.<id>.allow is legacy; use channels.zalouser.groups.<id>.enabled instead. Run "openclaw doctor --fix".',
|
|
142
|
+
match: hasLegacyZalouserGroupAllowAliases,
|
|
143
|
+
},
|
|
144
|
+
{
|
|
145
|
+
path: ["channels", "zalouser", "accounts"],
|
|
146
|
+
message:
|
|
147
|
+
'channels.zalouser.accounts.<id>.groups.<id>.allow is legacy; use channels.zalouser.accounts.<id>.groups.<id>.enabled instead. Run "openclaw doctor --fix".',
|
|
148
|
+
match: hasLegacyZalouserAccountGroupAllowAliases,
|
|
149
|
+
},
|
|
150
|
+
];
|
|
151
|
+
|
|
152
|
+
export function normalizeCompatibilityConfig(params: {
|
|
153
|
+
cfg: OpenClawConfig;
|
|
154
|
+
}): ChannelDoctorConfigMutation {
|
|
155
|
+
return normalizeZalouserCompatibilityConfig(params.cfg);
|
|
156
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { zalouserDoctor } from "./doctor.js";
|
|
3
|
+
|
|
4
|
+
describe("zalouser doctor", () => {
|
|
5
|
+
it("warns when mutable group names rely on disabled name matching", () => {
|
|
6
|
+
expect(
|
|
7
|
+
zalouserDoctor.collectMutableAllowlistWarnings?.({
|
|
8
|
+
cfg: {
|
|
9
|
+
channels: {
|
|
10
|
+
zalouser: {
|
|
11
|
+
groups: {
|
|
12
|
+
"group:trusted": {
|
|
13
|
+
enabled: true,
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
} as never,
|
|
19
|
+
}),
|
|
20
|
+
).toEqual(
|
|
21
|
+
expect.arrayContaining([
|
|
22
|
+
expect.stringContaining("mutable allowlist entry across zalouser"),
|
|
23
|
+
expect.stringContaining("channels.zalouser.groups: group:trusted"),
|
|
24
|
+
]),
|
|
25
|
+
);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("normalizes legacy group allow aliases to enabled", () => {
|
|
29
|
+
const normalize = zalouserDoctor.normalizeCompatibilityConfig;
|
|
30
|
+
expect(normalize).toBeDefined();
|
|
31
|
+
if (!normalize) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const result = normalize({
|
|
36
|
+
cfg: {
|
|
37
|
+
channels: {
|
|
38
|
+
zalouser: {
|
|
39
|
+
groups: {
|
|
40
|
+
"group:trusted": {
|
|
41
|
+
allow: true,
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
accounts: {
|
|
45
|
+
work: {
|
|
46
|
+
groups: {
|
|
47
|
+
"group:legacy": {
|
|
48
|
+
allow: false,
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
} as never,
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
expect(result.config.channels?.zalouser?.groups?.["group:trusted"]).toEqual({
|
|
59
|
+
enabled: true,
|
|
60
|
+
});
|
|
61
|
+
expect(
|
|
62
|
+
(
|
|
63
|
+
result.config.channels?.zalouser?.accounts?.work as
|
|
64
|
+
| { groups?: Record<string, unknown> }
|
|
65
|
+
| undefined
|
|
66
|
+
)?.groups?.["group:legacy"],
|
|
67
|
+
).toEqual({
|
|
68
|
+
enabled: false,
|
|
69
|
+
});
|
|
70
|
+
expect(result.changes).toEqual(
|
|
71
|
+
expect.arrayContaining([
|
|
72
|
+
"Moved channels.zalouser.groups.group:trusted.allow → channels.zalouser.groups.group:trusted.enabled (true).",
|
|
73
|
+
"Moved channels.zalouser.accounts.work.groups.group:legacy.allow → channels.zalouser.accounts.work.groups.group:legacy.enabled (false).",
|
|
74
|
+
]),
|
|
75
|
+
);
|
|
76
|
+
});
|
|
77
|
+
});
|
package/src/doctor.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { ChannelDoctorAdapter } from "openclaw/plugin-sdk/channel-contract";
|
|
2
|
+
import { createDangerousNameMatchingMutableAllowlistWarningCollector } from "openclaw/plugin-sdk/channel-policy";
|
|
3
|
+
import { legacyConfigRules, normalizeCompatibilityConfig } from "./doctor-contract.js";
|
|
4
|
+
import { isZalouserMutableGroupEntry } from "./security-audit.js";
|
|
5
|
+
|
|
6
|
+
function asObjectRecord(value: unknown): Record<string, unknown> | null {
|
|
7
|
+
return value && typeof value === "object" && !Array.isArray(value)
|
|
8
|
+
? (value as Record<string, unknown>)
|
|
9
|
+
: null;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const collectZalouserMutableAllowlistWarnings =
|
|
13
|
+
createDangerousNameMatchingMutableAllowlistWarningCollector({
|
|
14
|
+
channel: "zalouser",
|
|
15
|
+
detector: isZalouserMutableGroupEntry,
|
|
16
|
+
collectLists: (scope) => {
|
|
17
|
+
const groups = asObjectRecord(scope.account.groups);
|
|
18
|
+
return groups
|
|
19
|
+
? [
|
|
20
|
+
{
|
|
21
|
+
pathLabel: `${scope.prefix}.groups`,
|
|
22
|
+
list: Object.keys(groups),
|
|
23
|
+
},
|
|
24
|
+
]
|
|
25
|
+
: [];
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
export const zalouserDoctor: ChannelDoctorAdapter = {
|
|
30
|
+
dmAllowFromMode: "topOnly",
|
|
31
|
+
groupModel: "hybrid",
|
|
32
|
+
groupAllowFromFallbackToAllowFrom: false,
|
|
33
|
+
warnOnEmptyGroupSenderAllowlist: false,
|
|
34
|
+
legacyConfigRules,
|
|
35
|
+
normalizeCompatibilityConfig,
|
|
36
|
+
collectMutableAllowlistWarnings: collectZalouserMutableAllowlistWarnings,
|
|
37
|
+
};
|
package/src/group-policy.test.ts
CHANGED
|
@@ -37,7 +37,7 @@ describe("zalouser group policy helpers", () => {
|
|
|
37
37
|
|
|
38
38
|
it("finds the first matching group entry", () => {
|
|
39
39
|
const groups = {
|
|
40
|
-
"group:123": {
|
|
40
|
+
"group:123": { enabled: true },
|
|
41
41
|
"team-alpha": { requireMention: false },
|
|
42
42
|
"*": { requireMention: true },
|
|
43
43
|
};
|
|
@@ -49,12 +49,12 @@ describe("zalouser group policy helpers", () => {
|
|
|
49
49
|
includeGroupIdAlias: true,
|
|
50
50
|
}),
|
|
51
51
|
);
|
|
52
|
-
expect(entry).toEqual({
|
|
52
|
+
expect(entry).toEqual({ enabled: true });
|
|
53
53
|
});
|
|
54
54
|
|
|
55
55
|
it("evaluates allow/enable flags", () => {
|
|
56
|
-
expect(isZalouserGroupEntryAllowed({
|
|
57
|
-
expect(isZalouserGroupEntryAllowed({ allow: false })).toBe(false);
|
|
56
|
+
expect(isZalouserGroupEntryAllowed({ enabled: true })).toBe(true);
|
|
57
|
+
expect(isZalouserGroupEntryAllowed({ allow: false } as never)).toBe(false);
|
|
58
58
|
expect(isZalouserGroupEntryAllowed({ enabled: false })).toBe(false);
|
|
59
59
|
expect(isZalouserGroupEntryAllowed(undefined)).toBe(false);
|
|
60
60
|
});
|
package/src/group-policy.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { normalizeOptionalLowercaseString } from "openclaw/plugin-sdk/text-runtime";
|
|
1
2
|
import type { ZalouserGroupConfig } from "./types.js";
|
|
2
3
|
|
|
3
4
|
type ZalouserGroups = Record<string, ZalouserGroupConfig>;
|
|
@@ -7,7 +8,7 @@ function toGroupCandidate(value?: string | null): string {
|
|
|
7
8
|
}
|
|
8
9
|
|
|
9
10
|
export function normalizeZalouserGroupSlug(raw?: string | null): string {
|
|
10
|
-
const trimmed = raw
|
|
11
|
+
const trimmed = normalizeOptionalLowercaseString(raw) ?? "";
|
|
11
12
|
if (!trimmed) {
|
|
12
13
|
return "";
|
|
13
14
|
}
|
|
@@ -77,5 +78,6 @@ export function isZalouserGroupEntryAllowed(entry: ZalouserGroupConfig | undefin
|
|
|
77
78
|
if (!entry) {
|
|
78
79
|
return false;
|
|
79
80
|
}
|
|
80
|
-
|
|
81
|
+
const legacyAllow = (entry as ZalouserGroupConfig & { allow?: unknown }).allow;
|
|
82
|
+
return legacyAllow !== false && entry.enabled !== false;
|
|
81
83
|
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import type { OpenClawConfig, PluginRuntime, RuntimeEnv } from "openclaw/plugin-sdk/zalouser";
|
|
2
1
|
import { describe, expect, it, vi } from "vitest";
|
|
2
|
+
import type { OpenClawConfig, PluginRuntime } from "../runtime-api.js";
|
|
3
3
|
import "./monitor.send-mocks.js";
|
|
4
4
|
import { __testing } from "./monitor.js";
|
|
5
|
+
import "./zalo-js.test-mocks.js";
|
|
5
6
|
import { sendMessageZalouserMock } from "./monitor.send-mocks.js";
|
|
6
7
|
import { setZalouserRuntime } from "./runtime.js";
|
|
7
8
|
import { createZalouserRuntimeEnv } from "./test-helpers.js";
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import type { OpenClawConfig, PluginRuntime, RuntimeEnv } from "openclaw/plugin-sdk/zalouser";
|
|
2
1
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import type { OpenClawConfig, PluginRuntime } from "../runtime-api.js";
|
|
3
3
|
import "./monitor.send-mocks.js";
|
|
4
|
+
import "./zalo-js.test-mocks.js";
|
|
5
|
+
import { resolveZalouserAccountSync } from "./accounts.js";
|
|
4
6
|
import { __testing } from "./monitor.js";
|
|
5
7
|
import {
|
|
6
8
|
sendDeliveredZalouserMock,
|
|
@@ -19,6 +21,8 @@ function createAccount(): ResolvedZalouserAccount {
|
|
|
19
21
|
profile: "default",
|
|
20
22
|
authenticated: true,
|
|
21
23
|
config: {
|
|
24
|
+
dmPolicy: "open",
|
|
25
|
+
allowFrom: ["*"],
|
|
22
26
|
groupPolicy: "open",
|
|
23
27
|
groups: {
|
|
24
28
|
"*": { requireMention: true },
|
|
@@ -32,6 +36,8 @@ function createConfig(): OpenClawConfig {
|
|
|
32
36
|
channels: {
|
|
33
37
|
zalouser: {
|
|
34
38
|
enabled: true,
|
|
39
|
+
dmPolicy: "open",
|
|
40
|
+
allowFrom: ["*"],
|
|
35
41
|
groups: {
|
|
36
42
|
"*": { requireMention: true },
|
|
37
43
|
},
|
|
@@ -83,6 +89,91 @@ function installRuntime(params: {
|
|
|
83
89
|
const readSessionUpdatedAt = vi.fn(
|
|
84
90
|
(_params?: { storePath: string; sessionKey: string }): number | undefined => undefined,
|
|
85
91
|
);
|
|
92
|
+
type ResolvedTurn = Awaited<
|
|
93
|
+
ReturnType<Parameters<PluginRuntime["channel"]["turn"]["run"]>[0]["adapter"]["resolveTurn"]>
|
|
94
|
+
>;
|
|
95
|
+
const dispatchAssembled = vi.fn(async (turn: ResolvedTurn) => {
|
|
96
|
+
await turn.recordInboundSession({
|
|
97
|
+
storePath: turn.storePath,
|
|
98
|
+
sessionKey: turn.ctxPayload.SessionKey ?? turn.routeSessionKey,
|
|
99
|
+
ctx: turn.ctxPayload,
|
|
100
|
+
groupResolution: turn.record?.groupResolution,
|
|
101
|
+
createIfMissing: turn.record?.createIfMissing,
|
|
102
|
+
updateLastRoute: turn.record?.updateLastRoute,
|
|
103
|
+
onRecordError: turn.record?.onRecordError ?? (() => undefined),
|
|
104
|
+
});
|
|
105
|
+
if ("runDispatch" in turn) {
|
|
106
|
+
const dispatchResult = await turn.runDispatch();
|
|
107
|
+
return {
|
|
108
|
+
admission: { kind: "dispatch" as const },
|
|
109
|
+
dispatched: true,
|
|
110
|
+
ctxPayload: turn.ctxPayload,
|
|
111
|
+
routeSessionKey: turn.routeSessionKey,
|
|
112
|
+
dispatchResult,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
const dispatchResult = await turn.dispatchReplyWithBufferedBlockDispatcher({
|
|
116
|
+
ctx: turn.ctxPayload,
|
|
117
|
+
cfg: turn.cfg,
|
|
118
|
+
dispatcherOptions: {
|
|
119
|
+
...turn.dispatcherOptions,
|
|
120
|
+
deliver: async (...args: Parameters<typeof turn.delivery.deliver>) => {
|
|
121
|
+
await turn.delivery.deliver(...args);
|
|
122
|
+
},
|
|
123
|
+
onError: turn.delivery.onError,
|
|
124
|
+
},
|
|
125
|
+
replyOptions: turn.replyOptions,
|
|
126
|
+
replyResolver: turn.replyResolver,
|
|
127
|
+
});
|
|
128
|
+
return {
|
|
129
|
+
admission: { kind: "dispatch" as const },
|
|
130
|
+
dispatched: true,
|
|
131
|
+
ctxPayload: turn.ctxPayload,
|
|
132
|
+
routeSessionKey: turn.routeSessionKey,
|
|
133
|
+
dispatchResult,
|
|
134
|
+
};
|
|
135
|
+
});
|
|
136
|
+
const runTurn = vi.fn(async (params: Parameters<PluginRuntime["channel"]["turn"]["run"]>[0]) => {
|
|
137
|
+
const input = await params.adapter.ingest(params.raw);
|
|
138
|
+
if (!input) {
|
|
139
|
+
return { admission: { kind: "drop" as const, reason: "ingest-null" }, dispatched: false };
|
|
140
|
+
}
|
|
141
|
+
const resolved = await params.adapter.resolveTurn(
|
|
142
|
+
input,
|
|
143
|
+
{
|
|
144
|
+
kind: "message",
|
|
145
|
+
canStartAgentTurn: true,
|
|
146
|
+
},
|
|
147
|
+
{},
|
|
148
|
+
);
|
|
149
|
+
return await dispatchAssembled(resolved);
|
|
150
|
+
});
|
|
151
|
+
const buildContext = vi.fn(
|
|
152
|
+
(params: Parameters<PluginRuntime["channel"]["turn"]["buildContext"]>[0]) =>
|
|
153
|
+
({
|
|
154
|
+
Body: params.message.body ?? params.message.rawBody,
|
|
155
|
+
BodyForAgent: params.message.bodyForAgent ?? params.message.rawBody,
|
|
156
|
+
InboundHistory: params.message.inboundHistory,
|
|
157
|
+
RawBody: params.message.rawBody,
|
|
158
|
+
CommandBody: params.message.commandBody ?? params.message.rawBody,
|
|
159
|
+
BodyForCommands: params.message.commandBody ?? params.message.rawBody,
|
|
160
|
+
From: params.from,
|
|
161
|
+
To: params.reply.to,
|
|
162
|
+
SessionKey: params.route.dispatchSessionKey ?? params.route.routeSessionKey,
|
|
163
|
+
AccountId: params.route.accountId ?? params.accountId,
|
|
164
|
+
ChatType: params.conversation.kind,
|
|
165
|
+
ConversationLabel: params.conversation.label,
|
|
166
|
+
SenderName: params.sender.name,
|
|
167
|
+
SenderId: params.sender.id,
|
|
168
|
+
Provider: params.provider ?? params.channel,
|
|
169
|
+
Surface: params.surface ?? params.provider ?? params.channel,
|
|
170
|
+
MessageSid: params.messageId,
|
|
171
|
+
MessageSidFull: params.messageIdFull,
|
|
172
|
+
OriginatingChannel: params.channel,
|
|
173
|
+
OriginatingTo: params.reply.originatingTo,
|
|
174
|
+
...params.extra,
|
|
175
|
+
}) as ReturnType<PluginRuntime["channel"]["turn"]["buildContext"]>,
|
|
176
|
+
);
|
|
86
177
|
const buildAgentSessionKey = vi.fn(
|
|
87
178
|
(input: {
|
|
88
179
|
agentId: string;
|
|
@@ -134,8 +225,9 @@ function installRuntime(params: {
|
|
|
134
225
|
resolveRequireMention: vi.fn((input) => {
|
|
135
226
|
const cfg = input.cfg as OpenClawConfig;
|
|
136
227
|
const groupCfg = cfg.channels?.zalouser?.groups ?? {};
|
|
137
|
-
const
|
|
138
|
-
const
|
|
228
|
+
const typedGroupCfg = groupCfg as Record<string, { requireMention?: boolean }>;
|
|
229
|
+
const groupEntry = input.groupId ? typedGroupCfg[input.groupId] : undefined;
|
|
230
|
+
const defaultEntry = typedGroupCfg["*"];
|
|
139
231
|
if (typeof groupEntry?.requireMention === "boolean") {
|
|
140
232
|
return groupEntry.requireMention;
|
|
141
233
|
}
|
|
@@ -160,6 +252,10 @@ function installRuntime(params: {
|
|
|
160
252
|
finalizeInboundContext: vi.fn((ctx) => ctx),
|
|
161
253
|
dispatchReplyWithBufferedBlockDispatcher,
|
|
162
254
|
},
|
|
255
|
+
turn: {
|
|
256
|
+
run: runTurn as unknown as PluginRuntime["channel"]["turn"]["run"],
|
|
257
|
+
buildContext: buildContext as unknown as PluginRuntime["channel"]["turn"]["buildContext"],
|
|
258
|
+
},
|
|
163
259
|
text: {
|
|
164
260
|
resolveMarkdownTableMode: vi.fn(() => "code"),
|
|
165
261
|
convertMarkdownTables: vi.fn((text: string) => text),
|
|
@@ -346,8 +442,8 @@ describe("zalouser monitor group mention gating", () => {
|
|
|
346
442
|
groupPolicy: "allowlist",
|
|
347
443
|
groupAllowFrom: ["*"],
|
|
348
444
|
groups: {
|
|
349
|
-
"group:g-trusted-001": {
|
|
350
|
-
"Trusted Team": {
|
|
445
|
+
"group:g-trusted-001": { enabled: true },
|
|
446
|
+
"Trusted Team": { enabled: true },
|
|
351
447
|
},
|
|
352
448
|
},
|
|
353
449
|
},
|
|
@@ -376,6 +472,34 @@ describe("zalouser monitor group mention gating", () => {
|
|
|
376
472
|
await expectSkippedGroupMessage();
|
|
377
473
|
});
|
|
378
474
|
|
|
475
|
+
it("blocks mentioned group messages by default when groupPolicy is omitted", async () => {
|
|
476
|
+
const { dispatchReplyWithBufferedBlockDispatcher } = installRuntime({
|
|
477
|
+
commandAuthorized: false,
|
|
478
|
+
});
|
|
479
|
+
const cfg: OpenClawConfig = {
|
|
480
|
+
channels: {
|
|
481
|
+
zalouser: {
|
|
482
|
+
enabled: true,
|
|
483
|
+
},
|
|
484
|
+
},
|
|
485
|
+
};
|
|
486
|
+
const account = resolveZalouserAccountSync({ cfg, accountId: "default" });
|
|
487
|
+
|
|
488
|
+
await __testing.processMessage({
|
|
489
|
+
message: createGroupMessage({
|
|
490
|
+
content: "ping @bot",
|
|
491
|
+
hasAnyMention: true,
|
|
492
|
+
wasExplicitlyMentioned: true,
|
|
493
|
+
}),
|
|
494
|
+
account,
|
|
495
|
+
config: cfg,
|
|
496
|
+
runtime: createRuntimeEnv(),
|
|
497
|
+
});
|
|
498
|
+
|
|
499
|
+
expect(account.config.groupPolicy).toBe("allowlist");
|
|
500
|
+
expect(dispatchReplyWithBufferedBlockDispatcher).not.toHaveBeenCalled();
|
|
501
|
+
});
|
|
502
|
+
|
|
379
503
|
it("fails closed when requireMention=true but mention detection is unavailable", async () => {
|
|
380
504
|
await expectSkippedGroupMessage({
|
|
381
505
|
canResolveExplicitMention: false,
|
|
@@ -477,7 +601,36 @@ describe("zalouser monitor group mention gating", () => {
|
|
|
477
601
|
});
|
|
478
602
|
});
|
|
479
603
|
|
|
480
|
-
it("blocks
|
|
604
|
+
it("blocks routed allowlist groups without an explicit group sender allowlist", async () => {
|
|
605
|
+
const { dispatchReplyWithBufferedBlockDispatcher } = installRuntime({
|
|
606
|
+
commandAuthorized: false,
|
|
607
|
+
});
|
|
608
|
+
await __testing.processMessage({
|
|
609
|
+
message: createGroupMessage({
|
|
610
|
+
content: "ping @bot",
|
|
611
|
+
hasAnyMention: true,
|
|
612
|
+
wasExplicitlyMentioned: true,
|
|
613
|
+
senderId: "456",
|
|
614
|
+
}),
|
|
615
|
+
account: {
|
|
616
|
+
...createAccount(),
|
|
617
|
+
config: {
|
|
618
|
+
...createAccount().config,
|
|
619
|
+
groupPolicy: "allowlist",
|
|
620
|
+
allowFrom: ["123"],
|
|
621
|
+
groups: {
|
|
622
|
+
"group:g-1": { enabled: true, requireMention: true },
|
|
623
|
+
},
|
|
624
|
+
},
|
|
625
|
+
},
|
|
626
|
+
config: createConfig(),
|
|
627
|
+
runtime: createRuntimeEnv(),
|
|
628
|
+
});
|
|
629
|
+
|
|
630
|
+
expect(dispatchReplyWithBufferedBlockDispatcher).not.toHaveBeenCalled();
|
|
631
|
+
});
|
|
632
|
+
|
|
633
|
+
it("blocks group messages when sender is not in groupAllowFrom", async () => {
|
|
481
634
|
const { dispatchReplyWithBufferedBlockDispatcher } = installRuntime({
|
|
482
635
|
commandAuthorized: false,
|
|
483
636
|
});
|
|
@@ -493,6 +646,7 @@ describe("zalouser monitor group mention gating", () => {
|
|
|
493
646
|
...createAccount().config,
|
|
494
647
|
groupPolicy: "allowlist",
|
|
495
648
|
allowFrom: ["999"],
|
|
649
|
+
groupAllowFrom: ["999"],
|
|
496
650
|
},
|
|
497
651
|
},
|
|
498
652
|
config: createConfig(),
|
|
@@ -558,7 +712,7 @@ describe("zalouser monitor group mention gating", () => {
|
|
|
558
712
|
expect(callArg?.ctx?.SessionKey).toBe("agent:main:zalouser:group:321");
|
|
559
713
|
});
|
|
560
714
|
|
|
561
|
-
it("
|
|
715
|
+
it("skips pairing store read for open DM control commands", async () => {
|
|
562
716
|
const { readAllowFromStore } = installRuntime({
|
|
563
717
|
commandAuthorized: false,
|
|
564
718
|
});
|
|
@@ -576,7 +730,7 @@ describe("zalouser monitor group mention gating", () => {
|
|
|
576
730
|
runtime: createRuntimeEnv(),
|
|
577
731
|
});
|
|
578
732
|
|
|
579
|
-
expect(readAllowFromStore).
|
|
733
|
+
expect(readAllowFromStore).not.toHaveBeenCalled();
|
|
580
734
|
});
|
|
581
735
|
|
|
582
736
|
it("skips pairing store read for open DM non-command messages", async () => {
|