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, SessionId, UserId};
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
70                    .run(UserId::new(user_id.clone())?, SessionId::new(session_id)?, user_content)
71                    .await?;
72                let mut printer = StreamPrinter::default();
73
74                while let Some(event) = events.next().await {
75                    match event {
76                        Ok(evt) => {
77                            if let Some(content) = &evt.llm_response.content {
78                                for part in &content.parts {
79                                    printer.handle_part(part);
80                                }
81                            }
82                        }
83                        Err(e) => {
84                            eprintln!("\nError: {e}");
85                        }
86                    }
87                }
88
89                printer.finish();
90                println!("\n");
91            }
92            Err(rustyline::error::ReadlineError::Interrupted) => {
93                println!("Interrupted");
94                break;
95            }
96            Err(rustyline::error::ReadlineError::Eof) => {
97                println!("EOF");
98                break;
99            }
100            Err(err) => {
101                eprintln!("Error: {err}");
102                break;
103            }
104        }
105    }
106
107    Ok(())
108}