Skip to content

feat: integrate RAG environment selection in chat window#1227

Open
jeffmaury wants to merge 1 commit intokortex-hub:mainfrom
jeffmaury:GH-515
Open

feat: integrate RAG environment selection in chat window#1227
jeffmaury wants to merge 1 commit intokortex-hub:mainfrom
jeffmaury:GH-515

Conversation

@jeffmaury
Copy link
Copy Markdown
Contributor

To test:

  • Create a RAG environment and make sure it is connected
  • Go to the Chat page
  • In the RAG selector, select the RAG environment
  • Verify the number of selected tools is modified
  • Verify the tools for the Millvus database are selected in the Tools panel
  • Unselect the RAG environment in the RAG selector
  • Verify the number of selected tools is modified
  • Verify the tools for the Millvus database are unselected in the Tools panel

rag-chat

Fixes #515

Fixes kortex-hub#515

Signed-off-by: Jeff MAURY <jmaury@redhat.com>
@jeffmaury jeffmaury requested a review from a team as a code owner April 2, 2026 15:21
@jeffmaury jeffmaury requested review from bmahabirbu and fbricon and removed request for a team April 2, 2026 15:21
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 2, 2026

📝 Walkthrough

Walkthrough

This change introduces a RAG environment selection dropdown in the chat interface. A new selector component displays available RAG knowledge bases with MCP server integration. When users select a RAG environment, associated MCP servers are dynamically added or removed, and tool selections are updated accordingly.

Changes

Cohort / File(s) Summary
RAG Environment Selector Component
packages/renderer/src/lib/chat/components/rag-environment-selector.svelte
New Svelte component implementing a dropdown for selecting RAG knowledge bases. Filters environments by MCP server availability, displays selection state with book and chevron icons, handles selection callbacks, and shows disabled state when no MCP-enabled environments exist.
Chat Header Integration
packages/renderer/src/lib/chat/components/chat-header.svelte
Added onMCPServerAdd and onMCPServerRemove callback props. Integrated RagEnvironmentSelector component with onSelectRagEnvironment handler that removes previously associated MCP server and adds newly associated MCP server when RAG environment selection changes.
Chat Component Integration
packages/renderer/src/lib/chat/components/chat.svelte
Added onMCPServerAdd and onMCPServerRemove handlers that dynamically update selectedMCPTools state. Handlers initialize tool set from added server or delete entry for removed server, then wire into ChatHeader component.

Sequence Diagram

sequenceDiagram
    actor User
    participant RagSelector as RagEnvironmentSelector
    participant ChatHeader as ChatHeader
    participant Chat as Chat
    participant MCP as MCP State

    User->>RagSelector: Select RAG Environment
    RagSelector->>ChatHeader: onSelect(newEnvironment)
    ChatHeader->>ChatHeader: Determine old & new MCP servers
    alt Has Previous MCP Server
        ChatHeader->>Chat: onMCPServerRemove(oldServer)
        Chat->>MCP: Delete tools from selectedMCPTools
    end
    alt Has New MCP Server
        ChatHeader->>Chat: onMCPServerAdd(newServer)
        Chat->>MCP: Initialize tools from newServer.tools
    end
    ChatHeader->>ChatHeader: Store selectedRagEnvironment
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • kortex#1156: Updates RagEnvironment.mcpServer and emits environment update events, providing the backend state that this PR's frontend changes depend on to properly associate RAG environments with MCP servers.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title 'feat: integrate RAG environment selection in chat window' clearly summarizes the main change of adding RAG environment selection functionality to the chat interface.
Description check ✅ Passed The description provides testing steps for the RAG environment selection feature and references issue #515, which is directly related to the changeset.
Linked Issues check ✅ Passed The PR implements all core requirements from issue #515: a dropdown for RAG environment selection in the chat interface, persistence of selection during the session, and integration with the tool selection system.
Out of Scope Changes check ✅ Passed All changes are within scope: the new RAG environment selector component, integration into the chat header and chat components, and wiring of MCP server selection based on RAG environment changes align with issue #515 objectives.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (1)
packages/renderer/src/lib/chat/components/rag-environment-selector.svelte (1)

101-101: Use a more stable key than ragEnv.name for list rendering.

At Line 101, keying only by name can collide when names are duplicated. Prefer a composite key (for example name + provider + connection) to avoid DOM reuse issues.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/renderer/src/lib/chat/components/rag-environment-selector.svelte` at
line 101, The list is currently keyed with ragEnv.name which can collide for
duplicate names; update the each-block key to a stable composite identifier
(e.g., combine ragEnv.name with ragEnv.provider and ragEnv.connection or an
explicit unique id) so that the {`#each` ragEnvironmentsWithMCP as ragEnv
(ragEnv.name)} uses a non-colliding key (for example (ragEnv.name + ':' +
ragEnv.provider + ':' + ragEnv.connection) or ragEnv.id) to prevent DOM reuse
issues when rendering the ragEnvironmentsWithMCP list.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/renderer/src/lib/chat/components/chat.svelte`:
- Around line 86-90: The onMCPServerAdd handler currently only updates
selectedMCPTools when a matching server exists in $mcpRemoteServerInfos, so
selections get dropped if that store is stale; change onMCPServerAdd to always
call selectedMCPTools.set(mcpServer.id, new SvelteSet(...)) using
Object.keys(server.tools) when server is found and falling back to
Object.keys(mcpServer.tools) (the incoming payload) when not found, keeping the
SvelteSet usage and preserving behavior when $mcpRemoteServerInfos is empty.

In `@packages/renderer/src/lib/chat/components/rag-environment-selector.svelte`:
- Around line 34-43: The selected RagEnvironment must be cleared when the
available RAG environments change to avoid stale labels/tools; add a
watcher/subscription to the environments store (the list used to populate the
dropdown) that checks whether the current selected (selected) still exists and
still has an active MCP/tool, and if not set selected = undefined and call
onSelect(undefined) to clear active MCPs; implement this as a reactive statement
or store subscription that compares selected.id or selected.name against the new
environments list and resets selected and the selection handler accordingly so
selectedText and active tools cannot remain stale.

---

Nitpick comments:
In `@packages/renderer/src/lib/chat/components/rag-environment-selector.svelte`:
- Line 101: The list is currently keyed with ragEnv.name which can collide for
duplicate names; update the each-block key to a stable composite identifier
(e.g., combine ragEnv.name with ragEnv.provider and ragEnv.connection or an
explicit unique id) so that the {`#each` ragEnvironmentsWithMCP as ragEnv
(ragEnv.name)} uses a non-colliding key (for example (ragEnv.name + ':' +
ragEnv.provider + ':' + ragEnv.connection) or ragEnv.id) to prevent DOM reuse
issues when rendering the ragEnvironmentsWithMCP list.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 990df5ad-4f92-4695-890f-e0fbb94fdc9d

📥 Commits

Reviewing files that changed from the base of the PR and between 63b4518 and 39f86fe.

📒 Files selected for processing (3)
  • packages/renderer/src/lib/chat/components/chat-header.svelte
  • packages/renderer/src/lib/chat/components/chat.svelte
  • packages/renderer/src/lib/chat/components/rag-environment-selector.svelte

Comment on lines +86 to +90
function onMCPServerAdd(mcpServer: MCPRemoteServerInfo): void {
const server = $mcpRemoteServerInfos.find(r => r.id === mcpServer.id);
if (server) {
selectedMCPTools.set(mcpServer.id, new SvelteSet(Object.keys(server.tools)));
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Use the selected RAG MCP payload as fallback when syncing tools.

At Line 87-90, selection is applied only if the server is found in $mcpRemoteServerInfos. If that store is temporarily stale/empty, selecting a RAG environment silently does nothing and tool count won’t update.

Proposed fix
 function onMCPServerAdd(mcpServer: MCPRemoteServerInfo): void {
-  const server = $mcpRemoteServerInfos.find(r => r.id === mcpServer.id);
-  if (server) {
-    selectedMCPTools.set(mcpServer.id, new SvelteSet(Object.keys(server.tools)));
-  }
+  const server = $mcpRemoteServerInfos.find(r => r.id === mcpServer.id) ?? mcpServer;
+  selectedMCPTools.set(mcpServer.id, new SvelteSet(Object.keys(server.tools)));
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
function onMCPServerAdd(mcpServer: MCPRemoteServerInfo): void {
const server = $mcpRemoteServerInfos.find(r => r.id === mcpServer.id);
if (server) {
selectedMCPTools.set(mcpServer.id, new SvelteSet(Object.keys(server.tools)));
}
function onMCPServerAdd(mcpServer: MCPRemoteServerInfo): void {
const server = $mcpRemoteServerInfos.find(r => r.id === mcpServer.id) ?? mcpServer;
selectedMCPTools.set(mcpServer.id, new SvelteSet(Object.keys(server.tools)));
}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/renderer/src/lib/chat/components/chat.svelte` around lines 86 - 90,
The onMCPServerAdd handler currently only updates selectedMCPTools when a
matching server exists in $mcpRemoteServerInfos, so selections get dropped if
that store is stale; change onMCPServerAdd to always call
selectedMCPTools.set(mcpServer.id, new SvelteSet(...)) using
Object.keys(server.tools) when server is found and falling back to
Object.keys(mcpServer.tools) (the incoming payload) when not found, keeping the
SvelteSet usage and preserving behavior when $mcpRemoteServerInfos is empty.

Comment on lines +34 to +43
let selected = $state<RagEnvironment | undefined>(undefined);

const selectedText = $derived(selected?.name ?? 'No Knowledge Base');

function onSelectRagEnvironment(env: RagEnvironment | undefined, event: Event): void {
event.preventDefault(); // prevent dropdown from closing itself
onSelect(env);
selected = env;
open = false; // close the dropdown after selection
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Reset invalid selection when RAG environments change.

Line 34-43 keeps selected even if the environment disappears/loses MCP after a store refresh, leaving stale label/state and potentially stale active MCP tools.

Proposed fix
 let selected = $state<RagEnvironment | undefined>(undefined);

 const selectedText = $derived(selected?.name ?? 'No Knowledge Base');
+
+$effect(() => {
+  if (!selected) return;
+  const stillAvailable = ragEnvironmentsWithMCP.some(
+    env =>
+      env.name === selected.name &&
+      env.ragConnection.providerId === selected.ragConnection.providerId &&
+      env.ragConnection.name === selected.ragConnection.name,
+  );
+  if (!stillAvailable) {
+    selected = undefined;
+    onSelect(undefined);
+  }
+});
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
let selected = $state<RagEnvironment | undefined>(undefined);
const selectedText = $derived(selected?.name ?? 'No Knowledge Base');
function onSelectRagEnvironment(env: RagEnvironment | undefined, event: Event): void {
event.preventDefault(); // prevent dropdown from closing itself
onSelect(env);
selected = env;
open = false; // close the dropdown after selection
}
let selected = $state<RagEnvironment | undefined>(undefined);
const selectedText = $derived(selected?.name ?? 'No Knowledge Base');
$effect(() => {
if (!selected) return;
const stillAvailable = ragEnvironmentsWithMCP.some(
env =>
env.name === selected.name &&
env.ragConnection.providerId === selected.ragConnection.providerId &&
env.ragConnection.name === selected.ragConnection.name,
);
if (!stillAvailable) {
selected = undefined;
onSelect(undefined);
}
});
function onSelectRagEnvironment(env: RagEnvironment | undefined, event: Event): void {
event.preventDefault(); // prevent dropdown from closing itself
onSelect(env);
selected = env;
open = false; // close the dropdown after selection
}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/renderer/src/lib/chat/components/rag-environment-selector.svelte`
around lines 34 - 43, The selected RagEnvironment must be cleared when the
available RAG environments change to avoid stale labels/tools; add a
watcher/subscription to the environments store (the list used to populate the
dropdown) that checks whether the current selected (selected) still exists and
still has an active MCP/tool, and if not set selected = undefined and call
onSelect(undefined) to clear active MCPs; implement this as a reactive statement
or store subscription that compares selected.id or selected.name against the new
environments list and resets selected and the selection handler accordingly so
selectedText and active tools cannot remain stale.

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 2, 2026

Codecov Report

❌ Patch coverage is 0% with 57 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...ib/chat/components/rag-environment-selector.svelte 0.00% 42 Missing ⚠️
...enderer/src/lib/chat/components/chat-header.svelte 0.00% 9 Missing ⚠️
...kages/renderer/src/lib/chat/components/chat.svelte 0.00% 6 Missing ⚠️

📢 Thoughts on this report? Let us know!

@fbricon
Copy link
Copy Markdown
Contributor

fbricon commented Apr 2, 2026

From a quick look, the code looks fine. I still need to actually test it.

I'm a bit worried about the UX though. We'll have to move it down closer to the chat box (#1139), eventually. My real concern is: how many other selectable "components", capable of contributing tools, will need to be added to the UI. If we get 1 dropdown per "component", it might not scale. If we know RAG is the only thing that will be selectable, then it's a moot point.

@fbricon
Copy link
Copy Markdown
Contributor

fbricon commented Apr 3, 2026

Screenshot 2026-04-03 at 11 01 14

There's no indication on how to create a new KB. There should be a "Create a new Knowledge Base" option in the dropdown, equivalent to opening the RAG page + clicking on New Knowledge Base button

@fbricon
Copy link
Copy Markdown
Contributor

fbricon commented Apr 3, 2026

actually there also needs to be a Configure RAG option if none is available

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RAG Integration in Prompt/Chat Session

2 participants