File size: 7,142 Bytes
c3a3710
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# MnemoCore Integration Setup β€” Windows PowerShell
# =================================================
# One-command setup for Claude Code (MCP) on Windows.
# For full hook/wrapper support, use WSL or Git Bash.
#
# Usage:
#   .\setup.ps1
#   .\setup.ps1 -All
#   .\setup.ps1 -ClaudeCode

param(
    [switch]$All,
    [switch]$ClaudeCode,
    [switch]$Gemini,
    [switch]$Aider
)

$ErrorActionPreference = "Stop"

$ScriptDir    = Split-Path -Parent $MyInvocation.MyCommand.Path
$MnemoDir     = Split-Path -Parent $ScriptDir
$BridgePy     = Join-Path $ScriptDir "mnemo_bridge.py"
$ClaudeHome   = Join-Path $env:USERPROFILE ".claude"
$ClaudeMcp    = Join-Path $ClaudeHome "mcp.json"
$ClaudeSettings = Join-Path $ClaudeHome "settings.json"
$HooksDir     = Join-Path $ScriptDir "claude_code\hooks"

function Write-Info    { Write-Host "[INFO]  $args" -ForegroundColor Cyan }
function Write-Success { Write-Host "[OK]    $args" -ForegroundColor Green }
function Write-Warn    { Write-Host "[WARN]  $args" -ForegroundColor Yellow }
function Write-Err     { Write-Host "[ERROR] $args" -ForegroundColor Red }

# ── Prerequisite checks ────────────────────────────────────────────────────

Write-Info "Checking Python requests..."
$requestsCheck = python -c "import requests; print('ok')" 2>&1
if ($requestsCheck -ne "ok") {
    Write-Warn "Installing requests..."
    python -m pip install --quiet requests
}
Write-Success "Python requests available"

Write-Info "Checking MnemoCore connectivity..."
$healthCheck = python "$BridgePy" health 2>&1
if ($LASTEXITCODE -eq 0) {
    Write-Success "MnemoCore is online"
} else {
    Write-Warn "MnemoCore offline β€” start it first:"
    Write-Warn "  cd $MnemoDir"
    Write-Warn "  uvicorn mnemocore.api.main:app --port 8100"
}

# ── Claude Code MCP Setup ─────────────────────────────────────────────────

function Setup-ClaudeCode {
    Write-Info "Setting up Claude Code integration..."

    if (-not (Test-Path $ClaudeHome)) { New-Item -ItemType Directory -Path $ClaudeHome | Out-Null }
    New-Item -ItemType Directory -Path (Join-Path $ClaudeHome "mnemo_context") -Force | Out-Null

    # MCP config
    $McpTemplate = Get-Content (Join-Path $ScriptDir "claude_code\mcp_config.json") -Raw
    $McpTemplate = $McpTemplate `
        -replace '\$\{MNEMOCORE_DIR\}', $MnemoDir.Replace('\', '/') `
        -replace '\$\{HAIM_API_KEY\}', ($env:HAIM_API_KEY ?? '')

    if (-not (Test-Path $ClaudeMcp)) {
        '{"mcpServers":{}}' | Set-Content $ClaudeMcp
    }

    $Existing = Get-Content $ClaudeMcp -Raw | ConvertFrom-Json
    $New      = $McpTemplate | ConvertFrom-Json
    if (-not $Existing.mcpServers) { $Existing | Add-Member -MemberType NoteProperty -Name mcpServers -Value @{} }
    $New.mcpServers.PSObject.Properties | ForEach-Object {
        $Existing.mcpServers | Add-Member -MemberType NoteProperty -Name $_.Name -Value $_.Value -Force
    }
    $Existing | ConvertTo-Json -Depth 10 | Set-Content $ClaudeMcp
    Write-Success "MCP server registered in $ClaudeMcp"

    # Hooks
    if (-not (Test-Path $ClaudeSettings)) { '{}' | Set-Content $ClaudeSettings }
    $Settings = Get-Content $ClaudeSettings -Raw | ConvertFrom-Json

    if (-not $Settings.hooks) {
        $Settings | Add-Member -MemberType NoteProperty -Name hooks -Value @{}
    }
    $hooksObj = $Settings.hooks

    $preCmd  = "python `"$($HooksDir.Replace('\','/'))/pre_session_inject.py`""
    $postCmd = "python `"$($HooksDir.Replace('\','/'))/post_tool_store.py`""

    if (-not $hooksObj.PreToolUse) {
        $hooksObj | Add-Member -MemberType NoteProperty -Name PreToolUse -Value @()
    }
    if (-not $hooksObj.PostToolUse) {
        $hooksObj | Add-Member -MemberType NoteProperty -Name PostToolUse -Value @()
    }

    $existingPre = $hooksObj.PreToolUse | ForEach-Object { $_.hooks[0].command }
    if ($preCmd -notin $existingPre) {
        $hooksObj.PreToolUse += @{matcher=".*"; hooks=@(@{type="command"; command=$preCmd})}
    }
    $existingPost = $hooksObj.PostToolUse | ForEach-Object { $_.hooks[0].command }
    if ($postCmd -notin $existingPost) {
        $hooksObj.PostToolUse += @{matcher="Edit|Write|MultiEdit"; hooks=@(@{type="command"; command=$postCmd})}
    }

    $Settings | ConvertTo-Json -Depth 10 | Set-Content $ClaudeSettings
    Write-Success "Hooks installed in $ClaudeSettings"

    # CLAUDE.md snippet
    $ClaudeMd = Join-Path $MnemoDir "CLAUDE.md"
    $Snippet  = Get-Content (Join-Path $ScriptDir "claude_code\CLAUDE_memory_snippet.md") -Raw
    $Marker   = "# MnemoCore β€” Persistent Cognitive Memory"
    if (Test-Path $ClaudeMd) {
        $Current = Get-Content $ClaudeMd -Raw
        if ($Current -notlike "*$Marker*") {
            Add-Content $ClaudeMd "`n$Snippet"
            Write-Success "Memory instructions appended to CLAUDE.md"
        } else {
            Write-Info "CLAUDE.md already contains MnemoCore instructions"
        }
    } else {
        $Snippet | Set-Content $ClaudeMd
        Write-Success "Created CLAUDE.md with memory instructions"
    }

    Write-Success "Claude Code integration complete"
}

# ── Main ───────────────────────────────────────────────────────────────────

Write-Host ""
Write-Host "╔══════════════════════════════════════════╗" -ForegroundColor Magenta
Write-Host "β•‘    MnemoCore Integration Setup (Win)    β•‘" -ForegroundColor Magenta
Write-Host "β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•" -ForegroundColor Magenta
Write-Host ""

if (-not ($All -or $ClaudeCode -or $Gemini -or $Aider)) {
    Write-Host "Choose integrations:"
    Write-Host "  1) Claude Code (MCP + hooks + CLAUDE.md) β€” recommended"
    Write-Host "  4) All"
    $choice = Read-Host "Enter choice"
    switch ($choice) {
        "1" { $ClaudeCode = $true }
        "4" { $All = $true }
    }
}

if ($All -or $ClaudeCode) { Setup-ClaudeCode }

Write-Host ""
Write-Host "╔══════════════════════════════════════════╗" -ForegroundColor Green
Write-Host "β•‘   Setup complete!                       β•‘" -ForegroundColor Green
Write-Host "β•‘                                          β•‘" -ForegroundColor Green
Write-Host "β•‘   Test: python integrations/mnemo_bridge.py health" -ForegroundColor Green
Write-Host "β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•" -ForegroundColor Green
Write-Host ""