Crestapps core ai runtime

From CrestApps/CrestApps.AgentSkills · View interactive version

Crestapps core ai runtime is an AI agent skill from the GitHub repository CrestApps/CrestApps.AgentSkills. Skill for the provider-agnostic AI runtime in CrestApps.Core 2.0. Covers AddCoreAIServices and AddAISuite registration, provider connections and configured deployments under CrestApps AI Connections and Deployments, declaring deployment Features instead of the removed Purpose or Type, resolving depl

How to use this skill

Skill file

CrestApps.Core AI Runtime

The runtime is the provider-agnostic layer every other AI feature builds on. A connection holds credentials and an endpoint, a deployment names a model on a connection and declares what that model can do, and a slot says what the installation uses a deployment for.

Guidelines

Registration

using CrestApps.Core.AI;
using CrestApps.Core.AI.OpenAI;

builder.Services.AddCrestAppsCore(crestApps => crestApps
    .AddAISuite(ai => ai
        .AddOpenAI()
    )
);

AddAISuite(...) calls AddCoreServices(), AddCoreAIServices() and AddCoreAIOrchestration(). Without the builder:

builder.Services
    .AddCoreAIServices()
    .AddCoreAIOrchestration()
    .AddCoreAIOpenAI();

AddCoreAIServices() also chains AddCoreAITemplating() and AddCoreAIDeploymentCapabilities() (features, parameters and slots). IAIDeploymentManager is registered by AddCoreAIOrchestration(), so register orchestration whenever code resolves deployments.

Connections and Deployments

{
  "CrestApps": {
    "AI": {
      "Connections": [
        {
          "Name": "openai",
          "ClientName": "OpenAI",
          "ApiKey": "sk-..."
        }
      ],
      "Deployments": [
        {
          "ClientName": "OpenAI",
          "Name": "gpt-5-chat",
          "ModelName": "gpt-5",
          "ConnectionName": "openai",
          "Features": [ "textGeneration", "toolCalling", "streaming", "reasoning" ],
          "Parameters": {
            "reasoningEffort": { "AllowedValues": [ "Low", "Medium", "High" ], "DefaultValue": "Medium" }
          }
        },
        {
          "ClientName": "OpenAI",
          "Name": "embeddings",
          "ModelName": "text-embedding-3-large",
          "ConnectionName": "openai",
          "Features": [ "textEmbedding" ]
        }
      ]
    }
  }
}

Resolving a Deployment

| Slot (AIDeploymentSlotNames) | Required feature | Site default (DefaultAIDeploymentSettings) |

|---|---|---|

| Chat | textGeneration (excludes realtime) | DefaultChatDeploymentName |

| Utility | textGeneration (excludes realtime), falls back to Chat | DefaultUtilityDeploymentName |

| Embedding | textEmbedding | DefaultEmbeddingDeploymentName |

| Image | imageOutput | DefaultImageDeploymentName |

| Vision | imageInput | DefaultVisionDeploymentName |

| SpeechToText | speechToText | DefaultSpeechToTextDeploymentName |

| TextToSpeech | textToSpeech | DefaultTextToSpeechDeploymentName |

| Realtime | realtime | DefaultRealtimeDeploymentName |

Resolution walks one chain: the explicit name, the slot's site default, the fallback slot's explicit name and default, then the first capable deployment.

using CrestApps.Core.AI.Deployments;
using CrestApps.Core.AI.Models;

// Named deployment if it qualifies, otherwise the site default, otherwise the first capable one.
var chat = await deploymentManager.ResolveSlotAsync(AIDeploymentSlotNames.Chat, deploymentName: "gpt-5-chat");

// Background work: explicit utility, site utility, explicit chat, site chat, then first text-capable.
var utility = await deploymentManager.ResolveUtilityOrDefaultAsync(
    utilityDeploymentName: profile.UtilityDeploymentName,
    chatDeploymentName: profile.ChatDeploymentName);

// Throws InvalidOperationException when nothing can fill the slot.
var embedding = await deploymentManager.ResolveSlotOrThrowAsync(AIDeploymentSlotNames.Embedding);

// Everything eligible for a picker.
var candidates = await deploymentManager.GetAllBySlotAsync(AIDeploymentSlotNames.Chat);

DefaultAIDeploymentSettings is a plain options class. The framework does not bind it from configuration; configure it from your own section or site settings:

builder.Services.Configure<DefaultAIDeploymentSettings>(settings =>
{
    settings.DefaultChatDeploymentName = "gpt-5-chat";
    settings.DefaultUtilityDeploymentName = "gpt-5-mini";
    settings.DefaultEmbeddingDeploymentName = "embeddings";
});

Key Runtime Services

| Service | Use |

|---|---|

| IAIClientFactory | Create IChatClient, IEmbeddingGenerator, IImageGenerator, ISpeechToTextClient, ITextToSpeechClient and IRealtimeClient for a deployment. Every overload except realtime accepts an Action<...Builder> pipeline callback, and the factory owns the final Build(serviceProvider) |

| IAICompletionService | CompleteAsync / CompleteStreamingAsync(deployment, messages, context, cancellationToken) without the orchestration loop |

| IAICompletionContextBuilder | BuildAsync(resource, configure, cancellationToken) runs the context handlers for a profile, interaction or other resource |

| IAIDeploymentManager | Slot resolution and deployment CRUD |

| IAIDeploymentCapabilityService | What a deployment's model declares |

| IAIProviderConnectionStore / IAIDeploymentStore | Merged configuration and store views |

Every client the factory creates is metered, and every chat client is wrapped in CapabilityEnforcingChatClient, which strips tools, JSON response formats and reasoning options the deployment does not declare. Chat clients stream tool calls and results (FunctionCallContent, FunctionResultContent) through FunctionInvokingChatClient, which the AG-UI chat shows as tool cards; NamedAICompletionClient passes stored tool result messages to the provider and drops results whose calls a past-messages limit cut off. CreateRealtimeClientAsync returns a metering wrapper; use GetService to reach the provider client.

Telemetry Options

The framework registers a scoped DefaultAIOptions that applies the site's GeneralAIOptions overrides. Two of its options control the Microsoft.Extensions.AI gen_ai.* telemetry; the framework's own CrestApps.Core spans and metrics do not depend on them.

A site value applies only with its flag: GeneralAIOptions.OverrideEnableOpenTelemetry or OverrideEnableSensitiveData. See crestapps-core-observability.

Example Completion Service

using CrestApps.Core.AI.Completions;
using CrestApps.Core.AI.Deployments;
using CrestApps.Core.AI.Models;
using Microsoft.Extensions.AI;

public sealed class QuestionService
{
    private readonly IAICompletionService _completionService;
    private readonly IAIDeploymentManager _deploymentManager;

    public QuestionService(
        IAICompletionService completionService,
        IAIDeploymentManager deploymentManager)
    {
        _completionService = completionService;
        _deploymentManager = deploymentManager;
    }

    public async Task<string> AskAsync(string question, CancellationToken cancellationToken = default)
    {
        var deployment = await _deploymentManager.ResolveSlotAsync(
            AIDeploymentSlotNames.Chat,
            cancellationToken: cancellationToken)
            ?? throw new InvalidOperationException("No chat deployment is configured.");

        var context = new AICompletionContext
        {
            ChatDeploymentName = deployment.Name,
            SystemMessage = "You are a helpful assistant.",
            DisableTools = true,
        };

        var response = await _completionService.CompleteAsync(
            deployment,
            [new ChatMessage(ChatRole.User, question)],
            context,
            cancellationToken);

        return response.Text;
    }
}

To reuse a profile's prompts, tools, persona and model parameters, build the context from the profile instead: await completionContextBuilder.BuildAsync(profile, cancellationToken: cancellationToken).

Direct Clients

var chatClient = await clientFactory.CreateChatClientAsync(deployment, builder => builder
    .UseDefaultResilience()
    .UseUsageLabels(contextType: "Leads", purpose: "LeadScoring"));

var embeddingDeployment = await deploymentManager.ResolveSlotOrThrowAsync(AIDeploymentSlotNames.Embedding);
var generator = await clientFactory.CreateEmbeddingGeneratorAsync(embeddingDeployment);

When you finish a builder yourself, call Build(serviceProvider), never Build().

Custom Providers

A provider that creates Microsoft.Extensions.AI clients implements IAIClientProvider (CanHandle(clientName) plus GetChatClientAsync, GetEmbeddingGeneratorAsync, GetRealtimeClientAsync and the other client getters for a connection), so IAIClientFactory, capability enforcement and metering apply to it with no extra code. A provider that owns the whole completion loop implements IAICompletionClient (ClientName, CompleteAsync, CompleteStreamingAsync) and registers it with AddCoreAICompletionClient<TClient>(clientName, configure). Deriving from AICompletionServiceBase gives ResolveDeploymentAsync(slotName, providerName, deploymentName) and ResolveRequestDeploymentAsync(context, providerName), which honors AICompletionContext.IsUtilityCompletion.

Troubleshooting

Upgrading Within 2.0 Previews

Upgrading from 1.x

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.