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

@loopstack/dynamic-routing-example-workflow

v0.21.3

Published

A workflow demonstrating how to create dynamic routing an a workflow

Readme

@loopstack/dynamic-routing-example-workflow

A module for the Loopstack AI automation framework.

This module provides an example workflow demonstrating how to implement conditional routing based on runtime values using guards and transition priorities.

Overview

The Dynamic Routing Example Workflow shows how to create branching logic in workflows using @Guard decorators and @Transition priorities. It demonstrates how to route execution through different paths based on input values.

By using this workflow as a reference, you'll learn how to:

  • Define a workflow schema with z.object() for typed input arguments
  • Use @Guard('methodName') to conditionally gate transitions
  • Control transition evaluation order with the priority option
  • Build multi-level branching structures with fallback routes

This example is useful for developers building workflows that require decision trees, validation flows, or any logic that branches based on data.

Installation

See SETUP.md for installation and setup instructions.

How It Works

Workflow Class

The workflow extends BaseWorkflow<TArgs> with a typed argument object. The schema is defined in the @Workflow decorator using Zod:

@Workflow({
  uiConfig: __dirname + '/dynamic-routing-example.ui.yaml',
  schema: z
    .object({
      value: z.number().default(150),
    })
    .strict(),
})
export class DynamicRoutingExampleWorkflow extends BaseWorkflow<{ value: number }> {
  @InjectTool() createChatMessage: CreateChatMessage;

  value!: number;
}

Key Concepts

1. Receiving Input Arguments

The @Initial transition receives the validated input as a method argument. Store it as an instance property for use in guards and later transitions:

@Initial({ to: 'prepared' })
async createMockData(args: { value: number }) {
  this.value = args.value;
  await this.createChatMessage.call({
    role: 'assistant',
    content: `Analysing value = ${this.value}`,
  });
}

2. Guard Methods

A guard is a method that returns a boolean. It determines whether a transition should be taken. Reference it by name in the @Guard decorator:

@Transition({ from: 'prepared', to: 'placeA', priority: 10 })
@Guard('isAbove100')
routeToPlaceA() {}

isAbove100() {
  return this.value > 100;
}

When the workflow reaches the prepared state, it evaluates guards on all transitions from that state. If isAbove100() returns true, the workflow moves to placeA.

3. Transition Priority

When multiple transitions share the same from state, priority controls evaluation order (higher priority is evaluated first). The first transition whose guard passes (or that has no guard) is taken:

// Evaluated first (priority: 10) -- taken if value > 100
@Transition({ from: 'prepared', to: 'placeA', priority: 10 })
@Guard('isAbove100')
routeToPlaceA() {}

// Evaluated second (no priority = default) -- fallback route
@Transition({ from: 'prepared', to: 'placeB' })
routeToPlaceB() {}

A transition without a @Guard always matches, acting as a fallback.

4. Multi-Level Branching

Chain conditional transitions to create decision trees. After reaching placeA, a second level of guards routes further:

@Transition({ from: 'placeA', to: 'placeC', priority: 10 })
@Guard('isAbove200')
routeToPlaceC() {}

isAbove200() {
  return this.value > 200;
}

@Transition({ from: 'placeA', to: 'placeD' })
routeToPlaceD() {}

5. Complete Routing Flow

The workflow routes through different states based on the input value:

  • value <= 100 -> placeB -> "Value is less or equal 100"
  • 100 < value <= 200 -> placeA -> placeD -> "Value is less or equal 200, but greater than 100"
  • value > 200 -> placeA -> placeC -> "Value is greater than 200"

Complete Workflow

import { z } from 'zod';
import { BaseWorkflow, Final, Guard, Initial, InjectTool, Transition, Workflow } from '@loopstack/common';
import { CreateChatMessage } from '@loopstack/create-chat-message-tool';

@Workflow({
  uiConfig: __dirname + '/dynamic-routing-example.ui.yaml',
  schema: z
    .object({
      value: z.number().default(150),
    })
    .strict(),
})
export class DynamicRoutingExampleWorkflow extends BaseWorkflow<{ value: number }> {
  @InjectTool() createChatMessage: CreateChatMessage;

  value!: number;

  @Initial({ to: 'prepared' })
  async createMockData(args: { value: number }) {
    this.value = args.value;
    await this.createChatMessage.call({
      role: 'assistant',
      content: `Analysing value = ${this.value}`,
    });
  }

  @Transition({ from: 'prepared', to: 'placeA', priority: 10 })
  @Guard('isAbove100')
  routeToPlaceA() {}

  isAbove100() {
    return this.value > 100;
  }

  @Transition({ from: 'prepared', to: 'placeB' })
  routeToPlaceB() {}

  @Transition({ from: 'placeA', to: 'placeC', priority: 10 })
  @Guard('isAbove200')
  routeToPlaceC() {}

  isAbove200() {
    return this.value > 200;
  }

  @Transition({ from: 'placeA', to: 'placeD' })
  routeToPlaceD() {}

  @Final({ from: 'placeB' })
  async showMessagePlaceB() {
    await this.createChatMessage.call({
      role: 'assistant',
      content: 'Value is less or equal 100',
    });
  }

  @Final({ from: 'placeC' })
  async showMessagePlaceC() {
    await this.createChatMessage.call({
      role: 'assistant',
      content: 'Value is greater than 200',
    });
  }

  @Final({ from: 'placeD' })
  async showMessagePlaceD() {
    await this.createChatMessage.call({
      role: 'assistant',
      content: 'Value is less or equal 200, but greater than 100',
    });
  }
}

Dependencies

This workflow uses the following Loopstack modules:

  • @loopstack/common - Base classes, decorators, guards, and tool injection
  • @loopstack/create-chat-message-tool - Provides CreateChatMessage tool

About

Author: Jakob Klippel

License: Apache-2.0

Additional Resources