Socket
Book a DemoInstallSign in
Socket

@lmecosystem/chat-webkit

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@lmecosystem/chat-webkit

Provide some common components for chat

0.0.1
latest
npmnpm
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

chat-webkit 接入文档

提供会话相关的前端组件能力,组件列表:

  • ChatBox: SSE实时对话流
  • ChatManagement: 会话管理

1. 使用说明

在React工程中引入组件

npm i -S chat-webkit

环境要求

React >= 18.0.0

2. ChatBox

SSE实时对话流组件,提供对话交互UI、对话状态扭转和样式高度自定义

2.1 参数说明

2.1.1 组件入参说明

参数说明是否必需类型默认值版本
propsSessionList传入的历史对话记录SseMesage[]
urlsse 连接地址(唯一)string
eventNamesse 返回数据时,监听事件名称string
style对话框样式自定义React.CSSProperties
sseMaxWaitTimeSSE 连接超时时间number
sseNoMessageWaitTime无消息,超时会话等待number
sseActiveWaitTime会话中(已收到消息),超时提示等待number
sseActiveCloseTime会话中(已收到消息),超时主动关闭number
token头部鉴权信息string
requestMessageContainerStyle提问对话框样式自定义React.CSSProperties
responseMessageContainerStyle回答对话框样式自定义React.CSSProperties
isShowStopGenerateButton是否显示停止生成按钮booleantrue
stopGenerateButtonStyle停止生成按钮 样式自定义React.CSSProperties
stopGenerateWords停止生成文字配置string停止生成
isShowRegenerateButton当会话失败时, 是否显示重新生成 按钮booleantrue
reGenerateComponent重新生成组件 ,若不传则用默认JSX.Element
tooLongErrorComponent超长提示 暴露组件,若不传则用默认JSX.Element
modelCloseStyle模型关闭样式自定义React.CSSProperties

2.1.2 SseMesage说明

参数说明是否必需类型默认值版本
id聊天id(唯一)number
status对话状态,0:对话停止状态 2:对话进行中number
message对话回复信息string
question对话问题Astring

2.1.3 SSE返回的数据格式说明

参数说明是否必需类型默认值版本
code状态码AllStatus
data数据Data

2.1.4 AllStatus说明

枚举值说明版本
3发起请求5s 内,无消息
4发起请求5s 后,无消息
1接收到event stream,流式输出中
-1接收出错
0接收完成
-20003会话超长
-20008模型关闭
-20015会话中,后端超时

2.1.5 Data说明

参数说明是否必需类型默认值版本
contextstring
idnumber
requeststring
responsestring

2.2 使用demo

import React,{  useRef } from 'react';

import styles from './App.module.less';

import ChatWebkit from 'chat-webkit';

const TooLongErrorComponent = ()=>{
  const a = ()=>{
    alert("ddd")

  }

  return (
    <div onClick={a}>
      tooo lll
    </div>
  )
}

const App: React.FC = () => {
  const ref = useRef<any>();
  const ref1 = useRef<any>();
  const ref2 = useRef<any>();
  const ref3 = useRef<any>();

  const sessionList = [
    {
      id: 140385,
      status: 0,
      message: '你好!我是聊天机器人,很高兴能够帮助你。你有什么需要帮助的事情吗?',
      question: 'ni hao ',
    },
    {
      id: 140386,
      status: 0,
      message: '你好!我是聊天机器人,很高兴能够帮助你。你有什么需要帮助的事情吗?',
      question: 'ni hao ',
    },
  ];

  const startSse = () => {
    ref.current.startSse('test');
    ref1.current.startSse('test');
    ref2.current.startSse('test');
    ref3.current.startSse('test');
  };

  const stopSse = () => {
    ref.current.stopSse();
    ref1.current.stopSse();
    ref2.current.stopSse();
    ref3.current.stopSse();
  }

  const reGenerate = () => {
    ref.current.reGenerate();
    ref1.current.reGenerate();
    ref2.current.reGenerate();
    ref3.current.reGenerate();
  };

  const getSseStatus = () => {
    console.log(ref.current.getStatus());
    console.log(ref1.current.getStatus());
    console.log(ref2.current.getStatus());
    console.log(ref3.current.getStatus());
  };

  return (
    <>
      <div className={styles.chatContainer}>
        <ChatWebkit.ChatBox
          tooLongErrorComponent={TooLongErrorComponent()}
          eventName=''
          propsSessionList={sessionList}
          url={'/chat/generate?turn_id=125612&username=gtl'}
          ref={ref}
          requestMessageContainerStyle={{backgroundColor:'red'}}
          responseMessageContainerStyle={{backgroundColor:'blue'}}
        />
        <ChatWebkit.ChatBox
          propsSessionList={sessionList}
          url={'/puyu/chats/1951/records/generate'}
          ref={ref1}
        />
        <ChatWebkit.ChatBox
          propsSessionList={sessionList}
          url={'/puyu/chats/1952/records/generate'}
          ref={ref2}
        />
        <ChatWebkit.ChatBox
          propsSessionList={sessionList}
          url={'/puyu/chats/1953/records/generate'}
          ref={ref3}
        />
      </div>
      <button onClick={startSse}>开始会话</button>
      <button onClick={stopSse}>停止会话</button>
      <button onClick={reGenerate}>重试</button>
      <button onClick={getSseStatus}>获取状态</button>
    </>
  );
};

export default App;

3. ChatManagement

会话管理组件,提供会话管理的自定义能力和样式

3.1 参数说明

3.1.1 组件入参说明

参数说明是否必需类型默认值版本
list会话列表IChatItem[][]
selectedChatId选中的会话string''
ellipsis会话名称省略的最大行数Int2
addCallback新建会话回调,需更新list() => void-
deleteCallback删除会话回调,需更新list(id:string) => void-
selectCallback选中会话回调,需处理对话流的切换(id:string) => void-
emptyRender空列表状态下的自定义渲染,默认不渲染ReactNode-
itemRender单条会话的自定义渲染ReactNode-
addRender新建会话的自定义渲染ReactNode-
optionRender会话操作的自定义渲染ReactNode-

3.1.2 IChatItem说明

参数说明是否必需类型默认值版本
id唯一标识idstring-
name名称string-

3.2 使用demo

import style from './App.module.less';
import ChatWebkit, {IChatItem} from 'chat-webkit'
import { useState } from 'react';

function App() {
    const [curChatId, setCurChatId] = useState<string>('')
    const [chatList, setChatList] = useState<IChatItem[]>([{
        id: '1',
        name: '开始会话后,这里就会显示第一句话,如果太长',
    },{
        id: '2',
        name: '创建后的默认名称叫做“新会话”',
    },{
        id: '3',
        name: '最多显示两行'
    }])

    const addChat = () => {
        const newItem = {
            id: Date.now().toString(),
            name: '新会话'
        }
        const newList = chatList.slice()
        newList.unshift(newItem)
        setChatList(newList)
        setCurChatId(newItem.id)
    }

    const deleteChat = (id: string) => {
        const newList = chatList.slice()
        const index = chatList.findIndex(x => x.id === id)
        if (index >= 0) {
            newList.splice(index, 1)
            setChatList(newList)
        }
    }

    const selectChat = (id:string) => {
        setCurChatId(id)
    }

    return (
        <div className={style.app} id="app">
            <div className={style.content}>
                <div className={style.title}>ChatZoo</div>
                <ChatWebkit.ChatManagement  
                    list={chatList} 
                    selectedChatId={curChatId}
                    addCallback={addChat} 
                    deleteCallback={deleteChat} 
                    selectCallback={selectChat} 
                />
            </div>
            <div className={style.right}>
                {curChatId}
            </div>
        </div>
    );
}

export default App;

Keywords

sse

FAQs

Package last updated on 14 Aug 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.