Skip to content

Commit

Permalink
fix: trigger tool detection only on complete lines, for performance a…
Browse files Browse the repository at this point in the history
…nd to fix nested codeblocks in tooluse (#251)
  • Loading branch information
jrmi authored Nov 10, 2024
1 parent 7ad4c94 commit 5a228d4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
15 changes: 9 additions & 6 deletions gptme/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 10 additions & 0 deletions tests/test_codeblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("") == []

Expand Down

0 comments on commit 5a228d4

Please sign in to comment.