|
| 1 | +import uuid |
| 2 | +from typing import Dict, List |
| 3 | +from quantum_simulator import QuantumSimulator |
| 4 | +from webxos_wallet import WebXOSWallet |
| 5 | +from agents.agent1 import run_agent as run_agent1 |
| 6 | +from agents.agent2 import run_agent as run_agent2 |
| 7 | +from agents.agent3 import run_agent as run_agent3 |
| 8 | +from agents.agent4 import run_agent as run_agent4 |
| 9 | +import logging |
| 10 | + |
| 11 | +logger = logging.getLogger(__name__) |
| 12 | + |
| 13 | +class VialManager: |
| 14 | + def __init__(self, wallet: WebXOSWallet): |
| 15 | + self.wallet = wallet |
| 16 | + self.quantum_sim = QuantumSimulator() |
| 17 | + self.vials: Dict[str, Dict] = { |
| 18 | + f"vial{i+1}": { |
| 19 | + "id": f"vial{i+1}", |
| 20 | + "status": "stopped", |
| 21 | + "code": "", |
| 22 | + "code_length": 0, |
| 23 | + "is_python": True, |
| 24 | + "webxos_hash": str(uuid.uuid4()), |
| 25 | + "wallet": {"address": wallet.create_address(), "balance": 0.0}, |
| 26 | + "tasks": [] |
| 27 | + } for i in range(4) |
| 28 | + } |
| 29 | + self.agent_runners = { |
| 30 | + "vial1": run_agent1, |
| 31 | + "vial2": run_agent2, |
| 32 | + "vial3": run_agent3, |
| 33 | + "vial4": run_agent4 |
| 34 | + } |
| 35 | + |
| 36 | + def train_vials(self, network_id: str, code: str, filename: str) -> float: |
| 37 | + try: |
| 38 | + balance_earned = 0.0004 |
| 39 | + for vial_id, vial in self.vials.items(): |
| 40 | + vial["code"] = code |
| 41 | + vial["code_length"] = len(code) |
| 42 | + vial["is_python"] = filename.endswith('.py') |
| 43 | + vial["status"] = "running" |
| 44 | + vial["tasks"] = self.quantum_sim.simulate_training(vial_id, code)["tasks"] |
| 45 | + vial["wallet"]["balance"] += 0.0001 |
| 46 | + self.wallet.add_balance(vial["wallet"]["address"], 0.0001) |
| 47 | + self.agent_runners[vial_id](code) # Run the corresponding agent |
| 48 | + logger.info(f"Trained vials for network: {network_id}") |
| 49 | + return balance_earned |
| 50 | + except Exception as e: |
| 51 | + logger.error(f"Train vials error: {str(e)}") |
| 52 | + with open("errorlog.md", "a") as f: |
| 53 | + f.write(f"- **[2025-08-10T20:23:00Z]** Train vials error: {str(e)}\n") |
| 54 | + raise |
| 55 | + |
| 56 | + def reset_vials(self): |
| 57 | + try: |
| 58 | + for vial in self.vials.values(): |
| 59 | + vial["status"] = "stopped" |
| 60 | + vial["code"] = "" |
| 61 | + vial["code_length"] = 0 |
| 62 | + vial["is_python"] = True |
| 63 | + vial["webxos_hash"] = str(uuid.uuid4()) |
| 64 | + vial["tasks"] = [] |
| 65 | + vial["wallet"]["balance"] = 0.0 |
| 66 | + logger.info("Vials reset") |
| 67 | + except Exception as e: |
| 68 | + logger.error(f"Reset vials error: {str(e)}") |
| 69 | + with open("errorlog.md", "a") as f: |
| 70 | + f.write(f"- **[2025-08-10T20:23:00Z]** Reset vials error: {str(e)}\n") |
| 71 | + raise |
| 72 | + |
| 73 | + def get_vials(self) -> Dict: |
| 74 | + return self.vials |
0 commit comments