UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

307 lines (306 loc) • 12.2 kB
import { g as DEFAULT_ACCOUNT_ID } from "./session-key-DAhnzjyr.js"; import { a as DmPolicySchema, c as GroupPolicySchema, m as MarkdownConfigSchema } from "./zod-schema.core-BuVz8Rk7.js"; import { Bx as createOpenProviderGroupPolicyWarningCollector, Cx as buildOpenGroupPolicyRestrictSendersWarning, Db as formatAllowFromLowercase, Gb as stripTargetKindPrefix, Jb as adaptScopedAccountAccessor, Qb as createScopedChannelConfigAdapter, Vb as createChatChannelPlugin, Wb as stripChannelTargetPrefix, ax as mapAllowFromEntries, ex as createScopedDmSecurityResolver, wx as buildOpenGroupPolicyWarning, zb as buildChannelOutboundSessionRoute } from "./pi-embedded-CbCYZxIb.js"; import { n as describeAccountSnapshot } from "./account-helpers-BzkeDMfB.js"; import { r as buildSecretInputSchema } from "./secret-input-DXoOLNgb.js"; import { c as jsonResult, h as readStringParam } from "./common-zReYuoMM.js"; import { n as buildCatchallMultiAccountChannelSchema, r as buildChannelConfigSchema, t as AllowFromListSchema } from "./config-schema-CPzP2Ds6.js"; import { l as createComputedAccountStatusAdapter, o as buildTokenChannelStatusSummary, u as createDefaultChannelRuntimeState } from "./status-helpers-Cflmo90d.js"; import { Y as createStaticReplyToModeResolver } from "./conversation-runtime-nmWgJSXS.js"; import { a as createEmptyChannelResult, o as createRawChannelSendResultAdapter } from "./channel-send-result-DDWpviAF.js"; import { l as isNumericTargetId, y as sendPayloadWithChunkedTextAndMedia } from "./reply-payload-CiQ7TPgp.js"; import { i as createLazyRuntimeNamedExport, r as createLazyRuntimeModule } from "./lazy-runtime-CPDBPNrj.js"; import { d as listResolvedDirectoryUserEntriesFromAllowFrom, p as createChannelDirectoryAdapter } from "./directory-runtime-D9Y42mW-.js"; import { t as extractToolSend } from "./tool-send-CKtvWnSt.js"; import { i as coerceStatusIssueAccountId, o as readStatusIssueFields } from "./extension-shared-B8dMu-tU.js"; import { n as zaloSetupAdapter, t as zaloSetupWizard } from "./api-DlugfmwO.js"; import { i as resolveZaloAccount, n as listZaloAccountIds, r as resolveDefaultZaloAccountId, t as listEnabledZaloAccounts } from "./accounts-DcKwFiLd.js"; import { t as chunkTextForOutbound } from "./text-chunking-B3knjHhK.js"; import { z } from "zod"; //#region extensions/zalo/src/actions.ts const loadZaloActionsRuntime = createLazyRuntimeNamedExport(() => import("./actions.runtime-BpZDUKrQ.js"), "zaloActionsRuntime"); const providerId = "zalo"; function listEnabledAccounts(cfg) { return listEnabledZaloAccounts(cfg).filter((account) => account.enabled && account.tokenSource !== "none"); } const zaloMessageActions = { describeMessageTool: ({ cfg }) => { if (listEnabledAccounts(cfg).length === 0) return null; const actions = new Set(["send"]); return { actions: Array.from(actions), capabilities: [] }; }, extractToolSend: ({ args }) => extractToolSend(args, "sendMessage"), handleAction: async ({ action, params, cfg, accountId }) => { if (action === "send") { const to = readStringParam(params, "to", { required: true }); const content = readStringParam(params, "message", { required: true, allowEmpty: true }); const mediaUrl = readStringParam(params, "media", { trim: false }); const { sendMessageZalo } = await loadZaloActionsRuntime(); const result = await sendMessageZalo(to ?? "", content ?? "", { accountId: accountId ?? void 0, mediaUrl: mediaUrl ?? void 0, cfg }); if (!result.ok) return jsonResult({ ok: false, error: result.error ?? "Failed to send Zalo message" }); return jsonResult({ ok: true, to, messageId: result.messageId }); } throw new Error(`Action ${action} is not supported for provider ${providerId}.`); } }; const ZaloConfigSchema = buildCatchallMultiAccountChannelSchema(z.object({ name: z.string().optional(), enabled: z.boolean().optional(), markdown: MarkdownConfigSchema, botToken: buildSecretInputSchema().optional(), tokenFile: z.string().optional(), webhookUrl: z.string().optional(), webhookSecret: buildSecretInputSchema().optional(), webhookPath: z.string().optional(), dmPolicy: DmPolicySchema.optional(), allowFrom: AllowFromListSchema, groupPolicy: GroupPolicySchema.optional(), groupAllowFrom: AllowFromListSchema, mediaMaxMb: z.number().optional(), proxy: z.string().optional(), responsePrefix: z.string().optional() })); //#endregion //#region extensions/zalo/src/session-route.ts function resolveZaloOutboundSessionRoute(params) { const trimmed = stripChannelTargetPrefix(params.target, "zalo", "zl"); if (!trimmed) return null; const isGroup = trimmed.toLowerCase().startsWith("group:"); const peerId = stripTargetKindPrefix(trimmed); if (!peerId) return null; return buildChannelOutboundSessionRoute({ cfg: params.cfg, agentId: params.agentId, channel: "zalo", accountId: params.accountId, peer: { kind: isGroup ? "group" : "direct", id: peerId }, chatType: isGroup ? "group" : "direct", from: isGroup ? `zalo:group:${peerId}` : `zalo:${peerId}`, to: `zalo:${peerId}` }); } //#endregion //#region extensions/zalo/src/status-issues.ts const ZALO_STATUS_FIELDS = [ "accountId", "enabled", "configured", "dmPolicy" ]; function collectZaloStatusIssues(accounts) { const issues = []; for (const entry of accounts) { const account = readStatusIssueFields(entry, ZALO_STATUS_FIELDS); if (!account) continue; const accountId = coerceStatusIssueAccountId(account.accountId) ?? "default"; const enabled = account.enabled !== false; const configured = account.configured === true; if (!enabled || !configured) continue; if (account.dmPolicy === "open") issues.push({ channel: "zalo", accountId, kind: "config", message: "Zalo dmPolicy is \"open\", allowing any user to message the bot without pairing.", fix: "Set channels.zalo.dmPolicy to \"pairing\" or \"allowlist\" to restrict access." }); } return issues; } //#endregion //#region extensions/zalo/src/channel.ts const meta = { id: "zalo", label: "Zalo", selectionLabel: "Zalo (Bot API)", docsPath: "/channels/zalo", docsLabel: "zalo", blurb: "Vietnam-focused messaging platform with Bot API.", aliases: ["zl"], order: 80, quickstartAllowFrom: true }; function normalizeZaloMessagingTarget(raw) { const trimmed = raw?.trim(); if (!trimmed) return; return trimmed.replace(/^(zalo|zl):/i, "").trim(); } const loadZaloChannelRuntime = createLazyRuntimeModule(() => import("./channel.runtime-_l04Zc-i.js")); const zaloTextChunkLimit = 2e3; const zaloRawSendResultAdapter = createRawChannelSendResultAdapter({ channel: "zalo", sendText: async ({ to, text, accountId, cfg }) => await (await loadZaloChannelRuntime()).sendZaloText({ to, text, accountId: accountId ?? void 0, cfg }), sendMedia: async ({ to, text, mediaUrl, accountId, cfg }) => await (await loadZaloChannelRuntime()).sendZaloText({ to, text, accountId: accountId ?? void 0, mediaUrl, cfg }) }); const zaloConfigAdapter = createScopedChannelConfigAdapter({ sectionKey: "zalo", listAccountIds: listZaloAccountIds, resolveAccount: adaptScopedAccountAccessor(resolveZaloAccount), defaultAccountId: resolveDefaultZaloAccountId, clearBaseFields: [ "botToken", "tokenFile", "name" ], resolveAllowFrom: (account) => account.config.allowFrom, formatAllowFrom: (allowFrom) => formatAllowFromLowercase({ allowFrom, stripPrefixRe: /^(zalo|zl):/i }) }); const resolveZaloDmPolicy = createScopedDmSecurityResolver({ channelKey: "zalo", resolvePolicy: (account) => account.config.dmPolicy, resolveAllowFrom: (account) => account.config.allowFrom, policyPathSuffix: "dmPolicy", normalizeEntry: (raw) => raw.trim().replace(/^(zalo|zl):/i, "") }); const collectZaloSecurityWarnings = createOpenProviderGroupPolicyWarningCollector({ providerConfigPresent: (cfg) => cfg.channels?.zalo !== void 0, resolveGroupPolicy: ({ account }) => account.config.groupPolicy, collect: ({ account, groupPolicy }) => { if (groupPolicy !== "open") return []; const explicitGroupAllowFrom = mapAllowFromEntries(account.config.groupAllowFrom); const dmAllowFrom = mapAllowFromEntries(account.config.allowFrom); if ((explicitGroupAllowFrom.length > 0 ? explicitGroupAllowFrom : dmAllowFrom).length > 0) return [buildOpenGroupPolicyRestrictSendersWarning({ surface: "Zalo groups", openScope: "any member", groupPolicyPath: "channels.zalo.groupPolicy", groupAllowFromPath: "channels.zalo.groupAllowFrom" })]; return [buildOpenGroupPolicyWarning({ surface: "Zalo groups", openBehavior: "with no groupAllowFrom/allowFrom allowlist; any member can trigger (mention-gated)", remediation: "Set channels.zalo.groupPolicy=\"allowlist\" + channels.zalo.groupAllowFrom" })]; } }); const zaloPlugin = createChatChannelPlugin({ base: { id: "zalo", meta, setup: zaloSetupAdapter, setupWizard: zaloSetupWizard, capabilities: { chatTypes: ["direct", "group"], media: true, reactions: false, threads: false, polls: false, nativeCommands: false, blockStreaming: true }, reload: { configPrefixes: ["channels.zalo"] }, configSchema: buildChannelConfigSchema(ZaloConfigSchema), config: { ...zaloConfigAdapter, isConfigured: (account) => Boolean(account.token?.trim()), describeAccount: (account) => describeAccountSnapshot({ account, configured: Boolean(account.token?.trim()), extra: { tokenSource: account.tokenSource } }) }, groups: { resolveRequireMention: () => true }, actions: zaloMessageActions, messaging: { normalizeTarget: normalizeZaloMessagingTarget, resolveOutboundSessionRoute: (params) => resolveZaloOutboundSessionRoute(params), targetResolver: { looksLikeId: isNumericTargetId, hint: "<chatId>" } }, directory: createChannelDirectoryAdapter({ listPeers: async (params) => listResolvedDirectoryUserEntriesFromAllowFrom({ ...params, resolveAccount: adaptScopedAccountAccessor(resolveZaloAccount), resolveAllowFrom: (account) => account.config.allowFrom, normalizeId: (entry) => entry.trim().replace(/^(zalo|zl):/i, "") }), listGroups: async () => [] }), status: createComputedAccountStatusAdapter({ defaultRuntime: createDefaultChannelRuntimeState(DEFAULT_ACCOUNT_ID), collectStatusIssues: collectZaloStatusIssues, buildChannelSummary: ({ snapshot }) => buildTokenChannelStatusSummary(snapshot), probeAccount: async ({ account, timeoutMs }) => await (await loadZaloChannelRuntime()).probeZaloAccount({ account, timeoutMs }), resolveAccountSnapshot: ({ account }) => { const configured = Boolean(account.token?.trim()); return { accountId: account.accountId, name: account.name, enabled: account.enabled, configured, extra: { tokenSource: account.tokenSource, mode: account.config.webhookUrl ? "webhook" : "polling", dmPolicy: account.config.dmPolicy ?? "pairing" } }; } }), gateway: { startAccount: async (ctx) => await (await loadZaloChannelRuntime()).startZaloGatewayAccount(ctx) } }, security: { resolveDmPolicy: resolveZaloDmPolicy, collectWarnings: collectZaloSecurityWarnings }, pairing: { text: { idLabel: "zaloUserId", message: "Your pairing request has been approved.", normalizeAllowEntry: (entry) => entry.trim().replace(/^(zalo|zl):/i, ""), notify: async (params) => await (await loadZaloChannelRuntime()).notifyZaloPairingApproval(params) } }, threading: { resolveReplyToMode: createStaticReplyToModeResolver("off") }, outbound: { deliveryMode: "direct", chunker: chunkTextForOutbound, chunkerMode: "text", textChunkLimit: zaloTextChunkLimit, sendPayload: async (ctx) => await sendPayloadWithChunkedTextAndMedia({ ctx, textChunkLimit: zaloTextChunkLimit, chunker: chunkTextForOutbound, sendText: (nextCtx) => zaloRawSendResultAdapter.sendText(nextCtx), sendMedia: (nextCtx) => zaloRawSendResultAdapter.sendMedia(nextCtx), emptyResult: createEmptyChannelResult("zalo") }), ...zaloRawSendResultAdapter } }); //#endregion export { zaloPlugin as t };