mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-29 01:03:17 +00:00
* refactor(cache): organize v2 cache as a package * docs: clarify experimental v2 guidance * fix(cache): verify cache-hit accounting and preserve logging metadata * refactor(cache): separate execution facts from host accounting * refactor(rust): build messages routes with named dependencies * wip * fix(cache): preserve facade policy and preflight fallback * refactor(cache): defer shared Python logging changes * test(gateway-inference): allow dead code in shared test helpers Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(cache): key prepared requests and honor facade controls * feat(cache): use Python caches from Rust Messages inference * refactor(cache): separate native and Python cache adapters * refactor(cache): enforce shared composition and adapter boundaries * fix(cache): let Python key delegated Rust Messages entries --------- Co-authored-by: Yujong Lee <yujong@berri.ai> Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
104 lines
3.7 KiB
Rust
104 lines
3.7 KiB
Rust
use std::ops::ControlFlow;
|
|
|
|
use litellm_host::{
|
|
interceptors::Interceptors,
|
|
machine::{HostFailure, Machine, MachineStep},
|
|
protocol::{HostRequest, InterceptRequest, Protocol, Reply, StreamDelivery},
|
|
};
|
|
|
|
use crate::services::HostCallHandler;
|
|
|
|
type ProtocolOf<M> = <M as Machine>::Protocol;
|
|
type ErrorOf<M> = <ProtocolOf<M> as Protocol>::Error;
|
|
|
|
pub enum Boundary<M: Machine> {
|
|
Complete(M::Complete),
|
|
Open(<ProtocolOf<M> as Protocol>::StreamHead),
|
|
Chunk(<ProtocolOf<M> as Protocol>::Chunk),
|
|
}
|
|
|
|
/// Answers host calls and interceptors inline and stops at each stream delivery, holding its demand
|
|
/// reply until the consumer advances again. Dropping the driver drops the in-flight call.
|
|
pub struct Driver<M: Machine, S, H> {
|
|
machine: M,
|
|
services: S,
|
|
interceptors: H,
|
|
demand: Option<Reply<ControlFlow<()>>>,
|
|
}
|
|
|
|
impl<M, S, H> Driver<M, S, H>
|
|
where
|
|
M: Machine,
|
|
S: HostCallHandler<ProtocolOf<M>>,
|
|
H: Interceptors<ErrorOf<M>>,
|
|
{
|
|
pub fn new(machine: M, services: S, interceptors: H) -> Self {
|
|
Self {
|
|
machine,
|
|
services,
|
|
interceptors,
|
|
demand: None,
|
|
}
|
|
}
|
|
|
|
pub async fn advance(&mut self) -> Result<Boundary<M>, ErrorOf<M>> {
|
|
self.resume(ControlFlow::Continue(())).await
|
|
}
|
|
|
|
pub async fn detach(&mut self) -> Result<Boundary<M>, ErrorOf<M>> {
|
|
self.resume(ControlFlow::Break(())).await
|
|
}
|
|
|
|
/// Interrupts the machine with a failure the consumer hit at the last stream boundary,
|
|
/// dropping the held demand reply unanswered
|
|
pub async fn fail(&mut self, error: ErrorOf<M>) -> Result<M::Complete, ErrorOf<M>> {
|
|
self.demand = None;
|
|
self.machine.interrupt(HostFailure::Error(error)).await
|
|
}
|
|
|
|
async fn resume(&mut self, demand: ControlFlow<()>) -> Result<Boundary<M>, ErrorOf<M>> {
|
|
if let Some(reply) = self.demand.take() {
|
|
reply.send(demand);
|
|
}
|
|
loop {
|
|
let request = match self.machine.resume().await? {
|
|
MachineStep::Complete(complete) => return Ok(Boundary::Complete(complete)),
|
|
MachineStep::Suspended(request) => request,
|
|
};
|
|
let answered = match request {
|
|
HostRequest::Intercept(InterceptRequest::ResultReady { facts, reply }) => self
|
|
.interceptors
|
|
.result_ready(facts)
|
|
.await
|
|
.map(|()| reply.send(())),
|
|
HostRequest::HostCall(call) => self.services.handle_host_call(call).await,
|
|
HostRequest::Intercept(InterceptRequest::BeforeProviderRequest {
|
|
wire,
|
|
context,
|
|
reply,
|
|
}) => self
|
|
.interceptors
|
|
.before_provider_request(*wire, *context)
|
|
.await
|
|
.map(|wire| reply.send(wire)),
|
|
HostRequest::Intercept(InterceptRequest::AfterProviderResponse { raw, reply }) => {
|
|
self.interceptors
|
|
.after_provider_response(raw)
|
|
.await
|
|
.map(|()| reply.send(()))
|
|
}
|
|
HostRequest::Stream(StreamDelivery::Open(head, reply)) => {
|
|
self.demand = Some(reply);
|
|
return Ok(Boundary::Open(head));
|
|
}
|
|
HostRequest::Stream(StreamDelivery::Chunk(chunk, reply)) => {
|
|
self.demand = Some(reply);
|
|
return Ok(Boundary::Chunk(chunk));
|
|
}
|
|
};
|
|
if let Err(error) = answered {
|
|
return self.fail(error).await.map(Boundary::Complete);
|
|
}
|
|
}
|
|
}
|
|
}
|