llm_worker/handler.rs
1//! Handler/Kind型
2//!
3//! Timeline層でイベントを処理するためのトレイト。
4//! カスタムハンドラを実装してTimelineに登録することで、
5//! ストリームイベントを受信できます。
6
7use crate::timeline::event::*;
8
9// =============================================================================
10// Kind Trait
11// =============================================================================
12
13/// イベント種別を定義するマーカートレイト
14///
15/// 各Kindは対応するイベント型を指定します。
16/// HandlerはこのKindに対して実装され、同じKindに対して
17/// 異なるScope型を持つ複数のHandlerを登録できます。
18pub trait Kind {
19 /// このKindに対応するイベント型
20 type Event;
21}
22
23// =============================================================================
24// Handler Trait
25// =============================================================================
26
27/// イベントを処理するハンドラトレイト
28///
29/// 特定の`Kind`に対するイベント処理を定義します。
30/// `Scope`はブロックのライフサイクル中に保持される状態です。
31///
32/// # Examples
33///
34/// ```ignore
35/// use llm_worker::timeline::{Handler, TextBlockEvent, TextBlockKind};
36///
37/// struct TextCollector {
38/// texts: Vec<String>,
39/// }
40///
41/// impl Handler<TextBlockKind> for TextCollector {
42/// type Scope = String; // ブロックごとのバッファ
43///
44/// fn on_event(&mut self, buffer: &mut String, event: &TextBlockEvent) {
45/// match event {
46/// TextBlockEvent::Delta(text) => buffer.push_str(text),
47/// TextBlockEvent::Stop(_) => {
48/// self.texts.push(std::mem::take(buffer));
49/// }
50/// _ => {}
51/// }
52/// }
53/// }
54/// ```
55pub trait Handler<K: Kind> {
56 /// Handler固有のスコープ型
57 ///
58 /// ブロック開始時に`Default::default()`で生成され、
59 /// ブロック終了時に破棄されます。
60 type Scope: Default;
61
62 /// イベントを処理する
63 fn on_event(&mut self, scope: &mut Self::Scope, event: &K::Event);
64}
65
66// =============================================================================
67// Meta Kind Definitions
68// =============================================================================
69
70/// Usage Kind - 使用量イベント用
71pub struct UsageKind;
72impl Kind for UsageKind {
73 type Event = UsageEvent;
74}
75
76/// Ping Kind - Pingイベント用
77pub struct PingKind;
78impl Kind for PingKind {
79 type Event = PingEvent;
80}
81
82/// Status Kind - ステータスイベント用
83pub struct StatusKind;
84impl Kind for StatusKind {
85 type Event = StatusEvent;
86}
87
88/// Error Kind - エラーイベント用
89pub struct ErrorKind;
90impl Kind for ErrorKind {
91 type Event = ErrorEvent;
92}
93
94// =============================================================================
95// Block Kind Definitions
96// =============================================================================
97
98/// TextBlock Kind - テキストブロック用
99pub struct TextBlockKind;
100impl Kind for TextBlockKind {
101 type Event = TextBlockEvent;
102}
103
104/// テキストブロックのイベント
105#[derive(Debug, Clone, PartialEq)]
106pub enum TextBlockEvent {
107 Start(TextBlockStart),
108 Delta(String),
109 Stop(TextBlockStop),
110}
111
112#[derive(Debug, Clone, PartialEq)]
113pub struct TextBlockStart {
114 pub index: usize,
115}
116
117#[derive(Debug, Clone, PartialEq)]
118pub struct TextBlockStop {
119 pub index: usize,
120 pub stop_reason: Option<StopReason>,
121}
122
123/// ThinkingBlock Kind - 思考ブロック用
124pub struct ThinkingBlockKind;
125impl Kind for ThinkingBlockKind {
126 type Event = ThinkingBlockEvent;
127}
128
129/// 思考ブロックのイベント
130#[derive(Debug, Clone, PartialEq)]
131pub enum ThinkingBlockEvent {
132 Start(ThinkingBlockStart),
133 Delta(String),
134 Stop(ThinkingBlockStop),
135}
136
137#[derive(Debug, Clone, PartialEq)]
138pub struct ThinkingBlockStart {
139 pub index: usize,
140}
141
142#[derive(Debug, Clone, PartialEq)]
143pub struct ThinkingBlockStop {
144 pub index: usize,
145}
146
147/// ToolUseBlock Kind - ツール使用ブロック用
148pub struct ToolUseBlockKind;
149impl Kind for ToolUseBlockKind {
150 type Event = ToolUseBlockEvent;
151}
152
153/// ツール使用ブロックのイベント
154#[derive(Debug, Clone, PartialEq)]
155pub enum ToolUseBlockEvent {
156 Start(ToolUseBlockStart),
157 /// ツール引数のJSON部分文字列
158 InputJsonDelta(String),
159 Stop(ToolUseBlockStop),
160}
161
162#[derive(Debug, Clone, PartialEq)]
163pub struct ToolUseBlockStart {
164 pub index: usize,
165 pub id: String,
166 pub name: String,
167}
168
169#[derive(Debug, Clone, PartialEq)]
170pub struct ToolUseBlockStop {
171 pub index: usize,
172 pub id: String,
173 pub name: String,
174}