Skip to main content

AsyncChatbot

Struct AsyncChatbot 

Source
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

Source

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-1PSID cookie value
  • secure_1psidts - The __Secure-1PSIDTS cookie value (may be empty)
  • model - The model configuration to use
  • proxy - 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.

Source

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 send
  • image - 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.

Source

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.

Source

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.

Source

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.

Source

pub fn conversation_id(&self) -> &str

Returns the current conversation ID.

Source

pub fn model(&self) -> &Model

Returns the current model configuration.

Source

pub fn reset(&mut self)

Resets conversation IDs to start a fresh session.

Authentication (cookies and session token) is preserved.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more