openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
74 lines (73 loc) • 2.56 kB
JavaScript
import { a as resolveGlobalSingleton } from "./global-singleton-D7yf05nR.mjs";
import { t as LruCache } from "./lru-cache-wquJPIr-.mjs";
import { a as resolveRuntimeProcessEntrypointUrl } from "./runtime-process-url-CoHTemu-.mjs";
import { a as WorkerTaskError } from "./src-Dkfkl2Oz.mjs";
import { t as WorkerTaskPool } from "./worker-task-pool-BshJjv1P.mjs";
import { i as getOrCreatePromise } from "./lazy-promise-DiCvjz9w.mjs";
import path from "node:path";
//#region src/agents/identity-file-runtime.ts
/** Identity metadata and avatar bytes share admission and cache settlement, not file policy. */
function prepareCachedIdentityRead(e) {
const t = resolveGlobalSingleton(e.runtimeKey, () => ({
pending: /* @__PURE__ */ new Map(),
cached: new LruCache(64, {
maxBytes: 16777216,
sizeOf: e.sizeOf
})
}), (e) => {
e.closing ??= (async () => {
await e.pool?.close();
await Promise.allSettled(e.pending.values());
e.pool = void 0;
e.cached.clear();
})().finally(() => {
e.closing = void 0;
});
return e.closing;
});
if (t.closing) return Promise.reject(new WorkerTaskError(`${e.readerName} is closing`, "unavailable"));
const n = e.key();
return getOrCreatePromise(t.pending, n, async () => {
const r = t.cached.peek(n);
if (r && e.isCurrent) {
const i = await e.isCurrent(r);
if (t.closing) throw new WorkerTaskError(`${e.readerName} is closing`, "unavailable");
if (i) {
t.cached.set(n, r);
return r;
}
}
const i = t.pool ??= new WorkerTaskPool({
workerUrl: resolveRuntimeProcessEntrypointUrl(e.worker),
workerClass: "file-reader",
idleTimeoutMs: 6e5,
maxPendingTasks: 256,
maxPendingBytes: 1048576
});
const a = r ? e.revision(r) : void 0;
const o = await i.run(e.input(n, a), { inputBytes: 2 * (n.length + (a?.length ?? 0)) });
const s = e.prepare(o, r);
if (!s) throw new Error(`${e.readerName} returned an unknown revision`);
if (e.isLoaded(s)) t.cached.set(n, s);
else t.cached.delete(n);
return s;
}, { evictOnSettled: true });
}
function prepareIdentityFile(e) {
return prepareCachedIdentityRead({
runtimeKey: Symbol.for("openclaw.identityFiles"),
worker: "identityFile",
readerName: "Identity file reader",
key: () => path.resolve(e),
input: (e, t) => ({
identityPath: e,
knownRevision: t
}),
revision: (e) => e.revision,
sizeOf: (e) => e.size,
prepare: (e, t) => e.kind === "unchanged" ? t : e,
isLoaded: (e) => e.kind === "loaded"
});
}
//#endregion
export { prepareIdentityFile as n, prepareCachedIdentityRead as t };