Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improved macro node #162

Merged
merged 23 commits into from
Oct 4, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Install Node.js
uses: actions/setup-node@v3
with:
node-version: 16
node-version: 20
- uses: pnpm/action-setup@v2
name: Install pnpm
id: pnpm-install
Expand Down
2 changes: 1 addition & 1 deletion core/src/execute/debugger/format-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function eventBody(event: DebuggerEvent) {
.map(([pinId, size]) => `${pinId}: ${size}`)
.join(", ")}`;
case DebuggerEventType.ERROR:
return `Error: ${event.val}`;
return `Error: ${JSON.stringify(event.val)}`;
}
}

Expand Down
13 changes: 12 additions & 1 deletion core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
export * from "./common";
import { Pos, OMap } from "./common";
import { FlydeFlow } from "./flow-schema";
import {
CustomNode,
VisualNode,
CustomNode,
InputPinsConfig,
Node,
NodeDefinition,
Expand Down Expand Up @@ -38,3 +39,13 @@ export interface NodeLibraryGroup {
export interface NodeLibraryData {
groups: NodeLibraryGroup[];
}

export type ImportablesResult = {
importables: Record<string, NodesDefCollection>;
errors: { path: string; message: string }[];
};

export interface FlowJob {
flow: FlydeFlow;
id: string;
}
172 changes: 115 additions & 57 deletions core/src/node/macro-node.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,106 @@
import { CodeNode, CodeNodeDefinition, NodeMetadata } from "./node";
import type React from "react";
import { MacroNodeInstance } from "./node-instance";

export type MacroEditorFieldDefinitionTypeString = {
value: "string";
export type MacroEditorFieldDefinitionType =
| "string"
| "number"
| "boolean"
| "json"
| "select"
| "longtext"
| "enum"
| "dynamic";

// Replace the conditional type with this mapped type
export type MacroConfigurableValueTypeMap = {
string: string;
number: number;
boolean: boolean;
json: any;
select: string | number;
dynamic: undefined;
};

export type MacroEditorFieldDefinitionTypeNumber = {
value: "number";
min?: number;
max?: number;
};
export type MacroConfigurableValue = {
[K in keyof MacroConfigurableValueTypeMap]: {
type: K;
value: MacroConfigurableValueTypeMap[K];
};
}[keyof MacroConfigurableValueTypeMap];

export function macroConfigurableValue(
type: MacroConfigurableValue["type"],
value: MacroConfigurableValue["value"]
): MacroConfigurableValue {
return { type, value };
}

export type MacroEditorFieldDefinitionTypeBoolean = {
value: "boolean";
};
export type MacroEditorFieldDefinition =
| StringFieldDefinition
| NumberFieldDefinition
| BooleanFieldDefinition
| JsonFieldDefinition
| SelectFieldDefinition
| LongTextFieldDefinition
| EnumFieldDefinition;

export type MacroEditorFieldDefinitionTypeJson = {
value: "json";
label?: string;
};
interface BaseFieldDefinition {
label: string;
description?: string;
configKey: string;
templateSupport?: boolean;
typeConfigurable?: boolean;
}

export type MacroEditorFieldDefinitionTypeSelect = {
value: "select";
items: { value: string | number; label: string }[];
};
export interface StringFieldDefinition extends BaseFieldDefinition {
type: "string";
}

export type MacroEditorFieldDefinitionTypeLongText = {
value: "longtext";
rows?: number;
};
export interface BooleanFieldDefinition extends BaseFieldDefinition {
type: "boolean";
}

export type MacroEditorFieldDefinitionTypeEnum = {
value: "enum";
options: string[];
};
export interface JsonFieldDefinition extends BaseFieldDefinition {
type: "json";
typeData?: {
helperText?: string;
};
}

export type MacroEditorFieldDefinitionType =
| MacroEditorFieldDefinitionTypeString
| MacroEditorFieldDefinitionTypeNumber
| MacroEditorFieldDefinitionTypeBoolean
| MacroEditorFieldDefinitionTypeJson
| MacroEditorFieldDefinitionTypeSelect
| MacroEditorFieldDefinitionTypeLongText
| MacroEditorFieldDefinitionTypeEnum;

export interface MacroEditorFieldDefinition {
label: string;
description?: string;
configKey: string;
allowDynamic: boolean;
type: MacroEditorFieldDefinitionType;
defaultValue?: any;
export interface LongTextFieldDefinition extends BaseFieldDefinition {
type: "longtext";
typeData?: {
rows?: number;
};
}

export interface NumberFieldDefinition extends BaseFieldDefinition {
type: "number";
typeData?: NumberTypeData;
}

export interface SelectFieldDefinition extends BaseFieldDefinition {
type: "select";
typeData: SelectTypeData;
}

export interface EnumFieldDefinition extends BaseFieldDefinition {
type: "enum";
typeData: EnumTypeData;
}

export interface NumberTypeData {
min?: number;
max?: number;
}

export interface SelectTypeData {
items: { value: string | number; label: string }[];
}

export interface EnumTypeData {
options: string[];
}

export interface MacroEditorConfigCustomResolved {
Expand Down Expand Up @@ -104,25 +154,11 @@ export type MacroNodeDefinition<T> = Omit<
export interface MacroEditorCompProps<T> {
value: T;
onChange: (value: T) => void;
prompt: (message: string) => Promise<string>;
}

export interface MacroEditorComp<T> extends React.FC<MacroEditorCompProps<T>> {}

/* helpers, used flow-editor macro editor builder */

export type ConfigurableInputStatic<T> = {
mode: "static";
value: T;
};

export type ConfigurableInputDynamic = {
mode: "dynamic";
};

export type ConfigurableInput<T> =
| ConfigurableInputStatic<T>
| ConfigurableInputDynamic;

export const isMacroNode = (p: any): p is MacroNode<any> => {
return p && typeof (p as MacroNode<any>).runFnBuilder === "function";
};
Expand All @@ -140,3 +176,25 @@ export const isMacroNodeDefinition = (
return editorConfig?.type === "structured";
}
};

export function processMacroNodeInstance(
namespace: string,
macro: MacroNode<any>,
instance: MacroNodeInstance
) {
const metaData = macro.definitionBuilder(instance.macroData);
const runFn = macro.runFnBuilder(instance.macroData);

const id = `${namespace}${macro.id}__${instance.id}`;

const resolvedNode: CodeNode = {
...metaData,
defaultStyle: metaData.defaultStyle ?? macro.defaultStyle,
displayName: metaData.displayName ?? macro.id,
namespace: macro.namespace,
id,
run: runFn,
};

return resolvedNode;
}
7 changes: 1 addition & 6 deletions dev-server/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import axios from "axios";
import {
FlydeFlow,
ImportableSource,
NodeLibraryData,
NodeLibraryGroup,
ResolvedFlydeFlowDefinition,
ImportablesResult,
} from "@flyde/core";
import { FolderStructure } from "./fs-helper/shared";
import type { ImportablesResult } from "./service/scan-importable-nodes";
export type { ImportablesResult } from "./service/scan-importable-nodes";

export * from "./runner/shared";

export const createDevServerClient = (baseUrl: string) => {
return {
Expand Down
1 change: 0 additions & 1 deletion dev-server/src/runner/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from "./shared";
export * from "./runFlow.host";
2 changes: 1 addition & 1 deletion dev-server/src/runner/runFlow.host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { fork } from "child_process";
import { join } from "path";
import { runFlow } from "./runFlow";
import { FlowJob } from "./shared";
import type { FlowJob } from "@flyde/core";
import { onMessage, sendMessage } from "./typedProcessMessage";

export function forkRunFlow(data: {
Expand Down
3 changes: 1 addition & 2 deletions dev-server/src/runner/runFlow.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { FlydeFlow } from "@flyde/core";
import { FlowJob } from "./shared";
import { FlowJob, FlydeFlow } from "@flyde/core";
import { createId } from "@paralleldrive/cuid2";
import { loadFlowFromContent } from "@flyde/runtime";

Expand Down
6 changes: 0 additions & 6 deletions dev-server/src/runner/shared.ts

This file was deleted.

2 changes: 1 addition & 1 deletion dev-server/src/runner/typedProcessMessage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ChildProcess } from "child_process";
import type { runFlow } from "./runFlow";
import { FlowJob } from "./shared";
import { FlowJob } from "@flyde/core";

export interface ProcessMessageEventsMap {
runFlow: Parameters<typeof runFlow>;
Expand Down
6 changes: 1 addition & 5 deletions dev-server/src/service/scan-importable-nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
isBaseNode,
isMacroNode,
NodesDefCollection,
ImportablesResult,
} from "@flyde/core";
import { scanFolderStructure } from "./scan-folders-structure";
import { FlydeFile } from "../fs-helper/shared";
Expand All @@ -25,11 +26,6 @@ export interface CorruptScannedNode {
error: string;
}

export type ImportablesResult = {
importables: Record<string, NodesDefCollection>;
errors: { path: string; message: string }[];
};

export async function scanImportableNodes(
rootPath: string,
filename: string
Expand Down
3 changes: 1 addition & 2 deletions editor/src/integrated-flow-manager/IntegratedFlowManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { FlowEditor } from "@flyde/flow-editor"; // ../../common/flow-editor/Flo
import { useDebouncedCallback } from "use-debounce";

import { IntegratedFlowSideMenu } from "./side-menu";
import { NodeDefinition } from "@flyde/core";
import { NodeDefinition, ImportablesResult } from "@flyde/core";

import { AppToaster } from "@flyde/flow-editor"; // ../../common/toaster

Expand All @@ -49,7 +49,6 @@ import { useState } from "react";
import { useEffect } from "react";
import _ from "lodash";
import { useBootstrapData } from "./use-bootstrap-data";
import type { ImportablesResult } from "@flyde/dev-server";

export const PIECE_HEIGHT = 28;

Expand Down
2 changes: 1 addition & 1 deletion flow-editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"@blueprintjs/icons": "^5.1.5",
"@blueprintjs/select": "^5.0.1",
"@flyde/core": "workspace:*",
"@flyde/dev-server": "workspace:*",
"@flyde/remote-debugger": "workspace:*",
"@flyde/stdlib": "workspace:*",
"@fortawesome/fontawesome-svg-core": "^6.4.2",
"@fortawesome/free-brands-svg-icons": "^6.4.2",
"@fortawesome/free-solid-svg-icons": "^6.4.2",
Expand Down
2 changes: 1 addition & 1 deletion flow-editor/src/flow-editor/DependenciesContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {
ImportableSource,
Pos,
NodeLibraryData,
ImportablesResult,
} from "@flyde/core";
import type { ImportablesResult } from "@flyde/dev-server";
import { createContext, useContext } from "react";

// TODO - merge this interface with the one from the dev-server
Expand Down
3 changes: 2 additions & 1 deletion flow-editor/src/flow-editor/ports/ports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import {
NodeLibraryData,
ResolvedDependenciesDefinitions,
noop,
FlowJob,
ImportablesResult,
} from "@flyde/core";
import { FlowJob, ImportablesResult } from "@flyde/dev-server";
import { ReportEvent } from "./analytics";
import { toastMsg } from "../../toaster";

Expand Down
1 change: 1 addition & 0 deletions flow-editor/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ export * from "./physics";
export * from "./flow-editor/base-node-editor";
export * from "./visual-node-editor/utils";
export * from "./lib/logger";
export * from "./visual-node-editor/MacroInstanceEditor/SimpleJsonEditor";
Loading
Loading