config.ts
4.69 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { readFileSync, existsSync, writeFileSync, mkdirSync } from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { homedir } from 'node:os';
import {
loadConfig,
configExists,
emptyProfile,
type ModelConfig,
type FigmaConfig,
type ProjectProfile,
} from 'coderio';
import type { ServerConfigView } from './types';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
/** Canonical location for the target-project profile (画像) managed by the UI. */
export const PROJECT_PROFILE_FILE = path.resolve(homedir(), '.coderio', 'coderio.project.json');
/**
* Load the target-project profile (画像) that describes Project A's bespoke
* component library / tokens / utils / conventions. Returns `null` when the
* file does not exist, so generation falls back to generic output.
*/
export function loadProjectProfile(): ProjectProfile | null {
if (!existsSync(PROJECT_PROFILE_FILE)) return null;
try {
const raw = JSON.parse(readFileSync(PROJECT_PROFILE_FILE, 'utf8')) as Partial<ProjectProfile>;
return { ...emptyProfile(), ...raw, tokens: raw.tokens ?? [], components: raw.components ?? [], utils: raw.utils ?? [] };
} catch {
return null;
}
}
/** Persist the project profile to the canonical location (used by PUT). */
export function saveProjectProfile(profile: ProjectProfile): void {
const dir = path.dirname(PROJECT_PROFILE_FILE);
if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
writeFileSync(PROJECT_PROFILE_FILE, JSON.stringify(profile, null, 2), 'utf8');
}
/** A fresh seed template for the UI to present when no profile exists yet. */
export function projectProfileTemplate(): ProjectProfile {
return emptyProfile();
}
/**
* 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 };
}