@@ -61,32 +61,71 @@ const FullscreenConversation = React.memo(function FullscreenConversation({ mess
6161 // 将消息和工具执行按时间顺序混合
6262 const getMixedContent = ( ) => {
6363 const content = [ ] ;
64+ let toolIndex = 0 ; // 工具执行的当前索引
6465
65- // 添加消息
66- messages . forEach ( ( message , index ) => {
66+ // 遍历消息,将工具执行插入到对应的助手消息后面
67+ messages . forEach ( ( message , msgIndex ) => {
68+ // 添加消息
6769 content . push ( {
6870 type : 'message' ,
6971 data : message ,
70- key : message . key || `msg-${ index } ` ,
71- timestamp : message . timestamp || index
72+ key : message . key || `msg-${ msgIndex } ` ,
73+ timestamp : message . timestamp || msgIndex
7274 } ) ;
75+
76+ // 如果是助手消息且有工具调用,添加相关的工具执行
77+ if ( message . role === 'assistant' && message . complete && showTools ) {
78+ // 获取这条消息之后的工具执行
79+ // 通过时间戳或序号判断哪些工具属于这条消息
80+ const messageTime = message . key || Date . now ( ) ;
81+ const nextMessage = messages [ msgIndex + 1 ] ;
82+ const nextMessageTime = nextMessage ?. key || Infinity ;
83+
84+ // 找到所有在当前消息之后、下条消息之前完成的工具
85+ while ( toolIndex < toolExecutions . length ) {
86+ const tool = toolExecutions [ toolIndex ] ;
87+ // 计算工具完成时间:startTime + duration,或者使用 id(开始时间戳)
88+ const toolTime = ( tool . startTime && tool . duration )
89+ ? tool . startTime + tool . duration
90+ : tool . id || Date . now ( ) ;
91+
92+ // 如果工具完成时间在当前消息时间之后,且在下一条消息之前,则添加
93+ if ( toolTime >= messageTime && toolTime < nextMessageTime ) {
94+ content . push ( {
95+ type : 'tool' ,
96+ data : tool ,
97+ key : tool . id || `tool-${ toolIndex } ` ,
98+ timestamp : toolTime
99+ } ) ;
100+ toolIndex ++ ;
101+ } else if ( toolTime < messageTime ) {
102+ // 工具时间早于当前消息,跳过并继续
103+ toolIndex ++ ;
104+ } else {
105+ // 工具时间晚于当前消息时间窗口,停止处理
106+ break ;
107+ }
108+ }
109+ }
73110 } ) ;
74111
75- // 添加工具执行(仅在 showTools 为 true 时)
76- if ( showTools ) {
77- toolExecutions . forEach ( ( tool , index ) => {
112+ // 添加剩余的工具执行(如果有)
113+ if ( showTools && toolIndex < toolExecutions . length ) {
114+ while ( toolIndex < toolExecutions . length ) {
115+ const tool = toolExecutions [ toolIndex ] ;
116+ const toolTime = ( tool . startTime && tool . duration )
117+ ? tool . startTime + tool . duration
118+ : tool . id || Date . now ( ) ;
78119 content . push ( {
79120 type : 'tool' ,
80121 data : tool ,
81- key : tool . id || `tool-${ index } ` ,
82- timestamp : tool . id || Date . now ( ) + index
122+ key : tool . id || `tool-${ toolIndex } ` ,
123+ timestamp : toolTime
83124 } ) ;
84- } ) ;
125+ toolIndex ++ ;
126+ }
85127 }
86128
87- // 按时间戳排序(保持原有顺序,工具执行插入到对应位置)
88- // 由于消息没有精确时间戳,我们保持消息在前,工具在后的顺序
89- // 实际上工具执行是在 AI 响应过程中发生的,所以放在消息之后
90129 return content ;
91130 } ;
92131
0 commit comments