Skip to main content

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

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