* Set configuration value * @param key Configuration key * @param value Configuration value
(key: string, value: any)
| 389 | * @param value Configuration value |
| 390 | */ |
| 391 | async function setConfigValue(key: string, value: any): Promise<void> { |
| 392 | const configPath = Deno.env.get("SPARC2_CONFIG_PATH") || "config/sparc2-config.toml"; |
| 393 | |
| 394 | try { |
| 395 | // Read existing config |
| 396 | let config: Record<string, any> = {}; |
| 397 | |
| 398 | try { |
| 399 | const configContent = await Deno.readTextFile(configPath); |
| 400 | config = parse(configContent); |
| 401 | } catch { |
| 402 | // File doesn't exist or is empty, use empty config |
| 403 | } |
| 404 | |
| 405 | // Handle nested keys (e.g., "agent.name") |
| 406 | const keys = key.split("."); |
| 407 | let current: any = config; |
| 408 | |
| 409 | for (let i = 0; i < keys.length - 1; i++) { |
| 410 | const k = keys[i]; |
| 411 | |
| 412 | if (current[k] === undefined || current[k] === null || typeof current[k] !== "object") { |
| 413 | current[k] = {}; |
| 414 | } |
| 415 | |
| 416 | current = current[k]; |
| 417 | } |
| 418 | |
| 419 | // Set the value |
| 420 | current[keys[keys.length - 1]] = value; |
| 421 | |
| 422 | // Write config file |
| 423 | await Deno.writeTextFile(configPath, stringify(config)); |
| 424 | } catch (error: unknown) { |
| 425 | const errorMessage = error instanceof Error ? error.message : String(error); |
| 426 | console.error(`Error setting configuration: ${errorMessage}`); |
| 427 | throw error; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | /** |
| 432 | * Config command action |