22
33import 'dotenv/config'
44
5- import { execSync } from 'node:child_process'
6-
75import { createOpenAI } from '@ai-sdk/openai'
86import { confirm , outro , select , spinner , text } from '@clack/prompts'
97import { CoreMessage , generateText } from 'ai'
@@ -14,6 +12,7 @@ import { retro } from 'gradient-string'
1412import { z } from 'zod'
1513
1614import { reactNativePrompt } from './prompt.js'
15+ import { getApiKey } from './utils.js'
1716
1817const MessageSchema = z . union ( [
1918 z . object ( { type : z . literal ( 'select' ) , content : z . string ( ) , options : z . array ( z . string ( ) ) } ) ,
@@ -48,56 +47,12 @@ console.log(
4847 ` )
4948)
5049
51- const OPENAI_API_KEY =
52- process . env . OPENAI_API_KEY ||
53- ( await ( async ( ) => {
54- const apiKey = await text ( {
55- message : dedent `
56- ${ chalk . bold ( 'Please provide your OpenAI API key.' ) }
57-
58- To skip this message, set ${ chalk . bold ( 'OPENAI_API_KEY' ) } env variable, and run again.
59-
60- You can do it in three ways:
61- - by creating an ${ chalk . bold ( '.env.local' ) } file (make sure to ${ chalk . bold ( '.gitignore' ) } it)
62- ${ chalk . gray ( `\`\`\`
63- OPENAI_API_KEY=<your-key>
64- \`\`\`
65- ` ) }
66- - by passing it inline:
67- ${ chalk . gray ( `\`\`\`
68- OPENAI_API_KEY=<your-key> npx cali
69- \`\`\`
70- ` ) }
71- - by setting it as an env variable in your shell (e.g. in ~/.zshrc or ~/.bashrc):
72- ${ chalk . gray ( `\`\`\`
73- export OPENAI_API_KEY=<your-key>
74- \`\`\`
75- ` ) } ,
76- ` ,
77- validate : ( value ) => ( value . length > 0 ? undefined : 'Please provide a valid answer.' ) ,
78- } )
79-
80- if ( typeof apiKey === 'symbol' ) {
81- outro ( chalk . gray ( 'Bye!' ) )
82- process . exit ( 0 )
83- }
84-
85- const save = await confirm ( {
86- message : 'Do you want to save it for future runs in `.env.local`?' ,
87- } )
88-
89- if ( save ) {
90- execSync ( `echo "OPENAI_API_KEY=${ apiKey } " >> .env.local` )
91- execSync ( `echo ".env.local" >> .gitignore` )
92- }
93-
94- return apiKey
95- } ) ( ) )
50+ console . log ( )
9651
9752const AI_MODEL = process . env . AI_MODEL || 'gpt-4o'
9853
9954const openai = createOpenAI ( {
100- apiKey : OPENAI_API_KEY ,
55+ apiKey : await getApiKey ( 'OpenAI' , 'OPENAI_API_K2EY' ) ,
10156} )
10257
10358async function startSession ( ) : Promise < CoreMessage [ ] > {
0 commit comments