Skip to content

Commit 6baaed2

Browse files
committed
refactor: move rust ast type defs into separate module
`interpreter::ast` Signed-off-by: Nick Mitchell <[email protected]>
1 parent d012b0b commit 6baaed2

File tree

3 files changed

+115
-153
lines changed

3 files changed

+115
-153
lines changed

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

Lines changed: 4 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ use ::std::error::Error;
33
use ::std::fs::File;
44
use ::std::io::BufReader;
55

6-
use serde::{Deserialize, Serialize};
6+
use serde::Deserialize;
77
use serde_json::{from_reader, json, to_string, Value};
88

9+
use crate::interpreter::ast::{PdlBaseType, PdlBlock, PdlOptionalType, PdlParser, PdlType};
10+
911
macro_rules! zip {
1012
($x: expr) => ($x);
1113
($x: expr, $($y: expr), +) => (
@@ -116,132 +118,6 @@ struct BeeAiProgram {
116118
workflow: BeeAiWorkflow,
117119
}
118120

119-
#[derive(Serialize, Debug)]
120-
enum Parser {
121-
#[serde(rename = "json")]
122-
Json,
123-
/*#[serde(rename = "jsonl")]
124-
Jsonl,
125-
#[serde(rename = "yaml")]
126-
Yaml,*/
127-
}
128-
129-
#[derive(Serialize, Debug, Clone)]
130-
enum PdlBaseType {
131-
#[serde(rename = "str")]
132-
Str,
133-
#[serde(rename = "bool")]
134-
Bool,
135-
#[serde(rename = "int")]
136-
Int,
137-
#[serde(rename = "null")]
138-
Null,
139-
}
140-
141-
#[derive(Serialize, Debug)]
142-
struct PdlOptionalType {
143-
optional: PdlBaseType,
144-
}
145-
146-
#[derive(Serialize, Debug)]
147-
#[serde(untagged)]
148-
enum PdlType {
149-
Base(PdlBaseType),
150-
Optional(PdlOptionalType),
151-
Object(HashMap<String, PdlType>),
152-
}
153-
154-
#[derive(Serialize, Debug)]
155-
#[serde(untagged)]
156-
enum PdlBlock {
157-
String(String),
158-
/*If {
159-
#[serde(rename = "if")]
160-
condition: String,
161-
then: Box<PdlBlock>,
162-
},*/
163-
Object {
164-
object: HashMap<String, PdlBlock>,
165-
},
166-
Call {
167-
call: String,
168-
#[serde(skip_serializing_if = "Option::is_none")]
169-
args: Option<String>,
170-
#[serde(skip_serializing_if = "Option::is_none")]
171-
defs: Option<HashMap<String, PdlBlock>>,
172-
},
173-
Array {
174-
array: Vec<PdlBlock>,
175-
},
176-
Message {
177-
role: String,
178-
content: Box<PdlBlock>,
179-
#[serde(skip_serializing_if = "Option::is_none")]
180-
description: Option<String>,
181-
#[serde(skip_serializing_if = "Option::is_none")]
182-
name: Option<String>,
183-
#[serde(skip_serializing_if = "Option::is_none")]
184-
tool_call_id: Option<String>,
185-
},
186-
Repeat {
187-
#[serde(rename = "for")]
188-
for_: HashMap<String, String>,
189-
repeat: Box<PdlBlock>,
190-
},
191-
Text {
192-
#[serde(skip_serializing_if = "Option::is_none")]
193-
description: Option<String>,
194-
#[serde(skip_serializing_if = "Option::is_none")]
195-
role: Option<String>,
196-
text: Vec<PdlBlock>,
197-
#[serde(skip_serializing_if = "Option::is_none")]
198-
defs: Option<HashMap<String, PdlBlock>>,
199-
#[serde(skip_serializing_if = "Option::is_none")]
200-
parser: Option<Parser>,
201-
},
202-
Model {
203-
#[serde(skip_serializing_if = "Option::is_none")]
204-
description: Option<String>,
205-
model: String,
206-
#[serde(skip_serializing_if = "Option::is_none")]
207-
def: Option<String>,
208-
parameters: HashMap<String, Value>,
209-
#[serde(skip_serializing_if = "Option::is_none")]
210-
input: Option<Box<PdlBlock>>, // really this should be restricted to be PdlBlock::Array; how do we do this in rust?
211-
#[serde(rename = "modelResponse")]
212-
#[serde(skip_serializing_if = "Option::is_none")]
213-
model_response: Option<String>,
214-
},
215-
Function {
216-
function: HashMap<String, PdlType>,
217-
#[serde(rename = "return")]
218-
return_: Box<PdlBlock>,
219-
},
220-
PythonCode {
221-
lang: String,
222-
code: String,
223-
},
224-
}
225-
/*#[derive(Serialize, Debug)]
226-
struct PdlFunctionJsonSchemaParameters {
227-
#[serde(rename = "type")]
228-
schema_type: String, // TODO constant "object"
229-
properties: Option<HashMap<String, Value>>,
230-
}
231-
#[derive(Serialize, Debug)]
232-
struct PdlFunctionJsonSchema {
233-
name: String,
234-
#[serde(skip_serializing_if = "Option::is_none")]
235-
description: Option<String>,
236-
parameters: PdlFunctionJsonSchemaParameters,
237-
}
238-
#[derive(Serialize, Debug)]
239-
struct PdlFunctionDeclaration {
240-
#[serde(rename = "type")]
241-
declaration_type: String, // TODO constant "function"
242-
function: PdlFunctionJsonSchema,
243-
}*/
244-
245121
fn a_tool(tool: &BeeAiToolState) -> Value {
246122
json!({
247123
"type": "function",
@@ -338,7 +214,7 @@ fn json_loads(
338214
"{{\"{}\": {}}}",
339215
inner_name, value
340216
))],
341-
parser: Some(Parser::Json),
217+
parser: Some(PdlParser::Json),
342218
},
343219
);
344220
Some(m)
@@ -422,20 +298,6 @@ pub fn compile(
422298
.map(|prompt| PdlBlock::String(format!("{}\n", prompt)))
423299
.collect::<Vec<_>>();
424300

425-
/*let system_prompts = bee
426-
.workflow
427-
.workflow
428-
.steps
429-
.values()
430-
.filter_map(|step| step.state.dict.agent_metadata.state.dict.instructions.clone())
431-
.map(|instructions| PdlBlock::Text {
432-
role: Some(String::from("system")),
433-
text: vec![PdlBlock::String(instructions)],
434-
defs: None,
435-
description: None,
436-
})
437-
.collect::<Vec<_>>();*/
438-
439301
let tool_declarations = bee
440302
.workflow
441303
.workflow
@@ -480,17 +342,6 @@ asyncio.run(invoke())
480342
"".to_string()
481343
},
482344
import_fn,
483-
/*schema
484-
.iter()
485-
.filter_map(|(arg, arg_type)| match arg_type {
486-
PdlType::Base(PdlBaseType::Str) =>
487-
Some(format!("\"{}\":\"${{ {} }}\"", arg, arg)),
488-
PdlType::Base(_) =>
489-
Some(format!("\"{}\":${{ {} }}", arg, arg)),
490-
_ => None,
491-
})
492-
.collect::<Vec<_>>()
493-
.join(" "),*/
494345
if *debug {
495346
format!(
496347
"print(f'Response from tool {}: {{result}}')",
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
use ::std::collections::HashMap;
2+
use serde::Serialize;
3+
use serde_json::Value;
4+
5+
#[derive(Serialize, Debug)]
6+
pub enum PdlParser {
7+
#[serde(rename = "json")]
8+
Json,
9+
/*#[serde(rename = "jsonl")]
10+
Jsonl,
11+
#[serde(rename = "yaml")]
12+
Yaml,*/
13+
}
14+
15+
#[derive(Serialize, Debug, Clone)]
16+
pub enum PdlBaseType {
17+
#[serde(rename = "str")]
18+
Str,
19+
#[serde(rename = "bool")]
20+
Bool,
21+
#[serde(rename = "int")]
22+
Int,
23+
#[serde(rename = "null")]
24+
Null,
25+
}
26+
27+
#[derive(Serialize, Debug)]
28+
pub struct PdlOptionalType {
29+
pub optional: PdlBaseType,
30+
}
31+
32+
#[derive(Serialize, Debug)]
33+
#[serde(untagged)]
34+
pub enum PdlType {
35+
Base(PdlBaseType),
36+
Optional(PdlOptionalType),
37+
Object(HashMap<String, PdlType>),
38+
}
39+
40+
#[derive(Serialize, Debug)]
41+
#[serde(untagged)]
42+
pub enum PdlBlock {
43+
String(String),
44+
/*If {
45+
#[serde(rename = "if")]
46+
condition: String,
47+
then: Box<PdlBlock>,
48+
},*/
49+
Object {
50+
object: HashMap<String, PdlBlock>,
51+
},
52+
Call {
53+
call: String,
54+
#[serde(skip_serializing_if = "Option::is_none")]
55+
args: Option<String>,
56+
#[serde(skip_serializing_if = "Option::is_none")]
57+
defs: Option<HashMap<String, PdlBlock>>,
58+
},
59+
Array {
60+
array: Vec<PdlBlock>,
61+
},
62+
Message {
63+
role: String,
64+
content: Box<PdlBlock>,
65+
#[serde(skip_serializing_if = "Option::is_none")]
66+
description: Option<String>,
67+
#[serde(skip_serializing_if = "Option::is_none")]
68+
name: Option<String>,
69+
#[serde(skip_serializing_if = "Option::is_none")]
70+
tool_call_id: Option<String>,
71+
},
72+
Repeat {
73+
#[serde(rename = "for")]
74+
for_: HashMap<String, String>,
75+
repeat: Box<PdlBlock>,
76+
},
77+
Text {
78+
#[serde(skip_serializing_if = "Option::is_none")]
79+
description: Option<String>,
80+
#[serde(skip_serializing_if = "Option::is_none")]
81+
role: Option<String>,
82+
text: Vec<PdlBlock>,
83+
#[serde(skip_serializing_if = "Option::is_none")]
84+
defs: Option<HashMap<String, PdlBlock>>,
85+
#[serde(skip_serializing_if = "Option::is_none")]
86+
parser: Option<PdlParser>,
87+
},
88+
Model {
89+
#[serde(skip_serializing_if = "Option::is_none")]
90+
description: Option<String>,
91+
model: String,
92+
#[serde(skip_serializing_if = "Option::is_none")]
93+
def: Option<String>,
94+
parameters: HashMap<String, Value>,
95+
#[serde(skip_serializing_if = "Option::is_none")]
96+
input: Option<Box<PdlBlock>>, // really this should be restricted to be PdlBlock::Array; how do we do this in rust?
97+
#[serde(rename = "modelResponse")]
98+
#[serde(skip_serializing_if = "Option::is_none")]
99+
model_response: Option<String>,
100+
},
101+
Function {
102+
function: HashMap<String, PdlType>,
103+
#[serde(rename = "return")]
104+
return_: Box<PdlBlock>,
105+
},
106+
PythonCode {
107+
lang: String,
108+
code: String,
109+
},
110+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub mod ast;
12
pub mod extract;
23
pub mod pip;
34
pub mod pull;

0 commit comments

Comments
 (0)