Skip to content

Commit dfa7e3a

Browse files
authored
Fix IChatResponseHandlerResolver dependeny issue (#396)
1 parent a7f7ee3 commit dfa7e3a

File tree

9 files changed

+12
-142
lines changed

9 files changed

+12
-142
lines changed

src/CrestApps.OrchardCore.Documentations/docs/ai/response-handlers.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,9 +1036,11 @@ profile.AlterSettings<ResponseHandlerProfileSettings>(settings =>
10361036
});
10371037
```
10381038

1039-
### Via Chat UI
1039+
For custom clients that need to override the profile default at session start, call the chat hub directly and pass the handler name as the second argument to `AIChatHub.StartSession(profileId, initialResponseHandlerName)`.
10401040

1041-
When multiple response handlers are registered, the chat UI displays a **Response Handler** dropdown in the placeholder area before a session starts. Users can select which handler to use for their session, overriding the profile's default.
1041+
### Built-in chat UI behavior
1042+
1043+
The built-in CrestApps chat UI never displays a response-handler picker to end users. Sessions always start with the AI profile's configured initial response handler, or the default AI handler when the profile does not specify one.
10421044

10431045
## SignalR Groups for Deferred Responses
10441046

src/Modules/CrestApps.OrchardCore.AI.Chat/Assets/js/ai-chat.js

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,6 @@ window.openAIChatManager = function () {
383383
conversationModeEnabled: config.chatMode === 'Conversation',
384384
conversationButton: null,
385385
isConversationMode: false,
386-
selectedResponseHandler: '',
387-
responseHandlers: config.responseHandlers || [],
388386
};
389387
},
390388
computed: {
@@ -527,50 +525,6 @@ window.openAIChatManager = function () {
527525
if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + ' KB';
528526
return (bytes / (1024 * 1024)).toFixed(1) + ' MB';
529527
},
530-
renderHandlerSelector() {
531-
if (!this.placeholder || this.responseHandlers.length === 0) {
532-
return;
533-
}
534-
535-
var existing = this.placeholder.querySelector('.ai-chat-handler-selector');
536-
if (existing) {
537-
return;
538-
}
539-
540-
var container = document.createElement('div');
541-
container.className = 'ai-chat-handler-selector mt-2';
542-
container.style.cssText = 'font-size: 0.85rem;';
543-
544-
var label = document.createElement('label');
545-
label.className = 'form-label mb-1';
546-
label.style.fontSize = '0.8rem';
547-
label.textContent = 'Response Handler';
548-
549-
var select = document.createElement('select');
550-
select.className = 'form-select form-select-sm';
551-
552-
var defaultOption = document.createElement('option');
553-
defaultOption.value = '';
554-
defaultOption.textContent = 'Default (AI)';
555-
select.appendChild(defaultOption);
556-
557-
for (var i = 0; i < this.responseHandlers.length; i++) {
558-
var handler = this.responseHandlers[i];
559-
var option = document.createElement('option');
560-
option.value = handler.name;
561-
option.textContent = handler.name;
562-
select.appendChild(option);
563-
}
564-
565-
var self = this;
566-
select.addEventListener('change', function () {
567-
self.selectedResponseHandler = this.value;
568-
});
569-
570-
container.appendChild(label);
571-
container.appendChild(select);
572-
this.placeholder.appendChild(container);
573-
},
574528
renderDocumentBar() {
575529
if (!this.documentBar) return;
576530

@@ -1798,21 +1752,14 @@ window.openAIChatManager = function () {
17981752
return;
17991753
}
18001754

1801-
const handlerName = this.selectedResponseHandler || null;
1802-
1803-
this.connection.invoke("StartSession", profileId, handlerName).catch(err => console.error(err));
1755+
this.connection.invoke("StartSession", profileId, null).catch(err => console.error(err));
18041756
},
18051757
initializeApp() {
18061758
this.inputElement = document.querySelector(config.inputElementSelector);
18071759
this.buttonElement = document.querySelector(config.sendButtonElementSelector);
18081760
this.chatContainer = document.querySelector(config.chatContainerElementSelector);
18091761
this.placeholder = document.querySelector(config.placeholderElementSelector);
18101762

1811-
// Render the handler selector in the placeholder if multiple handlers are available.
1812-
if (this.placeholder && this.responseHandlers.length > 0) {
1813-
this.renderHandlerSelector();
1814-
}
1815-
18161763
const sessionId = this.getSessionId();
18171764
if (!config.widget && sessionId) {
18181765
this.loadSession(sessionId);

src/Modules/CrestApps.OrchardCore.AI.Chat/Views/AIChatAdminWidget.cshtml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -205,18 +205,6 @@
205205
["conversationButtonElementSelector"] = $"#{conversationBtnHtmlId}",
206206
};
207207

208-
var handlerResolver = ViewContext.HttpContext.RequestServices.GetService<IChatResponseHandlerResolver>();
209-
if (handlerResolver != null)
210-
{
211-
var handlers = handlerResolver.GetAll().ToList();
212-
if (handlers.Count > 1)
213-
{
214-
chatConfig["responseHandlers"] = handlers
215-
.Select(h => new { name = h.Name })
216-
.ToList();
217-
}
218-
}
219-
220208
if (sessionDocumentsEnabled)
221209
{
222210
chatConfig["sessionDocumentsEnabled"] = true;

src/Modules/CrestApps.OrchardCore.AI.Chat/Views/AIChatSessionChat.cshtml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -187,18 +187,6 @@
187187
["conversationButtonElementSelector"] = $"#{conversationBtnHtmlId}",
188188
};
189189

190-
var handlerResolver = ViewContext.HttpContext.RequestServices.GetService<IChatResponseHandlerResolver>();
191-
if (handlerResolver != null)
192-
{
193-
var handlers = handlerResolver.GetAll().ToList();
194-
if (handlers.Count > 1)
195-
{
196-
chatConfig["responseHandlers"] = handlers
197-
.Select(h => new { name = h.Name })
198-
.ToList();
199-
}
200-
}
201-
202190
if (sessionDocumentsEnabled)
203191
{
204192
chatConfig["sessionDocumentsEnabled"] = true;

src/Modules/CrestApps.OrchardCore.AI.Chat/Views/Widget-AIChat.cshtml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -241,18 +241,6 @@
241241
["conversationButtonElementSelector"] = $"#{conversationBtnHtmlId}",
242242
};
243243

244-
var handlerResolver = ViewContext.HttpContext.RequestServices.GetService<IChatResponseHandlerResolver>();
245-
if (handlerResolver != null)
246-
{
247-
var handlers = handlerResolver.GetAll().ToList();
248-
if (handlers.Count > 1)
249-
{
250-
chatConfig["responseHandlers"] = handlers
251-
.Select(h => new { name = h.Name })
252-
.ToList();
253-
}
254-
}
255-
256244
if (sessionDocumentsEnabled)
257245
{
258246
chatConfig["sessionDocumentsEnabled"] = true;

src/Modules/CrestApps.OrchardCore.AI.Chat/wwwroot/scripts/ai-chat.js

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,7 @@ window.openAIChatManager = function () {
327327
ttsButton: null,
328328
conversationModeEnabled: config.chatMode === 'Conversation',
329329
conversationButton: null,
330-
isConversationMode: false,
331-
selectedResponseHandler: '',
332-
responseHandlers: config.responseHandlers || []
330+
isConversationMode: false
333331
};
334332
},
335333
computed: {
@@ -539,42 +537,6 @@ window.openAIChatManager = function () {
539537
if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + ' KB';
540538
return (bytes / (1024 * 1024)).toFixed(1) + ' MB';
541539
},
542-
renderHandlerSelector: function renderHandlerSelector() {
543-
if (!this.placeholder || this.responseHandlers.length === 0) {
544-
return;
545-
}
546-
var existing = this.placeholder.querySelector('.ai-chat-handler-selector');
547-
if (existing) {
548-
return;
549-
}
550-
var container = document.createElement('div');
551-
container.className = 'ai-chat-handler-selector mt-2';
552-
container.style.cssText = 'font-size: 0.85rem;';
553-
var label = document.createElement('label');
554-
label.className = 'form-label mb-1';
555-
label.style.fontSize = '0.8rem';
556-
label.textContent = 'Response Handler';
557-
var select = document.createElement('select');
558-
select.className = 'form-select form-select-sm';
559-
var defaultOption = document.createElement('option');
560-
defaultOption.value = '';
561-
defaultOption.textContent = 'Default (AI)';
562-
select.appendChild(defaultOption);
563-
for (var i = 0; i < this.responseHandlers.length; i++) {
564-
var handler = this.responseHandlers[i];
565-
var option = document.createElement('option');
566-
option.value = handler.name;
567-
option.textContent = handler.name;
568-
select.appendChild(option);
569-
}
570-
var self = this;
571-
select.addEventListener('change', function () {
572-
self.selectedResponseHandler = this.value;
573-
});
574-
container.appendChild(label);
575-
container.appendChild(select);
576-
this.placeholder.appendChild(container);
577-
},
578540
renderDocumentBar: function renderDocumentBar() {
579541
if (!this.documentBar) return;
580542
if (!config.sessionDocumentsEnabled) {
@@ -1849,8 +1811,7 @@ window.openAIChatManager = function () {
18491811
if (!profileId || !this.connection) {
18501812
return;
18511813
}
1852-
var handlerName = this.selectedResponseHandler || null;
1853-
this.connection.invoke("StartSession", profileId, handlerName)["catch"](function (err) {
1814+
this.connection.invoke("StartSession", profileId, null)["catch"](function (err) {
18541815
return console.error(err);
18551816
});
18561817
},
@@ -1860,11 +1821,6 @@ window.openAIChatManager = function () {
18601821
this.buttonElement = document.querySelector(config.sendButtonElementSelector);
18611822
this.chatContainer = document.querySelector(config.chatContainerElementSelector);
18621823
this.placeholder = document.querySelector(config.placeholderElementSelector);
1863-
1864-
// Render the handler selector in the placeholder if multiple handlers are available.
1865-
if (this.placeholder && this.responseHandlers.length > 0) {
1866-
this.renderHandlerSelector();
1867-
}
18681824
var sessionId = this.getSessionId();
18691825
if (!config.widget && sessionId) {
18701826
this.loadSession(sessionId);

src/Modules/CrestApps.OrchardCore.AI.Chat/wwwroot/scripts/ai-chat.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Modules/CrestApps.OrchardCore.AI/Drivers/AIProfileResponseHandlerDisplayDriver.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ public AIProfileResponseHandlerDisplayDriver(
2727

2828
public override IDisplayResult Edit(AIProfile profile, BuildEditorContext context)
2929
{
30-
var handlers = _handlerResolver.GetAll().ToList();
30+
var handlers = _handlerResolver.GetAll();
3131

3232
// Only show the handler selector when there is at least one non-AI handler registered.
33-
if (handlers.Count <= 1)
33+
if (!handlers.Any())
3434
{
3535
return null;
3636
}

src/Modules/CrestApps.OrchardCore.AI/Startup.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ public override void ConfigureServices(IServiceCollection services)
7272
.AddScoped<CitationReferenceCollector>()
7373
.AddScoped<IAICompletionContextBuilderHandler, AIProfileCompletionContextBuilderHandler>()
7474
.AddDisplayDriver<AIProfile, AIProfileDisplayDriver>()
75-
.AddDisplayDriver<AIProfile, AIProfileResponseHandlerDisplayDriver>()
7675
.AddTransient<IConfigureOptions<DefaultAIOptions>, DefaultAIOptionsConfiguration>()
7776
.AddScoped(sp =>
7877
{
@@ -243,6 +242,8 @@ public override void ConfigureServices(IServiceCollection services)
243242
.AddIndexProvider<AIChatSessionIndexProvider>()
244243
.AddSingleton<IBackgroundTask, AIChatSessionCloseBackgroundTask>();
245244

245+
services.AddDisplayDriver<AIProfile, AIProfileResponseHandlerDisplayDriver>();
246+
246247
// Register the AI chat session prompt store.
247248
services.AddScoped<DefaultAIChatSessionPromptStore>()
248249
.AddScoped<IAIChatSessionPromptStore>(sp => sp.GetRequiredService<DefaultAIChatSessionPromptStore>())

0 commit comments

Comments
 (0)