99 lines
2.8 KiB
TypeScript
99 lines
2.8 KiB
TypeScript
import { DirectedGraph } from "graphology";
|
|
|
|
export function createLinearGraph(
|
|
nodes: string[],
|
|
edgeType: string = "sequential",
|
|
): DirectedGraph {
|
|
const graph = new DirectedGraph();
|
|
for (const node of nodes) {
|
|
graph.addNode(node, { name: node });
|
|
}
|
|
for (let i = 0; i < nodes.length - 1; i++) {
|
|
const source = nodes[i]!;
|
|
const target = nodes[i + 1]!;
|
|
graph.addEdgeWithKey(`${source}->${target}`, source, target, { edgeType });
|
|
}
|
|
return graph;
|
|
}
|
|
|
|
export function createDiamondGraph(
|
|
top: string,
|
|
left: string,
|
|
right: string,
|
|
bottom: string,
|
|
edgeType: string = "sequential",
|
|
): DirectedGraph {
|
|
const graph = new DirectedGraph();
|
|
graph.addNode(top, { name: top });
|
|
graph.addNode(left, { name: left });
|
|
graph.addNode(right, { name: right });
|
|
graph.addNode(bottom, { name: bottom });
|
|
graph.addEdgeWithKey(`${top}->${left}`, top, left, { edgeType });
|
|
graph.addEdgeWithKey(`${top}->${right}`, top, right, { edgeType });
|
|
graph.addEdgeWithKey(`${left}->${bottom}`, left, bottom, { edgeType });
|
|
graph.addEdgeWithKey(`${right}->${bottom}`, right, bottom, { edgeType });
|
|
return graph;
|
|
}
|
|
|
|
export function createOperationGraph(
|
|
specs: Array<{
|
|
name: string;
|
|
namespace?: string;
|
|
version?: string;
|
|
type?: string;
|
|
inputSchema?: Record<string, unknown>;
|
|
outputSchema?: Record<string, unknown>;
|
|
}>,
|
|
edges: Array<{ source: string; target: string; compatible?: boolean; detail?: string }>,
|
|
): DirectedGraph {
|
|
const graph = new DirectedGraph();
|
|
for (const spec of specs) {
|
|
const key = spec.namespace ? `${spec.namespace}.${spec.name}` : spec.name;
|
|
graph.addNode(key, {
|
|
name: spec.name,
|
|
namespace: spec.namespace ?? "test",
|
|
version: spec.version ?? "1.0.0",
|
|
type: spec.type ?? "query",
|
|
inputSchema: spec.inputSchema ?? {},
|
|
outputSchema: spec.outputSchema ?? {},
|
|
});
|
|
}
|
|
for (const edge of edges) {
|
|
graph.addEdgeWithKey(`${edge.source}->${edge.target}`, edge.source, edge.target, {
|
|
edgeType: "typed",
|
|
compatible: edge.compatible ?? true,
|
|
detail: edge.detail ?? "",
|
|
});
|
|
}
|
|
return graph;
|
|
}
|
|
|
|
export function createCallGraph(
|
|
nodes: Array<{
|
|
requestId: string;
|
|
operationId: string;
|
|
status: string;
|
|
parentRequestId?: string;
|
|
}>,
|
|
): DirectedGraph {
|
|
const graph = new DirectedGraph();
|
|
for (const node of nodes) {
|
|
graph.addNode(node.requestId, {
|
|
requestId: node.requestId,
|
|
operationId: node.operationId,
|
|
status: node.status,
|
|
});
|
|
if (node.parentRequestId) {
|
|
const parentKey = node.parentRequestId;
|
|
if (!graph.hasDirectedEdge(parentKey, node.requestId)) {
|
|
graph.addEdgeWithKey(
|
|
`${parentKey}->${node.requestId}`,
|
|
parentKey,
|
|
node.requestId,
|
|
{ edgeType: "triggered" },
|
|
);
|
|
}
|
|
}
|
|
}
|
|
return graph;
|
|
} |