Skip to content

agent聊天对话hook

@pt/hooks/network/useChatStream 提供了agent对话聊天的hook

基础使用

vue
<template>
  <div>
    <div v-for="(msg, index) in messages" :key="msg.id">
      <MarkdownCompiler :markdown="msg.content" />
    </div>
    <textarea ref="textarea" v-model="input" />
     <button @click="handleSendMsg">发送</button>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useChatStream } from '@pt/hooks/network';

const { messages, sendMessage } = useChatStream({ token: '登录后的token', agentCode: 'agent code' })
const input = ref('')
const handleSendMsg = async () => {
  await sendMessage(input.value)
}
</script>

参数说明

参数是一个对象,对象属性如下

属性名类型是否必传默认值说明
tokenstring-创建会话与聊天使用
threadIdstring与agentCode二选一-会话id,传如之后聊天时不会自动创建会话
emptyContentstring-当聊天出错或其他原因导致没有内容时显示的内容
handleDing(operate: any) => void-当前聊天为ding的情况,并且有操作数据时会调用
agentCodestring | ()=>string | () => Promise<string>与threadId二选一-创建会话时需要使用的参数(与threadId为二选一)

返回数据

属性名类型说明
messagesMsgItem[]聊天消息列表
threadIdstring当前会话id
sendMessage(content: string, options?: SendMsgOptions, requestConfig?: RequestInit)=> Promise<void>发送消息函数
pushspinMsgMsgItem当前ding的消息
handlePushspinMsg(msg: MsgItem)=>void添加ding消息
hanleCannelPushspinMsg()=>void取消ding消息

ding的说明

当需要针对某条消息单独询问是,例如当前消息是一个大屏页面,需要针对这个大屏页面针对性询问时,这是需要使用ding的模式,当ding时参数会有变动,这个内部已经处理,只需要将需要调用handlePushspinMsg,将需要ding的消息传入即可,当前同一时间只能ding一条消息

技能调用时的附加参数

typescript
input: {
  shortcuts: [
    {
      type: ShortcutType, // 'plug' | 'db' | 'doc' | 'flow'
      content: {
        plugin_id: string,
        name: string,
        desc: string,
        inputs: { [key: string]: any }
      }
    }
  ]
}

示例:

typescript
sendMessage('聊天内容', {
  input: {
    shortcuts: [
      {
        type: "plug",
        content: {
          plugin_id: "1914218921387433984",
          name: "xxxxxx",
          desc: null,
          inputs: {}
        }
      }
    ]
  }
})

amis确认等操作参数

typescript
{
  command: {
    resume: {
      type: "edit",
      args: args // markdwon渲染插件调用时传入的数据
    }
  }
}

示例:

typescript
sendMessage('确认', {
  command: {
    resume: {
      type: "edit",
      args: args // markdwon渲染插件调用时传入的数据
    }
  }
})

Released under the MIT License.