use std::fmt;
use promptforge_tool_picker::ToolPicker;
use crate::client::GatewayClient;
use crate::model::ModelCatalog;
use crate::{Error, Result};
use super::config::RunLimits;
#[derive(Clone, Copy)]
#[non_exhaustive]
pub struct ResolutionContext<'a> {
pub(crate) picker: &'a ToolPicker,
pub(crate) models: &'a ModelCatalog,
}
impl<'a> ResolutionContext<'a> {
#[must_use]
pub fn new(picker: &'a ToolPicker, models: &'a ModelCatalog) -> ResolutionContext<'a> {
ResolutionContext { picker, models }
}
}
impl fmt::Debug for ResolutionContext<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ResolutionContext").finish_non_exhaustive()
}
}
pub(crate) fn env_client_with_limits(limits: RunLimits) -> Result<GatewayClient> {
GatewayClient::from_env()
.map(|client| client.with_request_limits(limits.timeout(), limits.response_bytes()))
.map_err(Error::from)
}
#[derive(Clone)]
pub(crate) enum GatewaySource {
Ready(GatewayClient),
Env(RunLimits),
}
impl GatewaySource {
pub(crate) fn from_optional(client: Option<GatewayClient>, limits: RunLimits) -> GatewaySource {
client.map_or(GatewaySource::Env(limits), GatewaySource::Ready)
}
pub(crate) fn resolve(&self) -> Result<GatewayClient> {
match self {
GatewaySource::Ready(client) => Ok(client.clone()),
GatewaySource::Env(limits) => env_client_with_limits(*limits),
}
}
}