Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: offer to store api key locally #30

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 42 additions & 24 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import 'dotenv/config'

import { execSync } from 'node:child_process'

import { createOpenAI } from '@ai-sdk/openai'
import { confirm, log, select, spinner, text } from '@clack/prompts'
import { CoreMessage, generateText } from 'ai'
Expand Down Expand Up @@ -48,30 +50,46 @@ console.log()

const OPENAI_API_KEY =
process.env.OPENAI_API_KEY ||
((await text({
message: dedent`
Please provide your OpenAI API key.

To skip this message, set ${chalk.bold('OPENAI_API_KEY')} env variable, and run again.

You can do it in three ways:
- by creating an ${chalk.bold('.env.local')} file (make sure to ${chalk.bold('.gitignore')} it)
${chalk.gray(`\`\`\`
OPENAI_API_KEY=<your-key>
\`\`\`
`)}
- by passing it inline:
${chalk.gray(`\`\`\`
OPENAI_API_KEY=<your-key> npx cali
\`\`\`
`)}
- by setting it as an env variable in your shell (e.g. in ~/.zshrc or ~/.bashrc):
${chalk.gray(`\`\`\`
export OPENAI_API_KEY=<your-key>
\`\`\`
`)}
`,
})) as string)
(await (async () => {
let apiKey: string | symbol
do {
apiKey = await text({
message: dedent`
${chalk.bold('Please provide your OpenAI API key.')}

To skip this message, set ${chalk.bold('OPENAI_API_KEY')} env variable, and run again.

You can do it in three ways:
- by creating an ${chalk.bold('.env.local')} file (make sure to ${chalk.bold('.gitignore')} it)
${chalk.gray(`\`\`\`
OPENAI_API_KEY=<your-key>
\`\`\`
`)}
- by passing it inline:
${chalk.gray(`\`\`\`
OPENAI_API_KEY=<your-key> npx cali
\`\`\`
`)}
- by setting it as an env variable in your shell (e.g. in ~/.zshrc or ~/.bashrc):
${chalk.gray(`\`\`\`
export OPENAI_API_KEY=<your-key>
\`\`\`
`)},
`,
})
} while (typeof apiKey !== 'string')

const save = await confirm({
message: 'Do you want to save it for future runs in `.env.local`?',
})

if (save) {
execSync(`echo "OPENAI_API_KEY=${apiKey}" >> .env.local`)
execSync(`echo ".env.local" >> .gitignore`)
}
Comment on lines +86 to +89
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if saving to each project is a good idea, some tools introduce their own config file for example ai-cli created ~/.airc.json: https://github.com/callstack/ai-cli

I think most of the time user is going to use the same key between every project, wdyt?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah maybe let’s write to global config, that’s what I did in my project haha

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did change this locally, but I am not sure what is the best way to save into global configuration, so I guess I am going to merge it for now "as is" and we can always re-iterate later!


return apiKey
})())

const AI_MODEL = process.env.AI_MODEL || 'gpt-4o'

Expand Down