@@ -387,72 +387,6 @@ export const prepareChatRound = async (params: PrepareChatRoundParams) => {
387387 } ) ;
388388} ;
389389
390- export const ensurePendingChatRoundItems = async ( params : EnsurePendingChatRoundParams ) => {
391- const { chatId, appId, teamId, tmbId, responseChatItemId } = params ;
392- if ( ! chatId || chatId === 'NO_RECORD_HISTORIES' ) return ;
393-
394- const humanDataId = params . userContent . dataId ?? responseChatItemId ;
395-
396- const existingAi = await MongoChatItem . findOne ( {
397- appId,
398- chatId,
399- dataId : responseChatItemId ,
400- obj : ChatRoleEnum . AI
401- } )
402- . select ( '_id' )
403- . lean ( ) ;
404-
405- if ( existingAi ) return ;
406-
407- const userPayload : UserChatItemType & { dataId : string ; obj : typeof ChatRoleEnum . Human } = {
408- ...params . userContent ,
409- dataId : humanDataId ,
410- obj : ChatRoleEnum . Human
411- } ;
412-
413- userPayload . value ?. forEach ( ( item ) => {
414- if ( 'file' in item && item . file ?. key ) {
415- item . file . url = '' ;
416- }
417- } ) ;
418-
419- const aiPlaceholder : AIChatItemType & { dataId : string } = {
420- dataId : responseChatItemId ,
421- obj : ChatRoleEnum . AI ,
422- value : [ ]
423- } ;
424-
425- await mongoSessionRun ( async ( session ) => {
426- const upsertOpts = { session, upsert : true } ;
427- await MongoChatItem . updateOne (
428- { appId, chatId, dataId : humanDataId , obj : ChatRoleEnum . Human } ,
429- {
430- $setOnInsert : {
431- teamId,
432- tmbId,
433- chatId,
434- appId,
435- ...userPayload
436- }
437- } ,
438- upsertOpts
439- ) ;
440- await MongoChatItem . updateOne (
441- { appId, chatId, dataId : responseChatItemId , obj : ChatRoleEnum . AI } ,
442- {
443- $setOnInsert : {
444- teamId,
445- tmbId,
446- chatId,
447- appId,
448- ...aiPlaceholder
449- }
450- } ,
451- upsertOpts
452- ) ;
453- } ) ;
454- } ;
455-
456390export const finalizeChatRound = async ( props : Props ) => {
457391 beforeProcess ( props ) ;
458392
@@ -771,96 +705,30 @@ export const pushChatRecords = async (props: Props) => {
771705 errorMsg
772706 } ) ;
773707 const processedContent = [ userContent , aiResponse ] ;
774- const humanRoundDataId = ( processedContent [ 0 ] as { dataId ?: string } ) . dataId as string ;
775- const aiRoundDataId = ( processedContent [ 1 ] as { dataId ?: string } ) . dataId as string ;
776708
777709 await mongoSessionRun ( async ( session ) => {
778- const humanExisting = await MongoChatItem . findOne ( {
779- appId,
780- chatId,
781- dataId : humanRoundDataId ,
782- obj : ChatRoleEnum . Human
783- } ) . session ( session ) ;
784- const aiExisting = await MongoChatItem . findOne ( {
785- appId,
786- chatId,
787- dataId : aiRoundDataId ,
788- obj : ChatRoleEnum . AI
789- } ) . session ( session ) ;
790-
791- let chatItemIdHuman : unknown ;
792- let chatItemIdAi : unknown ;
793- let responseDataId = aiRoundDataId ;
794-
795- if ( humanExisting && aiExisting ) {
796- await MongoChatItem . updateOne (
797- { _id : humanExisting . _id } ,
798- {
799- $set : {
800- ...( processedContent [ 0 ] as Record < string , unknown > ) ,
801- obj : ChatRoleEnum . Human
802- }
803- } ,
804- { session }
805- ) ;
806- await MongoChatItem . updateOne (
807- { _id : aiExisting . _id } ,
808- {
809- $set : {
810- ...( processedContent [ 1 ] as Record < string , unknown > ) ,
811- obj : ChatRoleEnum . AI
812- }
813- } ,
814- { session }
815- ) ;
816-
817- await MongoChatItemResponse . deleteMany (
818- { appId, chatId, chatItemDataId : aiRoundDataId } ,
819- { session }
820- ) ;
821-
822- if ( nodeResponses ?. length ) {
823- await MongoChatItemResponse . create (
824- nodeResponses . map ( ( item ) => ( {
825- teamId,
826- appId,
827- chatId,
828- chatItemDataId : aiRoundDataId ,
829- data : item
830- } ) ) ,
831- { session, ordered : true }
832- ) ;
833- }
710+ const [ { _id : chatItemIdHuman } , { _id : chatItemIdAi , dataId } ] = await MongoChatItem . create (
711+ processedContent . map ( ( item ) => ( {
712+ chatId,
713+ teamId,
714+ tmbId,
715+ appId,
716+ ...item
717+ } ) ) ,
718+ { session, ordered : true , ...writePrimary }
719+ ) ;
834720
835- chatItemIdHuman = humanExisting . _id ;
836- chatItemIdAi = aiExisting . _id ;
837- } else {
838- const [ humanCreated , aiCreated ] = await MongoChatItem . create (
839- processedContent . map ( ( item ) => ( {
840- chatId,
721+ if ( nodeResponses ) {
722+ await MongoChatItemResponse . create (
723+ nodeResponses . map ( ( item ) => ( {
841724 teamId,
842- tmbId,
843725 appId,
844- ...item
726+ chatId,
727+ chatItemDataId : dataId ,
728+ data : item
845729 } ) ) ,
846- { session, ordered : true }
730+ { session, ordered : true , ... writePrimary }
847731 ) ;
848- chatItemIdHuman = humanCreated . _id ;
849- chatItemIdAi = aiCreated . _id ;
850- responseDataId = aiCreated . dataId ;
851-
852- if ( nodeResponses ) {
853- await MongoChatItemResponse . create (
854- nodeResponses . map ( ( item ) => ( {
855- teamId,
856- appId,
857- chatId,
858- chatItemDataId : responseDataId ,
859- data : item
860- } ) ) ,
861- { session, ordered : true }
862- ) ;
863- }
864732 }
865733
866734 await MongoChat . updateOne (
@@ -894,7 +762,8 @@ export const pushChatRecords = async (props: Props) => {
894762 } ,
895763 {
896764 session,
897- upsert : true
765+ upsert : true ,
766+ ...writePrimary
898767 }
899768 ) ;
900769
0 commit comments