| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454 |
- mod init;
- mod input;
- mod render;
- use std::collections::{BTreeMap, BTreeSet};
- use std::env;
- use std::fs;
- use std::io::{self, Read, Write};
- use std::net::TcpListener;
- use std::path::{Path, PathBuf};
- use std::process::Command;
- use std::time::{SystemTime, UNIX_EPOCH};
- use api::{
- resolve_startup_auth_source, AnthropicClient, AuthSource, ContentBlockDelta, InputContentBlock,
- InputMessage, MessageRequest, MessageResponse, OutputContentBlock,
- StreamEvent as ApiStreamEvent, ToolChoice, ToolDefinition, ToolResultContentBlock,
- };
- use commands::{
- render_slash_command_help, resume_supported_slash_commands, slash_command_specs, SlashCommand,
- };
- use compat_harness::{extract_manifest, UpstreamPaths};
- use init::initialize_repo;
- use render::{MarkdownStreamState, Spinner, TerminalRenderer};
- use runtime::{
- clear_oauth_credentials, generate_pkce_pair, generate_state, load_system_prompt,
- parse_oauth_callback_request_target, save_oauth_credentials, ApiClient, ApiRequest,
- AssistantEvent, CompactionConfig, ConfigLoader, ConfigSource, ContentBlock,
- ConversationMessage, ConversationRuntime, MessageRole, OAuthAuthorizationRequest,
- OAuthTokenExchangeRequest, PermissionMode, PermissionPolicy, ProjectContext, RuntimeError,
- Session, TokenUsage, ToolError, ToolExecutor, UsageTracker,
- };
- use serde_json::json;
- use tools::{execute_tool, mvp_tool_specs, ToolSpec};
- const DEFAULT_MODEL: &str = "claude-opus-4-6";
- fn max_tokens_for_model(model: &str) -> u32 {
- if model.contains("opus") {
- 32_000
- } else {
- 64_000
- }
- }
- const DEFAULT_DATE: &str = "2026-03-31";
- const DEFAULT_OAUTH_CALLBACK_PORT: u16 = 4545;
- const VERSION: &str = env!("CARGO_PKG_VERSION");
- const BUILD_TARGET: Option<&str> = option_env!("TARGET");
- const GIT_SHA: Option<&str> = option_env!("GIT_SHA");
- type AllowedToolSet = BTreeSet<String>;
- fn main() {
- if let Err(error) = run() {
- eprintln!(
- "error: {error}
- Run `claw --help` for usage."
- );
- std::process::exit(1);
- }
- }
- fn run() -> Result<(), Box<dyn std::error::Error>> {
- let args: Vec<String> = env::args().skip(1).collect();
- match parse_args(&args)? {
- CliAction::DumpManifests => dump_manifests(),
- CliAction::BootstrapPlan => print_bootstrap_plan(),
- CliAction::PrintSystemPrompt { cwd, date } => print_system_prompt(cwd, date),
- CliAction::Version => print_version(),
- CliAction::ResumeSession {
- session_path,
- commands,
- } => resume_session(&session_path, &commands),
- CliAction::Prompt {
- prompt,
- model,
- output_format,
- allowed_tools,
- permission_mode,
- } => LiveCli::new(model, true, allowed_tools, permission_mode)?
- .run_turn_with_output(&prompt, output_format)?,
- CliAction::Login => run_login()?,
- CliAction::Logout => run_logout()?,
- CliAction::Init => run_init()?,
- CliAction::Repl {
- model,
- allowed_tools,
- permission_mode,
- } => run_repl(model, allowed_tools, permission_mode)?,
- CliAction::Help => print_help(),
- }
- Ok(())
- }
- #[derive(Debug, Clone, PartialEq, Eq)]
- enum CliAction {
- DumpManifests,
- BootstrapPlan,
- PrintSystemPrompt {
- cwd: PathBuf,
- date: String,
- },
- Version,
- ResumeSession {
- session_path: PathBuf,
- commands: Vec<String>,
- },
- Prompt {
- prompt: String,
- model: String,
- output_format: CliOutputFormat,
- allowed_tools: Option<AllowedToolSet>,
- permission_mode: PermissionMode,
- },
- Login,
- Logout,
- Init,
- Repl {
- model: String,
- allowed_tools: Option<AllowedToolSet>,
- permission_mode: PermissionMode,
- },
- // prompt-mode formatting is only supported for non-interactive runs
- Help,
- }
- #[derive(Debug, Clone, Copy, PartialEq, Eq)]
- enum CliOutputFormat {
- Text,
- Json,
- }
- impl CliOutputFormat {
- fn parse(value: &str) -> Result<Self, String> {
- match value {
- "text" => Ok(Self::Text),
- "json" => Ok(Self::Json),
- other => Err(format!(
- "unsupported value for --output-format: {other} (expected text or json)"
- )),
- }
- }
- }
- #[allow(clippy::too_many_lines)]
- fn parse_args(args: &[String]) -> Result<CliAction, String> {
- let mut model = DEFAULT_MODEL.to_string();
- let mut output_format = CliOutputFormat::Text;
- let mut permission_mode = default_permission_mode();
- let mut wants_version = false;
- let mut allowed_tool_values = Vec::new();
- let mut rest = Vec::new();
- let mut index = 0;
- while index < args.len() {
- match args[index].as_str() {
- "--version" | "-V" => {
- wants_version = true;
- index += 1;
- }
- "--model" => {
- let value = args
- .get(index + 1)
- .ok_or_else(|| "missing value for --model".to_string())?;
- model = resolve_model_alias(value).to_string();
- index += 2;
- }
- flag if flag.starts_with("--model=") => {
- model = resolve_model_alias(&flag[8..]).to_string();
- index += 1;
- }
- "--output-format" => {
- let value = args
- .get(index + 1)
- .ok_or_else(|| "missing value for --output-format".to_string())?;
- output_format = CliOutputFormat::parse(value)?;
- index += 2;
- }
- "--permission-mode" => {
- let value = args
- .get(index + 1)
- .ok_or_else(|| "missing value for --permission-mode".to_string())?;
- permission_mode = parse_permission_mode_arg(value)?;
- index += 2;
- }
- flag if flag.starts_with("--output-format=") => {
- output_format = CliOutputFormat::parse(&flag[16..])?;
- index += 1;
- }
- flag if flag.starts_with("--permission-mode=") => {
- permission_mode = parse_permission_mode_arg(&flag[18..])?;
- index += 1;
- }
- "--dangerously-skip-permissions" => {
- permission_mode = PermissionMode::DangerFullAccess;
- index += 1;
- }
- "--allowedTools" | "--allowed-tools" => {
- let value = args
- .get(index + 1)
- .ok_or_else(|| "missing value for --allowedTools".to_string())?;
- allowed_tool_values.push(value.clone());
- index += 2;
- }
- flag if flag.starts_with("--allowedTools=") => {
- allowed_tool_values.push(flag[15..].to_string());
- index += 1;
- }
- flag if flag.starts_with("--allowed-tools=") => {
- allowed_tool_values.push(flag[16..].to_string());
- index += 1;
- }
- other => {
- rest.push(other.to_string());
- index += 1;
- }
- }
- }
- if wants_version {
- return Ok(CliAction::Version);
- }
- let allowed_tools = normalize_allowed_tools(&allowed_tool_values)?;
- if rest.is_empty() {
- return Ok(CliAction::Repl {
- model,
- allowed_tools,
- permission_mode,
- });
- }
- if matches!(rest.first().map(String::as_str), Some("--help" | "-h")) {
- return Ok(CliAction::Help);
- }
- if rest.first().map(String::as_str) == Some("--resume") {
- return parse_resume_args(&rest[1..]);
- }
- match rest[0].as_str() {
- "dump-manifests" => Ok(CliAction::DumpManifests),
- "bootstrap-plan" => Ok(CliAction::BootstrapPlan),
- "system-prompt" => parse_system_prompt_args(&rest[1..]),
- "login" => Ok(CliAction::Login),
- "logout" => Ok(CliAction::Logout),
- "init" => Ok(CliAction::Init),
- "prompt" => {
- let prompt = rest[1..].join(" ");
- if prompt.trim().is_empty() {
- return Err("prompt subcommand requires a prompt string".to_string());
- }
- Ok(CliAction::Prompt {
- prompt,
- model,
- output_format,
- allowed_tools,
- permission_mode,
- })
- }
- other if !other.starts_with('/') => Ok(CliAction::Prompt {
- prompt: rest.join(" "),
- model,
- output_format,
- allowed_tools,
- permission_mode,
- }),
- other => Err(format!("unknown subcommand: {other}")),
- }
- }
- fn resolve_model_alias(model: &str) -> &str {
- match model {
- "opus" => "claude-opus-4-6",
- "sonnet" => "claude-sonnet-4-6",
- "haiku" => "claude-haiku-4-5-20251213",
- _ => model,
- }
- }
- fn normalize_allowed_tools(values: &[String]) -> Result<Option<AllowedToolSet>, String> {
- if values.is_empty() {
- return Ok(None);
- }
- let canonical_names = mvp_tool_specs()
- .into_iter()
- .map(|spec| spec.name.to_string())
- .collect::<Vec<_>>();
- let mut name_map = canonical_names
- .iter()
- .map(|name| (normalize_tool_name(name), name.clone()))
- .collect::<BTreeMap<_, _>>();
- for (alias, canonical) in [
- ("read", "read_file"),
- ("write", "write_file"),
- ("edit", "edit_file"),
- ("glob", "glob_search"),
- ("grep", "grep_search"),
- ] {
- name_map.insert(alias.to_string(), canonical.to_string());
- }
- let mut allowed = AllowedToolSet::new();
- for value in values {
- for token in value
- .split(|ch: char| ch == ',' || ch.is_whitespace())
- .filter(|token| !token.is_empty())
- {
- let normalized = normalize_tool_name(token);
- let canonical = name_map.get(&normalized).ok_or_else(|| {
- format!(
- "unsupported tool in --allowedTools: {token} (expected one of: {})",
- canonical_names.join(", ")
- )
- })?;
- allowed.insert(canonical.clone());
- }
- }
- Ok(Some(allowed))
- }
- fn normalize_tool_name(value: &str) -> String {
- value.trim().replace('-', "_").to_ascii_lowercase()
- }
- fn parse_permission_mode_arg(value: &str) -> Result<PermissionMode, String> {
- normalize_permission_mode(value)
- .ok_or_else(|| {
- format!(
- "unsupported permission mode '{value}'. Use read-only, workspace-write, or danger-full-access."
- )
- })
- .map(permission_mode_from_label)
- }
- fn permission_mode_from_label(mode: &str) -> PermissionMode {
- match mode {
- "read-only" => PermissionMode::ReadOnly,
- "workspace-write" => PermissionMode::WorkspaceWrite,
- "danger-full-access" => PermissionMode::DangerFullAccess,
- other => panic!("unsupported permission mode label: {other}"),
- }
- }
- fn default_permission_mode() -> PermissionMode {
- env::var("RUSTY_CLAUDE_PERMISSION_MODE")
- .ok()
- .as_deref()
- .and_then(normalize_permission_mode)
- .map_or(PermissionMode::DangerFullAccess, permission_mode_from_label)
- }
- fn filter_tool_specs(allowed_tools: Option<&AllowedToolSet>) -> Vec<tools::ToolSpec> {
- mvp_tool_specs()
- .into_iter()
- .filter(|spec| allowed_tools.is_none_or(|allowed| allowed.contains(spec.name)))
- .collect()
- }
- fn parse_system_prompt_args(args: &[String]) -> Result<CliAction, String> {
- let mut cwd = env::current_dir().map_err(|error| error.to_string())?;
- let mut date = DEFAULT_DATE.to_string();
- let mut index = 0;
- while index < args.len() {
- match args[index].as_str() {
- "--cwd" => {
- let value = args
- .get(index + 1)
- .ok_or_else(|| "missing value for --cwd".to_string())?;
- cwd = PathBuf::from(value);
- index += 2;
- }
- "--date" => {
- let value = args
- .get(index + 1)
- .ok_or_else(|| "missing value for --date".to_string())?;
- date.clone_from(value);
- index += 2;
- }
- other => return Err(format!("unknown system-prompt option: {other}")),
- }
- }
- Ok(CliAction::PrintSystemPrompt { cwd, date })
- }
- fn parse_resume_args(args: &[String]) -> Result<CliAction, String> {
- let session_path = args
- .first()
- .ok_or_else(|| "missing session path for --resume".to_string())
- .map(PathBuf::from)?;
- let commands = args[1..].to_vec();
- if commands
- .iter()
- .any(|command| !command.trim_start().starts_with('/'))
- {
- return Err("--resume trailing arguments must be slash commands".to_string());
- }
- Ok(CliAction::ResumeSession {
- session_path,
- commands,
- })
- }
- fn dump_manifests() {
- let workspace_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../..");
- let paths = UpstreamPaths::from_workspace_dir(&workspace_dir);
- match extract_manifest(&paths) {
- Ok(manifest) => {
- println!("commands: {}", manifest.commands.entries().len());
- println!("tools: {}", manifest.tools.entries().len());
- println!("bootstrap phases: {}", manifest.bootstrap.phases().len());
- }
- Err(error) => {
- eprintln!("failed to extract manifests: {error}");
- std::process::exit(1);
- }
- }
- }
- fn print_bootstrap_plan() {
- for phase in runtime::BootstrapPlan::claude_code_default().phases() {
- println!("- {phase:?}");
- }
- }
- fn run_login() -> Result<(), Box<dyn std::error::Error>> {
- let cwd = env::current_dir()?;
- let config = ConfigLoader::default_for(&cwd).load()?;
- let oauth = config.oauth().ok_or_else(|| {
- io::Error::new(
- io::ErrorKind::NotFound,
- "OAuth config is missing. Add settings.oauth.clientId/authorizeUrl/tokenUrl first.",
- )
- })?;
- let callback_port = oauth.callback_port.unwrap_or(DEFAULT_OAUTH_CALLBACK_PORT);
- let redirect_uri = runtime::loopback_redirect_uri(callback_port);
- let pkce = generate_pkce_pair()?;
- let state = generate_state()?;
- let authorize_url =
- OAuthAuthorizationRequest::from_config(oauth, redirect_uri.clone(), state.clone(), &pkce)
- .build_url();
- println!("Starting Claude OAuth login...");
- println!("Listening for callback on {redirect_uri}");
- if let Err(error) = open_browser(&authorize_url) {
- eprintln!("warning: failed to open browser automatically: {error}");
- println!("Open this URL manually:\n{authorize_url}");
- }
- let callback = wait_for_oauth_callback(callback_port)?;
- if let Some(error) = callback.error {
- let description = callback
- .error_description
- .unwrap_or_else(|| "authorization failed".to_string());
- return Err(io::Error::other(format!("{error}: {description}")).into());
- }
- let code = callback.code.ok_or_else(|| {
- io::Error::new(io::ErrorKind::InvalidData, "callback did not include code")
- })?;
- let returned_state = callback.state.ok_or_else(|| {
- io::Error::new(io::ErrorKind::InvalidData, "callback did not include state")
- })?;
- if returned_state != state {
- return Err(io::Error::new(io::ErrorKind::InvalidData, "oauth state mismatch").into());
- }
- let client = AnthropicClient::from_auth(AuthSource::None).with_base_url(api::read_base_url());
- let exchange_request =
- OAuthTokenExchangeRequest::from_config(oauth, code, state, pkce.verifier, redirect_uri);
- let runtime = tokio::runtime::Runtime::new()?;
- let token_set = runtime.block_on(client.exchange_oauth_code(oauth, &exchange_request))?;
- save_oauth_credentials(&runtime::OAuthTokenSet {
- access_token: token_set.access_token,
- refresh_token: token_set.refresh_token,
- expires_at: token_set.expires_at,
- scopes: token_set.scopes,
- })?;
- println!("Claude OAuth login complete.");
- Ok(())
- }
- fn run_logout() -> Result<(), Box<dyn std::error::Error>> {
- clear_oauth_credentials()?;
- println!("Claude OAuth credentials cleared.");
- Ok(())
- }
- fn open_browser(url: &str) -> io::Result<()> {
- let commands = if cfg!(target_os = "macos") {
- vec![("open", vec![url])]
- } else if cfg!(target_os = "windows") {
- vec![("cmd", vec!["/C", "start", "", url])]
- } else {
- vec![("xdg-open", vec![url])]
- };
- for (program, args) in commands {
- match Command::new(program).args(args).spawn() {
- Ok(_) => return Ok(()),
- Err(error) if error.kind() == io::ErrorKind::NotFound => {}
- Err(error) => return Err(error),
- }
- }
- Err(io::Error::new(
- io::ErrorKind::NotFound,
- "no supported browser opener command found",
- ))
- }
- fn wait_for_oauth_callback(
- port: u16,
- ) -> Result<runtime::OAuthCallbackParams, Box<dyn std::error::Error>> {
- let listener = TcpListener::bind(("127.0.0.1", port))?;
- let (mut stream, _) = listener.accept()?;
- let mut buffer = [0_u8; 4096];
- let bytes_read = stream.read(&mut buffer)?;
- let request = String::from_utf8_lossy(&buffer[..bytes_read]);
- let request_line = request.lines().next().ok_or_else(|| {
- io::Error::new(io::ErrorKind::InvalidData, "missing callback request line")
- })?;
- let target = request_line.split_whitespace().nth(1).ok_or_else(|| {
- io::Error::new(
- io::ErrorKind::InvalidData,
- "missing callback request target",
- )
- })?;
- let callback = parse_oauth_callback_request_target(target)
- .map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error))?;
- let body = if callback.error.is_some() {
- "Claude OAuth login failed. You can close this window."
- } else {
- "Claude OAuth login succeeded. You can close this window."
- };
- let response = format!(
- "HTTP/1.1 200 OK\r\ncontent-type: text/plain; charset=utf-8\r\ncontent-length: {}\r\nconnection: close\r\n\r\n{}",
- body.len(),
- body
- );
- stream.write_all(response.as_bytes())?;
- Ok(callback)
- }
- fn print_system_prompt(cwd: PathBuf, date: String) {
- match load_system_prompt(cwd, date, env::consts::OS, "unknown") {
- Ok(sections) => println!("{}", sections.join("\n\n")),
- Err(error) => {
- eprintln!("failed to build system prompt: {error}");
- std::process::exit(1);
- }
- }
- }
- fn print_version() {
- println!("{}", render_version_report());
- }
- fn resume_session(session_path: &Path, commands: &[String]) {
- let session = match Session::load_from_path(session_path) {
- Ok(session) => session,
- Err(error) => {
- eprintln!("failed to restore session: {error}");
- std::process::exit(1);
- }
- };
- if commands.is_empty() {
- println!(
- "Restored session from {} ({} messages).",
- session_path.display(),
- session.messages.len()
- );
- return;
- }
- let mut session = session;
- for raw_command in commands {
- let Some(command) = SlashCommand::parse(raw_command) else {
- eprintln!("unsupported resumed command: {raw_command}");
- std::process::exit(2);
- };
- match run_resume_command(session_path, &session, &command) {
- Ok(ResumeCommandOutcome {
- session: next_session,
- message,
- }) => {
- session = next_session;
- if let Some(message) = message {
- println!("{message}");
- }
- }
- Err(error) => {
- eprintln!("{error}");
- std::process::exit(2);
- }
- }
- }
- }
- #[derive(Debug, Clone)]
- struct ResumeCommandOutcome {
- session: Session,
- message: Option<String>,
- }
- #[derive(Debug, Clone)]
- struct StatusContext {
- cwd: PathBuf,
- session_path: Option<PathBuf>,
- loaded_config_files: usize,
- discovered_config_files: usize,
- memory_file_count: usize,
- project_root: Option<PathBuf>,
- git_branch: Option<String>,
- }
- #[derive(Debug, Clone, Copy)]
- struct StatusUsage {
- message_count: usize,
- turns: u32,
- latest: TokenUsage,
- cumulative: TokenUsage,
- estimated_tokens: usize,
- }
- fn format_model_report(model: &str, message_count: usize, turns: u32) -> String {
- format!(
- "Model
- Current model {model}
- Session messages {message_count}
- Session turns {turns}
- Usage
- Inspect current model with /model
- Switch models with /model <name>"
- )
- }
- fn format_model_switch_report(previous: &str, next: &str, message_count: usize) -> String {
- format!(
- "Model updated
- Previous {previous}
- Current {next}
- Preserved msgs {message_count}"
- )
- }
- fn format_permissions_report(mode: &str) -> String {
- let modes = [
- ("read-only", "Read/search tools only", mode == "read-only"),
- (
- "workspace-write",
- "Edit files inside the workspace",
- mode == "workspace-write",
- ),
- (
- "danger-full-access",
- "Unrestricted tool access",
- mode == "danger-full-access",
- ),
- ]
- .into_iter()
- .map(|(name, description, is_current)| {
- let marker = if is_current {
- "● current"
- } else {
- "○ available"
- };
- format!(" {name:<18} {marker:<11} {description}")
- })
- .collect::<Vec<_>>()
- .join(
- "
- ",
- );
- format!(
- "Permissions
- Active mode {mode}
- Mode status live session default
- Modes
- {modes}
- Usage
- Inspect current mode with /permissions
- Switch modes with /permissions <mode>"
- )
- }
- fn format_permissions_switch_report(previous: &str, next: &str) -> String {
- format!(
- "Permissions updated
- Result mode switched
- Previous mode {previous}
- Active mode {next}
- Applies to subsequent tool calls
- Usage /permissions to inspect current mode"
- )
- }
- fn format_cost_report(usage: TokenUsage) -> String {
- format!(
- "Cost
- Input tokens {}
- Output tokens {}
- Cache create {}
- Cache read {}
- Total tokens {}",
- usage.input_tokens,
- usage.output_tokens,
- usage.cache_creation_input_tokens,
- usage.cache_read_input_tokens,
- usage.total_tokens(),
- )
- }
- fn format_resume_report(session_path: &str, message_count: usize, turns: u32) -> String {
- format!(
- "Session resumed
- Session file {session_path}
- Messages {message_count}
- Turns {turns}"
- )
- }
- fn format_compact_report(removed: usize, resulting_messages: usize, skipped: bool) -> String {
- if skipped {
- format!(
- "Compact
- Result skipped
- Reason session below compaction threshold
- Messages kept {resulting_messages}"
- )
- } else {
- format!(
- "Compact
- Result compacted
- Messages removed {removed}
- Messages kept {resulting_messages}"
- )
- }
- }
- fn parse_git_status_metadata(status: Option<&str>) -> (Option<PathBuf>, Option<String>) {
- let Some(status) = status else {
- return (None, None);
- };
- let branch = status.lines().next().and_then(|line| {
- line.strip_prefix("## ")
- .map(|line| {
- line.split(['.', ' '])
- .next()
- .unwrap_or_default()
- .to_string()
- })
- .filter(|value| !value.is_empty())
- });
- let project_root = find_git_root().ok();
- (project_root, branch)
- }
- fn find_git_root() -> Result<PathBuf, Box<dyn std::error::Error>> {
- let output = std::process::Command::new("git")
- .args(["rev-parse", "--show-toplevel"])
- .current_dir(env::current_dir()?)
- .output()?;
- if !output.status.success() {
- return Err("not a git repository".into());
- }
- let path = String::from_utf8(output.stdout)?.trim().to_string();
- if path.is_empty() {
- return Err("empty git root".into());
- }
- Ok(PathBuf::from(path))
- }
- #[allow(clippy::too_many_lines)]
- fn run_resume_command(
- session_path: &Path,
- session: &Session,
- command: &SlashCommand,
- ) -> Result<ResumeCommandOutcome, Box<dyn std::error::Error>> {
- match command {
- SlashCommand::Help => Ok(ResumeCommandOutcome {
- session: session.clone(),
- message: Some(render_repl_help()),
- }),
- SlashCommand::Compact => {
- let result = runtime::compact_session(
- session,
- CompactionConfig {
- max_estimated_tokens: 0,
- ..CompactionConfig::default()
- },
- );
- let removed = result.removed_message_count;
- let kept = result.compacted_session.messages.len();
- let skipped = removed == 0;
- result.compacted_session.save_to_path(session_path)?;
- Ok(ResumeCommandOutcome {
- session: result.compacted_session,
- message: Some(format_compact_report(removed, kept, skipped)),
- })
- }
- SlashCommand::Clear { confirm } => {
- if !confirm {
- return Ok(ResumeCommandOutcome {
- session: session.clone(),
- message: Some(
- "clear: confirmation required; rerun with /clear --confirm".to_string(),
- ),
- });
- }
- let cleared = Session::new();
- cleared.save_to_path(session_path)?;
- Ok(ResumeCommandOutcome {
- session: cleared,
- message: Some(format!(
- "Cleared resumed session file {}.",
- session_path.display()
- )),
- })
- }
- SlashCommand::Status => {
- let tracker = UsageTracker::from_session(session);
- let usage = tracker.cumulative_usage();
- Ok(ResumeCommandOutcome {
- session: session.clone(),
- message: Some(format_status_report(
- "restored-session",
- StatusUsage {
- message_count: session.messages.len(),
- turns: tracker.turns(),
- latest: tracker.current_turn_usage(),
- cumulative: usage,
- estimated_tokens: 0,
- },
- default_permission_mode().as_str(),
- &status_context(Some(session_path))?,
- )),
- })
- }
- SlashCommand::Cost => {
- let usage = UsageTracker::from_session(session).cumulative_usage();
- Ok(ResumeCommandOutcome {
- session: session.clone(),
- message: Some(format_cost_report(usage)),
- })
- }
- SlashCommand::Config { section } => Ok(ResumeCommandOutcome {
- session: session.clone(),
- message: Some(render_config_report(section.as_deref())?),
- }),
- SlashCommand::Memory => Ok(ResumeCommandOutcome {
- session: session.clone(),
- message: Some(render_memory_report()?),
- }),
- SlashCommand::Init => Ok(ResumeCommandOutcome {
- session: session.clone(),
- message: Some(init_claude_md()?),
- }),
- SlashCommand::Diff => Ok(ResumeCommandOutcome {
- session: session.clone(),
- message: Some(render_diff_report()?),
- }),
- SlashCommand::Version => Ok(ResumeCommandOutcome {
- session: session.clone(),
- message: Some(render_version_report()),
- }),
- SlashCommand::Export { path } => {
- let export_path = resolve_export_path(path.as_deref(), session)?;
- fs::write(&export_path, render_export_text(session))?;
- Ok(ResumeCommandOutcome {
- session: session.clone(),
- message: Some(format!(
- "Export\n Result wrote transcript\n File {}\n Messages {}",
- export_path.display(),
- session.messages.len(),
- )),
- })
- }
- SlashCommand::Resume { .. }
- | SlashCommand::Model { .. }
- | SlashCommand::Permissions { .. }
- | SlashCommand::Session { .. }
- | SlashCommand::Unknown(_) => Err("unsupported resumed slash command".into()),
- }
- }
- fn run_repl(
- model: String,
- allowed_tools: Option<AllowedToolSet>,
- permission_mode: PermissionMode,
- ) -> Result<(), Box<dyn std::error::Error>> {
- let mut cli = LiveCli::new(model, true, allowed_tools, permission_mode)?;
- let mut editor = input::LineEditor::new("> ", slash_command_completion_candidates());
- println!("{}", cli.startup_banner());
- loop {
- match editor.read_line()? {
- input::ReadOutcome::Submit(input) => {
- let trimmed = input.trim().to_string();
- if trimmed.is_empty() {
- continue;
- }
- if matches!(trimmed.as_str(), "/exit" | "/quit") {
- cli.persist_session()?;
- break;
- }
- if let Some(command) = SlashCommand::parse(&trimmed) {
- if cli.handle_repl_command(command)? {
- cli.persist_session()?;
- }
- continue;
- }
- editor.push_history(input);
- cli.run_turn(&trimmed)?;
- }
- input::ReadOutcome::Cancel => {}
- input::ReadOutcome::Exit => {
- cli.persist_session()?;
- break;
- }
- }
- }
- Ok(())
- }
- #[derive(Debug, Clone)]
- struct SessionHandle {
- id: String,
- path: PathBuf,
- }
- #[derive(Debug, Clone)]
- struct ManagedSessionSummary {
- id: String,
- path: PathBuf,
- modified_epoch_secs: u64,
- message_count: usize,
- }
- struct LiveCli {
- model: String,
- allowed_tools: Option<AllowedToolSet>,
- permission_mode: PermissionMode,
- system_prompt: Vec<String>,
- runtime: ConversationRuntime<AnthropicRuntimeClient, CliToolExecutor>,
- session: SessionHandle,
- }
- impl LiveCli {
- fn new(
- model: String,
- enable_tools: bool,
- allowed_tools: Option<AllowedToolSet>,
- permission_mode: PermissionMode,
- ) -> Result<Self, Box<dyn std::error::Error>> {
- let system_prompt = build_system_prompt()?;
- let session = create_managed_session_handle()?;
- let runtime = build_runtime(
- Session::new(),
- model.clone(),
- system_prompt.clone(),
- enable_tools,
- true,
- allowed_tools.clone(),
- permission_mode,
- )?;
- let cli = Self {
- model,
- allowed_tools,
- permission_mode,
- system_prompt,
- runtime,
- session,
- };
- cli.persist_session()?;
- Ok(cli)
- }
- fn startup_banner(&self) -> String {
- let cwd = env::current_dir().map_or_else(
- |_| "<unknown>".to_string(),
- |path| path.display().to_string(),
- );
- format!(
- "\x1b[38;5;196m\
- ██████╗██╗ █████╗ ██╗ ██╗\n\
- ██╔════╝██║ ██╔══██╗██║ ██║\n\
- ██║ ██║ ███████║██║ █╗ ██║\n\
- ██║ ██║ ██╔══██║██║███╗██║\n\
- ╚██████╗███████╗██║ ██║╚███╔███╔╝\n\
- ╚═════╝╚══════╝╚═╝ ╚═╝ ╚══╝╚══╝\x1b[0m \x1b[38;5;208mCode\x1b[0m 🦞\n\n\
- \x1b[2mModel\x1b[0m {}\n\
- \x1b[2mPermissions\x1b[0m {}\n\
- \x1b[2mDirectory\x1b[0m {}\n\
- \x1b[2mSession\x1b[0m {}\n\n\
- Type \x1b[1m/help\x1b[0m for commands · \x1b[2mShift+Enter\x1b[0m for newline",
- self.model,
- self.permission_mode.as_str(),
- cwd,
- self.session.id,
- )
- }
- fn run_turn(&mut self, input: &str) -> Result<(), Box<dyn std::error::Error>> {
- let mut spinner = Spinner::new();
- let mut stdout = io::stdout();
- spinner.tick(
- "🦀 Thinking...",
- TerminalRenderer::new().color_theme(),
- &mut stdout,
- )?;
- let mut permission_prompter = CliPermissionPrompter::new(self.permission_mode);
- let result = self.runtime.run_turn(input, Some(&mut permission_prompter));
- match result {
- Ok(_) => {
- spinner.finish(
- "✨ Done",
- TerminalRenderer::new().color_theme(),
- &mut stdout,
- )?;
- println!();
- self.persist_session()?;
- Ok(())
- }
- Err(error) => {
- spinner.fail(
- "❌ Request failed",
- TerminalRenderer::new().color_theme(),
- &mut stdout,
- )?;
- Err(Box::new(error))
- }
- }
- }
- fn run_turn_with_output(
- &mut self,
- input: &str,
- output_format: CliOutputFormat,
- ) -> Result<(), Box<dyn std::error::Error>> {
- match output_format {
- CliOutputFormat::Text => self.run_turn(input),
- CliOutputFormat::Json => self.run_prompt_json(input),
- }
- }
- fn run_prompt_json(&mut self, input: &str) -> Result<(), Box<dyn std::error::Error>> {
- let session = self.runtime.session().clone();
- let mut runtime = build_runtime(
- session,
- self.model.clone(),
- self.system_prompt.clone(),
- true,
- false,
- self.allowed_tools.clone(),
- self.permission_mode,
- )?;
- let mut permission_prompter = CliPermissionPrompter::new(self.permission_mode);
- let summary = runtime.run_turn(input, Some(&mut permission_prompter))?;
- self.runtime = runtime;
- self.persist_session()?;
- println!(
- "{}",
- json!({
- "message": final_assistant_text(&summary),
- "model": self.model,
- "iterations": summary.iterations,
- "tool_uses": collect_tool_uses(&summary),
- "tool_results": collect_tool_results(&summary),
- "usage": {
- "input_tokens": summary.usage.input_tokens,
- "output_tokens": summary.usage.output_tokens,
- "cache_creation_input_tokens": summary.usage.cache_creation_input_tokens,
- "cache_read_input_tokens": summary.usage.cache_read_input_tokens,
- }
- })
- );
- Ok(())
- }
- fn handle_repl_command(
- &mut self,
- command: SlashCommand,
- ) -> Result<bool, Box<dyn std::error::Error>> {
- Ok(match command {
- SlashCommand::Help => {
- println!("{}", render_repl_help());
- false
- }
- SlashCommand::Status => {
- self.print_status();
- false
- }
- SlashCommand::Compact => {
- self.compact()?;
- false
- }
- SlashCommand::Model { model } => self.set_model(model)?,
- SlashCommand::Permissions { mode } => self.set_permissions(mode)?,
- SlashCommand::Clear { confirm } => self.clear_session(confirm)?,
- SlashCommand::Cost => {
- self.print_cost();
- false
- }
- SlashCommand::Resume { session_path } => self.resume_session(session_path)?,
- SlashCommand::Config { section } => {
- Self::print_config(section.as_deref())?;
- false
- }
- SlashCommand::Memory => {
- Self::print_memory()?;
- false
- }
- SlashCommand::Init => {
- run_init()?;
- false
- }
- SlashCommand::Diff => {
- Self::print_diff()?;
- false
- }
- SlashCommand::Version => {
- Self::print_version();
- false
- }
- SlashCommand::Export { path } => {
- self.export_session(path.as_deref())?;
- false
- }
- SlashCommand::Session { action, target } => {
- self.handle_session_command(action.as_deref(), target.as_deref())?
- }
- SlashCommand::Unknown(name) => {
- eprintln!("unknown slash command: /{name}");
- false
- }
- })
- }
- fn persist_session(&self) -> Result<(), Box<dyn std::error::Error>> {
- self.runtime.session().save_to_path(&self.session.path)?;
- Ok(())
- }
- fn print_status(&self) {
- let cumulative = self.runtime.usage().cumulative_usage();
- let latest = self.runtime.usage().current_turn_usage();
- println!(
- "{}",
- format_status_report(
- &self.model,
- StatusUsage {
- message_count: self.runtime.session().messages.len(),
- turns: self.runtime.usage().turns(),
- latest,
- cumulative,
- estimated_tokens: self.runtime.estimated_tokens(),
- },
- self.permission_mode.as_str(),
- &status_context(Some(&self.session.path)).expect("status context should load"),
- )
- );
- }
- fn set_model(&mut self, model: Option<String>) -> Result<bool, Box<dyn std::error::Error>> {
- let Some(model) = model else {
- println!(
- "{}",
- format_model_report(
- &self.model,
- self.runtime.session().messages.len(),
- self.runtime.usage().turns(),
- )
- );
- return Ok(false);
- };
- let model = resolve_model_alias(&model).to_string();
- if model == self.model {
- println!(
- "{}",
- format_model_report(
- &self.model,
- self.runtime.session().messages.len(),
- self.runtime.usage().turns(),
- )
- );
- return Ok(false);
- }
- let previous = self.model.clone();
- let session = self.runtime.session().clone();
- let message_count = session.messages.len();
- self.runtime = build_runtime(
- session,
- model.clone(),
- self.system_prompt.clone(),
- true,
- true,
- self.allowed_tools.clone(),
- self.permission_mode,
- )?;
- self.model.clone_from(&model);
- println!(
- "{}",
- format_model_switch_report(&previous, &model, message_count)
- );
- Ok(true)
- }
- fn set_permissions(
- &mut self,
- mode: Option<String>,
- ) -> Result<bool, Box<dyn std::error::Error>> {
- let Some(mode) = mode else {
- println!(
- "{}",
- format_permissions_report(self.permission_mode.as_str())
- );
- return Ok(false);
- };
- let normalized = normalize_permission_mode(&mode).ok_or_else(|| {
- format!(
- "unsupported permission mode '{mode}'. Use read-only, workspace-write, or danger-full-access."
- )
- })?;
- if normalized == self.permission_mode.as_str() {
- println!("{}", format_permissions_report(normalized));
- return Ok(false);
- }
- let previous = self.permission_mode.as_str().to_string();
- let session = self.runtime.session().clone();
- self.permission_mode = permission_mode_from_label(normalized);
- self.runtime = build_runtime(
- session,
- self.model.clone(),
- self.system_prompt.clone(),
- true,
- true,
- self.allowed_tools.clone(),
- self.permission_mode,
- )?;
- println!(
- "{}",
- format_permissions_switch_report(&previous, normalized)
- );
- Ok(true)
- }
- fn clear_session(&mut self, confirm: bool) -> Result<bool, Box<dyn std::error::Error>> {
- if !confirm {
- println!(
- "clear: confirmation required; run /clear --confirm to start a fresh session."
- );
- return Ok(false);
- }
- self.session = create_managed_session_handle()?;
- self.runtime = build_runtime(
- Session::new(),
- self.model.clone(),
- self.system_prompt.clone(),
- true,
- true,
- self.allowed_tools.clone(),
- self.permission_mode,
- )?;
- println!(
- "Session cleared\n Mode fresh session\n Preserved model {}\n Permission mode {}\n Session {}",
- self.model,
- self.permission_mode.as_str(),
- self.session.id,
- );
- Ok(true)
- }
- fn print_cost(&self) {
- let cumulative = self.runtime.usage().cumulative_usage();
- println!("{}", format_cost_report(cumulative));
- }
- fn resume_session(
- &mut self,
- session_path: Option<String>,
- ) -> Result<bool, Box<dyn std::error::Error>> {
- let Some(session_ref) = session_path else {
- println!("Usage: /resume <session-path>");
- return Ok(false);
- };
- let handle = resolve_session_reference(&session_ref)?;
- let session = Session::load_from_path(&handle.path)?;
- let message_count = session.messages.len();
- self.runtime = build_runtime(
- session,
- self.model.clone(),
- self.system_prompt.clone(),
- true,
- true,
- self.allowed_tools.clone(),
- self.permission_mode,
- )?;
- self.session = handle;
- println!(
- "{}",
- format_resume_report(
- &self.session.path.display().to_string(),
- message_count,
- self.runtime.usage().turns(),
- )
- );
- Ok(true)
- }
- fn print_config(section: Option<&str>) -> Result<(), Box<dyn std::error::Error>> {
- println!("{}", render_config_report(section)?);
- Ok(())
- }
- fn print_memory() -> Result<(), Box<dyn std::error::Error>> {
- println!("{}", render_memory_report()?);
- Ok(())
- }
- fn print_diff() -> Result<(), Box<dyn std::error::Error>> {
- println!("{}", render_diff_report()?);
- Ok(())
- }
- fn print_version() {
- println!("{}", render_version_report());
- }
- fn export_session(
- &self,
- requested_path: Option<&str>,
- ) -> Result<(), Box<dyn std::error::Error>> {
- let export_path = resolve_export_path(requested_path, self.runtime.session())?;
- fs::write(&export_path, render_export_text(self.runtime.session()))?;
- println!(
- "Export\n Result wrote transcript\n File {}\n Messages {}",
- export_path.display(),
- self.runtime.session().messages.len(),
- );
- Ok(())
- }
- fn handle_session_command(
- &mut self,
- action: Option<&str>,
- target: Option<&str>,
- ) -> Result<bool, Box<dyn std::error::Error>> {
- match action {
- None | Some("list") => {
- println!("{}", render_session_list(&self.session.id)?);
- Ok(false)
- }
- Some("switch") => {
- let Some(target) = target else {
- println!("Usage: /session switch <session-id>");
- return Ok(false);
- };
- let handle = resolve_session_reference(target)?;
- let session = Session::load_from_path(&handle.path)?;
- let message_count = session.messages.len();
- self.runtime = build_runtime(
- session,
- self.model.clone(),
- self.system_prompt.clone(),
- true,
- true,
- self.allowed_tools.clone(),
- self.permission_mode,
- )?;
- self.session = handle;
- println!(
- "Session switched\n Active session {}\n File {}\n Messages {}",
- self.session.id,
- self.session.path.display(),
- message_count,
- );
- Ok(true)
- }
- Some(other) => {
- println!("Unknown /session action '{other}'. Use /session list or /session switch <session-id>.");
- Ok(false)
- }
- }
- }
- fn compact(&mut self) -> Result<(), Box<dyn std::error::Error>> {
- let result = self.runtime.compact(CompactionConfig::default());
- let removed = result.removed_message_count;
- let kept = result.compacted_session.messages.len();
- let skipped = removed == 0;
- self.runtime = build_runtime(
- result.compacted_session,
- self.model.clone(),
- self.system_prompt.clone(),
- true,
- true,
- self.allowed_tools.clone(),
- self.permission_mode,
- )?;
- self.persist_session()?;
- println!("{}", format_compact_report(removed, kept, skipped));
- Ok(())
- }
- }
- fn sessions_dir() -> Result<PathBuf, Box<dyn std::error::Error>> {
- let cwd = env::current_dir()?;
- let path = cwd.join(".claude").join("sessions");
- fs::create_dir_all(&path)?;
- Ok(path)
- }
- fn create_managed_session_handle() -> Result<SessionHandle, Box<dyn std::error::Error>> {
- let id = generate_session_id();
- let path = sessions_dir()?.join(format!("{id}.json"));
- Ok(SessionHandle { id, path })
- }
- fn generate_session_id() -> String {
- let millis = SystemTime::now()
- .duration_since(UNIX_EPOCH)
- .map(|duration| duration.as_millis())
- .unwrap_or_default();
- format!("session-{millis}")
- }
- fn resolve_session_reference(reference: &str) -> Result<SessionHandle, Box<dyn std::error::Error>> {
- let direct = PathBuf::from(reference);
- let path = if direct.exists() {
- direct
- } else {
- sessions_dir()?.join(format!("{reference}.json"))
- };
- if !path.exists() {
- return Err(format!("session not found: {reference}").into());
- }
- let id = path
- .file_stem()
- .and_then(|value| value.to_str())
- .unwrap_or(reference)
- .to_string();
- Ok(SessionHandle { id, path })
- }
- fn list_managed_sessions() -> Result<Vec<ManagedSessionSummary>, Box<dyn std::error::Error>> {
- let mut sessions = Vec::new();
- for entry in fs::read_dir(sessions_dir()?)? {
- let entry = entry?;
- let path = entry.path();
- if path.extension().and_then(|ext| ext.to_str()) != Some("json") {
- continue;
- }
- let metadata = entry.metadata()?;
- let modified_epoch_secs = metadata
- .modified()
- .ok()
- .and_then(|time| time.duration_since(UNIX_EPOCH).ok())
- .map(|duration| duration.as_secs())
- .unwrap_or_default();
- let message_count = Session::load_from_path(&path)
- .map(|session| session.messages.len())
- .unwrap_or_default();
- let id = path
- .file_stem()
- .and_then(|value| value.to_str())
- .unwrap_or("unknown")
- .to_string();
- sessions.push(ManagedSessionSummary {
- id,
- path,
- modified_epoch_secs,
- message_count,
- });
- }
- sessions.sort_by(|left, right| right.modified_epoch_secs.cmp(&left.modified_epoch_secs));
- Ok(sessions)
- }
- fn render_session_list(active_session_id: &str) -> Result<String, Box<dyn std::error::Error>> {
- let sessions = list_managed_sessions()?;
- let mut lines = vec![
- "Sessions".to_string(),
- format!(" Directory {}", sessions_dir()?.display()),
- ];
- if sessions.is_empty() {
- lines.push(" No managed sessions saved yet.".to_string());
- return Ok(lines.join("\n"));
- }
- for session in sessions {
- let marker = if session.id == active_session_id {
- "● current"
- } else {
- "○ saved"
- };
- lines.push(format!(
- " {id:<20} {marker:<10} msgs={msgs:<4} modified={modified} path={path}",
- id = session.id,
- msgs = session.message_count,
- modified = session.modified_epoch_secs,
- path = session.path.display(),
- ));
- }
- Ok(lines.join("\n"))
- }
- fn render_repl_help() -> String {
- [
- "REPL".to_string(),
- " /exit Quit the REPL".to_string(),
- " /quit Quit the REPL".to_string(),
- " Up/Down Navigate prompt history".to_string(),
- " Tab Complete slash commands".to_string(),
- " Ctrl-C Clear input (or exit on empty prompt)".to_string(),
- " Shift+Enter/Ctrl+J Insert a newline".to_string(),
- String::new(),
- render_slash_command_help(),
- ]
- .join(
- "
- ",
- )
- }
- fn status_context(
- session_path: Option<&Path>,
- ) -> Result<StatusContext, Box<dyn std::error::Error>> {
- let cwd = env::current_dir()?;
- let loader = ConfigLoader::default_for(&cwd);
- let discovered_config_files = loader.discover().len();
- let runtime_config = loader.load()?;
- let project_context = ProjectContext::discover_with_git(&cwd, DEFAULT_DATE)?;
- let (project_root, git_branch) =
- parse_git_status_metadata(project_context.git_status.as_deref());
- Ok(StatusContext {
- cwd,
- session_path: session_path.map(Path::to_path_buf),
- loaded_config_files: runtime_config.loaded_entries().len(),
- discovered_config_files,
- memory_file_count: project_context.instruction_files.len(),
- project_root,
- git_branch,
- })
- }
- fn format_status_report(
- model: &str,
- usage: StatusUsage,
- permission_mode: &str,
- context: &StatusContext,
- ) -> String {
- [
- format!(
- "Status
- Model {model}
- Permission mode {permission_mode}
- Messages {}
- Turns {}
- Estimated tokens {}",
- usage.message_count, usage.turns, usage.estimated_tokens,
- ),
- format!(
- "Usage
- Latest total {}
- Cumulative input {}
- Cumulative output {}
- Cumulative total {}",
- usage.latest.total_tokens(),
- usage.cumulative.input_tokens,
- usage.cumulative.output_tokens,
- usage.cumulative.total_tokens(),
- ),
- format!(
- "Workspace
- Cwd {}
- Project root {}
- Git branch {}
- Session {}
- Config files loaded {}/{}
- Memory files {}",
- context.cwd.display(),
- context
- .project_root
- .as_ref()
- .map_or_else(|| "unknown".to_string(), |path| path.display().to_string()),
- context.git_branch.as_deref().unwrap_or("unknown"),
- context.session_path.as_ref().map_or_else(
- || "live-repl".to_string(),
- |path| path.display().to_string()
- ),
- context.loaded_config_files,
- context.discovered_config_files,
- context.memory_file_count,
- ),
- ]
- .join(
- "
- ",
- )
- }
- fn render_config_report(section: Option<&str>) -> Result<String, Box<dyn std::error::Error>> {
- let cwd = env::current_dir()?;
- let loader = ConfigLoader::default_for(&cwd);
- let discovered = loader.discover();
- let runtime_config = loader.load()?;
- let mut lines = vec![
- format!(
- "Config
- Working directory {}
- Loaded files {}
- Merged keys {}",
- cwd.display(),
- runtime_config.loaded_entries().len(),
- runtime_config.merged().len()
- ),
- "Discovered files".to_string(),
- ];
- for entry in discovered {
- let source = match entry.source {
- ConfigSource::User => "user",
- ConfigSource::Project => "project",
- ConfigSource::Local => "local",
- };
- let status = if runtime_config
- .loaded_entries()
- .iter()
- .any(|loaded_entry| loaded_entry.path == entry.path)
- {
- "loaded"
- } else {
- "missing"
- };
- lines.push(format!(
- " {source:<7} {status:<7} {}",
- entry.path.display()
- ));
- }
- if let Some(section) = section {
- lines.push(format!("Merged section: {section}"));
- let value = match section {
- "env" => runtime_config.get("env"),
- "hooks" => runtime_config.get("hooks"),
- "model" => runtime_config.get("model"),
- other => {
- lines.push(format!(
- " Unsupported config section '{other}'. Use env, hooks, or model."
- ));
- return Ok(lines.join(
- "
- ",
- ));
- }
- };
- lines.push(format!(
- " {}",
- match value {
- Some(value) => value.render(),
- None => "<unset>".to_string(),
- }
- ));
- return Ok(lines.join(
- "
- ",
- ));
- }
- lines.push("Merged JSON".to_string());
- lines.push(format!(" {}", runtime_config.as_json().render()));
- Ok(lines.join(
- "
- ",
- ))
- }
- fn render_memory_report() -> Result<String, Box<dyn std::error::Error>> {
- let cwd = env::current_dir()?;
- let project_context = ProjectContext::discover(&cwd, DEFAULT_DATE)?;
- let mut lines = vec![format!(
- "Memory
- Working directory {}
- Instruction files {}",
- cwd.display(),
- project_context.instruction_files.len()
- )];
- if project_context.instruction_files.is_empty() {
- lines.push("Discovered files".to_string());
- lines.push(
- " No CLAUDE instruction files discovered in the current directory ancestry."
- .to_string(),
- );
- } else {
- lines.push("Discovered files".to_string());
- for (index, file) in project_context.instruction_files.iter().enumerate() {
- let preview = file.content.lines().next().unwrap_or("").trim();
- let preview = if preview.is_empty() {
- "<empty>"
- } else {
- preview
- };
- lines.push(format!(" {}. {}", index + 1, file.path.display(),));
- lines.push(format!(
- " lines={} preview={}",
- file.content.lines().count(),
- preview
- ));
- }
- }
- Ok(lines.join(
- "
- ",
- ))
- }
- fn init_claude_md() -> Result<String, Box<dyn std::error::Error>> {
- let cwd = env::current_dir()?;
- Ok(initialize_repo(&cwd)?.render())
- }
- fn run_init() -> Result<(), Box<dyn std::error::Error>> {
- println!("{}", init_claude_md()?);
- Ok(())
- }
- fn normalize_permission_mode(mode: &str) -> Option<&'static str> {
- match mode.trim() {
- "read-only" => Some("read-only"),
- "workspace-write" => Some("workspace-write"),
- "danger-full-access" => Some("danger-full-access"),
- _ => None,
- }
- }
- fn render_diff_report() -> Result<String, Box<dyn std::error::Error>> {
- let output = std::process::Command::new("git")
- .args(["diff", "--", ":(exclude).omx"])
- .current_dir(env::current_dir()?)
- .output()?;
- if !output.status.success() {
- let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
- return Err(format!("git diff failed: {stderr}").into());
- }
- let diff = String::from_utf8(output.stdout)?;
- if diff.trim().is_empty() {
- return Ok(
- "Diff\n Result clean working tree\n Detail no current changes"
- .to_string(),
- );
- }
- Ok(format!("Diff\n\n{}", diff.trim_end()))
- }
- fn render_version_report() -> String {
- let git_sha = GIT_SHA.unwrap_or("unknown");
- let target = BUILD_TARGET.unwrap_or("unknown");
- format!(
- "Claw Code\n Version {VERSION}\n Git SHA {git_sha}\n Target {target}\n Build date {DEFAULT_DATE}"
- )
- }
- fn render_export_text(session: &Session) -> String {
- let mut lines = vec!["# Conversation Export".to_string(), String::new()];
- for (index, message) in session.messages.iter().enumerate() {
- let role = match message.role {
- MessageRole::System => "system",
- MessageRole::User => "user",
- MessageRole::Assistant => "assistant",
- MessageRole::Tool => "tool",
- };
- lines.push(format!("## {}. {role}", index + 1));
- for block in &message.blocks {
- match block {
- ContentBlock::Text { text } => lines.push(text.clone()),
- ContentBlock::ToolUse { id, name, input } => {
- lines.push(format!("[tool_use id={id} name={name}] {input}"));
- }
- ContentBlock::ToolResult {
- tool_use_id,
- tool_name,
- output,
- is_error,
- } => {
- lines.push(format!(
- "[tool_result id={tool_use_id} name={tool_name} error={is_error}] {output}"
- ));
- }
- }
- }
- lines.push(String::new());
- }
- lines.join("\n")
- }
- fn default_export_filename(session: &Session) -> String {
- let stem = session
- .messages
- .iter()
- .find_map(|message| match message.role {
- MessageRole::User => message.blocks.iter().find_map(|block| match block {
- ContentBlock::Text { text } => Some(text.as_str()),
- _ => None,
- }),
- _ => None,
- })
- .map_or("conversation", |text| {
- text.lines().next().unwrap_or("conversation")
- })
- .chars()
- .map(|ch| {
- if ch.is_ascii_alphanumeric() {
- ch.to_ascii_lowercase()
- } else {
- '-'
- }
- })
- .collect::<String>()
- .split('-')
- .filter(|part| !part.is_empty())
- .take(8)
- .collect::<Vec<_>>()
- .join("-");
- let fallback = if stem.is_empty() {
- "conversation"
- } else {
- &stem
- };
- format!("{fallback}.txt")
- }
- fn resolve_export_path(
- requested_path: Option<&str>,
- session: &Session,
- ) -> Result<PathBuf, Box<dyn std::error::Error>> {
- let cwd = env::current_dir()?;
- let file_name =
- requested_path.map_or_else(|| default_export_filename(session), ToOwned::to_owned);
- let final_name = if Path::new(&file_name)
- .extension()
- .is_some_and(|ext| ext.eq_ignore_ascii_case("txt"))
- {
- file_name
- } else {
- format!("{file_name}.txt")
- };
- Ok(cwd.join(final_name))
- }
- fn build_system_prompt() -> Result<Vec<String>, Box<dyn std::error::Error>> {
- Ok(load_system_prompt(
- env::current_dir()?,
- DEFAULT_DATE,
- env::consts::OS,
- "unknown",
- )?)
- }
- fn build_runtime(
- session: Session,
- model: String,
- system_prompt: Vec<String>,
- enable_tools: bool,
- emit_output: bool,
- allowed_tools: Option<AllowedToolSet>,
- permission_mode: PermissionMode,
- ) -> Result<ConversationRuntime<AnthropicRuntimeClient, CliToolExecutor>, Box<dyn std::error::Error>>
- {
- Ok(ConversationRuntime::new(
- session,
- AnthropicRuntimeClient::new(model, enable_tools, emit_output, allowed_tools.clone())?,
- CliToolExecutor::new(allowed_tools, emit_output),
- permission_policy(permission_mode),
- system_prompt,
- ))
- }
- struct CliPermissionPrompter {
- current_mode: PermissionMode,
- }
- impl CliPermissionPrompter {
- fn new(current_mode: PermissionMode) -> Self {
- Self { current_mode }
- }
- }
- impl runtime::PermissionPrompter for CliPermissionPrompter {
- fn decide(
- &mut self,
- request: &runtime::PermissionRequest,
- ) -> runtime::PermissionPromptDecision {
- println!();
- println!("Permission approval required");
- println!(" Tool {}", request.tool_name);
- println!(" Current mode {}", self.current_mode.as_str());
- println!(" Required mode {}", request.required_mode.as_str());
- println!(" Input {}", request.input);
- print!("Approve this tool call? [y/N]: ");
- let _ = io::stdout().flush();
- let mut response = String::new();
- match io::stdin().read_line(&mut response) {
- Ok(_) => {
- let normalized = response.trim().to_ascii_lowercase();
- if matches!(normalized.as_str(), "y" | "yes") {
- runtime::PermissionPromptDecision::Allow
- } else {
- runtime::PermissionPromptDecision::Deny {
- reason: format!(
- "tool '{}' denied by user approval prompt",
- request.tool_name
- ),
- }
- }
- }
- Err(error) => runtime::PermissionPromptDecision::Deny {
- reason: format!("permission approval failed: {error}"),
- },
- }
- }
- }
- struct AnthropicRuntimeClient {
- runtime: tokio::runtime::Runtime,
- client: AnthropicClient,
- model: String,
- enable_tools: bool,
- emit_output: bool,
- allowed_tools: Option<AllowedToolSet>,
- }
- impl AnthropicRuntimeClient {
- fn new(
- model: String,
- enable_tools: bool,
- emit_output: bool,
- allowed_tools: Option<AllowedToolSet>,
- ) -> Result<Self, Box<dyn std::error::Error>> {
- Ok(Self {
- runtime: tokio::runtime::Runtime::new()?,
- client: AnthropicClient::from_auth(resolve_cli_auth_source()?)
- .with_base_url(api::read_base_url()),
- model,
- enable_tools,
- emit_output,
- allowed_tools,
- })
- }
- }
- fn resolve_cli_auth_source() -> Result<AuthSource, Box<dyn std::error::Error>> {
- Ok(resolve_startup_auth_source(|| {
- let cwd = env::current_dir().map_err(api::ApiError::from)?;
- let config = ConfigLoader::default_for(&cwd).load().map_err(|error| {
- api::ApiError::Auth(format!("failed to load runtime OAuth config: {error}"))
- })?;
- Ok(config.oauth().cloned())
- })?)
- }
- impl ApiClient for AnthropicRuntimeClient {
- #[allow(clippy::too_many_lines)]
- fn stream(&mut self, request: ApiRequest) -> Result<Vec<AssistantEvent>, RuntimeError> {
- let message_request = MessageRequest {
- model: self.model.clone(),
- max_tokens: max_tokens_for_model(&self.model),
- messages: convert_messages(&request.messages),
- system: (!request.system_prompt.is_empty()).then(|| request.system_prompt.join("\n\n")),
- tools: self.enable_tools.then(|| {
- filter_tool_specs(self.allowed_tools.as_ref())
- .into_iter()
- .map(|spec| ToolDefinition {
- name: spec.name.to_string(),
- description: Some(spec.description.to_string()),
- input_schema: spec.input_schema,
- })
- .collect()
- }),
- tool_choice: self.enable_tools.then_some(ToolChoice::Auto),
- stream: true,
- };
- self.runtime.block_on(async {
- let mut stream = self
- .client
- .stream_message(&message_request)
- .await
- .map_err(|error| RuntimeError::new(error.to_string()))?;
- let mut stdout = io::stdout();
- let mut sink = io::sink();
- let out: &mut dyn Write = if self.emit_output {
- &mut stdout
- } else {
- &mut sink
- };
- let renderer = TerminalRenderer::new();
- let mut markdown_stream = MarkdownStreamState::default();
- let mut events = Vec::new();
- let mut pending_tool: Option<(String, String, String)> = None;
- let mut saw_stop = false;
- while let Some(event) = stream
- .next_event()
- .await
- .map_err(|error| RuntimeError::new(error.to_string()))?
- {
- match event {
- ApiStreamEvent::MessageStart(start) => {
- for block in start.message.content {
- push_output_block(block, out, &mut events, &mut pending_tool, true)?;
- }
- }
- ApiStreamEvent::ContentBlockStart(start) => {
- push_output_block(
- start.content_block,
- out,
- &mut events,
- &mut pending_tool,
- true,
- )?;
- }
- ApiStreamEvent::ContentBlockDelta(delta) => match delta.delta {
- ContentBlockDelta::TextDelta { text } => {
- if !text.is_empty() {
- if let Some(rendered) = markdown_stream.push(&renderer, &text) {
- write!(out, "{rendered}")
- .and_then(|()| out.flush())
- .map_err(|error| RuntimeError::new(error.to_string()))?;
- }
- events.push(AssistantEvent::TextDelta(text));
- }
- }
- ContentBlockDelta::InputJsonDelta { partial_json } => {
- if let Some((_, _, input)) = &mut pending_tool {
- input.push_str(&partial_json);
- }
- }
- },
- ApiStreamEvent::ContentBlockStop(_) => {
- if let Some(rendered) = markdown_stream.flush(&renderer) {
- write!(out, "{rendered}")
- .and_then(|()| out.flush())
- .map_err(|error| RuntimeError::new(error.to_string()))?;
- }
- if let Some((id, name, input)) = pending_tool.take() {
- // Display tool call now that input is fully accumulated
- writeln!(out, "\n{}", format_tool_call_start(&name, &input))
- .and_then(|()| out.flush())
- .map_err(|error| RuntimeError::new(error.to_string()))?;
- events.push(AssistantEvent::ToolUse { id, name, input });
- }
- }
- ApiStreamEvent::MessageDelta(delta) => {
- events.push(AssistantEvent::Usage(TokenUsage {
- input_tokens: delta.usage.input_tokens,
- output_tokens: delta.usage.output_tokens,
- cache_creation_input_tokens: 0,
- cache_read_input_tokens: 0,
- }));
- }
- ApiStreamEvent::MessageStop(_) => {
- saw_stop = true;
- if let Some(rendered) = markdown_stream.flush(&renderer) {
- write!(out, "{rendered}")
- .and_then(|()| out.flush())
- .map_err(|error| RuntimeError::new(error.to_string()))?;
- }
- events.push(AssistantEvent::MessageStop);
- }
- }
- }
- if !saw_stop
- && events.iter().any(|event| {
- matches!(event, AssistantEvent::TextDelta(text) if !text.is_empty())
- || matches!(event, AssistantEvent::ToolUse { .. })
- })
- {
- events.push(AssistantEvent::MessageStop);
- }
- if events
- .iter()
- .any(|event| matches!(event, AssistantEvent::MessageStop))
- {
- return Ok(events);
- }
- let response = self
- .client
- .send_message(&MessageRequest {
- stream: false,
- ..message_request.clone()
- })
- .await
- .map_err(|error| RuntimeError::new(error.to_string()))?;
- response_to_events(response, out)
- })
- }
- }
- fn final_assistant_text(summary: &runtime::TurnSummary) -> String {
- summary
- .assistant_messages
- .last()
- .map(|message| {
- message
- .blocks
- .iter()
- .filter_map(|block| match block {
- ContentBlock::Text { text } => Some(text.as_str()),
- _ => None,
- })
- .collect::<Vec<_>>()
- .join("")
- })
- .unwrap_or_default()
- }
- fn collect_tool_uses(summary: &runtime::TurnSummary) -> Vec<serde_json::Value> {
- summary
- .assistant_messages
- .iter()
- .flat_map(|message| message.blocks.iter())
- .filter_map(|block| match block {
- ContentBlock::ToolUse { id, name, input } => Some(json!({
- "id": id,
- "name": name,
- "input": input,
- })),
- _ => None,
- })
- .collect()
- }
- fn collect_tool_results(summary: &runtime::TurnSummary) -> Vec<serde_json::Value> {
- summary
- .tool_results
- .iter()
- .flat_map(|message| message.blocks.iter())
- .filter_map(|block| match block {
- ContentBlock::ToolResult {
- tool_use_id,
- tool_name,
- output,
- is_error,
- } => Some(json!({
- "tool_use_id": tool_use_id,
- "tool_name": tool_name,
- "output": output,
- "is_error": is_error,
- })),
- _ => None,
- })
- .collect()
- }
- fn slash_command_completion_candidates() -> Vec<String> {
- slash_command_specs()
- .iter()
- .map(|spec| format!("/{}", spec.name))
- .collect()
- }
- fn format_tool_call_start(name: &str, input: &str) -> String {
- let parsed: serde_json::Value =
- serde_json::from_str(input).unwrap_or(serde_json::Value::String(input.to_string()));
- let detail = match name {
- "bash" | "Bash" => format_bash_call(&parsed),
- "read_file" | "Read" => {
- let path = extract_tool_path(&parsed);
- format!("\x1b[2m📄 Reading {path}…\x1b[0m")
- }
- "write_file" | "Write" => {
- let path = extract_tool_path(&parsed);
- let lines = parsed
- .get("content")
- .and_then(|value| value.as_str())
- .map_or(0, |content| content.lines().count());
- format!("\x1b[1;32m✏️ Writing {path}\x1b[0m \x1b[2m({lines} lines)\x1b[0m")
- }
- "edit_file" | "Edit" => {
- let path = extract_tool_path(&parsed);
- let old_value = parsed
- .get("old_string")
- .or_else(|| parsed.get("oldString"))
- .and_then(|value| value.as_str())
- .unwrap_or_default();
- let new_value = parsed
- .get("new_string")
- .or_else(|| parsed.get("newString"))
- .and_then(|value| value.as_str())
- .unwrap_or_default();
- format!(
- "\x1b[1;33m📝 Editing {path}\x1b[0m{}",
- format_patch_preview(old_value, new_value)
- .map(|preview| format!("\n{preview}"))
- .unwrap_or_default()
- )
- }
- "glob_search" | "Glob" => format_search_start("🔎 Glob", &parsed),
- "grep_search" | "Grep" => format_search_start("🔎 Grep", &parsed),
- "web_search" | "WebSearch" => parsed
- .get("query")
- .and_then(|value| value.as_str())
- .unwrap_or("?")
- .to_string(),
- _ => summarize_tool_payload(input),
- };
- let border = "─".repeat(name.len() + 8);
- format!(
- "\x1b[38;5;245m╭─ \x1b[1;36m{name}\x1b[0;38;5;245m ─╮\x1b[0m\n\x1b[38;5;245m│\x1b[0m {detail}\n\x1b[38;5;245m╰{border}╯\x1b[0m"
- )
- }
- fn format_tool_result(name: &str, output: &str, is_error: bool) -> String {
- let icon = if is_error {
- "\x1b[1;31m✗\x1b[0m"
- } else {
- "\x1b[1;32m✓\x1b[0m"
- };
- if is_error {
- let summary = truncate_for_summary(output.trim(), 160);
- return if summary.is_empty() {
- format!("{icon} \x1b[38;5;245m{name}\x1b[0m")
- } else {
- format!("{icon} \x1b[38;5;245m{name}\x1b[0m\n\x1b[38;5;203m{summary}\x1b[0m")
- };
- }
- let parsed: serde_json::Value =
- serde_json::from_str(output).unwrap_or(serde_json::Value::String(output.to_string()));
- match name {
- "bash" | "Bash" => format_bash_result(icon, &parsed),
- "read_file" | "Read" => format_read_result(icon, &parsed),
- "write_file" | "Write" => format_write_result(icon, &parsed),
- "edit_file" | "Edit" => format_edit_result(icon, &parsed),
- "glob_search" | "Glob" => format_glob_result(icon, &parsed),
- "grep_search" | "Grep" => format_grep_result(icon, &parsed),
- _ => {
- let summary = truncate_for_summary(output.trim(), 200);
- format!("{icon} \x1b[38;5;245m{name}:\x1b[0m {summary}")
- }
- }
- }
- fn extract_tool_path(parsed: &serde_json::Value) -> String {
- parsed
- .get("file_path")
- .or_else(|| parsed.get("filePath"))
- .or_else(|| parsed.get("path"))
- .and_then(|value| value.as_str())
- .unwrap_or("?")
- .to_string()
- }
- fn format_search_start(label: &str, parsed: &serde_json::Value) -> String {
- let pattern = parsed
- .get("pattern")
- .and_then(|value| value.as_str())
- .unwrap_or("?");
- let scope = parsed
- .get("path")
- .and_then(|value| value.as_str())
- .unwrap_or(".");
- format!("{label} {pattern}\n\x1b[2min {scope}\x1b[0m")
- }
- fn format_patch_preview(old_value: &str, new_value: &str) -> Option<String> {
- if old_value.is_empty() && new_value.is_empty() {
- return None;
- }
- Some(format!(
- "\x1b[38;5;203m- {}\x1b[0m\n\x1b[38;5;70m+ {}\x1b[0m",
- truncate_for_summary(first_visible_line(old_value), 72),
- truncate_for_summary(first_visible_line(new_value), 72)
- ))
- }
- fn format_bash_call(parsed: &serde_json::Value) -> String {
- let command = parsed
- .get("command")
- .and_then(|value| value.as_str())
- .unwrap_or_default();
- if command.is_empty() {
- String::new()
- } else {
- format!(
- "\x1b[48;5;236;38;5;255m $ {} \x1b[0m",
- truncate_for_summary(command, 160)
- )
- }
- }
- fn first_visible_line(text: &str) -> &str {
- text.lines()
- .find(|line| !line.trim().is_empty())
- .unwrap_or(text)
- }
- fn format_bash_result(icon: &str, parsed: &serde_json::Value) -> String {
- let mut lines = vec![format!("{icon} \x1b[38;5;245mbash\x1b[0m")];
- if let Some(task_id) = parsed
- .get("backgroundTaskId")
- .and_then(|value| value.as_str())
- {
- lines[0].push_str(&format!(" backgrounded ({task_id})"));
- } else if let Some(status) = parsed
- .get("returnCodeInterpretation")
- .and_then(|value| value.as_str())
- .filter(|status| !status.is_empty())
- {
- lines[0].push_str(&format!(" {status}"));
- }
- if let Some(stdout) = parsed.get("stdout").and_then(|value| value.as_str()) {
- if !stdout.trim().is_empty() {
- lines.push(stdout.trim_end().to_string());
- }
- }
- if let Some(stderr) = parsed.get("stderr").and_then(|value| value.as_str()) {
- if !stderr.trim().is_empty() {
- lines.push(format!("\x1b[38;5;203m{}\x1b[0m", stderr.trim_end()));
- }
- }
- lines.join("\n\n")
- }
- fn format_read_result(icon: &str, parsed: &serde_json::Value) -> String {
- let file = parsed.get("file").unwrap_or(parsed);
- let path = extract_tool_path(file);
- let start_line = file
- .get("startLine")
- .and_then(|value| value.as_u64())
- .unwrap_or(1);
- let num_lines = file
- .get("numLines")
- .and_then(|value| value.as_u64())
- .unwrap_or(0);
- let total_lines = file
- .get("totalLines")
- .and_then(|value| value.as_u64())
- .unwrap_or(num_lines);
- let content = file
- .get("content")
- .and_then(|value| value.as_str())
- .unwrap_or_default();
- let end_line = start_line.saturating_add(num_lines.saturating_sub(1));
- format!(
- "{icon} \x1b[2m📄 Read {path} (lines {}-{} of {})\x1b[0m\n{}",
- start_line,
- end_line.max(start_line),
- total_lines,
- content
- )
- }
- fn format_write_result(icon: &str, parsed: &serde_json::Value) -> String {
- let path = extract_tool_path(parsed);
- let kind = parsed
- .get("type")
- .and_then(|value| value.as_str())
- .unwrap_or("write");
- let line_count = parsed
- .get("content")
- .and_then(|value| value.as_str())
- .map(|content| content.lines().count())
- .unwrap_or(0);
- format!(
- "{icon} \x1b[1;32m✏️ {} {path}\x1b[0m \x1b[2m({line_count} lines)\x1b[0m",
- if kind == "create" { "Wrote" } else { "Updated" },
- )
- }
- fn format_structured_patch_preview(parsed: &serde_json::Value) -> Option<String> {
- let hunks = parsed.get("structuredPatch")?.as_array()?;
- let mut preview = Vec::new();
- for hunk in hunks.iter().take(2) {
- let lines = hunk.get("lines")?.as_array()?;
- for line in lines.iter().filter_map(|value| value.as_str()).take(6) {
- match line.chars().next() {
- Some('+') => preview.push(format!("\x1b[38;5;70m{line}\x1b[0m")),
- Some('-') => preview.push(format!("\x1b[38;5;203m{line}\x1b[0m")),
- _ => preview.push(line.to_string()),
- }
- }
- }
- if preview.is_empty() {
- None
- } else {
- Some(preview.join("\n"))
- }
- }
- fn format_edit_result(icon: &str, parsed: &serde_json::Value) -> String {
- let path = extract_tool_path(parsed);
- let suffix = if parsed
- .get("replaceAll")
- .and_then(|value| value.as_bool())
- .unwrap_or(false)
- {
- " (replace all)"
- } else {
- ""
- };
- let preview = format_structured_patch_preview(parsed).or_else(|| {
- let old_value = parsed
- .get("oldString")
- .and_then(|value| value.as_str())
- .unwrap_or_default();
- let new_value = parsed
- .get("newString")
- .and_then(|value| value.as_str())
- .unwrap_or_default();
- format_patch_preview(old_value, new_value)
- });
- match preview {
- Some(preview) => format!("{icon} \x1b[1;33m📝 Edited {path}{suffix}\x1b[0m\n{preview}"),
- None => format!("{icon} \x1b[1;33m📝 Edited {path}{suffix}\x1b[0m"),
- }
- }
- fn format_glob_result(icon: &str, parsed: &serde_json::Value) -> String {
- let num_files = parsed
- .get("numFiles")
- .and_then(|value| value.as_u64())
- .unwrap_or(0);
- let filenames = parsed
- .get("filenames")
- .and_then(|value| value.as_array())
- .map(|files| {
- files
- .iter()
- .filter_map(|value| value.as_str())
- .take(8)
- .collect::<Vec<_>>()
- .join("\n")
- })
- .unwrap_or_default();
- if filenames.is_empty() {
- format!("{icon} \x1b[38;5;245mglob_search\x1b[0m matched {num_files} files")
- } else {
- format!("{icon} \x1b[38;5;245mglob_search\x1b[0m matched {num_files} files\n{filenames}")
- }
- }
- fn format_grep_result(icon: &str, parsed: &serde_json::Value) -> String {
- let num_matches = parsed
- .get("numMatches")
- .and_then(|value| value.as_u64())
- .unwrap_or(0);
- let num_files = parsed
- .get("numFiles")
- .and_then(|value| value.as_u64())
- .unwrap_or(0);
- let content = parsed
- .get("content")
- .and_then(|value| value.as_str())
- .unwrap_or_default();
- let filenames = parsed
- .get("filenames")
- .and_then(|value| value.as_array())
- .map(|files| {
- files
- .iter()
- .filter_map(|value| value.as_str())
- .take(8)
- .collect::<Vec<_>>()
- .join("\n")
- })
- .unwrap_or_default();
- let summary = format!(
- "{icon} \x1b[38;5;245mgrep_search\x1b[0m {num_matches} matches across {num_files} files"
- );
- if !content.trim().is_empty() {
- format!("{summary}\n{}", content.trim_end())
- } else if !filenames.is_empty() {
- format!("{summary}\n{filenames}")
- } else {
- summary
- }
- }
- fn summarize_tool_payload(payload: &str) -> String {
- let compact = match serde_json::from_str::<serde_json::Value>(payload) {
- Ok(value) => value.to_string(),
- Err(_) => payload.trim().to_string(),
- };
- truncate_for_summary(&compact, 96)
- }
- fn truncate_for_summary(value: &str, limit: usize) -> String {
- let mut chars = value.chars();
- let truncated = chars.by_ref().take(limit).collect::<String>();
- if chars.next().is_some() {
- format!("{truncated}…")
- } else {
- truncated
- }
- }
- fn push_output_block(
- block: OutputContentBlock,
- out: &mut (impl Write + ?Sized),
- events: &mut Vec<AssistantEvent>,
- pending_tool: &mut Option<(String, String, String)>,
- streaming_tool_input: bool,
- ) -> Result<(), RuntimeError> {
- match block {
- OutputContentBlock::Text { text } => {
- if !text.is_empty() {
- let rendered = TerminalRenderer::new().markdown_to_ansi(&text);
- write!(out, "{rendered}")
- .and_then(|()| out.flush())
- .map_err(|error| RuntimeError::new(error.to_string()))?;
- events.push(AssistantEvent::TextDelta(text));
- }
- }
- OutputContentBlock::ToolUse { id, name, input } => {
- // During streaming, the initial content_block_start has an empty input ({}).
- // The real input arrives via input_json_delta events. In
- // non-streaming responses, preserve a legitimate empty object.
- let initial_input = if streaming_tool_input
- && input.is_object()
- && input.as_object().is_some_and(serde_json::Map::is_empty)
- {
- String::new()
- } else {
- input.to_string()
- };
- *pending_tool = Some((id, name, initial_input));
- }
- }
- Ok(())
- }
- fn response_to_events(
- response: MessageResponse,
- out: &mut (impl Write + ?Sized),
- ) -> Result<Vec<AssistantEvent>, RuntimeError> {
- let mut events = Vec::new();
- let mut pending_tool = None;
- for block in response.content {
- push_output_block(block, out, &mut events, &mut pending_tool, false)?;
- if let Some((id, name, input)) = pending_tool.take() {
- events.push(AssistantEvent::ToolUse { id, name, input });
- }
- }
- events.push(AssistantEvent::Usage(TokenUsage {
- input_tokens: response.usage.input_tokens,
- output_tokens: response.usage.output_tokens,
- cache_creation_input_tokens: response.usage.cache_creation_input_tokens,
- cache_read_input_tokens: response.usage.cache_read_input_tokens,
- }));
- events.push(AssistantEvent::MessageStop);
- Ok(events)
- }
- struct CliToolExecutor {
- renderer: TerminalRenderer,
- emit_output: bool,
- allowed_tools: Option<AllowedToolSet>,
- }
- impl CliToolExecutor {
- fn new(allowed_tools: Option<AllowedToolSet>, emit_output: bool) -> Self {
- Self {
- renderer: TerminalRenderer::new(),
- emit_output,
- allowed_tools,
- }
- }
- }
- impl ToolExecutor for CliToolExecutor {
- fn execute(&mut self, tool_name: &str, input: &str) -> Result<String, ToolError> {
- if self
- .allowed_tools
- .as_ref()
- .is_some_and(|allowed| !allowed.contains(tool_name))
- {
- return Err(ToolError::new(format!(
- "tool `{tool_name}` is not enabled by the current --allowedTools setting"
- )));
- }
- let value = serde_json::from_str(input)
- .map_err(|error| ToolError::new(format!("invalid tool input JSON: {error}")))?;
- match execute_tool(tool_name, &value) {
- Ok(output) => {
- if self.emit_output {
- let markdown = format_tool_result(tool_name, &output, false);
- self.renderer
- .stream_markdown(&markdown, &mut io::stdout())
- .map_err(|error| ToolError::new(error.to_string()))?;
- }
- Ok(output)
- }
- Err(error) => {
- if self.emit_output {
- let markdown = format_tool_result(tool_name, &error, true);
- self.renderer
- .stream_markdown(&markdown, &mut io::stdout())
- .map_err(|stream_error| ToolError::new(stream_error.to_string()))?;
- }
- Err(ToolError::new(error))
- }
- }
- }
- }
- fn permission_policy(mode: PermissionMode) -> PermissionPolicy {
- tool_permission_specs()
- .into_iter()
- .fold(PermissionPolicy::new(mode), |policy, spec| {
- policy.with_tool_requirement(spec.name, spec.required_permission)
- })
- }
- fn tool_permission_specs() -> Vec<ToolSpec> {
- mvp_tool_specs()
- }
- fn convert_messages(messages: &[ConversationMessage]) -> Vec<InputMessage> {
- messages
- .iter()
- .filter_map(|message| {
- let role = match message.role {
- MessageRole::System | MessageRole::User | MessageRole::Tool => "user",
- MessageRole::Assistant => "assistant",
- };
- let content = message
- .blocks
- .iter()
- .map(|block| match block {
- ContentBlock::Text { text } => InputContentBlock::Text { text: text.clone() },
- ContentBlock::ToolUse { id, name, input } => InputContentBlock::ToolUse {
- id: id.clone(),
- name: name.clone(),
- input: serde_json::from_str(input)
- .unwrap_or_else(|_| serde_json::json!({ "raw": input })),
- },
- ContentBlock::ToolResult {
- tool_use_id,
- output,
- is_error,
- ..
- } => InputContentBlock::ToolResult {
- tool_use_id: tool_use_id.clone(),
- content: vec![ToolResultContentBlock::Text {
- text: output.clone(),
- }],
- is_error: *is_error,
- },
- })
- .collect::<Vec<_>>();
- (!content.is_empty()).then(|| InputMessage {
- role: role.to_string(),
- content,
- })
- })
- .collect()
- }
- fn print_help_to(out: &mut impl Write) -> io::Result<()> {
- writeln!(out, "claw v{VERSION}")?;
- writeln!(out)?;
- writeln!(out, "Usage:")?;
- writeln!(
- out,
- " claw [--model MODEL] [--allowedTools TOOL[,TOOL...]]"
- )?;
- writeln!(out, " Start the interactive REPL")?;
- writeln!(
- out,
- " claw [--model MODEL] [--output-format text|json] prompt TEXT"
- )?;
- writeln!(out, " Send one prompt and exit")?;
- writeln!(
- out,
- " claw [--model MODEL] [--output-format text|json] TEXT"
- )?;
- writeln!(out, " Shorthand non-interactive prompt mode")?;
- writeln!(
- out,
- " claw --resume SESSION.json [/status] [/compact] [...]"
- )?;
- writeln!(
- out,
- " Inspect or maintain a saved session without entering the REPL"
- )?;
- writeln!(out, " claw dump-manifests")?;
- writeln!(out, " claw bootstrap-plan")?;
- writeln!(out, " claw system-prompt [--cwd PATH] [--date YYYY-MM-DD]")?;
- writeln!(out, " claw login")?;
- writeln!(out, " claw logout")?;
- writeln!(out, " claw init")?;
- writeln!(out)?;
- writeln!(out, "Flags:")?;
- writeln!(
- out,
- " --model MODEL Override the active model"
- )?;
- writeln!(
- out,
- " --output-format FORMAT Non-interactive output format: text or json"
- )?;
- writeln!(
- out,
- " --permission-mode MODE Set read-only, workspace-write, or danger-full-access"
- )?;
- writeln!(
- out,
- " --dangerously-skip-permissions Skip all permission checks"
- )?;
- writeln!(out, " --allowedTools TOOLS Restrict enabled tools (repeatable; comma-separated aliases supported)")?;
- writeln!(
- out,
- " --version, -V Print version and build information locally"
- )?;
- writeln!(out)?;
- writeln!(out, "Interactive slash commands:")?;
- writeln!(out, "{}", render_slash_command_help())?;
- writeln!(out)?;
- let resume_commands = resume_supported_slash_commands()
- .into_iter()
- .map(|spec| match spec.argument_hint {
- Some(argument_hint) => format!("/{} {}", spec.name, argument_hint),
- None => format!("/{}", spec.name),
- })
- .collect::<Vec<_>>()
- .join(", ");
- writeln!(out, "Resume-safe commands: {resume_commands}")?;
- writeln!(out, "Examples:")?;
- writeln!(out, " claw --model claude-opus \"summarize this repo\"")?;
- writeln!(
- out,
- " claw --output-format json prompt \"explain src/main.rs\""
- )?;
- writeln!(
- out,
- " claw --allowedTools read,glob \"summarize Cargo.toml\""
- )?;
- writeln!(
- out,
- " claw --resume session.json /status /diff /export notes.txt"
- )?;
- writeln!(out, " claw login")?;
- writeln!(out, " claw init")?;
- Ok(())
- }
- fn print_help() {
- let _ = print_help_to(&mut io::stdout());
- }
- #[cfg(test)]
- mod tests {
- use super::{
- filter_tool_specs, format_compact_report, format_cost_report, format_model_report,
- format_model_switch_report, format_permissions_report, format_permissions_switch_report,
- format_resume_report, format_status_report, format_tool_call_start, format_tool_result,
- normalize_permission_mode, parse_args, parse_git_status_metadata, print_help_to,
- push_output_block, render_config_report, render_memory_report, render_repl_help,
- resolve_model_alias, response_to_events, resume_supported_slash_commands, status_context,
- CliAction, CliOutputFormat, SlashCommand, StatusUsage, DEFAULT_MODEL,
- };
- use api::{MessageResponse, OutputContentBlock, Usage};
- use runtime::{AssistantEvent, ContentBlock, ConversationMessage, MessageRole, PermissionMode};
- use serde_json::json;
- use std::path::PathBuf;
- #[test]
- fn defaults_to_repl_when_no_args() {
- assert_eq!(
- parse_args(&[]).expect("args should parse"),
- CliAction::Repl {
- model: DEFAULT_MODEL.to_string(),
- allowed_tools: None,
- permission_mode: PermissionMode::DangerFullAccess,
- }
- );
- }
- #[test]
- fn parses_prompt_subcommand() {
- let args = vec![
- "prompt".to_string(),
- "hello".to_string(),
- "world".to_string(),
- ];
- assert_eq!(
- parse_args(&args).expect("args should parse"),
- CliAction::Prompt {
- prompt: "hello world".to_string(),
- model: DEFAULT_MODEL.to_string(),
- output_format: CliOutputFormat::Text,
- allowed_tools: None,
- permission_mode: PermissionMode::DangerFullAccess,
- }
- );
- }
- #[test]
- fn parses_bare_prompt_and_json_output_flag() {
- let args = vec![
- "--output-format=json".to_string(),
- "--model".to_string(),
- "claude-opus".to_string(),
- "explain".to_string(),
- "this".to_string(),
- ];
- assert_eq!(
- parse_args(&args).expect("args should parse"),
- CliAction::Prompt {
- prompt: "explain this".to_string(),
- model: "claude-opus".to_string(),
- output_format: CliOutputFormat::Json,
- allowed_tools: None,
- permission_mode: PermissionMode::DangerFullAccess,
- }
- );
- }
- #[test]
- fn resolves_model_aliases_in_args() {
- let args = vec![
- "--model".to_string(),
- "opus".to_string(),
- "explain".to_string(),
- "this".to_string(),
- ];
- assert_eq!(
- parse_args(&args).expect("args should parse"),
- CliAction::Prompt {
- prompt: "explain this".to_string(),
- model: "claude-opus-4-6".to_string(),
- output_format: CliOutputFormat::Text,
- allowed_tools: None,
- permission_mode: PermissionMode::DangerFullAccess,
- }
- );
- }
- #[test]
- fn resolves_known_model_aliases() {
- assert_eq!(resolve_model_alias("opus"), "claude-opus-4-6");
- assert_eq!(resolve_model_alias("sonnet"), "claude-sonnet-4-6");
- assert_eq!(resolve_model_alias("haiku"), "claude-haiku-4-5-20251213");
- assert_eq!(resolve_model_alias("claude-opus"), "claude-opus");
- }
- #[test]
- fn parses_version_flags_without_initializing_prompt_mode() {
- assert_eq!(
- parse_args(&["--version".to_string()]).expect("args should parse"),
- CliAction::Version
- );
- assert_eq!(
- parse_args(&["-V".to_string()]).expect("args should parse"),
- CliAction::Version
- );
- }
- #[test]
- fn parses_permission_mode_flag() {
- let args = vec!["--permission-mode=read-only".to_string()];
- assert_eq!(
- parse_args(&args).expect("args should parse"),
- CliAction::Repl {
- model: DEFAULT_MODEL.to_string(),
- allowed_tools: None,
- permission_mode: PermissionMode::ReadOnly,
- }
- );
- }
- #[test]
- fn parses_allowed_tools_flags_with_aliases_and_lists() {
- let args = vec![
- "--allowedTools".to_string(),
- "read,glob".to_string(),
- "--allowed-tools=write_file".to_string(),
- ];
- assert_eq!(
- parse_args(&args).expect("args should parse"),
- CliAction::Repl {
- model: DEFAULT_MODEL.to_string(),
- allowed_tools: Some(
- ["glob_search", "read_file", "write_file"]
- .into_iter()
- .map(str::to_string)
- .collect()
- ),
- permission_mode: PermissionMode::DangerFullAccess,
- }
- );
- }
- #[test]
- fn rejects_unknown_allowed_tools() {
- let error = parse_args(&["--allowedTools".to_string(), "teleport".to_string()])
- .expect_err("tool should be rejected");
- assert!(error.contains("unsupported tool in --allowedTools: teleport"));
- }
- #[test]
- fn parses_system_prompt_options() {
- let args = vec![
- "system-prompt".to_string(),
- "--cwd".to_string(),
- "/tmp/project".to_string(),
- "--date".to_string(),
- "2026-04-01".to_string(),
- ];
- assert_eq!(
- parse_args(&args).expect("args should parse"),
- CliAction::PrintSystemPrompt {
- cwd: PathBuf::from("/tmp/project"),
- date: "2026-04-01".to_string(),
- }
- );
- }
- #[test]
- fn parses_login_and_logout_subcommands() {
- assert_eq!(
- parse_args(&["login".to_string()]).expect("login should parse"),
- CliAction::Login
- );
- assert_eq!(
- parse_args(&["logout".to_string()]).expect("logout should parse"),
- CliAction::Logout
- );
- assert_eq!(
- parse_args(&["init".to_string()]).expect("init should parse"),
- CliAction::Init
- );
- }
- #[test]
- fn parses_resume_flag_with_slash_command() {
- let args = vec![
- "--resume".to_string(),
- "session.json".to_string(),
- "/compact".to_string(),
- ];
- assert_eq!(
- parse_args(&args).expect("args should parse"),
- CliAction::ResumeSession {
- session_path: PathBuf::from("session.json"),
- commands: vec!["/compact".to_string()],
- }
- );
- }
- #[test]
- fn parses_resume_flag_with_multiple_slash_commands() {
- let args = vec![
- "--resume".to_string(),
- "session.json".to_string(),
- "/status".to_string(),
- "/compact".to_string(),
- "/cost".to_string(),
- ];
- assert_eq!(
- parse_args(&args).expect("args should parse"),
- CliAction::ResumeSession {
- session_path: PathBuf::from("session.json"),
- commands: vec![
- "/status".to_string(),
- "/compact".to_string(),
- "/cost".to_string(),
- ],
- }
- );
- }
- #[test]
- fn filtered_tool_specs_respect_allowlist() {
- let allowed = ["read_file", "grep_search"]
- .into_iter()
- .map(str::to_string)
- .collect();
- let filtered = filter_tool_specs(Some(&allowed));
- let names = filtered
- .into_iter()
- .map(|spec| spec.name)
- .collect::<Vec<_>>();
- assert_eq!(names, vec!["read_file", "grep_search"]);
- }
- #[test]
- fn shared_help_uses_resume_annotation_copy() {
- let help = commands::render_slash_command_help();
- assert!(help.contains("Slash commands"));
- assert!(help.contains("works with --resume SESSION.json"));
- }
- #[test]
- fn repl_help_includes_shared_commands_and_exit() {
- let help = render_repl_help();
- assert!(help.contains("REPL"));
- assert!(help.contains("/help"));
- assert!(help.contains("/status"));
- assert!(help.contains("/model [model]"));
- assert!(help.contains("/permissions [read-only|workspace-write|danger-full-access]"));
- assert!(help.contains("/clear [--confirm]"));
- assert!(help.contains("/cost"));
- assert!(help.contains("/resume <session-path>"));
- assert!(help.contains("/config [env|hooks|model]"));
- assert!(help.contains("/memory"));
- assert!(help.contains("/init"));
- assert!(help.contains("/diff"));
- assert!(help.contains("/version"));
- assert!(help.contains("/export [file]"));
- assert!(help.contains("/session [list|switch <session-id>]"));
- assert!(help.contains("/exit"));
- }
- #[test]
- fn resume_supported_command_list_matches_expected_surface() {
- let names = resume_supported_slash_commands()
- .into_iter()
- .map(|spec| spec.name)
- .collect::<Vec<_>>();
- assert_eq!(
- names,
- vec![
- "help", "status", "compact", "clear", "cost", "config", "memory", "init", "diff",
- "version", "export",
- ]
- );
- }
- #[test]
- fn resume_report_uses_sectioned_layout() {
- let report = format_resume_report("session.json", 14, 6);
- assert!(report.contains("Session resumed"));
- assert!(report.contains("Session file session.json"));
- assert!(report.contains("Messages 14"));
- assert!(report.contains("Turns 6"));
- }
- #[test]
- fn compact_report_uses_structured_output() {
- let compacted = format_compact_report(8, 5, false);
- assert!(compacted.contains("Compact"));
- assert!(compacted.contains("Result compacted"));
- assert!(compacted.contains("Messages removed 8"));
- let skipped = format_compact_report(0, 3, true);
- assert!(skipped.contains("Result skipped"));
- }
- #[test]
- fn cost_report_uses_sectioned_layout() {
- let report = format_cost_report(runtime::TokenUsage {
- input_tokens: 20,
- output_tokens: 8,
- cache_creation_input_tokens: 3,
- cache_read_input_tokens: 1,
- });
- assert!(report.contains("Cost"));
- assert!(report.contains("Input tokens 20"));
- assert!(report.contains("Output tokens 8"));
- assert!(report.contains("Cache create 3"));
- assert!(report.contains("Cache read 1"));
- assert!(report.contains("Total tokens 32"));
- }
- #[test]
- fn permissions_report_uses_sectioned_layout() {
- let report = format_permissions_report("workspace-write");
- assert!(report.contains("Permissions"));
- assert!(report.contains("Active mode workspace-write"));
- assert!(report.contains("Modes"));
- assert!(report.contains("read-only ○ available Read/search tools only"));
- assert!(report.contains("workspace-write ● current Edit files inside the workspace"));
- assert!(report.contains("danger-full-access ○ available Unrestricted tool access"));
- }
- #[test]
- fn permissions_switch_report_is_structured() {
- let report = format_permissions_switch_report("read-only", "workspace-write");
- assert!(report.contains("Permissions updated"));
- assert!(report.contains("Result mode switched"));
- assert!(report.contains("Previous mode read-only"));
- assert!(report.contains("Active mode workspace-write"));
- assert!(report.contains("Applies to subsequent tool calls"));
- }
- #[test]
- fn init_help_mentions_direct_subcommand() {
- let mut help = Vec::new();
- print_help_to(&mut help).expect("help should render");
- let help = String::from_utf8(help).expect("help should be utf8");
- assert!(help.contains("claw init"));
- }
- #[test]
- fn model_report_uses_sectioned_layout() {
- let report = format_model_report("claude-sonnet", 12, 4);
- assert!(report.contains("Model"));
- assert!(report.contains("Current model claude-sonnet"));
- assert!(report.contains("Session messages 12"));
- assert!(report.contains("Switch models with /model <name>"));
- }
- #[test]
- fn model_switch_report_preserves_context_summary() {
- let report = format_model_switch_report("claude-sonnet", "claude-opus", 9);
- assert!(report.contains("Model updated"));
- assert!(report.contains("Previous claude-sonnet"));
- assert!(report.contains("Current claude-opus"));
- assert!(report.contains("Preserved msgs 9"));
- }
- #[test]
- fn status_line_reports_model_and_token_totals() {
- let status = format_status_report(
- "claude-sonnet",
- StatusUsage {
- message_count: 7,
- turns: 3,
- latest: runtime::TokenUsage {
- input_tokens: 5,
- output_tokens: 4,
- cache_creation_input_tokens: 1,
- cache_read_input_tokens: 0,
- },
- cumulative: runtime::TokenUsage {
- input_tokens: 20,
- output_tokens: 8,
- cache_creation_input_tokens: 2,
- cache_read_input_tokens: 1,
- },
- estimated_tokens: 128,
- },
- "workspace-write",
- &super::StatusContext {
- cwd: PathBuf::from("/tmp/project"),
- session_path: Some(PathBuf::from("session.json")),
- loaded_config_files: 2,
- discovered_config_files: 3,
- memory_file_count: 4,
- project_root: Some(PathBuf::from("/tmp")),
- git_branch: Some("main".to_string()),
- },
- );
- assert!(status.contains("Status"));
- assert!(status.contains("Model claude-sonnet"));
- assert!(status.contains("Permission mode workspace-write"));
- assert!(status.contains("Messages 7"));
- assert!(status.contains("Latest total 10"));
- assert!(status.contains("Cumulative total 31"));
- assert!(status.contains("Cwd /tmp/project"));
- assert!(status.contains("Project root /tmp"));
- assert!(status.contains("Git branch main"));
- assert!(status.contains("Session session.json"));
- assert!(status.contains("Config files loaded 2/3"));
- assert!(status.contains("Memory files 4"));
- }
- #[test]
- fn config_report_supports_section_views() {
- let report = render_config_report(Some("env")).expect("config report should render");
- assert!(report.contains("Merged section: env"));
- }
- #[test]
- fn memory_report_uses_sectioned_layout() {
- let report = render_memory_report().expect("memory report should render");
- assert!(report.contains("Memory"));
- assert!(report.contains("Working directory"));
- assert!(report.contains("Instruction files"));
- assert!(report.contains("Discovered files"));
- }
- #[test]
- fn config_report_uses_sectioned_layout() {
- let report = render_config_report(None).expect("config report should render");
- assert!(report.contains("Config"));
- assert!(report.contains("Discovered files"));
- assert!(report.contains("Merged JSON"));
- }
- #[test]
- fn parses_git_status_metadata() {
- let (root, branch) = parse_git_status_metadata(Some(
- "## rcc/cli...origin/rcc/cli
- M src/main.rs",
- ));
- assert_eq!(branch.as_deref(), Some("rcc/cli"));
- let _ = root;
- }
- #[test]
- fn status_context_reads_real_workspace_metadata() {
- let context = status_context(None).expect("status context should load");
- assert!(context.cwd.is_absolute());
- assert_eq!(context.discovered_config_files, 5);
- assert!(context.loaded_config_files <= context.discovered_config_files);
- }
- #[test]
- fn normalizes_supported_permission_modes() {
- assert_eq!(normalize_permission_mode("read-only"), Some("read-only"));
- assert_eq!(
- normalize_permission_mode("workspace-write"),
- Some("workspace-write")
- );
- assert_eq!(
- normalize_permission_mode("danger-full-access"),
- Some("danger-full-access")
- );
- assert_eq!(normalize_permission_mode("unknown"), None);
- }
- #[test]
- fn clear_command_requires_explicit_confirmation_flag() {
- assert_eq!(
- SlashCommand::parse("/clear"),
- Some(SlashCommand::Clear { confirm: false })
- );
- assert_eq!(
- SlashCommand::parse("/clear --confirm"),
- Some(SlashCommand::Clear { confirm: true })
- );
- }
- #[test]
- fn parses_resume_and_config_slash_commands() {
- assert_eq!(
- SlashCommand::parse("/resume saved-session.json"),
- Some(SlashCommand::Resume {
- session_path: Some("saved-session.json".to_string())
- })
- );
- assert_eq!(
- SlashCommand::parse("/clear --confirm"),
- Some(SlashCommand::Clear { confirm: true })
- );
- assert_eq!(
- SlashCommand::parse("/config"),
- Some(SlashCommand::Config { section: None })
- );
- assert_eq!(
- SlashCommand::parse("/config env"),
- Some(SlashCommand::Config {
- section: Some("env".to_string())
- })
- );
- assert_eq!(SlashCommand::parse("/memory"), Some(SlashCommand::Memory));
- assert_eq!(SlashCommand::parse("/init"), Some(SlashCommand::Init));
- }
- #[test]
- fn init_template_mentions_detected_rust_workspace() {
- let rendered = crate::init::render_init_claude_md(std::path::Path::new("."));
- assert!(rendered.contains("# CLAUDE.md"));
- assert!(rendered.contains("cargo clippy --workspace --all-targets -- -D warnings"));
- }
- #[test]
- fn converts_tool_roundtrip_messages() {
- let messages = vec![
- ConversationMessage::user_text("hello"),
- ConversationMessage::assistant(vec![ContentBlock::ToolUse {
- id: "tool-1".to_string(),
- name: "bash".to_string(),
- input: "{\"command\":\"pwd\"}".to_string(),
- }]),
- ConversationMessage {
- role: MessageRole::Tool,
- blocks: vec![ContentBlock::ToolResult {
- tool_use_id: "tool-1".to_string(),
- tool_name: "bash".to_string(),
- output: "ok".to_string(),
- is_error: false,
- }],
- usage: None,
- },
- ];
- let converted = super::convert_messages(&messages);
- assert_eq!(converted.len(), 3);
- assert_eq!(converted[1].role, "assistant");
- assert_eq!(converted[2].role, "user");
- }
- #[test]
- fn repl_help_mentions_history_completion_and_multiline() {
- let help = render_repl_help();
- assert!(help.contains("Up/Down"));
- assert!(help.contains("Tab"));
- assert!(help.contains("Shift+Enter/Ctrl+J"));
- }
- #[test]
- fn tool_rendering_helpers_compact_output() {
- let start = format_tool_call_start("read_file", r#"{"path":"src/main.rs"}"#);
- assert!(start.contains("read_file"));
- assert!(start.contains("src/main.rs"));
- let done = format_tool_result(
- "read_file",
- r#"{"file":{"filePath":"src/main.rs","content":"hello","numLines":1,"startLine":1,"totalLines":1}}"#,
- false,
- );
- assert!(done.contains("📄 Read src/main.rs"));
- assert!(done.contains("hello"));
- }
- #[test]
- fn push_output_block_renders_markdown_text() {
- let mut out = Vec::new();
- let mut events = Vec::new();
- let mut pending_tool = None;
- push_output_block(
- OutputContentBlock::Text {
- text: "# Heading".to_string(),
- },
- &mut out,
- &mut events,
- &mut pending_tool,
- false,
- )
- .expect("text block should render");
- let rendered = String::from_utf8(out).expect("utf8");
- assert!(rendered.contains("Heading"));
- assert!(rendered.contains('\u{1b}'));
- }
- #[test]
- fn push_output_block_skips_empty_object_prefix_for_tool_streams() {
- let mut out = Vec::new();
- let mut events = Vec::new();
- let mut pending_tool = None;
- push_output_block(
- OutputContentBlock::ToolUse {
- id: "tool-1".to_string(),
- name: "read_file".to_string(),
- input: json!({}),
- },
- &mut out,
- &mut events,
- &mut pending_tool,
- true,
- )
- .expect("tool block should accumulate");
- assert!(events.is_empty());
- assert_eq!(
- pending_tool,
- Some(("tool-1".to_string(), "read_file".to_string(), String::new(),))
- );
- }
- #[test]
- fn response_to_events_preserves_empty_object_json_input_outside_streaming() {
- let mut out = Vec::new();
- let events = response_to_events(
- MessageResponse {
- id: "msg-1".to_string(),
- kind: "message".to_string(),
- model: "claude-opus-4-6".to_string(),
- role: "assistant".to_string(),
- content: vec![OutputContentBlock::ToolUse {
- id: "tool-1".to_string(),
- name: "read_file".to_string(),
- input: json!({}),
- }],
- stop_reason: Some("tool_use".to_string()),
- stop_sequence: None,
- usage: Usage {
- input_tokens: 1,
- output_tokens: 1,
- cache_creation_input_tokens: 0,
- cache_read_input_tokens: 0,
- },
- request_id: None,
- },
- &mut out,
- )
- .expect("response conversion should succeed");
- assert!(matches!(
- &events[0],
- AssistantEvent::ToolUse { name, input, .. }
- if name == "read_file" && input == "{}"
- ));
- }
- #[test]
- fn response_to_events_preserves_non_empty_json_input_outside_streaming() {
- let mut out = Vec::new();
- let events = response_to_events(
- MessageResponse {
- id: "msg-2".to_string(),
- kind: "message".to_string(),
- model: "claude-opus-4-6".to_string(),
- role: "assistant".to_string(),
- content: vec![OutputContentBlock::ToolUse {
- id: "tool-2".to_string(),
- name: "read_file".to_string(),
- input: json!({ "path": "rust/Cargo.toml" }),
- }],
- stop_reason: Some("tool_use".to_string()),
- stop_sequence: None,
- usage: Usage {
- input_tokens: 1,
- output_tokens: 1,
- cache_creation_input_tokens: 0,
- cache_read_input_tokens: 0,
- },
- request_id: None,
- },
- &mut out,
- )
- .expect("response conversion should succeed");
- assert!(matches!(
- &events[0],
- AssistantEvent::ToolUse { name, input, .. }
- if name == "read_file" && input == "{\"path\":\"rust/Cargo.toml\"}"
- ));
- }
- }
|