UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

203 lines (202 loc) • 7.14 kB
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; import { l as resolveSessionStorePathCore } from "./paths-DvQ5AJvD.mjs"; import { r as resolveAgentConfig } from "./agent-scope-config-DmXgFGKP.mjs"; import { n as resolveDefaultModelForAgent } from "./model-selection-config-Bvf5WqHo.mjs"; import { o as resolveEffectiveAgentRuntime } from "./thinking-runtime-BKWl4CUs.mjs"; import { r as resolveStoredModelOverrideAsync } from "./stored-model-overrides-CyqayMPa.mjs"; import { a as formatFastModeCurrentStatus } from "./fast-mode-Dk3A3IUk.mjs"; import { t as resolveFastModeState } from "./fast-mode-BcwTQ_IZ.mjs"; import "./string-coerce-runtime-BwAZZDf5.mjs"; import { f as resolveCommandArgMenuAsync, i as formatCommandArgMenuTitle, l as parseCommandArgs, n as canResolveCommandArgMenu, r as findCommandByNativeName, t as buildCommandTextFromArgs } from "./commands-registry-CwAxsNJ9.mjs"; import { n as getSessionEntryAsync } from "./session-store-runtime-internal-DEwSyDjM.mjs"; import "./session-store-runtime-D5peOAR8.mjs"; import { r as resolveThinkingDefaultWithRuntimeCatalog } from "./agent-runtime-DA4W1L0G.mjs"; import "./command-auth-native-DT07BMZD.mjs"; import { n as withTelegramApiErrorLogging } from "./api-logging-D-u3hsql.mjs"; import { h as buildInlineKeyboard } from "./text-chunk-limit-DTiKmKhG.mjs"; import { t as buildTelegramNativeCommandCallbackData } from "./native-command-callback-data-DMBSeb_1.mjs"; import { t as prepareTelegramCommandDispatch } from "./bot-native-command-dispatch-C7b8k2Gj.mjs"; //#region extensions/telegram/src/bot-native-command-builtins.ts async function resolveTelegramCommandMenuModelContext(e, t = "menu") { const n = t === "fast" ? resolveDefaultModelForAgent(e) : void 0; const r = n ? { provider: n.provider, model: n.model } : {}; if (!e.sessionKey.trim()) return r; try { const r = resolveSessionStorePathCore(e.cfg.session?.store, { agentId: e.agentId }); const i = n ?? resolveDefaultModelForAgent(e); const a = await getSessionEntryAsync({ storePath: r, sessionKey: e.sessionKey }); const o = normalizeOptionalString(a?.thinkingLevel); const s = a?.fastMode; let c; if (a?.modelOverrideSource === "auto" && normalizeOptionalString(a.modelOverride)) c = { provider: i.provider, model: i.model }; else { const n = await resolveStoredModelOverrideAsync({ sessionEntry: a, loadSessionEntry: (e) => getSessionEntryAsync({ storePath: r, sessionKey: e }), sessionKey: e.sessionKey, defaultProvider: i.provider }); if (t === "fast") return { provider: n?.provider ?? i.provider, model: n?.model ?? i.model, sessionEntry: a }; if (n?.model) c = { provider: n.provider || i.provider, model: n.model }; else { const e = normalizeOptionalString(a?.providerOverride) ?? normalizeOptionalString(a?.modelProvider); const t = normalizeOptionalString(a?.modelOverride) ?? normalizeOptionalString(a?.model); c = { ...e ? { provider: e } : {}, ...t ? { model: t } : {} }; } } if (t === "fast") return { ...c, sessionEntry: a }; return { ...c, ...o ? { thinkingLevel: o } : {}, ...s !== void 0 ? { fastMode: s } : {}, agentRuntime: resolveEffectiveAgentRuntime({ cfg: e.cfg, provider: c.provider ?? i.provider, modelId: c.model ?? i.model, agentId: e.agentId, sessionKey: e.sessionKey, sessionEntry: a }) }; } catch { return r; } } async function executeTelegramBuiltinCommand(e) { const t = findCommandByNativeName(e.commandName, "telegram", { includeBundledChannelFallback: false }); if (!t) return "fall-through"; const n = parseCommandArgs(t, e.rawText); const r = buildCommandTextFromArgs(t, n); if (t.key !== "login" && !canResolveCommandArgMenu({ command: t, args: n })) return "fall-through"; if (t.key === "login" && e.shouldSkip?.()) return "handled"; const i = await prepareTelegramCommandDispatch({ ...e, requireAuth: true }); if (!i) return "handled"; if (t.key === "login") { const { executeTelegramLoginCommand: e } = await import("./bot-native-command-login-BLYqXPHC.mjs"); return await e({ dispatch: i, commandText: r, currentProvider: (await resolveTelegramCommandMenuModelContext({ cfg: i.runtimeCfg, agentId: i.route.agentId, sessionKey: i.targetSessionKey })).provider ?? resolveDefaultModelForAgent({ cfg: i.runtimeCfg, agentId: i.route.agentId }).provider }) ? "handled-clear-buttons" : "handled"; } const a = t.argsMenu && !(n?.raw && !n.values) && t.args?.some((e) => typeof e.choices === "function" && n?.values?.[e.name] == null); const o = { cfg: i.runtimeCfg, agentId: i.route.agentId, sessionKey: a ? i.targetSessionKey : "" }; const s = a ? await resolveTelegramCommandMenuModelContext(o, t.key === "fast" ? "fast" : "menu") : {}; const c = await resolveCommandArgMenuAsync({ command: t, args: n, cfg: i.runtimeCfg, session: { agentId: i.route.agentId, sessionKey: i.targetSessionKey }, ...s }); if (c) { if (e.shouldSkip?.()) return "handled"; let n; if (t.key === "think") { const e = c.modelCatalog ?? []; n = normalizeOptionalString(s.thinkingLevel); if (!n) n = normalizeOptionalString(resolveAgentConfig(i.runtimeCfg, i.route.agentId)?.thinkingDefault); if (!n) { const t = resolveDefaultModelForAgent({ cfg: i.runtimeCfg, agentId: i.route.agentId }); n = await resolveThinkingDefaultWithRuntimeCatalog({ cfg: i.runtimeCfg, agentId: i.route.agentId, provider: s.provider ?? t.provider, model: s.model ?? t.model, agentRuntime: s.agentRuntime, loadModelCatalog: async () => e }); } } let a; if (t.key === "fast") { const e = resolveDefaultModelForAgent({ cfg: i.runtimeCfg, agentId: i.route.agentId }); a = formatFastModeCurrentStatus(resolveFastModeState({ cfg: i.runtimeCfg, provider: s.provider ?? e.provider, model: s.model ?? e.model, agentId: i.route.agentId, sessionEntry: s.sessionEntry })); } let o = formatCommandArgMenuTitle({ command: t, menu: c }); if (t.key === "think" && n) o = `Current thinking level: ${n}.\n${o}`; else if (t.key === "fast" && a) { const e = c.choices.map((e) => e.label.trim()).filter(Boolean).join(", "); o = e ? `${a}\nOptions: ${e}.` : a; } const l = []; for (let e = 0; e < c.choices.length; e += 2) l.push(c.choices.slice(e, e + 2).map((e) => ({ text: e.label, callback_data: buildTelegramNativeCommandCallbackData(buildCommandTextFromArgs(t, { values: { [c.arg.name]: e.value } })) }))); const u = buildInlineKeyboard(l); await withTelegramApiErrorLogging({ operation: "sendMessage", runtime: i.runtime, fn: () => i.bot.api.sendMessage(i.chatId, o, { ...u ? { reply_markup: u } : {}, ...i.threadParams }) }); await i.recordDeliveredReply(r, `${o}\n${c.choices.map((e) => e.label).join(", ")}`, "menu"); return "handled"; } return "fall-through"; } //#endregion export { executeTelegramBuiltinCommand };