forked from 1b-agent/opencadstudio-upstream
refactor(plugin): share legacy round-trip helper across interactive calls
Collapse the triplicated send/timeout/deadline/nested-request loop in interactive_event, get_prompt and needs_entity_pick into PluginProcess::call_simple. Timeout floors, error strings and UnexpectedResponse behavior preserved exactly. Net: +64 / -72 in one file, no behavior change. Verified: cargo check clean, cargo test -p ocs_plugin_api --lib --features host (92 passed), clippy shows only pre-existing warnings.
This commit is contained in:
1 parent
1bc20b9ecc
commit
7ea51d81d3
1 file changed
+64
-72
@@ -553,6 +553,43 @@ impl PluginProcess {
|
||||
}
|
||||
}
|
||||
|
||||
/// Legacy (non-V4) round trip for simple interactive calls: send `req`,
|
||||
/// then loop until the matching response arrives, answering any nested
|
||||
/// plugin request with an error. `kind` drives the timeout floor;
|
||||
/// `nested_what` names the operation in that error; `extract` pulls the
|
||||
/// expected payload out of the response.
|
||||
fn call_simple<T>(
|
||||
&self,
|
||||
req: HostRequest,
|
||||
kind: &'static str,
|
||||
nested_what: &'static str,
|
||||
extract: impl Fn(HostResponse) -> Result<T, Box<HostResponse>>,
|
||||
) -> Result<T, PluginError> {
|
||||
self.send_request(req)?;
|
||||
let timeout = request_timeout(kind);
|
||||
let deadline = Instant::now() + timeout;
|
||||
loop {
|
||||
match recv_with_deadline::<PluginToHost>(
|
||||
&self.stream,
|
||||
&self.child,
|
||||
deadline,
|
||||
timeout,
|
||||
kind,
|
||||
)? {
|
||||
PluginToHost::Response(resp) => match extract(resp) {
|
||||
Ok(v) => return Ok(v),
|
||||
Err(other) => return Err(PluginError::UnexpectedResponse(other)),
|
||||
},
|
||||
PluginToHost::Request(req) => {
|
||||
let resp = crate::ipc::protocol::PluginResponse::Error(format!(
|
||||
"unexpected nested request during {nested_what}: {req:?}"
|
||||
));
|
||||
self.send_response(resp)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Send an interactive event for `command_id` and return the step the
|
||||
/// plugin command produces. Interactive events are not expected to trigger
|
||||
/// nested host API calls, so this path does not supply a `HostApi`.
|
||||
@@ -572,30 +609,15 @@ impl PluginProcess {
|
||||
other => Err(PluginError::UnexpectedResponse(Box::new(other))),
|
||||
}
|
||||
} else {
|
||||
self.send_request(HostRequest::InteractiveEvent { command_id, event })?;
|
||||
let kind = "InteractiveEvent";
|
||||
let timeout = request_timeout(kind);
|
||||
let deadline = Instant::now() + timeout;
|
||||
loop {
|
||||
match recv_with_deadline::<PluginToHost>(
|
||||
&self.stream,
|
||||
&self.child,
|
||||
deadline,
|
||||
timeout,
|
||||
kind,
|
||||
)? {
|
||||
PluginToHost::Response(HostResponse::CommandStep(s)) => return Ok(*s),
|
||||
PluginToHost::Response(other) => {
|
||||
return Err(PluginError::UnexpectedResponse(Box::new(other)))
|
||||
}
|
||||
PluginToHost::Request(req) => {
|
||||
let resp = crate::ipc::protocol::PluginResponse::Error(format!(
|
||||
"unexpected nested request during interactive event: {req:?}"
|
||||
));
|
||||
self.send_response(resp)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
self.call_simple(
|
||||
HostRequest::InteractiveEvent { command_id, event },
|
||||
"InteractiveEvent",
|
||||
"interactive event",
|
||||
|resp| match resp {
|
||||
HostResponse::CommandStep(s) => Ok(*s),
|
||||
other => Err(Box::new(other)),
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -612,30 +634,15 @@ impl PluginProcess {
|
||||
other => Err(PluginError::UnexpectedResponse(Box::new(other))),
|
||||
}
|
||||
} else {
|
||||
self.send_request(HostRequest::GetPrompt { command_id })?;
|
||||
let kind = "GetPrompt";
|
||||
let timeout = request_timeout(kind);
|
||||
let deadline = Instant::now() + timeout;
|
||||
loop {
|
||||
match recv_with_deadline::<PluginToHost>(
|
||||
&self.stream,
|
||||
&self.child,
|
||||
deadline,
|
||||
timeout,
|
||||
kind,
|
||||
)? {
|
||||
PluginToHost::Response(HostResponse::Text(s)) => return Ok(s),
|
||||
PluginToHost::Response(other) => {
|
||||
return Err(PluginError::UnexpectedResponse(Box::new(other)))
|
||||
}
|
||||
PluginToHost::Request(req) => {
|
||||
let resp = crate::ipc::protocol::PluginResponse::Error(format!(
|
||||
"unexpected nested request during get_prompt: {req:?}"
|
||||
));
|
||||
self.send_response(resp)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
self.call_simple(
|
||||
HostRequest::GetPrompt { command_id },
|
||||
"GetPrompt",
|
||||
"get_prompt",
|
||||
|resp| match resp {
|
||||
HostResponse::Text(s) => Ok(s),
|
||||
other => Err(Box::new(other)),
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -652,30 +659,15 @@ impl PluginProcess {
|
||||
other => Err(PluginError::UnexpectedResponse(Box::new(other))),
|
||||
}
|
||||
} else {
|
||||
self.send_request(HostRequest::NeedsEntityPick { command_id })?;
|
||||
let kind = "NeedsEntityPick";
|
||||
let timeout = request_timeout(kind);
|
||||
let deadline = Instant::now() + timeout;
|
||||
loop {
|
||||
match recv_with_deadline::<PluginToHost>(
|
||||
&self.stream,
|
||||
&self.child,
|
||||
deadline,
|
||||
timeout,
|
||||
kind,
|
||||
)? {
|
||||
PluginToHost::Response(HostResponse::Bool(b)) => return Ok(b),
|
||||
PluginToHost::Response(other) => {
|
||||
return Err(PluginError::UnexpectedResponse(Box::new(other)))
|
||||
}
|
||||
PluginToHost::Request(req) => {
|
||||
let resp = crate::ipc::protocol::PluginResponse::Error(format!(
|
||||
"unexpected nested request during needs_entity_pick: {req:?}"
|
||||
));
|
||||
self.send_response(resp)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
self.call_simple(
|
||||
HostRequest::NeedsEntityPick { command_id },
|
||||
"NeedsEntityPick",
|
||||
"needs_entity_pick",
|
||||
|resp| match resp {
|
||||
HostResponse::Bool(b) => Ok(b),
|
||||
other => Err(Box::new(other)),
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user