Files
humanlayer/humanlayer-ts
dexhorthy 22111fd908 Fix daemon integration test timing issues
Increase wait times from 200ms to 1s for daemon startup to ensure
the daemon has sufficient time to initialize SQLite database and
create its socket. Also adds stderr capture for better debugging
of failures and increases test timeout from 2s to 5s.

Fixes ENG-1625
2025-07-16 11:00:34 -05:00
..
2024-09-11 15:30:37 -05:00
2024-09-11 15:30:37 -05:00
2024-09-11 17:08:42 -05:00
2024-09-11 17:08:42 -05:00
2025-01-17 10:25:09 -08:00
wip
2025-06-17 09:29:57 -07:00
2025-06-03 13:37:26 -07:00
2024-09-11 15:30:37 -05:00
2025-02-20 17:12:57 -08:00

HumanLayer TypeScript SDK

The official TypeScript SDK for HumanLayer, providing human oversight for AI applications.

Installation

npm install humanlayer

Key Features

  • Human approval workflows for sensitive operations
  • Structured feedback collection from humans
  • Multiple contact channels (Slack, Email, etc.)
  • Full TypeScript support
  • Async/await API
  • Framework integrations

Basic Usage

import { humanlayer } from 'humanlayer'

const hl = humanlayer({
  runId: 'my-agent',
  contactChannel: {
    slack: {
      channelOrUserId: 'C123456',
      contextAboutChannelOrUser: 'the compliance team',
    },
  },
})

// Require approval for sensitive functions
const sendEmail = hl.requireApproval(async (to: string, subject: string) => {
  // Email sending logic here
})

// Get human input during execution
const support = hl.humanAsTool({
  responseOptions: [
    { name: 'approve', title: 'Approve' },
    { name: 'deny', title: 'Deny' },
  ],
})

Framework Support

  • OpenAI function calling
  • LangChain.js
  • Vercel AI SDK

Contact Channels

Configure how humans are contacted:

// Slack
const slackChannel = {
  slack: {
    channelOrUserId: 'C123456',
    contextAboutChannelOrUser: 'the support team',
  },
}

// Email
const emailChannel = {
  email: {
    address: 'support@company.com',
    contextAboutUser: 'the support team',
  },
}

Response Options

Structure human responses:

const options = [
  {
    name: 'approve',
    title: 'Approve',
    description: 'Approve the action',
  },
  {
    name: 'deny',
    title: 'Deny',
    description: 'Deny with feedback',
    promptFill: 'Denied because...',
  },
]

const approval = await hl.requireApproval(myFunction, {
  responseOptions: options,
})

Error Handling

The SDK provides detailed error types:

try {
  await hl.requireApproval(myFunction)()
} catch (error) {
  if (error instanceof HumanLayerException) {
    // Handle HumanLayer-specific errors
    console.error('HumanLayer error:', error.message)
  } else {
    // Handle other errors
    console.error('Unexpected error:', error)
  }
}

Environment Variables

Required:

  • HUMANLAYER_API_KEY: Your HumanLayer API key

Optional:

Examples

See the examples directory for complete working examples:

Development

# Install dependencies
npm install

# Run tests
npm test

# Build
npm run build

# Type check
npm run check

License

Apache 2.0 - see LICENSE