-
-
Notifications
You must be signed in to change notification settings - Fork 5
Javascript Node
The JavaScript node lets you run custom code inside your agent workflow. Use it for data transformation, calculations, querying stores, or exposing custom tools to the AI.
Your code runs immediately when the workflow reaches this node. The return value flows to the next connected node.
Your code is wrapped as a callable tool that an LLM Agent can invoke. You define an Input Schema so the AI knows what arguments to pass. Connect the Toolset output to an LLM Agent's Toolset input.
| Input | Type | Required | Description |
|---|---|---|---|
| Parameters | Any | No | Data from connected nodes. Accepts multiple connections. |
| Output | Mode | Description |
|---|---|---|
| String | Script | The return value of your code (converted to a string). |
| Toolset | Tool | A tool definition to connect to an LLM Agent. |
Click the settings icon to open the code editor:
- Mode — Script or Tool.
-
Code — Your JavaScript code. Runs as an async function, so you can use
await. - Input Schema (Tool mode only) — A JSON Schema that describes the parameters the AI can pass to your tool. The schema's title becomes the tool name in the AI's tool list.
Your code has access to an input variable:
| Scenario |
input contains |
|---|---|
| Single connection | The raw value from that node. |
| Multiple connections | An object with named keys based on the source node outputs. |
| No connection | undefined |
| Tool mode | An object matching your Input Schema. |
Interact with chats, messages, chapters, memories, and participants.
-
fetchChatMessages(chatId?, chapterId?)— Get messages from the current or specified chat. -
addChatMessage(params)— Add a message to the current chat. -
updateChatMessage(messageId, params)— Update a message. -
deleteChatMessage(messageId)— Delete a message. -
fetchChatChapters(chatId)— Get all chapters for a chat. -
addChatChapter(params)/updateChatChapter(id, params)/deleteChatChapter(id)— Manage chapters. -
addParticipant(participant)/removeParticipant(id)/updateParticipant(id, data)— Manage participants. -
updateShortMemory(role, content)/getShortMemoryContent(role)— Read and write short-term memory.
Work with characters.
-
getCharacterById(id)— Get a character by ID. -
fetchCharacters(filter?)— Load characters. Filter by type, name, page, limit. -
updateCharacter(id, data)/createCharacter(data)/deleteCharacter(id)— Manage characters.
Manage lorebooks and their entries.
-
loadLorebooks()— Load all lorebooks. -
loadLorebookEntries(lorebookId)— Get entries for a lorebook. -
createLorebookEntry(params)/updateLorebookEntry(id, params)/deleteLorebookEntry(id, lorebookId)— Manage entries.
Work with model configurations.
-
fetchModels(filter?)— Load models. -
getModelById(id)— Get a model by ID.
Helper functions.
-
utils.delay(ms)— Pause execution. -
utils.jsonParse(text)— Safely parse JSON (returnsnullon failure). -
utils.jsonStringify(value)— Convert a value to JSON string.
Log to the Chat Live Inspector's Logs tab.
-
console.log(...)/console.warn(...)/console.error(...)/console.info(...)
Get a character's personality:
const character = await stores.characters.getCharacterById(String(input));
if (!character) return "Character not found";
return character.custom?.personality ?? "";Filter and delete blank messages:
const messages = await stores.chat.fetchChatMessages();
const blank = messages.filter(m => !m.messages[m.message_index]?.trim());
for (const msg of blank) {
await stores.chat.deleteChatMessage(msg.id);
}
return `Deleted ${blank.length} blank messages`;Dice roll (Tool mode):
const { attribute = 10, difficulty = 15, modifier = 0 } = input ?? {};
const roll = Math.floor(Math.random() * 20) + 1;
const attrMod = Math.floor((attribute - 10) / 2);
const total = roll + attrMod + modifier;
const success = total >= difficulty;
return utils.jsonStringify({ roll, total, difficulty, success });-
Always
awaitstore calls. They return Promises. Forgettingawaitmeans your code continues before data arrives. -
Your code must
returna value to produce output. If you return nothing, the output is an empty string. -
In Tool mode, always return a string. Use
utils.jsonStringify()to convert objects. - Use the Snippets button in the code editor to insert ready-made examples.
-
Use
console.log()to debug — check the Live Inspector's Logs tab to see your output.