-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·313 lines (264 loc) · 9.33 KB
/
Copy pathsetup.sh
File metadata and controls
executable file
·313 lines (264 loc) · 9.33 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
#!/bin/bash
# Setup script for SessionFlow on Apple Silicon Mac.
# Single command to install everything: venv, deps, model, and verify.
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
echo "======================================================================"
echo "SessionFlow Setup for Apple Silicon Mac"
echo "======================================================================"
echo ""
# --- Check prerequisites ---
echo "Checking prerequisites..."
if [[ $(uname) != "Darwin" ]]; then
echo "Error: This script is for macOS only"
exit 1
fi
if [[ $(uname -m) != "arm64" ]]; then
echo "Error: This script requires Apple Silicon (M1/M2/M3/M4)"
exit 1
fi
if ! command -v python3 &> /dev/null; then
echo "Error: python3 not found. Install with: brew install python@3.12"
exit 1
fi
PYTHON_VERSION=$(python3 --version | cut -d' ' -f2)
PYTHON_MAJOR_MINOR=$(echo "$PYTHON_VERSION" | cut -d'.' -f1,2)
echo " Python $PYTHON_VERSION"
MACOS_VERSION=$(sw_vers -productVersion | cut -d'.' -f1)
echo " macOS $MACOS_VERSION"
echo ""
# --- Create virtual environment ---
echo "Creating virtual environment..."
if [ -d "venv" ]; then
echo " venv already exists, using existing"
else
python3 -m venv venv
echo " venv created"
fi
source venv/bin/activate
echo ""
echo "Installing dependencies..."
echo " This may take 1-2 minutes on first run..."
pip install --quiet --upgrade pip
echo " Installing Milvus Lite..."
pip install --quiet "setuptools>=70.0,<82.0" "pymilvus[milvus-lite]>=2.6.0"
echo " Installing MLX embeddings..."
pip install --quiet "mlx>=0.30.0" mlx-embeddings "transformers<5.0"
echo " Installing MCP + HTTP server..."
pip install --quiet "mcp>=1.0.0" starlette uvicorn httpx "watchdog>=4.0.0"
echo " All dependencies installed"
# --- Download model ---
echo ""
SESSIONFLOW_MODEL="${SESSIONFLOW_MODEL:-embeddinggemma}"
echo "Downloading embedding model ($SESSIONFLOW_MODEL)..."
chmod +x "$SCRIPT_DIR/download-model.sh"
SESSIONFLOW_MODEL="$SESSIONFLOW_MODEL" "$SCRIPT_DIR/download-model.sh"
# --- Test installation ---
echo ""
echo "Testing installation..."
SESSIONFLOW_MODEL="$SESSIONFLOW_MODEL" python3 << 'PYEOF'
import sys
sys.path.insert(0, '.')
try:
import rag_engine
import transcript_parser
import file_watcher
import tools
print(" All imports successful")
except Exception as e:
print(f" Import failed: {e}")
sys.exit(1)
try:
emb = rag_engine.embed_texts(["test embedding"])
dim = len(emb[0])
expected = rag_engine._EMBED_DIM
assert dim == expected, f"Expected {expected} dims, got {dim}"
print(f" Embedding works ({dim} dimensions, model: {rag_engine.get_model_name()})")
except Exception as e:
print(f" Embedding test failed: {e}")
sys.exit(1)
print("")
print(" All tests passed!")
PYEOF
if [ $? -ne 0 ]; then
echo "Installation test failed"
exit 1
fi
# --- Make scripts executable ---
chmod +x "$SCRIPT_DIR/sessionflow-server.sh"
chmod +x "$SCRIPT_DIR/download-model.sh"
chmod +x "$SCRIPT_DIR/index_hook.py"
# --- Install hooks into ~/.claude/settings.json ---
echo ""
echo "Installing hooks into ~/.claude/settings.json..."
VENV_PYTHON="$SCRIPT_DIR/venv/bin/python"
SETTINGS_FILE="$HOME/.claude/settings.json"
python3 << PYEOF
import json
import os
import sys
settings_file = "$SETTINGS_FILE"
script_dir = "$SCRIPT_DIR"
venv_python = "$VENV_PYTHON"
launcher = os.path.expanduser("~/.sessionflow/sessionflow-launcher.sh")
# Our hooks to install
our_hooks = {
"SessionStart": [
{
"type": "command",
"command": f"{launcher} start",
"timeout": 30000,
},
{
"type": "command",
"command": f"{script_dir}/session_start_hook.sh",
"timeout": 5000,
},
],
"Stop": [
{
"type": "command",
"command": f"{venv_python} {script_dir}/index_hook.py",
"timeout": 15000,
},
],
"PreCompact": [
{
"type": "command",
"command": f"{venv_python} {script_dir}/index_hook.py",
"timeout": 30000,
},
],
}
# Load existing settings
os.makedirs(os.path.dirname(settings_file), exist_ok=True)
if os.path.exists(settings_file):
with open(settings_file) as f:
settings = json.load(f)
else:
settings = {}
hooks = settings.setdefault("hooks", {})
for event, new_hook_entries in our_hooks.items():
# Get or create the event's hook groups array
groups = hooks.setdefault(event, [])
# Find or create a hook group (we use the first group)
if not groups:
groups.append({"hooks": []})
group = groups[0]
existing = group.setdefault("hooks", [])
# Collect existing commands to avoid duplicates
existing_commands = {h.get("command", "") for h in existing}
added = 0
for hook in new_hook_entries:
# Check if already installed (match by command containing our script dir)
cmd = hook["command"]
if cmd not in existing_commands:
# Also check if an older version exists (same script name, different path)
script_name = os.path.basename(cmd.split()[-1])
replaced = False
for i, eh in enumerate(existing):
if script_name in eh.get("command", ""):
existing[i] = hook
replaced = True
break
if not replaced:
existing.append(hook)
added += 1
if added:
print(f" {event}: {added} hook(s) installed")
else:
print(f" {event}: already configured")
with open(settings_file, "w") as f:
json.dump(settings, f, indent=2)
f.write("\n")
print(" Settings saved")
PYEOF
# --- Install global MCP server (user scope) ---
echo ""
echo "Installing global MCP server..."
# Create headersHelper script
MCP_HELPERS_DIR="$HOME/.claude/mcp-helpers"
mkdir -p "$MCP_HELPERS_DIR"
cat > "$MCP_HELPERS_DIR/sessionflow-headers.sh" << 'HELPEREOF'
#!/bin/bash
# Dynamic header helper for SessionFlow MCP server.
# Outputs JSON with X-Project-Root set to the git repo root (or cwd).
PROJECT_ROOT=$(git rev-parse --show-toplevel 2>/dev/null || pwd)
echo "{\"X-Project-Root\": \"$PROJECT_ROOT\"}"
HELPEREOF
chmod +x "$MCP_HELPERS_DIR/sessionflow-headers.sh"
echo " Header helper installed: $MCP_HELPERS_DIR/sessionflow-headers.sh"
# Add MCP server to ~/.claude.json (user scope)
CLAUDE_JSON="$HOME/.claude.json"
python3 << PYEOF
import json
import os
claude_json = "$CLAUDE_JSON"
helpers_dir = "$MCP_HELPERS_DIR"
if os.path.exists(claude_json):
with open(claude_json) as f:
data = json.load(f)
else:
data = {}
servers = data.setdefault("mcpServers", {})
servers["sessionflow"] = {
"type": "http",
"url": "http://127.0.0.1:7102/mcp/",
"headersHelper": f"{helpers_dir}/sessionflow-headers.sh",
}
with open(claude_json, "w") as f:
json.dump(data, f, indent=2)
f.write("\n")
print(" MCP server added to ~/.claude.json (user scope, all projects)")
PYEOF
# --- Optional LaunchAgent (autostart at login) ---
# Off by default. Enable non-interactively with SESSIONFLOW_INSTALL_AGENT=1, or
# answer "y" when prompted in an interactive shell. The LaunchAgent runs
# ~/.sessionflow/sessionflow-launcher.sh run at login, before any harness hooks fire — this
# avoids multiple terminals racing on server start when several harnesses
# (Claude Code, Codex, OpenCode, Antigravity CLI) launch at once.
echo ""
INSTALL_AGENT="${SESSIONFLOW_INSTALL_AGENT:-}"
if [ -z "$INSTALL_AGENT" ] && [ -t 0 ]; then
read -r -p "Install optional macOS LaunchAgent to autostart SessionFlow at login? [y/N] " INSTALL_AGENT || true
fi
case "${INSTALL_AGENT:-n}" in
1|y|Y|yes|YES)
echo "Installing LaunchAgent..."
"$SCRIPT_DIR/sessionflow-server.sh" install-agent || \
echo " LaunchAgent install reported a soft failure — run './sessionflow-server.sh agent-status' to verify."
;;
*)
echo "Skipping LaunchAgent install (optional)."
echo " Install later with: ./sessionflow-server.sh install-agent"
echo " Uninstall with: ./sessionflow-server.sh uninstall-agent"
echo " Inspect with: ./sessionflow-server.sh agent-status"
;;
esac
# --- Done ---
echo ""
echo "======================================================================"
echo "Installation Complete!"
echo "======================================================================"
echo ""
echo "Embedding model: $SESSIONFLOW_MODEL (see rag_engine.py for details)"
echo ""
echo "Hooks installed in: ~/.claude/settings.json"
echo " - SessionStart: auto-start server + register file watcher + backfill"
echo " - Stop: index final turns when session ends"
echo " - PreCompact: index turns before context compaction"
echo ""
echo "MCP server installed globally in: ~/.claude.json"
echo " - Available in all projects automatically"
echo " - Dynamic project root via headersHelper"
echo ""
echo "Next step: Restart Claude Code to activate"
echo ""
echo "Optional autostart (LaunchAgent) commands:"
echo " ./sessionflow-server.sh install-agent # autostart at login"
echo " ./sessionflow-server.sh agent-status # inspect LaunchAgent"
echo " ./sessionflow-server.sh uninstall-agent # remove LaunchAgent"
echo ""
echo "See README.md for full documentation."
echo "======================================================================"