fix(chat): prevent duplicate renderer requests and thinking messages (#870)
Co-authored-by: Cursor Agent <cursoragent@cursor.com> Co-authored-by: Haze <hazeone@users.noreply.github.com>
This commit is contained in:
@@ -491,6 +491,7 @@ export function ChatInput({ onSend, onStop, disabled = false, sending = false, i
|
||||
onPaste={handlePaste}
|
||||
placeholder={disabled ? t('composer.gatewayDisconnectedPlaceholder') : ''}
|
||||
disabled={disabled}
|
||||
data-testid="chat-composer-input"
|
||||
className="min-h-[40px] max-h-[200px] resize-none border-0 focus-visible:ring-0 focus-visible:ring-offset-0 shadow-none bg-transparent py-2.5 px-2 text-[15px] placeholder:text-muted-foreground/60 leading-relaxed"
|
||||
rows={1}
|
||||
/>
|
||||
@@ -501,6 +502,7 @@ export function ChatInput({ onSend, onStop, disabled = false, sending = false, i
|
||||
onClick={sending ? handleStop : handleSend}
|
||||
disabled={sending ? !canStop : !canSend}
|
||||
size="icon"
|
||||
data-testid="chat-composer-send"
|
||||
className={`shrink-0 h-10 w-10 rounded-full transition-colors ${
|
||||
(sending || canSend)
|
||||
? 'bg-black/5 dark:bg-white/10 text-foreground hover:bg-black/10 dark:hover:bg-white/20'
|
||||
|
||||
@@ -25,6 +25,44 @@ function cleanUserText(text: string): string {
|
||||
.trim();
|
||||
}
|
||||
|
||||
function normalizeProgressiveText(text: string | undefined): string {
|
||||
return typeof text === 'string' ? text.replace(/\r\n/g, '\n').trim() : '';
|
||||
}
|
||||
|
||||
function compactProgressiveParts(parts: string[]): string[] {
|
||||
const compacted: string[] = [];
|
||||
|
||||
for (const part of parts) {
|
||||
const current = normalizeProgressiveText(part);
|
||||
if (!current) continue;
|
||||
|
||||
const previous = compacted.at(-1);
|
||||
if (!previous) {
|
||||
compacted.push(part);
|
||||
continue;
|
||||
}
|
||||
|
||||
const normalizedPrevious = normalizeProgressiveText(previous);
|
||||
if (!normalizedPrevious) {
|
||||
compacted[compacted.length - 1] = part;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (current === normalizedPrevious || normalizedPrevious.startsWith(current)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (current.startsWith(normalizedPrevious)) {
|
||||
compacted[compacted.length - 1] = part;
|
||||
continue;
|
||||
}
|
||||
|
||||
compacted.push(part);
|
||||
}
|
||||
|
||||
return compacted;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract displayable text from a message's content field.
|
||||
* Handles both string content and array-of-blocks content.
|
||||
@@ -49,7 +87,7 @@ export function extractText(message: RawMessage | unknown): string {
|
||||
}
|
||||
}
|
||||
}
|
||||
const combined = parts.join('\n\n');
|
||||
const combined = compactProgressiveParts(parts).join('\n\n');
|
||||
result = combined.trim().length > 0 ? combined : '';
|
||||
} else if (typeof msg.text === 'string') {
|
||||
// Fallback: try .text field
|
||||
@@ -85,7 +123,7 @@ export function extractThinking(message: RawMessage | unknown): string | null {
|
||||
}
|
||||
}
|
||||
|
||||
const combined = parts.join('\n\n').trim();
|
||||
const combined = compactProgressiveParts(parts).join('\n\n').trim();
|
||||
return combined.length > 0 ? combined : null;
|
||||
}
|
||||
|
||||
|
||||
@@ -165,14 +165,178 @@ function saveImageCache(cache: Map<string, AttachedFileMeta>): void {
|
||||
|
||||
const _imageCache = loadImageCache();
|
||||
|
||||
function normalizeBlockText(text: string | undefined): string {
|
||||
return typeof text === 'string' ? text.replace(/\r\n/g, '\n').trim() : '';
|
||||
}
|
||||
|
||||
function compactProgressiveTextParts(parts: string[]): string[] {
|
||||
const compacted: string[] = [];
|
||||
|
||||
for (const part of parts) {
|
||||
const current = normalizeBlockText(part);
|
||||
if (!current) continue;
|
||||
|
||||
const previous = compacted.at(-1);
|
||||
if (!previous) {
|
||||
compacted.push(part);
|
||||
continue;
|
||||
}
|
||||
|
||||
const normalizedPrevious = normalizeBlockText(previous);
|
||||
if (!normalizedPrevious) {
|
||||
compacted[compacted.length - 1] = part;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (current === normalizedPrevious || normalizedPrevious.startsWith(current)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (current.startsWith(normalizedPrevious)) {
|
||||
compacted[compacted.length - 1] = part;
|
||||
continue;
|
||||
}
|
||||
|
||||
compacted.push(part);
|
||||
}
|
||||
|
||||
return compacted;
|
||||
}
|
||||
|
||||
function normalizeLiveContentBlocks(content: ContentBlock[]): ContentBlock[] {
|
||||
const normalized: ContentBlock[] = [];
|
||||
|
||||
let textBuffer: string[] = [];
|
||||
let thinkingBuffer: string[] = [];
|
||||
|
||||
const flushTextBuffer = () => {
|
||||
for (const part of compactProgressiveTextParts(textBuffer)) {
|
||||
normalized.push({ type: 'text', text: part });
|
||||
}
|
||||
textBuffer = [];
|
||||
};
|
||||
|
||||
const flushThinkingBuffer = () => {
|
||||
for (const part of compactProgressiveTextParts(thinkingBuffer)) {
|
||||
normalized.push({ type: 'thinking', thinking: part });
|
||||
}
|
||||
thinkingBuffer = [];
|
||||
};
|
||||
|
||||
for (const block of content) {
|
||||
if (block.type === 'text' && block.text) {
|
||||
textBuffer.push(block.text);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (block.type === 'thinking' && block.thinking) {
|
||||
thinkingBuffer.push(block.thinking);
|
||||
continue;
|
||||
}
|
||||
|
||||
flushTextBuffer();
|
||||
flushThinkingBuffer();
|
||||
normalized.push(block);
|
||||
}
|
||||
|
||||
flushTextBuffer();
|
||||
flushThinkingBuffer();
|
||||
|
||||
return normalized;
|
||||
}
|
||||
|
||||
function normalizeStreamingMessage(message: unknown): unknown {
|
||||
if (!message || typeof message !== 'object') return message;
|
||||
|
||||
const rawMessage = message as RawMessage;
|
||||
const rawContent = rawMessage.content;
|
||||
if (!Array.isArray(rawContent)) return rawMessage;
|
||||
|
||||
const normalizedContent = normalizeLiveContentBlocks(rawContent as ContentBlock[]);
|
||||
const didChange = normalizedContent.some((block, index) => block !== rawContent[index])
|
||||
|| normalizedContent.length !== rawContent.length;
|
||||
|
||||
return didChange
|
||||
? { ...rawMessage, content: normalizedContent }
|
||||
: rawMessage;
|
||||
}
|
||||
|
||||
function normalizeComparableUserText(content: unknown): string {
|
||||
return getMessageText(content)
|
||||
.replace(/\s+/g, ' ')
|
||||
.trim();
|
||||
}
|
||||
|
||||
function getComparableAttachmentSignature(message: Pick<RawMessage, '_attachedFiles'>): string {
|
||||
const files = (message._attachedFiles || [])
|
||||
.map((file) => file.filePath || `${file.fileName}|${file.mimeType}|${file.fileSize}`)
|
||||
.filter(Boolean)
|
||||
.sort();
|
||||
return files.join('::');
|
||||
}
|
||||
|
||||
function matchesOptimisticUserMessage(
|
||||
candidate: RawMessage,
|
||||
optimistic: RawMessage,
|
||||
optimisticTimestampMs: number,
|
||||
): boolean {
|
||||
if (candidate.role !== 'user') return false;
|
||||
|
||||
const optimisticText = normalizeComparableUserText(optimistic.content);
|
||||
const candidateText = normalizeComparableUserText(candidate.content);
|
||||
const sameText = optimisticText.length > 0 && optimisticText === candidateText;
|
||||
|
||||
const optimisticAttachments = getComparableAttachmentSignature(optimistic);
|
||||
const candidateAttachments = getComparableAttachmentSignature(candidate);
|
||||
const sameAttachments = optimisticAttachments.length > 0 && optimisticAttachments === candidateAttachments;
|
||||
|
||||
const hasOptimisticTimestamp = Number.isFinite(optimisticTimestampMs) && optimisticTimestampMs > 0;
|
||||
const hasCandidateTimestamp = candidate.timestamp != null;
|
||||
const timestampMatches = hasOptimisticTimestamp && hasCandidateTimestamp
|
||||
? Math.abs(toMs(candidate.timestamp as number) - optimisticTimestampMs) < 5000
|
||||
: false;
|
||||
|
||||
if (sameText && sameAttachments) return true;
|
||||
if (sameText && (!optimisticAttachments || !candidateAttachments) && (timestampMatches || !hasCandidateTimestamp)) return true;
|
||||
if (sameAttachments && (!optimisticText || !candidateText) && (timestampMatches || !hasCandidateTimestamp)) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
function snapshotStreamingAssistantMessage(
|
||||
currentStream: RawMessage | null,
|
||||
existingMessages: RawMessage[],
|
||||
runId: string,
|
||||
): RawMessage[] {
|
||||
if (!currentStream) return [];
|
||||
|
||||
const normalizedStream = normalizeStreamingMessage(currentStream) as RawMessage;
|
||||
const streamRole = normalizedStream.role;
|
||||
if (streamRole !== 'assistant' && streamRole !== undefined) return [];
|
||||
|
||||
const snapId = normalizedStream.id || `${runId || 'run'}-turn-${existingMessages.length}`;
|
||||
if (existingMessages.some((message) => message.id === snapId)) return [];
|
||||
|
||||
return [{
|
||||
...normalizedStream,
|
||||
role: 'assistant',
|
||||
id: snapId,
|
||||
}];
|
||||
}
|
||||
|
||||
function getLatestOptimisticUserMessage(messages: RawMessage[], userTimestampMs: number): RawMessage | undefined {
|
||||
return [...messages].reverse().find(
|
||||
(message) => message.role === 'user' && (!message.timestamp || Math.abs(toMs(message.timestamp) - userTimestampMs) < 5000),
|
||||
);
|
||||
}
|
||||
|
||||
/** Extract plain text from message content (string or content blocks) */
|
||||
function getMessageText(content: unknown): string {
|
||||
if (typeof content === 'string') return content;
|
||||
if (Array.isArray(content)) {
|
||||
return (content as Array<{ type?: string; text?: string }>)
|
||||
const parts = (content as Array<{ type?: string; text?: string }>)
|
||||
.filter(b => b.type === 'text' && b.text)
|
||||
.map(b => b.text!)
|
||||
.join('\n');
|
||||
.map(b => b.text!);
|
||||
return compactProgressiveTextParts(parts).join('\n');
|
||||
}
|
||||
return '';
|
||||
}
|
||||
@@ -1416,17 +1580,12 @@ export const useChatStore = create<ChatState>((set, get) => ({
|
||||
const userMsgAt = get().lastUserMessageAt;
|
||||
if (get().sending && userMsgAt) {
|
||||
const userMsMs = toMs(userMsgAt);
|
||||
const hasRecentUser = enrichedMessages.some(
|
||||
(m) => m.role === 'user' && m.timestamp && Math.abs(toMs(m.timestamp) - userMsMs) < 5000,
|
||||
);
|
||||
if (!hasRecentUser) {
|
||||
const currentMsgs = get().messages;
|
||||
const optimistic = [...currentMsgs].reverse().find(
|
||||
(m) => m.role === 'user' && m.timestamp && Math.abs(toMs(m.timestamp) - userMsMs) < 5000,
|
||||
);
|
||||
if (optimistic) {
|
||||
finalMessages = [...enrichedMessages, optimistic];
|
||||
}
|
||||
const optimistic = getLatestOptimisticUserMessage(get().messages, userMsMs);
|
||||
const hasMatchingUser = optimistic
|
||||
? enrichedMessages.some((message) => matchesOptimisticUserMessage(message, optimistic, userMsMs))
|
||||
: false;
|
||||
if (optimistic && !hasMatchingUser) {
|
||||
finalMessages = [...enrichedMessages, optimistic];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1890,7 +2049,7 @@ export const useChatStore = create<ChatState>((set, get) => ({
|
||||
const msgRole = (event.message as RawMessage).role;
|
||||
if (isToolResultRole(msgRole)) return s.streamingMessage;
|
||||
}
|
||||
return event.message ?? s.streamingMessage;
|
||||
return normalizeStreamingMessage(event.message ?? s.streamingMessage);
|
||||
})(),
|
||||
streamingTools: updates.length > 0 ? upsertToolStatuses(s.streamingTools, updates) : s.streamingTools,
|
||||
}));
|
||||
@@ -1902,17 +2061,18 @@ export const useChatStore = create<ChatState>((set, get) => ({
|
||||
// Message complete - add to history and clear streaming
|
||||
const finalMsg = event.message as RawMessage | undefined;
|
||||
if (finalMsg) {
|
||||
const updates = collectToolUpdates(finalMsg, resolvedState);
|
||||
if (isToolResultRole(finalMsg.role)) {
|
||||
const normalizedFinalMessage = normalizeStreamingMessage(finalMsg) as RawMessage;
|
||||
const updates = collectToolUpdates(normalizedFinalMessage, resolvedState);
|
||||
if (isToolResultRole(normalizedFinalMessage.role)) {
|
||||
// Resolve file path from the streaming assistant message's matching tool call
|
||||
const currentStreamForPath = get().streamingMessage as RawMessage | null;
|
||||
const matchedPath = (currentStreamForPath && finalMsg.toolCallId)
|
||||
? getToolCallFilePath(currentStreamForPath, finalMsg.toolCallId)
|
||||
const matchedPath = (currentStreamForPath && normalizedFinalMessage.toolCallId)
|
||||
? getToolCallFilePath(currentStreamForPath, normalizedFinalMessage.toolCallId)
|
||||
: undefined;
|
||||
|
||||
// Mirror enrichWithToolResultFiles: collect images + file refs for next assistant msg
|
||||
const toolFiles: AttachedFileMeta[] = [
|
||||
...extractImagesAsAttachedFiles(finalMsg.content),
|
||||
...extractImagesAsAttachedFiles(normalizedFinalMessage.content),
|
||||
];
|
||||
if (matchedPath) {
|
||||
for (const f of toolFiles) {
|
||||
@@ -1922,7 +2082,7 @@ export const useChatStore = create<ChatState>((set, get) => ({
|
||||
}
|
||||
}
|
||||
}
|
||||
const text = getMessageText(finalMsg.content);
|
||||
const text = getMessageText(normalizedFinalMessage.content);
|
||||
if (text) {
|
||||
const mediaRefs = extractMediaRefs(text);
|
||||
const mediaRefPaths = new Set(mediaRefs.map(r => r.filePath));
|
||||
@@ -1938,22 +2098,7 @@ export const useChatStore = create<ChatState>((set, get) => ({
|
||||
// tool result. Without snapshotting here, the intermediate thinking+tool steps
|
||||
// would be overwritten by the next turn's deltas and never appear in the UI.
|
||||
const currentStream = s.streamingMessage as RawMessage | null;
|
||||
const snapshotMsgs: RawMessage[] = [];
|
||||
if (currentStream) {
|
||||
const streamRole = currentStream.role;
|
||||
if (streamRole === 'assistant' || streamRole === undefined) {
|
||||
// Use message's own id if available, otherwise derive a stable one from runId
|
||||
const snapId = currentStream.id
|
||||
|| `${runId || 'run'}-turn-${s.messages.length}`;
|
||||
if (!s.messages.some(m => m.id === snapId)) {
|
||||
snapshotMsgs.push({
|
||||
...(currentStream as RawMessage),
|
||||
role: 'assistant',
|
||||
id: snapId,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
const snapshotMsgs = snapshotStreamingAssistantMessage(currentStream, s.messages, runId);
|
||||
return {
|
||||
messages: snapshotMsgs.length > 0 ? [...s.messages, ...snapshotMsgs] : s.messages,
|
||||
streamingText: '',
|
||||
@@ -1967,9 +2112,9 @@ export const useChatStore = create<ChatState>((set, get) => ({
|
||||
});
|
||||
break;
|
||||
}
|
||||
const toolOnly = isToolOnlyMessage(finalMsg);
|
||||
const hasOutput = hasNonToolAssistantContent(finalMsg);
|
||||
const msgId = finalMsg.id || (toolOnly ? `run-${runId}-tool-${Date.now()}` : `run-${runId}`);
|
||||
const toolOnly = isToolOnlyMessage(normalizedFinalMessage);
|
||||
const hasOutput = hasNonToolAssistantContent(normalizedFinalMessage);
|
||||
const msgId = normalizedFinalMessage.id || (toolOnly ? `run-${runId}-tool-${Date.now()}` : `run-${runId}`);
|
||||
set((s) => {
|
||||
const nextTools = updates.length > 0 ? upsertToolStatuses(s.streamingTools, updates) : s.streamingTools;
|
||||
const streamingTools = hasOutput ? [] : nextTools;
|
||||
@@ -1978,12 +2123,12 @@ export const useChatStore = create<ChatState>((set, get) => ({
|
||||
const pendingImgs = s.pendingToolImages;
|
||||
const msgWithImages: RawMessage = pendingImgs.length > 0
|
||||
? {
|
||||
...finalMsg,
|
||||
role: (finalMsg.role || 'assistant') as RawMessage['role'],
|
||||
...normalizedFinalMessage,
|
||||
role: (normalizedFinalMessage.role || 'assistant') as RawMessage['role'],
|
||||
id: msgId,
|
||||
_attachedFiles: [...(finalMsg._attachedFiles || []), ...pendingImgs],
|
||||
_attachedFiles: [...(normalizedFinalMessage._attachedFiles || []), ...pendingImgs],
|
||||
}
|
||||
: { ...finalMsg, role: (finalMsg.role || 'assistant') as RawMessage['role'], id: msgId };
|
||||
: { ...normalizedFinalMessage, role: (normalizedFinalMessage.role || 'assistant') as RawMessage['role'], id: msgId };
|
||||
const clearPendingImages = { pendingToolImages: [] as AttachedFileMeta[] };
|
||||
|
||||
// Check if message already exists (prevent duplicates)
|
||||
@@ -2044,15 +2189,15 @@ export const useChatStore = create<ChatState>((set, get) => ({
|
||||
// content ("Let me get that written down...") is preserved in the UI
|
||||
// rather than being silently discarded.
|
||||
const currentStream = get().streamingMessage as RawMessage | null;
|
||||
if (currentStream && (currentStream.role === 'assistant' || currentStream.role === undefined)) {
|
||||
const snapId = (currentStream as RawMessage).id
|
||||
|| `error-snap-${Date.now()}`;
|
||||
const alreadyExists = get().messages.some(m => m.id === snapId);
|
||||
if (!alreadyExists) {
|
||||
set((s) => ({
|
||||
messages: [...s.messages, { ...currentStream, role: 'assistant' as const, id: snapId }],
|
||||
}));
|
||||
}
|
||||
const errorSnapshot = snapshotStreamingAssistantMessage(
|
||||
currentStream,
|
||||
get().messages,
|
||||
`error-${runId || Date.now()}`,
|
||||
);
|
||||
if (errorSnapshot.length > 0) {
|
||||
set((s) => ({
|
||||
messages: [...s.messages, ...errorSnapshot],
|
||||
}));
|
||||
}
|
||||
|
||||
set({
|
||||
|
||||
@@ -70,6 +70,169 @@ function saveImageCache(cache: Map<string, AttachedFileMeta>): void {
|
||||
|
||||
const _imageCache = loadImageCache();
|
||||
|
||||
function normalizeBlockText(text: string | undefined): string {
|
||||
return typeof text === 'string' ? text.replace(/\r\n/g, '\n').trim() : '';
|
||||
}
|
||||
|
||||
function compactProgressiveTextParts(parts: string[]): string[] {
|
||||
const compacted: string[] = [];
|
||||
|
||||
for (const part of parts) {
|
||||
const current = normalizeBlockText(part);
|
||||
if (!current) continue;
|
||||
|
||||
const previous = compacted.at(-1);
|
||||
if (!previous) {
|
||||
compacted.push(part);
|
||||
continue;
|
||||
}
|
||||
|
||||
const normalizedPrevious = normalizeBlockText(previous);
|
||||
if (!normalizedPrevious) {
|
||||
compacted[compacted.length - 1] = part;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (current === normalizedPrevious || normalizedPrevious.startsWith(current)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (current.startsWith(normalizedPrevious)) {
|
||||
compacted[compacted.length - 1] = part;
|
||||
continue;
|
||||
}
|
||||
|
||||
compacted.push(part);
|
||||
}
|
||||
|
||||
return compacted;
|
||||
}
|
||||
|
||||
function normalizeLiveContentBlocks(content: ContentBlock[]): ContentBlock[] {
|
||||
const normalized: ContentBlock[] = [];
|
||||
let textBuffer: string[] = [];
|
||||
let thinkingBuffer: string[] = [];
|
||||
|
||||
const flushTextBuffer = () => {
|
||||
for (const part of compactProgressiveTextParts(textBuffer)) {
|
||||
normalized.push({ type: 'text', text: part });
|
||||
}
|
||||
textBuffer = [];
|
||||
};
|
||||
|
||||
const flushThinkingBuffer = () => {
|
||||
for (const part of compactProgressiveTextParts(thinkingBuffer)) {
|
||||
normalized.push({ type: 'thinking', thinking: part });
|
||||
}
|
||||
thinkingBuffer = [];
|
||||
};
|
||||
|
||||
for (const block of content) {
|
||||
if (block.type === 'text' && block.text) {
|
||||
textBuffer.push(block.text);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (block.type === 'thinking' && block.thinking) {
|
||||
thinkingBuffer.push(block.thinking);
|
||||
continue;
|
||||
}
|
||||
|
||||
flushTextBuffer();
|
||||
flushThinkingBuffer();
|
||||
normalized.push(block);
|
||||
}
|
||||
|
||||
flushTextBuffer();
|
||||
flushThinkingBuffer();
|
||||
|
||||
return normalized;
|
||||
}
|
||||
|
||||
function normalizeStreamingMessage(message: unknown): unknown {
|
||||
if (!message || typeof message !== 'object') return message;
|
||||
|
||||
const rawMessage = message as RawMessage;
|
||||
const rawContent = rawMessage.content;
|
||||
if (!Array.isArray(rawContent)) return rawMessage;
|
||||
|
||||
const normalizedContent = normalizeLiveContentBlocks(rawContent as ContentBlock[]);
|
||||
const didChange = normalizedContent.some((block, index) => block !== rawContent[index])
|
||||
|| normalizedContent.length !== rawContent.length;
|
||||
|
||||
return didChange
|
||||
? { ...rawMessage, content: normalizedContent }
|
||||
: rawMessage;
|
||||
}
|
||||
|
||||
function normalizeComparableUserText(content: unknown): string {
|
||||
return getMessageText(content)
|
||||
.replace(/\s+/g, ' ')
|
||||
.trim();
|
||||
}
|
||||
|
||||
function getComparableAttachmentSignature(message: Pick<RawMessage, '_attachedFiles'>): string {
|
||||
const files = (message._attachedFiles || [])
|
||||
.map((file) => file.filePath || `${file.fileName}|${file.mimeType}|${file.fileSize}`)
|
||||
.filter(Boolean)
|
||||
.sort();
|
||||
return files.join('::');
|
||||
}
|
||||
|
||||
function matchesOptimisticUserMessage(
|
||||
candidate: RawMessage,
|
||||
optimistic: RawMessage,
|
||||
optimisticTimestampMs: number,
|
||||
): boolean {
|
||||
if (candidate.role !== 'user') return false;
|
||||
|
||||
const optimisticText = normalizeComparableUserText(optimistic.content);
|
||||
const candidateText = normalizeComparableUserText(candidate.content);
|
||||
const sameText = optimisticText.length > 0 && optimisticText === candidateText;
|
||||
|
||||
const optimisticAttachments = getComparableAttachmentSignature(optimistic);
|
||||
const candidateAttachments = getComparableAttachmentSignature(candidate);
|
||||
const sameAttachments = optimisticAttachments.length > 0 && optimisticAttachments === candidateAttachments;
|
||||
|
||||
const hasOptimisticTimestamp = Number.isFinite(optimisticTimestampMs) && optimisticTimestampMs > 0;
|
||||
const hasCandidateTimestamp = candidate.timestamp != null;
|
||||
const timestampMatches = hasOptimisticTimestamp && hasCandidateTimestamp
|
||||
? Math.abs(toMs(candidate.timestamp as number) - optimisticTimestampMs) < 5000
|
||||
: false;
|
||||
|
||||
if (sameText && sameAttachments) return true;
|
||||
if (sameText && (!optimisticAttachments || !candidateAttachments) && (timestampMatches || !hasCandidateTimestamp)) return true;
|
||||
if (sameAttachments && (!optimisticText || !candidateText) && (timestampMatches || !hasCandidateTimestamp)) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
function snapshotStreamingAssistantMessage(
|
||||
currentStream: RawMessage | null,
|
||||
existingMessages: RawMessage[],
|
||||
runId: string,
|
||||
): RawMessage[] {
|
||||
if (!currentStream) return [];
|
||||
|
||||
const normalizedStream = normalizeStreamingMessage(currentStream) as RawMessage;
|
||||
const streamRole = normalizedStream.role;
|
||||
if (streamRole !== 'assistant' && streamRole !== undefined) return [];
|
||||
|
||||
const snapId = normalizedStream.id || `${runId || 'run'}-turn-${existingMessages.length}`;
|
||||
if (existingMessages.some((message) => message.id === snapId)) return [];
|
||||
|
||||
return [{
|
||||
...normalizedStream,
|
||||
role: 'assistant',
|
||||
id: snapId,
|
||||
}];
|
||||
}
|
||||
|
||||
function getLatestOptimisticUserMessage(messages: RawMessage[], userTimestampMs: number): RawMessage | undefined {
|
||||
return [...messages].reverse().find(
|
||||
(message) => message.role === 'user' && (!message.timestamp || Math.abs(toMs(message.timestamp) - userTimestampMs) < 5000),
|
||||
);
|
||||
}
|
||||
|
||||
function upsertImageCacheEntry(filePath: string, file: Omit<AttachedFileMeta, 'filePath'>): void {
|
||||
_imageCache.set(filePath, { ...file, filePath });
|
||||
saveImageCache(_imageCache);
|
||||
@@ -86,10 +249,10 @@ function withAttachedFileSource(
|
||||
function getMessageText(content: unknown): string {
|
||||
if (typeof content === 'string') return content;
|
||||
if (Array.isArray(content)) {
|
||||
return (content as Array<{ type?: string; text?: string }>)
|
||||
const parts = (content as Array<{ type?: string; text?: string }>)
|
||||
.filter(b => b.type === 'text' && b.text)
|
||||
.map(b => b.text!)
|
||||
.join('\n');
|
||||
.map(b => b.text!);
|
||||
return compactProgressiveTextParts(parts).join('\n');
|
||||
}
|
||||
return '';
|
||||
}
|
||||
@@ -627,7 +790,7 @@ function extractTextFromContent(content: unknown): string {
|
||||
parts.push(block.text);
|
||||
}
|
||||
}
|
||||
return parts.join('\n');
|
||||
return compactProgressiveTextParts(parts).join('\n');
|
||||
}
|
||||
|
||||
function summarizeToolOutput(text: string): string | undefined {
|
||||
@@ -855,6 +1018,10 @@ export {
|
||||
upsertToolStatuses,
|
||||
hasNonToolAssistantContent,
|
||||
isToolOnlyMessage,
|
||||
normalizeStreamingMessage,
|
||||
matchesOptimisticUserMessage,
|
||||
snapshotStreamingAssistantMessage,
|
||||
getLatestOptimisticUserMessage,
|
||||
setHistoryPollTimer,
|
||||
hasErrorRecoveryTimer,
|
||||
setErrorRecoveryTimer,
|
||||
|
||||
@@ -5,11 +5,13 @@ import {
|
||||
clearHistoryPoll,
|
||||
enrichWithCachedImages,
|
||||
enrichWithToolResultFiles,
|
||||
getLatestOptimisticUserMessage,
|
||||
getMessageText,
|
||||
hasNonToolAssistantContent,
|
||||
isInternalMessage,
|
||||
isToolResultRole,
|
||||
loadMissingPreviews,
|
||||
matchesOptimisticUserMessage,
|
||||
toMs,
|
||||
} from './helpers';
|
||||
import { buildCronSessionHistoryPath, isCronSessionKey } from './cron-session-utils';
|
||||
@@ -101,17 +103,12 @@ export function createHistoryActions(
|
||||
const userMsgAt = get().lastUserMessageAt;
|
||||
if (get().sending && userMsgAt) {
|
||||
const userMsMs = toMs(userMsgAt);
|
||||
const hasRecentUser = enrichedMessages.some(
|
||||
(m) => m.role === 'user' && m.timestamp && Math.abs(toMs(m.timestamp) - userMsMs) < 5000,
|
||||
);
|
||||
if (!hasRecentUser) {
|
||||
const currentMsgs = get().messages;
|
||||
const optimistic = [...currentMsgs].reverse().find(
|
||||
(m) => m.role === 'user' && m.timestamp && Math.abs(toMs(m.timestamp) - userMsMs) < 5000,
|
||||
);
|
||||
if (optimistic) {
|
||||
finalMessages = [...enrichedMessages, optimistic];
|
||||
}
|
||||
const optimistic = getLatestOptimisticUserMessage(get().messages, userMsMs);
|
||||
const hasMatchingUser = optimistic
|
||||
? enrichedMessages.some((message) => matchesOptimisticUserMessage(message, optimistic, userMsMs))
|
||||
: false;
|
||||
if (optimistic && !hasMatchingUser) {
|
||||
finalMessages = [...enrichedMessages, optimistic];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,9 @@ import {
|
||||
isToolOnlyMessage,
|
||||
isToolResultRole,
|
||||
makeAttachedFile,
|
||||
normalizeStreamingMessage,
|
||||
setErrorRecoveryTimer,
|
||||
snapshotStreamingAssistantMessage,
|
||||
upsertToolStatuses,
|
||||
} from './helpers';
|
||||
import type { AttachedFileMeta, RawMessage } from './types';
|
||||
@@ -65,7 +67,7 @@ export function handleRuntimeEventState(
|
||||
return s.streamingMessage;
|
||||
}
|
||||
}
|
||||
return event.message ?? s.streamingMessage;
|
||||
return normalizeStreamingMessage(event.message ?? s.streamingMessage);
|
||||
})(),
|
||||
streamingTools: updates.length > 0 ? upsertToolStatuses(s.streamingTools, updates) : s.streamingTools,
|
||||
}));
|
||||
@@ -77,16 +79,17 @@ export function handleRuntimeEventState(
|
||||
// Message complete - add to history and clear streaming
|
||||
const finalMsg = event.message as RawMessage | undefined;
|
||||
if (finalMsg) {
|
||||
const updates = collectToolUpdates(finalMsg, resolvedState);
|
||||
if (isToolResultRole(finalMsg.role)) {
|
||||
const normalizedFinalMessage = normalizeStreamingMessage(finalMsg) as RawMessage;
|
||||
const updates = collectToolUpdates(normalizedFinalMessage, resolvedState);
|
||||
if (isToolResultRole(normalizedFinalMessage.role)) {
|
||||
// Resolve file path from the streaming assistant message's matching tool call
|
||||
const currentStreamForPath = get().streamingMessage as RawMessage | null;
|
||||
const matchedPath = (currentStreamForPath && finalMsg.toolCallId)
|
||||
? getToolCallFilePath(currentStreamForPath, finalMsg.toolCallId)
|
||||
const matchedPath = (currentStreamForPath && normalizedFinalMessage.toolCallId)
|
||||
? getToolCallFilePath(currentStreamForPath, normalizedFinalMessage.toolCallId)
|
||||
: undefined;
|
||||
|
||||
// Mirror enrichWithToolResultFiles: collect images + file refs for next assistant msg
|
||||
const toolFiles: AttachedFileMeta[] = extractImagesAsAttachedFiles(finalMsg.content)
|
||||
const toolFiles: AttachedFileMeta[] = extractImagesAsAttachedFiles(normalizedFinalMessage.content)
|
||||
.map((file) => (file.source ? file : { ...file, source: 'tool-result' }));
|
||||
if (matchedPath) {
|
||||
for (const f of toolFiles) {
|
||||
@@ -96,7 +99,7 @@ export function handleRuntimeEventState(
|
||||
}
|
||||
}
|
||||
}
|
||||
const text = getMessageText(finalMsg.content);
|
||||
const text = getMessageText(normalizedFinalMessage.content);
|
||||
if (text) {
|
||||
const mediaRefs = extractMediaRefs(text);
|
||||
const mediaRefPaths = new Set(mediaRefs.map(r => r.filePath));
|
||||
@@ -112,22 +115,7 @@ export function handleRuntimeEventState(
|
||||
// tool result. Without snapshotting here, the intermediate thinking+tool steps
|
||||
// would be overwritten by the next turn's deltas and never appear in the UI.
|
||||
const currentStream = s.streamingMessage as RawMessage | null;
|
||||
const snapshotMsgs: RawMessage[] = [];
|
||||
if (currentStream) {
|
||||
const streamRole = currentStream.role;
|
||||
if (streamRole === 'assistant' || streamRole === undefined) {
|
||||
// Use message's own id if available, otherwise derive a stable one from runId
|
||||
const snapId = currentStream.id
|
||||
|| `${runId || 'run'}-turn-${s.messages.length}`;
|
||||
if (!s.messages.some(m => m.id === snapId)) {
|
||||
snapshotMsgs.push({
|
||||
...(currentStream as RawMessage),
|
||||
role: 'assistant',
|
||||
id: snapId,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
const snapshotMsgs = snapshotStreamingAssistantMessage(currentStream, s.messages, runId);
|
||||
return {
|
||||
messages: snapshotMsgs.length > 0 ? [...s.messages, ...snapshotMsgs] : s.messages,
|
||||
streamingText: '',
|
||||
@@ -141,9 +129,9 @@ export function handleRuntimeEventState(
|
||||
});
|
||||
break;
|
||||
}
|
||||
const toolOnly = isToolOnlyMessage(finalMsg);
|
||||
const hasOutput = hasNonToolAssistantContent(finalMsg);
|
||||
const msgId = finalMsg.id || (toolOnly ? `run-${runId}-tool-${Date.now()}` : `run-${runId}`);
|
||||
const toolOnly = isToolOnlyMessage(normalizedFinalMessage);
|
||||
const hasOutput = hasNonToolAssistantContent(normalizedFinalMessage);
|
||||
const msgId = normalizedFinalMessage.id || (toolOnly ? `run-${runId}-tool-${Date.now()}` : `run-${runId}`);
|
||||
set((s) => {
|
||||
const nextTools = updates.length > 0 ? upsertToolStatuses(s.streamingTools, updates) : s.streamingTools;
|
||||
const streamingTools = hasOutput ? [] : nextTools;
|
||||
@@ -152,12 +140,12 @@ export function handleRuntimeEventState(
|
||||
const pendingImgs = s.pendingToolImages;
|
||||
const msgWithImages: RawMessage = pendingImgs.length > 0
|
||||
? {
|
||||
...finalMsg,
|
||||
role: (finalMsg.role || 'assistant') as RawMessage['role'],
|
||||
...normalizedFinalMessage,
|
||||
role: (normalizedFinalMessage.role || 'assistant') as RawMessage['role'],
|
||||
id: msgId,
|
||||
_attachedFiles: [...(finalMsg._attachedFiles || []), ...pendingImgs],
|
||||
_attachedFiles: [...(normalizedFinalMessage._attachedFiles || []), ...pendingImgs],
|
||||
}
|
||||
: { ...finalMsg, role: (finalMsg.role || 'assistant') as RawMessage['role'], id: msgId };
|
||||
: { ...normalizedFinalMessage, role: (normalizedFinalMessage.role || 'assistant') as RawMessage['role'], id: msgId };
|
||||
const clearPendingImages = { pendingToolImages: [] as AttachedFileMeta[] };
|
||||
|
||||
// Check if message already exists (prevent duplicates)
|
||||
@@ -218,15 +206,15 @@ export function handleRuntimeEventState(
|
||||
// content ("Let me get that written down...") is preserved in the UI
|
||||
// rather than being silently discarded.
|
||||
const currentStream = get().streamingMessage as RawMessage | null;
|
||||
if (currentStream && (currentStream.role === 'assistant' || currentStream.role === undefined)) {
|
||||
const snapId = (currentStream as RawMessage).id
|
||||
|| `error-snap-${Date.now()}`;
|
||||
const alreadyExists = get().messages.some(m => m.id === snapId);
|
||||
if (!alreadyExists) {
|
||||
set((s) => ({
|
||||
messages: [...s.messages, { ...currentStream, role: 'assistant' as const, id: snapId }],
|
||||
}));
|
||||
}
|
||||
const errorSnapshot = snapshotStreamingAssistantMessage(
|
||||
currentStream,
|
||||
get().messages,
|
||||
`error-${runId || Date.now()}`,
|
||||
);
|
||||
if (errorSnapshot.length > 0) {
|
||||
set((s) => ({
|
||||
messages: [...s.messages, ...errorSnapshot],
|
||||
}));
|
||||
}
|
||||
|
||||
set({
|
||||
@@ -291,7 +279,7 @@ export function handleRuntimeEventState(
|
||||
console.warn(`[handleChatEvent] Unknown event state "${resolvedState}", treating message as streaming delta. Event keys:`, Object.keys(event));
|
||||
const updates = collectToolUpdates(event.message, 'delta');
|
||||
set((s) => ({
|
||||
streamingMessage: event.message ?? s.streamingMessage,
|
||||
streamingMessage: normalizeStreamingMessage(event.message ?? s.streamingMessage),
|
||||
streamingTools: updates.length > 0 ? upsertToolStatuses(s.streamingTools, updates) : s.streamingTools,
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -97,6 +97,9 @@ function maybeLoadSessions(
|
||||
state: { loadSessions: () => Promise<void> },
|
||||
force = false,
|
||||
): void {
|
||||
const { status } = useGatewayStore.getState();
|
||||
if (status.gatewayReady === false) return;
|
||||
|
||||
const now = Date.now();
|
||||
if (!force && now - lastLoadSessionsAt < LOAD_SESSIONS_MIN_INTERVAL_MS) return;
|
||||
lastLoadSessionsAt = now;
|
||||
|
||||
Reference in New Issue
Block a user