Skip to content

Commit e5b56b5

Browse files
committed
more tests plus some ast builders
Signed-off-by: Nick Mitchell <[email protected]>
1 parent c157bfd commit e5b56b5

File tree

4 files changed

+129
-49
lines changed

4 files changed

+129
-49
lines changed

pdl-live-react/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
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": "until [ -f ./src-tauri/target/debug/pdl ]; do sleep 1; done; for i in ./demos/beeai/*.py; do ./src-tauri/target/debug/pdl compile beeai $i -g --output - | jq; done",
17+
"test:bee": "until [ -f ./src-tauri/target/debug/pdl ]; do sleep 1; done; for i in ./demos/beeai/*.py; do ./src-tauri/target/debug/pdl compile beeai $i -g --output - | jq; done",
18+
"test:interpreter": "cd src-tauri && cargo test",
1819
"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",
19-
"test": "concurrently -n 'quality,playwright' 'npm run test:quality' 'npm run test:ui'",
20+
"test": "concurrently -n 'quality,playwright,interpreter' 'npm run test:quality' 'npm run test:ui' 'npm run test:interpreter'",
2021
"pdl": "./src-tauri/target/debug/pdl",
2122
"view": "npm run pdl view",
2223
"start": "npm run tauri dev"

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

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -171,30 +171,28 @@ fn call_tools(model: &String, parameters: &HashMap<String, Value>) -> PdlBlock {
171171
role: None,
172172
parser: None,
173173
description: Some("Calling tool ${ tool.function.name }".to_string()),
174-
text: vec![PdlBlock::Model(PdlModelBlock {
175-
parameters: Some(parameters.clone()),
176-
description: None, /*Some(
177-
"Sending tool ${ tool.function.name } response back to model".to_string(),
178-
),*/
179-
def: None,
180-
model_response: None,
181-
model: model.clone(),
182-
pdl_result: None,
183-
pdl_usage: None,
184-
input: Some(Box::new(PdlBlock::Array {
185-
array: vec![PdlBlock::Message(PdlMessageBlock {
186-
role: Role::Tool,
187-
description: None,
188-
name: Some("${ tool.function.name }".to_string()),
189-
tool_call_id: Some("${ tool.id }".to_string()),
190-
content: Box::new(PdlBlock::Call(PdlCallBlock {
191-
defs: json_loads(&"args", &"pdl__args", &"${ tool.function.arguments }"),
192-
call: "${ pdl__tools[tool.function.name] }".to_string(), // look up tool in tool_declarations def (see below)
193-
args: Some("${ args }".to_string()), // invoke with arguments as specified by the model
194-
})),
195-
})],
196-
})),
197-
})],
174+
text: vec![PdlBlock::Model(
175+
PdlModelBlock::new(model.as_str())
176+
.parameters(parameters)
177+
.input(PdlBlock::Array {
178+
array: vec![PdlBlock::Message(PdlMessageBlock {
179+
role: Role::Tool,
180+
description: None,
181+
name: Some("${ tool.function.name }".to_string()),
182+
tool_call_id: Some("${ tool.id }".to_string()),
183+
content: Box::new(PdlBlock::Call(PdlCallBlock {
184+
defs: json_loads(
185+
&"args",
186+
&"pdl__args",
187+
&"${ tool.function.arguments }",
188+
),
189+
call: "${ pdl__tools[tool.function.name] }".to_string(), // look up tool in tool_declarations def (see below)
190+
args: Some("${ args }".to_string()), // invoke with arguments as specified by the model
191+
})),
192+
})],
193+
})
194+
.build(),
195+
)],
198196
});
199197

200198
let mut for_ = HashMap::new();
@@ -218,16 +216,15 @@ fn json_loads(
218216
let mut m = HashMap::new();
219217
m.insert(
220218
outer_name.to_owned(),
221-
PdlBlock::Text(PdlTextBlock {
222-
defs: None,
223-
role: None,
224-
description: Some(format!("Parsing json for {}={}", inner_name, value)),
225-
text: vec![PdlBlock::String(format!(
219+
PdlBlock::Text(
220+
PdlTextBlock::new(vec![PdlBlock::String(format!(
226221
"{{\"{}\": {}}}",
227222
inner_name, value
228-
))],
229-
parser: Some(PdlParser::Json),
230-
}),
223+
))])
224+
.description(format!("Parsing json for {}={}", inner_name, value))
225+
.parser(PdlParser::Json)
226+
.build(),
227+
),
231228
);
232229
Some(m)
233230
}

pdl-live-react/src-tauri/src/pdl/ast.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,38 @@ pub struct PdlTextBlock {
6868
pub parser: Option<PdlParser>,
6969
}
7070

71+
impl PdlTextBlock {
72+
pub fn new(text: Vec<PdlBlock>) -> Self {
73+
PdlTextBlock {
74+
defs: None,
75+
description: None,
76+
role: None,
77+
parser: None,
78+
text: text,
79+
}
80+
}
81+
82+
pub fn description(&mut self, description: String) -> &mut Self {
83+
self.description = Some(description);
84+
self
85+
}
86+
87+
pub fn parser(&mut self, parser: PdlParser) -> &mut Self {
88+
self.parser = Some(parser);
89+
self
90+
}
91+
92+
pub fn build(&self) -> Self {
93+
self.clone()
94+
}
95+
}
96+
97+
impl From<Vec<PdlBlock>> for PdlTextBlock {
98+
fn from(v: Vec<PdlBlock>) -> Self {
99+
PdlTextBlock::new(v).build()
100+
}
101+
}
102+
71103
#[derive(Serialize, Deserialize, Debug, Clone)]
72104
pub struct PdlFunctionBlock {
73105
pub function: HashMap<String, PdlType>,
@@ -107,6 +139,40 @@ pub struct PdlModelBlock {
107139
pub pdl_usage: Option<PdlUsage>,
108140
}
109141

142+
impl PdlModelBlock {
143+
pub fn new(model: &str) -> Self {
144+
PdlModelBlock {
145+
def: None,
146+
description: None,
147+
model_response: None,
148+
parameters: None,
149+
pdl_result: None,
150+
pdl_usage: None,
151+
model: model.into(),
152+
input: None,
153+
}
154+
}
155+
156+
pub fn input(&mut self, input: PdlBlock) -> &mut Self {
157+
self.input = Some(Box::new(input));
158+
self
159+
}
160+
161+
pub fn input_str(&mut self, input: &str) -> &mut Self {
162+
self.input = Some(Box::new(PdlBlock::String(input.into())));
163+
self
164+
}
165+
166+
pub fn parameters(&mut self, parameters: &HashMap<String, Value>) -> &mut Self {
167+
self.parameters = Some(parameters.clone());
168+
self
169+
}
170+
171+
pub fn build(&self) -> Self {
172+
self.clone()
173+
}
174+
}
175+
110176
#[derive(Serialize, Deserialize, Debug, Clone)]
111177
pub struct PdlRepeatBlock {
112178
#[serde(rename = "for")]
@@ -145,3 +211,9 @@ pub enum PdlBlock {
145211
Function(PdlFunctionBlock),
146212
PythonCode { lang: String, code: String },
147213
}
214+
215+
impl From<&str> for PdlBlock {
216+
fn from(s: &str) -> Self {
217+
PdlBlock::String(s.into())
218+
}
219+
}

pdl-live-react/src-tauri/src/pdl/interpreter_tests.rs

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ mod tests {
55

66
use crate::pdl::{
77
ast::{
8-
PdlBlock,
9-
/*PdlCallBlock,*/
10-
PdlModelBlock, /*PdlRepeatBlock, PdlTextBlock, PdlUsage, Role,*/
8+
PdlBlock, /*PdlCallBlock,*/
9+
PdlModelBlock, /*PdlRepeatBlock,*/ /*PdlTextBlock,*/ /*, PdlUsage, Role,*/
1110
},
1211
interpreter::run,
1312
};
@@ -18,31 +17,42 @@ mod tests {
1817

1918
#[test]
2019
fn string() -> Result<(), Box<dyn Error>> {
21-
let (messages, _) = run(&PdlBlock::String("hello".into()), false)?;
20+
let (messages, _) = run(&"hello".into(), false)?;
2221
assert_eq!(messages.len(), 1);
2322
assert_eq!(messages[0].role, MessageRole::User);
2423
assert_eq!(messages[0].content, "hello");
2524
Ok(())
2625
}
2726

2827
#[test]
29-
fn single_model() -> Result<(), Box<dyn Error>> {
28+
fn single_model_via_input() -> Result<(), Box<dyn Error>> {
3029
let (messages, _) = run(
31-
&PdlBlock::Model(PdlModelBlock {
32-
def: None,
33-
description: None,
34-
model_response: None,
35-
parameters: None,
36-
pdl_result: None,
37-
pdl_usage: None,
38-
model: DEFAULT_MODEL.into(),
39-
input: Some(Box::new(PdlBlock::String("hello".into()))),
40-
}),
30+
&PdlBlock::Model(PdlModelBlock::new(DEFAULT_MODEL).input_str("hello").build()),
4131
false,
4232
)?;
4333
assert_eq!(messages.len(), 1);
4434
assert_eq!(messages[0].role, MessageRole::Assistant);
4535
assert!(messages[0].content.contains("Hello!"));
4636
Ok(())
4737
}
38+
39+
#[test]
40+
fn single_model_via_text_chain() -> Result<(), Box<dyn Error>> {
41+
let (messages, _) = run(
42+
&PdlBlock::Text(
43+
vec![
44+
"hello".into(),
45+
PdlBlock::Model(PdlModelBlock::new(DEFAULT_MODEL).build()),
46+
]
47+
.into(),
48+
),
49+
false,
50+
)?;
51+
assert_eq!(messages.len(), 2);
52+
assert_eq!(messages[0].role, MessageRole::User);
53+
assert!(messages[0].content.contains("hello"));
54+
assert_eq!(messages[1].role, MessageRole::Assistant);
55+
assert!(messages[1].content.contains("Hello!"));
56+
Ok(())
57+
}
4858
}

0 commit comments

Comments
 (0)