|
152 | 152 | write_trace, |
153 | 153 | ) |
154 | 154 |
|
155 | | -empty_scope: ScopeType = PdlDict({"pdl_context": DependentContext([])}) |
| 155 | +empty_scope: ScopeType = PdlDict({"pdl_context": DependentContext([]), "__pdl_replay": {}}) |
156 | 156 |
|
157 | 157 |
|
158 | 158 | RefT = TypeVar("RefT") |
@@ -623,6 +623,34 @@ def result_with_type_checking( |
623 | 623 | return result |
624 | 624 |
|
625 | 625 |
|
| 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 | + |
626 | 654 | def process_block_body( |
627 | 655 | state: InterpreterState, |
628 | 656 | scope: ScopeType, |
|
0 commit comments