Skip to main content

RequestContext

Struct RequestContext 

Source
pub struct RequestContext {
    pub request_id: Uuid,
    pub user_id: Uuid,
    pub tenant_id: Uuid,
    pub produce_ai_key_id: Uuid,
    pub model: String,
    pub provider: Option<String>,
    pub messages: Vec<Message>,
    pub stream: bool,
    pub pricing_snapshot: PricingSnapshot,
    pub started_at: DateTime<Utc>,
    /* private fields */
}
Expand description

请求上下文:贯穿全链路的唯一状态载体

§设计说明

  • usage 字段使用 Arc<UsageAccumulator> 实现共享状态,Clone 时会共享同一个用量累积器
  • 通过 add_output_tokens()set_input_tokens() 方法安全地更新用量
  • 使用 usage_snapshot() 获取当前用量快照
  • provider 字段在路由确定后被设置,用于精确的定价查询

Fields§

§request_id: Uuid§user_id: Uuid§tenant_id: Uuid§produce_ai_key_id: Uuid§model: String§provider: Option<String>

Provider 名称(路由确定后设置)

§messages: Vec<Message>§stream: bool§pricing_snapshot: PricingSnapshot§started_at: DateTime<Utc>

Implementations§

Source§

impl RequestContext

Source

pub fn new( user_id: Uuid, tenant_id: Uuid, produce_ai_key_id: Uuid, model: impl Into<String>, messages: Vec<Message>, stream: bool, pricing_snapshot: PricingSnapshot, ) -> Self

Source

pub fn set_provider(&mut self, provider: impl Into<String>)

设置 Provider(路由确定后调用)

Source

pub fn update_pricing(&mut self, pricing: PricingSnapshot)

更新定价快照(路由后根据实际 provider 更新)

Source

pub fn duration(&self) -> Duration

获取请求持续时间

Source

pub fn usage_snapshot(&self) -> (u32, u32)

获取当前用量快照

Source

pub fn add_output_tokens(&self, tokens: u32)

添加输出 token(原子更新)

Source

pub fn set_input_tokens(&self, tokens: u32)

设置输入 token(原子更新)

Trait Implementations§

Source§

impl Clone for RequestContext

Source§

fn clone(&self) -> RequestContext

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for RequestContext

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.