php-llm-agent/lib/actions/web.action.php
2025-09-18 15:42:38 +00:00

39 lines
892 B
PHP

<?php
/**
* ACTION: Web fetcher
* -------------------
* Call format:
* @web example.com
*
* Expected output:
* (Plain text content of the website via `lynx -dump -nolist`)
*/
// 1. Register the action with expected argument types
registerAction(
(new Action())
->setFunctionName("web")
->setArgumentsTypes(["website"]) // single argument: website URL
);
/**
* 2. Implement the function
*
* @param string $website
* @return string
*/
function web(string $website): string {
// Ensure URL has a scheme
if (!preg_match('~^https?://~i', $website)) {
$website = "https://$website";
}
// Escape for shell safety
$escaped = escapeshellarg($website);
// Run lynx and capture output
$output = shell_exec("lynx -dump -nolist $escaped 2>/dev/null | head -n 250");
return $output ?: "[error] Unable to fetch $website";
}