Skip to main content

gproxy_protocol/adapt/generate/gemini_responses/image_resources/
responses.rs

1use super::*;
2
3impl ResponsesViaGemini {
4    /// Materialize actual native fileData images and convert that view. An
5    /// image's signature goes to the client with its bytes, so nothing is kept.
6    pub async fn convert_response_with_image_resources<S: StateStore, R: ResourceAccess>(
7        &mut self,
8        native: g::GenerateContentResponseBody,
9        facts: GeminiReturnFacts,
10        _state: &GenerationStateAccess<'_, S>,
11        resources: &GenerationResources<'_, R>,
12        progress: &mut ImageResourceProgress<g::GenerateContentResponseBody, R::PublishedHandle>,
13    ) -> Result<Converted<r::GenerateContentResponseBody>, TransformError> {
14        let native = native.into_declared();
15        let view = progress.reads.materialize(&native, resources).await?;
16        self.convert_response(view, facts)
17    }
18    /// One buffered generation POST followed by scoped image reads. Recovery
19    /// retains the native result and never repeats the generation request.
20    #[allow(clippy::too_many_arguments)]
21    pub async fn invoke_with_image_resources<U: Upstream, S: StateStore, R: ResourceAccess>(
22        &mut self,
23        upstream: &U,
24        target: &U::Target,
25        limits: CodecLimits,
26        state: &GenerationStateAccess<'_, S>,
27        resources: &GenerationResources<'_, R>,
28        progress: &mut ImageResourceProgress<g::GenerateContentResponseBody, R::PublishedHandle>,
29        facts: impl FnOnce(&g::GenerateContentResponseBody) -> Result<GeminiReturnFacts, TransformError>,
30    ) -> Result<GenerationOutcome<r::GenerateContentResponseBody>, TransformError> {
31        transport::bind(
32            (&self.endpoint, &self.selected_model),
33            &self.identities,
34            (&self.original_request, &self.target_request),
35            state,
36            limits,
37            &mut progress.generation,
38            false,
39        )?;
40        if !transport::send(
41            upstream,
42            target,
43            self.endpoint.clone(),
44            self.target_request.clone(),
45            limits,
46            &mut progress.generation,
47        )
48        .await?
49        {
50            return Ok(transport::rejected(&progress.generation));
51        }
52        self.recover_with_image_resources(limits, state, resources, progress, facts)
53            .await
54    }
55    pub async fn recover_with_image_resources<S: StateStore, R: ResourceAccess>(
56        &mut self,
57        limits: CodecLimits,
58        state: &GenerationStateAccess<'_, S>,
59        resources: &GenerationResources<'_, R>,
60        progress: &mut ImageResourceProgress<g::GenerateContentResponseBody, R::PublishedHandle>,
61        facts: impl FnOnce(&g::GenerateContentResponseBody) -> Result<GeminiReturnFacts, TransformError>,
62    ) -> Result<GenerationOutcome<r::GenerateContentResponseBody>, TransformError> {
63        transport::bind(
64            (&self.endpoint, &self.selected_model),
65            &self.identities,
66            (&self.original_request, &self.target_request),
67            state,
68            limits,
69            &mut progress.generation,
70            true,
71        )?;
72        if progress
73            .generation
74            .raw_response
75            .as_ref()
76            .is_none_or(|response| !response.status.is_success())
77        {
78            return Err(TransformError::invalid_result(
79                "generation.recovery",
80                "successful native response required",
81            ));
82        }
83        let native = transport::recover_native(&mut progress.generation, limits)?;
84        let facts = facts(&native)?;
85        let converted = self
86            .convert_response_with_image_resources(native, facts, state, resources, progress)
87            .await?;
88        transport::finish(
89            &mut progress.generation,
90            converted,
91            self.report.clone(),
92            limits,
93        )
94    }
95}