buffer .= $token; if (str_contains($token, '```')) { $this->insideCodeBlock = !$this->insideCodeBlock; } if ($this->shouldFlush()) { $out = $this->buffer; $this->buffer = ''; return $out; } return null; } public function flush(): ?string { if ($this->buffer === '') { return null; } $out = $this->buffer; $this->buffer = ''; return $out; } private function shouldFlush(): bool { if ($this->insideCodeBlock) { return false; } if (str_ends_with($this->buffer, "\n\n")) { return true; } if (preg_match('/[.!?]\s$/', $this->buffer)) { return true; } if (preg_match('/\n[-*] .+\n$/', $this->buffer)) { return true; } return mb_strlen($this->buffer) >= $this->minChunkSize; } }