Skip to content

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

feat(core): add dedicated services for business logic

dextmorgn1cc88d1033a0
diff --git a/flowsint-core/src/flowsint_core/core/services/__init__.py b/flowsint-core/src/flowsint_core/core/services/__init__.py
new file mode 100644
index 00000000..6e6c6fa3
--- /dev/null
+++ b/flowsint-core/src/flowsint_core/core/services/__init__.py
@@ -0,0 +1,70 @@
+"""
+Service layer for flowsint-core.
+
+This module provides business logic services that encapsulate database operations
+and domain logic, enabling cleaner route handlers and better testability.
+"""
+
+from .exceptions import (
+ ServiceError,
+ NotFoundError,
+ PermissionDeniedError,
+ ValidationError,
+ DatabaseError,
+ AuthenticationError,
+ ConflictError,
+)
+from .base import BaseService
+from .auth_service import AuthService, create_auth_service
+from .key_service import KeyService, create_key_service
+from .investigation_service import InvestigationService, create_investigation_service
+from .sketch_service import SketchService, create_sketch_service
+from .analysis_service import AnalysisService, create_analysis_service
+from .chat_service import ChatService, create_chat_service
+from .scan_service import ScanService, create_scan_service
+from .log_service import LogService, create_log_service
+from .flow_service import FlowService, create_flow_service
+from .custom_type_service import CustomTypeService, create_custom_type_service
+from .type_registry_service import TypeRegistryService, create_type_registry_service
+from .enricher_service import EnricherService, create_enricher_service
+
+__all__ = [
+ # Exceptions
+ "ServiceError",
+ "NotFoundError",
+ "PermissionDeniedError",
+ "ValidationError",
+ "DatabaseError",
+ "AuthenticationError",
+ "ConflictError",
+ # Base
+ "BaseService",
+ # Services - Phase 1
+ "AuthService",
+ "create_auth_service",
+ "KeyService",
+ "create_key_service",
+ # Services - Phase 2
+ "InvestigationService",
+ "create_investigation_service",
+ "SketchService",
+ "create_sketch_service",
+ # Services - Phase 3
+ "AnalysisService",
+ "create_analysis_service",
+ "ChatService",
+ "create_chat_service",
+ "ScanService",
+ "create_scan_service",
+ "LogService",
+ "create_log_service",
+ # Services - Phase 4
+ "FlowService",
+ "create_flow_service",
+ "CustomTypeService",
+ "create_custom_type_service",
+ "TypeRegistryService",
+ "create_type_registry_service",
+ "EnricherService",
+ "create_enricher_service",
+]
diff --git a/flowsint-core/src/flowsint_core/core/services/analysis_service.py b/flowsint-core/src/flowsint_core/core/services/analysis_service.py
new file mode 100644
index 00000000..817aadbf
--- /dev/null
+++ b/flowsint-core/src/flowsint_core/core/services/analysis_service.py
@@ -0,0 +1,215 @@
+"""
+Analysis service for managing analyses within investigations.
+"""
+
+from typing import Any, Dict, List, Optional
+from uuid import UUID, uuid4
+from datetime import datetime
+
+from sqlalchemy import or_
+from sqlalchemy.orm import Session
+
+from ..models import Analysis, InvestigationUserRole
+from ..types import Role
+from .base import BaseService
+from .exceptions import NotFoundError, PermissionDeniedError
+
+
+class AnalysisService(BaseService):
+ """
+ Service for analysis CRUD operations.
+ """
+
+ def get_accessible_analyses(self, user_id: UUID) -> List[Analysis]:
+ """
+ Get all analyses accessible to a user based on their investigation roles.
+
+ Args:
+ user_id: The user's ID
+
+ Returns:
+ List of accessible analyses
+ """
+ allowed_roles = [Role.OWNER, Role.EDITOR, Role.VIEWER]
+
+ query = self._db.query(Analysis).join(
+ InvestigationUserRole,
+ InvestigationUserRole.investigation_id == Analysis.investigation_id,
+ )
+
+ query = query.filter(InvestigationUserRole.user_id == user_id)
+
+ conditions = [InvestigationUserRole.roles.any(role) for role in allowed_roles]
+ query = query.filter(or_(*conditions))
+
+ return query.distinct().all()
+
+ def get_by_id(self, analysis_id: UUID, user_id: UUID) -> Analysis:
+ """
+ Get an analysis by ID with permission check.
+
+ Args:
+ analysis_id: The analysis ID
+ user_id: The user's ID
+
+ Returns:
+ The analysis
+
+ Raises:
+ NotFoundError: If analysis not found
+ PermissionDeniedError: If user doesn't have permission
+ """
+ analysis = self._db.query(Analysis).filter(Analysis.id == analysis_id).first()
+ if not analysis:
+ raise NotFoundError("Analysis not found")
+
+ self._check_permission(user_id, analysis.investigation_id, ["read"])
+ return analysis
+
+ def get_by_investigation(
+ self, investigation_id: UUID, user_id: UUID
+ ) -> List[Analysis]:
+ """
+ Get all analyses for an investigation.
+
+ Args:
+ investigation_id: The investigation ID
+ user_id: The user's ID
+
+ Returns:
+ List of analyses
+
+ Raises:
+ PermissionDeniedError: If user doesn't have permission
+ """
+ self._check_permission(user_id, investigation_id, ["read"])
+
+ return (
+ self._db.query(Analysis)
+ .filter(Analysis.investigation_id == investigation_id)
+ .all()
+ )
+
+ def create(
+ self,
+ title: str,
+ description: Optional[str],
+ content: Optional[Dict[str, Any]],
+ investigation_id: UUID,
+ owner_id: UUID,
+ ) -> Analysis:
+ """
+ Create a new analysis.
+
+ Args:
+ title: Analysis title
+ description: Analysis description
+ content: Analysis content (JSON)
+ investigation_id: Parent investigation ID
+ owner_id: Owner user ID
+
+ Returns:
+ The created analysis
+
+ Raises:
+ PermissionDeniedError: If user can't create in this investigation
+ """
+ self._check_permission(owner_id, investigation_id, ["create"])
+
+ new_analysis = Analysis(
+ id=uuid4(),
+ title=title,
+ description=description,
+ content=content,
+ owner_id=owner_id,
+ investigation_id=investigation_id,
+ created_at=datetime.utcnow(),
+ last_updated_at=datetime.utcnow(),
+ )
+ self._add(new_analysis)
+ self._commit()
+ self._refresh(new_analysis)
+ return new_analysis
+
+ def update(
+ self,
+ analysis_id: UUID,
+ user_id: UUID,
+ title: Optional[str] = None,
+ description: Optional[str] = None,
+ content: Optional[Dict[str, Any]] = None,
+ investigation_id: Optional[UUID] = None,
+ ) -> Analysis:
+ """
+ Update an analysis.
+
+ Args:
+ analysis_id: The analysis ID
+ user_id: The user's ID
+ title: New title (optional)
+ description: New description (optional)
+ content: New content (optional)
+ investigation_id: New investigation ID (optional)
+
+ Returns:
+ The updated analysis
+
+ Raises:
+ NotFoundError: If analysis not found
+ PermissionDeniedError: If user doesn't have permission
+ """
+ analysis = self._db.query(Analysis).filter(Analysis.id == analysis_id).first()
+ if not analysis:
+ raise NotFoundError("Analysis not found")
+
+ self._check_permission(user_id, analysis.investigation_id, ["update"])
+
+ if title is not None:
+ analysis.title = title
+ if description is not None:
+ analysis.description = description
+ if content is not None:
+ analysis.content = content
+ if investigation_id is not None:
+ # Check permission for the new investigation as well
+ self._check_permission(user_id, investigation_id, ["update"])
+ analysis.investigation_id = investigation_id
+
+ analysis.last_updated_at = datetime.utcnow()
+ self._commit()
+ self._refresh(analysis)
+ return analysis
+
+ def delete(self, analysis_id: UUID, user_id: UUID) -> None:
+ """
+ Delete an analysis.
+
+ Args:
+ analysis_id: The analysis ID
+ user_id: The user's ID
+
+ Raises:
+ NotFoundError: If analysis not found
+ PermissionDeniedError: If user doesn't have permission
+ """
+ analysis = self._db.query(Analysis).filter(Analysis.id == analysis_id).first()
+ if not analysis:
+ raise NotFoundError("Analysis not found")
+
+ self._check_permission(user_id, analysis.investigation_id, ["delete"])
+
+ self._delete(analysis)
+ self._commit()
+
+
+def create_analysis_service(db: Session) -> AnalysisService:
+ """
+ Factory function to create an AnalysisService instance.
+
+ Args:
+ db: SQLAlchemy database session
+
+ Returns:
+ Configured AnalysisService instance
+ """
+ return AnalysisService(db=db)
diff --git a/flowsint-core/src/flowsint_core/core/services/auth_service.py b/flowsint-core/src/flowsint_core/core/services/auth_service.py
new file mode 100644
index 00000000..5efd24fb
--- /dev/null
+++ b/flowsint-core/src/flowsint_core/core/services/auth_service.py
@@ -0,0 +1,94 @@
+"""
+Authentication service for user login and registration.
+"""
+
+from typing import Dict, Any
+from sqlalchemy.orm import Session
+from sqlalchemy.exc import IntegrityError
+
+from ..models import Profile
+from ..auth import verify_password, create_access_token, get_password_hash
+from .base import BaseService
+from .exceptions import AuthenticationError, ConflictError, DatabaseError
+
+
+class AuthService(BaseService):
+ """
+ Service for user authentication and registration.
+ """
+
+ def authenticate(self, email: str, password: str) -> Dict[str, Any]:
+ """
+ Authenticate a user and return an access token.
+
+ Args:
+ email: User's email
+ password: User's password
+
+ Returns:
+ Dictionary containing access_token, user_id, and token_type
+
+ Raises:
+ AuthenticationError: If credentials are invalid
+ """
+ user = self._db.query(Profile).filter(Profile.email == email).first()
+
+ if not user or not verify_password(password, user.hashed_password):
+ raise AuthenticationError("Incorrect email or password")
+
+ access_token = create_access_token(data={"sub": user.email})
+
+ return {
+ "access_token": access_token,
+ "user_id": user.id,
+ "token_type": "bearer",
+ }
+
+ def register(self, email: str, password: str) -> Dict[str, Any]:
+ """
+ Register a new user.
+
+ Args:
+ email: User's email
+ password: User's password
+
+ Returns:
+ Dictionary containing success message and email
+
+ Raises:
+ ConflictError: If email is already registered
+ DatabaseError: If database operation fails
+ """
+ existing_user = self._db.query(Profile).filter(Profile.email == email).first()
+
+ if existing_user:
+ raise ConflictError("Email already registered")
+
+ hashed_password = get_password_hash(password)
+ new_user = Profile(email=email, hashed_password=hashed_password)
+
+ try:
+ self._add(new_user)
+ self._commit()
+ self._refresh(new_user)
+
+ return {
+ "message": "User registered successfully",
+ "email": new_user.email,
+ }
+ except IntegrityError:
+ self._rollback()
+ raise ConflictError("Email already registered")
+
+
+def create_auth_service(db: Session) -> AuthService:
+ """
+ Factory function to create an AuthService instance.
+
+ Args:
+ db: SQLAlchemy database session
+
+ Returns:
+ Configured AuthService instance
+ """
+ return AuthService(db=db)
diff --git a/flowsint-core/src/flowsint_core/core/services/base.py b/flowsint-core/src/flowsint_core/core/services/base.py
new file mode 100644
index 00000000..11f49142
--- /dev/null
+++ b/flowsint-core/src/flowsint_core/core/services/base.py
@@ -0,0 +1,194 @@
+"""
+Base service class providing common functionality for all services.
+"""
+
+from typing import Type, TypeVar, Optional, List
+from uuid import UUID
+
+from sqlalchemy.orm import Session
+from sqlalchemy.exc import SQLAlchemyError
+
+from .exceptions import DatabaseError, NotFoundError, PermissionDeniedError
+from ..models import InvestigationUserRole
+from ..types import Role
+
+T = TypeVar("T")
+
+
+class BaseService:
+ """
+ Base class for all services.
+
+ Provides common database operations and error handling patterns.
+ """
+
+ def __init__(self, db: Session):
+ """
+ Initialize the service with a database session.
+
+ Args:
+ db: SQLAlchemy database session
+ """
+ self._db = db
+
+ @property
+ def db(self) -> Session:
+ """Get the database session."""
+ return self._db
+
+ def _commit(self) -> None:
+ """
+ Commit the current transaction.
+
+ Raises:
+ DatabaseError: If the commit fails
+ """
+ try:
+ self._db.commit()
+ except SQLAlchemyError as e:
+ self._db.rollback()
+ raise DatabaseError(f"Database error: {e}")
+
+ def _rollback(self) -> None:
+ """Rollback the current transaction."""
+ self._db.rollback()
+
+ def _flush(self) -> None:
+ """
+ Flush pending changes to the database.
+
+ Raises:
+ DatabaseError: If the flush fails
+ """
+ try:
+ self._db.flush()
+ except SQLAlchemyError as e:
+ self._db.rollback()
+ raise DatabaseError(f"Database error: {e}")
+
+ def _refresh(self, entity: T) -> T:
+ """
+ Refresh an entity from the database.
+
+ Args:
+ entity: The entity to refresh
+
+ Returns:
+ The refreshed entity
+ """
+ self._db.refresh(entity)
+ return entity
+
+ def _add(self, entity: T) -> T:
+ """
+ Add an entity to the session.
+
+ Args:
+ entity: The entity to add
+
+ Returns:
+ The added entity
+ """
+ self._db.add(entity)
+ return entity
+
+ def _delete(self, entity: T) -> None:
+ """
+ Mark an entity for deletion.
+
+ Args:
+ entity: The entity to delete
+ """
+ self._db.delete(entity)
+
+ def _get_or_404(self, model: Type[T], id: UUID) -> T:
+ """
+ Get an entity by ID or raise NotFoundError.
+
+ Args:
+ model: The SQLAlchemy model class
+ id: The entity ID
+
+ Returns:
+ The found entity
+
+ Raises:
+ NotFoundError: If the entity is not found
+ """
+ entity = self._db.query(model).filter(model.id == id).first()
+ if not entity:
+ raise NotFoundError(f"{model.__name__} not found")
+ return entity
+
+ def _get_by_id(self, model: Type[T], id: UUID) -> Optional[T]:
+ """
+ Get an entity by ID, returning None if not found.
+
+ Args:
+ model: The SQLAlchemy model class
+ id: The entity ID
+
+ Returns:
+ The found entity or None
+ """
+ return self._db.query(model).filter(model.id == id).first()
+
+ def _get_all(self, model: Type[T]) -> List[T]:
+ """
+ Get all entities of a given model.
+
+ Args:
+ model: The SQLAlchemy model class
+
+ Returns:
+ List of all entities
+ """
+ return self._db.query(model).all()
+
+ def _can_user(self, roles: List[Role], actions: List[str]) -> bool:
+ """
+ Check if at least one role in the list allows at least one action.
+
+ Args:
+ roles: List of user roles
+ actions: List of actions to check
+
+ Returns:
+ True if any role allows any action
+ """
+ for role in roles:
+ for action in actions:
+ if role == Role.OWNER:
+ return True
+ if role == Role.EDITOR and action in ["read", "create", "update"]:
+ return True
+ if role == Role.VIEWER and action == "read":
+ return True
+ return False
+
+ def _check_permission(
+ self, user_id: UUID, investigation_id: UUID, actions: List[str]
+ ) -> bool:
+ """
+ Check if a user has permission to perform actions on an investigation.
+
+ Args:
+ user_id: The user's ID
+ investigation_id: The investigation ID
+ actions: List of actions to check
+
+ Returns:
+ True if user has permission
+
+ Raises:
+ PermissionDeniedError: If user doesn't have permission
+ """
+ role_entry = (
+ self._db.query(InvestigationUserRole)
+ .filter_by(user_id=user_id, investigation_id=investigation_id)
+ .first()
+ )
+
+ if not role_entry or not self._can_user(role_entry.roles, actions):
+ raise PermissionDeniedError("Forbidden")
+ return True
diff --git a/flowsint-core/src/flowsint_core/core/services/chat_service.py b/flowsint-core/src/flowsint_core/core/services/chat_service.py
new file mode 100644
index 00000000..ac897fef
--- /dev/null
+++ b/flowsint-core/src/flowsint_core/core/services/chat_service.py
@@ -0,0 +1,288 @@
+"""
+Chat service for managing chats and messages with AI integration.
+"""
+
+from typing import Any, Dict, List, Optional
+from uuid import UUID, uuid4
+from datetime import datetime
+import os
+import json
+
+from sqlalchemy.orm import Session
+
+from ..models import Chat, ChatMessage
+from .base import BaseService
+from .exceptions import NotFoundError, PermissionDeniedError, DatabaseError
+
+
+def clean_context(context: List[Dict]) -> List[Dict]:
+ """Remove unnecessary keys from context data."""
+ cleaned = []
+ for item in context:
+ if isinstance(item, dict):
+ cleaned_item = item.get("data", item).copy()
+ cleaned_item.pop("id", None)
+ cleaned_item.pop("sketch_id", None)
+ if "data" in cleaned_item and isinstance(cleaned_item["data"], dict):
+ cleaned_item["data"].pop("sketch_id", None)
+ cleaned_item.pop("measured", None)
+ cleaned.append(cleaned_item)
+ return cleaned
+
+
+class ChatService(BaseService):
+ """
+ Service for chat CRUD operations and AI message streaming.
+ """
+
+ def get_chats_for_user(self, user_id: UUID) -> List[Chat]:
+ """
+ Get all chats owned by a user.
+
+ Args:
+ user_id: The user's ID
+
+ Returns:
+ List of chats with sorted messages
+ """
+ chats = self._db.query(Chat).filter(Chat.owner_id == user_id).all()
+
+ # Sort messages for each chat by created_at in ascending order
+ for chat in chats:
+ chat.messages.sort(key=lambda x: x.created_at)
+
+ return chats
+
+ def get_by_investigation(
+ self, investigation_id: UUID, user_id: UUID
+ ) -> List[Chat]:
+ """
+ Get all chats for an investigation.
+
+ Args:
+ investigation_id: The investigation ID
+ user_id: The user's ID
+
+ Returns:
+ List of chats with sorted messages
+ """
+ chats = (
+ self._db.query(Chat)
+ .filter(Chat.investigation_id == investigation_id, Chat.owner_id == user_id)
+ .order_by(Chat.created_at.asc())
+ .all()
+ )
+
+ for chat in chats:
+ chat.messages.sort(key=lambda x: x.created_at)
+
+ return chats
+
+ def get_by_id(self, chat_id: UUID, user_id: UUID) -> Chat:
+ """
+ Get a chat by ID.
+
+ Args:
+ chat_id: The chat ID
+ user_id: The user's ID
+
+ Returns:
+ The chat with sorted messages
+
+ Raises:
+ NotFoundError: If chat not found or doesn't belong to user
+ """
+ chat = (
+ self._db.query(Chat)
+ .filter(Chat.id == chat_id, Chat.owner_id == user_id)
+ .first()
+ )
+ if not chat:
+ raise NotFoundError("Chat not found")
+
+ chat.messages.sort(key=lambda x: x.created_at)
+ return chat
+
+ def create(
+ self,
+ title: str,
+ description: Optional[str],
+ investigation_id: Optional[UUID],
+ owner_id: UUID,
+ ) -> Chat:
+ """
+ Create a new chat.
+
+ Args:
+ title: Chat title
+ description: Chat description
+ investigation_id: Parent investigation ID (optional)
+ owner_id: Owner user ID
+
+ Returns:
+ The created chat
+ """
+ new_chat = Chat(
+ id=uuid4(),
+ title=title,
+ description=description,
+ owner_id=owner_id,
+ investigation_id=investigation_id,
+ created_at=datetime.utcnow(),
+ last_updated_at=datetime.utcnow(),
+ )
+ self._add(new_chat)
+ self._commit()
+ self._refresh(new_chat)
+ return new_chat
+
+ def delete(self, chat_id: UUID, user_id: UUID) -> None:
+ """
+ Delete a chat.
+
+ Args:
+ chat_id: The chat ID
+ user_id: The user's ID
+
+ Raises:
+ NotFoundError: If chat not found or doesn't belong to user
+ """
+ chat = (
+ self._db.query(Chat)
+ .filter(Chat.id == chat_id, Chat.owner_id == user_id)
+ .first()
+ )
+ if not chat:
+ raise NotFoundError("Chat not found")
+
+ self._delete(chat)
+ self._commit()
+
+ def add_user_message(
+ self, chat_id: UUID, user_id: UUID, content: str, context: Optional[List[Dict]] = None
+ ) -> ChatMessage:
+ """
+ Add a user message to a chat.
+
+ Args:
+ chat_id: The chat ID
+ user_id: The user's ID
+ content: Message content
+ context: Optional context data
+
+ Returns:
+ The created message
+
+ Raises:
+ NotFoundError: If chat not found
+ """
+ chat = (
+ self._db.query(Chat)
+ .filter(Chat.id == chat_id, Chat.owner_id == user_id)
+ .first()
+ )
+ if not chat:
+ raise NotFoundError("Chat not found")
+
+ # Update chat's last_updated_at
+ chat.last_updated_at = datetime.utcnow()
+
+ user_message = ChatMessage(
+ id=uuid4(),
+ content=content,
+ context=context,
+ chat_id=chat_id,
+ is_bot=False,
+ created_at=datetime.utcnow(),
+ )
+ self._add(user_message)
+ self._commit()
+ self._refresh(user_message)
+ return user_message
+
+ def add_bot_message(self, chat_id: UUID, content: str) -> ChatMessage:
+ """
+ Add a bot message to a chat.
+
+ Args:
+ chat_id: The chat ID
+ content: Message content
+
+ Returns:
+ The created message
+ """
+ chat_message = ChatMessage(
+ id=uuid4(),
+ content=content,
+ chat_id=chat_id,
+ is_bot=True,
+ created_at=datetime.utcnow(),
+ )
+ self._add(chat_message)
+ self._commit()
+ self._refresh(chat_message)
+ return chat_message
+
+ def get_chat_with_context(self, chat_id: UUID, user_id: UUID) -> Chat:
+ """
+ Get a chat with its messages for AI context building.
+
+ Args:
+ chat_id: The chat ID
+ user_id: The user's ID
+
+ Returns:
+ The chat
+
+ Raises:
+ NotFoundError: If chat not found
+ """
+ return self.get_by_id(chat_id, user_id)
+
+ def prepare_ai_context(
+ self, chat: Chat, user_prompt: str, context: Optional[List[Dict]] = None
+ ) -> Dict[str, Any]:
+ """
+ Prepare context for AI message generation.
+
+ Args:
+ chat: The chat
+ user_prompt: The user's prompt
+ context: Optional additional context
+
+ Returns:
+ Dictionary with prepared context for AI
+ """
+ context_message = None
+ if context:
+ try:
+ cleaned_context = clean_context(context)
+ if cleaned_context:
+ context_str = json.dumps(cleaned_context, indent=2, default=str)
+ context_message = f"Context: {context_str}"
+ if len(context_message) > 2000:
+ context_message = context_message[:2000] + "..."
+ except Exception as e:
+ print(f"Context processing error: {e}")
+
+ sorted_messages = sorted(chat.messages, key=lambda x: x.created_at)
+ recent_messages = sorted_messages[-5:] if len(sorted_messages) > 5 else sorted_messages
+
+ return {
+ "recent_messages": recent_messages,
+ "context_message": context_message,
+ "user_prompt": user_prompt,
+ }
+
+
+def create_chat_service(db: Session) -> ChatService:
+ """
+ Factory function to create a ChatService instance.
+
+ Args:
+ db: SQLAlchemy database session
+
+ Returns:
+ Configured ChatService instance
+ """
+ return ChatService(db=db)
diff --git a/flowsint-core/src/flowsint_core/core/services/custom_type_service.py b/flowsint-core/src/flowsint_core/core/services/custom_type_service.py
new file mode 100644
index 00000000..3b7b770f
--- /dev/null
+++ b/flowsint-core/src/flowsint_core/core/services/custom_type_service.py
@@ -0,0 +1,270 @@
+"""
+Custom type service for managing user-defined types.
+"""
+
+from typing import Any, Dict, List, Optional, Tuple
+from uuid import UUID
+
+from sqlalchemy.orm import Session
+
+from ..models import CustomType
+from .base import BaseService
+from .exceptions import NotFoundError, ValidationError, ConflictError
+
+
+class CustomTypeService(BaseService):
+ """
+ Service for custom type CRUD operations and validation.
+ """
+
+ def list_custom_types(
+ self, user_id: UUID, status: Optional[str] = None
+ ) -> List[CustomType]:
+ """
+ List all custom types for a user.
+
+ Args:
+ user_id: The user's ID
+ status: Optional status filter (draft, published, archived)
+
+ Returns:
+ List of custom types
+
+ Raises:
+ ValidationError: If invalid status provided
+ """
+ query = self._db.query(CustomType).filter(CustomType.owner_id == user_id)
+
+ if status:
+ if status not in ["draft", "published", "archived"]:
+ raise ValidationError(
+ "Status must be one of: draft, published, archived"
+ )
+ query = query.filter(CustomType.status == status)
+
+ return query.order_by(CustomType.created_at.desc()).all()
+
+ def get_by_id(self, custom_type_id: UUID, user_id: UUID) -> CustomType:
+ """
+ Get a custom type by ID.
+
+ Args:
+ custom_type_id: The custom type ID
+ user_id: The user's ID
+
+ Returns:
+ The custom type
+
+ Raises:
+ NotFoundError: If custom type not found
+ """
+ custom_type = (
+ self._db.query(CustomType)
+ .filter(CustomType.id == custom_type_id, CustomType.owner_id == user_id)
+ .first()
+ )
+ if not custom_type:
+ raise NotFoundError("Custom type not found")
+ return custom_type
+
+ def get_schema(self, custom_type_id: UUID, user_id: UUID) -> Dict[str, Any]:
+ """
+ Get the raw JSON Schema for a custom type.
+
+ Args:
+ custom_type_id: The custom type ID
+ user_id: The user's ID
+
+ Returns:
+ The JSON schema
+
+ Raises:
+ NotFoundError: If custom type not found
+ """
+ custom_type = self.get_by_id(custom_type_id, user_id)
+ return custom_type.schema
+
+ def create(
+ self,
+ name: str,
+ json_schema: Dict[str, Any],
+ user_id: UUID,
+ description: Optional[str] = None,
+ status: str = "draft",
+ validate_schema_func=None,
+ calculate_checksum_func=None,
+ ) -> CustomType:
+ """
+ Create a new custom type.
+
+ Args:
+ name: Type name
+ json_schema: The JSON Schema
+ user_id: The owner's ID
+ description: Optional description
+ status: Initial status (default: draft)
+ validate_schema_func: Function to validate JSON schema
+ calculate_checksum_func: Function to calculate schema checksum
+
+ Returns:
+ The created custom type
+
+ Raises:
+ ValidationError: If schema is invalid
+ ConflictError: If name already exists
+ """
+ # Validate the JSON Schema
+ if validate_schema_func:
+ validate_schema_func(json_schema)
+
+ # Calculate checksum
+ checksum = calculate_checksum_func(json_schema) if calculate_checksum_func else None
+
+ # Check for duplicate name for this user
+ existing = (
+ self._db.query(CustomType)
+ .filter(CustomType.owner_id == user_id, CustomType.name == name)
+ .first()
+ )
+ if existing:
+ raise ConflictError(f"Custom type with name '{name}' already exists")
+
+ # Create the custom type
+ db_custom_type = CustomType(
+ name=name,
+ owner_id=user_id,
+ schema=json_schema,
+ description=description,
+ status=status,
+ checksum=checksum,
+ )
+
+ self._add(db_custom_type)
+ self._commit()
+ self._refresh(db_custom_type)
+
+ return db_custom_type
+
+ def update(
+ self,
+ custom_type_id: UUID,
+ user_id: UUID,
+ name: Optional[str] = None,
+ json_schema: Optional[Dict[str, Any]] = None,
+ description: Optional[str] = None,
+ status: Optional[str] = None,
+ validate_schema_func=None,
+ calculate_checksum_func=None,
+ ) -> CustomType:
+ """
+ Update a custom type.
+
+ Args:
+ custom_type_id: The custom type ID
+ user_id: The user's ID
+ name: New name (optional)
+ json_schema: New schema (optional)
+ description: New description (optional)
+ status: New status (optional)
+ validate_schema_func: Function to validate JSON schema
+ calculate_checksum_func: Function to calculate schema checksum
+
+ Returns:
+ The updated custom type
+
+ Raises:
+ NotFoundError: If custom type not found
+ ValidationError: If schema is invalid
+ ConflictError: If name already exists
+ """
+ custom_type = self.get_by_id(custom_type_id, user_id)
+
+ if name is not None:
+ # Check for duplicate name
+ existing = (
+ self._db.query(CustomType)
+ .filter(
+ CustomType.owner_id == user_id,
+ CustomType.name == name,
+ CustomType.id != custom_type_id,
+ )
+ .first()
+ )
+ if existing:
+ raise ConflictError(f"Custom type with name '{name}' already exists")
+ custom_type.name = name
+
+ if json_schema is not None:
+ if validate_schema_func:
+ validate_schema_func(json_schema)
+ custom_type.schema = json_schema
+ if calculate_checksum_func:
+ custom_type.checksum = calculate_checksum_func(json_schema)
+
+ if description is not None:
+ custom_type.description = description
+
+ if status is not None:
+ custom_type.status = status
+
+ self._commit()
+ self._refresh(custom_type)
+
+ return custom_type
+
+ def delete(self, custom_type_id: UUID, user_id: UUID) -> None:
+ """
+ Delete a custom type.
+
+ Args:
+ custom_type_id: The custom type ID
+ user_id: The user's ID
+
+ Raises:
+ NotFoundError: If custom type not found
+ """
+ custom_type = self.get_by_id(custom_type_id, user_id)
+ self._delete(custom_type)
+ self._commit()
+
+ def validate_payload(
+ self,
+ custom_type_id: UUID,
+ user_id: UUID,
+ payload: Dict[str, Any],
+ validate_payload_func=None,
+ ) -> Tuple[bool, Optional[List[str]]]:
+ """
+ Validate a payload against a custom type's schema.
+
+ Args:
+ custom_type_id: The custom type ID
+ user_id: The user's ID
+ payload: The payload to validate
+ validate_payload_func: Function to validate payload against schema
+
+ Returns:
+ Tuple of (is_valid, errors)
+
+ Raises:
+ NotFoundError: If custom type not found
+ """
+ custom_type = self.get_by_id(custom_type_id, user_id)
+
+ if validate_payload_func:
+ return validate_payload_func(payload, custom_type.schema)
+
+ return True, None
+
+
+def create_custom_type_service(db: Session) -> CustomTypeService:
+ """
+ Factory function to create a CustomTypeService instance.
+
+ Args:
+ db: SQLAlchemy database session
+
+ Returns:
+ Configured CustomTypeService instance
+ """
+ return CustomTypeService(db=db)
diff --git a/flowsint-core/src/flowsint_core/core/services/enricher_service.py b/flowsint-core/src/flowsint_core/core/services/enricher_service.py
new file mode 100644
index 00000000..369fbd91
--- /dev/null
+++ b/flowsint-core/src/flowsint_core/core/services/enricher_service.py
@@ -0,0 +1,64 @@
+"""
+Enricher service for managing enricher operations.
+"""
+
+from typing import Any, Dict, List, Optional
+from uuid import UUID
+
+from sqlalchemy import func
+from sqlalchemy.orm import Session
+
+from ..models import CustomType
+from .base import BaseService
+
+
+class EnricherService(BaseService):
+ """
+ Service for enricher operations and listing.
+ """
+
+ def get_enrichers(
+ self, category: Optional[str], user_id: UUID, enricher_registry
+ ) -> List[Dict[str, Any]]:
+ """
+ Get enrichers, optionally filtered by category.
+
+ Args:
+ category: Optional category filter
+ user_id: The user's ID
+ enricher_registry: The enricher registry instance
+
+ Returns:
+ List of enrichers
+ """
+ if not category or category.lower() == "undefined":
+ return enricher_registry.list(exclude=["n8n_connector"])
+
+ # Check if category is a custom type
+ custom_type = (
+ self._db.query(CustomType)
+ .filter(
+ CustomType.owner_id == user_id,
+ CustomType.status == "published",
+ func.lower(CustomType.name) == category.lower(),
+ )
+ .first()
+ )
+
+ if custom_type:
+ return enricher_registry.list(exclude=["n8n_connector"], wobbly_type=True)
+
+ return enricher_registry.list_by_input_type(category, exclude=["n8n_connector"])
+
+
+def create_enricher_service(db: Session) -> EnricherService:
+ """
+ Factory function to create an EnricherService instance.
+
+ Args:
+ db: SQLAlchemy database session
+
+ Returns:
+ Configured EnricherService instance
+ """
+ return EnricherService(db=db)
diff --git a/flowsint-core/src/flowsint_core/core/services/exceptions.py b/flowsint-core/src/flowsint_core/core/services/exceptions.py
new file mode 100644
index 00000000..cc60be0e
--- /dev/null
+++ b/flowsint-core/src/flowsint_core/core/services/exceptions.py
@@ -0,0 +1,56 @@
+"""
+Domain exceptions for the service layer.
+
+These exceptions represent business logic errors that can be caught
+and converted to appropriate HTTP responses by route handlers.
+"""
+
+
+class ServiceError(Exception):
+ """Base exception for all service errors."""
+
+ def __init__(self, message: str = "A service error occurred"):
+ self.message = message
+ super().__init__(self.message)
+
+
+class NotFoundError(ServiceError):
+ """Entity not found."""
+
+ def __init__(self, message: str = "Entity not found"):
+ super().__init__(message)
+
+
+class PermissionDeniedError(ServiceError):
+ """User does not have permission to perform the action."""
+
+ def __init__(self, message: str = "Permission denied"):
+ super().__init__(message)
+
+
+class ValidationError(ServiceError):
+ """Input validation failed."""
+
+ def __init__(self, message: str = "Validation failed"):
+ super().__init__(message)
+
+
+class DatabaseError(ServiceError):
+ """Database operation failed."""
+
+ def __init__(self, message: str = "Database operation failed"):
+ super().__init__(message)
+
+
+class AuthenticationError(ServiceError):
+ """Authentication failed."""
+
+ def __init__(self, message: str = "Authentication failed"):
+ super().__init__(message)
+
+
+class ConflictError(ServiceError):
+ """Resource conflict (e.g., duplicate entry)."""
+
+ def __init__(self, message: str = "Resource conflict"):
+ super().__init__(message)
diff --git a/flowsint-core/src/flowsint_core/core/services/flow_service.py b/flowsint-core/src/flowsint_core/core/services/flow_service.py
new file mode 100644
index 00000000..bfd5cf0a
--- /dev/null
+++ b/flowsint-core/src/flowsint_core/core/services/flow_service.py
@@ -0,0 +1,200 @@
+"""
+Flow service for managing flows and flow computations.
+"""
+
+from typing import Any, Dict, List, Optional
+from uuid import UUID, uuid4
+from datetime import datetime
+
+from sqlalchemy import func
+from sqlalchemy.orm import Session
+
+from ..models import Flow, CustomType, Sketch
+from ..types import FlowBranch, FlowEdge, FlowNode, FlowStep
+from .base import BaseService
+from .exceptions import NotFoundError, PermissionDeniedError
+
+
+class FlowService(BaseService):
+ """
+ Service for flow CRUD operations and flow computations.
+ """
+
+ def get_all_flows(
+ self, category: Optional[str], user_id: UUID
+ ) -> List[Dict[str, Any]]:
+ """
+ Get all flows, optionally filtered by category.
+
+ Args:
+ category: Optional category filter
+ user_id: The user's ID
+
+ Returns:
+ List of flows
+ """
+ if not category or category.lower() == "undefined":
+ return self._db.query(Flow).order_by(Flow.last_updated_at.desc()).all()
+
+ # Check if category is a custom type
+ custom_type = (
+ self._db.query(CustomType)
+ .filter(
+ CustomType.owner_id == user_id,
+ CustomType.status == "published",
+ func.lower(CustomType.name) == category.lower(),
+ )
+ .first()
+ )
+
+ if custom_type:
+ flows = self._db.query(Flow).order_by(Flow.last_updated_at.desc()).all()
+ return [
+ {
+ **(flow.to_dict() if hasattr(flow, "to_dict") else flow.__dict__),
+ "wobblyType": True,
+ }
+ for flow in flows
+ ]
+
+ # Filter by category
+ flows = self._db.query(Flow).order_by(Flow.last_updated_at.desc()).all()
+ return [
+ flow
+ for flow in flows
+ if any(cat.lower() == category.lower() for cat in flow.category)
+ ]
+
+ def get_by_id(self, flow_id: UUID) -> Flow:
+ """
+ Get a flow by ID.
+
+ Args:
+ flow_id: The flow ID
+
+ Returns:
+ The flow
+
+ Raises:
+ NotFoundError: If flow not found
+ """
+ flow = self._db.query(Flow).filter(Flow.id == flow_id).first()
+ if not flow:
+ raise NotFoundError("Flow not found")
+ return flow
+
+ def create(
+ self,
+ name: str,
+ description: Optional[str],
+ category: List[str],
+ flow_schema: Dict[str, Any],
+ ) -> Flow:
+ """
+ Create a new flow.
+
+ Args:
+ name: Flow name
+ description: Flow description
+ category: List of categories
+ flow_schema: Flow schema (nodes and edges)
+
+ Returns:
+ The created flow
+ """
+ new_flow = Flow(
+ id=uuid4(),
+ name=name,
+ description=description,
+ category=category,
+ flow_schema=flow_schema,
+ created_at=datetime.utcnow(),
+ last_updated_at=datetime.utcnow(),
+ )
+ self._add(new_flow)
+ self._commit()
+ self._refresh(new_flow)
+ return new_flow
+
+ def update(
+ self, flow_id: UUID, updates: Dict[str, Any]
+ ) -> Flow:
+ """
+ Update a flow.
+
+ Args:
+ flow_id: The flow ID
+ updates: Dictionary of updates
+
+ Returns:
+ The updated flow
+
+ Raises:
+ NotFoundError: If flow not found
+ """
+ flow = self._db.query(Flow).filter(Flow.id == flow_id).first()
+ if not flow:
+ raise NotFoundError("Flow not found")
+
+ for key, value in updates.items():
+ if key == "category":
+ if "SocialAccount" in value:
+ value.append("Username")
+ setattr(flow, key, value)
+
+ flow.last_updated_at = datetime.utcnow()
+ self._commit()
+ self._refresh(flow)
+ return flow
+
+ def delete(self, flow_id: UUID) -> None:
+ """
+ Delete a flow.
+
+ Args:
+ flow_id: The flow ID
+
+ Raises:
+ NotFoundError: If flow not found
+ """
+ flow = self._db.query(Flow).filter(Flow.id == flow_id).first()
+ if not flow:
+ raise NotFoundError("Flow not found")
+
+ self._delete(flow)
+ self._commit()
+
+ def get_sketch_for_launch(self, sketch_id: str, user_id: UUID) -> Sketch:
+ """
+ Get sketch for flow launch with permission check.
+
+ Args:
+ sketch_id: The sketch ID
+ user_id: The user's ID
+
+ Returns:
+ The sketch
+
+ Raises:
+ NotFoundError: If sketch not found
+ PermissionDeniedError: If user doesn't have permission
+ """
+ sketch = self._db.query(Sketch).filter(Sketch.id == sketch_id).first()
+ if not sketch:
+ raise NotFoundError("Sketch not found")
+
+ self._check_permission(user_id, sketch.investigation_id, ["update"])
+ return sketch
+
+
+def create_flow_service(db: Session) -> FlowService:
+ """
+ Factory function to create a FlowService instance.
+
+ Args:
+ db: SQLAlchemy database session
+
+ Returns:
+ Configured FlowService instance
+ """
+ return FlowService(db=db)
diff --git a/flowsint-core/src/flowsint_core/core/services/investigation_service.py b/flowsint-core/src/flowsint_core/core/services/investigation_service.py
new file mode 100644
index 00000000..4a4587bc
--- /dev/null
+++ b/flowsint-core/src/flowsint_core/core/services/investigation_service.py
@@ -0,0 +1,269 @@
+"""
+Investigation service for managing investigations and user roles.
+"""
+
+from typing import List, Optional
+from uuid import UUID, uuid4
+from datetime import datetime
+
+from sqlalchemy import or_
+from sqlalchemy.orm import Session, selectinload
+
+from ..models import Investigation, InvestigationUserRole, Sketch, Analysis
+from ..types import Role
+from ..graph import create_graph_service
+from .base import BaseService
+from .exceptions import NotFoundError, PermissionDeniedError, DatabaseError
+
+
+class InvestigationService(BaseService):
+ """
+ Service for investigation CRUD operations and role management.
+ """
+
+ def get_accessible_investigations(
+ self, user_id: UUID, allowed_roles: Optional[List[Role]] = None
+ ) -> List[Investigation]:
+ """
+ Get all investigations accessible to a user based on their roles.
+
+ Args:
+ user_id: The user's ID
+ allowed_roles: Optional list of roles to filter by
+
+ Returns:
+ List of accessible investigations
+ """
+ query = self._db.query(Investigation).join(
+ InvestigationUserRole,
+ InvestigationUserRole.investigation_id == Investigation.id,
+ )
+
+ query = query.filter(InvestigationUserRole.user_id == user_id)
+
+ if allowed_roles:
+ conditions = [
+ InvestigationUserRole.roles.any(role) for role in allowed_roles
+ ]
+ query = query.filter(or_(*conditions, Investigation.owner_id == user_id))
+
+ return (
+ query.options(
+ selectinload(Investigation.sketches),
+ selectinload(Investigation.analyses),
+ selectinload(Investigation.owner),
+ )
+ .distinct()
+ .all()
+ )
+
+ def get_by_id(self, investigation_id: UUID, user_id: UUID) -> Investigation:
+ """
+ Get an investigation by ID with permission check.
+
+ Args:
+ investigation_id: The investigation ID
+ user_id: The user's ID
+
+ Returns:
+ The investigation
+
+ Raises:
+ PermissionDeniedError: If user doesn't have read permission
+ NotFoundError: If investigation not found
+ """
+ self._check_permission(user_id, investigation_id, actions=["read"])
+
+ investigation = (
+ self._db.query(Investigation)
+ .options(
+ selectinload(Investigation.sketches),
+ selectinload(Investigation.analyses),
+ selectinload(Investigation.owner),
+ )
+ .filter(Investigation.id == investigation_id)
+ .filter(Investigation.owner_id == user_id)
+ .first()
+ )
+ if not investigation:
+ raise NotFoundError("Investigation not found")
+ return investigation
+
+ def get_sketches(self, investigation_id: UUID, user_id: UUID) -> List[Sketch]:
+ """
+ Get all sketches for an investigation.
+
+ Args:
+ investigation_id: The investigation ID
+ user_id: The user's ID
+
+ Returns:
+ List of sketches
+
+ Raises:
+ PermissionDeniedError: If user doesn't have read permission
+ NotFoundError: If no sketches found
+ """
+ self._check_permission(user_id, investigation_id, actions=["read"])
+
+ sketches = (
+ self._db.query(Sketch)
+ .filter(Sketch.investigation_id == investigation_id)
+ .all()
+ )
+ if not sketches:
+ raise NotFoundError("No sketches found for this investigation")
+ return sketches
+
+ def create(
+ self, name: str, description: Optional[str], owner_id: UUID
+ ) -> Investigation:
+ """
+ Create a new investigation with owner role.
+
+ Args:
+ name: Investigation name
+ description: Investigation description
+ owner_id: The owner's user ID
+
+ Returns:
+ The created investigation
+ """
+ new_investigation = Investigation(
+ id=uuid4(),
+ name=name,
+ description=description or name,
+ owner_id=owner_id,
+ status="active",
+ )
+ self._add(new_investigation)
+
+ new_roles = InvestigationUserRole(
+ id=uuid4(),
+ user_id=owner_id,
+ investigation_id=new_investigation.id,
+ roles=[Role.OWNER],
+ )
+ self._add(new_roles)
+
+ self._commit()
+ self._refresh(new_investigation)
+
+ return new_investigation
+
+ def update(
+ self,
+ investigation_id: UUID,
+ user_id: UUID,
+ name: str,
+ description: str,
+ status: str,
+ ) -> Investigation:
+ """
+ Update an investigation.
+
+ Args:
+ investigation_id: The investigation ID
+ user_id: The user's ID
+ name: New name
+ description: New description
+ status: New status
+
+ Returns:
+ The updated investigation
+
+ Raises:
+ PermissionDeniedError: If user doesn't have write permission
+ NotFoundError: If investigation not found
+ """
+ self._check_permission(user_id, investigation_id, actions=["write"])
+
+ investigation = (
+ self._db.query(Investigation)
+ .filter(Investigation.id == investigation_id)
+ .first()
+ )
+ if not investigation:
+ raise NotFoundError("Investigation not found")
+
+ investigation.name = name
+ investigation.description = description
+ investigation.status = status
+ investigation.last_updated_at = datetime.utcnow()
+
+ self._commit()
+ self._refresh(investigation)
+ return investigation
+
+ def delete(self, investigation_id: UUID, user_id: UUID) -> None:
+ """
+ Delete an investigation and all related data.
+
+ Args:
+ investigation_id: The investigation ID
+ user_id: The user's ID
+
+ Raises:
+ PermissionDeniedError: If user doesn't have delete permission
+ NotFoundError: If investigation not found
+ DatabaseError: If graph cleanup fails
+ """
+ self._check_permission(user_id, investigation_id, actions=["delete"])
+
+ investigation = (
+ self._db.query(Investigation)
+ .filter(
+ Investigation.id == investigation_id,
+ Investigation.owner_id == user_id,
+ )
+ .first()
+ )
+ if not investigation:
+ raise NotFoundError("Investigation not found")
+
+ # Get all sketches related to this investigation
+ sketches = (
+ self._db.query(Sketch)
+ .filter(Sketch.investigation_id == investigation_id)
+ .all()
+ )
+ analyses = (
+ self._db.query(Analysis)
+ .filter(Analysis.investigation_id == investigation_id)
+ .all()
+ )
+
+ # Delete all nodes and relationships for each sketch in Neo4j
+ for sketch in sketches:
+ try:
+ graph_service = create_graph_service(
+ sketch_id=str(sketch.id),
+ enable_batching=False,
+ )
+ graph_service.delete_all_sketch_nodes()
+ except Exception as e:
+ print(f"Neo4j cleanup error for sketch {sketch.id}: {e}")
+ raise DatabaseError("Failed to clean up graph data")
+
+ # Delete all sketches and analyses from PostgreSQL
+ for sketch in sketches:
+ self._delete(sketch)
+ for analysis in analyses:
+ self._delete(analysis)
+
+ # Finally delete the investigation
+ self._delete(investigation)
+ self._commit()
+
+
+def create_investigation_service(db: Session) -> InvestigationService:
+ """
+ Factory function to create an InvestigationService instance.
+
+ Args:
+ db: SQLAlchemy database session
+
+ Returns:
+ Configured InvestigationService instance
+ """
+ return InvestigationService(db=db)
diff --git a/flowsint-core/src/flowsint_core/core/services/key_service.py b/flowsint-core/src/flowsint_core/core/services/key_service.py
new file mode 100644
index 00000000..922eb6e0
--- /dev/null
+++ b/flowsint-core/src/flowsint_core/core/services/key_service.py
@@ -0,0 +1,120 @@
+"""
+API key management service with Vault integration.
+"""
+
+from typing import List, Optional
+from uuid import UUID
+
+from sqlalchemy.orm import Session
+
+from ..models import Key
+from ..vault import Vault
+from .base import BaseService
+from .exceptions import NotFoundError, DatabaseError
+
+
+class KeyService(BaseService):
+ """
+ Service for API key management with encryption via Vault.
+ """
+
+ def get_keys_for_user(self, user_id: UUID) -> List[Key]:
+ """
+ Get all keys for a user.
+
+ Args:
+ user_id: The user's ID
+
+ Returns:
+ List of Key entities (without decrypted values)
+ """
+ return self._db.query(Key).filter(Key.owner_id == user_id).all()
+
+ def get_key_by_id(self, key_id: UUID, user_id: UUID) -> Key:
+ """
+ Get a specific key by ID for a user.
+
+ Args:
+ key_id: The key's ID
+ user_id: The user's ID (for ownership verification)
+
+ Returns:
+ The Key entity
+
+ Raises:
+ NotFoundError: If key not found or doesn't belong to user
+ """
+ key = (
+ self._db.query(Key)
+ .filter(Key.id == key_id, Key.owner_id == user_id)
+ .first()
+ )
+ if not key:
+ raise NotFoundError("Key not found")
+ return key
+
+ def create_key(self, name: str, key_value: str, user_id: UUID) -> Key:
+ """
+ Create a new encrypted API key.
+
+ Args:
+ name: Key name (e.g., "shodan", "whoxy")
+ key_value: The plain text key value
+ user_id: The owner's ID
+
+ Returns:
+ The created Key entity
+
+ Raises:
+ DatabaseError: If key creation fails
+ """
+ try:
+ vault = Vault(db=self._db, owner_id=user_id)
+ key = vault.set_secret(vault_ref=name, plain_key=key_value)
+ if not key:
+ raise DatabaseError("An error occurred creating the key")
+ return key
+ except Exception as e:
+ raise DatabaseError(f"An error occurred creating the key: {e}")
+
+ def delete_key(self, key_id: UUID, user_id: UUID) -> None:
+ """
+ Delete a key by ID.
+
+ Args:
+ key_id: The key's ID
+ user_id: The user's ID (for ownership verification)
+
+ Raises:
+ NotFoundError: If key not found or doesn't belong to user
+ """
+ key = self.get_key_by_id(key_id, user_id)
+ self._delete(key)
+ self._commit()
+
+ def get_decrypted_key(self, name_or_id: str, user_id: UUID) -> Optional[str]:
+ """
+ Get a decrypted key value by name or ID.
+
+ Args:
+ name_or_id: Either the key name or UUID
+ user_id: The owner's ID
+
+ Returns:
+ The decrypted key value or None if not found
+ """
+ vault = Vault(db=self._db, owner_id=user_id)
+ return vault.get_secret(name_or_id)
+
+
+def create_key_service(db: Session) -> KeyService:
+ """
+ Factory function to create a KeyService instance.
+
+ Args:
+ db: SQLAlchemy database session
+
+ Returns:
+ Configured KeyService instance
+ """
+ return KeyService(db=db)
diff --git a/flowsint-core/src/flowsint_core/core/services/log_service.py b/flowsint-core/src/flowsint_core/core/services/log_service.py
new file mode 100644
index 00000000..fbf1b3f3
--- /dev/null
+++ b/flowsint-core/src/flowsint_core/core/services/log_service.py
@@ -0,0 +1,161 @@
+"""
+Log service for managing event logs.
+"""
+
+from typing import List, Optional
+from uuid import UUID
+from datetime import datetime, timedelta
+
+from sqlalchemy.orm import Session
+
+from ..models import Log, Sketch, Scan
+from ..types import Event
+from ..enums import EventLevel
+from .base import BaseService
+from .exceptions import NotFoundError, PermissionDeniedError, DatabaseError
+
+
+class LogService(BaseService):
+ """
+ Service for log operations.
+ """
+
+ def _get_sketch_with_permission(
+ self, sketch_id: str, user_id: UUID, actions: List[str]
+ ) -> Sketch:
+ """Get sketch and verify user has permission."""
+ sketch = self._db.query(Sketch).filter(Sketch.id == sketch_id).first()
+ if not sketch:
+ raise NotFoundError(f"Sketch with id {sketch_id} not found")
+ self._check_permission(user_id, sketch.investigation_id, actions)
+ return sketch
+
+ def get_logs_by_sketch(
+ self,
+ sketch_id: str,
+ user_id: UUID,
+ limit: int = 100,
+ since: Optional[datetime] = None,
+ ) -> List[Event]:
+ """
+ Get historical logs for a specific sketch.
+
+ Args:
+ sketch_id: The sketch ID
+ user_id: The user's ID
+ limit: Maximum number of logs to return
+ since: Only return logs after this time
+
+ Returns:
+ List of Event objects
+
+ Raises:
+ NotFoundError: If sketch not found
+ PermissionDeniedError: If user doesn't have permission
+ """
+ self._get_sketch_with_permission(sketch_id, user_id, ["read"])
+
+ query = (
+ self._db.query(Log)
+ .filter(Log.sketch_id == sketch_id)
+ .order_by(Log.created_at.desc())
+ )
+
+ if since:
+ query = query.filter(Log.created_at > since)
+ else:
+ # Default to last 24 hours if no since parameter
+ query = query.filter(Log.created_at > datetime.utcnow() - timedelta(days=1))
+
+ logs = query.limit(limit).all()
+
+ # Reverse to show chronologically (oldest to newest)
+ logs = list(reversed(logs))
+
+ results = []
+ for log in logs:
+ # Ensure payload is always a dictionary
+ if isinstance(log.content, dict):
+ payload = log.content
+ elif isinstance(log.content, str):
+ payload = {"message": log.content}
+ elif log.content is None:
+ payload = {}
+ else:
+ payload = {"content": str(log.content)}
+
+ results.append(
+ Event(
+ id=str(log.id),
+ sketch_id=str(log.sketch_id) if log.sketch_id else None,
+ type=log.type,
+ payload=payload,
+ created_at=log.created_at,
+ )
+ )
+
+ return results
+
+ def delete_logs_by_sketch(self, sketch_id: str, user_id: UUID) -> dict:
+ """
+ Delete all logs for a specific sketch.
+
+ Args:
+ sketch_id: The sketch ID
+ user_id: The user's ID
+
+ Returns:
+ Success message
+
+ Raises:
+ NotFoundError: If sketch not found
+ PermissionDeniedError: If user doesn't have permission
+ DatabaseError: If deletion fails
+ """
+ self._get_sketch_with_permission(sketch_id, user_id, ["delete"])
+
+ try:
+ self._db.query(Log).filter(Log.sketch_id == sketch_id).delete()
+ self._commit()
+ return {"message": "All logs have been deleted successfully"}
+ except Exception as e:
+ self._rollback()
+ raise DatabaseError(f"Failed to delete logs: {str(e)}")
+
+ def get_scan_with_permission(self, scan_id: str, user_id: UUID) -> Scan:
+ """
+ Get a scan and verify user has permission via sketch.
+
+ Args:
+ scan_id: The scan ID
+ user_id: The user's ID
+
+ Returns:
+ The scan
+
+ Raises:
+ NotFoundError: If scan not found
+ PermissionDeniedError: If user doesn't have permission
+ """
+ scan = self._db.query(Scan).filter(Scan.id == scan_id).first()
+ if not scan:
+ raise NotFoundError(f"Scan with id {scan_id} not found")
+
+ sketch = self._db.query(Sketch).filter(Sketch.id == scan.sketch_id).first()
+ if sketch:
+ self._check_permission(user_id, sketch.investigation_id, ["read"])
+
+ return scan
+
+
+def create_log_service(db: Session) -> LogService:
+ """
+ Factory function to create a LogService instance.
+
+ Args:
+ db: SQLAlchemy database session
+
+ Returns:
+ Configured LogService instance
+ """
+ return LogService(db=db)
diff --git a/flowsint-core/src/flowsint_core/core/services/scan_service.py b/flowsint-core/src/flowsint_core/core/services/scan_service.py
new file mode 100644
index 00000000..7f8d4574
--- /dev/null
+++ b/flowsint-core/src/flowsint_core/core/services/scan_service.py
@@ -0,0 +1,111 @@
+"""
+Scan service for managing scans.
+"""
+
+from typing import List
+from uuid import UUID
+
+from sqlalchemy import or_
+from sqlalchemy.orm import Session
+
+from ..models import Scan, Sketch, InvestigationUserRole
+from ..types import Role
+from .base import BaseService
+from .exceptions import NotFoundError, PermissionDeniedError
+
+
+class ScanService(BaseService):
+ """
+ Service for scan operations.
+ """
+
+ def get_accessible_scans(self, user_id: UUID) -> List[Scan]:
+ """
+ Get all scans accessible to a user based on their investigation roles.
+
+ Args:
+ user_id: The user's ID
+
+ Returns:
+ List of accessible scans
+ """
+ allowed_roles = [Role.OWNER, Role.EDITOR, Role.VIEWER]
+
+ query = (
+ self._db.query(Scan)
+ .join(Sketch, Sketch.id == Scan.sketch_id)
+ .join(
+ InvestigationUserRole,
+ InvestigationUserRole.investigation_id == Sketch.investigation_id,
+ )
+ )
+
+ query = query.filter(InvestigationUserRole.user_id == user_id)
+
+ conditions = [InvestigationUserRole.roles.any(role) for role in allowed_roles]
+ query = query.filter(or_(*conditions))
+
+ return query.distinct().all()
+
+ def get_by_id(self, scan_id: UUID, user_id: UUID) -> Scan:
+ """
+ Get a scan by ID with permission check.
+
+ Args:
+ scan_id: The scan ID
+ user_id: The user's ID
+
+ Returns:
+ The scan
+
+ Raises:
+ NotFoundError: If scan not found
+ PermissionDeniedError: If user doesn't have permission
+ """
+ scan = self._db.query(Scan).filter(Scan.id == scan_id).first()
+ if not scan:
+ raise NotFoundError("Scan not found")
+
+ # Check investigation permission via sketch
+ sketch = self._db.query(Sketch).filter(Sketch.id == scan.sketch_id).first()
+ if sketch:
+ self._check_permission(user_id, sketch.investigation_id, ["read"])
+
+ return scan
+
+ def delete(self, scan_id: UUID, user_id: UUID) -> None:
+ """
+ Delete a scan.
+
+ Args:
+ scan_id: The scan ID
+ user_id: The user's ID
+
+ Raises:
+ NotFoundError: If scan not found
+ PermissionDeniedError: If user doesn't have permission
+ """
+ scan = self._db.query(Scan).filter(Scan.id == scan_id).first()
+ if not scan:
+ raise NotFoundError("Scan not found")
+
+ # Check investigation permission via sketch
+ sketch = self._db.query(Sketch).filter(Sketch.id == scan.sketch_id).first()
+ if sketch:
+ self._check_permission(user_id, sketch.investigation_id, ["delete"])
+
+ self._delete(scan)
+ self._commit()
+
+
+def create_scan_service(db: Session) -> ScanService:
+ """
+ Factory function to create a ScanService instance.
+
+ Args:
+ db: SQLAlchemy database session
+
+ Returns:
+ Configured ScanService instance
+ """
+ return ScanService(db=db)
diff --git a/flowsint-core/src/flowsint_core/core/services/sketch_service.py b/flowsint-core/src/flowsint_core/core/services/sketch_service.py
new file mode 100644
index 00000000..0379a7c8
--- /dev/null
+++ b/flowsint-core/src/flowsint_core/core/services/sketch_service.py
@@ -0,0 +1,399 @@
+"""
+Sketch service for managing sketches and graph operations.
+"""
+
+from typing import Any, Dict, List, Optional
+from uuid import UUID
+
+from sqlalchemy.orm import Session
+
+from ..models import Sketch
+from ..graph import create_graph_service, GraphNode
+from ..graph.types import GraphData
+from .base import BaseService
+from .exceptions import NotFoundError, PermissionDeniedError, ValidationError, DatabaseError
+
+
+class SketchService(BaseService):
+ """
+ Service for sketch CRUD operations and graph interactions.
+ """
+
+ def _get_sketch_with_permission(
+ self, sketch_id: UUID, user_id: UUID, actions: List[str]
+ ) -> Sketch:
+ """Get sketch and verify user has permission."""
+ sketch = self._db.query(Sketch).filter(Sketch.id == sketch_id).first()
+ if not sketch:
+ raise NotFoundError("Sketch not found")
+ self._check_permission(user_id, sketch.investigation_id, actions)
+ return sketch
+
+ def list_sketches(self, user_id: UUID) -> List[Sketch]:
+ """Get all sketches owned by a user."""
+ return self._db.query(Sketch).filter(Sketch.owner_id == user_id).all()
+
+ def get_by_id(self, sketch_id: UUID, user_id: UUID) -> Sketch:
+ """Get a sketch by ID with permission check."""
+ return self._get_sketch_with_permission(sketch_id, user_id, ["read"])
+
+ def create(
+ self,
+ title: str,
+ description: Optional[str],
+ investigation_id: UUID,
+ owner_id: UUID,
+ ) -> Sketch:
+ """
+ Create a new sketch.
+
+ Args:
+ title: Sketch title
+ description: Sketch description
+ investigation_id: Parent investigation ID
+ owner_id: Owner user ID
+
+ Returns:
+ The created sketch
+
+ Raises:
+ ValidationError: If investigation_id is missing
+ PermissionDeniedError: If user can't create in this investigation
+ """
+ if not investigation_id:
+ raise ValidationError("Investigation not found")
+
+ self._check_permission(owner_id, investigation_id, ["create"])
+
+ sketch = Sketch(
+ title=title,
+ description=description,
+ investigation_id=investigation_id,
+ owner_id=owner_id,
+ )
+ self._add(sketch)
+ self._commit()
+ self._refresh(sketch)
+ return sketch
+
+ def update(
+ self, sketch_id: UUID, user_id: UUID, updates: Dict[str, Any]
+ ) -> Sketch:
+ """Update a sketch with permission check."""
+ sketch = self._get_sketch_with_permission(sketch_id, user_id, ["update"])
+
+ for key, value in updates.items():
+ if hasattr(sketch, key):
+ setattr(sketch, key, value)
+
+ self._commit()
+ self._refresh(sketch)
+ return sketch
+
+ def delete(self, sketch_id: UUID, user_id: UUID) -> None:
+ """Delete a sketch and its graph data."""
+ sketch = self._get_sketch_with_permission(sketch_id, user_id, ["delete"])
+
+ # Delete all nodes and relationships in Neo4j first
+ try:
+ graph_service = create_graph_service(
+ sketch_id=str(sketch_id), enable_batching=False
+ )
+ graph_service.delete_all_sketch_nodes()
+ except Exception as e:
+ print(f"Neo4j cleanup error: {e}")
+ raise DatabaseError("Failed to clean up graph data")
+
+ # Then delete the sketch from PostgreSQL
+ self._delete(sketch)
+ self._commit()
+
+ # --- Graph operations ---
+
+ def get_graph(
+ self, sketch_id: UUID, user_id: UUID, format: Optional[str] = None
+ ) -> Dict[str, Any]:
+ """
+ Get the graph data for a sketch.
+
+ Args:
+ sketch_id: Sketch ID
+ user_id: User ID for permission check
+ format: Optional format ("inline" for inline relationships)
+
+ Returns:
+ Graph data as dict with "nds" and "rls" keys, or inline format
+ """
+ sketch = self._get_sketch_with_permission(sketch_id, user_id, ["read"])
+
+ graph_service = create_graph_service(
+ sketch_id=str(sketch_id), enable_batching=False
+ )
+ graph_data = graph_service.get_sketch_graph()
+
+ if format == "inline":
+ from flowsint_core.utils import get_inline_relationships
+ return get_inline_relationships(graph_data.nodes, graph_data.edges)
+
+ graph = graph_data.model_dump(mode="json", serialize_as_any=True)
+ return {"nds": graph["nodes"], "rls": graph["edges"]}
+
+ def add_node(
+ self, sketch_id: UUID, user_id: UUID, node: GraphNode
+ ) -> Dict[str, Any]:
+ """Add a node to the sketch graph."""
+ self._get_sketch_with_permission(sketch_id, user_id, ["update"])
+
+ try:
+ graph_service = create_graph_service(
+ sketch_id=str(sketch_id), enable_batching=False
+ )
+ node_id = graph_service.create_node(node)
+ except Exception as e:
+ print(e)
+ raise DatabaseError(f"Database error: {str(e)}")
+
+ if not node_id:
+ raise ValidationError("Node creation failed")
+
+ node.id = node_id
+ return {"status": "node added", "node": node}
+
+ def add_relationship(
+ self,
+ sketch_id: UUID,
+ user_id: UUID,
+ source: str,
+ target: str,
+ label: str = "RELATED_TO",
+ ) -> Dict[str, Any]:
+ """Add a relationship between nodes."""
+ self._get_sketch_with_permission(sketch_id, user_id, ["update"])
+
+ try:
+ graph_service = create_graph_service(
+ sketch_id=str(sketch_id), enable_batching=False
+ )
+ result = graph_service.create_relationship_by_element_id(
+ from_element_id=source,
+ to_element_id=target,
+ rel_label=label,
+ )
+ except Exception as e:
+ print(f"Edge creation error: {e}")
+ raise DatabaseError("Failed to create edge")
+
+ if not result:
+ raise ValidationError("Edge creation failed")
+
+ return {"status": "edge added", "edge": result}
+
+ def update_node(
+ self, sketch_id: UUID, user_id: UUID, node_id: str, updates: Dict[str, Any]
+ ) -> Dict[str, Any]:
+ """Update a node's properties."""
+ self._get_sketch_with_permission(sketch_id, user_id, ["update"])
+
+ try:
+ graph_service = create_graph_service(
+ sketch_id=str(sketch_id), enable_batching=False
+ )
+ updated_element_id = graph_service.update_node(
+ element_id=node_id, updates=updates
+ )
+ except Exception as e:
+ print(f"Node update error: {e}")
+ raise DatabaseError("Failed to update node")
+
+ if not updated_element_id:
+ raise NotFoundError("Node not found or not accessible")
+
+ return {"status": "node updated", "node": {"id": updated_element_id}}
+
+ def update_node_positions(
+ self, sketch_id: UUID, user_id: UUID, positions: List[Dict[str, Any]]
+ ) -> Dict[str, Any]:
+ """Update positions for multiple nodes."""
+ self._get_sketch_with_permission(sketch_id, user_id, ["update"])
+
+ if not positions:
+ return {"status": "no positions to update", "count": 0}
+
+ try:
+ graph_service = create_graph_service(
+ sketch_id=str(sketch_id), enable_batching=False
+ )
+ updated_count = graph_service.update_nodes_positions(positions=positions)
+ except Exception as e:
+ print(f"Position update error: {e}")
+ raise DatabaseError("Failed to update node positions")
+
+ return {"status": "positions updated", "count": updated_count}
+
+ def delete_nodes(
+ self, sketch_id: UUID, user_id: UUID, node_ids: List[str]
+ ) -> Dict[str, Any]:
+ """Delete nodes from the graph."""
+ self._get_sketch_with_permission(sketch_id, user_id, ["update"])
+
+ try:
+ graph_service = create_graph_service(
+ sketch_id=str(sketch_id), enable_batching=False
+ )
+ deleted_count = graph_service.delete_nodes(node_ids)
+ except Exception as e:
+ print(f"Node deletion error: {e}")
+ raise DatabaseError("Failed to delete nodes")
+
+ return {"status": "nodes deleted", "count": deleted_count}
+
+ def delete_relationships(
+ self, sketch_id: UUID, user_id: UUID, relationship_ids: List[str]
+ ) -> Dict[str, Any]:
+ """Delete relationships from the graph."""
+ self._get_sketch_with_permission(sketch_id, user_id, ["update"])
+
+ try:
+ graph_service = create_graph_service(
+ sketch_id=str(sketch_id), enable_batching=False
+ )
+ deleted_count = graph_service.delete_relationships(relationship_ids)
+ except Exception as e:
+ print(f"Relationship deletion error: {e}")
+ raise DatabaseError("Failed to delete relationships")
+
+ return {"status": "relationships deleted", "count": deleted_count}
+
+ def update_relationship(
+ self,
+ sketch_id: UUID,
+ user_id: UUID,
+ relationship_id: str,
+ data: Dict[str, Any],
+ ) -> Dict[str, Any]:
+ """Update a relationship's properties."""
+ self._get_sketch_with_permission(sketch_id, user_id, ["update"])
+
+ try:
+ graph_service = create_graph_service(
+ sketch_id=str(sketch_id), enable_batching=False
+ )
+ result = graph_service.update_relationship(
+ element_id=relationship_id, properties=data
+ )
+ except Exception as e:
+ print(f"Relationship update error: {e}")
+ raise DatabaseError("Failed to update relationship")
+
+ if not result:
+ raise NotFoundError("Relationship not found or not accessible")
+
+ return {
+ "status": "relationship updated",
+ "relationship": {
+ "id": result["id"],
+ "label": result["type"],
+ "data": result["data"],
+ },
+ }
+
+ def merge_nodes(
+ self,
+ sketch_id: UUID,
+ user_id: UUID,
+ old_node_ids: List[str],
+ new_node_id: str,
+ node_data: Dict[str, Any],
+ ) -> Dict[str, Any]:
+ """Merge multiple nodes into one."""
+ from flowsint_core.utils import flatten
+
+ self._get_sketch_with_permission(sketch_id, user_id, ["update"])
+
+ if not old_node_ids:
+ raise ValidationError("oldNodes cannot be empty")
+
+ node_type = node_data.get("type", "Node")
+ properties = {
+ "type": node_type.lower(),
+ "label": node_data.get("label", "Merged Node"),
+ }
+ flattened_data = flatten(node_data)
+ properties.update(flattened_data)
+
+ try:
+ graph_service = create_graph_service(
+ sketch_id=str(sketch_id), enable_batching=False
+ )
+ new_node_element_id = graph_service.merge_nodes(
+ old_node_ids=old_node_ids,
+ new_node_data=properties,
+ new_node_id=new_node_id,
+ )
+ except Exception as e:
+ print(f"Node merge error: {e}")
+ raise DatabaseError(f"Failed to merge nodes: {str(e)}")
+
+ if not new_node_element_id:
+ raise DatabaseError("Failed to merge nodes")
+
+ return {
+ "status": "nodes merged",
+ "count": len(old_node_ids),
+ "new_node_id": new_node_element_id,
+ }
+
+ def get_neighbors(
+ self, sketch_id: UUID, user_id: UUID, node_id: str
+ ) -> Dict[str, Any]:
+ """Get neighboring nodes and edges for a node."""
+ self._get_sketch_with_permission(sketch_id, user_id, ["read"])
+
+ try:
+ graph_service = create_graph_service(sketch_id=str(sketch_id))
+ result = graph_service.get_neighbors(node_id)
+ except Exception as e:
+ print(e)
+ raise DatabaseError("Failed to retrieve related nodes")
+
+ if not result.nodes:
+ raise NotFoundError("Node not found")
+
+ return {"nds": result.nodes, "rls": result.edges}
+
+ def export_sketch(
+ self, sketch_id: UUID, user_id: UUID, format: str = "json"
+ ) -> Dict[str, Any]:
+ """Export sketch data."""
+ sketch = self._get_sketch_with_permission(sketch_id, user_id, ["read"])
+
+ graph_service = create_graph_service(
+ sketch_id=str(sketch_id), enable_batching=False
+ )
+ graph_data = graph_service.get_sketch_graph()
+
+ if format == "json":
+ return {
+ "sketch": {
+ "id": str(sketch.id),
+ "title": sketch.title,
+ "description": sketch.description,
+ },
+ "nodes": [node.model_dump(mode="json") for node in graph_data.nodes],
+ "edges": [edge.model_dump(mode="json") for edge in graph_data.edges],
+ }
+ else:
+ raise ValidationError(f"Unsupported format: {format}")
+
+
+def create_sketch_service(db: Session) -> SketchService:
+ """
+ Factory function to create a SketchService instance.
+
+ Args:
+ db: SQLAlchemy database session
+
+ Returns:
+ Configured SketchService instance
+ """
+ return SketchService(db=db)
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
new file mode 100644
index 00000000..8e783657
--- /dev/null
+++ b/flowsint-core/src/flowsint_core/core/services/type_registry_service.py
@@ -0,0 +1,328 @@
+"""
+Type registry service for managing flowsint types.
+"""
+
+from typing import Any, Dict, List, Optional, Type
+from uuid import UUID, uuid4
+
+from sqlalchemy.orm import Session
+from pydantic import BaseModel, TypeAdapter
+
+from ..models import CustomType
+from .base import BaseService
+
+
+class TypeRegistryService(BaseService):
+ """
+ Service for type registry operations and schema extraction.
+ """
+
+ def get_types_list(self, user_id: UUID) -> List[Dict[str, Any]]:
+ """
+ Get the complete types list for sketches.
+
+ Args:
+ user_id: The user's ID
+
+ Returns:
+ List of type categories with their children
+ """
+ from flowsint_types.registry import get_type
+
+ # Define categories with type names
+ category_definitions = self._get_category_definitions()
+
+ types = []
+ for category in category_definitions:
+ category_copy = category.copy()
+ children_schemas = []
+
+ for child_def in category["children"]:
+ type_name, label_key, icon = child_def
+ model = get_type(type_name, case_sensitive=True)
+
+ if model:
+ children_schemas.append(
+ self._extract_input_schema(model, label_key=label_key, icon=icon)
+ )
+ else:
+ print(f"Warning: Type {type_name} not found in TYPE_REGISTRY")
+
+ category_copy["children"] = children_schemas
+ types.append(category_copy)
+
+ # Add custom types
+ custom_types = (
+ self._db.query(CustomType)
+ .filter(
+ CustomType.owner_id == user_id,
+ CustomType.status == "published",
+ )
+ .all()
+ )
+
+ if custom_types:
+ custom_types_children = []
+ for custom_type in custom_types:
+ schema = custom_type.schema
+ properties = schema.get("properties", {})
+ required = schema.get("required", [])
+
+ label_key = (
+ required[0]
+ if required
+ else list(properties.keys())[0]
+ if properties
+ else "value"
+ )
+
+ custom_types_children.append(
+ {
+ "id": custom_type.id,
+ "type": custom_type.name,
+ "key": custom_type.name.lower(),
+ "label_key": label_key,
+ "icon": "custom",
+ "label": custom_type.name,
+ "description": custom_type.description or "",
+ "fields": [
+ {
+ "name": prop,
+ "label": info.get("title", prop),
+ "description": info.get("description", ""),
+ "type": "text",
+ "required": prop in required,
+ }
+ for prop, info in properties.items()
+ ],
+ "custom": True,
+ }
+ )
+
+ types.append(
+ {
+ "id": uuid4(),
+ "type": "custom_types_category",
+ "key": "custom_types",
+ "icon": "custom",
+ "label": "Custom types",
+ "fields": [],
+ "children": custom_types_children,
+ }
+ )
+
+ return types
+
+ def _get_category_definitions(self) -> List[Dict[str, Any]]:
+ """Get the category definitions for types."""
+ return [
+ {
+ "id": uuid4(),
+ "type": "global",
+ "key": "global_category",
+ "icon": "phrase",
+ "label": "Global",
+ "fields": [],
+ "children": [
+ ("Phrase", "text", None),
+ ("Location", "address", None),
+ ],
+ },
+ {
+ "id": uuid4(),
+ "type": "person",
+ "key": "person_category",
+ "icon": "individual",
+ "label": "Identities & Entities",
+ "fields": [],
+ "children": [
+ ("Individual", "full_name", None),
+ ("Username", "value", "username"),
+ ("Organization", "name", None),
+ ],
+ },
+ {
+ "id": uuid4(),
+ "type": "organization",
+ "key": "organization_category",
+ "icon": "organization",
+ "label": "Organization",
+ "fields": [],
+ "children": [
+ ("Organization", "name", None),
+ ],
+ },
+ {
+ "id": uuid4(),
+ "type": "contact_category",
+ "key": "contact",
+ "icon": "phone",
+ "label": "Communication & Contact",
+ "fields": [],
+ "children": [
+ ("Phone", "number", None),
+ ("Email", "email", None),
+ ("Username", "value", None),
+ ("SocialAccount", "username", "socialaccount"),
+ ("Message", "content", "message"),
+ ],
+ },
+ {
+ "id": uuid4(),
+ "type": "network_category",
+ "key": "network",
+ "icon": "domain",
+ "label": "Network",
+ "fields": [],
+ "children": [
+ ("ASN", "number", None),
+ ("CIDR", "network", None),
+ ("Domain", "domain", None),
+ ("Website", "url", None),
+ ("Ip", "address", None),
+ ("Port", "number", None),
+ ("DNSRecord", "name", "dns"),
+ ("SSLCertificate", "subject", "ssl"),
+ ("WebTracker", "name", "webtracker"),
+ ],
+ },
+ {
+ "id": uuid4(),
+ "type": "security_category",
+ "key": "security",
+ "icon": "credential",
+ "label": "Security & Access",
+ "fields": [],
+ "children": [
+ ("Credential", "username", "credential"),
+ ("Session", "session_id", "session"),
+ ("Device", "device_id", "device"),
+ ("Malware", "name", "malware"),
+ ("Weapon", "name", "weapon"),
+ ],
+ },
+ {
+ "id": uuid4(),
+ "type": "files_category",
+ "key": "files",
+ "icon": "file",
+ "label": "Files & Documents",
+ "fields": [],
+ "children": [
+ ("Document", "title", "document"),
+ ("File", "filename", "file"),
+ ],
+ },
+ {
+ "id": uuid4(),
+ "type": "financial_category",
+ "key": "financial",
+ "icon": "creditcard",
+ "label": "Financial Data",
+ "fields": [],
+ "children": [
+ ("BankAccount", "account_number", "creditcard"),
+ ("CreditCard", "card_number", "creditcard"),
+ ],
+ },
+ {
+ "id": uuid4(),
+ "type": "leak_category",
+ "key": "leaks",
+ "icon": "breach",
+ "label": "Leaks",
+ "fields": [],
+ "children": [
+ ("Leak", "name", "breach"),
+ ],
+ },
+ {
+ "id": uuid4(),
+ "type": "crypto_category",
+ "key": "crypto",
+ "icon": "cryptowallet",
+ "label": "Crypto",
+ "fields": [],
+ "children": [
+ ("CryptoWallet", "address", "cryptowallet"),
+ ("CryptoWalletTransaction", "hash", "cryptowallet"),
+ ("CryptoNFT", "name", "cryptowallet"),
+ ],
+ },
+ ]
+
+ def _extract_input_schema(
+ self, model: Type[BaseModel], label_key: str, icon: Optional[str] = None
+ ) -> Dict[str, Any]:
+ """Extract input schema from a Pydantic model."""
+ adapter = TypeAdapter(model)
+ schema = adapter.json_schema()
+ type_name = model.__name__
+ details = schema
+
+ return {
+ "id": uuid4(),
+ "type": type_name,
+ "key": type_name.lower(),
+ "label_key": label_key,
+ "icon": icon or type_name.lower(),
+ "label": type_name,
+ "description": details.get("description", ""),
+ "fields": [
+ self._resolve_field(prop, details=info, schema=schema)
+ for prop, info in details.get("properties", {}).items()
+ if prop != "nodeLabel"
+ ],
+ }
+
+ def _resolve_field(
+ self, prop: str, details: dict, schema: dict = None
+ ) -> Dict[str, Any]:
+ """Resolve a field definition from schema."""
+ field = {
+ "name": prop,
+ "label": details.get("title", prop),
+ "description": details.get("description", ""),
+ "type": "text",
+ }
+
+ if self._has_enum(details):
+ field["type"] = "select"
+ field["options"] = [
+ {"label": label, "value": label}
+ for label in self._get_enum_values(details)
+ ]
+
+ field["required"] = self._is_required(details)
+ return field
+
+ def _has_enum(self, schema: dict) -> bool:
+ """Check if schema has enum values."""
+ any_of = schema.get("anyOf", [])
+ return any(isinstance(entry, dict) and "enum" in entry for entry in any_of)
+
+ def _is_required(self, schema: dict) -> bool:
+ """Check if field is required."""
+ any_of = schema.get("anyOf", [])
+ return not any(entry == {"type": "null"} for entry in any_of)
+
+ def _get_enum_values(self, schema: dict) -> list:
+ """Get enum values from schema."""
+ enum_values = []
+ for entry in schema.get("anyOf", []):
+ if isinstance(entry, dict) and "enum" in entry:
+ enum_values.extend(entry["enum"])
+ return enum_values
+
+
+def create_type_registry_service(db: Session) -> TypeRegistryService:
+ """
+ Factory function to create a TypeRegistryService instance.
+
+ Args:
+ db: SQLAlchemy database session
+
+ Returns:
+ Configured TypeRegistryService instance
+ """
+ return TypeRegistryService(db=db)