Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions ui/desktop/src/hooks/useMessageStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import { getSecretKey } from '../config';
import { Message, createUserMessage, hasCompletedToolCalls } from '../types/message';
import { getSessionHistory } from '../api';

let messageIdCounter = 0;

function generateMessageId(): string {
return `msg-${Date.now()}-${++messageIdCounter}`;
}

// Ensure TextDecoder is available in the global scope
const TextDecoder = globalThis.TextDecoder;

Expand Down Expand Up @@ -297,6 +303,8 @@ export function useMessageStream({
// Create a new message object with the properties preserved or defaulted
const newMessage = {
...parsedEvent.message,
// Ensure the message has an ID - if not provided, generate one
id: parsedEvent.message.id || generateMessageId(),
// Only set to true if it's undefined (preserve false values)
display:
parsedEvent.message.display === undefined
Expand Down Expand Up @@ -358,7 +366,7 @@ export function useMessageStream({
// If this is a token limit error, create a contextLengthExceeded message instead of throwing
if (isTokenLimitError) {
const contextMessage: Message = {
id: `context-${Date.now()}`,
id: generateMessageId(),
role: 'assistant',
created: Math.floor(Date.now() / 1000),
content: [
Expand Down Expand Up @@ -461,7 +469,7 @@ export function useMessageStream({
async (requestMessages: Message[]) => {
try {
mutateLoading(true);
mutateWaiting(true); // Start in waiting state
mutateWaiting(true); // Start in waiting state
mutateStreaming(false);
setError(undefined);

Expand Down Expand Up @@ -539,7 +547,17 @@ export function useMessageStream({
}
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[api, processMessageStream, mutateLoading, mutateWaiting, mutateStreaming, setError, onResponse, onError, maxSteps]
[
api,
processMessageStream,
mutateLoading,
mutateWaiting,
mutateStreaming,
setError,
onResponse,
onError,
maxSteps,
]
);

// Append a new message and send request
Expand Down Expand Up @@ -637,6 +655,7 @@ export function useMessageStream({

// Create a tool response message
const toolResponseMessage: Message = {
id: generateMessageId(),
role: 'user' as const,
created: Math.floor(Date.now() / 1000),
content: [
Expand Down
Loading