Orchardcore ai response handlers

From CrestApps/CrestApps.AgentSkills · View interactive version

Orchardcore ai response handlers is an AI agent skill from the GitHub repository CrestApps/CrestApps.AgentSkills. Skill for implementing CrestApps chat response handlers in Orchard Core with CrestApps.OrchardCore 3.0. Covers IChatResponseHandler and IChatResponseHandlerResolver, ChatResponseHandlerContext and AssistantAppearance, streaming and deferred results, the AI handler fallback in Conversation chat mode,

How to use this skill

Skill file

Orchard Core Chat Response Handlers

Route a chat prompt to a handler

IChatResponseHandler is a shared CrestApps.Core contract used by Orchard chat

sessions and chat interactions. A handler either returns response updates now or

defers the assistant response until an external system supplies it.

using CrestApps.Core.AI.ResponseHandling;

namespace MyCompany.OrchardCore.Chat;

public sealed class LiveAgentResponseHandler : IChatResponseHandler
{
    public string Name => "LiveAgent";

    public async Task<ChatResponseHandlerResult> HandleAsync(
        ChatResponseHandlerContext context,
        CancellationToken cancellationToken = default)
    {
        await SendToAgentSystemAsync(context.Prompt, cancellationToken);

        return ChatResponseHandlerResult.Deferred();
    }

    private static Task SendToAgentSystemAsync(
        string prompt,
        CancellationToken cancellationToken)
    {
        return Task.CompletedTask;
    }
}

Register a custom handler as an enumerable scoped service:

using CrestApps.Core.AI.ResponseHandling;
using Microsoft.Extensions.DependencyInjection;

services.TryAddEnumerable(
    ServiceDescriptor.Scoped<IChatResponseHandler, LiveAgentResponseHandler>());

The active handler is selected by IChatResponseHandlerResolver.Resolve(handlerName, chatMode) from

AIChatSession.ResponseHandlerName or ChatInteraction.ResponseHandlerName. An empty or unknown name

falls back to the built-in AI handler (ChatResponseHandlerNames.AI, "AI"), and so does every request in

ChatMode.Conversation: spoken conversations always run through the AI handler, and a custom name is

logged and ignored.

ChatResponseHandlerContext gives the handler Prompt, ConnectionId, SessionId, ChatType,

ConversationHistory, Services, and the Profile, ChatSession or Interaction it serves. A handler can

set AssistantAppearance (Label, Icon, CssClass, DisableStreamingAnimation) to change how its

replies are labelled in the chat UI, for example to show a live agent's name.

Start new sessions on a handler

A profile can route new chat sessions to a handler from the first message. The setting is stored on

profile.Settings as ResponseHandlerProfileSettings.InitialResponseHandlerName and edited as

Initial response handler on the profile's General card (registered by the AI Chat Services

feature, CrestApps.OrchardCore.AI.Chat.Core, and useful only once a non-AI handler is registered).

{
  "steps": [
    {
      "name": "AIProfile",
      "Profiles": [
        {
          "Name": "live-support",
          "DisplayText": "Live Support",
          "Type": "Chat",
          "Settings": {
            "ResponseHandlerProfileSettings": { "InitialResponseHandlerName": "LiveAgent" }
          }
        }
      ]
    }
  ]
}

Profile templates carry the same value as ProfileTemplateMetadata.InitialResponseHandlerName. A tool or

an external event can switch an active session later by updating the session's or interaction's

ResponseHandlerName.

Return streaming or deferred output

Use Deferred() only when the handler has handed work to an external process

that will deliver the reply later. The hub persists the user prompt and does

not wait for an assistant response.

For immediate output, return an async sequence of ChatResponseUpdate values:

using CrestApps.Core.AI.ResponseHandling;
using Microsoft.Extensions.AI;

return ChatResponseHandlerResult.Streaming(StreamUpdatesAsync(cancellationToken));

static async IAsyncEnumerable<ChatResponseUpdate> StreamUpdatesAsync(
    [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken)
{
    yield return new ChatResponseUpdate(ChatRole.Assistant, "Connecting you to an agent.");
    await Task.CompletedTask;
}

The factory is ChatResponseHandlerResult.Streaming(...), not Stream(...).

Use a deferred handler only with an authenticated, idempotent callback or relay

strategy; a successful handoff does not make an arbitrary anonymous webhook

safe.

Send transient chat notifications

IChatNotificationSender sends UI-only notifications to the appropriate

SignalR clients. A notification is identified by its required Type; there is

no ChatNotification.Id property.

using CrestApps.Core.AI.Chat;
using CrestApps.Core.AI.Models;

await notifications.SendAsync(
    sessionId,
    ChatContextType.AIChatSession,
    new ChatNotification(ChatNotificationTypes.Transfer)
    {
        Content = "Transferring you to a live agent.",
        Icon = "fa-solid fa-headset",
        Dismissible = true,
    });

await notifications.RemoveAsync(
    sessionId,
    ChatContextType.AIChatSession,
    ChatNotificationTypes.Transfer);

SendAsync replaces an active notification with the same type.

UpdateAsync updates only an existing matching type, and RemoveAsync accepts

the notification type string. Use ChatNotificationTypes and

ChatNotificationActionNames for built-in values. Register a custom action

handler as a keyed IChatNotificationActionHandler.

Use the external relay contracts exactly

For persistent WebSocket, SSE, gRPC, or queue-based handoff, implement

IExternalChatRelay. It is protocol agnostic, but its signatures are fixed:

using CrestApps.Core.AI.Chat;
using CrestApps.Core.AI.Models;

namespace MyCompany.OrchardCore.Chat;

public sealed class LiveAgentRelay : IExternalChatRelay
{
    public Task<bool> IsConnectedAsync(CancellationToken cancellationToken = default)
        => Task.FromResult(true);

    public Task ConnectAsync(
        ExternalChatRelayContext context,
        CancellationToken cancellationToken = default)
        => Task.CompletedTask;

    public Task SendPromptAsync(string text, CancellationToken cancellationToken = default)
        => Task.CompletedTask;

    public Task SendSignalAsync(
        string signalName,
        IDictionary<string, string> data = null,
        CancellationToken cancellationToken = default)
        => Task.CompletedTask;

    public Task DisconnectAsync(CancellationToken cancellationToken = default)
        => Task.CompletedTask;

    public ValueTask DisposeAsync() => ValueTask.CompletedTask;
}

The singleton IExternalChatRelayManager owns connection lifetime. Create or

reuse a relay with:

var relay = await relayManager.GetOrCreateAsync(
    context.SessionId,
    new ExternalChatRelayContext
    {
        SessionId = context.SessionId,
        ChatType = context.ChatType,
    },
    () => new LiveAgentRelay(),
    cancellationToken);

await relay.SendPromptAsync(context.Prompt, cancellationToken);

The relay event callback is also identifier-based, not context-based:

await relayEventHandler.HandleEventAsync(
    sessionId,
    ChatContextType.AIChatSession,
    relayEvent,
    cancellationToken);

Register keyed IExternalChatRelayNotificationBuilder services for custom

event types. The default event handler selects the builder by

ExternalChatRelayEvent.EventType.

Transfer through an AI tool

When an AI tool changes the response-handler selection, implement a real

AITool and register it with the current Core API:

services.AddCoreAITool<TransferToLiveAgentTool>("transfer_to_live_agent");

Do not use the removed AddAITool registration name. Keep the tool's

authorization, input validation, and response-handler mutation in the same

tenant scope as the session or interaction being changed.

Operational checklist

interaction target before writing prompts or emitting notifications.

aware.

ends; it disconnects and disposes the registered relay.

Related skills

and authorization.

capability configuration.

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.