diff --git a/lib/ExpandFetcherCommands.functions.php b/lib/ExpandFetcherCommands.functions.php new file mode 100644 index 0000000..746b7bf --- /dev/null +++ b/lib/ExpandFetcherCommands.functions.php @@ -0,0 +1,57 @@ +function_name = $s; return $this; } + public function setArgumentsTypes(array $a) { $this->args_type = $a; $this->no_of_args = count($a); return $this; } + public function getExpectedNoOfArguments() { return $this->no_of_args; } + public function getFunctionName() { return $this->function_name; } +} +function registerAction(Action $a) { global $actions; $actions[$a->getFunctionName()] = $a; return true;} +// Register all actions available +require_once __DIR__."/actions/index.php"; + +function ExpandFetcherCommands(string $text) { + global $actions; + + // Make it recursive, while you have parenthesis + while(preg_match("/\(([^()]+)\)/",$text, $matches)) + $text = str_replace($matches[0],ExpandFetcherCommands($matches[1]),$text); + + + $function_name_regex_format = "@([a-zA-Z_][a-zA-Z0-9_]+)"; + $arguments_regex_format = "([\w:\/\.-]+)"; + + + if (preg_match("/$function_name_regex_format( |$)/",$text,$matches)) { + $function_name = $matches[1]; + + if ( !isset($actions[$function_name]) || + !function_exists($function_name )) return "[[ Action $function_name not available. ]]"; + + $action = $actions[$function_name]; + + // Fetch the arguments + $reg_exp = array_fill(0,$action->getExpectedNoOfArguments(),"([\w:\/\.-]+)"); + array_unshift($reg_exp, $function_name_regex_format); + + if (!preg_match("/".implode(" ",$reg_exp)."/",$text,$matches)) + return "[[ Action $function_name was passed with wrong number of arguemnts. Expected: ".$a->getExpectedNoOfArguments()." ]]"; + + $full_action_requested_string = $matches[0]; + + array_shift($matches); // Clip the first whole-string result + array_shift($matches); // Clip the function name + $arguments = $matches; + $actionResult = call_user_func_array($function_name, $arguments); + + $text = str_replace($full_action_requested_string,$actionResult,$text); + } + + return $text; + +} diff --git a/lib/actions/Makefile b/lib/actions/Makefile new file mode 100644 index 0000000..a5a8360 --- /dev/null +++ b/lib/actions/Makefile @@ -0,0 +1,2 @@ +%.action.php: %.to-be.action + ~/llama.cpp/build/bin/llama-cli -m ~/llama.cpp/models/Meta-Llama-3.1-8B-Instruct-Q4_K_M.gguf -sys "User will pass a sample template of code, and you'll adapt to the following request: $(shell cat $< | tr "\n" " ")" -f action.sample.php -no-cnv 2> llama.err.log > $@ diff --git a/lib/actions/simple_echo.action.php b/lib/actions/simple_echo.action.php new file mode 100644 index 0000000..b000fe9 --- /dev/null +++ b/lib/actions/simple_echo.action.php @@ -0,0 +1,29 @@ +setFunctionName("echo_text") + ->setArgumentsTypes(["input"]) // one argument +); + +/** + * 2. Implement the function + * + * @param string $input + * @return string + */ +function echo_text(string $input): string { + return $input; +} diff --git a/tests/06_ResolveFetchingActions.php b/tests/06_ResolveFetchingActions.php deleted file mode 100644 index 7d86875..0000000 --- a/tests/06_ResolveFetchingActions.php +++ /dev/null @@ -1,12 +0,0 @@ -