Skip to content
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
17 changes: 14 additions & 3 deletions include/agentty/runtime/msg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ namespace agentty {
// ── Composer ─────────────────────────────────────────────────────────────
struct ComposerCharInput { char32_t ch; };
struct ComposerBackspace {};
// Forward Delete: removes the code point (or whole attachment chip) after
// the caret; the caret stays put.
struct ComposerDeleteForward {};
struct ComposerEnter {};
struct ComposerNewline {};
struct ComposerSubmit {};
Expand Down Expand Up @@ -988,9 +991,12 @@ struct AccountRemove {};
struct LoginPickMethod { char32_t key; }; // '1' = ApiKey, '2' = OAuth, '3' = ChatGPT
struct LoginCharInput { char32_t ch; };
struct LoginBackspace {};
struct LoginDelete {};
struct LoginPaste { std::string text; };
struct LoginCursorLeft {};
struct LoginCursorRight {};
struct LoginCursorHome {};
struct LoginCursorEnd {};
struct LoginSubmit {};
// User pressed the "copy URL to clipboard" key while the OAuthCode
// modal is up. The reducer issues a cmd::write_clipboard with
Expand Down Expand Up @@ -1225,6 +1231,9 @@ struct PluginEdited {
bool parse_error = false;
};
struct SettingsListBackspace {};
// Add prompt caret editing: forward Delete, and caret moves.
struct SettingsListDelete {};
struct SettingsListCursor { enum class To { Left, Right, Home, End }; To to; };
// `p` on a plugin's tool row: pin it (always loaded) or unpin it (#98).
struct SettingsListPin {};
// → / ← on Plugins: show or hide a server's tools.
Expand Down Expand Up @@ -1435,7 +1444,7 @@ struct GitSignalsRefreshed {};
namespace msg {

using ComposerMsg = std::variant<
ComposerCharInput, ComposerBackspace, ComposerEnter, ComposerNewline,
ComposerCharInput, ComposerBackspace, ComposerDeleteForward, ComposerEnter, ComposerNewline,
ComposerSubmit, ComposerToggleExpand, ComposerToggleLoop,
ComposerCursorLeft, ComposerCursorRight, ComposerCursorHome, ComposerCursorEnd,
ComposerSlashMove, ComposerSlashComplete, ComposerSlashDismiss, SlashPromptRendered,
Expand Down Expand Up @@ -1546,6 +1555,7 @@ using SettingsListMsg = std::variant<
SettingsListEditOpen,
SettingsListChar, SettingsListPaste, PluginsUpdated, PluginEdited,
SettingsListBackspace, SettingsListSubmitInput, SettingsListCancelInput,
SettingsListDelete, SettingsListCursor,
SettingsAddDone>;

using ForkMsg = std::variant<
Expand All @@ -1557,8 +1567,9 @@ using TodoMsg = std::variant<
using LoginMsg = std::variant<
OpenLogin, CloseLogin, LoginBack, SignOut,
OpenAccounts, AccountMove, AccountSelect, AccountRemove,
LoginPickMethod, LoginCharInput, LoginBackspace,
LoginPaste, LoginCursorLeft, LoginCursorRight, LoginSubmit,
LoginPickMethod, LoginCharInput, LoginBackspace, LoginDelete,
LoginPaste, LoginCursorLeft, LoginCursorRight,
LoginCursorHome, LoginCursorEnd, LoginSubmit,
LoginCopyAuthUrl, LoginCopyCode, LoginOpenBrowserAgain,
LoginExchanged, LoginOAuthMinted, CodexDeviceCodeReady, CodexLoginDone,
DeviceCodeReady, DeviceLoginDone, TokenRefreshed, OAuthRefreshNotDue,
Expand Down
17 changes: 17 additions & 0 deletions src/runtime/app/subscribe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,19 @@ std::optional<Msg> on_settings_list(const KeyEvent& ev, bool input_active,
s.select = [] { return Msg{SettingsListSubmitInput{}}; };
s.filter_bs = [] { return Msg{SettingsListBackspace{}}; };
s.filter_ch = [](char32_t c) { return Msg{SettingsListChar{c}}; };
s.extra = [](const KeyEvent& e) -> std::optional<Msg> {
const auto* sk = std::get_if<SpecialKey>(&e.key);
if (!sk) return std::nullopt;
using To = SettingsListCursor::To;
switch (*sk) {
case SpecialKey::Left: return Msg{SettingsListCursor{To::Left}};
case SpecialKey::Right: return Msg{SettingsListCursor{To::Right}};
case SpecialKey::Home: return Msg{SettingsListCursor{To::Home}};
case SpecialKey::End: return Msg{SettingsListCursor{To::End}};
case SpecialKey::Delete: return Msg{SettingsListDelete{}};
default: return std::nullopt;
}
};
return nav::translate(s, ev);
}
if (hub) {
Expand Down Expand Up @@ -962,8 +975,11 @@ std::optional<Msg> on_login(const ui::login::State& state, const KeyEvent& ev) {
switch (std::get<SpecialKey>(ev.key)) {
case SpecialKey::Enter: return LoginSubmit{};
case SpecialKey::Backspace: return LoginBackspace{};
case SpecialKey::Delete: return LoginDelete{};
case SpecialKey::Left: return LoginCursorLeft{};
case SpecialKey::Right: return LoginCursorRight{};
case SpecialKey::Home: return LoginCursorHome{};
case SpecialKey::End: return LoginCursorEnd{};
default: return std::nullopt;
}
}
Expand Down Expand Up @@ -1133,6 +1149,7 @@ std::optional<Msg> on_composer(ComposerKeyState s, const KeyEvent& ev) {
if (ev.mods.alt && s.text_empty && s.has_queued && !s.in_queue_peek)
return Msg{ComposerQueuePopLast{}};
return ComposerBackspace{};
case SpecialKey::Delete: return ComposerDeleteForward{};
case SpecialKey::Left:
// Ctrl+Left = jump-by-word. Mirrors readline / every
// text editor. Plain Left is per-character (chip-aware).
Expand Down
11 changes: 11 additions & 0 deletions src/runtime/app/update/composer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,17 @@ Cmd composer_update(Model& m, msg::ComposerMsg cm) {
}
return Cmd::none();
},
[&](ComposerDeleteForward) -> Cmd {
// Mirror of Backspace: chip_next spans a whole placeholder, so
// Delete in front of an attachment removes the chip in one key.
const int n = static_cast<int>(m.ui.composer.text.size());
if (m.ui.composer.cursor < n) {
begin_edit(m.ui.composer);
const int q = ui::chip_next(m.ui.composer.text, m.ui.composer.cursor);
m.ui.composer.text.erase(m.ui.composer.cursor, q - m.ui.composer.cursor);
}
return Cmd::none();
},
[&](ComposerEnter) { return submit_message(m); },
[&](ComposerSubmit) { return submit_message(m); },
[&](ComposerNewline) -> Cmd {
Expand Down
37 changes: 37 additions & 0 deletions src/runtime/app/update/login.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,40 @@ Cmd login_paste(Model& m, std::string text) {
return Cmd::none();
}

// Every editable login field: (text, caret). Lets Delete/Home/End share one
// body instead of repeating the three-state visit per key.
template <class F>
void with_login_field(Model& m, F&& f) {
std::visit(overload{
[&](login::OAuthCode& s) { f(s.code_input, s.cursor); },
[&](login::ApiKeyInput& s) { f(s.key_input, s.cursor); },
[&](login::CustomHostInput& s) { f(s.host_input, s.cursor); },
[](auto&) {},
}, m.ui.login);
}

Cmd login_delete(Model& m) {
with_login_field(m, [](std::string& text, int& cursor) {
if (cursor >= static_cast<int>(text.size())) return;
const int q = ui::utf8_next(text, cursor);
text.erase(static_cast<std::size_t>(cursor),
static_cast<std::size_t>(q - cursor));
});
return Cmd::none();
}

Cmd login_cursor_home(Model& m) {
with_login_field(m, [](std::string&, int& cursor) { cursor = 0; });
return Cmd::none();
}

Cmd login_cursor_end(Model& m) {
with_login_field(m, [](std::string& text, int& cursor) {
cursor = static_cast<int>(text.size());
});
return Cmd::none();
}

Cmd login_cursor_left(Model& m) {
std::visit(overload{
[](login::OAuthCode& s) {
Expand Down Expand Up @@ -1390,6 +1424,9 @@ Cmd login_update(Model& m, msg::LoginMsg lm) {
[&](LoginPaste& e) -> Cmd { return login_paste(m, std::move(e.text)); },
[&](LoginCursorLeft) -> Cmd { return login_cursor_left(m); },
[&](LoginCursorRight) -> Cmd { return login_cursor_right(m); },
[&](LoginDelete) -> Cmd { return login_delete(m); },
[&](LoginCursorHome) -> Cmd { return login_cursor_home(m); },
[&](LoginCursorEnd) -> Cmd { return login_cursor_end(m); },
[&](LoginSubmit) -> Cmd { return login_submit(m); },
[&](LoginCopyAuthUrl) -> Cmd { return login_copy_auth_url(m); },
[&](LoginCopyCode) -> Cmd { return login_copy_code(m); },
Expand Down
22 changes: 22 additions & 0 deletions src/runtime/app/update/settings_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,28 @@ Cmd settings_list_update(Model& m, msg::SettingsListMsg sm) {
o->cursor = p;
return Cmd::none();
},
[&](SettingsListDelete) -> Cmd {
auto* o = m.ui.panel.get<pn::SettingsList>();
if (!o || !o->input_active
|| o->cursor >= static_cast<int>(o->input.size()))
return Cmd::none();
const int q = ui::utf8_next(o->input, o->cursor);
o->input.erase(static_cast<std::size_t>(o->cursor),
static_cast<std::size_t>(q - o->cursor));
return Cmd::none();
},
[&](SettingsListCursor& e) -> Cmd {
auto* o = m.ui.panel.get<pn::SettingsList>();
if (!o || !o->input_active) return Cmd::none();
using To = SettingsListCursor::To;
switch (e.to) {
case To::Left: o->cursor = ui::utf8_prev(o->input, o->cursor); break;
case To::Right: o->cursor = ui::utf8_next(o->input, o->cursor); break;
case To::Home: o->cursor = 0; break;
case To::End: o->cursor = static_cast<int>(o->input.size()); break;
}
return Cmd::none();
},
[&](SettingsListCancelInput) -> Cmd {
auto* o = m.ui.panel.get<pn::SettingsList>();
if (!o) return Cmd::none();
Expand Down
78 changes: 78 additions & 0 deletions tests/key_routing_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include "agentty/runtime/app/update/internal.hpp"
#include "agentty/runtime/app/subscribe.hpp"
#include "agentty/runtime/panel/appearance.hpp"
#include "agentty/runtime/panel/settings/categories.hpp"
#include "agentty/runtime/composer_attachment.hpp"
#include "agentty/runtime/app/deps.hpp"

using namespace agentty;
Expand Down Expand Up @@ -190,3 +192,79 @@ TEST_CASE("routing: every way ^/ arrives opens the model picker") {
CHECK(m.ui.panel.get<pn::Models>() != nullptr);
}
}

// The caret-based text inputs must all answer Delete (forward erase) and
// Home/End, through the real router, not only the reducer.
TEST_CASE("routing: Delete in the main prompt erases forward, a chip whole") {
install_stub_deps();
Model m;
m.ui.composer.text = "caf\xc3\xa9!"; // 2-byte code point before '!'
m.ui.composer.cursor = 3;
REQUIRE(press(m, special(maya::SpecialKey::Delete)));
CHECK(m.ui.composer.text == "caf!");
CHECK(m.ui.composer.cursor == 3);
REQUIRE(press(m, special(maya::SpecialKey::End)));
REQUIRE(press(m, special(maya::SpecialKey::Delete))); // at end: no-op
CHECK(m.ui.composer.text == "caf!");

Model c;
Attachment att;
att.kind = Attachment::Kind::Paste;
att.body.set_bytes("x");
c.ui.composer.attachments.push_back(std::move(att));
c.ui.composer.text = "a" + attachment::make_placeholder(0) + "b";
c.ui.composer.cursor = 1;
REQUIRE(press(c, special(maya::SpecialKey::Delete)));
CHECK(c.ui.composer.text == "ab");
CHECK(c.ui.composer.cursor == 1);
}

TEST_CASE("routing: login text fields answer Delete, Home and End") {
install_stub_deps();
Model m;
m.ui.login = ui::login::ApiKeyInput{.key_input = "sk-\xc3\xa9x", .cursor = 0};
const auto field = [&] { return std::get<ui::login::ApiKeyInput>(m.ui.login); };
REQUIRE(press(m, special(maya::SpecialKey::End)));
CHECK(field().cursor == 6);
REQUIRE(press(m, special(maya::SpecialKey::Home)));
CHECK(field().cursor == 0);
REQUIRE(press(m, special(maya::SpecialKey::Right)));
REQUIRE(press(m, special(maya::SpecialKey::Right)));
REQUIRE(press(m, special(maya::SpecialKey::Right)));
REQUIRE(press(m, special(maya::SpecialKey::Delete))); // the whole e-acute
CHECK(field().key_input == "sk-x");
CHECK(field().cursor == 3);
REQUIRE(press(m, special(maya::SpecialKey::End)));
REQUIRE(press(m, special(maya::SpecialKey::Delete))); // at end: no-op
CHECK(field().key_input == "sk-x");
}

TEST_CASE("routing: the settings add prompt answers Delete, arrows, Home and End") {
install_stub_deps();
Model m = app::update(Model{}, Msg{OpenSettingsList{settings::Category::Plugins}}).first;
// `n` is the one-line add prompt (`a` on Plugins opens the add form).
maya::KeyEvent a;
a.key = maya::CharKey{U'n'};
REQUIRE(press(m, a));
const auto list = [&] { return m.ui.panel.get<pn::SettingsList>(); };
REQUIRE(list() != nullptr);
REQUIRE(list()->input_active);
for (char32_t c : std::u32string{U"abc"}) {
maya::KeyEvent ev;
ev.key = maya::CharKey{c};
REQUIRE(press(m, ev));
}
REQUIRE(press(m, special(maya::SpecialKey::Home)));
CHECK(list()->cursor == 0);
REQUIRE(press(m, special(maya::SpecialKey::Delete)));
CHECK(list()->input == "bc");
REQUIRE(press(m, special(maya::SpecialKey::Right)));
CHECK(list()->cursor == 1);
REQUIRE(press(m, special(maya::SpecialKey::Left)));
CHECK(list()->cursor == 0);
REQUIRE(press(m, special(maya::SpecialKey::End)));
CHECK(list()->cursor == 2);
REQUIRE(press(m, special(maya::SpecialKey::Delete))); // at end: no-op
CHECK(list()->input == "bc");
CHECK(list()->input_active);
}
Loading