- Introduced a new contract for script returns, requiring an object with `output`, `context`, and `skipRemaining` fields. - Refactored existing scripts to align with the new return structure, ensuring compatibility with the updated context management. - Added utility functions for normalizing context and step results, improving the handling of script execution context. - Updated various scripts (e.g., fetch-binary, fetch-html, fetch-http) to return structured output and context, enhancing data flow and usability. - Improved error handling for script returns, ensuring clearer feedback when invalid structures are returned. This update significantly enhances the flexibility and clarity of script interactions within workflows.
68 lines
1.8 KiB
JavaScript
68 lines
1.8 KiB
JavaScript
import jsonata from "jsonata";
|
|
|
|
function passContext(ctx) {
|
|
if (ctx?.context != null && typeof ctx.context === "object" && !Array.isArray(ctx.context)) {
|
|
return { ...ctx.context };
|
|
}
|
|
return {};
|
|
}
|
|
|
|
async function triggerWorkflow(ctx) {
|
|
const name = ctx.config?.name;
|
|
if (typeof name !== "string" || name.length === 0) {
|
|
throw new Error("config.name is required");
|
|
}
|
|
|
|
let data = ctx.data;
|
|
const expression = ctx.config?.expression;
|
|
if (typeof expression === "string" && expression.length > 0) {
|
|
const result = jsonata(expression).evaluate(ctx);
|
|
data = await result;
|
|
}
|
|
|
|
const started = await $workflows.trigger(name, data);
|
|
return {
|
|
output: { name, runId: started?.runId ?? null },
|
|
context: passContext(ctx),
|
|
};
|
|
}
|
|
|
|
triggerWorkflow.meta = {
|
|
description:
|
|
"Fire-and-forget another workflow by YAML name (same owner). Destination must declare triggers: [{ type: workflow }]. Optionally reshape the destination input with JSONata against full ctx.",
|
|
previewConfigKey: "name",
|
|
tags: ["trigger"],
|
|
reads: "ctx",
|
|
config: {
|
|
name: {
|
|
type: "string",
|
|
required: true,
|
|
description: "Destination workflow YAML name (same owner)",
|
|
},
|
|
expression: {
|
|
type: "string",
|
|
required: false,
|
|
description:
|
|
"Optional JSONata expression evaluated against ctx; result becomes the destination run input",
|
|
},
|
|
},
|
|
input: {},
|
|
output: {
|
|
name: { type: "string", description: "Destination workflow name" },
|
|
runId: { type: "string", description: "Started run id (null if the destination failed to start)" },
|
|
},
|
|
example: {
|
|
data: {
|
|
title: "Hello",
|
|
url: "https://example.com/img.png",
|
|
},
|
|
config: {
|
|
name: "notify-comic",
|
|
expression:
|
|
'{ "title": data.title, "message": data.title, "attach": data.url }',
|
|
},
|
|
},
|
|
};
|
|
|
|
export default triggerWorkflow;
|