use super::types::{ModelPreferences, SamplingContent, SamplingMessage}; use crate::mcp::{GenericMeta, IncludeContext, IntoMcpRequest, RequestMeta, Role};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use serde_with::skip_serializing_none;
#[skip_serializing_none]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateMessageParams {
#[serde(rename = "_meta")]
pub meta: Option<RequestMeta>,
pub messages: Vec<SamplingMessage>,
pub max_tokens: i64,
pub model_preferences: Option<ModelPreferences>,
pub system_prompt: Option<String>,
pub include_context: Option<IncludeContext>,
pub temperature: Option<f64>,
pub stop_sequences: Option<Vec<String>>,
pub metadata: Option<Value>,
}
impl CreateMessageParams {
pub fn new(messages: Vec<SamplingMessage>, max_tokens: i64) -> Self {
Self {
meta: None,
messages,
max_tokens,
model_preferences: None,
system_prompt: None,
include_context: None,
temperature: None,
stop_sequences: None,
metadata: None,
}
}
pub fn with_meta(mut self, meta: RequestMeta) -> Self {
self.meta = Some(meta);
self
}
pub fn with_model_preferences(mut self, preferences: ModelPreferences) -> Self {
self.model_preferences = Some(preferences);
self
}
pub fn with_system_prompt(mut self, prompt: impl Into<String>) -> Self {
self.system_prompt = Some(prompt.into());
self
}
pub fn with_include_context(mut self, context: IncludeContext) -> Self {
self.include_context = Some(context);
self
}
pub fn with_temperature(mut self, temp: f64) -> Self {
self.temperature = Some(temp);
self
}
pub fn with_stop_sequences(mut self, sequences: Vec<String>) -> Self {
self.stop_sequences = Some(sequences);
self
}
pub fn append_stop_sequence(mut self, sequence: impl Into<String>) -> Self {
self.stop_sequences.get_or_insert_with(Vec::new).push(sequence.into());
self
}
pub fn with_metadata(mut self, metadata: Value) -> Self {
self.metadata = Some(metadata);
self
}
}
impl IntoMcpRequest<CreateMessageParams> for CreateMessageParams {
const METHOD: &'static str = "sampling/createMessage";
type McpResult = CreateMessageResult;
}
#[skip_serializing_none]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateMessageResult {
#[serde(rename = "_meta")]
pub meta: Option<GenericMeta>,
pub role: Role,
pub content: SamplingContent,
pub model: String,
pub stop_reason: Option<String>,
}
impl CreateMessageResult {
pub fn new_assistant(content: impl Into<SamplingContent>, model: impl Into<String>) -> Self {
Self::new(Role::Assistant, content, model)
}
pub fn new(role: Role, content: impl Into<SamplingContent>, model: impl Into<String>) -> Self {
let content = content.into();
Self {
meta: None,
role,
content,
model: model.into(),
stop_reason: None,
}
}
pub fn with_meta(mut self, meta: GenericMeta) -> Self {
self.meta = Some(meta);
self
}
pub fn with_stop_reason(mut self, reason: impl Into<String>) -> Self {
self.stop_reason = Some(reason.into());
self
}
}