Skip to content

A modern platform for visual, flexible, and extensible graph-based investigations. For cybersecurity analysts and investigators.

feat(api): add type detection route and service

dextmorgn972763343e40
diff --git a/flowsint-api/app/api/routes/types.py b/flowsint-api/app/api/routes/types.py
index 7b36f7e0..28a38806 100644
--- a/flowsint-api/app/api/routes/types.py
+++ b/flowsint-api/app/api/routes/types.py
@@ -1,4 +1,5 @@
from fastapi import APIRouter, Depends
+from pydantic import BaseModel
from sqlalchemy.orm import Session
from flowsint_core.core.models import Profile
@@ -16,3 +17,22 @@ async def get_types_list(
"""Get the complete types list for sketches."""
service = create_type_registry_service(db)
return service.get_types_list(current_user.id)
+
+
+class DetectRequest(BaseModel):
+ text: str
+
+
+async def detect_type(
+ body: DetectRequest,
+ db: Session = Depends(get_db),
+ current_user: Profile = Depends(get_current_user),
+):
+ """Detect the type of a given text input.
+
+ Returns the detected type and its fields with the primary field pre-filled.
+ Falls back to Phrase if no type matches.
+ """
+ service = create_type_registry_service(db)
+ return service.detect_type(body.text)
diff --git a/flowsint-core/src/flowsint_core/core/services/type_registry_service.py b/flowsint-core/src/flowsint_core/core/services/type_registry_service.py
index 82d67ffe..0ab4d73e 100644
--- a/flowsint-core/src/flowsint_core/core/services/type_registry_service.py
+++ b/flowsint-core/src/flowsint_core/core/services/type_registry_service.py
@@ -98,6 +98,65 @@ class TypeRegistryService(BaseService):
print(f"Warning: Type {type_name} found in cutsom types.")
return self._extract_input_schema(custom_type, label_key="nodeLabel")
+ def detect_type(self, text: str) -> Dict[str, Any]:
+ """Detect the type of a given text input.
+
+ Iterates over all registered types and calls their detect() classmethod.
+ Returns the detected type name and its required (primary) fields.
+ Falls back to Phrase if no type matches.
+ """
+ from flowsint_types.registry import TYPE_REGISTRY, load_all_types
+
+ load_all_types()
+
+ text = text.strip()
+ if not text:
+ return self._build_detection_result("Phrase", text)
+
+ for type_name, type_cls in TYPE_REGISTRY.all_types().items():
+ if type_name == "Phrase":
+ continue
+ try:
+ if hasattr(type_cls, "detect") and type_cls.detect(text):
+ return self._build_detection_result(type_name, text)
+ except Exception:
+ continue
+
+ return self._build_detection_result("Phrase", text)
+
+ def _build_detection_result(self, type_name: str, value: str) -> Dict[str, Any]:
+ """Build a detection result with type info and primary field pre-filled."""
+ from flowsint_types.registry import get_type
+
+ type_cls = get_type(type_name, case_sensitive=True)
+ if not type_cls:
+ return {"type": type_name, "fields": [], "value": value}
+
+ adapter = TypeAdapter(type_cls)
+ schema = adapter.json_schema()
+ properties = schema.get("properties", {})
+
+ fields = []
+ for prop, info in properties.items():
+ if prop == "nodeLabel":
+ continue
+ is_primary = info.get("primary", False)
+ field = {
+ "name": prop,
+ "label": info.get("title", prop),
+ "description": info.get("description", ""),
+ "required": self._is_required(info),
+ "primary": is_primary,
+ "value": value if is_primary else None,
+ }
+ fields.append(field)
+
+ return {
+ "type": type_name,
+ "key": type_name.lower(),
+ "fields": fields,
+ }
+
def get_types_list(self, user_id: UUID) -> List[Dict[str, Any]]:
from flowsint_types.registry import get_type