feat(server): enhance script execution and workflow management
- Introduced compileWorkflowScripts function to parse and validate workflow scripts, supporting both linear and DAG execution modes. - Added runLinearSteps and runDagSteps functions for executing compiled workflows based on their structure. - Updated createRegistry function to utilize the new workflow compilation and execution logic. - Enhanced script sandbox with meta extraction and instantiation capabilities for better script management. - Improved API endpoints to return script metadata and handle errors more effectively. - Removed deprecated scripts and updated workflows to reflect new structure and functionality.
This commit is contained in:
@@ -1,4 +0,0 @@
|
||||
export default function main(context) {
|
||||
log.info({ data: context.data }, "add-one");
|
||||
return { data: (context.data || 0) + 1 };
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
export default function main(context) {
|
||||
log.info({ input: context.input }, "add-ten");
|
||||
return context.input + 10;
|
||||
}
|
||||
@@ -37,7 +37,7 @@ function resolveUrl(ctx) {
|
||||
return null;
|
||||
}
|
||||
|
||||
export default async function fetchBinary(ctx) {
|
||||
async function fetchBinary(ctx) {
|
||||
ensureDataObject(ctx);
|
||||
|
||||
const url = resolveUrl(ctx);
|
||||
@@ -68,3 +68,29 @@ export default async function fetchBinary(ctx) {
|
||||
|
||||
return ctx;
|
||||
}
|
||||
|
||||
fetchBinary.meta = {
|
||||
description: "Download a binary URL into ctx.data as a Buffer",
|
||||
config: {
|
||||
url: { type: "string", required: false, description: "Direct download URL" },
|
||||
urlVar: { type: "string", required: false, description: "Key in ctx.data that holds the URL" },
|
||||
outputVar: { type: "string", default: "file", description: "ctx.data key for the Buffer" },
|
||||
filename: { type: "string", required: false, description: "Override saved filename" },
|
||||
},
|
||||
input: {
|
||||
attach: { type: "string", required: false, description: "Fallback URL" },
|
||||
url: { type: "string", required: false, description: "Fallback URL" },
|
||||
filename: { type: "string", required: false },
|
||||
},
|
||||
output: {
|
||||
file: { type: "buffer", description: "Downloaded bytes (or ctx.config.outputVar)" },
|
||||
filename: { type: "string" },
|
||||
contentType: { type: "string" },
|
||||
},
|
||||
example: {
|
||||
data: { attach: "https://example.com/image.png" },
|
||||
config: { outputVar: "file" },
|
||||
},
|
||||
};
|
||||
|
||||
export default fetchBinary;
|
||||
|
||||
@@ -29,7 +29,7 @@ function previewValue(value) {
|
||||
return { preview: `${json.slice(0, 500)}...`, truncated: true };
|
||||
}
|
||||
|
||||
export default async function fetchHtml(ctx) {
|
||||
async function fetchHtml(ctx) {
|
||||
const url = ctx.config?.url;
|
||||
if (typeof url !== "string" || url.length === 0) {
|
||||
throw new Error("fetch-html: ctx.config.url is required");
|
||||
@@ -83,3 +83,31 @@ export default async function fetchHtml(ctx) {
|
||||
|
||||
return ctx;
|
||||
}
|
||||
|
||||
fetchHtml.meta = {
|
||||
description: "Fetch HTML and optionally select elements or transform with JSONata",
|
||||
config: {
|
||||
url: { type: "string", required: true, description: "Page URL" },
|
||||
selector: { type: "string", required: false, description: "CSS selector" },
|
||||
outputVar: {
|
||||
type: "string",
|
||||
required: false,
|
||||
description: "Required when selector or jsonata is set",
|
||||
},
|
||||
jsonata: { type: "string", required: false, description: "JSONata applied to selector matches" },
|
||||
},
|
||||
input: {},
|
||||
output: {
|
||||
httpResponse: { type: "string", description: "Raw HTML" },
|
||||
},
|
||||
example: {
|
||||
data: {},
|
||||
config: {
|
||||
url: "https://example.com/",
|
||||
selector: "h1",
|
||||
outputVar: "headings",
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export default fetchHtml;
|
||||
|
||||
@@ -1,10 +1,26 @@
|
||||
// this script will get current time
|
||||
|
||||
export default function getCurrentTime() {
|
||||
function getCurrentTime() {
|
||||
return {
|
||||
data: {
|
||||
datetime: new Date().toISOString(),
|
||||
processId: "1234"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getCurrentTime.meta = {
|
||||
description: "Return the current time as ctx.data.datetime",
|
||||
config: {},
|
||||
input: {},
|
||||
output: {
|
||||
datetime: { type: "string", description: "ISO timestamp" },
|
||||
processId: { type: "string" },
|
||||
},
|
||||
example: {
|
||||
data: {},
|
||||
config: {},
|
||||
},
|
||||
};
|
||||
|
||||
export default getCurrentTime;
|
||||
|
||||
@@ -1,10 +1,27 @@
|
||||
import jsonata from "jsonata";
|
||||
|
||||
export default function jsonataFn(ctx) {
|
||||
function jsonataFn(ctx) {
|
||||
log.info({ctx}, "jsonata: context")
|
||||
log.info("jsonata: evaluating expression %s", ctx.config.expression);
|
||||
const expression = jsonata(ctx.config.expression);
|
||||
const result = expression.evaluate(ctx.data);
|
||||
log.info({result}, "jsonata: expression result");
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
jsonataFn.meta = {
|
||||
description: "Evaluate a JSONata expression against ctx.data and return the result as the next context",
|
||||
config: {
|
||||
expression: { type: "string", required: true, description: "JSONata expression" },
|
||||
},
|
||||
input: {},
|
||||
output: {},
|
||||
example: {
|
||||
data: { title: "Hello", url: "https://example.com" },
|
||||
config: {
|
||||
expression: '{"data": {"title": title, "message": title, "attach": url}}',
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export default jsonataFn;
|
||||
|
||||
@@ -9,7 +9,7 @@ function ntfyHeaders(ctx) {
|
||||
return headers;
|
||||
}
|
||||
|
||||
export default async function ntfy(ctx) {
|
||||
async function ntfy(ctx) {
|
||||
const file = ctx.data?.file;
|
||||
const hasFile = Buffer.isBuffer(file) || file instanceof Uint8Array;
|
||||
|
||||
@@ -64,3 +64,31 @@ export default async function ntfy(ctx) {
|
||||
});
|
||||
return { sent: "true" };
|
||||
}
|
||||
|
||||
ntfy.meta = {
|
||||
description: "Send a message or file to an ntfy topic",
|
||||
config: {
|
||||
url: {
|
||||
type: "string",
|
||||
default: "https://ntfy.sh/scrunner",
|
||||
description: "ntfy topic URL",
|
||||
},
|
||||
},
|
||||
input: {
|
||||
title: { type: "string", required: false },
|
||||
message: { type: "string", required: false },
|
||||
attach: { type: "string", required: false, description: "Remote attachment URL" },
|
||||
file: { type: "buffer", required: false, description: "Binary body to PUT" },
|
||||
filename: { type: "string", required: false },
|
||||
contentType: { type: "string", required: false },
|
||||
},
|
||||
output: {
|
||||
sent: { type: "string", description: "Replaces the workflow context with { sent: \"true\" }" },
|
||||
},
|
||||
example: {
|
||||
data: { title: "Hello", message: "Hello from scrunner" },
|
||||
config: { url: "https://ntfy.sh/scrunner" },
|
||||
},
|
||||
};
|
||||
|
||||
export default ntfy;
|
||||
|
||||
Reference in New Issue
Block a user