Skip to content

Commit dc5cd0e

Browse files
committed
fixed:stream parameter is not work for ai module
fixed:stream data parse is incorrect
1 parent d513090 commit dc5cd0e

File tree

1 file changed

+69
-40
lines changed

1 file changed

+69
-40
lines changed

XEngine_Module/XEngine_AIApi/AIApi_Chat/AIApi_Chat.cpp

Lines changed: 69 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ bool CAIApi_Chat::AIApi_Chat_Excute(XNETHANDLE xhToken, LPCXSTR lpszModelName, L
188188

189189
st_JsonArray.append(st_JsonSystemRole);
190190
}
191+
stl_MapIterator->second->bStream = bStream;
191192
//准备数据
192193
Json::Value st_JsonObject;
193194
AICLIENT_HISTORY st_AIHistory = {};
@@ -384,6 +385,9 @@ bool CAIApi_Chat::AIApi_Chat_Destory(XNETHANDLE xhToken)
384385
{
385386
APIClient_Http_Close(stl_MapIterator->second->xhToken);
386387

388+
free(stl_MapIterator->second->ptszMSGBuffer);
389+
stl_MapIterator->second->ptszMSGBuffer = NULL;
390+
387391
delete stl_MapIterator->second;
388392
stl_MapIterator->second = NULL;
389393
stl_MapAIClient.erase(stl_MapIterator);
@@ -421,10 +425,6 @@ bool CAIApi_Chat::AIApi_Chat_Parse(AICLIENT_CHAT* pSt_AIClient, LPCXSTR lpszMSGB
421425
{
422426
continue;
423427
}
424-
if (st_JsonMessage["content"].isNull() || st_JsonMessage["role"].isNull())
425-
{
426-
continue;
427-
}
428428
}
429429
else
430430
{
@@ -439,20 +439,27 @@ bool CAIApi_Chat::AIApi_Chat_Parse(AICLIENT_CHAT* pSt_AIClient, LPCXSTR lpszMSGB
439439
}
440440
}
441441
AICLIENT_HISTORY st_AIHistory = {};
442+
443+
if (!st_JsonMessage["role"].isNull())
444+
{
445+
_xstrcpy(st_AIHistory.tszRoleName, st_JsonMessage["role"].asCString(), sizeof(st_AIHistory.tszRoleName));
446+
}
442447
int nGBKLen = st_JsonMessage["content"].asString().length();
443-
_xstrcpy(st_AIHistory.tszRoleName, st_JsonMessage["role"].asCString(), sizeof(st_AIHistory.tszRoleName));
444448
_xstrcpy(st_AIHistory.tszRoleContent, st_JsonMessage["content"].asString().c_str(), sizeof(st_AIHistory.tszRoleContent));
449+
//某些回复没有内容
450+
if (_tcsxlen(st_AIHistory.tszRoleContent) > 0)
451+
{
445452
#ifdef _MSC_BUILD
446-
XCHAR tszGBKBuffer[8192] = {};
447-
BaseLib_Charset_UTFToAnsi(st_JsonMessage["content"].asString().c_str(), tszGBKBuffer, &nGBKLen);
448-
pSt_AIClient->lpCall_Chat(pSt_AIClient->xhToken, st_JsonRoot["model"].asCString(), tszGBKBuffer, nGBKLen, pSt_AIClient->lParam);
453+
XCHAR tszGBKBuffer[8192] = {};
454+
BaseLib_Charset_UTFToAnsi(st_JsonMessage["content"].asString().c_str(), tszGBKBuffer, &nGBKLen);
455+
pSt_AIClient->lpCall_Chat(pSt_AIClient->xhToken, st_JsonRoot["model"].asCString(), tszGBKBuffer, nGBKLen, pSt_AIClient->lParam);
449456
#else
450-
pSt_AIClient->lpCall_Chat(pSt_AIClient->xhToken, st_JsonRoot["model"].asCString(), st_JsonMessage["content"].asString().c_str(), st_JsonMessage["content"].asString().length(), pSt_AIClient->lParam);
457+
pSt_AIClient->lpCall_Chat(pSt_AIClient->xhToken, st_JsonRoot["model"].asCString(), st_JsonMessage["content"].asString().c_str(), st_JsonMessage["content"].asString().length(), pSt_AIClient->lParam);
451458
#endif
452-
453-
if (pSt_AIClient->bHistory)
454-
{
455-
pSt_AIClient->pStl_ListHistory->push_back(st_AIHistory);
459+
if (pSt_AIClient->bHistory)
460+
{
461+
pSt_AIClient->pStl_ListHistory->push_back(st_AIHistory);
462+
}
456463
}
457464
}
458465

@@ -465,44 +472,66 @@ void CAIApi_Chat::AIApi_Chat_CBRecv(XNETHANDLE xhToken, XPVOID lpszMsgBuffer, in
465472
{
466473
AICLIENT_CHAT* pSt_AIClient = (AICLIENT_CHAT*)lParam;
467474
CAIApi_Chat* pClass_This = (CAIApi_Chat*)pSt_AIClient->lClass;
468-
//SSE
469-
int nPos = 0;
470-
bool bSSEReply = false;
471-
LPCXSTR lpszSSEStr = _X("data: ");
472475

476+
if (pSt_AIClient->nMSGLen + nMsgLen > XENGINE_MEMORY_SIZE_MAX)
477+
{
478+
//清空旧数据并重置
479+
pSt_AIClient->nMSGLen = 0;
480+
}
481+
memcpy(pSt_AIClient->ptszMSGBuffer + pSt_AIClient->nMSGLen, lpszMsgBuffer, nMsgLen);
482+
pSt_AIClient->nMSGLen += nMsgLen;
473483
if (pSt_AIClient->bStream)
474484
{
475-
xstring m_StrBuffer;
476-
XCHAR tszMSGBuffer[8192] = {};
477-
memcpy(tszMSGBuffer, lpszMsgBuffer, nMsgLen);
478-
//stream可能会有chunk回车情况
479-
XCHAR* ptszTokNext = NULL;
480-
XCHAR* ptszTokStr = _tcsxtok_s(tszMSGBuffer, _X("\r\n\r\n"), &ptszTokNext);
481-
if (NULL == ptszTokStr)
485+
//SSE
486+
printf("%s\n", (LPCXSTR)lpszMsgBuffer);
487+
int nPos = 0;
488+
LPCXSTR lpszSSEStr = _X("data: ");
489+
490+
XCHAR* ptszStart = pSt_AIClient->ptszMSGBuffer;
491+
XCHAR* ptszEnd = ptszStart + pSt_AIClient->nMSGLen;
492+
while (true)
482493
{
483-
m_StrBuffer = (LPCXSTR)lpszMsgBuffer;
484-
//头为data:
485-
if (0 == _tcsxnicmp((LPCXSTR)lpszMsgBuffer, lpszSSEStr, _tcsxlen(lpszSSEStr)))
494+
// 查找是否存在完整的一个消息(由 \n\n 分隔)
495+
XCHAR* ptszSplit = _tcsxstr(ptszStart, _X("\n\n"));
496+
if (NULL == ptszSplit)
497+
{
498+
// 不完整,等待更多数据
499+
break;
500+
}
501+
// 得到一个完整消息的结束位置(包含 \r\n\r\n 的结尾)
502+
XCHAR* ptszNextMsg = ptszSplit + 2; // Skip "\n\n"
503+
int nOneMsgLen = (int)(ptszSplit - ptszStart); // 不含 "\n\n"
504+
// 检查是否是 data: 开头的 SSE 消息
505+
int nPos = 0;
506+
if (_tcsxnicmp(ptszStart, lpszSSEStr, _tcsxlen(lpszSSEStr)) == 0)
486507
{
487508
nPos = _tcsxlen(lpszSSEStr);
488-
bSSEReply = true;
489509
}
490-
}
491-
else
492-
{
493-
while (NULL != ptszTokStr)
510+
// 解析当前消息体
511+
if (!pClass_This->AIApi_Chat_Parse(pSt_AIClient, ptszStart + nPos, nOneMsgLen - nPos, true))
512+
{
513+
break;
514+
}
515+
// 计算剩余数据长度
516+
int nRemainLen = (int)(ptszEnd - ptszNextMsg);
517+
if (nRemainLen > 0)
494518
{
495-
m_StrBuffer += ptszTokStr;
496-
ptszTokStr = _tcsxtok_s(NULL, _X("\r\n\r\n"), &ptszTokNext);
519+
memmove(pSt_AIClient->ptszMSGBuffer, ptszNextMsg, nRemainLen * sizeof(XCHAR));
497520
}
521+
// 更新指针和长度
522+
pSt_AIClient->nMSGLen = nRemainLen;
523+
memset(pSt_AIClient->ptszMSGBuffer + pSt_AIClient->nMSGLen, '\0', XENGINE_MEMORY_SIZE_MAX - pSt_AIClient->nMSGLen);
524+
525+
ptszStart = pSt_AIClient->ptszMSGBuffer;
526+
ptszEnd = ptszStart + nRemainLen;
498527
}
499528
}
500-
501-
memcpy(pSt_AIClient->ptszMSGBuffer + pSt_AIClient->nMSGLen, lpszMsgBuffer, nMsgLen);
502-
pSt_AIClient->nMSGLen += nMsgLen;
503-
if (pClass_This->AIApi_Chat_Parse(pSt_AIClient, (LPCXSTR)pSt_AIClient->ptszMSGBuffer + nPos, pSt_AIClient->nMSGLen - nPos, bSSEReply))
529+
else
504530
{
505-
memset(pSt_AIClient->ptszMSGBuffer, '\0', XENGINE_MEMORY_SIZE_MAX);
506-
pSt_AIClient->nMSGLen = 0;
531+
if (pClass_This->AIApi_Chat_Parse(pSt_AIClient, (LPCXSTR)pSt_AIClient->ptszMSGBuffer, pSt_AIClient->nMSGLen, false))
532+
{
533+
memset(pSt_AIClient->ptszMSGBuffer, '\0', XENGINE_MEMORY_SIZE_MAX);
534+
pSt_AIClient->nMSGLen = 0;
535+
}
507536
}
508537
}

0 commit comments

Comments
 (0)