use crate::domain::ir::content::{Content, Role};
#[derive(Debug, Clone, PartialEq)]
pub struct PromptMessage {
pub role: Role,
pub content: Content,
}
impl PromptMessage {
#[must_use]
pub const fn new(role: Role, content: Content) -> Self {
Self { role, content }
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct GetPromptResult {
pub description: Option<String>,
pub messages: Vec<PromptMessage>,
}
impl GetPromptResult {
#[must_use]
pub const fn new(messages: Vec<PromptMessage>) -> Self {
Self {
description: None,
messages,
}
}
#[must_use]
pub fn with_description(mut self, description: impl Into<String>) -> Self {
self.description = Some(description.into());
self
}
}
#[cfg(test)]
mod tests {
use super::PromptMessage;
use crate::domain::ir::content::{Content, TextContent};
use crate::domain::ir::{GetPromptResult, Role};
#[test]
fn a_result_carries_its_messages_and_no_description_by_default() -> Result<(), String> {
let result = GetPromptResult::new(vec![PromptMessage::new(
Role::User,
Content::Text(TextContent::new("hello")),
)]);
match result.description {
None => {}
other => return Err(format!("a result with no description carried {other:?}")),
}
if result.messages.len() != 1 {
return Err(format!("expected 1 message, got {}", result.messages.len()));
}
Ok(())
}
#[test]
fn a_description_is_kept_when_there_is_one() -> Result<(), String> {
let result = GetPromptResult::new(vec![PromptMessage::new(
Role::Assistant,
Content::Text(TextContent::new("hi")),
)])
.with_description("greets");
match result.description.as_deref() {
Some("greets") => Ok(()),
other => Err(format!("the description was {other:?}")),
}
}
#[test]
fn a_message_keeps_the_block_it_was_given() -> Result<(), String> {
let message = PromptMessage::new(
Role::User,
Content::Text(TextContent::new("an image, not a string")),
);
match (&message.role, &message.content) {
(Role::User, Content::Text(text)) if text.text == "an image, not a string" => Ok(()),
other => Err(format!("the message became {other:?}")),
}
}
}