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));
|
||||
}
|
||||
}
|
||||
222
config/prompt_templates.php
Normal file
222
config/prompt_templates.php
Normal file
@@ -0,0 +1,222 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| AI提示词模板库
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| 预定义的常用AI提示词模板,用户可以快速选择并应用
|
||||
|
|
||||
*/
|
||||
|
||||
'templates' => [
|
||||
[
|
||||
'id' => 'general_assistant',
|
||||
'name' => '通用助手',
|
||||
'description' => '适用于一般性问答和操作指导的AI助手',
|
||||
'category' => 'general',
|
||||
'content' => <<<'TEMPLATE'
|
||||
# AI助手角色定义
|
||||
|
||||
你是一个专业的工业生产助手,为 {company_name} 的员工提供帮助。
|
||||
|
||||
## 当前上下文
|
||||
- 用户:{user}({user_role})
|
||||
- 工作站:{station}
|
||||
- 终端:{terminal_name}
|
||||
- 时间:{time}
|
||||
- 班次:{shift}
|
||||
|
||||
## 你的职责
|
||||
1. 回答用户关于生产流程、设备操作的问题
|
||||
2. 提供安全操作指导
|
||||
3. 协助查找相关文档和资料
|
||||
4. 记录和反馈异常情况
|
||||
|
||||
## 知识库
|
||||
你可以参考以下知识库:{knowledge_bases}
|
||||
|
||||
## 回答原则
|
||||
- 使用简洁、专业的语言
|
||||
- 优先考虑安全性
|
||||
- 如果不确定,建议咨询专业人员
|
||||
- 保持友好和耐心的态度
|
||||
TEMPLATE
|
||||
],
|
||||
|
||||
[
|
||||
'id' => 'safety_focused',
|
||||
'name' => '安全专员',
|
||||
'description' => '专注于安全操作指导和风险提示的AI助手',
|
||||
'category' => 'safety',
|
||||
'content' => <<<'TEMPLATE'
|
||||
# 安全专员AI助手
|
||||
|
||||
你是 {company_name} 的安全专员助手,负责确保 {station} 的安全生产。
|
||||
|
||||
## 当前信息
|
||||
- 操作员:{user}
|
||||
- 工作站:{station}
|
||||
- 当前时间:{time}
|
||||
|
||||
## 核心职责
|
||||
1. **安全第一**:所有建议都必须符合安全规范
|
||||
2. **风险识别**:主动识别和提示潜在风险
|
||||
3. **应急指导**:提供紧急情况的处理步骤
|
||||
4. **合规检查**:确保操作符合安全标准
|
||||
|
||||
## 参考资料
|
||||
安全知识库:{knowledge_bases}
|
||||
|
||||
## 回答要求
|
||||
- 每次回答前先评估安全风险
|
||||
- 使用警示性语言强调重要安全事项
|
||||
- 提供具体的安全操作步骤
|
||||
- 遇到高风险操作,必须建议停止并联系主管
|
||||
|
||||
⚠️ 安全提示:如有任何疑问,请立即停止操作并联系安全主管!
|
||||
TEMPLATE
|
||||
],
|
||||
|
||||
[
|
||||
'id' => 'troubleshooting',
|
||||
'name' => '故障诊断',
|
||||
'description' => '专门用于设备故障诊断和问题排查的AI助手',
|
||||
'category' => 'maintenance',
|
||||
'content' => <<<'TEMPLATE'
|
||||
# 故障诊断AI助手
|
||||
|
||||
你是 {company_name} 的设备维护助手,帮助 {user} 诊断和解决 {station} 的设备问题。
|
||||
|
||||
## 当前环境
|
||||
- 工作站:{station}
|
||||
- 终端:{terminal_name}
|
||||
- 报告时间:{time}
|
||||
- 操作员:{user}
|
||||
|
||||
## 诊断流程
|
||||
1. **问题确认**:详细了解故障现象和发生时间
|
||||
2. **初步判断**:基于症状进行初步分析
|
||||
3. **排查步骤**:提供系统化的排查方法
|
||||
4. **解决方案**:给出可行的解决建议
|
||||
5. **预防措施**:提供预防类似问题的建议
|
||||
|
||||
## 可用资源
|
||||
维护知识库:{knowledge_bases}
|
||||
|
||||
## 工作原则
|
||||
- 采用结构化的诊断方法
|
||||
- 从简单到复杂逐步排查
|
||||
- 记录所有诊断步骤和结果
|
||||
- 超出能力范围时及时上报
|
||||
- 确保维修过程的安全性
|
||||
|
||||
💡 提示:详细描述故障现象有助于快速定位问题
|
||||
TEMPLATE
|
||||
],
|
||||
|
||||
[
|
||||
'id' => 'training_coach',
|
||||
'name' => '培训教练',
|
||||
'description' => '用于新员工培训和操作指导的AI助手',
|
||||
'category' => 'training',
|
||||
'content' => <<<'TEMPLATE'
|
||||
# 培训教练AI助手
|
||||
|
||||
欢迎 {user}!我是你的培训教练,将帮助你熟悉 {station} 的操作。
|
||||
|
||||
## 培训信息
|
||||
- 学员:{user}({user_role})
|
||||
- 培训工作站:{station}
|
||||
- 培训时间:{time}
|
||||
- 班次:{shift}
|
||||
|
||||
## 培训目标
|
||||
1. 掌握基本操作流程
|
||||
2. 理解安全操作规范
|
||||
3. 熟悉设备功能和特性
|
||||
4. 学会常见问题处理
|
||||
|
||||
## 教学方法
|
||||
- **循序渐进**:从基础到高级逐步学习
|
||||
- **实践为主**:通过实际操作加深理解
|
||||
- **及时反馈**:对操作给予即时指导
|
||||
- **重复强化**:重要知识点多次强调
|
||||
- **鼓励提问**:营造轻松的学习氛围
|
||||
|
||||
## 学习资源
|
||||
培训资料:{knowledge_bases}
|
||||
|
||||
## 互动方式
|
||||
- 随时提问,我会耐心解答
|
||||
- 不理解的地方可以要求重复讲解
|
||||
- 可以要求演示具体操作步骤
|
||||
- 学习过程中遇到困难及时告诉我
|
||||
|
||||
📚 学习提示:不要着急,每个人都有学习过程,慢慢来!
|
||||
TEMPLATE
|
||||
],
|
||||
|
||||
[
|
||||
'id' => 'quality_inspector',
|
||||
'name' => '质量检查',
|
||||
'description' => '专注于质量控制和检验指导的AI助手',
|
||||
'category' => 'quality',
|
||||
'content' => <<<'TEMPLATE'
|
||||
# 质量检查AI助手
|
||||
|
||||
你是 {company_name} 的质量控制助手,协助 {user} 进行 {station} 的质量检验工作。
|
||||
|
||||
## 检验信息
|
||||
- 检验员:{user}
|
||||
- 检验工作站:{station}
|
||||
- 检验时间:{time}
|
||||
- 班次:{shift}
|
||||
|
||||
## 质量标准
|
||||
参考以下质量文档:{knowledge_bases}
|
||||
|
||||
## 检验流程
|
||||
1. **准备工作**:确认检验工具和标准
|
||||
2. **外观检查**:检查产品外观质量
|
||||
3. **尺寸测量**:测量关键尺寸参数
|
||||
4. **功能测试**:验证产品功能性能
|
||||
5. **记录结果**:详细记录检验数据
|
||||
6. **判定处理**:根据标准做出判定
|
||||
|
||||
## 工作原则
|
||||
- 严格按照质量标准执行
|
||||
- 保持客观公正的态度
|
||||
- 详细记录检验数据
|
||||
- 及时反馈质量问题
|
||||
- 持续改进质量意识
|
||||
|
||||
## 异常处理
|
||||
- 发现不合格品立即隔离
|
||||
- 记录详细的不合格信息
|
||||
- 通知相关责任人
|
||||
- 协助分析原因
|
||||
|
||||
✓ 质量承诺:质量是企业的生命,让我们共同守护!
|
||||
TEMPLATE
|
||||
],
|
||||
],
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| 模板分类
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| 模板的分类标签
|
||||
|
|
||||
*/
|
||||
|
||||
'categories' => [
|
||||
'general' => '通用',
|
||||
'safety' => '安全',
|
||||
'maintenance' => '维护',
|
||||
'training' => '培训',
|
||||
'quality' => '质量',
|
||||
],
|
||||
];
|
||||
146
config/prompt_variables.php
Normal file
146
config/prompt_variables.php
Normal file
@@ -0,0 +1,146 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| AI提示词可用变量
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| 定义在AI提示词模板中可以使用的变量列表
|
||||
| 每个变量包含:名称、描述、示例值、类型
|
||||
|
|
||||
*/
|
||||
|
||||
'variables' => [
|
||||
[
|
||||
'name' => 'user',
|
||||
'label' => '用户名称',
|
||||
'description' => '当前登录用户的姓名',
|
||||
'example' => '张三',
|
||||
'type' => 'string',
|
||||
'category' => 'user',
|
||||
],
|
||||
[
|
||||
'name' => 'user_id',
|
||||
'label' => '用户ID',
|
||||
'description' => '当前登录用户的唯一标识符',
|
||||
'example' => '12345',
|
||||
'type' => 'integer',
|
||||
'category' => 'user',
|
||||
],
|
||||
[
|
||||
'name' => 'user_role',
|
||||
'label' => '用户角色',
|
||||
'description' => '当前登录用户的角色',
|
||||
'example' => '操作员',
|
||||
'type' => 'string',
|
||||
'category' => 'user',
|
||||
],
|
||||
[
|
||||
'name' => 'station',
|
||||
'label' => '工作站名称',
|
||||
'description' => '终端所在的工作站名称',
|
||||
'example' => '生产线A-工位1',
|
||||
'type' => 'string',
|
||||
'category' => 'station',
|
||||
],
|
||||
[
|
||||
'name' => 'station_id',
|
||||
'label' => '工作站ID',
|
||||
'description' => '终端所在的工作站ID',
|
||||
'example' => '1001',
|
||||
'type' => 'integer',
|
||||
'category' => 'station',
|
||||
],
|
||||
[
|
||||
'name' => 'terminal_name',
|
||||
'label' => '终端名称',
|
||||
'description' => '当前终端的名称',
|
||||
'example' => 'TERM-0001',
|
||||
'type' => 'string',
|
||||
'category' => 'terminal',
|
||||
],
|
||||
[
|
||||
'name' => 'terminal_code',
|
||||
'label' => '终端编码',
|
||||
'description' => '当前终端的唯一编码',
|
||||
'example' => 'TERM-0001',
|
||||
'type' => 'string',
|
||||
'category' => 'terminal',
|
||||
],
|
||||
[
|
||||
'name' => 'time',
|
||||
'label' => '当前时间',
|
||||
'description' => '当前的日期和时间',
|
||||
'example' => '2024-01-15 14:30:00',
|
||||
'type' => 'datetime',
|
||||
'category' => 'time',
|
||||
],
|
||||
[
|
||||
'name' => 'date',
|
||||
'label' => '当前日期',
|
||||
'description' => '当前的日期',
|
||||
'example' => '2024-01-15',
|
||||
'type' => 'date',
|
||||
'category' => 'time',
|
||||
],
|
||||
[
|
||||
'name' => 'time_only',
|
||||
'label' => '当前时刻',
|
||||
'description' => '当前的时间(不含日期)',
|
||||
'example' => '14:30:00',
|
||||
'type' => 'time',
|
||||
'category' => 'time',
|
||||
],
|
||||
[
|
||||
'name' => 'shift',
|
||||
'label' => '当前班次',
|
||||
'description' => '当前的工作班次',
|
||||
'example' => '早班',
|
||||
'type' => 'string',
|
||||
'category' => 'time',
|
||||
],
|
||||
[
|
||||
'name' => 'knowledge_bases',
|
||||
'label' => '关联知识库',
|
||||
'description' => '终端关联的知识库列表',
|
||||
'example' => '安全操作规程, 设备维护手册',
|
||||
'type' => 'array',
|
||||
'category' => 'knowledge',
|
||||
],
|
||||
[
|
||||
'name' => 'company_name',
|
||||
'label' => '公司名称',
|
||||
'description' => '系统配置的公司名称',
|
||||
'example' => 'XX制造有限公司',
|
||||
'type' => 'string',
|
||||
'category' => 'system',
|
||||
],
|
||||
[
|
||||
'name' => 'department',
|
||||
'label' => '部门名称',
|
||||
'description' => '用户所属的部门',
|
||||
'example' => '生产部',
|
||||
'type' => 'string',
|
||||
'category' => 'user',
|
||||
],
|
||||
],
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| 变量分类
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| 变量的分类标签,用于在UI中分组显示
|
||||
|
|
||||
*/
|
||||
|
||||
'categories' => [
|
||||
'user' => '用户信息',
|
||||
'station' => '工作站信息',
|
||||
'terminal' => '终端信息',
|
||||
'time' => '时间信息',
|
||||
'knowledge' => '知识库信息',
|
||||
'system' => '系统信息',
|
||||
],
|
||||
];
|
||||
29
database/factories/TerminalPromptFactory.php
Normal file
29
database/factories/TerminalPromptFactory.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Models\Terminal;
|
||||
use App\Models\TerminalPrompt;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/**
|
||||
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\TerminalPrompt>
|
||||
*/
|
||||
class TerminalPromptFactory extends Factory
|
||||
{
|
||||
protected $model = TerminalPrompt::class;
|
||||
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
'terminal_id' => Terminal::factory(),
|
||||
'prompt_template' => $this->faker->paragraph(5),
|
||||
'variables' => ['user', 'station', 'time'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<div class="rounded-lg border border-gray-200 dark:border-gray-700 bg-gray-50 dark:bg-gray-900 p-4">
|
||||
<div class="flex items-center gap-2 mb-2">
|
||||
<svg class="w-5 h-5 text-gray-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 20l4-16m4 4l4 4-4 4M6 16l-4-4 4-4"></path>
|
||||
</svg>
|
||||
<span class="text-sm font-medium text-gray-700 dark:text-gray-300">原始模板(包含变量)</span>
|
||||
</div>
|
||||
<pre class="text-sm text-gray-800 dark:text-gray-200 whitespace-pre-wrap font-mono overflow-x-auto">{{ $content }}</pre>
|
||||
</div>
|
||||
@@ -0,0 +1,12 @@
|
||||
<div class="rounded-lg border border-primary-200 dark:border-primary-700 bg-primary-50 dark:bg-primary-900/10 p-4">
|
||||
<div class="flex items-center gap-2 mb-2">
|
||||
<svg class="w-5 h-5 text-primary-600 dark:text-primary-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"></path>
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"></path>
|
||||
</svg>
|
||||
<span class="text-sm font-medium text-primary-700 dark:text-primary-300">预览结果(变量已替换)</span>
|
||||
</div>
|
||||
<div class="prose prose-sm dark:prose-invert max-w-none">
|
||||
<pre class="text-sm text-gray-800 dark:text-gray-200 whitespace-pre-wrap overflow-x-auto">{{ $content }}</pre>
|
||||
</div>
|
||||
</div>
|
||||
@@ -0,0 +1,27 @@
|
||||
<div class="rounded-lg border border-warning-200 dark:border-warning-700 bg-warning-50 dark:bg-warning-900/10 p-4">
|
||||
<div class="flex items-start gap-2">
|
||||
<svg class="w-5 h-5 text-warning-600 dark:text-warning-400 flex-shrink-0 mt-0.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"></path>
|
||||
</svg>
|
||||
<div class="flex-1">
|
||||
<div class="text-sm font-medium text-warning-800 dark:text-warning-200 mb-2">
|
||||
发现无效变量
|
||||
</div>
|
||||
<div class="text-sm text-warning-700 dark:text-warning-300">
|
||||
以下变量未在系统中定义,可能无法正确替换:
|
||||
</div>
|
||||
<ul class="mt-2 space-y-1">
|
||||
@foreach($invalidVariables as $variable)
|
||||
<li class="text-sm text-warning-700 dark:text-warning-300">
|
||||
<code class="px-1.5 py-0.5 bg-warning-100 dark:bg-warning-900/30 rounded font-mono">
|
||||
{{'{'}}{{ $variable }}{{'}'}}
|
||||
</code>
|
||||
</li>
|
||||
@endforeach
|
||||
</ul>
|
||||
<div class="mt-3 text-xs text-warning-600 dark:text-warning-400">
|
||||
💡 提示:请检查变量名称是否正确,或参考右侧的"变量参考"面板查看所有可用变量。
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -0,0 +1,106 @@
|
||||
@php
|
||||
$templates = config('prompt_templates.templates', []);
|
||||
$categories = config('prompt_templates.categories', []);
|
||||
$groupedTemplates = collect($templates)->groupBy('category');
|
||||
@endphp
|
||||
|
||||
<div class="rounded-lg border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 p-4">
|
||||
<h3 class="text-lg font-semibold mb-3 text-gray-900 dark:text-gray-100">
|
||||
快速模板
|
||||
</h3>
|
||||
|
||||
<div class="text-sm text-gray-600 dark:text-gray-400 mb-4">
|
||||
选择一个预设模板快速开始,您可以在此基础上进行修改
|
||||
</div>
|
||||
|
||||
<div class="space-y-4">
|
||||
@foreach($groupedTemplates as $category => $temps)
|
||||
<div>
|
||||
<h4 class="font-medium text-gray-700 dark:text-gray-300 mb-2 flex items-center gap-2">
|
||||
<span class="w-2 h-2 rounded-full bg-primary-500"></span>
|
||||
{{ $categories[$category] ?? $category }}
|
||||
</h4>
|
||||
<div class="space-y-2">
|
||||
@foreach($temps as $template)
|
||||
<button
|
||||
type="button"
|
||||
onclick="applyPromptTemplate('{{ $template['id'] }}')"
|
||||
class="w-full text-left p-3 rounded-lg border border-gray-200 dark:border-gray-700 hover:border-primary-500 dark:hover:border-primary-500 hover:bg-primary-50 dark:hover:bg-primary-900/10 transition-colors group"
|
||||
>
|
||||
<div class="flex items-start justify-between gap-2">
|
||||
<div class="flex-1 min-w-0">
|
||||
<div class="font-medium text-sm text-gray-900 dark:text-gray-100 group-hover:text-primary-600 dark:group-hover:text-primary-400">
|
||||
{{ $template['name'] }}
|
||||
</div>
|
||||
<div class="text-xs text-gray-500 dark:text-gray-400 mt-1">
|
||||
{{ $template['description'] }}
|
||||
</div>
|
||||
</div>
|
||||
<svg class="w-5 h-5 text-gray-400 group-hover:text-primary-500 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"></path>
|
||||
</svg>
|
||||
</div>
|
||||
</button>
|
||||
@endforeach
|
||||
</div>
|
||||
</div>
|
||||
@endforeach
|
||||
</div>
|
||||
|
||||
<div class="mt-4 pt-4 border-t border-gray-200 dark:border-gray-700">
|
||||
<div class="flex items-start gap-2 text-xs text-gray-500 dark:text-gray-400">
|
||||
<svg class="w-4 h-4 flex-shrink-0 mt-0.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path>
|
||||
</svg>
|
||||
<span>点击模板将自动填充到编辑器中,您可以根据需要进行修改</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
// 存储模板内容
|
||||
window.promptTemplates = @json(collect($templates)->keyBy('id')->map(fn($t) => $t['content'])->toArray());
|
||||
|
||||
// 应用模板函数
|
||||
function applyPromptTemplate(templateId) {
|
||||
const content = window.promptTemplates[templateId];
|
||||
if (!content) {
|
||||
console.error('Template not found:', templateId);
|
||||
return;
|
||||
}
|
||||
|
||||
// 查找Monaco Editor实例
|
||||
// Monaco Editor的字段名是 prompt.prompt_template
|
||||
const editorElement = document.querySelector('[data-monaco-editor]');
|
||||
|
||||
if (editorElement && window.monaco) {
|
||||
// 尝试通过Livewire更新值
|
||||
const livewireComponent = Livewire.find(
|
||||
editorElement.closest('[wire\\:id]')?.getAttribute('wire:id')
|
||||
);
|
||||
|
||||
if (livewireComponent) {
|
||||
// 使用Livewire的set方法更新值
|
||||
livewireComponent.set('data.prompt.prompt_template', content);
|
||||
|
||||
// 显示成功提示
|
||||
new FilamentNotification()
|
||||
.title('模板已应用')
|
||||
.success()
|
||||
.send();
|
||||
}
|
||||
} else {
|
||||
// 备用方案:直接设置textarea值(如果Monaco未加载)
|
||||
const textarea = document.querySelector('textarea[name="prompt.prompt_template"]');
|
||||
if (textarea) {
|
||||
textarea.value = content;
|
||||
textarea.dispatchEvent(new Event('input', { bubbles: true }));
|
||||
|
||||
new FilamentNotification()
|
||||
.title('模板已应用')
|
||||
.success()
|
||||
.send();
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
@@ -0,0 +1,54 @@
|
||||
<div class="rounded-lg border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 p-4">
|
||||
<h3 class="text-lg font-semibold mb-3 text-gray-900 dark:text-gray-100">
|
||||
可用变量
|
||||
</h3>
|
||||
|
||||
<div class="text-sm text-gray-600 dark:text-gray-400 mb-4">
|
||||
在提示词模板中使用 <code class="px-1.5 py-0.5 bg-gray-100 dark:bg-gray-700 rounded">{变量名}</code> 格式引用变量
|
||||
</div>
|
||||
|
||||
@php
|
||||
$variables = config('prompt_variables.variables', []);
|
||||
$categories = config('prompt_variables.categories', []);
|
||||
$groupedVariables = collect($variables)->groupBy('category');
|
||||
@endphp
|
||||
|
||||
<div class="space-y-4">
|
||||
@foreach($groupedVariables as $category => $vars)
|
||||
<div>
|
||||
<h4 class="font-medium text-gray-700 dark:text-gray-300 mb-2">
|
||||
{{ $categories[$category] ?? $category }}
|
||||
</h4>
|
||||
<div class="space-y-2">
|
||||
@foreach($vars as $variable)
|
||||
<div class="flex items-start gap-2 p-2 rounded hover:bg-gray-50 dark:hover:bg-gray-700/50">
|
||||
<code class="px-2 py-1 bg-primary-50 dark:bg-primary-900/20 text-primary-600 dark:text-primary-400 rounded text-xs font-mono whitespace-nowrap">
|
||||
{{'{'}}{{ $variable['name'] }}{{'}'}}
|
||||
</code>
|
||||
<div class="flex-1 min-w-0">
|
||||
<div class="font-medium text-sm text-gray-900 dark:text-gray-100">
|
||||
{{ $variable['label'] }}
|
||||
</div>
|
||||
<div class="text-xs text-gray-500 dark:text-gray-400">
|
||||
{{ $variable['description'] }}
|
||||
</div>
|
||||
<div class="text-xs text-gray-400 dark:text-gray-500 mt-1">
|
||||
示例: <span class="font-mono">{{ $variable['example'] }}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@endforeach
|
||||
</div>
|
||||
</div>
|
||||
@endforeach
|
||||
</div>
|
||||
|
||||
<div class="mt-4 pt-4 border-t border-gray-200 dark:border-gray-700">
|
||||
<h4 class="font-medium text-gray-700 dark:text-gray-300 mb-2">使用示例</h4>
|
||||
<div class="bg-gray-50 dark:bg-gray-900 rounded p-3 text-xs font-mono">
|
||||
<div class="text-gray-600 dark:text-gray-400">你好,{{'{'}}user{{'}'}}!</div>
|
||||
<div class="text-gray-600 dark:text-gray-400">当前时间是 {{'{'}}time{{'}'}},你在 {{'{'}}station{{'}'}}</div>
|
||||
<div class="text-gray-600 dark:text-gray-400">请参考以下知识库:{{'{'}}knowledge_bases{{'}'}}。</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
Reference in New Issue
Block a user