UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

166 lines • 6.59 kB
/** * Brand profile resolution. * * A distribution that repackages this engine (for example the `omo-ai` package) injects a * single JSON environment variable describing how the product presents itself. The engine * parses it once at startup and then REMOVES it from the environment, so nested processes * spawned by tools inherit a clean environment and keep the engine's own identity. * * Absent or malformed input leaves every brand-derived value at its standalone default, so a * plain install behaves exactly as it did before this module existed. */ import { isAbsolute } from "node:path"; /** Environment variable carrying the JSON brand profile. */ export const BRAND_ENV_VAR = "SENPI_BRAND"; function readChangelog(source) { const value = source.changelog; if (typeof value !== "object" || value === null || Array.isArray(value)) return undefined; const changelog = value; const path = readString(changelog, "path"); if (!path?.startsWith("/") || path.includes("\0")) return undefined; const version = readString(changelog, "version"); return { path, ...(version ? { version } : {}) }; } function readExtensions(source) { const value = source.extensions; if (!Array.isArray(value)) return undefined; const paths = value.filter((path) => typeof path === "string" && isAbsolute(path) && !path.includes("\0")); return paths.length > 0 ? paths : undefined; } function readUpdateChannel(source) { const update = source.update; if (typeof update !== "object" || update === null || Array.isArray(update)) return undefined; const channel = update; const packageName = readString(channel, "packageName"); const command = readString(channel, "command"); if (!packageName || !command) return undefined; return { packageName, distTag: readString(channel, "distTag") || "latest", command, changelogUrl: readString(channel, "changelogUrl"), }; } /** * A config directory names ONE entry inside the home directory. Anything carrying a separator or * a parent reference would move agent state - and the migration that copies into it - somewhere * the user never agreed to, so such a profile is rejected rather than sanitised. */ function isSafeConfigDirName(value) { return value !== "." && value !== ".." && !/[\\/]/.test(value); } function readString(source, key) { const value = source[key]; return typeof value === "string" && value.trim() ? value.trim() : undefined; } /** * Parses a brand profile. Returns undefined for absent, malformed, or nameless input; a * malformed profile is reported once on stderr and never throws, because a broken brand * must not stop the agent from starting. */ export function parseBrandProfile(raw) { if (!raw?.trim()) return undefined; let parsed; try { parsed = JSON.parse(raw); } catch { process.stderr.write(`warning: ignoring malformed ${BRAND_ENV_VAR} (expected JSON)\n`); return undefined; } if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) { process.stderr.write(`warning: ignoring malformed ${BRAND_ENV_VAR} (expected a JSON object)\n`); return undefined; } const source = parsed; const name = readString(source, "name"); if (!name) { process.stderr.write(`warning: ignoring ${BRAND_ENV_VAR} without a "name"\n`); return undefined; } const configDir = readString(source, "configDir") || `.${name}`; if (!isSafeConfigDirName(configDir)) { process.stderr.write(`warning: ignoring ${BRAND_ENV_VAR} with an unsafe "configDir"\n`); return undefined; } const changelog = readChangelog(source); const extensions = readExtensions(source); return { name, command: readString(source, "command"), displayVersion: readString(source, "displayVersion"), configDir, flatLayout: source.flatLayout === true, envPrefix: (readString(source, "envPrefix") || name).toUpperCase(), userAgent: readString(source, "userAgent") || name, originator: readString(source, "originator"), logo: readString(source, "logo"), update: readUpdateChannel(source), ...(changelog ? { changelog } : {}), ...(extensions ? { extensions } : {}), }; } /** * Parses the brand profile without touching the environment. * * The CLI entrypoint re-spawns the real agent as a child process and hands it `process.env`, * so the variable has to survive that hop. Scrubbing therefore happens explicitly in the * process that actually runs the agent - see `scrubBrandFromEnvironment`. */ export function consumeBrandProfile(env = process.env) { return parseBrandProfile(env[BRAND_ENV_VAR]); } /** * Removes the brand variable so tools spawning the engine again inherit a clean environment * and keep the engine's own identity. Called once the profile has been resolved by the * process that runs the agent loop. */ export function scrubBrandFromEnvironment(env = process.env) { delete env[BRAND_ENV_VAR]; } /** * Environment prefixes read after the brand's own prefix, so a machine configured before the * rebrand keeps working unchanged. */ const LEGACY_ENV_PREFIXES = ["SENPI", "PI"]; let cachedProfile; let profileResolved = false; /** * The active brand profile, resolved (and scrubbed from the environment) on first use. */ export function brandProfile() { if (!profileResolved) { cachedProfile = consumeBrandProfile(); profileResolved = true; } return cachedProfile; } /** exported for tests only */ export function resetBrandProfileForTests() { cachedProfile = undefined; profileResolved = false; } /** Environment variable names for one setting, most specific first. */ export function brandEnvNames(suffix, profile = brandProfile()) { const prefixes = [profile?.envPrefix, ...LEGACY_ENV_PREFIXES].filter((prefix) => typeof prefix === "string" && prefix.length > 0); return [...new Set(prefixes)].map((prefix) => `${prefix}_${suffix}`); } /** * Reads one setting across the brand's prefix and the legacy prefixes, returning the first * DEFINED value so that an explicitly empty value keeps its existing meaning. */ export function envValue(suffix, env = process.env) { for (const name of brandEnvNames(suffix)) { const value = env[name]; if (value !== undefined) return value; } return undefined; } //# sourceMappingURL=brand.js.map