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 🙏

© 2026 – Pkg Stats / Ryan Hefner

megahal

v1.0.1

Published

A JavaScript port of the MegaHAL conversational engine supporting both Node.js and Browser environments.

Readme

MegaHAL-JS

CI License: MIT

A JavaScript port of Jason Hutchens' famous 1998 MegaHAL conversational engine.

MegaHAL-JS runs natively in Node.js (>= 20) and in all modern browsers.


Installation

npm install megahal

Quick Start

Modern ESM (Node & Browser)

import { MegaHal } from 'megahal';

// Instantiate with order N (default is 5)
const hal = new MegaHal(5);

// Train the engine
hal.learn('The cat sat on the mat.');
hal.learn('The dog chased the cat around the yard.');

// Generate a reply (this will also learn from the prompt before generating)
const reply = hal.respond('Tell me about the cat.');
console.log(reply);
// e.g., "The dog chased the cat sat on the mat."

Browser direct integration (Vanilla HTML/JS)

<!DOCTYPE html>
<html>
<head>
  <title>MegaHAL Chatbot</title>
</head>
<body>
  <script type="module">
    import { MegaHal } from './node_modules/megahal/index.js';
    
    const hal = new MegaHal(3);
    hal.learn('Hello world!');
    hal.learn('Welcome to the web browser version of MegaHAL.');
    
    console.log(hal.respond('hello'));
  </script>
</body>
</html>

API Reference

class MegaHal

constructor(order = 5, rng = null)

Creates a new MegaHAL engine.

  • order: The Markov n-gram depth (trie depth). Defaults to 5.
  • rng: An optional custom random number generator. Must implement randomRange(min, max) returning an integer in [min, max). If omitted, defaults to Math.random.

respond(input)

Learns from the input sentence, extracts its keywords, generates a response biased toward those keywords, capitalizes the response according to sentence-casing rules, and returns it.

  • input: The prompt string.
  • Returns: string

generate(input)

Generates a response to the prompt without learning from it. Returns null if no reply can be generated.

  • input: The prompt string.
  • Returns: string | null

greet()

Generates an initial greeting using a random word selected from the greeting keywords list. Falls back to the default fallback greeting if no greeting can be generated.

  • Returns: string

learn(input)

Tokenizes and trains both forward and backward models on the given sentence.

  • input: Sentence to train on.

setLimit({ timeout, maxIterations })

Configures generation limits for the reply loop.

  • timeout: Maximum milliseconds to spend generating candidate responses (defaults to 1000).
  • maxIterations: Maximum candidates to generate.

setKeywordConfig(config)

Overrides the extraction config containing banned words, auxiliary words, and the swap table.

  • config: A KeywordConfig instance.

setGreetings(greetings)

Sets the keywords list used to seed the initial greeting.

  • greetings: Array of string greeting words.

exportBrain()

Serializes the engine's internal dictionary and tries into a spec-compliant C-compatible .brn binary format and returns a Uint8Array.

  • Returns: Uint8Array

importBrain(data)

Deserializes a binary brain from a Uint8Array or ArrayBuffer into the engine, restoring dictionary and tries.

  • data: Binary data buffer.

trainFromContent(content)

Trains the model on multi-line text corpus. Lines starting with # are ignored as comments.

  • content: Plain text string.

saveBrain(path) (Node-only)

Asynchronously saves the serialized binary brain to a file.

  • path: Target file path.

loadBrain(path) (Node-only)

Asynchronously loads a serialized binary brain from a file.

  • path: Source file path.

trainFromFile(path) (Node-only)

Asynchronously trains the model from a corpus file.

  • path: Text file path.

Quality & Verification Commands

# Run unit tests
npm test

# Run test coverage
npm run test:coverage

# Perform TypeScript check
npm run typecheck

# Build declaration types
npm run build:types

# Run ESLint linter
npm run lint

# Run Stryker Mutation Testing
npm run test:mutation

License

MIT © Tony Gies