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

@sthg-sdk/dify.api

v1.2.7

Published

Dify API client with streaming support for STHG Tiangong SDK

Readme

@sthg-sdk/dify-api

Dify API client with streaming support for STHG Tiangong SDK.

Features

  • ✨ Complete Dify API coverage
  • 🚀 Streaming support using Microsoft's fetch-event-source
  • 📝 Full TypeScript support
  • 🔄 Both blocking and streaming modes
  • 🛡️ Built-in error handling
  • 🎯 Easy to use API

Installation

pnpm add @sthg-sdk/dify-api

Usage

Basic Setup

import { DifyClient } from '@sthg-sdk/dify-api';

const client = new DifyClient({
  baseUrl: 'https://api.dify.ai/v1',
  apiKey: 'your-api-key',
});

Chat Messages (Blocking)

const response = await client.chatMessages({
  inputs: {},
  query: 'Hello, how are you?',
  response_mode: 'blocking',
  user: 'user-123',
});

console.log(response.answer);

Chat Messages (Streaming)

await client.chatMessagesStream(
  {
    inputs: {},
    query: 'Tell me a long story',
    response_mode: 'streaming',
    user: 'user-123',
  },
  {
    onMessage: (data) => {
      console.log('Received:', data);
    },
    onEnd: () => {
      console.log('Stream ended');
    },
    onError: (error) => {
      console.error('Stream error:', error);
    },
    onClose: () => {
      console.log('Connection closed');
    },
  }
);

Completion

// Blocking mode
const completion = await client.completion({
  inputs: { name: 'John' },
  response_mode: 'blocking',
  user: 'user-123',
});

// Streaming mode
await client.completionStream(
  {
    inputs: { name: 'John' },
    response_mode: 'streaming',
    user: 'user-123',
  },
  {
    onMessage: (data) => console.log(data),
    onEnd: () => console.log('Done'),
  }
);

Workflow Run

// Blocking mode
const workflowResult = await client.workflowRun({
  inputs: { query: 'What is the weather today?' },
  response_mode: 'blocking',
  user: 'user-123',
});

// Streaming mode
await client.workflowRunStream(
  {
    inputs: { query: 'Process this data' },
    response_mode: 'streaming',
    user: 'user-123',
  },
  {
    onMessage: (data) => console.log('Workflow update:', data),
    onError: (error) => console.error('Workflow error:', error),
  }
);

Audio to Text

const audioFile = new File([audioBuffer], 'audio.wav', { type: 'audio/wav' });

const transcription = await client.audioToText({
  file: audioFile,
  user: 'user-123',
});

console.log(transcription.text);

Text to Audio

const audioBuffer = await client.textToAudio({
  message_id: 'msg-123',
  text: 'Hello world',
  user: 'user-123',
});

// Convert to audio file or play directly

Conversation Management

// Get conversations
const conversations = await client.getConversations('user-123');

// Get conversation messages
const messages = await client.getConversationMessages(
  'conversation-id',
  'user-123'
);

// Rename conversation
await client.renameConversation(
  'conversation-id',
  'New Name',
  'user-123'
);

// Delete conversation
await client.deleteConversation('conversation-id', 'user-123');

Stop Streaming

// Stop any active streaming connection
client.stopStreaming();

Configuration Options

interface DifyConfig {
  baseUrl: string;           // Required: Dify API base URL
  apiKey: string;            // Required: Your API key
  headers?: Record<string, string>; // Optional: Custom headers
  timeout?: number;          // Optional: Request timeout (default: 30000ms)
}

Error Handling

The client provides comprehensive error handling:

try {
  const response = await client.chatMessages({
    inputs: {},
    query: 'Hello',
    response_mode: 'blocking',
    user: 'user-123',
  });
} catch (error) {
  if (error.status === 401) {
    console.error('Authentication failed');
  } else if (error.status === 429) {
    console.error('Rate limit exceeded');
  } else {
    console.error('API error:', error.message);
  }
}

TypeScript Support

The package includes comprehensive TypeScript definitions for all request and response types:

import type {
  ChatRequest,
  ChatResponse,
  CompletionRequest,
  CompletionResponse,
  WorkflowRunRequest,
  WorkflowRunResponse,
  StreamingCallbacks,
  DifyError,
} from '@sthg-sdk/dify-api';

License

Apache-2.0