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

35 lines
874 B
PHP

<?php
/**
* FETCH FILE CONTENTS ACTION
* -------------------------
* Demonstrates how to register and implement a simple action that fetches file contents.
*
* Call format:
* @fetchFileContents filename
*
* Expected output:
* The contents of the file with the specified filename
*/
// 1. Register the action with expected argument types
registerAction(
(new Action())
->setFunctionName("fetchFileContents")
->setArgumentsTypes(["filename"]) // filename
);
/**
* 2. Implement the function
*
* @param string $filename
* @return string
* @throws Exception If the file does not exist or cannot be read
*/
function fetchFileContents(string $filename): string {
try {
return file_get_contents($filename);
} catch (Exception $e) {
throw new Exception("Failed to fetch file contents: " . $e->getMessage());
}
}