diff --git a/.devcontainer.json b/.devcontainer.json new file mode 100644 index 00000000..d8399bec --- /dev/null +++ b/.devcontainer.json @@ -0,0 +1,7 @@ +{ + "tasks": { + "test": "pip install -r requirements.txt && pytest", + "build": "pip install -r requirements.txt", + "launch": "python main.py" + } +} \ No newline at end of file diff --git a/.env.example b/.env.example index 71a94776..efc646f6 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,4 @@ ANTHROPIC_API_KEY="YOUR API KEY" TAVILY_API_KEY="YOUR API KEY" ELEVEN_LABS_API_KEY="YOUR API KEY" +BASE_URL="YOUR BASE URL" diff --git a/`pytest.ini` (ADD) b/`pytest.ini` (ADD) new file mode 100644 index 00000000..4ecb1ad2 --- /dev/null +++ b/`pytest.ini` (ADD) @@ -0,0 +1,5 @@ +[pytest] +testpaths = tests +python_files = test_*.py +python_classes = Test* +python_functions = test_* diff --git a/main.py b/main.py index 15c27076..38056669 100644 --- a/main.py +++ b/main.py @@ -273,6 +273,11 @@ def setup_virtual_environment() -> Tuple[str, str]: raise ValueError("TAVILY_API_KEY not found in environment variables") tavily = TavilyClient(api_key=tavily_api_key) +# Initialize the base URL +base_url = os.getenv("BASE_URL") +if not base_url: + raise ValueError("BASE_URL not found in environment variables") + console = Console() # Token tracking variables @@ -1268,7 +1273,7 @@ def save_chat(): elif message['role'] == 'assistant': if isinstance(message['content'], str): formatted_chat += f"## Claude\n\n{message['content']}\n\n" - elif isinstance(message['content'], list): + elif isinstance(message['content'], list]): for content in message['content']: if content['type'] == 'tool_use': formatted_chat += f"### Tool Use: {content['name']}\n\n```json\n{json.dumps(content['input'], indent=2)}\n```\n\n" diff --git a/settings.py b/settings.py new file mode 100644 index 00000000..a1865245 --- /dev/null +++ b/settings.py @@ -0,0 +1,44 @@ +from flask import Flask, render_template, request, redirect, url_for +import os +from dotenv import load_dotenv + +app = Flask(__name__) + +# Load environment variables from .env file +load_dotenv() + +@app.route('/settings', methods=['GET', 'POST']) +def settings(): + if request.method == 'POST': + # Update environment variables + anthropic_api_key = request.form.get('ANTHROPIC_API_KEY') + tavily_api_key = request.form.get('TAVILY_API_KEY') + eleven_labs_api_key = request.form.get('ELEVEN_LABS_API_KEY') + base_url = request.form.get('BASE_URL') + + # Save the updated environment variables to the .env file + with open('.env', 'w') as f: + f.write(f"ANTHROPIC_API_KEY={anthropic_api_key}\n") + f.write(f"TAVILY_API_KEY={tavily_api_key}\n") + f.write(f"ELEVEN_LABS_API_KEY={eleven_labs_api_key}\n") + f.write(f"BASE_URL={base_url}\n") + + # Reload the environment variables + load_dotenv() + + return redirect(url_for('settings')) + + # Load current environment variables + anthropic_api_key = os.getenv('ANTHROPIC_API_KEY') + tavily_api_key = os.getenv('TAVILY_API_KEY') + eleven_labs_api_key = os.getenv('ELEVEN_LABS_API_KEY') + base_url = os.getenv('BASE_URL') + + return render_template('settings.html', + anthropic_api_key=anthropic_api_key, + tavily_api_key=tavily_api_key, + eleven_labs_api_key=eleven_labs_api_key, + base_url=base_url) + +if __name__ == '__main__': + app.run(debug=True) diff --git a/tests b/tests new file mode 100644 index 00000000..e64d4602 --- /dev/null +++ b/tests @@ -0,0 +1,72 @@ +import os +import pytest +from dotenv import load_dotenv +from flask import Flask, render_template, request, redirect, url_for + +# Load environment variables from .env file +load_dotenv() + +# Initialize the Flask app +app = Flask(__name__) + +# Define the settings route +@app.route('/settings', methods=['GET', 'POST']) +def settings(): + if request.method == 'POST': + # Update environment variables + anthropic_api_key = request.form.get('ANTHROPIC_API_KEY') + tavily_api_key = request.form.get('TAVILY_API_KEY') + eleven_labs_api_key = request.form.get('ELEVEN_LABS_API_KEY') + base_url = request.form.get('BASE_URL') + + # Save the updated environment variables to the .env file + with open('.env', 'w') as f: + f.write(f"ANTHROPIC_API_KEY={anthropic_api_key}\n") + f.write(f"TAVILY_API_KEY={tavily_api_key}\n") + f.write(f"ELEVEN_LABS_API_KEY={eleven_labs_api_key}\n") + f.write(f"BASE_URL={base_url}\n") + + # Reload the environment variables + load_dotenv() + + return redirect(url_for('settings')) + + # Load current environment variables + anthropic_api_key = os.getenv('ANTHROPIC_API_KEY') + tavily_api_key = os.getenv('TAVILY_API_KEY') + eleven_labs_api_key = os.getenv('ELEVEN_LABS_API_KEY') + base_url = os.getenv('BASE_URL') + + return render_template('settings.html', + anthropic_api_key=anthropic_api_key, + tavily_api_key=tavily_api_key, + eleven_labs_api_key=eleven_labs_api_key, + base_url=base_url) + +# Test functions +def test_settings_page(client): + response = client.get('/settings') + assert response.status_code == 200 + assert b'ANTHROPIC_API_KEY' in response.data + assert b'TAVILY_API_KEY' in response.data + assert b'ELEVEN_LABS_API_KEY' in response.data + assert b'BASE_URL' in response.data + +def test_update_settings(client): + response = client.post('/settings', data={ + 'ANTHROPIC_API_KEY': 'new_anthropic_api_key', + 'TAVILY_API_KEY': 'new_tavily_api_key', + 'ELEVEN_LABS_API_KEY': 'new_eleven_labs_api_key', + 'BASE_URL': 'new_base_url' + }, follow_redirects=True) + assert response.status_code == 200 + assert b'new_anthropic_api_key' in response.data + assert b'new_tavily_api_key' in response.data + assert b'new_eleven_labs_api_key' in response.data + assert b'new_base_url' in response.data + +@pytest.fixture +def client(): + app.config['TESTING'] = True + with app.test_client() as client: + yield client