1. Go to this page and download the library: Download hudhaifas/silverstripe-ai library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
hudhaifas / silverstripe-ai example snippets
use Hudhaifas\AI\Agent\DataObjectAgent;
use NeuronAI\Agent\Middleware\ToolApproval;
use NeuronAI\Agent\Nodes\ToolNode;
class ProductAgent extends DataObjectAgent
{
// Cached by Anthropic for 5 min — put stable rules and tool descriptions here.
// For OpenAI this is just the first part of the system prompt.
protected function getStaticInstructions(): string
{
return 'You are a helpful assistant for managing products.
Use the available tools to read and update product data.
Always confirm with the user before making changes.';
}
// Injected fresh on every request — current entity state, date, session info.
// Not cached, so keep it concise.
protected function getDynamicContext(): string
{
$product = $this->contextEntity; // the DataObject passed from the controller
return "Current product: {$product->Title} (ID: {$product->ID})\n"
. "Price: {$product->Price}\n"
. "Stock: {$product->Stock}";
}
// All tools the LLM can call. Read-only tools run immediately;
// write tools are gated by ToolApproval in middleware() below.
public function tools(): array
{
return [
new GetProductDetailsTool(), // read — runs freely
new UpdateProductPriceTool(), // write —
use NeuronAI\Tools\Tool;
use NeuronAI\Tools\PropertyType;
use NeuronAI\Tools\ToolProperty;
class GetProductDetailsTool extends Tool
{
public function __construct()
{
parent::__construct(
name: 'GetProductDetails',
description: 'Get full details of a product including price, stock, and description.',
properties: [
new ToolProperty(
name: 'product_id',
type: PropertyType::INTEGER,
description: 'The ID of the product to retrieve.',
}
}
use NeuronAI\Tools\Tool;
use NeuronAI\Tools\PropertyType;
use NeuronAI\Tools\ToolProperty;
use SilverStripe\Security\Security;
class UpdateProductPriceTool extends Tool
{
public function __construct()
{
parent::__construct(
name: 'UpdateProductPrice',
description: 'Update the price of a product.',
properties: [
new ToolProperty('product_id', PropertyType::INTEGER, 'Product ID', true),
new ToolProperty('new_price', PropertyType::NUMBER, 'New price in dollars', true),
]
);
}
// Optional: human-readable summary for the HITL confirmation card
public static function summarise(array $inputs): string
{
$id = $inputs['product_id'] ?? '?';
$price = $inputs['new_price'] ?? '?';
return "Update product #{$id} price to \${$price}";
}
public function __invoke(int $product_id, float $new_price): string
{
$member = Security::getCurrentUser();
$product = Product::get()->byID($product_id);
if (!$product) {
return json_encode(['success' => false, 'error' => 'Product not found']);
}
if (!$product->canEdit($member)) {
return json_encode(['success' => false, 'error' => 'Permission denied']);
}
$oldPrice = $product->Price;
$product->Price = $new_price;
$product->write();
return json_encode([
'success' => true,
'product_id' => $product->ID,
'old_price' => $oldPrice,
'new_price' => $new_price,
]);
}
}
use Hudhaifas\AI\Extension\ContentExtension;
class ProductContentExtension extends ContentExtension
{
// Static prompt for the LLM — what kind of content to generate.
public function getStaticInstructions(): string
{
return 'Write a compelling product description in 2-3 paragraphs.
Focus on benefits, not just features. Use a friendly tone.';
}
// Dynamic context — the entity data the LLM uses to generate content.
public function getDynamicContext(): string
{
return "Product: {$this->owner->Title}\n"
. "Category: {$this->owner->Category()->Title}\n"
. "Features: {$this->owner->Features}\n"
. "Target audience: {$this->owner->TargetAudience}";
}
// Which DB field stores the generated content.
public function getContentField(): string
{
return 'Description';
}
// How to save the approved content.
public function saveContent(string $content): void
{
$this->owner->Description = $content;
$this->owner->IsAIGenerated = true;
$this->owner->write();
}
}
// In your PageController
public function ContentWidget()
{
$entity = $this->data(); // or fetch your DataObject
return $this->renderWith('Includes/ContentWidget', [
'EntityID' => $entity->ID,
'EntityClass' => $entity->ClassName,
'ContentField' => 'Description',
'ContentFieldValue' => $entity->Description,
'ContentFieldHTML' => DBField::create_field('HTMLText', $entity->Description),
'IsAIGenerated' => $entity->IsAIGenerated,
'canEdit' => $entity->canEdit(),
]);
}