Improve ergonomics of prompt and sampling message initialization (#120)
* Improve ergonomics of prompt and sampling message initialization * Deprecate previous / existing Message initializers
This commit is contained in:
parent
fac4df6b1a
commit
3b505ee575
1 changed files with 680 additions and 171 deletions
|
|
@ -29,10 +29,7 @@ struct PromptTests {
|
|||
|
||||
@Test("Prompt Message encoding and decoding")
|
||||
func testPromptMessageEncodingDecoding() throws {
|
||||
let textMessage = Prompt.Message(
|
||||
role: .user,
|
||||
content: .text(text: "Hello, world!")
|
||||
)
|
||||
let textMessage: Prompt.Message = .user("Hello, world!")
|
||||
|
||||
let encoder = JSONEncoder()
|
||||
let decoder = JSONDecoder()
|
||||
|
|
@ -134,9 +131,9 @@ struct PromptTests {
|
|||
|
||||
@Test("GetPrompt result validation")
|
||||
func testGetPromptResult() throws {
|
||||
let messages = [
|
||||
Prompt.Message(role: .user, content: .text(text: "User message")),
|
||||
Prompt.Message(role: .assistant, content: .text(text: "Assistant response")),
|
||||
let messages: [Prompt.Message] = [
|
||||
.user("User message"),
|
||||
.assistant("Assistant response"),
|
||||
]
|
||||
|
||||
let result = GetPrompt.Result(description: "Test description", messages: messages)
|
||||
|
|
@ -203,4 +200,246 @@ struct PromptTests {
|
|||
func testPromptListChangedNotification() throws {
|
||||
#expect(PromptListChangedNotification.name == "notifications/prompts/list_changed")
|
||||
}
|
||||
|
||||
@Test("Prompt Message factory methods")
|
||||
func testPromptMessageFactoryMethods() throws {
|
||||
// Test user message factory method
|
||||
let userMessage: Prompt.Message = .user("Hello, world!")
|
||||
#expect(userMessage.role == .user)
|
||||
if case .text(let text) = userMessage.content {
|
||||
#expect(text == "Hello, world!")
|
||||
} else {
|
||||
#expect(Bool(false), "Expected text content")
|
||||
}
|
||||
|
||||
// Test assistant message factory method
|
||||
let assistantMessage: Prompt.Message = .assistant("Hi there!")
|
||||
#expect(assistantMessage.role == .assistant)
|
||||
if case .text(let text) = assistantMessage.content {
|
||||
#expect(text == "Hi there!")
|
||||
} else {
|
||||
#expect(Bool(false), "Expected text content")
|
||||
}
|
||||
|
||||
// Test with image content
|
||||
let imageMessage: Prompt.Message = .user(.image(data: "base64data", mimeType: "image/png"))
|
||||
#expect(imageMessage.role == .user)
|
||||
if case .image(let data, let mimeType) = imageMessage.content {
|
||||
#expect(data == "base64data")
|
||||
#expect(mimeType == "image/png")
|
||||
} else {
|
||||
#expect(Bool(false), "Expected image content")
|
||||
}
|
||||
|
||||
// Test with audio content
|
||||
let audioMessage: Prompt.Message = .assistant(
|
||||
.audio(data: "base64audio", mimeType: "audio/wav"))
|
||||
#expect(audioMessage.role == .assistant)
|
||||
if case .audio(let data, let mimeType) = audioMessage.content {
|
||||
#expect(data == "base64audio")
|
||||
#expect(mimeType == "audio/wav")
|
||||
} else {
|
||||
#expect(Bool(false), "Expected audio content")
|
||||
}
|
||||
|
||||
// Test with resource content
|
||||
let resourceMessage: Prompt.Message = .user(
|
||||
.resource(
|
||||
uri: "file://test.txt", mimeType: "text/plain", text: "Sample text", blob: nil))
|
||||
#expect(resourceMessage.role == .user)
|
||||
if case .resource(let uri, let mimeType, let text, let blob) = resourceMessage.content {
|
||||
#expect(uri == "file://test.txt")
|
||||
#expect(mimeType == "text/plain")
|
||||
#expect(text == "Sample text")
|
||||
#expect(blob == nil)
|
||||
} else {
|
||||
#expect(Bool(false), "Expected resource content")
|
||||
}
|
||||
}
|
||||
|
||||
@Test("Prompt Content ExpressibleByStringLiteral")
|
||||
func testPromptContentExpressibleByStringLiteral() throws {
|
||||
// Test string literal assignment
|
||||
let content: Prompt.Message.Content = "Hello from string literal"
|
||||
|
||||
if case .text(let text) = content {
|
||||
#expect(text == "Hello from string literal")
|
||||
} else {
|
||||
#expect(Bool(false), "Expected text content")
|
||||
}
|
||||
|
||||
// Test in message creation
|
||||
let message: Prompt.Message = .user("Direct string literal")
|
||||
if case .text(let text) = message.content {
|
||||
#expect(text == "Direct string literal")
|
||||
} else {
|
||||
#expect(Bool(false), "Expected text content")
|
||||
}
|
||||
|
||||
// Test in array context
|
||||
let messages: [Prompt.Message] = [
|
||||
.user("First message"),
|
||||
.assistant("Second message"),
|
||||
.user("Third message"),
|
||||
]
|
||||
|
||||
#expect(messages.count == 3)
|
||||
#expect(messages[0].role == .user)
|
||||
#expect(messages[1].role == .assistant)
|
||||
#expect(messages[2].role == .user)
|
||||
}
|
||||
|
||||
@Test("Prompt Content ExpressibleByStringInterpolation")
|
||||
func testPromptContentExpressibleByStringInterpolation() throws {
|
||||
let userName = "Alice"
|
||||
let position = "Software Engineer"
|
||||
let company = "TechCorp"
|
||||
|
||||
// Test string interpolation
|
||||
let content: Prompt.Message.Content =
|
||||
"Hello \(userName), welcome to your \(position) interview at \(company)"
|
||||
|
||||
if case .text(let text) = content {
|
||||
#expect(text == "Hello Alice, welcome to your Software Engineer interview at TechCorp")
|
||||
} else {
|
||||
#expect(Bool(false), "Expected text content")
|
||||
}
|
||||
|
||||
// Test in message creation with interpolation
|
||||
let message: Prompt.Message = .user(
|
||||
"Hi \(userName), I'm excited about the \(position) role at \(company)")
|
||||
if case .text(let text) = message.content {
|
||||
#expect(text == "Hi Alice, I'm excited about the Software Engineer role at TechCorp")
|
||||
} else {
|
||||
#expect(Bool(false), "Expected text content")
|
||||
}
|
||||
|
||||
// Test complex interpolation
|
||||
let skills = ["Swift", "Python", "JavaScript"]
|
||||
let experience = 5
|
||||
let interviewMessage: Prompt.Message = .assistant(
|
||||
"I see you have \(experience) years of experience with \(skills.joined(separator: ", ")). That's impressive!"
|
||||
)
|
||||
|
||||
if case .text(let text) = interviewMessage.content {
|
||||
#expect(
|
||||
text
|
||||
== "I see you have 5 years of experience with Swift, Python, JavaScript. That's impressive!"
|
||||
)
|
||||
} else {
|
||||
#expect(Bool(false), "Expected text content")
|
||||
}
|
||||
}
|
||||
|
||||
@Test("Prompt Message factory methods with string interpolation")
|
||||
func testPromptMessageFactoryMethodsWithStringInterpolation() throws {
|
||||
let candidateName = "Bob"
|
||||
let position = "Data Scientist"
|
||||
let company = "DataCorp"
|
||||
let experience = 3
|
||||
|
||||
// Test user message with interpolation
|
||||
let userMessage: Prompt.Message = .user(
|
||||
"Hello, I'm \(candidateName) and I'm interviewing for the \(position) position")
|
||||
#expect(userMessage.role == .user)
|
||||
if case .text(let text) = userMessage.content {
|
||||
#expect(text == "Hello, I'm Bob and I'm interviewing for the Data Scientist position")
|
||||
} else {
|
||||
#expect(Bool(false), "Expected text content")
|
||||
}
|
||||
|
||||
// Test assistant message with interpolation
|
||||
let assistantMessage: Prompt.Message = .assistant(
|
||||
"Welcome \(candidateName)! Tell me about your \(experience) years of experience in data science"
|
||||
)
|
||||
#expect(assistantMessage.role == .assistant)
|
||||
if case .text(let text) = assistantMessage.content {
|
||||
#expect(text == "Welcome Bob! Tell me about your 3 years of experience in data science")
|
||||
} else {
|
||||
#expect(Bool(false), "Expected text content")
|
||||
}
|
||||
|
||||
// Test in conversation array
|
||||
let conversation: [Prompt.Message] = [
|
||||
.user("Hi, I'm \(candidateName) applying for \(position) at \(company)"),
|
||||
.assistant("Welcome \(candidateName)! How many years of experience do you have?"),
|
||||
.user("I have \(experience) years of experience in the field"),
|
||||
.assistant(
|
||||
"Great! \(experience) years is solid experience for a \(position) role at \(company)"
|
||||
),
|
||||
]
|
||||
|
||||
#expect(conversation.count == 4)
|
||||
|
||||
// Verify interpolated content
|
||||
if case .text(let text) = conversation[2].content {
|
||||
#expect(text == "I have 3 years of experience in the field")
|
||||
} else {
|
||||
#expect(Bool(false), "Expected text content")
|
||||
}
|
||||
}
|
||||
|
||||
@Test("Prompt ergonomic API usage patterns")
|
||||
func testPromptErgonomicAPIUsagePatterns() throws {
|
||||
// Test various ergonomic usage patterns enabled by the new API
|
||||
|
||||
// Pattern 1: Simple interview conversation
|
||||
let interviewConversation: [Prompt.Message] = [
|
||||
.user("Tell me about yourself"),
|
||||
.assistant("I'm a software engineer with 5 years of experience"),
|
||||
.user("What's your biggest strength?"),
|
||||
.assistant("I'm great at problem-solving and team collaboration"),
|
||||
]
|
||||
#expect(interviewConversation.count == 4)
|
||||
|
||||
// Pattern 2: Dynamic content with interpolation
|
||||
let candidateName = "Sarah"
|
||||
let role = "Product Manager"
|
||||
let yearsExp = 7
|
||||
|
||||
let dynamicConversation: [Prompt.Message] = [
|
||||
.user("Welcome \(candidateName) to the \(role) interview"),
|
||||
.assistant("Thank you! I'm excited about this \(role) opportunity"),
|
||||
.user("I see you have \(yearsExp) years of experience. Tell me about your background"),
|
||||
.assistant(
|
||||
"In my \(yearsExp) years as a \(role), I've led multiple successful product launches"
|
||||
),
|
||||
]
|
||||
#expect(dynamicConversation.count == 4)
|
||||
|
||||
// Pattern 3: Mixed content types
|
||||
let mixedContent: [Prompt.Message] = [
|
||||
.user("Please review this design mockup"),
|
||||
.assistant(.image(data: "design_mockup_data", mimeType: "image/png")),
|
||||
.user("What do you think of the user flow?"),
|
||||
.assistant(
|
||||
"The design looks clean and intuitive. I particularly like the navigation structure."
|
||||
),
|
||||
]
|
||||
#expect(mixedContent.count == 4)
|
||||
|
||||
// Verify content types
|
||||
if case .text = mixedContent[0].content,
|
||||
case .image = mixedContent[1].content,
|
||||
case .text = mixedContent[2].content,
|
||||
case .text = mixedContent[3].content
|
||||
{
|
||||
// All content types are correct
|
||||
} else {
|
||||
#expect(Bool(false), "Content types don't match expected pattern")
|
||||
}
|
||||
|
||||
// Pattern 4: Encoding/decoding still works
|
||||
let encoder = JSONEncoder()
|
||||
let decoder = JSONDecoder()
|
||||
|
||||
let data = try encoder.encode(interviewConversation)
|
||||
let decoded = try decoder.decode([Prompt.Message].self, from: data)
|
||||
|
||||
#expect(decoded.count == 4)
|
||||
#expect(decoded[0].role == .user)
|
||||
#expect(decoded[1].role == .assistant)
|
||||
#expect(decoded[2].role == .user)
|
||||
#expect(decoded[3].role == .assistant)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue