aKode fase 5: rinomina interna (crate akode-*, akode-rs/, akode-cli/, binario akode, AKODE_*, ~/.akode, snapshot)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
56
akode-rs/model-provider/src/request_preparation.rs
Normal file
56
akode-rs/model-provider/src/request_preparation.rs
Normal file
@@ -0,0 +1,56 @@
|
||||
//! Keeps request preparation bound to the credential revision it started with.
|
||||
|
||||
use std::future::Future;
|
||||
use std::io;
|
||||
|
||||
use akode_login::AuthChangeState;
|
||||
use akode_login::RequestAuthChange;
|
||||
use akode_protocol::error::Result;
|
||||
|
||||
use crate::ModelProvider;
|
||||
|
||||
/// Prepare a request without dispatching it. When the request uses managed auth,
|
||||
/// repeat preparation after a credential refresh and fail if the owner changes.
|
||||
pub async fn prepare_provider_request<T, Fut>(
|
||||
provider: &dyn ModelProvider,
|
||||
check_auth: bool,
|
||||
mut prepare: impl FnMut(Option<AuthChangeState>) -> Fut,
|
||||
) -> Result<T>
|
||||
where
|
||||
Fut: Future<Output = Result<T>>,
|
||||
{
|
||||
let auth_state = || {
|
||||
provider
|
||||
.auth_manager()
|
||||
.map(|manager| *manager.auth_change_state_receiver().borrow())
|
||||
};
|
||||
let mut before = auth_state();
|
||||
loop {
|
||||
let prepared = prepare(before).await?;
|
||||
if !check_auth {
|
||||
return Ok(prepared);
|
||||
}
|
||||
// A managed provider can begin or cease exposing its auth manager during setup.
|
||||
let after = auth_state();
|
||||
let change = match (before, after) {
|
||||
(Some(before), Some(after)) => {
|
||||
after.compare_for_request(before.owner_generation, before)
|
||||
}
|
||||
(None, None) => RequestAuthChange::Unchanged,
|
||||
_ => RequestAuthChange::OwnerChanged,
|
||||
};
|
||||
match change {
|
||||
RequestAuthChange::Unchanged => return Ok(prepared),
|
||||
RequestAuthChange::CredentialsChanged => before = after,
|
||||
RequestAuthChange::OwnerChanged => {
|
||||
return Err(
|
||||
io::Error::other("account changed while preparing model request").into(),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "request_preparation_tests.rs"]
|
||||
mod tests;
|
||||
Reference in New Issue
Block a user