Skip to main content

opencode_sdk_rs/resources/
tui.rs

1//! Tui resource types and methods mirroring the JS SDK's `resources/tui.ts`.
2
3use serde::{Deserialize, Serialize};
4
5use crate::{client::Opencode, error::OpencodeError};
6
7// ---------------------------------------------------------------------------
8// Type aliases
9// ---------------------------------------------------------------------------
10
11/// Response type for [`TuiResource::append_prompt`].
12pub type TuiAppendPromptResponse = bool;
13
14/// Response type for [`TuiResource::open_help`].
15pub type TuiOpenHelpResponse = bool;
16
17// ---------------------------------------------------------------------------
18// Params
19// ---------------------------------------------------------------------------
20
21/// Parameters for [`TuiResource::append_prompt`].
22#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
23pub struct TuiAppendPromptParams {
24    /// The text to append.
25    pub text: String,
26}
27
28// ---------------------------------------------------------------------------
29// Resource
30// ---------------------------------------------------------------------------
31
32/// Provides access to `/tui/*` endpoints.
33pub struct TuiResource<'a> {
34    client: &'a Opencode,
35}
36
37impl<'a> TuiResource<'a> {
38    pub(crate) const fn new(client: &'a Opencode) -> Self {
39        Self { client }
40    }
41
42    /// Append text to the TUI prompt.
43    ///
44    /// `POST /tui/append-prompt`
45    pub async fn append_prompt(
46        &self,
47        params: &TuiAppendPromptParams,
48    ) -> Result<TuiAppendPromptResponse, OpencodeError> {
49        self.client.post("/tui/append-prompt", Some(params), None).await
50    }
51
52    /// Open the TUI help panel.
53    ///
54    /// `POST /tui/open-help`
55    pub async fn open_help(&self) -> Result<TuiOpenHelpResponse, OpencodeError> {
56        self.client.post::<TuiOpenHelpResponse, ()>("/tui/open-help", None, None).await
57    }
58}
59
60// ---------------------------------------------------------------------------
61// Tests
62// ---------------------------------------------------------------------------
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67
68    #[test]
69    fn tui_append_prompt_params_round_trip() {
70        let params = TuiAppendPromptParams { text: "hello world".into() };
71        let json_str = serde_json::to_string(&params).unwrap();
72        assert!(json_str.contains(r#""text":"hello world"#));
73        let back: TuiAppendPromptParams = serde_json::from_str(&json_str).unwrap();
74        assert_eq!(params, back);
75    }
76}