Skip to content

Commit

Permalink
Merge pull request #243 from toyamarinyon/fix-ghost-connectors
Browse files Browse the repository at this point in the history
fix(graph): Handle ghost connections in graph flow derivation
  • Loading branch information
toyamarinyon authored Dec 17, 2024
2 parents 8402587 + ed8de34 commit faea333
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 9 deletions.
30 changes: 25 additions & 5 deletions app/(playground)/p/[agentId]/canary/lib/graph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,15 @@ const graph: Graph = {
name: "Goodday",
position: { x: 135, y: 360 },
selected: false,
type: "variable",
content: { type: "text", text: "Today is very good day" },
type: "action",
content: {
type: "textGeneration",
llm: "anthropic:claude-3-5-sonnet-latest",
temperature: 0.7,
topP: 1,
instruction: "Good day",
sources: [],
},
},
{
id: "nd_guzyxfacpt5db2n9lkjify3z",
Expand Down Expand Up @@ -104,7 +111,7 @@ const graph: Graph = {
{
id: "cnnc_x8dy20365eqk9h033a800a5a",
sourceNodeId: "nd_h8h4uhp7kov9v7pj1yyofen8",
sourceNodeType: "variable",
sourceNodeType: "action",
targetNodeId: "nd_ffz8hv1isj4w3r4s23a6klkz",
targetNodeType: "action",
targetNodeHandleId: "ndh_n3xuz7ao5dyfusfukagbi3l7",
Expand All @@ -117,6 +124,15 @@ const graph: Graph = {
targetNodeHandleId: "ndh_xjlzyp1yq7vd1ih43rxuo8l9",
targetNodeType: "action",
},
// Ghost connection(targetNodeId is not in nodes)
{
id: "cnnc_ghost_connection",
sourceNodeId: "nd_ffz8hv1isj4w3r4s23a6klkz",
sourceNodeType: "action",
targetNodeId: "nd_fake_node",
targetNodeHandleId: "ndh_fake_node_handle",
targetNodeType: "action",
},
],
artifacts: [],
version: "2024-12-09",
Expand All @@ -133,11 +149,15 @@ describe("deriveFlows", () => {
expect(flows[0].nodes.length).toBe(2);
expect(flows[1].nodes.length).toBe(3);
});
test("ignore ghost connectors", () => {
console.log(flows[1].jobs[2].steps);
expect(flows[1].jobs[2].steps.length).toBe(1);
});
});

describe("isLatestVersion", () => {
test("latest version", () => {
expect(isLatestVersion({ version: "20241213" } as Graph)).toBe(true);
expect(isLatestVersion({ version: "20241217" } as Graph)).toBe(true);
});
test("old version", () => {
expect(isLatestVersion({} as Graph)).toBe(false);
Expand Down Expand Up @@ -183,7 +203,7 @@ describe("migrateGraph", () => {
],
artifacts: [],
} as unknown as Graph);
expect(after.version).toBe("20241213");
expect(after.version).toBe("20241217");
expect(after.nodes[0].content.type).toBe("files");
});
});
19 changes: 18 additions & 1 deletion app/(playground)/p/[agentId]/canary/lib/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
Flow,
Graph,
Job,
LatestGraphVersion,
Node,
NodeId,
Step,
Expand All @@ -15,8 +16,15 @@ export function deriveFlows(graph: Graph): Flow[] {
const processedNodes = new Set<NodeId>();
const flows: Flow[] = [];
const connectionMap = new Map<NodeId, Set<NodeId>>();
const nodeIds = new Set<NodeId>(graph.nodes.map((node) => node.id));

for (const connection of graph.connections) {
if (
!nodeIds.has(connection.sourceNodeId) ||
!nodeIds.has(connection.targetNodeId)
) {
continue;
}
if (!connectionMap.has(connection.sourceNodeId)) {
connectionMap.set(connection.sourceNodeId, new Set());
}
Expand Down Expand Up @@ -268,7 +276,8 @@ export function deriveFlows(graph: Graph): Flow[] {
}

export function isLatestVersion(graph: Graph): boolean {
return graph.version === "20241213";
const latestGraphVersion = "20241217" satisfies LatestGraphVersion;
return graph.version === latestGraphVersion;
}

export function migrateGraph(graph: Graph): Graph {
Expand Down Expand Up @@ -320,5 +329,13 @@ export function migrateGraph(graph: Graph): Graph {
};
}

if (newGraph.version === "20241213") {
newGraph = {
...newGraph,
flows: deriveFlows(newGraph),
version: "20241217",
};
}

return newGraph;
}
2 changes: 1 addition & 1 deletion app/(playground)/p/[agentId]/canary/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export function initGraph(): Graph {
nodes: [],
connections: [],
artifacts: [],
version: "20241213" satisfies LatestGraphVersion,
version: "20241217" satisfies LatestGraphVersion,
flows: [],
executionIndexes: [],
};
Expand Down
6 changes: 4 additions & 2 deletions app/(playground)/p/[agentId]/canary/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,10 @@ type GraphVersion =
| "2024-12-10"
| "2024-12-11"
| "20241212"
| "20241213";
export type LatestGraphVersion = "20241213";
| "20241213"
| "20241217";

export type LatestGraphVersion = "20241217";
export interface Graph {
id: GraphId;
nodes: Node[];
Expand Down

0 comments on commit faea333

Please sign in to comment.