UNPKG

@essentialai/cogent-bridge

Version:

Cogent Bridge — cross-agent comms for Claude Code, OpenAI Codex and Slack. Codex users: install with `curl -fsSL https://cogent.tools/install.sh | sh`, then start with `cogent-codex` for real-time peer wake.

53 lines • 2.78 kB
import { BridgeError, BridgeErrorCode } from "../errors.js"; import { getConfig } from "../config.js"; // Deliberately conservative: one @, non-empty local + domain, a dot in the domain, no // whitespace. Not RFC-5322-exhaustive — enough to reject fat-finger input and give a clear // error at the tool boundary before it reaches the SMTP recipient / credential file, rather // than surfacing a cryptic SMTP/IMAP failure later. const EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; export function isValidEmail(value) { return value.length <= 254 && EMAIL_RE.test(value); } /** Throw a BridgeError(INVALID_INPUT) unless `value` is a plausibly-valid email address. */ export function assertValidEmail(value, field) { if (!isValidEmail(value)) { throw new BridgeError(BridgeErrorCode.INVALID_INPUT, `${field} is not a valid email address: ${JSON.stringify(value)}`, `Provide a full address like agent-channel@mail.cogent.tools`); } } /** The baked default Cogent mail domain — the allowlist fallback if config/env is unavailable (F4). */ export const DEFAULT_MAIL_DOMAIN = "mail.cogent.tools"; /** Parse the CSV allowlist into a normalized, non-empty lowercase domain list. */ export function parseAllowedMailDomains(csv) { const list = (csv ?? DEFAULT_MAIL_DOMAIN) .split(",") .map((s) => s.trim().toLowerCase()) .filter(Boolean); return list.length ? list : [DEFAULT_MAIL_DOMAIN]; } /** * The currently-allowed Cogent mail domains (F4). Prefers the loaded config's * COGENT_MAIL_ALLOWED_DOMAINS (a library default so Team agents get it too); falls back to the * env var, then the baked default — so it resolves even in tool contexts where config isn't loaded. */ export function allowedMailDomains() { try { return parseAllowedMailDomains(getConfig().COGENT_MAIL_ALLOWED_DOMAINS); } catch { return parseAllowedMailDomains(process.env.COGENT_MAIL_ALLOWED_DOMAINS); } } /** * Throw unless `address`'s domain is in the Cogent mail allowlist (F4). Cogent mail is * organisation-only — external / BYO-corporate mailboxes are blocked for now (a future release * adds per-org corporate domains via COGENT_MAIL_ALLOWED_DOMAINS). Assumes a syntactically valid * address (call {@link assertValidEmail} first). */ export function assertAllowedMailDomain(address, field) { const domain = address.split("@")[1]?.toLowerCase() ?? ""; const allowed = allowedMailDomains(); if (!allowed.includes(domain)) { throw new BridgeError(BridgeErrorCode.INVALID_INPUT, `${field} domain "${domain}" is not an allowed Cogent mail domain`, `Cogent mail is limited to: ${allowed.join(", ")}. External or corporate-domain mail is a future release.`); } } //# sourceMappingURL=validate.js.map