CrestApps.Core Chat Interactions
A Chat Interaction is an operator-controlled conversation that needs no AI Profile: the interaction itself stores its deployments, system message, sampling, tools, agents, MCP connections, data source and documents. It is the fastest way to validate a provider and deployment. AI Chat sessions (AIChatSession) are the profile-based equivalent used by end-user chat pages and widgets. Both ship in CrestApps.Core.AI.Chat and share the same chat UI scripts.
Guidelines
- Start with Chat Interactions to validate a connection and deployment, then move reusable behavior into an AI Profile (
crestapps-core-ai-profiles). - Register stores for
ChatInteractionandIChatInteractionPromptStoreon the chat interactions builder. - The framework does not ship a mapped hub. Subclass
ChatInteractionHubBase(andAIChatHubCore<TClient>for sessions), map it, and callConfigureChatHubOptions<THub>()for each concrete hub. - The hubs' authorization hooks return
trueby default, exceptAuthorizeSessionAsynconAIChatHubCore<TClient>, which only lets a session's owner in. Override them or put[Authorize]on the hub. - Chat Interactions do not get the AI Profile prompt-security preamble or injection blocking (the operator controls the prompt), but they do get input-boundary delimiters and a per-user tool access check at send time.
- Use
crestapps-core-chatfor server-side completion code,crestapps-core-signalrfor hub routes and authentication,crestapps-core-realtime-voicefor voice sessions, andcrestapps-core-ag-uifor the AG-UI runs (RunAgent, tool cards, approvals, shared state) the session chat uses by default. - Cookie-authenticated hosts put the antiforgery token on the chat page, or document uploads and removals answer
400.
Registration
using CrestApps.Core.AI.Chat;
using CrestApps.Core.AI.OpenAI;
using CrestApps.Core.AI.Security;
using CrestApps.Core.Data.EntityCore;
using CrestApps.Core.SignalR;
using MyApp.Hubs;
builder.Services.AddCrestAppsCore(crestApps => crestApps
.AddAISuite(ai => ai
.AddEntityCoreStores()
.AddOpenAI()
.AddSignalR(addStoreCommitterFilter: true)
.AddChatInteractions(chatInteractions => chatInteractions
.AddEntityCoreStores()
.ConfigureChatHubOptions<ChatInteractionHub>()
.ConfigureVisitorIdentity(options =>
{
options.RemoteAddressMode = AIVisitorRemoteAddressMode.Hashed;
})
.ConfigureChatRateLimiting(options =>
{
options.AnonymousSessionStartPartitions =
ChatRateLimitPartition.Visitor |
ChatRateLimitPartition.NetworkAddress;
})
)
)
.AddEntityCoreSqliteDataStore("Data Source=app.db")
);
// HubOptions<THub> is per concrete hub type; configure every chat hub you map.
builder.Services.ConfigureCrestAppsChatHubOptions<AIChatHub>();
var app = builder.Build();
app.MapHub<ChatInteractionHub>("/hubs/chat-interaction");
app.MapHub<AIChatHub>("/hubs/ai-chat");
Use .AddYesSqlStores() instead of .AddEntityCoreStores() for YesSql; YesSql hosts create the index tables with CreateChatInteractionIndexSchemaAsync and CreateChatInteractionPromptIndexSchemaAsync (see crestapps-core-yessql-stores).
AddCoreAIChatInteractions() (called by AddChatInteractions) registers visitor identity, chat notifications, the session-processing pipeline (AddCoreAIChatSessionProcessing: data extraction, post-session processing, the AIChatSessionCloseBackgroundService that closes inactive sessions every 5 minutes), CitationReferenceCollector, CompositeAIReferenceLinkResolver, ChatInteractionCompletionContextBuilderHandler, the built-in settings handlers (data source, prompt template, persona), and OptionsRealtimeIceServerProvider.
ConfigureChatHubOptions<THub>() uses AddHubOptions<THub> (a plain services.Configure<HubOptions<THub>> is silently ignored by SignalR) and sets a 10-minute client timeout, 15-second keep-alive, 10 MB maximum message size, 8 parallel invocations per client, and a stream buffer of 64.
Host the Hub
ChatInteractionHub and AIChatHub above are host classes. The interaction hub looks like this; the session hub is in chat-interactions-examples.md.
using CrestApps.Core.AI.Chat.Hubs;
using CrestApps.Core.AI.Models;
using Microsoft.AspNetCore.Authorization;
namespace MyApp.Hubs;
[Authorize]
public sealed class ChatInteractionHub : ChatInteractionHubBase
{
public ChatInteractionHub(
IServiceProvider services,
TimeProvider timeProvider,
ILogger<ChatInteractionHub> logger)
: base(services, timeProvider, logger)
{
}
protected override Task<bool> AuthorizeAsync(IServiceProvider services, ChatInteraction interaction)
=> Task.FromResult(string.Equals(interaction.OwnerId, Context.UserIdentifier, StringComparison.Ordinal));
protected override Task<ChatMode> GetChatModeAsync(IServiceProvider services)
=> Task.FromResult(ChatMode.Conversation);
}
Other virtual hooks include GetDeploymentSettingsAsync (returns the DefaultAIDeploymentSettings used for slot defaults), IsTextToSpeechPlaybackEnabledAsync, CollectStreamingReferences (merge citations with CitationReferenceCollector), OnAssistantPromptCreatedAsync, ValidateSettings, and the localizable message methods (GetNotAuthorizedMessage, GetSettingsValidationMessage, GetNoRealtimeDeploymentMessage, GetConversationDeploymentNotRealtimeMessage, and others). AIChatHubCore<TClient> exposes AuthorizeProfileAsync(IServiceProvider, AIProfile) and GetRateLimitedMessage(PromptSecurityResult), which returns a "slow down" message with the retry delay when the message rate limit is hit.
EvaluateMessageRateLimitAsync(services, profile, sessionId, cancellationToken)appliesIChatRateLimiterto input that does not pass throughValidatePromptSecurityAsync:SendAudioStream,SynthesizeSpeech,StartConversation,StartRealtimeConversation,StartRealtimeWebRtc, template prompts, blank messages, and resuming or continuing an AG-UI run. It runs after the profile is authorized and before a provider is called.- A
TemplatePromptprofile sent throughSendMessageneeds thesessionProfileIdof the chat profile the caller is in; both profiles are authorized and the message is rate-limited beforeProcessGeneratedPromptAsyncruns. A missing or unknown session profile gets the same "not authorized" answer. SynthesizeSpeechrefuses text overMaxSpeechTextLength(virtual, 10,000) withGetSpeechTextTooLongMessage()on both hubs;AIChatHubCorealso refuses a profile without text-to-speech playback or Conversation mode (IsSpeechSynthesisEnabledAsync, virtual) withGetTtsNotEnabledMessage().ResolveVisitorIdentity(IServiceProvider)isprotected static, so a derived hub builds the same visitor identity.- Over long polling the hub sets the connection's own
HttpContextonIHttpContextAccessorfor the invocation, so visitor identity and rate-limit keys work on every transport.
AIChatHubCore<TClient> also exposes AuthorizeSessionAsync(IServiceProvider, AIChatSession) and ValidateOutputAsync(...) (the output security filter, then the profile's guardrails; returns the replacement text for a blocked reply or null).
Session Ownership
Loading, continuing, rating and acting on a session all call AuthorizeSessionAsync first. By default it allows only the signed-in user whose id (ClaimTypes.NameIdentifier, else the name) equals AIChatSession.UserId, or, for a visitor session, the same ClientId from IAIVisitorIdentityResolver. A session with neither is refused. A refused id is answered with GetSessionNotFoundMessage(), and a message sent to it starts a new session for the caller. Let staff open other users' conversations by widening it:
protected override async Task<bool> AuthorizeSessionAsync(IServiceProvider services, AIChatSession chatSession)
{
if (await base.AuthorizeSessionAsync(services, chatSession))
{
return true;
}
var authorizationService = services.GetRequiredService<IAuthorizationService>();
return (await authorizationService.AuthorizeAsync(Context.User, "Admin")).Succeeded;
}
Stop After Streaming
When the user presses Stop after the answer has streamed, the hubs still validate, save and finish the turn: the remaining work ignores the cancelled token, and SendMessage runs in a child scope the hub owns because SignalR disposes the invocation scope on cancel. A host that overrides ExecuteInScopeAsync with its own scope keeps that scope.
Hub methods called by the client (interaction hub; the session hub takes profileId and sessionId instead of itemId):
| Method | Purpose |
|---|---|
| SendMessage(itemId, prompt) | Streams CompletionPartialMessage items through a ChannelReader |
| RunAgent(input) | Session hub only: an AG-UI run that streams AG-UI events (see crestapps-core-ag-ui) |
| LoadInteraction(itemId) / SaveSettings(itemId, settings) / ClearHistory(itemId) | Load, update and reset an interaction |
| StartConversation, SendAudioStream, SynthesizeSpeech | Speech-to-text and text-to-speech conversation mode |
| StartRealtimeConversation, StartRealtimeWebRtc, AddRealtimeIceCandidate, GetRealtimeIceServers, UpdateRealtimeSettings, UpdateRealtimeSpeechSpeed, StopConversation | Realtime voice (see crestapps-core-realtime-voice) |
| HandleNotificationAction(sessionId, notificationType, actionName) | Chat notification buttons |
| SetPageTools(tools) / ClearPageTools() | Session hub only: the WebMCP tools the tab exposes (see crestapps-core-webmcp) |
IChatInteractionHubClient callbacks: ReceiveError, LoadInteraction, SettingsSaved, HistoryCleared, ReceiveTranscript, ReceiveAudioChunk, ReceiveAudioComplete, ReceiveRealtimeAnswer, ReceiveRealtimeIceCandidate, ReceiveRealtimeEvent, ReceiveConversationUserMessage, ReceiveConversationAssistantToken, ReceiveConversationAssistantComplete, ReceiveNotification, UpdateNotification, RemoveNotification.
The Interaction and Its Settings
SaveSettings reads camelCase keys from the settings JSON: title, orchestratorName, connectionName, deploymentName (stored as ChatDeploymentName), utilityDeploymentName, conversationDeploymentName, realtimeVoiceName, systemMessage, temperature, topP, frequencyPenalty, presencePenalty, maxTokens, pastMessagesCount, toolNames, agentNames, mcpConnectionIds, plus whatever the registered IChatInteractionSettingsHandler implementations read (for example filter, personaId).
ChatDeploymentNameis the text model;UtilityDeploymentNameruns background work;ConversationDeploymentNamenames the realtime model and, because a chat interaction has no chat mode of its own, naming it is the opt-in to speaking. A conversation deployment that is not realtime-capable is reported with the deployment name rather than silently replaced.- The
filtersetting is validated withIODataValidatorbefore any settings handler runs. An invalid filter rejects the save withReceiveError("Invalid filter value. It must be a valid OData filter.") and the interaction keeps its settings. - Add your own settings with a handler; see chat-interactions-examples.md.
Security Behavior
- Tool access at send time. An interaction stores its own tool names, which are client-controllable, so every listable tool is re-checked with
IAIToolAccessEvaluator.IsAuthorizedAsync(user, toolName)and unauthorized tools are dropped with one warning. System and hidden tools are not checked; anullcaller (trusted server code) skips the check. AI Profile sessions do not apply this gate; the profile is their authorization boundary. - Input delimiters. The user's message is wrapped in input-boundary delimiters, controlled by
PromptSecurityOptions.EnableChatInteractionInputDelimiters(defaulttrue). - Anonymous visitors.
AIVisitorIdentityOptions.AllowCrossSiteEmbedding(off by default) writes the visitor cookieSameSite=None; Secure; Partitionedso a chat embedded in another site's frame keeps its visitor. Rate limiting for anonymous traffic uses tiers onPromptSecurityOptions(seecrestapps-core-prompt-security).
Documents
Upload and remove endpoints live in CrestApps.Core.AI.Documents.Endpoints and need document processing (crestapps-core-document-processing):
app.AddUploadChatInteractionDocumentEndpoint();
app.AddRemoveChatInteractionDocumentEndpoint();
app.AddUploadChatSessionDocumentEndpoint();
app.AddRemoveChatSessionDocumentEndpoint();
app.AddDownloadAIDocumentEndpoint();
Each Add*Endpoint() returns the endpoint's RouteHandlerBuilder (add filters or metadata to it), so call them on app one by one; they no longer chain. Authorize them by registering IAuthorizationHandler implementations for AIChatDocumentOperations.ManageDocuments on ChatInteraction and on AIChatSessionDocumentAuthorizationContext.
- Session uploads authorize before creating or saving a session, treat an unknown or foreign session id as no session, count a session-starting upload on
IChatSessionStartRateLimiter(429when throttled), and cap a session atChatDocumentsOptions.MaxDocumentsPerSession(namespaceCrestApps.Core.AI.Ingestion, default20,0disables; extra files come back infailed). Uploads to one session run one at a time per process; across instances use a store that checks concurrency, or sticky routing. - Ownership: remove and download endpoints answer
404for a session the caller does not own. A signed-in caller matches onNameIdentifier(onNameonly when there is none); a visitor on the visitor id. - Interaction removal only removes a document whose reference is that interaction (
chat-interaction); other ids answer404. - Antiforgery: the four upload and remove endpoints run
CookieAuthenticationAntiforgeryEndpointFilter. It validates theRequestVerificationTokenheader or form field unless every identity came from a type inAntiforgeryEndpointFilterOptions.NonCookieAuthenticationTypes(namespaceCrestApps.Core.Filters;Bearer,Api,ApiKey,OpenIddict.Validation.AspNetCoreby default; add the authentication type your API scheme produces, which for the JWT bearer handler isAuthenticationTypes.Federationunless the host sets another). AnAuthorizationheader exempts nothing, and an anonymous request carrying a cookie inAmbientCookieNames(the visitor cookie) is validated too.AddCoreAIDocumentProcessingregistersAddAntiforgery().
@inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Antiforgery
<meta name="RequestVerificationToken" content="@Antiforgery.GetAndStoreTokens(Context).RequestToken" />
The vendored scripts send the token (through window.CoreAIAntiforgery in chat-markers.js) from the chat config's antiforgeryToken, else that meta tag, else a hidden __RequestVerificationToken field, and only to same-origin URLs.
Chat UI Assets
The browser scripts are published as @crestapps/ai-chat-ui (npm or jsDelivr). Load Vue 3, marked, DOMPurify and a pinned SignalR client first, then the CrestApps scripts:
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@crestapps/ai-chat-ui/dist/chat-widget.min.css" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@crestapps/ai-chat-ui/dist/chat-image-carousel.min.css" />
<script src="https://cdn.jsdelivr.net/npm/@microsoft/signalr@10.0.11/dist/browser/signalr.min.js"
integrity="sha384-N+pL/4qXZHZcvsa6hVMd5/7cdF3lC59ZMNHeQyRo5q9jmC7yw0hcJILPjUDSU7ZS"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue@3/dist/vue.global.prod.js"></script>
<script src="https://cdn.jsdelivr.net/npm/dompurify@3/dist/purify.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@crestapps/ai-chat-ui/dist/chat-markers.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@crestapps/ai-chat-ui/dist/chat-image-carousel.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@crestapps/ai-chat-ui/dist/ai-chat.min.js"></script>
chat-markers.js(globalCoreAIChatMarkers) holds the shared[doc:n],[fig:n]and[chart:{...}]rules and must load beforeai-chat.js. It also splits combined citations such as[doc:1, doc:2]and merges references that print the same source.chat-image-carousel.jsis optional. With it, two or more adjacent pictures (and a single picture on its own line) become one swipeable stack with an S / M / L size control. A custom UI callsCoreAIChatImageCarousel.stackAdjacentImages(html)on sanitized HTML.realtime-audio.js(globalCoreAIRealtime) is the shared voice client;chat-interaction.jsandai-chat-widget.jsare the other entry points.- Assistant name and avatar:
data-coreai-chat-assistant-labelanddata-coreai-chat-assistant-avatar-url(Chat Interactions takeassistantLabelandassistantAvatarUrlindata-chat-interaction-config). Resolve them from the linked persona withawait services.ResolveChatAssistantDisplayAsync(profile)(namespaceCrestApps.Core.AI.Profiles; a host without personas gets the default look). - Pin package versions in production (
@crestapps/ai-chat-ui@<version>), and pin SignalR with anintegrityhash. - AG-UI options in the chat config:
protocol('agui'by default,'legacy'for the text-only stream) andsendPageContext(defaulttrue). Seecrestapps-core-ag-ui.
What an Answer May Render
CoreAIImages.sanitize (in chat-markers.js, used by both chat scripts and the MVC chat-interaction view) cleans every answer:
- Images load only from the page's origin,
data:/same-originblob:, or a host in theallowedImageHostschat option ('images.example.com','*.example.com'for subdomains; empty means same-origin only). Other images become plain links.<video>,<audio>,<source>,<track>, SVG, MathML,poster,background,srcsetand every<input>except the disabled task-list checkbox are removed. stylekeeps only size, spacing (no negative margins), text and color properties;id(except chart canvaseschat_chart_N/ci_chart_N),data-coreai-*,data-chat-interaction-*,data-agui-*,for,form,formaction,list,popovertarget,popovertargetaction,commandfor,commandand form-controlnameare removed, and every<button>becomestype="button".- Auto-start skips any chat configuration inside a rendered message or a mounted chat. A custom client calls
window.CoreAIChatSurface.isInsideRenderedMessage(element)before starting a chat andregisterRoot(element)for its mount; a custom settings script reads its controls withCoreAIChatSurface.queryPageElements(selector)/queryPageElement(selector).
window.coreAIChatManager().initialize({
// ...
allowedImageHosts: ['images.example.com', '*.cdn.example.com']
});
Upgrading Within 2.0 Previews
- The session hub only opens a session for its owner. Hubs whose staff review other users' conversations through the hub override
AuthorizeSessionAsync, as the sample hosts do for theAdminpolicy. Sessions with noUserIdand noClientId(for example ones started by another channel) are no longer opened by anyone unless the override allows them. - A Stop click after the answer streamed no longer loses the turn; remove any client workaround that re-sent the message.
- The session chat runs over AG-UI (
RunAgent) by default; setprotocol: 'legacy'to keepSendMessage. - Document
Add*Endpoint()methods returnRouteHandlerBuilder; split chained calls into separate statements. - Uploads and removals need the antiforgery token on cookie-authenticated pages. Hosts with
AllowCrossSiteEmbeddingalso write the antiforgery cookieSameSite=None; Secure; Partitionedand setAntiforgeryOptions.SuppressXFrameOptionsHeader(seecrestapps-core-prompt-security). - Template prompts need a
sessionProfileId; blank messages, speech methods and AG-UI resumes now count against the message rate limit. - Images from other hosts render as links until listed in
allowedImageHosts; inline styles other than size, spacing, text and color are dropped from answers, and answers keep noid. - A session the upload endpoint of an earlier version stamped with the user's
Name(where it differs fromNameIdentifier) no longer reads as that user's; update itsUserId.
Upgrading from 1.x
- The chat scripts are built with Vite for current browsers (Chrome/Edge 111, Firefox 114, Safari 16.4). URLs and globals are unchanged, but ES5 browsers no longer run them, the
*.css.mapfiles are gone, and Babel helpers are no longer page globals. ChatMode.RealtimeandAIProfile.RealtimeDeploymentNameare removed. UseConversationDeploymentNameon the interaction (orChatModeProfileSettings.ConversationDeploymentNameon a profile).- AI Profile sessions no longer apply
IAIToolAccessEvaluator; Chat Interactions now do, at send time. Keep a custom evaluator if you enforce per-user tool permissions. - Load
chat-markers.jsbefore the chat scripts if you host them yourself. - Configure hub options per concrete hub with
ConfigureChatHubOptions<THub>()/ConfigureCrestAppsChatHubOptions<THub>(); a hub mapped without it runs on SignalR defaults.