| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526 |
- use std::collections::{BTreeMap, BTreeSet};
- use std::path::{Path, PathBuf};
- use std::process::Command;
- use std::time::{Duration, Instant};
- use api::{
- max_tokens_for_model, resolve_model_alias, ContentBlockDelta, InputContentBlock, InputMessage,
- MessageRequest, MessageResponse, OutputContentBlock, ProviderClient,
- StreamEvent as ApiStreamEvent, ToolChoice, ToolDefinition, ToolResultContentBlock,
- };
- use plugins::PluginTool;
- use reqwest::blocking::Client;
- use runtime::{
- edit_file, execute_bash, glob_search, grep_search, load_system_prompt, read_file, write_file,
- ApiClient, ApiRequest, AssistantEvent, BashCommandInput, ContentBlock, ConversationMessage,
- ConversationRuntime, GrepSearchInput, MessageRole, PermissionMode, PermissionPolicy,
- PromptCacheEvent, RuntimeError, Session, ToolError, ToolExecutor,
- };
- use serde::{Deserialize, Serialize};
- use serde_json::{json, Value};
- #[derive(Debug, Clone, PartialEq, Eq)]
- pub struct ToolManifestEntry {
- pub name: String,
- pub source: ToolSource,
- }
- #[derive(Debug, Clone, Copy, PartialEq, Eq)]
- pub enum ToolSource {
- Base,
- Conditional,
- }
- #[derive(Debug, Clone, Default, PartialEq, Eq)]
- pub struct ToolRegistry {
- entries: Vec<ToolManifestEntry>,
- }
- impl ToolRegistry {
- #[must_use]
- pub fn new(entries: Vec<ToolManifestEntry>) -> Self {
- Self { entries }
- }
- #[must_use]
- pub fn entries(&self) -> &[ToolManifestEntry] {
- &self.entries
- }
- }
- #[derive(Debug, Clone, PartialEq, Eq)]
- pub struct ToolSpec {
- pub name: &'static str,
- pub description: &'static str,
- pub input_schema: Value,
- pub required_permission: PermissionMode,
- }
- #[derive(Debug, Clone, PartialEq)]
- pub struct GlobalToolRegistry {
- plugin_tools: Vec<PluginTool>,
- }
- impl GlobalToolRegistry {
- #[must_use]
- pub fn builtin() -> Self {
- Self {
- plugin_tools: Vec::new(),
- }
- }
- pub fn with_plugin_tools(plugin_tools: Vec<PluginTool>) -> Result<Self, String> {
- let builtin_names = mvp_tool_specs()
- .into_iter()
- .map(|spec| spec.name.to_string())
- .collect::<BTreeSet<_>>();
- let mut seen_plugin_names = BTreeSet::new();
- for tool in &plugin_tools {
- let name = tool.definition().name.clone();
- if builtin_names.contains(&name) {
- return Err(format!(
- "plugin tool `{name}` conflicts with a built-in tool name"
- ));
- }
- if !seen_plugin_names.insert(name.clone()) {
- return Err(format!("duplicate plugin tool name `{name}`"));
- }
- }
- Ok(Self { plugin_tools })
- }
- pub fn normalize_allowed_tools(
- &self,
- values: &[String],
- ) -> Result<Option<BTreeSet<String>>, String> {
- if values.is_empty() {
- return Ok(None);
- }
- let builtin_specs = mvp_tool_specs();
- let canonical_names = builtin_specs
- .iter()
- .map(|spec| spec.name.to_string())
- .chain(
- self.plugin_tools
- .iter()
- .map(|tool| tool.definition().name.clone()),
- )
- .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 = BTreeSet::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))
- }
- #[must_use]
- pub fn definitions(&self, allowed_tools: Option<&BTreeSet<String>>) -> Vec<ToolDefinition> {
- let builtin = mvp_tool_specs()
- .into_iter()
- .filter(|spec| allowed_tools.is_none_or(|allowed| allowed.contains(spec.name)))
- .map(|spec| ToolDefinition {
- name: spec.name.to_string(),
- description: Some(spec.description.to_string()),
- input_schema: spec.input_schema,
- });
- let plugin = self
- .plugin_tools
- .iter()
- .filter(|tool| {
- allowed_tools
- .is_none_or(|allowed| allowed.contains(tool.definition().name.as_str()))
- })
- .map(|tool| ToolDefinition {
- name: tool.definition().name.clone(),
- description: tool.definition().description.clone(),
- input_schema: tool.definition().input_schema.clone(),
- });
- builtin.chain(plugin).collect()
- }
- #[must_use]
- pub fn permission_specs(
- &self,
- allowed_tools: Option<&BTreeSet<String>>,
- ) -> Result<Vec<(String, PermissionMode)>, String> {
- let builtin = mvp_tool_specs()
- .into_iter()
- .filter(|spec| allowed_tools.is_none_or(|allowed| allowed.contains(spec.name)))
- .map(|spec| (spec.name.to_string(), spec.required_permission));
- let plugin = self
- .plugin_tools
- .iter()
- .filter(|tool| {
- allowed_tools
- .is_none_or(|allowed| allowed.contains(tool.definition().name.as_str()))
- })
- .map(|tool| {
- permission_mode_from_plugin(tool.required_permission())
- .map(|permission| (tool.definition().name.clone(), permission))
- })
- .collect::<Result<Vec<_>, _>>()?;
- Ok(builtin.chain(plugin).collect())
- }
- pub fn execute(&self, name: &str, input: &Value) -> Result<String, String> {
- if mvp_tool_specs().iter().any(|spec| spec.name == name) {
- return execute_tool(name, input);
- }
- self.plugin_tools
- .iter()
- .find(|tool| tool.definition().name == name)
- .ok_or_else(|| format!("unsupported tool: {name}"))?
- .execute(input)
- .map_err(|error| error.to_string())
- }
- }
- fn normalize_tool_name(value: &str) -> String {
- value.trim().replace('-', "_").to_ascii_lowercase()
- }
- fn permission_mode_from_plugin(value: &str) -> Result<PermissionMode, String> {
- match value {
- "read-only" => Ok(PermissionMode::ReadOnly),
- "workspace-write" => Ok(PermissionMode::WorkspaceWrite),
- "danger-full-access" => Ok(PermissionMode::DangerFullAccess),
- other => Err(format!("unsupported plugin permission: {other}")),
- }
- }
- #[must_use]
- #[allow(clippy::too_many_lines)]
- pub fn mvp_tool_specs() -> Vec<ToolSpec> {
- vec![
- ToolSpec {
- name: "bash",
- description: "Execute a shell command in the current workspace.",
- input_schema: json!({
- "type": "object",
- "properties": {
- "command": { "type": "string" },
- "timeout": { "type": "integer", "minimum": 1 },
- "description": { "type": "string" },
- "run_in_background": { "type": "boolean" },
- "dangerouslyDisableSandbox": { "type": "boolean" },
- "namespaceRestrictions": { "type": "boolean" },
- "isolateNetwork": { "type": "boolean" },
- "filesystemMode": { "type": "string", "enum": ["off", "workspace-only", "allow-list"] },
- "allowedMounts": { "type": "array", "items": { "type": "string" } }
- },
- "required": ["command"],
- "additionalProperties": false
- }),
- required_permission: PermissionMode::DangerFullAccess,
- },
- ToolSpec {
- name: "read_file",
- description: "Read a text file from the workspace.",
- input_schema: json!({
- "type": "object",
- "properties": {
- "path": { "type": "string" },
- "offset": { "type": "integer", "minimum": 0 },
- "limit": { "type": "integer", "minimum": 1 }
- },
- "required": ["path"],
- "additionalProperties": false
- }),
- required_permission: PermissionMode::ReadOnly,
- },
- ToolSpec {
- name: "write_file",
- description: "Write a text file in the workspace.",
- input_schema: json!({
- "type": "object",
- "properties": {
- "path": { "type": "string" },
- "content": { "type": "string" }
- },
- "required": ["path", "content"],
- "additionalProperties": false
- }),
- required_permission: PermissionMode::WorkspaceWrite,
- },
- ToolSpec {
- name: "edit_file",
- description: "Replace text in a workspace file.",
- input_schema: json!({
- "type": "object",
- "properties": {
- "path": { "type": "string" },
- "old_string": { "type": "string" },
- "new_string": { "type": "string" },
- "replace_all": { "type": "boolean" }
- },
- "required": ["path", "old_string", "new_string"],
- "additionalProperties": false
- }),
- required_permission: PermissionMode::WorkspaceWrite,
- },
- ToolSpec {
- name: "glob_search",
- description: "Find files by glob pattern.",
- input_schema: json!({
- "type": "object",
- "properties": {
- "pattern": { "type": "string" },
- "path": { "type": "string" }
- },
- "required": ["pattern"],
- "additionalProperties": false
- }),
- required_permission: PermissionMode::ReadOnly,
- },
- ToolSpec {
- name: "grep_search",
- description: "Search file contents with a regex pattern.",
- input_schema: json!({
- "type": "object",
- "properties": {
- "pattern": { "type": "string" },
- "path": { "type": "string" },
- "glob": { "type": "string" },
- "output_mode": { "type": "string" },
- "-B": { "type": "integer", "minimum": 0 },
- "-A": { "type": "integer", "minimum": 0 },
- "-C": { "type": "integer", "minimum": 0 },
- "context": { "type": "integer", "minimum": 0 },
- "-n": { "type": "boolean" },
- "-i": { "type": "boolean" },
- "type": { "type": "string" },
- "head_limit": { "type": "integer", "minimum": 1 },
- "offset": { "type": "integer", "minimum": 0 },
- "multiline": { "type": "boolean" }
- },
- "required": ["pattern"],
- "additionalProperties": false
- }),
- required_permission: PermissionMode::ReadOnly,
- },
- ToolSpec {
- name: "WebFetch",
- description:
- "Fetch a URL, convert it into readable text, and answer a prompt about it.",
- input_schema: json!({
- "type": "object",
- "properties": {
- "url": { "type": "string", "format": "uri" },
- "prompt": { "type": "string" }
- },
- "required": ["url", "prompt"],
- "additionalProperties": false
- }),
- required_permission: PermissionMode::ReadOnly,
- },
- ToolSpec {
- name: "WebSearch",
- description: "Search the web for current information and return cited results.",
- input_schema: json!({
- "type": "object",
- "properties": {
- "query": { "type": "string", "minLength": 2 },
- "allowed_domains": {
- "type": "array",
- "items": { "type": "string" }
- },
- "blocked_domains": {
- "type": "array",
- "items": { "type": "string" }
- }
- },
- "required": ["query"],
- "additionalProperties": false
- }),
- required_permission: PermissionMode::ReadOnly,
- },
- ToolSpec {
- name: "TodoWrite",
- description: "Update the structured task list for the current session.",
- input_schema: json!({
- "type": "object",
- "properties": {
- "todos": {
- "type": "array",
- "items": {
- "type": "object",
- "properties": {
- "content": { "type": "string" },
- "activeForm": { "type": "string" },
- "status": {
- "type": "string",
- "enum": ["pending", "in_progress", "completed"]
- }
- },
- "required": ["content", "activeForm", "status"],
- "additionalProperties": false
- }
- }
- },
- "required": ["todos"],
- "additionalProperties": false
- }),
- required_permission: PermissionMode::WorkspaceWrite,
- },
- ToolSpec {
- name: "Skill",
- description: "Load a local skill definition and its instructions.",
- input_schema: json!({
- "type": "object",
- "properties": {
- "skill": { "type": "string" },
- "args": { "type": "string" }
- },
- "required": ["skill"],
- "additionalProperties": false
- }),
- required_permission: PermissionMode::ReadOnly,
- },
- ToolSpec {
- name: "Agent",
- description: "Launch a specialized agent task and persist its handoff metadata.",
- input_schema: json!({
- "type": "object",
- "properties": {
- "description": { "type": "string" },
- "prompt": { "type": "string" },
- "subagent_type": { "type": "string" },
- "name": { "type": "string" },
- "model": { "type": "string" }
- },
- "required": ["description", "prompt"],
- "additionalProperties": false
- }),
- required_permission: PermissionMode::DangerFullAccess,
- },
- ToolSpec {
- name: "ToolSearch",
- description: "Search for deferred or specialized tools by exact name or keywords.",
- input_schema: json!({
- "type": "object",
- "properties": {
- "query": { "type": "string" },
- "max_results": { "type": "integer", "minimum": 1 }
- },
- "required": ["query"],
- "additionalProperties": false
- }),
- required_permission: PermissionMode::ReadOnly,
- },
- ToolSpec {
- name: "NotebookEdit",
- description: "Replace, insert, or delete a cell in a Jupyter notebook.",
- input_schema: json!({
- "type": "object",
- "properties": {
- "notebook_path": { "type": "string" },
- "cell_id": { "type": "string" },
- "new_source": { "type": "string" },
- "cell_type": { "type": "string", "enum": ["code", "markdown"] },
- "edit_mode": { "type": "string", "enum": ["replace", "insert", "delete"] }
- },
- "required": ["notebook_path"],
- "additionalProperties": false
- }),
- required_permission: PermissionMode::WorkspaceWrite,
- },
- ToolSpec {
- name: "Sleep",
- description: "Wait for a specified duration without holding a shell process.",
- input_schema: json!({
- "type": "object",
- "properties": {
- "duration_ms": { "type": "integer", "minimum": 0 }
- },
- "required": ["duration_ms"],
- "additionalProperties": false
- }),
- required_permission: PermissionMode::ReadOnly,
- },
- ToolSpec {
- name: "SendUserMessage",
- description: "Send a message to the user.",
- input_schema: json!({
- "type": "object",
- "properties": {
- "message": { "type": "string" },
- "attachments": {
- "type": "array",
- "items": { "type": "string" }
- },
- "status": {
- "type": "string",
- "enum": ["normal", "proactive"]
- }
- },
- "required": ["message", "status"],
- "additionalProperties": false
- }),
- required_permission: PermissionMode::ReadOnly,
- },
- ToolSpec {
- name: "Config",
- description: "Get or set Claude Code settings.",
- input_schema: json!({
- "type": "object",
- "properties": {
- "setting": { "type": "string" },
- "value": {
- "type": ["string", "boolean", "number"]
- }
- },
- "required": ["setting"],
- "additionalProperties": false
- }),
- required_permission: PermissionMode::WorkspaceWrite,
- },
- ToolSpec {
- name: "StructuredOutput",
- description: "Return structured output in the requested format.",
- input_schema: json!({
- "type": "object",
- "additionalProperties": true
- }),
- required_permission: PermissionMode::ReadOnly,
- },
- ToolSpec {
- name: "REPL",
- description: "Execute code in a REPL-like subprocess.",
- input_schema: json!({
- "type": "object",
- "properties": {
- "code": { "type": "string" },
- "language": { "type": "string" },
- "timeout_ms": { "type": "integer", "minimum": 1 }
- },
- "required": ["code", "language"],
- "additionalProperties": false
- }),
- required_permission: PermissionMode::DangerFullAccess,
- },
- ToolSpec {
- name: "PowerShell",
- description: "Execute a PowerShell command with optional timeout.",
- input_schema: json!({
- "type": "object",
- "properties": {
- "command": { "type": "string" },
- "timeout": { "type": "integer", "minimum": 1 },
- "description": { "type": "string" },
- "run_in_background": { "type": "boolean" }
- },
- "required": ["command"],
- "additionalProperties": false
- }),
- required_permission: PermissionMode::DangerFullAccess,
- },
- ]
- }
- pub fn execute_tool(name: &str, input: &Value) -> Result<String, String> {
- match name {
- "bash" => from_value::<BashCommandInput>(input).and_then(run_bash),
- "read_file" => from_value::<ReadFileInput>(input).and_then(run_read_file),
- "write_file" => from_value::<WriteFileInput>(input).and_then(run_write_file),
- "edit_file" => from_value::<EditFileInput>(input).and_then(run_edit_file),
- "glob_search" => from_value::<GlobSearchInputValue>(input).and_then(run_glob_search),
- "grep_search" => from_value::<GrepSearchInput>(input).and_then(run_grep_search),
- "WebFetch" => from_value::<WebFetchInput>(input).and_then(run_web_fetch),
- "WebSearch" => from_value::<WebSearchInput>(input).and_then(run_web_search),
- "TodoWrite" => from_value::<TodoWriteInput>(input).and_then(run_todo_write),
- "Skill" => from_value::<SkillInput>(input).and_then(run_skill),
- "Agent" => from_value::<AgentInput>(input).and_then(run_agent),
- "ToolSearch" => from_value::<ToolSearchInput>(input).and_then(run_tool_search),
- "NotebookEdit" => from_value::<NotebookEditInput>(input).and_then(run_notebook_edit),
- "Sleep" => from_value::<SleepInput>(input).and_then(run_sleep),
- "SendUserMessage" | "Brief" => from_value::<BriefInput>(input).and_then(run_brief),
- "Config" => from_value::<ConfigInput>(input).and_then(run_config),
- "StructuredOutput" => {
- from_value::<StructuredOutputInput>(input).and_then(run_structured_output)
- }
- "REPL" => from_value::<ReplInput>(input).and_then(run_repl),
- "PowerShell" => from_value::<PowerShellInput>(input).and_then(run_powershell),
- _ => Err(format!("unsupported tool: {name}")),
- }
- }
- fn from_value<T: for<'de> Deserialize<'de>>(input: &Value) -> Result<T, String> {
- serde_json::from_value(input.clone()).map_err(|error| error.to_string())
- }
- fn run_bash(input: BashCommandInput) -> Result<String, String> {
- serde_json::to_string_pretty(&execute_bash(input).map_err(|error| error.to_string())?)
- .map_err(|error| error.to_string())
- }
- #[allow(clippy::needless_pass_by_value)]
- fn run_read_file(input: ReadFileInput) -> Result<String, String> {
- to_pretty_json(read_file(&input.path, input.offset, input.limit).map_err(io_to_string)?)
- }
- #[allow(clippy::needless_pass_by_value)]
- fn run_write_file(input: WriteFileInput) -> Result<String, String> {
- to_pretty_json(write_file(&input.path, &input.content).map_err(io_to_string)?)
- }
- #[allow(clippy::needless_pass_by_value)]
- fn run_edit_file(input: EditFileInput) -> Result<String, String> {
- to_pretty_json(
- edit_file(
- &input.path,
- &input.old_string,
- &input.new_string,
- input.replace_all.unwrap_or(false),
- )
- .map_err(io_to_string)?,
- )
- }
- #[allow(clippy::needless_pass_by_value)]
- fn run_glob_search(input: GlobSearchInputValue) -> Result<String, String> {
- to_pretty_json(glob_search(&input.pattern, input.path.as_deref()).map_err(io_to_string)?)
- }
- #[allow(clippy::needless_pass_by_value)]
- fn run_grep_search(input: GrepSearchInput) -> Result<String, String> {
- to_pretty_json(grep_search(&input).map_err(io_to_string)?)
- }
- #[allow(clippy::needless_pass_by_value)]
- fn run_web_fetch(input: WebFetchInput) -> Result<String, String> {
- to_pretty_json(execute_web_fetch(&input)?)
- }
- #[allow(clippy::needless_pass_by_value)]
- fn run_web_search(input: WebSearchInput) -> Result<String, String> {
- to_pretty_json(execute_web_search(&input)?)
- }
- fn run_todo_write(input: TodoWriteInput) -> Result<String, String> {
- to_pretty_json(execute_todo_write(input)?)
- }
- fn run_skill(input: SkillInput) -> Result<String, String> {
- to_pretty_json(execute_skill(input)?)
- }
- fn run_agent(input: AgentInput) -> Result<String, String> {
- to_pretty_json(execute_agent(input)?)
- }
- fn run_tool_search(input: ToolSearchInput) -> Result<String, String> {
- to_pretty_json(execute_tool_search(input))
- }
- fn run_notebook_edit(input: NotebookEditInput) -> Result<String, String> {
- to_pretty_json(execute_notebook_edit(input)?)
- }
- fn run_sleep(input: SleepInput) -> Result<String, String> {
- to_pretty_json(execute_sleep(input))
- }
- fn run_brief(input: BriefInput) -> Result<String, String> {
- to_pretty_json(execute_brief(input)?)
- }
- fn run_config(input: ConfigInput) -> Result<String, String> {
- to_pretty_json(execute_config(input)?)
- }
- fn run_structured_output(input: StructuredOutputInput) -> Result<String, String> {
- to_pretty_json(execute_structured_output(input))
- }
- fn run_repl(input: ReplInput) -> Result<String, String> {
- to_pretty_json(execute_repl(input)?)
- }
- fn run_powershell(input: PowerShellInput) -> Result<String, String> {
- to_pretty_json(execute_powershell(input).map_err(|error| error.to_string())?)
- }
- fn to_pretty_json<T: serde::Serialize>(value: T) -> Result<String, String> {
- serde_json::to_string_pretty(&value).map_err(|error| error.to_string())
- }
- #[allow(clippy::needless_pass_by_value)]
- fn io_to_string(error: std::io::Error) -> String {
- error.to_string()
- }
- #[derive(Debug, Deserialize)]
- struct ReadFileInput {
- path: String,
- offset: Option<usize>,
- limit: Option<usize>,
- }
- #[derive(Debug, Deserialize)]
- struct WriteFileInput {
- path: String,
- content: String,
- }
- #[derive(Debug, Deserialize)]
- struct EditFileInput {
- path: String,
- old_string: String,
- new_string: String,
- replace_all: Option<bool>,
- }
- #[derive(Debug, Deserialize)]
- struct GlobSearchInputValue {
- pattern: String,
- path: Option<String>,
- }
- #[derive(Debug, Deserialize)]
- struct WebFetchInput {
- url: String,
- prompt: String,
- }
- #[derive(Debug, Deserialize)]
- struct WebSearchInput {
- query: String,
- allowed_domains: Option<Vec<String>>,
- blocked_domains: Option<Vec<String>>,
- }
- #[derive(Debug, Deserialize)]
- struct TodoWriteInput {
- todos: Vec<TodoItem>,
- }
- #[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
- struct TodoItem {
- content: String,
- #[serde(rename = "activeForm")]
- active_form: String,
- status: TodoStatus,
- }
- #[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
- #[serde(rename_all = "snake_case")]
- enum TodoStatus {
- Pending,
- InProgress,
- Completed,
- }
- #[derive(Debug, Deserialize)]
- struct SkillInput {
- skill: String,
- args: Option<String>,
- }
- #[derive(Debug, Deserialize)]
- struct AgentInput {
- description: String,
- prompt: String,
- subagent_type: Option<String>,
- name: Option<String>,
- model: Option<String>,
- }
- #[derive(Debug, Deserialize)]
- struct ToolSearchInput {
- query: String,
- max_results: Option<usize>,
- }
- #[derive(Debug, Deserialize)]
- struct NotebookEditInput {
- notebook_path: String,
- cell_id: Option<String>,
- new_source: Option<String>,
- cell_type: Option<NotebookCellType>,
- edit_mode: Option<NotebookEditMode>,
- }
- #[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq)]
- #[serde(rename_all = "lowercase")]
- enum NotebookCellType {
- Code,
- Markdown,
- }
- #[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq)]
- #[serde(rename_all = "lowercase")]
- enum NotebookEditMode {
- Replace,
- Insert,
- Delete,
- }
- #[derive(Debug, Deserialize)]
- struct SleepInput {
- duration_ms: u64,
- }
- #[derive(Debug, Deserialize)]
- struct BriefInput {
- message: String,
- attachments: Option<Vec<String>>,
- status: BriefStatus,
- }
- #[derive(Debug, Deserialize)]
- #[serde(rename_all = "lowercase")]
- enum BriefStatus {
- Normal,
- Proactive,
- }
- #[derive(Debug, Deserialize)]
- struct ConfigInput {
- setting: String,
- value: Option<ConfigValue>,
- }
- #[derive(Debug, Deserialize)]
- #[serde(untagged)]
- enum ConfigValue {
- String(String),
- Bool(bool),
- Number(f64),
- }
- #[derive(Debug, Deserialize)]
- #[serde(transparent)]
- struct StructuredOutputInput(BTreeMap<String, Value>);
- #[derive(Debug, Deserialize)]
- struct ReplInput {
- code: String,
- language: String,
- timeout_ms: Option<u64>,
- }
- #[derive(Debug, Deserialize)]
- struct PowerShellInput {
- command: String,
- timeout: Option<u64>,
- description: Option<String>,
- run_in_background: Option<bool>,
- }
- #[derive(Debug, Serialize)]
- struct WebFetchOutput {
- bytes: usize,
- code: u16,
- #[serde(rename = "codeText")]
- code_text: String,
- result: String,
- #[serde(rename = "durationMs")]
- duration_ms: u128,
- url: String,
- }
- #[derive(Debug, Serialize)]
- struct WebSearchOutput {
- query: String,
- results: Vec<WebSearchResultItem>,
- #[serde(rename = "durationSeconds")]
- duration_seconds: f64,
- }
- #[derive(Debug, Serialize)]
- struct TodoWriteOutput {
- #[serde(rename = "oldTodos")]
- old_todos: Vec<TodoItem>,
- #[serde(rename = "newTodos")]
- new_todos: Vec<TodoItem>,
- #[serde(rename = "verificationNudgeNeeded")]
- verification_nudge_needed: Option<bool>,
- }
- #[derive(Debug, Serialize)]
- struct SkillOutput {
- skill: String,
- path: String,
- args: Option<String>,
- description: Option<String>,
- prompt: String,
- }
- #[derive(Debug, Clone, Serialize, Deserialize)]
- struct AgentOutput {
- #[serde(rename = "agentId")]
- agent_id: String,
- name: String,
- description: String,
- #[serde(rename = "subagentType")]
- subagent_type: Option<String>,
- model: Option<String>,
- status: String,
- #[serde(rename = "outputFile")]
- output_file: String,
- #[serde(rename = "manifestFile")]
- manifest_file: String,
- #[serde(rename = "createdAt")]
- created_at: String,
- #[serde(rename = "startedAt", skip_serializing_if = "Option::is_none")]
- started_at: Option<String>,
- #[serde(rename = "completedAt", skip_serializing_if = "Option::is_none")]
- completed_at: Option<String>,
- #[serde(skip_serializing_if = "Option::is_none")]
- error: Option<String>,
- }
- #[derive(Debug, Clone)]
- struct AgentJob {
- manifest: AgentOutput,
- prompt: String,
- system_prompt: Vec<String>,
- allowed_tools: BTreeSet<String>,
- }
- #[derive(Debug, Serialize)]
- struct ToolSearchOutput {
- matches: Vec<String>,
- query: String,
- normalized_query: String,
- #[serde(rename = "total_deferred_tools")]
- total_deferred_tools: usize,
- #[serde(rename = "pending_mcp_servers")]
- pending_mcp_servers: Option<Vec<String>>,
- }
- #[derive(Debug, Serialize)]
- struct NotebookEditOutput {
- new_source: String,
- cell_id: Option<String>,
- cell_type: Option<NotebookCellType>,
- language: String,
- edit_mode: String,
- error: Option<String>,
- notebook_path: String,
- original_file: String,
- updated_file: String,
- }
- #[derive(Debug, Serialize)]
- struct SleepOutput {
- duration_ms: u64,
- message: String,
- }
- #[derive(Debug, Serialize)]
- struct BriefOutput {
- message: String,
- attachments: Option<Vec<ResolvedAttachment>>,
- #[serde(rename = "sentAt")]
- sent_at: String,
- }
- #[derive(Debug, Serialize)]
- struct ResolvedAttachment {
- path: String,
- size: u64,
- #[serde(rename = "isImage")]
- is_image: bool,
- }
- #[derive(Debug, Serialize)]
- struct ConfigOutput {
- success: bool,
- operation: Option<String>,
- setting: Option<String>,
- value: Option<Value>,
- #[serde(rename = "previousValue")]
- previous_value: Option<Value>,
- #[serde(rename = "newValue")]
- new_value: Option<Value>,
- error: Option<String>,
- }
- #[derive(Debug, Serialize)]
- struct StructuredOutputResult {
- data: String,
- structured_output: BTreeMap<String, Value>,
- }
- #[derive(Debug, Serialize)]
- struct ReplOutput {
- language: String,
- stdout: String,
- stderr: String,
- #[serde(rename = "exitCode")]
- exit_code: i32,
- #[serde(rename = "durationMs")]
- duration_ms: u128,
- }
- #[derive(Debug, Serialize)]
- #[serde(untagged)]
- enum WebSearchResultItem {
- SearchResult {
- tool_use_id: String,
- content: Vec<SearchHit>,
- },
- Commentary(String),
- }
- #[derive(Debug, Serialize)]
- struct SearchHit {
- title: String,
- url: String,
- }
- fn execute_web_fetch(input: &WebFetchInput) -> Result<WebFetchOutput, String> {
- let started = Instant::now();
- let client = build_http_client()?;
- let request_url = normalize_fetch_url(&input.url)?;
- let response = client
- .get(request_url.clone())
- .send()
- .map_err(|error| error.to_string())?;
- let status = response.status();
- let final_url = response.url().to_string();
- let code = status.as_u16();
- let code_text = status.canonical_reason().unwrap_or("Unknown").to_string();
- let content_type = response
- .headers()
- .get(reqwest::header::CONTENT_TYPE)
- .and_then(|value| value.to_str().ok())
- .unwrap_or_default()
- .to_string();
- let body = response.text().map_err(|error| error.to_string())?;
- let bytes = body.len();
- let normalized = normalize_fetched_content(&body, &content_type);
- let result = summarize_web_fetch(&final_url, &input.prompt, &normalized, &body, &content_type);
- Ok(WebFetchOutput {
- bytes,
- code,
- code_text,
- result,
- duration_ms: started.elapsed().as_millis(),
- url: final_url,
- })
- }
- fn execute_web_search(input: &WebSearchInput) -> Result<WebSearchOutput, String> {
- let started = Instant::now();
- let client = build_http_client()?;
- let search_url = build_search_url(&input.query)?;
- let response = client
- .get(search_url)
- .send()
- .map_err(|error| error.to_string())?;
- let final_url = response.url().clone();
- let html = response.text().map_err(|error| error.to_string())?;
- let mut hits = extract_search_hits(&html);
- if hits.is_empty() && final_url.host_str().is_some() {
- hits = extract_search_hits_from_generic_links(&html);
- }
- if let Some(allowed) = input.allowed_domains.as_ref() {
- hits.retain(|hit| host_matches_list(&hit.url, allowed));
- }
- if let Some(blocked) = input.blocked_domains.as_ref() {
- hits.retain(|hit| !host_matches_list(&hit.url, blocked));
- }
- dedupe_hits(&mut hits);
- hits.truncate(8);
- let summary = if hits.is_empty() {
- format!("No web search results matched the query {:?}.", input.query)
- } else {
- let rendered_hits = hits
- .iter()
- .map(|hit| format!("- [{}]({})", hit.title, hit.url))
- .collect::<Vec<_>>()
- .join("\n");
- format!(
- "Search results for {:?}. Include a Sources section in the final answer.\n{}",
- input.query, rendered_hits
- )
- };
- Ok(WebSearchOutput {
- query: input.query.clone(),
- results: vec![
- WebSearchResultItem::Commentary(summary),
- WebSearchResultItem::SearchResult {
- tool_use_id: String::from("web_search_1"),
- content: hits,
- },
- ],
- duration_seconds: started.elapsed().as_secs_f64(),
- })
- }
- fn build_http_client() -> Result<Client, String> {
- Client::builder()
- .timeout(Duration::from_secs(20))
- .redirect(reqwest::redirect::Policy::limited(10))
- .user_agent("clawd-rust-tools/0.1")
- .build()
- .map_err(|error| error.to_string())
- }
- fn normalize_fetch_url(url: &str) -> Result<String, String> {
- let parsed = reqwest::Url::parse(url).map_err(|error| error.to_string())?;
- if parsed.scheme() == "http" {
- let host = parsed.host_str().unwrap_or_default();
- if host != "localhost" && host != "127.0.0.1" && host != "::1" {
- let mut upgraded = parsed;
- upgraded
- .set_scheme("https")
- .map_err(|()| String::from("failed to upgrade URL to https"))?;
- return Ok(upgraded.to_string());
- }
- }
- Ok(parsed.to_string())
- }
- fn build_search_url(query: &str) -> Result<reqwest::Url, String> {
- if let Ok(base) = std::env::var("CLAWD_WEB_SEARCH_BASE_URL") {
- let mut url = reqwest::Url::parse(&base).map_err(|error| error.to_string())?;
- url.query_pairs_mut().append_pair("q", query);
- return Ok(url);
- }
- let mut url = reqwest::Url::parse("https://html.duckduckgo.com/html/")
- .map_err(|error| error.to_string())?;
- url.query_pairs_mut().append_pair("q", query);
- Ok(url)
- }
- fn normalize_fetched_content(body: &str, content_type: &str) -> String {
- if content_type.contains("html") {
- html_to_text(body)
- } else {
- body.trim().to_string()
- }
- }
- fn summarize_web_fetch(
- url: &str,
- prompt: &str,
- content: &str,
- raw_body: &str,
- content_type: &str,
- ) -> String {
- let lower_prompt = prompt.to_lowercase();
- let compact = collapse_whitespace(content);
- let detail = if lower_prompt.contains("title") {
- extract_title(content, raw_body, content_type).map_or_else(
- || preview_text(&compact, 600),
- |title| format!("Title: {title}"),
- )
- } else if lower_prompt.contains("summary") || lower_prompt.contains("summarize") {
- preview_text(&compact, 900)
- } else {
- let preview = preview_text(&compact, 900);
- format!("Prompt: {prompt}\nContent preview:\n{preview}")
- };
- format!("Fetched {url}\n{detail}")
- }
- fn extract_title(content: &str, raw_body: &str, content_type: &str) -> Option<String> {
- if content_type.contains("html") {
- let lowered = raw_body.to_lowercase();
- if let Some(start) = lowered.find("<title>") {
- let after = start + "<title>".len();
- if let Some(end_rel) = lowered[after..].find("</title>") {
- let title =
- collapse_whitespace(&decode_html_entities(&raw_body[after..after + end_rel]));
- if !title.is_empty() {
- return Some(title);
- }
- }
- }
- }
- for line in content.lines() {
- let trimmed = line.trim();
- if !trimmed.is_empty() {
- return Some(trimmed.to_string());
- }
- }
- None
- }
- fn html_to_text(html: &str) -> String {
- let mut text = String::with_capacity(html.len());
- let mut in_tag = false;
- let mut previous_was_space = false;
- for ch in html.chars() {
- match ch {
- '<' => in_tag = true,
- '>' => in_tag = false,
- _ if in_tag => {}
- '&' => {
- text.push('&');
- previous_was_space = false;
- }
- ch if ch.is_whitespace() => {
- if !previous_was_space {
- text.push(' ');
- previous_was_space = true;
- }
- }
- _ => {
- text.push(ch);
- previous_was_space = false;
- }
- }
- }
- collapse_whitespace(&decode_html_entities(&text))
- }
- fn decode_html_entities(input: &str) -> String {
- input
- .replace("&", "&")
- .replace("<", "<")
- .replace(">", ">")
- .replace(""", "\"")
- .replace("'", "'")
- .replace(" ", " ")
- }
- fn collapse_whitespace(input: &str) -> String {
- input.split_whitespace().collect::<Vec<_>>().join(" ")
- }
- fn preview_text(input: &str, max_chars: usize) -> String {
- if input.chars().count() <= max_chars {
- return input.to_string();
- }
- let shortened = input.chars().take(max_chars).collect::<String>();
- format!("{}…", shortened.trim_end())
- }
- fn extract_search_hits(html: &str) -> Vec<SearchHit> {
- let mut hits = Vec::new();
- let mut remaining = html;
- while let Some(anchor_start) = remaining.find("result__a") {
- let after_class = &remaining[anchor_start..];
- let Some(href_idx) = after_class.find("href=") else {
- remaining = &after_class[1..];
- continue;
- };
- let href_slice = &after_class[href_idx + 5..];
- let Some((url, rest)) = extract_quoted_value(href_slice) else {
- remaining = &after_class[1..];
- continue;
- };
- let Some(close_tag_idx) = rest.find('>') else {
- remaining = &after_class[1..];
- continue;
- };
- let after_tag = &rest[close_tag_idx + 1..];
- let Some(end_anchor_idx) = after_tag.find("</a>") else {
- remaining = &after_tag[1..];
- continue;
- };
- let title = html_to_text(&after_tag[..end_anchor_idx]);
- if let Some(decoded_url) = decode_duckduckgo_redirect(&url) {
- hits.push(SearchHit {
- title: title.trim().to_string(),
- url: decoded_url,
- });
- }
- remaining = &after_tag[end_anchor_idx + 4..];
- }
- hits
- }
- fn extract_search_hits_from_generic_links(html: &str) -> Vec<SearchHit> {
- let mut hits = Vec::new();
- let mut remaining = html;
- while let Some(anchor_start) = remaining.find("<a") {
- let after_anchor = &remaining[anchor_start..];
- let Some(href_idx) = after_anchor.find("href=") else {
- remaining = &after_anchor[2..];
- continue;
- };
- let href_slice = &after_anchor[href_idx + 5..];
- let Some((url, rest)) = extract_quoted_value(href_slice) else {
- remaining = &after_anchor[2..];
- continue;
- };
- let Some(close_tag_idx) = rest.find('>') else {
- remaining = &after_anchor[2..];
- continue;
- };
- let after_tag = &rest[close_tag_idx + 1..];
- let Some(end_anchor_idx) = after_tag.find("</a>") else {
- remaining = &after_anchor[2..];
- continue;
- };
- let title = html_to_text(&after_tag[..end_anchor_idx]);
- if title.trim().is_empty() {
- remaining = &after_tag[end_anchor_idx + 4..];
- continue;
- }
- let decoded_url = decode_duckduckgo_redirect(&url).unwrap_or(url);
- if decoded_url.starts_with("http://") || decoded_url.starts_with("https://") {
- hits.push(SearchHit {
- title: title.trim().to_string(),
- url: decoded_url,
- });
- }
- remaining = &after_tag[end_anchor_idx + 4..];
- }
- hits
- }
- fn extract_quoted_value(input: &str) -> Option<(String, &str)> {
- let quote = input.chars().next()?;
- if quote != '"' && quote != '\'' {
- return None;
- }
- let rest = &input[quote.len_utf8()..];
- let end = rest.find(quote)?;
- Some((rest[..end].to_string(), &rest[end + quote.len_utf8()..]))
- }
- fn decode_duckduckgo_redirect(url: &str) -> Option<String> {
- if url.starts_with("http://") || url.starts_with("https://") {
- return Some(html_entity_decode_url(url));
- }
- let joined = if url.starts_with("//") {
- format!("https:{url}")
- } else if url.starts_with('/') {
- format!("https://duckduckgo.com{url}")
- } else {
- return None;
- };
- let parsed = reqwest::Url::parse(&joined).ok()?;
- if parsed.path() == "/l/" || parsed.path() == "/l" {
- for (key, value) in parsed.query_pairs() {
- if key == "uddg" {
- return Some(html_entity_decode_url(value.as_ref()));
- }
- }
- }
- Some(joined)
- }
- fn html_entity_decode_url(url: &str) -> String {
- decode_html_entities(url)
- }
- fn host_matches_list(url: &str, domains: &[String]) -> bool {
- let Ok(parsed) = reqwest::Url::parse(url) else {
- return false;
- };
- let Some(host) = parsed.host_str() else {
- return false;
- };
- let host = host.to_ascii_lowercase();
- domains.iter().any(|domain| {
- let normalized = normalize_domain_filter(domain);
- !normalized.is_empty() && (host == normalized || host.ends_with(&format!(".{normalized}")))
- })
- }
- fn normalize_domain_filter(domain: &str) -> String {
- let trimmed = domain.trim();
- let candidate = reqwest::Url::parse(trimmed)
- .ok()
- .and_then(|url| url.host_str().map(str::to_string))
- .unwrap_or_else(|| trimmed.to_string());
- candidate
- .trim()
- .trim_start_matches('.')
- .trim_end_matches('/')
- .to_ascii_lowercase()
- }
- fn dedupe_hits(hits: &mut Vec<SearchHit>) {
- let mut seen = BTreeSet::new();
- hits.retain(|hit| seen.insert(hit.url.clone()));
- }
- fn execute_todo_write(input: TodoWriteInput) -> Result<TodoWriteOutput, String> {
- validate_todos(&input.todos)?;
- let store_path = todo_store_path()?;
- let old_todos = if store_path.exists() {
- serde_json::from_str::<Vec<TodoItem>>(
- &std::fs::read_to_string(&store_path).map_err(|error| error.to_string())?,
- )
- .map_err(|error| error.to_string())?
- } else {
- Vec::new()
- };
- let all_done = input
- .todos
- .iter()
- .all(|todo| matches!(todo.status, TodoStatus::Completed));
- let persisted = if all_done {
- Vec::new()
- } else {
- input.todos.clone()
- };
- if let Some(parent) = store_path.parent() {
- std::fs::create_dir_all(parent).map_err(|error| error.to_string())?;
- }
- std::fs::write(
- &store_path,
- serde_json::to_string_pretty(&persisted).map_err(|error| error.to_string())?,
- )
- .map_err(|error| error.to_string())?;
- let verification_nudge_needed = (all_done
- && input.todos.len() >= 3
- && !input
- .todos
- .iter()
- .any(|todo| todo.content.to_lowercase().contains("verif")))
- .then_some(true);
- Ok(TodoWriteOutput {
- old_todos,
- new_todos: input.todos,
- verification_nudge_needed,
- })
- }
- fn execute_skill(input: SkillInput) -> Result<SkillOutput, String> {
- let skill_path = resolve_skill_path(&input.skill)?;
- let prompt = std::fs::read_to_string(&skill_path).map_err(|error| error.to_string())?;
- let description = parse_skill_description(&prompt);
- Ok(SkillOutput {
- skill: input.skill,
- path: skill_path.display().to_string(),
- args: input.args,
- description,
- prompt,
- })
- }
- fn validate_todos(todos: &[TodoItem]) -> Result<(), String> {
- if todos.is_empty() {
- return Err(String::from("todos must not be empty"));
- }
- // Allow multiple in_progress items for parallel workflows
- if todos.iter().any(|todo| todo.content.trim().is_empty()) {
- return Err(String::from("todo content must not be empty"));
- }
- if todos.iter().any(|todo| todo.active_form.trim().is_empty()) {
- return Err(String::from("todo activeForm must not be empty"));
- }
- Ok(())
- }
- fn todo_store_path() -> Result<std::path::PathBuf, String> {
- if let Ok(path) = std::env::var("CLAWD_TODO_STORE") {
- return Ok(std::path::PathBuf::from(path));
- }
- let cwd = std::env::current_dir().map_err(|error| error.to_string())?;
- Ok(cwd.join(".clawd-todos.json"))
- }
- fn resolve_skill_path(skill: &str) -> Result<std::path::PathBuf, String> {
- let requested = skill.trim().trim_start_matches('/').trim_start_matches('$');
- if requested.is_empty() {
- return Err(String::from("skill must not be empty"));
- }
- let mut candidates = Vec::new();
- if let Ok(codex_home) = std::env::var("CODEX_HOME") {
- candidates.push(std::path::PathBuf::from(codex_home).join("skills"));
- }
- if let Ok(home) = std::env::var("HOME") {
- let home = std::path::PathBuf::from(home);
- candidates.push(home.join(".agents").join("skills"));
- candidates.push(home.join(".config").join("opencode").join("skills"));
- candidates.push(home.join(".codex").join("skills"));
- }
- candidates.push(std::path::PathBuf::from("/home/bellman/.codex/skills"));
- for root in candidates {
- let direct = root.join(requested).join("SKILL.md");
- if direct.exists() {
- return Ok(direct);
- }
- if let Ok(entries) = std::fs::read_dir(&root) {
- for entry in entries.flatten() {
- let path = entry.path().join("SKILL.md");
- if !path.exists() {
- continue;
- }
- if entry
- .file_name()
- .to_string_lossy()
- .eq_ignore_ascii_case(requested)
- {
- return Ok(path);
- }
- }
- }
- }
- Err(format!("unknown skill: {requested}"))
- }
- const DEFAULT_AGENT_MODEL: &str = "claude-opus-4-6";
- const DEFAULT_AGENT_SYSTEM_DATE: &str = "2026-03-31";
- const DEFAULT_AGENT_MAX_ITERATIONS: usize = 32;
- fn execute_agent(input: AgentInput) -> Result<AgentOutput, String> {
- execute_agent_with_spawn(input, spawn_agent_job)
- }
- fn execute_agent_with_spawn<F>(input: AgentInput, spawn_fn: F) -> Result<AgentOutput, String>
- where
- F: FnOnce(AgentJob) -> Result<(), String>,
- {
- if input.description.trim().is_empty() {
- return Err(String::from("description must not be empty"));
- }
- if input.prompt.trim().is_empty() {
- return Err(String::from("prompt must not be empty"));
- }
- let agent_id = make_agent_id();
- let output_dir = agent_store_dir()?;
- std::fs::create_dir_all(&output_dir).map_err(|error| error.to_string())?;
- let output_file = output_dir.join(format!("{agent_id}.md"));
- let manifest_file = output_dir.join(format!("{agent_id}.json"));
- let normalized_subagent_type = normalize_subagent_type(input.subagent_type.as_deref());
- let model = resolve_agent_model(input.model.as_deref());
- let agent_name = input
- .name
- .as_deref()
- .map(slugify_agent_name)
- .filter(|name| !name.is_empty())
- .unwrap_or_else(|| slugify_agent_name(&input.description));
- let created_at = iso8601_now();
- let system_prompt = build_agent_system_prompt(&normalized_subagent_type)?;
- let allowed_tools = allowed_tools_for_subagent(&normalized_subagent_type);
- let output_contents = format!(
- "# Agent Task
- - id: {}
- - name: {}
- - description: {}
- - subagent_type: {}
- - created_at: {}
- ## Prompt
- {}
- ",
- agent_id, agent_name, input.description, normalized_subagent_type, created_at, input.prompt
- );
- std::fs::write(&output_file, output_contents).map_err(|error| error.to_string())?;
- let manifest = AgentOutput {
- agent_id,
- name: agent_name,
- description: input.description,
- subagent_type: Some(normalized_subagent_type),
- model: Some(model),
- status: String::from("running"),
- output_file: output_file.display().to_string(),
- manifest_file: manifest_file.display().to_string(),
- created_at: created_at.clone(),
- started_at: Some(created_at),
- completed_at: None,
- error: None,
- };
- write_agent_manifest(&manifest)?;
- let manifest_for_spawn = manifest.clone();
- let job = AgentJob {
- manifest: manifest_for_spawn,
- prompt: input.prompt,
- system_prompt,
- allowed_tools,
- };
- if let Err(error) = spawn_fn(job) {
- let error = format!("failed to spawn sub-agent: {error}");
- persist_agent_terminal_state(&manifest, "failed", None, Some(error.clone()))?;
- return Err(error);
- }
- Ok(manifest)
- }
- fn spawn_agent_job(job: AgentJob) -> Result<(), String> {
- let thread_name = format!("clawd-agent-{}", job.manifest.agent_id);
- std::thread::Builder::new()
- .name(thread_name)
- .spawn(move || {
- let result =
- std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| run_agent_job(&job)));
- match result {
- Ok(Ok(())) => {}
- Ok(Err(error)) => {
- let _ =
- persist_agent_terminal_state(&job.manifest, "failed", None, Some(error));
- }
- Err(_) => {
- let _ = persist_agent_terminal_state(
- &job.manifest,
- "failed",
- None,
- Some(String::from("sub-agent thread panicked")),
- );
- }
- }
- })
- .map(|_| ())
- .map_err(|error| error.to_string())
- }
- fn run_agent_job(job: &AgentJob) -> Result<(), String> {
- let mut runtime = build_agent_runtime(job)?.with_max_iterations(DEFAULT_AGENT_MAX_ITERATIONS);
- let summary = runtime
- .run_turn(job.prompt.clone(), None)
- .map_err(|error| error.to_string())?;
- let final_text = final_assistant_text(&summary);
- persist_agent_terminal_state(&job.manifest, "completed", Some(final_text.as_str()), None)
- }
- fn build_agent_runtime(
- job: &AgentJob,
- ) -> Result<ConversationRuntime<ProviderRuntimeClient, SubagentToolExecutor>, String> {
- let model = job
- .manifest
- .model
- .clone()
- .unwrap_or_else(|| DEFAULT_AGENT_MODEL.to_string());
- let allowed_tools = job.allowed_tools.clone();
- let api_client = ProviderRuntimeClient::new(model, allowed_tools.clone())?;
- let tool_executor = SubagentToolExecutor::new(allowed_tools);
- Ok(ConversationRuntime::new(
- Session::new(),
- api_client,
- tool_executor,
- agent_permission_policy(),
- job.system_prompt.clone(),
- ))
- }
- fn build_agent_system_prompt(subagent_type: &str) -> Result<Vec<String>, String> {
- let cwd = std::env::current_dir().map_err(|error| error.to_string())?;
- let mut prompt = load_system_prompt(
- cwd,
- DEFAULT_AGENT_SYSTEM_DATE.to_string(),
- std::env::consts::OS,
- "unknown",
- )
- .map_err(|error| error.to_string())?;
- prompt.push(format!(
- "You are a background sub-agent of type `{subagent_type}`. Work only on the delegated task, use only the tools available to you, do not ask the user questions, and finish with a concise result."
- ));
- Ok(prompt)
- }
- fn resolve_agent_model(model: Option<&str>) -> String {
- model
- .map(str::trim)
- .filter(|model| !model.is_empty())
- .unwrap_or(DEFAULT_AGENT_MODEL)
- .to_string()
- }
- fn allowed_tools_for_subagent(subagent_type: &str) -> BTreeSet<String> {
- let tools = match subagent_type {
- "Explore" => vec![
- "read_file",
- "glob_search",
- "grep_search",
- "WebFetch",
- "WebSearch",
- "ToolSearch",
- "Skill",
- "StructuredOutput",
- ],
- "Plan" => vec![
- "read_file",
- "glob_search",
- "grep_search",
- "WebFetch",
- "WebSearch",
- "ToolSearch",
- "Skill",
- "TodoWrite",
- "StructuredOutput",
- "SendUserMessage",
- ],
- "Verification" => vec![
- "bash",
- "read_file",
- "glob_search",
- "grep_search",
- "WebFetch",
- "WebSearch",
- "ToolSearch",
- "TodoWrite",
- "StructuredOutput",
- "SendUserMessage",
- "PowerShell",
- ],
- "claw-guide" => vec![
- "read_file",
- "glob_search",
- "grep_search",
- "WebFetch",
- "WebSearch",
- "ToolSearch",
- "Skill",
- "StructuredOutput",
- "SendUserMessage",
- ],
- "statusline-setup" => vec![
- "bash",
- "read_file",
- "write_file",
- "edit_file",
- "glob_search",
- "grep_search",
- "ToolSearch",
- ],
- _ => vec![
- "bash",
- "read_file",
- "write_file",
- "edit_file",
- "glob_search",
- "grep_search",
- "WebFetch",
- "WebSearch",
- "TodoWrite",
- "Skill",
- "ToolSearch",
- "NotebookEdit",
- "Sleep",
- "SendUserMessage",
- "Config",
- "StructuredOutput",
- "REPL",
- "PowerShell",
- ],
- };
- tools.into_iter().map(str::to_string).collect()
- }
- fn agent_permission_policy() -> PermissionPolicy {
- mvp_tool_specs().into_iter().fold(
- PermissionPolicy::new(PermissionMode::DangerFullAccess),
- |policy, spec| policy.with_tool_requirement(spec.name, spec.required_permission),
- )
- }
- fn write_agent_manifest(manifest: &AgentOutput) -> Result<(), String> {
- std::fs::write(
- &manifest.manifest_file,
- serde_json::to_string_pretty(manifest).map_err(|error| error.to_string())?,
- )
- .map_err(|error| error.to_string())
- }
- fn persist_agent_terminal_state(
- manifest: &AgentOutput,
- status: &str,
- result: Option<&str>,
- error: Option<String>,
- ) -> Result<(), String> {
- append_agent_output(
- &manifest.output_file,
- &format_agent_terminal_output(status, result, error.as_deref()),
- )?;
- let mut next_manifest = manifest.clone();
- next_manifest.status = status.to_string();
- next_manifest.completed_at = Some(iso8601_now());
- next_manifest.error = error;
- write_agent_manifest(&next_manifest)
- }
- fn append_agent_output(path: &str, suffix: &str) -> Result<(), String> {
- use std::io::Write as _;
- let mut file = std::fs::OpenOptions::new()
- .append(true)
- .open(path)
- .map_err(|error| error.to_string())?;
- file.write_all(suffix.as_bytes())
- .map_err(|error| error.to_string())
- }
- fn format_agent_terminal_output(status: &str, result: Option<&str>, error: Option<&str>) -> String {
- let mut sections = vec![format!("\n## Result\n\n- status: {status}\n")];
- if let Some(result) = result.filter(|value| !value.trim().is_empty()) {
- sections.push(format!("\n### Final response\n\n{}\n", result.trim()));
- }
- if let Some(error) = error.filter(|value| !value.trim().is_empty()) {
- sections.push(format!("\n### Error\n\n{}\n", error.trim()));
- }
- sections.join("")
- }
- struct ProviderRuntimeClient {
- runtime: tokio::runtime::Runtime,
- client: ProviderClient,
- model: String,
- allowed_tools: BTreeSet<String>,
- }
- impl ProviderRuntimeClient {
- #[allow(clippy::needless_pass_by_value)]
- fn new(model: String, allowed_tools: BTreeSet<String>) -> Result<Self, String> {
- let model = resolve_model_alias(&model).clone();
- let client = ProviderClient::from_model(&model).map_err(|error| error.to_string())?;
- Ok(Self {
- runtime: tokio::runtime::Runtime::new().map_err(|error| error.to_string())?,
- client,
- model,
- allowed_tools,
- })
- }
- }
- impl ApiClient for ProviderRuntimeClient {
- #[allow(clippy::too_many_lines)]
- fn stream(&mut self, request: ApiRequest) -> Result<Vec<AssistantEvent>, RuntimeError> {
- let tools = tool_specs_for_allowed_tools(Some(&self.allowed_tools))
- .into_iter()
- .map(|spec| ToolDefinition {
- name: spec.name.to_string(),
- description: Some(spec.description.to_string()),
- input_schema: spec.input_schema,
- })
- .collect::<Vec<_>>();
- 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: (!tools.is_empty()).then_some(tools),
- tool_choice: (!self.allowed_tools.is_empty()).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 events = Vec::new();
- let mut pending_tools: BTreeMap<u32, (String, String, String)> = BTreeMap::new();
- 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, 0, &mut events, &mut pending_tools, true);
- }
- }
- ApiStreamEvent::ContentBlockStart(start) => {
- push_output_block(
- start.content_block,
- start.index,
- &mut events,
- &mut pending_tools,
- true,
- );
- }
- ApiStreamEvent::ContentBlockDelta(delta) => match delta.delta {
- ContentBlockDelta::TextDelta { text } => {
- if !text.is_empty() {
- events.push(AssistantEvent::TextDelta(text));
- }
- }
- ContentBlockDelta::InputJsonDelta { partial_json } => {
- if let Some((_, _, input)) = pending_tools.get_mut(&delta.index) {
- input.push_str(&partial_json);
- }
- }
- ContentBlockDelta::ThinkingDelta { .. }
- | ContentBlockDelta::SignatureDelta { .. } => {}
- },
- ApiStreamEvent::ContentBlockStop(stop) => {
- if let Some((id, name, input)) = pending_tools.remove(&stop.index) {
- events.push(AssistantEvent::ToolUse { id, name, input });
- }
- }
- ApiStreamEvent::MessageDelta(delta) => {
- events.push(AssistantEvent::Usage(delta.usage.token_usage()));
- }
- ApiStreamEvent::MessageStop(_) => {
- saw_stop = true;
- events.push(AssistantEvent::MessageStop);
- }
- }
- }
- push_prompt_cache_record(&self.client, &mut events);
- 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()))?;
- let mut events = response_to_events(response);
- push_prompt_cache_record(&self.client, &mut events);
- Ok(events)
- })
- }
- }
- struct SubagentToolExecutor {
- allowed_tools: BTreeSet<String>,
- }
- impl SubagentToolExecutor {
- fn new(allowed_tools: BTreeSet<String>) -> Self {
- Self { allowed_tools }
- }
- }
- impl ToolExecutor for SubagentToolExecutor {
- fn execute(&mut self, tool_name: &str, input: &str) -> Result<String, ToolError> {
- if !self.allowed_tools.contains(tool_name) {
- return Err(ToolError::new(format!(
- "tool `{tool_name}` is not enabled for this sub-agent"
- )));
- }
- let value = serde_json::from_str(input)
- .map_err(|error| ToolError::new(format!("invalid tool input JSON: {error}")))?;
- execute_tool(tool_name, &value).map_err(ToolError::new)
- }
- }
- fn tool_specs_for_allowed_tools(allowed_tools: Option<&BTreeSet<String>>) -> Vec<ToolSpec> {
- mvp_tool_specs()
- .into_iter()
- .filter(|spec| allowed_tools.is_none_or(|allowed| allowed.contains(spec.name)))
- .collect()
- }
- 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 push_output_block(
- block: OutputContentBlock,
- block_index: u32,
- events: &mut Vec<AssistantEvent>,
- pending_tools: &mut BTreeMap<u32, (String, String, String)>,
- streaming_tool_input: bool,
- ) {
- match block {
- OutputContentBlock::Text { text } => {
- if !text.is_empty() {
- events.push(AssistantEvent::TextDelta(text));
- }
- }
- OutputContentBlock::ToolUse { id, name, input } => {
- 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_tools.insert(block_index, (id, name, initial_input));
- }
- OutputContentBlock::Thinking { .. } | OutputContentBlock::RedactedThinking { .. } => {}
- }
- }
- fn response_to_events(response: MessageResponse) -> Vec<AssistantEvent> {
- let mut events = Vec::new();
- let mut pending_tools = BTreeMap::new();
- for (index, block) in response.content.into_iter().enumerate() {
- let index = u32::try_from(index).expect("response block index overflow");
- push_output_block(block, index, &mut events, &mut pending_tools, false);
- if let Some((id, name, input)) = pending_tools.remove(&index) {
- events.push(AssistantEvent::ToolUse { id, name, input });
- }
- }
- events.push(AssistantEvent::Usage(response.usage.token_usage()));
- events.push(AssistantEvent::MessageStop);
- events
- }
- fn push_prompt_cache_record(client: &ProviderClient, events: &mut Vec<AssistantEvent>) {
- if let Some(record) = client.take_last_prompt_cache_record() {
- if let Some(event) = prompt_cache_record_to_runtime_event(record) {
- events.push(AssistantEvent::PromptCache(event));
- }
- }
- }
- fn prompt_cache_record_to_runtime_event(
- record: api::PromptCacheRecord,
- ) -> Option<PromptCacheEvent> {
- let cache_break = record.cache_break?;
- Some(PromptCacheEvent {
- unexpected: cache_break.unexpected,
- reason: cache_break.reason,
- previous_cache_read_input_tokens: cache_break.previous_cache_read_input_tokens,
- current_cache_read_input_tokens: cache_break.current_cache_read_input_tokens,
- token_drop: cache_break.token_drop,
- })
- }
- 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()
- }
- #[allow(clippy::needless_pass_by_value)]
- fn execute_tool_search(input: ToolSearchInput) -> ToolSearchOutput {
- let deferred = deferred_tool_specs();
- let max_results = input.max_results.unwrap_or(5).max(1);
- let query = input.query.trim().to_string();
- let normalized_query = normalize_tool_search_query(&query);
- let matches = search_tool_specs(&query, max_results, &deferred);
- ToolSearchOutput {
- matches,
- query,
- normalized_query,
- total_deferred_tools: deferred.len(),
- pending_mcp_servers: None,
- }
- }
- fn deferred_tool_specs() -> Vec<ToolSpec> {
- mvp_tool_specs()
- .into_iter()
- .filter(|spec| {
- !matches!(
- spec.name,
- "bash" | "read_file" | "write_file" | "edit_file" | "glob_search" | "grep_search"
- )
- })
- .collect()
- }
- fn search_tool_specs(query: &str, max_results: usize, specs: &[ToolSpec]) -> Vec<String> {
- let lowered = query.to_lowercase();
- if let Some(selection) = lowered.strip_prefix("select:") {
- return selection
- .split(',')
- .map(str::trim)
- .filter(|part| !part.is_empty())
- .filter_map(|wanted| {
- let wanted = canonical_tool_token(wanted);
- specs
- .iter()
- .find(|spec| canonical_tool_token(spec.name) == wanted)
- .map(|spec| spec.name.to_string())
- })
- .take(max_results)
- .collect();
- }
- let mut required = Vec::new();
- let mut optional = Vec::new();
- for term in lowered.split_whitespace() {
- if let Some(rest) = term.strip_prefix('+') {
- if !rest.is_empty() {
- required.push(rest);
- }
- } else {
- optional.push(term);
- }
- }
- let terms = if required.is_empty() {
- optional.clone()
- } else {
- required.iter().chain(optional.iter()).copied().collect()
- };
- let mut scored = specs
- .iter()
- .filter_map(|spec| {
- let name = spec.name.to_lowercase();
- let canonical_name = canonical_tool_token(spec.name);
- let normalized_description = normalize_tool_search_query(spec.description);
- let haystack = format!(
- "{name} {} {canonical_name}",
- spec.description.to_lowercase()
- );
- let normalized_haystack = format!("{canonical_name} {normalized_description}");
- if required.iter().any(|term| !haystack.contains(term)) {
- return None;
- }
- let mut score = 0_i32;
- for term in &terms {
- let canonical_term = canonical_tool_token(term);
- if haystack.contains(term) {
- score += 2;
- }
- if name == *term {
- score += 8;
- }
- if name.contains(term) {
- score += 4;
- }
- if canonical_name == canonical_term {
- score += 12;
- }
- if normalized_haystack.contains(&canonical_term) {
- score += 3;
- }
- }
- if score == 0 && !lowered.is_empty() {
- return None;
- }
- Some((score, spec.name.to_string()))
- })
- .collect::<Vec<_>>();
- scored.sort_by(|left, right| right.0.cmp(&left.0).then_with(|| left.1.cmp(&right.1)));
- scored
- .into_iter()
- .map(|(_, name)| name)
- .take(max_results)
- .collect()
- }
- fn normalize_tool_search_query(query: &str) -> String {
- query
- .trim()
- .split(|ch: char| ch.is_whitespace() || ch == ',')
- .filter(|term| !term.is_empty())
- .map(canonical_tool_token)
- .collect::<Vec<_>>()
- .join(" ")
- }
- fn canonical_tool_token(value: &str) -> String {
- let mut canonical = value
- .chars()
- .filter(char::is_ascii_alphanumeric)
- .flat_map(char::to_lowercase)
- .collect::<String>();
- if let Some(stripped) = canonical.strip_suffix("tool") {
- canonical = stripped.to_string();
- }
- canonical
- }
- fn agent_store_dir() -> Result<std::path::PathBuf, String> {
- if let Ok(path) = std::env::var("CLAWD_AGENT_STORE") {
- return Ok(std::path::PathBuf::from(path));
- }
- let cwd = std::env::current_dir().map_err(|error| error.to_string())?;
- if let Some(workspace_root) = cwd.ancestors().nth(2) {
- return Ok(workspace_root.join(".clawd-agents"));
- }
- Ok(cwd.join(".clawd-agents"))
- }
- fn make_agent_id() -> String {
- let nanos = std::time::SystemTime::now()
- .duration_since(std::time::UNIX_EPOCH)
- .unwrap_or_default()
- .as_nanos();
- format!("agent-{nanos}")
- }
- fn slugify_agent_name(description: &str) -> String {
- let mut out = description
- .chars()
- .map(|ch| {
- if ch.is_ascii_alphanumeric() {
- ch.to_ascii_lowercase()
- } else {
- '-'
- }
- })
- .collect::<String>();
- while out.contains("--") {
- out = out.replace("--", "-");
- }
- out.trim_matches('-').chars().take(32).collect()
- }
- fn normalize_subagent_type(subagent_type: Option<&str>) -> String {
- let trimmed = subagent_type.map(str::trim).unwrap_or_default();
- if trimmed.is_empty() {
- return String::from("general-purpose");
- }
- match canonical_tool_token(trimmed).as_str() {
- "general" | "generalpurpose" | "generalpurposeagent" => String::from("general-purpose"),
- "explore" | "explorer" | "exploreagent" => String::from("Explore"),
- "plan" | "planagent" => String::from("Plan"),
- "verification" | "verificationagent" | "verify" | "verifier" => {
- String::from("Verification")
- }
- "clawguide" | "clawguideagent" | "guide" => String::from("claw-guide"),
- "statusline" | "statuslinesetup" => String::from("statusline-setup"),
- _ => trimmed.to_string(),
- }
- }
- fn iso8601_now() -> String {
- std::time::SystemTime::now()
- .duration_since(std::time::UNIX_EPOCH)
- .unwrap_or_default()
- .as_secs()
- .to_string()
- }
- #[allow(clippy::too_many_lines)]
- fn execute_notebook_edit(input: NotebookEditInput) -> Result<NotebookEditOutput, String> {
- let path = std::path::PathBuf::from(&input.notebook_path);
- if path.extension().and_then(|ext| ext.to_str()) != Some("ipynb") {
- return Err(String::from(
- "File must be a Jupyter notebook (.ipynb file).",
- ));
- }
- let original_file = std::fs::read_to_string(&path).map_err(|error| error.to_string())?;
- let mut notebook: serde_json::Value =
- serde_json::from_str(&original_file).map_err(|error| error.to_string())?;
- let language = notebook
- .get("metadata")
- .and_then(|metadata| metadata.get("kernelspec"))
- .and_then(|kernelspec| kernelspec.get("language"))
- .and_then(serde_json::Value::as_str)
- .unwrap_or("python")
- .to_string();
- let cells = notebook
- .get_mut("cells")
- .and_then(serde_json::Value::as_array_mut)
- .ok_or_else(|| String::from("Notebook cells array not found"))?;
- let edit_mode = input.edit_mode.unwrap_or(NotebookEditMode::Replace);
- let target_index = match input.cell_id.as_deref() {
- Some(cell_id) => Some(resolve_cell_index(cells, Some(cell_id), edit_mode)?),
- None if matches!(
- edit_mode,
- NotebookEditMode::Replace | NotebookEditMode::Delete
- ) =>
- {
- Some(resolve_cell_index(cells, None, edit_mode)?)
- }
- None => None,
- };
- let resolved_cell_type = match edit_mode {
- NotebookEditMode::Delete => None,
- NotebookEditMode::Insert => Some(input.cell_type.unwrap_or(NotebookCellType::Code)),
- NotebookEditMode::Replace => Some(input.cell_type.unwrap_or_else(|| {
- target_index
- .and_then(|index| cells.get(index))
- .and_then(cell_kind)
- .unwrap_or(NotebookCellType::Code)
- })),
- };
- let new_source = require_notebook_source(input.new_source, edit_mode)?;
- let cell_id = match edit_mode {
- NotebookEditMode::Insert => {
- let resolved_cell_type = resolved_cell_type.expect("insert cell type");
- let new_id = make_cell_id(cells.len());
- let new_cell = build_notebook_cell(&new_id, resolved_cell_type, &new_source);
- let insert_at = target_index.map_or(cells.len(), |index| index + 1);
- cells.insert(insert_at, new_cell);
- cells
- .get(insert_at)
- .and_then(|cell| cell.get("id"))
- .and_then(serde_json::Value::as_str)
- .map(ToString::to_string)
- }
- NotebookEditMode::Delete => {
- let removed = cells.remove(target_index.expect("delete target index"));
- removed
- .get("id")
- .and_then(serde_json::Value::as_str)
- .map(ToString::to_string)
- }
- NotebookEditMode::Replace => {
- let resolved_cell_type = resolved_cell_type.expect("replace cell type");
- let cell = cells
- .get_mut(target_index.expect("replace target index"))
- .ok_or_else(|| String::from("Cell index out of range"))?;
- cell["source"] = serde_json::Value::Array(source_lines(&new_source));
- cell["cell_type"] = serde_json::Value::String(match resolved_cell_type {
- NotebookCellType::Code => String::from("code"),
- NotebookCellType::Markdown => String::from("markdown"),
- });
- match resolved_cell_type {
- NotebookCellType::Code => {
- if !cell.get("outputs").is_some_and(serde_json::Value::is_array) {
- cell["outputs"] = json!([]);
- }
- if cell.get("execution_count").is_none() {
- cell["execution_count"] = serde_json::Value::Null;
- }
- }
- NotebookCellType::Markdown => {
- if let Some(object) = cell.as_object_mut() {
- object.remove("outputs");
- object.remove("execution_count");
- }
- }
- }
- cell.get("id")
- .and_then(serde_json::Value::as_str)
- .map(ToString::to_string)
- }
- };
- let updated_file =
- serde_json::to_string_pretty(¬ebook).map_err(|error| error.to_string())?;
- std::fs::write(&path, &updated_file).map_err(|error| error.to_string())?;
- Ok(NotebookEditOutput {
- new_source,
- cell_id,
- cell_type: resolved_cell_type,
- language,
- edit_mode: format_notebook_edit_mode(edit_mode),
- error: None,
- notebook_path: path.display().to_string(),
- original_file,
- updated_file,
- })
- }
- fn require_notebook_source(
- source: Option<String>,
- edit_mode: NotebookEditMode,
- ) -> Result<String, String> {
- match edit_mode {
- NotebookEditMode::Delete => Ok(source.unwrap_or_default()),
- NotebookEditMode::Insert | NotebookEditMode::Replace => source
- .ok_or_else(|| String::from("new_source is required for insert and replace edits")),
- }
- }
- fn build_notebook_cell(cell_id: &str, cell_type: NotebookCellType, source: &str) -> Value {
- let mut cell = json!({
- "cell_type": match cell_type {
- NotebookCellType::Code => "code",
- NotebookCellType::Markdown => "markdown",
- },
- "id": cell_id,
- "metadata": {},
- "source": source_lines(source),
- });
- if let Some(object) = cell.as_object_mut() {
- match cell_type {
- NotebookCellType::Code => {
- object.insert(String::from("outputs"), json!([]));
- object.insert(String::from("execution_count"), Value::Null);
- }
- NotebookCellType::Markdown => {}
- }
- }
- cell
- }
- fn cell_kind(cell: &serde_json::Value) -> Option<NotebookCellType> {
- cell.get("cell_type")
- .and_then(serde_json::Value::as_str)
- .map(|kind| {
- if kind == "markdown" {
- NotebookCellType::Markdown
- } else {
- NotebookCellType::Code
- }
- })
- }
- #[allow(clippy::needless_pass_by_value)]
- fn execute_sleep(input: SleepInput) -> SleepOutput {
- std::thread::sleep(Duration::from_millis(input.duration_ms));
- SleepOutput {
- duration_ms: input.duration_ms,
- message: format!("Slept for {}ms", input.duration_ms),
- }
- }
- fn execute_brief(input: BriefInput) -> Result<BriefOutput, String> {
- if input.message.trim().is_empty() {
- return Err(String::from("message must not be empty"));
- }
- let attachments = input
- .attachments
- .as_ref()
- .map(|paths| {
- paths
- .iter()
- .map(|path| resolve_attachment(path))
- .collect::<Result<Vec<_>, String>>()
- })
- .transpose()?;
- let message = match input.status {
- BriefStatus::Normal | BriefStatus::Proactive => input.message,
- };
- Ok(BriefOutput {
- message,
- attachments,
- sent_at: iso8601_timestamp(),
- })
- }
- fn resolve_attachment(path: &str) -> Result<ResolvedAttachment, String> {
- let resolved = std::fs::canonicalize(path).map_err(|error| error.to_string())?;
- let metadata = std::fs::metadata(&resolved).map_err(|error| error.to_string())?;
- Ok(ResolvedAttachment {
- path: resolved.display().to_string(),
- size: metadata.len(),
- is_image: is_image_path(&resolved),
- })
- }
- fn is_image_path(path: &Path) -> bool {
- matches!(
- path.extension()
- .and_then(|ext| ext.to_str())
- .map(str::to_ascii_lowercase)
- .as_deref(),
- Some("png" | "jpg" | "jpeg" | "gif" | "webp" | "bmp" | "svg")
- )
- }
- fn execute_config(input: ConfigInput) -> Result<ConfigOutput, String> {
- let setting = input.setting.trim();
- if setting.is_empty() {
- return Err(String::from("setting must not be empty"));
- }
- let Some(spec) = supported_config_setting(setting) else {
- return Ok(ConfigOutput {
- success: false,
- operation: None,
- setting: None,
- value: None,
- previous_value: None,
- new_value: None,
- error: Some(format!("Unknown setting: \"{setting}\"")),
- });
- };
- let path = config_file_for_scope(spec.scope)?;
- let mut document = read_json_object(&path)?;
- if let Some(value) = input.value {
- let normalized = normalize_config_value(spec, value)?;
- let previous_value = get_nested_value(&document, spec.path).cloned();
- set_nested_value(&mut document, spec.path, normalized.clone());
- write_json_object(&path, &document)?;
- Ok(ConfigOutput {
- success: true,
- operation: Some(String::from("set")),
- setting: Some(setting.to_string()),
- value: Some(normalized.clone()),
- previous_value,
- new_value: Some(normalized),
- error: None,
- })
- } else {
- Ok(ConfigOutput {
- success: true,
- operation: Some(String::from("get")),
- setting: Some(setting.to_string()),
- value: get_nested_value(&document, spec.path).cloned(),
- previous_value: None,
- new_value: None,
- error: None,
- })
- }
- }
- fn execute_structured_output(input: StructuredOutputInput) -> StructuredOutputResult {
- StructuredOutputResult {
- data: String::from("Structured output provided successfully"),
- structured_output: input.0,
- }
- }
- fn execute_repl(input: ReplInput) -> Result<ReplOutput, String> {
- if input.code.trim().is_empty() {
- return Err(String::from("code must not be empty"));
- }
- let _ = input.timeout_ms;
- let runtime = resolve_repl_runtime(&input.language)?;
- let started = Instant::now();
- let output = Command::new(runtime.program)
- .args(runtime.args)
- .arg(&input.code)
- .output()
- .map_err(|error| error.to_string())?;
- Ok(ReplOutput {
- language: input.language,
- stdout: String::from_utf8_lossy(&output.stdout).into_owned(),
- stderr: String::from_utf8_lossy(&output.stderr).into_owned(),
- exit_code: output.status.code().unwrap_or(1),
- duration_ms: started.elapsed().as_millis(),
- })
- }
- struct ReplRuntime {
- program: &'static str,
- args: &'static [&'static str],
- }
- fn resolve_repl_runtime(language: &str) -> Result<ReplRuntime, String> {
- match language.trim().to_ascii_lowercase().as_str() {
- "python" | "py" => Ok(ReplRuntime {
- program: detect_first_command(&["python3", "python"])
- .ok_or_else(|| String::from("python runtime not found"))?,
- args: &["-c"],
- }),
- "javascript" | "js" | "node" => Ok(ReplRuntime {
- program: detect_first_command(&["node"])
- .ok_or_else(|| String::from("node runtime not found"))?,
- args: &["-e"],
- }),
- "sh" | "shell" | "bash" => Ok(ReplRuntime {
- program: detect_first_command(&["bash", "sh"])
- .ok_or_else(|| String::from("shell runtime not found"))?,
- args: &["-lc"],
- }),
- other => Err(format!("unsupported REPL language: {other}")),
- }
- }
- fn detect_first_command(commands: &[&'static str]) -> Option<&'static str> {
- commands
- .iter()
- .copied()
- .find(|command| command_exists(command))
- }
- #[derive(Clone, Copy)]
- enum ConfigScope {
- Global,
- Settings,
- }
- #[derive(Clone, Copy)]
- struct ConfigSettingSpec {
- scope: ConfigScope,
- kind: ConfigKind,
- path: &'static [&'static str],
- options: Option<&'static [&'static str]>,
- }
- #[derive(Clone, Copy)]
- enum ConfigKind {
- Boolean,
- String,
- }
- fn supported_config_setting(setting: &str) -> Option<ConfigSettingSpec> {
- Some(match setting {
- "theme" => ConfigSettingSpec {
- scope: ConfigScope::Global,
- kind: ConfigKind::String,
- path: &["theme"],
- options: None,
- },
- "editorMode" => ConfigSettingSpec {
- scope: ConfigScope::Global,
- kind: ConfigKind::String,
- path: &["editorMode"],
- options: Some(&["default", "vim", "emacs"]),
- },
- "verbose" => ConfigSettingSpec {
- scope: ConfigScope::Global,
- kind: ConfigKind::Boolean,
- path: &["verbose"],
- options: None,
- },
- "preferredNotifChannel" => ConfigSettingSpec {
- scope: ConfigScope::Global,
- kind: ConfigKind::String,
- path: &["preferredNotifChannel"],
- options: None,
- },
- "autoCompactEnabled" => ConfigSettingSpec {
- scope: ConfigScope::Global,
- kind: ConfigKind::Boolean,
- path: &["autoCompactEnabled"],
- options: None,
- },
- "autoMemoryEnabled" => ConfigSettingSpec {
- scope: ConfigScope::Settings,
- kind: ConfigKind::Boolean,
- path: &["autoMemoryEnabled"],
- options: None,
- },
- "autoDreamEnabled" => ConfigSettingSpec {
- scope: ConfigScope::Settings,
- kind: ConfigKind::Boolean,
- path: &["autoDreamEnabled"],
- options: None,
- },
- "fileCheckpointingEnabled" => ConfigSettingSpec {
- scope: ConfigScope::Global,
- kind: ConfigKind::Boolean,
- path: &["fileCheckpointingEnabled"],
- options: None,
- },
- "showTurnDuration" => ConfigSettingSpec {
- scope: ConfigScope::Global,
- kind: ConfigKind::Boolean,
- path: &["showTurnDuration"],
- options: None,
- },
- "terminalProgressBarEnabled" => ConfigSettingSpec {
- scope: ConfigScope::Global,
- kind: ConfigKind::Boolean,
- path: &["terminalProgressBarEnabled"],
- options: None,
- },
- "todoFeatureEnabled" => ConfigSettingSpec {
- scope: ConfigScope::Global,
- kind: ConfigKind::Boolean,
- path: &["todoFeatureEnabled"],
- options: None,
- },
- "model" => ConfigSettingSpec {
- scope: ConfigScope::Settings,
- kind: ConfigKind::String,
- path: &["model"],
- options: None,
- },
- "alwaysThinkingEnabled" => ConfigSettingSpec {
- scope: ConfigScope::Settings,
- kind: ConfigKind::Boolean,
- path: &["alwaysThinkingEnabled"],
- options: None,
- },
- "permissions.defaultMode" => ConfigSettingSpec {
- scope: ConfigScope::Settings,
- kind: ConfigKind::String,
- path: &["permissions", "defaultMode"],
- options: Some(&["default", "plan", "acceptEdits", "dontAsk", "auto"]),
- },
- "language" => ConfigSettingSpec {
- scope: ConfigScope::Settings,
- kind: ConfigKind::String,
- path: &["language"],
- options: None,
- },
- "teammateMode" => ConfigSettingSpec {
- scope: ConfigScope::Global,
- kind: ConfigKind::String,
- path: &["teammateMode"],
- options: Some(&["tmux", "in-process", "auto"]),
- },
- _ => return None,
- })
- }
- fn normalize_config_value(spec: ConfigSettingSpec, value: ConfigValue) -> Result<Value, String> {
- let normalized = match (spec.kind, value) {
- (ConfigKind::Boolean, ConfigValue::Bool(value)) => Value::Bool(value),
- (ConfigKind::Boolean, ConfigValue::String(value)) => {
- match value.trim().to_ascii_lowercase().as_str() {
- "true" => Value::Bool(true),
- "false" => Value::Bool(false),
- _ => return Err(String::from("setting requires true or false")),
- }
- }
- (ConfigKind::Boolean, ConfigValue::Number(_)) => {
- return Err(String::from("setting requires true or false"))
- }
- (ConfigKind::String, ConfigValue::String(value)) => Value::String(value),
- (ConfigKind::String, ConfigValue::Bool(value)) => Value::String(value.to_string()),
- (ConfigKind::String, ConfigValue::Number(value)) => json!(value),
- };
- if let Some(options) = spec.options {
- let Some(as_str) = normalized.as_str() else {
- return Err(String::from("setting requires a string value"));
- };
- if !options.iter().any(|option| option == &as_str) {
- return Err(format!(
- "Invalid value \"{as_str}\". Options: {}",
- options.join(", ")
- ));
- }
- }
- Ok(normalized)
- }
- fn config_file_for_scope(scope: ConfigScope) -> Result<PathBuf, String> {
- let cwd = std::env::current_dir().map_err(|error| error.to_string())?;
- Ok(match scope {
- ConfigScope::Global => config_home_dir()?.join("settings.json"),
- ConfigScope::Settings => cwd.join(".claw").join("settings.local.json"),
- })
- }
- fn config_home_dir() -> Result<PathBuf, String> {
- if let Ok(path) = std::env::var("CLAW_CONFIG_HOME") {
- return Ok(PathBuf::from(path));
- }
- let home = std::env::var("HOME").map_err(|_| String::from("HOME is not set"))?;
- Ok(PathBuf::from(home).join(".claw"))
- }
- fn read_json_object(path: &Path) -> Result<serde_json::Map<String, Value>, String> {
- match std::fs::read_to_string(path) {
- Ok(contents) => {
- if contents.trim().is_empty() {
- return Ok(serde_json::Map::new());
- }
- serde_json::from_str::<Value>(&contents)
- .map_err(|error| error.to_string())?
- .as_object()
- .cloned()
- .ok_or_else(|| String::from("config file must contain a JSON object"))
- }
- Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(serde_json::Map::new()),
- Err(error) => Err(error.to_string()),
- }
- }
- fn write_json_object(path: &Path, value: &serde_json::Map<String, Value>) -> Result<(), String> {
- if let Some(parent) = path.parent() {
- std::fs::create_dir_all(parent).map_err(|error| error.to_string())?;
- }
- std::fs::write(
- path,
- serde_json::to_string_pretty(value).map_err(|error| error.to_string())?,
- )
- .map_err(|error| error.to_string())
- }
- fn get_nested_value<'a>(
- value: &'a serde_json::Map<String, Value>,
- path: &[&str],
- ) -> Option<&'a Value> {
- let (first, rest) = path.split_first()?;
- let mut current = value.get(*first)?;
- for key in rest {
- current = current.as_object()?.get(*key)?;
- }
- Some(current)
- }
- fn set_nested_value(root: &mut serde_json::Map<String, Value>, path: &[&str], new_value: Value) {
- let (first, rest) = path.split_first().expect("config path must not be empty");
- if rest.is_empty() {
- root.insert((*first).to_string(), new_value);
- return;
- }
- let entry = root
- .entry((*first).to_string())
- .or_insert_with(|| Value::Object(serde_json::Map::new()));
- if !entry.is_object() {
- *entry = Value::Object(serde_json::Map::new());
- }
- let map = entry.as_object_mut().expect("object inserted");
- set_nested_value(map, rest, new_value);
- }
- fn iso8601_timestamp() -> String {
- if let Ok(output) = Command::new("date")
- .args(["-u", "+%Y-%m-%dT%H:%M:%SZ"])
- .output()
- {
- if output.status.success() {
- return String::from_utf8_lossy(&output.stdout).trim().to_string();
- }
- }
- iso8601_now()
- }
- #[allow(clippy::needless_pass_by_value)]
- fn execute_powershell(input: PowerShellInput) -> std::io::Result<runtime::BashCommandOutput> {
- let _ = &input.description;
- let shell = detect_powershell_shell()?;
- execute_shell_command(
- shell,
- &input.command,
- input.timeout,
- input.run_in_background,
- )
- }
- fn detect_powershell_shell() -> std::io::Result<&'static str> {
- if command_exists("pwsh") {
- Ok("pwsh")
- } else if command_exists("powershell") {
- Ok("powershell")
- } else {
- Err(std::io::Error::new(
- std::io::ErrorKind::NotFound,
- "PowerShell executable not found (expected `pwsh` or `powershell` in PATH)",
- ))
- }
- }
- fn command_exists(command: &str) -> bool {
- std::process::Command::new("sh")
- .arg("-lc")
- .arg(format!("command -v {command} >/dev/null 2>&1"))
- .status()
- .map(|status| status.success())
- .unwrap_or(false)
- }
- #[allow(clippy::too_many_lines)]
- fn execute_shell_command(
- shell: &str,
- command: &str,
- timeout: Option<u64>,
- run_in_background: Option<bool>,
- ) -> std::io::Result<runtime::BashCommandOutput> {
- if run_in_background.unwrap_or(false) {
- let child = std::process::Command::new(shell)
- .arg("-NoProfile")
- .arg("-NonInteractive")
- .arg("-Command")
- .arg(command)
- .stdin(std::process::Stdio::null())
- .stdout(std::process::Stdio::null())
- .stderr(std::process::Stdio::null())
- .spawn()?;
- return Ok(runtime::BashCommandOutput {
- stdout: String::new(),
- stderr: String::new(),
- raw_output_path: None,
- interrupted: false,
- is_image: None,
- background_task_id: Some(child.id().to_string()),
- backgrounded_by_user: Some(true),
- assistant_auto_backgrounded: Some(false),
- dangerously_disable_sandbox: None,
- return_code_interpretation: None,
- no_output_expected: Some(true),
- structured_content: None,
- persisted_output_path: None,
- persisted_output_size: None,
- sandbox_status: None,
- });
- }
- let mut process = std::process::Command::new(shell);
- process
- .arg("-NoProfile")
- .arg("-NonInteractive")
- .arg("-Command")
- .arg(command);
- process
- .stdout(std::process::Stdio::piped())
- .stderr(std::process::Stdio::piped());
- if let Some(timeout_ms) = timeout {
- let mut child = process.spawn()?;
- let started = Instant::now();
- loop {
- if let Some(status) = child.try_wait()? {
- let output = child.wait_with_output()?;
- return Ok(runtime::BashCommandOutput {
- stdout: String::from_utf8_lossy(&output.stdout).into_owned(),
- stderr: String::from_utf8_lossy(&output.stderr).into_owned(),
- raw_output_path: None,
- interrupted: false,
- is_image: None,
- background_task_id: None,
- backgrounded_by_user: None,
- assistant_auto_backgrounded: None,
- dangerously_disable_sandbox: None,
- return_code_interpretation: status
- .code()
- .filter(|code| *code != 0)
- .map(|code| format!("exit_code:{code}")),
- no_output_expected: Some(output.stdout.is_empty() && output.stderr.is_empty()),
- structured_content: None,
- persisted_output_path: None,
- persisted_output_size: None,
- sandbox_status: None,
- });
- }
- if started.elapsed() >= Duration::from_millis(timeout_ms) {
- let _ = child.kill();
- let output = child.wait_with_output()?;
- let stderr = String::from_utf8_lossy(&output.stderr).into_owned();
- let stderr = if stderr.trim().is_empty() {
- format!("Command exceeded timeout of {timeout_ms} ms")
- } else {
- format!(
- "{}
- Command exceeded timeout of {timeout_ms} ms",
- stderr.trim_end()
- )
- };
- return Ok(runtime::BashCommandOutput {
- stdout: String::from_utf8_lossy(&output.stdout).into_owned(),
- stderr,
- raw_output_path: None,
- interrupted: true,
- is_image: None,
- background_task_id: None,
- backgrounded_by_user: None,
- assistant_auto_backgrounded: None,
- dangerously_disable_sandbox: None,
- return_code_interpretation: Some(String::from("timeout")),
- no_output_expected: Some(false),
- structured_content: None,
- persisted_output_path: None,
- persisted_output_size: None,
- sandbox_status: None,
- });
- }
- std::thread::sleep(Duration::from_millis(10));
- }
- }
- let output = process.output()?;
- Ok(runtime::BashCommandOutput {
- stdout: String::from_utf8_lossy(&output.stdout).into_owned(),
- stderr: String::from_utf8_lossy(&output.stderr).into_owned(),
- raw_output_path: None,
- interrupted: false,
- is_image: None,
- background_task_id: None,
- backgrounded_by_user: None,
- assistant_auto_backgrounded: None,
- dangerously_disable_sandbox: None,
- return_code_interpretation: output
- .status
- .code()
- .filter(|code| *code != 0)
- .map(|code| format!("exit_code:{code}")),
- no_output_expected: Some(output.stdout.is_empty() && output.stderr.is_empty()),
- structured_content: None,
- persisted_output_path: None,
- persisted_output_size: None,
- sandbox_status: None,
- })
- }
- fn resolve_cell_index(
- cells: &[serde_json::Value],
- cell_id: Option<&str>,
- edit_mode: NotebookEditMode,
- ) -> Result<usize, String> {
- if cells.is_empty()
- && matches!(
- edit_mode,
- NotebookEditMode::Replace | NotebookEditMode::Delete
- )
- {
- return Err(String::from("Notebook has no cells to edit"));
- }
- if let Some(cell_id) = cell_id {
- cells
- .iter()
- .position(|cell| cell.get("id").and_then(serde_json::Value::as_str) == Some(cell_id))
- .ok_or_else(|| format!("Cell id not found: {cell_id}"))
- } else {
- Ok(cells.len().saturating_sub(1))
- }
- }
- fn source_lines(source: &str) -> Vec<serde_json::Value> {
- if source.is_empty() {
- return vec![serde_json::Value::String(String::new())];
- }
- source
- .split_inclusive('\n')
- .map(|line| serde_json::Value::String(line.to_string()))
- .collect()
- }
- fn format_notebook_edit_mode(mode: NotebookEditMode) -> String {
- match mode {
- NotebookEditMode::Replace => String::from("replace"),
- NotebookEditMode::Insert => String::from("insert"),
- NotebookEditMode::Delete => String::from("delete"),
- }
- }
- fn make_cell_id(index: usize) -> String {
- format!("cell-{}", index + 1)
- }
- fn parse_skill_description(contents: &str) -> Option<String> {
- for line in contents.lines() {
- if let Some(value) = line.strip_prefix("description:") {
- let trimmed = value.trim();
- if !trimmed.is_empty() {
- return Some(trimmed.to_string());
- }
- }
- }
- None
- }
- #[cfg(test)]
- mod tests {
- use std::collections::BTreeMap;
- use std::collections::BTreeSet;
- use std::fs;
- use std::io::{Read, Write};
- use std::net::{SocketAddr, TcpListener};
- use std::path::PathBuf;
- use std::sync::{Arc, Mutex, OnceLock};
- use std::thread;
- use std::time::Duration;
- use super::{
- agent_permission_policy, allowed_tools_for_subagent, execute_agent_with_spawn,
- execute_tool, final_assistant_text, mvp_tool_specs, permission_mode_from_plugin,
- persist_agent_terminal_state, push_output_block, AgentInput, AgentJob,
- SubagentToolExecutor,
- };
- use api::OutputContentBlock;
- use runtime::{ApiRequest, AssistantEvent, ConversationRuntime, RuntimeError, Session};
- use serde_json::json;
- fn env_lock() -> &'static Mutex<()> {
- static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
- LOCK.get_or_init(|| Mutex::new(()))
- }
- fn temp_path(name: &str) -> PathBuf {
- let unique = std::time::SystemTime::now()
- .duration_since(std::time::UNIX_EPOCH)
- .expect("time")
- .as_nanos();
- std::env::temp_dir().join(format!("clawd-tools-{unique}-{name}"))
- }
- #[test]
- fn exposes_mvp_tools() {
- let names = mvp_tool_specs()
- .into_iter()
- .map(|spec| spec.name)
- .collect::<Vec<_>>();
- assert!(names.contains(&"bash"));
- assert!(names.contains(&"read_file"));
- assert!(names.contains(&"WebFetch"));
- assert!(names.contains(&"WebSearch"));
- assert!(names.contains(&"TodoWrite"));
- assert!(names.contains(&"Skill"));
- assert!(names.contains(&"Agent"));
- assert!(names.contains(&"ToolSearch"));
- assert!(names.contains(&"NotebookEdit"));
- assert!(names.contains(&"Sleep"));
- assert!(names.contains(&"SendUserMessage"));
- assert!(names.contains(&"Config"));
- assert!(names.contains(&"StructuredOutput"));
- assert!(names.contains(&"REPL"));
- assert!(names.contains(&"PowerShell"));
- }
- #[test]
- fn rejects_unknown_tool_names() {
- let error = execute_tool("nope", &json!({})).expect_err("tool should be rejected");
- assert!(error.contains("unsupported tool"));
- }
- #[test]
- fn permission_mode_from_plugin_rejects_invalid_inputs() {
- let unknown_permission = permission_mode_from_plugin("admin")
- .expect_err("unknown plugin permission should fail");
- assert!(unknown_permission.contains("unsupported plugin permission: admin"));
- let empty_permission =
- permission_mode_from_plugin("").expect_err("empty plugin permission should fail");
- assert!(empty_permission.contains("unsupported plugin permission: "));
- }
- #[test]
- fn web_fetch_returns_prompt_aware_summary() {
- let server = TestServer::spawn(Arc::new(|request_line: &str| {
- assert!(request_line.starts_with("GET /page "));
- HttpResponse::html(
- 200,
- "OK",
- "<html><head><title>Ignored</title></head><body><h1>Test Page</h1><p>Hello <b>world</b> from local server.</p></body></html>",
- )
- }));
- let result = execute_tool(
- "WebFetch",
- &json!({
- "url": format!("http://{}/page", server.addr()),
- "prompt": "Summarize this page"
- }),
- )
- .expect("WebFetch should succeed");
- let output: serde_json::Value = serde_json::from_str(&result).expect("valid json");
- assert_eq!(output["code"], 200);
- let summary = output["result"].as_str().expect("result string");
- assert!(summary.contains("Fetched"));
- assert!(summary.contains("Test Page"));
- assert!(summary.contains("Hello world from local server"));
- let titled = execute_tool(
- "WebFetch",
- &json!({
- "url": format!("http://{}/page", server.addr()),
- "prompt": "What is the page title?"
- }),
- )
- .expect("WebFetch title query should succeed");
- let titled_output: serde_json::Value = serde_json::from_str(&titled).expect("valid json");
- let titled_summary = titled_output["result"].as_str().expect("result string");
- assert!(titled_summary.contains("Title: Ignored"));
- }
- #[test]
- fn web_fetch_supports_plain_text_and_rejects_invalid_url() {
- let server = TestServer::spawn(Arc::new(|request_line: &str| {
- assert!(request_line.starts_with("GET /plain "));
- HttpResponse::text(200, "OK", "plain text response")
- }));
- let result = execute_tool(
- "WebFetch",
- &json!({
- "url": format!("http://{}/plain", server.addr()),
- "prompt": "Show me the content"
- }),
- )
- .expect("WebFetch should succeed for text content");
- let output: serde_json::Value = serde_json::from_str(&result).expect("valid json");
- assert_eq!(output["url"], format!("http://{}/plain", server.addr()));
- assert!(output["result"]
- .as_str()
- .expect("result")
- .contains("plain text response"));
- let error = execute_tool(
- "WebFetch",
- &json!({
- "url": "not a url",
- "prompt": "Summarize"
- }),
- )
- .expect_err("invalid URL should fail");
- assert!(error.contains("relative URL without a base") || error.contains("invalid"));
- }
- #[test]
- fn web_search_extracts_and_filters_results() {
- let server = TestServer::spawn(Arc::new(|request_line: &str| {
- assert!(request_line.contains("GET /search?q=rust+web+search "));
- HttpResponse::html(
- 200,
- "OK",
- r#"
- <html><body>
- <a class="result__a" href="https://docs.rs/reqwest">Reqwest docs</a>
- <a class="result__a" href="https://example.com/blocked">Blocked result</a>
- </body></html>
- "#,
- )
- }));
- std::env::set_var(
- "CLAWD_WEB_SEARCH_BASE_URL",
- format!("http://{}/search", server.addr()),
- );
- let result = execute_tool(
- "WebSearch",
- &json!({
- "query": "rust web search",
- "allowed_domains": ["https://DOCS.rs/"],
- "blocked_domains": ["HTTPS://EXAMPLE.COM"]
- }),
- )
- .expect("WebSearch should succeed");
- std::env::remove_var("CLAWD_WEB_SEARCH_BASE_URL");
- let output: serde_json::Value = serde_json::from_str(&result).expect("valid json");
- assert_eq!(output["query"], "rust web search");
- let results = output["results"].as_array().expect("results array");
- let search_result = results
- .iter()
- .find(|item| item.get("content").is_some())
- .expect("search result block present");
- let content = search_result["content"].as_array().expect("content array");
- assert_eq!(content.len(), 1);
- assert_eq!(content[0]["title"], "Reqwest docs");
- assert_eq!(content[0]["url"], "https://docs.rs/reqwest");
- }
- #[test]
- fn web_search_handles_generic_links_and_invalid_base_url() {
- let _guard = env_lock()
- .lock()
- .unwrap_or_else(std::sync::PoisonError::into_inner);
- let server = TestServer::spawn(Arc::new(|request_line: &str| {
- assert!(request_line.contains("GET /fallback?q=generic+links "));
- HttpResponse::html(
- 200,
- "OK",
- r#"
- <html><body>
- <a href="https://example.com/one">Example One</a>
- <a href="https://example.com/one">Duplicate Example One</a>
- <a href="https://docs.rs/tokio">Tokio Docs</a>
- </body></html>
- "#,
- )
- }));
- std::env::set_var(
- "CLAWD_WEB_SEARCH_BASE_URL",
- format!("http://{}/fallback", server.addr()),
- );
- let result = execute_tool(
- "WebSearch",
- &json!({
- "query": "generic links"
- }),
- )
- .expect("WebSearch fallback parsing should succeed");
- std::env::remove_var("CLAWD_WEB_SEARCH_BASE_URL");
- let output: serde_json::Value = serde_json::from_str(&result).expect("valid json");
- let results = output["results"].as_array().expect("results array");
- let search_result = results
- .iter()
- .find(|item| item.get("content").is_some())
- .expect("search result block present");
- let content = search_result["content"].as_array().expect("content array");
- assert_eq!(content.len(), 2);
- assert_eq!(content[0]["url"], "https://example.com/one");
- assert_eq!(content[1]["url"], "https://docs.rs/tokio");
- std::env::set_var("CLAWD_WEB_SEARCH_BASE_URL", "://bad-base-url");
- let error = execute_tool("WebSearch", &json!({ "query": "generic links" }))
- .expect_err("invalid base URL should fail");
- std::env::remove_var("CLAWD_WEB_SEARCH_BASE_URL");
- assert!(error.contains("relative URL without a base") || error.contains("empty host"));
- }
- #[test]
- fn pending_tools_preserve_multiple_streaming_tool_calls_by_index() {
- let mut events = Vec::new();
- let mut pending_tools = BTreeMap::new();
- push_output_block(
- OutputContentBlock::ToolUse {
- id: "tool-1".to_string(),
- name: "read_file".to_string(),
- input: json!({}),
- },
- 1,
- &mut events,
- &mut pending_tools,
- true,
- );
- push_output_block(
- OutputContentBlock::ToolUse {
- id: "tool-2".to_string(),
- name: "grep_search".to_string(),
- input: json!({}),
- },
- 2,
- &mut events,
- &mut pending_tools,
- true,
- );
- pending_tools
- .get_mut(&1)
- .expect("first tool pending")
- .2
- .push_str("{\"path\":\"src/main.rs\"}");
- pending_tools
- .get_mut(&2)
- .expect("second tool pending")
- .2
- .push_str("{\"pattern\":\"TODO\"}");
- assert_eq!(
- pending_tools.remove(&1),
- Some((
- "tool-1".to_string(),
- "read_file".to_string(),
- "{\"path\":\"src/main.rs\"}".to_string(),
- ))
- );
- assert_eq!(
- pending_tools.remove(&2),
- Some((
- "tool-2".to_string(),
- "grep_search".to_string(),
- "{\"pattern\":\"TODO\"}".to_string(),
- ))
- );
- }
- #[test]
- fn todo_write_persists_and_returns_previous_state() {
- let _guard = env_lock()
- .lock()
- .unwrap_or_else(std::sync::PoisonError::into_inner);
- let path = temp_path("todos.json");
- std::env::set_var("CLAWD_TODO_STORE", &path);
- let first = execute_tool(
- "TodoWrite",
- &json!({
- "todos": [
- {"content": "Add tool", "activeForm": "Adding tool", "status": "in_progress"},
- {"content": "Run tests", "activeForm": "Running tests", "status": "pending"}
- ]
- }),
- )
- .expect("TodoWrite should succeed");
- let first_output: serde_json::Value = serde_json::from_str(&first).expect("valid json");
- assert_eq!(first_output["oldTodos"].as_array().expect("array").len(), 0);
- let second = execute_tool(
- "TodoWrite",
- &json!({
- "todos": [
- {"content": "Add tool", "activeForm": "Adding tool", "status": "completed"},
- {"content": "Run tests", "activeForm": "Running tests", "status": "completed"},
- {"content": "Verify", "activeForm": "Verifying", "status": "completed"}
- ]
- }),
- )
- .expect("TodoWrite should succeed");
- std::env::remove_var("CLAWD_TODO_STORE");
- let _ = std::fs::remove_file(path);
- let second_output: serde_json::Value = serde_json::from_str(&second).expect("valid json");
- assert_eq!(
- second_output["oldTodos"].as_array().expect("array").len(),
- 2
- );
- assert_eq!(
- second_output["newTodos"].as_array().expect("array").len(),
- 3
- );
- assert!(second_output["verificationNudgeNeeded"].is_null());
- }
- #[test]
- fn todo_write_rejects_invalid_payloads_and_sets_verification_nudge() {
- let _guard = env_lock()
- .lock()
- .unwrap_or_else(std::sync::PoisonError::into_inner);
- let path = temp_path("todos-errors.json");
- std::env::set_var("CLAWD_TODO_STORE", &path);
- let empty = execute_tool("TodoWrite", &json!({ "todos": [] }))
- .expect_err("empty todos should fail");
- assert!(empty.contains("todos must not be empty"));
- // Multiple in_progress items are now allowed for parallel workflows
- let _multi_active = execute_tool(
- "TodoWrite",
- &json!({
- "todos": [
- {"content": "One", "activeForm": "Doing one", "status": "in_progress"},
- {"content": "Two", "activeForm": "Doing two", "status": "in_progress"}
- ]
- }),
- )
- .expect("multiple in-progress todos should succeed");
- let blank_content = execute_tool(
- "TodoWrite",
- &json!({
- "todos": [
- {"content": " ", "activeForm": "Doing it", "status": "pending"}
- ]
- }),
- )
- .expect_err("blank content should fail");
- assert!(blank_content.contains("todo content must not be empty"));
- let nudge = execute_tool(
- "TodoWrite",
- &json!({
- "todos": [
- {"content": "Write tests", "activeForm": "Writing tests", "status": "completed"},
- {"content": "Fix errors", "activeForm": "Fixing errors", "status": "completed"},
- {"content": "Ship branch", "activeForm": "Shipping branch", "status": "completed"}
- ]
- }),
- )
- .expect("completed todos should succeed");
- std::env::remove_var("CLAWD_TODO_STORE");
- let _ = fs::remove_file(path);
- let output: serde_json::Value = serde_json::from_str(&nudge).expect("valid json");
- assert_eq!(output["verificationNudgeNeeded"], true);
- }
- #[test]
- fn skill_loads_local_skill_prompt() {
- let _guard = env_lock().lock().expect("env lock should acquire");
- let home = temp_path("skills-home");
- let skill_dir = home.join(".agents").join("skills").join("help");
- fs::create_dir_all(&skill_dir).expect("skill dir should exist");
- fs::write(
- skill_dir.join("SKILL.md"),
- "# help\n\nGuide on using oh-my-codex plugin\n",
- )
- .expect("skill file should exist");
- let original_home = std::env::var("HOME").ok();
- std::env::set_var("HOME", &home);
- let result = execute_tool(
- "Skill",
- &json!({
- "skill": "help",
- "args": "overview"
- }),
- )
- .expect("Skill should succeed");
- let output: serde_json::Value = serde_json::from_str(&result).expect("valid json");
- assert_eq!(output["skill"], "help");
- assert!(output["path"]
- .as_str()
- .expect("path")
- .ends_with("/help/SKILL.md"));
- assert!(output["prompt"]
- .as_str()
- .expect("prompt")
- .contains("Guide on using oh-my-codex plugin"));
- let dollar_result = execute_tool(
- "Skill",
- &json!({
- "skill": "$help"
- }),
- )
- .expect("Skill should accept $skill invocation form");
- let dollar_output: serde_json::Value =
- serde_json::from_str(&dollar_result).expect("valid json");
- assert_eq!(dollar_output["skill"], "$help");
- assert!(dollar_output["path"]
- .as_str()
- .expect("path")
- .ends_with("/help/SKILL.md"));
- if let Some(home) = original_home {
- std::env::set_var("HOME", home);
- } else {
- std::env::remove_var("HOME");
- }
- fs::remove_dir_all(home).expect("temp home should clean up");
- }
- #[test]
- fn tool_search_supports_keyword_and_select_queries() {
- let keyword = execute_tool(
- "ToolSearch",
- &json!({"query": "web current", "max_results": 3}),
- )
- .expect("ToolSearch should succeed");
- let keyword_output: serde_json::Value = serde_json::from_str(&keyword).expect("valid json");
- let matches = keyword_output["matches"].as_array().expect("matches");
- assert!(matches.iter().any(|value| value == "WebSearch"));
- let selected = execute_tool("ToolSearch", &json!({"query": "select:Agent,Skill"}))
- .expect("ToolSearch should succeed");
- let selected_output: serde_json::Value =
- serde_json::from_str(&selected).expect("valid json");
- assert_eq!(selected_output["matches"][0], "Agent");
- assert_eq!(selected_output["matches"][1], "Skill");
- let aliased = execute_tool("ToolSearch", &json!({"query": "AgentTool"}))
- .expect("ToolSearch should support tool aliases");
- let aliased_output: serde_json::Value = serde_json::from_str(&aliased).expect("valid json");
- assert_eq!(aliased_output["matches"][0], "Agent");
- assert_eq!(aliased_output["normalized_query"], "agent");
- let selected_with_alias =
- execute_tool("ToolSearch", &json!({"query": "select:AgentTool,Skill"}))
- .expect("ToolSearch alias select should succeed");
- let selected_with_alias_output: serde_json::Value =
- serde_json::from_str(&selected_with_alias).expect("valid json");
- assert_eq!(selected_with_alias_output["matches"][0], "Agent");
- assert_eq!(selected_with_alias_output["matches"][1], "Skill");
- }
- #[test]
- fn agent_persists_handoff_metadata() {
- let _guard = env_lock()
- .lock()
- .unwrap_or_else(std::sync::PoisonError::into_inner);
- let dir = temp_path("agent-store");
- std::env::set_var("CLAWD_AGENT_STORE", &dir);
- let captured = Arc::new(Mutex::new(None::<AgentJob>));
- let captured_for_spawn = Arc::clone(&captured);
- let manifest = execute_agent_with_spawn(
- AgentInput {
- description: "Audit the branch".to_string(),
- prompt: "Check tests and outstanding work.".to_string(),
- subagent_type: Some("Explore".to_string()),
- name: Some("ship-audit".to_string()),
- model: None,
- },
- move |job| {
- *captured_for_spawn
- .lock()
- .unwrap_or_else(std::sync::PoisonError::into_inner) = Some(job);
- Ok(())
- },
- )
- .expect("Agent should succeed");
- std::env::remove_var("CLAWD_AGENT_STORE");
- assert_eq!(manifest.name, "ship-audit");
- assert_eq!(manifest.subagent_type.as_deref(), Some("Explore"));
- assert_eq!(manifest.status, "running");
- assert!(!manifest.created_at.is_empty());
- assert!(manifest.started_at.is_some());
- assert!(manifest.completed_at.is_none());
- let contents = std::fs::read_to_string(&manifest.output_file).expect("agent file exists");
- let manifest_contents =
- std::fs::read_to_string(&manifest.manifest_file).expect("manifest file exists");
- assert!(contents.contains("Audit the branch"));
- assert!(contents.contains("Check tests and outstanding work."));
- assert!(manifest_contents.contains("\"subagentType\": \"Explore\""));
- assert!(manifest_contents.contains("\"status\": \"running\""));
- let captured_job = captured
- .lock()
- .unwrap_or_else(std::sync::PoisonError::into_inner)
- .clone()
- .expect("spawn job should be captured");
- assert_eq!(captured_job.prompt, "Check tests and outstanding work.");
- assert!(captured_job.allowed_tools.contains("read_file"));
- assert!(!captured_job.allowed_tools.contains("Agent"));
- let normalized = execute_tool(
- "Agent",
- &json!({
- "description": "Verify the branch",
- "prompt": "Check tests.",
- "subagent_type": "explorer"
- }),
- )
- .expect("Agent should normalize built-in aliases");
- let normalized_output: serde_json::Value =
- serde_json::from_str(&normalized).expect("valid json");
- assert_eq!(normalized_output["subagentType"], "Explore");
- let named = execute_tool(
- "Agent",
- &json!({
- "description": "Review the branch",
- "prompt": "Inspect diff.",
- "name": "Ship Audit!!!"
- }),
- )
- .expect("Agent should normalize explicit names");
- let named_output: serde_json::Value = serde_json::from_str(&named).expect("valid json");
- assert_eq!(named_output["name"], "ship-audit");
- let _ = std::fs::remove_dir_all(dir);
- }
- #[test]
- fn agent_fake_runner_can_persist_completion_and_failure() {
- let _guard = env_lock()
- .lock()
- .unwrap_or_else(std::sync::PoisonError::into_inner);
- let dir = temp_path("agent-runner");
- std::env::set_var("CLAWD_AGENT_STORE", &dir);
- let completed = execute_agent_with_spawn(
- AgentInput {
- description: "Complete the task".to_string(),
- prompt: "Do the work".to_string(),
- subagent_type: Some("Explore".to_string()),
- name: Some("complete-task".to_string()),
- model: Some("claude-sonnet-4-6".to_string()),
- },
- |job| {
- persist_agent_terminal_state(
- &job.manifest,
- "completed",
- Some("Finished successfully"),
- None,
- )
- },
- )
- .expect("completed agent should succeed");
- let completed_manifest = std::fs::read_to_string(&completed.manifest_file)
- .expect("completed manifest should exist");
- let completed_output =
- std::fs::read_to_string(&completed.output_file).expect("completed output should exist");
- assert!(completed_manifest.contains("\"status\": \"completed\""));
- assert!(completed_output.contains("Finished successfully"));
- let failed = execute_agent_with_spawn(
- AgentInput {
- description: "Fail the task".to_string(),
- prompt: "Do the failing work".to_string(),
- subagent_type: Some("Verification".to_string()),
- name: Some("fail-task".to_string()),
- model: None,
- },
- |job| {
- persist_agent_terminal_state(
- &job.manifest,
- "failed",
- None,
- Some(String::from("simulated failure")),
- )
- },
- )
- .expect("failed agent should still spawn");
- let failed_manifest =
- std::fs::read_to_string(&failed.manifest_file).expect("failed manifest should exist");
- let failed_output =
- std::fs::read_to_string(&failed.output_file).expect("failed output should exist");
- assert!(failed_manifest.contains("\"status\": \"failed\""));
- assert!(failed_manifest.contains("simulated failure"));
- assert!(failed_output.contains("simulated failure"));
- let spawn_error = execute_agent_with_spawn(
- AgentInput {
- description: "Spawn error task".to_string(),
- prompt: "Never starts".to_string(),
- subagent_type: None,
- name: Some("spawn-error".to_string()),
- model: None,
- },
- |_| Err(String::from("thread creation failed")),
- )
- .expect_err("spawn errors should surface");
- assert!(spawn_error.contains("failed to spawn sub-agent"));
- let spawn_error_manifest = std::fs::read_dir(&dir)
- .expect("agent dir should exist")
- .filter_map(Result::ok)
- .map(|entry| entry.path())
- .filter(|path| path.extension().and_then(|ext| ext.to_str()) == Some("json"))
- .find_map(|path| {
- let contents = std::fs::read_to_string(&path).ok()?;
- contents
- .contains("\"name\": \"spawn-error\"")
- .then_some(contents)
- })
- .expect("failed manifest should still be written");
- assert!(spawn_error_manifest.contains("\"status\": \"failed\""));
- assert!(spawn_error_manifest.contains("thread creation failed"));
- std::env::remove_var("CLAWD_AGENT_STORE");
- let _ = std::fs::remove_dir_all(dir);
- }
- #[test]
- fn agent_tool_subset_mapping_is_expected() {
- let general = allowed_tools_for_subagent("general-purpose");
- assert!(general.contains("bash"));
- assert!(general.contains("write_file"));
- assert!(!general.contains("Agent"));
- let explore = allowed_tools_for_subagent("Explore");
- assert!(explore.contains("read_file"));
- assert!(explore.contains("grep_search"));
- assert!(!explore.contains("bash"));
- let plan = allowed_tools_for_subagent("Plan");
- assert!(plan.contains("TodoWrite"));
- assert!(plan.contains("StructuredOutput"));
- assert!(!plan.contains("Agent"));
- let verification = allowed_tools_for_subagent("Verification");
- assert!(verification.contains("bash"));
- assert!(verification.contains("PowerShell"));
- assert!(!verification.contains("write_file"));
- }
- #[derive(Debug)]
- struct MockSubagentApiClient {
- calls: usize,
- input_path: String,
- }
- impl runtime::ApiClient for MockSubagentApiClient {
- fn stream(&mut self, request: ApiRequest) -> Result<Vec<AssistantEvent>, RuntimeError> {
- self.calls += 1;
- match self.calls {
- 1 => {
- assert_eq!(request.messages.len(), 1);
- Ok(vec![
- AssistantEvent::ToolUse {
- id: "tool-1".to_string(),
- name: "read_file".to_string(),
- input: json!({ "path": self.input_path }).to_string(),
- },
- AssistantEvent::MessageStop,
- ])
- }
- 2 => {
- assert!(request.messages.len() >= 3);
- Ok(vec![
- AssistantEvent::TextDelta("Scope: completed mock review".to_string()),
- AssistantEvent::MessageStop,
- ])
- }
- _ => panic!("unexpected mock stream call"),
- }
- }
- }
- #[test]
- fn subagent_runtime_executes_tool_loop_with_isolated_session() {
- let _guard = env_lock()
- .lock()
- .unwrap_or_else(std::sync::PoisonError::into_inner);
- let path = temp_path("subagent-input.txt");
- std::fs::write(&path, "hello from child").expect("write input file");
- let mut runtime = ConversationRuntime::new(
- Session::new(),
- MockSubagentApiClient {
- calls: 0,
- input_path: path.display().to_string(),
- },
- SubagentToolExecutor::new(BTreeSet::from([String::from("read_file")])),
- agent_permission_policy(),
- vec![String::from("system prompt")],
- );
- let summary = runtime
- .run_turn("Inspect the delegated file", None)
- .expect("subagent loop should succeed");
- assert_eq!(
- final_assistant_text(&summary),
- "Scope: completed mock review"
- );
- assert!(runtime
- .session()
- .messages
- .iter()
- .flat_map(|message| message.blocks.iter())
- .any(|block| matches!(
- block,
- runtime::ContentBlock::ToolResult { output, .. }
- if output.contains("hello from child")
- )));
- let _ = std::fs::remove_file(path);
- }
- #[test]
- fn agent_rejects_blank_required_fields() {
- let missing_description = execute_tool(
- "Agent",
- &json!({
- "description": " ",
- "prompt": "Inspect"
- }),
- )
- .expect_err("blank description should fail");
- assert!(missing_description.contains("description must not be empty"));
- let missing_prompt = execute_tool(
- "Agent",
- &json!({
- "description": "Inspect branch",
- "prompt": " "
- }),
- )
- .expect_err("blank prompt should fail");
- assert!(missing_prompt.contains("prompt must not be empty"));
- }
- #[test]
- fn notebook_edit_replaces_inserts_and_deletes_cells() {
- let path = temp_path("notebook.ipynb");
- std::fs::write(
- &path,
- r#"{
- "cells": [
- {"cell_type": "code", "id": "cell-a", "metadata": {}, "source": ["print(1)\n"], "outputs": [], "execution_count": null}
- ],
- "metadata": {"kernelspec": {"language": "python"}},
- "nbformat": 4,
- "nbformat_minor": 5
- }"#,
- )
- .expect("write notebook");
- let replaced = execute_tool(
- "NotebookEdit",
- &json!({
- "notebook_path": path.display().to_string(),
- "cell_id": "cell-a",
- "new_source": "print(2)\n",
- "edit_mode": "replace"
- }),
- )
- .expect("NotebookEdit replace should succeed");
- let replaced_output: serde_json::Value = serde_json::from_str(&replaced).expect("json");
- assert_eq!(replaced_output["cell_id"], "cell-a");
- assert_eq!(replaced_output["cell_type"], "code");
- let inserted = execute_tool(
- "NotebookEdit",
- &json!({
- "notebook_path": path.display().to_string(),
- "cell_id": "cell-a",
- "new_source": "# heading\n",
- "cell_type": "markdown",
- "edit_mode": "insert"
- }),
- )
- .expect("NotebookEdit insert should succeed");
- let inserted_output: serde_json::Value = serde_json::from_str(&inserted).expect("json");
- assert_eq!(inserted_output["cell_type"], "markdown");
- let appended = execute_tool(
- "NotebookEdit",
- &json!({
- "notebook_path": path.display().to_string(),
- "new_source": "print(3)\n",
- "edit_mode": "insert"
- }),
- )
- .expect("NotebookEdit append should succeed");
- let appended_output: serde_json::Value = serde_json::from_str(&appended).expect("json");
- assert_eq!(appended_output["cell_type"], "code");
- let deleted = execute_tool(
- "NotebookEdit",
- &json!({
- "notebook_path": path.display().to_string(),
- "cell_id": "cell-a",
- "edit_mode": "delete"
- }),
- )
- .expect("NotebookEdit delete should succeed without new_source");
- let deleted_output: serde_json::Value = serde_json::from_str(&deleted).expect("json");
- assert!(deleted_output["cell_type"].is_null());
- assert_eq!(deleted_output["new_source"], "");
- let final_notebook: serde_json::Value =
- serde_json::from_str(&std::fs::read_to_string(&path).expect("read notebook"))
- .expect("valid notebook json");
- let cells = final_notebook["cells"].as_array().expect("cells array");
- assert_eq!(cells.len(), 2);
- assert_eq!(cells[0]["cell_type"], "markdown");
- assert!(cells[0].get("outputs").is_none());
- assert_eq!(cells[1]["cell_type"], "code");
- assert_eq!(cells[1]["source"][0], "print(3)\n");
- let _ = std::fs::remove_file(path);
- }
- #[test]
- fn notebook_edit_rejects_invalid_inputs() {
- let text_path = temp_path("notebook.txt");
- fs::write(&text_path, "not a notebook").expect("write text file");
- let wrong_extension = execute_tool(
- "NotebookEdit",
- &json!({
- "notebook_path": text_path.display().to_string(),
- "new_source": "print(1)\n"
- }),
- )
- .expect_err("non-ipynb file should fail");
- assert!(wrong_extension.contains("Jupyter notebook"));
- let _ = fs::remove_file(&text_path);
- let empty_notebook = temp_path("empty.ipynb");
- fs::write(
- &empty_notebook,
- r#"{"cells":[],"metadata":{"kernelspec":{"language":"python"}},"nbformat":4,"nbformat_minor":5}"#,
- )
- .expect("write empty notebook");
- let missing_source = execute_tool(
- "NotebookEdit",
- &json!({
- "notebook_path": empty_notebook.display().to_string(),
- "edit_mode": "insert"
- }),
- )
- .expect_err("insert without source should fail");
- assert!(missing_source.contains("new_source is required"));
- let missing_cell = execute_tool(
- "NotebookEdit",
- &json!({
- "notebook_path": empty_notebook.display().to_string(),
- "edit_mode": "delete"
- }),
- )
- .expect_err("delete on empty notebook should fail");
- assert!(missing_cell.contains("Notebook has no cells to edit"));
- let _ = fs::remove_file(empty_notebook);
- }
- #[test]
- fn bash_tool_reports_success_exit_failure_timeout_and_background() {
- let success = execute_tool("bash", &json!({ "command": "printf 'hello'" }))
- .expect("bash should succeed");
- let success_output: serde_json::Value = serde_json::from_str(&success).expect("json");
- assert_eq!(success_output["stdout"], "hello");
- assert_eq!(success_output["interrupted"], false);
- let failure = execute_tool("bash", &json!({ "command": "printf 'oops' >&2; exit 7" }))
- .expect("bash failure should still return structured output");
- let failure_output: serde_json::Value = serde_json::from_str(&failure).expect("json");
- assert_eq!(failure_output["returnCodeInterpretation"], "exit_code:7");
- assert!(failure_output["stderr"]
- .as_str()
- .expect("stderr")
- .contains("oops"));
- let timeout = execute_tool("bash", &json!({ "command": "sleep 1", "timeout": 10 }))
- .expect("bash timeout should return output");
- let timeout_output: serde_json::Value = serde_json::from_str(&timeout).expect("json");
- assert_eq!(timeout_output["interrupted"], true);
- assert_eq!(timeout_output["returnCodeInterpretation"], "timeout");
- assert!(timeout_output["stderr"]
- .as_str()
- .expect("stderr")
- .contains("Command exceeded timeout"));
- let background = execute_tool(
- "bash",
- &json!({ "command": "sleep 1", "run_in_background": true }),
- )
- .expect("bash background should succeed");
- let background_output: serde_json::Value = serde_json::from_str(&background).expect("json");
- assert!(background_output["backgroundTaskId"].as_str().is_some());
- assert_eq!(background_output["noOutputExpected"], true);
- }
- #[test]
- fn file_tools_cover_read_write_and_edit_behaviors() {
- let _guard = env_lock()
- .lock()
- .unwrap_or_else(std::sync::PoisonError::into_inner);
- let root = temp_path("fs-suite");
- fs::create_dir_all(&root).expect("create root");
- let original_dir = std::env::current_dir().expect("cwd");
- std::env::set_current_dir(&root).expect("set cwd");
- let write_create = execute_tool(
- "write_file",
- &json!({ "path": "nested/demo.txt", "content": "alpha\nbeta\nalpha\n" }),
- )
- .expect("write create should succeed");
- let write_create_output: serde_json::Value =
- serde_json::from_str(&write_create).expect("json");
- assert_eq!(write_create_output["type"], "create");
- assert!(root.join("nested/demo.txt").exists());
- let write_update = execute_tool(
- "write_file",
- &json!({ "path": "nested/demo.txt", "content": "alpha\nbeta\ngamma\n" }),
- )
- .expect("write update should succeed");
- let write_update_output: serde_json::Value =
- serde_json::from_str(&write_update).expect("json");
- assert_eq!(write_update_output["type"], "update");
- assert_eq!(write_update_output["originalFile"], "alpha\nbeta\nalpha\n");
- let read_full = execute_tool("read_file", &json!({ "path": "nested/demo.txt" }))
- .expect("read full should succeed");
- let read_full_output: serde_json::Value = serde_json::from_str(&read_full).expect("json");
- assert_eq!(read_full_output["file"]["content"], "alpha\nbeta\ngamma");
- assert_eq!(read_full_output["file"]["startLine"], 1);
- let read_slice = execute_tool(
- "read_file",
- &json!({ "path": "nested/demo.txt", "offset": 1, "limit": 1 }),
- )
- .expect("read slice should succeed");
- let read_slice_output: serde_json::Value = serde_json::from_str(&read_slice).expect("json");
- assert_eq!(read_slice_output["file"]["content"], "beta");
- assert_eq!(read_slice_output["file"]["startLine"], 2);
- let read_past_end = execute_tool(
- "read_file",
- &json!({ "path": "nested/demo.txt", "offset": 50 }),
- )
- .expect("read past EOF should succeed");
- let read_past_end_output: serde_json::Value =
- serde_json::from_str(&read_past_end).expect("json");
- assert_eq!(read_past_end_output["file"]["content"], "");
- assert_eq!(read_past_end_output["file"]["startLine"], 4);
- let read_error = execute_tool("read_file", &json!({ "path": "missing.txt" }))
- .expect_err("missing file should fail");
- assert!(!read_error.is_empty());
- let edit_once = execute_tool(
- "edit_file",
- &json!({ "path": "nested/demo.txt", "old_string": "alpha", "new_string": "omega" }),
- )
- .expect("single edit should succeed");
- let edit_once_output: serde_json::Value = serde_json::from_str(&edit_once).expect("json");
- assert_eq!(edit_once_output["replaceAll"], false);
- assert_eq!(
- fs::read_to_string(root.join("nested/demo.txt")).expect("read file"),
- "omega\nbeta\ngamma\n"
- );
- execute_tool(
- "write_file",
- &json!({ "path": "nested/demo.txt", "content": "alpha\nbeta\nalpha\n" }),
- )
- .expect("reset file");
- let edit_all = execute_tool(
- "edit_file",
- &json!({
- "path": "nested/demo.txt",
- "old_string": "alpha",
- "new_string": "omega",
- "replace_all": true
- }),
- )
- .expect("replace all should succeed");
- let edit_all_output: serde_json::Value = serde_json::from_str(&edit_all).expect("json");
- assert_eq!(edit_all_output["replaceAll"], true);
- assert_eq!(
- fs::read_to_string(root.join("nested/demo.txt")).expect("read file"),
- "omega\nbeta\nomega\n"
- );
- let edit_same = execute_tool(
- "edit_file",
- &json!({ "path": "nested/demo.txt", "old_string": "omega", "new_string": "omega" }),
- )
- .expect_err("identical old/new should fail");
- assert!(edit_same.contains("must differ"));
- let edit_missing = execute_tool(
- "edit_file",
- &json!({ "path": "nested/demo.txt", "old_string": "missing", "new_string": "omega" }),
- )
- .expect_err("missing substring should fail");
- assert!(edit_missing.contains("old_string not found"));
- std::env::set_current_dir(&original_dir).expect("restore cwd");
- let _ = fs::remove_dir_all(root);
- }
- #[test]
- fn glob_and_grep_tools_cover_success_and_errors() {
- let _guard = env_lock()
- .lock()
- .unwrap_or_else(std::sync::PoisonError::into_inner);
- let root = temp_path("search-suite");
- fs::create_dir_all(root.join("nested")).expect("create root");
- let original_dir = std::env::current_dir().expect("cwd");
- std::env::set_current_dir(&root).expect("set cwd");
- fs::write(
- root.join("nested/lib.rs"),
- "fn main() {}\nlet alpha = 1;\nlet alpha = 2;\n",
- )
- .expect("write rust file");
- fs::write(root.join("nested/notes.txt"), "alpha\nbeta\n").expect("write txt file");
- let globbed = execute_tool("glob_search", &json!({ "pattern": "nested/*.rs" }))
- .expect("glob should succeed");
- let globbed_output: serde_json::Value = serde_json::from_str(&globbed).expect("json");
- assert_eq!(globbed_output["numFiles"], 1);
- assert!(globbed_output["filenames"][0]
- .as_str()
- .expect("filename")
- .ends_with("nested/lib.rs"));
- let glob_error = execute_tool("glob_search", &json!({ "pattern": "[" }))
- .expect_err("invalid glob should fail");
- assert!(!glob_error.is_empty());
- let grep_content = execute_tool(
- "grep_search",
- &json!({
- "pattern": "alpha",
- "path": "nested",
- "glob": "*.rs",
- "output_mode": "content",
- "-n": true,
- "head_limit": 1,
- "offset": 1
- }),
- )
- .expect("grep content should succeed");
- let grep_content_output: serde_json::Value =
- serde_json::from_str(&grep_content).expect("json");
- assert_eq!(grep_content_output["numFiles"], 0);
- assert!(grep_content_output["appliedLimit"].is_null());
- assert_eq!(grep_content_output["appliedOffset"], 1);
- assert!(grep_content_output["content"]
- .as_str()
- .expect("content")
- .contains("let alpha = 2;"));
- let grep_count = execute_tool(
- "grep_search",
- &json!({ "pattern": "alpha", "path": "nested", "output_mode": "count" }),
- )
- .expect("grep count should succeed");
- let grep_count_output: serde_json::Value = serde_json::from_str(&grep_count).expect("json");
- assert_eq!(grep_count_output["numMatches"], 3);
- let grep_error = execute_tool(
- "grep_search",
- &json!({ "pattern": "(alpha", "path": "nested" }),
- )
- .expect_err("invalid regex should fail");
- assert!(!grep_error.is_empty());
- std::env::set_current_dir(&original_dir).expect("restore cwd");
- let _ = fs::remove_dir_all(root);
- }
- #[test]
- fn sleep_waits_and_reports_duration() {
- let started = std::time::Instant::now();
- let result =
- execute_tool("Sleep", &json!({"duration_ms": 20})).expect("Sleep should succeed");
- let elapsed = started.elapsed();
- let output: serde_json::Value = serde_json::from_str(&result).expect("json");
- assert_eq!(output["duration_ms"], 20);
- assert!(output["message"]
- .as_str()
- .expect("message")
- .contains("Slept for 20ms"));
- assert!(elapsed >= Duration::from_millis(15));
- }
- #[test]
- fn brief_returns_sent_message_and_attachment_metadata() {
- let attachment = std::env::temp_dir().join(format!(
- "clawd-brief-{}.png",
- std::time::SystemTime::now()
- .duration_since(std::time::UNIX_EPOCH)
- .expect("time")
- .as_nanos()
- ));
- std::fs::write(&attachment, b"png-data").expect("write attachment");
- let result = execute_tool(
- "SendUserMessage",
- &json!({
- "message": "hello user",
- "attachments": [attachment.display().to_string()],
- "status": "normal"
- }),
- )
- .expect("SendUserMessage should succeed");
- let output: serde_json::Value = serde_json::from_str(&result).expect("json");
- assert_eq!(output["message"], "hello user");
- assert!(output["sentAt"].as_str().is_some());
- assert_eq!(output["attachments"][0]["isImage"], true);
- let _ = std::fs::remove_file(attachment);
- }
- #[test]
- fn config_reads_and_writes_supported_values() {
- let _guard = env_lock()
- .lock()
- .unwrap_or_else(std::sync::PoisonError::into_inner);
- let root = std::env::temp_dir().join(format!(
- "clawd-config-{}",
- std::time::SystemTime::now()
- .duration_since(std::time::UNIX_EPOCH)
- .expect("time")
- .as_nanos()
- ));
- let home = root.join("home");
- let cwd = root.join("cwd");
- std::fs::create_dir_all(home.join(".claw")).expect("home dir");
- std::fs::create_dir_all(cwd.join(".claw")).expect("cwd dir");
- std::fs::write(
- home.join(".claw").join("settings.json"),
- r#"{"verbose":false}"#,
- )
- .expect("write global settings");
- let original_home = std::env::var("HOME").ok();
- let original_config_home = std::env::var("CLAW_CONFIG_HOME").ok();
- let original_dir = std::env::current_dir().expect("cwd");
- std::env::set_var("HOME", &home);
- std::env::remove_var("CLAW_CONFIG_HOME");
- std::env::set_current_dir(&cwd).expect("set cwd");
- let get = execute_tool("Config", &json!({"setting": "verbose"})).expect("get config");
- let get_output: serde_json::Value = serde_json::from_str(&get).expect("json");
- assert_eq!(get_output["value"], false);
- let set = execute_tool(
- "Config",
- &json!({"setting": "permissions.defaultMode", "value": "plan"}),
- )
- .expect("set config");
- let set_output: serde_json::Value = serde_json::from_str(&set).expect("json");
- assert_eq!(set_output["operation"], "set");
- assert_eq!(set_output["newValue"], "plan");
- let invalid = execute_tool(
- "Config",
- &json!({"setting": "permissions.defaultMode", "value": "bogus"}),
- )
- .expect_err("invalid config value should error");
- assert!(invalid.contains("Invalid value"));
- let unknown =
- execute_tool("Config", &json!({"setting": "nope"})).expect("unknown setting result");
- let unknown_output: serde_json::Value = serde_json::from_str(&unknown).expect("json");
- assert_eq!(unknown_output["success"], false);
- std::env::set_current_dir(&original_dir).expect("restore cwd");
- match original_home {
- Some(value) => std::env::set_var("HOME", value),
- None => std::env::remove_var("HOME"),
- }
- match original_config_home {
- Some(value) => std::env::set_var("CLAW_CONFIG_HOME", value),
- None => std::env::remove_var("CLAW_CONFIG_HOME"),
- }
- let _ = std::fs::remove_dir_all(root);
- }
- #[test]
- fn structured_output_echoes_input_payload() {
- let result = execute_tool("StructuredOutput", &json!({"ok": true, "items": [1, 2, 3]}))
- .expect("StructuredOutput should succeed");
- let output: serde_json::Value = serde_json::from_str(&result).expect("json");
- assert_eq!(output["data"], "Structured output provided successfully");
- assert_eq!(output["structured_output"]["ok"], true);
- assert_eq!(output["structured_output"]["items"][1], 2);
- }
- #[test]
- fn repl_executes_python_code() {
- let result = execute_tool(
- "REPL",
- &json!({"language": "python", "code": "print(1 + 1)", "timeout_ms": 500}),
- )
- .expect("REPL should succeed");
- let output: serde_json::Value = serde_json::from_str(&result).expect("json");
- assert_eq!(output["language"], "python");
- assert_eq!(output["exitCode"], 0);
- assert!(output["stdout"].as_str().expect("stdout").contains('2'));
- }
- #[test]
- fn powershell_runs_via_stub_shell() {
- let _guard = env_lock()
- .lock()
- .unwrap_or_else(std::sync::PoisonError::into_inner);
- let dir = std::env::temp_dir().join(format!(
- "clawd-pwsh-bin-{}",
- std::time::SystemTime::now()
- .duration_since(std::time::UNIX_EPOCH)
- .expect("time")
- .as_nanos()
- ));
- std::fs::create_dir_all(&dir).expect("create dir");
- let script = dir.join("pwsh");
- std::fs::write(
- &script,
- r#"#!/bin/sh
- while [ "$1" != "-Command" ] && [ $# -gt 0 ]; do shift; done
- shift
- printf 'pwsh:%s' "$1"
- "#,
- )
- .expect("write script");
- std::process::Command::new("/bin/chmod")
- .arg("+x")
- .arg(&script)
- .status()
- .expect("chmod");
- let original_path = std::env::var("PATH").unwrap_or_default();
- std::env::set_var("PATH", format!("{}:{}", dir.display(), original_path));
- let result = execute_tool(
- "PowerShell",
- &json!({"command": "Write-Output hello", "timeout": 1000}),
- )
- .expect("PowerShell should succeed");
- let background = execute_tool(
- "PowerShell",
- &json!({"command": "Write-Output hello", "run_in_background": true}),
- )
- .expect("PowerShell background should succeed");
- std::env::set_var("PATH", original_path);
- let _ = std::fs::remove_dir_all(dir);
- let output: serde_json::Value = serde_json::from_str(&result).expect("json");
- assert_eq!(output["stdout"], "pwsh:Write-Output hello");
- assert!(output["stderr"].as_str().expect("stderr").is_empty());
- let background_output: serde_json::Value = serde_json::from_str(&background).expect("json");
- assert!(background_output["backgroundTaskId"].as_str().is_some());
- assert_eq!(background_output["backgroundedByUser"], true);
- assert_eq!(background_output["assistantAutoBackgrounded"], false);
- }
- #[test]
- fn powershell_errors_when_shell_is_missing() {
- let _guard = env_lock()
- .lock()
- .unwrap_or_else(std::sync::PoisonError::into_inner);
- let original_path = std::env::var("PATH").unwrap_or_default();
- let empty_dir = std::env::temp_dir().join(format!(
- "clawd-empty-bin-{}",
- std::time::SystemTime::now()
- .duration_since(std::time::UNIX_EPOCH)
- .expect("time")
- .as_nanos()
- ));
- std::fs::create_dir_all(&empty_dir).expect("create empty dir");
- std::env::set_var("PATH", empty_dir.display().to_string());
- let err = execute_tool("PowerShell", &json!({"command": "Write-Output hello"}))
- .expect_err("PowerShell should fail when shell is missing");
- std::env::set_var("PATH", original_path);
- let _ = std::fs::remove_dir_all(empty_dir);
- assert!(err.contains("PowerShell executable not found"));
- }
- struct TestServer {
- addr: SocketAddr,
- shutdown: Option<std::sync::mpsc::Sender<()>>,
- handle: Option<thread::JoinHandle<()>>,
- }
- impl TestServer {
- fn spawn(handler: Arc<dyn Fn(&str) -> HttpResponse + Send + Sync + 'static>) -> Self {
- let listener = TcpListener::bind("127.0.0.1:0").expect("bind test server");
- listener
- .set_nonblocking(true)
- .expect("set nonblocking listener");
- let addr = listener.local_addr().expect("local addr");
- let (tx, rx) = std::sync::mpsc::channel::<()>();
- let handle = thread::spawn(move || loop {
- if rx.try_recv().is_ok() {
- break;
- }
- match listener.accept() {
- Ok((mut stream, _)) => {
- let mut buffer = [0_u8; 4096];
- let size = stream.read(&mut buffer).expect("read request");
- let request = String::from_utf8_lossy(&buffer[..size]).into_owned();
- let request_line = request.lines().next().unwrap_or_default().to_string();
- let response = handler(&request_line);
- stream
- .write_all(response.to_bytes().as_slice())
- .expect("write response");
- }
- Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => {
- thread::sleep(Duration::from_millis(10));
- }
- Err(error) => panic!("server accept failed: {error}"),
- }
- });
- Self {
- addr,
- shutdown: Some(tx),
- handle: Some(handle),
- }
- }
- fn addr(&self) -> SocketAddr {
- self.addr
- }
- }
- impl Drop for TestServer {
- fn drop(&mut self) {
- if let Some(tx) = self.shutdown.take() {
- let _ = tx.send(());
- }
- if let Some(handle) = self.handle.take() {
- handle.join().expect("join test server");
- }
- }
- }
- struct HttpResponse {
- status: u16,
- reason: &'static str,
- content_type: &'static str,
- body: String,
- }
- impl HttpResponse {
- fn html(status: u16, reason: &'static str, body: &str) -> Self {
- Self {
- status,
- reason,
- content_type: "text/html; charset=utf-8",
- body: body.to_string(),
- }
- }
- fn text(status: u16, reason: &'static str, body: &str) -> Self {
- Self {
- status,
- reason,
- content_type: "text/plain; charset=utf-8",
- body: body.to_string(),
- }
- }
- fn to_bytes(&self) -> Vec<u8> {
- format!(
- "HTTP/1.1 {} {}\r\nContent-Type: {}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
- self.status,
- self.reason,
- self.content_type,
- self.body.len(),
- self.body
- )
- .into_bytes()
- }
- }
- }
|