openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
184 lines (183 loc) • 6.61 kB
JavaScript
import { r as resolveGlobalMap } from "./global-singleton-D7yf05nR.mjs";
import { n as runWithSessionControllerCleanup } from "./session-controller.cleanup-context-DsmTHS1X.mjs";
import { r as runOutsideStoreWriterContext } from "./store-writer-queue-B36_RXST.mjs";
import { C as runWithGatewayDetachedWorkContinuation } from "./gateway-work-admission-79pid1hQ.mjs";
import "./session-controller.state-BApgK0mV.mjs";
import { i as MAX_VISIBLE_MESSAGE_MAX_MESSAGES, l as normalizeVisibleMessageLimit, r as MAX_VISIBLE_MESSAGE_MAX_BYTES } from "./session-accessor.sqlite-visible-cursor-BKIsjRgi.mjs";
import { n as createSessionResetBoundaryId } from "./session-reset-boundary-event-COASu9ts.mjs";
import { r as attachSessionEndTranscriptSource } from "./hooks-BiZpB_10.mjs";
import { t as capArrayByJsonBytes } from "./session-utils.fs-bIf8ZZfW.mjs";
import { c as readSessionMessagesAroundIdWithStatsAsync, n as readRecentSessionMessagesWithStatsAsync } from "./session-transcript-readers-D1WqgwbF.mjs";
//#region src/gateway/session-end-transcript-reader.ts
const ENDED_TRANSCRIPT_MAX_BYTES = Math.min(MAX_VISIBLE_MESSAGE_MAX_BYTES, 8388608);
function normalizeEndedTranscriptOptions(e) {
return {
maxMessages: normalizeVisibleMessageLimit(e.maxMessages, 1, MAX_VISIBLE_MESSAGE_MAX_MESSAGES, "maxMessages"),
maxBytes: normalizeVisibleMessageLimit(e.maxBytes, 1024, ENDED_TRANSCRIPT_MAX_BYTES, "maxBytes")
};
}
function finalizeEndedTranscriptRead(e, t, n) {
const r = capArrayByJsonBytes(e, n.maxBytes);
const i = r.bytes <= n.maxBytes ? r.items : [];
return {
messages: i,
totalMessages: t,
truncated: i.length < t
};
}
function createClosedSessionTranscriptSource(e) {
return {
available: true,
async readTail(t) {
const n = normalizeEndedTranscriptOptions(t);
const r = await readRecentSessionMessagesWithStatsAsync(e, {
maxMessages: n.maxMessages,
maxLines: n.maxMessages * 20 + 20,
maxBytes: n.maxBytes
});
return finalizeEndedTranscriptRead(r.messages, r.totalMessages, n);
}
};
}
function createResetBoundaryTranscriptSource(e, t) {
return {
available: true,
async readTail(n) {
const r = normalizeEndedTranscriptOptions(n);
const i = await readSessionMessagesAroundIdWithStatsAsync(e, {
closedResetInterval: true,
messageId: t,
maxMessages: r.maxMessages,
maxBytes: r.maxBytes,
direction: "older"
});
if (!i.found) throw new Error("Ended session reset boundary is no longer available");
return finalizeEndedTranscriptRead(i.messages, i.totalMessages, r);
}
};
}
function createArchivedSessionTranscriptSource(e) {
return {
available: true,
async readTail(t) {
const n = normalizeEndedTranscriptOptions(t);
const { readSessionHistoryPageInWorker: r } = await import("./session-history-worker-runtime-D8lOqPp3.mjs");
const i = await r({
kind: "recent-page",
params: {
target: {
sessionId: e.sessionId,
...e.agentId ? { agentId: e.agentId } : {},
...e.storePath ? { storePath: e.storePath } : {}
},
exactArchivePath: e.archivedPath,
options: {
maxMessages: n.maxMessages,
maxLines: n.maxMessages * 20 + 20,
maxBytes: n.maxBytes
}
}
});
if (!i.transcriptPath) throw new Error("Ended session archive is no longer available");
return finalizeEndedTranscriptRead(i.messages, i.totalMessages, n);
}
};
}
//#endregion
//#region src/auto-reply/reply/session-hooks.ts
function resolveExplicitSessionEndReason(e) {
return e === "/reset" ? "reset" : "new";
}
function createReplySessionResetBoundary(e) {
const t = e.previousReason === "idle" || e.previousReason === "daily" ? e.previousReason : "reset";
const n = e.resetTriggered ? {
context: "clear",
reason: e.explicitReason
} : {
context: "preserve-tail",
reason: t
};
return {
boundaryId: createSessionResetBoundaryId(),
cwd: e.cwd,
...n
};
}
function buildSessionHookContext(e) {
return {
sessionId: e.sessionId,
sessionKey: e.sessionKey,
agentId: e.agentId
};
}
function buildSessionStartHookPayload(e) {
return {
event: {
sessionId: e.sessionId,
sessionKey: e.sessionKey,
resumedFrom: e.resumedFrom
},
context: buildSessionHookContext(e)
};
}
function emitReplySessionStartHook(e, t) {
const n = buildSessionStartHookPayload(t);
runWithSessionControllerCleanup(() => runWithGatewayDetachedWorkContinuation(async () => {
await runOutsideStoreWriterContext(() => e.runSessionStart(n.event, n.context));
}, "hooks:session-start")).catch(() => {});
}
function buildSessionEndHookPayload(e) {
const t = buildSessionHookContext(e);
attachSessionEndTranscriptSource(t, e.endedTranscript ?? {
available: false,
reason: "unsupported-source"
});
return {
event: {
sessionId: e.sessionId,
sessionKey: e.sessionKey,
messageCount: e.messageCount ?? 0,
durationMs: e.durationMs,
reason: e.reason,
sessionFile: e.sessionFile,
transcriptArchived: e.transcriptArchived,
nextSessionId: e.nextSessionId,
nextSessionKey: e.nextSessionKey
},
context: t
};
}
function emitReplySessionEndHook(e) {
const t = e.resetBoundaryId ? createResetBoundaryTranscriptSource({
agentId: e.agentId,
sessionId: e.sessionId,
sessionKey: e.sessionKey,
storePath: e.storePath
}, e.resetBoundaryId) : {
available: false,
reason: "unsupported-source"
};
const n = buildSessionEndHookPayload({
...e,
endedTranscript: t
});
runWithSessionControllerCleanup(() => runWithGatewayDetachedWorkContinuation(async () => {
await runOutsideStoreWriterContext(() => e.hookRunner.runSessionEnd(n.event, n.context));
}, "hooks:session-end")).catch(() => {});
}
//#endregion
//#region src/gateway/active-sessions-shutdown-tracker.ts
const trackedSessions = resolveGlobalMap(Symbol.for("openclaw.activeSessionsForShutdown"), "close-and-restart");
function noteActiveSessionForShutdown(e) {
if (!e.sessionId) return;
trackedSessions.set(e.sessionId, e);
}
function forgetActiveSessionForShutdown(e) {
if (!e) return;
trackedSessions.delete(e);
}
function listActiveSessionsForShutdown() {
return Array.from(trackedSessions.values());
}
//#endregion
export { buildSessionStartHookPayload as a, emitReplySessionStartHook as c, createClosedSessionTranscriptSource as d, createResetBoundaryTranscriptSource as f, buildSessionEndHookPayload as i, resolveExplicitSessionEndReason as l, listActiveSessionsForShutdown as n, createReplySessionResetBoundary as o, noteActiveSessionForShutdown as r, emitReplySessionEndHook as s, forgetActiveSessionForShutdown as t, createArchivedSessionTranscriptSource as u };