Skip to content
This repository has been archived by the owner on Nov 23, 2023. It is now read-only.

Feature/graph positioning #118

Merged
merged 3 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/electron/lib/ai/ai-cookbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
* */

export function cookUnsafeResponse(data: any) {
return responseSchema.parse(data);

Check warning on line 131 in src/electron/lib/ai/ai-cookbook.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/ai/ai-cookbook.ts#L131

Added line #L131 was not covered by tests
}

// All the LLM methods should return some JS object which can be stringified
Expand All @@ -151,22 +151,24 @@
graphId: UUID,
args: AddNodeConfig["args"]
) {
try {
const node = registry[args.signature];

Check warning on line 155 in src/electron/lib/ai/ai-cookbook.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/ai/ai-cookbook.ts#L154-L155

Added lines #L154 - L155 were not covered by tests
if (!node) return errorResponse("The provided signature is invalid : node does not exist");

const response = graphManager.addNode(graphId, node, CoreGraphUpdateParticipant.ai);
// TODO: Calculate at what position to add node then set pos
const pos = { x: 0, y: 0 };
const response = graphManager.addNode(graphId, node, pos, CoreGraphUpdateParticipant.ai);

Check warning on line 160 in src/electron/lib/ai/ai-cookbook.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/ai/ai-cookbook.ts#L159-L160

Added lines #L159 - L160 were not covered by tests

if (response.status === "success" && response.data) {
// Truncate ids
response.data.inputs = truncId(response.data.inputs);
response.data.outputs = truncId(response.data.outputs);
response.data.nodeId = truncId([response.data.nodeId])[0];

Check warning on line 166 in src/electron/lib/ai/ai-cookbook.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/ai/ai-cookbook.ts#L164-L166

Added lines #L164 - L166 were not covered by tests
}

return response;

Check warning on line 169 in src/electron/lib/ai/ai-cookbook.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/ai/ai-cookbook.ts#L169

Added line #L169 was not covered by tests
} catch (error) {
return errorResponse(error as string);

Check warning on line 171 in src/electron/lib/ai/ai-cookbook.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/ai/ai-cookbook.ts#L171

Added line #L171 was not covered by tests
}
}

Expand All @@ -186,18 +188,18 @@
graphId: UUID,
args: RemoveNodeConfig["args"]
) {
try {
const graph = _exporter.export(graphManager.getGraph(graphId));
const { nodeMap } = graph;

Check warning on line 193 in src/electron/lib/ai/ai-cookbook.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/ai/ai-cookbook.ts#L191-L193

Added lines #L191 - L193 were not covered by tests

const fullId = nodeMap[args.id];

Check warning on line 195 in src/electron/lib/ai/ai-cookbook.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/ai/ai-cookbook.ts#L195

Added line #L195 was not covered by tests
if (fullId === undefined)
return errorResponse("The provided id is invalid : id does not exist");

Check warning on line 197 in src/electron/lib/ai/ai-cookbook.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/ai/ai-cookbook.ts#L197

Added line #L197 was not covered by tests

const response = graphManager.removeNode(graphId, fullId, CoreGraphUpdateParticipant.ai);
return response;

Check warning on line 200 in src/electron/lib/ai/ai-cookbook.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/ai/ai-cookbook.ts#L199-L200

Added lines #L199 - L200 were not covered by tests
} catch (error) {
return errorResponse(error as string);

Check warning on line 202 in src/electron/lib/ai/ai-cookbook.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/ai/ai-cookbook.ts#L202

Added line #L202 was not covered by tests
}
}

Expand All @@ -220,20 +222,20 @@
graphId: UUID,
args: AddEdgeConfig["args"]
) {
try {
const graph = _exporter.export(graphManager.getGraph(graphId));
const { anchorMap } = graph;
const output = anchorMap[args.output];
const input = anchorMap[args.input];

Check warning on line 229 in src/electron/lib/ai/ai-cookbook.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/ai/ai-cookbook.ts#L225-L229

Added lines #L225 - L229 were not covered by tests
if (output === undefined)
return errorResponse("Output anchor" + args.output + "does not exist");

Check warning on line 231 in src/electron/lib/ai/ai-cookbook.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/ai/ai-cookbook.ts#L231

Added line #L231 was not covered by tests
if (input === undefined) return errorResponse("Input anchor" + args.input + "does not exist");

const response = graphManager.addEdge(graphId, input, output, CoreGraphUpdateParticipant.ai);
return response;

Check warning on line 235 in src/electron/lib/ai/ai-cookbook.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/ai/ai-cookbook.ts#L234-L235

Added lines #L234 - L235 were not covered by tests
} catch (error) {
// Manual error to give ai
return errorResponse(error as string);

Check warning on line 238 in src/electron/lib/ai/ai-cookbook.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/ai/ai-cookbook.ts#L238

Added line #L238 was not covered by tests
}
}

Expand All @@ -255,24 +257,24 @@
graphId: UUID,
args: RemoveEdgeConfig["args"]
) {
try {
const graph = getGraph(graphManager, graphId);

Check warning on line 261 in src/electron/lib/ai/ai-cookbook.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/ai/ai-cookbook.ts#L260-L261

Added lines #L260 - L261 were not covered by tests

const edge = findEdgeById(graph.graph.edges, args.id);

Check warning on line 263 in src/electron/lib/ai/ai-cookbook.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/ai/ai-cookbook.ts#L263

Added line #L263 was not covered by tests

if (edge === undefined) return errorResponse("The provided id is invalid : id does not exist");

const { anchorMap } = graph;

Check warning on line 267 in src/electron/lib/ai/ai-cookbook.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/ai/ai-cookbook.ts#L267

Added line #L267 was not covered by tests
const anchor = anchorMap[edge?.input];

if (anchor === undefined)
return errorResponse("The provided id is invalid : id does not exist");

Check warning on line 271 in src/electron/lib/ai/ai-cookbook.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/ai/ai-cookbook.ts#L271

Added line #L271 was not covered by tests

const response = graphManager.removeEdge(graphId, anchor, CoreGraphUpdateParticipant.ai);
return response;

Check warning on line 274 in src/electron/lib/ai/ai-cookbook.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/ai/ai-cookbook.ts#L273-L274

Added lines #L273 - L274 were not covered by tests
} catch (error) {
// Manual error to give ai
return errorResponse(error as string);

Check warning on line 277 in src/electron/lib/ai/ai-cookbook.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/ai/ai-cookbook.ts#L277

Added line #L277 was not covered by tests
}
}

Expand All @@ -284,7 +286,7 @@
// const graph = _exporter.export(graphManager.getGraph(graphId));
// const { nodeMap } = graph;
// return graphManager.updateUIInputsTest(graphId, nodeMap[args.nodeId], args.changedInputValues);
return { status: "error", message: "Not implemented" } satisfies QueryResponse;

Check warning on line 289 in src/electron/lib/ai/ai-cookbook.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/ai/ai-cookbook.ts#L289

Added line #L289 was not covered by tests
}

export function updateInputValue(
Expand All @@ -292,26 +294,26 @@
graphId: string,
args: UpdateInputValueConfig["args"]
) {
const graph = graphManager.getGraph(graphId);

Check warning on line 297 in src/electron/lib/ai/ai-cookbook.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/ai/ai-cookbook.ts#L297

Added line #L297 was not covered by tests

if (!graph) {
return {

Check warning on line 300 in src/electron/lib/ai/ai-cookbook.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/ai/ai-cookbook.ts#L300

Added line #L300 was not covered by tests
status: "error",
message: "Graph does not exist",
};
}

const llmGraph = _exporter.export(graph);
const { nodeMap } = llmGraph;
const { inputValueId, newInputValue, nodeId } = args;
const changedUIInputs = { [inputValueId]: newInputValue };
const updatedInputValues = graph.getUpdatedUIInputs(nodeMap[nodeId], changedUIInputs);

Check warning on line 310 in src/electron/lib/ai/ai-cookbook.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/ai/ai-cookbook.ts#L306-L310

Added lines #L306 - L310 were not covered by tests

if (updatedInputValues.status === "error") {
return updatedInputValues;

Check warning on line 313 in src/electron/lib/ai/ai-cookbook.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/ai/ai-cookbook.ts#L313

Added line #L313 was not covered by tests
}

return graphManager.updateUIInputs(

Check warning on line 316 in src/electron/lib/ai/ai-cookbook.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/ai/ai-cookbook.ts#L316

Added line #L316 was not covered by tests
graphId,
nodeMap[nodeId],
updatedInputValues.data,
Expand All @@ -338,7 +340,7 @@
* */

export function findEdgeById(edges: Edge[], id: string): Edge | undefined {
return edges.find((edge) => edge.id === id);

Check warning on line 343 in src/electron/lib/ai/ai-cookbook.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/ai/ai-cookbook.ts#L343

Added line #L343 was not covered by tests
}

/**
Expand All @@ -348,11 +350,11 @@
* */

export function errorResponse(message: string) {
const response: QueryResponse = {

Check warning on line 353 in src/electron/lib/ai/ai-cookbook.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/ai/ai-cookbook.ts#L353

Added line #L353 was not covered by tests
status: "error",
message,
};
return response;

Check warning on line 357 in src/electron/lib/ai/ai-cookbook.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/ai/ai-cookbook.ts#L357

Added line #L357 was not covered by tests
}

/**
Expand All @@ -362,7 +364,7 @@
* */

export function getGraph(graphManager: CoreGraphManager, id: UUID): LLMGraph {
return _exporter.export(graphManager.getGraph(id));

Check warning on line 367 in src/electron/lib/ai/ai-cookbook.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/ai/ai-cookbook.ts#L367

Added line #L367 was not covered by tests
}

/**
Expand All @@ -372,31 +374,31 @@
* */

export function truncId(arr: string[]): string[] {
return arr.map((str) => str.slice(0, 6));

Check warning on line 377 in src/electron/lib/ai/ai-cookbook.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/ai/ai-cookbook.ts#L377

Added line #L377 was not covered by tests
}

export function splitStringIntoJSONObjects(input: string) {
if (input.trim() === "") {
return [];

Check warning on line 382 in src/electron/lib/ai/ai-cookbook.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/ai/ai-cookbook.ts#L382

Added line #L382 was not covered by tests
}

const jsonObjects = [];
let currentIndex = 0;
let openBrackets = 0;

Check warning on line 387 in src/electron/lib/ai/ai-cookbook.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/ai/ai-cookbook.ts#L385-L387

Added lines #L385 - L387 were not covered by tests

for (let i = 0; i < input.length; i++) {

Check warning on line 389 in src/electron/lib/ai/ai-cookbook.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/ai/ai-cookbook.ts#L389

Added line #L389 was not covered by tests
if (input[i] === "{") {
openBrackets++;

Check warning on line 391 in src/electron/lib/ai/ai-cookbook.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/ai/ai-cookbook.ts#L391

Added line #L391 was not covered by tests
} else if (input[i] === "}") {
openBrackets--;

Check warning on line 393 in src/electron/lib/ai/ai-cookbook.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/ai/ai-cookbook.ts#L393

Added line #L393 was not covered by tests
}

if (openBrackets === 0 && input[i] === "}") {
const substring = input.substring(currentIndex, i + 1);
jsonObjects.push(substring);
currentIndex = i + 1;

Check warning on line 399 in src/electron/lib/ai/ai-cookbook.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/ai/ai-cookbook.ts#L397-L399

Added lines #L397 - L399 were not covered by tests
}
}

return jsonObjects;

Check warning on line 403 in src/electron/lib/ai/ai-cookbook.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/ai/ai-cookbook.ts#L403

Added line #L403 was not covered by tests
}
5 changes: 3 additions & 2 deletions src/electron/lib/api/apis/GraphApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { type UUID } from "../../../../shared/utils/UniqueEntity";
import { type NodeSignature } from "../../../../shared/ui/ToolboxTypes";
import { type INodeUIInputs } from "../../../../shared/types";
import { CoreGraphUpdateParticipant } from "../../core-graph/CoreGraphInteractors";
import type { GraphMetadata } from "../../../../shared/ui/UIGraph";
import type { GraphMetadata, SvelvetCanvasPos } from "../../../../shared/ui/UIGraph";

// Graphs across projects are stored homogeneously and referenced by UUID
export class GraphApi implements ElectronMainApi<GraphApi> {
Expand All @@ -15,10 +15,11 @@ export class GraphApi implements ElectronMainApi<GraphApi> {
}

// TODO: Implement these properly
async addNode(graphUUID: UUID, nodeSignature: NodeSignature) {
async addNode(graphUUID: UUID, nodeSignature: NodeSignature, pos: SvelvetCanvasPos) {
return this._blix.graphManager.addNode(
graphUUID,
this._blix.toolbox.getNodeInstance(nodeSignature),
pos,
CoreGraphUpdateParticipant.user
);
}
Expand Down
17 changes: 15 additions & 2 deletions src/electron/lib/core-graph/CoreGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import type { EdgeToJSON, GraphToJSON, NodeToJSON } from "./CoreGraphExporter";
import { type NodeSignature } from "../../../shared/ui/ToolboxTypes";
import type { INodeUIInputs, QueryResponse, UIValue } from "../../../shared/types";
import { type GraphMetadata } from "../../../shared/ui/UIGraph";
import { type GraphMetadata, type SvelvetCanvasPos } from "../../../shared/ui/UIGraph";
import { type MediaOutputId } from "../../../shared/types/media";

// =========================================
Expand Down Expand Up @@ -51,6 +51,9 @@
// Maps a node UUID to a list of UI inputs
private uiInputs: { [key: UUID]: CoreNodeUIInputs };

// Maps a node UUID to a UI canvas position
private uiPositions: { [key: UUID]: SvelvetCanvasPos };

// private subscribers: CoreGraphSubscriber[];
private static nodeTracker = 0;
constructor() {
Expand All @@ -61,6 +64,7 @@
this.edgeSrc = {};
this.outputNodes = {};
this.uiInputs = {};
this.uiPositions = {};
this.metadata = {
displayName: "Graph",
};
Expand Down Expand Up @@ -145,7 +149,7 @@
}

public get getAllUIInputs() {
return this.uiInputs;

Check warning on line 152 in src/electron/lib/core-graph/CoreGraph.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraph.ts#L152

Added line #L152 was not covered by tests
}

public get getMetadata() {
Expand All @@ -156,8 +160,15 @@
return this.uiInputs[nodeUUID]?.getInputs || null;
}

public get getUIPositions() {
return this.uiPositions;
}

// We need to pass in node name and plugin name
public addNode(node: NodeInstance): QueryResponse<{
public addNode(
node: NodeInstance,
pos: SvelvetCanvasPos
): QueryResponse<{
nodeId: UUID;
inputs: string[];
outputs: string[];
Expand Down Expand Up @@ -185,6 +196,8 @@

// console.log(QueryResponseStatus.success)
const anchors: AiAnchors = n.returnAnchors();
// Add position of node to graph
this.uiPositions[n.uuid] = pos;
return {
status: "success",
message: "Node added succesfully",
Expand All @@ -196,7 +209,7 @@
},
};
} catch (error) {
return { status: "error", message: error as string };

Check warning on line 212 in src/electron/lib/core-graph/CoreGraph.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraph.ts#L212

Added line #L212 was not covered by tests
}
// TODO: Add Node Styling
}
Expand Down Expand Up @@ -264,38 +277,38 @@
}

public updateUIInputs(nodeUUID: UUID, nodeUIInputs: INodeUIInputs): QueryResponse {
this.uiInputs[nodeUUID] = new CoreNodeUIInputs(nodeUIInputs);

Check warning on line 280 in src/electron/lib/core-graph/CoreGraph.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraph.ts#L280

Added line #L280 was not covered by tests

// If output node, update output node id
if (this.outputNodes[nodeUUID]) {
this.outputNodes[nodeUUID] = nodeUIInputs.inputs.outputId as MediaOutputId;

Check warning on line 284 in src/electron/lib/core-graph/CoreGraph.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraph.ts#L284

Added line #L284 was not covered by tests
}

return { status: "success" };

Check warning on line 287 in src/electron/lib/core-graph/CoreGraph.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraph.ts#L287

Added line #L287 was not covered by tests
}

public getUpdatedUIInputs(nodeUUID: UUID, changedUIInputs: Record<string, unknown>) {
const currentInputValues = this.uiInputs[nodeUUID];

Check warning on line 291 in src/electron/lib/core-graph/CoreGraph.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraph.ts#L291

Added line #L291 was not covered by tests

if (!currentInputValues) {
return {

Check warning on line 294 in src/electron/lib/core-graph/CoreGraph.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraph.ts#L294

Added line #L294 was not covered by tests
status: "error",
message: "Node does not exist",
} satisfies QueryResponse;
}

const nodeUIInputs: INodeUIInputs = {

Check warning on line 300 in src/electron/lib/core-graph/CoreGraph.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraph.ts#L300

Added line #L300 was not covered by tests
inputs: currentInputValues.getInputs,
changes: [],
};

for (const key in changedUIInputs) {

Check warning on line 305 in src/electron/lib/core-graph/CoreGraph.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraph.ts#L305

Added line #L305 was not covered by tests
if (key in changedUIInputs) {
if (key in currentInputValues.getInputs) {
nodeUIInputs.inputs[key] = changedUIInputs[key];
nodeUIInputs.changes.push(key);

Check warning on line 309 in src/electron/lib/core-graph/CoreGraph.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraph.ts#L308-L309

Added lines #L308 - L309 were not covered by tests
} else {
return {

Check warning on line 311 in src/electron/lib/core-graph/CoreGraph.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraph.ts#L311

Added line #L311 was not covered by tests
status: "error",
message: `Input value with id ${key} does not exist`,
} satisfies QueryResponse;
Expand All @@ -303,7 +316,7 @@
}
}

return {

Check warning on line 319 in src/electron/lib/core-graph/CoreGraph.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraph.ts#L319

Added line #L319 was not covered by tests
status: "success",
data: nodeUIInputs,
} satisfies QueryResponse;
Expand Down Expand Up @@ -343,7 +356,7 @@
if (!node) return { status: "error", message: "Node to be deleted does not exist" };

if (this.outputNodes[nodeToDelete]) {
delete this.outputNodes[nodeToDelete];

Check warning on line 359 in src/electron/lib/core-graph/CoreGraph.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraph.ts#L359

Added line #L359 was not covered by tests
}

try {
Expand Down Expand Up @@ -372,7 +385,7 @@
delete this.nodes[node.uuid];
return { status: "success" };
} catch (error) {
return { status: "error", message: error as string };

Check warning on line 388 in src/electron/lib/core-graph/CoreGraph.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraph.ts#L388

Added line #L388 was not covered by tests
}
}

Expand Down Expand Up @@ -401,7 +414,7 @@

return { status: "success" };
} catch (error) {
return { status: "error", message: error as string };

Check warning on line 417 in src/electron/lib/core-graph/CoreGraph.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraph.ts#L417

Added line #L417 was not covered by tests
}
}

Expand All @@ -413,21 +426,21 @@

public updateMetadata(updatedMetadata: Partial<GraphMetadata>) {
if (!updatedMetadata) {
return {

Check warning on line 429 in src/electron/lib/core-graph/CoreGraph.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraph.ts#L429

Added line #L429 was not covered by tests
status: "error",
message: "No metadata provided",
} satisfies QueryResponse;
}

const { displayName } = updatedMetadata;

Check warning on line 435 in src/electron/lib/core-graph/CoreGraph.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraph.ts#L435

Added line #L435 was not covered by tests

const newMetadata: GraphMetadata = {

Check warning on line 437 in src/electron/lib/core-graph/CoreGraph.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraph.ts#L437

Added line #L437 was not covered by tests
displayName: displayName ? displayName : this.metadata.displayName,
};

this.metadata = newMetadata;

Check warning on line 441 in src/electron/lib/core-graph/CoreGraph.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraph.ts#L441

Added line #L441 was not covered by tests

return {

Check warning on line 443 in src/electron/lib/core-graph/CoreGraph.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraph.ts#L443

Added line #L443 was not covered by tests
status: "success",
message: "Metadata updated",
} satisfies QueryResponse;
Expand Down Expand Up @@ -560,7 +573,7 @@
}

public exportJSON(): NodeToJSON {
return {

Check warning on line 576 in src/electron/lib/core-graph/CoreGraph.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraph.ts#L576

Added line #L576 was not covered by tests
signature: `${this.plugin}.${this.name}`,
styling: this.styling!,
};
Expand Down Expand Up @@ -619,11 +632,11 @@
export class CoreNodeUIInputs {
private readonly inputs: { [key: string]: UIValue };
constructor(nodeUIInpust: INodeUIInputs) {
this.inputs = nodeUIInpust.inputs;

Check warning on line 635 in src/electron/lib/core-graph/CoreGraph.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraph.ts#L635

Added line #L635 was not covered by tests
}

public get getInputs() {
return this.inputs;

Check warning on line 639 in src/electron/lib/core-graph/CoreGraph.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraph.ts#L639

Added line #L639 was not covered by tests
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/electron/lib/core-graph/CoreGraphImporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ export class CoreGraphImporter {
const nodes: { [key: number]: UUID } = {};
for (const node of json.nodes) {
const nodeInstance: NodeInstance = this._toolbox.getNodeInstance(node.signature);
const res = graph.addNode(nodeInstance);
// TODO: fix importer to use imported node positions
const res = graph.addNode(nodeInstance, { x: 0, y: 0 });
if (res.status === "success" && res.data) {
nodes[cnt++] = res.data?.nodeId;
}
Expand Down
1 change: 1 addition & 0 deletions src/electron/lib/core-graph/CoreGraphInteractors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
}

public addListenParticipants(participants: CoreGraphUpdateParticipant[]) {
this.listenParticipants = new Set([...this.listenParticipants, ...participants]);

Check warning on line 47 in src/electron/lib/core-graph/CoreGraphInteractors.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraphInteractors.ts#L47

Added line #L47 was not covered by tests
}

public setListenParticipants(participants: CoreGraphUpdateParticipant[]) {
Expand All @@ -56,7 +56,7 @@
}

public addListenEvents(events: CoreGraphUpdateEvent[]) {
this.listenEvents = new Set([...this.listenEvents, ...events]);

Check warning on line 59 in src/electron/lib/core-graph/CoreGraphInteractors.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraphInteractors.ts#L59

Added line #L59 was not covered by tests
}

public setListenEvents(events: CoreGraphUpdateEvent[]) {
Expand All @@ -78,16 +78,16 @@

export class UIInputsGraphSubscriber extends CoreGraphSubscriber<IGraphUIInputs> {
onGraphChanged(graphId: UUID, graphData: CoreGraph): void {
const coreUIInputs = graphData.getAllUIInputs;
const graphUIInputs: IGraphUIInputs = {};

Check warning on line 82 in src/electron/lib/core-graph/CoreGraphInteractors.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraphInteractors.ts#L81-L82

Added lines #L81 - L82 were not covered by tests

// Convert core UI inputs to graph UI inputs
for (const node in coreUIInputs) {

Check warning on line 85 in src/electron/lib/core-graph/CoreGraphInteractors.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraphInteractors.ts#L85

Added line #L85 was not covered by tests
if (!coreUIInputs.hasOwnProperty(node)) continue;

graphUIInputs[node] = {} as INodeUIInputs;
graphUIInputs[node].inputs = coreUIInputs[node].getInputs;
graphUIInputs[node].changes = []; // TODO: Potentially tell the frontend exactly what changed

Check warning on line 90 in src/electron/lib/core-graph/CoreGraphInteractors.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraphInteractors.ts#L88-L90

Added lines #L88 - L90 were not covered by tests
}

if (this._notifyee) this._notifyee(graphId, graphUIInputs);
Expand All @@ -98,6 +98,7 @@
onGraphChanged(graphId: UUID, graphData: CoreGraph): void {
const uiGraph: UIGraph = new UIGraph(graphId);
uiGraph.metadata = graphData.getMetadata;
uiGraph.uiPositions = graphData.getUIPositions;
const nodesAndEdges: NodesAndEdgesGraph = graphData.exportNodesAndEdges();

for (const node in nodesAndEdges.nodes) {
Expand All @@ -118,9 +119,9 @@

for (const anchor in nodesAndEdges.nodes[node].outputs) {
if (!nodesAndEdges.nodes[node].outputs.hasOwnProperty(anchor)) continue;
const reducedAnchor = nodesAndEdges.nodes[node].outputs[anchor];

Check warning on line 122 in src/electron/lib/core-graph/CoreGraphInteractors.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraphInteractors.ts#L122

Added line #L122 was not covered by tests

uiGraph.nodes[node].anchorUUIDs[reducedAnchor.id] = anchor;

Check warning on line 124 in src/electron/lib/core-graph/CoreGraphInteractors.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraphInteractors.ts#L124

Added line #L124 was not covered by tests
}

// for (const anchor in nodesAndEdges.nodes[node].anchorUUIDs) {
Expand All @@ -131,9 +132,9 @@

for (const edge in nodesAndEdges.edges) {
if (!nodesAndEdges.edges.hasOwnProperty(edge)) continue;
const edgeData = nodesAndEdges.edges[edge];

Check warning on line 135 in src/electron/lib/core-graph/CoreGraphInteractors.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraphInteractors.ts#L135

Added line #L135 was not covered by tests

uiGraph.edges[edge] = new GraphEdge(

Check warning on line 137 in src/electron/lib/core-graph/CoreGraphInteractors.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraphInteractors.ts#L137

Added line #L137 was not covered by tests
edge,
edgeData.nodeUUIDFrom,
edgeData.nodeUUIDTo,
Expand Down
5 changes: 3 additions & 2 deletions src/electron/lib/core-graph/CoreGraphManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import { Blix } from "../Blix";
import type { INodeUIInputs, QueryResponse } from "../../../shared/types";
import type { MediaOutputId } from "../../../shared/types/media";
import { type GraphMetadata } from "../../../shared/ui/UIGraph";
import { type GraphMetadata, type SvelvetCanvasPos } from "../../../shared/ui/UIGraph";

const GRAPH_UPDATED_EVENT = new Set([CoreGraphUpdateEvent.graphUpdated]);

Expand Down Expand Up @@ -47,11 +47,12 @@
addNode(
graphUUID: UUID,
node: NodeInstance,
pos: SvelvetCanvasPos,
participant: CoreGraphUpdateParticipant
): QueryResponse<{ nodeId: UUID; inputs: string[]; outputs: string[] }> {
if (this._graphs[graphUUID] === undefined)
return { status: "error", message: "Graph does not exist" };
const res = this._graphs[graphUUID].addNode(node);
const res = this._graphs[graphUUID].addNode(node, pos);
if (res.status === "success") this.onGraphUpdated(graphUUID, GRAPH_UPDATED_EVENT, participant);
return res;
}
Expand Down Expand Up @@ -108,40 +109,40 @@
participant: CoreGraphUpdateParticipant
): QueryResponse {
if (this._graphs[graphUUID] === undefined)
return { status: "error", message: "Graph does not exist" };

Check warning on line 112 in src/electron/lib/core-graph/CoreGraphManager.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraphManager.ts#L112

Added line #L112 was not covered by tests

if (!nodeUIInputs) return { status: "error", message: "No node UI inputs provided" };

const res = this._graphs[graphUUID].updateUIInputs(nodeUUID, nodeUIInputs);

Check warning on line 116 in src/electron/lib/core-graph/CoreGraphManager.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraphManager.ts#L116

Added line #L116 was not covered by tests

const signature = this._graphs[graphUUID].getNodes[nodeUUID].getSignature;

Check warning on line 118 in src/electron/lib/core-graph/CoreGraphManager.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraphManager.ts#L118

Added line #L118 was not covered by tests

if (res.status === "success") {
// Determine whether the update should trigger the graph to recompute
const uiConfigs = this._toolbox.getNodeInstance(signature).uiConfigs;
const changes = nodeUIInputs.changes;

Check warning on line 123 in src/electron/lib/core-graph/CoreGraphManager.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraphManager.ts#L122-L123

Added lines #L122 - L123 were not covered by tests

if (signature === "blix.output") {
this._outputIds[nodeUUID] = nodeUIInputs.inputs.outputId as string;

Check warning on line 126 in src/electron/lib/core-graph/CoreGraphManager.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraphManager.ts#L126

Added line #L126 was not covered by tests
this._mainWindow?.apis.mediaClientApi.onMediaOutputIdsChanged(
new Set(Object.values(this._outputIds))
);
}

let shouldUpdate = false;
for (const change of changes) {

Check warning on line 133 in src/electron/lib/core-graph/CoreGraphManager.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraphManager.ts#L132-L133

Added lines #L132 - L133 were not covered by tests
if (uiConfigs[change].updatesBackend) {
shouldUpdate = true;
break;

Check warning on line 136 in src/electron/lib/core-graph/CoreGraphManager.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraphManager.ts#L135-L136

Added lines #L135 - L136 were not covered by tests
}
}

if (shouldUpdate) {
const updateEvents = new Set([CoreGraphUpdateEvent.uiInputsUpdated]);
this.onGraphUpdated(graphUUID, updateEvents, participant);

Check warning on line 142 in src/electron/lib/core-graph/CoreGraphManager.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraphManager.ts#L141-L142

Added lines #L141 - L142 were not covered by tests
}
}
return res;

Check warning on line 145 in src/electron/lib/core-graph/CoreGraphManager.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraphManager.ts#L145

Added line #L145 was not covered by tests
}

setPos(
Expand Down Expand Up @@ -205,20 +206,20 @@
updatedMetadata: Partial<GraphMetadata>,
participant: CoreGraphUpdateParticipant
) {
const graph = this._graphs[graphUUID];

Check warning on line 209 in src/electron/lib/core-graph/CoreGraphManager.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraphManager.ts#L209

Added line #L209 was not covered by tests

if (!graph) {
return {

Check warning on line 212 in src/electron/lib/core-graph/CoreGraphManager.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraphManager.ts#L212

Added line #L212 was not covered by tests
status: "error",
message: "Graph does not exist",
} satisfies QueryResponse;
}

const res = graph.updateMetadata(updatedMetadata);

Check warning on line 218 in src/electron/lib/core-graph/CoreGraphManager.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraphManager.ts#L218

Added line #L218 was not covered by tests
if (res.status === "success") {
this.onGraphUpdated(graphUUID, new Set([CoreGraphUpdateEvent.metadataUpdated]), participant);

Check warning on line 220 in src/electron/lib/core-graph/CoreGraphManager.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraphManager.ts#L220

Added line #L220 was not covered by tests
}
return res;

Check warning on line 222 in src/electron/lib/core-graph/CoreGraphManager.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/core-graph/CoreGraphManager.ts#L222

Added line #L222 was not covered by tests
}

// Notify all subscribers of change
Expand Down
82 changes: 44 additions & 38 deletions src/electron/lib/core-graph/CoreGraphTesting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,12 @@ export class TestGraph {

const g2: CoreGraph = new CoreGraph();

g2.addNode(tempNodesInt[0]);
g2.addNode(tempNodesInt[1]);
g2.addNode(tempNodesInt[2]);
g2.addNode(tempNodesInt[3]);
const pos = { x: 0, y: 0 };
g2.addNode(this.tempNodes[0], pos);
g2.addNode(this.tempNodes[1], pos);
g2.addNode(this.tempNodes[2], pos);
g2.addNode(this.tempNodes[3], pos);

// g2.addNode(tempNodesInt[4]);
// g2.addNode(tempNodesInt[5]);

Expand Down Expand Up @@ -267,10 +269,10 @@ export class TestGraph {
// =====================================

const g1: CoreGraph = new CoreGraph();

g1.addNode(this.tempNodes[0]);
g1.addNode(this.tempNodes[1]);
g1.addNode(this.tempNodes[2]);
const pos = { x: 0, y: 0 };
g1.addNode(this.tempNodes[0], pos);
g1.addNode(this.tempNodes[1], pos);
g1.addNode(this.tempNodes[2], pos);

const nodes = g1.getNodes;
const actualNode1 = Object.values(nodes)[0];
Expand Down Expand Up @@ -325,12 +327,13 @@ export class TestGraph {

const g2: CoreGraph = new CoreGraph();

g2.addNode(this.tempNodes[0]);
g2.addNode(this.tempNodes[1]);
g2.addNode(this.tempNodes[2]);
g2.addNode(this.tempNodes[3]);
g2.addNode(this.tempNodes[4]);
g2.addNode(this.tempNodes[5]);
const pos = { x: 0, y: 0 };
g2.addNode(this.tempNodes[0], pos);
g2.addNode(this.tempNodes[1], pos);
g2.addNode(this.tempNodes[2], pos);
g2.addNode(this.tempNodes[3], pos);
g2.addNode(this.tempNodes[4], pos);
g2.addNode(this.tempNodes[5], pos);

const g2Nodes = g2.getNodes;
const g2Node1 = Object.values(g2Nodes)[0];
Expand Down Expand Up @@ -408,8 +411,9 @@ export class TestGraph {

const g3: CoreGraph = new CoreGraph();

g3.addNode(this.tempNodes[0]);
g3.addNode(this.tempNodes[1]);
const pos = { x: 0, y: 0 };
g3.addNode(this.tempNodes[0], pos);
g3.addNode(this.tempNodes[1], pos);

const g3Nodes = g3.getNodes;
const g3Node1 = Object.values(g3Nodes)[0];
Expand Down Expand Up @@ -456,9 +460,9 @@ export class TestGraph {
// =====================================

const g4: CoreGraph = new CoreGraph();

g4.addNode(this.tempNodes[0]);
g4.addNode(this.tempNodes[1]);
const pos = { x: 0, y: 0 };
g4.addNode(this.tempNodes[0], pos);
g4.addNode(this.tempNodes[1], pos);

const g4Nodes = g4.getNodes;
const g4Node1 = Object.values(g4Nodes)[0];
Expand Down Expand Up @@ -504,7 +508,7 @@ export class TestGraph {

const g5: CoreGraph = new CoreGraph();

g5.addNode(this.tempNodes[0]);
g5.addNode(this.tempNodes[0], { x: 0, y: 0 });

const g5Nodes = g5.getNodes;
const g5Node1 = Object.values(g5Nodes)[0];
Expand Down Expand Up @@ -538,11 +542,12 @@ export class TestGraph {

const g6: CoreGraph = new CoreGraph();

g6.addNode(this.tempNodes[0]);
g6.addNode(this.tempNodes[1]);
g6.addNode(this.tempNodes[2]);
g6.addNode(this.tempNodes[3]);
g6.addNode(this.tempNodes[4]);
const pos = { x: 0, y: 0 };
g6.addNode(this.tempNodes[0], pos);
g6.addNode(this.tempNodes[1], pos);
g6.addNode(this.tempNodes[2], pos);
g6.addNode(this.tempNodes[3], pos);
g6.addNode(this.tempNodes[4], pos);

const g6Nodes = g6.getNodes;
const g6Node1 = Object.values(g6Nodes)[0];
Expand Down Expand Up @@ -621,11 +626,12 @@ export class TestGraph {

const g6: CoreGraph = new CoreGraph();

g6.addNode(this.tempNodes[0]);
g6.addNode(this.tempNodes[1]);
g6.addNode(this.tempNodes[2]);
g6.addNode(this.tempNodes[3]);
g6.addNode(this.tempNodes[4]);
const pos = { x: 0, y: 0 };
g6.addNode(this.tempNodes[0], pos);
g6.addNode(this.tempNodes[1], pos);
g6.addNode(this.tempNodes[2], pos);
g6.addNode(this.tempNodes[3], pos);
g6.addNode(this.tempNodes[4], pos);

const g6Nodes = g6.getNodes;
const g6Node1 = Object.values(g6Nodes)[0];
Expand Down Expand Up @@ -706,12 +712,12 @@ export class TestGraph {
// =====================================

const g6: CoreGraph = new CoreGraph();

g6.addNode(this.tempNodes[0]);
g6.addNode(this.tempNodes[1]);
g6.addNode(this.tempNodes[2]);
g6.addNode(this.tempNodes[3]);
g6.addNode(this.tempNodes[4]);
const pos = { x: 0, y: 0 };
g6.addNode(this.tempNodes[0], pos);
g6.addNode(this.tempNodes[1], pos);
g6.addNode(this.tempNodes[2], pos);
g6.addNode(this.tempNodes[3], pos);
g6.addNode(this.tempNodes[4], pos);

const g6Nodes = g6.getNodes;
const g6Node1 = Object.values(g6Nodes)[0];
Expand Down Expand Up @@ -919,8 +925,8 @@ export function testStuffies(blix: Blix) {
const graph = blix.graphManager.createGraph();
project.addGraph(graph);
const g = blix.graphManager.getGraph(graph);
g.addNode(tempNodes[0]);
g.addNode(tempNodes[1]);
g.addNode(tempNodes[0], { x: 0, y: 0 });
g.addNode(tempNodes[1], { x: 0, y: 0 });

const nodes = g.getNodes;
const node1 = Object.values(nodes)[0];
Expand Down
1 change: 1 addition & 0 deletions src/electron/lib/projects/ProjectCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
CoreGraphUpdateParticipant,
} from "../core-graph/CoreGraphInteractors";
import sharp from "sharp";
import type { SvelvetCanvasPos } from "../../../shared/ui/UIGraph";

export type SaveProjectArgs = {
projectId: UUID;
Expand Down Expand Up @@ -108,12 +109,12 @@
name: "Export Media As...",
description: "Save media to file system",
},
handler: async (ctx: CommandContext, args: ExportMedia) => {
const result = await exportMedia(ctx, args);

Check warning on line 113 in src/electron/lib/projects/ProjectCommands.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/projects/ProjectCommands.ts#L112-L113

Added lines #L112 - L113 were not covered by tests
if (result?.success && result.message) {
ctx.sendSuccessMessage(result?.message);
} else if (result?.error && result.error) {
ctx.sendErrorMessage(result.error);

Check warning on line 117 in src/electron/lib/projects/ProjectCommands.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/projects/ProjectCommands.ts#L117

Added line #L117 was not covered by tests
}
},
};
Expand Down Expand Up @@ -262,20 +263,20 @@

export async function exportMedia(ctx: CommandContext, args: ExportMedia) {
if (!args) {
return { success: false, error: "No media selected to export" };

Check warning on line 266 in src/electron/lib/projects/ProjectCommands.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/projects/ProjectCommands.ts#L266

Added line #L266 was not covered by tests
}

const { type, data } = args;

Check warning on line 269 in src/electron/lib/projects/ProjectCommands.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/projects/ProjectCommands.ts#L269

Added line #L269 was not covered by tests

if (!data) {
return { success: false, error: "No data was provided" };

Check warning on line 272 in src/electron/lib/projects/ProjectCommands.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/projects/ProjectCommands.ts#L272

Added line #L272 was not covered by tests
}

if (type === "Image") {
const base64Data = data.split(";base64, ");
const imgBuffer = Buffer.from(base64Data[1], "base64");

Check warning on line 277 in src/electron/lib/projects/ProjectCommands.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/projects/ProjectCommands.ts#L276-L277

Added lines #L276 - L277 were not covered by tests

const path = await showSaveDialog({

Check warning on line 279 in src/electron/lib/projects/ProjectCommands.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/projects/ProjectCommands.ts#L279

Added line #L279 was not covered by tests
title: "Export media as",
defaultPath: "blix.png",
// filters: [{ name: "Images", extensions: ["png, jpg", "jpeg"] }],
Expand All @@ -284,14 +285,14 @@

if (!path) return;

sharp(imgBuffer).toFile(path);

Check warning on line 288 in src/electron/lib/projects/ProjectCommands.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/projects/ProjectCommands.ts#L288

Added line #L288 was not covered by tests

return { success: true, message: "Media exported successfully" };

Check warning on line 290 in src/electron/lib/projects/ProjectCommands.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/projects/ProjectCommands.ts#L290

Added line #L290 was not covered by tests
} else if (type === "Number" || type === "color" || type === "string") {
const fileData = {

Check warning on line 292 in src/electron/lib/projects/ProjectCommands.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/projects/ProjectCommands.ts#L292

Added line #L292 was not covered by tests
OutputData: data,
};
const path = await showSaveDialog({

Check warning on line 295 in src/electron/lib/projects/ProjectCommands.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/projects/ProjectCommands.ts#L295

Added line #L295 was not covered by tests
title: "Export media as",
defaultPath: "blix.json",
filters: [{ name: "Data", extensions: ["json"] }],
Expand All @@ -300,14 +301,14 @@

if (!path) return;

try {
writeFile(path, JSON.stringify(fileData));

Check warning on line 305 in src/electron/lib/projects/ProjectCommands.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/projects/ProjectCommands.ts#L304-L305

Added lines #L304 - L305 were not covered by tests
} catch (err) {
logger.error(err);

Check warning on line 307 in src/electron/lib/projects/ProjectCommands.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/projects/ProjectCommands.ts#L307

Added line #L307 was not covered by tests
}

return { success: true, message: "Media exported successfully" };

Check warning on line 310 in src/electron/lib/projects/ProjectCommands.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/projects/ProjectCommands.ts#L310

Added line #L310 was not covered by tests
} else {
return { success: false, error: "Unsupported media type" };

Check warning on line 312 in src/electron/lib/projects/ProjectCommands.ts

View check run for this annotation

Codecov / codecov/patch

src/electron/lib/projects/ProjectCommands.ts#L312

Added line #L312 was not covered by tests
}
}
1 change: 1 addition & 0 deletions src/frontend/lib/stores/CommandStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
set({ commands: results });
}

async function addCommands(cmds: any[]) {

Check warning on line 20 in src/frontend/lib/stores/CommandStore.ts

View workflow job for this annotation

GitHub Actions / Lint Frontend

'cmds' is defined but never used
// window.apis.pluginApi.addCommand(cmds);
}

Expand Down Expand Up @@ -50,6 +50,7 @@
const blixCommandParams: Record<string, () => any> = {
"blix.projects.save": () => {
const project = get(projectsStore).activeProject;
// await window.apis.graphApi.updateUIPositions(project?.graphs);
return {
projectId: project?.id,
layout: project?.layout.saveLayout(),
Expand Down
15 changes: 13 additions & 2 deletions src/frontend/lib/stores/GraphStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
type SvelvetCanvasPos,
constructUIValueStore,
type GraphMetadata,
NodeStylingStore,
} from "@shared/ui/UIGraph";
import { writable, get, derived, type Writable, type Readable } from "svelte/store";
import { toolboxStore } from "./ToolboxStore";
Expand All @@ -31,14 +32,21 @@
// type Connections = (string | number | [string | number, string | number] | null)[];

// TODO: Return a GraphStore in createGraphStore for typing
export type GraphView = {
translation: { x: number; y: number };
dimensions: { width: number; height: number };
zoom: number;
};
export class GraphStore {
view: Writable<GraphView>;
graphStore: Writable<UIGraph>;
uiInputUnsubscribers: { [key: GraphNodeUUID]: (() => void)[] } = {};
uiInputSubscribers: { [key: GraphNodeUUID]: () => void } = {};

constructor(public uuid: GraphUUID) {
// Starts with empty graph
this.graphStore = writable<UIGraph>(new UIGraph(uuid));
this.view = writable<GraphView>();
}

public refreshUIInputs(newUIInputs: IGraphUIInputs) {
Expand Down Expand Up @@ -75,10 +83,13 @@
if (oldNodes[node]) {
// Node carried over from old graph, maintain its styling / UI inputs
graph.nodes[node].styling = oldNodes[node].styling;
// graph.nodes[node].styling.pos =
graph.nodes[node].inputUIValues = oldNodes[node].inputUIValues;
} else {
// If node has a UI input, create a store and subscribe to it
const toolboxNode = toolboxStore.getNode(graph.nodes[node].signature);
graph.nodes[node].styling = new NodeStylingStore();
graph.nodes[node].styling!.pos.set(newGraph.uiPositions[node]);

Check warning on line 92 in src/frontend/lib/stores/GraphStore.ts

View workflow job for this annotation

GitHub Actions / Lint Frontend

Forbidden non-null assertion

if (toolboxNode.ui) {
graph.nodes[node].inputUIValues = constructUIValueStore(
Expand All @@ -98,7 +109,7 @@
this.uiInputUnsubscribers[node].push(
inputs[input].subscribe(() => {
// console.log("UPDATE UI INPUTS", node, "->", input);
this.updateUIInputs(node, input).catch((err) => {

Check warning on line 112 in src/frontend/lib/stores/GraphStore.ts

View workflow job for this annotation

GitHub Actions / Lint Frontend

'err' is defined but never used
return;
});
})
Expand Down Expand Up @@ -128,9 +139,9 @@
});
}

async addNode(nodeSignature: NodeSignature, pos?: SvelvetCanvasPos) {
async addNode(nodeSignature: NodeSignature, pos: SvelvetCanvasPos) {
const thisUUID = get(this.graphStore).uuid;
const res = await window.apis.graphApi.addNode(thisUUID, nodeSignature);
const res = await window.apis.graphApi.addNode(thisUUID, nodeSignature, pos);

// if (pos) {
// console.log("SET NODE POS", pos);
Expand Down Expand Up @@ -159,20 +170,20 @@
payload.inputs[input] = get(nodeInputs.inputs[input]);
}

const res = await window.apis.graphApi.updateUIInputs(thisUUID, nodeUUID, payload);

Check warning on line 173 in src/frontend/lib/stores/GraphStore.ts

View workflow job for this annotation

GitHub Actions / Lint Frontend

'res' is assigned a value but never used

// Notify our UI subscribers
}

async removeNode(nodeUUID: GraphNodeUUID) {
const thisUUID = get(this.graphStore).uuid;
const res = await window.apis.graphApi.removeNode(thisUUID, nodeUUID);

Check warning on line 180 in src/frontend/lib/stores/GraphStore.ts

View workflow job for this annotation

GitHub Actions / Lint Frontend

'res' is assigned a value but never used
return false;
}

async removeEdge(anchorTo: AnchorUUID) {
const thisUUID = get(this.graphStore).uuid;
const res = await window.apis.graphApi.removeEdge(thisUUID, anchorTo);

Check warning on line 186 in src/frontend/lib/stores/GraphStore.ts

View workflow job for this annotation

GitHub Actions / Lint Frontend

'res' is assigned a value but never used
return false;
}

Expand All @@ -184,7 +195,7 @@
return this.graphStore.subscribe;
}

public addUISubscriber(callback: () => null) {

Check warning on line 198 in src/frontend/lib/stores/GraphStore.ts

View workflow job for this annotation

GitHub Actions / Lint Frontend

'callback' is defined but never used
// TODO
return;
}
Expand Down Expand Up @@ -234,7 +245,7 @@
return stores;
});

const val = get(this.mall);

Check warning on line 248 in src/frontend/lib/stores/GraphStore.ts

View workflow job for this annotation

GitHub Actions / Lint Frontend

'val' is assigned a value but never used
}

public refreshGraphUIInputs(graphUUID: GraphUUID, newUIInputs: IGraphUIInputs) {
Expand All @@ -246,7 +257,7 @@
return stores;
});

const val = get(this.mall);

Check warning on line 260 in src/frontend/lib/stores/GraphStore.ts

View workflow job for this annotation

GitHub Actions / Lint Frontend

'val' is assigned a value but never used
}

public refreshGraphMetadata(graphUUID: GraphUUID, newMetadata: GraphMetadata) {
Expand Down
11 changes: 11 additions & 0 deletions src/frontend/ui/tiles/Graph.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@
$: zoom = graphData?.transforms?.scale;
$: dimensions = graphData?.dimensions;

$: $thisGraphStore?.view?.set({
translation: $translation,
dimensions: $dimensions,
zoom: $zoom,
});
// $: if($thisGraphStore?.view) {
// console.log(get($thisGraphStore.view));
// }

$: console.log($zoom);

// Hooks exposed by <Svelvet />
let connectAnchorIds: (
sourceNode: NodeKey,
Expand Down
7 changes: 5 additions & 2 deletions src/frontend/ui/utils/ContextMenu.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,12 @@
if (!graphStore) return;

if (action.type === "addNode") {
const view = get(graphStore.view);
console.log(view);
console.log(graphMenuState.canvasPos);
graphStore.addNode(action.signature, {
x: graphMenuState.canvasPos.x,
y: graphMenuState.canvasPos.y,
x: view.dimensions.width / 2 - view.translation.x / view.zoom,
y: view.dimensions.height / 2 - view.translation.y / view.zoom,
});
}
},
Expand Down
1 change: 1 addition & 0 deletions src/shared/ui/UIGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
export class UIGraph {
public nodes: { [key: GraphNodeUUID]: GraphNode } = {};
public edges: { [key: GraphAnchorUUID]: GraphEdge } = {};
public uiPositions: { [key: GraphNodeUUID]: SvelvetCanvasPos } = {};
public metadata: GraphMetadata = {
displayName: "Graph",
};
Expand Down Expand Up @@ -75,17 +76,17 @@
}

export function constructUIValueStore(ui: NodeUI, uiConfigs: { [key: string]: UIComponentConfig }) {
let res = new UIValueStore();

Check warning on line 79 in src/shared/ui/UIGraph.ts

View check run for this annotation

Codecov / codecov/patch

src/shared/ui/UIGraph.ts#L79

Added line #L79 was not covered by tests
if (ui.type === "parent") {
for (const child of ui.params) {
res = { ...res, ...constructUIValueStore(child, uiConfigs) };

Check warning on line 82 in src/shared/ui/UIGraph.ts

View check run for this annotation

Codecov / codecov/patch

src/shared/ui/UIGraph.ts#L81-L82

Added lines #L81 - L82 were not covered by tests
}
} else if (ui.type === "leaf") {
const leaf = ui as NodeUILeaf;
res.inputs[ui.label] = writable<UIValue>(uiConfigs[leaf.label].defaultValue);

Check warning on line 86 in src/shared/ui/UIGraph.ts

View check run for this annotation

Codecov / codecov/patch

src/shared/ui/UIGraph.ts#L85-L86

Added lines #L85 - L86 were not covered by tests
}

return res;

Check warning on line 89 in src/shared/ui/UIGraph.ts

View check run for this annotation

Codecov / codecov/patch

src/shared/ui/UIGraph.ts#L89

Added line #L89 was not covered by tests
}

export class GraphAnchor {
Expand Down
Loading
Loading