diff --git a/extensions/imessage/src/setup-core.ts b/extensions/imessage/src/setup-core.ts index 875d29e6f66..96aa0f08172 100644 --- a/extensions/imessage/src/setup-core.ts +++ b/extensions/imessage/src/setup-core.ts @@ -111,38 +111,44 @@ export const imessageDmPolicy = { channel, policyKey: "channels.imessage.dmPolicy", allowFromKey: "channels.imessage.allowFrom", - resolveConfigKeys: (_cfg: OpenClawConfig, accountId?: string) => - accountId && accountId !== resolveDefaultIMessageAccountId(_cfg) + resolveConfigKeys: (_cfg: OpenClawConfig, accountId?: string) => { + const targetAccountId = accountId ?? resolveDefaultIMessageAccountId(_cfg); + return targetAccountId !== "default" ? { - policyKey: `channels.imessage.accounts.${accountId}.dmPolicy`, - allowFromKey: `channels.imessage.accounts.${accountId}.allowFrom`, + policyKey: `channels.imessage.accounts.${targetAccountId}.dmPolicy`, + allowFromKey: `channels.imessage.accounts.${targetAccountId}.allowFrom`, } : { policyKey: "channels.imessage.dmPolicy", allowFromKey: "channels.imessage.allowFrom", - }, - getCurrent: (cfg: OpenClawConfig, accountId?: string) => - resolveIMessageAccount({ cfg, accountId }).config.dmPolicy ?? "pairing", + }; + }, + getCurrent: (cfg: OpenClawConfig, accountId?: string) => { + const targetAccountId = accountId ?? resolveDefaultIMessageAccountId(cfg); + return resolveIMessageAccount({ cfg, accountId: targetAccountId }).config.dmPolicy ?? "pairing"; + }, setPolicy: ( cfg: OpenClawConfig, policy: "pairing" | "allowlist" | "open" | "disabled", accountId?: string, - ) => - patchChannelConfigForAccount({ + ) => { + const targetAccountId = accountId ?? resolveDefaultIMessageAccountId(cfg); + return patchChannelConfigForAccount({ cfg, channel, - accountId: accountId ?? resolveDefaultIMessageAccountId(cfg), + accountId: targetAccountId, patch: policy === "open" ? { dmPolicy: "open", allowFrom: mergeAllowFromEntries( - resolveIMessageAccount({ cfg, accountId }).config.allowFrom, + resolveIMessageAccount({ cfg, accountId: targetAccountId }).config.allowFrom, ["*"], ), } : { dmPolicy: policy }, - }), + }); + }, promptAllowFrom: promptIMessageAllowFrom, }; @@ -186,17 +192,12 @@ export const imessageSetupStatusBase = { unconfiguredHint: "imsg missing", configuredScore: 1, unconfiguredScore: 0, - resolveConfigured: ({ - cfg, - accountId, - }: { - cfg: OpenClawConfig; - accountId?: string; - }) => + resolveConfigured: ({ cfg, accountId }: { cfg: OpenClawConfig; accountId?: string }) => accountId ? resolveIMessageAccount({ cfg, accountId }).configured - : listIMessageAccountIds(cfg).some((listedAccountId) => - resolveIMessageAccount({ cfg, accountId: listedAccountId }).configured, + : listIMessageAccountIds(cfg).some( + (listedAccountId) => + resolveIMessageAccount({ cfg, accountId: listedAccountId }).configured, ), }; diff --git a/extensions/imessage/src/targets.test.ts b/extensions/imessage/src/targets.test.ts index a909109e8b5..5eda105a54e 100644 --- a/extensions/imessage/src/targets.test.ts +++ b/extensions/imessage/src/targets.test.ts @@ -2,16 +2,16 @@ import * as processRuntime from "openclaw/plugin-sdk/process-runtime"; import * as setupRuntime from "openclaw/plugin-sdk/setup"; import { beforeEach, describe, expect, it, vi } from "vitest"; import { createPluginSetupWizardStatus } from "../../../test/helpers/plugins/setup-wizard.js"; -import * as clientModule from "./client.js"; import { imessagePlugin } from "./channel.js"; import * as channelRuntimeModule from "./channel.runtime.js"; +import * as clientModule from "./client.js"; import { resolveIMessageGroupRequireMention, resolveIMessageGroupToolPolicy, } from "./group-policy.js"; import { probeIMessage } from "./probe.js"; -import { parseIMessageAllowFromEntries } from "./setup-surface.js"; import { imessageDmPolicy } from "./setup-core.js"; +import { parseIMessageAllowFromEntries } from "./setup-surface.js"; import { formatIMessageChatTarget, inferIMessageTargetChatType, @@ -236,6 +236,37 @@ describe("parseIMessageAllowFromEntries", () => { expect(next.channels?.imessage?.accounts?.work?.dmPolicy).toBe("open"); expect(next.channels?.imessage?.accounts?.work?.allowFrom).toEqual(["+15555550123", "*"]); }); + + it("uses the configured default account for omitted-account DM policy reads, keys, and writes", () => { + const cfg = { + channels: { + imessage: { + allowFrom: ["+15555550123"], + defaultAccount: "work", + accounts: { + work: { + cliPath: "imsg", + dmPolicy: "allowlist" as const, + allowFrom: ["chat_id:123"], + }, + }, + }, + }, + }; + + expect(imessageDmPolicy.getCurrent(cfg)).toBe("allowlist"); + expect(imessageDmPolicy.resolveConfigKeys?.(cfg)).toEqual({ + policyKey: "channels.imessage.accounts.work.dmPolicy", + allowFromKey: "channels.imessage.accounts.work.allowFrom", + }); + + const next = imessageDmPolicy.setPolicy(cfg, "open"); + + expect(next.channels?.imessage?.dmPolicy).toBeUndefined(); + expect(next.channels?.imessage?.allowFrom).toEqual(["+15555550123"]); + expect(next.channels?.imessage?.accounts?.work?.dmPolicy).toBe("open"); + expect(next.channels?.imessage?.accounts?.work?.allowFrom).toEqual(["chat_id:123", "*"]); + }); }); describe("imessage setup status", () => {