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 | 6x 5x 6x 1x 6x 4x 6x 20x 3x 3x 3x 3x 3x 3x 3x 3x 6x 2x 2x 2x 2x 2x 2x 1x 2x 2x 1x 1x 3x 2x 3x 1x 2x 6x 2x 2x 2x 2x 2x 1x 2x 2x 1x 1x 9x 2x 2x 7x 5x 1x 7x 6x 5x 5x 2x 5x 5x 6x 1x 1x 1x 1x 6x 1x 6x 6x 5x 5x 5x 5x 5x 5x 1x 5x 1x 5x 5x 5x 2x 1x | import WebLinkGraph from "@/apps/user/pages/knowledge/WebLinkGraph";
import {
AgentMemoryConfig,
KnowledgeGraphConfig,
KnowledgeNetworkConfig,
WebLinkGraphConfig,
} from "@/objects/Dashboard";
import { GraphData, Knowledge, NodeTypeDefinition } from "@/objects/Knowledge";
import {
KnowledgeNetworkSummary,
MemoryGraphSummary,
NetworkGraph,
} from "@/objects/Network";
import { APP_ROUTES } from "@/routes/const";
import { resourceHref } from "@/routes/resourceHref";
import { QUERY_KEYS } from "@/services/query_keys";
import { useApiHandler } from "@/services/useApiHandler";
import { serverTime } from "@/util/time";
import { useQuery } from "@tanstack/react-query";
import { Loader2 } from "lucide-react";
import React from "react";
import { Link } from "react-router-dom";
import EntityGraph from "./network/EntityGraph";
import NetworkCanvas from "./network/NetworkCanvas";
import { WidgetBodyProps } from "./types";
/**
* The network tiles: how agents, knowledge and memory connect.
*
* All four read stored records as the viewer (`data_kind: platform`). A tile
* that draws one source may leave its id empty; it then draws the newest
* source of its kind and its caption says so, rather than asking the person
* who added it from a template to go and find an id.
*/
const Spinner = () => (
<div className="h-full flex items-center justify-center text-gray-300 dark:text-gray-600">
<Loader2 className="w-4 h-4 animate-spin" />
</div>
);
const Note: React.FC<{ children: React.ReactNode }> = ({ children }) => (
<p className="text-xs text-gray-400 dark:text-gray-500 italic">{children}</p>
);
const Caption: React.FC<{ children: React.ReactNode }> = ({ children }) => (
<p className="mb-2 text-[11px] leading-snug text-gray-500 dark:text-gray-400">
{children}
</p>
);
const count = (n: number, one: string, many = `${one}s`) =>
`${n.toLocaleString()} ${n === 1 ? one : many}`;
// ─── Knowledge network ────────────────────────────────────────────────────────
/** One line saying what the network found. The two findings — sources several
* agents share, and sources no agent reaches — lead, because they are what
* the picture is for. */
export function knowledgeNetworkCaption(s: KnowledgeNetworkSummary): string {
const parts = [
count(s.agents, "agent"),
count(s.repositories, "repository", "repositories"),
count(s.sources, "source"),
];
const findings: string[] = [];
Eif (s.shared_sources) findings.push(`${s.shared_sources} shared`);
Eif (s.unreachable_sources)
findings.push(`${s.unreachable_sources} reached by no agent`);
Iif (s.agents_without_knowledge)
findings.push(
`${count(s.agents_without_knowledge, "agent")} with no knowledge`,
);
const memory =
s.memory_entries === null
? null
: count(s.memory_entries, "memory", "memories");
return [
parts.join(" · "),
findings.length ? ` — ${findings.join(", ")}` : "",
memory ? ` · ${memory}` : "",
].join("");
}
export const KnowledgeNetworkWidgetBody: React.FC<WidgetBodyProps> = ({
widget,
}) => {
const { getApiHandler } = useApiHandler();
const config = widget.config as unknown as KnowledgeNetworkConfig;
const agentIds = config?.agent_ids ?? [];
const includeMemory = config?.include_memory ?? true;
const includeUnconnected = config?.include_unconnected ?? true;
const { data, isLoading } = useQuery({
queryKey: [
QUERY_KEYS.GET.NETWORK_KNOWLEDGE,
agentIds.join(","),
includeMemory,
includeUnconnected,
],
queryFn: () =>
getApiHandler().network_router.getKnowledgeNetwork({
agentIds,
includeMemory,
includeUnconnected,
}),
});
// Bound explicitly: TS 5.0.2 cannot infer react-query v5's generic here.
const graph = (data as NetworkGraph<KnowledgeNetworkSummary> | null) ?? null;
if (isLoading) return <Spinner />;
Iif (!graph) return <Note>Could not load the knowledge network.</Note>;
return (
<div className="flex h-full min-h-0 flex-col">
<Caption>
{knowledgeNetworkCaption(graph.summary)}
{graph.truncated && " · large: the most-connected are drawn"}
</Caption>
<NetworkCanvas
graph={graph}
testId="knowledge-network"
empty={
<>
No agents or knowledge yet.{" "}
<Link
to={APP_ROUTES.USER.knowledge}
className="not-italic text-blue-600 dark:text-blue-400 hover:underline"
>
Add a knowledge source
</Link>{" "}
and grant its repository to an agent to see how they connect.
</>
}
/>
</div>
);
};
// ─── Memory graph ─────────────────────────────────────────────────────────────
export function memoryGraphCaption(s: MemoryGraphSummary): string {
if (!s.memory_available) return "Memory could not be read just now.";
const parts = [
count(s.agents, "agent"),
count(s.conversations, "conversation"),
count(s.entries ?? 0, "memory", "memories"),
];
if (s.deleted_conversations)
parts.push(
`${count(s.deleted_conversations, "deleted conversation")} still remembered`,
);
return parts.join(" · ");
}
export const AgentMemoryWidgetBody: React.FC<WidgetBodyProps> = ({
widget,
}) => {
const { getApiHandler } = useApiHandler();
const config = widget.config as unknown as AgentMemoryConfig;
const agentId = config?.agent_id ?? "";
const recent = config?.recent ?? 20;
const { data, isLoading } = useQuery({
queryKey: [QUERY_KEYS.GET.NETWORK_MEMORY, agentId, recent],
queryFn: () =>
getApiHandler().network_router.getMemoryGraph({
agentIds: agentId ? [agentId] : [],
recent,
}),
});
const graph = (data as NetworkGraph<MemoryGraphSummary> | null) ?? null;
if (isLoading) return <Spinner />;
Iif (!graph) return <Note>Could not load the memory graph.</Note>;
return (
<div className="flex h-full min-h-0 flex-col">
<Caption>
{memoryGraphCaption(graph.summary)}
{graph.truncated && " · older conversations folded"}
</Caption>
<NetworkCanvas
graph={graph}
testId="memory-graph"
empty={
graph.summary.memory_available
? "Nothing remembered yet. An agent writes a memory after each completed chat turn."
: "Memory is unavailable right now — try refreshing in a moment."
}
/>
</div>
);
};
// ─── Single-source graphs ─────────────────────────────────────────────────────
/** The source a tile draws: the one it names, or the newest of its kind. */
export function resolveSource(
sources: Knowledge[],
sourceType: string,
knowledgeId: string,
): { source: Knowledge | null; defaulted: boolean } {
if (knowledgeId) {
return {
source: sources.find((k) => k.id === knowledgeId) ?? null,
defaulted: false,
};
}
const newest = sources
.filter((k) => k.source_type === sourceType)
.sort((a, b) => serverTime(b.created_at) - serverTime(a.created_at))[0];
return { source: newest ?? null, defaulted: true };
}
const useSource = (sourceType: string, knowledgeId: string) => {
const { getApiHandler } = useApiHandler();
const { data, isLoading } = useQuery({
queryKey: [QUERY_KEYS.GET.KNOWLEDGE],
queryFn: () => getApiHandler().knowledge_router.list(),
});
const sources = (data as Knowledge[] | null) ?? [];
return { isLoading, ...resolveSource(sources, sourceType, knowledgeId) };
};
const SourceCaption: React.FC<{
source: Knowledge | null;
knowledgeId: string;
defaulted: boolean;
}> = ({ source, knowledgeId, defaulted }) => {
const id = source?.id ?? knowledgeId;
const href = id ? resourceHref("knowledge", id) : null;
const name = source?.name ?? "This source";
return (
<Caption>
{defaulted ? "Newest: " : ""}
{href ? (
<Link
to={href}
className="font-medium text-gray-700 dark:text-gray-200 hover:underline"
>
{name}
</Link>
) : (
name
)}
</Caption>
);
};
const NoSource: React.FC<{ kind: string }> = ({ kind }) => (
<Note>
No {kind} source yet.{" "}
<Link
to={APP_ROUTES.USER.knowledge}
className="not-italic text-blue-600 dark:text-blue-400 hover:underline"
>
Add one under Knowledge
</Link>{" "}
and it will appear here.
</Note>
);
export const WebLinkGraphWidgetBody: React.FC<WidgetBodyProps> = ({
widget,
}) => {
const config = widget.config as unknown as WebLinkGraphConfig;
const knowledgeId = config?.knowledge_id ?? "";
const { source, defaulted, isLoading } = useSource("web", knowledgeId);
const id = source?.id ?? knowledgeId;
if (isLoading) return <Spinner />;
if (!id) return <NoSource kind="website" />;
return (
<div className="flex h-full min-h-0 flex-col">
<SourceCaption
source={source}
knowledgeId={knowledgeId}
defaulted={defaulted}
/>
<div className="min-h-0 flex-1">
<WebLinkGraph
key={`${id}:${config?.mode}`}
knowledgeId={id}
embedded
initialMode={config?.mode === "all" ? "all" : "paths"}
/>
</div>
</div>
);
};
export const KnowledgeGraphWidgetBody: React.FC<WidgetBodyProps> = ({
widget,
}) => {
const { getApiHandler } = useApiHandler();
const config = widget.config as unknown as KnowledgeGraphConfig;
const knowledgeId = config?.knowledge_id ?? "";
const { source, defaulted, isLoading } = useSource("graph", knowledgeId);
const id = source?.id ?? knowledgeId;
const graphQuery = useQuery({
queryKey: [QUERY_KEYS.GET.KNOWLEDGE_GRAPH, id],
queryFn: () => getApiHandler().knowledge_router.getGraph(id),
enabled: !!id,
});
const typesQuery = useQuery({
queryKey: [QUERY_KEYS.GET.KNOWLEDGE_NODE_TYPES],
queryFn: () => getApiHandler().knowledge_router.getNodeTypes(),
enabled: !!id,
});
const graph = (graphQuery.data as GraphData | null) ?? null;
const types = (typesQuery.data as NodeTypeDefinition[] | null) ?? [];
if (isLoading || (id && graphQuery.isLoading)) return <Spinner />;
if (!id) return <NoSource kind="graph" />;
return (
<div className="flex h-full min-h-0 flex-col">
<SourceCaption
source={source}
knowledgeId={knowledgeId}
defaulted={defaulted}
/>
{!graph ? (
<Note>Could not load this graph.</Note>
) : graph.nodes.length === 0 ? (
<Note>This graph has no entities yet.</Note>
) : (
<>
<Caption>
{count(graph.nodes.length, "entity", "entities")} ·{" "}
{count(graph.edges.length, "relationship")}
</Caption>
<div className="min-h-0 flex-1">
<EntityGraph graph={graph} nodeTypes={types} />
</div>
</>
)}
</div>
);
};
|