Can I use custom actions as functions within a Node.js code step in my workflow?

This topic was automatically generated from Slack. You can find the original thread here.

Hey guys, I’m wondering if it’s possible to use my custom actions inside of a Node.js code step in my workflows? So instead of using it as a step in the workflow, I want to just run the action as a function inside a spesific nodejs code step.

For example, I’ve created a custom GPT action component and I would love to use this as a normal function in my nodejs code steps. So basically being able to import it as

import {gpt} from "my-actions" (or take it in as a prop in the given code step)

Then to just run it with the defined parameters

gpt("This is a prompt", "gpt-4")

In your examples on github, e.g. pipedream/prettify-json.mjs at master · PipedreamHQ/pipedream · GitHub I can see you’re using a helper_functions a lot, importing it from "../../helper_functions.app.mjs and passing it in as props, but I don’t quite understand how this would work in a workflow.

Sorry if this is a duplicate question. I would think this is a really common usecase, so I’m a bit confused as to not have found any info on it. (edited)

Hi , I think your question is a great case for Pipedream bot to help. I’ll pass your question to the Pipedream bot now. Please feel free to ask it any question

Hey guys, I’m wondering if it’s possible to use my custom actions inside of a Node.js code step in my workflows? So instead of using it as a step in the workflow, I want to just run the action as a function inside a spesific nodejs code step.

For example, I’ve created a custom GPT action component and I would love to use this as a normal function in my nodejs code steps. So basically being able to import it as

import {gpt} from "my-actions" (or take it in as a prop in the given code step)

Then to just run it with the defined parameters

gpt("This is a prompt", "gpt-4")

In your examples on github, e.g. pipedream/prettify-json.mjs at master · PipedreamHQ/pipedream · GitHub I can see you’re using a helper_functions a lot, importing it from "../../helper_functions.app.mjs and passing it in as props, but I don’t quite understand how this would work in a workflow.

Sorry if this is a duplicate question. I would think this is a really common usecase, so I’m a bit confused as to not have found any info on it.

Currently, you cannot directly import custom actions as functions within a Node.js code step in Pipedream workflows. Custom actions are designed to be used as separate steps in the workflow. However, you can achieve a similar result by modularizing your custom action’s logic into a separate function and then importing that function into both your custom action and your Node.js code step. You would need to host the shared function in a separate file, for example, in a GitHub repository, and then use an HTTP request to fetch the function’s code and execute it within your Node.js code step. Here’s an example of how you can fetch and execute a shared function from a remote file: 1. Host your shared function in a file, for example, shared-function.js in a GitHub repository. 2. In your Node.js code step, use the following code to fetch and execute the shared function: ```
const axios = require(“axios”); (async () => { // Replace the URL with the raw URL of your shared-function.js file on GitHub const url = “https://raw.githubusercontent.com/yourusername/yourrepo/main/shared-function.js”; const response = await axios.get(url); const sharedFunction = new Function("return " + response.data)(); // Call the shared function with your parameters const result = sharedFunction(“This is a prompt”, “gpt-4”); console.log(result); })();

Hi , the example by the bot is a little bit misleading. Instead of hosting your function code on Github, you can publish it on npm, an call it just like any other package

your example of hosting the function on github is invalid. The user will need to publish the action to npm

You’re right, publishing the shared function to npm is a better approach. Here’s how you can do that: 1. Create a new npm package for your shared function. You can follow the npm documentation on creating a Node.js module. 2. Publish your package to npm by following the npm documentation on publishing packages. 3. In your Pipedream Node.js code step, you can now install and use your published package: ```
// Import your shared function from your published npm package const { gpt } = require(“my-actions”); (async () => { // Call the shared function with your parameters const result = await gpt(“This is a prompt”, “gpt-4”); console.log(result); })();

the above answer is a better answer

Thank you! I was thinking maybe there’s a way to make it more integrated. Like, you could use secrets directly in the action and it would just work in the workflow. But now I think this way is actually better because then my custom actions won’t be limited to this platform, but I can use it wherever.

Awesome bot by the way, super cool!

There’s an alpha feature that allow you to build a reusable action here if you want to explore: Sharing code across workflows.

It does not allow you to import the action directly to your Node.js code, but it allow you to use the action across workflows

By the way, does the response to Mario mechanism simply request a new gpt response or is there some reinforcement learning something else going on?

does the response to Mario mechanism simply request a new gpt response or is there some reinforcement learning something else going on?
It’s like chatting with ChatGTP

Yeah thats what I have been looking at. Did not realise its an alpha feature. I recently discovered Val Town (https://www.val.town/) and from there discovered pipedream. On there its a core feature that you can reuse your own vals, or other peoples public vals

okay

We do a few things to add Pipedream knowledge to Mario. Writing a blog post about it now and will share!

Super cool! Really looking forward to this being a thing all products offer

Re: your original question, you can also import any methods that we expose in the app file for every app. Packages are namespaced to @pipedream, and named for the app. e.g @pipedream/rss, @pipedream/google_sheets

How do I use a method in an app file in a Node.js component? Show me an example where I import a “listItems” method from the @pipedream/rss package.

(On my phone, that method is fake, just an example)