fix(ui): slash command input now echoed as user message in chat (closes #840)
* fix(ui): echo slash command input as user message in chat (#840) Slash commands like /skills, /help, /status previously showed only the assistant response with no user message above it — the conversation appeared to start from nowhere. Fix: executeCommand() now returns {noEcho:bool} instead of true/false (returns null when no command matched). send() in messages.js pushes a user message bubble before returning when noEcho is false. Commands with noEcho:true are action-only and don't get echoed: /clear, /new, /stop, /retry, /undo, /voice, /model, /workspace, /theme, /usage, /reasoning. Commands without noEcho (get echoed): /help, /skills, /status, /title, /compress, /compact, /personality. 16 new tests in test_issue840_slash_echo.py. * fix(ui): push user message BEFORE running slash handler (ordering bug) The PR as originally written pushed the user message AFTER the slash command handler ran. That works correctly for async handlers (the assistant response lands later, after the user push) but breaks for sync handlers like cmdHelp which push their assistant response synchronously: S.messages = [assistant response, user "/help"] ← reverse order The chat would render the help content ABOVE the user's own "/help" input — not what the issue asked for. Fix: look up the command inline, push the user message first (for echo-worthy commands), then run the handler. If the handler opts out (returns false — e.g. /reasoning <level>), pop the user message back off so the normal send path can add it cleanly when forwarding to the agent. Renamed the flow so it's clear we're not calling executeCommand twice (my first attempt did that by accident). executeCommand() stays as a public API returning null or {noEcho:bool} — just isn't the only path send() uses now. Added 2 regression tests: - test_send_pushes_user_message_before_running_handler: asserts the user push appears before the handler invocation in source order. - test_send_rolls_back_user_push_on_handler_optout: asserts the S.messages.pop() for the opt-out case. Also tightened the existing `test_send_checks_noecho_flag` and `test_send_pushes_user_message_for_echo_commands` tests to look at the new `_cmd.noEcho` pattern inline (vs the original `cmdResult.noEcho`). Removed `test_send_uses_null_check_not_truthy` (obsoleted — the control flow no longer stores the executeCommand return in a variable). Full suite: 1767 passed, 0 failures. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix(ui): compress/compact noEcho + title/personality confirmation messages Applied Opus mentor review fixes: - compress and compact: add noEcho:true (S.messages reset internally causes user bubble to flicker/disappear without noEcho) - /title <name>: push assistant confirmation message after rename succeeds - /personality <name>: push assistant confirmation message after set succeeds - 4 new regression tests covering the above invariants --------- Co-authored-by: nesquena-hermes <nesquena-hermes@users.noreply.github.com> Co-authored-by: Nathan Esquenazi <nesquena@gmail.com> Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -3,24 +3,26 @@
|
||||
// (no round-trip to the agent) and shows feedback via toast or local message.
|
||||
|
||||
const COMMANDS=[
|
||||
// noEcho:true = action-only commands that don't produce a chat response.
|
||||
// Commands without noEcho get a user message echoed to the chat (#840).
|
||||
{name:'help', desc:t('cmd_help'), fn:cmdHelp},
|
||||
{name:'clear', desc:t('cmd_clear'), fn:cmdClear},
|
||||
{name:'compress', desc:t('cmd_compress'), fn:cmdCompress, arg:'[focus topic]'},
|
||||
{name:'compact', desc:t('cmd_compact_alias'), fn:cmdCompact},
|
||||
{name:'model', desc:t('cmd_model'), fn:cmdModel, arg:'model_name', subArgs:'models'},
|
||||
{name:'workspace', desc:t('cmd_workspace'), fn:cmdWorkspace, arg:'name'},
|
||||
{name:'new', desc:t('cmd_new'), fn:cmdNew},
|
||||
{name:'usage', desc:t('cmd_usage'), fn:cmdUsage},
|
||||
{name:'theme', desc:t('cmd_theme'), fn:cmdTheme, arg:'name'},
|
||||
{name:'clear', desc:t('cmd_clear'), fn:cmdClear, noEcho:true},
|
||||
{name:'compress', desc:t('cmd_compress'), fn:cmdCompress, arg:'[focus topic]', noEcho:true},
|
||||
{name:'compact', desc:t('cmd_compact_alias'), fn:cmdCompact, noEcho:true},
|
||||
{name:'model', desc:t('cmd_model'), fn:cmdModel, arg:'model_name', subArgs:'models', noEcho:true},
|
||||
{name:'workspace', desc:t('cmd_workspace'), fn:cmdWorkspace, arg:'name', noEcho:true},
|
||||
{name:'new', desc:t('cmd_new'), fn:cmdNew, noEcho:true},
|
||||
{name:'usage', desc:t('cmd_usage'), fn:cmdUsage, noEcho:true},
|
||||
{name:'theme', desc:t('cmd_theme'), fn:cmdTheme, arg:'name', noEcho:true},
|
||||
{name:'personality', desc:t('cmd_personality'), fn:cmdPersonality, arg:'name', subArgs:'personalities'},
|
||||
{name:'skills', desc:t('cmd_skills'), fn:cmdSkills, arg:'query'},
|
||||
{name:'stop', desc:t('cmd_stop'), fn:cmdStop},
|
||||
{name:'stop', desc:t('cmd_stop'), fn:cmdStop, noEcho:true},
|
||||
{name:'title', desc:t('cmd_title'), fn:cmdTitle, arg:'[title]'},
|
||||
{name:'retry', desc:t('cmd_retry'), fn:cmdRetry},
|
||||
{name:'undo', desc:t('cmd_undo'), fn:cmdUndo},
|
||||
{name:'retry', desc:t('cmd_retry'), fn:cmdRetry, noEcho:true},
|
||||
{name:'undo', desc:t('cmd_undo'), fn:cmdUndo, noEcho:true},
|
||||
{name:'status', desc:t('cmd_status'), fn:cmdStatus},
|
||||
{name:'voice', desc:t('cmd_voice'), fn:cmdVoice},
|
||||
{name:'reasoning', desc:t('cmd_reasoning'), fn:cmdReasoning, arg:'show|hide|none|minimal|low|medium|high|xhigh', subArgs:['show','hide','none','minimal','low','medium','high','xhigh']},
|
||||
{name:'voice', desc:t('cmd_voice'), fn:cmdVoice, noEcho:true},
|
||||
{name:'reasoning', desc:t('cmd_reasoning'), fn:cmdReasoning, arg:'show|hide|none|minimal|low|medium|high|xhigh', subArgs:['show','hide','none','minimal','low','medium','high','xhigh'], noEcho:true},
|
||||
];
|
||||
|
||||
const SLASH_SUBARG_SOURCES={
|
||||
@@ -38,13 +40,15 @@ function parseCommand(text){
|
||||
|
||||
function executeCommand(text){
|
||||
const parsed=parseCommand(text);
|
||||
if(!parsed)return false;
|
||||
if(!parsed)return null;
|
||||
const cmd=COMMANDS.find(c=>c.name===parsed.name);
|
||||
if(!cmd)return false;
|
||||
if(!cmd)return null;
|
||||
// A handler may return `false` to opt out of interception — e.g. /reasoning
|
||||
// with an effort level falls through so the agent's own handler sees it,
|
||||
// preserving the pre-existing pass-through behaviour for that subcommand.
|
||||
return cmd.fn(parsed.args) !== false;
|
||||
if(cmd.fn(parsed.args)===false)return null;
|
||||
// Return noEcho flag so send() knows whether to echo the command as a user message (#840).
|
||||
return {noEcho:!!cmd.noEcho};
|
||||
}
|
||||
|
||||
function getMatchingCommands(prefix){
|
||||
@@ -513,6 +517,8 @@ async function cmdPersonality(args){
|
||||
}
|
||||
try{
|
||||
const res=await api('/api/personality/set',{method:'POST',body:JSON.stringify({session_id:S.session.session_id,name})});
|
||||
S.messages.push({role:'assistant',content:t('personality_set')+`**${name}**`});
|
||||
renderMessages();
|
||||
showToast(t('personality_set')+name);
|
||||
}catch(e){showToast(t('failed_colon')+e.message);}
|
||||
}
|
||||
@@ -537,6 +543,8 @@ async function cmdTitle(args){
|
||||
if(typeof syncTopbar==='function')syncTopbar();
|
||||
if(typeof renderSessionList==='function')renderSessionList();
|
||||
showToast(`${t('title_set')} "${S.session.title}"`);
|
||||
S.messages.push({role:'assistant',content:`${t('title_set')} **${S.session.title}**`});
|
||||
renderMessages();
|
||||
}catch(e){showToast(t('failed_colon')+e.message);}
|
||||
}
|
||||
async function cmdRetry(){
|
||||
|
||||
@@ -16,9 +16,34 @@ async function send(){
|
||||
}
|
||||
return;
|
||||
}
|
||||
// Slash command intercept -- local commands handled without agent round-trip
|
||||
if(text.startsWith('/')&&!S.pendingFiles.length&&executeCommand(text)){
|
||||
$('msg').value='';autoResize();hideCmdDropdown();return;
|
||||
// Slash command intercept -- local commands handled without agent round-trip.
|
||||
// We push the user message BEFORE running the handler for echo-worthy
|
||||
// commands so chat order is correct: some handlers (e.g. cmdHelp) push
|
||||
// their assistant response synchronously. If we pushed AFTER, S.messages
|
||||
// would be [assistant, user] and the chat would show the response above
|
||||
// the user's own input — reverse chronological order (#840 ordering bug).
|
||||
if(text.startsWith('/')&&!S.pendingFiles.length){
|
||||
const _parsedCmd=parseCommand(text);
|
||||
const _cmd=_parsedCmd?COMMANDS.find(c=>c.name===_parsedCmd.name):null;
|
||||
if(_cmd){
|
||||
let _pushedUser=false;
|
||||
if(!_cmd.noEcho){
|
||||
if(!S.session){await newSession();await renderSessionList();}
|
||||
S.messages.push({role:'user',content:text,_ts:Date.now()/1000});
|
||||
_pushedUser=true;
|
||||
renderMessages();
|
||||
}
|
||||
// Run the handler directly (we already looked it up). If it returns
|
||||
// false it's opting out — e.g. /reasoning <level> falls through so the
|
||||
// agent sees the raw text. Roll back the echo push in that case so
|
||||
// the normal send path doesn't duplicate it.
|
||||
if(_cmd.fn(_parsedCmd.args)===false){
|
||||
if(_pushedUser){S.messages.pop();renderMessages();}
|
||||
// Fall through to normal send path
|
||||
} else {
|
||||
$('msg').value='';autoResize();hideCmdDropdown();return;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(!S.session){await newSession();await renderSessionList();}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user