File size: 3,064 Bytes
4d5727a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# agentmemory PowerShell terminal hook
# Source this file from your $PROFILE to automatically log terminal commands.
#
# Usage:
#   . C:\path\to\powershell-hook.ps1
#
# Configuration (set as environment variables or override at top of profile):
#   $env:AGENTMEMORY_URL    = "http://127.0.0.1:3111"  (default)
#   $env:AGENTMEMORY_SECRET = ""                         (optional)
#   $env:AGENTMEMORY_AGENT_ID = "powershell"             (default)

param(
    [string]$ServerUrl   = $env:AGENTMEMORY_URL   ?? "http://127.0.0.1:3111",
    [string]$AgentId     = $env:AGENTMEMORY_AGENT_ID ?? "powershell",
    [string]$Secret      = $env:AGENTMEMORY_SECRET ?? ""
)

function Send-AgentMemoryObservation {
    param(
        [string]$Text,
        [string]$FolderPath = (Get-Location).Path
    )

    $payload = @{
        folderPath = $FolderPath
        agentId    = $AgentId
        text       = $Text.Substring(0, [Math]::Min($Text.Length, 4000))
        timestamp  = (Get-Date).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ")
    } | ConvertTo-Json -Compress

    $headers = @{ "Content-Type" = "application/json" }
    if ($Secret) {
        $headers["Authorization"] = "Bearer $Secret"
    }

    try {
        Invoke-RestMethod `
            -Uri "$ServerUrl/agentmemory/agent/observe" `
            -Method Post `
            -Headers $headers `
            -Body $payload `
            -TimeoutSec 5 `
            -ErrorAction SilentlyContinue | Out-Null
    } catch {
        # Non-fatal — agentmemory is a best-effort sidecar
    }
}

# Hook into PSReadLine's CommandValidationHandler to log every command
if (Get-Module -Name PSReadLine -ErrorAction SilentlyContinue) {
    $existingHandler = (Get-PSReadLineOption).CommandValidationHandler

    Set-PSReadLineOption -CommandValidationHandler {
        param([System.Management.Automation.Language.CommandAst]$CommandAst)

        # Forward to existing handler if any
        if ($existingHandler) { & $existingHandler $CommandAst }

        # Log the command to agentmemory (best-effort, non-blocking)
        $cmd = $CommandAst.ToString()
        if ($cmd -and $cmd.Trim()) {
            $job = Start-Job -ScriptBlock {
                param($u, $a, $s, $t, $f)
                $payload = "{`"folderPath`":`"$f`",`"agentId`":`"$a`",`"text`":`"$t`",`"timestamp`":`"$(Get-Date -Format 'yyyy-MM-ddTHH:mm:ssZ')`"}"
                $h = @{"Content-Type"="application/json"}
                if ($s) { $h["Authorization"] = "Bearer $s" }
                try { Invoke-RestMethod -Uri "$u/agentmemory/agent/observe" -Method Post -Headers $h -Body $payload -TimeoutSec 5 -EA SilentlyContinue | Out-Null } catch {}
            } -ArgumentList $ServerUrl, $AgentId, $Secret, $cmd, (Get-Location).Path
            $null = $job  # fire-and-forget
        }

        return $true
    }
    Write-Host "[agentmemory] PowerShell hook active (agent: $AgentId, server: $ServerUrl)"
} else {
    Write-Warning "[agentmemory] PSReadLine not available — hook not installed. Call Send-AgentMemoryObservation manually."
}