Fix text and tool interleaving with minimal change
This commit is contained in:
@@ -2007,7 +2007,19 @@ export const DebugDrawer: React.FC<{
|
|||||||
const delta = String(payload.text || '');
|
const delta = String(payload.text || '');
|
||||||
if (!delta) return;
|
if (!delta) return;
|
||||||
setMessages((prev) => {
|
setMessages((prev) => {
|
||||||
const idx = assistantDraftIndexRef.current;
|
let idx = assistantDraftIndexRef.current;
|
||||||
|
if (idx === null || !prev[idx] || prev[idx].role !== 'model') {
|
||||||
|
// Tool records can be appended between assistant chunks; recover the
|
||||||
|
// latest model row instead of creating a duplicate assistant row.
|
||||||
|
for (let i = prev.length - 1; i >= 0; i -= 1) {
|
||||||
|
if (prev[i]?.role === 'model') {
|
||||||
|
idx = i;
|
||||||
|
assistantDraftIndexRef.current = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (prev[i]?.role === 'user') break;
|
||||||
|
}
|
||||||
|
}
|
||||||
if (idx === null || !prev[idx] || prev[idx].role !== 'model') {
|
if (idx === null || !prev[idx] || prev[idx].role !== 'model') {
|
||||||
const last = prev[prev.length - 1];
|
const last = prev[prev.length - 1];
|
||||||
if (last?.role === 'model' && last.text === delta) {
|
if (last?.role === 'model' && last.text === delta) {
|
||||||
@@ -2027,8 +2039,17 @@ export const DebugDrawer: React.FC<{
|
|||||||
if (type === 'assistant.response.final') {
|
if (type === 'assistant.response.final') {
|
||||||
const finalText = String(payload.text || '');
|
const finalText = String(payload.text || '');
|
||||||
setMessages((prev) => {
|
setMessages((prev) => {
|
||||||
const idx = assistantDraftIndexRef.current;
|
let idx = assistantDraftIndexRef.current;
|
||||||
assistantDraftIndexRef.current = null;
|
assistantDraftIndexRef.current = null;
|
||||||
|
if (idx === null || !prev[idx] || prev[idx].role !== 'model') {
|
||||||
|
for (let i = prev.length - 1; i >= 0; i -= 1) {
|
||||||
|
if (prev[i]?.role === 'model') {
|
||||||
|
idx = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (prev[i]?.role === 'user') break;
|
||||||
|
}
|
||||||
|
}
|
||||||
if (idx !== null && prev[idx] && prev[idx].role === 'model') {
|
if (idx !== null && prev[idx] && prev[idx].role === 'model') {
|
||||||
const next = [...prev];
|
const next = [...prev];
|
||||||
next[idx] = { ...next[idx], text: finalText || next[idx].text };
|
next[idx] = { ...next[idx], text: finalText || next[idx].text };
|
||||||
|
|||||||
Reference in New Issue
Block a user