Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.
JavaScript 3d-generationai-agentsanimationavatardigital-humangenerative-aiglbgltfimage-to-3dllmmcpmcp-servermodel-context-protocolsolanatext-to-3dthreejsweb-componentswebglwebxrx402
refactor(scripts): resolve service env through one helper that follows Secret Manager refs
nirholas7ed405f503db
diff --git a/scripts/lib/service-env.mjs b/scripts/lib/service-env.mjsnew file mode 100644index 000000000..d701eebf7--- /dev/null+++ b/scripts/lib/service-env.mjs@@ -0,0 +1,86 @@+// Read environment values off a Cloud Run service, following Secret Manager+// references.+//+// Production's authoritative env lives on the `three-ws-api` service, and a pile+// of operator scripts reach for it when `.env` has no copy (`vercel env pull`+// returns empty for secret-type vars, so it is never a substitute). Those scripts+// all read `env[].value`, which stopped carrying credentials on 2026-09-02 when+// every credential-bearing var moved into Secret Manager+// (scripts/migrate-plaintext-secrets.mjs): `describe` now returns a+// `valueFrom.secretKeyRef` in its place, and a `.value` read silently yields+// undefined. This module resolves both shapes so one helper serves both eras.+//+// Every function takes its gcloud callers as arguments so a test can drive them+// without a network or an authenticated session.++import { execFileSync } from 'node:child_process';+import './gcloud-path.mjs';++export const DEFAULT_SERVICE = 'three-ws-api';+export const DEFAULT_REGION = 'us-central1';+export const DEFAULT_PROJECT = 'aerial-vehicle-466722-p5';++export function describeService({+ service = DEFAULT_SERVICE,+ region = DEFAULT_REGION,+ project = DEFAULT_PROJECT,+} = {}) {+ return execFileSync(+ 'gcloud',+ ['run', 'services', 'describe', service, `--region=${region}`, `--project=${project}`, '--format=json'],+ { encoding: 'utf8', maxBuffer: 64 * 1024 * 1024, stdio: ['ignore', 'pipe', 'pipe'] },+ );+}++export function accessSecretVersion(secretName, version = 'latest', { project = DEFAULT_PROJECT } = {}) {+ return execFileSync(+ 'gcloud',+ ['secrets', 'versions', 'access', version, `--secret=${secretName}`, `--project=${project}`],+ { encoding: 'utf8', maxBuffer: 4 * 1024 * 1024, stdio: ['ignore', 'pipe', 'pipe'] },+ );+}++/** Every env entry on the service, in declaration order. */+export function serviceEnvEntries(opts = {}) {+ const describe = opts.describe || describeService;+ const svc = JSON.parse(describe(opts));+ return svc?.spec?.template?.spec?.containers?.[0]?.env || [];+}++/**+ * The value of one env var, whether it is stored as a literal or as a Secret+ * Manager reference. Returns null when the service does not carry the name, or+ * when the reference cannot be read (which usually means the caller is missing+ * `roles/secretmanager.secretAccessor` on that one secret).+ *+ * The payload is returned byte for byte, exactly what the container receives.+ * Trimming here would hand a caller a value production never uses.+ */+export function serviceEnvValue(name, opts = {}) {+ const access = opts.accessVersion || accessSecretVersion;+ const entry = serviceEnvEntries(opts).find((e) => e.name === name);+ if (!entry) return null;+ if (entry.value !== undefined && entry.value !== '') return entry.value;+ const ref = entry.valueFrom?.secretKeyRef;+ if (!ref?.name) return null;+ try {+ return access(ref.name, ref.key || 'latest', opts) || null;+ } catch {+ return null;+ }+}++/**+ * Same, but throws with the places to look instead of returning null, for the+ * scripts that cannot do anything useful without the value.+ */+export function requireServiceEnvValue(name, opts = {}) {+ const value = serviceEnvValue(name, opts);+ if (value) return value;+ throw new Error(+ `${name} is not readable: not in this process's env, and not resolvable off the ` ++ `${opts.service || DEFAULT_SERVICE} Cloud Run service. If it is a Secret Manager ` ++ `reference, this account needs roles/secretmanager.secretAccessor on that secret. ` ++ `Inspect with: node scripts/read-service-env.mjs '^${name}$' --names`,+ );+}diff --git a/scripts/rug-signature.mjs b/scripts/rug-signature.mjsindex 241b4d5f4..1345e5c38 100644--- a/scripts/rug-signature.mjs+++ b/scripts/rug-signature.mjs@@ -20,9 +20,9 @@// must compare rugs against WINNERS on the features visible at entry, and against// flats to make sure we're not just describing "low activity".-import { execSync } from 'node:child_process';import './lib/gcloud-path.mjs';import { createRequire } from 'node:module';+import { requireServiceEnvValue } from './lib/service-env.mjs';const require = createRequire(import.meta.url);const { Pool } = require('@neondatabase/serverless');@@ -41,14 +41,9 @@ const ACTIVE = activeBuyers ? `and i.unique_buyers >= ${activeBuyers}` : '';function resolveDatabaseUrl() {if (process.env.DATABASE_URL) return process.env.DATABASE_URL;- const svc = JSON.parse(execSync(- 'gcloud run services describe three-ws-api --region us-central1 --project aerial-vehicle-466722-p5 --format=json',- { encoding: 'utf8' },- ));- const env = svc.spec.template.spec.containers[0].env || [];- const row = env.find((e) => e.name === 'DATABASE_URL');- if (row?.value) return row.value;- throw new Error('DATABASE_URL not found in env or Cloud Run service');+ // Production's copy is a Secret Manager reference, not a literal on the+ // service, so this has to resolve the reference rather than read `.value`.+ return requireServiceEnvValue('DATABASE_URL');}// The entry-time features. Each is a SQL expression over pump_coin_intel (i) sodiff --git a/scripts/seed-sniper-experiments.mjs b/scripts/seed-sniper-experiments.mjsindex de55ca650..1166b4451 100644--- a/scripts/seed-sniper-experiments.mjs+++ b/scripts/seed-sniper-experiments.mjs@@ -23,9 +23,9 @@// (JWT_SECRET fallback) — run this with the SAME key material the agent-sniper// service decrypts with, or the worker will load a wallet it cannot open.-import { execSync } from 'node:child_process';import './lib/gcloud-path.mjs';import { createRequire } from 'node:module';+import { requireServiceEnvValue } from './lib/service-env.mjs';const require = createRequire(import.meta.url);const { Pool } = require('@neondatabase/serverless');@@ -276,14 +276,9 @@ async function provisionArm(pool, arm) {function resolveDatabaseUrl() {if (process.env.DATABASE_URL) return process.env.DATABASE_URL;- const svc = JSON.parse(execSync(- 'gcloud run services describe three-ws-api --region us-central1 --project aerial-vehicle-466722-p5 --format=json',- { encoding: 'utf8', maxBuffer: 64 * 1024 * 1024 },- ));- const env = svc.spec.template.spec.containers[0].env || [];- const url = env.find((e) => e.name === 'DATABASE_URL')?.value;- if (!url) throw new Error('DATABASE_URL not found in env or on the Cloud Run service');- return url;+ // Production's copy is a Secret Manager reference, not a literal on the+ // service, so this has to resolve the reference rather than read `.value`.+ return requireServiceEnvValue('DATABASE_URL');}const pool = new Pool({ connectionString: resolveDatabaseUrl() });diff --git a/scripts/sniper-evolve.mjs b/scripts/sniper-evolve.mjsindex 0aa2c7e23..9c5a2391e 100644--- a/scripts/sniper-evolve.mjs+++ b/scripts/sniper-evolve.mjs@@ -39,11 +39,11 @@// journaled to sniper_evolution_log (before/after + the evidence) so the whole// autonomous history is auditable and one UPDATE can roll any change back.-import { execSync } from 'node:child_process';import './lib/gcloud-path.mjs';import { createRequire } from 'node:module';import { budgetWeightFor, classifyAutonomy } from '../api/_lib/sniper-autonomy.js';import { resolveDatabaseUrl as resolveConfiguredDatabaseUrl } from '../api/_lib/env.js';+import { requireServiceEnvValue } from './lib/service-env.mjs';const require = createRequire(import.meta.url);const { Pool } = require('@neondatabase/serverless');@@ -74,14 +74,9 @@ const WRITABLE = new Set(['enabled', 'daily_budget_lamports']);function resolveDatabaseUrl() {const configured = resolveConfiguredDatabaseUrl();if (configured) return configured;- const svc = JSON.parse(execSync(- 'gcloud run services describe three-ws-api --region us-central1 --project aerial-vehicle-466722-p5 --format=json',- { encoding: 'utf8', maxBuffer: 64 * 1024 * 1024 },- ));- const env = svc.spec.template.spec.containers[0].env || [];- const url = env.find((e) => e.name === 'DATABASE_URL')?.value;- if (!url) throw new Error('DATABASE_URL not found in env or on the Cloud Run service');- return url;+ // Production's copy is a Secret Manager reference, not a literal on the+ // service, so this has to resolve the reference rather than read `.value`.+ return requireServiceEnvValue('DATABASE_URL');}// Wilson score interval for a binomial proportion. Gives a conservative lower