diff --git a/gptme/llm.py b/gptme/llm.py index 99407480..bf2d647b 100644 --- a/gptme/llm.py +++ b/gptme/llm.py @@ -96,12 +96,15 @@ def print_clear(): # need to flush stdout to get the print to show up sys.stdout.flush() - # TODO: make this more robust/general, maybe with a callback that runs on each char/chunk - # pause inference on finished code-block, letting user run the command before continuing - tooluses = list(ToolUse.iter_from_content(output)) - if tooluses and any(tooluse.is_runnable for tooluse in tooluses): - logger.debug("Found tool use, breaking") - break + # Trigger the tool detection only if the line is finished. + # Helps to detect nested start code blocks. + if char == "\n": + # TODO: make this more robust/general, maybe with a callback that runs on each char/chunk + # pause inference on finished code-block, letting user run the command before continuing + tooluses = list(ToolUse.iter_from_content(output)) + if tooluses and any(tooluse.is_runnable for tooluse in tooluses): + logger.debug("Found tool use, breaking") + break except KeyboardInterrupt: return Message("assistant", output + "... ^C Interrupted") finally: diff --git a/tests/test_codeblock.py b/tests/test_codeblock.py index aba2f98e..2705eb2b 100644 --- a/tests/test_codeblock.py +++ b/tests/test_codeblock.py @@ -58,6 +58,16 @@ def print_readme(): ] +def test_extract_codeblocks_unfinished_nested(): + markdown = """ +```python +def print_readme(): + print('''Usage: +```javascript +""" + assert Codeblock.iter_from_markdown(markdown) == [] + + def test_extract_codeblocks_empty(): assert Codeblock.iter_from_markdown("") == []