import React, { useState, useCallback } from "react"; import { Box, Text, useInput } from "ink"; import TextInput from "./text-input.tsx"; import { CenteredBox } from "./centered-box.tsx"; import { MenuHeader } from "./menu-panel.tsx"; import { writeKeyForModel } from "../config.ts"; import { PROVIDERS } from "../providers.ts"; export function SetApiKey({ baseUrl, onComplete, onCancel }: { nickname?: string, baseUrl: string, onComplete: (apiKey: string) => any, onCancel: () => any, }) { const provider = Object.values(PROVIDERS).find(provider => { return provider.baseUrl === baseUrl; }); const name = provider?.name || baseUrl; const [ saving, setSaving ] = useState(false); const [ varValue, setVarValue ] = useState(""); const [ errorMessage, setErrorMessage ] = useState(null); useInput((_, key) => { if(key.escape) onCancel(); }); const onValueChange = useCallback((value: string) => { setErrorMessage(null); setVarValue(value); }, []); const onSubmit = useCallback(() => { if(varValue === "") { setErrorMessage("API key can't be empty"); return; } setSaving(true); writeKeyForModel({ baseUrl }, varValue).then(() => { setSaving(false); onComplete(varValue); }, () => { setSaving(false); setErrorMessage("Write to key file failed. Is your filesystem corrupt?"); }); }, [ varValue ]); if(saving) { return } return Enter your API key for {name}{name !== baseUrl ? "" : "." } API key: { errorMessage && { errorMessage } } }