diff --git a/llembas/lua/llembas/core.lua b/llembas/lua/llembas/core.lua index d553a0f82..71ee60002 100644 --- a/llembas/lua/llembas/core.lua +++ b/llembas/lua/llembas/core.lua @@ -851,6 +851,20 @@ function M.context_command(path) return '/terminal-context ' .. M.clean(path, false) end +-- What is written into a LLeMbas pane to ask a question: the context line +-- (when there is a file) and Enter, then the question as a bracketed paste +-- and Enter. One string, so that it is one write and nothing can overtake +-- anything: the terminal's own paste call is delivered later than a plain +-- write, and Enter would arrive before the text. The question is +-- M.question_text's result: no ESC is left in it to end the paste early. +function M.ask_bytes(question, context_path) + local out = '' + if context_path then + out = M.context_command(context_path) .. '\r' + end + return out .. '\27[200~' .. M.question_text(question) .. '\27[201~\r' +end + ---------------------------------------------------------------------------- -- The LLeMbas CLI diff --git a/llembas/lua/llembas/init.lua b/llembas/lua/llembas/init.lua index 36f8c54e5..344156493 100644 --- a/llembas/lua/llembas/init.lua +++ b/llembas/lua/llembas/init.lua @@ -216,9 +216,8 @@ local function send_when_ready(pane, kind, text) global('pending_text')[key] = text end -local function send_question(pane, question) - pane:send_paste(question) - pane:send_text '\r' +local function send_question(pane, question, context_path) + pane:send_text(core.ask_bytes(question, context_path)) end local function deliver_pending(pane) @@ -418,10 +417,7 @@ local function ask(window, pane, line, explicit_screen) local target = session_pane_of_tab(pane) if target then - if path then - target:send_text(core.context_command(path) .. '\r') - end - send_question(target, question) + send_question(target, question, path) return end diff --git a/llembas/tests/run.lua b/llembas/tests/run.lua index d8c661ed5..149771cb6 100644 --- a/llembas/tests/run.lua +++ b/llembas/tests/run.lua @@ -424,6 +424,9 @@ test('question_text and context_command', function() 'command' ) eq(core.context_command '/x\r/quit', '/terminal-context /x /quit', 'no line break can be smuggled in') + eq(core.ask_bytes('hi', nil), '\27[200~hi\27[201~\r', 'question alone') + eq(core.ask_bytes('a\nb', '/r/c.json'), '/terminal-context /r/c.json\r\27[200~a\nb\27[201~\r', 'context, then a two-line question') + eq(core.ask_bytes('x\27[201~\ry', nil), '\27[200~x[201~y\27[201~\r', 'the paste cannot be ended from inside') end) ---------------------------------------------------------------------------- @@ -621,12 +624,11 @@ test('ask: new session beside the pane, with a context file', function() w.emit('user-var-changed', win, new, 'lembas_state', 'thinking') eq(#new.sent, 0, 'not on another state') w.emit('user-var-changed', win, new, 'lembas_state', 'idle') - eq(#new.sent, 2, 'sent when idle') - eq(new.sent[1][1], 'paste', 'pasted') - eq(new.sent[1][2], 'why did it fail?', 'the question') - eq(new.sent[2][2], '\r', 'then Enter') + eq(#new.sent, 1, 'sent when idle, in one write') + eq(new.sent[1][1], 'text', 'written, not handed to the asynchronous paste') + eq(new.sent[1][2], '\27[200~why did it fail?\27[201~\r', 'the question as a bracketed paste, then Enter') w.emit('user-var-changed', win, new, 'lembas_state', 'idle') - eq(#new.sent, 2, 'only once') + eq(#new.sent, 1, 'only once') end) test('ask: into the LLeMbas pane the tab already has', function() @@ -641,11 +643,12 @@ test('ask: into the LLeMbas pane the tab already has', function() local files = context_files(dir) eq(#files, 1, 'context file') eq(#pane.splits, 0, 'no new pane') - eq(#agent.sent, 3, 'three sends') - eq(agent.sent[1][2], '/terminal-context ' .. files[1] .. '\r', 'context first') - eq(agent.sent[2][1], 'paste', 'question pasted') - eq(agent.sent[2][2], 'explain[201~ this', 'and cannot end the paste') - eq(agent.sent[3][2], '\r', 'Enter') + eq(#agent.sent, 1, 'one write') + eq( + agent.sent[1][2], + '/terminal-context ' .. files[1] .. '\r\27[200~explain[201~ this\27[201~\r', + 'context line, Enter, the question pasted (and unable to end the paste), Enter' + ) eq(agent.read_zones, nil, 'the LLeMbas pane itself is not read') end) @@ -657,7 +660,7 @@ test('ask: from inside the LLeMbas pane it is just typed', function() fake.tab { agent } w.emit('llembas-ask-line', fake.window(), agent, 'hello') eq(#context_files(dir), 0, 'no context') - eq(agent.sent[1][2], 'hello', 'typed') + eq(agent.sent[1][2], '\27[200~hello\27[201~\r', 'typed') eq(agent.read_zones, nil, 'not read') end)