Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
27 changes: 17 additions & 10 deletions n00n-lua/src/api/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,22 +141,23 @@ async fn new(
///
/// @param text string The prompt to send. Must not be blank.
/// @param opts table? Optional fields: session (string) id of a live
/// session; defaults to the focused one.
/// session (defaults to the focused one); steer (boolean) request
/// delivery as a steering interrupt when the session is busy.
/// @return (string|nil, string|nil) "started" or "queued", or nil and an error.
/// @example
/// local state, err = n00n.session.prompt("run the tests", { session = id })
/// local state, err = n00n.session.prompt("run the tests", { session = id, steer = true })
#[lua_fn]
async fn prompt(
lua: Lua,
#[ctx] tx: Option<flume::Sender<UiAction>>,
text: String,
opts: Option<Table>,
) -> LuaResult<Pair> {
let id = match opts {
Some(opts) => opts.get("session")?,
None => None,
let (id, steer) = match opts {
Some(opts) => (opts.get("session")?, opts.get("steer")?),
None => (None, false),
};
roundtrip(lua, tx, SessionRequest::Prompt { id, text }).await
roundtrip(lua, tx, SessionRequest::Prompt { id, text, steer }).await
}

/// Cancels the current turn in a live session without deleting the session.
Expand Down Expand Up @@ -290,22 +291,28 @@ mod tests {
assert!(val);
}

#[test_case("return session.prompt('hi', { session = 'abc' })", Some("abc") ; "explicit_session_id")]
#[test_case("return session.prompt('hi')", None ; "defaults_to_focused")]
fn prompt_forwards_text_and_session_id(code: &str, expected_id: Option<&str>) {
#[test_case("return session.prompt('hi', { session = 'abc' })", Some("abc"), false ; "explicit_session_id")]
#[test_case("return session.prompt('hi')", None, false ; "defaults_to_focused")]
#[test_case("return session.prompt('hi', { session = 'abc', steer = true })", Some("abc"), true ; "explicit_session_id_steer")]
fn prompt_forwards_text_and_session_id(
code: &str,
expected_id: Option<&str>,
expected_steer: bool,
) {
let (tx, rx) = flume::unbounded::<UiAction>();
let lua = lua_with_session(Some(tx));
let expected_id = expected_id.map(str::to_owned);
let checker = std::thread::spawn(move || {
let Ok(UiAction::Session {
req: SessionRequest::Prompt { id, text },
req: SessionRequest::Prompt { id, text, steer },
reply_tx,
}) = rx.recv()
else {
panic!("expected prompt request");
};
assert_eq!(id, expected_id);
assert_eq!(text, "hi");
assert_eq!(steer, expected_steer);
reply_tx.send(Ok(json!("queued"))).unwrap();
});
let (val, err): (String, Option<String>) =
Expand Down
32 changes: 25 additions & 7 deletions n00n-lua/src/api/util/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,14 +406,32 @@ pub enum WinCommand {
pub enum SessionRequest {
List,
Live,
Status { id: String },
Status {
id: String,
},
Current,
New { prompt: Option<String>, focus: bool },
Prompt { id: Option<String>, text: String },
Cancel { id: String },
Focus { id: String },
Delete { id: String },
SetTitle { id: String, title: String },
New {
prompt: Option<String>,
focus: bool,
},
Prompt {
id: Option<String>,
text: String,
steer: bool,
},
Cancel {
id: String,
},
Focus {
id: String,
},
Delete {
id: String,
},
SetTitle {
id: String,
title: String,
},
}

pub type SessionReply = Result<serde_json::Value, String>;
Expand Down
2 changes: 1 addition & 1 deletion n00n-lua/tests/plugin_host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4755,7 +4755,7 @@ fn team_launcher_collects_goal_and_submits_configured_prompt() {
let n00n_lua::UiAction::Session { req, reply_tx } = action else {
panic!("expected Team session prompt");
};
let n00n_lua::SessionRequest::Prompt { id, text } = req else {
let n00n_lua::SessionRequest::Prompt { id, text, .. } = req else {
panic!("expected a prompt request");
};
assert!(id.is_none());
Expand Down
18 changes: 18 additions & 0 deletions n00n-ui/src/app/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,24 @@ impl App {
}
}

/// Control/steering path: interrupt an in-progress turn when possible,
/// otherwise start a new turn. Used by `agent_control` message/resume.
pub(crate) fn submit_control_prompt(&mut self, msg: QueuedMessage) -> SubmitOutcome {
if msg.text.trim().is_empty() && msg.images.is_empty() {
return SubmitOutcome::Rejected(EMPTY_PROMPT_ERR);
}
if self.status == Status::Streaming {
if self.queue_with_delivery(msg, Delivery::Steering) {
Comment thread
w0wl0lxd marked this conversation as resolved.
Resolved
SubmitOutcome::Queued
} else {
SubmitOutcome::Rejected(NO_QUEUE_ERR)
}
} else {
self.run_id += 1;
SubmitOutcome::Started(self.start_background_submission(&msg))
}
}

/// Keyboard path: nobody is around to receive an `Err`, so
/// rejections flash on screen instead.
/// Session API prompts do not have a user-visible optimistic bubble, so
Expand Down
15 changes: 10 additions & 5 deletions n00n-ui/src/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -873,22 +873,22 @@ impl<'t> EventLoop<'t> {
let idx = self.push_runtime(self.ctx.spawn_runtime(session));
let id = self.sessions[idx].id();
if let Some(prompt) = prompt {
let _ = self.submit_text(idx, prompt);
let _ = self.submit_text(idx, prompt, false);
}
if focus {
self.set_focus(idx);
}
let _ = reply_tx.send(Ok(json!(id)));
}
SessionRequest::Prompt { id, text } => {
SessionRequest::Prompt { id, text, steer } => {
let idx = match id {
None => Ok(self.focused),
Some(id) => parse_session_id(&id).and_then(|id| {
self.position(id)
.ok_or_else(|| format!("{NOT_LIVE_ERR}: {id}"))
}),
};
let _ = reply_tx.send(idx.and_then(|idx| self.submit_text(idx, text)));
let _ = reply_tx.send(idx.and_then(|idx| self.submit_text(idx, text, steer)));
}
SessionRequest::Cancel { id } => {
let reply = parse_session_id(&id).and_then(|id| {
Expand Down Expand Up @@ -932,12 +932,17 @@ impl<'t> EventLoop<'t> {
}
}

fn submit_text(&mut self, idx: usize, text: String) -> SessionReply {
fn submit_text(&mut self, idx: usize, text: String, steer: bool) -> SessionReply {
let msg = QueuedMessage {
text,
images: Vec::new(),
};
match self.sessions[idx].app.submit_background_prompt(msg) {
let outcome = if steer {
self.sessions[idx].app.submit_control_prompt(msg)
} else {
self.sessions[idx].app.submit_background_prompt(msg)
};
match outcome {
SubmitOutcome::Started(actions) => {
self.dispatch(idx, actions);
Ok(json!("started"))
Expand Down
2 changes: 1 addition & 1 deletion plugins/agent_control/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ local function handler(input)
end
end

local state, err = n00n.session.prompt(input.message, { session = input.agent_id })
local state, err = n00n.session.prompt(input.message, { session = input.agent_id, steer = true })
if not state then
return { llm_output = err, is_error = true }
end
Expand Down
Loading