UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

189 lines (188 loc) • 6.05 kB
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; import { d as asPositiveFiniteNumber } from "./number-coercion-D6u-4YD8.mjs"; import { g as normalizeUniqueStringEntries } from "./string-normalization-DLmfYZXI.mjs"; import { b as resolveGatewayPort, n as DEFAULT_GATEWAY_PORT } from "./paths-DAcAg5aV.mjs"; import { i as resolveExecutable } from "./executable-path-D8oWZRS5.mjs"; import { i as resolveTrustedWindowsCmdExe, n as isWindowsBatchCommand, t as buildWindowsCmdExeCommandLine } from "./windows-command-BgNYMnai.mjs"; import "./config-CMA-J084.mjs"; //#region src/hooks/gmail.ts const DEFAULT_GMAIL_LABEL = "INBOX"; const DEFAULT_GMAIL_TOPIC = "gog-gmail-watch"; const DEFAULT_GMAIL_SUBSCRIPTION = "gog-gmail-watch-push"; const DEFAULT_GMAIL_SERVE_BIND = "127.0.0.1"; const DEFAULT_GMAIL_SERVE_PORT = 8788; const DEFAULT_GMAIL_SERVE_PATH = "/gmail-pubsub"; const DEFAULT_GMAIL_MAX_BYTES = 2e4; const DEFAULT_GMAIL_RENEW_MINUTES = 720; const DEFAULT_HOOKS_PATH = "/hooks"; const GMAIL_WATCH_EXCLUDED_LABELS = "SPAM,TRASH,DRAFT,SENT"; const GMAIL_WATCH_SENSITIVE_FLAGS = /* @__PURE__ */ new Set([ "--token", "--hook-url", "--hook-token" ]); let gogBin; /** Resolve the per-message body byte bound gog is provisioned with (`--max-bytes`). */ function resolveGmailHookMaxBytes(e) { return Math.floor(asPositiveFiniteNumber(e) ?? 2e4); } function mergeHookPresets(e, t) { const n = new Set(normalizeUniqueStringEntries(e)); n.add(t); return Array.from(n); } function normalizeHooksPath(e) { const t = e?.trim() || DEFAULT_HOOKS_PATH; if (t === "/") return DEFAULT_HOOKS_PATH; return (t.startsWith("/") ? t : `/${t}`).replace(/\/+$/, ""); } function normalizeServePath(e) { const t = e?.trim() || "/gmail-pubsub"; if (t === "/") return "/"; return (t.startsWith("/") ? t : `/${t}`).replace(/\/+$/, ""); } function buildDefaultHookUrl(e, t = DEFAULT_GATEWAY_PORT) { const n = normalizeHooksPath(e); const r = new URL(`http://127.0.0.1:${t}`); r.pathname = `${n}/gmail`; return r.toString(); } function resolveGmailHookRuntimeConfig(e, t) { const n = e.hooks; const r = n?.gmail; const i = t.hookToken ?? n?.token ?? ""; if (!i) return { ok: false, error: "hooks.token missing (needed for gmail hook)" }; const a = t.account ?? r?.account ?? ""; if (!a) return { ok: false, error: "gmail account required" }; const o = t.topic ?? r?.topic ?? ""; if (!o) return { ok: false, error: "gmail topic required" }; const s = t.subscription ?? r?.subscription ?? "gog-gmail-watch-push"; const c = t.pushToken ?? r?.pushToken ?? ""; if (!c) return { ok: false, error: "gmail push token required" }; const l = t.hookUrl ?? r?.hookUrl ?? buildDefaultHookUrl(n?.path, resolveGatewayPort(e)); const u = t.includeBody ?? r?.includeBody ?? true; const d = resolveGmailHookMaxBytes(t.maxBytes ?? r?.maxBytes); const f = Math.floor(asPositiveFiniteNumber(t.renewEveryMinutes ?? r?.renewEveryMinutes) ?? 720); const p = t.serveBind ?? r?.serve?.bind ?? "127.0.0.1"; const m = Math.floor(asPositiveFiniteNumber(t.servePort ?? r?.serve?.port) ?? 8788); const h = normalizeServePath(normalizeOptionalString(t.servePath ?? r?.serve?.path)); const g = t.tailscaleTarget ?? r?.tailscale?.target; const _ = t.tailscaleMode ?? r?.tailscale?.mode ?? "off"; const v = _ !== "off" ? normalizeOptionalString(g) : void 0; const y = normalizeServePath(_ !== "off" && !v ? "/" : h); const b = t.tailscalePath ?? r?.tailscale?.path; const x = normalizeServePath(_ !== "off" ? b ?? h : b ?? y); return { ok: true, value: { account: a, label: t.label ?? r?.label ?? "INBOX", topic: o, subscription: s, pushToken: c, hookToken: i, hookUrl: l, includeBody: u, maxBytes: d, renewEveryMinutes: f, serve: { bind: p, port: m, path: y }, tailscale: { mode: _, path: x, target: v } } }; } function buildGogWatchStartArgs(e) { return [ "gmail", "watch", "start", "--account", e.account, "--label", e.label, "--topic", e.topic ]; } function buildGogWatchServeArgs(e) { const t = [ "gmail", "watch", "serve", "--account", e.account, "--bind", e.serve.bind, "--port", String(e.serve.port), "--path", e.serve.path, "--token", e.pushToken, "--hook-url", e.hookUrl, "--hook-token", e.hookToken ]; if (e.includeBody) t.push("--include-body"); t.push("--exclude-labels", GMAIL_WATCH_EXCLUDED_LABELS); if (e.maxBytes > 0) t.push("--max-bytes", String(e.maxBytes)); return t; } function buildGogWatchServeLogArgs(e) { return buildGogWatchServeArgs(e).filter((e, t, n) => !GMAIL_WATCH_SENSITIVE_FLAGS.has(e) && !GMAIL_WATCH_SENSITIVE_FLAGS.has(n[t - 1] ?? "")); } function resolveGogExecutable() { return gogBin ??= resolveExecutable("gog"); } function resolveGogServeInvocation(e) { const t = resolveGogExecutable(); if (!isWindowsBatchCommand(t)) return { command: t, args: e, windowsHide: process.platform === "win32" ? true : void 0 }; return { command: resolveTrustedWindowsCmdExe(), args: [ "/d", "/s", "/c", buildWindowsCmdExeCommandLine(t, e) ], windowsHide: true, windowsVerbatimArguments: true }; } function buildTopicPath(e, t) { return `projects/${e}/topics/${t}`; } function parseTopicPath(e) { const t = e.trim().match(/^projects\/([^/]+)\/topics\/([^/]+)$/i); if (!t) return null; return { projectId: t[1] ?? "", topicName: t[2] ?? "" }; } //#endregion export { resolveGmailHookMaxBytes as _, DEFAULT_GMAIL_SERVE_PORT as a, resolveGogServeInvocation as b, buildDefaultHookUrl as c, buildGogWatchStartArgs as d, buildTopicPath as f, parseTopicPath as g, normalizeServePath as h, DEFAULT_GMAIL_SERVE_PATH as i, buildGogWatchServeArgs as l, normalizeHooksPath as m, DEFAULT_GMAIL_MAX_BYTES as n, DEFAULT_GMAIL_SUBSCRIPTION as o, mergeHookPresets as p, DEFAULT_GMAIL_SERVE_BIND as r, DEFAULT_GMAIL_TOPIC as s, DEFAULT_GMAIL_LABEL as t, buildGogWatchServeLogArgs as u, resolveGmailHookRuntimeConfig as v, resolveGogExecutable as y };