Skip to main content

objectiveai_sdk/functions/profiles/computations/response/streaming/
function_profile_computation_chunk.rs

1use crate::agent::completions::response::streaming::AgentCompletionIds;
2use crate::{
3    agent,
4    functions::{self, profiles::computations::response},
5};
6use schemars::JsonSchema;
7use serde::{Deserialize, Serialize};
8
9#[derive(
10    Debug,
11    Clone,
12    PartialEq,
13    Serialize,
14    Deserialize,
15    JsonSchema,
16    arbitrary::Arbitrary,
17)]
18#[schemars(
19    rename = "functions.profiles.computations.response.streaming.FunctionProfileComputationChunk"
20)]
21pub struct FunctionProfileComputationChunk {
22    pub id: String,
23    pub executions: Vec<super::FunctionExecutionChunk>,
24    #[serde(skip_serializing_if = "Option::is_none")]
25    #[schemars(extend("omitempty" = true))]
26    pub executions_errors: Option<bool>,
27    #[serde(skip_serializing_if = "Option::is_none")]
28    #[schemars(extend("omitempty" = true))]
29    pub profile: Option<functions::InlineTasksProfile>,
30    #[serde(skip_serializing_if = "Option::is_none")]
31    #[schemars(extend("omitempty" = true))]
32    pub fitting_stats: Option<response::FittingStats>,
33    #[arbitrary(with = crate::arbitrary_util::arbitrary_u64)]
34    pub created: u64,
35    pub function: Option<crate::RemotePath>,
36    pub object: super::Object,
37    #[serde(skip_serializing_if = "Option::is_none")]
38    #[schemars(extend("omitempty" = true))]
39    pub usage: Option<agent::completions::response::Usage>,
40}
41
42impl AgentCompletionIds for FunctionProfileComputationChunk {
43    fn agent_completion_ids(&self) -> impl Iterator<Item = &str> + Send {
44        self.executions
45            .iter()
46            .flat_map(|e| e.agent_completion_ids())
47    }
48}
49
50impl FunctionProfileComputationChunk {
51    pub fn any_usage(&self) -> bool {
52        self.usage
53            .as_ref()
54            .is_some_and(agent::completions::response::Usage::any_usage)
55    }
56
57    pub fn push(
58        &mut self,
59        FunctionProfileComputationChunk {
60            executions,
61            executions_errors,
62            profile,
63            fitting_stats,
64            usage,
65            ..
66        }: &FunctionProfileComputationChunk,
67    ) {
68        self.push_executions(executions);
69        if let Some(true) = executions_errors {
70            self.executions_errors = Some(true);
71        }
72        if let Some(profile) = profile {
73            self.profile = Some(profile.clone());
74        }
75        if let Some(fitting_stats) = fitting_stats {
76            self.fitting_stats = Some(fitting_stats.clone());
77        }
78        match (&mut self.usage, usage) {
79            (Some(self_usage), Some(other_usage)) => {
80                self_usage.push(other_usage);
81            }
82            (None, Some(other_usage)) => {
83                self.usage = Some(other_usage.clone());
84            }
85            _ => {}
86        }
87    }
88
89    fn push_executions(
90        &mut self,
91        other_executions: &[super::FunctionExecutionChunk],
92    ) {
93        fn push_execution(
94            executions: &mut Vec<super::FunctionExecutionChunk>,
95            other: &super::FunctionExecutionChunk,
96        ) {
97            fn find_execution(
98                executions: &mut Vec<super::FunctionExecutionChunk>,
99                index: u64,
100            ) -> Option<&mut super::FunctionExecutionChunk> {
101                for execution in executions {
102                    if execution.index == index {
103                        return Some(execution);
104                    }
105                }
106                None
107            }
108            if let Some(execution) = find_execution(executions, other.index) {
109                execution.push(other);
110            } else {
111                executions.push(other.clone());
112            }
113        }
114        for other_execution in other_executions {
115            push_execution(&mut self.executions, other_execution);
116        }
117    }
118}