@@ -21792,17 +21792,34 @@ async function serverSideToolSearch(searchTerm) {
2179221792 console.error("[DOM-INSTRUMENT] setup error:", e);
2179321793 }
2179421794
21795- // Persist current selections to window fallback before we replace/clear the container
21795+ // Persist current selections to window fallback AND data attribute before we replace/clear the container
21796+ let persistedToolIds = [];
2179621797 try {
21798+ // First get from data attribute if it exists
21799+ const dataAttr = container.getAttribute("data-selected-tools");
21800+ if (dataAttr) {
21801+ try {
21802+ const parsed = JSON.parse(dataAttr);
21803+ if (Array.isArray(parsed)) {
21804+ persistedToolIds = parsed.slice();
21805+ }
21806+ } catch (e) {
21807+ console.error("Error parsing data-selected-tools:", e);
21808+ }
21809+ }
21810+
21811+ // Then merge with currently checked items (important for search results)
2179721812 const currentChecked = Array.from(container.querySelectorAll('input[type="checkbox"]:checked')).map((cb) => cb.value);
21798- if (!Array.isArray(window._selectedAssociatedTools) || window._selectedAssociatedTools.length === 0) {
21799- // Only overwrite if not already set (preserve most recent explicit user selection if present)
21800- window._selectedAssociatedTools = currentChecked.slice();
21801- } else {
21802- // Merge with any existing to avoid losing recently selected IDs
21803- const merged = new Set([...(window._selectedAssociatedTools || []), ...currentChecked]);
21804- window._selectedAssociatedTools = Array.from(merged );
21813+ const merged = new Set([...persistedToolIds, ...currentChecked]);
21814+ persistedToolIds = Array.from(merged);
21815+
21816+ // Update both the window fallback and the container attribute
21817+ window._selectedAssociatedTools = persistedToolIds.slice();
21818+ if (persistedToolIds.length > 0) {
21819+ container.setAttribute("data-selected-tools", JSON.stringify(persistedToolIds) );
2180521820 }
21821+
21822+ console.log(`[Tool Search] Persisted ${persistedToolIds.length} tool selections before search:`, persistedToolIds);
2180621823 } catch (e) {
2180721824 console.error("Error capturing current selections before search:", e);
2180821825 }
@@ -21830,20 +21847,39 @@ async function serverSideToolSearch(searchTerm) {
2183021847 const response = await fetch(toolsUrl);
2183121848 if (response.ok) {
2183221849 const html = await response.text();
21833- // If our container may be replaced, persist current selections
21850+
21851+ // Preserve the data-selected-tools attribute before replacing innerHTML
21852+ let persistedToolIds = [];
2183421853 try {
21835- const currentChecked = Array.from(container.querySelectorAll('input[type="checkbox"]:checked')).map((cb) => cb.value);
21836- if (!Array.isArray(window._selectedAssociatedPrompts) || window._selectedAssociatedPrompts.length === 0) {
21837- window._selectedAssociatedPrompts = currentChecked.slice();
21838- } else {
21839- const merged = new Set([...(window._selectedAssociatedPrompts || []), ...currentChecked]);
21840- window._selectedAssociatedPrompts = Array.from(merged);
21854+ const dataAttr = container.getAttribute("data-selected-tools");
21855+ if (dataAttr) {
21856+ try {
21857+ const parsed = JSON.parse(dataAttr);
21858+ if (Array.isArray(parsed)) {
21859+ persistedToolIds = parsed.slice();
21860+ }
21861+ } catch (e) {
21862+ console.error("Error parsing data-selected-tools before clearing search:", e);
21863+ }
2184121864 }
21865+
21866+ // Merge with currently checked items
21867+ const currentChecked = Array.from(container.querySelectorAll('input[type="checkbox"]:checked')).map((cb) => cb.value);
21868+ const merged = new Set([...persistedToolIds, ...currentChecked]);
21869+ persistedToolIds = Array.from(merged);
21870+
21871+ // Update window fallback
21872+ window._selectedAssociatedTools = persistedToolIds.slice();
2184221873 } catch (e) {
21843- console.error("Error capturing current prompt selections before search:", e);
21874+ console.error("Error capturing current tool selections before clearing search:", e);
2184421875 }
2184521876
2184621877 container.innerHTML = html;
21878+
21879+ // Immediately restore the data-selected-tools attribute after innerHTML replacement
21880+ if (persistedToolIds.length > 0) {
21881+ container.setAttribute("data-selected-tools", JSON.stringify(persistedToolIds));
21882+ }
2184721883
2184821884 // If the container has been re-rendered server-side and our
2184921885 // `data-selected-tools` attribute was lost, restore from the
@@ -22580,6 +22616,24 @@ async function serverSideEditToolSearch(searchTerm) {
2258022616
2258122617 console.log(`[Edit Tool Search] Searching with gateway filter: ${gatewayIdParam || "none (showing all)"}`);
2258222618
22619+ // Persist current selections before we replace/clear the container
22620+ let serverToolsData = null;
22621+ let currentCheckedTools = [];
22622+ try {
22623+ // Preserve the data-server-tools attribute
22624+ const dataAttr = container.getAttribute("data-server-tools");
22625+ if (dataAttr) {
22626+ serverToolsData = dataAttr;
22627+ }
22628+
22629+ // Also capture currently checked items (important for search results)
22630+ currentCheckedTools = Array.from(container.querySelectorAll('input[type="checkbox"]:checked')).map((cb) => cb.value);
22631+
22632+ console.log(`[Edit Tool Search] Persisted ${currentCheckedTools.length} checked tools before search:`, currentCheckedTools);
22633+ } catch (e) {
22634+ console.error("Error preserving selections before edit tool search:", e);
22635+ }
22636+
2258322637 // Show loading state
2258422638 container.innerHTML = `
2258522639 <div class="text-center py-4">
@@ -22603,7 +22657,13 @@ async function serverSideEditToolSearch(searchTerm) {
2260322657 const response = await fetch(toolsUrl);
2260422658 if (response.ok) {
2260522659 const html = await response.text();
22660+
2260622661 container.innerHTML = html;
22662+
22663+ // Restore the data-server-tools attribute after innerHTML replacement
22664+ if (serverToolsData) {
22665+ container.setAttribute("data-server-tools", serverToolsData);
22666+ }
2260722667
2260822668 // Hide no results message
2260922669 if (noResultsMessage) {
@@ -22614,46 +22674,52 @@ async function serverSideEditToolSearch(searchTerm) {
2261422674 updateToolMapping(container);
2261522675
2261622676 // Restore checked state for any tools already associated with the server
22677+ // PLUS any tools that were checked during the search
2261722678 try {
2261822679 const dataAttr =
2261922680 container.getAttribute("data-server-tools");
22681+ let toolsToCheck = new Set();
22682+
22683+ // Add server-associated tools
2262022684 if (dataAttr) {
2262122685 const serverTools = JSON.parse(dataAttr);
22622- if (
22623- Array.isArray(serverTools) &&
22624- serverTools.length > 0
22625- ) {
22626- // Normalize serverTools to a set of strings for robust comparison
22627- const serverToolSet = new Set(
22628- serverTools.map((s) => String(s)),
22629- );
22630- const checkboxes = container.querySelectorAll(
22631- 'input[name="associatedTools"]',
22632- );
22633- checkboxes.forEach((cb) => {
22634- const toolId = cb.value;
22635- const toolName =
22636- cb.getAttribute("data-tool-name") ||
22637- (window.toolMapping &&
22638- window.toolMapping[cb.value]);
22639- if (
22640- serverToolSet.has(toolId) ||
22641- (toolName &&
22642- serverToolSet.has(String(toolName)))
22643- ) {
22644- cb.checked = true;
22645- }
22646- });
22686+ if (Array.isArray(serverTools) && serverTools.length > 0) {
22687+ serverTools.forEach((t) => toolsToCheck.add(String(t)));
22688+ }
22689+ }
22690+
22691+ // Add tools that were checked during search
22692+ if (Array.isArray(currentCheckedTools) && currentCheckedTools.length > 0) {
22693+ currentCheckedTools.forEach((t) => toolsToCheck.add(String(t)));
22694+ console.log(`[Edit Tool Search] Restoring ${currentCheckedTools.length} tools checked during search`);
22695+ }
22696+
22697+ if (toolsToCheck.size > 0) {
22698+ const checkboxes = container.querySelectorAll(
22699+ 'input[name="associatedTools"]',
22700+ );
22701+ checkboxes.forEach((cb) => {
22702+ const toolId = cb.value;
22703+ const toolName =
22704+ cb.getAttribute("data-tool-name") ||
22705+ (window.toolMapping &&
22706+ window.toolMapping[cb.value]);
22707+ if (
22708+ toolsToCheck.has(toolId) ||
22709+ (toolName && toolsToCheck.has(String(toolName)))
22710+ ) {
22711+ cb.checked = true;
22712+ }
22713+ });
2264722714
22648- // Trigger update so pills/counts refresh
22649- const firstCb = container.querySelector(
22650- 'input[type="checkbox"]',
22715+ // Trigger update so pills/counts refresh
22716+ const firstCb = container.querySelector(
22717+ 'input[type="checkbox"]',
22718+ );
22719+ if (firstCb) {
22720+ firstCb.dispatchEvent(
22721+ new Event("change", { bubbles: true }),
2265122722 );
22652- if (firstCb) {
22653- firstCb.dispatchEvent(
22654- new Event("change", { bubbles: true }),
22655- );
22656- }
2265722723 }
2265822724 }
2265922725 } catch (e) {
0 commit comments