use std::collections::HashMap;
use std::process::Command;
struct Agent {
name: &'static str,
bin: &'static str,
vars: &'static [&'static str],
}
const AGENTS: &[Agent] = &[
Agent {
name: "claude",
bin: "claude",
vars: &["ANTHROPIC_BASE_URL"],
},
Agent {
name: "codex",
bin: "codex",
vars: &["OPENAI_BASE_URL"],
},
Agent {
name: "openai",
bin: "",
vars: &["OPENAI_BASE_URL"],
},
];
fn fail(msg: &str) -> ! {
eprintln!("firstpass launch: {msg}");
std::process::exit(2)
}
#[must_use]
pub fn base_url() -> String {
url_for_bind(&std::env::var("FIRSTPASS_BIND").unwrap_or_else(|_| "127.0.0.1:8080".to_owned()))
}
fn url_for_bind(bind: &str) -> String {
let host = bind
.strip_prefix("0.0.0.0")
.map_or_else(|| bind.to_owned(), |port| format!("127.0.0.1{port}"));
format!("http://{host}")
}
fn split(args: &[String]) -> (Option<&str>, Vec<String>) {
let mut rest = args.iter();
let name = rest.next().map(String::as_str);
let passthrough: Vec<String> = rest
.skip_while(|a| a.as_str() == "--")
.map(Clone::clone)
.collect();
(name, passthrough)
}
fn env_for(agent: &Agent, url: &str) -> HashMap<String, String> {
agent
.vars
.iter()
.map(|v| ((*v).to_owned(), url.to_owned()))
.collect()
}
#[derive(Debug, PartialEq, Eq)]
enum Listener {
Firstpass,
Foreign,
None,
}
fn classify(status_ok: bool, body: Option<&serde_json::Value>) -> Listener {
if !status_ok {
return Listener::None;
}
match body.and_then(|b| b.get("service")).and_then(|s| s.as_str()) {
Some("firstpass") => Listener::Firstpass,
_ => Listener::Foreign,
}
}
async fn probe(url: &str) -> Listener {
let Ok(client) = reqwest::Client::builder()
.timeout(std::time::Duration::from_secs(2))
.build()
else {
return Listener::None;
};
let Ok(resp) = client.get(format!("{url}/healthz")).send().await else {
return Listener::None;
};
let ok = resp.status().is_success();
let body = resp.json::<serde_json::Value>().await.ok();
classify(ok, body.as_ref())
}
pub async fn run(args: &[String]) -> Result<(), Box<dyn std::error::Error>> {
let (name, passthrough) = split(args);
let Some(name) = name else {
let names: Vec<&str> = AGENTS.iter().map(|a| a.name).collect();
fail(&format!(
"firstpass launch <agent> [-- args…]\n agents: {}\n\n\
`openai` needs an explicit command: firstpass launch openai -- <cmd> [args…]",
names.join(", ")
));
};
let Some(agent) = AGENTS.iter().find(|a| a.name == name) else {
let names: Vec<&str> = AGENTS.iter().map(|a| a.name).collect();
fail(&format!(
"unknown agent {name:?} (known: {})",
names.join(", ")
));
};
let (bin, argv) = if agent.bin.is_empty() {
let Some((first, rest)) = passthrough.split_first() else {
fail(&format!(
"`firstpass launch {name}` needs a command:\n\n firstpass launch {name} -- <cmd> [args…]"
));
};
(first.clone(), rest.to_vec())
} else {
(agent.bin.to_owned(), passthrough)
};
let url = base_url();
match probe(&url).await {
Listener::Firstpass => {}
Listener::None => {
fail(&format!(
"no proxy is listening at {url} — start one first:\n\n firstpass up\n\n\
(or `firstpass kiosk` to configure and start it in one press).\n\n\
Launching into a proxy that is not up produces a connection error from inside \
{bin}, which reads like {bin} is broken."
));
}
Listener::Foreign => {
fail(&format!(
"something is listening at {url}, but it is not firstpass.\n\n\
Refusing to launch {bin} against it: every request would go to that service \
instead of through the gate, and it may well answer, so nothing would look \
wrong.\n\n\
Free the port, or point firstpass elsewhere and use the same address here:\n\n \
FIRSTPASS_BIND=127.0.0.1:8099 firstpass up\n \
FIRSTPASS_BIND=127.0.0.1:8099 firstpass launch {name}"
));
}
}
let env = env_for(agent, &url);
for (k, v) in &env {
eprintln!("firstpass: {k}={v}");
}
eprintln!("firstpass: exec {bin}");
let mut cmd = Command::new(&bin);
cmd.args(&argv).envs(&env);
#[cfg(unix)]
{
use std::os::unix::process::CommandExt;
let err = cmd.exec();
Err(format!("could not exec {bin}: {err}. Is it installed and on PATH?").into())
}
#[cfg(not(unix))]
{
let status = cmd
.status()
.map_err(|e| format!("could not run {bin}: {e}. Is it installed and on PATH?"))?;
std::process::exit(status.code().unwrap_or(1));
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_wildcard_bind_becomes_a_connectable_loopback_address() {
assert_eq!(url_for_bind("0.0.0.0:9090"), "http://127.0.0.1:9090");
assert_eq!(url_for_bind("127.0.0.1:7777"), "http://127.0.0.1:7777");
}
#[test]
fn a_foreign_service_holding_the_port_is_not_mistaken_for_firstpass() {
let foreign = serde_json::json!({ "status": "ok", "llmMode": "api" });
assert_eq!(classify(true, Some(&foreign)), Listener::Foreign);
let ours =
serde_json::json!({ "status": "ok", "service": "firstpass", "version": "0.3.0" });
assert_eq!(classify(true, Some(&ours)), Listener::Firstpass);
assert_eq!(classify(false, Some(&ours)), Listener::None);
assert_eq!(classify(true, None), Listener::Foreign);
}
#[test]
fn a_bare_double_dash_separates_our_args_from_the_agents() {
let args: Vec<String> = ["claude", "--", "-p", "hi"]
.iter()
.map(|s| (*s).to_owned())
.collect();
let (name, rest) = split(&args);
assert_eq!(name, Some("claude"));
assert_eq!(rest, vec!["-p".to_owned(), "hi".to_owned()]);
}
#[test]
fn agent_args_survive_without_a_separator_too() {
let args: Vec<String> = ["claude", "-p", "hi"]
.iter()
.map(|s| (*s).to_owned())
.collect();
let (_, rest) = split(&args);
assert_eq!(rest, vec!["-p".to_owned(), "hi".to_owned()]);
}
#[test]
fn each_agent_routes_the_vendor_it_actually_speaks() {
let claude = AGENTS.iter().find(|a| a.name == "claude").expect("claude");
assert_eq!(
env_for(claude, "http://127.0.0.1:8080").get("ANTHROPIC_BASE_URL"),
Some(&"http://127.0.0.1:8080".to_owned())
);
let codex = AGENTS.iter().find(|a| a.name == "codex").expect("codex");
assert_eq!(
env_for(codex, "http://127.0.0.1:8080").get("OPENAI_BASE_URL"),
Some(&"http://127.0.0.1:8080".to_owned())
);
}
}