#![allow(
clippy::redundant_pub_crate,
reason = "Keep shared worker/test delivery helpers explicitly crate scoped"
)]
use std::io::{self, Write};
use bonsai_prism_runtime::{Result, runtime::Finish, swift_session::TextEvent};
use serde_json::{Value, json};
pub(crate) fn write_record(output: &mut impl Write, value: &Value) -> Result<()> {
serde_json::to_writer(&mut *output, value)?;
output.write_all(b"\n")?;
output.flush()?;
Ok(())
}
fn write_delta(output: &mut impl Write, id: u64, mut text: &str) -> Result<()> {
while !text.is_empty() {
let mut end = text.len().min(16 * 1024);
while !text.is_char_boundary(end) {
end -= 1;
}
write_record(output, &json!({"type":"delta","id":id,"text":&text[..end]}))?;
text = &text[end..];
}
Ok(())
}
pub(crate) struct Delivery<'a, W> {
output: &'a mut W,
id: u64,
terminal_sent: bool,
transport_failed: bool,
}
impl<'a, W: Write> Delivery<'a, W> {
pub(crate) const fn new(output: &'a mut W, id: u64) -> Self {
Self {
output,
id,
terminal_sent: false,
transport_failed: false,
}
}
pub(crate) fn emit(&mut self, event: &TextEvent<'_>) -> Result<()> {
if self.terminal_sent || self.transport_failed {
return Err(io::Error::other("Swift generation delivery already ended").into());
}
let terminal = matches!(event, TextEvent::Complete(_));
let result = match event {
TextEvent::Delta(text) => write_delta(self.output, self.id, text),
TextEvent::Complete(completion) => write_record(
self.output,
&json!({
"type":"complete","id":self.id,"prompt_tokens":completion.prompt_tokens,
"generated_tokens":completion.generated_tokens,
"finish_reason":if completion.finish == Finish::Stop {"stop"} else {"length"},
}),
),
};
self.transport_failed = result.is_err();
self.terminal_sent = terminal && result.is_ok();
result
}
pub(crate) fn finish(self, succeeded: bool, reset_required: bool) -> Result<()> {
if self.transport_failed || self.terminal_sent != succeeded {
return Err(io::Error::other("Swift generation delivery cannot resume").into());
}
if !succeeded {
write_record(
self.output,
&json!({"type":"error","id":self.id,
"error":"generation_failed","reset_required":reset_required}),
)?;
}
Ok(())
}
}