Skip to content

BrainAPI is a knowledge graph–powered AI memory layer that transforms unstructured data into structured knowledge, enabling intelligent search, recommendations, and contextual memory for AI agents and applications.

feat(tasks): added payload to failed cache

Christiane34167164121
diff --git a/src/services/api/routes/ingest.py b/src/services/api/routes/ingest.py
index 32a3052..62c42b0 100644
--- a/src/services/api/routes/ingest.py
+++ b/src/services/api/routes/ingest.py
@@ -8,6 +8,7 @@ Modified By: the developer formerly known as Christian Nonis at <alch.infoemail@
-----
"""
+import json
from fastapi import APIRouter
from typing import Literal
from typing_extensions import Annotated
@@ -18,6 +19,7 @@ from src.services.api.constants.requests import (
IngestionRequestBody,
IngestionStructuredRequestBody,
)
+from src.services.input.agents import cache_adapter
from src.workers.tasks.ingestion import ingest_data as ingest_data_task
from src.workers.tasks.ingestion import (
ingest_structured_data as ingest_structured_data_task,
@@ -33,6 +35,13 @@ async def ingest_data(data: IngestionRequestBody):
"""
task = ingest_data_task.delay(data.model_dump())
+ cache_adapter.set(
+ key=f"task:{task.id}",
+ value=json.dumps({"status": "processing", "task_id": task.id}),
+ brain_id=data.brain_id,
+ expires_in=3600 * 24,
+ )
+
return JSONResponse(
content={"message": "Data ingested successfully", "task_id": task.id}
)
@@ -45,6 +54,13 @@ async def ingest_structured_data(data: IngestionStructuredRequestBody):
"""
task = ingest_structured_data_task.delay(data.model_dump())
+ cache_adapter.set(
+ key=f"task:{task.id}",
+ value=json.dumps({"status": "processing", "task_id": task.id}),
+ brain_id=data.brain_id,
+ expires_in=3600 * 24,
+ )
+
return JSONResponse(
content={"message": "Structured data ingested successfully", "task_id": task.id}
)
@@ -61,4 +77,5 @@ async def ingest_file(
"""
Ingest a file into the processing pipeline and save to the memory.
"""
+
return JSONResponse(content={"message": "File ingested successfully"})
diff --git a/src/workers/tasks/ingestion.py b/src/workers/tasks/ingestion.py
index af88b47..d733890 100644
--- a/src/workers/tasks/ingestion.py
+++ b/src/workers/tasks/ingestion.py
@@ -140,15 +140,11 @@ def ingest_data(self, args: dict):
# ================================================
enrich_kg_from_input(payload.data.text_data, brain_id=payload.brain_id)
- result = {
- "status": "completed",
- "task_id": self.request.id,
- "text_chunk_id": text_chunk.id,
- "observations_count": len(observations),
- }
-
cache_adapter.set(
- f"task:{self.request.id}", json.dumps(result), expires_in=3600
+ key=f"task:{self.request.id}",
+ value=json.dumps({"status": "completed", "task_id": self.request.id}),
+ brain_id=payload.brain_id,
+ expires_in=3600 * 24,
)
return self.request.id
@@ -158,10 +154,14 @@ def ingest_data(self, args: dict):
"status": "failed",
"task_id": self.request.id,
"error": str(e),
+ "payload": payload.model_dump(mode="json"),
}
cache_adapter.set(
- f"task:{self.request.id}", json.dumps(error_result), expires_in=3600
+ key=f"task:{self.request.id}",
+ value=json.dumps(error_result),
+ brain_id=payload.brain_id,
+ expires_in=3600 * 24 * 7,
)
raise
@@ -311,14 +311,11 @@ def ingest_structured_data(self, args: dict):
element.textual_data, targeting=node, brain_id=payload.brain_id
)
- result = {
- "status": "completed",
- "task_id": self.request.id,
- "ingested_count": len(payload.data),
- }
-
cache_adapter.set(
- f"task:{self.request.id}", json.dumps(result), expires_in=3600
+ key=f"task:{self.request.id}",
+ value=json.dumps({"status": "completed", "task_id": self.request.id}),
+ brain_id=payload.brain_id,
+ expires_in=3600 * 24,
)
return self.request.id
@@ -328,10 +325,14 @@ def ingest_structured_data(self, args: dict):
"status": "failed",
"task_id": self.request.id,
"error": str(e),
+ "payload": payload.model_dump(mode="json"),
}
cache_adapter.set(
- f"task:{self.request.id}", json.dumps(error_result), expires_in=3600
+ key=f"task:{self.request.id}",
+ value=json.dumps(error_result),
+ brain_id=payload.brain_id,
+ expires_in=3600 * 24 * 7,
)
raise