Skip to content

Commit

Permalink
Merge pull request #78 from darrenburns/8sep-improvements
Browse files Browse the repository at this point in the history
8sep improvements
  • Loading branch information
darrenburns authored Sep 8, 2024
2 parents c8ade45 + 7c3864f commit 61ef601
Show file tree
Hide file tree
Showing 11 changed files with 1,837 additions and 473 deletions.
22 changes: 8 additions & 14 deletions elia_chat/elia.scss
Original file line number Diff line number Diff line change
Expand Up @@ -264,22 +264,16 @@ Chatbox {
}

Footer {
background: $main-darken-1 0%;
}

Footer > .footer--highlight {
background: $main-lighten-2 20%;
}
background: transparent;

Footer > .footer--key {
text-style: none;
background: $main-darken-2 0%;
color: $main-border-text-color;
}
& FooterKey {
background: transparent;
}

Footer > .footer--highlight-key {
background: $main-lighten-2 30%;
color: greenyellow;
.footer-key--key {
color: greenyellow;
background: transparent;
}
}

OptionList {
Expand Down
1 change: 1 addition & 0 deletions elia_chat/screens/help_screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class HelpScreen(ModalScreen[None]):
It's present on both the home screen and the chat page.
- `ctrl+j`: Submit the prompt
- `alt+enter`: Submit the prompt (only works in some terminals)
- `up`: Move the cursor up
- `down`: Move the cursor down
- `left`: Move the cursor left
Expand Down
6 changes: 5 additions & 1 deletion elia_chat/screens/home_screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ class HomeScreen(Screen[None]):

BINDINGS = [
Binding(
"ctrl+j", "send_message", "Send message", priority=True, key_display="^j"
"ctrl+j,alt+enter",
"send_message",
"Send message",
priority=True,
key_display="^j",
),
Binding("o,ctrl+o", "options", "Options", key_display="^o"),
]
Expand Down
19 changes: 11 additions & 8 deletions elia_chat/widgets/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ async def new_user_message(self, content: str) -> None:

self.stream_agent_response()

@work
@work(thread=True, group="agent_response")
async def stream_agent_response(self) -> None:
model = self.chat_data.model
log.debug(f"Creating streaming response with model {model.name!r}")
Expand Down Expand Up @@ -195,13 +195,16 @@ async def stream_agent_response(self) -> None:
"role": "assistant",
}
now = datetime.datetime.now(datetime.timezone.utc)
message = ChatMessage(message=ai_message, model=model, timestamp=now)

message = ChatMessage(message=ai_message, model=model, timestamp=now)
response_chatbox = Chatbox(
message=message,
model=self.chat_data.model,
classes="response-in-progress",
)
self.post_message(self.AgentResponseStarted())
self.app.call_from_thread(self.chat_container.mount, response_chatbox)

assert (
self.chat_container is not None
), "Textual has mounted container at this point in the lifecycle."
Expand All @@ -212,20 +215,20 @@ async def stream_agent_response(self) -> None:
chunk = cast(ModelResponse, chunk)
response_chatbox.border_title = "Agent is responding..."

if chunk_count == 0:
self.post_message(self.AgentResponseStarted())
await self.chat_container.mount(response_chatbox)

chunk_content = chunk.choices[0].delta.content
if isinstance(chunk_content, str):
response_chatbox.append_chunk(chunk_content)
self.app.call_from_thread(
response_chatbox.append_chunk, chunk_content
)
else:
break

scroll_y = self.chat_container.scroll_y
max_scroll_y = self.chat_container.max_scroll_y
if scroll_y in range(max_scroll_y - 3, max_scroll_y + 1):
self.chat_container.scroll_end(animate=False)
self.app.call_from_thread(
self.chat_container.scroll_end, animate=False
)

chunk_count += 1
except Exception:
Expand Down
2 changes: 1 addition & 1 deletion elia_chat/widgets/chat_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def compose(self) -> ComposeResult:
selected_model = self.runtime_config.selected_model
models_rs.border_title = "Available Models"
for model in self.elia.launch_config.all_models:
label = f"[dim]{escape(model.display_name or model.name)}"
label = f"{escape(model.display_name or model.name)}"
provider = model.provider
if provider:
label += f" [i]by[/] {provider}"
Expand Down
68 changes: 67 additions & 1 deletion elia_chat/widgets/chatbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,47 @@ class VisualModeToggled(Message):
description="Toggle visual select",
key_display="v",
),
Binding("up,k", "cursor_up", "Cursor Up", show=False),
Binding("down,j", "cursor_down", "Cursor Down", show=False),
Binding("right,l", "cursor_right", "Cursor Right", show=False),
Binding("left,h", "cursor_left", "Cursor Left", show=False),
Binding("shift+up,K", "cursor_up(True)", "cursor up select", show=False),
Binding("shift+down,J", "cursor_down(True)", "cursor down select", show=False),
Binding("shift+left,H", "cursor_left(True)", "cursor left select", show=False),
Binding(
"y,c", "copy_to_clipboard", description="Copy selection", key_display="y"
"shift+right,L", "cursor_right(True)", "cursor right select", show=False
),
Binding("ctrl+left,b", "cursor_word_left", "cursor word left", show=False),
Binding("ctrl+right,w", "cursor_word_right", "cursor word right", show=False),
Binding(
"home,ctrl+a,0,^", "cursor_line_start", "cursor line start", show=False
),
Binding("end,ctrl+e,$", "cursor_line_end", "cursor line end", show=False),
Binding("pageup,ctrl+b", "cursor_page_up", "cursor page up", show=False),
Binding("pagedown,ctrl+f", "cursor_page_down", "cursor page down", show=False),
Binding("ctrl+d", "cursor_half_page_down", "cursor half page down", show=False),
Binding("ctrl+u", "cursor_half_page_up", "cursor half page up", show=False),
Binding(
"ctrl+shift+left,B",
"cursor_word_left(True)",
"cursor left word select",
show=False,
),
Binding(
"ctrl+shift+right,W",
"cursor_word_right(True)",
"cursor right word select",
show=False,
),
Binding("f6,V", "select_line", "select line", show=False),
Binding(
"y,c",
"copy_to_clipboard",
description="Copy selection",
show=False,
),
Binding("g", "cursor_top", "Go to top", show=False),
Binding("G", "cursor_bottom", "Go to bottom", show=False),
Binding("u", "next_code_block", description="Next code block", key_display="u"),
]

Expand Down Expand Up @@ -88,6 +126,12 @@ def action_cursor_word_left(self, select: bool = False) -> None:
def action_cursor_word_right(self, select: bool = False) -> None:
return super().action_cursor_word_right(self.visual_mode or select)

def action_cursor_top(self) -> None:
self.selection = Selection.cursor((0, 0))

def action_cursor_bottom(self) -> None:
self.selection = Selection.cursor((self.document.line_count - 1, 0))

def action_copy_to_clipboard(self) -> None:
text_to_copy = self.selected_text

Expand Down Expand Up @@ -148,6 +192,28 @@ def action_next_code_block(self) -> None:
def action_leave_selection_mode(self) -> None:
self.post_message(self.LeaveSelectionMode())

def action_cursor_half_page_down(self) -> None:
"""Move the cursor and scroll down half of a page."""
half_height = self.content_size.height // 2
_, cursor_location = self.selection
target = self.navigator.get_location_at_y_offset(
cursor_location,
half_height,
)
self.scroll_relative(y=half_height, animate=False)
self.move_cursor(target)

def action_cursor_half_page_up(self) -> None:
"""Move the cursor and scroll down half of a page."""
half_height = self.content_size.height // 2
_, cursor_location = self.selection
target = self.navigator.get_location_at_y_offset(
cursor_location,
-half_height,
)
self.scroll_relative(y=-half_height, animate=False)
self.move_cursor(target)


class Chatbox(Widget, can_focus=True):
BINDINGS = [
Expand Down
6 changes: 5 additions & 1 deletion elia_chat/widgets/prompt_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ class CursorEscapingTop(Message):
class CursorEscapingBottom(Message):
pass

BINDINGS = [Binding("ctrl+j", "submit_prompt", "Send message", key_display="^j")]
BINDINGS = [
Binding("ctrl+j,alt+enter", "submit_prompt", "Send message", key_display="^j")
]

def __init__(
self,
Expand Down Expand Up @@ -67,3 +69,5 @@ def action_submit_prompt(self) -> None:
message = self.PromptSubmitted(self.text, prompt_input=self)
self.clear()
self.post_message(message)
else:
self.notify("Cannot send empty message!")
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ authors = [
{ name = "Darren Burns", email = "[email protected]" }
]
dependencies = [
"textual[syntax]==0.62",
"textual[syntax]==0.79.1",
"sqlmodel>=0.0.9",
"humanize>=4.6.0",
"click>=8.1.6",
Expand All @@ -28,7 +28,7 @@ elia = "elia_chat.__main__:cli"
requires = ["hatchling"]
build-backend = "hatchling.build"

[tool.rye]
[tool.uv]
managed = true
dev-dependencies = [
"black>=23.3.0",
Expand Down
Loading

0 comments on commit 61ef601

Please sign in to comment.