Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
isamu committed Jun 20, 2024
1 parent cf19da1 commit d12c61d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 28 deletions.
16 changes: 5 additions & 11 deletions src/utils/setAgentToLiteGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@ const createAgentNode = (agentData: AgentData) => {
Object.defineProperty(DynamicSubclass, "name", { value: agentData.name });

return DynamicSubclass;
}
};

const jsonSchemaToIO = (inputs: any, typeName: string) => {

if (!inputs) {
return [[typeName, ["string", "number", "object"]]];
}
Expand Down Expand Up @@ -89,22 +88,17 @@ const setAgentToLiteGraph = (agents: AgentFunctionInfoDictionary) => {
{
name: "number",
category: "static",
outputs: [
["Output", "number"],
],
outputs: [["Output", "number"]],
},
{
name: "string",
category: "static",
outputs: [
["Output", "string"],
],
outputs: [["Output", "string"]],
},
].map((agent: AgentData) => {
LiteGraph.registerNodeType([agent.category, agent.name].join("/"), createAgentNode(agent));
});


const lite2agent: Record<string, AgentFunctionInfo> = {};
const lite2inputs: Record<string, string[]> = {};
const lite2output: Record<string, string[]> = {};
Expand All @@ -114,8 +108,8 @@ const setAgentToLiteGraph = (agents: AgentFunctionInfoDictionary) => {
agent.category.forEach((category) => {
const name = agent.name.replace(/Agent$/, "");
const nodeType = [category, name].join("/");
lite2inputs[nodeType] = jsonSchemaToI2IOType(agent.inputs)
lite2output[nodeType] = jsonSchemaToI2IOType(agent.output)
lite2inputs[nodeType] = jsonSchemaToI2IOType(agent.inputs);
lite2output[nodeType] = jsonSchemaToI2IOType(agent.output);
lite2agent[nodeType] = agent;
LiteGraph.registerNodeType(
nodeType,
Expand Down
34 changes: 19 additions & 15 deletions src/utils/toGraphAi.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
import { NodeData, AgentFunctionInfo } from "graphai";
import { serializedLGraph, LGraphNode } from "litegraph.js";

const liteGraph2GraphData = (liteGraph: serializedLGraph, lite2agent: Record<string, AgentFunctionInfo>, lite2inputs: Record<string, string[]>, lite2output: Record<string, string[]>) => {

const liteGraph2GraphData = (
liteGraph: serializedLGraph,
lite2agent: Record<string, AgentFunctionInfo>,
lite2inputs: Record<string, string[]>,
lite2output: Record<string, string[]>,
) => {
const inputTypes = liteGraph.nodes.reduce((tmp: Record<string, string[]>, node: ReturnType<LGraphNode["serialize"]>) => {
tmp[node.id] = lite2inputs[node.type ?? '']
tmp[node.id] = lite2inputs[node.type ?? ""];
return tmp;
}, {});

const outTypes = liteGraph.nodes.reduce((tmp: Record<string, string[]>, node: ReturnType<LGraphNode["serialize"]>) => {
tmp[node.id] = lite2output[node.type ?? '']
tmp[node.id] = lite2output[node.type ?? ""];
return tmp;
}, {});

// [link index, out node, out position, in node, in position]

const linkObj = liteGraph.links.reduce((tmp: Record<string, string[] | any>, link: [number, number, number, number, number, string]) => {
const [,outNodeId, outPositionIndex, inNodeId, inPositionIndex] = link;
const [, outNodeId, outPositionIndex, inNodeId, inPositionIndex] = link;

const pos = outTypes[outNodeId] && outTypes[outNodeId].length > 0 ? '.' + outTypes[outNodeId][outPositionIndex] : ''
const pos = outTypes[outNodeId] && outTypes[outNodeId].length > 0 ? "." + outTypes[outNodeId][outPositionIndex] : "";

const inputNode = `:node_${outNodeId}${pos}`
const inputNode = `:node_${outNodeId}${pos}`;
if (inputTypes[inNodeId] && inputTypes[inNodeId].length > 0) {
if (tmp[inNodeId] === undefined) {
tmp[inNodeId] = {};
Expand All @@ -35,21 +39,21 @@ const liteGraph2GraphData = (liteGraph: serializedLGraph, lite2agent: Record<str
}
return tmp;
}, {});

const nodes = liteGraph.nodes.reduce((tmp: Record<string, NodeData>, node: ReturnType<LGraphNode["serialize"]>) => {
// [link index, out node, out position, in node, in position]
const inputs = linkObj[node.id];

if ((node.type||'').startsWith("static/")) {
if ((node.type || "").startsWith("static/")) {
tmp[`node_${node.id}`] = {
value: "123",
}
};
} else {
const agent = lite2agent[node.type||''];
const agent = lite2agent[node.type || ""];
tmp[`node_${node.id}`] = {
agent: agent ? agent.name : node.type || '',
agent: agent ? agent.name : node.type || "",
inputs: inputs ? inputs : undefined,
}
};
}
return tmp;
}, {});
Expand Down
4 changes: 2 additions & 2 deletions src/views/Home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ export default defineComponent({
let lite2agent = {};
let lite2inputs = {};
let lite2output = {};
const graph = new LGraph();
onMounted(() => {
const ret = setAgentToLiteGraph(vanillaAgents);
lite2agent = ret.lite2agent;
lite2inputs = ret.lite2inputs;
lite2output = ret.lite2output;
new LGraphCanvas(canvasRef.value, graph);
const node_const = LiteGraph.createNode("data/copy");
Expand Down

0 comments on commit d12c61d

Please sign in to comment.