#include "SessionPanel.hpp" #include "BoxLayout.hpp" #include using namespace tp; using namespace tp::taskboard; using namespace tp::ui; namespace { constexpr halnf kGap = 6; constexpr halnf kSectionGap = 14; constexpr halnf kSettingLabelWidth = 64; constexpr const char* kSummarySeparator = " \u00b7 "; std::vector optionIds(const std::vector& options) { std::vector ids; for (const AgentOption& option : options) ids.push_back(option.id); return ids; } std::vector optionLabels(const std::vector& options) { std::vector labels; for (const AgentOption& option : options) labels.push_back(option.label); return labels; } Widget& rowLayout(Widget& widget, halnf gap) { widget.setSizing(AxisSizing::fill(), AxisSizing::fit()); widget.emplaceLayout(gap).setCrossAlign(CrossAlign::Center); return widget; } Widget& columnLayout(Widget& widget, halnf gap) { widget.setSizing(AxisSizing::fill(), AxisSizing::fit()); widget.emplaceLayout(gap); return widget; } void settingLabel(Label& label) { label.setTone(Label::Tone::Muted); label.setSizing(AxisSizing::fixed(kSettingLabelWidth), AxisSizing::fit()); } } SubrouterStatus::SubrouterStatus() { setDebugName("subrouter status"); columnLayout(*this, kGap); for (Label* label : { &hint, &accounts }) { label->setWrap(true); label->setSizing(AxisSizing::fill(), AxisSizing::fit()); addChild(*label); } hint.setTone(Label::Tone::Muted); SubrouterInfo empty; empty.loginHint = "Run 'subrouter login anthropic' to add an account."; setInfo(empty); } void SubrouterStatus::setInfo(const SubrouterInfo& info) { if (!info.available) { hint.setText("Subrouter is not available on this machine."); hint.setVisible(true); accounts.setVisible(false); return; } hint.setText(info.loginHint); hint.setVisible(!info.loginHint.empty() && info.accounts.empty()); accounts.setVisible(!info.accounts.empty()); if (info.accounts.empty()) return; std::string text = "Subscription accounts:"; for (const SubrouterAccount& account : info.accounts) { text += std::string("\n") + (account.coolingDown ? "[!] " : "[ok] ") + account.email; if (!account.coolingDown) continue; text += " rate limited"; if (!account.cooldownUntil.empty()) text += " until " + account.cooldownUntil; } accounts.setText(text); } SessionPresetPicker::SessionPresetPicker(SessionPresets& presets) : mPresets(presets) { setDebugName("session preset picker"); buildLayout(); bindControls(); refreshOptions(); } void SessionPresetPicker::buildLayout() { settingLabel(mLabel); preset.setSizing(AxisSizing::fill(), AxisSizing::fit()); remove.setStyle(Button::Style::Flat); remove.setTooltip("Delete this preset"); rowLayout(mPresetRow, kGap); for (Widget* widget : std::initializer_list{ &mLabel, &preset, &saveAs, &remove }) mPresetRow.addChild(*widget); name.setPlaceholder("Preset name, e.g. smart or dumb agent"); name.setSizing(AxisSizing::fill(), AxisSizing::fit()); save.setStyle(Button::Style::Accent); rowLayout(mNamingRow, kGap); for (Widget* widget : std::initializer_list{ &name, &save, &cancel }) mNamingRow.addChild(*widget); mNamingRow.setVisible(false); problem.setTone(Label::Tone::Warning); problem.setWrap(true); problem.setSizing(AxisSizing::fill(), AxisSizing::fit()); problem.setVisible(false); columnLayout(*this, kGap); addChild(mPresetRow); addChild(mNamingRow); addChild(problem); } void SessionPresetPicker::bindControls() { preset.setOnSelected([this](long option) { presetChosen(option); }); saveAs.setOnActivate([this] { openNaming(); }); remove.setOnActivate([this] { removeSelected(); }); save.setOnActivate([this] { commitName(); }); cancel.setOnActivate([this] { closeNaming(); }); name.setOnCommitted([this](const std::string&) { if (name.isEditing()) commitName(); }); } void SessionPresetPicker::refreshOptions() { std::vector options = { kCustomOption }; for (const SessionPreset& entry : mPresets.all()) options.push_back(entry.name); const bool wasLoading = mLoading; mLoading = true; preset.setOptions(std::move(options)); preset.setSelectedIndex(mShownPreset ? long(*mShownPreset) + 1 : 0); mLoading = wasLoading; remove.setEnabled(mShownPreset.has_value()); } void SessionPresetPicker::showSettings(const LaunchSettings& settings) { mShownPreset = mPresets.matching(settings); refreshOptions(); } std::optional SessionPresetPicker::selectedPreset() const { const long option = preset.selectedIndex(); if (option <= 0) return std::nullopt; return size_t(option - 1); } void SessionPresetPicker::presetChosen(long option) { if (mLoading) return; const std::optional chosen = option > 0 ? std::optional(size_t(option - 1)) : std::nullopt; mShownPreset = chosen; remove.setEnabled(chosen.has_value()); if (chosen && *chosen < mPresets.all().size() && mOnApplied) mOnApplied(mPresets.all()[*chosen].settings); } void SessionPresetPicker::openNaming() { const std::optional shown = selectedPreset(); name.setText(shown ? mPresets.all()[*shown].name : std::string()); problem.setVisible(false); mNamingRow.setVisible(true); } void SessionPresetPicker::closeNaming() { mNamingRow.setVisible(false); name.setText(""); } void SessionPresetPicker::commitName() { const std::string chosen = name.text(); if (chosen.empty() || !mCurrent) return; mShownPreset = mPresets.save(chosen, mCurrent()); closeNaming(); refreshOptions(); problem.setText("Couldn't write presets to " + mPresets.storagePath()); problem.setVisible(mPresets.lastSaveFailed()); } void SessionPresetPicker::removeSelected() { const std::optional shown = selectedPreset(); if (!shown) return; mPresets.remove(*shown); mShownPreset.reset(); refreshOptions(); } SessionTab::SessionTab(TaskBoard& board, SessionPresets& presetStore) : presets(presetStore), mBoard(board) { setDebugName("session tab"); buildLayout(); bindControls(); setAgentOptions(mOptions); } void SessionTab::buildLayout() { state.setWrap(true); state.setSizing(AxisSizing::fill(), AxisSizing::fit()); buildParameters(); startSession.setStyle(Button::Style::Accent); rowLayout(mControls, kGap); for (Widget* widget : std::initializer_list{ &startSession, &stopSession, &openSession }) mControls.addChild(*widget); restartHint.setTone(Label::Tone::Warning); restartHint.setWrap(true); restartHint.setSizing(AxisSizing::fill(), AxisSizing::fit()); rowLayout(mRestartRow, kGap); mRestartRow.addChild(restartKeep); mRestartRow.addChild(restartFresh); columnLayout(*this, kSectionGap); for (Widget* widget : std::initializer_list{ &state, &presets, ¶meters, &mControls, &restartHint, &mRestartRow, &subrouter }) addChild(*widget); } void SessionTab::buildParameters() { parameters.setSizing(AxisSizing::fill(), AxisSizing::fit()); parameters.body().emplaceLayout(kGap); parameters.setExpanded(false, false); const std::pair settings[] = { { "Model", &model }, { "Harness", &harness }, { "Effort", &effort }, { "Mode", &permissionMode } }; for (const auto& [text, dropdown] : settings) { mSettingRows.push_back(std::make_unique()); Widget& row = *mSettingRows.back(); rowLayout(row, kGap); mSettingLabels.push_back(std::make_unique