openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
180 lines (179 loc) • 6.4 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { s as asFiniteNumber } from "./number-coercion-D6u-4YD8.mjs";
import { t as asBoolean } from "./boolean-2vRuVafy.mjs";
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs";
import { f as normalizeStringEntries } from "./string-normalization-DLmfYZXI.mjs";
import { o as redactSensitiveUrlLikeString } from "./redact-sensitive-url-D-E7DGoc.mjs";
import "./utils-C8YECn2Q.mjs";
//#region packages/net-policy/src/url-userinfo.ts
/** Strip username/password credentials from a URL string when it parses. */
function stripUrlUserInfo(e) {
const t = URL.parse(e);
if (!t || !t.username && !t.password) return e;
t.username = "";
t.password = "";
return t.toString();
}
//#endregion
//#region src/channels/account-snapshot-fields.ts
const CREDENTIAL_STATUS_KEYS = [
"tokenStatus",
"botTokenStatus",
"appTokenStatus",
"signingSecretStatus",
"userTokenStatus"
];
const CREDENTIAL_SOURCE_KEYS = [
"tokenSource",
"botTokenSource",
"appTokenSource",
"signingSecretSource",
"userTokenSource",
"credentialSource",
"secretSource"
];
/** Redacts a plugin-provided base URL after status hooks have produced their final record. */
function redactChannelStatusSummaryBaseUrl(e) {
if (!isRecord(e) || typeof e.baseUrl !== "string" || !e.baseUrl) return e;
const t = stripUrlUserInfo(redactSensitiveUrlLikeString(e.baseUrl));
return t === e.baseUrl ? e : {
...e,
baseUrl: t
};
}
function readNullableNumber(e, t) {
return e[t] === null ? null : asFiniteNumber(e[t]);
}
function setSnapshotField(e, t, n) {
if (n !== void 0) e[t] = n;
}
function readStringArray(e, t) {
const n = e[t];
if (!Array.isArray(n)) return;
const r = normalizeStringEntries(n.map((e) => typeof e === "string" || typeof e === "number" ? e : ""));
return r.length > 0 ? r : void 0;
}
function readCredentialStatus(e, t) {
const n = e[t];
return n === "available" || n === "configured_unavailable" || n === "missing" ? n : void 0;
}
function resolveConfiguredFromCredentialStatusKeys(e, t, n) {
if (!isRecord(e)) return;
let r;
for (const i of t) {
const t = readCredentialStatus(e, i);
if (!t) continue;
r = t !== "missing";
if (n === "any" ? r : !r) return r;
}
return r;
}
/** Status inspection must not redeem credentials to detect configured-but-unavailable accounts. */
function resolveConfiguredFromCredentialStatuses(e) {
return resolveConfiguredFromCredentialStatusKeys(e, CREDENTIAL_STATUS_KEYS, "any");
}
/** Infers configured state only from the credential status keys required by a channel. */
function resolveConfiguredFromRequiredCredentialStatuses(e, t) {
return resolveConfiguredFromCredentialStatusKeys(e, t, "all");
}
/** Returns true when a credential exists but cannot be resolved at status-render time. */
function hasConfiguredUnavailableCredentialStatus(e) {
const t = isRecord(e) ? e : null;
if (!t) return false;
return CREDENTIAL_STATUS_KEYS.some((e) => readCredentialStatus(t, e) === "configured_unavailable");
}
/** Reads typed, redacted credential diagnostics from a resolved channel account. */
function getCredentialUnavailableDiagnostics(e) {
const t = isRecord(e) ? e : null;
if (!t || !Array.isArray(t.credentialDiagnostics)) return [];
const n = [];
for (const e of t.credentialDiagnostics) {
if (!isRecord(e) || e.code !== "CREDENTIAL_FILE_UNAVAILABLE") continue;
const t = normalizeOptionalString(e.path);
const r = normalizeOptionalString(e.reason);
if (t && r) n.push({
code: e.code,
path: t,
reason: r
});
}
return n;
}
/** Returns true when account data contains a resolved credential value or available status. */
function hasResolvedCredentialValue(e) {
const t = isRecord(e) ? e : null;
if (!t) return false;
return [
"token",
"botToken",
"appToken",
"signingSecret",
"userToken"
].some((e) => {
return normalizeOptionalString(t[e]) !== void 0;
}) || CREDENTIAL_STATUS_KEYS.some((e) => readCredentialStatus(t, e) === "available");
}
/** Projects credential source/status metadata while omitting raw credential values. */
function projectCredentialSnapshotFields(e) {
const t = isRecord(e) ? e : null;
if (!t) return {};
const n = {};
for (const e of CREDENTIAL_SOURCE_KEYS) setSnapshotField(n, e, normalizeOptionalString(t[e]));
for (const e of CREDENTIAL_STATUS_KEYS) setSnapshotField(n, e, readCredentialStatus(t, e));
return n;
}
/** Explicitly allowlist status fields so new account fields cannot expose credentials. */
function projectSafeChannelAccountSnapshotFields(e) {
const t = isRecord(e) ? e : null;
if (!t) return {};
const n = {};
for (const e of [
"name",
"statusState",
"healthState",
"mode",
"dmPolicy",
"identity",
"audienceType",
"webhookPath",
"cliPath",
"dbPath"
]) setSnapshotField(n, e, normalizeOptionalString(t[e]));
for (const e of [
"linked",
"running",
"connected",
"restartPending",
"terminalDisconnect",
"busy",
"allowUnmentionedGroups"
]) setSnapshotField(n, e, asBoolean(t[e]));
for (const e of [
"reconnectAttempts",
"lastInboundAt",
"lastTransportActivityAt",
"activeRuns",
"port"
]) setSnapshotField(n, e, asFiniteNumber(t[e]));
for (const e of [
"lastConnectedAt",
"lastOutboundAt",
"lastMessageAt",
"lastEventAt",
"lastRunActivityAt",
"activeRunStartedAt"
]) setSnapshotField(n, e, readNullableNumber(t, e));
const r = t.lifecycle;
if (r === "starting" || r === "ready" || r === "recovering" || r === "blocked" || r === "stopped") n.lifecycle = r;
if (asBoolean(t.ingressUnavailable) === true) n.ingressUnavailable = true;
setSnapshotField(n, "allowFrom", readStringArray(t, "allowFrom"));
Object.assign(n, projectCredentialSnapshotFields(e));
setSnapshotField(n, "apiCredentialStatus", readCredentialStatus(t, "apiCredentialStatus"));
for (const e of ["baseUrl", "audience"]) {
const r = normalizeOptionalString(t[e]);
if (r) n[e] = stripUrlUserInfo(redactSensitiveUrlLikeString(r));
}
return n;
}
//#endregion
export { projectCredentialSnapshotFields as a, resolveConfiguredFromCredentialStatuses as c, hasResolvedCredentialValue as i, resolveConfiguredFromRequiredCredentialStatuses as l, getCredentialUnavailableDiagnostics as n, projectSafeChannelAccountSnapshotFields as o, hasConfiguredUnavailableCredentialStatus as r, redactChannelStatusSummaryBaseUrl as s, CREDENTIAL_STATUS_KEYS as t };