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    #[serde(skip_serializing_if = "Option::is_none")]
34    #[schemars(extend("omitempty" = true))]
35    pub retry_token: Option<String>,
36    #[arbitrary(with = crate::arbitrary_util::arbitrary_u64)]
37    pub created: u64,
38    pub function: Option<crate::RemotePath>,
39    pub object: super::Object,
40    #[serde(skip_serializing_if = "Option::is_none")]
41    #[schemars(extend("omitempty" = true))]
42    pub usage: Option<agent::completions::response::Usage>,
43}
44
45impl AgentCompletionIds for FunctionProfileComputationChunk {
46    fn agent_completion_ids(&self) -> impl Iterator<Item = &str> + Send {
47        self.executions
48            .iter()
49            .flat_map(|e| e.agent_completion_ids())
50    }
51}
52
53impl FunctionProfileComputationChunk {
54    pub fn any_usage(&self) -> bool {
55        self.usage
56            .as_ref()
57            .is_some_and(agent::completions::response::Usage::any_usage)
58    }
59
60    pub fn push(
61        &mut self,
62        FunctionProfileComputationChunk {
63            executions,
64            executions_errors,
65            profile,
66            fitting_stats,
67            retry_token,
68            usage,
69            ..
70        }: &FunctionProfileComputationChunk,
71    ) {
72        self.push_executions(executions);
73        if let Some(true) = executions_errors {
74            self.executions_errors = Some(true);
75        }
76        if let Some(profile) = profile {
77            self.profile = Some(profile.clone());
78        }
79        if let Some(fitting_stats) = fitting_stats {
80            self.fitting_stats = Some(fitting_stats.clone());
81        }
82        if let Some(retry_token) = retry_token {
83            self.retry_token = Some(retry_token.clone());
84        }
85        match (&mut self.usage, usage) {
86            (Some(self_usage), Some(other_usage)) => {
87                self_usage.push(other_usage);
88            }
89            (None, Some(other_usage)) => {
90                self.usage = Some(other_usage.clone());
91            }
92            _ => {}
93        }
94    }
95
96    fn push_executions(
97        &mut self,
98        other_executions: &[super::FunctionExecutionChunk],
99    ) {
100        fn push_execution(
101            executions: &mut Vec<super::FunctionExecutionChunk>,
102            other: &super::FunctionExecutionChunk,
103        ) {
104            fn find_execution(
105                executions: &mut Vec<super::FunctionExecutionChunk>,
106                index: u64,
107            ) -> Option<&mut super::FunctionExecutionChunk> {
108                for execution in executions {
109                    if execution.index == index {
110                        return Some(execution);
111                    }
112                }
113                None
114            }
115            if let Some(execution) = find_execution(executions, other.index) {
116                execution.push(other);
117            } else {
118                executions.push(other.clone());
119            }
120        }
121        for other_execution in other_executions {
122            push_execution(&mut self.executions, other_execution);
123        }
124    }
125}