nexo-brain 5.3.20 → 5.3.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (210) hide show
  1. package/.claude-plugin/plugin.json +1 -1
  2. package/package.json +1 -1
  3. package/src/auto_update.py +11 -8
  4. package/src/dashboard/static/favicon 2.svg +32 -0
  5. package/src/dashboard/static/nexo-logo 2.png +0 -0
  6. package/src/dashboard/static/nexo-logo 2.svg +40 -0
  7. package/src/dashboard/static/style 2.css +2458 -0
  8. package/src/dashboard/templates/adaptive 2.html +118 -0
  9. package/src/dashboard/templates/artifacts 2.html +133 -0
  10. package/src/dashboard/templates/backups 2.html +136 -0
  11. package/src/dashboard/templates/base 2.html +417 -0
  12. package/src/dashboard/templates/calendar 2.html +591 -0
  13. package/src/dashboard/templates/chat 2.html +356 -0
  14. package/src/dashboard/templates/claims 2.html +259 -0
  15. package/src/dashboard/templates/cortex 2.html +321 -0
  16. package/src/dashboard/templates/credentials 2.html +128 -0
  17. package/src/dashboard/templates/crons 2.html +370 -0
  18. package/src/dashboard/templates/dashboard 2.html +494 -0
  19. package/src/dashboard/templates/dreams 2.html +252 -0
  20. package/src/dashboard/templates/email 2.html +160 -0
  21. package/src/dashboard/templates/evolution 2.html +189 -0
  22. package/src/dashboard/templates/feed 2.html +249 -0
  23. package/src/dashboard/templates/followup_health 2.html +170 -0
  24. package/src/dashboard/templates/graph 2.html +201 -0
  25. package/src/dashboard/templates/guard 2.html +259 -0
  26. package/src/dashboard/templates/inbox 2.html +251 -0
  27. package/src/dashboard/templates/memory 2.html +420 -0
  28. package/src/dashboard/templates/operations 2.html +608 -0
  29. package/src/dashboard/templates/plugins 2.html +185 -0
  30. package/src/dashboard/templates/protocol 2.html +199 -0
  31. package/src/dashboard/templates/rules 2.html +246 -0
  32. package/src/dashboard/templates/sentiment 2.html +247 -0
  33. package/src/dashboard/templates/sessions 2.html +218 -0
  34. package/src/dashboard/templates/skills 2.html +329 -0
  35. package/src/dashboard/templates/somatic 2.html +73 -0
  36. package/src/dashboard/templates/triggers 2.html +133 -0
  37. package/src/dashboard/templates/trust 2.html +360 -0
  38. package/src/db/__init__ 2.py +259 -0
  39. package/src/db/_core 2.py +437 -0
  40. package/src/db/_credentials 2.py +124 -0
  41. package/src/db/_episodic 2.py +762 -0
  42. package/src/db/_evolution 2.py +54 -0
  43. package/src/db/_fts 2.py +406 -0
  44. package/src/db/_goal_profiles 2.py +376 -0
  45. package/src/db/_hot_context 2.py +660 -0
  46. package/src/db/_outcomes 2.py +800 -0
  47. package/src/db/_personal_scripts 2.py +582 -0
  48. package/src/db/_sessions 2.py +330 -0
  49. package/src/db/_tasks 2.py +91 -0
  50. package/src/db/_watchers 2.py +173 -0
  51. package/src/doctor/formatters 2.py +52 -0
  52. package/src/doctor/models 2.py +69 -0
  53. package/src/doctor/planes 2.py +87 -0
  54. package/src/doctor/providers/__init__ 2.py +1 -0
  55. package/src/doctor/providers/deep 2.py +367 -0
  56. package/src/evolution_cycle 2.py +519 -0
  57. package/src/hooks/auto_capture 2.py +208 -0
  58. package/src/hooks/caffeinate-guard 2.sh +8 -0
  59. package/src/hooks/capture-session 2.sh +21 -0
  60. package/src/hooks/capture-tool-logs 2.sh +158 -0
  61. package/src/hooks/daily-briefing-check 2.sh +33 -0
  62. package/src/hooks/heartbeat-enforcement 2.py +90 -0
  63. package/src/hooks/heartbeat-posttool 2.sh +18 -0
  64. package/src/hooks/inbox-hook 2.sh +76 -0
  65. package/src/hooks/post-compact 2.sh +152 -0
  66. package/src/hooks/pre-compact 2.sh +169 -0
  67. package/src/hooks/protocol-guardrail 2.sh +10 -0
  68. package/src/hooks/protocol-pretool-guardrail 2.sh +9 -0
  69. package/src/hooks/session-stop 2.sh +52 -0
  70. package/src/kg_populate 2.py +292 -0
  71. package/src/maintenance 2.py +53 -0
  72. package/src/memory_backends 2.py +71 -0
  73. package/src/migrate_embeddings 2.py +124 -0
  74. package/src/nexo_sdk 2.py +103 -0
  75. package/src/observability 2.py +199 -0
  76. package/src/plugin_loader 2.py +217 -0
  77. package/src/plugins/__init__ 2.py +0 -0
  78. package/src/plugins/artifact_registry 2.py +450 -0
  79. package/src/plugins/backup 2.py +127 -0
  80. package/src/plugins/claims_tools 2.py +119 -0
  81. package/src/plugins/cognitive_memory 2.py +609 -0
  82. package/src/plugins/core_rules 2.py +252 -0
  83. package/src/plugins/cortex 2.py +1155 -0
  84. package/src/plugins/entities 2.py +67 -0
  85. package/src/plugins/episodic_memory 2.py +560 -0
  86. package/src/plugins/evolution 2.py +167 -0
  87. package/src/plugins/goal_engine 2.py +142 -0
  88. package/src/plugins/guard 2.py +862 -0
  89. package/src/plugins/impact 2.py +29 -0
  90. package/src/plugins/knowledge_graph_tools 2.py +137 -0
  91. package/src/plugins/media_memory_tools 2.py +98 -0
  92. package/src/plugins/memory_export 2.py +196 -0
  93. package/src/plugins/outcomes 2.py +130 -0
  94. package/src/plugins/personal_scripts 2.py +117 -0
  95. package/src/plugins/preferences 2.py +47 -0
  96. package/src/plugins/protocol 2.py +1449 -0
  97. package/src/plugins/simple_api 2.py +106 -0
  98. package/src/plugins/skills 2.py +341 -0
  99. package/src/plugins/state_watchers 2.py +79 -0
  100. package/src/plugins/update 2.py +986 -0
  101. package/src/plugins/user_state_tools 2.py +43 -0
  102. package/src/plugins/workflow 2.py +588 -0
  103. package/src/protocol_settings 2.py +59 -0
  104. package/src/public_contribution 2.py +466 -0
  105. package/src/public_evolution_queue 2.py +241 -0
  106. package/src/requirements 2.txt +14 -0
  107. package/src/retroactive_learnings 2.py +373 -0
  108. package/src/rules/__init__ 2.py +0 -0
  109. package/src/rules/core-rules 2.json +331 -0
  110. package/src/rules/migrate 2.py +207 -0
  111. package/src/runtime_power 2.py +874 -0
  112. package/src/script_registry 2.py +1559 -0
  113. package/src/scripts/check-context 2.py +272 -0
  114. package/src/scripts/deep-sleep/apply_findings 2.py +2327 -0
  115. package/src/scripts/deep-sleep/collect 2.py +928 -0
  116. package/src/scripts/deep-sleep/extract 2.py +330 -0
  117. package/src/scripts/deep-sleep/extract-prompt 2.md +285 -0
  118. package/src/scripts/deep-sleep/synthesize 2.py +312 -0
  119. package/src/scripts/deep-sleep/synthesize-prompt 2.md +336 -0
  120. package/src/scripts/nexo-agent-run 2.py +75 -0
  121. package/src/scripts/nexo-auto-update 2.py +6 -0
  122. package/src/scripts/nexo-backup 2.sh +25 -0
  123. package/src/scripts/nexo-brain-activation 2.sh +140 -0
  124. package/src/scripts/nexo-catchup 2.py +300 -0
  125. package/src/scripts/nexo-cognitive-decay 2.py +257 -0
  126. package/src/scripts/nexo-cortex-cycle 2.py +293 -0
  127. package/src/scripts/nexo-cron-wrapper 2.sh +53 -0
  128. package/src/scripts/nexo-daily-self-audit 2.py +2161 -0
  129. package/src/scripts/nexo-dashboard 2.sh +29 -0
  130. package/src/scripts/nexo-deep-sleep 2.sh +86 -0
  131. package/src/scripts/nexo-evolution-run 2.py +1664 -0
  132. package/src/scripts/nexo-followup-hygiene 2.py +139 -0
  133. package/src/scripts/nexo-hook-record 2.py +42 -0
  134. package/src/scripts/nexo-immune 2.py +936 -0
  135. package/src/scripts/nexo-impact-scorer 2.py +117 -0
  136. package/src/scripts/nexo-inbox-hook 2.sh +74 -0
  137. package/src/scripts/nexo-install 2.py +6 -0
  138. package/src/scripts/nexo-learning-housekeep 2.py +401 -0
  139. package/src/scripts/nexo-learning-validator 2.py +266 -0
  140. package/src/scripts/nexo-migrate 2.py +260 -0
  141. package/src/scripts/nexo-outcome-checker 2.py +127 -0
  142. package/src/scripts/nexo-postmortem-consolidator 2.py +456 -0
  143. package/src/scripts/nexo-pre-commit 2.py +120 -0
  144. package/src/scripts/nexo-prevent-sleep 2.sh +35 -0
  145. package/src/scripts/nexo-proactive-dashboard 2.py +354 -0
  146. package/src/scripts/nexo-reflection 2.py +256 -0
  147. package/src/scripts/nexo-runtime-preflight 2.py +274 -0
  148. package/src/scripts/nexo-sleep 2.py +631 -0
  149. package/src/scripts/nexo-snapshot-restore 2.sh +35 -0
  150. package/src/scripts/nexo-sync-clients 2.py +16 -0
  151. package/src/scripts/nexo-synthesis 2.py +475 -0
  152. package/src/scripts/nexo-tcc-approve 2.sh +79 -0
  153. package/src/scripts/nexo-update 2.sh +306 -0
  154. package/src/scripts/nexo-watchdog 2.sh +1207 -0
  155. package/src/scripts/nexo-watchdog-smoke 2.py +119 -0
  156. package/src/scripts/rehydrate_learnings_from_archive 2.py +245 -0
  157. package/src/server 2.py +1296 -0
  158. package/src/skills/run-nexo-audit-phase/guide 2.md +43 -0
  159. package/src/skills/run-nexo-audit-phase/skill 2.json +59 -0
  160. package/src/skills/run-nexo-core-fix-cycle/guide 2.md +17 -0
  161. package/src/skills/run-nexo-core-fix-cycle/script 2.py +276 -0
  162. package/src/skills/run-nexo-core-fix-cycle/skill 2.json +58 -0
  163. package/src/skills/run-release-final-audit/guide 2.md +16 -0
  164. package/src/skills/run-release-final-audit/script 2.py +259 -0
  165. package/src/skills/run-release-final-audit/skill 2.json +77 -0
  166. package/src/skills/run-runtime-doctor/guide 2.md +12 -0
  167. package/src/skills/run-runtime-doctor/script 2.py +21 -0
  168. package/src/skills/run-runtime-doctor/skill 2.json +25 -0
  169. package/src/skills_runtime 2.py +932 -0
  170. package/src/state_watchers_runtime 2.py +475 -0
  171. package/src/storage_router 2.py +32 -0
  172. package/src/system_catalog 2.py +786 -0
  173. package/src/tools_coordination 2.py +103 -0
  174. package/src/tools_credentials 2.py +68 -0
  175. package/src/tools_drive 2.py +487 -0
  176. package/src/tools_hot_context 2.py +163 -0
  177. package/src/tools_learnings 2.py +612 -0
  178. package/src/tools_menu 2.py +229 -0
  179. package/src/tools_reminders 2.py +88 -0
  180. package/src/tools_reminders_crud 2.py +363 -0
  181. package/src/tools_sessions 2.py +1054 -0
  182. package/src/tools_system_catalog 2.py +19 -0
  183. package/src/tools_task_history 2.py +57 -0
  184. package/src/tools_transcripts 2.py +98 -0
  185. package/src/transcript_utils 2.py +412 -0
  186. package/src/user_context 2.py +46 -0
  187. package/src/user_data_portability 2.py +328 -0
  188. package/src/user_state_model 2.py +170 -0
  189. package/templates/CLAUDE.md 2.template +108 -0
  190. package/templates/CODEX.AGENTS.md 2.template +66 -0
  191. package/templates/launchagents/README 2.md +132 -0
  192. package/templates/launchagents/com.nexo.auto-close-sessions 2.plist +39 -0
  193. package/templates/launchagents/com.nexo.catchup 2.plist +39 -0
  194. package/templates/launchagents/com.nexo.cognitive-decay 2.plist +40 -0
  195. package/templates/launchagents/com.nexo.dashboard 2.plist +43 -0
  196. package/templates/launchagents/com.nexo.deep-sleep 2.plist +43 -0
  197. package/templates/launchagents/com.nexo.evolution 2.plist +44 -0
  198. package/templates/launchagents/com.nexo.followup-hygiene 2.plist +45 -0
  199. package/templates/launchagents/com.nexo.immune 2.plist +41 -0
  200. package/templates/launchagents/com.nexo.postmortem 2.plist +45 -0
  201. package/templates/launchagents/com.nexo.self-audit 2.plist +47 -0
  202. package/templates/launchagents/com.nexo.synthesis 2.plist +45 -0
  203. package/templates/launchagents/com.nexo.watchdog 2.plist +37 -0
  204. package/templates/nexo_helper 2.py +301 -0
  205. package/templates/openclaw 2.json +13 -0
  206. package/templates/plugin-template 2.py +40 -0
  207. package/templates/script-template 2.py +59 -0
  208. package/templates/script-template 2.sh +13 -0
  209. package/templates/skill-script-template 2.py +48 -0
  210. package/templates/skill-template 2.md +33 -0
@@ -0,0 +1,259 @@
1
+ {% extends "base.html" %}
2
+
3
+ {% block title %}Guard Heatmap{% endblock %}
4
+
5
+ {% block page_title %}Guard Heatmap{% endblock %}
6
+ {% block page_subtitle %}<span class="text-xs text-slate-500 ml-2">Risk map of the codebase</span>{% endblock %}
7
+
8
+ {% block header_actions %}
9
+ <button onclick="loadData()" class="text-xs text-slate-400 hover:text-slate-200 transition-colors px-2 py-1 rounded hover:bg-slate-800">
10
+ <svg class="w-3.5 h-3.5 inline -mt-0.5 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"/></svg>
11
+ Refresh
12
+ </button>
13
+ {% endblock %}
14
+
15
+ {% block content %}
16
+ <!-- Stats row -->
17
+ <div class="grid grid-cols-4 gap-4 mb-6">
18
+ <div class="bg-slate-900/50 border border-slate-800/50 rounded-xl p-5 card">
19
+ <p class="text-xs uppercase tracking-wider text-slate-500 font-semibold mb-1">Total Checks</p>
20
+ <p class="text-2xl font-display font-bold text-slate-100" id="stat-total">--</p>
21
+ </div>
22
+ <div class="bg-slate-900/50 border border-slate-800/50 rounded-xl p-5 card">
23
+ <p class="text-xs uppercase tracking-wider text-slate-500 font-semibold mb-1">Areas Covered</p>
24
+ <p class="text-2xl font-display font-bold text-slate-100" id="stat-areas">--</p>
25
+ </div>
26
+ <div class="bg-slate-900/50 border border-slate-800/50 rounded-xl p-5 card">
27
+ <p class="text-xs uppercase tracking-wider text-slate-500 font-semibold mb-1">Blocking Rate</p>
28
+ <p class="text-2xl font-display font-bold text-red-400" id="stat-blocking">--</p>
29
+ </div>
30
+ <div class="bg-slate-900/50 border border-slate-800/50 rounded-xl p-5 card">
31
+ <p class="text-xs uppercase tracking-wider text-slate-500 font-semibold mb-1">Somatic Risks</p>
32
+ <p class="text-2xl font-display font-bold text-amber-400" id="stat-somatic">--</p>
33
+ </div>
34
+ </div>
35
+
36
+ <!-- Heatmap + Somatic -->
37
+ <div class="grid grid-cols-3 gap-5 mb-6">
38
+ <!-- Heatmap Grid -->
39
+ <div class="col-span-2 bg-slate-900/50 border border-slate-800/50 rounded-xl p-5 card">
40
+ <div class="flex items-center justify-between mb-4">
41
+ <h2 class="text-xs uppercase tracking-wider text-slate-500 font-semibold">Risk Heatmap</h2>
42
+ <div class="flex items-center gap-2 text-[10px] text-slate-500">
43
+ <span class="flex items-center gap-1"><span class="w-3 h-3 rounded bg-emerald-500/80"></span>Low</span>
44
+ <span class="flex items-center gap-1"><span class="w-3 h-3 rounded bg-amber-500/80"></span>Medium</span>
45
+ <span class="flex items-center gap-1"><span class="w-3 h-3 rounded bg-red-500/80"></span>High</span>
46
+ </div>
47
+ </div>
48
+ <div id="heatmap-grid" class="grid grid-cols-3 gap-3">
49
+ <div class="col-span-3 py-12 text-center text-sm text-slate-500">Loading heatmap...</div>
50
+ </div>
51
+ </div>
52
+
53
+ <!-- Somatic Markers -->
54
+ <div class="bg-slate-900/50 border border-slate-800/50 rounded-xl p-5 card">
55
+ <h2 class="text-xs uppercase tracking-wider text-slate-500 font-semibold mb-4">Somatic Risk Targets</h2>
56
+ <div id="somatic-list" class="space-y-3">
57
+ <div class="py-8 text-center text-sm text-slate-500">Loading...</div>
58
+ </div>
59
+ </div>
60
+ </div>
61
+
62
+ <!-- Area Detail Modal -->
63
+ <div id="area-modal" class="hidden fixed inset-0 bg-black/60 backdrop-blur-sm z-50 flex items-center justify-center" onclick="if(event.target===this)closeModal()">
64
+ <div class="bg-slate-900 border border-slate-700 rounded-2xl p-6 max-w-lg w-full mx-4 shadow-2xl animate-slide-in">
65
+ <div class="flex items-center justify-between mb-4">
66
+ <h3 class="text-sm font-display font-semibold text-slate-100" id="modal-title">Area Details</h3>
67
+ <button onclick="closeModal()" class="text-slate-500 hover:text-slate-300 transition-colors">
68
+ <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M6 18L18 6M6 6l12 12"/></svg>
69
+ </button>
70
+ </div>
71
+ <div id="modal-content" class="space-y-3"></div>
72
+ </div>
73
+ </div>
74
+
75
+ <!-- Recent Guard Checks -->
76
+ <div class="bg-slate-900/50 border border-slate-800/50 rounded-xl p-5 card">
77
+ <div class="flex items-center justify-between mb-4">
78
+ <h2 class="text-xs uppercase tracking-wider text-slate-500 font-semibold">Recent Guard Checks</h2>
79
+ <span class="text-[10px] text-slate-600 font-mono" id="checks-count">--</span>
80
+ </div>
81
+ <div id="checks-table" class="overflow-x-auto">
82
+ <table class="w-full text-sm">
83
+ <thead>
84
+ <tr class="text-left text-xs text-slate-500 uppercase tracking-wider border-b border-slate-800/50">
85
+ <th class="pb-2 pr-3 font-semibold">Area</th>
86
+ <th class="pb-2 pr-3 font-semibold">Action</th>
87
+ <th class="pb-2 pr-3 font-semibold">Result</th>
88
+ <th class="pb-2 pr-3 font-semibold">Warnings</th>
89
+ <th class="pb-2 font-semibold">Time</th>
90
+ </tr>
91
+ </thead>
92
+ <tbody id="checks-body" class="divide-y divide-slate-800/30">
93
+ <tr><td colspan="5" class="py-8 text-center text-slate-500">Loading checks...</td></tr>
94
+ </tbody>
95
+ </table>
96
+ </div>
97
+ </div>
98
+ {% endblock %}
99
+
100
+ {% block scripts %}
101
+ <script>
102
+ let guardData = null;
103
+
104
+ function heatColor(blocking, total) {
105
+ if (!total) return { bg: 'bg-slate-800/50', border: 'border-slate-700/30', text: 'text-slate-500' };
106
+ const ratio = blocking / total;
107
+ if (ratio > 0.3) return { bg: 'bg-red-500/15', border: 'border-red-500/30', text: 'text-red-400' };
108
+ if (ratio > 0.1) return { bg: 'bg-amber-500/15', border: 'border-amber-500/30', text: 'text-amber-400' };
109
+ return { bg: 'bg-emerald-500/15', border: 'border-emerald-500/30', text: 'text-emerald-400' };
110
+ }
111
+
112
+ function showAreaDetail(area) {
113
+ if (!guardData || !guardData.heatmap) return;
114
+ const info = guardData.heatmap[area];
115
+ if (!info) return;
116
+
117
+ document.getElementById('modal-title').textContent = area;
118
+ const content = document.getElementById('modal-content');
119
+ content.innerHTML = `
120
+ <div class="grid grid-cols-3 gap-3">
121
+ <div class="bg-slate-800/50 rounded-lg p-3 text-center">
122
+ <p class="text-lg font-bold text-slate-100">${info.count || 0}</p>
123
+ <p class="text-[10px] text-slate-500 uppercase tracking-wider">Checks</p>
124
+ </div>
125
+ <div class="bg-slate-800/50 rounded-lg p-3 text-center">
126
+ <p class="text-lg font-bold text-red-400">${info.blocking || 0}</p>
127
+ <p class="text-[10px] text-slate-500 uppercase tracking-wider">Blocking</p>
128
+ </div>
129
+ <div class="bg-slate-800/50 rounded-lg p-3 text-center">
130
+ <p class="text-lg font-bold text-violet-400">${info.learnings || 0}</p>
131
+ <p class="text-[10px] text-slate-500 uppercase tracking-wider">Learnings</p>
132
+ </div>
133
+ </div>
134
+ ${(info.recent_checks || []).length ? `
135
+ <div class="mt-3">
136
+ <p class="text-xs text-slate-500 font-semibold uppercase tracking-wider mb-2">Recent Checks</p>
137
+ <div class="space-y-1.5">
138
+ ${(info.recent_checks || []).slice(0, 5).map(c => `
139
+ <div class="flex items-center justify-between bg-slate-800/30 rounded-lg px-3 py-2 text-xs">
140
+ <span class="text-slate-300">${escapeHtml(c.action || c.description || '--')}</span>
141
+ <span class="${c.passed ? 'text-emerald-400' : 'text-red-400'}">${c.passed ? 'PASS' : 'BLOCK'}</span>
142
+ </div>
143
+ `).join('')}
144
+ </div>
145
+ </div>` : ''}
146
+ `;
147
+ document.getElementById('area-modal').classList.remove('hidden');
148
+ }
149
+
150
+ function closeModal() {
151
+ document.getElementById('area-modal').classList.add('hidden');
152
+ }
153
+
154
+ async function loadData() {
155
+ const data = await fetchJSON('/api/guard');
156
+ if (!data) return;
157
+ guardData = data;
158
+
159
+ const checks = data.checks || [];
160
+ const heatmap = data.heatmap || {};
161
+ const markers = data.somatic_markers || [];
162
+
163
+ // Stats
164
+ const totalChecks = checks.length;
165
+ const areas = Object.keys(heatmap);
166
+ const totalBlocking = checks.filter(c => !c.passed && (c.type === 'blocking' || c.blocked)).length;
167
+ const blockingRate = totalChecks > 0 ? ((totalBlocking / totalChecks) * 100).toFixed(1) + '%' : '0%';
168
+
169
+ document.getElementById('stat-total').textContent = formatNumber(totalChecks);
170
+ document.getElementById('stat-areas').textContent = areas.length;
171
+ document.getElementById('stat-blocking').textContent = blockingRate;
172
+ document.getElementById('stat-somatic').textContent = markers.length;
173
+
174
+ // Heatmap grid
175
+ const grid = document.getElementById('heatmap-grid');
176
+ if (!areas.length) {
177
+ grid.innerHTML = '<div class="col-span-3 py-12 text-center text-sm text-slate-500">No guard areas recorded yet</div>';
178
+ } else {
179
+ grid.innerHTML = areas.map(area => {
180
+ const info = heatmap[area];
181
+ const c = heatColor(info.blocking || 0, info.count || 0);
182
+ return `
183
+ <button onclick="showAreaDetail('${escapeHtml(area)}')"
184
+ class="${c.bg} border ${c.border} rounded-xl p-4 text-left transition-all hover:scale-[1.02] hover:shadow-lg cursor-pointer group">
185
+ <p class="text-sm font-semibold text-slate-200 group-hover:text-white truncate">${escapeHtml(area)}</p>
186
+ <div class="flex items-center gap-3 mt-2">
187
+ <span class="text-xs text-slate-400">${info.count || 0} checks</span>
188
+ <span class="text-xs ${c.text} font-medium">${info.blocking || 0} blocking</span>
189
+ </div>
190
+ <div class="mt-2 h-1.5 bg-slate-800/50 rounded-full overflow-hidden">
191
+ <div class="h-full rounded-full transition-all duration-500 ${info.blocking > 0 ? (info.blocking / info.count > 0.3 ? 'bg-red-500' : 'bg-amber-500') : 'bg-emerald-500'}"
192
+ style="width: ${info.count ? Math.min(((info.blocking || 0) / info.count) * 100, 100).toFixed(1) : 0}%"></div>
193
+ </div>
194
+ </button>
195
+ `;
196
+ }).join('');
197
+ }
198
+
199
+ // Somatic markers
200
+ const somaticList = document.getElementById('somatic-list');
201
+ if (!markers.length) {
202
+ somaticList.innerHTML = '<div class="py-8 text-center text-sm text-slate-500">No somatic markers</div>';
203
+ } else {
204
+ const maxScore = Math.max(...markers.map(m => m.risk_score || 0), 1);
205
+ somaticList.innerHTML = markers.sort((a, b) => (b.risk_score || 0) - (a.risk_score || 0)).slice(0, 10).map(m => {
206
+ const score = m.risk_score || 0;
207
+ const pct = (score / maxScore * 100).toFixed(1);
208
+ const color = score > 5 ? 'bg-red-500' : score > 2 ? 'bg-amber-500' : 'bg-emerald-500';
209
+ const textColor = score > 5 ? 'text-red-400' : score > 2 ? 'text-amber-400' : 'text-emerald-400';
210
+ return `
211
+ <div class="flex items-center gap-3">
212
+ <div class="flex-1 min-w-0">
213
+ <p class="text-xs text-slate-300 truncate">${escapeHtml(m.target || '--')}</p>
214
+ <div class="mt-1 h-1.5 bg-slate-800/50 rounded-full overflow-hidden">
215
+ <div class="h-full rounded-full ${color} transition-all duration-700" style="width:${pct}%"></div>
216
+ </div>
217
+ </div>
218
+ <div class="text-right flex-shrink-0">
219
+ <span class="text-xs font-mono font-medium ${textColor}">${score.toFixed(1)}</span>
220
+ <p class="text-[10px] text-slate-600">${m.incident_count || 0} incidents</p>
221
+ </div>
222
+ </div>
223
+ `;
224
+ }).join('');
225
+ }
226
+
227
+ // Recent checks table
228
+ document.getElementById('checks-count').textContent = `${checks.length} checks`;
229
+ const tbody = document.getElementById('checks-body');
230
+ if (!checks.length) {
231
+ tbody.innerHTML = '<tr><td colspan="5" class="py-8 text-center text-slate-500 text-sm">No guard checks recorded</td></tr>';
232
+ } else {
233
+ tbody.innerHTML = checks.slice(0, 25).map(c => {
234
+ const passed = c.passed !== false && !c.blocked;
235
+ const warnings = c.warnings || c.warning_count || 0;
236
+ return `
237
+ <tr class="group hover:bg-slate-800/20 transition-colors">
238
+ <td class="py-2.5 pr-3 text-slate-300">${escapeHtml(c.area || c.category || '--')}</td>
239
+ <td class="py-2.5 pr-3 text-slate-400 font-mono text-xs max-w-[200px] truncate">${escapeHtml(c.action || c.description || '--')}</td>
240
+ <td class="py-2.5 pr-3">
241
+ <span class="inline-flex items-center gap-1 px-2 py-0.5 rounded-full text-[10px] font-semibold uppercase ${passed ? 'bg-emerald-500/15 text-emerald-400' : 'bg-red-500/15 text-red-400'}">
242
+ <span class="w-1.5 h-1.5 rounded-full ${passed ? 'bg-emerald-400' : 'bg-red-400'}"></span>
243
+ ${passed ? 'Pass' : 'Block'}
244
+ </span>
245
+ </td>
246
+ <td class="py-2.5 pr-3 text-xs ${warnings > 0 ? 'text-amber-400' : 'text-slate-600'}">${warnings > 0 ? warnings + ' warning' + (warnings !== 1 ? 's' : '') : 'none'}</td>
247
+ <td class="py-2.5 text-xs text-slate-500">${relativeTime(c.created_at || c.checked_at)}</td>
248
+ </tr>
249
+ `;
250
+ }).join('');
251
+ }
252
+ }
253
+
254
+ loadData();
255
+ setInterval(loadData, 60000);
256
+
257
+ document.addEventListener('keydown', e => { if (e.key === 'Escape') closeModal(); });
258
+ </script>
259
+ {% endblock %}
@@ -0,0 +1,251 @@
1
+ {% extends "base.html" %}
2
+
3
+ {% block title %}Inbox{% endblock %}
4
+ {% block page_title %}Inbox{% endblock %}
5
+
6
+ {% block header_actions %}
7
+ <button onclick="markAllRead()" class="text-xs px-2.5 py-1 rounded-md bg-slate-800 text-slate-400 hover:bg-slate-700 transition-colors">Mark all read</button>
8
+ {% endblock %}
9
+
10
+ {% block content %}
11
+ <div class="flex flex-col" style="height: calc(100vh - 8.5rem);">
12
+ <div id="chat-area" class="flex-1 overflow-y-auto space-y-3">
13
+ <div class="flex items-center justify-center h-full">
14
+ <p class="text-slate-500 text-sm">Loading messages...</p>
15
+ </div>
16
+ </div>
17
+
18
+ <!-- Reply indicator -->
19
+ <div id="reply-bar" class="hidden border-t border-violet-500/30 bg-violet-500/5 px-6 py-2 flex-shrink-0">
20
+ <div class="flex items-center gap-2 max-w-3xl mx-auto">
21
+ <div class="w-0.5 h-8 bg-violet-500 rounded-full flex-shrink-0"></div>
22
+ <div class="flex-1 min-w-0">
23
+ <span class="text-xs text-violet-400 font-display font-medium" id="reply-author"></span>
24
+ <p class="text-xs text-slate-400 truncate" id="reply-preview"></p>
25
+ </div>
26
+ <button onclick="cancelReply()" class="text-slate-500 hover:text-slate-300 text-lg leading-none flex-shrink-0">&times;</button>
27
+ </div>
28
+ </div>
29
+
30
+ <div class="border-t border-slate-800/50 pt-4 flex-shrink-0">
31
+ <div class="flex gap-3 max-w-3xl mx-auto">
32
+ <textarea id="inbox-msg" placeholder="Leave a note for NEXO..." rows="2"
33
+ class="flex-1 bg-slate-800 border border-slate-700 rounded-lg px-3 py-2 text-sm text-slate-200 placeholder-slate-500 focus:outline-none focus:ring-1 focus:ring-violet-500 resize-none"
34
+ onkeydown="if(event.key==='Enter'&&!event.shiftKey){event.preventDefault();sendNote()}"></textarea>
35
+ <button onclick="sendNote()" class="px-4 py-2 bg-violet-600 text-white rounded-lg text-sm hover:bg-violet-500 transition-colors self-end font-display font-medium">Send</button>
36
+ </div>
37
+ </div>
38
+ </div>
39
+ {% endblock %}
40
+
41
+ {% block scripts %}
42
+ <script>
43
+ let messages = [];
44
+ let pollTimer = null;
45
+ let replyingTo = null;
46
+
47
+ function inboxEscapeHtml(str) {
48
+ return String(str)
49
+ .replace(/&/g, '&amp;')
50
+ .replace(/</g, '&lt;')
51
+ .replace(/>/g, '&gt;')
52
+ .replace(/"/g, '&quot;');
53
+ }
54
+
55
+ function authorName(direction) {
56
+ return direction === 'to_nexo' ? 'User' : 'NEXO';
57
+ }
58
+
59
+ function renderMessage(msg) {
60
+ const isFromUser = msg.direction === 'to_nexo';
61
+ const time = new Date(msg.created_at).toLocaleString('es-ES', {
62
+ day: 'numeric', month: 'short', hour: '2-digit', minute: '2-digit'
63
+ });
64
+ const unreadDot = (!msg.read && !isFromUser)
65
+ ? '<span class="inline-block w-1.5 h-1.5 rounded-full bg-violet-400 ml-1.5 align-middle"></span>'
66
+ : '';
67
+
68
+ let quoteHtml = '';
69
+ if (msg.reply_to) {
70
+ const parent = messages.find(m => m.id === msg.reply_to);
71
+ if (parent) {
72
+ const parentAuthor = authorName(parent.direction);
73
+ const parentPreview = inboxEscapeHtml((parent.content || '').slice(0, 120));
74
+ quoteHtml = `
75
+ <div class="flex items-start gap-2 mb-2 cursor-pointer opacity-70 hover:opacity-100 transition-opacity" onclick="scrollToMsg(${parent.id})">
76
+ <div class="w-0.5 self-stretch bg-violet-500/50 rounded-full flex-shrink-0"></div>
77
+ <div class="min-w-0">
78
+ <span class="text-xs font-display font-medium ${parent.direction === 'to_nexo' ? 'text-slate-400' : 'text-pink-400/70'}">${parentAuthor}</span>
79
+ <p class="text-xs text-slate-500 truncate">${parentPreview}</p>
80
+ </div>
81
+ </div>`;
82
+ }
83
+ }
84
+
85
+ const replyBtn = `<button onclick="startReply(${msg.id})" class="text-slate-600 hover:text-violet-400 transition-colors text-xs font-mono ml-2" title="Reply">&#8617;</button>`;
86
+
87
+ if (isFromUser) {
88
+ return `
89
+ <div class="flex justify-end" data-id="${msg.id}" id="msg-${msg.id}">
90
+ <div class="max-w-lg bg-violet-600/20 border border-violet-500/20 rounded-lg px-4 py-3">
91
+ ${quoteHtml}
92
+ <div class="text-xs text-slate-500 mb-1 font-display">User</div>
93
+ <div class="text-sm text-slate-200 leading-relaxed">${inboxEscapeHtml(msg.content).replace(/\n/g, '<br>')}</div>
94
+ <div class="flex items-center justify-end mt-1">
95
+ <span class="text-xs text-slate-500 font-mono">${time}</span>
96
+ ${replyBtn}
97
+ </div>
98
+ </div>
99
+ </div>`;
100
+ } else {
101
+ return `
102
+ <div class="flex justify-start" data-id="${msg.id}" id="msg-${msg.id}">
103
+ <div class="max-w-lg bg-slate-800 border border-slate-700 rounded-lg px-4 py-3">
104
+ ${quoteHtml}
105
+ <div class="text-xs text-pink-400 mb-1 font-display font-medium">NEXO${unreadDot}</div>
106
+ <div class="text-sm text-slate-200 leading-relaxed">${inboxEscapeHtml(msg.content).replace(/\n/g, '<br>')}</div>
107
+ <div class="flex items-center mt-1">
108
+ <span class="text-xs text-slate-500 font-mono">${time}</span>
109
+ ${replyBtn}
110
+ </div>
111
+ </div>
112
+ </div>`;
113
+ }
114
+ }
115
+
116
+ function startReply(msgId) {
117
+ const msg = messages.find(m => m.id === msgId);
118
+ if (!msg) return;
119
+ replyingTo = msg;
120
+ document.getElementById('reply-bar').classList.remove('hidden');
121
+ document.getElementById('reply-author').textContent = authorName(msg.direction);
122
+ document.getElementById('reply-preview').textContent = (msg.content || '').slice(0, 150);
123
+ document.getElementById('inbox-msg').focus();
124
+ }
125
+
126
+ function cancelReply() {
127
+ replyingTo = null;
128
+ document.getElementById('reply-bar').classList.add('hidden');
129
+ }
130
+
131
+ function scrollToMsg(msgId) {
132
+ const el = document.getElementById('msg-' + msgId);
133
+ if (el) {
134
+ el.scrollIntoView({ behavior: 'smooth', block: 'center' });
135
+ el.firstElementChild.classList.add('ring-1', 'ring-violet-500/50');
136
+ setTimeout(() => el.firstElementChild.classList.remove('ring-1', 'ring-violet-500/50'), 2000);
137
+ }
138
+ }
139
+
140
+ function renderMessages() {
141
+ const area = document.getElementById('chat-area');
142
+ if (messages.length === 0) {
143
+ area.innerHTML = `
144
+ <div class="flex items-center justify-center h-full">
145
+ <p class="text-slate-500 text-sm">No messages yet. Leave a note for NEXO.</p>
146
+ </div>`;
147
+ return;
148
+ }
149
+ area.innerHTML = `<div class="space-y-3">${messages.map(renderMessage).join('')}</div>`;
150
+ area.scrollTop = area.scrollHeight;
151
+ }
152
+
153
+ async function loadMessages() {
154
+ try {
155
+ const res = await fetch('/api/inbox?limit=200');
156
+ if (!res.ok) {
157
+ let detail = `HTTP ${res.status}`;
158
+ try { const b = await res.json(); detail = b.error || b.detail || detail; } catch {}
159
+ throw new Error(detail);
160
+ }
161
+ const data = await res.json();
162
+ messages = data.notes || [];
163
+ messages.sort((a, b) => new Date(a.created_at) - new Date(b.created_at));
164
+ renderMessages();
165
+ markIncomingRead();
166
+ } catch (e) {
167
+ document.getElementById('chat-area').innerHTML =
168
+ `<div class="flex items-center justify-center h-full"><p class="text-red-400 text-sm">Error loading messages: ${inboxEscapeHtml(e.message)}</p></div>`;
169
+ }
170
+ }
171
+
172
+ async function markIncomingRead() {
173
+ const unread = messages.filter(m => m.direction === 'to_user' && !m.read);
174
+ for (const msg of unread) {
175
+ try {
176
+ await fetch(`/api/inbox/${msg.id}/read`, { method: 'PUT' });
177
+ msg.read = true;
178
+ } catch (e) { /* ignore */ }
179
+ }
180
+ }
181
+
182
+ async function sendNote() {
183
+ const textarea = document.getElementById('inbox-msg');
184
+ const content = textarea.value.trim();
185
+ if (!content) return;
186
+
187
+ const body = { direction: 'to_nexo', content };
188
+ if (replyingTo) body.reply_to = replyingTo.id;
189
+
190
+ try {
191
+ const res = await fetch('/api/inbox', {
192
+ method: 'POST',
193
+ headers: { 'Content-Type': 'application/json' },
194
+ body: JSON.stringify(body)
195
+ });
196
+ if (!res.ok) {
197
+ let detail = `HTTP ${res.status}`;
198
+ try { const b = await res.json(); detail = b.error || b.detail || detail; } catch {}
199
+ throw new Error(detail);
200
+ }
201
+ const data = await res.json();
202
+ textarea.value = '';
203
+ cancelReply();
204
+ messages.push({
205
+ id: data.note?.id || Date.now(),
206
+ direction: 'to_nexo',
207
+ content,
208
+ reply_to: body.reply_to || null,
209
+ created_at: data.note?.created_at || new Date().toISOString(),
210
+ read: true
211
+ });
212
+ renderMessages();
213
+ showToast('Note sent');
214
+ } catch (e) {
215
+ showToast('Error: ' + e.message);
216
+ }
217
+ }
218
+
219
+ async function markAllRead() {
220
+ const unread = messages.filter(m => !m.read);
221
+ for (const msg of unread) {
222
+ try {
223
+ await fetch(`/api/inbox/${msg.id}/read`, { method: 'PUT' });
224
+ msg.read = true;
225
+ } catch (e) { /* ignore */ }
226
+ }
227
+ renderMessages();
228
+ showToast('All messages marked as read');
229
+ }
230
+
231
+ async function pollNewMessages() {
232
+ try {
233
+ const res = await fetch('/api/inbox?limit=200');
234
+ const data = await res.json();
235
+ const newMessages = data.notes || [];
236
+ newMessages.sort((a, b) => new Date(a.created_at) - new Date(b.created_at));
237
+ const prevIds = messages.map(m => m.id).join(',');
238
+ const newIds = newMessages.map(m => m.id).join(',');
239
+ if (prevIds !== newIds || newMessages.length !== messages.length) {
240
+ messages = newMessages;
241
+ renderMessages();
242
+ markIncomingRead();
243
+ }
244
+ } catch (e) { /* silent */ }
245
+ }
246
+
247
+ // Initial load + polling
248
+ loadMessages();
249
+ pollTimer = setInterval(pollNewMessages, 10000);
250
+ </script>
251
+ {% endblock %}