Skip to content

Commit

Permalink
context_servers: simplistic formatting of prompt messages
Browse files Browse the repository at this point in the history
Since slash commands don't fully support writing proper messages
with roles into the assistant panel just yet, we need to just write it
out in text for now. Most likely, we only receive user messages anywhere,
which we special case and insert directly without a role.
  • Loading branch information
dsp-ant committed Sep 13, 2024
1 parent 983aaf3 commit 6684f70
Showing 1 changed file with 52 additions and 2 deletions.
54 changes: 52 additions & 2 deletions crates/assistant/src/slash_command/context_server_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use collections::HashMap;
use context_servers::{
manager::{ContextServer, ContextServerManager},
protocol::PromptInfo,
types::{SamplingContent, SamplingMessage, SamplingRole},
};
use gpui::{Task, WeakView, WindowContext};
use language::{CodeLabel, LspAdapterDelegate};
Expand Down Expand Up @@ -127,8 +128,7 @@ impl SlashCommand for ContextServerSlashCommand {
return Err(anyhow!("Context server not initialized"));
};
let result = protocol.run_prompt(&prompt_name, prompt_args).await?;
let mut prompt = result.prompt;

let mut prompt = format_messages(&result.messages);
// We must normalize the line endings here, since servers might return CR characters.
LineEnding::normalize(&mut prompt);

Expand Down Expand Up @@ -204,3 +204,53 @@ pub fn acceptable_prompt(prompt: &PromptInfo) -> bool {
_ => false,
}
}

/// Formats a list of `SamplingMessage`s into a single string.
///
/// This function takes a slice of `SamplingMessage`s and converts them into a formatted string.
/// The formatting depends on the content of the messages:
///
/// - If all messages are from the User, it concatenates their text content, separated by newlines.
/// - Otherwise, it formats each message as "Role: Content", with roles being either "User" or "Assistant".
///
/// # Notes
///
/// - Image content is currently ignored for User-only messages and represented as "[Image]" for mixed messages.
/// - Empty strings are used for image content in User-only messages to maintain consistency in newline separation.
///
/// # TODO
/// We should properly render these messages with the respective User/Assistant labels that the assistant panel
/// supports, but Slash command do not yet support within that assistant panel.
fn format_messages(messages: &[SamplingMessage]) -> String {
if messages
.iter()
.all(|msg| matches!(msg.role, SamplingRole::User))
{
messages
.iter()
.map(|msg| {
match &msg.content {
SamplingContent::Text(text) => text,
SamplingContent::Image { .. } => "", // Ignore images for now
}
})
.collect::<Vec<&str>>()
.join("\n")
} else {
messages
.iter()
.map(|msg| {
let role = match msg.role {
SamplingRole::User => "User",
SamplingRole::Assistant => "Assistant",
};
let content = match &msg.content {
SamplingContent::Text(text) => text,
SamplingContent::Image { .. } => "",
};
format!("{}: {}", role, content)
})
.collect::<Vec<String>>()
.join("\n")
}
}

0 comments on commit 6684f70

Please sign in to comment.