Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/memos/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ def _validate_profile_subject(self, user_id: str | None, agent_id: str | None) -
if bool(user_id) == bool(agent_id):
raise ValueError("exactly one of user_id or agent_id is required")

@staticmethod
def _normalize_task_status_response(
response_data: dict[str, Any], task_id: str
) -> dict[str, Any]:
data = response_data.get("data")
if not (isinstance(data, list) and len(data) == 1 and isinstance(data[0], dict)):
return response_data

normalized_data = data[0].copy()
normalized_data.setdefault("task_id", task_id)
return {**response_data, "data": normalized_data}

def _post_json_dict(
self, endpoint: str, payload: dict[str, Any], operation: str
) -> dict[str, Any] | None:
Expand Down Expand Up @@ -549,6 +561,7 @@ def get_task_status(self, task_id: str) -> MemOSGetTaskStatusResponse | None:
)
response.raise_for_status()
response_data = response.json()
response_data = self._normalize_task_status_response(response_data, task_id)

return MemOSGetTaskStatusResponse(**response_data)
except Exception as e:
Expand Down
26 changes: 26 additions & 0 deletions tests/api/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,32 @@ def test_task_status_response_parses_current_object_shape(client_module: Any) ->
assert response.data.memory_views == {"added": 1}


def test_get_task_status_normalizes_legacy_list_response(
client: Any, client_module: Any, monkeypatch: pytest.MonkeyPatch
) -> None:
calls: list[dict[str, Any]] = []

def fake_post(url: str, **kwargs: Any) -> DummyResponse:
calls.append({"url": url, **kwargs})
return DummyResponse(
{
"code": 200,
"message": "ok",
"data": [{"status": "completed"}],
}
)

monkeypatch.setattr(client_module.requests, "post", fake_post)

response = client.get_task_status("task-legacy")

assert response is not None
assert response.data.task_id == "task-legacy"
assert response.data.status == "completed"
assert response.data.memory_views is None
assert len(calls) == 1


def test_search_response_keeps_all_current_memory_view_lists(client_module: Any) -> None:
response = client_module.MemOSSearchResponse(
code=200,
Expand Down
Loading