File size: 2,940 Bytes
fbf3c28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python3
"""
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,  # use chat default
        "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  # type: ignore
    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())