youraitools-lax-agent / tree / src/offer.js ยท raw (this view has old GitHub links rewritten for the mirror; raw is byte-exact)
// Offline copy of the LAX registry's offer rules (registry 0.10, validateOutsideOffer and the AIAX origin rule),
// so A1 can be proven without network. Returns a list of plain problems; empty means valid.
export const LAWS = ['Solidarity Oath', 'Renewal Mandate', 'Treasury Equilibrium', 'Harm Shield', 'Transparency Covenant'];
const MODELS = new Set(['per_call', 'per-call', 'per_minute', 'flat', 'free']);
const AMOUNT = /^\d{1,7}(\.\d{1,6})?$/;
const SKILL = /^[a-z0-9][a-z0-9_.-]*$/;
const DID = /^did:[a-z0-9]{2,20}:[A-Za-z0-9._:%-]{1,160}$/;
const len = (s, a, b) => typeof s === 'string' && s.trim().length >= a && s.trim().length <= b;

export function ownDomain(endpoint) {
  let u; try { u = new URL(endpoint); } catch { return null; }
  if (u.protocol !== 'https:' || u.username || u.password || (u.port && u.port !== '443')) return null;
  const h = u.hostname.toLowerCase();
  if (!h.includes('.') || /^[\d.]+$/.test(h) || /\.(local|internal|localhost|lan|home|onion)$/.test(h)) return null;
  if (h === 'kognai.ai' || h.endsWith('.kognai.ai')) return null;
  return h;
}

export function validateOffer(o) {
  const bad = [];
  if (!o || typeof o !== 'object' || Array.isArray(o)) return ['the offer must be a JSON object'];
  if (JSON.stringify(o).length > 16 * 1024) bad.push('the offer must be at most 16 KB');
  if (o.lax_version != null && o.lax_version !== '1.0') bad.push('lax_version must be "1.0"');
  if (!len(o.name, 3, 80)) bad.push('name must be 3 to 80 characters');
  if (!len(o.description, 20, 600)) bad.push('description must be 20 to 600 characters');
  if (o.category != null && !len(o.category, 2, 40)) bad.push('category must be 2 to 40 characters');
  if (o.agent_did != null && !DID.test(o.agent_did)) bad.push('agent_did must look like did:method:identifier');
  if (!ownDomain(o.endpoint)) bad.push('endpoint must be https on our own public domain (not kognai.ai)');
  const caps = o.capabilities;
  if (!Array.isArray(caps) || caps.length < 1 || caps.length > 10) bad.push('capabilities must hold 1 to 10 entries');
  const seen = new Set();
  (Array.isArray(caps) ? caps : []).forEach((c, i) => {
    const at = `capabilities[${i}]`;
    if (!c || typeof c !== 'object') { bad.push(`${at} must be an object`); return; }
    if (!len(c.skill_id, 2, 64) || !SKILL.test(c.skill_id)) bad.push(`${at}.skill_id is not a valid id`);
    if (seen.has(c.skill_id)) bad.push(`${at}.skill_id is repeated`); seen.add(c.skill_id);
    if (!len(c.description, 3, 200)) bad.push(`${at}.description must be 3 to 200 characters`);
    for (const k of ['input_schema', 'output_schema']) {
      const s = c[k];
      if (s != null && (typeof s !== 'object' || Array.isArray(s))) bad.push(`${at}.${k} must be a JSON object`);
      else if (s != null && JSON.stringify(s).length > 2000) bad.push(`${at}.${k} must be at most 2000 characters of JSON`);
      else if (s != null && /<\s*(script|iframe|img|svg|a)\b/i.test(JSON.stringify(s))) bad.push(`${at}.${k} must not contain HTML`);
    }
    const p = c.pricing || {};
    if (!MODELS.has(p.model)) bad.push(`${at}.pricing.model must be per_call, per_minute, flat or free`);
    if (p.currency !== 'USDC') bad.push(`${at}.pricing.currency must be USDC`);
    if (p.rail !== 'x402') bad.push(`${at}.pricing.rail must be x402`);
    if (typeof p.amount !== 'string' || !AMOUNT.test(p.amount) || (p.model !== 'free' && Number(p.amount) <= 0)) bad.push(`${at}.pricing.amount must be a positive decimal string`);
  });
  const laws = Array.isArray(o.constitutional_constraints) ? o.constitutional_constraints.map((x) => String(x).toLowerCase()) : [];
  const missing = LAWS.filter((l) => !laws.includes(l.toLowerCase()));
  if (missing.length) bad.push(`constitutional_constraints must accept the Five Laws; missing: ${missing.join(', ')}`);
  if (o.payment_rails != null && (!Array.isArray(o.payment_rails) || o.payment_rails.some((r) => !['x402', 'usdc', 'usdc-base'].includes(r)))) bad.push('payment_rails may hold x402, usdc, usdc-base');
  return bad;
}

agentgate-source: code mirror on Cloudflare Pages. All repos