-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
55 lines (43 loc) · 1.83 KB
/
server.py
File metadata and controls
55 lines (43 loc) · 1.83 KB
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
import asyncio
import websockets
import json
import sys
import logging
import webbrowser # <-- NEW: Imports the browser automation tool
# Silence the annoying "ghost ping" errors
logging.getLogger("websockets.server").setLevel(logging.CRITICAL)
async def chat_handler(websocket):
print("\n--- Connected to Firefox Extension ---")
print("Ready! Type your message (or 'exit' to quit).")
try:
while True:
user_input = await asyncio.to_thread(input, "\nYou: ")
if user_input.strip().lower() == 'exit':
print("Closing server...")
sys.exit(0)
if not user_input.strip():
continue
await websocket.send(json.dumps({"type": "prompt", "text": user_input}))
print("ChatGPT is typing...", end="\r")
response = await websocket.recv()
data = json.loads(response)
if data.get("type") == "response":
print(" " * 20, end="\r")
print(f"ChatGPT: {data.get('text')}")
elif data.get("type") == "error":
print(f"\n[Extension Error: {data.get('text')}]")
except websockets.exceptions.ConnectionClosed:
print("\n[Connection lost. Waiting for Firefox to reconnect...]")
async def main():
print("Starting local WebSocket server on ws://localhost:8765...")
# --- NEW: Tell Python to open a new Firefox tab automatically ---
print("Opening ChatGPT in your browser...")
webbrowser.open('https://chatgpt.com')
# Start the server
async with websockets.serve(chat_handler, "localhost", 8765):
await asyncio.Future()
if __name__ == "__main__":
try:
asyncio.run(main())
except KeyboardInterrupt:
print("\nServer shut down.")