Skip to content
Python

fix(memhub): search the memory list on submit, over 20 results

alvinfei7308ed9df2cf
diff --git a/memhub/apps/core/src/one_memory/api.py b/memhub/apps/core/src/one_memory/api.py
index d84aea3..571219b 100644
--- a/memhub/apps/core/src/one_memory/api.py
+++ b/memhub/apps/core/src/one_memory/api.py
@@ -3159,7 +3159,9 @@ def create_app(
# no score floor, then keep the rows that contain the words, so
# a word in the library is found and a word in none of it finds
# nothing. The whole library is never listed in Core.
- fetch = 500
+ # A larger window made one search take long enough to freeze
+ # the page; 20 is the bounded window the library searches.
+ fetch = 20
search_raw = await asyncio.to_thread(
client.search,
search_query,
diff --git a/memhub/apps/core/tests/test_memory_notes.py b/memhub/apps/core/tests/test_memory_notes.py
index f2c55b4..5a7cd30 100644
--- a/memhub/apps/core/tests/test_memory_notes.py
+++ b/memhub/apps/core/tests/test_memory_notes.py
@@ -409,7 +409,7 @@ def test_sdk_memory_library_uses_sdk_search_and_skips_empty_rows_without_mutatio
assert [item["memory_id"] for item in body["memories"]] == ["release"]
assert search_calls[0][0] == "release"
assert search_calls[0][1]["include_sources"] == ["cursor"]
- assert search_calls[0][1]["limit"] == 500
+ assert search_calls[0][1]["limit"] == 20
assert search_calls[0][1]["intention_limit"] == 0
assert search_calls[0][1]["min_score"] == 0.0
assert list_calls == []
diff --git a/memhub/apps/web/src/MemoryMap.test.tsx b/memhub/apps/web/src/MemoryMap.test.tsx
index 6110ca4..1025e0b 100644
--- a/memhub/apps/web/src/MemoryMap.test.tsx
+++ b/memhub/apps/web/src/MemoryMap.test.tsx
@@ -181,8 +181,9 @@ it("OBS-22: the graph inspector has no editing entry and never says changes cann
expect(screen.queryByText(/暂不能保存更改/)).toBeNull();
});
-it("OBS-19: Enter in the memory filter re-reads, exactly like the refresh button", async () => {
+it("OBS-19: Enter in the memory filter submits it; submitting it again re-reads", async () => {
const onRetry = vi.fn();
+ const onQuery = vi.fn();
const page = {
items: memories,
total: memories.length,
@@ -200,7 +201,7 @@ it("OBS-19: Enter in the memory filter re-reads, exactly like the refresh button
loading: false,
failed: false,
stale: false,
- onQuery: vi.fn(),
+ onQuery,
onSource: vi.fn(),
resetPage: vi.fn(),
onPage: vi.fn(),
@@ -211,11 +212,39 @@ it("OBS-19: Enter in the memory filter re-reads, exactly like the refresh button
await act(async () => {});
await userEvent.click(screen.getByRole("button", { name: "刷新地图记忆" }));
expect(onRetry).toHaveBeenCalledTimes(1);
+ // Typing alone does not search; Enter submits the words.
+ const input = screen.getByRole("textbox", { name: "筛选地图记忆" });
+ await userEvent.type(input, "第一");
+ expect(onQuery).not.toHaveBeenCalled();
+ await userEvent.type(input, "{Enter}");
+ expect(onQuery).toHaveBeenCalledWith("第一");
+ // Submitting the words already applied re-reads, like the refresh button.
+ await userEvent.clear(input);
+ await userEvent.type(input, "{Enter}");
+ expect(onRetry).toHaveBeenCalledTimes(2);
+});
+
+it("searches only when 搜索 is pressed, not on every keystroke", async () => {
+ const onQuery = vi.fn();
+ const resetPage = vi.fn();
+ render(
+ <Host
+ simplified
+ libraryView={{
+ ...pagedView({ items: memories, total: 2, offset: 0, limit: 100 }),
+ onQuery,
+ resetPage,
+ }}
+ />,
+ );
await userEvent.type(
screen.getByRole("textbox", { name: "筛选地图记忆" }),
- "第一{Enter}",
+ "版本号",
);
- expect(onRetry).toHaveBeenCalledTimes(2);
+ expect(onQuery).not.toHaveBeenCalled();
+ await userEvent.click(screen.getByRole("button", { name: "搜索" }));
+ expect(onQuery).toHaveBeenCalledWith("版本号");
+ expect(resetPage).toHaveBeenCalled();
});
it("OBS-15: switching memory shows a read in progress instead of the previous memory", async () => {
diff --git a/memhub/apps/web/src/MemoryMap.tsx b/memhub/apps/web/src/MemoryMap.tsx
index 5026429..3e9d9ff 100644
--- a/memhub/apps/web/src/MemoryMap.tsx
+++ b/memhub/apps/web/src/MemoryMap.tsx
@@ -197,6 +197,13 @@ export function MemoryMap(props: Props) {
const [axis, setAxis] = useState<MapAxis>("source");
const [groupKey, setGroupKey] = useState("");
const [search, setSearch] = useState("");
+ // On a server-paged list, typing only edits the words; the search runs when
+ // the reader submits it (the 搜索 button or Enter), not on every keystroke.
+ const [searchDraft, setSearchDraft] = useState(library?.query ?? "");
+ const submittedQuery = library?.query;
+ useEffect(() => {
+ if (submittedQuery !== undefined) setSearchDraft(submittedQuery);
+ }, [submittedQuery]);
const [selectedId, setSelectedId] = useState<string | null>(null);
const [groupNode, setGroupNode] = useState<MemoryGraphNode | null>(null);
const [question, setQuestion] = useState("");
@@ -827,7 +834,23 @@ export function MemoryMap(props: Props) {
))}
</fieldset>
)}
- <div className="map-search">
+ <form
+ className="map-search"
+ role="search"
+ onSubmit={(e) => {
+ e.preventDefault();
+ if (!library || libraryPane === "dialogues") return;
+ const words = searchDraft.trim();
+ setFilterNotice("");
+ clear();
+ if (words === library.query.trim()) {
+ library.onRetry();
+ return;
+ }
+ library.onQuery(words);
+ library.resetPage();
+ }}
+ >
<Search size={16} aria-hidden="true" />
<input
aria-label={text("筛选地图记忆")}
@@ -843,31 +866,39 @@ export function MemoryMap(props: Props) {
value={
libraryPane === "dialogues"
? dialogueFilter
- : (library?.query ?? search)
+ : library
+ ? searchDraft
+ : search
}
maxLength={library ? 512 : undefined}
onKeyDown={(e) => {
- // The refresh button next to this list re-reads the page;
- // Enter has to do the same thing, not sit there doing nothing.
- if (e.key !== "Enter" || e.nativeEvent.isComposing) return;
- e.preventDefault();
- library?.onRetry();
+ // Enter that confirms an IME candidate is not a search.
+ if (e.key === "Enter" && e.nativeEvent.isComposing)
+ e.preventDefault();
}}
onChange={(e) => {
if (libraryPane === "dialogues") {
setDialogueFilter(e.target.value);
return;
}
- setFilterNotice("");
- setSearch(e.target.value);
if (library) {
- clear();
- library.onQuery(e.target.value);
- library.resetPage();
+ setSearchDraft(e.target.value);
+ return;
}
+ setFilterNotice("");
+ setSearch(e.target.value);
}}
/>
- </div>
+ {library && libraryPane === "memories" ? (
+ <button
+ type="submit"
+ className="map-search-submit"
+ disabled={library.loading || hidden}
+ >
+ {text("搜索")}
+ </button>
+ ) : null}
+ </form>
{library && libraryPane === "memories" && (
<div className="map-list-heading" aria-live="polite">
<span>
diff --git a/memhub/apps/web/src/MemorySpace.map-search.test.tsx b/memhub/apps/web/src/MemorySpace.map-search.test.tsx
index 5acfc47..365c382 100644
--- a/memhub/apps/web/src/MemorySpace.map-search.test.tsx
+++ b/memhub/apps/web/src/MemorySpace.map-search.test.tsx
@@ -218,7 +218,7 @@ it("default map search finds an existing 501st memory rather than declaring no m
fireEvent.change(screen.getByLabelText("筛选地图记忆"), {
target: { value: "第501条独有目标" },
});
- fireEvent.keyDown(screen.getByLabelText("筛选地图记忆"), { key: "Enter" });
+ fireEvent.submit(screen.getByRole("search"));
await waitFor(() =>
expect(
within(screen.getByRole("list", { name: "地图记忆列表" })).queryByText(
@@ -310,6 +310,7 @@ it("OBS-13: the settled-count headline names the filter it is counting", async (
fireEvent.change(screen.getByLabelText("筛选地图记忆"), {
target: { value: "第501条独有目标" },
});
+ fireEvent.submit(screen.getByRole("search"));
await waitFor(() =>
expect(
screen.getByText(
@@ -320,6 +321,7 @@ it("OBS-13: the settled-count headline names the filter it is counting", async (
fireEvent.change(screen.getByLabelText("筛选地图记忆"), {
target: { value: "" },
});
+ fireEvent.submit(screen.getByRole("search"));
await chooseSource("codex");
await waitFor(() =>
expect(
@@ -473,6 +475,7 @@ it("distinguishes failed search from empty, then retries the same query", async
fireEvent.change(screen.getByLabelText("筛选地图记忆"), {
target: { value: "目标" },
});
+ fireEvent.submit(screen.getByRole("search"));
await screen.findByText("记忆读取失败,请重试。");
expect(screen.queryByText("没有匹配的记忆")).toBeNull();
expect(screen.queryByText("还没有已生成的记忆")).toBeNull();
@@ -490,6 +493,7 @@ it("distinguishes failed search from empty, then retries the same query", async
fireEvent.change(screen.getByLabelText("筛选地图记忆"), {
target: { value: "确实没有这项" },
});
+ fireEvent.submit(screen.getByRole("search"));
await screen.findByText("没有匹配的记忆");
expect(screen.queryByText("还没有已生成的记忆")).toBeNull();
expect(
@@ -510,6 +514,7 @@ it("aborts an older query after a source change and never replaces the newer res
fireEvent.change(screen.getByLabelText("筛选地图记忆"), {
target: { value: "旧搜索" },
});
+ fireEvent.submit(screen.getByRole("search"));
await waitFor(() => expect(resolve).toHaveLength(1));
const signals = vi
.mocked(api.getMemoryLibrary)
@@ -521,6 +526,7 @@ it("aborts an older query after a source change and never replaces the newer res
fireEvent.change(screen.getByLabelText("筛选地图记忆"), {
target: { value: "目标" },
});
+ fireEvent.submit(screen.getByRole("search"));
await within(screen.getByRole("list", { name: "地图记忆列表" })).findByText(
"第501条独有目标",
);
@@ -553,6 +559,7 @@ it("hidden map aborts page reads and does not accept late body or poll until rea
fireEvent.change(screen.getByLabelText("筛选地图记忆"), {
target: { value: "迟到" },
});
+ fireEvent.submit(screen.getByRole("search"));
await act(async () => {});
const signals = vi
.mocked(api.getMemoryLibrary)
@@ -611,6 +618,7 @@ it("keeps the populated map while a search is in flight and does not treat it as
fireEvent.change(screen.getByLabelText("筛选地图记忆"), {
target: { value: "onememory" },
});
+ fireEvent.submit(screen.getByRole("search"));
await act(async () => {});
expect(screen.queryByText("正在读取…")).toBeNull();
expect(screen.getByText("已沉淀 501 条 · 当前展示 100 条")).toBeTruthy();
@@ -700,6 +708,7 @@ it("explicit refresh keeps the search scope and replaces the page with the lates
fireEvent.change(screen.getByLabelText("筛选地图记忆"), {
target: { value: "目标" },
});
+ fireEvent.submit(screen.getByRole("search"));
const list = screen.getByRole("list", { name: "地图记忆列表" });
await within(list).findByText("第501条独有目标");
vi.mocked(api.getMemoryLibrary).mockResolvedValueOnce({
diff --git a/memhub/apps/web/src/core-visual.css b/memhub/apps/web/src/core-visual.css
index 55a35fa..a6b7098 100644
--- a/memhub/apps/web/src/core-visual.css
+++ b/memhub/apps/web/src/core-visual.css
@@ -689,6 +689,23 @@ button.rail-button.active::before {
background: transparent;
font-size: 12px;
}
+.map-search input {
+ flex: 1;
+}
+.map-search-submit {
+ flex: none;
+ padding: 4px 10px;
+ border: 0;
+ border-radius: 6px;
+ color: #fff;
+ background: #3d5a70;
+ font-size: 12px;
+ cursor: pointer;
+}
+.map-search-submit:disabled {
+ opacity: 0.5;
+ cursor: default;
+}
.map-search:focus-within,
.map-question:focus-within {
outline: 2px solid #7892a6;