Skip to content

Commit

Permalink
Chat updates
Browse files Browse the repository at this point in the history
  • Loading branch information
jlowin committed Nov 20, 2023
1 parent 095084d commit 90f8d1f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/marvin/beta/chat_ui/chat_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async def get_messages() -> list[ThreadMessage]:

def server_process(host, port, thread_id, message_queue):
app = create_app(thread_id, message_queue)
config = uvicorn.Config(app, host=host, port=port, log_level="info")
config = uvicorn.Config(app, host=host, port=port, log_level="warning")
server = uvicorn.Server(config)
server.run()

Expand Down
2 changes: 1 addition & 1 deletion src/marvin/beta/chat_ui/static/chat.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
.user-message {
background-color: #b0ebff;
align-self: end;
text-align: right;
text-align: left;
margin-left: auto;
}

Expand Down
19 changes: 13 additions & 6 deletions src/marvin/beta/chat_ui/static/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ document.addEventListener('DOMContentLoaded', function () {

chatContainer.appendChild(messageDiv);
}
// Function to load messages from the thread
async function loadMessages(scrollDown = false) {

async function loadMessages() {
const shouldScroll = chatContainer.scrollTop + chatContainer.clientHeight >= chatContainer.scrollHeight - 1;

const response = await fetch(`http://127.0.0.1:${serverPort}/api/messages/`);
if (response.ok) {
const messages = await response.json();
Expand All @@ -45,13 +47,18 @@ document.addEventListener('DOMContentLoaded', function () {
const isUser = message.role === 'user';
appendMessage(message, isUser);
});
if (scrollDown) {
chatContainer.scrollTop = chatContainer.scrollHeight; // Scroll to the bottom of the chat

// Scroll after messages are appended
if (shouldScroll) {
chatContainer.scrollTop = chatContainer.scrollHeight;
}
} else {
console.error('Failed to load messages:', response.statusText);
}
}
}

// Rest of your JavaScript code


// Function to post a new message to the thread
async function sendChatMessage() {
Expand Down Expand Up @@ -83,6 +90,6 @@ document.addEventListener('DOMContentLoaded', function () {
sendButton.addEventListener('click', sendChatMessage);

// Initial loading of messages
loadMessages(scrollDown = true);
loadMessages();
setInterval(loadMessages, 1250); // Polling to refresh messages
});
40 changes: 25 additions & 15 deletions src/marvin/tools/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@


def _safe_create_file(path: str) -> None:
path = os.path.expanduser(path)
file_path = pathlib.Path(path)
file_path.parent.mkdir(parents=True, exist_ok=True)
file_path.touch(exist_ok=True)
Expand All @@ -14,22 +15,22 @@ def getcwd() -> str:
return os.getcwd()


def write(filename: str, contents: str) -> str:
def write(path: str, contents: str) -> str:
"""Creates or overwrites a file with the given contents"""

_safe_create_file(filename)
with open(filename, "w") as f:
path = os.path.expanduser(path)
_safe_create_file(path)
with open(path, "w") as f:
f.write(contents)
return f'Successfully wrote "{filename}"'
return f'Successfully wrote "{path}"'


def write_lines(
filename: str, contents: str, insert_line: int = -1, mode: str = "insert"
path: str, contents: str, insert_line: int = -1, mode: str = "insert"
) -> str:
"""Writes content to a specific line in the file.
Args:
filename (str): The name of the file to write to.
path (str): The name of the file to write to.
contents (str): The content to write to the file.
insert_line (int, optional): The line number to insert the content at.
Negative values count from the end of the file. Defaults to -1.
Expand All @@ -39,8 +40,9 @@ def write_lines(
Returns:
str: A message indicating whether the write was successful.
"""
_safe_create_file(filename)
with open(filename, "r") as f:
path = os.path.expanduser(path)
_safe_create_file(path)
with open(path, "r") as f:
lines = f.readlines()
if insert_line < 0:
insert_line = len(lines) + insert_line + 1
Expand All @@ -52,20 +54,22 @@ def write_lines(
)
else:
raise ValueError(f"Invalid mode: {mode}")
with open(filename, "w") as f:
with open(path, "w") as f:
f.writelines(lines)
return f'Successfully wrote to "{filename}"'
return f'Successfully wrote to "{path}"'


def read(filename: str) -> str:
def read(path: str) -> str:
"""Reads a file and returns the contents"""
with open(filename, "r") as f:
path = os.path.expanduser(path)
with open(path, "r") as f:
return f.read()


def read_lines(filename: str, start_line: int = 0, end_line: int = -1) -> str:
def read_lines(path: str, start_line: int = 0, end_line: int = -1) -> str:
"""Reads a partial file and returns the contents"""
with open(filename, "r") as f:
path = os.path.expanduser(path)
with open(path, "r") as f:
lines = f.readlines()
if start_line < 0:
start_line = len(lines) + start_line
Expand All @@ -76,13 +80,16 @@ def read_lines(filename: str, start_line: int = 0, end_line: int = -1) -> str:

def mkdir(path: str) -> str:
"""Creates a directory (and any parent directories))"""
path = os.path.expanduser(path)
path = pathlib.Path(path)
path.mkdir(parents=True, exist_ok=True)
return f'Successfully created directory "{path}"'


def mv(src: str, dest: str) -> str:
"""Moves a file or directory"""
src = os.path.expanduser(src)
dest = os.path.expanduser(dest)
src = pathlib.Path(src)
dest = pathlib.Path(dest)
src.rename(dest)
Expand All @@ -91,6 +98,8 @@ def mv(src: str, dest: str) -> str:

def cp(src: str, dest: str) -> str:
"""Copies a file or directory"""
src = os.path.expanduser(src)
dest = os.path.expanduser(dest)
src = pathlib.Path(src)
dest = pathlib.Path(dest)
shutil.copytree(src, dest)
Expand All @@ -99,5 +108,6 @@ def cp(src: str, dest: str) -> str:

def ls(path: str) -> str:
"""Lists the contents of a directory"""
path = os.path.expanduser(path)
path = pathlib.Path(path)
return "\n".join(str(p) for p in path.iterdir())

0 comments on commit 90f8d1f

Please sign in to comment.