openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
127 lines (126 loc) • 5.29 kB
JavaScript
import { a as asOptionalRecord } from "./record-coerce-TIsquoVR.mjs";
import { t as captureNativeSessionEntryCurrentRead } from "./session-entry-current-runtime-1pJji_m4.mjs";
import { n as projectChatHistoryWithReplies } from "./chat-history-reply-messages-DIvwfidN.mjs";
//#region src/gateway/cli-session-history.process-held.ts
/** Keep process-held SQLite custody on its existing owner; move matching and projection off-loop. */
async function readProcessHeldCliHistory(params, signal, incognito) {
const result = await readProcessHeldCliHistoryQuery({
kind: "rpc",
params
}, signal, incognito);
if (result.kind !== "rpc") throw new Error("Unexpected process-held history page");
return result.page;
}
async function readProcessHeldCliHistoryMessage(params, incognito) {
const result = await readProcessHeldCliHistoryQuery({
kind: "rpc-message",
params
}, void 0, incognito);
if (result.kind !== "rpc-message") throw new Error("Unexpected process-held history message");
return result.result;
}
async function readProcessHeldCliHistoryQuery(input, signal, incognito) {
const history = structuredClone(input);
const params = history.params;
params.encodeResponse = false;
const [{ runProcessHeldHistoryTask }, readers] = await Promise.all([import("./session-transcript-worker-runtime-D5GwnkHX.mjs"), incognito?.readers ?? import("./session-transcript-readers-DfmXjQjz.mjs")]);
const scope = {
agentId: params.sessionAgentId,
sessionId: params.sessionId,
sessionKey: params.canonicalKey,
storePath: params.storePath,
sessionEntry: params.entry
};
const current = incognito ? void 0 : captureNativeSessionEntryCurrentRead(scope);
const initial = current?.readCurrent();
if (!incognito && (!initial || initial.sessionId !== scope.sessionId || initial.lifecycleRevision !== params.entry?.lifecycleRevision)) throw new Error("Incognito history session is no longer current");
const assertCurrent = () => {
signal?.throwIfAborted();
if (incognito) {
incognito.assertCurrent();
return;
}
const entry = current?.readCurrent();
if (entry?.sessionId !== initial?.sessionId || entry?.lifecycleRevision !== initial?.lifecycleRevision) throw new Error("Incognito history session generation is no longer current");
};
assertCurrent();
const result = await runProcessHeldHistoryTask(history, async (value, { signal: requestSignal }) => {
requestSignal.throwIfAborted();
assertCurrent();
const request = value;
const readResult = request.kind === "page" ? await readers.readSessionMessagesPageWithStatsAsync(scope, request.options, requestSignal) : request.kind === "around" ? await readers.readSessionMessagesAroundIdWithStatsAsync(scope, request.options, requestSignal) : await readers.readSessionMessageByIdAsync(scope, request.messageId, request.options, requestSignal);
requestSignal.throwIfAborted();
if (readResult === void 0) throw new Error("Unsupported process-held history request");
assertCurrent();
return {
input: readResult,
timeoutMs: 6e4
};
}, signal);
assertCurrent();
if (result.kind === "rpc-message") return result;
const page = result.page;
const [{ createCurrentUserProfileMessageProjector }, { resolveCurrentUserProfileDisplay }] = await Promise.all([import("./chat-display-projection.core-DcMYAi6F.mjs"), import("./current-user-profile-display-BarOOh-t.mjs")]);
const project = createCurrentUserProfileMessageProjector(resolveCurrentUserProfileDisplay);
page.messages = await projectChatHistoryWithReplies(page.messages.filter((message) => Boolean(asOptionalRecord(message))), (messages) => messages.map(project));
assertCurrent();
return {
kind: "rpc",
page
};
}
async function readProcessHeldCliHistoryInWorker(history, channel) {
const params = history.params;
const request = async (value) => {
const response = await channel.request(value);
try {
return response.input;
} finally {
response.consumed();
}
};
const readers = {
readSessionMessageByIdAsync: (_scope, messageId, options) => request({
kind: "by-id",
messageId,
options
}),
readRecentSessionMessagesWithStatsAsync: (_scope, options) => request({
kind: "page",
options: {
...options,
offset: 0
}
}),
readSessionMessagesPageWithStatsAsync: (_scope, options) => request({
kind: "page",
options
}),
readSessionMessagesAroundIdWithStatsAsync: (_scope, options) => request({
kind: "around",
options
})
};
const [{ prepareCliSessionHistoryReader, readChatHistoryMessageFromReaders }, { readChatHistoryPageKernel }] = await Promise.all([import("./cli-session-history-JDEROcPo.mjs"), import("./chat-history-page-kernel-DuGoW0-t.mjs")]);
if (history.kind === "rpc-message") return {
kind: "rpc-message",
result: await readChatHistoryMessageFromReaders(history.params, readers)
};
const cli = await prepareCliSessionHistoryReader(params, readers);
try {
const page = await readChatHistoryPageKernel(params, {
readers: cli?.readers ?? readers,
deferProfileDisplay: true,
readMessageSequence: cli?.sequence
});
cli?.applyPagination(page);
return {
kind: "rpc",
page
};
} finally {
cli?.dispose();
}
}
//#endregion
export { readProcessHeldCliHistory, readProcessHeldCliHistoryInWorker, readProcessHeldCliHistoryMessage };