use anyhow::{Context, Result};
use polyc_rpc_client::{QuestionChoice, QuestionDialer, QuestionOutcome as QuestionRpcOutcome};
use crate::action::QuestionDecision;
use crate::components::questions::{QuestionOptionView, QuestionView};
pub(crate) async fn load_questions(
agent_addr: &str,
conversation_id: &str,
) -> Result<Vec<QuestionView>> {
let dialer = QuestionDialer::new(agent_addr)
.with_context(|| format!("invalid agent address {agent_addr:?}"))?;
let pending = dialer
.list_pending(conversation_id)
.await
.with_context(|| format!("QuestionService.ListPending for {conversation_id}"))?;
Ok(pending
.into_iter()
.map(|p| QuestionView {
conversation_id: conversation_id.to_owned(),
call_id: p.call_id,
index: p.index,
header: p.header,
question: p.question,
options: p
.options
.into_iter()
.map(|o| QuestionOptionView {
label: o.label,
description: o.description,
recommended: o.recommended,
})
.collect(),
args_json: p.args_json,
answer_token: p.answer_token,
turn_id: p.turn_id,
response: None,
})
.collect())
}
pub(crate) async fn submit_decision(
agent_addr: &str,
decision: &QuestionDecision,
) -> Result<QuestionRpcOutcome> {
let choice = decision
.selected_index
.map_or(QuestionChoice::Decline, QuestionChoice::SelectOption);
let outcome = QuestionDialer::new(agent_addr)
.with_context(|| format!("invalid agent address {agent_addr:?}"))?
.respond(
&decision.turn_id,
&decision.call_id,
decision.index,
choice,
&decision.conversation_id,
&decision.answer_token,
None,
)
.await
.with_context(|| format!("QuestionService.Respond at {agent_addr}"))?;
Ok(outcome)
}