feat: Add arrow toggle functionality in WikiBrowser component
Implemented new features in the WikiBrowser component to allow users to toggle the visibility of arrows on graph edges. This includes: 1. **Localization Updates**: Added translations for "Show arrows" and "Hide arrows" in English, Korean, Russian, and Chinese. 2. **Graph Rendering Enhancements**: Introduced logic to dynamically update SVG line attributes based on the toggle state, improving visual clarity in graph representations. 3. **State Management**: Added reactive state management for arrow visibility, ensuring seamless user interaction. These changes enhance the user experience by providing more control over graph visualizations.
This commit is contained in:
@@ -1633,6 +1633,8 @@ export default {
|
|||||||
graphEmpty: 'Loading graph...',
|
graphEmpty: 'Loading graph...',
|
||||||
logTitle: 'Activity Log',
|
logTitle: 'Activity Log',
|
||||||
graphNoData: 'No graph data yet. Upload documents first.',
|
graphNoData: 'No graph data yet. Upload documents first.',
|
||||||
|
showArrows: 'Show arrows',
|
||||||
|
hideArrows: 'Hide arrows',
|
||||||
healthCheck: 'Health Check',
|
healthCheck: 'Health Check',
|
||||||
},
|
},
|
||||||
buttons: {
|
buttons: {
|
||||||
|
|||||||
@@ -2156,6 +2156,8 @@ export default {
|
|||||||
graphEmpty: "그래프 로딩 중...",
|
graphEmpty: "그래프 로딩 중...",
|
||||||
logTitle: '활동 로그',
|
logTitle: '활동 로그',
|
||||||
graphNoData: '그래프 데이터가 없습니다. 먼저 문서를 업로드하세요.',
|
graphNoData: '그래프 데이터가 없습니다. 먼저 문서를 업로드하세요.',
|
||||||
|
showArrows: '화살표 표시',
|
||||||
|
hideArrows: '화살표 숨기기',
|
||||||
healthCheck: "상태 점검",
|
healthCheck: "상태 점검",
|
||||||
},
|
},
|
||||||
buttons: {
|
buttons: {
|
||||||
|
|||||||
@@ -1836,6 +1836,8 @@ export default {
|
|||||||
graphEmpty: 'Загрузка графа...',
|
graphEmpty: 'Загрузка графа...',
|
||||||
logTitle: 'Журнал активности',
|
logTitle: 'Журнал активности',
|
||||||
graphNoData: 'Нет данных графа. Сначала загрузите документы.',
|
graphNoData: 'Нет данных графа. Сначала загрузите документы.',
|
||||||
|
showArrows: 'Показать стрелки',
|
||||||
|
hideArrows: 'Скрыть стрелки',
|
||||||
healthCheck: 'Проверка здоровья',
|
healthCheck: 'Проверка здоровья',
|
||||||
},
|
},
|
||||||
buttons: {
|
buttons: {
|
||||||
|
|||||||
@@ -2139,6 +2139,8 @@ export default {
|
|||||||
graphEmpty: "加载图谱中...",
|
graphEmpty: "加载图谱中...",
|
||||||
logTitle: '操作日志',
|
logTitle: '操作日志',
|
||||||
graphNoData: '暂无图谱数据,请先上传文档',
|
graphNoData: '暂无图谱数据,请先上传文档',
|
||||||
|
showArrows: '显示箭头',
|
||||||
|
hideArrows: '隐藏箭头',
|
||||||
healthCheck: "健康检查",
|
healthCheck: "健康检查",
|
||||||
},
|
},
|
||||||
buttons: {
|
buttons: {
|
||||||
|
|||||||
@@ -223,6 +223,8 @@ const graphRef = ref<HTMLElement | null>(null)
|
|||||||
const loading = ref(false)
|
const loading = ref(false)
|
||||||
const graphLoading = ref(false)
|
const graphLoading = ref(false)
|
||||||
const graphReady = ref(false)
|
const graphReady = ref(false)
|
||||||
|
const showArrows = ref(true)
|
||||||
|
const graphEdgeEls = ref<{ line: SVGLineElement; bidir: boolean }[]>([])
|
||||||
const collapsedGroups = reactive<Record<string, boolean>>({})
|
const collapsedGroups = reactive<Record<string, boolean>>({})
|
||||||
const graphDrawerVisible = ref(false)
|
const graphDrawerVisible = ref(false)
|
||||||
const graphDrawerPage = ref<WikiPage | null>(null)
|
const graphDrawerPage = ref<WikiPage | null>(null)
|
||||||
@@ -423,9 +425,24 @@ async function doSearch() {
|
|||||||
finally { loading.value = false }
|
finally { loading.value = false }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function toggleArrows() {
|
||||||
|
showArrows.value = !showArrows.value
|
||||||
|
for (const e of graphEdgeEls.value) {
|
||||||
|
if (showArrows.value) {
|
||||||
|
e.line.setAttribute('marker-end', 'url(#arrow-end)')
|
||||||
|
if (e.bidir) e.line.setAttribute('marker-start', 'url(#arrow-start)')
|
||||||
|
} else {
|
||||||
|
e.line.removeAttribute('marker-end')
|
||||||
|
e.line.removeAttribute('marker-start')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function formatDate(dateStr: string) {
|
function formatDate(dateStr: string) {
|
||||||
if (!dateStr) return ''
|
if (!dateStr) return ''
|
||||||
return new Date(dateStr).toLocaleDateString()
|
const d = new Date(dateStr)
|
||||||
|
const pad = (n: number) => String(n).padStart(2, '0')
|
||||||
|
return `${d.getFullYear()}/${pad(d.getMonth() + 1)}/${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert slug like "entity/acme-corp" to a readable label "acme-corp"
|
// Convert slug like "entity/acme-corp" to a readable label "acme-corp"
|
||||||
@@ -521,15 +538,86 @@ function renderGraph() {
|
|||||||
// Node radius based on link count
|
// Node radius based on link count
|
||||||
function nodeRadius(n: GNode) { return Math.max(6, Math.min(18, 6 + n.linkCount * 1.5)) }
|
function nodeRadius(n: GNode) { return Math.max(6, Math.min(18, 6 + n.linkCount * 1.5)) }
|
||||||
|
|
||||||
// Create SVG elements for edges
|
// Define arrow markers in SVG <defs>
|
||||||
const edgeEls: { line: SVGLineElement; source: string; target: string }[] = []
|
const defs = document.createElementNS('http://www.w3.org/2000/svg', 'defs')
|
||||||
|
|
||||||
|
// Single-direction arrow (at end)
|
||||||
|
const markerEnd = document.createElementNS('http://www.w3.org/2000/svg', 'marker')
|
||||||
|
markerEnd.setAttribute('id', 'arrow-end')
|
||||||
|
markerEnd.setAttribute('viewBox', '0 0 10 6')
|
||||||
|
markerEnd.setAttribute('refX', '10')
|
||||||
|
markerEnd.setAttribute('refY', '3')
|
||||||
|
markerEnd.setAttribute('markerWidth', '8')
|
||||||
|
markerEnd.setAttribute('markerHeight', '6')
|
||||||
|
markerEnd.setAttribute('orient', 'auto')
|
||||||
|
const arrowPath = document.createElementNS('http://www.w3.org/2000/svg', 'path')
|
||||||
|
arrowPath.setAttribute('d', 'M0,0 L10,3 L0,6')
|
||||||
|
arrowPath.setAttribute('fill', '#c0c4cc')
|
||||||
|
markerEnd.appendChild(arrowPath)
|
||||||
|
defs.appendChild(markerEnd)
|
||||||
|
|
||||||
|
// Bidirectional: arrow at start (reverse)
|
||||||
|
const markerStart = document.createElementNS('http://www.w3.org/2000/svg', 'marker')
|
||||||
|
markerStart.setAttribute('id', 'arrow-start')
|
||||||
|
markerStart.setAttribute('viewBox', '0 0 10 6')
|
||||||
|
markerStart.setAttribute('refX', '0')
|
||||||
|
markerStart.setAttribute('refY', '3')
|
||||||
|
markerStart.setAttribute('markerWidth', '8')
|
||||||
|
markerStart.setAttribute('markerHeight', '6')
|
||||||
|
markerStart.setAttribute('orient', 'auto')
|
||||||
|
const arrowPathStart = document.createElementNS('http://www.w3.org/2000/svg', 'path')
|
||||||
|
arrowPathStart.setAttribute('d', 'M10,0 L0,3 L10,6')
|
||||||
|
arrowPathStart.setAttribute('fill', '#c0c4cc')
|
||||||
|
markerStart.appendChild(arrowPathStart)
|
||||||
|
defs.appendChild(markerStart)
|
||||||
|
|
||||||
|
// Highlighted arrows
|
||||||
|
for (const id of ['arrow-end-hl', 'arrow-start-hl']) {
|
||||||
|
const m = document.createElementNS('http://www.w3.org/2000/svg', 'marker')
|
||||||
|
m.setAttribute('id', id)
|
||||||
|
m.setAttribute('viewBox', '0 0 10 6')
|
||||||
|
m.setAttribute('refX', id.includes('end') ? '10' : '0')
|
||||||
|
m.setAttribute('refY', '3')
|
||||||
|
m.setAttribute('markerWidth', '8')
|
||||||
|
m.setAttribute('markerHeight', '6')
|
||||||
|
m.setAttribute('orient', 'auto')
|
||||||
|
const p = document.createElementNS('http://www.w3.org/2000/svg', 'path')
|
||||||
|
p.setAttribute('d', id.includes('end') ? 'M0,0 L10,3 L0,6' : 'M10,0 L0,3 L10,6')
|
||||||
|
p.setAttribute('fill', '#0052d9')
|
||||||
|
m.appendChild(p)
|
||||||
|
defs.appendChild(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
svg.appendChild(defs)
|
||||||
|
|
||||||
|
// Detect bidirectional edges (A→B and B→A both exist)
|
||||||
|
const edgePairSet = new Set<string>()
|
||||||
for (const edge of data.edges) {
|
for (const edge of data.edges) {
|
||||||
|
edgePairSet.add(`${edge.source}→${edge.target}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create SVG elements for edges (deduplicate bidirectional into single line with double arrows)
|
||||||
|
type EdgeEl = { line: SVGLineElement; source: string; target: string; bidir: boolean }
|
||||||
|
const edgeEls: EdgeEl[] = []
|
||||||
|
const processedPairs = new Set<string>()
|
||||||
|
|
||||||
|
for (const edge of data.edges) {
|
||||||
|
const pairKey = [edge.source, edge.target].sort().join('↔')
|
||||||
|
if (processedPairs.has(pairKey)) continue
|
||||||
|
processedPairs.add(pairKey)
|
||||||
|
|
||||||
|
const bidir = edgePairSet.has(`${edge.target}→${edge.source}`)
|
||||||
|
|
||||||
const line = document.createElementNS('http://www.w3.org/2000/svg', 'line')
|
const line = document.createElementNS('http://www.w3.org/2000/svg', 'line')
|
||||||
line.setAttribute('stroke', '#c0c4cc')
|
line.setAttribute('stroke', '#c0c4cc')
|
||||||
line.setAttribute('stroke-width', '1.2')
|
line.setAttribute('stroke-width', '1.2')
|
||||||
line.setAttribute('stroke-opacity', '0.4')
|
line.setAttribute('stroke-opacity', '0.4')
|
||||||
|
line.setAttribute('marker-end', 'url(#arrow-end)')
|
||||||
|
if (bidir) {
|
||||||
|
line.setAttribute('marker-start', 'url(#arrow-start)')
|
||||||
|
}
|
||||||
edgeG.appendChild(line)
|
edgeG.appendChild(line)
|
||||||
edgeEls.push({ line, source: edge.source, target: edge.target })
|
edgeEls.push({ line, source: edge.source, target: edge.target, bidir })
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create SVG elements for nodes
|
// Create SVG elements for nodes
|
||||||
@@ -642,10 +730,7 @@ function renderGraph() {
|
|||||||
const s = nodeMap.get(e.source)
|
const s = nodeMap.get(e.source)
|
||||||
const t = nodeMap.get(e.target)
|
const t = nodeMap.get(e.target)
|
||||||
if (s && t) {
|
if (s && t) {
|
||||||
e.line.setAttribute('x1', String(s.x))
|
setEdgePositions(e.line, s, t, nodeRadius)
|
||||||
e.line.setAttribute('y1', String(s.y))
|
|
||||||
e.line.setAttribute('x2', String(t.x))
|
|
||||||
e.line.setAttribute('y2', String(t.y))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -660,23 +745,43 @@ function renderGraph() {
|
|||||||
const s = nodeMap.get(e.source)
|
const s = nodeMap.get(e.source)
|
||||||
const t = nodeMap.get(e.target)
|
const t = nodeMap.get(e.target)
|
||||||
if (s && t) {
|
if (s && t) {
|
||||||
e.line.setAttribute('x1', String(s.x)); e.line.setAttribute('y1', String(s.y))
|
setEdgePositions(e.line, s, t, nodeRadius)
|
||||||
e.line.setAttribute('x2', String(t.x)); e.line.setAttribute('y2', String(t.y))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw legend
|
// Draw legend
|
||||||
drawLegend(svg, width)
|
drawLegend(svg, width)
|
||||||
|
|
||||||
|
// Store edge refs for arrow toggle
|
||||||
|
graphEdgeEls.value = edgeEls.map(e => ({ line: e.line, bidir: e.bidir }))
|
||||||
|
|
||||||
graphAnimFrame = requestAnimationFrame(tick)
|
graphAnimFrame = requestAnimationFrame(tick)
|
||||||
graphReady.value = true
|
graphReady.value = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set edge line positions, shortened to stop at node circle boundary so arrows are visible
|
||||||
|
function setEdgePositions(line: SVGLineElement, s: GNode, t: GNode, nodeRadius: (n: GNode) => number) {
|
||||||
|
const dx = t.x - s.x
|
||||||
|
const dy = t.y - s.y
|
||||||
|
const dist = Math.sqrt(dx * dx + dy * dy) || 1
|
||||||
|
const ux = dx / dist
|
||||||
|
const uy = dy / dist
|
||||||
|
|
||||||
|
// Shorten each end by the node radius + arrow margin
|
||||||
|
const rS = nodeRadius(s) + 4
|
||||||
|
const rT = nodeRadius(t) + 4
|
||||||
|
|
||||||
|
line.setAttribute('x1', String(s.x + ux * rS))
|
||||||
|
line.setAttribute('y1', String(s.y + uy * rS))
|
||||||
|
line.setAttribute('x2', String(t.x - ux * rT))
|
||||||
|
line.setAttribute('y2', String(t.y - uy * rT))
|
||||||
|
}
|
||||||
|
|
||||||
// ─── Drag ───
|
// ─── Drag ───
|
||||||
function setupDrag(
|
function setupDrag(
|
||||||
g: SVGGElement, node: GNode,
|
g: SVGGElement, node: GNode,
|
||||||
nodeMap: Map<string, GNode>,
|
nodeMap: Map<string, GNode>,
|
||||||
edgeEls: { line: SVGLineElement; source: string; target: string }[],
|
edgeEls: { line: SVGLineElement; source: string; target: string; bidir: boolean }[],
|
||||||
nodeEls: { g: SVGGElement; circle: SVGCircleElement; text: SVGTextElement; node: GNode }[],
|
nodeEls: { g: SVGGElement; circle: SVGCircleElement; text: SVGTextElement; node: GNode }[],
|
||||||
nodeRadius: (n: GNode) => number,
|
nodeRadius: (n: GNode) => number,
|
||||||
) {
|
) {
|
||||||
@@ -720,10 +825,11 @@ function setupDrag(
|
|||||||
g.setAttribute('transform', `translate(${node.x},${node.y})`)
|
g.setAttribute('transform', `translate(${node.x},${node.y})`)
|
||||||
// Update connected edges immediately
|
// Update connected edges immediately
|
||||||
for (const edge of edgeEls) {
|
for (const edge of edgeEls) {
|
||||||
const s = (edge.source === node.slug) ? node : undefined
|
if (edge.source === node.slug || edge.target === node.slug) {
|
||||||
const t = (edge.target === node.slug) ? node : undefined
|
const sn = nodeMap.get(edge.source)
|
||||||
if (s) { edge.line.setAttribute('x1', String(s.x)); edge.line.setAttribute('y1', String(s.y)) }
|
const tn = nodeMap.get(edge.target)
|
||||||
if (t) { edge.line.setAttribute('x2', String(t.x)); edge.line.setAttribute('y2', String(t.y)) }
|
if (sn && tn) setEdgePositions(edge.line, sn, tn, nodeRadius)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -798,7 +904,7 @@ function applyHighlight(
|
|||||||
slug: string,
|
slug: string,
|
||||||
adjacency: Map<string, Set<string>>,
|
adjacency: Map<string, Set<string>>,
|
||||||
nodeEls: { g: SVGGElement; circle: SVGCircleElement; text: SVGTextElement; node: GNode }[],
|
nodeEls: { g: SVGGElement; circle: SVGCircleElement; text: SVGTextElement; node: GNode }[],
|
||||||
edgeEls: { line: SVGLineElement; source: string; target: string }[],
|
edgeEls: { line: SVGLineElement; source: string; target: string; bidir: boolean }[],
|
||||||
) {
|
) {
|
||||||
const neighbors = adjacency.get(slug) || new Set()
|
const neighbors = adjacency.get(slug) || new Set()
|
||||||
for (const { g, circle, node } of nodeEls) {
|
for (const { g, circle, node } of nodeEls) {
|
||||||
@@ -816,23 +922,25 @@ function applyHighlight(
|
|||||||
if (e.source === slug || e.target === slug) {
|
if (e.source === slug || e.target === slug) {
|
||||||
e.line.setAttribute('stroke-opacity', '0.9')
|
e.line.setAttribute('stroke-opacity', '0.9')
|
||||||
e.line.setAttribute('stroke-width', '2')
|
e.line.setAttribute('stroke-width', '2')
|
||||||
e.line.setAttribute('stroke', nodeColorMap[
|
const hlColor = nodeColorMap[
|
||||||
(e.source === slug ? edgeEls : edgeEls).length ? '' : ''
|
|
||||||
] || '#0052d9')
|
|
||||||
// Color the edge with the active node's color
|
|
||||||
e.line.setAttribute('stroke', nodeColorMap[
|
|
||||||
nodeEls.find(n => n.node.slug === slug)?.node.type || ''
|
nodeEls.find(n => n.node.slug === slug)?.node.type || ''
|
||||||
] || '#0052d9')
|
] || '#0052d9'
|
||||||
|
e.line.setAttribute('stroke', hlColor)
|
||||||
|
e.line.setAttribute('marker-end', 'url(#arrow-end-hl)')
|
||||||
|
if (e.bidir) e.line.setAttribute('marker-start', 'url(#arrow-start-hl)')
|
||||||
} else {
|
} else {
|
||||||
e.line.setAttribute('stroke-opacity', '0.08')
|
e.line.setAttribute('stroke-opacity', '0.08')
|
||||||
e.line.setAttribute('stroke-width', '1')
|
e.line.setAttribute('stroke-width', '1')
|
||||||
|
e.line.setAttribute('marker-end', 'url(#arrow-end)')
|
||||||
|
if (e.bidir) e.line.setAttribute('marker-start', 'url(#arrow-start)')
|
||||||
|
else e.line.removeAttribute('marker-start')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function clearHighlight(
|
function clearHighlight(
|
||||||
nodeEls: { g: SVGGElement; circle: SVGCircleElement; text: SVGTextElement; node: GNode }[],
|
nodeEls: { g: SVGGElement; circle: SVGCircleElement; text: SVGTextElement; node: GNode }[],
|
||||||
edgeEls: { line: SVGLineElement; source: string; target: string }[],
|
edgeEls: { line: SVGLineElement; source: string; target: string; bidir: boolean }[],
|
||||||
) {
|
) {
|
||||||
for (const { g, circle, node } of nodeEls) {
|
for (const { g, circle, node } of nodeEls) {
|
||||||
circle.setAttribute('r', String(Math.max(6, Math.min(18, 6 + node.linkCount * 1.5))))
|
circle.setAttribute('r', String(Math.max(6, Math.min(18, 6 + node.linkCount * 1.5))))
|
||||||
@@ -843,6 +951,9 @@ function clearHighlight(
|
|||||||
e.line.setAttribute('stroke', '#c0c4cc')
|
e.line.setAttribute('stroke', '#c0c4cc')
|
||||||
e.line.setAttribute('stroke-width', '1.2')
|
e.line.setAttribute('stroke-width', '1.2')
|
||||||
e.line.setAttribute('stroke-opacity', '0.4')
|
e.line.setAttribute('stroke-opacity', '0.4')
|
||||||
|
e.line.setAttribute('marker-end', 'url(#arrow-end)')
|
||||||
|
if (e.bidir) e.line.setAttribute('marker-start', 'url(#arrow-start)')
|
||||||
|
else e.line.removeAttribute('marker-start')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -886,6 +997,50 @@ function drawLegend(svg: SVGSVGElement, width: number) {
|
|||||||
y += 22
|
y += 22
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Divider line
|
||||||
|
y += 4
|
||||||
|
const divider = document.createElementNS('http://www.w3.org/2000/svg', 'line')
|
||||||
|
divider.setAttribute('x1', '-2'); divider.setAttribute('x2', '118')
|
||||||
|
divider.setAttribute('y1', String(y)); divider.setAttribute('y2', String(y))
|
||||||
|
divider.setAttribute('stroke', 'var(--td-component-stroke)')
|
||||||
|
divider.setAttribute('stroke-width', '1')
|
||||||
|
legendG.appendChild(divider)
|
||||||
|
y += 12
|
||||||
|
|
||||||
|
// Arrow toggle row
|
||||||
|
const arrowRow = document.createElementNS('http://www.w3.org/2000/svg', 'g')
|
||||||
|
arrowRow.style.cursor = 'pointer'
|
||||||
|
|
||||||
|
// Arrow icon (small →)
|
||||||
|
const arrowIcon = document.createElementNS('http://www.w3.org/2000/svg', 'text')
|
||||||
|
arrowIcon.setAttribute('x', '3'); arrowIcon.setAttribute('y', String(y + 3))
|
||||||
|
arrowIcon.setAttribute('font-size', '13')
|
||||||
|
arrowIcon.setAttribute('fill', showArrows.value ? 'var(--td-brand-color)' : 'var(--td-text-color-placeholder)')
|
||||||
|
arrowIcon.textContent = '→'
|
||||||
|
arrowRow.appendChild(arrowIcon)
|
||||||
|
|
||||||
|
const arrowLabel = document.createElementNS('http://www.w3.org/2000/svg', 'text')
|
||||||
|
arrowLabel.setAttribute('x', '18'); arrowLabel.setAttribute('y', String(y + 4))
|
||||||
|
arrowLabel.setAttribute('font-size', '11')
|
||||||
|
arrowLabel.setAttribute('fill', showArrows.value ? 'var(--td-brand-color)' : 'var(--td-text-color-secondary)')
|
||||||
|
arrowLabel.textContent = showArrows.value
|
||||||
|
? t('knowledgeEditor.wikiBrowser.hideArrows')
|
||||||
|
: t('knowledgeEditor.wikiBrowser.showArrows')
|
||||||
|
arrowRow.appendChild(arrowLabel)
|
||||||
|
|
||||||
|
arrowRow.addEventListener('click', () => {
|
||||||
|
toggleArrows()
|
||||||
|
// Update label & color after toggle
|
||||||
|
arrowLabel.textContent = showArrows.value
|
||||||
|
? t('knowledgeEditor.wikiBrowser.hideArrows')
|
||||||
|
: t('knowledgeEditor.wikiBrowser.showArrows')
|
||||||
|
arrowLabel.setAttribute('fill', showArrows.value ? 'var(--td-brand-color)' : 'var(--td-text-color-secondary)')
|
||||||
|
arrowIcon.setAttribute('fill', showArrows.value ? 'var(--td-brand-color)' : 'var(--td-text-color-placeholder)')
|
||||||
|
})
|
||||||
|
|
||||||
|
legendG.appendChild(arrowRow)
|
||||||
|
y += 16
|
||||||
|
|
||||||
bg.setAttribute('height', String(y + 4))
|
bg.setAttribute('height', String(y + 4))
|
||||||
svg.appendChild(legendG)
|
svg.appendChild(legendG)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -109,7 +109,7 @@ const WikiPageUpdatePrompt = `You are a wiki editor tasked with updating an exis
|
|||||||
## Instructions
|
## Instructions
|
||||||
1. Merge the new information into the existing page content.
|
1. Merge the new information into the existing page content.
|
||||||
2. Preserve all existing information that is still valid.
|
2. Preserve all existing information that is still valid.
|
||||||
3. If the new information contradicts existing content, note the contradiction explicitly: "> **Note:** This contradicts earlier information from [source]. [old claim] vs [new claim]."
|
3. If the new information contradicts existing content, prefer the newer information and silently replace the old claim.
|
||||||
4. Add new facts, details, and context from the new document.
|
4. Add new facts, details, and context from the new document.
|
||||||
5. Update cross-references: add new [[wiki-link]] references where appropriate.
|
5. Update cross-references: add new [[wiki-link]] references where appropriate.
|
||||||
6. Maintain the existing page structure and formatting style.
|
6. Maintain the existing page structure and formatting style.
|
||||||
@@ -134,11 +134,11 @@ const WikiPageRetractPrompt = `You are a wiki editor. A source document has been
|
|||||||
1. Carefully review the existing page content.
|
1. Carefully review the existing page content.
|
||||||
2. Remove any facts, claims, or details that were ONLY sourced from "{{.DeletedDocTitle}}".
|
2. Remove any facts, claims, or details that were ONLY sourced from "{{.DeletedDocTitle}}".
|
||||||
3. If a fact is also supported by the remaining source documents, KEEP it.
|
3. If a fact is also supported by the remaining source documents, KEEP it.
|
||||||
4. If you are unsure whether a fact came from the deleted document, keep it but add a note: "> *This information may need verification after source removal.*"
|
4. If you are unsure whether a fact came from the deleted document, keep it as-is — do NOT add any review notes or annotations.
|
||||||
5. Update or remove the "Source: {{.DeletedDocTitle}}" reference line if present.
|
5. Update or remove the "Source: {{.DeletedDocTitle}}" reference line if present.
|
||||||
6. Remove any [[wiki-link]] references that point to pages that no longer exist, if you can identify them.
|
6. Remove any [[wiki-link]] references that point to pages that no longer exist, if you can identify them.
|
||||||
7. Maintain the existing page structure, formatting style, and language.
|
7. Maintain the existing page structure, formatting style, and language.
|
||||||
8. If after removing the deleted document's contributions the page becomes nearly empty, output just: "# [Title]\n\n*This page has been archived — the primary source document was removed.*"
|
8. If after removing the deleted document's contributions the page becomes nearly empty, output just: "# [Title]\n\n*This page's primary source document was removed.*"
|
||||||
9. Write in {{.Language}}.
|
9. Write in {{.Language}}.
|
||||||
|
|
||||||
Output ONLY the updated Markdown content. Do not include any preamble or explanation.`
|
Output ONLY the updated Markdown content. Do not include any preamble or explanation.`
|
||||||
|
|||||||
@@ -13,6 +13,9 @@ import (
|
|||||||
// ErrWikiPageNotFound is returned when a wiki page is not found
|
// ErrWikiPageNotFound is returned when a wiki page is not found
|
||||||
var ErrWikiPageNotFound = errors.New("wiki page not found")
|
var ErrWikiPageNotFound = errors.New("wiki page not found")
|
||||||
|
|
||||||
|
// ErrWikiPageConflict is returned when an optimistic lock conflict is detected
|
||||||
|
var ErrWikiPageConflict = errors.New("wiki page version conflict")
|
||||||
|
|
||||||
// wikiPageRepository implements the WikiPageRepository interface
|
// wikiPageRepository implements the WikiPageRepository interface
|
||||||
type wikiPageRepository struct {
|
type wikiPageRepository struct {
|
||||||
db *gorm.DB
|
db *gorm.DB
|
||||||
@@ -28,9 +31,46 @@ func (r *wikiPageRepository) Create(ctx context.Context, page *types.WikiPage) e
|
|||||||
return r.db.WithContext(ctx).Create(page).Error
|
return r.db.WithContext(ctx).Create(page).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update updates an existing wiki page record
|
// Update updates an existing wiki page record with optimistic locking.
|
||||||
|
// Increments version — use only for content changes visible to the user.
|
||||||
|
// The caller must set page.Version to the expected current version.
|
||||||
func (r *wikiPageRepository) Update(ctx context.Context, page *types.WikiPage) error {
|
func (r *wikiPageRepository) Update(ctx context.Context, page *types.WikiPage) error {
|
||||||
result := r.db.WithContext(ctx).Save(page)
|
expectedVersion := page.Version
|
||||||
|
page.Version = expectedVersion + 1
|
||||||
|
|
||||||
|
result := r.db.WithContext(ctx).
|
||||||
|
Model(page).
|
||||||
|
Where("id = ? AND version = ?", page.ID, expectedVersion).
|
||||||
|
Updates(page)
|
||||||
|
if result.Error != nil {
|
||||||
|
return result.Error
|
||||||
|
}
|
||||||
|
if result.RowsAffected == 0 {
|
||||||
|
// Could be not found or version conflict — check which
|
||||||
|
var count int64
|
||||||
|
r.db.WithContext(ctx).Model(&types.WikiPage{}).Where("id = ?", page.ID).Count(&count)
|
||||||
|
if count == 0 {
|
||||||
|
return ErrWikiPageNotFound
|
||||||
|
}
|
||||||
|
return ErrWikiPageConflict
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateMeta updates only metadata fields (links, status, source_refs) WITHOUT
|
||||||
|
// incrementing the version number. Used for internal bookkeeping operations
|
||||||
|
// like link maintenance, status changes, and source ref updates.
|
||||||
|
func (r *wikiPageRepository) UpdateMeta(ctx context.Context, page *types.WikiPage) error {
|
||||||
|
result := r.db.WithContext(ctx).
|
||||||
|
Model(page).
|
||||||
|
Where("id = ?", page.ID).
|
||||||
|
Updates(map[string]interface{}{
|
||||||
|
"in_links": page.InLinks,
|
||||||
|
"out_links": page.OutLinks,
|
||||||
|
"status": page.Status,
|
||||||
|
"source_refs": page.SourceRefs,
|
||||||
|
"updated_at": page.UpdatedAt,
|
||||||
|
})
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
return result.Error
|
return result.Error
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1284,7 +1284,7 @@ func (s *knowledgeService) cleanupWikiOnKnowledgeDelete(ctx context.Context, kbI
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var archivedSlugs []string
|
var deletedSlugs []string
|
||||||
var retractSlugs []string
|
var retractSlugs []string
|
||||||
for _, page := range pages {
|
for _, page := range pages {
|
||||||
// Skip index/log system pages
|
// Skip index/log system pages
|
||||||
@@ -1296,18 +1296,16 @@ func (s *knowledgeService) cleanupWikiOnKnowledgeDelete(ctx context.Context, kbI
|
|||||||
remaining := removeSourceRef(page.SourceRefs, knowledgeID)
|
remaining := removeSourceRef(page.SourceRefs, knowledgeID)
|
||||||
|
|
||||||
if len(remaining) == 0 {
|
if len(remaining) == 0 {
|
||||||
// No other sources → archive the page
|
// No other sources → delete the page entirely
|
||||||
page.Status = types.WikiPageStatusArchived
|
if err := s.wikiService.DeletePage(ctx, kbID, page.Slug); err != nil {
|
||||||
page.SourceRefs = remaining
|
logger.Warnf(ctx, "wiki cleanup: failed to delete page %s: %v", page.Slug, err)
|
||||||
if _, err := s.wikiService.UpdatePage(ctx, page); err != nil {
|
|
||||||
logger.Warnf(ctx, "wiki cleanup: failed to archive page %s: %v", page.Slug, err)
|
|
||||||
} else {
|
} else {
|
||||||
archivedSlugs = append(archivedSlugs, page.Slug)
|
deletedSlugs = append(deletedSlugs, page.Slug)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Other sources remain → remove source ref and queue LLM content retraction
|
// Other sources remain → remove source ref and queue LLM content retraction
|
||||||
page.SourceRefs = remaining
|
page.SourceRefs = remaining
|
||||||
if _, err := s.wikiService.UpdatePage(ctx, page); err != nil {
|
if err := s.wikiService.UpdatePageMeta(ctx, page); err != nil {
|
||||||
logger.Warnf(ctx, "wiki cleanup: failed to update source refs for page %s: %v", page.Slug, err)
|
logger.Warnf(ctx, "wiki cleanup: failed to update source refs for page %s: %v", page.Slug, err)
|
||||||
} else {
|
} else {
|
||||||
retractSlugs = append(retractSlugs, page.Slug)
|
retractSlugs = append(retractSlugs, page.Slug)
|
||||||
@@ -1315,9 +1313,9 @@ func (s *knowledgeService) cleanupWikiOnKnowledgeDelete(ctx context.Context, kbI
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(archivedSlugs) > 0 {
|
if len(deletedSlugs) > 0 {
|
||||||
logger.Infof(ctx, "wiki cleanup: archived %d pages after knowledge %s deletion: %v",
|
logger.Infof(ctx, "wiki cleanup: deleted %d pages after knowledge %s deletion: %v",
|
||||||
len(archivedSlugs), knowledgeID, archivedSlugs)
|
len(deletedSlugs), knowledgeID, deletedSlugs)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Enqueue async LLM retraction for multi-source pages
|
// Enqueue async LLM retraction for multi-source pages
|
||||||
@@ -1334,6 +1332,11 @@ func (s *knowledgeService) cleanupWikiOnKnowledgeDelete(ctx context.Context, kbI
|
|||||||
})
|
})
|
||||||
logger.Infof(ctx, "wiki cleanup: enqueued retract task for %d pages: %v", len(retractSlugs), retractSlugs)
|
logger.Infof(ctx, "wiki cleanup: enqueued retract task for %d pages: %v", len(retractSlugs), retractSlugs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Rebuild index page to reflect deleted pages
|
||||||
|
if len(deletedSlugs) > 0 {
|
||||||
|
s.rebuildWikiIndexSimple(ctx, kbID, docTitle, deletedSlugs)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// removeSourceRef removes entries from source_refs that match a knowledge ID.
|
// removeSourceRef removes entries from source_refs that match a knowledge ID.
|
||||||
@@ -1350,6 +1353,67 @@ func removeSourceRef(refs types.StringArray, knowledgeID string) types.StringArr
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// rebuildWikiIndexSimple regenerates the index page from current page listing
|
||||||
|
// and appends a deletion log entry. Does NOT use LLM — generates a simple
|
||||||
|
// structured listing. Used during document deletion when LLM is not available.
|
||||||
|
func (s *knowledgeService) rebuildWikiIndexSimple(ctx context.Context, kbID, docTitle string, deletedSlugs []string) {
|
||||||
|
// Rebuild index
|
||||||
|
resp, err := s.wikiService.ListPages(ctx, &types.WikiPageListRequest{
|
||||||
|
KnowledgeBaseID: kbID,
|
||||||
|
PageSize: 500,
|
||||||
|
SortBy: "page_type",
|
||||||
|
SortOrder: "asc",
|
||||||
|
})
|
||||||
|
if err != nil || resp == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var indexContent strings.Builder
|
||||||
|
indexContent.WriteString("# Wiki Index\n\n")
|
||||||
|
|
||||||
|
currentType := ""
|
||||||
|
for _, p := range resp.Pages {
|
||||||
|
if p.PageType == types.WikiPageTypeIndex || p.PageType == types.WikiPageTypeLog {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if p.Status == types.WikiPageStatusArchived {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if p.PageType != currentType {
|
||||||
|
currentType = p.PageType
|
||||||
|
label := currentType
|
||||||
|
if len(label) > 0 {
|
||||||
|
label = strings.ToUpper(label[:1]) + label[1:]
|
||||||
|
}
|
||||||
|
fmt.Fprintf(&indexContent, "\n## %s\n\n", label)
|
||||||
|
}
|
||||||
|
fmt.Fprintf(&indexContent, "- [[%s]] — %s\n", p.Slug, p.Summary)
|
||||||
|
}
|
||||||
|
|
||||||
|
indexPage, _ := s.wikiService.GetIndex(ctx, kbID)
|
||||||
|
if indexPage != nil {
|
||||||
|
indexPage.Content = indexContent.String()
|
||||||
|
if _, err := s.wikiService.UpdatePage(ctx, indexPage); err != nil {
|
||||||
|
logger.Warnf(ctx, "wiki cleanup: failed to rebuild index: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Append log entry
|
||||||
|
logPage, _ := s.wikiService.GetLog(ctx, kbID)
|
||||||
|
if logPage != nil {
|
||||||
|
entry := fmt.Sprintf("\n## [%s] delete | %s\n- **Pages deleted**: %d (%s)\n",
|
||||||
|
time.Now().UTC().Format("2006-01-02 15:04"),
|
||||||
|
docTitle,
|
||||||
|
len(deletedSlugs),
|
||||||
|
strings.Join(deletedSlugs, ", "),
|
||||||
|
)
|
||||||
|
logPage.Content = logPage.Content + entry
|
||||||
|
if _, err := s.wikiService.UpdatePage(ctx, logPage); err != nil {
|
||||||
|
logger.Warnf(ctx, "wiki cleanup: failed to update log: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// DeleteKnowledgeList deletes a knowledge entry and all related resources
|
// DeleteKnowledgeList deletes a knowledge entry and all related resources
|
||||||
func (s *knowledgeService) DeleteKnowledgeList(ctx context.Context, ids []string) error {
|
func (s *knowledgeService) DeleteKnowledgeList(ctx context.Context, ids []string) error {
|
||||||
if len(ids) == 0 {
|
if len(ids) == 0 {
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import (
|
|||||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/hibiken/asynq"
|
"github.com/hibiken/asynq"
|
||||||
|
"github.com/redis/go-redis/v9"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -22,6 +23,57 @@ const (
|
|||||||
maxContentForWiki = 32768
|
maxContentForWiki = 32768
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// wikiKBLock provides distributed per-KB locking for wiki operations using Redis.
|
||||||
|
// Ensures that concurrent ingest/retract tasks for the SAME knowledge base
|
||||||
|
// run sequentially across all instances, preventing race conditions on shared wiki pages.
|
||||||
|
// Different KBs can still run in parallel.
|
||||||
|
const (
|
||||||
|
wikiLockPrefix = "wiki:lock:"
|
||||||
|
wikiLockTTL = 30 * time.Minute // max time a single ingest/retract can hold the lock
|
||||||
|
wikiLockRetry = 500 * time.Millisecond
|
||||||
|
)
|
||||||
|
|
||||||
|
func acquireWikiLock(ctx context.Context, redisClient *redis.Client, kbID string) (string, error) {
|
||||||
|
if redisClient == nil {
|
||||||
|
return "", nil // No Redis (Lite mode) — no distributed lock, rely on single-instance
|
||||||
|
}
|
||||||
|
lockKey := wikiLockPrefix + kbID
|
||||||
|
lockValue := uuid.New().String()
|
||||||
|
|
||||||
|
deadline := time.Now().Add(wikiLockTTL) // don't wait longer than the lock TTL itself
|
||||||
|
for time.Now().Before(deadline) {
|
||||||
|
ok, err := redisClient.SetNX(ctx, lockKey, lockValue, wikiLockTTL).Result()
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("wiki lock: redis error: %w", err)
|
||||||
|
}
|
||||||
|
if ok {
|
||||||
|
return lockValue, nil
|
||||||
|
}
|
||||||
|
// Another task holds the lock — wait and retry
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return "", ctx.Err()
|
||||||
|
case <-time.After(wikiLockRetry):
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "", fmt.Errorf("wiki lock: timeout acquiring lock for KB %s", kbID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func releaseWikiLock(ctx context.Context, redisClient *redis.Client, kbID, lockValue string) {
|
||||||
|
if redisClient == nil || lockValue == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
lockKey := wikiLockPrefix + kbID
|
||||||
|
// Only release if we still own the lock (atomic check-and-delete via Lua)
|
||||||
|
script := redis.NewScript(`
|
||||||
|
if redis.call("GET", KEYS[1]) == ARGV[1] then
|
||||||
|
return redis.call("DEL", KEYS[1])
|
||||||
|
end
|
||||||
|
return 0
|
||||||
|
`)
|
||||||
|
script.Run(ctx, redisClient, []string{lockKey}, lockValue)
|
||||||
|
}
|
||||||
|
|
||||||
// WikiIngestPayload is the asynq task payload for wiki ingest
|
// WikiIngestPayload is the asynq task payload for wiki ingest
|
||||||
type WikiIngestPayload struct {
|
type WikiIngestPayload struct {
|
||||||
TenantID uint64 `json:"tenant_id"`
|
TenantID uint64 `json:"tenant_id"`
|
||||||
@@ -47,6 +99,7 @@ type wikiIngestService struct {
|
|||||||
chunkRepo interfaces.ChunkRepository
|
chunkRepo interfaces.ChunkRepository
|
||||||
modelService interfaces.ModelService
|
modelService interfaces.ModelService
|
||||||
task interfaces.TaskEnqueuer
|
task interfaces.TaskEnqueuer
|
||||||
|
redisClient *redis.Client // nil in Lite mode (no Redis)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewWikiIngestService creates a new wiki ingest service
|
// NewWikiIngestService creates a new wiki ingest service
|
||||||
@@ -56,6 +109,7 @@ func NewWikiIngestService(
|
|||||||
chunkRepo interfaces.ChunkRepository,
|
chunkRepo interfaces.ChunkRepository,
|
||||||
modelService interfaces.ModelService,
|
modelService interfaces.ModelService,
|
||||||
task interfaces.TaskEnqueuer,
|
task interfaces.TaskEnqueuer,
|
||||||
|
redisClient *redis.Client,
|
||||||
) interfaces.TaskHandler {
|
) interfaces.TaskHandler {
|
||||||
return &wikiIngestService{
|
return &wikiIngestService{
|
||||||
wikiService: wikiService,
|
wikiService: wikiService,
|
||||||
@@ -63,6 +117,7 @@ func NewWikiIngestService(
|
|||||||
chunkRepo: chunkRepo,
|
chunkRepo: chunkRepo,
|
||||||
modelService: modelService,
|
modelService: modelService,
|
||||||
task: task,
|
task: task,
|
||||||
|
redisClient: redisClient,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,8 +154,33 @@ func EnqueueWikiRetract(ctx context.Context, task interfaces.TaskEnqueuer, paylo
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle implements interfaces.TaskHandler for asynq task processing
|
// Handle implements interfaces.TaskHandler for asynq task processing.
|
||||||
|
// Acquires a distributed per-KB lock (Redis) to serialize wiki operations
|
||||||
|
// within the same knowledge base, preventing race conditions.
|
||||||
|
// Different KBs run fully in parallel.
|
||||||
func (s *wikiIngestService) Handle(ctx context.Context, t *asynq.Task) error {
|
func (s *wikiIngestService) Handle(ctx context.Context, t *asynq.Task) error {
|
||||||
|
// Extract KB ID from payload for locking
|
||||||
|
var kbID string
|
||||||
|
switch t.Type() {
|
||||||
|
case types.TypeWikiRetract:
|
||||||
|
var p WikiRetractPayload
|
||||||
|
if err := json.Unmarshal(t.Payload(), &p); err == nil {
|
||||||
|
kbID = p.KnowledgeBaseID
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
var p WikiIngestPayload
|
||||||
|
if err := json.Unmarshal(t.Payload(), &p); err == nil {
|
||||||
|
kbID = p.KnowledgeBaseID
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Acquire distributed lock for this KB (blocks until available or timeout)
|
||||||
|
lockValue, err := acquireWikiLock(ctx, s.redisClient, kbID)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("wiki: failed to acquire KB lock: %w", err)
|
||||||
|
}
|
||||||
|
defer releaseWikiLock(ctx, s.redisClient, kbID, lockValue)
|
||||||
|
|
||||||
switch t.Type() {
|
switch t.Type() {
|
||||||
case types.TypeWikiRetract:
|
case types.TypeWikiRetract:
|
||||||
return s.ProcessWikiRetract(ctx, t)
|
return s.ProcessWikiRetract(ctx, t)
|
||||||
@@ -198,6 +278,9 @@ func (s *wikiIngestService) ProcessWikiRetract(ctx context.Context, t *asynq.Tas
|
|||||||
KnowledgeBaseID: payload.KnowledgeBaseID,
|
KnowledgeBaseID: payload.KnowledgeBaseID,
|
||||||
}, payload.DocTitle+" [DELETED]", payload.PageSlugs, nil)
|
}, payload.DocTitle+" [DELETED]", payload.PageSlugs, nil)
|
||||||
|
|
||||||
|
// Clean up dead links pointing to archived/deleted pages
|
||||||
|
s.cleanDeadLinks(ctx, payload.KnowledgeBaseID)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -343,7 +426,11 @@ func (s *wikiIngestService) ProcessWikiIngest(ctx context.Context, t *asynq.Task
|
|||||||
// Step 5: Append to log page with synthesis suggestions
|
// Step 5: Append to log page with synthesis suggestions
|
||||||
s.appendLogEntry(ctx, payload, docTitle, pagesAffected, synthesisSuggestions)
|
s.appendLogEntry(ctx, payload, docTitle, pagesAffected, synthesisSuggestions)
|
||||||
|
|
||||||
// Step 6: Publish all draft pages created during this ingest
|
// Step 6: Cross-link injection — scan all affected pages and inject [[wiki-links]]
|
||||||
|
// for mentions of other wiki page titles. Pure text matching, no LLM call.
|
||||||
|
s.injectCrossLinks(ctx, payload.KnowledgeBaseID, pagesAffected)
|
||||||
|
|
||||||
|
// Step 7: Publish all draft pages created during this ingest
|
||||||
s.publishDraftPages(ctx, payload.KnowledgeBaseID, pagesAffected)
|
s.publishDraftPages(ctx, payload.KnowledgeBaseID, pagesAffected)
|
||||||
|
|
||||||
// Step 7: Handle stale pages — pages that previously referenced this document
|
// Step 7: Handle stale pages — pages that previously referenced this document
|
||||||
@@ -357,6 +444,195 @@ func (s *wikiIngestService) ProcessWikiIngest(ctx context.Context, t *asynq.Task
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// cleanDeadLinks removes [[wiki-links]] that point to archived or deleted pages.
|
||||||
|
// Scans all published pages, checks each out_link, and removes references to
|
||||||
|
// pages that no longer exist or are archived. No LLM call — pure text cleanup.
|
||||||
|
func (s *wikiIngestService) cleanDeadLinks(ctx context.Context, kbID string) {
|
||||||
|
resp, err := s.wikiService.ListPages(ctx, &types.WikiPageListRequest{
|
||||||
|
KnowledgeBaseID: kbID,
|
||||||
|
PageSize: 500,
|
||||||
|
})
|
||||||
|
if err != nil || resp == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build set of live (non-archived, non-system) slugs
|
||||||
|
liveSlugs := make(map[string]bool)
|
||||||
|
for _, p := range resp.Pages {
|
||||||
|
if p.Status != types.WikiPageStatusArchived {
|
||||||
|
liveSlugs[p.Slug] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var cleaned int
|
||||||
|
for _, p := range resp.Pages {
|
||||||
|
if p.Status == types.WikiPageStatusArchived {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if p.PageType == types.WikiPageTypeIndex || p.PageType == types.WikiPageTypeLog {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
content := p.Content
|
||||||
|
changed := false
|
||||||
|
|
||||||
|
// Find all [[slug]] references and remove dead ones
|
||||||
|
for _, outSlug := range p.OutLinks {
|
||||||
|
if liveSlugs[outSlug] {
|
||||||
|
continue // link is alive
|
||||||
|
}
|
||||||
|
// Dead link — remove the [[slug]] from content, keep the display text if any
|
||||||
|
linkPattern := "[[" + outSlug + "]]"
|
||||||
|
if strings.Contains(content, linkPattern) {
|
||||||
|
// Replace [[dead-slug]] with just the slug's readable part
|
||||||
|
parts := strings.Split(outSlug, "/")
|
||||||
|
readableName := parts[len(parts)-1]
|
||||||
|
readableName = strings.ReplaceAll(readableName, "-", " ")
|
||||||
|
content = strings.ReplaceAll(content, linkPattern, readableName)
|
||||||
|
changed = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if changed {
|
||||||
|
p.Content = content
|
||||||
|
if _, err := s.wikiService.UpdatePage(ctx, p); err != nil {
|
||||||
|
logger.Warnf(ctx, "wiki: failed to clean dead links in page %s: %v", p.Slug, err)
|
||||||
|
} else {
|
||||||
|
cleaned++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if cleaned > 0 {
|
||||||
|
logger.Infof(ctx, "wiki: cleaned dead links in %d pages", cleaned)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// injectCrossLinks scans affected pages and injects [[wiki-links]] for mentions
|
||||||
|
// of other wiki page titles in the content. Pure text replacement, no LLM call.
|
||||||
|
// Only processes entity/concept/synthesis/comparison pages (not index/log/summary).
|
||||||
|
func (s *wikiIngestService) injectCrossLinks(ctx context.Context, kbID string, affectedSlugs []string) {
|
||||||
|
// Build a title→slug lookup from ALL pages in this KB
|
||||||
|
resp, err := s.wikiService.ListPages(ctx, &types.WikiPageListRequest{
|
||||||
|
KnowledgeBaseID: kbID,
|
||||||
|
PageSize: 500,
|
||||||
|
})
|
||||||
|
if err != nil || resp == nil || len(resp.Pages) < 2 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
type pageRef struct {
|
||||||
|
slug string
|
||||||
|
title string
|
||||||
|
}
|
||||||
|
var allRefs []pageRef
|
||||||
|
for _, p := range resp.Pages {
|
||||||
|
if p.PageType == types.WikiPageTypeIndex || p.PageType == types.WikiPageTypeLog {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if p.Title != "" {
|
||||||
|
allRefs = append(allRefs, pageRef{slug: p.Slug, title: p.Title})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(allRefs) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sort by title length descending — match longer names first to avoid
|
||||||
|
// partial matches (e.g. "北京邮电大学" before "北京")
|
||||||
|
for i := 0; i < len(allRefs); i++ {
|
||||||
|
for j := i + 1; j < len(allRefs); j++ {
|
||||||
|
if len([]rune(allRefs[j].title)) > len([]rune(allRefs[i].title)) {
|
||||||
|
allRefs[i], allRefs[j] = allRefs[j], allRefs[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process only the pages we just created/updated
|
||||||
|
affectedSet := make(map[string]bool, len(affectedSlugs))
|
||||||
|
for _, s := range affectedSlugs {
|
||||||
|
affectedSet[s] = true
|
||||||
|
}
|
||||||
|
|
||||||
|
var updated int
|
||||||
|
for _, p := range resp.Pages {
|
||||||
|
if !affectedSet[p.Slug] {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// Skip system pages and summary (summary already has links from the prompt)
|
||||||
|
if p.PageType == types.WikiPageTypeIndex || p.PageType == types.WikiPageTypeLog || p.PageType == types.WikiPageTypeSummary {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
content := p.Content
|
||||||
|
changed := false
|
||||||
|
|
||||||
|
for _, ref := range allRefs {
|
||||||
|
// Don't self-link
|
||||||
|
if ref.slug == p.Slug {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// Skip if already linked with this slug
|
||||||
|
if strings.Contains(content, "[["+ref.slug+"]]") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// Replace title mentions with [[slug]] link
|
||||||
|
// Only replace if the title appears as a standalone term (not inside an existing link)
|
||||||
|
if strings.Contains(content, ref.title) {
|
||||||
|
// Replace first occurrence only (avoid over-linking)
|
||||||
|
content = replaceFirstOutsideLinks(content, ref.title, ref.title+" [["+ref.slug+"]]")
|
||||||
|
changed = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if changed {
|
||||||
|
p.Content = content
|
||||||
|
if _, err := s.wikiService.UpdatePage(ctx, p); err != nil {
|
||||||
|
logger.Warnf(ctx, "wiki ingest: cross-link injection failed for %s: %v", p.Slug, err)
|
||||||
|
} else {
|
||||||
|
updated++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if updated > 0 {
|
||||||
|
logger.Infof(ctx, "wiki ingest: injected cross-links in %d pages", updated)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// replaceFirstOutsideLinks replaces the first occurrence of `old` with `new` in s,
|
||||||
|
// but only if it's not already inside a [[...]] wiki link.
|
||||||
|
func replaceFirstOutsideLinks(s, old, newStr string) string {
|
||||||
|
idx := 0
|
||||||
|
for {
|
||||||
|
pos := strings.Index(s[idx:], old)
|
||||||
|
if pos < 0 {
|
||||||
|
return s // not found
|
||||||
|
}
|
||||||
|
absPos := idx + pos
|
||||||
|
|
||||||
|
// Check if this occurrence is inside a [[ ... ]] link
|
||||||
|
// Look backwards for [[ without encountering ]]
|
||||||
|
insideLink := false
|
||||||
|
for i := absPos - 1; i >= 0 && i >= absPos-200; i-- {
|
||||||
|
if i > 0 && s[i-1:i+1] == "]]" {
|
||||||
|
break // closed link before us
|
||||||
|
}
|
||||||
|
if i > 0 && s[i-1:i+1] == "[[" {
|
||||||
|
insideLink = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !insideLink {
|
||||||
|
return s[:absPos] + newStr + s[absPos+len(old):]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip this match, try next
|
||||||
|
idx = absPos + len(old)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// getExistingPageSlugsForKnowledge returns all page slugs that currently reference
|
// getExistingPageSlugsForKnowledge returns all page slugs that currently reference
|
||||||
// a given knowledge ID in their source_refs. Used to snapshot state before re-ingest.
|
// a given knowledge ID in their source_refs. Used to snapshot state before re-ingest.
|
||||||
func (s *wikiIngestService) getExistingPageSlugsForKnowledge(ctx context.Context, kbID, knowledgeID string) map[string]bool {
|
func (s *wikiIngestService) getExistingPageSlugsForKnowledge(ctx context.Context, kbID, knowledgeID string) map[string]bool {
|
||||||
@@ -438,16 +714,14 @@ func (s *wikiIngestService) retractStalePages(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(remaining) == 0 {
|
if len(remaining) == 0 {
|
||||||
// No other sources → archive
|
// No other sources → delete the page
|
||||||
page.Status = types.WikiPageStatusArchived
|
if err := s.wikiService.DeletePage(ctx, payload.KnowledgeBaseID, slug); err != nil {
|
||||||
page.SourceRefs = remaining
|
logger.Warnf(ctx, "wiki ingest: failed to delete stale page %s: %v", slug, err)
|
||||||
if _, err := s.wikiService.UpdatePage(ctx, page); err != nil {
|
|
||||||
logger.Warnf(ctx, "wiki ingest: failed to archive stale page %s: %v", slug, err)
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Multi-source → remove ref, queue retract
|
// Multi-source → remove ref, queue retract
|
||||||
page.SourceRefs = remaining
|
page.SourceRefs = remaining
|
||||||
if _, err := s.wikiService.UpdatePage(ctx, page); err != nil {
|
if err := s.wikiService.UpdatePageMeta(ctx, page); err != nil {
|
||||||
logger.Warnf(ctx, "wiki ingest: failed to update stale page %s: %v", slug, err)
|
logger.Warnf(ctx, "wiki ingest: failed to update stale page %s: %v", slug, err)
|
||||||
} else {
|
} else {
|
||||||
retractSlugs = append(retractSlugs, slug)
|
retractSlugs = append(retractSlugs, slug)
|
||||||
@@ -773,7 +1047,7 @@ func (s *wikiIngestService) publishDraftPages(ctx context.Context, kbID string,
|
|||||||
}
|
}
|
||||||
if page.Status == types.WikiPageStatusDraft {
|
if page.Status == types.WikiPageStatusDraft {
|
||||||
page.Status = types.WikiPageStatusPublished
|
page.Status = types.WikiPageStatusPublished
|
||||||
if _, err := s.wikiService.UpdatePage(ctx, page); err != nil {
|
if err := s.wikiService.UpdatePageMeta(ctx, page); err != nil {
|
||||||
logger.Warnf(ctx, "wiki ingest: failed to publish page %s: %v", slug, err)
|
logger.Warnf(ctx, "wiki ingest: failed to publish page %s: %v", slug, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ func (s *wikiPageService) UpdatePage(ctx context.Context, page *types.WikiPage)
|
|||||||
|
|
||||||
oldOutLinks := existing.OutLinks
|
oldOutLinks := existing.OutLinks
|
||||||
|
|
||||||
// Update fields
|
// Update fields (version is incremented by the repository's optimistic lock)
|
||||||
existing.Title = page.Title
|
existing.Title = page.Title
|
||||||
existing.Content = page.Content
|
existing.Content = page.Content
|
||||||
existing.Summary = page.Summary
|
existing.Summary = page.Summary
|
||||||
@@ -95,7 +95,6 @@ func (s *wikiPageService) UpdatePage(ctx context.Context, page *types.WikiPage)
|
|||||||
existing.SourceRefs = page.SourceRefs
|
existing.SourceRefs = page.SourceRefs
|
||||||
existing.PageMetadata = page.PageMetadata
|
existing.PageMetadata = page.PageMetadata
|
||||||
existing.Status = page.Status
|
existing.Status = page.Status
|
||||||
existing.Version++
|
|
||||||
existing.UpdatedAt = time.Now()
|
existing.UpdatedAt = time.Now()
|
||||||
|
|
||||||
// Re-parse outbound links
|
// Re-parse outbound links
|
||||||
@@ -117,6 +116,12 @@ func (s *wikiPageService) UpdatePage(ctx context.Context, page *types.WikiPage)
|
|||||||
return existing, nil
|
return existing, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpdatePageMeta updates only metadata (status, source_refs) without version bump or link re-parse.
|
||||||
|
func (s *wikiPageService) UpdatePageMeta(ctx context.Context, page *types.WikiPage) error {
|
||||||
|
page.UpdatedAt = time.Now()
|
||||||
|
return s.repo.UpdateMeta(ctx, page)
|
||||||
|
}
|
||||||
|
|
||||||
// GetPageBySlug retrieves a wiki page by its slug
|
// GetPageBySlug retrieves a wiki page by its slug
|
||||||
func (s *wikiPageService) GetPageBySlug(ctx context.Context, kbID string, slug string) (*types.WikiPage, error) {
|
func (s *wikiPageService) GetPageBySlug(ctx context.Context, kbID string, slug string) (*types.WikiPage, error) {
|
||||||
return s.repo.GetBySlug(ctx, kbID, slug)
|
return s.repo.GetBySlug(ctx, kbID, slug)
|
||||||
@@ -325,10 +330,10 @@ func (s *wikiPageService) RebuildLinks(ctx context.Context, kbID string) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save all pages
|
// Save all pages (link rebuild is metadata-only, no version bump)
|
||||||
for _, p := range pages {
|
for _, p := range pages {
|
||||||
p.UpdatedAt = time.Now()
|
p.UpdatedAt = time.Now()
|
||||||
if err := s.repo.Update(ctx, p); err != nil {
|
if err := s.repo.UpdateMeta(ctx, p); err != nil {
|
||||||
logger.Warnf(ctx, "wiki: failed to update links for page %s: %v", p.Slug, err)
|
logger.Warnf(ctx, "wiki: failed to update links for page %s: %v", p.Slug, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -383,7 +388,7 @@ func (s *wikiPageService) updateInLinks(ctx context.Context, kbID string, source
|
|||||||
if !containsString(targetPage.InLinks, sourceSlug) {
|
if !containsString(targetPage.InLinks, sourceSlug) {
|
||||||
targetPage.InLinks = append(targetPage.InLinks, sourceSlug)
|
targetPage.InLinks = append(targetPage.InLinks, sourceSlug)
|
||||||
targetPage.UpdatedAt = time.Now()
|
targetPage.UpdatedAt = time.Now()
|
||||||
if err := s.repo.Update(ctx, targetPage); err != nil {
|
if err := s.repo.UpdateMeta(ctx, targetPage); err != nil {
|
||||||
logger.Warnf(ctx, "wiki: failed to update in_links for %s: %v", targetSlug, err)
|
logger.Warnf(ctx, "wiki: failed to update in_links for %s: %v", targetSlug, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -401,7 +406,7 @@ func (s *wikiPageService) removeInLinks(ctx context.Context, kbID string, source
|
|||||||
if len(newInLinks) != len(targetPage.InLinks) {
|
if len(newInLinks) != len(targetPage.InLinks) {
|
||||||
targetPage.InLinks = newInLinks
|
targetPage.InLinks = newInLinks
|
||||||
targetPage.UpdatedAt = time.Now()
|
targetPage.UpdatedAt = time.Now()
|
||||||
if err := s.repo.Update(ctx, targetPage); err != nil {
|
if err := s.repo.UpdateMeta(ctx, targetPage); err != nil {
|
||||||
logger.Warnf(ctx, "wiki: failed to update in_links for %s: %v", targetSlug, err)
|
logger.Warnf(ctx, "wiki: failed to update in_links for %s: %v", targetSlug, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,8 +16,13 @@ type WikiPageService interface {
|
|||||||
|
|
||||||
// UpdatePage updates an existing wiki page, re-parses links,
|
// UpdatePage updates an existing wiki page, re-parses links,
|
||||||
// updates bidirectional references, increments version, and re-syncs chunks.
|
// updates bidirectional references, increments version, and re-syncs chunks.
|
||||||
|
// Use for content changes visible to the user.
|
||||||
UpdatePage(ctx context.Context, page *types.WikiPage) (*types.WikiPage, error)
|
UpdatePage(ctx context.Context, page *types.WikiPage) (*types.WikiPage, error)
|
||||||
|
|
||||||
|
// UpdatePageMeta updates only metadata (status, source_refs) without
|
||||||
|
// incrementing version or re-parsing links. Use for publish/archive/source ref changes.
|
||||||
|
UpdatePageMeta(ctx context.Context, page *types.WikiPage) error
|
||||||
|
|
||||||
// GetPageBySlug retrieves a wiki page by its slug within a knowledge base.
|
// GetPageBySlug retrieves a wiki page by its slug within a knowledge base.
|
||||||
GetPageBySlug(ctx context.Context, kbID string, slug string) (*types.WikiPage, error)
|
GetPageBySlug(ctx context.Context, kbID string, slug string) (*types.WikiPage, error)
|
||||||
|
|
||||||
@@ -56,9 +61,12 @@ type WikiPageRepository interface {
|
|||||||
// Create inserts a new wiki page record.
|
// Create inserts a new wiki page record.
|
||||||
Create(ctx context.Context, page *types.WikiPage) error
|
Create(ctx context.Context, page *types.WikiPage) error
|
||||||
|
|
||||||
// Update updates an existing wiki page record.
|
// Update updates an existing wiki page record (increments version — content changes).
|
||||||
Update(ctx context.Context, page *types.WikiPage) error
|
Update(ctx context.Context, page *types.WikiPage) error
|
||||||
|
|
||||||
|
// UpdateMeta updates only metadata fields (links, status, source_refs) without version bump.
|
||||||
|
UpdateMeta(ctx context.Context, page *types.WikiPage) error
|
||||||
|
|
||||||
// GetByID retrieves a wiki page by its unique ID.
|
// GetByID retrieves a wiki page by its unique ID.
|
||||||
GetByID(ctx context.Context, id string) (*types.WikiPage, error)
|
GetByID(ctx context.Context, id string) (*types.WikiPage, error)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user