kcl-lib 0.2.185

KittyCAD Language implementation and tools
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//! Types used to send data to the test server.

use std::path::PathBuf;

use kittycad_modeling_cmds::websocket::RawFile;

use crate::ConnectionError;
use crate::ExecError;
use crate::KclError;
use crate::KclErrorWithOutputs;
use crate::Program;
use crate::engine::new_zoo_client;
use crate::errors::ExecErrorWithState;
use crate::execution::EnvironmentRef;
use crate::execution::ExecState;
use crate::execution::ExecutorContext;
use crate::execution::ExecutorSettings;

#[derive(serde::Deserialize, serde::Serialize)]
pub struct RequestBody {
    pub kcl_program: String,
    #[serde(default)]
    pub test_name: String,
}

/// Executes a KCL program. Only returns success or error.
pub async fn execute(code: &str, current_file: Option<PathBuf>) -> Result<(), ExecError> {
    let ctx = new_context_engine_graphics(true, current_file).await?;
    let program = Program::parse_no_errs(code).map_err(KclErrorWithOutputs::no_outputs)?;
    let res = do_execute(&ctx, program, None)
        .await
        .map(|_| ())
        .map_err(|err| err.error);
    ctx.close().await;
    res
}

#[cfg(test)]
pub struct Snapshot3d {
    /// Bytes of the snapshot.
    pub image: image::DynamicImage,
    /// Glb binary containing mesh and brep data
    pub glb: Glb,
}

/// Execute the kcl and ask the engine to render an image
/// 2d kcl files can't be exported for local render
/// Fails if geometry_only = true
pub async fn execute_locally_and_render_on_engine(
    ctx: &ExecutorContext,
    program: Program,
    deprecation_version_override: Option<&str>,
) -> Result<(ExecState, EnvironmentRef, image::DynamicImage), ExecErrorWithState> {
    let (exec_state, env_ref) = do_execute(ctx, program, deprecation_version_override).await?;
    let snapshot_png_bytes = ctx
        .prepare_snapshot()
        .await
        .map_err(|err| ExecErrorWithState::new(err, exec_state.clone(), None))?
        .contents
        .0;

    // Decode the snapshot, return it.
    let img = image::ImageReader::new(std::io::Cursor::new(snapshot_png_bytes))
        .with_guessed_format()
        .map_err(|e| ExecError::BadPng(e.to_string()))
        .and_then(|x| x.decode().map_err(|e| ExecError::BadPng(e.to_string())))
        .map_err(|err| ExecErrorWithState::new(err, exec_state.clone(), None))?;

    Ok((exec_state, env_ref, img))
}

/// Execute the kcl then export the resulting glb and CPU render an image locally
/// cheaper than engine render since we can use the engine in geometry-only mode.
#[cfg(test)]
pub async fn execute_export_and_render_locally(
    ctx: &ExecutorContext,
    program: Program,
    deprecation_version_override: Option<&str>,
) -> Result<(ExecState, EnvironmentRef, Snapshot3d), ExecErrorWithState> {
    let (exec_state, env_ref) = do_execute(ctx, program, deprecation_version_override).await?;

    // export glb
    let glb_blob_files = match ctx
        .export(kittycad_modeling_cmds::format::OutputFormat3d::Gltf(
            kittycad_modeling_cmds::format::gltf::export::Options::builder()
                .storage(kittycad_modeling_cmds::format::gltf::export::Storage::Binary)
                .build(),
        ))
        .await
    {
        Ok(f) => f,
        Err(err) => {
            // Close the context to avoid any resource leaks.
            ctx.close().await;
            return Err(ExecErrorWithState::new(
                ExecError::BadExport(format!("Export failed: {err:?}")),
                exec_state.clone(),
                None,
            ));
        }
    };
    if glb_blob_files.len() != 1 {
        return Err(ExecErrorWithState::new(
            ExecError::BadExport(format!("Expected 1 glb file, found {}", glb_blob_files.len())),
            exec_state,
            None,
        ));
    }
    let glb: Glb = glb_blob_files
        .into_iter()
        .next()
        .unwrap_or_else(|| RawFile {
            name: String::new(),
            contents: vec![],
        })
        .into();
    let image = glb_render::render(&glb.bytes)
        .map_err(|e| ExecErrorWithState::new(ExecError::BadExport(e), exec_state.clone(), None))?;

    let snap_3d = Snapshot3d { image, glb };
    Ok((exec_state, env_ref, snap_3d))
}

/// single-file binary blob containing mesh and brep
pub struct Glb {
    pub name: String,
    pub bytes: Vec<u8>,
}

impl From<RawFile> for Glb {
    fn from(value: RawFile) -> Self {
        Glb {
            name: value.name,
            bytes: value.contents,
        }
    }
}

#[cfg(test)]
pub enum TestGraphicsArtifact {
    Image(image::DynamicImage),
    ImageAndGlb { image: image::DynamicImage, glb: Glb },
    None,
}

#[cfg(test)]
impl TestGraphicsArtifact {
    pub fn image(self) -> Option<image::DynamicImage> {
        match self {
            Self::Image(img) => Some(img),
            Self::ImageAndGlb { image, .. } => Some(image),
            Self::None => None,
        }
    }
}

#[cfg(test)]
enum TestGraphicsParams {
    /// use the 3d engine scene to render an image
    EngineRender,
    /// the model is exportable. export and CPU render
    ExportAndRender,
    /// the model doesn't need any graphical test output
    None,
}

#[cfg(test)]
impl TestGraphicsParams {
    fn geometry_only(&self) -> bool {
        matches!(self, Self::ExportAndRender | Self::None)
    }
    /// kcl tests have `no3d` or `norun` flags in their declaration.
    /// `norun` means "no graphics" and "no3d" means we want graphics but the model can't yet be exported for local rendering.
    /// Translate these requirements into a more descriptive type here.
    fn from_kcl_sample_spec(no_3d: bool, no_run: bool) -> Self {
        match (no_3d, no_run) {
            (true, false) => Self::EngineRender,
            (false, false) => Self::ExportAndRender,
            (true, true) | (false, true) => Self::None,
        }
    }
}

#[cfg(test)]
pub async fn kcl_doc_execute_and_snapshot(
    code: &str,
    current_file: Option<PathBuf>,
    no_3d: bool,
    no_run: bool,
) -> Result<TestGraphicsArtifact, ExecError> {
    let graphics = TestGraphicsParams::from_kcl_sample_spec(no_3d, no_run);
    let ctx = new_context(true, current_file, graphics.geometry_only()).await?;
    let program = Program::parse_no_errs(code).map_err(KclErrorWithOutputs::no_outputs)?;

    let result = match graphics {
        TestGraphicsParams::EngineRender => execute_locally_and_render_on_engine(&ctx, program, None)
            .await
            .map(|(_, _, image)| TestGraphicsArtifact::Image(image))
            .map_err(|err| err.error)?,
        TestGraphicsParams::ExportAndRender => execute_export_and_render_locally(&ctx, program, None)
            .await
            .map(|(_, _, snap_3d)| TestGraphicsArtifact::ImageAndGlb {
                image: snap_3d.image,
                glb: snap_3d.glb,
            })
            .map_err(|err| err.error)?,
        TestGraphicsParams::None => {
            _ = do_execute(&ctx, program, None).await.map_err(|err| err.error)?;
            TestGraphicsArtifact::None
        }
    };
    ctx.close().await;
    Ok(result)
}

/// Executes a kcl program and takes a snapshot of the result.
/// This returns the bytes of the snapshot.
pub async fn execute_and_snapshot_legacy_sim_test(
    code: &str,
    current_file: Option<PathBuf>,
) -> Result<image::DynamicImage, ExecError> {
    let ctx = new_context_engine_graphics(true, current_file).await?;
    let program = Program::parse_no_errs(code).map_err(KclErrorWithOutputs::no_outputs)?;
    let res = execute_locally_and_render_on_engine(&ctx, program, None)
        .await
        .map(|(_, _, img)| img)
        .map_err(|err| err.error);
    ctx.close().await;
    res
}

/// Executes a KCL program and takes a snapshot without closing the engine
/// connection. If OK, the caller must close the returned context.
/// If Err, the context will already be closed within this function.
#[cfg(test)]
pub async fn execute_and_snapshot_ast_no_close(
    ast: Program,
    current_file: Option<PathBuf>,
    deprecation_version_override: Option<&str>,
) -> Result<(ExecState, ExecutorContext, EnvironmentRef, image::DynamicImage), ExecErrorWithState> {
    execute_and_snapshot_ast_with_heartbeats(ast, current_file, deprecation_version_override, Some(5)).await
}

#[cfg(test)]
async fn execute_and_snapshot_ast_with_heartbeats(
    ast: Program,
    current_file: Option<PathBuf>,
    deprecation_version_override: Option<&str>,
    heartbeats: Option<u64>,
) -> Result<(ExecState, ExecutorContext, EnvironmentRef, image::DynamicImage), ExecErrorWithState> {
    let ctx = new_context_with_heartbeats(true, current_file, heartbeats, false).await?;
    let (exec_state, env, image) =
        match execute_locally_and_render_on_engine(&ctx, ast, deprecation_version_override).await {
            Ok((exec_state, env_ref, image)) => (exec_state, env_ref, image),
            Err(err) => {
                // If there was an error executing the program, return it.
                // Close the context to avoid any resource leaks.
                ctx.close().await;
                return Err(err);
            }
        };
    Ok((exec_state, ctx, env, image))
}

pub async fn execute_and_snapshot_no_auth(
    code: &str,
    current_file: Option<PathBuf>,
) -> Result<(image::DynamicImage, EnvironmentRef), ExecError> {
    let ctx = new_context_engine_graphics(false, current_file).await?;
    let program = Program::parse_no_errs(code).map_err(KclErrorWithOutputs::no_outputs)?;
    let res = execute_locally_and_render_on_engine(&ctx, program, None)
        .await
        .map(|(_, env_ref, image)| (image, env_ref))
        .map_err(|err| err.error);
    ctx.close().await;
    res
}

async fn do_execute(
    ctx: &ExecutorContext,
    program: Program,
    _deprecation_version_override: Option<&str>,
) -> Result<(ExecState, EnvironmentRef), ExecErrorWithState> {
    let mut exec_state = ExecState::new(ctx);
    #[cfg(test)]
    exec_state.set_deprecation_version_override(_deprecation_version_override);
    let _ = ctx.send_clear_scene(&mut exec_state, Default::default()).await;
    let result = ctx.run(&program, &mut exec_state).await;
    let responses = if result.is_err() {
        #[cfg(feature = "snapshot-engine-responses")]
        {
            Some(exec_state.take_root_module_responses())
        }
        #[cfg(not(feature = "snapshot-engine-responses"))]
        None
    } else {
        None
    };
    let result = result.map_err(|err| ExecErrorWithState::new(err.into(), exec_state.clone(), responses))?;
    for issue in exec_state.issues() {
        if issue.severity.is_err() {
            return Err(ExecErrorWithState::new(
                KclErrorWithOutputs::no_outputs(KclError::new_semantic(issue.clone().into())).into(),
                exec_state.clone(),
                None,
            ));
        }
    }

    Ok((exec_state, result.0))
}

pub async fn new_context_engine_graphics(
    with_auth: bool,
    current_file: Option<PathBuf>,
) -> Result<ExecutorContext, ConnectionError> {
    new_context_with_heartbeats(with_auth, current_file, None, false).await
}

pub async fn new_context(
    with_auth: bool,
    current_file: Option<PathBuf>,
    geometry_only: bool,
) -> Result<ExecutorContext, ConnectionError> {
    new_context_with_heartbeats(with_auth, current_file, None, geometry_only).await
}

async fn new_context_with_heartbeats(
    with_auth: bool,
    current_file: Option<PathBuf>,
    heartbeats: Option<u64>,
    geometry_only: bool,
) -> Result<ExecutorContext, ConnectionError> {
    let mut client = new_zoo_client(if with_auth { None } else { Some("bad_token".to_string()) }, None)
        .map_err(ConnectionError::CouldNotMakeClient)?;
    if !with_auth {
        // Use prod, don't override based on env vars.
        // We do this so even in the engine repo, tests that need to run with
        // no auth can fail in the same way as they would in prod.
        client.set_base_url("https://api.zoo.dev".to_string());
    }

    let mut settings = ExecutorSettings {
        highlight_edges: true,
        enable_ssao: false,
        show_grid: false,
        replay: None,
        project_directory: None,
        current_file: None,
        fixed_size_grid: true,
        skip_artifact_graph: false,
        heartbeats,
        default_backface_color: Some("#00D5FF".to_owned()),
        geometry_only,
    };
    if let Some(current_file) = current_file {
        settings.with_current_file(crate::TypedPath(current_file));
    }
    let ctx = ExecutorContext::new(&client, settings)
        .await
        .map_err(ConnectionError::Establishing)?;
    Ok(ctx)
}

pub async fn execute_and_export_step(
    code: &str,
    current_file: Option<PathBuf>,
) -> Result<
    (
        ExecState,
        EnvironmentRef,
        Vec<kittycad_modeling_cmds::websocket::RawFile>,
    ),
    ExecErrorWithState,
> {
    let ctx = new_context_engine_graphics(true, current_file).await?;
    let mut exec_state = ExecState::new(&ctx);
    let program = Program::parse_no_errs(code).map_err(|err| {
        ExecErrorWithState::new(KclErrorWithOutputs::no_outputs(err).into(), exec_state.clone(), None)
    })?;
    let result = ctx
        .run(&program, &mut exec_state)
        .await
        .map_err(|err| ExecErrorWithState::new(err.into(), exec_state.clone(), None))?;
    for issue in exec_state.issues() {
        if issue.severity.is_err() {
            return Err(ExecErrorWithState::new(
                KclErrorWithOutputs::no_outputs(KclError::new_semantic(issue.clone().into())).into(),
                exec_state.clone(),
                None,
            ));
        }
    }

    let files = match ctx.export_step(true).await {
        Ok(f) => f,
        Err(err) => {
            return Err(ExecErrorWithState::new(
                ExecError::BadExport(format!("Export failed: {err:?}")),
                exec_state.clone(),
                None,
            ));
        }
    };

    ctx.close().await;

    Ok((exec_state, result.0, files))
}