Skip to content

Commit

Permalink
Fix to not send engine commands when only importing functions or cons…
Browse files Browse the repository at this point in the history
…tants (#5371)

* Change to not send engine commands when in isolated mode

* Update output since not sending engine commands
  • Loading branch information
jtran authored Feb 13, 2025
1 parent 1f405b9 commit a572d7b
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 323 deletions.
33 changes: 32 additions & 1 deletion src/wasm-lib/kcl/src/engine/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ use kcmc::{
},
ModelingCmd,
};
use kittycad_modeling_cmds::{self as kcmc, id::ModelingCmdId, websocket::ModelingBatch};
use kittycad_modeling_cmds::{
self as kcmc, id::ModelingCmdId, ok_response::OkModelingCmdResponse, websocket::ModelingBatch,
};
use tokio::sync::{mpsc, oneshot, RwLock};
use tokio_tungstenite::tungstenite::Message as WsMsg;
use uuid::Uuid;
Expand Down Expand Up @@ -459,6 +461,35 @@ impl EngineManager for EngineConnection {
_ => {}
}

// In isolated mode, we don't send the command to the engine.
if self.execution_kind().is_isolated() {
return match &cmd {
WebSocketRequest::ModelingCmdBatchReq(ModelingBatch { requests, .. }) => {
let mut responses = HashMap::with_capacity(requests.len());
for request in requests {
responses.insert(
request.cmd_id,
BatchResponse::Success {
response: OkModelingCmdResponse::Empty {},
},
);
}
Ok(WebSocketResponse::Success(SuccessWebSocketResponse {
request_id: Some(id),
resp: OkWebSocketResponseData::ModelingBatch { responses },
success: true,
}))
}
_ => Ok(WebSocketResponse::Success(SuccessWebSocketResponse {
request_id: Some(id),
resp: OkWebSocketResponseData::Modeling {
modeling_response: OkModelingCmdResponse::Empty {},
},
success: true,
})),
};
}

let (tx, rx) = oneshot::channel();

// Send the request to the engine, via the actor.
Expand Down
2 changes: 1 addition & 1 deletion src/wasm-lib/kcl/src/engine/conn_mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl crate::engine::EngineManager for EngineConnection {
responses: _,
}) => {
// Create the empty responses.
let mut responses = HashMap::new();
let mut responses = HashMap::with_capacity(requests.len());
for request in requests {
self.handle_command(&request.cmd, request.cmd_id, &id_to_source_range)?;
responses.insert(
Expand Down
35 changes: 34 additions & 1 deletion src/wasm-lib/kcl/src/engine/conn_wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ use anyhow::Result;
use indexmap::IndexMap;
use kcmc::{
id::ModelingCmdId,
websocket::{ModelingBatch, WebSocketRequest, WebSocketResponse},
ok_response::OkModelingCmdResponse,
websocket::{
BatchResponse, ModelingBatch, OkWebSocketResponseData, SuccessWebSocketResponse, WebSocketRequest,
WebSocketResponse,
},
ModelingCmd,
};
use kittycad_modeling_cmds as kcmc;
Expand Down Expand Up @@ -222,6 +226,35 @@ impl crate::engine::EngineManager for EngineConnection {
_ => {}
}

// In isolated mode, we don't send the command to the engine.
if self.execution_kind().is_isolated() {
return match &cmd {
WebSocketRequest::ModelingCmdBatchReq(ModelingBatch { requests, .. }) => {
let mut responses = HashMap::with_capacity(requests.len());
for request in requests {
responses.insert(
request.cmd_id,
BatchResponse::Success {
response: OkModelingCmdResponse::Empty {},
},
);
}
Ok(WebSocketResponse::Success(SuccessWebSocketResponse {
request_id: Some(id),
resp: OkWebSocketResponseData::ModelingBatch { responses },
success: true,
}))
}
_ => Ok(WebSocketResponse::Success(SuccessWebSocketResponse {
request_id: Some(id),
resp: OkWebSocketResponseData::Modeling {
modeling_response: OkModelingCmdResponse::Empty {},
},
success: true,
})),
};
}

let source_range_str = serde_json::to_string(&source_range).map_err(|e| {
KclError::Engine(KclErrorDetails {
message: format!("Failed to serialize source range: {:?}", e),
Expand Down
2 changes: 1 addition & 1 deletion src/wasm-lib/kcl/src/execution/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ pub struct PreImportedGeometry {
}

pub async fn send_to_engine(pre: PreImportedGeometry, ctxt: &ExecutorContext) -> Result<ImportedGeometry, KclError> {
if ctxt.is_mock() {
if ctxt.no_engine_commands() {
return Ok(ImportedGeometry {
id: pre.id,
value: pre.command.files.iter().map(|f| f.path.to_string()).collect(),
Expand Down
5 changes: 5 additions & 0 deletions src/wasm-lib/kcl/src/execution/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,11 @@ impl ExecutorContext {
self.context_type == ContextType::Mock || self.context_type == ContextType::MockCustomForwarded
}

/// Returns true if we should not send engine commands for any reason.
pub fn no_engine_commands(&self) -> bool {
self.is_mock() || self.engine.execution_kind().is_isolated()
}

pub async fn send_clear_scene(
&self,
exec_state: &mut ExecState,
Expand Down
4 changes: 2 additions & 2 deletions src/wasm-lib/kcl/src/std/extrude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ pub(crate) async fn do_post_extrude(
Some(extrude_surface)
}
}
} else if args.ctx.is_mock() {
} else if args.ctx.no_engine_commands() {
// Only pre-populate the extrude surface if we are in mock mode.

let extrude_surface = ExtrudeSurface::ExtrudePlane(crate::execution::ExtrudePlane {
Expand Down Expand Up @@ -317,7 +317,7 @@ fn analyze_faces(exec_state: &mut ExecState, args: &Args, face_infos: Vec<Extrus
sides: HashMap::with_capacity(face_infos.len()),
..Default::default()
};
if args.ctx.is_mock() {
if args.ctx.no_engine_commands() {
// Create fake IDs for start and end caps, to make extrudes mock-execute safe
faces.start_cap_id = Some(exec_state.next_uuid());
faces.end_cap_id = Some(exec_state.next_uuid());
Expand Down
6 changes: 3 additions & 3 deletions src/wasm-lib/kcl/src/std/fillet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ pub async fn get_opposite_edge(exec_state: &mut ExecState, args: Args) -> Result
name = "getOppositeEdge",
}]
async fn inner_get_opposite_edge(tag: TagIdentifier, exec_state: &mut ExecState, args: Args) -> Result<Uuid, KclError> {
if args.ctx.is_mock() {
if args.ctx.no_engine_commands() {
return Ok(exec_state.next_uuid());
}
let face_id = args.get_adjacent_face_to_tag(exec_state, &tag, false).await?;
Expand Down Expand Up @@ -299,7 +299,7 @@ async fn inner_get_next_adjacent_edge(
exec_state: &mut ExecState,
args: Args,
) -> Result<Uuid, KclError> {
if args.ctx.is_mock() {
if args.ctx.no_engine_commands() {
return Ok(exec_state.next_uuid());
}
let face_id = args.get_adjacent_face_to_tag(exec_state, &tag, false).await?;
Expand Down Expand Up @@ -384,7 +384,7 @@ async fn inner_get_previous_adjacent_edge(
exec_state: &mut ExecState,
args: Args,
) -> Result<Uuid, KclError> {
if args.ctx.is_mock() {
if args.ctx.no_engine_commands() {
return Ok(exec_state.next_uuid());
}
let face_id = args.get_adjacent_face_to_tag(exec_state, &tag, false).await?;
Expand Down
2 changes: 1 addition & 1 deletion src/wasm-lib/kcl/src/std/helix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ async fn inner_helix(
meta: vec![args.source_range.into()],
});

if args.ctx.is_mock() {
if args.ctx.no_engine_commands() {
return Ok(helix_result);
}

Expand Down
2 changes: 1 addition & 1 deletion src/wasm-lib/kcl/src/std/mirror.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ async fn inner_mirror_2d(
SketchSet::Sketches(sketches) => sketches,
};

if args.ctx.is_mock() {
if args.ctx.no_engine_commands() {
return Ok(starting_sketches);
}

Expand Down
Loading

0 comments on commit a572d7b

Please sign in to comment.