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

ai-sdk-graph

v0.6.0

Published

Graph-based workflows for the AI SDK

Readme

ai-sdk-graph

A TypeScript library for building stateful, resumable workflows with human-in-the-loop support.

Features

  • Human in the Loop — Suspend execution to wait for user input, approvals, or external data, then resume seamlessly
  • Type-safe — Full TypeScript support with generic state management
  • Resumable — Checkpoint and resume execution from any point
  • Composable — Nested subgraphs with state mapping
  • Parallel Execution — Fork workflows with multiple edges from a single node
  • Conditional Routing — Dynamic edges based on state
  • Visualization — Generate Mermaid diagrams of your workflows

Installation

npm i ai-sdk-graph

Quick Start

import { graph } from 'ai-sdk-graph'

const workflow = graph<{ value: number }>()
  .node('validate', ({ update }) => {
    update({ value: 10 })
  })
  .node('transform', ({ update, state }) => {
    update({ value: state().value * 2 })
  })
  .edge('START', 'validate')
  .edge('validate', 'transform')
  .edge('transform', 'END')

// Execute the workflow
const stream = workflow.execute('run-1', { value: 0 })

Core Concepts

Nodes

Nodes are execution units that receive a context object with:

  • state() — Read the current state
  • update(changes) — Update state with partial object or function
  • suspense(data?) — Pause execution for human-in-the-loop
  • writer — Stream writer for UI integration
graph<{ count: number }>()
  .node('increment', ({ state, update }) => {
    update({ count: state().count + 1 })
  })

Edges

Connect nodes with static or dynamic routing:

// Static edge
.edge('START', 'validate')

// Dynamic edge based on state
.edge('router', (state) => state.isValid ? 'process' : 'reject')

State Management

State is type-safe and immutable. Updates can be partial objects or functions:

// Partial update
update({ status: 'complete' })

// Functional update
update((state) => ({ count: state.count + 1 }))

Human in the Loop

Suspend execution to wait for user input, approvals, or external data:

const workflow = graph<{ approved: boolean }>()
  .node('review', ({ state, suspense }) => {
    if (!state().approved) {
      suspense({ reason: 'Waiting for approval' })
    }
  })
  .edge('START', 'review')
  .edge('review', 'END')

// First execution suspends
workflow.execute('run-1', { approved: false })

// Resume with updated state after user approves
workflow.execute('run-1', (existing) => ({ ...existing, approved: true }))

Parallel Execution

Multiple edges from the same node execute targets in parallel:

graph<{ results: string[] }>()
  .node('fork', () => {})
  .node('taskA', ({ update }) => update({ results: ['A'] }))
  .node('taskB', ({ update }) => update({ results: ['B'] }))
  .node('join', () => {})
  .edge('START', 'fork')
  .edge('fork', 'taskA')  // Both taskA and taskB
  .edge('fork', 'taskB')  // execute in parallel
  .edge('taskA', 'join')
  .edge('taskB', 'join')
  .edge('join', 'END')

Subgraphs

Compose workflows with nested graphs:

const childGraph = graph<{ value: number }>()
  .node('double', ({ update, state }) => {
    update({ value: state().value * 2 })
  })
  .edge('START', 'double')
  .edge('double', 'END')

const parentGraph = graph<{ input: number; result: number }>()
  .graph('process', childGraph, {
    input: (parentState) => ({ value: parentState.input }),
    output: (childState) => ({ result: childState.value })
  })
  .edge('START', 'process')
  .edge('process', 'END')

Storage

By default, graphs use in-memory storage. For production, use Redis:

import { graph } from 'ai-sdk-graph'
import { RedisStorage } from 'ai-sdk-graph/storage'
import Redis from 'ioredis'

const redis = new Redis()
const storage = new RedisStorage(redis)

const workflow = graph<State>(storage)
  // ... define nodes and edges

Visualization

Generate Mermaid diagrams of your workflows:

const diagram = workflow.toMermaid()
// or with direction
const diagram = workflow.toMermaid({ direction: 'LR' })

Output:

flowchart TB
    START([START])
    validate[validate]
    transform[transform]
    END([END])
    START --> validate
    validate --> transform
    transform --> END

API Reference

graph<State>(storage?)

Create a new graph with optional storage backend.

.node(id, handler)

Add a node with an execution handler.

.edge(from, to)

Add a static edge between nodes.

.edge(from, (state) => nodeId)

Add a dynamic edge that routes based on state.

.graph(id, subgraph, options)

Add a nested subgraph with state mapping.

.execute(runId, initialState)

Execute the graph and return a readable stream.

.toMermaid(options?)

Generate a Mermaid flowchart diagram.

License

MIT