php-agentic/Models/Prompt.php
darbs-claude 2bc17efa47
Some checks failed
CI / PHP 8.3 (pull_request) Failing after 1m11s
CI / PHP 8.4 (pull_request) Failing after 1m2s
refactor: add Builder return types to all Eloquent query scopes
Add `Builder $query` parameter type and `: Builder` return type to
18 query scopes across 8 model files. Import `Illuminate\Database\Eloquent\Builder`
in each affected model.

Affected models: Task, AgentSession, AgentApiKey, AgentPhase, AgentPlan,
Prompt, AgentWorkspaceState, WorkspaceState.

Closes #16

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-23 05:32:38 +00:00

106 lines
2.4 KiB
PHP

<?php
declare(strict_types=1);
namespace Core\Mod\Agentic\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Mod\Content\Models\ContentTask;
class Prompt extends Model
{
use HasFactory;
protected $fillable = [
'name',
'category',
'description',
'system_prompt',
'user_template',
'variables',
'model',
'model_config',
'is_active',
];
protected $casts = [
'variables' => 'array',
'model_config' => 'array',
'is_active' => 'boolean',
];
/**
* Get the version history for this prompt.
*/
public function versions(): HasMany
{
return $this->hasMany(PromptVersion::class)->orderByDesc('version');
}
/**
* Get the content tasks using this prompt.
*/
public function tasks(): HasMany
{
return $this->hasMany(ContentTask::class);
}
/**
* Create a new version snapshot before saving changes.
*/
public function createVersion(?int $userId = null): PromptVersion
{
$latestVersion = $this->versions()->max('version') ?? 0;
return $this->versions()->create([
'version' => $latestVersion + 1,
'system_prompt' => $this->system_prompt,
'user_template' => $this->user_template,
'variables' => $this->variables,
'created_by' => $userId,
]);
}
/**
* Interpolate variables into the user template.
*/
public function interpolate(array $data): string
{
$template = $this->user_template;
foreach ($data as $key => $value) {
if (is_string($value)) {
$template = str_replace("{{{$key}}}", $value, $template);
}
}
return $template;
}
/**
* Scope to only active prompts.
*/
public function scopeActive(Builder $query): Builder
{
return $query->where('is_active', true);
}
/**
* Scope by category.
*/
public function scopeCategory(Builder $query, string $category): Builder
{
return $query->where('category', $category);
}
/**
* Scope by model provider.
*/
public function scopeForModel(Builder $query, string $model): Builder
{
return $query->where('model', $model);
}
}