75 lines
2.2 KiB
JavaScript
Executable File
75 lines
2.2 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { createDbPool } from '../db.mjs';
|
|
import { createLlmProviderService } from '../llm-providers.mjs';
|
|
|
|
const root = path.join(path.dirname(fileURLToPath(import.meta.url)), '..');
|
|
|
|
function loadEnvFile(filePath) {
|
|
if (!fs.existsSync(filePath)) return;
|
|
for (const line of fs.readFileSync(filePath, 'utf8').split('\n')) {
|
|
const trimmed = line.trim();
|
|
if (!trimmed || trimmed.startsWith('#')) continue;
|
|
const eq = trimmed.indexOf('=');
|
|
if (eq < 0) continue;
|
|
const key = trimmed.slice(0, eq).trim();
|
|
const value = trimmed.slice(eq + 1).trim();
|
|
if (!process.env[key]) process.env[key] = value;
|
|
}
|
|
}
|
|
|
|
function shellQuote(value) {
|
|
return `'${String(value).replace(/'/g, `'\\''`)}'`;
|
|
}
|
|
|
|
function parseArgs(argv) {
|
|
const out = {
|
|
executor: String(argv[0] ?? '').trim(),
|
|
purpose: 'default',
|
|
shell: false,
|
|
};
|
|
for (let i = 1; i < argv.length; i += 1) {
|
|
const item = argv[i];
|
|
if (item === '--purpose') out.purpose = String(argv[++i] ?? out.purpose);
|
|
else if (item === '--shell') out.shell = true;
|
|
}
|
|
return out;
|
|
}
|
|
|
|
loadEnvFile(path.join(root, '.env'));
|
|
loadEnvFile(path.join(root, '.env.local'));
|
|
|
|
const parsed = parseArgs(process.argv.slice(2));
|
|
if (!parsed.executor) {
|
|
console.error('Usage: node scripts/executor-env.mjs <executor> [--purpose default] [--shell]');
|
|
process.exit(1);
|
|
}
|
|
|
|
const pool = createDbPool();
|
|
try {
|
|
const svc = createLlmProviderService(pool, {
|
|
apiTarget: process.env.TKMIND_API_TARGET ?? 'https://127.0.0.1:18006',
|
|
apiSecret: process.env.TKMIND_SERVER__SECRET_KEY ?? 'local-dev-secret',
|
|
});
|
|
const runtime = await svc.getExecutorRuntimeConfig(parsed.executor, {
|
|
purpose: parsed.purpose,
|
|
includeSecret: true,
|
|
});
|
|
if (!runtime?.ok) {
|
|
console.error(runtime?.message ?? 'executor runtime unavailable');
|
|
process.exit(2);
|
|
}
|
|
const env = runtime.env ?? {};
|
|
if (parsed.shell) {
|
|
for (const [key, value] of Object.entries(env)) {
|
|
process.stdout.write(`export ${key}=${shellQuote(value)}\n`);
|
|
}
|
|
} else {
|
|
process.stdout.write(`${JSON.stringify({ executor: parsed.executor, env })}\n`);
|
|
}
|
|
} finally {
|
|
await pool.end().catch(() => {});
|
|
}
|