diff --git a/extensions/bluebubbles/src/channel.ts b/extensions/bluebubbles/src/channel.ts index e00364cf115..741f93d3ae0 100644 --- a/extensions/bluebubbles/src/channel.ts +++ b/extensions/bluebubbles/src/channel.ts @@ -6,6 +6,7 @@ import type { import { applyAccountNameToChannelSection, buildChannelConfigSchema, + buildComputedAccountStatusSnapshot, buildProbeChannelStatusSummary, collectBlueBubblesStatusIssues, DEFAULT_ACCOUNT_ID, @@ -25,6 +26,7 @@ import { resolveDefaultBlueBubblesAccountId, } from "./accounts.js"; import { bluebubblesMessageActions } from "./actions.js"; +import { applyBlueBubblesConnectionConfig } from "./config-apply.js"; import { BlueBubblesConfigSchema } from "./config-schema.js"; import { sendBlueBubblesMedia } from "./media-send.js"; import { resolveBlueBubblesMessageId } from "./monitor.js"; @@ -255,40 +257,27 @@ export const bluebubblesPlugin: ChannelPlugin = { }) : namedConfig; if (accountId === DEFAULT_ACCOUNT_ID) { - return { - ...next, - channels: { - ...next.channels, - bluebubbles: { - ...next.channels?.bluebubbles, - enabled: true, - ...(input.httpUrl ? { serverUrl: input.httpUrl } : {}), - ...(input.password ? { password: input.password } : {}), - ...(input.webhookPath ? { webhookPath: input.webhookPath } : {}), - }, + return applyBlueBubblesConnectionConfig({ + cfg: next, + accountId, + patch: { + serverUrl: input.httpUrl, + password: input.password, + webhookPath: input.webhookPath, }, - } as OpenClawConfig; + onlyDefinedFields: true, + }); } - return { - ...next, - channels: { - ...next.channels, - bluebubbles: { - ...next.channels?.bluebubbles, - enabled: true, - accounts: { - ...next.channels?.bluebubbles?.accounts, - [accountId]: { - ...next.channels?.bluebubbles?.accounts?.[accountId], - enabled: true, - ...(input.httpUrl ? { serverUrl: input.httpUrl } : {}), - ...(input.password ? { password: input.password } : {}), - ...(input.webhookPath ? { webhookPath: input.webhookPath } : {}), - }, - }, - }, + return applyBlueBubblesConnectionConfig({ + cfg: next, + accountId, + patch: { + serverUrl: input.httpUrl, + password: input.password, + webhookPath: input.webhookPath, }, - } as OpenClawConfig; + onlyDefinedFields: true, + }); }, }, pairing: { @@ -372,20 +361,18 @@ export const bluebubblesPlugin: ChannelPlugin = { buildAccountSnapshot: ({ account, runtime, probe }) => { const running = runtime?.running ?? false; const probeOk = (probe as BlueBubblesProbe | undefined)?.ok; - return { + const base = buildComputedAccountStatusSnapshot({ accountId: account.accountId, name: account.name, enabled: account.enabled, configured: account.configured, - baseUrl: account.baseUrl, - running, - connected: probeOk ?? running, - lastStartAt: runtime?.lastStartAt ?? null, - lastStopAt: runtime?.lastStopAt ?? null, - lastError: runtime?.lastError ?? null, + runtime, probe, - lastInboundAt: runtime?.lastInboundAt ?? null, - lastOutboundAt: runtime?.lastOutboundAt ?? null, + }); + return { + ...base, + baseUrl: account.baseUrl, + connected: probeOk ?? running, }; }, }, diff --git a/extensions/bluebubbles/src/chat.ts b/extensions/bluebubbles/src/chat.ts index 5489077eaca..b63f09272f2 100644 --- a/extensions/bluebubbles/src/chat.ts +++ b/extensions/bluebubbles/src/chat.ts @@ -30,6 +30,39 @@ function resolvePartIndex(partIndex: number | undefined): number { return typeof partIndex === "number" ? partIndex : 0; } +async function sendBlueBubblesChatEndpointRequest(params: { + chatGuid: string; + opts: BlueBubblesChatOpts; + endpoint: "read" | "typing"; + method: "POST" | "DELETE"; + action: "read" | "typing"; +}): Promise { + const trimmed = params.chatGuid.trim(); + if (!trimmed) { + return; + } + const { baseUrl, password, accountId } = resolveAccount(params.opts); + if (getCachedBlueBubblesPrivateApiStatus(accountId) === false) { + return; + } + const url = buildBlueBubblesApiUrl({ + baseUrl, + path: `/api/v1/chat/${encodeURIComponent(trimmed)}/${params.endpoint}`, + password, + }); + const res = await blueBubblesFetchWithTimeout( + url, + { method: params.method }, + params.opts.timeoutMs, + ); + if (!res.ok) { + const errorText = await res.text().catch(() => ""); + throw new Error( + `BlueBubbles ${params.action} failed (${res.status}): ${errorText || "unknown"}`, + ); + } +} + async function sendPrivateApiJsonRequest(params: { opts: BlueBubblesChatOpts; feature: string; @@ -65,24 +98,13 @@ export async function markBlueBubblesChatRead( chatGuid: string, opts: BlueBubblesChatOpts = {}, ): Promise { - const trimmed = chatGuid.trim(); - if (!trimmed) { - return; - } - const { baseUrl, password, accountId } = resolveAccount(opts); - if (getCachedBlueBubblesPrivateApiStatus(accountId) === false) { - return; - } - const url = buildBlueBubblesApiUrl({ - baseUrl, - path: `/api/v1/chat/${encodeURIComponent(trimmed)}/read`, - password, + await sendBlueBubblesChatEndpointRequest({ + chatGuid, + opts, + endpoint: "read", + method: "POST", + action: "read", }); - const res = await blueBubblesFetchWithTimeout(url, { method: "POST" }, opts.timeoutMs); - if (!res.ok) { - const errorText = await res.text().catch(() => ""); - throw new Error(`BlueBubbles read failed (${res.status}): ${errorText || "unknown"}`); - } } export async function sendBlueBubblesTyping( @@ -90,28 +112,13 @@ export async function sendBlueBubblesTyping( typing: boolean, opts: BlueBubblesChatOpts = {}, ): Promise { - const trimmed = chatGuid.trim(); - if (!trimmed) { - return; - } - const { baseUrl, password, accountId } = resolveAccount(opts); - if (getCachedBlueBubblesPrivateApiStatus(accountId) === false) { - return; - } - const url = buildBlueBubblesApiUrl({ - baseUrl, - path: `/api/v1/chat/${encodeURIComponent(trimmed)}/typing`, - password, + await sendBlueBubblesChatEndpointRequest({ + chatGuid, + opts, + endpoint: "typing", + method: typing ? "POST" : "DELETE", + action: "typing", }); - const res = await blueBubblesFetchWithTimeout( - url, - { method: typing ? "POST" : "DELETE" }, - opts.timeoutMs, - ); - if (!res.ok) { - const errorText = await res.text().catch(() => ""); - throw new Error(`BlueBubbles typing failed (${res.status}): ${errorText || "unknown"}`); - } } /** diff --git a/extensions/bluebubbles/src/config-apply.ts b/extensions/bluebubbles/src/config-apply.ts new file mode 100644 index 00000000000..70b8c7cae37 --- /dev/null +++ b/extensions/bluebubbles/src/config-apply.ts @@ -0,0 +1,77 @@ +import { DEFAULT_ACCOUNT_ID, type OpenClawConfig } from "openclaw/plugin-sdk/bluebubbles"; + +type BlueBubblesConfigPatch = { + serverUrl?: string; + password?: unknown; + webhookPath?: string; +}; + +type AccountEnabledMode = boolean | "preserve-or-true"; + +function normalizePatch( + patch: BlueBubblesConfigPatch, + onlyDefinedFields: boolean, +): BlueBubblesConfigPatch { + if (!onlyDefinedFields) { + return patch; + } + const next: BlueBubblesConfigPatch = {}; + if (patch.serverUrl !== undefined) { + next.serverUrl = patch.serverUrl; + } + if (patch.password !== undefined) { + next.password = patch.password; + } + if (patch.webhookPath !== undefined) { + next.webhookPath = patch.webhookPath; + } + return next; +} + +export function applyBlueBubblesConnectionConfig(params: { + cfg: OpenClawConfig; + accountId: string; + patch: BlueBubblesConfigPatch; + onlyDefinedFields?: boolean; + accountEnabled?: AccountEnabledMode; +}): OpenClawConfig { + const patch = normalizePatch(params.patch, params.onlyDefinedFields === true); + if (params.accountId === DEFAULT_ACCOUNT_ID) { + return { + ...params.cfg, + channels: { + ...params.cfg.channels, + bluebubbles: { + ...params.cfg.channels?.bluebubbles, + enabled: true, + ...patch, + }, + }, + }; + } + + const currentAccount = params.cfg.channels?.bluebubbles?.accounts?.[params.accountId]; + const enabled = + params.accountEnabled === "preserve-or-true" + ? (currentAccount?.enabled ?? true) + : (params.accountEnabled ?? true); + + return { + ...params.cfg, + channels: { + ...params.cfg.channels, + bluebubbles: { + ...params.cfg.channels?.bluebubbles, + enabled: true, + accounts: { + ...params.cfg.channels?.bluebubbles?.accounts, + [params.accountId]: { + ...currentAccount, + enabled, + ...patch, + }, + }, + }, + }, + }; +} diff --git a/extensions/bluebubbles/src/monitor-normalize.ts b/extensions/bluebubbles/src/monitor-normalize.ts index e591f21dfb9..22705e6b12c 100644 --- a/extensions/bluebubbles/src/monitor-normalize.ts +++ b/extensions/bluebubbles/src/monitor-normalize.ts @@ -1,3 +1,4 @@ +import { parseFiniteNumber } from "../../../src/infra/parse-finite-number.js"; import { extractHandleFromChatGuid, normalizeBlueBubblesHandle } from "./targets.js"; import type { BlueBubblesAttachment } from "./types.js"; @@ -35,17 +36,7 @@ function readNumberLike(record: Record | null, key: string): nu if (!record) { return undefined; } - const value = record[key]; - if (typeof value === "number" && Number.isFinite(value)) { - return value; - } - if (typeof value === "string") { - const parsed = Number.parseFloat(value); - if (Number.isFinite(parsed)) { - return parsed; - } - } - return undefined; + return parseFiniteNumber(record[key]); } function extractAttachments(message: Record): BlueBubblesAttachment[] { diff --git a/extensions/bluebubbles/src/monitor.webhook-auth.test.ts b/extensions/bluebubbles/src/monitor.webhook-auth.test.ts index 9dd8e6f470b..201216c89ca 100644 --- a/extensions/bluebubbles/src/monitor.webhook-auth.test.ts +++ b/extensions/bluebubbles/src/monitor.webhook-auth.test.ts @@ -240,6 +240,15 @@ function getFirstDispatchCall(): DispatchReplyParams { } describe("BlueBubbles webhook monitor", () => { + const WEBHOOK_PATH = "/bluebubbles-webhook"; + const BASE_WEBHOOK_MESSAGE_DATA = { + text: "hello", + handle: { address: "+15551234567" }, + isGroup: false, + isFromMe: false, + guid: "msg-1", + } as const; + let unregister: () => void; beforeEach(() => { @@ -261,122 +270,144 @@ describe("BlueBubbles webhook monitor", () => { unregister?.(); }); + function createWebhookPayload( + dataOverrides: Record = {}, + ): Record { + return { + type: "new-message", + data: { + ...BASE_WEBHOOK_MESSAGE_DATA, + ...dataOverrides, + }, + }; + } + + function createWebhookTargetDeps(core?: PluginRuntime): { + config: OpenClawConfig; + core: PluginRuntime; + runtime: { + log: ReturnType void>>; + error: ReturnType void>>; + }; + } { + const resolvedCore = core ?? createMockRuntime(); + setBlueBubblesRuntime(resolvedCore); + return { + config: {}, + core: resolvedCore, + runtime: { + log: vi.fn<(message: string) => void>(), + error: vi.fn<(message: string) => void>(), + }, + }; + } + + function registerWebhookTarget( + params: { + account?: ResolvedBlueBubblesAccount; + config?: OpenClawConfig; + core?: PluginRuntime; + runtime?: { + log: ReturnType void>>; + error: ReturnType void>>; + }; + path?: string; + statusSink?: Parameters[0]["statusSink"]; + trackForCleanup?: boolean; + } = {}, + ): { + config: OpenClawConfig; + core: PluginRuntime; + runtime: { + log: ReturnType void>>; + error: ReturnType void>>; + }; + stop: () => void; + } { + const deps = + params.config && params.core && params.runtime + ? { config: params.config, core: params.core, runtime: params.runtime } + : createWebhookTargetDeps(params.core); + const stop = registerBlueBubblesWebhookTarget({ + account: params.account ?? createMockAccount(), + ...deps, + path: params.path ?? WEBHOOK_PATH, + statusSink: params.statusSink, + }); + if (params.trackForCleanup !== false) { + unregister = stop; + } + return { ...deps, stop }; + } + + async function sendWebhookRequest(params: { + method?: string; + url?: string; + body?: unknown; + headers?: Record; + remoteAddress?: string; + }): Promise<{ + req: IncomingMessage; + res: ServerResponse & { body: string; statusCode: number }; + handled: boolean; + }> { + const req = createMockRequest( + params.method ?? "POST", + params.url ?? WEBHOOK_PATH, + params.body ?? createWebhookPayload(), + params.headers, + ); + if (params.remoteAddress) { + (req as unknown as { socket: { remoteAddress: string } }).socket = { + remoteAddress: params.remoteAddress, + }; + } + const res = createMockResponse(); + const handled = await handleBlueBubblesWebhookRequest(req, res); + return { req, res, handled }; + } + describe("webhook parsing + auth handling", () => { it("rejects non-POST requests", async () => { - const account = createMockAccount(); - const config: OpenClawConfig = {}; - const core = createMockRuntime(); - setBlueBubblesRuntime(core); - - unregister = registerBlueBubblesWebhookTarget({ - account, - config, - runtime: { log: vi.fn(), error: vi.fn() }, - core, - path: "/bluebubbles-webhook", + registerWebhookTarget(); + const { handled, res } = await sendWebhookRequest({ + method: "GET", + body: {}, }); - const req = createMockRequest("GET", "/bluebubbles-webhook", {}); - const res = createMockResponse(); - - const handled = await handleBlueBubblesWebhookRequest(req, res); - expect(handled).toBe(true); expect(res.statusCode).toBe(405); }); it("accepts POST requests with valid JSON payload", async () => { - const account = createMockAccount(); - const config: OpenClawConfig = {}; - const core = createMockRuntime(); - setBlueBubblesRuntime(core); - - unregister = registerBlueBubblesWebhookTarget({ - account, - config, - runtime: { log: vi.fn(), error: vi.fn() }, - core, - path: "/bluebubbles-webhook", + registerWebhookTarget(); + const { handled, res } = await sendWebhookRequest({ + body: createWebhookPayload({ date: Date.now() }), }); - const payload = { - type: "new-message", - data: { - text: "hello", - handle: { address: "+15551234567" }, - isGroup: false, - isFromMe: false, - guid: "msg-1", - date: Date.now(), - }, - }; - - const req = createMockRequest("POST", "/bluebubbles-webhook", payload); - const res = createMockResponse(); - - const handled = await handleBlueBubblesWebhookRequest(req, res); - expect(handled).toBe(true); expect(res.statusCode).toBe(200); expect(res.body).toBe("ok"); }); it("rejects requests with invalid JSON", async () => { - const account = createMockAccount(); - const config: OpenClawConfig = {}; - const core = createMockRuntime(); - setBlueBubblesRuntime(core); - - unregister = registerBlueBubblesWebhookTarget({ - account, - config, - runtime: { log: vi.fn(), error: vi.fn() }, - core, - path: "/bluebubbles-webhook", + registerWebhookTarget(); + const { handled, res } = await sendWebhookRequest({ + body: "invalid json {{", }); - const req = createMockRequest("POST", "/bluebubbles-webhook", "invalid json {{"); - const res = createMockResponse(); - - const handled = await handleBlueBubblesWebhookRequest(req, res); - expect(handled).toBe(true); expect(res.statusCode).toBe(400); }); it("accepts URL-encoded payload wrappers", async () => { - const account = createMockAccount(); - const config: OpenClawConfig = {}; - const core = createMockRuntime(); - setBlueBubblesRuntime(core); - - unregister = registerBlueBubblesWebhookTarget({ - account, - config, - runtime: { log: vi.fn(), error: vi.fn() }, - core, - path: "/bluebubbles-webhook", - }); - - const payload = { - type: "new-message", - data: { - text: "hello", - handle: { address: "+15551234567" }, - isGroup: false, - isFromMe: false, - guid: "msg-1", - date: Date.now(), - }, - }; + registerWebhookTarget(); + const payload = createWebhookPayload({ date: Date.now() }); const encodedBody = new URLSearchParams({ payload: JSON.stringify(payload), }).toString(); - const req = createMockRequest("POST", "/bluebubbles-webhook", encodedBody); - const res = createMockResponse(); - - const handled = await handleBlueBubblesWebhookRequest(req, res); + const { handled, res } = await sendWebhookRequest({ body: encodedBody }); expect(handled).toBe(true); expect(res.statusCode).toBe(200); @@ -386,23 +417,12 @@ describe("BlueBubbles webhook monitor", () => { it("returns 408 when request body times out (Slow-Loris protection)", async () => { vi.useFakeTimers(); try { - const account = createMockAccount(); - const config: OpenClawConfig = {}; - const core = createMockRuntime(); - setBlueBubblesRuntime(core); - - unregister = registerBlueBubblesWebhookTarget({ - account, - config, - runtime: { log: vi.fn(), error: vi.fn() }, - core, - path: "/bluebubbles-webhook", - }); + registerWebhookTarget(); // Create a request that never sends data or ends (simulates slow-loris) const req = new EventEmitter() as IncomingMessage; req.method = "POST"; - req.url = "/bluebubbles-webhook?password=test-password"; + req.url = `${WEBHOOK_PATH}?password=test-password`; req.headers = {}; (req as unknown as { socket: { remoteAddress: string } }).socket = { remoteAddress: "127.0.0.1", @@ -426,22 +446,13 @@ describe("BlueBubbles webhook monitor", () => { }); it("rejects unauthorized requests before reading the body", async () => { - const account = createMockAccount({ password: "secret-token" }); - const config: OpenClawConfig = {}; - const core = createMockRuntime(); - setBlueBubblesRuntime(core); - - unregister = registerBlueBubblesWebhookTarget({ - account, - config, - runtime: { log: vi.fn(), error: vi.fn() }, - core, - path: "/bluebubbles-webhook", + registerWebhookTarget({ + account: createMockAccount({ password: "secret-token" }), }); const req = new EventEmitter() as IncomingMessage; req.method = "POST"; - req.url = "/bluebubbles-webhook?password=wrong-token"; + req.url = `${WEBHOOK_PATH}?password=wrong-token`; req.headers = {}; const onSpy = vi.spyOn(req, "on"); (req as unknown as { socket: { remoteAddress: string } }).socket = { @@ -457,112 +468,43 @@ describe("BlueBubbles webhook monitor", () => { }); it("authenticates via password query parameter", async () => { - const account = createMockAccount({ password: "secret-token" }); - const config: OpenClawConfig = {}; - const core = createMockRuntime(); - setBlueBubblesRuntime(core); - - // Mock non-localhost request - const req = createMockRequest("POST", "/bluebubbles-webhook?password=secret-token", { - type: "new-message", - data: { - text: "hello", - handle: { address: "+15551234567" }, - isGroup: false, - isFromMe: false, - guid: "msg-1", - }, + registerWebhookTarget({ + account: createMockAccount({ password: "secret-token" }), }); - (req as unknown as { socket: { remoteAddress: string } }).socket = { + const { handled, res } = await sendWebhookRequest({ + url: `${WEBHOOK_PATH}?password=secret-token`, + body: createWebhookPayload(), remoteAddress: "192.168.1.100", - }; - - unregister = registerBlueBubblesWebhookTarget({ - account, - config, - runtime: { log: vi.fn(), error: vi.fn() }, - core, - path: "/bluebubbles-webhook", }); - const res = createMockResponse(); - const handled = await handleBlueBubblesWebhookRequest(req, res); - expect(handled).toBe(true); expect(res.statusCode).toBe(200); }); it("authenticates via x-password header", async () => { - const account = createMockAccount({ password: "secret-token" }); - const config: OpenClawConfig = {}; - const core = createMockRuntime(); - setBlueBubblesRuntime(core); - - const req = createMockRequest( - "POST", - "/bluebubbles-webhook", - { - type: "new-message", - data: { - text: "hello", - handle: { address: "+15551234567" }, - isGroup: false, - isFromMe: false, - guid: "msg-1", - }, - }, - { "x-password": "secret-token" }, - ); - (req as unknown as { socket: { remoteAddress: string } }).socket = { - remoteAddress: "192.168.1.100", - }; - - unregister = registerBlueBubblesWebhookTarget({ - account, - config, - runtime: { log: vi.fn(), error: vi.fn() }, - core, - path: "/bluebubbles-webhook", + registerWebhookTarget({ + account: createMockAccount({ password: "secret-token" }), + }); + const { handled, res } = await sendWebhookRequest({ + body: createWebhookPayload(), + headers: { "x-password": "secret-token" }, + remoteAddress: "192.168.1.100", }); - - const res = createMockResponse(); - const handled = await handleBlueBubblesWebhookRequest(req, res); expect(handled).toBe(true); expect(res.statusCode).toBe(200); }); it("rejects unauthorized requests with wrong password", async () => { - const account = createMockAccount({ password: "secret-token" }); - const config: OpenClawConfig = {}; - const core = createMockRuntime(); - setBlueBubblesRuntime(core); - - const req = createMockRequest("POST", "/bluebubbles-webhook?password=wrong-token", { - type: "new-message", - data: { - text: "hello", - handle: { address: "+15551234567" }, - isGroup: false, - isFromMe: false, - guid: "msg-1", - }, + registerWebhookTarget({ + account: createMockAccount({ password: "secret-token" }), }); - (req as unknown as { socket: { remoteAddress: string } }).socket = { + const { handled, res } = await sendWebhookRequest({ + url: `${WEBHOOK_PATH}?password=wrong-token`, + body: createWebhookPayload(), remoteAddress: "192.168.1.100", - }; - - unregister = registerBlueBubblesWebhookTarget({ - account, - config, - runtime: { log: vi.fn(), error: vi.fn() }, - core, - path: "/bluebubbles-webhook", }); - const res = createMockResponse(); - const handled = await handleBlueBubblesWebhookRequest(req, res); - expect(handled).toBe(true); expect(res.statusCode).toBe(401); }); @@ -570,50 +512,37 @@ describe("BlueBubbles webhook monitor", () => { it("rejects ambiguous routing when multiple targets match the same password", async () => { const accountA = createMockAccount({ password: "secret-token" }); const accountB = createMockAccount({ password: "secret-token" }); - const config: OpenClawConfig = {}; - const core = createMockRuntime(); - setBlueBubblesRuntime(core); + const { config, core, runtime } = createWebhookTargetDeps(); const sinkA = vi.fn(); const sinkB = vi.fn(); - const req = createMockRequest("POST", "/bluebubbles-webhook?password=secret-token", { - type: "new-message", - data: { - text: "hello", - handle: { address: "+15551234567" }, - isGroup: false, - isFromMe: false, - guid: "msg-1", - }, - }); - (req as unknown as { socket: { remoteAddress: string } }).socket = { - remoteAddress: "192.168.1.100", - }; - - const unregisterA = registerBlueBubblesWebhookTarget({ + const unregisterA = registerWebhookTarget({ account: accountA, config, - runtime: { log: vi.fn(), error: vi.fn() }, + runtime, core, - path: "/bluebubbles-webhook", + trackForCleanup: false, statusSink: sinkA, - }); - const unregisterB = registerBlueBubblesWebhookTarget({ + }).stop; + const unregisterB = registerWebhookTarget({ account: accountB, config, - runtime: { log: vi.fn(), error: vi.fn() }, + runtime, core, - path: "/bluebubbles-webhook", + trackForCleanup: false, statusSink: sinkB, - }); + }).stop; unregister = () => { unregisterA(); unregisterB(); }; - const res = createMockResponse(); - const handled = await handleBlueBubblesWebhookRequest(req, res); + const { handled, res } = await sendWebhookRequest({ + url: `${WEBHOOK_PATH}?password=secret-token`, + body: createWebhookPayload(), + remoteAddress: "192.168.1.100", + }); expect(handled).toBe(true); expect(res.statusCode).toBe(401); @@ -624,50 +553,37 @@ describe("BlueBubbles webhook monitor", () => { it("ignores targets without passwords when a password-authenticated target matches", async () => { const accountStrict = createMockAccount({ password: "secret-token" }); const accountWithoutPassword = createMockAccount({ password: undefined }); - const config: OpenClawConfig = {}; - const core = createMockRuntime(); - setBlueBubblesRuntime(core); + const { config, core, runtime } = createWebhookTargetDeps(); const sinkStrict = vi.fn(); const sinkWithoutPassword = vi.fn(); - const req = createMockRequest("POST", "/bluebubbles-webhook?password=secret-token", { - type: "new-message", - data: { - text: "hello", - handle: { address: "+15551234567" }, - isGroup: false, - isFromMe: false, - guid: "msg-1", - }, - }); - (req as unknown as { socket: { remoteAddress: string } }).socket = { - remoteAddress: "192.168.1.100", - }; - - const unregisterStrict = registerBlueBubblesWebhookTarget({ + const unregisterStrict = registerWebhookTarget({ account: accountStrict, config, - runtime: { log: vi.fn(), error: vi.fn() }, + runtime, core, - path: "/bluebubbles-webhook", + trackForCleanup: false, statusSink: sinkStrict, - }); - const unregisterNoPassword = registerBlueBubblesWebhookTarget({ + }).stop; + const unregisterNoPassword = registerWebhookTarget({ account: accountWithoutPassword, config, - runtime: { log: vi.fn(), error: vi.fn() }, + runtime, core, - path: "/bluebubbles-webhook", + trackForCleanup: false, statusSink: sinkWithoutPassword, - }); + }).stop; unregister = () => { unregisterStrict(); unregisterNoPassword(); }; - const res = createMockResponse(); - const handled = await handleBlueBubblesWebhookRequest(req, res); + const { handled, res } = await sendWebhookRequest({ + url: `${WEBHOOK_PATH}?password=secret-token`, + body: createWebhookPayload(), + remoteAddress: "192.168.1.100", + }); expect(handled).toBe(true); expect(res.statusCode).toBe(200); @@ -677,34 +593,20 @@ describe("BlueBubbles webhook monitor", () => { it("requires authentication for loopback requests when password is configured", async () => { const account = createMockAccount({ password: "secret-token" }); - const config: OpenClawConfig = {}; - const core = createMockRuntime(); - setBlueBubblesRuntime(core); + const { config, core, runtime } = createWebhookTargetDeps(); for (const remoteAddress of ["127.0.0.1", "::1", "::ffff:127.0.0.1"]) { - const req = createMockRequest("POST", "/bluebubbles-webhook", { - type: "new-message", - data: { - text: "hello", - handle: { address: "+15551234567" }, - isGroup: false, - isFromMe: false, - guid: "msg-1", - }, - }); - (req as unknown as { socket: { remoteAddress: string } }).socket = { - remoteAddress, - }; - - const loopbackUnregister = registerBlueBubblesWebhookTarget({ + const loopbackUnregister = registerWebhookTarget({ account, config, - runtime: { log: vi.fn(), error: vi.fn() }, + runtime, core, - path: "/bluebubbles-webhook", - }); + trackForCleanup: false, + }).stop; - const res = createMockResponse(); - const handled = await handleBlueBubblesWebhookRequest(req, res); + const { handled, res } = await sendWebhookRequest({ + body: createWebhookPayload(), + remoteAddress, + }); expect(handled).toBe(true); expect(res.statusCode).toBe(401); @@ -713,17 +615,8 @@ describe("BlueBubbles webhook monitor", () => { }); it("rejects targets without passwords for loopback and proxied-looking requests", async () => { - const account = createMockAccount({ password: undefined }); - const config: OpenClawConfig = {}; - const core = createMockRuntime(); - setBlueBubblesRuntime(core); - - unregister = registerBlueBubblesWebhookTarget({ - account, - config, - runtime: { log: vi.fn(), error: vi.fn() }, - core, - path: "/bluebubbles-webhook", + registerWebhookTarget({ + account: createMockAccount({ password: undefined }), }); const headerVariants: Record[] = [ @@ -732,26 +625,11 @@ describe("BlueBubbles webhook monitor", () => { { host: "localhost", forwarded: "for=203.0.113.10;proto=https;host=example.com" }, ]; for (const headers of headerVariants) { - const req = createMockRequest( - "POST", - "/bluebubbles-webhook", - { - type: "new-message", - data: { - text: "hello", - handle: { address: "+15551234567" }, - isGroup: false, - isFromMe: false, - guid: "msg-1", - }, - }, + const { handled, res } = await sendWebhookRequest({ + body: createWebhookPayload(), headers, - ); - (req as unknown as { socket: { remoteAddress: string } }).socket = { remoteAddress: "127.0.0.1", - }; - const res = createMockResponse(); - const handled = await handleBlueBubblesWebhookRequest(req, res); + }); expect(handled).toBe(true); expect(res.statusCode).toBe(401); } @@ -770,36 +648,18 @@ describe("BlueBubbles webhook monitor", () => { const { resolveChatGuidForTarget } = await import("./send.js"); vi.mocked(resolveChatGuidForTarget).mockClear(); - const account = createMockAccount({ groupPolicy: "open" }); - const config: OpenClawConfig = {}; - const core = createMockRuntime(); - setBlueBubblesRuntime(core); - - unregister = registerBlueBubblesWebhookTarget({ - account, - config, - runtime: { log: vi.fn(), error: vi.fn() }, - core, - path: "/bluebubbles-webhook", + registerWebhookTarget({ + account: createMockAccount({ groupPolicy: "open" }), }); - const payload = { - type: "new-message", - data: { + await sendWebhookRequest({ + body: createWebhookPayload({ text: "hello from group", - handle: { address: "+15551234567" }, isGroup: true, - isFromMe: false, - guid: "msg-1", chatId: "123", date: Date.now(), - }, - }; - - const req = createMockRequest("POST", "/bluebubbles-webhook", payload); - const res = createMockResponse(); - - await handleBlueBubblesWebhookRequest(req, res); + }), + }); await flushAsync(); expect(resolveChatGuidForTarget).toHaveBeenCalledWith( @@ -819,36 +679,18 @@ describe("BlueBubbles webhook monitor", () => { return EMPTY_DISPATCH_RESULT; }); - const account = createMockAccount({ groupPolicy: "open" }); - const config: OpenClawConfig = {}; - const core = createMockRuntime(); - setBlueBubblesRuntime(core); - - unregister = registerBlueBubblesWebhookTarget({ - account, - config, - runtime: { log: vi.fn(), error: vi.fn() }, - core, - path: "/bluebubbles-webhook", + registerWebhookTarget({ + account: createMockAccount({ groupPolicy: "open" }), }); - const payload = { - type: "new-message", - data: { + await sendWebhookRequest({ + body: createWebhookPayload({ text: "hello from group", - handle: { address: "+15551234567" }, isGroup: true, - isFromMe: false, - guid: "msg-1", chat: { chatGuid: "iMessage;+;chat123456" }, date: Date.now(), - }, - }; - - const req = createMockRequest("POST", "/bluebubbles-webhook", payload); - const res = createMockResponse(); - - await handleBlueBubblesWebhookRequest(req, res); + }), + }); await flushAsync(); expect(resolveChatGuidForTarget).not.toHaveBeenCalled(); diff --git a/extensions/bluebubbles/src/onboarding.ts b/extensions/bluebubbles/src/onboarding.ts index 8936d3d5c52..bd6bb0913b8 100644 --- a/extensions/bluebubbles/src/onboarding.ts +++ b/extensions/bluebubbles/src/onboarding.ts @@ -18,6 +18,7 @@ import { resolveBlueBubblesAccount, resolveDefaultBlueBubblesAccountId, } from "./accounts.js"; +import { applyBlueBubblesConnectionConfig } from "./config-apply.js"; import { hasConfiguredSecretInput, normalizeSecretInputString } from "./secret-input.js"; import { parseBlueBubblesAllowTarget } from "./targets.js"; import { normalizeBlueBubblesServerUrl } from "./types.js"; @@ -283,42 +284,16 @@ export const blueBubblesOnboardingAdapter: ChannelOnboardingAdapter = { } // Apply config - if (accountId === DEFAULT_ACCOUNT_ID) { - next = { - ...next, - channels: { - ...next.channels, - bluebubbles: { - ...next.channels?.bluebubbles, - enabled: true, - serverUrl, - password, - webhookPath, - }, - }, - }; - } else { - next = { - ...next, - channels: { - ...next.channels, - bluebubbles: { - ...next.channels?.bluebubbles, - enabled: true, - accounts: { - ...next.channels?.bluebubbles?.accounts, - [accountId]: { - ...next.channels?.bluebubbles?.accounts?.[accountId], - enabled: next.channels?.bluebubbles?.accounts?.[accountId]?.enabled ?? true, - serverUrl, - password, - webhookPath, - }, - }, - }, - }, - }; - } + next = applyBlueBubblesConnectionConfig({ + cfg: next, + accountId, + patch: { + serverUrl, + password, + webhookPath, + }, + accountEnabled: "preserve-or-true", + }); await prompter.note( [ diff --git a/extensions/bluebubbles/src/request-url.ts b/extensions/bluebubbles/src/request-url.ts index 0be775359d5..cd1527f186f 100644 --- a/extensions/bluebubbles/src/request-url.ts +++ b/extensions/bluebubbles/src/request-url.ts @@ -1,12 +1 @@ -export function resolveRequestUrl(input: RequestInfo | URL): string { - if (typeof input === "string") { - return input; - } - if (input instanceof URL) { - return input.toString(); - } - if (typeof input === "object" && input && "url" in input && typeof input.url === "string") { - return input.url; - } - return String(input); -} +export { resolveRequestUrl } from "openclaw/plugin-sdk/bluebubbles"; diff --git a/extensions/bluebubbles/src/send.ts b/extensions/bluebubbles/src/send.ts index a32fd92d470..8c12e88bd23 100644 --- a/extensions/bluebubbles/src/send.ts +++ b/extensions/bluebubbles/src/send.ts @@ -108,6 +108,19 @@ function resolvePrivateApiDecision(params: { }; } +async function parseBlueBubblesMessageResponse(res: Response): Promise { + const body = await res.text(); + if (!body) { + return { messageId: "ok" }; + } + try { + const parsed = JSON.parse(body) as unknown; + return { messageId: extractBlueBubblesMessageId(parsed) }; + } catch { + return { messageId: "ok" }; + } +} + type BlueBubblesChatRecord = Record; function extractChatGuid(chat: BlueBubblesChatRecord): string | null { @@ -342,16 +355,7 @@ async function createNewChatWithMessage(params: { } throw new Error(`BlueBubbles create chat failed (${res.status}): ${errorText || "unknown"}`); } - const body = await res.text(); - if (!body) { - return { messageId: "ok" }; - } - try { - const parsed = JSON.parse(body) as unknown; - return { messageId: extractBlueBubblesMessageId(parsed) }; - } catch { - return { messageId: "ok" }; - } + return parseBlueBubblesMessageResponse(res); } export async function sendMessageBlueBubbles( @@ -464,14 +468,5 @@ export async function sendMessageBlueBubbles( const errorText = await res.text(); throw new Error(`BlueBubbles send failed (${res.status}): ${errorText || "unknown"}`); } - const body = await res.text(); - if (!body) { - return { messageId: "ok" }; - } - try { - const parsed = JSON.parse(body) as unknown; - return { messageId: extractBlueBubblesMessageId(parsed) }; - } catch { - return { messageId: "ok" }; - } + return parseBlueBubblesMessageResponse(res); } diff --git a/extensions/diffs/src/config.test.ts b/extensions/diffs/src/config.test.ts index a2795546fdb..b7845326483 100644 --- a/extensions/diffs/src/config.test.ts +++ b/extensions/diffs/src/config.test.ts @@ -7,6 +7,23 @@ import { resolveDiffsPluginSecurity, } from "./config.js"; +const FULL_DEFAULTS = { + fontFamily: "JetBrains Mono", + fontSize: 17, + lineSpacing: 1.8, + layout: "split", + showLineNumbers: false, + diffIndicators: "classic", + wordWrap: false, + background: false, + theme: "light", + fileFormat: "pdf", + fileQuality: "hq", + fileScale: 2.6, + fileMaxWidth: 1280, + mode: "file", +} as const; + describe("resolveDiffsPluginDefaults", () => { it("returns built-in defaults when config is missing", () => { expect(resolveDiffsPluginDefaults(undefined)).toEqual(DEFAULT_DIFFS_TOOL_DEFAULTS); @@ -15,39 +32,9 @@ describe("resolveDiffsPluginDefaults", () => { it("applies configured defaults from plugin config", () => { expect( resolveDiffsPluginDefaults({ - defaults: { - fontFamily: "JetBrains Mono", - fontSize: 17, - lineSpacing: 1.8, - layout: "split", - showLineNumbers: false, - diffIndicators: "classic", - wordWrap: false, - background: false, - theme: "light", - fileFormat: "pdf", - fileQuality: "hq", - fileScale: 2.6, - fileMaxWidth: 1280, - mode: "file", - }, + defaults: FULL_DEFAULTS, }), - ).toEqual({ - fontFamily: "JetBrains Mono", - fontSize: 17, - lineSpacing: 1.8, - layout: "split", - showLineNumbers: false, - diffIndicators: "classic", - wordWrap: false, - background: false, - theme: "light", - fileFormat: "pdf", - fileQuality: "hq", - fileScale: 2.6, - fileMaxWidth: 1280, - mode: "file", - }); + ).toEqual(FULL_DEFAULTS); }); it("clamps and falls back for invalid line spacing and indicators", () => { diff --git a/extensions/diffs/src/tool.test.ts b/extensions/diffs/src/tool.test.ts index ba72c011c76..97ee6234148 100644 --- a/extensions/diffs/src/tool.test.ts +++ b/extensions/diffs/src/tool.test.ts @@ -95,23 +95,11 @@ describe("diffs tool", () => { }); it("renders PDF output when fileFormat is pdf", async () => { - const screenshotter = { - screenshotHtml: vi.fn( - async ({ - outputPath, - image, - }: { - outputPath: string; - image: { format: string; qualityPreset: string; scale: number; maxWidth: number }; - }) => { - expect(image.format).toBe("pdf"); - expect(outputPath).toMatch(/preview\.pdf$/); - await fs.mkdir(path.dirname(outputPath), { recursive: true }); - await fs.writeFile(outputPath, Buffer.from("%PDF-1.7")); - return outputPath; - }, - ), - }; + const screenshotter = createPdfScreenshotter({ + assertOutputPath: (outputPath) => { + expect(outputPath).toMatch(/preview\.pdf$/); + }, + }); const tool = createDiffsTool({ api: createApi(), @@ -208,22 +196,7 @@ describe("diffs tool", () => { }); it("accepts deprecated format alias for fileFormat", async () => { - const screenshotter = { - screenshotHtml: vi.fn( - async ({ - outputPath, - image, - }: { - outputPath: string; - image: { format: string; qualityPreset: string; scale: number; maxWidth: number }; - }) => { - expect(image.format).toBe("pdf"); - await fs.mkdir(path.dirname(outputPath), { recursive: true }); - await fs.writeFile(outputPath, Buffer.from("%PDF-1.7")); - return outputPath; - }, - ), - }; + const screenshotter = createPdfScreenshotter(); const tool = createDiffsTool({ api: createApi(), @@ -492,6 +465,23 @@ function createPngScreenshotter( }; } +function createPdfScreenshotter( + params: { + assertOutputPath?: (outputPath: string) => void; + } = {}, +): DiffScreenshotter { + const screenshotHtml: DiffScreenshotter["screenshotHtml"] = vi.fn( + async ({ outputPath, image }: { outputPath: string; image: DiffRenderOptions["image"] }) => { + expect(image.format).toBe("pdf"); + params.assertOutputPath?.(outputPath); + await fs.mkdir(path.dirname(outputPath), { recursive: true }); + await fs.writeFile(outputPath, Buffer.from("%PDF-1.7")); + return outputPath; + }, + ); + return { screenshotHtml }; +} + function readTextContent(result: unknown, index: number): string { const content = (result as { content?: Array<{ type?: string; text?: string }> } | undefined) ?.content; diff --git a/extensions/feishu/src/channel.ts b/extensions/feishu/src/channel.ts index 1e631c407e0..a8fa04d5700 100644 --- a/extensions/feishu/src/channel.ts +++ b/extensions/feishu/src/channel.ts @@ -1,6 +1,7 @@ import type { ChannelMeta, ChannelPlugin, ClawdbotConfig } from "openclaw/plugin-sdk/feishu"; import { - buildBaseChannelStatusSummary, + buildProbeChannelStatusSummary, + buildRuntimeAccountStatusSnapshot, createDefaultChannelRuntimeState, DEFAULT_ACCOUNT_ID, PAIRING_APPROVED_MESSAGE, @@ -54,6 +55,30 @@ const secretInputJsonSchema = { ], } as const; +function setFeishuNamedAccountEnabled( + cfg: ClawdbotConfig, + accountId: string, + enabled: boolean, +): ClawdbotConfig { + const feishuCfg = cfg.channels?.feishu as FeishuConfig | undefined; + return { + ...cfg, + channels: { + ...cfg.channels, + feishu: { + ...feishuCfg, + accounts: { + ...feishuCfg?.accounts, + [accountId]: { + ...feishuCfg?.accounts?.[accountId], + enabled, + }, + }, + }, + }, + }; +} + export const feishuPlugin: ChannelPlugin = { id: "feishu", meta: { @@ -178,23 +203,7 @@ export const feishuPlugin: ChannelPlugin = { } // For named accounts, set enabled in accounts[accountId] - const feishuCfg = cfg.channels?.feishu as FeishuConfig | undefined; - return { - ...cfg, - channels: { - ...cfg.channels, - feishu: { - ...feishuCfg, - accounts: { - ...feishuCfg?.accounts, - [accountId]: { - ...feishuCfg?.accounts?.[accountId], - enabled, - }, - }, - }, - }, - }; + return setFeishuNamedAccountEnabled(cfg, accountId, enabled); }, deleteAccount: ({ cfg, accountId }) => { const isDefault = accountId === DEFAULT_ACCOUNT_ID; @@ -281,23 +290,7 @@ export const feishuPlugin: ChannelPlugin = { }; } - const feishuCfg = cfg.channels?.feishu as FeishuConfig | undefined; - return { - ...cfg, - channels: { - ...cfg.channels, - feishu: { - ...feishuCfg, - accounts: { - ...feishuCfg?.accounts, - [accountId]: { - ...feishuCfg?.accounts?.[accountId], - enabled: true, - }, - }, - }, - }, - }; + return setFeishuNamedAccountEnabled(cfg, accountId, true); }, }, onboarding: feishuOnboardingAdapter, @@ -342,12 +335,10 @@ export const feishuPlugin: ChannelPlugin = { outbound: feishuOutbound, status: { defaultRuntime: createDefaultChannelRuntimeState(DEFAULT_ACCOUNT_ID, { port: null }), - buildChannelSummary: ({ snapshot }) => ({ - ...buildBaseChannelStatusSummary(snapshot), - port: snapshot.port ?? null, - probe: snapshot.probe, - lastProbeAt: snapshot.lastProbeAt ?? null, - }), + buildChannelSummary: ({ snapshot }) => + buildProbeChannelStatusSummary(snapshot, { + port: snapshot.port ?? null, + }), probeAccount: async ({ account }) => await probeFeishu(account), buildAccountSnapshot: ({ account, runtime, probe }) => ({ accountId: account.accountId, @@ -356,12 +347,8 @@ export const feishuPlugin: ChannelPlugin = { name: account.name, appId: account.appId, domain: account.domain, - running: runtime?.running ?? false, - lastStartAt: runtime?.lastStartAt ?? null, - lastStopAt: runtime?.lastStopAt ?? null, - lastError: runtime?.lastError ?? null, + ...buildRuntimeAccountStatusSnapshot({ runtime, probe }), port: runtime?.port ?? null, - probe, }), }, gateway: { diff --git a/extensions/feishu/src/drive.ts b/extensions/feishu/src/drive.ts index f9eacc9287d..227c30fbbb7 100644 --- a/extensions/feishu/src/drive.ts +++ b/extensions/feishu/src/drive.ts @@ -3,15 +3,11 @@ import type { OpenClawPluginApi } from "openclaw/plugin-sdk/feishu"; import { listEnabledFeishuAccounts } from "./accounts.js"; import { FeishuDriveSchema, type FeishuDriveParams } from "./drive-schema.js"; import { createFeishuToolClient, resolveAnyEnabledFeishuToolsConfig } from "./tool-account.js"; - -// ============ Helpers ============ - -function json(data: unknown) { - return { - content: [{ type: "text" as const, text: JSON.stringify(data, null, 2) }], - details: data, - }; -} +import { + jsonToolResult, + toolExecutionErrorResult, + unknownToolActionResult, +} from "./tool-result.js"; // ============ Actions ============ @@ -206,21 +202,21 @@ export function registerFeishuDriveTools(api: OpenClawPluginApi) { }); switch (p.action) { case "list": - return json(await listFolder(client, p.folder_token)); + return jsonToolResult(await listFolder(client, p.folder_token)); case "info": - return json(await getFileInfo(client, p.file_token)); + return jsonToolResult(await getFileInfo(client, p.file_token)); case "create_folder": - return json(await createFolder(client, p.name, p.folder_token)); + return jsonToolResult(await createFolder(client, p.name, p.folder_token)); case "move": - return json(await moveFile(client, p.file_token, p.type, p.folder_token)); + return jsonToolResult(await moveFile(client, p.file_token, p.type, p.folder_token)); case "delete": - return json(await deleteFile(client, p.file_token, p.type)); + return jsonToolResult(await deleteFile(client, p.file_token, p.type)); default: // eslint-disable-next-line @typescript-eslint/no-explicit-any -- exhaustive check fallback - return json({ error: `Unknown action: ${(p as any).action}` }); + return unknownToolActionResult((p as { action?: unknown }).action); } } catch (err) { - return json({ error: err instanceof Error ? err.message : String(err) }); + return toolExecutionErrorResult(err); } }, }; diff --git a/extensions/feishu/src/monitor.reaction.test.ts b/extensions/feishu/src/monitor.reaction.test.ts index 06eb0e37a97..5537af6b214 100644 --- a/extensions/feishu/src/monitor.reaction.test.ts +++ b/extensions/feishu/src/monitor.reaction.test.ts @@ -51,6 +51,30 @@ function makeReactionEvent( }; } +function createFetchedReactionMessage(chatId: string) { + return { + messageId: "om_msg1", + chatId, + senderOpenId: "ou_bot", + content: "hello", + contentType: "text", + }; +} + +async function resolveReactionWithLookup(params: { + event?: FeishuReactionCreatedEvent; + lookupChatId: string; +}) { + return await resolveReactionSyntheticEvent({ + cfg, + accountId: "default", + event: params.event ?? makeReactionEvent(), + botOpenId: "ou_bot", + fetchMessage: async () => createFetchedReactionMessage(params.lookupChatId), + uuid: () => "fixed-uuid", + }); +} + type FeishuMention = NonNullable[number]; function buildDebounceConfig(): ClawdbotConfig { @@ -152,6 +176,30 @@ function getFirstDispatchedEvent(): FeishuMessageEvent { return firstParams.event; } +function setDedupPassThroughMocks(): void { + vi.spyOn(dedup, "tryRecordMessage").mockReturnValue(true); + vi.spyOn(dedup, "tryRecordMessagePersistent").mockResolvedValue(true); + vi.spyOn(dedup, "hasRecordedMessage").mockReturnValue(false); + vi.spyOn(dedup, "hasRecordedMessagePersistent").mockResolvedValue(false); +} + +function createMention(params: { openId: string; name: string; key?: string }): FeishuMention { + return { + key: params.key ?? "@_user_1", + id: { open_id: params.openId }, + name: params.name, + }; +} + +async function enqueueDebouncedMessage( + onMessage: (data: unknown) => Promise, + event: FeishuMessageEvent, +): Promise { + await onMessage(event); + await Promise.resolve(); + await Promise.resolve(); +} + describe("resolveReactionSyntheticEvent", () => { it("filters app self-reactions", async () => { const event = makeReactionEvent({ operator_type: "app" }); @@ -272,23 +320,12 @@ describe("resolveReactionSyntheticEvent", () => { }); it("uses event chat context when provided", async () => { - const event = makeReactionEvent({ - chat_id: "oc_group_from_event", - chat_type: "group", - }); - const result = await resolveReactionSyntheticEvent({ - cfg, - accountId: "default", - event, - botOpenId: "ou_bot", - fetchMessage: async () => ({ - messageId: "om_msg1", - chatId: "oc_group_from_lookup", - senderOpenId: "ou_bot", - content: "hello", - contentType: "text", + const result = await resolveReactionWithLookup({ + event: makeReactionEvent({ + chat_id: "oc_group_from_event", + chat_type: "group", }), - uuid: () => "fixed-uuid", + lookupChatId: "oc_group_from_lookup", }); expect(result).toEqual({ @@ -309,20 +346,8 @@ describe("resolveReactionSyntheticEvent", () => { }); it("falls back to reacted message chat_id when event chat_id is absent", async () => { - const event = makeReactionEvent(); - const result = await resolveReactionSyntheticEvent({ - cfg, - accountId: "default", - event, - botOpenId: "ou_bot", - fetchMessage: async () => ({ - messageId: "om_msg1", - chatId: "oc_group_from_lookup", - senderOpenId: "ou_bot", - content: "hello", - contentType: "text", - }), - uuid: () => "fixed-uuid", + const result = await resolveReactionWithLookup({ + lookupChatId: "oc_group_from_lookup", }); expect(result?.message.chat_id).toBe("oc_group_from_lookup"); @@ -330,20 +355,8 @@ describe("resolveReactionSyntheticEvent", () => { }); it("falls back to sender p2p chat when lookup returns empty chat_id", async () => { - const event = makeReactionEvent(); - const result = await resolveReactionSyntheticEvent({ - cfg, - accountId: "default", - event, - botOpenId: "ou_bot", - fetchMessage: async () => ({ - messageId: "om_msg1", - chatId: "", - senderOpenId: "ou_bot", - content: "hello", - contentType: "text", - }), - uuid: () => "fixed-uuid", + const result = await resolveReactionWithLookup({ + lookupChatId: "", }); expect(result?.message.chat_id).toBe("p2p:ou_user1"); @@ -396,42 +409,25 @@ describe("Feishu inbound debounce regressions", () => { }); it("keeps bot mention when per-message mention keys collide across non-forward messages", async () => { - vi.spyOn(dedup, "tryRecordMessage").mockReturnValue(true); - vi.spyOn(dedup, "tryRecordMessagePersistent").mockResolvedValue(true); - vi.spyOn(dedup, "hasRecordedMessage").mockReturnValue(false); - vi.spyOn(dedup, "hasRecordedMessagePersistent").mockResolvedValue(false); + setDedupPassThroughMocks(); const onMessage = await setupDebounceMonitor(); - await onMessage( + await enqueueDebouncedMessage( + onMessage, createTextEvent({ messageId: "om_1", text: "first", - mentions: [ - { - key: "@_user_1", - id: { open_id: "ou_user_a" }, - name: "user-a", - }, - ], + mentions: [createMention({ openId: "ou_user_a", name: "user-a" })], }), ); - await Promise.resolve(); - await Promise.resolve(); - await onMessage( + await enqueueDebouncedMessage( + onMessage, createTextEvent({ messageId: "om_2", text: "@bot second", - mentions: [ - { - key: "@_user_1", - id: { open_id: "ou_bot" }, - name: "bot", - }, - ], + mentions: [createMention({ openId: "ou_bot", name: "bot" })], }), ); - await Promise.resolve(); - await Promise.resolve(); await vi.advanceTimersByTimeAsync(25); expect(handleFeishuMessageMock).toHaveBeenCalledTimes(1); @@ -473,42 +469,25 @@ describe("Feishu inbound debounce regressions", () => { }); it("does not synthesize mention-forward intent across separate messages", async () => { - vi.spyOn(dedup, "tryRecordMessage").mockReturnValue(true); - vi.spyOn(dedup, "tryRecordMessagePersistent").mockResolvedValue(true); - vi.spyOn(dedup, "hasRecordedMessage").mockReturnValue(false); - vi.spyOn(dedup, "hasRecordedMessagePersistent").mockResolvedValue(false); + setDedupPassThroughMocks(); const onMessage = await setupDebounceMonitor(); - await onMessage( + await enqueueDebouncedMessage( + onMessage, createTextEvent({ messageId: "om_user_mention", text: "@alice first", - mentions: [ - { - key: "@_user_1", - id: { open_id: "ou_alice" }, - name: "alice", - }, - ], + mentions: [createMention({ openId: "ou_alice", name: "alice" })], }), ); - await Promise.resolve(); - await Promise.resolve(); - await onMessage( + await enqueueDebouncedMessage( + onMessage, createTextEvent({ messageId: "om_bot_mention", text: "@bot second", - mentions: [ - { - key: "@_user_1", - id: { open_id: "ou_bot" }, - name: "bot", - }, - ], + mentions: [createMention({ openId: "ou_bot", name: "bot" })], }), ); - await Promise.resolve(); - await Promise.resolve(); await vi.advanceTimersByTimeAsync(25); expect(handleFeishuMessageMock).toHaveBeenCalledTimes(1); @@ -521,35 +500,24 @@ describe("Feishu inbound debounce regressions", () => { }); it("preserves bot mention signal when the latest merged message has no mentions", async () => { - vi.spyOn(dedup, "tryRecordMessage").mockReturnValue(true); - vi.spyOn(dedup, "tryRecordMessagePersistent").mockResolvedValue(true); - vi.spyOn(dedup, "hasRecordedMessage").mockReturnValue(false); - vi.spyOn(dedup, "hasRecordedMessagePersistent").mockResolvedValue(false); + setDedupPassThroughMocks(); const onMessage = await setupDebounceMonitor(); - await onMessage( + await enqueueDebouncedMessage( + onMessage, createTextEvent({ messageId: "om_bot_first", text: "@bot first", - mentions: [ - { - key: "@_user_1", - id: { open_id: "ou_bot" }, - name: "bot", - }, - ], + mentions: [createMention({ openId: "ou_bot", name: "bot" })], }), ); - await Promise.resolve(); - await Promise.resolve(); - await onMessage( + await enqueueDebouncedMessage( + onMessage, createTextEvent({ messageId: "om_plain_second", text: "plain follow-up", }), ); - await Promise.resolve(); - await Promise.resolve(); await vi.advanceTimersByTimeAsync(25); expect(handleFeishuMessageMock).toHaveBeenCalledTimes(1); diff --git a/extensions/feishu/src/monitor.startup.test.ts b/extensions/feishu/src/monitor.startup.test.ts index 7e1c2c60e5d..e62ff49148a 100644 --- a/extensions/feishu/src/monitor.startup.test.ts +++ b/extensions/feishu/src/monitor.startup.test.ts @@ -1,6 +1,10 @@ import type { ClawdbotConfig } from "openclaw/plugin-sdk/feishu"; import { afterEach, describe, expect, it, vi } from "vitest"; import { monitorFeishuProvider, stopFeishuMonitor } from "./monitor.js"; +import { + createFeishuClientMockModule, + createFeishuRuntimeMockModule, +} from "./monitor.test-mocks.js"; const probeFeishuMock = vi.hoisted(() => vi.fn()); @@ -8,27 +12,8 @@ vi.mock("./probe.js", () => ({ probeFeishu: probeFeishuMock, })); -vi.mock("./client.js", () => ({ - createFeishuWSClient: vi.fn(() => ({ start: vi.fn() })), - createEventDispatcher: vi.fn(() => ({ register: vi.fn() })), -})); - -vi.mock("./runtime.js", () => ({ - getFeishuRuntime: () => ({ - channel: { - debounce: { - resolveInboundDebounceMs: () => 0, - createInboundDebouncer: () => ({ - enqueue: async () => {}, - flushKey: async () => {}, - }), - }, - text: { - hasControlCommand: () => false, - }, - }, - }), -})); +vi.mock("./client.js", () => createFeishuClientMockModule()); +vi.mock("./runtime.js", () => createFeishuRuntimeMockModule()); function buildMultiAccountWebsocketConfig(accountIds: string[]): ClawdbotConfig { return { diff --git a/extensions/feishu/src/monitor.test-mocks.ts b/extensions/feishu/src/monitor.test-mocks.ts index 41e5d9c0086..939afcf5ee7 100644 --- a/extensions/feishu/src/monitor.test-mocks.ts +++ b/extensions/feishu/src/monitor.test-mocks.ts @@ -1,12 +1,27 @@ import { vi } from "vitest"; -export const probeFeishuMock: ReturnType = vi.fn(); +export function createFeishuClientMockModule() { + return { + createFeishuWSClient: vi.fn(() => ({ start: vi.fn() })), + createEventDispatcher: vi.fn(() => ({ register: vi.fn() })), + }; +} -vi.mock("./probe.js", () => ({ - probeFeishu: probeFeishuMock, -})); - -vi.mock("./client.js", () => ({ - createFeishuWSClient: vi.fn(() => ({ start: vi.fn() })), - createEventDispatcher: vi.fn(() => ({ register: vi.fn() })), -})); +export function createFeishuRuntimeMockModule() { + return { + getFeishuRuntime: () => ({ + channel: { + debounce: { + resolveInboundDebounceMs: () => 0, + createInboundDebouncer: () => ({ + enqueue: async () => {}, + flushKey: async () => {}, + }), + }, + text: { + hasControlCommand: () => false, + }, + }, + }), + }; +} diff --git a/extensions/feishu/src/monitor.webhook-security.test.ts b/extensions/feishu/src/monitor.webhook-security.test.ts index d52b417009f..cc64291b4ef 100644 --- a/extensions/feishu/src/monitor.webhook-security.test.ts +++ b/extensions/feishu/src/monitor.webhook-security.test.ts @@ -2,6 +2,10 @@ import { createServer } from "node:http"; import type { AddressInfo } from "node:net"; import type { ClawdbotConfig } from "openclaw/plugin-sdk/feishu"; import { afterEach, describe, expect, it, vi } from "vitest"; +import { + createFeishuClientMockModule, + createFeishuRuntimeMockModule, +} from "./monitor.test-mocks.js"; const probeFeishuMock = vi.hoisted(() => vi.fn()); @@ -9,27 +13,8 @@ vi.mock("./probe.js", () => ({ probeFeishu: probeFeishuMock, })); -vi.mock("./client.js", () => ({ - createFeishuWSClient: vi.fn(() => ({ start: vi.fn() })), - createEventDispatcher: vi.fn(() => ({ register: vi.fn() })), -})); - -vi.mock("./runtime.js", () => ({ - getFeishuRuntime: () => ({ - channel: { - debounce: { - resolveInboundDebounceMs: () => 0, - createInboundDebouncer: () => ({ - enqueue: async () => {}, - flushKey: async () => {}, - }), - }, - text: { - hasControlCommand: () => false, - }, - }, - }), -})); +vi.mock("./client.js", () => createFeishuClientMockModule()); +vi.mock("./runtime.js", () => createFeishuRuntimeMockModule()); vi.mock("@larksuiteoapi/node-sdk", () => ({ adaptDefault: vi.fn( diff --git a/extensions/feishu/src/perm.ts b/extensions/feishu/src/perm.ts index 8ff1a794e29..a031bb015ef 100644 --- a/extensions/feishu/src/perm.ts +++ b/extensions/feishu/src/perm.ts @@ -3,15 +3,11 @@ import type { OpenClawPluginApi } from "openclaw/plugin-sdk/feishu"; import { listEnabledFeishuAccounts } from "./accounts.js"; import { FeishuPermSchema, type FeishuPermParams } from "./perm-schema.js"; import { createFeishuToolClient, resolveAnyEnabledFeishuToolsConfig } from "./tool-account.js"; - -// ============ Helpers ============ - -function json(data: unknown) { - return { - content: [{ type: "text" as const, text: JSON.stringify(data, null, 2) }], - details: data, - }; -} +import { + jsonToolResult, + toolExecutionErrorResult, + unknownToolActionResult, +} from "./tool-result.js"; type ListTokenType = | "doc" @@ -154,21 +150,21 @@ export function registerFeishuPermTools(api: OpenClawPluginApi) { }); switch (p.action) { case "list": - return json(await listMembers(client, p.token, p.type)); + return jsonToolResult(await listMembers(client, p.token, p.type)); case "add": - return json( + return jsonToolResult( await addMember(client, p.token, p.type, p.member_type, p.member_id, p.perm), ); case "remove": - return json( + return jsonToolResult( await removeMember(client, p.token, p.type, p.member_type, p.member_id), ); default: // eslint-disable-next-line @typescript-eslint/no-explicit-any -- exhaustive check fallback - return json({ error: `Unknown action: ${(p as any).action}` }); + return unknownToolActionResult((p as { action?: unknown }).action); } } catch (err) { - return json({ error: err instanceof Error ? err.message : String(err) }); + return toolExecutionErrorResult(err); } }, }; diff --git a/extensions/feishu/src/send-message.ts b/extensions/feishu/src/send-message.ts new file mode 100644 index 00000000000..21772ec374f --- /dev/null +++ b/extensions/feishu/src/send-message.ts @@ -0,0 +1,71 @@ +import { assertFeishuMessageApiSuccess, toFeishuSendResult } from "./send-result.js"; + +type FeishuMessageClient = { + im: { + message: { + reply: (params: { + path: { message_id: string }; + data: Record; + }) => Promise<{ code?: number; msg?: string; data?: { message_id?: string } }>; + create: (params: { + params: { receive_id_type: string }; + data: Record; + }) => Promise<{ code?: number; msg?: string; data?: { message_id?: string } }>; + }; + }; +}; + +export async function sendFeishuMessageWithOptionalReply(params: { + client: FeishuMessageClient; + receiveId: string; + receiveIdType: string; + content: string; + msgType: string; + replyToMessageId?: string; + replyInThread?: boolean; + sendErrorPrefix: string; + replyErrorPrefix: string; + fallbackSendErrorPrefix?: string; + shouldFallbackFromReply?: (response: { code?: number; msg?: string }) => boolean; +}): Promise<{ messageId: string; chatId: string }> { + const data = { + content: params.content, + msg_type: params.msgType, + }; + + if (params.replyToMessageId) { + const response = await params.client.im.message.reply({ + path: { message_id: params.replyToMessageId }, + data: { + ...data, + ...(params.replyInThread ? { reply_in_thread: true } : {}), + }, + }); + if (params.shouldFallbackFromReply?.(response)) { + const fallback = await params.client.im.message.create({ + params: { receive_id_type: params.receiveIdType }, + data: { + receive_id: params.receiveId, + ...data, + }, + }); + assertFeishuMessageApiSuccess( + fallback, + params.fallbackSendErrorPrefix ?? params.sendErrorPrefix, + ); + return toFeishuSendResult(fallback, params.receiveId); + } + assertFeishuMessageApiSuccess(response, params.replyErrorPrefix); + return toFeishuSendResult(response, params.receiveId); + } + + const response = await params.client.im.message.create({ + params: { receive_id_type: params.receiveIdType }, + data: { + receive_id: params.receiveId, + ...data, + }, + }); + assertFeishuMessageApiSuccess(response, params.sendErrorPrefix); + return toFeishuSendResult(response, params.receiveId); +} diff --git a/extensions/feishu/src/tool-result.test.ts b/extensions/feishu/src/tool-result.test.ts new file mode 100644 index 00000000000..d4538133872 --- /dev/null +++ b/extensions/feishu/src/tool-result.test.ts @@ -0,0 +1,32 @@ +import { describe, expect, it } from "vitest"; +import { + jsonToolResult, + toolExecutionErrorResult, + unknownToolActionResult, +} from "./tool-result.js"; + +describe("jsonToolResult", () => { + it("formats tool result with text content and details", () => { + const payload = { ok: true, id: "abc" }; + expect(jsonToolResult(payload)).toEqual({ + content: [{ type: "text", text: JSON.stringify(payload, null, 2) }], + details: payload, + }); + }); + + it("formats unknown action errors", () => { + expect(unknownToolActionResult("create")).toEqual({ + content: [ + { type: "text", text: JSON.stringify({ error: "Unknown action: create" }, null, 2) }, + ], + details: { error: "Unknown action: create" }, + }); + }); + + it("formats execution errors", () => { + expect(toolExecutionErrorResult(new Error("boom"))).toEqual({ + content: [{ type: "text", text: JSON.stringify({ error: "boom" }, null, 2) }], + details: { error: "boom" }, + }); + }); +}); diff --git a/extensions/feishu/src/tool-result.ts b/extensions/feishu/src/tool-result.ts new file mode 100644 index 00000000000..d45bb0cf1c0 --- /dev/null +++ b/extensions/feishu/src/tool-result.ts @@ -0,0 +1,14 @@ +export function jsonToolResult(data: unknown) { + return { + content: [{ type: "text" as const, text: JSON.stringify(data, null, 2) }], + details: data, + }; +} + +export function unknownToolActionResult(action: unknown) { + return jsonToolResult({ error: `Unknown action: ${String(action)}` }); +} + +export function toolExecutionErrorResult(error: unknown) { + return jsonToolResult({ error: error instanceof Error ? error.message : String(error) }); +} diff --git a/extensions/feishu/src/wiki.ts b/extensions/feishu/src/wiki.ts index ef74b5dc0a7..e701f57b3aa 100644 --- a/extensions/feishu/src/wiki.ts +++ b/extensions/feishu/src/wiki.ts @@ -2,17 +2,13 @@ import type * as Lark from "@larksuiteoapi/node-sdk"; import type { OpenClawPluginApi } from "openclaw/plugin-sdk/feishu"; import { listEnabledFeishuAccounts } from "./accounts.js"; import { createFeishuToolClient, resolveAnyEnabledFeishuToolsConfig } from "./tool-account.js"; +import { + jsonToolResult, + toolExecutionErrorResult, + unknownToolActionResult, +} from "./tool-result.js"; import { FeishuWikiSchema, type FeishuWikiParams } from "./wiki-schema.js"; -// ============ Helpers ============ - -function json(data: unknown) { - return { - content: [{ type: "text" as const, text: JSON.stringify(data, null, 2) }], - details: data, - }; -} - type ObjType = "doc" | "sheet" | "mindnote" | "bitable" | "file" | "docx" | "slides"; // ============ Actions ============ @@ -194,22 +190,22 @@ export function registerFeishuWikiTools(api: OpenClawPluginApi) { }); switch (p.action) { case "spaces": - return json(await listSpaces(client)); + return jsonToolResult(await listSpaces(client)); case "nodes": - return json(await listNodes(client, p.space_id, p.parent_node_token)); + return jsonToolResult(await listNodes(client, p.space_id, p.parent_node_token)); case "get": - return json(await getNode(client, p.token)); + return jsonToolResult(await getNode(client, p.token)); case "search": - return json({ + return jsonToolResult({ error: "Search is not available. Use feishu_wiki with action: 'nodes' to browse or action: 'get' to lookup by token.", }); case "create": - return json( + return jsonToolResult( await createNode(client, p.space_id, p.title, p.obj_type, p.parent_node_token), ); case "move": - return json( + return jsonToolResult( await moveNode( client, p.space_id, @@ -219,13 +215,13 @@ export function registerFeishuWikiTools(api: OpenClawPluginApi) { ), ); case "rename": - return json(await renameNode(client, p.space_id, p.node_token, p.title)); + return jsonToolResult(await renameNode(client, p.space_id, p.node_token, p.title)); default: // eslint-disable-next-line @typescript-eslint/no-explicit-any -- exhaustive check fallback - return json({ error: `Unknown action: ${(p as any).action}` }); + return unknownToolActionResult((p as { action?: unknown }).action); } } catch (err) { - return json({ error: err instanceof Error ? err.message : String(err) }); + return toolExecutionErrorResult(err); } }, }; diff --git a/extensions/googlechat/src/monitor-webhook.ts b/extensions/googlechat/src/monitor-webhook.ts index 4272b2bfa87..5f380722267 100644 --- a/extensions/googlechat/src/monitor-webhook.ts +++ b/extensions/googlechat/src/monitor-webhook.ts @@ -25,6 +25,7 @@ function extractBearerToken(header: unknown): string { type ParsedGoogleChatInboundPayload = | { ok: true; event: GoogleChatEvent; addOnBearerToken: string } | { ok: false }; +type ParsedGoogleChatInboundSuccess = Extract; function parseGoogleChatInboundPayload( raw: unknown, @@ -116,6 +117,23 @@ export function createGoogleChatWebhookRequestHandler(params: { const headerBearer = extractBearerToken(req.headers.authorization); let selectedTarget: WebhookTarget | null = null; let parsedEvent: GoogleChatEvent | null = null; + const readAndParseEvent = async ( + profile: "pre-auth" | "post-auth", + ): Promise => { + const body = await readJsonWebhookBodyOrReject({ + req, + res, + profile, + emptyObjectOnEmpty: false, + invalidJsonMessage: "invalid payload", + }); + if (!body.ok) { + return null; + } + + const parsed = parseGoogleChatInboundPayload(body.value, res); + return parsed.ok ? parsed : null; + }; if (headerBearer) { selectedTarget = await resolveWebhookTargetWithAuthOrReject({ @@ -134,36 +152,14 @@ export function createGoogleChatWebhookRequestHandler(params: { return true; } - const body = await readJsonWebhookBodyOrReject({ - req, - res, - profile: "post-auth", - emptyObjectOnEmpty: false, - invalidJsonMessage: "invalid payload", - }); - if (!body.ok) { - return true; - } - - const parsed = parseGoogleChatInboundPayload(body.value, res); - if (!parsed.ok) { + const parsed = await readAndParseEvent("post-auth"); + if (!parsed) { return true; } parsedEvent = parsed.event; } else { - const body = await readJsonWebhookBodyOrReject({ - req, - res, - profile: "pre-auth", - emptyObjectOnEmpty: false, - invalidJsonMessage: "invalid payload", - }); - if (!body.ok) { - return true; - } - - const parsed = parseGoogleChatInboundPayload(body.value, res); - if (!parsed.ok) { + const parsed = await readAndParseEvent("pre-auth"); + if (!parsed) { return true; } parsedEvent = parsed.event; diff --git a/extensions/imessage/src/channel.ts b/extensions/imessage/src/channel.ts index 0835f6734ad..8c77f2a94bf 100644 --- a/extensions/imessage/src/channel.ts +++ b/extensions/imessage/src/channel.ts @@ -1,6 +1,7 @@ import { applyAccountNameToChannelSection, buildChannelConfigSchema, + collectStatusIssuesFromLastError, DEFAULT_ACCOUNT_ID, deleteAccountFromConfigSection, formatPairingApproveHint, @@ -266,21 +267,7 @@ export const imessagePlugin: ChannelPlugin = { cliPath: null, dbPath: null, }, - collectStatusIssues: (accounts) => - accounts.flatMap((account) => { - const lastError = typeof account.lastError === "string" ? account.lastError.trim() : ""; - if (!lastError) { - return []; - } - return [ - { - channel: "imessage", - accountId: account.accountId, - kind: "runtime", - message: `Channel error: ${lastError}`, - }, - ]; - }), + collectStatusIssues: (accounts) => collectStatusIssuesFromLastError("imessage", accounts), buildChannelSummary: ({ snapshot }) => ({ configured: snapshot.configured ?? false, running: snapshot.running ?? false, diff --git a/extensions/irc/src/inbound.ts b/extensions/irc/src/inbound.ts index 2c3378de1c1..6c03ebadf02 100644 --- a/extensions/irc/src/inbound.ts +++ b/extensions/irc/src/inbound.ts @@ -1,8 +1,7 @@ import { GROUP_POLICY_BLOCKED_LABEL, createScopedPairingAccess, - createNormalizedOutboundDeliverer, - createReplyPrefixOptions, + dispatchInboundReplyWithBase, formatTextWithAttachmentLinks, logInboundDrop, isDangerousNameMatchingEnabled, @@ -332,44 +331,31 @@ export async function handleIrcInbound(params: { CommandAuthorized: commandAuthorized, }); - await core.channel.session.recordInboundSession({ + await dispatchInboundReplyWithBase({ + cfg: config as OpenClawConfig, + channel: CHANNEL_ID, + accountId: account.accountId, + route, storePath, - sessionKey: ctxPayload.SessionKey ?? route.sessionKey, - ctx: ctxPayload, + ctxPayload, + core, + deliver: async (payload) => { + await deliverIrcReply({ + payload, + target: peerId, + accountId: account.accountId, + sendReply: params.sendReply, + statusSink, + }); + }, onRecordError: (err) => { runtime.error?.(`irc: failed updating session meta: ${String(err)}`); }, - }); - - const { onModelSelected, ...prefixOptions } = createReplyPrefixOptions({ - cfg: config as OpenClawConfig, - agentId: route.agentId, - channel: CHANNEL_ID, - accountId: account.accountId, - }); - const deliverReply = createNormalizedOutboundDeliverer(async (payload) => { - await deliverIrcReply({ - payload, - target: peerId, - accountId: account.accountId, - sendReply: params.sendReply, - statusSink, - }); - }); - - await core.channel.reply.dispatchReplyWithBufferedBlockDispatcher({ - ctx: ctxPayload, - cfg: config as OpenClawConfig, - dispatcherOptions: { - ...prefixOptions, - deliver: deliverReply, - onError: (err, info) => { - runtime.error?.(`irc ${info.kind} reply failed: ${String(err)}`); - }, + onDispatchError: (err, info) => { + runtime.error?.(`irc ${info.kind} reply failed: ${String(err)}`); }, replyOptions: { skillFilter: groupMatch.groupConfig?.skills, - onModelSelected, disableBlockStreaming: typeof account.config.blockStreaming === "boolean" ? !account.config.blockStreaming diff --git a/extensions/line/src/channel.ts b/extensions/line/src/channel.ts index c29046eaaf0..69491cf61f2 100644 --- a/extensions/line/src/channel.ts +++ b/extensions/line/src/channel.ts @@ -1,6 +1,8 @@ import { buildChannelConfigSchema, + buildComputedAccountStatusSnapshot, buildTokenChannelStatusSummary, + clearAccountEntryFields, DEFAULT_ACCOUNT_ID, LineConfigSchema, processLineMessage, @@ -27,6 +29,42 @@ const meta = { systemImage: "message.fill", }; +function patchLineAccountConfig( + cfg: OpenClawConfig, + lineConfig: LineConfig, + accountId: string, + patch: Record, +): OpenClawConfig { + if (accountId === DEFAULT_ACCOUNT_ID) { + return { + ...cfg, + channels: { + ...cfg.channels, + line: { + ...lineConfig, + ...patch, + }, + }, + }; + } + return { + ...cfg, + channels: { + ...cfg.channels, + line: { + ...lineConfig, + accounts: { + ...lineConfig.accounts, + [accountId]: { + ...lineConfig.accounts?.[accountId], + ...patch, + }, + }, + }, + }, + }; +} + export const linePlugin: ChannelPlugin = { id: "line", meta: { @@ -67,34 +105,7 @@ export const linePlugin: ChannelPlugin = { defaultAccountId: (cfg) => getLineRuntime().channel.line.resolveDefaultLineAccountId(cfg), setAccountEnabled: ({ cfg, accountId, enabled }) => { const lineConfig = (cfg.channels?.line ?? {}) as LineConfig; - if (accountId === DEFAULT_ACCOUNT_ID) { - return { - ...cfg, - channels: { - ...cfg.channels, - line: { - ...lineConfig, - enabled, - }, - }, - }; - } - return { - ...cfg, - channels: { - ...cfg.channels, - line: { - ...lineConfig, - accounts: { - ...lineConfig.accounts, - [accountId]: { - ...lineConfig.accounts?.[accountId], - enabled, - }, - }, - }, - }, - }; + return patchLineAccountConfig(cfg, lineConfig, accountId, { enabled }); }, deleteAccount: ({ cfg, accountId }) => { const lineConfig = (cfg.channels?.line ?? {}) as LineConfig; @@ -224,34 +235,7 @@ export const linePlugin: ChannelPlugin = { getLineRuntime().channel.line.normalizeAccountId(accountId), applyAccountName: ({ cfg, accountId, name }) => { const lineConfig = (cfg.channels?.line ?? {}) as LineConfig; - if (accountId === DEFAULT_ACCOUNT_ID) { - return { - ...cfg, - channels: { - ...cfg.channels, - line: { - ...lineConfig, - name, - }, - }, - }; - } - return { - ...cfg, - channels: { - ...cfg.channels, - line: { - ...lineConfig, - accounts: { - ...lineConfig.accounts, - [accountId]: { - ...lineConfig.accounts?.[accountId], - name, - }, - }, - }, - }, - }; + return patchLineAccountConfig(cfg, lineConfig, accountId, { name }); }, validateInput: ({ accountId, input }) => { const typedInput = input as { @@ -615,20 +599,18 @@ export const linePlugin: ChannelPlugin = { const configured = Boolean( account.channelAccessToken?.trim() && account.channelSecret?.trim(), ); - return { + const base = buildComputedAccountStatusSnapshot({ accountId: account.accountId, name: account.name, enabled: account.enabled, configured, - tokenSource: account.tokenSource, - running: runtime?.running ?? false, - lastStartAt: runtime?.lastStartAt ?? null, - lastStopAt: runtime?.lastStopAt ?? null, - lastError: runtime?.lastError ?? null, - mode: "webhook", + runtime, probe, - lastInboundAt: runtime?.lastInboundAt ?? null, - lastOutboundAt: runtime?.lastOutboundAt ?? null, + }); + return { + ...base, + tokenSource: account.tokenSource, + mode: "webhook", }; }, }, @@ -699,39 +681,21 @@ export const linePlugin: ChannelPlugin = { } } - const accounts = nextLine.accounts ? { ...nextLine.accounts } : undefined; - if (accounts && accountId in accounts) { - const entry = accounts[accountId]; - if (entry && typeof entry === "object") { - const nextEntry = { ...entry } as Record; - if ( - "channelAccessToken" in nextEntry || - "channelSecret" in nextEntry || - "tokenFile" in nextEntry || - "secretFile" in nextEntry - ) { - cleared = true; - delete nextEntry.channelAccessToken; - delete nextEntry.channelSecret; - delete nextEntry.tokenFile; - delete nextEntry.secretFile; - changed = true; - } - if (Object.keys(nextEntry).length === 0) { - delete accounts[accountId]; - changed = true; - } else { - accounts[accountId] = nextEntry as typeof entry; - } + const accountCleanup = clearAccountEntryFields({ + accounts: nextLine.accounts, + accountId, + fields: ["channelAccessToken", "channelSecret", "tokenFile", "secretFile"], + markClearedOnFieldPresence: true, + }); + if (accountCleanup.changed) { + changed = true; + if (accountCleanup.cleared) { + cleared = true; } - } - - if (accounts) { - if (Object.keys(accounts).length === 0) { - delete nextLine.accounts; - changed = true; + if (accountCleanup.nextAccounts) { + nextLine.accounts = accountCleanup.nextAccounts; } else { - nextLine.accounts = accounts; + delete nextLine.accounts; } } diff --git a/extensions/matrix/src/channel.ts b/extensions/matrix/src/channel.ts index 3ccfd2a8ae4..29dfe5fd357 100644 --- a/extensions/matrix/src/channel.ts +++ b/extensions/matrix/src/channel.ts @@ -2,6 +2,7 @@ import { applyAccountNameToChannelSection, buildChannelConfigSchema, buildProbeChannelStatusSummary, + collectStatusIssuesFromLastError, DEFAULT_ACCOUNT_ID, deleteAccountFromConfigSection, formatPairingApproveHint, @@ -380,21 +381,7 @@ export const matrixPlugin: ChannelPlugin = { lastStopAt: null, lastError: null, }, - collectStatusIssues: (accounts) => - accounts.flatMap((account) => { - const lastError = typeof account.lastError === "string" ? account.lastError.trim() : ""; - if (!lastError) { - return []; - } - return [ - { - channel: "matrix", - accountId: account.accountId, - kind: "runtime", - message: `Channel error: ${lastError}`, - }, - ]; - }), + collectStatusIssues: (accounts) => collectStatusIssuesFromLastError("matrix", accounts), buildChannelSummary: ({ snapshot }) => buildProbeChannelStatusSummary(snapshot, { baseUrl: snapshot.baseUrl ?? null }), probeAccount: async ({ account, timeoutMs, cfg }) => { diff --git a/extensions/matrix/src/matrix/monitor/handler.ts b/extensions/matrix/src/matrix/monitor/handler.ts index 53651ce4b16..bacd6890ab9 100644 --- a/extensions/matrix/src/matrix/monitor/handler.ts +++ b/extensions/matrix/src/matrix/monitor/handler.ts @@ -4,9 +4,11 @@ import { createScopedPairingAccess, createReplyPrefixOptions, createTypingCallbacks, + dispatchReplyFromConfigWithSettledDispatcher, formatAllowlistMatchMeta, logInboundDrop, logTypingFailure, + resolveInboundSessionEnvelopeContext, resolveControlCommandGate, type PluginRuntime, type RuntimeEnv, @@ -484,14 +486,12 @@ export function createMatrixRoomMessageHandler(params: MatrixMonitorHandlerParam const textWithId = threadRootId ? `${bodyText}\n[matrix event id: ${messageId} room: ${roomId} thread: ${threadRootId}]` : `${bodyText}\n[matrix event id: ${messageId} room: ${roomId}]`; - const storePath = core.channel.session.resolveStorePath(cfg.session?.store, { - agentId: route.agentId, - }); - const envelopeOptions = core.channel.reply.resolveEnvelopeFormatOptions(cfg); - const previousTimestamp = core.channel.session.readSessionUpdatedAt({ - storePath, - sessionKey: route.sessionKey, - }); + const { storePath, envelopeOptions, previousTimestamp } = + resolveInboundSessionEnvelopeContext({ + cfg, + agentId: route.agentId, + sessionKey: route.sessionKey, + }); const body = core.channel.reply.formatInboundEnvelope({ channel: "Matrix", from: envelopeFrom, @@ -655,22 +655,18 @@ export function createMatrixRoomMessageHandler(params: MatrixMonitorHandlerParam }, }); - const { queuedFinal, counts } = await core.channel.reply.withReplyDispatcher({ + const { queuedFinal, counts } = await dispatchReplyFromConfigWithSettledDispatcher({ + cfg, + ctxPayload, dispatcher, onSettled: () => { markDispatchIdle(); }, - run: () => - core.channel.reply.dispatchReplyFromConfig({ - ctx: ctxPayload, - cfg, - dispatcher, - replyOptions: { - ...replyOptions, - skillFilter: roomConfig?.skills, - onModelSelected, - }, - }), + replyOptions: { + ...replyOptions, + skillFilter: roomConfig?.skills, + onModelSelected, + }, }); if (!queuedFinal) { return; diff --git a/extensions/matrix/src/matrix/monitor/index.ts b/extensions/matrix/src/matrix/monitor/index.ts index 2449b215715..1634a75502b 100644 --- a/extensions/matrix/src/matrix/monitor/index.ts +++ b/extensions/matrix/src/matrix/monitor/index.ts @@ -1,7 +1,7 @@ import { - createLoggerBackedRuntime, GROUP_POLICY_BLOCKED_LABEL, mergeAllowlist, + resolveRuntimeEnv, resolveAllowlistProviderRuntimeGroupPolicy, resolveDefaultGroupPolicy, summarizeMapping, @@ -241,11 +241,10 @@ export async function monitorMatrixProvider(opts: MonitorMatrixOpts = {}): Promi } const logger = core.logging.getChildLogger({ module: "matrix-auto-reply" }); - const runtime: RuntimeEnv = - opts.runtime ?? - createLoggerBackedRuntime({ - logger, - }); + const runtime: RuntimeEnv = resolveRuntimeEnv({ + runtime: opts.runtime, + logger, + }); const logVerboseMessage = (message: string) => { if (!core.logging.shouldLogVerbose()) { return; diff --git a/extensions/minimax-portal-auth/index.ts b/extensions/minimax-portal-auth/index.ts index 6eee6bdabe1..d2d1bab9899 100644 --- a/extensions/minimax-portal-auth/index.ts +++ b/extensions/minimax-portal-auth/index.ts @@ -1,4 +1,5 @@ import { + buildOauthProviderAuthResult, emptyPluginConfigSchema, type OpenClawPluginApi, type ProviderAuthContext, @@ -60,22 +61,14 @@ function createOAuthHandler(region: MiniMaxRegion) { await ctx.prompter.note(result.notification_message, "MiniMax OAuth"); } - const profileId = `${PROVIDER_ID}:default`; const baseUrl = result.resourceUrl || defaultBaseUrl; - return { - profiles: [ - { - profileId, - credential: { - type: "oauth" as const, - provider: PROVIDER_ID, - access: result.access, - refresh: result.refresh, - expires: result.expires, - }, - }, - ], + return buildOauthProviderAuthResult({ + providerId: PROVIDER_ID, + defaultModel: modelRef(DEFAULT_MODEL), + access: result.access, + refresh: result.refresh, + expires: result.expires, configPatch: { models: { providers: { @@ -119,13 +112,12 @@ function createOAuthHandler(region: MiniMaxRegion) { }, }, }, - defaultModel: modelRef(DEFAULT_MODEL), notes: [ "MiniMax OAuth tokens auto-refresh. Re-run login if refresh fails or access is revoked.", `Base URL defaults to ${defaultBaseUrl}. Override models.providers.${PROVIDER_ID}.baseUrl if needed.`, ...(result.notification_message ? [result.notification_message] : []), ], - }; + }); } catch (err) { const errorMsg = err instanceof Error ? err.message : String(err); progress.stop(`MiniMax OAuth failed: ${errorMsg}`); diff --git a/extensions/msteams/src/channel.ts b/extensions/msteams/src/channel.ts index 90223956988..be804a25c44 100644 --- a/extensions/msteams/src/channel.ts +++ b/extensions/msteams/src/channel.ts @@ -4,7 +4,8 @@ import type { OpenClawConfig, } from "openclaw/plugin-sdk/msteams"; import { - buildBaseChannelStatusSummary, + buildProbeChannelStatusSummary, + buildRuntimeAccountStatusSnapshot, buildChannelConfigSchema, createDefaultChannelRuntimeState, DEFAULT_ACCOUNT_ID, @@ -250,11 +251,43 @@ export const msteamsPlugin: ChannelPlugin = { name: undefined as string | undefined, note: undefined as string | undefined, })); + type ResolveTargetResultEntry = (typeof results)[number]; + type PendingTargetEntry = { input: string; query: string; index: number }; const stripPrefix = (value: string) => normalizeMSTeamsUserInput(value); + const markPendingLookupFailed = (pending: PendingTargetEntry[]) => { + pending.forEach(({ index }) => { + const entry = results[index]; + if (entry) { + entry.note = "lookup failed"; + } + }); + }; + const resolvePending = async ( + pending: PendingTargetEntry[], + resolveEntries: (entries: string[]) => Promise, + applyResolvedEntry: (target: ResolveTargetResultEntry, entry: T) => void, + ) => { + if (pending.length === 0) { + return; + } + try { + const resolved = await resolveEntries(pending.map((entry) => entry.query)); + resolved.forEach((entry, idx) => { + const target = results[pending[idx]?.index ?? -1]; + if (!target) { + return; + } + applyResolvedEntry(target, entry); + }); + } catch (err) { + runtime.error?.(`msteams resolve failed: ${String(err)}`); + markPendingLookupFailed(pending); + } + }; if (kind === "user") { - const pending: Array<{ input: string; query: string; index: number }> = []; + const pending: PendingTargetEntry[] = []; results.forEach((entry, index) => { const trimmed = entry.input.trim(); if (!trimmed) { @@ -270,37 +303,21 @@ export const msteamsPlugin: ChannelPlugin = { pending.push({ input: entry.input, query: cleaned, index }); }); - if (pending.length > 0) { - try { - const resolved = await resolveMSTeamsUserAllowlist({ - cfg, - entries: pending.map((entry) => entry.query), - }); - resolved.forEach((entry, idx) => { - const target = results[pending[idx]?.index ?? -1]; - if (!target) { - return; - } - target.resolved = entry.resolved; - target.id = entry.id; - target.name = entry.name; - target.note = entry.note; - }); - } catch (err) { - runtime.error?.(`msteams resolve failed: ${String(err)}`); - pending.forEach(({ index }) => { - const entry = results[index]; - if (entry) { - entry.note = "lookup failed"; - } - }); - } - } + await resolvePending( + pending, + (entries) => resolveMSTeamsUserAllowlist({ cfg, entries }), + (target, entry) => { + target.resolved = entry.resolved; + target.id = entry.id; + target.name = entry.name; + target.note = entry.note; + }, + ); return results; } - const pending: Array<{ input: string; query: string; index: number }> = []; + const pending: PendingTargetEntry[] = []; results.forEach((entry, index) => { const trimmed = entry.input.trim(); if (!trimmed) { @@ -323,48 +340,32 @@ export const msteamsPlugin: ChannelPlugin = { pending.push({ input: entry.input, query, index }); }); - if (pending.length > 0) { - try { - const resolved = await resolveMSTeamsChannelAllowlist({ - cfg, - entries: pending.map((entry) => entry.query), - }); - resolved.forEach((entry, idx) => { - const target = results[pending[idx]?.index ?? -1]; - if (!target) { - return; - } - if (!entry.resolved || !entry.teamId) { - target.resolved = false; - target.note = entry.note; - return; - } - target.resolved = true; - if (entry.channelId) { - target.id = `${entry.teamId}/${entry.channelId}`; - target.name = - entry.channelName && entry.teamName - ? `${entry.teamName}/${entry.channelName}` - : (entry.channelName ?? entry.teamName); - } else { - target.id = entry.teamId; - target.name = entry.teamName; - target.note = "team id"; - } - if (entry.note) { - target.note = entry.note; - } - }); - } catch (err) { - runtime.error?.(`msteams resolve failed: ${String(err)}`); - pending.forEach(({ index }) => { - const entry = results[index]; - if (entry) { - entry.note = "lookup failed"; - } - }); - } - } + await resolvePending( + pending, + (entries) => resolveMSTeamsChannelAllowlist({ cfg, entries }), + (target, entry) => { + if (!entry.resolved || !entry.teamId) { + target.resolved = false; + target.note = entry.note; + return; + } + target.resolved = true; + if (entry.channelId) { + target.id = `${entry.teamId}/${entry.channelId}`; + target.name = + entry.channelName && entry.teamName + ? `${entry.teamName}/${entry.channelName}` + : (entry.channelName ?? entry.teamName); + } else { + target.id = entry.teamId; + target.name = entry.teamName; + target.note = "team id"; + } + if (entry.note) { + target.note = entry.note; + } + }, + ); return results; }, @@ -429,23 +430,17 @@ export const msteamsPlugin: ChannelPlugin = { outbound: msteamsOutbound, status: { defaultRuntime: createDefaultChannelRuntimeState(DEFAULT_ACCOUNT_ID, { port: null }), - buildChannelSummary: ({ snapshot }) => ({ - ...buildBaseChannelStatusSummary(snapshot), - port: snapshot.port ?? null, - probe: snapshot.probe, - lastProbeAt: snapshot.lastProbeAt ?? null, - }), + buildChannelSummary: ({ snapshot }) => + buildProbeChannelStatusSummary(snapshot, { + port: snapshot.port ?? null, + }), probeAccount: async ({ cfg }) => await probeMSTeams(cfg.channels?.msteams), buildAccountSnapshot: ({ account, runtime, probe }) => ({ accountId: account.accountId, enabled: account.enabled, configured: account.configured, - running: runtime?.running ?? false, - lastStartAt: runtime?.lastStartAt ?? null, - lastStopAt: runtime?.lastStopAt ?? null, - lastError: runtime?.lastError ?? null, + ...buildRuntimeAccountStatusSnapshot({ runtime, probe }), port: runtime?.port ?? null, - probe, }), }, gateway: { diff --git a/extensions/msteams/src/messenger.test.ts b/extensions/msteams/src/messenger.test.ts index 627bad15d94..aa0a92b5159 100644 --- a/extensions/msteams/src/messenger.test.ts +++ b/extensions/msteams/src/messenger.test.ts @@ -72,6 +72,17 @@ const createRecordedSendActivity = ( }; }; +const REVOCATION_ERROR = "Cannot perform 'set' on a proxy that has been revoked"; + +const createFallbackAdapter = (proactiveSent: string[]): MSTeamsAdapter => ({ + continueConversation: async (_appId, _reference, logic) => { + await logic({ + sendActivity: createRecordedSendActivity(proactiveSent), + }); + }, + process: async () => {}, +}); + describe("msteams messenger", () => { beforeEach(() => { setMSTeamsRuntime(runtimeStub); @@ -297,18 +308,11 @@ describe("msteams messenger", () => { const ctx = { sendActivity: async () => { - throw new TypeError("Cannot perform 'set' on a proxy that has been revoked"); + throw new TypeError(REVOCATION_ERROR); }, }; - const adapter: MSTeamsAdapter = { - continueConversation: async (_appId, _reference, logic) => { - await logic({ - sendActivity: createRecordedSendActivity(proactiveSent), - }); - }, - process: async () => {}, - }; + const adapter = createFallbackAdapter(proactiveSent); const ids = await sendMSTeamsMessages({ replyStyle: "thread", @@ -338,18 +342,11 @@ describe("msteams messenger", () => { threadSent.push(content); return { id: `id:${content}` }; } - throw new TypeError("Cannot perform 'set' on a proxy that has been revoked"); + throw new TypeError(REVOCATION_ERROR); }, }; - const adapter: MSTeamsAdapter = { - continueConversation: async (_appId, _reference, logic) => { - await logic({ - sendActivity: createRecordedSendActivity(proactiveSent), - }); - }, - process: async () => {}, - }; + const adapter = createFallbackAdapter(proactiveSent); const ids = await sendMSTeamsMessages({ replyStyle: "thread", diff --git a/extensions/msteams/src/monitor-handler/message-handler.ts b/extensions/msteams/src/monitor-handler/message-handler.ts index b4a305fd7d4..ba68fc9f5c9 100644 --- a/extensions/msteams/src/monitor-handler/message-handler.ts +++ b/extensions/msteams/src/monitor-handler/message-handler.ts @@ -2,6 +2,7 @@ import { DEFAULT_ACCOUNT_ID, buildPendingHistoryContextFromMap, clearHistoryEntriesIfEnabled, + dispatchReplyFromConfigWithSettledDispatcher, DEFAULT_GROUP_HISTORY_LIMIT, createScopedPairingAccess, logInboundDrop, @@ -11,6 +12,7 @@ import { isDangerousNameMatchingEnabled, readStoreAllowFromForDmPolicy, resolveMentionGating, + resolveInboundSessionEnvelopeContext, formatAllowlistMatchMeta, resolveEffectiveAllowFromLists, resolveDmGroupAccessWithLists, @@ -451,12 +453,9 @@ export function createMSTeamsMessageHandler(deps: MSTeamsMessageHandlerDeps) { const mediaPayload = buildMSTeamsMediaPayload(mediaList); const envelopeFrom = isDirectMessage ? senderName : conversationType; - const storePath = core.channel.session.resolveStorePath(cfg.session?.store, { + const { storePath, envelopeOptions, previousTimestamp } = resolveInboundSessionEnvelopeContext({ + cfg, agentId: route.agentId, - }); - const envelopeOptions = core.channel.reply.resolveEnvelopeFormatOptions(cfg); - const previousTimestamp = core.channel.session.readSessionUpdatedAt({ - storePath, sessionKey: route.sessionKey, }); const body = core.channel.reply.formatAgentEnvelope({ @@ -559,18 +558,14 @@ export function createMSTeamsMessageHandler(deps: MSTeamsMessageHandlerDeps) { log.info("dispatching to agent", { sessionKey: route.sessionKey }); try { - const { queuedFinal, counts } = await core.channel.reply.withReplyDispatcher({ + const { queuedFinal, counts } = await dispatchReplyFromConfigWithSettledDispatcher({ + cfg, + ctxPayload, dispatcher, onSettled: () => { markDispatchIdle(); }, - run: () => - core.channel.reply.dispatchReplyFromConfig({ - ctx: ctxPayload, - cfg, - dispatcher, - replyOptions, - }), + replyOptions, }); log.info("dispatch complete", { queuedFinal, counts }); diff --git a/extensions/msteams/src/send.ts b/extensions/msteams/src/send.ts index cfa023d8871..48fe0443a22 100644 --- a/extensions/msteams/src/send.ts +++ b/extensions/msteams/src/send.ts @@ -157,24 +157,13 @@ export async function sendMessageMSTeams( log.debug?.("sending file consent card", { uploadId, fileName, size: media.buffer.length }); - const baseRef = buildConversationReference(ref); - const proactiveRef = { ...baseRef, activityId: undefined }; - - let messageId = "unknown"; - try { - await adapter.continueConversation(appId, proactiveRef, async (turnCtx) => { - const response = await turnCtx.sendActivity(activity); - messageId = extractMessageId(response) ?? "unknown"; - }); - } catch (err) { - const classification = classifyMSTeamsSendError(err); - const hint = formatMSTeamsSendErrorHint(classification); - const status = classification.statusCode ? ` (HTTP ${classification.statusCode})` : ""; - throw new Error( - `msteams consent card send failed${status}: ${formatUnknownError(err)}${hint ? ` (${hint})` : ""}`, - { cause: err }, - ); - } + const messageId = await sendProactiveActivity({ + adapter, + appId, + ref, + activity, + errorPrefix: "msteams consent card send", + }); log.info("sent file consent card", { conversationId, messageId, uploadId }); @@ -245,14 +234,11 @@ export async function sendMessageMSTeams( text: messageText || undefined, attachments: [fileCardAttachment], }; - - const baseRef = buildConversationReference(ref); - const proactiveRef = { ...baseRef, activityId: undefined }; - - let messageId = "unknown"; - await adapter.continueConversation(appId, proactiveRef, async (turnCtx) => { - const response = await turnCtx.sendActivity(activity); - messageId = extractMessageId(response) ?? "unknown"; + const messageId = await sendProactiveActivityRaw({ + adapter, + appId, + ref, + activity, }); log.info("sent native file card", { @@ -288,14 +274,11 @@ export async function sendMessageMSTeams( type: "message", text: messageText ? `${messageText}\n\n${fileLink}` : fileLink, }; - - const baseRef = buildConversationReference(ref); - const proactiveRef = { ...baseRef, activityId: undefined }; - - let messageId = "unknown"; - await adapter.continueConversation(appId, proactiveRef, async (turnCtx) => { - const response = await turnCtx.sendActivity(activity); - messageId = extractMessageId(response) ?? "unknown"; + const messageId = await sendProactiveActivityRaw({ + adapter, + appId, + ref, + activity, }); log.info("sent message with OneDrive file link", { @@ -382,13 +365,14 @@ type ProactiveActivityParams = { errorPrefix: string; }; -async function sendProactiveActivity({ +type ProactiveActivityRawParams = Omit; + +async function sendProactiveActivityRaw({ adapter, appId, ref, activity, - errorPrefix, -}: ProactiveActivityParams): Promise { +}: ProactiveActivityRawParams): Promise { const baseRef = buildConversationReference(ref); const proactiveRef = { ...baseRef, @@ -396,12 +380,27 @@ async function sendProactiveActivity({ }; let messageId = "unknown"; + await adapter.continueConversation(appId, proactiveRef, async (ctx) => { + const response = await ctx.sendActivity(activity); + messageId = extractMessageId(response) ?? "unknown"; + }); + return messageId; +} + +async function sendProactiveActivity({ + adapter, + appId, + ref, + activity, + errorPrefix, +}: ProactiveActivityParams): Promise { try { - await adapter.continueConversation(appId, proactiveRef, async (ctx) => { - const response = await ctx.sendActivity(activity); - messageId = extractMessageId(response) ?? "unknown"; + return await sendProactiveActivityRaw({ + adapter, + appId, + ref, + activity, }); - return messageId; } catch (err) { const classification = classifyMSTeamsSendError(err); const hint = formatMSTeamsSendErrorHint(classification); diff --git a/extensions/nextcloud-talk/src/channel.ts b/extensions/nextcloud-talk/src/channel.ts index 003a118e2ef..a547a735ad3 100644 --- a/extensions/nextcloud-talk/src/channel.ts +++ b/extensions/nextcloud-talk/src/channel.ts @@ -1,6 +1,9 @@ import { applyAccountNameToChannelSection, + buildBaseChannelStatusSummary, buildChannelConfigSchema, + buildRuntimeAccountStatusSnapshot, + clearAccountEntryFields, DEFAULT_ACCOUNT_ID, deleteAccountFromConfigSection, formatPairingApproveHint, @@ -288,17 +291,21 @@ export const nextcloudTalkPlugin: ChannelPlugin = lastStopAt: null, lastError: null, }, - buildChannelSummary: ({ snapshot }) => ({ - configured: snapshot.configured ?? false, - secretSource: snapshot.secretSource ?? "none", - running: snapshot.running ?? false, - mode: "webhook", - lastStartAt: snapshot.lastStartAt ?? null, - lastStopAt: snapshot.lastStopAt ?? null, - lastError: snapshot.lastError ?? null, - }), + buildChannelSummary: ({ snapshot }) => { + const base = buildBaseChannelStatusSummary(snapshot); + return { + configured: base.configured, + secretSource: snapshot.secretSource ?? "none", + running: base.running, + mode: "webhook", + lastStartAt: base.lastStartAt, + lastStopAt: base.lastStopAt, + lastError: base.lastError, + }; + }, buildAccountSnapshot: ({ account, runtime }) => { const configured = Boolean(account.secret?.trim() && account.baseUrl?.trim()); + const runtimeSnapshot = buildRuntimeAccountStatusSnapshot({ runtime }); return { accountId: account.accountId, name: account.name, @@ -306,10 +313,10 @@ export const nextcloudTalkPlugin: ChannelPlugin = configured, secretSource: account.secretSource, baseUrl: account.baseUrl ? "[set]" : "[missing]", - running: runtime?.running ?? false, - lastStartAt: runtime?.lastStartAt ?? null, - lastStopAt: runtime?.lastStopAt ?? null, - lastError: runtime?.lastError ?? null, + running: runtimeSnapshot.running, + lastStartAt: runtimeSnapshot.lastStartAt, + lastStopAt: runtimeSnapshot.lastStopAt, + lastError: runtimeSnapshot.lastError, mode: "webhook", lastInboundAt: runtime?.lastInboundAt ?? null, lastOutboundAt: runtime?.lastOutboundAt ?? null, @@ -353,36 +360,20 @@ export const nextcloudTalkPlugin: ChannelPlugin = cleared = true; changed = true; } - const accounts = - nextSection.accounts && typeof nextSection.accounts === "object" - ? { ...nextSection.accounts } - : undefined; - if (accounts && accountId in accounts) { - const entry = accounts[accountId]; - if (entry && typeof entry === "object") { - const nextEntry = { ...entry } as Record; - if ("botSecret" in nextEntry) { - const secret = nextEntry.botSecret; - if (typeof secret === "string" ? secret.trim() : secret) { - cleared = true; - } - delete nextEntry.botSecret; - changed = true; - } - if (Object.keys(nextEntry).length === 0) { - delete accounts[accountId]; - changed = true; - } else { - accounts[accountId] = nextEntry as typeof entry; - } + const accountCleanup = clearAccountEntryFields({ + accounts: nextSection.accounts, + accountId, + fields: ["botSecret"], + }); + if (accountCleanup.changed) { + changed = true; + if (accountCleanup.cleared) { + cleared = true; } - } - if (accounts) { - if (Object.keys(accounts).length === 0) { - delete nextSection.accounts; - changed = true; + if (accountCleanup.nextAccounts) { + nextSection.accounts = accountCleanup.nextAccounts; } else { - nextSection.accounts = accounts; + delete nextSection.accounts; } } } diff --git a/extensions/nextcloud-talk/src/inbound.ts b/extensions/nextcloud-talk/src/inbound.ts index 3b0addf257d..1657cbd9113 100644 --- a/extensions/nextcloud-talk/src/inbound.ts +++ b/extensions/nextcloud-talk/src/inbound.ts @@ -1,8 +1,7 @@ import { GROUP_POLICY_BLOCKED_LABEL, createScopedPairingAccess, - createNormalizedOutboundDeliverer, - createReplyPrefixOptions, + dispatchInboundReplyWithBase, formatTextWithAttachmentLinks, logInboundDrop, readStoreAllowFromForDmPolicy, @@ -291,43 +290,30 @@ export async function handleNextcloudTalkInbound(params: { CommandAuthorized: commandAuthorized, }); - await core.channel.session.recordInboundSession({ + await dispatchInboundReplyWithBase({ + cfg: config as OpenClawConfig, + channel: CHANNEL_ID, + accountId: account.accountId, + route, storePath, - sessionKey: ctxPayload.SessionKey ?? route.sessionKey, - ctx: ctxPayload, + ctxPayload, + core, + deliver: async (payload) => { + await deliverNextcloudTalkReply({ + payload, + roomToken, + accountId: account.accountId, + statusSink, + }); + }, onRecordError: (err) => { runtime.error?.(`nextcloud-talk: failed updating session meta: ${String(err)}`); }, - }); - - const { onModelSelected, ...prefixOptions } = createReplyPrefixOptions({ - cfg: config as OpenClawConfig, - agentId: route.agentId, - channel: CHANNEL_ID, - accountId: account.accountId, - }); - const deliverReply = createNormalizedOutboundDeliverer(async (payload) => { - await deliverNextcloudTalkReply({ - payload, - roomToken, - accountId: account.accountId, - statusSink, - }); - }); - - await core.channel.reply.dispatchReplyWithBufferedBlockDispatcher({ - ctx: ctxPayload, - cfg: config as OpenClawConfig, - dispatcherOptions: { - ...prefixOptions, - deliver: deliverReply, - onError: (err, info) => { - runtime.error?.(`nextcloud-talk ${info.kind} reply failed: ${String(err)}`); - }, + onDispatchError: (err, info) => { + runtime.error?.(`nextcloud-talk ${info.kind} reply failed: ${String(err)}`); }, replyOptions: { skillFilter: roomConfig?.skills, - onModelSelected, disableBlockStreaming: typeof account.config.blockStreaming === "boolean" ? !account.config.blockStreaming diff --git a/extensions/nextcloud-talk/src/onboarding.ts b/extensions/nextcloud-talk/src/onboarding.ts index 1f07ce48162..71d904c7a0e 100644 --- a/extensions/nextcloud-talk/src/onboarding.ts +++ b/extensions/nextcloud-talk/src/onboarding.ts @@ -43,6 +43,45 @@ function setNextcloudTalkDmPolicy(cfg: CoreConfig, dmPolicy: DmPolicy): CoreConf } as CoreConfig; } +function setNextcloudTalkAccountConfig( + cfg: CoreConfig, + accountId: string, + updates: Record, +): CoreConfig { + if (accountId === DEFAULT_ACCOUNT_ID) { + return { + ...cfg, + channels: { + ...cfg.channels, + "nextcloud-talk": { + ...cfg.channels?.["nextcloud-talk"], + enabled: true, + ...updates, + }, + }, + }; + } + + return { + ...cfg, + channels: { + ...cfg.channels, + "nextcloud-talk": { + ...cfg.channels?.["nextcloud-talk"], + enabled: true, + accounts: { + ...cfg.channels?.["nextcloud-talk"]?.accounts, + [accountId]: { + ...cfg.channels?.["nextcloud-talk"]?.accounts?.[accountId], + enabled: cfg.channels?.["nextcloud-talk"]?.accounts?.[accountId]?.enabled ?? true, + ...updates, + }, + }, + }, + }, + }; +} + async function noteNextcloudTalkSecretHelp(prompter: WizardPrompter): Promise { await prompter.note( [ @@ -105,40 +144,10 @@ async function promptNextcloudTalkAllowFrom(params: { ]; const unique = mergeAllowFromEntries(undefined, merged); - if (accountId === DEFAULT_ACCOUNT_ID) { - return { - ...cfg, - channels: { - ...cfg.channels, - "nextcloud-talk": { - ...cfg.channels?.["nextcloud-talk"], - enabled: true, - dmPolicy: "allowlist", - allowFrom: unique, - }, - }, - }; - } - - return { - ...cfg, - channels: { - ...cfg.channels, - "nextcloud-talk": { - ...cfg.channels?.["nextcloud-talk"], - enabled: true, - accounts: { - ...cfg.channels?.["nextcloud-talk"]?.accounts, - [accountId]: { - ...cfg.channels?.["nextcloud-talk"]?.accounts?.[accountId], - enabled: cfg.channels?.["nextcloud-talk"]?.accounts?.[accountId]?.enabled ?? true, - dmPolicy: "allowlist", - allowFrom: unique, - }, - }, - }, - }, - }; + return setNextcloudTalkAccountConfig(cfg, accountId, { + dmPolicy: "allowlist", + allowFrom: unique, + }); } async function promptNextcloudTalkAllowFromForAccount(params: { @@ -265,41 +274,10 @@ export const nextcloudTalkOnboardingAdapter: ChannelOnboardingAdapter = { } if (secretResult.action === "use-env" || secret || baseUrl !== resolvedAccount.baseUrl) { - if (accountId === DEFAULT_ACCOUNT_ID) { - next = { - ...next, - channels: { - ...next.channels, - "nextcloud-talk": { - ...next.channels?.["nextcloud-talk"], - enabled: true, - baseUrl, - ...(secret ? { botSecret: secret } : {}), - }, - }, - }; - } else { - next = { - ...next, - channels: { - ...next.channels, - "nextcloud-talk": { - ...next.channels?.["nextcloud-talk"], - enabled: true, - accounts: { - ...next.channels?.["nextcloud-talk"]?.accounts, - [accountId]: { - ...next.channels?.["nextcloud-talk"]?.accounts?.[accountId], - enabled: - next.channels?.["nextcloud-talk"]?.accounts?.[accountId]?.enabled ?? true, - baseUrl, - ...(secret ? { botSecret: secret } : {}), - }, - }, - }, - }, - }; - } + next = setNextcloudTalkAccountConfig(next, accountId, { + baseUrl, + ...(secret ? { botSecret: secret } : {}), + }); } const existingApiUser = resolvedAccount.config.apiUser?.trim(); @@ -333,41 +311,10 @@ export const nextcloudTalkOnboardingAdapter: ChannelOnboardingAdapter = { preferredEnvVar: "NEXTCLOUD_TALK_API_PASSWORD", }); const apiPassword = apiPasswordResult.action === "set" ? apiPasswordResult.value : undefined; - if (accountId === DEFAULT_ACCOUNT_ID) { - next = { - ...next, - channels: { - ...next.channels, - "nextcloud-talk": { - ...next.channels?.["nextcloud-talk"], - enabled: true, - apiUser, - ...(apiPassword ? { apiPassword } : {}), - }, - }, - }; - } else { - next = { - ...next, - channels: { - ...next.channels, - "nextcloud-talk": { - ...next.channels?.["nextcloud-talk"], - enabled: true, - accounts: { - ...next.channels?.["nextcloud-talk"]?.accounts, - [accountId]: { - ...next.channels?.["nextcloud-talk"]?.accounts?.[accountId], - enabled: - next.channels?.["nextcloud-talk"]?.accounts?.[accountId]?.enabled ?? true, - apiUser, - ...(apiPassword ? { apiPassword } : {}), - }, - }, - }, - }, - }; - } + next = setNextcloudTalkAccountConfig(next, accountId, { + apiUser, + ...(apiPassword ? { apiPassword } : {}), + }); } if (forceAllowFrom) { diff --git a/extensions/qwen-portal-auth/index.ts b/extensions/qwen-portal-auth/index.ts index c592c0e223c..643663c1ffa 100644 --- a/extensions/qwen-portal-auth/index.ts +++ b/extensions/qwen-portal-auth/index.ts @@ -1,4 +1,5 @@ import { + buildOauthProviderAuthResult, emptyPluginConfigSchema, type OpenClawPluginApi, type ProviderAuthContext, @@ -63,22 +64,14 @@ const qwenPortalPlugin = { progress.stop("Qwen OAuth complete"); - const profileId = `${PROVIDER_ID}:default`; const baseUrl = normalizeBaseUrl(result.resourceUrl); - return { - profiles: [ - { - profileId, - credential: { - type: "oauth", - provider: PROVIDER_ID, - access: result.access, - refresh: result.refresh, - expires: result.expires, - }, - }, - ], + return buildOauthProviderAuthResult({ + providerId: PROVIDER_ID, + defaultModel: DEFAULT_MODEL, + access: result.access, + refresh: result.refresh, + expires: result.expires, configPatch: { models: { providers: { @@ -110,12 +103,11 @@ const qwenPortalPlugin = { }, }, }, - defaultModel: DEFAULT_MODEL, notes: [ "Qwen OAuth tokens auto-refresh. Re-run login if refresh fails or access is revoked.", `Base URL defaults to ${DEFAULT_BASE_URL}. Override models.providers.${PROVIDER_ID}.baseUrl if needed.`, ], - }; + }); } catch (err) { progress.stop("Qwen OAuth failed"); await ctx.prompter.note( diff --git a/extensions/synology-chat/src/channel.test.ts b/extensions/synology-chat/src/channel.test.ts index 713ecf7f8c3..4e3be192f39 100644 --- a/extensions/synology-chat/src/channel.test.ts +++ b/extensions/synology-chat/src/channel.test.ts @@ -317,20 +317,11 @@ describe("createSynologyChatPlugin", () => { }); describe("gateway", () => { - it("startAccount returns pending promise for disabled account", async () => { - const plugin = createSynologyChatPlugin(); - const abortController = new AbortController(); - const ctx = { - cfg: { - channels: { "synology-chat": { enabled: false } }, - }, - accountId: "default", - log: { info: vi.fn(), warn: vi.fn(), error: vi.fn() }, - abortSignal: abortController.signal, - }; - const result = plugin.gateway.startAccount(ctx); + async function expectPendingStartAccountPromise( + result: Promise, + abortController: AbortController, + ) { expect(result).toBeInstanceOf(Promise); - // Promise should stay pending (never resolve) to prevent restart loop const resolved = await Promise.race([ result, new Promise((r) => setTimeout(() => r("pending"), 50)), @@ -338,29 +329,29 @@ describe("createSynologyChatPlugin", () => { expect(resolved).toBe("pending"); abortController.abort(); await result; + } + + async function expectPendingStartAccount(accountConfig: Record) { + const plugin = createSynologyChatPlugin(); + const abortController = new AbortController(); + const ctx = { + cfg: { + channels: { "synology-chat": accountConfig }, + }, + accountId: "default", + log: { info: vi.fn(), warn: vi.fn(), error: vi.fn() }, + abortSignal: abortController.signal, + }; + const result = plugin.gateway.startAccount(ctx); + await expectPendingStartAccountPromise(result, abortController); + } + + it("startAccount returns pending promise for disabled account", async () => { + await expectPendingStartAccount({ enabled: false }); }); it("startAccount returns pending promise for account without token", async () => { - const plugin = createSynologyChatPlugin(); - const abortController = new AbortController(); - const ctx = { - cfg: { - channels: { "synology-chat": { enabled: true } }, - }, - accountId: "default", - log: { info: vi.fn(), warn: vi.fn(), error: vi.fn() }, - abortSignal: abortController.signal, - }; - const result = plugin.gateway.startAccount(ctx); - expect(result).toBeInstanceOf(Promise); - // Promise should stay pending (never resolve) to prevent restart loop - const resolved = await Promise.race([ - result, - new Promise((r) => setTimeout(() => r("pending"), 50)), - ]); - expect(resolved).toBe("pending"); - abortController.abort(); - await result; + await expectPendingStartAccount({ enabled: true }); }); it("startAccount refuses allowlist accounts with empty allowedUserIds", async () => { @@ -387,16 +378,9 @@ describe("createSynologyChatPlugin", () => { }; const result = plugin.gateway.startAccount(ctx); - expect(result).toBeInstanceOf(Promise); - const resolved = await Promise.race([ - result, - new Promise((r) => setTimeout(() => r("pending"), 50)), - ]); - expect(resolved).toBe("pending"); + await expectPendingStartAccountPromise(result, abortController); expect(ctx.log.warn).toHaveBeenCalledWith(expect.stringContaining("empty allowedUserIds")); expect(registerMock).not.toHaveBeenCalled(); - abortController.abort(); - await result; }); it("deregisters stale route before re-registering same account/path", async () => { diff --git a/extensions/synology-chat/src/client.test.ts b/extensions/synology-chat/src/client.test.ts index ef5ff06beb7..416412f0408 100644 --- a/extensions/synology-chat/src/client.test.ts +++ b/extensions/synology-chat/src/client.test.ts @@ -118,26 +118,21 @@ describe("sendFileUrl", () => { function mockUserListResponse( users: Array<{ user_id: number; username: string; nickname: string }>, ) { - const httpsGet = vi.mocked((https as any).get); - httpsGet.mockImplementation((_url: any, _opts: any, callback: any) => { - const res = new EventEmitter() as any; - res.statusCode = 200; - process.nextTick(() => { - callback(res); - res.emit("data", Buffer.from(JSON.stringify({ success: true, data: { users } }))); - res.emit("end"); - }); - const req = new EventEmitter() as any; - req.destroy = vi.fn(); - return req; - }); + mockUserListResponseImpl(users, false); } function mockUserListResponseOnce( users: Array<{ user_id: number; username: string; nickname: string }>, +) { + mockUserListResponseImpl(users, true); +} + +function mockUserListResponseImpl( + users: Array<{ user_id: number; username: string; nickname: string }>, + once: boolean, ) { const httpsGet = vi.mocked((https as any).get); - httpsGet.mockImplementationOnce((_url: any, _opts: any, callback: any) => { + const impl = (_url: any, _opts: any, callback: any) => { const res = new EventEmitter() as any; res.statusCode = 200; process.nextTick(() => { @@ -148,7 +143,12 @@ function mockUserListResponseOnce( const req = new EventEmitter() as any; req.destroy = vi.fn(); return req; - }); + }; + if (once) { + httpsGet.mockImplementationOnce(impl); + return; + } + httpsGet.mockImplementation(impl); } describe("resolveChatUserId", () => { diff --git a/extensions/telegram/src/channel.test.ts b/extensions/telegram/src/channel.test.ts index 5f755a7284b..7473bb5e533 100644 --- a/extensions/telegram/src/channel.test.ts +++ b/extensions/telegram/src/channel.test.ts @@ -52,6 +52,25 @@ function createStartAccountCtx(params: { }; } +function installGatewayRuntime(params?: { probeOk?: boolean; botUsername?: string }) { + const monitorTelegramProvider = vi.fn(async () => undefined); + const probeTelegram = vi.fn(async () => + params?.probeOk ? { ok: true, bot: { username: params.botUsername ?? "bot" } } : { ok: false }, + ); + setTelegramRuntime({ + channel: { + telegram: { + monitorTelegramProvider, + probeTelegram, + }, + }, + logging: { + shouldLogVerbose: () => false, + }, + } as unknown as PluginRuntime); + return { monitorTelegramProvider, probeTelegram }; +} + describe("telegramPlugin duplicate token guard", () => { it("marks secondary account as not configured when token is shared", async () => { const cfg = createCfg(); @@ -84,20 +103,7 @@ describe("telegramPlugin duplicate token guard", () => { }); it("blocks startup for duplicate token accounts before polling starts", async () => { - const monitorTelegramProvider = vi.fn(async () => undefined); - const probeTelegram = vi.fn(async () => ({ ok: true, bot: { username: "bot" } })); - const runtime = { - channel: { - telegram: { - monitorTelegramProvider, - probeTelegram, - }, - }, - logging: { - shouldLogVerbose: () => false, - }, - } as unknown as PluginRuntime; - setTelegramRuntime(runtime); + const { monitorTelegramProvider, probeTelegram } = installGatewayRuntime({ probeOk: true }); await expect( telegramPlugin.gateway!.startAccount!( @@ -114,20 +120,10 @@ describe("telegramPlugin duplicate token guard", () => { }); it("passes webhookPort through to monitor startup options", async () => { - const monitorTelegramProvider = vi.fn(async () => undefined); - const probeTelegram = vi.fn(async () => ({ ok: true, bot: { username: "opsbot" } })); - const runtime = { - channel: { - telegram: { - monitorTelegramProvider, - probeTelegram, - }, - }, - logging: { - shouldLogVerbose: () => false, - }, - } as unknown as PluginRuntime; - setTelegramRuntime(runtime); + const { monitorTelegramProvider } = installGatewayRuntime({ + probeOk: true, + botUsername: "opsbot", + }); const cfg = createCfg(); cfg.channels!.telegram!.accounts!.ops = { @@ -192,20 +188,7 @@ describe("telegramPlugin duplicate token guard", () => { }); it("does not crash startup when a resolved account token is undefined", async () => { - const monitorTelegramProvider = vi.fn(async () => undefined); - const probeTelegram = vi.fn(async () => ({ ok: false })); - const runtime = { - channel: { - telegram: { - monitorTelegramProvider, - probeTelegram, - }, - }, - logging: { - shouldLogVerbose: () => false, - }, - } as unknown as PluginRuntime; - setTelegramRuntime(runtime); + const { monitorTelegramProvider } = installGatewayRuntime({ probeOk: false }); const cfg = createCfg(); const ctx = createStartAccountCtx({ diff --git a/extensions/telegram/src/channel.ts b/extensions/telegram/src/channel.ts index f7c2ad16328..ccb22dab55b 100644 --- a/extensions/telegram/src/channel.ts +++ b/extensions/telegram/src/channel.ts @@ -2,6 +2,7 @@ import { applyAccountNameToChannelSection, buildChannelConfigSchema, buildTokenChannelStatusSummary, + clearAccountEntryFields, collectTelegramStatusIssues, DEFAULT_ACCOUNT_ID, deleteAccountFromConfigSection, @@ -519,36 +520,20 @@ export const telegramPlugin: ChannelPlugin; - if ("botToken" in nextEntry) { - const token = nextEntry.botToken; - if (typeof token === "string" ? token.trim() : token) { - cleared = true; - } - delete nextEntry.botToken; - changed = true; - } - if (Object.keys(nextEntry).length === 0) { - delete accounts[accountId]; - changed = true; - } else { - accounts[accountId] = nextEntry as typeof entry; - } + const accountCleanup = clearAccountEntryFields({ + accounts: nextTelegram.accounts, + accountId, + fields: ["botToken"], + }); + if (accountCleanup.changed) { + changed = true; + if (accountCleanup.cleared) { + cleared = true; } - } - if (accounts) { - if (Object.keys(accounts).length === 0) { - delete nextTelegram.accounts; - changed = true; + if (accountCleanup.nextAccounts) { + nextTelegram.accounts = accountCleanup.nextAccounts; } else { - nextTelegram.accounts = accounts; + delete nextTelegram.accounts; } } } diff --git a/extensions/twitch/src/access-control.test.ts b/extensions/twitch/src/access-control.test.ts index 83746717e4a..874326c9697 100644 --- a/extensions/twitch/src/access-control.test.ts +++ b/extensions/twitch/src/access-control.test.ts @@ -51,14 +51,10 @@ describe("checkTwitchAccessControl", () => { describe("when no restrictions are configured", () => { it("allows messages that mention the bot (default requireMention)", () => { - const message: TwitchChatMessage = { - ...mockMessage, - message: "@testbot hello", - }; - const result = checkTwitchAccessControl({ - message, - account: mockAccount, - botUsername: "testbot", + const result = runAccessCheck({ + message: { + message: "@testbot hello", + }, }); expect(result.allowed).toBe(true); }); @@ -66,30 +62,20 @@ describe("checkTwitchAccessControl", () => { describe("requireMention default", () => { it("defaults to true when undefined", () => { - const message: TwitchChatMessage = { - ...mockMessage, - message: "hello bot", - }; - - const result = checkTwitchAccessControl({ - message, - account: mockAccount, - botUsername: "testbot", + const result = runAccessCheck({ + message: { + message: "hello bot", + }, }); expect(result.allowed).toBe(false); expect(result.reason).toContain("does not mention the bot"); }); it("allows mention when requireMention is undefined", () => { - const message: TwitchChatMessage = { - ...mockMessage, - message: "@testbot hello", - }; - - const result = checkTwitchAccessControl({ - message, - account: mockAccount, - botUsername: "testbot", + const result = runAccessCheck({ + message: { + message: "@testbot hello", + }, }); expect(result.allowed).toBe(true); }); @@ -97,52 +83,25 @@ describe("checkTwitchAccessControl", () => { describe("requireMention", () => { it("allows messages that mention the bot", () => { - const account: TwitchAccountConfig = { - ...mockAccount, - requireMention: true, - }; - const message: TwitchChatMessage = { - ...mockMessage, - message: "@testbot hello", - }; - - const result = checkTwitchAccessControl({ - message, - account, - botUsername: "testbot", + const result = runAccessCheck({ + account: { requireMention: true }, + message: { message: "@testbot hello" }, }); expect(result.allowed).toBe(true); }); it("blocks messages that don't mention the bot", () => { - const account: TwitchAccountConfig = { - ...mockAccount, - requireMention: true, - }; - - const result = checkTwitchAccessControl({ - message: mockMessage, - account, - botUsername: "testbot", + const result = runAccessCheck({ + account: { requireMention: true }, }); expect(result.allowed).toBe(false); expect(result.reason).toContain("does not mention the bot"); }); it("is case-insensitive for bot username", () => { - const account: TwitchAccountConfig = { - ...mockAccount, - requireMention: true, - }; - const message: TwitchChatMessage = { - ...mockMessage, - message: "@TestBot hello", - }; - - const result = checkTwitchAccessControl({ - message, - account, - botUsername: "testbot", + const result = runAccessCheck({ + account: { requireMention: true }, + message: { message: "@TestBot hello" }, }); expect(result.allowed).toBe(true); }); diff --git a/extensions/twitch/src/status.test.ts b/extensions/twitch/src/status.test.ts index 7aa8b909df3..d0340ec852e 100644 --- a/extensions/twitch/src/status.test.ts +++ b/extensions/twitch/src/status.test.ts @@ -14,17 +14,28 @@ import { describe, expect, it } from "vitest"; import { collectTwitchStatusIssues } from "./status.js"; import type { ChannelAccountSnapshot } from "./types.js"; +function createSnapshot(overrides: Partial = {}): ChannelAccountSnapshot { + return { + accountId: "default", + configured: true, + enabled: true, + running: false, + ...overrides, + }; +} + +function createSimpleTwitchConfig(overrides: Record) { + return { + channels: { + twitch: overrides, + }, + }; +} + describe("status", () => { describe("collectTwitchStatusIssues", () => { it("should detect unconfigured accounts", () => { - const snapshots: ChannelAccountSnapshot[] = [ - { - accountId: "default", - configured: false, - enabled: true, - running: false, - }, - ]; + const snapshots: ChannelAccountSnapshot[] = [createSnapshot({ configured: false })]; const issues = collectTwitchStatusIssues(snapshots); @@ -34,14 +45,7 @@ describe("status", () => { }); it("should detect disabled accounts", () => { - const snapshots: ChannelAccountSnapshot[] = [ - { - accountId: "default", - configured: true, - enabled: false, - running: false, - }, - ]; + const snapshots: ChannelAccountSnapshot[] = [createSnapshot({ enabled: false })]; const issues = collectTwitchStatusIssues(snapshots); @@ -51,24 +55,12 @@ describe("status", () => { }); it("should detect missing clientId when account configured (simplified config)", () => { - const snapshots: ChannelAccountSnapshot[] = [ - { - accountId: "default", - configured: true, - enabled: true, - running: false, - }, - ]; - - const mockCfg = { - channels: { - twitch: { - username: "testbot", - accessToken: "oauth:test123", - // clientId missing - }, - }, - }; + const snapshots: ChannelAccountSnapshot[] = [createSnapshot()]; + const mockCfg = createSimpleTwitchConfig({ + username: "testbot", + accessToken: "oauth:test123", + // clientId missing + }); const issues = collectTwitchStatusIssues(snapshots, () => mockCfg as never); @@ -77,24 +69,12 @@ describe("status", () => { }); it("should warn about oauth: prefix in token (simplified config)", () => { - const snapshots: ChannelAccountSnapshot[] = [ - { - accountId: "default", - configured: true, - enabled: true, - running: false, - }, - ]; - - const mockCfg = { - channels: { - twitch: { - username: "testbot", - accessToken: "oauth:test123", // has prefix - clientId: "test-id", - }, - }, - }; + const snapshots: ChannelAccountSnapshot[] = [createSnapshot()]; + const mockCfg = createSimpleTwitchConfig({ + username: "testbot", + accessToken: "oauth:test123", // has prefix + clientId: "test-id", + }); const issues = collectTwitchStatusIssues(snapshots, () => mockCfg as never); @@ -104,26 +84,14 @@ describe("status", () => { }); it("should detect clientSecret without refreshToken (simplified config)", () => { - const snapshots: ChannelAccountSnapshot[] = [ - { - accountId: "default", - configured: true, - enabled: true, - running: false, - }, - ]; - - const mockCfg = { - channels: { - twitch: { - username: "testbot", - accessToken: "oauth:test123", - clientId: "test-id", - clientSecret: "secret123", - // refreshToken missing - }, - }, - }; + const snapshots: ChannelAccountSnapshot[] = [createSnapshot()]; + const mockCfg = createSimpleTwitchConfig({ + username: "testbot", + accessToken: "oauth:test123", + clientId: "test-id", + clientSecret: "secret123", + // refreshToken missing + }); const issues = collectTwitchStatusIssues(snapshots, () => mockCfg as never); @@ -132,25 +100,13 @@ describe("status", () => { }); it("should detect empty allowFrom array (simplified config)", () => { - const snapshots: ChannelAccountSnapshot[] = [ - { - accountId: "default", - configured: true, - enabled: true, - running: false, - }, - ]; - - const mockCfg = { - channels: { - twitch: { - username: "testbot", - accessToken: "test123", - clientId: "test-id", - allowFrom: [], // empty array - }, - }, - }; + const snapshots: ChannelAccountSnapshot[] = [createSnapshot()]; + const mockCfg = createSimpleTwitchConfig({ + username: "testbot", + accessToken: "test123", + clientId: "test-id", + allowFrom: [], // empty array + }); const issues = collectTwitchStatusIssues(snapshots, () => mockCfg as never); @@ -159,26 +115,14 @@ describe("status", () => { }); it("should detect allowedRoles 'all' with allowFrom conflict (simplified config)", () => { - const snapshots: ChannelAccountSnapshot[] = [ - { - accountId: "default", - configured: true, - enabled: true, - running: false, - }, - ]; - - const mockCfg = { - channels: { - twitch: { - username: "testbot", - accessToken: "test123", - clientId: "test-id", - allowedRoles: ["all"], - allowFrom: ["123456"], // conflict! - }, - }, - }; + const snapshots: ChannelAccountSnapshot[] = [createSnapshot()]; + const mockCfg = createSimpleTwitchConfig({ + username: "testbot", + accessToken: "test123", + clientId: "test-id", + allowedRoles: ["all"], + allowFrom: ["123456"], // conflict! + }); const issues = collectTwitchStatusIssues(snapshots, () => mockCfg as never); @@ -189,13 +133,7 @@ describe("status", () => { it("should detect runtime errors", () => { const snapshots: ChannelAccountSnapshot[] = [ - { - accountId: "default", - configured: true, - enabled: true, - running: false, - lastError: "Connection timeout", - }, + createSnapshot({ lastError: "Connection timeout" }), ]; const issues = collectTwitchStatusIssues(snapshots); @@ -207,15 +145,11 @@ describe("status", () => { it("should detect accounts that never connected", () => { const snapshots: ChannelAccountSnapshot[] = [ - { - accountId: "default", - configured: true, - enabled: true, - running: false, + createSnapshot({ lastStartAt: undefined, lastInboundAt: undefined, lastOutboundAt: undefined, - }, + }), ]; const issues = collectTwitchStatusIssues(snapshots); @@ -230,13 +164,10 @@ describe("status", () => { const oldDate = Date.now() - 8 * 24 * 60 * 60 * 1000; // 8 days ago const snapshots: ChannelAccountSnapshot[] = [ - { - accountId: "default", - configured: true, - enabled: true, + createSnapshot({ running: true, lastStartAt: oldDate, - }, + }), ]; const issues = collectTwitchStatusIssues(snapshots); diff --git a/extensions/voice-call/index.ts b/extensions/voice-call/index.ts index c4b543b232a..8e2fba9898f 100644 --- a/extensions/voice-call/index.ts +++ b/extensions/voice-call/index.ts @@ -209,6 +209,23 @@ const voiceCallPlugin = { const rt = await ensureRuntime(); return { rt, callId, message } as const; }; + const initiateCallAndRespond = async (params: { + rt: VoiceCallRuntime; + respond: GatewayRequestHandlerOptions["respond"]; + to: string; + message?: string; + mode?: "notify" | "conversation"; + }) => { + const result = await params.rt.manager.initiateCall(params.to, undefined, { + message: params.message, + mode: params.mode, + }); + if (!result.success) { + params.respond(false, { error: result.error || "initiate failed" }); + return; + } + params.respond(true, { callId: result.callId, initiated: true }); + }; api.registerGatewayMethod( "voicecall.initiate", @@ -230,15 +247,13 @@ const voiceCallPlugin = { } const mode = params?.mode === "notify" || params?.mode === "conversation" ? params.mode : undefined; - const result = await rt.manager.initiateCall(to, undefined, { + await initiateCallAndRespond({ + rt, + respond, + to, message, mode, }); - if (!result.success) { - respond(false, { error: result.error || "initiate failed" }); - return; - } - respond(true, { callId: result.callId, initiated: true }); } catch (err) { sendError(respond, err); } @@ -347,14 +362,12 @@ const voiceCallPlugin = { return; } const rt = await ensureRuntime(); - const result = await rt.manager.initiateCall(to, undefined, { + await initiateCallAndRespond({ + rt, + respond, + to, message: message || undefined, }); - if (!result.success) { - respond(false, { error: result.error || "initiate failed" }); - return; - } - respond(true, { callId: result.callId, initiated: true }); } catch (err) { sendError(respond, err); } diff --git a/extensions/voice-call/src/config.test.ts b/extensions/voice-call/src/config.test.ts index ba1889edb4f..03cc011fc66 100644 --- a/extensions/voice-call/src/config.test.ts +++ b/extensions/voice-call/src/config.test.ts @@ -1,49 +1,9 @@ import { afterEach, beforeEach, describe, expect, it } from "vitest"; import { validateProviderConfig, resolveVoiceCallConfig, type VoiceCallConfig } from "./config.js"; +import { createVoiceCallBaseConfig } from "./test-fixtures.js"; function createBaseConfig(provider: "telnyx" | "twilio" | "plivo" | "mock"): VoiceCallConfig { - return { - enabled: true, - provider, - fromNumber: "+15550001234", - inboundPolicy: "disabled", - allowFrom: [], - outbound: { defaultMode: "notify", notifyHangupDelaySec: 3 }, - maxDurationSeconds: 300, - staleCallReaperSeconds: 600, - silenceTimeoutMs: 800, - transcriptTimeoutMs: 180000, - ringTimeoutMs: 30000, - maxConcurrentCalls: 1, - serve: { port: 3334, bind: "127.0.0.1", path: "/voice/webhook" }, - tailscale: { mode: "off", path: "/voice/webhook" }, - tunnel: { provider: "none", allowNgrokFreeTierLoopbackBypass: false }, - webhookSecurity: { - allowedHosts: [], - trustForwardingHeaders: false, - trustedProxyIPs: [], - }, - streaming: { - enabled: false, - sttProvider: "openai-realtime", - sttModel: "gpt-4o-transcribe", - silenceDurationMs: 800, - vadThreshold: 0.5, - streamPath: "/voice/stream", - preStartTimeoutMs: 5000, - maxPendingConnections: 32, - maxPendingConnectionsPerIp: 4, - maxConnections: 128, - }, - skipSignatureVerification: false, - stt: { provider: "openai", model: "whisper-1" }, - tts: { - provider: "openai", - openai: { model: "gpt-4o-mini-tts", voice: "coral" }, - }, - responseModel: "openai/gpt-4o-mini", - responseTimeoutMs: 30000, - }; + return createVoiceCallBaseConfig({ provider }); } describe("validateProviderConfig", () => { diff --git a/extensions/voice-call/src/providers/tts-openai.ts b/extensions/voice-call/src/providers/tts-openai.ts index c483d681990..d1c95420392 100644 --- a/extensions/voice-call/src/providers/tts-openai.ts +++ b/extensions/voice-call/src/providers/tts-openai.ts @@ -1,3 +1,5 @@ +import { pcmToMulaw } from "../telephony-audio.js"; + /** * OpenAI TTS Provider * @@ -179,55 +181,6 @@ function clamp16(value: number): number { return Math.max(-32768, Math.min(32767, value)); } -/** - * Convert 16-bit PCM to 8-bit mu-law. - * Standard G.711 mu-law encoding for telephony. - */ -function pcmToMulaw(pcm: Buffer): Buffer { - const samples = pcm.length / 2; - const mulaw = Buffer.alloc(samples); - - for (let i = 0; i < samples; i++) { - const sample = pcm.readInt16LE(i * 2); - mulaw[i] = linearToMulaw(sample); - } - - return mulaw; -} - -/** - * Convert a single 16-bit linear sample to 8-bit mu-law. - * Implements ITU-T G.711 mu-law encoding. - */ -function linearToMulaw(sample: number): number { - const BIAS = 132; - const CLIP = 32635; - - // Get sign bit - const sign = sample < 0 ? 0x80 : 0; - if (sample < 0) { - sample = -sample; - } - - // Clip to prevent overflow - if (sample > CLIP) { - sample = CLIP; - } - - // Add bias and find segment - sample += BIAS; - let exponent = 7; - for (let expMask = 0x4000; (sample & expMask) === 0 && exponent > 0; exponent--, expMask >>= 1) { - // Find the segment (exponent) - } - - // Extract mantissa bits - const mantissa = (sample >> (exponent + 3)) & 0x0f; - - // Combine into mu-law byte (inverted for transmission) - return ~(sign | (exponent << 4) | mantissa) & 0xff; -} - /** * Convert 8-bit mu-law to 16-bit linear PCM. * Useful for decoding incoming audio. diff --git a/extensions/voice-call/src/runtime.test.ts b/extensions/voice-call/src/runtime.test.ts index 26cdbea82cc..dcb8fa2a158 100644 --- a/extensions/voice-call/src/runtime.test.ts +++ b/extensions/voice-call/src/runtime.test.ts @@ -1,6 +1,7 @@ import { beforeEach, describe, expect, it, vi } from "vitest"; import type { VoiceCallConfig } from "./config.js"; import type { CoreConfig } from "./core-bridge.js"; +import { createVoiceCallBaseConfig } from "./test-fixtures.js"; const mocks = vi.hoisted(() => ({ resolveVoiceCallConfig: vi.fn(), @@ -45,48 +46,7 @@ vi.mock("./webhook/tailscale.js", () => ({ import { createVoiceCallRuntime } from "./runtime.js"; function createBaseConfig(): VoiceCallConfig { - return { - enabled: true, - provider: "mock", - fromNumber: "+15550001234", - inboundPolicy: "disabled", - allowFrom: [], - outbound: { defaultMode: "notify", notifyHangupDelaySec: 3 }, - maxDurationSeconds: 300, - staleCallReaperSeconds: 600, - silenceTimeoutMs: 800, - transcriptTimeoutMs: 180000, - ringTimeoutMs: 30000, - maxConcurrentCalls: 1, - serve: { port: 3334, bind: "127.0.0.1", path: "/voice/webhook" }, - tailscale: { mode: "off", path: "/voice/webhook" }, - tunnel: { provider: "ngrok", allowNgrokFreeTierLoopbackBypass: false }, - webhookSecurity: { - allowedHosts: [], - trustForwardingHeaders: false, - trustedProxyIPs: [], - }, - streaming: { - enabled: false, - sttProvider: "openai-realtime", - sttModel: "gpt-4o-transcribe", - silenceDurationMs: 800, - vadThreshold: 0.5, - streamPath: "/voice/stream", - preStartTimeoutMs: 5000, - maxPendingConnections: 32, - maxPendingConnectionsPerIp: 4, - maxConnections: 128, - }, - skipSignatureVerification: false, - stt: { provider: "openai", model: "whisper-1" }, - tts: { - provider: "openai", - openai: { model: "gpt-4o-mini-tts", voice: "coral" }, - }, - responseModel: "openai/gpt-4o-mini", - responseTimeoutMs: 30000, - }; + return createVoiceCallBaseConfig({ tunnelProvider: "ngrok" }); } describe("createVoiceCallRuntime lifecycle", () => { diff --git a/extensions/voice-call/src/test-fixtures.ts b/extensions/voice-call/src/test-fixtures.ts new file mode 100644 index 00000000000..594aa064ba5 --- /dev/null +++ b/extensions/voice-call/src/test-fixtures.ts @@ -0,0 +1,52 @@ +import type { VoiceCallConfig } from "./config.js"; + +export function createVoiceCallBaseConfig(params?: { + provider?: "telnyx" | "twilio" | "plivo" | "mock"; + tunnelProvider?: "none" | "ngrok"; +}): VoiceCallConfig { + return { + enabled: true, + provider: params?.provider ?? "mock", + fromNumber: "+15550001234", + inboundPolicy: "disabled", + allowFrom: [], + outbound: { defaultMode: "notify", notifyHangupDelaySec: 3 }, + maxDurationSeconds: 300, + staleCallReaperSeconds: 600, + silenceTimeoutMs: 800, + transcriptTimeoutMs: 180000, + ringTimeoutMs: 30000, + maxConcurrentCalls: 1, + serve: { port: 3334, bind: "127.0.0.1", path: "/voice/webhook" }, + tailscale: { mode: "off", path: "/voice/webhook" }, + tunnel: { + provider: params?.tunnelProvider ?? "none", + allowNgrokFreeTierLoopbackBypass: false, + }, + webhookSecurity: { + allowedHosts: [], + trustForwardingHeaders: false, + trustedProxyIPs: [], + }, + streaming: { + enabled: false, + sttProvider: "openai-realtime", + sttModel: "gpt-4o-transcribe", + silenceDurationMs: 800, + vadThreshold: 0.5, + streamPath: "/voice/stream", + preStartTimeoutMs: 5000, + maxPendingConnections: 32, + maxPendingConnectionsPerIp: 4, + maxConnections: 128, + }, + skipSignatureVerification: false, + stt: { provider: "openai", model: "whisper-1" }, + tts: { + provider: "openai", + openai: { model: "gpt-4o-mini-tts", voice: "coral" }, + }, + responseModel: "openai/gpt-4o-mini", + responseTimeoutMs: 30000, + }; +} diff --git a/extensions/zalo/src/channel.ts b/extensions/zalo/src/channel.ts index a3233ce5228..b6a7f7d0486 100644 --- a/extensions/zalo/src/channel.ts +++ b/extensions/zalo/src/channel.ts @@ -6,8 +6,10 @@ import type { } from "openclaw/plugin-sdk/zalo"; import { applyAccountNameToChannelSection, + buildBaseAccountStatusSnapshot, buildChannelConfigSchema, buildTokenChannelStatusSummary, + buildChannelSendResult, DEFAULT_ACCOUNT_ID, deleteAccountFromConfigSection, chunkTextForOutbound, @@ -15,10 +17,13 @@ import { formatPairingApproveHint, migrateBaseNameToDefaultAccount, normalizeAccountId, + isNumericTargetId, PAIRING_APPROVED_MESSAGE, + resolveOutboundMediaUrls, resolveDefaultGroupPolicy, resolveOpenProviderRuntimeGroupPolicy, resolveChannelAccountConfigBasePath, + sendPayloadWithChunkedTextAndMedia, setAccountEnabledInConfigSection, } from "openclaw/plugin-sdk/zalo"; import { @@ -182,13 +187,7 @@ export const zaloPlugin: ChannelPlugin = { messaging: { normalizeTarget: normalizeZaloMessagingTarget, targetResolver: { - looksLikeId: (raw) => { - const trimmed = raw.trim(); - if (!trimmed) { - return false; - } - return /^\d{3,}$/.test(trimmed); - }, + looksLikeId: isNumericTargetId, hint: "", }, }, @@ -303,51 +302,21 @@ export const zaloPlugin: ChannelPlugin = { chunker: chunkTextForOutbound, chunkerMode: "text", textChunkLimit: 2000, - sendPayload: async (ctx) => { - const text = ctx.payload.text ?? ""; - const urls = ctx.payload.mediaUrls?.length - ? ctx.payload.mediaUrls - : ctx.payload.mediaUrl - ? [ctx.payload.mediaUrl] - : []; - if (!text && urls.length === 0) { - return { channel: "zalo", messageId: "" }; - } - if (urls.length > 0) { - let lastResult = await zaloPlugin.outbound!.sendMedia!({ - ...ctx, - text, - mediaUrl: urls[0], - }); - for (let i = 1; i < urls.length; i++) { - lastResult = await zaloPlugin.outbound!.sendMedia!({ - ...ctx, - text: "", - mediaUrl: urls[i], - }); - } - return lastResult; - } - const outbound = zaloPlugin.outbound!; - const limit = outbound.textChunkLimit; - const chunks = limit && outbound.chunker ? outbound.chunker(text, limit) : [text]; - let lastResult: Awaited>>; - for (const chunk of chunks) { - lastResult = await outbound.sendText!({ ...ctx, text: chunk }); - } - return lastResult!; - }, + sendPayload: async (ctx) => + await sendPayloadWithChunkedTextAndMedia({ + ctx, + textChunkLimit: zaloPlugin.outbound!.textChunkLimit, + chunker: zaloPlugin.outbound!.chunker, + sendText: (nextCtx) => zaloPlugin.outbound!.sendText!(nextCtx), + sendMedia: (nextCtx) => zaloPlugin.outbound!.sendMedia!(nextCtx), + emptyResult: { channel: "zalo", messageId: "" }, + }), sendText: async ({ to, text, accountId, cfg }) => { const result = await sendMessageZalo(to, text, { accountId: accountId ?? undefined, cfg: cfg, }); - return { - channel: "zalo", - ok: result.ok, - messageId: result.messageId ?? "", - error: result.error ? new Error(result.error) : undefined, - }; + return buildChannelSendResult("zalo", result); }, sendMedia: async ({ to, text, mediaUrl, accountId, cfg }) => { const result = await sendMessageZalo(to, text, { @@ -355,12 +324,7 @@ export const zaloPlugin: ChannelPlugin = { mediaUrl, cfg: cfg, }); - return { - channel: "zalo", - ok: result.ok, - messageId: result.messageId ?? "", - error: result.error ? new Error(result.error) : undefined, - }; + return buildChannelSendResult("zalo", result); }, }, status: { @@ -377,19 +341,19 @@ export const zaloPlugin: ChannelPlugin = { probeZalo(account.token, timeoutMs, resolveZaloProxyFetch(account.config.proxy)), buildAccountSnapshot: ({ account, runtime }) => { const configured = Boolean(account.token?.trim()); + const base = buildBaseAccountStatusSnapshot({ + account: { + accountId: account.accountId, + name: account.name, + enabled: account.enabled, + configured, + }, + runtime, + }); return { - accountId: account.accountId, - name: account.name, - enabled: account.enabled, - configured, + ...base, tokenSource: account.tokenSource, - running: runtime?.running ?? false, - lastStartAt: runtime?.lastStartAt ?? null, - lastStopAt: runtime?.lastStopAt ?? null, - lastError: runtime?.lastError ?? null, mode: account.config.webhookUrl ? "webhook" : "polling", - lastInboundAt: runtime?.lastInboundAt ?? null, - lastOutboundAt: runtime?.lastOutboundAt ?? null, dmPolicy: account.config.dmPolicy ?? "pairing", }; }, diff --git a/extensions/zalo/src/monitor.webhook.test.ts b/extensions/zalo/src/monitor.webhook.test.ts index 8cdecd0560c..6ae1a954255 100644 --- a/extensions/zalo/src/monitor.webhook.test.ts +++ b/extensions/zalo/src/monitor.webhook.test.ts @@ -94,6 +94,33 @@ function createPairingAuthCore(params?: { storeAllowFrom?: string[]; pairingCrea return { core, readAllowFromStore, upsertPairingRequest }; } +async function postUntilRateLimited(params: { + baseUrl: string; + path: string; + secret: string; + withNonceQuery?: boolean; + attempts?: number; +}): Promise { + const attempts = params.attempts ?? 130; + for (let i = 0; i < attempts; i += 1) { + const url = params.withNonceQuery + ? `${params.baseUrl}${params.path}?nonce=${i}` + : `${params.baseUrl}${params.path}`; + const response = await fetch(url, { + method: "POST", + headers: { + "x-bot-api-secret-token": params.secret, + "content-type": "application/json", + }, + body: "{}", + }); + if (response.status === 429) { + return true; + } + } + return false; +} + describe("handleZaloWebhookRequest", () => { afterEach(() => { clearZaloWebhookSecurityStateForTest(); @@ -239,21 +266,11 @@ describe("handleZaloWebhookRequest", () => { try { await withServer(webhookRequestHandler, async (baseUrl) => { - let saw429 = false; - for (let i = 0; i < 130; i += 1) { - const response = await fetch(`${baseUrl}/hook-rate`, { - method: "POST", - headers: { - "x-bot-api-secret-token": "secret", - "content-type": "application/json", - }, - body: "{}", - }); - if (response.status === 429) { - saw429 = true; - break; - } - } + const saw429 = await postUntilRateLimited({ + baseUrl, + path: "/hook-rate", + secret: "secret", + }); expect(saw429).toBe(true); }); @@ -290,21 +307,12 @@ describe("handleZaloWebhookRequest", () => { try { await withServer(webhookRequestHandler, async (baseUrl) => { - let saw429 = false; - for (let i = 0; i < 130; i += 1) { - const response = await fetch(`${baseUrl}/hook-query-rate?nonce=${i}`, { - method: "POST", - headers: { - "x-bot-api-secret-token": "secret", - "content-type": "application/json", - }, - body: "{}", - }); - if (response.status === 429) { - saw429 = true; - break; - } - } + const saw429 = await postUntilRateLimited({ + baseUrl, + path: "/hook-query-rate", + secret: "secret", + withNonceQuery: true, + }); expect(saw429).toBe(true); expect(getZaloWebhookRateLimitStateSizeForTest()).toBe(1); diff --git a/extensions/zalo/src/send.ts b/extensions/zalo/src/send.ts index c58142f8633..44f1549067a 100644 --- a/extensions/zalo/src/send.ts +++ b/extensions/zalo/src/send.ts @@ -40,37 +40,47 @@ function resolveSendContext(options: ZaloSendOptions): { return { token, fetcher: resolveZaloProxyFetch(proxy) }; } +function resolveValidatedSendContext( + chatId: string, + options: ZaloSendOptions, +): { ok: true; chatId: string; token: string; fetcher?: ZaloFetch } | { ok: false; error: string } { + const { token, fetcher } = resolveSendContext(options); + if (!token) { + return { ok: false, error: "No Zalo bot token configured" }; + } + const trimmedChatId = chatId?.trim(); + if (!trimmedChatId) { + return { ok: false, error: "No chat_id provided" }; + } + return { ok: true, chatId: trimmedChatId, token, fetcher }; +} + export async function sendMessageZalo( chatId: string, text: string, options: ZaloSendOptions = {}, ): Promise { - const { token, fetcher } = resolveSendContext(options); - - if (!token) { - return { ok: false, error: "No Zalo bot token configured" }; - } - - if (!chatId?.trim()) { - return { ok: false, error: "No chat_id provided" }; + const context = resolveValidatedSendContext(chatId, options); + if (!context.ok) { + return { ok: false, error: context.error }; } if (options.mediaUrl) { - return sendPhotoZalo(chatId, options.mediaUrl, { + return sendPhotoZalo(context.chatId, options.mediaUrl, { ...options, - token, + token: context.token, caption: text || options.caption, }); } try { const response = await sendMessage( - token, + context.token, { - chat_id: chatId.trim(), + chat_id: context.chatId, text: text.slice(0, 2000), }, - fetcher, + context.fetcher, ); if (response.ok && response.result) { @@ -88,14 +98,9 @@ export async function sendPhotoZalo( photoUrl: string, options: ZaloSendOptions = {}, ): Promise { - const { token, fetcher } = resolveSendContext(options); - - if (!token) { - return { ok: false, error: "No Zalo bot token configured" }; - } - - if (!chatId?.trim()) { - return { ok: false, error: "No chat_id provided" }; + const context = resolveValidatedSendContext(chatId, options); + if (!context.ok) { + return { ok: false, error: context.error }; } if (!photoUrl?.trim()) { @@ -104,13 +109,13 @@ export async function sendPhotoZalo( try { const response = await sendPhoto( - token, + context.token, { - chat_id: chatId.trim(), + chat_id: context.chatId, photo: photoUrl.trim(), caption: options.caption?.slice(0, 2000), }, - fetcher, + context.fetcher, ); if (response.ok && response.result) { diff --git a/extensions/zalo/src/token.ts b/extensions/zalo/src/token.ts index 2d9496fa5c2..00ed1d720f7 100644 --- a/extensions/zalo/src/token.ts +++ b/extensions/zalo/src/token.ts @@ -8,6 +8,19 @@ export type ZaloTokenResolution = BaseTokenResolution & { source: "env" | "config" | "configFile" | "none"; }; +function readTokenFromFile(tokenFile: string | undefined): string { + const trimmedPath = tokenFile?.trim(); + if (!trimmedPath) { + return ""; + } + try { + return readFileSync(trimmedPath, "utf8").trim(); + } catch { + // ignore read failures + return ""; + } +} + export function resolveZaloToken( config: ZaloConfig | undefined, accountId?: string | null, @@ -44,28 +57,16 @@ export function resolveZaloToken( if (token) { return { token, source: "config" }; } - const tokenFile = accountConfig.tokenFile?.trim(); - if (tokenFile) { - try { - const fileToken = readFileSync(tokenFile, "utf8").trim(); - if (fileToken) { - return { token: fileToken, source: "configFile" }; - } - } catch { - // ignore read failures - } + const fileToken = readTokenFromFile(accountConfig.tokenFile); + if (fileToken) { + return { token: fileToken, source: "configFile" }; } } - const accountTokenFile = accountConfig?.tokenFile?.trim(); - if (!accountHasBotToken && accountTokenFile) { - try { - const fileToken = readFileSync(accountTokenFile, "utf8").trim(); - if (fileToken) { - return { token: fileToken, source: "configFile" }; - } - } catch { - // ignore read failures + if (!accountHasBotToken) { + const fileToken = readTokenFromFile(accountConfig?.tokenFile); + if (fileToken) { + return { token: fileToken, source: "configFile" }; } } @@ -79,16 +80,9 @@ export function resolveZaloToken( if (token) { return { token, source: "config" }; } - const tokenFile = baseConfig?.tokenFile?.trim(); - if (tokenFile) { - try { - const fileToken = readFileSync(tokenFile, "utf8").trim(); - if (fileToken) { - return { token: fileToken, source: "configFile" }; - } - } catch { - // ignore read failures - } + const fileToken = readTokenFromFile(baseConfig?.tokenFile); + if (fileToken) { + return { token: fileToken, source: "configFile" }; } } diff --git a/extensions/zalouser/src/channel.ts b/extensions/zalouser/src/channel.ts index 2c2228b05b9..41327f1fe7e 100644 --- a/extensions/zalouser/src/channel.ts +++ b/extensions/zalouser/src/channel.ts @@ -1,5 +1,3 @@ -import fsp from "node:fs/promises"; -import path from "node:path"; import type { ChannelAccountSnapshot, ChannelDirectoryEntry, @@ -12,16 +10,19 @@ import type { } from "openclaw/plugin-sdk/zalouser"; import { applyAccountNameToChannelSection, + buildChannelSendResult, + buildBaseAccountStatusSnapshot, buildChannelConfigSchema, DEFAULT_ACCOUNT_ID, chunkTextForOutbound, deleteAccountFromConfigSection, formatAllowFromLowercase, formatPairingApproveHint, + isNumericTargetId, migrateBaseNameToDefaultAccount, normalizeAccountId, - resolvePreferredOpenClawTmpDir, resolveChannelAccountConfigBasePath, + sendPayloadWithChunkedTextAndMedia, setAccountEnabledInConfigSection, } from "openclaw/plugin-sdk/zalouser"; import { @@ -37,6 +38,7 @@ import { buildZalouserGroupCandidates, findZalouserGroupEntry } from "./group-po import { resolveZalouserReactionMessageIds } from "./message-sid.js"; import { zalouserOnboardingAdapter } from "./onboarding.js"; import { probeZalouser } from "./probe.js"; +import { writeQrDataUrlToTempFile } from "./qr-temp-file.js"; import { sendMessageZalouser, sendReactionZalouser } from "./send.js"; import { collectZalouserStatusIssues } from "./status-issues.js"; import { @@ -69,25 +71,6 @@ function resolveZalouserQrProfile(accountId?: string | null): string { return normalized; } -async function writeQrDataUrlToTempFile( - qrDataUrl: string, - profile: string, -): Promise { - const trimmed = qrDataUrl.trim(); - const match = trimmed.match(/^data:image\/png;base64,(.+)$/i); - const base64 = (match?.[1] ?? "").trim(); - if (!base64) { - return null; - } - const safeProfile = profile.replace(/[^a-zA-Z0-9_-]+/g, "-") || "default"; - const filePath = path.join( - resolvePreferredOpenClawTmpDir(), - `openclaw-zalouser-qr-${safeProfile}.png`, - ); - await fsp.writeFile(filePath, Buffer.from(base64, "base64")); - return filePath; -} - function mapUser(params: { id: string; name?: string | null; @@ -116,39 +99,30 @@ function mapGroup(params: { }; } +function resolveZalouserGroupPolicyEntry(params: ChannelGroupContext) { + const account = resolveZalouserAccountSync({ + cfg: params.cfg, + accountId: params.accountId ?? undefined, + }); + const groups = account.config.groups ?? {}; + return findZalouserGroupEntry( + groups, + buildZalouserGroupCandidates({ + groupId: params.groupId, + groupChannel: params.groupChannel, + includeWildcard: true, + }), + ); +} + function resolveZalouserGroupToolPolicy( params: ChannelGroupContext, ): GroupToolPolicyConfig | undefined { - const account = resolveZalouserAccountSync({ - cfg: params.cfg, - accountId: params.accountId ?? undefined, - }); - const groups = account.config.groups ?? {}; - const entry = findZalouserGroupEntry( - groups, - buildZalouserGroupCandidates({ - groupId: params.groupId, - groupChannel: params.groupChannel, - includeWildcard: true, - }), - ); - return entry?.tools; + return resolveZalouserGroupPolicyEntry(params)?.tools; } function resolveZalouserRequireMention(params: ChannelGroupContext): boolean { - const account = resolveZalouserAccountSync({ - cfg: params.cfg, - accountId: params.accountId ?? undefined, - }); - const groups = account.config.groups ?? {}; - const entry = findZalouserGroupEntry( - groups, - buildZalouserGroupCandidates({ - groupId: params.groupId, - groupChannel: params.groupChannel, - includeWildcard: true, - }), - ); + const entry = resolveZalouserGroupPolicyEntry(params); if (typeof entry?.requireMention === "boolean") { return entry.requireMention; } @@ -395,13 +369,7 @@ export const zalouserPlugin: ChannelPlugin = { return trimmed.replace(/^(zalouser|zlu):/i, ""); }, targetResolver: { - looksLikeId: (raw) => { - const trimmed = raw.trim(); - if (!trimmed) { - return false; - } - return /^\d{3,}$/.test(trimmed); - }, + looksLikeId: isNumericTargetId, hint: "", }, }, @@ -560,49 +528,19 @@ export const zalouserPlugin: ChannelPlugin = { chunker: chunkTextForOutbound, chunkerMode: "text", textChunkLimit: 2000, - sendPayload: async (ctx) => { - const text = ctx.payload.text ?? ""; - const urls = ctx.payload.mediaUrls?.length - ? ctx.payload.mediaUrls - : ctx.payload.mediaUrl - ? [ctx.payload.mediaUrl] - : []; - if (!text && urls.length === 0) { - return { channel: "zalouser", messageId: "" }; - } - if (urls.length > 0) { - let lastResult = await zalouserPlugin.outbound!.sendMedia!({ - ...ctx, - text, - mediaUrl: urls[0], - }); - for (let i = 1; i < urls.length; i++) { - lastResult = await zalouserPlugin.outbound!.sendMedia!({ - ...ctx, - text: "", - mediaUrl: urls[i], - }); - } - return lastResult; - } - const outbound = zalouserPlugin.outbound!; - const limit = outbound.textChunkLimit; - const chunks = limit && outbound.chunker ? outbound.chunker(text, limit) : [text]; - let lastResult: Awaited>>; - for (const chunk of chunks) { - lastResult = await outbound.sendText!({ ...ctx, text: chunk }); - } - return lastResult!; - }, + sendPayload: async (ctx) => + await sendPayloadWithChunkedTextAndMedia({ + ctx, + textChunkLimit: zalouserPlugin.outbound!.textChunkLimit, + chunker: zalouserPlugin.outbound!.chunker, + sendText: (nextCtx) => zalouserPlugin.outbound!.sendText!(nextCtx), + sendMedia: (nextCtx) => zalouserPlugin.outbound!.sendMedia!(nextCtx), + emptyResult: { channel: "zalouser", messageId: "" }, + }), sendText: async ({ to, text, accountId, cfg }) => { const account = resolveZalouserAccountSync({ cfg: cfg, accountId }); const result = await sendMessageZalouser(to, text, { profile: account.profile }); - return { - channel: "zalouser", - ok: result.ok, - messageId: result.messageId ?? "", - error: result.error ? new Error(result.error) : undefined, - }; + return buildChannelSendResult("zalouser", result); }, sendMedia: async ({ to, text, mediaUrl, accountId, cfg, mediaLocalRoots }) => { const account = resolveZalouserAccountSync({ cfg: cfg, accountId }); @@ -611,12 +549,7 @@ export const zalouserPlugin: ChannelPlugin = { mediaUrl, mediaLocalRoots, }); - return { - channel: "zalouser", - ok: result.ok, - messageId: result.messageId ?? "", - error: result.error ? new Error(result.error) : undefined, - }; + return buildChannelSendResult("zalouser", result); }, }, status: { @@ -641,17 +574,19 @@ export const zalouserPlugin: ChannelPlugin = { buildAccountSnapshot: async ({ account, runtime }) => { const configured = await checkZcaAuthenticated(account.profile); const configError = "not authenticated"; + const base = buildBaseAccountStatusSnapshot({ + account: { + accountId: account.accountId, + name: account.name, + enabled: account.enabled, + configured, + }, + runtime: configured + ? runtime + : { ...runtime, lastError: runtime?.lastError ?? configError }, + }); return { - accountId: account.accountId, - name: account.name, - enabled: account.enabled, - configured, - running: runtime?.running ?? false, - lastStartAt: runtime?.lastStartAt ?? null, - lastStopAt: runtime?.lastStopAt ?? null, - lastError: configured ? (runtime?.lastError ?? null) : (runtime?.lastError ?? configError), - lastInboundAt: runtime?.lastInboundAt ?? null, - lastOutboundAt: runtime?.lastOutboundAt ?? null, + ...base, dmPolicy: account.config.dmPolicy ?? "pairing", }; }, diff --git a/extensions/zalouser/src/monitor.account-scope.test.ts b/extensions/zalouser/src/monitor.account-scope.test.ts index 931a6cde6eb..e1e1a348a95 100644 --- a/extensions/zalouser/src/monitor.account-scope.test.ts +++ b/extensions/zalouser/src/monitor.account-scope.test.ts @@ -1,21 +1,10 @@ import type { OpenClawConfig, PluginRuntime, RuntimeEnv } from "openclaw/plugin-sdk/zalouser"; import { describe, expect, it, vi } from "vitest"; import { __testing } from "./monitor.js"; +import { sendMessageZalouserMock } from "./monitor.send-mocks.js"; import { setZalouserRuntime } from "./runtime.js"; import type { ResolvedZalouserAccount, ZaloInboundMessage } from "./types.js"; -const sendMessageZalouserMock = vi.hoisted(() => vi.fn(async () => {})); -const sendTypingZalouserMock = vi.hoisted(() => vi.fn(async () => {})); -const sendDeliveredZalouserMock = vi.hoisted(() => vi.fn(async () => {})); -const sendSeenZalouserMock = vi.hoisted(() => vi.fn(async () => {})); - -vi.mock("./send.js", () => ({ - sendMessageZalouser: sendMessageZalouserMock, - sendTypingZalouser: sendTypingZalouserMock, - sendDeliveredZalouser: sendDeliveredZalouserMock, - sendSeenZalouser: sendSeenZalouserMock, -})); - describe("zalouser monitor pairing account scoping", () => { it("scopes DM pairing-store reads and pairing requests to accountId", async () => { const readAllowFromStore = vi.fn( diff --git a/extensions/zalouser/src/monitor.group-gating.test.ts b/extensions/zalouser/src/monitor.group-gating.test.ts index dda0ed0a3de..6af8d5ae115 100644 --- a/extensions/zalouser/src/monitor.group-gating.test.ts +++ b/extensions/zalouser/src/monitor.group-gating.test.ts @@ -1,21 +1,15 @@ import type { OpenClawConfig, PluginRuntime, RuntimeEnv } from "openclaw/plugin-sdk/zalouser"; import { beforeEach, describe, expect, it, vi } from "vitest"; import { __testing } from "./monitor.js"; +import { + sendDeliveredZalouserMock, + sendMessageZalouserMock, + sendSeenZalouserMock, + sendTypingZalouserMock, +} from "./monitor.send-mocks.js"; import { setZalouserRuntime } from "./runtime.js"; import type { ResolvedZalouserAccount, ZaloInboundMessage } from "./types.js"; -const sendMessageZalouserMock = vi.hoisted(() => vi.fn(async () => {})); -const sendTypingZalouserMock = vi.hoisted(() => vi.fn(async () => {})); -const sendDeliveredZalouserMock = vi.hoisted(() => vi.fn(async () => {})); -const sendSeenZalouserMock = vi.hoisted(() => vi.fn(async () => {})); - -vi.mock("./send.js", () => ({ - sendMessageZalouser: sendMessageZalouserMock, - sendTypingZalouser: sendTypingZalouserMock, - sendDeliveredZalouser: sendDeliveredZalouserMock, - sendSeenZalouser: sendSeenZalouserMock, -})); - function createAccount(): ResolvedZalouserAccount { return { accountId: "default", diff --git a/extensions/zalouser/src/monitor.send-mocks.ts b/extensions/zalouser/src/monitor.send-mocks.ts new file mode 100644 index 00000000000..9e576f5e830 --- /dev/null +++ b/extensions/zalouser/src/monitor.send-mocks.ts @@ -0,0 +1,20 @@ +import { vi } from "vitest"; + +const sendMocks = vi.hoisted(() => ({ + sendMessageZalouserMock: vi.fn(async () => {}), + sendTypingZalouserMock: vi.fn(async () => {}), + sendDeliveredZalouserMock: vi.fn(async () => {}), + sendSeenZalouserMock: vi.fn(async () => {}), +})); + +export const sendMessageZalouserMock = sendMocks.sendMessageZalouserMock; +export const sendTypingZalouserMock = sendMocks.sendTypingZalouserMock; +export const sendDeliveredZalouserMock = sendMocks.sendDeliveredZalouserMock; +export const sendSeenZalouserMock = sendMocks.sendSeenZalouserMock; + +vi.mock("./send.js", () => ({ + sendMessageZalouser: sendMessageZalouserMock, + sendTypingZalouser: sendTypingZalouserMock, + sendDeliveredZalouser: sendDeliveredZalouserMock, + sendSeenZalouser: sendSeenZalouserMock, +})); diff --git a/extensions/zalouser/src/qr-temp-file.ts b/extensions/zalouser/src/qr-temp-file.ts new file mode 100644 index 00000000000..07babfcc731 --- /dev/null +++ b/extensions/zalouser/src/qr-temp-file.ts @@ -0,0 +1,22 @@ +import fsp from "node:fs/promises"; +import path from "node:path"; +import { resolvePreferredOpenClawTmpDir } from "openclaw/plugin-sdk/zalouser"; + +export async function writeQrDataUrlToTempFile( + qrDataUrl: string, + profile: string, +): Promise { + const trimmed = qrDataUrl.trim(); + const match = trimmed.match(/^data:image\/png;base64,(.+)$/i); + const base64 = (match?.[1] ?? "").trim(); + if (!base64) { + return null; + } + const safeProfile = profile.replace(/[^a-zA-Z0-9_-]+/g, "-") || "default"; + const filePath = path.join( + resolvePreferredOpenClawTmpDir(), + `openclaw-zalouser-qr-${safeProfile}.png`, + ); + await fsp.writeFile(filePath, Buffer.from(base64, "base64")); + return filePath; +} diff --git a/extensions/zalouser/src/zca-client.ts b/extensions/zalouser/src/zca-client.ts index 94e291b710f..605b07522d6 100644 --- a/extensions/zalouser/src/zca-client.ts +++ b/extensions/zalouser/src/zca-client.ts @@ -126,6 +126,20 @@ export type Listener = { stop(): void; }; +type DeliveryEventMessage = { + msgId: string; + cliMsgId: string; + uidFrom: string; + idTo: string; + msgType: string; + st: number; + at: number; + cmd: number; + ts: string | number; +}; + +type DeliveryEventMessages = DeliveryEventMessage | DeliveryEventMessage[]; + export type API = { listener: Listener; getContext(): { @@ -185,57 +199,10 @@ export type API = { ): Promise; sendDeliveredEvent( isSeen: boolean, - messages: - | { - msgId: string; - cliMsgId: string; - uidFrom: string; - idTo: string; - msgType: string; - st: number; - at: number; - cmd: number; - ts: string | number; - } - | Array<{ - msgId: string; - cliMsgId: string; - uidFrom: string; - idTo: string; - msgType: string; - st: number; - at: number; - cmd: number; - ts: string | number; - }>, - type?: number, - ): Promise; - sendSeenEvent( - messages: - | { - msgId: string; - cliMsgId: string; - uidFrom: string; - idTo: string; - msgType: string; - st: number; - at: number; - cmd: number; - ts: string | number; - } - | Array<{ - msgId: string; - cliMsgId: string; - uidFrom: string; - idTo: string; - msgType: string; - st: number; - at: number; - cmd: number; - ts: string | number; - }>, + messages: DeliveryEventMessages, type?: number, ): Promise; + sendSeenEvent(messages: DeliveryEventMessages, type?: number): Promise; }; type ZaloCtor = new (options?: { logging?: boolean; selfListen?: boolean }) => {