mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-28 00:00:38 +00:00
* refactor(rust): extract litellm-host-native as the shared Rust host driver Move service and hook dispatch out of host-http into a Driver that owns the machine and Rust handlers, returning at completion or a stream boundary and holding the demand reply until the consumer advances. Move the in-process runner onto the same driver. host-http now layers encoding, SSE, body polling and lifecycle observation over it. host-python keeps driving litellm-host directly Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(rust): interrupt the machine when the in-process stream consumer fails Restores the pre-refactor interruption path for StreamConsumer errors via Driver::fail and ports the generic run lifecycle tests into host-native. Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * refactor(rust): separate the machine contract from coroutine execution * auth update * refactor(rust): use standard flow control for host requests * style(rust): keep host driver imports formatted * chores * mostly relocation * refactor(rust): separate interceptors from queued observers * refactor(rust): centralize legacy callback mappings and lifecycle * docs: define Python host boundaries and migration plan * refactor: enforce Python host and bridge boundaries * refactor(rust): separate operations from callback composition * refactor(rust): compose SDK policy through call hooks --------- Co-authored-by: Yujong Lee <yujong@berri.ai> Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
115 lines
3.4 KiB
Rust
115 lines
3.4 KiB
Rust
//! The caller's public call as the legacy `Logging` contract sees it. Legacy callbacks
|
|
//! receive these exact objects and may mutate them, so the call keeps them for its whole
|
|
//! lifetime. No other callback host has that obligation, which is why nothing outside
|
|
//! this crate holds them.
|
|
|
|
use litellm_host_python::lookup;
|
|
use pyo3::{
|
|
gc::{PyTraverseError, PyVisit},
|
|
prelude::*,
|
|
types::{PyDict, PyTuple},
|
|
};
|
|
|
|
pub struct PublicCall {
|
|
args: Py<PyTuple>,
|
|
kwargs: Py<PyDict>,
|
|
request: Py<PyAny>,
|
|
}
|
|
|
|
impl PublicCall {
|
|
/// Copies the keyword arguments once, so the legacy path's rewrites never reach the
|
|
/// caller's own dict while every value keeps its identity.
|
|
pub fn capture(
|
|
request: &Bound<'_, PyAny>,
|
|
args: &Bound<'_, PyTuple>,
|
|
kwargs: &Bound<'_, PyDict>,
|
|
) -> PyResult<Self> {
|
|
Ok(Self {
|
|
args: args.clone().unbind(),
|
|
kwargs: kwargs.copy()?.unbind(),
|
|
request: request.clone().unbind(),
|
|
})
|
|
}
|
|
|
|
pub fn arguments(&self, py: Python<'_>) -> Py<PyDict> {
|
|
self.kwargs.clone_ref(py)
|
|
}
|
|
|
|
pub(crate) fn args(&self) -> &Py<PyTuple> {
|
|
&self.args
|
|
}
|
|
|
|
/// The keyword view the legacy path currently reads: the caller's copy until
|
|
/// `function_setup`, then each rewrite (setup, deployment hook, the driver's preflight)
|
|
/// in turn.
|
|
pub(crate) fn kwargs(&self) -> &Py<PyDict> {
|
|
&self.kwargs
|
|
}
|
|
|
|
pub(crate) fn set_kwargs(&mut self, kwargs: Py<PyDict>) {
|
|
self.kwargs = kwargs;
|
|
}
|
|
|
|
pub(crate) fn lookup<'py>(
|
|
&self,
|
|
py: Python<'py>,
|
|
name: &str,
|
|
) -> PyResult<Option<Bound<'py, PyAny>>> {
|
|
lookup(self.kwargs.bind(py), self.request.bind(py), name)
|
|
}
|
|
|
|
pub(crate) fn traverse(&self, visit: &PyVisit<'_>) -> Result<(), PyTraverseError> {
|
|
visit.call(&self.args)?;
|
|
visit.call(&self.kwargs)?;
|
|
visit.call(&self.request)
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
fn capture<'py>(py: Python<'py>, source: &std::ffi::CStr) -> (PublicCall, Bound<'py, PyDict>) {
|
|
let locals = PyDict::new(py);
|
|
py.run(source, Some(&locals), Some(&locals)).unwrap();
|
|
let request = locals.get_item("request").unwrap().unwrap();
|
|
let kwargs = locals
|
|
.get_item("kwargs")
|
|
.unwrap()
|
|
.unwrap()
|
|
.cast_into::<PyDict>()
|
|
.unwrap();
|
|
let call = PublicCall::capture(&request, &PyTuple::empty(py), &kwargs).unwrap();
|
|
(call, locals)
|
|
}
|
|
|
|
#[rstest::rstest]
|
|
fn capture_copies_the_keyword_dict_without_copying_its_values() {
|
|
Python::initialize();
|
|
Python::attach(|py| {
|
|
let (call, locals) = capture(
|
|
py,
|
|
c"
|
|
pages = [0]
|
|
class Request:
|
|
pass
|
|
request = Request()
|
|
kwargs = {'pages': pages}
|
|
",
|
|
);
|
|
let caller = locals
|
|
.get_item("kwargs")
|
|
.unwrap()
|
|
.unwrap()
|
|
.cast_into::<PyDict>()
|
|
.unwrap();
|
|
call.kwargs()
|
|
.bind(py)
|
|
.set_item("litellm_call_id", "call")
|
|
.unwrap();
|
|
assert!(!caller.contains("litellm_call_id").unwrap());
|
|
let pages = locals.get_item("pages").unwrap().unwrap();
|
|
assert!(call.lookup(py, "pages").unwrap().unwrap().is(&pages));
|
|
});
|
|
}
|
|
}
|