Spaces:
Running

Komalpreet Kaur commited on
Commit
e95f953
·
unverified ·
1 Parent(s): 42b18d8

Phase 1 Complete: Implemented Internal Reflection node and Monologue UI.

Browse files
app/api/endpoints.py CHANGED
@@ -167,6 +167,7 @@ async def process_query_stream(request: QueryRequest):
167
  "chat_history": history,
168
  "context": [],
169
  "graph_context": [],
 
170
  "response": ""
171
  }
172
 
@@ -180,7 +181,13 @@ async def process_query_stream(request: QueryRequest):
180
  # For simplicity in this demo, we use the stream directly
181
  for output in orchestrator.stream(state_input):
182
  for node_name, node_output in output.items():
183
- if node_name == "retrieve":
 
 
 
 
 
 
184
  # Send Recall & Association Trace
185
  trace_data = node_output.get("trace_data", {})
186
  recall_msg = f"Found {trace_data.get('sensory_count')} sensory memories."
 
167
  "chat_history": history,
168
  "context": [],
169
  "graph_context": [],
170
+ "reflection": "",
171
  "response": ""
172
  }
173
 
 
181
  # For simplicity in this demo, we use the stream directly
182
  for output in orchestrator.stream(state_input):
183
  for node_name, node_output in output.items():
184
+ if node_name == "reflect":
185
+ # Send Internal Reflection Trace
186
+ reflection = node_output.get("reflection", "")
187
+ yield f"event: reflection\ndata: {json.dumps({'message': reflection})}\n\n"
188
+ await asyncio.sleep(0.3) # Give user time to read the 'thought'
189
+
190
+ elif node_name == "retrieve":
191
  # Send Recall & Association Trace
192
  trace_data = node_output.get("trace_data", {})
193
  recall_msg = f"Found {trace_data.get('sensory_count')} sensory memories."
app/services/orchestrator.py CHANGED
@@ -11,9 +11,29 @@ class AgentState(TypedDict):
11
  chat_history: List[dict]
12
  context: List[str]
13
  graph_context: List[str]
 
14
  response: str
15
 
16
- def retrieve(state: AgentState):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  # Phase 1: Perception (Implicit in state["input"])
18
 
19
  # Phase 2: Recall (Sensory Memory)
@@ -80,13 +100,15 @@ def create_graph():
80
  workflow = StateGraph(AgentState)
81
 
82
  # Add nodes
 
83
  workflow.add_node("retrieve", retrieve)
84
  workflow.add_node("call_model", call_model)
85
 
86
  # Set entry point
87
- workflow.set_entry_point("retrieve")
88
 
89
  # Add edges
 
90
  workflow.add_edge("retrieve", "call_model")
91
  workflow.add_edge("call_model", END)
92
 
 
11
  chat_history: List[dict]
12
  context: List[str]
13
  graph_context: List[str]
14
+ reflection: str
15
  response: str
16
 
17
+ def reflect(state: AgentState):
18
+ """Initial cognitive phase: Internal reflection on the user intent."""
19
+ api_key = settings.GROQ_API_KEY if settings.GROQ_API_KEY else "dummy_key"
20
+ llm = ChatGroq(model="llama-3.1-8b-instant", api_key=api_key)
21
+
22
+ prompt = f"""You are the internal monologue of Soma, a cognitive AI.
23
+ Briefly reflect on the user's input. What is their core intent?
24
+ What cognitive connections should we prioritize?
25
+ Keep it under 30 words. Express it as a raw, internal thought.
26
+
27
+ USER INPUT: {state["input"]}
28
+ INTERNAL REFLECTION:"""
29
+
30
+ try:
31
+ response = llm.invoke([HumanMessage(content=prompt)])
32
+ reflection = response.content.strip()
33
+ except:
34
+ reflection = "Processing intent through neural pathways..."
35
+
36
+ return {"reflection": reflection}
37
  # Phase 1: Perception (Implicit in state["input"])
38
 
39
  # Phase 2: Recall (Sensory Memory)
 
100
  workflow = StateGraph(AgentState)
101
 
102
  # Add nodes
103
+ workflow.add_node("reflect", reflect)
104
  workflow.add_node("retrieve", retrieve)
105
  workflow.add_node("call_model", call_model)
106
 
107
  # Set entry point
108
+ workflow.set_entry_point("reflect")
109
 
110
  # Add edges
111
+ workflow.add_edge("reflect", "retrieve")
112
  workflow.add_edge("retrieve", "call_model")
113
  workflow.add_edge("call_model", END)
114
 
frontend/src/App.jsx CHANGED
@@ -12,6 +12,7 @@ function App() {
12
  workingMemory: 0,
13
  statusMessage: "Brain idle.",
14
  isLoading: false,
 
15
  traces: []
16
  })
17
  const [currentView, setCurrentView] = useState('chat') // 'chat' or 'graph'
 
12
  workingMemory: 0,
13
  statusMessage: "Brain idle.",
14
  isLoading: false,
15
+ reflection: "",
16
  traces: []
17
  })
18
  const [currentView, setCurrentView] = useState('chat') // 'chat' or 'graph'
frontend/src/components/ChatPanel.jsx CHANGED
@@ -28,6 +28,7 @@ function ChatPanel({ messages, setMessages, setBrainState, isLoading }) {
28
  ...prev,
29
  isLoading: true,
30
  statusMessage: 'Initiating neural handshake...',
 
31
  traces: []
32
  }));
33
 
@@ -64,6 +65,11 @@ function ChatPanel({ messages, setMessages, setBrainState, isLoading }) {
64
  statusMessage: data.message,
65
  traces: [...prev.traces, data]
66
  }));
 
 
 
 
 
67
  } else if (eventType === 'final_result') {
68
  setMessages(prev => [...prev, { role: 'soma', content: data.response }]);
69
  } else if (eventType === 'error') {
 
28
  ...prev,
29
  isLoading: true,
30
  statusMessage: 'Initiating neural handshake...',
31
+ reflection: '',
32
  traces: []
33
  }));
34
 
 
65
  statusMessage: data.message,
66
  traces: [...prev.traces, data]
67
  }));
68
+ } else if (eventType === 'reflection') {
69
+ setBrainState(prev => ({
70
+ ...prev,
71
+ reflection: data.message
72
+ }));
73
  } else if (eventType === 'final_result') {
74
  setMessages(prev => [...prev, { role: 'soma', content: data.response }]);
75
  } else if (eventType === 'error') {
frontend/src/components/CognitiveDashboard.jsx CHANGED
@@ -124,6 +124,13 @@ function CognitiveDashboard({ brainState, setBrainState }) {
124
  <h3 className="label-mono" style={{ fontSize: '0.65rem', marginBottom: '10px' }}>Neural Status</h3>
125
  <div className="label-mono" style={{ color: 'var(--accent-primary)', fontSize: '0.6rem', marginBottom: '10px' }}>{brainState.statusMessage}</div>
126
 
 
 
 
 
 
 
 
127
  <CognitiveTrace traces={brainState.traces} />
128
 
129
  {ingestStatus && (
 
124
  <h3 className="label-mono" style={{ fontSize: '0.65rem', marginBottom: '10px' }}>Neural Status</h3>
125
  <div className="label-mono" style={{ color: 'var(--accent-primary)', fontSize: '0.6rem', marginBottom: '10px' }}>{brainState.statusMessage}</div>
126
 
127
+ {brainState.reflection && (
128
+ <div className="internal-monologue label-mono">
129
+ <span className="label-mono" style={{ fontSize: '0.5rem', opacity: 0.5, display: 'block', marginBottom: '4px' }}>// INTERNAL REFLECTION</span>
130
+ {brainState.reflection}
131
+ </div>
132
+ )}
133
+
134
  <CognitiveTrace traces={brainState.traces} />
135
 
136
  {ingestStatus && (
frontend/src/index.css CHANGED
@@ -183,6 +183,25 @@ button:disabled {
183
  opacity: 0.8;
184
  }
185
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
186
  /* ── Neural Pulse Animations ── */
187
  .pulse-active {
188
  animation: box-glow 2s infinite alternate;
 
183
  opacity: 0.8;
184
  }
185
 
186
+ .internal-monologue {
187
+ background: rgba(0, 0, 0, 0.4);
188
+ border-left: 2px solid var(--accent-secondary);
189
+ padding: 12px;
190
+ margin: 10px 0 15px 0;
191
+ font-size: 0.65rem;
192
+ line-height: 1.5;
193
+ color: var(--text-secondary);
194
+ font-style: italic;
195
+ box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.5);
196
+ animation: monologue-fade 0.6s ease-out;
197
+ border-radius: 4px;
198
+ }
199
+
200
+ @keyframes monologue-fade {
201
+ from { opacity: 0; transform: translateY(-5px); }
202
+ to { opacity: 1; transform: translateY(0); }
203
+ }
204
+
205
  /* ── Neural Pulse Animations ── */
206
  .pulse-active {
207
  animation: box-glow 2s infinite alternate;
soma_sessions.db ADDED
Binary file (12.3 kB). View file
 
working_memory.db ADDED
Binary file (12.3 kB). View file