setAiContext() stores module-specific context for the AI assistant.
Purpose
setAiContext() stores module-specific context for the AI assistant. It lets a controller customize Aksara Core behavior while keeping the request inside the built-in CRUD, rendering, permission, validation, and response pipeline.
When to Use
Use it near the beginning of a controller method to configure how Core handles the current request.
Reference
setAiContext(array $context)
Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
$context | array | Yes | - | AI context array containing scope, instructions, max tokens, data, tone, or audience. |
Return Value
static
Returns the current controller instance so it can be chained with other Core methods.
Behavior
setAiContext() stores request-level configuration on the controller. Call it before the permission, rendering, or form-processing step that depends on it.
[!NOTE]
AI context is cached per route for the current module flow. Keep the provided data small and avoid secrets.
Basic Usage
$this->setAiContext(['scope' => 'product', 'instructions' => 'Use concise product descriptions.', 'max_tokens' => 2048]);
return $this->render('orders');
Advanced Usage
$this->setTitle(phrase('Orders'))
->setIcon('mdi mdi-cart-outline')
->setPermission();
Complete Example
namespace Modules\Orders\Controllers;
use Aksara\Laboratory\Core;
class Orders extends Core
{
public function index()
{
$this->setTitle(phrase('Orders'))
->setAiContext(['scope' => 'product', 'instructions' => 'Use concise product descriptions.', 'max_tokens' => 2048]);
return $this->render('orders');
}
}
Result
The controller stores the configuration and applies it later in the current request lifecycle.
Notes
- This method is chainable and returns the current controller instance.
- Call configuration methods before
setPermission()orrender()when those steps depend on the configured value.
Common Mistakes
- Calling the method after the permission or render step that already needed it.
- Spreading related configuration across distant parts of the controller.
