You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
1.3KB

  1. #!/usr/bin/env bash
  2. # Fetch every memory the API key can read (follows next_cursor) and print them as one JSON array.
  3. # AMK_KEY=amk_... bash scripts/export-memories.sh [extra query, e.g. "status=active&type=semantic"] > memories.json
  4. # Env: AMK_KEY (required), BASE (default http://localhost:8801/api/v1), FULL=1 to include the full content per memory
  5. # (a GET /memories/:id per item; the list endpoint returns summaries only if it does not include content).
  6. set -eu
  7. BASE=${BASE:-http://localhost:8801/api/v1}
  8. : "${AMK_KEY:amk_734bbd0bd49099ac928a179fbe9ba1c025e796ab93203d4abd9631c2e0517099}"
  9. EXTRA=${1:-}
  10. cursor=""
  11. python -I - "$BASE" "$AMK_KEY" "$EXTRA" "${FULL:-0}" <<'PY'
  12. import sys, json, urllib.request, urllib.parse
  13. base, key, extra, full = sys.argv[1:5]
  14. def get(path):
  15. req = urllib.request.Request(base + path, headers={"Authorization": "Bearer " + key})
  16. with urllib.request.urlopen(req) as r:
  17. return json.load(r)
  18. out, cursor = [], None
  19. while True:
  20. q = "limit=100" + ("&" + extra if extra else "") + ("&cursor=" + urllib.parse.quote(cursor) if cursor else "")
  21. page = get("/memories?" + q)
  22. out.extend(page["data"])
  23. cursor = page.get("next_cursor")
  24. if not cursor:
  25. break
  26. if full == "1":
  27. out = [get("/memories/" + m["id"]).get("data", m) for m in out]
  28. json.dump(out, sys.stdout, indent=2, ensure_ascii=False)
  29. PY

Powered by TurnKey Linux.