perf(wiki): subgraph API and interactive exploration for large graphs

Wikis with tens of thousands of pages used to crash the browser trying
to render the entire link graph at once (30MB+ JSON, 100k+ SVG elements).
This change moves the graph viewer from "fetch everything, render
everything" to "fetch a slice, expand on demand" with several matching
UX improvements.

Backend

* GET /wiki/graph now accepts `mode` (overview | ego), `center`, `depth`,
  `types`, `limit` query params. Default is overview top-500 by
  link_count, capped at 2000. Response includes a Meta object so the
  frontend can render a truncation hint and drive the UI.
* Pure helper `computeGraphSubset` extracted for testability; six unit
  tests cover overview truncation / type filter / ego BFS / missing
  center error.
* WikiLintService passes Limit=0 (uncapped) so link integrity checks
  still walk every page.
* Repo `Search` adds CASE-based relevance ranking (title 4, slug 3,
  summary 2, content 1) so full-text results put the most obvious
  matches first instead of whatever was updated most recently.

Frontend

* Graph viewer fetches overview on entry; ego pivot on double-click,
  search, wiki-link click, URL ?slug=, global issues jump.
* Shift+click or hover ⊕ button blooms neighbors onto the current
  canvas additively. Bloom tracks generations and LRU-evicts oldest
  when total exceeds 1500 nodes; ego center / selected node / latest
  anchor are always protected.
* "Grow frontier (N)" legend action expands every dashed-ring node in
  one click (6-way concurrency) while skipping Index/Log super-nodes
  that would otherwise dump the whole wiki onto the canvas.
* Node dashed expansion ring + drawer "X/Y neighbors shown" hint tell
  users which nodes still have neighbors to load.
* Search dropdown uses remote full-text search (debounced, sequence-
  numbered to drop stale responses) and falls back to the overview
  top-500 snapshot when the input is empty.
* Type filters now round-trip to the server so top-N is always
  computed from the user's active type set rather than hiding nodes
  client-side and shrinking the view.
* Status card replaces the cramped "centered on X · N hops · M nodes"
  line with a structured focus/overview summary, resolving slugs to
  page titles.
* Help popover in the legend lists every canvas shortcut.

i18n entries added for zh-CN / en-US / ko-KR / ru-RU.
This commit is contained in:
wizardchen
2026-05-08 13:29:19 +08:00
committed by lyingbug
parent fc5d16e331
commit df1e9a1bfd
13 changed files with 1962 additions and 137 deletions
+7 -2
View File
@@ -56,8 +56,13 @@ type WikiPageService interface {
// Creates a default one if it doesn't exist.
GetLog(ctx context.Context, kbID string) (*types.WikiPage, error)
// GetGraph returns the link graph data for visualization.
GetGraph(ctx context.Context, kbID string) (*types.WikiGraphData, error)
// GetGraph returns the link graph data for visualization. The caller
// supplies a WikiGraphRequest describing the desired slice of the graph
// (overview top-N or ego neighborhood around a center slug). Callers
// that need the full graph (e.g. wiki lint) can set Limit <= 0 to
// disable the node cap; the HTTP handler always clamps Limit to a
// safe range before invoking the service.
GetGraph(ctx context.Context, req *types.WikiGraphRequest) (*types.WikiGraphData, error)
// GetStats returns aggregate statistics about the wiki.
GetStats(ctx context.Context, kbID string) (*types.WikiStats, error)
+41 -1
View File
@@ -198,10 +198,50 @@ type WikiPageListResponse struct {
TotalPages int `json:"total_pages"`
}
// WikiGraphData represents the link graph structure for visualization
// WikiGraphMode enumerates the graph query modes exposed to the API.
const (
// WikiGraphModeOverview returns the top-N most-connected pages as an
// overview of the knowledge base. Intended for the first graph open.
WikiGraphModeOverview = "overview"
// WikiGraphModeEgo returns the neighborhood around a center page up to a
// configurable depth. Intended for drill-down interactions.
WikiGraphModeEgo = "ego"
)
// WikiGraphRequest is the service-layer input for graph queries. It is
// populated by the HTTP handler from query params and passed down to the
// service, which is responsible for enforcing mode-specific semantics.
//
// Limit policy: a non-positive `Limit` means "no cap" and is reserved for
// internal callers (e.g. wiki lint) that need the full graph. The HTTP
// handler always clamps `Limit` into a safe range before calling the
// service so external traffic can never request an uncapped graph.
type WikiGraphRequest struct {
KnowledgeBaseID string
Mode string // "overview" (default) | "ego"
Center string // ego mode center slug (required when Mode == "ego")
Depth int // ego mode BFS depth, >= 1
Types []string // optional page_type filter; empty = no filter
Limit int // max nodes to return; <= 0 means uncapped
}
// WikiGraphData represents the link graph structure for visualization.
type WikiGraphData struct {
Nodes []WikiGraphNode `json:"nodes"`
Edges []WikiGraphEdge `json:"edges"`
Meta WikiGraphMeta `json:"meta"`
}
// WikiGraphMeta describes how the returned subgraph relates to the full
// knowledge base graph. The frontend uses `Truncated` to decide whether to
// surface a "showing X of Y" hint and to enable ego-expansion UI.
type WikiGraphMeta struct {
Mode string `json:"mode"`
Total int `json:"total"` // total node count in the KB before filtering/limit
Returned int `json:"returned"` // number of nodes actually returned
Truncated bool `json:"truncated"` // true when Returned < Total (after filters)
Center string `json:"center,omitempty"` // populated in ego mode
Depth int `json:"depth,omitempty"` // populated in ego mode
}
// WikiGraphNode represents a node in the wiki link graph