|
| 1 | +import prompts from 'prompts'; |
| 2 | +import { checkIfConfigExists, createConfigFile } from '../../config-file'; |
| 3 | +import * as output from '../../output'; |
| 4 | +import { resolveProvider } from '../../providers'; |
| 5 | + |
| 6 | +export async function init() { |
| 7 | + try { |
| 8 | + await initInternal(); |
| 9 | + } catch (error) { |
| 10 | + output.clearLine(); |
| 11 | + output.outputError(error); |
| 12 | + process.exit(1); |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +async function initInternal() { |
| 17 | + const configExists = checkIfConfigExists(); |
| 18 | + |
| 19 | + if (configExists) { |
| 20 | + const response = await prompts({ |
| 21 | + type: 'confirm', |
| 22 | + message: 'Config found, do you want to re-initialize it?', |
| 23 | + name: 'reinitialize', |
| 24 | + }); |
| 25 | + |
| 26 | + if (!response.reinitialize) { |
| 27 | + output.outputBold('Cancelling initialization'); |
| 28 | + return; |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + output.outputBold("Welcome to AI CLI. Let's set you up quickly."); |
| 33 | + |
| 34 | + const response = await prompts([ |
| 35 | + { |
| 36 | + type: 'select', |
| 37 | + name: 'provider', |
| 38 | + message: 'Which inference provider would you like to use:', |
| 39 | + choices: [ |
| 40 | + { title: 'OpenAI', value: 'openai' }, |
| 41 | + { title: 'Perplexity', value: 'perplexity' }, |
| 42 | + ], |
| 43 | + initial: 0, |
| 44 | + hint: '', |
| 45 | + }, |
| 46 | + { |
| 47 | + type: 'confirm', |
| 48 | + message: (_, { provider }) => |
| 49 | + `Do you already have ${resolveProvider(provider).label} API key?`, |
| 50 | + name: 'hasApiKey', |
| 51 | + }, |
| 52 | + { |
| 53 | + type: (prev) => (prev ? 'password' : null), |
| 54 | + name: 'apiKey', |
| 55 | + message: (_, { provider }) => `Paste ${resolveProvider(provider).label} API key here:`, |
| 56 | + mask: '', |
| 57 | + validate: (value) => (value === '' ? 'API key cannot be an empty string' : true), |
| 58 | + }, |
| 59 | + ]); |
| 60 | + |
| 61 | + if (!response.hasApiKey) { |
| 62 | + const provider = resolveProvider(response.provider); |
| 63 | + output.outputDefault(`You can get your ${provider.label} API key here:`); |
| 64 | + output.outputDefault(provider.apiKeyUrl); |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + await createConfigFile({ |
| 69 | + providers: { |
| 70 | + [response.provider]: { |
| 71 | + apiKey: response.apiKey, |
| 72 | + }, |
| 73 | + }, |
| 74 | + }); |
| 75 | + |
| 76 | + output.outputBold( |
| 77 | + "\nI have written your settings into '~/.airc.json` file. You can now start using AI CLI.\n" |
| 78 | + ); |
| 79 | + output.outputBold('For a single question and answer just pass the prompt as param'); |
| 80 | + output.outputDefault('$ ai "Tell me a joke" \n'); |
| 81 | + |
| 82 | + output.outputBold('For interactive session use "-i" (or "--interactive") option. '); |
| 83 | + output.outputDefault('$ ai -i "Tell me an interesting fact about JavaScript"\n'); |
| 84 | + |
| 85 | + output.outputBold('or just start "ai" without any params.'); |
| 86 | + output.outputDefault('$ ai \n'); |
| 87 | +} |
0 commit comments