feat: Create api_helper.py for external copilot communication
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
|
||||
import http.server
|
||||
import socketserver
|
||||
import os
|
||||
import logging
|
||||
|
||||
# Configure logging
|
||||
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
||||
|
||||
COPILOT_HOST = os.getenv("COPILOT_HOST", "0.0.0.0")
|
||||
COPILOT_PORT = int(os.getenv("COPILOT_PORT", 8000))
|
||||
COPILOT_PATH = "/copilot"
|
||||
|
||||
class CopilotRequestHandler(http.server.BaseHTTPRequestHandler):
|
||||
def do_POST(self):
|
||||
if self.path == COPILOT_PATH:
|
||||
content_length = int(self.headers['Content-Length'])
|
||||
post_data_bytes = self.rfile.read(content_length)
|
||||
post_data_str = post_data_bytes.decode('utf-8')
|
||||
|
||||
logging.info(f"Received data from {self.client_address[0]}: {post_data_str}")
|
||||
|
||||
# In a real scenario, you would pass post_data_str to your AI model
|
||||
# and get a response. For now, we just echo it back.
|
||||
response_text = f"Copilot received: {post_data_str}"
|
||||
|
||||
self.send_response(200)
|
||||
self.send_header('Content-type', 'text/plain; charset=utf-8')
|
||||
self.end_headers()
|
||||
self.wfile.write(response_text.encode('utf-8'))
|
||||
else:
|
||||
self.send_response(404)
|
||||
self.end_headers()
|
||||
self.wfile.write(b"Not Found")
|
||||
|
||||
def do_GET(self):
|
||||
if self.path == "/health":
|
||||
self.send_response(200)
|
||||
self.send_header('Content-type', 'text/plain; charset=utf-8')
|
||||
self.end_headers()
|
||||
self.wfile.write(b"API Helper is running")
|
||||
else:
|
||||
self.send_response(404)
|
||||
self.end_headers()
|
||||
self.wfile.write(b"Not Found")
|
||||
|
||||
def run_server(server_class=http.server.HTTPServer, handler_class=CopilotRequestHandler, host=COPILOT_HOST, port=COPILOT_PORT):
|
||||
server_address = (host, port)
|
||||
httpd = server_class(server_address, handler_class)
|
||||
logging.info(f"Starting Copilot API helper on http://{host}:{port}{COPILOT_PATH}")
|
||||
logging.info(f"Health check available at http://{host}:{port}/health")
|
||||
try:
|
||||
httpd.serve_forever()
|
||||
except KeyboardInterrupt:
|
||||
logging.info("Server shutting down...")
|
||||
httpd.server_close()
|
||||
|
||||
if __name__ == '__main__':
|
||||
run_server()
|
||||
Reference in New Issue
Block a user