Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5caa766

Browse files
committed
feat: update beeai compiler to support compiling directly from python source
This refactors the pip install logic (small changes) to allow for more flexible specification of which "internal" requirements.txt to use --- internal in the sense that it is specified in our tauri.conf.json to be embedded into the app. Signed-off-by: Nick Mitchell <[email protected]>
1 parent 021dd83 commit 5caa766

File tree

6 files changed

+77
-10
lines changed

6 files changed

+77
-10
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-e git+https://github.com/starpit/bee-agent-framework.git@nick-meta-combo#egg=beeai_framework&subdirectory=python
2+
#beeai_framework==0.1

pdl-live-react/src-tauri/src/cli/run.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use duct::cmd;
33
use futures::executor::block_on;
44
use yaml_rust2::yaml::LoadError;
55

6-
use crate::interpreter::pip::pip_install_interpreter_if_needed;
6+
use crate::interpreter::pip::pip_install_internal_if_needed;
77
use crate::interpreter::pull::pull_if_needed;
88

99
#[cfg(desktop)]
@@ -21,7 +21,8 @@ pub fn run_pdl_program(
2121

2222
// async the model pull and pip installs
2323
let pull_future = pull_if_needed(&source_file_path);
24-
let bin_path_future = pip_install_interpreter_if_needed(app_handle);
24+
let bin_path_future =
25+
pip_install_internal_if_needed(app_handle, &"interpreter/requirements.txt");
2526

2627
// wait for any model pulls to finish
2728
block_on(pull_future).map_err(|e| match e {

pdl-live-react/src-tauri/src/cli/setup.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@ pub fn cli(app: &mut tauri::App) -> Result<(), Box<dyn ::std::error::Error>> {
5050
value: Value::Bool(debug),
5151
..
5252
}),
53-
) => compile::beeai::compile(source_file_path, output_file_path, debug),
53+
) => compile::beeai::compile(
54+
app.handle().clone(),
55+
source_file_path,
56+
output_file_path,
57+
debug,
58+
),
5459
_ => Err(Box::from("Invalid compile subcommand")),
5560
}
5661
}

pdl-live-react/src-tauri/src/compile/beeai.rs

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
use ::std::collections::HashMap;
22
use ::std::error::Error;
3+
use ::std::ffi::OsStr;
34
use ::std::fs::File;
45
use ::std::io::BufReader;
6+
use ::std::path::{Path, PathBuf};
57

8+
use duct::cmd;
9+
use futures::executor::block_on;
610
use serde::Deserialize;
711
use serde_json::{from_reader, json, to_string, Value};
12+
use tempfile::Builder;
813

914
use crate::interpreter::ast::{PdlBaseType, PdlBlock, PdlOptionalType, PdlParser, PdlType};
15+
use crate::interpreter::pip::pip_install_internal_if_needed;
1016

1117
macro_rules! zip {
1218
($x: expr) => ($x);
@@ -276,18 +282,69 @@ fn tool_imports(object: &String) -> (&str, &str) {
276282
}
277283
}
278284

285+
fn python_source_to_json(
286+
app_handle: tauri::AppHandle,
287+
source_file_path: &String,
288+
debug: &bool,
289+
) -> Result<PathBuf, Box<dyn Error>> {
290+
if *debug {
291+
eprintln!("Compiling from Python source");
292+
}
293+
let bin_path = block_on(pip_install_internal_if_needed(
294+
app_handle,
295+
&"interpreter/beeai-requirements.txt",
296+
))?;
297+
298+
let dry_run_file_path = Builder::new()
299+
.prefix(&"pdl-bee")
300+
.suffix(".json")
301+
.tempfile()?;
302+
let (_f, dry_run_file) = dry_run_file_path.keep()?;
303+
304+
let args = vec![source_file_path];
305+
306+
cmd(bin_path.join("python"), &args)
307+
.env("DRY_RUN", "True")
308+
.env("DRY_RUN_FILE", &dry_run_file)
309+
.run()?;
310+
311+
if *debug {
312+
eprintln!(
313+
"Finished generating BeeAi JSON snapshot to {:?}",
314+
&dry_run_file
315+
)
316+
}
317+
Ok(dry_run_file)
318+
}
319+
279320
pub fn compile(
321+
app_handle: tauri::AppHandle,
280322
source_file_path: &String,
281323
output_path: &String,
282324
debug: &bool,
283325
) -> Result<(), Box<dyn Error>> {
284-
println!("Compiling beeai {} to {}", source_file_path, output_path);
326+
if *debug {
327+
eprintln!("Compiling beeai {} to {}", source_file_path, output_path);
328+
}
285329

286-
// Open the file in read-only mode with buffer.
287-
let file = File::open(source_file_path)?;
288-
let reader = BufReader::new(file);
330+
let file = match Path::new(source_file_path)
331+
.extension()
332+
.and_then(OsStr::to_str)
333+
{
334+
Some("py") => {
335+
let json_snapshot_file = python_source_to_json(app_handle, source_file_path, debug)?;
336+
File::open(json_snapshot_file)
337+
}
338+
_ => {
339+
if *debug {
340+
eprintln!("Compiling from JSON snapshot");
341+
}
342+
File::open(source_file_path)
343+
}
344+
}?;
289345

290346
// Read the JSON contents of the file as an instance of `User`.
347+
let reader = BufReader::new(file);
291348
let bee: BeeAiProgram = from_reader(reader)?;
292349

293350
let inputs: Vec<PdlBlock> = bee

pdl-live-react/src-tauri/src/interpreter/pip.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,14 @@ pub async fn pip_install_if_needed(
3737
}
3838

3939
#[cfg(desktop)]
40-
pub async fn pip_install_interpreter_if_needed(
40+
pub async fn pip_install_internal_if_needed(
4141
app_handle: tauri::AppHandle,
42+
requirements: &str,
4243
) -> Result<PathBuf, tauri::Error> {
4344
// the interpreter requirements.txt
4445
let requirements_path = app_handle
4546
.path()
46-
.resolve("interpreter/requirements.txt", BaseDirectory::Resource)?;
47+
.resolve(requirements, BaseDirectory::Resource)?;
4748

4849
let cache_path = app_handle.path().cache_dir()?.join("pdl");
4950

pdl-live-react/src-tauri/tauri.conf.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@
8888
"active": true,
8989
"targets": "all",
9090
"resources": {
91-
"../requirements.txt": "interpreter/requirements.txt"
91+
"../requirements.txt": "interpreter/requirements.txt",
92+
"../beeai-requirements.txt": "interpreter/beeai-requirements.txt"
9293
},
9394
"icon": [
9495
"icons/32x32.png",

0 commit comments

Comments
 (0)