Skip to content

Commit 5591fd8

Browse files
committed
feat: add a replay mechanism
Signed-off-by: Louis Mandel <[email protected]>
1 parent 67da7c3 commit 5591fd8

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

src/pdl/pdl_interpreter.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@
152152
write_trace,
153153
)
154154

155-
empty_scope: ScopeType = PdlDict({"pdl_context": DependentContext([])})
155+
empty_scope: ScopeType = PdlDict({"pdl_context": DependentContext([]), "__pdl_replay": {}})
156156

157157

158158
RefT = TypeVar("RefT")
@@ -623,6 +623,34 @@ def result_with_type_checking(
623623
return result
624624

625625

626+
def process_block_body_with_replay(
627+
state: InterpreterState,
628+
scope: ScopeType,
629+
block: AdvancedBlockType,
630+
loc: PdlLocationType,
631+
) -> tuple[PdlLazy[Any], LazyMessages, ScopeType, AdvancedBlockType]:
632+
if isinstance(block, LeafBlock):
633+
block_id = block.pdl__id
634+
replay_scope = scope["__pdl_replay"]
635+
assert(isinstance(block_id, str))
636+
assert(isinstance(replay_scope, dict))
637+
try:
638+
result = replay_scope[block_id]
639+
background = SingletonContext(
640+
PdlDict({"role": state.role, "content": result})
641+
)
642+
if state.yield_result:
643+
yield_result(result.result(), block.kind)
644+
if state.yield_background:
645+
yield_background(background)
646+
trace = block
647+
except KeyError:
648+
result, background, scope, trace = process_block_body(state, scope, block, loc)
649+
scope = scope | { "__pdl_replay": (replay_scope | {block_id: result}) }
650+
else:
651+
result, background, scope, trace = process_block_body(state, scope, block, loc)
652+
return result, background, scope, trace
653+
626654
def process_block_body(
627655
state: InterpreterState,
628656
scope: ScopeType,

0 commit comments

Comments
 (0)