rho-coding-agent 3.13.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
use std::num::NonZeroUsize;

use pretty_assertions::assert_eq;
use rho_sdk::{
    tool::{tool_progress_channel, ToolContext, ToolErrorKind, ToolInvocation},
    CancellationToken, ToolCallId,
};
use serde_json::json;

use super::{questionnaire_bundle, SelfAsks};
use crate::{app::self_questions::SelfQuestions, tools::sdk_registry::ToolBundle};

// Covers: in a session that reports to its starter, `questionnaire` hands a
// valid form to the session's host and returns at once, with no host input
// to wait on, while a form that does not parse is refused and asks nothing.
// A miss blocks a worker's turn on a person, or opens a form nobody can
// answer.
// Owner: the questionnaire tool's ask without waiting.
#[tokio::test]
async fn a_bound_questionnaire_asks_its_host_and_returns_at_once() {
    let form = json!({"questions": [{"id": "color", "question": "Which color?", "choices": ["Red", "Blue"]}]});
    let cases = [
        (form.clone(), Ok(()), vec![form]),
        (
            json!({"questions": []}),
            Err(ToolErrorKind::InvalidArguments),
            vec![],
        ),
    ];
    for (arguments, expected, expected_asked) in cases {
        let name = arguments.to_string();
        let questions = SelfQuestions::new();
        let mut asked = questions.take_asked().unwrap();
        let asks = SelfAsks::default();
        asks.bind(questions);
        let bundle = questionnaire_bundle(crate::config::QuestionnaireExpiry::default(), asks);
        let tool = &bundle.tools()[0];
        // No host input: a call that waited on one would fail.
        let (progress, _receiver) = tool_progress_channel(NonZeroUsize::MIN);
        let context = ToolContext::new(None, CancellationToken::new(), progress);

        let result = tool
            .call(ToolInvocation::new(ToolCallId::new(), arguments), context)
            .await
            .map(|_| ())
            .map_err(|error| error.kind());
        let mut taken = Vec::new();
        while let Ok(question) = asked.try_recv() {
            taken.push(question.form.arguments);
        }

        assert_eq!((result, taken), (expected, expected_asked), "{name}");
    }
}