openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
74 lines (73 loc) • 3.55 kB
JavaScript
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs";
import { s as parseRegistryNpmSpec } from "./npm-registry-spec-HKkAvl6N.mjs";
import { c as readClawHubBooleanField, d as readClawHubStringArrayField, f as readClawHubStringField, g as readRequiredClawHubStringField, i as fetchClawHubJson, m as readRequiredClawHubNumberField, p as readRequiredClawHubBooleanField, u as readClawHubNonEmptyStringFields } from "./clawhub-client-BLg-dsn2.mjs";
//#region src/infra/clawhub-promotions.ts
const CLAWHUB_PROMOTION_MODEL_REF_RE = /^[A-Za-z0-9][A-Za-z0-9._:/-]*$/;
function parseClawHubPromotionModel(e, t) {
if (!isRecord(e)) throw new Error(`Malformed ClawHub ${t}: expected each model to be an object.`);
const n = readRequiredClawHubStringField(e, "modelRef", t);
if (!CLAWHUB_PROMOTION_MODEL_REF_RE.test(n)) throw new Error(`Malformed ClawHub ${t}: modelRef contains unsupported characters.`);
const r = {
modelRef: n,
...readClawHubNonEmptyStringFields(e, ["alias"], t)
};
const i = readClawHubBooleanField(e, "suggestedDefault", t);
if (i !== void 0) r.suggestedDefault = i;
return r;
}
const CLAWHUB_PROMOTION_SLUG_RE = /^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/;
const CLAWHUB_PROMOTION_IDENTIFIER_RE = /^[A-Za-z0-9][A-Za-z0-9._@/-]*$/;
function parseClawHubPromotion(e) {
const t = "promotion";
if (!isRecord(e)) throw new Error(`Malformed ClawHub ${t}: expected an object.`);
const n = e.models;
if (!Array.isArray(n) || n.length === 0) throw new Error(`Malformed ClawHub ${t}: expected models to be a non-empty array.`);
const r = readRequiredClawHubStringField(e, "slug", t);
if (!CLAWHUB_PROMOTION_SLUG_RE.test(r)) throw new Error(`Malformed ClawHub ${t}: slug must be lowercase [a-z0-9-].`);
const i = readRequiredClawHubNumberField(e, "startsAt", t);
const a = readRequiredClawHubNumberField(e, "endsAt", t);
if (a <= i) throw new Error(`Malformed ClawHub ${t}: promotion window must end after it starts.`);
const o = {
slug: r,
title: readRequiredClawHubStringField(e, "title", t),
blurb: readRequiredClawHubStringField(e, "blurb", t),
startsAt: i,
endsAt: a,
models: n.map((e) => parseClawHubPromotionModel(e, t)),
...readClawHubNonEmptyStringFields(e, [
"sponsor",
"signupUrl",
"docsUrl",
"launchPageUrl"
], t)
};
for (const n of ["provider", "authChoiceId"]) {
const r = readClawHubStringField(e, n, t);
if (!r) continue;
if (!CLAWHUB_PROMOTION_IDENTIFIER_RE.test(r)) throw new Error(`Malformed ClawHub ${t}: ${n} contains unsupported characters.`);
o[n] = r;
}
const s = readClawHubStringArrayField(e, "pluginNames", t);
if (s && s.length > 0) {
for (const e of s) {
const n = parseRegistryNpmSpec(e);
if (!n || n.selectorKind !== "none" || n.name !== e) throw new Error(`Malformed ClawHub ${t}: pluginNames must contain npm package names.`);
}
o.pluginNames = s;
}
return {
...o,
status: readRequiredClawHubStringField(e, "status", t),
active: readRequiredClawHubBooleanField(e, "active", t)
};
}
async function fetchClawHubPromotions() {
const e = await fetchClawHubJson({ path: "/api/v1/promotions" });
if (!isRecord(e) || !Array.isArray(e.promotions)) throw new Error("Malformed ClawHub promotions response: expected a promotions array.");
return e.promotions.map((e) => parseClawHubPromotion(e));
}
async function fetchClawHubPromotion(e) {
return parseClawHubPromotion(await fetchClawHubJson({ path: `/api/v1/promotions/${encodeURIComponent(e.slug)}` }));
}
//#endregion
export { fetchClawHubPromotions as n, fetchClawHubPromotion as t };