pub struct AsyncChatbot { /* private fields */ }Expand description
Async chatbot client for interacting with Gemini.
This client is stateful. Each successful call to ask updates
internal conversation IDs so the next call continues the same thread. Call
reset to start a new conversation while keeping cookies valid.
§Example
use gemini_chat_api::{load_cookies, AsyncChatbot, Model, Result};
#[tokio::main]
async fn main() -> Result<()> {
let (psid, psidts) = load_cookies("cookies.json")?;
let mut chatbot = AsyncChatbot::new(&psid, &psidts, Model::default(), None, 30).await?;
// Continues the same conversation thread across calls.
let first = chatbot.ask("Summarize Rust ownership in one paragraph.", None).await?;
println!("{}", first.content);
let followup = chatbot.ask("Now give me a short code example.", None).await?;
println!("{}", followup.content);
// Start a new conversation thread (cookies/session stay valid).
chatbot.reset();
let fresh = chatbot.ask("New topic: explain HTTP caching.", None).await?;
println!("{}", fresh.content);
Ok(())
}Implementations§
Source§impl AsyncChatbot
impl AsyncChatbot
Sourcepub async fn new(
secure_1psid: &str,
secure_1psidts: &str,
model: Model,
proxy: Option<&str>,
timeout: u64,
) -> Result<Self>
pub async fn new( secure_1psid: &str, secure_1psidts: &str, model: Model, proxy: Option<&str>, timeout: u64, ) -> Result<Self>
Creates a new AsyncChatbot instance and fetches the session token.
§Arguments
secure_1psid- The__Secure-1PSIDcookie valuesecure_1psidts- The__Secure-1PSIDTScookie value (may be empty)model- The model configuration to useproxy- Optional proxy URL (applied to all requests)timeout- Request timeout in seconds
§Returns
A fully initialized client with a valid session token.
§Errors
Returns an error if authentication fails (Error::Authentication),
if the network request fails (Error::Network), or if the session token
cannot be extracted (Error::Parse).
There is no retry or polling behavior; a single request is attempted.
Timeouts are handled by reqwest and surface as Error::Network.
Sourcepub async fn ask(
&mut self,
message: &str,
image: Option<&[u8]>,
) -> Result<ChatResponse>
pub async fn ask( &mut self, message: &str, image: Option<&[u8]>, ) -> Result<ChatResponse>
Sends a message and returns the parsed response.
§Arguments
message- The message text to sendimage- Optional image bytes to upload and attach
§Returns
A ChatResponse containing the reply and metadata.
§Errors
Returns an error if the client is not initialized (Error::NotInitialized),
if the image upload fails (Error::Upload), if the network request fails
(Error::Network), or if the backend response cannot be parsed
(Error::Parse).
There is no retry or polling behavior. Timeouts are handled by reqwest
and surface as Error::Network. External failures (Gemini backend,
upload endpoint, or connectivity issues) are returned as errors.
Sourcepub async fn save_conversation(
&self,
file_path: &str,
conversation_name: &str,
) -> Result<()>
pub async fn save_conversation( &self, file_path: &str, conversation_name: &str, ) -> Result<()>
Saves the current conversation state to a JSON file.
The file format is a JSON array of SavedConversation values. If an
entry with the same conversation_name already exists, it is replaced.
Sourcepub async fn load_conversations(
&self,
file_path: &str,
) -> Result<Vec<SavedConversation>>
pub async fn load_conversations( &self, file_path: &str, ) -> Result<Vec<SavedConversation>>
Loads all saved conversations from a JSON file.
If the file does not exist, this returns an empty vector.
Sourcepub async fn load_conversation(
&mut self,
file_path: &str,
conversation_name: &str,
) -> Result<bool>
pub async fn load_conversation( &mut self, file_path: &str, conversation_name: &str, ) -> Result<bool>
Loads a specific conversation by name and applies it to this client.
If the saved model name is unrecognized, the current model is left unchanged.
Returns true if the conversation was found and loaded.
Sourcepub fn conversation_id(&self) -> &str
pub fn conversation_id(&self) -> &str
Returns the current conversation ID.