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

Show on the map →

Called by none

Calls none

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

Show on the map →

Called by none

Calls none

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

REQ-LOOP-008

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
lines 98–98utf8_buf_.append(data, len);lines 101–101size_t safe = utf8_buf_.size();lines 102–102◇ if (safe == 0)lines 102–102return;lines 105–105auto* buf = reinterpret_cast<const unsigned char*>(utf8_buf_.data());lines 106–106◇ for (size_t i = 1; i <= 4 && i <= safe; ++i)lines 107–107unsigned char c = buf[safe - i];lines 108–108int expected = utf8_char_len(c);lines 109–109◇ if (expected > 0)lines 111–111size_t available = i;lines 112–112◇ if (available < static_cast<size_t>(expected))lines 114–114safe -= i;lines 116–116break;lines 120–120◇ if (safe > 0)lines 121–121cb_(utf8_buf_.data(), safe, ud_);lines 123–123utf8_buf_.erase(0, safe);

Requirements

REQ-LOOP-008

flush

void entropic::StreamThinkFilter::flush()

Flush any buffered partial tag content.

src/core/stream_think_filter.cpp:182

Show on the map →

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
lines 183–183◇ if (!tag_buf_.empty() && !in_think_)lines 184–184emit_utf8_safe(tag_buf_.data(), tag_buf_.size());lines 186–186tag_buf_.clear();lines 188–188◇ if (!utf8_buf_.empty())lines 189–189cb_(utf8_buf_.data(), utf8_buf_.size(), ud_);lines 190–190utf8_buf_.clear();

Requirements

REQ-LOOP-008

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

Show on the map →

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
lines 170–170◇ if (raw_cb_)lines 170–170raw_cb_(chunk, len, raw_ud_);lines 172–172◇ for (size_t i = 0; i < len; ++i)lines 173–173process_byte(chunk[i]);

Requirements

REQ-LOOP-008

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
lines 141–141◇ if (tag_buf_.empty() && c != open_marker_[0] && c != close_marker_[0])lines 142–142◇ if (!in_think_)lines 142–142emit_utf8_safe(&c, 1);lines 143–143return;lines 145–145tag_buf_ += c;lines 146–146bool is_open = false;lines 147–147◇ if (match_tag(tag_buf_, is_open))lines 148–148in_think_ = is_open;lines 149–149tag_buf_.clear();lines 150–150return;lines 153–153◇ if (tag_buf_.size() > max_marker_len_)lines 154–154◇ if (!in_think_)lines 155–155emit_utf8_safe(tag_buf_.data(), tag_buf_.size());lines 157–157tag_buf_.clear();

Requirements

REQ-LOOP-008

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

Show on the map →

Called by TEST_CASE, run_streaming

Calls none

Source · 4 lines
void StreamThinkFilter::set_raw_callback(TokenCallback cb, void* ud) {
    raw_cb_ = cb;
    raw_ud_ = ud;
}
Activity · 2 steps
lines 50–50raw_cb_ = cb;lines 51–51raw_ud_ = ud;

Requirements

REQ-LOOP-008

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;
}
Activity · 12 steps
lines 77–77◇ if (byte < 0x80)lines 77–77return 1;lines 78–78int result = 0;lines 79–79◇ if (byte >= 0xF0)lines 79–79result = 4;lines 80–80elselines 80–80◇ if (byte >= 0xE0)lines 80–80result = 3;lines 81–81elselines 81–81◇ if (byte >= 0xC0)lines 81–81result = 2;lines 82–82return result;

Requirements

REQ-LOOP-008