-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·427 lines (385 loc) · 14 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·427 lines (385 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#!/usr/bin/env bash
# persistent-memory installer.
#
# What it does, in order:
# 1. doctor preflight — scans the machine and auto-installs missing prerequisites
# 2. creates .venv (python3.12) and pip-installs the package with [daemon,mcp] extras
# 3. copies skill/SKILL.md into ~/.claude/skills/persistent-memory
# 4. merges the five hooks (UserPromptSubmit/Stop/PreCompact/SessionStart/PreToolUse) into
# ~/.claude/settings.json (idempotent; existing user hooks are preserved; needs jq)
# 5. if the `codex` CLI (or ~/.codex) is detected, mirrors hooks + skill into ~/.codex
# 6. if the `kimi` CLI (or ~/.kimi-code) is detected, merges hooks/skill/MCP into Kimi
# 7. if the `grok` CLI (or ~/.grok) is detected, writes hooks/skill/MCP into ~/.grok
# 8. registers the read-only MCP server with `claude mcp` / `codex mcp` / `grok mcp` when available
# 9. writes a macOS LaunchAgent plist and loads the daemon via launchctl
#
# Assumptions: macOS (launchd step), python3.12 and jq on PATH (doctor installs them),
# Claude Code / Codex / Kimi / Grok optional — missing tools are skipped with a message.
#
# Usage: ./install.sh [--dry-run]
# Env flags: PM_TARGET_HOME (target home dir), PM_SKIP_DOCTOR=1, PM_SKIP_VENV=1,
# PM_SKIP_LAUNCHD=1, PM_SKIP_CODEX=1, PM_SKIP_MCP=1, PM_INSTALL_CODEX=1 (force Codex step),
# PM_SKIP_KIMI=1, PM_INSTALL_KIMI=1 (force Kimi step),
# PM_SKIP_GROK=1, PM_INSTALL_GROK=1 (force Grok step).
set -euo pipefail
DRY_RUN=0
[[ "${1:-}" == "--dry-run" ]] && DRY_RUN=1
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
TARGET_HOME="${PM_TARGET_HOME:-$HOME}"
CLAUDE_DIR="$TARGET_HOME/.claude"
SKILL_DEST="$CLAUDE_DIR/skills/persistent-memory"
SETTINGS_FILE="$CLAUDE_DIR/settings.json"
LAUNCH_AGENTS_DIR="$TARGET_HOME/Library/LaunchAgents"
LOGS_DIR="$TARGET_HOME/Library/Logs/persistent-memory"
PLIST_DEST="$LAUNCH_AGENTS_DIR/com.persistent-memory.daemon.plist"
VENV_DIR="$REPO_ROOT/.venv"
CODEX_DIR="$TARGET_HOME/.codex"
CODEX_HOOKS_FILE="$CODEX_DIR/hooks.json"
CODEX_SKILL_DEST="$CODEX_DIR/skills/persistent-memory"
KIMI_DIR="$TARGET_HOME/.kimi-code"
KIMI_CONFIG="$KIMI_DIR/config.toml"
KIMI_MCP="$KIMI_DIR/mcp.json"
KIMI_SKILL_DEST="$KIMI_DIR/skills/persistent-memory"
GROK_DIR="$TARGET_HOME/.grok"
GROK_HOOKS_FILE="$GROK_DIR/hooks/persistent-memory.json"
GROK_SKILL_DEST="$GROK_DIR/skills/persistent-memory"
GROK_CONFIG="$GROK_DIR/config.toml"
HOOK_EVENTS=("UserPromptSubmit" "Stop" "PreCompact" "SessionStart" "PreToolUse")
say() { echo "$@"; }
plan() { say "DRY-RUN: $*"; }
DOCTOR_MODULE="persistent_memory.doctor"
run_doctor() {
[[ "${PM_SKIP_DOCTOR:-0}" == "1" ]] && return
if [[ $DRY_RUN -eq 1 ]]; then
plan "preflight: PYTHONPATH=$REPO_ROOT/src python3 -m $DOCTOR_MODULE --dry-run"
( cd "$REPO_ROOT" && PYTHONPATH="$REPO_ROOT/src" python3 -m "$DOCTOR_MODULE" --dry-run )
return
fi
( cd "$REPO_ROOT" && PYTHONPATH="$REPO_ROOT/src" python3 -m "$DOCTOR_MODULE" )
}
create_venv() {
if [[ $DRY_RUN -eq 1 ]]; then
plan "create .venv at $VENV_DIR and pip install -e .[daemon,mcp] tomli_w"
return
fi
[[ "${PM_SKIP_VENV:-0}" == "1" ]] && return
[[ -d "$VENV_DIR" ]] || python3.12 -m venv "$VENV_DIR"
"$VENV_DIR/bin/pip" install -e "$REPO_ROOT[daemon,mcp]" tomli_w
}
install_skill() {
if [[ $DRY_RUN -eq 1 ]]; then
plan "copy $REPO_ROOT/skill/SKILL.md to $SKILL_DEST/SKILL.md (skills/persistent-memory)"
return
fi
mkdir -p "$SKILL_DEST"
cp "$REPO_ROOT/skill/SKILL.md" "$SKILL_DEST/SKILL.md"
}
hook_command() {
echo "$VENV_DIR/bin/python -m persistent_memory.hooks.$1"
}
merge_hooks_into() {
local file="$1"
[[ -f "$file" ]] || echo '{}' > "$file"
local tmp; tmp="$(mktemp)"
jq \
--arg ups "$(hook_command user_prompt_submit)" \
--arg stop "$(hook_command stop_or_session_end)" \
--arg pre "$(hook_command pre_compact)" \
--arg ss "$(hook_command session_start)" \
--arg ptu "$(hook_command pre_tool_use)" \
'def upsert(cmd): map(select((.hooks[0].command // "") != cmd)) + [{"hooks":[{"type":"command","command":cmd}]}];
def upsert_matcher(cmd; matcher; tout): map(select((.hooks[0].command // "") != cmd)) + [{"matcher":matcher,"hooks":[{"type":"command","command":cmd,"timeout":tout}]}];
.hooks = (.hooks // {})
| .hooks.UserPromptSubmit = ((.hooks.UserPromptSubmit // []) | upsert($ups))
| .hooks.Stop = ((.hooks.Stop // []) | upsert($stop))
| .hooks.PreCompact = ((.hooks.PreCompact // []) | upsert($pre))
| .hooks.SessionStart = ((.hooks.SessionStart // []) | upsert($ss))
| .hooks.PreToolUse = ((.hooks.PreToolUse // []) | upsert_matcher($ptu; "Agent|Task|Workflow"; 5))' \
"$file" > "$tmp"
mv "$tmp" "$file"
}
register_hooks() {
if [[ $DRY_RUN -eq 1 ]]; then
for event in "${HOOK_EVENTS[@]}"; do
plan "merge hook $event into settings.json (Claude Code)"
done
return
fi
mkdir -p "$CLAUDE_DIR"
merge_hooks_into "$SETTINGS_FILE"
}
register_codex_hooks() {
[[ "${PM_SKIP_CODEX:-0}" == "1" ]] && return
if ! command -v codex >/dev/null 2>&1 && [[ ! -d "$CODEX_DIR" && "${PM_INSTALL_CODEX:-0}" != "1" ]]; then
say "codex not found — skipping Codex hooks (force with PM_INSTALL_CODEX=1)"
return
fi
if [[ $DRY_RUN -eq 1 ]]; then
for event in "${HOOK_EVENTS[@]}"; do
plan "merge codex hook $event into $CODEX_HOOKS_FILE"
done
plan "copy $REPO_ROOT/skill/SKILL.md to $CODEX_SKILL_DEST/SKILL.md (.codex/skills/persistent-memory)"
return
fi
mkdir -p "$CODEX_DIR"
merge_hooks_into "$CODEX_HOOKS_FILE"
mkdir -p "$CODEX_SKILL_DEST"
cp "$REPO_ROOT/skill/SKILL.md" "$CODEX_SKILL_DEST/SKILL.md"
say "Codex hooks written to $CODEX_HOOKS_FILE — trust them via '/hooks' in Codex (or --dangerously-bypass-hook-trust)."
}
register_mcp() {
[[ "${PM_SKIP_MCP:-0}" == "1" ]] && return
local name="persistent-memory"
local cmd="$VENV_DIR/bin/python"
if [[ $DRY_RUN -eq 1 ]]; then
plan "register MCP server '$name' ($cmd -m persistent_memory.mcp_server) in Claude + Codex"
return
fi
if command -v claude >/dev/null 2>&1; then
claude mcp remove -s user "$name" >/dev/null 2>&1 || true
if claude mcp add -s user "$name" -- "$cmd" -m persistent_memory.mcp_server >/dev/null 2>&1; then
say "Claude MCP '$name' registered (scope user)."
else
say "Claude MCP registration failed — run manually: claude mcp add -s user $name -- $cmd -m persistent_memory.mcp_server"
fi
fi
if command -v codex >/dev/null 2>&1; then
codex mcp remove "$name" >/dev/null 2>&1 || true
if codex mcp add "$name" -- "$cmd" -m persistent_memory.mcp_server >/dev/null 2>&1; then
say "Codex MCP '$name' registered."
else
say "Codex MCP registration failed — run manually: codex mcp add $name -- $cmd -m persistent_memory.mcp_server"
fi
fi
}
should_install_kimi() {
[[ "${PM_SKIP_KIMI:-0}" == "1" ]] && return 1
[[ "${PM_INSTALL_KIMI:-0}" == "1" ]] && return 0
command -v kimi >/dev/null 2>&1 && return 0
[[ -d "$KIMI_DIR" ]] && return 0
return 1
}
install_kimi_skill() {
if [[ $DRY_RUN -eq 1 ]]; then
plan "copy $REPO_ROOT/skill/SKILL.md to $KIMI_SKILL_DEST/SKILL.md (.kimi-code/skills/persistent-memory)"
return
fi
mkdir -p "$KIMI_SKILL_DEST"
cp "$REPO_ROOT/skill/SKILL.md" "$KIMI_SKILL_DEST/SKILL.md"
}
register_kimi_hooks() {
if [[ $DRY_RUN -eq 1 ]]; then
for event in "${HOOK_EVENTS[@]}"; do
plan "merge kimi hook $event into $KIMI_CONFIG"
done
return
fi
mkdir -p "$KIMI_DIR"
"$VENV_DIR/bin/python" - "$KIMI_CONFIG" "$VENV_DIR" <<'PYEOF'
import json
import os
import sys
try:
import tomllib
except ImportError: # pragma: no cover
import tomli as tomllib
import tomli_w
config_path = sys.argv[1]
venv = sys.argv[2]
ours = [
{"event": "UserPromptSubmit", "command": f"{venv}/bin/python -m persistent_memory.hooks.user_prompt_submit"},
{"event": "Stop", "command": f"{venv}/bin/python -m persistent_memory.hooks.stop_or_session_end"},
{"event": "PreCompact", "command": f"{venv}/bin/python -m persistent_memory.hooks.pre_compact"},
{"event": "SessionStart", "command": f"{venv}/bin/python -m persistent_memory.hooks.session_start"},
{"event": "PreToolUse", "matcher": "Agent|Task|Workflow", "command": f"{venv}/bin/python -m persistent_memory.hooks.pre_tool_use", "timeout": 5},
]
our_cmds = {h["command"] for h in ours}
try:
with open(config_path, "rb") as f:
data = tomllib.load(f)
except FileNotFoundError:
data = {}
hooks = [h for h in data.get("hooks", []) if not (isinstance(h, dict) and h.get("command") in our_cmds)]
hooks.extend(ours)
data["hooks"] = hooks
with open(config_path, "wb") as f:
tomli_w.dump(data, f)
PYEOF
say "Kimi hooks written to $KIMI_CONFIG"
}
register_kimi_mcp() {
[[ "${PM_SKIP_MCP:-0}" == "1" ]] && return
local cmd="$VENV_DIR/bin/python"
if [[ $DRY_RUN -eq 1 ]]; then
plan "register MCP server 'persistent-memory' ($cmd -m persistent_memory.mcp_server) in $KIMI_MCP"
return
fi
mkdir -p "$KIMI_DIR"
"$VENV_DIR/bin/python" - "$KIMI_MCP" "$cmd" <<'PYEOF'
import json
import os
import sys
mcp_path = sys.argv[1]
cmd = sys.argv[2]
try:
with open(mcp_path, "r", encoding="utf-8") as f:
data = json.load(f)
except (FileNotFoundError, json.JSONDecodeError):
data = {}
servers = data.setdefault("mcpServers", {})
servers["persistent-memory"] = {
"command": cmd,
"args": ["-m", "persistent_memory.mcp_server"],
}
with open(mcp_path, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2, ensure_ascii=False)
f.write("\n")
PYEOF
say "Kimi MCP 'persistent-memory' registered in $KIMI_MCP"
}
should_install_grok() {
[[ "${PM_SKIP_GROK:-0}" == "1" ]] && return 1
[[ "${PM_INSTALL_GROK:-0}" == "1" ]] && return 0
command -v grok >/dev/null 2>&1 && return 0
[[ -d "$GROK_DIR" ]] && return 0
return 1
}
install_grok_skill() {
if [[ $DRY_RUN -eq 1 ]]; then
plan "copy $REPO_ROOT/skill/SKILL.md to $GROK_SKILL_DEST/SKILL.md (.grok/skills/persistent-memory)"
return
fi
mkdir -p "$GROK_SKILL_DEST"
cp "$REPO_ROOT/skill/SKILL.md" "$GROK_SKILL_DEST/SKILL.md"
}
register_grok_hooks() {
if [[ $DRY_RUN -eq 1 ]]; then
for event in "${HOOK_EVENTS[@]}"; do
plan "write grok hook $event into $GROK_HOOKS_FILE"
done
return
fi
mkdir -p "$(dirname "$GROK_HOOKS_FILE")"
local tmp; tmp="$(mktemp)"
jq -n \
--arg ups "$(hook_command user_prompt_submit)" \
--arg stop "$(hook_command stop_or_session_end)" \
--arg pre "$(hook_command pre_compact)" \
--arg ss "$(hook_command session_start)" \
--arg ptu "$(hook_command pre_tool_use)" \
'{
hooks: {
UserPromptSubmit: [{hooks: [{type: "command", command: $ups}]}],
Stop: [{hooks: [{type: "command", command: $stop}]}],
PreCompact: [{hooks: [{type: "command", command: $pre}]}],
SessionStart: [{hooks: [{type: "command", command: $ss}]}],
PreToolUse: [{matcher: "Agent|Task|Workflow|spawn_subagent", hooks: [{type: "command", command: $ptu, timeout: 5}]}]
}
}' > "$tmp"
mv "$tmp" "$GROK_HOOKS_FILE"
say "Grok hooks written to $GROK_HOOKS_FILE"
}
register_grok_mcp() {
[[ "${PM_SKIP_MCP:-0}" == "1" ]] && return
local name="persistent-memory"
local cmd="$VENV_DIR/bin/python"
if [[ $DRY_RUN -eq 1 ]]; then
plan "register MCP server '$name' ($cmd -m persistent_memory.mcp_server) in Grok ($GROK_CONFIG or grok mcp)"
return
fi
mkdir -p "$GROK_DIR"
if command -v grok >/dev/null 2>&1; then
grok mcp remove "$name" >/dev/null 2>&1 || true
if grok mcp add "$name" -- "$cmd" -m persistent_memory.mcp_server >/dev/null 2>&1; then
say "Grok MCP '$name' registered via grok mcp."
return
fi
fi
"$VENV_DIR/bin/python" - "$GROK_CONFIG" "$cmd" <<'PYEOF'
import sys
try:
import tomllib
except ImportError: # pragma: no cover
import tomli as tomllib
import tomli_w
config_path = sys.argv[1]
cmd = sys.argv[2]
try:
with open(config_path, "rb") as f:
data = tomllib.load(f)
except FileNotFoundError:
data = {}
servers = data.setdefault("mcp_servers", {})
servers["persistent-memory"] = {
"command": cmd,
"args": ["-m", "persistent_memory.mcp_server"],
"enabled": True,
}
with open(config_path, "wb") as f:
tomli_w.dump(data, f)
PYEOF
say "Grok MCP 'persistent-memory' registered in $GROK_CONFIG"
}
_lang_subtag() {
echo "$1" | sed 's/[._@-].*//' | tr '[:upper:]' '[:lower:]'
}
detect_lang() {
local lang=""
for var in PM_LANG LC_ALL LANG; do
local val="${!var:-}"
if [[ -n "$val" ]]; then
lang="$(_lang_subtag "$val")"
if [[ -n "$lang" ]]; then
echo "$lang"
return
fi
fi
done
local apple
apple="$(defaults read -g AppleLocale 2>/dev/null || true)"
if [[ -n "$apple" ]]; then
lang="$(_lang_subtag "$apple")"
[[ -n "$lang" ]] && echo "$lang" && return
fi
echo "en"
}
install_launchd() {
local install_lang
install_lang="$(detect_lang)"
if [[ $DRY_RUN -eq 1 ]]; then
plan "write LaunchAgents plist $PLIST_DEST with PM_LANG=$install_lang and launchctl load"
return
fi
[[ "${PM_SKIP_LAUNCHD:-0}" == "1" ]] && return
mkdir -p "$LAUNCH_AGENTS_DIR"
mkdir -p "$LOGS_DIR"
PYTHONPATH="$REPO_ROOT/src" "$VENV_DIR/bin/python" - "$VENV_DIR/bin/python" "$REPO_ROOT" "$install_lang" "$TARGET_HOME" <<'PYEOF' > "$PLIST_DEST"
import sys
from persistent_memory.daemon.launch_agent import build_launch_agent_plist
python_bin, working_dir, lang, home_dir = sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4]
print(build_launch_agent_plist(python_bin=python_bin, working_dir=working_dir, lang=lang if lang else None, home_dir=home_dir), end="")
PYEOF
launchctl unload "$PLIST_DEST" 2>/dev/null || true
launchctl bootout "gui/$(id -u)/com.persistent-memory.daemon" 2>/dev/null || true
launchctl remove com.persistent-memory.daemon 2>/dev/null || true
launchctl load "$PLIST_DEST"
}
say "persistent-memory install (dry_run=$DRY_RUN)"
run_doctor
create_venv
install_skill
register_hooks
register_codex_hooks
if should_install_kimi; then
install_kimi_skill
register_kimi_hooks
register_kimi_mcp
fi
if should_install_grok; then
install_grok_skill
register_grok_hooks
register_grok_mcp
fi
register_mcp
install_launchd
say "Done."