#[derive(Debug, Clone, PartialEq)]
pub enum Role {
System,
User,
Assistant,
}
#[derive(Debug, Clone, PartialEq)]
pub enum ImageSource {
Base64 { media_type: String, data: String },
Url(String),
}
#[derive(Debug, Clone, PartialEq)]
pub struct ToolUse {
pub id: String,
pub name: String,
pub input: String,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ToolResult {
pub tool_use_id: String,
pub content: String,
pub is_error: bool,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Content {
Text(String),
Image(ImageSource),
ToolUse(ToolUse),
ToolResult(ToolResult),
}
#[derive(Debug, Clone, PartialEq)]
pub struct Message {
pub role: Role,
pub content: Vec<Content>,
pub response_metadata: Option<ResponseMetadata>,
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct ResponseMetadata {
pub safety_ratings: Option<Vec<SafetyRating>>,
pub grounding: Option<GroundingMetadata>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct SafetyRating {
pub category: String,
pub probability: String,
pub blocked: bool,
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct GroundingMetadata {
pub web_search_queries: Vec<String>,
pub grounding_chunks: Vec<GroundingChunk>,
pub grounding_supports: Vec<GroundingSupport>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct GroundingChunk {
pub source_type: String,
pub uri: Option<String>,
pub title: Option<String>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct GroundingSupport {
pub start_index: usize,
pub end_index: usize,
pub chunk_indices: Vec<usize>,
pub confidence_scores: Vec<f32>,
}
impl Message {
pub fn new(role: Role, text: impl Into<String>) -> Self {
Self {
role,
content: vec![Content::Text(text.into())],
response_metadata: None,
}
}
pub fn with_content(role: Role, content: Vec<Content>) -> Self {
Self {
role,
content,
response_metadata: None,
}
}
pub fn with_metadata(role: Role, content: Vec<Content>, metadata: ResponseMetadata) -> Self {
Self {
role,
content,
response_metadata: Some(metadata),
}
}
pub fn system(text: impl Into<String>) -> Self {
Self::new(Role::System, text)
}
pub fn user(text: impl Into<String>) -> Self {
Self::new(Role::User, text)
}
pub fn assistant(text: impl Into<String>) -> Self {
Self::new(Role::Assistant, text)
}
pub fn tool_result(
tool_use_id: impl Into<String>,
content: impl Into<String>,
is_error: bool,
) -> Self {
Self {
role: Role::User,
content: vec![Content::ToolResult(ToolResult {
tool_use_id: tool_use_id.into(),
content: content.into(),
is_error,
})],
response_metadata: None,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Tool {
pub name: String,
pub description: String,
pub input_schema: String,
}
impl Tool {
pub fn new(
name: impl Into<String>,
description: impl Into<String>,
input_schema: impl Into<String>,
) -> Self {
Self {
name: name.into(),
description: description.into(),
input_schema: input_schema.into(),
}
}
}
#[derive(Debug, Clone, PartialEq, Default)]
pub enum ToolChoice {
#[default]
Auto,
Any,
Tool(String),
None,
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Metadata {
pub user_id: Option<String>,
}
#[derive(Debug, Clone, Default)]
pub struct MessageOptions {
pub temperature: Option<f32>,
pub max_tokens: Option<u32>,
pub model: Option<String>,
pub tools: Option<Vec<Tool>>,
pub tool_choice: Option<ToolChoice>,
pub stop_sequences: Option<Vec<String>>,
pub top_p: Option<f32>,
pub top_k: Option<u32>,
pub metadata: Option<Metadata>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum StreamEvent {
MessageStart { message_id: String, model: String },
ContentBlockStart {
index: usize,
block_type: ContentBlockType,
},
TextDelta { index: usize, text: String },
InputJsonDelta { index: usize, json: String },
ContentBlockStop { index: usize },
MessageDelta {
stop_reason: Option<String>,
usage: Option<Usage>,
},
MessageStop,
Ping,
}
#[derive(Debug, Clone, PartialEq)]
pub enum ContentBlockType {
Text,
ToolUse { id: String, name: String },
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Usage {
pub input_tokens: u32,
pub output_tokens: u32,
}