UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

166 lines (165 loc) • 6.14 kB
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; import { l as resolveSessionStorePathCore } from "./paths-DvQ5AJvD.mjs"; import { t as formatSqliteSessionFileMarker } from "./legacy-sqlite-marker-D_RUp-nb.mjs"; import { a as hasOutboundReplyContent } from "./reply-payload-CwASJYas.mjs"; import "./string-coerce-runtime-BwAZZDf5.mjs"; import { n as getSessionEntryAsync } from "./session-store-runtime-internal-DEwSyDjM.mjs"; import "./session-store-runtime-D5peOAR8.mjs"; import { n as withTelegramApiErrorLogging } from "./api-logging-D-u3hsql.mjs"; import { o as recordSentMessage } from "./send-outbound-8-7P0nHN.mjs"; import { r as buildTelegramGroupFrom, s as buildTelegramRoutingTarget } from "./helpers-CoGccTxe.mjs"; import { h as buildInlineKeyboard } from "./text-chunk-limit-DTiKmKhG.mjs"; import { d as shouldSuppressLocalTelegramExecApprovalPrompt } from "./exec-approvals-Crn--YHh.mjs"; import { t as loadTelegramSendModule } from "./send-runtime-CZEXtMaJ.mjs"; import { n as resolveTelegramNativeCommandThreadContext, t as prepareTelegramCommandDispatch } from "./bot-native-command-dispatch-C7b8k2Gj.mjs"; import { randomUUID } from "node:crypto"; //#region extensions/telegram/src/bot-native-command-plugins.ts const EMPTY_RESPONSE_FALLBACK = "No response generated. Please try again."; function inspectTelegramNativeReply(e) { const t = e.channelData?.telegram; const n = Boolean(normalizeOptionalString(t?.reaction?.emoji)); const { channelData: r, ...i } = e; return { telegramData: t, hasReaction: n, renderable: hasOutboundReplyContent(i, { trimText: true }) || Boolean(buildInlineKeyboard(t?.buttons) || n), editable: !e.mediaUrl && (!e.mediaUrls || e.mediaUrls.length === 0) && !e.presentation && !e.interactive && !e.btw && !n && t?.pin !== true }; } async function resolveTelegramCommandTranscriptContext(e) { const t = e.sessionKey.trim(); if (!t) return {}; try { const n = resolveSessionStorePathCore(e.cfg.session?.store, { agentId: e.agentId }); const r = await getSessionEntryAsync({ agentId: e.agentId, sessionKey: t, storePath: n }); const i = r?.sessionId?.trim() || randomUUID(); const a = formatSqliteSessionFileMarker({ agentId: e.agentId, sessionId: i, storePath: n }); const o = normalizeOptionalString(r?.authProfileOverride); return { sessionId: i, sessionFile: a, ...o ? { authProfileId: o } : {} }; } catch { return {}; } } async function executeTelegramPluginCommand(e) { const t = `/${e.commandName}${e.rawText ? ` ${e.rawText}` : ""}`; const n = e.candidate.prepareDispatch(e.rawText); if (n.kind === "non-plugin") { await withTelegramApiErrorLogging({ operation: "sendMessage", runtime: e.runtime, fn: async () => await e.bot.api.sendMessage(e.msg.chat.id, "Command not found.", (await resolveTelegramNativeCommandThreadContext(e)).threadParams ?? {}) }); return; } const r = await prepareTelegramCommandDispatch({ ...e, requireAuth: e.candidate.requireAuth }); if (!r) return; const i = r.isGroup ? buildTelegramGroupFrom(r.chatId, r.threadSpec) : `telegram:${r.chatId}`; const a = r.threadSpec.scope === "direct-messages" ? buildTelegramRoutingTarget(r.chatId, r.threadSpec) : `telegram:${r.chatId}`; const { deliverReplies: o, emitTelegramMessageSentHooks: s } = await r.loadDeliveryRuntime(); let c; const cleanupProgressPlaceholder = async () => { const e = c; if (e == null) return; try { await withTelegramApiErrorLogging({ operation: "deleteMessage", runtime: r.runtime, fn: () => r.bot.api.deleteMessage(r.chatId, e) }); } catch {} }; if (e.candidate.progressMessage) try { c = (await withTelegramApiErrorLogging({ operation: "sendMessage", runtime: r.runtime, fn: () => r.bot.api.sendMessage(r.chatId, e.candidate.progressMessage, r.threadParams) })).message_id; } catch {} const l = await resolveTelegramCommandTranscriptContext({ cfg: r.runtimeCfg, agentId: r.route.agentId, sessionKey: r.targetSessionKey }); const u = await n.execute({ senderId: r.senderId, channel: "telegram", isAuthorizedSender: r.commandAuthorized, senderIsOwner: r.senderIsOwner, assertOwnerCurrent: r.assertOwnerCurrent, agentId: r.route.agentId, sessionKey: r.targetSessionKey, sessionId: l.sessionId, sessionFile: l.sessionFile, authProfileId: l.authProfileId, commandBody: t, config: r.runtimeCfg, from: i, to: a, accountId: r.accountId, messageThreadId: r.threadSpec.id }); if (shouldSuppressLocalTelegramExecApprovalPrompt({ cfg: r.runtimeCfg, accountId: r.route.accountId, payload: u }) || u.suppressReply === true) { await cleanupProgressPlaceholder(); return; } const { hasReaction: d, renderable: f, editable: p, telegramData: m } = inspectTelegramNativeReply(u); const h = f ? d && !normalizeOptionalString(u.replyToId) ? { ...u, replyToId: String(r.msg.message_id) } : u : { text: EMPTY_RESPONSE_FALLBACK }; const g = h.text; if (c != null && typeof g === "string" && g.trim().length > 0 && (!f || p)) try { const { editMessageTelegram: e } = await loadTelegramSendModule(); await e(r.chatId, c, g, { cfg: r.runtimeCfg, accountId: r.route.accountId, textMode: "markdown", linkPreview: r.runtimeTelegramCfg.linkPreview, buttons: f ? m?.buttons : void 0 }); await recordSentMessage(r.chatId, c, r.runtimeCfg, { accountId: r.route.accountId, agentId: r.opts.ownerAgentId }); s({ sessionKeyForInternalHooks: r.targetSessionKey, chatId: String(r.chatId), accountId: r.route.accountId, content: g, success: true, messageId: c, isGroup: r.isGroup, groupId: r.isGroup ? String(r.chatId) : void 0 }); await r.recordDeliveredReply(t, g, String(c)); return; } catch {} await cleanupProgressPlaceholder(); if ((await o({ replies: [h], ...r.deliveryOptions, ...d ? { replyToMode: "all" } : {}, silent: r.runtimeTelegramCfg.silentErrorReplies === true && h.isError === true })).delivered && h.text) await r.recordDeliveredReply(t, h.text, "final"); } //#endregion export { executeTelegramPluginCommand };