#!/usr/bin/env node import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; import { dirname, join, resolve } from 'node:path'; import { homedir } from 'node:os'; import { fileURLToPath } from 'node:url'; const __dirname = dirname(fileURLToPath(import.meta.url)); const ROOT = resolve(__dirname, '..'); const OUTPUT_DIR = join(ROOT, 'build', 'yinian-internal'); const OUTPUT_FILE = join(OUTPUT_DIR, 'model-auth-profiles.json'); const SHOULD_BUNDLE = process.env.YINIAN_BUNDLE_MODEL_AUTH === '1'; const SOURCE_AUTH_PROFILES = process.env.YINIAN_MODEL_AUTH_SOURCE ? resolve(process.env.YINIAN_MODEL_AUTH_SOURCE) : join(homedir(), '.openclaw', 'agents', 'main', 'agent', 'auth-profiles.json'); const PROFILE_IDS = ['minimax:default', 'minimax:cn']; function log(message) { console.log(`[yinian-model-auth] ${message}`); } function fail(message) { console.error(`[yinian-model-auth] ${message}`); process.exit(1); } function readJson(filePath) { try { return JSON.parse(readFileSync(filePath, 'utf8')); } catch { return null; } } function writeManifest(manifest) { mkdirSync(OUTPUT_DIR, { recursive: true }); writeFileSync(OUTPUT_FILE, `${JSON.stringify(manifest, null, 2)}\n`, 'utf8'); } rmSync(OUTPUT_DIR, { recursive: true, force: true }); if (!SHOULD_BUNDLE) { writeManifest({ bundled: false, reason: 'YINIAN_BUNDLE_MODEL_AUTH is not enabled', }); log('pilot model auth bundling disabled.'); process.exit(0); } if (!existsSync(SOURCE_AUTH_PROFILES)) { fail(`source auth profiles not found: ${SOURCE_AUTH_PROFILES}`); } const source = readJson(SOURCE_AUTH_PROFILES); const sourceProfiles = source && typeof source === 'object' && source.profiles && typeof source.profiles === 'object' ? source.profiles : {}; const profiles = {}; for (const profileId of PROFILE_IDS) { const profile = sourceProfiles[profileId]; if (!profile || typeof profile !== 'object') continue; if (profile.type !== 'api_key' || profile.provider !== 'minimax') continue; if (typeof profile.key !== 'string' || profile.key.trim().length < 8) continue; profiles[profileId] = { type: 'api_key', provider: 'minimax', key: profile.key, }; } if (!profiles['minimax:default']) { fail('minimax:default API key profile is required for pilot model auth bundling.'); } writeManifest({ bundled: true, purpose: 'internal-pilot-only', source: 'local-openclaw-auth-profiles', profileIds: Object.keys(profiles), store: { version: 1, profiles, order: { minimax: Object.keys(profiles), }, lastGood: { minimax: 'minimax:default', }, }, }); log(`bundled ${Object.keys(profiles).length} internal pilot model auth profile(s).`);