A python Agent used to Direct Python user into new and better code practices -Using open ai and some intresting parsing i thought of while high XD-
import requests

def get_github_api_version():
    response = requests.get("https://api.github.com")
    
    if response.status_code == 200 and 'X-GitHub-Media-Type' in response.headers:
        media_type = response.headers['X-GitHub-Media-Type']
        version = media_type.split(";")[0].split(".")[-1]
        return version
    else:
        return None

current_version = get_github_api_version()
if current_version:
    print(f"Current GitHub API version: {current_version}")
else:
    print("Failed to retrieve GitHub API version.")

Example logs/gitapu.py
Nbtguyoriginal/Pybonce
import openai
import requests
import base64
import re
import tkinter as tk
from tkinter import simpledialog, messagebox, Text, Scrollbar

# It's recommended to fetch API keys from a secure location or environment variable.
# For the sake of this example, they are hardcoded.
OPENAI_API_KEY = 'YOUR_OPENAI_API_KEY_HERE'
GITHUB_TOKEN = 'YOUR_GITHUB_TOKEN_HERE'

openai.api_key = OPENAI_API_KEY

class GitHubRepoPlugin:
    # ... [rest of the GitHubRepoPlugin class as provided]

def chat_with_gpt(message, conversation_history="", temperature=0.7, max_tokens=150):
    plugin = GitHubRepoPlugin(GITHUB_TOKEN)
    
    # Check if the message is a URL
    if "github.com" in message:
        plugin_response = plugin.query(message)
        conversation_history += f"User: {message}\nAgent: {plugin_response}\n"
    
    # Pass the message (and any GitHub info) to OpenAI API
    conversation_payload = {
        "messages": [{"role": "user", "content": message}],
        "max_tokens": max_tokens,
        "temperature": temperature
    }
    headers = {
        "Authorization": f"Bearer {OPENAI_API_KEY}",
        "Content-Type": "application/json"
    }
    try:
        response = requests.post("https://api.openai.com/v2/completions", headers=headers, json=conversation_payload)
        response_data = response.json()
        
        # Check for errors in the response
        if 'error' in response_data:
            return response_data['error']['message'], conversation_history
        
        assistant_response = response_data['choices'][0]['message']['content']
        conversation_history += f"Assistant: {assistant_response}\n"
        return assistant_response, conversation_history
    except Exception as e:
        return str(e), conversation_history

class ChatGPTGUI:
    def __init__(self, root):
        self.root = root
        self.root.title("gitmaster GitHub Plugin")

        self.conversation_history = ""
        self.temperature = 0.7
        self.max_tokens = 300

        self.text_widget = Text(root, wrap=tk.WORD, state=tk.DISABLED)
        self.text_widget.pack(expand=1, fill=tk.BOTH, padx=10, pady=10)

        self.scrollbar = Scrollbar(root, command=self.text_widget.yview)
        self.scrollbar.pack(side=tk.RIGHT, fill=tk.Y)

        self.text_widget.config(yscrollcommand=self.scrollbar.set)

        self.entry = tk.Entry(root, width=50)
        self.entry.pack(pady=10)
        self.entry.bind('<Return>', self.send_message)

        self.button = tk.Button(root, text="Send", command=self.send_message)
        self.button.pack(pady=10)

        self.menu = tk.Menu(root)
        root.config(menu=self.menu)

        self.settings_menu = tk.Menu(self.menu)
        self.menu.add_cascade(label="Settings", menu=self.settings_menu)
        self.settings_menu.add_command(label="Modify Agent", command=self.modify_agent)

    def send_message(self, event=None):
        user_message = self.entry.get()
        if user_message:
            response, self.conversation_history = chat_with_gpt(user_message, self.conversation_history, self.temperature, self.max_tokens)
            self.display_message(f"User: {user_message}\nAgent: {response}\n")
            self.entry.delete(0, tk.END)

    def display_message(self, message):
        self.text_widget.config(state=tk.NORMAL)
        self.text_widget.insert(tk.END, message)
        self.text_widget.see(tk.END)
        self.text_widget.config(state=tk.DISABLED)

    def modify_agent(self):
        def apply_changes():
            self.temperature = temperature_slider.get()
            self.max_tokens = int(max_tokens_entry.get())
            modify_window.destroy()
            messagebox.showinfo("Info", f"Agent modified with temperature: {self.temperature} and max tokens: {self.max_tokens}")

        modify_window = tk.Toplevel(self.root)
        modify_window.title("Modify Agent Parameters")

        # Temperature
        temperature_label = tk.Label(modify_window, text="Temperature:")
        temperature_label.pack(pady=10)

        temperature_slider = tk.Scale(modify_window, from_=0.0, to=10.0, resolution=0.01, orient=tk.HORIZONTAL)
        temperature_slider.set(self.temperature)
        temperature_slider.pack(pady=10)

        # Max Tokens
        max_tokens_label = tk.Label(modify_window, text="Max Tokens:")
        max_tokens_label.pack(pady=10)

        max_tokens_entry = tk.Entry(modify_window)
        max_tokens_entry.insert(0, str(self.max_tokens))
        max_tokens_entry.pack(pady=10)

        apply_button = tk.Button(modify_window, text="Apply", command=apply_changes)
        apply_button.pack(pady=10)

root = tk.Tk()
gui = ChatGPTGUI(root)
root.mainloop()
Example logs/gitmaster.py
Nbtguyoriginal/Pybonce
import os
import openai
import config

openai.api_key = config.API_KEY


class Brain_matter:
    def __init__(self):
        self.memory_path = "memory_files"
        self.reference_path = "memory_reference"
        self.subconscious_path = "subconscious"

    def save_memory(self, content, memory_type):
        # Create subconscious directory if it doesn't exist
        if not os.path.exists(self.subconscious_path):
            os.makedirs(self.subconscious_path)

    # Save the full content to subconscious
        with open(os.path.join(self.subconscious_path, f"{memory_type}.txt"), "a") as f:
            f.write(content + "\n")

    # Create memory directory if it doesn't exist
        if not os.path.exists(self.memory_path):
            os.makedirs(self.memory_path)

    # Summarize the content
        summarized_content = self.summarize_memory(content)

    # Save the summarized content to memory
        with open(os.path.join(self.memory_path, f"{memory_type}.txt"), "a") as f:
            f.write(summarized_content + "\n")


    def summarize_memory(self, content):
        response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo",
            messages=[
                {
                    "role": "user",
                    "content": f"Summarize the following content: {content}"
                }
            ],
            temperature=0.02,
            max_tokens=417,
            top_p=1,
            frequency_penalty=0.51,
            presence_penalty=0.12
        )
        return response.choices[0].message['content']

    def retrieve_memory(self, memory_type):
        with open(os.path.join(self.memory_path, f"{memory_type}.txt"), "r") as f:
            memories = f.readlines()
        return memories

    def evolve_memory(self, memory_type, content):
        # Store the evolved memory in the reference folder
        if not os.path.exists(self.reference_path):
            os.makedirs(self.reference_path)

        with open(os.path.join(self.reference_path, f"{memory_type}.txt"), "a") as f:
            f.write(content + "\n")

    def learn_passively(self, content):
        response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo",
            messages=[
                {
                    "role": "user",
                    "content": f"Tell me more about {content}"
                }
            ],
            temperature=0.02,
            max_tokens=417,
            top_p=1,
            frequency_penalty=0.51,
            presence_penalty=0.12
        )
        return response.choices[0].message['content']

# Example usage:
# memory_system = MemorySystem()
# memory_system.store_memory("The Eiffel Tower is located in Paris.", "landmarks")
# print(memory_system.retrieve_memory("landmarks"))
# print(memory_system.learn_passively("Eiffel Tower"))


PyCells.py
Nbtguyoriginal/Pybonce
# --------------------------
# Configuration for pyreader.py
# --------------------------
# Program by: Tcoder
# Free to use upgrade change or distribute with credit given to 
# Credit: https://github.com/Nbtguyoriginal/Pybonce

#---------------------------------------------------------------------------------------------------------------------------------------------
# OpenAI Configuration   
# OpenAI API Key
API_KEY = 'key here'

LINES_PER_API_CALL = 10  # Number of lines to send to OpenAI API in a single call

# UI Configuration
HIGHLIGHT_COLOR = '#FF33A1'  # Color used to highlight specific lines in the analysis log
SENDING_COLOR = '#FFA1A1'  # Light red
ANALYSIS_COLOR = '#83C1A1'  # Aquamarine
TITLE_COLOR = '#FFFFE0'     # lightyellow

# Logging Configuration
LOG_FILE = 'audit.txt'  
# File where the analysis log is saved

#use the first instance of the log name 
#ex "better_code": "Better_code", you use "beter_code" here--- CURRENT_LOG_TYPE = LOG_TYPES["beter_code"]
# this will return a log with  Suggestions for code improvements 
#be sure to change your log file name to refelct the type of log your makng for easy orginization 

# Define different types of logs
LOG_TYPES = {
    "default": "Default",  # General-purpose logs
    "modifications": "Possible_mods",  # Logs related to changes or modifications
    "General_notes": "General_notes",  # General remarks or observations
    "Detailed_notes": "Detailed_notes",  # In-depth remarks or observations
    "better_code": "Better_code",  # Suggestions for code improvements
    "error": "Error",  # Logs for error events
    "warning": "Warning",  # Logs for warning events
    "info": "Information",  # General informational logs
    "debug": "Debug",  # Logs used for debugging purposes
    "critical": "Critical",  # Logs for critical events
    "authentication": "Authentication",  # Logs related to user authentication
    "authorization": "Authorization",  # Logs related to user permissions
    "database": "Database",  # Logs related to database operations
    "network": "Network",  # Logs related to network operations
    "performance": "Performance",  # Logs related to performance metrics
    "audit": "Audit",  # Logs for auditing purposes
    "backup": "Backup",  # Logs related to data backups
    "maintenance": "Maintenance",  # Logs related to system maintenance
    "startup": "Startup",  # Logs generated at system startup
    "shutdown": "Shutdown",  # Logs generated at system shutdown
    "configuration": "Configuration",  # Logs related to system configurations
    "deprecation": "Deprecation",  # Logs related to deprecated features
    "migration": "Migration",  # Logs related to data migration
    "user_activity": "User_Activity",  # Logs related to user actions
    "system_activity": "System_Activity",  # Logs related to system actions
    "security": "Security",  # Logs related to security events
    "access": "Access",  # Logs related to resource access
    "transaction": "Transaction",  # Logs related to transactions
    "synchronization": "Synchronization",  # Logs related to data synchronization
    "communication": "Communication",  # Logs related to system communications
    "service": "Service",  # Logs related to services or daemons
    "resource": "Resource",  # Logs related to resource usage
    "cache": "Cache",  # Logs related to caching operations
    "schedule": "Schedule",  # Logs related to scheduled tasks
    "update": "Update",  # Logs related to updates or patches
    "deletion": "Deletion",  # Logs related to data deletion
    "creation": "Creation",  # Logs related to data creation
    "validation": "Validation"  # Logs related to data validation
}


# Set the current log type
CURRENT_LOG_TYPE = LOG_TYPES["audit"]

WARP_ITERATIONS = 3  # Number of times the warp process should iterate
WARP_RULES = "Your specific warp rules or criteria here"
#warp rules iterative generation 

# Specify the method to use: 'combine', 'sequential', or 'selective' -- explained below 
WARP_METHOD = 'sequential'  # Change this to your desired method


WARP_RULES_SET_1 = """
1. Identify any loops that can be optimized using list comprehensions or Python's built-in functions.
2. Highlight any hard-coded values and suggest moving them to constants or configuration files.
3. Check for the use of deprecated libraries or functions and recommend modern alternatives.
4. Point out any lack of error handling, especially around external API calls or file operations.
5. Suggest improvements for code readability, such as breaking long functions into smaller, more manageable pieces.
"""

WARP_RULES_SET_2 = """
1. Look for potential security vulnerabilities, especially in user input handling or database operations.
2. Recommend the use of logging for critical operations to aid in debugging and monitoring.
3. Identify areas where parallel processing or threading might speed up execution.
4. Check for consistent naming conventions and adherence to PEP 8 standards.
5. Highlight any potential memory leaks or resource-intensive operations.
"""

WARP_RULES_SET_3 = """
1. Assess the modularity of the code and suggest ways to improve code reusability.
2. Point out any missed opportunities for using object-oriented principles.
3. Recommend ways to improve the user experience, such as better prompts or error messages.
4. Check for potential race conditions or concurrency issues.
5. Suggest improvements in data structures used for better efficiency.
"""

WARP_RULES_SET_4 = """
1. Identify any potential bottlenecks in the code that could slow down execution.
2. Highlight areas where unit tests or integration tests might be beneficial.
3. Check for the use of magic numbers and suggest replacing them with named constants.
4. Assess the clarity of comments and suggest improvements where necessary.
5. Recommend ways to make the code more scalable and maintainable.
"""

WARP_RULES_SET_5 = """
1. Point out any areas where exception handling is too broad or too specific.
2. Suggest the use of design patterns where they might be beneficial.
3. Check for any potential issues with data integrity or persistence.
4. Recommend ways to improve user input validation and sanitation.
5. Highlight any areas where the code might not be cross-platform compatible.
"""

WARP_RULES_SET_6 = """
1. Assess the efficiency of database queries and suggest optimizations.
2. Point out any potential issues with file handling, especially regarding file permissions or file locks.
3. Recommend the use of environment variables for sensitive information instead of hard-coding.
4. Check for any potential issues with third-party library versions or dependencies.
5. Suggest ways to make the code more readable for new developers.
"""

WARP_RULES_SET_7 = """
1. Highlight areas where the code might benefit from lazy loading or caching.
2. Check for the correct use of global variables and suggest alternatives.
3. Assess the potential for code injection attacks and recommend preventative measures.
4. Recommend ways to improve the structure of the codebase, such as directory structure or module organization.
5. Point out any areas where the code might be over-engineered or too complex.
"""

WARP_RULES_SET_8 = """
1. Suggest improvements for better integration with other systems or APIs.
2. Check for the correct handling of date and time operations, considering time zones.
3. Highlight any potential issues with multithreading or multiprocessing.
4. Assess the clarity and usefulness of debug messages or logs.
5. Recommend ways to improve the efficiency of data processing operations.
"""

WARP_RULES_SET_9 = """
1. Point out any potential issues with Unicode or character encoding.
2. Suggest improvements for better error recovery or graceful degradation.
3. Check for the potential misuse of recursion or potential for stack overflow.
4. Highlight any areas where the code might be making unnecessary computations.
5. Recommend ways to improve the user interface or user experience.
"""

WARP_RULES_SET_10 = """
1. Assess the potential for floating-point errors or precision issues.
2. Point out any areas where the code might be vulnerable to denial-of-service attacks.
3. Check for the correct handling of network operations, considering timeouts and retries.
4. Suggest improvements for better documentation or code comments.
5. Highlight any potential issues with backward compatibility or versioning.
"""
# If using the 'selective' method, specify which sets to use
SELECTED_WARP_SETS = [WARP_RULES_SET_1, WARP_RULES_SET_2]
#----------------------------------------------------------------------------------------------------------------------------------------------

# ==================== USER INFORMATION ====================
# Do not modify anything below this point. This section is for user information only.

# ------------------- API Call Configuration -------------------
# It's recommended to set "/lines_per_api_call/" to a minimum of "10" for optimal results.
# If your code isn't well described in the log or GUI, consider adjusting this value.
# However, setting it below "10" is not advised for compact code or small scripts.
#setting bellow 10 can break the script or cost hundreds of dollars for very small code bases 

# ------------------- WARP_METHOD Configuration -------------------
# This setting determines how the warp rules are applied during code analysis:
# - 'combine': Merges all specified warp rule sets for a comprehensive analysis. -- this can result in huge rule sets depending on your usage 
# - 'sequential': Applies each warp rule set one after the other in their listed order. -- typically the most user friendly but also the most cost intensive 
# - 'selective': Uses only the specific warp rule set assigned to WARP_RULES. -- fine graind control over deliverd rules 
# To choose a method, set WARP_METHOD to 'combine', 'sequential', or 'selective'.

# ------------------- Warp Rules Overview -------------------
# Warp rules offer precise line-by-line code analysis. Using a single set provides fine-grained control, 
# while multiple sets enhance analysis depth and precision. Each rule set should be centered around a guiding question for consistency.

# Guiding Questions for Each Rule Set:
# WARP_RULES_SET_1: "How can we optimize the code for performance and readability?"
# WARP_RULES_SET_2: "What measures enhance the code's security and efficiency?"
# WARP_RULES_SET_3: "How can we improve code modularity and user experience?"
# WARP_RULES_SET_4: "What ensures the code is maintainable and scalable?"
# WARP_RULES_SET_5: "How to ensure robustness and cross-platform compatibility?"
# WARP_RULES_SET_6: "Best practices for database interactions and readability?"
# WARP_RULES_SET_7: "Optimizing code structure and reducing complexity?"
# WARP_RULES_SET_8: "Improving code integration capabilities and data processing?"
# WARP_RULES_SET_9: "Enhancing code resilience and user interface?"
# WARP_RULES_SET_10: "Ensuring precision and compatibility in the code?"

# Adjust the rules and guiding questions to fit your specific needs and context.

# --------------------------
# Configuration notes for pyreader.py
# --------------------------



# Note: setting the "lines_per_api_call" any lower than "10" is not advised unless your code is very compact or you have a very small script.

# The default amount for best functions is "10", but adjust this number if you find your code is not well described in the log or GUI.
# Feel free to fork this repository and add other functions as it was coded quickly.


# setting the "lines_per_api_call" any lower then "10" is not advised unless your code is very compact or you have a very small script

# the Default ammount for best functions is "10" but adjust this number if you find your code is not well described in the log enough or gui

# Feel free to fork this reposstitory and add other Functions as I coded this pretty quick

config.py
Nbtguyoriginal/Pybonce
import os
import tkinter as tk
from tkinter import filedialog, Text, messagebox, simpledialog, ttk
import openai
import threading
import config
from tkinter.ttk import Progressbar
import webbrowser
import shutil
from PyCells import Brain_matter


openai.api_key = config.API_KEY
memory = Brain_matter()



class AnalysisState:
    def __init__(self):
        self.current_thread = None
        self.abort_event = threading.Event()
        self.analyzed_files = set()


state = AnalysisState()

def start_time_warp_analysis_in_thread():
    directory = filedialog.askdirectory()
    if directory:  # Check if a directory was selected
        threading.Thread(target=time_warp_analysis, args=(directory,)).start()
    # Save a memory when Time Warp function is triggered
    memory.save_memory("Time Warp analysis started for directory: " + directory, "time_warp_start")
    
def setup_text_tags():
    analysis_text.tag_configure('sending', background=config.SENDING_COLOR, font=("Arial", 10))
    # Save a memory for 'sending' tag configuration
    memory.save_memory("Configured 'sending' tag with background color: " + config.SENDING_COLOR, "tag_configuration")

    analysis_text.tag_configure('analysis', background=config.ANALYSIS_COLOR, font=("Arial", 10))
    # Save a memory for 'analysis' tag configuration
    memory.save_memory("Configured 'analysis' tag with background color: " + config.ANALYSIS_COLOR, "tag_configuration")

    analysis_text.tag_configure('title', background=config.TITLE_COLOR, font=("Arial", 10, 'bold'))
    # Save a memory for 'title' tag configuration
    memory.save_memory("Configured 'title' tag with background color: " + config.TITLE_COLOR, "tag_configuration")

    analysis_text.tag_configure('highlight', background=config.HIGHLIGHT_COLOR, font=("Arial", 10))
    # Save a memory for 'highlight' tag configuration
    memory.save_memory("Configured 'highlight' tag with background color: " + config.HIGHLIGHT_COLOR, "tag_configuration")




def select_directory():
    folder_selected = filedialog.askdirectory()
    if not folder_selected:
        messagebox.showerror("Error", "No directory selected.")
        # Save a memory for the error when no directory is selected
        memory.save_memory("User attempted to select a directory but did not choose any.", "directory_selection_error")
        return
    load_treeview(folder_selected)
    # Save a memory for the successful directory selection
    memory.save_memory(f"User successfully selected the directory: {folder_selected}", "directory_selection")


def load_treeview(directory):
    for i in tree.get_children():
        tree.delete(i)
    populate_tree(tree, directory)


def populate_tree(tree, parent_path, parent=""):
    for entry in os.scandir(parent_path):
        oid = tree.insert(parent, "end", text=entry.name, open=False)
        if entry.is_dir():
            populate_tree(tree, entry.path, oid)


def write_log(content, log_type=config.LOG_TYPES["default"]):
    if log_type == config.LOG_TYPES["default"]:
        log_filename = config.LOG_FILE
    else:
        log_filename = f"{log_type}_log.txt"

    with open(log_filename, "a") as log_file:
        log_file.write(content + "\n\n")
        
    # Save a memory for the log entry written
    memory.save_memory(f"Log entry written to {log_filename}. Content: {content[:50]}...", "log_entry_written")  # Saving only the first 50 characters for brevity
    # Save a memory for the type of log written
    memory.save_memory(f"Log type used: {log_type}", "log_type_used")



def create_help_button(parent):
    def open_discord_invite():
        webbrowser.open('https://discord.gg/RxUAFDmubs')

    def open_help_menu():
        # Create a new window for the help menu
        help_window = tk.Toplevel(parent)
        help_window.title("Help Menu")
        
        # Function to display information when a tab is clicked
        def display_info(tab_number):
            info = {
                1: 'Pybonce is an intuitive Python code analysis tool designed to enhance the quality and efficiency of your scripts. By leveraging advanced AI capabilities, Pybonce offers in-depth insights into your code\'s performance, style, security, and functionality. With its user-friendly GUI, Pybonce allows users to easily select directories for analysis, view detailed feedback, and even apply specialized "warp rules" for customized code evaluations. Whether you\'re a seasoned developer or just starting out, Pybonce provides valuable feedback to elevate your coding skills and produce optimal Python scripts.', 
                2: 'simple explanation: In Pybonce, users select a directory with Python scripts for analysis, then the tool reads the scripts, sending code segments to an advanced AI model which evaluates the code on criteria like performance, style, security, and functionality, providing detailed feedback; users can further refine this analysis using "warp rules" for a tailored experience, and the resulting feedback is displayed in the GUI, highlighting key areas and insights, all while tracking the number of tokens processed by the AI to inform users of the computational cost. -Your code ---> pybonce agent--internal analysis-->external analysis---returned info---> visual log---->physical log ',
                3: 'Warp rules, integral to Pybonce\'s analytical capabilities, are meticulously crafted guidelines designed to facilitate a granular examination of code. By enabling users to zoom in on distinct facets of their scripts, be it performance optimization, security vulnerabilities, coding style, or functionality enhancements, these rules offer a bespoke analysis experience. The astute implementation of warp rules is paramount. When thoughtfully configured, they not only streamline the evaluation process but also ensure that the feedback generated is both pertinent and actionable. Instead of inundating users with generic or overly broad recommendations, a well-implemented warp rule system provides insights that directly align with a coder\'s unique objectives and priorities, making it an invaluable tool for refining and perfecting code.',
                4: 'Logging features in Pybonce serve as a comprehensive record-keeping mechanism, meticulously documenting every facet of the code analysis journey. These features capture the AI\'s feedback, segmenting it into distinct categories such as notes, modifications, and general observations. By maintaining a detailed chronicle of the AI\'s evaluations, users can effortlessly trace back to specific recommendations, understand the rationale behind them, and gauge the evolution of their code over time. Moreover, the logging system is not just a passive recorder; it\'s an active tool that aids in debugging, offers insights into recurring issues, and provides a structured format for future reference. When utilized effectively, these logging capabilities transform the ephemeral nature of feedback into a lasting resource, ensuring that users can continually leverage past insights to inform future coding decisions.',
                # ... add more tabs as needed
            }
            messagebox.showinfo(f"Information Tab {tab_number}", info[tab_number])


        # Add clickable information tabs
        tk.Button(help_window, text="Pybonce", command=lambda: display_info(1)).pack(pady=10)
        tk.Button(help_window, text="How Pybonce works", command=lambda: display_info(2)).pack(pady=10)
        tk.Button(help_window, text="Warp rules", command=lambda: display_info(2)).pack(pady=10)
        tk.Button(help_window, text="Logs and stuff", command=lambda: display_info(2)).pack(pady=10)
        # ... add more tabs as needed

    # Create the help button
    help_button = tk.Menubutton(parent, text="Help", relief=tk.RAISED)
    help_button.grid(row=0, column=4, padx=10, pady=10)

    # Create the dropdown menu
    help_menu = tk.Menu(help_button, tearoff=0)
    help_button.configure(menu=help_menu)

    # Add options to the dropdown menu
    help_menu.add_command(label="Get Socials", command=open_discord_invite)
    help_menu.add_command(label="Help Menu", command=open_help_menu)

    return help_button

def start_analysis():
    directory = filedialog.askdirectory()
    if not directory:
        return
    state.abort_event.clear()
    state.current_thread = threading.Thread(target=process_directory, args=(directory,))
    state.current_thread.start()
    abort_button.config(state=tk.DISABLED)
    # Save a memory when analysis starts
    memory.save_memory("Analysis started for directory: " + directory, "analysis_start")




def process_directory(directory):
    python_files = [f for f in os.listdir(directory) if f.endswith('.py')]

    total_tokens_used = 0
    total_estimated_cost = 0.0

    # Initialize the progress bar
    progress_var = tk.DoubleVar()
    progress_bar = Progressbar(root, variable=progress_var, maximum=len(python_files))
    progress_bar.pack(fill=tk.X, padx=10, pady=5)

    for index, file in enumerate(python_files):
        if state.abort_event.is_set():
            break
        file_path = os.path.join(directory, file)
        if file_path in state.analyzed_files:
            continue
        with open(file_path, 'r') as f:
            lines_buffer = []
            for line in f:
                lines_buffer.append(line)
                if len(lines_buffer) >= config.LINES_PER_API_CALL:
                    # Display the lines being sent with 'sending' tag
                    analysis_text.insert(tk.END, f"Sending lines for analysis from {file}:\n", 'title')
                    analysis_text.insert(tk.END, ''.join(lines_buffer) + "\n\n", 'sending')

                    context, highlighted_lines, tokens_used, estimated_cost = establish_context(lines_buffer, file)

                    # Display the analysis with 'analysis' tag
                    analysis_text.insert(tk.END, f"Analysis for {file}:\n", 'title')
                    analysis_text.insert(tk.END, ''.join(context) + "\n\n", 'analysis')

                    total_tokens_used += tokens_used
                    total_estimated_cost += estimated_cost
                    stats_label.config(text=f"Tokens Used: {total_tokens_used}\nEstimated Cost: ${total_estimated_cost:.2f}")

                    lines_buffer = []
            # Process any remaining lines in the buffer
            if lines_buffer:
                # Display the lines being sent with 'sending' tag
                analysis_text.insert(tk.END, f"Sending lines for analysis from {file}:\n", 'title')
                analysis_text.insert(tk.END, ''.join(lines_buffer) + "\n\n", 'sending')

                context, highlighted_lines, tokens_used, estimated_cost = establish_context(lines_buffer, file)

                # Display the analysis with 'analysis' tag
                analysis_text.insert(tk.END, f"Analysis for {file}:\n", 'title')
                analysis_text.insert(tk.END, ''.join(context) + "\n\n", 'analysis')

                total_tokens_used += tokens_used
                total_estimated_cost += estimated_cost
                stats_label.config(text=f"Tokens Used: {total_tokens_used}\nEstimated Cost: ${total_estimated_cost:.2f}")
        state.analyzed_files.add(file_path)

        # Update the progress bar
        progress_var.set(index + 1)
        root.update_idletasks()

    progress_bar.pack_forget()


def establish_context(lines, filename):
    prompt = (f"Thoroughly analyze the Python script named '{filename}' as if you are a seasoned Python and Windows expert. "
              "For each section or line of code, provide detailed feedback segmented into the following categories:\n"
              "- **Performance**: Are there any inefficiencies? How can they be optimized?\n"
              "- **Style**: Does the code follow best practices for readability and Pythonic style? How can it be made clearer?\n"
              "- **Security**: Are there potential vulnerabilities or unsafe practices?\n"
              "- **Functionality**: Are there potential bugs or areas of improvement in the logic?\n"
              "For each suggestion, explain the reasoning behind it. If a section is already optimal, acknowledge it. "
              "For place holders in .py files show best possible viable code based on previous suggestions"
              "Ensure a comprehensive review.\n\n"
              "After the analysis, provide a section titled 'MODIFICATIONS' where you list all the specific changes to be made to the code. "
              "Begin the analysis:\n\n" + "".join(lines))
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[
            {"role": "system", "content": "You are a helpful assistant with skills as if you are a seasoned Python and Windows expert."},
            {"role": "user", "content": prompt}
        ]
    )

    tokens_used = response['usage']['total_tokens']
    estimated_cost = (tokens_used / 1000) * TOKEN_COST_PER_THOUSAND

    context = response.choices[0].message['content'].strip()
    highlighted_lines = [context]

    # Log the analysis results based on content type
    if "NOTE:" in context:
        write_log(context, log_type=config.LOG_TYPES["notes"])
        # Save a memory for the type of feedback
        memory.save_memory(f"Feedback type: NOTE for script {filename}.")
    elif "MODIFICATION:" in context:
        write_log(context, log_type=config.LOG_TYPES["modifications"])
        # Save a memory for the type of feedback
        memory.save_memory(f"Feedback type: MODIFICATION for script {filename}.")
    else:
        write_log(context, log_type=config.LOG_TYPES["default"])

    # Save a memory for the script analysis
    memory.save_memory(f"Analyzed script: {filename}. Tokens used: {tokens_used}. Estimated cost: ${estimated_cost:.2f}.", "script_analysis")
    # Save a memory for the estimated cost of the analysis
    memory.save_memory(f"Estimated cost for analyzing {filename}: ${estimated_cost:.2f}.", "analysis_cost")

    return [context], highlighted_lines, tokens_used, estimated_cost




def clone_directory(source_directory):
    if not os.path.exists(source_directory):
        error_message = f"Source directory '{source_directory}' does not exist."
        memory.save_memory(f"Error encountered: {error_message}")
        raise ValueError(error_message)
    
    cloned_directory = source_directory + '_cloned'
    
    if os.path.exists(cloned_directory):
        error_message = f"Destination directory '{cloned_directory}' already exists."
        memory.save_memory(f"Error encountered: {error_message}")
        raise ValueError(error_message)
    
    shutil.copytree(source_directory, cloned_directory)
    
    # Save a memory for the directory cloning
    memory.save_memory(f"Cloned directory from '{source_directory}' to '{cloned_directory}'.", "directory_clone")
    
    return cloned_directory


def time_warp_analysis(directory):
    memory.save_memory(f"Started time warp analysis for directory: {directory}.", "time_warp_analysis_start")
    
    cloned_directory = clone_directory(directory)
    load_treeview(cloned_directory)
    python_files = [f for f in os.listdir(cloned_directory) if f.endswith('.py')]
    warp_rules = config.WARP_RULES
    warp_updates_log = []
    code_log = []
    modifications_log = []
    warp_manifest_log = []

    for file in python_files:
        memory.save_memory(f"Analyzing file: {file} using warp rules.")
        
        file_path = os.path.join(cloned_directory, file)
        with open(file_path, 'r') as f:
            lines = f.readlines()
            context = warp_analysis(lines, file, warp_rules)
            warp_updates_log.append((file, context))
            code_log.append(context)
            modifications_log.append(context)
            warp_manifest_log.append(context)

    with open(os.path.join(cloned_directory, "code.txt"), "w") as f:
        f.write("\n".join(code_log))
    with open(os.path.join(cloned_directory, "modifications.txt"), "w") as f:
        f.write("\n".join(modifications_log))
    with open(os.path.join(cloned_directory, "warp_manifest.txt"), "w") as f:
        f.write("\n".join(warp_manifest_log))

    for log in warp_updates_log:
        write_log(log[1])

    memory.save_memory(f"Completed time warp analysis for directory: {directory}.")
    
    return warp_updates_log


def warp_analysis(lines, filename, warp_rules):
    memory.save_memory(f"Started warp analysis for file: {filename} using warp rules.", "warp_analysis_start")
    
    prompt = (f"Analyze the Python script named '{filename}' based on the warp rules: '{warp_rules}'. "
              "Provide detailed notes about improvements, specifying the line number and the exact change required. "
              "Begin the analysis:\n\n" + "".join(lines))

    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[
            {"role": "system", "content": "You are a helpful assistant with skills as if you are a seasoned Python and Windows expert."},
            {"role": "user", "content": prompt}
        ]
    )

    context = response.choices[0].message['content'].strip()
    
    memory.save_memory(f"Completed warp analysis for file: {filename}. Results: {context[:100]}...", "warp_analysis_complete")  # Saving a snippet of the results for brevity.
    
    return context
def apply_warp_updates(directory, warp_updates_log):
    warp_rules = config.WARP_RULES
    updated_code_list = []
    placements = []  # Log to store the details of the changes

    for file, context in warp_updates_log:
        file_path = os.path.join(directory, file)
        memory.save_memory(f"Applying warp updates for file: {file_path}.")
        
        with open(file_path, 'r') as f:
            original_lines = f.readlines()

        # Extract the line number and change details from context
        # This is a placeholder and needs to be implemented based on the specific format of the context
        line_number, change_details = extract_line_and_change(context)

        # Pass the line and change details to ChatGPT with warp rules
        updated_line = get_updated_line(original_lines[line_number], change_details, warp_rules)

        # Save the updated line next to the original line
        updated_code_list.append((original_lines[line_number], updated_line))
        memory.save_memory(f"Original line: {original_lines[line_number]}. Updated line: {updated_line}.")

        # Log the placement details
        placement_detail = {
            "file_path": file_path,
            "line_number": line_number,
            "original_code": original_lines[line_number],
            "updated_code": updated_line
        }
        placements.append(placement_detail)
        memory.save_memory(f"Placement details for file: {file_path}. Line number: {line_number}. Original code: {original_lines[line_number]}. Updated code: {updated_line}.")

    # Write the placements log to a file
    with open("placements_log.txt", "w") as log_file:
        for placement in placements:
            log_file.write(f"File: {placement['file_path']}\n")
            log_file.write(f"Line Number: {placement['line_number']}\n")
            log_file.write(f"Original Code: {placement['original_code']}\n")
            log_file.write(f"Updated Code: {placement['updated_code']}\n")
            log_file.write("-" * 50 + "\n")
    memory.save_memory(f"Placements log written to placements_log.txt.", "placements_log_written")

    return updated_code_list


def apply_warp_rules(directory, memory_system):
    method = config.WARP_METHOD
    warp_sets = [config.WARP_RULES_SET_1, config.WARP_RULES_SET_2, config.WARP_RULES_SET_3, config.WARP_RULES_SET_4, config.WARP_RULES_SET_5,
                 config.WARP_RULES_SET_6, config.WARP_RULES_SET_7, config.WARP_RULES_SET_8, config.WARP_RULES_SET_9, config.WARP_RULES_SET_10]
    warp_updates_log = []

    # Store the initial information about warp rule application
    memory_system.store_memory(f"Starting warp rule application using method: {method}", "warp_rules")

    if method == 'combine':
        combined_rules = "\n".join(warp_sets)
        config.WARP_RULES = combined_rules

        # Store information about combining warp sets
        memory_system.store_memory(f"Combining all warp sets into one set. Applying combined rules.", "warp_rules")
        
        warp_updates_log.extend(time_warp_analysis(directory))

    elif method == 'sequential':
        # Store information about applying warp sets sequentially
        memory_system.store_memory(f"Applying warp sets sequentially.", "warp_rules")
        
        for warp_set in warp_sets:
            config.WARP_RULES = warp_set

            # Store information about applying a specific warp set
            memory_system.store_memory(f"Applying warp set: {warp_set}.", "warp_rules")
            
            warp_updates_log.extend(time_warp_analysis(directory))

    elif method == 'selective':
        # Store information about applying selected warp sets
        memory_system.store_memory(f"Applying selected warp sets.", "warp_rules")
        
        for warp_set in config.SELECTED_WARP_SETS:
            config.WARP_RULES = warp_set

            # Store information about applying a selected warp set
            memory_system.store_memory(f"Applying selected warp set: {warp_set}.", "warp_rules")
            
            warp_updates_log.extend(time_warp_analysis(directory))

    else:
        # Store information about an invalid method
        memory_system.store_memory(f"Invalid method detected: {method}.", "warp_rules")
        raise ValueError(f"Invalid method: {method}. Choose from 'combine', 'sequential', or 'selective'.")

    return warp_updates_log



def abort_analysis():
    if state.current_thread:
        state.abort_event.set()
        state.current_thread.join()
        state.current_thread = None
        abort_button.config(state=tk.NORMAL)
        messagebox.showinfo("Info", "Analysis aborted.")
        # Save a memory when analysis is aborted
        memory.save_memory("Analysis aborted.", "analysis_abort")


TOKEN_COST_PER_THOUSAND = 0.0010

root = tk.Tk()
root.title("Pybonce: The Python Pulse")
root.geometry("1000x600")

button_frame = tk.Frame(root, bg="white")
button_frame.pack(fill=tk.X, padx=10, pady=5)

open_button = tk.Button(button_frame, text="Select Directory", padx=10, pady=5, fg="white", bg="#263D42", command=select_directory)
open_button.grid(row=0, column=0, padx=5)

start_button = tk.Button(button_frame, text="Start Analysis", padx=10, pady=5, fg="white", bg="#263D42", command=start_analysis)
start_button.grid(row=0, column=1, padx=5)

abort_button = tk.Button(button_frame, text="Abort Analysis", padx=10, pady=5, fg="white", bg="#263D42", command=abort_analysis)
abort_button.grid(row=0, column=2, padx=5)

tree = ttk.Treeview(root)
tree.pack(side=tk.LEFT, fill=tk.Y, padx=10, pady=10)

time_warp_button = tk.Button(button_frame, text="Time Warp Function is a (Wip) and will probably freeze the gui or break programs", padx=10, pady=5, fg="#33FFA1", bg="white", state=tk.DISABLED)
time_warp_button.grid(row=0, column=3, padx=5)

time_warp_button = tk.Button(button_frame, text="Time Warp", padx=10, pady=5, fg="white", bg="#263D42", command=start_time_warp_analysis_in_thread)
time_warp_button.grid(row=1, column=3, padx=5)

stats_label = tk.Label(root, text="Tokens Used: 0\nEstimated Cost: $0.00", bg="white", font=("Arial", 10))
stats_label.pack(pady=10)

analysis_text = Text(root, wrap=tk.WORD)
analysis_text.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True, padx=10, pady=10)

create_help_button(button_frame)

setup_text_tags()

root.mainloop()

pyreader.py
Nbtguyoriginal/Pybonce