Repository navigation
Conversation
When the LLM returns an empty or malformed markdown answer, the mind-map panel in the search page only shows a single 'root' node. This change adds a fallback tree built from the retrieved chunks on both the Go backend and the frontend. Backend (internal/handler/mindmap.go): - Return a fallback tree from retrieved sections when the LLM answer is empty or parsing yields no children. - Trim whitespace from the LLM answer before deciding it is empty. Frontend (web/src/pages/next-search/hooks.ts): - Pass current retrieval chunks to the mind-map request. - If the API returns an empty root node, build a fallback tree from the chunk contents so users still see a useful mind map. - Move the mind-map fetch into a useEffect so it reloads when the question, kb_ids, search_id, or chunks change. Fixes: search mindmap only shows root node Labels: ci
📝 WalkthroughWalkthroughAdds fallback mind-map generation when the primary LLM/backend response is empty or root-only. The Go handler builds a fallback tree from retrieval sections; the frontend hooks build a fallback tree from search chunks and wire the chunks parameter through the mind-map drawer flow. ChangesMind Map Fallback
Estimated code review effort: 2 (Simple) | ~15 minutes Sequence Diagram(s)sequenceDiagram
participant useSearching
participant useShowMindMapDrawer
participant useSearchFetchMindMap
participant BackendHandler
participant LLM
useSearching->>useShowMindMapDrawer: chunks, question, searchId
useShowMindMapDrawer->>useSearchFetchMindMap: fetchMindMap(searchParams, chunks)
useSearchFetchMindMap->>BackendHandler: request mind map
BackendHandler->>LLM: get answer
LLM-->>BackendHandler: response (nil/blank/root-only)
BackendHandler->>BackendHandler: fallbackMindMapFromSections(sections)
BackendHandler-->>useSearchFetchMindMap: mind map (possibly empty)
useSearchFetchMindMap->>useSearchFetchMindMap: check root-with-no-children
useSearchFetchMindMap->>useSearchFetchMindMap: buildFallbackMindMap(chunks)
useSearchFetchMindMap-->>useShowMindMapDrawer: final mind map
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
web/src/pages/next-search/hooks.ts (1)
139-152: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick winInclude
chunksin the ref gate.
Atweb/src/pages/next-search/hooks.ts:139-152, the effect depends onchunks, but theisEqual(searchParams, ref.current)check only tracksquestion,kb_ids, andsearch_id. A chunks-only update re-runs the effect and then gets blocked, sofetchMindMapnever fires with the new chunks. That breaks the fallback path when retrieval results arrive after the first request. Keepchunks(or a stable fingerprint) in the compared state.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@web/src/pages/next-search/hooks.ts` around lines 139 - 152, The effect in next-search/hooks.ts is gating fetchMindMap with ref.current using only question, kb_ids, and search_id, so chunks-only changes are ignored. Update the ref comparison in the useEffect block to include chunks as part of the stored/searchParams state, or compare against a stable chunks fingerprint, so fetchMindMap runs when chunks changes even if the other fields stay the same.
🧹 Nitpick comments (2)
web/src/pages/next-search/hooks.ts (2)
135-138: 🚀 Performance & Scalability | 🔵 Trivial | ⚖️ Poor tradeoffFetch now decoupled from drawer open — mind map generated eagerly on every question/chunk change.
handleShowModalno longer triggersfetchMindMap; the fetch runs unconditionally via the effect wheneverquestion/kb_ids/search_id/chunkschange, even if the user never opens the mind-map drawer. This proactively calls the (likely LLM-backed) mind-map endpoint on every search, which may add unnecessary backend/LLM cost for users who never open the mind map. If this eager preloading is intentional (per PR objective), consider gating onvisibleor lazily fetching only when the drawer is first shown, to avoid wasted calls.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@web/src/pages/next-search/hooks.ts` around lines 135 - 138, The mind map fetch in the `useCallback`/`handleShowModal` flow is now decoupled from opening the drawer, so it runs eagerly on every `question`, `kb_ids`, `search_id`, or `chunks` change. Update the `hooks.ts` logic so `fetchMindMap` is only triggered when appropriate for the intended UX, ideally by gating it on `visible` or fetching lazily inside `handleShowModal`/the drawer-open path, rather than unconditionally in the effect.
65-80: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winConsider typed chunk shape instead of
any[].
buildFallbackMindMapand the surrounding mutation (chunks?: any[], line 95) useanythroughout, losing type safety for the chunk content extraction (content_with_weight/content/text). Typingchunksagainst the project's existing chunk interface (e.g. as used byITestingResult/useSelectTestingResult) would catch field-name mismatches at compile time.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@web/src/pages/next-search/hooks.ts` around lines 65 - 80, The fallback mind map builder currently uses any for chunks, which removes type safety around content extraction. Update buildFallbackMindMap and the related mutation input to use the project’s existing chunk interface (the same shape used by ITestingResult/useSelectTestingResult) instead of any[], and adjust the content_with_weight/content/text access to compile against that type so field-name mismatches are caught early.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@internal/handler/mindmap.go`:
- Around line 232-246: The fallbackMindMapFromSections helper is truncating
section labels with a byte slice, which can break UTF-8 for non-ASCII text;
update the truncation logic to be rune-safe so mind map IDs remain valid for
CJK, accented characters, and emoji. Use a UTF-8 aware approach in
fallbackMindMapFromSections, and add the needed unicode/utf8 handling where the
length check and truncation happen, keeping the existing cleanMindMapText flow
intact.
---
Outside diff comments:
In `@web/src/pages/next-search/hooks.ts`:
- Around line 139-152: The effect in next-search/hooks.ts is gating fetchMindMap
with ref.current using only question, kb_ids, and search_id, so chunks-only
changes are ignored. Update the ref comparison in the useEffect block to include
chunks as part of the stored/searchParams state, or compare against a stable
chunks fingerprint, so fetchMindMap runs when chunks changes even if the other
fields stay the same.
---
Nitpick comments:
In `@web/src/pages/next-search/hooks.ts`:
- Around line 135-138: The mind map fetch in the `useCallback`/`handleShowModal`
flow is now decoupled from opening the drawer, so it runs eagerly on every
`question`, `kb_ids`, `search_id`, or `chunks` change. Update the `hooks.ts`
logic so `fetchMindMap` is only triggered when appropriate for the intended UX,
ideally by gating it on `visible` or fetching lazily inside
`handleShowModal`/the drawer-open path, rather than unconditionally in the
effect.
- Around line 65-80: The fallback mind map builder currently uses any for
chunks, which removes type safety around content extraction. Update
buildFallbackMindMap and the related mutation input to use the project’s
existing chunk interface (the same shape used by
ITestingResult/useSelectTestingResult) instead of any[], and adjust the
content_with_weight/content/text access to compile against that type so
field-name mismatches are caught early.
🪄 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: a826efd1-ef95-4cab-ba08-650cd8931196
📒 Files selected for processing (2)
internal/handler/mindmap.goweb/src/pages/next-search/hooks.ts
| func fallbackMindMapFromSections(sections []string) mindMapNode { | ||
| root := mindMapNode{ID: "root", Children: []mindMapNode{}} | ||
| for _, section := range sections { | ||
| section = cleanMindMapText(section) | ||
| if section == "" { | ||
| continue | ||
| } | ||
| if len(section) > 80 { | ||
| section = section[:80] + "..." | ||
| } | ||
| root.Children = append(root.Children, mindMapNode{ID: section, Children: []mindMapNode{}}) | ||
| } | ||
| return root | ||
| } | ||
|
|
There was a problem hiding this comment.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Byte-based truncation can corrupt multi-byte UTF-8 text.
section[:80] truncates by byte index. For non-ASCII content (e.g. CJK, accented text, emoji) commonly present in retrieved chunk sections, this can split a multi-byte UTF-8 rune, producing an invalid/garbled string in the fallback mind map labels.
🐛 Proposed fix using rune-safe truncation
+ runeCount := 0
+ byteIdx := 0
for _, section := range sections {
section = cleanMindMapText(section)
if section == "" {
continue
}
- if len(section) > 80 {
- section = section[:80] + "..."
+ if utf8.RuneCountInString(section) > 80 {
+ runeCount = 0
+ byteIdx = 0
+ for byteIdx < len(section) && runeCount < 80 {
+ _, size := utf8.DecodeRuneInString(section[byteIdx:])
+ byteIdx += size
+ runeCount++
+ }
+ section = section[:byteIdx] + "..."
}
root.Children = append(root.Children, mindMapNode{ID: section, Children: []mindMapNode{}})
}(requires importing unicode/utf8)
📝 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.
| func fallbackMindMapFromSections(sections []string) mindMapNode { | |
| root := mindMapNode{ID: "root", Children: []mindMapNode{}} | |
| for _, section := range sections { | |
| section = cleanMindMapText(section) | |
| if section == "" { | |
| continue | |
| } | |
| if len(section) > 80 { | |
| section = section[:80] + "..." | |
| } | |
| root.Children = append(root.Children, mindMapNode{ID: section, Children: []mindMapNode{}}) | |
| } | |
| return root | |
| } | |
| func fallbackMindMapFromSections(sections []string) mindMapNode { | |
| root := mindMapNode{ID: "root", Children: []mindMapNode{}} | |
| runeCount := 0 | |
| byteIdx := 0 | |
| for _, section := range sections { | |
| section = cleanMindMapText(section) | |
| if section == "" { | |
| continue | |
| } | |
| if utf8.RuneCountInString(section) > 80 { | |
| runeCount = 0 | |
| byteIdx = 0 | |
| for byteIdx < len(section) && runeCount < 80 { | |
| _, size := utf8.DecodeRuneInString(section[byteIdx:]) | |
| byteIdx += size | |
| runeCount++ | |
| } | |
| section = section[:byteIdx] + "..." | |
| } | |
| root.Children = append(root.Children, mindMapNode{ID: section, Children: []mindMapNode{}}) | |
| } | |
| return root | |
| } |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@internal/handler/mindmap.go` around lines 232 - 246, The
fallbackMindMapFromSections helper is truncating section labels with a byte
slice, which can break UTF-8 for non-ASCII text; update the truncation logic to
be rune-safe so mind map IDs remain valid for CJK, accented characters, and
emoji. Use a UTF-8 aware approach in fallbackMindMapFromSections, and add the
needed unicode/utf8 handling where the length check and truncation happen,
keeping the existing cleanMindMapText flow intact.
Summary
The search mind-map (思维导图) only shows a single "root" node when the LLM returns an empty or unparsable markdown answer. This PR fixes the issue by adding a fallback tree built from the retrieved chunks on both the Go backend and the frontend, so the mind map always displays meaningful content.
What changed
internal/handler/mindmap.goweb/src/pages/next-search/hooks.tsrootnode, build a fallback tree from the chunk contents.useEffectso it reloads when the question,kb_ids,search_id, or chunks change.How to verify
bash build.sh --test ./internal/handler -run TestParseMindMapMarkdownType of change
Related issue