Skip to main content

sim_lib_openai_server/routes/
response_runtime.rs

1use sim_kernel::ContentId;
2
3use crate::{
4    ids::GatewayIdGenerator,
5    objects::{GatewayEvent, GatewayResponse},
6    runtime::{OpenAiFederation, OpenAiRunnerRegistry},
7};
8
9use super::errors::OpenAiRouteError;
10
11/// Bundles the eval targets a response run may dispatch to: the runner
12/// registry and an optional federation surface for cross-node eval.
13#[derive(Clone, Copy)]
14pub struct ResponseRuntimeTargets<'a> {
15    runners: &'a OpenAiRunnerRegistry,
16    federation: Option<&'a OpenAiFederation>,
17}
18
19impl<'a> ResponseRuntimeTargets<'a> {
20    /// Builds targets backed by the runner registry alone, with no federation.
21    pub fn runners(runners: &'a OpenAiRunnerRegistry) -> Self {
22        Self {
23            runners,
24            federation: None,
25        }
26    }
27
28    /// Builds targets backed by both the runner registry and a federation
29    /// surface.
30    pub fn with_federation(
31        runners: &'a OpenAiRunnerRegistry,
32        federation: &'a OpenAiFederation,
33    ) -> Self {
34        Self {
35            runners,
36            federation: Some(federation),
37        }
38    }
39
40    /// Returns the runner registry.
41    pub fn runners_ref(self) -> &'a OpenAiRunnerRegistry {
42        self.runners
43    }
44
45    /// Returns the federation surface, if one was configured.
46    pub fn federation_ref(self) -> Option<&'a OpenAiFederation> {
47        self.federation
48    }
49}
50
51/// Holds the per-kind id generators used to mint request, run, response, and
52/// event ids for a single response execution.
53#[derive(Clone, Debug)]
54pub struct ResponseIdGenerators {
55    pub(crate) request: GatewayIdGenerator,
56    pub(crate) run: GatewayIdGenerator,
57    pub(crate) response: GatewayIdGenerator,
58    event: GatewayIdGenerator,
59}
60
61impl ResponseIdGenerators {
62    /// Builds id generators seeded deterministically from `start`, so a given
63    /// seed always yields the same id sequence.
64    pub fn deterministic(start: u64) -> Self {
65        Self {
66            request: GatewayIdGenerator::deterministic("gwreq", start),
67            run: GatewayIdGenerator::deterministic("gwrun", start),
68            response: GatewayIdGenerator::deterministic("resp", start),
69            event: GatewayIdGenerator::deterministic("gwevt", start),
70        }
71    }
72
73    pub(crate) fn next_event_id(&mut self) -> sim_kernel::Result<String> {
74        self.event.next_id()
75    }
76}
77
78/// Captures the outcome of executing a `/v1/responses` request: the wire
79/// response plus the content-addressed ledger ids and events it produced.
80#[derive(Clone, Debug)]
81pub struct ResponseExecution {
82    pub(crate) response: GatewayResponse,
83    pub(crate) request_content_id: Option<ContentId>,
84    pub(crate) run_content_id: Option<ContentId>,
85    pub(crate) event_content_ids: Vec<ContentId>,
86    pub(crate) events: Vec<GatewayEvent>,
87    pub(crate) response_id: Option<String>,
88    pub(crate) response_created_at_ms: Option<u64>,
89    pub(crate) response_content_id: Option<ContentId>,
90}
91
92impl ResponseExecution {
93    /// Returns the wire response produced by the execution.
94    pub fn response(&self) -> &GatewayResponse {
95        &self.response
96    }
97
98    /// Returns the content id of the stored request, if it was recorded.
99    pub fn request_content_id(&self) -> Option<&ContentId> {
100        self.request_content_id.as_ref()
101    }
102
103    /// Returns the content id of the stored run, if it was recorded.
104    pub fn run_content_id(&self) -> Option<&ContentId> {
105        self.run_content_id.as_ref()
106    }
107
108    /// Returns the content ids of the stored events, in sequence order.
109    pub fn event_content_ids(&self) -> &[ContentId] {
110        &self.event_content_ids
111    }
112
113    /// Returns the events emitted during the execution.
114    pub fn events(&self) -> &[GatewayEvent] {
115        &self.events
116    }
117
118    /// Returns the generated response id, if a response was produced.
119    pub fn response_id(&self) -> Option<&str> {
120        self.response_id.as_deref()
121    }
122
123    /// Returns the response creation timestamp in milliseconds, if set.
124    pub fn response_created_at_ms(&self) -> Option<u64> {
125        self.response_created_at_ms
126    }
127
128    /// Returns the content id of the stored response object, if it was recorded.
129    pub fn response_content_id(&self) -> Option<&ContentId> {
130        self.response_content_id.as_ref()
131    }
132
133    pub(crate) fn error(error: OpenAiRouteError) -> Self {
134        Self {
135            response: error.into_response(),
136            request_content_id: None,
137            run_content_id: None,
138            event_content_ids: Vec::new(),
139            events: Vec::new(),
140            response_id: None,
141            response_created_at_ms: None,
142            response_content_id: None,
143        }
144    }
145}