Skip to content

Commit

Permalink
fix: stop crash when parsing empty files and strings
Browse files Browse the repository at this point in the history
Signed-off-by: Donnie Adams <[email protected]>
  • Loading branch information
thedadams committed Aug 13, 2024
1 parent ce89f63 commit 67f6e5c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions gptscript/gptscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,16 @@ def run(
async def parse(self, file_path: str, disable_cache: bool = False) -> list[Text | Tool]:
out = await self._run_basic_command("parse", {"file": file_path, "disableCache": disable_cache})
parsed_nodes = json.loads(out)
if parsed_nodes is None or parsed_nodes.get("nodes", None) is None:
return []
return [Text(**node["textNode"]) if "textNode" in node else Tool(**node.get("toolNode", {}).get("tool", {})) for
node in parsed_nodes.get("nodes", [])]

async def parse_tool(self, tool_def: str) -> list[Text | Tool]:
out = await self._run_basic_command("parse", {"content": tool_def})
parsed_nodes = json.loads(out)
if parsed_nodes is None or parsed_nodes.get("nodes", None) is None:
return []
return [Text(**node["textNode"]) if "textNode" in node else Tool(**node.get("toolNode", {}).get("tool", {})) for
node in parsed_nodes.get("nodes", [])]

Expand Down
Empty file added tests/fixtures/empty.gpt
Empty file.
13 changes: 13 additions & 0 deletions tests/test_gptscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,19 @@ async def test_parse_simple_file(gptscript):
"Unexpected output from parsing simple file"


@pytest.mark.asyncio
async def test_parse_empty_file(gptscript):
wd = os.getcwd()
tools = await gptscript.parse(wd + "/tests//fixtures/empty.gpt")
assert len(tools) == 0, "Unexpected number of tools for parsing emtpy file"


@pytest.mark.asyncio
async def test_parse_empty_str(gptscript):
tools = await gptscript.parse_tool("")
assert len(tools) == 0, "Unexpected number of tools for parsing empty string"


@pytest.mark.asyncio
async def test_parse_tool_with_metadata(gptscript):
wd = os.getcwd()
Expand Down

0 comments on commit 67f6e5c

Please sign in to comment.