feat: 删除 知识库-终端 关联, 简化 prompt 配置

This commit is contained in:
2026-03-23 15:27:06 +08:00
parent 81a22a2b54
commit 89af7c17f1
23 changed files with 102 additions and 1633 deletions

View File

@@ -1,91 +0,0 @@
<?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)),
];
});
}
}

View File

@@ -131,44 +131,6 @@ class TerminalResource extends Resource
->columns(2)
->description('配置终端的语音唤醒能力'),
Forms\Components\Section::make('知识库关联')
->schema([
Forms\Components\Repeater::make('knowledgeBaseAssociations')
->label('关联知识库')
->relationship('knowledgeBases')
->schema([
Forms\Components\Select::make('id')
->label('知识库')
->options(\App\Models\KnowledgeBase::where('status', 'active')->pluck('name', 'id'))
->required()
->searchable()
->distinct()
->disableOptionsWhenSelectedInSiblingRepeaterItems()
->helperText('选择要关联的知识库'),
Forms\Components\TextInput::make('priority')
->label('优先级')
->numeric()
->default(0)
->required()
->minValue(0)
->helperText('数字越小优先级越高0为最高优先级'),
])
->columns(2)
->reorderable()
->reorderableWithButtons()
->addActionLabel('添加知识库')
->reorderableWithDragAndDrop(false)
->itemLabel(
fn(array $state): ?string =>
\App\Models\KnowledgeBase::find($state['id'])?->name ?? '未选择'
)
->collapsed()
->collapsible()
->helperText('可以关联多个知识库,并设置优先级。拖动或使用按钮调整顺序。'),
])
->description('配置终端可以访问的知识库及其优先级'),
Forms\Components\Section::make('指引关联')
->schema([
Forms\Components\Repeater::make('guideAssociations')
@@ -215,17 +177,13 @@ class TerminalResource extends Resource
->label('提示词模板')
->language('markdown')
->fontSize('14px')
->helperText('编辑AI提示词模板支持使用变量如 {user}, {station}, {time} 等')
->helperText('编辑AI提示词模板支持使用占位符 {station_id}, {user}, {time}由HMI端替换')
->placeholderText('请输入AI提示词模板...')
->disablePreview()
->columnSpan(2),
Forms\Components\Grid::make(1)
->schema([
Forms\Components\Placeholder::make('template_selector')
->label('模板库')
->content(fn() => view('filament.components.prompt-template-selector')),
Forms\Components\Placeholder::make('variable_helper')
->label('变量参考')
->content(fn() => view('filament.components.prompt-variable-helper')),
@@ -233,7 +191,7 @@ class TerminalResource extends Resource
->columnSpan(1),
]),
])
->description('配置终端的AI提示词模板用于指导AI助手的行为')
->description('配置终端的AI提示词模板占位符由HMI端替换')
->collapsible(),
Forms\Components\Section::make('状态信息')

View File

@@ -53,23 +53,6 @@ class ViewTerminal extends ViewRecord
->openUrlInNewTab(),
]),
Infolists\Components\Section::make('知识库关联')
->schema([
Infolists\Components\RepeatableEntry::make('knowledgeBases')
->label('关联的知识库')
->schema([
Infolists\Components\TextEntry::make('name')
->label('知识库名称'),
Infolists\Components\TextEntry::make('pivot.priority')
->label('优先级')
->badge()
->color('success'),
])
->columns(2)
->placeholder('未关联任何知识库'),
])
->collapsible(),
Infolists\Components\Section::make('AI提示词配置')
->schema([
Infolists\Components\TextEntry::make('prompt.prompt_template')

View File

@@ -3,7 +3,6 @@
namespace App\Filament\Widgets;
use App\Models\Terminal;
use App\Models\TerminalKnowledgeBase;
use App\Models\TerminalPrompt;
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;
@@ -17,25 +16,17 @@ class TerminalStatsWidget extends BaseWidget
// 统计终端数据
$totalTerminals = Terminal::count();
$onlineTerminals = Terminal::where('is_online', true)->count();
// 统计知识库关联
$totalKnowledgeBases = TerminalKnowledgeBase::count();
// 统计提示词
$totalPrompts = TerminalPrompt::count();
return [
Stat::make('终端总数', $totalTerminals)
->description("{$onlineTerminals} 个在线")
->descriptionIcon('heroicon-m-computer-desktop')
->color('primary')
->url(route('filament.admin.resources.terminals.index')),
Stat::make('知识库关联', $totalKnowledgeBases)
->description('终端知识库配置数')
->descriptionIcon('heroicon-m-link')
->color('info'),
Stat::make('提示词配置', $totalPrompts)
->description('终端提示词总数')
->descriptionIcon('heroicon-m-chat-bubble-left-right')