Skip to content

LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows

refactor: Add dynamic plugin loading for enterprise components (#1736)

Hari John Kuriakoseb312f03919b5
diff --git a/backend/migrating/v2/unstract_migrations.py b/backend/migrating/v2/unstract_migrations.py
index 7effa6fe0..69993c3fa 100644
--- a/backend/migrating/v2/unstract_migrations.py
+++ b/backend/migrating/v2/unstract_migrations.py
@@ -1,5 +1,66 @@
+"""Migration utilities for Unstract.
+
+This module provides the UnstractMigration class which extends MigrationQuery.
+Additional migrations can be provided through pluggable_apps.migrations_ext
+which will be dynamically loaded if available.
+"""
+
+import logging
+
from migrating.v2.query import MigrationQuery
+logger = logging.getLogger(__name__)
+
class UnstractMigration(MigrationQuery):
- pass
+ """Migration query class that supports extensible migrations.
+
+ Additional migrations can be loaded from pluggable_apps.migrations_ext
+ if that module is available.
+ """
+
+ def get_public_schema_migrations(self) -> list[dict[str, str]]:
+ """Returns a list of dictionaries containing the schema migration details.
+
+ Returns:
+ list: A list of dictionaries containing the schema migration details.
+ """
+ core_migrations = super().get_public_schema_migrations()
+
+ try:
+ from pluggable_apps.migrations_ext.migrations import (
+ get_extended_public_schema_migrations,
+ )
+
+ return core_migrations + get_extended_public_schema_migrations(self.v2_schema)
+ except ImportError:
+ pass
+
+ return core_migrations
+
+ def get_organization_migrations(
+ self, schema: str, organization_id: str
+ ) -> list[dict[str, str]]:
+ """Returns a list of dictionaries containing the organization migration details.
+
+ Args:
+ schema (str): The name of the schema for the organization.
+ organization_id (str): The ID of the organization.
+
+ Returns:
+ list: A list of dictionaries containing the organization migration details.
+ """
+ core_migrations = super().get_organization_migrations(schema, organization_id)
+
+ try:
+ from pluggable_apps.migrations_ext.migrations import (
+ get_extended_organization_migrations,
+ )
+
+ return core_migrations + get_extended_organization_migrations(
+ self.v2_schema, schema, organization_id
+ )
+ except ImportError:
+ pass
+
+ return core_migrations
diff --git a/backend/plugins/workflow_manager/workflow_v2/api_hub_usage_utils.py b/backend/plugins/workflow_manager/workflow_v2/api_hub_usage_utils.py
index 3b895773e..a92be5503 100644
--- a/backend/plugins/workflow_manager/workflow_v2/api_hub_usage_utils.py
+++ b/backend/plugins/workflow_manager/workflow_v2/api_hub_usage_utils.py
@@ -1,21 +1,18 @@
"""API Hub usage tracking utilities for workflow execution.
This module provides a clean interface for API Hub usage tracking in workflows.
-The OSS version provides default no-op implementations that can be overridden
-by enterprise plugins during the build process.
+Usage tracking functionality is loaded via get_plugin if available.
"""
import logging
+from plugins import get_plugin
+
logger = logging.getLogger(__name__)
class APIHubUsageUtil:
- """Utility class for handling API Hub usage tracking in workflows.
-
- This is the OSS version that provides default no-op implementations.
- Enterprise builds will replace this file with enhanced functionality.
- """
+ """Utility class for handling API Hub usage tracking in workflows."""
@staticmethod
def track_api_hub_usage(
@@ -23,10 +20,7 @@ class APIHubUsageUtil:
workflow_file_execution_id: str,
organization_id: str | None = None,
) -> bool:
- """Track API hub usage if enterprise plugin is available.
-
- OSS version: This is a no-op implementation that always returns False.
- Enterprise version: Will track actual usage in verticals.subscription_usage table.
+ """Track API hub usage if the verticals_usage plugin is available.
Args:
workflow_execution_id: The workflow execution ID
@@ -34,28 +28,52 @@ class APIHubUsageUtil:
organization_id: Optional organization ID
Returns:
- bool: False in OSS version (usage tracking not available).
- Enterprise version returns True if tracking succeeded.
+ bool: True if tracking succeeded, False otherwise.
"""
- # OSS version - no usage tracking available
- logger.debug("API hub usage tracking not available in OSS version")
- return False
+ verticals_usage_plugin = get_plugin("verticals_usage")
+ if not verticals_usage_plugin:
+ return False
+
+ try:
+ headers_cache = verticals_usage_plugin["headers_cache_class"]()
+ usage_tracker = verticals_usage_plugin["service_class"]()
+
+ api_hub_headers = headers_cache.get_headers(workflow_execution_id)
+
+ if api_hub_headers:
+ return usage_tracker.store_usage(
+ file_execution_id=workflow_file_execution_id,
+ api_hub_headers=api_hub_headers,
+ organization_id=organization_id,
+ )
+ return False
+
+ except Exception as e:
+ logger.error(
+ f"Failed to track API hub usage for execution {workflow_execution_id}: {e}"
+ )
+ return False
@staticmethod
def extract_api_hub_headers(request_headers: dict) -> dict | None:
"""Extract API hub headers from request headers.
- OSS version: Returns None (no API hub support).
- Enterprise version: Extracts and normalizes API hub headers.
-
Args:
request_headers: Request headers dictionary
Returns:
- None in OSS version. Enterprise version returns normalized headers.
+ Normalized API hub headers or None if not available.
"""
- # OSS version - no API hub header extraction
- return None
+ verticals_usage_plugin = get_plugin("verticals_usage")
+ if not verticals_usage_plugin:
+ return None
+
+ try:
+ usage_tracker = verticals_usage_plugin["service_class"]()
+ return usage_tracker.extract_api_hub_headers_from_request(request_headers)
+ except Exception as e:
+ logger.error(f"Error extracting API hub headers: {e}")
+ return None
@staticmethod
def cache_api_hub_headers(
@@ -65,16 +83,21 @@ class APIHubUsageUtil:
) -> bool:
"""Cache API hub headers for later usage tracking.
- OSS version: No-op implementation.
- Enterprise version: Caches headers in Redis.
-
Args:
execution_id: The execution ID to use as cache key
headers: Headers to cache
ttl_seconds: Time-to-live in seconds (default 2 hours)
Returns:
- False in OSS version. Enterprise version returns True if caching succeeded.
+ True if caching succeeded, False otherwise.
"""
- # OSS version - no caching available
- return False
+ verticals_usage_plugin = get_plugin("verticals_usage")
+ if not verticals_usage_plugin:
+ return False
+
+ try:
+ headers_cache = verticals_usage_plugin["headers_cache_class"]()
+ return headers_cache.store_headers(execution_id, headers)
+ except Exception as e:
+ logger.error(f"Error caching API hub headers: {e}")
+ return False
diff --git a/backend/plugins/workflow_manager/workflow_v2/utils.py b/backend/plugins/workflow_manager/workflow_v2/utils.py
index f65842ca2..a07c5b5b4 100644
--- a/backend/plugins/workflow_manager/workflow_v2/utils.py
+++ b/backend/plugins/workflow_manager/workflow_v2/utils.py
@@ -1,13 +1,25 @@
+"""Workflow utility functions.
+
+This module provides utility functions for workflow operations, including
+file selection for manual review and rule validation. Extended features
+are loaded from pluggable_apps.manual_review_v2 if available.
+"""
+
+import logging
+import random
from typing import Any
from workflow_manager.endpoint_v2.dto import FileHash
from workflow_manager.workflow_v2.models.workflow import Workflow
+logger = logging.getLogger(__name__)
+
class WorkflowUtil:
- """A utility class for managing workflow operations, particularly for
- selecting files for manual review and updating file destination based on
- review criteria.
+ """Utility class for workflow operations.
+
+ Extended functionality for manual review is loaded from
+ pluggable_apps.manual_review_v2 if available.
"""
@staticmethod
@@ -15,31 +27,38 @@ class WorkflowUtil:
percentage: float,
n: int,
) -> Any:
- """Placeholder method for selecting a subset of files based on a given
- percentage.
+ """Select a subset of files based on a given percentage.
Args:
percentage (float): The percentage of files to select.
n (int): The total number of files.
Returns:
- Any: The method is currently a placeholder and does not return a value.
+ A set of file indices, or None if manual_review_v2 is not available.
"""
- pass
+ num_to_select = max(1, int(n * (percentage / 100)))
+ return set(random.sample(range(1, n + 1), num_to_select))
@classmethod
def get_q_no_list(cls, workflow: Workflow, total_files: int) -> Any:
- """Placeholder method for retrieving a list of files to be reviewed
- based on workflow rules.
+ """Retrieve a list of files to be reviewed based on workflow rules.
Args:
- workflow (Workflow): The workflow instance to be processed.
- total_files (int): The total number of files in the workflow.
+ workflow (Workflow): The workflow instance.
+ total_files (int): The total number of files.
Returns:
- Any: The method is currently a placeholder and does not return a value.
+ A set of file indices to be reviewed, or None.
"""
- pass
+ try:
+ from pluggable_apps.manual_review_v2.helper import get_db_rules_by_workflow_id
+ except ImportError:
+ return None
+
+ q_db_rules = get_db_rules_by_workflow_id(workflow=workflow)
+ if q_db_rules and q_db_rules.percentage > 0:
+ return cls._mrq_files(q_db_rules.percentage, total_files)
+ return None
@staticmethod
def add_file_destination_filehash(
@@ -47,18 +66,26 @@ class WorkflowUtil:
q_file_no_list: Any,
file_hash: FileHash,
) -> FileHash:
- """Updates the file destination in the FileHash object if the file
- index is marked for manual review.
+ """Update the file destination if the file index is marked for review.
Args:
- index (int): The index of the file being processed.
- q_file_no_list (Any): A list or set of file indices marked for review.
- file_hash (FileHash): The FileHash object to be updated.
+ index (int): The index of the file.
+ q_file_no_list: The list of file indices to be reviewed.
+ file_hash (FileHash): The FileHash object.
Returns:
- FileHash: The potentially updated FileHash object with the file
- destination modified.
+ FileHash: The potentially updated FileHash object.
"""
+ if q_file_no_list is None:
+ return file_hash
+
+ try:
+ from workflow_manager.endpoint_v2.models import WorkflowEndpoint
+ except ImportError:
+ return file_hash
+
+ if index in q_file_no_list:
+ file_hash.file_destination = WorkflowEndpoint.ConnectionType.MANUALREVIEW
return file_hash
@staticmethod
@@ -68,21 +95,28 @@ class WorkflowUtil:
file_destination: tuple[str, str] | None,
rule_type: str = "DB",
) -> bool:
- """Placeholder method to check the rule engine - MRQ
+ """Check the rule engine for manual review qualification.
Args:
- result (Optional[Any]): The result dictionary containing
- confidence_data.
+ result: The result dictionary containing confidence_data.
workflow (Workflow): The workflow to retrieve the rules.
- file_destination (Optional[tuple[str, str]]): The destination of
- the file (e.g., MANUALREVIEW or other).
- rule_type (str): Rule type - "DB" for database destination,
- "API" for API deployments with hitl_queue_name.
+ file_destination: The destination of the file.
+ rule_type (str): Rule type - "DB" or "API".
Returns:
- bool: True if the field_key conditions are met based on rule logic
- and file destination.
+ bool: True if conditions are met for manual review.
"""
+ try:
+ from plugins.workflow_manager.workflow_v2.rule_engine import (
+ RuleEngineValidator,
+ )
+
+ return RuleEngineValidator.validate(
+ result, workflow, file_destination, rule_type
+ )
+ except ImportError:
+ pass
+
return False
@staticmethod
@@ -91,20 +125,9 @@ class WorkflowUtil:
workflow: Workflow,
file_destination: tuple[str, str] | None,
) -> bool:
- """Backward compatible method to check the db rules - MRQ
+ """Backward compatible method to check db rules.
Deprecated: Use validate_rule_engine instead.
-
- Args:
- result (Optional[Any]): The result dictionary containing
- confidence_data.
- workflow (Workflow): The workflow to retrieve the rules.
- file_destination (Optional[tuple[str, str]]): The destination of
- the file (e.g., MANUALREVIEW or other).
-
- Returns:
- bool: True if the field_key conditions are met based on rule logic
- and file destination.
"""
return WorkflowUtil.validate_rule_engine(
result, workflow, file_destination, rule_type="DB"
@@ -118,21 +141,27 @@ class WorkflowUtil:
workflow (Workflow): The workflow to check.
Returns:
- bool: True if API rules exist for this workflow, False otherwise.
- For OSS version, always returns False.
+ bool: True if API rules exist for this workflow.
"""
+ try:
+ from pluggable_apps.manual_review_v2.helper import get_db_rules_by_workflow_id
+
+ db_rule = get_db_rules_by_workflow_id(workflow=workflow)
+ return db_rule is not None and db_rule.rule_string is not None
+ except ImportError:
+ pass
+
return False
@staticmethod
- def get_hitl_ttl_seconds(workflow: Workflow) -> Any:
+ def get_hitl_ttl_seconds(workflow: Workflow) -> int | None:
"""Get TTL in seconds for HITL settings for a workflow.
Args:
workflow (Workflow): The workflow to get HITL TTL settings for.
Returns:
- Optional[int]: TTL in seconds if custom TTL is set, None for unlimited TTL.
- For OSS version, always returns None (unlimited TTL).
+ int | None: TTL in seconds if set, None for unlimited TTL.
"""
try:
from pluggable_apps.manual_review_v2.helper import (
diff --git a/docker/docker-compose.yaml b/docker/docker-compose.yaml
index 946b8702c..24b00f0c6 100644
--- a/docker/docker-compose.yaml
+++ b/docker/docker-compose.yaml
@@ -33,6 +33,7 @@ services:
labels:
- traefik.enable=true
- traefik.http.routers.backend.rule=Host(`frontend.unstract.localhost`) && (PathPrefix(`/api/v1`) || PathPrefix(`/deployment`))
+ - traefik.http.services.backend.loadbalancer.server.port=8000
extra_hosts:
# "host-gateway" is a special string that translates to host docker0 i/f IP.
- "host.docker.internal:host-gateway"
@@ -174,6 +175,7 @@ services:
labels:
- traefik.enable=true
- traefik.http.routers.frontend.rule=Host(`frontend.unstract.localhost`) && !PathPrefix(`/api/v1`) && !PathPrefix(`/deployment`)
+ - traefik.http.services.frontend.loadbalancer.server.port=80
platform-service:
image: unstract/platform-service:${VERSION}
diff --git a/docker/dockerfiles/backend.Dockerfile b/docker/dockerfiles/backend.Dockerfile
index 37e2b8da1..122edd6ff 100644
--- a/docker/dockerfiles/backend.Dockerfile
+++ b/docker/dockerfiles/backend.Dockerfile
@@ -73,6 +73,11 @@ RUN uv sync --group deploy --locked && \
uv pip install opentelemetry-instrumentation-openai && \
chmod +x ./entrypoint.sh
+# Install additional requirements if present
+RUN if [ -f requirements.txt ]; then \
+ uv pip install -r requirements.txt; \
+ fi
+
EXPOSE 8000
ENTRYPOINT [ "./entrypoint.sh" ]
diff --git a/docker/dockerfiles/frontend.Dockerfile b/docker/dockerfiles/frontend.Dockerfile
index 1a69660fc..20b8fde2f 100644
--- a/docker/dockerfiles/frontend.Dockerfile
+++ b/docker/dockerfiles/frontend.Dockerfile
@@ -1,8 +1,11 @@
# Multi-stage build for both development and production
+# Global ARG available to all stages
+ARG BUILD_CONTEXT_PATH=frontend
+
# Base stage with common setup
FROM node:20-alpine AS base
-ENV BUILD_CONTEXT_PATH=frontend
+ARG BUILD_CONTEXT_PATH
WORKDIR /app
### FOR DEVELOPMENT ###
@@ -16,9 +19,16 @@ RUN npm install --ignore-scripts
# Copy the rest of the application files
COPY ${BUILD_CONTEXT_PATH}/ /app/
+# Copy the environment script
+COPY ${BUILD_CONTEXT_PATH}/generate-runtime-config.sh /app/generate-runtime-config.sh
+RUN chmod +x /app/generate-runtime-config.sh
+
EXPOSE 3000
-CMD ["npm", "start"]
+# Run the environment config script before starting the
+# dev server, as the node alpine base image does not
+# auto-run /docker-entrypoint.d/*.
+CMD ["/bin/sh", "-c", "/app/generate-runtime-config.sh && npm start"]
### FOR PRODUCTION ###
# Builder stage for production build
@@ -39,6 +49,7 @@ RUN npm run build
# Production stage
FROM nginx:1.29.1-alpine AS production
+ARG BUILD_CONTEXT_PATH
LABEL maintainer="Zipstack Inc."
# Copy built assets from the builder stage
@@ -53,7 +64,7 @@ RUN mkdir -p /usr/share/nginx/html/config && \
chmod 755 /usr/share/nginx/html/config
# Copy the environment script
-COPY ../frontend/generate-runtime-config.sh /docker-entrypoint.d/40-env.sh
+COPY ${BUILD_CONTEXT_PATH}/generate-runtime-config.sh /docker-entrypoint.d/40-env.sh
RUN chmod +x /docker-entrypoint.d/40-env.sh
EXPOSE 80
diff --git a/run-platform.sh b/run-platform.sh
index 7abded3c0..7ffaca714 100755
--- a/run-platform.sh
+++ b/run-platform.sh
@@ -195,10 +195,9 @@ setup_env() {
DEFAULT_AUTH_KEY="unstract"
for service in "${services[@]}"; do
- # Skip services that are spawned at runtime
- for ignore_service in "${spawned_services[@]}"; do
+ for ignore_service in "${ignore_services[@]}"; do
if [[ "$service" == "$ignore_service" ]]; then
- echo -e "Skipped env for ${blue_text}$service${default_text} as it's generated at runtime"
+ echo -e "Skipped env for ignored service ${blue_text}$service${default_text}"
continue 2
fi
done
@@ -344,7 +343,7 @@ first_setup=false
services=($(VERSION=$opt_version $docker_compose_cmd -f "$script_dir/docker/docker-compose.build.yaml" config --services))
# Add workers manually for env setup
services+=("workers")
-spawned_services=("tool-structure" "tool-sidecar")
+ignore_services=("tool-structure" "tool-sidecar" "tool-classifier" "tool-text_extractor" "worker-unified")
current_version=""
target_branch=""