fix: echo protocol run inputs
This commit is contained in:
@@ -1167,6 +1167,17 @@ mod ws_tests {
|
||||
WorkerExecutionRunState::Idle,
|
||||
)
|
||||
}
|
||||
|
||||
fn dispatch_method(
|
||||
&self,
|
||||
_handle: &WorkerExecutionHandle,
|
||||
_method: protocol::Method,
|
||||
) -> WorkerExecutionResult {
|
||||
WorkerExecutionResult::accepted(
|
||||
WorkerExecutionOperation::ProtocolMethod,
|
||||
WorkerExecutionRunState::Idle,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn ws_test_bundle(profile: ProfileSelector) -> ConfigBundle {
|
||||
@@ -1328,6 +1339,34 @@ mod ws_tests {
|
||||
));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn protocol_ws_echoes_accepted_run_as_user_message() {
|
||||
let (_runtime, _worker_ref, url) = spawn_runtime_server().await;
|
||||
let (mut stream, _) = connect_async(&url).await.unwrap();
|
||||
let _ = next_frame(&mut stream).await;
|
||||
|
||||
stream
|
||||
.send(Message::Text(
|
||||
serde_json::to_string(&protocol::Method::Run {
|
||||
input: vec![protocol::Segment::text("hello from protocol")],
|
||||
})
|
||||
.unwrap()
|
||||
.into(),
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
match next_frame(&mut stream).await {
|
||||
protocol::Event::UserMessage { segments } => {
|
||||
assert_eq!(
|
||||
protocol::Segment::flatten_to_text(&segments),
|
||||
"hello from protocol"
|
||||
);
|
||||
}
|
||||
event => panic!("expected user message echo, got {event:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn protocol_ws_reports_malformed_cursor_and_method_frame() {
|
||||
let (_runtime, _worker_ref, url) = spawn_runtime_server().await;
|
||||
|
||||
@@ -576,6 +576,8 @@ impl Runtime {
|
||||
return Ok(vec![Event::Completions { kind, entries }]);
|
||||
}
|
||||
|
||||
let observation_payload = protocol_method_observation_event(&method);
|
||||
|
||||
let (backend, handle) = {
|
||||
let mut state = self.lock()?;
|
||||
state.ensure_running()?;
|
||||
@@ -622,6 +624,9 @@ impl Runtime {
|
||||
}
|
||||
|
||||
self.record_execution_result(worker_ref, dispatch_result)?;
|
||||
if let Some(payload) = observation_payload {
|
||||
self.record_protocol_method_observation(worker_ref, payload)?;
|
||||
}
|
||||
Ok(Vec::new())
|
||||
}
|
||||
|
||||
@@ -958,12 +963,7 @@ impl Runtime {
|
||||
worker_ref: &WorkerRef,
|
||||
input: WorkerInput,
|
||||
) -> Result<(), RuntimeError> {
|
||||
let mut state = self.lock()?;
|
||||
state.ensure_worker_ref(worker_ref)?;
|
||||
let event =
|
||||
state.push_worker_observation_event(worker_ref.clone(), input_protocol_event(&input));
|
||||
state.persist_worker_observation_event(&event)?;
|
||||
Ok(())
|
||||
self.record_protocol_method_observation(worker_ref, input_protocol_event(&input))
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "ws-server"))]
|
||||
@@ -975,6 +975,28 @@ impl Runtime {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(feature = "ws-server")]
|
||||
fn record_protocol_method_observation(
|
||||
&self,
|
||||
worker_ref: &WorkerRef,
|
||||
payload: Event,
|
||||
) -> Result<(), RuntimeError> {
|
||||
let mut state = self.lock()?;
|
||||
state.ensure_worker_ref(worker_ref)?;
|
||||
let event = state.push_worker_observation_event(worker_ref.clone(), payload);
|
||||
state.persist_worker_observation_event(&event)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "ws-server"))]
|
||||
fn record_protocol_method_observation(
|
||||
&self,
|
||||
_worker_ref: &WorkerRef,
|
||||
_payload: Event,
|
||||
) -> Result<(), RuntimeError> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn transition_worker(
|
||||
&self,
|
||||
worker_ref: &WorkerRef,
|
||||
@@ -1836,6 +1858,48 @@ fn validate_worker_input(input: &WorkerInput) -> Result<(), RuntimeError> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(feature = "ws-server")]
|
||||
fn protocol_method_observation_event(method: &Method) -> Option<Event> {
|
||||
match method {
|
||||
Method::Run { input } => Some(Event::UserMessage {
|
||||
segments: input.clone(),
|
||||
}),
|
||||
Method::Notify { message, .. } => Some(Event::SystemItem {
|
||||
item: serde_json::json!({
|
||||
"kind": "embedded_worker_system_input",
|
||||
"content": message,
|
||||
}),
|
||||
}),
|
||||
Method::RegisterPeer { name } => Some(Event::SystemItem {
|
||||
item: serde_json::json!({
|
||||
"kind": "embedded_worker_command_input",
|
||||
"command": "register_peer",
|
||||
"content": name,
|
||||
}),
|
||||
}),
|
||||
Method::Compact => Some(Event::SystemItem {
|
||||
item: serde_json::json!({
|
||||
"kind": "embedded_worker_command_input",
|
||||
"command": "compact",
|
||||
"content": "",
|
||||
}),
|
||||
}),
|
||||
Method::ListRewindTargets => Some(Event::SystemItem {
|
||||
item: serde_json::json!({
|
||||
"kind": "embedded_worker_command_input",
|
||||
"command": "list_rewind_targets",
|
||||
"content": "",
|
||||
}),
|
||||
}),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "ws-server"))]
|
||||
fn protocol_method_observation_event(_method: &Method) -> Option<Event> {
|
||||
None
|
||||
}
|
||||
|
||||
#[cfg(feature = "ws-server")]
|
||||
fn input_protocol_event(input: &WorkerInput) -> protocol::Event {
|
||||
match input.kind {
|
||||
|
||||
Reference in New Issue
Block a user