diff --git a/README.md b/README.md index 5e50808..6de4f74 100644 --- a/README.md +++ b/README.md @@ -111,16 +111,21 @@ const response = await client.models(); ## API -- `chat()` -- `models()` -- `putFile()` -- `putFileStream()` -- `files()` -- `getFile()` -- `getFileContent()` -- `deleteFile()` -- `estimateTokenCount()` -- `getBalance()` +- Chat + - `chat()` + - `models()` + - `estimateTokenCount()` + +- Files + - `putFile()` + - `putFileStream()` + - `files()` + - `getFile()` + - `getFileContent()` + - `deleteFile()` + +- Others + - `getBalance()` The detail of parameters can be found at or [`test/kimi.test.js`](./test/kimi.test.js). diff --git a/bin/kimi.js b/bin/kimi.js index 106cba2..1d268b7 100755 --- a/bin/kimi.js +++ b/bin/kimi.js @@ -14,8 +14,9 @@ import Kimi from '../lib/kimi.js'; import { loadConfig, saveConfig } from '../lib/config.js'; const KIMI_RC_PATH = path.join(homedir(), '.moonshot_ai_rc'); +const rcPath = KIMI_RC_PATH; -const config = await loadConfig(KIMI_RC_PATH); +const config = await loadConfig(rcPath); async function question(prompt) { const answers = await inquirer.prompt([ @@ -57,7 +58,7 @@ async function chooseAPIKey() { mask: '*' }); config.api_key = apikey.trim(); - await saveConfig(config, KIMI_RC_PATH); + await saveConfig(config, rcPath); } if (!config.api_key) { @@ -96,7 +97,7 @@ async function chooseModel() { if (model) { config.model = model; } - await saveConfig(config, KIMI_RC_PATH); + await saveConfig(config, rcPath); } if (!config.model) { @@ -152,7 +153,7 @@ while (true) { }); config.api_key = apikey; - await saveConfig(config, KIMI_RC_PATH); + await saveConfig(config, rcPath); console.log('The new API key is set.'); continue; } @@ -169,7 +170,7 @@ while (true) { }); config.verbose = verbose === 'true'; - await saveConfig(config, KIMI_RC_PATH); + await saveConfig(config, rcPath); console.log(`The verbose mode is turned ${config.verbose ? 'on' : 'off'} now.`); continue; } diff --git a/lib/apikey.js b/lib/apikey.js new file mode 100644 index 0000000..09e86a6 --- /dev/null +++ b/lib/apikey.js @@ -0,0 +1,18 @@ +import { homedir } from 'os'; +import { join } from 'path'; +import { loadConfig } from './config.js'; + +const KIMI_RC_PATH = join(homedir(), '.moonshot_ai_rc'); + +export async function getAPIKey(rcPath = KIMI_RC_PATH) { + if (process.env.MOONSHOT_API_KEY) { + return process.env.MOONSHOT_API_KEY; + } + + const config = await loadConfig(rcPath); + if (config && config.api_key) { + return config.api_key; + } + + return ''; +}