openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
84 lines (83 loc) • 3.03 kB
JavaScript
import crypto from "node:crypto";
//#region src/config/sessions/types.ts
function normalizeRuntimeField(value) {
const trimmed = value?.trim();
return trimmed ? trimmed : void 0;
}
function normalizeSessionRuntimeModelFields(entry) {
const normalizedModel = normalizeRuntimeField(entry.model);
const normalizedProvider = normalizeRuntimeField(entry.modelProvider);
let next = entry;
if (!normalizedModel) {
if (entry.model !== void 0 || entry.modelProvider !== void 0) {
next = { ...next };
delete next.model;
delete next.modelProvider;
}
return next;
}
if (entry.model !== normalizedModel) {
if (next === entry) next = { ...next };
next.model = normalizedModel;
}
if (!normalizedProvider) {
if (entry.modelProvider !== void 0) {
if (next === entry) next = { ...next };
delete next.modelProvider;
}
return next;
}
if (entry.modelProvider !== normalizedProvider) {
if (next === entry) next = { ...next };
next.modelProvider = normalizedProvider;
}
return next;
}
function setSessionRuntimeModel(entry, runtime) {
const provider = runtime.provider.trim();
const model = runtime.model.trim();
if (!provider || !model) return false;
entry.modelProvider = provider;
entry.model = model;
return true;
}
function resolveMergedUpdatedAt(existing, patch, options) {
if (options?.policy === "preserve-activity" && existing) return existing.updatedAt ?? patch.updatedAt ?? options.now ?? Date.now();
return Math.max(existing?.updatedAt ?? 0, patch.updatedAt ?? 0, options?.now ?? Date.now());
}
function mergeSessionEntryWithPolicy(existing, patch, options) {
const sessionId = patch.sessionId ?? existing?.sessionId ?? crypto.randomUUID();
const updatedAt = resolveMergedUpdatedAt(existing, patch, options);
if (!existing) return normalizeSessionRuntimeModelFields({
...patch,
sessionId,
updatedAt
});
const next = {
...existing,
...patch,
sessionId,
updatedAt
};
if (Object.hasOwn(patch, "model") && !Object.hasOwn(patch, "modelProvider")) {
const patchedModel = normalizeRuntimeField(patch.model);
const existingModel = normalizeRuntimeField(existing.model);
if (patchedModel && patchedModel !== existingModel) delete next.modelProvider;
}
return normalizeSessionRuntimeModelFields(next);
}
function mergeSessionEntry(existing, patch) {
return mergeSessionEntryWithPolicy(existing, patch);
}
function mergeSessionEntryPreserveActivity(existing, patch) {
return mergeSessionEntryWithPolicy(existing, patch, { policy: "preserve-activity" });
}
function resolveFreshSessionTotalTokens(entry) {
const total = entry?.totalTokens;
if (typeof total !== "number" || !Number.isFinite(total) || total < 0) return;
if (entry?.totalTokensFresh === false) return;
return total;
}
const DEFAULT_RESET_TRIGGERS = ["/new", "/reset"];
//#endregion
export { resolveFreshSessionTotalTokens as a, normalizeSessionRuntimeModelFields as i, mergeSessionEntry as n, setSessionRuntimeModel as o, mergeSessionEntryPreserveActivity as r, DEFAULT_RESET_TRIGGERS as t };