Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Images are not persistent on page refresh #705

Open
conacts opened this issue Jan 14, 2025 · 2 comments
Open

Images are not persistent on page refresh #705

conacts opened this issue Jan 14, 2025 · 2 comments

Comments

@conacts
Copy link

conacts commented Jan 14, 2025

I tracked this back to convertToUIMessages extracting and removing the images from our messages from database (const messagesFromDb = await getMessagesByChatId({ id });).

Is there anyway to retain these images, possibly using convertToCoreMessages rather than convertToUIMessages? It seems we store our messages in our DB as Core Messages, what is the advantage to UI Messages over core messages?

Screen.Recording.2025-01-13.at.10.57.19.AM.mov
@conacts
Copy link
Author

conacts commented Jan 14, 2025

I can raise a PR

I can raise the PR for this, I have this running on my own for with this implementation:

I just didn't because I didn't want to set up a new neon & blob db.

export function convertToUIMessages(messages: Array<DBMessage>): Array<Message> {
	return messages.reduce((chatMessages: Array<Message>, message) => {
		if (message.role === 'tool') {
			return addToolMessageToChat({
				toolMessage: message as CoreToolMessage,
				messages: chatMessages,
			});
		}

		let textContent = '';
		const toolInvocations: Array<ToolInvocation> = [];
		const experimental_attachments: Attachment[] = [];

		if (typeof message.content === 'string') {
			textContent = message.content;
		} else if (Array.isArray(message.content)) {
			for (const content of message.content) {
				if (content.type === 'text') {
					textContent += content.text;
				} else if (content.type === 'tool-call') {
					toolInvocations.push({
						state: 'call',
						toolCallId: content.toolCallId,
						toolName: content.toolName,
						args: content.args,
					});
				} else if (content.type === 'image' && content.image) {
					experimental_attachments.push({
						name: content.name,
						url: content.image,
						contentType: 'image',
					});
				}
			}
		}

		chatMessages.push({
			id: message.id,
			role: message.role as Message['role'],
			content: textContent,
			toolInvocations,
			experimental_attachments: experimental_attachments.length > 0 ? experimental_attachments : undefined,
		});

		return chatMessages;
	}, []);
}

@michaelwlt
Copy link

@conacts Do you actually get the correct name for the image after reloading the chat?
In my case it seems like the content.name is not persistent and I need to fetch it from the url with url.split('/').pop().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants