-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.py
More file actions
260 lines (220 loc) · 12.3 KB
/
Copy pathinstall.py
File metadata and controls
260 lines (220 loc) · 12.3 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
"""
MetaOps — local installer (run inside the cloned repo)
Usage: cd MetaOps && python install.py
"""
import sys
import shutil
import subprocess
import sqlite3
from pathlib import Path
# Force UTF-8 on Windows consoles
if sys.platform == "win32":
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
# ── Resolve project root ONCE ────────────────────────────────────────────────
PROJECT_ROOT = Path(__file__).resolve().parent
DATA_DIR = PROJECT_ROOT / ".data"
ENV_FILE = PROJECT_ROOT / ".env"
ENV_EXAMPLE = PROJECT_ROOT / ".env.example"
# ── Colors ────────────────────────────────────────────────────────────────────
OK = "\033[92m[OK]\033[0m"
FAIL = "\033[91m[FAIL]\033[0m"
WARN = "\033[93m[WARN]\033[0m"
INFO = "\033[94m[-->]\033[0m"
BOLD = "\033[1m"
RESET = "\033[0m"
def ok(msg):
print(f" {OK} {msg}")
def fail(msg):
print(f" {FAIL} {msg}")
sys.exit(1)
def warn(msg):
print(f" {WARN} {msg}")
def info(msg):
print(f" {INFO} {msg}")
def header(msg):
print(f"\n{BOLD}{msg}{RESET}")
# ── 1. Python version ─────────────────────────────────────────────────────────
header("1 / 7 Python version")
major, minor = sys.version_info[:2]
if (major, minor) < (3, 10):
fail(f"Python 3.10+ required — found {major}.{minor}")
ok(f"Python {major}.{minor}")
# ── 2. Installer (uv preferred, pip fallback) ─────────────────────────────────
header("2 / 7 Installer")
uv = shutil.which("uv")
if not uv:
info("uv not found — installing it via pip (uv is much faster)...")
subprocess.run(
[sys.executable, "-m", "pip", "install", "uv", "--quiet"], capture_output=True
)
uv = shutil.which("uv")
if uv:
ok(f"uv: {uv}")
install_cmd = [uv, "pip", "install", "--python", sys.executable]
else:
pip = shutil.which("pip") or shutil.which("pip3")
if not pip:
fail("Neither uv nor pip found — install one first")
warn(f"uv unavailable — falling back to pip: {pip}")
install_cmd = [sys.executable, "-m", "pip", "install", "--quiet"]
# ── 3. Install MetaOps package ────────────────────────────────────────────────
header("3 / 7 Installing MetaOps + dependencies")
info(f"Running: {' '.join(install_cmd)} -e .")
result = subprocess.run(
install_cmd + ["-e", ".", "--reinstall"],
capture_output=True,
text=True,
cwd=str(PROJECT_ROOT),
)
if result.returncode != 0:
print(result.stderr[-2000:])
fail("Installation failed — see errors above")
ok("MetaOps installed")
# ── 4. Optional audit tools ───────────────────────────────────────────────────
header("4 / 7 Optional tools (audit workflow)")
for tool, package in [("bandit", "bandit"), ("pip-audit", "pip-audit")]:
if shutil.which(tool):
ok(f"{tool} already installed")
else:
info(f"Installing {package}...")
r = subprocess.run(install_cmd + [package], capture_output=True, text=True)
if r.returncode == 0:
ok(f"{tool} installed")
else:
warn(f"{tool} could not be installed — audit scans will skip it")
# ── 5. Node MCP servers ────────────────────────────────────────────────────────
header("5 / 7 Node MCP servers")
npm = shutil.which("npm")
if npm:
info("Installing local MCP servers via npm (filesystem, memory)...")
r = subprocess.run(
[
npm,
"install",
"@modelcontextprotocol/server-filesystem",
"@modelcontextprotocol/server-memory",
"--quiet",
],
cwd=str(PROJECT_ROOT),
)
if r.returncode == 0:
ok("MCP servers installed locally")
else:
warn(
"Failed to install local MCP servers via npm — npx fallback will be used at runtime"
)
else:
warn("npm not found — skipping local MCP server installation")
# ── 6. Data directories ───────────────────────────────────────────────────────
header("6 / 7 Data directories")
DATA_DIR.mkdir(parents=True, exist_ok=True)
(DATA_DIR / "artifacts").mkdir(parents=True, exist_ok=True)
ok(f"{DATA_DIR} ready")
# Initialize database so first launch is instant
# Schemas must match database.py exactly
for db_name, schemas in [
(
"metaops.db",
[
# Skills (L1/L2/L3 structure)
"CREATE TABLE IF NOT EXISTS skills (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT UNIQUE NOT NULL, description TEXT NOT NULL, instructions TEXT NOT NULL, status TEXT NOT NULL DEFAULT 'pending_review', version INTEGER NOT NULL DEFAULT 1, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME DEFAULT CURRENT_TIMESTAMP)",
"CREATE TABLE IF NOT EXISTS skill_resources (id INTEGER PRIMARY KEY AUTOINCREMENT, skill_name TEXT NOT NULL, path TEXT NOT NULL, content TEXT NOT NULL, FOREIGN KEY (skill_name) REFERENCES skills(name) ON DELETE CASCADE, UNIQUE(skill_name, path))",
# RAG sources
"CREATE TABLE IF NOT EXISTS rag_sources (file_path TEXT PRIMARY KEY, filename TEXT NOT NULL, description TEXT, global_context TEXT, file_size INTEGER NOT NULL, chunk_count INTEGER NOT NULL, timestamp DATETIME DEFAULT CURRENT_TIMESTAMP)",
# Observability logs
"CREATE TABLE IF NOT EXISTS llm_calls (id TEXT PRIMARY KEY, timestamp DATETIME DEFAULT CURRENT_TIMESTAMP, session_id TEXT, role TEXT, provider TEXT NOT NULL, model TEXT NOT NULL, prompt TEXT, completion TEXT, prompt_tokens INTEGER, completion_tokens INTEGER, total_tokens INTEGER, latency_ms INTEGER, status_code INTEGER, error_message TEXT)",
"CREATE TABLE IF NOT EXISTS subagent_logs (id TEXT PRIMARY KEY, timestamp DATETIME DEFAULT CURRENT_TIMESTAMP, session_id TEXT, parent_agent TEXT NOT NULL, subagent_name TEXT NOT NULL, query TEXT, response TEXT, prompt_tokens INTEGER, completion_tokens INTEGER, total_tokens INTEGER, latency_ms INTEGER, status TEXT)",
# Autonomous loop context (must match database.py exactly)
"CREATE TABLE IF NOT EXISTS loop_context (id TEXT PRIMARY KEY, timestamp DATETIME DEFAULT CURRENT_TIMESTAMP, session_id TEXT, loop_type TEXT NOT NULL, query TEXT NOT NULL, result TEXT, iterations INTEGER DEFAULT 0, approved INTEGER DEFAULT 0, prompt_tokens INTEGER DEFAULT 0, completion_tokens INTEGER DEFAULT 0, total_tokens INTEGER DEFAULT 0, latency_ms INTEGER DEFAULT 0, status TEXT DEFAULT 'running', app_name TEXT, user_id TEXT, plan_json TEXT, todo_list TEXT, todo_done INTEGER DEFAULT 0, todo_total INTEGER DEFAULT 0, started_at DATETIME, completed_at DATETIME, error_message TEXT, assigned_to TEXT)",
# Checkpoints (crash recovery)
"CREATE TABLE IF NOT EXISTS checkpoints (session_key TEXT PRIMARY KEY, data TEXT NOT NULL, updated_at DATETIME DEFAULT CURRENT_TIMESTAMP)",
# Free models catalog
"CREATE TABLE IF NOT EXISTS free_models (id TEXT NOT NULL, provider TEXT NOT NULL, source TEXT NOT NULL, description TEXT, context_window INTEGER, max_completion_tokens INTEGER, pricing_prompt REAL, pricing_completion REAL, last_seen DATETIME DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id, provider, source))",
# Memory stores (persistent user preferences)
"CREATE TABLE IF NOT EXISTS memory_stores (id TEXT PRIMARY KEY, user_id TEXT NOT NULL, store_name TEXT NOT NULL, key TEXT NOT NULL, value TEXT NOT NULL, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME DEFAULT CURRENT_TIMESTAMP, UNIQUE(user_id, store_name, key))",
"CREATE INDEX IF NOT EXISTS idx_memory_stores_user ON memory_stores(user_id, store_name)",
"CREATE INDEX IF NOT EXISTS idx_free_models_provider ON free_models(provider)",
"CREATE INDEX IF NOT EXISTS idx_llm_calls_session_id ON llm_calls(session_id)",
"CREATE INDEX IF NOT EXISTS idx_llm_calls_role ON llm_calls(role)",
"CREATE INDEX IF NOT EXISTS idx_llm_calls_timestamp ON llm_calls(timestamp)",
"CREATE INDEX IF NOT EXISTS idx_subagent_logs_session_id ON subagent_logs(session_id)",
"CREATE INDEX IF NOT EXISTS idx_subagent_logs_subagent ON subagent_logs(subagent_name)",
"CREATE INDEX IF NOT EXISTS idx_subagent_logs_timestamp ON subagent_logs(timestamp)",
"CREATE INDEX IF NOT EXISTS idx_loop_context_loop_type ON loop_context(loop_type)",
"CREATE INDEX IF NOT EXISTS idx_loop_context_session_id ON loop_context(session_id)",
"CREATE INDEX IF NOT EXISTS idx_loop_context_timestamp ON loop_context(timestamp)",
],
),
]:
db_path = DATA_DIR / db_name
if not db_path.exists():
conn = sqlite3.connect(str(db_path))
for stmt in schemas:
conn.execute(stmt)
conn.commit()
conn.close()
ok(f"{db_name} created")
# ChromaDB — create via venv Python
chroma_dir = DATA_DIR / "metaops_vector_db"
chroma_dir.mkdir(parents=True, exist_ok=True)
r = subprocess.run(
[
sys.executable,
"-c",
f"""
import chromadb
from pathlib import Path
db = Path('{chroma_dir}')
db.mkdir(parents=True, exist_ok=True)
c = chromadb.PersistentClient(path=str(db))
for n in ["episodic_memory", "semantic_memory", "procedural_memory"]:
c.get_or_create_collection(n)
print("OK")
""",
],
capture_output=True,
text=True,
)
if r.returncode == 0 and "OK" in r.stdout:
ok("ChromaDB vector collections created")
else:
warn(f"ChromaDB init: {r.stderr.strip()[:200]}")
# ── 7. .env setup ─────────────────────────────────────────────────────────────
header("7 / 7 Environment configuration")
if ENV_FILE.exists():
ok(".env already exists — not overwritten")
elif ENV_EXAMPLE.exists():
shutil.copy(ENV_EXAMPLE, ENV_FILE)
warn(".env created from .env.example — fill in your API keys before starting")
else:
warn("No .env found — create one from .env.example before starting")
# ── Shell detection ───────────────────────────────────────────────────────────
shell = shutil.which("bash") or shutil.which("powershell") or shutil.which("cmd")
if shell:
ok(f"Shell detected: {shell}")
else:
warn("No shell found (bash/powershell/cmd) — terminal tools will not work")
# ── Smoke test ────────────────────────────────────────────────────────────────
header("Smoke test")
info("Verifying install in a fresh Python process...")
r = subprocess.run(
[sys.executable, "-c", "from metaops.config import MetaOpsConfig; MetaOpsConfig()"],
capture_output=True,
text=True,
cwd=str(PROJECT_ROOT),
)
if r.returncode != 0:
print(r.stderr.strip())
fail("Import check failed — see errors above")
ok("MetaOps imports correctly")
# ── Done ──────────────────────────────────────────────────────────────────────
print(f"""
{BOLD}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
MetaOps is ready.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━{RESET}
Location : {PROJECT_ROOT}
Next steps:
1. Fill in your API keys in .env
2. (Optional) Edit mcp_servers.json to add MCP servers
3. Start: python -m metaops.main
""")