inline-tools.h 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849
  1. #pragma once
  2. #include <string>
  3. #include <vector>
  4. #include <functional>
  5. #include <map>
  6. #include <sstream>
  7. #include <cmath>
  8. #include <iostream>
  9. #include <fstream>
  10. #include <regex>
  11. #include <chrono>
  12. #include <ctime>
  13. #include <iomanip>
  14. #include <algorithm>
  15. #include "base64.hpp"
  16. #include <random>
  17. #include <cctype>
  18. #include <climits>
  19. // Inline tool callback function type
  20. // Args are passed as strings, return value is string
  21. using InlineToolCallback = std::function<std::string(const std::vector<std::string>&)>;
  22. struct InlineTool {
  23. std::string name;
  24. InlineToolCallback callback;
  25. int num_args;
  26. };
  27. class InlineToolManager {
  28. private:
  29. std::map<std::string, InlineTool> tools;
  30. public:
  31. InlineToolManager() {
  32. // Register default tools
  33. // Addition
  34. register_tool("add", [](const std::vector<std::string>& args) -> std::string {
  35. if (args.size() < 2) {
  36. return "ERROR: add() requires at least 2 arguments, got " + std::to_string(args.size()) + ". Usage: [[add(5,10,15){==} → 30";
  37. }
  38. try {
  39. double sum = 0.0;
  40. for (size_t i = 0; i < args.size(); i++) {
  41. try {
  42. sum += std::stod(args[i]);
  43. } catch (...) {
  44. return "ERROR: add() argument " + std::to_string(i+1) + " is not a valid number: '" + args[i] + "'. Usage: [[add(5,10){==} → 15";
  45. }
  46. }
  47. if (sum == std::floor(sum)) {
  48. return std::to_string(static_cast<long long>(sum));
  49. }
  50. return std::to_string(sum);
  51. } catch (...) {
  52. return "ERROR: add() failed. Usage: [[add(5,10,15){==} → 30";
  53. }
  54. }, -1); // Variable number of args
  55. // Subtraction
  56. register_tool("sub", [](const std::vector<std::string>& args) -> std::string {
  57. if (args.size() != 2) {
  58. return "ERROR: sub() requires exactly 2 arguments, got " + std::to_string(args.size()) + ". Usage: [[sub(10,3){==} → 7";
  59. }
  60. try {
  61. double a = std::stod(args[0]);
  62. double b = std::stod(args[1]);
  63. double result = a - b;
  64. if (result == std::floor(result)) {
  65. return std::to_string(static_cast<long long>(result));
  66. }
  67. return std::to_string(result);
  68. } catch (...) {
  69. return "ERROR: sub() arguments must be numbers. Got: '" + args[0] + "', '" + args[1] + "'. Usage: [[sub(10,3){==} → 7";
  70. }
  71. }, 2);
  72. // Division
  73. register_tool("div", [](const std::vector<std::string>& args) -> std::string {
  74. if (args.size() != 2) {
  75. return "ERROR: div() requires exactly 2 arguments, got " + std::to_string(args.size()) + ". Usage: [[div(10,2){==} → 5";
  76. }
  77. try {
  78. double a = std::stod(args[0]);
  79. double b = std::stod(args[1]);
  80. if (b == 0) {
  81. return "ERROR: Division by zero (" + args[0] + "/0). Cannot divide by zero. Usage: [[div(10,2){==} → 5";
  82. }
  83. double result = a / b;
  84. if (result == std::floor(result)) {
  85. return std::to_string(static_cast<long long>(result));
  86. }
  87. return std::to_string(result);
  88. } catch (...) {
  89. return "ERROR: div() arguments must be numbers. Got: '" + args[0] + "', '" + args[1] + "'. Usage: [[div(10,2){==} → 5";
  90. }
  91. }, 2);
  92. // Square Root
  93. register_tool("sqrt", [](const std::vector<std::string>& args) -> std::string {
  94. if (args.size() != 1) {
  95. return "ERROR: sqrt() requires exactly 1 argument, got " + std::to_string(args.size()) + ". Usage: [[sqrt(25){==} → 5";
  96. }
  97. try {
  98. double a = std::stod(args[0]);
  99. if (a < 0) {
  100. return "ERROR: sqrt() requires non-negative number, got " + args[0] + ". Cannot take square root of negative. Usage: [[sqrt(25){==} → 5";
  101. }
  102. double result = std::sqrt(a);
  103. if (result == std::floor(result)) {
  104. return std::to_string(static_cast<long long>(result));
  105. }
  106. return std::to_string(result);
  107. } catch (...) {
  108. return "ERROR: sqrt() argument must be a number. Got: '" + args[0] + "'. Usage: [[sqrt(25){==} → 5";
  109. }
  110. }, 1);
  111. // Power
  112. register_tool("pow", [](const std::vector<std::string>& args) -> std::string {
  113. if (args.size() != 2) {
  114. return "ERROR: pow() requires exactly 2 arguments (base, exponent), got " + std::to_string(args.size()) + ". Usage: [[pow(2,10){==} → 1024";
  115. }
  116. try {
  117. double base = std::stod(args[0]);
  118. double exp = std::stod(args[1]);
  119. double result = std::pow(base, exp);
  120. if (std::isnan(result) || std::isinf(result)) {
  121. return "ERROR: pow(" + args[0] + "," + args[1] + ") resulted in invalid value. Usage: [[pow(2,10){==} → 1024";
  122. }
  123. if (result == std::floor(result)) {
  124. return std::to_string(static_cast<long long>(result));
  125. }
  126. return std::to_string(result);
  127. } catch (...) {
  128. return "ERROR";
  129. }
  130. }, 2);
  131. // Linear equation solver: ax + b = c
  132. register_tool("solve_linear", [](const std::vector<std::string>& args) -> std::string {
  133. if (args.size() != 3) return "ERROR";
  134. try {
  135. double a = std::stod(args[0]);
  136. double b = std::stod(args[1]);
  137. double c = std::stod(args[2]);
  138. if (a == 0) return "ERROR: Not a linear equation";
  139. double x = (c - b) / a;
  140. if (x == std::floor(x)) {
  141. return std::to_string(static_cast<long long>(x));
  142. }
  143. return std::to_string(x);
  144. } catch (...) {
  145. return "ERROR";
  146. }
  147. }, 3);
  148. // Quadratic equation solver: ax² + bx + c = 0
  149. register_tool("solve_quadratic", [](const std::vector<std::string>& args) -> std::string {
  150. if (args.size() != 3) return "ERROR";
  151. try {
  152. double a = std::stod(args[0]);
  153. double b = std::stod(args[1]);
  154. double c = std::stod(args[2]);
  155. if (a == 0) return "ERROR: Not a quadratic equation";
  156. double discriminant = b*b - 4*a*c;
  157. if (discriminant < 0) return "ERROR: No real solutions";
  158. double sqrt_disc = std::sqrt(discriminant);
  159. double x1 = (-b + sqrt_disc) / (2*a);
  160. double x2 = (-b - sqrt_disc) / (2*a);
  161. std::string result = std::to_string(x1) + ", " + std::to_string(x2);
  162. return result;
  163. } catch (...) {
  164. return "ERROR";
  165. }
  166. }, 3);
  167. // Multiplication (existing)
  168. register_tool("multiply", [](const std::vector<std::string>& args) -> std::string {
  169. if (args.size() != 2) {
  170. return "ERROR: multiply() requires exactly 2 arguments, got " + std::to_string(args.size()) + ". Usage: [[multiply(7,8){==} → 56";
  171. }
  172. try {
  173. double a = std::stod(args[0]);
  174. double b = std::stod(args[1]);
  175. double result = a * b;
  176. if (std::isnan(result) || std::isinf(result)) {
  177. return "ERROR: multiply(" + args[0] + "," + args[1] + ") resulted in invalid value. Usage: [[multiply(7,8){==} → 56";
  178. }
  179. if (result == std::floor(result)) {
  180. return std::to_string(static_cast<long long>(result));
  181. }
  182. return std::to_string(result);
  183. } catch (...) {
  184. return "ERROR";
  185. }
  186. }, 2);
  187. // ===== INTEGER ARITHMETIC =====
  188. // Factorial
  189. register_tool("factorial", [](const std::vector<std::string>& args) -> std::string {
  190. if (args.size() != 1) {
  191. return "ERROR: factorial() requires exactly 1 argument, got " + std::to_string(args.size()) + ". Usage: [[factorial(5){==} → 120";
  192. }
  193. try {
  194. long long n = std::stoll(args[0]);
  195. if (n < 0) {
  196. return "ERROR: factorial() requires non-negative integer, got " + args[0] + ". Usage: [[factorial(5){==} → 120";
  197. }
  198. if (n > 20) {
  199. return "ERROR: factorial() input too large (>20), got " + args[0] + ". Would cause overflow. Usage: [[factorial(5){==} → 120";
  200. }
  201. long long result = 1;
  202. for (int i = 2; i <= n; i++) result *= i;
  203. return std::to_string(result);
  204. } catch (...) {
  205. return "ERROR: factorial() argument must be an integer. Got: '" + args[0] + "'. Usage: [[factorial(5){==} → 120";
  206. }
  207. }, 1);
  208. // GCD (variable args)
  209. register_tool("gcd", [](const std::vector<std::string>& args) -> std::string {
  210. if (args.size() < 2) {
  211. return "ERROR: gcd() requires at least 2 arguments, got " + std::to_string(args.size()) + ". Usage: [[gcd(48,18){==} → 6";
  212. }
  213. try {
  214. auto gcd_func = [](long long a, long long b) -> long long {
  215. while (b) { long long t = b; b = a % b; a = t; }
  216. return a;
  217. };
  218. long long result = std::stoll(args[0]);
  219. for (size_t i = 1; i < args.size(); i++) {
  220. result = gcd_func(result, std::stoll(args[i]));
  221. }
  222. return std::to_string(result);
  223. } catch (...) {
  224. return "ERROR: gcd() arguments must be integers. Usage: [[gcd(48,18){==} → 6";
  225. }
  226. }, -1);
  227. // LCM (variable args)
  228. register_tool("lcm", [](const std::vector<std::string>& args) -> std::string {
  229. if (args.size() < 2) {
  230. return "ERROR: lcm() requires at least 2 arguments, got " + std::to_string(args.size()) + ". Usage: [[lcm(12,15){==} → 60";
  231. }
  232. try {
  233. auto gcd_func = [](long long a, long long b) -> long long {
  234. while (b) { long long t = b; b = a % b; a = t; }
  235. return a;
  236. };
  237. auto lcm_func = [&](long long a, long long b) -> long long {
  238. return (a / gcd_func(a, b)) * b;
  239. };
  240. long long result = std::stoll(args[0]);
  241. for (size_t i = 1; i < args.size(); i++) {
  242. result = lcm_func(result, std::stoll(args[i]));
  243. }
  244. return std::to_string(result);
  245. } catch (...) {
  246. return "ERROR: lcm() arguments must be integers. Usage: [[lcm(12,15){==} → 60";
  247. }
  248. }, -1);
  249. // Integer square root
  250. register_tool("isqrt", [](const std::vector<std::string>& args) -> std::string {
  251. if (args.size() != 1) {
  252. return "ERROR: isqrt() requires exactly 1 argument, got " + std::to_string(args.size()) + ". Usage: [[isqrt(25){==} → 5";
  253. }
  254. try {
  255. long long n = std::stoll(args[0]);
  256. if (n < 0) {
  257. return "ERROR: isqrt() requires non-negative integer, got " + args[0] + ". Usage: [[isqrt(25){==} → 5";
  258. }
  259. long long result = static_cast<long long>(std::sqrt(n));
  260. return std::to_string(result);
  261. } catch (...) { return "ERROR"; }
  262. }, 1);
  263. // Combinations: C(n,k) = n! / (k! * (n-k)!)
  264. register_tool("comb", [](const std::vector<std::string>& args) -> std::string {
  265. if (args.size() != 2) {
  266. return "ERROR: comb() requires exactly 2 arguments (n,k), got " + std::to_string(args.size()) + ". Usage: [[comb(5,2){==} → 10";
  267. }
  268. try {
  269. long long n = std::stoll(args[0]);
  270. long long k = std::stoll(args[1]);
  271. if (n < 0 || k < 0) {
  272. return "ERROR: comb() requires non-negative integers. Got n=" + args[0] + ", k=" + args[1] + ". Usage: [[comb(5,2){==} → 10";
  273. }
  274. if (k > n) {
  275. return "ERROR: comb() requires k ≤ n. Got n=" + args[0] + ", k=" + args[1] + ". Usage: [[comb(5,2){==} → 10";
  276. }
  277. if (n > 30) {
  278. return "ERROR: comb() input too large (>30), got n=" + args[0] + ". Would cause overflow. Usage: [[comb(5,2){==} → 10";
  279. }
  280. if (k > n - k) k = n - k;
  281. long long result = 1;
  282. for (int i = 0; i < k; i++) {
  283. result = result * (n - i) / (i + 1);
  284. }
  285. return std::to_string(result);
  286. } catch (...) { return "ERROR"; }
  287. }, 2);
  288. // Permutations: P(n,k) = n! / (n-k)!
  289. register_tool("perm", [](const std::vector<std::string>& args) -> std::string {
  290. if (args.size() != 2) {
  291. return "ERROR: perm() requires exactly 2 arguments (n,k), got " + std::to_string(args.size()) + ". Usage: [[perm(5,2){==} → 20";
  292. }
  293. try {
  294. long long n = std::stoll(args[0]);
  295. long long k = std::stoll(args[1]);
  296. if (n < 0 || k < 0) {
  297. return "ERROR: perm() requires non-negative integers. Got n=" + args[0] + ", k=" + args[1] + ". Usage: [[perm(5,2){==} → 20";
  298. }
  299. if (k > n) {
  300. return "ERROR: perm() requires k ≤ n. Got n=" + args[0] + ", k=" + args[1] + ". Usage: [[perm(5,2){==} → 20";
  301. }
  302. if (n > 20) {
  303. return "ERROR: perm() input too large (>20), got n=" + args[0] + ". Would cause overflow. Usage: [[perm(5,2){==} → 20";
  304. }
  305. long long result = 1;
  306. for (int i = 0; i < k; i++) {
  307. result *= (n - i);
  308. }
  309. return std::to_string(result);
  310. } catch (...) { return "ERROR"; }
  311. }, 2);
  312. // ===== FLOATING POINT MANIPULATION =====
  313. // Ceiling
  314. register_tool("ceil", [](const std::vector<std::string>& args) -> std::string {
  315. if (args.size() != 1) {
  316. return "ERROR: ceil() requires exactly 1 argument, got " + std::to_string(args.size()) + ". Usage: [[ceil(3.2){==} → 4";
  317. }
  318. try {
  319. double x = std::stod(args[0]);
  320. return std::to_string(std::ceil(x));
  321. } catch (...) {
  322. return "ERROR: ceil() argument must be a number. Got: '" + args[0] + "'. Usage: [[ceil(3.2){==} → 4";
  323. }
  324. }, 1);
  325. // Floor
  326. register_tool("floor", [](const std::vector<std::string>& args) -> std::string {
  327. if (args.size() != 1) {
  328. return "ERROR: floor() requires exactly 1 argument, got " + std::to_string(args.size()) + ". Usage: [[floor(3.9){==} → 3";
  329. }
  330. try {
  331. double x = std::stod(args[0]);
  332. return std::to_string(std::floor(x));
  333. } catch (...) {
  334. return "ERROR: floor() argument must be a number. Got: '" + args[0] + "'. Usage: [[floor(3.9){==} → 3";
  335. }
  336. }, 1);
  337. // Absolute value
  338. register_tool("fabs", [](const std::vector<std::string>& args) -> std::string {
  339. if (args.size() != 1) {
  340. return "ERROR: fabs() requires exactly 1 argument, got " + std::to_string(args.size()) + ". Usage: [[fabs(-5.3){==} → 5.3";
  341. }
  342. try {
  343. double x = std::stod(args[0]);
  344. return std::to_string(std::fabs(x));
  345. } catch (...) {
  346. return "ERROR: fabs() argument must be a number. Got: '" + args[0] + "'. Usage: [[fabs(-5.3){==} → 5.3";
  347. }
  348. }, 1);
  349. // Truncate
  350. register_tool("trunc", [](const std::vector<std::string>& args) -> std::string {
  351. if (args.size() != 1) {
  352. return "ERROR: trunc() requires exactly 1 argument, got " + std::to_string(args.size()) + ". Usage: [[trunc(3.9){==} → 3";
  353. }
  354. try {
  355. double x = std::stod(args[0]);
  356. return std::to_string(std::trunc(x));
  357. } catch (...) {
  358. return "ERROR: trunc() argument must be a number. Got: '" + args[0] + "'. Usage: [[trunc(3.9){==} → 3";
  359. }
  360. }, 1);
  361. // Floating point modulo
  362. register_tool("fmod", [](const std::vector<std::string>& args) -> std::string {
  363. if (args.size() != 2) {
  364. return "ERROR: fmod() requires exactly 2 arguments (x,y), got " + std::to_string(args.size()) + ". Usage: [[fmod(10,3){==} → 1";
  365. }
  366. try {
  367. double x = std::stod(args[0]);
  368. double y = std::stod(args[1]);
  369. if (y == 0) {
  370. return "ERROR: fmod() division by zero (mod by 0). Usage: [[fmod(10,3){==} → 1";
  371. }
  372. return std::to_string(std::fmod(x, y));
  373. } catch (...) {
  374. return "ERROR: fmod() arguments must be numbers. Got: '" + args[0] + "', '" + args[1] + "'. Usage: [[fmod(10,3){==} → 1";
  375. }
  376. }, 2);
  377. // ===== EXPONENTIAL & LOGARITHMIC =====
  378. // Cube root
  379. register_tool("cbrt", [](const std::vector<std::string>& args) -> std::string {
  380. if (args.size() != 1) {
  381. return "ERROR: cbrt() requires exactly 1 argument, got " + std::to_string(args.size()) + ". Usage: [[cbrt(27){==} → 3";
  382. }
  383. try {
  384. double x = std::stod(args[0]);
  385. return std::to_string(std::cbrt(x));
  386. } catch (...) {
  387. return "ERROR: cbrt() argument must be a number. Got: '" + args[0] + "'. Usage: [[cbrt(27){==} → 3";
  388. }
  389. }, 1);
  390. // e^x
  391. register_tool("exp", [](const std::vector<std::string>& args) -> std::string {
  392. if (args.size() != 1) {
  393. return "ERROR: exp() requires exactly 1 argument, got " + std::to_string(args.size()) + ". Usage: [[exp(1){==} → 2.71828";
  394. }
  395. try {
  396. double x = std::stod(args[0]);
  397. double result = std::exp(x);
  398. if (std::isinf(result)) {
  399. return "ERROR: exp() result too large (overflow). Input: '" + args[0] + "'. Usage: [[exp(1){==} → 2.71828";
  400. }
  401. return std::to_string(result);
  402. } catch (...) {
  403. return "ERROR: exp() argument must be a number. Got: '" + args[0] + "'. Usage: [[exp(1){==} → 2.71828";
  404. }
  405. }, 1);
  406. // 2^x
  407. register_tool("exp2", [](const std::vector<std::string>& args) -> std::string {
  408. if (args.size() != 1) {
  409. return "ERROR: exp2() requires exactly 1 argument, got " + std::to_string(args.size()) + ". Usage: [[exp2(3){==} → 8";
  410. }
  411. try {
  412. double x = std::stod(args[0]);
  413. double result = std::exp2(x);
  414. if (std::isinf(result)) {
  415. return "ERROR: exp2() result too large (overflow). Input: '" + args[0] + "'. Usage: [[exp2(3){==} → 8";
  416. }
  417. return std::to_string(result);
  418. } catch (...) {
  419. return "ERROR: exp2() argument must be a number. Got: '" + args[0] + "'. Usage: [[exp2(3){==} → 8";
  420. }
  421. }, 1);
  422. // e^x - 1
  423. register_tool("expm1", [](const std::vector<std::string>& args) -> std::string {
  424. if (args.size() != 1) {
  425. return "ERROR: expm1() requires exactly 1 argument, got " + std::to_string(args.size()) + ". Usage: [[expm1(1){==} → 1.71828";
  426. }
  427. try {
  428. double x = std::stod(args[0]);
  429. double result = std::expm1(x);
  430. if (std::isinf(result)) {
  431. return "ERROR: expm1() result too large (overflow). Input: '" + args[0] + "'. Usage: [[expm1(1){==} → 1.71828";
  432. }
  433. return std::to_string(result);
  434. } catch (...) {
  435. return "ERROR: expm1() argument must be a number. Got: '" + args[0] + "'. Usage: [[expm1(1){==} → 1.71828";
  436. }
  437. }, 1);
  438. // Natural logarithm (ln)
  439. register_tool("log", [](const std::vector<std::string>& args) -> std::string {
  440. if (args.size() != 1) {
  441. return "ERROR: log() requires exactly 1 argument, got " + std::to_string(args.size()) + ". Usage: [[log(2.71828){==} → 1";
  442. }
  443. try {
  444. double x = std::stod(args[0]);
  445. if (x <= 0) {
  446. return "ERROR: log() input must be positive (>0). Got: " + args[0] + ". Usage: [[log(2.71828){==} → 1";
  447. }
  448. return std::to_string(std::log(x));
  449. } catch (...) {
  450. return "ERROR: log() argument must be a number. Got: '" + args[0] + "'. Usage: [[log(2.71828){==} → 1";
  451. }
  452. }, 1);
  453. // Base-2 logarithm
  454. register_tool("log2", [](const std::vector<std::string>& args) -> std::string {
  455. if (args.size() != 1) {
  456. return "ERROR: log2() requires exactly 1 argument, got " + std::to_string(args.size()) + ". Usage: [[log2(8){==} → 3";
  457. }
  458. try {
  459. double x = std::stod(args[0]);
  460. if (x <= 0) {
  461. return "ERROR: log2() input must be positive (>0). Got: " + args[0] + ". Usage: [[log2(8){==} → 3";
  462. }
  463. return std::to_string(std::log2(x));
  464. } catch (...) {
  465. return "ERROR: log2() argument must be a number. Got: '" + args[0] + "'. Usage: [[log2(8){==} → 3";
  466. }
  467. }, 1);
  468. // Base-10 logarithm
  469. register_tool("log10", [](const std::vector<std::string>& args) -> std::string {
  470. if (args.size() != 1) {
  471. return "ERROR: log10() requires exactly 1 argument, got " + std::to_string(args.size()) + ". Usage: [[log10(100){==} → 2";
  472. }
  473. try {
  474. double x = std::stod(args[0]);
  475. if (x <= 0) {
  476. return "ERROR: log10() input must be positive (>0). Got: " + args[0] + ". Usage: [[log10(100){==} → 2";
  477. }
  478. return std::to_string(std::log10(x));
  479. } catch (...) {
  480. return "ERROR: log10() argument must be a number. Got: '" + args[0] + "'. Usage: [[log10(100){==} → 2";
  481. }
  482. }, 1);
  483. // ln(1+x)
  484. register_tool("log1p", [](const std::vector<std::string>& args) -> std::string {
  485. if (args.size() != 1) {
  486. return "ERROR: log1p() requires exactly 1 argument, got " + std::to_string(args.size()) + ". Usage: [[log1p(1){==} → 0.693147";
  487. }
  488. try {
  489. double x = std::stod(args[0]);
  490. if (x <= -1) {
  491. return "ERROR: log1p() input must be > -1. Got: " + args[0] + ". Usage: [[log1p(1){==} → 0.693147";
  492. }
  493. return std::to_string(std::log1p(x));
  494. } catch (...) {
  495. return "ERROR: log1p() argument must be a number. Got: '" + args[0] + "'. Usage: [[log1p(1){==} → 0.693147";
  496. }
  497. }, 1);
  498. // ===== TRIGONOMETRIC FUNCTIONS =====
  499. // Sine
  500. register_tool("sin", [](const std::vector<std::string>& args) -> std::string {
  501. if (args.size() != 1) {
  502. return "ERROR: sin() requires exactly 1 argument, got " + std::to_string(args.size()) + ". Usage: [[sin(0){==} → 0";
  503. }
  504. try {
  505. double x = std::stod(args[0]);
  506. return std::to_string(std::sin(x));
  507. } catch (...) {
  508. return "ERROR: sin() argument must be a number (radians). Got: '" + args[0] + "'. Usage: [[sin(0){==} → 0";
  509. }
  510. }, 1);
  511. // Cosine
  512. register_tool("cos", [](const std::vector<std::string>& args) -> std::string {
  513. if (args.size() != 1) {
  514. return "ERROR: cos() requires exactly 1 argument, got " + std::to_string(args.size()) + ". Usage: [[cos(0){==} → 1";
  515. }
  516. try {
  517. double x = std::stod(args[0]);
  518. return std::to_string(std::cos(x));
  519. } catch (...) {
  520. return "ERROR: cos() argument must be a number (radians). Got: '" + args[0] + "'. Usage: [[cos(0){==} → 1";
  521. }
  522. }, 1);
  523. // Tangent
  524. register_tool("tan", [](const std::vector<std::string>& args) -> std::string {
  525. if (args.size() != 1) {
  526. return "ERROR: tan() requires exactly 1 argument, got " + std::to_string(args.size()) + ". Usage: [[tan(0){==} → 0";
  527. }
  528. try {
  529. double x = std::stod(args[0]);
  530. double result = std::tan(x);
  531. if (std::isinf(result)) {
  532. return "ERROR: tan() undefined (asymptote). Input: '" + args[0] + "' (radians). Usage: [[tan(0){==} → 0";
  533. }
  534. return std::to_string(result);
  535. } catch (...) {
  536. return "ERROR: tan() argument must be a number (radians). Got: '" + args[0] + "'. Usage: [[tan(0){==} → 0";
  537. }
  538. }, 1);
  539. // Arcsine
  540. register_tool("asin", [](const std::vector<std::string>& args) -> std::string {
  541. if (args.size() != 1) {
  542. return "ERROR: asin() requires exactly 1 argument, got " + std::to_string(args.size()) + ". Usage: [[asin(0){==} → 0";
  543. }
  544. try {
  545. double x = std::stod(args[0]);
  546. if (x < -1 || x > 1) {
  547. return "ERROR: asin() input must be between -1 and 1. Got: " + args[0] + ". Usage: [[asin(0){==} → 0";
  548. }
  549. return std::to_string(std::asin(x));
  550. } catch (...) {
  551. return "ERROR: asin() argument must be a number. Got: '" + args[0] + "'. Usage: [[asin(0){==} → 0";
  552. }
  553. }, 1);
  554. // Arccosine
  555. register_tool("acos", [](const std::vector<std::string>& args) -> std::string {
  556. if (args.size() != 1) {
  557. return "ERROR: acos() requires exactly 1 argument, got " + std::to_string(args.size()) + ". Usage: [[acos(1){==} → 0";
  558. }
  559. try {
  560. double x = std::stod(args[0]);
  561. if (x < -1 || x > 1) {
  562. return "ERROR: acos() input must be between -1 and 1. Got: " + args[0] + ". Usage: [[acos(1){==} → 0";
  563. }
  564. return std::to_string(std::acos(x));
  565. } catch (...) {
  566. return "ERROR: acos() argument must be a number. Got: '" + args[0] + "'. Usage: [[acos(1){==} → 0";
  567. }
  568. }, 1);
  569. // Arctangent
  570. register_tool("atan", [](const std::vector<std::string>& args) -> std::string {
  571. if (args.size() != 1) {
  572. return "ERROR: atan() requires exactly 1 argument, got " + std::to_string(args.size()) + ". Usage: [[atan(1){==} → 0.785398";
  573. }
  574. try {
  575. double x = std::stod(args[0]);
  576. return std::to_string(std::atan(x));
  577. } catch (...) {
  578. return "ERROR: atan() argument must be a number. Got: '" + args[0] + "'. Usage: [[atan(1){==} → 0.785398";
  579. }
  580. }, 1);
  581. // Arctangent2 (y, x)
  582. register_tool("atan2", [](const std::vector<std::string>& args) -> std::string {
  583. if (args.size() != 2) {
  584. return "ERROR: atan2() requires exactly 2 arguments (y,x), got " + std::to_string(args.size()) + ". Usage: [[atan2(1,1){==} → 0.785398";
  585. }
  586. try {
  587. double y = std::stod(args[0]);
  588. double x = std::stod(args[1]);
  589. return std::to_string(std::atan2(y, x));
  590. } catch (...) {
  591. return "ERROR: atan2() arguments must be numbers. Got: '" + args[0] + "', '" + args[1] + "'. Usage: [[atan2(1,1){==} → 0.785398";
  592. }
  593. }, 2);
  594. // Degrees to radians
  595. register_tool("radians", [](const std::vector<std::string>& args) -> std::string {
  596. if (args.size() != 1) {
  597. return "ERROR: radians() requires exactly 1 argument, got " + std::to_string(args.size()) + ". Usage: [[radians(180){==} → 3.14159";
  598. }
  599. try {
  600. double x = std::stod(args[0]);
  601. return std::to_string(x * M_PI / 180.0);
  602. } catch (...) {
  603. return "ERROR: radians() argument must be a number. Got: '" + args[0] + "'. Usage: [[radians(180){==} → 3.14159";
  604. }
  605. }, 1);
  606. // Radians to degrees
  607. register_tool("degrees", [](const std::vector<std::string>& args) -> std::string {
  608. if (args.size() != 1) {
  609. return "ERROR: degrees() requires exactly 1 argument, got " + std::to_string(args.size()) + ". Usage: [[degrees(3.14159){==} → 180";
  610. }
  611. try {
  612. double x = std::stod(args[0]);
  613. return std::to_string(x * 180.0 / M_PI);
  614. } catch (...) {
  615. return "ERROR: degrees() argument must be a number. Got: '" + args[0] + "'. Usage: [[degrees(3.14159){==} → 180";
  616. }
  617. }, 1);
  618. // ===== HYPERBOLIC FUNCTIONS =====
  619. // Hyperbolic sine
  620. register_tool("sinh", [](const std::vector<std::string>& args) -> std::string {
  621. if (args.size() != 1) {
  622. return "ERROR: sinh() requires exactly 1 argument, got " + std::to_string(args.size()) + ". Usage: [[sinh(0){==} → 0";
  623. }
  624. try {
  625. double x = std::stod(args[0]);
  626. double result = std::sinh(x);
  627. if (std::isinf(result)) {
  628. return "ERROR: sinh() result too large (overflow). Input: '" + args[0] + "'. Usage: [[sinh(0){==} → 0";
  629. }
  630. return std::to_string(result);
  631. } catch (...) {
  632. return "ERROR: sinh() argument must be a number. Got: '" + args[0] + "'. Usage: [[sinh(0){==} → 0";
  633. }
  634. }, 1);
  635. // Hyperbolic cosine
  636. register_tool("cosh", [](const std::vector<std::string>& args) -> std::string {
  637. if (args.size() != 1) {
  638. return "ERROR: cosh() requires exactly 1 argument, got " + std::to_string(args.size()) + ". Usage: [[cosh(0){==} → 1";
  639. }
  640. try {
  641. double x = std::stod(args[0]);
  642. double result = std::cosh(x);
  643. if (std::isinf(result)) {
  644. return "ERROR: cosh() result too large (overflow). Input: '" + args[0] + "'. Usage: [[cosh(0){==} → 1";
  645. }
  646. return std::to_string(result);
  647. } catch (...) {
  648. return "ERROR: cosh() argument must be a number. Got: '" + args[0] + "'. Usage: [[cosh(0){==} → 1";
  649. }
  650. }, 1);
  651. // Hyperbolic tangent
  652. register_tool("tanh", [](const std::vector<std::string>& args) -> std::string {
  653. if (args.size() != 1) {
  654. return "ERROR: tanh() requires exactly 1 argument, got " + std::to_string(args.size()) + ". Usage: [[tanh(0){==} → 0";
  655. }
  656. try {
  657. double x = std::stod(args[0]);
  658. return std::to_string(std::tanh(x));
  659. } catch (...) {
  660. return "ERROR: tanh() argument must be a number. Got: '" + args[0] + "'. Usage: [[tanh(0){==} → 0";
  661. }
  662. }, 1);
  663. // Inverse hyperbolic sine
  664. register_tool("asinh", [](const std::vector<std::string>& args) -> std::string {
  665. if (args.size() != 1) {
  666. return "ERROR: asinh() requires exactly 1 argument, got " + std::to_string(args.size()) + ". Usage: [[asinh(0){==} → 0";
  667. }
  668. try {
  669. double x = std::stod(args[0]);
  670. return std::to_string(std::asinh(x));
  671. } catch (...) {
  672. return "ERROR: asinh() argument must be a number. Got: '" + args[0] + "'. Usage: [[asinh(0){==} → 0";
  673. }
  674. }, 1);
  675. // Inverse hyperbolic cosine
  676. register_tool("acosh", [](const std::vector<std::string>& args) -> std::string {
  677. if (args.size() != 1) {
  678. return "ERROR: acosh() requires exactly 1 argument, got " + std::to_string(args.size()) + ". Usage: [[acosh(1){==} → 0";
  679. }
  680. try {
  681. double x = std::stod(args[0]);
  682. if (x < 1) {
  683. return "ERROR: acosh() input must be >= 1. Got: " + args[0] + ". Usage: [[acosh(1){==} → 0";
  684. }
  685. return std::to_string(std::acosh(x));
  686. } catch (...) {
  687. return "ERROR: acosh() argument must be a number. Got: '" + args[0] + "'. Usage: [[acosh(1){==} → 0";
  688. }
  689. }, 1);
  690. // Inverse hyperbolic tangent
  691. register_tool("atanh", [](const std::vector<std::string>& args) -> std::string {
  692. if (args.size() != 1) {
  693. return "ERROR: atanh() requires exactly 1 argument, got " + std::to_string(args.size()) + ". Usage: [[atanh(0){==} → 0";
  694. }
  695. try {
  696. double x = std::stod(args[0]);
  697. if (x <= -1 || x >= 1) {
  698. return "ERROR: atanh() input must be between -1 and 1. Got: " + args[0] + ". Usage: [[atanh(0){==} → 0";
  699. }
  700. return std::to_string(std::atanh(x));
  701. } catch (...) {
  702. return "ERROR: atanh() argument must be a number. Got: '" + args[0] + "'. Usage: [[atanh(0){==} → 0";
  703. }
  704. }, 1);
  705. // ===== SPECIAL FUNCTIONS =====
  706. // Error function
  707. register_tool("erf", [](const std::vector<std::string>& args) -> std::string {
  708. if (args.size() != 1) {
  709. return "ERROR: erf() requires exactly 1 argument, got " + std::to_string(args.size()) + ". Usage: [[erf(0){==} → 0";
  710. }
  711. try {
  712. double x = std::stod(args[0]);
  713. return std::to_string(std::erf(x));
  714. } catch (...) {
  715. return "ERROR: erf() argument must be a number. Got: '" + args[0] + "'. Usage: [[erf(0){==} → 0";
  716. }
  717. }, 1);
  718. // Complementary error function
  719. register_tool("erfc", [](const std::vector<std::string>& args) -> std::string {
  720. if (args.size() != 1) {
  721. return "ERROR: erfc() requires exactly 1 argument, got " + std::to_string(args.size()) + ". Usage: [[erfc(0){==} → 1";
  722. }
  723. try {
  724. double x = std::stod(args[0]);
  725. return std::to_string(std::erfc(x));
  726. } catch (...) {
  727. return "ERROR: erfc() argument must be a number. Got: '" + args[0] + "'. Usage: [[erfc(0){==} → 1";
  728. }
  729. }, 1);
  730. // Gamma function
  731. register_tool("gamma", [](const std::vector<std::string>& args) -> std::string {
  732. if (args.size() != 1) {
  733. return "ERROR: gamma() requires exactly 1 argument, got " + std::to_string(args.size()) + ". Usage: [[gamma(5){==} → 24";
  734. }
  735. try {
  736. double x = std::stod(args[0]);
  737. if (x <= 0 && std::floor(x) == x) {
  738. return "ERROR: gamma() undefined for non-positive integers. Got: " + args[0] + ". Usage: [[gamma(5){==} → 24";
  739. }
  740. double result = std::tgamma(x);
  741. if (std::isinf(result)) {
  742. return "ERROR: gamma() result too large (overflow). Input: '" + args[0] + "'. Usage: [[gamma(5){==} → 24";
  743. }
  744. return std::to_string(result);
  745. } catch (...) {
  746. return "ERROR: gamma() argument must be a number. Got: '" + args[0] + "'. Usage: [[gamma(5){==} → 24";
  747. }
  748. }, 1);
  749. // Log gamma function
  750. register_tool("lgamma", [](const std::vector<std::string>& args) -> std::string {
  751. if (args.size() != 1) {
  752. return "ERROR: lgamma() requires exactly 1 argument, got " + std::to_string(args.size()) + ". Usage: [[lgamma(5){==} → 3.17805";
  753. }
  754. try {
  755. double x = std::stod(args[0]);
  756. if (x <= 0 && std::floor(x) == x) {
  757. return "ERROR: lgamma() undefined for non-positive integers. Got: " + args[0] + ". Usage: [[lgamma(5){==} → 3.17805";
  758. }
  759. return std::to_string(std::lgamma(x));
  760. } catch (...) {
  761. return "ERROR: lgamma() argument must be a number. Got: '" + args[0] + "'. Usage: [[lgamma(5){==} → 3.17805";
  762. }
  763. }, 1);
  764. // ===== MATHEMATICAL CONSTANTS =====
  765. // Pi constant
  766. register_tool("pi", [](const std::vector<std::string>& args) -> std::string {
  767. if (args.size() != 0) {
  768. return "ERROR: pi() takes no arguments, got " + std::to_string(args.size()) + ". Usage: [[pi(){==} → 3.14159";
  769. }
  770. return std::to_string(M_PI);
  771. }, 0);
  772. // Euler's number
  773. register_tool("e", [](const std::vector<std::string>& args) -> std::string {
  774. if (args.size() != 0) {
  775. return "ERROR: e() takes no arguments, got " + std::to_string(args.size()) + ". Usage: [[e(){==} → 2.71828";
  776. }
  777. return std::to_string(M_E);
  778. }, 0);
  779. // Tau (2*pi)
  780. register_tool("tau", [](const std::vector<std::string>& args) -> std::string {
  781. if (args.size() != 0) {
  782. return "ERROR: tau() takes no arguments, got " + std::to_string(args.size()) + ". Usage: [[tau(){==} → 6.28318";
  783. }
  784. return std::to_string(2.0 * M_PI);
  785. }, 0);
  786. // ===== DATE & TIME FUNCTIONS =====
  787. // Get current Unix timestamp (seconds since epoch)
  788. register_tool("now", [](const std::vector<std::string>& args) -> std::string {
  789. if (args.size() != 0) {
  790. return "ERROR: now() takes no arguments, got " + std::to_string(args.size()) + ". Usage: [[now(){==} → 1640995200";
  791. }
  792. try {
  793. auto now = std::chrono::system_clock::now();
  794. auto epoch = now.time_since_epoch();
  795. auto seconds = std::chrono::duration_cast<std::chrono::seconds>(epoch).count();
  796. return std::to_string(seconds);
  797. } catch (...) {
  798. return "ERROR: now() failed to get current timestamp. Usage: [[now(){==} → 1640995200";
  799. }
  800. }, 0);
  801. // Get current date in YYYY-MM-DD format (UTC)
  802. register_tool("current_date", [](const std::vector<std::string>& args) -> std::string {
  803. if (args.size() != 0) {
  804. return "ERROR: current_date() takes no arguments, got " + std::to_string(args.size()) + ". Usage: [[current_date(){==} → '2024-01-01'";
  805. }
  806. try {
  807. auto now = std::chrono::system_clock::now();
  808. std::time_t now_c = std::chrono::system_clock::to_time_t(now);
  809. std::tm* now_tm = std::gmtime(&now_c);
  810. std::ostringstream oss;
  811. oss << std::put_time(now_tm, "%Y-%m-%d");
  812. return oss.str();
  813. } catch (...) {
  814. return "ERROR: current_date() failed to get current date. Usage: [[current_date(){==} → '2024-01-01'";
  815. }
  816. }, 0);
  817. // Get current time in HH:MM:SS format (UTC)
  818. register_tool("current_time", [](const std::vector<std::string>& args) -> std::string {
  819. if (args.size() != 0) {
  820. return "ERROR: current_time() takes no arguments, got " + std::to_string(args.size()) + ". Usage: [[current_time(){==} → '12:30:45'";
  821. }
  822. try {
  823. auto now = std::chrono::system_clock::now();
  824. std::time_t now_c = std::chrono::system_clock::to_time_t(now);
  825. std::tm* now_tm = std::gmtime(&now_c);
  826. std::ostringstream oss;
  827. oss << std::put_time(now_tm, "%H:%M:%S");
  828. return oss.str();
  829. } catch (...) {
  830. return "ERROR: current_time() failed to get current time. Usage: [[current_time(){==} → '12:30:45'";
  831. }
  832. }, 0);
  833. // Get current datetime in ISO 8601 format (UTC)
  834. register_tool("current_datetime", [](const std::vector<std::string>& args) -> std::string {
  835. if (args.size() != 0) {
  836. return "ERROR: current_datetime() takes no arguments, got " + std::to_string(args.size()) + ". Usage: [[current_datetime(){==} → '2024-01-01 12:30:45 UTC'";
  837. }
  838. try {
  839. auto now = std::chrono::system_clock::now();
  840. std::time_t now_c = std::chrono::system_clock::to_time_t(now);
  841. std::tm* now_tm = std::gmtime(&now_c);
  842. std::ostringstream oss;
  843. oss << std::put_time(now_tm, "%Y-%m-%d %H:%M:%S UTC");
  844. return oss.str();
  845. } catch (...) {
  846. return "ERROR: current_datetime() failed to get current datetime. Usage: [[current_datetime(){==} → '2024-01-01 12:30:45 UTC'";
  847. }
  848. }, 0);
  849. // Get current datetime in local timezone
  850. register_tool("local_datetime", [](const std::vector<std::string>& args) -> std::string {
  851. if (args.size() != 0) {
  852. return "ERROR: local_datetime() takes no arguments, got " + std::to_string(args.size()) + ". Usage: [[local_datetime(){==} → '2024-01-01 12:30:45 (UTC+0)'";
  853. }
  854. try {
  855. auto now = std::chrono::system_clock::now();
  856. std::time_t now_c = std::chrono::system_clock::to_time_t(now);
  857. std::tm* local_tm = std::localtime(&now_c);
  858. std::ostringstream oss;
  859. oss << std::put_time(local_tm, "%Y-%m-%d %H:%M:%S");
  860. // Get timezone offset
  861. std::tm* utc_tm = std::gmtime(&now_c);
  862. int offset_hours = local_tm->tm_hour - utc_tm->tm_hour;
  863. int offset_mins = local_tm->tm_min - utc_tm->tm_min;
  864. // Handle day boundary crossings
  865. if (local_tm->tm_mday != utc_tm->tm_mday) {
  866. if (local_tm->tm_mday > utc_tm->tm_mday ||
  867. (local_tm->tm_mday == 1 && utc_tm->tm_mday > 20)) {
  868. offset_hours += 24;
  869. } else {
  870. offset_hours -= 24;
  871. }
  872. }
  873. int total_offset = offset_hours;
  874. if (offset_mins != 0) {
  875. total_offset = offset_hours >= 0 ? offset_hours : offset_hours;
  876. }
  877. oss << " (UTC";
  878. if (total_offset >= 0) oss << "+";
  879. oss << total_offset << ")";
  880. return oss.str();
  881. } catch (...) {
  882. return "ERROR: local_datetime() failed to get local datetime. Usage: [[local_datetime(){==} → '2024-01-01 12:30:45 (UTC+0)'";
  883. }
  884. }, 0);
  885. // Get system timezone offset (hours from UTC)
  886. register_tool("system_timezone_offset", [](const std::vector<std::string>& args) -> std::string {
  887. if (args.size() != 0) {
  888. return "ERROR: system_timezone_offset() takes no arguments, got " + std::to_string(args.size()) + ". Usage: [[system_timezone_offset(){==} → '0'";
  889. }
  890. try {
  891. auto now = std::chrono::system_clock::now();
  892. std::time_t now_c = std::chrono::system_clock::to_time_t(now);
  893. std::tm* local_tm = std::localtime(&now_c);
  894. std::tm* utc_tm = std::gmtime(&now_c);
  895. int offset_hours = local_tm->tm_hour - utc_tm->tm_hour;
  896. // Handle day boundary crossings
  897. if (local_tm->tm_mday != utc_tm->tm_mday) {
  898. if (local_tm->tm_mday > utc_tm->tm_mday ||
  899. (local_tm->tm_mday == 1 && utc_tm->tm_mday > 20)) {
  900. offset_hours += 24;
  901. } else {
  902. offset_hours -= 24;
  903. }
  904. }
  905. return std::to_string(offset_hours);
  906. } catch (...) {
  907. return "ERROR: system_timezone_offset() failed to get timezone offset. Usage: [[system_timezone_offset(){==} → '0'";
  908. }
  909. }, 0);
  910. // Add days to a Unix timestamp
  911. register_tool("add_days", [](const std::vector<std::string>& args) -> std::string {
  912. if (args.size() != 2) {
  913. return "ERROR: add_days() requires exactly 2 arguments (timestamp, days), got " + std::to_string(args.size()) + ". Usage: [[add_days(1640995200,7){==} → 1641600000";
  914. }
  915. try {
  916. long long timestamp = std::stoll(args[0]);
  917. long long days = std::stoll(args[1]);
  918. if (days < LLONG_MIN / 86400 || days > LLONG_MAX / 86400) {
  919. return "ERROR: add_days() days value too large, got " + args[1] + ". Usage: [[add_days(1640995200,7){==} → 1641600000";
  920. }
  921. long long result = timestamp + (days * 86400LL); // 86400 seconds per day
  922. return std::to_string(result);
  923. } catch (const std::out_of_range&) {
  924. return "ERROR: add_days() arguments out of range. Got: '" + args[0] + "', '" + args[1] + "'. Usage: [[add_days(1640995200,7){==} → 1641600000";
  925. } catch (const std::invalid_argument&) {
  926. return "ERROR: add_days() arguments must be numbers (timestamp, days). Got: '" + args[0] + "', '" + args[1] + "'. Usage: [[add_days(1640995200,7){==} → 1641600000";
  927. }
  928. }, 2);
  929. // Add hours to a Unix timestamp
  930. register_tool("add_hours", [](const std::vector<std::string>& args) -> std::string {
  931. if (args.size() != 2) {
  932. return "ERROR: add_hours() requires exactly 2 arguments (timestamp, hours), got " + std::to_string(args.size()) + ". Usage: [[add_hours(1640995200,24){==} → 1641081600";
  933. }
  934. try {
  935. long long timestamp = std::stoll(args[0]);
  936. long long hours = std::stoll(args[1]);
  937. if (hours < LLONG_MIN / 3600 || hours > LLONG_MAX / 3600) {
  938. return "ERROR: add_hours() hours value too large, got " + args[1] + ". Usage: [[add_hours(1640995200,24){==} → 1641081600";
  939. }
  940. long long result = timestamp + (hours * 3600LL); // 3600 seconds per hour
  941. return std::to_string(result);
  942. } catch (const std::out_of_range&) {
  943. return "ERROR: add_hours() arguments out of range. Got: '" + args[0] + "', '" + args[1] + "'. Usage: [[add_hours(1640995200,24){==} → 1641081600";
  944. } catch (const std::invalid_argument&) {
  945. return "ERROR: add_hours() arguments must be numbers (timestamp, hours). Got: '" + args[0] + "', '" + args[1] + "'. Usage: [[add_hours(1640995200,24){==} → 1641081600";
  946. }
  947. }, 2);
  948. // Add minutes to a Unix timestamp
  949. register_tool("add_minutes", [](const std::vector<std::string>& args) -> std::string {
  950. if (args.size() != 2) {
  951. return "ERROR: add_minutes() requires exactly 2 arguments (timestamp, minutes), got " + std::to_string(args.size()) + ". Usage: [[add_minutes(1640995200,60){==} → 1640995260";
  952. }
  953. try {
  954. long long timestamp = std::stoll(args[0]);
  955. long long minutes = std::stoll(args[1]);
  956. if (minutes < LLONG_MIN / 60 || minutes > LLONG_MAX / 60) {
  957. return "ERROR: add_minutes() minutes value too large, got " + args[1] + ". Usage: [[add_minutes(1640995200,60){==} → 1640995260";
  958. }
  959. long long result = timestamp + (minutes * 60LL);
  960. return std::to_string(result);
  961. } catch (const std::out_of_range&) {
  962. return "ERROR: add_minutes() arguments out of range. Got: '" + args[0] + "', '" + args[1] + "'. Usage: [[add_minutes(1640995200,60){==} → 1640995260";
  963. } catch (const std::invalid_argument&) {
  964. return "ERROR: add_minutes() arguments must be numbers (timestamp, minutes). Got: '" + args[0] + "', '" + args[1] + "'. Usage: [[add_minutes(1640995200,60){==} → 1640995260";
  965. }
  966. }, 2);
  967. // Calculate difference in days between two timestamps
  968. register_tool("diff_days", [](const std::vector<std::string>& args) -> std::string {
  969. if (args.size() != 2) {
  970. return "ERROR: diff_days() requires exactly 2 arguments (ts1, ts2), got " + std::to_string(args.size()) + ". Usage: [[diff_days(1640995200,1641600000){==} → 7";
  971. }
  972. try {
  973. long long ts1 = std::stoll(args[0]);
  974. long long ts2 = std::stoll(args[1]);
  975. long long diff = (ts2 - ts1) / 86400LL;
  976. return std::to_string(diff);
  977. } catch (...) {
  978. return "ERROR: diff_days() arguments must be timestamps. Got: '" + args[0] + "', '" + args[1] + "'. Usage: [[diff_days(1640995200,1641600000){==} → 7";
  979. }
  980. }, 2);
  981. // Calculate difference in hours between two timestamps
  982. register_tool("diff_hours", [](const std::vector<std::string>& args) -> std::string {
  983. if (args.size() != 2) {
  984. return "ERROR: diff_hours() requires exactly 2 arguments (ts1, ts2), got " + std::to_string(args.size()) + ". Usage: [[diff_hours(1640995200,1641081600){==} → 24";
  985. }
  986. try {
  987. long long ts1 = std::stoll(args[0]);
  988. long long ts2 = std::stoll(args[1]);
  989. long long diff = (ts2 - ts1) / 3600LL;
  990. return std::to_string(diff);
  991. } catch (...) {
  992. return "ERROR: diff_hours() arguments must be timestamps. Got: '" + args[0] + "', '" + args[1] + "'. Usage: [[diff_hours(1640995200,1641081600){==} → 24";
  993. }
  994. }, 2);
  995. // Convert timestamp to readable date string
  996. register_tool("timestamp_to_date", [](const std::vector<std::string>& args) -> std::string {
  997. if (args.size() != 1) {
  998. return "ERROR: timestamp_to_date() requires exactly 1 argument, got " + std::to_string(args.size()) + ". Usage: [[timestamp_to_date(1640995200){==} → '2022-01-01 00:00:00 UTC'";
  999. }
  1000. try {
  1001. long long timestamp = std::stoll(args[0]);
  1002. std::time_t time = static_cast<std::time_t>(timestamp);
  1003. std::tm* tm_utc = std::gmtime(&time);
  1004. std::ostringstream oss;
  1005. oss << std::put_time(tm_utc, "%Y-%m-%d %H:%M:%S UTC");
  1006. return oss.str();
  1007. } catch (...) {
  1008. return "ERROR: timestamp_to_date() argument must be a timestamp. Got: '" + args[0] + "'. Usage: [[timestamp_to_date(1640995200){==} → '2022-01-01 00:00:00 UTC'";
  1009. }
  1010. }, 1);
  1011. // Get day of week from timestamp (0=Sunday, 6=Saturday)
  1012. register_tool("day_of_week", [](const std::vector<std::string>& args) -> std::string {
  1013. if (args.size() != 1) {
  1014. return "ERROR: day_of_week() requires exactly 1 argument, got " + std::to_string(args.size()) + ". Usage: [[day_of_week(1640995200){==} → 'Saturday'";
  1015. }
  1016. try {
  1017. long long timestamp = std::stoll(args[0]);
  1018. std::time_t time = static_cast<std::time_t>(timestamp);
  1019. std::tm* tm_utc = std::gmtime(&time);
  1020. const char* days[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
  1021. return days[tm_utc->tm_wday];
  1022. } catch (...) {
  1023. return "ERROR: day_of_week() argument must be a timestamp. Got: '" + args[0] + "'. Usage: [[day_of_week(1640995200){==} → 'Saturday'";
  1024. }
  1025. }, 1);
  1026. // Convert time between timezones (offset in hours from UTC)
  1027. // Args: timestamp, from_offset, to_offset
  1028. // Example: timezone_convert(now, 3, 0) converts from UTC+3 (Istanbul) to UTC (London winter)
  1029. register_tool("timezone_convert", [](const std::vector<std::string>& args) -> std::string {
  1030. if (args.size() != 3) {
  1031. return "ERROR: timezone_convert() requires exactly 3 arguments (timestamp, from_offset, to_offset), got " + std::to_string(args.size()) + ". Usage: [[timezone_convert(1640995200,3,0){==} → 1640988000";
  1032. }
  1033. try {
  1034. long long timestamp = std::stoll(args[0]);
  1035. long long from_offset = std::stoll(args[1]); // hours from UTC
  1036. long long to_offset = std::stoll(args[2]); // hours from UTC
  1037. if (from_offset < LLONG_MIN / 3600 || from_offset > LLONG_MAX / 3600 ||
  1038. to_offset < LLONG_MIN / 3600 || to_offset > LLONG_MAX / 3600) {
  1039. return "ERROR: timezone_convert() offset values too large. Got: '" + args[1] + "', '" + args[2] + "'. Usage: [[timezone_convert(1640995200,3,0){==} → 1640988000";
  1040. }
  1041. // Adjust timestamp from source timezone to UTC, then to target timezone
  1042. long long utc_timestamp = timestamp - (from_offset * 3600LL);
  1043. long long result = utc_timestamp + (to_offset * 3600LL);
  1044. return std::to_string(result);
  1045. } catch (...) {
  1046. return "ERROR: timezone_convert() arguments must be numbers (timestamp, from_offset, to_offset). Got: '" + args[0] + "', '" + args[1] + "', '" + args[2] + "'. Usage: [[timezone_convert(1640995200,3,0){==} → 1640988000";
  1047. }
  1048. }, 3);
  1049. // Get hour from timestamp in a given timezone offset
  1050. register_tool("get_hour_tz", [](const std::vector<std::string>& args) -> std::string {
  1051. if (args.size() != 2) {
  1052. return "ERROR: get_hour_tz() requires exactly 2 arguments (timestamp, offset), got " + std::to_string(args.size()) + ". Usage: [[get_hour_tz(1640995200,3){==} → 3";
  1053. }
  1054. try {
  1055. long long timestamp = std::stoll(args[0]);
  1056. long long tz_offset = std::stoll(args[1]); // hours from UTC
  1057. if (tz_offset < LLONG_MIN / 3600 || tz_offset > LLONG_MAX / 3600) {
  1058. return "ERROR: get_hour_tz() timezone offset too large, got " + args[1] + ". Usage: [[get_hour_tz(1640995200,3){==} → 3";
  1059. }
  1060. long long adjusted = timestamp + (tz_offset * 3600LL);
  1061. std::time_t time = static_cast<std::time_t>(adjusted);
  1062. std::tm* tm_utc = std::gmtime(&time);
  1063. return std::to_string(tm_utc->tm_hour);
  1064. } catch (const std::out_of_range&) {
  1065. return "ERROR: get_hour_tz() arguments out of range. Got: '" + args[0] + "', '" + args[1] + "'. Usage: [[get_hour_tz(1640995200,3){==} → 3";
  1066. } catch (const std::invalid_argument&) {
  1067. return "ERROR: get_hour_tz() arguments must be numbers (timestamp, offset). Got: '" + args[0] + "', '" + args[1] + "'. Usage: [[get_hour_tz(1640995200,3){==} → 3";
  1068. }
  1069. }, 2);
  1070. // Get formatted datetime in a specific timezone
  1071. register_tool("datetime_in_tz", [](const std::vector<std::string>& args) -> std::string {
  1072. if (args.size() != 2) {
  1073. return "ERROR: datetime_in_tz() requires exactly 2 arguments (timestamp, offset), got " + std::to_string(args.size()) + ". Usage: [[datetime_in_tz(1640995200,3){==} → '2022-01-01 03:00:00 (UTC+3)'";
  1074. }
  1075. try {
  1076. long long timestamp = std::stoll(args[0]);
  1077. long long tz_offset = std::stoll(args[1]); // hours from UTC
  1078. if (tz_offset < LLONG_MIN / 3600 || tz_offset > LLONG_MAX / 3600) {
  1079. return "ERROR: datetime_in_tz() timezone offset too large, got " + args[1] + ". Usage: [[datetime_in_tz(1640995200,3){==} → '2022-01-01 03:00:00 (UTC+3)'";
  1080. }
  1081. long long adjusted = timestamp + (tz_offset * 3600LL);
  1082. std::time_t time = static_cast<std::time_t>(adjusted);
  1083. std::tm* tm_utc = std::gmtime(&time);
  1084. std::ostringstream oss;
  1085. oss << std::put_time(tm_utc, "%Y-%m-%d %H:%M:%S (UTC");
  1086. if (tz_offset >= 0) oss << "+";
  1087. oss << tz_offset << ")";
  1088. return oss.str();
  1089. } catch (const std::out_of_range&) {
  1090. return "ERROR: datetime_in_tz() arguments out of range. Got: '" + args[0] + "', '" + args[1] + "'. Usage: [[datetime_in_tz(1640995200,3){==} → '2022-01-01 03:00:00 (UTC+3)'";
  1091. } catch (const std::invalid_argument&) {
  1092. return "ERROR: datetime_in_tz() arguments must be numbers (timestamp, offset). Got: '" + args[0] + "', '" + args[1] + "'. Usage: [[datetime_in_tz(1640995200,3){==} → '2022-01-01 03:00:00 (UTC+3)'";
  1093. }
  1094. }, 2);
  1095. // Create timestamp from date components (year, month, day, hour, minute, second)
  1096. register_tool("make_timestamp", [](const std::vector<std::string>& args) -> std::string {
  1097. if (args.size() != 6) {
  1098. return "ERROR: make_timestamp() requires exactly 6 arguments (y,m,d,h,m,s), got " + std::to_string(args.size()) + ". Usage: [[make_timestamp(2022,1,1,0,0,0){==} → 1640995200";
  1099. }
  1100. try {
  1101. std::tm time_info = {};
  1102. long long year = std::stoll(args[0]);
  1103. long long month = std::stoll(args[1]);
  1104. long long day = std::stoll(args[2]);
  1105. long long hour = std::stoll(args[3]);
  1106. long long minute = std::stoll(args[4]);
  1107. long long second = std::stoll(args[5]);
  1108. // Validate ranges
  1109. if (year < 1970 || year > 2038) {
  1110. return "ERROR: make_timestamp() year must be between 1970-2038, got " + args[0] + ". Usage: [[make_timestamp(2022,1,1,0,0,0){==} → 1640995200";
  1111. }
  1112. if (month < 1 || month > 12) {
  1113. return "ERROR: make_timestamp() month must be 1-12, got " + args[1] + ". Usage: [[make_timestamp(2022,1,1,0,0,0){==} → 1640995200";
  1114. }
  1115. if (day < 1 || day > 31) {
  1116. return "ERROR: make_timestamp() day must be 1-31, got " + args[2] + ". Usage: [[make_timestamp(2022,1,1,0,0,0){==} → 1640995200";
  1117. }
  1118. if (hour < 0 || hour > 23) {
  1119. return "ERROR: make_timestamp() hour must be 0-23, got " + args[3] + ". Usage: [[make_timestamp(2022,1,1,0,0,0){==} → 1640995200";
  1120. }
  1121. if (minute < 0 || minute > 59) {
  1122. return "ERROR: make_timestamp() minute must be 0-59, got " + args[4] + ". Usage: [[make_timestamp(2022,1,1,0,0,0){==} → 1640995200";
  1123. }
  1124. if (second < 0 || second > 59) {
  1125. return "ERROR: make_timestamp() second must be 0-59, got " + args[5] + ". Usage: [[make_timestamp(2022,1,1,0,0,0){==} → 1640995200";
  1126. }
  1127. time_info.tm_year = static_cast<int>(year) - 1900;
  1128. time_info.tm_mon = static_cast<int>(month) - 1;
  1129. time_info.tm_mday = static_cast<int>(day);
  1130. time_info.tm_hour = static_cast<int>(hour);
  1131. time_info.tm_min = static_cast<int>(minute);
  1132. time_info.tm_sec = static_cast<int>(second);
  1133. std::time_t timestamp = timegm(&time_info);
  1134. return std::to_string(timestamp);
  1135. } catch (...) {
  1136. return "ERROR: make_timestamp() arguments must be numbers (y,m,d,h,m,s). Got: '" + args[0] + "', '" + args[1] + "', '" + args[2] + "', '" + args[3] + "', '" + args[4] + "', '" + args[5] + "'. Usage: [[make_timestamp(2022,1,1,0,0,0){==} → 1640995200";
  1137. }
  1138. }, 6);
  1139. // ===== STRING & ENCODING UTILITIES =====
  1140. // Base64 encode
  1141. register_tool("base64_encode", [](const std::vector<std::string>& args) -> std::string {
  1142. if (args.size() != 1) {
  1143. return "ERROR: base64_encode() requires exactly 1 argument, got " + std::to_string(args.size()) + ". Usage: [[base64_encode('Hello'){==} → 'SGVsbG8='";
  1144. }
  1145. try {
  1146. return base64::encode(args[0]);
  1147. } catch (const std::exception& e) {
  1148. return "ERROR: base64_encode() failed: " + std::string(e.what()) + ". Usage: [[base64_encode('Hello'){==} → 'SGVsbG8='";
  1149. }
  1150. }, 1);
  1151. // Base64 decode
  1152. register_tool("base64_decode", [](const std::vector<std::string>& args) -> std::string {
  1153. if (args.size() != 1) {
  1154. return "ERROR: base64_decode() requires exactly 1 argument, got " + std::to_string(args.size()) + ". Usage: [[base64_decode('SGVsbG8='){==} → 'Hello'";
  1155. }
  1156. try {
  1157. return base64::decode(args[0]);
  1158. } catch (const base64_error& e) {
  1159. return "ERROR: base64_decode() invalid input: " + std::string(e.what()) + ". Input must be valid base64. Usage: [[base64_decode('SGVsbG8='){==} → 'Hello'";
  1160. } catch (const std::exception& e) {
  1161. return "ERROR: base64_decode() failed: " + std::string(e.what()) + ". Usage: [[base64_decode('SGVsbG8='){==} → 'Hello'";
  1162. }
  1163. }, 1);
  1164. // String length
  1165. register_tool("strlen", [](const std::vector<std::string>& args) -> std::string {
  1166. if (args.size() != 1) {
  1167. return "ERROR: strlen() requires exactly 1 argument, got " + std::to_string(args.size()) + ". Usage: [[strlen('Hello'){==} → 5";
  1168. }
  1169. return std::to_string(args[0].length());
  1170. }, 1);
  1171. // Substring (string, start, length)
  1172. register_tool("substring", [](const std::vector<std::string>& args) -> std::string {
  1173. if (args.size() != 3) {
  1174. return "ERROR: substring() requires exactly 3 arguments (string, start, length), got " + std::to_string(args.size()) + ". Usage: [[substring('Hello',0,3){==} → 'Hel'";
  1175. }
  1176. try {
  1177. const std::string& str = args[0];
  1178. size_t start = std::stoul(args[1]);
  1179. size_t length = std::stoul(args[2]);
  1180. if (start >= str.length()) {
  1181. return "ERROR: substring() start position " + args[1] + " is beyond string length " + std::to_string(str.length()) + ". Usage: [[substring('Hello',0,3){==} → 'Hel'";
  1182. }
  1183. return str.substr(start, length);
  1184. } catch (...) {
  1185. return "ERROR: substring() start and length must be numbers. Usage: [[substring('Hello',0,3){==} → 'Hel'";
  1186. }
  1187. }, 3);
  1188. // String replace (string, old, new)
  1189. register_tool("str_replace", [](const std::vector<std::string>& args) -> std::string {
  1190. if (args.size() != 3) {
  1191. return "ERROR: str_replace() requires exactly 3 arguments (string, old, new), got " + std::to_string(args.size()) + ". Usage: [[str_replace('Hello','l','L'){==} → 'HeLLo'";
  1192. }
  1193. try {
  1194. std::string result = args[0];
  1195. const std::string& old_str = args[1];
  1196. const std::string& new_str = args[2];
  1197. size_t pos = 0;
  1198. while ((pos = result.find(old_str, pos)) != std::string::npos) {
  1199. result.replace(pos, old_str.length(), new_str);
  1200. pos += new_str.length();
  1201. }
  1202. return result;
  1203. } catch (...) { return "ERROR"; }
  1204. }, 3);
  1205. // Uppercase
  1206. register_tool("uppercase", [](const std::vector<std::string>& args) -> std::string {
  1207. if (args.size() != 1) {
  1208. return "ERROR: uppercase() requires exactly 1 argument, got " + std::to_string(args.size()) + ". Usage: [[uppercase('hello'){==} → 'HELLO'. Tip: Use quotes for strings with spaces.";
  1209. }
  1210. std::string result = args[0];
  1211. std::transform(result.begin(), result.end(), result.begin(), ::toupper);
  1212. return result;
  1213. }, 1);
  1214. // Lowercase
  1215. register_tool("lowercase", [](const std::vector<std::string>& args) -> std::string {
  1216. if (args.size() != 1) {
  1217. return "ERROR: lowercase() requires exactly 1 argument, got " + std::to_string(args.size()) + ". Usage: [[lowercase('HELLO'){==} → 'hello'. Tip: Use quotes for strings with spaces.";
  1218. }
  1219. std::string result = args[0];
  1220. std::transform(result.begin(), result.end(), result.begin(), ::tolower);
  1221. return result;
  1222. }, 1);
  1223. // String contains (haystack, needle) - returns "true" or "false"
  1224. register_tool("str_contains", [](const std::vector<std::string>& args) -> std::string {
  1225. if (args.size() != 2) {
  1226. return "ERROR: str_contains() requires exactly 2 arguments (string, substring), got " + std::to_string(args.size()) + ". Usage: [[str_contains('Hello, World','World'){==} → 'true'. Tip: Use quotes if string has commas/spaces.";
  1227. }
  1228. return args[0].find(args[1]) != std::string::npos ? "true" : "false";
  1229. }, 2);
  1230. // String starts with
  1231. register_tool("str_startswith", [](const std::vector<std::string>& args) -> std::string {
  1232. if (args.size() != 2) {
  1233. return "ERROR: str_startswith() requires exactly 2 arguments (string, prefix), got " + std::to_string(args.size()) + ". Usage: [[str_startswith('Hello','Hel'){==} → 'true'. Tip: Use quotes for strings.";
  1234. }
  1235. const std::string& str = args[0];
  1236. const std::string& prefix = args[1];
  1237. return str.size() >= prefix.size() &&
  1238. str.compare(0, prefix.size(), prefix) == 0 ? "true" : "false";
  1239. }, 2);
  1240. // String ends with
  1241. register_tool("str_endswith", [](const std::vector<std::string>& args) -> std::string {
  1242. if (args.size() != 2) {
  1243. return "ERROR: str_endswith() requires exactly 2 arguments (string, suffix), got " + std::to_string(args.size()) + ". Usage: [[str_endswith('Hello','lo'){==} → 'true'. Tip: Use quotes for strings.";
  1244. }
  1245. const std::string& str = args[0];
  1246. const std::string& suffix = args[1];
  1247. return str.size() >= suffix.size() &&
  1248. str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0 ? "true" : "false";
  1249. }, 2);
  1250. // URL encode
  1251. register_tool("url_encode", [](const std::vector<std::string>& args) -> std::string {
  1252. if (args.size() != 1) return "ERROR";
  1253. try {
  1254. std::string result;
  1255. const std::string& input = args[0];
  1256. for (unsigned char c : input) {
  1257. if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') {
  1258. result += c;
  1259. } else {
  1260. char hex[4];
  1261. snprintf(hex, sizeof(hex), "%%%02X", c);
  1262. result += hex;
  1263. }
  1264. }
  1265. return result;
  1266. } catch (...) { return "ERROR"; }
  1267. }, 1);
  1268. // URL decode
  1269. register_tool("url_decode", [](const std::vector<std::string>& args) -> std::string {
  1270. if (args.size() != 1) return "ERROR";
  1271. try {
  1272. std::string result;
  1273. const std::string& input = args[0];
  1274. for (size_t i = 0; i < input.length(); i++) {
  1275. if (input[i] == '%' && i + 2 < input.length()) {
  1276. unsigned int value;
  1277. sscanf(input.substr(i + 1, 2).c_str(), "%x", &value);
  1278. result += static_cast<char>(value);
  1279. i += 2;
  1280. } else if (input[i] == '+') {
  1281. result += ' ';
  1282. } else {
  1283. result += input[i];
  1284. }
  1285. }
  1286. return result;
  1287. } catch (...) { return "ERROR"; }
  1288. }, 1);
  1289. // Simple hash (djb2 algorithm) - for basic string hashing
  1290. register_tool("hash_string", [](const std::vector<std::string>& args) -> std::string {
  1291. if (args.size() != 1) return "ERROR";
  1292. unsigned long hash = 5381;
  1293. for (char c : args[0]) {
  1294. hash = ((hash << 5) + hash) + static_cast<unsigned char>(c);
  1295. }
  1296. return std::to_string(hash);
  1297. }, 1);
  1298. // Random integer (min, max inclusive)
  1299. register_tool("random_int", [](const std::vector<std::string>& args) -> std::string {
  1300. if (args.size() != 2) return "ERROR: random_int() requires exactly 2 arguments (min, max), got " + std::to_string(args.size()) + ". Usage: [[random_int(1,100){==} → 42";
  1301. try {
  1302. long long min = std::stoll(args[0]);
  1303. long long max = std::stoll(args[1]);
  1304. if (min > max) return "ERROR: random_int() min > max. Got min=" + args[0] + ", max=" + args[1] + ". Usage: [[random_int(1,100){==} → 42";
  1305. static std::random_device rd;
  1306. static std::mt19937_64 gen(rd());
  1307. std::uniform_int_distribution<long long> dis(min, max);
  1308. return std::to_string(dis(gen));
  1309. } catch (const std::out_of_range&) {
  1310. return "ERROR: random_int() arguments out of range. Values must be within 64-bit range. Got: '" + args[0] + "', '" + args[1] + "'. Usage: [[random_int(1,100){==} → 42";
  1311. } catch (const std::invalid_argument&) {
  1312. return "ERROR: random_int() arguments must be integers. Got: '" + args[0] + "', '" + args[1] + "'. Usage: [[random_int(1,100){==} → 42";
  1313. } catch (...) {
  1314. return "ERROR: random_int() failed with arguments: '" + args[0] + "', '" + args[1] + "'. Usage: [[random_int(1,100){==} → 42";
  1315. }
  1316. }, 2);
  1317. // Generate UUID (simplified v4)
  1318. register_tool("uuid", [](const std::vector<std::string>& args) -> std::string {
  1319. if (args.size() != 0) return "ERROR";
  1320. try {
  1321. static std::random_device rd;
  1322. static std::mt19937 gen(rd());
  1323. static std::uniform_int_distribution<> dis(0, 15);
  1324. static std::uniform_int_distribution<> dis2(8, 11);
  1325. std::stringstream ss;
  1326. ss << std::hex;
  1327. for (int i = 0; i < 8; i++) ss << dis(gen);
  1328. ss << "-";
  1329. for (int i = 0; i < 4; i++) ss << dis(gen);
  1330. ss << "-4"; // version 4
  1331. for (int i = 0; i < 3; i++) ss << dis(gen);
  1332. ss << "-";
  1333. ss << dis2(gen); // variant
  1334. for (int i = 0; i < 3; i++) ss << dis(gen);
  1335. ss << "-";
  1336. for (int i = 0; i < 12; i++) ss << dis(gen);
  1337. return ss.str();
  1338. } catch (...) {
  1339. return "ERROR: uuid() failed to generate unique ID. Usage: [[uuid(){==} → '123e4567-e89b-12d3-a456-426614174000'";
  1340. }
  1341. }, 0);
  1342. // Regex match (returns "true" or "false")
  1343. register_tool("regex_match", [](const std::vector<std::string>& args) -> std::string {
  1344. if (args.size() != 2) {
  1345. return "ERROR: regex_match() requires exactly 2 arguments (string, pattern), got " + std::to_string(args.size()) + ". Usage: [[regex_match('hello','^h.*o$'){==} → 'true'";
  1346. }
  1347. try {
  1348. std::regex pattern(args[1]);
  1349. return std::regex_search(args[0], pattern) ? "true" : "false";
  1350. } catch (const std::regex_error& e) {
  1351. return "ERROR: regex_match() invalid pattern: '" + args[1] + "' - " + e.what() + ". Usage: [[regex_match('hello','^h.*o$'){==} → 'true'";
  1352. } catch (...) {
  1353. return "ERROR: regex_match() failed. Usage: [[regex_match('hello','^h.*o$'){==} → 'true'";
  1354. }
  1355. }, 2);
  1356. // Split string by delimiter (returns count of parts)
  1357. register_tool("str_split_count", [](const std::vector<std::string>& args) -> std::string {
  1358. if (args.size() != 2) {
  1359. return "ERROR: str_split_count() requires exactly 2 arguments (string, delimiter), got " + std::to_string(args.size()) + ". Usage: [[str_split_count('a,b,c',','){==} → 3";
  1360. }
  1361. try {
  1362. const std::string& str = args[0];
  1363. const std::string& delim = args[1];
  1364. if (str.empty()) return "0";
  1365. if (delim.empty()) return "1";
  1366. size_t count = 1;
  1367. size_t pos = 0;
  1368. while ((pos = str.find(delim, pos)) != std::string::npos) {
  1369. count++;
  1370. pos += delim.length();
  1371. }
  1372. return std::to_string(count);
  1373. } catch (...) {
  1374. return "ERROR: str_split_count() failed. Usage: [[str_split_count('a,b,c',','){==} → 3";
  1375. }
  1376. }, 2);
  1377. // Count occurrences of a character/substring in a string
  1378. register_tool("str_count_char", [](const std::vector<std::string>& args) -> std::string {
  1379. if (args.size() != 2) {
  1380. return "ERROR: str_count_char() requires exactly 2 arguments (string, character), got " + std::to_string(args.size()) + ". Usage: [[str_count_char('Hello','l'){==} → 2. Tip: Use quotes for single chars like 'p'";
  1381. }
  1382. try {
  1383. const std::string& str = args[0];
  1384. const std::string& target = args[1];
  1385. if (target.empty()) {
  1386. return "ERROR: str_count_char() target character cannot be empty. Usage: [[str_count_char('test','t'){==} → 2";
  1387. }
  1388. if (str.empty()) return "0";
  1389. size_t count = 0;
  1390. size_t pos = 0;
  1391. while ((pos = str.find(target, pos)) != std::string::npos) {
  1392. count++;
  1393. pos += target.length();
  1394. }
  1395. return std::to_string(count);
  1396. } catch (...) {
  1397. return "ERROR: str_count_char() failed. Usage: [[str_count_char('Hello','l'){==} → 2. Remember: use quotes for single characters!";
  1398. }
  1399. }, 2);
  1400. // MEMORY MANAGEMENT - Persistent user information storage
  1401. // Memory file path (max 5KB)
  1402. const std::string memory_file = "llama_memory.txt";
  1403. const size_t MAX_MEMORY_SIZE = 5 * 1024; // 5KB
  1404. // Read entire memory contents
  1405. register_tool("memory_read", [memory_file](const std::vector<std::string>& args) -> std::string {
  1406. if (args.size() != 0) return "ERROR";
  1407. try {
  1408. std::ifstream file(memory_file);
  1409. if (!file.is_open()) return "[Empty - No memory saved yet]";
  1410. std::stringstream buffer;
  1411. buffer << file.rdbuf();
  1412. std::string content = buffer.str();
  1413. if (content.empty()) return "[Empty - No memory saved yet]";
  1414. return content;
  1415. } catch (...) { return "ERROR"; }
  1416. }, 0);
  1417. // Save/overwrite entire memory (replaces all content)
  1418. register_tool("memory_save", [memory_file, MAX_MEMORY_SIZE](const std::vector<std::string>& args) -> std::string {
  1419. if (args.size() != 1) return "ERROR";
  1420. try {
  1421. const std::string& content = args[0];
  1422. // Check size limit
  1423. if (content.size() > MAX_MEMORY_SIZE) {
  1424. return "ERROR: Memory size exceeds 5KB limit";
  1425. }
  1426. std::ofstream file(memory_file, std::ios::trunc);
  1427. if (!file.is_open()) return "ERROR: Cannot write to memory file";
  1428. file << content;
  1429. file.close();
  1430. return "Memory saved successfully";
  1431. } catch (...) { return "ERROR"; }
  1432. }, 1);
  1433. // Append text to memory
  1434. register_tool("memory_append", [memory_file, MAX_MEMORY_SIZE](const std::vector<std::string>& args) -> std::string {
  1435. if (args.size() != 1) return "ERROR";
  1436. try {
  1437. const std::string& new_content = args[0];
  1438. // Read current content
  1439. std::string current_content;
  1440. std::ifstream infile(memory_file);
  1441. if (infile.is_open()) {
  1442. std::stringstream buffer;
  1443. buffer << infile.rdbuf();
  1444. current_content = buffer.str();
  1445. infile.close();
  1446. }
  1447. // Add newline if current content doesn't end with one
  1448. if (!current_content.empty() && current_content.back() != '\n') {
  1449. current_content += "\n";
  1450. }
  1451. current_content += new_content;
  1452. // Check size limit
  1453. if (current_content.size() > MAX_MEMORY_SIZE) {
  1454. return "ERROR: Memory would exceed 5KB limit";
  1455. }
  1456. // Write back
  1457. std::ofstream outfile(memory_file, std::ios::trunc);
  1458. if (!outfile.is_open()) return "ERROR: Cannot write to memory file";
  1459. outfile << current_content;
  1460. outfile.close();
  1461. return "Content appended to memory";
  1462. } catch (...) { return "ERROR"; }
  1463. }, 1);
  1464. // Clear all memory
  1465. register_tool("memory_clear", [memory_file](const std::vector<std::string>& args) -> std::string {
  1466. if (args.size() != 0) return "ERROR";
  1467. try {
  1468. std::ofstream file(memory_file, std::ios::trunc);
  1469. if (!file.is_open()) return "ERROR: Cannot write to memory file";
  1470. file.close();
  1471. return "Memory cleared successfully";
  1472. } catch (...) { return "ERROR"; }
  1473. }, 0);
  1474. // Replace text in memory
  1475. register_tool("memory_replace", [memory_file, MAX_MEMORY_SIZE](const std::vector<std::string>& args) -> std::string {
  1476. if (args.size() != 2) return "ERROR";
  1477. try {
  1478. const std::string& old_text = args[0];
  1479. const std::string& new_text = args[1];
  1480. // Read current content
  1481. std::ifstream infile(memory_file);
  1482. if (!infile.is_open()) return "ERROR: No memory file found";
  1483. std::stringstream buffer;
  1484. buffer << infile.rdbuf();
  1485. std::string content = buffer.str();
  1486. infile.close();
  1487. // Find and replace
  1488. size_t pos = content.find(old_text);
  1489. if (pos == std::string::npos) {
  1490. return "ERROR: Text not found in memory";
  1491. }
  1492. content.replace(pos, old_text.length(), new_text);
  1493. // Check size limit
  1494. if (content.size() > MAX_MEMORY_SIZE) {
  1495. return "ERROR: Memory would exceed 5KB limit";
  1496. }
  1497. // Write back
  1498. std::ofstream outfile(memory_file, std::ios::trunc);
  1499. if (!outfile.is_open()) return "ERROR: Cannot write to memory file";
  1500. outfile << content;
  1501. outfile.close();
  1502. return "Memory updated successfully";
  1503. } catch (...) { return "ERROR"; }
  1504. }, 2);
  1505. // Remove specific line from memory (1-indexed)
  1506. register_tool("memory_remove_line", [memory_file](const std::vector<std::string>& args) -> std::string {
  1507. if (args.size() != 1) return "ERROR";
  1508. try {
  1509. long long line_num = std::stoll(args[0]);
  1510. if (line_num < 1) return "ERROR: Line number must be >= 1";
  1511. if (line_num > INT_MAX) return "ERROR: Line number too large, got " + args[0];
  1512. // Read all lines
  1513. std::ifstream infile(memory_file);
  1514. if (!infile.is_open()) return "ERROR: No memory file found";
  1515. std::vector<std::string> lines;
  1516. std::string line;
  1517. while (std::getline(infile, line)) {
  1518. lines.push_back(line);
  1519. }
  1520. infile.close();
  1521. // Check if line exists
  1522. if (line_num > static_cast<int>(lines.size())) {
  1523. return "ERROR: Line number out of range";
  1524. }
  1525. // Remove line (convert from 1-indexed to 0-indexed)
  1526. lines.erase(lines.begin() + (line_num - 1));
  1527. // Write back
  1528. std::ofstream outfile(memory_file, std::ios::trunc);
  1529. if (!outfile.is_open()) return "ERROR: Cannot write to memory file";
  1530. for (size_t i = 0; i < lines.size(); ++i) {
  1531. outfile << lines[i];
  1532. if (i < lines.size() - 1) outfile << "\n";
  1533. }
  1534. outfile.close();
  1535. return "Line removed from memory";
  1536. } catch (...) { return "ERROR"; }
  1537. }, 1);
  1538. // Get line count in memory
  1539. register_tool("memory_line_count", [memory_file](const std::vector<std::string>& args) -> std::string {
  1540. if (args.size() != 0) return "ERROR";
  1541. try {
  1542. std::ifstream file(memory_file);
  1543. if (!file.is_open()) return "0";
  1544. int count = 0;
  1545. std::string line;
  1546. while (std::getline(file, line)) {
  1547. count++;
  1548. }
  1549. file.close();
  1550. return std::to_string(count);
  1551. } catch (...) { return "ERROR"; }
  1552. }, 0);
  1553. }
  1554. void register_tool(const std::string& name, InlineToolCallback callback, int num_args) {
  1555. tools[name] = {name, callback, num_args};
  1556. }
  1557. // Returns empty string if no match or incomplete
  1558. // Returns result string if matched and executed
  1559. // Buffer should be the recent context ending with "{==}"
  1560. // Format expected: [[tool_name(arg1, arg2){==}
  1561. // Returns: [KERNEL_ANSWER: result]
  1562. std::string check_and_execute(const std::string& buffer) {
  1563. // Simple parser
  1564. // Find the last "[["
  1565. size_t open_pos = buffer.rfind("[[");
  1566. if (open_pos == std::string::npos) return "";
  1567. // Check if we have "{==}" at the end (or close to it, allowing for whitespace)
  1568. size_t eq_pos = buffer.rfind("{==}");
  1569. if (eq_pos == std::string::npos || eq_pos < open_pos) return "";
  1570. // Extract the content between [[ and {==}
  1571. std::string content = buffer.substr(open_pos + 2, eq_pos - (open_pos + 2));
  1572. std::cerr << "[DEBUG] Tool content found: '" << content << "'" << std::endl;
  1573. // Parse tool name and args
  1574. // Content should be "name(arg1,arg2)"
  1575. size_t paren_open = content.find("(");
  1576. size_t paren_close = content.rfind(")");
  1577. if (paren_open == std::string::npos || paren_close == std::string::npos || paren_close < paren_open) {
  1578. std::cerr << "[DEBUG] Malformed tool call: Missing parentheses" << std::endl;
  1579. return "";
  1580. }
  1581. std::string name = content.substr(0, paren_open);
  1582. // trim name
  1583. name.erase(0, name.find_first_not_of(" \t\n\r\f\v"));
  1584. name.erase(name.find_last_not_of(" \t\n\r\f\v") + 1);
  1585. std::cerr << "[DEBUG] Tool name: '" << name << "'" << std::endl;
  1586. if (tools.find(name) == tools.end()) {
  1587. std::cerr << "[DEBUG] Unknown tool: '" << name << "'" << std::endl;
  1588. return "[KERNEL_ANSWER: ERROR - Unknown command '" + name + "'. Check available commands in system prompt.]";
  1589. }
  1590. std::string args_str = content.substr(paren_open + 1, paren_close - (paren_open + 1));
  1591. std::vector<std::string> args;
  1592. // Quote-aware argument parsing
  1593. // Handles: strings with commas, quotes, multi-line, spaces
  1594. std::string current_arg;
  1595. char quote_char = 0; // 0 = not in quote, '\'' or '"' = in quote
  1596. bool escaped = false;
  1597. for (size_t i = 0; i < args_str.length(); i++) {
  1598. char c = args_str[i];
  1599. if (escaped) {
  1600. current_arg += c;
  1601. escaped = false;
  1602. continue;
  1603. }
  1604. if (c == '\\') {
  1605. escaped = true;
  1606. continue;
  1607. }
  1608. // Handle quotes
  1609. if (c == '\'' || c == '"') {
  1610. if (quote_char == 0) {
  1611. // Start of quoted string - don't include the quote
  1612. quote_char = c;
  1613. } else if (c == quote_char) {
  1614. // End of quoted string - don't include the quote
  1615. quote_char = 0;
  1616. } else {
  1617. // Different quote inside quoted string
  1618. current_arg += c;
  1619. }
  1620. continue;
  1621. }
  1622. // Comma delimiter (only if not inside quotes)
  1623. if (c == ',' && quote_char == 0) {
  1624. // Trim and add argument
  1625. size_t first = current_arg.find_first_not_of(" \t\n\r\f\v");
  1626. if (first != std::string::npos) {
  1627. size_t last = current_arg.find_last_not_of(" \t\n\r\f\v");
  1628. args.push_back(current_arg.substr(first, (last - first + 1)));
  1629. }
  1630. current_arg.clear();
  1631. continue;
  1632. }
  1633. // Regular character
  1634. current_arg += c;
  1635. }
  1636. // Add last argument
  1637. if (!current_arg.empty() || args_str.empty()) {
  1638. size_t first = current_arg.find_first_not_of(" \t\n\r\f\v");
  1639. if (first != std::string::npos) {
  1640. size_t last = current_arg.find_last_not_of(" \t\n\r\f\v");
  1641. args.push_back(current_arg.substr(first, (last - first + 1)));
  1642. }
  1643. }
  1644. std::cerr << "[DEBUG] Args count: " << args.size() << std::endl;
  1645. for(const auto& a : args) std::cerr << "[DEBUG] Arg: '" << a << "'" << std::endl;
  1646. const auto& tool = tools[name];
  1647. // For variable args (num_args = -1), check min 1 arg (or specific logic in callback)
  1648. if (tool.num_args != -1 && args.size() != (size_t)tool.num_args) {
  1649. std::cerr << "[DEBUG] Arg count mismatch. Expected " << tool.num_args << ", got " << args.size() << std::endl;
  1650. return "[KERNEL_ANSWER: ERROR - Command '" + name + "' requires " + std::to_string(tool.num_args) +
  1651. " arguments, but got " + std::to_string(args.size()) + ". Check command syntax.]";
  1652. }
  1653. // Execute
  1654. std::string result = tool.callback(args);
  1655. std::cerr << "[DEBUG] Tool result: " << result << std::endl;
  1656. // Format: [KERNEL_ANSWER: result]
  1657. // Inject a newline to clearly end the tool output line.
  1658. return "[KERNEL_ANSWER: " + result + "]\n";
  1659. }
  1660. };