refactor: change config to yaml in root

This commit is contained in:
2026-06-28 22:00:33 +09:00
parent dcc3d8af0e
commit 1cebbf65eb
4 changed files with 42 additions and 21 deletions

View File

@@ -1,5 +0,0 @@
SUPERMEMORY_API_KEY=
BRAINBOX_ROOT_PATH=./brainbox-data
OPENROUTER_API_KEY=

View File

@@ -8,9 +8,9 @@
"@openrouter/sdk": "^0.12.79",
"chalk": "^5.6.2",
"commander": "^15.0.0",
"dotenv": "^17.4.2",
"prettier": "^3.8.3",
"supermemory": "^4.24.12",
"yaml": "^2.9.0",
},
"devDependencies": {
"@types/node": "^25.9.1",
@@ -29,8 +29,6 @@
"commander": ["commander@15.0.0", "", {}, "sha512-z67u4ZhzCL/Tydu1lJARtEZYWbWaN7oYLHbsuzocr6y4N6WZAagG3RQ4FW61V1/0+jImpj293XfrcYnd1qxtPg=="],
"dotenv": ["dotenv@17.4.2", "", {}, "sha512-nI4U3TottKAcAD9LLud4Cb7b2QztQMUEfHbvhTH09bqXTxnSie8WnjPALV/WMCrJZ6UV/qHJ6L03OqO3LcdYZw=="],
"prettier": ["prettier@3.8.3", "", { "bin": { "prettier": "bin/prettier.cjs" } }, "sha512-7igPTM53cGHMW8xWuVTydi2KO233VFiTNyF5hLJqpilHfmn8C8gPf+PS7dUT64YcXFbiMGZxS9pCSxL/Dxm/Jw=="],
"supermemory": ["supermemory@4.24.12", "", { "bin": { "supermemory": "bin/cli" } }, "sha512-xAFextuqk4JuoW33jJaFGqT1oMppN2IgfWUrV18Fv3qAAZ6M1SR1tb+7EBq8vrEQIx4iY2MQh5p+qnfL6lI8Yw=="],
@@ -39,6 +37,8 @@
"undici-types": ["undici-types@7.24.6", "", {}, "sha512-WRNW+sJgj5OBN4/0JpHFqtqzhpbnV0GuB+OozA9gCL7a993SmU+1JBZCzLNxYsbMfIeDL+lTsphD5jN5N+n0zg=="],
"yaml": ["yaml@2.9.0", "", { "bin": { "yaml": "bin.mjs" } }, "sha512-2AvhNX3mb8zd6Zy7INTtSpl1F15HW6Wnqj0srWlkKLcpYl/gMIMJiyuGq2KeI2YFxUPjdlB+3Lc10seMLtL4cA=="],
"zod": ["zod@4.4.3", "", {}, "sha512-ytENFjIJFl2UwYglde2jchW2Hwm4GJFLDiSXWdTrJQBIN9Fcyp7n4DhxJEiWNAJMV1/BqWfW/kkg71UDcHJyTQ=="],
}
}

View File

@@ -20,8 +20,8 @@
"@openrouter/sdk": "^0.12.79",
"chalk": "^5.6.2",
"commander": "^15.0.0",
"dotenv": "^17.4.2",
"prettier": "^3.8.3",
"supermemory": "^4.24.12"
"supermemory": "^4.24.12",
"yaml": "^2.9.0"
}
}

View File

@@ -1,5 +1,7 @@
import "dotenv/config";
import { join } from "path";
import { mkdirSync, readFileSync, writeFileSync } from "fs";
import { homedir } from "os";
import { dirname, join, resolve } from "path";
import { parse as parseYaml } from "yaml";
export interface Config {
openrouterApiKey: string;
@@ -7,19 +9,43 @@ export interface Config {
brainboxRoot: string;
}
const openrouterApiKey = process.env["OPENROUTER_API_KEY"];
if (!openrouterApiKey) throw new Error("OPENROUTER_API_KEY is missing");
const brainboxRoot = process.env["BRAINBOX_ROOT_PATH"]
? resolve(process.cwd(), process.env["BRAINBOX_ROOT_PATH"])
: join(homedir(), ".brainbox");
const supermemoryApiKey = process.env["SUPERMEMORY_API_KEY"];
if (!supermemoryApiKey) throw new Error("SUPERMEMORY_API_KEY is missing");
interface BrainboxYaml {
openrouter?: { apiKey?: string };
supermemory?: { apiKey?: string };
}
const brainboxRoot = join(
process.cwd(),
process.env["BRAINBOX_ROOT_PATH"] ?? "brainbox-data",
);
const yamlPath = join(brainboxRoot, "brainbox.yaml");
let parsed: BrainboxYaml = {};
try {
parsed = parseYaml(readFileSync(yamlPath, "utf8")) ?? {};
} catch (err) {
if ((err as NodeJS.ErrnoException).code === "ENOENT") {
mkdirSync(dirname(yamlPath), { recursive: true });
writeFileSync(
yamlPath,
"# Fill in your API keys, then run brainbox again.\n" +
"openrouter:\n" +
" apiKey: \n" +
"supermemory:\n" +
" apiKey: \n",
);
} else {
throw err;
}
}
const openrouterApiKey = parsed.openrouter?.apiKey;
if (!openrouterApiKey) throw new Error(`openrouter.apiKey is missing in ${yamlPath}`);
const supermemoryApiKey = parsed.supermemory?.apiKey;
if (!supermemoryApiKey) throw new Error(`supermemory.apiKey is missing in ${yamlPath}`);
export const config: Config = {
openrouterApiKey,
supermemoryApiKey,
brainboxRoot,
};
};