Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(web): Preserve whitespace in message search controller (#13096)

Ben Bachem01df1ede47e6
diff --git a/web/src/components/ChatMessages/messageSearch/context.tsx b/web/src/components/ChatMessages/messageSearch/context.tsx
index 34cae1ac89..cb5deefdfc 100644
--- a/web/src/components/ChatMessages/messageSearch/context.tsx
+++ b/web/src/components/ChatMessages/messageSearch/context.tsx
@@ -142,6 +142,7 @@ export function useMessageSearch() {
openSearch: controller.openSearch,
closeSearch: controller.closeSearch,
setQueryInput: controller.setQueryInput,
+ blurQueryInput: controller.blurQueryInput,
nextMatch: controller.nextMatch,
previousMatch: controller.previousMatch,
};
diff --git a/web/src/components/ChatMessages/messageSearch/controller.clienttest.ts b/web/src/components/ChatMessages/messageSearch/controller.clienttest.ts
index f547745fe2..fe044b4eb2 100644
--- a/web/src/components/ChatMessages/messageSearch/controller.clienttest.ts
+++ b/web/src/components/ChatMessages/messageSearch/controller.clienttest.ts
@@ -123,4 +123,60 @@ describe("message search controller", () => {
expect.objectContaining({ from: 5, to: 6 }),
]);
});
+
+ it("preserves leading and trailing whitespace in literal queries", () => {
+ const controller = createMessageSearchController(["page-1"]);
+
+ controller.registerPageMessages("page-1", [
+ {
+ id: "message-1",
+ type: ChatMessageType.System,
+ role: ChatMessageRole.System,
+ content: " foo foo ",
+ },
+ ]);
+
+ expect(commitQuery(controller, " foo")).toEqual([
+ expect.objectContaining({ from: 0, to: 4 }),
+ expect.objectContaining({ from: 4, to: 8 }),
+ ]);
+ expect(controller.getSnapshot().query).toBe(" foo");
+
+ controller.setQueryInput("foo ");
+ controller.nextMatch();
+
+ expect(controller.getSnapshot().query).toBe("foo ");
+ expect(controller.getSnapshot().matches).toEqual([
+ expect.objectContaining({ from: 1, to: 5 }),
+ expect.objectContaining({ from: 5, to: 9 }),
+ ]);
+ });
+
+ it("clears whitespace-only input on blur", () => {
+ const controller = createMessageSearchController(["page-1"]);
+
+ controller.registerPageMessages("page-1", [
+ {
+ id: "message-1",
+ type: ChatMessageType.System,
+ role: ChatMessageRole.System,
+ content: "a b",
+ },
+ ]);
+
+ controller.setQueryInput(" ");
+ jest.runAllTimers();
+
+ expect(controller.getSnapshot().queryInput).toBe(" ");
+ expect(controller.getSnapshot().query).toBe(" ");
+ expect(controller.getSnapshot().matches).toEqual([
+ expect.objectContaining({ from: 1, to: 4 }),
+ ]);
+
+ controller.blurQueryInput();
+
+ expect(controller.getSnapshot().queryInput).toBe("");
+ expect(controller.getSnapshot().query).toBe("");
+ expect(controller.getSnapshot().matches).toEqual([]);
+ });
});
diff --git a/web/src/components/ChatMessages/messageSearch/controller.ts b/web/src/components/ChatMessages/messageSearch/controller.ts
index 5801bea9e4..8608f8dc85 100644
--- a/web/src/components/ChatMessages/messageSearch/controller.ts
+++ b/web/src/components/ChatMessages/messageSearch/controller.ts
@@ -67,6 +67,7 @@ export type MessageSearchController = {
openSearch: () => void;
closeSearch: () => void;
setQueryInput: (value: string) => void;
+ blurQueryInput: () => void;
nextMatch: () => void;
previousMatch: () => void;
setPageIds: (pageIds: string[]) => void;
@@ -376,7 +377,7 @@ export function createMessageSearchController(
clearPendingQueryTimeout();
- const queryChanged = commitSearchQuery(state.queryInput.trim());
+ const queryChanged = commitSearchQuery(state.queryInput);
if (queryChanged) {
emit();
}
@@ -465,8 +466,7 @@ export function createMessageSearchController(
state.queryInput = value;
clearPendingQueryTimeout();
- const nextSearchQuery = value.trim();
- if (nextSearchQuery === "") {
+ if (value === "") {
commitSearchQuery("");
emit();
return;
@@ -474,20 +474,38 @@ export function createMessageSearchController(
emit();
- if (nextSearchQuery === state.searchQuery) {
+ if (value === state.searchQuery) {
return;
}
pendingQueryTimeout = window.setTimeout(() => {
pendingQueryTimeout = null;
- const queryChanged = commitSearchQuery(nextSearchQuery);
+ const queryChanged = commitSearchQuery(value);
if (queryChanged) {
emit();
}
}, SEARCH_INPUT_DEBOUNCE_MS);
},
+ blurQueryInput() {
+ if (state.queryInput.trim() !== "") {
+ return;
+ }
+
+ clearPendingQueryTimeout();
+
+ const queryChanged = commitSearchQuery("");
+ const inputChanged = state.queryInput !== "";
+ if (inputChanged) {
+ state.queryInput = "";
+ }
+
+ if (queryChanged || inputChanged) {
+ emit();
+ }
+ },
+
nextMatch() {
moveActiveMatch(1);
},
diff --git a/web/src/components/ChatMessages/messageSearch/toolbar.tsx b/web/src/components/ChatMessages/messageSearch/toolbar.tsx
index 3836533631..b4ada147b9 100644
--- a/web/src/components/ChatMessages/messageSearch/toolbar.tsx
+++ b/web/src/components/ChatMessages/messageSearch/toolbar.tsx
@@ -25,6 +25,7 @@ export function MessageSearchToolbar({ className }: { className?: string }) {
openSearch,
closeSearch,
setQueryInput,
+ blurQueryInput,
nextMatch,
previousMatch,
} = useMessageSearch();
@@ -71,6 +72,7 @@ export function MessageSearchToolbar({ className }: { className?: string }) {
ref={inputRef}
value={queryInput}
onChange={(event) => setQueryInput(event.target.value)}
+ onBlur={blurQueryInput}
placeholder="Find in messages"
className="h-6 min-w-40 border-0 px-1 text-xs shadow-none focus-visible:ring-0 sm:min-w-56"
onKeyDown={(event) => {