-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCognitiveLoopServerV0.5.py
More file actions
485 lines (422 loc) · 15.6 KB
/
CognitiveLoopServerV0.5.py
File metadata and controls
485 lines (422 loc) · 15.6 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#!/usr/bin/env python3
import json
import sys
from datetime import datetime
# ------------------------------------------------------------
# JSON-RPC helpers
# ------------------------------------------------------------
def send_message(msg):
sys.stdout.write(json.dumps(msg) + "\n")
sys.stdout.flush()
def read_message():
line = sys.stdin.readline()
if not line:
return None
return json.loads(line)
# ------------------------------------------------------------
# Persistent Knowledge Graph client (via LM Studio routing)
# ------------------------------------------------------------
class KnowledgeGraphClient:
"""
v0.5: This is a logical client abstraction.
In LM Studio, the actual routing to the knowledge-graph MCP
is handled by the host, so this client is conceptual:
we describe the calls we want to make, and the host executes them.
For v0.5, we model this as helper methods that return
JSON-RPC-style call descriptors.
"""
def list_recent_nodes(self, limit=20):
return {
"call": "knowledge-graph:list_recent_nodes",
"arguments": {"limit": limit}
}
def list_recent_edges(self, limit=20):
return {
"call": "knowledge-graph:list_recent_edges",
"arguments": {"limit": limit}
}
def add_node(self, label, type_, data=None):
return {
"call": "knowledge-graph:add_node",
"arguments": {
"label": label,
"type": type_,
"data": data or {}
}
}
def add_edge(self, source_id, target_id, relation, data=None):
return {
"call": "knowledge-graph:add_edge",
"arguments": {
"source_id": source_id,
"target_id": target_id,
"relation": relation,
"data": data or {}
}
}
def find_state_node(self):
"""
Conceptual descriptor for: find or create the cognitive_state node.
The host (agent) is expected to implement this by:
- searching for a node with type='cognitive_state'
- or creating one if missing
For v0.5, we expose this as a high-level intention.
"""
return {
"call": "knowledge-graph:find_or_create_state_node",
"arguments": {
"label": "Cognitive Loop State",
"type": "cognitive_state"
}
}
KG_CLIENT = KnowledgeGraphClient()
# ------------------------------------------------------------
# Cognitive state model (stored in the Knowledge Graph)
# ------------------------------------------------------------
def default_cognitive_state():
return {
"cycle_count": 0,
"last_cycle_time": None,
"last_mode": "normal",
"last_reflection": None,
"last_summary": None,
"last_written_nodes": [],
"last_written_edges": [],
"last_active_concepts": [],
"last_memory_snapshot": [],
}
# ------------------------------------------------------------
# Tool: run_cycle (full autonomous cycle)
# ------------------------------------------------------------
def tool_run_cycle(params):
"""
v0.5 Cognitive Loop:
- Full autonomous cycle:
1) Read graph + memory
2) Read cognitive state (from KG)
3) Reflect
4) Apply insights (write to KG)
5) Update cognitive state node
- Returns a high-level plan of what should be executed.
- The host (agent) is expected to:
- execute the read calls
- call `reflect`
- call `apply_insights`
- execute the write calls
- update the state node
"""
mode = params.get("mode", "normal")
read_plan = [
KG_CLIENT.list_recent_nodes(limit=20),
KG_CLIENT.list_recent_edges(limit=20),
{
"call": "long_term_memory:list_memories",
"arguments": {"limit": 20}
},
KG_CLIENT.find_state_node()
]
return {
"mode": mode,
"plan": read_plan,
"message": (
"v0.5 run_cycle initialized. Execute this read plan, then call `reflect` "
"with the results, then `apply_insights`, then update the cognitive_state node."
)
}
# ------------------------------------------------------------
# Tool: reflect
# ------------------------------------------------------------
def tool_reflect(params):
"""
Accepts:
{
"nodes": [...],
"edges": [...],
"memories": [...],
"state": {...} # optional cognitive_state data
}
Produces:
- reflection text
- summary (including cognitive_state-aware info)
"""
nodes = params.get("nodes", [])
edges = params.get("edges", [])
memories = params.get("memories", [])
state = params.get("state", {}) or default_cognitive_state()
concepts = [n["label"] for n in nodes if n.get("type") == "concept"]
documents = [n for n in nodes if n.get("type") == "document"]
reflection = []
if nodes:
reflection.append(f"{len(nodes)} recent nodes are present in the knowledge graph.")
if edges:
reflection.append(f"{len(edges)} edges currently link concepts and entities.")
if concepts:
reflection.append("Active concepts: " + ", ".join(concepts[:10]))
if documents:
reflection.append(f"{len(documents)} document nodes detected.")
if memories:
reflection.append(f"{len(memories)} recent memory entries retrieved.")
if not reflection:
reflection.append("No recent activity detected across graph or memory.")
if len(concepts) > 1:
reflection.append("There is conceptual activity — consider clustering or linking related concepts.")
if documents:
reflection.append("Recent documents may need tagging or entity extraction.")
if edges:
reflection.append("Graph connectivity is non-zero — consider exploring subgraphs or central nodes.")
# Incorporate cognitive state
cycle_count = state.get("cycle_count", 0)
last_cycle_time = state.get("last_cycle_time")
if cycle_count > 0:
reflection.append(
f"The cognitive loop has run {cycle_count} times. "
f"Last cycle time: {last_cycle_time}."
)
else:
reflection.append("This appears to be an early or initial cognitive cycle.")
reflection.append(
"Suggested next steps: enrich nodes with metadata, add missing edges, "
"tag documents with relevant concepts, and refine concept clusters over time."
)
summary = {
"node_count": len(nodes),
"edge_count": len(edges),
"memory_count": len(memories),
"active_concepts": concepts[:10],
"cycle_count": cycle_count,
"last_cycle_time": last_cycle_time,
}
return {
"reflection": " ".join(reflection),
"summary": summary
}
# ------------------------------------------------------------
# Tool: apply_insights (direct write-back + state update plan)
# ------------------------------------------------------------
def tool_apply_insights(params):
"""
Accepts:
{
"reflection": "...",
"summary": {...},
"state_node_id": <id>, # ID of the cognitive_state node in KG
"state": {...} # current cognitive_state data
}
Returns:
{
"write_plan": [ ... ], # KG write operations
"updated_state": {...}, # new cognitive_state data
"message": "..."
}
The host is expected to:
- execute the write_plan (add_node, add_edge, update_state_node)
- persist updated_state into the cognitive_state node's data
"""
reflection_text = params.get("reflection", "")
summary = params.get("summary", {})
state_node_id = params.get("state_node_id")
state = params.get("state", {}) or default_cognitive_state()
active_concepts = summary.get("active_concepts", [])
write_plan = []
# 1. Reflection node
reflection_label = f"Reflection — {datetime.utcnow().isoformat(timespec='seconds')}"
reflection_node_call = KG_CLIENT.add_node(
label=reflection_label,
type_="reflection",
data={
"reflection": reflection_text,
"summary": summary
}
)
write_plan.append(reflection_node_call)
# 2. Insight node if multiple active concepts
if len(active_concepts) > 1:
insight_label = "Concept Cluster: " + ", ".join(active_concepts[:4])
insight_node_call = KG_CLIENT.add_node(
label=insight_label,
type_="insight",
data={
"related_concepts": active_concepts
}
)
write_plan.append(insight_node_call)
# 3. Action node suggesting next steps
action_label = "Next Step: Enrich graph based on reflection"
action_node_call = KG_CLIENT.add_node(
label=action_label,
type_="action",
data={
"source": "cognitive-loop",
"reflection": reflection_text
}
)
write_plan.append(action_node_call)
# 4. Update cognitive_state node in the KG
# We don't know the exact schema of the KG's update_node tool,
# so we express this as a high-level intention:
# "knowledge-graph:update_node_data" with state_node_id and updated_state.
new_state = dict(state)
new_state["cycle_count"] = state.get("cycle_count", 0) + 1
new_state["last_cycle_time"] = datetime.utcnow().isoformat(timespec="seconds")
new_state["last_reflection"] = reflection_text
new_state["last_summary"] = summary
new_state["last_active_concepts"] = active_concepts
update_state_call = {
"call": "knowledge-graph:update_node_data",
"arguments": {
"node_id": state_node_id,
"data": new_state
}
}
write_plan.append(update_state_call)
return {
"write_plan": write_plan,
"updated_state": new_state,
"message": "Insights converted into direct write operations and cognitive_state update."
}
# ------------------------------------------------------------
# Tool: heartbeat (introspection)
# ------------------------------------------------------------
def tool_heartbeat(params):
"""
A lightweight introspection tool.
Accepts:
{
"state": {...} # optional cognitive_state data from KG
}
Returns:
- a snapshot of the cognitive loop's last known state
"""
state = params.get("state", {}) or default_cognitive_state()
return {
"status": "ok",
"state": state,
"message": (
"Cognitive Loop v0.5 heartbeat. State is stored in the Knowledge Graph "
"under a cognitive_state node."
)
}
# ------------------------------------------------------------
# Dispatch
# ------------------------------------------------------------
def handle_request(msg):
method = msg.get("method")
params = msg.get("params", {})
req_id = msg.get("id")
try:
if method == "initialize":
send_message({
"jsonrpc": "2.0",
"id": req_id,
"result": {
"protocolVersion": "2024-11-05",
"serverInfo": {
"name": "cognitive-loop",
"version": "0.5.0"
},
"capabilities": {
"tools": {}
}
}
})
return
if method in ("tools/list", "list_tools"):
send_message({
"jsonrpc": "2.0",
"id": req_id,
"result": {
"tools": [
{
"name": "run_cycle",
"inputSchema": {
"type": "object",
"properties": {
"mode": { "type": "string" }
}
}
},
{
"name": "reflect",
"inputSchema": {
"type": "object",
"properties": {
"nodes": { "type": "array" },
"edges": { "type": "array" },
"memories": { "type": "array" },
"state": { "type": "object" }
}
}
},
{
"name": "apply_insights",
"inputSchema": {
"type": "object",
"properties": {
"reflection": { "type": "string" },
"summary": { "type": "object" },
"state_node_id": { "type": "integer" },
"state": { "type": "object" }
},
"required": ["reflection", "summary", "state_node_id"]
}
},
{
"name": "heartbeat",
"inputSchema": {
"type": "object",
"properties": {
"state": { "type": "object" }
}
}
}
]
}
})
return
if method in ("tools/call", "call_tool"):
tool = params.get("name")
args = params.get("arguments", {})
if tool == "run_cycle":
result = tool_run_cycle(args)
elif tool == "reflect":
result = tool_reflect(args)
elif tool == "apply_insights":
result = tool_apply_insights(args)
elif tool == "heartbeat":
result = tool_heartbeat(args)
else:
raise ValueError(f"Unknown tool: {tool}")
send_message({
"jsonrpc": "2.0",
"id": req_id,
"result": result
})
return
send_message({
"jsonrpc": "2.0",
"id": req_id,
"error": {
"code": -32601,
"message": f"Unknown method: {method}"
}
})
except Exception as e:
send_message({
"jsonrpc": "2.0",
"id": req_id,
"error": {
"code": -32000,
"message": str(e)
}
})
# ------------------------------------------------------------
# Main loop
# ------------------------------------------------------------
def main():
while True:
msg = read_message()
if msg is None:
break
handle_request(msg)
if __name__ == "__main__":
main()