// Client messages match the format used by the Nut protocol. type MessageRole = 'user' | 'assistant'; interface MessageBase { id: string; role: MessageRole; repositoryId?: string; } interface MessageText extends MessageBase { type: 'text'; content: string; } interface MessageImage extends MessageBase { type: 'image'; dataURL: string; } export type Message = MessageText | MessageImage; // Get the repositoryId before any changes in the message at the given index. export function getPreviousRepositoryId(messages: Message[], index: number): string | undefined { for (let i = index - 1; i >= 0; i--) { const message = messages[i]; if (message.repositoryId) { return message.repositoryId; } } return undefined; } // Get the repositoryId after applying some messages. export function getMessagesRepositoryId(messages: Message[]): string | undefined { return getPreviousRepositoryId(messages, messages.length); }