Skip to content

Commit 3ef4fdb

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 3ef4fdb

File tree

7 files changed

+80
-11
lines changed

7 files changed

+80
-11
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/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
"tauri": "tauri",
1515
"test:quality": "concurrently -n 'lint,types,formatting' 'npm run lint' 'tsc --build --noEmit' \"prettier --check 'tests/**/*.ts' 'src/**/*.{ts,tsx,css}'\"",
1616
"test:ui": "playwright install --with-deps && playwright test",
17+
"test:bee": "./src-tauri/target/debug/pdl compile beeai demos/beeai.py -g --output - | jq",
1718
"types": "(cd .. && python -m src.pdl.pdl --schema > src/pdl/pdl-schema.json) && json2ts ../src/pdl/pdl-schema.json src/pdl_ast.d.ts --unreachableDefinitions && npm run format",
18-
"test": "concurrently -n 'quality,playwright' 'npm run test:quality' 'npm run test:ui'",
19+
"test": "concurrently -n 'quality,playwright,bee' 'npm run test:quality' 'npm run test:ui' 'npm run test:bee'",
1920
"pdl": "./src-tauri/target/debug/pdl",
2021
"view": "npm run pdl view",
2122
"start": "npm run tauri dev"

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: 62 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,70 @@ 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+
.stdout_null()
310+
.run()?;
311+
312+
if *debug {
313+
eprintln!(
314+
"Finished generating BeeAi JSON snapshot to {:?}",
315+
&dry_run_file
316+
)
317+
}
318+
Ok(dry_run_file)
319+
}
320+
279321
pub fn compile(
322+
app_handle: tauri::AppHandle,
280323
source_file_path: &String,
281324
output_path: &String,
282325
debug: &bool,
283326
) -> Result<(), Box<dyn Error>> {
284-
println!("Compiling beeai {} to {}", source_file_path, output_path);
327+
if *debug {
328+
eprintln!("Compiling beeai {} to {}", source_file_path, output_path);
329+
}
285330

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

290347
// Read the JSON contents of the file as an instance of `User`.
348+
let reader = BufReader::new(file);
291349
let bee: BeeAiProgram = from_reader(reader)?;
292350

293351
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)