feat: OpenResponses
This commit is contained in:
@@ -8,7 +8,7 @@ mod common;
|
||||
use common::MockLlmClient;
|
||||
use llm_worker::Worker;
|
||||
use llm_worker::llm_client::event::{Event, ResponseStatus, StatusEvent};
|
||||
use llm_worker::{Message, MessageContent};
|
||||
use llm_worker::Item;
|
||||
|
||||
// =============================================================================
|
||||
// Mutable State Tests
|
||||
@@ -39,12 +39,12 @@ fn test_mutable_history_manipulation() {
|
||||
assert!(worker.history().is_empty());
|
||||
|
||||
// Add to history
|
||||
worker.push_message(Message::user("Hello"));
|
||||
worker.push_message(Message::assistant("Hi there!"));
|
||||
worker.push_item(Item::user_message("Hello"));
|
||||
worker.push_item(Item::assistant_message("Hi there!"));
|
||||
assert_eq!(worker.history().len(), 2);
|
||||
|
||||
// Mutable access to history
|
||||
worker.history_mut().push(Message::user("How are you?"));
|
||||
worker.history_mut().push(Item::user_message("How are you?"));
|
||||
assert_eq!(worker.history().len(), 3);
|
||||
|
||||
// Clear history
|
||||
@@ -52,8 +52,8 @@ fn test_mutable_history_manipulation() {
|
||||
assert!(worker.history().is_empty());
|
||||
|
||||
// Set history
|
||||
let messages = vec![Message::user("Test"), Message::assistant("Response")];
|
||||
worker.set_history(messages);
|
||||
let items = vec![Item::user_message("Test"), Item::assistant_message("Response")];
|
||||
worker.set_history(items);
|
||||
assert_eq!(worker.history().len(), 2);
|
||||
}
|
||||
|
||||
@@ -63,29 +63,29 @@ fn test_mutable_builder_pattern() {
|
||||
let client = MockLlmClient::new(vec![]);
|
||||
let worker = Worker::new(client)
|
||||
.system_prompt("System prompt")
|
||||
.with_message(Message::user("Hello"))
|
||||
.with_message(Message::assistant("Hi!"))
|
||||
.with_messages(vec![
|
||||
Message::user("How are you?"),
|
||||
Message::assistant("I'm fine!"),
|
||||
.with_item(Item::user_message("Hello"))
|
||||
.with_item(Item::assistant_message("Hi!"))
|
||||
.with_items(vec![
|
||||
Item::user_message("How are you?"),
|
||||
Item::assistant_message("I'm fine!"),
|
||||
]);
|
||||
|
||||
assert_eq!(worker.get_system_prompt(), Some("System prompt"));
|
||||
assert_eq!(worker.history().len(), 4);
|
||||
}
|
||||
|
||||
/// Verify that multiple messages can be added with extend_history
|
||||
/// Verify that multiple items can be added with extend_history
|
||||
#[test]
|
||||
fn test_mutable_extend_history() {
|
||||
let client = MockLlmClient::new(vec![]);
|
||||
let mut worker = Worker::new(client);
|
||||
|
||||
worker.push_message(Message::user("First"));
|
||||
worker.push_item(Item::user_message("First"));
|
||||
|
||||
worker.extend_history(vec![
|
||||
Message::assistant("Response 1"),
|
||||
Message::user("Second"),
|
||||
Message::assistant("Response 2"),
|
||||
Item::assistant_message("Response 1"),
|
||||
Item::user_message("Second"),
|
||||
Item::assistant_message("Response 2"),
|
||||
]);
|
||||
|
||||
assert_eq!(worker.history().len(), 4);
|
||||
@@ -102,8 +102,8 @@ fn test_lock_transition() {
|
||||
let mut worker = Worker::new(client);
|
||||
|
||||
worker.set_system_prompt("System");
|
||||
worker.push_message(Message::user("Hello"));
|
||||
worker.push_message(Message::assistant("Hi"));
|
||||
worker.push_item(Item::user_message("Hello"));
|
||||
worker.push_item(Item::assistant_message("Hi"));
|
||||
|
||||
// Lock
|
||||
let locked_worker = worker.lock();
|
||||
@@ -120,14 +120,14 @@ fn test_unlock_transition() {
|
||||
let client = MockLlmClient::new(vec![]);
|
||||
let mut worker = Worker::new(client);
|
||||
|
||||
worker.push_message(Message::user("Hello"));
|
||||
worker.push_item(Item::user_message("Hello"));
|
||||
let locked_worker = worker.lock();
|
||||
|
||||
// Unlock
|
||||
let mut worker = locked_worker.unlock();
|
||||
|
||||
// History operations are available again in Mutable state
|
||||
worker.push_message(Message::assistant("Hi"));
|
||||
worker.push_item(Item::assistant_message("Hi"));
|
||||
worker.clear_history();
|
||||
assert!(worker.history().is_empty());
|
||||
}
|
||||
@@ -160,16 +160,10 @@ async fn test_mutable_run_updates_history() {
|
||||
assert_eq!(history.len(), 2); // user + assistant
|
||||
|
||||
// User message
|
||||
assert!(matches!(
|
||||
&history[0].content,
|
||||
MessageContent::Text(t) if t == "Hi there"
|
||||
));
|
||||
assert_eq!(history[0].as_text(), Some("Hi there"));
|
||||
|
||||
// Assistant message
|
||||
assert!(matches!(
|
||||
&history[1].content,
|
||||
MessageContent::Text(t) if t == "Hello, I'm an assistant!"
|
||||
));
|
||||
assert_eq!(history[1].as_text(), Some("Hello, I'm an assistant!"));
|
||||
}
|
||||
|
||||
/// Verify that history accumulates correctly over multiple turns in CacheLocked state
|
||||
@@ -201,7 +195,7 @@ async fn test_locked_multi_turn_history_accumulation() {
|
||||
|
||||
// Lock (after setting system prompt)
|
||||
let mut locked_worker = worker.lock();
|
||||
assert_eq!(locked_worker.locked_prefix_len(), 0); // No messages yet
|
||||
assert_eq!(locked_worker.locked_prefix_len(), 0); // No items yet
|
||||
|
||||
// Turn 1
|
||||
let result1 = locked_worker.run("Hello!").await;
|
||||
@@ -217,16 +211,16 @@ async fn test_locked_multi_turn_history_accumulation() {
|
||||
let history = locked_worker.history();
|
||||
|
||||
// Turn 1 user message
|
||||
assert!(matches!(&history[0].content, MessageContent::Text(t) if t == "Hello!"));
|
||||
assert_eq!(history[0].as_text(), Some("Hello!"));
|
||||
|
||||
// Turn 1 assistant message
|
||||
assert!(matches!(&history[1].content, MessageContent::Text(t) if t == "Nice to meet you!"));
|
||||
assert_eq!(history[1].as_text(), Some("Nice to meet you!"));
|
||||
|
||||
// Turn 2 user message
|
||||
assert!(matches!(&history[2].content, MessageContent::Text(t) if t == "Can you help me?"));
|
||||
assert_eq!(history[2].as_text(), Some("Can you help me?"));
|
||||
|
||||
// Turn 2 assistant message
|
||||
assert!(matches!(&history[3].content, MessageContent::Text(t) if t == "I can help with that."));
|
||||
assert_eq!(history[3].as_text(), Some("I can help with that."));
|
||||
}
|
||||
|
||||
/// Verify that locked_prefix_len correctly records history length at lock time
|
||||
@@ -253,15 +247,15 @@ async fn test_locked_prefix_len_tracking() {
|
||||
|
||||
let mut worker = Worker::new(client);
|
||||
|
||||
// Add messages beforehand
|
||||
worker.push_message(Message::user("Pre-existing message 1"));
|
||||
worker.push_message(Message::assistant("Pre-existing response 1"));
|
||||
// Add items beforehand
|
||||
worker.push_item(Item::user_message("Pre-existing message 1"));
|
||||
worker.push_item(Item::assistant_message("Pre-existing response 1"));
|
||||
|
||||
assert_eq!(worker.history().len(), 2);
|
||||
|
||||
// Lock
|
||||
let mut locked_worker = worker.lock();
|
||||
assert_eq!(locked_worker.locked_prefix_len(), 2); // 2 messages at lock time
|
||||
assert_eq!(locked_worker.locked_prefix_len(), 2); // 2 items at lock time
|
||||
|
||||
// Execute turn
|
||||
locked_worker.run("New message").await.unwrap();
|
||||
@@ -317,8 +311,8 @@ async fn test_unlock_edit_relock() {
|
||||
]]);
|
||||
|
||||
let worker = Worker::new(client)
|
||||
.with_message(Message::user("Hello"))
|
||||
.with_message(Message::assistant("Hi"));
|
||||
.with_item(Item::user_message("Hello"))
|
||||
.with_item(Item::assistant_message("Hi"));
|
||||
|
||||
// Lock -> Unlock
|
||||
let locked = worker.lock();
|
||||
@@ -328,7 +322,7 @@ async fn test_unlock_edit_relock() {
|
||||
|
||||
// Edit history
|
||||
unlocked.clear_history();
|
||||
unlocked.push_message(Message::user("Fresh start"));
|
||||
unlocked.push_item(Item::user_message("Fresh start"));
|
||||
|
||||
// Re-lock
|
||||
let relocked = unlocked.lock();
|
||||
|
||||
Reference in New Issue
Block a user