Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
270 changes: 147 additions & 123 deletions crates/jcode-app-core/src/server/client_lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ fn initial_subscribe_working_dir(request: &Request) -> std::result::Result<Strin
}
}

fn initial_subscribe_terminal_env(request: &Request) -> Vec<(String, String)> {
match request {
Request::Subscribe { terminal_env, .. } => terminal_env.clone(),
_ => Vec::new(),
}
}

struct ProcessingMessage {
id: u64,
content: String,
Expand Down Expand Up @@ -468,6 +475,7 @@ pub(super) async fn handle_client(
return Ok(());
}
};
let mut active_terminal_env = initial_subscribe_terminal_env(&initial_request);

// Per-client state
let mut client_is_processing = false;
Expand All @@ -493,11 +501,15 @@ pub(super) async fn handle_client(

// Create a new session for this client
let t0 = std::time::Instant::now();
let mut new_agent = Agent::new_with_initial_working_dir(
Arc::clone(&provider),
registry.clone(),
Some(&initial_working_dir),
);
let mut new_agent =
crate::hooks::with_client_terminal_env(active_terminal_env.clone(), async {
Agent::new_with_initial_working_dir(
Arc::clone(&provider),
registry.clone(),
Some(&initial_working_dir),
)
})
.await;
let agent_new_ms = t0.elapsed().as_millis();

new_agent.set_memory_enabled(crate::config::config().features.memory);
Expand Down Expand Up @@ -1132,6 +1144,7 @@ pub(super) async fn handle_client(
&agent,
&client_event_tx,
&processing_done_tx,
active_terminal_env.clone(),
&SwarmStatusRefs {
members: &swarm_members,
swarms_by_id: &swarms_by_id,
Expand Down Expand Up @@ -1209,28 +1222,31 @@ pub(super) async fn handle_client(
) {
continue;
}
handle_clear_session(
id,
client_selfdev,
&mut client_session_id,
&client_connection_id,
&agent,
&provider,
&registry,
&sessions,
&shutdown_signals,
&soft_interrupt_queues,
&client_connections,
&swarm_members,
&swarms_by_id,
&file_touch,
&channel_subscriptions,
&channel_subscriptions_by_session,
&swarm_plans,
&event_history,
&event_counter,
&swarm_event_tx,
&client_event_tx,
crate::hooks::with_client_terminal_env(
active_terminal_env.clone(),
handle_clear_session(
id,
client_selfdev,
&mut client_session_id,
&client_connection_id,
&agent,
&provider,
&registry,
&sessions,
&shutdown_signals,
&soft_interrupt_queues,
&client_connections,
&swarm_members,
&swarms_by_id,
&file_touch,
&channel_subscriptions,
&channel_subscriptions_by_session,
&swarm_plans,
&event_history,
&event_counter,
&swarm_event_tx,
&client_event_tx,
),
)
.await;
session_control = refresh_session_control_handle(
Expand Down Expand Up @@ -1418,43 +1434,47 @@ pub(super) async fn handle_client(
}
}
}
active_terminal_env = terminal_env.clone();
if let Some(target_session_id) = target_session_id {
if crate::session::session_exists(&target_session_id) {
let pre_resume_session_id = client_session_id.clone();
agent = handle_resume_session(
id,
target_session_id.clone(),
subscribe_working_dir.as_deref(),
client_instance_id.as_deref(),
client_has_local_history,
allow_session_takeover,
&mut client_selfdev,
&mut client_session_id,
&client_connection_id,
&agent,
&provider,
&registry,
&sessions,
&shutdown_signals,
&soft_interrupt_queues,
&client_connections,
&client_debug_state,
&swarm_members,
&swarms_by_id,
&file_touch,
&channel_subscriptions,
&channel_subscriptions_by_session,
&swarm_plans,
&swarm_coordinators,
&client_count,
&writer,
&server_name,
&server_icon,
&client_event_tx,
&mcp_pool,
&event_history,
&event_counter,
&swarm_event_tx,
agent = crate::hooks::with_client_terminal_env(
active_terminal_env.clone(),
handle_resume_session(
id,
target_session_id.clone(),
subscribe_working_dir.as_deref(),
client_instance_id.as_deref(),
client_has_local_history,
allow_session_takeover,
&mut client_selfdev,
&mut client_session_id,
&client_connection_id,
&agent,
&provider,
&registry,
&sessions,
&shutdown_signals,
&soft_interrupt_queues,
&client_connections,
&client_debug_state,
&swarm_members,
&swarms_by_id,
&file_touch,
&channel_subscriptions,
&channel_subscriptions_by_session,
&swarm_plans,
&swarm_coordinators,
&client_count,
&writer,
&server_name,
&server_icon,
&client_event_tx,
&mcp_pool,
&event_history,
&event_counter,
&swarm_event_tx,
),
)
.await?;
session_control = refresh_session_control_handle(
Expand Down Expand Up @@ -1659,40 +1679,43 @@ pub(super) async fn handle_client(
info.client_instance_id = client_instance_id.clone();
}
}
agent = handle_resume_session(
id,
session_id,
resume_working_dir.as_deref(),
client_instance_id.as_deref(),
client_has_local_history,
allow_session_takeover,
&mut client_selfdev,
&mut client_session_id,
&client_connection_id,
&agent,
&provider,
&registry,
&sessions,
&shutdown_signals,
&soft_interrupt_queues,
&client_connections,
&client_debug_state,
&swarm_members,
&swarms_by_id,
&file_touch,
&channel_subscriptions,
&channel_subscriptions_by_session,
&swarm_plans,
&swarm_coordinators,
&client_count,
&writer,
&server_name,
&server_icon,
&client_event_tx,
&mcp_pool,
&event_history,
&event_counter,
&swarm_event_tx,
agent = crate::hooks::with_client_terminal_env(
active_terminal_env.clone(),
handle_resume_session(
id,
session_id,
resume_working_dir.as_deref(),
client_instance_id.as_deref(),
client_has_local_history,
allow_session_takeover,
&mut client_selfdev,
&mut client_session_id,
&client_connection_id,
&agent,
&provider,
&registry,
&sessions,
&shutdown_signals,
&soft_interrupt_queues,
&client_connections,
&client_debug_state,
&swarm_members,
&swarms_by_id,
&file_touch,
&channel_subscriptions,
&channel_subscriptions_by_session,
&swarm_plans,
&swarm_coordinators,
&client_count,
&writer,
&server_name,
&server_icon,
&client_event_tx,
&mcp_pool,
&event_history,
&event_counter,
&swarm_event_tx,
),
)
.await?;
session_control = refresh_session_control_handle(
Expand Down Expand Up @@ -2753,28 +2776,31 @@ pub(super) async fn handle_client(
}
}

cleanup_client_connection(
&sessions,
&client_session_id,
client_is_processing,
&mut processing_task,
event_handle,
&swarm_members,
&swarms_by_id,
&swarm_coordinators,
&swarm_plans,
&file_touch,
&channel_subscriptions,
&channel_subscriptions_by_session,
&client_debug_state,
&client_debug_id,
&client_connections,
&client_connection_id,
&shutdown_signals,
&soft_interrupt_queues,
&event_history,
&event_counter,
&swarm_event_tx,
crate::hooks::with_client_terminal_env(
active_terminal_env,
cleanup_client_connection(
&sessions,
&client_session_id,
client_is_processing,
&mut processing_task,
event_handle,
&swarm_members,
&swarms_by_id,
&swarm_coordinators,
&swarm_plans,
&file_touch,
&channel_subscriptions,
&channel_subscriptions_by_session,
&client_debug_state,
&client_debug_id,
&client_connections,
&client_connection_id,
&shutdown_signals,
&soft_interrupt_queues,
&event_history,
&event_counter,
&swarm_event_tx,
),
)
.await?;
Ok(())
Expand Down Expand Up @@ -2818,6 +2844,7 @@ async fn start_processing_message(
agent: &Arc<Mutex<Agent>>,
client_event_tx: &mpsc::UnboundedSender<ServerEvent>,
processing_done_tx: &mpsc::UnboundedSender<(u64, Result<()>, Option<String>)>,
client_terminal_env: Vec<(String, String)>,
swarm: &SwarmStatusRefs<'_>,
) {
let ProcessingMessage {
Expand Down Expand Up @@ -2888,12 +2915,9 @@ async fn start_processing_message(
crate::logging::info(&format!("Processing message id={} spawning task", id));
*state.task = Some(tokio::spawn(async move {
let event_tx = tx.clone();
let result = match std::panic::AssertUnwindSafe(process_message_streaming_mpsc(
agent,
&content,
images,
system_reminder,
event_tx,
let result = match std::panic::AssertUnwindSafe(crate::hooks::with_client_terminal_env(
client_terminal_env,
process_message_streaming_mpsc(agent, &content, images, system_reminder, event_tx),
))
.catch_unwind()
.await
Expand Down
13 changes: 7 additions & 6 deletions crates/jcode-base/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
pub use jcode_config_types::{
AgentsConfig, AmbientConfig, AuthConfig, AutoJudgeConfig, AutoReviewConfig, CompactionConfig,
CompactionMode, CrossProviderFailoverMode, DiagramDisplayMode, DiagramPanePosition,
DiffDisplayMode, DisplayConfig, FeatureConfig, GatewayConfig, HooksConfig, KeybindingsConfig,
LatexRenderingMode, LaunchHotkeyEntry, LaunchHotkeysConfig, MarkdownSpacingMode,
NamedProviderAuth, NamedProviderConfig, NamedProviderModelConfig, NamedProviderType,
NativeScrollbarConfig, NotificationsConfig, OverscrollStatusMode, PowerConfig, ProviderConfig,
ReasoningDisplayMode, SafetyConfig, SessionPickerResumeAction, SponsorsConfig, SwarmSpawnMode,
SwarmStripLayout, TerminalConfig, UpdateChannel, WebSearchConfig, WebSearchEngine,
DiffDisplayMode, DisplayConfig, FeatureConfig, GatewayConfig, HookCommands, HooksConfig,
KeybindingsConfig, LatexRenderingMode, LaunchHotkeyEntry, LaunchHotkeysConfig,
MarkdownSpacingMode, NamedProviderAuth, NamedProviderConfig, NamedProviderModelConfig,
NamedProviderType, NativeScrollbarConfig, NotificationsConfig, OverscrollStatusMode,
PowerConfig, ProviderConfig, ReasoningDisplayMode, SafetyConfig, SessionPickerResumeAction,
SponsorsConfig, SwarmSpawnMode, SwarmStripLayout, TerminalConfig, UpdateChannel,
WebSearchConfig, WebSearchEngine,
};
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, BTreeSet, HashSet};
Expand Down
14 changes: 12 additions & 2 deletions crates/jcode-base/src/config/env_overrides.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,13 +431,23 @@ impl Config {
}

// Lifecycle hooks. Empty env values disable config-file hooks.
fn hook_env_override(slot: &mut Option<String>, key: &str) {
fn hook_env_override(slot: &mut Option<HookCommands>, key: &str) {
if let Ok(v) = std::env::var(key) {
let trimmed = v.trim();
*slot = if trimmed.is_empty() {
None
} else if trimmed.starts_with('[') {
#[derive(serde::Deserialize)]
struct HookOverride {
commands: HookCommands,
}

toml::from_str::<HookOverride>(&format!("commands = {trimmed}"))
.map(|parsed| parsed.commands)
.ok()
.or_else(|| Some(HookCommands::one(trimmed)))
} else {
Some(trimmed.to_string())
Some(HookCommands::one(trimmed))
};
}
}
Expand Down
Loading
Loading