UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

250 lines (249 loc) • 9.1 kB
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; import { t as isPromiseLike } from "./promise-like-aMoWEh5w.mjs"; import { i as getPluginRegistryForContext } from "./gateway-request-scope-BvvDhs5D.mjs"; import { t as createSubsystemLogger } from "./subsystem-BHJiy01V.mjs"; import { G as buildPluginAgentTurnPrepareContext } from "./runtime-ezCEpumg.mjs"; import { t as isPluginJsonValue } from "./host-hook-json-BdnpRWog.mjs"; import { t as normalizeSessionEntrySlotKey } from "./session-entry-slot-keys-4srnoW5y.mjs"; import "./session-accessor-CizTbDQj.mjs"; import { n as readResolvedSessionEntryInWorker } from "./session-accessor.entry-read-runtime-y5p3nnUX.mjs"; import { o as updateResolvedSessionEntry } from "./session-accessor.entry-f0bkn7cv.mjs"; import { n as resolvePromptInjectionAllowed } from "./hook-policy-decisions-CQDTD66O.mjs"; import { randomUUID } from "node:crypto"; //#region src/plugins/host-hook-state.ts const log = createSubsystemLogger("plugins/host-hook-state"); const MAX_PLUGIN_NEXT_TURN_INJECTION_TEXT_LENGTH = 32768; const MAX_PLUGIN_NEXT_TURN_INJECTION_IDEMPOTENCY_KEY_LENGTH = 512; const MAX_PLUGIN_NEXT_TURN_INJECTIONS_PER_SESSION = 32; function isPluginNextTurnInjectionPlacement(e) { return e === "prepend_context" || e === "append_context"; } function isPluginNextTurnInjectionRecord(e) { if (!e || typeof e !== "object") return false; const t = e; return typeof t.id === "string" && typeof t.pluginId === "string" && typeof t.text === "string" && typeof t.createdAt === "number" && Number.isFinite(t.createdAt) && isPluginNextTurnInjectionPlacement(t.placement) && (t.ttlMs === void 0 || typeof t.ttlMs === "number" && Number.isFinite(t.ttlMs) && t.ttlMs >= 0) && (t.idempotencyKey === void 0 || typeof t.idempotencyKey === "string"); } function isExpired(e, t) { if (!isPluginNextTurnInjectionRecord(e)) return true; return e.ttlMs !== void 0 && t - e.createdAt > e.ttlMs; } async function enqueuePluginNextTurnInjection(e) { const t = normalizeOptionalString(e.injection.sessionKey) ?? ""; if (!t) return { enqueued: false, id: "", sessionKey: t }; const n = normalizeOptionalString(e.injection.text); if (!n || n.length > MAX_PLUGIN_NEXT_TURN_INJECTION_TEXT_LENGTH || e.injection.metadata !== void 0 && !isPluginJsonValue(e.injection.metadata) || e.injection.idempotencyKey !== void 0 && (typeof e.injection.idempotencyKey !== "string" || e.injection.idempotencyKey.trim().length === 0 || e.injection.idempotencyKey.length > MAX_PLUGIN_NEXT_TURN_INJECTION_IDEMPOTENCY_KEY_LENGTH) || e.injection.placement !== void 0 && !isPluginNextTurnInjectionPlacement(e.injection.placement) || e.injection.ttlMs !== void 0 && (!Number.isFinite(e.injection.ttlMs) || e.injection.ttlMs < 0)) return { enqueued: false, id: "", sessionKey: t }; const r = e.now ?? Date.now(); const i = { ...e.injection }; const a = { id: i.idempotencyKey?.trim() || randomUUID(), pluginId: e.pluginId, pluginName: e.pluginName, text: n, idempotencyKey: i.idempotencyKey?.trim() || void 0, placement: i.placement ?? "prepend_context", ttlMs: i.ttlMs, createdAt: r, metadata: i.metadata }; const o = { cfg: e.cfg, sessionKey: t, agentId: e.injection.agentId }; const s = await updateResolvedSessionEntry(o, (t) => { const n = { ...t.pluginNextTurnInjections }; const i = n[e.pluginId]; const o = (Array.isArray(i) ? i : []).filter((e) => !isExpired(e, r)); const s = a.idempotencyKey ? o.find((e) => e.idempotencyKey === a.idempotencyKey) : void 0; const c = !s && o.length < MAX_PLUGIN_NEXT_TURN_INJECTIONS_PER_SESSION; n[e.pluginId] = c ? [...o, a] : o; t.pluginNextTurnInjections = n; if (c) t.updatedAt = r; return { enqueued: c, id: s?.id ?? a.id }; }); if (!s.found) return { enqueued: false, id: "", sessionKey: t }; return { ...s.result, sessionKey: s.canonicalKey }; } async function drainPluginNextTurnInjections(e) { const t = e.sessionKey?.trim(); if (!t) return []; const n = { cfg: e.cfg, sessionKey: t, agentId: e.agentId }; const r = await readResolvedSessionEntryInWorker(n); if (!r?.pluginNextTurnInjections || Object.keys(r.pluginNextTurnInjections).length === 0) return []; const i = Date.now(); const a = await updateResolvedSessionEntry(n, (t) => { if (!t?.pluginNextTurnInjections) return []; const n = new Set((getPluginRegistryForContext()?.plugins ?? []).filter((e) => e.status === "loaded").map((e) => e.id)); const r = []; for (const [a, o] of Object.entries(t.pluginNextTurnInjections)) { if (!n.has(a) || !resolvePromptInjectionAllowed(e.cfg.plugins?.entries?.[a]?.hooks)) continue; if (!Array.isArray(o)) continue; const t = o.filter((e) => !isExpired(e, i)); r.push(...t); } r.sort((e, t) => e.createdAt - t.createdAt); delete t.pluginNextTurnInjections; if (r.length > 0) t.updatedAt = i; return r; }); return a.found ? a.result : []; } async function drainPluginNextTurnInjectionContext(e) { const t = await drainPluginNextTurnInjections(e); return { queuedInjections: t, ...buildPluginAgentTurnPrepareContext({ queuedInjections: t }) }; } async function getPluginSessionExtensionState(e) { const t = e.pluginId.trim(); const n = normalizeOptionalString(e.sessionKey); if (!t || !n) return; const r = (await readResolvedSessionEntryInWorker({ cfg: e.cfg, sessionKey: n, agentId: e.agentId }))?.pluginExtensions?.[t]; return r ? structuredClone(r) : void 0; } async function patchPluginSessionExtension(e) { const t = e.namespace.trim(); const n = e.pluginId.trim(); if (!n || !t) return { ok: false, error: "pluginId and namespace are required" }; if (e.unset === true && e.value !== void 0) return { ok: false, error: "plugin session extension cannot specify both unset and value" }; if (e.value !== void 0 && !isPluginJsonValue(e.value)) return { ok: false, error: "plugin session extension value must be JSON-compatible" }; if (e.unset !== true && e.value === void 0) return { ok: false, error: "plugin session extension value is required unless unset is true" }; const r = e.value; const i = (getPluginRegistryForContext()?.sessionExtensions ?? []).find((e) => e.pluginId === n && e.extension.namespace === t); if (!i) return { ok: false, error: `unknown plugin session extension: ${n}/${t}` }; const a = i.extension.sessionEntrySlotKey; const o = await updateResolvedSessionEntry({ cfg: e.cfg, sessionKey: e.sessionKey, agentId: e.agentId }, (o, s) => { e.assertCurrent?.(); const c = o; const l = { ...o.pluginExtensions }; const u = { ...l[n] }; if (e.unset === true) delete u[t]; else u[t] = structuredClone(r); if (Object.keys(u).length > 0) l[n] = u; else delete l[n]; if (Object.keys(l).length > 0) o.pluginExtensions = l; else delete o.pluginExtensions; const d = { ...o.pluginExtensionSlotKeys }; const f = { ...d[n] }; const p = normalizeSessionEntrySlotKey(f[t]); if (p.ok && p.key !== a) delete c[p.key]; if (a && e.unset !== true) f[t] = a; else delete f[t]; if (Object.keys(f).length > 0) d[n] = f; else delete d[n]; if (Object.keys(d).length > 0) o.pluginExtensionSlotKeys = d; else delete o.pluginExtensionSlotKeys; if (a) { const t = projectSessionExtensionValue({ pluginId: i.pluginId, namespace: i.extension.namespace, project: i.extension.project, sessionKey: s.canonicalKey, sessionId: o.sessionId, state: e.unset === true ? void 0 : r }); if (t === void 0) delete c[a]; else c[a] = t; } o.updatedAt = Date.now(); return u[t]; }); if (!o.found) return { ok: false, error: `unknown session key: ${e.sessionKey}` }; return { ok: true, key: o.canonicalKey, value: o.result }; } function projectPluginSessionExtensionsSync(e) { const t = getPluginRegistryForContext()?.sessionExtensions ?? []; if (t.length === 0) return []; const n = []; for (const r of t) { const t = e.entry.pluginExtensions?.[r.pluginId]?.[r.extension.namespace]; const i = projectSessionExtensionValue({ pluginId: r.pluginId, namespace: r.extension.namespace, project: r.extension.project, sessionKey: e.sessionKey, sessionId: e.entry.sessionId, state: t }); if (i !== void 0) n.push({ pluginId: r.pluginId, namespace: r.extension.namespace, value: i }); } return n; } function projectSessionExtensionValue(e) { if (e.state === void 0) return; let t; try { t = e.project ? e.project({ sessionKey: e.sessionKey, sessionId: e.sessionId, state: e.state }) : e.state; } catch (t) { log.warn(`plugin session extension projection failed: plugin=${e.pluginId} namespace=${e.namespace} error=${String(t)}`); return; } if (isPromiseLike(t)) { Promise.resolve(t).catch(() => void 0); return; } return isPluginJsonValue(t) ? structuredClone(t) : void 0; } //#endregion export { projectPluginSessionExtensionsSync as a, patchPluginSessionExtension as i, enqueuePluginNextTurnInjection as n, getPluginSessionExtensionState as r, drainPluginNextTurnInjectionContext as t };