npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

nodejs-swarm-openai

v1.0.11

Published

Test implementation of OpenAI Swarm for Node.js — a multi-agent system with support for finalization, custom agents, and asynchronous tools.

Readme

nodejs-swarm-openai

nodejs-swarm-openai is an experimental Node.js SDK for orchestrating multi-agent conversations using OpenAI's models (GPT-4o, GPT-4, GPT-3.5). This library is a custom test implementation built from scratch, featuring support for finalizer agents, async tools, and improved error handling. It is intended for learning, prototyping, and exploring the principles behind Swarm-like coordination.

✅ NPM


✅ Features

  • ✨ Orchestrates multiple agents (chatbots) with distinct roles and instructions.
  • 🔁 Automatic tool call handling via OpenAI function calling.
  • 🧠 Optional finalizer agent to generate a unified final message.
  • 🔧 Supports async tool functions (e.g., DB queries).
  • 🐛 Safe internal error handling (errors never leak to user).
  • 🧪 Built-in debug mode for developer insight.
  • 🌐 Proxy support.

🔧 Installation

npm install nodejs-swarm-openai

📦 Usage

import { Agent, Swarm, printMessagesPretty, getFinalAnswer } from 'nodejs-swarm-openai';

const needsAnalyst = new Agent({
  name: 'needs_analyst',
  description: 'Understands customer needs',
  instructions: 'Identify what matters most to the customer: budget, quality, speed, etc.',
  model: 'gpt-4o',
  tools: []
});

const positioningAgent = new Agent({
  name: 'positioning',
  description: 'Describes company strengths',
  instructions: 'Mention experience, materials, guarantees, and successful projects.',
  model: 'gpt-4o',
  tools: []
});

const sellerAgent = new Agent({
  name: 'seller',
  description: 'Invites to action',
  instructions: 'Encourage user to leave a phone number or schedule a call.',
  model: 'gpt-4o',
  tools: []
});

const routerAgent = new Agent({
  name: 'manager',
  description: 'Manages tasks and delegates to agents',
  instructions: `
You are a Swarm manager. Given a customer request, decide which agent to invoke:

- needs_analyst — to analyze customer needs;
- positioning — to describe the company;
- seller — to invite to next step.

Always respond with tool_call only. Never reply directly.`,
  model: 'gpt-4o'
});

const swarm = new Swarm(process.env.OPENAI_API_KEY, {
  proxyUrl: process.env.PROXY_GPT // Optional proxy support
});

(async () => {
  const { messages } = await swarm.run({
    routerAgent,
    agents: [needsAnalyst, positioningAgent, sellerAgent],
    finalizerAgent: new Agent({ //Optional
    name: 'my_finalizer',
    description: 'Custom summary',
    instructions: 'Merge replies into 1 final answer. Use polite tone.',
    model: 'gpt-4o',
    tools: []
  }),
    messages: [
      { role: 'user', content: 'I want a cheap and high-quality wooden sauna' }
    ],
    maxTurns: 5,
    debug: true // Optional
  });

  printMessagesPretty(messages);
  const final = getFinalAnswer(messages);
  console.log('\nFinal Answer:\n', final || 'No final message.');
})();

✅ Use Cases

  • Multi-step sales dialogs
  • Lead qualification
  • Information extraction and synthesis
  • Decision trees based on roles

🛠 Advanced

Finalizer agent

By default, the library adds a final agent to summarize all assistant replies into one. You can override it:

const swarm = new Swarm(apiKey);
swarm.run({
  finalizerAgent: new Agent({
    name: 'my_finalizer',
    description: 'Custom summary',
    instructions: 'Merge replies into 1 final answer. Use polite tone.',
    model: 'gpt-4o',
    tools: []
  }),
  ...
});

Spent tokens

const result = await swarm.run({
  routerAgent,
  agents: [needsAnalyst, positioningAgent, sellerAgent],
  finalizerAgent,
  messages: [
    { role: 'user', content: 'I want a cheap and high-quality wooden sauna' }
  ],
  maxTurns: 6,
  debug: false
});
console.log(result.totalTokens)
{
  messages:[...]
  tokenUsage: {
    manager: 1797,
    needs_analyst: 262,
    positioning: 415,
    seller: 579,
    finalizer: 595
  },
  totalTokens: 3648
}

Proxy

Pass a proxy URL using proxyUrl option:

const swarm = new Swarm(apiKey, {
  proxyUrl: 'http://127.0.0.1:8080'
});

This SDK is part of Neurounit AI — my personal platform that helps businesses leverage artificial intelligence for automated lead generation.
I develop this project in my spare time.

💜 Support

If you'd like to support this project, you can:

  • Donate via TRC20: TBJWkKyrQEmG1dr8rd3psDwWY4tEvUFnzw
  • Mention it on social media 🙌
  • Star ⭐ the repo to show appreciation