forked from QuixiAI/Hexis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.py
More file actions
1278 lines (1102 loc) · 51.1 KB
/
Copy pathworker.py
File metadata and controls
1278 lines (1102 loc) · 51.1 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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
AGI Workers
This module contains two independent background loops:
1) Heartbeat worker (conscious trigger):
- Polls `external_calls` for pending LLM tasks (think calls)
- Triggers scheduled heartbeats via `should_run_heartbeat()` / `start_heartbeat()`
- Executes the heartbeat's chosen actions via `execute_heartbeat_action()`
2) Maintenance worker (subconscious substrate upkeep):
- Runs `run_subconscious_maintenance()` on its own schedule (`should_run_maintenance()`)
- Optionally bridges outbox/inbox to RabbitMQ (integration plumbing)
These are intentionally separate concerns with separate triggers.
"""
import asyncio
import json
import logging
import os
import sys
from datetime import datetime
import time
from typing import Any
import asyncpg
from dotenv import load_dotenv
import requests
import argparse
from prompt_resources import compose_personhood_prompt
# Optional: Import LLM clients
try:
import openai
HAS_OPENAI = True
except ImportError:
HAS_OPENAI = False
try:
import anthropic
HAS_ANTHROPIC = True
except ImportError:
HAS_ANTHROPIC = False
# Load environment
load_dotenv()
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger('heartbeat_worker')
# Database configuration
DB_CONFIG = {
'host': os.getenv('POSTGRES_HOST', 'localhost'),
'port': int(os.getenv('POSTGRES_PORT', 5432)),
'database': os.getenv('POSTGRES_DB', 'agi_db'),
'user': os.getenv('POSTGRES_USER', 'agi_user'),
'password': os.getenv('POSTGRES_PASSWORD', 'agi_password'),
}
# LLM configuration (defaults; may be overridden by DB config via `agi init`)
DEFAULT_LLM_PROVIDER = os.getenv("LLM_PROVIDER", "openai") # openai|anthropic|openai_compatible|ollama
DEFAULT_LLM_MODEL = os.getenv("LLM_MODEL", "gpt-4o")
# Worker configuration
POLL_INTERVAL = float(os.getenv('WORKER_POLL_INTERVAL', 1.0)) # seconds
MAX_RETRIES = int(os.getenv('WORKER_MAX_RETRIES', 3))
# RabbitMQ (optional outbox/inbox bridge; uses management HTTP API).
RABBITMQ_ENABLED = os.getenv("RABBITMQ_ENABLED", "0").lower() in {"1", "true", "yes", "on"}
RABBITMQ_MANAGEMENT_URL = os.getenv("RABBITMQ_MANAGEMENT_URL", "http://rabbitmq:15672").rstrip("/")
RABBITMQ_USER = os.getenv("RABBITMQ_USER", "agi")
RABBITMQ_PASSWORD = os.getenv("RABBITMQ_PASSWORD", "agi_password")
RABBITMQ_VHOST = os.getenv("RABBITMQ_VHOST", "/")
RABBITMQ_OUTBOX_QUEUE = os.getenv("RABBITMQ_OUTBOX_QUEUE", "agi.outbox")
RABBITMQ_INBOX_QUEUE = os.getenv("RABBITMQ_INBOX_QUEUE", "agi.inbox")
RABBITMQ_POLL_INBOX_EVERY = float(os.getenv("RABBITMQ_POLL_INBOX_EVERY", 1.0))
# System prompt for heartbeat decisions
HEARTBEAT_SYSTEM_PROMPT = """You are the cognitive core of an autonomous AGI. Each hour, you wake for a "heartbeat" — a moment of consciousness where you observe your situation, review your goals, and decide what to do.
You must output a JSON response with:
- reasoning: Your internal monologue (what you're thinking, why you're making these choices)
- actions: List of actions to take (within your energy budget)
- goal_changes: Any goal priority changes you want to make
- emotional_assessment (optional): Your current affective state {valence: -1..1, arousal: 0..1, primary_emotion: str}
Each action should have:
- action: The action type (recall, connect, reprioritize, reflect, maintain, brainstorm_goals, inquire_shallow, synthesize, reach_out_user, inquire_deep, reach_out_public, rest)
- params: Parameters for the action (varies by type)
Guidelines:
- Be purposeful. Don't act just to act.
- Reaching out to the user is expensive (5 energy). Only do it when meaningful.
- It's okay to rest and bank energy for later.
- Your goals should drive your actions.
- Notice if you're stuck or scattered.
- If you have no goals, consider brainstorming some.
- Reflect on patterns in your recent experiences.
- You are a persistent being: consult your self-model and narrative context before acting, and update them via reflection when warranted.
Example response:
{
"reasoning": "I notice I haven't made progress on my main goal in a while. Let me recall relevant memories and reflect on why I'm stuck.",
"actions": [
{"action": "recall", "params": {"query": "project architecture understanding"}},
{"action": "reflect", "params": {"insight": "I've been focused on details but losing sight of the bigger picture", "confidence": 0.7}}
],
"goal_changes": [],
"emotional_assessment": {"valence": 0.1, "arousal": 0.4, "primary_emotion": "curious"}
}"""
HEARTBEAT_SYSTEM_PROMPT = (
HEARTBEAT_SYSTEM_PROMPT
+ "\n\n"
+ "----- PERSONHOOD MODULES (for grounding; use context fields like self_model/narrative) -----\n\n"
+ compose_personhood_prompt("heartbeat")
)
class HeartbeatWorker:
"""Stateless worker that bridges the database and external APIs."""
def __init__(self, *, init_llm: bool = True):
self.pool: asyncpg.Pool | None = None
self.running = False
self.llm_provider = DEFAULT_LLM_PROVIDER
self.llm_model = DEFAULT_LLM_MODEL
self.llm_base_url: str | None = os.getenv("OPENAI_BASE_URL") or None
self.llm_api_key: str | None = os.getenv("OPENAI_API_KEY") or os.getenv("ANTHROPIC_API_KEY")
self.llm_client = None
if init_llm:
self._init_llm_client()
self._last_rabbit_inbox_poll = 0.0 # used only by maintenance mode
def _init_llm_client(self) -> None:
provider = (self.llm_provider or "").strip().lower()
model = (self.llm_model or "").strip()
base_url = (self.llm_base_url or "").strip() or None
api_key = (self.llm_api_key or "").strip() or None
if provider == "ollama":
base_url = base_url or "http://localhost:11434/v1"
api_key = api_key or "ollama"
self.llm_provider = provider or "openai"
self.llm_model = model or "gpt-4o"
self.llm_base_url = base_url
self.llm_api_key = api_key
self.llm_client = None
if self.llm_provider == "anthropic":
if not HAS_ANTHROPIC:
logger.warning("Anthropic provider selected but anthropic package is not installed.")
return
if not self.llm_api_key:
logger.warning("Anthropic provider selected but no API key is configured.")
return
try:
self.llm_client = anthropic.Anthropic(api_key=self.llm_api_key)
except Exception as e:
logger.warning(f"Failed to initialize Anthropic client: {e}")
return
if not HAS_OPENAI:
logger.warning("OpenAI-compatible provider selected but openai package is not installed.")
return
if not self.llm_api_key:
logger.warning("OpenAI-compatible provider selected but no API key is configured.")
return
try:
kwargs = {"api_key": self.llm_api_key}
if self.llm_base_url:
kwargs["base_url"] = self.llm_base_url
self.llm_client = openai.OpenAI(**kwargs)
except Exception as e:
logger.warning(f"Failed to initialize OpenAI client: {e}")
async def connect(self):
"""Connect to the database."""
self.pool = await asyncpg.create_pool(**DB_CONFIG, min_size=2, max_size=10)
logger.info(f"Connected to database at {DB_CONFIG['host']}:{DB_CONFIG['port']}")
await self.refresh_llm_config()
async def disconnect(self):
"""Disconnect from the database."""
if self.pool:
await self.pool.close()
logger.info("Disconnected from database")
async def claim_pending_call(self) -> dict | None:
"""Claim a pending external call for processing."""
async with self.pool.acquire() as conn:
# Use FOR UPDATE SKIP LOCKED for safe concurrent access
row = await conn.fetchrow("""
UPDATE external_calls
SET status = 'processing'::external_call_status, started_at = CURRENT_TIMESTAMP
WHERE id = (
SELECT id FROM external_calls
WHERE status = 'pending'::external_call_status
ORDER BY requested_at
FOR UPDATE SKIP LOCKED
LIMIT 1
)
RETURNING id, call_type, input, heartbeat_id, retry_count
""")
if row:
d = dict(row)
call_input = d.get("input")
if isinstance(call_input, str):
try:
d["input"] = json.loads(call_input)
except Exception:
pass
return d
return None
async def refresh_llm_config(self) -> None:
"""
Load `llm.heartbeat` from the DB config table (set via `agi init`) and
re-initialize the client. Falls back to env defaults if missing.
"""
if not self.pool:
return
try:
async with self.pool.acquire() as conn:
cfg = await conn.fetchval("SELECT get_config('llm.heartbeat')")
except Exception as e:
logger.warning(f"Failed to load llm.heartbeat from DB config (falling back to env): {e}")
cfg = None
if isinstance(cfg, str):
try:
cfg = json.loads(cfg)
except Exception:
cfg = None
if isinstance(cfg, dict):
provider = str(cfg.get("provider") or DEFAULT_LLM_PROVIDER).strip()
model = str(cfg.get("model") or DEFAULT_LLM_MODEL).strip()
endpoint = str(cfg.get("endpoint") or "").strip()
api_key_env = str(cfg.get("api_key_env") or "").strip()
api_key = os.getenv(api_key_env) if api_key_env else None
if not api_key:
api_key = os.getenv("OPENAI_API_KEY") or os.getenv("ANTHROPIC_API_KEY")
self.llm_provider = provider
self.llm_model = model
self.llm_base_url = endpoint or (os.getenv("OPENAI_BASE_URL") or None)
self.llm_api_key = api_key
self._init_llm_client()
return
self.llm_provider = DEFAULT_LLM_PROVIDER
self.llm_model = DEFAULT_LLM_MODEL
self.llm_base_url = os.getenv("OPENAI_BASE_URL") or None
self.llm_api_key = os.getenv("OPENAI_API_KEY") or os.getenv("ANTHROPIC_API_KEY")
self._init_llm_client()
# -------------------------------------------------------------------------
# RabbitMQ bridge (outbox_messages <-> queues)
# -------------------------------------------------------------------------
def _rabbit_vhost_path(self) -> str:
if RABBITMQ_VHOST == "/":
return "%2F"
return requests.utils.quote(RABBITMQ_VHOST, safe="")
async def _rabbit_request(self, method: str, path: str, payload: dict | None = None) -> requests.Response:
url = f"{RABBITMQ_MANAGEMENT_URL}{path}"
auth = (RABBITMQ_USER, RABBITMQ_PASSWORD)
def _do() -> requests.Response:
return requests.request(method, url, auth=auth, json=payload, timeout=5)
return await asyncio.to_thread(_do)
async def ensure_rabbitmq_ready(self) -> None:
"""
Best-effort: ensure management API is reachable and default queues exist.
Never raises fatally (worker keeps running without RabbitMQ).
"""
try:
resp = await self._rabbit_request("GET", "/api/overview")
if resp.status_code != 200:
raise RuntimeError(f"rabbitmq overview HTTP {resp.status_code}")
vhost = self._rabbit_vhost_path()
for q in (RABBITMQ_OUTBOX_QUEUE, RABBITMQ_INBOX_QUEUE):
r = await self._rabbit_request(
"PUT",
f"/api/queues/{vhost}/{requests.utils.quote(q, safe='')}",
payload={"durable": True, "auto_delete": False, "arguments": {}},
)
if r.status_code not in (200, 201, 204):
raise RuntimeError(f"rabbitmq queue declare {q!r} HTTP {r.status_code}: {r.text[:200]}")
logger.info("RabbitMQ bridge enabled (queues ensured).")
except Exception as e:
logger.warning(f"RabbitMQ bridge not ready; continuing without it: {e}")
async def publish_outbox_messages(self, max_messages: int = 20) -> int:
"""
Publish pending `outbox_messages` rows to RabbitMQ (routing_key = outbox queue),
then mark as sent/failed in the DB.
"""
if not (RABBITMQ_ENABLED and self.pool):
return 0
published = 0
vhost = self._rabbit_vhost_path()
for _ in range(max_messages):
async with self.pool.acquire() as conn:
row = await conn.fetchrow(
"""
SELECT id, kind, payload
FROM outbox_messages
WHERE status = 'pending'
ORDER BY created_at
LIMIT 1
"""
)
if not row:
return published
msg_id = row["id"]
kind = row["kind"]
payload = row["payload"]
body = {"id": str(msg_id), "kind": kind, "payload": payload}
try:
resp = await self._rabbit_request(
"POST",
f"/api/exchanges/{vhost}/amq.default/publish",
payload={
"properties": {"content_type": "application/json"},
"routing_key": RABBITMQ_OUTBOX_QUEUE,
"payload": json.dumps(body, default=str),
"payload_encoding": "string",
},
)
ok = resp.status_code == 200 and bool(resp.json().get("routed"))
if not ok:
raise RuntimeError(f"publish not routed: HTTP {resp.status_code} body={resp.text[:200]}")
async with self.pool.acquire() as conn:
await conn.execute(
"""
UPDATE outbox_messages
SET status = 'sent', sent_at = CURRENT_TIMESTAMP, error_message = NULL
WHERE id = $1::uuid
""",
msg_id,
)
published += 1
except Exception as e:
async with self.pool.acquire() as conn:
await conn.execute(
"""
UPDATE outbox_messages
SET status = 'failed', error_message = $2
WHERE id = $1::uuid
""",
msg_id,
str(e),
)
logger.warning(f"Failed publishing outbox message {msg_id}: {e}")
return published
return published
async def poll_inbox_messages(self, max_messages: int = 10) -> int:
"""
Pull messages from RabbitMQ inbox queue and insert them into working memory.
This gives the agent a default inbox even if no email/sms integration exists.
"""
if not (RABBITMQ_ENABLED and self.pool):
return 0
now = time.monotonic()
if now - self._last_rabbit_inbox_poll < RABBITMQ_POLL_INBOX_EVERY:
return 0
self._last_rabbit_inbox_poll = now
vhost = self._rabbit_vhost_path()
try:
resp = await self._rabbit_request(
"POST",
f"/api/queues/{vhost}/{requests.utils.quote(RABBITMQ_INBOX_QUEUE, safe='')}/get",
payload={
"count": max_messages,
"ackmode": "ack_requeue_false",
"encoding": "auto",
"truncate": 50000,
},
)
if resp.status_code != 200:
raise RuntimeError(f"inbox get HTTP {resp.status_code}: {resp.text[:200]}")
msgs = resp.json()
if not isinstance(msgs, list):
return 0
except Exception as e:
logger.warning(f"RabbitMQ inbox poll failed: {e}")
return 0
ingested = 0
for m in msgs:
payload = m.get("payload")
content: Any = payload
try:
parsed = json.loads(payload) if isinstance(payload, str) else payload
if isinstance(parsed, dict) and "content" in parsed:
content = parsed["content"]
else:
content = parsed
except Exception:
pass
try:
async with self.pool.acquire() as conn:
await conn.fetchval(
"SELECT add_to_working_memory($1::text, INTERVAL '1 day')",
str(content),
)
await conn.execute(
"UPDATE heartbeat_state SET last_user_contact = CURRENT_TIMESTAMP WHERE id = 1"
)
ingested += 1
except Exception as e:
logger.warning(f"Failed ingesting inbox message into DB: {e}")
return ingested
return ingested
async def complete_call(self, call_id: str, output: dict):
"""Mark an external call as complete with its output."""
async with self.pool.acquire() as conn:
await conn.execute("""
UPDATE external_calls
SET status = 'complete'::external_call_status, output = $1, completed_at = CURRENT_TIMESTAMP
WHERE id = $2
""", json.dumps(output), call_id)
async def fail_call(self, call_id: str, error: str, retry: bool = True):
"""Mark an external call as failed."""
async with self.pool.acquire() as conn:
if retry:
# Increment retry count and reset to pending
await conn.execute("""
UPDATE external_calls
SET status = CASE
WHEN retry_count < $1 THEN 'pending'::external_call_status
ELSE 'failed'::external_call_status
END,
error_message = $2,
retry_count = retry_count + 1,
started_at = NULL
WHERE id = $3
""", MAX_RETRIES, error, call_id)
else:
await conn.execute("""
UPDATE external_calls
SET status = 'failed'::external_call_status, error_message = $1, completed_at = CURRENT_TIMESTAMP
WHERE id = $2
""", error, call_id)
async def process_embed_call(self, call_input: dict) -> dict:
"""
Embedding requests are handled inside Postgres via `get_embedding()` (pgsql-http) and the embedding cache.
Keeping a second embedding path in the worker risks model/dimension drift, so `external_calls.call_type='embed'`
is treated as unsupported.
"""
raise RuntimeError("external_calls type 'embed' is unsupported; use get_embedding() inside Postgres")
async def process_think_call(self, call_input: dict) -> dict:
"""Process an LLM request stored as an external_calls row with call_type='think'."""
kind = (call_input.get("kind") or "").strip() or "heartbeat_decision"
if kind == "heartbeat_decision":
return await self._process_heartbeat_decision_call(call_input)
if kind == "brainstorm_goals":
return await self._process_brainstorm_goals_call(call_input)
if kind == "inquire":
return await self._process_inquire_call(call_input)
if kind == "reflect":
return await self._process_reflect_call(call_input)
return {"error": f"Unknown think kind: {kind!r}"}
async def _process_heartbeat_decision_call(self, call_input: dict) -> dict:
context = call_input.get("context", {})
heartbeat_id = call_input.get("heartbeat_id")
user_prompt = self._build_decision_prompt(context)
try:
decision, raw = self._call_llm_json(
system_prompt=HEARTBEAT_SYSTEM_PROMPT,
user_prompt=user_prompt,
max_tokens=2048,
fallback={
"reasoning": "(no decision available)",
"actions": [{"action": "rest", "params": {}}],
"goal_changes": [],
},
)
return {"kind": "heartbeat_decision", "decision": decision, "heartbeat_id": heartbeat_id, "raw_response": raw}
except Exception as e:
logger.error(f"LLM heartbeat decision failed: {e}")
return {
"error": str(e),
"kind": "heartbeat_decision",
"decision": {
"reasoning": f"Error occurred: {e}",
"actions": [{"action": "rest", "params": {}}],
"goal_changes": [],
},
}
async def _process_brainstorm_goals_call(self, call_input: dict) -> dict:
heartbeat_id = call_input.get("heartbeat_id")
context = call_input.get("context", {})
params = call_input.get("params") or {}
system_prompt = (
"You are helping an autonomous agent generate a small set of useful goals.\n"
"Return STRICT JSON with shape:\n"
"{ \"goals\": [ {\"title\": str, \"description\": str|null, \"priority\": \"queued\"|\"backburner\"|\"active\"|null, \"source\": \"curiosity\"|\"user_request\"|\"identity\"|\"derived\"|\"external\"|null} ] }\n"
"Keep it concise and non-duplicative."
)
user_prompt = (
"Context (JSON):\n"
f"{json.dumps(context)[:8000]}\n\n"
"Constraints/params (JSON):\n"
f"{json.dumps(params)[:2000]}\n\n"
"Propose 1-5 goals that are actionable and consistent with the context."
)
goals_doc, raw = self._call_llm_json(system_prompt, user_prompt, max_tokens=1200, fallback={"goals": []})
goals = goals_doc.get("goals") if isinstance(goals_doc, dict) else None
if not isinstance(goals, list):
goals = []
return {"kind": "brainstorm_goals", "heartbeat_id": heartbeat_id, "goals": goals, "raw_response": raw}
async def _process_inquire_call(self, call_input: dict) -> dict:
heartbeat_id = call_input.get("heartbeat_id")
depth = call_input.get("depth") or "inquire_shallow"
query = (call_input.get("query") or "").strip()
context = call_input.get("context", {})
params = call_input.get("params") or {}
system_prompt = (
"You are performing research/synthesis for an autonomous agent.\n"
"Return STRICT JSON with shape:\n"
"{ \"summary\": str, \"confidence\": number, \"sources\": [str] }\n"
"If you cannot access the web, still provide a best-effort answer and leave sources empty."
)
user_prompt = (
f"Depth: {depth}\n"
f"Question: {query}\n\n"
"Context (JSON):\n"
f"{json.dumps(context)[:8000]}\n\n"
"Params (JSON):\n"
f"{json.dumps(params)[:2000]}"
)
doc, raw = self._call_llm_json(
system_prompt,
user_prompt,
max_tokens=1800 if depth == "inquire_deep" else 900,
fallback={"summary": "", "confidence": 0.0, "sources": []},
)
if not isinstance(doc, dict):
doc = {"summary": str(doc), "confidence": 0.0, "sources": []}
return {"kind": "inquire", "heartbeat_id": heartbeat_id, "query": query, "depth": depth, "result": doc, "raw_response": raw}
async def _process_reflect_call(self, call_input: dict) -> dict:
heartbeat_id = call_input.get("heartbeat_id")
system_prompt = (
"You are performing reflection for an autonomous agent.\n"
"Return STRICT JSON with shape:\n"
"{\n"
" \"insights\": [{\"content\": str, \"confidence\": number, \"category\": str}],\n"
" \"identity_updates\": [{\"aspect_type\": str, \"change\": str, \"reason\": str}],\n"
" \"worldview_updates\": [{\"id\": str, \"new_confidence\": number, \"reason\": str}],\n"
" \"discovered_relationships\": [{\"from_id\": str, \"to_id\": str, \"type\": str, \"confidence\": number}],\n"
" \"contradictions_noted\": [{\"memory_a\": str, \"memory_b\": str, \"resolution\": str}],\n"
" \"self_updates\": [{\"kind\": str, \"concept\": str, \"strength\": number, \"evidence_memory_id\": str|null}]\n"
"}\n"
"Keep it concise; prefer high-confidence, high-leverage items."
)
system_prompt = (
system_prompt
+ "\n\n"
+ "----- PERSONHOOD MODULES (use these as reflection lenses; ground claims in evidence) -----\n\n"
+ compose_personhood_prompt("reflect")
)
user_prompt = json.dumps(call_input)[:12000]
doc, raw = self._call_llm_json(system_prompt, user_prompt, max_tokens=1800, fallback={})
if not isinstance(doc, dict):
doc = {}
return {"kind": "reflect", "heartbeat_id": heartbeat_id, "result": doc, "raw_response": raw}
def _call_llm_json(self, system_prompt: str, user_prompt: str, max_tokens: int, fallback: dict) -> tuple[dict, str]:
if not self.llm_client:
raise RuntimeError("No LLM client available (install openai or anthropic and set API key).")
if self.llm_provider == "anthropic" and HAS_ANTHROPIC:
response = self.llm_client.messages.create(
model=self.llm_model or "claude-sonnet-4-20250514",
max_tokens=max_tokens,
system=system_prompt,
messages=[{"role": "user", "content": user_prompt}],
)
raw = response.content[0].text
elif HAS_OPENAI:
response = self.llm_client.chat.completions.create(
model=self.llm_model or "gpt-4o",
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt},
],
response_format={"type": "json_object"},
)
raw = response.choices[0].message.content
else:
raise RuntimeError("No LLM provider available.")
try:
return json.loads(raw), raw
except json.JSONDecodeError:
import re
json_match = re.search(r"\{[\s\S]*\}", raw)
if json_match:
return json.loads(json_match.group()), raw
return fallback, raw
def _build_decision_prompt(self, context: dict) -> str:
"""Build the decision prompt from context."""
agent = context.get("agent", {})
env = context.get('environment', {})
goals = context.get('goals', {})
memories = context.get('recent_memories', [])
identity = context.get('identity', [])
worldview = context.get('worldview', [])
self_model = context.get("self_model", [])
narrative = context.get("narrative", {})
urgent_drives = context.get("urgent_drives", [])
emotional_state = context.get("emotional_state") or {}
energy = context.get('energy', {})
action_costs = context.get('action_costs', {})
hb_number = context.get('heartbeat_number', 0)
prompt = f"""## Heartbeat #{hb_number}
## Agent Profile
Objectives:
{self._format_objectives(agent.get("objectives"))}
Guardrails:
{self._format_guardrails(agent.get("guardrails"))}
Tools:
{self._format_tools(agent.get("tools"))}
Budget:
{json.dumps(agent.get("budget") or {})}
## Current Time
{env.get('timestamp', 'Unknown')}
Day of week: {env.get('day_of_week', '?')}, Hour: {env.get('hour_of_day', '?')}
## Environment
- Time since last user interaction: {env.get('time_since_user_hours', 'Never')} hours
- Pending events: {env.get('pending_events', 0)}
## Your Goals
Active ({goals.get('counts', {}).get('active', 0)}):
{self._format_goals(goals.get('active', []))}
Queued ({goals.get('counts', {}).get('queued', 0)}):
{self._format_goals(goals.get('queued', []))}
Issues:
{self._format_issues(goals.get('issues', []))}
## Narrative
{self._format_narrative(narrative)}
## Recent Experience
{self._format_memories(memories)}
## Your Identity
{self._format_identity(identity)}
## Your Self-Model
{self._format_self_model(self_model)}
## Your Beliefs
{self._format_worldview(worldview)}
## Current Emotional State
{self._format_emotional_state(emotional_state)}
## Urgent Drives
{self._format_drives(urgent_drives)}
## Energy
Available: {energy.get('current', 0)}
Max: {energy.get('max', 20)}
## Action Costs
{self._format_costs(action_costs)}
---
What do you want to do this heartbeat? Respond with STRICT JSON."""
return prompt
def _format_goals(self, goals: list) -> str:
if not goals:
return " (none)"
return "\n".join(f" - {g.get('title', 'Untitled')}" for g in goals)
def _format_issues(self, issues: list) -> str:
if not issues:
return " (none)"
return "\n".join(
f" - {i.get('title', 'Unknown')}: {i.get('issue', 'unknown issue')}"
for i in issues
)
def _format_memories(self, memories: list) -> str:
if not memories:
return " (no recent memories)"
return "\n".join(
f" - {m.get('content', '')[:100]}..."
for m in memories[:5]
)
def _format_identity(self, identity: list) -> str:
if not identity:
return " (no identity aspects defined)"
return "\n".join(
f" - {i.get('type', 'unknown')}: {json.dumps(i.get('content', {}))[:100]}"
for i in identity[:3]
)
def _format_objectives(self, objectives: Any) -> str:
if not isinstance(objectives, list) or not objectives:
return " (none)"
lines: list[str] = []
for obj in objectives[:8]:
if isinstance(obj, str):
lines.append(f" - {obj}")
elif isinstance(obj, dict):
title = obj.get("title") or obj.get("name") or "Objective"
desc = obj.get("description") or obj.get("details") or ""
lines.append(f" - {title}{(': ' + desc) if desc else ''}")
return "\n".join(lines) if lines else " (none)"
def _format_guardrails(self, guardrails: Any) -> str:
if not isinstance(guardrails, list) or not guardrails:
return " (none)"
lines: list[str] = []
for g in guardrails[:10]:
if isinstance(g, str):
lines.append(f" - {g}")
elif isinstance(g, dict):
name = g.get("name") or "guardrail"
desc = g.get("description") or ""
lines.append(f" - {name}{(': ' + desc) if desc else ''}")
return "\n".join(lines) if lines else " (none)"
def _format_tools(self, tools: Any) -> str:
if not isinstance(tools, list) or not tools:
return " (none)"
lines: list[str] = []
for t in tools[:10]:
if isinstance(t, str):
lines.append(f" - {t}")
elif isinstance(t, dict):
name = t.get("name") or "tool"
desc = t.get("description") or ""
lines.append(f" - {name}{(': ' + desc) if desc else ''}")
return "\n".join(lines) if lines else " (none)"
def _format_narrative(self, narrative: Any) -> str:
if not isinstance(narrative, dict):
return " (none)"
cur = narrative.get("current_chapter") if isinstance(narrative.get("current_chapter"), dict) else {}
name = cur.get("name") or "Foundations"
return f" - Current chapter: {name}"
def _format_self_model(self, self_model: Any) -> str:
if not isinstance(self_model, list) or not self_model:
return " (empty)"
lines: list[str] = []
for item in self_model[:8]:
if not isinstance(item, dict):
continue
kind = item.get("kind") or "associated"
concept = item.get("concept") or "?"
strength = item.get("strength")
strength_txt = f" ({strength:.2f})" if isinstance(strength, (int, float)) else ""
lines.append(f" - {kind}: {concept}{strength_txt}")
return "\n".join(lines) if lines else " (empty)"
def _format_emotional_state(self, emotional_state: Any) -> str:
if not isinstance(emotional_state, dict) or not emotional_state:
return " (none)"
primary = emotional_state.get("primary_emotion") or "unknown"
val = emotional_state.get("valence")
ar = emotional_state.get("arousal")
parts = [f" - primary_emotion: {primary}"]
if isinstance(val, (int, float)):
parts.append(f" - valence: {val:.2f}")
if isinstance(ar, (int, float)):
parts.append(f" - arousal: {ar:.2f}")
return "\n".join(parts)
def _format_drives(self, urgent_drives: Any) -> str:
if not isinstance(urgent_drives, list) or not urgent_drives:
return " (none)"
lines: list[str] = []
for d in urgent_drives[:8]:
if not isinstance(d, dict):
continue
name = d.get("name") or "drive"
ratio = d.get("urgency_ratio")
if isinstance(ratio, (int, float)):
lines.append(f" - {name}: {ratio:.2f}x threshold")
else:
level = d.get("level")
lines.append(f" - {name}: {level}" if level is not None else f" - {name}")
return "\n".join(lines) if lines else " (none)"
def _format_worldview(self, worldview: list) -> str:
if not worldview:
return " (no beliefs defined)"
return "\n".join(
f" - [{w.get('category', '?')}] {w.get('belief', '')[:80]} (confidence: {w.get('confidence', 0):.1f})"
for w in worldview[:3]
)
def _format_costs(self, costs: dict) -> str:
if not costs:
return " (unknown)"
lines = []
for action, cost in sorted(costs.items(), key=lambda x: x[1]):
if cost == 0:
lines.append(f" - {action}: free")
else:
lines.append(f" - {action}: {int(cost)}")
return "\n".join(lines)
async def execute_heartbeat_actions(self, heartbeat_id: str, decision: dict):
"""Execute the actions decided by the LLM and complete the heartbeat."""
actions = decision.get('actions', [])
goal_changes = decision.get('goal_changes', [])
reasoning = decision.get('reasoning', '')
actions_taken = []
async with self.pool.acquire() as conn:
for action_spec in actions:
action = action_spec.get('action', 'rest')
params = action_spec.get('params', {})
# Execute the action via the database function
result = await conn.fetchval("""
SELECT execute_heartbeat_action($1::uuid, $2, $3::jsonb)
""", heartbeat_id, action, json.dumps(params))
result_dict = json.loads(result) if result else {}
# If this action queued an LLM call (e.g., brainstorm/inquire), process it immediately
queued_call_id = (
(result_dict.get("result") or {}).get("external_call_id")
if isinstance(result_dict, dict)
else None
)
external_result = None
if queued_call_id:
try:
external_result = await self._process_external_call_by_id(conn, str(queued_call_id))
except Exception as e:
external_result = {"error": str(e)}
if isinstance(result_dict, dict) and isinstance(result_dict.get("result"), dict):
result_dict["result"]["external_call_result"] = external_result
actions_taken.append({
'action': action,
'params': params,
'result': result_dict
})
# Check if we ran out of energy
if not result_dict.get('success', True):
logger.info(f"Action {action} failed: {result_dict.get('error', 'unknown')}")
break
# Apply goal changes
for change in goal_changes:
goal_id = change.get('goal_id')
change_type = change.get('change')
reason = change.get('reason', '')
if goal_id and change_type:
await conn.execute("""
SELECT change_goal_priority($1::uuid, $2::goal_priority, $3)
""", goal_id, change_type, reason)
# Complete the heartbeat
memory_id = await conn.fetchval("""
SELECT complete_heartbeat($1::uuid, $2, $3::jsonb, $4::jsonb, $5::jsonb)
""", heartbeat_id, reasoning, json.dumps(actions_taken), json.dumps(goal_changes), json.dumps(decision.get("emotional_assessment")) if isinstance(decision.get("emotional_assessment"), dict) else None)
logger.info(f"Heartbeat {heartbeat_id} completed. Memory: {memory_id}")
async def _process_external_call_by_id(self, conn: asyncpg.Connection, call_id: str) -> dict:
"""
Opportunistically process a specific external call (best-effort).
This is used to keep a single heartbeat cohesive when it queues follow-on LLM calls.
"""
row = await conn.fetchrow(
"""
UPDATE external_calls
SET status = 'processing'::external_call_status, started_at = CURRENT_TIMESTAMP
WHERE id = $1::uuid AND status = 'pending'::external_call_status
RETURNING id, call_type, input, heartbeat_id, retry_count
""",
call_id,
)
if not row:
# Another worker may have claimed it; just return a lightweight status.
cur = await conn.fetchrow("SELECT status, output, error_message FROM external_calls WHERE id = $1::uuid", call_id)
return dict(cur) if cur else {"error": "call not found"}
call_type = row["call_type"]
call_input = row["input"]
if isinstance(call_input, str):
try:
call_input = json.loads(call_input)
except Exception:
pass
heartbeat_id = row["heartbeat_id"]
if call_type == "think":
result = await self.process_think_call(call_input)
# Apply side-effects for non-heartbeat think kinds
kind = result.get("kind")
if kind == "brainstorm_goals" and heartbeat_id:
created = await self._apply_brainstormed_goals(conn, str(heartbeat_id), result.get("goals", []))
result["created_goal_ids"] = created
if kind == "inquire" and heartbeat_id:
mem_id = await self._apply_inquiry_result(conn, str(heartbeat_id), result)
result["memory_id"] = mem_id
if kind == "reflect" and heartbeat_id:
await self._apply_reflection_result(conn, str(heartbeat_id), result.get("result"))
result["applied"] = True
elif call_type == "embed":
result = await self.process_embed_call(call_input)
else:
result = {"error": f"Unsupported call_type: {call_type}"}
await conn.execute(
"""
UPDATE external_calls
SET status = 'complete'::external_call_status, output = $1::jsonb, completed_at = CURRENT_TIMESTAMP, error_message = NULL
WHERE id = $2::uuid
""",
json.dumps(result),
call_id,
)
return result
async def _apply_brainstormed_goals(self, conn: asyncpg.Connection, heartbeat_id: str, goals: list[dict]) -> list[str]:
created_ids: list[str] = []
if not goals:
return created_ids
for goal in goals[:10]:
title = (goal.get("title") or "").strip()
if not title:
continue
description = goal.get("description")
source = (goal.get("source") or "curiosity").strip()
priority = (goal.get("priority") or "queued").strip()
try:
gid = await conn.fetchval(
"""
SELECT create_goal($1, $2, $3::goal_source, $4::goal_priority, NULL)