openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
135 lines (134 loc) • 4.82 kB
JavaScript
import { r as isIncognitoSessionKey } from "./session-key-DW1jcHbM.mjs";
import "./session-key-CqAyZeVk.mjs";
import { o as registerOpenClawAgentDatabaseAsyncResource } from "./openclaw-agent-db-resources-Dp8HvLsg.mjs";
import { _ as retainPreparedSessionEntryPredicate } from "./session-accessor.sqlite-entry-cache-publication-state-B4mkg2yK.mjs";
import { a as resolveSessionStoreIdentity } from "./session-store-key-CEwU3noi.mjs";
import { l as hasSessionReadAccessChanged } from "./session-sharing-policy-3TAaxC-R.mjs";
import { r as PreparedModelRuntimePublicationSupersededError } from "./prepared-model-runtime-admission-errors-LxrcCg4d.mjs";
import { t as chatMetadataSessionFields } from "./chat-metadata-contract-ZIuiGXh-.mjs";
import { u as withGatewaySessionEntry } from "./session-utils-store-CiI_s_OK.mjs";
import "./prepared-model-runtime.errors-BRsU9-dm.mjs";
import { t as retainGatewaySessionEntryReadOnly } from "./session-utils-read-lifetime-Cx8swkJr.mjs";
import { isDeepStrictEqual } from "node:util";
//#region src/gateway/server-methods/chat-metadata-session-read.ts
function sameMetadataEntry(e, t) {
return e && t ? !hasSessionReadAccessChanged(e, t) && chatMetadataSessionFields.every((n) => isDeepStrictEqual(e[n], t[n])) : e === t;
}
async function prepareChatMetadataSessionRead(e) {
const changed = () => new PreparedModelRuntimePublicationSupersededError("Session changed while preparing its metadata. Retry the request.");
let t = true;
let n;
const r = [];
const i = /* @__PURE__ */ new Set();
const release = () => {
t = false;
for (const e of r.splice(0).toReversed()) e();
};
const assertRefreshable = () => {
e.assertRequestCurrent();
if (!t || n?.canRefresh() === false) throw changed();
};
const isCurrent = () => t && n?.isCurrent() !== false;
const assertCurrent = () => {
assertRefreshable();
if (!isCurrent()) throw changed();
};
const a = {
agentId: e.agentId,
projection: []
};
try {
if (isIncognitoSessionKey(resolveSessionStoreIdentity(e).canonicalKey)) {
const t = await retainGatewaySessionEntryReadOnly(e.sessionKey, e.agentId, sameMetadataEntry, e.cfg);
r.push(t.release);
const assertNativeCurrent = () => {
assertRefreshable();
if (!t.isCurrentAtResponse()) throw changed();
};
assertNativeCurrent();
return {
selected: t,
isCurrent: t.isCurrent,
assertCurrent: assertNativeCurrent,
beforeRequest: assertNativeCurrent,
release,
async withCurrent(e) {
assertNativeCurrent();
return await e();
}
};
}
const t = await withGatewaySessionEntry(e.sessionKey, a, (e) => {
assertCurrent();
const t = new Map([
{
agentId: e.readSource?.agentId ?? e.agentId,
path: e.storePath
},
...e.readSource ? [e.readSource] : [],
...e.capturedReadSources ?? []
].map((e) => [e.path, e]));
for (const e of t.values()) r.push(registerOpenClawAgentDatabaseAsyncResource({
...e,
revoke: release,
close: async () => {
release();
await Promise.allSettled(i);
}
}));
if (e.capturedReadSource) {
if (typeof e.capturedReadSource.databaseIdentity !== "string") throw changed();
n = retainPreparedSessionEntryPredicate({
databaseIdentity: `file:${e.capturedReadSource.databaseIdentity}`,
sessionKey: e.legacyKey ?? e.canonicalKey,
entry: e.entry,
matches: sameMetadataEntry
});
r.push(n.release);
}
return e;
}, e.cfg, assertCurrent);
assertCurrent();
const matchesSelected = (e) => e.agentId === t.agentId && e.canonicalKey === t.canonicalKey && e.legacyKey === t.legacyKey && e.storePath === t.storePath && isDeepStrictEqual(e.readSource, t.readSource) && isDeepStrictEqual(e.capturedReadSource, t.capturedReadSource) && isDeepStrictEqual(e.capturedReadSources, t.capturedReadSources) && sameMetadataEntry(t.entry, e.entry);
return {
selected: t,
isCurrent,
assertCurrent,
beforeRequest: assertCurrent,
release,
async withCurrent(t) {
assertRefreshable();
const r = n?.captureRevision();
let o;
let s;
const c = withGatewaySessionEntry(e.sessionKey, a, (e) => {
assertRefreshable();
if (!matchesSelected(e) || n && !n.acknowledge(e.entry, r)) throw changed();
assertCurrent();
o = (async () => await t())();
const a = o;
i.add(a);
const settled = () => i.delete(a);
a.then(settled, (e) => {
s ??= { error: e };
settled();
});
return { value: a };
}, e.cfg, assertRefreshable);
try {
return await (await c).value;
} catch (e) {
s ??= { error: e };
release();
await Promise.allSettled(o ? [o] : []);
throw s.error;
}
}
};
} catch (e) {
release();
throw e;
}
}
//#endregion
export { prepareChatMetadataSessionRead as t };