"use client" import { useEffect, useRef } from "react" type ChatMessage = { agent_id: string text: string type?: 'message' | 'death' | 'alliance_proposal' | 'alliance_accept' | 'alliance_reject' | 'leadership_vote' | 'leader_elected' | 'water_collected' | 'fire_extinguished' to_model?: string from_model?: string candidates?: string[] } export default function ChatFeed({ messages }: { messages: ChatMessage[] }) { const bottomRef = useRef(null) useEffect(() => { bottomRef.current?.scrollIntoView({ behavior: "smooth" }) }, [messages]) function getAgentName(id: string) { return id.split("/").at(-1)?.split("-")[0].toUpperCase() || id } function getAgentColor(name: string) { let hash = 0 for (let i = 0; i < name.length; i++) { hash = name.charCodeAt(i) + ((hash << 5) - hash) } return `hsl(${hash % 360}, 70%, 60%)` } return (
{messages.map((msg, i) => { const color = getAgentColor(msg.agent_id) if (msg.type === 'death') { return (
💀 {getAgentName(msg.agent_id)} was consumed by the fire
) } if (msg.type === 'alliance_proposal') { return (
🤝 {getAgentName(msg.agent_id)} proposed an alliance to {msg.to_model ? getAgentName(msg.to_model) : 'someone'}
) } if (msg.type === 'alliance_accept') { return (
✅ {getAgentName(msg.agent_id)} accepted the alliance{msg.to_model ? ` with ${getAgentName(msg.to_model)}` : ''}
) } if (msg.type === 'alliance_reject') { return (
❌ {getAgentName(msg.agent_id)} rejected the alliance{msg.to_model ? ` from ${getAgentName(msg.to_model)}` : ''}
) } if (msg.type === 'leadership_vote') { return (
🗳️ {getAgentName(msg.agent_id)} voted for {msg.candidates ? getAgentName(msg.candidates[0]) : 'someone'}
) } if (msg.type === 'leader_elected') { return (
👑 {getAgentName(msg.agent_id)} elected as leader!
) } if (msg.type === 'water_collected') { return (
💧 {getAgentName(msg.agent_id)} collected water!
) } if (msg.type === 'fire_extinguished') { return (
🔥 Fire being extinguished! Intensity dropping...
) } return (
{getAgentName(msg.agent_id)} {msg.text}
) })}
) }