UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

90 lines (89 loc) • 2.83 kB
import { a as resolveGlobalSingleton } from "./global-singleton-D7yf05nR.mjs"; import { AsyncLocalStorage } from "node:async_hooks"; //#region src/auto-reply/group-thread-context.ts const turns = resolveGlobalSingleton(Symbol.for("openclaw.groupThreadContext"), () => new AsyncLocalStorage()); const toolReplies = resolveGlobalSingleton(Symbol.for("openclaw.groupThreadToolReplies"), () => new AsyncLocalStorage()); async function captureGroupThreadToolReply(e, t) { if (!currentScope()) return { result: await e(), observed: false }; const n = { observed: false, source: t }; return { result: await toolReplies.run(n, e), observed: n.observed, text: n.text }; } /** Restore source attribution after modifying hooks; undefined preserves cancellation. */ function finalizeGroupThreadToolReply(e, t, n) { const r = toolReplies.getStore(); let i = e; if (r && currentScope() && (!r.source || r.source.matches(t, n))) { if (i?.trim()) i = r.source?.format(i) ?? i; r.observed = true; r.text = i?.slice(0, 4e3); } return i; } function withGroupThreadTurn(e, t) { const n = { ...e, active: true }; return turns.run(n, async () => { try { return await t(); } finally { n.active = false; } }); } function currentScope() { const e = turns.getStore(); return e?.active ? e : void 0; } function getGroupThreadTurn() { return currentScope()?.turn; } function getGroupThreadParticipant() { return currentScope()?.participant; } async function adoptGroupThreadRoot(e) { if (e) await currentScope()?.adopt?.(e); } function bindGroupThreadDispatchContext(e) { const t = {}; const n = currentScope(); if (n) n.dispatch = { ctx: e, runState: t }; return t; } function getGroupThreadDispatchContext() { return currentScope()?.dispatch; } /** Data-only delivery identity, including unlabeled single-participant groups. */ function getGroupThreadDeliverySession() { const e = currentScope()?.dispatch?.ctx; return e?.AgentId && e.SessionKey ? { agentId: e.AgentId, sessionKey: e.SessionKey } : void 0; } function recordGroupThreadReply(e) { currentScope()?.recordReply(e); } /** Message actions use the initiating channel's encoder, without changing delivery ownership. */ function formatGroupThreadReply(e) { const t = currentScope(); if (!t?.participant || !t.formatReply) return e; const n = t.formatReply("", t.participant); return n && e.startsWith(n) ? e : t.formatReply(e, t.participant); } //#endregion export { formatGroupThreadReply as a, getGroupThreadParticipant as c, withGroupThreadTurn as d, finalizeGroupThreadToolReply as i, getGroupThreadTurn as l, bindGroupThreadDispatchContext as n, getGroupThreadDeliverySession as o, captureGroupThreadToolReply as r, getGroupThreadDispatchContext as s, adoptGroupThreadRoot as t, recordGroupThreadReply as u };