mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-07-24 21:36:06 +00:00
feat: webchat stream is ok
This commit is contained in:
@@ -14,8 +14,8 @@ class WebChatDebugRouterGroup(group.RouterGroup):
|
|||||||
|
|
||||||
async def stream_generator(generator):
|
async def stream_generator(generator):
|
||||||
async for message in generator:
|
async for message in generator:
|
||||||
yield rf"data:{json.dumps({'message': message})}\n\n"
|
yield f"data: {json.dumps({'message': message})}\n\n"
|
||||||
yield "data:{type: end}\n\n''"
|
yield "data: {\"type\": \"end\"}\n\n"
|
||||||
try:
|
try:
|
||||||
data = await quart.request.get_json()
|
data = await quart.request.get_json()
|
||||||
session_type = data.get('session_type', 'person')
|
session_type = data.get('session_type', 'person')
|
||||||
|
|||||||
@@ -242,11 +242,12 @@ class WebChatAdapter(msadapter.MessagePlatformAdapter):
|
|||||||
|
|
||||||
if is_stream:
|
if is_stream:
|
||||||
queue = use_session.resp_queues[message_id]
|
queue = use_session.resp_queues[message_id]
|
||||||
|
msg_id = len(use_session.get_message_list(pipeline_uuid)) + 1
|
||||||
while True:
|
while True:
|
||||||
resp_message = await queue.get()
|
resp_message = await queue.get()
|
||||||
print(resp_message)
|
resp_message.id = msg_id
|
||||||
if resp_message.is_final:
|
if resp_message.is_final:
|
||||||
resp_message.id = len(use_session.get_message_list(pipeline_uuid)) + 1
|
resp_message.id = msg_id
|
||||||
use_session.get_message_list(pipeline_uuid).append(resp_message)
|
use_session.get_message_list(pipeline_uuid).append(resp_message)
|
||||||
yield resp_message.model_dump()
|
yield resp_message.model_dump()
|
||||||
break
|
break
|
||||||
|
|||||||
@@ -201,15 +201,17 @@ export default function DebugDialog({
|
|||||||
(data) => {
|
(data) => {
|
||||||
// 处理流式响应数据
|
// 处理流式响应数据
|
||||||
if (data.message) {
|
if (data.message) {
|
||||||
accumulatedContent += data.message;
|
accumulatedContent += data.message.content;
|
||||||
|
|
||||||
// 更新bot消息
|
// 更新bot消息
|
||||||
setMessages((prevMessages) => {
|
setMessages((prevMessages) => {
|
||||||
const updatedMessages = [...prevMessages];
|
const updatedMessages = [...prevMessages];
|
||||||
const botMessageIndex = updatedMessages.findIndex(
|
// const botMessageIndex = updatedMessages.findIndex(
|
||||||
(msg) =>
|
// (msg) =>
|
||||||
msg.id === botMessageId && msg.role === 'assistant',
|
// msg.id === botMessageId && msg.role === 'assistant',
|
||||||
);
|
// );
|
||||||
|
// 使用索引来更新消息,而不是id匹配
|
||||||
|
const botMessageIndex = updatedMessages.length - 1;
|
||||||
|
|
||||||
if (botMessageIndex !== -1) {
|
if (botMessageIndex !== -1) {
|
||||||
const updatedBotMessage = {
|
const updatedBotMessage = {
|
||||||
|
|||||||
@@ -359,14 +359,12 @@ class HttpClient {
|
|||||||
messageChain: object[],
|
messageChain: object[],
|
||||||
pipelineId: string,
|
pipelineId: string,
|
||||||
timeout: number = 15000,
|
timeout: number = 15000,
|
||||||
is_stream: boolean = false,
|
|
||||||
): Promise<ApiRespWebChatMessage> {
|
): Promise<ApiRespWebChatMessage> {
|
||||||
return this.post(
|
return this.post(
|
||||||
`/api/v1/pipelines/${pipelineId}/chat/send`,
|
`/api/v1/pipelines/${pipelineId}/chat/send`,
|
||||||
{
|
{
|
||||||
session_type: sessionType,
|
session_type: sessionType,
|
||||||
message: messageChain,
|
message: messageChain,
|
||||||
is_stream: is_stream,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
timeout,
|
timeout,
|
||||||
@@ -432,10 +430,10 @@ class HttpClient {
|
|||||||
|
|
||||||
// 处理完整的JSON对象
|
// 处理完整的JSON对象
|
||||||
const lines = buffer.split('\n\n');
|
const lines = buffer.split('\n\n');
|
||||||
buffer = lines.pop() || '';
|
buffer = '';
|
||||||
|
|
||||||
for (const line of lines) {
|
for (const line of lines) {
|
||||||
if (line.startsWith('data: ')) {
|
if (line.startsWith('data:')) {
|
||||||
try {
|
try {
|
||||||
const data = JSON.parse(line.slice(6));
|
const data = JSON.parse(line.slice(6));
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user