src — wizard
src — wizard
The src/wizard module provides interactive command-line interfaces to guide users through the initial setup and configuration of Code Buddy, particularly concerning AI provider integration and API key validation. It's designed to be user-friendly, ensuring that developers can quickly get started with the tool.
This module is split into two main files, each serving a distinct but related purpose:
onboarding.ts: Handles the initial, high-level setup of Code Buddy, including selecting a primary AI provider, model, and text-to-speech (TTS) options. It generates the coreconfig.jsonfile.provider-onboarding.ts: Focuses on more detailed, provider-specific setup, primarily validating API keys and guiding users on how to set up environment variables for various AI services.
src/wizard/onboarding.ts: Initial Setup Wizard
This file contains the primary onboarding wizard that new users encounter when first setting up Code Buddy. Its goal is to collect essential configuration details and persist them to a local configuration file.
Purpose
The runOnboarding function orchestrates a series of interactive prompts to gather information such as:
- The user's preferred AI provider (e.g., Grok, Claude, ChatGPT).
- The specific model to use with that provider.
- Whether text-to-speech (TTS) should be enabled and, if so, which TTS provider.
- Optionally, an API key for the chosen provider, though it primarily advises on environment variables.
The collected information is then saved into a config.json file within the .codebuddy directory in the current working directory.
How it Works
The runOnboarding function guides the user through a step-by-step process:
- Welcome Message: Displays a friendly welcome banner.
- Provider Selection: Uses
askChoiceto present a list of supported AI providers (PROVIDERS) and allows the user to select one. - API Key Guidance: Based on the selected provider, it checks
PROVIDER_ENV_MAPto identify the relevant environment variable for the API key. It prompts the user to enter the key or reminds them to set the environment variable later. - Model Selection: Uses
PROVIDER_DEFAULT_MODELto suggest a default model for the chosen provider and allows the user to override it viaask. - TTS Configuration: Asks if TTS should be enabled and, if yes, prompts for a TTS provider using
askChoice. - Configuration Persistence: Calls
writeConfigto create or update the.codebuddy/config.jsonfile with the collected settings. - Summary: Prints a summary of the configured settings and any remaining actions (like setting environment variables).
Execution Flow
graph TD
A[Start runOnboarding] --> B{Display Welcome};
B --> C{Ask for AI Provider};
C --> D{Ask for API Key (if applicable)};
D --> E{Ask for Model};
E --> F{Ask for TTS Enablement};
F -- TTS Enabled --> G{Ask for TTS Provider};
F -- TTS Disabled --> H[Construct OnboardingResult];
G --> H;
H --> I[Call writeConfig];
I --> J{Display Summary};
J --> K[End runOnboarding];
Key Components
OnboardingResultinterface: Defines the structure of the configuration data collected by the wizard.
export interface OnboardingResult {
provider: string;
apiKey: string; class="hl-cmt">// Note: This is collected but not saved to config.json directly
model: string;
ttsEnabled: boolean;
ttsProvider?: string;
}
PROVIDER_ENV_MAP: A mapping from provider IDs (e.g.,grok,chatgpt) to their corresponding environment variable names (e.g.,GROK_API_KEY,OPENAI_API_KEY). This helps guide the user on where to store their API keys.PROVIDER_DEFAULT_MODEL: Maps provider IDs to their recommended default model, sourced fromMODEL_DEFAULTS.ask(rl, question, defaultValue): A utility function to prompt the user for a free-form text input.askChoice(rl, question, choices, defaultIdx): A utility function to prompt the user to select from a list of numbered choices.writeConfig(configDir, result): Creates the.codebuddydirectory if it doesn't exist and writes theOnboardingResult(excluding theapiKey) toconfig.json.
Configuration Output
The writeConfig function generates a config.json file similar to this:
// .codebuddy/config.json
{
"provider": "chatgpt",
"model": "gpt-4o",
"ttsEnabled": true,
"ttsProvider": "edge-tts"
}
Usage
The runOnboarding function is typically invoked as part of the CLI's initial setup command.
Example Call (from CLI):
class="hl-cmt">// commands/cli/utility-commands.ts
import { runOnboarding } from 39;../src/wizard/onboarding.js39;;
class="hl-cmt">// ...
registerUtilityCommands(program) {
program
.command(39;setup39;)
.description(39;Run the initial setup wizard for Code Buddy39;)
.action(async () => {
await runOnboarding();
});
}
src/wizard/provider-onboarding.ts: Advanced Provider Configuration
This file provides a more granular and robust onboarding experience for individual AI providers, focusing on API key validation and environment variable setup. It's designed to ensure that a user's API key is functional before they proceed.
Purpose
The primary goals of this module are:
- Guided API Key Setup: Walk the user through obtaining and entering API keys for various AI providers.
- API Key Validation: Programmatically test the entered API key against the provider's API to confirm its validity and connectivity.
- Model Discovery: Attempt to list available models from the provider's API, giving the user immediate feedback on what they can use.
- Environment Variable Guidance: Instruct the user on how to persist their API key using environment variables.
- Local Provider Connectivity: For local providers like Ollama or LM Studio, it checks if the local server is running and accessible.
How it Works (High-Level)
The module defines a set of PROVIDER_CONFIGS that contain all necessary information for each AI provider (API endpoints, environment variable names, instructions). The interactive functions (runProviderOnboarding, runFullProviderOnboarding) use these configs to guide the user, while validateProviderKey performs the actual API key verification.
Core Logic: validateProviderKey
This asynchronous function is the heart of the provider onboarding. It attempts to make a GET request to a provider-specific validation endpoint using the provided API key.
- URL & Headers Construction:
- It constructs the full validation URL using
config.baseUrlandconfig.validateEndpoint. - It dynamically builds HTTP headers, handling different authentication schemes (e.g.,
Authorization: Bearerfor OpenAI,x-api-keyfor Anthropic, query parameters for Google Gemini, or no auth for local providers).
fetchRequest: It performs afetchrequest to the constructed URL with a 15-second timeout.- Error Handling:
- Catches network errors (e.g., connection refused, timeout).
- Checks HTTP status codes:
401or403typically indicate an invalid API key.- Other non-
2xxcodes are reported as API errors.
- Model Extraction: If the request is successful (
response.ok), it callsextractModelsto parse the response body and identify available models. - Result: Returns a
ProviderValidationResultindicatingvalid: true/falseand optionallymodelsor anerrormessage.
Provider Configurations (PROVIDER_CONFIGS)
This array holds detailed configuration objects for each supported AI provider. Each ProviderOnboardingConfig includes:
id: Unique identifier (e.g.,grok,openai).name: Human-readable name (e.g.,Grok (xAI)).envKey: The environment variable name for the API key (e.g.,GROK_API_KEY).baseUrl: The base URL for the provider's API.validateEndpoint: A relative path to an endpoint that can be used to test API key validity (e.g.,/v1/models).instructions: User-facing guidance on where to obtain an API key.oauthFlow(optional): Placeholder for future OAuth integration.
This centralized configuration makes it easy to add or update provider details.
Interactive Flows
runProviderOnboarding(providerId)
This function provides an interactive wizard for a specific provider.
- Find Config: Retrieves the
ProviderOnboardingConfigfor the givenproviderId. - Existing Key Check: Checks
process.envfor an existing API key. If found, it offers to re-validate it. - Local Provider Handling: For
ollamaandlmstudio, it performs a connectivity check to their default local host (http://localhost:11434orhttp://localhost:1234) instead of prompting for an API key. - API Key Prompt: Prompts the user to enter their API key.
- Validation: Calls
validateProviderKeywith the entered key. - Feedback & Persistence: Reports the validation result. If valid, it sets
process.env[config.envKey]for the current session and provides instructions for persisting the key in the user's shell profile.
runFullProviderOnboarding()
This function allows the user to choose which provider they want to onboard.
- Provider Selection: Uses an
askChoiceprompt to list all providers fromPROVIDER_CONFIGS. - Delegate: Once a provider is chosen, it delegates to
runProviderOnboardingfor the actual setup process.
Utility Functions
getProviderConfig(providerId): Retrieves aProviderOnboardingConfigobject by its ID.listConfiguredProviders(): Returns an array ofProviderOnboardingConfigobjects for providers that either have their environment variable set or are local providers (Ollama, LM Studio).
Integration and Extension
CLI Integration
Both runOnboarding and runFullProviderOnboarding are designed to be called from CLI commands. runOnboarding is typically for the initial setup command, while runFullProviderOnboarding could be used for a provider-setup or configure command.
Adding New Providers
To add support for a new AI provider:
- Update
PROVIDER_CONFIGS: Add a newProviderOnboardingConfigobject to thePROVIDER_CONFIGSarray insrc/wizard/provider-onboarding.ts. Ensure all fields are correctly populated, especiallybaseUrl,validateEndpoint, andenvKey. - Update
extractModels(if needed): If the new provider's/v1/models(or equivalent) endpoint returns a model list in a non-standard format (i.e., notdata: [{ id: "model-name" }]), you'll need to add a specific parsing logic within theextractModelsfunction. - Update
validateProviderKey(if needed): If the new provider uses a unique authentication header or query parameter scheme not covered by existing logic, add a specific condition to theheadersorfinalUrlconstruction invalidateProviderKey. - Update
onboarding.ts(optional): If this new provider should be part of the initialrunOnboardingwizard, add itsidto thePROVIDERSarray and define itsPROVIDER_DEFAULT_MODELandPROVIDER_ENV_MAPentries insrc/wizard/onboarding.ts.
Testing
The module relies heavily on readline for interactive prompts and fetch for network requests. When testing, these dependencies are typically mocked to ensure deterministic behavior and avoid actual network calls or user interaction. For example, readline.createInterface and fetch would be mocked to return predefined inputs or responses.