@@ -42,17 +42,39 @@ def parse(self, text: str, context: AgentContext) -> Action:
4242 def _blocks_from_text (client : Steamship , text : str ) -> List [Block ]:
4343 last_response = text .split ("AI:" )[- 1 ].strip ()
4444
45- block_id_regex = r"(?:\[ Block)?\(?([A-F0-9]{8}\-[A-F0-9]{4}\-[A-F0-9]{4}\-[A-F0-9]{4}\-[A-F0-9]{12})\)?\] ?"
45+ block_id_regex = r"(?:(?:\[|\()? Block)?\(?([A-F0-9]{8}\-[A-F0-9]{4}\-[A-F0-9]{4}\-[A-F0-9]{4}\-[A-F0-9]{12})\)?(?:(\]|\))) ?"
4646 remaining_text = last_response
4747 result_blocks : List [Block ] = []
4848 while remaining_text is not None and len (remaining_text ) > 0 :
4949 match = re .search (block_id_regex , remaining_text )
5050 if match :
51- result_blocks .append (Block (text = remaining_text [0 : match .start ()]))
51+ pre_block_text = ReACTOutputParser ._remove_block_prefix (
52+ candidate = remaining_text [0 : match .start ()]
53+ )
54+ if len (pre_block_text ) > 0 :
55+ result_blocks .append (Block (text = pre_block_text ))
5256 result_blocks .append (Block .get (client , _id = match .group (1 )))
53- remaining_text = remaining_text [match .end () :]
57+ remaining_text = ReACTOutputParser ._remove_block_suffix (
58+ remaining_text [match .end () :]
59+ )
5460 else :
5561 result_blocks .append (Block (text = remaining_text ))
5662 remaining_text = ""
5763
5864 return result_blocks
65+
66+ @staticmethod
67+ def _remove_block_prefix (candidate : str ) -> str :
68+ removed = candidate
69+ if removed .endswith ("(Block" ) or removed .endswith ("[Block" ):
70+ removed = removed [len ("Block" ) + 1 :]
71+ elif removed .endswith ("Block" ):
72+ removed = removed [len ("Block" ) :]
73+ return removed
74+
75+ @staticmethod
76+ def _remove_block_suffix (candidate : str ) -> str :
77+ removed = candidate
78+ if removed .startswith (")" ) or removed .endswith ("]" ):
79+ removed = removed [1 :]
80+ return removed
0 commit comments