Skip to main content

muse_code_sdk/session/
user_input.rs

1//! User-input answers through a session.
2
3use crate::client::FacadeResult;
4use crate::errors::ProtocolError;
5use crate::msp::{UserInputAnswer, UserInputAnswerParams, UserInputAnswerResult};
6
7use super::Session;
8
9impl Session {
10    /// Answers a pending user-input question with `userInput/answer`.
11    ///
12    /// The result is the server's admission only; the question settles when
13    /// the server's `userInput/settled` event arrives. Pass `user_input_id`
14    /// exactly as the server sent it (the `userInputId` of a
15    /// `pending_user_inputs()` entry of [`Session::fold`]): the echoed id is
16    /// compared byte for byte.
17    pub async fn answer_user_input(
18        &self,
19        user_input_id: impl Into<String>,
20        answers: Vec<UserInputAnswer>,
21    ) -> FacadeResult<UserInputAnswerResult> {
22        self.inner.ensure_available()?;
23        let command_id = self.inner.client.mint_command_id()?;
24        let params = UserInputAnswerParams {
25            session_id: self.inner.session_id.clone(),
26            command_id,
27            user_input_id: user_input_id.into(),
28            answers,
29        };
30        let result = self
31            .inner
32            .client
33            .command::<_, UserInputAnswerResult>("userInput/answer", &params)
34            .await?;
35        if result.command_id != params.command_id || result.user_input_id != params.user_input_id {
36            return Err(ProtocolError::new(
37                format!(
38                    "userInput/answer echoed command {} for question {}, expected command {} for question {}",
39                    result.command_id, result.user_input_id, params.command_id, params.user_input_id
40                ),
41                None,
42            )
43            .into());
44        }
45        super::submit::require_accepted("userInput/answer", &result.status)?;
46        Ok(result)
47    }
48}