rust-genai-types 0.3.1

Shared types for the Rust Gemini SDK
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
use crate::base64_serde;
use serde::{Deserialize, Serialize};

/// 引用日期。
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GoogleTypeDate {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub day: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub month: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub year: Option<i32>,
}

/// 引用信息。
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Citation {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub end_index: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub license: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub publication_date: Option<GoogleTypeDate>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub start_index: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub uri: Option<String>,
}

/// 引用元数据。
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CitationMetadata {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub citations: Option<Vec<Citation>>,
}

/// Author attribution for Maps.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PlaceAnswerSourcesAuthorAttribution {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub display_name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub photo_uri: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub uri: Option<String>,
}

/// Review snippet.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PlaceAnswerSourcesReviewSnippet {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub author_attribution: Option<PlaceAnswerSourcesAuthorAttribution>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub flag_content_uri: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub google_maps_uri: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub relative_publish_time_description: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub review: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub review_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,
}

/// Sources used to generate the place answer.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PlaceAnswerSources {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub flag_content_uri: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub review_snippets: Option<Vec<PlaceAnswerSourcesReviewSnippet>>,
}

/// Grounding chunk from Google Maps.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MapsChunk {
    pub uri: String,
    pub title: String,
    pub text: String,
    pub place_id: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub place_answer_sources: Option<PlaceAnswerSources>,
}

/// Rag chunk page span.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RagChunkPageSpan {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub first_page: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub last_page: Option<i32>,
}

/// RAG chunk.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RagChunk {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub page_span: Option<RagChunkPageSpan>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub text: Option<String>,
}

/// Grounding chunk from retrieved context.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RetrievedContextChunk {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub document_name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rag_chunk: Option<RagChunk>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub text: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub uri: Option<String>,
}

/// Grounding chunk from web.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct WebChunk {
    pub uri: String,
    pub title: String,
}

/// Grounding chunk union.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", untagged)]
pub enum GroundingChunk {
    Web {
        web: WebChunk,
    },
    RetrievedContext {
        retrieved_context: RetrievedContextChunk,
    },
    Maps {
        maps: MapsChunk,
    },
}

impl GroundingChunk {
    /// 获取来源 URI(如果存在)。
    #[must_use]
    pub fn uri(&self) -> Option<&str> {
        match self {
            Self::Web { web } => Some(web.uri.as_str()),
            Self::Maps { maps } => Some(maps.uri.as_str()),
            Self::RetrievedContext { retrieved_context } => retrieved_context.uri.as_deref(),
        }
    }

    /// 获取标题(如果存在)。
    #[must_use]
    pub fn title(&self) -> Option<&str> {
        match self {
            Self::Web { web } => Some(web.title.as_str()),
            Self::Maps { maps } => Some(maps.title.as_str()),
            Self::RetrievedContext { retrieved_context } => retrieved_context.title.as_deref(),
        }
    }
}

/// Segment of the content.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Segment {
    pub part_index: i32,
    pub start_index: i32,
    pub end_index: i32,
    pub text: String,
}

/// Grounding support.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GroundingSupport {
    #[serde(default)]
    pub grounding_chunk_indices: Vec<i32>,
    #[serde(default)]
    pub confidence_scores: Vec<f64>,
    pub segment: Segment,
}

/// Retrieval metadata.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RetrievalMetadata {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub google_search_dynamic_retrieval_score: Option<f32>,
}

/// Google search entry point.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SearchEntryPoint {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rendered_content: Option<String>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        with = "base64_serde::option"
    )]
    pub sdk_blob: Option<Vec<u8>>,
}

/// Source flagging URI.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GroundingMetadataSourceFlaggingUri {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub flag_content_uri: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source_id: Option<String>,
}

/// Grounding 元数据。
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct GroundingMetadata {
    #[serde(default)]
    pub grounding_chunks: Vec<GroundingChunk>,
    #[serde(default)]
    pub grounding_supports: Vec<GroundingSupport>,
    #[serde(default)]
    pub web_search_queries: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub search_entry_point: Option<SearchEntryPoint>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub retrieval_metadata: Option<RetrievalMetadata>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub google_maps_widget_context_token: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub retrieval_queries: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source_flagging_uris: Option<Vec<GroundingMetadataSourceFlaggingUri>>,
}

impl GroundingMetadata {
    /// 生成带内联引用的文本(使用 `grounding_supports` 的 `segment.end_index` 位置插入引用序号)。
    #[must_use]
    pub fn add_citations(&self, text: &str) -> String {
        if self.grounding_supports.is_empty() {
            return text.to_string();
        }

        let mut positions =
            std::collections::BTreeMap::<usize, std::collections::BTreeSet<i32>>::new();

        for support in &self.grounding_supports {
            let Ok(end_index) = usize::try_from(support.segment.end_index) else {
                continue;
            };
            let Some(byte_end) = char_index_to_byte(text, end_index) else {
                continue;
            };

            let entry = positions.entry(byte_end).or_default();
            for idx in &support.grounding_chunk_indices {
                if let Some(one_based) = idx.checked_add(1) {
                    if one_based > 0 {
                        entry.insert(one_based);
                    }
                }
            }
        }

        if positions.is_empty() {
            return text.to_string();
        }

        let mut output = text.to_string();
        for (pos, indices) in positions.into_iter().rev() {
            if pos > output.len() {
                continue;
            }
            let label = indices
                .into_iter()
                .map(|value| value.to_string())
                .collect::<Vec<_>>()
                .join(",");
            output.insert_str(pos, &format!(" [{label}]"));
        }

        output
    }

    /// 提取引用链接(按照 `grounding_chunks` 顺序去重)。
    #[must_use]
    pub fn citation_uris(&self) -> Vec<String> {
        let mut seen = std::collections::HashSet::new();
        let mut uris = Vec::new();

        for chunk in &self.grounding_chunks {
            if let Some(uri) = chunk.uri() {
                if seen.insert(uri.to_string()) {
                    uris.push(uri.to_string());
                }
            }
        }

        uris
    }
}

fn char_index_to_byte(text: &str, index: usize) -> Option<usize> {
    if index == 0 {
        return Some(0);
    }
    let mut count = 0usize;
    for (byte_idx, _) in text.char_indices() {
        if count == index {
            return Some(byte_idx);
        }
        count += 1;
    }
    if count == index {
        Some(text.len())
    } else {
        None
    }
}

// 兼容旧名称(避免外部依赖受影响)。
pub type GroundingChunkMapsPlaceAnswerSources = PlaceAnswerSources;
pub type GroundingChunkMapsPlaceAnswerSourcesReviewSnippet = PlaceAnswerSourcesReviewSnippet;
pub type GroundingChunkMapsPlaceAnswerSourcesAuthorAttribution =
    PlaceAnswerSourcesAuthorAttribution;
pub type GroundingChunkMaps = MapsChunk;
pub type GroundingChunkRetrievedContext = RetrievedContextChunk;
pub type GroundingChunkWeb = WebChunk;

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn grounding_chunk_uri_and_title() {
        let web = GroundingChunk::Web {
            web: WebChunk {
                uri: "https://example.com".to_string(),
                title: "Example".to_string(),
            },
        };
        let maps = GroundingChunk::Maps {
            maps: MapsChunk {
                uri: "https://maps.example.com".to_string(),
                title: "Map".to_string(),
                text: "info".to_string(),
                place_id: "place-1".to_string(),
                place_answer_sources: None,
            },
        };
        let retrieved = GroundingChunk::RetrievedContext {
            retrieved_context: RetrievedContextChunk {
                document_name: None,
                rag_chunk: None,
                text: None,
                title: Some("Doc".to_string()),
                uri: Some("https://doc.example.com".to_string()),
            },
        };

        assert_eq!(web.uri(), Some("https://example.com"));
        assert_eq!(maps.title(), Some("Map"));
        assert_eq!(retrieved.uri(), Some("https://doc.example.com"));
        assert_eq!(retrieved.title(), Some("Doc"));
    }

    #[test]
    fn search_entry_point_base64_roundtrip() {
        let entry = SearchEntryPoint {
            rendered_content: Some("rendered".to_string()),
            sdk_blob: Some(vec![1, 2, 3]),
        };
        let value = serde_json::to_value(&entry).unwrap();
        assert_eq!(
            value,
            json!({
                "renderedContent": "rendered",
                "sdkBlob": "AQID"
            })
        );

        let decoded: SearchEntryPoint = serde_json::from_value(value).unwrap();
        assert_eq!(decoded.sdk_blob, Some(vec![1, 2, 3]));
    }

    #[test]
    fn grounding_metadata_add_citations_and_uris() {
        let metadata = GroundingMetadata {
            grounding_chunks: vec![
                GroundingChunk::Web {
                    web: WebChunk {
                        uri: "https://a.example".to_string(),
                        title: "A".to_string(),
                    },
                },
                GroundingChunk::Web {
                    web: WebChunk {
                        uri: "https://b.example".to_string(),
                        title: "B".to_string(),
                    },
                },
            ],
            grounding_supports: vec![GroundingSupport {
                grounding_chunk_indices: vec![0, 1],
                confidence_scores: vec![0.9],
                segment: Segment {
                    part_index: 0,
                    start_index: 0,
                    end_index: 2,
                    text: "hi".to_string(),
                },
            }],
            ..Default::default()
        };

        let cited = metadata.add_citations("hi!");
        assert_eq!(cited, "hi [1,2]!");
        let uris = metadata.citation_uris();
        assert_eq!(uris, vec!["https://a.example", "https://b.example"]);
    }
}