opencode-codes 1.18.5

Typed Rust SDK for the opencode agent server: serde models of its OpenAPI 3.1 HTTP/SSE protocol, plus an async (Tokio) client and a managed local-server launcher for driving opencode sessions, prompts, and permission flows.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
//! High-level async client for the opencode server.
//!
//! [`OpencodeClient`] wraps the low-level [`crate::http::HttpTransport`] with
//! typed methods for the six hand-wrapped endpoints. Construct one with
//! [`OpencodeClient::builder`]:
//!
//! ```rust,ignore
//! use opencode_codes::client_async::OpencodeClient;
//! use opencode_codes::protocol_generated::types::SessionCreateParams;
//!
//! # async fn demo() -> opencode_codes::Result<()> {
//! let client = OpencodeClient::builder()
//!     .base_url("http://127.0.0.1:4096")
//!     .build()?;
//! let session = client.create_session(&SessionCreateParams {
//!     title: Some("demo".into()),
//!     ..Default::default()
//! }).await?;
//! # Ok(())
//! # }
//! ```
//!
//! # Reconciliation
//!
//! The `GET /event` SSE stream (see [`crate::sse`]) is best-effort and must not
//! be trusted as the sole source of truth. After observing activity on the
//! stream, poll [`OpencodeClient::list_messages`] to reconcile against the
//! server's authoritative message state.

use std::time::Duration;

use reqwest::{Client, Method};
use serde::de::DeserializeOwned;
use serde_json::Value;

use crate::error::Result;
use crate::http::{BasicAuth, HttpTransport, Scope};
use crate::protocol_generated::types::{
    MessageWithParts, PermissionReplyParams, PromptAsyncParams, Session, SessionCreateParams,
};
use crate::sse::{EventStream, RetryConfig};

/// Base URL of a default local `opencode serve` instance.
pub const DEFAULT_BASE_URL: &str = "http://127.0.0.1:4096";

/// Async client for the opencode HTTP/SSE server.
///
/// Cloning is cheap; the underlying [`reqwest::Client`] is shared.
#[derive(Clone, Debug)]
pub struct OpencodeClient {
    transport: HttpTransport,
}

impl OpencodeClient {
    /// Start building a client. Equivalent to [`OpencodeClientBuilder::new`].
    pub fn builder() -> OpencodeClientBuilder {
        OpencodeClientBuilder::new()
    }

    /// The underlying transport, exposing base-URL, auth, and the `GET /event`
    /// URL for an SSE subscriber.
    pub fn transport(&self) -> &HttpTransport {
        &self.transport
    }

    /// 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.
    pub fn event_stream(&self, retry: RetryConfig) -> Result<EventStream> {
        EventStream::from_request(self.transport.event_request(), retry)
    }

    /// 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.
    pub async fn create_session(&self, params: &SessionCreateParams) -> Result<Session> {
        let body = serde_json::to_value(params)?;
        self.transport
            .request_json(
                Method::POST,
                &self.transport.session_create_url(),
                Some(body),
            )
            .await
    }

    /// 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`].
    pub async fn prompt_async(&self, session_id: &str, params: &PromptAsyncParams) -> Result<()> {
        let body = serde_json::to_value(params)?;
        self.transport
            .request_unit(
                Method::POST,
                &self.transport.prompt_async_url(session_id),
                Some(body),
            )
            .await
    }

    /// 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`].
    pub async fn list_messages(&self, session_id: &str) -> Result<Vec<MessageWithParts>> {
        self.list_messages_page(session_id, None, None).await
    }

    /// 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.
    pub async fn list_messages_page(
        &self,
        session_id: &str,
        limit: Option<u64>,
        before: Option<&str>,
    ) -> Result<Vec<MessageWithParts>> {
        self.transport
            .request_json(
                Method::GET,
                &self.transport.messages_url(session_id, limit, before),
                None,
            )
            .await
    }

    /// Abort in-flight work — `POST /session/{sessionID}/abort`.
    ///
    /// Returns `true` when the session had work that was aborted.
    pub async fn abort(&self, session_id: &str) -> Result<bool> {
        self.transport
            .request_json(Method::POST, &self.transport.abort_url(session_id), None)
            .await
    }

    /// 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`](crate::protocol_generated::types::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`](crate::protocol_generated::types::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`](crate::protocol_generated::types::PermissionReplyResponse::Once),
    /// [`Always`](crate::protocol_generated::types::PermissionReplyResponse::Always),
    /// or [`Reject`](crate::protocol_generated::types::PermissionReplyResponse::Reject).
    /// Returns `true` when the reply was accepted; a stale or unknown permission
    /// id yields [`crate::Error::Http`] with status 404.
    pub async fn respond_permission(
        &self,
        session_id: &str,
        permission_id: &str,
        reply: &PermissionReplyParams,
    ) -> Result<bool> {
        let body = serde_json::to_value(reply)?;
        self.transport
            .request_json(
                Method::POST,
                &self.transport.permission_url(session_id, permission_id),
                Some(body),
            )
            .await
    }

    /// 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`].
    pub async fn request<T: DeserializeOwned>(
        &self,
        method: Method,
        path: &str,
        body: Option<Value>,
    ) -> Result<T> {
        self.transport
            .request_json(method, &self.transport.join(path), body)
            .await
    }

    /// 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.
    pub async fn request_unit(
        &self,
        method: Method,
        path: &str,
        body: Option<Value>,
    ) -> Result<()> {
        self.transport
            .request_unit(method, &self.transport.join(path), body)
            .await
    }
}

/// Builder for [`OpencodeClient`].
#[derive(Clone, Debug)]
pub struct OpencodeClientBuilder {
    base_url: String,
    auth: Option<BasicAuth>,
    timeout: Option<Duration>,
    client: Option<Client>,
    scope: Scope,
}

impl Default for OpencodeClientBuilder {
    fn default() -> Self {
        Self {
            base_url: DEFAULT_BASE_URL.to_string(),
            auth: None,
            timeout: None,
            client: None,
            scope: Scope::default(),
        }
    }
}

impl OpencodeClientBuilder {
    /// A builder defaulting to [`DEFAULT_BASE_URL`] with no auth or timeout.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the opencode server base URL (e.g. `http://127.0.0.1:4096`).
    #[must_use]
    pub fn base_url(mut self, base_url: impl Into<String>) -> Self {
        self.base_url = base_url.into();
        self
    }

    /// Attach explicit HTTP Basic credentials.
    #[must_use]
    pub fn auth(mut self, auth: BasicAuth) -> Self {
        self.auth = Some(auth);
        self
    }

    /// Attach HTTP Basic credentials derived from `OPENCODE_SERVER_PASSWORD`
    /// (username `"opencode"`). A no-op when the variable is unset or empty.
    ///
    /// This is the explicit opt-in for reading the environment; the request path
    /// never consults it implicitly.
    #[must_use]
    pub fn auth_from_env(mut self) -> Self {
        if let Some(auth) = BasicAuth::from_env() {
            self.auth = Some(auth);
        }
        self
    }

    /// Apply a per-request timeout to every call the client makes.
    ///
    /// Applied per request, so it also constrains an injected
    /// [`reqwest::Client`] that was built without a timeout.
    #[must_use]
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.timeout = Some(timeout);
        self
    }

    /// Inject a pre-configured [`reqwest::Client`] (connection pools, proxies,
    /// custom TLS). When omitted, a default client is built.
    #[must_use]
    pub fn reqwest_client(mut self, client: Client) -> Self {
        self.client = Some(client);
        self
    }

    /// Scope every session endpoint (and the `GET /event` stream) to a project
    /// `directory`.
    ///
    /// `opencode serve` can manage several directories at once; without this the
    /// server uses its own working directory. Set it to target a specific
    /// project on a multi-directory server. Build one client per directory (the
    /// clone is cheap) to drive several concurrently.
    #[must_use]
    pub fn directory(mut self, directory: impl Into<String>) -> Self {
        self.scope.directory = Some(directory.into());
        self
    }

    /// Scope every session endpoint (and the `GET /event` stream) to a
    /// `workspace` identifier. See [`directory`](Self::directory).
    #[must_use]
    pub fn workspace(mut self, workspace: impl Into<String>) -> Self {
        self.scope.workspace = Some(workspace.into());
        self
    }

    /// Set the full directory/workspace [`Scope`] at once.
    #[must_use]
    pub fn scope(mut self, scope: Scope) -> Self {
        self.scope = scope;
        self
    }

    /// Build the [`OpencodeClient`].
    ///
    /// # Errors
    ///
    /// Returns [`crate::Error::Transport`] if a default [`reqwest::Client`] must
    /// be constructed and its builder fails.
    pub fn build(self) -> Result<OpencodeClient> {
        let client = match self.client {
            Some(client) => client,
            None => Client::builder().build()?,
        };
        let transport =
            HttpTransport::new(client, self.base_url, self.timeout, self.auth, self.scope);
        Ok(OpencodeClient { transport })
    }
}

#[cfg(all(test, feature = "integration-tests"))]
mod tests {
    use super::*;
    use crate::protocol_generated::types::{PromptAsyncParamsPartsItem, TextPartInput};

    fn client() -> OpencodeClient {
        let base_url = std::env::var("OPENCODE_BASE_URL")
            .unwrap_or_else(|_| "http://127.0.0.1:41999".to_string());
        OpencodeClient::builder()
            .base_url(base_url)
            .auth_from_env()
            .timeout(Duration::from_secs(30))
            .build()
            .expect("client builds")
    }

    #[tokio::test]
    async fn create_list_abort_roundtrip() {
        let client = client();
        let session = client
            .create_session(&SessionCreateParams {
                title: Some("opencode-codes integration probe".into()),
                agent: None,
                metadata: None,
                model: None,
                parent_id: None,
                permission: None,
                workspace_id: None,
            })
            .await
            .expect("create session");
        assert!(session.id.starts_with("ses"));

        let messages = client
            .list_messages(&session.id)
            .await
            .expect("list messages");
        assert!(messages.is_empty());

        let aborted = client.abort(&session.id).await.expect("abort");
        // No work was running, but the endpoint still answers with a boolean.
        let _ = aborted;
    }

    #[tokio::test]
    async fn respond_to_unknown_permission_is_404() {
        let client = client();
        let session = client
            .create_session(&SessionCreateParams {
                title: Some("opencode-codes permission probe".into()),
                agent: None,
                metadata: None,
                model: None,
                parent_id: None,
                permission: None,
                workspace_id: None,
            })
            .await
            .expect("create session");

        let err = client
            .respond_permission(
                &session.id,
                "per_does_not_exist",
                &PermissionReplyParams {
                    response: "reject".into(),
                },
            )
            .await
            .expect_err("stale permission id must fail");
        match err {
            crate::Error::Http { status, .. } => assert_eq!(status, 404),
            other => panic!("expected HTTP 404, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn raw_request_escape_hatch() {
        let client = client();
        let config: Value = client
            .request(Method::GET, "/config", None)
            .await
            .expect("raw GET /config");
        assert!(config.is_object());
    }

    #[test]
    fn prompt_parts_serialize_shape() {
        let params = PromptAsyncParams {
            agent: None,
            format: None,
            message_id: None,
            model: None,
            no_reply: None,
            parts: vec![PromptAsyncParamsPartsItem::Text(TextPartInput {
                id: None,
                ignored: None,
                metadata: None,
                synthetic: None,
                text: "hello".into(),
                time: None,
                type_: String::new(),
            })],
            system: None,
            tools: None,
            variant: None,
        };
        let value = serde_json::to_value(&params).expect("serialize");
        assert_eq!(value["parts"][0]["type"], "text");
        assert_eq!(value["parts"][0]["text"], "hello");
    }
}