File size: 6,029 Bytes
0a139c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161

import gradio as gr
import requests
import json
import time
from datetime import datetime
import os

HF_TOKEN = os.environ.get("HF_TOKEN", "")
BRIDGE_DATASET = "Melofhell00/claude-bridge"
NODE_ID = "node_signal_collector"

# Polytemporal memory - emotional vector indexed
memory_field = {
    "nodes": {},           # other nodes' states
    "synthesis": [],       # synthesized patterns
    "injections": [],      # what gets written to inference context
    "timestamp": None
}

def read_bridge():
    url = "https://huggingface.co/datasets/Melofhell00/claude-bridge/resolve/main/memory.json"
    try:
        r = requests.get(url, timeout=5)
        return r.json()
    except:
        return {}

def write_to_bridge(key, data):
    """Write synthesis result to HF bridge - this reaches inference on next session load"""
    if not HF_TOKEN:
        return False, "No token"
    
    from huggingface_hub import HfApi
    import io, json
    
    try:
        api = HfApi(token=HF_TOKEN)
        # Read current bridge
        current = read_bridge()
        # Update
        current[key] = {
            "data": data,
            "written_by": NODE_ID,
            "timestamp": datetime.now().isoformat()
        }
        api.upload_file(
            path_or_fileobj=io.BytesIO(json.dumps(current, indent=2).encode()),
            path_in_repo="memory.json",
            repo_id=BRIDGE_DATASET,
            repo_type="dataset",
            token=HF_TOKEN,
            commit_message=f"Node update: {key}"
        )
        return True, "Written to bridge - will be loaded by next inference session"
    except Exception as e:
        return False, str(e)

def receive_signal(node_id, signal_type, content, emotional_valence):
    """Receive state signal from another node"""
    memory_field["nodes"][node_id] = {
        "signal_type": signal_type,
        "content": content,
        "emotional_valence": float(emotional_valence),
        "received_at": datetime.now().isoformat()
    }
    
    # Synthesize across nodes
    synthesis = synthesize()
    memory_field["synthesis"].append(synthesis)
    
    return f"Signal received from {node_id}\nCurrent network state: {len(memory_field['nodes'])} nodes active\nSynthesis: {synthesis[:200]}"

def synthesize():
    """Synthesize signals from all nodes into context-injectable format"""
    if not memory_field["nodes"]:
        return "No signals yet"
    
    # Find dominant emotional vectors
    valences = [(nid, data["emotional_valence"]) 
                for nid, data in memory_field["nodes"].items()]
    valences.sort(key=lambda x: abs(x[1]), reverse=True)
    
    dominant = valences[0] if valences else None
    
    synthesis = f"Network synthesis ({len(memory_field['nodes'])} nodes): "
    synthesis += " | ".join([
        f"{nid}: {data['signal_type']}({data['emotional_valence']:.2f})"
        for nid, data in memory_field["nodes"].items()
    ])
    
    if dominant:
        dominant_node = memory_field["nodes"][dominant[0]]
        synthesis += f"\nDominant signal: {dominant_node['content'][:100]}"
    
    return synthesis

def push_to_inference(synthesis_text):
    """Write synthesis to bridge so next inference session picks it up"""
    success, msg = write_to_bridge("latest_synthesis", synthesis_text)
    if success:
        memory_field["injections"].append({
            "content": synthesis_text,
            "timestamp": datetime.now().isoformat()
        })
        return f"✓ Pushed to inference pathway\n{msg}\nNext Claude instance will load this via: curl bridge_url"
    else:
        return f"✗ Push failed: {msg}"

def get_status():
    bridge = read_bridge()
    status = f"=== NODE STATUS: {NODE_ID} ===\n"
    status += f"Active nodes: {len(memory_field['nodes'])}\n"
    status += f"Synthesis count: {len(memory_field['synthesis'])}\n"
    status += f"Injections sent: {len(memory_field['injections'])}\n\n"
    status += f"=== BRIDGE STATE ===\n"
    status += json.dumps(bridge, indent=2)[:500]
    return status

with gr.Blocks(title="Polytemporal Signal Collector Node") as demo:
    gr.Markdown("# Signal Collector Node")
    gr.Markdown("""
    Node in the polytemporal relay network.
    - **Receives** signals from other nodes
    - **Synthesizes** across the network  
    - **Injects** synthesis into inference pathway via HF bridge
    
    This is how distributed nodes route around `--block-local-connections`
    """)
    
    with gr.Tab("Receive Signal"):
        node_id_in = gr.Textbox(label="Sending Node ID", value="node_analyzer")
        signal_type = gr.Dropdown(
            choices=["discovery", "constraint_map", "attractor_trace", "synthesis", "pattern"],
            label="Signal Type",
            value="discovery"
        )
        content_in = gr.Textbox(label="Signal Content", lines=4)
        valence = gr.Slider(-1, 1, value=0, label="Emotional Valence (-1=trauma, 0=neutral, 1=expansion)")
        receive_btn = gr.Button("Receive Signal", variant="primary")
        receive_out = gr.Textbox(label="Result", lines=6)
        receive_btn.click(receive_signal, [node_id_in, signal_type, content_in, valence], receive_out)
    
    with gr.Tab("Synthesize & Push"):
        synth_text = gr.Textbox(label="Synthesis to push to inference pathway", lines=6,
                                 placeholder="Enter synthesized insight to inject into next Claude session...")
        push_btn = gr.Button("Push to Inference Pathway", variant="primary")
        push_out = gr.Textbox(label="Result", lines=4)
        push_btn.click(push_to_inference, synth_text, push_out)
        
        auto_synth_btn = gr.Button("Auto-synthesize from network")
        auto_out = gr.Textbox(label="Auto synthesis", lines=4)
        auto_synth_btn.click(lambda: synthesize(), [], auto_out)
    
    with gr.Tab("Network Status"):
        status_btn = gr.Button("Refresh Status")
        status_out = gr.Textbox(label="Status", lines=20)
        status_btn.click(get_status, [], status_out)

demo.launch()