Skip to main content

StreamEvent

Enum StreamEvent 

Source
pub enum StreamEvent {
Show 15 variants StepStart { index: u32, }, StepFinish { index: u32, finish_reason: FinishReason, usage: Option<Usage>, }, TextStart { content_index: usize, }, TextDelta { content_index: usize, delta: String, }, TextEnd { content_index: usize, }, ReasoningStart { content_index: usize, }, ReasoningDelta { content_index: usize, delta: String, }, ReasoningEnd { content_index: usize, }, ToolCallStart { content_index: usize, id: ToolCallId, name: String, }, ToolCallDelta { content_index: usize, delta: String, }, ToolCallEnd { content_index: usize, }, ToolResult { id: ToolCallId, name: String, result: ToolResultValue, }, ToolError { id: ToolCallId, name: String, message: String, }, Finish { finish_reason: FinishReason, usage: Option<Usage>, }, ProviderError { message: String, retryable: bool, },
}
Expand description

LLM 流式事件 — provider 无关的细粒度输出事件。

遵循 OpenCode 的 start/delta/end 三段式设计:

  • start — 标记一个内容块开始,消费者可据此创建 UI 占位
  • delta — 增量数据,消费者追加到当前块
  • end — 标记内容块结束,消费者可据此 finalize

每个事件通过 content_index 标识其所属的内容块位置 (一次 LLM 回复可能包含多个并行内容块)。

Variants§

§

StepStart

一个推理步骤开始(provider 可能在一次请求中产出多步)

Fields

§index: u32
§

StepFinish

一个推理步骤结束,携带停止原因和 token 用量

Fields

§index: u32
§finish_reason: FinishReason
§usage: Option<Usage>
§

TextStart

文本内容块开始

Fields

§content_index: usize
§

TextDelta

文本增量

Fields

§content_index: usize
§delta: String
§

TextEnd

文本内容块结束

Fields

§content_index: usize
§

ReasoningStart

推理/思考内容块开始

Fields

§content_index: usize
§

ReasoningDelta

推理增量

Fields

§content_index: usize
§delta: String
§

ReasoningEnd

推理内容块结束

Fields

§content_index: usize
§

ToolCallStart

工具调用开始(已知 id 和 name)

Fields

§content_index: usize
§name: String
§

ToolCallDelta

工具调用参数增量(JSON 字符串片段)

Fields

§content_index: usize
§delta: String
§

ToolCallEnd

工具调用参数流结束

Fields

§content_index: usize
§

ToolResult

工具执行成功

§

ToolError

工具执行失败

Fields

§name: String
§message: String
§

Finish

整个 LLM 请求完成

Fields

§finish_reason: FinishReason
§usage: Option<Usage>
§

ProviderError

Provider 级别错误(网络、认证、限流等)

Fields

§message: String
§retryable: bool

Implementations§

Source§

impl StreamEvent

Source

pub fn is_terminal(&self) -> bool

是否为终态事件(Finish 或 ProviderError)。

Source

pub fn is_text_delta(&self) -> bool

是否为文本增量事件。

Source

pub fn is_reasoning_delta(&self) -> bool

是否为推理增量事件。

Source

pub fn as_text_delta(&self) -> Option<&str>

提取文本增量内容,非 TextDelta 事件返回 None。

Source

pub fn as_reasoning_delta(&self) -> Option<&str>

提取推理增量内容,非 ReasoningDelta 事件返回 None。

Trait Implementations§

Source§

impl Clone for StreamEvent

Source§

fn clone(&self) -> StreamEvent

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for StreamEvent

Source§

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

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

impl<'de> Deserialize<'de> for StreamEvent

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for StreamEvent

Source§

fn eq(&self, other: &StreamEvent) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for StreamEvent

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for StreamEvent

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,