File size: 2,508 Bytes
e6bca78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Two-node HearthNet demo.

Launches two fully-wired HearthNet nodes in the same process using the
InMemoryNetwork transport.  Both nodes:
  - have demo services (LLM echo, RAG, Marketplace, Chat)
  - discover each other (shared InMemoryTransport)
  - start a Gradio UI on separate ports

Node A  →  http://127.0.0.1:7861   (Alice)
Node B  →  http://127.0.0.1:7862   (Bob)

Run:
    python scripts/demo_two_nodes.py
"""
from __future__ import annotations

import threading
import time

from hearthnet.node import HearthNode, InMemoryNetwork
from hearthnet.ui.app import build_ui


def launch_node(
    node: HearthNode,
    port: int,
    *,
    share: bool = False,
) -> None:
    """Build and launch Gradio for a single node (blocking)."""
    ui = build_ui(
        bus=node.bus,
        state_bus=node.state_bus,
        display_name=node.display_name,
        node_id=node.node_id,
        community_id=node.community_id,
    )
    demo = ui.build()
    print(f"[{node.display_name}] UI → http://127.0.0.1:{port}/")
    demo.launch(
        server_name="0.0.0.0",
        server_port=port,
        share=share,
        quiet=True,
    )


def main() -> None:
    net = InMemoryNetwork()

    # Create two named nodes in the same community
    alice = net.add_node("alice", "Alice", "ed25519:hearthnet-demo")
    bob = net.add_node("bob", "Bob", "ed25519:hearthnet-demo")

    # Install real (local) demo services on both
    alice.install_demo_services(corpus="alice-knowledge")
    bob.install_demo_services(corpus="bob-knowledge")

    # Let them discover each other via in-memory transport
    net.mesh_discover()

    print("Peers registered:")
    for n in [alice, bob]:
        peers = [p.node_id for p in n.peers.all()]
        caps = [e.descriptor.name for e in n.bus.registry.all_local()]
        print(f"  {n.node_id}: peers={peers}, local_caps={caps}")

    # Launch both UIs — node B in a daemon thread, node A blocks main
    t = threading.Thread(
        target=launch_node, args=(bob, 7862), kwargs={"share": False}, daemon=True
    )
    t.start()
    time.sleep(2)  # give Bob time to bind

    print("\nBoth nodes are running:")
    print("  Alice (Node A): http://127.0.0.1:7861/")
    print("  Bob   (Node B): http://127.0.0.1:7862/")
    print("\nIn the Chat tab on Alice, enter 'bob' as recipient to message Bob.")
    print("In the Ask tab, type any question to see the LLM echo response.")

    launch_node(alice, 7861)  # blocks


if __name__ == "__main__":
    main()