From 0045ebb1f98cc74983ab1265266aed870ff54d7d Mon Sep 17 00:00:00 2001 From: Richard Abrich Date: Thu, 25 Jul 2024 12:34:57 -0400 Subject: [PATCH] fix(VisualReplayStrategy): avoid re-using failing segmentations --- openadapt/drivers/openai.py | 3 +++ openadapt/strategies/visual.py | 17 +++++++++-------- openadapt/utils.py | 7 +++++-- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/openadapt/drivers/openai.py b/openadapt/drivers/openai.py index 01e0219ad..f81e23747 100644 --- a/openadapt/drivers/openai.py +++ b/openadapt/drivers/openai.py @@ -127,6 +127,9 @@ def get_response( if "error" in result: error = result["error"] message = error["message"] + logger.warning(f"{message=}") + if "retry" in message: + return get_response(payload) raise Exception(message) return result diff --git a/openadapt/strategies/visual.py b/openadapt/strategies/visual.py index 256aaa0dc..1d29a04ab 100644 --- a/openadapt/strategies/visual.py +++ b/openadapt/strategies/visual.py @@ -391,14 +391,15 @@ def get_window_segmentation( if DEBUG: original_image.show() - similar_segmentation, similar_segmentation_diff = find_similar_image_segmentation( - original_image, - ) - if similar_segmentation: - # TODO XXX: create copy of similar_segmentation, but overwrite with segments of - # regions of new image where segments of similar_segmentation overlap non-zero - # regions of similar_segmentation_diff - return similar_segmentation + if not exceptions: + similar_segmentation, similar_segmentation_diff = ( + find_similar_image_segmentation(original_image) + ) + if similar_segmentation: + # TODO XXX: create copy of similar_segmentation, but overwrite with segments + # of regions of new image where segments of similar_segmentation overlap + # non-zero regions of similar_segmentation_diff + return similar_segmentation segmentation_adapter = adapters.get_default_segmentation_adapter() segmented_image = segmentation_adapter.fetch_segmented_image(original_image) diff --git a/openadapt/utils.py b/openadapt/utils.py index 6f22f6cea..dc2795f15 100644 --- a/openadapt/utils.py +++ b/openadapt/utils.py @@ -591,7 +591,10 @@ def parse_code_snippet(snippet: str) -> dict: """ code_block = extract_code_block(snippet) # remove backtick lines - code_content = "\n".join(code_block.splitlines()[1:-1]) + if "```" in code_block: + code_content = "\n".join(code_block.splitlines()[1:-1]) + else: + code_content = code_block # convert literals from Javascript to Python to_by_from = { "true": "True", @@ -637,7 +640,7 @@ def extract_code_block(text: str) -> str: raise ValueError("Uneven number of backtick lines") if len(backtick_idxs) < 2: - return "" # No enclosing backticks found, return empty string + return text # Extract only the lines between the first and last backtick line, # including the backticks