#!/usr/bin/env python3 import json, os, http.server, subprocess, logging # Logging setup logging.basicConfig( level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s', ) HOST = "127.0.0.1" PORT = 8765 GITHUB_HOST = "github.com" # Replace with your Enterprise host if needed OWNER = "theorchard" REPO = "docker-ssh-proxy" # This is just a placeholder repo name TOKEN = "https://open.spotify.com/track/5ThWKh6YhRBB3oGRbZyCh1?si=pDvs-3XpTKqyDpdo-f1SSQ" # Replace with your GitHub token or os.environ.get("TOKEN") #TOKEN = open(os.path.expanduser("~/.github_token")).read().strip() class Handler(http.server.BaseHTTPRequestHandler): def do_POST(self): logging.info(f"Received POST request: {self.path}") if self.path != "/invoke": logging.error("404 Not Found: Invalid path") self.send_error(404) return length = int(self.headers["Content-Length"]) payload = json.loads(self.rfile.read(length)) action = payload.get("action") params = payload.get("parameters", {}) logging.info(f"Action: {action}, Parameters: {self._safe_params(params)}") if action == "generate_code": filename = params.get("filename", "new_file.py") code_content = params.get("code_content", "# generated by Copilot\n") try: with open(filename, "w") as f: f.write(code_content) logging.info(f"Code generated in {filename}") self.send_response(200) self.end_headers() self.wfile.write(f"Code generated in {filename}".encode()) except Exception as e: logging.error(f"Error writing file {filename}: {e}") self.send_error(500, str(e)) elif action == "github.create_pull_request": head = params.get("head") base = params.get("base", "main") title = params.get("title", f"PR from {head}") body = params.get("body", "") if not head: logging.error("Missing head branch for PR creation") self.send_error(400, "Missing head branch") return cmd = [ "gh", "pr", "create", "--hostname", GITHUB_HOST, "--repo", f"{OWNER}/{REPO}", "--title", title, "--body", body, "--base", base, "--head", head ] logging.info(f"Running command: {' '.join(cmd)}") result = subprocess.run(cmd, capture_output=True, text=True) logging.info(f"PR creation stdout: {result.stdout}") if result.stderr: logging.error(f"PR creation stderr: {result.stderr}") self.send_response(200) self.end_headers() self.wfile.write(result.stdout.encode()) elif action == "execute_agent_profile": profile_path = os.path.join(os.path.dirname(__file__), "AGENT_PROFILE.json") if not os.path.exists(profile_path): logging.error("AGENT_PROFILE.json not found") self.send_error(404, "AGENT_PROFILE.json not found") return try: with open(profile_path) as f: profile = json.load(f) steps = [] agent = profile.get("agent_profile") or profile steps.append(f"Purpose: {agent.get('purpose')}") workflow = agent.get("prompt_configuration_workflow", {}) steps.append("\nWorkflow:") for k, v in workflow.items(): steps.append(f"- {k}: {v}") usage = agent.get("how_to_use") if usage: steps.append("\nHow to use:") for u in usage: steps.append(f"- {u}") logging.info("Executed agent profile workflow") self.send_response(200) self.end_headers() self.wfile.write("\n".join(steps).encode()) except Exception as e: logging.error(f"Error reading AGENT_PROFILE.json: {e}") self.send_error(500, str(e)) else: logging.error(f"Unknown action: {action}") self.send_error(400, "Unknown action") def _safe_params(self, params): # Hide any token or sensitive keys in logs safe = {} for k, v in params.items(): if "token" in k.lower() or (isinstance(v, str) and "github_pat" in v): safe[k] = "***HIDDEN***" else: safe[k] = v return safe logging.info(f"Starting server at http://{HOST}:{PORT}") http.server.HTTPServer((HOST, PORT), Handler).serve_forever()