Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

feat(hooks): PowerShell helper learns marker walk-up

Pablo Winter67f93563bb1d
diff --git a/crates/ai-memory-cli/src/commands/install_hooks.rs b/crates/ai-memory-cli/src/commands/install_hooks.rs
index a82295fe..c36b758c 100644
--- a/crates/ai-memory-cli/src/commands/install_hooks.rs
+++ b/crates/ai-memory-cli/src/commands/install_hooks.rs
@@ -1282,7 +1282,7 @@ mod tests {
.unwrap_or_else(|| panic!("{} missing agent", path.display()))
+ agent_marker.len();
let agent = rest[agent_start..]
- .split(['"', '\'', ' ', '\n', '\r'])
+ .split(['"', '\'', ' ', '\n', '\r', '$'])
.next()
.unwrap_or_else(|| panic!("{} missing agent value", path.display()))
.to_string();
diff --git a/hooks/lib/ai-memory-hook.ps1 b/hooks/lib/ai-memory-hook.ps1
index 636dd940..d3d6aadc 100644
--- a/hooks/lib/ai-memory-hook.ps1
+++ b/hooks/lib/ai-memory-hook.ps1
@@ -1,3 +1,49 @@
+function Get-AiMemoryCwd {
+ param([string] $Payload)
+ if (-not $Payload) { return $null }
+ $match = [regex]::Match($Payload, '"cwd"\s*:\s*"([^"]*)"')
+ if ($match.Success) { return $match.Groups[1].Value }
+ return $null
+}
+
+function Get-AiMemoryMarkerToml {
+ param([string] $Cwd)
+ if (-not $Cwd) { return $null }
+ $dir = $Cwd
+ while ($dir -and (Test-Path $dir)) {
+ $candidate = Join-Path $dir ".ai-memory.toml"
+ if (Test-Path $candidate -PathType Leaf) { return $candidate }
+ if ($env:HOME -and $dir -eq $env:HOME) { return $null }
+ if ($env:USERPROFILE -and $dir -eq $env:USERPROFILE) { return $null }
+ $parent = Split-Path $dir -Parent
+ if (-not $parent -or $parent -eq $dir) { return $null }
+ $dir = $parent
+ }
+ return $null
+}
+
+function Get-AiMemoryTomlKey {
+ param([string] $File, [string] $Key)
+ if (-not (Test-Path $File -PathType Leaf)) { return $null }
+ foreach ($line in Get-Content $File) {
+ $m = [regex]::Match($line, "^\s*$Key\s*=\s*`"([^`"]*)`"")
+ if ($m.Success) { return $m.Groups[1].Value }
+ }
+ return $null
+}
+
+function Get-AiMemoryMarkerQuery {
+ param([string] $Cwd)
+ $marker = Get-AiMemoryMarkerToml -Cwd $Cwd
+ if (-not $marker) { return "" }
+ $qs = ""
+ $ws = Get-AiMemoryTomlKey -File $marker -Key "workspace"
+ if ($ws) { $qs += "&workspace=$([uri]::EscapeDataString($ws))" }
+ $proj = Get-AiMemoryTomlKey -File $marker -Key "project"
+ if ($proj) { $qs += "&project=$([uri]::EscapeDataString($proj))" }
+ return $qs
+}
+
function Invoke-AiMemoryHook {
param(
[Parameter(Mandatory = $true)] [string] $Event,
@@ -7,6 +53,8 @@ function Invoke-AiMemoryHook {
$Server = if ($env:AI_MEMORY_HOOK_URL) { $env:AI_MEMORY_HOOK_URL } else { "http://127.0.0.1:49374" }
$Payload = [Console]::In.ReadToEnd()
+ $Cwd = Get-AiMemoryCwd -Payload $Payload
+ $QS = Get-AiMemoryMarkerQuery -Cwd $Cwd
$Headers = @{}
if ($env:AI_MEMORY_AUTH_TOKEN) {
@@ -18,7 +66,7 @@ function Invoke-AiMemoryHook {
-UseBasicParsing `
-TimeoutSec 1 `
-Method Post `
- -Uri "$Server/hook?event=$Event&agent=$Agent" `
+ -Uri "$Server/hook?event=$Event&agent=$Agent$QS" `
-Headers $Headers `
-ContentType "application/json" `
-Body $Payload | Out-Null
@@ -30,7 +78,7 @@ function Invoke-AiMemoryHook {
$Response = Invoke-WebRequest `
-UseBasicParsing `
-TimeoutSec 1 `
- -Uri "$Server/handoff?agent=$Agent" `
+ -Uri "$Server/handoff?agent=$Agent$QS" `
-Headers $Headers
if ($null -ne $Response -and $null -ne $Response.Content) {
[Console]::Out.Write($Response.Content)