/** * @license * Copyright 2025 Vybestack LLC * SPDX-License-Identifier: Apache-2.0 */ import type React from 'react'; import { useState, useCallback } from 'react'; import { Box, Text } from 'ink'; import { Colors } from '../../colors.js'; import { TextInput } from './TextInput.js'; import { useKeypress } from '../../hooks/useKeypress.js'; import { PROVIDER_OPTIONS } from './constants.js'; import { validateBaseUrl } from './validation.js'; import { getStepPosition } from './utils.js'; import type { WizardState } from './types.js'; import { firstNonEmptyString } from '../../../utils/coalesce.js'; const CustomProviderExamples: React.FC = () => ( <> Examples: • https://api.x.ai/v1/ • https://openrouter.ai/api/v1/ • https://api.fireworks.ai/inference/v1/ ); interface BaseUrlConfigStepProps { state: WizardState; onUpdateBaseUrl: (baseUrl: string) => void; onContinue: () => void; onBack: () => void; } export const BaseUrlConfigStep: React.FC = ({ state, onUpdateBaseUrl, onContinue, onBack, }) => { useKeypress( (key) => { if (key.name === 'escape') { onBack(); } }, { isActive: true }, ); const providerOption = PROVIDER_OPTIONS.find( (p) => p.value === state.config.provider, ); const defaultBaseUrl = providerOption?.defaultBaseUrl ?? ''; const [inputValue, setInputValue] = useState( firstNonEmptyString(state.config.baseUrl, defaultBaseUrl), ); const [validationError, setValidationError] = useState(null); const handleInputChange = useCallback( (value: string) => { setInputValue(value); const validation = validateBaseUrl(value); setValidationError(validation.valid ? null : (validation.error ?? null)); if (validation.valid) { onUpdateBaseUrl(value); } }, [onUpdateBaseUrl], ); const handleInputSubmit = useCallback(() => { const validation = validateBaseUrl(inputValue); if (validation.valid) { onContinue(); } else { setValidationError(validation.error ?? 'Invalid URL'); } }, [inputValue, onContinue]); const isCustomProvider = state.config.provider === 'custom'; const helpText = isCustomProvider ? 'Enter the API endpoint for your custom provider:' : `${providerOption?.label ?? 'This provider'} typically runs on the default port. Edit if using a different configuration.`; const { current, total } = getStepPosition(state); return ( Create New Profile - Step {current} of {total} Configure Base URL: {helpText} {isCustomProvider && } Base URL: {validationError && ( ✗ {validationError} )} {!validationError && inputValue && ( ✓ Valid )} ← → Move cursor Enter Continue Esc Back ); };