* Get configuration value * @param key Configuration key * @returns Configuration value
(key: string)
| 345 | * @returns Configuration value |
| 346 | */ |
| 347 | async function getConfigValue(key: string): Promise<any> { |
| 348 | const configPath = Deno.env.get("SPARC2_CONFIG_PATH") || "config/sparc2-config.toml"; |
| 349 | |
| 350 | try { |
| 351 | // Check if config file exists |
| 352 | try { |
| 353 | await Deno.stat(configPath); |
| 354 | } catch { |
| 355 | // Config file doesn't exist, create it |
| 356 | await Deno.writeTextFile(configPath, "# SPARC2 Configuration\n"); |
| 357 | return undefined; |
| 358 | } |
| 359 | |
| 360 | // Read config file |
| 361 | const configContent = await Deno.readTextFile(configPath); |
| 362 | |
| 363 | // Parse TOML |
| 364 | const config = parse(configContent); |
| 365 | |
| 366 | // Handle nested keys (e.g., "agent.name") |
| 367 | const keys = key.split("."); |
| 368 | let value: any = config; |
| 369 | |
| 370 | for (const k of keys) { |
| 371 | if (value === undefined || value === null) { |
| 372 | return undefined; |
| 373 | } |
| 374 | |
| 375 | value = value[k]; |
| 376 | } |
| 377 | |
| 378 | return value; |
| 379 | } catch (error: unknown) { |
| 380 | const errorMessage = error instanceof Error ? error.message : String(error); |
| 381 | console.error(`Error reading configuration: ${errorMessage}`); |
| 382 | return undefined; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * Set configuration value |