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
|
/// Send an interactive event for `command_id` and return the step the
|
||||||
/// plugin command produces. Interactive events are not expected to trigger
|
/// plugin command produces. Interactive events are not expected to trigger
|
||||||
/// nested host API calls, so this path does not supply a `HostApi`.
|
/// 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))),
|
other => Err(PluginError::UnexpectedResponse(Box::new(other))),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
self.send_request(HostRequest::InteractiveEvent { command_id, event })?;
|
self.call_simple(
|
||||||
let kind = "InteractiveEvent";
|
HostRequest::InteractiveEvent { command_id, event },
|
||||||
let timeout = request_timeout(kind);
|
"InteractiveEvent",
|
||||||
let deadline = Instant::now() + timeout;
|
"interactive event",
|
||||||
loop {
|
|resp| match resp {
|
||||||
match recv_with_deadline::<PluginToHost>(
|
HostResponse::CommandStep(s) => Ok(*s),
|
||||||
&self.stream,
|
other => Err(Box::new(other)),
|
||||||
&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)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -612,30 +634,15 @@ impl PluginProcess {
|
|||||||
other => Err(PluginError::UnexpectedResponse(Box::new(other))),
|
other => Err(PluginError::UnexpectedResponse(Box::new(other))),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
self.send_request(HostRequest::GetPrompt { command_id })?;
|
self.call_simple(
|
||||||
let kind = "GetPrompt";
|
HostRequest::GetPrompt { command_id },
|
||||||
let timeout = request_timeout(kind);
|
"GetPrompt",
|
||||||
let deadline = Instant::now() + timeout;
|
"get_prompt",
|
||||||
loop {
|
|resp| match resp {
|
||||||
match recv_with_deadline::<PluginToHost>(
|
HostResponse::Text(s) => Ok(s),
|
||||||
&self.stream,
|
other => Err(Box::new(other)),
|
||||||
&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)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -652,30 +659,15 @@ impl PluginProcess {
|
|||||||
other => Err(PluginError::UnexpectedResponse(Box::new(other))),
|
other => Err(PluginError::UnexpectedResponse(Box::new(other))),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
self.send_request(HostRequest::NeedsEntityPick { command_id })?;
|
self.call_simple(
|
||||||
let kind = "NeedsEntityPick";
|
HostRequest::NeedsEntityPick { command_id },
|
||||||
let timeout = request_timeout(kind);
|
"NeedsEntityPick",
|
||||||
let deadline = Instant::now() + timeout;
|
"needs_entity_pick",
|
||||||
loop {
|
|resp| match resp {
|
||||||
match recv_with_deadline::<PluginToHost>(
|
HostResponse::Bool(b) => Ok(b),
|
||||||
&self.stream,
|
other => Err(Box::new(other)),
|
||||||
&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)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user