File size: 3,933 Bytes
2ca5874
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# ==========================================
# Copyright (c) 2026 Gabriela Berger AI Oberland
# All Rights Reserved.
# This code is subject to the custom NON-COMMERCIAL 
# & ANTI-CORPORATE LICENSE (Maximum 20 PCs) found in the LICENSE file.
# ==========================================
import requests
import subprocess
import time
from config import AppConfig
from tkinter import messagebox

class NetworkManager:
    def __init__(self):
        self.config = AppConfig()
        self.active_model = None
        self.active_agent = None

    def start_model_server(self, model_name):
        if model_name not in self.config.models:
            messagebox.showerror("Error", f"Model {model_name} not found")
            return False

        model = self.config.models[model_name]
        try:
            # Start the model server
            self.model_process = subprocess.Popen(
                model.command,
                shell=True,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE
            )

            # Wait for server to start
            max_attempts = 30
            for _ in range(max_attempts):
                try:
                    response = requests.get(f"http://{model.host}:{model.port}/v1/models", timeout=1)
                    if response.status_code == 200:
                        self.active_model = model
                        messagebox.showinfo("Success", f"{model_name} server started successfully")
                        return True
                except:
                    time.sleep(1)

            messagebox.showerror("Error", f"Failed to start {model_name} server")
            return False

        except Exception as e:
            messagebox.showerror("Error", f"Failed to start model: {str(e)}")
            return False

    def start_agent(self, agent_name):
        if agent_name not in self.config.agents:
            messagebox.showerror("Error", f"Agent {agent_name} not found")
            return False

        agent = self.config.agents[agent_name]
        try:
            # Start the agent
            self.agent_process = subprocess.Popen(
                agent.command,
                shell=True,
                cwd=agent.workdir,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE
            )
            self.active_agent = agent
            messagebox.showinfo("Success", f"{agent_name} agent started successfully")
            return True

        except Exception as e:
            messagebox.showerror("Error", f"Failed to start agent: {str(e)}")
            return False

    def send_to_model(self, prompt):
        if not self.active_model:
            return {"error": "No active model"}

        try:
            response = requests.post(
                f"http://{self.active_model.host}:{self.active_model.port}/v1/completions",
                json={"prompt": prompt, "max_tokens": 800},
                timeout=600
            )
            return response.json()
        except Exception as e:
            return {"error": str(e)}

    def send_to_agent(self, message):
        if not self.active_agent:
            return {"error": "No active agent"}

        # In a real implementation, this would connect to the agent's API
        return {"response": f"Agent {self.active_agent.name} received: {message}"}

    def post(self, url, json=None, data=None, headers=None, timeout=10):
        """Generic POST method for main_window.py compatibility."""
        response = requests.post(url, json=json, data=data, headers=headers, timeout=timeout)
        response.raise_for_status()
        return response

    def get(self, url, params=None, headers=None, timeout=10):
        """Generic GET method for main_window.py compatibility."""
        response = requests.get(url, params=params, headers=headers, timeout=timeout)
        response.raise_for_status()
        return response