Dev Guide

A Complete Developer Guide To Rune AI

Rune AI & Controller Comprehensive Developer Guide

Table of Contents

  1. Introduction

  2. System Architecture

  3. Development Environment Setup

  4. Core Components

    • Command Processing System

    • AI Integration

    • Graphical User Interface

    • System Control Functions

  5. Detailed Function Analysis

  6. Error Handling and Logging

  7. Performance Optimization

  8. Security Considerations

  9. Testing Strategies

  10. Extending the Application

  11. Deployment Guidelines

  12. Troubleshooting and FAQs

  13. Contributing Guidelines

  14. Version History and Roadmap

1. Introduction

Rune AI & Controller is a sophisticated Python application that combines system control capabilities with AI-powered chat functionality. It provides users with a graphical interface to interact with their system through text commands or natural language queries, leveraging the power of Google's Gemini AI model.

Key Features:

  • System control through text commands

  • AI-powered natural language processing

  • File and folder management

  • Application control

  • System settings adjustment (resolution, volume, brightness, etc.)

  • Web search integration

  • Screenshot capture

2. System Architecture

The application follows a modular architecture with the following main components:

  1. GUI Layer: Handles user input and displays responses

  2. Command Processing Layer: Interprets user input and routes to appropriate functions

  3. AI Integration Layer: Communicates with the Gemini AI model

  4. System Control Layer: Executes system-level operations

  5. Utility Functions: Provides helper functions for various tasks

[User] <-> [GUI Layer] <-> [Command Processing Layer] <-> [System Control Layer] <-> [Operating System]
                             ^
                             |
                      [AI Integration Layer] <-> [Gemini AI API]

3. Development Environment Setup

Prerequisites:

  • Python 3.7 or higher

  • pip (Python package manager)

  • Git (for version control)

Step-by-step setup:

  1. Clone the repository:

    git clone https://github.com/Coder-soft/RuneAI-Python.git
    cd rune-ai-controller
  2. Create a virtual environment:

    python -m venv venv
    source venv/bin/activate  # On Windows, use `venv\Scripts\activate`
  3. Install dependencies:

    pip install -r requirements.txt
  4. Set up environment variables: Create a .env file in the project root and add:

    GEMINI_API_KEY=your_api_key_here
  5. Run the application:

    python main.py

4. Core Components

Command Processing System

The command processing system is the heart of the application. It interprets user input and routes it to the appropriate function.

Key function: process_command(command)

def process_command(command):
    command = command.lower()
    if command.startswith("ps "):
        return run_powershell_command(command[3:])
    elif "close" in command:
        return close_app_by_name_or_title(command.split("close ", 1)[-1])
    elif "resolution" in command:
        match = re.search(r'resolution (\d+)x(\d+)', command)
        if match:
            return change_resolution(*map(int, match.groups()))
    # ... other command handlers ...
    else:
        return handle_unrecognized_command(command)

This function uses a series of conditional statements to identify the command type and call the appropriate handler function.

AI Integration

The AI integration is handled by the chat_with_gemini() function, which communicates with the Gemini AI model.

import google.generativeai as genai

genai.configure(api_key="YOUR_API_KEY")
model = genai.GenerativeModel("gemini-1.5-flash")

def chat_with_gemini(prompt):
    try:
        context = "You are an AI assistant with the following additional knowledge:\n"
        for key, value in custom_knowledge.items():
            context += f"- {key.capitalize()}: {value}\n"
        context += "\nPlease use this information when relevant to answer the following question or command:\n"
        full_prompt = context + prompt
        response = model.generate_content(full_prompt)
        return response.text
    except Exception as e:
        return f"Error chatting with Rune AI: {str(e)}"

This function prepares a context for the AI, including custom knowledge about the application, and then sends the user's prompt to the Gemini model.

Graphical User Interface

The GUI is built using the customtkinter library, which provides a modern and customizable interface.

Key components:

  • chat_log: A Text widget for displaying the conversation

  • chat_input: An Entry widget for user input

  • send_button: A Button widget for sending messages

  • show_commands_button: A Button widget for displaying available commands

app = ctk.CTk()
app.title("Rune AI & Rune Controller")
app.geometry("1280x720")

chat_log = tk.Text(app, width=40, height=25, wrap='word', font=("Consolas", 16))
chat_log.pack(pady=10, padx=10, fill=tk.BOTH, expand=True)

input_frame = ctk.CTkFrame(app)
input_frame.pack(fill="x", padx=10, pady=10)

chat_input = ctk.CTkEntry(input_frame, placeholder_text="$~Type your message here...", font=("Fira Code Regular", 16))
chat_input.pack(side="left", fill="x", expand=True)

send_button = ctk.CTkButton(input_frame, text="↑", width=40, command=send_message, fg_color="#6bd0f0", text_color="black")
send_button.pack(side="right", padx=(0, 10))

commands_button = ctk.CTkButton(app, text="Show Commands", command=show_commands, fg_color="#6bd0f0", text_color="black")
commands_button.pack(pady=10)

System Control Functions

These functions interact with the operating system to perform various tasks. Here's an example of the volume control function:

from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
from ctypes import cast, POINTER
from comtypes import CLSCTX_ALL

def adjust_volume(level):
    devices = AudioUtilities.GetSpeakers()
    interface = devices.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
    volume = cast(interface, POINTER(IAudioEndpointVolume))
    volume.SetMasterVolumeLevelScalar(level/100.0, None)
    return f"Volume set to {level}%."

This function uses the pycaw library to interact with the Windows Core Audio API and adjust the system volume.

5. Detailed Function Analysis

Let's analyze a complex function in detail:

def capture_screenshot():
    def on_closing():
        if rect is not None and canvas.bbox("rect") is not None:
            x1 = root.winfo_rootx() + canvas.bbox("rect")[0]
            y1 = root.winfo_rooty() + canvas.bbox("rect")[1]
            x2 = x1 + (canvas.bbox("rect")[2] - canvas.bbox("rect")[0])
            y2 = y1 + (canvas.bbox("rect")[3] - canvas.bbox("rect")[1])
            screenshot = ImageGrab.grab(bbox=(x1, y1, x2, y2))
            save_path = filedialog.asksaveasfilename(defaultextension=".png", filetypes=[("PNG files", "*.png")])
            if save_path:
                screenshot.save(save_path)
                print(f"Screenshot saved to: {save_path}")
        else:
            print("No selection area was made.")
        root.quit()

    root = tk.Tk()
    root.attributes("-fullscreen", True)
    root.attributes("-alpha", 0.3)
    canvas = tk.Canvas(root, cursor="cross")
    canvas.pack(fill=tk.BOTH, expand=True)
    rect = None
    start_x = None
    start_y = None

    def on_mouse_down(event):
        nonlocal start_x, start_y, rect
        start_x = event.x
        start_y = event.y
        if rect:
            canvas.delete(rect)
        rect = canvas.create_rectangle(start_x, start_y, start_x, start_y, outline="red", width=2)

    def on_mouse_drag(event):
        nonlocal rect
        if rect:
            canvas.coords(rect, start_x, start_y, event.x, event.y)

    canvas.bind("<ButtonPress-1>", on_mouse_down)
    canvas.bind("<B1-Motion>", on_mouse_drag)
    root.bind("<Escape>", lambda event: root.quit())
    root.bind("<Return>", lambda event: on_closing())
    root.mainloop()

This function creates a fullscreen, semi-transparent window that allows the user to select an area for screenshot capture. It uses Tkinter for the GUI and PIL for image processing. The function employs nested function definitions and event bindings to handle user interactions.

6. Error Handling and Logging

Implement a robust error handling and logging system:

import logging

logging.basicConfig(filename='rune_ai.log', level=logging.INFO, 
                    format='%(asctime)s - %(levelname)s - %(message)s')

def safe_execute(func):
    def wrapper(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except Exception as e:
            error_msg = f"Error in {func.__name__}: {str(e)}"
            logging.error(error_msg)
            return f"An error occurred: {error_msg}"
    return wrapper

@safe_execute
def some_function():
    # Function implementation
    pass

Use this decorator on functions that might raise exceptions to ensure they're caught and logged.

7. Performance Optimization

For long-running tasks, use threading to keep the GUI responsive:

import threading

def handle_command(user_input):
    chat_log.insert(tk.END, f"$~You: {user_input}\n", "user")
    chat_log.insert(tk.END, "$~RuneAI: Processing...\n", "ai")
    
    def process_in_background():
        response = process_command(user_input)
        chat_log.delete("end-2l", "end-1l")
        chat_log.insert(tk.END, f"$~RuneAI: {response}\n", "ai")
    
    threading.Thread(target=process_in_background).start()

8. Security Considerations

  • Sanitize user input to prevent command injection:

import shlex

def safe_command(command):
    return shlex.quote(command)

# Usage
os.system(safe_command(user_input))
  • Use least privilege principle for system operations

  • Encrypt sensitive data (API keys, user data) at rest and in transit

9. Testing Strategies

Implement unit tests for individual functions:

import unittest

class TestRuneAI(unittest.TestCase):
    def test_adjust_volume(self):
        result = adjust_volume(50)
        self.assertEqual(result, "Volume set to 50%.")

    # Add more test cases

if __name__ == '__main__':
    unittest.main()

Implement integration tests to ensure different components work together correctly.

10. Extending the Application

To add a new feature:

  1. Create a new function in the appropriate module

  2. Add a new condition in process_command()

  3. Update the AI's knowledge base

  4. Add any necessary GUI elements

  5. Update documentation and tests

Example of adding a new "weather" command:

def get_weather(city):
    # Implement weather API call
    return f"Weather in {city}: ..."

# In process_command()
elif command.startswith("weather "):
    city = command.split("weather ", 1)[1]
    return get_weather(city)

# Update custom_knowledge
custom_knowledge["weather"] = "You can check weather by typing 'weather <city_name>'"

11. Deployment Guidelines

  1. Package the application:

    pyinstaller --onefile --windowed main.py
  2. Create an installer using a tool like Inno Setup

  3. Set up auto-updating mechanism

  4. Implement crash reporting

12. Troubleshooting and FAQs

Provide solutions for common issues:

  • "API key not found": Ensure the .env file is properly set up

  • "Module not found": Check if all dependencies are installed

  • "Permission denied": Run the application with administrator privileges

13. Contributing Guidelines

  1. Fork the repository

  2. Create a new branch for each feature or bug fix

  3. Follow the coding style guide (PEP 8 for Python)

  4. Write unit tests for new features

  5. Submit a pull request with a clear description of changes

14. Version History and Roadmap

  • v1.0.0: Initial release with basic functionality

  • v1.1.0: Added AI integration

  • v1.2.0: Improved GUI and added more system control features

Roadmap:

  • Implement user accounts and personalization

  • Add support for voice commands

  • Develop a plugin system for easy extensibility

This comprehensive guide should provide developers with a deep understanding of the Rune AI & Controller project, enabling them to effectively maintain, extend, and contribute to the application.

Last updated