Skip to main content

OpencodeClient

Struct OpencodeClient 

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

Async client for the opencode HTTP/SSE server.

Cloning is cheap; the underlying reqwest::Client is shared.

Implementations§

Source§

impl OpencodeClient

Source

pub fn builder() -> OpencodeClientBuilder

Start building a client. Equivalent to OpencodeClientBuilder::new.

Source

pub fn transport(&self) -> &HttpTransport

The underlying transport, exposing base-URL, auth, and the GET /event URL for an SSE subscriber.

Source

pub fn event_stream(&self, retry: RetryConfig) -> Result<EventStream>

Open the GET /event SSE stream, reusing this client’s base URL, auth, directory/workspace scope, and reqwest connection pool.

This is the ergonomic counterpart to OpencodeClient::list_messages: the same client drives both the low-latency event stream and the authoritative reconciliation poll, so credentials configured with OpencodeClientBuilder::auth flow to the stream without a detour through the OPENCODE_SERVER_PASSWORD environment variable.

§Errors

Returns crate::Error if the request cannot be prepared for streaming.

Source

pub async fn create_session( &self, params: &SessionCreateParams, ) -> Result<Session>

Create a new session — POST /session.

The response is the freshly created Session; its id (a ses… string) is used to address every subsequent per-session call.

Source

pub async fn prompt_async( &self, session_id: &str, params: &PromptAsyncParams, ) -> Result<()>

Submit a prompt — POST /session/{sessionID}/prompt_async.

Returns as soon as the server accepts the work (HTTP 204); the agent’s output is observed on the GET /event SSE stream and reconciled via OpencodeClient::list_messages.

Source

pub async fn list_messages( &self, session_id: &str, ) -> Result<Vec<MessageWithParts>>

List a session’s messages — GET /session/{sessionID}/message.

Returns every message with its parts. This is the authoritative reconciliation path for the best-effort SSE stream. For pagination use OpencodeClient::list_messages_page.

Source

pub async fn list_messages_page( &self, session_id: &str, limit: Option<u64>, before: Option<&str>, ) -> Result<Vec<MessageWithParts>>

Paginated variant of OpencodeClient::list_messages.

limit caps the number of messages returned; before is a message id cursor (results strictly older than it) for walking history backwards.

Source

pub async fn abort(&self, session_id: &str) -> Result<bool>

Abort in-flight work — POST /session/{sessionID}/abort.

Returns true when the session had work that was aborted.

Source

pub async fn respond_permission( &self, session_id: &str, permission_id: &str, reply: &PermissionReplyParams, ) -> Result<bool>

Reply to a permission request — POST /session/{sessionID}/permissions/{permissionID}.

§Deprecation

In the 1.18.5 spec this route (operation permission.respond) is marked deprecated in favor of the newer reply endpoints POST /permission/{requestID}/reply (operation permission.reply) and POST /api/session/{sessionID}/permission/{requestID}/reply (operation v2.session.permission.reply), neither of which this crate wraps yet. Reach either through the raw OpencodeClient::request escape hatch using those exact paths — note the session-scoped one requires the /api/ prefix. This deprecated route remains the reply channel for the permission.asked event and works on 1.18.5; a future opencode release may remove it.

§Correlation contract

Permission handling is deliberately split across two channels and correlating them is the consumer’s responsibility:

  1. A permission request arrives on the GET /event SSE stream as an Event::PermissionAsked event (wire type permission.asked), whose properties carry a ses… session id and a per… permission id. This is the only ask event that pairs with this call: the coexisting Event::PermissionV2Asked (permission.v2.asked) belongs to the unwrapped v2 reply endpoints, so do not feed its id here.
  2. The reply is this separate REST call, addressed by exactly those two ids. There is no server-side callback and no implicit pairing: the caller must remember which pending (session_id, permission_id) a reply answers.

PermissionReplyParams::response is one of PermissionReplyResponse::Once, Always, or Reject. Returns true when the reply was accepted; a stale or unknown permission id yields crate::Error::Http with status 404.

Source

pub async fn request<T: DeserializeOwned>( &self, method: Method, path: &str, body: Option<Value>, ) -> Result<T>

Raw escape hatch for endpoints this crate does not hand-wrap.

path is joined onto the configured base URL (leading slash optional) and used verbatim; body, when present, is sent as a JSON request body. The 2xx response is deserialized into T; non-2xx becomes crate::Error::Http.

Source

pub async fn request_unit( &self, method: Method, path: &str, body: Option<Value>, ) -> Result<()>

Raw escape hatch for endpoints that answer with an empty body.

Identical to OpencodeClient::request but discards the response body instead of deserializing it. Many opencode POST endpoints reply 204 No Content (e.g. prompt_async and several unwrapped routes); calling those through OpencodeClient::request would fail deserializing the empty body, so use this variant for them.

Trait Implementations§

Source§

impl Clone for OpencodeClient

Source§

fn clone(&self) -> OpencodeClient

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for OpencodeClient

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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: Sized + 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: Sized + 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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