pub struct ThinkingAccumulator {
state: AccState,
buf: String,
}Expand description
Stateful accumulator for streaming thinking-tag detection.
Feed SSE text deltas one-by-one via push and collect the
returned ThinkingEvents. The accumulator handles tags that are split
arbitrarily across chunks.
§Example
use gglib_core::domain::thinking::{ThinkingAccumulator, ThinkingEvent};
let mut acc = ThinkingAccumulator::new();
// Tag arrives split across three chunks
let e1 = acc.push("<thi");
assert!(e1.is_empty()); // buffered
let e2 = acc.push("nk>");
assert_eq!(e2, vec![]); // open tag consumed, no content yet
let e3 = acc.push("hmm");
assert_eq!(e3, vec![ThinkingEvent::ThinkingDelta("hmm".into())]);
let e4 = acc.push("</think>");
assert_eq!(e4, vec![ThinkingEvent::ThinkingEnd]);
let e5 = acc.push("Hello!");
assert_eq!(e5, vec![ThinkingEvent::ContentDelta("Hello!".into())]);Fields§
§state: AccState§buf: StringBuffer for bytes that might be part of an opening or closing tag.
Implementations§
Source§impl ThinkingAccumulator
impl ThinkingAccumulator
Sourcepub fn push(&mut self, chunk: &str) -> Vec<ThinkingEvent>
pub fn push(&mut self, chunk: &str) -> Vec<ThinkingEvent>
Feed a new text chunk and return any events produced.
Sourcepub fn flush(&mut self) -> Vec<ThinkingEvent>
pub fn flush(&mut self) -> Vec<ThinkingEvent>
Flush any remaining buffered content.
Call this when the stream ends to emit any buffered text that turned out not to be a tag.
fn handle_awaiting_open(&mut self, chunk: &str) -> Vec<ThinkingEvent>
fn handle_inside_thinking(&mut self, chunk: &str) -> Vec<ThinkingEvent>
fn handle_content(chunk: &str) -> Vec<ThinkingEvent>
Sourcefn find_open_tag_end(lower: &str) -> Option<usize>
fn find_open_tag_end(lower: &str) -> Option<usize>
Find the position of > that closes an opening <think...> tag.
Returns the byte index of > in lower.
Trait Implementations§
Source§impl Debug for ThinkingAccumulator
impl Debug for ThinkingAccumulator
Auto Trait Implementations§
impl Freeze for ThinkingAccumulator
impl RefUnwindSafe for ThinkingAccumulator
impl Send for ThinkingAccumulator
impl Sync for ThinkingAccumulator
impl Unpin for ThinkingAccumulator
impl UnwindSafe for ThinkingAccumulator
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more