Skip to content
35 changes: 33 additions & 2 deletions src/cortex-engine/src/session/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ use crate::rollout::recorder::SessionMeta;
use crate::summarization::SummarizationStrategy;

use super::Session;
use super::prompt::build_system_prompt;
use super::prompt::{
USE_SKILL_BASED_PROMPT, auto_detect_skills_from_message, build_system_prompt,
build_system_prompt_with_skills, inject_skills,
};

impl Session {
/// Handle an incoming submission.
Expand Down Expand Up @@ -122,7 +125,12 @@ impl Session {
// Update system prompt in existing message history
if let Some(msg) = self.messages.first_mut() {
if matches!(msg.role, crate::client::MessageRole::System) {
*msg = Message::system(build_system_prompt(&self.config));
let new_prompt = if USE_SKILL_BASED_PROMPT {
build_system_prompt_with_skills(&self.config, &[])
} else {
build_system_prompt(&self.config)
};
*msg = Message::system(new_prompt);
}
}
}
Expand Down Expand Up @@ -193,6 +201,29 @@ impl Session {

tracing::debug!("User message: {}", user_text);

// Auto-detect and inject skills based on the user's message (only on first message)
// This reduces context window usage by only loading relevant skills.
if USE_SKILL_BASED_PROMPT && self.turn_id == 1 {
let detected_skills = auto_detect_skills_from_message(&user_text);
if !detected_skills.is_empty() {
tracing::info!("Auto-detected skills for task: {:?}", detected_skills);

// Update the system prompt with detected skills
if let Some(msg) = self.messages.first_mut() {
if matches!(msg.role, MessageRole::System) {
if let Some(current_content) = msg.content.as_text() {
let updated_prompt = inject_skills(current_content, &detected_skills);
*msg = Message::system(updated_prompt);
tracing::debug!(
"Injected skills into system prompt: {:?}",
detected_skills
);
}
}
}
}
}

// Track messages for potential redo functionality
let _msg_start_idx = self.messages.len();

Expand Down
28 changes: 24 additions & 4 deletions src/cortex-engine/src/session/lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::rollout::{RolloutRecorder, SESSIONS_SUBDIR, get_rollout_path, read_ro
use crate::tools::ToolRouter;

use super::Session;
use super::prompt::build_system_prompt;
use super::prompt::{USE_SKILL_BASED_PROMPT, build_system_prompt, build_system_prompt_with_skills};
use super::types::{SessionHandle, SessionInfo, TokenCounter};

impl Session {
Expand Down Expand Up @@ -64,8 +64,16 @@ impl Session {
recorder.record_meta(&meta)?;

// Initialize with system prompt
// Use skill-based minimal prompt by default to reduce context window usage.
// Skills will be loaded on-demand based on task requirements.
let mut messages = Vec::new();
messages.push(Message::system(build_system_prompt(&config)));
let initial_prompt = if USE_SKILL_BASED_PROMPT {
// Start with the minimal base prompt - skills will be injected on first user message
build_system_prompt_with_skills(&config, &[])
} else {
build_system_prompt(&config)
};
messages.push(Message::system(initial_prompt));

// Initialize snapshot manager
let snapshot_dir = config
Expand Down Expand Up @@ -145,8 +153,15 @@ impl Session {
let mut tool_router = ToolRouter::new();

// Rebuild messages from events
// For resumed sessions, we still use the base prompt since skills might have been
// loaded during the previous session and we want consistency.
let mut messages = Vec::new();
messages.push(Message::system(build_system_prompt(&config)));
let initial_prompt = if USE_SKILL_BASED_PROMPT {
build_system_prompt_with_skills(&config, &[])
} else {
build_system_prompt(&config)
};
messages.push(Message::system(initial_prompt));

let events = get_events(&entries);
for event_msg in events {
Expand Down Expand Up @@ -257,7 +272,12 @@ impl Session {
let entries = read_rollout(&rollout_path)?;

let mut messages = Vec::new();
messages.push(Message::system(build_system_prompt(&config)));
let initial_prompt = if USE_SKILL_BASED_PROMPT {
build_system_prompt_with_skills(&config, &[])
} else {
build_system_prompt(&config)
};
messages.push(Message::system(initial_prompt));

let events = get_events(&entries);

Expand Down
5 changes: 4 additions & 1 deletion src/cortex-engine/src/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ use crate::rollout::RolloutRecorder;
use crate::tools::ToolRouter;

pub use lifecycle::list_sessions;
pub use prompt::build_system_prompt;
pub use prompt::{
USE_SKILL_BASED_PROMPT, auto_detect_skills_from_message, available_skills, build_system_prompt,
build_system_prompt_with_skills, inject_skills, is_valid_skill,
};
pub use types::{SessionHandle, SessionInfo, TokenCounter};

/// A running session that handles conversation with the model.
Expand Down
Loading