File size: 2,961 Bytes
26a284a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import sys
import json
import urllib.request
import urllib.error
import subprocess
import threading
from pathlib import Path

DEFAULT_BASE_URL = "http://localhost:3111"

def load_env():
    """Load ~/.agentmemory/.env or XDG_CONFIG_HOME config into os.environ (best effort)."""
    candidates = []
    home = os.environ.get("HOME") or os.environ.get("USERPROFILE")
    if home:
        candidates.append(Path(home) / ".agentmemory" / ".env")
    xdg_config = os.environ.get("XDG_CONFIG_HOME")
    if xdg_config:
        candidates.append(Path(xdg_config) / "agentmemory" / ".env")
    
    for path in candidates:
        try:
            if not path.is_file():
                continue
            for line in path.read_text(encoding="utf-8").splitlines():
                line = line.strip()
                if not line or line.startswith("#") or "=" not in line:
                    continue
                key, _, val = line.partition("=")
                key = key.strip()
                val = val.strip().strip('"').strip("'")
                if key:
                    os.environ.setdefault(key, val)
        except Exception:
            continue
    os.environ.setdefault("AGENTMEMORY_URL", DEFAULT_BASE_URL)

# Load env variables immediately upon importing this utility
load_env()

def resolve_project(cwd=None):
    explicit = os.environ.get("AGENTMEMORY_PROJECT_NAME")
    if explicit and explicit.strip():
        return explicit.strip()
    
    directory = cwd if (cwd and cwd.strip()) else os.getcwd()
    try:
        top = subprocess.check_output(
            ["git", "rev-parse", "--show-toplevel"],
            cwd=directory,
            stderr=subprocess.DEVNULL,
            timeout=0.5
        ).decode().strip()
        if top:
            return os.path.basename(top)
    except Exception:
        pass
    
    return os.path.basename(os.path.abspath(directory))

def is_sdk_child(payload):
    if os.environ.get("AGENTMEMORY_SDK_CHILD") == "1":
        return True
    if not isinstance(payload, dict):
        return False
    return payload.get("entrypoint") == "sdk-ts" or payload.get("entrypoint") == "sdk-python"

def api_call(path, body=None, timeout=1.5):
    base_url = os.environ.get("AGENTMEMORY_URL", DEFAULT_BASE_URL)
    url = f"{base_url}/agentmemory/{path}"
    headers = {"Content-Type": "application/json"}
    secret = os.environ.get("AGENTMEMORY_SECRET", "")
    if secret:
        headers["Authorization"] = f"Bearer {secret}"
        
    req = urllib.request.Request(
        url,
        data=json.dumps(body).encode("utf-8") if body else None,
        headers=headers,
        method="POST"
    )
    try:
        with urllib.request.urlopen(req, timeout=timeout) as resp:
            return json.loads(resp.read().decode("utf-8"))
    except Exception:
        return None

def api_call_bg(path, body=None):
    t = threading.Thread(target=api_call, args=(path, body), daemon=True)
    t.start()