package tui import ( "strings" "sync" "time" ) type Event struct { Type string Content string Agent string } type EventWriter struct { ch chan Event mu sync.Mutex buffer strings.Builder } func NewEventWriter(ch chan Event) *EventWriter { return &EventWriter{ch: ch} } func (w *EventWriter) Write(p []byte) (n int, err error) { w.mu.Lock() defer w.mu.Unlock() w.buffer.Write(p) content := w.buffer.String() if strings.Contains(content, "\n") || len(content) > 80 { w.sendEvent(Event{Type: "stream", Content: content}) w.buffer.Reset() } return len(p), nil } func (w *EventWriter) Flush() { w.mu.Lock() defer w.mu.Unlock() if w.buffer.Len() > 0 { w.sendEvent(Event{Type: "stream", Content: w.buffer.String()}) w.buffer.Reset() } } func (w *EventWriter) sendEvent(ev Event) { select { case w.ch <- ev: case <-time.After(100 * time.Millisecond): } }