-
Notifications
You must be signed in to change notification settings - Fork 617
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: interactive after run #1377
Merged
+51
−61
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,6 @@ use goose::agents::Agent; | |
use goose::message::{Message, MessageContent}; | ||
use mcp_core::handler::ToolError; | ||
use rand::{distributions::Alphanumeric, Rng}; | ||
use rustyline::Editor; | ||
use std::path::PathBuf; | ||
use tokio; | ||
|
||
|
@@ -104,8 +103,22 @@ impl Session { | |
Ok(()) | ||
} | ||
|
||
pub async fn start(&mut self) -> Result<()> { | ||
let mut editor = Editor::<(), rustyline::history::DefaultHistory>::new()?; | ||
/// Process a single message and get the response | ||
async fn process_message(&mut self, message: String) -> Result<()> { | ||
self.messages.push(Message::user().with_text(&message)); | ||
storage::persist_messages(&self.session_file, &self.messages)?; | ||
self.process_agent_response(false).await?; | ||
Ok(()) | ||
} | ||
|
||
/// Start an interactive session, optionally with an initial message | ||
pub async fn interactive(&mut self, message: Option<String>) -> Result<()> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
// Process initial message if provided | ||
if let Some(msg) = message { | ||
self.process_message(msg).await?; | ||
} | ||
|
||
let mut editor = rustyline::Editor::<(), rustyline::history::DefaultHistory>::new()?; | ||
|
||
// Load history from messages | ||
for msg in self | ||
|
@@ -129,7 +142,7 @@ impl Session { | |
storage::persist_messages(&self.session_file, &self.messages)?; | ||
|
||
output::show_thinking(); | ||
self.process_agent_response(&mut editor).await?; | ||
self.process_agent_response(true).await?; | ||
output::hide_thinking(); | ||
} | ||
input::InputResult::Exit => break, | ||
|
@@ -184,65 +197,39 @@ impl Session { | |
Ok(()) | ||
} | ||
|
||
pub async fn headless_start(&mut self, initial_message: String) -> Result<()> { | ||
self.messages | ||
.push(Message::user().with_text(&initial_message)); | ||
storage::persist_messages(&self.session_file, &self.messages)?; | ||
let mut editor = Editor::<(), rustyline::history::DefaultHistory>::new()?; | ||
self.process_agent_response(&mut editor).await?; | ||
Ok(()) | ||
/// Process a single message and exit | ||
pub async fn headless(&mut self, message: String) -> Result<()> { | ||
self.process_message(message).await | ||
} | ||
|
||
async fn process_agent_response( | ||
&mut self, | ||
editor: &mut Editor<(), rustyline::history::DefaultHistory>, | ||
) -> Result<()> { | ||
async fn process_agent_response(&mut self, interactive: bool) -> Result<()> { | ||
let mut stream = self.agent.reply(&self.messages).await?; | ||
|
||
use futures::StreamExt; | ||
loop { | ||
tokio::select! { | ||
result = stream.next() => { | ||
match result { | ||
Some(Ok(mut message)) => { | ||
|
||
// Handle tool confirmation requests before rendering | ||
Some(Ok(message)) => { | ||
// If it's a confirmation request, get approval but otherwise do not render/persist | ||
if let Some(MessageContent::ToolConfirmationRequest(confirmation)) = message.content.first() { | ||
output::hide_thinking(); | ||
|
||
// Format the confirmation prompt | ||
let prompt = "Goose would like to call the above tool. Allow? (y/n):".to_string(); | ||
|
||
let confirmation_request = Message::user().with_tool_confirmation_request( | ||
confirmation.id.clone(), | ||
confirmation.tool_name.clone(), | ||
confirmation.arguments.clone(), | ||
Some(prompt) | ||
); | ||
output::render_message(&confirmation_request); | ||
let prompt = "Goose would like to call the above tool, do you approve?".to_string(); | ||
|
||
// Get confirmation from user | ||
let confirmed = match input::get_input(editor)? { | ||
input::InputResult::Message(content) => { | ||
content.trim().to_lowercase().starts_with('y') | ||
} | ||
_ => false, | ||
}; | ||
|
||
let confirmed = cliclack::confirm(prompt).initial_value(true).interact()?; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice, this is much cleaner! |
||
self.agent.handle_confirmation(confirmation.id.clone(), confirmed).await; | ||
|
||
message = confirmation_request; | ||
} | ||
|
||
// Only push the message if it's not a tool confirmation request | ||
if !message.content.iter().any(|content| matches!(content, MessageContent::ToolConfirmationRequest(_))) { | ||
// otherwise we have a model/tool to render | ||
else { | ||
self.messages.push(message.clone()); | ||
storage::persist_messages(&self.session_file, &self.messages)?; | ||
if interactive {output::hide_thinking()}; | ||
output::render_message(&message); | ||
if interactive {output::show_thinking()}; | ||
} | ||
|
||
storage::persist_messages(&self.session_file, &self.messages)?; | ||
output::hide_thinking(); | ||
output::render_message(&message); | ||
output::show_thinking(); | ||
} | ||
Some(Err(e)) => { | ||
eprintln!("Error: {}", e); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-s
? why not-i
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-i was already taken