openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
167 lines (166 loc) • 6.06 kB
JavaScript
import { a as resolveGlobalSingleton } from "./global-singleton-D7yf05nR.mjs";
import { r as resolveStateDir } from "./state-dir-DiG0gXzD.mjs";
import "./paths-DAcAg5aV.mjs";
import { a as resolveRuntimeProcessEntrypointUrl } from "./runtime-process-url-CoHTemu-.mjs";
import { a as getOpenClawDatabaseMaintenanceScope } from "./openclaw-state-maintenance-context-D6i7Zdhn.mjs";
import { g as resolveDatabasePathKeyAsync } from "./sqlite-worker-identity-BH8ZkJU9.mjs";
import { t as resolveClientCredentialStorePath } from "./client-credential-paths-CIYPaDgB.mjs";
import { n as enqueueKeyedTask } from "./keyed-async-queue-DewLnYvW.mjs";
import path from "node:path";
//#region src/infra/client-credential-async.ts
const queues = resolveGlobalSingleton(Symbol.for("openclaw.clientCredentialOperations"), () => /* @__PURE__ */ new Map());
const runtime = resolveGlobalSingleton(Symbol.for("openclaw.clientCredentialWorker"), () => ({ resources: /* @__PURE__ */ new Map() }), () => closeClientCredentialWorker());
function closeClientCredentialWorker() {
runtime.closing ??= (async () => {
await Promise.all(queues.values());
await runtime.pool?.close();
runtime.pool = void 0;
for (const e of runtime.resources.values()) e.unregister();
runtime.resources.clear();
})().finally(() => {
runtime.closing = void 0;
});
return runtime.closing;
}
/** Run one bounded off-thread operation, routing to the app when it owns this root. */
async function runClientCredentialOperationAsync(e, t, n = {}) {
return runClientCredentialTaskAsync(e, t, n);
}
/** Reuse native admission's postimage without reading credentials on the caller thread. */
async function prepareClientCredentialStoreAsync(e) {
return (await runClientCredentialTaskAsync(e, { kind: "startup.prepare" })).state;
}
async function runClientCredentialTaskAsync(e, t, n = {}) {
n.signal?.throwIfAborted();
n.assertCurrent?.();
const r = await enqueueKeyedTask({
tails: queues,
key: "\0credential-root-resolution",
task: () => resolveDatabasePathKeyAsync(path.resolve(resolveStateDir(e)))
});
n.signal?.throwIfAborted();
n.assertCurrent?.();
const i = {
HOME: e.HOME,
USERPROFILE: e.USERPROFILE,
OPENCLAW_PROFILE: e.OPENCLAW_PROFILE,
OPENCLAW_STATE_DIR: r
};
const a = resolveClientCredentialStorePath({ OPENCLAW_STATE_DIR: r });
const assertCurrent = () => {
n.signal?.throwIfAborted();
n.assertCurrent?.();
};
assertCurrent();
return enqueueKeyedTask({
tails: queues,
key: r,
task: async () => {
const offline = async () => {
const [{ registerOpenClawAgentDatabaseReadCandidateResource: e }, { hydrateOpenClawStateWorkerError: i, retainOpenClawStateWorkerErrorPayload: o }, { acquireStateDatabaseSchemaLease: s, captureStateDatabaseAccess: c }, { retainSqliteWriteAdmissionService: l }, { createSqliteWorkerOperationAdmission: u }, { WorkerTaskPool: d }] = await Promise.all([
import("./openclaw-agent-db-resources-DR2pIReK.mjs"),
import("./openclaw-state-worker-error-LU35UwYz.mjs"),
import("./gateway-state-owner-D5aGeZ70.mjs"),
import("./sqlite-transaction-CnMcSOlp.mjs"),
import("./sqlite-worker-operation-admission-BZuBdqe3.mjs"),
import("./worker-task-pool-BrtanqWr.mjs")
]);
assertCurrent();
if (runtime.closing) throw new Error("Client credential worker is closing.");
let f = runtime.resources.get(a);
if (!f) {
const t = await c(a);
assertCurrent();
if (runtime.closing) throw new Error("Client credential worker is closing.");
f = {
access: t,
unregister: e({
path: a,
revoke() {
closeClientCredentialWorker().catch(() => {});
},
close: closeClientCredentialWorker
})
};
runtime.resources.set(a, f);
}
const p = getOpenClawDatabaseMaintenanceScope();
const m = await f.access.prepareAccess({ maintenanceScope: p });
assertCurrent();
let h;
const assertAccess = () => {
assertCurrent();
p?.assertAdmission();
m({
maintenanceScope: p,
schemaLease: h
});
};
const g = u((e, t) => {
assertAccess();
if (!t()) throw new Error("Client credential operation authority expired.");
});
g.bindDatabaseAuthority({
databasePath: a,
assertRequest: assertCurrent,
assertAccess,
acquireSchema() {
assertAccess();
const acquire = () => s(a);
const e = p ? p.run(acquire) : acquire();
h = e;
p?.own(e, "shared-resources", () => e.release());
return e;
}
});
const _ = l([a], () => g.service());
try {
runtime.pool ??= new d({
workerUrl: resolveRuntimeProcessEntrypointUrl("clientCredentials"),
workerClass: "singleton"
});
const e = await runtime.pool.run({
root: r,
operation: t,
admission: g.port
}, {
signal: n.signal,
transferList: (e) => [e.admission]
});
assertAccess();
if (e.kind === "error") {
const t = /* @__PURE__ */ new Error("Client credential operation failed");
o(t, e.error);
throw i(t, { includeOrdinary: true });
}
return e;
} catch (e) {
throw g.failure ?? e;
} finally {
_();
g.finish();
}
};
const e = getOpenClawDatabaseMaintenanceScope();
if (e?.ownsSchemaMaintenance) e.assertAdmission();
else {
const { requestClientCredentialsFromApp: e } = await import("./client-credential-ipc-BzYlBjyf.mjs");
assertCurrent();
const r = await e(i, t.kind === "startup.prepare" ? {
kind: "identity.read",
identity_key: "primary"
} : t, n.signal, assertCurrent);
assertCurrent();
if (r) return t.kind === "startup.prepare" ? {
kind: "preparation",
state: "ready"
} : r;
}
const o = await offline();
if (o.kind === "result") return o.result;
throw new Error("Client credentials are busy; retry after the current owner finishes.");
}
});
}
//#endregion
export { runClientCredentialOperationAsync as n, prepareClientCredentialStoreAsync as t };