Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 | 7x 7x 7x 7x 7x 12x 7x 8x 7x 7x 5x 5x 2x 2x 2x 2x 2x 2x 1x 2x 2x 2x 5x 3x 2x 2x 2x 1x 1x 2x 2x 5x 1x 1x 2x 1x 7x 7x 7x 7x 2x 2x 2x 2x 2x 2x 7x 7x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 2x 2x 2x 1x 1x | import { GeneratedMark } from "@/components/badge/GeneratedMark";
import {
agentIcon,
repositoryIcon,
} from "@/components/appearance/icons";
import { TONES, Tone } from "@/components/appearance/tones";
import { sourceIcon } from "@/apps/user/pages/knowledge/util";
import {
NetworkEdge,
NetworkEdgeKind,
NetworkGraph,
NetworkNode,
NetworkNodeKind,
} from "@/objects/Network";
import { chatMessageHref, resourceHref } from "@/routes/resourceHref";
import { parseServerDateOrNull, timeAgo } from "@/util/time";
import { Edge, Handle, Node, NodeProps, Position } from "@xyflow/react";
import { ArrowUpRight, Brain, MessageSquare, NotebookPen } from "lucide-react";
import React, { useMemo, useState } from "react";
import { Link } from "react-router-dom";
import FlowFrame from "./FlowFrame";
import { drawnEnds, layeredLayout, lineageOf } from "./layout";
/**
* One network tile's graph: typed nodes in columns, left to right.
*
* Shared by the knowledge network and the memory graph, which differ only in
* which kinds they hold. Each kind has a column, a glyph and a tone here, and
* a line of figures from its `meta`; the backend decides what the nodes are
* (`app/services/network/`). Selecting a node lights its lineage — what it
* reaches and what reaches it — and names it, with a link, beneath the canvas.
*/
// ─── Kinds ────────────────────────────────────────────────────────────────────
/** Memory sits left of its agent — what the agent carries — and everything
* it reaches runs right. */
const COLUMN: Record<NetworkNodeKind, number> = {
memory: 0,
agent: 1,
repository: 2,
conversation: 2,
knowledge: 3,
memory_entry: 3,
};
const KIND_TONE: Record<NetworkNodeKind, Tone> = {
agent: "violet",
repository: "indigo",
knowledge: "sky",
memory: "amber",
conversation: "emerald",
memory_entry: "slate",
};
const EDGE_TONE: Record<NetworkEdgeKind, Tone> = {
grant: "violet",
member: "indigo",
memory: "amber",
conversation: "emerald",
entry: "slate",
};
const KIND_NOUN: Record<NetworkNodeKind, string> = {
agent: "Agent",
repository: "Repository",
knowledge: "Source",
memory: "Memory",
conversation: "Conversation",
memory_entry: "Memory entry",
};
const num = (meta: Record<string, unknown>, key: string): number | null =>
typeof meta[key] === "number" ? (meta[key] as number) : null;
const plural = (n: number, one: string, many = `${one}s`) =>
`${n.toLocaleString()} ${n === 1 ? one : many}`;
const ago = (value: unknown): string | null => {
const date = typeof value === "string" ? parseServerDateOrNull(value) : null;
return date ? timeAgo(date) : null;
};
const ITEM_NOUN: Record<string, [string, string]> = {
web: ["page", "pages"],
graph: ["entity", "entities"],
};
/** The figures under a node's name. */
export function nodeFigures(node: NetworkNode): string {
const m = node.meta ?? {};
switch (node.kind) {
case "agent": {
const parts: string[] = [];
const sources = num(m, "sources");
Eif (sources !== null) parts.push(plural(sources, "source"));
const memories = num(m, "memory_entries");
if (memories !== null) parts.push(plural(memories, "memory", "memories"));
return parts.join(" · ");
}
case "repository":
return [
plural(num(m, "sources") ?? 0, "source"),
plural(num(m, "agents") ?? 0, "agent"),
].join(" · ");
case "knowledge": {
const [one, many] = ITEM_NOUN[String(m.source_type)] ?? [
"document",
"documents",
];
const agents = num(m, "agents") ?? 0;
return [
plural(num(m, "items") ?? 0, one, many),
agents ? plural(agents, "agent") : "no agent",
].join(" · ");
}
case "memory":
return [
plural(num(m, "conversations") ?? 0, "conversation"),
ago(m.last_written_at),
]
.filter(Boolean)
.join(" · ");
case "conversation":
return [
plural(num(m, "entries") ?? 0, "memory", "memories"),
ago(m.last_written_at),
]
.filter(Boolean)
.join(" · ");
case "memory_entry":
return ago(m.created_at) ?? "";
default:
return "";
}
}
/** Faint: a node that is on the graph but reaches or is reached by nothing
* — an unreachable source, a deleted conversation. */
export function isFaint(node: NetworkNode): boolean {
if (node.kind === "knowledge") return (num(node.meta, "agents") ?? 0) === 0;
if (node.kind === "conversation") return node.meta?.deleted === true;
return false;
}
function iconFor(node: NetworkNode): React.ElementType {
const m = node.meta ?? {};
switch (node.kind) {
case "agent":
return agentIcon(m.icon as string | null);
case "repository":
return repositoryIcon(m.icon as string | null);
case "knowledge":
return sourceIcon(String(m.source_type ?? ""));
case "memory":
return Brain;
case "conversation":
return MessageSquare;
default:
return NotebookPen;
}
}
function toneFor(node: NetworkNode): Tone {
const accent = node.meta?.accent as Tone | undefined;
return accent && accent in TONES ? accent : KIND_TONE[node.kind];
}
/** Where a node opens, or null when it is not a page of its own. */
export function nodeHref(node: NetworkNode): string | null {
switch (node.kind) {
case "agent":
case "memory":
return node.ref_id ? resourceHref("agent", node.ref_id) : null;
case "repository":
return node.ref_id
? resourceHref("knowledge_repository", node.ref_id)
: null;
case "knowledge":
return node.ref_id ? resourceHref("knowledge", node.ref_id) : null;
case "conversation":
return chatMessageHref({
agentId: node.meta?.agent_id as string | undefined,
conversationId: node.ref_id,
});
default:
return null;
}
}
// ─── Node ─────────────────────────────────────────────────────────────────────
export const NODE_W = 184;
const NODE_H = 44;
interface NetworkNodeData extends Record<string, unknown> {
node: NetworkNode;
dimmed: boolean;
}
type NetworkFlowNode = Node<NetworkNodeData, "network">;
const hidden = "!opacity-0 !pointer-events-none !min-w-0 !min-h-0 !size-px";
const NetworkNodeView: React.FC<NodeProps<NetworkFlowNode>> = ({
data,
selected,
}) => {
const { node, dimmed } = data;
const tone = TONES[toneFor(node)];
const Icon = iconFor(node);
const faint = isFaint(node);
const shared = node.kind === "knowledge" && (num(node.meta, "agents") ?? 0) > 1;
return (
<div
style={{ width: NODE_W, height: NODE_H }}
className={`flex items-center gap-2 rounded-lg border bg-white dark:bg-gray-800 px-2 shadow-sm transition-opacity ${
faint
? "border-dashed border-gray-300 dark:border-gray-600"
: tone.border
} ${dimmed ? "opacity-25" : faint ? "opacity-70" : "opacity-100"} ${
selected
? "ring-2 ring-offset-1 ring-gray-900 dark:ring-gray-100 dark:ring-offset-gray-900"
: ""
}`}
>
<Handle type="target" position={Position.Left} className={hidden} />
<GeneratedMark generated={node.meta?.is_ai_generated === true} size="sm">
<div className={`shrink-0 rounded-md p-1 ${tone.bg}`}>
<Icon className={`size-3.5 ${tone.fg}`} />
</div>
</GeneratedMark>
<div className="min-w-0 flex-1 leading-tight">
<div className="flex items-center gap-1">
<span className="truncate text-[11px] font-medium text-gray-800 dark:text-gray-100">
{node.label}
</span>
{shared && (
<span className="shrink-0 rounded bg-sky-100 dark:bg-sky-900/40 px-1 text-[9px] font-semibold text-sky-700 dark:text-sky-300">
×{num(node.meta, "agents")}
</span>
)}
</div>
<div className="truncate text-[10px] text-gray-400 dark:text-gray-500">
{nodeFigures(node)}
</div>
</div>
<Handle type="source" position={Position.Right} className={hidden} />
</div>
);
};
const nodeTypes = { network: NetworkNodeView };
// ─── Canvas ───────────────────────────────────────────────────────────────────
export interface NetworkCanvasProps {
graph: NetworkGraph<unknown>;
/** What an empty graph means, in the tile's own words. */
empty: React.ReactNode;
testId?: string;
}
const NetworkCanvas: React.FC<NetworkCanvasProps> = ({
graph,
empty,
testId,
}) => {
const [selectedId, setSelectedId] = useState<string | null>(null);
const positions = useMemo(
() =>
layeredLayout(
graph.nodes.map((n) => ({ id: n.id, column: COLUMN[n.kind] ?? 3 })),
graph.edges,
{ anchor: COLUMN.agent },
),
[graph],
);
const lit = useMemo(
() => (selectedId ? lineageOf(selectedId, graph.edges) : null),
[graph, selectedId],
);
const flowNodes: NetworkFlowNode[] = useMemo(
() =>
graph.nodes.map((node) => {
const at = positions[node.id] ?? { x: 0, y: 0 };
return {
id: node.id,
type: "network",
position: { x: at.x, y: at.y - NODE_H / 2 },
selected: node.id === selectedId,
ariaLabel: `${KIND_NOUN[node.kind]}: ${node.label}`,
data: { node, dimmed: !!lit && !lit.has(node.id) },
};
}),
[graph, positions, lit, selectedId],
);
const flowEdges: Edge[] = useMemo(
() =>
graph.edges.map((edge: NetworkEdge) => {
const on = !lit || (lit.has(edge.source) && lit.has(edge.target));
const [source, target] = drawnEnds(edge, positions);
return {
id: `${edge.source}->${edge.target}`,
source,
target,
selectable: false,
focusable: false,
style: {
stroke: TONES[EDGE_TONE[edge.kind] ?? "slate"].hex,
strokeWidth: edge.kind === "grant" ? 1.75 : 1.25,
strokeDasharray: edge.kind === "memory" ? "5 4" : undefined,
opacity: on ? 0.8 : 0.1,
},
};
}),
[graph, lit, positions],
);
const selected = graph.nodes.find((n) => n.id === selectedId) ?? null;
const href = selected ? nodeHref(selected) : null;
if (graph.nodes.length === 0) {
return (
<p className="text-xs text-gray-400 dark:text-gray-500 italic">{empty}</p>
);
}
return (
<div className="flex h-full min-h-0 flex-col gap-2">
<FlowFrame<NetworkFlowNode>
testId={testId}
nodes={flowNodes}
edges={flowEdges}
nodeTypes={nodeTypes}
onNodeClick={(_, node) =>
setSelectedId((id) => (id === node.id ? null : node.id))
}
onPaneClick={() => setSelectedId(null)}
/>
<div className="flex min-h-[1.5rem] items-center gap-2 text-[11px] text-gray-500 dark:text-gray-400">
{selected ? (
<>
<span className="shrink-0 font-medium text-gray-400 dark:text-gray-500">
{KIND_NOUN[selected.kind]}
</span>
<span className="min-w-0 truncate text-gray-700 dark:text-gray-200">
{selected.label}
</span>
{href && (
<Link
to={href}
className="ml-auto inline-flex shrink-0 items-center gap-0.5 font-medium text-blue-600 dark:text-blue-400 hover:underline"
>
Open <ArrowUpRight className="size-3" />
</Link>
)}
</>
) : (
<span className="italic text-gray-400 dark:text-gray-500">
Select a node to trace what it reaches and what reaches it.
</span>
)}
</div>
</div>
);
};
export default NetworkCanvas;
|