openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
197 lines (196 loc) • 7.15 kB
JavaScript
import { B as isRecord, F as normalizeOptionalString } from "./runtime-doctor-migrations-JGL9tD-z.js";
import "./number-coercion-BtnBjAcf.js";
//#region packages/normalization-core/src/cjk-chars.ts
const WEIGHTED_CJK_RANGES = [
[
[],
[],
[],
[],
[]
],
[
[],
[],
[],
[],
[],
[],
[],
[],
[]
],
[
[],
[],
[],
[],
[],
[],
[],
[],
[]
],
[[119648, 119665]],
[
[],
[],
[],
[],
[],
[]
]
];
const WEIGHTED_CJK_RE = new RegExp(`[${WEIGHTED_CJK_RANGES.flatMap((e) => e.map(([e, t]) => `\\u{${e.toString(16)}}-\\u{${t.toString(16)}}`)).join("")}]`, "u");
const CJK_CATEGORY = (() => {
const e = Math.max(...WEIGHTED_CJK_RANGES.flatMap((e) => e.map(([, e]) => e)));
const t = new Uint8Array(e + 1);
for (const [e, n] of WEIGHTED_CJK_RANGES.entries()) for (const [r, i] of n) t.fill(e + 1, r, i + 1);
return t;
})();
//#endregion
//#region packages/normalization-core/src/expect.ts
/** Returns the value or throws with the named context; use for genuine invariants only. */
function expectDefined(e, t) {
if (e === null || e === void 0) throw new Error("expected " + t + " to be defined");
return e;
}
//#endregion
//#region src/secrets/ref-contract.ts
/** Shared SecretRef grammar and validation helpers for config, schema, SDK, and gateway parity. */
/** Provider alias used when a SecretRef omits a source-specific provider. */
const DEFAULT_SECRET_PROVIDER_ALIAS = "default";
const ENV_SECRET_REF_ID_RE = /^[A-Z][A-Z0-9_]{0,127}$/;
function isSecretRef(e) {
if (!isRecord(e)) return false;
if (Object.keys(e).length !== 3) return false;
return (e.source === "env" || e.source === "file" || e.source === "exec" || e.source === "store") && typeof e.provider === "string" && e.provider.trim().length > 0 && typeof e.id === "string" && e.id.trim().length > 0;
}
/**
* Runtime secret-reference grammar shared by config parsing, plugin SDK schemas,
* gateway parity checks, and resolver planning.
*/
const FILE_SECRET_REF_SEGMENT_PATTERN = /^(?:[^~]|~0|~1)*$/;
/** Shared alias grammar for env/file/exec/store secret provider names. */
const SECRET_PROVIDER_ALIAS_PATTERN = /^[a-z][a-z0-9_-]{0,63}$/;
const EXEC_SECRET_REF_ID_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._:/
/** Canonical id for file secret providers that expose exactly one value. */
const SINGLE_VALUE_FILE_REF_ID = "value";
function secretRefKey(e) {
return `${e.source}:${e.provider}:${e.id}`;
}
/** Validates file secret ref ids against the shared JSON-pointer-style contract. */
function isValidFileSecretRefId(e) {
if (e === "value") return true;
if (!e.startsWith("/")) return false;
return e.slice(1).split("/").every((e) => FILE_SECRET_REF_SEGMENT_PATTERN.test(e));
}
function isValidExecSecretRefId(e) {
if (!EXEC_SECRET_REF_ID_PATTERN.test(e)) return false;
for (const t of e.split("/")) if (t === "." || t === "..") return false;
return true;
}
function formatExecSecretRefIdValidationMessage() {
return [
"Exec secret reference id must match /^[A-Za-z0-9][A-Za-z0-9._:/#-]{0,255}$/",
"and must not include \".\" or \"..\" path segments",
"(example: \"vault/openai/api-key\" or \"aws/secret#json_key\")."
].join(" ");
}
//#endregion
//#region src/config/types.secrets.ts
const ENV_SECRET_TEMPLATE_RE = /^\$\{([A-Z][A-Z0-9_]{0,127})\}$/;
const ENV_SECRET_SHORTHAND_RE = /^\$([A-Z][A-Z0-9_]{0,127})$/;
function isLegacySecretRefWithoutProvider(e) {
if (!isRecord(e)) return false;
return (e.source === "env" || e.source === "file" || e.source === "exec" || e.source === "store") && typeof e.id === "string" && e.id.trim().length > 0 && e.provider === void 0;
}
/** Parse `$NAME` and `${NAME}` env-secret shorthand strings into env SecretRefs. */
function parseEnvTemplateSecretRef(e, t = DEFAULT_SECRET_PROVIDER_ALIAS) {
if (typeof e !== "string") return null;
const n = e.trim();
const r = ENV_SECRET_TEMPLATE_RE.exec(n) ?? ENV_SECRET_SHORTHAND_RE.exec(n);
if (!r) return null;
return {
source: "env",
provider: t.trim() || "default",
id: expectDefined(r[1], "types.secrets regex capture 1")
};
}
/** Parse current structured refs and supported env shorthand without legacy normalization. */
function parseSecretRef(e, t) {
return isSecretRef(e) ? e : parseEnvTemplateSecretRef(e, t?.env);
}
/** Public SDK input adapter; persisted config and state are normalized by Doctor. */
function coerceSecretRef(e, t) {
if (isLegacySecretRefWithoutProvider(e)) {
const n = t?.[e.source] ?? "default";
return {
source: e.source,
provider: n,
id: e.id
};
}
return parseSecretRef(e, t);
}
/** Return whether a value contains either a literal secret string or resolvable SecretRef shape. */
function hasConfiguredSecretInput(e, t) {
if (normalizeSecretInputString(e)) return true;
return coerceSecretRef(e, t) !== null;
}
/** Trim a literal secret input string while leaving non-string inputs unresolved. */
function normalizeSecretInputString(e) {
return normalizeOptionalString(e);
}
/** Error thrown when strict secret reads encounter a configured but unresolved SecretRef. */
var UnresolvedSecretInputError = class extends Error {
constructor(e) {
super(`${e.path}: unresolved SecretRef "${secretRefKey(e.ref)}". Resolve this command against an active gateway runtime snapshot before reading it.`);
this.name = "UnresolvedSecretInputError";
this.path = e.path;
this.ref = e.ref;
}
};
/** Resolve a secret field to either a literal value, a configured-unavailable ref, or missing. */
function resolveSecretInputString(e) {
const { explicitRef: t, ref: n } = resolveSecretInputRef(e);
const r = normalizeSecretInputString(e.value);
if (r && !t) return {
status: "available",
value: r,
ref: null
};
if (!n) return {
status: "missing",
value: void 0,
ref: null
};
if ((e.mode ?? "strict") === "strict") throw new UnresolvedSecretInputError({
path: e.path,
ref: n
});
return {
status: "configured_unavailable",
value: void 0,
ref: n
};
}
/** Return a strict literal secret value, throwing if the field still points at a SecretRef. */
function normalizeResolvedSecretInputString(e) {
return resolveSecretInputString({
...e,
mode: "strict"
}).value;
}
/** Resolve explicit `refValue` before inline secret references embedded in `value`. */
function resolveSecretInputRef(e) {
const t = coerceSecretRef(e.refValue, e.defaults);
const n = t ? null : coerceSecretRef(e.value, e.defaults);
return {
explicitRef: t,
inlineRef: n,
ref: t ?? n
};
}
//#endregion
export { ENV_SECRET_REF_ID_RE as a, isValidExecSecretRefId as c, parseEnvTemplateSecretRef as i, isValidFileSecretRefId as l, normalizeResolvedSecretInputString as n, SECRET_PROVIDER_ALIAS_PATTERN as o, normalizeSecretInputString as r, formatExecSecretRefIdValidationMessage as s, hasConfiguredSecretInput as t, expectDefined as u };