unofficial-opencode-sdk 0.1.0

Unofficial Rust client SDK for the OpenCode server HTTP and SSE APIs
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
//! Preview client surface for OpenCode's `/api/*` V2 contract.
//!
//! OpenCode currently ships this contract through the official JavaScript
//! package's `/v2` export while the package itself remains on the 1.x release
//! line. The V2 schema is still evolving, so this module is intentionally
//! isolated from the legacy/current root client.

use std::collections::{BTreeMap, VecDeque};
use std::pin::Pin;
use std::task::{Context, Poll};

use bytes::Bytes;
use futures_core::Stream;
use futures_util::StreamExt;
use futures_util::stream::{self, BoxStream};
use reqwest::Method;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::{Error, SseDecoder};

mod discovery;
mod resources;
mod session;
pub use discovery::*;
pub use resources::*;
pub use session::*;

/// V2 client for OpenCode's `/api/*` HTTP surface.
#[derive(Clone)]
pub struct Client {
    inner: crate::Client,
    directory: Option<String>,
    workspace_id: Option<String>,
}

#[derive(Default)]
pub struct ClientBuilder {
    inner: crate::ClientBuilder,
    directory: Option<String>,
    workspace_id: Option<String>,
}

impl Client {
    pub fn builder() -> ClientBuilder {
        ClientBuilder::default()
    }

    pub(crate) fn from_current(client: crate::Client) -> Self {
        let directory = client.configured_directory().map(str::to_owned);
        let workspace_id = client.configured_workspace().map(str::to_owned);
        Self {
            inner: client,
            directory,
            workspace_id,
        }
    }

    pub fn session(&self) -> SessionApi<'_> {
        SessionApi { client: self }
    }

    pub fn events(&self) -> EventsApi<'_> {
        EventsApi { client: self }
    }
}

impl From<crate::Client> for Client {
    fn from(client: crate::Client) -> Self {
        Self::from_current(client)
    }
}

impl ClientBuilder {
    pub fn base_url(mut self, base_url: impl Into<String>) -> Self {
        self.inner = self.inner.base_url(base_url);
        self
    }

    pub fn http_client(mut self, client: reqwest::Client) -> Self {
        self.inner = self.inner.http_client(client);
        self
    }

    /// Set the default V2 location directory.
    pub fn directory(mut self, directory: impl Into<String>) -> Self {
        self.directory = Some(directory.into());
        self
    }

    /// Set the default V2 workspace ID.
    pub fn workspace_id(mut self, workspace_id: impl Into<String>) -> Self {
        self.workspace_id = Some(workspace_id.into());
        self
    }

    pub fn basic_auth(mut self, username: impl Into<String>, password: impl Into<String>) -> Self {
        self.inner = self.inner.basic_auth(username, password);
        self
    }

    pub fn password(mut self, password: impl Into<String>) -> Self {
        self.inner = self.inner.password(password);
        self
    }

    pub fn build(self) -> Result<Client, Error> {
        Ok(Client {
            inner: self.inner.build()?,
            directory: self.directory,
            workspace_id: self.workspace_id,
        })
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct LocationRef {
    pub directory: String,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "workspaceID"
    )]
    pub workspace_id: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct ModelRef {
    pub id: String,
    #[serde(rename = "providerID")]
    pub provider_id: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub variant: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Session {
    pub id: String,
    #[serde(default, rename = "parentID")]
    pub parent_id: Option<String>,
    #[serde(rename = "projectID")]
    pub project_id: String,
    #[serde(default)]
    pub agent: Option<String>,
    #[serde(default)]
    pub model: Option<ModelRef>,
    pub cost: f64,
    pub tokens: TokenUsage,
    pub time: SessionTime,
    pub title: String,
    pub location: LocationRef,
    #[serde(default)]
    pub subpath: Option<String>,
    #[serde(default)]
    pub revert: Option<Value>,
    #[serde(flatten)]
    pub extra: BTreeMap<String, Value>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct TokenUsage {
    pub input: u64,
    pub output: u64,
    pub reasoning: u64,
    pub cache: CacheUsage,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct CacheUsage {
    pub read: u64,
    pub write: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct SessionTime {
    pub created: u64,
    pub updated: u64,
    #[serde(default)]
    pub archived: Option<u64>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct Cursor {
    #[serde(default)]
    pub previous: Option<String>,
    #[serde(default)]
    pub next: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct SessionPage {
    pub data: Vec<Session>,
    pub cursor: Cursor,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum Order {
    Asc,
    Desc,
}

#[derive(Debug, Clone, Default, Serialize)]
pub struct ListSessionsOptions {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub workspace: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub order: Option<Order>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub search: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub directory: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub project: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub subpath: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cursor: Option<String>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct CreateSessionRequest {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub agent: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub model: Option<ModelRef>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub location: Option<LocationRef>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct PromptSource {
    pub start: u64,
    pub end: u64,
    pub text: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct PromptFileAttachment {
    pub uri: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub source: Option<PromptSource>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct PromptAgentAttachment {
    pub name: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub source: Option<PromptSource>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct PromptInput {
    pub text: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub files: Option<Vec<PromptFileAttachment>>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub agents: Option<Vec<PromptAgentAttachment>>,
}

impl PromptInput {
    pub fn text(text: impl Into<String>) -> Self {
        Self {
            text: text.into(),
            files: None,
            agents: None,
        }
    }
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum Delivery {
    Steer,
    Queue,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
pub struct PromptRequest {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub prompt: Option<PromptInput>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub delivery: Option<Delivery>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub resume: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct SessionInputAdmitted {
    pub admitted_seq: u64,
    pub id: String,
    #[serde(rename = "sessionID")]
    pub session_id: String,
    pub prompt: Value,
    pub delivery: Delivery,
    pub time_created: u64,
    #[serde(default)]
    pub promoted_seq: Option<u64>,
    #[serde(flatten)]
    pub extra: BTreeMap<String, Value>,
}

#[derive(Debug, Deserialize)]
struct DataEnvelope<T> {
    data: T,
}

pub struct SessionApi<'a> {
    client: &'a Client,
}

impl SessionApi<'_> {
    pub async fn list(&self, options: &ListSessionsOptions) -> Result<SessionPage, Error> {
        let mut url = self.client.inner.url("api/session")?;
        {
            let mut query = url.query_pairs_mut();
            if let Some(workspace) = options
                .workspace
                .as_deref()
                .or(self.client.workspace_id.as_deref())
            {
                query.append_pair("workspace", workspace);
            }
            if let Some(limit) = options.limit {
                query.append_pair("limit", &limit.to_string());
            }
            if let Some(order) = options.order {
                query.append_pair(
                    "order",
                    match order {
                        Order::Asc => "asc",
                        Order::Desc => "desc",
                    },
                );
            }
            if let Some(search) = options.search.as_deref() {
                query.append_pair("search", search);
            }
            if let Some(directory) = options
                .directory
                .as_deref()
                .or(self.client.directory.as_deref())
            {
                query.append_pair("directory", directory);
            }
            if let Some(project) = options.project.as_deref() {
                query.append_pair("project", project);
            }
            if let Some(subpath) = options.subpath.as_deref() {
                query.append_pair("subpath", subpath);
            }
            if let Some(cursor) = options.cursor.as_deref() {
                query.append_pair("cursor", cursor);
            }
        }
        let response = self
            .client
            .inner
            .request_url(Method::GET, url)
            .send()
            .await?;
        self.client.inner.decode(response).await
    }

    pub async fn create(&self, body: &CreateSessionRequest) -> Result<Session, Error> {
        let mut body = body.clone();
        if body.location.is_none() {
            if let Some(directory) = &self.client.directory {
                body.location = Some(LocationRef {
                    directory: directory.clone(),
                    workspace_id: self.client.workspace_id.clone(),
                });
            }
        }
        let response = self
            .client
            .inner
            .request_base(Method::POST, "api/session")?
            .json(&body)
            .send()
            .await?;
        let envelope: DataEnvelope<Session> = self.client.inner.decode(response).await?;
        Ok(envelope.data)
    }

    pub async fn get(&self, session_id: &str) -> Result<Session, Error> {
        let response = self
            .client
            .inner
            .request_base(Method::GET, &format!("api/session/{session_id}"))?
            .send()
            .await?;
        let envelope: DataEnvelope<Session> = self.client.inner.decode(response).await?;
        Ok(envelope.data)
    }

    pub async fn prompt(
        &self,
        session_id: &str,
        body: &PromptRequest,
    ) -> Result<SessionInputAdmitted, Error> {
        let response = self
            .client
            .inner
            .request_base(Method::POST, &format!("api/session/{session_id}/prompt"))?
            .json(body)
            .send()
            .await?;
        let envelope: DataEnvelope<SessionInputAdmitted> =
            self.client.inner.decode(response).await?;
        Ok(envelope.data)
    }

    pub async fn wait(&self, session_id: &str) -> Result<(), Error> {
        let response = self
            .client
            .inner
            .request_base(Method::POST, &format!("api/session/{session_id}/wait"))?
            .send()
            .await?;
        self.client.inner.ensure_success(response).await
    }

    pub async fn interrupt(&self, session_id: &str) -> Result<(), Error> {
        let response = self
            .client
            .inner
            .request_base(Method::POST, &format!("api/session/{session_id}/interrupt"))?
            .send()
            .await?;
        self.client.inner.ensure_success(response).await
    }

    /// Subscribe to the V2 durable event stream for one session.
    ///
    /// Event data is intentionally kept as raw text because the V2 durable
    /// event union is still evolving upstream.
    pub async fn events(
        &self,
        session_id: &str,
        after: Option<&str>,
    ) -> Result<EventStream, Error> {
        let mut url = self
            .client
            .inner
            .url(&format!("api/session/{session_id}/event"))?;
        if let Some(after) = after {
            url.query_pairs_mut().append_pair("after", after);
        }
        let response = self
            .client
            .inner
            .request_url(Method::GET, url)
            .send()
            .await?;
        if !response.status().is_success() {
            self.client.inner.ensure_success(response).await?;
            unreachable!();
        }
        Ok(event_stream_from_response(response))
    }
}

pub struct EventsApi<'a> {
    client: &'a Client,
}

impl EventsApi<'_> {
    /// Subscribe to the native V2 server event stream at `/api/event`.
    pub async fn subscribe(&self) -> Result<EventStream, Error> {
        let response = self
            .client
            .inner
            .request_base(Method::GET, "api/event")?
            .send()
            .await?;
        if !response.status().is_success() {
            self.client.inner.ensure_success(response).await?;
            unreachable!();
        }
        Ok(event_stream_from_response(response))
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Event {
    pub id: Option<String>,
    pub event: Option<String>,
    pub data: String,
}

pub struct EventStream {
    inner: BoxStream<'static, Result<Event, Error>>,
}

impl Stream for EventStream {
    type Item = Result<Event, Error>;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        Pin::new(&mut self.inner).poll_next(cx)
    }
}

type ByteStream = BoxStream<'static, Result<Bytes, reqwest::Error>>;

struct EventStreamState {
    bytes: ByteStream,
    decoder: SseDecoder,
    queued: VecDeque<Event>,
    finished: bool,
}

fn event_stream_from_response(response: reqwest::Response) -> EventStream {
    let state = EventStreamState {
        bytes: response.bytes_stream().boxed(),
        decoder: SseDecoder::new(),
        queued: VecDeque::new(),
        finished: false,
    };
    let inner = stream::unfold(state, |mut state| async move {
        loop {
            if let Some(event) = state.queued.pop_front() {
                return Some((Ok(event), state));
            }
            if state.finished {
                return None;
            }
            match state.bytes.next().await {
                Some(Ok(chunk)) => match state.decoder.push(&chunk) {
                    Ok(frames) => state.queued.extend(frames.into_iter().map(|frame| Event {
                        id: frame.id,
                        event: frame.event,
                        data: frame.data,
                    })),
                    Err(error) => return Some((Err(error), state)),
                },
                Some(Err(error)) => return Some((Err(Error::Http(error)), state)),
                None => {
                    state.finished = true;
                    match state.decoder.finish() {
                        Ok(frames) => state.queued.extend(frames.into_iter().map(|frame| Event {
                            id: frame.id,
                            event: frame.event,
                            data: frame.data,
                        })),
                        Err(error) => return Some((Err(error), state)),
                    }
                }
            }
        }
    })
    .boxed();
    EventStream { inner }
}