config.ts
6.06 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import { readFileSync, existsSync, writeFileSync, mkdirSync } from 'fs';
import { resolve } from 'path';
import { homedir } from 'os';
import yaml from 'js-yaml';
// Configuration directory in user's home
export const CONFIG_DIR = resolve(homedir(), '.coderio');
const CONFIG_FILE = resolve(CONFIG_DIR, 'config.yaml');
/**
* Model configuration interface
*/
export interface ModelConfig {
provider: string;
model: string;
baseUrl: string;
apiKey: string;
/** Force non-streaming mode. evoltagent's default streaming hangs against
* some OpenAI-compatible endpoints (e.g. Aliyun MAAS Qwen) that stream
* `reasoning_content`, so we disable it globally here. */
stream?: boolean;
}
/**
* Figma configuration interface
*/
export interface FigmaConfig {
token: string;
}
/**
* Debug configuration interface
*/
export interface DebugConfig {
enabled: boolean;
outputDir?: string;
}
/**
* Configuration file structure
*/
interface Config {
model: ModelConfig;
figma: FigmaConfig;
debug?: DebugConfig;
}
// Cache for loaded configuration
let cachedConfig: Config | null = null;
/**
* Load configuration from user's config directory
* The configuration is cached after first load
* @returns Full configuration object
*/
export function loadConfig(): Config {
if (cachedConfig) {
return cachedConfig;
}
if (!existsSync(CONFIG_FILE)) {
throw new Error(`Configuration file not found at: ${CONFIG_FILE}\n` + `Please create the configuration file.`);
}
try {
const fileContent = readFileSync(CONFIG_FILE, 'utf8');
const rawConfig = yaml.load(fileContent) as Config;
if (!rawConfig) {
throw new Error('Invalid config.yaml structure: configuration is empty');
}
cachedConfig = rawConfig;
return cachedConfig;
} catch (error) {
if (error instanceof Error) {
throw new Error(`Failed to load config.yaml: ${error.message}`);
}
throw error;
}
}
/**
* Get model configuration from config.yaml
* @returns Model configuration
* @throws Error if configuration is invalid
*/
export function getModelConfig(): ModelConfig {
if (runtimeModel) {
return { ...runtimeModel, stream: false };
}
const config = loadConfig();
if (!config.model) {
throw new Error('Model configuration not found in config.yaml');
}
// Disable streaming globally: evoltagent defaults to streaming, which hangs
// against OpenAI-compatible endpoints that emit `reasoning_content` chunks
// (e.g. Aliyun MAAS Qwen). Non-streaming is verified working for those.
return { ...config.model, stream: false };
}
/**
* Get Figma configuration from config.yaml
* @returns Figma configuration
* @throws Error if configuration is invalid
*/
export function getFigmaConfig(): FigmaConfig {
if (runtimeFigma) {
return runtimeFigma;
}
const config = loadConfig();
if (!config.figma) {
throw new Error('Figma configuration not found in config.yaml');
}
return config.figma;
}
/**
* Get debug configuration from config.yaml
* @returns Debug configuration (defaults to disabled if not specified)
*/
export function getDebugConfig(): DebugConfig {
const config = loadConfig();
return config.debug || { enabled: false };
}
/**
* Get the path to the configuration file
*/
export function getConfigPath(): string {
return CONFIG_FILE;
}
/**
* Check if configuration file exists
*/
export function configExists(): boolean {
return existsSync(CONFIG_FILE);
}
/**
* Clear the configuration cache
* Useful for testing or when configuration needs to be reloaded
*/
export function clearConfigCache(): void {
cachedConfig = null;
}
/**
* Runtime configuration override injected by the orchestration backend
* (apps/server). When set, `getModelConfig()` / `getFigmaConfig()` return it
* instead of the file-based config, so the backend can own credentials per
* request without relying on a global `~/.coderio/config.yaml`.
*/
let runtimeModel: ModelConfig | null = null;
let runtimeFigma: FigmaConfig | null = null;
export function setRuntimeConfig(override: {
model?: Partial<ModelConfig>;
figma?: Partial<FigmaConfig>;
}): void {
if (override.model) {
runtimeModel = {
...(runtimeModel ?? { provider: '', model: '', baseUrl: '', apiKey: '' }),
...override.model,
} as ModelConfig;
}
if (override.figma) {
runtimeFigma = { ...(runtimeFigma ?? { token: '' }), ...override.figma } as FigmaConfig;
}
}
export function clearRuntimeConfig(): void {
runtimeModel = null;
runtimeFigma = null;
}
/**
* Merge a partial configuration into the existing config.yaml and persist it.
*
* Only the provided top-level keys are merged (shallow per section), so callers
* can update `model` without touching `figma`. The in-memory cache is cleared
* afterwards so subsequent `loadConfig()` reads the new values.
*
* @param partial - Sections to merge (e.g. `{ model: {...}, figma: {...} }`)
*/
export function saveConfig(partial: Partial<Config>): void {
let existing: Partial<Config> = {};
if (configExists()) {
try {
const raw = yaml.load(readFileSync(CONFIG_FILE, 'utf8')) as Partial<Config> | null;
if (raw && typeof raw === 'object') {
existing = raw;
}
} catch {
existing = {};
}
}
const merged: Config = {
model: { ...(existing.model as ModelConfig), ...(partial.model as Partial<ModelConfig>) } as ModelConfig,
figma: { ...(existing.figma as FigmaConfig), ...(partial.figma as Partial<FigmaConfig>) } as FigmaConfig,
};
if (partial.debug !== undefined) {
merged.debug = { ...(existing.debug as DebugConfig), ...partial.debug } as DebugConfig;
} else if (existing.debug) {
merged.debug = existing.debug;
}
if (!existsSync(CONFIG_DIR)) {
mkdirSync(CONFIG_DIR, { recursive: true });
}
writeFileSync(CONFIG_FILE, yaml.dump(merged), 'utf8');
clearConfigCache();
}