stream_think_filter.cpp¶
src/core/stream_think_filter.cpp
@file stream_think_filter.cpp @brief StreamThinkFilter implementation.
Owners: @tvanfossen
StreamThinkFilter
entropic::StreamThinkFilter::StreamThinkFilter(TokenCallback cb, void *ud)
Construct with consumer callback.
src/core/stream_think_filter.cpp:22
Source · 2 lines
StreamThinkFilter::StreamThinkFilter(TokenCallback cb, void* ud)
: cb_(cb), ud_(ud) {}entropic::StreamThinkFilter::StreamThinkFilter(TokenCallback cb, void *ud, std::string open_marker, std::string close_marker)
Construct with family-specific reasoning delimiters (gh#108).
src/core/stream_think_filter.cpp:34
Source · 7 lines
StreamThinkFilter::StreamThinkFilter(TokenCallback cb, void* ud,
std::string open_marker,
std::string close_marker)
: cb_(cb), ud_(ud),
open_marker_(std::move(open_marker)),
close_marker_(std::move(close_marker)),
max_marker_len_(std::max(open_marker_.size(), close_marker_.size())) {}Requirements
emit_utf8_safe
void entropic::StreamThinkFilter::emit_utf8_safe(const char *data, size_t len)
Emit bytes to consumer, buffering incomplete UTF-8 codepoints.
src/core/stream_think_filter.cpp:97
Source · 28 lines
void StreamThinkFilter::emit_utf8_safe(const char* data, size_t len) {
utf8_buf_.append(data, len);
// Find the last complete codepoint boundary
size_t safe = utf8_buf_.size();
if (safe == 0) { return; }
// Walk backward from end to find any incomplete trailing sequence
auto* buf = reinterpret_cast<const unsigned char*>(utf8_buf_.data());
for (size_t i = 1; i <= 4 && i <= safe; ++i) {
unsigned char c = buf[safe - i];
int expected = utf8_char_len(c);
if (expected > 0) {
// Found a lead byte at position (safe - i)
size_t available = i; // bytes from lead to end
if (available < static_cast<size_t>(expected)) {
// Incomplete sequence — emit up to lead byte
safe -= i;
}
break;
}
}
if (safe > 0) {
cb_(utf8_buf_.data(), safe, ud_);
}
utf8_buf_.erase(0, safe);
}Activity · 16 steps
Requirements
flush
void entropic::StreamThinkFilter::flush()
Flush any buffered partial tag content.
src/core/stream_think_filter.cpp:182
Called by SCENARIO, TEST_CASE, filter_once, generate_streaming, run_streaming
Calls emit_utf8_safe
Source · 11 lines
void StreamThinkFilter::flush() {
if (!tag_buf_.empty() && !in_think_) {
emit_utf8_safe(tag_buf_.data(), tag_buf_.size());
}
tag_buf_.clear();
// Flush any remaining UTF-8 buffer (may be incomplete at stream end)
if (!utf8_buf_.empty()) {
cb_(utf8_buf_.data(), utf8_buf_.size(), ud_);
utf8_buf_.clear();
}
}Activity · 6 steps
Requirements
on_token
void entropic::StreamThinkFilter::on_token(const char *chunk, size_t len)
Process a chunk of tokens.
src/core/stream_think_filter.cpp:168
Called by SCENARIO, TEST_CASE, filter_once, generate_streaming, on_stream_chunk
Calls process_byte
Source · 8 lines
void StreamThinkFilter::on_token(const char* chunk, size_t len) {
// Raw callback always gets everything (unfiltered, no UTF-8 alignment)
if (raw_cb_) { raw_cb_(chunk, len, raw_ud_); }
for (size_t i = 0; i < len; ++i) {
process_byte(chunk[i]);
}
}Activity · 4 steps
Requirements
process_byte
void entropic::StreamThinkFilter::process_byte(char c)
Process one byte through the tag-buffering state machine.
src/core/stream_think_filter.cpp:139
Source · 21 lines
void StreamThinkFilter::process_byte(char c) {
// Only start buffering on a byte that could begin either delimiter.
if (tag_buf_.empty() && c != open_marker_[0] && c != close_marker_[0]) {
if (!in_think_) { emit_utf8_safe(&c, 1); }
return;
}
tag_buf_ += c;
bool is_open = false;
if (match_tag(tag_buf_, is_open)) {
in_think_ = is_open;
tag_buf_.clear();
return;
}
// Past the longest delimiter this family uses, it cannot be one.
if (tag_buf_.size() > max_marker_len_) {
if (!in_think_) {
emit_utf8_safe(tag_buf_.data(), tag_buf_.size());
}
tag_buf_.clear();
}
}Activity · 14 steps
Requirements
set_raw_callback
void entropic::StreamThinkFilter::set_raw_callback(TokenCallback cb, void *ud)
Set optional raw callback (receives ALL tokens unfiltered).
src/core/stream_think_filter.cpp:49
Source · 4 lines
void StreamThinkFilter::set_raw_callback(TokenCallback cb, void* ud) {
raw_cb_ = cb;
raw_ud_ = ud;
}Activity · 2 steps
Requirements
utf8_char_len
static int entropic::utf8_char_len(unsigned char byte)
Count expected bytes in a UTF-8 sequence from lead byte.
src/core/stream_think_filter.cpp:75
Source · 9 lines
static int utf8_char_len(unsigned char byte) {
// Lookup: ASCII | continuation | 2-byte | 3-byte | 4-byte lead.
if (byte < 0x80) { return 1; }
int result = 0; // continuation byte default
if (byte >= 0xF0) { result = 4; }
else if (byte >= 0xE0) { result = 3; }
else if (byte >= 0xC0) { result = 2; }
return result;
}