Skip to content

Commit ad10597

Browse files
authored
refactor: AST and UI cleanup (#984)
Signed-off-by: Louis Mandel <[email protected]>
1 parent dc21fd4 commit ad10597

File tree

10 files changed

+46
-49
lines changed

10 files changed

+46
-49
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ fn call_tools(model: &String, parameters: &HashMap<String, Value>) -> PdlBlock {
216216
metadata: None,
217217
body: Message(MessageBlock {
218218
role: Role::Tool,
219-
pdl__defsite: None,
219+
pdl_defsite: None,
220220
name: Some("${ tool.function.name }".to_string()),
221221
tool_call_id: Some("${ tool.id }".to_string()),
222222
content: Box::new(Advanced(Block {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,8 @@ pub struct MessageBlock {
414414
pub content: Box<PdlBlock>,
415415

416416
/// pdl_id of block that defined the `content of this message
417-
#[serde(skip_serializing_if = "Option::is_none")]
418-
pub pdl__defsite: Option<String>,
417+
#[serde(rename = "pdl__defsite", skip_serializing_if = "Option::is_none")]
418+
pub pdl_defsite: Option<String>,
419419

420420
/// For example, the name of the tool that was invoked, for which this message is the tool response
421421
#[serde(skip_serializing_if = "Option::is_none")]

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,7 +1081,7 @@ impl<'a> Interpreter<'a> {
10811081
content: Box::new(PdlBlock::String(m.content.clone())),
10821082
name: None,
10831083
tool_call_id: None,
1084-
pdl__defsite: None,
1084+
pdl_defsite: None,
10851085
})
10861086
.collect(),
10871087
);
@@ -1094,7 +1094,7 @@ impl<'a> Interpreter<'a> {
10941094
content: Box::new(PdlBlock::String(m.content.clone())),
10951095
name: None,
10961096
tool_call_id: None,
1097-
pdl__defsite: None,
1097+
pdl_defsite: None,
10981098
})
10991099
.collect(),
11001100
);
@@ -1476,7 +1476,7 @@ impl<'a> Interpreter<'a> {
14761476
role: block.role.clone(),
14771477
content: Box::new(content_trace),
14781478
name: name,
1479-
pdl__defsite: None,
1479+
pdl_defsite: None,
14801480
tool_call_id: tool_call_id,
14811481
}),
14821482
))

pdl-live-react/src/pdl_ast.d.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1893,7 +1893,6 @@ export type Else =
18931893
| ErrorBlock
18941894
| EmptyBlock
18951895
| null
1896-
export type IfResult = boolean | null
18971896
/**
18981897
* Name of the variable used to store the result of the execution of the block.
18991898
*
@@ -2705,6 +2704,11 @@ export type PdlContext19 =
27052704
export type PdlId19 = string | null
27062705
export type PdlIsLeaf19 = true
27072706
export type Kind19 = "call"
2707+
/**
2708+
* Function to call.
2709+
*
2710+
*/
2711+
export type Call = LocalizedExpression | FunctionBlock | string
27082712
export type PdlTrace3 =
27092713
| boolean
27102714
| number
@@ -3048,7 +3052,7 @@ export interface CallBlock {
30483052
pdl__timing?: PdlTiming | null
30493053
pdl__is_leaf?: PdlIsLeaf19
30503054
kind?: Kind19
3051-
call: unknown
3055+
call: Call
30523056
args?: unknown
30533057
pdl__trace?: PdlTrace3
30543058
}
@@ -3715,7 +3719,6 @@ export interface IfBlock {
37153719
if: If1
37163720
then: Then1
37173721
else?: Else
3718-
if_result?: IfResult
37193722
}
37203723
/**
37213724
* Set of definitions executed before the execution of the block.
Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
1+
import { match, P } from "ts-pattern"
12
import Group from "../Group"
3+
import { stringify } from "yaml"
24

35
export default function ModelItems({
4-
block: { if: condition, if_result },
6+
block: { if: condition },
57
}: {
68
block: import("../../../pdl_ast").IfBlock
79
}) {
810
return (
911
<>
10-
{typeof condition === "string" && (
11-
<Group
12-
description={condition.replace(/^\$\{*(.+)\}$/, "$1").trim()}
13-
term="Condition"
14-
/>
15-
)}
16-
{if_result !== undefined && (
17-
<Group
18-
description={
19-
if_result === true
20-
? "Then (condition is true)"
21-
: "Else (condition is false)"
22-
}
23-
term="Then or Else?"
24-
/>
25-
)}
12+
{match(condition)
13+
.with(P.string, (cond) => (
14+
<Group description={cond.trim()} term="Condition" />
15+
))
16+
.with({ pdl__expr: P._ }, (cond) => (
17+
<Group description={stringify(cond.pdl__expr)} term="Condition" />
18+
))
19+
.otherwise((cond) => (
20+
<Group description={stringify(cond)} term="Condition" />
21+
))}
22+
{match(condition)
23+
.with({ pdl__result: P.boolean }, (cond) => (
24+
<Group
25+
description={
26+
cond.pdl__result
27+
? "Then (condition is true)"
28+
: "Else (condition is false)"
29+
}
30+
term="Then or Else?"
31+
/>
32+
))
33+
.otherwise(() => false)}
2634
</>
2735
)
2836
}

pdl-live-react/src/view/timeline/model.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { match } from "ts-pattern"
1+
import { match, P } from "ts-pattern"
22

33
import { type PdlBlock } from "../../pdl_ast"
44
import {
@@ -127,9 +127,10 @@ export function childrenOf(block: NonScalarPdlBlock) {
127127
.with({ kind: "code" }, (data) => [data.pdl__result])
128128
.with({ kind: "get" }, (data) => [data.pdl__result])
129129
.with({ kind: "data" }, (data) => [data.pdl__result])
130-
.with({ kind: "if" }, (data) =>
131-
data.if_result ? [data.then] : [data.else],
130+
.with({ kind: "if", if: { pdl__result: P._ } }, (data) =>
131+
data.if.pdl__result ? [data.then] : [data.else],
132132
)
133+
.with({ kind: "if" }, (data) => [data.then, data.else])
133134
.with({ kind: "match" }, (data) => [data.with]) // TODO
134135
.with({ kind: "read" }, (data) => [data.pdl__result])
135136
.with({ kind: "include" }, (data) => [data.pdl__trace ?? data.pdl__result])

src/pdl/pdl-schema.json

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,7 +1330,9 @@
13301330
{
13311331
"$ref": "#/$defs/LocalizedExpression_TypeVar_"
13321332
},
1333-
{},
1333+
{
1334+
"$ref": "#/$defs/FunctionBlock"
1335+
},
13341336
{
13351337
"type": "string"
13361338
}
@@ -5245,18 +5247,6 @@
52455247
"default": null,
52465248
"description": "Branch to execute if the condition is false.\n ",
52475249
"title": "Else"
5248-
},
5249-
"if_result": {
5250-
"anyOf": [
5251-
{
5252-
"type": "boolean"
5253-
},
5254-
{
5255-
"type": "null"
5256-
}
5257-
],
5258-
"default": null,
5259-
"title": "If Result"
52605250
}
52615251
},
52625252
"required": [

src/pdl/pdl_ast.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -355,10 +355,10 @@ class Block(BaseModel):
355355
Typical roles are `system`, `user`, and `assistant`,
356356
but there may be other roles such as `available_tools`.
357357
"""
358+
# Fields for internal use
358359
pdl__context: Optional[ModelInput] = []
359360
"""Current context
360361
"""
361-
# Fields for internal use
362362
pdl__id: Optional[str] = ""
363363
"""Unique identifier for this block
364364
"""
@@ -406,7 +406,7 @@ class CallBlock(LeafBlock):
406406
"""Calling a function."""
407407

408408
kind: Literal[BlockKind.CALL] = BlockKind.CALL
409-
call: ExpressionType
409+
call: ExpressionType[FunctionBlock]
410410
"""Function to call.
411411
"""
412412
args: ExpressionType = {}
@@ -740,8 +740,6 @@ class IfBlock(StructuredBlock):
740740
else_: Optional["BlockType"] = Field(default=None, alias="else")
741741
"""Branch to execute if the condition is false.
742742
"""
743-
# Field for internal use
744-
if_result: Optional[bool] = None
745743

746744

747745
class MatchCase(BaseModel):

src/pdl/pdl_dumper.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,6 @@ def block_to_dict( # noqa: C901
218218
d["then"] = block_to_dict(block.then, json_compatible)
219219
if block.else_ is not None:
220220
d["else"] = block_to_dict(block.else_, json_compatible)
221-
if block.if_result is not None:
222-
d["if_result"] = block.if_result
223221
case MatchBlock():
224222
d["match"] = expr_to_dict(block.match_, json_compatible)
225223
d["with"] = [

src/pdl/pdl_interpreter.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,6 @@ def process_block_body(
688688
trace = trace.model_copy(
689689
update={
690690
"condition": if_trace,
691-
"if_result": b,
692691
}
693692
)
694693
case MatchBlock():

0 commit comments

Comments
 (0)