Skip to content

Commit f3dfd30

Browse files
authored
fix: improve dumping and UI for functions (#988)
Signed-off-by: Louis Mandel <[email protected]>
1 parent 0a0047e commit f3dfd30

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import {
1919

2020
import type Tile from "./Tile"
2121
import { hasStabilityMetrics, type StabilityMetric } from "./stability"
22+
import { match, P } from "ts-pattern"
23+
import { block_code_cleanup } from "../../pdl_code_cleanup"
2224

2325
/** The final result of the block */
2426
/* function result(block: import("../../pdl_ast").PdlBlock) {
@@ -60,14 +62,18 @@ export default function computeModel(block: import("../../pdl_ast").PdlBlock) {
6062
const { resultForDisplay, meta, lang } = isLLMBlock(block)
6163
? extractStructuredModelResponse(block)
6264
: {
63-
resultForDisplay:
64-
typeof block.pdl__result === "object"
65-
? stringify(block.pdl__result)
66-
: typeof block.pdl__result === "string" ||
67-
typeof block.pdl__result === "number" ||
68-
typeof block.pdl__result === "boolean"
69-
? block.pdl__result
70-
: String(block.pdl__result),
65+
resultForDisplay: match(block.pdl__result)
66+
.with(
67+
{ kind: "function", function: P._, return: P._ },
68+
// @ts-expect-error: unable to check that it is a function block
69+
(closure) => stringify(block_code_cleanup(closure)),
70+
)
71+
.with({}, (result) => stringify(result))
72+
.with(
73+
P.union(P.string, P.number, P.boolean),
74+
(result) => result,
75+
)
76+
.otherwise((result) => String(result)),
7177
meta: undefined,
7278
lang:
7379
typeof block.pdl__result === "object"

src/pdl/pdl_dumper.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -276,12 +276,7 @@ def block_to_dict( # noqa: C901
276276
]:
277277
d["contribute"] = contribute_to_list(block.contribute)
278278
if block.pdl__result is not None:
279-
if isinstance(block.pdl__result, FunctionBlock):
280-
d["pdl__result"] = ""
281-
elif json_compatible:
282-
d["pdl__result"] = as_json(block.pdl__result.result())
283-
else:
284-
d["pdl__result"] = block.pdl__result.result()
279+
d["pdl__result"] = data_to_dict(block.pdl__result.result(), json_compatible)
285280
if block.parser is not None:
286281
d["parser"] = parser_to_dict(block.parser)
287282
# if block.pdl__location is not None:
@@ -302,7 +297,10 @@ def block_to_dict( # noqa: C901
302297

303298

304299
def data_to_dict(data: Any, json_compatible: bool):
305-
if json_compatible:
300+
d: Any
301+
if isinstance(data, FunctionBlock):
302+
d = block_to_dict(data, json_compatible)
303+
elif json_compatible:
306304
d = as_json(data)
307305
else:
308306
d = data

tests/test_examples_run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ def __execute_file(self, pdl_file_name: str) -> None:
228228

229229
exec_result.result = str(output["result"])
230230
exec_result.error_code = ExecutionErrorCode.NO_ERROR
231-
231+
pdl.write_trace("/dev/null", output["trace"])
232232
except PDLParseError:
233233
exec_result.error_code = ExecutionErrorCode.PARSE_ERROR
234234
except Exception:

0 commit comments

Comments
 (0)