1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//! The [`LanguageModel`] trait, its [`LmError`], the chat-context types
//! ([`ChatRole`], [`Message`], [`Conversation`], [`GenParams`]), and the
//! [`TokenStream`] alias.
use Arc;
use async_trait;
use MaybeSendSync;
/// A role in the language model's chat context.
///
/// Deliberately distinct from core's transcript
/// [`Role`](pipecrab_core::Role): the LM context needs a [`System`](ChatRole::System)
/// role that a conversation transcript has no notion of, and overloading the
/// frame's two-valued `Role` to carry it would be dishonest. The stage maps
/// between them (a [`Role::User`](pipecrab_core::Role::User) transcript becomes a
/// [`User`](ChatRole::User) message; a generated reply becomes an
/// [`Assistant`](ChatRole::Assistant) message).
/// One turn in a [`Conversation`]: a [`ChatRole`] and its text.
/// The chat context handed to [`LanguageModel::generate`]: an ordered list of
/// [`Message`]s, oldest first. The system prompt, if any, is the first message.
/// Knobs for one [`generate`](LanguageModel::generate) call. All optional; an
/// engine applies its own default for any left `None`.
/// One incremental piece of a generation: a text delta to append.
/// The output of a [`LanguageModel::generate`] call: a boxed stream of
/// [`TokenOut`] results, delivered delta by delta.
///
/// `BoxStream` on native and `LocalBoxStream` on `wasm32`, behind one cfg'd alias
/// — the same `Send`-where-it-exists split as
/// [`MaybeSend`](pipecrab_runtime::MaybeSend): the pipeline is one logical task
/// that stays `Send` for a work-stealing executor natively, while on `wasm32`
/// (one thread, `!Send` JS handles) that bound must vanish.
pub type TokenStream = BoxStream;
/// The output of a [`LanguageModel::generate`] call: a boxed stream of
/// [`TokenOut`] results, delivered delta by delta.
pub type TokenStream = LocalBoxStream;
/// The swappable language-model capability: a chat context in, generated text out
/// incrementally.
///
/// This is the durable interface. A native engine (e.g. a llama.cpp context) and
/// a browser engine (in a Web Worker) both implement this one trait, so
/// [`LmStage`](crate::LmStage) — and the pipeline above it — never names a
/// concrete model.
///
/// # Engines are worker-handles
///
/// An implementor is expected to be a thin *handle* to a long-lived worker that
/// owns the model's mutable decode state: a dedicated thread on native (a
/// llama.cpp context is `!Send`, so the worker pattern is mandatory there), a Web
/// Worker on `wasm32`. That is why [`generate`](Self::generate) takes `&self` —
/// it hands the context to the worker and returns its token stream — and why the
/// worker outlives any single call.
///
/// # Streaming is a barge-in requirement
///
/// [`generate`](Self::generate) yields text a delta at a time rather than one
/// buffer at the end: every item of the returned [`TokenStream`] is a preemption
/// point the run loop can drop an in-flight generation at, so a user barging in
/// stops the reply within one delta instead of after the whole turn. Dropping the
/// stream is how the *stage* stops pulling; [`cancel`](Self::cancel) is how the
/// *engine* stops producing.
///
/// [`cancel`](Self::cancel) is a *control call* (see
/// [`Processor`](pipecrab_core::Processor)'s control-call carve-out): it maps to
/// the engine's abort callback / an atomic the decode loop checks, so it is
/// synchronous, non-blocking, and safe to invoke directly from a stage's
/// `decide_*` where the barge-in is decided.
///
/// `?Send` on `wasm32` matches pipecrab's single-threaded execution model, so one
/// implementation runs unchanged on a current-thread executor and in the browser,
/// where `Send` bounds cannot be satisfied.
/// Why a [`LanguageModel`] call failed.
///
/// Mirrors the message-plus-kind shape of the pipeline's other error types (e.g.
/// `pipecrab-tts`'s `TtsError`) so the conversion at the stage boundary
/// (`impl From<LmError> for StageError`) is direct.