feat(阶段三): 实现AI提示词编辑功能
- 集成 Monaco Editor 用于提示词编辑 - 创建提示词变量配置(14个可用变量) - 创建提示词模板库(5个预设模板) - 实现 PromptTemplateService 服务类 - 创建变量替换和预览功能 - 添加 PreviewPromptAction 用于预览提示词 - 创建变量帮助文档和模板选择器视图组件 - 支持变量验证和自动替换
This commit is contained in:
91
app/Filament/Actions/PreviewPromptAction.php
Normal file
91
app/Filament/Actions/PreviewPromptAction.php
Normal file
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Actions;
|
||||
|
||||
use App\Services\PromptTemplateService;
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Forms\Components\Placeholder;
|
||||
use Filament\Forms\Components\ViewField;
|
||||
use Filament\Support\Enums\MaxWidth;
|
||||
|
||||
class PreviewPromptAction
|
||||
{
|
||||
/**
|
||||
* 创建预览提示词的Action
|
||||
*
|
||||
* @return Action
|
||||
*/
|
||||
public static function make(): Action
|
||||
{
|
||||
return Action::make('previewPrompt')
|
||||
->label('预览提示词')
|
||||
->icon('heroicon-o-eye')
|
||||
->color('info')
|
||||
->modalHeading('提示词预览')
|
||||
->modalDescription('查看变量替换后的实际提示词内容')
|
||||
->modalWidth(MaxWidth::FourExtraLarge)
|
||||
->modalSubmitAction(false)
|
||||
->modalCancelActionLabel('关闭')
|
||||
->form(function ($record, $livewire) {
|
||||
$service = app(PromptTemplateService::class);
|
||||
|
||||
// 获取当前表单数据
|
||||
$data = $livewire->data ?? [];
|
||||
$promptTemplate = $data['prompt']['prompt_template'] ?? '';
|
||||
|
||||
if (empty($promptTemplate)) {
|
||||
return [
|
||||
Placeholder::make('empty')
|
||||
->label('')
|
||||
->content('请先输入提示词模板内容'),
|
||||
];
|
||||
}
|
||||
|
||||
// 如果是编辑模式,使用记录的终端信息
|
||||
// 如果是创建模式,使用表单数据创建临时终端对象
|
||||
if ($record) {
|
||||
$terminal = $record;
|
||||
} else {
|
||||
// 创建临时终端对象用于预览
|
||||
$terminal = new \App\Models\Terminal([
|
||||
'name' => $data['name'] ?? '新终端',
|
||||
'code' => $data['code'] ?? 'TEMP-001',
|
||||
'station_id' => $data['station_id'] ?? null,
|
||||
]);
|
||||
}
|
||||
|
||||
// 替换变量
|
||||
$previewContent = $service->replaceVariables($promptTemplate, $terminal);
|
||||
|
||||
// 验证变量
|
||||
$invalidVariables = $service->validateVariables($promptTemplate);
|
||||
|
||||
return [
|
||||
Placeholder::make('original')
|
||||
->label('原始模板')
|
||||
->content(function () use ($promptTemplate) {
|
||||
return view('filament.components.prompt-preview-original', [
|
||||
'content' => $promptTemplate,
|
||||
]);
|
||||
}),
|
||||
|
||||
Placeholder::make('preview')
|
||||
->label('预览结果')
|
||||
->content(function () use ($previewContent) {
|
||||
return view('filament.components.prompt-preview-result', [
|
||||
'content' => $previewContent,
|
||||
]);
|
||||
}),
|
||||
|
||||
Placeholder::make('validation')
|
||||
->label('变量验证')
|
||||
->content(function () use ($invalidVariables) {
|
||||
return view('filament.components.prompt-preview-validation', [
|
||||
'invalidVariables' => $invalidVariables,
|
||||
]);
|
||||
})
|
||||
->visible(fn () => !empty($invalidVariables)),
|
||||
];
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -2,13 +2,14 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Spatie\Activitylog\Traits\LogsActivity;
|
||||
use Spatie\Activitylog\LogOptions;
|
||||
|
||||
class TerminalPrompt extends Model
|
||||
{
|
||||
use LogsActivity;
|
||||
use HasFactory, LogsActivity;
|
||||
/**
|
||||
* 可批量赋值的属性
|
||||
*
|
||||
|
||||
168
app/Services/PromptTemplateService.php
Normal file
168
app/Services/PromptTemplateService.php
Normal file
@@ -0,0 +1,168 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Terminal;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class PromptTemplateService
|
||||
{
|
||||
/**
|
||||
* 获取所有可用的模板
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getTemplates(): array
|
||||
{
|
||||
return config('prompt_templates.templates', []);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ID获取模板
|
||||
*
|
||||
* @param string $templateId
|
||||
* @return array|null
|
||||
*/
|
||||
public function getTemplate(string $templateId): ?array
|
||||
{
|
||||
$templates = $this->getTemplates();
|
||||
|
||||
foreach ($templates as $template) {
|
||||
if ($template['id'] === $templateId) {
|
||||
return $template;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 应用模板到终端
|
||||
*
|
||||
* @param Terminal $terminal
|
||||
* @param string $templateId
|
||||
* @return string
|
||||
*/
|
||||
public function applyTemplate(Terminal $terminal, string $templateId): string
|
||||
{
|
||||
$template = $this->getTemplate($templateId);
|
||||
|
||||
if (!$template) {
|
||||
throw new \InvalidArgumentException("模板不存在: {$templateId}");
|
||||
}
|
||||
|
||||
return $template['content'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 替换模板中的变量
|
||||
*
|
||||
* @param string $template
|
||||
* @param Terminal $terminal
|
||||
* @param array $additionalVars
|
||||
* @return string
|
||||
*/
|
||||
public function replaceVariables(string $template, Terminal $terminal, array $additionalVars = []): string
|
||||
{
|
||||
$variables = $this->getVariableValues($terminal, $additionalVars);
|
||||
|
||||
foreach ($variables as $key => $value) {
|
||||
// 处理数组类型的变量
|
||||
if (is_array($value)) {
|
||||
$value = implode(', ', $value);
|
||||
}
|
||||
|
||||
$template = str_replace('{' . $key . '}', $value, $template);
|
||||
}
|
||||
|
||||
return $template;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取变量的实际值
|
||||
*
|
||||
* @param Terminal $terminal
|
||||
* @param array $additionalVars
|
||||
* @return array
|
||||
*/
|
||||
public function getVariableValues(Terminal $terminal, array $additionalVars = []): array
|
||||
{
|
||||
$user = Auth::user();
|
||||
$now = now();
|
||||
|
||||
$variables = [
|
||||
'user' => $user?->name ?? '访客',
|
||||
'user_id' => $user?->id ?? 0,
|
||||
'user_role' => $user?->roles?->first()?->name ?? '未知',
|
||||
'department' => $user?->department ?? '未知部门',
|
||||
|
||||
'station' => $terminal->station_id ? "工作站 {$terminal->station_id}" : '未绑定工作站',
|
||||
'station_id' => $terminal->station_id ?? '未绑定',
|
||||
|
||||
'terminal_name' => $terminal->name,
|
||||
'terminal_code' => $terminal->code,
|
||||
|
||||
'time' => $now->format('Y-m-d H:i:s'),
|
||||
'date' => $now->format('Y-m-d'),
|
||||
'time_only' => $now->format('H:i:s'),
|
||||
'shift' => $this->getCurrentShift($now),
|
||||
|
||||
'knowledge_bases' => $terminal->knowledgeBases->pluck('name')->toArray(),
|
||||
|
||||
'company_name' => config('app.name', '公司名称'),
|
||||
];
|
||||
|
||||
return array_merge($variables, $additionalVars);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据时间判断当前班次
|
||||
*
|
||||
* @param \Illuminate\Support\Carbon $time
|
||||
* @return string
|
||||
*/
|
||||
protected function getCurrentShift($time): string
|
||||
{
|
||||
$hour = $time->hour;
|
||||
|
||||
if ($hour >= 8 && $hour < 16) {
|
||||
return '早班';
|
||||
} elseif ($hour >= 16 && $hour < 24) {
|
||||
return '中班';
|
||||
} else {
|
||||
return '夜班';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 预览模板(替换变量后的结果)
|
||||
*
|
||||
* @param string $template
|
||||
* @param Terminal $terminal
|
||||
* @return string
|
||||
*/
|
||||
public function preview(string $template, Terminal $terminal): string
|
||||
{
|
||||
return $this->replaceVariables($template, $terminal);
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证模板中的变量是否都是有效的
|
||||
*
|
||||
* @param string $template
|
||||
* @return array 返回无效的变量列表
|
||||
*/
|
||||
public function validateVariables(string $template): array
|
||||
{
|
||||
$validVariables = array_column(config('prompt_variables.variables', []), 'name');
|
||||
|
||||
// 提取模板中的所有变量
|
||||
preg_match_all('/\{([a-z_]+)\}/', $template, $matches);
|
||||
$usedVariables = $matches[1] ?? [];
|
||||
|
||||
// 找出无效的变量
|
||||
$invalidVariables = array_diff($usedVariables, $validVariables);
|
||||
|
||||
return array_values(array_unique($invalidVariables));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user