UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

134 lines (133 loc) • 5.15 kB
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; import { n as ok, t as err } from "./result-CV1-31DB.mjs"; import { r as isIncognitoSessionKey } from "./session-key-DW1jcHbM.mjs"; import "./session-key-CqAyZeVk.mjs"; import { i as resolveSessionWorkStartError } from "./session-work-start-CFJs8WNp.mjs"; import { X as captureSessionTarget } from "./session-controller.state-BApgK0mV.mjs"; import { t as ErrorCodes } from "./gateway-error-details-B69XiHdh.mjs"; import { p as errorShape } from "./error-codes-BzF7aRQ0.mjs"; import { _ as retainPreparedSessionEntryPredicate } from "./session-accessor.sqlite-entry-cache-publication-state-B4mkg2yK.mjs"; import { T as resolveEffectiveChatHistoryMaxChars } from "./chat-display-projection.canvas-XAAIxHM8.mjs"; import { t as beginSessionEffect } from "./session-controller.lifecycle-C_Z3sybN.mjs"; import { r as resolveRequestedSessionAgentId } from "./session-request-agent-tfwWH53J.mjs"; import "./sessions-D5Vvu3AE.mjs"; import { t as findCanonicalStoreMatch } from "./session-utils-store-selection-B-u2328z.mjs"; import { t as loadGatewaySessionEntryReadOnlyInWorker } from "./session-utils-store-worker-BU_tdh8T.mjs"; import { i as loadIncognitoGatewaySessionEntryReadOnly } from "./session-utils-store-CiI_s_OK.mjs"; import { t as formatForLog } from "./ws-log-CbOV6uhk.mjs"; import { o as projectChatDisplayMessage } from "./chat-display-projection.core-ApUvO4fQ.mjs"; import "./session-utils-CavYkLyd.mjs"; import "./chat-display-projection-lHBiRfmJ.mjs"; import { l as resolveGlobalAwareNodeChatDeliveryKeys, u as sendGlobalAwareNodeChatPayload } from "./chat-broadcast-til2j59n.mjs"; import { t as appendInjectedAssistantMessageToTranscript } from "./chat-transcript-inject-BGdtJFJV.mjs"; //#region src/gateway/server-methods/chat-inject.ts /** Append and publish one Gateway-authored assistant message in an existing session. */ async function injectGatewayAssistantMessage(e, t, n) { const r = t.sessionKey; const i = normalizeOptionalString(t.agentId); const a = e.getRuntimeConfig(); const o = resolveRequestedSessionAgentId(a, r, i); if (!o.ok) return err(o.error); const s = { agentId: o.agentId }; let c; const { agentId: l, storePath: u, entry: d, canonicalKey: f } = isIncognitoSessionKey(r) ? loadIncognitoGatewaySessionEntryReadOnly(r, s, a) : await loadGatewaySessionEntryReadOnlyInWorker({ excludeInternalEffects: true, cfg: a, key: r, ...s, onSelected: (e) => { c?.release(); const t = findCanonicalStoreMatch(e.store, e.storeKeys)?.entry; if (typeof e.capturedReadSource?.databaseIdentity === "string") c = retainPreparedSessionEntryPredicate({ databaseIdentity: `file:${e.capturedReadSource.databaseIdentity}`, sessionKey: e.canonicalKey, entry: t, matches: (e, n) => n?.sessionId === t?.sessionId }); } }).catch((e) => { c?.release(); throw e; }); const p = d?.sessionId; if (!p || !u) { c?.release(); return err(errorShape(ErrorCodes.INVALID_REQUEST, "session not found")); } let m; try { const e = await beginSessionEffect({ agentId: l, target: captureSessionTarget({ storeScope: u, sessionKey: f, agentId: l, incarnation: p }), storeWriterIdentities: [f], assertAllowed: () => { n?.(); const e = c?.readCurrent(); if (c && !e) throw new Error(`Session "${f}" changed while starting work. Retry.`); const t = isIncognitoSessionKey(f) ? loadIncognitoGatewaySessionEntryReadOnly(r, s).entry : e?.entry ?? d; if (t?.sessionId !== p) throw new Error(`Session "${f}" changed while starting work. Retry.`); const i = resolveSessionWorkStartError(f, t); if (i) throw new Error(i); } }); try { m = await e.run(async () => { n?.(); return await appendInjectedAssistantMessageToTranscript({ sessionKey: f, message: t.message, label: t.label, idempotencyKey: t.idempotencyKey, humanMentions: t.humanMentions, ...t.literal ? { literal: true } : {}, ...n ? { assertCurrent: n } : {}, onMessageCommitted: t.onMessageCommitted, sessionId: p, storePath: u, agentId: l, config: a }); }); } finally { e.release(); } } catch (e) { return err(errorShape(ErrorCodes.INVALID_REQUEST, formatForLog(e))); } finally { c?.release(); } if (!m.ok || !m.messageId || !m.message) return err(errorShape(ErrorCodes.UNAVAILABLE, `failed to write transcript: ${m.error ?? "unknown error"}`)); if (m.replayed) return ok({ messageId: m.messageId, replayed: true }); const h = projectChatDisplayMessage(m.message, { maxChars: resolveEffectiveChatHistoryMaxChars() }); const g = { runId: `inject-${m.messageId}`, sessionKey: f, ...l ? { agentId: l } : {}, seq: 0, state: "final", message: h }; e.broadcast("chat", g, { sessionKeys: resolveGlobalAwareNodeChatDeliveryKeys({ cfg: a, sessionKey: f, agentId: l }) }); sendGlobalAwareNodeChatPayload({ context: e, sessionKey: f, agentId: l, event: "chat", payload: g }); return ok({ messageId: m.messageId }); } //#endregion export { injectGatewayAssistantMessage as t };