use sim_kernel::ContentId;
use crate::{
ids::GatewayIdGenerator,
objects::{GatewayEvent, GatewayResponse},
runtime::{OpenAiFederation, OpenAiRunnerRegistry},
};
use super::errors::OpenAiRouteError;
#[derive(Clone, Copy)]
pub struct ResponseRuntimeTargets<'a> {
runners: &'a OpenAiRunnerRegistry,
federation: Option<&'a OpenAiFederation>,
}
impl<'a> ResponseRuntimeTargets<'a> {
pub fn runners(runners: &'a OpenAiRunnerRegistry) -> Self {
Self {
runners,
federation: None,
}
}
pub fn with_federation(
runners: &'a OpenAiRunnerRegistry,
federation: &'a OpenAiFederation,
) -> Self {
Self {
runners,
federation: Some(federation),
}
}
pub fn runners_ref(self) -> &'a OpenAiRunnerRegistry {
self.runners
}
pub fn federation_ref(self) -> Option<&'a OpenAiFederation> {
self.federation
}
}
#[derive(Clone, Debug)]
pub struct ResponseIdGenerators {
pub(crate) request: GatewayIdGenerator,
pub(crate) run: GatewayIdGenerator,
pub(crate) response: GatewayIdGenerator,
event: GatewayIdGenerator,
}
impl ResponseIdGenerators {
pub fn deterministic(start: u64) -> Self {
Self {
request: GatewayIdGenerator::deterministic("gwreq", start),
run: GatewayIdGenerator::deterministic("gwrun", start),
response: GatewayIdGenerator::deterministic("resp", start),
event: GatewayIdGenerator::deterministic("gwevt", start),
}
}
pub(crate) fn next_event_id(&mut self) -> sim_kernel::Result<String> {
self.event.next_id()
}
}
#[derive(Clone, Debug)]
pub struct ResponseExecution {
pub(crate) response: GatewayResponse,
pub(crate) request_content_id: Option<ContentId>,
pub(crate) run_content_id: Option<ContentId>,
pub(crate) event_content_ids: Vec<ContentId>,
pub(crate) events: Vec<GatewayEvent>,
pub(crate) response_id: Option<String>,
pub(crate) response_created_at_ms: Option<u64>,
pub(crate) response_content_id: Option<ContentId>,
}
impl ResponseExecution {
pub fn response(&self) -> &GatewayResponse {
&self.response
}
pub fn request_content_id(&self) -> Option<&ContentId> {
self.request_content_id.as_ref()
}
pub fn run_content_id(&self) -> Option<&ContentId> {
self.run_content_id.as_ref()
}
pub fn event_content_ids(&self) -> &[ContentId] {
&self.event_content_ids
}
pub fn events(&self) -> &[GatewayEvent] {
&self.events
}
pub fn response_id(&self) -> Option<&str> {
self.response_id.as_deref()
}
pub fn response_created_at_ms(&self) -> Option<u64> {
self.response_created_at_ms
}
pub fn response_content_id(&self) -> Option<&ContentId> {
self.response_content_id.as_ref()
}
pub(crate) fn error(error: OpenAiRouteError) -> Self {
Self {
response: error.into_response(),
request_content_id: None,
run_content_id: None,
event_content_ids: Vec::new(),
events: Vec::new(),
response_id: None,
response_created_at_ms: None,
response_content_id: None,
}
}
}