nexo-brain 5.3.26 → 5.3.28

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 (212) hide show
  1. package/.claude-plugin/plugin.json +1 -1
  2. package/package.json +1 -1
  3. package/src/hook_guardrails.py +44 -0
  4. package/src/server.py +3 -0
  5. package/src/tools_sessions.py +6 -1
  6. package/src/dashboard/static/favicon 2.svg +0 -32
  7. package/src/dashboard/static/nexo-logo 2.png +0 -0
  8. package/src/dashboard/static/nexo-logo 2.svg +0 -40
  9. package/src/dashboard/static/style 2.css +0 -2458
  10. package/src/dashboard/templates/adaptive 2.html +0 -118
  11. package/src/dashboard/templates/artifacts 2.html +0 -133
  12. package/src/dashboard/templates/backups 2.html +0 -136
  13. package/src/dashboard/templates/base 2.html +0 -417
  14. package/src/dashboard/templates/calendar 2.html +0 -591
  15. package/src/dashboard/templates/chat 2.html +0 -356
  16. package/src/dashboard/templates/claims 2.html +0 -259
  17. package/src/dashboard/templates/cortex 2.html +0 -321
  18. package/src/dashboard/templates/credentials 2.html +0 -128
  19. package/src/dashboard/templates/crons 2.html +0 -370
  20. package/src/dashboard/templates/dashboard 2.html +0 -494
  21. package/src/dashboard/templates/dreams 2.html +0 -252
  22. package/src/dashboard/templates/email 2.html +0 -160
  23. package/src/dashboard/templates/evolution 2.html +0 -189
  24. package/src/dashboard/templates/feed 2.html +0 -249
  25. package/src/dashboard/templates/followup_health 2.html +0 -170
  26. package/src/dashboard/templates/graph 2.html +0 -201
  27. package/src/dashboard/templates/guard 2.html +0 -259
  28. package/src/dashboard/templates/inbox 2.html +0 -251
  29. package/src/dashboard/templates/memory 2.html +0 -420
  30. package/src/dashboard/templates/operations 2.html +0 -608
  31. package/src/dashboard/templates/plugins 2.html +0 -185
  32. package/src/dashboard/templates/protocol 2.html +0 -199
  33. package/src/dashboard/templates/rules 2.html +0 -246
  34. package/src/dashboard/templates/sentiment 2.html +0 -247
  35. package/src/dashboard/templates/sessions 2.html +0 -218
  36. package/src/dashboard/templates/skills 2.html +0 -329
  37. package/src/dashboard/templates/somatic 2.html +0 -73
  38. package/src/dashboard/templates/triggers 2.html +0 -133
  39. package/src/dashboard/templates/trust 2.html +0 -360
  40. package/src/db/__init__ 2.py +0 -259
  41. package/src/db/_core 2.py +0 -437
  42. package/src/db/_credentials 2.py +0 -124
  43. package/src/db/_episodic 2.py +0 -762
  44. package/src/db/_evolution 2.py +0 -54
  45. package/src/db/_fts 2.py +0 -406
  46. package/src/db/_goal_profiles 2.py +0 -376
  47. package/src/db/_hot_context 2.py +0 -660
  48. package/src/db/_outcomes 2.py +0 -800
  49. package/src/db/_personal_scripts 2.py +0 -582
  50. package/src/db/_sessions 2.py +0 -330
  51. package/src/db/_tasks 2.py +0 -91
  52. package/src/db/_watchers 2.py +0 -173
  53. package/src/doctor/formatters 2.py +0 -52
  54. package/src/doctor/models 2.py +0 -69
  55. package/src/doctor/planes 2.py +0 -87
  56. package/src/doctor/providers/__init__ 2.py +0 -1
  57. package/src/doctor/providers/deep 2.py +0 -367
  58. package/src/evolution_cycle 2.py +0 -519
  59. package/src/hooks/auto_capture 2.py +0 -208
  60. package/src/hooks/caffeinate-guard 2.sh +0 -8
  61. package/src/hooks/capture-session 2.sh +0 -21
  62. package/src/hooks/capture-tool-logs 2.sh +0 -158
  63. package/src/hooks/daily-briefing-check 2.sh +0 -33
  64. package/src/hooks/heartbeat-enforcement 2.py +0 -90
  65. package/src/hooks/heartbeat-posttool 2.sh +0 -18
  66. package/src/hooks/inbox-hook 2.sh +0 -76
  67. package/src/hooks/post-compact 2.sh +0 -152
  68. package/src/hooks/pre-compact 2.sh +0 -169
  69. package/src/hooks/protocol-guardrail 2.sh +0 -10
  70. package/src/hooks/protocol-pretool-guardrail 2.sh +0 -9
  71. package/src/hooks/session-stop 2.sh +0 -52
  72. package/src/kg_populate 2.py +0 -292
  73. package/src/maintenance 2.py +0 -53
  74. package/src/memory_backends 2.py +0 -71
  75. package/src/migrate_embeddings 2.py +0 -124
  76. package/src/nexo_sdk 2.py +0 -103
  77. package/src/observability 2.py +0 -199
  78. package/src/plugin_loader 2.py +0 -217
  79. package/src/plugins/__init__ 2.py +0 -0
  80. package/src/plugins/artifact_registry 2.py +0 -450
  81. package/src/plugins/backup 2.py +0 -127
  82. package/src/plugins/claims_tools 2.py +0 -119
  83. package/src/plugins/cognitive_memory 2.py +0 -609
  84. package/src/plugins/core_rules 2.py +0 -252
  85. package/src/plugins/cortex 2.py +0 -1155
  86. package/src/plugins/entities 2.py +0 -67
  87. package/src/plugins/episodic_memory 2.py +0 -560
  88. package/src/plugins/evolution 2.py +0 -167
  89. package/src/plugins/goal_engine 2.py +0 -142
  90. package/src/plugins/guard 2.py +0 -862
  91. package/src/plugins/impact 2.py +0 -29
  92. package/src/plugins/knowledge_graph_tools 2.py +0 -137
  93. package/src/plugins/media_memory_tools 2.py +0 -98
  94. package/src/plugins/memory_export 2.py +0 -196
  95. package/src/plugins/outcomes 2.py +0 -130
  96. package/src/plugins/personal_scripts 2.py +0 -117
  97. package/src/plugins/preferences 2.py +0 -47
  98. package/src/plugins/protocol 2.py +0 -1449
  99. package/src/plugins/simple_api 2.py +0 -106
  100. package/src/plugins/skills 2.py +0 -341
  101. package/src/plugins/state_watchers 2.py +0 -79
  102. package/src/plugins/update 2.py +0 -986
  103. package/src/plugins/user_state_tools 2.py +0 -43
  104. package/src/plugins/workflow 2.py +0 -588
  105. package/src/protocol_settings 2.py +0 -59
  106. package/src/public_contribution 2.py +0 -466
  107. package/src/public_evolution_queue 2.py +0 -241
  108. package/src/requirements 2.txt +0 -14
  109. package/src/retroactive_learnings 2.py +0 -373
  110. package/src/rules/__init__ 2.py +0 -0
  111. package/src/rules/core-rules 2.json +0 -331
  112. package/src/rules/migrate 2.py +0 -207
  113. package/src/runtime_power 2.py +0 -874
  114. package/src/script_registry 2.py +0 -1559
  115. package/src/scripts/check-context 2.py +0 -272
  116. package/src/scripts/deep-sleep/apply_findings 2.py +0 -2327
  117. package/src/scripts/deep-sleep/collect 2.py +0 -928
  118. package/src/scripts/deep-sleep/extract 2.py +0 -330
  119. package/src/scripts/deep-sleep/extract-prompt 2.md +0 -285
  120. package/src/scripts/deep-sleep/synthesize 2.py +0 -312
  121. package/src/scripts/deep-sleep/synthesize-prompt 2.md +0 -336
  122. package/src/scripts/nexo-agent-run 2.py +0 -75
  123. package/src/scripts/nexo-auto-update 2.py +0 -6
  124. package/src/scripts/nexo-backup 2.sh +0 -25
  125. package/src/scripts/nexo-brain-activation 2.sh +0 -140
  126. package/src/scripts/nexo-catchup 2.py +0 -300
  127. package/src/scripts/nexo-cognitive-decay 2.py +0 -257
  128. package/src/scripts/nexo-cortex-cycle 2.py +0 -293
  129. package/src/scripts/nexo-cron-wrapper 2.sh +0 -53
  130. package/src/scripts/nexo-daily-self-audit 2.py +0 -2161
  131. package/src/scripts/nexo-dashboard 2.sh +0 -29
  132. package/src/scripts/nexo-deep-sleep 2.sh +0 -86
  133. package/src/scripts/nexo-evolution-run 2.py +0 -1664
  134. package/src/scripts/nexo-followup-hygiene 2.py +0 -139
  135. package/src/scripts/nexo-hook-record 2.py +0 -42
  136. package/src/scripts/nexo-immune 2.py +0 -936
  137. package/src/scripts/nexo-impact-scorer 2.py +0 -117
  138. package/src/scripts/nexo-inbox-hook 2.sh +0 -74
  139. package/src/scripts/nexo-install 2.py +0 -6
  140. package/src/scripts/nexo-learning-housekeep 2.py +0 -401
  141. package/src/scripts/nexo-learning-validator 2.py +0 -266
  142. package/src/scripts/nexo-migrate 2.py +0 -260
  143. package/src/scripts/nexo-outcome-checker 2.py +0 -127
  144. package/src/scripts/nexo-postmortem-consolidator 2.py +0 -456
  145. package/src/scripts/nexo-pre-commit 2.py +0 -120
  146. package/src/scripts/nexo-prevent-sleep 2.sh +0 -35
  147. package/src/scripts/nexo-proactive-dashboard 2.py +0 -354
  148. package/src/scripts/nexo-reflection 2.py +0 -256
  149. package/src/scripts/nexo-runtime-preflight 2.py +0 -274
  150. package/src/scripts/nexo-sleep 2.py +0 -631
  151. package/src/scripts/nexo-snapshot-restore 2.sh +0 -35
  152. package/src/scripts/nexo-sync-clients 2.py +0 -16
  153. package/src/scripts/nexo-synthesis 2.py +0 -475
  154. package/src/scripts/nexo-tcc-approve 2.sh +0 -79
  155. package/src/scripts/nexo-update 2.sh +0 -306
  156. package/src/scripts/nexo-watchdog 2.sh +0 -1207
  157. package/src/scripts/nexo-watchdog-smoke 2.py +0 -119
  158. package/src/scripts/rehydrate_learnings_from_archive 2.py +0 -245
  159. package/src/server 2.py +0 -1296
  160. package/src/skills/run-nexo-audit-phase/guide 2.md +0 -43
  161. package/src/skills/run-nexo-audit-phase/skill 2.json +0 -59
  162. package/src/skills/run-nexo-core-fix-cycle/guide 2.md +0 -17
  163. package/src/skills/run-nexo-core-fix-cycle/script 2.py +0 -276
  164. package/src/skills/run-nexo-core-fix-cycle/skill 2.json +0 -58
  165. package/src/skills/run-release-final-audit/guide 2.md +0 -16
  166. package/src/skills/run-release-final-audit/script 2.py +0 -259
  167. package/src/skills/run-release-final-audit/skill 2.json +0 -77
  168. package/src/skills/run-runtime-doctor/guide 2.md +0 -12
  169. package/src/skills/run-runtime-doctor/script 2.py +0 -21
  170. package/src/skills/run-runtime-doctor/skill 2.json +0 -25
  171. package/src/skills_runtime 2.py +0 -932
  172. package/src/state_watchers_runtime 2.py +0 -475
  173. package/src/storage_router 2.py +0 -32
  174. package/src/system_catalog 2.py +0 -786
  175. package/src/tools_coordination 2.py +0 -103
  176. package/src/tools_credentials 2.py +0 -68
  177. package/src/tools_drive 2.py +0 -487
  178. package/src/tools_hot_context 2.py +0 -163
  179. package/src/tools_learnings 2.py +0 -612
  180. package/src/tools_menu 2.py +0 -229
  181. package/src/tools_reminders 2.py +0 -88
  182. package/src/tools_reminders_crud 2.py +0 -363
  183. package/src/tools_sessions 2.py +0 -1054
  184. package/src/tools_system_catalog 2.py +0 -19
  185. package/src/tools_task_history 2.py +0 -57
  186. package/src/tools_transcripts 2.py +0 -98
  187. package/src/transcript_utils 2.py +0 -412
  188. package/src/user_context 2.py +0 -46
  189. package/src/user_data_portability 2.py +0 -328
  190. package/src/user_state_model 2.py +0 -170
  191. package/templates/CLAUDE.md 2.template +0 -108
  192. package/templates/CODEX.AGENTS.md 2.template +0 -66
  193. package/templates/launchagents/README 2.md +0 -132
  194. package/templates/launchagents/com.nexo.auto-close-sessions 2.plist +0 -39
  195. package/templates/launchagents/com.nexo.catchup 2.plist +0 -39
  196. package/templates/launchagents/com.nexo.cognitive-decay 2.plist +0 -40
  197. package/templates/launchagents/com.nexo.dashboard 2.plist +0 -43
  198. package/templates/launchagents/com.nexo.deep-sleep 2.plist +0 -43
  199. package/templates/launchagents/com.nexo.evolution 2.plist +0 -44
  200. package/templates/launchagents/com.nexo.followup-hygiene 2.plist +0 -45
  201. package/templates/launchagents/com.nexo.immune 2.plist +0 -41
  202. package/templates/launchagents/com.nexo.postmortem 2.plist +0 -45
  203. package/templates/launchagents/com.nexo.self-audit 2.plist +0 -47
  204. package/templates/launchagents/com.nexo.synthesis 2.plist +0 -45
  205. package/templates/launchagents/com.nexo.watchdog 2.plist +0 -37
  206. package/templates/nexo_helper 2.py +0 -301
  207. package/templates/openclaw 2.json +0 -13
  208. package/templates/plugin-template 2.py +0 -40
  209. package/templates/script-template 2.py +0 -59
  210. package/templates/script-template 2.sh +0 -13
  211. package/templates/skill-script-template 2.py +0 -48
  212. package/templates/skill-template 2.md +0 -33
@@ -1,217 +0,0 @@
1
- from __future__ import annotations
2
- """Dynamic plugin loader for NEXO MCP server."""
3
-
4
- import importlib
5
- import importlib.util
6
- import os
7
- import signal
8
- import sys
9
- import time
10
-
11
- from db import get_db
12
- from fastmcp.tools import Tool
13
-
14
- SERVER_DIR = os.path.dirname(os.path.abspath(__file__))
15
- PLUGINS_DIR = os.path.join(SERVER_DIR, "plugins")
16
-
17
- # Personal plugins directory: NEXO_HOME/plugins/ (env var, defaults to ~/.nexo/)
18
- NEXO_HOME = os.environ.get("NEXO_HOME", os.path.expanduser("~/.nexo"))
19
- PERSONAL_PLUGINS_DIR = os.path.join(NEXO_HOME, "plugins")
20
-
21
- PLUGIN_LOAD_TIMEOUT = 10 # seconds per plugin
22
-
23
-
24
- class _PluginTimeout(Exception):
25
- pass
26
-
27
-
28
- def _timeout_handler(signum, frame):
29
- raise _PluginTimeout("Plugin loading timed out")
30
-
31
-
32
- def _ensure_src_in_path():
33
- """Ensure server src/ is in sys.path so personal plugins can import db, cognitive, etc."""
34
- if SERVER_DIR not in sys.path:
35
- sys.path.insert(0, SERVER_DIR)
36
-
37
-
38
- def load_all_plugins(mcp) -> int:
39
- """Load all plugins from repo and personal directories at startup. Returns total tools loaded."""
40
- _ensure_src_in_path()
41
- total = 0
42
-
43
- # Collect plugins: repo first, personal overrides
44
- plugin_map = {} # filename -> (dir_path, source_label)
45
-
46
- # 1. Repo plugins (base)
47
- if os.path.isdir(PLUGINS_DIR):
48
- for f in sorted(os.listdir(PLUGINS_DIR)):
49
- if f.endswith(".py") and f != "__init__.py":
50
- plugin_map[f] = (PLUGINS_DIR, "repo")
51
-
52
- # 2. Personal plugins (override if same filename)
53
- if os.path.isdir(PERSONAL_PLUGINS_DIR):
54
- for f in sorted(os.listdir(PERSONAL_PLUGINS_DIR)):
55
- if f.endswith(".py") and f != "__init__.py":
56
- source = "personal (override)" if f in plugin_map else "personal"
57
- plugin_map[f] = (PERSONAL_PLUGINS_DIR, source)
58
-
59
- # Load all in sorted order
60
- for f in sorted(plugin_map):
61
- plugins_dir, source_label = plugin_map[f]
62
- try:
63
- old_handler = signal.signal(signal.SIGALRM, _timeout_handler)
64
- signal.alarm(PLUGIN_LOAD_TIMEOUT)
65
- try:
66
- n = load_plugin(mcp, f, plugins_dir=plugins_dir)
67
- total += n
68
- print(f"[PLUGIN LOADED] {f} ({n} tools) from {source_label}: {plugins_dir}", file=sys.stderr)
69
- finally:
70
- signal.alarm(0)
71
- signal.signal(signal.SIGALRM, old_handler)
72
- except _PluginTimeout:
73
- print(f"[PLUGIN TIMEOUT] {f}: skipped after {PLUGIN_LOAD_TIMEOUT}s", file=sys.stderr)
74
- except Exception as e:
75
- print(f"[PLUGIN ERROR] {f}: {e}", file=sys.stderr)
76
- return total
77
-
78
-
79
- def load_plugin(mcp, filename: str, plugins_dir: str | None = None) -> int:
80
- """Load or reload a single plugin. Returns number of tools registered.
81
-
82
- Args:
83
- plugins_dir: Directory to load from. If None, searches repo PLUGINS_DIR first,
84
- then PERSONAL_PLUGINS_DIR. Personal plugins are loaded via
85
- importlib.util.spec_from_file_location.
86
- """
87
- if not filename.endswith(".py"):
88
- filename += ".py"
89
-
90
- # Reject path separators and traversal sequences before joining
91
- if "/" in filename or "\\" in filename or ".." in filename:
92
- raise ValueError(f"Invalid plugin filename (path separators or '..' not allowed): {filename}")
93
-
94
- if plugins_dir is not None:
95
- filepath = os.path.join(plugins_dir, filename)
96
- if not os.path.isfile(filepath):
97
- raise FileNotFoundError(f"Plugin not found: {filepath}")
98
- else:
99
- # Search repo first, then personal
100
- repo_path = os.path.join(PLUGINS_DIR, filename)
101
- personal_path = os.path.join(PERSONAL_PLUGINS_DIR, filename)
102
- if os.path.isfile(repo_path):
103
- plugins_dir = PLUGINS_DIR
104
- filepath = repo_path
105
- elif os.path.isfile(personal_path):
106
- plugins_dir = PERSONAL_PLUGINS_DIR
107
- filepath = personal_path
108
- else:
109
- raise FileNotFoundError(
110
- f"Plugin not found in repo ({PLUGINS_DIR}) or personal ({PERSONAL_PLUGINS_DIR}): {filename}"
111
- )
112
-
113
- # Security: reject path traversal — resolved path must stay inside allowed directories
114
- real_path = os.path.realpath(filepath)
115
- real_plugins = os.path.realpath(PLUGINS_DIR)
116
- real_personal = os.path.realpath(PERSONAL_PLUGINS_DIR)
117
- if not (real_path.startswith(real_plugins + os.sep) or real_path.startswith(real_personal + os.sep)):
118
- raise ValueError(
119
- f"Path traversal blocked: {filename!r} resolves to {real_path}, "
120
- f"which is outside {real_plugins} and {real_personal}"
121
- )
122
-
123
- module_name = f"plugins.{filename[:-3]}"
124
-
125
- # For personal plugins (outside repo), use spec_from_file_location
126
- if plugins_dir != PLUGINS_DIR:
127
- _ensure_src_in_path()
128
- spec = importlib.util.spec_from_file_location(module_name, filepath)
129
- if spec is None or spec.loader is None:
130
- raise ImportError(f"Cannot create module spec for {filepath}")
131
- mod = importlib.util.module_from_spec(spec)
132
- sys.modules[module_name] = mod
133
- spec.loader.exec_module(mod)
134
- elif module_name in sys.modules:
135
- mod = importlib.reload(sys.modules[module_name])
136
- else:
137
- mod = importlib.import_module(module_name)
138
-
139
- tools_list = getattr(mod, "TOOLS", [])
140
- tool_names = []
141
-
142
- for func, name, description in tools_list:
143
- try:
144
- mcp.local_provider.remove_tool(name)
145
- except Exception:
146
- pass
147
- t = Tool.from_function(func, name=name, description=description)
148
- mcp.add_tool(t)
149
- tool_names.append(name)
150
-
151
- source_label = "personal" if plugins_dir != PLUGINS_DIR else "repo"
152
- _update_registry(filename, len(tool_names), ",".join(tool_names), source_label)
153
-
154
- return len(tool_names)
155
-
156
-
157
- def remove_plugin(mcp, filename: str) -> list[str]:
158
- """Unregister a plugin's tools from MCP and clean the registry.
159
-
160
- Does NOT delete plugin files — only unregisters tools to avoid
161
- accidental deletion of code from repo or personal directories.
162
- """
163
- if not filename.endswith(".py"):
164
- filename += ".py"
165
-
166
- conn = get_db()
167
- row = conn.execute("SELECT tool_names FROM plugins WHERE filename = ?", (filename,)).fetchone()
168
-
169
- removed = []
170
- if row and row["tool_names"]:
171
- for name in row["tool_names"].split(","):
172
- name = name.strip()
173
- if name:
174
- try:
175
- mcp.local_provider.remove_tool(name)
176
- removed.append(name)
177
- except Exception:
178
- pass
179
-
180
- module_name = f"plugins.{filename[:-3]}"
181
- sys.modules.pop(module_name, None)
182
-
183
- conn = get_db()
184
- conn.execute("DELETE FROM plugins WHERE filename = ?", (filename,))
185
- conn.commit()
186
-
187
- return removed
188
-
189
-
190
- def list_plugins() -> list[dict]:
191
- """List all registered plugins with source info (repo/personal)."""
192
- conn = get_db()
193
- rows = conn.execute(
194
- "SELECT filename, tools_count, tool_names, loaded_at, created_by FROM plugins ORDER BY filename"
195
- ).fetchall()
196
- result = []
197
- for r in rows:
198
- d = dict(r)
199
- d["source"] = d.get("created_by", "repo")
200
- result.append(d)
201
- return result
202
-
203
-
204
- def _update_registry(filename: str, tools_count: int, tool_names: str, created_by: str):
205
- """Insert or update plugin registry entry. Non-fatal on lock — tools still work."""
206
- now = time.time()
207
- try:
208
- conn = get_db()
209
- conn.execute(
210
- "INSERT INTO plugins (filename, tools_count, tool_names, loaded_at, created_by) "
211
- "VALUES (?, ?, ?, ?, ?) "
212
- "ON CONFLICT(filename) DO UPDATE SET tools_count=?, tool_names=?, loaded_at=?, created_by=?",
213
- (filename, tools_count, tool_names, now, created_by, tools_count, tool_names, now, created_by),
214
- )
215
- conn.commit()
216
- except Exception as e:
217
- print(f"[PLUGIN REGISTRY] Skipped update for {filename}: {e}")
File without changes
@@ -1,450 +0,0 @@
1
- """Artifact Registry plugin — structured index of things NEXO creates/deploys.
2
-
3
- Solves 'recent work amnesia': NEXO builds services, dashboards, scripts, APIs
4
- but can't find them hours later because semantic search ('backend') doesn't
5
- match operational terms ('FastAPI localhost:6174').
6
-
7
- Architecture (from 3-way AI debate — GPT-5.4 + Gemini 3.1 Pro + Claude Opus 4.6):
8
- 1. Structured SQLite table with aliases, ports, paths, run commands
9
- 2. Retrieval ladder: exact alias → port/path match → fuzzy token → semantic fallback
10
- 3. User-language alias learning: when the user says 'backend' and it resolves
11
- to dashboard:6174, store that mapping for O(1) next time
12
- 4. Temporal filtering: 'last night' → hard SQL constraint before any search
13
- """
14
-
15
- import json
16
- import datetime
17
- from db import get_db
18
-
19
-
20
- # Valid artifact kinds
21
- VALID_KINDS = {
22
- 'service', 'dashboard', 'script', 'api', 'cron', 'website',
23
- 'database', 'repo', 'config', 'tool', 'plugin', 'other',
24
- }
25
-
26
- VALID_STATES = {'active', 'inactive', 'broken', 'archived'}
27
-
28
-
29
- def _cognitive_ingest_safe(content, source_type, source_id="", source_title="", domain=""):
30
- """Ingest to cognitive STM. Silently fails if cognitive engine unavailable."""
31
- try:
32
- import cognitive
33
- cognitive.ingest(content, source_type, source_id, source_title, domain)
34
- except Exception:
35
- pass
36
-
37
-
38
- def handle_artifact_create(
39
- kind: str,
40
- canonical_name: str,
41
- aliases: str = '[]',
42
- description: str = '',
43
- uri: str = '',
44
- ports: str = '[]',
45
- paths: str = '[]',
46
- run_cmd: str = '',
47
- repo: str = '',
48
- domain: str = '',
49
- session_id: str = '',
50
- metadata: str = '{}',
51
- ) -> str:
52
- """Register a new artifact (service, dashboard, script, API, etc.).
53
-
54
- Call this whenever NEXO creates, deploys, or discovers a runnable/accessible artifact.
55
-
56
- Args:
57
- kind: Type — service, dashboard, script, api, cron, website, database, repo, config, tool, plugin, other
58
- canonical_name: Primary name (e.g., 'NEXO Brain Dashboard')
59
- aliases: JSON array of alternative names users might use (e.g., '["backend", "dashboard", "nexo web"]')
60
- description: What it does (1-2 sentences)
61
- uri: Access URL or address (e.g., 'localhost:6174', 'nexo-brain.com')
62
- ports: JSON array of ports (e.g., '[6174]')
63
- paths: JSON array of file paths (e.g., '["/Users/x/nexo/src/dashboard/app.py"]')
64
- run_cmd: Command to start/open it (e.g., 'python3 -m dashboard.app --port 6174')
65
- repo: Repository path or URL
66
- domain: Project domain (nexo, my-project, project-a, project-b, etc.)
67
- session_id: Current session ID
68
- metadata: JSON object with extra key-value pairs
69
- """
70
- if kind not in VALID_KINDS:
71
- return f"ERROR: kind must be one of: {', '.join(sorted(VALID_KINDS))}"
72
-
73
- # Parse aliases
74
- try:
75
- alias_list = json.loads(aliases) if aliases and aliases != '[]' else []
76
- except (json.JSONDecodeError, TypeError):
77
- alias_list = [a.strip() for a in aliases.split(',') if a.strip()]
78
-
79
- conn = get_db()
80
- cur = conn.execute(
81
- """INSERT INTO artifact_registry
82
- (kind, canonical_name, aliases, description, uri, ports, paths,
83
- run_cmd, repo, domain, state, session_id, metadata)
84
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'active', ?, ?)""",
85
- (kind, canonical_name, json.dumps(alias_list), description, uri, ports,
86
- paths, run_cmd, repo, domain, session_id, metadata),
87
- )
88
- artifact_id = cur.lastrowid
89
- conn.commit()
90
-
91
- # Insert aliases into lookup table
92
- for alias in alias_list + [canonical_name.lower()]:
93
- alias_clean = alias.strip().lower()
94
- if alias_clean:
95
- try:
96
- conn.execute(
97
- "INSERT OR IGNORE INTO artifact_aliases (artifact_id, phrase, source) VALUES (?, ?, 'create')",
98
- (artifact_id, alias_clean),
99
- )
100
- except Exception:
101
- pass
102
- conn.commit()
103
-
104
- # Ingest to cognitive memory
105
- content = f"Artifact: {canonical_name} ({kind}). {description}. URI: {uri}. Aliases: {', '.join(alias_list)}"
106
- _cognitive_ingest_safe(content, "artifact", f"A{artifact_id}", canonical_name[:80], domain)
107
-
108
- return f"Artifact #{artifact_id} created: {canonical_name} ({kind}) — {uri or 'no URI'}"
109
-
110
-
111
- def handle_artifact_find(query: str, kind: str = '', state: str = 'active') -> str:
112
- """Find artifacts using the retrieval ladder: exact alias → port/path → fuzzy token → all recent.
113
-
114
- This is the PRIMARY retrieval tool. Use it when the user references something
115
- they or NEXO built/deployed/created. Designed for natural language like
116
- 'the backend', 'that script from yesterday', 'localhost something'.
117
-
118
- Args:
119
- query: What to search for — name, alias, port, path, or description fragment
120
- kind: Filter by kind (optional)
121
- state: Filter by state — default 'active'. Use 'all' for everything.
122
- """
123
- conn = get_db()
124
- results = []
125
- query_lower = query.strip().lower()
126
-
127
- state_filter = "AND state = ?" if state != 'all' else ""
128
- state_params = (state,) if state != 'all' else ()
129
-
130
- kind_filter = "AND kind = ?" if kind else ""
131
- kind_params = (kind,) if kind else ()
132
-
133
- extra_filters = state_filter + " " + kind_filter
134
- extra_params = state_params + kind_params
135
-
136
- # --- STAGE 1: Exact alias match (fastest, O(1)) ---
137
- rows = conn.execute(
138
- f"""SELECT DISTINCT r.* FROM artifact_registry r
139
- JOIN artifact_aliases a ON a.artifact_id = r.id
140
- WHERE a.phrase = ? {extra_filters}
141
- ORDER BY r.last_touched_at DESC LIMIT 5""",
142
- (query_lower,) + extra_params,
143
- ).fetchall()
144
- if rows:
145
- results = [dict(r) for r in rows]
146
- return _format_results(results, "alias match", query)
147
-
148
- # --- STAGE 2: Port or URI match ---
149
- rows = conn.execute(
150
- f"""SELECT * FROM artifact_registry
151
- WHERE (uri LIKE ? OR ports LIKE ?) {extra_filters}
152
- ORDER BY last_touched_at DESC LIMIT 5""",
153
- (f"%{query_lower}%", f"%{query_lower}%") + extra_params,
154
- ).fetchall()
155
- if rows:
156
- results = [dict(r) for r in rows]
157
- return _format_results(results, "URI/port match", query)
158
-
159
- # --- STAGE 3: Path match ---
160
- rows = conn.execute(
161
- f"""SELECT * FROM artifact_registry
162
- WHERE paths LIKE ? {extra_filters}
163
- ORDER BY last_touched_at DESC LIMIT 5""",
164
- (f"%{query_lower}%",) + extra_params,
165
- ).fetchall()
166
- if rows:
167
- results = [dict(r) for r in rows]
168
- return _format_results(results, "path match", query)
169
-
170
- # --- STAGE 4: Fuzzy token match on name, description, aliases ---
171
- tokens = query_lower.split()
172
- if tokens:
173
- conditions = " AND ".join(
174
- "(LOWER(canonical_name) LIKE ? OR LOWER(description) LIKE ? OR LOWER(aliases) LIKE ?)"
175
- for _ in tokens
176
- )
177
- params = []
178
- for t in tokens:
179
- p = f"%{t}%"
180
- params.extend([p, p, p])
181
- rows = conn.execute(
182
- f"""SELECT * FROM artifact_registry
183
- WHERE {conditions} {extra_filters}
184
- ORDER BY last_touched_at DESC LIMIT 10""",
185
- tuple(params) + extra_params,
186
- ).fetchall()
187
- if rows:
188
- results = [dict(r) for r in rows]
189
- return _format_results(results, "token match", query)
190
-
191
- # --- STAGE 5: Recent artifacts (last 72h) as fallback ---
192
- cutoff = (datetime.datetime.now() - datetime.timedelta(hours=72)).isoformat()
193
- rows = conn.execute(
194
- f"""SELECT * FROM artifact_registry
195
- WHERE last_touched_at >= ? {extra_filters}
196
- ORDER BY last_touched_at DESC LIMIT 10""",
197
- (cutoff,) + extra_params,
198
- ).fetchall()
199
- if rows:
200
- results = [dict(r) for r in rows]
201
- return _format_results(results, "recent (72h)", query)
202
-
203
- return f"No artifacts found for '{query}'. Use artifact_list to see all registered artifacts."
204
-
205
-
206
- def handle_artifact_update(
207
- id: int,
208
- canonical_name: str = '',
209
- aliases: str = '',
210
- description: str = '',
211
- uri: str = '',
212
- ports: str = '',
213
- paths: str = '',
214
- run_cmd: str = '',
215
- state: str = '',
216
- domain: str = '',
217
- metadata: str = '',
218
- ) -> str:
219
- """Update an artifact. Only non-empty fields are changed.
220
-
221
- Args:
222
- id: Artifact ID to update
223
- canonical_name: New primary name
224
- aliases: New JSON array of aliases (replaces existing)
225
- description: New description
226
- uri: New URI
227
- ports: New ports JSON array
228
- paths: New paths JSON array
229
- run_cmd: New run command
230
- state: New state (active, inactive, broken, archived)
231
- domain: New domain
232
- metadata: New metadata JSON (merged with existing)
233
- """
234
- conn = get_db()
235
- row = conn.execute("SELECT * FROM artifact_registry WHERE id = ?", (id,)).fetchone()
236
- if not row:
237
- return f"ERROR: Artifact #{id} not found."
238
-
239
- updates = []
240
- params = []
241
-
242
- if canonical_name:
243
- updates.append("canonical_name = ?"); params.append(canonical_name)
244
- if description:
245
- updates.append("description = ?"); params.append(description)
246
- if uri:
247
- updates.append("uri = ?"); params.append(uri)
248
- if ports:
249
- updates.append("ports = ?"); params.append(ports)
250
- if paths:
251
- updates.append("paths = ?"); params.append(paths)
252
- if run_cmd:
253
- updates.append("run_cmd = ?"); params.append(run_cmd)
254
- if domain:
255
- updates.append("domain = ?"); params.append(domain)
256
- if state:
257
- if state not in VALID_STATES:
258
- return f"ERROR: state must be one of: {', '.join(sorted(VALID_STATES))}"
259
- updates.append("state = ?"); params.append(state)
260
- if metadata:
261
- try:
262
- existing = json.loads(row["metadata"] or '{}')
263
- new = json.loads(metadata)
264
- existing.update(new)
265
- updates.append("metadata = ?"); params.append(json.dumps(existing))
266
- except (json.JSONDecodeError, TypeError):
267
- pass
268
-
269
- if aliases:
270
- try:
271
- alias_list = json.loads(aliases) if aliases.startswith('[') else [a.strip() for a in aliases.split(',')]
272
- except (json.JSONDecodeError, TypeError):
273
- alias_list = [a.strip() for a in aliases.split(',')]
274
- updates.append("aliases = ?"); params.append(json.dumps(alias_list))
275
- # Rebuild alias lookup table
276
- conn.execute("DELETE FROM artifact_aliases WHERE artifact_id = ?", (id,))
277
- for alias in alias_list:
278
- alias_clean = alias.strip().lower()
279
- if alias_clean:
280
- conn.execute(
281
- "INSERT OR IGNORE INTO artifact_aliases (artifact_id, phrase, source) VALUES (?, ?, 'update')",
282
- (id, alias_clean),
283
- )
284
-
285
- if not updates:
286
- return "Nothing to update."
287
-
288
- updates.append("last_touched_at = datetime('now')")
289
- params.append(id)
290
- conn.execute(f"UPDATE artifact_registry SET {', '.join(updates)} WHERE id = ?", tuple(params))
291
- conn.commit()
292
- return f"Artifact #{id} updated."
293
-
294
-
295
- def handle_artifact_learn_alias(id: int, phrase: str) -> str:
296
- """Learn a new alias from user language. Call this when the user refers to an
297
- artifact with a term not yet registered (e.g., the user says 'backend' for dashboard:6174).
298
-
299
- Args:
300
- id: Artifact ID
301
- phrase: The user's term (e.g., 'backend', 'that api thing')
302
- """
303
- conn = get_db()
304
- row = conn.execute("SELECT * FROM artifact_registry WHERE id = ?", (id,)).fetchone()
305
- if not row:
306
- return f"ERROR: Artifact #{id} not found."
307
-
308
- phrase_clean = phrase.strip().lower()
309
- if not phrase_clean:
310
- return "ERROR: Empty phrase."
311
-
312
- # Add to alias lookup table
313
- conn.execute(
314
- "INSERT OR IGNORE INTO artifact_aliases (artifact_id, phrase, source) VALUES (?, ?, 'user_language')",
315
- (id, phrase_clean),
316
- )
317
-
318
- # Also add to the artifact's aliases JSON array
319
- try:
320
- existing = json.loads(row["aliases"] or '[]')
321
- except (json.JSONDecodeError, TypeError):
322
- existing = []
323
- if phrase_clean not in [a.lower() for a in existing]:
324
- existing.append(phrase_clean)
325
- conn.execute(
326
- "UPDATE artifact_registry SET aliases = ?, last_touched_at = datetime('now') WHERE id = ?",
327
- (json.dumps(existing), id),
328
- )
329
-
330
- conn.commit()
331
- return f"Alias '{phrase_clean}' learned for artifact #{id} ({row['canonical_name']})."
332
-
333
-
334
- def handle_artifact_list(kind: str = '', state: str = 'active', recent_hours: int = 0) -> str:
335
- """List all artifacts, optionally filtered.
336
-
337
- Args:
338
- kind: Filter by kind (service, dashboard, script, etc.)
339
- state: Filter by state — 'active' (default), 'all', 'inactive', 'broken', 'archived'
340
- recent_hours: If >0, only show artifacts touched in the last N hours
341
- """
342
- conn = get_db()
343
- conditions = []
344
- params = []
345
-
346
- if state != 'all':
347
- conditions.append("state = ?"); params.append(state)
348
- if kind:
349
- conditions.append("kind = ?"); params.append(kind)
350
- if recent_hours > 0:
351
- cutoff = (datetime.datetime.now() - datetime.timedelta(hours=recent_hours)).isoformat()
352
- conditions.append("last_touched_at >= ?"); params.append(cutoff)
353
-
354
- where = "WHERE " + " AND ".join(conditions) if conditions else ""
355
- rows = conn.execute(
356
- f"SELECT * FROM artifact_registry {where} ORDER BY last_touched_at DESC",
357
- tuple(params),
358
- ).fetchall()
359
-
360
- if not rows:
361
- filters = []
362
- if kind: filters.append(f"kind={kind}")
363
- if state != 'all': filters.append(f"state={state}")
364
- if recent_hours: filters.append(f"last {recent_hours}h")
365
- return f"No artifacts found{' (' + ', '.join(filters) + ')' if filters else ''}."
366
-
367
- lines = [f"ARTIFACT REGISTRY ({len(rows)}):"]
368
- for r in rows:
369
- r = dict(r)
370
- aliases_str = ""
371
- try:
372
- aliases = json.loads(r.get("aliases", "[]"))
373
- if aliases:
374
- aliases_str = f" aka [{', '.join(aliases[:3])}]"
375
- except (json.JSONDecodeError, TypeError):
376
- pass
377
- uri_str = f" → {r['uri']}" if r.get("uri") else ""
378
- cmd_str = f" | cmd: {r['run_cmd'][:60]}" if r.get("run_cmd") else ""
379
- touched = r.get("last_touched_at", "")[:16]
380
- lines.append(
381
- f" #{r['id']} [{r['kind']}] {r['canonical_name']}{aliases_str}{uri_str}{cmd_str} "
382
- f"({r['state']}, {touched})"
383
- )
384
- return "\n".join(lines)
385
-
386
-
387
- def handle_artifact_delete(id: int) -> str:
388
- """Delete an artifact from the registry.
389
-
390
- Args:
391
- id: Artifact ID to delete
392
- """
393
- conn = get_db()
394
- row = conn.execute("SELECT canonical_name FROM artifact_registry WHERE id = ?", (id,)).fetchone()
395
- if not row:
396
- return f"ERROR: Artifact #{id} not found."
397
- name = row["canonical_name"]
398
- conn.execute("DELETE FROM artifact_aliases WHERE artifact_id = ?", (id,))
399
- conn.execute("DELETE FROM artifact_registry WHERE id = ?", (id,))
400
- conn.commit()
401
- return f"Artifact #{id} ({name}) deleted."
402
-
403
-
404
- def _format_results(results, method, query):
405
- """Format search results for display."""
406
- lines = [f"ARTIFACTS FOUND ({len(results)}, via {method} for '{query}'):"]
407
- for r in results:
408
- aliases_str = ""
409
- try:
410
- aliases = json.loads(r.get("aliases", "[]"))
411
- if aliases:
412
- aliases_str = f" aka [{', '.join(aliases[:4])}]"
413
- except (json.JSONDecodeError, TypeError):
414
- pass
415
- uri_str = f" → {r['uri']}" if r.get("uri") else ""
416
- cmd_str = f"\n Run: {r['run_cmd']}" if r.get("run_cmd") else ""
417
- paths_str = ""
418
- try:
419
- paths = json.loads(r.get("paths", "[]"))
420
- if paths:
421
- paths_str = f"\n Paths: {', '.join(paths[:3])}"
422
- except (json.JSONDecodeError, TypeError):
423
- pass
424
- touched = r.get("last_touched_at", "")[:16]
425
- lines.append(
426
- f" #{r['id']} [{r['kind']}] {r['canonical_name']}{aliases_str}{uri_str} "
427
- f"({r['state']}, {touched}){cmd_str}{paths_str}"
428
- )
429
- return "\n".join(lines)
430
-
431
-
432
- # Plugin registration — TOOLS array consumed by plugin_loader.py
433
- TOOLS = [
434
- (handle_artifact_create, "nexo_artifact_create",
435
- "Register a new artifact (service, dashboard, script, API, etc.) in the Artifact Registry. "
436
- "Call this whenever NEXO creates, deploys, or discovers a runnable/accessible artifact."),
437
- (handle_artifact_find, "nexo_artifact_find",
438
- "Find artifacts using the retrieval ladder: exact alias → port/path → fuzzy token → recent. "
439
- "PRIMARY retrieval tool for when users reference something built/deployed. Handles natural "
440
- "language like 'the backend', 'that script', 'localhost something'."),
441
- (handle_artifact_update, "nexo_artifact_update",
442
- "Update an existing artifact. Only non-empty fields are changed."),
443
- (handle_artifact_learn_alias, "nexo_artifact_learn_alias",
444
- "Learn a new alias from user language. Call when the user refers to an artifact with "
445
- "an unregistered term (e.g., 'backend' for the NEXO Brain Dashboard)."),
446
- (handle_artifact_list, "nexo_artifact_list",
447
- "List all registered artifacts, optionally filtered by kind, state, or recency."),
448
- (handle_artifact_delete, "nexo_artifact_delete",
449
- "Delete an artifact from the registry."),
450
- ]