-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add exit tool for agent-initiated session exit #57
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
Merged
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| use anyhow::Result; | ||
| use async_trait::async_trait; | ||
| use std::collections::HashMap; | ||
| use std::sync::atomic::{AtomicBool, Ordering}; | ||
|
|
||
| use super::Tool; | ||
|
|
||
| /// A tool the agent can invoke to exit the REPL. | ||
| /// | ||
| /// When the user says something like "bye" or "quit", the LLM can call this | ||
| /// tool to signal a graceful shutdown. The caller checks [`ExitTool::triggered`] | ||
| /// after each engine run to decide whether to break the loop. | ||
| /// | ||
| /// Share via `Arc<ExitTool>` — the `AtomicBool` is stored inline; no inner | ||
| /// `Arc` is needed. | ||
| pub struct ExitTool { | ||
| flag: AtomicBool, | ||
| } | ||
|
|
||
| impl ExitTool { | ||
| pub fn new() -> Self { | ||
| Self { | ||
| flag: AtomicBool::new(false), | ||
| } | ||
| } | ||
|
|
||
| /// Returns `true` if the tool was invoked (the agent wants to exit). | ||
| pub fn triggered(&self) -> bool { | ||
| self.flag.load(Ordering::Acquire) | ||
| } | ||
|
|
||
| /// Reset the flag (useful if the caller decides not to exit after all). | ||
| pub fn reset(&self) { | ||
| self.flag.store(false, Ordering::Release); | ||
| } | ||
|
assapir marked this conversation as resolved.
|
||
| } | ||
|
|
||
| impl Default for ExitTool { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|
|
||
| #[async_trait] | ||
| impl Tool for ExitTool { | ||
| fn name(&self) -> &str { | ||
| "exit" | ||
| } | ||
|
|
||
| fn description(&self) -> &str { | ||
| "Exit the session. Call this when the user wants to quit, say goodbye, or end the conversation. No arguments required." | ||
| } | ||
|
|
||
| async fn execute(&self, _args: &HashMap<String, String>) -> Result<String> { | ||
| self.flag.store(true, Ordering::Release); | ||
| Ok("Goodbye! Exiting session.".to_string()) | ||
| } | ||
|
assapir marked this conversation as resolved.
|
||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn not_triggered_initially() { | ||
| let tool = ExitTool::new(); | ||
| assert!(!tool.triggered()); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn triggered_after_execute() { | ||
| let tool = ExitTool::new(); | ||
| let result = tool.execute(&HashMap::new()).await; | ||
| assert!(result.is_ok()); | ||
| assert!(tool.triggered()); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn reset_clears_flag() { | ||
| let tool = ExitTool::new(); | ||
| tool.execute(&HashMap::new()).await.unwrap(); | ||
| assert!(tool.triggered()); | ||
| tool.reset(); | ||
| assert!(!tool.triggered()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn name_is_exit() { | ||
| let tool = ExitTool::new(); | ||
| assert_eq!(Tool::name(&tool), "exit"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn description_mentions_quit() { | ||
| let tool = ExitTool::new(); | ||
| assert!(Tool::description(&tool).contains("quit")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn default_impl_works() { | ||
| let tool = ExitTool::default(); | ||
| assert!(!tool.triggered()); | ||
| } | ||
| } | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| pub mod exit; | ||
| pub mod shell; | ||
|
|
||
| use anyhow::Result; | ||
|
|
||
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.