Skip to content
Vitor Lima edited this page Mar 19, 2026 · 6 revisions

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.

Modes

Script Mode

Your code runs immediately when the workflow reaches this node. The return value flows to the next connected node.

Tool Mode

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.

Inputs

Input Type Required Description
Parameters Any No Data from connected nodes. Accepts multiple connections.

Outputs

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.

Configuration

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.

The input Variable

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.

Available APIs

stores.chat

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.

stores.characters

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.

stores.lorebook

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.

stores.models

Work with model configurations.

  • fetchModels(filter?) — Load models.
  • getModelById(id) — Get a model by ID.

utils

Helper functions.

  • utils.delay(ms) — Pause execution.
  • utils.jsonParse(text) — Safely parse JSON (returns null on failure).
  • utils.jsonStringify(value) — Convert a value to JSON string.

console

Log to the Chat Live Inspector's Logs tab.

  • console.log(...) / console.warn(...) / console.error(...) / console.info(...)

Examples

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 });

Tips

  • Always await store calls. They return Promises. Forgetting await means your code continues before data arrives.
  • Your code must return a 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.

Clone this wiki locally