WorkerSubscriber

Trait WorkerSubscriber 

Source
pub trait WorkerSubscriber: Send {
    type TextBlockScope: Default + Send + Sync;
    type ToolUseBlockScope: Default + Send + Sync;

    // Provided methods
    fn on_text_block(
        &mut self,
        scope: &mut Self::TextBlockScope,
        event: &TextBlockEvent,
    ) { ... }
    fn on_tool_use_block(
        &mut self,
        scope: &mut Self::ToolUseBlockScope,
        event: &ToolUseBlockEvent,
    ) { ... }
    fn on_usage(&mut self, event: &UsageEvent) { ... }
    fn on_status(&mut self, event: &StatusEvent) { ... }
    fn on_error(&mut self, event: &ErrorEvent) { ... }
    fn on_text_complete(&mut self, text: &str) { ... }
    fn on_tool_call_complete(&mut self, call: &ToolCall) { ... }
    fn on_turn_start(&mut self, turn: usize) { ... }
    fn on_turn_end(&mut self, turn: usize) { ... }
}
Expand description

LLMからのストリーミングイベントを購読するトレイト

Workerに登録すると、テキスト生成やツール呼び出しのイベントを リアルタイムで受信できます。UIへのストリーム表示に最適です。

§受信できるイベント

  • ブロックイベント: テキスト、ツール使用(スコープ付き)
  • メタイベント: 使用量、ステータス、エラー
  • 完了イベント: テキスト完了、ツール呼び出し完了
  • ターン制御: ターン開始、ターン終了

§Examples

use llm_worker::subscriber::WorkerSubscriber;
use llm_worker::timeline::TextBlockEvent;

struct StreamPrinter;

impl WorkerSubscriber for StreamPrinter {
    type TextBlockScope = ();
    type ToolUseBlockScope = ();

    fn on_text_block(&mut self, _: &mut (), event: &TextBlockEvent) {
        if let TextBlockEvent::Delta(text) = event {
            print!("{}", text);  // リアルタイム出力
        }
    }

    fn on_text_complete(&mut self, text: &str) {
        println!("\n--- Complete: {} chars ---", text.len());
    }
}

// Workerに登録
worker.subscribe(StreamPrinter);

Required Associated Types§

Source

type TextBlockScope: Default + Send + Sync

テキストブロック処理用のスコープ型

ブロック開始時にDefault::default()で生成され、 ブロック終了時に破棄される。

Source

type ToolUseBlockScope: Default + Send + Sync

ツール使用ブロック処理用のスコープ型

Provided Methods§

Source

fn on_text_block( &mut self, scope: &mut Self::TextBlockScope, event: &TextBlockEvent, )

テキストブロックイベント

Start/Delta/Stopのライフサイクルを持つ。 scopeはブロック開始時に生成され、終了時に破棄される。

Source

fn on_tool_use_block( &mut self, scope: &mut Self::ToolUseBlockScope, event: &ToolUseBlockEvent, )

ツール使用ブロックイベント

Start/InputJsonDelta/Stopのライフサイクルを持つ。

Source

fn on_usage(&mut self, event: &UsageEvent)

使用量イベント

Source

fn on_status(&mut self, event: &StatusEvent)

ステータスイベント

Source

fn on_error(&mut self, event: &ErrorEvent)

エラーイベント

Source

fn on_text_complete(&mut self, text: &str)

テキスト完了イベント

テキストブロックが完了した時点で、累積されたテキスト全体が渡される。 ブロック処理後の最終結果を受け取るのに便利。

Source

fn on_tool_call_complete(&mut self, call: &ToolCall)

ツール呼び出し完了イベント

ツール使用ブロックが完了した時点で、完全なToolCallが渡される。

Source

fn on_turn_start(&mut self, turn: usize)

ターン開始時

turnは0から始まるターン番号。

Source

fn on_turn_end(&mut self, turn: usize)

ターン終了時

Implementors§