Skip to main content

adk_cli/
console.rs

1//! Legacy console entry point.
2//!
3//! New code should use [`Launcher`](crate::Launcher) instead, which provides
4//! the same REPL with additional configuration options (memory, artifacts,
5//! streaming mode, etc.).
6
7use adk_core::Agent;
8use adk_runner::{Runner, RunnerConfig};
9use adk_session::{CreateRequest, InMemorySessionService, SessionService};
10use anyhow::Result;
11use futures::StreamExt;
12use rustyline::DefaultEditor;
13use std::collections::HashMap;
14use std::sync::Arc;
15
16use crate::launcher::StreamPrinter;
17
18/// Run an interactive console session with the given agent.
19///
20/// This is a convenience wrapper kept for backward compatibility with
21/// existing examples. Prefer [`Launcher`](crate::Launcher) for new code.
22pub async fn run_console(agent: Arc<dyn Agent>, app_name: String, user_id: String) -> Result<()> {
23    let session_service = Arc::new(InMemorySessionService::new());
24
25    let session = session_service
26        .create(CreateRequest {
27            app_name: app_name.clone(),
28            user_id: user_id.clone(),
29            session_id: None,
30            state: HashMap::new(),
31        })
32        .await?;
33
34    let runner = Runner::new(RunnerConfig {
35        app_name,
36        agent: agent.clone(),
37        session_service: session_service.clone(),
38        artifact_service: None,
39        memory_service: None,
40        plugin_manager: None,
41        run_config: None,
42        compaction_config: None,
43        context_cache_config: None,
44        cache_capable: None,
45        request_context: None,
46        cancellation_token: None,
47    })?;
48
49    let mut rl = DefaultEditor::new()?;
50
51    println!("ADK Console Mode");
52    println!("Agent: {}", agent.name());
53    println!("Type your message and press Enter. Ctrl+C to exit.\n");
54
55    loop {
56        let readline = rl.readline("User -> ");
57        match readline {
58            Ok(line) => {
59                if line.trim().is_empty() {
60                    continue;
61                }
62
63                rl.add_history_entry(&line)?;
64
65                let user_content = adk_core::Content::new("user").with_text(line);
66                print!("\nAgent -> ");
67
68                let session_id = session.id().to_string();
69                let mut events = runner.run(user_id.clone(), session_id, user_content).await?;
70                let mut printer = StreamPrinter::default();
71
72                while let Some(event) = events.next().await {
73                    match event {
74                        Ok(evt) => {
75                            if let Some(content) = &evt.llm_response.content {
76                                for part in &content.parts {
77                                    printer.handle_part(part);
78                                }
79                            }
80                        }
81                        Err(e) => {
82                            eprintln!("\nError: {e}");
83                        }
84                    }
85                }
86
87                printer.finish();
88                println!("\n");
89            }
90            Err(rustyline::error::ReadlineError::Interrupted) => {
91                println!("Interrupted");
92                break;
93            }
94            Err(rustyline::error::ReadlineError::Eof) => {
95                println!("EOF");
96                break;
97            }
98            Err(err) => {
99                eprintln!("Error: {err}");
100                break;
101            }
102        }
103    }
104
105    Ok(())
106}