Extension Points & Plugin Architecture¶
Referenced Files in This Document - apps/service/core/parser.py - apps/service/core/document_parser.py - apps/service/core/image_parser.py - apps/service/core/image_classifier.py - apps/service/core/pipeline.py - apps/service/core/decision.py - apps/service/core/confidence.py - apps/service/core/matcher.py - apps/service/core/types.py - apps/service/deps.py - apps/service/app.py - apps/service/routes/config.py - apps/service/routes/logs.py - apps/service/routes/status.py - src/notifier.py - src/config.py - src/logger.py
Table of Contents¶
- Introduction
- Project Structure
- Core Components
- Architecture Overview
- Detailed Component Analysis
- Dependency Analysis
- Performance Considerations
- Troubleshooting Guide
- Conclusion
- Appendices
Introduction¶
This document explains the extension points and plugin architecture for the project, focusing on how to add custom parsers for new file formats, integrate additional AI classifiers, and extend notification channels. It describes the plugin registration mechanism, interface contracts, lifecycle management, configuration patterns, dependency injection, testing strategies, versioning considerations, backward compatibility, discovery mechanisms, and security implications.
The system is organized around a pipeline that orchestrates parsing, classification, matching, decision-making, and notifications. Each stage exposes clear interfaces so that custom implementations can be plugged in without modifying core logic.
Project Structure¶
At a high level: - Core processing lives under apps/service/core with modules for parsing, classification, matching, decisions, confidence scoring, and pipeline orchestration. - Service wiring and dependency injection are defined in apps/service/deps.py and apps/service/app.py. - HTTP routes expose configuration and status endpoints used by plugins and dashboards. - Notification extensions live in src/notifier.py. - Global configuration and logging utilities are in src/config.py and src/logger.py.
graph TB
subgraph "Service"
A["app.py"]
B["deps.py"]
C["routes/config.py"]
D["routes/status.py"]
E["routes/logs.py"]
end
subgraph "Core"
P["pipeline.py"]
PAR["parser.py"]
DP["document_parser.py"]
IP["image_parser.py"]
IC["image_classifier.py"]
M["matcher.py"]
DEC["decision.py"]
CONF["confidence.py"]
T["types.py"]
end
subgraph "Extensions"
N["notifier.py"]
CFG["config.py"]
LOG["logger.py"]
end
A --> B
A --> P
P --> PAR
P --> DP
P --> IP
P --> IC
P --> M
P --> DEC
P --> CONF
P --> T
C --> CFG
D --> CFG
E --> LOG
P --> N
Diagram sources - apps/service/app.py - apps/service/deps.py - apps/service/core/pipeline.py - apps/service/core/parser.py - apps/service/core/document_parser.py - apps/service/core/image_parser.py - apps/service/core/image_classifier.py - apps/service/core/matcher.py - apps/service/core/decision.py - apps/service/core/confidence.py - apps/service/core/types.py - apps/service/routes/config.py - apps/service/routes/status.py - apps/service/routes/logs.py - src/notifier.py - src/config.py - src/logger.py
Section sources - apps/service/app.py - apps/service/deps.py - apps/service/core/pipeline.py - apps/service/core/parser.py - apps/service/core/document_parser.py - apps/service/core/image_parser.py - apps/service/core/image_classifier.py - apps/service/core/matcher.py - apps/service/core/decision.py - apps/service/core/confidence.py - apps/service/core/types.py - apps/service/routes/config.py - apps/service/routes/status.py - apps/service/routes/logs.py - src/notifier.py - src/config.py - src/logger.py
Core Components¶
- Pipeline: Orchestrates stages (parse, classify, match, decide, notify). It composes concrete components via dependency injection and exposes hooks for extension.
- Parsers: Abstracted through a common contract; document and image parsers implement format-specific extraction.
- Classifier: Encapsulates AI-based classification logic with pluggable providers.
- Matcher: Applies rules or heuristics to map parsed content to known entities.
- Decision Engine: Computes final actions based on parsed data, classifier outputs, matcher results, and confidence thresholds.
- Notifier: Emits notifications through configured channels.
Key responsibilities and extension points: - Custom parsers: Implement the parser contract and register them with the pipeline. - Custom AI classifiers: Provide an implementation of the classifier interface and wire it into the pipeline. - Additional notification channels: Implement the notifier protocol and configure it via settings.
Section sources - apps/service/core/pipeline.py - apps/service/core/parser.py - apps/service/core/document_parser.py - apps/service/core/image_parser.py - apps/service/core/image_classifier.py - apps/service/core/matcher.py - apps/service/core/decision.py - apps/service/core/confidence.py - apps/service/core/types.py - src/notifier.py
Architecture Overview¶
The pipeline composes components and executes them in sequence. Configuration drives which implementations are active. Dependency injection centralizes wiring and enables swapping implementations at runtime.
sequenceDiagram
participant Client as "Client"
participant API as "FastAPI App"
participant DI as "Deps"
participant Pipe as "Pipeline"
participant Parser as "Parser(s)"
participant Classif as "Classifier"
participant Match as "Matcher"
participant Dec as "Decision"
participant Conf as "Confidence"
participant Notify as "Notifier"
Client->>API : "Request to process media"
API->>DI : "Resolve dependencies"
DI-->>API : "Pipeline + services"
API->>Pipe : "Run pipeline"
Pipe->>Parser : "Extract metadata"
Parser-->>Pipe : "Parsed payload"
Pipe->>Classif : "AI classification"
Classif-->>Pipe : "Labels + scores"
Pipe->>Match : "Apply matching rules"
Match-->>Pipe : "Matches"
Pipe->>Dec : "Compute decision"
Dec->>Conf : "Evaluate confidence"
Conf-->>Dec : "Thresholds"
Dec-->>Pipe : "Action"
Pipe->>Notify : "Send notifications"
Notify-->>Client : "Async delivery"
Diagram sources - apps/service/app.py - apps/service/deps.py - apps/service/core/pipeline.py - apps/service/core/parser.py - apps/service/core/image_classifier.py - apps/service/core/matcher.py - apps/service/core/decision.py - apps/service/core/confidence.py - src/notifier.py
Detailed Component Analysis¶
Parser Extension Point¶
Purpose: Extract structured metadata from files (documents and images). The design separates generic parsing from format-specific implementations.
Interface contract: - A base parser defines a uniform method signature for parsing input and returning a typed result. - Format-specific parsers implement this contract and may declare supported MIME types or file extensions.
Registration mechanism: - Parsers are registered with the pipeline via a registry or factory pattern. - The pipeline selects a parser based on file type or explicit configuration.
Lifecycle: - Initialization: Load configuration and optional resources. - Execution: Parse input, validate output against schema, return structured data. - Teardown: Release resources if needed.
Example integration steps: - Implement the parser interface for your format. - Register the parser with the pipeline’s registry. - Configure supported types and any required options.
classDiagram
class BaseParser {
+parse(input) Result
+supported_types() list
}
class DocumentParser {
+parse(input) Result
+supported_types() list
}
class ImageParser {
+parse(input) Result
+supported_types() list
}
class CustomParser {
+parse(input) Result
+supported_types() list
}
BaseParser <|-- DocumentParser
BaseParser <|-- ImageParser
BaseParser <|-- CustomParser
Diagram sources - apps/service/core/parser.py - apps/service/core/document_parser.py - apps/service/core/image_parser.py
Section sources - apps/service/core/parser.py - apps/service/core/document_parser.py - apps/service/core/image_parser.py
AI Classifier Extension Point¶
Purpose: Provide AI-powered classification of media content. Pluggable providers allow switching between different models or vendors.
Interface contract: - A classifier interface defines methods for classification and optionally capability introspection (e.g., supported labels).
Provider selection: - Providers are selected via configuration or environment variables. - The pipeline injects the active provider instance.
Lifecycle: - Initialize with credentials and model parameters. - Classify inputs and return labels with confidence scores. - Gracefully handle rate limits and errors.
Integration steps: - Implement the classifier interface for your provider. - Wire the provider into the pipeline via dependency injection. - Configure provider-specific settings (endpoints, keys, timeouts).
classDiagram
class BaseClassifier {
+classify(media) ClassificationResult
+capabilities() Capabilities
}
class ProviderA {
+classify(media) ClassificationResult
+capabilities() Capabilities
}
class ProviderB {
+classify(media) ClassificationResult
+capabilities() Capabilities
}
BaseClassifier <|-- ProviderA
BaseClassifier <|-- ProviderB
Diagram sources - apps/service/core/image_classifier.py
Section sources - apps/service/core/image_classifier.py
Decision Engine Extension Point¶
Purpose: Compute final actions based on parsed data, classifier outputs, matcher results, and confidence thresholds.
Extension points: - Add custom rules to the decision engine. - Introduce new confidence scoring strategies. - Extend decision outcomes and associated actions.
Configuration: - Thresholds and rule priorities are configurable. - Rules can be toggled per run or persisted in configuration.
flowchart TD
Start(["Start Decision"]) --> Gather["Gather Inputs<br/>parsed_data, labels, matches"]
Gather --> ApplyRules["Apply Rule Set"]
ApplyRules --> Confidence["Compute Confidence"]
Confidence --> Threshold{"Above Threshold?"}
Threshold --> |Yes| Action["Select Action"]
Threshold --> |No| Fallback["Fallback Action"]
Action --> End(["End Decision"])
Fallback --> End
Diagram sources - apps/service/core/decision.py - apps/service/core/confidence.py
Section sources - apps/service/core/decision.py - apps/service/core/confidence.py
Matcher Extension Point¶
Purpose: Map parsed content to known entities using rules or heuristics.
Extension points: - Add new matching strategies. - Integrate external knowledge bases or indexes. - Adjust weighting and fallback behavior.
Section sources - apps/service/core/matcher.py
Notification Channel Extension Point¶
Purpose: Deliver notifications through multiple channels (e.g., push, email, webhook).
Interface contract: - A notifier protocol defines a send method and channel capabilities.
Registration and configuration: - Channels are configured via settings and activated conditionally. - The pipeline triggers notifications after decisions.
Integration steps: - Implement the notifier protocol for your channel. - Register the channel and configure credentials/endpoints. - Ensure idempotency and retry policies.
Section sources - src/notifier.py
Pipeline Orchestration and Lifecycle¶
Responsibilities: - Compose components via dependency injection. - Execute stages in order with error handling and retries. - Expose hooks for pre/post-processing and metrics.
Lifecycle: - Startup: Resolve dependencies, initialize parsers/classifiers, load config. - Runtime: Process items through the pipeline. - Shutdown: Flush buffers, close connections, release resources.
Section sources - apps/service/core/pipeline.py - apps/service/deps.py - apps/service/app.py
Types and Contracts¶
Shared types define the shape of parsed payloads, classification results, decisions, and configuration objects. These ensure consistency across components and simplify testing.
Section sources - apps/service/core/types.py
Dependency Analysis¶
Dependency injection centralizes component wiring and makes it easy to swap implementations.
graph LR
App["app.py"] --> Deps["deps.py"]
Deps --> Pipe["pipeline.py"]
Pipe --> Parser["parser.py"]
Pipe --> DocP["document_parser.py"]
Pipe --> ImgP["image_parser.py"]
Pipe --> Classif["image_classifier.py"]
Pipe --> Match["matcher.py"]
Pipe --> Dec["decision.py"]
Pipe --> Conf["confidence.py"]
Pipe --> Types["types.py"]
Pipe --> Notif["notifier.py"]
Diagram sources - apps/service/app.py - apps/service/deps.py - apps/service/core/pipeline.py - apps/service/core/parser.py - apps/service/core/document_parser.py - apps/service/core/image_parser.py - apps/service/core/image_classifier.py - apps/service/core/matcher.py - apps/service/core/decision.py - apps/service/core/confidence.py - apps/service/core/types.py - src/notifier.py
Section sources - apps/service/app.py - apps/service/deps.py - apps/service/core/pipeline.py
Performance Considerations¶
- Prefer lazy initialization for heavy components (e.g., AI providers) to reduce startup time.
- Use connection pooling and caching where applicable (e.g., TMDB lookups).
- Batch operations when possible to reduce network calls.
- Instrument key stages with timing and metrics to identify bottlenecks.
- Tune concurrency levels for I/O-bound tasks like parsing and classification.
[No sources needed since this section provides general guidance]
Troubleshooting Guide¶
Common issues and diagnostics: - Misconfigured endpoints or credentials for AI providers: Validate configuration via status and config endpoints. - Parser failures: Check logs for exceptions during parse stage; ensure supported types are correctly declared. - Decision threshold misbehavior: Review confidence thresholds and rule ordering. - Notification delivery failures: Inspect channel logs and retry policies.
Operational endpoints: - Configuration inspection and updates. - Status reporting for service health and component readiness. - Log access for debugging.
Section sources - apps/service/routes/config.py - apps/service/routes/status.py - apps/service/routes/logs.py - src/logger.py
Conclusion¶
The system’s extensibility centers on well-defined interfaces and a dependency-injected pipeline. By implementing the parser, classifier, matcher, decision, and notifier contracts, you can introduce new file formats, AI providers, and notification channels with minimal changes to core logic. Configuration-driven selection and robust lifecycle management enable safe evolution and backward-compatible upgrades.
[No sources needed since this section summarizes without analyzing specific files]
Appendices¶
Plugin Registration Mechanism¶
- Registry: Maintain a mapping from type identifiers (MIME or extension) to parser implementations.
- Factory: Create instances based on configuration or runtime hints.
- Discovery: Optionally scan packages or directories for plugins and auto-register them.
Best practices: - Keep registrations declarative and testable. - Validate capabilities before execution. - Fail fast on invalid configurations.
Section sources - apps/service/core/pipeline.py - apps/service/deps.py
Interface Contracts Summary¶
- Parser: parse(input) -> Result; supported_types() -> list
- Classifier: classify(media) -> ClassificationResult; capabilities() -> Capabilities
- Notifier: send(event) -> bool
- Decision: compute(parsed, labels, matches, thresholds) -> Action
- Matcher: apply(parsed, labels) -> Matches
Section sources - apps/service/core/parser.py - apps/service/core/image_classifier.py - apps/service/core/decision.py - apps/service/core/matcher.py - src/notifier.py
Configuration Patterns¶
- Environment variables for secrets and endpoints.
- YAML/JSON settings for feature flags and thresholds.
- Runtime overrides via configuration endpoints.
Section sources - apps/service/routes/config.py - src/config.py
Dependency Injection for Extensions¶
- Centralized resolver constructs components with their dependencies.
- Swappable implementations via configuration or flags.
- Clear separation between wiring and business logic.
Section sources - apps/service/deps.py - apps/service/app.py
Testing Strategies¶
- Unit tests for individual components against stubbed dependencies.
- Integration tests exercising the full pipeline with mock parsers and classifiers.
- Contract tests ensuring shared types remain compatible.
- Property-based tests for parsers to validate robustness against malformed inputs.
[No sources needed since this section provides general guidance]
Versioning and Backward Compatibility¶
- Semantic versioning for public interfaces and configuration schemas.
- Migration scripts for database and configuration changes.
- Feature flags to gradually roll out breaking changes.
Section sources - apps/service/routes/config.py
Security Implications and Sandboxing¶
- Validate and sanitize all inputs before parsing.
- Restrict network access for untrusted parsers.
- Run untrusted code in isolated processes or containers.
- Enforce least privilege for file system and environment access.
- Audit plugin loading and execution paths.
[No sources needed since this section provides general guidance]