-
Notifications
You must be signed in to change notification settings - Fork 5
Initial AI Assistant implementation (fixes #8) #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| """ | ||
| InsecureWebApp - an insecure Python/Flask Web application | ||
|
|
||
| Copyright (C) 2024-2025 Kevin A. Lee (kadraman) | ||
|
|
||
| This program is free software: you can redistribute it and/or modify | ||
| it under the terms of the GNU General Public License as published by | ||
| the Free Software Foundation, either version 3 of the License, or | ||
| (at your option) any later version. | ||
|
|
||
| This program is distributed in the hope that it will be useful, | ||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| GNU General Public License for more details. | ||
|
|
||
| You should have received a copy of the GNU General Public License | ||
| along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| """ | ||
|
|
||
| from flask import Blueprint | ||
|
|
||
| assistant_bp = Blueprint("assistant", __name__, template_folder='templates', static_folder='static') | ||
|
|
||
| from iwa.blueprints.assistant import assistant_routes |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,169 @@ | ||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||
| InsecureWebApp - an insecure Python/Flask Web application | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| Copyright (C) 2024-2025 Kevin A. Lee (kadraman) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| This program is free software: you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| it under the terms of the GNU General Public License as published by | ||||||||||||||||||||||||||||
| the Free Software Foundation, either version 3 of the License, or | ||||||||||||||||||||||||||||
| (at your option) any later version. | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| This program is distributed in the hope that it will be useful, | ||||||||||||||||||||||||||||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||||||||||||||||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||||||||||||||||||||||||
| GNU General Public License for more details. | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| You should have received a copy of the GNU General Public License | ||||||||||||||||||||||||||||
| along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||
| import logging | ||||||||||||||||||||||||||||
| import os | ||||||||||||||||||||||||||||
| import time | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| from flask import render_template, request, jsonify, session | ||||||||||||||||||||||||||||
| from openai import OpenAI | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| from iwa.blueprints.assistant import assistant_bp | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| logger = logging.getLogger(__name__) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Initialize the OpenAI client | ||||||||||||||||||||||||||||
| api_key = os.getenv("OPENAI_API_KEY") | ||||||||||||||||||||||||||||
| if api_key: | ||||||||||||||||||||||||||||
| logger.debug("OPENAI_API_KEY: %s", api_key) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
| logger.debug("OPENAI_API_KEY: %s", api_key) | |
| masked_key = api_key[:4] + "*" * (len(api_key) - 8) + api_key[-4:] if len(api_key) > 8 else "*" * len(api_key) | |
| logger.debug("OPENAI_API_KEY: %s", masked_key) |
Copilot
AI
Sep 16, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using global variables for assistant_id and thread_id creates potential race conditions in multi-threaded environments. These should be stored per-session or user to avoid conflicts between concurrent users.
Copilot
AI
Sep 16, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Global chat_history will be shared across all users and sessions, causing privacy issues and data corruption. Chat history should be stored per-session using Flask's session object or a database.
| # Define a global chat history | |
| chat_history = [ | |
| {"role": "system", "content": "You are a helpful assistant."}, | |
| ] | |
| # Chat history will be stored per-session using Flask's session object. | |
| # To initialize chat history for a session, use: | |
| # if 'chat_history' not in session: | |
| # session['chat_history'] = [{"role": "system", "content": "You are a helpful assistant."}] |
Copilot
AI
Sep 16, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing input validation allows potential crashes if request.json is None or doesn't contain 'message' key. Add proper validation and error handling to prevent server errors from malformed requests.
| content = request.json["message"] | |
| if not request.is_json: | |
| return jsonify(success=False, message="Request must be JSON"), 400 | |
| data = request.get_json() | |
| if not data or "message" not in data: | |
| return jsonify(success=False, message="Missing 'message' in request"), 400 | |
| content = data["message"] |
Copilot
AI
Sep 16, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This infinite loop with polling could hang indefinitely if the OpenAI API fails or returns an unexpected status. Add a timeout mechanism and maximum retry count to prevent blocking the application.
| while run.status != "completed": | |
| time.sleep(0.5) | |
| run = client.beta.threads.runs.retrieve(thread_id=thread_id, run_id=run.id) | |
| max_retries = 40 # 0.5s * 40 = 20 seconds max wait | |
| retries = 0 | |
| while run.status != "completed" and retries < max_retries: | |
| time.sleep(0.5) | |
| run = client.beta.threads.runs.retrieve(thread_id=thread_id, run_id=run.id) | |
| retries += 1 | |
| if run.status != "completed": | |
| logger.error("OpenAI API run did not complete after %d retries.", max_retries) | |
| return jsonify(success=False, message="The AI Assistant did not respond in time. Please try again later.") |
Uh oh!
There was an error while loading. Please reload this page.