From 87b0016f47a1d906d3da1e0c63be0b54c3d65a62 Mon Sep 17 00:00:00 2001 From: SorsOps <80043879+SorsOps@users.noreply.github.com> Date: Tue, 17 Dec 2024 11:28:05 +0200 Subject: [PATCH] Fixes an issue with loading not working , relaxes restriction on requiring an input nodes as part of execution via options (#594) --- .changeset/large-peas-dance.md | 5 ++ packages/graph-engine/src/graph/graph.ts | 10 +++- .../graph-engine/src/programmatic/node.ts | 2 +- .../tests/suites/graph/execution.test.ts | 48 +++++++++++++++++++ .../tests/suites/graph/opts.test.ts | 27 +++++++++++ ....timestamp-1732108255012-05584d17095ba.mjs | 13 ----- 6 files changed, 89 insertions(+), 16 deletions(-) create mode 100644 .changeset/large-peas-dance.md create mode 100644 packages/graph-engine/tests/suites/graph/execution.test.ts create mode 100644 packages/graph-engine/tests/suites/graph/opts.test.ts delete mode 100644 packages/graph-engine/vitest.config.ts.timestamp-1732108255012-05584d17095ba.mjs diff --git a/.changeset/large-peas-dance.md b/.changeset/large-peas-dance.md new file mode 100644 index 000000000..2d9f14402 --- /dev/null +++ b/.changeset/large-peas-dance.md @@ -0,0 +1,5 @@ +--- +"@tokens-studio/graph-engine": patch +--- + +Fixes an issue with loading not working , relaxes restriction on requiring an input nodes as part of execution via options diff --git a/packages/graph-engine/src/graph/graph.ts b/packages/graph-engine/src/graph/graph.ts index b873b88e5..73c73638f 100644 --- a/packages/graph-engine/src/graph/graph.ts +++ b/packages/graph-engine/src/graph/graph.ts @@ -30,6 +30,11 @@ export type InputDefinition = { }; export type GraphExecuteOptions = { + /** + * Throws an error if the input/output nodes are not found + */ + strict?: boolean; + inputs?: Record; /** * Whether to track and emit stats as part of the execution @@ -149,6 +154,7 @@ export interface BatchExecution { const defaultGraphOpts: IGraph = { annotations: {} }; + /** * This is our internal graph representation that we use to perform transformations on */ @@ -643,7 +649,7 @@ export class Graph { const input = Object.values(this.nodes).find( x => x.factory.type === 'studio.tokens.generic.input' ); - if (!input) { + if (opts?.strict && !input) { throw new Error('No input node found'); } @@ -659,7 +665,7 @@ export class Graph { } //Its possible that there is no input with the name - input.inputs[key]?.setValue(value.value, opts); + input?.inputs[key]?.setValue(value.value, opts); }); } diff --git a/packages/graph-engine/src/programmatic/node.ts b/packages/graph-engine/src/programmatic/node.ts index 31b2cdde1..d4aeda13c 100644 --- a/packages/graph-engine/src/programmatic/node.ts +++ b/packages/graph-engine/src/programmatic/node.ts @@ -207,7 +207,7 @@ export class Node { * @param data */ async load(uri: string, data?: unknown) { - this._graph?.loadResource(uri, this, data); + return this._graph?.loadResource(uri, this, data); } get isRunning() { diff --git a/packages/graph-engine/tests/suites/graph/execution.test.ts b/packages/graph-engine/tests/suites/graph/execution.test.ts new file mode 100644 index 000000000..b359a4f9f --- /dev/null +++ b/packages/graph-engine/tests/suites/graph/execution.test.ts @@ -0,0 +1,48 @@ +import { Graph } from '../../../src/graph/graph.js'; +import { Node, NumberSchema } from '../../../src/index.js'; +import { describe, expect, test } from 'vitest'; + +class ExternalNode extends Node { + constructor(props) { + super(props); + + this.addOutput('foo', { type: NumberSchema }); + } + + async execute() { + const val = await this.load('foo'); + this.outputs.foo.set(val); + } +} + +describe('Graph/execution', () => { + test('throws an error when the external loader can be caught', async () => { + const graph = new Graph(); + + new ExternalNode({ id: 'a', graph }); + + let thrown; + try { + await graph.execute(); + } catch (err) { + thrown = err; + } finally { + expect(thrown).toBeDefined(); + expect(thrown.message).toEqual('No external loader specified'); + } + }); + + test('External loading works as expected', async () => { + const graph = new Graph(); + + graph.externalLoader = async () => { + return 5; + }; + + new ExternalNode({ id: 'a', graph }); + + await graph.execute(); + + expect(graph.nodes.a.outputs.foo.value).toEqual(5); + }); +}); diff --git a/packages/graph-engine/tests/suites/graph/opts.test.ts b/packages/graph-engine/tests/suites/graph/opts.test.ts new file mode 100644 index 000000000..01f9e3595 --- /dev/null +++ b/packages/graph-engine/tests/suites/graph/opts.test.ts @@ -0,0 +1,27 @@ +import { Graph } from '../../../src/graph/graph.js'; +import { describe, expect, test } from 'vitest'; +import Passthrough from '../../../src/nodes/generic/passthrough.js'; + +describe('Graph/options', () => { + test('throws an error if strict mode is enabled', async () => { + const graph = new Graph(); + + new Passthrough({ id: 'a', graph }); + let thrown; + try { + await graph.execute({ + strict: true, + inputs: { + foo: { + value: 1 + } + } + }); + } catch (err) { + thrown = err; + } finally { + expect(thrown).toBeDefined(); + expect(thrown.message).toEqual('No input node found'); + } + }); +}); diff --git a/packages/graph-engine/vitest.config.ts.timestamp-1732108255012-05584d17095ba.mjs b/packages/graph-engine/vitest.config.ts.timestamp-1732108255012-05584d17095ba.mjs deleted file mode 100644 index 0d463859c..000000000 --- a/packages/graph-engine/vitest.config.ts.timestamp-1732108255012-05584d17095ba.mjs +++ /dev/null @@ -1,13 +0,0 @@ -// vitest.config.ts -import { defineConfig } from "file:///C:/Users/Alex%20(Hyma)/Documents/GitHub/graph-engine/packages/graph-engine/node_modules/vite/dist/node/index.js"; -import tsconfigPaths from "file:///C:/Users/Alex%20(Hyma)/Documents/GitHub/graph-engine/node_modules/vite-tsconfig-paths/dist/index.mjs"; -var vitest_config_default = defineConfig({ - test: { - // ... Specify options here. - }, - plugins: [tsconfigPaths()] -}); -export { - vitest_config_default as default -}; -//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsidml0ZXN0LmNvbmZpZy50cyJdLAogICJzb3VyY2VzQ29udGVudCI6IFsiY29uc3QgX192aXRlX2luamVjdGVkX29yaWdpbmFsX2Rpcm5hbWUgPSBcIkM6XFxcXFVzZXJzXFxcXEFsZXggKEh5bWEpXFxcXERvY3VtZW50c1xcXFxHaXRIdWJcXFxcZ3JhcGgtZW5naW5lXFxcXHBhY2thZ2VzXFxcXGdyYXBoLWVuZ2luZVwiO2NvbnN0IF9fdml0ZV9pbmplY3RlZF9vcmlnaW5hbF9maWxlbmFtZSA9IFwiQzpcXFxcVXNlcnNcXFxcQWxleCAoSHltYSlcXFxcRG9jdW1lbnRzXFxcXEdpdEh1Ylxc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