pub struct Client { /* private fields */ }Expand description
OpenCode client for interacting with the server.
Implementations§
Source§impl Client
impl Client
Sourcepub fn builder() -> ClientBuilder
pub fn builder() -> ClientBuilder
Create a new client builder.
Sourcepub fn sessions(&self) -> SessionsApi
pub fn sessions(&self) -> SessionsApi
Get the sessions API.
Sourcepub fn messages(&self) -> MessagesApi
pub fn messages(&self) -> MessagesApi
Get the messages API.
Sourcepub fn permissions(&self) -> PermissionsApi
pub fn permissions(&self) -> PermissionsApi
Get the permissions API.
Sourcepub fn questions(&self) -> QuestionsApi
pub fn questions(&self) -> QuestionsApi
Get the questions API.
Sourcepub fn providers(&self) -> ProvidersApi
pub fn providers(&self) -> ProvidersApi
Get the providers API.
Sourcepub fn project(&self) -> ProjectApi
pub fn project(&self) -> ProjectApi
Get the project API.
Sourcepub fn worktree(&self) -> WorktreeApi
pub fn worktree(&self) -> WorktreeApi
Get the worktree API.
Sourcepub async fn run_simple_text(&self, text: impl Into<String>) -> Result<Session>
pub async fn run_simple_text(&self, text: impl Into<String>) -> Result<Session>
Simple helper to create session and send a text prompt.
Note: This method returns immediately after sending the prompt.
The AI response will arrive asynchronously via SSE events.
Use [subscribe_session] to receive the response.
§Errors
Returns an error if session creation or prompt fails.
Sourcepub async fn create_session_with_title(
&self,
title: impl Into<String>,
) -> Result<Session>
pub async fn create_session_with_title( &self, title: impl Into<String>, ) -> Result<Session>
Create a session with a title.
This convenience helper wraps crate::http::sessions::SessionsApi::create_with.
§Errors
Returns an error if session creation fails.
Sourcepub async fn send_text_async(
&self,
session_id: &str,
text: impl Into<String>,
model: Option<ModelRef>,
) -> Result<()>
pub async fn send_text_async( &self, session_id: &str, text: impl Into<String>, model: Option<ModelRef>, ) -> Result<()>
Send plain text asynchronously to a session.
This convenience helper wraps crate::http::messages::MessagesApi::send_text_async.
The server returns an empty body on success.
§Errors
Returns an error if the request fails.
Source§impl Client
impl Client
Sourcepub async fn wait_for_idle_text(
&self,
session_id: &str,
timeout: Duration,
) -> Result<String>
pub async fn wait_for_idle_text( &self, session_id: &str, timeout: Duration, ) -> Result<String>
Wait until a session reaches idle state and collect streamed assistant text.
This helper listens to message.part.updated and idle/status events for the session.
§Errors
Returns an error if the stream closes, times out, or the session emits session.error.
Sourcepub async fn send_text_async_and_wait_for_idle(
&self,
session_id: &str,
text: impl Into<String>,
model: Option<ModelRef>,
timeout: Duration,
) -> Result<String>
pub async fn send_text_async_and_wait_for_idle( &self, session_id: &str, text: impl Into<String>, model: Option<ModelRef>, timeout: Duration, ) -> Result<String>
Send text asynchronously and wait until session idle while collecting text output.
This subscribes before sending to avoid missing early stream events.
§Errors
Returns an error if sending fails, stream closes, times out, or session emits session.error.
Sourcepub fn sse_subscriber(&self) -> SseSubscriber
pub fn sse_subscriber(&self) -> SseSubscriber
Get an SSE subscriber for streaming events.
Sourcepub async fn subscribe(&self) -> Result<SseSubscription>
pub async fn subscribe(&self) -> Result<SseSubscription>
Subscribe to all events for the configured directory with default options.
This subscribes to the /event endpoint which streams all events
for the directory specified in the client configuration.
§Errors
Returns an error if the subscription cannot be created.
Sourcepub async fn subscribe_typed(&self) -> Result<SseSubscription>
pub async fn subscribe_typed(&self) -> Result<SseSubscription>
Subscribe to all events for the configured directory as typed events.
This is equivalent to Self::subscribe, but explicitly named to
distinguish it from Self::subscribe_raw.
Sourcepub async fn subscribe_session(
&self,
session_id: &str,
) -> Result<SseSubscription>
pub async fn subscribe_session( &self, session_id: &str, ) -> Result<SseSubscription>
Subscribe to events filtered by session ID with default options.
Events are filtered client-side to only include events matching the specified session ID.
§Errors
Returns an error if the subscription cannot be created.
Sourcepub async fn session_event_router_with_options(
&self,
opts: SessionEventRouterOptions,
) -> Result<SessionEventRouter>
pub async fn session_event_router_with_options( &self, opts: SessionEventRouterOptions, ) -> Result<SessionEventRouter>
Create a new session event router with one upstream /event subscription.
This returns a dedicated router instance and does not modify the default
cached router used by Self::subscribe_session.
Sourcepub async fn session_event_router(&self) -> Result<SessionEventRouter>
pub async fn session_event_router(&self) -> Result<SessionEventRouter>
Get the default session event router.
The first call lazily creates the router; subsequent calls return the same router instance.
Sourcepub async fn subscribe_raw(&self) -> Result<RawSseSubscription>
pub async fn subscribe_raw(&self) -> Result<RawSseSubscription>
Subscribe to raw JSON frames from /event for debugging.
§Errors
Returns an error if the subscription cannot be created.
Sourcepub async fn subscribe_global(&self) -> Result<SseSubscription>
pub async fn subscribe_global(&self) -> Result<SseSubscription>
Subscribe to global events with default options (all directories).
§Errors
Returns an error if the subscription cannot be created.
Sourcepub async fn subscribe_typed_global(&self) -> Result<SseSubscription>
pub async fn subscribe_typed_global(&self) -> Result<SseSubscription>
Subscribe to global events as typed events (all directories).
§Errors
Returns an error if the subscription cannot be created.