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

inspur-yunjia-chat

v0.0.8

Published

Node.js WebSocket Client SDK for Chat System

Readme

Chat SDK for Node.js

License: MIT Node Version TypeScript

Node.js WebSocket Client SDK for Chat System - 浪潮云加聊天系统官方 SDK

功能特性

  • 完整的连接流程 - Passport 认证 → 客户端注册 → WebSocket 连接
  • 流式消息支持 - 支持分块发送大型消息,包含 start/continue/end 状态管理
  • 智能文本分割 - SmartTextSplitter 用于将长文本智能分割为适合流式传输的块
  • 自动重连 - WebSocket 断线自动重连机制
  • TypeScript 支持 - 完整的类型定义文件
  • 事件驱动架构 - 灵活的事件监听和处理机制

安装

npm install inspur-yunjia-chat

环境要求

  • Node.js >= 22.0.0

快速开始

基础使用

const { ChatClientV1, PASSPORT_DEFAULT_CONFIG } = require('inspur-yunjia-chat');

// 创建客户端实例
const client = new ChatClientV1({
  ...PASSPORT_DEFAULT_CONFIG,
  debug: true
});

// 连接到服务器
async function main() {
  try {
    await client.connectToServer('username', 'password', {
      enterprise: 'your-enterprise-id'
    });

    // 监听消息
    client.onMessage((message) => {
      console.log('收到消息:', message);
    });

    // 发送消息
    client.emitMessage({
      type: 'text',
      content: 'Hello World!'
    });

  } catch (error) {
    console.error('连接失败:', error);
  }
}

main();

流式消息发送

const { ChatClientV1, StreamMessage, StreamMessageType } = require('inspur-yunjia-chat');

async function sendStreamMessage() {
  const client = new ChatClientV1(config);
  await client.connectToServer('username', 'password');

  // 创建流式消息
  const streamMsg = new StreamMessage(
    client,
    'enterprise-id',
    'channel-id',
    StreamMessageType.DynamicMarkdown
  );

  // 开始流式发送
  await streamMsg.start();

  // 发送内容块
  const chunks = ['这是', '一个', '流式', '消息'];
  for (const chunk of chunks) {
    await streamMsg.continue(chunk);
  }

  // 结束流式发送
  await streamMsg.end();
}

使用 SmartTextSplitter

const { SmartTextSplitter } = require('inspur-yunjia-chat');

// 智能分割长文本
const longText = "这是一段很长的文本...";
const splitter = new SmartTextSplitter({
  maxLength: 100,      // 最大长度
  minLength: 20,       // 最小长度
  delimiter: '。',     // 分隔符
  overlap: 10          // 重叠字符数
});

const chunks = splitter.split(longText);
console.log(`分割为 ${chunks.length} 个块`);

API 文档

ChatClientV1

高级客户端类,管理完整的连接工作流程。

构造函数

constructor(config: PassportConfig)

参数:

  • config.baseUrl - 基础 URL
  • config.clientId - 客户端 ID
  • config.clientSecret - 客户端密钥
  • config.debug - 启用调试日志 (可选)

方法

connectToServer

完整的连接流程:认证 → 注册 → 连接

async connectToServer(
  username: string,
  password: string,
  options?: ConnectOptions
): Promise<ChatClientV1>
onMessage

快捷方式:监听聊天消息

onMessage<T = any>(handler: EventHandler<T>): ChatClientV1
emitMessage

快捷方式:发送聊天消息

emitMessage(message: any): ChatClientV1
disconnect

断开连接

disconnect(): void

StreamMessage

流式消息类,用于管理分块消息的发送。

构造函数

constructor(
  chatClientV1: ChatClientV1,
  enterprise: string,
  channelId: string,
  messageType: StreamMessageType,
  streamId?: string,
  roundId?: string,
  sessionId?: string,
  config?: StreamMessageConfig
)

方法

start

开始流式发送

async start(): Promise<StreamChunk>
continue

发送内容块

async continue(chunkMessage: string): Promise<StreamChunk>
end

结束流式发送

async end(): Promise<StreamChunk>

StreamMessageType

流式消息类型枚举

  • Thinking - 代理思考状态消息
  • DynamicMarkdown - 动态 Markdown 消息

StreamStatus

流状态枚举

  • START - 开始
  • CONTINUE - 继续
  • END - 结束

SmartTextSplitter

智能文本分割器

构造函数

constructor(options: {
  maxLength?: number;
  minLength?: number;
  delimiter?: string;
  overlap?: number;
})

方法

split

分割文本

split(text: string): string[]

开发

构建

npm run build

监听模式构建

npm run build:watch

类型检查

npm run typecheck

项目结构

yunjia-chat-sdk/
├── src/
│   ├── index.ts                 # 主入口
│   ├── ChatClientV1.ts          # 高级客户端
│   ├── WebsocketClient.ts       # WebSocket 客户端
│   ├── PassportAuth.ts          # Passport 认证
│   ├── ClientRegistry.ts        # 客户端注册
│   ├── StreamMessage.ts         # 流式消息
│   ├── StreamMessageManager.ts  # 流式消息管理器
│   ├── ChatMessageRequest.ts    # 聊天消息请求
│   └── SmartTextSpliter.ts      # 智能文本分割器
├── dist/                        # 编译输出
├── package.json
├── tsconfig.json
└── README.md

技术栈

  • TypeScript - 类型安全的 JavaScript
  • Socket.IO Client - WebSocket 客户端库
  • Axios - HTTP 请求库
  • node-machine-id - 机器 ID 生成

许可证

MIT License - 详见 LICENSE 文件

作者

Inspur CCS

关键词

yunjia, inspur, chat, websocket, streaming, sdk