UNPKG

@tokens-studio/graph-engine

Version:

An execution engine to handle Token Studios generators and resolvers

160 lines 4.94 kB
import { Graph } from '../graph/index.js'; import { GraphSchema } from '../schemas/index.js'; import { IDeserializeOpts, SerializedNode } from '../graph/types.js'; import { Input } from './input.js'; import { Output } from './output.js'; import type { NodeRun } from '../types.js'; export interface ISubgraphContainer { getSubgraphs(): Graph[]; getGraphProperties(): Record<string, Graph | undefined>; } export declare function isSubgraphContainer(node: unknown): node is Node & ISubgraphContainer; export interface INodeDefinition { graph: Graph; id?: string; inputs?: Record<string, Input>; outputs?: Record<string, Output>; annotations?: Record<string, unknown>; } export interface TypeDefinition { type: GraphSchema; /** * When exposing an array of inputs or outputs, allow specific control for connecting each item */ variadic?: boolean; /** * Whether the input is visible by default in the UI */ visible?: boolean; /** * Additional annotations to store on the input */ annotations?: Record<string, unknown>; } type Prettify<T> = { [K in keyof T]: T[K]; } & {}; type InputValue<T> = T extends Input<infer U> ? U : never; type UnwrapInput<T> = Prettify<{ [K in keyof T]: InputValue<T[K]>; }>; export declare class Node { /** * Unique instance specific identifier */ readonly id: string; static readonly description?: string; static readonly title?: string; static readonly annotations: Record<string, unknown>; /** * The groups this node belongs to as a string array */ static groups?: string[]; static readonly type: string; /** * This holds the definitions of the inputs and outputs */ inputs: Record<string, Input>; outputs: Record<string, Output>; annotations: Record<string, unknown>; lastExecutedDuration: number; private _graph; error: Error | null; constructor(props: INodeDefinition); /** * Creates a new input and adds it to the node * @param name * @param input */ addInput<T = unknown>(name: string, type: TypeDefinition): Input<T>; addOutput<T = unknown>(name: string, type: TypeDefinition): void; setAnnotation(key: string, value: unknown): void; /** * Removes a named input from the node. This should only be used for dynamic inputs * @param name */ removeInput(name: string): void; removeOutput(name: string): void; /** * This is the place to add applicate specific logic to execute the node. * This can be used directly, but you should preferably never call this and instead execute from the graph which will control the lifecycle of the node * @override */ execute(): Promise<void> | void; /** * Runs the node. Internally this calls the execute method, but the run entrypoint allows for additional tracking and lifecycle management */ run(): Promise<NodeRun>; /** * Asks the controlling graph to load a resource. * This cannot be called if the node is not part of a graph * @param uri * @param data */ load(uri: string, data?: unknown): Promise<any>; get isRunning(): boolean; /** * Clears all the outputs */ clearOutputs(): void; setGraph(graph: Graph): void; getGraph(): Graph; clone(newGraph: Graph): Node; /** * Get the type of the nodes * @returns */ nodeType: () => any; /** * Returns the underlying class of the node. Useful for getting class specific properties * @returns */ get factory(): typeof Node; /** * Serializes the node value for storage * @returns */ serialize(): SerializedNode; /** * How to deserialize the node * @param serialized * @returns */ static deserialize(opts: IDeserializeOpts): Promise<Node>; static getType: () => string; getAllInputs: () => UnwrapInput<this["inputs"]>; /** * Handles cleanup for nodes with state. * Use the super method to clear the graph reference * * @example * ```typescript * class MyNode extends Node { * dispose() { * * Node.prototype.dispose.call(this); * // or if you have full ES6 support * super.dispose(); * * //Some additional manual cleanup * // ... * } */ dispose: () => void; /** * Function to call when the graph has been started. * This is only really necessary for nodes that need to do something when the graph is expected to be running continuously */ onStart: () => void; onStop: () => void; /** * By default, the node will stop when the graph is paused */ onPause: () => void; /** * By default, the node will start when the graph is resumed */ onResume: () => void; } export {}; //# sourceMappingURL=node.d.ts.map