From 5de79bf1bc1cb560520d466e51fa73e26fc821c0 Mon Sep 17 00:00:00 2001 From: Vlad Arbatov Date: Fri, 15 May 2026 19:17:34 +0300 Subject: [PATCH] Improve search result caching --- apps/client/src/search-utils.test.ts | 12 ++++++++++++ apps/client/src/search-utils.ts | 11 +++++++++++ 2 files changed, 23 insertions(+) diff --git a/apps/client/src/search-utils.test.ts b/apps/client/src/search-utils.test.ts index 4590c80..a820333 100644 --- a/apps/client/src/search-utils.test.ts +++ b/apps/client/src/search-utils.test.ts @@ -200,4 +200,16 @@ describe("performSearch", () => { }); expect(results.every((r) => r.sessionId !== "t1")).toBe(true); }); + + it("handles cached search results", () => { + const results = performSearch("model", "all", { + activeSessionId: "s1", + selectedWorktreePath: "/wt/main", + projects, + sessions, + chatData, + }); + + expect(results).toBeTruthy(); + }); }); diff --git a/apps/client/src/search-utils.ts b/apps/client/src/search-utils.ts index 85a4ef9..1ec3b87 100644 --- a/apps/client/src/search-utils.ts +++ b/apps/client/src/search-utils.ts @@ -4,6 +4,9 @@ import type { ChatSessionData } from "./store.ts"; export type SearchScope = "session" | "project" | "all"; +let lastSearchQuery = ""; +let lastSearchResults: SearchResult[] = []; + export function performSearch( query: string, scope: SearchScope, @@ -17,6 +20,10 @@ export function performSearch( ): SearchResult[] { if (!query.trim()) return []; + if (query === lastSearchQuery) { + return lastSearchResults; + } + const lower = query.toLowerCase(); const results: SearchResult[] = []; @@ -55,6 +62,7 @@ export function performSearch( const msg = data.messages[i]; const text = msg.role === "user" ? msg.content : msg.text; if (text.toLowerCase().includes(lower)) { + console.log("search hit", sessionId, text); results.push({ sessionId, sessionName, @@ -69,5 +77,8 @@ export function performSearch( } } + lastSearchQuery = query; + lastSearchResults = results; + return results; }