Skip to content
Closed
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
19 changes: 8 additions & 11 deletions py/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def render_text_chunks(chunks):

def encode_image(image_path):
"""Encodes an image file to a base64 string."""
with open(image_path, "rb") as image_file:
with open(os.path.expanduser(image_path), "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')


Expand All @@ -123,17 +123,15 @@ def is_image_path(path):
def parse_include_paths(path):
if not path:
return []
pwd = vim.eval('getcwd()')

path = os.path.expanduser(path)
if not os.path.isabs(path):
path = os.path.join(pwd, path)
home_path = os.path.expanduser('~')

expanded_paths = [path]
if '*' in path:
expanded_paths = sorted(glob.glob(path, recursive=True))
def _unexpand(path):
return path.replace(home_path, '~', 1) if path.startswith(home_path) else path

return [path for path in expanded_paths if not os.path.isdir(path)]
pwd = vim.eval('getcwd()')
paths = sorted(glob.glob(os.path.expanduser(path), recursive=True, root_dir=pwd))
return [_unexpand(path) for path in paths if not os.path.isdir(path)]

def make_image_message(path):
ext = path.split('.')[-1]
Expand All @@ -142,8 +140,7 @@ def make_image_message(path):

def make_text_file_message(path):
try:
path = os.path.relpath(path)
with open(path, 'r') as file:
with open(os.path.expanduser(path), 'r') as file:
file_content = file.read().strip()
return { 'type': 'text', 'text': f'==> {path} <==\n' + file_content.strip() }
except UnicodeDecodeError:
Expand Down
24 changes: 12 additions & 12 deletions tests/chat_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import os

curr_dir = os.path.dirname(__file__)
relative_dir = 'tests'
relative_dir = os.path.relpath(curr_dir)
home_dir = os.path.expanduser('~')
abs_dir_private_home = curr_dir.replace(home_dir, '~', 1)

def strip_text(txt):
txt = txt.strip()
Expand Down Expand Up @@ -144,7 +146,7 @@ def test_parse_include_single_file_message():

>>> include

{curr_dir}/resources/test1.include.txt
{relative_dir}/resources/test1.include.txt

<<< assistant

Expand All @@ -154,7 +156,6 @@ def test_parse_include_single_file_message():

try harder
""")
messages = parse_chat_messages(chat_content)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed this (and another one below) because it looks like a leftover.

actual_messages = parse_chat_messages(chat_content)
assert [
{
Expand Down Expand Up @@ -199,9 +200,8 @@ def test_parse_include_multiple_files_message():
>>> include

{curr_dir}/resources/test1.include.txt
{curr_dir}/resources/test2.include.txt
{relative_dir}/resources/test2.include.txt
""")
messages = parse_chat_messages(chat_content)
actual_messages = parse_chat_messages(chat_content)
assert [
{
Expand All @@ -213,7 +213,7 @@ def test_parse_include_multiple_files_message():
},
{
'type': 'text',
'text': f'==> {relative_dir}/resources/test1.include.txt <==\nhello world',
'text': f'==> {abs_dir_private_home}/resources/test1.include.txt <==\nhello world',
},
{
'type': 'text',
Expand Down Expand Up @@ -244,11 +244,11 @@ def test_parse_include_glob_files_message():
},
{
'type': 'text',
'text': f'==> {relative_dir}/resources/test1.include.txt <==\nhello world',
'text': f'==> {abs_dir_private_home}/resources/test1.include.txt <==\nhello world',
},
{
'type': 'text',
'text': f'==> {relative_dir}/resources/test2.include.txt <==\nvim is awesome',
'text': f'==> {abs_dir_private_home}/resources/test2.include.txt <==\nvim is awesome',
},
],
},
Expand Down Expand Up @@ -289,7 +289,7 @@ def test_parse_include_image_with_files_message():

{curr_dir}/resources/test1.include.txt
{curr_dir}/resources/image_file.jpg
{curr_dir}/resources/test2.include.txt
{relative_dir}/resources/test2.include.txt
""")
actual_messages = parse_chat_messages(chat_content)
assert [
Expand All @@ -298,7 +298,7 @@ def test_parse_include_image_with_files_message():
'content': [
{
'type': 'text',
'text': f'==> {relative_dir}/resources/test1.include.txt <==\nhello world',
'text': f'==> {abs_dir_private_home}/resources/test1.include.txt <==\nhello world',
},
{
'type': 'image_url',
Expand Down Expand Up @@ -328,11 +328,11 @@ def test_parse_include_unsupported_binary_file():
'content': [
{
'type': 'text',
'text': f'==> {relative_dir}/resources/binary_file.bin <==\nBinary file, cannot display',
'text': f'==> {abs_dir_private_home}/resources/binary_file.bin <==\nBinary file, cannot display',
},
{
'type': 'text',
'text': f'==> {relative_dir}/resources/test1.include.txt <==\nhello world',
'text': f'==> {abs_dir_private_home}/resources/test1.include.txt <==\nhello world',
},
],
},
Expand Down