diff --git a/crates/ledger-cli/src/main.rs b/crates/ledger-cli/src/main.rs index 326d5e12..ba57781c 100644 --- a/crates/ledger-cli/src/main.rs +++ b/crates/ledger-cli/src/main.rs @@ -155,8 +155,12 @@ impl ledger_gui::owner::Owner for ViewerOwner { self.server.revoke_token(book, name) } - fn bridge_command(&self, book: &str) -> Result { - ledger_host::commands::bridge_command_line(&self.common, book) + fn command_line(&self, words: &[&str]) -> Result { + ledger_host::commands::command_line(&self.common, words) + } + + fn make_key(&self, book: &str) -> Result { + ledger_host::commands::ensure_key(&self.common, book) } fn close_books(&self) -> Result { diff --git a/crates/ledger-gui/src/owner.rs b/crates/ledger-gui/src/owner.rs index 9e2bc318..4c98e612 100644 --- a/crates/ledger-gui/src/owner.rs +++ b/crates/ledger-gui/src/owner.rs @@ -267,10 +267,16 @@ pub trait Owner: Send + Sync { fn serve_revoke(&self, _book: &str, _name: &str) -> Result { Err("not here".into()) } - /// The command an assistant runs to reach the company `book`, as text - /// to type (the program and `mcp --book ID`, quoted for a terminal): - /// what the Connect page shows for an assistant it has no button for. - fn bridge_command(&self, _book: &str) -> Result { + /// This program run with `words`, as a line to type in a terminal (the + /// program's own path, the words, the data root if one was named): what + /// the Connect page shows for an assistant it has no button for. + fn command_line(&self, _words: &[&str]) -> Result { + Err("not here".into()) + } + /// The key an assistant needs to reach the company `book` exists (made + /// now if it did not): `Ok(true)` when it was made now. What `connect` + /// does first, for an assistant set up by hand. Blocks. + fn make_key(&self, _book: &str) -> Result { Err("not here".into()) } /// Close the books: everything holding them lets go. diff --git a/crates/ledger-host/src/commands.rs b/crates/ledger-host/src/commands.rs index 20189011..278055eb 100644 --- a/crates/ledger-host/src/commands.rs +++ b/crates/ledger-host/src/commands.rs @@ -1402,42 +1402,43 @@ pub fn connect( } } -/// The command an assistant runs to reach the books `book`: this program's -/// own executable and `mcp --book `, with `--root` when the data root -/// was named and the knobs this run was given. The one place it is made: -/// `connect_agent` registers it, and the window's Connect page shows it to -/// be typed into an assistant that has no `connect` of its own. -pub fn bridge_serve_args(common: &Common, book: &str) -> Result, String> { - let book = ledger_store::root::BookId::parse(book) - .map_err(|e| e.to_string())? - .to_string(); +/// This program's own executable followed by `words`, then `--root` when +/// the data root was named and the knobs this run was given: what an +/// assistant, or a person at a terminal, runs to reach these books. The one +/// place it is made. +fn program_args(common: &Common, words: &[&str]) -> Result, String> { // A translocated macOS app's path (P11a step 6) is not a place to point // an agent: the copy goes away with the app. let exe = crate::start::this_executable()?; if let Some(why) = crate::start::translocation_refusal(&exe) { return Err(why.to_string()); } - let mut serve: Vec = vec![ - exe.into_os_string(), - "mcp".into(), - "--book".into(), - book.into(), - ]; + let mut args: Vec = vec![exe.into_os_string()]; + args.extend(words.iter().map(std::ffi::OsString::from)); if common.root.is_some() { let found = ledger_store::root::Root::find(common.root.as_deref()).map_err(|e| e.to_string())?; - serve.push("--root".into()); - serve.push(found.path().as_os_str().to_os_string()); + args.push("--root".into()); + args.push(found.path().as_os_str().to_os_string()); } - serve.extend(common.knobs.args().into_iter().map(Into::into)); - Ok(serve) + args.extend(common.knobs.args().into_iter().map(Into::into)); + Ok(args) } -/// [`bridge_serve_args`] as text, for an assistant that is set up by hand: -/// a part with a space in it is quoted for a terminal. -pub fn bridge_command_line(common: &Common, book: &str) -> Result { - let args = bridge_serve_args(common, book)?; - Ok(args +/// The command an assistant runs to reach the books `book`: this program +/// and `mcp --book `. `connect_agent` registers it, and the window's +/// Connect page shows it for an assistant that has no `connect` of its own. +pub fn bridge_serve_args(common: &Common, book: &str) -> Result, String> { + let book = ledger_store::root::BookId::parse(book) + .map_err(|e| e.to_string())? + .to_string(); + program_args(common, &["mcp", "--book", &book]) +} + +/// This program run with `words`, as a line to type in a terminal: a part +/// with a space in it is quoted. +pub fn command_line(common: &Common, words: &[&str]) -> Result { + Ok(program_args(common, words)? .iter() .map(|a| { let a = a.to_string_lossy(); @@ -1451,6 +1452,28 @@ pub fn bridge_command_line(common: &Common, book: &str) -> Result Result { + let book = ledger_store::root::BookId::parse(book) + .map_err(|e| e.to_string())? + .to_string(); + let found = + ledger_store::root::Root::find(common.root.as_deref()).map_err(|e| e.to_string())?; + let run = found.run_dir().map_err(|e| e.to_string())?; + let account = crate::tokens::Account::new(found.path(), &book, crate::tokens::DEFAULT_NAME); + if crate::tokens::keeper(&run).get(&account)?.is_some() { + return Ok(false); + } + let scopes = ledger_store::auth::Scope::ALL + .iter() + .map(|s| s.as_str().to_string()) + .collect(); + make_token(common, &book, crate::tokens::DEFAULT_NAME, scopes, true)?; + Ok(true) +} + /// A revoked token's copy in the token file goes with it (what `token /// revoke` does after the server has ended the token). `run_dir` is the /// root's `run/` folder. @@ -1483,16 +1506,7 @@ pub fn connect_agent(common: &Common, agent: &str, book: &str) -> Result