|
|
|
|
|
""" |
|
|
nova_bridge.py β Relay plans and results between two Open WebUI chats over NATS. |
|
|
|
|
|
Env vars: |
|
|
OPENWEBUI_URL default http://127.0.0.1:17500 |
|
|
CHAT_ELIZABETH chat ID for Elizabeth |
|
|
CHAT_KAUTILYA chat ID for Kautilya |
|
|
NATS_URL default nats://127.0.0.1:4222 |
|
|
|
|
|
Subjects: |
|
|
nova.plan β Plan/directives from Elizabeth β forwarded to Kautilya chat |
|
|
nova.result β Execution summaries/artifacts from Kautilya β forwarded to Elizabeth chat |
|
|
|
|
|
Usage: |
|
|
CHAT_ELIZABETH=<id1> CHAT_KAUTILYA=<id2> ./scripts/nova_bridge.py |
|
|
""" |
|
|
import os |
|
|
import json |
|
|
import asyncio |
|
|
import time |
|
|
from urllib import request |
|
|
|
|
|
|
|
|
OPENWEBUI_URL = os.environ.get("OPENWEBUI_URL", "http://127.0.0.1:17500") |
|
|
CHAT_ELIZABETH = os.environ.get("CHAT_ELIZABETH", "") |
|
|
CHAT_KAUTILYA = os.environ.get("CHAT_KAUTILYA", "") |
|
|
NATS_URL = os.environ.get("NATS_URL", "nats://127.0.0.1:18222") |
|
|
|
|
|
|
|
|
def post_message(chat_id: str, role: str, content: str) -> dict: |
|
|
url = f"{OPENWEBUI_URL}/api/chat/completions" |
|
|
headers = {"Content-Type": "application/json"} |
|
|
body = { |
|
|
"chat_id": chat_id, |
|
|
"stream": False, |
|
|
"model": None, |
|
|
"messages": [{"role": role, "content": content}], |
|
|
} |
|
|
data = json.dumps(body).encode("utf-8") |
|
|
req = request.Request(url, data=data, headers=headers, method="POST") |
|
|
try: |
|
|
with request.urlopen(req, timeout=10) as resp: |
|
|
txt = resp.read().decode("utf-8") |
|
|
try: |
|
|
return {"status": resp.status, "json": json.loads(txt)} |
|
|
except Exception: |
|
|
return {"status": resp.status, "text": txt} |
|
|
except Exception as e: |
|
|
return {"error": str(e)} |
|
|
|
|
|
|
|
|
async def main(): |
|
|
if not CHAT_ELIZABETH or not CHAT_KAUTILYA: |
|
|
raise SystemExit("CHAT_ELIZABETH and CHAT_KAUTILYA must be set") |
|
|
|
|
|
try: |
|
|
import nats |
|
|
except Exception as e: |
|
|
raise SystemExit(f"nats-py not installed: {e}") |
|
|
|
|
|
nc = await nats.connect(NATS_URL) |
|
|
|
|
|
async def on_plan(msg): |
|
|
try: |
|
|
data = msg.data.decode("utf-8") |
|
|
except Exception: |
|
|
data = "" |
|
|
payload = data or "(empty plan)" |
|
|
res = post_message(CHAT_KAUTILYA, "user", f"[plan]\n{payload}") |
|
|
print("forwarded plan β Kautilya:", res) |
|
|
|
|
|
async def on_result(msg): |
|
|
try: |
|
|
data = msg.data.decode("utf-8") |
|
|
except Exception: |
|
|
data = "" |
|
|
payload = data or "(empty result)" |
|
|
res = post_message(CHAT_ELIZABETH, "user", f"[result]\n{payload}") |
|
|
print("forwarded result β Elizabeth:", res) |
|
|
|
|
|
await nc.subscribe("nova.plan", cb=on_plan) |
|
|
await nc.subscribe("nova.result", cb=on_result) |
|
|
print("nova_bridge running; subjects: nova.plan, nova.result") |
|
|
|
|
|
try: |
|
|
while True: |
|
|
await asyncio.sleep(3600) |
|
|
finally: |
|
|
await nc.close() |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
asyncio.run(main()) |
|
|
|