🔒 You must be logged in as an Administrator or Editor to listen to this audio.
The Startup.cs file in the CrestApps.OrchardCore.AI module acts as the central registry for the module's dependency injection (DI), routing, and feature toggles. Because Orchard Core is a multi-tenant, modular framework, this file is not a single Startup class. Instead, it is divided into multiple StartupBase classes that are dynamically loaded based on which features or dependencies are enabled in the tenant.
Here is a technical guide to understanding how this file is structured and how the system boundaries are defined.
1. The Core Startup Class (The Foundation)
The first Startup class in the file is the trunk of the module. It runs when the base CrestApps.OrchardCore.AI feature is enabled.
- Fluid / Liquid Template Registration: Notice the
services.Configure<TemplateOptions>(o => ... )block. This registers AI-related models (likeAIProfile,AIChatSession,AIChatSessionPrompt) so they can be securely accessed inside Orchard Core's Liquid templates. This is critical for dynamic prompt engineering within the CMS. - Data Providers & Migrations: It registers index providers (
AIProfileIndexProvider) and migrations (AIProfileDefaultContextMigrations) using YesSql, Orchard Core's document database abstraction. - Display Drivers (The UI Layer): The code makes heavy use of
.AddDisplayDriver<TModel, TDriver>(). Instead of tightly coupling views to models, this registers handlers that build the UI shapes for the admin dashboard (e.g., configuring anAIProfileorAIDeployment). - Tool Registries: The
IAIToolsServiceis registered here, setting up the infrastructure required for the LLM to discover and execute C# backend functions.
2. Cross-Module Integrations ([RequireFeatures])
Orchard Core allows modules to interact without hard dependencies. The next few classes use the [RequireFeatures("...")] attribute to register services only if the host application has those specific Orchard Core modules enabled.
RecipesStartup(OrchardCore.Recipes.Core): Registers steps so that AI profiles and deployments can be created automatically using JSON recipe files during a tenant setup.WorkflowsStartup(OrchardCore.Workflows): This maps backend AI execution to Orchard Core's visual workflow engine. It registers Activities (AICompletionFromProfileTask) and Events (AIChatSessionClosedEvent). This allows an administrator to trigger a workflow visually when an AI chat session ends or when specific data is extracted.OCDeploymentsStartup(OrchardCore.Deployment): Handles exporting and importing AI configurations across different environments (e.g., migrating from staging to production).
3. Feature Toggles ([Feature("...")])
The rest of the file breaks down sub-features within the AI module itself. These boundaries ensure that background tasks, API endpoints, and memory usage are only allocated if the specific feature is turned on.
ChatCoreStartup(AIConstants.Feature.ChatCore): Sets up the stateful nature of AI conversations. It registers the session manager (IAIChatSessionManager), background tasks for closing orphaned sessions (AIChatSessionCloseBackgroundTask), and handlers for orchestrating the flow of messages between the user, the LLM, and external data tools.ApiChatStartup(AIConstants.Feature.ChatApi): If the CMS needs to act as a headless AI backend, this class usesIEndpointRouteBuilderto expose standard REST APIs (.AddApiAIChatSessionEndpoint(), etc.) for frontend applications.ConnectionManagementStartup: Isolates the secure storage and management of API keys and endpoint URLs for various AI providers (OpenAI, Ollama, DeepSeek).
How to Navigate This as a Backend Developer
When debugging or extending this module, treat Startup.cs as your roadmap:
- If you are building a new AI Tool: You will want to look at how
IAIToolsServiceis registered in the mainStartupclass and trace its interface. - If you are adding custom AI events: You will add your logic to
WorkflowsStartupto expose it to the Orchard Core workflow UI. - If you are modifying how prompts are stored: Look at the YesSql indexes and migrations defined inside
ChatCoreStartup.
This file strictly adheres to the principle of inversion of control. By heavily relying on scoped interface registrations and isolated StartupBase classes, it ensures that the core AI execution engine remains completely decoupled from the UI, HTTP routing, and workflow orchestration.