Skip to main content

vv_agent/sdk/session/run/
prompt.rs

1use std::collections::BTreeMap;
2
3use serde_json::Value;
4
5use crate::sdk::types::AgentRun;
6use crate::types::AgentStatus;
7
8use super::super::util::normalize_session_prompt;
9use super::super::AgentSession;
10
11impl AgentSession {
12    pub fn prompt(&mut self, prompt: impl Into<String>) -> Result<AgentRun, String> {
13        self.prompt_with_auto_follow_up(prompt, true)
14    }
15
16    pub fn prompt_with_auto_follow_up(
17        &mut self,
18        prompt: impl Into<String>,
19        auto_follow_up: bool,
20    ) -> Result<AgentRun, String> {
21        let mut run = self.run_once(normalize_session_prompt(prompt.into(), "prompt")?)?;
22        if !auto_follow_up {
23            return Ok(run);
24        }
25
26        while run.result.status == AgentStatus::Completed {
27            let follow_up_prompt = self
28                .follow_up_queue
29                .lock()
30                .expect("session follow-up queue lock")
31                .pop_front();
32            let Some(follow_up_prompt) = follow_up_prompt else {
33                break;
34            };
35            self.emit(
36                "session_follow_up_dequeued",
37                BTreeMap::from([(
38                    "prompt".to_string(),
39                    Value::String(follow_up_prompt.clone()),
40                )]),
41            );
42            run = self.run_once(follow_up_prompt)?;
43        }
44        Ok(run)
45    }
46}