Skip to main content

objectiveai_sdk/vector/completions/response/unary/
vector_completion.rs

1//! Unary (non-streaming) vector completion response.
2
3use crate::{agent, vector::completions::response};
4use schemars::JsonSchema;
5use serde::{Deserialize, Serialize};
6
7/// A complete vector completion response (non-streaming).
8///
9/// Contains the final scores, all votes from the swarm, and the underlying
10/// agent completions that produced those votes.
11#[derive(Debug, Clone, Serialize, Deserialize, Default, JsonSchema)]
12#[schemars(rename = "vector.completions.response.unary.VectorCompletion")]
13pub struct VectorCompletion {
14    /// Unique identifier for this vector completion.
15    pub id: String,
16    /// The underlying agent completions from each agent in the swarm.
17    pub completions: Vec<super::AgentCompletion>,
18    /// Individual votes from each agent, showing their selections.
19    pub votes: Vec<response::Vote>,
20    /// Final weighted scores for each response option. Sums to 1.
21    #[serde(deserialize_with = "crate::serde_util::vec_decimal")]
22    #[schemars(with = "Vec<f64>")]
23    pub scores: Vec<rust_decimal::Decimal>,
24    /// Total weight allocated to each response option. Same length as `scores`.
25    /// For discrete votes, an LLM's full weight goes to its selected response.
26    /// For probabilistic votes, the weight is divided according to the distribution.
27    #[serde(deserialize_with = "crate::serde_util::vec_decimal")]
28    #[schemars(with = "Vec<f64>")]
29    pub weights: Vec<rust_decimal::Decimal>,
30    /// Unix timestamp when the completion was created.
31    pub created: u64,
32    /// ID of the swarm used for this completion.
33    pub swarm: String,
34    /// Object type identifier (`"vector.completion"`).
35    pub object: super::Object,
36    /// Aggregated token and cost usage across all completions.
37    pub usage: agent::completions::response::Usage,
38}
39
40impl VectorCompletion {
41    /// Normalize non-deterministic fields for test snapshot comparison.
42    pub fn normalize_for_tests(&mut self) {
43        self.id = String::new();
44        self.created = 0;
45        for completion in &mut self.completions {
46            completion.inner.normalize_for_tests();
47        }
48        self.votes.sort_by_key(|v| v.flat_swarm_index);
49
50        // sort completions by JSON representation since ordering is non-determinstic
51        self.completions
52            .sort_by_cached_key(|c| serde_json::to_string(&c.inner).unwrap());
53
54        // re-apply completion indices since indices are non-deterministic
55        let mut i = 0;
56        for completion in &mut self.completions {
57            completion.index = i;
58            i += 1;
59        }
60    }
61
62    /// Creates a default completion with uniform scores for the given number of responses.
63    pub fn default_from_request_responses_len(
64        request_responses_len: usize,
65    ) -> Self {
66        let weights = vec![rust_decimal::Decimal::ZERO; request_responses_len];
67        let scores =
68            vec![
69                rust_decimal::Decimal::ONE
70                    / rust_decimal::Decimal::from(request_responses_len);
71                request_responses_len
72            ];
73        Self {
74            id: String::new(),
75            completions: Vec::new(),
76            votes: Vec::new(),
77            scores,
78            weights,
79            created: 0,
80            swarm: String::new(),
81            object: super::Object::default(),
82            usage: agent::completions::response::Usage::default(),
83        }
84    }
85}
86
87impl From<response::streaming::VectorCompletionChunk> for VectorCompletion {
88    fn from(
89        response::streaming::VectorCompletionChunk {
90            id,
91            completions,
92            votes,
93            scores,
94            weights,
95            created,
96            swarm,
97            object,
98            usage,
99        }: response::streaming::VectorCompletionChunk,
100    ) -> Self {
101        Self {
102            id,
103            completions: completions
104                .into_iter()
105                .map(super::AgentCompletion::from)
106                .collect(),
107            votes,
108            scores,
109            weights,
110            created,
111            swarm,
112            object: object.into(),
113            usage: usage.unwrap_or_default(),
114        }
115    }
116}