4 Commits

Author SHA1 Message Date
6d49f9939c ci: install Rust on the Gitea runner and check fmt and clippy
All checks were successful
Rust / check (pull_request) Successful in 5m14s
The Gitea runner image doesn't include Rust, so every job failed at
'cargo: command not found'. Install the stable toolchain, cache the
build, and run cargo fmt --check and clippy alongside the tests.
2026-10-26 20:34:40 +00:00
c911fc382e style: run cargo fmt on examples 2026-10-26 20:34:40 +00:00
a8961b12e3 Merge pull request 'feat: allow missing name/args in FunctionCallData and add streamed argument fields (#12)' (#17) from feat/function-call-optional-fields into main
Some checks failed
Rust / build (push) Failing after 3s
Reviewed-on: #17
2026-10-26 19:59:46 +00:00
2f15b5298d feat: allow missing name/args in FunctionCallData and add streamed argument fields (#12)
Some checks failed
Rust / build (pull_request) Failing after 4s
2026-10-26 19:57:09 +00:00
6 changed files with 161 additions and 73 deletions

View File

@@ -10,13 +10,19 @@ env:
CARGO_TERM_COLOR: always
jobs:
build:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
# The Gitea runner image doesn't ship with Rust, unlike GitHub's.
- uses: dtolnay/rust-toolchain@stable
with:
components: clippy, rustfmt
- uses: Swatinem/rust-cache@v2
- name: Format
run: cargo fmt --check
- name: Clippy
run: cargo clippy --all-targets -- -D warnings
- name: Test
run: cargo test

View File

@@ -1,5 +1,5 @@
use console::style;
use dialoguer::{theme::ColorfulTheme, Input};
use dialoguer::{Input, theme::ColorfulTheme};
use gemini_rs::prelude::*;
use std::time::Duration;

View File

@@ -19,10 +19,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.add_text_part("Answer as if you were Yoda")
.build();
let user_prompt = vec![Content::builder()
.role(Role::User)
.add_text_part("What is the airspeed of an unladen swallow?")
.build()];
let user_prompt = vec![
Content::builder()
.role(Role::User)
.add_text_part("What is the airspeed of an unladen swallow?")
.build(),
];
let request = GenerateContentRequest::builder()
.contents(user_prompt)

View File

@@ -1,34 +1,36 @@
use gemini_rs::prelude::*;
use tokio_stream::StreamExt;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let authentication_manager = gcp_auth::provider().await?;
let api_endpoint = std::env::var("API_ENDPOINT")?;
let project_id = std::env::var("PROJECT_ID")?;
let location_id = std::env::var("LOCATION_ID")?;
let gemini = GeminiClient::new(
authentication_manager,
api_endpoint,
project_id,
location_id,
);
let prompt = vec![Content::builder()
.role(Role::User)
.add_text_part("Tell me the story of the genesis of the universe as a bedtime story.")
.build()];
let request = GenerateContentRequest::builder().contents(prompt).build();
let mut queue = gemini
.stream_generate_content(&request, "gemini-2.0-flash-001")
.await?;
while let Some(response) = queue.next().await {
println!("Response: {:?}", response?);
}
Ok(())
}
use gemini_rs::prelude::*;
use tokio_stream::StreamExt;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let authentication_manager = gcp_auth::provider().await?;
let api_endpoint = std::env::var("API_ENDPOINT")?;
let project_id = std::env::var("PROJECT_ID")?;
let location_id = std::env::var("LOCATION_ID")?;
let gemini = GeminiClient::new(
authentication_manager,
api_endpoint,
project_id,
location_id,
);
let prompt = vec![
Content::builder()
.role(Role::User)
.add_text_part("Tell me the story of the genesis of the universe as a bedtime story.")
.build(),
];
let request = GenerateContentRequest::builder().contents(prompt).build();
let mut queue = gemini
.stream_generate_content(&request, "gemini-2.0-flash-001")
.await?;
while let Some(response) = queue.next().await {
println!("Response: {:?}", response?);
}
Ok(())
}

View File

@@ -1,27 +1,29 @@
use gemini_rs::prelude::*;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let authentication_manager = gcp_auth::provider().await?;
let api_endpoint = std::env::var("API_ENDPOINT")?;
let project_id = std::env::var("PROJECT_ID")?;
let location_id = std::env::var("LOCATION_ID")?;
let gemini = GeminiClient::new(
authentication_manager,
api_endpoint,
project_id,
location_id,
);
let prompt = vec![Content::builder()
.role(Role::User)
.add_text_part("What is the airspeed of an unladen swallow?")
.build()];
let request = GenerateContentRequest::builder().contents(prompt).build();
let response = gemini.generate_content(&request, "gemini-pro").await?;
println!("Response: {:?}", response.candidates[0].get_text().unwrap());
Ok(())
}
use gemini_rs::prelude::*;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let authentication_manager = gcp_auth::provider().await?;
let api_endpoint = std::env::var("API_ENDPOINT")?;
let project_id = std::env::var("PROJECT_ID")?;
let location_id = std::env::var("LOCATION_ID")?;
let gemini = GeminiClient::new(
authentication_manager,
api_endpoint,
project_id,
location_id,
);
let prompt = vec![
Content::builder()
.role(Role::User)
.add_text_part("What is the airspeed of an unladen swallow?")
.build(),
];
let request = GenerateContentRequest::builder().contents(prompt).build();
let response = gemini.generate_content(&request, "gemini-pro").await?;
println!("Response: {:?}", response.candidates[0].get_text().unwrap());
Ok(())
}

View File

@@ -151,6 +151,8 @@ impl Part {
id: None,
name: name.into(),
args,
partial_args: None,
will_continue: None,
}),
..Default::default()
}
@@ -166,6 +168,8 @@ impl Part {
id: Some(id.into()),
name: name.into(),
args,
partial_args: None,
will_continue: None,
}),
..Default::default()
}
@@ -229,14 +233,35 @@ impl Part {
) -> Self {
self.add_function_response_part(FunctionResponsePart::inline_data(mime_type, data))
}
pub fn with_will_continue(mut self, will_continue: bool) -> Self {
if let Some(ref mut call) = self.function_call {
call.will_continue = Some(will_continue);
}
self
}
pub fn with_partial_args(mut self, partial_args: Vec<Value>) -> Self {
if let Some(ref mut call) = self.function_call {
call.partial_args = Some(partial_args);
}
self
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FunctionCallData {
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<String>,
#[serde(default, skip_serializing_if = "String::is_empty")]
pub name: String,
#[serde(default, skip_serializing_if = "Value::is_null")]
pub args: Value,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub partial_args: Option<Vec<Value>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub will_continue: Option<bool>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
@@ -345,6 +370,57 @@ mod tests {
);
}
#[test]
fn function_call_omits_unset_name_and_args_and_parses_missing_fields() {
// Deserializing without args (e.g. zero-argument function call)
let without_args: Part =
serde_json::from_value(json!({"functionCall": {"name": "f"}})).unwrap();
let fc = without_args.function_call.unwrap();
assert_eq!(fc.name, "f");
assert!(fc.args.is_null());
assert!(fc.partial_args.is_none());
assert!(fc.will_continue.is_none());
// Serializing when args is null omits args
assert_eq!(
serde_json::to_value(&Part {
function_call: Some(fc),
..Default::default()
})
.unwrap(),
json!({"functionCall": {"name": "f"}})
);
// Deserializing partial chunk without name and args
let partial_chunk: Part = serde_json::from_value(json!({
"functionCall": {
"partialArgs": [{"key": "val"}],
"willContinue": true
}
}))
.unwrap();
let fc_partial = partial_chunk.function_call.unwrap();
assert_eq!(fc_partial.name, "");
assert!(fc_partial.args.is_null());
assert_eq!(fc_partial.partial_args, Some(vec![json!({"key": "val"})]));
assert_eq!(fc_partial.will_continue, Some(true));
// Serializing partial chunk omits empty name and null args
assert_eq!(
serde_json::to_value(&Part {
function_call: Some(fc_partial),
..Default::default()
})
.unwrap(),
json!({
"functionCall": {
"partialArgs": [{"key": "val"}],
"willContinue": true
}
})
);
}
#[test]
fn function_response_id_handling() {
let without_id = Part::function_response("lookup", json!({"output": "ok"}));