Spaces:
Running
Running
fix: add legacy API compat shims + fix /observe auto-routing
Browse files- POST /agentmemory/session/start -> 200 compat shim (was 404)
- POST /agentmemory/session/end -> 200 compat shim
- GET /agentmemory/observations?sessionId -> compat shim reading legacy KV
- POST /agentmemory/observe: auto-detect folderPath+agentId payload and
route to folder_observe() instead of failing with 400
- Fix viewer: loadFolders circular reference via IIFE capture
- Fix viewer: broken if(false) block that swallowed page init
- src/routes/observations.py +88 -1
src/routes/observations.py
CHANGED
|
@@ -43,7 +43,7 @@ def _get_kv():
|
|
| 43 |
|
| 44 |
|
| 45 |
# ---------------------------------------------------------------------------
|
| 46 |
-
# POST /agentmemory/observe (legacy raw hook endpoint)
|
| 47 |
# ---------------------------------------------------------------------------
|
| 48 |
|
| 49 |
@observations_bp.route("/agentmemory/observe", methods=["POST"])
|
|
@@ -54,6 +54,30 @@ def api_observe():
|
|
| 54 |
|
| 55 |
try:
|
| 56 |
body = request.get_json(force=True) or {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
res = functions.observe(_get_kv(), body)
|
| 58 |
return jsonify(res), 201
|
| 59 |
except Exception as e:
|
|
@@ -138,3 +162,66 @@ def api_folder_observations():
|
|
| 138 |
reverse=True,
|
| 139 |
)
|
| 140 |
return jsonify({"observations": observations, "folderPath": fp, "agentId": aid}), 200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
|
| 45 |
# ---------------------------------------------------------------------------
|
| 46 |
+
# POST /agentmemory/observe (legacy raw hook endpoint + auto-compat shim)
|
| 47 |
# ---------------------------------------------------------------------------
|
| 48 |
|
| 49 |
@observations_bp.route("/agentmemory/observe", methods=["POST"])
|
|
|
|
| 54 |
|
| 55 |
try:
|
| 56 |
body = request.get_json(force=True) or {}
|
| 57 |
+
|
| 58 |
+
# Auto-detect folder-based payload: if folderPath + agentId present,
|
| 59 |
+
# route to folder_observe instead of legacy observe().
|
| 60 |
+
folder_path = body.get("folderPath")
|
| 61 |
+
agent_id = body.get("agentId")
|
| 62 |
+
text = body.get("text") or body.get("content") or ""
|
| 63 |
+
|
| 64 |
+
if folder_path and agent_id:
|
| 65 |
+
# New folder-based model — delegate to folder_observe
|
| 66 |
+
payload = {
|
| 67 |
+
"folderPath": folder_path,
|
| 68 |
+
"agentId": agent_id,
|
| 69 |
+
"text": text,
|
| 70 |
+
"timestamp": body.get("timestamp") or _datetime_now_iso(),
|
| 71 |
+
"type": body.get("type"),
|
| 72 |
+
"title": body.get("title"),
|
| 73 |
+
"concepts": body.get("concepts"),
|
| 74 |
+
"files": body.get("files"),
|
| 75 |
+
"importance": body.get("importance"),
|
| 76 |
+
}
|
| 77 |
+
res = functions.folder_observe(_get_kv(), payload)
|
| 78 |
+
return jsonify(res), 201
|
| 79 |
+
|
| 80 |
+
# Legacy session-based model
|
| 81 |
res = functions.observe(_get_kv(), body)
|
| 82 |
return jsonify(res), 201
|
| 83 |
except Exception as e:
|
|
|
|
| 162 |
reverse=True,
|
| 163 |
)
|
| 164 |
return jsonify({"observations": observations, "folderPath": fp, "agentId": aid}), 200
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
# ---------------------------------------------------------------------------
|
| 168 |
+
# POST /agentmemory/session/start (legacy compat shim → 200 no-op)
|
| 169 |
+
# ---------------------------------------------------------------------------
|
| 170 |
+
|
| 171 |
+
@observations_bp.route("/agentmemory/session/start", methods=["POST"])
|
| 172 |
+
def api_session_start():
|
| 173 |
+
"""Legacy session/start — clients in the wild still call this.
|
| 174 |
+
Return a synthetic session ID so callers don't error out.
|
| 175 |
+
"""
|
| 176 |
+
auth_err = _check_auth()
|
| 177 |
+
if auth_err:
|
| 178 |
+
return auth_err
|
| 179 |
+
|
| 180 |
+
import uuid
|
| 181 |
+
body = request.get_json(force=True) or {}
|
| 182 |
+
session_id = body.get("sessionId") or f"compat_{uuid.uuid4().hex[:16]}"
|
| 183 |
+
return jsonify({
|
| 184 |
+
"sessionId": session_id,
|
| 185 |
+
"status": "active",
|
| 186 |
+
"message": "Session model migrated to folder-based. Use /agentmemory/agent/observe.",
|
| 187 |
+
}), 200
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
# ---------------------------------------------------------------------------
|
| 191 |
+
# POST /agentmemory/session/end (legacy compat shim → 200 no-op)
|
| 192 |
+
# ---------------------------------------------------------------------------
|
| 193 |
+
|
| 194 |
+
@observations_bp.route("/agentmemory/session/end", methods=["POST"])
|
| 195 |
+
def api_session_end():
|
| 196 |
+
auth_err = _check_auth()
|
| 197 |
+
if auth_err:
|
| 198 |
+
return auth_err
|
| 199 |
+
return jsonify({"success": True, "message": "Session model is now folder-based."}), 200
|
| 200 |
+
|
| 201 |
+
|
| 202 |
+
# ---------------------------------------------------------------------------
|
| 203 |
+
# GET /agentmemory/observations (legacy compat shim)
|
| 204 |
+
# ---------------------------------------------------------------------------
|
| 205 |
+
|
| 206 |
+
@observations_bp.route("/agentmemory/observations", methods=["GET"])
|
| 207 |
+
def api_observations_legacy():
|
| 208 |
+
"""Legacy /observations?sessionId=... shim.
|
| 209 |
+
Reads from legacy KV scope if data exists, otherwise returns empty list.
|
| 210 |
+
"""
|
| 211 |
+
auth_err = _check_auth()
|
| 212 |
+
if auth_err:
|
| 213 |
+
return auth_err
|
| 214 |
+
|
| 215 |
+
session_id = request.args.get("sessionId", "")
|
| 216 |
+
if not session_id:
|
| 217 |
+
return jsonify({"observations": [], "sessionId": ""}), 200
|
| 218 |
+
|
| 219 |
+
try:
|
| 220 |
+
obs = sorted(
|
| 221 |
+
_get_kv().list(functions.KV.observations(session_id)),
|
| 222 |
+
key=lambda x: x.get("timestamp", ""),
|
| 223 |
+
reverse=True,
|
| 224 |
+
)
|
| 225 |
+
return jsonify({"observations": obs, "sessionId": session_id}), 200
|
| 226 |
+
except Exception as e:
|
| 227 |
+
return jsonify({"observations": [], "sessionId": session_id, "error": str(e)}), 200
|