repositories / smith

smith

There are many coding harnesses - but this one is fast

owned by bugabinga

refactor: extract provider round consumption from the agent loop

0f514f9bbfb939f107a47e5f559f552308db2de1

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (1)

+54 / −28 text lines

Diff layout

smith-core/src/agent.rs

Modified

+54 / −28 text lines

159159 pub complete: bool,
162/// One consumed provider round.
163struct Round {
164 /// Accumulated assistant text.
165 text: String,
166 /// Complete tool uses, in emission order.
167 tools: Vec<(ToolCallId, String, serde_json::Value)>,
168 /// Terminal stop with usage, when the round ended cleanly.
169 terminal: Option<(StopReason, Usage)>,
170}
162172impl Agent {
163173 /// An agent over a stream function and a recorded tool session.
164174 #[must_use]
262272 self.deliver_between_request_queue()?;
263273 let request = self.build_request();
264274 let stream = (self.stream)(request, self.cancel.clone());
265 let mut text = String::new();
266 let mut pending_tools: Vec<(ToolCallId, String, serde_json::Value)> = Vec::new();
267 let mut terminal: Option<(StopReason, Usage)> = None;
268 for item in futures::executor::block_on_stream(stream) {
269 self.check_cancelled()?;
270 let event = item?;
271 match event.clone() {
272 StreamEvent::TextDelta { delta, .. } => text.push_str(&delta),
273 StreamEvent::ThinkingDelta { .. } | StreamEvent::ToolResult { .. } => {}
274 StreamEvent::ToolUse {
275 name,
276 input,
277 call_id,
278 ..
279 } => pending_tools.push((call_id, name, input)),
280 StreamEvent::Stop { reason, usage } => {
281 terminal = Some((reason, usage.unwrap_or_default()));
282 // Terminal is emitted once; the stream ends here.
283 break;
284 }
285 StreamEvent::Error { error } => {
286 return Err(SmithError::Provider {
287 fault: smith::error::ProviderFault::Protocol { message: error },
288 });
289 }
290 }
291 events.push(AgentEvent::Streamed { event });
292 }
275 let Round {
276 text,
277 tools: pending_tools,
278 terminal,
279 } = self.consume(stream, events)?;
293280 let Some((reason, usage)) = terminal else {
294281 return Err(SmithError::Provider {
295282 fault: smith::error::ProviderFault::Incomplete,
365352 }
366353 }
355 /// Poll one provider stream to its single terminal outcome.
356 fn consume(
357 &self,
358 stream: smith::provider::ProviderStream,
359 events: &mut Vec<AgentEvent>,
360 ) -> Result<Round> {
361 let mut round = Round {
362 text: String::new(),
363 tools: Vec::new(),
364 terminal: None,
365 };
366 for item in futures::executor::block_on_stream(stream) {
367 self.check_cancelled()?;
368 let event = item?;
369 match event.clone() {
370 StreamEvent::TextDelta { delta, .. } => round.text.push_str(&delta),
371 StreamEvent::ThinkingDelta { .. } | StreamEvent::ToolResult { .. } => {}
372 StreamEvent::ToolUse {
373 name,
374 input,
375 call_id,
376 ..
377 } => round.tools.push((call_id, name, input)),
378 StreamEvent::Stop { reason, usage } => {
379 round.terminal = Some((reason, usage.unwrap_or_default()));
380 // Terminal is emitted once; the stream ends here.
381 break;
382 }
383 StreamEvent::Error { error } => {
384 return Err(SmithError::Provider {
385 fault: smith::error::ProviderFault::Protocol { message: error },
386 });
387 }
388 }
389 events.push(AgentEvent::Streamed { event });
390 }
391 Ok(round)
392 }
368394 fn build_request(&self) -> ProviderRequest {
369395 // Requests carry the active branch since the compaction boundary.
370396 let since = self.tools.session.compaction_boundary;