config.ts
3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { readFileSync, existsSync } from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { loadConfig, configExists, type ModelConfig, type FigmaConfig } from 'coderio';
import type { ServerConfigView } from './types';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
/**
* Minimal `.env` loader (no external dependency). Reads the first `.env` found
* in CWD or the server package dir, populating `process.env` for keys that are
* not already set. This keeps secrets server-side and out of the frontend.
*/
export function loadDotEnv(): void {
const candidates = [
path.resolve(process.cwd(), '.env'),
path.join(__dirname, '..', '.env'),
];
for (const file of candidates) {
if (existsSync(file)) {
try {
const raw = readFileSync(file, 'utf8');
for (const line of raw.split('\n')) {
const m = line.match(/^\s*([\w.-]+)\s*=\s*(.*)\s*$/);
if (!m) continue;
const key = m[1];
const val = m[2].replace(/^["']|["']$/g, '');
if (process.env[key] === undefined) process.env[key] = val;
}
} catch {
/* ignore malformed .env */
}
return;
}
}
}
/**
* The full credential bundle the server uses to drive the coderio engine.
* Resolution order: environment variables > ~/.coderio/config.yaml > defaults.
*/
export interface ServerModelConfig {
provider: string;
model: string;
baseUrl: string;
apiKey: string;
figmaToken: string;
}
export function loadServerModelConfig(): ServerModelConfig {
const env = process.env;
let fileModel: Partial<ModelConfig> = {};
let fileFigma: Partial<FigmaConfig> = {};
if (configExists()) {
try {
const c = loadConfig();
fileModel = c.model ?? {};
fileFigma = c.figma ?? {};
} catch {
/* ignore unreadable config */
}
}
return {
provider: env.CODERIO_PROVIDER ?? fileModel.provider ?? 'openai',
model: env.CODERIO_MODEL ?? fileModel.model ?? 'qwen3.8-max',
baseUrl: env.CODERIO_BASE_URL ?? fileModel.baseUrl ?? '',
apiKey: env.CODERIO_API_KEY ?? fileModel.apiKey ?? '',
figmaToken: env.CODERIO_FIGMA_TOKEN ?? fileFigma.token ?? '',
};
}
/** Frontend-safe view: never includes the actual secret values. */
export function configView(model: ServerModelConfig, maxConcurrent: number): ServerConfigView {
return {
provider: model.provider,
model: model.model,
baseUrl: model.baseUrl,
apiKeySet: Boolean(model.apiKey),
figmaTokenSet: Boolean(model.figmaToken),
maxConcurrent,
};
}
export function resolveServerOptions() {
const dataDir = path.resolve(process.env.CODERIO_SERVER_DATA_DIR ?? './data');
const outputDir = path.resolve(process.env.CODERIO_OUTPUT_DIR ?? path.join(dataDir, 'output'));
const port = Number(process.env.CODERIO_SERVER_PORT ?? 4173);
const host = process.env.CODERIO_SERVER_HOST ?? '0.0.0.0';
const maxConcurrent = Math.max(1, Number(process.env.CODERIO_MAX_CONCURRENT ?? 1) || 1);
return { dataDir, outputDir, port, host, maxConcurrent };
}