unofficial-opencode-sdk 0.1.1

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
use std::collections::BTreeMap;

use reqwest::Method;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use super::{ModelRef, Order, PermissionRequest, QuestionRequest, ServeDialect, SessionApi};
use crate::Error;

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum SessionActiveState {
    Running,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct SessionActive {
    #[serde(rename = "type")]
    pub state: SessionActiveState,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct SessionHistory {
    #[serde(default)]
    pub data: Vec<Value>,
    pub has_more: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct SessionMessagesPage {
    #[serde(default)]
    pub data: Vec<Value>,
    pub cursor: super::Cursor,
}

#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct SessionMessagesOptions {
    pub limit: Option<u32>,
    pub order: Option<Order>,
    pub cursor: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum FileDiffStatus {
    Added,
    Modified,
    Deleted,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct FileDiff {
    pub path: String,
    pub status: FileDiffStatus,
    pub additions: u64,
    pub deletions: u64,
    pub patch: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct RevertState {
    #[serde(rename = "messageID")]
    pub message_id: String,
    #[serde(default, rename = "partID")]
    pub part_id: Option<String>,
    #[serde(default)]
    pub snapshot: Option<String>,
    #[serde(default)]
    pub diff: Option<String>,
    #[serde(default)]
    pub files: Vec<FileDiff>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct RevertStageRequest {
    #[serde(rename = "messageID")]
    pub message_id: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub files: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum PermissionReply {
    Once,
    Always,
    Reject,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum PermissionEffect {
    Allow,
    Deny,
    Ask,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct PermissionCreated {
    pub id: String,
    pub effect: PermissionEffect,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct PermissionSource {
    #[serde(rename = "type")]
    pub source_type: String,
    #[serde(rename = "messageID")]
    pub message_id: String,
    #[serde(rename = "callID")]
    pub call_id: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
#[serde(rename_all = "camelCase")]
pub struct CreatePermissionRequest {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    pub action: String,
    #[serde(default)]
    pub resources: Vec<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub save: Vec<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub metadata: Option<Value>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub source: Option<PermissionSource>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub agent: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ReplyPermissionRequest {
    pub reply: PermissionReply,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub message: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct QuestionReplyRequest {
    pub answers: Vec<Vec<String>>,
}

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

pub struct SessionRevertApi<'a> {
    client: &'a super::Client,
    session_id: &'a str,
}

impl SessionRevertApi<'_> {
    pub async fn stage(&self, body: &RevertStageRequest) -> Result<RevertState, Error> {
        let response = self
            .client
            .inner
            .request_base(
                Method::POST,
                &format!("api/session/{}/revert/stage", self.session_id),
            )?
            .json(body)
            .send()
            .await?;
        let envelope: Envelope<RevertState> = self.client.inner.decode(response).await?;
        Ok(envelope.data)
    }

    /// Clear staged revert state: `POST .../revert/clear` on 1.x,
    /// `DELETE .../revert` on 2.x.
    pub async fn clear(&self) -> Result<(), Error> {
        let response = match self.client.serve_dialect().await? {
            ServeDialect::Preview1x => {
                self.client
                    .inner
                    .request_base(
                        Method::POST,
                        &format!("api/session/{}/revert/clear", self.session_id),
                    )?
                    .send()
                    .await?
            }
            ServeDialect::Native2x => {
                self.client
                    .inner
                    .request_base(
                        Method::DELETE,
                        &format!("api/session/{}/revert", self.session_id),
                    )?
                    .send()
                    .await?
            }
        };
        self.client.inner.ensure_success(response).await
    }

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

pub struct SessionPermissionApi<'a> {
    client: &'a super::Client,
    session_id: &'a str,
}

impl SessionPermissionApi<'_> {
    pub async fn list(&self) -> Result<Vec<PermissionRequest>, Error> {
        let response = self
            .client
            .inner
            .request_base(
                Method::GET,
                &format!("api/session/{}/permission", self.session_id),
            )?
            .send()
            .await?;
        let envelope: Envelope<Vec<PermissionRequest>> = self.client.inner.decode(response).await?;
        Ok(envelope.data)
    }

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

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

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

pub struct SessionQuestionApi<'a> {
    client: &'a super::Client,
    session_id: &'a str,
}

impl SessionQuestionApi<'_> {
    /// Pending questions (1.x `api/session/{id}/question`) or forms
    /// (2.x `api/session/{id}/form`); both decode into [`QuestionRequest`].
    pub async fn list(&self) -> Result<Vec<QuestionRequest>, Error> {
        let path = match self.client.serve_dialect().await? {
            ServeDialect::Preview1x => format!("api/session/{}/question", self.session_id),
            ServeDialect::Native2x => format!("api/session/{}/form", self.session_id),
        };
        let response = self
            .client
            .inner
            .request_base(Method::GET, &path)?
            .send()
            .await?;
        let envelope: Envelope<Vec<QuestionRequest>> = self.client.inner.decode(response).await?;
        Ok(envelope.data)
    }

    /// Answer with positional `answers` (OpenCode 1.x question contract).
    ///
    /// OpenCode 2.x forms are keyed by field key instead of position — use
    /// [`SessionQuestionApi::reply_answer`] there. Returns
    /// [`Error::Unsupported`] on OpenCode 2.x.
    pub async fn reply(&self, request_id: &str, body: &QuestionReplyRequest) -> Result<(), Error> {
        if self.client.serve_dialect().await? == ServeDialect::Native2x {
            return Err(Error::Unsupported(
                "positional question replies are 1.x only; use reply_answer() for 2.x forms",
            ));
        }
        let response = self
            .client
            .inner
            .request_base(
                Method::POST,
                &format!(
                    "api/session/{}/question/{request_id}/reply",
                    self.session_id
                ),
            )?
            .json(body)
            .send()
            .await?;
        self.client.inner.ensure_success(response).await
    }

    /// Submit a keyed form answer (OpenCode 2.x `api/session/{id}/form/{formID}/reply`).
    ///
    /// `answer` keys must match the form's field keys. Returns
    /// [`Error::Unsupported`] on OpenCode 1.x.
    pub async fn reply_answer(
        &self,
        form_id: &str,
        answer: &BTreeMap<String, Value>,
    ) -> Result<(), Error> {
        if self.client.serve_dialect().await? != ServeDialect::Native2x {
            return Err(Error::Unsupported(
                "keyed form replies require OpenCode 2.x; use reply() for 1.x questions",
            ));
        }
        let response = self
            .client
            .inner
            .request_base(
                Method::POST,
                &format!("api/session/{}/form/{form_id}/reply", self.session_id),
            )?
            .json(&serde_json::json!({ "answer": answer }))
            .send()
            .await?;
        self.client.inner.ensure_success(response).await
    }

    /// Reject/cancel a pending question (1.x) or form (2.x `DELETE .../form/{id}`).
    pub async fn reject(&self, request_id: &str) -> Result<(), Error> {
        let response = match self.client.serve_dialect().await? {
            ServeDialect::Preview1x => {
                self.client
                    .inner
                    .request_base(
                        Method::POST,
                        &format!(
                            "api/session/{}/question/{request_id}/reject",
                            self.session_id
                        ),
                    )?
                    .send()
                    .await?
            }
            ServeDialect::Native2x => {
                self.client
                    .inner
                    .request_base(
                        Method::DELETE,
                        &format!("api/session/{}/form/{request_id}", self.session_id),
                    )?
                    .send()
                    .await?
            }
        };
        self.client.inner.ensure_success(response).await
    }
}

impl SessionApi<'_> {
    pub async fn active(&self) -> Result<BTreeMap<String, SessionActive>, Error> {
        let response = self
            .client
            .inner
            .request_base(Method::GET, "api/session/active")?
            .send()
            .await?;
        let envelope: Envelope<BTreeMap<String, SessionActive>> =
            self.client.inner.decode(response).await?;
        Ok(envelope.data)
    }

    pub async fn switch_agent(&self, session_id: &str, agent: &str) -> Result<(), Error> {
        #[derive(Serialize)]
        struct Body<'a> {
            agent: &'a str,
        }

        let response = self
            .client
            .inner
            .request_base(Method::POST, &format!("api/session/{session_id}/agent"))?
            .json(&Body { agent })
            .send()
            .await?;
        self.client.inner.ensure_success(response).await
    }

    pub async fn switch_model(&self, session_id: &str, model: &ModelRef) -> Result<(), Error> {
        #[derive(Serialize)]
        struct Body<'a> {
            model: &'a ModelRef,
        }

        let response = self
            .client
            .inner
            .request_base(Method::POST, &format!("api/session/{session_id}/model"))?
            .json(&Body { model })
            .send()
            .await?;
        self.client.inner.ensure_success(response).await
    }

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

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

    /// Session history. OpenCode 2.x has no history route — returns
    /// [`Error::Unsupported`] there; use `messages()` for 2.x message history.
    pub async fn history(
        &self,
        session_id: &str,
        limit: Option<u64>,
        after: Option<u64>,
    ) -> Result<SessionHistory, Error> {
        if self.client.serve_dialect().await? == ServeDialect::Native2x {
            return Err(Error::Unsupported(
                "api/session/{id}/history is 1.x only; use messages() on 2.x",
            ));
        }
        let mut url = self
            .client
            .inner
            .url(&format!("api/session/{session_id}/history"))?;
        {
            let mut query = url.query_pairs_mut();
            if let Some(limit) = limit {
                query.append_pair("limit", &limit.to_string());
            }
            if let Some(after) = after {
                query.append_pair("after", &after.to_string());
            }
        }
        let response = self
            .client
            .inner
            .request_url(Method::GET, url)
            .send()
            .await?;
        self.client.inner.decode(response).await
    }

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

    pub async fn messages(
        &self,
        session_id: &str,
        options: &SessionMessagesOptions,
    ) -> Result<SessionMessagesPage, Error> {
        let mut url = self
            .client
            .inner
            .url(&format!("api/session/{session_id}/message"))?;
        {
            let mut query = url.query_pairs_mut();
            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(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 fn revert<'a>(&'a self, session_id: &'a str) -> SessionRevertApi<'a> {
        SessionRevertApi {
            client: self.client,
            session_id,
        }
    }

    pub fn permission<'a>(&'a self, session_id: &'a str) -> SessionPermissionApi<'a> {
        SessionPermissionApi {
            client: self.client,
            session_id,
        }
    }

    pub fn question<'a>(&'a self, session_id: &'a str) -> SessionQuestionApi<'a> {
        SessionQuestionApi {
            client: self.client,
            session_id,
        }
    }
}