lib.rs 119 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509
  1. use std::collections::{BTreeMap, BTreeSet};
  2. use std::path::{Path, PathBuf};
  3. use std::process::Command;
  4. use std::time::{Duration, Instant};
  5. use reqwest::blocking::Client;
  6. use runtime::{
  7. edit_file, execute_bash, glob_search, grep_search, read_file, write_file, BashCommandInput,
  8. GrepSearchInput, PermissionMode,
  9. };
  10. use serde::{Deserialize, Serialize};
  11. use serde_json::{json, Value};
  12. #[derive(Debug, Clone, PartialEq, Eq)]
  13. pub struct ToolManifestEntry {
  14. pub name: String,
  15. pub source: ToolSource,
  16. }
  17. #[derive(Debug, Clone, Copy, PartialEq, Eq)]
  18. pub enum ToolSource {
  19. Base,
  20. Conditional,
  21. }
  22. #[derive(Debug, Clone, Default, PartialEq, Eq)]
  23. pub struct ToolRegistry {
  24. entries: Vec<ToolManifestEntry>,
  25. }
  26. impl ToolRegistry {
  27. #[must_use]
  28. pub fn new(entries: Vec<ToolManifestEntry>) -> Self {
  29. Self { entries }
  30. }
  31. #[must_use]
  32. pub fn entries(&self) -> &[ToolManifestEntry] {
  33. &self.entries
  34. }
  35. }
  36. #[derive(Debug, Clone, PartialEq, Eq)]
  37. pub struct ToolSpec {
  38. pub name: &'static str,
  39. pub description: &'static str,
  40. pub input_schema: Value,
  41. pub required_permission: PermissionMode,
  42. }
  43. #[must_use]
  44. #[allow(clippy::too_many_lines)]
  45. pub fn mvp_tool_specs() -> Vec<ToolSpec> {
  46. vec![
  47. ToolSpec {
  48. name: "bash",
  49. description: "Execute a shell command in the current workspace.",
  50. input_schema: json!({
  51. "type": "object",
  52. "properties": {
  53. "command": { "type": "string" },
  54. "timeout": { "type": "integer", "minimum": 1 },
  55. "description": { "type": "string" },
  56. "run_in_background": { "type": "boolean" },
  57. "dangerouslyDisableSandbox": { "type": "boolean" }
  58. },
  59. "required": ["command"],
  60. "additionalProperties": false
  61. }),
  62. required_permission: PermissionMode::DangerFullAccess,
  63. },
  64. ToolSpec {
  65. name: "read_file",
  66. description: "Read a text file from the workspace.",
  67. input_schema: json!({
  68. "type": "object",
  69. "properties": {
  70. "path": { "type": "string" },
  71. "offset": { "type": "integer", "minimum": 0 },
  72. "limit": { "type": "integer", "minimum": 1 }
  73. },
  74. "required": ["path"],
  75. "additionalProperties": false
  76. }),
  77. required_permission: PermissionMode::ReadOnly,
  78. },
  79. ToolSpec {
  80. name: "write_file",
  81. description: "Write a text file in the workspace.",
  82. input_schema: json!({
  83. "type": "object",
  84. "properties": {
  85. "path": { "type": "string" },
  86. "content": { "type": "string" }
  87. },
  88. "required": ["path", "content"],
  89. "additionalProperties": false
  90. }),
  91. required_permission: PermissionMode::WorkspaceWrite,
  92. },
  93. ToolSpec {
  94. name: "edit_file",
  95. description: "Replace text in a workspace file.",
  96. input_schema: json!({
  97. "type": "object",
  98. "properties": {
  99. "path": { "type": "string" },
  100. "old_string": { "type": "string" },
  101. "new_string": { "type": "string" },
  102. "replace_all": { "type": "boolean" }
  103. },
  104. "required": ["path", "old_string", "new_string"],
  105. "additionalProperties": false
  106. }),
  107. required_permission: PermissionMode::WorkspaceWrite,
  108. },
  109. ToolSpec {
  110. name: "glob_search",
  111. description: "Find files by glob pattern.",
  112. input_schema: json!({
  113. "type": "object",
  114. "properties": {
  115. "pattern": { "type": "string" },
  116. "path": { "type": "string" }
  117. },
  118. "required": ["pattern"],
  119. "additionalProperties": false
  120. }),
  121. required_permission: PermissionMode::ReadOnly,
  122. },
  123. ToolSpec {
  124. name: "grep_search",
  125. description: "Search file contents with a regex pattern.",
  126. input_schema: json!({
  127. "type": "object",
  128. "properties": {
  129. "pattern": { "type": "string" },
  130. "path": { "type": "string" },
  131. "glob": { "type": "string" },
  132. "output_mode": { "type": "string" },
  133. "-B": { "type": "integer", "minimum": 0 },
  134. "-A": { "type": "integer", "minimum": 0 },
  135. "-C": { "type": "integer", "minimum": 0 },
  136. "context": { "type": "integer", "minimum": 0 },
  137. "-n": { "type": "boolean" },
  138. "-i": { "type": "boolean" },
  139. "type": { "type": "string" },
  140. "head_limit": { "type": "integer", "minimum": 1 },
  141. "offset": { "type": "integer", "minimum": 0 },
  142. "multiline": { "type": "boolean" }
  143. },
  144. "required": ["pattern"],
  145. "additionalProperties": false
  146. }),
  147. required_permission: PermissionMode::ReadOnly,
  148. },
  149. ToolSpec {
  150. name: "WebFetch",
  151. description:
  152. "Fetch a URL, convert it into readable text, and answer a prompt about it.",
  153. input_schema: json!({
  154. "type": "object",
  155. "properties": {
  156. "url": { "type": "string", "format": "uri" },
  157. "prompt": { "type": "string" }
  158. },
  159. "required": ["url", "prompt"],
  160. "additionalProperties": false
  161. }),
  162. required_permission: PermissionMode::ReadOnly,
  163. },
  164. ToolSpec {
  165. name: "WebSearch",
  166. description: "Search the web for current information and return cited results.",
  167. input_schema: json!({
  168. "type": "object",
  169. "properties": {
  170. "query": { "type": "string", "minLength": 2 },
  171. "allowed_domains": {
  172. "type": "array",
  173. "items": { "type": "string" }
  174. },
  175. "blocked_domains": {
  176. "type": "array",
  177. "items": { "type": "string" }
  178. }
  179. },
  180. "required": ["query"],
  181. "additionalProperties": false
  182. }),
  183. required_permission: PermissionMode::ReadOnly,
  184. },
  185. ToolSpec {
  186. name: "TodoWrite",
  187. description: "Update the structured task list for the current session.",
  188. input_schema: json!({
  189. "type": "object",
  190. "properties": {
  191. "todos": {
  192. "type": "array",
  193. "items": {
  194. "type": "object",
  195. "properties": {
  196. "content": { "type": "string" },
  197. "activeForm": { "type": "string" },
  198. "status": {
  199. "type": "string",
  200. "enum": ["pending", "in_progress", "completed"]
  201. }
  202. },
  203. "required": ["content", "activeForm", "status"],
  204. "additionalProperties": false
  205. }
  206. }
  207. },
  208. "required": ["todos"],
  209. "additionalProperties": false
  210. }),
  211. required_permission: PermissionMode::WorkspaceWrite,
  212. },
  213. ToolSpec {
  214. name: "Skill",
  215. description: "Load a local skill definition and its instructions.",
  216. input_schema: json!({
  217. "type": "object",
  218. "properties": {
  219. "skill": { "type": "string" },
  220. "args": { "type": "string" }
  221. },
  222. "required": ["skill"],
  223. "additionalProperties": false
  224. }),
  225. required_permission: PermissionMode::ReadOnly,
  226. },
  227. ToolSpec {
  228. name: "Agent",
  229. description: "Launch a specialized agent task and persist its handoff metadata.",
  230. input_schema: json!({
  231. "type": "object",
  232. "properties": {
  233. "description": { "type": "string" },
  234. "prompt": { "type": "string" },
  235. "subagent_type": { "type": "string" },
  236. "name": { "type": "string" },
  237. "model": { "type": "string" }
  238. },
  239. "required": ["description", "prompt"],
  240. "additionalProperties": false
  241. }),
  242. required_permission: PermissionMode::DangerFullAccess,
  243. },
  244. ToolSpec {
  245. name: "ToolSearch",
  246. description: "Search for deferred or specialized tools by exact name or keywords.",
  247. input_schema: json!({
  248. "type": "object",
  249. "properties": {
  250. "query": { "type": "string" },
  251. "max_results": { "type": "integer", "minimum": 1 }
  252. },
  253. "required": ["query"],
  254. "additionalProperties": false
  255. }),
  256. required_permission: PermissionMode::ReadOnly,
  257. },
  258. ToolSpec {
  259. name: "NotebookEdit",
  260. description: "Replace, insert, or delete a cell in a Jupyter notebook.",
  261. input_schema: json!({
  262. "type": "object",
  263. "properties": {
  264. "notebook_path": { "type": "string" },
  265. "cell_id": { "type": "string" },
  266. "new_source": { "type": "string" },
  267. "cell_type": { "type": "string", "enum": ["code", "markdown"] },
  268. "edit_mode": { "type": "string", "enum": ["replace", "insert", "delete"] }
  269. },
  270. "required": ["notebook_path"],
  271. "additionalProperties": false
  272. }),
  273. required_permission: PermissionMode::WorkspaceWrite,
  274. },
  275. ToolSpec {
  276. name: "Sleep",
  277. description: "Wait for a specified duration without holding a shell process.",
  278. input_schema: json!({
  279. "type": "object",
  280. "properties": {
  281. "duration_ms": { "type": "integer", "minimum": 0 }
  282. },
  283. "required": ["duration_ms"],
  284. "additionalProperties": false
  285. }),
  286. required_permission: PermissionMode::ReadOnly,
  287. },
  288. ToolSpec {
  289. name: "SendUserMessage",
  290. description: "Send a message to the user.",
  291. input_schema: json!({
  292. "type": "object",
  293. "properties": {
  294. "message": { "type": "string" },
  295. "attachments": {
  296. "type": "array",
  297. "items": { "type": "string" }
  298. },
  299. "status": {
  300. "type": "string",
  301. "enum": ["normal", "proactive"]
  302. }
  303. },
  304. "required": ["message", "status"],
  305. "additionalProperties": false
  306. }),
  307. required_permission: PermissionMode::ReadOnly,
  308. },
  309. ToolSpec {
  310. name: "Config",
  311. description: "Get or set Claude Code settings.",
  312. input_schema: json!({
  313. "type": "object",
  314. "properties": {
  315. "setting": { "type": "string" },
  316. "value": {
  317. "type": ["string", "boolean", "number"]
  318. }
  319. },
  320. "required": ["setting"],
  321. "additionalProperties": false
  322. }),
  323. required_permission: PermissionMode::WorkspaceWrite,
  324. },
  325. ToolSpec {
  326. name: "StructuredOutput",
  327. description: "Return structured output in the requested format.",
  328. input_schema: json!({
  329. "type": "object",
  330. "additionalProperties": true
  331. }),
  332. required_permission: PermissionMode::ReadOnly,
  333. },
  334. ToolSpec {
  335. name: "REPL",
  336. description: "Execute code in a REPL-like subprocess.",
  337. input_schema: json!({
  338. "type": "object",
  339. "properties": {
  340. "code": { "type": "string" },
  341. "language": { "type": "string" },
  342. "timeout_ms": { "type": "integer", "minimum": 1 }
  343. },
  344. "required": ["code", "language"],
  345. "additionalProperties": false
  346. }),
  347. required_permission: PermissionMode::DangerFullAccess,
  348. },
  349. ToolSpec {
  350. name: "PowerShell",
  351. description: "Execute a PowerShell command with optional timeout.",
  352. input_schema: json!({
  353. "type": "object",
  354. "properties": {
  355. "command": { "type": "string" },
  356. "timeout": { "type": "integer", "minimum": 1 },
  357. "description": { "type": "string" },
  358. "run_in_background": { "type": "boolean" }
  359. },
  360. "required": ["command"],
  361. "additionalProperties": false
  362. }),
  363. required_permission: PermissionMode::DangerFullAccess,
  364. },
  365. ]
  366. }
  367. pub fn execute_tool(name: &str, input: &Value) -> Result<String, String> {
  368. match name {
  369. "bash" => from_value::<BashCommandInput>(input).and_then(run_bash),
  370. "read_file" => from_value::<ReadFileInput>(input).and_then(run_read_file),
  371. "write_file" => from_value::<WriteFileInput>(input).and_then(run_write_file),
  372. "edit_file" => from_value::<EditFileInput>(input).and_then(run_edit_file),
  373. "glob_search" => from_value::<GlobSearchInputValue>(input).and_then(run_glob_search),
  374. "grep_search" => from_value::<GrepSearchInput>(input).and_then(run_grep_search),
  375. "WebFetch" => from_value::<WebFetchInput>(input).and_then(run_web_fetch),
  376. "WebSearch" => from_value::<WebSearchInput>(input).and_then(run_web_search),
  377. "TodoWrite" => from_value::<TodoWriteInput>(input).and_then(run_todo_write),
  378. "Skill" => from_value::<SkillInput>(input).and_then(run_skill),
  379. "Agent" => from_value::<AgentInput>(input).and_then(run_agent),
  380. "ToolSearch" => from_value::<ToolSearchInput>(input).and_then(run_tool_search),
  381. "NotebookEdit" => from_value::<NotebookEditInput>(input).and_then(run_notebook_edit),
  382. "Sleep" => from_value::<SleepInput>(input).and_then(run_sleep),
  383. "SendUserMessage" | "Brief" => from_value::<BriefInput>(input).and_then(run_brief),
  384. "Config" => from_value::<ConfigInput>(input).and_then(run_config),
  385. "StructuredOutput" => {
  386. from_value::<StructuredOutputInput>(input).and_then(run_structured_output)
  387. }
  388. "REPL" => from_value::<ReplInput>(input).and_then(run_repl),
  389. "PowerShell" => from_value::<PowerShellInput>(input).and_then(run_powershell),
  390. _ => Err(format!("unsupported tool: {name}")),
  391. }
  392. }
  393. fn from_value<T: for<'de> Deserialize<'de>>(input: &Value) -> Result<T, String> {
  394. serde_json::from_value(input.clone()).map_err(|error| error.to_string())
  395. }
  396. fn run_bash(input: BashCommandInput) -> Result<String, String> {
  397. serde_json::to_string_pretty(&execute_bash(input).map_err(|error| error.to_string())?)
  398. .map_err(|error| error.to_string())
  399. }
  400. #[allow(clippy::needless_pass_by_value)]
  401. fn run_read_file(input: ReadFileInput) -> Result<String, String> {
  402. to_pretty_json(read_file(&input.path, input.offset, input.limit).map_err(io_to_string)?)
  403. }
  404. #[allow(clippy::needless_pass_by_value)]
  405. fn run_write_file(input: WriteFileInput) -> Result<String, String> {
  406. to_pretty_json(write_file(&input.path, &input.content).map_err(io_to_string)?)
  407. }
  408. #[allow(clippy::needless_pass_by_value)]
  409. fn run_edit_file(input: EditFileInput) -> Result<String, String> {
  410. to_pretty_json(
  411. edit_file(
  412. &input.path,
  413. &input.old_string,
  414. &input.new_string,
  415. input.replace_all.unwrap_or(false),
  416. )
  417. .map_err(io_to_string)?,
  418. )
  419. }
  420. #[allow(clippy::needless_pass_by_value)]
  421. fn run_glob_search(input: GlobSearchInputValue) -> Result<String, String> {
  422. to_pretty_json(glob_search(&input.pattern, input.path.as_deref()).map_err(io_to_string)?)
  423. }
  424. #[allow(clippy::needless_pass_by_value)]
  425. fn run_grep_search(input: GrepSearchInput) -> Result<String, String> {
  426. to_pretty_json(grep_search(&input).map_err(io_to_string)?)
  427. }
  428. #[allow(clippy::needless_pass_by_value)]
  429. fn run_web_fetch(input: WebFetchInput) -> Result<String, String> {
  430. to_pretty_json(execute_web_fetch(&input)?)
  431. }
  432. #[allow(clippy::needless_pass_by_value)]
  433. fn run_web_search(input: WebSearchInput) -> Result<String, String> {
  434. to_pretty_json(execute_web_search(&input)?)
  435. }
  436. fn run_todo_write(input: TodoWriteInput) -> Result<String, String> {
  437. to_pretty_json(execute_todo_write(input)?)
  438. }
  439. fn run_skill(input: SkillInput) -> Result<String, String> {
  440. to_pretty_json(execute_skill(input)?)
  441. }
  442. fn run_agent(input: AgentInput) -> Result<String, String> {
  443. to_pretty_json(execute_agent(input)?)
  444. }
  445. fn run_tool_search(input: ToolSearchInput) -> Result<String, String> {
  446. to_pretty_json(execute_tool_search(input))
  447. }
  448. fn run_notebook_edit(input: NotebookEditInput) -> Result<String, String> {
  449. to_pretty_json(execute_notebook_edit(input)?)
  450. }
  451. fn run_sleep(input: SleepInput) -> Result<String, String> {
  452. to_pretty_json(execute_sleep(input))
  453. }
  454. fn run_brief(input: BriefInput) -> Result<String, String> {
  455. to_pretty_json(execute_brief(input)?)
  456. }
  457. fn run_config(input: ConfigInput) -> Result<String, String> {
  458. to_pretty_json(execute_config(input)?)
  459. }
  460. fn run_structured_output(input: StructuredOutputInput) -> Result<String, String> {
  461. to_pretty_json(execute_structured_output(input))
  462. }
  463. fn run_repl(input: ReplInput) -> Result<String, String> {
  464. to_pretty_json(execute_repl(input)?)
  465. }
  466. fn run_powershell(input: PowerShellInput) -> Result<String, String> {
  467. to_pretty_json(execute_powershell(input).map_err(|error| error.to_string())?)
  468. }
  469. fn to_pretty_json<T: serde::Serialize>(value: T) -> Result<String, String> {
  470. serde_json::to_string_pretty(&value).map_err(|error| error.to_string())
  471. }
  472. #[allow(clippy::needless_pass_by_value)]
  473. fn io_to_string(error: std::io::Error) -> String {
  474. error.to_string()
  475. }
  476. #[derive(Debug, Deserialize)]
  477. struct ReadFileInput {
  478. path: String,
  479. offset: Option<usize>,
  480. limit: Option<usize>,
  481. }
  482. #[derive(Debug, Deserialize)]
  483. struct WriteFileInput {
  484. path: String,
  485. content: String,
  486. }
  487. #[derive(Debug, Deserialize)]
  488. struct EditFileInput {
  489. path: String,
  490. old_string: String,
  491. new_string: String,
  492. replace_all: Option<bool>,
  493. }
  494. #[derive(Debug, Deserialize)]
  495. struct GlobSearchInputValue {
  496. pattern: String,
  497. path: Option<String>,
  498. }
  499. #[derive(Debug, Deserialize)]
  500. struct WebFetchInput {
  501. url: String,
  502. prompt: String,
  503. }
  504. #[derive(Debug, Deserialize)]
  505. struct WebSearchInput {
  506. query: String,
  507. allowed_domains: Option<Vec<String>>,
  508. blocked_domains: Option<Vec<String>>,
  509. }
  510. #[derive(Debug, Deserialize)]
  511. struct TodoWriteInput {
  512. todos: Vec<TodoItem>,
  513. }
  514. #[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
  515. struct TodoItem {
  516. content: String,
  517. #[serde(rename = "activeForm")]
  518. active_form: String,
  519. status: TodoStatus,
  520. }
  521. #[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
  522. #[serde(rename_all = "snake_case")]
  523. enum TodoStatus {
  524. Pending,
  525. InProgress,
  526. Completed,
  527. }
  528. #[derive(Debug, Deserialize)]
  529. struct SkillInput {
  530. skill: String,
  531. args: Option<String>,
  532. }
  533. #[derive(Debug, Deserialize)]
  534. struct AgentInput {
  535. description: String,
  536. prompt: String,
  537. subagent_type: Option<String>,
  538. name: Option<String>,
  539. model: Option<String>,
  540. }
  541. #[derive(Debug, Deserialize)]
  542. struct ToolSearchInput {
  543. query: String,
  544. max_results: Option<usize>,
  545. }
  546. #[derive(Debug, Deserialize)]
  547. struct NotebookEditInput {
  548. notebook_path: String,
  549. cell_id: Option<String>,
  550. new_source: Option<String>,
  551. cell_type: Option<NotebookCellType>,
  552. edit_mode: Option<NotebookEditMode>,
  553. }
  554. #[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq)]
  555. #[serde(rename_all = "lowercase")]
  556. enum NotebookCellType {
  557. Code,
  558. Markdown,
  559. }
  560. #[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq)]
  561. #[serde(rename_all = "lowercase")]
  562. enum NotebookEditMode {
  563. Replace,
  564. Insert,
  565. Delete,
  566. }
  567. #[derive(Debug, Deserialize)]
  568. struct SleepInput {
  569. duration_ms: u64,
  570. }
  571. #[derive(Debug, Deserialize)]
  572. struct BriefInput {
  573. message: String,
  574. attachments: Option<Vec<String>>,
  575. status: BriefStatus,
  576. }
  577. #[derive(Debug, Deserialize)]
  578. #[serde(rename_all = "lowercase")]
  579. enum BriefStatus {
  580. Normal,
  581. Proactive,
  582. }
  583. #[derive(Debug, Deserialize)]
  584. struct ConfigInput {
  585. setting: String,
  586. value: Option<ConfigValue>,
  587. }
  588. #[derive(Debug, Deserialize)]
  589. #[serde(untagged)]
  590. enum ConfigValue {
  591. String(String),
  592. Bool(bool),
  593. Number(f64),
  594. }
  595. #[derive(Debug, Deserialize)]
  596. #[serde(transparent)]
  597. struct StructuredOutputInput(BTreeMap<String, Value>);
  598. #[derive(Debug, Deserialize)]
  599. struct ReplInput {
  600. code: String,
  601. language: String,
  602. timeout_ms: Option<u64>,
  603. }
  604. #[derive(Debug, Deserialize)]
  605. struct PowerShellInput {
  606. command: String,
  607. timeout: Option<u64>,
  608. description: Option<String>,
  609. run_in_background: Option<bool>,
  610. }
  611. #[derive(Debug, Serialize)]
  612. struct WebFetchOutput {
  613. bytes: usize,
  614. code: u16,
  615. #[serde(rename = "codeText")]
  616. code_text: String,
  617. result: String,
  618. #[serde(rename = "durationMs")]
  619. duration_ms: u128,
  620. url: String,
  621. }
  622. #[derive(Debug, Serialize)]
  623. struct WebSearchOutput {
  624. query: String,
  625. results: Vec<WebSearchResultItem>,
  626. #[serde(rename = "durationSeconds")]
  627. duration_seconds: f64,
  628. }
  629. #[derive(Debug, Serialize)]
  630. struct TodoWriteOutput {
  631. #[serde(rename = "oldTodos")]
  632. old_todos: Vec<TodoItem>,
  633. #[serde(rename = "newTodos")]
  634. new_todos: Vec<TodoItem>,
  635. #[serde(rename = "verificationNudgeNeeded")]
  636. verification_nudge_needed: Option<bool>,
  637. }
  638. #[derive(Debug, Serialize)]
  639. struct SkillOutput {
  640. skill: String,
  641. path: String,
  642. args: Option<String>,
  643. description: Option<String>,
  644. prompt: String,
  645. }
  646. #[derive(Debug, Serialize, Deserialize)]
  647. struct AgentOutput {
  648. #[serde(rename = "agentId")]
  649. agent_id: String,
  650. name: String,
  651. description: String,
  652. #[serde(rename = "subagentType")]
  653. subagent_type: Option<String>,
  654. model: Option<String>,
  655. status: String,
  656. #[serde(rename = "outputFile")]
  657. output_file: String,
  658. #[serde(rename = "manifestFile")]
  659. manifest_file: String,
  660. #[serde(rename = "createdAt")]
  661. created_at: String,
  662. }
  663. #[derive(Debug, Serialize)]
  664. struct ToolSearchOutput {
  665. matches: Vec<String>,
  666. query: String,
  667. normalized_query: String,
  668. #[serde(rename = "total_deferred_tools")]
  669. total_deferred_tools: usize,
  670. #[serde(rename = "pending_mcp_servers")]
  671. pending_mcp_servers: Option<Vec<String>>,
  672. }
  673. #[derive(Debug, Serialize)]
  674. struct NotebookEditOutput {
  675. new_source: String,
  676. cell_id: Option<String>,
  677. cell_type: Option<NotebookCellType>,
  678. language: String,
  679. edit_mode: String,
  680. error: Option<String>,
  681. notebook_path: String,
  682. original_file: String,
  683. updated_file: String,
  684. }
  685. #[derive(Debug, Serialize)]
  686. struct SleepOutput {
  687. duration_ms: u64,
  688. message: String,
  689. }
  690. #[derive(Debug, Serialize)]
  691. struct BriefOutput {
  692. message: String,
  693. attachments: Option<Vec<ResolvedAttachment>>,
  694. #[serde(rename = "sentAt")]
  695. sent_at: String,
  696. }
  697. #[derive(Debug, Serialize)]
  698. struct ResolvedAttachment {
  699. path: String,
  700. size: u64,
  701. #[serde(rename = "isImage")]
  702. is_image: bool,
  703. }
  704. #[derive(Debug, Serialize)]
  705. struct ConfigOutput {
  706. success: bool,
  707. operation: Option<String>,
  708. setting: Option<String>,
  709. value: Option<Value>,
  710. #[serde(rename = "previousValue")]
  711. previous_value: Option<Value>,
  712. #[serde(rename = "newValue")]
  713. new_value: Option<Value>,
  714. error: Option<String>,
  715. }
  716. #[derive(Debug, Serialize)]
  717. struct StructuredOutputResult {
  718. data: String,
  719. structured_output: BTreeMap<String, Value>,
  720. }
  721. #[derive(Debug, Serialize)]
  722. struct ReplOutput {
  723. language: String,
  724. stdout: String,
  725. stderr: String,
  726. #[serde(rename = "exitCode")]
  727. exit_code: i32,
  728. #[serde(rename = "durationMs")]
  729. duration_ms: u128,
  730. }
  731. #[derive(Debug, Serialize)]
  732. #[serde(untagged)]
  733. enum WebSearchResultItem {
  734. SearchResult {
  735. tool_use_id: String,
  736. content: Vec<SearchHit>,
  737. },
  738. Commentary(String),
  739. }
  740. #[derive(Debug, Serialize)]
  741. struct SearchHit {
  742. title: String,
  743. url: String,
  744. }
  745. fn execute_web_fetch(input: &WebFetchInput) -> Result<WebFetchOutput, String> {
  746. let started = Instant::now();
  747. let client = build_http_client()?;
  748. let request_url = normalize_fetch_url(&input.url)?;
  749. let response = client
  750. .get(request_url.clone())
  751. .send()
  752. .map_err(|error| error.to_string())?;
  753. let status = response.status();
  754. let final_url = response.url().to_string();
  755. let code = status.as_u16();
  756. let code_text = status.canonical_reason().unwrap_or("Unknown").to_string();
  757. let content_type = response
  758. .headers()
  759. .get(reqwest::header::CONTENT_TYPE)
  760. .and_then(|value| value.to_str().ok())
  761. .unwrap_or_default()
  762. .to_string();
  763. let body = response.text().map_err(|error| error.to_string())?;
  764. let bytes = body.len();
  765. let normalized = normalize_fetched_content(&body, &content_type);
  766. let result = summarize_web_fetch(&final_url, &input.prompt, &normalized, &body, &content_type);
  767. Ok(WebFetchOutput {
  768. bytes,
  769. code,
  770. code_text,
  771. result,
  772. duration_ms: started.elapsed().as_millis(),
  773. url: final_url,
  774. })
  775. }
  776. fn execute_web_search(input: &WebSearchInput) -> Result<WebSearchOutput, String> {
  777. let started = Instant::now();
  778. let client = build_http_client()?;
  779. let search_url = build_search_url(&input.query)?;
  780. let response = client
  781. .get(search_url)
  782. .send()
  783. .map_err(|error| error.to_string())?;
  784. let final_url = response.url().clone();
  785. let html = response.text().map_err(|error| error.to_string())?;
  786. let mut hits = extract_search_hits(&html);
  787. if hits.is_empty() && final_url.host_str().is_some() {
  788. hits = extract_search_hits_from_generic_links(&html);
  789. }
  790. if let Some(allowed) = input.allowed_domains.as_ref() {
  791. hits.retain(|hit| host_matches_list(&hit.url, allowed));
  792. }
  793. if let Some(blocked) = input.blocked_domains.as_ref() {
  794. hits.retain(|hit| !host_matches_list(&hit.url, blocked));
  795. }
  796. dedupe_hits(&mut hits);
  797. hits.truncate(8);
  798. let summary = if hits.is_empty() {
  799. format!("No web search results matched the query {:?}.", input.query)
  800. } else {
  801. let rendered_hits = hits
  802. .iter()
  803. .map(|hit| format!("- [{}]({})", hit.title, hit.url))
  804. .collect::<Vec<_>>()
  805. .join("\n");
  806. format!(
  807. "Search results for {:?}. Include a Sources section in the final answer.\n{}",
  808. input.query, rendered_hits
  809. )
  810. };
  811. Ok(WebSearchOutput {
  812. query: input.query.clone(),
  813. results: vec![
  814. WebSearchResultItem::Commentary(summary),
  815. WebSearchResultItem::SearchResult {
  816. tool_use_id: String::from("web_search_1"),
  817. content: hits,
  818. },
  819. ],
  820. duration_seconds: started.elapsed().as_secs_f64(),
  821. })
  822. }
  823. fn build_http_client() -> Result<Client, String> {
  824. Client::builder()
  825. .timeout(Duration::from_secs(20))
  826. .redirect(reqwest::redirect::Policy::limited(10))
  827. .user_agent("clawd-rust-tools/0.1")
  828. .build()
  829. .map_err(|error| error.to_string())
  830. }
  831. fn normalize_fetch_url(url: &str) -> Result<String, String> {
  832. let parsed = reqwest::Url::parse(url).map_err(|error| error.to_string())?;
  833. if parsed.scheme() == "http" {
  834. let host = parsed.host_str().unwrap_or_default();
  835. if host != "localhost" && host != "127.0.0.1" && host != "::1" {
  836. let mut upgraded = parsed;
  837. upgraded
  838. .set_scheme("https")
  839. .map_err(|()| String::from("failed to upgrade URL to https"))?;
  840. return Ok(upgraded.to_string());
  841. }
  842. }
  843. Ok(parsed.to_string())
  844. }
  845. fn build_search_url(query: &str) -> Result<reqwest::Url, String> {
  846. if let Ok(base) = std::env::var("CLAWD_WEB_SEARCH_BASE_URL") {
  847. let mut url = reqwest::Url::parse(&base).map_err(|error| error.to_string())?;
  848. url.query_pairs_mut().append_pair("q", query);
  849. return Ok(url);
  850. }
  851. let mut url = reqwest::Url::parse("https://html.duckduckgo.com/html/")
  852. .map_err(|error| error.to_string())?;
  853. url.query_pairs_mut().append_pair("q", query);
  854. Ok(url)
  855. }
  856. fn normalize_fetched_content(body: &str, content_type: &str) -> String {
  857. if content_type.contains("html") {
  858. html_to_text(body)
  859. } else {
  860. body.trim().to_string()
  861. }
  862. }
  863. fn summarize_web_fetch(
  864. url: &str,
  865. prompt: &str,
  866. content: &str,
  867. raw_body: &str,
  868. content_type: &str,
  869. ) -> String {
  870. let lower_prompt = prompt.to_lowercase();
  871. let compact = collapse_whitespace(content);
  872. let detail = if lower_prompt.contains("title") {
  873. extract_title(content, raw_body, content_type).map_or_else(
  874. || preview_text(&compact, 600),
  875. |title| format!("Title: {title}"),
  876. )
  877. } else if lower_prompt.contains("summary") || lower_prompt.contains("summarize") {
  878. preview_text(&compact, 900)
  879. } else {
  880. let preview = preview_text(&compact, 900);
  881. format!("Prompt: {prompt}\nContent preview:\n{preview}")
  882. };
  883. format!("Fetched {url}\n{detail}")
  884. }
  885. fn extract_title(content: &str, raw_body: &str, content_type: &str) -> Option<String> {
  886. if content_type.contains("html") {
  887. let lowered = raw_body.to_lowercase();
  888. if let Some(start) = lowered.find("<title>") {
  889. let after = start + "<title>".len();
  890. if let Some(end_rel) = lowered[after..].find("</title>") {
  891. let title =
  892. collapse_whitespace(&decode_html_entities(&raw_body[after..after + end_rel]));
  893. if !title.is_empty() {
  894. return Some(title);
  895. }
  896. }
  897. }
  898. }
  899. for line in content.lines() {
  900. let trimmed = line.trim();
  901. if !trimmed.is_empty() {
  902. return Some(trimmed.to_string());
  903. }
  904. }
  905. None
  906. }
  907. fn html_to_text(html: &str) -> String {
  908. let mut text = String::with_capacity(html.len());
  909. let mut in_tag = false;
  910. let mut previous_was_space = false;
  911. for ch in html.chars() {
  912. match ch {
  913. '<' => in_tag = true,
  914. '>' => in_tag = false,
  915. _ if in_tag => {}
  916. '&' => {
  917. text.push('&');
  918. previous_was_space = false;
  919. }
  920. ch if ch.is_whitespace() => {
  921. if !previous_was_space {
  922. text.push(' ');
  923. previous_was_space = true;
  924. }
  925. }
  926. _ => {
  927. text.push(ch);
  928. previous_was_space = false;
  929. }
  930. }
  931. }
  932. collapse_whitespace(&decode_html_entities(&text))
  933. }
  934. fn decode_html_entities(input: &str) -> String {
  935. input
  936. .replace("&amp;", "&")
  937. .replace("&lt;", "<")
  938. .replace("&gt;", ">")
  939. .replace("&quot;", "\"")
  940. .replace("&#39;", "'")
  941. .replace("&nbsp;", " ")
  942. }
  943. fn collapse_whitespace(input: &str) -> String {
  944. input.split_whitespace().collect::<Vec<_>>().join(" ")
  945. }
  946. fn preview_text(input: &str, max_chars: usize) -> String {
  947. if input.chars().count() <= max_chars {
  948. return input.to_string();
  949. }
  950. let shortened = input.chars().take(max_chars).collect::<String>();
  951. format!("{}…", shortened.trim_end())
  952. }
  953. fn extract_search_hits(html: &str) -> Vec<SearchHit> {
  954. let mut hits = Vec::new();
  955. let mut remaining = html;
  956. while let Some(anchor_start) = remaining.find("result__a") {
  957. let after_class = &remaining[anchor_start..];
  958. let Some(href_idx) = after_class.find("href=") else {
  959. remaining = &after_class[1..];
  960. continue;
  961. };
  962. let href_slice = &after_class[href_idx + 5..];
  963. let Some((url, rest)) = extract_quoted_value(href_slice) else {
  964. remaining = &after_class[1..];
  965. continue;
  966. };
  967. let Some(close_tag_idx) = rest.find('>') else {
  968. remaining = &after_class[1..];
  969. continue;
  970. };
  971. let after_tag = &rest[close_tag_idx + 1..];
  972. let Some(end_anchor_idx) = after_tag.find("</a>") else {
  973. remaining = &after_tag[1..];
  974. continue;
  975. };
  976. let title = html_to_text(&after_tag[..end_anchor_idx]);
  977. if let Some(decoded_url) = decode_duckduckgo_redirect(&url) {
  978. hits.push(SearchHit {
  979. title: title.trim().to_string(),
  980. url: decoded_url,
  981. });
  982. }
  983. remaining = &after_tag[end_anchor_idx + 4..];
  984. }
  985. hits
  986. }
  987. fn extract_search_hits_from_generic_links(html: &str) -> Vec<SearchHit> {
  988. let mut hits = Vec::new();
  989. let mut remaining = html;
  990. while let Some(anchor_start) = remaining.find("<a") {
  991. let after_anchor = &remaining[anchor_start..];
  992. let Some(href_idx) = after_anchor.find("href=") else {
  993. remaining = &after_anchor[2..];
  994. continue;
  995. };
  996. let href_slice = &after_anchor[href_idx + 5..];
  997. let Some((url, rest)) = extract_quoted_value(href_slice) else {
  998. remaining = &after_anchor[2..];
  999. continue;
  1000. };
  1001. let Some(close_tag_idx) = rest.find('>') else {
  1002. remaining = &after_anchor[2..];
  1003. continue;
  1004. };
  1005. let after_tag = &rest[close_tag_idx + 1..];
  1006. let Some(end_anchor_idx) = after_tag.find("</a>") else {
  1007. remaining = &after_anchor[2..];
  1008. continue;
  1009. };
  1010. let title = html_to_text(&after_tag[..end_anchor_idx]);
  1011. if title.trim().is_empty() {
  1012. remaining = &after_tag[end_anchor_idx + 4..];
  1013. continue;
  1014. }
  1015. let decoded_url = decode_duckduckgo_redirect(&url).unwrap_or(url);
  1016. if decoded_url.starts_with("http://") || decoded_url.starts_with("https://") {
  1017. hits.push(SearchHit {
  1018. title: title.trim().to_string(),
  1019. url: decoded_url,
  1020. });
  1021. }
  1022. remaining = &after_tag[end_anchor_idx + 4..];
  1023. }
  1024. hits
  1025. }
  1026. fn extract_quoted_value(input: &str) -> Option<(String, &str)> {
  1027. let quote = input.chars().next()?;
  1028. if quote != '"' && quote != '\'' {
  1029. return None;
  1030. }
  1031. let rest = &input[quote.len_utf8()..];
  1032. let end = rest.find(quote)?;
  1033. Some((rest[..end].to_string(), &rest[end + quote.len_utf8()..]))
  1034. }
  1035. fn decode_duckduckgo_redirect(url: &str) -> Option<String> {
  1036. if url.starts_with("http://") || url.starts_with("https://") {
  1037. return Some(html_entity_decode_url(url));
  1038. }
  1039. let joined = if url.starts_with("//") {
  1040. format!("https:{url}")
  1041. } else if url.starts_with('/') {
  1042. format!("https://duckduckgo.com{url}")
  1043. } else {
  1044. return None;
  1045. };
  1046. let parsed = reqwest::Url::parse(&joined).ok()?;
  1047. if parsed.path() == "/l/" || parsed.path() == "/l" {
  1048. for (key, value) in parsed.query_pairs() {
  1049. if key == "uddg" {
  1050. return Some(html_entity_decode_url(value.as_ref()));
  1051. }
  1052. }
  1053. }
  1054. Some(joined)
  1055. }
  1056. fn html_entity_decode_url(url: &str) -> String {
  1057. decode_html_entities(url)
  1058. }
  1059. fn host_matches_list(url: &str, domains: &[String]) -> bool {
  1060. let Ok(parsed) = reqwest::Url::parse(url) else {
  1061. return false;
  1062. };
  1063. let Some(host) = parsed.host_str() else {
  1064. return false;
  1065. };
  1066. let host = host.to_ascii_lowercase();
  1067. domains.iter().any(|domain| {
  1068. let normalized = normalize_domain_filter(domain);
  1069. !normalized.is_empty() && (host == normalized || host.ends_with(&format!(".{normalized}")))
  1070. })
  1071. }
  1072. fn normalize_domain_filter(domain: &str) -> String {
  1073. let trimmed = domain.trim();
  1074. let candidate = reqwest::Url::parse(trimmed)
  1075. .ok()
  1076. .and_then(|url| url.host_str().map(str::to_string))
  1077. .unwrap_or_else(|| trimmed.to_string());
  1078. candidate
  1079. .trim()
  1080. .trim_start_matches('.')
  1081. .trim_end_matches('/')
  1082. .to_ascii_lowercase()
  1083. }
  1084. fn dedupe_hits(hits: &mut Vec<SearchHit>) {
  1085. let mut seen = BTreeSet::new();
  1086. hits.retain(|hit| seen.insert(hit.url.clone()));
  1087. }
  1088. fn execute_todo_write(input: TodoWriteInput) -> Result<TodoWriteOutput, String> {
  1089. validate_todos(&input.todos)?;
  1090. let store_path = todo_store_path()?;
  1091. let old_todos = if store_path.exists() {
  1092. serde_json::from_str::<Vec<TodoItem>>(
  1093. &std::fs::read_to_string(&store_path).map_err(|error| error.to_string())?,
  1094. )
  1095. .map_err(|error| error.to_string())?
  1096. } else {
  1097. Vec::new()
  1098. };
  1099. let all_done = input
  1100. .todos
  1101. .iter()
  1102. .all(|todo| matches!(todo.status, TodoStatus::Completed));
  1103. let persisted = if all_done {
  1104. Vec::new()
  1105. } else {
  1106. input.todos.clone()
  1107. };
  1108. if let Some(parent) = store_path.parent() {
  1109. std::fs::create_dir_all(parent).map_err(|error| error.to_string())?;
  1110. }
  1111. std::fs::write(
  1112. &store_path,
  1113. serde_json::to_string_pretty(&persisted).map_err(|error| error.to_string())?,
  1114. )
  1115. .map_err(|error| error.to_string())?;
  1116. let verification_nudge_needed = (all_done
  1117. && input.todos.len() >= 3
  1118. && !input
  1119. .todos
  1120. .iter()
  1121. .any(|todo| todo.content.to_lowercase().contains("verif")))
  1122. .then_some(true);
  1123. Ok(TodoWriteOutput {
  1124. old_todos,
  1125. new_todos: input.todos,
  1126. verification_nudge_needed,
  1127. })
  1128. }
  1129. fn execute_skill(input: SkillInput) -> Result<SkillOutput, String> {
  1130. let skill_path = resolve_skill_path(&input.skill)?;
  1131. let prompt = std::fs::read_to_string(&skill_path).map_err(|error| error.to_string())?;
  1132. let description = parse_skill_description(&prompt);
  1133. Ok(SkillOutput {
  1134. skill: input.skill,
  1135. path: skill_path.display().to_string(),
  1136. args: input.args,
  1137. description,
  1138. prompt,
  1139. })
  1140. }
  1141. fn validate_todos(todos: &[TodoItem]) -> Result<(), String> {
  1142. if todos.is_empty() {
  1143. return Err(String::from("todos must not be empty"));
  1144. }
  1145. let in_progress = todos
  1146. .iter()
  1147. .filter(|todo| matches!(todo.status, TodoStatus::InProgress))
  1148. .count();
  1149. if in_progress > 1 {
  1150. return Err(String::from(
  1151. "exactly zero or one todo items may be in_progress",
  1152. ));
  1153. }
  1154. if todos.iter().any(|todo| todo.content.trim().is_empty()) {
  1155. return Err(String::from("todo content must not be empty"));
  1156. }
  1157. if todos.iter().any(|todo| todo.active_form.trim().is_empty()) {
  1158. return Err(String::from("todo activeForm must not be empty"));
  1159. }
  1160. Ok(())
  1161. }
  1162. fn todo_store_path() -> Result<std::path::PathBuf, String> {
  1163. if let Ok(path) = std::env::var("CLAWD_TODO_STORE") {
  1164. return Ok(std::path::PathBuf::from(path));
  1165. }
  1166. let cwd = std::env::current_dir().map_err(|error| error.to_string())?;
  1167. Ok(cwd.join(".clawd-todos.json"))
  1168. }
  1169. fn resolve_skill_path(skill: &str) -> Result<std::path::PathBuf, String> {
  1170. let requested = skill.trim().trim_start_matches('/').trim_start_matches('$');
  1171. if requested.is_empty() {
  1172. return Err(String::from("skill must not be empty"));
  1173. }
  1174. let mut candidates = Vec::new();
  1175. if let Ok(codex_home) = std::env::var("CODEX_HOME") {
  1176. candidates.push(std::path::PathBuf::from(codex_home).join("skills"));
  1177. }
  1178. candidates.push(std::path::PathBuf::from("/home/bellman/.codex/skills"));
  1179. for root in candidates {
  1180. let direct = root.join(requested).join("SKILL.md");
  1181. if direct.exists() {
  1182. return Ok(direct);
  1183. }
  1184. if let Ok(entries) = std::fs::read_dir(&root) {
  1185. for entry in entries.flatten() {
  1186. let path = entry.path().join("SKILL.md");
  1187. if !path.exists() {
  1188. continue;
  1189. }
  1190. if entry
  1191. .file_name()
  1192. .to_string_lossy()
  1193. .eq_ignore_ascii_case(requested)
  1194. {
  1195. return Ok(path);
  1196. }
  1197. }
  1198. }
  1199. }
  1200. Err(format!("unknown skill: {requested}"))
  1201. }
  1202. fn execute_agent(input: AgentInput) -> Result<AgentOutput, String> {
  1203. if input.description.trim().is_empty() {
  1204. return Err(String::from("description must not be empty"));
  1205. }
  1206. if input.prompt.trim().is_empty() {
  1207. return Err(String::from("prompt must not be empty"));
  1208. }
  1209. let agent_id = make_agent_id();
  1210. let output_dir = agent_store_dir()?;
  1211. std::fs::create_dir_all(&output_dir).map_err(|error| error.to_string())?;
  1212. let output_file = output_dir.join(format!("{agent_id}.md"));
  1213. let manifest_file = output_dir.join(format!("{agent_id}.json"));
  1214. let normalized_subagent_type = normalize_subagent_type(input.subagent_type.as_deref());
  1215. let agent_name = input
  1216. .name
  1217. .as_deref()
  1218. .map(slugify_agent_name)
  1219. .filter(|name| !name.is_empty())
  1220. .unwrap_or_else(|| slugify_agent_name(&input.description));
  1221. let created_at = iso8601_now();
  1222. let output_contents = format!(
  1223. "# Agent Task
  1224. - id: {}
  1225. - name: {}
  1226. - description: {}
  1227. - subagent_type: {}
  1228. - created_at: {}
  1229. ## Prompt
  1230. {}
  1231. ",
  1232. agent_id, agent_name, input.description, normalized_subagent_type, created_at, input.prompt
  1233. );
  1234. std::fs::write(&output_file, output_contents).map_err(|error| error.to_string())?;
  1235. let manifest = AgentOutput {
  1236. agent_id,
  1237. name: agent_name,
  1238. description: input.description,
  1239. subagent_type: Some(normalized_subagent_type),
  1240. model: input.model,
  1241. status: String::from("queued"),
  1242. output_file: output_file.display().to_string(),
  1243. manifest_file: manifest_file.display().to_string(),
  1244. created_at,
  1245. };
  1246. std::fs::write(
  1247. &manifest_file,
  1248. serde_json::to_string_pretty(&manifest).map_err(|error| error.to_string())?,
  1249. )
  1250. .map_err(|error| error.to_string())?;
  1251. Ok(manifest)
  1252. }
  1253. #[allow(clippy::needless_pass_by_value)]
  1254. fn execute_tool_search(input: ToolSearchInput) -> ToolSearchOutput {
  1255. let deferred = deferred_tool_specs();
  1256. let max_results = input.max_results.unwrap_or(5).max(1);
  1257. let query = input.query.trim().to_string();
  1258. let normalized_query = normalize_tool_search_query(&query);
  1259. let matches = search_tool_specs(&query, max_results, &deferred);
  1260. ToolSearchOutput {
  1261. matches,
  1262. query,
  1263. normalized_query,
  1264. total_deferred_tools: deferred.len(),
  1265. pending_mcp_servers: None,
  1266. }
  1267. }
  1268. fn deferred_tool_specs() -> Vec<ToolSpec> {
  1269. mvp_tool_specs()
  1270. .into_iter()
  1271. .filter(|spec| {
  1272. !matches!(
  1273. spec.name,
  1274. "bash" | "read_file" | "write_file" | "edit_file" | "glob_search" | "grep_search"
  1275. )
  1276. })
  1277. .collect()
  1278. }
  1279. fn search_tool_specs(query: &str, max_results: usize, specs: &[ToolSpec]) -> Vec<String> {
  1280. let lowered = query.to_lowercase();
  1281. if let Some(selection) = lowered.strip_prefix("select:") {
  1282. return selection
  1283. .split(',')
  1284. .map(str::trim)
  1285. .filter(|part| !part.is_empty())
  1286. .filter_map(|wanted| {
  1287. let wanted = canonical_tool_token(wanted);
  1288. specs
  1289. .iter()
  1290. .find(|spec| canonical_tool_token(spec.name) == wanted)
  1291. .map(|spec| spec.name.to_string())
  1292. })
  1293. .take(max_results)
  1294. .collect();
  1295. }
  1296. let mut required = Vec::new();
  1297. let mut optional = Vec::new();
  1298. for term in lowered.split_whitespace() {
  1299. if let Some(rest) = term.strip_prefix('+') {
  1300. if !rest.is_empty() {
  1301. required.push(rest);
  1302. }
  1303. } else {
  1304. optional.push(term);
  1305. }
  1306. }
  1307. let terms = if required.is_empty() {
  1308. optional.clone()
  1309. } else {
  1310. required.iter().chain(optional.iter()).copied().collect()
  1311. };
  1312. let mut scored = specs
  1313. .iter()
  1314. .filter_map(|spec| {
  1315. let name = spec.name.to_lowercase();
  1316. let canonical_name = canonical_tool_token(spec.name);
  1317. let normalized_description = normalize_tool_search_query(spec.description);
  1318. let haystack = format!(
  1319. "{name} {} {canonical_name}",
  1320. spec.description.to_lowercase()
  1321. );
  1322. let normalized_haystack = format!("{canonical_name} {normalized_description}");
  1323. if required.iter().any(|term| !haystack.contains(term)) {
  1324. return None;
  1325. }
  1326. let mut score = 0_i32;
  1327. for term in &terms {
  1328. let canonical_term = canonical_tool_token(term);
  1329. if haystack.contains(term) {
  1330. score += 2;
  1331. }
  1332. if name == *term {
  1333. score += 8;
  1334. }
  1335. if name.contains(term) {
  1336. score += 4;
  1337. }
  1338. if canonical_name == canonical_term {
  1339. score += 12;
  1340. }
  1341. if normalized_haystack.contains(&canonical_term) {
  1342. score += 3;
  1343. }
  1344. }
  1345. if score == 0 && !lowered.is_empty() {
  1346. return None;
  1347. }
  1348. Some((score, spec.name.to_string()))
  1349. })
  1350. .collect::<Vec<_>>();
  1351. scored.sort_by(|left, right| right.0.cmp(&left.0).then_with(|| left.1.cmp(&right.1)));
  1352. scored
  1353. .into_iter()
  1354. .map(|(_, name)| name)
  1355. .take(max_results)
  1356. .collect()
  1357. }
  1358. fn normalize_tool_search_query(query: &str) -> String {
  1359. query
  1360. .trim()
  1361. .split(|ch: char| ch.is_whitespace() || ch == ',')
  1362. .filter(|term| !term.is_empty())
  1363. .map(canonical_tool_token)
  1364. .collect::<Vec<_>>()
  1365. .join(" ")
  1366. }
  1367. fn canonical_tool_token(value: &str) -> String {
  1368. let mut canonical = value
  1369. .chars()
  1370. .filter(char::is_ascii_alphanumeric)
  1371. .flat_map(char::to_lowercase)
  1372. .collect::<String>();
  1373. if let Some(stripped) = canonical.strip_suffix("tool") {
  1374. canonical = stripped.to_string();
  1375. }
  1376. canonical
  1377. }
  1378. fn agent_store_dir() -> Result<std::path::PathBuf, String> {
  1379. if let Ok(path) = std::env::var("CLAWD_AGENT_STORE") {
  1380. return Ok(std::path::PathBuf::from(path));
  1381. }
  1382. let cwd = std::env::current_dir().map_err(|error| error.to_string())?;
  1383. if let Some(workspace_root) = cwd.ancestors().nth(2) {
  1384. return Ok(workspace_root.join(".clawd-agents"));
  1385. }
  1386. Ok(cwd.join(".clawd-agents"))
  1387. }
  1388. fn make_agent_id() -> String {
  1389. let nanos = std::time::SystemTime::now()
  1390. .duration_since(std::time::UNIX_EPOCH)
  1391. .unwrap_or_default()
  1392. .as_nanos();
  1393. format!("agent-{nanos}")
  1394. }
  1395. fn slugify_agent_name(description: &str) -> String {
  1396. let mut out = description
  1397. .chars()
  1398. .map(|ch| {
  1399. if ch.is_ascii_alphanumeric() {
  1400. ch.to_ascii_lowercase()
  1401. } else {
  1402. '-'
  1403. }
  1404. })
  1405. .collect::<String>();
  1406. while out.contains("--") {
  1407. out = out.replace("--", "-");
  1408. }
  1409. out.trim_matches('-').chars().take(32).collect()
  1410. }
  1411. fn normalize_subagent_type(subagent_type: Option<&str>) -> String {
  1412. let trimmed = subagent_type.map(str::trim).unwrap_or_default();
  1413. if trimmed.is_empty() {
  1414. return String::from("general-purpose");
  1415. }
  1416. match canonical_tool_token(trimmed).as_str() {
  1417. "general" | "generalpurpose" | "generalpurposeagent" => String::from("general-purpose"),
  1418. "explore" | "explorer" | "exploreagent" => String::from("Explore"),
  1419. "plan" | "planagent" => String::from("Plan"),
  1420. "verification" | "verificationagent" | "verify" | "verifier" => {
  1421. String::from("Verification")
  1422. }
  1423. "claudecodeguide" | "claudecodeguideagent" | "guide" => String::from("claude-code-guide"),
  1424. "statusline" | "statuslinesetup" => String::from("statusline-setup"),
  1425. _ => trimmed.to_string(),
  1426. }
  1427. }
  1428. fn iso8601_now() -> String {
  1429. std::time::SystemTime::now()
  1430. .duration_since(std::time::UNIX_EPOCH)
  1431. .unwrap_or_default()
  1432. .as_secs()
  1433. .to_string()
  1434. }
  1435. #[allow(clippy::too_many_lines)]
  1436. fn execute_notebook_edit(input: NotebookEditInput) -> Result<NotebookEditOutput, String> {
  1437. let path = std::path::PathBuf::from(&input.notebook_path);
  1438. if path.extension().and_then(|ext| ext.to_str()) != Some("ipynb") {
  1439. return Err(String::from(
  1440. "File must be a Jupyter notebook (.ipynb file).",
  1441. ));
  1442. }
  1443. let original_file = std::fs::read_to_string(&path).map_err(|error| error.to_string())?;
  1444. let mut notebook: serde_json::Value =
  1445. serde_json::from_str(&original_file).map_err(|error| error.to_string())?;
  1446. let language = notebook
  1447. .get("metadata")
  1448. .and_then(|metadata| metadata.get("kernelspec"))
  1449. .and_then(|kernelspec| kernelspec.get("language"))
  1450. .and_then(serde_json::Value::as_str)
  1451. .unwrap_or("python")
  1452. .to_string();
  1453. let cells = notebook
  1454. .get_mut("cells")
  1455. .and_then(serde_json::Value::as_array_mut)
  1456. .ok_or_else(|| String::from("Notebook cells array not found"))?;
  1457. let edit_mode = input.edit_mode.unwrap_or(NotebookEditMode::Replace);
  1458. let target_index = match input.cell_id.as_deref() {
  1459. Some(cell_id) => Some(resolve_cell_index(cells, Some(cell_id), edit_mode)?),
  1460. None if matches!(
  1461. edit_mode,
  1462. NotebookEditMode::Replace | NotebookEditMode::Delete
  1463. ) =>
  1464. {
  1465. Some(resolve_cell_index(cells, None, edit_mode)?)
  1466. }
  1467. None => None,
  1468. };
  1469. let resolved_cell_type = match edit_mode {
  1470. NotebookEditMode::Delete => None,
  1471. NotebookEditMode::Insert => Some(input.cell_type.unwrap_or(NotebookCellType::Code)),
  1472. NotebookEditMode::Replace => Some(input.cell_type.unwrap_or_else(|| {
  1473. target_index
  1474. .and_then(|index| cells.get(index))
  1475. .and_then(cell_kind)
  1476. .unwrap_or(NotebookCellType::Code)
  1477. })),
  1478. };
  1479. let new_source = require_notebook_source(input.new_source, edit_mode)?;
  1480. let cell_id = match edit_mode {
  1481. NotebookEditMode::Insert => {
  1482. let resolved_cell_type = resolved_cell_type.expect("insert cell type");
  1483. let new_id = make_cell_id(cells.len());
  1484. let new_cell = build_notebook_cell(&new_id, resolved_cell_type, &new_source);
  1485. let insert_at = target_index.map_or(cells.len(), |index| index + 1);
  1486. cells.insert(insert_at, new_cell);
  1487. cells
  1488. .get(insert_at)
  1489. .and_then(|cell| cell.get("id"))
  1490. .and_then(serde_json::Value::as_str)
  1491. .map(ToString::to_string)
  1492. }
  1493. NotebookEditMode::Delete => {
  1494. let removed = cells.remove(target_index.expect("delete target index"));
  1495. removed
  1496. .get("id")
  1497. .and_then(serde_json::Value::as_str)
  1498. .map(ToString::to_string)
  1499. }
  1500. NotebookEditMode::Replace => {
  1501. let resolved_cell_type = resolved_cell_type.expect("replace cell type");
  1502. let cell = cells
  1503. .get_mut(target_index.expect("replace target index"))
  1504. .ok_or_else(|| String::from("Cell index out of range"))?;
  1505. cell["source"] = serde_json::Value::Array(source_lines(&new_source));
  1506. cell["cell_type"] = serde_json::Value::String(match resolved_cell_type {
  1507. NotebookCellType::Code => String::from("code"),
  1508. NotebookCellType::Markdown => String::from("markdown"),
  1509. });
  1510. match resolved_cell_type {
  1511. NotebookCellType::Code => {
  1512. if !cell.get("outputs").is_some_and(serde_json::Value::is_array) {
  1513. cell["outputs"] = json!([]);
  1514. }
  1515. if cell.get("execution_count").is_none() {
  1516. cell["execution_count"] = serde_json::Value::Null;
  1517. }
  1518. }
  1519. NotebookCellType::Markdown => {
  1520. if let Some(object) = cell.as_object_mut() {
  1521. object.remove("outputs");
  1522. object.remove("execution_count");
  1523. }
  1524. }
  1525. }
  1526. cell.get("id")
  1527. .and_then(serde_json::Value::as_str)
  1528. .map(ToString::to_string)
  1529. }
  1530. };
  1531. let updated_file =
  1532. serde_json::to_string_pretty(&notebook).map_err(|error| error.to_string())?;
  1533. std::fs::write(&path, &updated_file).map_err(|error| error.to_string())?;
  1534. Ok(NotebookEditOutput {
  1535. new_source,
  1536. cell_id,
  1537. cell_type: resolved_cell_type,
  1538. language,
  1539. edit_mode: format_notebook_edit_mode(edit_mode),
  1540. error: None,
  1541. notebook_path: path.display().to_string(),
  1542. original_file,
  1543. updated_file,
  1544. })
  1545. }
  1546. fn require_notebook_source(
  1547. source: Option<String>,
  1548. edit_mode: NotebookEditMode,
  1549. ) -> Result<String, String> {
  1550. match edit_mode {
  1551. NotebookEditMode::Delete => Ok(source.unwrap_or_default()),
  1552. NotebookEditMode::Insert | NotebookEditMode::Replace => source
  1553. .ok_or_else(|| String::from("new_source is required for insert and replace edits")),
  1554. }
  1555. }
  1556. fn build_notebook_cell(cell_id: &str, cell_type: NotebookCellType, source: &str) -> Value {
  1557. let mut cell = json!({
  1558. "cell_type": match cell_type {
  1559. NotebookCellType::Code => "code",
  1560. NotebookCellType::Markdown => "markdown",
  1561. },
  1562. "id": cell_id,
  1563. "metadata": {},
  1564. "source": source_lines(source),
  1565. });
  1566. if let Some(object) = cell.as_object_mut() {
  1567. match cell_type {
  1568. NotebookCellType::Code => {
  1569. object.insert(String::from("outputs"), json!([]));
  1570. object.insert(String::from("execution_count"), Value::Null);
  1571. }
  1572. NotebookCellType::Markdown => {}
  1573. }
  1574. }
  1575. cell
  1576. }
  1577. fn cell_kind(cell: &serde_json::Value) -> Option<NotebookCellType> {
  1578. cell.get("cell_type")
  1579. .and_then(serde_json::Value::as_str)
  1580. .map(|kind| {
  1581. if kind == "markdown" {
  1582. NotebookCellType::Markdown
  1583. } else {
  1584. NotebookCellType::Code
  1585. }
  1586. })
  1587. }
  1588. #[allow(clippy::needless_pass_by_value)]
  1589. fn execute_sleep(input: SleepInput) -> SleepOutput {
  1590. std::thread::sleep(Duration::from_millis(input.duration_ms));
  1591. SleepOutput {
  1592. duration_ms: input.duration_ms,
  1593. message: format!("Slept for {}ms", input.duration_ms),
  1594. }
  1595. }
  1596. fn execute_brief(input: BriefInput) -> Result<BriefOutput, String> {
  1597. if input.message.trim().is_empty() {
  1598. return Err(String::from("message must not be empty"));
  1599. }
  1600. let attachments = input
  1601. .attachments
  1602. .as_ref()
  1603. .map(|paths| {
  1604. paths
  1605. .iter()
  1606. .map(|path| resolve_attachment(path))
  1607. .collect::<Result<Vec<_>, String>>()
  1608. })
  1609. .transpose()?;
  1610. let message = match input.status {
  1611. BriefStatus::Normal | BriefStatus::Proactive => input.message,
  1612. };
  1613. Ok(BriefOutput {
  1614. message,
  1615. attachments,
  1616. sent_at: iso8601_timestamp(),
  1617. })
  1618. }
  1619. fn resolve_attachment(path: &str) -> Result<ResolvedAttachment, String> {
  1620. let resolved = std::fs::canonicalize(path).map_err(|error| error.to_string())?;
  1621. let metadata = std::fs::metadata(&resolved).map_err(|error| error.to_string())?;
  1622. Ok(ResolvedAttachment {
  1623. path: resolved.display().to_string(),
  1624. size: metadata.len(),
  1625. is_image: is_image_path(&resolved),
  1626. })
  1627. }
  1628. fn is_image_path(path: &Path) -> bool {
  1629. matches!(
  1630. path.extension()
  1631. .and_then(|ext| ext.to_str())
  1632. .map(str::to_ascii_lowercase)
  1633. .as_deref(),
  1634. Some("png" | "jpg" | "jpeg" | "gif" | "webp" | "bmp" | "svg")
  1635. )
  1636. }
  1637. fn execute_config(input: ConfigInput) -> Result<ConfigOutput, String> {
  1638. let setting = input.setting.trim();
  1639. if setting.is_empty() {
  1640. return Err(String::from("setting must not be empty"));
  1641. }
  1642. let Some(spec) = supported_config_setting(setting) else {
  1643. return Ok(ConfigOutput {
  1644. success: false,
  1645. operation: None,
  1646. setting: None,
  1647. value: None,
  1648. previous_value: None,
  1649. new_value: None,
  1650. error: Some(format!("Unknown setting: \"{setting}\"")),
  1651. });
  1652. };
  1653. let path = config_file_for_scope(spec.scope)?;
  1654. let mut document = read_json_object(&path)?;
  1655. if let Some(value) = input.value {
  1656. let normalized = normalize_config_value(spec, value)?;
  1657. let previous_value = get_nested_value(&document, spec.path).cloned();
  1658. set_nested_value(&mut document, spec.path, normalized.clone());
  1659. write_json_object(&path, &document)?;
  1660. Ok(ConfigOutput {
  1661. success: true,
  1662. operation: Some(String::from("set")),
  1663. setting: Some(setting.to_string()),
  1664. value: Some(normalized.clone()),
  1665. previous_value,
  1666. new_value: Some(normalized),
  1667. error: None,
  1668. })
  1669. } else {
  1670. Ok(ConfigOutput {
  1671. success: true,
  1672. operation: Some(String::from("get")),
  1673. setting: Some(setting.to_string()),
  1674. value: get_nested_value(&document, spec.path).cloned(),
  1675. previous_value: None,
  1676. new_value: None,
  1677. error: None,
  1678. })
  1679. }
  1680. }
  1681. fn execute_structured_output(input: StructuredOutputInput) -> StructuredOutputResult {
  1682. StructuredOutputResult {
  1683. data: String::from("Structured output provided successfully"),
  1684. structured_output: input.0,
  1685. }
  1686. }
  1687. fn execute_repl(input: ReplInput) -> Result<ReplOutput, String> {
  1688. if input.code.trim().is_empty() {
  1689. return Err(String::from("code must not be empty"));
  1690. }
  1691. let _ = input.timeout_ms;
  1692. let runtime = resolve_repl_runtime(&input.language)?;
  1693. let started = Instant::now();
  1694. let output = Command::new(runtime.program)
  1695. .args(runtime.args)
  1696. .arg(&input.code)
  1697. .output()
  1698. .map_err(|error| error.to_string())?;
  1699. Ok(ReplOutput {
  1700. language: input.language,
  1701. stdout: String::from_utf8_lossy(&output.stdout).into_owned(),
  1702. stderr: String::from_utf8_lossy(&output.stderr).into_owned(),
  1703. exit_code: output.status.code().unwrap_or(1),
  1704. duration_ms: started.elapsed().as_millis(),
  1705. })
  1706. }
  1707. struct ReplRuntime {
  1708. program: &'static str,
  1709. args: &'static [&'static str],
  1710. }
  1711. fn resolve_repl_runtime(language: &str) -> Result<ReplRuntime, String> {
  1712. match language.trim().to_ascii_lowercase().as_str() {
  1713. "python" | "py" => Ok(ReplRuntime {
  1714. program: detect_first_command(&["python3", "python"])
  1715. .ok_or_else(|| String::from("python runtime not found"))?,
  1716. args: &["-c"],
  1717. }),
  1718. "javascript" | "js" | "node" => Ok(ReplRuntime {
  1719. program: detect_first_command(&["node"])
  1720. .ok_or_else(|| String::from("node runtime not found"))?,
  1721. args: &["-e"],
  1722. }),
  1723. "sh" | "shell" | "bash" => Ok(ReplRuntime {
  1724. program: detect_first_command(&["bash", "sh"])
  1725. .ok_or_else(|| String::from("shell runtime not found"))?,
  1726. args: &["-lc"],
  1727. }),
  1728. other => Err(format!("unsupported REPL language: {other}")),
  1729. }
  1730. }
  1731. fn detect_first_command(commands: &[&'static str]) -> Option<&'static str> {
  1732. commands
  1733. .iter()
  1734. .copied()
  1735. .find(|command| command_exists(command))
  1736. }
  1737. #[derive(Clone, Copy)]
  1738. enum ConfigScope {
  1739. Global,
  1740. Settings,
  1741. }
  1742. #[derive(Clone, Copy)]
  1743. struct ConfigSettingSpec {
  1744. scope: ConfigScope,
  1745. kind: ConfigKind,
  1746. path: &'static [&'static str],
  1747. options: Option<&'static [&'static str]>,
  1748. }
  1749. #[derive(Clone, Copy)]
  1750. enum ConfigKind {
  1751. Boolean,
  1752. String,
  1753. }
  1754. fn supported_config_setting(setting: &str) -> Option<ConfigSettingSpec> {
  1755. Some(match setting {
  1756. "theme" => ConfigSettingSpec {
  1757. scope: ConfigScope::Global,
  1758. kind: ConfigKind::String,
  1759. path: &["theme"],
  1760. options: None,
  1761. },
  1762. "editorMode" => ConfigSettingSpec {
  1763. scope: ConfigScope::Global,
  1764. kind: ConfigKind::String,
  1765. path: &["editorMode"],
  1766. options: Some(&["default", "vim", "emacs"]),
  1767. },
  1768. "verbose" => ConfigSettingSpec {
  1769. scope: ConfigScope::Global,
  1770. kind: ConfigKind::Boolean,
  1771. path: &["verbose"],
  1772. options: None,
  1773. },
  1774. "preferredNotifChannel" => ConfigSettingSpec {
  1775. scope: ConfigScope::Global,
  1776. kind: ConfigKind::String,
  1777. path: &["preferredNotifChannel"],
  1778. options: None,
  1779. },
  1780. "autoCompactEnabled" => ConfigSettingSpec {
  1781. scope: ConfigScope::Global,
  1782. kind: ConfigKind::Boolean,
  1783. path: &["autoCompactEnabled"],
  1784. options: None,
  1785. },
  1786. "autoMemoryEnabled" => ConfigSettingSpec {
  1787. scope: ConfigScope::Settings,
  1788. kind: ConfigKind::Boolean,
  1789. path: &["autoMemoryEnabled"],
  1790. options: None,
  1791. },
  1792. "autoDreamEnabled" => ConfigSettingSpec {
  1793. scope: ConfigScope::Settings,
  1794. kind: ConfigKind::Boolean,
  1795. path: &["autoDreamEnabled"],
  1796. options: None,
  1797. },
  1798. "fileCheckpointingEnabled" => ConfigSettingSpec {
  1799. scope: ConfigScope::Global,
  1800. kind: ConfigKind::Boolean,
  1801. path: &["fileCheckpointingEnabled"],
  1802. options: None,
  1803. },
  1804. "showTurnDuration" => ConfigSettingSpec {
  1805. scope: ConfigScope::Global,
  1806. kind: ConfigKind::Boolean,
  1807. path: &["showTurnDuration"],
  1808. options: None,
  1809. },
  1810. "terminalProgressBarEnabled" => ConfigSettingSpec {
  1811. scope: ConfigScope::Global,
  1812. kind: ConfigKind::Boolean,
  1813. path: &["terminalProgressBarEnabled"],
  1814. options: None,
  1815. },
  1816. "todoFeatureEnabled" => ConfigSettingSpec {
  1817. scope: ConfigScope::Global,
  1818. kind: ConfigKind::Boolean,
  1819. path: &["todoFeatureEnabled"],
  1820. options: None,
  1821. },
  1822. "model" => ConfigSettingSpec {
  1823. scope: ConfigScope::Settings,
  1824. kind: ConfigKind::String,
  1825. path: &["model"],
  1826. options: None,
  1827. },
  1828. "alwaysThinkingEnabled" => ConfigSettingSpec {
  1829. scope: ConfigScope::Settings,
  1830. kind: ConfigKind::Boolean,
  1831. path: &["alwaysThinkingEnabled"],
  1832. options: None,
  1833. },
  1834. "permissions.defaultMode" => ConfigSettingSpec {
  1835. scope: ConfigScope::Settings,
  1836. kind: ConfigKind::String,
  1837. path: &["permissions", "defaultMode"],
  1838. options: Some(&["default", "plan", "acceptEdits", "dontAsk", "auto"]),
  1839. },
  1840. "language" => ConfigSettingSpec {
  1841. scope: ConfigScope::Settings,
  1842. kind: ConfigKind::String,
  1843. path: &["language"],
  1844. options: None,
  1845. },
  1846. "teammateMode" => ConfigSettingSpec {
  1847. scope: ConfigScope::Global,
  1848. kind: ConfigKind::String,
  1849. path: &["teammateMode"],
  1850. options: Some(&["tmux", "in-process", "auto"]),
  1851. },
  1852. _ => return None,
  1853. })
  1854. }
  1855. fn normalize_config_value(spec: ConfigSettingSpec, value: ConfigValue) -> Result<Value, String> {
  1856. let normalized = match (spec.kind, value) {
  1857. (ConfigKind::Boolean, ConfigValue::Bool(value)) => Value::Bool(value),
  1858. (ConfigKind::Boolean, ConfigValue::String(value)) => {
  1859. match value.trim().to_ascii_lowercase().as_str() {
  1860. "true" => Value::Bool(true),
  1861. "false" => Value::Bool(false),
  1862. _ => return Err(String::from("setting requires true or false")),
  1863. }
  1864. }
  1865. (ConfigKind::Boolean, ConfigValue::Number(_)) => {
  1866. return Err(String::from("setting requires true or false"))
  1867. }
  1868. (ConfigKind::String, ConfigValue::String(value)) => Value::String(value),
  1869. (ConfigKind::String, ConfigValue::Bool(value)) => Value::String(value.to_string()),
  1870. (ConfigKind::String, ConfigValue::Number(value)) => json!(value),
  1871. };
  1872. if let Some(options) = spec.options {
  1873. let Some(as_str) = normalized.as_str() else {
  1874. return Err(String::from("setting requires a string value"));
  1875. };
  1876. if !options.iter().any(|option| option == &as_str) {
  1877. return Err(format!(
  1878. "Invalid value \"{as_str}\". Options: {}",
  1879. options.join(", ")
  1880. ));
  1881. }
  1882. }
  1883. Ok(normalized)
  1884. }
  1885. fn config_file_for_scope(scope: ConfigScope) -> Result<PathBuf, String> {
  1886. let cwd = std::env::current_dir().map_err(|error| error.to_string())?;
  1887. Ok(match scope {
  1888. ConfigScope::Global => config_home_dir()?.join("settings.json"),
  1889. ConfigScope::Settings => cwd.join(".claude").join("settings.local.json"),
  1890. })
  1891. }
  1892. fn config_home_dir() -> Result<PathBuf, String> {
  1893. if let Ok(path) = std::env::var("CLAUDE_CONFIG_HOME") {
  1894. return Ok(PathBuf::from(path));
  1895. }
  1896. let home = std::env::var("HOME").map_err(|_| String::from("HOME is not set"))?;
  1897. Ok(PathBuf::from(home).join(".claude"))
  1898. }
  1899. fn read_json_object(path: &Path) -> Result<serde_json::Map<String, Value>, String> {
  1900. match std::fs::read_to_string(path) {
  1901. Ok(contents) => {
  1902. if contents.trim().is_empty() {
  1903. return Ok(serde_json::Map::new());
  1904. }
  1905. serde_json::from_str::<Value>(&contents)
  1906. .map_err(|error| error.to_string())?
  1907. .as_object()
  1908. .cloned()
  1909. .ok_or_else(|| String::from("config file must contain a JSON object"))
  1910. }
  1911. Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(serde_json::Map::new()),
  1912. Err(error) => Err(error.to_string()),
  1913. }
  1914. }
  1915. fn write_json_object(path: &Path, value: &serde_json::Map<String, Value>) -> Result<(), String> {
  1916. if let Some(parent) = path.parent() {
  1917. std::fs::create_dir_all(parent).map_err(|error| error.to_string())?;
  1918. }
  1919. std::fs::write(
  1920. path,
  1921. serde_json::to_string_pretty(value).map_err(|error| error.to_string())?,
  1922. )
  1923. .map_err(|error| error.to_string())
  1924. }
  1925. fn get_nested_value<'a>(
  1926. value: &'a serde_json::Map<String, Value>,
  1927. path: &[&str],
  1928. ) -> Option<&'a Value> {
  1929. let (first, rest) = path.split_first()?;
  1930. let mut current = value.get(*first)?;
  1931. for key in rest {
  1932. current = current.as_object()?.get(*key)?;
  1933. }
  1934. Some(current)
  1935. }
  1936. fn set_nested_value(root: &mut serde_json::Map<String, Value>, path: &[&str], new_value: Value) {
  1937. let (first, rest) = path.split_first().expect("config path must not be empty");
  1938. if rest.is_empty() {
  1939. root.insert((*first).to_string(), new_value);
  1940. return;
  1941. }
  1942. let entry = root
  1943. .entry((*first).to_string())
  1944. .or_insert_with(|| Value::Object(serde_json::Map::new()));
  1945. if !entry.is_object() {
  1946. *entry = Value::Object(serde_json::Map::new());
  1947. }
  1948. let map = entry.as_object_mut().expect("object inserted");
  1949. set_nested_value(map, rest, new_value);
  1950. }
  1951. fn iso8601_timestamp() -> String {
  1952. if let Ok(output) = Command::new("date")
  1953. .args(["-u", "+%Y-%m-%dT%H:%M:%SZ"])
  1954. .output()
  1955. {
  1956. if output.status.success() {
  1957. return String::from_utf8_lossy(&output.stdout).trim().to_string();
  1958. }
  1959. }
  1960. iso8601_now()
  1961. }
  1962. #[allow(clippy::needless_pass_by_value)]
  1963. fn execute_powershell(input: PowerShellInput) -> std::io::Result<runtime::BashCommandOutput> {
  1964. let _ = &input.description;
  1965. let shell = detect_powershell_shell()?;
  1966. execute_shell_command(
  1967. shell,
  1968. &input.command,
  1969. input.timeout,
  1970. input.run_in_background,
  1971. )
  1972. }
  1973. fn detect_powershell_shell() -> std::io::Result<&'static str> {
  1974. if command_exists("pwsh") {
  1975. Ok("pwsh")
  1976. } else if command_exists("powershell") {
  1977. Ok("powershell")
  1978. } else {
  1979. Err(std::io::Error::new(
  1980. std::io::ErrorKind::NotFound,
  1981. "PowerShell executable not found (expected `pwsh` or `powershell` in PATH)",
  1982. ))
  1983. }
  1984. }
  1985. fn command_exists(command: &str) -> bool {
  1986. std::process::Command::new("sh")
  1987. .arg("-lc")
  1988. .arg(format!("command -v {command} >/dev/null 2>&1"))
  1989. .status()
  1990. .map(|status| status.success())
  1991. .unwrap_or(false)
  1992. }
  1993. #[allow(clippy::too_many_lines)]
  1994. fn execute_shell_command(
  1995. shell: &str,
  1996. command: &str,
  1997. timeout: Option<u64>,
  1998. run_in_background: Option<bool>,
  1999. ) -> std::io::Result<runtime::BashCommandOutput> {
  2000. if run_in_background.unwrap_or(false) {
  2001. let child = std::process::Command::new(shell)
  2002. .arg("-NoProfile")
  2003. .arg("-NonInteractive")
  2004. .arg("-Command")
  2005. .arg(command)
  2006. .stdin(std::process::Stdio::null())
  2007. .stdout(std::process::Stdio::null())
  2008. .stderr(std::process::Stdio::null())
  2009. .spawn()?;
  2010. return Ok(runtime::BashCommandOutput {
  2011. stdout: String::new(),
  2012. stderr: String::new(),
  2013. raw_output_path: None,
  2014. interrupted: false,
  2015. is_image: None,
  2016. background_task_id: Some(child.id().to_string()),
  2017. backgrounded_by_user: Some(true),
  2018. assistant_auto_backgrounded: Some(false),
  2019. dangerously_disable_sandbox: None,
  2020. return_code_interpretation: None,
  2021. no_output_expected: Some(true),
  2022. structured_content: None,
  2023. persisted_output_path: None,
  2024. persisted_output_size: None,
  2025. sandbox_status: None,
  2026. });
  2027. }
  2028. let mut process = std::process::Command::new(shell);
  2029. process
  2030. .arg("-NoProfile")
  2031. .arg("-NonInteractive")
  2032. .arg("-Command")
  2033. .arg(command);
  2034. process
  2035. .stdout(std::process::Stdio::piped())
  2036. .stderr(std::process::Stdio::piped());
  2037. if let Some(timeout_ms) = timeout {
  2038. let mut child = process.spawn()?;
  2039. let started = Instant::now();
  2040. loop {
  2041. if let Some(status) = child.try_wait()? {
  2042. let output = child.wait_with_output()?;
  2043. return Ok(runtime::BashCommandOutput {
  2044. stdout: String::from_utf8_lossy(&output.stdout).into_owned(),
  2045. stderr: String::from_utf8_lossy(&output.stderr).into_owned(),
  2046. raw_output_path: None,
  2047. interrupted: false,
  2048. is_image: None,
  2049. background_task_id: None,
  2050. backgrounded_by_user: None,
  2051. assistant_auto_backgrounded: None,
  2052. dangerously_disable_sandbox: None,
  2053. return_code_interpretation: status
  2054. .code()
  2055. .filter(|code| *code != 0)
  2056. .map(|code| format!("exit_code:{code}")),
  2057. no_output_expected: Some(output.stdout.is_empty() && output.stderr.is_empty()),
  2058. structured_content: None,
  2059. persisted_output_path: None,
  2060. persisted_output_size: None,
  2061. sandbox_status: None,
  2062. });
  2063. }
  2064. if started.elapsed() >= Duration::from_millis(timeout_ms) {
  2065. let _ = child.kill();
  2066. let output = child.wait_with_output()?;
  2067. let stderr = String::from_utf8_lossy(&output.stderr).into_owned();
  2068. let stderr = if stderr.trim().is_empty() {
  2069. format!("Command exceeded timeout of {timeout_ms} ms")
  2070. } else {
  2071. format!(
  2072. "{}
  2073. Command exceeded timeout of {timeout_ms} ms",
  2074. stderr.trim_end()
  2075. )
  2076. };
  2077. return Ok(runtime::BashCommandOutput {
  2078. stdout: String::from_utf8_lossy(&output.stdout).into_owned(),
  2079. stderr,
  2080. raw_output_path: None,
  2081. interrupted: true,
  2082. is_image: None,
  2083. background_task_id: None,
  2084. backgrounded_by_user: None,
  2085. assistant_auto_backgrounded: None,
  2086. dangerously_disable_sandbox: None,
  2087. return_code_interpretation: Some(String::from("timeout")),
  2088. no_output_expected: Some(false),
  2089. structured_content: None,
  2090. persisted_output_path: None,
  2091. persisted_output_size: None,
  2092. sandbox_status: None,
  2093. });
  2094. }
  2095. std::thread::sleep(Duration::from_millis(10));
  2096. }
  2097. }
  2098. let output = process.output()?;
  2099. Ok(runtime::BashCommandOutput {
  2100. stdout: String::from_utf8_lossy(&output.stdout).into_owned(),
  2101. stderr: String::from_utf8_lossy(&output.stderr).into_owned(),
  2102. raw_output_path: None,
  2103. interrupted: false,
  2104. is_image: None,
  2105. background_task_id: None,
  2106. backgrounded_by_user: None,
  2107. assistant_auto_backgrounded: None,
  2108. dangerously_disable_sandbox: None,
  2109. return_code_interpretation: output
  2110. .status
  2111. .code()
  2112. .filter(|code| *code != 0)
  2113. .map(|code| format!("exit_code:{code}")),
  2114. no_output_expected: Some(output.stdout.is_empty() && output.stderr.is_empty()),
  2115. structured_content: None,
  2116. persisted_output_path: None,
  2117. persisted_output_size: None,
  2118. sandbox_status: None,
  2119. })
  2120. }
  2121. fn resolve_cell_index(
  2122. cells: &[serde_json::Value],
  2123. cell_id: Option<&str>,
  2124. edit_mode: NotebookEditMode,
  2125. ) -> Result<usize, String> {
  2126. if cells.is_empty()
  2127. && matches!(
  2128. edit_mode,
  2129. NotebookEditMode::Replace | NotebookEditMode::Delete
  2130. )
  2131. {
  2132. return Err(String::from("Notebook has no cells to edit"));
  2133. }
  2134. if let Some(cell_id) = cell_id {
  2135. cells
  2136. .iter()
  2137. .position(|cell| cell.get("id").and_then(serde_json::Value::as_str) == Some(cell_id))
  2138. .ok_or_else(|| format!("Cell id not found: {cell_id}"))
  2139. } else {
  2140. Ok(cells.len().saturating_sub(1))
  2141. }
  2142. }
  2143. fn source_lines(source: &str) -> Vec<serde_json::Value> {
  2144. if source.is_empty() {
  2145. return vec![serde_json::Value::String(String::new())];
  2146. }
  2147. source
  2148. .split_inclusive('\n')
  2149. .map(|line| serde_json::Value::String(line.to_string()))
  2150. .collect()
  2151. }
  2152. fn format_notebook_edit_mode(mode: NotebookEditMode) -> String {
  2153. match mode {
  2154. NotebookEditMode::Replace => String::from("replace"),
  2155. NotebookEditMode::Insert => String::from("insert"),
  2156. NotebookEditMode::Delete => String::from("delete"),
  2157. }
  2158. }
  2159. fn make_cell_id(index: usize) -> String {
  2160. format!("cell-{}", index + 1)
  2161. }
  2162. fn parse_skill_description(contents: &str) -> Option<String> {
  2163. for line in contents.lines() {
  2164. if let Some(value) = line.strip_prefix("description:") {
  2165. let trimmed = value.trim();
  2166. if !trimmed.is_empty() {
  2167. return Some(trimmed.to_string());
  2168. }
  2169. }
  2170. }
  2171. None
  2172. }
  2173. #[cfg(test)]
  2174. mod tests {
  2175. use std::fs;
  2176. use std::io::{Read, Write};
  2177. use std::net::{SocketAddr, TcpListener};
  2178. use std::path::PathBuf;
  2179. use std::sync::{Arc, Mutex, OnceLock};
  2180. use std::thread;
  2181. use std::time::Duration;
  2182. use super::{execute_tool, mvp_tool_specs};
  2183. use serde_json::json;
  2184. fn env_lock() -> &'static Mutex<()> {
  2185. static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
  2186. LOCK.get_or_init(|| Mutex::new(()))
  2187. }
  2188. fn temp_path(name: &str) -> PathBuf {
  2189. let unique = std::time::SystemTime::now()
  2190. .duration_since(std::time::UNIX_EPOCH)
  2191. .expect("time")
  2192. .as_nanos();
  2193. std::env::temp_dir().join(format!("clawd-tools-{unique}-{name}"))
  2194. }
  2195. #[test]
  2196. fn exposes_mvp_tools() {
  2197. let names = mvp_tool_specs()
  2198. .into_iter()
  2199. .map(|spec| spec.name)
  2200. .collect::<Vec<_>>();
  2201. assert!(names.contains(&"bash"));
  2202. assert!(names.contains(&"read_file"));
  2203. assert!(names.contains(&"WebFetch"));
  2204. assert!(names.contains(&"WebSearch"));
  2205. assert!(names.contains(&"TodoWrite"));
  2206. assert!(names.contains(&"Skill"));
  2207. assert!(names.contains(&"Agent"));
  2208. assert!(names.contains(&"ToolSearch"));
  2209. assert!(names.contains(&"NotebookEdit"));
  2210. assert!(names.contains(&"Sleep"));
  2211. assert!(names.contains(&"SendUserMessage"));
  2212. assert!(names.contains(&"Config"));
  2213. assert!(names.contains(&"StructuredOutput"));
  2214. assert!(names.contains(&"REPL"));
  2215. assert!(names.contains(&"PowerShell"));
  2216. }
  2217. #[test]
  2218. fn rejects_unknown_tool_names() {
  2219. let error = execute_tool("nope", &json!({})).expect_err("tool should be rejected");
  2220. assert!(error.contains("unsupported tool"));
  2221. }
  2222. #[test]
  2223. fn web_fetch_returns_prompt_aware_summary() {
  2224. let server = TestServer::spawn(Arc::new(|request_line: &str| {
  2225. assert!(request_line.starts_with("GET /page "));
  2226. HttpResponse::html(
  2227. 200,
  2228. "OK",
  2229. "<html><head><title>Ignored</title></head><body><h1>Test Page</h1><p>Hello <b>world</b> from local server.</p></body></html>",
  2230. )
  2231. }));
  2232. let result = execute_tool(
  2233. "WebFetch",
  2234. &json!({
  2235. "url": format!("http://{}/page", server.addr()),
  2236. "prompt": "Summarize this page"
  2237. }),
  2238. )
  2239. .expect("WebFetch should succeed");
  2240. let output: serde_json::Value = serde_json::from_str(&result).expect("valid json");
  2241. assert_eq!(output["code"], 200);
  2242. let summary = output["result"].as_str().expect("result string");
  2243. assert!(summary.contains("Fetched"));
  2244. assert!(summary.contains("Test Page"));
  2245. assert!(summary.contains("Hello world from local server"));
  2246. let titled = execute_tool(
  2247. "WebFetch",
  2248. &json!({
  2249. "url": format!("http://{}/page", server.addr()),
  2250. "prompt": "What is the page title?"
  2251. }),
  2252. )
  2253. .expect("WebFetch title query should succeed");
  2254. let titled_output: serde_json::Value = serde_json::from_str(&titled).expect("valid json");
  2255. let titled_summary = titled_output["result"].as_str().expect("result string");
  2256. assert!(titled_summary.contains("Title: Ignored"));
  2257. }
  2258. #[test]
  2259. fn web_fetch_supports_plain_text_and_rejects_invalid_url() {
  2260. let server = TestServer::spawn(Arc::new(|request_line: &str| {
  2261. assert!(request_line.starts_with("GET /plain "));
  2262. HttpResponse::text(200, "OK", "plain text response")
  2263. }));
  2264. let result = execute_tool(
  2265. "WebFetch",
  2266. &json!({
  2267. "url": format!("http://{}/plain", server.addr()),
  2268. "prompt": "Show me the content"
  2269. }),
  2270. )
  2271. .expect("WebFetch should succeed for text content");
  2272. let output: serde_json::Value = serde_json::from_str(&result).expect("valid json");
  2273. assert_eq!(output["url"], format!("http://{}/plain", server.addr()));
  2274. assert!(output["result"]
  2275. .as_str()
  2276. .expect("result")
  2277. .contains("plain text response"));
  2278. let error = execute_tool(
  2279. "WebFetch",
  2280. &json!({
  2281. "url": "not a url",
  2282. "prompt": "Summarize"
  2283. }),
  2284. )
  2285. .expect_err("invalid URL should fail");
  2286. assert!(error.contains("relative URL without a base") || error.contains("invalid"));
  2287. }
  2288. #[test]
  2289. fn web_search_extracts_and_filters_results() {
  2290. let server = TestServer::spawn(Arc::new(|request_line: &str| {
  2291. assert!(request_line.contains("GET /search?q=rust+web+search "));
  2292. HttpResponse::html(
  2293. 200,
  2294. "OK",
  2295. r#"
  2296. <html><body>
  2297. <a class="result__a" href="https://docs.rs/reqwest">Reqwest docs</a>
  2298. <a class="result__a" href="https://example.com/blocked">Blocked result</a>
  2299. </body></html>
  2300. "#,
  2301. )
  2302. }));
  2303. std::env::set_var(
  2304. "CLAWD_WEB_SEARCH_BASE_URL",
  2305. format!("http://{}/search", server.addr()),
  2306. );
  2307. let result = execute_tool(
  2308. "WebSearch",
  2309. &json!({
  2310. "query": "rust web search",
  2311. "allowed_domains": ["https://DOCS.rs/"],
  2312. "blocked_domains": ["HTTPS://EXAMPLE.COM"]
  2313. }),
  2314. )
  2315. .expect("WebSearch should succeed");
  2316. std::env::remove_var("CLAWD_WEB_SEARCH_BASE_URL");
  2317. let output: serde_json::Value = serde_json::from_str(&result).expect("valid json");
  2318. assert_eq!(output["query"], "rust web search");
  2319. let results = output["results"].as_array().expect("results array");
  2320. let search_result = results
  2321. .iter()
  2322. .find(|item| item.get("content").is_some())
  2323. .expect("search result block present");
  2324. let content = search_result["content"].as_array().expect("content array");
  2325. assert_eq!(content.len(), 1);
  2326. assert_eq!(content[0]["title"], "Reqwest docs");
  2327. assert_eq!(content[0]["url"], "https://docs.rs/reqwest");
  2328. }
  2329. #[test]
  2330. fn web_search_handles_generic_links_and_invalid_base_url() {
  2331. let _guard = env_lock()
  2332. .lock()
  2333. .unwrap_or_else(std::sync::PoisonError::into_inner);
  2334. let server = TestServer::spawn(Arc::new(|request_line: &str| {
  2335. assert!(request_line.contains("GET /fallback?q=generic+links "));
  2336. HttpResponse::html(
  2337. 200,
  2338. "OK",
  2339. r#"
  2340. <html><body>
  2341. <a href="https://example.com/one">Example One</a>
  2342. <a href="https://example.com/one">Duplicate Example One</a>
  2343. <a href="https://docs.rs/tokio">Tokio Docs</a>
  2344. </body></html>
  2345. "#,
  2346. )
  2347. }));
  2348. std::env::set_var(
  2349. "CLAWD_WEB_SEARCH_BASE_URL",
  2350. format!("http://{}/fallback", server.addr()),
  2351. );
  2352. let result = execute_tool(
  2353. "WebSearch",
  2354. &json!({
  2355. "query": "generic links"
  2356. }),
  2357. )
  2358. .expect("WebSearch fallback parsing should succeed");
  2359. std::env::remove_var("CLAWD_WEB_SEARCH_BASE_URL");
  2360. let output: serde_json::Value = serde_json::from_str(&result).expect("valid json");
  2361. let results = output["results"].as_array().expect("results array");
  2362. let search_result = results
  2363. .iter()
  2364. .find(|item| item.get("content").is_some())
  2365. .expect("search result block present");
  2366. let content = search_result["content"].as_array().expect("content array");
  2367. assert_eq!(content.len(), 2);
  2368. assert_eq!(content[0]["url"], "https://example.com/one");
  2369. assert_eq!(content[1]["url"], "https://docs.rs/tokio");
  2370. std::env::set_var("CLAWD_WEB_SEARCH_BASE_URL", "://bad-base-url");
  2371. let error = execute_tool("WebSearch", &json!({ "query": "generic links" }))
  2372. .expect_err("invalid base URL should fail");
  2373. std::env::remove_var("CLAWD_WEB_SEARCH_BASE_URL");
  2374. assert!(error.contains("relative URL without a base") || error.contains("empty host"));
  2375. }
  2376. #[test]
  2377. fn todo_write_persists_and_returns_previous_state() {
  2378. let _guard = env_lock()
  2379. .lock()
  2380. .unwrap_or_else(std::sync::PoisonError::into_inner);
  2381. let path = temp_path("todos.json");
  2382. std::env::set_var("CLAWD_TODO_STORE", &path);
  2383. let first = execute_tool(
  2384. "TodoWrite",
  2385. &json!({
  2386. "todos": [
  2387. {"content": "Add tool", "activeForm": "Adding tool", "status": "in_progress"},
  2388. {"content": "Run tests", "activeForm": "Running tests", "status": "pending"}
  2389. ]
  2390. }),
  2391. )
  2392. .expect("TodoWrite should succeed");
  2393. let first_output: serde_json::Value = serde_json::from_str(&first).expect("valid json");
  2394. assert_eq!(first_output["oldTodos"].as_array().expect("array").len(), 0);
  2395. let second = execute_tool(
  2396. "TodoWrite",
  2397. &json!({
  2398. "todos": [
  2399. {"content": "Add tool", "activeForm": "Adding tool", "status": "completed"},
  2400. {"content": "Run tests", "activeForm": "Running tests", "status": "completed"},
  2401. {"content": "Verify", "activeForm": "Verifying", "status": "completed"}
  2402. ]
  2403. }),
  2404. )
  2405. .expect("TodoWrite should succeed");
  2406. std::env::remove_var("CLAWD_TODO_STORE");
  2407. let _ = std::fs::remove_file(path);
  2408. let second_output: serde_json::Value = serde_json::from_str(&second).expect("valid json");
  2409. assert_eq!(
  2410. second_output["oldTodos"].as_array().expect("array").len(),
  2411. 2
  2412. );
  2413. assert_eq!(
  2414. second_output["newTodos"].as_array().expect("array").len(),
  2415. 3
  2416. );
  2417. assert!(second_output["verificationNudgeNeeded"].is_null());
  2418. }
  2419. #[test]
  2420. fn todo_write_rejects_invalid_payloads_and_sets_verification_nudge() {
  2421. let _guard = env_lock()
  2422. .lock()
  2423. .unwrap_or_else(std::sync::PoisonError::into_inner);
  2424. let path = temp_path("todos-errors.json");
  2425. std::env::set_var("CLAWD_TODO_STORE", &path);
  2426. let empty = execute_tool("TodoWrite", &json!({ "todos": [] }))
  2427. .expect_err("empty todos should fail");
  2428. assert!(empty.contains("todos must not be empty"));
  2429. let too_many_active = execute_tool(
  2430. "TodoWrite",
  2431. &json!({
  2432. "todos": [
  2433. {"content": "One", "activeForm": "Doing one", "status": "in_progress"},
  2434. {"content": "Two", "activeForm": "Doing two", "status": "in_progress"}
  2435. ]
  2436. }),
  2437. )
  2438. .expect_err("multiple in-progress todos should fail");
  2439. assert!(too_many_active.contains("zero or one todo items may be in_progress"));
  2440. let blank_content = execute_tool(
  2441. "TodoWrite",
  2442. &json!({
  2443. "todos": [
  2444. {"content": " ", "activeForm": "Doing it", "status": "pending"}
  2445. ]
  2446. }),
  2447. )
  2448. .expect_err("blank content should fail");
  2449. assert!(blank_content.contains("todo content must not be empty"));
  2450. let nudge = execute_tool(
  2451. "TodoWrite",
  2452. &json!({
  2453. "todos": [
  2454. {"content": "Write tests", "activeForm": "Writing tests", "status": "completed"},
  2455. {"content": "Fix errors", "activeForm": "Fixing errors", "status": "completed"},
  2456. {"content": "Ship branch", "activeForm": "Shipping branch", "status": "completed"}
  2457. ]
  2458. }),
  2459. )
  2460. .expect("completed todos should succeed");
  2461. std::env::remove_var("CLAWD_TODO_STORE");
  2462. let _ = fs::remove_file(path);
  2463. let output: serde_json::Value = serde_json::from_str(&nudge).expect("valid json");
  2464. assert_eq!(output["verificationNudgeNeeded"], true);
  2465. }
  2466. #[test]
  2467. fn skill_loads_local_skill_prompt() {
  2468. let result = execute_tool(
  2469. "Skill",
  2470. &json!({
  2471. "skill": "help",
  2472. "args": "overview"
  2473. }),
  2474. )
  2475. .expect("Skill should succeed");
  2476. let output: serde_json::Value = serde_json::from_str(&result).expect("valid json");
  2477. assert_eq!(output["skill"], "help");
  2478. assert!(output["path"]
  2479. .as_str()
  2480. .expect("path")
  2481. .ends_with("/help/SKILL.md"));
  2482. assert!(output["prompt"]
  2483. .as_str()
  2484. .expect("prompt")
  2485. .contains("Guide on using oh-my-codex plugin"));
  2486. let dollar_result = execute_tool(
  2487. "Skill",
  2488. &json!({
  2489. "skill": "$help"
  2490. }),
  2491. )
  2492. .expect("Skill should accept $skill invocation form");
  2493. let dollar_output: serde_json::Value =
  2494. serde_json::from_str(&dollar_result).expect("valid json");
  2495. assert_eq!(dollar_output["skill"], "$help");
  2496. assert!(dollar_output["path"]
  2497. .as_str()
  2498. .expect("path")
  2499. .ends_with("/help/SKILL.md"));
  2500. }
  2501. #[test]
  2502. fn tool_search_supports_keyword_and_select_queries() {
  2503. let keyword = execute_tool(
  2504. "ToolSearch",
  2505. &json!({"query": "web current", "max_results": 3}),
  2506. )
  2507. .expect("ToolSearch should succeed");
  2508. let keyword_output: serde_json::Value = serde_json::from_str(&keyword).expect("valid json");
  2509. let matches = keyword_output["matches"].as_array().expect("matches");
  2510. assert!(matches.iter().any(|value| value == "WebSearch"));
  2511. let selected = execute_tool("ToolSearch", &json!({"query": "select:Agent,Skill"}))
  2512. .expect("ToolSearch should succeed");
  2513. let selected_output: serde_json::Value =
  2514. serde_json::from_str(&selected).expect("valid json");
  2515. assert_eq!(selected_output["matches"][0], "Agent");
  2516. assert_eq!(selected_output["matches"][1], "Skill");
  2517. let aliased = execute_tool("ToolSearch", &json!({"query": "AgentTool"}))
  2518. .expect("ToolSearch should support tool aliases");
  2519. let aliased_output: serde_json::Value = serde_json::from_str(&aliased).expect("valid json");
  2520. assert_eq!(aliased_output["matches"][0], "Agent");
  2521. assert_eq!(aliased_output["normalized_query"], "agent");
  2522. let selected_with_alias =
  2523. execute_tool("ToolSearch", &json!({"query": "select:AgentTool,Skill"}))
  2524. .expect("ToolSearch alias select should succeed");
  2525. let selected_with_alias_output: serde_json::Value =
  2526. serde_json::from_str(&selected_with_alias).expect("valid json");
  2527. assert_eq!(selected_with_alias_output["matches"][0], "Agent");
  2528. assert_eq!(selected_with_alias_output["matches"][1], "Skill");
  2529. }
  2530. #[test]
  2531. fn agent_persists_handoff_metadata() {
  2532. let _guard = env_lock()
  2533. .lock()
  2534. .unwrap_or_else(std::sync::PoisonError::into_inner);
  2535. let dir = temp_path("agent-store");
  2536. std::env::set_var("CLAWD_AGENT_STORE", &dir);
  2537. let result = execute_tool(
  2538. "Agent",
  2539. &json!({
  2540. "description": "Audit the branch",
  2541. "prompt": "Check tests and outstanding work.",
  2542. "subagent_type": "Explore",
  2543. "name": "ship-audit"
  2544. }),
  2545. )
  2546. .expect("Agent should succeed");
  2547. std::env::remove_var("CLAWD_AGENT_STORE");
  2548. let output: serde_json::Value = serde_json::from_str(&result).expect("valid json");
  2549. assert_eq!(output["name"], "ship-audit");
  2550. assert_eq!(output["subagentType"], "Explore");
  2551. assert_eq!(output["status"], "queued");
  2552. assert!(output["createdAt"].as_str().is_some());
  2553. let manifest_file = output["manifestFile"].as_str().expect("manifest file");
  2554. let output_file = output["outputFile"].as_str().expect("output file");
  2555. let contents = std::fs::read_to_string(output_file).expect("agent file exists");
  2556. let manifest_contents =
  2557. std::fs::read_to_string(manifest_file).expect("manifest file exists");
  2558. assert!(contents.contains("Audit the branch"));
  2559. assert!(contents.contains("Check tests and outstanding work."));
  2560. assert!(manifest_contents.contains("\"subagentType\": \"Explore\""));
  2561. let normalized = execute_tool(
  2562. "Agent",
  2563. &json!({
  2564. "description": "Verify the branch",
  2565. "prompt": "Check tests.",
  2566. "subagent_type": "explorer"
  2567. }),
  2568. )
  2569. .expect("Agent should normalize built-in aliases");
  2570. let normalized_output: serde_json::Value =
  2571. serde_json::from_str(&normalized).expect("valid json");
  2572. assert_eq!(normalized_output["subagentType"], "Explore");
  2573. let named = execute_tool(
  2574. "Agent",
  2575. &json!({
  2576. "description": "Review the branch",
  2577. "prompt": "Inspect diff.",
  2578. "name": "Ship Audit!!!"
  2579. }),
  2580. )
  2581. .expect("Agent should normalize explicit names");
  2582. let named_output: serde_json::Value = serde_json::from_str(&named).expect("valid json");
  2583. assert_eq!(named_output["name"], "ship-audit");
  2584. let _ = std::fs::remove_dir_all(dir);
  2585. }
  2586. #[test]
  2587. fn agent_rejects_blank_required_fields() {
  2588. let missing_description = execute_tool(
  2589. "Agent",
  2590. &json!({
  2591. "description": " ",
  2592. "prompt": "Inspect"
  2593. }),
  2594. )
  2595. .expect_err("blank description should fail");
  2596. assert!(missing_description.contains("description must not be empty"));
  2597. let missing_prompt = execute_tool(
  2598. "Agent",
  2599. &json!({
  2600. "description": "Inspect branch",
  2601. "prompt": " "
  2602. }),
  2603. )
  2604. .expect_err("blank prompt should fail");
  2605. assert!(missing_prompt.contains("prompt must not be empty"));
  2606. }
  2607. #[test]
  2608. fn notebook_edit_replaces_inserts_and_deletes_cells() {
  2609. let path = temp_path("notebook.ipynb");
  2610. std::fs::write(
  2611. &path,
  2612. r#"{
  2613. "cells": [
  2614. {"cell_type": "code", "id": "cell-a", "metadata": {}, "source": ["print(1)\n"], "outputs": [], "execution_count": null}
  2615. ],
  2616. "metadata": {"kernelspec": {"language": "python"}},
  2617. "nbformat": 4,
  2618. "nbformat_minor": 5
  2619. }"#,
  2620. )
  2621. .expect("write notebook");
  2622. let replaced = execute_tool(
  2623. "NotebookEdit",
  2624. &json!({
  2625. "notebook_path": path.display().to_string(),
  2626. "cell_id": "cell-a",
  2627. "new_source": "print(2)\n",
  2628. "edit_mode": "replace"
  2629. }),
  2630. )
  2631. .expect("NotebookEdit replace should succeed");
  2632. let replaced_output: serde_json::Value = serde_json::from_str(&replaced).expect("json");
  2633. assert_eq!(replaced_output["cell_id"], "cell-a");
  2634. assert_eq!(replaced_output["cell_type"], "code");
  2635. let inserted = execute_tool(
  2636. "NotebookEdit",
  2637. &json!({
  2638. "notebook_path": path.display().to_string(),
  2639. "cell_id": "cell-a",
  2640. "new_source": "# heading\n",
  2641. "cell_type": "markdown",
  2642. "edit_mode": "insert"
  2643. }),
  2644. )
  2645. .expect("NotebookEdit insert should succeed");
  2646. let inserted_output: serde_json::Value = serde_json::from_str(&inserted).expect("json");
  2647. assert_eq!(inserted_output["cell_type"], "markdown");
  2648. let appended = execute_tool(
  2649. "NotebookEdit",
  2650. &json!({
  2651. "notebook_path": path.display().to_string(),
  2652. "new_source": "print(3)\n",
  2653. "edit_mode": "insert"
  2654. }),
  2655. )
  2656. .expect("NotebookEdit append should succeed");
  2657. let appended_output: serde_json::Value = serde_json::from_str(&appended).expect("json");
  2658. assert_eq!(appended_output["cell_type"], "code");
  2659. let deleted = execute_tool(
  2660. "NotebookEdit",
  2661. &json!({
  2662. "notebook_path": path.display().to_string(),
  2663. "cell_id": "cell-a",
  2664. "edit_mode": "delete"
  2665. }),
  2666. )
  2667. .expect("NotebookEdit delete should succeed without new_source");
  2668. let deleted_output: serde_json::Value = serde_json::from_str(&deleted).expect("json");
  2669. assert!(deleted_output["cell_type"].is_null());
  2670. assert_eq!(deleted_output["new_source"], "");
  2671. let final_notebook: serde_json::Value =
  2672. serde_json::from_str(&std::fs::read_to_string(&path).expect("read notebook"))
  2673. .expect("valid notebook json");
  2674. let cells = final_notebook["cells"].as_array().expect("cells array");
  2675. assert_eq!(cells.len(), 2);
  2676. assert_eq!(cells[0]["cell_type"], "markdown");
  2677. assert!(cells[0].get("outputs").is_none());
  2678. assert_eq!(cells[1]["cell_type"], "code");
  2679. assert_eq!(cells[1]["source"][0], "print(3)\n");
  2680. let _ = std::fs::remove_file(path);
  2681. }
  2682. #[test]
  2683. fn notebook_edit_rejects_invalid_inputs() {
  2684. let text_path = temp_path("notebook.txt");
  2685. fs::write(&text_path, "not a notebook").expect("write text file");
  2686. let wrong_extension = execute_tool(
  2687. "NotebookEdit",
  2688. &json!({
  2689. "notebook_path": text_path.display().to_string(),
  2690. "new_source": "print(1)\n"
  2691. }),
  2692. )
  2693. .expect_err("non-ipynb file should fail");
  2694. assert!(wrong_extension.contains("Jupyter notebook"));
  2695. let _ = fs::remove_file(&text_path);
  2696. let empty_notebook = temp_path("empty.ipynb");
  2697. fs::write(
  2698. &empty_notebook,
  2699. r#"{"cells":[],"metadata":{"kernelspec":{"language":"python"}},"nbformat":4,"nbformat_minor":5}"#,
  2700. )
  2701. .expect("write empty notebook");
  2702. let missing_source = execute_tool(
  2703. "NotebookEdit",
  2704. &json!({
  2705. "notebook_path": empty_notebook.display().to_string(),
  2706. "edit_mode": "insert"
  2707. }),
  2708. )
  2709. .expect_err("insert without source should fail");
  2710. assert!(missing_source.contains("new_source is required"));
  2711. let missing_cell = execute_tool(
  2712. "NotebookEdit",
  2713. &json!({
  2714. "notebook_path": empty_notebook.display().to_string(),
  2715. "edit_mode": "delete"
  2716. }),
  2717. )
  2718. .expect_err("delete on empty notebook should fail");
  2719. assert!(missing_cell.contains("Notebook has no cells to edit"));
  2720. let _ = fs::remove_file(empty_notebook);
  2721. }
  2722. #[test]
  2723. fn bash_tool_reports_success_exit_failure_timeout_and_background() {
  2724. let success = execute_tool("bash", &json!({ "command": "printf 'hello'" }))
  2725. .expect("bash should succeed");
  2726. let success_output: serde_json::Value = serde_json::from_str(&success).expect("json");
  2727. assert_eq!(success_output["stdout"], "hello");
  2728. assert_eq!(success_output["interrupted"], false);
  2729. let failure = execute_tool("bash", &json!({ "command": "printf 'oops' >&2; exit 7" }))
  2730. .expect("bash failure should still return structured output");
  2731. let failure_output: serde_json::Value = serde_json::from_str(&failure).expect("json");
  2732. assert_eq!(failure_output["returnCodeInterpretation"], "exit_code:7");
  2733. assert!(failure_output["stderr"]
  2734. .as_str()
  2735. .expect("stderr")
  2736. .contains("oops"));
  2737. let timeout = execute_tool("bash", &json!({ "command": "sleep 1", "timeout": 10 }))
  2738. .expect("bash timeout should return output");
  2739. let timeout_output: serde_json::Value = serde_json::from_str(&timeout).expect("json");
  2740. assert_eq!(timeout_output["interrupted"], true);
  2741. assert_eq!(timeout_output["returnCodeInterpretation"], "timeout");
  2742. assert!(timeout_output["stderr"]
  2743. .as_str()
  2744. .expect("stderr")
  2745. .contains("Command exceeded timeout"));
  2746. let background = execute_tool(
  2747. "bash",
  2748. &json!({ "command": "sleep 1", "run_in_background": true }),
  2749. )
  2750. .expect("bash background should succeed");
  2751. let background_output: serde_json::Value = serde_json::from_str(&background).expect("json");
  2752. assert!(background_output["backgroundTaskId"].as_str().is_some());
  2753. assert_eq!(background_output["noOutputExpected"], true);
  2754. }
  2755. #[test]
  2756. fn file_tools_cover_read_write_and_edit_behaviors() {
  2757. let _guard = env_lock()
  2758. .lock()
  2759. .unwrap_or_else(std::sync::PoisonError::into_inner);
  2760. let root = temp_path("fs-suite");
  2761. fs::create_dir_all(&root).expect("create root");
  2762. let original_dir = std::env::current_dir().expect("cwd");
  2763. std::env::set_current_dir(&root).expect("set cwd");
  2764. let write_create = execute_tool(
  2765. "write_file",
  2766. &json!({ "path": "nested/demo.txt", "content": "alpha\nbeta\nalpha\n" }),
  2767. )
  2768. .expect("write create should succeed");
  2769. let write_create_output: serde_json::Value =
  2770. serde_json::from_str(&write_create).expect("json");
  2771. assert_eq!(write_create_output["type"], "create");
  2772. assert!(root.join("nested/demo.txt").exists());
  2773. let write_update = execute_tool(
  2774. "write_file",
  2775. &json!({ "path": "nested/demo.txt", "content": "alpha\nbeta\ngamma\n" }),
  2776. )
  2777. .expect("write update should succeed");
  2778. let write_update_output: serde_json::Value =
  2779. serde_json::from_str(&write_update).expect("json");
  2780. assert_eq!(write_update_output["type"], "update");
  2781. assert_eq!(write_update_output["originalFile"], "alpha\nbeta\nalpha\n");
  2782. let read_full = execute_tool("read_file", &json!({ "path": "nested/demo.txt" }))
  2783. .expect("read full should succeed");
  2784. let read_full_output: serde_json::Value = serde_json::from_str(&read_full).expect("json");
  2785. assert_eq!(read_full_output["file"]["content"], "alpha\nbeta\ngamma");
  2786. assert_eq!(read_full_output["file"]["startLine"], 1);
  2787. let read_slice = execute_tool(
  2788. "read_file",
  2789. &json!({ "path": "nested/demo.txt", "offset": 1, "limit": 1 }),
  2790. )
  2791. .expect("read slice should succeed");
  2792. let read_slice_output: serde_json::Value = serde_json::from_str(&read_slice).expect("json");
  2793. assert_eq!(read_slice_output["file"]["content"], "beta");
  2794. assert_eq!(read_slice_output["file"]["startLine"], 2);
  2795. let read_past_end = execute_tool(
  2796. "read_file",
  2797. &json!({ "path": "nested/demo.txt", "offset": 50 }),
  2798. )
  2799. .expect("read past EOF should succeed");
  2800. let read_past_end_output: serde_json::Value =
  2801. serde_json::from_str(&read_past_end).expect("json");
  2802. assert_eq!(read_past_end_output["file"]["content"], "");
  2803. assert_eq!(read_past_end_output["file"]["startLine"], 4);
  2804. let read_error = execute_tool("read_file", &json!({ "path": "missing.txt" }))
  2805. .expect_err("missing file should fail");
  2806. assert!(!read_error.is_empty());
  2807. let edit_once = execute_tool(
  2808. "edit_file",
  2809. &json!({ "path": "nested/demo.txt", "old_string": "alpha", "new_string": "omega" }),
  2810. )
  2811. .expect("single edit should succeed");
  2812. let edit_once_output: serde_json::Value = serde_json::from_str(&edit_once).expect("json");
  2813. assert_eq!(edit_once_output["replaceAll"], false);
  2814. assert_eq!(
  2815. fs::read_to_string(root.join("nested/demo.txt")).expect("read file"),
  2816. "omega\nbeta\ngamma\n"
  2817. );
  2818. execute_tool(
  2819. "write_file",
  2820. &json!({ "path": "nested/demo.txt", "content": "alpha\nbeta\nalpha\n" }),
  2821. )
  2822. .expect("reset file");
  2823. let edit_all = execute_tool(
  2824. "edit_file",
  2825. &json!({
  2826. "path": "nested/demo.txt",
  2827. "old_string": "alpha",
  2828. "new_string": "omega",
  2829. "replace_all": true
  2830. }),
  2831. )
  2832. .expect("replace all should succeed");
  2833. let edit_all_output: serde_json::Value = serde_json::from_str(&edit_all).expect("json");
  2834. assert_eq!(edit_all_output["replaceAll"], true);
  2835. assert_eq!(
  2836. fs::read_to_string(root.join("nested/demo.txt")).expect("read file"),
  2837. "omega\nbeta\nomega\n"
  2838. );
  2839. let edit_same = execute_tool(
  2840. "edit_file",
  2841. &json!({ "path": "nested/demo.txt", "old_string": "omega", "new_string": "omega" }),
  2842. )
  2843. .expect_err("identical old/new should fail");
  2844. assert!(edit_same.contains("must differ"));
  2845. let edit_missing = execute_tool(
  2846. "edit_file",
  2847. &json!({ "path": "nested/demo.txt", "old_string": "missing", "new_string": "omega" }),
  2848. )
  2849. .expect_err("missing substring should fail");
  2850. assert!(edit_missing.contains("old_string not found"));
  2851. std::env::set_current_dir(&original_dir).expect("restore cwd");
  2852. let _ = fs::remove_dir_all(root);
  2853. }
  2854. #[test]
  2855. fn glob_and_grep_tools_cover_success_and_errors() {
  2856. let _guard = env_lock()
  2857. .lock()
  2858. .unwrap_or_else(std::sync::PoisonError::into_inner);
  2859. let root = temp_path("search-suite");
  2860. fs::create_dir_all(root.join("nested")).expect("create root");
  2861. let original_dir = std::env::current_dir().expect("cwd");
  2862. std::env::set_current_dir(&root).expect("set cwd");
  2863. fs::write(
  2864. root.join("nested/lib.rs"),
  2865. "fn main() {}\nlet alpha = 1;\nlet alpha = 2;\n",
  2866. )
  2867. .expect("write rust file");
  2868. fs::write(root.join("nested/notes.txt"), "alpha\nbeta\n").expect("write txt file");
  2869. let globbed = execute_tool("glob_search", &json!({ "pattern": "nested/*.rs" }))
  2870. .expect("glob should succeed");
  2871. let globbed_output: serde_json::Value = serde_json::from_str(&globbed).expect("json");
  2872. assert_eq!(globbed_output["numFiles"], 1);
  2873. assert!(globbed_output["filenames"][0]
  2874. .as_str()
  2875. .expect("filename")
  2876. .ends_with("nested/lib.rs"));
  2877. let glob_error = execute_tool("glob_search", &json!({ "pattern": "[" }))
  2878. .expect_err("invalid glob should fail");
  2879. assert!(!glob_error.is_empty());
  2880. let grep_content = execute_tool(
  2881. "grep_search",
  2882. &json!({
  2883. "pattern": "alpha",
  2884. "path": "nested",
  2885. "glob": "*.rs",
  2886. "output_mode": "content",
  2887. "-n": true,
  2888. "head_limit": 1,
  2889. "offset": 1
  2890. }),
  2891. )
  2892. .expect("grep content should succeed");
  2893. let grep_content_output: serde_json::Value =
  2894. serde_json::from_str(&grep_content).expect("json");
  2895. assert_eq!(grep_content_output["numFiles"], 0);
  2896. assert!(grep_content_output["appliedLimit"].is_null());
  2897. assert_eq!(grep_content_output["appliedOffset"], 1);
  2898. assert!(grep_content_output["content"]
  2899. .as_str()
  2900. .expect("content")
  2901. .contains("let alpha = 2;"));
  2902. let grep_count = execute_tool(
  2903. "grep_search",
  2904. &json!({ "pattern": "alpha", "path": "nested", "output_mode": "count" }),
  2905. )
  2906. .expect("grep count should succeed");
  2907. let grep_count_output: serde_json::Value = serde_json::from_str(&grep_count).expect("json");
  2908. assert_eq!(grep_count_output["numMatches"], 3);
  2909. let grep_error = execute_tool(
  2910. "grep_search",
  2911. &json!({ "pattern": "(alpha", "path": "nested" }),
  2912. )
  2913. .expect_err("invalid regex should fail");
  2914. assert!(!grep_error.is_empty());
  2915. std::env::set_current_dir(&original_dir).expect("restore cwd");
  2916. let _ = fs::remove_dir_all(root);
  2917. }
  2918. #[test]
  2919. fn sleep_waits_and_reports_duration() {
  2920. let started = std::time::Instant::now();
  2921. let result =
  2922. execute_tool("Sleep", &json!({"duration_ms": 20})).expect("Sleep should succeed");
  2923. let elapsed = started.elapsed();
  2924. let output: serde_json::Value = serde_json::from_str(&result).expect("json");
  2925. assert_eq!(output["duration_ms"], 20);
  2926. assert!(output["message"]
  2927. .as_str()
  2928. .expect("message")
  2929. .contains("Slept for 20ms"));
  2930. assert!(elapsed >= Duration::from_millis(15));
  2931. }
  2932. #[test]
  2933. fn brief_returns_sent_message_and_attachment_metadata() {
  2934. let attachment = std::env::temp_dir().join(format!(
  2935. "clawd-brief-{}.png",
  2936. std::time::SystemTime::now()
  2937. .duration_since(std::time::UNIX_EPOCH)
  2938. .expect("time")
  2939. .as_nanos()
  2940. ));
  2941. std::fs::write(&attachment, b"png-data").expect("write attachment");
  2942. let result = execute_tool(
  2943. "SendUserMessage",
  2944. &json!({
  2945. "message": "hello user",
  2946. "attachments": [attachment.display().to_string()],
  2947. "status": "normal"
  2948. }),
  2949. )
  2950. .expect("SendUserMessage should succeed");
  2951. let output: serde_json::Value = serde_json::from_str(&result).expect("json");
  2952. assert_eq!(output["message"], "hello user");
  2953. assert!(output["sentAt"].as_str().is_some());
  2954. assert_eq!(output["attachments"][0]["isImage"], true);
  2955. let _ = std::fs::remove_file(attachment);
  2956. }
  2957. #[test]
  2958. fn config_reads_and_writes_supported_values() {
  2959. let _guard = env_lock()
  2960. .lock()
  2961. .unwrap_or_else(std::sync::PoisonError::into_inner);
  2962. let root = std::env::temp_dir().join(format!(
  2963. "clawd-config-{}",
  2964. std::time::SystemTime::now()
  2965. .duration_since(std::time::UNIX_EPOCH)
  2966. .expect("time")
  2967. .as_nanos()
  2968. ));
  2969. let home = root.join("home");
  2970. let cwd = root.join("cwd");
  2971. std::fs::create_dir_all(home.join(".claude")).expect("home dir");
  2972. std::fs::create_dir_all(cwd.join(".claude")).expect("cwd dir");
  2973. std::fs::write(
  2974. home.join(".claude").join("settings.json"),
  2975. r#"{"verbose":false}"#,
  2976. )
  2977. .expect("write global settings");
  2978. let original_home = std::env::var("HOME").ok();
  2979. let original_claude_home = std::env::var("CLAUDE_CONFIG_HOME").ok();
  2980. let original_dir = std::env::current_dir().expect("cwd");
  2981. std::env::set_var("HOME", &home);
  2982. std::env::remove_var("CLAUDE_CONFIG_HOME");
  2983. std::env::set_current_dir(&cwd).expect("set cwd");
  2984. let get = execute_tool("Config", &json!({"setting": "verbose"})).expect("get config");
  2985. let get_output: serde_json::Value = serde_json::from_str(&get).expect("json");
  2986. assert_eq!(get_output["value"], false);
  2987. let set = execute_tool(
  2988. "Config",
  2989. &json!({"setting": "permissions.defaultMode", "value": "plan"}),
  2990. )
  2991. .expect("set config");
  2992. let set_output: serde_json::Value = serde_json::from_str(&set).expect("json");
  2993. assert_eq!(set_output["operation"], "set");
  2994. assert_eq!(set_output["newValue"], "plan");
  2995. let invalid = execute_tool(
  2996. "Config",
  2997. &json!({"setting": "permissions.defaultMode", "value": "bogus"}),
  2998. )
  2999. .expect_err("invalid config value should error");
  3000. assert!(invalid.contains("Invalid value"));
  3001. let unknown =
  3002. execute_tool("Config", &json!({"setting": "nope"})).expect("unknown setting result");
  3003. let unknown_output: serde_json::Value = serde_json::from_str(&unknown).expect("json");
  3004. assert_eq!(unknown_output["success"], false);
  3005. std::env::set_current_dir(&original_dir).expect("restore cwd");
  3006. match original_home {
  3007. Some(value) => std::env::set_var("HOME", value),
  3008. None => std::env::remove_var("HOME"),
  3009. }
  3010. match original_claude_home {
  3011. Some(value) => std::env::set_var("CLAUDE_CONFIG_HOME", value),
  3012. None => std::env::remove_var("CLAUDE_CONFIG_HOME"),
  3013. }
  3014. let _ = std::fs::remove_dir_all(root);
  3015. }
  3016. #[test]
  3017. fn structured_output_echoes_input_payload() {
  3018. let result = execute_tool("StructuredOutput", &json!({"ok": true, "items": [1, 2, 3]}))
  3019. .expect("StructuredOutput should succeed");
  3020. let output: serde_json::Value = serde_json::from_str(&result).expect("json");
  3021. assert_eq!(output["data"], "Structured output provided successfully");
  3022. assert_eq!(output["structured_output"]["ok"], true);
  3023. assert_eq!(output["structured_output"]["items"][1], 2);
  3024. }
  3025. #[test]
  3026. fn repl_executes_python_code() {
  3027. let result = execute_tool(
  3028. "REPL",
  3029. &json!({"language": "python", "code": "print(1 + 1)", "timeout_ms": 500}),
  3030. )
  3031. .expect("REPL should succeed");
  3032. let output: serde_json::Value = serde_json::from_str(&result).expect("json");
  3033. assert_eq!(output["language"], "python");
  3034. assert_eq!(output["exitCode"], 0);
  3035. assert!(output["stdout"].as_str().expect("stdout").contains('2'));
  3036. }
  3037. #[test]
  3038. fn powershell_runs_via_stub_shell() {
  3039. let _guard = env_lock()
  3040. .lock()
  3041. .unwrap_or_else(std::sync::PoisonError::into_inner);
  3042. let dir = std::env::temp_dir().join(format!(
  3043. "clawd-pwsh-bin-{}",
  3044. std::time::SystemTime::now()
  3045. .duration_since(std::time::UNIX_EPOCH)
  3046. .expect("time")
  3047. .as_nanos()
  3048. ));
  3049. std::fs::create_dir_all(&dir).expect("create dir");
  3050. let script = dir.join("pwsh");
  3051. std::fs::write(
  3052. &script,
  3053. r#"#!/bin/sh
  3054. while [ "$1" != "-Command" ] && [ $# -gt 0 ]; do shift; done
  3055. shift
  3056. printf 'pwsh:%s' "$1"
  3057. "#,
  3058. )
  3059. .expect("write script");
  3060. std::process::Command::new("/bin/chmod")
  3061. .arg("+x")
  3062. .arg(&script)
  3063. .status()
  3064. .expect("chmod");
  3065. let original_path = std::env::var("PATH").unwrap_or_default();
  3066. std::env::set_var("PATH", format!("{}:{}", dir.display(), original_path));
  3067. let result = execute_tool(
  3068. "PowerShell",
  3069. &json!({"command": "Write-Output hello", "timeout": 1000}),
  3070. )
  3071. .expect("PowerShell should succeed");
  3072. let background = execute_tool(
  3073. "PowerShell",
  3074. &json!({"command": "Write-Output hello", "run_in_background": true}),
  3075. )
  3076. .expect("PowerShell background should succeed");
  3077. std::env::set_var("PATH", original_path);
  3078. let _ = std::fs::remove_dir_all(dir);
  3079. let output: serde_json::Value = serde_json::from_str(&result).expect("json");
  3080. assert_eq!(output["stdout"], "pwsh:Write-Output hello");
  3081. assert!(output["stderr"].as_str().expect("stderr").is_empty());
  3082. let background_output: serde_json::Value = serde_json::from_str(&background).expect("json");
  3083. assert!(background_output["backgroundTaskId"].as_str().is_some());
  3084. assert_eq!(background_output["backgroundedByUser"], true);
  3085. assert_eq!(background_output["assistantAutoBackgrounded"], false);
  3086. }
  3087. #[test]
  3088. fn powershell_errors_when_shell_is_missing() {
  3089. let _guard = env_lock()
  3090. .lock()
  3091. .unwrap_or_else(std::sync::PoisonError::into_inner);
  3092. let original_path = std::env::var("PATH").unwrap_or_default();
  3093. let empty_dir = std::env::temp_dir().join(format!(
  3094. "clawd-empty-bin-{}",
  3095. std::time::SystemTime::now()
  3096. .duration_since(std::time::UNIX_EPOCH)
  3097. .expect("time")
  3098. .as_nanos()
  3099. ));
  3100. std::fs::create_dir_all(&empty_dir).expect("create empty dir");
  3101. std::env::set_var("PATH", empty_dir.display().to_string());
  3102. let err = execute_tool("PowerShell", &json!({"command": "Write-Output hello"}))
  3103. .expect_err("PowerShell should fail when shell is missing");
  3104. std::env::set_var("PATH", original_path);
  3105. let _ = std::fs::remove_dir_all(empty_dir);
  3106. assert!(err.contains("PowerShell executable not found"));
  3107. }
  3108. struct TestServer {
  3109. addr: SocketAddr,
  3110. shutdown: Option<std::sync::mpsc::Sender<()>>,
  3111. handle: Option<thread::JoinHandle<()>>,
  3112. }
  3113. impl TestServer {
  3114. fn spawn(handler: Arc<dyn Fn(&str) -> HttpResponse + Send + Sync + 'static>) -> Self {
  3115. let listener = TcpListener::bind("127.0.0.1:0").expect("bind test server");
  3116. listener
  3117. .set_nonblocking(true)
  3118. .expect("set nonblocking listener");
  3119. let addr = listener.local_addr().expect("local addr");
  3120. let (tx, rx) = std::sync::mpsc::channel::<()>();
  3121. let handle = thread::spawn(move || loop {
  3122. if rx.try_recv().is_ok() {
  3123. break;
  3124. }
  3125. match listener.accept() {
  3126. Ok((mut stream, _)) => {
  3127. let mut buffer = [0_u8; 4096];
  3128. let size = stream.read(&mut buffer).expect("read request");
  3129. let request = String::from_utf8_lossy(&buffer[..size]).into_owned();
  3130. let request_line = request.lines().next().unwrap_or_default().to_string();
  3131. let response = handler(&request_line);
  3132. stream
  3133. .write_all(response.to_bytes().as_slice())
  3134. .expect("write response");
  3135. }
  3136. Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => {
  3137. thread::sleep(Duration::from_millis(10));
  3138. }
  3139. Err(error) => panic!("server accept failed: {error}"),
  3140. }
  3141. });
  3142. Self {
  3143. addr,
  3144. shutdown: Some(tx),
  3145. handle: Some(handle),
  3146. }
  3147. }
  3148. fn addr(&self) -> SocketAddr {
  3149. self.addr
  3150. }
  3151. }
  3152. impl Drop for TestServer {
  3153. fn drop(&mut self) {
  3154. if let Some(tx) = self.shutdown.take() {
  3155. let _ = tx.send(());
  3156. }
  3157. if let Some(handle) = self.handle.take() {
  3158. handle.join().expect("join test server");
  3159. }
  3160. }
  3161. }
  3162. struct HttpResponse {
  3163. status: u16,
  3164. reason: &'static str,
  3165. content_type: &'static str,
  3166. body: String,
  3167. }
  3168. impl HttpResponse {
  3169. fn html(status: u16, reason: &'static str, body: &str) -> Self {
  3170. Self {
  3171. status,
  3172. reason,
  3173. content_type: "text/html; charset=utf-8",
  3174. body: body.to_string(),
  3175. }
  3176. }
  3177. fn text(status: u16, reason: &'static str, body: &str) -> Self {
  3178. Self {
  3179. status,
  3180. reason,
  3181. content_type: "text/plain; charset=utf-8",
  3182. body: body.to_string(),
  3183. }
  3184. }
  3185. fn to_bytes(&self) -> Vec<u8> {
  3186. format!(
  3187. "HTTP/1.1 {} {}\r\nContent-Type: {}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
  3188. self.status,
  3189. self.reason,
  3190. self.content_type,
  3191. self.body.len(),
  3192. self.body
  3193. )
  3194. .into_bytes()
  3195. }
  3196. }
  3197. }