Skip to content

Commit cc7110b

Browse files
Copilotyiwang
andcommitted
Add tool factory support to Telegram bot for dangerous tools
- Add ToolFactory type to telegram module (same pattern as HeartbeatRunner) - Modify run_telegram_bot() to accept optional ToolFactory parameter - Store tool_factory in BotState - Extend agent with CLI tools when factory is provided in handle_chat() - Update daemon to inject create_cli_tools factory when starting Telegram bot - This allows Telegram agent to use bash, read_file, write_file, edit_file tools Co-authored-by: yiwang <142937+yiwang@users.noreply.github.com>
1 parent 350210d commit cc7110b

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

crates/cli/src/cli/daemon.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,13 @@ async fn run_daemon_services(config: &Config, agent_id: &str) -> Result<()> {
196196
let tg_gate = turn_gate.clone();
197197
println!(" Telegram: enabled");
198198
Some(tokio::spawn(async move {
199-
if let Err(e) = localgpt_server::telegram::run_telegram_bot(&tg_config, tg_gate).await {
199+
// Create tool factory that provides CLI tools to Telegram
200+
let tool_factory: localgpt_server::telegram::ToolFactory =
201+
Box::new(|config: &localgpt_core::config::Config| {
202+
crate::tools::create_cli_tools(config)
203+
});
204+
205+
if let Err(e) = localgpt_server::telegram::run_telegram_bot(&tg_config, tg_gate, Some(tool_factory)).await {
200206
tracing::error!("Telegram bot error: {}", e);
201207
}
202208
}))

crates/server/src/telegram.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use teloxide::types::{MessageId, ParseMode};
1414
use tokio::sync::Mutex;
1515
use tracing::{debug, error, info, warn};
1616

17-
use localgpt_core::agent::{Agent, AgentConfig, StreamEvent, extract_tool_detail};
17+
use localgpt_core::agent::{Agent, AgentConfig, StreamEvent, extract_tool_detail, tools::Tool};
1818
use localgpt_core::concurrency::TurnGate;
1919
use localgpt_core::config::Config;
2020
use localgpt_core::memory::MemoryManager;
@@ -28,6 +28,10 @@ const MAX_MESSAGE_LENGTH: usize = 4096;
2828
/// Debounce interval for message edits (seconds)
2929
const EDIT_DEBOUNCE_SECS: u64 = 2;
3030

31+
/// Factory function type for creating additional tools for the Telegram agent.
32+
/// This allows the caller (e.g., CLI daemon) to inject dangerous tools like bash, file I/O.
33+
pub type ToolFactory = Box<dyn Fn(&Config) -> Result<Vec<Box<dyn Tool>>> + Send + Sync>;
34+
3135
#[derive(Debug, Serialize, Deserialize)]
3236
struct PairedUser {
3337
user_id: u64,
@@ -47,6 +51,7 @@ struct BotState {
4751
turn_gate: TurnGate,
4852
paired_user: Mutex<Option<PairedUser>>,
4953
pending_pairing_code: Mutex<Option<String>>,
54+
tool_factory: Option<ToolFactory>,
5055
}
5156

5257
fn pairing_file_path() -> Result<PathBuf> {
@@ -78,7 +83,7 @@ fn generate_pairing_code() -> String {
7883
format!("{:06}", code)
7984
}
8085

81-
pub async fn run_telegram_bot(config: &Config, turn_gate: TurnGate) -> Result<()> {
86+
pub async fn run_telegram_bot(config: &Config, turn_gate: TurnGate, tool_factory: Option<ToolFactory>) -> Result<()> {
8287
let telegram_config = config
8388
.telegram
8489
.as_ref()
@@ -116,6 +121,7 @@ pub async fn run_telegram_bot(config: &Config, turn_gate: TurnGate) -> Result<()
116121
turn_gate,
117122
paired_user: Mutex::new(paired_user),
118123
pending_pairing_code: Mutex::new(None),
124+
tool_factory,
119125
});
120126

121127
// Register bot commands so Telegram clients show the "/" menu
@@ -667,6 +673,18 @@ async fn handle_chat(
667673

668674
match Agent::new(agent_config, &state.config, state.memory.clone()).await {
669675
Ok(mut agent) => {
676+
// Extend agent with additional tools from factory if provided (e.g., CLI tools from daemon)
677+
if let Some(ref factory) = state.tool_factory {
678+
match factory(&state.config) {
679+
Ok(extra_tools) => {
680+
agent.extend_tools(extra_tools);
681+
}
682+
Err(err) => {
683+
error!("Failed to create additional tools: {}", err);
684+
}
685+
}
686+
}
687+
670688
if let Err(err) = agent.new_session().await {
671689
error!("Failed to create session: {}", err);
672690
let _ = bot

0 commit comments

Comments
 (0)