diff --git a/SCRIPTS_README.md b/SCRIPTS_README.md new file mode 100644 index 000000000..1b87e36ea --- /dev/null +++ b/SCRIPTS_README.md @@ -0,0 +1,277 @@ +# Codegen SDK Demo Scripts + +This directory contains convenience scripts to quickly set up and use the Codegen SDK. + +## ๐Ÿ“ Scripts Overview + +### ๐Ÿ”ง `setup.sh` +**Purpose:** Initialize the development environment + +**What it does:** +- Checks Python installation (requires Python 3.8+) +- Creates a virtual environment (`.venv`) +- Installs the Codegen SDK +- Creates a `demo.py` file with example code +- Creates `.env.example` for credential reference + +**Usage:** +```bash +./setup.sh +``` + +**When to use:** Run this once when first setting up the project, or when you need to reset your environment. + +--- + +### โ–ถ๏ธ `start.sh` +**Purpose:** Run the demo application + +**What it does:** +- Activates the virtual environment +- Validates that credentials are set +- Runs the `demo.py` script +- Shows task status and results + +**Usage:** +```bash +# First, set your credentials: +export CODEGEN_ORG_ID='your-org-id' +export CODEGEN_TOKEN='your-api-token' + +# Then run: +./start.sh +``` + +**When to use:** After setup is complete and you want to run the demo. + +--- + +### ๐Ÿ“ค `send_request.sh` +**Purpose:** Send custom prompts to the Codegen API + +**What it does:** +- Accepts a custom prompt as an argument +- Sends it to the Codegen API +- Waits for completion (up to 60 seconds) +- Displays the result + +**Usage:** +```bash +# With a prompt as argument: +./send_request.sh "Create a function to validate email addresses" + +# Or run without arguments to be prompted: +./send_request.sh +``` + +**Examples:** +```bash +./send_request.sh "Write a Python function to calculate Fibonacci numbers" +./send_request.sh "Create a REST API endpoint for user authentication" +./send_request.sh "Generate unit tests for a sorting algorithm" +``` + +**When to use:** When you want to quickly test the API with custom prompts. + +--- + +### ๐Ÿš€ `all.sh` +**Purpose:** Complete setup and run workflow (all-in-one) + +**What it does:** +1. Runs `setup.sh` to initialize the environment +2. Checks that credentials are configured +3. Runs `start.sh` to execute the demo +4. Shows a summary and next steps + +**Usage:** +```bash +# Set credentials first: +export CODEGEN_ORG_ID='your-org-id' +export CODEGEN_TOKEN='your-api-token' + +# Run everything: +./all.sh +``` + +**When to use:** Perfect for first-time setup or when you want to run everything in one command. + +--- + +## ๐Ÿ”‘ Getting Credentials + +1. Visit [codegen.com/token](https://codegen.com/token) +2. Sign in or create an account +3. Copy your Organization ID and API Token +4. Set them as environment variables: + +```bash +export CODEGEN_ORG_ID='your-org-id-here' +export CODEGEN_TOKEN='your-api-token-here' +``` + +**Tip:** Add these to your `~/.bashrc` or `~/.zshrc` to persist them across sessions: + +```bash +echo 'export CODEGEN_ORG_ID="your-org-id-here"' >> ~/.bashrc +echo 'export CODEGEN_TOKEN="your-api-token-here"' >> ~/.bashrc +source ~/.bashrc +``` + +--- + +## ๐ŸŽฏ Quick Start Guide + +### Option 1: All-in-One (Recommended for first time) + +```bash +# 1. Get credentials from https://codegen.com/token +# 2. Set environment variables +export CODEGEN_ORG_ID='your-org-id' +export CODEGEN_TOKEN='your-api-token' + +# 3. Run everything +./all.sh +``` + +### Option 2: Step-by-Step + +```bash +# Step 1: Setup environment +./setup.sh + +# Step 2: Set credentials +export CODEGEN_ORG_ID='your-org-id' +export CODEGEN_TOKEN='your-api-token' + +# Step 3: Run demo +./start.sh + +# Step 4 (Optional): Try custom prompts +./send_request.sh "Your custom prompt here" +``` + +--- + +## ๐Ÿ“‹ Workflow Diagram + +``` +โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” +โ”‚ setup.sh โ”‚ โ† Initialize environment +โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜ + โ”‚ + โ”œโ”€โ”€ Creates virtual environment + โ”œโ”€โ”€ Installs dependencies + โ”œโ”€โ”€ Creates demo.py + โ””โ”€โ”€ Creates .env.example + +โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” +โ”‚ start.sh โ”‚ โ† Run the demo +โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜ + โ”‚ + โ”œโ”€โ”€ Activates venv + โ”œโ”€โ”€ Validates credentials + โ””โ”€โ”€ Runs demo.py + +โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” +โ”‚ send_request.sh โ”‚ โ† Send custom requests +โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ + โ”‚ + โ”œโ”€โ”€ Accepts custom prompt + โ”œโ”€โ”€ Sends to API + โ”œโ”€โ”€ Waits for response + โ””โ”€โ”€ Displays result + +โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” +โ”‚ all.sh โ”‚ โ† Do everything at once +โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜ + โ”‚ + โ”œโ”€โ”€ Runs setup.sh + โ”œโ”€โ”€ Checks credentials + โ”œโ”€โ”€ Runs start.sh + โ””โ”€โ”€ Shows summary +``` + +--- + +## ๐Ÿ› Troubleshooting + +### Error: "Python 3 is not installed" +**Solution:** Install Python 3.8 or higher: +```bash +# On Ubuntu/Debian +sudo apt update && sudo apt install python3 python3-venv python3-pip + +# On macOS +brew install python3 + +# On Windows (WSL) +sudo apt update && sudo apt install python3 python3-venv python3-pip +``` + +### Error: "CODEGEN_ORG_ID and CODEGEN_TOKEN are not set" +**Solution:** Set your credentials: +```bash +export CODEGEN_ORG_ID='your-org-id' +export CODEGEN_TOKEN='your-api-token' +``` + +Get them from: https://codegen.com/token + +### Error: "Virtual environment not found" +**Solution:** Run setup first: +```bash +./setup.sh +``` + +### Script won't run: "Permission denied" +**Solution:** Make scripts executable: +```bash +chmod +x setup.sh start.sh send_request.sh all.sh +``` + +--- + +## ๐Ÿงช Testing the Scripts + +To verify all scripts are working: + +```bash +# Test syntax +bash -n setup.sh +bash -n start.sh +bash -n send_request.sh +bash -n all.sh + +# Run a dry-run of setup +./setup.sh +``` + +--- + +## ๐Ÿ“š Additional Resources + +- **Main Documentation:** [docs.codegen.com](https://docs.codegen.com) +- **Get API Token:** [codegen.com/token](https://codegen.com/token) +- **Community:** [community.codegen.com](https://community.codegen.com) +- **GitHub:** [github.com/codegen-sh/codegen](https://github.com/codegen-sh/codegen) + +--- + +## ๐Ÿ’ก Tips + +1. **Environment Variables:** Consider using a `.env` file with a tool like `python-dotenv` for managing credentials +2. **Script Organization:** All scripts follow the same pattern: check dependencies โ†’ validate โ†’ execute +3. **Error Handling:** Scripts use `set -e` to exit on errors, ensuring failures are caught early +4. **Virtual Environment:** Always activated before running Python code to ensure clean dependencies + +--- + +## ๐Ÿค Contributing + +Found an issue or want to improve these scripts? Please submit a PR or open an issue on GitHub! + +--- + +**Last Updated:** October 2025 + diff --git a/all.sh b/all.sh new file mode 100755 index 000000000..4d2cd7387 --- /dev/null +++ b/all.sh @@ -0,0 +1,79 @@ +#!/bin/bash +# all.sh - Complete setup and run workflow for Codegen SDK demo + +set -e # Exit on error + +echo "โ•”โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•—" +echo "โ•‘ Codegen SDK - Complete Setup & Run โ•‘" +echo "โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•" +echo "" + +# Function to print section headers +print_section() { + echo "" + echo "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”" + echo " $1" + echo "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”" + echo "" +} + +# Step 1: Setup +print_section "Step 1: Setup Environment" +if [ -f "./setup.sh" ]; then + chmod +x ./setup.sh + ./setup.sh +else + echo "โŒ setup.sh not found!" + exit 1 +fi + +# Step 2: Check credentials +print_section "Step 2: Verify Credentials" +if [ -z "$CODEGEN_ORG_ID" ] || [ -z "$CODEGEN_TOKEN" ]; then + echo "โš ๏ธ Credentials not found in environment" + echo "" + echo "Please set your credentials before running the demo:" + echo "" + echo " export CODEGEN_ORG_ID='your-org-id'" + echo " export CODEGEN_TOKEN='your-api-token'" + echo "" + echo "Get them from: https://codegen.com/token" + echo "" + echo "After setting credentials, run this script again:" + echo " ./all.sh" + echo "" + exit 1 +else + echo "โœ… Credentials found:" + echo " ORG_ID: ${CODEGEN_ORG_ID:0:10}..." + echo " TOKEN: ${CODEGEN_TOKEN:0:10}..." +fi + +# Step 3: Run demo +print_section "Step 3: Run Demo" +if [ -f "./start.sh" ]; then + chmod +x ./start.sh + ./start.sh +else + echo "โŒ start.sh not found!" + exit 1 +fi + +# Step 4: Summary +print_section "โœ… All Steps Completed" +echo "๐Ÿ“‹ What just happened:" +echo " โœ“ Environment set up with virtual environment" +echo " โœ“ Codegen SDK installed" +echo " โœ“ Demo executed successfully" +echo "" +echo "๐Ÿ“š Next steps:" +echo " โ€ข Try custom prompts with: ./send_request.sh \"your prompt\"" +echo " โ€ข View the code in: demo.py" +echo " โ€ข Read the docs: https://docs.codegen.com" +echo "" +echo "๐Ÿ’ก Examples:" +echo " ./send_request.sh \"Create a REST API endpoint for user login\"" +echo " ./send_request.sh \"Write unit tests for a sorting algorithm\"" +echo " ./send_request.sh \"Optimize this SQL query for performance\"" +echo "" + diff --git a/send_request.sh b/send_request.sh new file mode 100755 index 000000000..5addb2fad --- /dev/null +++ b/send_request.sh @@ -0,0 +1,105 @@ +#!/bin/bash +# send_request.sh - Send a custom request to the Codegen API + +set -e # Exit on error + +echo "๐Ÿ“ค Codegen API Request Tool" +echo "" + +# Check if virtual environment exists +if [ ! -d ".venv" ]; then + echo "โŒ Virtual environment not found. Please run ./setup.sh first" + exit 1 +fi + +# Activate virtual environment +source .venv/bin/activate + +# Check if credentials are set +if [ -z "$CODEGEN_ORG_ID" ] || [ -z "$CODEGEN_TOKEN" ]; then + echo "โŒ Error: CODEGEN_ORG_ID and CODEGEN_TOKEN are not set" + echo "" + echo "Please set your credentials:" + echo " export CODEGEN_ORG_ID='your-org-id'" + echo " export CODEGEN_TOKEN='your-api-token'" + echo "" + echo "Get them from: https://codegen.com/token" + exit 1 +fi + +# Get prompt from command line argument or ask for it +if [ -z "$1" ]; then + echo "Usage: $0 \"Your prompt here\"" + echo "" + echo "Example:" + echo " $0 \"Create a function to validate email addresses\"" + echo "" + read -p "Enter your prompt: " PROMPT +else + PROMPT="$*" +fi + +if [ -z "$PROMPT" ]; then + echo "โŒ Error: No prompt provided" + exit 1 +fi + +# Create a temporary Python script to send the request +cat > /tmp/codegen_request.py << EOF +import os +from codegen.agents.agent import Agent + +org_id = os.getenv("CODEGEN_ORG_ID") +token = os.getenv("CODEGEN_TOKEN") +prompt = """$PROMPT""" + +print("๐Ÿค– Initializing Codegen Agent...") +agent = Agent(org_id=org_id, token=token) + +print(f"\n๐Ÿ“ค Sending prompt: {prompt}") +print("โณ Running agent...\n") + +task = agent.run(prompt=prompt) + +print(f"๐Ÿ“Š Status: {task.status}") +print(f"๐Ÿ†” Task ID: {task.id}") +print() + +# Wait and refresh +import time +max_wait = 60 # Maximum wait time in seconds +wait_time = 0 +interval = 5 + +while task.status not in ["completed", "failed", "cancelled"] and wait_time < max_wait: + print(f"โณ Waiting... ({wait_time}s / {max_wait}s)") + time.sleep(interval) + wait_time += interval + task.refresh() + print(f"๐Ÿ“Š Status: {task.status}") + +if task.status == "completed": + print("\nโœ… Task completed successfully!") + print("\n" + "="*60) + print("RESULT:") + print("="*60) + print(task.result) +elif task.status == "failed": + print("\nโŒ Task failed") + if hasattr(task, 'error'): + print(f"Error: {task.error}") +else: + print(f"\nโณ Task is still running after {max_wait}s") + print(f" Task ID: {task.id}") + print(" Check the Codegen dashboard for updates") +EOF + +# Run the request +python /tmp/codegen_request.py + +# Clean up +rm /tmp/codegen_request.py + +echo "" +echo "โœ… Request completed!" + diff --git a/setup.sh b/setup.sh new file mode 100755 index 000000000..163be9521 --- /dev/null +++ b/setup.sh @@ -0,0 +1,130 @@ +#!/bin/bash +# setup.sh - Initialize the Codegen SDK demo environment + +set -e # Exit on error + +echo "๐Ÿš€ Setting up Codegen SDK Demo Environment..." + +# Check if Python is installed +if ! command -v python3 &> /dev/null; then + echo "โŒ Error: Python 3 is not installed. Please install Python 3.8 or higher." + exit 1 +fi + +# Check Python version +python_version=$(python3 -c 'import sys; print(".".join(map(str, sys.version_info[:2])))') +echo "โœ“ Found Python $python_version" + +# Create virtual environment if it doesn't exist +if [ ! -d ".venv" ]; then + echo "๐Ÿ“ฆ Creating virtual environment..." + python3 -m venv .venv +else + echo "โœ“ Virtual environment already exists" +fi + +# Activate virtual environment +echo "๐Ÿ”ง Activating virtual environment..." +source .venv/bin/activate + +# Upgrade pip +echo "โฌ†๏ธ Upgrading pip..." +pip install --upgrade pip --quiet + +# Install the codegen package +echo "๐Ÿ“ฅ Installing Codegen SDK..." +pip install -e . --quiet + +# Create demo.py if it doesn't exist +if [ ! -f "demo.py" ]; then + echo "๐Ÿ“ Creating demo.py file..." + cat > demo.py << 'EOF' +""" +Codegen SDK Demo +----------------- +This demo shows how to use the Codegen SDK to interact with AI coding agents. +""" + +import os +from codegen.agents.agent import Agent + +def main(): + # Get credentials from environment variables + org_id = os.getenv("CODEGEN_ORG_ID") + token = os.getenv("CODEGEN_TOKEN") + + if not org_id or not token: + print("โŒ Error: Please set CODEGEN_ORG_ID and CODEGEN_TOKEN environment variables") + print("\nGet your credentials from: https://codegen.com/token") + print("\nSet them using:") + print(" export CODEGEN_ORG_ID='your-org-id'") + print(" export CODEGEN_TOKEN='your-api-token'") + return + + print("๐Ÿค– Initializing Codegen Agent...") + agent = Agent(org_id=org_id, token=token) + + # Example prompt + prompt = "Create a simple Python function to calculate the factorial of a number" + + print(f"\n๐Ÿ“ค Sending prompt: {prompt}") + print("โณ Running agent (this may take a moment)...\n") + + # Run the agent + task = agent.run(prompt=prompt) + + print(f"๐Ÿ“Š Initial Status: {task.status}") + print(f"๐Ÿ†” Task ID: {task.id}") + + # Refresh to get updated status + print("\n๐Ÿ”„ Refreshing task status...") + task.refresh() + + print(f"๐Ÿ“Š Updated Status: {task.status}") + + # Display result if completed + if task.status == "completed": + print("\nโœ… Task completed successfully!") + print("\n" + "="*60) + print("RESULT:") + print("="*60) + print(task.result) + elif task.status == "failed": + print("\nโŒ Task failed") + else: + print(f"\nโณ Task is still running. Current status: {task.status}") + print(" Run this script again or check the Codegen dashboard") + +if __name__ == "__main__": + main() +EOF + chmod +x demo.py +fi + +# Create .env.example file +if [ ! -f ".env.example" ]; then + echo "๐Ÿ“ Creating .env.example file..." + cat > .env.example << 'EOF' +# Codegen API Credentials +# Get yours from: https://codegen.com/token + +CODEGEN_ORG_ID=your-org-id-here +CODEGEN_TOKEN=your-api-token-here +EOF +fi + +echo "" +echo "โœ… Setup complete!" +echo "" +echo "๐Ÿ“‹ Next steps:" +echo " 1. Get your credentials from: https://codegen.com/token" +echo " 2. Export your credentials:" +echo " export CODEGEN_ORG_ID='your-org-id'" +echo " export CODEGEN_TOKEN='your-api-token'" +echo " 3. Run the demo:" +echo " ./start.sh" +echo "" +echo " Or use the all-in-one script:" +echo " ./all.sh" +echo "" + diff --git a/start.sh b/start.sh new file mode 100755 index 000000000..45af2dae9 --- /dev/null +++ b/start.sh @@ -0,0 +1,44 @@ +#!/bin/bash +# start.sh - Run the Codegen SDK demo + +set -e # Exit on error + +echo "๐Ÿš€ Starting Codegen SDK Demo..." +echo "" + +# Check if virtual environment exists +if [ ! -d ".venv" ]; then + echo "โŒ Virtual environment not found. Please run ./setup.sh first" + exit 1 +fi + +# Activate virtual environment +source .venv/bin/activate + +# Check if credentials are set +if [ -z "$CODEGEN_ORG_ID" ] || [ -z "$CODEGEN_TOKEN" ]; then + echo "โš ๏ธ Warning: CODEGEN_ORG_ID and CODEGEN_TOKEN are not set" + echo "" + echo "Please set your credentials:" + echo " export CODEGEN_ORG_ID='your-org-id'" + echo " export CODEGEN_TOKEN='your-api-token'" + echo "" + echo "Get them from: https://codegen.com/token" + echo "" + exit 1 +fi + +# Check if demo.py exists +if [ ! -f "demo.py" ]; then + echo "โŒ demo.py not found. Please run ./setup.sh first" + exit 1 +fi + +# Run the demo +echo "โ–ถ๏ธ Running demo..." +echo "" +python demo.py + +echo "" +echo "โœ… Demo completed!" +