Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trigger tool detection only on complete lines to help nested code block start detection #251

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading