|
| 1 | +import type { |
| 2 | + ChannelDoctorAdapter, |
| 3 | + ChannelDoctorConfigMutation, |
| 4 | + ChannelDoctorLegacyConfigRule, |
| 5 | +} from "openclaw/plugin-sdk/channel-contract"; |
| 6 | +import type { OpenClawConfig } from "openclaw/plugin-sdk/config-runtime"; |
| 7 | +import { |
| 8 | + hasLegacyFlatAllowPrivateNetworkAlias, |
| 9 | + migrateLegacyFlatAllowPrivateNetworkAlias, |
| 10 | +} from "openclaw/plugin-sdk/ssrf-runtime"; |
| 11 | + |
| 12 | +function isRecord(value: unknown): value is Record<string, unknown> { |
| 13 | + return Boolean(value) && typeof value === "object" && !Array.isArray(value); |
| 14 | +} |
| 15 | + |
| 16 | +function hasLegacyAllowPrivateNetworkInAccounts(value: unknown): boolean { |
| 17 | + const accounts = isRecord(value) ? value : null; |
| 18 | + return Boolean( |
| 19 | + accounts && |
| 20 | + Object.values(accounts).some((account) => |
| 21 | + hasLegacyFlatAllowPrivateNetworkAlias(isRecord(account) ? account : {}), |
| 22 | + ), |
| 23 | + ); |
| 24 | +} |
| 25 | + |
| 26 | +function normalizeBlueBubblesCompatibilityConfig(cfg: OpenClawConfig): ChannelDoctorConfigMutation { |
| 27 | + const channels = isRecord(cfg.channels) ? cfg.channels : null; |
| 28 | + const bluebubbles = isRecord(channels?.bluebubbles) ? channels.bluebubbles : null; |
| 29 | + if (!bluebubbles) { |
| 30 | + return { config: cfg, changes: [] }; |
| 31 | + } |
| 32 | + |
| 33 | + const changes: string[] = []; |
| 34 | + let updatedBluebubbles = bluebubbles; |
| 35 | + let changed = false; |
| 36 | + |
| 37 | + const topLevel = migrateLegacyFlatAllowPrivateNetworkAlias({ |
| 38 | + entry: updatedBluebubbles, |
| 39 | + pathPrefix: "channels.bluebubbles", |
| 40 | + changes, |
| 41 | + }); |
| 42 | + updatedBluebubbles = topLevel.entry; |
| 43 | + changed = changed || topLevel.changed; |
| 44 | + |
| 45 | + const accounts = isRecord(updatedBluebubbles.accounts) ? updatedBluebubbles.accounts : null; |
| 46 | + if (accounts) { |
| 47 | + let accountsChanged = false; |
| 48 | + const nextAccounts: Record<string, unknown> = { ...accounts }; |
| 49 | + for (const [accountId, accountValue] of Object.entries(accounts)) { |
| 50 | + const account = isRecord(accountValue) ? accountValue : null; |
| 51 | + if (!account) { |
| 52 | + continue; |
| 53 | + } |
| 54 | + const migrated = migrateLegacyFlatAllowPrivateNetworkAlias({ |
| 55 | + entry: account, |
| 56 | + pathPrefix: `channels.bluebubbles.accounts.${accountId}`, |
| 57 | + changes, |
| 58 | + }); |
| 59 | + if (!migrated.changed) { |
| 60 | + continue; |
| 61 | + } |
| 62 | + nextAccounts[accountId] = migrated.entry; |
| 63 | + accountsChanged = true; |
| 64 | + } |
| 65 | + if (accountsChanged) { |
| 66 | + updatedBluebubbles = { ...updatedBluebubbles, accounts: nextAccounts }; |
| 67 | + changed = true; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + if (!changed) { |
| 72 | + return { config: cfg, changes: [] }; |
| 73 | + } |
| 74 | + |
| 75 | + return { |
| 76 | + config: { |
| 77 | + ...cfg, |
| 78 | + channels: { |
| 79 | + ...cfg.channels, |
| 80 | + bluebubbles: updatedBluebubbles as NonNullable<OpenClawConfig["channels"]>["bluebubbles"], |
| 81 | + }, |
| 82 | + }, |
| 83 | + changes, |
| 84 | + }; |
| 85 | +} |
| 86 | + |
| 87 | +const BLUEBUBBLES_LEGACY_CONFIG_RULES: ChannelDoctorLegacyConfigRule[] = [ |
| 88 | + { |
| 89 | + path: ["channels", "bluebubbles"], |
| 90 | + message: |
| 91 | + "channels.bluebubbles.allowPrivateNetwork is legacy; use channels.bluebubbles.network.dangerouslyAllowPrivateNetwork instead (auto-migrated on load).", |
| 92 | + match: (value) => hasLegacyFlatAllowPrivateNetworkAlias(isRecord(value) ? value : {}), |
| 93 | + }, |
| 94 | + { |
| 95 | + path: ["channels", "bluebubbles", "accounts"], |
| 96 | + message: |
| 97 | + "channels.bluebubbles.accounts.<id>.allowPrivateNetwork is legacy; use channels.bluebubbles.accounts.<id>.network.dangerouslyAllowPrivateNetwork instead (auto-migrated on load).", |
| 98 | + match: hasLegacyAllowPrivateNetworkInAccounts, |
| 99 | + }, |
| 100 | +]; |
| 101 | + |
| 102 | +export const bluebubblesDoctor: ChannelDoctorAdapter = { |
| 103 | + legacyConfigRules: BLUEBUBBLES_LEGACY_CONFIG_RULES, |
| 104 | + normalizeCompatibilityConfig: ({ cfg }) => normalizeBlueBubblesCompatibilityConfig(cfg), |
| 105 | +}; |
0 commit comments