UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

864 lines (863 loc) • 32.6 kB
import "../../redact-BDinS1q9.js"; import "../../errors-BxyFnvP3.js"; import "../../unhandled-rejections-CDJ8dOVP.js"; import "../../logger-kwZIqwuw.js"; import "../../paths-ViKUYWUK.js"; import "../../tmp-openclaw-dir-idKIOMmb.js"; import "../../theme-CdOoMzRk.js"; import "../../globals-DBUMOBZ8.js"; import "../../subsystem-DISldKSB.js"; import "../../ansi-BEJF8NKS.js"; import "../../boolean-C3GkJetE.js"; import "../../env-Dnra1IpT.js"; import "../../warning-filter-CBhOcgHd.js"; import "../../utils-CS0Ikux6.js"; import "../../links-8xRhWBQL.js"; import "../../paths-C4Oy7wjk.js"; import "../../auth-profiles-DRuJBw5y.js"; import { _ as normalizeAccountId, g as DEFAULT_ACCOUNT_ID } from "../../session-key-DAhnzjyr.js"; import "../../agent-scope-bjWqU22i.js"; import "../../boundary-path-Dm0QJ7-y.js"; import "../../boundary-file-read-DcZxlWD8.js"; import "../../logger-BmpSCz93.js"; import "../../exec-B5_AYfQG.js"; import "../../workspace-D4K6QX9X.js"; import "../../model-selection-BnFtDmP7.js"; import "../../io-y3Az_Onx.js"; import "../../shell-env-BOu7XeT_.js"; import "../../safe-text-yavot2qw.js"; import "../../version-CD3oP1-d.js"; import "../../env-substitution-O6uabUmO.js"; import "../../includes-Bj3eLUWH.js"; import "../../zod-schema.providers-core-COy5nBQ0.js"; import "../../legacy-web-search-BB-ZHEhz.js"; import "../../registry-C5UkPpaO.js"; import "../../config-state-Br0ucqMb.js"; import "../../min-host-version-RMBWtIAR.js"; import "../../manifest-registry-B5JNQdOM.js"; import "../../runtime-guard-PhQ6PwQa.js"; import "../../avatar-policy-B5nOfso_.js"; import "../../ip-Ce8EDTBZ.js"; import "../../zod-schema.agent-runtime-Dtg4Jy6G.js"; import "../../zod-schema.core-BuVz8Rk7.js"; import "../../config-D4zN4BRl.js"; import "../../file-lock-DEiVq2ow.js"; import "../../audit-fs-DTQIjp2r.js"; import "../../resolve-B7S4PlnO.js"; import "../../profiles-COPO-hHI.js"; import "../../tailscale-CxY-91GZ.js"; import "../../tailnet-DHiKSG7V.js"; import "../../net-JAg9zL0G.js"; import "../../auth-Buq0Niri.js"; import "../../credentials-D8pfRimu.js"; import "../../message-channel-BliByQBl.js"; import "../../store-C5UK26Ce.js"; import "../../runtime-Iz8uZ7EU.js"; import "../../plugins-B09-vgme.js"; import "../../sessions-D9ZgHCHb.js"; import "../../paths-rhN9LKM_.js"; import "../../session-write-lock-va3qZE6f.js"; import "../../method-scopes-BiEi0X2g.js"; import "../../call-CQbSO4Fr.js"; import "../../prompt-style-C4CV4Y_p.js"; import "../../ports-lsof-BLEgL_Gn.js"; import "../../restart-stale-pids-BsPvlGJd.js"; import "../../ports-FldL_Lhq.js"; import "../../logging-CznuqaMu.js"; import "../../commands-C6oAw_H-.js"; import "../../issue-format-BtLIwOtN.js"; import "../../identity-Dp-UIak4.js"; import "../../heartbeat-Bdfr_onc.js"; import { Hb as defineChannelPluginEntry, Vb as createChatChannelPlugin, ex as createScopedDmSecurityResolver, zb as buildChannelOutboundSessionRoute } from "../../pi-embedded-CbCYZxIb.js"; import "../../internal-hooks-CIUghoK4.js"; import "../../multimodal-B8yEZcl5.js"; import "../../memory-search-B_aq08y5.js"; import "../../provider-catalog-rVN7ZbR5.js"; import "../../secret-input-DXoOLNgb.js"; import "../../bindings-CFudGB9Z.js"; import "../../resolve-route-DwFWP4JW.js"; import "../../routing-zOQ0wwtV.js"; import "../../identity-file-DVLib087.js"; import "../../outbound-runtime-m-y0nd6F.js"; import "../../provider-env-vars-CpUJqT5v.js"; import "../../provider-auth-input-Bus9W_qh.js"; import "../../provider-model-minimax-DSmX4BNO.js"; import "../../provider-models-BKjzpTsb.js"; import "../../anthropic-vertex-provider-B2x4YywE.js"; import "../../provider-catalog-BTgF8VyO.js"; import "../../provider-catalog-D0IlX18O.js"; import "../../provider-catalog-BTmUX0Mp.js"; import "../../provider-catalog-sph_Rl63.js"; import "../../provider-catalog-C7tUyBwV.js"; import "../../provider-catalog-C8dPZpHW.js"; import "../../provider-catalog-BJNff1VI.js"; import "../../provider-catalog-IAdymoqT.js"; import "../../provider-catalog-Bjc7evzh.js"; import "../../models-config.providers.discovery-CjkO08q6.js"; import "../../text-runtime-DVXt5xhi.js"; import "../../tool-catalog-BjSY4C4F.js"; import "../../docker-Bhjg8g2t.js"; import "../../sandbox-DTlKNieF.js"; import "../../common-zReYuoMM.js"; import "../../image-ops-By4YfyWa.js"; import "../../thinking-CJoSRIGR.js"; import "../../path-alias-guards-CwRM04O1.js"; import "../../sandbox-paths-Cn8i8Jxm.js"; import "../../channel-actions-fV93bLjV.js"; import "../../mime-CsUtzcn2.js"; import "../../ssrf-0bPJMoZR.js"; import "../../fetch-guard-CYl1q2XH.js"; import "../../provider-web-search-COZNygbd.js"; import "../../manager-Bw8JrihM.js"; import "../../config-schema-CPzP2Ds6.js"; import "../../channel-plugin-common-ClptUZHU.js"; import { c as createAsyncComputedAccountStatusAdapter, 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 "../../runtime-whatsapp-boundary-XjdkYMz_.js"; import "../../pairing-store-BGKLrjo6.js"; import "../../json-store--JDX3zNL.js"; import { i as createPairingPrefixStripper } from "../../channel-pairing-BrG9NqjP.js"; import "../../secret-file-Bif7RlZ-.js"; import "../../channel-config-schema-CgpS17zB.js"; import "../../setup-binary-C2MYZTCc.js"; import "../../archive-pOxyr9r4.js"; import "../../fs-safe-DZyOgpPN.js"; import "../../signal-cli-install-fVOL9FvX.js"; import "../../setup-wizard-proxy-DoOpOF8S.js"; import "../../setup-BI3ohdrT.js"; import "../../dm-policy-shared-DtcQProI.js"; import "../../runtime-env-DQOYrXVl.js"; import "../../temp-path-B91lt0LF.js"; import "../../diagnostic-CDTtMQBT.js"; import "../../logging-CCTKg_fh.js"; import "../../config-runtime-BYNizC50.js"; import { n as isDangerousNameMatchingEnabled } from "../../dangerous-name-matching-BQW4HoPt.js"; import "../../level-overrides-DzIzQ3ZY.js"; import "../../exec-approvals-BF_Qfdq8.js"; import "../../webhook-ingress-D-H5frou.js"; import "../../system-events-BU_NYynX.js"; import "../../ssrf-policy-DYINyIKC.js"; import { l as isNumericTargetId, y as sendPayloadWithChunkedTextAndMedia } from "../../reply-payload-CiQ7TPgp.js"; import "../../hook-runtime-BUYmDbaA.js"; import "../../templating-BDXRiJUh.js"; import "../../channel-reply-pipeline-BxH4-AP2.js"; import "../../reply-history-n7nOPLta.js"; import "../../commands-registry.data-BFXJWoqi.js"; import "../../commands-registry-Bh9pJ817.js"; import "../../frontmatter-CP5371Rm.js"; import "../../env-overrides-BNSo89u5.js"; import "../../skills-M0AZJeXx.js"; import "../../skills-remote-BHxDA4Jw.js"; import "../../workspace-dirs-DqDLiRvZ.js"; import "../../pairing-token-DAszGmwD.js"; import "../../skill-commands-D73dGR_l.js"; import "../../config-CLRQPUSt.js"; import "../../routes-XT2JfvT-.js"; import "../../ssh-tunnel-LM52qY6N.js"; import "../../server-middleware-BP59C6FL.js"; import "../../provider-auth-ref-CQEp9sp3.js"; import "../../provider-auth-helpers-Do3RFvdi.js"; import "../../provider-api-key-auth-BEeRW3dq.js"; import "../../pairing-labels-D27Y4Vv6.js"; import "../../directory-runtime-D9Y42mW-.js"; import "../../read-only-account-inspect-CJPRBFj7.js"; import "../../src-BoNU6eQs.js"; import "../../web-media-PKy3RbRG.js"; import "../../media-understanding-BDOSvuM9.js"; import "../../web-media-CiSbdGPs.js"; import "../../state-paths-Dvb3Mfl3.js"; import "../../llm-task-Br6Ixjlh.js"; import "../../pi-model-discovery-DO3tFksS.js"; import "../../exec-inline-eval-CpmxWJsW.js"; import "../../target-registry-9OUOvlxj.js"; import "../../external-content-DZI9vlJn.js"; import "../../brave-onFD06fq.js"; import "../../duckduckgo-CZuAOfiQ.js"; import "../../exa-5bwgY04k.js"; import "../../security-runtime-xlw0Fxx2.js"; import "../../provider-usage-ClxTGn5c.js"; import "../../provider-models-Bjv0V4OP.js"; import "../../media-understanding-provider-DllGbg-v.js"; import "../../media-understanding-provider-BDq4rFL-.js"; import "../../provider-onboard-CSnPvkMM.js"; import "../../onboard-BU8aRvq7.js"; import "../../perplexity-web-search-provider-CI38RNUU.js"; import "../../perplexity-CMZbsreI.js"; import "../../model-definitions-CLagLj0g.js"; import "../../onboard-0P7GBI2D.js"; import "../../provider-catalog-CKHo9CRM.js"; import "../../provider-models-Dmjtw6kn.js"; import "../../stream-C-KGzzT0.js"; import "../../web-search-CO1hvu_l.js"; import "../../stagger-CFqdtCYL.js"; import "../../command-secret-targets-DLs8jC2H.js"; import "../../delivery-queue-DHURMSqX.js"; import "../../channel-summary-Ctrv7moa.js"; import "../../follow-redirects-CVAaqIk4.js"; import "../../tool-policy-match-DQWWRSN4.js"; import "../../runtime-CAdCdG-M.js"; import "../../channel-status-BfM90KqH.js"; import "../../discord-core-DLbIyXWy.js"; import "../../cli-runtime-Cx1v7N7v.js"; import "../../config-presence-DA3j8Nmi.js"; import "../../query-expansion-BOJBTXsp.js"; import "../../search-manager-DNBHwaee.js"; import "../../acp-runtime-BHZj-SLU.js"; import "../../telegram-core-DD8gCuaX.js"; import "../../audit-BI48Fi3j.js"; import "../../gateway-runtime-Dn2qAGY7.js"; import "../../connection-auth-Nl2a3tDb.js"; import { t as createAccountStatusSink } from "../../channel-lifecycle-DmiShiUP.js"; import "../../mcp-config-CDIWSqVk.js"; import "../../channel-setup-C5JbULY6.js"; import { i as coerceStatusIssueAccountId, n as buildPassiveProbedChannelStatusSummary, o as readStatusIssueFields } from "../../extension-shared-B8dMu-tU.js"; import "../../outbound-media-CVWWX2Ba.js"; import "../../semver-DgFx6ze4.js"; import "../../zalouser-BPCNqkr-.js"; import "../../json-bigint-CbAcNBLP.js"; import "../../runtime-api-DyobrjkL.js"; import { a as listZaloGroupMembers, b as waitForZaloQrLogin, c as logoutZaloProfile, i as listZaloFriendsMatching, n as getZaloUserInfo, s as listZaloGroupsMatching, t as checkZaloAuthenticated, y as startZaloQrLogin } from "../../zalo-js-HULxcu6w.js"; import { a as listZalouserAccountIds, i as getZcaUserInfo, n as zalouserSetupAdapter, r as writeQrDataUrlToTempFile, s as resolveZalouserAccountSync, t as zalouserSetupWizard } from "../../setup-surface-CiV2AJTU.js"; import { a as sendReactionZalouser, c as getZalouserRuntime, f as resolveZalouserReactionMessageIds, i as sendMessageZalouser, l as setZalouserRuntime, m as findZalouserGroupEntry, n as sendImageZalouser, p as buildZalouserGroupCandidates, r as sendLinkZalouser } from "../../send-C_N0Xend.js"; import { t as createZalouserPluginBase } from "../../shared-DsGPU2OP.js"; import { Type } from "@sinclair/typebox"; //#region extensions/zalouser/src/probe.ts async function probeZalouser(profile, timeoutMs) { try { const user = timeoutMs ? await Promise.race([getZaloUserInfo(profile), new Promise((resolve) => setTimeout(() => resolve(null), Math.max(timeoutMs, 1e3)))]) : await getZaloUserInfo(profile); if (!user) return { ok: false, error: "Not authenticated" }; return { ok: true, user }; } catch (error) { return { ok: false, error: error instanceof Error ? error.message : String(error) }; } } //#endregion //#region extensions/zalouser/src/session-route.ts function stripZalouserTargetPrefix(raw) { return raw.trim().replace(/^(zalouser|zlu):/i, "").trim(); } function normalizeZalouserTarget(raw) { const trimmed = stripZalouserTargetPrefix(raw); if (!trimmed) return; const lower = trimmed.toLowerCase(); if (lower.startsWith("group:")) { const id = trimmed.slice(6).trim(); return id ? `group:${id}` : void 0; } if (lower.startsWith("g:")) { const id = trimmed.slice(2).trim(); return id ? `group:${id}` : void 0; } if (lower.startsWith("user:")) { const id = trimmed.slice(5).trim(); return id ? `user:${id}` : void 0; } if (lower.startsWith("dm:")) { const id = trimmed.slice(3).trim(); return id ? `user:${id}` : void 0; } if (lower.startsWith("u:")) { const id = trimmed.slice(2).trim(); return id ? `user:${id}` : void 0; } if (/^g-\S+$/i.test(trimmed)) return `group:${trimmed}`; if (/^u-\S+$/i.test(trimmed)) return `user:${trimmed}`; return trimmed; } function parseZalouserOutboundTarget(raw) { const normalized = normalizeZalouserTarget(raw); if (!normalized) throw new Error("Zalouser target is required"); const lowered = normalized.toLowerCase(); if (lowered.startsWith("group:")) { const threadId = normalized.slice(6).trim(); if (!threadId) throw new Error("Zalouser group target is missing group id"); return { threadId, isGroup: true }; } if (lowered.startsWith("user:")) { const threadId = normalized.slice(5).trim(); if (!threadId) throw new Error("Zalouser user target is missing user id"); return { threadId, isGroup: false }; } return { threadId: normalized, isGroup: false }; } function parseZalouserDirectoryGroupId(raw) { const normalized = normalizeZalouserTarget(raw); if (!normalized) throw new Error("Zalouser group target is required"); const lowered = normalized.toLowerCase(); if (lowered.startsWith("group:")) { const groupId = normalized.slice(6).trim(); if (!groupId) throw new Error("Zalouser group target is missing group id"); return groupId; } if (lowered.startsWith("user:")) throw new Error("Zalouser group members lookup requires a group target (group:<id>)"); return normalized; } function resolveZalouserOutboundSessionRoute(params) { const normalized = normalizeZalouserTarget(params.target); if (!normalized) return null; const isGroup = normalized.toLowerCase().startsWith("group:"); const peerId = normalized.replace(/^(group|user):/i, "").trim(); return buildChannelOutboundSessionRoute({ cfg: params.cfg, agentId: params.agentId, channel: "zalouser", accountId: params.accountId, peer: { kind: isGroup ? "group" : "direct", id: peerId }, chatType: isGroup ? "group" : "direct", from: isGroup ? `zalouser:group:${peerId}` : `zalouser:${peerId}`, to: `zalouser:${peerId}` }); } //#endregion //#region extensions/zalouser/src/status-issues.ts const ZALOUSER_STATUS_FIELDS = [ "accountId", "enabled", "configured", "dmPolicy", "lastError" ]; function collectZalouserStatusIssues(accounts) { const issues = []; for (const entry of accounts) { const account = readStatusIssueFields(entry, ZALOUSER_STATUS_FIELDS); if (!account) continue; const accountId = coerceStatusIssueAccountId(account.accountId) ?? "default"; if (!(account.enabled !== false)) continue; if (!(account.configured === true)) { issues.push({ channel: "zalouser", accountId, kind: "auth", message: "Not authenticated (no saved Zalo session).", fix: "Run: openclaw channels login --channel zalouser" }); continue; } if (account.dmPolicy === "open") issues.push({ channel: "zalouser", accountId, kind: "config", message: "Zalo Personal dmPolicy is \"open\", allowing any user to message the bot without pairing.", fix: "Set channels.zalouser.dmPolicy to \"pairing\" or \"allowlist\" to restrict access." }); } return issues; } //#endregion //#region extensions/zalouser/src/channel.ts const ZALOUSER_TEXT_CHUNK_LIMIT = 2e3; const zalouserRawSendResultAdapter = createRawChannelSendResultAdapter({ channel: "zalouser", sendText: async ({ to, text, accountId, cfg }) => { const account = resolveZalouserAccountSync({ cfg, accountId }); const target = parseZalouserOutboundTarget(to); return await sendMessageZalouser(target.threadId, text, { profile: account.profile, isGroup: target.isGroup, textMode: "markdown", textChunkMode: resolveZalouserOutboundChunkMode(cfg, account.accountId), textChunkLimit: resolveZalouserOutboundTextChunkLimit(cfg, account.accountId) }); }, sendMedia: async ({ to, text, mediaUrl, accountId, cfg, mediaLocalRoots }) => { const account = resolveZalouserAccountSync({ cfg, accountId }); const target = parseZalouserOutboundTarget(to); return await sendMessageZalouser(target.threadId, text, { profile: account.profile, isGroup: target.isGroup, mediaUrl, mediaLocalRoots, textMode: "markdown", textChunkMode: resolveZalouserOutboundChunkMode(cfg, account.accountId), textChunkLimit: resolveZalouserOutboundTextChunkLimit(cfg, account.accountId) }); } }); function resolveZalouserQrProfile(accountId) { const normalized = normalizeAccountId(accountId); if (!normalized || normalized === "default") return process.env.ZALOUSER_PROFILE?.trim() || process.env.ZCA_PROFILE?.trim() || "default"; return normalized; } function resolveZalouserOutboundChunkMode(cfg, accountId) { return getZalouserRuntime().channel.text.resolveChunkMode(cfg, "zalouser", accountId); } function resolveZalouserOutboundTextChunkLimit(cfg, accountId) { return getZalouserRuntime().channel.text.resolveTextChunkLimit(cfg, "zalouser", accountId, { fallbackLimit: ZALOUSER_TEXT_CHUNK_LIMIT }); } function mapUser(params) { return { kind: "user", id: params.id, name: params.name ?? void 0, avatarUrl: params.avatarUrl ?? void 0, raw: params.raw }; } function mapGroup(params) { return { kind: "group", id: params.id, name: params.name ?? void 0, raw: params.raw }; } function resolveZalouserGroupPolicyEntry(params) { const account = resolveZalouserAccountSync({ cfg: params.cfg, accountId: params.accountId ?? void 0 }); return findZalouserGroupEntry(account.config.groups ?? {}, buildZalouserGroupCandidates({ groupId: params.groupId, groupChannel: params.groupChannel, includeWildcard: true, allowNameMatching: isDangerousNameMatchingEnabled(account.config) })); } function resolveZalouserGroupToolPolicy(params) { return resolveZalouserGroupPolicyEntry(params)?.tools; } function resolveZalouserRequireMention(params) { const entry = resolveZalouserGroupPolicyEntry(params); if (typeof entry?.requireMention === "boolean") return entry.requireMention; return true; } const resolveZalouserDmPolicy = createScopedDmSecurityResolver({ channelKey: "zalouser", resolvePolicy: (account) => account.config.dmPolicy, resolveAllowFrom: (account) => account.config.allowFrom, policyPathSuffix: "dmPolicy", normalizeEntry: (raw) => raw.trim().replace(/^(zalouser|zlu):/i, "") }); const zalouserMessageActions = { describeMessageTool: ({ cfg }) => { if (listZalouserAccountIds(cfg).map((accountId) => resolveZalouserAccountSync({ cfg, accountId })).filter((account) => account.enabled).length === 0) return null; return { actions: ["react"] }; }, supportsAction: ({ action }) => action === "react", handleAction: async ({ action, params, cfg, accountId, toolContext }) => { if (action !== "react") throw new Error(`Zalouser action ${action} not supported`); const account = resolveZalouserAccountSync({ cfg, accountId }); const threadId = (typeof params.threadId === "string" ? params.threadId.trim() : "") || (typeof params.to === "string" ? params.to.trim() : "") || (typeof params.chatId === "string" ? params.chatId.trim() : "") || (toolContext?.currentChannelId?.trim() ?? ""); if (!threadId) throw new Error("Zalouser react requires threadId (or to/chatId)."); const emoji = typeof params.emoji === "string" ? params.emoji.trim() : ""; if (!emoji) throw new Error("Zalouser react requires emoji."); const ids = resolveZalouserReactionMessageIds({ messageId: typeof params.messageId === "string" ? params.messageId : void 0, cliMsgId: typeof params.cliMsgId === "string" ? params.cliMsgId : void 0, currentMessageId: toolContext?.currentMessageId }); if (!ids) throw new Error("Zalouser react requires messageId + cliMsgId (or a current message context id)."); const result = await sendReactionZalouser({ profile: account.profile, threadId, isGroup: params.isGroup === true, msgId: ids.msgId, cliMsgId: ids.cliMsgId, emoji, remove: params.remove === true }); if (!result.ok) throw new Error(result.error || "Failed to react on Zalo message"); return { content: [{ type: "text", text: params.remove === true ? `Removed reaction ${emoji} from ${ids.msgId}` : `Reacted ${emoji} on ${ids.msgId}` }], details: { messageId: ids.msgId, cliMsgId: ids.cliMsgId, threadId } }; } }; const zalouserPlugin = createChatChannelPlugin({ base: { ...createZalouserPluginBase({ setupWizard: zalouserSetupWizard, setup: zalouserSetupAdapter }), groups: { resolveRequireMention: resolveZalouserRequireMention, resolveToolPolicy: resolveZalouserGroupToolPolicy }, actions: zalouserMessageActions, messaging: { normalizeTarget: (raw) => normalizeZalouserTarget(raw), resolveOutboundSessionRoute: (params) => resolveZalouserOutboundSessionRoute(params), targetResolver: { looksLikeId: (raw) => { const normalized = normalizeZalouserTarget(raw); if (!normalized) return false; if (/^group:[^\s]+$/i.test(normalized) || /^user:[^\s]+$/i.test(normalized)) return true; return isNumericTargetId(normalized); }, hint: "<user:id|group:id>" } }, directory: { self: async ({ cfg, accountId }) => { const parsed = await getZaloUserInfo(resolveZalouserAccountSync({ cfg, accountId }).profile); if (!parsed?.userId) return null; return mapUser({ id: String(parsed.userId), name: parsed.displayName ?? null, avatarUrl: parsed.avatar ?? null, raw: parsed }); }, listPeers: async ({ cfg, accountId, query, limit }) => { const rows = (await listZaloFriendsMatching(resolveZalouserAccountSync({ cfg, accountId }).profile, query)).map((friend) => mapUser({ id: String(friend.userId), name: friend.displayName ?? null, avatarUrl: friend.avatar ?? null, raw: friend })); return typeof limit === "number" && limit > 0 ? rows.slice(0, limit) : rows; }, listGroups: async ({ cfg, accountId, query, limit }) => { const rows = (await listZaloGroupsMatching(resolveZalouserAccountSync({ cfg, accountId }).profile, query)).map((group) => mapGroup({ id: `group:${String(group.groupId)}`, name: group.name ?? null, raw: group })); return typeof limit === "number" && limit > 0 ? rows.slice(0, limit) : rows; }, listGroupMembers: async ({ cfg, accountId, groupId, limit }) => { const account = resolveZalouserAccountSync({ cfg, accountId }); const normalizedGroupId = parseZalouserDirectoryGroupId(groupId); const rows = (await listZaloGroupMembers(account.profile, normalizedGroupId)).map((member) => mapUser({ id: member.userId, name: member.displayName, avatarUrl: member.avatar ?? null, raw: member })); return typeof limit === "number" && limit > 0 ? rows.slice(0, limit) : rows; } }, resolver: { resolveTargets: async ({ cfg, accountId, inputs, kind, runtime }) => { const results = []; for (const input of inputs) { const trimmed = input.trim(); if (!trimmed) { results.push({ input, resolved: false, note: "empty input" }); continue; } if (/^\d+$/.test(trimmed)) { results.push({ input, resolved: true, id: trimmed }); continue; } try { const account = resolveZalouserAccountSync({ cfg, accountId: accountId ?? "default" }); if (kind === "user") { const friends = await listZaloFriendsMatching(account.profile, trimmed); const best = friends[0]; results.push({ input, resolved: Boolean(best?.userId), id: best?.userId, name: best?.displayName, note: friends.length > 1 ? "multiple matches; chose first" : void 0 }); } else { const groups = await listZaloGroupsMatching(account.profile, trimmed); const best = groups.find((group) => group.name.toLowerCase() === trimmed.toLowerCase()) ?? groups[0]; results.push({ input, resolved: Boolean(best?.groupId), id: best?.groupId, name: best?.name, note: groups.length > 1 ? "multiple matches; chose first" : void 0 }); } } catch (err) { runtime.error?.(`zalouser resolve failed: ${String(err)}`); results.push({ input, resolved: false, note: "lookup failed" }); } } return results; } }, auth: { login: async ({ cfg, accountId, runtime }) => { const account = resolveZalouserAccountSync({ cfg, accountId: accountId ?? "default" }); runtime.log(`Generating QR login for Zalo Personal (account: ${account.accountId}, profile: ${account.profile})...`); const started = await startZaloQrLogin({ profile: account.profile, timeoutMs: 35e3 }); if (!started.qrDataUrl) throw new Error(started.message || "Failed to start QR login"); const qrPath = await writeQrDataUrlToTempFile(started.qrDataUrl, account.profile); if (qrPath) runtime.log(`Scan QR image: ${qrPath}`); else runtime.log("QR generated but could not be written to a temp file."); const waited = await waitForZaloQrLogin({ profile: account.profile, timeoutMs: 18e4 }); if (!waited.connected) throw new Error(waited.message || "Zalouser login failed"); runtime.log(waited.message); } }, status: createAsyncComputedAccountStatusAdapter({ defaultRuntime: createDefaultChannelRuntimeState(DEFAULT_ACCOUNT_ID), collectStatusIssues: collectZalouserStatusIssues, buildChannelSummary: ({ snapshot }) => buildPassiveProbedChannelStatusSummary(snapshot), probeAccount: async ({ account, timeoutMs }) => probeZalouser(account.profile, timeoutMs), resolveAccountSnapshot: async ({ account, runtime }) => { const configured = await checkZaloAuthenticated(account.profile); return { accountId: account.accountId, name: account.name, enabled: account.enabled, configured, extra: { dmPolicy: account.config.dmPolicy ?? "pairing", lastError: configured ? runtime?.lastError ?? null : runtime?.lastError ?? "not authenticated" } }; } }), gateway: { startAccount: async (ctx) => { const account = ctx.account; let userLabel = ""; try { const userInfo = await getZcaUserInfo(account.profile); if (userInfo?.displayName) userLabel = ` (${userInfo.displayName})`; ctx.setStatus({ accountId: account.accountId, profile: userInfo }); } catch {} const statusSink = createAccountStatusSink({ accountId: ctx.accountId, setStatus: ctx.setStatus }); ctx.log?.info(`[${account.accountId}] starting zalouser provider${userLabel}`); const { monitorZalouserProvider } = await import("../../monitor-B7vCDw_t.js"); return monitorZalouserProvider({ account, config: ctx.cfg, runtime: ctx.runtime, abortSignal: ctx.abortSignal, statusSink }); }, loginWithQrStart: async (params) => { return await startZaloQrLogin({ profile: resolveZalouserQrProfile(params.accountId), force: params.force, timeoutMs: params.timeoutMs }); }, loginWithQrWait: async (params) => { return await waitForZaloQrLogin({ profile: resolveZalouserQrProfile(params.accountId), timeoutMs: params.timeoutMs }); }, logoutAccount: async (ctx) => await logoutZaloProfile(ctx.account.profile || resolveZalouserQrProfile(ctx.accountId)) } }, security: { resolveDmPolicy: resolveZalouserDmPolicy }, threading: { resolveReplyToMode: createStaticReplyToModeResolver("off") }, pairing: { text: { idLabel: "zalouserUserId", message: "Your pairing request has been approved.", normalizeAllowEntry: createPairingPrefixStripper(/^(zalouser|zlu):/i), notify: async ({ cfg, id, message }) => { const account = resolveZalouserAccountSync({ cfg }); if (!await checkZaloAuthenticated(account.profile)) throw new Error("Zalouser not authenticated"); await sendMessageZalouser(id, message, { profile: account.profile }); } } }, outbound: { deliveryMode: "direct", chunker: (text, limit) => getZalouserRuntime().channel.text.chunkMarkdownText(text, limit), chunkerMode: "markdown", sendPayload: async (ctx) => await sendPayloadWithChunkedTextAndMedia({ ctx, sendText: (nextCtx) => zalouserRawSendResultAdapter.sendText(nextCtx), sendMedia: (nextCtx) => zalouserRawSendResultAdapter.sendMedia(nextCtx), emptyResult: createEmptyChannelResult("zalouser") }), ...zalouserRawSendResultAdapter } }); //#endregion //#region extensions/zalouser/src/tool.ts const ACTIONS = [ "send", "image", "link", "friends", "groups", "me", "status" ]; function stringEnum(values, options = {}) { return Type.Unsafe({ type: "string", enum: [...values], ...options }); } const ZalouserToolSchema = Type.Object({ action: stringEnum(ACTIONS, { description: `Action to perform: ${ACTIONS.join(", ")}` }), threadId: Type.Optional(Type.String({ description: "Thread ID for messaging" })), message: Type.Optional(Type.String({ description: "Message text" })), isGroup: Type.Optional(Type.Boolean({ description: "Is group chat" })), profile: Type.Optional(Type.String({ description: "Profile name" })), query: Type.Optional(Type.String({ description: "Search query" })), url: Type.Optional(Type.String({ description: "URL for media/link" })) }, { additionalProperties: false }); function json(payload) { return { content: [{ type: "text", text: JSON.stringify(payload, null, 2) }], details: payload }; } async function executeZalouserTool(_toolCallId, params, _signal, _onUpdate) { try { switch (params.action) { case "send": { if (!params.threadId || !params.message) throw new Error("threadId and message required for send action"); const result = await sendMessageZalouser(params.threadId, params.message, { profile: params.profile, isGroup: params.isGroup }); if (!result.ok) throw new Error(result.error || "Failed to send message"); return json({ success: true, messageId: result.messageId }); } case "image": { if (!params.threadId) throw new Error("threadId required for image action"); if (!params.url) throw new Error("url required for image action"); const result = await sendImageZalouser(params.threadId, params.url, { profile: params.profile, caption: params.message, isGroup: params.isGroup }); if (!result.ok) throw new Error(result.error || "Failed to send image"); return json({ success: true, messageId: result.messageId }); } case "link": { if (!params.threadId || !params.url) throw new Error("threadId and url required for link action"); const result = await sendLinkZalouser(params.threadId, params.url, { profile: params.profile, caption: params.message, isGroup: params.isGroup }); if (!result.ok) throw new Error(result.error || "Failed to send link"); return json({ success: true, messageId: result.messageId }); } case "friends": return json(await listZaloFriendsMatching(params.profile, params.query)); case "groups": return json(await listZaloGroupsMatching(params.profile, params.query)); case "me": return json(await getZaloUserInfo(params.profile) ?? { error: "Not authenticated" }); case "status": { const authenticated = await checkZaloAuthenticated(params.profile); return json({ authenticated, output: authenticated ? "authenticated" : "not authenticated" }); } default: params.action; throw new Error(`Unknown action: ${String(params.action)}. Valid actions: send, image, link, friends, groups, me, status`); } } catch (err) { return json({ error: err instanceof Error ? err.message : String(err) }); } } //#endregion //#region extensions/zalouser/index.ts var zalouser_default = defineChannelPluginEntry({ id: "zalouser", name: "Zalo Personal", description: "Zalo personal account messaging via native zca-js integration", plugin: zalouserPlugin, setRuntime: setZalouserRuntime, registerFull(api) { api.registerTool({ name: "zalouser", label: "Zalo Personal", description: "Send messages and access data via Zalo personal account. Actions: send (text message), image (send image URL), link (send link), friends (list/search friends), groups (list groups), me (profile info), status (auth check).", parameters: ZalouserToolSchema, execute: executeZalouserTool }); } }); //#endregion export { zalouser_default as default, setZalouserRuntime, zalouserPlugin };