ThinkingAccumulator

Struct ThinkingAccumulator 

Source
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: String

Buffer for bytes that might be part of an opening or closing tag.

Implementations§

Source§

impl ThinkingAccumulator

Source

pub const fn new() -> Self

Create a new accumulator in the initial state.

Source

pub fn push(&mut self, chunk: &str) -> Vec<ThinkingEvent>

Feed a new text chunk and return any events produced.

Source

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.

Source

fn handle_awaiting_open(&mut self, chunk: &str) -> Vec<ThinkingEvent>

Source

fn handle_inside_thinking(&mut self, chunk: &str) -> Vec<ThinkingEvent>

Source

fn handle_content(chunk: &str) -> Vec<ThinkingEvent>

Source

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

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ThinkingAccumulator

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more