Skip to content

Commit 3473881

Browse files
committed
move code around
1 parent 0e40303 commit 3473881

17 files changed

Lines changed: 164 additions & 26 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/goose-cli/src/cli.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,37 @@ enum SchedulerCommand {
592592
CronHelp {},
593593
}
594594

595+
#[derive(Subcommand)]
596+
enum GatewayCommand {
597+
#[command(about = "Show gateway status")]
598+
Status {},
599+
600+
#[command(about = "Start a gateway")]
601+
Start {
602+
#[arg(help = "Gateway type (e.g., 'telegram')")]
603+
gateway_type: String,
604+
605+
#[arg(
606+
long = "bot-token",
607+
help = "Bot token for the gateway platform",
608+
long_help = "Authentication token for the gateway platform (e.g., Telegram bot token)"
609+
)]
610+
bot_token: String,
611+
},
612+
613+
#[command(about = "Stop a running gateway")]
614+
Stop {
615+
#[arg(help = "Gateway type to stop (e.g., 'telegram')")]
616+
gateway_type: String,
617+
},
618+
619+
#[command(about = "Generate a pairing code for a gateway")]
620+
Pair {
621+
#[arg(help = "Gateway type to generate pairing code for")]
622+
gateway_type: String,
623+
},
624+
}
625+
595626
#[derive(Subcommand)]
596627
enum RecipeCommand {
597628
/// Validate a recipe file
@@ -785,6 +816,16 @@ enum Command {
785816
command: SchedulerCommand,
786817
},
787818

819+
/// Manage gateways for external platform integrations (e.g., Telegram)
820+
#[command(
821+
about = "Manage gateways for external platform integrations",
822+
visible_alias = "gw"
823+
)]
824+
Gateway {
825+
#[command(subcommand)]
826+
command: GatewayCommand,
827+
},
828+
788829
/// Update the goose CLI version
789830
#[command(about = "Update the goose CLI version")]
790831
Update {
@@ -959,6 +1000,7 @@ fn get_command_name(command: &Option<Command>) -> &'static str {
9591000
Some(Command::Project {}) => "project",
9601001
Some(Command::Projects) => "projects",
9611002
Some(Command::Run { .. }) => "run",
1003+
Some(Command::Gateway { .. }) => "gateway",
9621004
Some(Command::Schedule { .. }) => "schedule",
9631005
Some(Command::Update { .. }) => "update",
9641006
Some(Command::Recipe { .. }) => "recipe",
@@ -1350,6 +1392,23 @@ async fn handle_run_command(
13501392
}
13511393
}
13521394

1395+
async fn handle_gateway_command(command: GatewayCommand) -> Result<()> {
1396+
use crate::commands::gateway;
1397+
1398+
match command {
1399+
GatewayCommand::Status {} => gateway::handle_gateway_status().await,
1400+
GatewayCommand::Start {
1401+
gateway_type,
1402+
bot_token,
1403+
} => {
1404+
let platform_config = serde_json::json!({ "bot_token": bot_token });
1405+
gateway::handle_gateway_start(gateway_type, platform_config).await
1406+
}
1407+
GatewayCommand::Stop { gateway_type } => gateway::handle_gateway_stop(gateway_type).await,
1408+
GatewayCommand::Pair { gateway_type } => gateway::handle_gateway_pair(gateway_type).await,
1409+
}
1410+
}
1411+
13531412
async fn handle_schedule_command(command: SchedulerCommand) -> Result<()> {
13541413
match command {
13551414
SchedulerCommand::Add {
@@ -1513,6 +1572,7 @@ pub async fn cli() -> anyhow::Result<()> {
15131572
)
15141573
.await
15151574
}
1575+
Some(Command::Gateway { command }) => handle_gateway_command(command).await,
15161576
Some(Command::Schedule { command }) => handle_schedule_command(command).await,
15171577
Some(Command::Update {
15181578
canary,
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
use anyhow::Result;
2+
use goose::execution::manager::AgentManager;
3+
use goose::gateway::manager::GatewayManager;
4+
use std::sync::Arc;
5+
6+
pub async fn handle_gateway_status() -> Result<()> {
7+
let agent_manager = AgentManager::instance().await?;
8+
let gateway_manager = Arc::new(GatewayManager::new(agent_manager));
9+
let statuses = gateway_manager.status().await;
10+
11+
if statuses.is_empty() {
12+
println!("No gateways configured.");
13+
return Ok(());
14+
}
15+
16+
for status in statuses {
17+
let state = if status.running { "running" } else { "stopped" };
18+
println!(
19+
"{}: {} ({} paired users)",
20+
status.gateway_type,
21+
state,
22+
status.paired_users.len()
23+
);
24+
for user in &status.paired_users {
25+
println!(
26+
" - {}/{} (session: {})",
27+
user.platform,
28+
user.display_name.as_deref().unwrap_or(&user.user_id),
29+
user.session_id
30+
);
31+
}
32+
}
33+
34+
Ok(())
35+
}
36+
37+
pub async fn handle_gateway_start(
38+
gateway_type: String,
39+
platform_config: serde_json::Value,
40+
) -> Result<()> {
41+
let agent_manager = AgentManager::instance().await?;
42+
let gateway_manager = Arc::new(GatewayManager::new(agent_manager));
43+
44+
let mut config = goose::gateway::GatewayConfig {
45+
gateway_type,
46+
platform_config,
47+
max_sessions: 0,
48+
};
49+
50+
let gw = goose::gateway::create_gateway(&mut config)?;
51+
gateway_manager.start_gateway(config, gw).await?;
52+
53+
println!("Gateway started. Press Ctrl+C to stop.");
54+
55+
tokio::signal::ctrl_c().await?;
56+
gateway_manager.stop_all().await;
57+
58+
Ok(())
59+
}
60+
61+
pub async fn handle_gateway_stop(gateway_type: String) -> Result<()> {
62+
let agent_manager = AgentManager::instance().await?;
63+
let gateway_manager = Arc::new(GatewayManager::new(agent_manager));
64+
gateway_manager.stop_gateway(&gateway_type).await?;
65+
println!("Gateway '{}' stopped.", gateway_type);
66+
Ok(())
67+
}
68+
69+
pub async fn handle_gateway_pair(gateway_type: String) -> Result<()> {
70+
let agent_manager = AgentManager::instance().await?;
71+
let gateway_manager = Arc::new(GatewayManager::new(agent_manager));
72+
let (code, expires_at) = gateway_manager.generate_pairing_code(&gateway_type).await?;
73+
74+
let expires = chrono::DateTime::from_timestamp(expires_at, 0)
75+
.map(|dt| dt.format("%H:%M:%S").to_string())
76+
.unwrap_or_else(|| "unknown".to_string());
77+
78+
println!("Pairing code: {}", code);
79+
println!("Expires at: {}", expires);
80+
81+
Ok(())
82+
}

crates/goose-cli/src/commands/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod configure;
2+
pub mod gateway;
23
pub mod info;
34
pub mod project;
45
pub mod recipe;

crates/goose-server/Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@ rustls = { version = "0.23", features = ["ring"] }
5151
uuid = { workspace = true }
5252
once_cell = { workspace = true }
5353
dirs = { workspace = true }
54-
sqlx = { version = "0.8.6", features = ["runtime-tokio", "sqlite"] }
55-
async-trait.workspace = true
56-
pulldown-cmark = "0.13.0"
5754

5855
[target.'cfg(windows)'.dependencies]
5956
winreg = { version = "0.55.0" }

crates/goose-server/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
pub mod auth;
22
pub mod configuration;
33
pub mod error;
4-
pub mod gateway;
54
pub mod openapi;
65
pub mod routes;
76
pub mod state;

crates/goose-server/src/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
mod commands;
22
mod configuration;
33
mod error;
4-
mod gateway;
54
mod logging;
65
mod openapi;
76
mod routes;

crates/goose-server/src/routes/gateway.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use crate::gateway::manager::GatewayStatus;
2-
use crate::gateway::{self, GatewayConfig};
31
use crate::routes::errors::ErrorResponse;
42
use crate::state::AppState;
53
use axum::{
@@ -9,6 +7,8 @@ use axum::{
97
routing::{delete, get, post},
108
Json, Router,
119
};
10+
use goose::gateway::manager::GatewayStatus;
11+
use goose::gateway::GatewayConfig;
1212
use serde::{Deserialize, Serialize};
1313
use std::sync::Arc;
1414
use utoipa::ToSchema;
@@ -67,7 +67,7 @@ pub async fn start_gateway(
6767
max_sessions: request.max_sessions,
6868
};
6969

70-
let gw = match gateway::create_gateway(&mut config) {
70+
let gw = match goose::gateway::create_gateway(&mut config) {
7171
Ok(gw) => gw,
7272
Err(e) => return ErrorResponse::bad_request(e.to_string()).into_response(),
7373
};

crates/goose-server/src/state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ use std::sync::Arc;
1212
use tokio::sync::Mutex;
1313
use tokio::task::JoinHandle;
1414

15-
use crate::gateway::manager::GatewayManager;
1615
use crate::tunnel::TunnelManager;
1716
use goose::agents::ExtensionLoadResult;
17+
use goose::gateway::manager::GatewayManager;
1818

1919
type ExtensionLoadingTasks =
2020
Arc<Mutex<HashMap<String, Arc<Mutex<Option<JoinHandle<Vec<ExtensionLoadResult>>>>>>>>;

crates/goose/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ ignore = { workspace = true }
126126
which = { workspace = true }
127127
pctx_code_mode = "^0.2.3"
128128
unbinder = "0.1.7"
129+
pulldown-cmark = "0.13.0"
129130

130131
[target.'cfg(target_os = "windows")'.dependencies]
131132
winapi = { version = "0.3", features = ["wincred"] }

0 commit comments

Comments
 (0)