Skip to main content

Client

Struct Client 

Source
pub struct Client { /* private fields */ }
Expand description

Stateful client for multi-turn Gemini CLI sessions.

Each Client instance manages a single Gemini CLI subprocess and JSON-RPC session. Calls to send / [send_content] return streams of Message values translated from raw session/update notifications.

§Lifecycle

  1. Construct with Client::new.
  2. Call connect — this spawns the subprocess and runs the handshake.
  3. Call send one or more times to converse with the model.
  4. Call close when finished. Dropping without closing is safe but may leave the subprocess running briefly until the OS reclaims it.

§Threading

Client is Send but not Sync. Share across tasks by wrapping in Arc<Mutex<Client>> when concurrent access is required.

Implementations§

Source§

impl Client

Source

pub fn new(config: ClientConfig) -> Result<Self>

Create a new client with the given configuration.

Resolves the gemini binary path (via config.cli_path or PATH) and constructs a GeminiTransport. The subprocess is not spawned until connect is called.

§Errors

Returns Error::CliNotFound when the binary cannot be located on PATH and config.cli_path is None.

Source

pub fn with_gemini_transport( config: ClientConfig, transport: Arc<GeminiTransport>, ) -> Self

Create a client backed by a caller-supplied GeminiTransport.

Useful when the caller has already constructed the transport with custom parameters (e.g. a non-default working directory or extra env vars).

Source

pub fn session_id(&self) -> Option<&str>

Return the session ID assigned by the server, or None before connect is called.

Source

pub fn prompt(&self) -> &str

Return the prompt field from the config.

Exposed as a convenience for the free-function wrappers in lib.rs that need to send the initial prompt without a separate send() call.

Source

pub fn is_connected(&self) -> bool

Return true if connect has been called successfully.

Source

pub async fn connect(&mut self) -> Result<SessionInfo>

Connect to the Gemini CLI and establish a session.

Performs the full initialisation sequence in order:

  1. Spawn the subprocess via Transport::connect.
  2. Take the notification stream (must happen before any requests).
  3. Register the optional [PermissionHandler] as the reverse-request handler.
  4. Send the initialize JSON-RPC request and await the result.
  5. Send session/new (or session/load when config.resume is set).
  6. Initialise the [TranslationContext] and hook context.

Returns SessionInfo describing the established session.

§Errors
Source

pub async fn send( &self, message: &str, ) -> Result<impl Stream<Item = Result<Message>> + '_>

Send a plain-text prompt and return a stream of translated Message values.

Convenience wrapper around send_content that constructs a single UserContent::Text block from the provided string slice.

§Errors

Returns Error::NotConnected if connect has not been called. Returns Error::Config if a UserPromptSubmit hook blocks the send.

Source

pub async fn send_content( &self, content: Vec<UserContent>, ) -> Result<impl Stream<Item = Result<Message>> + '_>

Send structured content and return a stream of translated Message values.

Accepts any mix of UserContent variants (text, base-64 image, URL image). The content is serialised to WireContentBlock format and sent to the CLI as a session/prompt JSON-RPC notification. The returned stream drains session/update notifications from the shared channel until the transport closes or the caller drops the stream.

§Backpressure

The notification channel is shared across all send_content calls on the same client. Only one stream should be active at a time; concurrent polling will contend on the notification_stream Mutex and may interleave results.

§Errors

Returns Error::NotConnected if connect has not been called. Returns Error::Config if a UserPromptSubmit hook blocks the send.

Source

pub async fn interrupt(&self) -> Result<()>

Interrupt the current in-progress prompt turn.

Sends a session/cancel notification to the CLI (best-effort) and then delivers a process-level interrupt signal via the transport (SIGINT on Unix, CTRL_C_EVENT on Windows).

§Errors

The session/cancel notification errors are silently ignored. Transport interrupt errors are propagated.

Source

pub async fn close(&mut self) -> Result<()>

Close the client and terminate the CLI subprocess.

Fires the Stop lifecycle hook before closing the transport. The call is idempotent — invoking it on an already-closed client is safe.

§Errors

Propagates errors from the transport’s close() implementation. Hook errors are silently ignored.

Auto Trait Implementations§

§

impl !Freeze for Client

§

impl !RefUnwindSafe for Client

§

impl Send for Client

§

impl Sync for Client

§

impl Unpin for Client

§

impl UnsafeUnpin for Client

§

impl !UnwindSafe for Client

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. 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<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