Skip to content

Commit 5845b5b

Browse files
committed
refactor: share multi-account config schema fragments
1 parent 52a253f commit 5845b5b

5 files changed

Lines changed: 34 additions & 24 deletions

File tree

extensions/bluebubbles/src/config-schema.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1+
import { AllowFromEntrySchema, buildCatchallMultiAccountChannelSchema } from "openclaw/plugin-sdk";
12
import { MarkdownConfigSchema, ToolPolicySchema } from "openclaw/plugin-sdk/bluebubbles";
23
import { z } from "zod";
34
import { buildSecretInputSchema, hasConfiguredSecretInput } from "./secret-input.js";
45

5-
const allowFromEntry = z.union([z.string(), z.number()]);
6-
76
const bluebubblesActionSchema = z
87
.object({
98
reactions: z.boolean().default(true),
@@ -34,8 +33,8 @@ const bluebubblesAccountSchema = z
3433
password: buildSecretInputSchema().optional(),
3534
webhookPath: z.string().optional(),
3635
dmPolicy: z.enum(["pairing", "allowlist", "open", "disabled"]).optional(),
37-
allowFrom: z.array(allowFromEntry).optional(),
38-
groupAllowFrom: z.array(allowFromEntry).optional(),
36+
allowFrom: z.array(AllowFromEntrySchema).optional(),
37+
groupAllowFrom: z.array(AllowFromEntrySchema).optional(),
3938
groupPolicy: z.enum(["open", "disabled", "allowlist"]).optional(),
4039
historyLimit: z.number().int().min(0).optional(),
4140
dmHistoryLimit: z.number().int().min(0).optional(),
@@ -60,8 +59,8 @@ const bluebubblesAccountSchema = z
6059
}
6160
});
6261

63-
export const BlueBubblesConfigSchema = bluebubblesAccountSchema.extend({
64-
accounts: z.object({}).catchall(bluebubblesAccountSchema).optional(),
65-
defaultAccount: z.string().optional(),
62+
export const BlueBubblesConfigSchema = buildCatchallMultiAccountChannelSchema(
63+
bluebubblesAccountSchema,
64+
).extend({
6665
actions: bluebubblesActionSchema,
6766
});
Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1+
import { AllowFromEntrySchema, buildCatchallMultiAccountChannelSchema } from "openclaw/plugin-sdk";
12
import { MarkdownConfigSchema } from "openclaw/plugin-sdk/zalo";
23
import { z } from "zod";
34
import { buildSecretInputSchema } from "./secret-input.js";
45

5-
const allowFromEntry = z.union([z.string(), z.number()]);
6-
76
const zaloAccountSchema = z.object({
87
name: z.string().optional(),
98
enabled: z.boolean().optional(),
@@ -14,15 +13,12 @@ const zaloAccountSchema = z.object({
1413
webhookSecret: buildSecretInputSchema().optional(),
1514
webhookPath: z.string().optional(),
1615
dmPolicy: z.enum(["pairing", "allowlist", "open", "disabled"]).optional(),
17-
allowFrom: z.array(allowFromEntry).optional(),
16+
allowFrom: z.array(AllowFromEntrySchema).optional(),
1817
groupPolicy: z.enum(["disabled", "allowlist", "open"]).optional(),
19-
groupAllowFrom: z.array(allowFromEntry).optional(),
18+
groupAllowFrom: z.array(AllowFromEntrySchema).optional(),
2019
mediaMaxMb: z.number().optional(),
2120
proxy: z.string().optional(),
2221
responsePrefix: z.string().optional(),
2322
});
2423

25-
export const ZaloConfigSchema = zaloAccountSchema.extend({
26-
accounts: z.object({}).catchall(zaloAccountSchema).optional(),
27-
defaultAccount: z.string().optional(),
28-
});
24+
export const ZaloConfigSchema = buildCatchallMultiAccountChannelSchema(zaloAccountSchema);
Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
import { AllowFromEntrySchema, buildCatchallMultiAccountChannelSchema } from "openclaw/plugin-sdk";
12
import { MarkdownConfigSchema, ToolPolicySchema } from "openclaw/plugin-sdk/zalouser";
23
import { z } from "zod";
34

4-
const allowFromEntry = z.union([z.string(), z.number()]);
5-
65
const groupConfigSchema = z.object({
76
allow: z.boolean().optional(),
87
enabled: z.boolean().optional(),
@@ -16,16 +15,13 @@ const zalouserAccountSchema = z.object({
1615
markdown: MarkdownConfigSchema,
1716
profile: z.string().optional(),
1817
dmPolicy: z.enum(["pairing", "allowlist", "open", "disabled"]).optional(),
19-
allowFrom: z.array(allowFromEntry).optional(),
18+
allowFrom: z.array(AllowFromEntrySchema).optional(),
2019
historyLimit: z.number().int().min(0).optional(),
21-
groupAllowFrom: z.array(allowFromEntry).optional(),
20+
groupAllowFrom: z.array(AllowFromEntrySchema).optional(),
2221
groupPolicy: z.enum(["disabled", "allowlist", "open"]).optional(),
2322
groups: z.object({}).catchall(groupConfigSchema).optional(),
2423
messagePrefix: z.string().optional(),
2524
responsePrefix: z.string().optional(),
2625
});
2726

28-
export const ZalouserConfigSchema = zalouserAccountSchema.extend({
29-
accounts: z.object({}).catchall(zalouserAccountSchema).optional(),
30-
defaultAccount: z.string().optional(),
31-
});
27+
export const ZalouserConfigSchema = buildCatchallMultiAccountChannelSchema(zalouserAccountSchema);

src/channels/plugins/config-schema.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
1-
import type { ZodTypeAny } from "zod";
1+
import { z, type ZodTypeAny } from "zod";
22
import type { ChannelConfigSchema } from "./types.plugin.js";
33

44
type ZodSchemaWithToJsonSchema = ZodTypeAny & {
55
toJSONSchema?: (params?: Record<string, unknown>) => unknown;
66
};
77

8+
type ExtendableZodObject = ZodTypeAny & {
9+
extend: (shape: Record<string, ZodTypeAny>) => ZodTypeAny;
10+
};
11+
12+
export const AllowFromEntrySchema = z.union([z.string(), z.number()]);
13+
14+
export function buildCatchallMultiAccountChannelSchema<T extends ExtendableZodObject>(
15+
accountSchema: T,
16+
): T {
17+
return accountSchema.extend({
18+
accounts: z.object({}).catchall(accountSchema).optional(),
19+
defaultAccount: z.string().optional(),
20+
}) as T;
21+
}
22+
823
export function buildChannelConfigSchema(schema: ZodTypeAny): ChannelConfigSchema {
924
const schemaWithJson = schema as ZodSchemaWithToJsonSchema;
1025
if (typeof schemaWithJson.toJSONSchema === "function") {

src/plugin-sdk/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ export { formatResolvedUnresolvedNote } from "./resolution-notes.js";
195195
export { buildChannelSendResult } from "./channel-send-result.js";
196196
export type { ChannelSendRawResult } from "./channel-send-result.js";
197197
export { createPluginRuntimeStore } from "./runtime-store.js";
198+
export {
199+
AllowFromEntrySchema,
200+
buildCatchallMultiAccountChannelSchema,
201+
} from "../channels/plugins/config-schema.js";
198202
export type { ChannelDock } from "../channels/dock.js";
199203
export { getChatChannelMeta } from "../channels/registry.js";
200204
export { resolveAllowlistMatchByCandidates } from "../channels/allowlist-match.js";

0 commit comments

Comments
 (0)