openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
151 lines (150 loc) • 5.53 kB
JavaScript
import { r as asNullableRecord } from "./record-coerce-CVPuDqcc.mjs";
import { t as DEFAULT_GROUP_HISTORY_LIMIT } from "./history-limit-D9prPWZZ.mjs";
import { o as normalizeChannelAccounts } from "./legacy-private-network-migration-CT-ZAc9V.mjs";
import { l as hasLegacyAccountStreamingAliases, o as createLegacyWebhookListenerDoctorContract } from "./runtime-doctor-migrations-YTlcIEpR.mjs";
import { a as mergeTelegramAccountConfig, n as listTelegramAccountIds } from "./account-selection-CGYGB8bX.mjs";
import { n as resolveTelegramGatewayWebhookUrl, r as resolveTelegramWebhookPathConflict, t as DEFAULT_TELEGRAM_WEBHOOK_PATH } from "./webhook-route-uIeBtETz.mjs";
//#region extensions/telegram/src/doctor-contract.ts
const webhookListenerMigration = createLegacyWebhookListenerDoctorContract({
channelKey: "telegram",
defaultPort: 8787,
defaultHost: "127.0.0.1"
});
const { historicalWebhookListener } = webhookListenerMigration;
const RETIRED_TUNING_KEYS = /* @__PURE__ */ new Set([
"timeoutSeconds",
"mediaGroupFlushMs",
"pollingStallThresholdMs",
"retry",
"errorCooldownMs"
]);
function stripRetiredTelegramTuning(e, t) {
let n = false;
const r = { ...e };
for (const e of t === "channel" || t === "account" ? RETIRED_TUNING_KEYS : ["errorCooldownMs"]) if (Object.hasOwn(r, e)) {
delete r[e];
n = true;
}
const i = t === "topic" ? [] : t === "chat" ? ["topics"] : ["groups", "direct"];
if (t === "channel") i.push("accounts");
for (const t of i) {
const i = asNullableRecord(e[t]);
if (!i) continue;
const a = { ...i };
for (const [e, o] of Object.entries(i)) {
const i = asNullableRecord(o);
if (!i) continue;
const s = stripRetiredTelegramTuning(i, t === "accounts" ? "account" : t === "topics" ? "topic" : "chat");
if (s.changed) {
a[e] = s.entry;
r[t] = a;
n = true;
}
}
}
return {
entry: n ? r : e,
changed: n
};
}
function hasRetiredTelegramGroupHistoryContextConfig(e) {
return asNullableRecord(e)?.includeGroupHistoryContext !== void 0;
}
function removeRetiredTelegramGroupHistoryContextConfig(e) {
if (e.entry.includeGroupHistoryContext === void 0) return {
entry: e.entry,
changed: false
};
const { includeGroupHistoryContext: t, ...n } = e.entry;
const r = t === "none" ? 0 : t === "recent" && e.preserveRecentHistoryLimit !== void 0 && e.entry.historyLimit === void 0 ? e.preserveRecentHistoryLimit : void 0;
const i = r === void 0 ? n : {
...n,
historyLimit: r
};
const a = r === void 0 ? "" : ` and set historyLimit to ${r}`;
e.changes.push(`Removed ${e.pathPrefix}.includeGroupHistoryContext${a}; Telegram group history is always on for groups and bounded by historyLimit.`);
return {
entry: i,
changed: true
};
}
const legacyConfigRules = [
...webhookListenerMigration.legacyConfigRules,
{
path: ["channels", "telegram"],
message: "channels.telegram.includeGroupHistoryContext was removed; Telegram group history is always on for groups and bounded by historyLimit. Run \"openclaw doctor --fix\".",
match: hasRetiredTelegramGroupHistoryContextConfig
},
{
path: [
"channels",
"telegram",
"accounts"
],
message: "channels.telegram.accounts.<id>.includeGroupHistoryContext was removed; Telegram group history is always on for groups and bounded by historyLimit. Run \"openclaw doctor --fix\".",
match: (e) => hasLegacyAccountStreamingAliases(e, hasRetiredTelegramGroupHistoryContextConfig)
}
];
function normalizeHistoricalWebhookConfig({ cfg: e }) {
const t = e.channels?.telegram?.enabled === false ? [] : listTelegramAccountIds(e).filter((t) => {
const n = mergeTelegramAccountConfig(e, t);
if (n.enabled === false || !n.webhookUrl?.trim()) return false;
const r = n.webhookPath ?? "/telegram-webhook";
const i = resolveTelegramGatewayWebhookUrl(e, r);
return !i || URL.parse(n.webhookUrl)?.href !== i || resolveTelegramWebhookPathConflict(r) !== void 0;
});
return {
...webhookListenerMigration.normalizeCompatibilityConfig({ cfg: e }),
historicalWebhookAccountIds: t
};
}
function normalizeCompatibilityConfig({ cfg: e }) {
const t = normalizeHistoricalWebhookConfig({ cfg: e });
const { historicalWebhookAccountIds: n } = t;
const r = [...t.changes];
const i = asNullableRecord(t.config.channels?.telegram);
if (!i) return {
config: e,
changes: [],
historicalWebhookAccountIds: n
};
const a = stripRetiredTelegramTuning(i, "channel");
let o = a.entry;
if (a.changed) r.push("Removed retired Telegram tuning knobs.");
const s = o.includeGroupHistoryContext;
const c = typeof o.historyLimit === "number" ? o.historyLimit : e.messages?.groupChat?.historyLimit ?? 50;
o = removeRetiredTelegramGroupHistoryContextConfig({
entry: o,
pathPrefix: "channels.telegram",
changes: r
}).entry;
o = normalizeChannelAccounts({
entry: o,
pathPrefix: "channels.telegram",
changes: r,
normalizeAccount: ({ account: e, pathPrefix: t, changes: n }) => removeRetiredTelegramGroupHistoryContextConfig({
entry: e,
pathPrefix: t,
changes: n,
...s === "none" ? { preserveRecentHistoryLimit: c } : {}
})
}).entry;
if (t.config === e && o === i && r.length === 0) return {
config: e,
changes: [],
historicalWebhookAccountIds: n
};
return {
config: {
...t.config,
channels: {
...t.config.channels,
telegram: o
}
},
changes: r,
historicalWebhookAccountIds: n
};
}
//#endregion
export { normalizeHistoricalWebhookConfig as i, legacyConfigRules as n, normalizeCompatibilityConfig as r, historicalWebhookListener as t };